blob: 83c6ec7f7634e66f06fafd9d4ec3a4028b49c447 [file] [log] [blame]
Linus Walleijdae5f0a2018-09-25 09:08:48 +02001// SPDX-License-Identifier: GPL-2.0
Andy Shevchenko923a6542017-05-25 16:08:38 +03002#include <linux/bitmap.h>
David Brownelld2876d02008-02-04 22:28:20 -08003#include <linux/kernel.h>
4#include <linux/module.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -07005#include <linux/interrupt.h>
David Brownelld2876d02008-02-04 22:28:20 -08006#include <linux/irq.h>
7#include <linux/spinlock.h>
Alexandre Courbot1a989d02013-02-03 01:29:24 +09008#include <linux/list.h>
David Brownelld8f388d82008-07-25 01:46:07 -07009#include <linux/device.h>
10#include <linux/err.h>
11#include <linux/debugfs.h>
12#include <linux/seq_file.h>
13#include <linux/gpio.h>
Anton Vorontsov391c9702010-06-08 07:48:17 -060014#include <linux/of_gpio.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -070015#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Rafael J. Wysocki7b199812013-11-11 22:41:56 +010017#include <linux/acpi.h>
Alexandre Courbot53e7cac2013-11-16 21:44:52 +090018#include <linux/gpio/driver.h>
Linus Walleij0a6d3152014-07-24 20:08:55 +020019#include <linux/gpio/machine.h>
Jonas Gorskic771c2f2015-10-11 17:34:15 +020020#include <linux/pinctrl/consumer.h>
Linus Walleij3c702e92015-10-21 15:29:53 +020021#include <linux/cdev.h>
22#include <linux/fs.h>
23#include <linux/uaccess.h>
Linus Walleij8b92e172016-05-27 14:24:04 +020024#include <linux/compat.h>
Linus Walleijd7c51b42016-04-26 10:35:29 +020025#include <linux/anon_inodes.h>
Lars-Peter Clausen953b9562016-10-24 13:59:15 +020026#include <linux/file.h>
Linus Walleij61f922d2016-06-02 11:30:15 +020027#include <linux/kfifo.h>
28#include <linux/poll.h>
29#include <linux/timekeeping.h>
Linus Walleij3c702e92015-10-21 15:29:53 +020030#include <uapi/linux/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080031
Mika Westerberg664e3e52014-01-08 12:40:54 +020032#include "gpiolib.h"
33
Uwe Kleine-König3f397c212011-05-20 00:40:19 -060034#define CREATE_TRACE_POINTS
35#include <trace/events/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080036
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070037/* Implementation infrastructure for GPIO interfaces.
David Brownelld2876d02008-02-04 22:28:20 -080038 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070039 * The GPIO programming interface allows for inlining speed-critical
40 * get/set operations for common cases, so that access to SOC-integrated
41 * GPIOs can sometimes cost only an instruction or two per bit.
David Brownelld2876d02008-02-04 22:28:20 -080042 */
43
44
45/* When debugging, extend minimal trust to callers and platform code.
46 * Also emit diagnostic messages that may help initial bringup, when
47 * board setup or driver bugs are most common.
48 *
49 * Otherwise, minimize overhead in what may be bitbanging codepaths.
50 */
51#ifdef DEBUG
52#define extra_checks 1
53#else
54#define extra_checks 0
55#endif
56
Linus Walleijff2b1352015-10-20 11:10:38 +020057/* Device and char device-related information */
58static DEFINE_IDA(gpio_ida);
Linus Walleij3c702e92015-10-21 15:29:53 +020059static dev_t gpio_devt;
60#define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
61static struct bus_type gpio_bus_type = {
62 .name = "gpio",
63};
Linus Walleijff2b1352015-10-20 11:10:38 +020064
Laura Abbott30277432018-05-21 10:57:07 -070065/*
66 * Number of GPIOs to use for the fast path in set array
67 */
68#define FASTPATH_NGPIO CONFIG_GPIOLIB_FASTPATH_LIMIT
69
David Brownelld2876d02008-02-04 22:28:20 -080070/* gpio_lock prevents conflicts during gpio_desc[] table updates.
71 * While any GPIO is requested, its gpio_chip is not removable;
72 * each GPIO's "requested" flag serves as a lock and refcount.
73 */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090074DEFINE_SPINLOCK(gpio_lock);
David Brownelld2876d02008-02-04 22:28:20 -080075
Alexandre Courbotbae48da2013-10-17 10:21:38 -070076static DEFINE_MUTEX(gpio_lookup_lock);
77static LIST_HEAD(gpio_lookup_list);
Linus Walleijff2b1352015-10-20 11:10:38 +020078LIST_HEAD(gpio_devices);
Johan Hovold6d867502015-05-04 17:23:25 +020079
Bartosz Golaszewskia411e812018-04-10 22:30:28 +020080static DEFINE_MUTEX(gpio_machine_hogs_mutex);
81static LIST_HEAD(gpio_machine_hogs);
82
Johan Hovold6d867502015-05-04 17:23:25 +020083static void gpiochip_free_hogs(struct gpio_chip *chip);
Thierry Reding959bc7b2017-11-07 19:15:59 +010084static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
Andrew Lunn39c3fd52017-12-02 18:11:04 +010085 struct lock_class_key *lock_key,
86 struct lock_class_key *request_key);
Johan Hovold6d867502015-05-04 17:23:25 +020087static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
Mika Westerberg79b804c2016-09-20 15:15:21 +030088static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip);
89static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip);
Johan Hovold6d867502015-05-04 17:23:25 +020090
Guenter Roeck159f3cd2016-03-31 08:11:30 -070091static bool gpiolib_initialized;
Johan Hovold6d867502015-05-04 17:23:25 +020092
David Brownelld2876d02008-02-04 22:28:20 -080093static inline void desc_set_label(struct gpio_desc *d, const char *label)
94{
David Brownelld2876d02008-02-04 22:28:20 -080095 d->label = label;
David Brownelld2876d02008-02-04 22:28:20 -080096}
97
Alexandre Courbot372e7222013-02-03 01:29:29 +090098/**
Thierry Reding950d55f52017-07-24 16:57:22 +020099 * gpio_to_desc - Convert a GPIO number to its descriptor
100 * @gpio: global GPIO number
101 *
102 * Returns:
103 * The GPIO descriptor associated with the given GPIO, or %NULL if no GPIO
104 * with the given number exists in the system.
Alexandre Courbot372e7222013-02-03 01:29:29 +0900105 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700106struct gpio_desc *gpio_to_desc(unsigned gpio)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900107{
Linus Walleijff2b1352015-10-20 11:10:38 +0200108 struct gpio_device *gdev;
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900109 unsigned long flags;
110
111 spin_lock_irqsave(&gpio_lock, flags);
112
Linus Walleijff2b1352015-10-20 11:10:38 +0200113 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100114 if (gdev->base <= gpio &&
115 gdev->base + gdev->ngpio > gpio) {
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900116 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100117 return &gdev->descs[gpio - gdev->base];
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900118 }
119 }
120
121 spin_unlock_irqrestore(&gpio_lock, flags);
122
Alexandre Courbot0e9a5ed2014-12-02 23:15:05 +0900123 if (!gpio_is_valid(gpio))
124 WARN(1, "invalid GPIO %d\n", gpio);
125
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900126 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900127}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700128EXPORT_SYMBOL_GPL(gpio_to_desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900129
130/**
Thierry Reding950d55f52017-07-24 16:57:22 +0200131 * gpiochip_get_desc - get the GPIO descriptor corresponding to the given
132 * hardware number for this chip
133 * @chip: GPIO chip
134 * @hwnum: hardware number of the GPIO for this chip
135 *
136 * Returns:
137 * A pointer to the GPIO descriptor or %ERR_PTR(-EINVAL) if no GPIO exists
138 * in the given chip for the specified hardware number.
Linus Walleijd468bf92013-09-24 11:54:38 +0200139 */
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900140struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
141 u16 hwnum)
Linus Walleijd468bf92013-09-24 11:54:38 +0200142{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100143 struct gpio_device *gdev = chip->gpiodev;
144
145 if (hwnum >= gdev->ngpio)
Alexandre Courbotb7d0a282013-12-03 12:31:11 +0900146 return ERR_PTR(-EINVAL);
Linus Walleijd468bf92013-09-24 11:54:38 +0200147
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100148 return &gdev->descs[hwnum];
Linus Walleijd468bf92013-09-24 11:54:38 +0200149}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900150
151/**
Thierry Reding950d55f52017-07-24 16:57:22 +0200152 * desc_to_gpio - convert a GPIO descriptor to the integer namespace
153 * @desc: GPIO descriptor
154 *
Alexandre Courbot372e7222013-02-03 01:29:29 +0900155 * This should disappear in the future but is needed since we still
Thierry Reding950d55f52017-07-24 16:57:22 +0200156 * use GPIO numbers for error messages and sysfs nodes.
157 *
158 * Returns:
159 * The global GPIO number for the GPIO specified by its descriptor.
Alexandre Courbot372e7222013-02-03 01:29:29 +0900160 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700161int desc_to_gpio(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900162{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100163 return desc->gdev->base + (desc - &desc->gdev->descs[0]);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900164}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700165EXPORT_SYMBOL_GPL(desc_to_gpio);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900166
167
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700168/**
169 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
170 * @desc: descriptor to return the chip of
171 */
172struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900173{
Vladimir Zapolskiydd3b9a42017-12-21 18:37:30 +0200174 if (!desc || !desc->gdev)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100175 return NULL;
176 return desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900177}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700178EXPORT_SYMBOL_GPL(gpiod_to_chip);
David Brownelld2876d02008-02-04 22:28:20 -0800179
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700180/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
181static int gpiochip_find_base(int ngpio)
182{
Linus Walleijff2b1352015-10-20 11:10:38 +0200183 struct gpio_device *gdev;
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900184 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700185
Linus Walleijff2b1352015-10-20 11:10:38 +0200186 list_for_each_entry_reverse(gdev, &gpio_devices, list) {
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900187 /* found a free space? */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100188 if (gdev->base + gdev->ngpio <= base)
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900189 break;
190 else
191 /* nope, check the space right before the chip */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100192 base = gdev->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700193 }
194
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900195 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700196 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900197 return base;
198 } else {
199 pr_err("%s: cannot find free range\n", __func__);
200 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700201 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700202}
203
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700204/**
205 * gpiod_get_direction - return the current direction of a GPIO
206 * @desc: GPIO to get the direction of
207 *
Wolfram Sang94fc7302018-01-09 12:35:53 +0100208 * Returns 0 for output, 1 for input, or an error code in case of error.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700209 *
210 * This function may sleep if gpiod_cansleep() is true.
211 */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900212int gpiod_get_direction(struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300213{
Wolfram Sangd0121b82018-07-11 18:33:19 +0200214 struct gpio_chip *chip;
215 unsigned offset;
216 int status;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300217
Alexandre Courbot372e7222013-02-03 01:29:29 +0900218 chip = gpiod_to_chip(desc);
219 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300220
221 if (!chip->get_direction)
Wolfram Sangd0121b82018-07-11 18:33:19 +0200222 return -ENOTSUPP;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300223
Alexandre Courbot372e7222013-02-03 01:29:29 +0900224 status = chip->get_direction(chip, offset);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300225 if (status > 0) {
226 /* GPIOF_DIR_IN, or other positive */
227 status = 1;
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900228 clear_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300229 }
230 if (status == 0) {
231 /* GPIOF_DIR_OUT */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900232 set_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300233 }
234 return status;
235}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700236EXPORT_SYMBOL_GPL(gpiod_get_direction);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300237
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900238/*
239 * Add a new chip to the global chips list, keeping the list of chips sorted
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800240 * by range(means [base, base + ngpio - 1]) order.
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900241 *
242 * Return -EBUSY if the new chip overlaps with some other chip's integer
243 * space.
244 */
Linus Walleijff2b1352015-10-20 11:10:38 +0200245static int gpiodev_add_to_list(struct gpio_device *gdev)
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900246{
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800247 struct gpio_device *prev, *next;
Linus Walleijff2b1352015-10-20 11:10:38 +0200248
249 if (list_empty(&gpio_devices)) {
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800250 /* initial entry in list */
Linus Walleijff2b1352015-10-20 11:10:38 +0200251 list_add_tail(&gdev->list, &gpio_devices);
Sudip Mukherjeee28ecca2015-12-27 19:06:50 +0530252 return 0;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900253 }
254
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800255 next = list_entry(gpio_devices.next, struct gpio_device, list);
256 if (gdev->base + gdev->ngpio <= next->base) {
257 /* add before first entry */
258 list_add(&gdev->list, &gpio_devices);
Julien Grossholtz96098df2016-01-07 16:46:45 -0500259 return 0;
260 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900261
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800262 prev = list_entry(gpio_devices.prev, struct gpio_device, list);
263 if (prev->base + prev->ngpio <= gdev->base) {
264 /* add behind last entry */
265 list_add_tail(&gdev->list, &gpio_devices);
266 return 0;
267 }
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800268
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800269 list_for_each_entry_safe(prev, next, &gpio_devices, list) {
270 /* at the end of the list */
271 if (&next->list == &gpio_devices)
272 break;
273
274 /* add between prev and next */
275 if (prev->base + prev->ngpio <= gdev->base
276 && gdev->base + gdev->ngpio <= next->base) {
277 list_add(&gdev->list, &prev->list);
278 return 0;
279 }
280 }
281
282 dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n");
283 return -EBUSY;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900284}
285
Thierry Reding950d55f52017-07-24 16:57:22 +0200286/*
Linus Walleijf881bab02015-09-23 16:20:43 -0700287 * Convert a GPIO name to its descriptor
288 */
289static struct gpio_desc *gpio_name_to_desc(const char * const name)
290{
Linus Walleijff2b1352015-10-20 11:10:38 +0200291 struct gpio_device *gdev;
Linus Walleijf881bab02015-09-23 16:20:43 -0700292 unsigned long flags;
293
294 spin_lock_irqsave(&gpio_lock, flags);
295
Linus Walleijff2b1352015-10-20 11:10:38 +0200296 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijf881bab02015-09-23 16:20:43 -0700297 int i;
298
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100299 for (i = 0; i != gdev->ngpio; ++i) {
300 struct gpio_desc *desc = &gdev->descs[i];
Linus Walleijf881bab02015-09-23 16:20:43 -0700301
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100302 if (!desc->name || !name)
Linus Walleijf881bab02015-09-23 16:20:43 -0700303 continue;
304
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100305 if (!strcmp(desc->name, name)) {
Linus Walleijf881bab02015-09-23 16:20:43 -0700306 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100307 return desc;
Linus Walleijf881bab02015-09-23 16:20:43 -0700308 }
309 }
310 }
311
312 spin_unlock_irqrestore(&gpio_lock, flags);
313
314 return NULL;
315}
316
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200317/*
318 * Takes the names from gc->names and checks if they are all unique. If they
319 * are, they are assigned to their gpio descriptors.
320 *
Bamvor Jian Zhanged379152015-11-14 16:43:20 +0800321 * Warning if one of the names is already used for a different GPIO.
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200322 */
323static int gpiochip_set_desc_names(struct gpio_chip *gc)
324{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100325 struct gpio_device *gdev = gc->gpiodev;
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200326 int i;
327
328 if (!gc->names)
329 return 0;
330
331 /* First check all names if they are unique */
332 for (i = 0; i != gc->ngpio; ++i) {
333 struct gpio_desc *gpio;
334
335 gpio = gpio_name_to_desc(gc->names[i]);
Linus Walleijf881bab02015-09-23 16:20:43 -0700336 if (gpio)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100337 dev_warn(&gdev->dev,
Linus Walleij34ffd852015-10-20 11:31:54 +0200338 "Detected name collision for GPIO name '%s'\n",
Linus Walleijf881bab02015-09-23 16:20:43 -0700339 gc->names[i]);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200340 }
341
342 /* Then add all names to the GPIO descriptors */
343 for (i = 0; i != gc->ngpio; ++i)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100344 gdev->descs[i].name = gc->names[i];
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200345
346 return 0;
347}
348
Stephen Boyde4371f6e2018-03-23 09:34:50 -0700349static unsigned long *gpiochip_allocate_mask(struct gpio_chip *chip)
350{
351 unsigned long *p;
352
Stephen Boydace569352018-03-23 09:34:51 -0700353 p = kmalloc_array(BITS_TO_LONGS(chip->ngpio), sizeof(*p), GFP_KERNEL);
Stephen Boyde4371f6e2018-03-23 09:34:50 -0700354 if (!p)
355 return NULL;
356
357 /* Assume by default all GPIOs are valid */
358 bitmap_fill(p, chip->ngpio);
359
360 return p;
361}
362
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700363static int gpiochip_init_valid_mask(struct gpio_chip *gpiochip)
364{
365#ifdef CONFIG_OF_GPIO
366 int size;
367 struct device_node *np = gpiochip->of_node;
368
369 size = of_property_count_u32_elems(np, "gpio-reserved-ranges");
370 if (size > 0 && size % 2 == 0)
371 gpiochip->need_valid_mask = true;
372#endif
373
374 if (!gpiochip->need_valid_mask)
375 return 0;
376
377 gpiochip->valid_mask = gpiochip_allocate_mask(gpiochip);
378 if (!gpiochip->valid_mask)
379 return -ENOMEM;
380
381 return 0;
382}
383
384static void gpiochip_free_valid_mask(struct gpio_chip *gpiochip)
385{
386 kfree(gpiochip->valid_mask);
387 gpiochip->valid_mask = NULL;
388}
389
390bool gpiochip_line_is_valid(const struct gpio_chip *gpiochip,
391 unsigned int offset)
392{
393 /* No mask means all valid */
394 if (likely(!gpiochip->valid_mask))
395 return true;
396 return test_bit(offset, gpiochip->valid_mask);
397}
398EXPORT_SYMBOL_GPL(gpiochip_line_is_valid);
399
Linus Walleijd7c51b42016-04-26 10:35:29 +0200400/*
401 * GPIO line handle management
402 */
403
404/**
405 * struct linehandle_state - contains the state of a userspace handle
406 * @gdev: the GPIO device the handle pertains to
407 * @label: consumer label used to tag descriptors
408 * @descs: the GPIO descriptors held by this handle
409 * @numdescs: the number of descriptors held in the descs array
410 */
411struct linehandle_state {
412 struct gpio_device *gdev;
413 const char *label;
414 struct gpio_desc *descs[GPIOHANDLES_MAX];
415 u32 numdescs;
416};
417
Lars-Peter Clausene3e847c2016-10-18 16:54:05 +0200418#define GPIOHANDLE_REQUEST_VALID_FLAGS \
419 (GPIOHANDLE_REQUEST_INPUT | \
420 GPIOHANDLE_REQUEST_OUTPUT | \
421 GPIOHANDLE_REQUEST_ACTIVE_LOW | \
422 GPIOHANDLE_REQUEST_OPEN_DRAIN | \
423 GPIOHANDLE_REQUEST_OPEN_SOURCE)
424
Linus Walleijd7c51b42016-04-26 10:35:29 +0200425static long linehandle_ioctl(struct file *filep, unsigned int cmd,
426 unsigned long arg)
427{
428 struct linehandle_state *lh = filep->private_data;
429 void __user *ip = (void __user *)arg;
430 struct gpiohandle_data ghd;
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +0200431 DECLARE_BITMAP(vals, GPIOHANDLES_MAX);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200432 int i;
433
434 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
Bartosz Golaszewski2b955b32018-07-16 10:34:24 +0200435 /* NOTE: It's ok to read values of output lines. */
Lukas Wunnereec1d562017-10-12 12:40:10 +0200436 int ret = gpiod_get_array_value_complex(false,
437 true,
438 lh->numdescs,
439 lh->descs,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +0200440 NULL,
Lukas Wunnereec1d562017-10-12 12:40:10 +0200441 vals);
442 if (ret)
443 return ret;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200444
Lars-Peter Clausen3eded5d2016-10-18 16:54:02 +0200445 memset(&ghd, 0, sizeof(ghd));
Lukas Wunnereec1d562017-10-12 12:40:10 +0200446 for (i = 0; i < lh->numdescs; i++)
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +0200447 ghd.values[i] = test_bit(i, vals);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200448
449 if (copy_to_user(ip, &ghd, sizeof(ghd)))
450 return -EFAULT;
451
452 return 0;
453 } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
Bartosz Golaszewskie5332d52018-07-16 10:34:23 +0200454 /*
455 * All line descriptors were created at once with the same
456 * flags so just check if the first one is really output.
457 */
458 if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags))
459 return -EPERM;
460
Linus Walleijd7c51b42016-04-26 10:35:29 +0200461 if (copy_from_user(&ghd, ip, sizeof(ghd)))
462 return -EFAULT;
463
464 /* Clamp all values to [0,1] */
465 for (i = 0; i < lh->numdescs; i++)
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +0200466 __assign_bit(i, vals, ghd.values[i]);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200467
468 /* Reuse the array setting function */
Laura Abbott30277432018-05-21 10:57:07 -0700469 return gpiod_set_array_value_complex(false,
Linus Walleijd7c51b42016-04-26 10:35:29 +0200470 true,
471 lh->numdescs,
472 lh->descs,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +0200473 NULL,
Linus Walleijd7c51b42016-04-26 10:35:29 +0200474 vals);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200475 }
476 return -EINVAL;
477}
478
479#ifdef CONFIG_COMPAT
480static long linehandle_ioctl_compat(struct file *filep, unsigned int cmd,
481 unsigned long arg)
482{
483 return linehandle_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
484}
485#endif
486
487static int linehandle_release(struct inode *inode, struct file *filep)
488{
489 struct linehandle_state *lh = filep->private_data;
490 struct gpio_device *gdev = lh->gdev;
491 int i;
492
493 for (i = 0; i < lh->numdescs; i++)
494 gpiod_free(lh->descs[i]);
495 kfree(lh->label);
496 kfree(lh);
497 put_device(&gdev->dev);
498 return 0;
499}
500
501static const struct file_operations linehandle_fileops = {
502 .release = linehandle_release,
503 .owner = THIS_MODULE,
504 .llseek = noop_llseek,
505 .unlocked_ioctl = linehandle_ioctl,
506#ifdef CONFIG_COMPAT
507 .compat_ioctl = linehandle_ioctl_compat,
508#endif
509};
510
511static int linehandle_create(struct gpio_device *gdev, void __user *ip)
512{
513 struct gpiohandle_request handlereq;
514 struct linehandle_state *lh;
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200515 struct file *file;
Timur Tabiab3dbcf2018-03-29 13:29:12 -0500516 int fd, i, count = 0, ret;
Bartosz Golaszewski418ee8e2017-10-16 11:32:29 +0200517 u32 lflags;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200518
519 if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
520 return -EFAULT;
521 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
522 return -EINVAL;
523
Bartosz Golaszewski418ee8e2017-10-16 11:32:29 +0200524 lflags = handlereq.flags;
525
526 /* Return an error if an unknown flag is set */
527 if (lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
528 return -EINVAL;
529
Bartosz Golaszewski588fc3b2017-11-15 16:47:43 +0100530 /*
531 * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
532 * the hardware actually supports enabling both at the same time the
533 * electrical result would be disastrous.
534 */
535 if ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) &&
536 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
537 return -EINVAL;
538
Bartosz Golaszewski609aaf62017-10-16 11:32:30 +0200539 /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
540 if (!(lflags & GPIOHANDLE_REQUEST_OUTPUT) &&
541 ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
542 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
543 return -EINVAL;
544
Linus Walleijd7c51b42016-04-26 10:35:29 +0200545 lh = kzalloc(sizeof(*lh), GFP_KERNEL);
546 if (!lh)
547 return -ENOMEM;
548 lh->gdev = gdev;
549 get_device(&gdev->dev);
550
551 /* Make sure this is terminated */
552 handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
553 if (strlen(handlereq.consumer_label)) {
554 lh->label = kstrdup(handlereq.consumer_label,
555 GFP_KERNEL);
556 if (!lh->label) {
557 ret = -ENOMEM;
558 goto out_free_lh;
559 }
560 }
561
562 /* Request each GPIO */
563 for (i = 0; i < handlereq.lines; i++) {
564 u32 offset = handlereq.lineoffsets[i];
Linus Walleijd7c51b42016-04-26 10:35:29 +0200565 struct gpio_desc *desc;
566
Lars-Peter Clausene405f9f2016-10-18 16:54:01 +0200567 if (offset >= gdev->ngpio) {
568 ret = -EINVAL;
569 goto out_free_descs;
570 }
571
Linus Walleijd7c51b42016-04-26 10:35:29 +0200572 desc = &gdev->descs[offset];
573 ret = gpiod_request(desc, lh->label);
574 if (ret)
575 goto out_free_descs;
576 lh->descs[i] = desc;
Timur Tabiab3dbcf2018-03-29 13:29:12 -0500577 count = i;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200578
579 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
580 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
581 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
582 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
583 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
584 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
585
Andrew Jefferye10f72b2017-11-30 14:25:24 +1030586 ret = gpiod_set_transitory(desc, false);
587 if (ret < 0)
588 goto out_free_descs;
589
Linus Walleijd7c51b42016-04-26 10:35:29 +0200590 /*
591 * Lines have to be requested explicitly for input
592 * or output, else the line will be treated "as is".
593 */
594 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
595 int val = !!handlereq.default_values[i];
596
597 ret = gpiod_direction_output(desc, val);
598 if (ret)
599 goto out_free_descs;
600 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
601 ret = gpiod_direction_input(desc);
602 if (ret)
603 goto out_free_descs;
604 }
605 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
606 offset);
607 }
Linus Walleije2f608b2016-06-18 10:56:43 +0200608 /* Let i point at the last handle */
609 i--;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200610 lh->numdescs = handlereq.lines;
611
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200612 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200613 if (fd < 0) {
614 ret = fd;
615 goto out_free_descs;
616 }
617
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200618 file = anon_inode_getfile("gpio-linehandle",
619 &linehandle_fileops,
620 lh,
621 O_RDONLY | O_CLOEXEC);
622 if (IS_ERR(file)) {
623 ret = PTR_ERR(file);
624 goto out_put_unused_fd;
625 }
626
Linus Walleijd7c51b42016-04-26 10:35:29 +0200627 handlereq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200628 if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200629 /*
630 * fput() will trigger the release() callback, so do not go onto
631 * the regular error cleanup path here.
632 */
633 fput(file);
634 put_unused_fd(fd);
635 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +0200636 }
Linus Walleijd7c51b42016-04-26 10:35:29 +0200637
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200638 fd_install(fd, file);
639
Linus Walleijd7c51b42016-04-26 10:35:29 +0200640 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
641 lh->numdescs);
642
643 return 0;
644
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200645out_put_unused_fd:
646 put_unused_fd(fd);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200647out_free_descs:
Timur Tabiab3dbcf2018-03-29 13:29:12 -0500648 for (i = 0; i < count; i++)
Linus Walleijd7c51b42016-04-26 10:35:29 +0200649 gpiod_free(lh->descs[i]);
650 kfree(lh->label);
651out_free_lh:
652 kfree(lh);
653 put_device(&gdev->dev);
654 return ret;
655}
656
Linus Walleij61f922d2016-06-02 11:30:15 +0200657/*
658 * GPIO line event management
659 */
660
661/**
662 * struct lineevent_state - contains the state of a userspace event
663 * @gdev: the GPIO device the event pertains to
664 * @label: consumer label used to tag descriptors
665 * @desc: the GPIO descriptor held by this event
666 * @eflags: the event flags this line was requested with
667 * @irq: the interrupt that trigger in response to events on this GPIO
668 * @wait: wait queue that handles blocking reads of events
669 * @events: KFIFO for the GPIO events
670 * @read_lock: mutex lock to protect reads from colliding with adding
671 * new events to the FIFO
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100672 * @timestamp: cache for the timestamp storing it between hardirq
673 * and IRQ thread, used to bring the timestamp close to the actual
674 * event
Linus Walleij61f922d2016-06-02 11:30:15 +0200675 */
676struct lineevent_state {
677 struct gpio_device *gdev;
678 const char *label;
679 struct gpio_desc *desc;
680 u32 eflags;
681 int irq;
682 wait_queue_head_t wait;
683 DECLARE_KFIFO(events, struct gpioevent_data, 16);
684 struct mutex read_lock;
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100685 u64 timestamp;
Linus Walleij61f922d2016-06-02 11:30:15 +0200686};
687
Lars-Peter Clausenac7dbb92016-10-18 16:54:06 +0200688#define GPIOEVENT_REQUEST_VALID_FLAGS \
689 (GPIOEVENT_REQUEST_RISING_EDGE | \
690 GPIOEVENT_REQUEST_FALLING_EDGE)
691
Al Viroafc9a422017-07-03 06:39:46 -0400692static __poll_t lineevent_poll(struct file *filep,
Linus Walleij61f922d2016-06-02 11:30:15 +0200693 struct poll_table_struct *wait)
694{
695 struct lineevent_state *le = filep->private_data;
Al Viroafc9a422017-07-03 06:39:46 -0400696 __poll_t events = 0;
Linus Walleij61f922d2016-06-02 11:30:15 +0200697
698 poll_wait(filep, &le->wait, wait);
699
700 if (!kfifo_is_empty(&le->events))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800701 events = EPOLLIN | EPOLLRDNORM;
Linus Walleij61f922d2016-06-02 11:30:15 +0200702
703 return events;
704}
705
706
707static ssize_t lineevent_read(struct file *filep,
708 char __user *buf,
709 size_t count,
710 loff_t *f_ps)
711{
712 struct lineevent_state *le = filep->private_data;
713 unsigned int copied;
714 int ret;
715
716 if (count < sizeof(struct gpioevent_data))
717 return -EINVAL;
718
719 do {
720 if (kfifo_is_empty(&le->events)) {
721 if (filep->f_flags & O_NONBLOCK)
722 return -EAGAIN;
723
724 ret = wait_event_interruptible(le->wait,
725 !kfifo_is_empty(&le->events));
726 if (ret)
727 return ret;
728 }
729
730 if (mutex_lock_interruptible(&le->read_lock))
731 return -ERESTARTSYS;
732 ret = kfifo_to_user(&le->events, buf, count, &copied);
733 mutex_unlock(&le->read_lock);
734
735 if (ret)
736 return ret;
737
738 /*
739 * If we couldn't read anything from the fifo (a different
740 * thread might have been faster) we either return -EAGAIN if
741 * the file descriptor is non-blocking, otherwise we go back to
742 * sleep and wait for more data to arrive.
743 */
744 if (copied == 0 && (filep->f_flags & O_NONBLOCK))
745 return -EAGAIN;
746
747 } while (copied == 0);
748
749 return copied;
750}
751
752static int lineevent_release(struct inode *inode, struct file *filep)
753{
754 struct lineevent_state *le = filep->private_data;
755 struct gpio_device *gdev = le->gdev;
756
757 free_irq(le->irq, le);
758 gpiod_free(le->desc);
759 kfree(le->label);
760 kfree(le);
761 put_device(&gdev->dev);
762 return 0;
763}
764
765static long lineevent_ioctl(struct file *filep, unsigned int cmd,
766 unsigned long arg)
767{
768 struct lineevent_state *le = filep->private_data;
769 void __user *ip = (void __user *)arg;
770 struct gpiohandle_data ghd;
771
772 /*
773 * We can get the value for an event line but not set it,
774 * because it is input by definition.
775 */
776 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
777 int val;
778
Lars-Peter Clausend82aa4a2016-10-18 16:54:04 +0200779 memset(&ghd, 0, sizeof(ghd));
780
Linus Walleij61f922d2016-06-02 11:30:15 +0200781 val = gpiod_get_value_cansleep(le->desc);
782 if (val < 0)
783 return val;
784 ghd.values[0] = val;
785
786 if (copy_to_user(ip, &ghd, sizeof(ghd)))
787 return -EFAULT;
788
789 return 0;
790 }
791 return -EINVAL;
792}
793
794#ifdef CONFIG_COMPAT
795static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd,
796 unsigned long arg)
797{
798 return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
799}
800#endif
801
802static const struct file_operations lineevent_fileops = {
803 .release = lineevent_release,
804 .read = lineevent_read,
805 .poll = lineevent_poll,
806 .owner = THIS_MODULE,
807 .llseek = noop_llseek,
808 .unlocked_ioctl = lineevent_ioctl,
809#ifdef CONFIG_COMPAT
810 .compat_ioctl = lineevent_ioctl_compat,
811#endif
812};
813
Ben Dooks33265b12016-06-17 16:03:13 +0100814static irqreturn_t lineevent_irq_thread(int irq, void *p)
Linus Walleij61f922d2016-06-02 11:30:15 +0200815{
816 struct lineevent_state *le = p;
817 struct gpioevent_data ge;
Uwe Kleine-Königfa388692018-08-20 08:32:53 +0200818 int ret;
Linus Walleij61f922d2016-06-02 11:30:15 +0200819
Linus Walleij24bd3ef2018-01-22 13:19:28 +0100820 /* Do not leak kernel stack to userspace */
821 memset(&ge, 0, sizeof(ge));
822
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100823 ge.timestamp = le->timestamp;
Linus Walleij61f922d2016-06-02 11:30:15 +0200824
Bartosz Golaszewskiad537b82017-06-23 13:45:16 +0200825 if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
826 && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
Uwe Kleine-Königfa388692018-08-20 08:32:53 +0200827 int level = gpiod_get_value_cansleep(le->desc);
Linus Walleij61f922d2016-06-02 11:30:15 +0200828 if (level)
829 /* Emit low-to-high event */
830 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
831 else
832 /* Emit high-to-low event */
833 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Uwe Kleine-Königfa388692018-08-20 08:32:53 +0200834 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200835 /* Emit low-to-high event */
836 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
Uwe Kleine-Königfa388692018-08-20 08:32:53 +0200837 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200838 /* Emit high-to-low event */
839 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Arnd Bergmannbc0207a2016-06-16 11:02:41 +0200840 } else {
841 return IRQ_NONE;
Linus Walleij61f922d2016-06-02 11:30:15 +0200842 }
843
844 ret = kfifo_put(&le->events, ge);
845 if (ret != 0)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800846 wake_up_poll(&le->wait, EPOLLIN);
Linus Walleij61f922d2016-06-02 11:30:15 +0200847
848 return IRQ_HANDLED;
849}
850
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100851static irqreturn_t lineevent_irq_handler(int irq, void *p)
852{
853 struct lineevent_state *le = p;
854
855 /*
856 * Just store the timestamp in hardirq context so we get it as
857 * close in time as possible to the actual event.
858 */
859 le->timestamp = ktime_get_real_ns();
860
861 return IRQ_WAKE_THREAD;
862}
863
Linus Walleij61f922d2016-06-02 11:30:15 +0200864static int lineevent_create(struct gpio_device *gdev, void __user *ip)
865{
866 struct gpioevent_request eventreq;
867 struct lineevent_state *le;
868 struct gpio_desc *desc;
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200869 struct file *file;
Linus Walleij61f922d2016-06-02 11:30:15 +0200870 u32 offset;
871 u32 lflags;
872 u32 eflags;
873 int fd;
874 int ret;
875 int irqflags = 0;
876
877 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
878 return -EFAULT;
879
880 le = kzalloc(sizeof(*le), GFP_KERNEL);
881 if (!le)
882 return -ENOMEM;
883 le->gdev = gdev;
884 get_device(&gdev->dev);
885
886 /* Make sure this is terminated */
887 eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
888 if (strlen(eventreq.consumer_label)) {
889 le->label = kstrdup(eventreq.consumer_label,
890 GFP_KERNEL);
891 if (!le->label) {
892 ret = -ENOMEM;
893 goto out_free_le;
894 }
895 }
896
897 offset = eventreq.lineoffset;
898 lflags = eventreq.handleflags;
899 eflags = eventreq.eventflags;
900
Lars-Peter Clausenb8b0e3d2016-10-18 16:54:03 +0200901 if (offset >= gdev->ngpio) {
902 ret = -EINVAL;
903 goto out_free_label;
904 }
905
Lars-Peter Clausenac7dbb92016-10-18 16:54:06 +0200906 /* Return an error if a unknown flag is set */
907 if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
908 (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) {
909 ret = -EINVAL;
910 goto out_free_label;
911 }
912
Linus Walleij61f922d2016-06-02 11:30:15 +0200913 /* This is just wrong: we don't look for events on output lines */
914 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
915 ret = -EINVAL;
916 goto out_free_label;
917 }
918
919 desc = &gdev->descs[offset];
920 ret = gpiod_request(desc, le->label);
921 if (ret)
Uwe Kleine-Königf001cc32018-04-16 13:17:53 +0200922 goto out_free_label;
Linus Walleij61f922d2016-06-02 11:30:15 +0200923 le->desc = desc;
924 le->eflags = eflags;
925
926 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
927 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
928 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
929 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
930 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
931 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
932
933 ret = gpiod_direction_input(desc);
934 if (ret)
935 goto out_free_desc;
936
937 le->irq = gpiod_to_irq(desc);
938 if (le->irq <= 0) {
939 ret = -ENODEV;
940 goto out_free_desc;
941 }
942
943 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
944 irqflags |= IRQF_TRIGGER_RISING;
945 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
946 irqflags |= IRQF_TRIGGER_FALLING;
947 irqflags |= IRQF_ONESHOT;
Linus Walleij61f922d2016-06-02 11:30:15 +0200948
949 INIT_KFIFO(le->events);
950 init_waitqueue_head(&le->wait);
951 mutex_init(&le->read_lock);
952
953 /* Request a thread to read the events */
954 ret = request_threaded_irq(le->irq,
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100955 lineevent_irq_handler,
Linus Walleij61f922d2016-06-02 11:30:15 +0200956 lineevent_irq_thread,
957 irqflags,
958 le->label,
959 le);
960 if (ret)
961 goto out_free_desc;
962
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200963 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleij61f922d2016-06-02 11:30:15 +0200964 if (fd < 0) {
965 ret = fd;
966 goto out_free_irq;
967 }
968
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200969 file = anon_inode_getfile("gpio-event",
970 &lineevent_fileops,
971 le,
972 O_RDONLY | O_CLOEXEC);
973 if (IS_ERR(file)) {
974 ret = PTR_ERR(file);
975 goto out_put_unused_fd;
976 }
977
Linus Walleij61f922d2016-06-02 11:30:15 +0200978 eventreq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200979 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200980 /*
981 * fput() will trigger the release() callback, so do not go onto
982 * the regular error cleanup path here.
983 */
984 fput(file);
985 put_unused_fd(fd);
986 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +0200987 }
Linus Walleij61f922d2016-06-02 11:30:15 +0200988
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200989 fd_install(fd, file);
990
Linus Walleij61f922d2016-06-02 11:30:15 +0200991 return 0;
992
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200993out_put_unused_fd:
994 put_unused_fd(fd);
Linus Walleij61f922d2016-06-02 11:30:15 +0200995out_free_irq:
996 free_irq(le->irq, le);
997out_free_desc:
998 gpiod_free(le->desc);
999out_free_label:
1000 kfree(le->label);
1001out_free_le:
1002 kfree(le);
1003 put_device(&gdev->dev);
1004 return ret;
1005}
1006
Thierry Reding950d55f52017-07-24 16:57:22 +02001007/*
Linus Walleij3c702e92015-10-21 15:29:53 +02001008 * gpio_ioctl() - ioctl handler for the GPIO chardev
1009 */
1010static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1011{
1012 struct gpio_device *gdev = filp->private_data;
1013 struct gpio_chip *chip = gdev->chip;
Linus Walleij8b92e172016-05-27 14:24:04 +02001014 void __user *ip = (void __user *)arg;
Linus Walleij3c702e92015-10-21 15:29:53 +02001015
1016 /* We fail any subsequent ioctl():s when the chip is gone */
1017 if (!chip)
1018 return -ENODEV;
1019
Linus Walleij521a2ad2016-02-12 22:25:22 +01001020 /* Fill in the struct and pass to userspace */
Linus Walleij3c702e92015-10-21 15:29:53 +02001021 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
Linus Walleij521a2ad2016-02-12 22:25:22 +01001022 struct gpiochip_info chipinfo;
1023
Lars-Peter Clausen0f4bbb22016-10-18 16:54:00 +02001024 memset(&chipinfo, 0, sizeof(chipinfo));
1025
Linus Walleij3c702e92015-10-21 15:29:53 +02001026 strncpy(chipinfo.name, dev_name(&gdev->dev),
1027 sizeof(chipinfo.name));
1028 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
Linus Walleijdf4878e2016-02-12 14:48:23 +01001029 strncpy(chipinfo.label, gdev->label,
1030 sizeof(chipinfo.label));
1031 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001032 chipinfo.lines = gdev->ngpio;
Linus Walleij3c702e92015-10-21 15:29:53 +02001033 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
1034 return -EFAULT;
1035 return 0;
Linus Walleij521a2ad2016-02-12 22:25:22 +01001036 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
1037 struct gpioline_info lineinfo;
1038 struct gpio_desc *desc;
1039
1040 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
1041 return -EFAULT;
Lars-Peter Clausen1f1cc452016-10-18 16:53:59 +02001042 if (lineinfo.line_offset >= gdev->ngpio)
Linus Walleij521a2ad2016-02-12 22:25:22 +01001043 return -EINVAL;
1044
1045 desc = &gdev->descs[lineinfo.line_offset];
1046 if (desc->name) {
1047 strncpy(lineinfo.name, desc->name,
1048 sizeof(lineinfo.name));
1049 lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
1050 } else {
1051 lineinfo.name[0] = '\0';
1052 }
1053 if (desc->label) {
Linus Walleij214338e2016-02-25 21:01:48 +01001054 strncpy(lineinfo.consumer, desc->label,
1055 sizeof(lineinfo.consumer));
1056 lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +01001057 } else {
Linus Walleij214338e2016-02-25 21:01:48 +01001058 lineinfo.consumer[0] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +01001059 }
1060
1061 /*
1062 * Userspace only need to know that the kernel is using
1063 * this GPIO so it can't use it.
1064 */
1065 lineinfo.flags = 0;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001066 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
1067 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
1068 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
1069 test_bit(FLAG_EXPORT, &desc->flags) ||
1070 test_bit(FLAG_SYSFS, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001071 lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001072 if (test_bit(FLAG_IS_OUT, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001073 lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001074 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001075 lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001076 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001077 lineinfo.flags |= GPIOLINE_FLAG_OPEN_DRAIN;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001078 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001079 lineinfo.flags |= GPIOLINE_FLAG_OPEN_SOURCE;
1080
1081 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
1082 return -EFAULT;
1083 return 0;
Linus Walleijd7c51b42016-04-26 10:35:29 +02001084 } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
1085 return linehandle_create(gdev, ip);
Linus Walleij61f922d2016-06-02 11:30:15 +02001086 } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
1087 return lineevent_create(gdev, ip);
Linus Walleij3c702e92015-10-21 15:29:53 +02001088 }
1089 return -EINVAL;
1090}
1091
Linus Walleij8b92e172016-05-27 14:24:04 +02001092#ifdef CONFIG_COMPAT
1093static long gpio_ioctl_compat(struct file *filp, unsigned int cmd,
1094 unsigned long arg)
1095{
1096 return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
1097}
1098#endif
1099
Linus Walleij3c702e92015-10-21 15:29:53 +02001100/**
1101 * gpio_chrdev_open() - open the chardev for ioctl operations
1102 * @inode: inode for this chardev
1103 * @filp: file struct for storing private data
1104 * Returns 0 on success
1105 */
1106static int gpio_chrdev_open(struct inode *inode, struct file *filp)
1107{
1108 struct gpio_device *gdev = container_of(inode->i_cdev,
1109 struct gpio_device, chrdev);
1110
1111 /* Fail on open if the backing gpiochip is gone */
Stephen Boydfb505742017-01-09 11:47:44 -08001112 if (!gdev->chip)
Linus Walleij3c702e92015-10-21 15:29:53 +02001113 return -ENODEV;
1114 get_device(&gdev->dev);
1115 filp->private_data = gdev;
Lars-Peter Clausenf4e81c52016-11-30 13:05:21 +01001116
1117 return nonseekable_open(inode, filp);
Linus Walleij3c702e92015-10-21 15:29:53 +02001118}
1119
1120/**
1121 * gpio_chrdev_release() - close chardev after ioctl operations
1122 * @inode: inode for this chardev
1123 * @filp: file struct for storing private data
1124 * Returns 0 on success
1125 */
1126static int gpio_chrdev_release(struct inode *inode, struct file *filp)
1127{
1128 struct gpio_device *gdev = container_of(inode->i_cdev,
1129 struct gpio_device, chrdev);
1130
Linus Walleij3c702e92015-10-21 15:29:53 +02001131 put_device(&gdev->dev);
1132 return 0;
1133}
1134
1135
1136static const struct file_operations gpio_fileops = {
1137 .release = gpio_chrdev_release,
1138 .open = gpio_chrdev_open,
1139 .owner = THIS_MODULE,
Lars-Peter Clausenf4e81c52016-11-30 13:05:21 +01001140 .llseek = no_llseek,
Linus Walleij3c702e92015-10-21 15:29:53 +02001141 .unlocked_ioctl = gpio_ioctl,
Linus Walleij8b92e172016-05-27 14:24:04 +02001142#ifdef CONFIG_COMPAT
1143 .compat_ioctl = gpio_ioctl_compat,
1144#endif
Linus Walleij3c702e92015-10-21 15:29:53 +02001145};
1146
Linus Walleijff2b1352015-10-20 11:10:38 +02001147static void gpiodevice_release(struct device *dev)
1148{
1149 struct gpio_device *gdev = dev_get_drvdata(dev);
1150
1151 list_del(&gdev->list);
1152 ida_simple_remove(&gpio_ida, gdev->id);
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +01001153 kfree_const(gdev->label);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001154 kfree(gdev->descs);
Linus Walleij9efd9e62016-02-09 14:27:42 +01001155 kfree(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001156}
1157
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001158static int gpiochip_setup_dev(struct gpio_device *gdev)
1159{
1160 int status;
1161
1162 cdev_init(&gdev->chrdev, &gpio_fileops);
1163 gdev->chrdev.owner = THIS_MODULE;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001164 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001165
1166 status = cdev_device_add(&gdev->chrdev, &gdev->dev);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001167 if (status)
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001168 return status;
1169
1170 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
1171 MAJOR(gpio_devt), gdev->id);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001172
1173 status = gpiochip_sysfs_register(gdev);
1174 if (status)
1175 goto err_remove_device;
1176
1177 /* From this point, the .release() function cleans up gpio_device */
1178 gdev->dev.release = gpiodevice_release;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001179 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
1180 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
1181 dev_name(&gdev->dev), gdev->chip->label ? : "generic");
1182
1183 return 0;
1184
1185err_remove_device:
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001186 cdev_device_del(&gdev->chrdev, &gdev->dev);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001187 return status;
1188}
1189
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02001190static void gpiochip_machine_hog(struct gpio_chip *chip, struct gpiod_hog *hog)
1191{
1192 struct gpio_desc *desc;
1193 int rv;
1194
1195 desc = gpiochip_get_desc(chip, hog->chip_hwnum);
1196 if (IS_ERR(desc)) {
1197 pr_err("%s: unable to get GPIO desc: %ld\n",
1198 __func__, PTR_ERR(desc));
1199 return;
1200 }
1201
Dan Carpenterba3efdf2018-05-01 10:36:39 +03001202 if (test_bit(FLAG_IS_HOGGED, &desc->flags))
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02001203 return;
1204
1205 rv = gpiod_hog(desc, hog->line_name, hog->lflags, hog->dflags);
1206 if (rv)
1207 pr_err("%s: unable to hog GPIO line (%s:%u): %d\n",
1208 __func__, chip->label, hog->chip_hwnum, rv);
1209}
1210
1211static void machine_gpiochip_add(struct gpio_chip *chip)
1212{
1213 struct gpiod_hog *hog;
1214
1215 mutex_lock(&gpio_machine_hogs_mutex);
1216
1217 list_for_each_entry(hog, &gpio_machine_hogs, list) {
1218 if (!strcmp(chip->label, hog->chip_label))
1219 gpiochip_machine_hog(chip, hog);
1220 }
1221
1222 mutex_unlock(&gpio_machine_hogs_mutex);
1223}
1224
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001225static void gpiochip_setup_devs(void)
1226{
1227 struct gpio_device *gdev;
1228 int err;
1229
1230 list_for_each_entry(gdev, &gpio_devices, list) {
1231 err = gpiochip_setup_dev(gdev);
1232 if (err)
1233 pr_err("%s: Failed to initialize gpio device (%d)\n",
1234 dev_name(&gdev->dev), err);
1235 }
1236}
1237
Thierry Reding959bc7b2017-11-07 19:15:59 +01001238int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001239 struct lock_class_key *lock_key,
1240 struct lock_class_key *request_key)
David Brownelld2876d02008-02-04 22:28:20 -08001241{
1242 unsigned long flags;
1243 int status = 0;
Linus Walleijff2b1352015-10-20 11:10:38 +02001244 unsigned i;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001245 int base = chip->base;
Linus Walleijff2b1352015-10-20 11:10:38 +02001246 struct gpio_device *gdev;
David Brownelld2876d02008-02-04 22:28:20 -08001247
Linus Walleijff2b1352015-10-20 11:10:38 +02001248 /*
1249 * First: allocate and populate the internal stat container, and
1250 * set up the struct device.
1251 */
Josh Cartwright969f07b2016-02-17 16:44:15 -06001252 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
Linus Walleijff2b1352015-10-20 11:10:38 +02001253 if (!gdev)
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001254 return -ENOMEM;
Linus Walleij3c702e92015-10-21 15:29:53 +02001255 gdev->dev.bus = &gpio_bus_type;
Linus Walleijff2b1352015-10-20 11:10:38 +02001256 gdev->chip = chip;
1257 chip->gpiodev = gdev;
1258 if (chip->parent) {
1259 gdev->dev.parent = chip->parent;
1260 gdev->dev.of_node = chip->parent->of_node;
Thierry Redingacc6e332016-07-05 14:11:14 +02001261 }
1262
Linus Walleijff2b1352015-10-20 11:10:38 +02001263#ifdef CONFIG_OF_GPIO
1264 /* If the gpiochip has an assigned OF node this takes precedence */
Thierry Redingacc6e332016-07-05 14:11:14 +02001265 if (chip->of_node)
1266 gdev->dev.of_node = chip->of_node;
Biju Das6ff04972018-08-06 10:48:01 +01001267 else
1268 chip->of_node = gdev->dev.of_node;
Linus Walleijff2b1352015-10-20 11:10:38 +02001269#endif
Thierry Redingacc6e332016-07-05 14:11:14 +02001270
Linus Walleijff2b1352015-10-20 11:10:38 +02001271 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
1272 if (gdev->id < 0) {
1273 status = gdev->id;
1274 goto err_free_gdev;
1275 }
1276 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
1277 device_initialize(&gdev->dev);
1278 dev_set_drvdata(&gdev->dev, gdev);
1279 if (chip->parent && chip->parent->driver)
1280 gdev->owner = chip->parent->driver->owner;
1281 else if (chip->owner)
1282 /* TODO: remove chip->owner */
1283 gdev->owner = chip->owner;
1284 else
1285 gdev->owner = THIS_MODULE;
David Brownelld2876d02008-02-04 22:28:20 -08001286
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001287 gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001288 if (!gdev->descs) {
Linus Walleijff2b1352015-10-20 11:10:38 +02001289 status = -ENOMEM;
1290 goto err_free_gdev;
1291 }
1292
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001293 if (chip->ngpio == 0) {
1294 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
Linus Walleijff2b1352015-10-20 11:10:38 +02001295 status = -EINVAL;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001296 goto err_free_descs;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001297 }
Linus Walleijdf4878e2016-02-12 14:48:23 +01001298
Laura Abbott30277432018-05-21 10:57:07 -07001299 if (chip->ngpio > FASTPATH_NGPIO)
1300 chip_warn(chip, "line cnt %u is greater than fast path cnt %u\n",
1301 chip->ngpio, FASTPATH_NGPIO);
1302
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +01001303 gdev->label = kstrdup_const(chip->label ?: "unknown", GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001304 if (!gdev->label) {
1305 status = -ENOMEM;
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001306 goto err_free_descs;
Linus Walleijdf4878e2016-02-12 14:48:23 +01001307 }
1308
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001309 gdev->ngpio = chip->ngpio;
Linus Walleij43c54ec2016-02-11 11:37:48 +01001310 gdev->data = data;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001311
David Brownelld2876d02008-02-04 22:28:20 -08001312 spin_lock_irqsave(&gpio_lock, flags);
1313
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001314 /*
1315 * TODO: this allocates a Linux GPIO number base in the global
1316 * GPIO numberspace for this chip. In the long run we want to
1317 * get *rid* of this numberspace and use only descriptors, but
1318 * it may be a pipe dream. It will not happen before we get rid
1319 * of the sysfs interface anyways.
1320 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001321 if (base < 0) {
1322 base = gpiochip_find_base(chip->ngpio);
1323 if (base < 0) {
1324 status = base;
Johan Hovold225fce82015-01-12 17:12:25 +01001325 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001326 goto err_free_label;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001327 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001328 /*
1329 * TODO: it should not be necessary to reflect the assigned
1330 * base outside of the GPIO subsystem. Go over drivers and
1331 * see if anyone makes use of this, else drop this and assign
1332 * a poison instead.
1333 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001334 chip->base = base;
1335 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001336 gdev->base = base;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001337
Linus Walleijff2b1352015-10-20 11:10:38 +02001338 status = gpiodev_add_to_list(gdev);
Johan Hovold05aa5202015-01-12 17:12:26 +01001339 if (status) {
1340 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001341 goto err_free_label;
Johan Hovold05aa5202015-01-12 17:12:26 +01001342 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001343
Linus Walleij545ebd92016-05-30 17:11:59 +02001344 spin_unlock_irqrestore(&gpio_lock, flags);
1345
Linus Walleijff2b1352015-10-20 11:10:38 +02001346 for (i = 0; i < chip->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001347 struct gpio_desc *desc = &gdev->descs[i];
David Brownelld8f388d82008-07-25 01:46:07 -07001348
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001349 desc->gdev = gdev;
Timur Tabi1ca2a922017-12-20 13:10:31 -06001350
1351 /* REVISIT: most hardware initializes GPIOs as inputs (often
1352 * with pullups enabled) so power usage is minimized. Linux
1353 * code should set the gpio direction first thing; but until
1354 * it does, and in case chip->get_direction is not set, we may
1355 * expose the wrong direction in sysfs.
Johan Hovold05aa5202015-01-12 17:12:26 +01001356 */
Timur Tabi1ca2a922017-12-20 13:10:31 -06001357 desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0;
David Brownelld2876d02008-02-04 22:28:20 -08001358 }
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001359
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301360#ifdef CONFIG_PINCTRL
Linus Walleij20ec3e32016-02-11 11:03:06 +01001361 INIT_LIST_HEAD(&gdev->pin_ranges);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301362#endif
1363
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001364 status = gpiochip_set_desc_names(chip);
1365 if (status)
1366 goto err_remove_from_list;
1367
Mika Westerberg79b804c2016-09-20 15:15:21 +03001368 status = gpiochip_irqchip_init_valid_mask(chip);
1369 if (status)
1370 goto err_remove_from_list;
1371
Stephen Boyd726cb3b2018-03-23 09:34:52 -07001372 status = gpiochip_init_valid_mask(chip);
1373 if (status)
1374 goto err_remove_irqchip_mask;
1375
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001376 status = gpiochip_add_irqchip(chip, lock_key, request_key);
Thierry Redinge0d89722017-11-07 19:15:54 +01001377 if (status)
1378 goto err_remove_chip;
1379
Tomeu Vizoso28355f82015-07-14 10:29:54 +02001380 status = of_gpiochip_add(chip);
1381 if (status)
1382 goto err_remove_chip;
1383
Mika Westerberg664e3e52014-01-08 12:40:54 +02001384 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001385
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02001386 machine_gpiochip_add(chip);
1387
Linus Walleij3c702e92015-10-21 15:29:53 +02001388 /*
1389 * By first adding the chardev, and then adding the device,
1390 * we get a device node entry in sysfs under
1391 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1392 * coldplug of device nodes and other udev business.
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001393 * We can do this only if gpiolib has been initialized.
1394 * Otherwise, defer until later.
Linus Walleij3c702e92015-10-21 15:29:53 +02001395 */
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001396 if (gpiolib_initialized) {
1397 status = gpiochip_setup_dev(gdev);
1398 if (status)
1399 goto err_remove_chip;
1400 }
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001401 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001402
Johan Hovold225fce82015-01-12 17:12:25 +01001403err_remove_chip:
1404 acpi_gpiochip_remove(chip);
Johan Hovold6d867502015-05-04 17:23:25 +02001405 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +01001406 of_gpiochip_remove(chip);
Stephen Boyd726cb3b2018-03-23 09:34:52 -07001407 gpiochip_free_valid_mask(chip);
1408err_remove_irqchip_mask:
Mika Westerberg79b804c2016-09-20 15:15:21 +03001409 gpiochip_irqchip_free_valid_mask(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001410err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +01001411 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001412 list_del(&gdev->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001413 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001414err_free_label:
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +01001415 kfree_const(gdev->label);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001416err_free_descs:
1417 kfree(gdev->descs);
Linus Walleijff2b1352015-10-20 11:10:38 +02001418err_free_gdev:
1419 ida_simple_remove(&gpio_ida, gdev->id);
David Brownelld2876d02008-02-04 22:28:20 -08001420 /* failures here can mean systems won't boot... */
Marcel Ziswiler1777fc92018-07-20 09:54:49 +02001421 pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__,
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001422 gdev->base, gdev->base + gdev->ngpio - 1,
Marcel Ziswiler1777fc92018-07-20 09:54:49 +02001423 chip->label ? : "generic", status);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001424 kfree(gdev);
David Brownelld2876d02008-02-04 22:28:20 -08001425 return status;
1426}
Thierry Reding959bc7b2017-11-07 19:15:59 +01001427EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);
David Brownelld2876d02008-02-04 22:28:20 -08001428
1429/**
Linus Walleij43c54ec2016-02-11 11:37:48 +01001430 * gpiochip_get_data() - get per-subdriver data for the chip
Thierry Reding950d55f52017-07-24 16:57:22 +02001431 * @chip: GPIO chip
1432 *
1433 * Returns:
1434 * The per-subdriver data for the chip.
Linus Walleij43c54ec2016-02-11 11:37:48 +01001435 */
1436void *gpiochip_get_data(struct gpio_chip *chip)
1437{
1438 return chip->gpiodev->data;
1439}
1440EXPORT_SYMBOL_GPL(gpiochip_get_data);
1441
1442/**
David Brownelld2876d02008-02-04 22:28:20 -08001443 * gpiochip_remove() - unregister a gpio_chip
1444 * @chip: the chip to unregister
1445 *
1446 * A gpio_chip with any GPIOs still requested may not be removed.
1447 */
abdoulaye berthee1db1702014-07-05 18:28:50 +02001448void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -08001449{
Linus Walleijff2b1352015-10-20 11:10:38 +02001450 struct gpio_device *gdev = chip->gpiodev;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001451 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001452 unsigned long flags;
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001453 unsigned i;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001454 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -08001455
Linus Walleijff2b1352015-10-20 11:10:38 +02001456 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
Linus Walleijafbc4f32016-02-09 13:21:06 +01001457 gpiochip_sysfs_unregister(gdev);
Geert Uytterhoeven5018ada2016-12-19 18:29:23 +01001458 gpiochip_free_hogs(chip);
Bamvor Jian Zhangbd203bd2016-02-20 13:13:19 +08001459 /* Numb the device, cancelling all outstanding operations */
1460 gdev->chip = NULL;
Johan Hovold00acc3d2015-01-12 17:12:27 +01001461 gpiochip_irqchip_remove(chip);
Mika Westerberg6072b9d2014-03-10 14:54:53 +02001462 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +01001463 gpiochip_remove_pin_ranges(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001464 of_gpiochip_remove(chip);
Stephen Boyd726cb3b2018-03-23 09:34:52 -07001465 gpiochip_free_valid_mask(chip);
Linus Walleij43c54ec2016-02-11 11:37:48 +01001466 /*
1467 * We accept no more calls into the driver from this point, so
1468 * NULL the driver data pointer
1469 */
1470 gdev->data = NULL;
Anton Vorontsov391c9702010-06-08 07:48:17 -06001471
Johan Hovold6798aca2015-01-12 17:12:28 +01001472 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001473 for (i = 0; i < gdev->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001474 desc = &gdev->descs[i];
Johan Hovoldfab28b82015-05-04 17:10:27 +02001475 if (test_bit(FLAG_REQUESTED, &desc->flags))
1476 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -08001477 }
David Brownelld2876d02008-02-04 22:28:20 -08001478 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001479
Johan Hovoldfab28b82015-05-04 17:10:27 +02001480 if (requested)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001481 dev_crit(&gdev->dev,
Linus Walleij58383c782015-11-04 09:56:26 +01001482 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +02001483
Linus Walleijff2b1352015-10-20 11:10:38 +02001484 /*
1485 * The gpiochip side puts its use of the device to rest here:
1486 * if there are no userspace clients, the chardev and device will
1487 * be removed, else it will be dangling until the last user is
1488 * gone.
1489 */
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001490 cdev_device_del(&gdev->chrdev, &gdev->dev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001491 put_device(&gdev->dev);
David Brownelld2876d02008-02-04 22:28:20 -08001492}
1493EXPORT_SYMBOL_GPL(gpiochip_remove);
1494
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301495static void devm_gpio_chip_release(struct device *dev, void *res)
1496{
1497 struct gpio_chip *chip = *(struct gpio_chip **)res;
1498
1499 gpiochip_remove(chip);
1500}
1501
1502static int devm_gpio_chip_match(struct device *dev, void *res, void *data)
1503
1504{
1505 struct gpio_chip **r = res;
1506
1507 if (!r || !*r) {
1508 WARN_ON(!r || !*r);
1509 return 0;
1510 }
1511
1512 return *r == data;
1513}
1514
1515/**
Jonathan Neuschäfer689fd022017-12-21 17:56:34 +01001516 * devm_gpiochip_add_data() - Resource manager gpiochip_add_data()
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301517 * @dev: the device pointer on which irq_chip belongs to.
1518 * @chip: the chip to register, with chip->base initialized
Thierry Reding950d55f52017-07-24 16:57:22 +02001519 * @data: driver-private data associated with this chip
1520 *
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301521 * Context: potentially before irqs will work
1522 *
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301523 * The gpio chip automatically be released when the device is unbound.
Thierry Reding950d55f52017-07-24 16:57:22 +02001524 *
1525 * Returns:
1526 * A negative errno if the chip can't be registered, such as because the
1527 * chip->base is invalid or already associated with a different chip.
1528 * Otherwise it returns zero as a success code.
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301529 */
1530int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
1531 void *data)
1532{
1533 struct gpio_chip **ptr;
1534 int ret;
1535
1536 ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
1537 GFP_KERNEL);
1538 if (!ptr)
1539 return -ENOMEM;
1540
1541 ret = gpiochip_add_data(chip, data);
1542 if (ret < 0) {
1543 devres_free(ptr);
1544 return ret;
1545 }
1546
1547 *ptr = chip;
1548 devres_add(dev, ptr);
1549
1550 return 0;
1551}
1552EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
1553
1554/**
1555 * devm_gpiochip_remove() - Resource manager of gpiochip_remove()
1556 * @dev: device for which which resource was allocated
1557 * @chip: the chip to remove
1558 *
1559 * A gpio_chip with any GPIOs still requested may not be removed.
1560 */
1561void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip)
1562{
1563 int ret;
1564
1565 ret = devres_release(dev, devm_gpio_chip_release,
1566 devm_gpio_chip_match, chip);
Christophe JAILLET3988d662017-01-27 12:56:42 +01001567 WARN_ON(ret);
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301568}
1569EXPORT_SYMBOL_GPL(devm_gpiochip_remove);
1570
Grant Likely594fa262010-06-08 07:48:16 -06001571/**
1572 * gpiochip_find() - iterator for locating a specific gpio_chip
1573 * @data: data to pass to match function
Thierry Reding950d55f52017-07-24 16:57:22 +02001574 * @match: Callback function to check gpio_chip
Grant Likely594fa262010-06-08 07:48:16 -06001575 *
1576 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1577 * determined by a user supplied @match callback. The callback should return
1578 * 0 if the device doesn't match and non-zero if it does. If the callback is
1579 * non-zero, this function will return to the caller and not iterate over any
1580 * more gpio_chips.
1581 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001582struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001583 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001584 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001585{
Linus Walleijff2b1352015-10-20 11:10:38 +02001586 struct gpio_device *gdev;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001587 struct gpio_chip *chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -06001588 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001589
1590 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001591 list_for_each_entry(gdev, &gpio_devices, list)
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001592 if (gdev->chip && match(gdev->chip, data)) {
1593 chip = gdev->chip;
Grant Likely594fa262010-06-08 07:48:16 -06001594 break;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001595 }
Linus Walleijff2b1352015-10-20 11:10:38 +02001596
Grant Likely594fa262010-06-08 07:48:16 -06001597 spin_unlock_irqrestore(&gpio_lock, flags);
1598
1599 return chip;
1600}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001601EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001602
Alexandre Courbot79697ef2013-11-16 21:39:32 +09001603static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1604{
1605 const char *name = data;
1606
1607 return !strcmp(chip->label, name);
1608}
1609
1610static struct gpio_chip *find_chip_by_name(const char *name)
1611{
1612 return gpiochip_find((void *)name, gpiochip_match_name);
1613}
1614
Linus Walleij14250522014-03-25 10:40:18 +01001615#ifdef CONFIG_GPIOLIB_IRQCHIP
1616
1617/*
1618 * The following is irqchip helper code for gpiochips.
1619 */
1620
Mika Westerberg79b804c2016-09-20 15:15:21 +03001621static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1622{
Thierry Redingdc7b0382017-11-07 19:15:52 +01001623 if (!gpiochip->irq.need_valid_mask)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001624 return 0;
1625
Stephen Boyde4371f6e2018-03-23 09:34:50 -07001626 gpiochip->irq.valid_mask = gpiochip_allocate_mask(gpiochip);
Thierry Redingdc7b0382017-11-07 19:15:52 +01001627 if (!gpiochip->irq.valid_mask)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001628 return -ENOMEM;
1629
Mika Westerberg79b804c2016-09-20 15:15:21 +03001630 return 0;
1631}
1632
1633static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1634{
Thierry Redingdc7b0382017-11-07 19:15:52 +01001635 kfree(gpiochip->irq.valid_mask);
1636 gpiochip->irq.valid_mask = NULL;
Mika Westerberg79b804c2016-09-20 15:15:21 +03001637}
1638
Stephen Boyd64ff2c82018-01-09 17:58:46 -08001639bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
1640 unsigned int offset)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001641{
Stephen Boyd726cb3b2018-03-23 09:34:52 -07001642 if (!gpiochip_line_is_valid(gpiochip, offset))
1643 return false;
Mika Westerberg79b804c2016-09-20 15:15:21 +03001644 /* No mask means all valid */
Thierry Redingdc7b0382017-11-07 19:15:52 +01001645 if (likely(!gpiochip->irq.valid_mask))
Mika Westerberg79b804c2016-09-20 15:15:21 +03001646 return true;
Thierry Redingdc7b0382017-11-07 19:15:52 +01001647 return test_bit(offset, gpiochip->irq.valid_mask);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001648}
Stephen Boyd64ff2c82018-01-09 17:58:46 -08001649EXPORT_SYMBOL_GPL(gpiochip_irqchip_irq_valid);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001650
Linus Walleij14250522014-03-25 10:40:18 +01001651/**
Linus Walleijd245b3f2016-11-24 10:57:25 +01001652 * gpiochip_set_cascaded_irqchip() - connects a cascaded irqchip to a gpiochip
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001653 * @gpiochip: the gpiochip to set the irqchip chain to
1654 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01001655 * @parent_irq: the irq number corresponding to the parent IRQ for this
1656 * chained irqchip
1657 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001658 * coming out of the gpiochip. If the interrupt is nested rather than
1659 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +01001660 */
Linus Walleijd245b3f2016-11-24 10:57:25 +01001661static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gpiochip,
1662 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001663 unsigned int parent_irq,
Linus Walleijd245b3f2016-11-24 10:57:25 +01001664 irq_flow_handler_t parent_handler)
Linus Walleij14250522014-03-25 10:40:18 +01001665{
Linus Walleij83141a72014-09-26 13:50:12 +02001666 unsigned int offset;
1667
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001668 if (!gpiochip->irq.domain) {
Linus Walleij83141a72014-09-26 13:50:12 +02001669 chip_err(gpiochip, "called %s before setting up irqchip\n",
1670 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +02001671 return;
1672 }
1673
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001674 if (parent_handler) {
1675 if (gpiochip->can_sleep) {
1676 chip_err(gpiochip,
Andy Shevchenkob1911712018-07-03 03:39:03 +03001677 "you cannot have chained interrupts on a chip that may sleep\n");
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001678 return;
1679 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001680 /*
1681 * The parent irqchip is already using the chip_data for this
1682 * irqchip, so our callbacks simply use the handler_data.
1683 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +02001684 irq_set_chained_handler_and_data(parent_irq, parent_handler,
1685 gpiochip);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001686
Thierry Reding39e5f092017-11-07 19:15:50 +01001687 gpiochip->irq.parents = &parent_irq;
1688 gpiochip->irq.num_parents = 1;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001689 }
Linus Walleij83141a72014-09-26 13:50:12 +02001690
1691 /* Set the parent IRQ for all affected IRQs */
Mika Westerberg79b804c2016-09-20 15:15:21 +03001692 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1693 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1694 continue;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001695 irq_set_parent(irq_find_mapping(gpiochip->irq.domain, offset),
Linus Walleij83141a72014-09-26 13:50:12 +02001696 parent_irq);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001697 }
Linus Walleij14250522014-03-25 10:40:18 +01001698}
Linus Walleijd245b3f2016-11-24 10:57:25 +01001699
1700/**
1701 * gpiochip_set_chained_irqchip() - connects a chained irqchip to a gpiochip
1702 * @gpiochip: the gpiochip to set the irqchip chain to
1703 * @irqchip: the irqchip to chain to the gpiochip
1704 * @parent_irq: the irq number corresponding to the parent IRQ for this
1705 * chained irqchip
1706 * @parent_handler: the parent interrupt handler for the accumulated IRQ
1707 * coming out of the gpiochip. If the interrupt is nested rather than
1708 * cascaded, pass NULL in this handler argument
1709 */
1710void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
1711 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001712 unsigned int parent_irq,
Linus Walleijd245b3f2016-11-24 10:57:25 +01001713 irq_flow_handler_t parent_handler)
1714{
Thierry Reding60ed54c2017-11-07 19:15:57 +01001715 if (gpiochip->irq.threaded) {
1716 chip_err(gpiochip, "tried to chain a threaded gpiochip\n");
1717 return;
1718 }
1719
Linus Walleijd245b3f2016-11-24 10:57:25 +01001720 gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
1721 parent_handler);
1722}
Linus Walleij14250522014-03-25 10:40:18 +01001723EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
1724
1725/**
Linus Walleijd245b3f2016-11-24 10:57:25 +01001726 * gpiochip_set_nested_irqchip() - connects a nested irqchip to a gpiochip
1727 * @gpiochip: the gpiochip to set the irqchip nested handler to
1728 * @irqchip: the irqchip to nest to the gpiochip
1729 * @parent_irq: the irq number corresponding to the parent IRQ for this
1730 * nested irqchip
1731 */
1732void gpiochip_set_nested_irqchip(struct gpio_chip *gpiochip,
1733 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001734 unsigned int parent_irq)
Linus Walleijd245b3f2016-11-24 10:57:25 +01001735{
Linus Walleijd245b3f2016-11-24 10:57:25 +01001736 gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
1737 NULL);
1738}
1739EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip);
1740
1741/**
Linus Walleij14250522014-03-25 10:40:18 +01001742 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1743 * @d: the irqdomain used by this irqchip
1744 * @irq: the global irq number used by this GPIO irqchip irq
1745 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1746 *
1747 * This function will set up the mapping for a certain IRQ line on a
1748 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1749 * stored inside the gpiochip.
1750 */
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001751int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1752 irq_hw_number_t hwirq)
Linus Walleij14250522014-03-25 10:40:18 +01001753{
1754 struct gpio_chip *chip = d->host_data;
Thierry Redinge0d89722017-11-07 19:15:54 +01001755 int err = 0;
Linus Walleij14250522014-03-25 10:40:18 +01001756
Grygorii Strashkodc749a02017-07-21 11:49:00 -05001757 if (!gpiochip_irqchip_irq_valid(chip, hwirq))
1758 return -ENXIO;
1759
Linus Walleij14250522014-03-25 10:40:18 +01001760 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001761 /*
1762 * This lock class tells lockdep that GPIO irqs are in a different
1763 * category than their parents, so it won't report false recursion.
1764 */
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001765 irq_set_lockdep_class(irq, chip->irq.lock_key, chip->irq.request_key);
Thierry Redingc7a0aa52017-11-07 19:15:48 +01001766 irq_set_chip_and_handler(irq, chip->irq.chip, chip->irq.handler);
Linus Walleijd245b3f2016-11-24 10:57:25 +01001767 /* Chips that use nested thread handlers have them marked */
Thierry Reding60ed54c2017-11-07 19:15:57 +01001768 if (chip->irq.threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02001769 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +01001770 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -05001771
Thierry Redinge0d89722017-11-07 19:15:54 +01001772 if (chip->irq.num_parents == 1)
1773 err = irq_set_parent(irq, chip->irq.parents[0]);
1774 else if (chip->irq.map)
1775 err = irq_set_parent(irq, chip->irq.map[hwirq]);
1776
1777 if (err < 0)
1778 return err;
1779
Linus Walleij1333b902014-04-23 16:45:12 +02001780 /*
1781 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1782 * is passed as default type.
1783 */
Thierry Reding3634eeb2017-11-07 19:15:49 +01001784 if (chip->irq.default_type != IRQ_TYPE_NONE)
1785 irq_set_irq_type(irq, chip->irq.default_type);
Linus Walleij14250522014-03-25 10:40:18 +01001786
1787 return 0;
1788}
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001789EXPORT_SYMBOL_GPL(gpiochip_irq_map);
Linus Walleij14250522014-03-25 10:40:18 +01001790
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001791void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
Linus Walleijc3626fd2014-03-28 20:42:01 +01001792{
Linus Walleij1c8732b2014-04-09 13:34:39 +02001793 struct gpio_chip *chip = d->host_data;
1794
Thierry Reding60ed54c2017-11-07 19:15:57 +01001795 if (chip->irq.threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02001796 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001797 irq_set_chip_and_handler(irq, NULL, NULL);
1798 irq_set_chip_data(irq, NULL);
1799}
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001800EXPORT_SYMBOL_GPL(gpiochip_irq_unmap);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001801
Linus Walleij14250522014-03-25 10:40:18 +01001802static const struct irq_domain_ops gpiochip_domain_ops = {
1803 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +01001804 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +01001805 /* Virtually all GPIO irqchips are twocell:ed */
1806 .xlate = irq_domain_xlate_twocell,
1807};
1808
Linus Walleij14250522014-03-25 10:40:18 +01001809static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
1810{
Grygorii Strashkodc749a02017-07-21 11:49:00 -05001811 if (!gpiochip_irqchip_irq_valid(chip, offset))
1812 return -ENXIO;
Thierry Redinge0d89722017-11-07 19:15:54 +01001813
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001814 return irq_create_mapping(chip->irq.domain, offset);
Linus Walleij14250522014-03-25 10:40:18 +01001815}
1816
Hans Verkuil4e6b8232018-09-08 11:23:14 +02001817static int gpiochip_irq_reqres(struct irq_data *d)
1818{
1819 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1820
1821 return gpiochip_reqres_irq(chip, d->hwirq);
1822}
1823
1824static void gpiochip_irq_relres(struct irq_data *d)
1825{
1826 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1827
1828 gpiochip_relres_irq(chip, d->hwirq);
1829}
1830
Hans Verkuil461c1a72018-09-08 11:23:17 +02001831static void gpiochip_irq_enable(struct irq_data *d)
1832{
1833 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1834
1835 gpiochip_enable_irq(chip, d->hwirq);
1836 if (chip->irq.irq_enable)
1837 chip->irq.irq_enable(d);
1838 else
1839 chip->irq.chip->irq_unmask(d);
1840}
1841
1842static void gpiochip_irq_disable(struct irq_data *d)
1843{
1844 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1845
1846 if (chip->irq.irq_disable)
1847 chip->irq.irq_disable(d);
1848 else
1849 chip->irq.chip->irq_mask(d);
1850 gpiochip_disable_irq(chip, d->hwirq);
1851}
1852
Hans Verkuilca620f22018-09-08 11:23:15 +02001853static void gpiochip_set_irq_hooks(struct gpio_chip *gpiochip)
1854{
1855 struct irq_chip *irqchip = gpiochip->irq.chip;
1856
1857 if (!irqchip->irq_request_resources &&
1858 !irqchip->irq_release_resources) {
1859 irqchip->irq_request_resources = gpiochip_irq_reqres;
1860 irqchip->irq_release_resources = gpiochip_irq_relres;
1861 }
Hans Verkuil461c1a72018-09-08 11:23:17 +02001862 if (WARN_ON(gpiochip->irq.irq_enable))
1863 return;
Hans Verkuil171948e2018-09-14 10:36:39 +02001864 /* Check if the irqchip already has this hook... */
1865 if (irqchip->irq_enable == gpiochip_irq_enable) {
1866 /*
1867 * ...and if so, give a gentle warning that this is bad
1868 * practice.
1869 */
1870 chip_info(gpiochip,
1871 "detected irqchip that is shared with multiple gpiochips: please fix the driver.\n");
1872 return;
1873 }
Hans Verkuil461c1a72018-09-08 11:23:17 +02001874 gpiochip->irq.irq_enable = irqchip->irq_enable;
1875 gpiochip->irq.irq_disable = irqchip->irq_disable;
1876 irqchip->irq_enable = gpiochip_irq_enable;
1877 irqchip->irq_disable = gpiochip_irq_disable;
Hans Verkuilca620f22018-09-08 11:23:15 +02001878}
1879
Linus Walleij14250522014-03-25 10:40:18 +01001880/**
Thierry Redinge0d89722017-11-07 19:15:54 +01001881 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
1882 * @gpiochip: the GPIO chip to add the IRQ chip to
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001883 * @lock_key: lockdep class for IRQ lock
1884 * @request_key: lockdep class for IRQ request
Thierry Redinge0d89722017-11-07 19:15:54 +01001885 */
Thierry Reding959bc7b2017-11-07 19:15:59 +01001886static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001887 struct lock_class_key *lock_key,
1888 struct lock_class_key *request_key)
Thierry Redinge0d89722017-11-07 19:15:54 +01001889{
1890 struct irq_chip *irqchip = gpiochip->irq.chip;
1891 const struct irq_domain_ops *ops;
1892 struct device_node *np;
1893 unsigned int type;
1894 unsigned int i;
1895
1896 if (!irqchip)
1897 return 0;
1898
1899 if (gpiochip->irq.parent_handler && gpiochip->can_sleep) {
Andy Shevchenkob1911712018-07-03 03:39:03 +03001900 chip_err(gpiochip, "you cannot have chained interrupts on a chip that may sleep\n");
Thierry Redinge0d89722017-11-07 19:15:54 +01001901 return -EINVAL;
1902 }
1903
1904 np = gpiochip->gpiodev->dev.of_node;
1905 type = gpiochip->irq.default_type;
1906
1907 /*
1908 * Specifying a default trigger is a terrible idea if DT or ACPI is
1909 * used to configure the interrupts, as you may end up with
1910 * conflicting triggers. Tell the user, and reset to NONE.
1911 */
1912 if (WARN(np && type != IRQ_TYPE_NONE,
1913 "%s: Ignoring %u default trigger\n", np->full_name, type))
1914 type = IRQ_TYPE_NONE;
1915
1916 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
1917 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1918 "Ignoring %u default trigger\n", type);
1919 type = IRQ_TYPE_NONE;
1920 }
1921
1922 gpiochip->to_irq = gpiochip_to_irq;
1923 gpiochip->irq.default_type = type;
Thierry Reding959bc7b2017-11-07 19:15:59 +01001924 gpiochip->irq.lock_key = lock_key;
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001925 gpiochip->irq.request_key = request_key;
Thierry Redinge0d89722017-11-07 19:15:54 +01001926
1927 if (gpiochip->irq.domain_ops)
1928 ops = gpiochip->irq.domain_ops;
1929 else
1930 ops = &gpiochip_domain_ops;
1931
1932 gpiochip->irq.domain = irq_domain_add_simple(np, gpiochip->ngpio,
Thierry Reding8302cf52017-11-07 19:15:58 +01001933 gpiochip->irq.first,
1934 ops, gpiochip);
Thierry Redinge0d89722017-11-07 19:15:54 +01001935 if (!gpiochip->irq.domain)
1936 return -EINVAL;
1937
Thierry Redinge0d89722017-11-07 19:15:54 +01001938 if (gpiochip->irq.parent_handler) {
1939 void *data = gpiochip->irq.parent_handler_data ?: gpiochip;
1940
1941 for (i = 0; i < gpiochip->irq.num_parents; i++) {
1942 /*
1943 * The parent IRQ chip is already using the chip_data
1944 * for this IRQ chip, so our callbacks simply use the
1945 * handler_data.
1946 */
1947 irq_set_chained_handler_and_data(gpiochip->irq.parents[i],
1948 gpiochip->irq.parent_handler,
1949 data);
1950 }
Thierry Redinge0d89722017-11-07 19:15:54 +01001951 }
1952
Hans Verkuilca620f22018-09-08 11:23:15 +02001953 gpiochip_set_irq_hooks(gpiochip);
1954
Thierry Redinge0d89722017-11-07 19:15:54 +01001955 acpi_gpiochip_request_interrupts(gpiochip);
1956
1957 return 0;
1958}
1959
1960/**
Linus Walleij14250522014-03-25 10:40:18 +01001961 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1962 * @gpiochip: the gpiochip to remove the irqchip from
1963 *
1964 * This is called only from gpiochip_remove()
1965 */
1966static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
1967{
Hans Verkuilca620f22018-09-08 11:23:15 +02001968 struct irq_chip *irqchip = gpiochip->irq.chip;
Thierry Reding39e5f092017-11-07 19:15:50 +01001969 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +01001970
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001971 acpi_gpiochip_free_interrupts(gpiochip);
1972
Hans Verkuilca620f22018-09-08 11:23:15 +02001973 if (irqchip && gpiochip->irq.parent_handler) {
Thierry Reding39e5f092017-11-07 19:15:50 +01001974 struct gpio_irq_chip *irq = &gpiochip->irq;
1975 unsigned int i;
1976
1977 for (i = 0; i < irq->num_parents; i++)
1978 irq_set_chained_handler_and_data(irq->parents[i],
1979 NULL, NULL);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001980 }
1981
Linus Walleijc3626fd2014-03-28 20:42:01 +01001982 /* Remove all IRQ mappings and delete the domain */
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001983 if (gpiochip->irq.domain) {
Thierry Reding39e5f092017-11-07 19:15:50 +01001984 unsigned int irq;
1985
Mika Westerberg79b804c2016-09-20 15:15:21 +03001986 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1987 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1988 continue;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001989
1990 irq = irq_find_mapping(gpiochip->irq.domain, offset);
1991 irq_dispose_mapping(irq);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001992 }
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001993
1994 irq_domain_remove(gpiochip->irq.domain);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001995 }
Linus Walleij14250522014-03-25 10:40:18 +01001996
Hans Verkuil461c1a72018-09-08 11:23:17 +02001997 if (irqchip) {
1998 if (irqchip->irq_request_resources == gpiochip_irq_reqres) {
1999 irqchip->irq_request_resources = NULL;
2000 irqchip->irq_release_resources = NULL;
2001 }
2002 if (irqchip->irq_enable == gpiochip_irq_enable) {
2003 irqchip->irq_enable = gpiochip->irq.irq_enable;
2004 irqchip->irq_disable = gpiochip->irq.irq_disable;
2005 }
Linus Walleij14250522014-03-25 10:40:18 +01002006 }
Hans Verkuil461c1a72018-09-08 11:23:17 +02002007 gpiochip->irq.irq_enable = NULL;
2008 gpiochip->irq.irq_disable = NULL;
Hans Verkuilca620f22018-09-08 11:23:15 +02002009 gpiochip->irq.chip = NULL;
Mika Westerberg79b804c2016-09-20 15:15:21 +03002010
2011 gpiochip_irqchip_free_valid_mask(gpiochip);
Linus Walleij14250522014-03-25 10:40:18 +01002012}
2013
2014/**
Linus Walleij739e6f52017-01-11 13:37:07 +01002015 * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01002016 * @gpiochip: the gpiochip to add the irqchip to
2017 * @irqchip: the irqchip to add to the gpiochip
2018 * @first_irq: if not dynamically assigned, the base (first) IRQ to
2019 * allocate gpiochip irqs from
2020 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +02002021 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
2022 * to have the core avoid setting up any default type in the hardware.
Thierry Reding60ed54c2017-11-07 19:15:57 +01002023 * @threaded: whether this irqchip uses a nested thread handler
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002024 * @lock_key: lockdep class for IRQ lock
2025 * @request_key: lockdep class for IRQ request
Linus Walleij14250522014-03-25 10:40:18 +01002026 *
2027 * This function closely associates a certain irqchip with a certain
2028 * gpiochip, providing an irq domain to translate the local IRQs to
2029 * global irqs in the gpiolib core, and making sure that the gpiochip
2030 * is passed as chip data to all related functions. Driver callbacks
Linus Walleij09dd5f92015-12-07 15:31:58 +01002031 * need to use gpiochip_get_data() to get their local state containers back
Linus Walleij14250522014-03-25 10:40:18 +01002032 * from the gpiochip passed as chip data. An irqdomain will be stored
2033 * in the gpiochip that shall be used by the driver to handle IRQ number
2034 * translation. The gpiochip will need to be initialized and registered
2035 * before calling this function.
2036 *
Linus Walleijc3626fd2014-03-28 20:42:01 +01002037 * This function will handle two cell:ed simple IRQs and assumes all
2038 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +01002039 * need to be open coded.
2040 */
Linus Walleij739e6f52017-01-11 13:37:07 +01002041int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
2042 struct irq_chip *irqchip,
2043 unsigned int first_irq,
2044 irq_flow_handler_t handler,
2045 unsigned int type,
Thierry Reding60ed54c2017-11-07 19:15:57 +01002046 bool threaded,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002047 struct lock_class_key *lock_key,
2048 struct lock_class_key *request_key)
Linus Walleij14250522014-03-25 10:40:18 +01002049{
2050 struct device_node *of_node;
Linus Walleij14250522014-03-25 10:40:18 +01002051
2052 if (!gpiochip || !irqchip)
2053 return -EINVAL;
2054
Linus Walleij58383c782015-11-04 09:56:26 +01002055 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +01002056 pr_err("missing gpiochip .dev parent pointer\n");
2057 return -EINVAL;
2058 }
Thierry Reding60ed54c2017-11-07 19:15:57 +01002059 gpiochip->irq.threaded = threaded;
Linus Walleij58383c782015-11-04 09:56:26 +01002060 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +01002061#ifdef CONFIG_OF_GPIO
2062 /*
Colin Cronin20a8a962015-05-18 11:41:43 -07002063 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08002064 * FIXME: get rid of this and use gpiochip->parent->of_node
2065 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +01002066 */
2067 if (gpiochip->of_node)
2068 of_node = gpiochip->of_node;
2069#endif
Marc Zyngier332e99d2016-09-07 09:12:11 +01002070 /*
Mika Westerberg0a1e0052016-09-12 14:29:51 +03002071 * Specifying a default trigger is a terrible idea if DT or ACPI is
Marc Zyngier332e99d2016-09-07 09:12:11 +01002072 * used to configure the interrupts, as you may end-up with
2073 * conflicting triggers. Tell the user, and reset to NONE.
2074 */
2075 if (WARN(of_node && type != IRQ_TYPE_NONE,
Rob Herring7eb6ce22017-07-18 16:43:03 -05002076 "%pOF: Ignoring %d default trigger\n", of_node, type))
Marc Zyngier332e99d2016-09-07 09:12:11 +01002077 type = IRQ_TYPE_NONE;
Mika Westerberg0a1e0052016-09-12 14:29:51 +03002078 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
2079 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
2080 "Ignoring %d default trigger\n", type);
2081 type = IRQ_TYPE_NONE;
2082 }
Marc Zyngier332e99d2016-09-07 09:12:11 +01002083
Thierry Redingda80ff82017-11-07 19:15:46 +01002084 gpiochip->irq.chip = irqchip;
Thierry Redingc7a0aa52017-11-07 19:15:48 +01002085 gpiochip->irq.handler = handler;
Thierry Reding3634eeb2017-11-07 19:15:49 +01002086 gpiochip->irq.default_type = type;
Linus Walleij14250522014-03-25 10:40:18 +01002087 gpiochip->to_irq = gpiochip_to_irq;
Thierry Redingca9df052017-11-07 19:15:53 +01002088 gpiochip->irq.lock_key = lock_key;
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002089 gpiochip->irq.request_key = request_key;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01002090 gpiochip->irq.domain = irq_domain_add_simple(of_node,
Linus Walleij14250522014-03-25 10:40:18 +01002091 gpiochip->ngpio, first_irq,
2092 &gpiochip_domain_ops, gpiochip);
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01002093 if (!gpiochip->irq.domain) {
Thierry Redingda80ff82017-11-07 19:15:46 +01002094 gpiochip->irq.chip = NULL;
Linus Walleij14250522014-03-25 10:40:18 +01002095 return -EINVAL;
2096 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +02002097
Hans Verkuilca620f22018-09-08 11:23:15 +02002098 gpiochip_set_irq_hooks(gpiochip);
Linus Walleij14250522014-03-25 10:40:18 +01002099
Mika Westerbergafa82fa2014-07-25 09:54:48 +03002100 acpi_gpiochip_request_interrupts(gpiochip);
2101
Linus Walleij14250522014-03-25 10:40:18 +01002102 return 0;
2103}
Linus Walleij739e6f52017-01-11 13:37:07 +01002104EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key);
Linus Walleij14250522014-03-25 10:40:18 +01002105
2106#else /* CONFIG_GPIOLIB_IRQCHIP */
2107
Thierry Reding959bc7b2017-11-07 19:15:59 +01002108static inline int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002109 struct lock_class_key *lock_key,
2110 struct lock_class_key *request_key)
Thierry Redinge0d89722017-11-07 19:15:54 +01002111{
2112 return 0;
2113}
2114
Linus Walleij14250522014-03-25 10:40:18 +01002115static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
Mika Westerberg79b804c2016-09-20 15:15:21 +03002116static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
2117{
2118 return 0;
2119}
2120static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
2121{ }
Linus Walleij14250522014-03-25 10:40:18 +01002122
2123#endif /* CONFIG_GPIOLIB_IRQCHIP */
2124
Jonas Gorskic771c2f2015-10-11 17:34:15 +02002125/**
2126 * gpiochip_generic_request() - request the gpio function for a pin
2127 * @chip: the gpiochip owning the GPIO
2128 * @offset: the offset of the GPIO to request for GPIO function
2129 */
2130int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
2131{
Linus Walleija9a1d2a2017-09-22 11:02:10 +02002132 return pinctrl_gpio_request(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02002133}
2134EXPORT_SYMBOL_GPL(gpiochip_generic_request);
2135
2136/**
2137 * gpiochip_generic_free() - free the gpio function from a pin
2138 * @chip: the gpiochip to request the gpio function for
2139 * @offset: the offset of the GPIO to free from GPIO function
2140 */
2141void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
2142{
Linus Walleija9a1d2a2017-09-22 11:02:10 +02002143 pinctrl_gpio_free(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02002144}
2145EXPORT_SYMBOL_GPL(gpiochip_generic_free);
2146
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002147/**
2148 * gpiochip_generic_config() - apply configuration for a pin
2149 * @chip: the gpiochip owning the GPIO
2150 * @offset: the offset of the GPIO to apply the configuration
2151 * @config: the configuration to be applied
2152 */
2153int gpiochip_generic_config(struct gpio_chip *chip, unsigned offset,
2154 unsigned long config)
2155{
2156 return pinctrl_gpio_set_config(chip->gpiodev->base + offset, config);
2157}
2158EXPORT_SYMBOL_GPL(gpiochip_generic_config);
2159
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302160#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01002161
Linus Walleij3f0f8672012-11-20 12:40:15 +01002162/**
Christian Ruppert586a87e2013-10-15 15:37:54 +02002163 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
2164 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +02002165 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +02002166 * @gpio_offset: the start offset in the current gpio_chip number space
2167 * @pin_group: name of the pin group inside the pin controller
Christian Lamparter973c1712018-05-21 22:57:39 +02002168 *
2169 * Calling this function directly from a DeviceTree-supported
2170 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
2171 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
2172 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
Christian Ruppert586a87e2013-10-15 15:37:54 +02002173 */
2174int gpiochip_add_pingroup_range(struct gpio_chip *chip,
2175 struct pinctrl_dev *pctldev,
2176 unsigned int gpio_offset, const char *pin_group)
2177{
2178 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002179 struct gpio_device *gdev = chip->gpiodev;
Christian Ruppert586a87e2013-10-15 15:37:54 +02002180 int ret;
2181
2182 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
2183 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002184 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +02002185 return -ENOMEM;
2186 }
2187
2188 /* Use local offset as range ID */
2189 pin_range->range.id = gpio_offset;
2190 pin_range->range.gc = chip;
2191 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002192 pin_range->range.base = gdev->base + gpio_offset;
Christian Ruppert586a87e2013-10-15 15:37:54 +02002193 pin_range->pctldev = pctldev;
2194
2195 ret = pinctrl_get_group_pins(pctldev, pin_group,
2196 &pin_range->range.pins,
2197 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +01002198 if (ret < 0) {
2199 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +02002200 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +01002201 }
Christian Ruppert586a87e2013-10-15 15:37:54 +02002202
2203 pinctrl_add_gpio_range(pctldev, &pin_range->range);
2204
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002205 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
2206 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +02002207 pinctrl_dev_get_devname(pctldev), pin_group);
2208
Linus Walleij20ec3e32016-02-11 11:03:06 +01002209 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Christian Ruppert586a87e2013-10-15 15:37:54 +02002210
2211 return 0;
2212}
2213EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
2214
2215/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01002216 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
2217 * @chip: the gpiochip to add the range for
Thierry Reding950d55f52017-07-24 16:57:22 +02002218 * @pinctl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01002219 * @gpio_offset: the start offset in the current gpio_chip number space
2220 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01002221 * @npins: the number of pins from the offset of each pin space (GPIO and
2222 * pin controller) to accumulate in this range
Thierry Reding950d55f52017-07-24 16:57:22 +02002223 *
2224 * Returns:
2225 * 0 on success, or a negative error-code on failure.
Christian Lamparter973c1712018-05-21 22:57:39 +02002226 *
2227 * Calling this function directly from a DeviceTree-supported
2228 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
2229 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
2230 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
Linus Walleij3f0f8672012-11-20 12:40:15 +01002231 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002232int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01002233 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01002234 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302235{
2236 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002237 struct gpio_device *gdev = chip->gpiodev;
Axel Linb4d4b1f2012-11-21 14:33:56 +08002238 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302239
Linus Walleij3f0f8672012-11-20 12:40:15 +01002240 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302241 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002242 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002243 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302244 }
2245
Linus Walleij3f0f8672012-11-20 12:40:15 +01002246 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01002247 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01002248 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302249 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002250 pin_range->range.base = gdev->base + gpio_offset;
Linus Walleij316511c2012-11-21 08:48:09 +01002251 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302252 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01002253 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302254 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01002255 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08002256 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002257 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +01002258 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08002259 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01002260 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002261 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
2262 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +01002263 pinctl_name,
2264 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302265
Linus Walleij20ec3e32016-02-11 11:03:06 +01002266 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002267
2268 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302269}
Linus Walleij165adc92012-11-06 14:49:39 +01002270EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302271
Linus Walleij3f0f8672012-11-20 12:40:15 +01002272/**
2273 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
2274 * @chip: the chip to remove all the mappings for
2275 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302276void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
2277{
2278 struct gpio_pin_range *pin_range, *tmp;
Linus Walleij20ec3e32016-02-11 11:03:06 +01002279 struct gpio_device *gdev = chip->gpiodev;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302280
Linus Walleij20ec3e32016-02-11 11:03:06 +01002281 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302282 list_del(&pin_range->node);
2283 pinctrl_remove_gpio_range(pin_range->pctldev,
2284 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01002285 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302286 }
2287}
Linus Walleij165adc92012-11-06 14:49:39 +01002288EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
2289
2290#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302291
David Brownelld2876d02008-02-04 22:28:20 -08002292/* These "optional" allocation calls help prevent drivers from stomping
2293 * on each other, and help provide better diagnostics in debugfs.
2294 * They're called even less than the "set direction" calls.
2295 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002296static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08002297{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002298 struct gpio_chip *chip = desc->gdev->chip;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002299 int status;
David Brownelld2876d02008-02-04 22:28:20 -08002300 unsigned long flags;
Biju Das3789f5a2018-08-07 08:15:18 +01002301 unsigned offset;
David Brownelld2876d02008-02-04 22:28:20 -08002302
2303 spin_lock_irqsave(&gpio_lock, flags);
2304
David Brownelld2876d02008-02-04 22:28:20 -08002305 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07002306 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08002307 */
2308
2309 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
2310 desc_set_label(desc, label ? : "?");
2311 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07002312 } else {
David Brownelld2876d02008-02-04 22:28:20 -08002313 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -08002314 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07002315 }
2316
2317 if (chip->request) {
2318 /* chip->request may sleep */
2319 spin_unlock_irqrestore(&gpio_lock, flags);
Biju Das3789f5a2018-08-07 08:15:18 +01002320 offset = gpio_chip_hwgpio(desc);
2321 if (gpiochip_line_is_valid(chip, offset))
2322 status = chip->request(chip, offset);
2323 else
2324 status = -EINVAL;
David Brownell35e8bb52008-10-15 22:03:16 -07002325 spin_lock_irqsave(&gpio_lock, flags);
2326
2327 if (status < 0) {
2328 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -07002329 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03002330 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07002331 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07002332 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03002333 if (chip->get_direction) {
2334 /* chip->get_direction may sleep */
2335 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002336 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03002337 spin_lock_irqsave(&gpio_lock, flags);
2338 }
David Brownelld2876d02008-02-04 22:28:20 -08002339done:
Mika Westerberg77c2d792014-03-10 14:54:50 +02002340 spin_unlock_irqrestore(&gpio_lock, flags);
2341 return status;
2342}
2343
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002344/*
2345 * This descriptor validation needs to be inserted verbatim into each
2346 * function taking a descriptor, so we need to use a preprocessor
Linus Walleij54d77192016-05-30 16:48:39 +02002347 * macro to avoid endless duplication. If the desc is NULL it is an
2348 * optional GPIO and calls should just bail out.
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002349 */
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002350static int validate_desc(const struct gpio_desc *desc, const char *func)
2351{
2352 if (!desc)
2353 return 0;
2354 if (IS_ERR(desc)) {
2355 pr_warn("%s: invalid GPIO (errorpointer)\n", func);
2356 return PTR_ERR(desc);
2357 }
2358 if (!desc->gdev) {
2359 pr_warn("%s: invalid GPIO (no device)\n", func);
2360 return -EINVAL;
2361 }
2362 if (!desc->gdev->chip) {
2363 dev_warn(&desc->gdev->dev,
2364 "%s: backing chip is gone\n", func);
2365 return 0;
2366 }
2367 return 1;
2368}
2369
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002370#define VALIDATE_DESC(desc) do { \
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002371 int __valid = validate_desc(desc, __func__); \
2372 if (__valid <= 0) \
2373 return __valid; \
2374 } while (0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002375
2376#define VALIDATE_DESC_VOID(desc) do { \
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002377 int __valid = validate_desc(desc, __func__); \
2378 if (__valid <= 0) \
Linus Walleij54d77192016-05-30 16:48:39 +02002379 return; \
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002380 } while (0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002381
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002382int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002383{
2384 int status = -EPROBE_DEFER;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002385 struct gpio_device *gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002386
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002387 VALIDATE_DESC(desc);
2388 gdev = desc->gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002389
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002390 if (try_module_get(gdev->owner)) {
Linus Walleijfac9d882017-09-26 20:58:28 +02002391 status = gpiod_request_commit(desc, label);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002392 if (status < 0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002393 module_put(gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002394 else
2395 get_device(&gdev->dev);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002396 }
2397
David Brownelld2876d02008-02-04 22:28:20 -08002398 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +02002399 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002400
David Brownelld2876d02008-02-04 22:28:20 -08002401 return status;
2402}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002403
Linus Walleijfac9d882017-09-26 20:58:28 +02002404static bool gpiod_free_commit(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002405{
Mika Westerberg77c2d792014-03-10 14:54:50 +02002406 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08002407 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07002408 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002409
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07002410 might_sleep();
2411
Alexandre Courbot372e7222013-02-03 01:29:29 +09002412 gpiod_unexport(desc);
David Brownelld8f388d82008-07-25 01:46:07 -07002413
David Brownelld2876d02008-02-04 22:28:20 -08002414 spin_lock_irqsave(&gpio_lock, flags);
2415
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002416 chip = desc->gdev->chip;
David Brownell35e8bb52008-10-15 22:03:16 -07002417 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
2418 if (chip->free) {
2419 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07002420 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002421 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07002422 spin_lock_irqsave(&gpio_lock, flags);
2423 }
David Brownelld2876d02008-02-04 22:28:20 -08002424 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08002425 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07002426 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302427 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302428 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06002429 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002430 ret = true;
2431 }
David Brownelld2876d02008-02-04 22:28:20 -08002432
2433 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002434 return ret;
2435}
2436
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002437void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002438{
Linus Walleijfac9d882017-09-26 20:58:28 +02002439 if (desc && desc->gdev && gpiod_free_commit(desc)) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002440 module_put(desc->gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002441 put_device(&desc->gdev->dev);
2442 } else {
Mika Westerberg77c2d792014-03-10 14:54:50 +02002443 WARN_ON(extra_checks);
Linus Walleij33a68e82016-02-11 10:28:44 +01002444 }
David Brownelld2876d02008-02-04 22:28:20 -08002445}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002446
David Brownelld2876d02008-02-04 22:28:20 -08002447/**
2448 * gpiochip_is_requested - return string iff signal was requested
2449 * @chip: controller managing the signal
2450 * @offset: of signal within controller's 0..(ngpio - 1) range
2451 *
2452 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09002453 * The string returned is the label passed to gpio_request(); if none has been
2454 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08002455 *
2456 * This function is for use by GPIO controller drivers. The label can
2457 * help with diagnostics, and knowing that the signal is used as a GPIO
2458 * can help avoid accidentally multiplexing it to another controller.
2459 */
2460const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
2461{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002462 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08002463
Dirk Behme48b59532015-08-18 18:02:32 +02002464 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08002465 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002466
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002467 desc = &chip->gpiodev->descs[offset];
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002468
Alexandre Courbot372e7222013-02-03 01:29:29 +09002469 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08002470 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002471 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08002472}
2473EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2474
Mika Westerberg77c2d792014-03-10 14:54:50 +02002475/**
2476 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
Thierry Reding950d55f52017-07-24 16:57:22 +02002477 * @chip: GPIO chip
2478 * @hwnum: hardware number of the GPIO for which to request the descriptor
Mika Westerberg77c2d792014-03-10 14:54:50 +02002479 * @label: label for the GPIO
2480 *
2481 * Function allows GPIO chip drivers to request and use their own GPIO
2482 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2483 * function will not increase reference count of the GPIO chip module. This
2484 * allows the GPIO chip module to be unloaded as needed (we assume that the
2485 * GPIO chip driver handles freeing the GPIOs it has requested).
Thierry Reding950d55f52017-07-24 16:57:22 +02002486 *
2487 * Returns:
2488 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2489 * code on failure.
Mika Westerberg77c2d792014-03-10 14:54:50 +02002490 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002491struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
2492 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002493{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002494 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
2495 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002496
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002497 if (IS_ERR(desc)) {
2498 chip_err(chip, "failed to get GPIO descriptor\n");
2499 return desc;
2500 }
2501
Linus Walleijfac9d882017-09-26 20:58:28 +02002502 err = gpiod_request_commit(desc, label);
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002503 if (err < 0)
2504 return ERR_PTR(err);
2505
2506 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002507}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002508EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002509
2510/**
2511 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2512 * @desc: GPIO descriptor to free
2513 *
2514 * Function frees the given GPIO requested previously with
2515 * gpiochip_request_own_desc().
2516 */
2517void gpiochip_free_own_desc(struct gpio_desc *desc)
2518{
2519 if (desc)
Linus Walleijfac9d882017-09-26 20:58:28 +02002520 gpiod_free_commit(desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002521}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002522EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08002523
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002524/*
2525 * Drivers MUST set GPIO direction before making get/set calls. In
David Brownelld2876d02008-02-04 22:28:20 -08002526 * some cases this is done in early boot, before IRQs are enabled.
2527 *
2528 * As a rule these aren't called more than once (except for drivers
2529 * using the open-drain emulation idiom) so these are natural places
2530 * to accumulate extra debugging checks. Note that we can't (yet)
2531 * rely on gpio_request() having been called beforehand.
2532 */
2533
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002534/**
2535 * gpiod_direction_input - set the GPIO direction to input
2536 * @desc: GPIO to set to input
2537 *
2538 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2539 * be called safely on it.
2540 *
2541 * Return 0 in case of success, else an error code.
2542 */
2543int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002544{
David Brownelld2876d02008-02-04 22:28:20 -08002545 struct gpio_chip *chip;
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02002546 int status = 0;
David Brownelld2876d02008-02-04 22:28:20 -08002547
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002548 VALIDATE_DESC(desc);
2549 chip = desc->gdev->chip;
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002550
Linus Walleije48d1942018-09-25 09:54:14 +02002551 /*
2552 * It is legal to have no .get() and .direction_input() specified if
2553 * the chip is output-only, but you can't specify .direction_input()
2554 * and not support the .get() operation, that doesn't make sense.
2555 */
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02002556 if (!chip->get && chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01002557 gpiod_warn(desc,
Linus Walleije48d1942018-09-25 09:54:14 +02002558 "%s: missing get() but have direction_input()\n",
2559 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002560 return -EIO;
2561 }
2562
Linus Walleije48d1942018-09-25 09:54:14 +02002563 /*
2564 * If we have a .direction_input() callback, things are simple,
2565 * just call it. Else we are some input-only chip so try to check the
2566 * direction (if .get_direction() is supported) else we silently
2567 * assume we are in input mode after this.
2568 */
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02002569 if (chip->direction_input) {
2570 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
2571 } else if (chip->get_direction &&
2572 (chip->get_direction(chip, gpio_chip_hwgpio(desc)) != 1)) {
2573 gpiod_warn(desc,
Linus Walleije48d1942018-09-25 09:54:14 +02002574 "%s: missing direction_input() operation and line is output\n",
2575 __func__);
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02002576 return -EIO;
2577 }
David Brownelld2876d02008-02-04 22:28:20 -08002578 if (status == 0)
2579 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002580
Alexandre Courbot372e7222013-02-03 01:29:29 +09002581 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09002582
David Brownelld2876d02008-02-04 22:28:20 -08002583 return status;
2584}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002585EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002586
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002587static int gpio_set_drive_single_ended(struct gpio_chip *gc, unsigned offset,
2588 enum pin_config_param mode)
2589{
2590 unsigned long config = { PIN_CONF_PACKED(mode, 0) };
2591
2592 return gc->set_config ? gc->set_config(gc, offset, config) : -ENOTSUPP;
2593}
2594
Linus Walleijfac9d882017-09-26 20:58:28 +02002595static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08002596{
Linus Walleijc663e5f2016-03-22 10:51:16 +01002597 struct gpio_chip *gc = desc->gdev->chip;
Linus Walleijad177312016-11-13 23:02:44 +01002598 int val = !!value;
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02002599 int ret = 0;
David Brownelld2876d02008-02-04 22:28:20 -08002600
Linus Walleije48d1942018-09-25 09:54:14 +02002601 /*
2602 * It's OK not to specify .direction_output() if the gpiochip is
2603 * output-only, but if there is then not even a .set() operation it
2604 * is pretty tricky to drive the output line.
2605 */
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02002606 if (!gc->set && !gc->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01002607 gpiod_warn(desc,
Linus Walleije48d1942018-09-25 09:54:14 +02002608 "%s: missing set() and direction_output() operations\n",
2609 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002610 return -EIO;
2611 }
2612
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02002613 if (gc->direction_output) {
2614 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
2615 } else {
Linus Walleije48d1942018-09-25 09:54:14 +02002616 /* Check that we are in output mode if we can */
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02002617 if (gc->get_direction &&
2618 gc->get_direction(gc, gpio_chip_hwgpio(desc))) {
2619 gpiod_warn(desc,
2620 "%s: missing direction_output() operation\n",
2621 __func__);
2622 return -EIO;
2623 }
Linus Walleije48d1942018-09-25 09:54:14 +02002624 /*
2625 * If we can't actively set the direction, we are some
2626 * output-only chip, so just drive the output as desired.
2627 */
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02002628 gc->set(gc, gpio_chip_hwgpio(desc), val);
2629 }
2630
Linus Walleijc663e5f2016-03-22 10:51:16 +01002631 if (!ret)
David Brownelld2876d02008-02-04 22:28:20 -08002632 set_bit(FLAG_IS_OUT, &desc->flags);
Linus Walleijad177312016-11-13 23:02:44 +01002633 trace_gpio_value(desc_to_gpio(desc), 0, val);
Linus Walleijc663e5f2016-03-22 10:51:16 +01002634 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2635 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08002636}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002637
2638/**
2639 * gpiod_direction_output_raw - set the GPIO direction to output
2640 * @desc: GPIO to set to output
2641 * @value: initial output value of the GPIO
2642 *
2643 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2644 * be called safely on it. The initial value of the output must be specified
2645 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2646 *
2647 * Return 0 in case of success, else an error code.
2648 */
2649int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2650{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002651 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02002652 return gpiod_direction_output_raw_commit(desc, value);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002653}
2654EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2655
2656/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05302657 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002658 * @desc: GPIO to set to output
2659 * @value: initial output value of the GPIO
2660 *
2661 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2662 * be called safely on it. The initial value of the output must be specified
2663 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2664 * account.
2665 *
2666 * Return 0 in case of success, else an error code.
2667 */
2668int gpiod_direction_output(struct gpio_desc *desc, int value)
2669{
Vladimir Zapolskiy30322bc2017-12-21 18:37:24 +02002670 struct gpio_chip *gc;
Linus Walleij02e47982017-09-26 21:20:23 +02002671 int ret;
2672
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002673 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002674 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2675 value = !value;
Linus Walleijad177312016-11-13 23:02:44 +01002676 else
2677 value = !!value;
Linus Walleij02e47982017-09-26 21:20:23 +02002678
Hans Verkuil4e9439d2018-09-08 11:23:16 +02002679 /* GPIOs used for enabled IRQs shall not be set as output */
2680 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags) &&
2681 test_bit(FLAG_IRQ_IS_ENABLED, &desc->flags)) {
Linus Walleij02e47982017-09-26 21:20:23 +02002682 gpiod_err(desc,
2683 "%s: tried to set a GPIO tied to an IRQ as output\n",
2684 __func__);
2685 return -EIO;
2686 }
2687
Vladimir Zapolskiy30322bc2017-12-21 18:37:24 +02002688 gc = desc->gdev->chip;
Linus Walleij02e47982017-09-26 21:20:23 +02002689 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2690 /* First see if we can enable open drain in hardware */
2691 ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2692 PIN_CONFIG_DRIVE_OPEN_DRAIN);
2693 if (!ret)
2694 goto set_output_value;
2695 /* Emulate open drain by not actively driving the line high */
2696 if (value)
2697 return gpiod_direction_input(desc);
2698 }
2699 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2700 ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2701 PIN_CONFIG_DRIVE_OPEN_SOURCE);
2702 if (!ret)
2703 goto set_output_value;
2704 /* Emulate open source by not actively driving the line low */
2705 if (!value)
2706 return gpiod_direction_input(desc);
2707 } else {
2708 gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2709 PIN_CONFIG_DRIVE_PUSH_PULL);
2710 }
2711
2712set_output_value:
Linus Walleijfac9d882017-09-26 20:58:28 +02002713 return gpiod_direction_output_raw_commit(desc, value);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002714}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002715EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08002716
Felipe Balbic4b5be92010-05-26 14:42:23 -07002717/**
Thierry Reding950d55f52017-07-24 16:57:22 +02002718 * gpiod_set_debounce - sets @debounce time for a GPIO
2719 * @desc: descriptor of the GPIO for which to set debounce time
2720 * @debounce: debounce time in microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02002721 *
Thierry Reding950d55f52017-07-24 16:57:22 +02002722 * Returns:
2723 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2724 * debounce time.
Felipe Balbic4b5be92010-05-26 14:42:23 -07002725 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002726int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07002727{
Felipe Balbic4b5be92010-05-26 14:42:23 -07002728 struct gpio_chip *chip;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002729 unsigned long config;
Felipe Balbic4b5be92010-05-26 14:42:23 -07002730
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002731 VALIDATE_DESC(desc);
2732 chip = desc->gdev->chip;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002733 if (!chip->set || !chip->set_config) {
Mark Brown6424de52013-09-09 10:33:49 +01002734 gpiod_dbg(desc,
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002735 "%s: missing set() or set_config() operations\n",
Mark Brown6424de52013-09-09 10:33:49 +01002736 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02002737 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002738 }
2739
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002740 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
2741 return chip->set_config(chip, gpio_chip_hwgpio(desc), config);
Felipe Balbic4b5be92010-05-26 14:42:23 -07002742}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002743EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002744
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002745/**
Andrew Jefferye10f72b2017-11-30 14:25:24 +10302746 * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
2747 * @desc: descriptor of the GPIO for which to configure persistence
2748 * @transitory: True to lose state on suspend or reset, false for persistence
2749 *
2750 * Returns:
2751 * 0 on success, otherwise a negative error code.
2752 */
2753int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
2754{
2755 struct gpio_chip *chip;
2756 unsigned long packed;
2757 int gpio;
2758 int rc;
2759
Vladimir Zapolskiy156dd392017-12-21 18:37:35 +02002760 VALIDATE_DESC(desc);
Andrew Jefferye10f72b2017-11-30 14:25:24 +10302761 /*
2762 * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
2763 * persistence state.
2764 */
2765 if (transitory)
2766 set_bit(FLAG_TRANSITORY, &desc->flags);
2767 else
2768 clear_bit(FLAG_TRANSITORY, &desc->flags);
2769
2770 /* If the driver supports it, set the persistence state now */
2771 chip = desc->gdev->chip;
2772 if (!chip->set_config)
2773 return 0;
2774
2775 packed = pinconf_to_config_packed(PIN_CONFIG_PERSIST_STATE,
2776 !transitory);
2777 gpio = gpio_chip_hwgpio(desc);
2778 rc = chip->set_config(chip, gpio, packed);
2779 if (rc == -ENOTSUPP) {
2780 dev_dbg(&desc->gdev->dev, "Persistence not supported for GPIO %d\n",
2781 gpio);
2782 return 0;
2783 }
2784
2785 return rc;
2786}
2787EXPORT_SYMBOL_GPL(gpiod_set_transitory);
2788
2789/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002790 * gpiod_is_active_low - test whether a GPIO is active-low or not
2791 * @desc: the gpio descriptor to test
2792 *
2793 * Returns 1 if the GPIO is active-low, 0 otherwise.
2794 */
2795int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002796{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002797 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002798 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002799}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002800EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08002801
2802/* I/O calls are only valid after configuration completed; the relevant
2803 * "is this a valid GPIO" error checks should already have been done.
2804 *
2805 * "Get" operations are often inlinable as reading a pin value register,
2806 * and masking the relevant bit in that register.
2807 *
2808 * When "set" operations are inlinable, they involve writing that mask to
2809 * one register to set a low value, or a different register to set it high.
2810 * Otherwise locking is needed, so there may be little value to inlining.
2811 *
2812 *------------------------------------------------------------------------
2813 *
2814 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2815 * have requested the GPIO. That can include implicit requesting by
2816 * a direction setting call. Marking a gpio as requested locks its chip
2817 * in memory, guaranteeing that these table lookups need no more locking
2818 * and that gpiochip_remove() will fail.
2819 *
2820 * REVISIT when debugging, consider adding some instrumentation to ensure
2821 * that the GPIO was actually requested.
2822 */
2823
Linus Walleijfac9d882017-09-26 20:58:28 +02002824static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002825{
2826 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002827 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002828 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002829
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002830 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002831 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002832 value = chip->get ? chip->get(chip, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01002833 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002834 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002835 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002836}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002837
Lukas Wunnereec1d562017-10-12 12:40:10 +02002838static int gpio_chip_get_multiple(struct gpio_chip *chip,
2839 unsigned long *mask, unsigned long *bits)
2840{
2841 if (chip->get_multiple) {
2842 return chip->get_multiple(chip, mask, bits);
2843 } else if (chip->get) {
2844 int i, value;
2845
2846 for_each_set_bit(i, mask, chip->ngpio) {
2847 value = chip->get(chip, i);
2848 if (value < 0)
2849 return value;
2850 __assign_bit(i, bits, value);
2851 }
2852 return 0;
2853 }
2854 return -EIO;
2855}
2856
2857int gpiod_get_array_value_complex(bool raw, bool can_sleep,
2858 unsigned int array_size,
2859 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02002860 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02002861 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02002862{
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02002863 int err, i = 0;
2864
2865 /*
2866 * Validate array_info against desc_array and its size.
2867 * It should immediately follow desc_array if both
2868 * have been obtained from the same gpiod_get_array() call.
2869 */
2870 if (array_info && array_info->desc == desc_array &&
2871 array_size <= array_info->size &&
2872 (void *)array_info == desc_array + array_info->size) {
2873 if (!can_sleep)
2874 WARN_ON(array_info->chip->can_sleep);
2875
2876 err = gpio_chip_get_multiple(array_info->chip,
2877 array_info->get_mask,
2878 value_bitmap);
2879 if (err)
2880 return err;
2881
2882 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
2883 bitmap_xor(value_bitmap, value_bitmap,
2884 array_info->invert_mask, array_size);
2885
2886 if (bitmap_full(array_info->get_mask, array_size))
2887 return 0;
2888
2889 i = find_first_zero_bit(array_info->get_mask, array_size);
2890 } else {
2891 array_info = NULL;
2892 }
Lukas Wunnereec1d562017-10-12 12:40:10 +02002893
2894 while (i < array_size) {
2895 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Laura Abbott30277432018-05-21 10:57:07 -07002896 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
2897 unsigned long *mask, *bits;
Lukas Wunnereec1d562017-10-12 12:40:10 +02002898 int first, j, ret;
2899
Laura Abbott30277432018-05-21 10:57:07 -07002900 if (likely(chip->ngpio <= FASTPATH_NGPIO)) {
2901 mask = fastpath;
2902 } else {
2903 mask = kmalloc_array(2 * BITS_TO_LONGS(chip->ngpio),
2904 sizeof(*mask),
2905 can_sleep ? GFP_KERNEL : GFP_ATOMIC);
2906 if (!mask)
2907 return -ENOMEM;
2908 }
2909
2910 bits = mask + BITS_TO_LONGS(chip->ngpio);
2911 bitmap_zero(mask, chip->ngpio);
2912
Lukas Wunnereec1d562017-10-12 12:40:10 +02002913 if (!can_sleep)
2914 WARN_ON(chip->can_sleep);
2915
2916 /* collect all inputs belonging to the same chip */
2917 first = i;
Lukas Wunnereec1d562017-10-12 12:40:10 +02002918 do {
2919 const struct gpio_desc *desc = desc_array[i];
2920 int hwgpio = gpio_chip_hwgpio(desc);
2921
2922 __set_bit(hwgpio, mask);
Janusz Krzysztofik799d5eb2018-09-29 14:20:22 +02002923 i++;
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02002924
2925 if (array_info)
Janusz Krzysztofik35ae7f92018-09-24 01:53:35 +02002926 i = find_next_zero_bit(array_info->get_mask,
2927 array_size, i);
Lukas Wunnereec1d562017-10-12 12:40:10 +02002928 } while ((i < array_size) &&
2929 (desc_array[i]->gdev->chip == chip));
2930
2931 ret = gpio_chip_get_multiple(chip, mask, bits);
Laura Abbott30277432018-05-21 10:57:07 -07002932 if (ret) {
2933 if (mask != fastpath)
2934 kfree(mask);
Lukas Wunnereec1d562017-10-12 12:40:10 +02002935 return ret;
Laura Abbott30277432018-05-21 10:57:07 -07002936 }
Lukas Wunnereec1d562017-10-12 12:40:10 +02002937
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02002938 for (j = first; j < i; ) {
Lukas Wunnereec1d562017-10-12 12:40:10 +02002939 const struct gpio_desc *desc = desc_array[j];
2940 int hwgpio = gpio_chip_hwgpio(desc);
2941 int value = test_bit(hwgpio, bits);
2942
2943 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2944 value = !value;
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02002945 __assign_bit(j, value_bitmap, value);
Lukas Wunnereec1d562017-10-12 12:40:10 +02002946 trace_gpio_value(desc_to_gpio(desc), 1, value);
Janusz Krzysztofik799d5eb2018-09-29 14:20:22 +02002947 j++;
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02002948
2949 if (array_info)
Janusz Krzysztofik35ae7f92018-09-24 01:53:35 +02002950 j = find_next_zero_bit(array_info->get_mask, i,
2951 j);
Lukas Wunnereec1d562017-10-12 12:40:10 +02002952 }
Laura Abbott30277432018-05-21 10:57:07 -07002953
2954 if (mask != fastpath)
2955 kfree(mask);
Lukas Wunnereec1d562017-10-12 12:40:10 +02002956 }
2957 return 0;
2958}
2959
David Brownelld2876d02008-02-04 22:28:20 -08002960/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002961 * gpiod_get_raw_value() - return a gpio's raw value
2962 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08002963 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002964 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002965 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002966 *
2967 * This function should be called from contexts where we cannot sleep, and will
2968 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002969 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002970int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002971{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002972 VALIDATE_DESC(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002973 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002974 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02002975 return gpiod_get_raw_value_commit(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002976}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002977EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002978
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002979/**
2980 * gpiod_get_value() - return a gpio's value
2981 * @desc: gpio whose value will be returned
2982 *
2983 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002984 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002985 *
2986 * This function should be called from contexts where we cannot sleep, and will
2987 * complain if the GPIO chip functions potentially sleep.
2988 */
2989int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002990{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002991 int value;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002992
2993 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002994 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002995 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002996
Linus Walleijfac9d882017-09-26 20:58:28 +02002997 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002998 if (value < 0)
2999 return value;
3000
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003001 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3002 value = !value;
3003
3004 return value;
David Brownelld2876d02008-02-04 22:28:20 -08003005}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003006EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08003007
Lukas Wunnereec1d562017-10-12 12:40:10 +02003008/**
3009 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003010 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02003011 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003012 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003013 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02003014 *
3015 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3016 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3017 * else an error code.
3018 *
3019 * This function should be called from contexts where we cannot sleep,
3020 * and it will complain if the GPIO chip functions potentially sleep.
3021 */
3022int gpiod_get_raw_array_value(unsigned int array_size,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003023 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003024 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003025 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02003026{
3027 if (!desc_array)
3028 return -EINVAL;
3029 return gpiod_get_array_value_complex(true, false, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003030 desc_array, array_info,
3031 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003032}
3033EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
3034
3035/**
3036 * gpiod_get_array_value() - read values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003037 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02003038 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003039 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003040 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02003041 *
3042 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3043 * into account. Return 0 in case of success, else an error code.
3044 *
3045 * This function should be called from contexts where we cannot sleep,
3046 * and it will complain if the GPIO chip functions potentially sleep.
3047 */
3048int gpiod_get_array_value(unsigned int array_size,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003049 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003050 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003051 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02003052{
3053 if (!desc_array)
3054 return -EINVAL;
3055 return gpiod_get_array_value_complex(false, false, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003056 desc_array, array_info,
3057 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003058}
3059EXPORT_SYMBOL_GPL(gpiod_get_array_value);
3060
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303061/*
Linus Walleijfac9d882017-09-26 20:58:28 +02003062 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003063 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07003064 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303065 */
Linus Walleijfac9d882017-09-26 20:58:28 +02003066static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303067{
3068 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003069 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003070 int offset = gpio_chip_hwgpio(desc);
3071
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303072 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09003073 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303074 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003075 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303076 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09003077 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303078 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003079 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303080 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09003081 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303082 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01003083 gpiod_err(desc,
3084 "%s: Error in set_value for open drain err %d\n",
3085 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303086}
3087
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303088/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003089 * _gpio_set_open_source_value() - Set the open source gpio's value.
3090 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07003091 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303092 */
Linus Walleijfac9d882017-09-26 20:58:28 +02003093static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303094{
3095 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003096 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003097 int offset = gpio_chip_hwgpio(desc);
3098
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303099 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09003100 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303101 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003102 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303103 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09003104 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303105 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003106 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303107 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09003108 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303109 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01003110 gpiod_err(desc,
3111 "%s: Error in set_value for open source err %d\n",
3112 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303113}
3114
Linus Walleijfac9d882017-09-26 20:58:28 +02003115static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08003116{
3117 struct gpio_chip *chip;
3118
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003119 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003120 trace_gpio_value(desc_to_gpio(desc), 0, value);
Linus Walleij02e47982017-09-26 21:20:23 +02003121 chip->set(chip, gpio_chip_hwgpio(desc), value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003122}
3123
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003124/*
3125 * set multiple outputs on the same chip;
3126 * use the chip's set_multiple function if available;
3127 * otherwise set the outputs sequentially;
3128 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
3129 * defines which outputs are to be changed
3130 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
3131 * defines the values the outputs specified by mask are to be set to
3132 */
3133static void gpio_chip_set_multiple(struct gpio_chip *chip,
3134 unsigned long *mask, unsigned long *bits)
3135{
3136 if (chip->set_multiple) {
3137 chip->set_multiple(chip, mask, bits);
3138 } else {
Andy Shevchenko5e4e6fb2017-01-03 19:01:17 +02003139 unsigned int i;
3140
3141 /* set outputs if the corresponding mask bit is set */
3142 for_each_set_bit(i, mask, chip->ngpio)
3143 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003144 }
3145}
3146
Laura Abbott30277432018-05-21 10:57:07 -07003147int gpiod_set_array_value_complex(bool raw, bool can_sleep,
Geert Uytterhoeven3c940662018-09-27 13:38:10 +02003148 unsigned int array_size,
3149 struct gpio_desc **desc_array,
3150 struct gpio_array *array_info,
3151 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003152{
3153 int i = 0;
3154
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003155 /*
3156 * Validate array_info against desc_array and its size.
3157 * It should immediately follow desc_array if both
3158 * have been obtained from the same gpiod_get_array() call.
3159 */
3160 if (array_info && array_info->desc == desc_array &&
3161 array_size <= array_info->size &&
3162 (void *)array_info == desc_array + array_info->size) {
3163 if (!can_sleep)
3164 WARN_ON(array_info->chip->can_sleep);
3165
3166 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
3167 bitmap_xor(value_bitmap, value_bitmap,
3168 array_info->invert_mask, array_size);
3169
3170 gpio_chip_set_multiple(array_info->chip, array_info->set_mask,
3171 value_bitmap);
3172
3173 if (bitmap_full(array_info->set_mask, array_size))
3174 return 0;
3175
3176 i = find_first_zero_bit(array_info->set_mask, array_size);
3177 } else {
3178 array_info = NULL;
3179 }
3180
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003181 while (i < array_size) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003182 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Laura Abbott30277432018-05-21 10:57:07 -07003183 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
3184 unsigned long *mask, *bits;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003185 int count = 0;
3186
Laura Abbott30277432018-05-21 10:57:07 -07003187 if (likely(chip->ngpio <= FASTPATH_NGPIO)) {
3188 mask = fastpath;
3189 } else {
3190 mask = kmalloc_array(2 * BITS_TO_LONGS(chip->ngpio),
3191 sizeof(*mask),
3192 can_sleep ? GFP_KERNEL : GFP_ATOMIC);
3193 if (!mask)
3194 return -ENOMEM;
3195 }
3196
3197 bits = mask + BITS_TO_LONGS(chip->ngpio);
3198 bitmap_zero(mask, chip->ngpio);
3199
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003200 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003201 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003202
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003203 do {
3204 struct gpio_desc *desc = desc_array[i];
3205 int hwgpio = gpio_chip_hwgpio(desc);
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003206 int value = test_bit(i, value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003207
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003208 /*
3209 * Pins applicable for fast input but not for
3210 * fast output processing may have been already
3211 * inverted inside the fast path, skip them.
3212 */
3213 if (!raw && !(array_info &&
3214 test_bit(i, array_info->invert_mask)) &&
3215 test_bit(FLAG_ACTIVE_LOW, &desc->flags))
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003216 value = !value;
3217 trace_gpio_value(desc_to_gpio(desc), 0, value);
3218 /*
3219 * collect all normal outputs belonging to the same chip
3220 * open drain and open source outputs are set individually
3221 */
Linus Walleij02e47982017-09-26 21:20:23 +02003222 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02003223 gpio_set_open_drain_value_commit(desc, value);
Linus Walleij02e47982017-09-26 21:20:23 +02003224 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02003225 gpio_set_open_source_value_commit(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003226 } else {
3227 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003228 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003229 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003230 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003231 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003232 count++;
3233 }
Janusz Krzysztofik799d5eb2018-09-29 14:20:22 +02003234 i++;
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003235
3236 if (array_info)
Janusz Krzysztofik35ae7f92018-09-24 01:53:35 +02003237 i = find_next_zero_bit(array_info->set_mask,
3238 array_size, i);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003239 } while ((i < array_size) &&
3240 (desc_array[i]->gdev->chip == chip));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003241 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003242 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003243 gpio_chip_set_multiple(chip, mask, bits);
Laura Abbott30277432018-05-21 10:57:07 -07003244
3245 if (mask != fastpath)
3246 kfree(mask);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003247 }
Laura Abbott30277432018-05-21 10:57:07 -07003248 return 0;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003249}
3250
David Brownelld2876d02008-02-04 22:28:20 -08003251/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003252 * gpiod_set_raw_value() - assign a gpio's raw value
3253 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08003254 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08003255 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003256 * Set the raw value of the GPIO, i.e. the value of its physical line without
3257 * regard for its ACTIVE_LOW status.
3258 *
3259 * This function should be called from contexts where we cannot sleep, and will
3260 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003261 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003262void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003263{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003264 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01003265 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003266 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02003267 gpiod_set_raw_value_commit(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08003268}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003269EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08003270
3271/**
Geert Uytterhoeven1e77fc82018-01-09 19:08:21 +01003272 * gpiod_set_value_nocheck() - set a GPIO line value without checking
3273 * @desc: the descriptor to set the value on
3274 * @value: value to set
3275 *
3276 * This sets the value of a GPIO line backing a descriptor, applying
3277 * different semantic quirks like active low and open drain/source
3278 * handling.
3279 */
3280static void gpiod_set_value_nocheck(struct gpio_desc *desc, int value)
3281{
3282 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3283 value = !value;
3284 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
3285 gpio_set_open_drain_value_commit(desc, value);
3286 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
3287 gpio_set_open_source_value_commit(desc, value);
3288 else
3289 gpiod_set_raw_value_commit(desc, value);
3290}
3291
3292/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003293 * gpiod_set_value() - assign a gpio's value
3294 * @desc: gpio whose value will be assigned
3295 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08003296 *
Linus Walleij02e47982017-09-26 21:20:23 +02003297 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
3298 * OPEN_DRAIN and OPEN_SOURCE flags into account.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003299 *
3300 * This function should be called from contexts where we cannot sleep, and will
3301 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003302 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003303void gpiod_set_value(struct gpio_desc *desc, int value)
3304{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003305 VALIDATE_DESC_VOID(desc);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003306 WARN_ON(desc->gdev->chip->can_sleep);
Geert Uytterhoeven1e77fc82018-01-09 19:08:21 +01003307 gpiod_set_value_nocheck(desc, value);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003308}
3309EXPORT_SYMBOL_GPL(gpiod_set_value);
3310
3311/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003312 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003313 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003314 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003315 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003316 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003317 *
3318 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3319 * without regard for their ACTIVE_LOW status.
3320 *
3321 * This function should be called from contexts where we cannot sleep, and will
3322 * complain if the GPIO chip functions potentially sleep.
3323 */
Laura Abbott30277432018-05-21 10:57:07 -07003324int gpiod_set_raw_array_value(unsigned int array_size,
Geert Uytterhoeven3c940662018-09-27 13:38:10 +02003325 struct gpio_desc **desc_array,
3326 struct gpio_array *array_info,
3327 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003328{
3329 if (!desc_array)
Laura Abbott30277432018-05-21 10:57:07 -07003330 return -EINVAL;
3331 return gpiod_set_array_value_complex(true, false, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003332 desc_array, array_info, value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003333}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003334EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003335
3336/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003337 * gpiod_set_array_value() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003338 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003339 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003340 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003341 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003342 *
3343 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3344 * into account.
3345 *
3346 * This function should be called from contexts where we cannot sleep, and will
3347 * complain if the GPIO chip functions potentially sleep.
3348 */
Geert Uytterhoevencf9af0d2018-09-27 13:38:09 +02003349int gpiod_set_array_value(unsigned int array_size,
3350 struct gpio_desc **desc_array,
3351 struct gpio_array *array_info,
3352 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003353{
3354 if (!desc_array)
Geert Uytterhoevencf9af0d2018-09-27 13:38:09 +02003355 return -EINVAL;
3356 return gpiod_set_array_value_complex(false, false, array_size,
3357 desc_array, array_info,
3358 value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003359}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003360EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003361
3362/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003363 * gpiod_cansleep() - report whether gpio value access may sleep
3364 * @desc: gpio to check
3365 *
3366 */
3367int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003368{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003369 VALIDATE_DESC(desc);
3370 return desc->gdev->chip->can_sleep;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003371}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003372EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08003373
David Brownell0f6d5042008-10-15 22:03:14 -07003374/**
Linus Walleij90b39402e2018-06-01 13:21:27 +02003375 * gpiod_set_consumer_name() - set the consumer name for the descriptor
3376 * @desc: gpio to set the consumer name on
3377 * @name: the new consumer name
3378 */
3379void gpiod_set_consumer_name(struct gpio_desc *desc, const char *name)
3380{
3381 VALIDATE_DESC_VOID(desc);
3382 /* Just overwrite whatever the previous name was */
3383 desc->label = name;
3384}
3385EXPORT_SYMBOL_GPL(gpiod_set_consumer_name);
3386
3387/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003388 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
3389 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07003390 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003391 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
3392 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07003393 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003394int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07003395{
Linus Walleij4c37ce82016-05-02 13:13:10 +02003396 struct gpio_chip *chip;
3397 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07003398
Linus Walleij79bb71b2016-06-15 22:57:38 +02003399 /*
3400 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
3401 * requires this function to not return zero on an invalid descriptor
3402 * but rather a negative error number.
3403 */
Linus Walleijbfbbe442016-06-16 11:55:55 +02003404 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
Linus Walleij79bb71b2016-06-15 22:57:38 +02003405 return -EINVAL;
3406
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003407 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003408 offset = gpio_chip_hwgpio(desc);
Linus Walleij4c37ce82016-05-02 13:13:10 +02003409 if (chip->to_irq) {
3410 int retirq = chip->to_irq(chip, offset);
3411
3412 /* Zero means NO_IRQ */
3413 if (!retirq)
3414 return -ENXIO;
3415
3416 return retirq;
3417 }
3418 return -ENXIO;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003419}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003420EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003421
Linus Walleijd468bf92013-09-24 11:54:38 +02003422/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003423 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003424 * @chip: the chip the GPIO to lock belongs to
3425 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02003426 *
3427 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08003428 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08003429 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003430int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08003431{
Linus Walleij9c102802016-05-25 10:56:03 +02003432 struct gpio_desc *desc;
Linus Walleijd468bf92013-09-24 11:54:38 +02003433
Linus Walleij9c102802016-05-25 10:56:03 +02003434 desc = gpiochip_get_desc(chip, offset);
3435 if (IS_ERR(desc))
3436 return PTR_ERR(desc);
3437
Linus Walleij60f83392016-11-12 15:01:09 +01003438 /*
3439 * If it's fast: flush the direction setting if something changed
3440 * behind our back
3441 */
3442 if (!chip->can_sleep && chip->get_direction) {
Andy Shevchenko80956792018-07-09 21:47:21 +03003443 int dir = gpiod_get_direction(desc);
Linus Walleij9c102802016-05-25 10:56:03 +02003444
Andy Shevchenko36b31272018-07-03 03:38:31 +03003445 if (dir < 0) {
3446 chip_err(chip, "%s: cannot get GPIO direction\n",
3447 __func__);
3448 return dir;
3449 }
Linus Walleij9c102802016-05-25 10:56:03 +02003450 }
3451
3452 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003453 chip_err(chip,
Andy Shevchenkob1911712018-07-03 03:39:03 +03003454 "%s: tried to flag a GPIO set as output for IRQ\n",
3455 __func__);
Linus Walleijd468bf92013-09-24 11:54:38 +02003456 return -EIO;
3457 }
3458
Linus Walleij9c102802016-05-25 10:56:03 +02003459 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
Hans Verkuil4e9439d2018-09-08 11:23:16 +02003460 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
Linus Walleij3940c342016-11-14 00:09:07 +01003461
3462 /*
3463 * If the consumer has not set up a label (such as when the
3464 * IRQ is referenced from .to_irq()) we set up a label here
3465 * so it is clear this is used as an interrupt.
3466 */
3467 if (!desc->label)
3468 desc_set_label(desc, "interrupt");
3469
Linus Walleijd468bf92013-09-24 11:54:38 +02003470 return 0;
3471}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003472EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02003473
Linus Walleijd468bf92013-09-24 11:54:38 +02003474/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003475 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003476 * @chip: the chip the GPIO to lock belongs to
3477 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02003478 *
3479 * This is used directly by GPIO drivers that want to indicate
3480 * that a certain GPIO is no longer used exclusively for IRQ.
3481 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003482void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02003483{
Linus Walleij3940c342016-11-14 00:09:07 +01003484 struct gpio_desc *desc;
3485
3486 desc = gpiochip_get_desc(chip, offset);
3487 if (IS_ERR(desc))
Linus Walleijd468bf92013-09-24 11:54:38 +02003488 return;
3489
Linus Walleij3940c342016-11-14 00:09:07 +01003490 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
Hans Verkuil4e9439d2018-09-08 11:23:16 +02003491 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
Linus Walleij3940c342016-11-14 00:09:07 +01003492
3493 /* If we only had this marking, erase it */
3494 if (desc->label && !strcmp(desc->label, "interrupt"))
3495 desc_set_label(desc, NULL);
Linus Walleijd468bf92013-09-24 11:54:38 +02003496}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003497EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02003498
Hans Verkuil4e9439d2018-09-08 11:23:16 +02003499void gpiochip_disable_irq(struct gpio_chip *chip, unsigned int offset)
3500{
3501 struct gpio_desc *desc = gpiochip_get_desc(chip, offset);
3502
3503 if (!IS_ERR(desc) &&
3504 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags)))
3505 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3506}
3507EXPORT_SYMBOL_GPL(gpiochip_disable_irq);
3508
3509void gpiochip_enable_irq(struct gpio_chip *chip, unsigned int offset)
3510{
3511 struct gpio_desc *desc = gpiochip_get_desc(chip, offset);
3512
3513 if (!IS_ERR(desc) &&
3514 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) {
3515 WARN_ON(test_bit(FLAG_IS_OUT, &desc->flags));
3516 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3517 }
3518}
3519EXPORT_SYMBOL_GPL(gpiochip_enable_irq);
3520
Linus Walleij6cee3822016-02-11 20:16:45 +01003521bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
3522{
3523 if (offset >= chip->ngpio)
3524 return false;
3525
3526 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
3527}
3528EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
3529
Hans Verkuil4e6b8232018-09-08 11:23:14 +02003530int gpiochip_reqres_irq(struct gpio_chip *chip, unsigned int offset)
3531{
3532 int ret;
3533
3534 if (!try_module_get(chip->gpiodev->owner))
3535 return -ENODEV;
3536
3537 ret = gpiochip_lock_as_irq(chip, offset);
3538 if (ret) {
3539 chip_err(chip, "unable to lock HW IRQ %u for IRQ\n", offset);
3540 module_put(chip->gpiodev->owner);
3541 return ret;
3542 }
3543 return 0;
3544}
3545EXPORT_SYMBOL_GPL(gpiochip_reqres_irq);
3546
3547void gpiochip_relres_irq(struct gpio_chip *chip, unsigned int offset)
3548{
3549 gpiochip_unlock_as_irq(chip, offset);
3550 module_put(chip->gpiodev->owner);
3551}
3552EXPORT_SYMBOL_GPL(gpiochip_relres_irq);
3553
Linus Walleij143b65d2016-02-16 15:41:42 +01003554bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
3555{
3556 if (offset >= chip->ngpio)
3557 return false;
3558
3559 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
3560}
3561EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
3562
3563bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
3564{
3565 if (offset >= chip->ngpio)
3566 return false;
3567
3568 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
3569}
3570EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
3571
Charles Keepax05f479b2017-05-23 15:47:29 +01003572bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset)
3573{
3574 if (offset >= chip->ngpio)
3575 return false;
3576
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303577 return !test_bit(FLAG_TRANSITORY, &chip->gpiodev->descs[offset].flags);
Charles Keepax05f479b2017-05-23 15:47:29 +01003578}
3579EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
3580
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003581/**
3582 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
3583 * @desc: gpio whose value will be returned
3584 *
3585 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003586 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003587 *
3588 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003589 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003590int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08003591{
David Brownelld2876d02008-02-04 22:28:20 -08003592 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003593 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003594 return gpiod_get_raw_value_commit(desc);
David Brownelld2876d02008-02-04 22:28:20 -08003595}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003596EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003597
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003598/**
3599 * gpiod_get_value_cansleep() - return a gpio's value
3600 * @desc: gpio whose value will be returned
3601 *
3602 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003603 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003604 *
3605 * This function is to be called from contexts that can sleep.
3606 */
3607int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003608{
David Brownelld2876d02008-02-04 22:28:20 -08003609 int value;
David Brownelld2876d02008-02-04 22:28:20 -08003610
3611 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003612 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003613 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003614 if (value < 0)
3615 return value;
3616
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003617 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3618 value = !value;
3619
David Brownelld2876d02008-02-04 22:28:20 -08003620 return value;
3621}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003622EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003623
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003624/**
Lukas Wunnereec1d562017-10-12 12:40:10 +02003625 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003626 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02003627 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003628 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003629 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02003630 *
3631 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3632 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3633 * else an error code.
3634 *
3635 * This function is to be called from contexts that can sleep.
3636 */
3637int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
3638 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003639 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003640 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02003641{
3642 might_sleep_if(extra_checks);
3643 if (!desc_array)
3644 return -EINVAL;
3645 return gpiod_get_array_value_complex(true, true, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003646 desc_array, array_info,
3647 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003648}
3649EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
3650
3651/**
3652 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003653 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02003654 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003655 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003656 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02003657 *
3658 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3659 * into account. Return 0 in case of success, else an error code.
3660 *
3661 * This function is to be called from contexts that can sleep.
3662 */
3663int gpiod_get_array_value_cansleep(unsigned int array_size,
3664 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003665 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003666 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02003667{
3668 might_sleep_if(extra_checks);
3669 if (!desc_array)
3670 return -EINVAL;
3671 return gpiod_get_array_value_complex(false, true, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003672 desc_array, array_info,
3673 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003674}
3675EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
3676
3677/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003678 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
3679 * @desc: gpio whose value will be assigned
3680 * @value: value to assign
3681 *
3682 * Set the raw value of the GPIO, i.e. the value of its physical line without
3683 * regard for its ACTIVE_LOW status.
3684 *
3685 * This function is to be called from contexts that can sleep.
3686 */
3687void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003688{
David Brownelld2876d02008-02-04 22:28:20 -08003689 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003690 VALIDATE_DESC_VOID(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003691 gpiod_set_raw_value_commit(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003692}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003693EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003694
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003695/**
3696 * gpiod_set_value_cansleep() - assign a gpio's value
3697 * @desc: gpio whose value will be assigned
3698 * @value: value to assign
3699 *
3700 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3701 * account
3702 *
3703 * This function is to be called from contexts that can sleep.
3704 */
3705void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003706{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003707 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003708 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1e77fc82018-01-09 19:08:21 +01003709 gpiod_set_value_nocheck(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08003710}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003711EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08003712
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003713/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003714 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003715 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003716 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003717 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003718 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003719 *
3720 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3721 * without regard for their ACTIVE_LOW status.
3722 *
3723 * This function is to be called from contexts that can sleep.
3724 */
Laura Abbott30277432018-05-21 10:57:07 -07003725int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
Geert Uytterhoeven3c940662018-09-27 13:38:10 +02003726 struct gpio_desc **desc_array,
3727 struct gpio_array *array_info,
3728 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003729{
3730 might_sleep_if(extra_checks);
3731 if (!desc_array)
Laura Abbott30277432018-05-21 10:57:07 -07003732 return -EINVAL;
3733 return gpiod_set_array_value_complex(true, true, array_size, desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003734 array_info, value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003735}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003736EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003737
3738/**
Dmitry Torokhov3946d182017-08-14 21:59:55 -07003739 * gpiod_add_lookup_tables() - register GPIO device consumers
3740 * @tables: list of tables of consumers to register
3741 * @n: number of tables in the list
3742 */
3743void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
3744{
3745 unsigned int i;
3746
3747 mutex_lock(&gpio_lookup_lock);
3748
3749 for (i = 0; i < n; i++)
3750 list_add_tail(&tables[i]->list, &gpio_lookup_list);
3751
3752 mutex_unlock(&gpio_lookup_lock);
3753}
3754
3755/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003756 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003757 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003758 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003759 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003760 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003761 *
3762 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3763 * into account.
3764 *
3765 * This function is to be called from contexts that can sleep.
3766 */
Geert Uytterhoevencf9af0d2018-09-27 13:38:09 +02003767int gpiod_set_array_value_cansleep(unsigned int array_size,
3768 struct gpio_desc **desc_array,
3769 struct gpio_array *array_info,
3770 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003771{
3772 might_sleep_if(extra_checks);
3773 if (!desc_array)
Geert Uytterhoevencf9af0d2018-09-27 13:38:09 +02003774 return -EINVAL;
3775 return gpiod_set_array_value_complex(false, true, array_size,
3776 desc_array, array_info,
3777 value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003778}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003779EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003780
3781/**
Alexandre Courbotad824782013-12-03 12:20:11 +09003782 * gpiod_add_lookup_table() - register GPIO device consumers
3783 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003784 */
Alexandre Courbotad824782013-12-03 12:20:11 +09003785void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003786{
3787 mutex_lock(&gpio_lookup_lock);
3788
Alexandre Courbotad824782013-12-03 12:20:11 +09003789 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003790
3791 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08003792}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02003793EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
David Brownelld2876d02008-02-04 22:28:20 -08003794
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05303795/**
3796 * gpiod_remove_lookup_table() - unregister GPIO device consumers
3797 * @table: table of consumers to unregister
3798 */
3799void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
3800{
3801 mutex_lock(&gpio_lookup_lock);
3802
3803 list_del(&table->list);
3804
3805 mutex_unlock(&gpio_lookup_lock);
3806}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02003807EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05303808
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02003809/**
3810 * gpiod_add_hogs() - register a set of GPIO hogs from machine code
3811 * @hogs: table of gpio hog entries with a zeroed sentinel at the end
3812 */
3813void gpiod_add_hogs(struct gpiod_hog *hogs)
3814{
3815 struct gpio_chip *chip;
3816 struct gpiod_hog *hog;
3817
3818 mutex_lock(&gpio_machine_hogs_mutex);
3819
3820 for (hog = &hogs[0]; hog->chip_label; hog++) {
3821 list_add_tail(&hog->list, &gpio_machine_hogs);
3822
3823 /*
3824 * The chip may have been registered earlier, so check if it
3825 * exists and, if so, try to hog the line now.
3826 */
3827 chip = find_chip_by_name(hog->chip_label);
3828 if (chip)
3829 gpiochip_machine_hog(chip, hog);
3830 }
3831
3832 mutex_unlock(&gpio_machine_hogs_mutex);
3833}
3834EXPORT_SYMBOL_GPL(gpiod_add_hogs);
3835
Alexandre Courbotad824782013-12-03 12:20:11 +09003836static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
3837{
3838 const char *dev_id = dev ? dev_name(dev) : NULL;
3839 struct gpiod_lookup_table *table;
3840
3841 mutex_lock(&gpio_lookup_lock);
3842
3843 list_for_each_entry(table, &gpio_lookup_list, list) {
3844 if (table->dev_id && dev_id) {
3845 /*
3846 * Valid strings on both ends, must be identical to have
3847 * a match
3848 */
3849 if (!strcmp(table->dev_id, dev_id))
3850 goto found;
3851 } else {
3852 /*
3853 * One of the pointers is NULL, so both must be to have
3854 * a match
3855 */
3856 if (dev_id == table->dev_id)
3857 goto found;
3858 }
3859 }
3860 table = NULL;
3861
3862found:
3863 mutex_unlock(&gpio_lookup_lock);
3864 return table;
3865}
3866
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003867static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09003868 unsigned int idx,
3869 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003870{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003871 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09003872 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003873 struct gpiod_lookup *p;
3874
Alexandre Courbotad824782013-12-03 12:20:11 +09003875 table = gpiod_find_lookup_table(dev);
3876 if (!table)
3877 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003878
Alexandre Courbotad824782013-12-03 12:20:11 +09003879 for (p = &table->table[0]; p->chip_label; p++) {
3880 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003881
Alexandre Courbotad824782013-12-03 12:20:11 +09003882 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003883 if (p->idx != idx)
3884 continue;
3885
Alexandre Courbotad824782013-12-03 12:20:11 +09003886 /* If the lookup entry has a con_id, require exact match */
3887 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
3888 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003889
Alexandre Courbotad824782013-12-03 12:20:11 +09003890 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003891
Alexandre Courbotad824782013-12-03 12:20:11 +09003892 if (!chip) {
Janusz Krzysztofik8853daf2018-07-04 00:18:19 +02003893 /*
3894 * As the lookup table indicates a chip with
3895 * p->chip_label should exist, assume it may
3896 * still appear later and let the interested
3897 * consumer be probed again or let the Deferred
3898 * Probe infrastructure handle the error.
3899 */
3900 dev_warn(dev, "cannot find GPIO chip %s, deferring\n",
3901 p->chip_label);
3902 return ERR_PTR(-EPROBE_DEFER);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003903 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003904
Alexandre Courbotad824782013-12-03 12:20:11 +09003905 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003906 dev_err(dev,
3907 "requested GPIO %d is out of range [0..%d] for chip %s\n",
3908 idx, chip->ngpio, chip->label);
3909 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09003910 }
3911
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09003912 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09003913 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003914
3915 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09003916 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003917
3918 return desc;
3919}
3920
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003921static int dt_gpio_count(struct device *dev, const char *con_id)
3922{
3923 int ret;
3924 char propname[32];
3925 unsigned int i;
3926
3927 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
3928 if (con_id)
3929 snprintf(propname, sizeof(propname), "%s-%s",
3930 con_id, gpio_suffixes[i]);
3931 else
3932 snprintf(propname, sizeof(propname), "%s",
3933 gpio_suffixes[i]);
3934
3935 ret = of_gpio_named_count(dev->of_node, propname);
Andy Shevchenko4033d4a2017-02-20 18:15:47 +02003936 if (ret > 0)
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003937 break;
3938 }
Andy Shevchenko4033d4a2017-02-20 18:15:47 +02003939 return ret ? ret : -ENOENT;
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003940}
3941
3942static int platform_gpio_count(struct device *dev, const char *con_id)
3943{
3944 struct gpiod_lookup_table *table;
3945 struct gpiod_lookup *p;
3946 unsigned int count = 0;
3947
3948 table = gpiod_find_lookup_table(dev);
3949 if (!table)
3950 return -ENOENT;
3951
3952 for (p = &table->table[0]; p->chip_label; p++) {
3953 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3954 (!con_id && !p->con_id))
3955 count++;
3956 }
3957 if (!count)
3958 return -ENOENT;
3959
3960 return count;
3961}
3962
3963/**
3964 * gpiod_count - return the number of GPIOs associated with a device / function
3965 * or -ENOENT if no GPIO has been assigned to the requested function
3966 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3967 * @con_id: function within the GPIO consumer
3968 */
3969int gpiod_count(struct device *dev, const char *con_id)
3970{
3971 int count = -ENOENT;
3972
3973 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
3974 count = dt_gpio_count(dev, con_id);
3975 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
3976 count = acpi_gpio_count(dev, con_id);
3977
3978 if (count < 0)
3979 count = platform_gpio_count(dev, con_id);
3980
3981 return count;
3982}
3983EXPORT_SYMBOL_GPL(gpiod_count);
3984
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003985/**
Thierry Reding08791622014-04-25 16:54:22 +02003986 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09003987 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003988 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003989 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003990 *
3991 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003992 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07003993 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003994 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003995struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003996 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003997{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003998 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003999}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004000EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004001
4002/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02004003 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
4004 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4005 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004006 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02004007 *
4008 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
4009 * the requested function it will return NULL. This is convenient for drivers
4010 * that need to handle optional GPIOs.
4011 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004012struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004013 const char *con_id,
4014 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02004015{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004016 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02004017}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004018EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02004019
Benoit Parrotf625d462015-02-02 11:44:44 -06004020
4021/**
4022 * gpiod_configure_flags - helper function to configure a given GPIO
4023 * @desc: gpio whose value will be assigned
4024 * @con_id: function within the GPIO consumer
Johan Hovold85b03b32016-07-03 18:32:05 +02004025 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
4026 * of_get_gpio_hog()
Benoit Parrotf625d462015-02-02 11:44:44 -06004027 * @dflags: gpiod_flags - optional GPIO initialization flags
4028 *
4029 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
4030 * requested function and/or index, or another IS_ERR() code if an error
4031 * occurred while trying to acquire the GPIO.
4032 */
Andy Shevchenkoc29fd9e2017-05-23 20:03:16 +03004033int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Johan Hovold85b03b32016-07-03 18:32:05 +02004034 unsigned long lflags, enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06004035{
4036 int status;
4037
Johan Hovold85b03b32016-07-03 18:32:05 +02004038 if (lflags & GPIO_ACTIVE_LOW)
4039 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
Linus Walleijf926dfc2017-09-10 19:26:22 +02004040
Johan Hovold85b03b32016-07-03 18:32:05 +02004041 if (lflags & GPIO_OPEN_DRAIN)
4042 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
Linus Walleijf926dfc2017-09-10 19:26:22 +02004043 else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) {
4044 /*
4045 * This enforces open drain mode from the consumer side.
4046 * This is necessary for some busses like I2C, but the lookup
4047 * should *REALLY* have specified them as open drain in the
4048 * first place, so print a little warning here.
4049 */
4050 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
4051 gpiod_warn(desc,
4052 "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
4053 }
4054
Johan Hovold85b03b32016-07-03 18:32:05 +02004055 if (lflags & GPIO_OPEN_SOURCE)
4056 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Andrew Jefferye10f72b2017-11-30 14:25:24 +10304057
4058 status = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
4059 if (status < 0)
4060 return status;
Johan Hovold85b03b32016-07-03 18:32:05 +02004061
Benoit Parrotf625d462015-02-02 11:44:44 -06004062 /* No particular flag request, return here... */
4063 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
4064 pr_debug("no flags found for %s\n", con_id);
4065 return 0;
4066 }
4067
4068 /* Process flags */
4069 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
4070 status = gpiod_direction_output(desc,
Linus Walleijad177312016-11-13 23:02:44 +01004071 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
Benoit Parrotf625d462015-02-02 11:44:44 -06004072 else
4073 status = gpiod_direction_input(desc);
4074
4075 return status;
4076}
4077
Thierry Reding29a1f2332014-04-25 17:10:06 +02004078/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004079 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02004080 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004081 * @con_id: function within the GPIO consumer
4082 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004083 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004084 *
4085 * This variant of gpiod_get() allows to access GPIOs other than the first
4086 * defined one for functions that define several GPIOs.
4087 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004088 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
4089 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07004090 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004091 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004092struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004093 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004094 unsigned int idx,
4095 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004096{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09004097 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004098 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004099 enum gpio_lookup_flags lookupflags = 0;
Linus Walleij7d18f0a2018-01-16 08:29:50 +01004100 /* Maybe we have a device name, maybe not */
4101 const char *devname = dev ? dev_name(dev) : "?";
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004102
4103 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
4104
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01004105 if (dev) {
4106 /* Using device tree? */
4107 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
4108 dev_dbg(dev, "using device tree for GPIO lookup\n");
4109 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
4110 } else if (ACPI_COMPANION(dev)) {
4111 dev_dbg(dev, "using ACPI for GPIO lookup\n");
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +03004112 desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags);
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01004113 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09004114 }
4115
4116 /*
4117 * Either we are not using DT or ACPI, or their lookup did not return
4118 * a result. In that case, use platform lookup as a fallback.
4119 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004120 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04004121 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004122 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004123 }
4124
4125 if (IS_ERR(desc)) {
Wang Dongsheng9d5a1f22018-02-27 00:12:13 -08004126 dev_dbg(dev, "No GPIO consumer %s found\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004127 return desc;
4128 }
4129
Linus Walleij7d18f0a2018-01-16 08:29:50 +01004130 /*
4131 * If a connection label was passed use that, else attempt to use
4132 * the device name as label
4133 */
4134 status = gpiod_request(desc, con_id ? con_id : devname);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004135 if (status < 0)
4136 return ERR_PTR(status);
4137
Johan Hovold85b03b32016-07-03 18:32:05 +02004138 status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004139 if (status < 0) {
4140 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
4141 gpiod_put(desc);
4142 return ERR_PTR(status);
4143 }
4144
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004145 return desc;
4146}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004147EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004148
4149/**
Linus Walleij6392cca2017-12-29 02:07:54 +01004150 * gpiod_get_from_of_node() - obtain a GPIO from an OF node
4151 * @node: handle of the OF node
4152 * @propname: name of the DT property representing the GPIO
4153 * @index: index of the GPIO to obtain for the consumer
Andy Shevchenkoa264d102017-01-09 16:02:28 +02004154 * @dflags: GPIO initialization flags
Thierry Reding950d55f52017-07-24 16:57:22 +02004155 * @label: label to attach to the requested GPIO
Mika Westerberg40b73182014-10-21 13:33:59 +02004156 *
Thierry Reding950d55f52017-07-24 16:57:22 +02004157 * Returns:
Andy Shevchenkoff213782017-02-28 17:03:12 +02004158 * On successful request the GPIO pin is configured in accordance with
Linus Walleij6392cca2017-12-29 02:07:54 +01004159 * provided @dflags. If the node does not have the requested GPIO
4160 * property, NULL is returned.
Andy Shevchenkoa264d102017-01-09 16:02:28 +02004161 *
Mika Westerberg40b73182014-10-21 13:33:59 +02004162 * In case of error an ERR_PTR() is returned.
4163 */
Linus Walleij92542ed2017-12-29 22:52:02 +01004164struct gpio_desc *gpiod_get_from_of_node(struct device_node *node,
4165 const char *propname, int index,
4166 enum gpiod_flags dflags,
4167 const char *label)
Mika Westerberg40b73182014-10-21 13:33:59 +02004168{
Colin Ian King40a3c9d2018-01-16 11:56:11 +00004169 struct gpio_desc *desc;
Andy Shevchenkoa264d102017-01-09 16:02:28 +02004170 unsigned long lflags = 0;
Linus Walleij6392cca2017-12-29 02:07:54 +01004171 enum of_gpio_flags flags;
Mika Westerberg40b73182014-10-21 13:33:59 +02004172 bool active_low = false;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03004173 bool single_ended = false;
Laxman Dewangan4c0facd2017-04-06 19:05:52 +05304174 bool open_drain = false;
Andrew Jefferye10f72b2017-11-30 14:25:24 +10304175 bool transitory = false;
Mika Westerberg40b73182014-10-21 13:33:59 +02004176 int ret;
4177
Linus Walleij6392cca2017-12-29 02:07:54 +01004178 desc = of_get_named_gpiod_flags(node, propname,
4179 index, &flags);
Mika Westerberg40b73182014-10-21 13:33:59 +02004180
Linus Walleij6392cca2017-12-29 02:07:54 +01004181 if (!desc || IS_ERR(desc)) {
4182 /* If it is not there, just return NULL */
4183 if (PTR_ERR(desc) == -ENOENT)
4184 return NULL;
4185 return desc;
Mika Westerberg40b73182014-10-21 13:33:59 +02004186 }
4187
Linus Walleij6392cca2017-12-29 02:07:54 +01004188 active_low = flags & OF_GPIO_ACTIVE_LOW;
4189 single_ended = flags & OF_GPIO_SINGLE_ENDED;
4190 open_drain = flags & OF_GPIO_OPEN_DRAIN;
4191 transitory = flags & OF_GPIO_TRANSITORY;
Mika Westerberg40b73182014-10-21 13:33:59 +02004192
Alexander Steinb2987d72017-01-12 17:39:24 +01004193 ret = gpiod_request(desc, label);
Johan Hovold85b03b32016-07-03 18:32:05 +02004194 if (ret)
4195 return ERR_PTR(ret);
4196
Mika Westerberg40b73182014-10-21 13:33:59 +02004197 if (active_low)
Andy Shevchenkoa264d102017-01-09 16:02:28 +02004198 lflags |= GPIO_ACTIVE_LOW;
Mika Westerberg40b73182014-10-21 13:33:59 +02004199
Laurent Pinchart90b665f2015-10-13 00:20:21 +03004200 if (single_ended) {
Laxman Dewangan4c0facd2017-04-06 19:05:52 +05304201 if (open_drain)
Andy Shevchenkoa264d102017-01-09 16:02:28 +02004202 lflags |= GPIO_OPEN_DRAIN;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03004203 else
Andy Shevchenkoa264d102017-01-09 16:02:28 +02004204 lflags |= GPIO_OPEN_SOURCE;
4205 }
4206
Andrew Jefferye10f72b2017-11-30 14:25:24 +10304207 if (transitory)
4208 lflags |= GPIO_TRANSITORY;
4209
Andy Shevchenkoa264d102017-01-09 16:02:28 +02004210 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
4211 if (ret < 0) {
4212 gpiod_put(desc);
4213 return ERR_PTR(ret);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03004214 }
4215
Mika Westerberg40b73182014-10-21 13:33:59 +02004216 return desc;
4217}
Linus Walleij92542ed2017-12-29 22:52:02 +01004218EXPORT_SYMBOL(gpiod_get_from_of_node);
Linus Walleij6392cca2017-12-29 02:07:54 +01004219
4220/**
Mika Westerberg40b73182014-10-21 13:33:59 +02004221 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
4222 * @fwnode: handle of the firmware node
4223 * @propname: name of the firmware property representing the GPIO
Linus Walleij6392cca2017-12-29 02:07:54 +01004224 * @index: index of the GPIO to obtain for the consumer
Mika Westerberg40b73182014-10-21 13:33:59 +02004225 * @dflags: GPIO initialization flags
4226 * @label: label to attach to the requested GPIO
4227 *
4228 * This function can be used for drivers that get their configuration
Linus Walleij6392cca2017-12-29 02:07:54 +01004229 * from opaque firmware.
Thierry Reding29a1f2332014-04-25 17:10:06 +02004230 *
Linus Walleij6392cca2017-12-29 02:07:54 +01004231 * The function properly finds the corresponding GPIO using whatever is the
Thierry Reding29a1f2332014-04-25 17:10:06 +02004232 * underlying firmware interface and then makes sure that the GPIO
4233 * descriptor is requested before it is returned to the caller.
4234 *
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004235 * Returns:
Thierry Reding29a1f2332014-04-25 17:10:06 +02004236 * On successful request the GPIO pin is configured in accordance with
4237 * provided @dflags.
4238 *
4239 * In case of error an ERR_PTR() is returned.
4240 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004241struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
Thierry Reding29a1f2332014-04-25 17:10:06 +02004242 const char *propname, int index,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004243 enum gpiod_flags dflags,
4244 const char *label)
Thierry Reding29a1f2332014-04-25 17:10:06 +02004245{
4246 struct gpio_desc *desc = ERR_PTR(-ENODEV);
4247 unsigned long lflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004248 int ret;
4249
David Brownelld2876d02008-02-04 22:28:20 -08004250 if (!fwnode)
David Brownelld2876d02008-02-04 22:28:20 -08004251 return ERR_PTR(-EINVAL);
4252
4253 if (is_of_node(fwnode)) {
Linus Walleij6392cca2017-12-29 02:07:54 +01004254 desc = gpiod_get_from_of_node(to_of_node(fwnode),
4255 propname, index,
4256 dflags,
4257 label);
4258 return desc;
David Brownelld2876d02008-02-04 22:28:20 -08004259 } else if (is_acpi_node(fwnode)) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09004260 struct acpi_gpio_info info;
David Brownelld2876d02008-02-04 22:28:20 -08004261
Linus Walleijd468bf92013-09-24 11:54:38 +02004262 desc = acpi_node_get_gpiod(fwnode, propname, index, &info);
Linus Walleij6392cca2017-12-29 02:07:54 +01004263 if (IS_ERR(desc))
4264 return desc;
4265
4266 acpi_gpio_update_gpiod_flags(&dflags, &info);
4267
4268 if (info.polarity == GPIO_ACTIVE_LOW)
4269 lflags |= GPIO_ACTIVE_LOW;
Thierry Redingf9c4a312012-04-12 13:26:01 +02004270 }
4271
Linus Walleij6392cca2017-12-29 02:07:54 +01004272 /* Currently only ACPI takes this path */
Thierry Redingf9c4a312012-04-12 13:26:01 +02004273 ret = gpiod_request(desc, label);
4274 if (ret)
4275 return ERR_PTR(ret);
Linus Walleijd468bf92013-09-24 11:54:38 +02004276
Thierry Redingf9c4a312012-04-12 13:26:01 +02004277 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
4278 if (ret < 0) {
4279 gpiod_put(desc);
4280 return ERR_PTR(ret);
4281 }
4282
4283 return desc;
Linus Walleijd468bf92013-09-24 11:54:38 +02004284}
4285EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
David Brownelld2876d02008-02-04 22:28:20 -08004286
4287/**
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07004288 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
David Brownelld8f388d82008-07-25 01:46:07 -07004289 * function
Linus Walleijd468bf92013-09-24 11:54:38 +02004290 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4291 * @con_id: function within the GPIO consumer
David Brownelld2876d02008-02-04 22:28:20 -08004292 * @index: index of the GPIO to obtain in the consumer
4293 * @flags: optional GPIO initialization flags
4294 *
4295 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
4296 * specified index was assigned to the requested function it will return NULL.
4297 * This is convenient for drivers that need to handle optional GPIOs.
Grant Likely362432a2013-02-09 09:41:49 +00004298 */
David Brownelld8f388d82008-07-25 01:46:07 -07004299struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09004300 const char *con_id,
Thierry Redingf9c4a312012-04-12 13:26:01 +02004301 unsigned int index,
4302 enum gpiod_flags flags)
4303{
Grant Likely362432a2013-02-09 09:41:49 +00004304 struct gpio_desc *desc;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09004305
Grant Likely362432a2013-02-09 09:41:49 +00004306 desc = gpiod_get_index(dev, con_id, index, flags);
4307 if (IS_ERR(desc)) {
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09004308 if (PTR_ERR(desc) == -ENOENT)
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004309 return NULL;
David Brownelld2876d02008-02-04 22:28:20 -08004310 }
4311
Benoit Parrotf625d462015-02-02 11:44:44 -06004312 return desc;
4313}
4314EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
4315
4316/**
4317 * gpiod_hog - Hog the specified GPIO desc given the provided flags
4318 * @desc: gpio whose value will be assigned
4319 * @name: gpio line name
4320 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
4321 * of_get_gpio_hog()
4322 * @dflags: gpiod_flags - optional GPIO initialization flags
4323 */
4324int gpiod_hog(struct gpio_desc *desc, const char *name,
4325 unsigned long lflags, enum gpiod_flags dflags)
4326{
4327 struct gpio_chip *chip;
4328 struct gpio_desc *local_desc;
4329 int hwnum;
4330 int status;
4331
Laxman Dewanganc31a5712016-03-11 19:13:21 +05304332 chip = gpiod_to_chip(desc);
4333 hwnum = gpio_chip_hwgpio(desc);
4334
4335 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
Benoit Parrotf625d462015-02-02 11:44:44 -06004336 if (IS_ERR(local_desc)) {
4337 status = PTR_ERR(local_desc);
Johan Hovold85b03b32016-07-03 18:32:05 +02004338 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
Benoit Parrotf625d462015-02-02 11:44:44 -06004339 name, chip->label, hwnum, status);
Laxman Dewanganc31a5712016-03-11 19:13:21 +05304340 return status;
4341 }
Benoit Parrotf625d462015-02-02 11:44:44 -06004342
4343 status = gpiod_configure_flags(desc, name, lflags, dflags);
4344 if (status < 0) {
4345 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed, %d\n",
4346 name, chip->label, hwnum, status);
4347 gpiochip_free_own_desc(desc);
4348 return status;
4349 }
4350
4351 /* Mark GPIO as hogged so it can be identified and removed later */
4352 set_bit(FLAG_IS_HOGGED, &desc->flags);
4353
4354 pr_info("GPIO line %d (%s) hogged as %s%s\n",
4355 desc_to_gpio(desc), name,
4356 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
4357 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
4358 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
4359
4360 return 0;
4361}
4362
4363/**
4364 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
4365 * @chip: gpio chip to act on
4366 *
4367 * This is only used by of_gpiochip_remove to free hogged gpios
4368 */
Linus Walleij1c3cdb12016-02-09 13:51:59 +01004369static void gpiochip_free_hogs(struct gpio_chip *chip)
4370{
Benoit Parrotf625d462015-02-02 11:44:44 -06004371 int id;
4372
4373 for (id = 0; id < chip->ngpio; id++) {
4374 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004375 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
4376 }
4377}
4378
4379/**
4380 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
4381 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4382 * @con_id: function within the GPIO consumer
4383 * @flags: optional GPIO initialization flags
4384 *
4385 * This function acquires all the GPIOs defined under a given function.
4386 *
4387 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
4388 * no GPIO has been assigned to the requested function, or another IS_ERR()
4389 * code if an error occurred while trying to acquire the GPIOs.
4390 */
4391struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
4392 const char *con_id,
4393 enum gpiod_flags flags)
4394{
4395 struct gpio_desc *desc;
4396 struct gpio_descs *descs;
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004397 struct gpio_array *array_info = NULL;
4398 struct gpio_chip *chip;
4399 int count, bitmap_size;
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004400
4401 count = gpiod_count(dev, con_id);
4402 if (count < 0)
4403 return ERR_PTR(count);
4404
Kees Cookacafe7e2018-05-08 13:45:50 -07004405 descs = kzalloc(struct_size(descs, desc, count), GFP_KERNEL);
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004406 if (!descs)
4407 return ERR_PTR(-ENOMEM);
4408
4409 for (descs->ndescs = 0; descs->ndescs < count; ) {
4410 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
4411 if (IS_ERR(desc)) {
4412 gpiod_put_array(descs);
4413 return ERR_CAST(desc);
4414 }
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004415
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004416 descs->desc[descs->ndescs] = desc;
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004417
4418 chip = gpiod_to_chip(desc);
4419 /*
Janusz Krzysztofikc4c958a2018-09-24 01:53:36 +02004420 * If pin hardware number of array member 0 is also 0, select
4421 * its chip as a candidate for fast bitmap processing path.
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004422 */
Janusz Krzysztofikc4c958a2018-09-24 01:53:36 +02004423 if (descs->ndescs == 0 && gpio_chip_hwgpio(desc) == 0) {
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004424 struct gpio_descs *array;
4425
4426 bitmap_size = BITS_TO_LONGS(chip->ngpio > count ?
4427 chip->ngpio : count);
4428
4429 array = kzalloc(struct_size(descs, desc, count) +
4430 struct_size(array_info, invert_mask,
4431 3 * bitmap_size), GFP_KERNEL);
4432 if (!array) {
4433 gpiod_put_array(descs);
4434 return ERR_PTR(-ENOMEM);
4435 }
4436
4437 memcpy(array, descs,
4438 struct_size(descs, desc, descs->ndescs + 1));
4439 kfree(descs);
4440
4441 descs = array;
4442 array_info = (void *)(descs->desc + count);
4443 array_info->get_mask = array_info->invert_mask +
4444 bitmap_size;
4445 array_info->set_mask = array_info->get_mask +
4446 bitmap_size;
4447
4448 array_info->desc = descs->desc;
4449 array_info->size = count;
4450 array_info->chip = chip;
4451 bitmap_set(array_info->get_mask, descs->ndescs,
4452 count - descs->ndescs);
4453 bitmap_set(array_info->set_mask, descs->ndescs,
4454 count - descs->ndescs);
4455 descs->info = array_info;
4456 }
Janusz Krzysztofikc4c958a2018-09-24 01:53:36 +02004457 /* Unmark array members which don't belong to the 'fast' chip */
4458 if (array_info && array_info->chip != chip) {
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004459 __clear_bit(descs->ndescs, array_info->get_mask);
4460 __clear_bit(descs->ndescs, array_info->set_mask);
Janusz Krzysztofikc4c958a2018-09-24 01:53:36 +02004461 }
4462 /*
4463 * Detect array members which belong to the 'fast' chip
4464 * but their pins are not in hardware order.
4465 */
4466 else if (array_info &&
4467 gpio_chip_hwgpio(desc) != descs->ndescs) {
4468 /*
4469 * Don't use fast path if all array members processed so
4470 * far belong to the same chip as this one but its pin
4471 * hardware number is different from its array index.
4472 */
4473 if (bitmap_full(array_info->get_mask, descs->ndescs)) {
4474 array_info = NULL;
4475 } else {
4476 __clear_bit(descs->ndescs,
4477 array_info->get_mask);
4478 __clear_bit(descs->ndescs,
4479 array_info->set_mask);
4480 }
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004481 } else if (array_info) {
4482 /* Exclude open drain or open source from fast output */
4483 if (gpiochip_line_is_open_drain(chip, descs->ndescs) ||
4484 gpiochip_line_is_open_source(chip, descs->ndescs))
4485 __clear_bit(descs->ndescs,
4486 array_info->set_mask);
4487 /* Identify 'fast' pins which require invertion */
4488 if (gpiod_is_active_low(desc))
4489 __set_bit(descs->ndescs,
4490 array_info->invert_mask);
4491 }
4492
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004493 descs->ndescs++;
4494 }
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004495 if (array_info)
4496 dev_dbg(dev,
4497 "GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n",
4498 array_info->chip->label, array_info->size,
4499 *array_info->get_mask, *array_info->set_mask,
4500 *array_info->invert_mask);
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004501 return descs;
4502}
4503EXPORT_SYMBOL_GPL(gpiod_get_array);
4504
4505/**
4506 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
4507 * function
4508 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4509 * @con_id: function within the GPIO consumer
4510 * @flags: optional GPIO initialization flags
4511 *
4512 * This is equivalent to gpiod_get_array(), except that when no GPIO was
4513 * assigned to the requested function it will return NULL.
4514 */
4515struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
4516 const char *con_id,
4517 enum gpiod_flags flags)
4518{
4519 struct gpio_descs *descs;
4520
4521 descs = gpiod_get_array(dev, con_id, flags);
4522 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
4523 return NULL;
4524
4525 return descs;
4526}
4527EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
4528
4529/**
David Brownelld2876d02008-02-04 22:28:20 -08004530 * gpiod_put - dispose of a GPIO descriptor
4531 * @desc: GPIO descriptor to dispose of
4532 *
4533 * No descriptor can be used after gpiod_put() has been called on it.
4534 */
4535void gpiod_put(struct gpio_desc *desc)
4536{
4537 gpiod_free(desc);
4538}
4539EXPORT_SYMBOL_GPL(gpiod_put);
4540
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004541/**
4542 * gpiod_put_array - dispose of multiple GPIO descriptors
4543 * @descs: struct gpio_descs containing an array of descriptors
4544 */
4545void gpiod_put_array(struct gpio_descs *descs)
4546{
4547 unsigned int i;
4548
4549 for (i = 0; i < descs->ndescs; i++)
4550 gpiod_put(descs->desc[i]);
4551
4552 kfree(descs);
4553}
4554EXPORT_SYMBOL_GPL(gpiod_put_array);
4555
Linus Walleij3c702e92015-10-21 15:29:53 +02004556static int __init gpiolib_dev_init(void)
4557{
4558 int ret;
4559
4560 /* Register GPIO sysfs bus */
Andy Shevchenkob1911712018-07-03 03:39:03 +03004561 ret = bus_register(&gpio_bus_type);
Linus Walleij3c702e92015-10-21 15:29:53 +02004562 if (ret < 0) {
4563 pr_err("gpiolib: could not register GPIO bus type\n");
4564 return ret;
4565 }
4566
4567 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
4568 if (ret < 0) {
4569 pr_err("gpiolib: failed to allocate char dev region\n");
4570 bus_unregister(&gpio_bus_type);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07004571 } else {
4572 gpiolib_initialized = true;
4573 gpiochip_setup_devs();
Linus Walleij3c702e92015-10-21 15:29:53 +02004574 }
4575 return ret;
4576}
4577core_initcall(gpiolib_dev_init);
4578
David Brownelld2876d02008-02-04 22:28:20 -08004579#ifdef CONFIG_DEBUG_FS
4580
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004581static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
David Brownelld2876d02008-02-04 22:28:20 -08004582{
4583 unsigned i;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004584 struct gpio_chip *chip = gdev->chip;
4585 unsigned gpio = gdev->base;
4586 struct gpio_desc *gdesc = &gdev->descs[0];
David Brownelld2876d02008-02-04 22:28:20 -08004587 int is_out;
4588 int is_irq;
4589
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004590 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02004591 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
4592 if (gdesc->name) {
4593 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
4594 gpio, gdesc->name);
4595 }
David Brownelld2876d02008-02-04 22:28:20 -08004596 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02004597 }
David Brownelld2876d02008-02-04 22:28:20 -08004598
4599 gpiod_get_direction(gdesc);
4600 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
4601 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Markus Pargmannced433e2015-08-14 16:11:02 +02004602 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
4603 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08004604 is_out ? "out" : "in ",
Andy Shevchenko1c22a252018-07-12 20:36:42 +03004605 chip->get ? (chip->get(chip, i) ? "hi" : "lo") : "? ",
David Brownelld2876d02008-02-04 22:28:20 -08004606 is_irq ? "IRQ" : " ");
4607 seq_printf(s, "\n");
4608 }
4609}
4610
4611static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
4612{
4613 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02004614 struct gpio_device *gdev = NULL;
David Brownelld2876d02008-02-04 22:28:20 -08004615 loff_t index = *pos;
4616
4617 s->private = "";
4618
4619 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02004620 list_for_each_entry(gdev, &gpio_devices, list)
David Brownelld2876d02008-02-04 22:28:20 -08004621 if (index-- == 0) {
4622 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02004623 return gdev;
David Brownelld2876d02008-02-04 22:28:20 -08004624 }
4625 spin_unlock_irqrestore(&gpio_lock, flags);
4626
4627 return NULL;
4628}
4629
4630static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
4631{
4632 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02004633 struct gpio_device *gdev = v;
David Brownelld2876d02008-02-04 22:28:20 -08004634 void *ret = NULL;
4635
4636 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02004637 if (list_is_last(&gdev->list, &gpio_devices))
David Brownelld2876d02008-02-04 22:28:20 -08004638 ret = NULL;
4639 else
Linus Walleijff2b1352015-10-20 11:10:38 +02004640 ret = list_entry(gdev->list.next, struct gpio_device, list);
David Brownelld2876d02008-02-04 22:28:20 -08004641 spin_unlock_irqrestore(&gpio_lock, flags);
4642
4643 s->private = "\n";
4644 ++*pos;
4645
4646 return ret;
4647}
4648
4649static void gpiolib_seq_stop(struct seq_file *s, void *v)
4650{
4651}
4652
4653static int gpiolib_seq_show(struct seq_file *s, void *v)
4654{
Linus Walleijff2b1352015-10-20 11:10:38 +02004655 struct gpio_device *gdev = v;
4656 struct gpio_chip *chip = gdev->chip;
4657 struct device *parent;
David Brownelld2876d02008-02-04 22:28:20 -08004658
Linus Walleijff2b1352015-10-20 11:10:38 +02004659 if (!chip) {
4660 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
4661 dev_name(&gdev->dev));
4662 return 0;
4663 }
4664
4665 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
4666 dev_name(&gdev->dev),
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004667 gdev->base, gdev->base + gdev->ngpio - 1);
Linus Walleijff2b1352015-10-20 11:10:38 +02004668 parent = chip->parent;
4669 if (parent)
4670 seq_printf(s, ", parent: %s/%s",
4671 parent->bus ? parent->bus->name : "no-bus",
4672 dev_name(parent));
David Brownelld2876d02008-02-04 22:28:20 -08004673 if (chip->label)
4674 seq_printf(s, ", %s", chip->label);
4675 if (chip->can_sleep)
4676 seq_printf(s, ", can sleep");
4677 seq_printf(s, ":\n");
4678
4679 if (chip->dbg_show)
4680 chip->dbg_show(s, chip);
4681 else
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004682 gpiolib_dbg_show(s, gdev);
David Brownelld2876d02008-02-04 22:28:20 -08004683
4684 return 0;
4685}
4686
4687static const struct seq_operations gpiolib_seq_ops = {
4688 .start = gpiolib_seq_start,
4689 .next = gpiolib_seq_next,
4690 .stop = gpiolib_seq_stop,
4691 .show = gpiolib_seq_show,
4692};
4693
4694static int gpiolib_open(struct inode *inode, struct file *file)
4695{
4696 return seq_open(file, &gpiolib_seq_ops);
4697}
4698
4699static const struct file_operations gpiolib_operations = {
4700 .owner = THIS_MODULE,
4701 .open = gpiolib_open,
4702 .read = seq_read,
4703 .llseek = seq_lseek,
4704 .release = seq_release,
4705};
4706
4707static int __init gpiolib_debugfs_init(void)
4708{
4709 /* /sys/kernel/debug/gpio */
4710 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
4711 NULL, NULL, &gpiolib_operations);
4712 return 0;
4713}
4714subsys_initcall(gpiolib_debugfs_init);
4715
4716#endif /* DEBUG_FS */