blob: 104ed299d5eaa41c439e61dbcde5eb70d620840c [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 | \
425 GPIOHANDLE_REQUEST_OPEN_DRAIN | \
426 GPIOHANDLE_REQUEST_OPEN_SOURCE)
427
Linus Walleijd7c51b42016-04-26 10:35:29 +0200428static long linehandle_ioctl(struct file *filep, unsigned int cmd,
429 unsigned long arg)
430{
431 struct linehandle_state *lh = filep->private_data;
432 void __user *ip = (void __user *)arg;
433 struct gpiohandle_data ghd;
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +0200434 DECLARE_BITMAP(vals, GPIOHANDLES_MAX);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200435 int i;
436
437 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
Bartosz Golaszewski2b955b32018-07-16 10:34:24 +0200438 /* NOTE: It's ok to read values of output lines. */
Lukas Wunnereec1d562017-10-12 12:40:10 +0200439 int ret = gpiod_get_array_value_complex(false,
440 true,
441 lh->numdescs,
442 lh->descs,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +0200443 NULL,
Lukas Wunnereec1d562017-10-12 12:40:10 +0200444 vals);
445 if (ret)
446 return ret;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200447
Lars-Peter Clausen3eded5d2016-10-18 16:54:02 +0200448 memset(&ghd, 0, sizeof(ghd));
Lukas Wunnereec1d562017-10-12 12:40:10 +0200449 for (i = 0; i < lh->numdescs; i++)
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +0200450 ghd.values[i] = test_bit(i, vals);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200451
452 if (copy_to_user(ip, &ghd, sizeof(ghd)))
453 return -EFAULT;
454
455 return 0;
456 } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
Bartosz Golaszewskie5332d52018-07-16 10:34:23 +0200457 /*
458 * All line descriptors were created at once with the same
459 * flags so just check if the first one is really output.
460 */
461 if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags))
462 return -EPERM;
463
Linus Walleijd7c51b42016-04-26 10:35:29 +0200464 if (copy_from_user(&ghd, ip, sizeof(ghd)))
465 return -EFAULT;
466
467 /* Clamp all values to [0,1] */
468 for (i = 0; i < lh->numdescs; i++)
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +0200469 __assign_bit(i, vals, ghd.values[i]);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200470
471 /* Reuse the array setting function */
Laura Abbott30277432018-05-21 10:57:07 -0700472 return gpiod_set_array_value_complex(false,
Linus Walleijd7c51b42016-04-26 10:35:29 +0200473 true,
474 lh->numdescs,
475 lh->descs,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +0200476 NULL,
Linus Walleijd7c51b42016-04-26 10:35:29 +0200477 vals);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200478 }
479 return -EINVAL;
480}
481
482#ifdef CONFIG_COMPAT
483static long linehandle_ioctl_compat(struct file *filep, unsigned int cmd,
484 unsigned long arg)
485{
486 return linehandle_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
487}
488#endif
489
490static int linehandle_release(struct inode *inode, struct file *filep)
491{
492 struct linehandle_state *lh = filep->private_data;
493 struct gpio_device *gdev = lh->gdev;
494 int i;
495
496 for (i = 0; i < lh->numdescs; i++)
497 gpiod_free(lh->descs[i]);
498 kfree(lh->label);
499 kfree(lh);
500 put_device(&gdev->dev);
501 return 0;
502}
503
504static const struct file_operations linehandle_fileops = {
505 .release = linehandle_release,
506 .owner = THIS_MODULE,
507 .llseek = noop_llseek,
508 .unlocked_ioctl = linehandle_ioctl,
509#ifdef CONFIG_COMPAT
510 .compat_ioctl = linehandle_ioctl_compat,
511#endif
512};
513
514static int linehandle_create(struct gpio_device *gdev, void __user *ip)
515{
516 struct gpiohandle_request handlereq;
517 struct linehandle_state *lh;
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200518 struct file *file;
Timur Tabiab3dbcf2018-03-29 13:29:12 -0500519 int fd, i, count = 0, ret;
Bartosz Golaszewski418ee8e2017-10-16 11:32:29 +0200520 u32 lflags;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200521
522 if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
523 return -EFAULT;
524 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
525 return -EINVAL;
526
Bartosz Golaszewski418ee8e2017-10-16 11:32:29 +0200527 lflags = handlereq.flags;
528
529 /* Return an error if an unknown flag is set */
530 if (lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
531 return -EINVAL;
532
Bartosz Golaszewski588fc3b2017-11-15 16:47:43 +0100533 /*
Kent Gibsone95fbc12019-09-09 03:22:18 +0000534 * Do not allow both INPUT & OUTPUT flags to be set as they are
535 * contradictory.
536 */
537 if ((lflags & GPIOHANDLE_REQUEST_INPUT) &&
538 (lflags & GPIOHANDLE_REQUEST_OUTPUT))
539 return -EINVAL;
540
541 /*
Bartosz Golaszewski588fc3b2017-11-15 16:47:43 +0100542 * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
543 * the hardware actually supports enabling both at the same time the
544 * electrical result would be disastrous.
545 */
546 if ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) &&
547 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
548 return -EINVAL;
549
Bartosz Golaszewski609aaf62017-10-16 11:32:30 +0200550 /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
551 if (!(lflags & GPIOHANDLE_REQUEST_OUTPUT) &&
552 ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
553 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
554 return -EINVAL;
555
Linus Walleijd7c51b42016-04-26 10:35:29 +0200556 lh = kzalloc(sizeof(*lh), GFP_KERNEL);
557 if (!lh)
558 return -ENOMEM;
559 lh->gdev = gdev;
560 get_device(&gdev->dev);
561
562 /* Make sure this is terminated */
563 handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
564 if (strlen(handlereq.consumer_label)) {
565 lh->label = kstrdup(handlereq.consumer_label,
566 GFP_KERNEL);
567 if (!lh->label) {
568 ret = -ENOMEM;
569 goto out_free_lh;
570 }
571 }
572
573 /* Request each GPIO */
574 for (i = 0; i < handlereq.lines; i++) {
575 u32 offset = handlereq.lineoffsets[i];
Linus Walleijd7c51b42016-04-26 10:35:29 +0200576 struct gpio_desc *desc;
577
Lars-Peter Clausene405f9f2016-10-18 16:54:01 +0200578 if (offset >= gdev->ngpio) {
579 ret = -EINVAL;
580 goto out_free_descs;
581 }
582
Linus Walleijd7c51b42016-04-26 10:35:29 +0200583 desc = &gdev->descs[offset];
584 ret = gpiod_request(desc, lh->label);
585 if (ret)
586 goto out_free_descs;
587 lh->descs[i] = desc;
Ricardo Ribalda Delgado19a4fbf2018-09-13 15:37:04 +0200588 count = i + 1;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200589
590 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
591 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
592 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
593 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
594 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
595 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
596
Andrew Jefferye10f72b2017-11-30 14:25:24 +1030597 ret = gpiod_set_transitory(desc, false);
598 if (ret < 0)
599 goto out_free_descs;
600
Linus Walleijd7c51b42016-04-26 10:35:29 +0200601 /*
602 * Lines have to be requested explicitly for input
603 * or output, else the line will be treated "as is".
604 */
605 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
606 int val = !!handlereq.default_values[i];
607
608 ret = gpiod_direction_output(desc, val);
609 if (ret)
610 goto out_free_descs;
611 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
612 ret = gpiod_direction_input(desc);
613 if (ret)
614 goto out_free_descs;
615 }
616 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
617 offset);
618 }
Linus Walleije2f608b2016-06-18 10:56:43 +0200619 /* Let i point at the last handle */
620 i--;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200621 lh->numdescs = handlereq.lines;
622
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200623 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200624 if (fd < 0) {
625 ret = fd;
626 goto out_free_descs;
627 }
628
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200629 file = anon_inode_getfile("gpio-linehandle",
630 &linehandle_fileops,
631 lh,
632 O_RDONLY | O_CLOEXEC);
633 if (IS_ERR(file)) {
634 ret = PTR_ERR(file);
635 goto out_put_unused_fd;
636 }
637
Linus Walleijd7c51b42016-04-26 10:35:29 +0200638 handlereq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200639 if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200640 /*
641 * fput() will trigger the release() callback, so do not go onto
642 * the regular error cleanup path here.
643 */
644 fput(file);
645 put_unused_fd(fd);
646 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +0200647 }
Linus Walleijd7c51b42016-04-26 10:35:29 +0200648
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200649 fd_install(fd, file);
650
Linus Walleijd7c51b42016-04-26 10:35:29 +0200651 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
652 lh->numdescs);
653
654 return 0;
655
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200656out_put_unused_fd:
657 put_unused_fd(fd);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200658out_free_descs:
Timur Tabiab3dbcf2018-03-29 13:29:12 -0500659 for (i = 0; i < count; i++)
Linus Walleijd7c51b42016-04-26 10:35:29 +0200660 gpiod_free(lh->descs[i]);
661 kfree(lh->label);
662out_free_lh:
663 kfree(lh);
664 put_device(&gdev->dev);
665 return ret;
666}
667
Linus Walleij61f922d2016-06-02 11:30:15 +0200668/*
669 * GPIO line event management
670 */
671
672/**
673 * struct lineevent_state - contains the state of a userspace event
674 * @gdev: the GPIO device the event pertains to
675 * @label: consumer label used to tag descriptors
676 * @desc: the GPIO descriptor held by this event
677 * @eflags: the event flags this line was requested with
678 * @irq: the interrupt that trigger in response to events on this GPIO
679 * @wait: wait queue that handles blocking reads of events
680 * @events: KFIFO for the GPIO events
681 * @read_lock: mutex lock to protect reads from colliding with adding
682 * new events to the FIFO
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100683 * @timestamp: cache for the timestamp storing it between hardirq
684 * and IRQ thread, used to bring the timestamp close to the actual
685 * event
Linus Walleij61f922d2016-06-02 11:30:15 +0200686 */
687struct lineevent_state {
688 struct gpio_device *gdev;
689 const char *label;
690 struct gpio_desc *desc;
691 u32 eflags;
692 int irq;
693 wait_queue_head_t wait;
694 DECLARE_KFIFO(events, struct gpioevent_data, 16);
695 struct mutex read_lock;
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100696 u64 timestamp;
Linus Walleij61f922d2016-06-02 11:30:15 +0200697};
698
Lars-Peter Clausenac7dbb92016-10-18 16:54:06 +0200699#define GPIOEVENT_REQUEST_VALID_FLAGS \
700 (GPIOEVENT_REQUEST_RISING_EDGE | \
701 GPIOEVENT_REQUEST_FALLING_EDGE)
702
Al Viroafc9a422017-07-03 06:39:46 -0400703static __poll_t lineevent_poll(struct file *filep,
Linus Walleij61f922d2016-06-02 11:30:15 +0200704 struct poll_table_struct *wait)
705{
706 struct lineevent_state *le = filep->private_data;
Al Viroafc9a422017-07-03 06:39:46 -0400707 __poll_t events = 0;
Linus Walleij61f922d2016-06-02 11:30:15 +0200708
709 poll_wait(filep, &le->wait, wait);
710
711 if (!kfifo_is_empty(&le->events))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800712 events = EPOLLIN | EPOLLRDNORM;
Linus Walleij61f922d2016-06-02 11:30:15 +0200713
714 return events;
715}
716
717
718static ssize_t lineevent_read(struct file *filep,
719 char __user *buf,
720 size_t count,
721 loff_t *f_ps)
722{
723 struct lineevent_state *le = filep->private_data;
724 unsigned int copied;
725 int ret;
726
727 if (count < sizeof(struct gpioevent_data))
728 return -EINVAL;
729
730 do {
731 if (kfifo_is_empty(&le->events)) {
732 if (filep->f_flags & O_NONBLOCK)
733 return -EAGAIN;
734
735 ret = wait_event_interruptible(le->wait,
736 !kfifo_is_empty(&le->events));
737 if (ret)
738 return ret;
739 }
740
741 if (mutex_lock_interruptible(&le->read_lock))
742 return -ERESTARTSYS;
743 ret = kfifo_to_user(&le->events, buf, count, &copied);
744 mutex_unlock(&le->read_lock);
745
746 if (ret)
747 return ret;
748
749 /*
750 * If we couldn't read anything from the fifo (a different
751 * thread might have been faster) we either return -EAGAIN if
752 * the file descriptor is non-blocking, otherwise we go back to
753 * sleep and wait for more data to arrive.
754 */
755 if (copied == 0 && (filep->f_flags & O_NONBLOCK))
756 return -EAGAIN;
757
758 } while (copied == 0);
759
760 return copied;
761}
762
763static int lineevent_release(struct inode *inode, struct file *filep)
764{
765 struct lineevent_state *le = filep->private_data;
766 struct gpio_device *gdev = le->gdev;
767
768 free_irq(le->irq, le);
769 gpiod_free(le->desc);
770 kfree(le->label);
771 kfree(le);
772 put_device(&gdev->dev);
773 return 0;
774}
775
776static long lineevent_ioctl(struct file *filep, unsigned int cmd,
777 unsigned long arg)
778{
779 struct lineevent_state *le = filep->private_data;
780 void __user *ip = (void __user *)arg;
781 struct gpiohandle_data ghd;
782
783 /*
784 * We can get the value for an event line but not set it,
785 * because it is input by definition.
786 */
787 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
788 int val;
789
Lars-Peter Clausend82aa4a2016-10-18 16:54:04 +0200790 memset(&ghd, 0, sizeof(ghd));
791
Linus Walleij61f922d2016-06-02 11:30:15 +0200792 val = gpiod_get_value_cansleep(le->desc);
793 if (val < 0)
794 return val;
795 ghd.values[0] = val;
796
797 if (copy_to_user(ip, &ghd, sizeof(ghd)))
798 return -EFAULT;
799
800 return 0;
801 }
802 return -EINVAL;
803}
804
805#ifdef CONFIG_COMPAT
806static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd,
807 unsigned long arg)
808{
809 return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
810}
811#endif
812
813static const struct file_operations lineevent_fileops = {
814 .release = lineevent_release,
815 .read = lineevent_read,
816 .poll = lineevent_poll,
817 .owner = THIS_MODULE,
818 .llseek = noop_llseek,
819 .unlocked_ioctl = lineevent_ioctl,
820#ifdef CONFIG_COMPAT
821 .compat_ioctl = lineevent_ioctl_compat,
822#endif
823};
824
Ben Dooks33265b12016-06-17 16:03:13 +0100825static irqreturn_t lineevent_irq_thread(int irq, void *p)
Linus Walleij61f922d2016-06-02 11:30:15 +0200826{
827 struct lineevent_state *le = p;
828 struct gpioevent_data ge;
Uwe Kleine-Königfa388692018-08-20 08:32:53 +0200829 int ret;
Linus Walleij61f922d2016-06-02 11:30:15 +0200830
Linus Walleij24bd3ef2018-01-22 13:19:28 +0100831 /* Do not leak kernel stack to userspace */
832 memset(&ge, 0, sizeof(ge));
833
Bartosz Golaszewski1033be52019-01-04 11:24:20 +0100834 /*
835 * We may be running from a nested threaded interrupt in which case
836 * we didn't get the timestamp from lineevent_irq_handler().
837 */
838 if (!le->timestamp)
839 ge.timestamp = ktime_get_real_ns();
840 else
841 ge.timestamp = le->timestamp;
Linus Walleij61f922d2016-06-02 11:30:15 +0200842
Bartosz Golaszewskiad537b82017-06-23 13:45:16 +0200843 if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
844 && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
Uwe Kleine-Königfa388692018-08-20 08:32:53 +0200845 int level = gpiod_get_value_cansleep(le->desc);
Linus Walleij61f922d2016-06-02 11:30:15 +0200846 if (level)
847 /* Emit low-to-high event */
848 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
849 else
850 /* Emit high-to-low event */
851 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Uwe Kleine-Königfa388692018-08-20 08:32:53 +0200852 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200853 /* Emit low-to-high event */
854 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
Uwe Kleine-Königfa388692018-08-20 08:32:53 +0200855 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200856 /* Emit high-to-low event */
857 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Arnd Bergmannbc0207a2016-06-16 11:02:41 +0200858 } else {
859 return IRQ_NONE;
Linus Walleij61f922d2016-06-02 11:30:15 +0200860 }
861
862 ret = kfifo_put(&le->events, ge);
Saiyam Doshi2efc6bf2019-09-07 23:09:10 +0530863 if (ret)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800864 wake_up_poll(&le->wait, EPOLLIN);
Linus Walleij61f922d2016-06-02 11:30:15 +0200865
866 return IRQ_HANDLED;
867}
868
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100869static irqreturn_t lineevent_irq_handler(int irq, void *p)
870{
871 struct lineevent_state *le = p;
872
873 /*
874 * Just store the timestamp in hardirq context so we get it as
875 * close in time as possible to the actual event.
876 */
877 le->timestamp = ktime_get_real_ns();
878
879 return IRQ_WAKE_THREAD;
880}
881
Linus Walleij61f922d2016-06-02 11:30:15 +0200882static int lineevent_create(struct gpio_device *gdev, void __user *ip)
883{
884 struct gpioevent_request eventreq;
885 struct lineevent_state *le;
886 struct gpio_desc *desc;
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200887 struct file *file;
Linus Walleij61f922d2016-06-02 11:30:15 +0200888 u32 offset;
889 u32 lflags;
890 u32 eflags;
891 int fd;
892 int ret;
893 int irqflags = 0;
894
895 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
896 return -EFAULT;
897
898 le = kzalloc(sizeof(*le), GFP_KERNEL);
899 if (!le)
900 return -ENOMEM;
901 le->gdev = gdev;
902 get_device(&gdev->dev);
903
904 /* Make sure this is terminated */
905 eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
906 if (strlen(eventreq.consumer_label)) {
907 le->label = kstrdup(eventreq.consumer_label,
908 GFP_KERNEL);
909 if (!le->label) {
910 ret = -ENOMEM;
911 goto out_free_le;
912 }
913 }
914
915 offset = eventreq.lineoffset;
916 lflags = eventreq.handleflags;
917 eflags = eventreq.eventflags;
918
Lars-Peter Clausenb8b0e3d2016-10-18 16:54:03 +0200919 if (offset >= gdev->ngpio) {
920 ret = -EINVAL;
921 goto out_free_label;
922 }
923
Lars-Peter Clausenac7dbb92016-10-18 16:54:06 +0200924 /* Return an error if a unknown flag is set */
925 if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
926 (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) {
927 ret = -EINVAL;
928 goto out_free_label;
929 }
930
Linus Walleij61f922d2016-06-02 11:30:15 +0200931 /* This is just wrong: we don't look for events on output lines */
Kent Gibson5ca2f542019-09-09 03:24:06 +0000932 if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) ||
933 (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
934 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200935 ret = -EINVAL;
936 goto out_free_label;
937 }
938
939 desc = &gdev->descs[offset];
940 ret = gpiod_request(desc, le->label);
941 if (ret)
Uwe Kleine-Königf001cc32018-04-16 13:17:53 +0200942 goto out_free_label;
Linus Walleij61f922d2016-06-02 11:30:15 +0200943 le->desc = desc;
944 le->eflags = eflags;
945
946 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
947 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
Linus Walleij61f922d2016-06-02 11:30:15 +0200948
949 ret = gpiod_direction_input(desc);
950 if (ret)
951 goto out_free_desc;
952
953 le->irq = gpiod_to_irq(desc);
954 if (le->irq <= 0) {
955 ret = -ENODEV;
956 goto out_free_desc;
957 }
958
959 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
Michael Wu223ecaf2019-07-08 13:23:08 +0800960 irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
961 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
Linus Walleij61f922d2016-06-02 11:30:15 +0200962 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
Michael Wu223ecaf2019-07-08 13:23:08 +0800963 irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
964 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
Linus Walleij61f922d2016-06-02 11:30:15 +0200965 irqflags |= IRQF_ONESHOT;
Linus Walleij61f922d2016-06-02 11:30:15 +0200966
967 INIT_KFIFO(le->events);
968 init_waitqueue_head(&le->wait);
969 mutex_init(&le->read_lock);
970
971 /* Request a thread to read the events */
972 ret = request_threaded_irq(le->irq,
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100973 lineevent_irq_handler,
Linus Walleij61f922d2016-06-02 11:30:15 +0200974 lineevent_irq_thread,
975 irqflags,
976 le->label,
977 le);
978 if (ret)
979 goto out_free_desc;
980
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200981 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleij61f922d2016-06-02 11:30:15 +0200982 if (fd < 0) {
983 ret = fd;
984 goto out_free_irq;
985 }
986
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200987 file = anon_inode_getfile("gpio-event",
988 &lineevent_fileops,
989 le,
990 O_RDONLY | O_CLOEXEC);
991 if (IS_ERR(file)) {
992 ret = PTR_ERR(file);
993 goto out_put_unused_fd;
994 }
995
Linus Walleij61f922d2016-06-02 11:30:15 +0200996 eventreq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200997 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200998 /*
999 * fput() will trigger the release() callback, so do not go onto
1000 * the regular error cleanup path here.
1001 */
1002 fput(file);
1003 put_unused_fd(fd);
1004 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +02001005 }
Linus Walleij61f922d2016-06-02 11:30:15 +02001006
Lars-Peter Clausen953b9562016-10-24 13:59:15 +02001007 fd_install(fd, file);
1008
Linus Walleij61f922d2016-06-02 11:30:15 +02001009 return 0;
1010
Lars-Peter Clausen953b9562016-10-24 13:59:15 +02001011out_put_unused_fd:
1012 put_unused_fd(fd);
Linus Walleij61f922d2016-06-02 11:30:15 +02001013out_free_irq:
1014 free_irq(le->irq, le);
1015out_free_desc:
1016 gpiod_free(le->desc);
1017out_free_label:
1018 kfree(le->label);
1019out_free_le:
1020 kfree(le);
1021 put_device(&gdev->dev);
1022 return ret;
1023}
1024
Thierry Reding950d55f52017-07-24 16:57:22 +02001025/*
Linus Walleij3c702e92015-10-21 15:29:53 +02001026 * gpio_ioctl() - ioctl handler for the GPIO chardev
1027 */
1028static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1029{
1030 struct gpio_device *gdev = filp->private_data;
1031 struct gpio_chip *chip = gdev->chip;
Linus Walleij8b92e172016-05-27 14:24:04 +02001032 void __user *ip = (void __user *)arg;
Linus Walleij3c702e92015-10-21 15:29:53 +02001033
1034 /* We fail any subsequent ioctl():s when the chip is gone */
1035 if (!chip)
1036 return -ENODEV;
1037
Linus Walleij521a2ad2016-02-12 22:25:22 +01001038 /* Fill in the struct and pass to userspace */
Linus Walleij3c702e92015-10-21 15:29:53 +02001039 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
Linus Walleij521a2ad2016-02-12 22:25:22 +01001040 struct gpiochip_info chipinfo;
1041
Lars-Peter Clausen0f4bbb22016-10-18 16:54:00 +02001042 memset(&chipinfo, 0, sizeof(chipinfo));
1043
Linus Walleij3c702e92015-10-21 15:29:53 +02001044 strncpy(chipinfo.name, dev_name(&gdev->dev),
1045 sizeof(chipinfo.name));
1046 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
Linus Walleijdf4878e2016-02-12 14:48:23 +01001047 strncpy(chipinfo.label, gdev->label,
1048 sizeof(chipinfo.label));
1049 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001050 chipinfo.lines = gdev->ngpio;
Linus Walleij3c702e92015-10-21 15:29:53 +02001051 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
1052 return -EFAULT;
1053 return 0;
Linus Walleij521a2ad2016-02-12 22:25:22 +01001054 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
1055 struct gpioline_info lineinfo;
1056 struct gpio_desc *desc;
1057
1058 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
1059 return -EFAULT;
Lars-Peter Clausen1f1cc452016-10-18 16:53:59 +02001060 if (lineinfo.line_offset >= gdev->ngpio)
Linus Walleij521a2ad2016-02-12 22:25:22 +01001061 return -EINVAL;
1062
1063 desc = &gdev->descs[lineinfo.line_offset];
1064 if (desc->name) {
1065 strncpy(lineinfo.name, desc->name,
1066 sizeof(lineinfo.name));
1067 lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
1068 } else {
1069 lineinfo.name[0] = '\0';
1070 }
1071 if (desc->label) {
Linus Walleij214338e2016-02-25 21:01:48 +01001072 strncpy(lineinfo.consumer, desc->label,
1073 sizeof(lineinfo.consumer));
1074 lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +01001075 } else {
Linus Walleij214338e2016-02-25 21:01:48 +01001076 lineinfo.consumer[0] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +01001077 }
1078
1079 /*
1080 * Userspace only need to know that the kernel is using
1081 * this GPIO so it can't use it.
1082 */
1083 lineinfo.flags = 0;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001084 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
1085 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
1086 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
1087 test_bit(FLAG_EXPORT, &desc->flags) ||
Stefan Wahren472a61e2019-08-14 14:00:35 +03001088 test_bit(FLAG_SYSFS, &desc->flags) ||
1089 !pinctrl_gpio_can_use_line(chip->base + lineinfo.line_offset))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001090 lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001091 if (test_bit(FLAG_IS_OUT, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001092 lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001093 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001094 lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001095 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Bartosz Golaszewski2c60e6b2019-08-06 13:41:51 +02001096 lineinfo.flags |= (GPIOLINE_FLAG_OPEN_DRAIN |
1097 GPIOLINE_FLAG_IS_OUT);
Linus Walleij9d8cc892016-02-22 13:44:53 +01001098 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Bartosz Golaszewski2c60e6b2019-08-06 13:41:51 +02001099 lineinfo.flags |= (GPIOLINE_FLAG_OPEN_SOURCE |
1100 GPIOLINE_FLAG_IS_OUT);
Linus Walleij521a2ad2016-02-12 22:25:22 +01001101
1102 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
1103 return -EFAULT;
1104 return 0;
Linus Walleijd7c51b42016-04-26 10:35:29 +02001105 } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
1106 return linehandle_create(gdev, ip);
Linus Walleij61f922d2016-06-02 11:30:15 +02001107 } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
1108 return lineevent_create(gdev, ip);
Linus Walleij3c702e92015-10-21 15:29:53 +02001109 }
1110 return -EINVAL;
1111}
1112
Linus Walleij8b92e172016-05-27 14:24:04 +02001113#ifdef CONFIG_COMPAT
1114static long gpio_ioctl_compat(struct file *filp, unsigned int cmd,
1115 unsigned long arg)
1116{
1117 return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
1118}
1119#endif
1120
Linus Walleij3c702e92015-10-21 15:29:53 +02001121/**
1122 * gpio_chrdev_open() - open the chardev for ioctl operations
1123 * @inode: inode for this chardev
1124 * @filp: file struct for storing private data
1125 * Returns 0 on success
1126 */
1127static int gpio_chrdev_open(struct inode *inode, struct file *filp)
1128{
1129 struct gpio_device *gdev = container_of(inode->i_cdev,
1130 struct gpio_device, chrdev);
1131
1132 /* Fail on open if the backing gpiochip is gone */
Stephen Boydfb505742017-01-09 11:47:44 -08001133 if (!gdev->chip)
Linus Walleij3c702e92015-10-21 15:29:53 +02001134 return -ENODEV;
1135 get_device(&gdev->dev);
1136 filp->private_data = gdev;
Lars-Peter Clausenf4e81c52016-11-30 13:05:21 +01001137
1138 return nonseekable_open(inode, filp);
Linus Walleij3c702e92015-10-21 15:29:53 +02001139}
1140
1141/**
1142 * gpio_chrdev_release() - close chardev after ioctl operations
1143 * @inode: inode for this chardev
1144 * @filp: file struct for storing private data
1145 * Returns 0 on success
1146 */
1147static int gpio_chrdev_release(struct inode *inode, struct file *filp)
1148{
1149 struct gpio_device *gdev = container_of(inode->i_cdev,
1150 struct gpio_device, chrdev);
1151
Linus Walleij3c702e92015-10-21 15:29:53 +02001152 put_device(&gdev->dev);
1153 return 0;
1154}
1155
1156
1157static const struct file_operations gpio_fileops = {
1158 .release = gpio_chrdev_release,
1159 .open = gpio_chrdev_open,
1160 .owner = THIS_MODULE,
Lars-Peter Clausenf4e81c52016-11-30 13:05:21 +01001161 .llseek = no_llseek,
Linus Walleij3c702e92015-10-21 15:29:53 +02001162 .unlocked_ioctl = gpio_ioctl,
Linus Walleij8b92e172016-05-27 14:24:04 +02001163#ifdef CONFIG_COMPAT
1164 .compat_ioctl = gpio_ioctl_compat,
1165#endif
Linus Walleij3c702e92015-10-21 15:29:53 +02001166};
1167
Linus Walleijff2b1352015-10-20 11:10:38 +02001168static void gpiodevice_release(struct device *dev)
1169{
1170 struct gpio_device *gdev = dev_get_drvdata(dev);
1171
1172 list_del(&gdev->list);
1173 ida_simple_remove(&gpio_ida, gdev->id);
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +01001174 kfree_const(gdev->label);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001175 kfree(gdev->descs);
Linus Walleij9efd9e62016-02-09 14:27:42 +01001176 kfree(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001177}
1178
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001179static int gpiochip_setup_dev(struct gpio_device *gdev)
1180{
Linus Walleijd377f562019-07-16 11:11:45 +02001181 int ret;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001182
1183 cdev_init(&gdev->chrdev, &gpio_fileops);
1184 gdev->chrdev.owner = THIS_MODULE;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001185 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001186
Linus Walleijd377f562019-07-16 11:11:45 +02001187 ret = cdev_device_add(&gdev->chrdev, &gdev->dev);
1188 if (ret)
1189 return ret;
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001190
1191 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
1192 MAJOR(gpio_devt), gdev->id);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001193
Linus Walleijd377f562019-07-16 11:11:45 +02001194 ret = gpiochip_sysfs_register(gdev);
1195 if (ret)
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001196 goto err_remove_device;
1197
1198 /* From this point, the .release() function cleans up gpio_device */
1199 gdev->dev.release = gpiodevice_release;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001200 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
1201 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
1202 dev_name(&gdev->dev), gdev->chip->label ? : "generic");
1203
1204 return 0;
1205
1206err_remove_device:
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001207 cdev_device_del(&gdev->chrdev, &gdev->dev);
Linus Walleijd377f562019-07-16 11:11:45 +02001208 return ret;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001209}
1210
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02001211static void gpiochip_machine_hog(struct gpio_chip *chip, struct gpiod_hog *hog)
1212{
1213 struct gpio_desc *desc;
1214 int rv;
1215
1216 desc = gpiochip_get_desc(chip, hog->chip_hwnum);
1217 if (IS_ERR(desc)) {
1218 pr_err("%s: unable to get GPIO desc: %ld\n",
1219 __func__, PTR_ERR(desc));
1220 return;
1221 }
1222
Dan Carpenterba3efdf2018-05-01 10:36:39 +03001223 if (test_bit(FLAG_IS_HOGGED, &desc->flags))
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02001224 return;
1225
1226 rv = gpiod_hog(desc, hog->line_name, hog->lflags, hog->dflags);
1227 if (rv)
1228 pr_err("%s: unable to hog GPIO line (%s:%u): %d\n",
1229 __func__, chip->label, hog->chip_hwnum, rv);
1230}
1231
1232static void machine_gpiochip_add(struct gpio_chip *chip)
1233{
1234 struct gpiod_hog *hog;
1235
1236 mutex_lock(&gpio_machine_hogs_mutex);
1237
1238 list_for_each_entry(hog, &gpio_machine_hogs, list) {
1239 if (!strcmp(chip->label, hog->chip_label))
1240 gpiochip_machine_hog(chip, hog);
1241 }
1242
1243 mutex_unlock(&gpio_machine_hogs_mutex);
1244}
1245
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001246static void gpiochip_setup_devs(void)
1247{
1248 struct gpio_device *gdev;
Linus Walleijd377f562019-07-16 11:11:45 +02001249 int ret;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001250
1251 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijd377f562019-07-16 11:11:45 +02001252 ret = gpiochip_setup_dev(gdev);
1253 if (ret)
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001254 pr_err("%s: Failed to initialize gpio device (%d)\n",
Linus Walleijd377f562019-07-16 11:11:45 +02001255 dev_name(&gdev->dev), ret);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001256 }
1257}
1258
Thierry Reding959bc7b2017-11-07 19:15:59 +01001259int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001260 struct lock_class_key *lock_key,
1261 struct lock_class_key *request_key)
David Brownelld2876d02008-02-04 22:28:20 -08001262{
1263 unsigned long flags;
Linus Walleijd377f562019-07-16 11:11:45 +02001264 int ret = 0;
Linus Walleijff2b1352015-10-20 11:10:38 +02001265 unsigned i;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001266 int base = chip->base;
Linus Walleijff2b1352015-10-20 11:10:38 +02001267 struct gpio_device *gdev;
David Brownelld2876d02008-02-04 22:28:20 -08001268
Linus Walleijff2b1352015-10-20 11:10:38 +02001269 /*
1270 * First: allocate and populate the internal stat container, and
1271 * set up the struct device.
1272 */
Josh Cartwright969f07b2016-02-17 16:44:15 -06001273 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
Linus Walleijff2b1352015-10-20 11:10:38 +02001274 if (!gdev)
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001275 return -ENOMEM;
Linus Walleij3c702e92015-10-21 15:29:53 +02001276 gdev->dev.bus = &gpio_bus_type;
Linus Walleijff2b1352015-10-20 11:10:38 +02001277 gdev->chip = chip;
1278 chip->gpiodev = gdev;
1279 if (chip->parent) {
1280 gdev->dev.parent = chip->parent;
1281 gdev->dev.of_node = chip->parent->of_node;
Thierry Redingacc6e332016-07-05 14:11:14 +02001282 }
1283
Linus Walleijff2b1352015-10-20 11:10:38 +02001284#ifdef CONFIG_OF_GPIO
1285 /* If the gpiochip has an assigned OF node this takes precedence */
Thierry Redingacc6e332016-07-05 14:11:14 +02001286 if (chip->of_node)
1287 gdev->dev.of_node = chip->of_node;
Biju Das6ff04972018-08-06 10:48:01 +01001288 else
1289 chip->of_node = gdev->dev.of_node;
Linus Walleijff2b1352015-10-20 11:10:38 +02001290#endif
Thierry Redingacc6e332016-07-05 14:11:14 +02001291
Linus Walleijff2b1352015-10-20 11:10:38 +02001292 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
1293 if (gdev->id < 0) {
Linus Walleijd377f562019-07-16 11:11:45 +02001294 ret = gdev->id;
Linus Walleijff2b1352015-10-20 11:10:38 +02001295 goto err_free_gdev;
1296 }
1297 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
1298 device_initialize(&gdev->dev);
1299 dev_set_drvdata(&gdev->dev, gdev);
1300 if (chip->parent && chip->parent->driver)
1301 gdev->owner = chip->parent->driver->owner;
1302 else if (chip->owner)
1303 /* TODO: remove chip->owner */
1304 gdev->owner = chip->owner;
1305 else
1306 gdev->owner = THIS_MODULE;
David Brownelld2876d02008-02-04 22:28:20 -08001307
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001308 gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001309 if (!gdev->descs) {
Linus Walleijd377f562019-07-16 11:11:45 +02001310 ret = -ENOMEM;
Vladimir Zapolskiya05a1402018-11-02 15:39:43 +02001311 goto err_free_ida;
Linus Walleijff2b1352015-10-20 11:10:38 +02001312 }
1313
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001314 if (chip->ngpio == 0) {
1315 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
Linus Walleijd377f562019-07-16 11:11:45 +02001316 ret = -EINVAL;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001317 goto err_free_descs;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001318 }
Linus Walleijdf4878e2016-02-12 14:48:23 +01001319
Laura Abbott30277432018-05-21 10:57:07 -07001320 if (chip->ngpio > FASTPATH_NGPIO)
1321 chip_warn(chip, "line cnt %u is greater than fast path cnt %u\n",
1322 chip->ngpio, FASTPATH_NGPIO);
1323
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +01001324 gdev->label = kstrdup_const(chip->label ?: "unknown", GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001325 if (!gdev->label) {
Linus Walleijd377f562019-07-16 11:11:45 +02001326 ret = -ENOMEM;
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001327 goto err_free_descs;
Linus Walleijdf4878e2016-02-12 14:48:23 +01001328 }
1329
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001330 gdev->ngpio = chip->ngpio;
Linus Walleij43c54ec2016-02-11 11:37:48 +01001331 gdev->data = data;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001332
David Brownelld2876d02008-02-04 22:28:20 -08001333 spin_lock_irqsave(&gpio_lock, flags);
1334
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001335 /*
1336 * TODO: this allocates a Linux GPIO number base in the global
1337 * GPIO numberspace for this chip. In the long run we want to
1338 * get *rid* of this numberspace and use only descriptors, but
1339 * it may be a pipe dream. It will not happen before we get rid
1340 * of the sysfs interface anyways.
1341 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001342 if (base < 0) {
1343 base = gpiochip_find_base(chip->ngpio);
1344 if (base < 0) {
Linus Walleijd377f562019-07-16 11:11:45 +02001345 ret = base;
Johan Hovold225fce82015-01-12 17:12:25 +01001346 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001347 goto err_free_label;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001348 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001349 /*
1350 * TODO: it should not be necessary to reflect the assigned
1351 * base outside of the GPIO subsystem. Go over drivers and
1352 * see if anyone makes use of this, else drop this and assign
1353 * a poison instead.
1354 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001355 chip->base = base;
1356 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001357 gdev->base = base;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001358
Linus Walleijd377f562019-07-16 11:11:45 +02001359 ret = gpiodev_add_to_list(gdev);
1360 if (ret) {
Johan Hovold05aa5202015-01-12 17:12:26 +01001361 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001362 goto err_free_label;
Johan Hovold05aa5202015-01-12 17:12:26 +01001363 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001364
Linus Walleij545ebd92016-05-30 17:11:59 +02001365 spin_unlock_irqrestore(&gpio_lock, flags);
1366
Ricardo Ribalda Delgado767cd172018-10-12 08:11:36 +02001367 for (i = 0; i < chip->ngpio; i++)
1368 gdev->descs[i].gdev = gdev;
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001369
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301370#ifdef CONFIG_PINCTRL
Linus Walleij20ec3e32016-02-11 11:03:06 +01001371 INIT_LIST_HEAD(&gdev->pin_ranges);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301372#endif
1373
Linus Walleijd377f562019-07-16 11:11:45 +02001374 ret = gpiochip_set_desc_names(chip);
1375 if (ret)
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001376 goto err_remove_from_list;
1377
Linus Walleij151a4102019-09-05 11:40:54 +02001378 ret = gpiochip_alloc_valid_mask(chip);
Linus Walleijd377f562019-07-16 11:11:45 +02001379 if (ret)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001380 goto err_remove_from_list;
1381
Linus Walleijd377f562019-07-16 11:11:45 +02001382 ret = of_gpiochip_add(chip);
1383 if (ret)
Linus Walleij48057ed2019-08-20 10:05:27 +02001384 goto err_free_gpiochip_mask;
Tomeu Vizoso28355f82015-07-14 10:29:54 +02001385
Linus Walleijd377f562019-07-16 11:11:45 +02001386 ret = gpiochip_init_valid_mask(chip);
1387 if (ret)
Geert Uytterhoeven35779892019-04-24 15:59:33 +02001388 goto err_remove_of_chip;
Ricardo Ribalda Delgadof8ec92a2018-10-05 08:52:58 +02001389
Ricardo Ribalda Delgado3edfb7b2018-10-05 08:53:00 +02001390 for (i = 0; i < chip->ngpio; i++) {
1391 struct gpio_desc *desc = &gdev->descs[i];
1392
Chris Packhamd95da992019-07-08 08:35:58 +12001393 if (chip->get_direction && gpiochip_line_is_valid(chip, i)) {
1394 if (!chip->get_direction(chip, i))
1395 set_bit(FLAG_IS_OUT, &desc->flags);
1396 else
1397 clear_bit(FLAG_IS_OUT, &desc->flags);
1398 } else {
1399 if (!chip->direction_input)
1400 set_bit(FLAG_IS_OUT, &desc->flags);
1401 else
1402 clear_bit(FLAG_IS_OUT, &desc->flags);
1403 }
Ricardo Ribalda Delgado3edfb7b2018-10-05 08:53:00 +02001404 }
1405
Mika Westerberg664e3e52014-01-08 12:40:54 +02001406 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001407
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02001408 machine_gpiochip_add(chip);
1409
Andy Shevchenko9411e3a2019-10-09 17:34:44 +03001410 ret = gpiochip_irqchip_init_hw(chip);
1411 if (ret)
1412 goto err_remove_acpi_chip;
1413
Linus Walleijfbdf8d42019-09-06 12:05:35 +02001414 ret = gpiochip_irqchip_init_valid_mask(chip);
1415 if (ret)
Linus Walleij48057ed2019-08-20 10:05:27 +02001416 goto err_remove_acpi_chip;
1417
Linus Walleijfbdf8d42019-09-06 12:05:35 +02001418 ret = gpiochip_add_irqchip(chip, lock_key, request_key);
1419 if (ret)
Linus Walleij48057ed2019-08-20 10:05:27 +02001420 goto err_remove_irqchip_mask;
1421
Linus Walleij3c702e92015-10-21 15:29:53 +02001422 /*
1423 * By first adding the chardev, and then adding the device,
1424 * we get a device node entry in sysfs under
1425 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1426 * coldplug of device nodes and other udev business.
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001427 * We can do this only if gpiolib has been initialized.
1428 * Otherwise, defer until later.
Linus Walleij3c702e92015-10-21 15:29:53 +02001429 */
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001430 if (gpiolib_initialized) {
Linus Walleijd377f562019-07-16 11:11:45 +02001431 ret = gpiochip_setup_dev(gdev);
1432 if (ret)
Linus Walleij48057ed2019-08-20 10:05:27 +02001433 goto err_remove_irqchip;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001434 }
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001435 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001436
Linus Walleij48057ed2019-08-20 10:05:27 +02001437err_remove_irqchip:
1438 gpiochip_irqchip_remove(chip);
1439err_remove_irqchip_mask:
1440 gpiochip_irqchip_free_valid_mask(chip);
Geert Uytterhoeven35779892019-04-24 15:59:33 +02001441err_remove_acpi_chip:
Johan Hovold225fce82015-01-12 17:12:25 +01001442 acpi_gpiochip_remove(chip);
Geert Uytterhoeven35779892019-04-24 15:59:33 +02001443err_remove_of_chip:
Johan Hovold6d867502015-05-04 17:23:25 +02001444 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +01001445 of_gpiochip_remove(chip);
Geert Uytterhoeven35779892019-04-24 15:59:33 +02001446err_free_gpiochip_mask:
Stephen Boyd726cb3b2018-03-23 09:34:52 -07001447 gpiochip_free_valid_mask(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001448err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +01001449 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001450 list_del(&gdev->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001451 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001452err_free_label:
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +01001453 kfree_const(gdev->label);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001454err_free_descs:
1455 kfree(gdev->descs);
Vladimir Zapolskiya05a1402018-11-02 15:39:43 +02001456err_free_ida:
Linus Walleijff2b1352015-10-20 11:10:38 +02001457 ida_simple_remove(&gpio_ida, gdev->id);
Vladimir Zapolskiya05a1402018-11-02 15:39:43 +02001458err_free_gdev:
David Brownelld2876d02008-02-04 22:28:20 -08001459 /* failures here can mean systems won't boot... */
Marcel Ziswiler1777fc92018-07-20 09:54:49 +02001460 pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__,
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001461 gdev->base, gdev->base + gdev->ngpio - 1,
Linus Walleijd377f562019-07-16 11:11:45 +02001462 chip->label ? : "generic", ret);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001463 kfree(gdev);
Linus Walleijd377f562019-07-16 11:11:45 +02001464 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08001465}
Thierry Reding959bc7b2017-11-07 19:15:59 +01001466EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);
David Brownelld2876d02008-02-04 22:28:20 -08001467
1468/**
Linus Walleij43c54ec2016-02-11 11:37:48 +01001469 * gpiochip_get_data() - get per-subdriver data for the chip
Thierry Reding950d55f52017-07-24 16:57:22 +02001470 * @chip: GPIO chip
1471 *
1472 * Returns:
1473 * The per-subdriver data for the chip.
Linus Walleij43c54ec2016-02-11 11:37:48 +01001474 */
1475void *gpiochip_get_data(struct gpio_chip *chip)
1476{
1477 return chip->gpiodev->data;
1478}
1479EXPORT_SYMBOL_GPL(gpiochip_get_data);
1480
1481/**
David Brownelld2876d02008-02-04 22:28:20 -08001482 * gpiochip_remove() - unregister a gpio_chip
1483 * @chip: the chip to unregister
1484 *
1485 * A gpio_chip with any GPIOs still requested may not be removed.
1486 */
abdoulaye berthee1db1702014-07-05 18:28:50 +02001487void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -08001488{
Linus Walleijff2b1352015-10-20 11:10:38 +02001489 struct gpio_device *gdev = chip->gpiodev;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001490 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001491 unsigned long flags;
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001492 unsigned i;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001493 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -08001494
Linus Walleijff2b1352015-10-20 11:10:38 +02001495 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
Linus Walleijafbc4f32016-02-09 13:21:06 +01001496 gpiochip_sysfs_unregister(gdev);
Geert Uytterhoeven5018ada2016-12-19 18:29:23 +01001497 gpiochip_free_hogs(chip);
Bamvor Jian Zhangbd203bd2016-02-20 13:13:19 +08001498 /* Numb the device, cancelling all outstanding operations */
1499 gdev->chip = NULL;
Johan Hovold00acc3d2015-01-12 17:12:27 +01001500 gpiochip_irqchip_remove(chip);
Mika Westerberg6072b9d2014-03-10 14:54:53 +02001501 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +01001502 gpiochip_remove_pin_ranges(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001503 of_gpiochip_remove(chip);
Stephen Boyd726cb3b2018-03-23 09:34:52 -07001504 gpiochip_free_valid_mask(chip);
Linus Walleij43c54ec2016-02-11 11:37:48 +01001505 /*
1506 * We accept no more calls into the driver from this point, so
1507 * NULL the driver data pointer
1508 */
1509 gdev->data = NULL;
Anton Vorontsov391c9702010-06-08 07:48:17 -06001510
Johan Hovold6798aca2015-01-12 17:12:28 +01001511 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001512 for (i = 0; i < gdev->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001513 desc = &gdev->descs[i];
Johan Hovoldfab28b82015-05-04 17:10:27 +02001514 if (test_bit(FLAG_REQUESTED, &desc->flags))
1515 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -08001516 }
David Brownelld2876d02008-02-04 22:28:20 -08001517 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001518
Johan Hovoldfab28b82015-05-04 17:10:27 +02001519 if (requested)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001520 dev_crit(&gdev->dev,
Linus Walleij58383c782015-11-04 09:56:26 +01001521 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +02001522
Linus Walleijff2b1352015-10-20 11:10:38 +02001523 /*
1524 * The gpiochip side puts its use of the device to rest here:
1525 * if there are no userspace clients, the chardev and device will
1526 * be removed, else it will be dangling until the last user is
1527 * gone.
1528 */
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001529 cdev_device_del(&gdev->chrdev, &gdev->dev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001530 put_device(&gdev->dev);
David Brownelld2876d02008-02-04 22:28:20 -08001531}
1532EXPORT_SYMBOL_GPL(gpiochip_remove);
1533
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301534static void devm_gpio_chip_release(struct device *dev, void *res)
1535{
1536 struct gpio_chip *chip = *(struct gpio_chip **)res;
1537
1538 gpiochip_remove(chip);
1539}
1540
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301541/**
Jonathan Neuschäfer689fd022017-12-21 17:56:34 +01001542 * devm_gpiochip_add_data() - Resource manager gpiochip_add_data()
Uwe Kleine-König3925b90f2018-10-08 11:34:03 +02001543 * @dev: pointer to the device that gpio_chip belongs to.
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301544 * @chip: the chip to register, with chip->base initialized
Thierry Reding950d55f52017-07-24 16:57:22 +02001545 * @data: driver-private data associated with this chip
1546 *
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301547 * Context: potentially before irqs will work
1548 *
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301549 * The gpio chip automatically be released when the device is unbound.
Thierry Reding950d55f52017-07-24 16:57:22 +02001550 *
1551 * Returns:
1552 * A negative errno if the chip can't be registered, such as because the
1553 * chip->base is invalid or already associated with a different chip.
1554 * Otherwise it returns zero as a success code.
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301555 */
1556int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
1557 void *data)
1558{
1559 struct gpio_chip **ptr;
1560 int ret;
1561
1562 ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
1563 GFP_KERNEL);
1564 if (!ptr)
1565 return -ENOMEM;
1566
1567 ret = gpiochip_add_data(chip, data);
1568 if (ret < 0) {
1569 devres_free(ptr);
1570 return ret;
1571 }
1572
1573 *ptr = chip;
1574 devres_add(dev, ptr);
1575
1576 return 0;
1577}
1578EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
1579
1580/**
Grant Likely594fa262010-06-08 07:48:16 -06001581 * gpiochip_find() - iterator for locating a specific gpio_chip
1582 * @data: data to pass to match function
Thierry Reding950d55f52017-07-24 16:57:22 +02001583 * @match: Callback function to check gpio_chip
Grant Likely594fa262010-06-08 07:48:16 -06001584 *
1585 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1586 * determined by a user supplied @match callback. The callback should return
1587 * 0 if the device doesn't match and non-zero if it does. If the callback is
1588 * non-zero, this function will return to the caller and not iterate over any
1589 * more gpio_chips.
1590 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001591struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001592 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001593 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001594{
Linus Walleijff2b1352015-10-20 11:10:38 +02001595 struct gpio_device *gdev;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001596 struct gpio_chip *chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -06001597 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001598
1599 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001600 list_for_each_entry(gdev, &gpio_devices, list)
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001601 if (gdev->chip && match(gdev->chip, data)) {
1602 chip = gdev->chip;
Grant Likely594fa262010-06-08 07:48:16 -06001603 break;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001604 }
Linus Walleijff2b1352015-10-20 11:10:38 +02001605
Grant Likely594fa262010-06-08 07:48:16 -06001606 spin_unlock_irqrestore(&gpio_lock, flags);
1607
1608 return chip;
1609}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001610EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001611
Alexandre Courbot79697ef2013-11-16 21:39:32 +09001612static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1613{
1614 const char *name = data;
1615
1616 return !strcmp(chip->label, name);
1617}
1618
1619static struct gpio_chip *find_chip_by_name(const char *name)
1620{
1621 return gpiochip_find((void *)name, gpiochip_match_name);
1622}
1623
Linus Walleij14250522014-03-25 10:40:18 +01001624#ifdef CONFIG_GPIOLIB_IRQCHIP
1625
1626/*
1627 * The following is irqchip helper code for gpiochips.
1628 */
1629
Andy Shevchenko9411e3a2019-10-09 17:34:44 +03001630static int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
1631{
1632 struct gpio_irq_chip *girq = &gc->irq;
1633
1634 if (!girq->init_hw)
1635 return 0;
1636
1637 return girq->init_hw(gc);
1638}
1639
Linus Walleij5fbe5b52019-09-04 16:01:04 +02001640static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001641{
Linus Walleij5fbe5b52019-09-04 16:01:04 +02001642 struct gpio_irq_chip *girq = &gc->irq;
1643
1644 if (!girq->init_valid_mask)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001645 return 0;
1646
Linus Walleij5fbe5b52019-09-04 16:01:04 +02001647 girq->valid_mask = gpiochip_allocate_mask(gc);
1648 if (!girq->valid_mask)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001649 return -ENOMEM;
1650
Linus Walleij5fbe5b52019-09-04 16:01:04 +02001651 girq->init_valid_mask(gc, girq->valid_mask, gc->ngpio);
1652
Mika Westerberg79b804c2016-09-20 15:15:21 +03001653 return 0;
1654}
1655
1656static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1657{
Masahiro Yamada7bdbd1ec2019-07-18 15:51:01 +09001658 bitmap_free(gpiochip->irq.valid_mask);
Thierry Redingdc7b0382017-11-07 19:15:52 +01001659 gpiochip->irq.valid_mask = NULL;
Mika Westerberg79b804c2016-09-20 15:15:21 +03001660}
1661
Stephen Boyd64ff2c82018-01-09 17:58:46 -08001662bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
1663 unsigned int offset)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001664{
Stephen Boyd726cb3b2018-03-23 09:34:52 -07001665 if (!gpiochip_line_is_valid(gpiochip, offset))
1666 return false;
Mika Westerberg79b804c2016-09-20 15:15:21 +03001667 /* No mask means all valid */
Thierry Redingdc7b0382017-11-07 19:15:52 +01001668 if (likely(!gpiochip->irq.valid_mask))
Mika Westerberg79b804c2016-09-20 15:15:21 +03001669 return true;
Thierry Redingdc7b0382017-11-07 19:15:52 +01001670 return test_bit(offset, gpiochip->irq.valid_mask);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001671}
Stephen Boyd64ff2c82018-01-09 17:58:46 -08001672EXPORT_SYMBOL_GPL(gpiochip_irqchip_irq_valid);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001673
Linus Walleij14250522014-03-25 10:40:18 +01001674/**
Linus Walleijd245b3f2016-11-24 10:57:25 +01001675 * gpiochip_set_cascaded_irqchip() - connects a cascaded irqchip to a gpiochip
Linus Walleij4892d3a2019-06-14 10:12:26 +02001676 * @gc: the gpiochip to set the irqchip chain to
Linus Walleij14250522014-03-25 10:40:18 +01001677 * @parent_irq: the irq number corresponding to the parent IRQ for this
1678 * chained irqchip
1679 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001680 * coming out of the gpiochip. If the interrupt is nested rather than
1681 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +01001682 */
Linus Walleij4892d3a2019-06-14 10:12:26 +02001683static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gc,
Thierry Reding6f793092017-04-03 18:05:21 +02001684 unsigned int parent_irq,
Linus Walleijd245b3f2016-11-24 10:57:25 +01001685 irq_flow_handler_t parent_handler)
Linus Walleij14250522014-03-25 10:40:18 +01001686{
Linus Walleij4892d3a2019-06-14 10:12:26 +02001687 struct gpio_irq_chip *girq = &gc->irq;
1688 struct device *dev = &gc->gpiodev->dev;
1689
1690 if (!girq->domain) {
1691 chip_err(gc, "called %s before setting up irqchip\n",
Linus Walleij83141a72014-09-26 13:50:12 +02001692 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +02001693 return;
1694 }
1695
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001696 if (parent_handler) {
Linus Walleij4892d3a2019-06-14 10:12:26 +02001697 if (gc->can_sleep) {
1698 chip_err(gc,
Andy Shevchenkob1911712018-07-03 03:39:03 +03001699 "you cannot have chained interrupts on a chip that may sleep\n");
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001700 return;
1701 }
Linus Walleij4892d3a2019-06-14 10:12:26 +02001702 girq->parents = devm_kcalloc(dev, 1,
1703 sizeof(*girq->parents),
1704 GFP_KERNEL);
1705 if (!girq->parents) {
1706 chip_err(gc, "out of memory allocating parent IRQ\n");
1707 return;
1708 }
1709 girq->parents[0] = parent_irq;
1710 girq->num_parents = 1;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001711 /*
1712 * The parent irqchip is already using the chip_data for this
1713 * irqchip, so our callbacks simply use the handler_data.
1714 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +02001715 irq_set_chained_handler_and_data(parent_irq, parent_handler,
Linus Walleij4892d3a2019-06-14 10:12:26 +02001716 gc);
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001717 }
Linus Walleij14250522014-03-25 10:40:18 +01001718}
Linus Walleijd245b3f2016-11-24 10:57:25 +01001719
1720/**
1721 * gpiochip_set_chained_irqchip() - connects a chained irqchip to a gpiochip
1722 * @gpiochip: the gpiochip to set the irqchip chain to
1723 * @irqchip: the irqchip to chain to the gpiochip
1724 * @parent_irq: the irq number corresponding to the parent IRQ for this
1725 * chained irqchip
1726 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Stephen Boyd40f5ff42018-10-08 09:32:16 -07001727 * coming out of the gpiochip.
Linus Walleijd245b3f2016-11-24 10:57:25 +01001728 */
1729void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
1730 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001731 unsigned int parent_irq,
Linus Walleijd245b3f2016-11-24 10:57:25 +01001732 irq_flow_handler_t parent_handler)
1733{
Thierry Reding60ed54c2017-11-07 19:15:57 +01001734 if (gpiochip->irq.threaded) {
1735 chip_err(gpiochip, "tried to chain a threaded gpiochip\n");
1736 return;
1737 }
1738
Stephen Boyd3c1f6b22018-10-08 09:32:15 -07001739 gpiochip_set_cascaded_irqchip(gpiochip, parent_irq, parent_handler);
Linus Walleijd245b3f2016-11-24 10:57:25 +01001740}
Linus Walleij14250522014-03-25 10:40:18 +01001741EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
1742
1743/**
Linus Walleijd245b3f2016-11-24 10:57:25 +01001744 * gpiochip_set_nested_irqchip() - connects a nested irqchip to a gpiochip
1745 * @gpiochip: the gpiochip to set the irqchip nested handler to
1746 * @irqchip: the irqchip to nest to the gpiochip
1747 * @parent_irq: the irq number corresponding to the parent IRQ for this
1748 * nested irqchip
1749 */
1750void gpiochip_set_nested_irqchip(struct gpio_chip *gpiochip,
1751 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001752 unsigned int parent_irq)
Linus Walleijd245b3f2016-11-24 10:57:25 +01001753{
Stephen Boyd3c1f6b22018-10-08 09:32:15 -07001754 gpiochip_set_cascaded_irqchip(gpiochip, parent_irq, NULL);
Linus Walleijd245b3f2016-11-24 10:57:25 +01001755}
1756EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip);
1757
Linus Walleijfdd61a02019-08-08 14:32:37 +02001758#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1759
1760/**
1761 * gpiochip_set_hierarchical_irqchip() - connects a hierarchical irqchip
1762 * to a gpiochip
1763 * @gc: the gpiochip to set the irqchip hierarchical handler to
1764 * @irqchip: the irqchip to handle this level of the hierarchy, the interrupt
1765 * will then percolate up to the parent
1766 */
1767static void gpiochip_set_hierarchical_irqchip(struct gpio_chip *gc,
1768 struct irq_chip *irqchip)
1769{
1770 /* DT will deal with mapping each IRQ as we go along */
1771 if (is_of_node(gc->irq.fwnode))
1772 return;
1773
1774 /*
1775 * This is for legacy and boardfile "irqchip" fwnodes: allocate
1776 * irqs upfront instead of dynamically since we don't have the
1777 * dynamic type of allocation that hardware description languages
1778 * provide. Once all GPIO drivers using board files are gone from
1779 * the kernel we can delete this code, but for a transitional period
1780 * it is necessary to keep this around.
1781 */
1782 if (is_fwnode_irqchip(gc->irq.fwnode)) {
1783 int i;
1784 int ret;
1785
1786 for (i = 0; i < gc->ngpio; i++) {
1787 struct irq_fwspec fwspec;
1788 unsigned int parent_hwirq;
1789 unsigned int parent_type;
1790 struct gpio_irq_chip *girq = &gc->irq;
1791
1792 /*
1793 * We call the child to parent translation function
1794 * only to check if the child IRQ is valid or not.
1795 * Just pick the rising edge type here as that is what
1796 * we likely need to support.
1797 */
1798 ret = girq->child_to_parent_hwirq(gc, i,
1799 IRQ_TYPE_EDGE_RISING,
1800 &parent_hwirq,
1801 &parent_type);
1802 if (ret) {
1803 chip_err(gc, "skip set-up on hwirq %d\n",
1804 i);
1805 continue;
1806 }
1807
1808 fwspec.fwnode = gc->irq.fwnode;
1809 /* This is the hwirq for the GPIO line side of things */
1810 fwspec.param[0] = girq->child_offset_to_irq(gc, i);
1811 /* Just pick something */
1812 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
1813 fwspec.param_count = 2;
1814 ret = __irq_domain_alloc_irqs(gc->irq.domain,
1815 /* just pick something */
1816 -1,
1817 1,
1818 NUMA_NO_NODE,
1819 &fwspec,
1820 false,
1821 NULL);
1822 if (ret < 0) {
1823 chip_err(gc,
1824 "can not allocate irq for GPIO line %d parent hwirq %d in hierarchy domain: %d\n",
1825 i, parent_hwirq,
1826 ret);
1827 }
1828 }
1829 }
1830
1831 chip_err(gc, "%s unknown fwnode type proceed anyway\n", __func__);
1832
1833 return;
1834}
1835
1836static int gpiochip_hierarchy_irq_domain_translate(struct irq_domain *d,
1837 struct irq_fwspec *fwspec,
1838 unsigned long *hwirq,
1839 unsigned int *type)
1840{
1841 /* We support standard DT translation */
1842 if (is_of_node(fwspec->fwnode) && fwspec->param_count == 2) {
1843 return irq_domain_translate_twocell(d, fwspec, hwirq, type);
1844 }
1845
1846 /* This is for board files and others not using DT */
1847 if (is_fwnode_irqchip(fwspec->fwnode)) {
1848 int ret;
1849
1850 ret = irq_domain_translate_twocell(d, fwspec, hwirq, type);
1851 if (ret)
1852 return ret;
1853 WARN_ON(*type == IRQ_TYPE_NONE);
1854 return 0;
1855 }
1856 return -EINVAL;
1857}
1858
1859static int gpiochip_hierarchy_irq_domain_alloc(struct irq_domain *d,
1860 unsigned int irq,
1861 unsigned int nr_irqs,
1862 void *data)
1863{
1864 struct gpio_chip *gc = d->host_data;
1865 irq_hw_number_t hwirq;
1866 unsigned int type = IRQ_TYPE_NONE;
1867 struct irq_fwspec *fwspec = data;
1868 struct irq_fwspec parent_fwspec;
1869 unsigned int parent_hwirq;
1870 unsigned int parent_type;
1871 struct gpio_irq_chip *girq = &gc->irq;
1872 int ret;
1873
1874 /*
1875 * The nr_irqs parameter is always one except for PCI multi-MSI
1876 * so this should not happen.
1877 */
1878 WARN_ON(nr_irqs != 1);
1879
1880 ret = gc->irq.child_irq_domain_ops.translate(d, fwspec, &hwirq, &type);
1881 if (ret)
1882 return ret;
1883
1884 chip_info(gc, "allocate IRQ %d, hwirq %lu\n", irq, hwirq);
1885
1886 ret = girq->child_to_parent_hwirq(gc, hwirq, type,
1887 &parent_hwirq, &parent_type);
1888 if (ret) {
1889 chip_err(gc, "can't look up hwirq %lu\n", hwirq);
1890 return ret;
1891 }
1892 chip_info(gc, "found parent hwirq %u\n", parent_hwirq);
1893
1894 /*
1895 * We set handle_bad_irq because the .set_type() should
1896 * always be invoked and set the right type of handler.
1897 */
1898 irq_domain_set_info(d,
1899 irq,
1900 hwirq,
1901 gc->irq.chip,
1902 gc,
1903 girq->handler,
1904 NULL, NULL);
1905 irq_set_probe(irq);
1906
1907 /*
1908 * Create a IRQ fwspec to send up to the parent irqdomain:
1909 * specify the hwirq we address on the parent and tie it
1910 * all together up the chain.
1911 */
1912 parent_fwspec.fwnode = d->parent->fwnode;
1913 /* This parent only handles asserted level IRQs */
1914 girq->populate_parent_fwspec(gc, &parent_fwspec, parent_hwirq,
1915 parent_type);
1916 chip_info(gc, "alloc_irqs_parent for %d parent hwirq %d\n",
1917 irq, parent_hwirq);
1918 ret = irq_domain_alloc_irqs_parent(d, irq, 1, &parent_fwspec);
1919 if (ret)
1920 chip_err(gc,
1921 "failed to allocate parent hwirq %d for hwirq %lu\n",
1922 parent_hwirq, hwirq);
1923
1924 return ret;
1925}
1926
1927static unsigned int gpiochip_child_offset_to_irq_noop(struct gpio_chip *chip,
1928 unsigned int offset)
1929{
1930 return offset;
1931}
1932
1933static void gpiochip_hierarchy_setup_domain_ops(struct irq_domain_ops *ops)
1934{
1935 ops->activate = gpiochip_irq_domain_activate;
1936 ops->deactivate = gpiochip_irq_domain_deactivate;
1937 ops->alloc = gpiochip_hierarchy_irq_domain_alloc;
1938 ops->free = irq_domain_free_irqs_common;
1939
1940 /*
1941 * We only allow overriding the translate() function for
1942 * hierarchical chips, and this should only be done if the user
1943 * really need something other than 1:1 translation.
1944 */
1945 if (!ops->translate)
1946 ops->translate = gpiochip_hierarchy_irq_domain_translate;
1947}
1948
1949static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
1950{
1951 if (!gc->irq.child_to_parent_hwirq ||
1952 !gc->irq.fwnode) {
1953 chip_err(gc, "missing irqdomain vital data\n");
1954 return -EINVAL;
1955 }
1956
1957 if (!gc->irq.child_offset_to_irq)
1958 gc->irq.child_offset_to_irq = gpiochip_child_offset_to_irq_noop;
1959
1960 if (!gc->irq.populate_parent_fwspec)
1961 gc->irq.populate_parent_fwspec =
1962 gpiochip_populate_parent_fwspec_twocell;
1963
1964 gpiochip_hierarchy_setup_domain_ops(&gc->irq.child_irq_domain_ops);
1965
1966 gc->irq.domain = irq_domain_create_hierarchy(
1967 gc->irq.parent_domain,
1968 0,
1969 gc->ngpio,
1970 gc->irq.fwnode,
1971 &gc->irq.child_irq_domain_ops,
1972 gc);
1973
1974 if (!gc->irq.domain)
1975 return -ENOMEM;
1976
1977 gpiochip_set_hierarchical_irqchip(gc, gc->irq.chip);
1978
1979 return 0;
1980}
1981
1982static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
1983{
1984 return !!gc->irq.parent_domain;
1985}
1986
1987void gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *chip,
1988 struct irq_fwspec *fwspec,
1989 unsigned int parent_hwirq,
1990 unsigned int parent_type)
1991{
1992 fwspec->param_count = 2;
1993 fwspec->param[0] = parent_hwirq;
1994 fwspec->param[1] = parent_type;
1995}
1996EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_twocell);
1997
1998void gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *chip,
1999 struct irq_fwspec *fwspec,
2000 unsigned int parent_hwirq,
2001 unsigned int parent_type)
2002{
2003 fwspec->param_count = 4;
2004 fwspec->param[0] = 0;
2005 fwspec->param[1] = parent_hwirq;
2006 fwspec->param[2] = 0;
2007 fwspec->param[3] = parent_type;
2008}
2009EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_fourcell);
2010
2011#else
2012
2013static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
2014{
2015 return -EINVAL;
2016}
2017
2018static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
2019{
2020 return false;
2021}
2022
2023#endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
2024
Linus Walleijd245b3f2016-11-24 10:57:25 +01002025/**
Linus Walleij14250522014-03-25 10:40:18 +01002026 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
2027 * @d: the irqdomain used by this irqchip
2028 * @irq: the global irq number used by this GPIO irqchip irq
2029 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
2030 *
2031 * This function will set up the mapping for a certain IRQ line on a
2032 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
2033 * stored inside the gpiochip.
2034 */
Thierry Reding1b95b4e2017-11-07 19:15:55 +01002035int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
2036 irq_hw_number_t hwirq)
Linus Walleij14250522014-03-25 10:40:18 +01002037{
2038 struct gpio_chip *chip = d->host_data;
Linus Walleijd377f562019-07-16 11:11:45 +02002039 int ret = 0;
Linus Walleij14250522014-03-25 10:40:18 +01002040
Grygorii Strashkodc749a02017-07-21 11:49:00 -05002041 if (!gpiochip_irqchip_irq_valid(chip, hwirq))
2042 return -ENXIO;
2043
Linus Walleij14250522014-03-25 10:40:18 +01002044 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03002045 /*
2046 * This lock class tells lockdep that GPIO irqs are in a different
2047 * category than their parents, so it won't report false recursion.
2048 */
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002049 irq_set_lockdep_class(irq, chip->irq.lock_key, chip->irq.request_key);
Thierry Redingc7a0aa52017-11-07 19:15:48 +01002050 irq_set_chip_and_handler(irq, chip->irq.chip, chip->irq.handler);
Linus Walleijd245b3f2016-11-24 10:57:25 +01002051 /* Chips that use nested thread handlers have them marked */
Thierry Reding60ed54c2017-11-07 19:15:57 +01002052 if (chip->irq.threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02002053 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +01002054 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -05002055
Thierry Redinge0d89722017-11-07 19:15:54 +01002056 if (chip->irq.num_parents == 1)
Linus Walleijd377f562019-07-16 11:11:45 +02002057 ret = irq_set_parent(irq, chip->irq.parents[0]);
Thierry Redinge0d89722017-11-07 19:15:54 +01002058 else if (chip->irq.map)
Linus Walleijd377f562019-07-16 11:11:45 +02002059 ret = irq_set_parent(irq, chip->irq.map[hwirq]);
Thierry Redinge0d89722017-11-07 19:15:54 +01002060
Linus Walleijd377f562019-07-16 11:11:45 +02002061 if (ret < 0)
2062 return ret;
Thierry Redinge0d89722017-11-07 19:15:54 +01002063
Linus Walleij1333b902014-04-23 16:45:12 +02002064 /*
2065 * No set-up of the hardware will happen if IRQ_TYPE_NONE
2066 * is passed as default type.
2067 */
Thierry Reding3634eeb2017-11-07 19:15:49 +01002068 if (chip->irq.default_type != IRQ_TYPE_NONE)
2069 irq_set_irq_type(irq, chip->irq.default_type);
Linus Walleij14250522014-03-25 10:40:18 +01002070
2071 return 0;
2072}
Thierry Reding1b95b4e2017-11-07 19:15:55 +01002073EXPORT_SYMBOL_GPL(gpiochip_irq_map);
Linus Walleij14250522014-03-25 10:40:18 +01002074
Thierry Reding1b95b4e2017-11-07 19:15:55 +01002075void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
Linus Walleijc3626fd2014-03-28 20:42:01 +01002076{
Linus Walleij1c8732b2014-04-09 13:34:39 +02002077 struct gpio_chip *chip = d->host_data;
2078
Thierry Reding60ed54c2017-11-07 19:15:57 +01002079 if (chip->irq.threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02002080 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +01002081 irq_set_chip_and_handler(irq, NULL, NULL);
2082 irq_set_chip_data(irq, NULL);
2083}
Thierry Reding1b95b4e2017-11-07 19:15:55 +01002084EXPORT_SYMBOL_GPL(gpiochip_irq_unmap);
Linus Walleijc3626fd2014-03-28 20:42:01 +01002085
Linus Walleij14250522014-03-25 10:40:18 +01002086static const struct irq_domain_ops gpiochip_domain_ops = {
2087 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +01002088 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +01002089 /* Virtually all GPIO irqchips are twocell:ed */
2090 .xlate = irq_domain_xlate_twocell,
2091};
2092
Linus Walleijfdd61a02019-08-08 14:32:37 +02002093/*
2094 * TODO: move these activate/deactivate in under the hierarchicial
2095 * irqchip implementation as static once SPMI and SSBI (all external
2096 * users) are phased over.
2097 */
Brian Masneyef74f702019-01-19 15:42:42 -05002098/**
2099 * gpiochip_irq_domain_activate() - Lock a GPIO to be used as an IRQ
2100 * @domain: The IRQ domain used by this IRQ chip
2101 * @data: Outermost irq_data associated with the IRQ
2102 * @reserve: If set, only reserve an interrupt vector instead of assigning one
2103 *
2104 * This function is a wrapper that calls gpiochip_lock_as_irq() and is to be
2105 * used as the activate function for the &struct irq_domain_ops. The host_data
2106 * for the IRQ domain must be the &struct gpio_chip.
2107 */
2108int gpiochip_irq_domain_activate(struct irq_domain *domain,
2109 struct irq_data *data, bool reserve)
2110{
2111 struct gpio_chip *chip = domain->host_data;
2112
2113 return gpiochip_lock_as_irq(chip, data->hwirq);
2114}
2115EXPORT_SYMBOL_GPL(gpiochip_irq_domain_activate);
2116
2117/**
2118 * gpiochip_irq_domain_deactivate() - Unlock a GPIO used as an IRQ
2119 * @domain: The IRQ domain used by this IRQ chip
2120 * @data: Outermost irq_data associated with the IRQ
2121 *
2122 * This function is a wrapper that will call gpiochip_unlock_as_irq() and is to
2123 * be used as the deactivate function for the &struct irq_domain_ops. The
2124 * host_data for the IRQ domain must be the &struct gpio_chip.
2125 */
2126void gpiochip_irq_domain_deactivate(struct irq_domain *domain,
2127 struct irq_data *data)
2128{
2129 struct gpio_chip *chip = domain->host_data;
2130
2131 return gpiochip_unlock_as_irq(chip, data->hwirq);
2132}
2133EXPORT_SYMBOL_GPL(gpiochip_irq_domain_deactivate);
2134
Linus Walleij14250522014-03-25 10:40:18 +01002135static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
2136{
Linus Walleijfdd61a02019-08-08 14:32:37 +02002137 struct irq_domain *domain = chip->irq.domain;
2138
Grygorii Strashkodc749a02017-07-21 11:49:00 -05002139 if (!gpiochip_irqchip_irq_valid(chip, offset))
2140 return -ENXIO;
Thierry Redinge0d89722017-11-07 19:15:54 +01002141
Linus Walleijfdd61a02019-08-08 14:32:37 +02002142#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
2143 if (irq_domain_is_hierarchy(domain)) {
2144 struct irq_fwspec spec;
2145
2146 spec.fwnode = domain->fwnode;
2147 spec.param_count = 2;
2148 spec.param[0] = chip->irq.child_offset_to_irq(chip, offset);
2149 spec.param[1] = IRQ_TYPE_NONE;
2150
2151 return irq_create_fwspec_mapping(&spec);
2152 }
2153#endif
2154
2155 return irq_create_mapping(domain, offset);
Linus Walleij14250522014-03-25 10:40:18 +01002156}
2157
Hans Verkuil4e6b8232018-09-08 11:23:14 +02002158static int gpiochip_irq_reqres(struct irq_data *d)
2159{
2160 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
2161
2162 return gpiochip_reqres_irq(chip, d->hwirq);
2163}
2164
2165static void gpiochip_irq_relres(struct irq_data *d)
2166{
2167 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
2168
2169 gpiochip_relres_irq(chip, d->hwirq);
2170}
2171
Hans Verkuil461c1a72018-09-08 11:23:17 +02002172static void gpiochip_irq_enable(struct irq_data *d)
2173{
2174 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
2175
2176 gpiochip_enable_irq(chip, d->hwirq);
2177 if (chip->irq.irq_enable)
2178 chip->irq.irq_enable(d);
2179 else
2180 chip->irq.chip->irq_unmask(d);
2181}
2182
2183static void gpiochip_irq_disable(struct irq_data *d)
2184{
2185 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
2186
2187 if (chip->irq.irq_disable)
2188 chip->irq.irq_disable(d);
2189 else
2190 chip->irq.chip->irq_mask(d);
2191 gpiochip_disable_irq(chip, d->hwirq);
2192}
2193
Hans Verkuilca620f22018-09-08 11:23:15 +02002194static void gpiochip_set_irq_hooks(struct gpio_chip *gpiochip)
2195{
2196 struct irq_chip *irqchip = gpiochip->irq.chip;
2197
2198 if (!irqchip->irq_request_resources &&
2199 !irqchip->irq_release_resources) {
2200 irqchip->irq_request_resources = gpiochip_irq_reqres;
2201 irqchip->irq_release_resources = gpiochip_irq_relres;
2202 }
Hans Verkuil461c1a72018-09-08 11:23:17 +02002203 if (WARN_ON(gpiochip->irq.irq_enable))
2204 return;
Hans Verkuil171948e2018-09-14 10:36:39 +02002205 /* Check if the irqchip already has this hook... */
2206 if (irqchip->irq_enable == gpiochip_irq_enable) {
2207 /*
2208 * ...and if so, give a gentle warning that this is bad
2209 * practice.
2210 */
2211 chip_info(gpiochip,
2212 "detected irqchip that is shared with multiple gpiochips: please fix the driver.\n");
2213 return;
2214 }
Hans Verkuil461c1a72018-09-08 11:23:17 +02002215 gpiochip->irq.irq_enable = irqchip->irq_enable;
2216 gpiochip->irq.irq_disable = irqchip->irq_disable;
2217 irqchip->irq_enable = gpiochip_irq_enable;
2218 irqchip->irq_disable = gpiochip_irq_disable;
Hans Verkuilca620f22018-09-08 11:23:15 +02002219}
2220
Linus Walleij14250522014-03-25 10:40:18 +01002221/**
Thierry Redinge0d89722017-11-07 19:15:54 +01002222 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
2223 * @gpiochip: the GPIO chip to add the IRQ chip to
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002224 * @lock_key: lockdep class for IRQ lock
2225 * @request_key: lockdep class for IRQ request
Thierry Redinge0d89722017-11-07 19:15:54 +01002226 */
Thierry Reding959bc7b2017-11-07 19:15:59 +01002227static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002228 struct lock_class_key *lock_key,
2229 struct lock_class_key *request_key)
Thierry Redinge0d89722017-11-07 19:15:54 +01002230{
2231 struct irq_chip *irqchip = gpiochip->irq.chip;
Linus Walleijfdd61a02019-08-08 14:32:37 +02002232 const struct irq_domain_ops *ops = NULL;
Thierry Redinge0d89722017-11-07 19:15:54 +01002233 struct device_node *np;
2234 unsigned int type;
2235 unsigned int i;
2236
2237 if (!irqchip)
2238 return 0;
2239
2240 if (gpiochip->irq.parent_handler && gpiochip->can_sleep) {
Andy Shevchenkob1911712018-07-03 03:39:03 +03002241 chip_err(gpiochip, "you cannot have chained interrupts on a chip that may sleep\n");
Thierry Redinge0d89722017-11-07 19:15:54 +01002242 return -EINVAL;
2243 }
2244
2245 np = gpiochip->gpiodev->dev.of_node;
2246 type = gpiochip->irq.default_type;
2247
2248 /*
2249 * Specifying a default trigger is a terrible idea if DT or ACPI is
2250 * used to configure the interrupts, as you may end up with
2251 * conflicting triggers. Tell the user, and reset to NONE.
2252 */
2253 if (WARN(np && type != IRQ_TYPE_NONE,
2254 "%s: Ignoring %u default trigger\n", np->full_name, type))
2255 type = IRQ_TYPE_NONE;
2256
2257 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
2258 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
2259 "Ignoring %u default trigger\n", type);
2260 type = IRQ_TYPE_NONE;
2261 }
2262
2263 gpiochip->to_irq = gpiochip_to_irq;
2264 gpiochip->irq.default_type = type;
Thierry Reding959bc7b2017-11-07 19:15:59 +01002265 gpiochip->irq.lock_key = lock_key;
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002266 gpiochip->irq.request_key = request_key;
Thierry Redinge0d89722017-11-07 19:15:54 +01002267
Linus Walleijfdd61a02019-08-08 14:32:37 +02002268 /* If a parent irqdomain is provided, let's build a hierarchy */
2269 if (gpiochip_hierarchy_is_hierarchical(gpiochip)) {
2270 int ret = gpiochip_hierarchy_add_domain(gpiochip);
2271 if (ret)
2272 return ret;
2273 } else {
2274 /* Some drivers provide custom irqdomain ops */
2275 if (gpiochip->irq.domain_ops)
2276 ops = gpiochip->irq.domain_ops;
Thierry Redinge0d89722017-11-07 19:15:54 +01002277
Linus Walleijfdd61a02019-08-08 14:32:37 +02002278 if (!ops)
2279 ops = &gpiochip_domain_ops;
2280 gpiochip->irq.domain = irq_domain_add_simple(np,
2281 gpiochip->ngpio,
2282 gpiochip->irq.first,
2283 ops, gpiochip);
2284 if (!gpiochip->irq.domain)
2285 return -EINVAL;
2286 }
Thierry Redinge0d89722017-11-07 19:15:54 +01002287
Thierry Redinge0d89722017-11-07 19:15:54 +01002288 if (gpiochip->irq.parent_handler) {
2289 void *data = gpiochip->irq.parent_handler_data ?: gpiochip;
2290
2291 for (i = 0; i < gpiochip->irq.num_parents; i++) {
2292 /*
2293 * The parent IRQ chip is already using the chip_data
2294 * for this IRQ chip, so our callbacks simply use the
2295 * handler_data.
2296 */
2297 irq_set_chained_handler_and_data(gpiochip->irq.parents[i],
2298 gpiochip->irq.parent_handler,
2299 data);
2300 }
Thierry Redinge0d89722017-11-07 19:15:54 +01002301 }
2302
Hans Verkuilca620f22018-09-08 11:23:15 +02002303 gpiochip_set_irq_hooks(gpiochip);
2304
Thierry Redinge0d89722017-11-07 19:15:54 +01002305 acpi_gpiochip_request_interrupts(gpiochip);
2306
2307 return 0;
2308}
2309
2310/**
Linus Walleij14250522014-03-25 10:40:18 +01002311 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
2312 * @gpiochip: the gpiochip to remove the irqchip from
2313 *
2314 * This is called only from gpiochip_remove()
2315 */
2316static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
2317{
Hans Verkuilca620f22018-09-08 11:23:15 +02002318 struct irq_chip *irqchip = gpiochip->irq.chip;
Thierry Reding39e5f092017-11-07 19:15:50 +01002319 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +01002320
Mika Westerbergafa82fa2014-07-25 09:54:48 +03002321 acpi_gpiochip_free_interrupts(gpiochip);
2322
Hans Verkuilca620f22018-09-08 11:23:15 +02002323 if (irqchip && gpiochip->irq.parent_handler) {
Thierry Reding39e5f092017-11-07 19:15:50 +01002324 struct gpio_irq_chip *irq = &gpiochip->irq;
2325 unsigned int i;
2326
2327 for (i = 0; i < irq->num_parents; i++)
2328 irq_set_chained_handler_and_data(irq->parents[i],
2329 NULL, NULL);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03002330 }
2331
Linus Walleijc3626fd2014-03-28 20:42:01 +01002332 /* Remove all IRQ mappings and delete the domain */
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01002333 if (gpiochip->irq.domain) {
Thierry Reding39e5f092017-11-07 19:15:50 +01002334 unsigned int irq;
2335
Mika Westerberg79b804c2016-09-20 15:15:21 +03002336 for (offset = 0; offset < gpiochip->ngpio; offset++) {
2337 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
2338 continue;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01002339
2340 irq = irq_find_mapping(gpiochip->irq.domain, offset);
2341 irq_dispose_mapping(irq);
Mika Westerberg79b804c2016-09-20 15:15:21 +03002342 }
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01002343
2344 irq_domain_remove(gpiochip->irq.domain);
Linus Walleijc3626fd2014-03-28 20:42:01 +01002345 }
Linus Walleij14250522014-03-25 10:40:18 +01002346
Hans Verkuil461c1a72018-09-08 11:23:17 +02002347 if (irqchip) {
2348 if (irqchip->irq_request_resources == gpiochip_irq_reqres) {
2349 irqchip->irq_request_resources = NULL;
2350 irqchip->irq_release_resources = NULL;
2351 }
2352 if (irqchip->irq_enable == gpiochip_irq_enable) {
2353 irqchip->irq_enable = gpiochip->irq.irq_enable;
2354 irqchip->irq_disable = gpiochip->irq.irq_disable;
2355 }
Linus Walleij14250522014-03-25 10:40:18 +01002356 }
Hans Verkuil461c1a72018-09-08 11:23:17 +02002357 gpiochip->irq.irq_enable = NULL;
2358 gpiochip->irq.irq_disable = NULL;
Hans Verkuilca620f22018-09-08 11:23:15 +02002359 gpiochip->irq.chip = NULL;
Mika Westerberg79b804c2016-09-20 15:15:21 +03002360
2361 gpiochip_irqchip_free_valid_mask(gpiochip);
Linus Walleij14250522014-03-25 10:40:18 +01002362}
2363
2364/**
Linus Walleij739e6f52017-01-11 13:37:07 +01002365 * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01002366 * @gpiochip: the gpiochip to add the irqchip to
2367 * @irqchip: the irqchip to add to the gpiochip
2368 * @first_irq: if not dynamically assigned, the base (first) IRQ to
2369 * allocate gpiochip irqs from
2370 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +02002371 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
2372 * to have the core avoid setting up any default type in the hardware.
Thierry Reding60ed54c2017-11-07 19:15:57 +01002373 * @threaded: whether this irqchip uses a nested thread handler
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002374 * @lock_key: lockdep class for IRQ lock
2375 * @request_key: lockdep class for IRQ request
Linus Walleij14250522014-03-25 10:40:18 +01002376 *
2377 * This function closely associates a certain irqchip with a certain
2378 * gpiochip, providing an irq domain to translate the local IRQs to
2379 * global irqs in the gpiolib core, and making sure that the gpiochip
2380 * is passed as chip data to all related functions. Driver callbacks
Linus Walleij09dd5f92015-12-07 15:31:58 +01002381 * need to use gpiochip_get_data() to get their local state containers back
Linus Walleij14250522014-03-25 10:40:18 +01002382 * from the gpiochip passed as chip data. An irqdomain will be stored
2383 * in the gpiochip that shall be used by the driver to handle IRQ number
2384 * translation. The gpiochip will need to be initialized and registered
2385 * before calling this function.
2386 *
Linus Walleijc3626fd2014-03-28 20:42:01 +01002387 * This function will handle two cell:ed simple IRQs and assumes all
2388 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +01002389 * need to be open coded.
2390 */
Linus Walleij739e6f52017-01-11 13:37:07 +01002391int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
2392 struct irq_chip *irqchip,
2393 unsigned int first_irq,
2394 irq_flow_handler_t handler,
2395 unsigned int type,
Thierry Reding60ed54c2017-11-07 19:15:57 +01002396 bool threaded,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002397 struct lock_class_key *lock_key,
2398 struct lock_class_key *request_key)
Linus Walleij14250522014-03-25 10:40:18 +01002399{
2400 struct device_node *of_node;
Linus Walleij14250522014-03-25 10:40:18 +01002401
2402 if (!gpiochip || !irqchip)
2403 return -EINVAL;
2404
Linus Walleij58383c782015-11-04 09:56:26 +01002405 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +01002406 pr_err("missing gpiochip .dev parent pointer\n");
2407 return -EINVAL;
2408 }
Thierry Reding60ed54c2017-11-07 19:15:57 +01002409 gpiochip->irq.threaded = threaded;
Linus Walleij58383c782015-11-04 09:56:26 +01002410 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +01002411#ifdef CONFIG_OF_GPIO
2412 /*
Colin Cronin20a8a962015-05-18 11:41:43 -07002413 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08002414 * FIXME: get rid of this and use gpiochip->parent->of_node
2415 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +01002416 */
2417 if (gpiochip->of_node)
2418 of_node = gpiochip->of_node;
2419#endif
Marc Zyngier332e99d2016-09-07 09:12:11 +01002420 /*
Mika Westerberg0a1e0052016-09-12 14:29:51 +03002421 * Specifying a default trigger is a terrible idea if DT or ACPI is
Marc Zyngier332e99d2016-09-07 09:12:11 +01002422 * used to configure the interrupts, as you may end-up with
2423 * conflicting triggers. Tell the user, and reset to NONE.
2424 */
2425 if (WARN(of_node && type != IRQ_TYPE_NONE,
Rob Herring7eb6ce22017-07-18 16:43:03 -05002426 "%pOF: Ignoring %d default trigger\n", of_node, type))
Marc Zyngier332e99d2016-09-07 09:12:11 +01002427 type = IRQ_TYPE_NONE;
Mika Westerberg0a1e0052016-09-12 14:29:51 +03002428 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
2429 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
2430 "Ignoring %d default trigger\n", type);
2431 type = IRQ_TYPE_NONE;
2432 }
Marc Zyngier332e99d2016-09-07 09:12:11 +01002433
Thierry Redingda80ff82017-11-07 19:15:46 +01002434 gpiochip->irq.chip = irqchip;
Thierry Redingc7a0aa52017-11-07 19:15:48 +01002435 gpiochip->irq.handler = handler;
Thierry Reding3634eeb2017-11-07 19:15:49 +01002436 gpiochip->irq.default_type = type;
Linus Walleij14250522014-03-25 10:40:18 +01002437 gpiochip->to_irq = gpiochip_to_irq;
Thierry Redingca9df052017-11-07 19:15:53 +01002438 gpiochip->irq.lock_key = lock_key;
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002439 gpiochip->irq.request_key = request_key;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01002440 gpiochip->irq.domain = irq_domain_add_simple(of_node,
Linus Walleij14250522014-03-25 10:40:18 +01002441 gpiochip->ngpio, first_irq,
2442 &gpiochip_domain_ops, gpiochip);
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01002443 if (!gpiochip->irq.domain) {
Thierry Redingda80ff82017-11-07 19:15:46 +01002444 gpiochip->irq.chip = NULL;
Linus Walleij14250522014-03-25 10:40:18 +01002445 return -EINVAL;
2446 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +02002447
Hans Verkuilca620f22018-09-08 11:23:15 +02002448 gpiochip_set_irq_hooks(gpiochip);
Linus Walleij14250522014-03-25 10:40:18 +01002449
Mika Westerbergafa82fa2014-07-25 09:54:48 +03002450 acpi_gpiochip_request_interrupts(gpiochip);
2451
Linus Walleij14250522014-03-25 10:40:18 +01002452 return 0;
2453}
Linus Walleij739e6f52017-01-11 13:37:07 +01002454EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key);
Linus Walleij14250522014-03-25 10:40:18 +01002455
2456#else /* CONFIG_GPIOLIB_IRQCHIP */
2457
Thierry Reding959bc7b2017-11-07 19:15:59 +01002458static inline int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002459 struct lock_class_key *lock_key,
2460 struct lock_class_key *request_key)
Thierry Redinge0d89722017-11-07 19:15:54 +01002461{
2462 return 0;
2463}
Linus Walleij14250522014-03-25 10:40:18 +01002464static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
Andy Shevchenko9411e3a2019-10-09 17:34:44 +03002465
2466static inline int gpiochip_irqchip_init_hw(struct gpio_chip *gpiochip)
2467{
2468 return 0;
2469}
2470
Mika Westerberg79b804c2016-09-20 15:15:21 +03002471static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
2472{
2473 return 0;
2474}
2475static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
2476{ }
Linus Walleij14250522014-03-25 10:40:18 +01002477
2478#endif /* CONFIG_GPIOLIB_IRQCHIP */
2479
Jonas Gorskic771c2f2015-10-11 17:34:15 +02002480/**
2481 * gpiochip_generic_request() - request the gpio function for a pin
2482 * @chip: the gpiochip owning the GPIO
2483 * @offset: the offset of the GPIO to request for GPIO function
2484 */
2485int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
2486{
Linus Walleija9a1d2a2017-09-22 11:02:10 +02002487 return pinctrl_gpio_request(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02002488}
2489EXPORT_SYMBOL_GPL(gpiochip_generic_request);
2490
2491/**
2492 * gpiochip_generic_free() - free the gpio function from a pin
2493 * @chip: the gpiochip to request the gpio function for
2494 * @offset: the offset of the GPIO to free from GPIO function
2495 */
2496void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
2497{
Linus Walleija9a1d2a2017-09-22 11:02:10 +02002498 pinctrl_gpio_free(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02002499}
2500EXPORT_SYMBOL_GPL(gpiochip_generic_free);
2501
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002502/**
2503 * gpiochip_generic_config() - apply configuration for a pin
2504 * @chip: the gpiochip owning the GPIO
2505 * @offset: the offset of the GPIO to apply the configuration
2506 * @config: the configuration to be applied
2507 */
2508int gpiochip_generic_config(struct gpio_chip *chip, unsigned offset,
2509 unsigned long config)
2510{
2511 return pinctrl_gpio_set_config(chip->gpiodev->base + offset, config);
2512}
2513EXPORT_SYMBOL_GPL(gpiochip_generic_config);
2514
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302515#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01002516
Linus Walleij3f0f8672012-11-20 12:40:15 +01002517/**
Christian Ruppert586a87e2013-10-15 15:37:54 +02002518 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
2519 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +02002520 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +02002521 * @gpio_offset: the start offset in the current gpio_chip number space
2522 * @pin_group: name of the pin group inside the pin controller
Christian Lamparter973c1712018-05-21 22:57:39 +02002523 *
2524 * Calling this function directly from a DeviceTree-supported
2525 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
2526 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
2527 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
Christian Ruppert586a87e2013-10-15 15:37:54 +02002528 */
2529int gpiochip_add_pingroup_range(struct gpio_chip *chip,
2530 struct pinctrl_dev *pctldev,
2531 unsigned int gpio_offset, const char *pin_group)
2532{
2533 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002534 struct gpio_device *gdev = chip->gpiodev;
Christian Ruppert586a87e2013-10-15 15:37:54 +02002535 int ret;
2536
2537 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
2538 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002539 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +02002540 return -ENOMEM;
2541 }
2542
2543 /* Use local offset as range ID */
2544 pin_range->range.id = gpio_offset;
2545 pin_range->range.gc = chip;
2546 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002547 pin_range->range.base = gdev->base + gpio_offset;
Christian Ruppert586a87e2013-10-15 15:37:54 +02002548 pin_range->pctldev = pctldev;
2549
2550 ret = pinctrl_get_group_pins(pctldev, pin_group,
2551 &pin_range->range.pins,
2552 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +01002553 if (ret < 0) {
2554 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +02002555 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +01002556 }
Christian Ruppert586a87e2013-10-15 15:37:54 +02002557
2558 pinctrl_add_gpio_range(pctldev, &pin_range->range);
2559
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002560 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
2561 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +02002562 pinctrl_dev_get_devname(pctldev), pin_group);
2563
Linus Walleij20ec3e32016-02-11 11:03:06 +01002564 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Christian Ruppert586a87e2013-10-15 15:37:54 +02002565
2566 return 0;
2567}
2568EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
2569
2570/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01002571 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
2572 * @chip: the gpiochip to add the range for
Thierry Reding950d55f52017-07-24 16:57:22 +02002573 * @pinctl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01002574 * @gpio_offset: the start offset in the current gpio_chip number space
2575 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01002576 * @npins: the number of pins from the offset of each pin space (GPIO and
2577 * pin controller) to accumulate in this range
Thierry Reding950d55f52017-07-24 16:57:22 +02002578 *
2579 * Returns:
2580 * 0 on success, or a negative error-code on failure.
Christian Lamparter973c1712018-05-21 22:57:39 +02002581 *
2582 * Calling this function directly from a DeviceTree-supported
2583 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
2584 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
2585 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
Linus Walleij3f0f8672012-11-20 12:40:15 +01002586 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002587int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01002588 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01002589 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302590{
2591 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002592 struct gpio_device *gdev = chip->gpiodev;
Axel Linb4d4b1f2012-11-21 14:33:56 +08002593 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302594
Linus Walleij3f0f8672012-11-20 12:40:15 +01002595 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302596 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002597 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002598 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302599 }
2600
Linus Walleij3f0f8672012-11-20 12:40:15 +01002601 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01002602 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01002603 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302604 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002605 pin_range->range.base = gdev->base + gpio_offset;
Linus Walleij316511c2012-11-21 08:48:09 +01002606 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302607 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01002608 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302609 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01002610 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08002611 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002612 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +01002613 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08002614 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01002615 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002616 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
2617 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +01002618 pinctl_name,
2619 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302620
Linus Walleij20ec3e32016-02-11 11:03:06 +01002621 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002622
2623 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302624}
Linus Walleij165adc92012-11-06 14:49:39 +01002625EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302626
Linus Walleij3f0f8672012-11-20 12:40:15 +01002627/**
2628 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
2629 * @chip: the chip to remove all the mappings for
2630 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302631void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
2632{
2633 struct gpio_pin_range *pin_range, *tmp;
Linus Walleij20ec3e32016-02-11 11:03:06 +01002634 struct gpio_device *gdev = chip->gpiodev;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302635
Linus Walleij20ec3e32016-02-11 11:03:06 +01002636 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302637 list_del(&pin_range->node);
2638 pinctrl_remove_gpio_range(pin_range->pctldev,
2639 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01002640 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302641 }
2642}
Linus Walleij165adc92012-11-06 14:49:39 +01002643EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
2644
2645#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302646
David Brownelld2876d02008-02-04 22:28:20 -08002647/* These "optional" allocation calls help prevent drivers from stomping
2648 * on each other, and help provide better diagnostics in debugfs.
2649 * They're called even less than the "set direction" calls.
2650 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002651static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08002652{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002653 struct gpio_chip *chip = desc->gdev->chip;
Linus Walleijd377f562019-07-16 11:11:45 +02002654 int ret;
David Brownelld2876d02008-02-04 22:28:20 -08002655 unsigned long flags;
Biju Das3789f5a2018-08-07 08:15:18 +01002656 unsigned offset;
David Brownelld2876d02008-02-04 22:28:20 -08002657
Muchun Song18534df2018-11-01 21:12:50 +08002658 if (label) {
2659 label = kstrdup_const(label, GFP_KERNEL);
2660 if (!label)
2661 return -ENOMEM;
2662 }
2663
David Brownelld2876d02008-02-04 22:28:20 -08002664 spin_lock_irqsave(&gpio_lock, flags);
2665
David Brownelld2876d02008-02-04 22:28:20 -08002666 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07002667 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08002668 */
2669
2670 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
2671 desc_set_label(desc, label ? : "?");
Linus Walleijd377f562019-07-16 11:11:45 +02002672 ret = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07002673 } else {
Muchun Song18534df2018-11-01 21:12:50 +08002674 kfree_const(label);
Linus Walleijd377f562019-07-16 11:11:45 +02002675 ret = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -08002676 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07002677 }
2678
2679 if (chip->request) {
2680 /* chip->request may sleep */
2681 spin_unlock_irqrestore(&gpio_lock, flags);
Biju Das3789f5a2018-08-07 08:15:18 +01002682 offset = gpio_chip_hwgpio(desc);
2683 if (gpiochip_line_is_valid(chip, offset))
Linus Walleijd377f562019-07-16 11:11:45 +02002684 ret = chip->request(chip, offset);
Biju Das3789f5a2018-08-07 08:15:18 +01002685 else
Linus Walleijd377f562019-07-16 11:11:45 +02002686 ret = -EINVAL;
David Brownell35e8bb52008-10-15 22:03:16 -07002687 spin_lock_irqsave(&gpio_lock, flags);
2688
Linus Walleijd377f562019-07-16 11:11:45 +02002689 if (ret < 0) {
David Brownell35e8bb52008-10-15 22:03:16 -07002690 desc_set_label(desc, NULL);
Muchun Song18534df2018-11-01 21:12:50 +08002691 kfree_const(label);
David Brownell35e8bb52008-10-15 22:03:16 -07002692 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03002693 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07002694 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07002695 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03002696 if (chip->get_direction) {
2697 /* chip->get_direction may sleep */
2698 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002699 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03002700 spin_lock_irqsave(&gpio_lock, flags);
2701 }
David Brownelld2876d02008-02-04 22:28:20 -08002702done:
Mika Westerberg77c2d792014-03-10 14:54:50 +02002703 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijd377f562019-07-16 11:11:45 +02002704 return ret;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002705}
2706
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002707/*
2708 * This descriptor validation needs to be inserted verbatim into each
2709 * function taking a descriptor, so we need to use a preprocessor
Linus Walleij54d77192016-05-30 16:48:39 +02002710 * macro to avoid endless duplication. If the desc is NULL it is an
2711 * optional GPIO and calls should just bail out.
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002712 */
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002713static int validate_desc(const struct gpio_desc *desc, const char *func)
2714{
2715 if (!desc)
2716 return 0;
2717 if (IS_ERR(desc)) {
2718 pr_warn("%s: invalid GPIO (errorpointer)\n", func);
2719 return PTR_ERR(desc);
2720 }
2721 if (!desc->gdev) {
2722 pr_warn("%s: invalid GPIO (no device)\n", func);
2723 return -EINVAL;
2724 }
2725 if (!desc->gdev->chip) {
2726 dev_warn(&desc->gdev->dev,
2727 "%s: backing chip is gone\n", func);
2728 return 0;
2729 }
2730 return 1;
2731}
2732
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002733#define VALIDATE_DESC(desc) do { \
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002734 int __valid = validate_desc(desc, __func__); \
2735 if (__valid <= 0) \
2736 return __valid; \
2737 } while (0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002738
2739#define VALIDATE_DESC_VOID(desc) do { \
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002740 int __valid = validate_desc(desc, __func__); \
2741 if (__valid <= 0) \
Linus Walleij54d77192016-05-30 16:48:39 +02002742 return; \
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002743 } while (0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002744
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002745int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002746{
Linus Walleijd377f562019-07-16 11:11:45 +02002747 int ret = -EPROBE_DEFER;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002748 struct gpio_device *gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002749
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002750 VALIDATE_DESC(desc);
2751 gdev = desc->gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002752
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002753 if (try_module_get(gdev->owner)) {
Linus Walleijd377f562019-07-16 11:11:45 +02002754 ret = gpiod_request_commit(desc, label);
2755 if (ret < 0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002756 module_put(gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002757 else
2758 get_device(&gdev->dev);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002759 }
2760
Linus Walleijd377f562019-07-16 11:11:45 +02002761 if (ret)
2762 gpiod_dbg(desc, "%s: status %d\n", __func__, ret);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002763
Linus Walleijd377f562019-07-16 11:11:45 +02002764 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08002765}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002766
Linus Walleijfac9d882017-09-26 20:58:28 +02002767static bool gpiod_free_commit(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002768{
Mika Westerberg77c2d792014-03-10 14:54:50 +02002769 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08002770 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07002771 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002772
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07002773 might_sleep();
2774
Alexandre Courbot372e7222013-02-03 01:29:29 +09002775 gpiod_unexport(desc);
David Brownelld8f388d82008-07-25 01:46:07 -07002776
David Brownelld2876d02008-02-04 22:28:20 -08002777 spin_lock_irqsave(&gpio_lock, flags);
2778
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002779 chip = desc->gdev->chip;
David Brownell35e8bb52008-10-15 22:03:16 -07002780 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
2781 if (chip->free) {
2782 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07002783 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002784 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07002785 spin_lock_irqsave(&gpio_lock, flags);
2786 }
Muchun Song18534df2018-11-01 21:12:50 +08002787 kfree_const(desc->label);
David Brownelld2876d02008-02-04 22:28:20 -08002788 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08002789 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07002790 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302791 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302792 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06002793 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002794 ret = true;
2795 }
David Brownelld2876d02008-02-04 22:28:20 -08002796
2797 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002798 return ret;
2799}
2800
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002801void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002802{
Linus Walleijfac9d882017-09-26 20:58:28 +02002803 if (desc && desc->gdev && gpiod_free_commit(desc)) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002804 module_put(desc->gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002805 put_device(&desc->gdev->dev);
2806 } else {
Mika Westerberg77c2d792014-03-10 14:54:50 +02002807 WARN_ON(extra_checks);
Linus Walleij33a68e82016-02-11 10:28:44 +01002808 }
David Brownelld2876d02008-02-04 22:28:20 -08002809}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002810
David Brownelld2876d02008-02-04 22:28:20 -08002811/**
2812 * gpiochip_is_requested - return string iff signal was requested
2813 * @chip: controller managing the signal
2814 * @offset: of signal within controller's 0..(ngpio - 1) range
2815 *
2816 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09002817 * The string returned is the label passed to gpio_request(); if none has been
2818 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08002819 *
2820 * This function is for use by GPIO controller drivers. The label can
2821 * help with diagnostics, and knowing that the signal is used as a GPIO
2822 * can help avoid accidentally multiplexing it to another controller.
2823 */
2824const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
2825{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002826 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08002827
Dirk Behme48b59532015-08-18 18:02:32 +02002828 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08002829 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002830
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002831 desc = &chip->gpiodev->descs[offset];
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002832
Alexandre Courbot372e7222013-02-03 01:29:29 +09002833 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08002834 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002835 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08002836}
2837EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2838
Mika Westerberg77c2d792014-03-10 14:54:50 +02002839/**
2840 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
Thierry Reding950d55f52017-07-24 16:57:22 +02002841 * @chip: GPIO chip
2842 * @hwnum: hardware number of the GPIO for which to request the descriptor
Mika Westerberg77c2d792014-03-10 14:54:50 +02002843 * @label: label for the GPIO
Linus Walleij5923ea62019-04-26 14:40:18 +02002844 * @lflags: lookup flags for this GPIO or 0 if default, this can be used to
2845 * specify things like line inversion semantics with the machine flags
2846 * such as GPIO_OUT_LOW
2847 * @dflags: descriptor request flags for this GPIO or 0 if default, this
2848 * can be used to specify consumer semantics such as open drain
Mika Westerberg77c2d792014-03-10 14:54:50 +02002849 *
2850 * Function allows GPIO chip drivers to request and use their own GPIO
2851 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2852 * function will not increase reference count of the GPIO chip module. This
2853 * allows the GPIO chip module to be unloaded as needed (we assume that the
2854 * GPIO chip driver handles freeing the GPIOs it has requested).
Thierry Reding950d55f52017-07-24 16:57:22 +02002855 *
2856 * Returns:
2857 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2858 * code on failure.
Mika Westerberg77c2d792014-03-10 14:54:50 +02002859 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002860struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
Linus Walleij21abf102018-09-04 13:31:45 +02002861 const char *label,
Linus Walleij5923ea62019-04-26 14:40:18 +02002862 enum gpio_lookup_flags lflags,
2863 enum gpiod_flags dflags)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002864{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002865 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
Linus Walleijd377f562019-07-16 11:11:45 +02002866 int ret;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002867
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002868 if (IS_ERR(desc)) {
2869 chip_err(chip, "failed to get GPIO descriptor\n");
2870 return desc;
2871 }
2872
Linus Walleijd377f562019-07-16 11:11:45 +02002873 ret = gpiod_request_commit(desc, label);
2874 if (ret < 0)
2875 return ERR_PTR(ret);
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002876
Linus Walleijd377f562019-07-16 11:11:45 +02002877 ret = gpiod_configure_flags(desc, label, lflags, dflags);
2878 if (ret) {
Linus Walleij21abf102018-09-04 13:31:45 +02002879 chip_err(chip, "setup of own GPIO %s failed\n", label);
2880 gpiod_free_commit(desc);
Linus Walleijd377f562019-07-16 11:11:45 +02002881 return ERR_PTR(ret);
Linus Walleij21abf102018-09-04 13:31:45 +02002882 }
2883
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002884 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002885}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002886EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002887
2888/**
2889 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2890 * @desc: GPIO descriptor to free
2891 *
2892 * Function frees the given GPIO requested previously with
2893 * gpiochip_request_own_desc().
2894 */
2895void gpiochip_free_own_desc(struct gpio_desc *desc)
2896{
2897 if (desc)
Linus Walleijfac9d882017-09-26 20:58:28 +02002898 gpiod_free_commit(desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002899}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002900EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08002901
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002902/*
2903 * Drivers MUST set GPIO direction before making get/set calls. In
David Brownelld2876d02008-02-04 22:28:20 -08002904 * some cases this is done in early boot, before IRQs are enabled.
2905 *
2906 * As a rule these aren't called more than once (except for drivers
2907 * using the open-drain emulation idiom) so these are natural places
2908 * to accumulate extra debugging checks. Note that we can't (yet)
2909 * rely on gpio_request() having been called beforehand.
2910 */
2911
Thomas Petazzoni71479782019-02-07 17:28:56 +01002912static int gpio_set_config(struct gpio_chip *gc, unsigned offset,
2913 enum pin_config_param mode)
2914{
Maxime Ripard542f3612019-03-14 20:32:50 +01002915 unsigned long config;
2916 unsigned arg;
Thomas Petazzoni71479782019-02-07 17:28:56 +01002917
Maxime Ripard542f3612019-03-14 20:32:50 +01002918 switch (mode) {
2919 case PIN_CONFIG_BIAS_PULL_DOWN:
2920 case PIN_CONFIG_BIAS_PULL_UP:
2921 arg = 1;
2922 break;
2923
2924 default:
2925 arg = 0;
2926 }
2927
2928 config = PIN_CONF_PACKED(mode, arg);
Thomas Petazzoni71479782019-02-07 17:28:56 +01002929 return gc->set_config ? gc->set_config(gc, offset, config) : -ENOTSUPP;
2930}
2931
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002932/**
2933 * gpiod_direction_input - set the GPIO direction to input
2934 * @desc: GPIO to set to input
2935 *
2936 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2937 * be called safely on it.
2938 *
2939 * Return 0 in case of success, else an error code.
2940 */
2941int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002942{
David Brownelld2876d02008-02-04 22:28:20 -08002943 struct gpio_chip *chip;
Linus Walleijd377f562019-07-16 11:11:45 +02002944 int ret = 0;
David Brownelld2876d02008-02-04 22:28:20 -08002945
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002946 VALIDATE_DESC(desc);
2947 chip = desc->gdev->chip;
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002948
Linus Walleije48d1942018-09-25 09:54:14 +02002949 /*
2950 * It is legal to have no .get() and .direction_input() specified if
2951 * the chip is output-only, but you can't specify .direction_input()
2952 * and not support the .get() operation, that doesn't make sense.
2953 */
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02002954 if (!chip->get && chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01002955 gpiod_warn(desc,
Linus Walleije48d1942018-09-25 09:54:14 +02002956 "%s: missing get() but have direction_input()\n",
2957 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002958 return -EIO;
2959 }
2960
Linus Walleije48d1942018-09-25 09:54:14 +02002961 /*
2962 * If we have a .direction_input() callback, things are simple,
2963 * just call it. Else we are some input-only chip so try to check the
2964 * direction (if .get_direction() is supported) else we silently
2965 * assume we are in input mode after this.
2966 */
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02002967 if (chip->direction_input) {
Linus Walleijd377f562019-07-16 11:11:45 +02002968 ret = chip->direction_input(chip, gpio_chip_hwgpio(desc));
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02002969 } else if (chip->get_direction &&
2970 (chip->get_direction(chip, gpio_chip_hwgpio(desc)) != 1)) {
2971 gpiod_warn(desc,
Linus Walleije48d1942018-09-25 09:54:14 +02002972 "%s: missing direction_input() operation and line is output\n",
2973 __func__);
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02002974 return -EIO;
2975 }
Linus Walleijd377f562019-07-16 11:11:45 +02002976 if (ret == 0)
David Brownelld2876d02008-02-04 22:28:20 -08002977 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002978
Thomas Petazzonid4499912019-02-07 17:28:58 +01002979 if (test_bit(FLAG_PULL_UP, &desc->flags))
2980 gpio_set_config(chip, gpio_chip_hwgpio(desc),
2981 PIN_CONFIG_BIAS_PULL_UP);
2982 else if (test_bit(FLAG_PULL_DOWN, &desc->flags))
2983 gpio_set_config(chip, gpio_chip_hwgpio(desc),
2984 PIN_CONFIG_BIAS_PULL_DOWN);
2985
Linus Walleijd377f562019-07-16 11:11:45 +02002986 trace_gpio_direction(desc_to_gpio(desc), 1, ret);
Alexandre Courbotd82da792014-07-22 16:17:43 +09002987
Linus Walleijd377f562019-07-16 11:11:45 +02002988 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08002989}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002990EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002991
Linus Walleijfac9d882017-09-26 20:58:28 +02002992static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08002993{
Linus Walleijc663e5f2016-03-22 10:51:16 +01002994 struct gpio_chip *gc = desc->gdev->chip;
Linus Walleijad177312016-11-13 23:02:44 +01002995 int val = !!value;
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02002996 int ret = 0;
David Brownelld2876d02008-02-04 22:28:20 -08002997
Linus Walleije48d1942018-09-25 09:54:14 +02002998 /*
2999 * It's OK not to specify .direction_output() if the gpiochip is
3000 * output-only, but if there is then not even a .set() operation it
3001 * is pretty tricky to drive the output line.
3002 */
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003003 if (!gc->set && !gc->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01003004 gpiod_warn(desc,
Linus Walleije48d1942018-09-25 09:54:14 +02003005 "%s: missing set() and direction_output() operations\n",
3006 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02003007 return -EIO;
3008 }
3009
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003010 if (gc->direction_output) {
3011 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
3012 } else {
Linus Walleije48d1942018-09-25 09:54:14 +02003013 /* Check that we are in output mode if we can */
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003014 if (gc->get_direction &&
3015 gc->get_direction(gc, gpio_chip_hwgpio(desc))) {
3016 gpiod_warn(desc,
3017 "%s: missing direction_output() operation\n",
3018 __func__);
3019 return -EIO;
3020 }
Linus Walleije48d1942018-09-25 09:54:14 +02003021 /*
3022 * If we can't actively set the direction, we are some
3023 * output-only chip, so just drive the output as desired.
3024 */
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003025 gc->set(gc, gpio_chip_hwgpio(desc), val);
3026 }
3027
Linus Walleijc663e5f2016-03-22 10:51:16 +01003028 if (!ret)
David Brownelld2876d02008-02-04 22:28:20 -08003029 set_bit(FLAG_IS_OUT, &desc->flags);
Linus Walleijad177312016-11-13 23:02:44 +01003030 trace_gpio_value(desc_to_gpio(desc), 0, val);
Linus Walleijc663e5f2016-03-22 10:51:16 +01003031 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
3032 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08003033}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01003034
3035/**
3036 * gpiod_direction_output_raw - set the GPIO direction to output
3037 * @desc: GPIO to set to output
3038 * @value: initial output value of the GPIO
3039 *
3040 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
3041 * be called safely on it. The initial value of the output must be specified
3042 * as raw value on the physical line without regard for the ACTIVE_LOW status.
3043 *
3044 * Return 0 in case of success, else an error code.
3045 */
3046int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
3047{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003048 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003049 return gpiod_direction_output_raw_commit(desc, value);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01003050}
3051EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
3052
3053/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05303054 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01003055 * @desc: GPIO to set to output
3056 * @value: initial output value of the GPIO
3057 *
3058 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
3059 * be called safely on it. The initial value of the output must be specified
3060 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3061 * account.
3062 *
3063 * Return 0 in case of success, else an error code.
3064 */
3065int gpiod_direction_output(struct gpio_desc *desc, int value)
3066{
Vladimir Zapolskiy30322bc2017-12-21 18:37:24 +02003067 struct gpio_chip *gc;
Linus Walleij02e47982017-09-26 21:20:23 +02003068 int ret;
3069
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003070 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01003071 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3072 value = !value;
Linus Walleijad177312016-11-13 23:02:44 +01003073 else
3074 value = !!value;
Linus Walleij02e47982017-09-26 21:20:23 +02003075
Hans Verkuil4e9439d2018-09-08 11:23:16 +02003076 /* GPIOs used for enabled IRQs shall not be set as output */
3077 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags) &&
3078 test_bit(FLAG_IRQ_IS_ENABLED, &desc->flags)) {
Linus Walleij02e47982017-09-26 21:20:23 +02003079 gpiod_err(desc,
3080 "%s: tried to set a GPIO tied to an IRQ as output\n",
3081 __func__);
3082 return -EIO;
3083 }
3084
Vladimir Zapolskiy30322bc2017-12-21 18:37:24 +02003085 gc = desc->gdev->chip;
Linus Walleij02e47982017-09-26 21:20:23 +02003086 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
3087 /* First see if we can enable open drain in hardware */
Thomas Petazzoni71479782019-02-07 17:28:56 +01003088 ret = gpio_set_config(gc, gpio_chip_hwgpio(desc),
3089 PIN_CONFIG_DRIVE_OPEN_DRAIN);
Linus Walleij02e47982017-09-26 21:20:23 +02003090 if (!ret)
3091 goto set_output_value;
3092 /* Emulate open drain by not actively driving the line high */
Bartosz Golaszewskie7352442019-10-01 11:44:53 +02003093 if (value) {
3094 ret = gpiod_direction_input(desc);
3095 goto set_output_flag;
3096 }
Linus Walleij02e47982017-09-26 21:20:23 +02003097 }
3098 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
Thomas Petazzoni71479782019-02-07 17:28:56 +01003099 ret = gpio_set_config(gc, gpio_chip_hwgpio(desc),
3100 PIN_CONFIG_DRIVE_OPEN_SOURCE);
Linus Walleij02e47982017-09-26 21:20:23 +02003101 if (!ret)
3102 goto set_output_value;
3103 /* Emulate open source by not actively driving the line low */
Bartosz Golaszewskie7352442019-10-01 11:44:53 +02003104 if (!value) {
3105 ret = gpiod_direction_input(desc);
3106 goto set_output_flag;
3107 }
Linus Walleij02e47982017-09-26 21:20:23 +02003108 } else {
Thomas Petazzoni71479782019-02-07 17:28:56 +01003109 gpio_set_config(gc, gpio_chip_hwgpio(desc),
3110 PIN_CONFIG_DRIVE_PUSH_PULL);
Linus Walleij02e47982017-09-26 21:20:23 +02003111 }
3112
3113set_output_value:
Linus Walleijfac9d882017-09-26 20:58:28 +02003114 return gpiod_direction_output_raw_commit(desc, value);
Bartosz Golaszewskie7352442019-10-01 11:44:53 +02003115
3116set_output_flag:
3117 /*
3118 * When emulating open-source or open-drain functionalities by not
3119 * actively driving the line (setting mode to input) we still need to
3120 * set the IS_OUT flag or otherwise we won't be able to set the line
3121 * value anymore.
3122 */
3123 if (ret == 0)
3124 set_bit(FLAG_IS_OUT, &desc->flags);
3125 return ret;
Philipp Zabelef70bbe2014-01-07 12:34:11 +01003126}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003127EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08003128
Felipe Balbic4b5be92010-05-26 14:42:23 -07003129/**
Thierry Reding950d55f52017-07-24 16:57:22 +02003130 * gpiod_set_debounce - sets @debounce time for a GPIO
3131 * @desc: descriptor of the GPIO for which to set debounce time
3132 * @debounce: debounce time in microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02003133 *
Thierry Reding950d55f52017-07-24 16:57:22 +02003134 * Returns:
3135 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
3136 * debounce time.
Felipe Balbic4b5be92010-05-26 14:42:23 -07003137 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003138int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07003139{
Felipe Balbic4b5be92010-05-26 14:42:23 -07003140 struct gpio_chip *chip;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03003141 unsigned long config;
Felipe Balbic4b5be92010-05-26 14:42:23 -07003142
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003143 VALIDATE_DESC(desc);
3144 chip = desc->gdev->chip;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03003145 if (!chip->set || !chip->set_config) {
Mark Brown6424de52013-09-09 10:33:49 +01003146 gpiod_dbg(desc,
Mika Westerberg2956b5d2017-01-23 15:34:34 +03003147 "%s: missing set() or set_config() operations\n",
Mark Brown6424de52013-09-09 10:33:49 +01003148 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02003149 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02003150 }
3151
Mika Westerberg2956b5d2017-01-23 15:34:34 +03003152 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
Andrew Jefferyfa59dd232019-03-26 15:19:54 +10303153 return chip->set_config(chip, gpio_chip_hwgpio(desc), config);
Felipe Balbic4b5be92010-05-26 14:42:23 -07003154}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003155EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003156
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003157/**
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303158 * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
3159 * @desc: descriptor of the GPIO for which to configure persistence
3160 * @transitory: True to lose state on suspend or reset, false for persistence
3161 *
3162 * Returns:
3163 * 0 on success, otherwise a negative error code.
3164 */
3165int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
3166{
3167 struct gpio_chip *chip;
3168 unsigned long packed;
3169 int gpio;
3170 int rc;
3171
Vladimir Zapolskiy156dd392017-12-21 18:37:35 +02003172 VALIDATE_DESC(desc);
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303173 /*
3174 * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
3175 * persistence state.
3176 */
3177 if (transitory)
3178 set_bit(FLAG_TRANSITORY, &desc->flags);
3179 else
3180 clear_bit(FLAG_TRANSITORY, &desc->flags);
3181
3182 /* If the driver supports it, set the persistence state now */
3183 chip = desc->gdev->chip;
3184 if (!chip->set_config)
3185 return 0;
3186
3187 packed = pinconf_to_config_packed(PIN_CONFIG_PERSIST_STATE,
3188 !transitory);
3189 gpio = gpio_chip_hwgpio(desc);
Andrew Jefferyfa59dd232019-03-26 15:19:54 +10303190 rc = chip->set_config(chip, gpio, packed);
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303191 if (rc == -ENOTSUPP) {
3192 dev_dbg(&desc->gdev->dev, "Persistence not supported for GPIO %d\n",
3193 gpio);
3194 return 0;
3195 }
3196
3197 return rc;
3198}
3199EXPORT_SYMBOL_GPL(gpiod_set_transitory);
3200
3201/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003202 * gpiod_is_active_low - test whether a GPIO is active-low or not
3203 * @desc: the gpio descriptor to test
3204 *
3205 * Returns 1 if the GPIO is active-low, 0 otherwise.
3206 */
3207int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003208{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003209 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003210 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003211}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003212EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08003213
3214/* I/O calls are only valid after configuration completed; the relevant
3215 * "is this a valid GPIO" error checks should already have been done.
3216 *
3217 * "Get" operations are often inlinable as reading a pin value register,
3218 * and masking the relevant bit in that register.
3219 *
3220 * When "set" operations are inlinable, they involve writing that mask to
3221 * one register to set a low value, or a different register to set it high.
3222 * Otherwise locking is needed, so there may be little value to inlining.
3223 *
3224 *------------------------------------------------------------------------
3225 *
3226 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
3227 * have requested the GPIO. That can include implicit requesting by
3228 * a direction setting call. Marking a gpio as requested locks its chip
3229 * in memory, guaranteeing that these table lookups need no more locking
3230 * and that gpiochip_remove() will fail.
3231 *
3232 * REVISIT when debugging, consider adding some instrumentation to ensure
3233 * that the GPIO was actually requested.
3234 */
3235
Linus Walleijfac9d882017-09-26 20:58:28 +02003236static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08003237{
3238 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003239 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003240 int value;
David Brownelld2876d02008-02-04 22:28:20 -08003241
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003242 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003243 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003244 value = chip->get ? chip->get(chip, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01003245 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003246 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06003247 return value;
David Brownelld2876d02008-02-04 22:28:20 -08003248}
Alexandre Courbot372e7222013-02-03 01:29:29 +09003249
Lukas Wunnereec1d562017-10-12 12:40:10 +02003250static int gpio_chip_get_multiple(struct gpio_chip *chip,
3251 unsigned long *mask, unsigned long *bits)
3252{
3253 if (chip->get_multiple) {
3254 return chip->get_multiple(chip, mask, bits);
3255 } else if (chip->get) {
3256 int i, value;
3257
3258 for_each_set_bit(i, mask, chip->ngpio) {
3259 value = chip->get(chip, i);
3260 if (value < 0)
3261 return value;
3262 __assign_bit(i, bits, value);
3263 }
3264 return 0;
3265 }
3266 return -EIO;
3267}
3268
3269int gpiod_get_array_value_complex(bool raw, bool can_sleep,
3270 unsigned int array_size,
3271 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003272 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003273 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02003274{
Linus Walleijd377f562019-07-16 11:11:45 +02003275 int ret, i = 0;
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003276
3277 /*
3278 * Validate array_info against desc_array and its size.
3279 * It should immediately follow desc_array if both
3280 * have been obtained from the same gpiod_get_array() call.
3281 */
3282 if (array_info && array_info->desc == desc_array &&
3283 array_size <= array_info->size &&
3284 (void *)array_info == desc_array + array_info->size) {
3285 if (!can_sleep)
3286 WARN_ON(array_info->chip->can_sleep);
3287
Linus Walleijd377f562019-07-16 11:11:45 +02003288 ret = gpio_chip_get_multiple(array_info->chip,
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003289 array_info->get_mask,
3290 value_bitmap);
Linus Walleijd377f562019-07-16 11:11:45 +02003291 if (ret)
3292 return ret;
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003293
3294 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
3295 bitmap_xor(value_bitmap, value_bitmap,
3296 array_info->invert_mask, array_size);
3297
3298 if (bitmap_full(array_info->get_mask, array_size))
3299 return 0;
3300
3301 i = find_first_zero_bit(array_info->get_mask, array_size);
3302 } else {
3303 array_info = NULL;
3304 }
Lukas Wunnereec1d562017-10-12 12:40:10 +02003305
3306 while (i < array_size) {
3307 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Laura Abbott30277432018-05-21 10:57:07 -07003308 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
3309 unsigned long *mask, *bits;
Lukas Wunnereec1d562017-10-12 12:40:10 +02003310 int first, j, ret;
3311
Laura Abbott30277432018-05-21 10:57:07 -07003312 if (likely(chip->ngpio <= FASTPATH_NGPIO)) {
3313 mask = fastpath;
3314 } else {
3315 mask = kmalloc_array(2 * BITS_TO_LONGS(chip->ngpio),
3316 sizeof(*mask),
3317 can_sleep ? GFP_KERNEL : GFP_ATOMIC);
3318 if (!mask)
3319 return -ENOMEM;
3320 }
3321
3322 bits = mask + BITS_TO_LONGS(chip->ngpio);
3323 bitmap_zero(mask, chip->ngpio);
3324
Lukas Wunnereec1d562017-10-12 12:40:10 +02003325 if (!can_sleep)
3326 WARN_ON(chip->can_sleep);
3327
3328 /* collect all inputs belonging to the same chip */
3329 first = i;
Lukas Wunnereec1d562017-10-12 12:40:10 +02003330 do {
3331 const struct gpio_desc *desc = desc_array[i];
3332 int hwgpio = gpio_chip_hwgpio(desc);
3333
3334 __set_bit(hwgpio, mask);
3335 i++;
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003336
3337 if (array_info)
Janusz Krzysztofik35ae7f92018-09-24 01:53:35 +02003338 i = find_next_zero_bit(array_info->get_mask,
3339 array_size, i);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003340 } while ((i < array_size) &&
3341 (desc_array[i]->gdev->chip == chip));
3342
3343 ret = gpio_chip_get_multiple(chip, mask, bits);
Laura Abbott30277432018-05-21 10:57:07 -07003344 if (ret) {
3345 if (mask != fastpath)
3346 kfree(mask);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003347 return ret;
Laura Abbott30277432018-05-21 10:57:07 -07003348 }
Lukas Wunnereec1d562017-10-12 12:40:10 +02003349
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003350 for (j = first; j < i; ) {
Lukas Wunnereec1d562017-10-12 12:40:10 +02003351 const struct gpio_desc *desc = desc_array[j];
3352 int hwgpio = gpio_chip_hwgpio(desc);
3353 int value = test_bit(hwgpio, bits);
3354
3355 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3356 value = !value;
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003357 __assign_bit(j, value_bitmap, value);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003358 trace_gpio_value(desc_to_gpio(desc), 1, value);
Janusz Krzysztofik799d5eb2018-09-29 14:20:22 +02003359 j++;
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003360
3361 if (array_info)
Janusz Krzysztofik35ae7f92018-09-24 01:53:35 +02003362 j = find_next_zero_bit(array_info->get_mask, i,
3363 j);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003364 }
Laura Abbott30277432018-05-21 10:57:07 -07003365
3366 if (mask != fastpath)
3367 kfree(mask);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003368 }
3369 return 0;
3370}
3371
David Brownelld2876d02008-02-04 22:28:20 -08003372/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003373 * gpiod_get_raw_value() - return a gpio's raw value
3374 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08003375 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003376 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003377 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003378 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003379 * This function can be called from contexts where we cannot sleep, and will
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003380 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003381 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003382int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003383{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003384 VALIDATE_DESC(desc);
Geert Uytterhoeven3285170f2019-07-01 16:27:38 +02003385 /* Should be using gpiod_get_raw_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003386 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02003387 return gpiod_get_raw_value_commit(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003388}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003389EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08003390
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003391/**
3392 * gpiod_get_value() - return a gpio's value
3393 * @desc: gpio whose value will be returned
3394 *
3395 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003396 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003397 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003398 * This function can be called from contexts where we cannot sleep, and will
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003399 * complain if the GPIO chip functions potentially sleep.
3400 */
3401int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08003402{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003403 int value;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003404
3405 VALIDATE_DESC(desc);
Geert Uytterhoeven3285170f2019-07-01 16:27:38 +02003406 /* Should be using gpiod_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003407 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003408
Linus Walleijfac9d882017-09-26 20:58:28 +02003409 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003410 if (value < 0)
3411 return value;
3412
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003413 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3414 value = !value;
3415
3416 return value;
David Brownelld2876d02008-02-04 22:28:20 -08003417}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003418EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08003419
Lukas Wunnereec1d562017-10-12 12:40:10 +02003420/**
3421 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003422 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02003423 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003424 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003425 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02003426 *
3427 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3428 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3429 * else an error code.
3430 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003431 * This function can be called from contexts where we cannot sleep,
Lukas Wunnereec1d562017-10-12 12:40:10 +02003432 * and it will complain if the GPIO chip functions potentially sleep.
3433 */
3434int gpiod_get_raw_array_value(unsigned int array_size,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003435 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003436 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003437 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02003438{
3439 if (!desc_array)
3440 return -EINVAL;
3441 return gpiod_get_array_value_complex(true, false, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003442 desc_array, array_info,
3443 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003444}
3445EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
3446
3447/**
3448 * gpiod_get_array_value() - read values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003449 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02003450 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003451 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003452 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02003453 *
3454 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3455 * into account. Return 0 in case of success, else an error code.
3456 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003457 * This function can be called from contexts where we cannot sleep,
Lukas Wunnereec1d562017-10-12 12:40:10 +02003458 * and it will complain if the GPIO chip functions potentially sleep.
3459 */
3460int gpiod_get_array_value(unsigned int array_size,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003461 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003462 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003463 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02003464{
3465 if (!desc_array)
3466 return -EINVAL;
3467 return gpiod_get_array_value_complex(false, false, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003468 desc_array, array_info,
3469 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003470}
3471EXPORT_SYMBOL_GPL(gpiod_get_array_value);
3472
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303473/*
Linus Walleijfac9d882017-09-26 20:58:28 +02003474 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003475 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07003476 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303477 */
Linus Walleijfac9d882017-09-26 20:58:28 +02003478static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303479{
Linus Walleijd377f562019-07-16 11:11:45 +02003480 int ret = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003481 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003482 int offset = gpio_chip_hwgpio(desc);
3483
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303484 if (value) {
Linus Walleijd377f562019-07-16 11:11:45 +02003485 ret = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303486 } else {
Linus Walleijd377f562019-07-16 11:11:45 +02003487 ret = chip->direction_output(chip, offset, 0);
3488 if (!ret)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003489 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303490 }
Linus Walleijd377f562019-07-16 11:11:45 +02003491 trace_gpio_direction(desc_to_gpio(desc), value, ret);
3492 if (ret < 0)
Mark Brown6424de52013-09-09 10:33:49 +01003493 gpiod_err(desc,
3494 "%s: Error in set_value for open drain err %d\n",
Linus Walleijd377f562019-07-16 11:11:45 +02003495 __func__, ret);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303496}
3497
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303498/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003499 * _gpio_set_open_source_value() - Set the open source gpio's value.
3500 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07003501 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303502 */
Linus Walleijfac9d882017-09-26 20:58:28 +02003503static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303504{
Linus Walleijd377f562019-07-16 11:11:45 +02003505 int ret = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003506 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003507 int offset = gpio_chip_hwgpio(desc);
3508
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303509 if (value) {
Linus Walleijd377f562019-07-16 11:11:45 +02003510 ret = chip->direction_output(chip, offset, 1);
3511 if (!ret)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003512 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303513 } else {
Linus Walleijd377f562019-07-16 11:11:45 +02003514 ret = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303515 }
Linus Walleijd377f562019-07-16 11:11:45 +02003516 trace_gpio_direction(desc_to_gpio(desc), !value, ret);
3517 if (ret < 0)
Mark Brown6424de52013-09-09 10:33:49 +01003518 gpiod_err(desc,
3519 "%s: Error in set_value for open source err %d\n",
Linus Walleijd377f562019-07-16 11:11:45 +02003520 __func__, ret);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303521}
3522
Linus Walleijfac9d882017-09-26 20:58:28 +02003523static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08003524{
3525 struct gpio_chip *chip;
3526
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003527 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003528 trace_gpio_value(desc_to_gpio(desc), 0, value);
Linus Walleij02e47982017-09-26 21:20:23 +02003529 chip->set(chip, gpio_chip_hwgpio(desc), value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003530}
3531
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003532/*
3533 * set multiple outputs on the same chip;
3534 * use the chip's set_multiple function if available;
3535 * otherwise set the outputs sequentially;
3536 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
3537 * defines which outputs are to be changed
3538 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
3539 * defines the values the outputs specified by mask are to be set to
3540 */
3541static void gpio_chip_set_multiple(struct gpio_chip *chip,
3542 unsigned long *mask, unsigned long *bits)
3543{
3544 if (chip->set_multiple) {
3545 chip->set_multiple(chip, mask, bits);
3546 } else {
Andy Shevchenko5e4e6fb2017-01-03 19:01:17 +02003547 unsigned int i;
3548
3549 /* set outputs if the corresponding mask bit is set */
3550 for_each_set_bit(i, mask, chip->ngpio)
3551 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003552 }
3553}
3554
Laura Abbott30277432018-05-21 10:57:07 -07003555int gpiod_set_array_value_complex(bool raw, bool can_sleep,
Geert Uytterhoeven3c940662018-09-27 13:38:10 +02003556 unsigned int array_size,
3557 struct gpio_desc **desc_array,
3558 struct gpio_array *array_info,
3559 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003560{
3561 int i = 0;
3562
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003563 /*
3564 * Validate array_info against desc_array and its size.
3565 * It should immediately follow desc_array if both
3566 * have been obtained from the same gpiod_get_array() call.
3567 */
3568 if (array_info && array_info->desc == desc_array &&
3569 array_size <= array_info->size &&
3570 (void *)array_info == desc_array + array_info->size) {
3571 if (!can_sleep)
3572 WARN_ON(array_info->chip->can_sleep);
3573
3574 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
3575 bitmap_xor(value_bitmap, value_bitmap,
3576 array_info->invert_mask, array_size);
3577
3578 gpio_chip_set_multiple(array_info->chip, array_info->set_mask,
3579 value_bitmap);
3580
3581 if (bitmap_full(array_info->set_mask, array_size))
3582 return 0;
3583
3584 i = find_first_zero_bit(array_info->set_mask, array_size);
3585 } else {
3586 array_info = NULL;
3587 }
3588
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003589 while (i < array_size) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003590 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Laura Abbott30277432018-05-21 10:57:07 -07003591 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
3592 unsigned long *mask, *bits;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003593 int count = 0;
3594
Laura Abbott30277432018-05-21 10:57:07 -07003595 if (likely(chip->ngpio <= FASTPATH_NGPIO)) {
3596 mask = fastpath;
3597 } else {
3598 mask = kmalloc_array(2 * BITS_TO_LONGS(chip->ngpio),
3599 sizeof(*mask),
3600 can_sleep ? GFP_KERNEL : GFP_ATOMIC);
3601 if (!mask)
3602 return -ENOMEM;
3603 }
3604
3605 bits = mask + BITS_TO_LONGS(chip->ngpio);
3606 bitmap_zero(mask, chip->ngpio);
3607
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003608 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003609 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003610
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003611 do {
3612 struct gpio_desc *desc = desc_array[i];
3613 int hwgpio = gpio_chip_hwgpio(desc);
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003614 int value = test_bit(i, value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003615
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003616 /*
3617 * Pins applicable for fast input but not for
3618 * fast output processing may have been already
3619 * inverted inside the fast path, skip them.
3620 */
3621 if (!raw && !(array_info &&
3622 test_bit(i, array_info->invert_mask)) &&
3623 test_bit(FLAG_ACTIVE_LOW, &desc->flags))
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003624 value = !value;
3625 trace_gpio_value(desc_to_gpio(desc), 0, value);
3626 /*
3627 * collect all normal outputs belonging to the same chip
3628 * open drain and open source outputs are set individually
3629 */
Linus Walleij02e47982017-09-26 21:20:23 +02003630 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02003631 gpio_set_open_drain_value_commit(desc, value);
Linus Walleij02e47982017-09-26 21:20:23 +02003632 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02003633 gpio_set_open_source_value_commit(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003634 } else {
3635 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003636 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003637 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003638 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003639 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003640 count++;
3641 }
3642 i++;
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003643
3644 if (array_info)
Janusz Krzysztofik35ae7f92018-09-24 01:53:35 +02003645 i = find_next_zero_bit(array_info->set_mask,
3646 array_size, i);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003647 } while ((i < array_size) &&
3648 (desc_array[i]->gdev->chip == chip));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003649 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003650 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003651 gpio_chip_set_multiple(chip, mask, bits);
Laura Abbott30277432018-05-21 10:57:07 -07003652
3653 if (mask != fastpath)
3654 kfree(mask);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003655 }
Laura Abbott30277432018-05-21 10:57:07 -07003656 return 0;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003657}
3658
David Brownelld2876d02008-02-04 22:28:20 -08003659/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003660 * gpiod_set_raw_value() - assign a gpio's raw value
3661 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08003662 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08003663 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003664 * Set the raw value of the GPIO, i.e. the value of its physical line without
3665 * regard for its ACTIVE_LOW status.
3666 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003667 * This function can be called from contexts where we cannot sleep, and will
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003668 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003669 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003670void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003671{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003672 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven3285170f2019-07-01 16:27:38 +02003673 /* Should be using gpiod_set_raw_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003674 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02003675 gpiod_set_raw_value_commit(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08003676}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003677EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08003678
3679/**
Geert Uytterhoeven1e77fc82018-01-09 19:08:21 +01003680 * gpiod_set_value_nocheck() - set a GPIO line value without checking
3681 * @desc: the descriptor to set the value on
3682 * @value: value to set
3683 *
3684 * This sets the value of a GPIO line backing a descriptor, applying
3685 * different semantic quirks like active low and open drain/source
3686 * handling.
3687 */
3688static void gpiod_set_value_nocheck(struct gpio_desc *desc, int value)
3689{
3690 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3691 value = !value;
3692 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
3693 gpio_set_open_drain_value_commit(desc, value);
3694 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
3695 gpio_set_open_source_value_commit(desc, value);
3696 else
3697 gpiod_set_raw_value_commit(desc, value);
3698}
3699
3700/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003701 * gpiod_set_value() - assign a gpio's value
3702 * @desc: gpio whose value will be assigned
3703 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08003704 *
Linus Walleij02e47982017-09-26 21:20:23 +02003705 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
3706 * OPEN_DRAIN and OPEN_SOURCE flags into account.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003707 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003708 * This function can be called from contexts where we cannot sleep, and will
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003709 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003710 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003711void gpiod_set_value(struct gpio_desc *desc, int value)
3712{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003713 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven3285170f2019-07-01 16:27:38 +02003714 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003715 WARN_ON(desc->gdev->chip->can_sleep);
Geert Uytterhoeven1e77fc82018-01-09 19:08:21 +01003716 gpiod_set_value_nocheck(desc, value);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003717}
3718EXPORT_SYMBOL_GPL(gpiod_set_value);
3719
3720/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003721 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003722 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003723 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003724 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003725 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003726 *
3727 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3728 * without regard for their ACTIVE_LOW status.
3729 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003730 * This function can be called from contexts where we cannot sleep, and will
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003731 * complain if the GPIO chip functions potentially sleep.
3732 */
Laura Abbott30277432018-05-21 10:57:07 -07003733int gpiod_set_raw_array_value(unsigned int array_size,
Geert Uytterhoeven3c940662018-09-27 13:38:10 +02003734 struct gpio_desc **desc_array,
3735 struct gpio_array *array_info,
3736 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003737{
3738 if (!desc_array)
Laura Abbott30277432018-05-21 10:57:07 -07003739 return -EINVAL;
3740 return gpiod_set_array_value_complex(true, false, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003741 desc_array, array_info, value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003742}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003743EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003744
3745/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003746 * gpiod_set_array_value() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003747 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003748 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003749 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003750 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003751 *
3752 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3753 * into account.
3754 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003755 * This function can be called from contexts where we cannot sleep, and will
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003756 * complain if the GPIO chip functions potentially sleep.
3757 */
Geert Uytterhoevencf9af0d2018-09-27 13:38:09 +02003758int gpiod_set_array_value(unsigned int array_size,
3759 struct gpio_desc **desc_array,
3760 struct gpio_array *array_info,
3761 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003762{
3763 if (!desc_array)
Geert Uytterhoevencf9af0d2018-09-27 13:38:09 +02003764 return -EINVAL;
3765 return gpiod_set_array_value_complex(false, false, array_size,
3766 desc_array, array_info,
3767 value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003768}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003769EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003770
3771/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003772 * gpiod_cansleep() - report whether gpio value access may sleep
3773 * @desc: gpio to check
3774 *
3775 */
3776int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003777{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003778 VALIDATE_DESC(desc);
3779 return desc->gdev->chip->can_sleep;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003780}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003781EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08003782
David Brownell0f6d5042008-10-15 22:03:14 -07003783/**
Linus Walleij90b39402e2018-06-01 13:21:27 +02003784 * gpiod_set_consumer_name() - set the consumer name for the descriptor
3785 * @desc: gpio to set the consumer name on
3786 * @name: the new consumer name
3787 */
Muchun Song18534df2018-11-01 21:12:50 +08003788int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name)
Linus Walleij90b39402e2018-06-01 13:21:27 +02003789{
Muchun Song18534df2018-11-01 21:12:50 +08003790 VALIDATE_DESC(desc);
3791 if (name) {
3792 name = kstrdup_const(name, GFP_KERNEL);
3793 if (!name)
3794 return -ENOMEM;
3795 }
3796
3797 kfree_const(desc->label);
3798 desc_set_label(desc, name);
3799
3800 return 0;
Linus Walleij90b39402e2018-06-01 13:21:27 +02003801}
3802EXPORT_SYMBOL_GPL(gpiod_set_consumer_name);
3803
3804/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003805 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
3806 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07003807 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003808 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
3809 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07003810 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003811int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07003812{
Linus Walleij4c37ce82016-05-02 13:13:10 +02003813 struct gpio_chip *chip;
3814 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07003815
Linus Walleij79bb71b2016-06-15 22:57:38 +02003816 /*
3817 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
3818 * requires this function to not return zero on an invalid descriptor
3819 * but rather a negative error number.
3820 */
Linus Walleijbfbbe442016-06-16 11:55:55 +02003821 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
Linus Walleij79bb71b2016-06-15 22:57:38 +02003822 return -EINVAL;
3823
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003824 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003825 offset = gpio_chip_hwgpio(desc);
Linus Walleij4c37ce82016-05-02 13:13:10 +02003826 if (chip->to_irq) {
3827 int retirq = chip->to_irq(chip, offset);
3828
3829 /* Zero means NO_IRQ */
3830 if (!retirq)
3831 return -ENXIO;
3832
3833 return retirq;
3834 }
3835 return -ENXIO;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003836}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003837EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003838
Linus Walleijd468bf92013-09-24 11:54:38 +02003839/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003840 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003841 * @chip: the chip the GPIO to lock belongs to
3842 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02003843 *
3844 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08003845 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08003846 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003847int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08003848{
Linus Walleij9c102802016-05-25 10:56:03 +02003849 struct gpio_desc *desc;
Linus Walleijd468bf92013-09-24 11:54:38 +02003850
Linus Walleij9c102802016-05-25 10:56:03 +02003851 desc = gpiochip_get_desc(chip, offset);
3852 if (IS_ERR(desc))
3853 return PTR_ERR(desc);
3854
Linus Walleij60f83392016-11-12 15:01:09 +01003855 /*
3856 * If it's fast: flush the direction setting if something changed
3857 * behind our back
3858 */
3859 if (!chip->can_sleep && chip->get_direction) {
Andy Shevchenko80956792018-07-09 21:47:21 +03003860 int dir = gpiod_get_direction(desc);
Linus Walleij9c102802016-05-25 10:56:03 +02003861
Andy Shevchenko36b31272018-07-03 03:38:31 +03003862 if (dir < 0) {
3863 chip_err(chip, "%s: cannot get GPIO direction\n",
3864 __func__);
3865 return dir;
3866 }
Linus Walleij9c102802016-05-25 10:56:03 +02003867 }
3868
3869 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003870 chip_err(chip,
Andy Shevchenkob1911712018-07-03 03:39:03 +03003871 "%s: tried to flag a GPIO set as output for IRQ\n",
3872 __func__);
Linus Walleijd468bf92013-09-24 11:54:38 +02003873 return -EIO;
3874 }
3875
Linus Walleij9c102802016-05-25 10:56:03 +02003876 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
Hans Verkuil4e9439d2018-09-08 11:23:16 +02003877 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
Linus Walleij3940c342016-11-14 00:09:07 +01003878
3879 /*
3880 * If the consumer has not set up a label (such as when the
3881 * IRQ is referenced from .to_irq()) we set up a label here
3882 * so it is clear this is used as an interrupt.
3883 */
3884 if (!desc->label)
3885 desc_set_label(desc, "interrupt");
3886
Linus Walleijd468bf92013-09-24 11:54:38 +02003887 return 0;
3888}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003889EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02003890
Linus Walleijd468bf92013-09-24 11:54:38 +02003891/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003892 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003893 * @chip: the chip the GPIO to lock belongs to
3894 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02003895 *
3896 * This is used directly by GPIO drivers that want to indicate
3897 * that a certain GPIO is no longer used exclusively for IRQ.
3898 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003899void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02003900{
Linus Walleij3940c342016-11-14 00:09:07 +01003901 struct gpio_desc *desc;
3902
3903 desc = gpiochip_get_desc(chip, offset);
3904 if (IS_ERR(desc))
Linus Walleijd468bf92013-09-24 11:54:38 +02003905 return;
3906
Linus Walleij3940c342016-11-14 00:09:07 +01003907 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
Hans Verkuil4e9439d2018-09-08 11:23:16 +02003908 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
Linus Walleij3940c342016-11-14 00:09:07 +01003909
3910 /* If we only had this marking, erase it */
3911 if (desc->label && !strcmp(desc->label, "interrupt"))
3912 desc_set_label(desc, NULL);
Linus Walleijd468bf92013-09-24 11:54:38 +02003913}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003914EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02003915
Hans Verkuil4e9439d2018-09-08 11:23:16 +02003916void gpiochip_disable_irq(struct gpio_chip *chip, unsigned int offset)
3917{
3918 struct gpio_desc *desc = gpiochip_get_desc(chip, offset);
3919
3920 if (!IS_ERR(desc) &&
3921 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags)))
3922 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3923}
3924EXPORT_SYMBOL_GPL(gpiochip_disable_irq);
3925
3926void gpiochip_enable_irq(struct gpio_chip *chip, unsigned int offset)
3927{
3928 struct gpio_desc *desc = gpiochip_get_desc(chip, offset);
3929
3930 if (!IS_ERR(desc) &&
3931 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) {
3932 WARN_ON(test_bit(FLAG_IS_OUT, &desc->flags));
3933 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3934 }
3935}
3936EXPORT_SYMBOL_GPL(gpiochip_enable_irq);
3937
Linus Walleij6cee3822016-02-11 20:16:45 +01003938bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
3939{
3940 if (offset >= chip->ngpio)
3941 return false;
3942
3943 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
3944}
3945EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
3946
Hans Verkuil4e6b8232018-09-08 11:23:14 +02003947int gpiochip_reqres_irq(struct gpio_chip *chip, unsigned int offset)
3948{
3949 int ret;
3950
3951 if (!try_module_get(chip->gpiodev->owner))
3952 return -ENODEV;
3953
3954 ret = gpiochip_lock_as_irq(chip, offset);
3955 if (ret) {
3956 chip_err(chip, "unable to lock HW IRQ %u for IRQ\n", offset);
3957 module_put(chip->gpiodev->owner);
3958 return ret;
3959 }
3960 return 0;
3961}
3962EXPORT_SYMBOL_GPL(gpiochip_reqres_irq);
3963
3964void gpiochip_relres_irq(struct gpio_chip *chip, unsigned int offset)
3965{
3966 gpiochip_unlock_as_irq(chip, offset);
3967 module_put(chip->gpiodev->owner);
3968}
3969EXPORT_SYMBOL_GPL(gpiochip_relres_irq);
3970
Linus Walleij143b65d2016-02-16 15:41:42 +01003971bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
3972{
3973 if (offset >= chip->ngpio)
3974 return false;
3975
3976 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
3977}
3978EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
3979
3980bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
3981{
3982 if (offset >= chip->ngpio)
3983 return false;
3984
3985 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
3986}
3987EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
3988
Charles Keepax05f479b2017-05-23 15:47:29 +01003989bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset)
3990{
3991 if (offset >= chip->ngpio)
3992 return false;
3993
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303994 return !test_bit(FLAG_TRANSITORY, &chip->gpiodev->descs[offset].flags);
Charles Keepax05f479b2017-05-23 15:47:29 +01003995}
3996EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
3997
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003998/**
3999 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
4000 * @desc: gpio whose value will be returned
4001 *
4002 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07004003 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004004 *
4005 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08004006 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004007int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08004008{
David Brownelld2876d02008-02-04 22:28:20 -08004009 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004010 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02004011 return gpiod_get_raw_value_commit(desc);
David Brownelld2876d02008-02-04 22:28:20 -08004012}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004013EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09004014
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004015/**
4016 * gpiod_get_value_cansleep() - return a gpio's value
4017 * @desc: gpio whose value will be returned
4018 *
4019 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07004020 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004021 *
4022 * This function is to be called from contexts that can sleep.
4023 */
4024int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09004025{
David Brownelld2876d02008-02-04 22:28:20 -08004026 int value;
David Brownelld2876d02008-02-04 22:28:20 -08004027
4028 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004029 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02004030 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07004031 if (value < 0)
4032 return value;
4033
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004034 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
4035 value = !value;
4036
David Brownelld2876d02008-02-04 22:28:20 -08004037 return value;
4038}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004039EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09004040
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004041/**
Lukas Wunnereec1d562017-10-12 12:40:10 +02004042 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004043 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02004044 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004045 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004046 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02004047 *
4048 * Read the raw values of the GPIOs, i.e. the values of the physical lines
4049 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
4050 * else an error code.
4051 *
4052 * This function is to be called from contexts that can sleep.
4053 */
4054int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
4055 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004056 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004057 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02004058{
4059 might_sleep_if(extra_checks);
4060 if (!desc_array)
4061 return -EINVAL;
4062 return gpiod_get_array_value_complex(true, true, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004063 desc_array, array_info,
4064 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02004065}
4066EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
4067
4068/**
4069 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004070 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02004071 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004072 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004073 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02004074 *
4075 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
4076 * into account. Return 0 in case of success, else an error code.
4077 *
4078 * This function is to be called from contexts that can sleep.
4079 */
4080int gpiod_get_array_value_cansleep(unsigned int array_size,
4081 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004082 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004083 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02004084{
4085 might_sleep_if(extra_checks);
4086 if (!desc_array)
4087 return -EINVAL;
4088 return gpiod_get_array_value_complex(false, true, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004089 desc_array, array_info,
4090 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02004091}
4092EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
4093
4094/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004095 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
4096 * @desc: gpio whose value will be assigned
4097 * @value: value to assign
4098 *
4099 * Set the raw value of the GPIO, i.e. the value of its physical line without
4100 * regard for its ACTIVE_LOW status.
4101 *
4102 * This function is to be called from contexts that can sleep.
4103 */
4104void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09004105{
David Brownelld2876d02008-02-04 22:28:20 -08004106 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004107 VALIDATE_DESC_VOID(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02004108 gpiod_set_raw_value_commit(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09004109}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004110EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09004111
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004112/**
4113 * gpiod_set_value_cansleep() - assign a gpio's value
4114 * @desc: gpio whose value will be assigned
4115 * @value: value to assign
4116 *
4117 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
4118 * account
4119 *
4120 * This function is to be called from contexts that can sleep.
4121 */
4122void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09004123{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004124 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004125 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1e77fc82018-01-09 19:08:21 +01004126 gpiod_set_value_nocheck(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08004127}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004128EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08004129
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004130/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02004131 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004132 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004133 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004134 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004135 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004136 *
4137 * Set the raw values of the GPIOs, i.e. the values of the physical lines
4138 * without regard for their ACTIVE_LOW status.
4139 *
4140 * This function is to be called from contexts that can sleep.
4141 */
Laura Abbott30277432018-05-21 10:57:07 -07004142int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
Geert Uytterhoeven3c940662018-09-27 13:38:10 +02004143 struct gpio_desc **desc_array,
4144 struct gpio_array *array_info,
4145 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004146{
4147 might_sleep_if(extra_checks);
4148 if (!desc_array)
Laura Abbott30277432018-05-21 10:57:07 -07004149 return -EINVAL;
4150 return gpiod_set_array_value_complex(true, true, array_size, desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004151 array_info, value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004152}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02004153EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004154
4155/**
Dmitry Torokhov3946d182017-08-14 21:59:55 -07004156 * gpiod_add_lookup_tables() - register GPIO device consumers
4157 * @tables: list of tables of consumers to register
4158 * @n: number of tables in the list
4159 */
4160void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
4161{
4162 unsigned int i;
4163
4164 mutex_lock(&gpio_lookup_lock);
4165
4166 for (i = 0; i < n; i++)
4167 list_add_tail(&tables[i]->list, &gpio_lookup_list);
4168
4169 mutex_unlock(&gpio_lookup_lock);
4170}
4171
4172/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02004173 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004174 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004175 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004176 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004177 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004178 *
4179 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
4180 * into account.
4181 *
4182 * This function is to be called from contexts that can sleep.
4183 */
Geert Uytterhoevencf9af0d2018-09-27 13:38:09 +02004184int gpiod_set_array_value_cansleep(unsigned int array_size,
4185 struct gpio_desc **desc_array,
4186 struct gpio_array *array_info,
4187 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004188{
4189 might_sleep_if(extra_checks);
4190 if (!desc_array)
Geert Uytterhoevencf9af0d2018-09-27 13:38:09 +02004191 return -EINVAL;
4192 return gpiod_set_array_value_complex(false, true, array_size,
4193 desc_array, array_info,
4194 value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004195}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02004196EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004197
4198/**
Alexandre Courbotad824782013-12-03 12:20:11 +09004199 * gpiod_add_lookup_table() - register GPIO device consumers
4200 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004201 */
Alexandre Courbotad824782013-12-03 12:20:11 +09004202void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004203{
4204 mutex_lock(&gpio_lookup_lock);
4205
Alexandre Courbotad824782013-12-03 12:20:11 +09004206 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004207
4208 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08004209}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02004210EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
David Brownelld2876d02008-02-04 22:28:20 -08004211
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05304212/**
4213 * gpiod_remove_lookup_table() - unregister GPIO device consumers
4214 * @table: table of consumers to unregister
4215 */
4216void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
4217{
4218 mutex_lock(&gpio_lookup_lock);
4219
4220 list_del(&table->list);
4221
4222 mutex_unlock(&gpio_lookup_lock);
4223}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02004224EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05304225
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02004226/**
4227 * gpiod_add_hogs() - register a set of GPIO hogs from machine code
4228 * @hogs: table of gpio hog entries with a zeroed sentinel at the end
4229 */
4230void gpiod_add_hogs(struct gpiod_hog *hogs)
4231{
4232 struct gpio_chip *chip;
4233 struct gpiod_hog *hog;
4234
4235 mutex_lock(&gpio_machine_hogs_mutex);
4236
4237 for (hog = &hogs[0]; hog->chip_label; hog++) {
4238 list_add_tail(&hog->list, &gpio_machine_hogs);
4239
4240 /*
4241 * The chip may have been registered earlier, so check if it
4242 * exists and, if so, try to hog the line now.
4243 */
4244 chip = find_chip_by_name(hog->chip_label);
4245 if (chip)
4246 gpiochip_machine_hog(chip, hog);
4247 }
4248
4249 mutex_unlock(&gpio_machine_hogs_mutex);
4250}
4251EXPORT_SYMBOL_GPL(gpiod_add_hogs);
4252
Alexandre Courbotad824782013-12-03 12:20:11 +09004253static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
4254{
4255 const char *dev_id = dev ? dev_name(dev) : NULL;
4256 struct gpiod_lookup_table *table;
4257
4258 mutex_lock(&gpio_lookup_lock);
4259
4260 list_for_each_entry(table, &gpio_lookup_list, list) {
4261 if (table->dev_id && dev_id) {
4262 /*
4263 * Valid strings on both ends, must be identical to have
4264 * a match
4265 */
4266 if (!strcmp(table->dev_id, dev_id))
4267 goto found;
4268 } else {
4269 /*
4270 * One of the pointers is NULL, so both must be to have
4271 * a match
4272 */
4273 if (dev_id == table->dev_id)
4274 goto found;
4275 }
4276 }
4277 table = NULL;
4278
4279found:
4280 mutex_unlock(&gpio_lookup_lock);
4281 return table;
4282}
4283
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004284static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Andy Shevchenkofed70262019-04-10 18:39:16 +03004285 unsigned int idx, unsigned long *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004286{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004287 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09004288 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004289 struct gpiod_lookup *p;
4290
Alexandre Courbotad824782013-12-03 12:20:11 +09004291 table = gpiod_find_lookup_table(dev);
4292 if (!table)
4293 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004294
Alexandre Courbotad824782013-12-03 12:20:11 +09004295 for (p = &table->table[0]; p->chip_label; p++) {
4296 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004297
Alexandre Courbotad824782013-12-03 12:20:11 +09004298 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004299 if (p->idx != idx)
4300 continue;
4301
Alexandre Courbotad824782013-12-03 12:20:11 +09004302 /* If the lookup entry has a con_id, require exact match */
4303 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
4304 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004305
Alexandre Courbotad824782013-12-03 12:20:11 +09004306 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004307
Alexandre Courbotad824782013-12-03 12:20:11 +09004308 if (!chip) {
Janusz Krzysztofik8853daf2018-07-04 00:18:19 +02004309 /*
4310 * As the lookup table indicates a chip with
4311 * p->chip_label should exist, assume it may
4312 * still appear later and let the interested
4313 * consumer be probed again or let the Deferred
4314 * Probe infrastructure handle the error.
4315 */
4316 dev_warn(dev, "cannot find GPIO chip %s, deferring\n",
4317 p->chip_label);
4318 return ERR_PTR(-EPROBE_DEFER);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004319 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004320
Alexandre Courbotad824782013-12-03 12:20:11 +09004321 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004322 dev_err(dev,
4323 "requested GPIO %d is out of range [0..%d] for chip %s\n",
4324 idx, chip->ngpio, chip->label);
4325 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09004326 }
4327
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09004328 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09004329 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004330
4331 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09004332 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004333
4334 return desc;
4335}
4336
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004337static int platform_gpio_count(struct device *dev, const char *con_id)
4338{
4339 struct gpiod_lookup_table *table;
4340 struct gpiod_lookup *p;
4341 unsigned int count = 0;
4342
4343 table = gpiod_find_lookup_table(dev);
4344 if (!table)
4345 return -ENOENT;
4346
4347 for (p = &table->table[0]; p->chip_label; p++) {
4348 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
4349 (!con_id && !p->con_id))
4350 count++;
4351 }
4352 if (!count)
4353 return -ENOENT;
4354
4355 return count;
4356}
4357
4358/**
4359 * gpiod_count - return the number of GPIOs associated with a device / function
4360 * or -ENOENT if no GPIO has been assigned to the requested function
4361 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4362 * @con_id: function within the GPIO consumer
4363 */
4364int gpiod_count(struct device *dev, const char *con_id)
4365{
4366 int count = -ENOENT;
4367
4368 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
Linus Walleijf626d6d2019-07-17 09:10:01 +02004369 count = of_gpio_get_count(dev, con_id);
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004370 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
4371 count = acpi_gpio_count(dev, con_id);
4372
4373 if (count < 0)
4374 count = platform_gpio_count(dev, con_id);
4375
4376 return count;
4377}
4378EXPORT_SYMBOL_GPL(gpiod_count);
4379
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004380/**
Thierry Reding08791622014-04-25 16:54:22 +02004381 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09004382 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004383 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004384 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004385 *
4386 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004387 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07004388 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004389 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004390struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004391 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004392{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004393 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004394}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004395EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004396
4397/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02004398 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
4399 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4400 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004401 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02004402 *
4403 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
4404 * the requested function it will return NULL. This is convenient for drivers
4405 * that need to handle optional GPIOs.
4406 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004407struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004408 const char *con_id,
4409 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02004410{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004411 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02004412}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004413EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02004414
Benoit Parrotf625d462015-02-02 11:44:44 -06004415
4416/**
4417 * gpiod_configure_flags - helper function to configure a given GPIO
4418 * @desc: gpio whose value will be assigned
4419 * @con_id: function within the GPIO consumer
Andy Shevchenkofed70262019-04-10 18:39:16 +03004420 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4421 * of_find_gpio() or of_get_gpio_hog()
Benoit Parrotf625d462015-02-02 11:44:44 -06004422 * @dflags: gpiod_flags - optional GPIO initialization flags
4423 *
4424 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
4425 * requested function and/or index, or another IS_ERR() code if an error
4426 * occurred while trying to acquire the GPIO.
4427 */
Andy Shevchenkoc29fd9e2017-05-23 20:03:16 +03004428int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Johan Hovold85b03b32016-07-03 18:32:05 +02004429 unsigned long lflags, enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06004430{
Linus Walleijd377f562019-07-16 11:11:45 +02004431 int ret;
Benoit Parrotf625d462015-02-02 11:44:44 -06004432
Johan Hovold85b03b32016-07-03 18:32:05 +02004433 if (lflags & GPIO_ACTIVE_LOW)
4434 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
Linus Walleijf926dfc2017-09-10 19:26:22 +02004435
Johan Hovold85b03b32016-07-03 18:32:05 +02004436 if (lflags & GPIO_OPEN_DRAIN)
4437 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
Linus Walleijf926dfc2017-09-10 19:26:22 +02004438 else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) {
4439 /*
4440 * This enforces open drain mode from the consumer side.
4441 * This is necessary for some busses like I2C, but the lookup
4442 * should *REALLY* have specified them as open drain in the
4443 * first place, so print a little warning here.
4444 */
4445 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
4446 gpiod_warn(desc,
4447 "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
4448 }
4449
Johan Hovold85b03b32016-07-03 18:32:05 +02004450 if (lflags & GPIO_OPEN_SOURCE)
4451 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Andrew Jefferye10f72b2017-11-30 14:25:24 +10304452
Thomas Petazzonid4499912019-02-07 17:28:58 +01004453 if ((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DOWN)) {
4454 gpiod_err(desc,
4455 "both pull-up and pull-down enabled, invalid configuration\n");
4456 return -EINVAL;
4457 }
4458
4459 if (lflags & GPIO_PULL_UP)
4460 set_bit(FLAG_PULL_UP, &desc->flags);
4461 else if (lflags & GPIO_PULL_DOWN)
4462 set_bit(FLAG_PULL_DOWN, &desc->flags);
4463
Linus Walleijd377f562019-07-16 11:11:45 +02004464 ret = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
4465 if (ret < 0)
4466 return ret;
Johan Hovold85b03b32016-07-03 18:32:05 +02004467
Benoit Parrotf625d462015-02-02 11:44:44 -06004468 /* No particular flag request, return here... */
4469 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
4470 pr_debug("no flags found for %s\n", con_id);
4471 return 0;
4472 }
4473
4474 /* Process flags */
4475 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
Linus Walleijd377f562019-07-16 11:11:45 +02004476 ret = gpiod_direction_output(desc,
Linus Walleijad177312016-11-13 23:02:44 +01004477 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
Benoit Parrotf625d462015-02-02 11:44:44 -06004478 else
Linus Walleijd377f562019-07-16 11:11:45 +02004479 ret = gpiod_direction_input(desc);
Benoit Parrotf625d462015-02-02 11:44:44 -06004480
Linus Walleijd377f562019-07-16 11:11:45 +02004481 return ret;
Benoit Parrotf625d462015-02-02 11:44:44 -06004482}
4483
Thierry Reding29a1f2332014-04-25 17:10:06 +02004484/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004485 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02004486 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004487 * @con_id: function within the GPIO consumer
4488 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004489 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004490 *
4491 * This variant of gpiod_get() allows to access GPIOs other than the first
4492 * defined one for functions that define several GPIOs.
4493 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004494 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
4495 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07004496 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004497 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004498struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004499 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004500 unsigned int idx,
4501 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004502{
Andy Shevchenko2d6c06f2019-04-10 18:39:17 +03004503 unsigned long lookupflags = GPIO_LOOKUP_FLAGS_DEFAULT;
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09004504 struct gpio_desc *desc = NULL;
Linus Walleijd377f562019-07-16 11:11:45 +02004505 int ret;
Linus Walleij7d18f0a2018-01-16 08:29:50 +01004506 /* Maybe we have a device name, maybe not */
4507 const char *devname = dev ? dev_name(dev) : "?";
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004508
4509 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
4510
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01004511 if (dev) {
4512 /* Using device tree? */
4513 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
4514 dev_dbg(dev, "using device tree for GPIO lookup\n");
4515 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
4516 } else if (ACPI_COMPANION(dev)) {
4517 dev_dbg(dev, "using ACPI for GPIO lookup\n");
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +03004518 desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags);
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01004519 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09004520 }
4521
4522 /*
4523 * Either we are not using DT or ACPI, or their lookup did not return
4524 * a result. In that case, use platform lookup as a fallback.
4525 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004526 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04004527 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004528 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004529 }
4530
4531 if (IS_ERR(desc)) {
Wang Dongsheng9d5a1f22018-02-27 00:12:13 -08004532 dev_dbg(dev, "No GPIO consumer %s found\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004533 return desc;
4534 }
4535
Linus Walleij7d18f0a2018-01-16 08:29:50 +01004536 /*
4537 * If a connection label was passed use that, else attempt to use
4538 * the device name as label
4539 */
Linus Walleijd377f562019-07-16 11:11:45 +02004540 ret = gpiod_request(desc, con_id ? con_id : devname);
4541 if (ret < 0) {
4542 if (ret == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE) {
Linus Walleijb0ce7b292018-10-12 14:54:12 +02004543 /*
4544 * This happens when there are several consumers for
4545 * the same GPIO line: we just return here without
4546 * further initialization. It is a bit if a hack.
4547 * This is necessary to support fixed regulators.
4548 *
4549 * FIXME: Make this more sane and safe.
4550 */
4551 dev_info(dev, "nonexclusive access to GPIO for %s\n",
4552 con_id ? con_id : devname);
4553 return desc;
4554 } else {
Linus Walleijd377f562019-07-16 11:11:45 +02004555 return ERR_PTR(ret);
Linus Walleijb0ce7b292018-10-12 14:54:12 +02004556 }
4557 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004558
Linus Walleijd377f562019-07-16 11:11:45 +02004559 ret = gpiod_configure_flags(desc, con_id, lookupflags, flags);
4560 if (ret < 0) {
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004561 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
4562 gpiod_put(desc);
Linus Walleijd377f562019-07-16 11:11:45 +02004563 return ERR_PTR(ret);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004564 }
4565
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004566 return desc;
4567}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004568EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004569
4570/**
Mika Westerberg40b73182014-10-21 13:33:59 +02004571 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
4572 * @fwnode: handle of the firmware node
4573 * @propname: name of the firmware property representing the GPIO
Linus Walleij6392cca2017-12-29 02:07:54 +01004574 * @index: index of the GPIO to obtain for the consumer
Andy Shevchenkoa264d102017-01-09 16:02:28 +02004575 * @dflags: GPIO initialization flags
Thierry Reding950d55f52017-07-24 16:57:22 +02004576 * @label: label to attach to the requested GPIO
Mika Westerberg40b73182014-10-21 13:33:59 +02004577 *
4578 * This function can be used for drivers that get their configuration
Linus Walleij6392cca2017-12-29 02:07:54 +01004579 * from opaque firmware.
Mika Westerberg40b73182014-10-21 13:33:59 +02004580 *
Linus Walleij6392cca2017-12-29 02:07:54 +01004581 * The function properly finds the corresponding GPIO using whatever is the
Mika Westerberg40b73182014-10-21 13:33:59 +02004582 * underlying firmware interface and then makes sure that the GPIO
4583 * descriptor is requested before it is returned to the caller.
4584 *
Thierry Reding950d55f52017-07-24 16:57:22 +02004585 * Returns:
Andy Shevchenkoff213782017-02-28 17:03:12 +02004586 * On successful request the GPIO pin is configured in accordance with
Andy Shevchenkoa264d102017-01-09 16:02:28 +02004587 * provided @dflags.
4588 *
Mika Westerberg40b73182014-10-21 13:33:59 +02004589 * In case of error an ERR_PTR() is returned.
4590 */
4591struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
Boris Brezillon537b94d2017-02-02 14:53:11 +01004592 const char *propname, int index,
Alexander Steinb2987d72017-01-12 17:39:24 +01004593 enum gpiod_flags dflags,
4594 const char *label)
Mika Westerberg40b73182014-10-21 13:33:59 +02004595{
Andy Shevchenko2d6c06f2019-04-10 18:39:17 +03004596 unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
Mika Westerberg40b73182014-10-21 13:33:59 +02004597 struct gpio_desc *desc = ERR_PTR(-ENODEV);
Mika Westerberg40b73182014-10-21 13:33:59 +02004598 int ret;
4599
4600 if (!fwnode)
4601 return ERR_PTR(-EINVAL);
4602
4603 if (is_of_node(fwnode)) {
Linus Walleij6392cca2017-12-29 02:07:54 +01004604 desc = gpiod_get_from_of_node(to_of_node(fwnode),
4605 propname, index,
4606 dflags,
4607 label);
4608 return desc;
Mika Westerberg40b73182014-10-21 13:33:59 +02004609 } else if (is_acpi_node(fwnode)) {
4610 struct acpi_gpio_info info;
4611
Boris Brezillon537b94d2017-02-02 14:53:11 +01004612 desc = acpi_node_get_gpiod(fwnode, propname, index, &info);
Linus Walleij6392cca2017-12-29 02:07:54 +01004613 if (IS_ERR(desc))
4614 return desc;
4615
4616 acpi_gpio_update_gpiod_flags(&dflags, &info);
Andy Shevchenko606be342019-04-10 18:39:20 +03004617 acpi_gpio_update_gpiod_lookup_flags(&lflags, &info);
Mika Westerberg40b73182014-10-21 13:33:59 +02004618 }
4619
Linus Walleij6392cca2017-12-29 02:07:54 +01004620 /* Currently only ACPI takes this path */
Alexander Steinb2987d72017-01-12 17:39:24 +01004621 ret = gpiod_request(desc, label);
Johan Hovold85b03b32016-07-03 18:32:05 +02004622 if (ret)
4623 return ERR_PTR(ret);
4624
Andy Shevchenkoa264d102017-01-09 16:02:28 +02004625 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
4626 if (ret < 0) {
4627 gpiod_put(desc);
4628 return ERR_PTR(ret);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03004629 }
4630
Mika Westerberg40b73182014-10-21 13:33:59 +02004631 return desc;
4632}
4633EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
4634
4635/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02004636 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
4637 * function
4638 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4639 * @con_id: function within the GPIO consumer
4640 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004641 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02004642 *
4643 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
4644 * specified index was assigned to the requested function it will return NULL.
4645 * This is convenient for drivers that need to handle optional GPIOs.
4646 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004647struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02004648 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004649 unsigned int index,
4650 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02004651{
4652 struct gpio_desc *desc;
4653
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004654 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02004655 if (IS_ERR(desc)) {
4656 if (PTR_ERR(desc) == -ENOENT)
4657 return NULL;
4658 }
4659
4660 return desc;
4661}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004662EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02004663
4664/**
Benoit Parrotf625d462015-02-02 11:44:44 -06004665 * gpiod_hog - Hog the specified GPIO desc given the provided flags
4666 * @desc: gpio whose value will be assigned
4667 * @name: gpio line name
Andy Shevchenkofed70262019-04-10 18:39:16 +03004668 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4669 * of_find_gpio() or of_get_gpio_hog()
Benoit Parrotf625d462015-02-02 11:44:44 -06004670 * @dflags: gpiod_flags - optional GPIO initialization flags
4671 */
4672int gpiod_hog(struct gpio_desc *desc, const char *name,
4673 unsigned long lflags, enum gpiod_flags dflags)
4674{
4675 struct gpio_chip *chip;
4676 struct gpio_desc *local_desc;
4677 int hwnum;
Linus Walleijd377f562019-07-16 11:11:45 +02004678 int ret;
Benoit Parrotf625d462015-02-02 11:44:44 -06004679
4680 chip = gpiod_to_chip(desc);
4681 hwnum = gpio_chip_hwgpio(desc);
4682
Linus Walleij5923ea62019-04-26 14:40:18 +02004683 local_desc = gpiochip_request_own_desc(chip, hwnum, name,
4684 lflags, dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -06004685 if (IS_ERR(local_desc)) {
Linus Walleijd377f562019-07-16 11:11:45 +02004686 ret = PTR_ERR(local_desc);
Laxman Dewanganc31a5712016-03-11 19:13:21 +05304687 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
Linus Walleijd377f562019-07-16 11:11:45 +02004688 name, chip->label, hwnum, ret);
4689 return ret;
Benoit Parrotf625d462015-02-02 11:44:44 -06004690 }
4691
Benoit Parrotf625d462015-02-02 11:44:44 -06004692 /* Mark GPIO as hogged so it can be identified and removed later */
4693 set_bit(FLAG_IS_HOGGED, &desc->flags);
4694
4695 pr_info("GPIO line %d (%s) hogged as %s%s\n",
4696 desc_to_gpio(desc), name,
4697 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
4698 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
4699 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
4700
4701 return 0;
4702}
4703
4704/**
4705 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
4706 * @chip: gpio chip to act on
Benoit Parrotf625d462015-02-02 11:44:44 -06004707 */
4708static void gpiochip_free_hogs(struct gpio_chip *chip)
4709{
4710 int id;
4711
4712 for (id = 0; id < chip->ngpio; id++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01004713 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
4714 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
Benoit Parrotf625d462015-02-02 11:44:44 -06004715 }
4716}
4717
4718/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004719 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
4720 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4721 * @con_id: function within the GPIO consumer
4722 * @flags: optional GPIO initialization flags
4723 *
4724 * This function acquires all the GPIOs defined under a given function.
4725 *
4726 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
4727 * no GPIO has been assigned to the requested function, or another IS_ERR()
4728 * code if an error occurred while trying to acquire the GPIOs.
4729 */
4730struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
4731 const char *con_id,
4732 enum gpiod_flags flags)
4733{
4734 struct gpio_desc *desc;
4735 struct gpio_descs *descs;
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004736 struct gpio_array *array_info = NULL;
4737 struct gpio_chip *chip;
4738 int count, bitmap_size;
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004739
4740 count = gpiod_count(dev, con_id);
4741 if (count < 0)
4742 return ERR_PTR(count);
4743
Kees Cookacafe7e2018-05-08 13:45:50 -07004744 descs = kzalloc(struct_size(descs, desc, count), GFP_KERNEL);
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004745 if (!descs)
4746 return ERR_PTR(-ENOMEM);
4747
4748 for (descs->ndescs = 0; descs->ndescs < count; ) {
4749 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
4750 if (IS_ERR(desc)) {
4751 gpiod_put_array(descs);
4752 return ERR_CAST(desc);
4753 }
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004754
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004755 descs->desc[descs->ndescs] = desc;
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004756
4757 chip = gpiod_to_chip(desc);
4758 /*
Janusz Krzysztofikc4c958a2018-09-24 01:53:36 +02004759 * If pin hardware number of array member 0 is also 0, select
4760 * its chip as a candidate for fast bitmap processing path.
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004761 */
Janusz Krzysztofikc4c958a2018-09-24 01:53:36 +02004762 if (descs->ndescs == 0 && gpio_chip_hwgpio(desc) == 0) {
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004763 struct gpio_descs *array;
4764
4765 bitmap_size = BITS_TO_LONGS(chip->ngpio > count ?
4766 chip->ngpio : count);
4767
4768 array = kzalloc(struct_size(descs, desc, count) +
4769 struct_size(array_info, invert_mask,
4770 3 * bitmap_size), GFP_KERNEL);
4771 if (!array) {
4772 gpiod_put_array(descs);
4773 return ERR_PTR(-ENOMEM);
4774 }
4775
4776 memcpy(array, descs,
4777 struct_size(descs, desc, descs->ndescs + 1));
4778 kfree(descs);
4779
4780 descs = array;
4781 array_info = (void *)(descs->desc + count);
4782 array_info->get_mask = array_info->invert_mask +
4783 bitmap_size;
4784 array_info->set_mask = array_info->get_mask +
4785 bitmap_size;
4786
4787 array_info->desc = descs->desc;
4788 array_info->size = count;
4789 array_info->chip = chip;
4790 bitmap_set(array_info->get_mask, descs->ndescs,
4791 count - descs->ndescs);
4792 bitmap_set(array_info->set_mask, descs->ndescs,
4793 count - descs->ndescs);
4794 descs->info = array_info;
4795 }
Janusz Krzysztofikc4c958a2018-09-24 01:53:36 +02004796 /* Unmark array members which don't belong to the 'fast' chip */
4797 if (array_info && array_info->chip != chip) {
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004798 __clear_bit(descs->ndescs, array_info->get_mask);
4799 __clear_bit(descs->ndescs, array_info->set_mask);
Janusz Krzysztofikc4c958a2018-09-24 01:53:36 +02004800 }
4801 /*
4802 * Detect array members which belong to the 'fast' chip
4803 * but their pins are not in hardware order.
4804 */
4805 else if (array_info &&
4806 gpio_chip_hwgpio(desc) != descs->ndescs) {
4807 /*
4808 * Don't use fast path if all array members processed so
4809 * far belong to the same chip as this one but its pin
4810 * hardware number is different from its array index.
4811 */
4812 if (bitmap_full(array_info->get_mask, descs->ndescs)) {
4813 array_info = NULL;
4814 } else {
4815 __clear_bit(descs->ndescs,
4816 array_info->get_mask);
4817 __clear_bit(descs->ndescs,
4818 array_info->set_mask);
4819 }
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004820 } else if (array_info) {
4821 /* Exclude open drain or open source from fast output */
4822 if (gpiochip_line_is_open_drain(chip, descs->ndescs) ||
4823 gpiochip_line_is_open_source(chip, descs->ndescs))
4824 __clear_bit(descs->ndescs,
4825 array_info->set_mask);
4826 /* Identify 'fast' pins which require invertion */
4827 if (gpiod_is_active_low(desc))
4828 __set_bit(descs->ndescs,
4829 array_info->invert_mask);
4830 }
4831
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004832 descs->ndescs++;
4833 }
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004834 if (array_info)
4835 dev_dbg(dev,
4836 "GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n",
4837 array_info->chip->label, array_info->size,
4838 *array_info->get_mask, *array_info->set_mask,
4839 *array_info->invert_mask);
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004840 return descs;
4841}
4842EXPORT_SYMBOL_GPL(gpiod_get_array);
4843
4844/**
4845 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
4846 * function
4847 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4848 * @con_id: function within the GPIO consumer
4849 * @flags: optional GPIO initialization flags
4850 *
4851 * This is equivalent to gpiod_get_array(), except that when no GPIO was
4852 * assigned to the requested function it will return NULL.
4853 */
4854struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
4855 const char *con_id,
4856 enum gpiod_flags flags)
4857{
4858 struct gpio_descs *descs;
4859
4860 descs = gpiod_get_array(dev, con_id, flags);
4861 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
4862 return NULL;
4863
4864 return descs;
4865}
4866EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
4867
4868/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004869 * gpiod_put - dispose of a GPIO descriptor
4870 * @desc: GPIO descriptor to dispose of
4871 *
4872 * No descriptor can be used after gpiod_put() has been called on it.
4873 */
4874void gpiod_put(struct gpio_desc *desc)
4875{
Andy Shevchenko1d7765b2019-03-26 17:21:14 +02004876 if (desc)
4877 gpiod_free(desc);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004878}
4879EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08004880
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004881/**
4882 * gpiod_put_array - dispose of multiple GPIO descriptors
4883 * @descs: struct gpio_descs containing an array of descriptors
4884 */
4885void gpiod_put_array(struct gpio_descs *descs)
4886{
4887 unsigned int i;
4888
4889 for (i = 0; i < descs->ndescs; i++)
4890 gpiod_put(descs->desc[i]);
4891
4892 kfree(descs);
4893}
4894EXPORT_SYMBOL_GPL(gpiod_put_array);
4895
Linus Walleij3c702e92015-10-21 15:29:53 +02004896static int __init gpiolib_dev_init(void)
4897{
4898 int ret;
4899
4900 /* Register GPIO sysfs bus */
Andy Shevchenkob1911712018-07-03 03:39:03 +03004901 ret = bus_register(&gpio_bus_type);
Linus Walleij3c702e92015-10-21 15:29:53 +02004902 if (ret < 0) {
4903 pr_err("gpiolib: could not register GPIO bus type\n");
4904 return ret;
4905 }
4906
4907 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
4908 if (ret < 0) {
4909 pr_err("gpiolib: failed to allocate char dev region\n");
4910 bus_unregister(&gpio_bus_type);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07004911 } else {
4912 gpiolib_initialized = true;
4913 gpiochip_setup_devs();
Linus Walleij3c702e92015-10-21 15:29:53 +02004914 }
4915 return ret;
4916}
4917core_initcall(gpiolib_dev_init);
4918
David Brownelld2876d02008-02-04 22:28:20 -08004919#ifdef CONFIG_DEBUG_FS
4920
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004921static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
David Brownelld2876d02008-02-04 22:28:20 -08004922{
4923 unsigned i;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004924 struct gpio_chip *chip = gdev->chip;
4925 unsigned gpio = gdev->base;
4926 struct gpio_desc *gdesc = &gdev->descs[0];
Linus Walleij90fd2272018-10-01 23:06:17 +02004927 bool is_out;
4928 bool is_irq;
4929 bool active_low;
David Brownelld2876d02008-02-04 22:28:20 -08004930
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004931 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02004932 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
4933 if (gdesc->name) {
4934 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
4935 gpio, gdesc->name);
4936 }
David Brownelld2876d02008-02-04 22:28:20 -08004937 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02004938 }
David Brownelld2876d02008-02-04 22:28:20 -08004939
Alexandre Courbot372e7222013-02-03 01:29:29 +09004940 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08004941 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02004942 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Linus Walleij90fd2272018-10-01 23:06:17 +02004943 active_low = test_bit(FLAG_ACTIVE_LOW, &gdesc->flags);
4944 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s%s",
Markus Pargmannced433e2015-08-14 16:11:02 +02004945 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08004946 is_out ? "out" : "in ",
Andy Shevchenko1c22a252018-07-12 20:36:42 +03004947 chip->get ? (chip->get(chip, i) ? "hi" : "lo") : "? ",
Linus Walleij90fd2272018-10-01 23:06:17 +02004948 is_irq ? "IRQ " : "",
4949 active_low ? "ACTIVE LOW" : "");
David Brownelld2876d02008-02-04 22:28:20 -08004950 seq_printf(s, "\n");
4951 }
4952}
4953
Thierry Redingf9c4a312012-04-12 13:26:01 +02004954static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08004955{
Grant Likely362432a2013-02-09 09:41:49 +00004956 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02004957 struct gpio_device *gdev = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09004958 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02004959
4960 s->private = "";
4961
Grant Likely362432a2013-02-09 09:41:49 +00004962 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02004963 list_for_each_entry(gdev, &gpio_devices, list)
Grant Likely362432a2013-02-09 09:41:49 +00004964 if (index-- == 0) {
4965 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02004966 return gdev;
Grant Likely362432a2013-02-09 09:41:49 +00004967 }
4968 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09004969
4970 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02004971}
4972
4973static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
4974{
Grant Likely362432a2013-02-09 09:41:49 +00004975 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02004976 struct gpio_device *gdev = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02004977 void *ret = NULL;
4978
Grant Likely362432a2013-02-09 09:41:49 +00004979 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02004980 if (list_is_last(&gdev->list, &gpio_devices))
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09004981 ret = NULL;
4982 else
Linus Walleijff2b1352015-10-20 11:10:38 +02004983 ret = list_entry(gdev->list.next, struct gpio_device, list);
Grant Likely362432a2013-02-09 09:41:49 +00004984 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02004985
4986 s->private = "\n";
4987 ++*pos;
4988
4989 return ret;
4990}
4991
4992static void gpiolib_seq_stop(struct seq_file *s, void *v)
4993{
4994}
4995
4996static int gpiolib_seq_show(struct seq_file *s, void *v)
4997{
Linus Walleijff2b1352015-10-20 11:10:38 +02004998 struct gpio_device *gdev = v;
4999 struct gpio_chip *chip = gdev->chip;
5000 struct device *parent;
Thierry Redingf9c4a312012-04-12 13:26:01 +02005001
Linus Walleijff2b1352015-10-20 11:10:38 +02005002 if (!chip) {
5003 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
5004 dev_name(&gdev->dev));
5005 return 0;
5006 }
5007
5008 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
5009 dev_name(&gdev->dev),
Linus Walleijfdeb8e12016-02-10 10:57:36 +01005010 gdev->base, gdev->base + gdev->ngpio - 1);
Linus Walleijff2b1352015-10-20 11:10:38 +02005011 parent = chip->parent;
5012 if (parent)
5013 seq_printf(s, ", parent: %s/%s",
5014 parent->bus ? parent->bus->name : "no-bus",
5015 dev_name(parent));
Thierry Redingf9c4a312012-04-12 13:26:01 +02005016 if (chip->label)
5017 seq_printf(s, ", %s", chip->label);
5018 if (chip->can_sleep)
5019 seq_printf(s, ", can sleep");
5020 seq_printf(s, ":\n");
5021
5022 if (chip->dbg_show)
5023 chip->dbg_show(s, chip);
5024 else
Linus Walleijfdeb8e12016-02-10 10:57:36 +01005025 gpiolib_dbg_show(s, gdev);
Thierry Redingf9c4a312012-04-12 13:26:01 +02005026
David Brownelld2876d02008-02-04 22:28:20 -08005027 return 0;
5028}
5029
Thierry Redingf9c4a312012-04-12 13:26:01 +02005030static const struct seq_operations gpiolib_seq_ops = {
5031 .start = gpiolib_seq_start,
5032 .next = gpiolib_seq_next,
5033 .stop = gpiolib_seq_stop,
5034 .show = gpiolib_seq_show,
5035};
5036
David Brownelld2876d02008-02-04 22:28:20 -08005037static int gpiolib_open(struct inode *inode, struct file *file)
5038{
Thierry Redingf9c4a312012-04-12 13:26:01 +02005039 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08005040}
5041
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005042static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02005043 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08005044 .open = gpiolib_open,
5045 .read = seq_read,
5046 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02005047 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08005048};
5049
5050static int __init gpiolib_debugfs_init(void)
5051{
5052 /* /sys/kernel/debug/gpio */
Greg Kroah-Hartmanacc68b02019-06-18 17:50:45 +02005053 debugfs_create_file("gpio", S_IFREG | S_IRUGO, NULL, NULL,
5054 &gpiolib_operations);
David Brownelld2876d02008-02-04 22:28:20 -08005055 return 0;
5056}
5057subsys_initcall(gpiolib_debugfs_init);
5058
5059#endif /* DEBUG_FS */