blob: a9826627f3475c7af0fbed149839fa49d7a0dc10 [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"
Andy Shevchenko77cb9072019-07-30 13:43:36 +030033#include "gpiolib-acpi.h"
Mika Westerberg664e3e52014-01-08 12:40:54 +020034
Uwe Kleine-König3f397c212011-05-20 00:40:19 -060035#define CREATE_TRACE_POINTS
36#include <trace/events/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080037
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070038/* Implementation infrastructure for GPIO interfaces.
David Brownelld2876d02008-02-04 22:28:20 -080039 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070040 * The GPIO programming interface allows for inlining speed-critical
41 * get/set operations for common cases, so that access to SOC-integrated
42 * GPIOs can sometimes cost only an instruction or two per bit.
David Brownelld2876d02008-02-04 22:28:20 -080043 */
44
45
46/* When debugging, extend minimal trust to callers and platform code.
47 * Also emit diagnostic messages that may help initial bringup, when
48 * board setup or driver bugs are most common.
49 *
50 * Otherwise, minimize overhead in what may be bitbanging codepaths.
51 */
52#ifdef DEBUG
53#define extra_checks 1
54#else
55#define extra_checks 0
56#endif
57
Linus Walleijff2b1352015-10-20 11:10:38 +020058/* Device and char device-related information */
59static DEFINE_IDA(gpio_ida);
Linus Walleij3c702e92015-10-21 15:29:53 +020060static dev_t gpio_devt;
61#define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
62static struct bus_type gpio_bus_type = {
63 .name = "gpio",
64};
Linus Walleijff2b1352015-10-20 11:10:38 +020065
Laura Abbott30277432018-05-21 10:57:07 -070066/*
67 * Number of GPIOs to use for the fast path in set array
68 */
69#define FASTPATH_NGPIO CONFIG_GPIOLIB_FASTPATH_LIMIT
70
David Brownelld2876d02008-02-04 22:28:20 -080071/* gpio_lock prevents conflicts during gpio_desc[] table updates.
72 * While any GPIO is requested, its gpio_chip is not removable;
73 * each GPIO's "requested" flag serves as a lock and refcount.
74 */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090075DEFINE_SPINLOCK(gpio_lock);
David Brownelld2876d02008-02-04 22:28:20 -080076
Alexandre Courbotbae48da2013-10-17 10:21:38 -070077static DEFINE_MUTEX(gpio_lookup_lock);
78static LIST_HEAD(gpio_lookup_list);
Linus Walleijff2b1352015-10-20 11:10:38 +020079LIST_HEAD(gpio_devices);
Johan Hovold6d867502015-05-04 17:23:25 +020080
Bartosz Golaszewskia411e812018-04-10 22:30:28 +020081static DEFINE_MUTEX(gpio_machine_hogs_mutex);
82static LIST_HEAD(gpio_machine_hogs);
83
Johan Hovold6d867502015-05-04 17:23:25 +020084static void gpiochip_free_hogs(struct gpio_chip *chip);
Thierry Reding959bc7b2017-11-07 19:15:59 +010085static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
Andrew Lunn39c3fd52017-12-02 18:11:04 +010086 struct lock_class_key *lock_key,
87 struct lock_class_key *request_key);
Johan Hovold6d867502015-05-04 17:23:25 +020088static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
Andy Shevchenko9411e3a2019-10-09 17:34:44 +030089static int gpiochip_irqchip_init_hw(struct gpio_chip *gpiochip);
Mika Westerberg79b804c2016-09-20 15:15:21 +030090static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip);
91static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip);
Johan Hovold6d867502015-05-04 17:23:25 +020092
Guenter Roeck159f3cd2016-03-31 08:11:30 -070093static bool gpiolib_initialized;
Johan Hovold6d867502015-05-04 17:23:25 +020094
David Brownelld2876d02008-02-04 22:28:20 -080095static inline void desc_set_label(struct gpio_desc *d, const char *label)
96{
David Brownelld2876d02008-02-04 22:28:20 -080097 d->label = label;
David Brownelld2876d02008-02-04 22:28:20 -080098}
99
Alexandre Courbot372e7222013-02-03 01:29:29 +0900100/**
Thierry Reding950d55f52017-07-24 16:57:22 +0200101 * gpio_to_desc - Convert a GPIO number to its descriptor
102 * @gpio: global GPIO number
103 *
104 * Returns:
105 * The GPIO descriptor associated with the given GPIO, or %NULL if no GPIO
106 * with the given number exists in the system.
Alexandre Courbot372e7222013-02-03 01:29:29 +0900107 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700108struct gpio_desc *gpio_to_desc(unsigned gpio)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900109{
Linus Walleijff2b1352015-10-20 11:10:38 +0200110 struct gpio_device *gdev;
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900111 unsigned long flags;
112
113 spin_lock_irqsave(&gpio_lock, flags);
114
Linus Walleijff2b1352015-10-20 11:10:38 +0200115 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100116 if (gdev->base <= gpio &&
117 gdev->base + gdev->ngpio > gpio) {
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900118 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100119 return &gdev->descs[gpio - gdev->base];
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900120 }
121 }
122
123 spin_unlock_irqrestore(&gpio_lock, flags);
124
Alexandre Courbot0e9a5ed2014-12-02 23:15:05 +0900125 if (!gpio_is_valid(gpio))
126 WARN(1, "invalid GPIO %d\n", gpio);
127
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900128 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900129}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700130EXPORT_SYMBOL_GPL(gpio_to_desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900131
132/**
Thierry Reding950d55f52017-07-24 16:57:22 +0200133 * gpiochip_get_desc - get the GPIO descriptor corresponding to the given
134 * hardware number for this chip
135 * @chip: GPIO chip
136 * @hwnum: hardware number of the GPIO for this chip
137 *
138 * Returns:
139 * A pointer to the GPIO descriptor or %ERR_PTR(-EINVAL) if no GPIO exists
140 * in the given chip for the specified hardware number.
Linus Walleijd468bf92013-09-24 11:54:38 +0200141 */
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900142struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
143 u16 hwnum)
Linus Walleijd468bf92013-09-24 11:54:38 +0200144{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100145 struct gpio_device *gdev = chip->gpiodev;
146
147 if (hwnum >= gdev->ngpio)
Alexandre Courbotb7d0a282013-12-03 12:31:11 +0900148 return ERR_PTR(-EINVAL);
Linus Walleijd468bf92013-09-24 11:54:38 +0200149
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100150 return &gdev->descs[hwnum];
Linus Walleijd468bf92013-09-24 11:54:38 +0200151}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900152
153/**
Thierry Reding950d55f52017-07-24 16:57:22 +0200154 * desc_to_gpio - convert a GPIO descriptor to the integer namespace
155 * @desc: GPIO descriptor
156 *
Alexandre Courbot372e7222013-02-03 01:29:29 +0900157 * This should disappear in the future but is needed since we still
Thierry Reding950d55f52017-07-24 16:57:22 +0200158 * use GPIO numbers for error messages and sysfs nodes.
159 *
160 * Returns:
161 * The global GPIO number for the GPIO specified by its descriptor.
Alexandre Courbot372e7222013-02-03 01:29:29 +0900162 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700163int desc_to_gpio(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900164{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100165 return desc->gdev->base + (desc - &desc->gdev->descs[0]);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900166}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700167EXPORT_SYMBOL_GPL(desc_to_gpio);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900168
169
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700170/**
171 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
172 * @desc: descriptor to return the chip of
173 */
174struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900175{
Vladimir Zapolskiydd3b9a42017-12-21 18:37:30 +0200176 if (!desc || !desc->gdev)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100177 return NULL;
178 return desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900179}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700180EXPORT_SYMBOL_GPL(gpiod_to_chip);
David Brownelld2876d02008-02-04 22:28:20 -0800181
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700182/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
183static int gpiochip_find_base(int ngpio)
184{
Linus Walleijff2b1352015-10-20 11:10:38 +0200185 struct gpio_device *gdev;
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900186 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700187
Linus Walleijff2b1352015-10-20 11:10:38 +0200188 list_for_each_entry_reverse(gdev, &gpio_devices, list) {
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900189 /* found a free space? */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100190 if (gdev->base + gdev->ngpio <= base)
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900191 break;
192 else
193 /* nope, check the space right before the chip */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100194 base = gdev->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700195 }
196
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900197 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700198 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900199 return base;
200 } else {
201 pr_err("%s: cannot find free range\n", __func__);
202 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700203 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700204}
205
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700206/**
207 * gpiod_get_direction - return the current direction of a GPIO
208 * @desc: GPIO to get the direction of
209 *
Wolfram Sang94fc7302018-01-09 12:35:53 +0100210 * Returns 0 for output, 1 for input, or an error code in case of error.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700211 *
212 * This function may sleep if gpiod_cansleep() is true.
213 */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900214int gpiod_get_direction(struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300215{
Wolfram Sangd0121b82018-07-11 18:33:19 +0200216 struct gpio_chip *chip;
217 unsigned offset;
Linus Walleijd377f562019-07-16 11:11:45 +0200218 int ret;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300219
Alexandre Courbot372e7222013-02-03 01:29:29 +0900220 chip = gpiod_to_chip(desc);
221 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300222
223 if (!chip->get_direction)
Wolfram Sangd0121b82018-07-11 18:33:19 +0200224 return -ENOTSUPP;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300225
Linus Walleijd377f562019-07-16 11:11:45 +0200226 ret = chip->get_direction(chip, offset);
227 if (ret > 0) {
Mathias Nyman80b0a602012-10-24 17:25:27 +0300228 /* GPIOF_DIR_IN, or other positive */
Linus Walleijd377f562019-07-16 11:11:45 +0200229 ret = 1;
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900230 clear_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300231 }
Linus Walleijd377f562019-07-16 11:11:45 +0200232 if (ret == 0) {
Mathias Nyman80b0a602012-10-24 17:25:27 +0300233 /* GPIOF_DIR_OUT */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900234 set_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300235 }
Linus Walleijd377f562019-07-16 11:11:45 +0200236 return ret;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300237}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700238EXPORT_SYMBOL_GPL(gpiod_get_direction);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300239
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900240/*
241 * Add a new chip to the global chips list, keeping the list of chips sorted
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800242 * by range(means [base, base + ngpio - 1]) order.
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900243 *
244 * Return -EBUSY if the new chip overlaps with some other chip's integer
245 * space.
246 */
Linus Walleijff2b1352015-10-20 11:10:38 +0200247static int gpiodev_add_to_list(struct gpio_device *gdev)
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900248{
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800249 struct gpio_device *prev, *next;
Linus Walleijff2b1352015-10-20 11:10:38 +0200250
251 if (list_empty(&gpio_devices)) {
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800252 /* initial entry in list */
Linus Walleijff2b1352015-10-20 11:10:38 +0200253 list_add_tail(&gdev->list, &gpio_devices);
Sudip Mukherjeee28ecca2015-12-27 19:06:50 +0530254 return 0;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900255 }
256
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800257 next = list_entry(gpio_devices.next, struct gpio_device, list);
258 if (gdev->base + gdev->ngpio <= next->base) {
259 /* add before first entry */
260 list_add(&gdev->list, &gpio_devices);
Julien Grossholtz96098df2016-01-07 16:46:45 -0500261 return 0;
262 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900263
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800264 prev = list_entry(gpio_devices.prev, struct gpio_device, list);
265 if (prev->base + prev->ngpio <= gdev->base) {
266 /* add behind last entry */
267 list_add_tail(&gdev->list, &gpio_devices);
268 return 0;
269 }
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800270
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800271 list_for_each_entry_safe(prev, next, &gpio_devices, list) {
272 /* at the end of the list */
273 if (&next->list == &gpio_devices)
274 break;
275
276 /* add between prev and next */
277 if (prev->base + prev->ngpio <= gdev->base
278 && gdev->base + gdev->ngpio <= next->base) {
279 list_add(&gdev->list, &prev->list);
280 return 0;
281 }
282 }
283
284 dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n");
285 return -EBUSY;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900286}
287
Thierry Reding950d55f52017-07-24 16:57:22 +0200288/*
Linus Walleijf881bab02015-09-23 16:20:43 -0700289 * Convert a GPIO name to its descriptor
290 */
291static struct gpio_desc *gpio_name_to_desc(const char * const name)
292{
Linus Walleijff2b1352015-10-20 11:10:38 +0200293 struct gpio_device *gdev;
Linus Walleijf881bab02015-09-23 16:20:43 -0700294 unsigned long flags;
295
296 spin_lock_irqsave(&gpio_lock, flags);
297
Linus Walleijff2b1352015-10-20 11:10:38 +0200298 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijf881bab02015-09-23 16:20:43 -0700299 int i;
300
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100301 for (i = 0; i != gdev->ngpio; ++i) {
302 struct gpio_desc *desc = &gdev->descs[i];
Linus Walleijf881bab02015-09-23 16:20:43 -0700303
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100304 if (!desc->name || !name)
Linus Walleijf881bab02015-09-23 16:20:43 -0700305 continue;
306
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100307 if (!strcmp(desc->name, name)) {
Linus Walleijf881bab02015-09-23 16:20:43 -0700308 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100309 return desc;
Linus Walleijf881bab02015-09-23 16:20:43 -0700310 }
311 }
312 }
313
314 spin_unlock_irqrestore(&gpio_lock, flags);
315
316 return NULL;
317}
318
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200319/*
320 * Takes the names from gc->names and checks if they are all unique. If they
321 * are, they are assigned to their gpio descriptors.
322 *
Bamvor Jian Zhanged379152015-11-14 16:43:20 +0800323 * Warning if one of the names is already used for a different GPIO.
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200324 */
325static int gpiochip_set_desc_names(struct gpio_chip *gc)
326{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100327 struct gpio_device *gdev = gc->gpiodev;
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200328 int i;
329
330 if (!gc->names)
331 return 0;
332
333 /* First check all names if they are unique */
334 for (i = 0; i != gc->ngpio; ++i) {
335 struct gpio_desc *gpio;
336
337 gpio = gpio_name_to_desc(gc->names[i]);
Linus Walleijf881bab02015-09-23 16:20:43 -0700338 if (gpio)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100339 dev_warn(&gdev->dev,
Linus Walleij34ffd852015-10-20 11:31:54 +0200340 "Detected name collision for GPIO name '%s'\n",
Linus Walleijf881bab02015-09-23 16:20:43 -0700341 gc->names[i]);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200342 }
343
344 /* Then add all names to the GPIO descriptors */
345 for (i = 0; i != gc->ngpio; ++i)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100346 gdev->descs[i].name = gc->names[i];
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200347
348 return 0;
349}
350
Stephen Boyde4371f6e2018-03-23 09:34:50 -0700351static unsigned long *gpiochip_allocate_mask(struct gpio_chip *chip)
352{
353 unsigned long *p;
354
Masahiro Yamada7bdbd1ec2019-07-18 15:51:01 +0900355 p = bitmap_alloc(chip->ngpio, GFP_KERNEL);
Stephen Boyde4371f6e2018-03-23 09:34:50 -0700356 if (!p)
357 return NULL;
358
359 /* Assume by default all GPIOs are valid */
360 bitmap_fill(p, chip->ngpio);
361
362 return p;
363}
364
Linus Walleijf626d6d2019-07-17 09:10:01 +0200365static int gpiochip_alloc_valid_mask(struct gpio_chip *gc)
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700366{
Linus Walleijeb1e8bd2019-08-19 11:30:58 +0200367 if (!(of_gpio_need_valid_mask(gc) || gc->init_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
Linus Walleijc9fc5af2019-08-19 10:49:04 +0200377static int gpiochip_init_valid_mask(struct gpio_chip *gc)
Ricardo Ribalda Delgadof8ec92a2018-10-05 08:52:58 +0200378{
Linus Walleijc9fc5af2019-08-19 10:49:04 +0200379 if (gc->init_valid_mask)
380 return gc->init_valid_mask(gc,
381 gc->valid_mask,
382 gc->ngpio);
Ricardo Ribalda Delgadof8ec92a2018-10-05 08:52:58 +0200383
384 return 0;
385}
386
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700387static void gpiochip_free_valid_mask(struct gpio_chip *gpiochip)
388{
Masahiro Yamada7bdbd1ec2019-07-18 15:51:01 +0900389 bitmap_free(gpiochip->valid_mask);
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700390 gpiochip->valid_mask = NULL;
391}
392
393bool gpiochip_line_is_valid(const struct gpio_chip *gpiochip,
394 unsigned int offset)
395{
396 /* No mask means all valid */
397 if (likely(!gpiochip->valid_mask))
398 return true;
399 return test_bit(offset, gpiochip->valid_mask);
400}
401EXPORT_SYMBOL_GPL(gpiochip_line_is_valid);
402
Linus Walleijd7c51b42016-04-26 10:35:29 +0200403/*
404 * GPIO line handle management
405 */
406
407/**
408 * struct linehandle_state - contains the state of a userspace handle
409 * @gdev: the GPIO device the handle pertains to
410 * @label: consumer label used to tag descriptors
411 * @descs: the GPIO descriptors held by this handle
412 * @numdescs: the number of descriptors held in the descs array
413 */
414struct linehandle_state {
415 struct gpio_device *gdev;
416 const char *label;
417 struct gpio_desc *descs[GPIOHANDLES_MAX];
418 u32 numdescs;
419};
420
Lars-Peter Clausene3e847c2016-10-18 16:54:05 +0200421#define GPIOHANDLE_REQUEST_VALID_FLAGS \
422 (GPIOHANDLE_REQUEST_INPUT | \
423 GPIOHANDLE_REQUEST_OUTPUT | \
424 GPIOHANDLE_REQUEST_ACTIVE_LOW | \
Drew Fustini9225d512019-11-05 10:04:23 +0800425 GPIOHANDLE_REQUEST_BIAS_PULL_UP | \
426 GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | \
Kent Gibson2148ad72019-11-05 10:04:25 +0800427 GPIOHANDLE_REQUEST_BIAS_DISABLE | \
Lars-Peter Clausene3e847c2016-10-18 16:54:05 +0200428 GPIOHANDLE_REQUEST_OPEN_DRAIN | \
429 GPIOHANDLE_REQUEST_OPEN_SOURCE)
430
Kent Gibsonb043ed72019-11-05 10:04:28 +0800431static int linehandle_validate_flags(u32 flags)
432{
433 /* Return an error if an unknown flag is set */
434 if (flags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
435 return -EINVAL;
436
437 /*
438 * Do not allow both INPUT & OUTPUT flags to be set as they are
439 * contradictory.
440 */
441 if ((flags & GPIOHANDLE_REQUEST_INPUT) &&
442 (flags & GPIOHANDLE_REQUEST_OUTPUT))
443 return -EINVAL;
444
445 /*
446 * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
447 * the hardware actually supports enabling both at the same time the
448 * electrical result would be disastrous.
449 */
450 if ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) &&
451 (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
452 return -EINVAL;
453
454 /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
455 if (!(flags & GPIOHANDLE_REQUEST_OUTPUT) &&
456 ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
457 (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
458 return -EINVAL;
459
460 /* Bias flags only allowed for input or output mode. */
461 if (!((flags & GPIOHANDLE_REQUEST_INPUT) ||
462 (flags & GPIOHANDLE_REQUEST_OUTPUT)) &&
463 ((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) ||
464 (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP) ||
465 (flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN)))
466 return -EINVAL;
467
468 /* Only one bias flag can be set. */
469 if (((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
470 (flags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
471 GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
472 ((flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
473 (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
474 return -EINVAL;
475
476 return 0;
477}
478
Linus Walleijd7c51b42016-04-26 10:35:29 +0200479static long linehandle_ioctl(struct file *filep, unsigned int cmd,
480 unsigned long arg)
481{
482 struct linehandle_state *lh = filep->private_data;
483 void __user *ip = (void __user *)arg;
484 struct gpiohandle_data ghd;
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +0200485 DECLARE_BITMAP(vals, GPIOHANDLES_MAX);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200486 int i;
487
488 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
Bartosz Golaszewski2b955b32018-07-16 10:34:24 +0200489 /* NOTE: It's ok to read values of output lines. */
Lukas Wunnereec1d562017-10-12 12:40:10 +0200490 int ret = gpiod_get_array_value_complex(false,
491 true,
492 lh->numdescs,
493 lh->descs,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +0200494 NULL,
Lukas Wunnereec1d562017-10-12 12:40:10 +0200495 vals);
496 if (ret)
497 return ret;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200498
Lars-Peter Clausen3eded5d2016-10-18 16:54:02 +0200499 memset(&ghd, 0, sizeof(ghd));
Lukas Wunnereec1d562017-10-12 12:40:10 +0200500 for (i = 0; i < lh->numdescs; i++)
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +0200501 ghd.values[i] = test_bit(i, vals);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200502
503 if (copy_to_user(ip, &ghd, sizeof(ghd)))
504 return -EFAULT;
505
506 return 0;
507 } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
Bartosz Golaszewskie5332d52018-07-16 10:34:23 +0200508 /*
509 * All line descriptors were created at once with the same
510 * flags so just check if the first one is really output.
511 */
512 if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags))
513 return -EPERM;
514
Linus Walleijd7c51b42016-04-26 10:35:29 +0200515 if (copy_from_user(&ghd, ip, sizeof(ghd)))
516 return -EFAULT;
517
518 /* Clamp all values to [0,1] */
519 for (i = 0; i < lh->numdescs; i++)
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +0200520 __assign_bit(i, vals, ghd.values[i]);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200521
522 /* Reuse the array setting function */
Laura Abbott30277432018-05-21 10:57:07 -0700523 return gpiod_set_array_value_complex(false,
Linus Walleijd7c51b42016-04-26 10:35:29 +0200524 true,
525 lh->numdescs,
526 lh->descs,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +0200527 NULL,
Linus Walleijd7c51b42016-04-26 10:35:29 +0200528 vals);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200529 }
530 return -EINVAL;
531}
532
533#ifdef CONFIG_COMPAT
534static long linehandle_ioctl_compat(struct file *filep, unsigned int cmd,
535 unsigned long arg)
536{
537 return linehandle_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
538}
539#endif
540
541static int linehandle_release(struct inode *inode, struct file *filep)
542{
543 struct linehandle_state *lh = filep->private_data;
544 struct gpio_device *gdev = lh->gdev;
545 int i;
546
547 for (i = 0; i < lh->numdescs; i++)
548 gpiod_free(lh->descs[i]);
549 kfree(lh->label);
550 kfree(lh);
551 put_device(&gdev->dev);
552 return 0;
553}
554
555static const struct file_operations linehandle_fileops = {
556 .release = linehandle_release,
557 .owner = THIS_MODULE,
558 .llseek = noop_llseek,
559 .unlocked_ioctl = linehandle_ioctl,
560#ifdef CONFIG_COMPAT
561 .compat_ioctl = linehandle_ioctl_compat,
562#endif
563};
564
565static int linehandle_create(struct gpio_device *gdev, void __user *ip)
566{
567 struct gpiohandle_request handlereq;
568 struct linehandle_state *lh;
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200569 struct file *file;
Timur Tabiab3dbcf2018-03-29 13:29:12 -0500570 int fd, i, count = 0, ret;
Bartosz Golaszewski418ee8e2017-10-16 11:32:29 +0200571 u32 lflags;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200572
573 if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
574 return -EFAULT;
575 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
576 return -EINVAL;
577
Bartosz Golaszewski418ee8e2017-10-16 11:32:29 +0200578 lflags = handlereq.flags;
579
Kent Gibsonb043ed72019-11-05 10:04:28 +0800580 ret = linehandle_validate_flags(lflags);
581 if (ret)
582 return ret;
Kent Gibson2148ad72019-11-05 10:04:25 +0800583
Linus Walleijd7c51b42016-04-26 10:35:29 +0200584 lh = kzalloc(sizeof(*lh), GFP_KERNEL);
585 if (!lh)
586 return -ENOMEM;
587 lh->gdev = gdev;
588 get_device(&gdev->dev);
589
590 /* Make sure this is terminated */
591 handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
592 if (strlen(handlereq.consumer_label)) {
593 lh->label = kstrdup(handlereq.consumer_label,
594 GFP_KERNEL);
595 if (!lh->label) {
596 ret = -ENOMEM;
597 goto out_free_lh;
598 }
599 }
600
601 /* Request each GPIO */
602 for (i = 0; i < handlereq.lines; i++) {
603 u32 offset = handlereq.lineoffsets[i];
Linus Walleijd7c51b42016-04-26 10:35:29 +0200604 struct gpio_desc *desc;
605
Lars-Peter Clausene405f9f2016-10-18 16:54:01 +0200606 if (offset >= gdev->ngpio) {
607 ret = -EINVAL;
608 goto out_free_descs;
609 }
610
Linus Walleijd7c51b42016-04-26 10:35:29 +0200611 desc = &gdev->descs[offset];
612 ret = gpiod_request(desc, lh->label);
613 if (ret)
614 goto out_free_descs;
615 lh->descs[i] = desc;
Ricardo Ribalda Delgado19a4fbf2018-09-13 15:37:04 +0200616 count = i + 1;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200617
618 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
619 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
620 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
621 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
622 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
623 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Kent Gibson2148ad72019-11-05 10:04:25 +0800624 if (lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE)
625 set_bit(FLAG_BIAS_DISABLE, &desc->flags);
Drew Fustini9225d512019-11-05 10:04:23 +0800626 if (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN)
627 set_bit(FLAG_PULL_DOWN, &desc->flags);
628 if (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)
629 set_bit(FLAG_PULL_UP, &desc->flags);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200630
Andrew Jefferye10f72b2017-11-30 14:25:24 +1030631 ret = gpiod_set_transitory(desc, false);
632 if (ret < 0)
633 goto out_free_descs;
634
Linus Walleijd7c51b42016-04-26 10:35:29 +0200635 /*
636 * Lines have to be requested explicitly for input
637 * or output, else the line will be treated "as is".
638 */
639 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
640 int val = !!handlereq.default_values[i];
641
642 ret = gpiod_direction_output(desc, val);
643 if (ret)
644 goto out_free_descs;
645 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
646 ret = gpiod_direction_input(desc);
647 if (ret)
648 goto out_free_descs;
649 }
650 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
651 offset);
652 }
Linus Walleije2f608b2016-06-18 10:56:43 +0200653 /* Let i point at the last handle */
654 i--;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200655 lh->numdescs = handlereq.lines;
656
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200657 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200658 if (fd < 0) {
659 ret = fd;
660 goto out_free_descs;
661 }
662
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200663 file = anon_inode_getfile("gpio-linehandle",
664 &linehandle_fileops,
665 lh,
666 O_RDONLY | O_CLOEXEC);
667 if (IS_ERR(file)) {
668 ret = PTR_ERR(file);
669 goto out_put_unused_fd;
670 }
671
Linus Walleijd7c51b42016-04-26 10:35:29 +0200672 handlereq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200673 if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200674 /*
675 * fput() will trigger the release() callback, so do not go onto
676 * the regular error cleanup path here.
677 */
678 fput(file);
679 put_unused_fd(fd);
680 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +0200681 }
Linus Walleijd7c51b42016-04-26 10:35:29 +0200682
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200683 fd_install(fd, file);
684
Linus Walleijd7c51b42016-04-26 10:35:29 +0200685 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
686 lh->numdescs);
687
688 return 0;
689
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200690out_put_unused_fd:
691 put_unused_fd(fd);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200692out_free_descs:
Timur Tabiab3dbcf2018-03-29 13:29:12 -0500693 for (i = 0; i < count; i++)
Linus Walleijd7c51b42016-04-26 10:35:29 +0200694 gpiod_free(lh->descs[i]);
695 kfree(lh->label);
696out_free_lh:
697 kfree(lh);
698 put_device(&gdev->dev);
699 return ret;
700}
701
Linus Walleij61f922d2016-06-02 11:30:15 +0200702/*
703 * GPIO line event management
704 */
705
706/**
707 * struct lineevent_state - contains the state of a userspace event
708 * @gdev: the GPIO device the event pertains to
709 * @label: consumer label used to tag descriptors
710 * @desc: the GPIO descriptor held by this event
711 * @eflags: the event flags this line was requested with
712 * @irq: the interrupt that trigger in response to events on this GPIO
713 * @wait: wait queue that handles blocking reads of events
714 * @events: KFIFO for the GPIO events
715 * @read_lock: mutex lock to protect reads from colliding with adding
716 * new events to the FIFO
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100717 * @timestamp: cache for the timestamp storing it between hardirq
718 * and IRQ thread, used to bring the timestamp close to the actual
719 * event
Linus Walleij61f922d2016-06-02 11:30:15 +0200720 */
721struct lineevent_state {
722 struct gpio_device *gdev;
723 const char *label;
724 struct gpio_desc *desc;
725 u32 eflags;
726 int irq;
727 wait_queue_head_t wait;
728 DECLARE_KFIFO(events, struct gpioevent_data, 16);
729 struct mutex read_lock;
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100730 u64 timestamp;
Linus Walleij61f922d2016-06-02 11:30:15 +0200731};
732
Lars-Peter Clausenac7dbb92016-10-18 16:54:06 +0200733#define GPIOEVENT_REQUEST_VALID_FLAGS \
734 (GPIOEVENT_REQUEST_RISING_EDGE | \
735 GPIOEVENT_REQUEST_FALLING_EDGE)
736
Al Viroafc9a422017-07-03 06:39:46 -0400737static __poll_t lineevent_poll(struct file *filep,
Linus Walleij61f922d2016-06-02 11:30:15 +0200738 struct poll_table_struct *wait)
739{
740 struct lineevent_state *le = filep->private_data;
Al Viroafc9a422017-07-03 06:39:46 -0400741 __poll_t events = 0;
Linus Walleij61f922d2016-06-02 11:30:15 +0200742
743 poll_wait(filep, &le->wait, wait);
744
745 if (!kfifo_is_empty(&le->events))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800746 events = EPOLLIN | EPOLLRDNORM;
Linus Walleij61f922d2016-06-02 11:30:15 +0200747
748 return events;
749}
750
751
752static ssize_t lineevent_read(struct file *filep,
753 char __user *buf,
754 size_t count,
755 loff_t *f_ps)
756{
757 struct lineevent_state *le = filep->private_data;
758 unsigned int copied;
759 int ret;
760
761 if (count < sizeof(struct gpioevent_data))
762 return -EINVAL;
763
764 do {
765 if (kfifo_is_empty(&le->events)) {
766 if (filep->f_flags & O_NONBLOCK)
767 return -EAGAIN;
768
769 ret = wait_event_interruptible(le->wait,
770 !kfifo_is_empty(&le->events));
771 if (ret)
772 return ret;
773 }
774
775 if (mutex_lock_interruptible(&le->read_lock))
776 return -ERESTARTSYS;
777 ret = kfifo_to_user(&le->events, buf, count, &copied);
778 mutex_unlock(&le->read_lock);
779
780 if (ret)
781 return ret;
782
783 /*
784 * If we couldn't read anything from the fifo (a different
785 * thread might have been faster) we either return -EAGAIN if
786 * the file descriptor is non-blocking, otherwise we go back to
787 * sleep and wait for more data to arrive.
788 */
789 if (copied == 0 && (filep->f_flags & O_NONBLOCK))
790 return -EAGAIN;
791
792 } while (copied == 0);
793
794 return copied;
795}
796
797static int lineevent_release(struct inode *inode, struct file *filep)
798{
799 struct lineevent_state *le = filep->private_data;
800 struct gpio_device *gdev = le->gdev;
801
802 free_irq(le->irq, le);
803 gpiod_free(le->desc);
804 kfree(le->label);
805 kfree(le);
806 put_device(&gdev->dev);
807 return 0;
808}
809
810static long lineevent_ioctl(struct file *filep, unsigned int cmd,
811 unsigned long arg)
812{
813 struct lineevent_state *le = filep->private_data;
814 void __user *ip = (void __user *)arg;
815 struct gpiohandle_data ghd;
816
817 /*
818 * We can get the value for an event line but not set it,
819 * because it is input by definition.
820 */
821 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
822 int val;
823
Lars-Peter Clausend82aa4a2016-10-18 16:54:04 +0200824 memset(&ghd, 0, sizeof(ghd));
825
Linus Walleij61f922d2016-06-02 11:30:15 +0200826 val = gpiod_get_value_cansleep(le->desc);
827 if (val < 0)
828 return val;
829 ghd.values[0] = val;
830
831 if (copy_to_user(ip, &ghd, sizeof(ghd)))
832 return -EFAULT;
833
834 return 0;
835 }
836 return -EINVAL;
837}
838
839#ifdef CONFIG_COMPAT
840static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd,
841 unsigned long arg)
842{
843 return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
844}
845#endif
846
847static const struct file_operations lineevent_fileops = {
848 .release = lineevent_release,
849 .read = lineevent_read,
850 .poll = lineevent_poll,
851 .owner = THIS_MODULE,
852 .llseek = noop_llseek,
853 .unlocked_ioctl = lineevent_ioctl,
854#ifdef CONFIG_COMPAT
855 .compat_ioctl = lineevent_ioctl_compat,
856#endif
857};
858
Ben Dooks33265b12016-06-17 16:03:13 +0100859static irqreturn_t lineevent_irq_thread(int irq, void *p)
Linus Walleij61f922d2016-06-02 11:30:15 +0200860{
861 struct lineevent_state *le = p;
862 struct gpioevent_data ge;
Uwe Kleine-Königfa388692018-08-20 08:32:53 +0200863 int ret;
Linus Walleij61f922d2016-06-02 11:30:15 +0200864
Linus Walleij24bd3ef2018-01-22 13:19:28 +0100865 /* Do not leak kernel stack to userspace */
866 memset(&ge, 0, sizeof(ge));
867
Bartosz Golaszewski1033be52019-01-04 11:24:20 +0100868 /*
869 * We may be running from a nested threaded interrupt in which case
870 * we didn't get the timestamp from lineevent_irq_handler().
871 */
872 if (!le->timestamp)
873 ge.timestamp = ktime_get_real_ns();
874 else
875 ge.timestamp = le->timestamp;
Linus Walleij61f922d2016-06-02 11:30:15 +0200876
Bartosz Golaszewskiad537b82017-06-23 13:45:16 +0200877 if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
878 && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
Uwe Kleine-Königfa388692018-08-20 08:32:53 +0200879 int level = gpiod_get_value_cansleep(le->desc);
Linus Walleij61f922d2016-06-02 11:30:15 +0200880 if (level)
881 /* Emit low-to-high event */
882 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
883 else
884 /* Emit high-to-low event */
885 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Uwe Kleine-Königfa388692018-08-20 08:32:53 +0200886 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200887 /* Emit low-to-high event */
888 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
Uwe Kleine-Königfa388692018-08-20 08:32:53 +0200889 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200890 /* Emit high-to-low event */
891 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Arnd Bergmannbc0207a2016-06-16 11:02:41 +0200892 } else {
893 return IRQ_NONE;
Linus Walleij61f922d2016-06-02 11:30:15 +0200894 }
895
896 ret = kfifo_put(&le->events, ge);
Saiyam Doshi2efc6bf2019-09-07 23:09:10 +0530897 if (ret)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800898 wake_up_poll(&le->wait, EPOLLIN);
Linus Walleij61f922d2016-06-02 11:30:15 +0200899
900 return IRQ_HANDLED;
901}
902
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100903static irqreturn_t lineevent_irq_handler(int irq, void *p)
904{
905 struct lineevent_state *le = p;
906
907 /*
908 * Just store the timestamp in hardirq context so we get it as
909 * close in time as possible to the actual event.
910 */
911 le->timestamp = ktime_get_real_ns();
912
913 return IRQ_WAKE_THREAD;
914}
915
Linus Walleij61f922d2016-06-02 11:30:15 +0200916static int lineevent_create(struct gpio_device *gdev, void __user *ip)
917{
918 struct gpioevent_request eventreq;
919 struct lineevent_state *le;
920 struct gpio_desc *desc;
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200921 struct file *file;
Linus Walleij61f922d2016-06-02 11:30:15 +0200922 u32 offset;
923 u32 lflags;
924 u32 eflags;
925 int fd;
926 int ret;
927 int irqflags = 0;
928
929 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
930 return -EFAULT;
931
Bartosz Golaszewskibcc6d992019-09-16 11:46:23 +0200932 offset = eventreq.lineoffset;
933 lflags = eventreq.handleflags;
934 eflags = eventreq.eventflags;
935
936 if (offset >= gdev->ngpio)
937 return -EINVAL;
938
939 /* Return an error if a unknown flag is set */
940 if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
941 (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS))
942 return -EINVAL;
943
944 /* This is just wrong: we don't look for events on output lines */
945 if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) ||
946 (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
947 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
948 return -EINVAL;
949
Kent Gibson2148ad72019-11-05 10:04:25 +0800950 /* Only one bias flag can be set. */
951 if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
952 (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
953 GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
954 ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
955 (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
956 return -EINVAL;
957
Linus Walleij61f922d2016-06-02 11:30:15 +0200958 le = kzalloc(sizeof(*le), GFP_KERNEL);
959 if (!le)
960 return -ENOMEM;
961 le->gdev = gdev;
962 get_device(&gdev->dev);
963
964 /* Make sure this is terminated */
965 eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
966 if (strlen(eventreq.consumer_label)) {
967 le->label = kstrdup(eventreq.consumer_label,
968 GFP_KERNEL);
969 if (!le->label) {
970 ret = -ENOMEM;
971 goto out_free_le;
972 }
973 }
974
Linus Walleij61f922d2016-06-02 11:30:15 +0200975 desc = &gdev->descs[offset];
976 ret = gpiod_request(desc, le->label);
977 if (ret)
Uwe Kleine-Königf001cc32018-04-16 13:17:53 +0200978 goto out_free_label;
Linus Walleij61f922d2016-06-02 11:30:15 +0200979 le->desc = desc;
980 le->eflags = eflags;
981
982 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
983 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
Kent Gibson2148ad72019-11-05 10:04:25 +0800984 if (lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE)
985 set_bit(FLAG_BIAS_DISABLE, &desc->flags);
Kent Gibson7b479a82019-11-05 10:04:24 +0800986 if (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN)
987 set_bit(FLAG_PULL_DOWN, &desc->flags);
988 if (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)
989 set_bit(FLAG_PULL_UP, &desc->flags);
Linus Walleij61f922d2016-06-02 11:30:15 +0200990
991 ret = gpiod_direction_input(desc);
992 if (ret)
993 goto out_free_desc;
994
995 le->irq = gpiod_to_irq(desc);
996 if (le->irq <= 0) {
997 ret = -ENODEV;
998 goto out_free_desc;
999 }
1000
1001 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
Michael Wu223ecaf2019-07-08 13:23:08 +08001002 irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
1003 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
Linus Walleij61f922d2016-06-02 11:30:15 +02001004 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
Michael Wu223ecaf2019-07-08 13:23:08 +08001005 irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
1006 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
Linus Walleij61f922d2016-06-02 11:30:15 +02001007 irqflags |= IRQF_ONESHOT;
Linus Walleij61f922d2016-06-02 11:30:15 +02001008
1009 INIT_KFIFO(le->events);
1010 init_waitqueue_head(&le->wait);
1011 mutex_init(&le->read_lock);
1012
1013 /* Request a thread to read the events */
1014 ret = request_threaded_irq(le->irq,
Linus Walleijd58f2bf2017-11-30 10:23:27 +01001015 lineevent_irq_handler,
Linus Walleij61f922d2016-06-02 11:30:15 +02001016 lineevent_irq_thread,
1017 irqflags,
1018 le->label,
1019 le);
1020 if (ret)
1021 goto out_free_desc;
1022
Lars-Peter Clausen953b9562016-10-24 13:59:15 +02001023 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleij61f922d2016-06-02 11:30:15 +02001024 if (fd < 0) {
1025 ret = fd;
1026 goto out_free_irq;
1027 }
1028
Lars-Peter Clausen953b9562016-10-24 13:59:15 +02001029 file = anon_inode_getfile("gpio-event",
1030 &lineevent_fileops,
1031 le,
1032 O_RDONLY | O_CLOEXEC);
1033 if (IS_ERR(file)) {
1034 ret = PTR_ERR(file);
1035 goto out_put_unused_fd;
1036 }
1037
Linus Walleij61f922d2016-06-02 11:30:15 +02001038 eventreq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +02001039 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +02001040 /*
1041 * fput() will trigger the release() callback, so do not go onto
1042 * the regular error cleanup path here.
1043 */
1044 fput(file);
1045 put_unused_fd(fd);
1046 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +02001047 }
Linus Walleij61f922d2016-06-02 11:30:15 +02001048
Lars-Peter Clausen953b9562016-10-24 13:59:15 +02001049 fd_install(fd, file);
1050
Linus Walleij61f922d2016-06-02 11:30:15 +02001051 return 0;
1052
Lars-Peter Clausen953b9562016-10-24 13:59:15 +02001053out_put_unused_fd:
1054 put_unused_fd(fd);
Linus Walleij61f922d2016-06-02 11:30:15 +02001055out_free_irq:
1056 free_irq(le->irq, le);
1057out_free_desc:
1058 gpiod_free(le->desc);
1059out_free_label:
1060 kfree(le->label);
1061out_free_le:
1062 kfree(le);
1063 put_device(&gdev->dev);
1064 return ret;
1065}
1066
Thierry Reding950d55f52017-07-24 16:57:22 +02001067/*
Linus Walleij3c702e92015-10-21 15:29:53 +02001068 * gpio_ioctl() - ioctl handler for the GPIO chardev
1069 */
1070static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1071{
1072 struct gpio_device *gdev = filp->private_data;
1073 struct gpio_chip *chip = gdev->chip;
Linus Walleij8b92e172016-05-27 14:24:04 +02001074 void __user *ip = (void __user *)arg;
Linus Walleij3c702e92015-10-21 15:29:53 +02001075
1076 /* We fail any subsequent ioctl():s when the chip is gone */
1077 if (!chip)
1078 return -ENODEV;
1079
Linus Walleij521a2ad2016-02-12 22:25:22 +01001080 /* Fill in the struct and pass to userspace */
Linus Walleij3c702e92015-10-21 15:29:53 +02001081 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
Linus Walleij521a2ad2016-02-12 22:25:22 +01001082 struct gpiochip_info chipinfo;
1083
Lars-Peter Clausen0f4bbb22016-10-18 16:54:00 +02001084 memset(&chipinfo, 0, sizeof(chipinfo));
1085
Linus Walleij3c702e92015-10-21 15:29:53 +02001086 strncpy(chipinfo.name, dev_name(&gdev->dev),
1087 sizeof(chipinfo.name));
1088 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
Linus Walleijdf4878e2016-02-12 14:48:23 +01001089 strncpy(chipinfo.label, gdev->label,
1090 sizeof(chipinfo.label));
1091 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001092 chipinfo.lines = gdev->ngpio;
Linus Walleij3c702e92015-10-21 15:29:53 +02001093 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
1094 return -EFAULT;
1095 return 0;
Linus Walleij521a2ad2016-02-12 22:25:22 +01001096 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
1097 struct gpioline_info lineinfo;
1098 struct gpio_desc *desc;
1099
1100 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
1101 return -EFAULT;
Lars-Peter Clausen1f1cc452016-10-18 16:53:59 +02001102 if (lineinfo.line_offset >= gdev->ngpio)
Linus Walleij521a2ad2016-02-12 22:25:22 +01001103 return -EINVAL;
1104
1105 desc = &gdev->descs[lineinfo.line_offset];
1106 if (desc->name) {
1107 strncpy(lineinfo.name, desc->name,
1108 sizeof(lineinfo.name));
1109 lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
1110 } else {
1111 lineinfo.name[0] = '\0';
1112 }
1113 if (desc->label) {
Linus Walleij214338e2016-02-25 21:01:48 +01001114 strncpy(lineinfo.consumer, desc->label,
1115 sizeof(lineinfo.consumer));
1116 lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +01001117 } else {
Linus Walleij214338e2016-02-25 21:01:48 +01001118 lineinfo.consumer[0] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +01001119 }
1120
1121 /*
1122 * Userspace only need to know that the kernel is using
1123 * this GPIO so it can't use it.
1124 */
1125 lineinfo.flags = 0;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001126 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
1127 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
1128 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
1129 test_bit(FLAG_EXPORT, &desc->flags) ||
Stefan Wahren472a61e2019-08-14 14:00:35 +03001130 test_bit(FLAG_SYSFS, &desc->flags) ||
1131 !pinctrl_gpio_can_use_line(chip->base + lineinfo.line_offset))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001132 lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001133 if (test_bit(FLAG_IS_OUT, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001134 lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001135 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001136 lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001137 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Bartosz Golaszewski2c60e6b2019-08-06 13:41:51 +02001138 lineinfo.flags |= (GPIOLINE_FLAG_OPEN_DRAIN |
1139 GPIOLINE_FLAG_IS_OUT);
Linus Walleij9d8cc892016-02-22 13:44:53 +01001140 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Bartosz Golaszewski2c60e6b2019-08-06 13:41:51 +02001141 lineinfo.flags |= (GPIOLINE_FLAG_OPEN_SOURCE |
1142 GPIOLINE_FLAG_IS_OUT);
Kent Gibson2148ad72019-11-05 10:04:25 +08001143 if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
1144 lineinfo.flags |= GPIOLINE_FLAG_BIAS_DISABLE;
Drew Fustini9225d512019-11-05 10:04:23 +08001145 if (test_bit(FLAG_PULL_DOWN, &desc->flags))
1146 lineinfo.flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN;
1147 if (test_bit(FLAG_PULL_UP, &desc->flags))
1148 lineinfo.flags |= GPIOLINE_FLAG_BIAS_PULL_UP;
Linus Walleij521a2ad2016-02-12 22:25:22 +01001149
1150 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
1151 return -EFAULT;
1152 return 0;
Linus Walleijd7c51b42016-04-26 10:35:29 +02001153 } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
1154 return linehandle_create(gdev, ip);
Linus Walleij61f922d2016-06-02 11:30:15 +02001155 } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
1156 return lineevent_create(gdev, ip);
Linus Walleij3c702e92015-10-21 15:29:53 +02001157 }
1158 return -EINVAL;
1159}
1160
Linus Walleij8b92e172016-05-27 14:24:04 +02001161#ifdef CONFIG_COMPAT
1162static long gpio_ioctl_compat(struct file *filp, unsigned int cmd,
1163 unsigned long arg)
1164{
1165 return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
1166}
1167#endif
1168
Linus Walleij3c702e92015-10-21 15:29:53 +02001169/**
1170 * gpio_chrdev_open() - open the chardev for ioctl operations
1171 * @inode: inode for this chardev
1172 * @filp: file struct for storing private data
1173 * Returns 0 on success
1174 */
1175static int gpio_chrdev_open(struct inode *inode, struct file *filp)
1176{
1177 struct gpio_device *gdev = container_of(inode->i_cdev,
1178 struct gpio_device, chrdev);
1179
1180 /* Fail on open if the backing gpiochip is gone */
Stephen Boydfb505742017-01-09 11:47:44 -08001181 if (!gdev->chip)
Linus Walleij3c702e92015-10-21 15:29:53 +02001182 return -ENODEV;
1183 get_device(&gdev->dev);
1184 filp->private_data = gdev;
Lars-Peter Clausenf4e81c52016-11-30 13:05:21 +01001185
1186 return nonseekable_open(inode, filp);
Linus Walleij3c702e92015-10-21 15:29:53 +02001187}
1188
1189/**
1190 * gpio_chrdev_release() - close chardev after ioctl operations
1191 * @inode: inode for this chardev
1192 * @filp: file struct for storing private data
1193 * Returns 0 on success
1194 */
1195static int gpio_chrdev_release(struct inode *inode, struct file *filp)
1196{
1197 struct gpio_device *gdev = container_of(inode->i_cdev,
1198 struct gpio_device, chrdev);
1199
Linus Walleij3c702e92015-10-21 15:29:53 +02001200 put_device(&gdev->dev);
1201 return 0;
1202}
1203
1204
1205static const struct file_operations gpio_fileops = {
1206 .release = gpio_chrdev_release,
1207 .open = gpio_chrdev_open,
1208 .owner = THIS_MODULE,
Lars-Peter Clausenf4e81c52016-11-30 13:05:21 +01001209 .llseek = no_llseek,
Linus Walleij3c702e92015-10-21 15:29:53 +02001210 .unlocked_ioctl = gpio_ioctl,
Linus Walleij8b92e172016-05-27 14:24:04 +02001211#ifdef CONFIG_COMPAT
1212 .compat_ioctl = gpio_ioctl_compat,
1213#endif
Linus Walleij3c702e92015-10-21 15:29:53 +02001214};
1215
Linus Walleijff2b1352015-10-20 11:10:38 +02001216static void gpiodevice_release(struct device *dev)
1217{
1218 struct gpio_device *gdev = dev_get_drvdata(dev);
1219
1220 list_del(&gdev->list);
1221 ida_simple_remove(&gpio_ida, gdev->id);
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +01001222 kfree_const(gdev->label);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001223 kfree(gdev->descs);
Linus Walleij9efd9e62016-02-09 14:27:42 +01001224 kfree(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001225}
1226
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001227static int gpiochip_setup_dev(struct gpio_device *gdev)
1228{
Linus Walleijd377f562019-07-16 11:11:45 +02001229 int ret;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001230
1231 cdev_init(&gdev->chrdev, &gpio_fileops);
1232 gdev->chrdev.owner = THIS_MODULE;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001233 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001234
Linus Walleijd377f562019-07-16 11:11:45 +02001235 ret = cdev_device_add(&gdev->chrdev, &gdev->dev);
1236 if (ret)
1237 return ret;
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001238
1239 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
1240 MAJOR(gpio_devt), gdev->id);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001241
Linus Walleijd377f562019-07-16 11:11:45 +02001242 ret = gpiochip_sysfs_register(gdev);
1243 if (ret)
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001244 goto err_remove_device;
1245
1246 /* From this point, the .release() function cleans up gpio_device */
1247 gdev->dev.release = gpiodevice_release;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001248 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
1249 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
1250 dev_name(&gdev->dev), gdev->chip->label ? : "generic");
1251
1252 return 0;
1253
1254err_remove_device:
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001255 cdev_device_del(&gdev->chrdev, &gdev->dev);
Linus Walleijd377f562019-07-16 11:11:45 +02001256 return ret;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001257}
1258
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02001259static void gpiochip_machine_hog(struct gpio_chip *chip, struct gpiod_hog *hog)
1260{
1261 struct gpio_desc *desc;
1262 int rv;
1263
1264 desc = gpiochip_get_desc(chip, hog->chip_hwnum);
1265 if (IS_ERR(desc)) {
1266 pr_err("%s: unable to get GPIO desc: %ld\n",
1267 __func__, PTR_ERR(desc));
1268 return;
1269 }
1270
Dan Carpenterba3efdf2018-05-01 10:36:39 +03001271 if (test_bit(FLAG_IS_HOGGED, &desc->flags))
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02001272 return;
1273
1274 rv = gpiod_hog(desc, hog->line_name, hog->lflags, hog->dflags);
1275 if (rv)
1276 pr_err("%s: unable to hog GPIO line (%s:%u): %d\n",
1277 __func__, chip->label, hog->chip_hwnum, rv);
1278}
1279
1280static void machine_gpiochip_add(struct gpio_chip *chip)
1281{
1282 struct gpiod_hog *hog;
1283
1284 mutex_lock(&gpio_machine_hogs_mutex);
1285
1286 list_for_each_entry(hog, &gpio_machine_hogs, list) {
1287 if (!strcmp(chip->label, hog->chip_label))
1288 gpiochip_machine_hog(chip, hog);
1289 }
1290
1291 mutex_unlock(&gpio_machine_hogs_mutex);
1292}
1293
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001294static void gpiochip_setup_devs(void)
1295{
1296 struct gpio_device *gdev;
Linus Walleijd377f562019-07-16 11:11:45 +02001297 int ret;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001298
1299 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijd377f562019-07-16 11:11:45 +02001300 ret = gpiochip_setup_dev(gdev);
1301 if (ret)
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001302 pr_err("%s: Failed to initialize gpio device (%d)\n",
Linus Walleijd377f562019-07-16 11:11:45 +02001303 dev_name(&gdev->dev), ret);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001304 }
1305}
1306
Thierry Reding959bc7b2017-11-07 19:15:59 +01001307int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001308 struct lock_class_key *lock_key,
1309 struct lock_class_key *request_key)
David Brownelld2876d02008-02-04 22:28:20 -08001310{
1311 unsigned long flags;
Linus Walleijd377f562019-07-16 11:11:45 +02001312 int ret = 0;
Linus Walleijff2b1352015-10-20 11:10:38 +02001313 unsigned i;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001314 int base = chip->base;
Linus Walleijff2b1352015-10-20 11:10:38 +02001315 struct gpio_device *gdev;
David Brownelld2876d02008-02-04 22:28:20 -08001316
Linus Walleijff2b1352015-10-20 11:10:38 +02001317 /*
1318 * First: allocate and populate the internal stat container, and
1319 * set up the struct device.
1320 */
Josh Cartwright969f07b2016-02-17 16:44:15 -06001321 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
Linus Walleijff2b1352015-10-20 11:10:38 +02001322 if (!gdev)
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001323 return -ENOMEM;
Linus Walleij3c702e92015-10-21 15:29:53 +02001324 gdev->dev.bus = &gpio_bus_type;
Linus Walleijff2b1352015-10-20 11:10:38 +02001325 gdev->chip = chip;
1326 chip->gpiodev = gdev;
1327 if (chip->parent) {
1328 gdev->dev.parent = chip->parent;
1329 gdev->dev.of_node = chip->parent->of_node;
Thierry Redingacc6e332016-07-05 14:11:14 +02001330 }
1331
Linus Walleijff2b1352015-10-20 11:10:38 +02001332#ifdef CONFIG_OF_GPIO
1333 /* If the gpiochip has an assigned OF node this takes precedence */
Thierry Redingacc6e332016-07-05 14:11:14 +02001334 if (chip->of_node)
1335 gdev->dev.of_node = chip->of_node;
Biju Das6ff04972018-08-06 10:48:01 +01001336 else
1337 chip->of_node = gdev->dev.of_node;
Linus Walleijff2b1352015-10-20 11:10:38 +02001338#endif
Thierry Redingacc6e332016-07-05 14:11:14 +02001339
Linus Walleijff2b1352015-10-20 11:10:38 +02001340 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
1341 if (gdev->id < 0) {
Linus Walleijd377f562019-07-16 11:11:45 +02001342 ret = gdev->id;
Linus Walleijff2b1352015-10-20 11:10:38 +02001343 goto err_free_gdev;
1344 }
1345 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
1346 device_initialize(&gdev->dev);
1347 dev_set_drvdata(&gdev->dev, gdev);
1348 if (chip->parent && chip->parent->driver)
1349 gdev->owner = chip->parent->driver->owner;
1350 else if (chip->owner)
1351 /* TODO: remove chip->owner */
1352 gdev->owner = chip->owner;
1353 else
1354 gdev->owner = THIS_MODULE;
David Brownelld2876d02008-02-04 22:28:20 -08001355
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001356 gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001357 if (!gdev->descs) {
Linus Walleijd377f562019-07-16 11:11:45 +02001358 ret = -ENOMEM;
Vladimir Zapolskiya05a1402018-11-02 15:39:43 +02001359 goto err_free_ida;
Linus Walleijff2b1352015-10-20 11:10:38 +02001360 }
1361
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001362 if (chip->ngpio == 0) {
1363 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
Linus Walleijd377f562019-07-16 11:11:45 +02001364 ret = -EINVAL;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001365 goto err_free_descs;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001366 }
Linus Walleijdf4878e2016-02-12 14:48:23 +01001367
Laura Abbott30277432018-05-21 10:57:07 -07001368 if (chip->ngpio > FASTPATH_NGPIO)
1369 chip_warn(chip, "line cnt %u is greater than fast path cnt %u\n",
1370 chip->ngpio, FASTPATH_NGPIO);
1371
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +01001372 gdev->label = kstrdup_const(chip->label ?: "unknown", GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001373 if (!gdev->label) {
Linus Walleijd377f562019-07-16 11:11:45 +02001374 ret = -ENOMEM;
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001375 goto err_free_descs;
Linus Walleijdf4878e2016-02-12 14:48:23 +01001376 }
1377
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001378 gdev->ngpio = chip->ngpio;
Linus Walleij43c54ec2016-02-11 11:37:48 +01001379 gdev->data = data;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001380
David Brownelld2876d02008-02-04 22:28:20 -08001381 spin_lock_irqsave(&gpio_lock, flags);
1382
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001383 /*
1384 * TODO: this allocates a Linux GPIO number base in the global
1385 * GPIO numberspace for this chip. In the long run we want to
1386 * get *rid* of this numberspace and use only descriptors, but
1387 * it may be a pipe dream. It will not happen before we get rid
1388 * of the sysfs interface anyways.
1389 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001390 if (base < 0) {
1391 base = gpiochip_find_base(chip->ngpio);
1392 if (base < 0) {
Linus Walleijd377f562019-07-16 11:11:45 +02001393 ret = base;
Johan Hovold225fce82015-01-12 17:12:25 +01001394 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001395 goto err_free_label;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001396 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001397 /*
1398 * TODO: it should not be necessary to reflect the assigned
1399 * base outside of the GPIO subsystem. Go over drivers and
1400 * see if anyone makes use of this, else drop this and assign
1401 * a poison instead.
1402 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001403 chip->base = base;
1404 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001405 gdev->base = base;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001406
Linus Walleijd377f562019-07-16 11:11:45 +02001407 ret = gpiodev_add_to_list(gdev);
1408 if (ret) {
Johan Hovold05aa5202015-01-12 17:12:26 +01001409 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001410 goto err_free_label;
Johan Hovold05aa5202015-01-12 17:12:26 +01001411 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001412
Linus Walleij545ebd92016-05-30 17:11:59 +02001413 spin_unlock_irqrestore(&gpio_lock, flags);
1414
Ricardo Ribalda Delgado767cd172018-10-12 08:11:36 +02001415 for (i = 0; i < chip->ngpio; i++)
1416 gdev->descs[i].gdev = gdev;
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001417
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301418#ifdef CONFIG_PINCTRL
Linus Walleij20ec3e32016-02-11 11:03:06 +01001419 INIT_LIST_HEAD(&gdev->pin_ranges);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301420#endif
1421
Linus Walleijd377f562019-07-16 11:11:45 +02001422 ret = gpiochip_set_desc_names(chip);
1423 if (ret)
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001424 goto err_remove_from_list;
1425
Linus Walleij151a4102019-09-05 11:40:54 +02001426 ret = gpiochip_alloc_valid_mask(chip);
Linus Walleijd377f562019-07-16 11:11:45 +02001427 if (ret)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001428 goto err_remove_from_list;
1429
Linus Walleijd377f562019-07-16 11:11:45 +02001430 ret = of_gpiochip_add(chip);
1431 if (ret)
Linus Walleij48057ed2019-08-20 10:05:27 +02001432 goto err_free_gpiochip_mask;
Tomeu Vizoso28355f82015-07-14 10:29:54 +02001433
Linus Walleijd377f562019-07-16 11:11:45 +02001434 ret = gpiochip_init_valid_mask(chip);
1435 if (ret)
Geert Uytterhoeven35779892019-04-24 15:59:33 +02001436 goto err_remove_of_chip;
Ricardo Ribalda Delgadof8ec92a2018-10-05 08:52:58 +02001437
Ricardo Ribalda Delgado3edfb7b2018-10-05 08:53:00 +02001438 for (i = 0; i < chip->ngpio; i++) {
1439 struct gpio_desc *desc = &gdev->descs[i];
1440
Chris Packhamd95da992019-07-08 08:35:58 +12001441 if (chip->get_direction && gpiochip_line_is_valid(chip, i)) {
1442 if (!chip->get_direction(chip, i))
1443 set_bit(FLAG_IS_OUT, &desc->flags);
1444 else
1445 clear_bit(FLAG_IS_OUT, &desc->flags);
1446 } else {
1447 if (!chip->direction_input)
1448 set_bit(FLAG_IS_OUT, &desc->flags);
1449 else
1450 clear_bit(FLAG_IS_OUT, &desc->flags);
1451 }
Ricardo Ribalda Delgado3edfb7b2018-10-05 08:53:00 +02001452 }
1453
Mika Westerberg664e3e52014-01-08 12:40:54 +02001454 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001455
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02001456 machine_gpiochip_add(chip);
1457
Linus Walleij504369c2019-10-30 13:29:14 +01001458 ret = gpiochip_irqchip_init_valid_mask(chip);
Andy Shevchenko9411e3a2019-10-09 17:34:44 +03001459 if (ret)
1460 goto err_remove_acpi_chip;
1461
Linus Walleij504369c2019-10-30 13:29:14 +01001462 ret = gpiochip_irqchip_init_hw(chip);
Linus Walleijfbdf8d42019-09-06 12:05:35 +02001463 if (ret)
Linus Walleij48057ed2019-08-20 10:05:27 +02001464 goto err_remove_acpi_chip;
1465
Linus Walleijfbdf8d42019-09-06 12:05:35 +02001466 ret = gpiochip_add_irqchip(chip, lock_key, request_key);
1467 if (ret)
Linus Walleij48057ed2019-08-20 10:05:27 +02001468 goto err_remove_irqchip_mask;
1469
Linus Walleij3c702e92015-10-21 15:29:53 +02001470 /*
1471 * By first adding the chardev, and then adding the device,
1472 * we get a device node entry in sysfs under
1473 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1474 * coldplug of device nodes and other udev business.
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001475 * We can do this only if gpiolib has been initialized.
1476 * Otherwise, defer until later.
Linus Walleij3c702e92015-10-21 15:29:53 +02001477 */
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001478 if (gpiolib_initialized) {
Linus Walleijd377f562019-07-16 11:11:45 +02001479 ret = gpiochip_setup_dev(gdev);
1480 if (ret)
Linus Walleij48057ed2019-08-20 10:05:27 +02001481 goto err_remove_irqchip;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001482 }
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001483 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001484
Linus Walleij48057ed2019-08-20 10:05:27 +02001485err_remove_irqchip:
1486 gpiochip_irqchip_remove(chip);
1487err_remove_irqchip_mask:
1488 gpiochip_irqchip_free_valid_mask(chip);
Geert Uytterhoeven35779892019-04-24 15:59:33 +02001489err_remove_acpi_chip:
Johan Hovold225fce82015-01-12 17:12:25 +01001490 acpi_gpiochip_remove(chip);
Geert Uytterhoeven35779892019-04-24 15:59:33 +02001491err_remove_of_chip:
Johan Hovold6d867502015-05-04 17:23:25 +02001492 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +01001493 of_gpiochip_remove(chip);
Geert Uytterhoeven35779892019-04-24 15:59:33 +02001494err_free_gpiochip_mask:
Andy Shevchenko2f4133b2019-11-05 20:06:54 +02001495 gpiochip_remove_pin_ranges(chip);
Stephen Boyd726cb3b2018-03-23 09:34:52 -07001496 gpiochip_free_valid_mask(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001497err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +01001498 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001499 list_del(&gdev->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001500 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001501err_free_label:
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +01001502 kfree_const(gdev->label);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001503err_free_descs:
1504 kfree(gdev->descs);
Vladimir Zapolskiya05a1402018-11-02 15:39:43 +02001505err_free_ida:
Linus Walleijff2b1352015-10-20 11:10:38 +02001506 ida_simple_remove(&gpio_ida, gdev->id);
Vladimir Zapolskiya05a1402018-11-02 15:39:43 +02001507err_free_gdev:
David Brownelld2876d02008-02-04 22:28:20 -08001508 /* failures here can mean systems won't boot... */
Marcel Ziswiler1777fc92018-07-20 09:54:49 +02001509 pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__,
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001510 gdev->base, gdev->base + gdev->ngpio - 1,
Linus Walleijd377f562019-07-16 11:11:45 +02001511 chip->label ? : "generic", ret);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001512 kfree(gdev);
Linus Walleijd377f562019-07-16 11:11:45 +02001513 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08001514}
Thierry Reding959bc7b2017-11-07 19:15:59 +01001515EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);
David Brownelld2876d02008-02-04 22:28:20 -08001516
1517/**
Linus Walleij43c54ec2016-02-11 11:37:48 +01001518 * gpiochip_get_data() - get per-subdriver data for the chip
Thierry Reding950d55f52017-07-24 16:57:22 +02001519 * @chip: GPIO chip
1520 *
1521 * Returns:
1522 * The per-subdriver data for the chip.
Linus Walleij43c54ec2016-02-11 11:37:48 +01001523 */
1524void *gpiochip_get_data(struct gpio_chip *chip)
1525{
1526 return chip->gpiodev->data;
1527}
1528EXPORT_SYMBOL_GPL(gpiochip_get_data);
1529
1530/**
David Brownelld2876d02008-02-04 22:28:20 -08001531 * gpiochip_remove() - unregister a gpio_chip
1532 * @chip: the chip to unregister
1533 *
1534 * A gpio_chip with any GPIOs still requested may not be removed.
1535 */
abdoulaye berthee1db1702014-07-05 18:28:50 +02001536void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -08001537{
Linus Walleijff2b1352015-10-20 11:10:38 +02001538 struct gpio_device *gdev = chip->gpiodev;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001539 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001540 unsigned long flags;
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001541 unsigned i;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001542 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -08001543
Linus Walleijff2b1352015-10-20 11:10:38 +02001544 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
Linus Walleijafbc4f32016-02-09 13:21:06 +01001545 gpiochip_sysfs_unregister(gdev);
Geert Uytterhoeven5018ada2016-12-19 18:29:23 +01001546 gpiochip_free_hogs(chip);
Bamvor Jian Zhangbd203bd2016-02-20 13:13:19 +08001547 /* Numb the device, cancelling all outstanding operations */
1548 gdev->chip = NULL;
Johan Hovold00acc3d2015-01-12 17:12:27 +01001549 gpiochip_irqchip_remove(chip);
Mika Westerberg6072b9d2014-03-10 14:54:53 +02001550 acpi_gpiochip_remove(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001551 of_gpiochip_remove(chip);
Andy Shevchenko2f4133b2019-11-05 20:06:54 +02001552 gpiochip_remove_pin_ranges(chip);
Stephen Boyd726cb3b2018-03-23 09:34:52 -07001553 gpiochip_free_valid_mask(chip);
Linus Walleij43c54ec2016-02-11 11:37:48 +01001554 /*
1555 * We accept no more calls into the driver from this point, so
1556 * NULL the driver data pointer
1557 */
1558 gdev->data = NULL;
Anton Vorontsov391c9702010-06-08 07:48:17 -06001559
Johan Hovold6798aca2015-01-12 17:12:28 +01001560 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001561 for (i = 0; i < gdev->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001562 desc = &gdev->descs[i];
Johan Hovoldfab28b82015-05-04 17:10:27 +02001563 if (test_bit(FLAG_REQUESTED, &desc->flags))
1564 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -08001565 }
David Brownelld2876d02008-02-04 22:28:20 -08001566 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001567
Johan Hovoldfab28b82015-05-04 17:10:27 +02001568 if (requested)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001569 dev_crit(&gdev->dev,
Linus Walleij58383c782015-11-04 09:56:26 +01001570 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +02001571
Linus Walleijff2b1352015-10-20 11:10:38 +02001572 /*
1573 * The gpiochip side puts its use of the device to rest here:
1574 * if there are no userspace clients, the chardev and device will
1575 * be removed, else it will be dangling until the last user is
1576 * gone.
1577 */
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001578 cdev_device_del(&gdev->chrdev, &gdev->dev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001579 put_device(&gdev->dev);
David Brownelld2876d02008-02-04 22:28:20 -08001580}
1581EXPORT_SYMBOL_GPL(gpiochip_remove);
1582
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301583static void devm_gpio_chip_release(struct device *dev, void *res)
1584{
1585 struct gpio_chip *chip = *(struct gpio_chip **)res;
1586
1587 gpiochip_remove(chip);
1588}
1589
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301590/**
Jonathan Neuschäfer689fd022017-12-21 17:56:34 +01001591 * devm_gpiochip_add_data() - Resource manager gpiochip_add_data()
Uwe Kleine-König3925b90f2018-10-08 11:34:03 +02001592 * @dev: pointer to the device that gpio_chip belongs to.
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301593 * @chip: the chip to register, with chip->base initialized
Thierry Reding950d55f52017-07-24 16:57:22 +02001594 * @data: driver-private data associated with this chip
1595 *
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301596 * Context: potentially before irqs will work
1597 *
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301598 * The gpio chip automatically be released when the device is unbound.
Thierry Reding950d55f52017-07-24 16:57:22 +02001599 *
1600 * Returns:
1601 * A negative errno if the chip can't be registered, such as because the
1602 * chip->base is invalid or already associated with a different chip.
1603 * Otherwise it returns zero as a success code.
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301604 */
1605int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
1606 void *data)
1607{
1608 struct gpio_chip **ptr;
1609 int ret;
1610
1611 ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
1612 GFP_KERNEL);
1613 if (!ptr)
1614 return -ENOMEM;
1615
1616 ret = gpiochip_add_data(chip, data);
1617 if (ret < 0) {
1618 devres_free(ptr);
1619 return ret;
1620 }
1621
1622 *ptr = chip;
1623 devres_add(dev, ptr);
1624
1625 return 0;
1626}
1627EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
1628
1629/**
Grant Likely594fa262010-06-08 07:48:16 -06001630 * gpiochip_find() - iterator for locating a specific gpio_chip
1631 * @data: data to pass to match function
Thierry Reding950d55f52017-07-24 16:57:22 +02001632 * @match: Callback function to check gpio_chip
Grant Likely594fa262010-06-08 07:48:16 -06001633 *
1634 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1635 * determined by a user supplied @match callback. The callback should return
1636 * 0 if the device doesn't match and non-zero if it does. If the callback is
1637 * non-zero, this function will return to the caller and not iterate over any
1638 * more gpio_chips.
1639 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001640struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001641 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001642 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001643{
Linus Walleijff2b1352015-10-20 11:10:38 +02001644 struct gpio_device *gdev;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001645 struct gpio_chip *chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -06001646 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001647
1648 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001649 list_for_each_entry(gdev, &gpio_devices, list)
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001650 if (gdev->chip && match(gdev->chip, data)) {
1651 chip = gdev->chip;
Grant Likely594fa262010-06-08 07:48:16 -06001652 break;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001653 }
Linus Walleijff2b1352015-10-20 11:10:38 +02001654
Grant Likely594fa262010-06-08 07:48:16 -06001655 spin_unlock_irqrestore(&gpio_lock, flags);
1656
1657 return chip;
1658}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001659EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001660
Alexandre Courbot79697ef2013-11-16 21:39:32 +09001661static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1662{
1663 const char *name = data;
1664
1665 return !strcmp(chip->label, name);
1666}
1667
1668static struct gpio_chip *find_chip_by_name(const char *name)
1669{
1670 return gpiochip_find((void *)name, gpiochip_match_name);
1671}
1672
Linus Walleij14250522014-03-25 10:40:18 +01001673#ifdef CONFIG_GPIOLIB_IRQCHIP
1674
1675/*
1676 * The following is irqchip helper code for gpiochips.
1677 */
1678
Andy Shevchenko9411e3a2019-10-09 17:34:44 +03001679static int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
1680{
1681 struct gpio_irq_chip *girq = &gc->irq;
1682
1683 if (!girq->init_hw)
1684 return 0;
1685
1686 return girq->init_hw(gc);
1687}
1688
Linus Walleij5fbe5b52019-09-04 16:01:04 +02001689static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001690{
Linus Walleij5fbe5b52019-09-04 16:01:04 +02001691 struct gpio_irq_chip *girq = &gc->irq;
1692
1693 if (!girq->init_valid_mask)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001694 return 0;
1695
Linus Walleij5fbe5b52019-09-04 16:01:04 +02001696 girq->valid_mask = gpiochip_allocate_mask(gc);
1697 if (!girq->valid_mask)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001698 return -ENOMEM;
1699
Linus Walleij5fbe5b52019-09-04 16:01:04 +02001700 girq->init_valid_mask(gc, girq->valid_mask, gc->ngpio);
1701
Mika Westerberg79b804c2016-09-20 15:15:21 +03001702 return 0;
1703}
1704
1705static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1706{
Masahiro Yamada7bdbd1ec2019-07-18 15:51:01 +09001707 bitmap_free(gpiochip->irq.valid_mask);
Thierry Redingdc7b0382017-11-07 19:15:52 +01001708 gpiochip->irq.valid_mask = NULL;
Mika Westerberg79b804c2016-09-20 15:15:21 +03001709}
1710
Stephen Boyd64ff2c82018-01-09 17:58:46 -08001711bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
1712 unsigned int offset)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001713{
Stephen Boyd726cb3b2018-03-23 09:34:52 -07001714 if (!gpiochip_line_is_valid(gpiochip, offset))
1715 return false;
Mika Westerberg79b804c2016-09-20 15:15:21 +03001716 /* No mask means all valid */
Thierry Redingdc7b0382017-11-07 19:15:52 +01001717 if (likely(!gpiochip->irq.valid_mask))
Mika Westerberg79b804c2016-09-20 15:15:21 +03001718 return true;
Thierry Redingdc7b0382017-11-07 19:15:52 +01001719 return test_bit(offset, gpiochip->irq.valid_mask);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001720}
Stephen Boyd64ff2c82018-01-09 17:58:46 -08001721EXPORT_SYMBOL_GPL(gpiochip_irqchip_irq_valid);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001722
Linus Walleij14250522014-03-25 10:40:18 +01001723/**
Linus Walleijd245b3f2016-11-24 10:57:25 +01001724 * gpiochip_set_cascaded_irqchip() - connects a cascaded irqchip to a gpiochip
Linus Walleij4892d3a2019-06-14 10:12:26 +02001725 * @gc: the gpiochip to set the irqchip chain to
Linus Walleij14250522014-03-25 10:40:18 +01001726 * @parent_irq: the irq number corresponding to the parent IRQ for this
1727 * chained irqchip
1728 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001729 * coming out of the gpiochip. If the interrupt is nested rather than
1730 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +01001731 */
Linus Walleij4892d3a2019-06-14 10:12:26 +02001732static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gc,
Thierry Reding6f793092017-04-03 18:05:21 +02001733 unsigned int parent_irq,
Linus Walleijd245b3f2016-11-24 10:57:25 +01001734 irq_flow_handler_t parent_handler)
Linus Walleij14250522014-03-25 10:40:18 +01001735{
Linus Walleij4892d3a2019-06-14 10:12:26 +02001736 struct gpio_irq_chip *girq = &gc->irq;
1737 struct device *dev = &gc->gpiodev->dev;
1738
1739 if (!girq->domain) {
1740 chip_err(gc, "called %s before setting up irqchip\n",
Linus Walleij83141a72014-09-26 13:50:12 +02001741 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +02001742 return;
1743 }
1744
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001745 if (parent_handler) {
Linus Walleij4892d3a2019-06-14 10:12:26 +02001746 if (gc->can_sleep) {
1747 chip_err(gc,
Andy Shevchenkob1911712018-07-03 03:39:03 +03001748 "you cannot have chained interrupts on a chip that may sleep\n");
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001749 return;
1750 }
Linus Walleij4892d3a2019-06-14 10:12:26 +02001751 girq->parents = devm_kcalloc(dev, 1,
1752 sizeof(*girq->parents),
1753 GFP_KERNEL);
1754 if (!girq->parents) {
1755 chip_err(gc, "out of memory allocating parent IRQ\n");
1756 return;
1757 }
1758 girq->parents[0] = parent_irq;
1759 girq->num_parents = 1;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001760 /*
1761 * The parent irqchip is already using the chip_data for this
1762 * irqchip, so our callbacks simply use the handler_data.
1763 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +02001764 irq_set_chained_handler_and_data(parent_irq, parent_handler,
Linus Walleij4892d3a2019-06-14 10:12:26 +02001765 gc);
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001766 }
Linus Walleij14250522014-03-25 10:40:18 +01001767}
Linus Walleijd245b3f2016-11-24 10:57:25 +01001768
1769/**
1770 * gpiochip_set_chained_irqchip() - connects a chained irqchip to a gpiochip
1771 * @gpiochip: the gpiochip to set the irqchip chain to
1772 * @irqchip: the irqchip to chain to the gpiochip
1773 * @parent_irq: the irq number corresponding to the parent IRQ for this
1774 * chained irqchip
1775 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Stephen Boyd40f5ff42018-10-08 09:32:16 -07001776 * coming out of the gpiochip.
Linus Walleijd245b3f2016-11-24 10:57:25 +01001777 */
1778void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
1779 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001780 unsigned int parent_irq,
Linus Walleijd245b3f2016-11-24 10:57:25 +01001781 irq_flow_handler_t parent_handler)
1782{
Thierry Reding60ed54c2017-11-07 19:15:57 +01001783 if (gpiochip->irq.threaded) {
1784 chip_err(gpiochip, "tried to chain a threaded gpiochip\n");
1785 return;
1786 }
1787
Stephen Boyd3c1f6b22018-10-08 09:32:15 -07001788 gpiochip_set_cascaded_irqchip(gpiochip, parent_irq, parent_handler);
Linus Walleijd245b3f2016-11-24 10:57:25 +01001789}
Linus Walleij14250522014-03-25 10:40:18 +01001790EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
1791
1792/**
Linus Walleijd245b3f2016-11-24 10:57:25 +01001793 * gpiochip_set_nested_irqchip() - connects a nested irqchip to a gpiochip
1794 * @gpiochip: the gpiochip to set the irqchip nested handler to
1795 * @irqchip: the irqchip to nest to the gpiochip
1796 * @parent_irq: the irq number corresponding to the parent IRQ for this
1797 * nested irqchip
1798 */
1799void gpiochip_set_nested_irqchip(struct gpio_chip *gpiochip,
1800 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001801 unsigned int parent_irq)
Linus Walleijd245b3f2016-11-24 10:57:25 +01001802{
Stephen Boyd3c1f6b22018-10-08 09:32:15 -07001803 gpiochip_set_cascaded_irqchip(gpiochip, parent_irq, NULL);
Linus Walleijd245b3f2016-11-24 10:57:25 +01001804}
1805EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip);
1806
Linus Walleijfdd61a02019-08-08 14:32:37 +02001807#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1808
1809/**
1810 * gpiochip_set_hierarchical_irqchip() - connects a hierarchical irqchip
1811 * to a gpiochip
1812 * @gc: the gpiochip to set the irqchip hierarchical handler to
1813 * @irqchip: the irqchip to handle this level of the hierarchy, the interrupt
1814 * will then percolate up to the parent
1815 */
1816static void gpiochip_set_hierarchical_irqchip(struct gpio_chip *gc,
1817 struct irq_chip *irqchip)
1818{
1819 /* DT will deal with mapping each IRQ as we go along */
1820 if (is_of_node(gc->irq.fwnode))
1821 return;
1822
1823 /*
1824 * This is for legacy and boardfile "irqchip" fwnodes: allocate
1825 * irqs upfront instead of dynamically since we don't have the
1826 * dynamic type of allocation that hardware description languages
1827 * provide. Once all GPIO drivers using board files are gone from
1828 * the kernel we can delete this code, but for a transitional period
1829 * it is necessary to keep this around.
1830 */
1831 if (is_fwnode_irqchip(gc->irq.fwnode)) {
1832 int i;
1833 int ret;
1834
1835 for (i = 0; i < gc->ngpio; i++) {
1836 struct irq_fwspec fwspec;
1837 unsigned int parent_hwirq;
1838 unsigned int parent_type;
1839 struct gpio_irq_chip *girq = &gc->irq;
1840
1841 /*
1842 * We call the child to parent translation function
1843 * only to check if the child IRQ is valid or not.
1844 * Just pick the rising edge type here as that is what
1845 * we likely need to support.
1846 */
1847 ret = girq->child_to_parent_hwirq(gc, i,
1848 IRQ_TYPE_EDGE_RISING,
1849 &parent_hwirq,
1850 &parent_type);
1851 if (ret) {
1852 chip_err(gc, "skip set-up on hwirq %d\n",
1853 i);
1854 continue;
1855 }
1856
1857 fwspec.fwnode = gc->irq.fwnode;
1858 /* This is the hwirq for the GPIO line side of things */
1859 fwspec.param[0] = girq->child_offset_to_irq(gc, i);
1860 /* Just pick something */
1861 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
1862 fwspec.param_count = 2;
1863 ret = __irq_domain_alloc_irqs(gc->irq.domain,
1864 /* just pick something */
1865 -1,
1866 1,
1867 NUMA_NO_NODE,
1868 &fwspec,
1869 false,
1870 NULL);
1871 if (ret < 0) {
1872 chip_err(gc,
1873 "can not allocate irq for GPIO line %d parent hwirq %d in hierarchy domain: %d\n",
1874 i, parent_hwirq,
1875 ret);
1876 }
1877 }
1878 }
1879
1880 chip_err(gc, "%s unknown fwnode type proceed anyway\n", __func__);
1881
1882 return;
1883}
1884
1885static int gpiochip_hierarchy_irq_domain_translate(struct irq_domain *d,
1886 struct irq_fwspec *fwspec,
1887 unsigned long *hwirq,
1888 unsigned int *type)
1889{
1890 /* We support standard DT translation */
1891 if (is_of_node(fwspec->fwnode) && fwspec->param_count == 2) {
1892 return irq_domain_translate_twocell(d, fwspec, hwirq, type);
1893 }
1894
1895 /* This is for board files and others not using DT */
1896 if (is_fwnode_irqchip(fwspec->fwnode)) {
1897 int ret;
1898
1899 ret = irq_domain_translate_twocell(d, fwspec, hwirq, type);
1900 if (ret)
1901 return ret;
1902 WARN_ON(*type == IRQ_TYPE_NONE);
1903 return 0;
1904 }
1905 return -EINVAL;
1906}
1907
1908static int gpiochip_hierarchy_irq_domain_alloc(struct irq_domain *d,
1909 unsigned int irq,
1910 unsigned int nr_irqs,
1911 void *data)
1912{
1913 struct gpio_chip *gc = d->host_data;
1914 irq_hw_number_t hwirq;
1915 unsigned int type = IRQ_TYPE_NONE;
1916 struct irq_fwspec *fwspec = data;
1917 struct irq_fwspec parent_fwspec;
1918 unsigned int parent_hwirq;
1919 unsigned int parent_type;
1920 struct gpio_irq_chip *girq = &gc->irq;
1921 int ret;
1922
1923 /*
1924 * The nr_irqs parameter is always one except for PCI multi-MSI
1925 * so this should not happen.
1926 */
1927 WARN_ON(nr_irqs != 1);
1928
1929 ret = gc->irq.child_irq_domain_ops.translate(d, fwspec, &hwirq, &type);
1930 if (ret)
1931 return ret;
1932
1933 chip_info(gc, "allocate IRQ %d, hwirq %lu\n", irq, hwirq);
1934
1935 ret = girq->child_to_parent_hwirq(gc, hwirq, type,
1936 &parent_hwirq, &parent_type);
1937 if (ret) {
1938 chip_err(gc, "can't look up hwirq %lu\n", hwirq);
1939 return ret;
1940 }
1941 chip_info(gc, "found parent hwirq %u\n", parent_hwirq);
1942
1943 /*
1944 * We set handle_bad_irq because the .set_type() should
1945 * always be invoked and set the right type of handler.
1946 */
1947 irq_domain_set_info(d,
1948 irq,
1949 hwirq,
1950 gc->irq.chip,
1951 gc,
1952 girq->handler,
1953 NULL, NULL);
1954 irq_set_probe(irq);
1955
1956 /*
1957 * Create a IRQ fwspec to send up to the parent irqdomain:
1958 * specify the hwirq we address on the parent and tie it
1959 * all together up the chain.
1960 */
1961 parent_fwspec.fwnode = d->parent->fwnode;
1962 /* This parent only handles asserted level IRQs */
1963 girq->populate_parent_fwspec(gc, &parent_fwspec, parent_hwirq,
1964 parent_type);
1965 chip_info(gc, "alloc_irqs_parent for %d parent hwirq %d\n",
1966 irq, parent_hwirq);
1967 ret = irq_domain_alloc_irqs_parent(d, irq, 1, &parent_fwspec);
1968 if (ret)
1969 chip_err(gc,
1970 "failed to allocate parent hwirq %d for hwirq %lu\n",
1971 parent_hwirq, hwirq);
1972
1973 return ret;
1974}
1975
1976static unsigned int gpiochip_child_offset_to_irq_noop(struct gpio_chip *chip,
1977 unsigned int offset)
1978{
1979 return offset;
1980}
1981
1982static void gpiochip_hierarchy_setup_domain_ops(struct irq_domain_ops *ops)
1983{
1984 ops->activate = gpiochip_irq_domain_activate;
1985 ops->deactivate = gpiochip_irq_domain_deactivate;
1986 ops->alloc = gpiochip_hierarchy_irq_domain_alloc;
1987 ops->free = irq_domain_free_irqs_common;
1988
1989 /*
1990 * We only allow overriding the translate() function for
1991 * hierarchical chips, and this should only be done if the user
1992 * really need something other than 1:1 translation.
1993 */
1994 if (!ops->translate)
1995 ops->translate = gpiochip_hierarchy_irq_domain_translate;
1996}
1997
1998static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
1999{
2000 if (!gc->irq.child_to_parent_hwirq ||
2001 !gc->irq.fwnode) {
2002 chip_err(gc, "missing irqdomain vital data\n");
2003 return -EINVAL;
2004 }
2005
2006 if (!gc->irq.child_offset_to_irq)
2007 gc->irq.child_offset_to_irq = gpiochip_child_offset_to_irq_noop;
2008
2009 if (!gc->irq.populate_parent_fwspec)
2010 gc->irq.populate_parent_fwspec =
2011 gpiochip_populate_parent_fwspec_twocell;
2012
2013 gpiochip_hierarchy_setup_domain_ops(&gc->irq.child_irq_domain_ops);
2014
2015 gc->irq.domain = irq_domain_create_hierarchy(
2016 gc->irq.parent_domain,
2017 0,
2018 gc->ngpio,
2019 gc->irq.fwnode,
2020 &gc->irq.child_irq_domain_ops,
2021 gc);
2022
2023 if (!gc->irq.domain)
2024 return -ENOMEM;
2025
2026 gpiochip_set_hierarchical_irqchip(gc, gc->irq.chip);
2027
2028 return 0;
2029}
2030
2031static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
2032{
2033 return !!gc->irq.parent_domain;
2034}
2035
2036void gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *chip,
2037 struct irq_fwspec *fwspec,
2038 unsigned int parent_hwirq,
2039 unsigned int parent_type)
2040{
2041 fwspec->param_count = 2;
2042 fwspec->param[0] = parent_hwirq;
2043 fwspec->param[1] = parent_type;
2044}
2045EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_twocell);
2046
2047void gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *chip,
2048 struct irq_fwspec *fwspec,
2049 unsigned int parent_hwirq,
2050 unsigned int parent_type)
2051{
2052 fwspec->param_count = 4;
2053 fwspec->param[0] = 0;
2054 fwspec->param[1] = parent_hwirq;
2055 fwspec->param[2] = 0;
2056 fwspec->param[3] = parent_type;
2057}
2058EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_fourcell);
2059
2060#else
2061
2062static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
2063{
2064 return -EINVAL;
2065}
2066
2067static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
2068{
2069 return false;
2070}
2071
2072#endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
2073
Linus Walleijd245b3f2016-11-24 10:57:25 +01002074/**
Linus Walleij14250522014-03-25 10:40:18 +01002075 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
2076 * @d: the irqdomain used by this irqchip
2077 * @irq: the global irq number used by this GPIO irqchip irq
2078 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
2079 *
2080 * This function will set up the mapping for a certain IRQ line on a
2081 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
2082 * stored inside the gpiochip.
2083 */
Thierry Reding1b95b4e2017-11-07 19:15:55 +01002084int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
2085 irq_hw_number_t hwirq)
Linus Walleij14250522014-03-25 10:40:18 +01002086{
2087 struct gpio_chip *chip = d->host_data;
Linus Walleijd377f562019-07-16 11:11:45 +02002088 int ret = 0;
Linus Walleij14250522014-03-25 10:40:18 +01002089
Grygorii Strashkodc749a02017-07-21 11:49:00 -05002090 if (!gpiochip_irqchip_irq_valid(chip, hwirq))
2091 return -ENXIO;
2092
Linus Walleij14250522014-03-25 10:40:18 +01002093 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03002094 /*
2095 * This lock class tells lockdep that GPIO irqs are in a different
2096 * category than their parents, so it won't report false recursion.
2097 */
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002098 irq_set_lockdep_class(irq, chip->irq.lock_key, chip->irq.request_key);
Thierry Redingc7a0aa52017-11-07 19:15:48 +01002099 irq_set_chip_and_handler(irq, chip->irq.chip, chip->irq.handler);
Linus Walleijd245b3f2016-11-24 10:57:25 +01002100 /* Chips that use nested thread handlers have them marked */
Thierry Reding60ed54c2017-11-07 19:15:57 +01002101 if (chip->irq.threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02002102 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +01002103 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -05002104
Thierry Redinge0d89722017-11-07 19:15:54 +01002105 if (chip->irq.num_parents == 1)
Linus Walleijd377f562019-07-16 11:11:45 +02002106 ret = irq_set_parent(irq, chip->irq.parents[0]);
Thierry Redinge0d89722017-11-07 19:15:54 +01002107 else if (chip->irq.map)
Linus Walleijd377f562019-07-16 11:11:45 +02002108 ret = irq_set_parent(irq, chip->irq.map[hwirq]);
Thierry Redinge0d89722017-11-07 19:15:54 +01002109
Linus Walleijd377f562019-07-16 11:11:45 +02002110 if (ret < 0)
2111 return ret;
Thierry Redinge0d89722017-11-07 19:15:54 +01002112
Linus Walleij1333b902014-04-23 16:45:12 +02002113 /*
2114 * No set-up of the hardware will happen if IRQ_TYPE_NONE
2115 * is passed as default type.
2116 */
Thierry Reding3634eeb2017-11-07 19:15:49 +01002117 if (chip->irq.default_type != IRQ_TYPE_NONE)
2118 irq_set_irq_type(irq, chip->irq.default_type);
Linus Walleij14250522014-03-25 10:40:18 +01002119
2120 return 0;
2121}
Thierry Reding1b95b4e2017-11-07 19:15:55 +01002122EXPORT_SYMBOL_GPL(gpiochip_irq_map);
Linus Walleij14250522014-03-25 10:40:18 +01002123
Thierry Reding1b95b4e2017-11-07 19:15:55 +01002124void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
Linus Walleijc3626fd2014-03-28 20:42:01 +01002125{
Linus Walleij1c8732b2014-04-09 13:34:39 +02002126 struct gpio_chip *chip = d->host_data;
2127
Thierry Reding60ed54c2017-11-07 19:15:57 +01002128 if (chip->irq.threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02002129 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +01002130 irq_set_chip_and_handler(irq, NULL, NULL);
2131 irq_set_chip_data(irq, NULL);
2132}
Thierry Reding1b95b4e2017-11-07 19:15:55 +01002133EXPORT_SYMBOL_GPL(gpiochip_irq_unmap);
Linus Walleijc3626fd2014-03-28 20:42:01 +01002134
Linus Walleij14250522014-03-25 10:40:18 +01002135static const struct irq_domain_ops gpiochip_domain_ops = {
2136 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +01002137 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +01002138 /* Virtually all GPIO irqchips are twocell:ed */
2139 .xlate = irq_domain_xlate_twocell,
2140};
2141
Linus Walleijfdd61a02019-08-08 14:32:37 +02002142/*
2143 * TODO: move these activate/deactivate in under the hierarchicial
2144 * irqchip implementation as static once SPMI and SSBI (all external
2145 * users) are phased over.
2146 */
Brian Masneyef74f702019-01-19 15:42:42 -05002147/**
2148 * gpiochip_irq_domain_activate() - Lock a GPIO to be used as an IRQ
2149 * @domain: The IRQ domain used by this IRQ chip
2150 * @data: Outermost irq_data associated with the IRQ
2151 * @reserve: If set, only reserve an interrupt vector instead of assigning one
2152 *
2153 * This function is a wrapper that calls gpiochip_lock_as_irq() and is to be
2154 * used as the activate function for the &struct irq_domain_ops. The host_data
2155 * for the IRQ domain must be the &struct gpio_chip.
2156 */
2157int gpiochip_irq_domain_activate(struct irq_domain *domain,
2158 struct irq_data *data, bool reserve)
2159{
2160 struct gpio_chip *chip = domain->host_data;
2161
2162 return gpiochip_lock_as_irq(chip, data->hwirq);
2163}
2164EXPORT_SYMBOL_GPL(gpiochip_irq_domain_activate);
2165
2166/**
2167 * gpiochip_irq_domain_deactivate() - Unlock a GPIO used as an IRQ
2168 * @domain: The IRQ domain used by this IRQ chip
2169 * @data: Outermost irq_data associated with the IRQ
2170 *
2171 * This function is a wrapper that will call gpiochip_unlock_as_irq() and is to
2172 * be used as the deactivate function for the &struct irq_domain_ops. The
2173 * host_data for the IRQ domain must be the &struct gpio_chip.
2174 */
2175void gpiochip_irq_domain_deactivate(struct irq_domain *domain,
2176 struct irq_data *data)
2177{
2178 struct gpio_chip *chip = domain->host_data;
2179
2180 return gpiochip_unlock_as_irq(chip, data->hwirq);
2181}
2182EXPORT_SYMBOL_GPL(gpiochip_irq_domain_deactivate);
2183
Linus Walleij14250522014-03-25 10:40:18 +01002184static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
2185{
Linus Walleijfdd61a02019-08-08 14:32:37 +02002186 struct irq_domain *domain = chip->irq.domain;
2187
Grygorii Strashkodc749a02017-07-21 11:49:00 -05002188 if (!gpiochip_irqchip_irq_valid(chip, offset))
2189 return -ENXIO;
Thierry Redinge0d89722017-11-07 19:15:54 +01002190
Linus Walleijfdd61a02019-08-08 14:32:37 +02002191#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
2192 if (irq_domain_is_hierarchy(domain)) {
2193 struct irq_fwspec spec;
2194
2195 spec.fwnode = domain->fwnode;
2196 spec.param_count = 2;
2197 spec.param[0] = chip->irq.child_offset_to_irq(chip, offset);
2198 spec.param[1] = IRQ_TYPE_NONE;
2199
2200 return irq_create_fwspec_mapping(&spec);
2201 }
2202#endif
2203
2204 return irq_create_mapping(domain, offset);
Linus Walleij14250522014-03-25 10:40:18 +01002205}
2206
Hans Verkuil4e6b8232018-09-08 11:23:14 +02002207static int gpiochip_irq_reqres(struct irq_data *d)
2208{
2209 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
2210
2211 return gpiochip_reqres_irq(chip, d->hwirq);
2212}
2213
2214static void gpiochip_irq_relres(struct irq_data *d)
2215{
2216 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
2217
2218 gpiochip_relres_irq(chip, d->hwirq);
2219}
2220
Hans Verkuil461c1a72018-09-08 11:23:17 +02002221static void gpiochip_irq_enable(struct irq_data *d)
2222{
2223 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
2224
2225 gpiochip_enable_irq(chip, d->hwirq);
2226 if (chip->irq.irq_enable)
2227 chip->irq.irq_enable(d);
2228 else
2229 chip->irq.chip->irq_unmask(d);
2230}
2231
2232static void gpiochip_irq_disable(struct irq_data *d)
2233{
2234 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
2235
2236 if (chip->irq.irq_disable)
2237 chip->irq.irq_disable(d);
2238 else
2239 chip->irq.chip->irq_mask(d);
2240 gpiochip_disable_irq(chip, d->hwirq);
2241}
2242
Hans Verkuilca620f22018-09-08 11:23:15 +02002243static void gpiochip_set_irq_hooks(struct gpio_chip *gpiochip)
2244{
2245 struct irq_chip *irqchip = gpiochip->irq.chip;
2246
2247 if (!irqchip->irq_request_resources &&
2248 !irqchip->irq_release_resources) {
2249 irqchip->irq_request_resources = gpiochip_irq_reqres;
2250 irqchip->irq_release_resources = gpiochip_irq_relres;
2251 }
Hans Verkuil461c1a72018-09-08 11:23:17 +02002252 if (WARN_ON(gpiochip->irq.irq_enable))
2253 return;
Hans Verkuil171948e2018-09-14 10:36:39 +02002254 /* Check if the irqchip already has this hook... */
2255 if (irqchip->irq_enable == gpiochip_irq_enable) {
2256 /*
2257 * ...and if so, give a gentle warning that this is bad
2258 * practice.
2259 */
2260 chip_info(gpiochip,
2261 "detected irqchip that is shared with multiple gpiochips: please fix the driver.\n");
2262 return;
2263 }
Hans Verkuil461c1a72018-09-08 11:23:17 +02002264 gpiochip->irq.irq_enable = irqchip->irq_enable;
2265 gpiochip->irq.irq_disable = irqchip->irq_disable;
2266 irqchip->irq_enable = gpiochip_irq_enable;
2267 irqchip->irq_disable = gpiochip_irq_disable;
Hans Verkuilca620f22018-09-08 11:23:15 +02002268}
2269
Linus Walleij14250522014-03-25 10:40:18 +01002270/**
Thierry Redinge0d89722017-11-07 19:15:54 +01002271 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
2272 * @gpiochip: the GPIO chip to add the IRQ chip to
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002273 * @lock_key: lockdep class for IRQ lock
2274 * @request_key: lockdep class for IRQ request
Thierry Redinge0d89722017-11-07 19:15:54 +01002275 */
Thierry Reding959bc7b2017-11-07 19:15:59 +01002276static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002277 struct lock_class_key *lock_key,
2278 struct lock_class_key *request_key)
Thierry Redinge0d89722017-11-07 19:15:54 +01002279{
2280 struct irq_chip *irqchip = gpiochip->irq.chip;
Linus Walleijfdd61a02019-08-08 14:32:37 +02002281 const struct irq_domain_ops *ops = NULL;
Thierry Redinge0d89722017-11-07 19:15:54 +01002282 struct device_node *np;
2283 unsigned int type;
2284 unsigned int i;
2285
2286 if (!irqchip)
2287 return 0;
2288
2289 if (gpiochip->irq.parent_handler && gpiochip->can_sleep) {
Andy Shevchenkob1911712018-07-03 03:39:03 +03002290 chip_err(gpiochip, "you cannot have chained interrupts on a chip that may sleep\n");
Thierry Redinge0d89722017-11-07 19:15:54 +01002291 return -EINVAL;
2292 }
2293
2294 np = gpiochip->gpiodev->dev.of_node;
2295 type = gpiochip->irq.default_type;
2296
2297 /*
2298 * Specifying a default trigger is a terrible idea if DT or ACPI is
2299 * used to configure the interrupts, as you may end up with
2300 * conflicting triggers. Tell the user, and reset to NONE.
2301 */
2302 if (WARN(np && type != IRQ_TYPE_NONE,
2303 "%s: Ignoring %u default trigger\n", np->full_name, type))
2304 type = IRQ_TYPE_NONE;
2305
2306 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
2307 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
2308 "Ignoring %u default trigger\n", type);
2309 type = IRQ_TYPE_NONE;
2310 }
2311
2312 gpiochip->to_irq = gpiochip_to_irq;
2313 gpiochip->irq.default_type = type;
Thierry Reding959bc7b2017-11-07 19:15:59 +01002314 gpiochip->irq.lock_key = lock_key;
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002315 gpiochip->irq.request_key = request_key;
Thierry Redinge0d89722017-11-07 19:15:54 +01002316
Linus Walleijfdd61a02019-08-08 14:32:37 +02002317 /* If a parent irqdomain is provided, let's build a hierarchy */
2318 if (gpiochip_hierarchy_is_hierarchical(gpiochip)) {
2319 int ret = gpiochip_hierarchy_add_domain(gpiochip);
2320 if (ret)
2321 return ret;
2322 } else {
2323 /* Some drivers provide custom irqdomain ops */
2324 if (gpiochip->irq.domain_ops)
2325 ops = gpiochip->irq.domain_ops;
Thierry Redinge0d89722017-11-07 19:15:54 +01002326
Linus Walleijfdd61a02019-08-08 14:32:37 +02002327 if (!ops)
2328 ops = &gpiochip_domain_ops;
2329 gpiochip->irq.domain = irq_domain_add_simple(np,
2330 gpiochip->ngpio,
2331 gpiochip->irq.first,
2332 ops, gpiochip);
2333 if (!gpiochip->irq.domain)
2334 return -EINVAL;
2335 }
Thierry Redinge0d89722017-11-07 19:15:54 +01002336
Thierry Redinge0d89722017-11-07 19:15:54 +01002337 if (gpiochip->irq.parent_handler) {
2338 void *data = gpiochip->irq.parent_handler_data ?: gpiochip;
2339
2340 for (i = 0; i < gpiochip->irq.num_parents; i++) {
2341 /*
2342 * The parent IRQ chip is already using the chip_data
2343 * for this IRQ chip, so our callbacks simply use the
2344 * handler_data.
2345 */
2346 irq_set_chained_handler_and_data(gpiochip->irq.parents[i],
2347 gpiochip->irq.parent_handler,
2348 data);
2349 }
Thierry Redinge0d89722017-11-07 19:15:54 +01002350 }
2351
Hans Verkuilca620f22018-09-08 11:23:15 +02002352 gpiochip_set_irq_hooks(gpiochip);
2353
Thierry Redinge0d89722017-11-07 19:15:54 +01002354 acpi_gpiochip_request_interrupts(gpiochip);
2355
2356 return 0;
2357}
2358
2359/**
Linus Walleij14250522014-03-25 10:40:18 +01002360 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
2361 * @gpiochip: the gpiochip to remove the irqchip from
2362 *
2363 * This is called only from gpiochip_remove()
2364 */
2365static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
2366{
Hans Verkuilca620f22018-09-08 11:23:15 +02002367 struct irq_chip *irqchip = gpiochip->irq.chip;
Thierry Reding39e5f092017-11-07 19:15:50 +01002368 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +01002369
Mika Westerbergafa82fa2014-07-25 09:54:48 +03002370 acpi_gpiochip_free_interrupts(gpiochip);
2371
Hans Verkuilca620f22018-09-08 11:23:15 +02002372 if (irqchip && gpiochip->irq.parent_handler) {
Thierry Reding39e5f092017-11-07 19:15:50 +01002373 struct gpio_irq_chip *irq = &gpiochip->irq;
2374 unsigned int i;
2375
2376 for (i = 0; i < irq->num_parents; i++)
2377 irq_set_chained_handler_and_data(irq->parents[i],
2378 NULL, NULL);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03002379 }
2380
Linus Walleijc3626fd2014-03-28 20:42:01 +01002381 /* Remove all IRQ mappings and delete the domain */
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01002382 if (gpiochip->irq.domain) {
Thierry Reding39e5f092017-11-07 19:15:50 +01002383 unsigned int irq;
2384
Mika Westerberg79b804c2016-09-20 15:15:21 +03002385 for (offset = 0; offset < gpiochip->ngpio; offset++) {
2386 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
2387 continue;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01002388
2389 irq = irq_find_mapping(gpiochip->irq.domain, offset);
2390 irq_dispose_mapping(irq);
Mika Westerberg79b804c2016-09-20 15:15:21 +03002391 }
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01002392
2393 irq_domain_remove(gpiochip->irq.domain);
Linus Walleijc3626fd2014-03-28 20:42:01 +01002394 }
Linus Walleij14250522014-03-25 10:40:18 +01002395
Hans Verkuil461c1a72018-09-08 11:23:17 +02002396 if (irqchip) {
2397 if (irqchip->irq_request_resources == gpiochip_irq_reqres) {
2398 irqchip->irq_request_resources = NULL;
2399 irqchip->irq_release_resources = NULL;
2400 }
2401 if (irqchip->irq_enable == gpiochip_irq_enable) {
2402 irqchip->irq_enable = gpiochip->irq.irq_enable;
2403 irqchip->irq_disable = gpiochip->irq.irq_disable;
2404 }
Linus Walleij14250522014-03-25 10:40:18 +01002405 }
Hans Verkuil461c1a72018-09-08 11:23:17 +02002406 gpiochip->irq.irq_enable = NULL;
2407 gpiochip->irq.irq_disable = NULL;
Hans Verkuilca620f22018-09-08 11:23:15 +02002408 gpiochip->irq.chip = NULL;
Mika Westerberg79b804c2016-09-20 15:15:21 +03002409
2410 gpiochip_irqchip_free_valid_mask(gpiochip);
Linus Walleij14250522014-03-25 10:40:18 +01002411}
2412
2413/**
Linus Walleij739e6f52017-01-11 13:37:07 +01002414 * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01002415 * @gpiochip: the gpiochip to add the irqchip to
2416 * @irqchip: the irqchip to add to the gpiochip
2417 * @first_irq: if not dynamically assigned, the base (first) IRQ to
2418 * allocate gpiochip irqs from
2419 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +02002420 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
2421 * to have the core avoid setting up any default type in the hardware.
Thierry Reding60ed54c2017-11-07 19:15:57 +01002422 * @threaded: whether this irqchip uses a nested thread handler
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002423 * @lock_key: lockdep class for IRQ lock
2424 * @request_key: lockdep class for IRQ request
Linus Walleij14250522014-03-25 10:40:18 +01002425 *
2426 * This function closely associates a certain irqchip with a certain
2427 * gpiochip, providing an irq domain to translate the local IRQs to
2428 * global irqs in the gpiolib core, and making sure that the gpiochip
2429 * is passed as chip data to all related functions. Driver callbacks
Linus Walleij09dd5f92015-12-07 15:31:58 +01002430 * need to use gpiochip_get_data() to get their local state containers back
Linus Walleij14250522014-03-25 10:40:18 +01002431 * from the gpiochip passed as chip data. An irqdomain will be stored
2432 * in the gpiochip that shall be used by the driver to handle IRQ number
2433 * translation. The gpiochip will need to be initialized and registered
2434 * before calling this function.
2435 *
Linus Walleijc3626fd2014-03-28 20:42:01 +01002436 * This function will handle two cell:ed simple IRQs and assumes all
2437 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +01002438 * need to be open coded.
2439 */
Linus Walleij739e6f52017-01-11 13:37:07 +01002440int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
2441 struct irq_chip *irqchip,
2442 unsigned int first_irq,
2443 irq_flow_handler_t handler,
2444 unsigned int type,
Thierry Reding60ed54c2017-11-07 19:15:57 +01002445 bool threaded,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002446 struct lock_class_key *lock_key,
2447 struct lock_class_key *request_key)
Linus Walleij14250522014-03-25 10:40:18 +01002448{
2449 struct device_node *of_node;
Linus Walleij14250522014-03-25 10:40:18 +01002450
2451 if (!gpiochip || !irqchip)
2452 return -EINVAL;
2453
Linus Walleij58383c782015-11-04 09:56:26 +01002454 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +01002455 pr_err("missing gpiochip .dev parent pointer\n");
2456 return -EINVAL;
2457 }
Thierry Reding60ed54c2017-11-07 19:15:57 +01002458 gpiochip->irq.threaded = threaded;
Linus Walleij58383c782015-11-04 09:56:26 +01002459 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +01002460#ifdef CONFIG_OF_GPIO
2461 /*
Colin Cronin20a8a962015-05-18 11:41:43 -07002462 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08002463 * FIXME: get rid of this and use gpiochip->parent->of_node
2464 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +01002465 */
2466 if (gpiochip->of_node)
2467 of_node = gpiochip->of_node;
2468#endif
Marc Zyngier332e99d2016-09-07 09:12:11 +01002469 /*
Mika Westerberg0a1e0052016-09-12 14:29:51 +03002470 * Specifying a default trigger is a terrible idea if DT or ACPI is
Marc Zyngier332e99d2016-09-07 09:12:11 +01002471 * used to configure the interrupts, as you may end-up with
2472 * conflicting triggers. Tell the user, and reset to NONE.
2473 */
2474 if (WARN(of_node && type != IRQ_TYPE_NONE,
Rob Herring7eb6ce22017-07-18 16:43:03 -05002475 "%pOF: Ignoring %d default trigger\n", of_node, type))
Marc Zyngier332e99d2016-09-07 09:12:11 +01002476 type = IRQ_TYPE_NONE;
Mika Westerberg0a1e0052016-09-12 14:29:51 +03002477 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
2478 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
2479 "Ignoring %d default trigger\n", type);
2480 type = IRQ_TYPE_NONE;
2481 }
Marc Zyngier332e99d2016-09-07 09:12:11 +01002482
Thierry Redingda80ff82017-11-07 19:15:46 +01002483 gpiochip->irq.chip = irqchip;
Thierry Redingc7a0aa52017-11-07 19:15:48 +01002484 gpiochip->irq.handler = handler;
Thierry Reding3634eeb2017-11-07 19:15:49 +01002485 gpiochip->irq.default_type = type;
Linus Walleij14250522014-03-25 10:40:18 +01002486 gpiochip->to_irq = gpiochip_to_irq;
Thierry Redingca9df052017-11-07 19:15:53 +01002487 gpiochip->irq.lock_key = lock_key;
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002488 gpiochip->irq.request_key = request_key;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01002489 gpiochip->irq.domain = irq_domain_add_simple(of_node,
Linus Walleij14250522014-03-25 10:40:18 +01002490 gpiochip->ngpio, first_irq,
2491 &gpiochip_domain_ops, gpiochip);
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01002492 if (!gpiochip->irq.domain) {
Thierry Redingda80ff82017-11-07 19:15:46 +01002493 gpiochip->irq.chip = NULL;
Linus Walleij14250522014-03-25 10:40:18 +01002494 return -EINVAL;
2495 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +02002496
Hans Verkuilca620f22018-09-08 11:23:15 +02002497 gpiochip_set_irq_hooks(gpiochip);
Linus Walleij14250522014-03-25 10:40:18 +01002498
Mika Westerbergafa82fa2014-07-25 09:54:48 +03002499 acpi_gpiochip_request_interrupts(gpiochip);
2500
Linus Walleij14250522014-03-25 10:40:18 +01002501 return 0;
2502}
Linus Walleij739e6f52017-01-11 13:37:07 +01002503EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key);
Linus Walleij14250522014-03-25 10:40:18 +01002504
2505#else /* CONFIG_GPIOLIB_IRQCHIP */
2506
Thierry Reding959bc7b2017-11-07 19:15:59 +01002507static inline int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002508 struct lock_class_key *lock_key,
2509 struct lock_class_key *request_key)
Thierry Redinge0d89722017-11-07 19:15:54 +01002510{
2511 return 0;
2512}
Linus Walleij14250522014-03-25 10:40:18 +01002513static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
Andy Shevchenko9411e3a2019-10-09 17:34:44 +03002514
2515static inline int gpiochip_irqchip_init_hw(struct gpio_chip *gpiochip)
2516{
2517 return 0;
2518}
2519
Mika Westerberg79b804c2016-09-20 15:15:21 +03002520static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
2521{
2522 return 0;
2523}
2524static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
2525{ }
Linus Walleij14250522014-03-25 10:40:18 +01002526
2527#endif /* CONFIG_GPIOLIB_IRQCHIP */
2528
Jonas Gorskic771c2f2015-10-11 17:34:15 +02002529/**
2530 * gpiochip_generic_request() - request the gpio function for a pin
2531 * @chip: the gpiochip owning the GPIO
2532 * @offset: the offset of the GPIO to request for GPIO function
2533 */
2534int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
2535{
Linus Walleija9a1d2a2017-09-22 11:02:10 +02002536 return pinctrl_gpio_request(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02002537}
2538EXPORT_SYMBOL_GPL(gpiochip_generic_request);
2539
2540/**
2541 * gpiochip_generic_free() - free the gpio function from a pin
2542 * @chip: the gpiochip to request the gpio function for
2543 * @offset: the offset of the GPIO to free from GPIO function
2544 */
2545void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
2546{
Linus Walleija9a1d2a2017-09-22 11:02:10 +02002547 pinctrl_gpio_free(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02002548}
2549EXPORT_SYMBOL_GPL(gpiochip_generic_free);
2550
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002551/**
2552 * gpiochip_generic_config() - apply configuration for a pin
2553 * @chip: the gpiochip owning the GPIO
2554 * @offset: the offset of the GPIO to apply the configuration
2555 * @config: the configuration to be applied
2556 */
2557int gpiochip_generic_config(struct gpio_chip *chip, unsigned offset,
2558 unsigned long config)
2559{
2560 return pinctrl_gpio_set_config(chip->gpiodev->base + offset, config);
2561}
2562EXPORT_SYMBOL_GPL(gpiochip_generic_config);
2563
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302564#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01002565
Linus Walleij3f0f8672012-11-20 12:40:15 +01002566/**
Christian Ruppert586a87e2013-10-15 15:37:54 +02002567 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
2568 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +02002569 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +02002570 * @gpio_offset: the start offset in the current gpio_chip number space
2571 * @pin_group: name of the pin group inside the pin controller
Christian Lamparter973c1712018-05-21 22:57:39 +02002572 *
2573 * Calling this function directly from a DeviceTree-supported
2574 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
2575 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
2576 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
Christian Ruppert586a87e2013-10-15 15:37:54 +02002577 */
2578int gpiochip_add_pingroup_range(struct gpio_chip *chip,
2579 struct pinctrl_dev *pctldev,
2580 unsigned int gpio_offset, const char *pin_group)
2581{
2582 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002583 struct gpio_device *gdev = chip->gpiodev;
Christian Ruppert586a87e2013-10-15 15:37:54 +02002584 int ret;
2585
2586 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
2587 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002588 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +02002589 return -ENOMEM;
2590 }
2591
2592 /* Use local offset as range ID */
2593 pin_range->range.id = gpio_offset;
2594 pin_range->range.gc = chip;
2595 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002596 pin_range->range.base = gdev->base + gpio_offset;
Christian Ruppert586a87e2013-10-15 15:37:54 +02002597 pin_range->pctldev = pctldev;
2598
2599 ret = pinctrl_get_group_pins(pctldev, pin_group,
2600 &pin_range->range.pins,
2601 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +01002602 if (ret < 0) {
2603 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +02002604 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +01002605 }
Christian Ruppert586a87e2013-10-15 15:37:54 +02002606
2607 pinctrl_add_gpio_range(pctldev, &pin_range->range);
2608
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002609 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
2610 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +02002611 pinctrl_dev_get_devname(pctldev), pin_group);
2612
Linus Walleij20ec3e32016-02-11 11:03:06 +01002613 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Christian Ruppert586a87e2013-10-15 15:37:54 +02002614
2615 return 0;
2616}
2617EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
2618
2619/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01002620 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
2621 * @chip: the gpiochip to add the range for
Thierry Reding950d55f52017-07-24 16:57:22 +02002622 * @pinctl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01002623 * @gpio_offset: the start offset in the current gpio_chip number space
2624 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01002625 * @npins: the number of pins from the offset of each pin space (GPIO and
2626 * pin controller) to accumulate in this range
Thierry Reding950d55f52017-07-24 16:57:22 +02002627 *
2628 * Returns:
2629 * 0 on success, or a negative error-code on failure.
Christian Lamparter973c1712018-05-21 22:57:39 +02002630 *
2631 * Calling this function directly from a DeviceTree-supported
2632 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
2633 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
2634 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
Linus Walleij3f0f8672012-11-20 12:40:15 +01002635 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002636int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01002637 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01002638 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302639{
2640 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002641 struct gpio_device *gdev = chip->gpiodev;
Axel Linb4d4b1f2012-11-21 14:33:56 +08002642 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302643
Linus Walleij3f0f8672012-11-20 12:40:15 +01002644 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302645 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002646 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002647 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302648 }
2649
Linus Walleij3f0f8672012-11-20 12:40:15 +01002650 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01002651 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01002652 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302653 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002654 pin_range->range.base = gdev->base + gpio_offset;
Linus Walleij316511c2012-11-21 08:48:09 +01002655 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302656 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01002657 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302658 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01002659 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08002660 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002661 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +01002662 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08002663 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01002664 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002665 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
2666 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +01002667 pinctl_name,
2668 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302669
Linus Walleij20ec3e32016-02-11 11:03:06 +01002670 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002671
2672 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302673}
Linus Walleij165adc92012-11-06 14:49:39 +01002674EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302675
Linus Walleij3f0f8672012-11-20 12:40:15 +01002676/**
2677 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
2678 * @chip: the chip to remove all the mappings for
2679 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302680void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
2681{
2682 struct gpio_pin_range *pin_range, *tmp;
Linus Walleij20ec3e32016-02-11 11:03:06 +01002683 struct gpio_device *gdev = chip->gpiodev;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302684
Linus Walleij20ec3e32016-02-11 11:03:06 +01002685 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302686 list_del(&pin_range->node);
2687 pinctrl_remove_gpio_range(pin_range->pctldev,
2688 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01002689 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302690 }
2691}
Linus Walleij165adc92012-11-06 14:49:39 +01002692EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
2693
2694#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302695
David Brownelld2876d02008-02-04 22:28:20 -08002696/* These "optional" allocation calls help prevent drivers from stomping
2697 * on each other, and help provide better diagnostics in debugfs.
2698 * They're called even less than the "set direction" calls.
2699 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002700static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08002701{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002702 struct gpio_chip *chip = desc->gdev->chip;
Linus Walleijd377f562019-07-16 11:11:45 +02002703 int ret;
David Brownelld2876d02008-02-04 22:28:20 -08002704 unsigned long flags;
Biju Das3789f5a2018-08-07 08:15:18 +01002705 unsigned offset;
David Brownelld2876d02008-02-04 22:28:20 -08002706
Muchun Song18534df2018-11-01 21:12:50 +08002707 if (label) {
2708 label = kstrdup_const(label, GFP_KERNEL);
2709 if (!label)
2710 return -ENOMEM;
2711 }
2712
David Brownelld2876d02008-02-04 22:28:20 -08002713 spin_lock_irqsave(&gpio_lock, flags);
2714
David Brownelld2876d02008-02-04 22:28:20 -08002715 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07002716 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08002717 */
2718
2719 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
2720 desc_set_label(desc, label ? : "?");
Linus Walleijd377f562019-07-16 11:11:45 +02002721 ret = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07002722 } else {
Muchun Song18534df2018-11-01 21:12:50 +08002723 kfree_const(label);
Linus Walleijd377f562019-07-16 11:11:45 +02002724 ret = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -08002725 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07002726 }
2727
2728 if (chip->request) {
2729 /* chip->request may sleep */
2730 spin_unlock_irqrestore(&gpio_lock, flags);
Biju Das3789f5a2018-08-07 08:15:18 +01002731 offset = gpio_chip_hwgpio(desc);
2732 if (gpiochip_line_is_valid(chip, offset))
Linus Walleijd377f562019-07-16 11:11:45 +02002733 ret = chip->request(chip, offset);
Biju Das3789f5a2018-08-07 08:15:18 +01002734 else
Linus Walleijd377f562019-07-16 11:11:45 +02002735 ret = -EINVAL;
David Brownell35e8bb52008-10-15 22:03:16 -07002736 spin_lock_irqsave(&gpio_lock, flags);
2737
Linus Walleijd377f562019-07-16 11:11:45 +02002738 if (ret < 0) {
David Brownell35e8bb52008-10-15 22:03:16 -07002739 desc_set_label(desc, NULL);
Muchun Song18534df2018-11-01 21:12:50 +08002740 kfree_const(label);
David Brownell35e8bb52008-10-15 22:03:16 -07002741 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03002742 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07002743 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07002744 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03002745 if (chip->get_direction) {
2746 /* chip->get_direction may sleep */
2747 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002748 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03002749 spin_lock_irqsave(&gpio_lock, flags);
2750 }
David Brownelld2876d02008-02-04 22:28:20 -08002751done:
Mika Westerberg77c2d792014-03-10 14:54:50 +02002752 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijd377f562019-07-16 11:11:45 +02002753 return ret;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002754}
2755
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002756/*
2757 * This descriptor validation needs to be inserted verbatim into each
2758 * function taking a descriptor, so we need to use a preprocessor
Linus Walleij54d77192016-05-30 16:48:39 +02002759 * macro to avoid endless duplication. If the desc is NULL it is an
2760 * optional GPIO and calls should just bail out.
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002761 */
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002762static int validate_desc(const struct gpio_desc *desc, const char *func)
2763{
2764 if (!desc)
2765 return 0;
2766 if (IS_ERR(desc)) {
2767 pr_warn("%s: invalid GPIO (errorpointer)\n", func);
2768 return PTR_ERR(desc);
2769 }
2770 if (!desc->gdev) {
2771 pr_warn("%s: invalid GPIO (no device)\n", func);
2772 return -EINVAL;
2773 }
2774 if (!desc->gdev->chip) {
2775 dev_warn(&desc->gdev->dev,
2776 "%s: backing chip is gone\n", func);
2777 return 0;
2778 }
2779 return 1;
2780}
2781
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002782#define VALIDATE_DESC(desc) do { \
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002783 int __valid = validate_desc(desc, __func__); \
2784 if (__valid <= 0) \
2785 return __valid; \
2786 } while (0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002787
2788#define VALIDATE_DESC_VOID(desc) do { \
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002789 int __valid = validate_desc(desc, __func__); \
2790 if (__valid <= 0) \
Linus Walleij54d77192016-05-30 16:48:39 +02002791 return; \
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002792 } while (0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002793
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002794int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002795{
Linus Walleijd377f562019-07-16 11:11:45 +02002796 int ret = -EPROBE_DEFER;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002797 struct gpio_device *gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002798
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002799 VALIDATE_DESC(desc);
2800 gdev = desc->gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002801
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002802 if (try_module_get(gdev->owner)) {
Linus Walleijd377f562019-07-16 11:11:45 +02002803 ret = gpiod_request_commit(desc, label);
2804 if (ret < 0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002805 module_put(gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002806 else
2807 get_device(&gdev->dev);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002808 }
2809
Linus Walleijd377f562019-07-16 11:11:45 +02002810 if (ret)
2811 gpiod_dbg(desc, "%s: status %d\n", __func__, ret);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002812
Linus Walleijd377f562019-07-16 11:11:45 +02002813 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08002814}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002815
Linus Walleijfac9d882017-09-26 20:58:28 +02002816static bool gpiod_free_commit(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002817{
Mika Westerberg77c2d792014-03-10 14:54:50 +02002818 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08002819 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07002820 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002821
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07002822 might_sleep();
2823
Alexandre Courbot372e7222013-02-03 01:29:29 +09002824 gpiod_unexport(desc);
David Brownelld8f388d82008-07-25 01:46:07 -07002825
David Brownelld2876d02008-02-04 22:28:20 -08002826 spin_lock_irqsave(&gpio_lock, flags);
2827
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002828 chip = desc->gdev->chip;
David Brownell35e8bb52008-10-15 22:03:16 -07002829 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
2830 if (chip->free) {
2831 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07002832 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002833 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07002834 spin_lock_irqsave(&gpio_lock, flags);
2835 }
Muchun Song18534df2018-11-01 21:12:50 +08002836 kfree_const(desc->label);
David Brownelld2876d02008-02-04 22:28:20 -08002837 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08002838 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07002839 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302840 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302841 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Drew Fustini9225d512019-11-05 10:04:23 +08002842 clear_bit(FLAG_PULL_UP, &desc->flags);
2843 clear_bit(FLAG_PULL_DOWN, &desc->flags);
Kent Gibson2148ad72019-11-05 10:04:25 +08002844 clear_bit(FLAG_BIAS_DISABLE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06002845 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002846 ret = true;
2847 }
David Brownelld2876d02008-02-04 22:28:20 -08002848
2849 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002850 return ret;
2851}
2852
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002853void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002854{
Linus Walleijfac9d882017-09-26 20:58:28 +02002855 if (desc && desc->gdev && gpiod_free_commit(desc)) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002856 module_put(desc->gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002857 put_device(&desc->gdev->dev);
2858 } else {
Mika Westerberg77c2d792014-03-10 14:54:50 +02002859 WARN_ON(extra_checks);
Linus Walleij33a68e82016-02-11 10:28:44 +01002860 }
David Brownelld2876d02008-02-04 22:28:20 -08002861}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002862
David Brownelld2876d02008-02-04 22:28:20 -08002863/**
2864 * gpiochip_is_requested - return string iff signal was requested
2865 * @chip: controller managing the signal
2866 * @offset: of signal within controller's 0..(ngpio - 1) range
2867 *
2868 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09002869 * The string returned is the label passed to gpio_request(); if none has been
2870 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08002871 *
2872 * This function is for use by GPIO controller drivers. The label can
2873 * help with diagnostics, and knowing that the signal is used as a GPIO
2874 * can help avoid accidentally multiplexing it to another controller.
2875 */
2876const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
2877{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002878 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08002879
Dirk Behme48b59532015-08-18 18:02:32 +02002880 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08002881 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002882
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002883 desc = &chip->gpiodev->descs[offset];
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002884
Alexandre Courbot372e7222013-02-03 01:29:29 +09002885 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08002886 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002887 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08002888}
2889EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2890
Mika Westerberg77c2d792014-03-10 14:54:50 +02002891/**
2892 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
Thierry Reding950d55f52017-07-24 16:57:22 +02002893 * @chip: GPIO chip
2894 * @hwnum: hardware number of the GPIO for which to request the descriptor
Mika Westerberg77c2d792014-03-10 14:54:50 +02002895 * @label: label for the GPIO
Linus Walleij5923ea62019-04-26 14:40:18 +02002896 * @lflags: lookup flags for this GPIO or 0 if default, this can be used to
2897 * specify things like line inversion semantics with the machine flags
2898 * such as GPIO_OUT_LOW
2899 * @dflags: descriptor request flags for this GPIO or 0 if default, this
2900 * can be used to specify consumer semantics such as open drain
Mika Westerberg77c2d792014-03-10 14:54:50 +02002901 *
2902 * Function allows GPIO chip drivers to request and use their own GPIO
2903 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2904 * function will not increase reference count of the GPIO chip module. This
2905 * allows the GPIO chip module to be unloaded as needed (we assume that the
2906 * GPIO chip driver handles freeing the GPIOs it has requested).
Thierry Reding950d55f52017-07-24 16:57:22 +02002907 *
2908 * Returns:
2909 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2910 * code on failure.
Mika Westerberg77c2d792014-03-10 14:54:50 +02002911 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002912struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
Linus Walleij21abf102018-09-04 13:31:45 +02002913 const char *label,
Linus Walleij5923ea62019-04-26 14:40:18 +02002914 enum gpio_lookup_flags lflags,
2915 enum gpiod_flags dflags)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002916{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002917 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
Linus Walleijd377f562019-07-16 11:11:45 +02002918 int ret;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002919
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002920 if (IS_ERR(desc)) {
2921 chip_err(chip, "failed to get GPIO descriptor\n");
2922 return desc;
2923 }
2924
Linus Walleijd377f562019-07-16 11:11:45 +02002925 ret = gpiod_request_commit(desc, label);
2926 if (ret < 0)
2927 return ERR_PTR(ret);
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002928
Linus Walleijd377f562019-07-16 11:11:45 +02002929 ret = gpiod_configure_flags(desc, label, lflags, dflags);
2930 if (ret) {
Linus Walleij21abf102018-09-04 13:31:45 +02002931 chip_err(chip, "setup of own GPIO %s failed\n", label);
2932 gpiod_free_commit(desc);
Linus Walleijd377f562019-07-16 11:11:45 +02002933 return ERR_PTR(ret);
Linus Walleij21abf102018-09-04 13:31:45 +02002934 }
2935
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002936 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002937}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002938EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002939
2940/**
2941 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2942 * @desc: GPIO descriptor to free
2943 *
2944 * Function frees the given GPIO requested previously with
2945 * gpiochip_request_own_desc().
2946 */
2947void gpiochip_free_own_desc(struct gpio_desc *desc)
2948{
2949 if (desc)
Linus Walleijfac9d882017-09-26 20:58:28 +02002950 gpiod_free_commit(desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002951}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002952EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08002953
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002954/*
2955 * Drivers MUST set GPIO direction before making get/set calls. In
David Brownelld2876d02008-02-04 22:28:20 -08002956 * some cases this is done in early boot, before IRQs are enabled.
2957 *
2958 * As a rule these aren't called more than once (except for drivers
2959 * using the open-drain emulation idiom) so these are natural places
2960 * to accumulate extra debugging checks. Note that we can't (yet)
2961 * rely on gpio_request() having been called beforehand.
2962 */
2963
Thomas Petazzoni71479782019-02-07 17:28:56 +01002964static int gpio_set_config(struct gpio_chip *gc, unsigned offset,
2965 enum pin_config_param mode)
2966{
Maxime Ripard542f3612019-03-14 20:32:50 +01002967 unsigned long config;
2968 unsigned arg;
Thomas Petazzoni71479782019-02-07 17:28:56 +01002969
Maxime Ripard542f3612019-03-14 20:32:50 +01002970 switch (mode) {
Kent Gibson2148ad72019-11-05 10:04:25 +08002971 case PIN_CONFIG_BIAS_DISABLE:
Maxime Ripard542f3612019-03-14 20:32:50 +01002972 case PIN_CONFIG_BIAS_PULL_DOWN:
2973 case PIN_CONFIG_BIAS_PULL_UP:
2974 arg = 1;
2975 break;
2976
2977 default:
2978 arg = 0;
2979 }
2980
2981 config = PIN_CONF_PACKED(mode, arg);
Thomas Petazzoni71479782019-02-07 17:28:56 +01002982 return gc->set_config ? gc->set_config(gc, offset, config) : -ENOTSUPP;
2983}
2984
Kent Gibson2148ad72019-11-05 10:04:25 +08002985static int gpio_set_bias(struct gpio_chip *chip, struct gpio_desc *desc)
2986{
2987 int bias = 0;
2988 int ret = 0;
2989
2990 if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
2991 bias = PIN_CONFIG_BIAS_DISABLE;
2992 else if (test_bit(FLAG_PULL_UP, &desc->flags))
2993 bias = PIN_CONFIG_BIAS_PULL_UP;
2994 else if (test_bit(FLAG_PULL_DOWN, &desc->flags))
2995 bias = PIN_CONFIG_BIAS_PULL_DOWN;
2996
2997 if (bias) {
2998 ret = gpio_set_config(chip, gpio_chip_hwgpio(desc), bias);
2999 if (ret != -ENOTSUPP)
3000 return ret;
3001 }
3002 return 0;
3003}
3004
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003005/**
3006 * gpiod_direction_input - set the GPIO direction to input
3007 * @desc: GPIO to set to input
3008 *
3009 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
3010 * be called safely on it.
3011 *
3012 * Return 0 in case of success, else an error code.
3013 */
3014int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08003015{
David Brownelld2876d02008-02-04 22:28:20 -08003016 struct gpio_chip *chip;
Linus Walleijd377f562019-07-16 11:11:45 +02003017 int ret = 0;
David Brownelld2876d02008-02-04 22:28:20 -08003018
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003019 VALIDATE_DESC(desc);
3020 chip = desc->gdev->chip;
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09003021
Linus Walleije48d1942018-09-25 09:54:14 +02003022 /*
3023 * It is legal to have no .get() and .direction_input() specified if
3024 * the chip is output-only, but you can't specify .direction_input()
3025 * and not support the .get() operation, that doesn't make sense.
3026 */
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003027 if (!chip->get && chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01003028 gpiod_warn(desc,
Linus Walleije48d1942018-09-25 09:54:14 +02003029 "%s: missing get() but have direction_input()\n",
3030 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02003031 return -EIO;
3032 }
3033
Linus Walleije48d1942018-09-25 09:54:14 +02003034 /*
3035 * If we have a .direction_input() callback, things are simple,
3036 * just call it. Else we are some input-only chip so try to check the
3037 * direction (if .get_direction() is supported) else we silently
3038 * assume we are in input mode after this.
3039 */
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003040 if (chip->direction_input) {
Linus Walleijd377f562019-07-16 11:11:45 +02003041 ret = chip->direction_input(chip, gpio_chip_hwgpio(desc));
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003042 } else if (chip->get_direction &&
3043 (chip->get_direction(chip, gpio_chip_hwgpio(desc)) != 1)) {
3044 gpiod_warn(desc,
Linus Walleije48d1942018-09-25 09:54:14 +02003045 "%s: missing direction_input() operation and line is output\n",
3046 __func__);
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003047 return -EIO;
3048 }
Kent Gibson2148ad72019-11-05 10:04:25 +08003049 if (ret == 0) {
David Brownelld2876d02008-02-04 22:28:20 -08003050 clear_bit(FLAG_IS_OUT, &desc->flags);
Kent Gibson2148ad72019-11-05 10:04:25 +08003051 ret = gpio_set_bias(chip, desc);
3052 }
Thomas Petazzonid4499912019-02-07 17:28:58 +01003053
Linus Walleijd377f562019-07-16 11:11:45 +02003054 trace_gpio_direction(desc_to_gpio(desc), 1, ret);
Alexandre Courbotd82da792014-07-22 16:17:43 +09003055
Linus Walleijd377f562019-07-16 11:11:45 +02003056 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08003057}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003058EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003059
Linus Walleijfac9d882017-09-26 20:58:28 +02003060static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08003061{
Linus Walleijc663e5f2016-03-22 10:51:16 +01003062 struct gpio_chip *gc = desc->gdev->chip;
Linus Walleijad177312016-11-13 23:02:44 +01003063 int val = !!value;
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003064 int ret = 0;
David Brownelld2876d02008-02-04 22:28:20 -08003065
Linus Walleije48d1942018-09-25 09:54:14 +02003066 /*
3067 * It's OK not to specify .direction_output() if the gpiochip is
3068 * output-only, but if there is then not even a .set() operation it
3069 * is pretty tricky to drive the output line.
3070 */
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003071 if (!gc->set && !gc->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01003072 gpiod_warn(desc,
Linus Walleije48d1942018-09-25 09:54:14 +02003073 "%s: missing set() and direction_output() operations\n",
3074 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02003075 return -EIO;
3076 }
3077
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003078 if (gc->direction_output) {
3079 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
3080 } else {
Linus Walleije48d1942018-09-25 09:54:14 +02003081 /* Check that we are in output mode if we can */
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003082 if (gc->get_direction &&
3083 gc->get_direction(gc, gpio_chip_hwgpio(desc))) {
3084 gpiod_warn(desc,
3085 "%s: missing direction_output() operation\n",
3086 __func__);
3087 return -EIO;
3088 }
Linus Walleije48d1942018-09-25 09:54:14 +02003089 /*
3090 * If we can't actively set the direction, we are some
3091 * output-only chip, so just drive the output as desired.
3092 */
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003093 gc->set(gc, gpio_chip_hwgpio(desc), val);
3094 }
3095
Linus Walleijc663e5f2016-03-22 10:51:16 +01003096 if (!ret)
David Brownelld2876d02008-02-04 22:28:20 -08003097 set_bit(FLAG_IS_OUT, &desc->flags);
Linus Walleijad177312016-11-13 23:02:44 +01003098 trace_gpio_value(desc_to_gpio(desc), 0, val);
Linus Walleijc663e5f2016-03-22 10:51:16 +01003099 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
3100 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08003101}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01003102
3103/**
3104 * gpiod_direction_output_raw - set the GPIO direction to output
3105 * @desc: GPIO to set to output
3106 * @value: initial output value of the GPIO
3107 *
3108 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
3109 * be called safely on it. The initial value of the output must be specified
3110 * as raw value on the physical line without regard for the ACTIVE_LOW status.
3111 *
3112 * Return 0 in case of success, else an error code.
3113 */
3114int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
3115{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003116 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003117 return gpiod_direction_output_raw_commit(desc, value);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01003118}
3119EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
3120
3121/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05303122 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01003123 * @desc: GPIO to set to output
3124 * @value: initial output value of the GPIO
3125 *
3126 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
3127 * be called safely on it. The initial value of the output must be specified
3128 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3129 * account.
3130 *
3131 * Return 0 in case of success, else an error code.
3132 */
3133int gpiod_direction_output(struct gpio_desc *desc, int value)
3134{
Vladimir Zapolskiy30322bc2017-12-21 18:37:24 +02003135 struct gpio_chip *gc;
Linus Walleij02e47982017-09-26 21:20:23 +02003136 int ret;
3137
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003138 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01003139 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3140 value = !value;
Linus Walleijad177312016-11-13 23:02:44 +01003141 else
3142 value = !!value;
Linus Walleij02e47982017-09-26 21:20:23 +02003143
Hans Verkuil4e9439d2018-09-08 11:23:16 +02003144 /* GPIOs used for enabled IRQs shall not be set as output */
3145 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags) &&
3146 test_bit(FLAG_IRQ_IS_ENABLED, &desc->flags)) {
Linus Walleij02e47982017-09-26 21:20:23 +02003147 gpiod_err(desc,
3148 "%s: tried to set a GPIO tied to an IRQ as output\n",
3149 __func__);
3150 return -EIO;
3151 }
3152
Vladimir Zapolskiy30322bc2017-12-21 18:37:24 +02003153 gc = desc->gdev->chip;
Linus Walleij02e47982017-09-26 21:20:23 +02003154 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
3155 /* First see if we can enable open drain in hardware */
Thomas Petazzoni71479782019-02-07 17:28:56 +01003156 ret = gpio_set_config(gc, gpio_chip_hwgpio(desc),
3157 PIN_CONFIG_DRIVE_OPEN_DRAIN);
Linus Walleij02e47982017-09-26 21:20:23 +02003158 if (!ret)
3159 goto set_output_value;
3160 /* Emulate open drain by not actively driving the line high */
Bartosz Golaszewskie7352442019-10-01 11:44:53 +02003161 if (value) {
3162 ret = gpiod_direction_input(desc);
3163 goto set_output_flag;
3164 }
Linus Walleij02e47982017-09-26 21:20:23 +02003165 }
3166 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
Thomas Petazzoni71479782019-02-07 17:28:56 +01003167 ret = gpio_set_config(gc, gpio_chip_hwgpio(desc),
3168 PIN_CONFIG_DRIVE_OPEN_SOURCE);
Linus Walleij02e47982017-09-26 21:20:23 +02003169 if (!ret)
3170 goto set_output_value;
3171 /* Emulate open source by not actively driving the line low */
Bartosz Golaszewskie7352442019-10-01 11:44:53 +02003172 if (!value) {
3173 ret = gpiod_direction_input(desc);
3174 goto set_output_flag;
3175 }
Linus Walleij02e47982017-09-26 21:20:23 +02003176 } else {
Thomas Petazzoni71479782019-02-07 17:28:56 +01003177 gpio_set_config(gc, gpio_chip_hwgpio(desc),
3178 PIN_CONFIG_DRIVE_PUSH_PULL);
Linus Walleij02e47982017-09-26 21:20:23 +02003179 }
3180
3181set_output_value:
Kent Gibson2821ae52019-11-05 10:04:26 +08003182 ret = gpio_set_bias(gc, desc);
3183 if (ret)
3184 return ret;
Linus Walleijfac9d882017-09-26 20:58:28 +02003185 return gpiod_direction_output_raw_commit(desc, value);
Bartosz Golaszewskie7352442019-10-01 11:44:53 +02003186
3187set_output_flag:
3188 /*
3189 * When emulating open-source or open-drain functionalities by not
3190 * actively driving the line (setting mode to input) we still need to
3191 * set the IS_OUT flag or otherwise we won't be able to set the line
3192 * value anymore.
3193 */
3194 if (ret == 0)
3195 set_bit(FLAG_IS_OUT, &desc->flags);
3196 return ret;
Philipp Zabelef70bbe2014-01-07 12:34:11 +01003197}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003198EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08003199
Felipe Balbic4b5be92010-05-26 14:42:23 -07003200/**
Thierry Reding950d55f52017-07-24 16:57:22 +02003201 * gpiod_set_debounce - sets @debounce time for a GPIO
3202 * @desc: descriptor of the GPIO for which to set debounce time
3203 * @debounce: debounce time in microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02003204 *
Thierry Reding950d55f52017-07-24 16:57:22 +02003205 * Returns:
3206 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
3207 * debounce time.
Felipe Balbic4b5be92010-05-26 14:42:23 -07003208 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003209int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07003210{
Felipe Balbic4b5be92010-05-26 14:42:23 -07003211 struct gpio_chip *chip;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03003212 unsigned long config;
Felipe Balbic4b5be92010-05-26 14:42:23 -07003213
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003214 VALIDATE_DESC(desc);
3215 chip = desc->gdev->chip;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03003216 if (!chip->set || !chip->set_config) {
Mark Brown6424de52013-09-09 10:33:49 +01003217 gpiod_dbg(desc,
Mika Westerberg2956b5d2017-01-23 15:34:34 +03003218 "%s: missing set() or set_config() operations\n",
Mark Brown6424de52013-09-09 10:33:49 +01003219 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02003220 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02003221 }
3222
Mika Westerberg2956b5d2017-01-23 15:34:34 +03003223 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
Andrew Jefferyfa59dd232019-03-26 15:19:54 +10303224 return chip->set_config(chip, gpio_chip_hwgpio(desc), config);
Felipe Balbic4b5be92010-05-26 14:42:23 -07003225}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003226EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003227
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003228/**
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303229 * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
3230 * @desc: descriptor of the GPIO for which to configure persistence
3231 * @transitory: True to lose state on suspend or reset, false for persistence
3232 *
3233 * Returns:
3234 * 0 on success, otherwise a negative error code.
3235 */
3236int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
3237{
3238 struct gpio_chip *chip;
3239 unsigned long packed;
3240 int gpio;
3241 int rc;
3242
Vladimir Zapolskiy156dd392017-12-21 18:37:35 +02003243 VALIDATE_DESC(desc);
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303244 /*
3245 * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
3246 * persistence state.
3247 */
3248 if (transitory)
3249 set_bit(FLAG_TRANSITORY, &desc->flags);
3250 else
3251 clear_bit(FLAG_TRANSITORY, &desc->flags);
3252
3253 /* If the driver supports it, set the persistence state now */
3254 chip = desc->gdev->chip;
3255 if (!chip->set_config)
3256 return 0;
3257
3258 packed = pinconf_to_config_packed(PIN_CONFIG_PERSIST_STATE,
3259 !transitory);
3260 gpio = gpio_chip_hwgpio(desc);
Andrew Jefferyfa59dd232019-03-26 15:19:54 +10303261 rc = chip->set_config(chip, gpio, packed);
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303262 if (rc == -ENOTSUPP) {
3263 dev_dbg(&desc->gdev->dev, "Persistence not supported for GPIO %d\n",
3264 gpio);
3265 return 0;
3266 }
3267
3268 return rc;
3269}
3270EXPORT_SYMBOL_GPL(gpiod_set_transitory);
3271
3272/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003273 * gpiod_is_active_low - test whether a GPIO is active-low or not
3274 * @desc: the gpio descriptor to test
3275 *
3276 * Returns 1 if the GPIO is active-low, 0 otherwise.
3277 */
3278int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003279{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003280 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003281 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003282}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003283EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08003284
3285/* I/O calls are only valid after configuration completed; the relevant
3286 * "is this a valid GPIO" error checks should already have been done.
3287 *
3288 * "Get" operations are often inlinable as reading a pin value register,
3289 * and masking the relevant bit in that register.
3290 *
3291 * When "set" operations are inlinable, they involve writing that mask to
3292 * one register to set a low value, or a different register to set it high.
3293 * Otherwise locking is needed, so there may be little value to inlining.
3294 *
3295 *------------------------------------------------------------------------
3296 *
3297 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
3298 * have requested the GPIO. That can include implicit requesting by
3299 * a direction setting call. Marking a gpio as requested locks its chip
3300 * in memory, guaranteeing that these table lookups need no more locking
3301 * and that gpiochip_remove() will fail.
3302 *
3303 * REVISIT when debugging, consider adding some instrumentation to ensure
3304 * that the GPIO was actually requested.
3305 */
3306
Linus Walleijfac9d882017-09-26 20:58:28 +02003307static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08003308{
3309 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003310 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003311 int value;
David Brownelld2876d02008-02-04 22:28:20 -08003312
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003313 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003314 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003315 value = chip->get ? chip->get(chip, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01003316 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003317 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06003318 return value;
David Brownelld2876d02008-02-04 22:28:20 -08003319}
Alexandre Courbot372e7222013-02-03 01:29:29 +09003320
Lukas Wunnereec1d562017-10-12 12:40:10 +02003321static int gpio_chip_get_multiple(struct gpio_chip *chip,
3322 unsigned long *mask, unsigned long *bits)
3323{
3324 if (chip->get_multiple) {
3325 return chip->get_multiple(chip, mask, bits);
3326 } else if (chip->get) {
3327 int i, value;
3328
3329 for_each_set_bit(i, mask, chip->ngpio) {
3330 value = chip->get(chip, i);
3331 if (value < 0)
3332 return value;
3333 __assign_bit(i, bits, value);
3334 }
3335 return 0;
3336 }
3337 return -EIO;
3338}
3339
3340int gpiod_get_array_value_complex(bool raw, bool can_sleep,
3341 unsigned int array_size,
3342 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003343 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003344 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02003345{
Linus Walleijd377f562019-07-16 11:11:45 +02003346 int ret, i = 0;
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003347
3348 /*
3349 * Validate array_info against desc_array and its size.
3350 * It should immediately follow desc_array if both
3351 * have been obtained from the same gpiod_get_array() call.
3352 */
3353 if (array_info && array_info->desc == desc_array &&
3354 array_size <= array_info->size &&
3355 (void *)array_info == desc_array + array_info->size) {
3356 if (!can_sleep)
3357 WARN_ON(array_info->chip->can_sleep);
3358
Linus Walleijd377f562019-07-16 11:11:45 +02003359 ret = gpio_chip_get_multiple(array_info->chip,
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003360 array_info->get_mask,
3361 value_bitmap);
Linus Walleijd377f562019-07-16 11:11:45 +02003362 if (ret)
3363 return ret;
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003364
3365 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
3366 bitmap_xor(value_bitmap, value_bitmap,
3367 array_info->invert_mask, array_size);
3368
3369 if (bitmap_full(array_info->get_mask, array_size))
3370 return 0;
3371
3372 i = find_first_zero_bit(array_info->get_mask, array_size);
3373 } else {
3374 array_info = NULL;
3375 }
Lukas Wunnereec1d562017-10-12 12:40:10 +02003376
3377 while (i < array_size) {
3378 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Laura Abbott30277432018-05-21 10:57:07 -07003379 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
3380 unsigned long *mask, *bits;
Lukas Wunnereec1d562017-10-12 12:40:10 +02003381 int first, j, ret;
3382
Laura Abbott30277432018-05-21 10:57:07 -07003383 if (likely(chip->ngpio <= FASTPATH_NGPIO)) {
3384 mask = fastpath;
3385 } else {
3386 mask = kmalloc_array(2 * BITS_TO_LONGS(chip->ngpio),
3387 sizeof(*mask),
3388 can_sleep ? GFP_KERNEL : GFP_ATOMIC);
3389 if (!mask)
3390 return -ENOMEM;
3391 }
3392
3393 bits = mask + BITS_TO_LONGS(chip->ngpio);
3394 bitmap_zero(mask, chip->ngpio);
3395
Lukas Wunnereec1d562017-10-12 12:40:10 +02003396 if (!can_sleep)
3397 WARN_ON(chip->can_sleep);
3398
3399 /* collect all inputs belonging to the same chip */
3400 first = i;
Lukas Wunnereec1d562017-10-12 12:40:10 +02003401 do {
3402 const struct gpio_desc *desc = desc_array[i];
3403 int hwgpio = gpio_chip_hwgpio(desc);
3404
3405 __set_bit(hwgpio, mask);
3406 i++;
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003407
3408 if (array_info)
Janusz Krzysztofik35ae7f92018-09-24 01:53:35 +02003409 i = find_next_zero_bit(array_info->get_mask,
3410 array_size, i);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003411 } while ((i < array_size) &&
3412 (desc_array[i]->gdev->chip == chip));
3413
3414 ret = gpio_chip_get_multiple(chip, mask, bits);
Laura Abbott30277432018-05-21 10:57:07 -07003415 if (ret) {
3416 if (mask != fastpath)
3417 kfree(mask);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003418 return ret;
Laura Abbott30277432018-05-21 10:57:07 -07003419 }
Lukas Wunnereec1d562017-10-12 12:40:10 +02003420
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003421 for (j = first; j < i; ) {
Lukas Wunnereec1d562017-10-12 12:40:10 +02003422 const struct gpio_desc *desc = desc_array[j];
3423 int hwgpio = gpio_chip_hwgpio(desc);
3424 int value = test_bit(hwgpio, bits);
3425
3426 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3427 value = !value;
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003428 __assign_bit(j, value_bitmap, value);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003429 trace_gpio_value(desc_to_gpio(desc), 1, value);
Janusz Krzysztofik799d5eb2018-09-29 14:20:22 +02003430 j++;
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003431
3432 if (array_info)
Janusz Krzysztofik35ae7f92018-09-24 01:53:35 +02003433 j = find_next_zero_bit(array_info->get_mask, i,
3434 j);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003435 }
Laura Abbott30277432018-05-21 10:57:07 -07003436
3437 if (mask != fastpath)
3438 kfree(mask);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003439 }
3440 return 0;
3441}
3442
David Brownelld2876d02008-02-04 22:28:20 -08003443/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003444 * gpiod_get_raw_value() - return a gpio's raw value
3445 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08003446 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003447 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003448 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003449 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003450 * This function can be called from contexts where we cannot sleep, and will
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003451 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003452 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003453int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003454{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003455 VALIDATE_DESC(desc);
Geert Uytterhoeven3285170f2019-07-01 16:27:38 +02003456 /* Should be using gpiod_get_raw_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003457 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02003458 return gpiod_get_raw_value_commit(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003459}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003460EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08003461
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003462/**
3463 * gpiod_get_value() - return a gpio's value
3464 * @desc: gpio whose value will be returned
3465 *
3466 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003467 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003468 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003469 * This function can be called from contexts where we cannot sleep, and will
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003470 * complain if the GPIO chip functions potentially sleep.
3471 */
3472int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08003473{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003474 int value;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003475
3476 VALIDATE_DESC(desc);
Geert Uytterhoeven3285170f2019-07-01 16:27:38 +02003477 /* Should be using gpiod_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003478 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003479
Linus Walleijfac9d882017-09-26 20:58:28 +02003480 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003481 if (value < 0)
3482 return value;
3483
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003484 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3485 value = !value;
3486
3487 return value;
David Brownelld2876d02008-02-04 22:28:20 -08003488}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003489EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08003490
Lukas Wunnereec1d562017-10-12 12:40:10 +02003491/**
3492 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003493 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02003494 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003495 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003496 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02003497 *
3498 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3499 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3500 * else an error code.
3501 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003502 * This function can be called from contexts where we cannot sleep,
Lukas Wunnereec1d562017-10-12 12:40:10 +02003503 * and it will complain if the GPIO chip functions potentially sleep.
3504 */
3505int gpiod_get_raw_array_value(unsigned int array_size,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003506 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003507 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003508 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02003509{
3510 if (!desc_array)
3511 return -EINVAL;
3512 return gpiod_get_array_value_complex(true, false, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003513 desc_array, array_info,
3514 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003515}
3516EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
3517
3518/**
3519 * gpiod_get_array_value() - read values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003520 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02003521 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003522 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003523 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02003524 *
3525 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3526 * into account. Return 0 in case of success, else an error code.
3527 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003528 * This function can be called from contexts where we cannot sleep,
Lukas Wunnereec1d562017-10-12 12:40:10 +02003529 * and it will complain if the GPIO chip functions potentially sleep.
3530 */
3531int gpiod_get_array_value(unsigned int array_size,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003532 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003533 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003534 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02003535{
3536 if (!desc_array)
3537 return -EINVAL;
3538 return gpiod_get_array_value_complex(false, false, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003539 desc_array, array_info,
3540 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003541}
3542EXPORT_SYMBOL_GPL(gpiod_get_array_value);
3543
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303544/*
Linus Walleijfac9d882017-09-26 20:58:28 +02003545 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003546 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07003547 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303548 */
Linus Walleijfac9d882017-09-26 20:58:28 +02003549static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303550{
Linus Walleijd377f562019-07-16 11:11:45 +02003551 int ret = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003552 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003553 int offset = gpio_chip_hwgpio(desc);
3554
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303555 if (value) {
Linus Walleijd377f562019-07-16 11:11:45 +02003556 ret = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303557 } else {
Linus Walleijd377f562019-07-16 11:11:45 +02003558 ret = chip->direction_output(chip, offset, 0);
3559 if (!ret)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003560 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303561 }
Linus Walleijd377f562019-07-16 11:11:45 +02003562 trace_gpio_direction(desc_to_gpio(desc), value, ret);
3563 if (ret < 0)
Mark Brown6424de52013-09-09 10:33:49 +01003564 gpiod_err(desc,
3565 "%s: Error in set_value for open drain err %d\n",
Linus Walleijd377f562019-07-16 11:11:45 +02003566 __func__, ret);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303567}
3568
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303569/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003570 * _gpio_set_open_source_value() - Set the open source gpio's value.
3571 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07003572 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303573 */
Linus Walleijfac9d882017-09-26 20:58:28 +02003574static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303575{
Linus Walleijd377f562019-07-16 11:11:45 +02003576 int ret = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003577 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003578 int offset = gpio_chip_hwgpio(desc);
3579
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303580 if (value) {
Linus Walleijd377f562019-07-16 11:11:45 +02003581 ret = chip->direction_output(chip, offset, 1);
3582 if (!ret)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003583 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303584 } else {
Linus Walleijd377f562019-07-16 11:11:45 +02003585 ret = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303586 }
Linus Walleijd377f562019-07-16 11:11:45 +02003587 trace_gpio_direction(desc_to_gpio(desc), !value, ret);
3588 if (ret < 0)
Mark Brown6424de52013-09-09 10:33:49 +01003589 gpiod_err(desc,
3590 "%s: Error in set_value for open source err %d\n",
Linus Walleijd377f562019-07-16 11:11:45 +02003591 __func__, ret);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303592}
3593
Linus Walleijfac9d882017-09-26 20:58:28 +02003594static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08003595{
3596 struct gpio_chip *chip;
3597
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003598 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003599 trace_gpio_value(desc_to_gpio(desc), 0, value);
Linus Walleij02e47982017-09-26 21:20:23 +02003600 chip->set(chip, gpio_chip_hwgpio(desc), value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003601}
3602
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003603/*
3604 * set multiple outputs on the same chip;
3605 * use the chip's set_multiple function if available;
3606 * otherwise set the outputs sequentially;
3607 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
3608 * defines which outputs are to be changed
3609 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
3610 * defines the values the outputs specified by mask are to be set to
3611 */
3612static void gpio_chip_set_multiple(struct gpio_chip *chip,
3613 unsigned long *mask, unsigned long *bits)
3614{
3615 if (chip->set_multiple) {
3616 chip->set_multiple(chip, mask, bits);
3617 } else {
Andy Shevchenko5e4e6fb2017-01-03 19:01:17 +02003618 unsigned int i;
3619
3620 /* set outputs if the corresponding mask bit is set */
3621 for_each_set_bit(i, mask, chip->ngpio)
3622 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003623 }
3624}
3625
Laura Abbott30277432018-05-21 10:57:07 -07003626int gpiod_set_array_value_complex(bool raw, bool can_sleep,
Geert Uytterhoeven3c940662018-09-27 13:38:10 +02003627 unsigned int array_size,
3628 struct gpio_desc **desc_array,
3629 struct gpio_array *array_info,
3630 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003631{
3632 int i = 0;
3633
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003634 /*
3635 * Validate array_info against desc_array and its size.
3636 * It should immediately follow desc_array if both
3637 * have been obtained from the same gpiod_get_array() call.
3638 */
3639 if (array_info && array_info->desc == desc_array &&
3640 array_size <= array_info->size &&
3641 (void *)array_info == desc_array + array_info->size) {
3642 if (!can_sleep)
3643 WARN_ON(array_info->chip->can_sleep);
3644
3645 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
3646 bitmap_xor(value_bitmap, value_bitmap,
3647 array_info->invert_mask, array_size);
3648
3649 gpio_chip_set_multiple(array_info->chip, array_info->set_mask,
3650 value_bitmap);
3651
3652 if (bitmap_full(array_info->set_mask, array_size))
3653 return 0;
3654
3655 i = find_first_zero_bit(array_info->set_mask, array_size);
3656 } else {
3657 array_info = NULL;
3658 }
3659
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003660 while (i < array_size) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003661 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Laura Abbott30277432018-05-21 10:57:07 -07003662 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
3663 unsigned long *mask, *bits;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003664 int count = 0;
3665
Laura Abbott30277432018-05-21 10:57:07 -07003666 if (likely(chip->ngpio <= FASTPATH_NGPIO)) {
3667 mask = fastpath;
3668 } else {
3669 mask = kmalloc_array(2 * BITS_TO_LONGS(chip->ngpio),
3670 sizeof(*mask),
3671 can_sleep ? GFP_KERNEL : GFP_ATOMIC);
3672 if (!mask)
3673 return -ENOMEM;
3674 }
3675
3676 bits = mask + BITS_TO_LONGS(chip->ngpio);
3677 bitmap_zero(mask, chip->ngpio);
3678
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003679 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003680 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003681
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003682 do {
3683 struct gpio_desc *desc = desc_array[i];
3684 int hwgpio = gpio_chip_hwgpio(desc);
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003685 int value = test_bit(i, value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003686
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003687 /*
3688 * Pins applicable for fast input but not for
3689 * fast output processing may have been already
3690 * inverted inside the fast path, skip them.
3691 */
3692 if (!raw && !(array_info &&
3693 test_bit(i, array_info->invert_mask)) &&
3694 test_bit(FLAG_ACTIVE_LOW, &desc->flags))
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003695 value = !value;
3696 trace_gpio_value(desc_to_gpio(desc), 0, value);
3697 /*
3698 * collect all normal outputs belonging to the same chip
3699 * open drain and open source outputs are set individually
3700 */
Linus Walleij02e47982017-09-26 21:20:23 +02003701 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02003702 gpio_set_open_drain_value_commit(desc, value);
Linus Walleij02e47982017-09-26 21:20:23 +02003703 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02003704 gpio_set_open_source_value_commit(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003705 } else {
3706 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003707 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003708 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003709 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003710 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003711 count++;
3712 }
3713 i++;
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003714
3715 if (array_info)
Janusz Krzysztofik35ae7f92018-09-24 01:53:35 +02003716 i = find_next_zero_bit(array_info->set_mask,
3717 array_size, i);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003718 } while ((i < array_size) &&
3719 (desc_array[i]->gdev->chip == chip));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003720 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003721 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003722 gpio_chip_set_multiple(chip, mask, bits);
Laura Abbott30277432018-05-21 10:57:07 -07003723
3724 if (mask != fastpath)
3725 kfree(mask);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003726 }
Laura Abbott30277432018-05-21 10:57:07 -07003727 return 0;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003728}
3729
David Brownelld2876d02008-02-04 22:28:20 -08003730/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003731 * gpiod_set_raw_value() - assign a gpio's raw value
3732 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08003733 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08003734 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003735 * Set the raw value of the GPIO, i.e. the value of its physical line without
3736 * regard for its ACTIVE_LOW status.
3737 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003738 * This function can be called from contexts where we cannot sleep, and will
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003739 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003740 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003741void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003742{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003743 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven3285170f2019-07-01 16:27:38 +02003744 /* Should be using gpiod_set_raw_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003745 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02003746 gpiod_set_raw_value_commit(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08003747}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003748EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08003749
3750/**
Geert Uytterhoeven1e77fc82018-01-09 19:08:21 +01003751 * gpiod_set_value_nocheck() - set a GPIO line value without checking
3752 * @desc: the descriptor to set the value on
3753 * @value: value to set
3754 *
3755 * This sets the value of a GPIO line backing a descriptor, applying
3756 * different semantic quirks like active low and open drain/source
3757 * handling.
3758 */
3759static void gpiod_set_value_nocheck(struct gpio_desc *desc, int value)
3760{
3761 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3762 value = !value;
3763 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
3764 gpio_set_open_drain_value_commit(desc, value);
3765 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
3766 gpio_set_open_source_value_commit(desc, value);
3767 else
3768 gpiod_set_raw_value_commit(desc, value);
3769}
3770
3771/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003772 * gpiod_set_value() - assign a gpio's value
3773 * @desc: gpio whose value will be assigned
3774 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08003775 *
Linus Walleij02e47982017-09-26 21:20:23 +02003776 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
3777 * OPEN_DRAIN and OPEN_SOURCE flags into account.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003778 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003779 * This function can be called from contexts where we cannot sleep, and will
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003780 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003781 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003782void gpiod_set_value(struct gpio_desc *desc, int value)
3783{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003784 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven3285170f2019-07-01 16:27:38 +02003785 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003786 WARN_ON(desc->gdev->chip->can_sleep);
Geert Uytterhoeven1e77fc82018-01-09 19:08:21 +01003787 gpiod_set_value_nocheck(desc, value);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003788}
3789EXPORT_SYMBOL_GPL(gpiod_set_value);
3790
3791/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003792 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003793 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003794 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003795 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003796 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003797 *
3798 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3799 * without regard for their ACTIVE_LOW status.
3800 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003801 * This function can be called from contexts where we cannot sleep, and will
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003802 * complain if the GPIO chip functions potentially sleep.
3803 */
Laura Abbott30277432018-05-21 10:57:07 -07003804int gpiod_set_raw_array_value(unsigned int array_size,
Geert Uytterhoeven3c940662018-09-27 13:38:10 +02003805 struct gpio_desc **desc_array,
3806 struct gpio_array *array_info,
3807 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003808{
3809 if (!desc_array)
Laura Abbott30277432018-05-21 10:57:07 -07003810 return -EINVAL;
3811 return gpiod_set_array_value_complex(true, false, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003812 desc_array, array_info, value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003813}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003814EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003815
3816/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003817 * gpiod_set_array_value() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003818 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003819 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003820 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003821 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003822 *
3823 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3824 * into account.
3825 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003826 * This function can be called from contexts where we cannot sleep, and will
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003827 * complain if the GPIO chip functions potentially sleep.
3828 */
Geert Uytterhoevencf9af0d2018-09-27 13:38:09 +02003829int gpiod_set_array_value(unsigned int array_size,
3830 struct gpio_desc **desc_array,
3831 struct gpio_array *array_info,
3832 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003833{
3834 if (!desc_array)
Geert Uytterhoevencf9af0d2018-09-27 13:38:09 +02003835 return -EINVAL;
3836 return gpiod_set_array_value_complex(false, false, array_size,
3837 desc_array, array_info,
3838 value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003839}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003840EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003841
3842/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003843 * gpiod_cansleep() - report whether gpio value access may sleep
3844 * @desc: gpio to check
3845 *
3846 */
3847int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003848{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003849 VALIDATE_DESC(desc);
3850 return desc->gdev->chip->can_sleep;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003851}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003852EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08003853
David Brownell0f6d5042008-10-15 22:03:14 -07003854/**
Linus Walleij90b39402e2018-06-01 13:21:27 +02003855 * gpiod_set_consumer_name() - set the consumer name for the descriptor
3856 * @desc: gpio to set the consumer name on
3857 * @name: the new consumer name
3858 */
Muchun Song18534df2018-11-01 21:12:50 +08003859int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name)
Linus Walleij90b39402e2018-06-01 13:21:27 +02003860{
Muchun Song18534df2018-11-01 21:12:50 +08003861 VALIDATE_DESC(desc);
3862 if (name) {
3863 name = kstrdup_const(name, GFP_KERNEL);
3864 if (!name)
3865 return -ENOMEM;
3866 }
3867
3868 kfree_const(desc->label);
3869 desc_set_label(desc, name);
3870
3871 return 0;
Linus Walleij90b39402e2018-06-01 13:21:27 +02003872}
3873EXPORT_SYMBOL_GPL(gpiod_set_consumer_name);
3874
3875/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003876 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
3877 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07003878 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003879 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
3880 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07003881 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003882int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07003883{
Linus Walleij4c37ce82016-05-02 13:13:10 +02003884 struct gpio_chip *chip;
3885 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07003886
Linus Walleij79bb71b2016-06-15 22:57:38 +02003887 /*
3888 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
3889 * requires this function to not return zero on an invalid descriptor
3890 * but rather a negative error number.
3891 */
Linus Walleijbfbbe442016-06-16 11:55:55 +02003892 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
Linus Walleij79bb71b2016-06-15 22:57:38 +02003893 return -EINVAL;
3894
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003895 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003896 offset = gpio_chip_hwgpio(desc);
Linus Walleij4c37ce82016-05-02 13:13:10 +02003897 if (chip->to_irq) {
3898 int retirq = chip->to_irq(chip, offset);
3899
3900 /* Zero means NO_IRQ */
3901 if (!retirq)
3902 return -ENXIO;
3903
3904 return retirq;
3905 }
3906 return -ENXIO;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003907}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003908EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003909
Linus Walleijd468bf92013-09-24 11:54:38 +02003910/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003911 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003912 * @chip: the chip the GPIO to lock belongs to
3913 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02003914 *
3915 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08003916 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08003917 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003918int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08003919{
Linus Walleij9c102802016-05-25 10:56:03 +02003920 struct gpio_desc *desc;
Linus Walleijd468bf92013-09-24 11:54:38 +02003921
Linus Walleij9c102802016-05-25 10:56:03 +02003922 desc = gpiochip_get_desc(chip, offset);
3923 if (IS_ERR(desc))
3924 return PTR_ERR(desc);
3925
Linus Walleij60f83392016-11-12 15:01:09 +01003926 /*
3927 * If it's fast: flush the direction setting if something changed
3928 * behind our back
3929 */
3930 if (!chip->can_sleep && chip->get_direction) {
Andy Shevchenko80956792018-07-09 21:47:21 +03003931 int dir = gpiod_get_direction(desc);
Linus Walleij9c102802016-05-25 10:56:03 +02003932
Andy Shevchenko36b31272018-07-03 03:38:31 +03003933 if (dir < 0) {
3934 chip_err(chip, "%s: cannot get GPIO direction\n",
3935 __func__);
3936 return dir;
3937 }
Linus Walleij9c102802016-05-25 10:56:03 +02003938 }
3939
3940 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003941 chip_err(chip,
Andy Shevchenkob1911712018-07-03 03:39:03 +03003942 "%s: tried to flag a GPIO set as output for IRQ\n",
3943 __func__);
Linus Walleijd468bf92013-09-24 11:54:38 +02003944 return -EIO;
3945 }
3946
Linus Walleij9c102802016-05-25 10:56:03 +02003947 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
Hans Verkuil4e9439d2018-09-08 11:23:16 +02003948 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
Linus Walleij3940c342016-11-14 00:09:07 +01003949
3950 /*
3951 * If the consumer has not set up a label (such as when the
3952 * IRQ is referenced from .to_irq()) we set up a label here
3953 * so it is clear this is used as an interrupt.
3954 */
3955 if (!desc->label)
3956 desc_set_label(desc, "interrupt");
3957
Linus Walleijd468bf92013-09-24 11:54:38 +02003958 return 0;
3959}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003960EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02003961
Linus Walleijd468bf92013-09-24 11:54:38 +02003962/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003963 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003964 * @chip: the chip the GPIO to lock belongs to
3965 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02003966 *
3967 * This is used directly by GPIO drivers that want to indicate
3968 * that a certain GPIO is no longer used exclusively for IRQ.
3969 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003970void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02003971{
Linus Walleij3940c342016-11-14 00:09:07 +01003972 struct gpio_desc *desc;
3973
3974 desc = gpiochip_get_desc(chip, offset);
3975 if (IS_ERR(desc))
Linus Walleijd468bf92013-09-24 11:54:38 +02003976 return;
3977
Linus Walleij3940c342016-11-14 00:09:07 +01003978 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
Hans Verkuil4e9439d2018-09-08 11:23:16 +02003979 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
Linus Walleij3940c342016-11-14 00:09:07 +01003980
3981 /* If we only had this marking, erase it */
3982 if (desc->label && !strcmp(desc->label, "interrupt"))
3983 desc_set_label(desc, NULL);
Linus Walleijd468bf92013-09-24 11:54:38 +02003984}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003985EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02003986
Hans Verkuil4e9439d2018-09-08 11:23:16 +02003987void gpiochip_disable_irq(struct gpio_chip *chip, unsigned int offset)
3988{
3989 struct gpio_desc *desc = gpiochip_get_desc(chip, offset);
3990
3991 if (!IS_ERR(desc) &&
3992 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags)))
3993 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3994}
3995EXPORT_SYMBOL_GPL(gpiochip_disable_irq);
3996
3997void gpiochip_enable_irq(struct gpio_chip *chip, unsigned int offset)
3998{
3999 struct gpio_desc *desc = gpiochip_get_desc(chip, offset);
4000
4001 if (!IS_ERR(desc) &&
4002 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) {
4003 WARN_ON(test_bit(FLAG_IS_OUT, &desc->flags));
4004 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
4005 }
4006}
4007EXPORT_SYMBOL_GPL(gpiochip_enable_irq);
4008
Linus Walleij6cee3822016-02-11 20:16:45 +01004009bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
4010{
4011 if (offset >= chip->ngpio)
4012 return false;
4013
4014 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
4015}
4016EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
4017
Hans Verkuil4e6b8232018-09-08 11:23:14 +02004018int gpiochip_reqres_irq(struct gpio_chip *chip, unsigned int offset)
4019{
4020 int ret;
4021
4022 if (!try_module_get(chip->gpiodev->owner))
4023 return -ENODEV;
4024
4025 ret = gpiochip_lock_as_irq(chip, offset);
4026 if (ret) {
4027 chip_err(chip, "unable to lock HW IRQ %u for IRQ\n", offset);
4028 module_put(chip->gpiodev->owner);
4029 return ret;
4030 }
4031 return 0;
4032}
4033EXPORT_SYMBOL_GPL(gpiochip_reqres_irq);
4034
4035void gpiochip_relres_irq(struct gpio_chip *chip, unsigned int offset)
4036{
4037 gpiochip_unlock_as_irq(chip, offset);
4038 module_put(chip->gpiodev->owner);
4039}
4040EXPORT_SYMBOL_GPL(gpiochip_relres_irq);
4041
Linus Walleij143b65d2016-02-16 15:41:42 +01004042bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
4043{
4044 if (offset >= chip->ngpio)
4045 return false;
4046
4047 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
4048}
4049EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
4050
4051bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
4052{
4053 if (offset >= chip->ngpio)
4054 return false;
4055
4056 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
4057}
4058EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
4059
Charles Keepax05f479b2017-05-23 15:47:29 +01004060bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset)
4061{
4062 if (offset >= chip->ngpio)
4063 return false;
4064
Andrew Jefferye10f72b2017-11-30 14:25:24 +10304065 return !test_bit(FLAG_TRANSITORY, &chip->gpiodev->descs[offset].flags);
Charles Keepax05f479b2017-05-23 15:47:29 +01004066}
4067EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
4068
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004069/**
4070 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
4071 * @desc: gpio whose value will be returned
4072 *
4073 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07004074 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004075 *
4076 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08004077 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004078int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08004079{
David Brownelld2876d02008-02-04 22:28:20 -08004080 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004081 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02004082 return gpiod_get_raw_value_commit(desc);
David Brownelld2876d02008-02-04 22:28:20 -08004083}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004084EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09004085
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004086/**
4087 * gpiod_get_value_cansleep() - return a gpio's value
4088 * @desc: gpio whose value will be returned
4089 *
4090 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07004091 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004092 *
4093 * This function is to be called from contexts that can sleep.
4094 */
4095int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09004096{
David Brownelld2876d02008-02-04 22:28:20 -08004097 int value;
David Brownelld2876d02008-02-04 22:28:20 -08004098
4099 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004100 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02004101 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07004102 if (value < 0)
4103 return value;
4104
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004105 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
4106 value = !value;
4107
David Brownelld2876d02008-02-04 22:28:20 -08004108 return value;
4109}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004110EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09004111
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004112/**
Lukas Wunnereec1d562017-10-12 12:40:10 +02004113 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004114 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02004115 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004116 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004117 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02004118 *
4119 * Read the raw values of the GPIOs, i.e. the values of the physical lines
4120 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
4121 * else an error code.
4122 *
4123 * This function is to be called from contexts that can sleep.
4124 */
4125int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
4126 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004127 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004128 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02004129{
4130 might_sleep_if(extra_checks);
4131 if (!desc_array)
4132 return -EINVAL;
4133 return gpiod_get_array_value_complex(true, true, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004134 desc_array, array_info,
4135 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02004136}
4137EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
4138
4139/**
4140 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004141 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02004142 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004143 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004144 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02004145 *
4146 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
4147 * into account. Return 0 in case of success, else an error code.
4148 *
4149 * This function is to be called from contexts that can sleep.
4150 */
4151int gpiod_get_array_value_cansleep(unsigned int array_size,
4152 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004153 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004154 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02004155{
4156 might_sleep_if(extra_checks);
4157 if (!desc_array)
4158 return -EINVAL;
4159 return gpiod_get_array_value_complex(false, true, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004160 desc_array, array_info,
4161 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02004162}
4163EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
4164
4165/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004166 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
4167 * @desc: gpio whose value will be assigned
4168 * @value: value to assign
4169 *
4170 * Set the raw value of the GPIO, i.e. the value of its physical line without
4171 * regard for its ACTIVE_LOW status.
4172 *
4173 * This function is to be called from contexts that can sleep.
4174 */
4175void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09004176{
David Brownelld2876d02008-02-04 22:28:20 -08004177 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004178 VALIDATE_DESC_VOID(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02004179 gpiod_set_raw_value_commit(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09004180}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004181EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09004182
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004183/**
4184 * gpiod_set_value_cansleep() - assign a gpio's value
4185 * @desc: gpio whose value will be assigned
4186 * @value: value to assign
4187 *
4188 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
4189 * account
4190 *
4191 * This function is to be called from contexts that can sleep.
4192 */
4193void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09004194{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004195 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004196 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1e77fc82018-01-09 19:08:21 +01004197 gpiod_set_value_nocheck(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08004198}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004199EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08004200
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004201/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02004202 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004203 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004204 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004205 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004206 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004207 *
4208 * Set the raw values of the GPIOs, i.e. the values of the physical lines
4209 * without regard for their ACTIVE_LOW status.
4210 *
4211 * This function is to be called from contexts that can sleep.
4212 */
Laura Abbott30277432018-05-21 10:57:07 -07004213int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
Geert Uytterhoeven3c940662018-09-27 13:38:10 +02004214 struct gpio_desc **desc_array,
4215 struct gpio_array *array_info,
4216 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004217{
4218 might_sleep_if(extra_checks);
4219 if (!desc_array)
Laura Abbott30277432018-05-21 10:57:07 -07004220 return -EINVAL;
4221 return gpiod_set_array_value_complex(true, true, array_size, desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004222 array_info, value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004223}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02004224EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004225
4226/**
Dmitry Torokhov3946d182017-08-14 21:59:55 -07004227 * gpiod_add_lookup_tables() - register GPIO device consumers
4228 * @tables: list of tables of consumers to register
4229 * @n: number of tables in the list
4230 */
4231void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
4232{
4233 unsigned int i;
4234
4235 mutex_lock(&gpio_lookup_lock);
4236
4237 for (i = 0; i < n; i++)
4238 list_add_tail(&tables[i]->list, &gpio_lookup_list);
4239
4240 mutex_unlock(&gpio_lookup_lock);
4241}
4242
4243/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02004244 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004245 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004246 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004247 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004248 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004249 *
4250 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
4251 * into account.
4252 *
4253 * This function is to be called from contexts that can sleep.
4254 */
Geert Uytterhoevencf9af0d2018-09-27 13:38:09 +02004255int gpiod_set_array_value_cansleep(unsigned int array_size,
4256 struct gpio_desc **desc_array,
4257 struct gpio_array *array_info,
4258 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004259{
4260 might_sleep_if(extra_checks);
4261 if (!desc_array)
Geert Uytterhoevencf9af0d2018-09-27 13:38:09 +02004262 return -EINVAL;
4263 return gpiod_set_array_value_complex(false, true, array_size,
4264 desc_array, array_info,
4265 value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004266}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02004267EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004268
4269/**
Alexandre Courbotad824782013-12-03 12:20:11 +09004270 * gpiod_add_lookup_table() - register GPIO device consumers
4271 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004272 */
Alexandre Courbotad824782013-12-03 12:20:11 +09004273void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004274{
4275 mutex_lock(&gpio_lookup_lock);
4276
Alexandre Courbotad824782013-12-03 12:20:11 +09004277 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004278
4279 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08004280}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02004281EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
David Brownelld2876d02008-02-04 22:28:20 -08004282
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05304283/**
4284 * gpiod_remove_lookup_table() - unregister GPIO device consumers
4285 * @table: table of consumers to unregister
4286 */
4287void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
4288{
4289 mutex_lock(&gpio_lookup_lock);
4290
4291 list_del(&table->list);
4292
4293 mutex_unlock(&gpio_lookup_lock);
4294}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02004295EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05304296
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02004297/**
4298 * gpiod_add_hogs() - register a set of GPIO hogs from machine code
4299 * @hogs: table of gpio hog entries with a zeroed sentinel at the end
4300 */
4301void gpiod_add_hogs(struct gpiod_hog *hogs)
4302{
4303 struct gpio_chip *chip;
4304 struct gpiod_hog *hog;
4305
4306 mutex_lock(&gpio_machine_hogs_mutex);
4307
4308 for (hog = &hogs[0]; hog->chip_label; hog++) {
4309 list_add_tail(&hog->list, &gpio_machine_hogs);
4310
4311 /*
4312 * The chip may have been registered earlier, so check if it
4313 * exists and, if so, try to hog the line now.
4314 */
4315 chip = find_chip_by_name(hog->chip_label);
4316 if (chip)
4317 gpiochip_machine_hog(chip, hog);
4318 }
4319
4320 mutex_unlock(&gpio_machine_hogs_mutex);
4321}
4322EXPORT_SYMBOL_GPL(gpiod_add_hogs);
4323
Alexandre Courbotad824782013-12-03 12:20:11 +09004324static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
4325{
4326 const char *dev_id = dev ? dev_name(dev) : NULL;
4327 struct gpiod_lookup_table *table;
4328
4329 mutex_lock(&gpio_lookup_lock);
4330
4331 list_for_each_entry(table, &gpio_lookup_list, list) {
4332 if (table->dev_id && dev_id) {
4333 /*
4334 * Valid strings on both ends, must be identical to have
4335 * a match
4336 */
4337 if (!strcmp(table->dev_id, dev_id))
4338 goto found;
4339 } else {
4340 /*
4341 * One of the pointers is NULL, so both must be to have
4342 * a match
4343 */
4344 if (dev_id == table->dev_id)
4345 goto found;
4346 }
4347 }
4348 table = NULL;
4349
4350found:
4351 mutex_unlock(&gpio_lookup_lock);
4352 return table;
4353}
4354
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004355static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Andy Shevchenkofed70262019-04-10 18:39:16 +03004356 unsigned int idx, unsigned long *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004357{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004358 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09004359 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004360 struct gpiod_lookup *p;
4361
Alexandre Courbotad824782013-12-03 12:20:11 +09004362 table = gpiod_find_lookup_table(dev);
4363 if (!table)
4364 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004365
Alexandre Courbotad824782013-12-03 12:20:11 +09004366 for (p = &table->table[0]; p->chip_label; p++) {
4367 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004368
Alexandre Courbotad824782013-12-03 12:20:11 +09004369 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004370 if (p->idx != idx)
4371 continue;
4372
Alexandre Courbotad824782013-12-03 12:20:11 +09004373 /* If the lookup entry has a con_id, require exact match */
4374 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
4375 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004376
Alexandre Courbotad824782013-12-03 12:20:11 +09004377 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004378
Alexandre Courbotad824782013-12-03 12:20:11 +09004379 if (!chip) {
Janusz Krzysztofik8853daf2018-07-04 00:18:19 +02004380 /*
4381 * As the lookup table indicates a chip with
4382 * p->chip_label should exist, assume it may
4383 * still appear later and let the interested
4384 * consumer be probed again or let the Deferred
4385 * Probe infrastructure handle the error.
4386 */
4387 dev_warn(dev, "cannot find GPIO chip %s, deferring\n",
4388 p->chip_label);
4389 return ERR_PTR(-EPROBE_DEFER);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004390 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004391
Alexandre Courbotad824782013-12-03 12:20:11 +09004392 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004393 dev_err(dev,
4394 "requested GPIO %d is out of range [0..%d] for chip %s\n",
4395 idx, chip->ngpio, chip->label);
4396 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09004397 }
4398
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09004399 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09004400 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004401
4402 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09004403 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004404
4405 return desc;
4406}
4407
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004408static int platform_gpio_count(struct device *dev, const char *con_id)
4409{
4410 struct gpiod_lookup_table *table;
4411 struct gpiod_lookup *p;
4412 unsigned int count = 0;
4413
4414 table = gpiod_find_lookup_table(dev);
4415 if (!table)
4416 return -ENOENT;
4417
4418 for (p = &table->table[0]; p->chip_label; p++) {
4419 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
4420 (!con_id && !p->con_id))
4421 count++;
4422 }
4423 if (!count)
4424 return -ENOENT;
4425
4426 return count;
4427}
4428
4429/**
Dmitry Torokhov13949fa2019-09-12 20:22:39 -07004430 * fwnode_gpiod_get_index - obtain a GPIO from firmware node
4431 * @fwnode: handle of the firmware node
4432 * @con_id: function within the GPIO consumer
4433 * @index: index of the GPIO to obtain for the consumer
4434 * @flags: GPIO initialization flags
4435 * @label: label to attach to the requested GPIO
4436 *
4437 * This function can be used for drivers that get their configuration
4438 * from opaque firmware.
4439 *
4440 * The function properly finds the corresponding GPIO using whatever is the
4441 * underlying firmware interface and then makes sure that the GPIO
4442 * descriptor is requested before it is returned to the caller.
4443 *
4444 * Returns:
4445 * On successful request the GPIO pin is configured in accordance with
4446 * provided @flags.
4447 *
4448 * In case of error an ERR_PTR() is returned.
4449 */
4450struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
4451 const char *con_id, int index,
4452 enum gpiod_flags flags,
4453 const char *label)
4454{
4455 struct gpio_desc *desc;
4456 char prop_name[32]; /* 32 is max size of property name */
4457 unsigned int i;
4458
4459 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
4460 if (con_id)
4461 snprintf(prop_name, sizeof(prop_name), "%s-%s",
4462 con_id, gpio_suffixes[i]);
4463 else
4464 snprintf(prop_name, sizeof(prop_name), "%s",
4465 gpio_suffixes[i]);
4466
4467 desc = fwnode_get_named_gpiod(fwnode, prop_name, index, flags,
4468 label);
4469 if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT))
4470 break;
4471 }
4472
4473 return desc;
4474}
4475EXPORT_SYMBOL_GPL(fwnode_gpiod_get_index);
4476
4477/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004478 * gpiod_count - return the number of GPIOs associated with a device / function
4479 * or -ENOENT if no GPIO has been assigned to the requested function
4480 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4481 * @con_id: function within the GPIO consumer
4482 */
4483int gpiod_count(struct device *dev, const char *con_id)
4484{
4485 int count = -ENOENT;
4486
4487 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
Linus Walleijf626d6d2019-07-17 09:10:01 +02004488 count = of_gpio_get_count(dev, con_id);
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004489 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
4490 count = acpi_gpio_count(dev, con_id);
4491
4492 if (count < 0)
4493 count = platform_gpio_count(dev, con_id);
4494
4495 return count;
4496}
4497EXPORT_SYMBOL_GPL(gpiod_count);
4498
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004499/**
Thierry Reding08791622014-04-25 16:54:22 +02004500 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09004501 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004502 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004503 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004504 *
4505 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004506 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07004507 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004508 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004509struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004510 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004511{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004512 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004513}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004514EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004515
4516/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02004517 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
4518 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4519 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004520 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02004521 *
4522 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
4523 * the requested function it will return NULL. This is convenient for drivers
4524 * that need to handle optional GPIOs.
4525 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004526struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004527 const char *con_id,
4528 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02004529{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004530 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02004531}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004532EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02004533
Benoit Parrotf625d462015-02-02 11:44:44 -06004534
4535/**
4536 * gpiod_configure_flags - helper function to configure a given GPIO
4537 * @desc: gpio whose value will be assigned
4538 * @con_id: function within the GPIO consumer
Andy Shevchenkofed70262019-04-10 18:39:16 +03004539 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4540 * of_find_gpio() or of_get_gpio_hog()
Benoit Parrotf625d462015-02-02 11:44:44 -06004541 * @dflags: gpiod_flags - optional GPIO initialization flags
4542 *
4543 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
4544 * requested function and/or index, or another IS_ERR() code if an error
4545 * occurred while trying to acquire the GPIO.
4546 */
Andy Shevchenkoc29fd9e2017-05-23 20:03:16 +03004547int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Johan Hovold85b03b32016-07-03 18:32:05 +02004548 unsigned long lflags, enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06004549{
Linus Walleijd377f562019-07-16 11:11:45 +02004550 int ret;
Benoit Parrotf625d462015-02-02 11:44:44 -06004551
Johan Hovold85b03b32016-07-03 18:32:05 +02004552 if (lflags & GPIO_ACTIVE_LOW)
4553 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
Linus Walleijf926dfc2017-09-10 19:26:22 +02004554
Johan Hovold85b03b32016-07-03 18:32:05 +02004555 if (lflags & GPIO_OPEN_DRAIN)
4556 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
Linus Walleijf926dfc2017-09-10 19:26:22 +02004557 else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) {
4558 /*
4559 * This enforces open drain mode from the consumer side.
4560 * This is necessary for some busses like I2C, but the lookup
4561 * should *REALLY* have specified them as open drain in the
4562 * first place, so print a little warning here.
4563 */
4564 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
4565 gpiod_warn(desc,
4566 "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
4567 }
4568
Johan Hovold85b03b32016-07-03 18:32:05 +02004569 if (lflags & GPIO_OPEN_SOURCE)
4570 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Andrew Jefferye10f72b2017-11-30 14:25:24 +10304571
Thomas Petazzonid4499912019-02-07 17:28:58 +01004572 if ((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DOWN)) {
4573 gpiod_err(desc,
4574 "both pull-up and pull-down enabled, invalid configuration\n");
4575 return -EINVAL;
4576 }
4577
4578 if (lflags & GPIO_PULL_UP)
4579 set_bit(FLAG_PULL_UP, &desc->flags);
4580 else if (lflags & GPIO_PULL_DOWN)
4581 set_bit(FLAG_PULL_DOWN, &desc->flags);
4582
Linus Walleijd377f562019-07-16 11:11:45 +02004583 ret = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
4584 if (ret < 0)
4585 return ret;
Johan Hovold85b03b32016-07-03 18:32:05 +02004586
Benoit Parrotf625d462015-02-02 11:44:44 -06004587 /* No particular flag request, return here... */
4588 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
4589 pr_debug("no flags found for %s\n", con_id);
4590 return 0;
4591 }
4592
4593 /* Process flags */
4594 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
Linus Walleijd377f562019-07-16 11:11:45 +02004595 ret = gpiod_direction_output(desc,
Linus Walleijad177312016-11-13 23:02:44 +01004596 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
Benoit Parrotf625d462015-02-02 11:44:44 -06004597 else
Linus Walleijd377f562019-07-16 11:11:45 +02004598 ret = gpiod_direction_input(desc);
Benoit Parrotf625d462015-02-02 11:44:44 -06004599
Linus Walleijd377f562019-07-16 11:11:45 +02004600 return ret;
Benoit Parrotf625d462015-02-02 11:44:44 -06004601}
4602
Thierry Reding29a1f2332014-04-25 17:10:06 +02004603/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004604 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02004605 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004606 * @con_id: function within the GPIO consumer
4607 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004608 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004609 *
4610 * This variant of gpiod_get() allows to access GPIOs other than the first
4611 * defined one for functions that define several GPIOs.
4612 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004613 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
4614 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07004615 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004616 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004617struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004618 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004619 unsigned int idx,
4620 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004621{
Andy Shevchenko2d6c06f2019-04-10 18:39:17 +03004622 unsigned long lookupflags = GPIO_LOOKUP_FLAGS_DEFAULT;
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09004623 struct gpio_desc *desc = NULL;
Linus Walleijd377f562019-07-16 11:11:45 +02004624 int ret;
Linus Walleij7d18f0a2018-01-16 08:29:50 +01004625 /* Maybe we have a device name, maybe not */
4626 const char *devname = dev ? dev_name(dev) : "?";
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004627
4628 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
4629
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01004630 if (dev) {
4631 /* Using device tree? */
4632 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
4633 dev_dbg(dev, "using device tree for GPIO lookup\n");
4634 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
4635 } else if (ACPI_COMPANION(dev)) {
4636 dev_dbg(dev, "using ACPI for GPIO lookup\n");
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +03004637 desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags);
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01004638 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09004639 }
4640
4641 /*
4642 * Either we are not using DT or ACPI, or their lookup did not return
4643 * a result. In that case, use platform lookup as a fallback.
4644 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004645 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04004646 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004647 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004648 }
4649
4650 if (IS_ERR(desc)) {
Wang Dongsheng9d5a1f22018-02-27 00:12:13 -08004651 dev_dbg(dev, "No GPIO consumer %s found\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004652 return desc;
4653 }
4654
Linus Walleij7d18f0a2018-01-16 08:29:50 +01004655 /*
4656 * If a connection label was passed use that, else attempt to use
4657 * the device name as label
4658 */
Linus Walleijd377f562019-07-16 11:11:45 +02004659 ret = gpiod_request(desc, con_id ? con_id : devname);
4660 if (ret < 0) {
4661 if (ret == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE) {
Linus Walleijb0ce7b292018-10-12 14:54:12 +02004662 /*
4663 * This happens when there are several consumers for
4664 * the same GPIO line: we just return here without
4665 * further initialization. It is a bit if a hack.
4666 * This is necessary to support fixed regulators.
4667 *
4668 * FIXME: Make this more sane and safe.
4669 */
4670 dev_info(dev, "nonexclusive access to GPIO for %s\n",
4671 con_id ? con_id : devname);
4672 return desc;
4673 } else {
Linus Walleijd377f562019-07-16 11:11:45 +02004674 return ERR_PTR(ret);
Linus Walleijb0ce7b292018-10-12 14:54:12 +02004675 }
4676 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004677
Linus Walleijd377f562019-07-16 11:11:45 +02004678 ret = gpiod_configure_flags(desc, con_id, lookupflags, flags);
4679 if (ret < 0) {
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004680 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
4681 gpiod_put(desc);
Linus Walleijd377f562019-07-16 11:11:45 +02004682 return ERR_PTR(ret);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004683 }
4684
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004685 return desc;
4686}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004687EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004688
4689/**
Mika Westerberg40b73182014-10-21 13:33:59 +02004690 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
4691 * @fwnode: handle of the firmware node
4692 * @propname: name of the firmware property representing the GPIO
Linus Walleij6392cca2017-12-29 02:07:54 +01004693 * @index: index of the GPIO to obtain for the consumer
Andy Shevchenkoa264d102017-01-09 16:02:28 +02004694 * @dflags: GPIO initialization flags
Thierry Reding950d55f52017-07-24 16:57:22 +02004695 * @label: label to attach to the requested GPIO
Mika Westerberg40b73182014-10-21 13:33:59 +02004696 *
4697 * This function can be used for drivers that get their configuration
Linus Walleij6392cca2017-12-29 02:07:54 +01004698 * from opaque firmware.
Mika Westerberg40b73182014-10-21 13:33:59 +02004699 *
Linus Walleij6392cca2017-12-29 02:07:54 +01004700 * The function properly finds the corresponding GPIO using whatever is the
Mika Westerberg40b73182014-10-21 13:33:59 +02004701 * underlying firmware interface and then makes sure that the GPIO
4702 * descriptor is requested before it is returned to the caller.
4703 *
Thierry Reding950d55f52017-07-24 16:57:22 +02004704 * Returns:
Andy Shevchenkoff213782017-02-28 17:03:12 +02004705 * On successful request the GPIO pin is configured in accordance with
Andy Shevchenkoa264d102017-01-09 16:02:28 +02004706 * provided @dflags.
4707 *
Mika Westerberg40b73182014-10-21 13:33:59 +02004708 * In case of error an ERR_PTR() is returned.
4709 */
4710struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
Boris Brezillon537b94d2017-02-02 14:53:11 +01004711 const char *propname, int index,
Alexander Steinb2987d72017-01-12 17:39:24 +01004712 enum gpiod_flags dflags,
4713 const char *label)
Mika Westerberg40b73182014-10-21 13:33:59 +02004714{
Andy Shevchenko2d6c06f2019-04-10 18:39:17 +03004715 unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
Mika Westerberg40b73182014-10-21 13:33:59 +02004716 struct gpio_desc *desc = ERR_PTR(-ENODEV);
Mika Westerberg40b73182014-10-21 13:33:59 +02004717 int ret;
4718
4719 if (!fwnode)
4720 return ERR_PTR(-EINVAL);
4721
4722 if (is_of_node(fwnode)) {
Linus Walleij6392cca2017-12-29 02:07:54 +01004723 desc = gpiod_get_from_of_node(to_of_node(fwnode),
4724 propname, index,
4725 dflags,
4726 label);
4727 return desc;
Mika Westerberg40b73182014-10-21 13:33:59 +02004728 } else if (is_acpi_node(fwnode)) {
4729 struct acpi_gpio_info info;
4730
Boris Brezillon537b94d2017-02-02 14:53:11 +01004731 desc = acpi_node_get_gpiod(fwnode, propname, index, &info);
Linus Walleij6392cca2017-12-29 02:07:54 +01004732 if (IS_ERR(desc))
4733 return desc;
4734
4735 acpi_gpio_update_gpiod_flags(&dflags, &info);
Andy Shevchenko606be342019-04-10 18:39:20 +03004736 acpi_gpio_update_gpiod_lookup_flags(&lflags, &info);
Mika Westerberg40b73182014-10-21 13:33:59 +02004737 }
4738
Linus Walleij6392cca2017-12-29 02:07:54 +01004739 /* Currently only ACPI takes this path */
Alexander Steinb2987d72017-01-12 17:39:24 +01004740 ret = gpiod_request(desc, label);
Johan Hovold85b03b32016-07-03 18:32:05 +02004741 if (ret)
4742 return ERR_PTR(ret);
4743
Andy Shevchenkoa264d102017-01-09 16:02:28 +02004744 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
4745 if (ret < 0) {
4746 gpiod_put(desc);
4747 return ERR_PTR(ret);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03004748 }
4749
Mika Westerberg40b73182014-10-21 13:33:59 +02004750 return desc;
4751}
4752EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
4753
4754/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02004755 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
4756 * function
4757 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4758 * @con_id: function within the GPIO consumer
4759 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004760 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02004761 *
4762 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
4763 * specified index was assigned to the requested function it will return NULL.
4764 * This is convenient for drivers that need to handle optional GPIOs.
4765 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004766struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02004767 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004768 unsigned int index,
4769 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02004770{
4771 struct gpio_desc *desc;
4772
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004773 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02004774 if (IS_ERR(desc)) {
4775 if (PTR_ERR(desc) == -ENOENT)
4776 return NULL;
4777 }
4778
4779 return desc;
4780}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004781EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02004782
4783/**
Benoit Parrotf625d462015-02-02 11:44:44 -06004784 * gpiod_hog - Hog the specified GPIO desc given the provided flags
4785 * @desc: gpio whose value will be assigned
4786 * @name: gpio line name
Andy Shevchenkofed70262019-04-10 18:39:16 +03004787 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4788 * of_find_gpio() or of_get_gpio_hog()
Benoit Parrotf625d462015-02-02 11:44:44 -06004789 * @dflags: gpiod_flags - optional GPIO initialization flags
4790 */
4791int gpiod_hog(struct gpio_desc *desc, const char *name,
4792 unsigned long lflags, enum gpiod_flags dflags)
4793{
4794 struct gpio_chip *chip;
4795 struct gpio_desc *local_desc;
4796 int hwnum;
Linus Walleijd377f562019-07-16 11:11:45 +02004797 int ret;
Benoit Parrotf625d462015-02-02 11:44:44 -06004798
4799 chip = gpiod_to_chip(desc);
4800 hwnum = gpio_chip_hwgpio(desc);
4801
Linus Walleij5923ea62019-04-26 14:40:18 +02004802 local_desc = gpiochip_request_own_desc(chip, hwnum, name,
4803 lflags, dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -06004804 if (IS_ERR(local_desc)) {
Linus Walleijd377f562019-07-16 11:11:45 +02004805 ret = PTR_ERR(local_desc);
Laxman Dewanganc31a5712016-03-11 19:13:21 +05304806 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
Linus Walleijd377f562019-07-16 11:11:45 +02004807 name, chip->label, hwnum, ret);
4808 return ret;
Benoit Parrotf625d462015-02-02 11:44:44 -06004809 }
4810
Benoit Parrotf625d462015-02-02 11:44:44 -06004811 /* Mark GPIO as hogged so it can be identified and removed later */
4812 set_bit(FLAG_IS_HOGGED, &desc->flags);
4813
4814 pr_info("GPIO line %d (%s) hogged as %s%s\n",
4815 desc_to_gpio(desc), name,
4816 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
4817 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
4818 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
4819
4820 return 0;
4821}
4822
4823/**
4824 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
4825 * @chip: gpio chip to act on
Benoit Parrotf625d462015-02-02 11:44:44 -06004826 */
4827static void gpiochip_free_hogs(struct gpio_chip *chip)
4828{
4829 int id;
4830
4831 for (id = 0; id < chip->ngpio; id++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01004832 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
4833 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
Benoit Parrotf625d462015-02-02 11:44:44 -06004834 }
4835}
4836
4837/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004838 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
4839 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4840 * @con_id: function within the GPIO consumer
4841 * @flags: optional GPIO initialization flags
4842 *
4843 * This function acquires all the GPIOs defined under a given function.
4844 *
4845 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
4846 * no GPIO has been assigned to the requested function, or another IS_ERR()
4847 * code if an error occurred while trying to acquire the GPIOs.
4848 */
4849struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
4850 const char *con_id,
4851 enum gpiod_flags flags)
4852{
4853 struct gpio_desc *desc;
4854 struct gpio_descs *descs;
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004855 struct gpio_array *array_info = NULL;
4856 struct gpio_chip *chip;
4857 int count, bitmap_size;
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004858
4859 count = gpiod_count(dev, con_id);
4860 if (count < 0)
4861 return ERR_PTR(count);
4862
Kees Cookacafe7e2018-05-08 13:45:50 -07004863 descs = kzalloc(struct_size(descs, desc, count), GFP_KERNEL);
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004864 if (!descs)
4865 return ERR_PTR(-ENOMEM);
4866
4867 for (descs->ndescs = 0; descs->ndescs < count; ) {
4868 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
4869 if (IS_ERR(desc)) {
4870 gpiod_put_array(descs);
4871 return ERR_CAST(desc);
4872 }
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004873
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004874 descs->desc[descs->ndescs] = desc;
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004875
4876 chip = gpiod_to_chip(desc);
4877 /*
Janusz Krzysztofikc4c958a2018-09-24 01:53:36 +02004878 * If pin hardware number of array member 0 is also 0, select
4879 * its chip as a candidate for fast bitmap processing path.
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004880 */
Janusz Krzysztofikc4c958a2018-09-24 01:53:36 +02004881 if (descs->ndescs == 0 && gpio_chip_hwgpio(desc) == 0) {
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004882 struct gpio_descs *array;
4883
4884 bitmap_size = BITS_TO_LONGS(chip->ngpio > count ?
4885 chip->ngpio : count);
4886
4887 array = kzalloc(struct_size(descs, desc, count) +
4888 struct_size(array_info, invert_mask,
4889 3 * bitmap_size), GFP_KERNEL);
4890 if (!array) {
4891 gpiod_put_array(descs);
4892 return ERR_PTR(-ENOMEM);
4893 }
4894
4895 memcpy(array, descs,
4896 struct_size(descs, desc, descs->ndescs + 1));
4897 kfree(descs);
4898
4899 descs = array;
4900 array_info = (void *)(descs->desc + count);
4901 array_info->get_mask = array_info->invert_mask +
4902 bitmap_size;
4903 array_info->set_mask = array_info->get_mask +
4904 bitmap_size;
4905
4906 array_info->desc = descs->desc;
4907 array_info->size = count;
4908 array_info->chip = chip;
4909 bitmap_set(array_info->get_mask, descs->ndescs,
4910 count - descs->ndescs);
4911 bitmap_set(array_info->set_mask, descs->ndescs,
4912 count - descs->ndescs);
4913 descs->info = array_info;
4914 }
Janusz Krzysztofikc4c958a2018-09-24 01:53:36 +02004915 /* Unmark array members which don't belong to the 'fast' chip */
4916 if (array_info && array_info->chip != chip) {
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004917 __clear_bit(descs->ndescs, array_info->get_mask);
4918 __clear_bit(descs->ndescs, array_info->set_mask);
Janusz Krzysztofikc4c958a2018-09-24 01:53:36 +02004919 }
4920 /*
4921 * Detect array members which belong to the 'fast' chip
4922 * but their pins are not in hardware order.
4923 */
4924 else if (array_info &&
4925 gpio_chip_hwgpio(desc) != descs->ndescs) {
4926 /*
4927 * Don't use fast path if all array members processed so
4928 * far belong to the same chip as this one but its pin
4929 * hardware number is different from its array index.
4930 */
4931 if (bitmap_full(array_info->get_mask, descs->ndescs)) {
4932 array_info = NULL;
4933 } else {
4934 __clear_bit(descs->ndescs,
4935 array_info->get_mask);
4936 __clear_bit(descs->ndescs,
4937 array_info->set_mask);
4938 }
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004939 } else if (array_info) {
4940 /* Exclude open drain or open source from fast output */
4941 if (gpiochip_line_is_open_drain(chip, descs->ndescs) ||
4942 gpiochip_line_is_open_source(chip, descs->ndescs))
4943 __clear_bit(descs->ndescs,
4944 array_info->set_mask);
4945 /* Identify 'fast' pins which require invertion */
4946 if (gpiod_is_active_low(desc))
4947 __set_bit(descs->ndescs,
4948 array_info->invert_mask);
4949 }
4950
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004951 descs->ndescs++;
4952 }
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004953 if (array_info)
4954 dev_dbg(dev,
4955 "GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n",
4956 array_info->chip->label, array_info->size,
4957 *array_info->get_mask, *array_info->set_mask,
4958 *array_info->invert_mask);
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004959 return descs;
4960}
4961EXPORT_SYMBOL_GPL(gpiod_get_array);
4962
4963/**
4964 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
4965 * function
4966 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4967 * @con_id: function within the GPIO consumer
4968 * @flags: optional GPIO initialization flags
4969 *
4970 * This is equivalent to gpiod_get_array(), except that when no GPIO was
4971 * assigned to the requested function it will return NULL.
4972 */
4973struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
4974 const char *con_id,
4975 enum gpiod_flags flags)
4976{
4977 struct gpio_descs *descs;
4978
4979 descs = gpiod_get_array(dev, con_id, flags);
4980 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
4981 return NULL;
4982
4983 return descs;
4984}
4985EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
4986
4987/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004988 * gpiod_put - dispose of a GPIO descriptor
4989 * @desc: GPIO descriptor to dispose of
4990 *
4991 * No descriptor can be used after gpiod_put() has been called on it.
4992 */
4993void gpiod_put(struct gpio_desc *desc)
4994{
Andy Shevchenko1d7765b2019-03-26 17:21:14 +02004995 if (desc)
4996 gpiod_free(desc);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004997}
4998EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08004999
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01005000/**
5001 * gpiod_put_array - dispose of multiple GPIO descriptors
5002 * @descs: struct gpio_descs containing an array of descriptors
5003 */
5004void gpiod_put_array(struct gpio_descs *descs)
5005{
5006 unsigned int i;
5007
5008 for (i = 0; i < descs->ndescs; i++)
5009 gpiod_put(descs->desc[i]);
5010
5011 kfree(descs);
5012}
5013EXPORT_SYMBOL_GPL(gpiod_put_array);
5014
Linus Walleij3c702e92015-10-21 15:29:53 +02005015static int __init gpiolib_dev_init(void)
5016{
5017 int ret;
5018
5019 /* Register GPIO sysfs bus */
Andy Shevchenkob1911712018-07-03 03:39:03 +03005020 ret = bus_register(&gpio_bus_type);
Linus Walleij3c702e92015-10-21 15:29:53 +02005021 if (ret < 0) {
5022 pr_err("gpiolib: could not register GPIO bus type\n");
5023 return ret;
5024 }
5025
5026 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
5027 if (ret < 0) {
5028 pr_err("gpiolib: failed to allocate char dev region\n");
5029 bus_unregister(&gpio_bus_type);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07005030 } else {
5031 gpiolib_initialized = true;
5032 gpiochip_setup_devs();
Linus Walleij3c702e92015-10-21 15:29:53 +02005033 }
5034 return ret;
5035}
5036core_initcall(gpiolib_dev_init);
5037
David Brownelld2876d02008-02-04 22:28:20 -08005038#ifdef CONFIG_DEBUG_FS
5039
Linus Walleijfdeb8e12016-02-10 10:57:36 +01005040static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
David Brownelld2876d02008-02-04 22:28:20 -08005041{
5042 unsigned i;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01005043 struct gpio_chip *chip = gdev->chip;
5044 unsigned gpio = gdev->base;
5045 struct gpio_desc *gdesc = &gdev->descs[0];
Linus Walleij90fd2272018-10-01 23:06:17 +02005046 bool is_out;
5047 bool is_irq;
5048 bool active_low;
David Brownelld2876d02008-02-04 22:28:20 -08005049
Linus Walleijfdeb8e12016-02-10 10:57:36 +01005050 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02005051 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
5052 if (gdesc->name) {
5053 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
5054 gpio, gdesc->name);
5055 }
David Brownelld2876d02008-02-04 22:28:20 -08005056 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02005057 }
David Brownelld2876d02008-02-04 22:28:20 -08005058
Alexandre Courbot372e7222013-02-03 01:29:29 +09005059 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08005060 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02005061 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Linus Walleij90fd2272018-10-01 23:06:17 +02005062 active_low = test_bit(FLAG_ACTIVE_LOW, &gdesc->flags);
5063 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s%s",
Markus Pargmannced433e2015-08-14 16:11:02 +02005064 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08005065 is_out ? "out" : "in ",
Andy Shevchenko1c22a252018-07-12 20:36:42 +03005066 chip->get ? (chip->get(chip, i) ? "hi" : "lo") : "? ",
Linus Walleij90fd2272018-10-01 23:06:17 +02005067 is_irq ? "IRQ " : "",
5068 active_low ? "ACTIVE LOW" : "");
David Brownelld2876d02008-02-04 22:28:20 -08005069 seq_printf(s, "\n");
5070 }
5071}
5072
Thierry Redingf9c4a312012-04-12 13:26:01 +02005073static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08005074{
Grant Likely362432a2013-02-09 09:41:49 +00005075 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02005076 struct gpio_device *gdev = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09005077 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02005078
5079 s->private = "";
5080
Grant Likely362432a2013-02-09 09:41:49 +00005081 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02005082 list_for_each_entry(gdev, &gpio_devices, list)
Grant Likely362432a2013-02-09 09:41:49 +00005083 if (index-- == 0) {
5084 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02005085 return gdev;
Grant Likely362432a2013-02-09 09:41:49 +00005086 }
5087 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09005088
5089 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02005090}
5091
5092static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
5093{
Grant Likely362432a2013-02-09 09:41:49 +00005094 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02005095 struct gpio_device *gdev = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02005096 void *ret = NULL;
5097
Grant Likely362432a2013-02-09 09:41:49 +00005098 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02005099 if (list_is_last(&gdev->list, &gpio_devices))
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09005100 ret = NULL;
5101 else
Linus Walleijff2b1352015-10-20 11:10:38 +02005102 ret = list_entry(gdev->list.next, struct gpio_device, list);
Grant Likely362432a2013-02-09 09:41:49 +00005103 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02005104
5105 s->private = "\n";
5106 ++*pos;
5107
5108 return ret;
5109}
5110
5111static void gpiolib_seq_stop(struct seq_file *s, void *v)
5112{
5113}
5114
5115static int gpiolib_seq_show(struct seq_file *s, void *v)
5116{
Linus Walleijff2b1352015-10-20 11:10:38 +02005117 struct gpio_device *gdev = v;
5118 struct gpio_chip *chip = gdev->chip;
5119 struct device *parent;
Thierry Redingf9c4a312012-04-12 13:26:01 +02005120
Linus Walleijff2b1352015-10-20 11:10:38 +02005121 if (!chip) {
5122 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
5123 dev_name(&gdev->dev));
5124 return 0;
5125 }
5126
5127 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
5128 dev_name(&gdev->dev),
Linus Walleijfdeb8e12016-02-10 10:57:36 +01005129 gdev->base, gdev->base + gdev->ngpio - 1);
Linus Walleijff2b1352015-10-20 11:10:38 +02005130 parent = chip->parent;
5131 if (parent)
5132 seq_printf(s, ", parent: %s/%s",
5133 parent->bus ? parent->bus->name : "no-bus",
5134 dev_name(parent));
Thierry Redingf9c4a312012-04-12 13:26:01 +02005135 if (chip->label)
5136 seq_printf(s, ", %s", chip->label);
5137 if (chip->can_sleep)
5138 seq_printf(s, ", can sleep");
5139 seq_printf(s, ":\n");
5140
5141 if (chip->dbg_show)
5142 chip->dbg_show(s, chip);
5143 else
Linus Walleijfdeb8e12016-02-10 10:57:36 +01005144 gpiolib_dbg_show(s, gdev);
Thierry Redingf9c4a312012-04-12 13:26:01 +02005145
David Brownelld2876d02008-02-04 22:28:20 -08005146 return 0;
5147}
5148
Thierry Redingf9c4a312012-04-12 13:26:01 +02005149static const struct seq_operations gpiolib_seq_ops = {
5150 .start = gpiolib_seq_start,
5151 .next = gpiolib_seq_next,
5152 .stop = gpiolib_seq_stop,
5153 .show = gpiolib_seq_show,
5154};
5155
David Brownelld2876d02008-02-04 22:28:20 -08005156static int gpiolib_open(struct inode *inode, struct file *file)
5157{
Thierry Redingf9c4a312012-04-12 13:26:01 +02005158 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08005159}
5160
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005161static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02005162 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08005163 .open = gpiolib_open,
5164 .read = seq_read,
5165 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02005166 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08005167};
5168
5169static int __init gpiolib_debugfs_init(void)
5170{
5171 /* /sys/kernel/debug/gpio */
Greg Kroah-Hartmanacc68b02019-06-18 17:50:45 +02005172 debugfs_create_file("gpio", S_IFREG | S_IRUGO, NULL, NULL,
5173 &gpiolib_operations);
David Brownelld2876d02008-02-04 22:28:20 -08005174 return 0;
5175}
5176subsys_initcall(gpiolib_debugfs_init);
5177
5178#endif /* DEBUG_FS */