blob: 03883f519d53dff0b709f4f73d5cfa496c227c6f [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>
Daniel Glöcknerff77c352009-09-22 16:46:38 -070014#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Rafael J. Wysocki7b199812013-11-11 22:41:56 +010016#include <linux/acpi.h>
Alexandre Courbot53e7cac2013-11-16 21:44:52 +090017#include <linux/gpio/driver.h>
Linus Walleij0a6d3152014-07-24 20:08:55 +020018#include <linux/gpio/machine.h>
Jonas Gorskic771c2f2015-10-11 17:34:15 +020019#include <linux/pinctrl/consumer.h>
Linus Walleij3c702e92015-10-21 15:29:53 +020020#include <linux/cdev.h>
21#include <linux/fs.h>
22#include <linux/uaccess.h>
Linus Walleij8b92e172016-05-27 14:24:04 +020023#include <linux/compat.h>
Linus Walleijd7c51b42016-04-26 10:35:29 +020024#include <linux/anon_inodes.h>
Lars-Peter Clausen953b9562016-10-24 13:59:15 +020025#include <linux/file.h>
Linus Walleij61f922d2016-06-02 11:30:15 +020026#include <linux/kfifo.h>
27#include <linux/poll.h>
28#include <linux/timekeeping.h>
Linus Walleij3c702e92015-10-21 15:29:53 +020029#include <uapi/linux/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080030
Mika Westerberg664e3e52014-01-08 12:40:54 +020031#include "gpiolib.h"
Linus Walleijf626d6d2019-07-17 09:10:01 +020032#include "gpiolib-of.h"
Mika Westerberg664e3e52014-01-08 12:40:54 +020033
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
Linus Walleijf626d6d2019-07-17 09:10:01 +0200363static int gpiochip_alloc_valid_mask(struct gpio_chip *gc)
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700364{
Linus Walleijf626d6d2019-07-17 09:10:01 +0200365 if (IS_ENABLED(CONFIG_OF_GPIO))
366 gc->need_valid_mask = of_gpio_need_valid_mask(gc);
367 if (!gc->need_valid_mask)
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700368 return 0;
369
Linus Walleijf626d6d2019-07-17 09:10:01 +0200370 gc->valid_mask = gpiochip_allocate_mask(gc);
371 if (!gc->valid_mask)
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700372 return -ENOMEM;
373
374 return 0;
375}
376
Ricardo Ribalda Delgadof8ec92a2018-10-05 08:52:58 +0200377static int gpiochip_init_valid_mask(struct gpio_chip *gpiochip)
378{
379 if (gpiochip->init_valid_mask)
380 return gpiochip->init_valid_mask(gpiochip);
381
382 return 0;
383}
384
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700385static void gpiochip_free_valid_mask(struct gpio_chip *gpiochip)
386{
387 kfree(gpiochip->valid_mask);
388 gpiochip->valid_mask = NULL;
389}
390
391bool gpiochip_line_is_valid(const struct gpio_chip *gpiochip,
392 unsigned int offset)
393{
394 /* No mask means all valid */
395 if (likely(!gpiochip->valid_mask))
396 return true;
397 return test_bit(offset, gpiochip->valid_mask);
398}
399EXPORT_SYMBOL_GPL(gpiochip_line_is_valid);
400
Linus Walleijd7c51b42016-04-26 10:35:29 +0200401/*
402 * GPIO line handle management
403 */
404
405/**
406 * struct linehandle_state - contains the state of a userspace handle
407 * @gdev: the GPIO device the handle pertains to
408 * @label: consumer label used to tag descriptors
409 * @descs: the GPIO descriptors held by this handle
410 * @numdescs: the number of descriptors held in the descs array
411 */
412struct linehandle_state {
413 struct gpio_device *gdev;
414 const char *label;
415 struct gpio_desc *descs[GPIOHANDLES_MAX];
416 u32 numdescs;
417};
418
Lars-Peter Clausene3e847c2016-10-18 16:54:05 +0200419#define GPIOHANDLE_REQUEST_VALID_FLAGS \
420 (GPIOHANDLE_REQUEST_INPUT | \
421 GPIOHANDLE_REQUEST_OUTPUT | \
422 GPIOHANDLE_REQUEST_ACTIVE_LOW | \
423 GPIOHANDLE_REQUEST_OPEN_DRAIN | \
424 GPIOHANDLE_REQUEST_OPEN_SOURCE)
425
Linus Walleijd7c51b42016-04-26 10:35:29 +0200426static long linehandle_ioctl(struct file *filep, unsigned int cmd,
427 unsigned long arg)
428{
429 struct linehandle_state *lh = filep->private_data;
430 void __user *ip = (void __user *)arg;
431 struct gpiohandle_data ghd;
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +0200432 DECLARE_BITMAP(vals, GPIOHANDLES_MAX);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200433 int i;
434
435 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
Bartosz Golaszewski2b955b32018-07-16 10:34:24 +0200436 /* NOTE: It's ok to read values of output lines. */
Lukas Wunnereec1d562017-10-12 12:40:10 +0200437 int ret = gpiod_get_array_value_complex(false,
438 true,
439 lh->numdescs,
440 lh->descs,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +0200441 NULL,
Lukas Wunnereec1d562017-10-12 12:40:10 +0200442 vals);
443 if (ret)
444 return ret;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200445
Lars-Peter Clausen3eded5d2016-10-18 16:54:02 +0200446 memset(&ghd, 0, sizeof(ghd));
Lukas Wunnereec1d562017-10-12 12:40:10 +0200447 for (i = 0; i < lh->numdescs; i++)
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +0200448 ghd.values[i] = test_bit(i, vals);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200449
450 if (copy_to_user(ip, &ghd, sizeof(ghd)))
451 return -EFAULT;
452
453 return 0;
454 } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
Bartosz Golaszewskie5332d52018-07-16 10:34:23 +0200455 /*
456 * All line descriptors were created at once with the same
457 * flags so just check if the first one is really output.
458 */
459 if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags))
460 return -EPERM;
461
Linus Walleijd7c51b42016-04-26 10:35:29 +0200462 if (copy_from_user(&ghd, ip, sizeof(ghd)))
463 return -EFAULT;
464
465 /* Clamp all values to [0,1] */
466 for (i = 0; i < lh->numdescs; i++)
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +0200467 __assign_bit(i, vals, ghd.values[i]);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200468
469 /* Reuse the array setting function */
Laura Abbott30277432018-05-21 10:57:07 -0700470 return gpiod_set_array_value_complex(false,
Linus Walleijd7c51b42016-04-26 10:35:29 +0200471 true,
472 lh->numdescs,
473 lh->descs,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +0200474 NULL,
Linus Walleijd7c51b42016-04-26 10:35:29 +0200475 vals);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200476 }
477 return -EINVAL;
478}
479
480#ifdef CONFIG_COMPAT
481static long linehandle_ioctl_compat(struct file *filep, unsigned int cmd,
482 unsigned long arg)
483{
484 return linehandle_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
485}
486#endif
487
488static int linehandle_release(struct inode *inode, struct file *filep)
489{
490 struct linehandle_state *lh = filep->private_data;
491 struct gpio_device *gdev = lh->gdev;
492 int i;
493
494 for (i = 0; i < lh->numdescs; i++)
495 gpiod_free(lh->descs[i]);
496 kfree(lh->label);
497 kfree(lh);
498 put_device(&gdev->dev);
499 return 0;
500}
501
502static const struct file_operations linehandle_fileops = {
503 .release = linehandle_release,
504 .owner = THIS_MODULE,
505 .llseek = noop_llseek,
506 .unlocked_ioctl = linehandle_ioctl,
507#ifdef CONFIG_COMPAT
508 .compat_ioctl = linehandle_ioctl_compat,
509#endif
510};
511
512static int linehandle_create(struct gpio_device *gdev, void __user *ip)
513{
514 struct gpiohandle_request handlereq;
515 struct linehandle_state *lh;
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200516 struct file *file;
Timur Tabiab3dbcf2018-03-29 13:29:12 -0500517 int fd, i, count = 0, ret;
Bartosz Golaszewski418ee8e2017-10-16 11:32:29 +0200518 u32 lflags;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200519
520 if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
521 return -EFAULT;
522 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
523 return -EINVAL;
524
Bartosz Golaszewski418ee8e2017-10-16 11:32:29 +0200525 lflags = handlereq.flags;
526
527 /* Return an error if an unknown flag is set */
528 if (lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
529 return -EINVAL;
530
Bartosz Golaszewski588fc3b2017-11-15 16:47:43 +0100531 /*
532 * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
533 * the hardware actually supports enabling both at the same time the
534 * electrical result would be disastrous.
535 */
536 if ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) &&
537 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
538 return -EINVAL;
539
Bartosz Golaszewski609aaf62017-10-16 11:32:30 +0200540 /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
541 if (!(lflags & GPIOHANDLE_REQUEST_OUTPUT) &&
542 ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
543 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
544 return -EINVAL;
545
Linus Walleijd7c51b42016-04-26 10:35:29 +0200546 lh = kzalloc(sizeof(*lh), GFP_KERNEL);
547 if (!lh)
548 return -ENOMEM;
549 lh->gdev = gdev;
550 get_device(&gdev->dev);
551
552 /* Make sure this is terminated */
553 handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
554 if (strlen(handlereq.consumer_label)) {
555 lh->label = kstrdup(handlereq.consumer_label,
556 GFP_KERNEL);
557 if (!lh->label) {
558 ret = -ENOMEM;
559 goto out_free_lh;
560 }
561 }
562
563 /* Request each GPIO */
564 for (i = 0; i < handlereq.lines; i++) {
565 u32 offset = handlereq.lineoffsets[i];
Linus Walleijd7c51b42016-04-26 10:35:29 +0200566 struct gpio_desc *desc;
567
Lars-Peter Clausene405f9f2016-10-18 16:54:01 +0200568 if (offset >= gdev->ngpio) {
569 ret = -EINVAL;
570 goto out_free_descs;
571 }
572
Linus Walleijd7c51b42016-04-26 10:35:29 +0200573 desc = &gdev->descs[offset];
574 ret = gpiod_request(desc, lh->label);
575 if (ret)
576 goto out_free_descs;
577 lh->descs[i] = desc;
Ricardo Ribalda Delgado19a4fbf2018-09-13 15:37:04 +0200578 count = i + 1;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200579
580 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
581 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
582 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
583 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
584 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
585 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
586
Andrew Jefferye10f72b2017-11-30 14:25:24 +1030587 ret = gpiod_set_transitory(desc, false);
588 if (ret < 0)
589 goto out_free_descs;
590
Linus Walleijd7c51b42016-04-26 10:35:29 +0200591 /*
592 * Lines have to be requested explicitly for input
593 * or output, else the line will be treated "as is".
594 */
595 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
596 int val = !!handlereq.default_values[i];
597
598 ret = gpiod_direction_output(desc, val);
599 if (ret)
600 goto out_free_descs;
601 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
602 ret = gpiod_direction_input(desc);
603 if (ret)
604 goto out_free_descs;
605 }
606 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
607 offset);
608 }
Linus Walleije2f608b2016-06-18 10:56:43 +0200609 /* Let i point at the last handle */
610 i--;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200611 lh->numdescs = handlereq.lines;
612
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200613 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200614 if (fd < 0) {
615 ret = fd;
616 goto out_free_descs;
617 }
618
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200619 file = anon_inode_getfile("gpio-linehandle",
620 &linehandle_fileops,
621 lh,
622 O_RDONLY | O_CLOEXEC);
623 if (IS_ERR(file)) {
624 ret = PTR_ERR(file);
625 goto out_put_unused_fd;
626 }
627
Linus Walleijd7c51b42016-04-26 10:35:29 +0200628 handlereq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200629 if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200630 /*
631 * fput() will trigger the release() callback, so do not go onto
632 * the regular error cleanup path here.
633 */
634 fput(file);
635 put_unused_fd(fd);
636 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +0200637 }
Linus Walleijd7c51b42016-04-26 10:35:29 +0200638
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200639 fd_install(fd, file);
640
Linus Walleijd7c51b42016-04-26 10:35:29 +0200641 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
642 lh->numdescs);
643
644 return 0;
645
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200646out_put_unused_fd:
647 put_unused_fd(fd);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200648out_free_descs:
Timur Tabiab3dbcf2018-03-29 13:29:12 -0500649 for (i = 0; i < count; i++)
Linus Walleijd7c51b42016-04-26 10:35:29 +0200650 gpiod_free(lh->descs[i]);
651 kfree(lh->label);
652out_free_lh:
653 kfree(lh);
654 put_device(&gdev->dev);
655 return ret;
656}
657
Linus Walleij61f922d2016-06-02 11:30:15 +0200658/*
659 * GPIO line event management
660 */
661
662/**
663 * struct lineevent_state - contains the state of a userspace event
664 * @gdev: the GPIO device the event pertains to
665 * @label: consumer label used to tag descriptors
666 * @desc: the GPIO descriptor held by this event
667 * @eflags: the event flags this line was requested with
668 * @irq: the interrupt that trigger in response to events on this GPIO
669 * @wait: wait queue that handles blocking reads of events
670 * @events: KFIFO for the GPIO events
671 * @read_lock: mutex lock to protect reads from colliding with adding
672 * new events to the FIFO
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100673 * @timestamp: cache for the timestamp storing it between hardirq
674 * and IRQ thread, used to bring the timestamp close to the actual
675 * event
Linus Walleij61f922d2016-06-02 11:30:15 +0200676 */
677struct lineevent_state {
678 struct gpio_device *gdev;
679 const char *label;
680 struct gpio_desc *desc;
681 u32 eflags;
682 int irq;
683 wait_queue_head_t wait;
684 DECLARE_KFIFO(events, struct gpioevent_data, 16);
685 struct mutex read_lock;
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100686 u64 timestamp;
Linus Walleij61f922d2016-06-02 11:30:15 +0200687};
688
Lars-Peter Clausenac7dbb92016-10-18 16:54:06 +0200689#define GPIOEVENT_REQUEST_VALID_FLAGS \
690 (GPIOEVENT_REQUEST_RISING_EDGE | \
691 GPIOEVENT_REQUEST_FALLING_EDGE)
692
Al Viroafc9a422017-07-03 06:39:46 -0400693static __poll_t lineevent_poll(struct file *filep,
Linus Walleij61f922d2016-06-02 11:30:15 +0200694 struct poll_table_struct *wait)
695{
696 struct lineevent_state *le = filep->private_data;
Al Viroafc9a422017-07-03 06:39:46 -0400697 __poll_t events = 0;
Linus Walleij61f922d2016-06-02 11:30:15 +0200698
699 poll_wait(filep, &le->wait, wait);
700
701 if (!kfifo_is_empty(&le->events))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800702 events = EPOLLIN | EPOLLRDNORM;
Linus Walleij61f922d2016-06-02 11:30:15 +0200703
704 return events;
705}
706
707
708static ssize_t lineevent_read(struct file *filep,
709 char __user *buf,
710 size_t count,
711 loff_t *f_ps)
712{
713 struct lineevent_state *le = filep->private_data;
714 unsigned int copied;
715 int ret;
716
717 if (count < sizeof(struct gpioevent_data))
718 return -EINVAL;
719
720 do {
721 if (kfifo_is_empty(&le->events)) {
722 if (filep->f_flags & O_NONBLOCK)
723 return -EAGAIN;
724
725 ret = wait_event_interruptible(le->wait,
726 !kfifo_is_empty(&le->events));
727 if (ret)
728 return ret;
729 }
730
731 if (mutex_lock_interruptible(&le->read_lock))
732 return -ERESTARTSYS;
733 ret = kfifo_to_user(&le->events, buf, count, &copied);
734 mutex_unlock(&le->read_lock);
735
736 if (ret)
737 return ret;
738
739 /*
740 * If we couldn't read anything from the fifo (a different
741 * thread might have been faster) we either return -EAGAIN if
742 * the file descriptor is non-blocking, otherwise we go back to
743 * sleep and wait for more data to arrive.
744 */
745 if (copied == 0 && (filep->f_flags & O_NONBLOCK))
746 return -EAGAIN;
747
748 } while (copied == 0);
749
750 return copied;
751}
752
753static int lineevent_release(struct inode *inode, struct file *filep)
754{
755 struct lineevent_state *le = filep->private_data;
756 struct gpio_device *gdev = le->gdev;
757
758 free_irq(le->irq, le);
759 gpiod_free(le->desc);
760 kfree(le->label);
761 kfree(le);
762 put_device(&gdev->dev);
763 return 0;
764}
765
766static long lineevent_ioctl(struct file *filep, unsigned int cmd,
767 unsigned long arg)
768{
769 struct lineevent_state *le = filep->private_data;
770 void __user *ip = (void __user *)arg;
771 struct gpiohandle_data ghd;
772
773 /*
774 * We can get the value for an event line but not set it,
775 * because it is input by definition.
776 */
777 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
778 int val;
779
Lars-Peter Clausend82aa4a2016-10-18 16:54:04 +0200780 memset(&ghd, 0, sizeof(ghd));
781
Linus Walleij61f922d2016-06-02 11:30:15 +0200782 val = gpiod_get_value_cansleep(le->desc);
783 if (val < 0)
784 return val;
785 ghd.values[0] = val;
786
787 if (copy_to_user(ip, &ghd, sizeof(ghd)))
788 return -EFAULT;
789
790 return 0;
791 }
792 return -EINVAL;
793}
794
795#ifdef CONFIG_COMPAT
796static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd,
797 unsigned long arg)
798{
799 return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
800}
801#endif
802
803static const struct file_operations lineevent_fileops = {
804 .release = lineevent_release,
805 .read = lineevent_read,
806 .poll = lineevent_poll,
807 .owner = THIS_MODULE,
808 .llseek = noop_llseek,
809 .unlocked_ioctl = lineevent_ioctl,
810#ifdef CONFIG_COMPAT
811 .compat_ioctl = lineevent_ioctl_compat,
812#endif
813};
814
Ben Dooks33265b12016-06-17 16:03:13 +0100815static irqreturn_t lineevent_irq_thread(int irq, void *p)
Linus Walleij61f922d2016-06-02 11:30:15 +0200816{
817 struct lineevent_state *le = p;
818 struct gpioevent_data ge;
Uwe Kleine-Königfa388692018-08-20 08:32:53 +0200819 int ret;
Linus Walleij61f922d2016-06-02 11:30:15 +0200820
Linus Walleij24bd3ef2018-01-22 13:19:28 +0100821 /* Do not leak kernel stack to userspace */
822 memset(&ge, 0, sizeof(ge));
823
Bartosz Golaszewski1033be52019-01-04 11:24:20 +0100824 /*
825 * We may be running from a nested threaded interrupt in which case
826 * we didn't get the timestamp from lineevent_irq_handler().
827 */
828 if (!le->timestamp)
829 ge.timestamp = ktime_get_real_ns();
830 else
831 ge.timestamp = le->timestamp;
Linus Walleij61f922d2016-06-02 11:30:15 +0200832
Bartosz Golaszewskiad537b82017-06-23 13:45:16 +0200833 if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
834 && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
Uwe Kleine-Königfa388692018-08-20 08:32:53 +0200835 int level = gpiod_get_value_cansleep(le->desc);
Linus Walleij61f922d2016-06-02 11:30:15 +0200836 if (level)
837 /* Emit low-to-high event */
838 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
839 else
840 /* Emit high-to-low event */
841 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Uwe Kleine-Königfa388692018-08-20 08:32:53 +0200842 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200843 /* Emit low-to-high event */
844 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
Uwe Kleine-Königfa388692018-08-20 08:32:53 +0200845 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200846 /* Emit high-to-low event */
847 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Arnd Bergmannbc0207a2016-06-16 11:02:41 +0200848 } else {
849 return IRQ_NONE;
Linus Walleij61f922d2016-06-02 11:30:15 +0200850 }
851
852 ret = kfifo_put(&le->events, ge);
853 if (ret != 0)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800854 wake_up_poll(&le->wait, EPOLLIN);
Linus Walleij61f922d2016-06-02 11:30:15 +0200855
856 return IRQ_HANDLED;
857}
858
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100859static irqreturn_t lineevent_irq_handler(int irq, void *p)
860{
861 struct lineevent_state *le = p;
862
863 /*
864 * Just store the timestamp in hardirq context so we get it as
865 * close in time as possible to the actual event.
866 */
867 le->timestamp = ktime_get_real_ns();
868
869 return IRQ_WAKE_THREAD;
870}
871
Linus Walleij61f922d2016-06-02 11:30:15 +0200872static int lineevent_create(struct gpio_device *gdev, void __user *ip)
873{
874 struct gpioevent_request eventreq;
875 struct lineevent_state *le;
876 struct gpio_desc *desc;
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200877 struct file *file;
Linus Walleij61f922d2016-06-02 11:30:15 +0200878 u32 offset;
879 u32 lflags;
880 u32 eflags;
881 int fd;
882 int ret;
883 int irqflags = 0;
884
885 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
886 return -EFAULT;
887
888 le = kzalloc(sizeof(*le), GFP_KERNEL);
889 if (!le)
890 return -ENOMEM;
891 le->gdev = gdev;
892 get_device(&gdev->dev);
893
894 /* Make sure this is terminated */
895 eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
896 if (strlen(eventreq.consumer_label)) {
897 le->label = kstrdup(eventreq.consumer_label,
898 GFP_KERNEL);
899 if (!le->label) {
900 ret = -ENOMEM;
901 goto out_free_le;
902 }
903 }
904
905 offset = eventreq.lineoffset;
906 lflags = eventreq.handleflags;
907 eflags = eventreq.eventflags;
908
Lars-Peter Clausenb8b0e3d2016-10-18 16:54:03 +0200909 if (offset >= gdev->ngpio) {
910 ret = -EINVAL;
911 goto out_free_label;
912 }
913
Lars-Peter Clausenac7dbb92016-10-18 16:54:06 +0200914 /* Return an error if a unknown flag is set */
915 if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
916 (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) {
917 ret = -EINVAL;
918 goto out_free_label;
919 }
920
Linus Walleij61f922d2016-06-02 11:30:15 +0200921 /* This is just wrong: we don't look for events on output lines */
922 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
923 ret = -EINVAL;
924 goto out_free_label;
925 }
926
927 desc = &gdev->descs[offset];
928 ret = gpiod_request(desc, le->label);
929 if (ret)
Uwe Kleine-Königf001cc32018-04-16 13:17:53 +0200930 goto out_free_label;
Linus Walleij61f922d2016-06-02 11:30:15 +0200931 le->desc = desc;
932 le->eflags = eflags;
933
934 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
935 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
936 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
937 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
938 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
939 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
940
941 ret = gpiod_direction_input(desc);
942 if (ret)
943 goto out_free_desc;
944
945 le->irq = gpiod_to_irq(desc);
946 if (le->irq <= 0) {
947 ret = -ENODEV;
948 goto out_free_desc;
949 }
950
951 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
952 irqflags |= IRQF_TRIGGER_RISING;
953 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
954 irqflags |= IRQF_TRIGGER_FALLING;
955 irqflags |= IRQF_ONESHOT;
Linus Walleij61f922d2016-06-02 11:30:15 +0200956
957 INIT_KFIFO(le->events);
958 init_waitqueue_head(&le->wait);
959 mutex_init(&le->read_lock);
960
961 /* Request a thread to read the events */
962 ret = request_threaded_irq(le->irq,
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100963 lineevent_irq_handler,
Linus Walleij61f922d2016-06-02 11:30:15 +0200964 lineevent_irq_thread,
965 irqflags,
966 le->label,
967 le);
968 if (ret)
969 goto out_free_desc;
970
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200971 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleij61f922d2016-06-02 11:30:15 +0200972 if (fd < 0) {
973 ret = fd;
974 goto out_free_irq;
975 }
976
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200977 file = anon_inode_getfile("gpio-event",
978 &lineevent_fileops,
979 le,
980 O_RDONLY | O_CLOEXEC);
981 if (IS_ERR(file)) {
982 ret = PTR_ERR(file);
983 goto out_put_unused_fd;
984 }
985
Linus Walleij61f922d2016-06-02 11:30:15 +0200986 eventreq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200987 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200988 /*
989 * fput() will trigger the release() callback, so do not go onto
990 * the regular error cleanup path here.
991 */
992 fput(file);
993 put_unused_fd(fd);
994 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +0200995 }
Linus Walleij61f922d2016-06-02 11:30:15 +0200996
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200997 fd_install(fd, file);
998
Linus Walleij61f922d2016-06-02 11:30:15 +0200999 return 0;
1000
Lars-Peter Clausen953b9562016-10-24 13:59:15 +02001001out_put_unused_fd:
1002 put_unused_fd(fd);
Linus Walleij61f922d2016-06-02 11:30:15 +02001003out_free_irq:
1004 free_irq(le->irq, le);
1005out_free_desc:
1006 gpiod_free(le->desc);
1007out_free_label:
1008 kfree(le->label);
1009out_free_le:
1010 kfree(le);
1011 put_device(&gdev->dev);
1012 return ret;
1013}
1014
Thierry Reding950d55f52017-07-24 16:57:22 +02001015/*
Linus Walleij3c702e92015-10-21 15:29:53 +02001016 * gpio_ioctl() - ioctl handler for the GPIO chardev
1017 */
1018static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1019{
1020 struct gpio_device *gdev = filp->private_data;
1021 struct gpio_chip *chip = gdev->chip;
Linus Walleij8b92e172016-05-27 14:24:04 +02001022 void __user *ip = (void __user *)arg;
Linus Walleij3c702e92015-10-21 15:29:53 +02001023
1024 /* We fail any subsequent ioctl():s when the chip is gone */
1025 if (!chip)
1026 return -ENODEV;
1027
Linus Walleij521a2ad2016-02-12 22:25:22 +01001028 /* Fill in the struct and pass to userspace */
Linus Walleij3c702e92015-10-21 15:29:53 +02001029 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
Linus Walleij521a2ad2016-02-12 22:25:22 +01001030 struct gpiochip_info chipinfo;
1031
Lars-Peter Clausen0f4bbb22016-10-18 16:54:00 +02001032 memset(&chipinfo, 0, sizeof(chipinfo));
1033
Linus Walleij3c702e92015-10-21 15:29:53 +02001034 strncpy(chipinfo.name, dev_name(&gdev->dev),
1035 sizeof(chipinfo.name));
1036 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
Linus Walleijdf4878e2016-02-12 14:48:23 +01001037 strncpy(chipinfo.label, gdev->label,
1038 sizeof(chipinfo.label));
1039 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001040 chipinfo.lines = gdev->ngpio;
Linus Walleij3c702e92015-10-21 15:29:53 +02001041 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
1042 return -EFAULT;
1043 return 0;
Linus Walleij521a2ad2016-02-12 22:25:22 +01001044 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
1045 struct gpioline_info lineinfo;
1046 struct gpio_desc *desc;
1047
1048 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
1049 return -EFAULT;
Lars-Peter Clausen1f1cc452016-10-18 16:53:59 +02001050 if (lineinfo.line_offset >= gdev->ngpio)
Linus Walleij521a2ad2016-02-12 22:25:22 +01001051 return -EINVAL;
1052
1053 desc = &gdev->descs[lineinfo.line_offset];
1054 if (desc->name) {
1055 strncpy(lineinfo.name, desc->name,
1056 sizeof(lineinfo.name));
1057 lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
1058 } else {
1059 lineinfo.name[0] = '\0';
1060 }
1061 if (desc->label) {
Linus Walleij214338e2016-02-25 21:01:48 +01001062 strncpy(lineinfo.consumer, desc->label,
1063 sizeof(lineinfo.consumer));
1064 lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +01001065 } else {
Linus Walleij214338e2016-02-25 21:01:48 +01001066 lineinfo.consumer[0] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +01001067 }
1068
1069 /*
1070 * Userspace only need to know that the kernel is using
1071 * this GPIO so it can't use it.
1072 */
1073 lineinfo.flags = 0;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001074 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
1075 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
1076 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
1077 test_bit(FLAG_EXPORT, &desc->flags) ||
1078 test_bit(FLAG_SYSFS, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001079 lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001080 if (test_bit(FLAG_IS_OUT, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001081 lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001082 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001083 lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001084 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001085 lineinfo.flags |= GPIOLINE_FLAG_OPEN_DRAIN;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001086 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001087 lineinfo.flags |= GPIOLINE_FLAG_OPEN_SOURCE;
1088
1089 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
1090 return -EFAULT;
1091 return 0;
Linus Walleijd7c51b42016-04-26 10:35:29 +02001092 } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
1093 return linehandle_create(gdev, ip);
Linus Walleij61f922d2016-06-02 11:30:15 +02001094 } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
1095 return lineevent_create(gdev, ip);
Linus Walleij3c702e92015-10-21 15:29:53 +02001096 }
1097 return -EINVAL;
1098}
1099
Linus Walleij8b92e172016-05-27 14:24:04 +02001100#ifdef CONFIG_COMPAT
1101static long gpio_ioctl_compat(struct file *filp, unsigned int cmd,
1102 unsigned long arg)
1103{
1104 return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
1105}
1106#endif
1107
Linus Walleij3c702e92015-10-21 15:29:53 +02001108/**
1109 * gpio_chrdev_open() - open the chardev for ioctl operations
1110 * @inode: inode for this chardev
1111 * @filp: file struct for storing private data
1112 * Returns 0 on success
1113 */
1114static int gpio_chrdev_open(struct inode *inode, struct file *filp)
1115{
1116 struct gpio_device *gdev = container_of(inode->i_cdev,
1117 struct gpio_device, chrdev);
1118
1119 /* Fail on open if the backing gpiochip is gone */
Stephen Boydfb505742017-01-09 11:47:44 -08001120 if (!gdev->chip)
Linus Walleij3c702e92015-10-21 15:29:53 +02001121 return -ENODEV;
1122 get_device(&gdev->dev);
1123 filp->private_data = gdev;
Lars-Peter Clausenf4e81c52016-11-30 13:05:21 +01001124
1125 return nonseekable_open(inode, filp);
Linus Walleij3c702e92015-10-21 15:29:53 +02001126}
1127
1128/**
1129 * gpio_chrdev_release() - close chardev after ioctl operations
1130 * @inode: inode for this chardev
1131 * @filp: file struct for storing private data
1132 * Returns 0 on success
1133 */
1134static int gpio_chrdev_release(struct inode *inode, struct file *filp)
1135{
1136 struct gpio_device *gdev = container_of(inode->i_cdev,
1137 struct gpio_device, chrdev);
1138
Linus Walleij3c702e92015-10-21 15:29:53 +02001139 put_device(&gdev->dev);
1140 return 0;
1141}
1142
1143
1144static const struct file_operations gpio_fileops = {
1145 .release = gpio_chrdev_release,
1146 .open = gpio_chrdev_open,
1147 .owner = THIS_MODULE,
Lars-Peter Clausenf4e81c52016-11-30 13:05:21 +01001148 .llseek = no_llseek,
Linus Walleij3c702e92015-10-21 15:29:53 +02001149 .unlocked_ioctl = gpio_ioctl,
Linus Walleij8b92e172016-05-27 14:24:04 +02001150#ifdef CONFIG_COMPAT
1151 .compat_ioctl = gpio_ioctl_compat,
1152#endif
Linus Walleij3c702e92015-10-21 15:29:53 +02001153};
1154
Linus Walleijff2b1352015-10-20 11:10:38 +02001155static void gpiodevice_release(struct device *dev)
1156{
1157 struct gpio_device *gdev = dev_get_drvdata(dev);
1158
1159 list_del(&gdev->list);
1160 ida_simple_remove(&gpio_ida, gdev->id);
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +01001161 kfree_const(gdev->label);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001162 kfree(gdev->descs);
Linus Walleij9efd9e62016-02-09 14:27:42 +01001163 kfree(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001164}
1165
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001166static int gpiochip_setup_dev(struct gpio_device *gdev)
1167{
1168 int status;
1169
1170 cdev_init(&gdev->chrdev, &gpio_fileops);
1171 gdev->chrdev.owner = THIS_MODULE;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001172 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001173
1174 status = cdev_device_add(&gdev->chrdev, &gdev->dev);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001175 if (status)
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001176 return status;
1177
1178 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
1179 MAJOR(gpio_devt), gdev->id);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001180
1181 status = gpiochip_sysfs_register(gdev);
1182 if (status)
1183 goto err_remove_device;
1184
1185 /* From this point, the .release() function cleans up gpio_device */
1186 gdev->dev.release = gpiodevice_release;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001187 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
1188 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
1189 dev_name(&gdev->dev), gdev->chip->label ? : "generic");
1190
1191 return 0;
1192
1193err_remove_device:
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001194 cdev_device_del(&gdev->chrdev, &gdev->dev);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001195 return status;
1196}
1197
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02001198static void gpiochip_machine_hog(struct gpio_chip *chip, struct gpiod_hog *hog)
1199{
1200 struct gpio_desc *desc;
1201 int rv;
1202
1203 desc = gpiochip_get_desc(chip, hog->chip_hwnum);
1204 if (IS_ERR(desc)) {
1205 pr_err("%s: unable to get GPIO desc: %ld\n",
1206 __func__, PTR_ERR(desc));
1207 return;
1208 }
1209
Dan Carpenterba3efdf2018-05-01 10:36:39 +03001210 if (test_bit(FLAG_IS_HOGGED, &desc->flags))
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02001211 return;
1212
1213 rv = gpiod_hog(desc, hog->line_name, hog->lflags, hog->dflags);
1214 if (rv)
1215 pr_err("%s: unable to hog GPIO line (%s:%u): %d\n",
1216 __func__, chip->label, hog->chip_hwnum, rv);
1217}
1218
1219static void machine_gpiochip_add(struct gpio_chip *chip)
1220{
1221 struct gpiod_hog *hog;
1222
1223 mutex_lock(&gpio_machine_hogs_mutex);
1224
1225 list_for_each_entry(hog, &gpio_machine_hogs, list) {
1226 if (!strcmp(chip->label, hog->chip_label))
1227 gpiochip_machine_hog(chip, hog);
1228 }
1229
1230 mutex_unlock(&gpio_machine_hogs_mutex);
1231}
1232
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001233static void gpiochip_setup_devs(void)
1234{
1235 struct gpio_device *gdev;
1236 int err;
1237
1238 list_for_each_entry(gdev, &gpio_devices, list) {
1239 err = gpiochip_setup_dev(gdev);
1240 if (err)
1241 pr_err("%s: Failed to initialize gpio device (%d)\n",
1242 dev_name(&gdev->dev), err);
1243 }
1244}
1245
Thierry Reding959bc7b2017-11-07 19:15:59 +01001246int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001247 struct lock_class_key *lock_key,
1248 struct lock_class_key *request_key)
David Brownelld2876d02008-02-04 22:28:20 -08001249{
1250 unsigned long flags;
1251 int status = 0;
Linus Walleijff2b1352015-10-20 11:10:38 +02001252 unsigned i;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001253 int base = chip->base;
Linus Walleijff2b1352015-10-20 11:10:38 +02001254 struct gpio_device *gdev;
David Brownelld2876d02008-02-04 22:28:20 -08001255
Linus Walleijff2b1352015-10-20 11:10:38 +02001256 /*
1257 * First: allocate and populate the internal stat container, and
1258 * set up the struct device.
1259 */
Josh Cartwright969f07b2016-02-17 16:44:15 -06001260 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
Linus Walleijff2b1352015-10-20 11:10:38 +02001261 if (!gdev)
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001262 return -ENOMEM;
Linus Walleij3c702e92015-10-21 15:29:53 +02001263 gdev->dev.bus = &gpio_bus_type;
Linus Walleijff2b1352015-10-20 11:10:38 +02001264 gdev->chip = chip;
1265 chip->gpiodev = gdev;
1266 if (chip->parent) {
1267 gdev->dev.parent = chip->parent;
1268 gdev->dev.of_node = chip->parent->of_node;
Thierry Redingacc6e332016-07-05 14:11:14 +02001269 }
1270
Linus Walleijff2b1352015-10-20 11:10:38 +02001271#ifdef CONFIG_OF_GPIO
1272 /* If the gpiochip has an assigned OF node this takes precedence */
Thierry Redingacc6e332016-07-05 14:11:14 +02001273 if (chip->of_node)
1274 gdev->dev.of_node = chip->of_node;
Biju Das6ff04972018-08-06 10:48:01 +01001275 else
1276 chip->of_node = gdev->dev.of_node;
Linus Walleijff2b1352015-10-20 11:10:38 +02001277#endif
Thierry Redingacc6e332016-07-05 14:11:14 +02001278
Linus Walleijff2b1352015-10-20 11:10:38 +02001279 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
1280 if (gdev->id < 0) {
1281 status = gdev->id;
1282 goto err_free_gdev;
1283 }
1284 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
1285 device_initialize(&gdev->dev);
1286 dev_set_drvdata(&gdev->dev, gdev);
1287 if (chip->parent && chip->parent->driver)
1288 gdev->owner = chip->parent->driver->owner;
1289 else if (chip->owner)
1290 /* TODO: remove chip->owner */
1291 gdev->owner = chip->owner;
1292 else
1293 gdev->owner = THIS_MODULE;
David Brownelld2876d02008-02-04 22:28:20 -08001294
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001295 gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001296 if (!gdev->descs) {
Linus Walleijff2b1352015-10-20 11:10:38 +02001297 status = -ENOMEM;
Vladimir Zapolskiya05a1402018-11-02 15:39:43 +02001298 goto err_free_ida;
Linus Walleijff2b1352015-10-20 11:10:38 +02001299 }
1300
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001301 if (chip->ngpio == 0) {
1302 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
Linus Walleijff2b1352015-10-20 11:10:38 +02001303 status = -EINVAL;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001304 goto err_free_descs;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001305 }
Linus Walleijdf4878e2016-02-12 14:48:23 +01001306
Laura Abbott30277432018-05-21 10:57:07 -07001307 if (chip->ngpio > FASTPATH_NGPIO)
1308 chip_warn(chip, "line cnt %u is greater than fast path cnt %u\n",
1309 chip->ngpio, FASTPATH_NGPIO);
1310
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +01001311 gdev->label = kstrdup_const(chip->label ?: "unknown", GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001312 if (!gdev->label) {
1313 status = -ENOMEM;
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001314 goto err_free_descs;
Linus Walleijdf4878e2016-02-12 14:48:23 +01001315 }
1316
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001317 gdev->ngpio = chip->ngpio;
Linus Walleij43c54ec2016-02-11 11:37:48 +01001318 gdev->data = data;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001319
David Brownelld2876d02008-02-04 22:28:20 -08001320 spin_lock_irqsave(&gpio_lock, flags);
1321
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001322 /*
1323 * TODO: this allocates a Linux GPIO number base in the global
1324 * GPIO numberspace for this chip. In the long run we want to
1325 * get *rid* of this numberspace and use only descriptors, but
1326 * it may be a pipe dream. It will not happen before we get rid
1327 * of the sysfs interface anyways.
1328 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001329 if (base < 0) {
1330 base = gpiochip_find_base(chip->ngpio);
1331 if (base < 0) {
1332 status = base;
Johan Hovold225fce82015-01-12 17:12:25 +01001333 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001334 goto err_free_label;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001335 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001336 /*
1337 * TODO: it should not be necessary to reflect the assigned
1338 * base outside of the GPIO subsystem. Go over drivers and
1339 * see if anyone makes use of this, else drop this and assign
1340 * a poison instead.
1341 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001342 chip->base = base;
1343 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001344 gdev->base = base;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001345
Linus Walleijff2b1352015-10-20 11:10:38 +02001346 status = gpiodev_add_to_list(gdev);
Johan Hovold05aa5202015-01-12 17:12:26 +01001347 if (status) {
1348 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001349 goto err_free_label;
Johan Hovold05aa5202015-01-12 17:12:26 +01001350 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001351
Linus Walleij545ebd92016-05-30 17:11:59 +02001352 spin_unlock_irqrestore(&gpio_lock, flags);
1353
Ricardo Ribalda Delgado767cd172018-10-12 08:11:36 +02001354 for (i = 0; i < chip->ngpio; i++)
1355 gdev->descs[i].gdev = gdev;
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001356
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301357#ifdef CONFIG_PINCTRL
Linus Walleij20ec3e32016-02-11 11:03:06 +01001358 INIT_LIST_HEAD(&gdev->pin_ranges);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301359#endif
1360
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001361 status = gpiochip_set_desc_names(chip);
1362 if (status)
1363 goto err_remove_from_list;
1364
Mika Westerberg79b804c2016-09-20 15:15:21 +03001365 status = gpiochip_irqchip_init_valid_mask(chip);
1366 if (status)
1367 goto err_remove_from_list;
1368
Ricardo Ribalda Delgadof8ec92a2018-10-05 08:52:58 +02001369 status = gpiochip_alloc_valid_mask(chip);
Stephen Boyd726cb3b2018-03-23 09:34:52 -07001370 if (status)
1371 goto err_remove_irqchip_mask;
1372
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001373 status = gpiochip_add_irqchip(chip, lock_key, request_key);
Thierry Redinge0d89722017-11-07 19:15:54 +01001374 if (status)
Geert Uytterhoeven35779892019-04-24 15:59:33 +02001375 goto err_free_gpiochip_mask;
Thierry Redinge0d89722017-11-07 19:15:54 +01001376
Tomeu Vizoso28355f82015-07-14 10:29:54 +02001377 status = of_gpiochip_add(chip);
1378 if (status)
1379 goto err_remove_chip;
1380
Ricardo Ribalda Delgadof8ec92a2018-10-05 08:52:58 +02001381 status = gpiochip_init_valid_mask(chip);
1382 if (status)
Geert Uytterhoeven35779892019-04-24 15:59:33 +02001383 goto err_remove_of_chip;
Ricardo Ribalda Delgadof8ec92a2018-10-05 08:52:58 +02001384
Ricardo Ribalda Delgado3edfb7b2018-10-05 08:53:00 +02001385 for (i = 0; i < chip->ngpio; i++) {
1386 struct gpio_desc *desc = &gdev->descs[i];
1387
Ricardo Ribalda Delgado3edfb7b2018-10-05 08:53:00 +02001388 if (chip->get_direction && gpiochip_line_is_valid(chip, i))
1389 desc->flags = !chip->get_direction(chip, i) ?
1390 (1 << FLAG_IS_OUT) : 0;
1391 else
1392 desc->flags = !chip->direction_input ?
1393 (1 << FLAG_IS_OUT) : 0;
1394 }
1395
Mika Westerberg664e3e52014-01-08 12:40:54 +02001396 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001397
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02001398 machine_gpiochip_add(chip);
1399
Linus Walleij3c702e92015-10-21 15:29:53 +02001400 /*
1401 * By first adding the chardev, and then adding the device,
1402 * we get a device node entry in sysfs under
1403 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1404 * coldplug of device nodes and other udev business.
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001405 * We can do this only if gpiolib has been initialized.
1406 * Otherwise, defer until later.
Linus Walleij3c702e92015-10-21 15:29:53 +02001407 */
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001408 if (gpiolib_initialized) {
1409 status = gpiochip_setup_dev(gdev);
1410 if (status)
Geert Uytterhoeven35779892019-04-24 15:59:33 +02001411 goto err_remove_acpi_chip;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001412 }
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001413 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001414
Geert Uytterhoeven35779892019-04-24 15:59:33 +02001415err_remove_acpi_chip:
Johan Hovold225fce82015-01-12 17:12:25 +01001416 acpi_gpiochip_remove(chip);
Geert Uytterhoeven35779892019-04-24 15:59:33 +02001417err_remove_of_chip:
Johan Hovold6d867502015-05-04 17:23:25 +02001418 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +01001419 of_gpiochip_remove(chip);
Geert Uytterhoeven35779892019-04-24 15:59:33 +02001420err_remove_chip:
1421 gpiochip_irqchip_remove(chip);
1422err_free_gpiochip_mask:
Stephen Boyd726cb3b2018-03-23 09:34:52 -07001423 gpiochip_free_valid_mask(chip);
1424err_remove_irqchip_mask:
Mika Westerberg79b804c2016-09-20 15:15:21 +03001425 gpiochip_irqchip_free_valid_mask(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001426err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +01001427 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001428 list_del(&gdev->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001429 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001430err_free_label:
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +01001431 kfree_const(gdev->label);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001432err_free_descs:
1433 kfree(gdev->descs);
Vladimir Zapolskiya05a1402018-11-02 15:39:43 +02001434err_free_ida:
Linus Walleijff2b1352015-10-20 11:10:38 +02001435 ida_simple_remove(&gpio_ida, gdev->id);
Vladimir Zapolskiya05a1402018-11-02 15:39:43 +02001436err_free_gdev:
David Brownelld2876d02008-02-04 22:28:20 -08001437 /* failures here can mean systems won't boot... */
Marcel Ziswiler1777fc92018-07-20 09:54:49 +02001438 pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__,
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001439 gdev->base, gdev->base + gdev->ngpio - 1,
Marcel Ziswiler1777fc92018-07-20 09:54:49 +02001440 chip->label ? : "generic", status);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001441 kfree(gdev);
David Brownelld2876d02008-02-04 22:28:20 -08001442 return status;
1443}
Thierry Reding959bc7b2017-11-07 19:15:59 +01001444EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);
David Brownelld2876d02008-02-04 22:28:20 -08001445
1446/**
Linus Walleij43c54ec2016-02-11 11:37:48 +01001447 * gpiochip_get_data() - get per-subdriver data for the chip
Thierry Reding950d55f52017-07-24 16:57:22 +02001448 * @chip: GPIO chip
1449 *
1450 * Returns:
1451 * The per-subdriver data for the chip.
Linus Walleij43c54ec2016-02-11 11:37:48 +01001452 */
1453void *gpiochip_get_data(struct gpio_chip *chip)
1454{
1455 return chip->gpiodev->data;
1456}
1457EXPORT_SYMBOL_GPL(gpiochip_get_data);
1458
1459/**
David Brownelld2876d02008-02-04 22:28:20 -08001460 * gpiochip_remove() - unregister a gpio_chip
1461 * @chip: the chip to unregister
1462 *
1463 * A gpio_chip with any GPIOs still requested may not be removed.
1464 */
abdoulaye berthee1db1702014-07-05 18:28:50 +02001465void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -08001466{
Linus Walleijff2b1352015-10-20 11:10:38 +02001467 struct gpio_device *gdev = chip->gpiodev;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001468 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001469 unsigned long flags;
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001470 unsigned i;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001471 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -08001472
Linus Walleijff2b1352015-10-20 11:10:38 +02001473 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
Linus Walleijafbc4f32016-02-09 13:21:06 +01001474 gpiochip_sysfs_unregister(gdev);
Geert Uytterhoeven5018ada2016-12-19 18:29:23 +01001475 gpiochip_free_hogs(chip);
Bamvor Jian Zhangbd203bd2016-02-20 13:13:19 +08001476 /* Numb the device, cancelling all outstanding operations */
1477 gdev->chip = NULL;
Johan Hovold00acc3d2015-01-12 17:12:27 +01001478 gpiochip_irqchip_remove(chip);
Mika Westerberg6072b9d2014-03-10 14:54:53 +02001479 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +01001480 gpiochip_remove_pin_ranges(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001481 of_gpiochip_remove(chip);
Stephen Boyd726cb3b2018-03-23 09:34:52 -07001482 gpiochip_free_valid_mask(chip);
Linus Walleij43c54ec2016-02-11 11:37:48 +01001483 /*
1484 * We accept no more calls into the driver from this point, so
1485 * NULL the driver data pointer
1486 */
1487 gdev->data = NULL;
Anton Vorontsov391c9702010-06-08 07:48:17 -06001488
Johan Hovold6798aca2015-01-12 17:12:28 +01001489 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001490 for (i = 0; i < gdev->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001491 desc = &gdev->descs[i];
Johan Hovoldfab28b82015-05-04 17:10:27 +02001492 if (test_bit(FLAG_REQUESTED, &desc->flags))
1493 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -08001494 }
David Brownelld2876d02008-02-04 22:28:20 -08001495 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001496
Johan Hovoldfab28b82015-05-04 17:10:27 +02001497 if (requested)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001498 dev_crit(&gdev->dev,
Linus Walleij58383c782015-11-04 09:56:26 +01001499 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +02001500
Linus Walleijff2b1352015-10-20 11:10:38 +02001501 /*
1502 * The gpiochip side puts its use of the device to rest here:
1503 * if there are no userspace clients, the chardev and device will
1504 * be removed, else it will be dangling until the last user is
1505 * gone.
1506 */
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001507 cdev_device_del(&gdev->chrdev, &gdev->dev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001508 put_device(&gdev->dev);
David Brownelld2876d02008-02-04 22:28:20 -08001509}
1510EXPORT_SYMBOL_GPL(gpiochip_remove);
1511
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301512static void devm_gpio_chip_release(struct device *dev, void *res)
1513{
1514 struct gpio_chip *chip = *(struct gpio_chip **)res;
1515
1516 gpiochip_remove(chip);
1517}
1518
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301519/**
Jonathan Neuschäfer689fd022017-12-21 17:56:34 +01001520 * devm_gpiochip_add_data() - Resource manager gpiochip_add_data()
Uwe Kleine-König3925b90f2018-10-08 11:34:03 +02001521 * @dev: pointer to the device that gpio_chip belongs to.
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301522 * @chip: the chip to register, with chip->base initialized
Thierry Reding950d55f52017-07-24 16:57:22 +02001523 * @data: driver-private data associated with this chip
1524 *
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301525 * Context: potentially before irqs will work
1526 *
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301527 * The gpio chip automatically be released when the device is unbound.
Thierry Reding950d55f52017-07-24 16:57:22 +02001528 *
1529 * Returns:
1530 * A negative errno if the chip can't be registered, such as because the
1531 * chip->base is invalid or already associated with a different chip.
1532 * Otherwise it returns zero as a success code.
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301533 */
1534int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
1535 void *data)
1536{
1537 struct gpio_chip **ptr;
1538 int ret;
1539
1540 ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
1541 GFP_KERNEL);
1542 if (!ptr)
1543 return -ENOMEM;
1544
1545 ret = gpiochip_add_data(chip, data);
1546 if (ret < 0) {
1547 devres_free(ptr);
1548 return ret;
1549 }
1550
1551 *ptr = chip;
1552 devres_add(dev, ptr);
1553
1554 return 0;
1555}
1556EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
1557
1558/**
Grant Likely594fa262010-06-08 07:48:16 -06001559 * gpiochip_find() - iterator for locating a specific gpio_chip
1560 * @data: data to pass to match function
Thierry Reding950d55f52017-07-24 16:57:22 +02001561 * @match: Callback function to check gpio_chip
Grant Likely594fa262010-06-08 07:48:16 -06001562 *
1563 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1564 * determined by a user supplied @match callback. The callback should return
1565 * 0 if the device doesn't match and non-zero if it does. If the callback is
1566 * non-zero, this function will return to the caller and not iterate over any
1567 * more gpio_chips.
1568 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001569struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001570 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001571 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001572{
Linus Walleijff2b1352015-10-20 11:10:38 +02001573 struct gpio_device *gdev;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001574 struct gpio_chip *chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -06001575 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001576
1577 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001578 list_for_each_entry(gdev, &gpio_devices, list)
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001579 if (gdev->chip && match(gdev->chip, data)) {
1580 chip = gdev->chip;
Grant Likely594fa262010-06-08 07:48:16 -06001581 break;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001582 }
Linus Walleijff2b1352015-10-20 11:10:38 +02001583
Grant Likely594fa262010-06-08 07:48:16 -06001584 spin_unlock_irqrestore(&gpio_lock, flags);
1585
1586 return chip;
1587}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001588EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001589
Alexandre Courbot79697ef2013-11-16 21:39:32 +09001590static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1591{
1592 const char *name = data;
1593
1594 return !strcmp(chip->label, name);
1595}
1596
1597static struct gpio_chip *find_chip_by_name(const char *name)
1598{
1599 return gpiochip_find((void *)name, gpiochip_match_name);
1600}
1601
Linus Walleij14250522014-03-25 10:40:18 +01001602#ifdef CONFIG_GPIOLIB_IRQCHIP
1603
1604/*
1605 * The following is irqchip helper code for gpiochips.
1606 */
1607
Mika Westerberg79b804c2016-09-20 15:15:21 +03001608static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1609{
Thierry Redingdc7b0382017-11-07 19:15:52 +01001610 if (!gpiochip->irq.need_valid_mask)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001611 return 0;
1612
Stephen Boyde4371f6e2018-03-23 09:34:50 -07001613 gpiochip->irq.valid_mask = gpiochip_allocate_mask(gpiochip);
Thierry Redingdc7b0382017-11-07 19:15:52 +01001614 if (!gpiochip->irq.valid_mask)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001615 return -ENOMEM;
1616
Mika Westerberg79b804c2016-09-20 15:15:21 +03001617 return 0;
1618}
1619
1620static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1621{
Thierry Redingdc7b0382017-11-07 19:15:52 +01001622 kfree(gpiochip->irq.valid_mask);
1623 gpiochip->irq.valid_mask = NULL;
Mika Westerberg79b804c2016-09-20 15:15:21 +03001624}
1625
Stephen Boyd64ff2c82018-01-09 17:58:46 -08001626bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
1627 unsigned int offset)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001628{
Stephen Boyd726cb3b2018-03-23 09:34:52 -07001629 if (!gpiochip_line_is_valid(gpiochip, offset))
1630 return false;
Mika Westerberg79b804c2016-09-20 15:15:21 +03001631 /* No mask means all valid */
Thierry Redingdc7b0382017-11-07 19:15:52 +01001632 if (likely(!gpiochip->irq.valid_mask))
Mika Westerberg79b804c2016-09-20 15:15:21 +03001633 return true;
Thierry Redingdc7b0382017-11-07 19:15:52 +01001634 return test_bit(offset, gpiochip->irq.valid_mask);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001635}
Stephen Boyd64ff2c82018-01-09 17:58:46 -08001636EXPORT_SYMBOL_GPL(gpiochip_irqchip_irq_valid);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001637
Linus Walleij14250522014-03-25 10:40:18 +01001638/**
Linus Walleijd245b3f2016-11-24 10:57:25 +01001639 * gpiochip_set_cascaded_irqchip() - connects a cascaded irqchip to a gpiochip
Linus Walleij4892d3a2019-06-14 10:12:26 +02001640 * @gc: the gpiochip to set the irqchip chain to
Linus Walleij14250522014-03-25 10:40:18 +01001641 * @parent_irq: the irq number corresponding to the parent IRQ for this
1642 * chained irqchip
1643 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001644 * coming out of the gpiochip. If the interrupt is nested rather than
1645 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +01001646 */
Linus Walleij4892d3a2019-06-14 10:12:26 +02001647static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gc,
Thierry Reding6f793092017-04-03 18:05:21 +02001648 unsigned int parent_irq,
Linus Walleijd245b3f2016-11-24 10:57:25 +01001649 irq_flow_handler_t parent_handler)
Linus Walleij14250522014-03-25 10:40:18 +01001650{
Linus Walleij4892d3a2019-06-14 10:12:26 +02001651 struct gpio_irq_chip *girq = &gc->irq;
1652 struct device *dev = &gc->gpiodev->dev;
1653
1654 if (!girq->domain) {
1655 chip_err(gc, "called %s before setting up irqchip\n",
Linus Walleij83141a72014-09-26 13:50:12 +02001656 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +02001657 return;
1658 }
1659
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001660 if (parent_handler) {
Linus Walleij4892d3a2019-06-14 10:12:26 +02001661 if (gc->can_sleep) {
1662 chip_err(gc,
Andy Shevchenkob1911712018-07-03 03:39:03 +03001663 "you cannot have chained interrupts on a chip that may sleep\n");
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001664 return;
1665 }
Linus Walleij4892d3a2019-06-14 10:12:26 +02001666 girq->parents = devm_kcalloc(dev, 1,
1667 sizeof(*girq->parents),
1668 GFP_KERNEL);
1669 if (!girq->parents) {
1670 chip_err(gc, "out of memory allocating parent IRQ\n");
1671 return;
1672 }
1673 girq->parents[0] = parent_irq;
1674 girq->num_parents = 1;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001675 /*
1676 * The parent irqchip is already using the chip_data for this
1677 * irqchip, so our callbacks simply use the handler_data.
1678 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +02001679 irq_set_chained_handler_and_data(parent_irq, parent_handler,
Linus Walleij4892d3a2019-06-14 10:12:26 +02001680 gc);
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001681 }
Linus Walleij14250522014-03-25 10:40:18 +01001682}
Linus Walleijd245b3f2016-11-24 10:57:25 +01001683
1684/**
1685 * gpiochip_set_chained_irqchip() - connects a chained irqchip to a gpiochip
1686 * @gpiochip: the gpiochip to set the irqchip chain to
1687 * @irqchip: the irqchip to chain to the gpiochip
1688 * @parent_irq: the irq number corresponding to the parent IRQ for this
1689 * chained irqchip
1690 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Stephen Boyd40f5ff42018-10-08 09:32:16 -07001691 * coming out of the gpiochip.
Linus Walleijd245b3f2016-11-24 10:57:25 +01001692 */
1693void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
1694 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001695 unsigned int parent_irq,
Linus Walleijd245b3f2016-11-24 10:57:25 +01001696 irq_flow_handler_t parent_handler)
1697{
Thierry Reding60ed54c2017-11-07 19:15:57 +01001698 if (gpiochip->irq.threaded) {
1699 chip_err(gpiochip, "tried to chain a threaded gpiochip\n");
1700 return;
1701 }
1702
Stephen Boyd3c1f6b22018-10-08 09:32:15 -07001703 gpiochip_set_cascaded_irqchip(gpiochip, parent_irq, parent_handler);
Linus Walleijd245b3f2016-11-24 10:57:25 +01001704}
Linus Walleij14250522014-03-25 10:40:18 +01001705EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
1706
1707/**
Linus Walleijd245b3f2016-11-24 10:57:25 +01001708 * gpiochip_set_nested_irqchip() - connects a nested irqchip to a gpiochip
1709 * @gpiochip: the gpiochip to set the irqchip nested handler to
1710 * @irqchip: the irqchip to nest to the gpiochip
1711 * @parent_irq: the irq number corresponding to the parent IRQ for this
1712 * nested irqchip
1713 */
1714void gpiochip_set_nested_irqchip(struct gpio_chip *gpiochip,
1715 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001716 unsigned int parent_irq)
Linus Walleijd245b3f2016-11-24 10:57:25 +01001717{
Stephen Boyd3c1f6b22018-10-08 09:32:15 -07001718 gpiochip_set_cascaded_irqchip(gpiochip, parent_irq, NULL);
Linus Walleijd245b3f2016-11-24 10:57:25 +01001719}
1720EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip);
1721
1722/**
Linus Walleij14250522014-03-25 10:40:18 +01001723 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1724 * @d: the irqdomain used by this irqchip
1725 * @irq: the global irq number used by this GPIO irqchip irq
1726 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1727 *
1728 * This function will set up the mapping for a certain IRQ line on a
1729 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1730 * stored inside the gpiochip.
1731 */
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001732int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1733 irq_hw_number_t hwirq)
Linus Walleij14250522014-03-25 10:40:18 +01001734{
1735 struct gpio_chip *chip = d->host_data;
Thierry Redinge0d89722017-11-07 19:15:54 +01001736 int err = 0;
Linus Walleij14250522014-03-25 10:40:18 +01001737
Grygorii Strashkodc749a02017-07-21 11:49:00 -05001738 if (!gpiochip_irqchip_irq_valid(chip, hwirq))
1739 return -ENXIO;
1740
Linus Walleij14250522014-03-25 10:40:18 +01001741 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001742 /*
1743 * This lock class tells lockdep that GPIO irqs are in a different
1744 * category than their parents, so it won't report false recursion.
1745 */
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001746 irq_set_lockdep_class(irq, chip->irq.lock_key, chip->irq.request_key);
Thierry Redingc7a0aa52017-11-07 19:15:48 +01001747 irq_set_chip_and_handler(irq, chip->irq.chip, chip->irq.handler);
Linus Walleijd245b3f2016-11-24 10:57:25 +01001748 /* Chips that use nested thread handlers have them marked */
Thierry Reding60ed54c2017-11-07 19:15:57 +01001749 if (chip->irq.threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02001750 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +01001751 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -05001752
Thierry Redinge0d89722017-11-07 19:15:54 +01001753 if (chip->irq.num_parents == 1)
1754 err = irq_set_parent(irq, chip->irq.parents[0]);
1755 else if (chip->irq.map)
1756 err = irq_set_parent(irq, chip->irq.map[hwirq]);
1757
1758 if (err < 0)
1759 return err;
1760
Linus Walleij1333b902014-04-23 16:45:12 +02001761 /*
1762 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1763 * is passed as default type.
1764 */
Thierry Reding3634eeb2017-11-07 19:15:49 +01001765 if (chip->irq.default_type != IRQ_TYPE_NONE)
1766 irq_set_irq_type(irq, chip->irq.default_type);
Linus Walleij14250522014-03-25 10:40:18 +01001767
1768 return 0;
1769}
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001770EXPORT_SYMBOL_GPL(gpiochip_irq_map);
Linus Walleij14250522014-03-25 10:40:18 +01001771
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001772void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
Linus Walleijc3626fd2014-03-28 20:42:01 +01001773{
Linus Walleij1c8732b2014-04-09 13:34:39 +02001774 struct gpio_chip *chip = d->host_data;
1775
Thierry Reding60ed54c2017-11-07 19:15:57 +01001776 if (chip->irq.threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02001777 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001778 irq_set_chip_and_handler(irq, NULL, NULL);
1779 irq_set_chip_data(irq, NULL);
1780}
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001781EXPORT_SYMBOL_GPL(gpiochip_irq_unmap);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001782
Linus Walleij14250522014-03-25 10:40:18 +01001783static const struct irq_domain_ops gpiochip_domain_ops = {
1784 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +01001785 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +01001786 /* Virtually all GPIO irqchips are twocell:ed */
1787 .xlate = irq_domain_xlate_twocell,
1788};
1789
Brian Masneyef74f702019-01-19 15:42:42 -05001790/**
1791 * gpiochip_irq_domain_activate() - Lock a GPIO to be used as an IRQ
1792 * @domain: The IRQ domain used by this IRQ chip
1793 * @data: Outermost irq_data associated with the IRQ
1794 * @reserve: If set, only reserve an interrupt vector instead of assigning one
1795 *
1796 * This function is a wrapper that calls gpiochip_lock_as_irq() and is to be
1797 * used as the activate function for the &struct irq_domain_ops. The host_data
1798 * for the IRQ domain must be the &struct gpio_chip.
1799 */
1800int gpiochip_irq_domain_activate(struct irq_domain *domain,
1801 struct irq_data *data, bool reserve)
1802{
1803 struct gpio_chip *chip = domain->host_data;
1804
1805 return gpiochip_lock_as_irq(chip, data->hwirq);
1806}
1807EXPORT_SYMBOL_GPL(gpiochip_irq_domain_activate);
1808
1809/**
1810 * gpiochip_irq_domain_deactivate() - Unlock a GPIO used as an IRQ
1811 * @domain: The IRQ domain used by this IRQ chip
1812 * @data: Outermost irq_data associated with the IRQ
1813 *
1814 * This function is a wrapper that will call gpiochip_unlock_as_irq() and is to
1815 * be used as the deactivate function for the &struct irq_domain_ops. The
1816 * host_data for the IRQ domain must be the &struct gpio_chip.
1817 */
1818void gpiochip_irq_domain_deactivate(struct irq_domain *domain,
1819 struct irq_data *data)
1820{
1821 struct gpio_chip *chip = domain->host_data;
1822
1823 return gpiochip_unlock_as_irq(chip, data->hwirq);
1824}
1825EXPORT_SYMBOL_GPL(gpiochip_irq_domain_deactivate);
1826
Linus Walleij14250522014-03-25 10:40:18 +01001827static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
1828{
Grygorii Strashkodc749a02017-07-21 11:49:00 -05001829 if (!gpiochip_irqchip_irq_valid(chip, offset))
1830 return -ENXIO;
Thierry Redinge0d89722017-11-07 19:15:54 +01001831
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001832 return irq_create_mapping(chip->irq.domain, offset);
Linus Walleij14250522014-03-25 10:40:18 +01001833}
1834
Hans Verkuil4e6b8232018-09-08 11:23:14 +02001835static int gpiochip_irq_reqres(struct irq_data *d)
1836{
1837 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1838
1839 return gpiochip_reqres_irq(chip, d->hwirq);
1840}
1841
1842static void gpiochip_irq_relres(struct irq_data *d)
1843{
1844 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1845
1846 gpiochip_relres_irq(chip, d->hwirq);
1847}
1848
Hans Verkuil461c1a72018-09-08 11:23:17 +02001849static void gpiochip_irq_enable(struct irq_data *d)
1850{
1851 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1852
1853 gpiochip_enable_irq(chip, d->hwirq);
1854 if (chip->irq.irq_enable)
1855 chip->irq.irq_enable(d);
1856 else
1857 chip->irq.chip->irq_unmask(d);
1858}
1859
1860static void gpiochip_irq_disable(struct irq_data *d)
1861{
1862 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1863
1864 if (chip->irq.irq_disable)
1865 chip->irq.irq_disable(d);
1866 else
1867 chip->irq.chip->irq_mask(d);
1868 gpiochip_disable_irq(chip, d->hwirq);
1869}
1870
Hans Verkuilca620f22018-09-08 11:23:15 +02001871static void gpiochip_set_irq_hooks(struct gpio_chip *gpiochip)
1872{
1873 struct irq_chip *irqchip = gpiochip->irq.chip;
1874
1875 if (!irqchip->irq_request_resources &&
1876 !irqchip->irq_release_resources) {
1877 irqchip->irq_request_resources = gpiochip_irq_reqres;
1878 irqchip->irq_release_resources = gpiochip_irq_relres;
1879 }
Hans Verkuil461c1a72018-09-08 11:23:17 +02001880 if (WARN_ON(gpiochip->irq.irq_enable))
1881 return;
Hans Verkuil171948e2018-09-14 10:36:39 +02001882 /* Check if the irqchip already has this hook... */
1883 if (irqchip->irq_enable == gpiochip_irq_enable) {
1884 /*
1885 * ...and if so, give a gentle warning that this is bad
1886 * practice.
1887 */
1888 chip_info(gpiochip,
1889 "detected irqchip that is shared with multiple gpiochips: please fix the driver.\n");
1890 return;
1891 }
Hans Verkuil461c1a72018-09-08 11:23:17 +02001892 gpiochip->irq.irq_enable = irqchip->irq_enable;
1893 gpiochip->irq.irq_disable = irqchip->irq_disable;
1894 irqchip->irq_enable = gpiochip_irq_enable;
1895 irqchip->irq_disable = gpiochip_irq_disable;
Hans Verkuilca620f22018-09-08 11:23:15 +02001896}
1897
Linus Walleij14250522014-03-25 10:40:18 +01001898/**
Thierry Redinge0d89722017-11-07 19:15:54 +01001899 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
1900 * @gpiochip: the GPIO chip to add the IRQ chip to
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001901 * @lock_key: lockdep class for IRQ lock
1902 * @request_key: lockdep class for IRQ request
Thierry Redinge0d89722017-11-07 19:15:54 +01001903 */
Thierry Reding959bc7b2017-11-07 19:15:59 +01001904static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001905 struct lock_class_key *lock_key,
1906 struct lock_class_key *request_key)
Thierry Redinge0d89722017-11-07 19:15:54 +01001907{
1908 struct irq_chip *irqchip = gpiochip->irq.chip;
1909 const struct irq_domain_ops *ops;
1910 struct device_node *np;
1911 unsigned int type;
1912 unsigned int i;
1913
1914 if (!irqchip)
1915 return 0;
1916
1917 if (gpiochip->irq.parent_handler && gpiochip->can_sleep) {
Andy Shevchenkob1911712018-07-03 03:39:03 +03001918 chip_err(gpiochip, "you cannot have chained interrupts on a chip that may sleep\n");
Thierry Redinge0d89722017-11-07 19:15:54 +01001919 return -EINVAL;
1920 }
1921
1922 np = gpiochip->gpiodev->dev.of_node;
1923 type = gpiochip->irq.default_type;
1924
1925 /*
1926 * Specifying a default trigger is a terrible idea if DT or ACPI is
1927 * used to configure the interrupts, as you may end up with
1928 * conflicting triggers. Tell the user, and reset to NONE.
1929 */
1930 if (WARN(np && type != IRQ_TYPE_NONE,
1931 "%s: Ignoring %u default trigger\n", np->full_name, type))
1932 type = IRQ_TYPE_NONE;
1933
1934 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
1935 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1936 "Ignoring %u default trigger\n", type);
1937 type = IRQ_TYPE_NONE;
1938 }
1939
1940 gpiochip->to_irq = gpiochip_to_irq;
1941 gpiochip->irq.default_type = type;
Thierry Reding959bc7b2017-11-07 19:15:59 +01001942 gpiochip->irq.lock_key = lock_key;
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001943 gpiochip->irq.request_key = request_key;
Thierry Redinge0d89722017-11-07 19:15:54 +01001944
1945 if (gpiochip->irq.domain_ops)
1946 ops = gpiochip->irq.domain_ops;
1947 else
1948 ops = &gpiochip_domain_ops;
1949
1950 gpiochip->irq.domain = irq_domain_add_simple(np, gpiochip->ngpio,
Thierry Reding8302cf52017-11-07 19:15:58 +01001951 gpiochip->irq.first,
1952 ops, gpiochip);
Thierry Redinge0d89722017-11-07 19:15:54 +01001953 if (!gpiochip->irq.domain)
1954 return -EINVAL;
1955
Thierry Redinge0d89722017-11-07 19:15:54 +01001956 if (gpiochip->irq.parent_handler) {
1957 void *data = gpiochip->irq.parent_handler_data ?: gpiochip;
1958
1959 for (i = 0; i < gpiochip->irq.num_parents; i++) {
1960 /*
1961 * The parent IRQ chip is already using the chip_data
1962 * for this IRQ chip, so our callbacks simply use the
1963 * handler_data.
1964 */
1965 irq_set_chained_handler_and_data(gpiochip->irq.parents[i],
1966 gpiochip->irq.parent_handler,
1967 data);
1968 }
Thierry Redinge0d89722017-11-07 19:15:54 +01001969 }
1970
Hans Verkuilca620f22018-09-08 11:23:15 +02001971 gpiochip_set_irq_hooks(gpiochip);
1972
Thierry Redinge0d89722017-11-07 19:15:54 +01001973 acpi_gpiochip_request_interrupts(gpiochip);
1974
1975 return 0;
1976}
1977
1978/**
Linus Walleij14250522014-03-25 10:40:18 +01001979 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1980 * @gpiochip: the gpiochip to remove the irqchip from
1981 *
1982 * This is called only from gpiochip_remove()
1983 */
1984static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
1985{
Hans Verkuilca620f22018-09-08 11:23:15 +02001986 struct irq_chip *irqchip = gpiochip->irq.chip;
Thierry Reding39e5f092017-11-07 19:15:50 +01001987 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +01001988
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001989 acpi_gpiochip_free_interrupts(gpiochip);
1990
Hans Verkuilca620f22018-09-08 11:23:15 +02001991 if (irqchip && gpiochip->irq.parent_handler) {
Thierry Reding39e5f092017-11-07 19:15:50 +01001992 struct gpio_irq_chip *irq = &gpiochip->irq;
1993 unsigned int i;
1994
1995 for (i = 0; i < irq->num_parents; i++)
1996 irq_set_chained_handler_and_data(irq->parents[i],
1997 NULL, NULL);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001998 }
1999
Linus Walleijc3626fd2014-03-28 20:42:01 +01002000 /* Remove all IRQ mappings and delete the domain */
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01002001 if (gpiochip->irq.domain) {
Thierry Reding39e5f092017-11-07 19:15:50 +01002002 unsigned int irq;
2003
Mika Westerberg79b804c2016-09-20 15:15:21 +03002004 for (offset = 0; offset < gpiochip->ngpio; offset++) {
2005 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
2006 continue;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01002007
2008 irq = irq_find_mapping(gpiochip->irq.domain, offset);
2009 irq_dispose_mapping(irq);
Mika Westerberg79b804c2016-09-20 15:15:21 +03002010 }
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01002011
2012 irq_domain_remove(gpiochip->irq.domain);
Linus Walleijc3626fd2014-03-28 20:42:01 +01002013 }
Linus Walleij14250522014-03-25 10:40:18 +01002014
Hans Verkuil461c1a72018-09-08 11:23:17 +02002015 if (irqchip) {
2016 if (irqchip->irq_request_resources == gpiochip_irq_reqres) {
2017 irqchip->irq_request_resources = NULL;
2018 irqchip->irq_release_resources = NULL;
2019 }
2020 if (irqchip->irq_enable == gpiochip_irq_enable) {
2021 irqchip->irq_enable = gpiochip->irq.irq_enable;
2022 irqchip->irq_disable = gpiochip->irq.irq_disable;
2023 }
Linus Walleij14250522014-03-25 10:40:18 +01002024 }
Hans Verkuil461c1a72018-09-08 11:23:17 +02002025 gpiochip->irq.irq_enable = NULL;
2026 gpiochip->irq.irq_disable = NULL;
Hans Verkuilca620f22018-09-08 11:23:15 +02002027 gpiochip->irq.chip = NULL;
Mika Westerberg79b804c2016-09-20 15:15:21 +03002028
2029 gpiochip_irqchip_free_valid_mask(gpiochip);
Linus Walleij14250522014-03-25 10:40:18 +01002030}
2031
2032/**
Linus Walleij739e6f52017-01-11 13:37:07 +01002033 * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01002034 * @gpiochip: the gpiochip to add the irqchip to
2035 * @irqchip: the irqchip to add to the gpiochip
2036 * @first_irq: if not dynamically assigned, the base (first) IRQ to
2037 * allocate gpiochip irqs from
2038 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +02002039 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
2040 * to have the core avoid setting up any default type in the hardware.
Thierry Reding60ed54c2017-11-07 19:15:57 +01002041 * @threaded: whether this irqchip uses a nested thread handler
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002042 * @lock_key: lockdep class for IRQ lock
2043 * @request_key: lockdep class for IRQ request
Linus Walleij14250522014-03-25 10:40:18 +01002044 *
2045 * This function closely associates a certain irqchip with a certain
2046 * gpiochip, providing an irq domain to translate the local IRQs to
2047 * global irqs in the gpiolib core, and making sure that the gpiochip
2048 * is passed as chip data to all related functions. Driver callbacks
Linus Walleij09dd5f92015-12-07 15:31:58 +01002049 * need to use gpiochip_get_data() to get their local state containers back
Linus Walleij14250522014-03-25 10:40:18 +01002050 * from the gpiochip passed as chip data. An irqdomain will be stored
2051 * in the gpiochip that shall be used by the driver to handle IRQ number
2052 * translation. The gpiochip will need to be initialized and registered
2053 * before calling this function.
2054 *
Linus Walleijc3626fd2014-03-28 20:42:01 +01002055 * This function will handle two cell:ed simple IRQs and assumes all
2056 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +01002057 * need to be open coded.
2058 */
Linus Walleij739e6f52017-01-11 13:37:07 +01002059int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
2060 struct irq_chip *irqchip,
2061 unsigned int first_irq,
2062 irq_flow_handler_t handler,
2063 unsigned int type,
Thierry Reding60ed54c2017-11-07 19:15:57 +01002064 bool threaded,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002065 struct lock_class_key *lock_key,
2066 struct lock_class_key *request_key)
Linus Walleij14250522014-03-25 10:40:18 +01002067{
2068 struct device_node *of_node;
Linus Walleij14250522014-03-25 10:40:18 +01002069
2070 if (!gpiochip || !irqchip)
2071 return -EINVAL;
2072
Linus Walleij58383c782015-11-04 09:56:26 +01002073 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +01002074 pr_err("missing gpiochip .dev parent pointer\n");
2075 return -EINVAL;
2076 }
Thierry Reding60ed54c2017-11-07 19:15:57 +01002077 gpiochip->irq.threaded = threaded;
Linus Walleij58383c782015-11-04 09:56:26 +01002078 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +01002079#ifdef CONFIG_OF_GPIO
2080 /*
Colin Cronin20a8a962015-05-18 11:41:43 -07002081 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08002082 * FIXME: get rid of this and use gpiochip->parent->of_node
2083 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +01002084 */
2085 if (gpiochip->of_node)
2086 of_node = gpiochip->of_node;
2087#endif
Marc Zyngier332e99d2016-09-07 09:12:11 +01002088 /*
Mika Westerberg0a1e0052016-09-12 14:29:51 +03002089 * Specifying a default trigger is a terrible idea if DT or ACPI is
Marc Zyngier332e99d2016-09-07 09:12:11 +01002090 * used to configure the interrupts, as you may end-up with
2091 * conflicting triggers. Tell the user, and reset to NONE.
2092 */
2093 if (WARN(of_node && type != IRQ_TYPE_NONE,
Rob Herring7eb6ce22017-07-18 16:43:03 -05002094 "%pOF: Ignoring %d default trigger\n", of_node, type))
Marc Zyngier332e99d2016-09-07 09:12:11 +01002095 type = IRQ_TYPE_NONE;
Mika Westerberg0a1e0052016-09-12 14:29:51 +03002096 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
2097 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
2098 "Ignoring %d default trigger\n", type);
2099 type = IRQ_TYPE_NONE;
2100 }
Marc Zyngier332e99d2016-09-07 09:12:11 +01002101
Thierry Redingda80ff82017-11-07 19:15:46 +01002102 gpiochip->irq.chip = irqchip;
Thierry Redingc7a0aa52017-11-07 19:15:48 +01002103 gpiochip->irq.handler = handler;
Thierry Reding3634eeb2017-11-07 19:15:49 +01002104 gpiochip->irq.default_type = type;
Linus Walleij14250522014-03-25 10:40:18 +01002105 gpiochip->to_irq = gpiochip_to_irq;
Thierry Redingca9df052017-11-07 19:15:53 +01002106 gpiochip->irq.lock_key = lock_key;
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002107 gpiochip->irq.request_key = request_key;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01002108 gpiochip->irq.domain = irq_domain_add_simple(of_node,
Linus Walleij14250522014-03-25 10:40:18 +01002109 gpiochip->ngpio, first_irq,
2110 &gpiochip_domain_ops, gpiochip);
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01002111 if (!gpiochip->irq.domain) {
Thierry Redingda80ff82017-11-07 19:15:46 +01002112 gpiochip->irq.chip = NULL;
Linus Walleij14250522014-03-25 10:40:18 +01002113 return -EINVAL;
2114 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +02002115
Hans Verkuilca620f22018-09-08 11:23:15 +02002116 gpiochip_set_irq_hooks(gpiochip);
Linus Walleij14250522014-03-25 10:40:18 +01002117
Mika Westerbergafa82fa2014-07-25 09:54:48 +03002118 acpi_gpiochip_request_interrupts(gpiochip);
2119
Linus Walleij14250522014-03-25 10:40:18 +01002120 return 0;
2121}
Linus Walleij739e6f52017-01-11 13:37:07 +01002122EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key);
Linus Walleij14250522014-03-25 10:40:18 +01002123
2124#else /* CONFIG_GPIOLIB_IRQCHIP */
2125
Thierry Reding959bc7b2017-11-07 19:15:59 +01002126static inline int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002127 struct lock_class_key *lock_key,
2128 struct lock_class_key *request_key)
Thierry Redinge0d89722017-11-07 19:15:54 +01002129{
2130 return 0;
2131}
2132
Linus Walleij14250522014-03-25 10:40:18 +01002133static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
Mika Westerberg79b804c2016-09-20 15:15:21 +03002134static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
2135{
2136 return 0;
2137}
2138static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
2139{ }
Linus Walleij14250522014-03-25 10:40:18 +01002140
2141#endif /* CONFIG_GPIOLIB_IRQCHIP */
2142
Jonas Gorskic771c2f2015-10-11 17:34:15 +02002143/**
2144 * gpiochip_generic_request() - request the gpio function for a pin
2145 * @chip: the gpiochip owning the GPIO
2146 * @offset: the offset of the GPIO to request for GPIO function
2147 */
2148int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
2149{
Linus Walleija9a1d2a2017-09-22 11:02:10 +02002150 return pinctrl_gpio_request(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02002151}
2152EXPORT_SYMBOL_GPL(gpiochip_generic_request);
2153
2154/**
2155 * gpiochip_generic_free() - free the gpio function from a pin
2156 * @chip: the gpiochip to request the gpio function for
2157 * @offset: the offset of the GPIO to free from GPIO function
2158 */
2159void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
2160{
Linus Walleija9a1d2a2017-09-22 11:02:10 +02002161 pinctrl_gpio_free(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02002162}
2163EXPORT_SYMBOL_GPL(gpiochip_generic_free);
2164
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002165/**
2166 * gpiochip_generic_config() - apply configuration for a pin
2167 * @chip: the gpiochip owning the GPIO
2168 * @offset: the offset of the GPIO to apply the configuration
2169 * @config: the configuration to be applied
2170 */
2171int gpiochip_generic_config(struct gpio_chip *chip, unsigned offset,
2172 unsigned long config)
2173{
2174 return pinctrl_gpio_set_config(chip->gpiodev->base + offset, config);
2175}
2176EXPORT_SYMBOL_GPL(gpiochip_generic_config);
2177
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302178#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01002179
Linus Walleij3f0f8672012-11-20 12:40:15 +01002180/**
Christian Ruppert586a87e2013-10-15 15:37:54 +02002181 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
2182 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +02002183 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +02002184 * @gpio_offset: the start offset in the current gpio_chip number space
2185 * @pin_group: name of the pin group inside the pin controller
Christian Lamparter973c1712018-05-21 22:57:39 +02002186 *
2187 * Calling this function directly from a DeviceTree-supported
2188 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
2189 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
2190 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
Christian Ruppert586a87e2013-10-15 15:37:54 +02002191 */
2192int gpiochip_add_pingroup_range(struct gpio_chip *chip,
2193 struct pinctrl_dev *pctldev,
2194 unsigned int gpio_offset, const char *pin_group)
2195{
2196 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002197 struct gpio_device *gdev = chip->gpiodev;
Christian Ruppert586a87e2013-10-15 15:37:54 +02002198 int ret;
2199
2200 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
2201 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002202 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +02002203 return -ENOMEM;
2204 }
2205
2206 /* Use local offset as range ID */
2207 pin_range->range.id = gpio_offset;
2208 pin_range->range.gc = chip;
2209 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002210 pin_range->range.base = gdev->base + gpio_offset;
Christian Ruppert586a87e2013-10-15 15:37:54 +02002211 pin_range->pctldev = pctldev;
2212
2213 ret = pinctrl_get_group_pins(pctldev, pin_group,
2214 &pin_range->range.pins,
2215 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +01002216 if (ret < 0) {
2217 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +02002218 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +01002219 }
Christian Ruppert586a87e2013-10-15 15:37:54 +02002220
2221 pinctrl_add_gpio_range(pctldev, &pin_range->range);
2222
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002223 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
2224 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +02002225 pinctrl_dev_get_devname(pctldev), pin_group);
2226
Linus Walleij20ec3e32016-02-11 11:03:06 +01002227 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Christian Ruppert586a87e2013-10-15 15:37:54 +02002228
2229 return 0;
2230}
2231EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
2232
2233/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01002234 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
2235 * @chip: the gpiochip to add the range for
Thierry Reding950d55f52017-07-24 16:57:22 +02002236 * @pinctl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01002237 * @gpio_offset: the start offset in the current gpio_chip number space
2238 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01002239 * @npins: the number of pins from the offset of each pin space (GPIO and
2240 * pin controller) to accumulate in this range
Thierry Reding950d55f52017-07-24 16:57:22 +02002241 *
2242 * Returns:
2243 * 0 on success, or a negative error-code on failure.
Christian Lamparter973c1712018-05-21 22:57:39 +02002244 *
2245 * Calling this function directly from a DeviceTree-supported
2246 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
2247 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
2248 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
Linus Walleij3f0f8672012-11-20 12:40:15 +01002249 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002250int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01002251 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01002252 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302253{
2254 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002255 struct gpio_device *gdev = chip->gpiodev;
Axel Linb4d4b1f2012-11-21 14:33:56 +08002256 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302257
Linus Walleij3f0f8672012-11-20 12:40:15 +01002258 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302259 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002260 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002261 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302262 }
2263
Linus Walleij3f0f8672012-11-20 12:40:15 +01002264 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01002265 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01002266 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302267 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002268 pin_range->range.base = gdev->base + gpio_offset;
Linus Walleij316511c2012-11-21 08:48:09 +01002269 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302270 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01002271 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302272 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01002273 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08002274 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002275 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +01002276 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08002277 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01002278 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002279 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
2280 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +01002281 pinctl_name,
2282 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302283
Linus Walleij20ec3e32016-02-11 11:03:06 +01002284 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002285
2286 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302287}
Linus Walleij165adc92012-11-06 14:49:39 +01002288EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302289
Linus Walleij3f0f8672012-11-20 12:40:15 +01002290/**
2291 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
2292 * @chip: the chip to remove all the mappings for
2293 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302294void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
2295{
2296 struct gpio_pin_range *pin_range, *tmp;
Linus Walleij20ec3e32016-02-11 11:03:06 +01002297 struct gpio_device *gdev = chip->gpiodev;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302298
Linus Walleij20ec3e32016-02-11 11:03:06 +01002299 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302300 list_del(&pin_range->node);
2301 pinctrl_remove_gpio_range(pin_range->pctldev,
2302 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01002303 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302304 }
2305}
Linus Walleij165adc92012-11-06 14:49:39 +01002306EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
2307
2308#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302309
David Brownelld2876d02008-02-04 22:28:20 -08002310/* These "optional" allocation calls help prevent drivers from stomping
2311 * on each other, and help provide better diagnostics in debugfs.
2312 * They're called even less than the "set direction" calls.
2313 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002314static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08002315{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002316 struct gpio_chip *chip = desc->gdev->chip;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002317 int status;
David Brownelld2876d02008-02-04 22:28:20 -08002318 unsigned long flags;
Biju Das3789f5a2018-08-07 08:15:18 +01002319 unsigned offset;
David Brownelld2876d02008-02-04 22:28:20 -08002320
Muchun Song18534df2018-11-01 21:12:50 +08002321 if (label) {
2322 label = kstrdup_const(label, GFP_KERNEL);
2323 if (!label)
2324 return -ENOMEM;
2325 }
2326
David Brownelld2876d02008-02-04 22:28:20 -08002327 spin_lock_irqsave(&gpio_lock, flags);
2328
David Brownelld2876d02008-02-04 22:28:20 -08002329 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07002330 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08002331 */
2332
2333 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
2334 desc_set_label(desc, label ? : "?");
2335 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07002336 } else {
Muchun Song18534df2018-11-01 21:12:50 +08002337 kfree_const(label);
David Brownelld2876d02008-02-04 22:28:20 -08002338 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -08002339 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07002340 }
2341
2342 if (chip->request) {
2343 /* chip->request may sleep */
2344 spin_unlock_irqrestore(&gpio_lock, flags);
Biju Das3789f5a2018-08-07 08:15:18 +01002345 offset = gpio_chip_hwgpio(desc);
2346 if (gpiochip_line_is_valid(chip, offset))
2347 status = chip->request(chip, offset);
2348 else
2349 status = -EINVAL;
David Brownell35e8bb52008-10-15 22:03:16 -07002350 spin_lock_irqsave(&gpio_lock, flags);
2351
2352 if (status < 0) {
2353 desc_set_label(desc, NULL);
Muchun Song18534df2018-11-01 21:12:50 +08002354 kfree_const(label);
David Brownell35e8bb52008-10-15 22:03:16 -07002355 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03002356 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07002357 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07002358 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03002359 if (chip->get_direction) {
2360 /* chip->get_direction may sleep */
2361 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002362 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03002363 spin_lock_irqsave(&gpio_lock, flags);
2364 }
David Brownelld2876d02008-02-04 22:28:20 -08002365done:
Mika Westerberg77c2d792014-03-10 14:54:50 +02002366 spin_unlock_irqrestore(&gpio_lock, flags);
2367 return status;
2368}
2369
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002370/*
2371 * This descriptor validation needs to be inserted verbatim into each
2372 * function taking a descriptor, so we need to use a preprocessor
Linus Walleij54d77192016-05-30 16:48:39 +02002373 * macro to avoid endless duplication. If the desc is NULL it is an
2374 * optional GPIO and calls should just bail out.
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002375 */
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002376static int validate_desc(const struct gpio_desc *desc, const char *func)
2377{
2378 if (!desc)
2379 return 0;
2380 if (IS_ERR(desc)) {
2381 pr_warn("%s: invalid GPIO (errorpointer)\n", func);
2382 return PTR_ERR(desc);
2383 }
2384 if (!desc->gdev) {
2385 pr_warn("%s: invalid GPIO (no device)\n", func);
2386 return -EINVAL;
2387 }
2388 if (!desc->gdev->chip) {
2389 dev_warn(&desc->gdev->dev,
2390 "%s: backing chip is gone\n", func);
2391 return 0;
2392 }
2393 return 1;
2394}
2395
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002396#define VALIDATE_DESC(desc) do { \
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002397 int __valid = validate_desc(desc, __func__); \
2398 if (__valid <= 0) \
2399 return __valid; \
2400 } while (0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002401
2402#define VALIDATE_DESC_VOID(desc) do { \
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002403 int __valid = validate_desc(desc, __func__); \
2404 if (__valid <= 0) \
Linus Walleij54d77192016-05-30 16:48:39 +02002405 return; \
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002406 } while (0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002407
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002408int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002409{
2410 int status = -EPROBE_DEFER;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002411 struct gpio_device *gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002412
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002413 VALIDATE_DESC(desc);
2414 gdev = desc->gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002415
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002416 if (try_module_get(gdev->owner)) {
Linus Walleijfac9d882017-09-26 20:58:28 +02002417 status = gpiod_request_commit(desc, label);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002418 if (status < 0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002419 module_put(gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002420 else
2421 get_device(&gdev->dev);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002422 }
2423
David Brownelld2876d02008-02-04 22:28:20 -08002424 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +02002425 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002426
David Brownelld2876d02008-02-04 22:28:20 -08002427 return status;
2428}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002429
Linus Walleijfac9d882017-09-26 20:58:28 +02002430static bool gpiod_free_commit(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002431{
Mika Westerberg77c2d792014-03-10 14:54:50 +02002432 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08002433 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07002434 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002435
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07002436 might_sleep();
2437
Alexandre Courbot372e7222013-02-03 01:29:29 +09002438 gpiod_unexport(desc);
David Brownelld8f388d82008-07-25 01:46:07 -07002439
David Brownelld2876d02008-02-04 22:28:20 -08002440 spin_lock_irqsave(&gpio_lock, flags);
2441
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002442 chip = desc->gdev->chip;
David Brownell35e8bb52008-10-15 22:03:16 -07002443 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
2444 if (chip->free) {
2445 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07002446 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002447 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07002448 spin_lock_irqsave(&gpio_lock, flags);
2449 }
Muchun Song18534df2018-11-01 21:12:50 +08002450 kfree_const(desc->label);
David Brownelld2876d02008-02-04 22:28:20 -08002451 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08002452 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07002453 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302454 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302455 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06002456 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002457 ret = true;
2458 }
David Brownelld2876d02008-02-04 22:28:20 -08002459
2460 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002461 return ret;
2462}
2463
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002464void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002465{
Linus Walleijfac9d882017-09-26 20:58:28 +02002466 if (desc && desc->gdev && gpiod_free_commit(desc)) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002467 module_put(desc->gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002468 put_device(&desc->gdev->dev);
2469 } else {
Mika Westerberg77c2d792014-03-10 14:54:50 +02002470 WARN_ON(extra_checks);
Linus Walleij33a68e82016-02-11 10:28:44 +01002471 }
David Brownelld2876d02008-02-04 22:28:20 -08002472}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002473
David Brownelld2876d02008-02-04 22:28:20 -08002474/**
2475 * gpiochip_is_requested - return string iff signal was requested
2476 * @chip: controller managing the signal
2477 * @offset: of signal within controller's 0..(ngpio - 1) range
2478 *
2479 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09002480 * The string returned is the label passed to gpio_request(); if none has been
2481 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08002482 *
2483 * This function is for use by GPIO controller drivers. The label can
2484 * help with diagnostics, and knowing that the signal is used as a GPIO
2485 * can help avoid accidentally multiplexing it to another controller.
2486 */
2487const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
2488{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002489 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08002490
Dirk Behme48b59532015-08-18 18:02:32 +02002491 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08002492 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002493
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002494 desc = &chip->gpiodev->descs[offset];
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002495
Alexandre Courbot372e7222013-02-03 01:29:29 +09002496 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08002497 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002498 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08002499}
2500EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2501
Mika Westerberg77c2d792014-03-10 14:54:50 +02002502/**
2503 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
Thierry Reding950d55f52017-07-24 16:57:22 +02002504 * @chip: GPIO chip
2505 * @hwnum: hardware number of the GPIO for which to request the descriptor
Mika Westerberg77c2d792014-03-10 14:54:50 +02002506 * @label: label for the GPIO
Linus Walleij5923ea62019-04-26 14:40:18 +02002507 * @lflags: lookup flags for this GPIO or 0 if default, this can be used to
2508 * specify things like line inversion semantics with the machine flags
2509 * such as GPIO_OUT_LOW
2510 * @dflags: descriptor request flags for this GPIO or 0 if default, this
2511 * can be used to specify consumer semantics such as open drain
Mika Westerberg77c2d792014-03-10 14:54:50 +02002512 *
2513 * Function allows GPIO chip drivers to request and use their own GPIO
2514 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2515 * function will not increase reference count of the GPIO chip module. This
2516 * allows the GPIO chip module to be unloaded as needed (we assume that the
2517 * GPIO chip driver handles freeing the GPIOs it has requested).
Thierry Reding950d55f52017-07-24 16:57:22 +02002518 *
2519 * Returns:
2520 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2521 * code on failure.
Mika Westerberg77c2d792014-03-10 14:54:50 +02002522 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002523struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
Linus Walleij21abf102018-09-04 13:31:45 +02002524 const char *label,
Linus Walleij5923ea62019-04-26 14:40:18 +02002525 enum gpio_lookup_flags lflags,
2526 enum gpiod_flags dflags)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002527{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002528 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
2529 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002530
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002531 if (IS_ERR(desc)) {
2532 chip_err(chip, "failed to get GPIO descriptor\n");
2533 return desc;
2534 }
2535
Linus Walleijfac9d882017-09-26 20:58:28 +02002536 err = gpiod_request_commit(desc, label);
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002537 if (err < 0)
2538 return ERR_PTR(err);
2539
Linus Walleij5923ea62019-04-26 14:40:18 +02002540 err = gpiod_configure_flags(desc, label, lflags, dflags);
Linus Walleij21abf102018-09-04 13:31:45 +02002541 if (err) {
2542 chip_err(chip, "setup of own GPIO %s failed\n", label);
2543 gpiod_free_commit(desc);
2544 return ERR_PTR(err);
2545 }
2546
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002547 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002548}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002549EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002550
2551/**
2552 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2553 * @desc: GPIO descriptor to free
2554 *
2555 * Function frees the given GPIO requested previously with
2556 * gpiochip_request_own_desc().
2557 */
2558void gpiochip_free_own_desc(struct gpio_desc *desc)
2559{
2560 if (desc)
Linus Walleijfac9d882017-09-26 20:58:28 +02002561 gpiod_free_commit(desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002562}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002563EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08002564
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002565/*
2566 * Drivers MUST set GPIO direction before making get/set calls. In
David Brownelld2876d02008-02-04 22:28:20 -08002567 * some cases this is done in early boot, before IRQs are enabled.
2568 *
2569 * As a rule these aren't called more than once (except for drivers
2570 * using the open-drain emulation idiom) so these are natural places
2571 * to accumulate extra debugging checks. Note that we can't (yet)
2572 * rely on gpio_request() having been called beforehand.
2573 */
2574
Thomas Petazzoni71479782019-02-07 17:28:56 +01002575static int gpio_set_config(struct gpio_chip *gc, unsigned offset,
2576 enum pin_config_param mode)
2577{
Maxime Ripard542f3612019-03-14 20:32:50 +01002578 unsigned long config;
2579 unsigned arg;
Thomas Petazzoni71479782019-02-07 17:28:56 +01002580
Maxime Ripard542f3612019-03-14 20:32:50 +01002581 switch (mode) {
2582 case PIN_CONFIG_BIAS_PULL_DOWN:
2583 case PIN_CONFIG_BIAS_PULL_UP:
2584 arg = 1;
2585 break;
2586
2587 default:
2588 arg = 0;
2589 }
2590
2591 config = PIN_CONF_PACKED(mode, arg);
Thomas Petazzoni71479782019-02-07 17:28:56 +01002592 return gc->set_config ? gc->set_config(gc, offset, config) : -ENOTSUPP;
2593}
2594
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002595/**
2596 * gpiod_direction_input - set the GPIO direction to input
2597 * @desc: GPIO to set to input
2598 *
2599 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2600 * be called safely on it.
2601 *
2602 * Return 0 in case of success, else an error code.
2603 */
2604int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002605{
David Brownelld2876d02008-02-04 22:28:20 -08002606 struct gpio_chip *chip;
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02002607 int status = 0;
David Brownelld2876d02008-02-04 22:28:20 -08002608
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002609 VALIDATE_DESC(desc);
2610 chip = desc->gdev->chip;
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002611
Linus Walleije48d1942018-09-25 09:54:14 +02002612 /*
2613 * It is legal to have no .get() and .direction_input() specified if
2614 * the chip is output-only, but you can't specify .direction_input()
2615 * and not support the .get() operation, that doesn't make sense.
2616 */
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02002617 if (!chip->get && chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01002618 gpiod_warn(desc,
Linus Walleije48d1942018-09-25 09:54:14 +02002619 "%s: missing get() but have direction_input()\n",
2620 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002621 return -EIO;
2622 }
2623
Linus Walleije48d1942018-09-25 09:54:14 +02002624 /*
2625 * If we have a .direction_input() callback, things are simple,
2626 * just call it. Else we are some input-only chip so try to check the
2627 * direction (if .get_direction() is supported) else we silently
2628 * assume we are in input mode after this.
2629 */
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02002630 if (chip->direction_input) {
2631 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
2632 } else if (chip->get_direction &&
2633 (chip->get_direction(chip, gpio_chip_hwgpio(desc)) != 1)) {
2634 gpiod_warn(desc,
Linus Walleije48d1942018-09-25 09:54:14 +02002635 "%s: missing direction_input() operation and line is output\n",
2636 __func__);
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02002637 return -EIO;
2638 }
David Brownelld2876d02008-02-04 22:28:20 -08002639 if (status == 0)
2640 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002641
Thomas Petazzonid4499912019-02-07 17:28:58 +01002642 if (test_bit(FLAG_PULL_UP, &desc->flags))
2643 gpio_set_config(chip, gpio_chip_hwgpio(desc),
2644 PIN_CONFIG_BIAS_PULL_UP);
2645 else if (test_bit(FLAG_PULL_DOWN, &desc->flags))
2646 gpio_set_config(chip, gpio_chip_hwgpio(desc),
2647 PIN_CONFIG_BIAS_PULL_DOWN);
2648
Alexandre Courbot372e7222013-02-03 01:29:29 +09002649 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09002650
David Brownelld2876d02008-02-04 22:28:20 -08002651 return status;
2652}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002653EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002654
Linus Walleijfac9d882017-09-26 20:58:28 +02002655static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08002656{
Linus Walleijc663e5f2016-03-22 10:51:16 +01002657 struct gpio_chip *gc = desc->gdev->chip;
Linus Walleijad177312016-11-13 23:02:44 +01002658 int val = !!value;
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02002659 int ret = 0;
David Brownelld2876d02008-02-04 22:28:20 -08002660
Linus Walleije48d1942018-09-25 09:54:14 +02002661 /*
2662 * It's OK not to specify .direction_output() if the gpiochip is
2663 * output-only, but if there is then not even a .set() operation it
2664 * is pretty tricky to drive the output line.
2665 */
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02002666 if (!gc->set && !gc->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01002667 gpiod_warn(desc,
Linus Walleije48d1942018-09-25 09:54:14 +02002668 "%s: missing set() and direction_output() operations\n",
2669 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002670 return -EIO;
2671 }
2672
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02002673 if (gc->direction_output) {
2674 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
2675 } else {
Linus Walleije48d1942018-09-25 09:54:14 +02002676 /* Check that we are in output mode if we can */
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02002677 if (gc->get_direction &&
2678 gc->get_direction(gc, gpio_chip_hwgpio(desc))) {
2679 gpiod_warn(desc,
2680 "%s: missing direction_output() operation\n",
2681 __func__);
2682 return -EIO;
2683 }
Linus Walleije48d1942018-09-25 09:54:14 +02002684 /*
2685 * If we can't actively set the direction, we are some
2686 * output-only chip, so just drive the output as desired.
2687 */
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02002688 gc->set(gc, gpio_chip_hwgpio(desc), val);
2689 }
2690
Linus Walleijc663e5f2016-03-22 10:51:16 +01002691 if (!ret)
David Brownelld2876d02008-02-04 22:28:20 -08002692 set_bit(FLAG_IS_OUT, &desc->flags);
Linus Walleijad177312016-11-13 23:02:44 +01002693 trace_gpio_value(desc_to_gpio(desc), 0, val);
Linus Walleijc663e5f2016-03-22 10:51:16 +01002694 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2695 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08002696}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002697
2698/**
2699 * gpiod_direction_output_raw - set the GPIO direction to output
2700 * @desc: GPIO to set to output
2701 * @value: initial output value of the GPIO
2702 *
2703 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2704 * be called safely on it. The initial value of the output must be specified
2705 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2706 *
2707 * Return 0 in case of success, else an error code.
2708 */
2709int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2710{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002711 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02002712 return gpiod_direction_output_raw_commit(desc, value);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002713}
2714EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2715
2716/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05302717 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002718 * @desc: GPIO to set to output
2719 * @value: initial output value of the GPIO
2720 *
2721 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2722 * be called safely on it. The initial value of the output must be specified
2723 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2724 * account.
2725 *
2726 * Return 0 in case of success, else an error code.
2727 */
2728int gpiod_direction_output(struct gpio_desc *desc, int value)
2729{
Vladimir Zapolskiy30322bc2017-12-21 18:37:24 +02002730 struct gpio_chip *gc;
Linus Walleij02e47982017-09-26 21:20:23 +02002731 int ret;
2732
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002733 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002734 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2735 value = !value;
Linus Walleijad177312016-11-13 23:02:44 +01002736 else
2737 value = !!value;
Linus Walleij02e47982017-09-26 21:20:23 +02002738
Hans Verkuil4e9439d2018-09-08 11:23:16 +02002739 /* GPIOs used for enabled IRQs shall not be set as output */
2740 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags) &&
2741 test_bit(FLAG_IRQ_IS_ENABLED, &desc->flags)) {
Linus Walleij02e47982017-09-26 21:20:23 +02002742 gpiod_err(desc,
2743 "%s: tried to set a GPIO tied to an IRQ as output\n",
2744 __func__);
2745 return -EIO;
2746 }
2747
Vladimir Zapolskiy30322bc2017-12-21 18:37:24 +02002748 gc = desc->gdev->chip;
Linus Walleij02e47982017-09-26 21:20:23 +02002749 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2750 /* First see if we can enable open drain in hardware */
Thomas Petazzoni71479782019-02-07 17:28:56 +01002751 ret = gpio_set_config(gc, gpio_chip_hwgpio(desc),
2752 PIN_CONFIG_DRIVE_OPEN_DRAIN);
Linus Walleij02e47982017-09-26 21:20:23 +02002753 if (!ret)
2754 goto set_output_value;
2755 /* Emulate open drain by not actively driving the line high */
2756 if (value)
2757 return gpiod_direction_input(desc);
2758 }
2759 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
Thomas Petazzoni71479782019-02-07 17:28:56 +01002760 ret = gpio_set_config(gc, gpio_chip_hwgpio(desc),
2761 PIN_CONFIG_DRIVE_OPEN_SOURCE);
Linus Walleij02e47982017-09-26 21:20:23 +02002762 if (!ret)
2763 goto set_output_value;
2764 /* Emulate open source by not actively driving the line low */
2765 if (!value)
2766 return gpiod_direction_input(desc);
2767 } else {
Thomas Petazzoni71479782019-02-07 17:28:56 +01002768 gpio_set_config(gc, gpio_chip_hwgpio(desc),
2769 PIN_CONFIG_DRIVE_PUSH_PULL);
Linus Walleij02e47982017-09-26 21:20:23 +02002770 }
2771
2772set_output_value:
Linus Walleijfac9d882017-09-26 20:58:28 +02002773 return gpiod_direction_output_raw_commit(desc, value);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002774}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002775EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08002776
Felipe Balbic4b5be92010-05-26 14:42:23 -07002777/**
Thierry Reding950d55f52017-07-24 16:57:22 +02002778 * gpiod_set_debounce - sets @debounce time for a GPIO
2779 * @desc: descriptor of the GPIO for which to set debounce time
2780 * @debounce: debounce time in microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02002781 *
Thierry Reding950d55f52017-07-24 16:57:22 +02002782 * Returns:
2783 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2784 * debounce time.
Felipe Balbic4b5be92010-05-26 14:42:23 -07002785 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002786int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07002787{
Felipe Balbic4b5be92010-05-26 14:42:23 -07002788 struct gpio_chip *chip;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002789 unsigned long config;
Felipe Balbic4b5be92010-05-26 14:42:23 -07002790
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002791 VALIDATE_DESC(desc);
2792 chip = desc->gdev->chip;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002793 if (!chip->set || !chip->set_config) {
Mark Brown6424de52013-09-09 10:33:49 +01002794 gpiod_dbg(desc,
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002795 "%s: missing set() or set_config() operations\n",
Mark Brown6424de52013-09-09 10:33:49 +01002796 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02002797 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002798 }
2799
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002800 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
Andrew Jefferyfa59dd232019-03-26 15:19:54 +10302801 return chip->set_config(chip, gpio_chip_hwgpio(desc), config);
Felipe Balbic4b5be92010-05-26 14:42:23 -07002802}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002803EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002804
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002805/**
Andrew Jefferye10f72b2017-11-30 14:25:24 +10302806 * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
2807 * @desc: descriptor of the GPIO for which to configure persistence
2808 * @transitory: True to lose state on suspend or reset, false for persistence
2809 *
2810 * Returns:
2811 * 0 on success, otherwise a negative error code.
2812 */
2813int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
2814{
2815 struct gpio_chip *chip;
2816 unsigned long packed;
2817 int gpio;
2818 int rc;
2819
Vladimir Zapolskiy156dd392017-12-21 18:37:35 +02002820 VALIDATE_DESC(desc);
Andrew Jefferye10f72b2017-11-30 14:25:24 +10302821 /*
2822 * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
2823 * persistence state.
2824 */
2825 if (transitory)
2826 set_bit(FLAG_TRANSITORY, &desc->flags);
2827 else
2828 clear_bit(FLAG_TRANSITORY, &desc->flags);
2829
2830 /* If the driver supports it, set the persistence state now */
2831 chip = desc->gdev->chip;
2832 if (!chip->set_config)
2833 return 0;
2834
2835 packed = pinconf_to_config_packed(PIN_CONFIG_PERSIST_STATE,
2836 !transitory);
2837 gpio = gpio_chip_hwgpio(desc);
Andrew Jefferyfa59dd232019-03-26 15:19:54 +10302838 rc = chip->set_config(chip, gpio, packed);
Andrew Jefferye10f72b2017-11-30 14:25:24 +10302839 if (rc == -ENOTSUPP) {
2840 dev_dbg(&desc->gdev->dev, "Persistence not supported for GPIO %d\n",
2841 gpio);
2842 return 0;
2843 }
2844
2845 return rc;
2846}
2847EXPORT_SYMBOL_GPL(gpiod_set_transitory);
2848
2849/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002850 * gpiod_is_active_low - test whether a GPIO is active-low or not
2851 * @desc: the gpio descriptor to test
2852 *
2853 * Returns 1 if the GPIO is active-low, 0 otherwise.
2854 */
2855int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002856{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002857 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002858 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002859}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002860EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08002861
2862/* I/O calls are only valid after configuration completed; the relevant
2863 * "is this a valid GPIO" error checks should already have been done.
2864 *
2865 * "Get" operations are often inlinable as reading a pin value register,
2866 * and masking the relevant bit in that register.
2867 *
2868 * When "set" operations are inlinable, they involve writing that mask to
2869 * one register to set a low value, or a different register to set it high.
2870 * Otherwise locking is needed, so there may be little value to inlining.
2871 *
2872 *------------------------------------------------------------------------
2873 *
2874 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2875 * have requested the GPIO. That can include implicit requesting by
2876 * a direction setting call. Marking a gpio as requested locks its chip
2877 * in memory, guaranteeing that these table lookups need no more locking
2878 * and that gpiochip_remove() will fail.
2879 *
2880 * REVISIT when debugging, consider adding some instrumentation to ensure
2881 * that the GPIO was actually requested.
2882 */
2883
Linus Walleijfac9d882017-09-26 20:58:28 +02002884static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002885{
2886 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002887 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002888 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002889
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002890 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002891 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002892 value = chip->get ? chip->get(chip, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01002893 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002894 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002895 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002896}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002897
Lukas Wunnereec1d562017-10-12 12:40:10 +02002898static int gpio_chip_get_multiple(struct gpio_chip *chip,
2899 unsigned long *mask, unsigned long *bits)
2900{
2901 if (chip->get_multiple) {
2902 return chip->get_multiple(chip, mask, bits);
2903 } else if (chip->get) {
2904 int i, value;
2905
2906 for_each_set_bit(i, mask, chip->ngpio) {
2907 value = chip->get(chip, i);
2908 if (value < 0)
2909 return value;
2910 __assign_bit(i, bits, value);
2911 }
2912 return 0;
2913 }
2914 return -EIO;
2915}
2916
2917int gpiod_get_array_value_complex(bool raw, bool can_sleep,
2918 unsigned int array_size,
2919 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02002920 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02002921 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02002922{
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02002923 int err, i = 0;
2924
2925 /*
2926 * Validate array_info against desc_array and its size.
2927 * It should immediately follow desc_array if both
2928 * have been obtained from the same gpiod_get_array() call.
2929 */
2930 if (array_info && array_info->desc == desc_array &&
2931 array_size <= array_info->size &&
2932 (void *)array_info == desc_array + array_info->size) {
2933 if (!can_sleep)
2934 WARN_ON(array_info->chip->can_sleep);
2935
2936 err = gpio_chip_get_multiple(array_info->chip,
2937 array_info->get_mask,
2938 value_bitmap);
2939 if (err)
2940 return err;
2941
2942 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
2943 bitmap_xor(value_bitmap, value_bitmap,
2944 array_info->invert_mask, array_size);
2945
2946 if (bitmap_full(array_info->get_mask, array_size))
2947 return 0;
2948
2949 i = find_first_zero_bit(array_info->get_mask, array_size);
2950 } else {
2951 array_info = NULL;
2952 }
Lukas Wunnereec1d562017-10-12 12:40:10 +02002953
2954 while (i < array_size) {
2955 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Laura Abbott30277432018-05-21 10:57:07 -07002956 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
2957 unsigned long *mask, *bits;
Lukas Wunnereec1d562017-10-12 12:40:10 +02002958 int first, j, ret;
2959
Laura Abbott30277432018-05-21 10:57:07 -07002960 if (likely(chip->ngpio <= FASTPATH_NGPIO)) {
2961 mask = fastpath;
2962 } else {
2963 mask = kmalloc_array(2 * BITS_TO_LONGS(chip->ngpio),
2964 sizeof(*mask),
2965 can_sleep ? GFP_KERNEL : GFP_ATOMIC);
2966 if (!mask)
2967 return -ENOMEM;
2968 }
2969
2970 bits = mask + BITS_TO_LONGS(chip->ngpio);
2971 bitmap_zero(mask, chip->ngpio);
2972
Lukas Wunnereec1d562017-10-12 12:40:10 +02002973 if (!can_sleep)
2974 WARN_ON(chip->can_sleep);
2975
2976 /* collect all inputs belonging to the same chip */
2977 first = i;
Lukas Wunnereec1d562017-10-12 12:40:10 +02002978 do {
2979 const struct gpio_desc *desc = desc_array[i];
2980 int hwgpio = gpio_chip_hwgpio(desc);
2981
2982 __set_bit(hwgpio, mask);
2983 i++;
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02002984
2985 if (array_info)
Janusz Krzysztofik35ae7f92018-09-24 01:53:35 +02002986 i = find_next_zero_bit(array_info->get_mask,
2987 array_size, i);
Lukas Wunnereec1d562017-10-12 12:40:10 +02002988 } while ((i < array_size) &&
2989 (desc_array[i]->gdev->chip == chip));
2990
2991 ret = gpio_chip_get_multiple(chip, mask, bits);
Laura Abbott30277432018-05-21 10:57:07 -07002992 if (ret) {
2993 if (mask != fastpath)
2994 kfree(mask);
Lukas Wunnereec1d562017-10-12 12:40:10 +02002995 return ret;
Laura Abbott30277432018-05-21 10:57:07 -07002996 }
Lukas Wunnereec1d562017-10-12 12:40:10 +02002997
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02002998 for (j = first; j < i; ) {
Lukas Wunnereec1d562017-10-12 12:40:10 +02002999 const struct gpio_desc *desc = desc_array[j];
3000 int hwgpio = gpio_chip_hwgpio(desc);
3001 int value = test_bit(hwgpio, bits);
3002
3003 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3004 value = !value;
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003005 __assign_bit(j, value_bitmap, value);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003006 trace_gpio_value(desc_to_gpio(desc), 1, value);
Janusz Krzysztofik799d5eb2018-09-29 14:20:22 +02003007 j++;
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003008
3009 if (array_info)
Janusz Krzysztofik35ae7f92018-09-24 01:53:35 +02003010 j = find_next_zero_bit(array_info->get_mask, i,
3011 j);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003012 }
Laura Abbott30277432018-05-21 10:57:07 -07003013
3014 if (mask != fastpath)
3015 kfree(mask);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003016 }
3017 return 0;
3018}
3019
David Brownelld2876d02008-02-04 22:28:20 -08003020/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003021 * gpiod_get_raw_value() - return a gpio's raw value
3022 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08003023 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003024 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003025 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003026 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003027 * This function can be called from contexts where we cannot sleep, and will
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003028 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003029 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003030int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003031{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003032 VALIDATE_DESC(desc);
Geert Uytterhoeven3285170f2019-07-01 16:27:38 +02003033 /* Should be using gpiod_get_raw_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003034 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02003035 return gpiod_get_raw_value_commit(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003036}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003037EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08003038
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003039/**
3040 * gpiod_get_value() - return a gpio's value
3041 * @desc: gpio whose value will be returned
3042 *
3043 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003044 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003045 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003046 * This function can be called from contexts where we cannot sleep, and will
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003047 * complain if the GPIO chip functions potentially sleep.
3048 */
3049int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08003050{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003051 int value;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003052
3053 VALIDATE_DESC(desc);
Geert Uytterhoeven3285170f2019-07-01 16:27:38 +02003054 /* Should be using gpiod_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003055 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003056
Linus Walleijfac9d882017-09-26 20:58:28 +02003057 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003058 if (value < 0)
3059 return value;
3060
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003061 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3062 value = !value;
3063
3064 return value;
David Brownelld2876d02008-02-04 22:28:20 -08003065}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003066EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08003067
Lukas Wunnereec1d562017-10-12 12:40:10 +02003068/**
3069 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003070 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02003071 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003072 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003073 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02003074 *
3075 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3076 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3077 * else an error code.
3078 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003079 * This function can be called from contexts where we cannot sleep,
Lukas Wunnereec1d562017-10-12 12:40:10 +02003080 * and it will complain if the GPIO chip functions potentially sleep.
3081 */
3082int gpiod_get_raw_array_value(unsigned int array_size,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003083 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003084 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003085 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02003086{
3087 if (!desc_array)
3088 return -EINVAL;
3089 return gpiod_get_array_value_complex(true, false, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003090 desc_array, array_info,
3091 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003092}
3093EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
3094
3095/**
3096 * gpiod_get_array_value() - read values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003097 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02003098 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003099 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003100 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02003101 *
3102 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3103 * into account. Return 0 in case of success, else an error code.
3104 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003105 * This function can be called from contexts where we cannot sleep,
Lukas Wunnereec1d562017-10-12 12:40:10 +02003106 * and it will complain if the GPIO chip functions potentially sleep.
3107 */
3108int gpiod_get_array_value(unsigned int array_size,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003109 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003110 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003111 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02003112{
3113 if (!desc_array)
3114 return -EINVAL;
3115 return gpiod_get_array_value_complex(false, false, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003116 desc_array, array_info,
3117 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003118}
3119EXPORT_SYMBOL_GPL(gpiod_get_array_value);
3120
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303121/*
Linus Walleijfac9d882017-09-26 20:58:28 +02003122 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003123 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07003124 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303125 */
Linus Walleijfac9d882017-09-26 20:58:28 +02003126static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303127{
3128 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003129 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003130 int offset = gpio_chip_hwgpio(desc);
3131
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303132 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09003133 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303134 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003135 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303136 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09003137 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303138 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003139 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303140 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09003141 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303142 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01003143 gpiod_err(desc,
3144 "%s: Error in set_value for open drain err %d\n",
3145 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303146}
3147
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303148/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003149 * _gpio_set_open_source_value() - Set the open source gpio's value.
3150 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07003151 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303152 */
Linus Walleijfac9d882017-09-26 20:58:28 +02003153static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303154{
3155 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003156 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003157 int offset = gpio_chip_hwgpio(desc);
3158
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303159 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09003160 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303161 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003162 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303163 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09003164 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303165 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003166 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303167 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09003168 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303169 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01003170 gpiod_err(desc,
3171 "%s: Error in set_value for open source err %d\n",
3172 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303173}
3174
Linus Walleijfac9d882017-09-26 20:58:28 +02003175static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08003176{
3177 struct gpio_chip *chip;
3178
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003179 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003180 trace_gpio_value(desc_to_gpio(desc), 0, value);
Linus Walleij02e47982017-09-26 21:20:23 +02003181 chip->set(chip, gpio_chip_hwgpio(desc), value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003182}
3183
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003184/*
3185 * set multiple outputs on the same chip;
3186 * use the chip's set_multiple function if available;
3187 * otherwise set the outputs sequentially;
3188 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
3189 * defines which outputs are to be changed
3190 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
3191 * defines the values the outputs specified by mask are to be set to
3192 */
3193static void gpio_chip_set_multiple(struct gpio_chip *chip,
3194 unsigned long *mask, unsigned long *bits)
3195{
3196 if (chip->set_multiple) {
3197 chip->set_multiple(chip, mask, bits);
3198 } else {
Andy Shevchenko5e4e6fb2017-01-03 19:01:17 +02003199 unsigned int i;
3200
3201 /* set outputs if the corresponding mask bit is set */
3202 for_each_set_bit(i, mask, chip->ngpio)
3203 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003204 }
3205}
3206
Laura Abbott30277432018-05-21 10:57:07 -07003207int gpiod_set_array_value_complex(bool raw, bool can_sleep,
Geert Uytterhoeven3c940662018-09-27 13:38:10 +02003208 unsigned int array_size,
3209 struct gpio_desc **desc_array,
3210 struct gpio_array *array_info,
3211 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003212{
3213 int i = 0;
3214
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003215 /*
3216 * Validate array_info against desc_array and its size.
3217 * It should immediately follow desc_array if both
3218 * have been obtained from the same gpiod_get_array() call.
3219 */
3220 if (array_info && array_info->desc == desc_array &&
3221 array_size <= array_info->size &&
3222 (void *)array_info == desc_array + array_info->size) {
3223 if (!can_sleep)
3224 WARN_ON(array_info->chip->can_sleep);
3225
3226 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
3227 bitmap_xor(value_bitmap, value_bitmap,
3228 array_info->invert_mask, array_size);
3229
3230 gpio_chip_set_multiple(array_info->chip, array_info->set_mask,
3231 value_bitmap);
3232
3233 if (bitmap_full(array_info->set_mask, array_size))
3234 return 0;
3235
3236 i = find_first_zero_bit(array_info->set_mask, array_size);
3237 } else {
3238 array_info = NULL;
3239 }
3240
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003241 while (i < array_size) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003242 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Laura Abbott30277432018-05-21 10:57:07 -07003243 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
3244 unsigned long *mask, *bits;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003245 int count = 0;
3246
Laura Abbott30277432018-05-21 10:57:07 -07003247 if (likely(chip->ngpio <= FASTPATH_NGPIO)) {
3248 mask = fastpath;
3249 } else {
3250 mask = kmalloc_array(2 * BITS_TO_LONGS(chip->ngpio),
3251 sizeof(*mask),
3252 can_sleep ? GFP_KERNEL : GFP_ATOMIC);
3253 if (!mask)
3254 return -ENOMEM;
3255 }
3256
3257 bits = mask + BITS_TO_LONGS(chip->ngpio);
3258 bitmap_zero(mask, chip->ngpio);
3259
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003260 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003261 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003262
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003263 do {
3264 struct gpio_desc *desc = desc_array[i];
3265 int hwgpio = gpio_chip_hwgpio(desc);
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003266 int value = test_bit(i, value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003267
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003268 /*
3269 * Pins applicable for fast input but not for
3270 * fast output processing may have been already
3271 * inverted inside the fast path, skip them.
3272 */
3273 if (!raw && !(array_info &&
3274 test_bit(i, array_info->invert_mask)) &&
3275 test_bit(FLAG_ACTIVE_LOW, &desc->flags))
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003276 value = !value;
3277 trace_gpio_value(desc_to_gpio(desc), 0, value);
3278 /*
3279 * collect all normal outputs belonging to the same chip
3280 * open drain and open source outputs are set individually
3281 */
Linus Walleij02e47982017-09-26 21:20:23 +02003282 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02003283 gpio_set_open_drain_value_commit(desc, value);
Linus Walleij02e47982017-09-26 21:20:23 +02003284 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02003285 gpio_set_open_source_value_commit(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003286 } else {
3287 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003288 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003289 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003290 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003291 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003292 count++;
3293 }
3294 i++;
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003295
3296 if (array_info)
Janusz Krzysztofik35ae7f92018-09-24 01:53:35 +02003297 i = find_next_zero_bit(array_info->set_mask,
3298 array_size, i);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003299 } while ((i < array_size) &&
3300 (desc_array[i]->gdev->chip == chip));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003301 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003302 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003303 gpio_chip_set_multiple(chip, mask, bits);
Laura Abbott30277432018-05-21 10:57:07 -07003304
3305 if (mask != fastpath)
3306 kfree(mask);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003307 }
Laura Abbott30277432018-05-21 10:57:07 -07003308 return 0;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003309}
3310
David Brownelld2876d02008-02-04 22:28:20 -08003311/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003312 * gpiod_set_raw_value() - assign a gpio's raw value
3313 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08003314 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08003315 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003316 * Set the raw value of the GPIO, i.e. the value of its physical line without
3317 * regard for its ACTIVE_LOW status.
3318 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003319 * This function can be called from contexts where we cannot sleep, and will
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003320 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003321 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003322void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003323{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003324 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven3285170f2019-07-01 16:27:38 +02003325 /* Should be using gpiod_set_raw_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003326 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02003327 gpiod_set_raw_value_commit(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08003328}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003329EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08003330
3331/**
Geert Uytterhoeven1e77fc82018-01-09 19:08:21 +01003332 * gpiod_set_value_nocheck() - set a GPIO line value without checking
3333 * @desc: the descriptor to set the value on
3334 * @value: value to set
3335 *
3336 * This sets the value of a GPIO line backing a descriptor, applying
3337 * different semantic quirks like active low and open drain/source
3338 * handling.
3339 */
3340static void gpiod_set_value_nocheck(struct gpio_desc *desc, int value)
3341{
3342 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3343 value = !value;
3344 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
3345 gpio_set_open_drain_value_commit(desc, value);
3346 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
3347 gpio_set_open_source_value_commit(desc, value);
3348 else
3349 gpiod_set_raw_value_commit(desc, value);
3350}
3351
3352/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003353 * gpiod_set_value() - assign a gpio's value
3354 * @desc: gpio whose value will be assigned
3355 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08003356 *
Linus Walleij02e47982017-09-26 21:20:23 +02003357 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
3358 * OPEN_DRAIN and OPEN_SOURCE flags into account.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003359 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003360 * This function can be called from contexts where we cannot sleep, and will
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003361 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003362 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003363void gpiod_set_value(struct gpio_desc *desc, int value)
3364{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003365 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven3285170f2019-07-01 16:27:38 +02003366 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003367 WARN_ON(desc->gdev->chip->can_sleep);
Geert Uytterhoeven1e77fc82018-01-09 19:08:21 +01003368 gpiod_set_value_nocheck(desc, value);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003369}
3370EXPORT_SYMBOL_GPL(gpiod_set_value);
3371
3372/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003373 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003374 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003375 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003376 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003377 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003378 *
3379 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3380 * without regard for their ACTIVE_LOW status.
3381 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003382 * This function can be called from contexts where we cannot sleep, and will
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003383 * complain if the GPIO chip functions potentially sleep.
3384 */
Laura Abbott30277432018-05-21 10:57:07 -07003385int gpiod_set_raw_array_value(unsigned int array_size,
Geert Uytterhoeven3c940662018-09-27 13:38:10 +02003386 struct gpio_desc **desc_array,
3387 struct gpio_array *array_info,
3388 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003389{
3390 if (!desc_array)
Laura Abbott30277432018-05-21 10:57:07 -07003391 return -EINVAL;
3392 return gpiod_set_array_value_complex(true, false, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003393 desc_array, array_info, value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003394}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003395EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003396
3397/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003398 * gpiod_set_array_value() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003399 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003400 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003401 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003402 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003403 *
3404 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3405 * into account.
3406 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003407 * This function can be called from contexts where we cannot sleep, and will
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003408 * complain if the GPIO chip functions potentially sleep.
3409 */
Geert Uytterhoevencf9af0d2018-09-27 13:38:09 +02003410int gpiod_set_array_value(unsigned int array_size,
3411 struct gpio_desc **desc_array,
3412 struct gpio_array *array_info,
3413 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003414{
3415 if (!desc_array)
Geert Uytterhoevencf9af0d2018-09-27 13:38:09 +02003416 return -EINVAL;
3417 return gpiod_set_array_value_complex(false, false, array_size,
3418 desc_array, array_info,
3419 value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003420}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003421EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003422
3423/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003424 * gpiod_cansleep() - report whether gpio value access may sleep
3425 * @desc: gpio to check
3426 *
3427 */
3428int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003429{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003430 VALIDATE_DESC(desc);
3431 return desc->gdev->chip->can_sleep;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003432}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003433EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08003434
David Brownell0f6d5042008-10-15 22:03:14 -07003435/**
Linus Walleij90b39402e2018-06-01 13:21:27 +02003436 * gpiod_set_consumer_name() - set the consumer name for the descriptor
3437 * @desc: gpio to set the consumer name on
3438 * @name: the new consumer name
3439 */
Muchun Song18534df2018-11-01 21:12:50 +08003440int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name)
Linus Walleij90b39402e2018-06-01 13:21:27 +02003441{
Muchun Song18534df2018-11-01 21:12:50 +08003442 VALIDATE_DESC(desc);
3443 if (name) {
3444 name = kstrdup_const(name, GFP_KERNEL);
3445 if (!name)
3446 return -ENOMEM;
3447 }
3448
3449 kfree_const(desc->label);
3450 desc_set_label(desc, name);
3451
3452 return 0;
Linus Walleij90b39402e2018-06-01 13:21:27 +02003453}
3454EXPORT_SYMBOL_GPL(gpiod_set_consumer_name);
3455
3456/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003457 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
3458 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07003459 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003460 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
3461 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07003462 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003463int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07003464{
Linus Walleij4c37ce82016-05-02 13:13:10 +02003465 struct gpio_chip *chip;
3466 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07003467
Linus Walleij79bb71b2016-06-15 22:57:38 +02003468 /*
3469 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
3470 * requires this function to not return zero on an invalid descriptor
3471 * but rather a negative error number.
3472 */
Linus Walleijbfbbe442016-06-16 11:55:55 +02003473 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
Linus Walleij79bb71b2016-06-15 22:57:38 +02003474 return -EINVAL;
3475
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003476 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003477 offset = gpio_chip_hwgpio(desc);
Linus Walleij4c37ce82016-05-02 13:13:10 +02003478 if (chip->to_irq) {
3479 int retirq = chip->to_irq(chip, offset);
3480
3481 /* Zero means NO_IRQ */
3482 if (!retirq)
3483 return -ENXIO;
3484
3485 return retirq;
3486 }
3487 return -ENXIO;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003488}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003489EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003490
Linus Walleijd468bf92013-09-24 11:54:38 +02003491/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003492 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003493 * @chip: the chip the GPIO to lock belongs to
3494 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02003495 *
3496 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08003497 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08003498 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003499int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08003500{
Linus Walleij9c102802016-05-25 10:56:03 +02003501 struct gpio_desc *desc;
Linus Walleijd468bf92013-09-24 11:54:38 +02003502
Linus Walleij9c102802016-05-25 10:56:03 +02003503 desc = gpiochip_get_desc(chip, offset);
3504 if (IS_ERR(desc))
3505 return PTR_ERR(desc);
3506
Linus Walleij60f83392016-11-12 15:01:09 +01003507 /*
3508 * If it's fast: flush the direction setting if something changed
3509 * behind our back
3510 */
3511 if (!chip->can_sleep && chip->get_direction) {
Andy Shevchenko80956792018-07-09 21:47:21 +03003512 int dir = gpiod_get_direction(desc);
Linus Walleij9c102802016-05-25 10:56:03 +02003513
Andy Shevchenko36b31272018-07-03 03:38:31 +03003514 if (dir < 0) {
3515 chip_err(chip, "%s: cannot get GPIO direction\n",
3516 __func__);
3517 return dir;
3518 }
Linus Walleij9c102802016-05-25 10:56:03 +02003519 }
3520
3521 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003522 chip_err(chip,
Andy Shevchenkob1911712018-07-03 03:39:03 +03003523 "%s: tried to flag a GPIO set as output for IRQ\n",
3524 __func__);
Linus Walleijd468bf92013-09-24 11:54:38 +02003525 return -EIO;
3526 }
3527
Linus Walleij9c102802016-05-25 10:56:03 +02003528 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
Hans Verkuil4e9439d2018-09-08 11:23:16 +02003529 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
Linus Walleij3940c342016-11-14 00:09:07 +01003530
3531 /*
3532 * If the consumer has not set up a label (such as when the
3533 * IRQ is referenced from .to_irq()) we set up a label here
3534 * so it is clear this is used as an interrupt.
3535 */
3536 if (!desc->label)
3537 desc_set_label(desc, "interrupt");
3538
Linus Walleijd468bf92013-09-24 11:54:38 +02003539 return 0;
3540}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003541EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02003542
Linus Walleijd468bf92013-09-24 11:54:38 +02003543/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003544 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003545 * @chip: the chip the GPIO to lock belongs to
3546 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02003547 *
3548 * This is used directly by GPIO drivers that want to indicate
3549 * that a certain GPIO is no longer used exclusively for IRQ.
3550 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003551void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02003552{
Linus Walleij3940c342016-11-14 00:09:07 +01003553 struct gpio_desc *desc;
3554
3555 desc = gpiochip_get_desc(chip, offset);
3556 if (IS_ERR(desc))
Linus Walleijd468bf92013-09-24 11:54:38 +02003557 return;
3558
Linus Walleij3940c342016-11-14 00:09:07 +01003559 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
Hans Verkuil4e9439d2018-09-08 11:23:16 +02003560 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
Linus Walleij3940c342016-11-14 00:09:07 +01003561
3562 /* If we only had this marking, erase it */
3563 if (desc->label && !strcmp(desc->label, "interrupt"))
3564 desc_set_label(desc, NULL);
Linus Walleijd468bf92013-09-24 11:54:38 +02003565}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003566EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02003567
Hans Verkuil4e9439d2018-09-08 11:23:16 +02003568void gpiochip_disable_irq(struct gpio_chip *chip, unsigned int offset)
3569{
3570 struct gpio_desc *desc = gpiochip_get_desc(chip, offset);
3571
3572 if (!IS_ERR(desc) &&
3573 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags)))
3574 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3575}
3576EXPORT_SYMBOL_GPL(gpiochip_disable_irq);
3577
3578void gpiochip_enable_irq(struct gpio_chip *chip, unsigned int offset)
3579{
3580 struct gpio_desc *desc = gpiochip_get_desc(chip, offset);
3581
3582 if (!IS_ERR(desc) &&
3583 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) {
3584 WARN_ON(test_bit(FLAG_IS_OUT, &desc->flags));
3585 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3586 }
3587}
3588EXPORT_SYMBOL_GPL(gpiochip_enable_irq);
3589
Linus Walleij6cee3822016-02-11 20:16:45 +01003590bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
3591{
3592 if (offset >= chip->ngpio)
3593 return false;
3594
3595 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
3596}
3597EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
3598
Hans Verkuil4e6b8232018-09-08 11:23:14 +02003599int gpiochip_reqres_irq(struct gpio_chip *chip, unsigned int offset)
3600{
3601 int ret;
3602
3603 if (!try_module_get(chip->gpiodev->owner))
3604 return -ENODEV;
3605
3606 ret = gpiochip_lock_as_irq(chip, offset);
3607 if (ret) {
3608 chip_err(chip, "unable to lock HW IRQ %u for IRQ\n", offset);
3609 module_put(chip->gpiodev->owner);
3610 return ret;
3611 }
3612 return 0;
3613}
3614EXPORT_SYMBOL_GPL(gpiochip_reqres_irq);
3615
3616void gpiochip_relres_irq(struct gpio_chip *chip, unsigned int offset)
3617{
3618 gpiochip_unlock_as_irq(chip, offset);
3619 module_put(chip->gpiodev->owner);
3620}
3621EXPORT_SYMBOL_GPL(gpiochip_relres_irq);
3622
Linus Walleij143b65d2016-02-16 15:41:42 +01003623bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
3624{
3625 if (offset >= chip->ngpio)
3626 return false;
3627
3628 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
3629}
3630EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
3631
3632bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
3633{
3634 if (offset >= chip->ngpio)
3635 return false;
3636
3637 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
3638}
3639EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
3640
Charles Keepax05f479b2017-05-23 15:47:29 +01003641bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset)
3642{
3643 if (offset >= chip->ngpio)
3644 return false;
3645
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303646 return !test_bit(FLAG_TRANSITORY, &chip->gpiodev->descs[offset].flags);
Charles Keepax05f479b2017-05-23 15:47:29 +01003647}
3648EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
3649
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003650/**
3651 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
3652 * @desc: gpio whose value will be returned
3653 *
3654 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003655 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003656 *
3657 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003658 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003659int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08003660{
David Brownelld2876d02008-02-04 22:28:20 -08003661 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003662 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003663 return gpiod_get_raw_value_commit(desc);
David Brownelld2876d02008-02-04 22:28:20 -08003664}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003665EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003666
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003667/**
3668 * gpiod_get_value_cansleep() - return a gpio's value
3669 * @desc: gpio whose value will be returned
3670 *
3671 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003672 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003673 *
3674 * This function is to be called from contexts that can sleep.
3675 */
3676int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003677{
David Brownelld2876d02008-02-04 22:28:20 -08003678 int value;
David Brownelld2876d02008-02-04 22:28:20 -08003679
3680 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003681 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003682 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003683 if (value < 0)
3684 return value;
3685
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003686 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3687 value = !value;
3688
David Brownelld2876d02008-02-04 22:28:20 -08003689 return value;
3690}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003691EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003692
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003693/**
Lukas Wunnereec1d562017-10-12 12:40:10 +02003694 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003695 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02003696 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003697 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003698 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02003699 *
3700 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3701 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3702 * else an error code.
3703 *
3704 * This function is to be called from contexts that can sleep.
3705 */
3706int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
3707 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003708 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003709 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02003710{
3711 might_sleep_if(extra_checks);
3712 if (!desc_array)
3713 return -EINVAL;
3714 return gpiod_get_array_value_complex(true, true, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003715 desc_array, array_info,
3716 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003717}
3718EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
3719
3720/**
3721 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003722 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02003723 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003724 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003725 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02003726 *
3727 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3728 * into account. Return 0 in case of success, else an error code.
3729 *
3730 * This function is to be called from contexts that can sleep.
3731 */
3732int gpiod_get_array_value_cansleep(unsigned int array_size,
3733 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003734 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003735 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02003736{
3737 might_sleep_if(extra_checks);
3738 if (!desc_array)
3739 return -EINVAL;
3740 return gpiod_get_array_value_complex(false, true, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003741 desc_array, array_info,
3742 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003743}
3744EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
3745
3746/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003747 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
3748 * @desc: gpio whose value will be assigned
3749 * @value: value to assign
3750 *
3751 * Set the raw value of the GPIO, i.e. the value of its physical line without
3752 * regard for its ACTIVE_LOW status.
3753 *
3754 * This function is to be called from contexts that can sleep.
3755 */
3756void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003757{
David Brownelld2876d02008-02-04 22:28:20 -08003758 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003759 VALIDATE_DESC_VOID(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003760 gpiod_set_raw_value_commit(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003761}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003762EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003763
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003764/**
3765 * gpiod_set_value_cansleep() - assign a gpio's value
3766 * @desc: gpio whose value will be assigned
3767 * @value: value to assign
3768 *
3769 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3770 * account
3771 *
3772 * This function is to be called from contexts that can sleep.
3773 */
3774void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003775{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003776 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003777 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1e77fc82018-01-09 19:08:21 +01003778 gpiod_set_value_nocheck(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08003779}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003780EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08003781
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003782/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003783 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003784 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003785 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003786 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003787 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003788 *
3789 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3790 * without regard for their ACTIVE_LOW status.
3791 *
3792 * This function is to be called from contexts that can sleep.
3793 */
Laura Abbott30277432018-05-21 10:57:07 -07003794int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
Geert Uytterhoeven3c940662018-09-27 13:38:10 +02003795 struct gpio_desc **desc_array,
3796 struct gpio_array *array_info,
3797 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003798{
3799 might_sleep_if(extra_checks);
3800 if (!desc_array)
Laura Abbott30277432018-05-21 10:57:07 -07003801 return -EINVAL;
3802 return gpiod_set_array_value_complex(true, true, array_size, desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003803 array_info, value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003804}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003805EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003806
3807/**
Dmitry Torokhov3946d182017-08-14 21:59:55 -07003808 * gpiod_add_lookup_tables() - register GPIO device consumers
3809 * @tables: list of tables of consumers to register
3810 * @n: number of tables in the list
3811 */
3812void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
3813{
3814 unsigned int i;
3815
3816 mutex_lock(&gpio_lookup_lock);
3817
3818 for (i = 0; i < n; i++)
3819 list_add_tail(&tables[i]->list, &gpio_lookup_list);
3820
3821 mutex_unlock(&gpio_lookup_lock);
3822}
3823
3824/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003825 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003826 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003827 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003828 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003829 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003830 *
3831 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3832 * into account.
3833 *
3834 * This function is to be called from contexts that can sleep.
3835 */
Geert Uytterhoevencf9af0d2018-09-27 13:38:09 +02003836int gpiod_set_array_value_cansleep(unsigned int array_size,
3837 struct gpio_desc **desc_array,
3838 struct gpio_array *array_info,
3839 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003840{
3841 might_sleep_if(extra_checks);
3842 if (!desc_array)
Geert Uytterhoevencf9af0d2018-09-27 13:38:09 +02003843 return -EINVAL;
3844 return gpiod_set_array_value_complex(false, true, array_size,
3845 desc_array, array_info,
3846 value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003847}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003848EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003849
3850/**
Alexandre Courbotad824782013-12-03 12:20:11 +09003851 * gpiod_add_lookup_table() - register GPIO device consumers
3852 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003853 */
Alexandre Courbotad824782013-12-03 12:20:11 +09003854void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003855{
3856 mutex_lock(&gpio_lookup_lock);
3857
Alexandre Courbotad824782013-12-03 12:20:11 +09003858 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003859
3860 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08003861}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02003862EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
David Brownelld2876d02008-02-04 22:28:20 -08003863
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05303864/**
3865 * gpiod_remove_lookup_table() - unregister GPIO device consumers
3866 * @table: table of consumers to unregister
3867 */
3868void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
3869{
3870 mutex_lock(&gpio_lookup_lock);
3871
3872 list_del(&table->list);
3873
3874 mutex_unlock(&gpio_lookup_lock);
3875}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02003876EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05303877
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02003878/**
3879 * gpiod_add_hogs() - register a set of GPIO hogs from machine code
3880 * @hogs: table of gpio hog entries with a zeroed sentinel at the end
3881 */
3882void gpiod_add_hogs(struct gpiod_hog *hogs)
3883{
3884 struct gpio_chip *chip;
3885 struct gpiod_hog *hog;
3886
3887 mutex_lock(&gpio_machine_hogs_mutex);
3888
3889 for (hog = &hogs[0]; hog->chip_label; hog++) {
3890 list_add_tail(&hog->list, &gpio_machine_hogs);
3891
3892 /*
3893 * The chip may have been registered earlier, so check if it
3894 * exists and, if so, try to hog the line now.
3895 */
3896 chip = find_chip_by_name(hog->chip_label);
3897 if (chip)
3898 gpiochip_machine_hog(chip, hog);
3899 }
3900
3901 mutex_unlock(&gpio_machine_hogs_mutex);
3902}
3903EXPORT_SYMBOL_GPL(gpiod_add_hogs);
3904
Alexandre Courbotad824782013-12-03 12:20:11 +09003905static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
3906{
3907 const char *dev_id = dev ? dev_name(dev) : NULL;
3908 struct gpiod_lookup_table *table;
3909
3910 mutex_lock(&gpio_lookup_lock);
3911
3912 list_for_each_entry(table, &gpio_lookup_list, list) {
3913 if (table->dev_id && dev_id) {
3914 /*
3915 * Valid strings on both ends, must be identical to have
3916 * a match
3917 */
3918 if (!strcmp(table->dev_id, dev_id))
3919 goto found;
3920 } else {
3921 /*
3922 * One of the pointers is NULL, so both must be to have
3923 * a match
3924 */
3925 if (dev_id == table->dev_id)
3926 goto found;
3927 }
3928 }
3929 table = NULL;
3930
3931found:
3932 mutex_unlock(&gpio_lookup_lock);
3933 return table;
3934}
3935
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003936static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Andy Shevchenkofed70262019-04-10 18:39:16 +03003937 unsigned int idx, unsigned long *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003938{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003939 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09003940 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003941 struct gpiod_lookup *p;
3942
Alexandre Courbotad824782013-12-03 12:20:11 +09003943 table = gpiod_find_lookup_table(dev);
3944 if (!table)
3945 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003946
Alexandre Courbotad824782013-12-03 12:20:11 +09003947 for (p = &table->table[0]; p->chip_label; p++) {
3948 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003949
Alexandre Courbotad824782013-12-03 12:20:11 +09003950 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003951 if (p->idx != idx)
3952 continue;
3953
Alexandre Courbotad824782013-12-03 12:20:11 +09003954 /* If the lookup entry has a con_id, require exact match */
3955 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
3956 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003957
Alexandre Courbotad824782013-12-03 12:20:11 +09003958 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003959
Alexandre Courbotad824782013-12-03 12:20:11 +09003960 if (!chip) {
Janusz Krzysztofik8853daf2018-07-04 00:18:19 +02003961 /*
3962 * As the lookup table indicates a chip with
3963 * p->chip_label should exist, assume it may
3964 * still appear later and let the interested
3965 * consumer be probed again or let the Deferred
3966 * Probe infrastructure handle the error.
3967 */
3968 dev_warn(dev, "cannot find GPIO chip %s, deferring\n",
3969 p->chip_label);
3970 return ERR_PTR(-EPROBE_DEFER);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003971 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003972
Alexandre Courbotad824782013-12-03 12:20:11 +09003973 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003974 dev_err(dev,
3975 "requested GPIO %d is out of range [0..%d] for chip %s\n",
3976 idx, chip->ngpio, chip->label);
3977 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09003978 }
3979
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09003980 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09003981 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003982
3983 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09003984 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003985
3986 return desc;
3987}
3988
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003989static int platform_gpio_count(struct device *dev, const char *con_id)
3990{
3991 struct gpiod_lookup_table *table;
3992 struct gpiod_lookup *p;
3993 unsigned int count = 0;
3994
3995 table = gpiod_find_lookup_table(dev);
3996 if (!table)
3997 return -ENOENT;
3998
3999 for (p = &table->table[0]; p->chip_label; p++) {
4000 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
4001 (!con_id && !p->con_id))
4002 count++;
4003 }
4004 if (!count)
4005 return -ENOENT;
4006
4007 return count;
4008}
4009
4010/**
4011 * gpiod_count - return the number of GPIOs associated with a device / function
4012 * or -ENOENT if no GPIO has been assigned to the requested function
4013 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4014 * @con_id: function within the GPIO consumer
4015 */
4016int gpiod_count(struct device *dev, const char *con_id)
4017{
4018 int count = -ENOENT;
4019
4020 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
Linus Walleijf626d6d2019-07-17 09:10:01 +02004021 count = of_gpio_get_count(dev, con_id);
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004022 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
4023 count = acpi_gpio_count(dev, con_id);
4024
4025 if (count < 0)
4026 count = platform_gpio_count(dev, con_id);
4027
4028 return count;
4029}
4030EXPORT_SYMBOL_GPL(gpiod_count);
4031
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004032/**
Thierry Reding08791622014-04-25 16:54:22 +02004033 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09004034 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004035 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004036 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004037 *
4038 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004039 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07004040 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004041 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004042struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004043 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004044{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004045 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004046}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004047EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004048
4049/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02004050 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
4051 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4052 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004053 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02004054 *
4055 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
4056 * the requested function it will return NULL. This is convenient for drivers
4057 * that need to handle optional GPIOs.
4058 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004059struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004060 const char *con_id,
4061 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02004062{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004063 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02004064}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004065EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02004066
Benoit Parrotf625d462015-02-02 11:44:44 -06004067
4068/**
4069 * gpiod_configure_flags - helper function to configure a given GPIO
4070 * @desc: gpio whose value will be assigned
4071 * @con_id: function within the GPIO consumer
Andy Shevchenkofed70262019-04-10 18:39:16 +03004072 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4073 * of_find_gpio() or of_get_gpio_hog()
Benoit Parrotf625d462015-02-02 11:44:44 -06004074 * @dflags: gpiod_flags - optional GPIO initialization flags
4075 *
4076 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
4077 * requested function and/or index, or another IS_ERR() code if an error
4078 * occurred while trying to acquire the GPIO.
4079 */
Andy Shevchenkoc29fd9e2017-05-23 20:03:16 +03004080int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Johan Hovold85b03b32016-07-03 18:32:05 +02004081 unsigned long lflags, enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06004082{
4083 int status;
4084
Johan Hovold85b03b32016-07-03 18:32:05 +02004085 if (lflags & GPIO_ACTIVE_LOW)
4086 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
Linus Walleijf926dfc2017-09-10 19:26:22 +02004087
Johan Hovold85b03b32016-07-03 18:32:05 +02004088 if (lflags & GPIO_OPEN_DRAIN)
4089 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
Linus Walleijf926dfc2017-09-10 19:26:22 +02004090 else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) {
4091 /*
4092 * This enforces open drain mode from the consumer side.
4093 * This is necessary for some busses like I2C, but the lookup
4094 * should *REALLY* have specified them as open drain in the
4095 * first place, so print a little warning here.
4096 */
4097 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
4098 gpiod_warn(desc,
4099 "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
4100 }
4101
Johan Hovold85b03b32016-07-03 18:32:05 +02004102 if (lflags & GPIO_OPEN_SOURCE)
4103 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Andrew Jefferye10f72b2017-11-30 14:25:24 +10304104
Thomas Petazzonid4499912019-02-07 17:28:58 +01004105 if ((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DOWN)) {
4106 gpiod_err(desc,
4107 "both pull-up and pull-down enabled, invalid configuration\n");
4108 return -EINVAL;
4109 }
4110
4111 if (lflags & GPIO_PULL_UP)
4112 set_bit(FLAG_PULL_UP, &desc->flags);
4113 else if (lflags & GPIO_PULL_DOWN)
4114 set_bit(FLAG_PULL_DOWN, &desc->flags);
4115
Andrew Jefferye10f72b2017-11-30 14:25:24 +10304116 status = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
4117 if (status < 0)
4118 return status;
Johan Hovold85b03b32016-07-03 18:32:05 +02004119
Benoit Parrotf625d462015-02-02 11:44:44 -06004120 /* No particular flag request, return here... */
4121 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
4122 pr_debug("no flags found for %s\n", con_id);
4123 return 0;
4124 }
4125
4126 /* Process flags */
4127 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
4128 status = gpiod_direction_output(desc,
Linus Walleijad177312016-11-13 23:02:44 +01004129 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
Benoit Parrotf625d462015-02-02 11:44:44 -06004130 else
4131 status = gpiod_direction_input(desc);
4132
4133 return status;
4134}
4135
Thierry Reding29a1f2332014-04-25 17:10:06 +02004136/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004137 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02004138 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004139 * @con_id: function within the GPIO consumer
4140 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004141 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004142 *
4143 * This variant of gpiod_get() allows to access GPIOs other than the first
4144 * defined one for functions that define several GPIOs.
4145 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004146 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
4147 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07004148 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004149 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004150struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004151 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004152 unsigned int idx,
4153 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004154{
Andy Shevchenko2d6c06f2019-04-10 18:39:17 +03004155 unsigned long lookupflags = GPIO_LOOKUP_FLAGS_DEFAULT;
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09004156 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004157 int status;
Linus Walleij7d18f0a2018-01-16 08:29:50 +01004158 /* Maybe we have a device name, maybe not */
4159 const char *devname = dev ? dev_name(dev) : "?";
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004160
4161 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
4162
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01004163 if (dev) {
4164 /* Using device tree? */
4165 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
4166 dev_dbg(dev, "using device tree for GPIO lookup\n");
4167 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
4168 } else if (ACPI_COMPANION(dev)) {
4169 dev_dbg(dev, "using ACPI for GPIO lookup\n");
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +03004170 desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags);
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01004171 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09004172 }
4173
4174 /*
4175 * Either we are not using DT or ACPI, or their lookup did not return
4176 * a result. In that case, use platform lookup as a fallback.
4177 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004178 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04004179 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004180 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004181 }
4182
4183 if (IS_ERR(desc)) {
Wang Dongsheng9d5a1f22018-02-27 00:12:13 -08004184 dev_dbg(dev, "No GPIO consumer %s found\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004185 return desc;
4186 }
4187
Linus Walleij7d18f0a2018-01-16 08:29:50 +01004188 /*
4189 * If a connection label was passed use that, else attempt to use
4190 * the device name as label
4191 */
4192 status = gpiod_request(desc, con_id ? con_id : devname);
Linus Walleijb0ce7b292018-10-12 14:54:12 +02004193 if (status < 0) {
4194 if (status == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE) {
4195 /*
4196 * This happens when there are several consumers for
4197 * the same GPIO line: we just return here without
4198 * further initialization. It is a bit if a hack.
4199 * This is necessary to support fixed regulators.
4200 *
4201 * FIXME: Make this more sane and safe.
4202 */
4203 dev_info(dev, "nonexclusive access to GPIO for %s\n",
4204 con_id ? con_id : devname);
4205 return desc;
4206 } else {
4207 return ERR_PTR(status);
4208 }
4209 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004210
Johan Hovold85b03b32016-07-03 18:32:05 +02004211 status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004212 if (status < 0) {
4213 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
4214 gpiod_put(desc);
4215 return ERR_PTR(status);
4216 }
4217
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004218 return desc;
4219}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004220EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004221
4222/**
Mika Westerberg40b73182014-10-21 13:33:59 +02004223 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
4224 * @fwnode: handle of the firmware node
4225 * @propname: name of the firmware property representing the GPIO
Linus Walleij6392cca2017-12-29 02:07:54 +01004226 * @index: index of the GPIO to obtain for the consumer
Andy Shevchenkoa264d102017-01-09 16:02:28 +02004227 * @dflags: GPIO initialization flags
Thierry Reding950d55f52017-07-24 16:57:22 +02004228 * @label: label to attach to the requested GPIO
Mika Westerberg40b73182014-10-21 13:33:59 +02004229 *
4230 * This function can be used for drivers that get their configuration
Linus Walleij6392cca2017-12-29 02:07:54 +01004231 * from opaque firmware.
Mika Westerberg40b73182014-10-21 13:33:59 +02004232 *
Linus Walleij6392cca2017-12-29 02:07:54 +01004233 * The function properly finds the corresponding GPIO using whatever is the
Mika Westerberg40b73182014-10-21 13:33:59 +02004234 * underlying firmware interface and then makes sure that the GPIO
4235 * descriptor is requested before it is returned to the caller.
4236 *
Thierry Reding950d55f52017-07-24 16:57:22 +02004237 * Returns:
Andy Shevchenkoff213782017-02-28 17:03:12 +02004238 * On successful request the GPIO pin is configured in accordance with
Andy Shevchenkoa264d102017-01-09 16:02:28 +02004239 * provided @dflags.
4240 *
Mika Westerberg40b73182014-10-21 13:33:59 +02004241 * In case of error an ERR_PTR() is returned.
4242 */
4243struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
Boris Brezillon537b94d2017-02-02 14:53:11 +01004244 const char *propname, int index,
Alexander Steinb2987d72017-01-12 17:39:24 +01004245 enum gpiod_flags dflags,
4246 const char *label)
Mika Westerberg40b73182014-10-21 13:33:59 +02004247{
Andy Shevchenko2d6c06f2019-04-10 18:39:17 +03004248 unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
Mika Westerberg40b73182014-10-21 13:33:59 +02004249 struct gpio_desc *desc = ERR_PTR(-ENODEV);
Mika Westerberg40b73182014-10-21 13:33:59 +02004250 int ret;
4251
4252 if (!fwnode)
4253 return ERR_PTR(-EINVAL);
4254
4255 if (is_of_node(fwnode)) {
Linus Walleij6392cca2017-12-29 02:07:54 +01004256 desc = gpiod_get_from_of_node(to_of_node(fwnode),
4257 propname, index,
4258 dflags,
4259 label);
4260 return desc;
Mika Westerberg40b73182014-10-21 13:33:59 +02004261 } else if (is_acpi_node(fwnode)) {
4262 struct acpi_gpio_info info;
4263
Boris Brezillon537b94d2017-02-02 14:53:11 +01004264 desc = acpi_node_get_gpiod(fwnode, propname, index, &info);
Linus Walleij6392cca2017-12-29 02:07:54 +01004265 if (IS_ERR(desc))
4266 return desc;
4267
4268 acpi_gpio_update_gpiod_flags(&dflags, &info);
Andy Shevchenko606be342019-04-10 18:39:20 +03004269 acpi_gpio_update_gpiod_lookup_flags(&lflags, &info);
Mika Westerberg40b73182014-10-21 13:33:59 +02004270 }
4271
Linus Walleij6392cca2017-12-29 02:07:54 +01004272 /* Currently only ACPI takes this path */
Alexander Steinb2987d72017-01-12 17:39:24 +01004273 ret = gpiod_request(desc, label);
Johan Hovold85b03b32016-07-03 18:32:05 +02004274 if (ret)
4275 return ERR_PTR(ret);
4276
Andy Shevchenkoa264d102017-01-09 16:02:28 +02004277 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
4278 if (ret < 0) {
4279 gpiod_put(desc);
4280 return ERR_PTR(ret);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03004281 }
4282
Mika Westerberg40b73182014-10-21 13:33:59 +02004283 return desc;
4284}
4285EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
4286
4287/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02004288 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
4289 * function
4290 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4291 * @con_id: function within the GPIO consumer
4292 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004293 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02004294 *
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.
4298 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004299struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02004300 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004301 unsigned int index,
4302 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02004303{
4304 struct gpio_desc *desc;
4305
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004306 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02004307 if (IS_ERR(desc)) {
4308 if (PTR_ERR(desc) == -ENOENT)
4309 return NULL;
4310 }
4311
4312 return desc;
4313}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004314EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02004315
4316/**
Benoit Parrotf625d462015-02-02 11:44:44 -06004317 * gpiod_hog - Hog the specified GPIO desc given the provided flags
4318 * @desc: gpio whose value will be assigned
4319 * @name: gpio line name
Andy Shevchenkofed70262019-04-10 18:39:16 +03004320 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4321 * of_find_gpio() or of_get_gpio_hog()
Benoit Parrotf625d462015-02-02 11:44:44 -06004322 * @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
4332 chip = gpiod_to_chip(desc);
4333 hwnum = gpio_chip_hwgpio(desc);
4334
Linus Walleij5923ea62019-04-26 14:40:18 +02004335 local_desc = gpiochip_request_own_desc(chip, hwnum, name,
4336 lflags, dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -06004337 if (IS_ERR(local_desc)) {
Laxman Dewanganc31a5712016-03-11 19:13:21 +05304338 status = PTR_ERR(local_desc);
4339 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
4340 name, chip->label, hwnum, status);
4341 return status;
Benoit Parrotf625d462015-02-02 11:44:44 -06004342 }
4343
Benoit Parrotf625d462015-02-02 11:44:44 -06004344 /* Mark GPIO as hogged so it can be identified and removed later */
4345 set_bit(FLAG_IS_HOGGED, &desc->flags);
4346
4347 pr_info("GPIO line %d (%s) hogged as %s%s\n",
4348 desc_to_gpio(desc), name,
4349 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
4350 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
4351 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
4352
4353 return 0;
4354}
4355
4356/**
4357 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
4358 * @chip: gpio chip to act on
Benoit Parrotf625d462015-02-02 11:44:44 -06004359 */
4360static void gpiochip_free_hogs(struct gpio_chip *chip)
4361{
4362 int id;
4363
4364 for (id = 0; id < chip->ngpio; id++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01004365 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
4366 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
Benoit Parrotf625d462015-02-02 11:44:44 -06004367 }
4368}
4369
4370/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004371 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
4372 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4373 * @con_id: function within the GPIO consumer
4374 * @flags: optional GPIO initialization flags
4375 *
4376 * This function acquires all the GPIOs defined under a given function.
4377 *
4378 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
4379 * no GPIO has been assigned to the requested function, or another IS_ERR()
4380 * code if an error occurred while trying to acquire the GPIOs.
4381 */
4382struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
4383 const char *con_id,
4384 enum gpiod_flags flags)
4385{
4386 struct gpio_desc *desc;
4387 struct gpio_descs *descs;
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004388 struct gpio_array *array_info = NULL;
4389 struct gpio_chip *chip;
4390 int count, bitmap_size;
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004391
4392 count = gpiod_count(dev, con_id);
4393 if (count < 0)
4394 return ERR_PTR(count);
4395
Kees Cookacafe7e2018-05-08 13:45:50 -07004396 descs = kzalloc(struct_size(descs, desc, count), GFP_KERNEL);
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004397 if (!descs)
4398 return ERR_PTR(-ENOMEM);
4399
4400 for (descs->ndescs = 0; descs->ndescs < count; ) {
4401 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
4402 if (IS_ERR(desc)) {
4403 gpiod_put_array(descs);
4404 return ERR_CAST(desc);
4405 }
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004406
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004407 descs->desc[descs->ndescs] = desc;
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004408
4409 chip = gpiod_to_chip(desc);
4410 /*
Janusz Krzysztofikc4c958a2018-09-24 01:53:36 +02004411 * If pin hardware number of array member 0 is also 0, select
4412 * its chip as a candidate for fast bitmap processing path.
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004413 */
Janusz Krzysztofikc4c958a2018-09-24 01:53:36 +02004414 if (descs->ndescs == 0 && gpio_chip_hwgpio(desc) == 0) {
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004415 struct gpio_descs *array;
4416
4417 bitmap_size = BITS_TO_LONGS(chip->ngpio > count ?
4418 chip->ngpio : count);
4419
4420 array = kzalloc(struct_size(descs, desc, count) +
4421 struct_size(array_info, invert_mask,
4422 3 * bitmap_size), GFP_KERNEL);
4423 if (!array) {
4424 gpiod_put_array(descs);
4425 return ERR_PTR(-ENOMEM);
4426 }
4427
4428 memcpy(array, descs,
4429 struct_size(descs, desc, descs->ndescs + 1));
4430 kfree(descs);
4431
4432 descs = array;
4433 array_info = (void *)(descs->desc + count);
4434 array_info->get_mask = array_info->invert_mask +
4435 bitmap_size;
4436 array_info->set_mask = array_info->get_mask +
4437 bitmap_size;
4438
4439 array_info->desc = descs->desc;
4440 array_info->size = count;
4441 array_info->chip = chip;
4442 bitmap_set(array_info->get_mask, descs->ndescs,
4443 count - descs->ndescs);
4444 bitmap_set(array_info->set_mask, descs->ndescs,
4445 count - descs->ndescs);
4446 descs->info = array_info;
4447 }
Janusz Krzysztofikc4c958a2018-09-24 01:53:36 +02004448 /* Unmark array members which don't belong to the 'fast' chip */
4449 if (array_info && array_info->chip != chip) {
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004450 __clear_bit(descs->ndescs, array_info->get_mask);
4451 __clear_bit(descs->ndescs, array_info->set_mask);
Janusz Krzysztofikc4c958a2018-09-24 01:53:36 +02004452 }
4453 /*
4454 * Detect array members which belong to the 'fast' chip
4455 * but their pins are not in hardware order.
4456 */
4457 else if (array_info &&
4458 gpio_chip_hwgpio(desc) != descs->ndescs) {
4459 /*
4460 * Don't use fast path if all array members processed so
4461 * far belong to the same chip as this one but its pin
4462 * hardware number is different from its array index.
4463 */
4464 if (bitmap_full(array_info->get_mask, descs->ndescs)) {
4465 array_info = NULL;
4466 } else {
4467 __clear_bit(descs->ndescs,
4468 array_info->get_mask);
4469 __clear_bit(descs->ndescs,
4470 array_info->set_mask);
4471 }
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004472 } else if (array_info) {
4473 /* Exclude open drain or open source from fast output */
4474 if (gpiochip_line_is_open_drain(chip, descs->ndescs) ||
4475 gpiochip_line_is_open_source(chip, descs->ndescs))
4476 __clear_bit(descs->ndescs,
4477 array_info->set_mask);
4478 /* Identify 'fast' pins which require invertion */
4479 if (gpiod_is_active_low(desc))
4480 __set_bit(descs->ndescs,
4481 array_info->invert_mask);
4482 }
4483
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004484 descs->ndescs++;
4485 }
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004486 if (array_info)
4487 dev_dbg(dev,
4488 "GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n",
4489 array_info->chip->label, array_info->size,
4490 *array_info->get_mask, *array_info->set_mask,
4491 *array_info->invert_mask);
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004492 return descs;
4493}
4494EXPORT_SYMBOL_GPL(gpiod_get_array);
4495
4496/**
4497 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
4498 * function
4499 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4500 * @con_id: function within the GPIO consumer
4501 * @flags: optional GPIO initialization flags
4502 *
4503 * This is equivalent to gpiod_get_array(), except that when no GPIO was
4504 * assigned to the requested function it will return NULL.
4505 */
4506struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
4507 const char *con_id,
4508 enum gpiod_flags flags)
4509{
4510 struct gpio_descs *descs;
4511
4512 descs = gpiod_get_array(dev, con_id, flags);
4513 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
4514 return NULL;
4515
4516 return descs;
4517}
4518EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
4519
4520/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004521 * gpiod_put - dispose of a GPIO descriptor
4522 * @desc: GPIO descriptor to dispose of
4523 *
4524 * No descriptor can be used after gpiod_put() has been called on it.
4525 */
4526void gpiod_put(struct gpio_desc *desc)
4527{
Andy Shevchenko1d7765b2019-03-26 17:21:14 +02004528 if (desc)
4529 gpiod_free(desc);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004530}
4531EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08004532
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004533/**
4534 * gpiod_put_array - dispose of multiple GPIO descriptors
4535 * @descs: struct gpio_descs containing an array of descriptors
4536 */
4537void gpiod_put_array(struct gpio_descs *descs)
4538{
4539 unsigned int i;
4540
4541 for (i = 0; i < descs->ndescs; i++)
4542 gpiod_put(descs->desc[i]);
4543
4544 kfree(descs);
4545}
4546EXPORT_SYMBOL_GPL(gpiod_put_array);
4547
Linus Walleij3c702e92015-10-21 15:29:53 +02004548static int __init gpiolib_dev_init(void)
4549{
4550 int ret;
4551
4552 /* Register GPIO sysfs bus */
Andy Shevchenkob1911712018-07-03 03:39:03 +03004553 ret = bus_register(&gpio_bus_type);
Linus Walleij3c702e92015-10-21 15:29:53 +02004554 if (ret < 0) {
4555 pr_err("gpiolib: could not register GPIO bus type\n");
4556 return ret;
4557 }
4558
4559 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
4560 if (ret < 0) {
4561 pr_err("gpiolib: failed to allocate char dev region\n");
4562 bus_unregister(&gpio_bus_type);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07004563 } else {
4564 gpiolib_initialized = true;
4565 gpiochip_setup_devs();
Linus Walleij3c702e92015-10-21 15:29:53 +02004566 }
4567 return ret;
4568}
4569core_initcall(gpiolib_dev_init);
4570
David Brownelld2876d02008-02-04 22:28:20 -08004571#ifdef CONFIG_DEBUG_FS
4572
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004573static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
David Brownelld2876d02008-02-04 22:28:20 -08004574{
4575 unsigned i;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004576 struct gpio_chip *chip = gdev->chip;
4577 unsigned gpio = gdev->base;
4578 struct gpio_desc *gdesc = &gdev->descs[0];
Linus Walleij90fd2272018-10-01 23:06:17 +02004579 bool is_out;
4580 bool is_irq;
4581 bool active_low;
David Brownelld2876d02008-02-04 22:28:20 -08004582
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004583 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02004584 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
4585 if (gdesc->name) {
4586 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
4587 gpio, gdesc->name);
4588 }
David Brownelld2876d02008-02-04 22:28:20 -08004589 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02004590 }
David Brownelld2876d02008-02-04 22:28:20 -08004591
Alexandre Courbot372e7222013-02-03 01:29:29 +09004592 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08004593 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02004594 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Linus Walleij90fd2272018-10-01 23:06:17 +02004595 active_low = test_bit(FLAG_ACTIVE_LOW, &gdesc->flags);
4596 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s%s",
Markus Pargmannced433e2015-08-14 16:11:02 +02004597 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08004598 is_out ? "out" : "in ",
Andy Shevchenko1c22a252018-07-12 20:36:42 +03004599 chip->get ? (chip->get(chip, i) ? "hi" : "lo") : "? ",
Linus Walleij90fd2272018-10-01 23:06:17 +02004600 is_irq ? "IRQ " : "",
4601 active_low ? "ACTIVE LOW" : "");
David Brownelld2876d02008-02-04 22:28:20 -08004602 seq_printf(s, "\n");
4603 }
4604}
4605
Thierry Redingf9c4a312012-04-12 13:26:01 +02004606static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08004607{
Grant Likely362432a2013-02-09 09:41:49 +00004608 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02004609 struct gpio_device *gdev = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09004610 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02004611
4612 s->private = "";
4613
Grant Likely362432a2013-02-09 09:41:49 +00004614 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02004615 list_for_each_entry(gdev, &gpio_devices, list)
Grant Likely362432a2013-02-09 09:41:49 +00004616 if (index-- == 0) {
4617 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02004618 return gdev;
Grant Likely362432a2013-02-09 09:41:49 +00004619 }
4620 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09004621
4622 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02004623}
4624
4625static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
4626{
Grant Likely362432a2013-02-09 09:41:49 +00004627 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02004628 struct gpio_device *gdev = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02004629 void *ret = NULL;
4630
Grant Likely362432a2013-02-09 09:41:49 +00004631 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02004632 if (list_is_last(&gdev->list, &gpio_devices))
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09004633 ret = NULL;
4634 else
Linus Walleijff2b1352015-10-20 11:10:38 +02004635 ret = list_entry(gdev->list.next, struct gpio_device, list);
Grant Likely362432a2013-02-09 09:41:49 +00004636 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02004637
4638 s->private = "\n";
4639 ++*pos;
4640
4641 return ret;
4642}
4643
4644static void gpiolib_seq_stop(struct seq_file *s, void *v)
4645{
4646}
4647
4648static int gpiolib_seq_show(struct seq_file *s, void *v)
4649{
Linus Walleijff2b1352015-10-20 11:10:38 +02004650 struct gpio_device *gdev = v;
4651 struct gpio_chip *chip = gdev->chip;
4652 struct device *parent;
Thierry Redingf9c4a312012-04-12 13:26:01 +02004653
Linus Walleijff2b1352015-10-20 11:10:38 +02004654 if (!chip) {
4655 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
4656 dev_name(&gdev->dev));
4657 return 0;
4658 }
4659
4660 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
4661 dev_name(&gdev->dev),
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004662 gdev->base, gdev->base + gdev->ngpio - 1);
Linus Walleijff2b1352015-10-20 11:10:38 +02004663 parent = chip->parent;
4664 if (parent)
4665 seq_printf(s, ", parent: %s/%s",
4666 parent->bus ? parent->bus->name : "no-bus",
4667 dev_name(parent));
Thierry Redingf9c4a312012-04-12 13:26:01 +02004668 if (chip->label)
4669 seq_printf(s, ", %s", chip->label);
4670 if (chip->can_sleep)
4671 seq_printf(s, ", can sleep");
4672 seq_printf(s, ":\n");
4673
4674 if (chip->dbg_show)
4675 chip->dbg_show(s, chip);
4676 else
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004677 gpiolib_dbg_show(s, gdev);
Thierry Redingf9c4a312012-04-12 13:26:01 +02004678
David Brownelld2876d02008-02-04 22:28:20 -08004679 return 0;
4680}
4681
Thierry Redingf9c4a312012-04-12 13:26:01 +02004682static const struct seq_operations gpiolib_seq_ops = {
4683 .start = gpiolib_seq_start,
4684 .next = gpiolib_seq_next,
4685 .stop = gpiolib_seq_stop,
4686 .show = gpiolib_seq_show,
4687};
4688
David Brownelld2876d02008-02-04 22:28:20 -08004689static int gpiolib_open(struct inode *inode, struct file *file)
4690{
Thierry Redingf9c4a312012-04-12 13:26:01 +02004691 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08004692}
4693
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004694static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02004695 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08004696 .open = gpiolib_open,
4697 .read = seq_read,
4698 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02004699 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08004700};
4701
4702static int __init gpiolib_debugfs_init(void)
4703{
4704 /* /sys/kernel/debug/gpio */
Greg Kroah-Hartmanacc68b02019-06-18 17:50:45 +02004705 debugfs_create_file("gpio", S_IFREG | S_IRUGO, NULL, NULL,
4706 &gpiolib_operations);
David Brownelld2876d02008-02-04 22:28:20 -08004707 return 0;
4708}
4709subsys_initcall(gpiolib_debugfs_init);
4710
4711#endif /* DEBUG_FS */