blob: 8acb0c96fa4c2abfe381c133a30d1ee53b94218f [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:
Mauro Carvalho Chehab35c6cfb2020-03-17 15:54:21 +0100139 * A pointer to the GPIO descriptor or ``ERR_PTR(-EINVAL)`` if no GPIO exists
Thierry Reding950d55f52017-07-24 16:57:22 +0200140 * 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,
Bartosz Golaszewski06863622019-12-24 13:06:59 +0100143 unsigned int 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
Russell King256efae2019-12-07 16:20:18 +0000223 /*
224 * Open drain emulation using input mode may incorrectly report
225 * input here, fix that up.
226 */
227 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) &&
228 test_bit(FLAG_IS_OUT, &desc->flags))
229 return 0;
230
Mathias Nyman80b0a602012-10-24 17:25:27 +0300231 if (!chip->get_direction)
Wolfram Sangd0121b82018-07-11 18:33:19 +0200232 return -ENOTSUPP;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300233
Linus Walleijd377f562019-07-16 11:11:45 +0200234 ret = chip->get_direction(chip, offset);
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +0200235 if (ret < 0)
236 return ret;
237
238 /* GPIOF_DIR_IN or other positive, otherwise GPIOF_DIR_OUT */
239 if (ret > 0)
Linus Walleijd377f562019-07-16 11:11:45 +0200240 ret = 1;
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +0200241
242 assign_bit(FLAG_IS_OUT, &desc->flags, !ret);
243
Linus Walleijd377f562019-07-16 11:11:45 +0200244 return ret;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300245}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700246EXPORT_SYMBOL_GPL(gpiod_get_direction);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300247
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900248/*
249 * Add a new chip to the global chips list, keeping the list of chips sorted
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800250 * by range(means [base, base + ngpio - 1]) order.
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900251 *
252 * Return -EBUSY if the new chip overlaps with some other chip's integer
253 * space.
254 */
Linus Walleijff2b1352015-10-20 11:10:38 +0200255static int gpiodev_add_to_list(struct gpio_device *gdev)
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900256{
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800257 struct gpio_device *prev, *next;
Linus Walleijff2b1352015-10-20 11:10:38 +0200258
259 if (list_empty(&gpio_devices)) {
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800260 /* initial entry in list */
Linus Walleijff2b1352015-10-20 11:10:38 +0200261 list_add_tail(&gdev->list, &gpio_devices);
Sudip Mukherjeee28ecca2015-12-27 19:06:50 +0530262 return 0;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900263 }
264
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800265 next = list_entry(gpio_devices.next, struct gpio_device, list);
266 if (gdev->base + gdev->ngpio <= next->base) {
267 /* add before first entry */
268 list_add(&gdev->list, &gpio_devices);
Julien Grossholtz96098df2016-01-07 16:46:45 -0500269 return 0;
270 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900271
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800272 prev = list_entry(gpio_devices.prev, struct gpio_device, list);
273 if (prev->base + prev->ngpio <= gdev->base) {
274 /* add behind last entry */
275 list_add_tail(&gdev->list, &gpio_devices);
276 return 0;
277 }
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800278
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800279 list_for_each_entry_safe(prev, next, &gpio_devices, list) {
280 /* at the end of the list */
281 if (&next->list == &gpio_devices)
282 break;
283
284 /* add between prev and next */
285 if (prev->base + prev->ngpio <= gdev->base
286 && gdev->base + gdev->ngpio <= next->base) {
287 list_add(&gdev->list, &prev->list);
288 return 0;
289 }
290 }
291
292 dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n");
293 return -EBUSY;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900294}
295
Thierry Reding950d55f52017-07-24 16:57:22 +0200296/*
Linus Walleijf881bab02015-09-23 16:20:43 -0700297 * Convert a GPIO name to its descriptor
298 */
299static struct gpio_desc *gpio_name_to_desc(const char * const name)
300{
Linus Walleijff2b1352015-10-20 11:10:38 +0200301 struct gpio_device *gdev;
Linus Walleijf881bab02015-09-23 16:20:43 -0700302 unsigned long flags;
303
Michał Mirosławee203bb2020-03-15 17:34:34 +0100304 if (!name)
305 return NULL;
306
Linus Walleijf881bab02015-09-23 16:20:43 -0700307 spin_lock_irqsave(&gpio_lock, flags);
308
Linus Walleijff2b1352015-10-20 11:10:38 +0200309 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijf881bab02015-09-23 16:20:43 -0700310 int i;
311
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100312 for (i = 0; i != gdev->ngpio; ++i) {
313 struct gpio_desc *desc = &gdev->descs[i];
Linus Walleijf881bab02015-09-23 16:20:43 -0700314
Michał Mirosławee203bb2020-03-15 17:34:34 +0100315 if (!desc->name)
Linus Walleijf881bab02015-09-23 16:20:43 -0700316 continue;
317
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100318 if (!strcmp(desc->name, name)) {
Linus Walleijf881bab02015-09-23 16:20:43 -0700319 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100320 return desc;
Linus Walleijf881bab02015-09-23 16:20:43 -0700321 }
322 }
323 }
324
325 spin_unlock_irqrestore(&gpio_lock, flags);
326
327 return NULL;
328}
329
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200330/*
331 * Takes the names from gc->names and checks if they are all unique. If they
332 * are, they are assigned to their gpio descriptors.
333 *
Bamvor Jian Zhanged379152015-11-14 16:43:20 +0800334 * Warning if one of the names is already used for a different GPIO.
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200335 */
336static int gpiochip_set_desc_names(struct gpio_chip *gc)
337{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100338 struct gpio_device *gdev = gc->gpiodev;
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200339 int i;
340
341 if (!gc->names)
342 return 0;
343
344 /* First check all names if they are unique */
345 for (i = 0; i != gc->ngpio; ++i) {
346 struct gpio_desc *gpio;
347
348 gpio = gpio_name_to_desc(gc->names[i]);
Linus Walleijf881bab02015-09-23 16:20:43 -0700349 if (gpio)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100350 dev_warn(&gdev->dev,
Linus Walleij34ffd852015-10-20 11:31:54 +0200351 "Detected name collision for GPIO name '%s'\n",
Linus Walleijf881bab02015-09-23 16:20:43 -0700352 gc->names[i]);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200353 }
354
355 /* Then add all names to the GPIO descriptors */
356 for (i = 0; i != gc->ngpio; ++i)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100357 gdev->descs[i].name = gc->names[i];
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200358
359 return 0;
360}
361
Stephen Boyde4371f6e2018-03-23 09:34:50 -0700362static unsigned long *gpiochip_allocate_mask(struct gpio_chip *chip)
363{
364 unsigned long *p;
365
Masahiro Yamada7bdbd1ec2019-07-18 15:51:01 +0900366 p = bitmap_alloc(chip->ngpio, GFP_KERNEL);
Stephen Boyde4371f6e2018-03-23 09:34:50 -0700367 if (!p)
368 return NULL;
369
370 /* Assume by default all GPIOs are valid */
371 bitmap_fill(p, chip->ngpio);
372
373 return p;
374}
375
Linus Walleijf626d6d2019-07-17 09:10:01 +0200376static int gpiochip_alloc_valid_mask(struct gpio_chip *gc)
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700377{
Linus Walleijeb1e8bd2019-08-19 11:30:58 +0200378 if (!(of_gpio_need_valid_mask(gc) || gc->init_valid_mask))
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700379 return 0;
380
Linus Walleijf626d6d2019-07-17 09:10:01 +0200381 gc->valid_mask = gpiochip_allocate_mask(gc);
382 if (!gc->valid_mask)
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700383 return -ENOMEM;
384
385 return 0;
386}
387
Linus Walleijc9fc5af2019-08-19 10:49:04 +0200388static int gpiochip_init_valid_mask(struct gpio_chip *gc)
Ricardo Ribalda Delgadof8ec92a2018-10-05 08:52:58 +0200389{
Linus Walleijc9fc5af2019-08-19 10:49:04 +0200390 if (gc->init_valid_mask)
391 return gc->init_valid_mask(gc,
392 gc->valid_mask,
393 gc->ngpio);
Ricardo Ribalda Delgadof8ec92a2018-10-05 08:52:58 +0200394
395 return 0;
396}
397
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700398static void gpiochip_free_valid_mask(struct gpio_chip *gpiochip)
399{
Masahiro Yamada7bdbd1ec2019-07-18 15:51:01 +0900400 bitmap_free(gpiochip->valid_mask);
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700401 gpiochip->valid_mask = NULL;
402}
403
Andy Shevchenkob056ca12019-11-04 18:09:39 +0200404static int gpiochip_add_pin_ranges(struct gpio_chip *gc)
405{
406 if (gc->add_pin_ranges)
407 return gc->add_pin_ranges(gc);
408
409 return 0;
410}
411
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700412bool gpiochip_line_is_valid(const struct gpio_chip *gpiochip,
413 unsigned int offset)
414{
415 /* No mask means all valid */
416 if (likely(!gpiochip->valid_mask))
417 return true;
418 return test_bit(offset, gpiochip->valid_mask);
419}
420EXPORT_SYMBOL_GPL(gpiochip_line_is_valid);
421
Linus Walleijd7c51b42016-04-26 10:35:29 +0200422/*
423 * GPIO line handle management
424 */
425
426/**
427 * struct linehandle_state - contains the state of a userspace handle
428 * @gdev: the GPIO device the handle pertains to
429 * @label: consumer label used to tag descriptors
430 * @descs: the GPIO descriptors held by this handle
431 * @numdescs: the number of descriptors held in the descs array
432 */
433struct linehandle_state {
434 struct gpio_device *gdev;
435 const char *label;
436 struct gpio_desc *descs[GPIOHANDLES_MAX];
437 u32 numdescs;
438};
439
Lars-Peter Clausene3e847c2016-10-18 16:54:05 +0200440#define GPIOHANDLE_REQUEST_VALID_FLAGS \
441 (GPIOHANDLE_REQUEST_INPUT | \
442 GPIOHANDLE_REQUEST_OUTPUT | \
443 GPIOHANDLE_REQUEST_ACTIVE_LOW | \
Drew Fustini9225d512019-11-05 10:04:23 +0800444 GPIOHANDLE_REQUEST_BIAS_PULL_UP | \
445 GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | \
Kent Gibson2148ad72019-11-05 10:04:25 +0800446 GPIOHANDLE_REQUEST_BIAS_DISABLE | \
Lars-Peter Clausene3e847c2016-10-18 16:54:05 +0200447 GPIOHANDLE_REQUEST_OPEN_DRAIN | \
448 GPIOHANDLE_REQUEST_OPEN_SOURCE)
449
Kent Gibsonb043ed72019-11-05 10:04:28 +0800450static int linehandle_validate_flags(u32 flags)
451{
452 /* Return an error if an unknown flag is set */
453 if (flags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
454 return -EINVAL;
455
456 /*
457 * Do not allow both INPUT & OUTPUT flags to be set as they are
458 * contradictory.
459 */
460 if ((flags & GPIOHANDLE_REQUEST_INPUT) &&
461 (flags & GPIOHANDLE_REQUEST_OUTPUT))
462 return -EINVAL;
463
464 /*
465 * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
466 * the hardware actually supports enabling both at the same time the
467 * electrical result would be disastrous.
468 */
469 if ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) &&
470 (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
471 return -EINVAL;
472
473 /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
474 if (!(flags & GPIOHANDLE_REQUEST_OUTPUT) &&
475 ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
476 (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
477 return -EINVAL;
478
479 /* Bias flags only allowed for input or output mode. */
480 if (!((flags & GPIOHANDLE_REQUEST_INPUT) ||
481 (flags & GPIOHANDLE_REQUEST_OUTPUT)) &&
482 ((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) ||
483 (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP) ||
484 (flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN)))
485 return -EINVAL;
486
487 /* Only one bias flag can be set. */
488 if (((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
489 (flags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
490 GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
491 ((flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
492 (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
493 return -EINVAL;
494
495 return 0;
496}
497
Kent Gibsone588bb12019-11-05 10:04:29 +0800498static long linehandle_set_config(struct linehandle_state *lh,
499 void __user *ip)
500{
501 struct gpiohandle_config gcnf;
502 struct gpio_desc *desc;
503 int i, ret;
504 u32 lflags;
505 unsigned long *flagsp;
506
507 if (copy_from_user(&gcnf, ip, sizeof(gcnf)))
508 return -EFAULT;
509
510 lflags = gcnf.flags;
511 ret = linehandle_validate_flags(lflags);
512 if (ret)
513 return ret;
514
515 for (i = 0; i < lh->numdescs; i++) {
516 desc = lh->descs[i];
517 flagsp = &desc->flags;
518
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +0200519 assign_bit(FLAG_ACTIVE_LOW, flagsp,
Kent Gibsone588bb12019-11-05 10:04:29 +0800520 lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW);
521
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +0200522 assign_bit(FLAG_OPEN_DRAIN, flagsp,
Kent Gibsone588bb12019-11-05 10:04:29 +0800523 lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN);
524
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +0200525 assign_bit(FLAG_OPEN_SOURCE, flagsp,
Kent Gibsone588bb12019-11-05 10:04:29 +0800526 lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE);
527
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +0200528 assign_bit(FLAG_PULL_UP, flagsp,
Kent Gibsone588bb12019-11-05 10:04:29 +0800529 lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP);
530
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +0200531 assign_bit(FLAG_PULL_DOWN, flagsp,
Kent Gibsone588bb12019-11-05 10:04:29 +0800532 lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN);
533
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +0200534 assign_bit(FLAG_BIAS_DISABLE, flagsp,
Kent Gibsone588bb12019-11-05 10:04:29 +0800535 lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE);
536
537 /*
538 * Lines have to be requested explicitly for input
539 * or output, else the line will be treated "as is".
540 */
541 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
542 int val = !!gcnf.default_values[i];
543
544 ret = gpiod_direction_output(desc, val);
545 if (ret)
546 return ret;
547 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
548 ret = gpiod_direction_input(desc);
549 if (ret)
550 return ret;
551 }
Bartosz Golaszewski51c10642019-11-22 15:19:21 +0100552
553 atomic_notifier_call_chain(&desc->gdev->notifier,
554 GPIOLINE_CHANGED_CONFIG, desc);
Kent Gibsone588bb12019-11-05 10:04:29 +0800555 }
556 return 0;
557}
558
Linus Walleijd7c51b42016-04-26 10:35:29 +0200559static long linehandle_ioctl(struct file *filep, unsigned int cmd,
560 unsigned long arg)
561{
562 struct linehandle_state *lh = filep->private_data;
563 void __user *ip = (void __user *)arg;
564 struct gpiohandle_data ghd;
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +0200565 DECLARE_BITMAP(vals, GPIOHANDLES_MAX);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200566 int i;
567
568 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
Bartosz Golaszewski2b955b32018-07-16 10:34:24 +0200569 /* NOTE: It's ok to read values of output lines. */
Lukas Wunnereec1d562017-10-12 12:40:10 +0200570 int ret = gpiod_get_array_value_complex(false,
571 true,
572 lh->numdescs,
573 lh->descs,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +0200574 NULL,
Lukas Wunnereec1d562017-10-12 12:40:10 +0200575 vals);
576 if (ret)
577 return ret;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200578
Lars-Peter Clausen3eded5d2016-10-18 16:54:02 +0200579 memset(&ghd, 0, sizeof(ghd));
Lukas Wunnereec1d562017-10-12 12:40:10 +0200580 for (i = 0; i < lh->numdescs; i++)
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +0200581 ghd.values[i] = test_bit(i, vals);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200582
583 if (copy_to_user(ip, &ghd, sizeof(ghd)))
584 return -EFAULT;
585
586 return 0;
587 } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
Bartosz Golaszewskie5332d52018-07-16 10:34:23 +0200588 /*
589 * All line descriptors were created at once with the same
590 * flags so just check if the first one is really output.
591 */
592 if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags))
593 return -EPERM;
594
Linus Walleijd7c51b42016-04-26 10:35:29 +0200595 if (copy_from_user(&ghd, ip, sizeof(ghd)))
596 return -EFAULT;
597
598 /* Clamp all values to [0,1] */
599 for (i = 0; i < lh->numdescs; i++)
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +0200600 __assign_bit(i, vals, ghd.values[i]);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200601
602 /* Reuse the array setting function */
Laura Abbott30277432018-05-21 10:57:07 -0700603 return gpiod_set_array_value_complex(false,
Linus Walleijd7c51b42016-04-26 10:35:29 +0200604 true,
605 lh->numdescs,
606 lh->descs,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +0200607 NULL,
Linus Walleijd7c51b42016-04-26 10:35:29 +0200608 vals);
Kent Gibsone588bb12019-11-05 10:04:29 +0800609 } else if (cmd == GPIOHANDLE_SET_CONFIG_IOCTL) {
610 return linehandle_set_config(lh, ip);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200611 }
612 return -EINVAL;
613}
614
615#ifdef CONFIG_COMPAT
616static long linehandle_ioctl_compat(struct file *filep, unsigned int cmd,
617 unsigned long arg)
618{
619 return linehandle_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
620}
621#endif
622
623static int linehandle_release(struct inode *inode, struct file *filep)
624{
625 struct linehandle_state *lh = filep->private_data;
626 struct gpio_device *gdev = lh->gdev;
627 int i;
628
629 for (i = 0; i < lh->numdescs; i++)
630 gpiod_free(lh->descs[i]);
631 kfree(lh->label);
632 kfree(lh);
633 put_device(&gdev->dev);
634 return 0;
635}
636
637static const struct file_operations linehandle_fileops = {
638 .release = linehandle_release,
639 .owner = THIS_MODULE,
640 .llseek = noop_llseek,
641 .unlocked_ioctl = linehandle_ioctl,
642#ifdef CONFIG_COMPAT
643 .compat_ioctl = linehandle_ioctl_compat,
644#endif
645};
646
647static int linehandle_create(struct gpio_device *gdev, void __user *ip)
648{
649 struct gpiohandle_request handlereq;
650 struct linehandle_state *lh;
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200651 struct file *file;
Timur Tabiab3dbcf2018-03-29 13:29:12 -0500652 int fd, i, count = 0, ret;
Bartosz Golaszewski418ee8e2017-10-16 11:32:29 +0200653 u32 lflags;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200654
655 if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
656 return -EFAULT;
657 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
658 return -EINVAL;
659
Bartosz Golaszewski418ee8e2017-10-16 11:32:29 +0200660 lflags = handlereq.flags;
661
Kent Gibsonb043ed72019-11-05 10:04:28 +0800662 ret = linehandle_validate_flags(lflags);
663 if (ret)
664 return ret;
Kent Gibson2148ad72019-11-05 10:04:25 +0800665
Linus Walleijd7c51b42016-04-26 10:35:29 +0200666 lh = kzalloc(sizeof(*lh), GFP_KERNEL);
667 if (!lh)
668 return -ENOMEM;
669 lh->gdev = gdev;
670 get_device(&gdev->dev);
671
672 /* Make sure this is terminated */
673 handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
674 if (strlen(handlereq.consumer_label)) {
675 lh->label = kstrdup(handlereq.consumer_label,
676 GFP_KERNEL);
677 if (!lh->label) {
678 ret = -ENOMEM;
679 goto out_free_lh;
680 }
681 }
682
683 /* Request each GPIO */
684 for (i = 0; i < handlereq.lines; i++) {
685 u32 offset = handlereq.lineoffsets[i];
Bartosz Golaszewski0f41dab2019-12-24 13:07:00 +0100686 struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200687
Bartosz Golaszewski0f41dab2019-12-24 13:07:00 +0100688 if (IS_ERR(desc)) {
689 ret = PTR_ERR(desc);
Lars-Peter Clausene405f9f2016-10-18 16:54:01 +0200690 goto out_free_descs;
691 }
692
Linus Walleijd7c51b42016-04-26 10:35:29 +0200693 ret = gpiod_request(desc, lh->label);
694 if (ret)
695 goto out_free_descs;
696 lh->descs[i] = desc;
Ricardo Ribalda Delgado19a4fbf2018-09-13 15:37:04 +0200697 count = i + 1;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200698
699 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
700 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
701 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
702 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
703 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
704 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Kent Gibson2148ad72019-11-05 10:04:25 +0800705 if (lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE)
706 set_bit(FLAG_BIAS_DISABLE, &desc->flags);
Drew Fustini9225d512019-11-05 10:04:23 +0800707 if (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN)
708 set_bit(FLAG_PULL_DOWN, &desc->flags);
709 if (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)
710 set_bit(FLAG_PULL_UP, &desc->flags);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200711
Andrew Jefferye10f72b2017-11-30 14:25:24 +1030712 ret = gpiod_set_transitory(desc, false);
713 if (ret < 0)
714 goto out_free_descs;
715
Linus Walleijd7c51b42016-04-26 10:35:29 +0200716 /*
717 * Lines have to be requested explicitly for input
718 * or output, else the line will be treated "as is".
719 */
720 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
721 int val = !!handlereq.default_values[i];
722
723 ret = gpiod_direction_output(desc, val);
724 if (ret)
725 goto out_free_descs;
726 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
727 ret = gpiod_direction_input(desc);
728 if (ret)
729 goto out_free_descs;
730 }
731 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
732 offset);
733 }
Linus Walleije2f608b2016-06-18 10:56:43 +0200734 /* Let i point at the last handle */
735 i--;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200736 lh->numdescs = handlereq.lines;
737
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200738 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200739 if (fd < 0) {
740 ret = fd;
741 goto out_free_descs;
742 }
743
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200744 file = anon_inode_getfile("gpio-linehandle",
745 &linehandle_fileops,
746 lh,
747 O_RDONLY | O_CLOEXEC);
748 if (IS_ERR(file)) {
749 ret = PTR_ERR(file);
750 goto out_put_unused_fd;
751 }
752
Linus Walleijd7c51b42016-04-26 10:35:29 +0200753 handlereq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200754 if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200755 /*
756 * fput() will trigger the release() callback, so do not go onto
757 * the regular error cleanup path here.
758 */
759 fput(file);
760 put_unused_fd(fd);
761 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +0200762 }
Linus Walleijd7c51b42016-04-26 10:35:29 +0200763
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200764 fd_install(fd, file);
765
Linus Walleijd7c51b42016-04-26 10:35:29 +0200766 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
767 lh->numdescs);
768
769 return 0;
770
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200771out_put_unused_fd:
772 put_unused_fd(fd);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200773out_free_descs:
Timur Tabiab3dbcf2018-03-29 13:29:12 -0500774 for (i = 0; i < count; i++)
Linus Walleijd7c51b42016-04-26 10:35:29 +0200775 gpiod_free(lh->descs[i]);
776 kfree(lh->label);
777out_free_lh:
778 kfree(lh);
779 put_device(&gdev->dev);
780 return ret;
781}
782
Linus Walleij61f922d2016-06-02 11:30:15 +0200783/*
784 * GPIO line event management
785 */
786
787/**
788 * struct lineevent_state - contains the state of a userspace event
789 * @gdev: the GPIO device the event pertains to
790 * @label: consumer label used to tag descriptors
791 * @desc: the GPIO descriptor held by this event
792 * @eflags: the event flags this line was requested with
793 * @irq: the interrupt that trigger in response to events on this GPIO
794 * @wait: wait queue that handles blocking reads of events
795 * @events: KFIFO for the GPIO events
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100796 * @timestamp: cache for the timestamp storing it between hardirq
797 * and IRQ thread, used to bring the timestamp close to the actual
798 * event
Linus Walleij61f922d2016-06-02 11:30:15 +0200799 */
800struct lineevent_state {
801 struct gpio_device *gdev;
802 const char *label;
803 struct gpio_desc *desc;
804 u32 eflags;
805 int irq;
806 wait_queue_head_t wait;
807 DECLARE_KFIFO(events, struct gpioevent_data, 16);
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100808 u64 timestamp;
Linus Walleij61f922d2016-06-02 11:30:15 +0200809};
810
Lars-Peter Clausenac7dbb92016-10-18 16:54:06 +0200811#define GPIOEVENT_REQUEST_VALID_FLAGS \
812 (GPIOEVENT_REQUEST_RISING_EDGE | \
813 GPIOEVENT_REQUEST_FALLING_EDGE)
814
Al Viroafc9a422017-07-03 06:39:46 -0400815static __poll_t lineevent_poll(struct file *filep,
Linus Walleij61f922d2016-06-02 11:30:15 +0200816 struct poll_table_struct *wait)
817{
818 struct lineevent_state *le = filep->private_data;
Al Viroafc9a422017-07-03 06:39:46 -0400819 __poll_t events = 0;
Linus Walleij61f922d2016-06-02 11:30:15 +0200820
821 poll_wait(filep, &le->wait, wait);
822
Bartosz Golaszewskidea9c802019-11-27 12:19:21 +0100823 if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800824 events = EPOLLIN | EPOLLRDNORM;
Linus Walleij61f922d2016-06-02 11:30:15 +0200825
826 return events;
827}
828
829
830static ssize_t lineevent_read(struct file *filep,
831 char __user *buf,
832 size_t count,
833 loff_t *f_ps)
834{
835 struct lineevent_state *le = filep->private_data;
Andy Shevchenkodf2cd582020-02-13 19:09:04 +0200836 struct gpioevent_data ge;
Bartosz Golaszewskidea9c802019-11-27 12:19:21 +0100837 ssize_t bytes_read = 0;
Linus Walleij61f922d2016-06-02 11:30:15 +0200838 int ret;
839
Andy Shevchenkodf2cd582020-02-13 19:09:04 +0200840 if (count < sizeof(ge))
Linus Walleij61f922d2016-06-02 11:30:15 +0200841 return -EINVAL;
842
843 do {
Bartosz Golaszewskidea9c802019-11-27 12:19:21 +0100844 spin_lock(&le->wait.lock);
Linus Walleij61f922d2016-06-02 11:30:15 +0200845 if (kfifo_is_empty(&le->events)) {
Bartosz Golaszewskidea9c802019-11-27 12:19:21 +0100846 if (bytes_read) {
847 spin_unlock(&le->wait.lock);
848 return bytes_read;
849 }
Linus Walleij61f922d2016-06-02 11:30:15 +0200850
Bartosz Golaszewskidea9c802019-11-27 12:19:21 +0100851 if (filep->f_flags & O_NONBLOCK) {
852 spin_unlock(&le->wait.lock);
853 return -EAGAIN;
854 }
855
856 ret = wait_event_interruptible_locked(le->wait,
Linus Walleij61f922d2016-06-02 11:30:15 +0200857 !kfifo_is_empty(&le->events));
Bartosz Golaszewskidea9c802019-11-27 12:19:21 +0100858 if (ret) {
859 spin_unlock(&le->wait.lock);
Linus Walleij61f922d2016-06-02 11:30:15 +0200860 return ret;
Bartosz Golaszewskidea9c802019-11-27 12:19:21 +0100861 }
Linus Walleij61f922d2016-06-02 11:30:15 +0200862 }
863
Andy Shevchenkodf2cd582020-02-13 19:09:04 +0200864 ret = kfifo_out(&le->events, &ge, 1);
Bartosz Golaszewskidea9c802019-11-27 12:19:21 +0100865 spin_unlock(&le->wait.lock);
866 if (ret != 1) {
867 /*
868 * This should never happen - we were holding the lock
869 * from the moment we learned the fifo is no longer
870 * empty until now.
871 */
872 ret = -EIO;
873 break;
874 }
Linus Walleij61f922d2016-06-02 11:30:15 +0200875
Andy Shevchenkodf2cd582020-02-13 19:09:04 +0200876 if (copy_to_user(buf + bytes_read, &ge, sizeof(ge)))
Bartosz Golaszewskidea9c802019-11-27 12:19:21 +0100877 return -EFAULT;
Andy Shevchenkodf2cd582020-02-13 19:09:04 +0200878 bytes_read += sizeof(ge);
879 } while (count >= bytes_read + sizeof(ge));
Linus Walleij61f922d2016-06-02 11:30:15 +0200880
Bartosz Golaszewskidea9c802019-11-27 12:19:21 +0100881 return bytes_read;
Linus Walleij61f922d2016-06-02 11:30:15 +0200882}
883
884static int lineevent_release(struct inode *inode, struct file *filep)
885{
886 struct lineevent_state *le = filep->private_data;
887 struct gpio_device *gdev = le->gdev;
888
889 free_irq(le->irq, le);
890 gpiod_free(le->desc);
891 kfree(le->label);
892 kfree(le);
893 put_device(&gdev->dev);
894 return 0;
895}
896
897static long lineevent_ioctl(struct file *filep, unsigned int cmd,
898 unsigned long arg)
899{
900 struct lineevent_state *le = filep->private_data;
901 void __user *ip = (void __user *)arg;
902 struct gpiohandle_data ghd;
903
904 /*
905 * We can get the value for an event line but not set it,
906 * because it is input by definition.
907 */
908 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
909 int val;
910
Lars-Peter Clausend82aa4a2016-10-18 16:54:04 +0200911 memset(&ghd, 0, sizeof(ghd));
912
Linus Walleij61f922d2016-06-02 11:30:15 +0200913 val = gpiod_get_value_cansleep(le->desc);
914 if (val < 0)
915 return val;
916 ghd.values[0] = val;
917
918 if (copy_to_user(ip, &ghd, sizeof(ghd)))
919 return -EFAULT;
920
921 return 0;
922 }
923 return -EINVAL;
924}
925
926#ifdef CONFIG_COMPAT
927static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd,
928 unsigned long arg)
929{
930 return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
931}
932#endif
933
934static const struct file_operations lineevent_fileops = {
935 .release = lineevent_release,
936 .read = lineevent_read,
937 .poll = lineevent_poll,
938 .owner = THIS_MODULE,
939 .llseek = noop_llseek,
940 .unlocked_ioctl = lineevent_ioctl,
941#ifdef CONFIG_COMPAT
942 .compat_ioctl = lineevent_ioctl_compat,
943#endif
944};
945
Ben Dooks33265b12016-06-17 16:03:13 +0100946static irqreturn_t lineevent_irq_thread(int irq, void *p)
Linus Walleij61f922d2016-06-02 11:30:15 +0200947{
948 struct lineevent_state *le = p;
949 struct gpioevent_data ge;
Uwe Kleine-Königfa388692018-08-20 08:32:53 +0200950 int ret;
Linus Walleij61f922d2016-06-02 11:30:15 +0200951
Linus Walleij24bd3ef2018-01-22 13:19:28 +0100952 /* Do not leak kernel stack to userspace */
953 memset(&ge, 0, sizeof(ge));
954
Bartosz Golaszewski1033be52019-01-04 11:24:20 +0100955 /*
956 * We may be running from a nested threaded interrupt in which case
957 * we didn't get the timestamp from lineevent_irq_handler().
958 */
959 if (!le->timestamp)
Linus Walleijf8850202020-02-20 15:56:24 +0100960 ge.timestamp = ktime_get_ns();
Bartosz Golaszewski1033be52019-01-04 11:24:20 +0100961 else
962 ge.timestamp = le->timestamp;
Linus Walleij61f922d2016-06-02 11:30:15 +0200963
Bartosz Golaszewskiad537b82017-06-23 13:45:16 +0200964 if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
965 && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
Uwe Kleine-Königfa388692018-08-20 08:32:53 +0200966 int level = gpiod_get_value_cansleep(le->desc);
Linus Walleij61f922d2016-06-02 11:30:15 +0200967 if (level)
968 /* Emit low-to-high event */
969 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
970 else
971 /* Emit high-to-low event */
972 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Uwe Kleine-Königfa388692018-08-20 08:32:53 +0200973 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200974 /* Emit low-to-high event */
975 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
Uwe Kleine-Königfa388692018-08-20 08:32:53 +0200976 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200977 /* Emit high-to-low event */
978 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Arnd Bergmannbc0207a2016-06-16 11:02:41 +0200979 } else {
980 return IRQ_NONE;
Linus Walleij61f922d2016-06-02 11:30:15 +0200981 }
982
Bartosz Golaszewskidea9c802019-11-27 12:19:21 +0100983 ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge,
984 1, &le->wait.lock);
Saiyam Doshi2efc6bf2019-09-07 23:09:10 +0530985 if (ret)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800986 wake_up_poll(&le->wait, EPOLLIN);
Bartosz Golaszewski248ae172019-11-29 11:22:18 +0100987 else
988 pr_debug_ratelimited("event FIFO is full - event dropped\n");
Linus Walleij61f922d2016-06-02 11:30:15 +0200989
990 return IRQ_HANDLED;
991}
992
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100993static irqreturn_t lineevent_irq_handler(int irq, void *p)
994{
995 struct lineevent_state *le = p;
996
997 /*
998 * Just store the timestamp in hardirq context so we get it as
999 * close in time as possible to the actual event.
1000 */
Linus Walleijf8850202020-02-20 15:56:24 +01001001 le->timestamp = ktime_get_ns();
Linus Walleijd58f2bf2017-11-30 10:23:27 +01001002
1003 return IRQ_WAKE_THREAD;
1004}
1005
Linus Walleij61f922d2016-06-02 11:30:15 +02001006static int lineevent_create(struct gpio_device *gdev, void __user *ip)
1007{
1008 struct gpioevent_request eventreq;
1009 struct lineevent_state *le;
1010 struct gpio_desc *desc;
Lars-Peter Clausen953b9562016-10-24 13:59:15 +02001011 struct file *file;
Linus Walleij61f922d2016-06-02 11:30:15 +02001012 u32 offset;
1013 u32 lflags;
1014 u32 eflags;
1015 int fd;
1016 int ret;
1017 int irqflags = 0;
1018
1019 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
1020 return -EFAULT;
1021
Bartosz Golaszewskibcc6d992019-09-16 11:46:23 +02001022 offset = eventreq.lineoffset;
1023 lflags = eventreq.handleflags;
1024 eflags = eventreq.eventflags;
1025
Bartosz Golaszewski45e23602019-12-24 13:07:01 +01001026 desc = gpiochip_get_desc(gdev->chip, offset);
1027 if (IS_ERR(desc))
1028 return PTR_ERR(desc);
Bartosz Golaszewskibcc6d992019-09-16 11:46:23 +02001029
1030 /* Return an error if a unknown flag is set */
1031 if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
1032 (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS))
1033 return -EINVAL;
1034
1035 /* This is just wrong: we don't look for events on output lines */
1036 if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) ||
1037 (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
1038 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
1039 return -EINVAL;
1040
Kent Gibson2148ad72019-11-05 10:04:25 +08001041 /* Only one bias flag can be set. */
1042 if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
1043 (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
1044 GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
1045 ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
1046 (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
1047 return -EINVAL;
1048
Linus Walleij61f922d2016-06-02 11:30:15 +02001049 le = kzalloc(sizeof(*le), GFP_KERNEL);
1050 if (!le)
1051 return -ENOMEM;
1052 le->gdev = gdev;
1053 get_device(&gdev->dev);
1054
1055 /* Make sure this is terminated */
1056 eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
1057 if (strlen(eventreq.consumer_label)) {
1058 le->label = kstrdup(eventreq.consumer_label,
1059 GFP_KERNEL);
1060 if (!le->label) {
1061 ret = -ENOMEM;
1062 goto out_free_le;
1063 }
1064 }
1065
Linus Walleij61f922d2016-06-02 11:30:15 +02001066 ret = gpiod_request(desc, le->label);
1067 if (ret)
Uwe Kleine-Königf001cc32018-04-16 13:17:53 +02001068 goto out_free_label;
Linus Walleij61f922d2016-06-02 11:30:15 +02001069 le->desc = desc;
1070 le->eflags = eflags;
1071
1072 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
1073 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
Kent Gibson2148ad72019-11-05 10:04:25 +08001074 if (lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE)
1075 set_bit(FLAG_BIAS_DISABLE, &desc->flags);
Kent Gibson7b479a82019-11-05 10:04:24 +08001076 if (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN)
1077 set_bit(FLAG_PULL_DOWN, &desc->flags);
1078 if (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)
1079 set_bit(FLAG_PULL_UP, &desc->flags);
Linus Walleij61f922d2016-06-02 11:30:15 +02001080
1081 ret = gpiod_direction_input(desc);
1082 if (ret)
1083 goto out_free_desc;
1084
1085 le->irq = gpiod_to_irq(desc);
1086 if (le->irq <= 0) {
1087 ret = -ENODEV;
1088 goto out_free_desc;
1089 }
1090
1091 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
Michael Wu223ecaf2019-07-08 13:23:08 +08001092 irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
1093 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
Linus Walleij61f922d2016-06-02 11:30:15 +02001094 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
Michael Wu223ecaf2019-07-08 13:23:08 +08001095 irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
1096 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
Linus Walleij61f922d2016-06-02 11:30:15 +02001097 irqflags |= IRQF_ONESHOT;
Linus Walleij61f922d2016-06-02 11:30:15 +02001098
1099 INIT_KFIFO(le->events);
1100 init_waitqueue_head(&le->wait);
Linus Walleij61f922d2016-06-02 11:30:15 +02001101
1102 /* Request a thread to read the events */
1103 ret = request_threaded_irq(le->irq,
Linus Walleijd58f2bf2017-11-30 10:23:27 +01001104 lineevent_irq_handler,
Linus Walleij61f922d2016-06-02 11:30:15 +02001105 lineevent_irq_thread,
1106 irqflags,
1107 le->label,
1108 le);
1109 if (ret)
1110 goto out_free_desc;
1111
Lars-Peter Clausen953b9562016-10-24 13:59:15 +02001112 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleij61f922d2016-06-02 11:30:15 +02001113 if (fd < 0) {
1114 ret = fd;
1115 goto out_free_irq;
1116 }
1117
Lars-Peter Clausen953b9562016-10-24 13:59:15 +02001118 file = anon_inode_getfile("gpio-event",
1119 &lineevent_fileops,
1120 le,
1121 O_RDONLY | O_CLOEXEC);
1122 if (IS_ERR(file)) {
1123 ret = PTR_ERR(file);
1124 goto out_put_unused_fd;
1125 }
1126
Linus Walleij61f922d2016-06-02 11:30:15 +02001127 eventreq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +02001128 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +02001129 /*
1130 * fput() will trigger the release() callback, so do not go onto
1131 * the regular error cleanup path here.
1132 */
1133 fput(file);
1134 put_unused_fd(fd);
1135 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +02001136 }
Linus Walleij61f922d2016-06-02 11:30:15 +02001137
Lars-Peter Clausen953b9562016-10-24 13:59:15 +02001138 fd_install(fd, file);
1139
Linus Walleij61f922d2016-06-02 11:30:15 +02001140 return 0;
1141
Lars-Peter Clausen953b9562016-10-24 13:59:15 +02001142out_put_unused_fd:
1143 put_unused_fd(fd);
Linus Walleij61f922d2016-06-02 11:30:15 +02001144out_free_irq:
1145 free_irq(le->irq, le);
1146out_free_desc:
1147 gpiod_free(le->desc);
1148out_free_label:
1149 kfree(le->label);
1150out_free_le:
1151 kfree(le);
1152 put_device(&gdev->dev);
1153 return ret;
1154}
1155
Bartosz Golaszewskid2ac2572019-12-03 14:08:44 +01001156static void gpio_desc_to_lineinfo(struct gpio_desc *desc,
1157 struct gpioline_info *info)
1158{
1159 struct gpio_chip *chip = desc->gdev->chip;
1160 unsigned long flags;
1161
1162 spin_lock_irqsave(&gpio_lock, flags);
1163
1164 if (desc->name) {
1165 strncpy(info->name, desc->name, sizeof(info->name));
1166 info->name[sizeof(info->name) - 1] = '\0';
1167 } else {
1168 info->name[0] = '\0';
1169 }
1170
1171 if (desc->label) {
1172 strncpy(info->consumer, desc->label, sizeof(info->consumer));
1173 info->consumer[sizeof(info->consumer) - 1] = '\0';
1174 } else {
1175 info->consumer[0] = '\0';
1176 }
1177
1178 /*
1179 * Userspace only need to know that the kernel is using this GPIO so
1180 * it can't use it.
1181 */
1182 info->flags = 0;
1183 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
1184 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
1185 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
1186 test_bit(FLAG_EXPORT, &desc->flags) ||
1187 test_bit(FLAG_SYSFS, &desc->flags) ||
1188 !pinctrl_gpio_can_use_line(chip->base + info->line_offset))
1189 info->flags |= GPIOLINE_FLAG_KERNEL;
1190 if (test_bit(FLAG_IS_OUT, &desc->flags))
1191 info->flags |= GPIOLINE_FLAG_IS_OUT;
1192 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1193 info->flags |= GPIOLINE_FLAG_ACTIVE_LOW;
1194 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1195 info->flags |= (GPIOLINE_FLAG_OPEN_DRAIN |
1196 GPIOLINE_FLAG_IS_OUT);
1197 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1198 info->flags |= (GPIOLINE_FLAG_OPEN_SOURCE |
1199 GPIOLINE_FLAG_IS_OUT);
1200 if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
1201 info->flags |= GPIOLINE_FLAG_BIAS_DISABLE;
1202 if (test_bit(FLAG_PULL_DOWN, &desc->flags))
1203 info->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN;
1204 if (test_bit(FLAG_PULL_UP, &desc->flags))
1205 info->flags |= GPIOLINE_FLAG_BIAS_PULL_UP;
1206
1207 spin_unlock_irqrestore(&gpio_lock, flags);
1208}
1209
Bartosz Golaszewski51c10642019-11-22 15:19:21 +01001210struct gpio_chardev_data {
1211 struct gpio_device *gdev;
1212 wait_queue_head_t wait;
1213 DECLARE_KFIFO(events, struct gpioline_info_changed, 32);
1214 struct notifier_block lineinfo_changed_nb;
1215 unsigned long *watched_lines;
1216};
1217
Thierry Reding950d55f52017-07-24 16:57:22 +02001218/*
Linus Walleij3c702e92015-10-21 15:29:53 +02001219 * gpio_ioctl() - ioctl handler for the GPIO chardev
1220 */
1221static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1222{
Bartosz Golaszewski51c10642019-11-22 15:19:21 +01001223 struct gpio_chardev_data *priv = filp->private_data;
1224 struct gpio_device *gdev = priv->gdev;
Linus Walleij3c702e92015-10-21 15:29:53 +02001225 struct gpio_chip *chip = gdev->chip;
Linus Walleij8b92e172016-05-27 14:24:04 +02001226 void __user *ip = (void __user *)arg;
Bartosz Golaszewski51c10642019-11-22 15:19:21 +01001227 struct gpio_desc *desc;
1228 __u32 offset;
Linus Walleij3c702e92015-10-21 15:29:53 +02001229
1230 /* We fail any subsequent ioctl():s when the chip is gone */
1231 if (!chip)
1232 return -ENODEV;
1233
Linus Walleij521a2ad2016-02-12 22:25:22 +01001234 /* Fill in the struct and pass to userspace */
Linus Walleij3c702e92015-10-21 15:29:53 +02001235 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
Linus Walleij521a2ad2016-02-12 22:25:22 +01001236 struct gpiochip_info chipinfo;
1237
Lars-Peter Clausen0f4bbb22016-10-18 16:54:00 +02001238 memset(&chipinfo, 0, sizeof(chipinfo));
1239
Linus Walleij3c702e92015-10-21 15:29:53 +02001240 strncpy(chipinfo.name, dev_name(&gdev->dev),
1241 sizeof(chipinfo.name));
1242 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
Linus Walleijdf4878e2016-02-12 14:48:23 +01001243 strncpy(chipinfo.label, gdev->label,
1244 sizeof(chipinfo.label));
1245 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001246 chipinfo.lines = gdev->ngpio;
Linus Walleij3c702e92015-10-21 15:29:53 +02001247 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
1248 return -EFAULT;
1249 return 0;
Bartosz Golaszewski51c10642019-11-22 15:19:21 +01001250 } else if (cmd == GPIO_GET_LINEINFO_IOCTL ||
1251 cmd == GPIO_GET_LINEINFO_WATCH_IOCTL) {
Linus Walleij521a2ad2016-02-12 22:25:22 +01001252 struct gpioline_info lineinfo;
Linus Walleij521a2ad2016-02-12 22:25:22 +01001253
1254 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
1255 return -EFAULT;
Linus Walleij521a2ad2016-02-12 22:25:22 +01001256
Bartosz Golaszewski2a2cabd2019-12-24 13:07:02 +01001257 desc = gpiochip_get_desc(chip, lineinfo.line_offset);
1258 if (IS_ERR(desc))
1259 return PTR_ERR(desc);
1260
Bartosz Golaszewskid2ac2572019-12-03 14:08:44 +01001261 gpio_desc_to_lineinfo(desc, &lineinfo);
Linus Walleij521a2ad2016-02-12 22:25:22 +01001262
1263 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
1264 return -EFAULT;
Bartosz Golaszewski51c10642019-11-22 15:19:21 +01001265
1266 if (cmd == GPIO_GET_LINEINFO_WATCH_IOCTL)
Bartosz Golaszewski19314792020-02-26 14:53:23 +01001267 set_bit(gpio_chip_hwgpio(desc), priv->watched_lines);
Bartosz Golaszewski51c10642019-11-22 15:19:21 +01001268
Linus Walleij521a2ad2016-02-12 22:25:22 +01001269 return 0;
Linus Walleijd7c51b42016-04-26 10:35:29 +02001270 } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
1271 return linehandle_create(gdev, ip);
Linus Walleij61f922d2016-06-02 11:30:15 +02001272 } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
1273 return lineevent_create(gdev, ip);
Bartosz Golaszewski51c10642019-11-22 15:19:21 +01001274 } else if (cmd == GPIO_GET_LINEINFO_UNWATCH_IOCTL) {
1275 if (copy_from_user(&offset, ip, sizeof(offset)))
1276 return -EFAULT;
1277
1278 desc = gpiochip_get_desc(chip, offset);
1279 if (IS_ERR(desc))
1280 return PTR_ERR(desc);
1281
Bartosz Golaszewski19314792020-02-26 14:53:23 +01001282 clear_bit(gpio_chip_hwgpio(desc), priv->watched_lines);
Bartosz Golaszewski51c10642019-11-22 15:19:21 +01001283 return 0;
Linus Walleij3c702e92015-10-21 15:29:53 +02001284 }
1285 return -EINVAL;
1286}
1287
Linus Walleij8b92e172016-05-27 14:24:04 +02001288#ifdef CONFIG_COMPAT
1289static long gpio_ioctl_compat(struct file *filp, unsigned int cmd,
1290 unsigned long arg)
1291{
1292 return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
1293}
1294#endif
1295
Bartosz Golaszewski51c10642019-11-22 15:19:21 +01001296static struct gpio_chardev_data *
1297to_gpio_chardev_data(struct notifier_block *nb)
1298{
1299 return container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb);
1300}
1301
1302static int lineinfo_changed_notify(struct notifier_block *nb,
1303 unsigned long action, void *data)
1304{
1305 struct gpio_chardev_data *priv = to_gpio_chardev_data(nb);
1306 struct gpioline_info_changed chg;
1307 struct gpio_desc *desc = data;
1308 int ret;
1309
Bartosz Golaszewski19314792020-02-26 14:53:23 +01001310 if (!test_bit(gpio_chip_hwgpio(desc), priv->watched_lines))
Bartosz Golaszewski51c10642019-11-22 15:19:21 +01001311 return NOTIFY_DONE;
1312
1313 memset(&chg, 0, sizeof(chg));
1314 chg.info.line_offset = gpio_chip_hwgpio(desc);
1315 chg.event_type = action;
1316 chg.timestamp = ktime_get_ns();
1317 gpio_desc_to_lineinfo(desc, &chg.info);
1318
1319 ret = kfifo_in_spinlocked(&priv->events, &chg, 1, &priv->wait.lock);
1320 if (ret)
1321 wake_up_poll(&priv->wait, EPOLLIN);
1322 else
1323 pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n");
1324
1325 return NOTIFY_OK;
1326}
1327
1328static __poll_t lineinfo_watch_poll(struct file *filep,
1329 struct poll_table_struct *pollt)
1330{
1331 struct gpio_chardev_data *priv = filep->private_data;
1332 __poll_t events = 0;
1333
1334 poll_wait(filep, &priv->wait, pollt);
1335
1336 if (!kfifo_is_empty_spinlocked_noirqsave(&priv->events,
1337 &priv->wait.lock))
1338 events = EPOLLIN | EPOLLRDNORM;
1339
1340 return events;
1341}
1342
1343static ssize_t lineinfo_watch_read(struct file *filep, char __user *buf,
1344 size_t count, loff_t *off)
1345{
1346 struct gpio_chardev_data *priv = filep->private_data;
1347 struct gpioline_info_changed event;
1348 ssize_t bytes_read = 0;
1349 int ret;
1350
1351 if (count < sizeof(event))
1352 return -EINVAL;
1353
1354 do {
1355 spin_lock(&priv->wait.lock);
1356 if (kfifo_is_empty(&priv->events)) {
1357 if (bytes_read) {
1358 spin_unlock(&priv->wait.lock);
1359 return bytes_read;
1360 }
1361
1362 if (filep->f_flags & O_NONBLOCK) {
1363 spin_unlock(&priv->wait.lock);
1364 return -EAGAIN;
1365 }
1366
1367 ret = wait_event_interruptible_locked(priv->wait,
1368 !kfifo_is_empty(&priv->events));
1369 if (ret) {
1370 spin_unlock(&priv->wait.lock);
1371 return ret;
1372 }
1373 }
1374
1375 ret = kfifo_out(&priv->events, &event, 1);
1376 spin_unlock(&priv->wait.lock);
1377 if (ret != 1) {
1378 ret = -EIO;
1379 break;
1380 /* We should never get here. See lineevent_read(). */
1381 }
1382
1383 if (copy_to_user(buf + bytes_read, &event, sizeof(event)))
1384 return -EFAULT;
1385 bytes_read += sizeof(event);
1386 } while (count >= bytes_read + sizeof(event));
1387
1388 return bytes_read;
1389}
1390
Linus Walleij3c702e92015-10-21 15:29:53 +02001391/**
1392 * gpio_chrdev_open() - open the chardev for ioctl operations
1393 * @inode: inode for this chardev
1394 * @filp: file struct for storing private data
1395 * Returns 0 on success
1396 */
1397static int gpio_chrdev_open(struct inode *inode, struct file *filp)
1398{
1399 struct gpio_device *gdev = container_of(inode->i_cdev,
1400 struct gpio_device, chrdev);
Bartosz Golaszewski51c10642019-11-22 15:19:21 +01001401 struct gpio_chardev_data *priv;
1402 int ret = -ENOMEM;
Linus Walleij3c702e92015-10-21 15:29:53 +02001403
1404 /* Fail on open if the backing gpiochip is gone */
Stephen Boydfb505742017-01-09 11:47:44 -08001405 if (!gdev->chip)
Linus Walleij3c702e92015-10-21 15:29:53 +02001406 return -ENODEV;
Lars-Peter Clausenf4e81c52016-11-30 13:05:21 +01001407
Bartosz Golaszewski51c10642019-11-22 15:19:21 +01001408 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1409 if (!priv)
1410 return -ENOMEM;
1411
1412 priv->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL);
1413 if (!priv->watched_lines)
1414 goto out_free_priv;
1415
1416 init_waitqueue_head(&priv->wait);
1417 INIT_KFIFO(priv->events);
1418 priv->gdev = gdev;
1419
1420 priv->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify;
1421 ret = atomic_notifier_chain_register(&gdev->notifier,
1422 &priv->lineinfo_changed_nb);
1423 if (ret)
1424 goto out_free_bitmap;
1425
1426 get_device(&gdev->dev);
1427 filp->private_data = priv;
1428
1429 ret = nonseekable_open(inode, filp);
1430 if (ret)
1431 goto out_unregister_notifier;
1432
1433 return ret;
1434
1435out_unregister_notifier:
1436 atomic_notifier_chain_unregister(&gdev->notifier,
1437 &priv->lineinfo_changed_nb);
1438out_free_bitmap:
1439 bitmap_free(priv->watched_lines);
1440out_free_priv:
1441 kfree(priv);
1442 return ret;
Linus Walleij3c702e92015-10-21 15:29:53 +02001443}
1444
1445/**
1446 * gpio_chrdev_release() - close chardev after ioctl operations
1447 * @inode: inode for this chardev
1448 * @filp: file struct for storing private data
1449 * Returns 0 on success
1450 */
1451static int gpio_chrdev_release(struct inode *inode, struct file *filp)
1452{
Bartosz Golaszewski51c10642019-11-22 15:19:21 +01001453 struct gpio_chardev_data *priv = filp->private_data;
1454 struct gpio_device *gdev = priv->gdev;
Linus Walleij3c702e92015-10-21 15:29:53 +02001455
Bartosz Golaszewski51c10642019-11-22 15:19:21 +01001456 bitmap_free(priv->watched_lines);
1457 atomic_notifier_chain_unregister(&gdev->notifier,
1458 &priv->lineinfo_changed_nb);
Linus Walleij3c702e92015-10-21 15:29:53 +02001459 put_device(&gdev->dev);
Bartosz Golaszewski51c10642019-11-22 15:19:21 +01001460 kfree(priv);
1461
Linus Walleij3c702e92015-10-21 15:29:53 +02001462 return 0;
1463}
1464
Linus Walleij3c702e92015-10-21 15:29:53 +02001465static const struct file_operations gpio_fileops = {
1466 .release = gpio_chrdev_release,
1467 .open = gpio_chrdev_open,
Bartosz Golaszewski51c10642019-11-22 15:19:21 +01001468 .poll = lineinfo_watch_poll,
1469 .read = lineinfo_watch_read,
Linus Walleij3c702e92015-10-21 15:29:53 +02001470 .owner = THIS_MODULE,
Lars-Peter Clausenf4e81c52016-11-30 13:05:21 +01001471 .llseek = no_llseek,
Linus Walleij3c702e92015-10-21 15:29:53 +02001472 .unlocked_ioctl = gpio_ioctl,
Linus Walleij8b92e172016-05-27 14:24:04 +02001473#ifdef CONFIG_COMPAT
1474 .compat_ioctl = gpio_ioctl_compat,
1475#endif
Linus Walleij3c702e92015-10-21 15:29:53 +02001476};
1477
Linus Walleijff2b1352015-10-20 11:10:38 +02001478static void gpiodevice_release(struct device *dev)
1479{
1480 struct gpio_device *gdev = dev_get_drvdata(dev);
1481
1482 list_del(&gdev->list);
1483 ida_simple_remove(&gpio_ida, gdev->id);
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +01001484 kfree_const(gdev->label);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001485 kfree(gdev->descs);
Linus Walleij9efd9e62016-02-09 14:27:42 +01001486 kfree(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001487}
1488
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001489static int gpiochip_setup_dev(struct gpio_device *gdev)
1490{
Linus Walleijd377f562019-07-16 11:11:45 +02001491 int ret;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001492
1493 cdev_init(&gdev->chrdev, &gpio_fileops);
1494 gdev->chrdev.owner = THIS_MODULE;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001495 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001496
Linus Walleijd377f562019-07-16 11:11:45 +02001497 ret = cdev_device_add(&gdev->chrdev, &gdev->dev);
1498 if (ret)
1499 return ret;
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001500
1501 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
1502 MAJOR(gpio_devt), gdev->id);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001503
Linus Walleijd377f562019-07-16 11:11:45 +02001504 ret = gpiochip_sysfs_register(gdev);
1505 if (ret)
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001506 goto err_remove_device;
1507
1508 /* From this point, the .release() function cleans up gpio_device */
1509 gdev->dev.release = gpiodevice_release;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001510 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
1511 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
1512 dev_name(&gdev->dev), gdev->chip->label ? : "generic");
1513
1514 return 0;
1515
1516err_remove_device:
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001517 cdev_device_del(&gdev->chrdev, &gdev->dev);
Linus Walleijd377f562019-07-16 11:11:45 +02001518 return ret;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001519}
1520
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02001521static void gpiochip_machine_hog(struct gpio_chip *chip, struct gpiod_hog *hog)
1522{
1523 struct gpio_desc *desc;
1524 int rv;
1525
1526 desc = gpiochip_get_desc(chip, hog->chip_hwnum);
1527 if (IS_ERR(desc)) {
1528 pr_err("%s: unable to get GPIO desc: %ld\n",
1529 __func__, PTR_ERR(desc));
1530 return;
1531 }
1532
Dan Carpenterba3efdf2018-05-01 10:36:39 +03001533 if (test_bit(FLAG_IS_HOGGED, &desc->flags))
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02001534 return;
1535
1536 rv = gpiod_hog(desc, hog->line_name, hog->lflags, hog->dflags);
1537 if (rv)
1538 pr_err("%s: unable to hog GPIO line (%s:%u): %d\n",
1539 __func__, chip->label, hog->chip_hwnum, rv);
1540}
1541
1542static void machine_gpiochip_add(struct gpio_chip *chip)
1543{
1544 struct gpiod_hog *hog;
1545
1546 mutex_lock(&gpio_machine_hogs_mutex);
1547
1548 list_for_each_entry(hog, &gpio_machine_hogs, list) {
1549 if (!strcmp(chip->label, hog->chip_label))
1550 gpiochip_machine_hog(chip, hog);
1551 }
1552
1553 mutex_unlock(&gpio_machine_hogs_mutex);
1554}
1555
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001556static void gpiochip_setup_devs(void)
1557{
1558 struct gpio_device *gdev;
Linus Walleijd377f562019-07-16 11:11:45 +02001559 int ret;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001560
1561 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijd377f562019-07-16 11:11:45 +02001562 ret = gpiochip_setup_dev(gdev);
1563 if (ret)
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001564 pr_err("%s: Failed to initialize gpio device (%d)\n",
Linus Walleijd377f562019-07-16 11:11:45 +02001565 dev_name(&gdev->dev), ret);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001566 }
1567}
1568
Thierry Reding959bc7b2017-11-07 19:15:59 +01001569int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001570 struct lock_class_key *lock_key,
1571 struct lock_class_key *request_key)
David Brownelld2876d02008-02-04 22:28:20 -08001572{
1573 unsigned long flags;
Linus Walleijd377f562019-07-16 11:11:45 +02001574 int ret = 0;
Linus Walleijff2b1352015-10-20 11:10:38 +02001575 unsigned i;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001576 int base = chip->base;
Linus Walleijff2b1352015-10-20 11:10:38 +02001577 struct gpio_device *gdev;
David Brownelld2876d02008-02-04 22:28:20 -08001578
Linus Walleijff2b1352015-10-20 11:10:38 +02001579 /*
1580 * First: allocate and populate the internal stat container, and
1581 * set up the struct device.
1582 */
Josh Cartwright969f07b2016-02-17 16:44:15 -06001583 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
Linus Walleijff2b1352015-10-20 11:10:38 +02001584 if (!gdev)
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001585 return -ENOMEM;
Linus Walleij3c702e92015-10-21 15:29:53 +02001586 gdev->dev.bus = &gpio_bus_type;
Linus Walleijff2b1352015-10-20 11:10:38 +02001587 gdev->chip = chip;
1588 chip->gpiodev = gdev;
1589 if (chip->parent) {
1590 gdev->dev.parent = chip->parent;
1591 gdev->dev.of_node = chip->parent->of_node;
Thierry Redingacc6e332016-07-05 14:11:14 +02001592 }
1593
Linus Walleijff2b1352015-10-20 11:10:38 +02001594#ifdef CONFIG_OF_GPIO
1595 /* If the gpiochip has an assigned OF node this takes precedence */
Thierry Redingacc6e332016-07-05 14:11:14 +02001596 if (chip->of_node)
1597 gdev->dev.of_node = chip->of_node;
Biju Das6ff04972018-08-06 10:48:01 +01001598 else
1599 chip->of_node = gdev->dev.of_node;
Linus Walleijff2b1352015-10-20 11:10:38 +02001600#endif
Thierry Redingacc6e332016-07-05 14:11:14 +02001601
Linus Walleijff2b1352015-10-20 11:10:38 +02001602 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
1603 if (gdev->id < 0) {
Linus Walleijd377f562019-07-16 11:11:45 +02001604 ret = gdev->id;
Linus Walleijff2b1352015-10-20 11:10:38 +02001605 goto err_free_gdev;
1606 }
Geert Uytterhoevenddd88912019-11-27 09:42:47 +01001607 dev_set_name(&gdev->dev, GPIOCHIP_NAME "%d", gdev->id);
Linus Walleijff2b1352015-10-20 11:10:38 +02001608 device_initialize(&gdev->dev);
1609 dev_set_drvdata(&gdev->dev, gdev);
1610 if (chip->parent && chip->parent->driver)
1611 gdev->owner = chip->parent->driver->owner;
1612 else if (chip->owner)
1613 /* TODO: remove chip->owner */
1614 gdev->owner = chip->owner;
1615 else
1616 gdev->owner = THIS_MODULE;
David Brownelld2876d02008-02-04 22:28:20 -08001617
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001618 gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001619 if (!gdev->descs) {
Linus Walleijd377f562019-07-16 11:11:45 +02001620 ret = -ENOMEM;
Vladimir Zapolskiya05a1402018-11-02 15:39:43 +02001621 goto err_free_ida;
Linus Walleijff2b1352015-10-20 11:10:38 +02001622 }
1623
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001624 if (chip->ngpio == 0) {
1625 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
Linus Walleijd377f562019-07-16 11:11:45 +02001626 ret = -EINVAL;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001627 goto err_free_descs;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001628 }
Linus Walleijdf4878e2016-02-12 14:48:23 +01001629
Laura Abbott30277432018-05-21 10:57:07 -07001630 if (chip->ngpio > FASTPATH_NGPIO)
1631 chip_warn(chip, "line cnt %u is greater than fast path cnt %u\n",
Enrico Weigelt, metux IT consult2ddac5a2020-01-04 20:43:34 +01001632 chip->ngpio, FASTPATH_NGPIO);
Laura Abbott30277432018-05-21 10:57:07 -07001633
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +01001634 gdev->label = kstrdup_const(chip->label ?: "unknown", GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001635 if (!gdev->label) {
Linus Walleijd377f562019-07-16 11:11:45 +02001636 ret = -ENOMEM;
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001637 goto err_free_descs;
Linus Walleijdf4878e2016-02-12 14:48:23 +01001638 }
1639
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001640 gdev->ngpio = chip->ngpio;
Linus Walleij43c54ec2016-02-11 11:37:48 +01001641 gdev->data = data;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001642
David Brownelld2876d02008-02-04 22:28:20 -08001643 spin_lock_irqsave(&gpio_lock, flags);
1644
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001645 /*
1646 * TODO: this allocates a Linux GPIO number base in the global
1647 * GPIO numberspace for this chip. In the long run we want to
1648 * get *rid* of this numberspace and use only descriptors, but
1649 * it may be a pipe dream. It will not happen before we get rid
1650 * of the sysfs interface anyways.
1651 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001652 if (base < 0) {
1653 base = gpiochip_find_base(chip->ngpio);
1654 if (base < 0) {
Linus Walleijd377f562019-07-16 11:11:45 +02001655 ret = base;
Johan Hovold225fce82015-01-12 17:12:25 +01001656 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001657 goto err_free_label;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001658 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001659 /*
1660 * TODO: it should not be necessary to reflect the assigned
1661 * base outside of the GPIO subsystem. Go over drivers and
1662 * see if anyone makes use of this, else drop this and assign
1663 * a poison instead.
1664 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001665 chip->base = base;
1666 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001667 gdev->base = base;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001668
Linus Walleijd377f562019-07-16 11:11:45 +02001669 ret = gpiodev_add_to_list(gdev);
1670 if (ret) {
Johan Hovold05aa5202015-01-12 17:12:26 +01001671 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001672 goto err_free_label;
Johan Hovold05aa5202015-01-12 17:12:26 +01001673 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001674
Ricardo Ribalda Delgado767cd172018-10-12 08:11:36 +02001675 for (i = 0; i < chip->ngpio; i++)
1676 gdev->descs[i].gdev = gdev;
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001677
Dan Callaghan207270d2020-01-21 10:12:17 +10001678 spin_unlock_irqrestore(&gpio_lock, flags);
1679
Bartosz Golaszewski51c10642019-11-22 15:19:21 +01001680 ATOMIC_INIT_NOTIFIER_HEAD(&gdev->notifier);
1681
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301682#ifdef CONFIG_PINCTRL
Linus Walleij20ec3e32016-02-11 11:03:06 +01001683 INIT_LIST_HEAD(&gdev->pin_ranges);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301684#endif
1685
Linus Walleijd377f562019-07-16 11:11:45 +02001686 ret = gpiochip_set_desc_names(chip);
1687 if (ret)
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001688 goto err_remove_from_list;
1689
Linus Walleij151a4102019-09-05 11:40:54 +02001690 ret = gpiochip_alloc_valid_mask(chip);
Linus Walleijd377f562019-07-16 11:11:45 +02001691 if (ret)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001692 goto err_remove_from_list;
1693
Linus Walleijd377f562019-07-16 11:11:45 +02001694 ret = of_gpiochip_add(chip);
1695 if (ret)
Linus Walleij48057ed2019-08-20 10:05:27 +02001696 goto err_free_gpiochip_mask;
Tomeu Vizoso28355f82015-07-14 10:29:54 +02001697
Linus Walleijd377f562019-07-16 11:11:45 +02001698 ret = gpiochip_init_valid_mask(chip);
1699 if (ret)
Geert Uytterhoeven35779892019-04-24 15:59:33 +02001700 goto err_remove_of_chip;
Ricardo Ribalda Delgadof8ec92a2018-10-05 08:52:58 +02001701
Ricardo Ribalda Delgado3edfb7b2018-10-05 08:53:00 +02001702 for (i = 0; i < chip->ngpio; i++) {
1703 struct gpio_desc *desc = &gdev->descs[i];
1704
Chris Packhamd95da992019-07-08 08:35:58 +12001705 if (chip->get_direction && gpiochip_line_is_valid(chip, i)) {
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +02001706 assign_bit(FLAG_IS_OUT,
1707 &desc->flags, !chip->get_direction(chip, i));
Chris Packhamd95da992019-07-08 08:35:58 +12001708 } else {
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +02001709 assign_bit(FLAG_IS_OUT,
1710 &desc->flags, !chip->direction_input);
Chris Packhamd95da992019-07-08 08:35:58 +12001711 }
Ricardo Ribalda Delgado3edfb7b2018-10-05 08:53:00 +02001712 }
1713
Andy Shevchenkob056ca12019-11-04 18:09:39 +02001714 ret = gpiochip_add_pin_ranges(chip);
1715 if (ret)
1716 goto err_remove_of_chip;
1717
Mika Westerberg664e3e52014-01-08 12:40:54 +02001718 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001719
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02001720 machine_gpiochip_add(chip);
1721
Linus Walleij504369c2019-10-30 13:29:14 +01001722 ret = gpiochip_irqchip_init_valid_mask(chip);
Andy Shevchenko9411e3a2019-10-09 17:34:44 +03001723 if (ret)
1724 goto err_remove_acpi_chip;
1725
Linus Walleij504369c2019-10-30 13:29:14 +01001726 ret = gpiochip_irqchip_init_hw(chip);
Linus Walleijfbdf8d42019-09-06 12:05:35 +02001727 if (ret)
Linus Walleij48057ed2019-08-20 10:05:27 +02001728 goto err_remove_acpi_chip;
1729
Linus Walleijfbdf8d42019-09-06 12:05:35 +02001730 ret = gpiochip_add_irqchip(chip, lock_key, request_key);
1731 if (ret)
Linus Walleij48057ed2019-08-20 10:05:27 +02001732 goto err_remove_irqchip_mask;
1733
Linus Walleij3c702e92015-10-21 15:29:53 +02001734 /*
1735 * By first adding the chardev, and then adding the device,
1736 * we get a device node entry in sysfs under
1737 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1738 * coldplug of device nodes and other udev business.
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001739 * We can do this only if gpiolib has been initialized.
1740 * Otherwise, defer until later.
Linus Walleij3c702e92015-10-21 15:29:53 +02001741 */
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001742 if (gpiolib_initialized) {
Linus Walleijd377f562019-07-16 11:11:45 +02001743 ret = gpiochip_setup_dev(gdev);
1744 if (ret)
Linus Walleij48057ed2019-08-20 10:05:27 +02001745 goto err_remove_irqchip;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001746 }
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001747 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001748
Linus Walleij48057ed2019-08-20 10:05:27 +02001749err_remove_irqchip:
1750 gpiochip_irqchip_remove(chip);
1751err_remove_irqchip_mask:
1752 gpiochip_irqchip_free_valid_mask(chip);
Geert Uytterhoeven35779892019-04-24 15:59:33 +02001753err_remove_acpi_chip:
Johan Hovold225fce82015-01-12 17:12:25 +01001754 acpi_gpiochip_remove(chip);
Geert Uytterhoeven35779892019-04-24 15:59:33 +02001755err_remove_of_chip:
Johan Hovold6d867502015-05-04 17:23:25 +02001756 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +01001757 of_gpiochip_remove(chip);
Geert Uytterhoeven35779892019-04-24 15:59:33 +02001758err_free_gpiochip_mask:
Andy Shevchenko2f4133b2019-11-05 20:06:54 +02001759 gpiochip_remove_pin_ranges(chip);
Stephen Boyd726cb3b2018-03-23 09:34:52 -07001760 gpiochip_free_valid_mask(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001761err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +01001762 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001763 list_del(&gdev->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001764 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001765err_free_label:
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +01001766 kfree_const(gdev->label);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001767err_free_descs:
1768 kfree(gdev->descs);
Vladimir Zapolskiya05a1402018-11-02 15:39:43 +02001769err_free_ida:
Linus Walleijff2b1352015-10-20 11:10:38 +02001770 ida_simple_remove(&gpio_ida, gdev->id);
Vladimir Zapolskiya05a1402018-11-02 15:39:43 +02001771err_free_gdev:
David Brownelld2876d02008-02-04 22:28:20 -08001772 /* failures here can mean systems won't boot... */
Marcel Ziswiler1777fc92018-07-20 09:54:49 +02001773 pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__,
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001774 gdev->base, gdev->base + gdev->ngpio - 1,
Linus Walleijd377f562019-07-16 11:11:45 +02001775 chip->label ? : "generic", ret);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001776 kfree(gdev);
Linus Walleijd377f562019-07-16 11:11:45 +02001777 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08001778}
Thierry Reding959bc7b2017-11-07 19:15:59 +01001779EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);
David Brownelld2876d02008-02-04 22:28:20 -08001780
1781/**
Linus Walleij43c54ec2016-02-11 11:37:48 +01001782 * gpiochip_get_data() - get per-subdriver data for the chip
Thierry Reding950d55f52017-07-24 16:57:22 +02001783 * @chip: GPIO chip
1784 *
1785 * Returns:
1786 * The per-subdriver data for the chip.
Linus Walleij43c54ec2016-02-11 11:37:48 +01001787 */
1788void *gpiochip_get_data(struct gpio_chip *chip)
1789{
1790 return chip->gpiodev->data;
1791}
1792EXPORT_SYMBOL_GPL(gpiochip_get_data);
1793
1794/**
David Brownelld2876d02008-02-04 22:28:20 -08001795 * gpiochip_remove() - unregister a gpio_chip
1796 * @chip: the chip to unregister
1797 *
1798 * A gpio_chip with any GPIOs still requested may not be removed.
1799 */
abdoulaye berthee1db1702014-07-05 18:28:50 +02001800void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -08001801{
Linus Walleijff2b1352015-10-20 11:10:38 +02001802 struct gpio_device *gdev = chip->gpiodev;
David Brownelld2876d02008-02-04 22:28:20 -08001803 unsigned long flags;
Andy Shevchenko869233f2020-02-25 13:47:25 +02001804 unsigned int i;
David Brownelld2876d02008-02-04 22:28:20 -08001805
Linus Walleijff2b1352015-10-20 11:10:38 +02001806 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
Linus Walleijafbc4f32016-02-09 13:21:06 +01001807 gpiochip_sysfs_unregister(gdev);
Geert Uytterhoeven5018ada2016-12-19 18:29:23 +01001808 gpiochip_free_hogs(chip);
Bamvor Jian Zhangbd203bd2016-02-20 13:13:19 +08001809 /* Numb the device, cancelling all outstanding operations */
1810 gdev->chip = NULL;
Johan Hovold00acc3d2015-01-12 17:12:27 +01001811 gpiochip_irqchip_remove(chip);
Mika Westerberg6072b9d2014-03-10 14:54:53 +02001812 acpi_gpiochip_remove(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001813 of_gpiochip_remove(chip);
Andy Shevchenko2f4133b2019-11-05 20:06:54 +02001814 gpiochip_remove_pin_ranges(chip);
Stephen Boyd726cb3b2018-03-23 09:34:52 -07001815 gpiochip_free_valid_mask(chip);
Linus Walleij43c54ec2016-02-11 11:37:48 +01001816 /*
1817 * We accept no more calls into the driver from this point, so
1818 * NULL the driver data pointer
1819 */
1820 gdev->data = NULL;
Anton Vorontsov391c9702010-06-08 07:48:17 -06001821
Johan Hovold6798aca2015-01-12 17:12:28 +01001822 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001823 for (i = 0; i < gdev->ngpio; i++) {
Andy Shevchenko869233f2020-02-25 13:47:25 +02001824 if (gpiochip_is_requested(chip, i))
1825 break;
David Brownelld2876d02008-02-04 22:28:20 -08001826 }
David Brownelld2876d02008-02-04 22:28:20 -08001827 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001828
Geert Uytterhoevenca18a852020-03-02 09:24:48 +01001829 if (i != gdev->ngpio)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001830 dev_crit(&gdev->dev,
Linus Walleij58383c782015-11-04 09:56:26 +01001831 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +02001832
Linus Walleijff2b1352015-10-20 11:10:38 +02001833 /*
1834 * The gpiochip side puts its use of the device to rest here:
1835 * if there are no userspace clients, the chardev and device will
1836 * be removed, else it will be dangling until the last user is
1837 * gone.
1838 */
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001839 cdev_device_del(&gdev->chrdev, &gdev->dev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001840 put_device(&gdev->dev);
David Brownelld2876d02008-02-04 22:28:20 -08001841}
1842EXPORT_SYMBOL_GPL(gpiochip_remove);
1843
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301844/**
Grant Likely594fa262010-06-08 07:48:16 -06001845 * gpiochip_find() - iterator for locating a specific gpio_chip
1846 * @data: data to pass to match function
Thierry Reding950d55f52017-07-24 16:57:22 +02001847 * @match: Callback function to check gpio_chip
Grant Likely594fa262010-06-08 07:48:16 -06001848 *
1849 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1850 * determined by a user supplied @match callback. The callback should return
1851 * 0 if the device doesn't match and non-zero if it does. If the callback is
1852 * non-zero, this function will return to the caller and not iterate over any
1853 * more gpio_chips.
1854 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001855struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001856 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001857 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001858{
Linus Walleijff2b1352015-10-20 11:10:38 +02001859 struct gpio_device *gdev;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001860 struct gpio_chip *chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -06001861 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001862
1863 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001864 list_for_each_entry(gdev, &gpio_devices, list)
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001865 if (gdev->chip && match(gdev->chip, data)) {
1866 chip = gdev->chip;
Grant Likely594fa262010-06-08 07:48:16 -06001867 break;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001868 }
Linus Walleijff2b1352015-10-20 11:10:38 +02001869
Grant Likely594fa262010-06-08 07:48:16 -06001870 spin_unlock_irqrestore(&gpio_lock, flags);
1871
1872 return chip;
1873}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001874EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001875
Alexandre Courbot79697ef2013-11-16 21:39:32 +09001876static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1877{
1878 const char *name = data;
1879
1880 return !strcmp(chip->label, name);
1881}
1882
1883static struct gpio_chip *find_chip_by_name(const char *name)
1884{
1885 return gpiochip_find((void *)name, gpiochip_match_name);
1886}
1887
Linus Walleij14250522014-03-25 10:40:18 +01001888#ifdef CONFIG_GPIOLIB_IRQCHIP
1889
1890/*
1891 * The following is irqchip helper code for gpiochips.
1892 */
1893
Andy Shevchenko9411e3a2019-10-09 17:34:44 +03001894static int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
1895{
1896 struct gpio_irq_chip *girq = &gc->irq;
1897
1898 if (!girq->init_hw)
1899 return 0;
1900
1901 return girq->init_hw(gc);
1902}
1903
Linus Walleij5fbe5b52019-09-04 16:01:04 +02001904static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001905{
Linus Walleij5fbe5b52019-09-04 16:01:04 +02001906 struct gpio_irq_chip *girq = &gc->irq;
1907
1908 if (!girq->init_valid_mask)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001909 return 0;
1910
Linus Walleij5fbe5b52019-09-04 16:01:04 +02001911 girq->valid_mask = gpiochip_allocate_mask(gc);
1912 if (!girq->valid_mask)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001913 return -ENOMEM;
1914
Linus Walleij5fbe5b52019-09-04 16:01:04 +02001915 girq->init_valid_mask(gc, girq->valid_mask, gc->ngpio);
1916
Mika Westerberg79b804c2016-09-20 15:15:21 +03001917 return 0;
1918}
1919
1920static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1921{
Masahiro Yamada7bdbd1ec2019-07-18 15:51:01 +09001922 bitmap_free(gpiochip->irq.valid_mask);
Thierry Redingdc7b0382017-11-07 19:15:52 +01001923 gpiochip->irq.valid_mask = NULL;
Mika Westerberg79b804c2016-09-20 15:15:21 +03001924}
1925
Stephen Boyd64ff2c82018-01-09 17:58:46 -08001926bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
1927 unsigned int offset)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001928{
Stephen Boyd726cb3b2018-03-23 09:34:52 -07001929 if (!gpiochip_line_is_valid(gpiochip, offset))
1930 return false;
Mika Westerberg79b804c2016-09-20 15:15:21 +03001931 /* No mask means all valid */
Thierry Redingdc7b0382017-11-07 19:15:52 +01001932 if (likely(!gpiochip->irq.valid_mask))
Mika Westerberg79b804c2016-09-20 15:15:21 +03001933 return true;
Thierry Redingdc7b0382017-11-07 19:15:52 +01001934 return test_bit(offset, gpiochip->irq.valid_mask);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001935}
Stephen Boyd64ff2c82018-01-09 17:58:46 -08001936EXPORT_SYMBOL_GPL(gpiochip_irqchip_irq_valid);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001937
Linus Walleij14250522014-03-25 10:40:18 +01001938/**
Linus Walleijd245b3f2016-11-24 10:57:25 +01001939 * gpiochip_set_cascaded_irqchip() - connects a cascaded irqchip to a gpiochip
Linus Walleij4892d3a2019-06-14 10:12:26 +02001940 * @gc: the gpiochip to set the irqchip chain to
Linus Walleij14250522014-03-25 10:40:18 +01001941 * @parent_irq: the irq number corresponding to the parent IRQ for this
Linus Walleij72780ce2020-01-13 23:08:00 +01001942 * cascaded irqchip
Linus Walleij14250522014-03-25 10:40:18 +01001943 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001944 * coming out of the gpiochip. If the interrupt is nested rather than
1945 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +01001946 */
Linus Walleij4892d3a2019-06-14 10:12:26 +02001947static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gc,
Thierry Reding6f793092017-04-03 18:05:21 +02001948 unsigned int parent_irq,
Linus Walleijd245b3f2016-11-24 10:57:25 +01001949 irq_flow_handler_t parent_handler)
Linus Walleij14250522014-03-25 10:40:18 +01001950{
Linus Walleij4892d3a2019-06-14 10:12:26 +02001951 struct gpio_irq_chip *girq = &gc->irq;
1952 struct device *dev = &gc->gpiodev->dev;
1953
1954 if (!girq->domain) {
1955 chip_err(gc, "called %s before setting up irqchip\n",
Linus Walleij83141a72014-09-26 13:50:12 +02001956 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +02001957 return;
1958 }
1959
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001960 if (parent_handler) {
Linus Walleij4892d3a2019-06-14 10:12:26 +02001961 if (gc->can_sleep) {
1962 chip_err(gc,
Andy Shevchenkob1911712018-07-03 03:39:03 +03001963 "you cannot have chained interrupts on a chip that may sleep\n");
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001964 return;
1965 }
Linus Walleij4892d3a2019-06-14 10:12:26 +02001966 girq->parents = devm_kcalloc(dev, 1,
1967 sizeof(*girq->parents),
1968 GFP_KERNEL);
1969 if (!girq->parents) {
1970 chip_err(gc, "out of memory allocating parent IRQ\n");
1971 return;
1972 }
1973 girq->parents[0] = parent_irq;
1974 girq->num_parents = 1;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001975 /*
1976 * The parent irqchip is already using the chip_data for this
1977 * irqchip, so our callbacks simply use the handler_data.
1978 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +02001979 irq_set_chained_handler_and_data(parent_irq, parent_handler,
Linus Walleij4892d3a2019-06-14 10:12:26 +02001980 gc);
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001981 }
Linus Walleij14250522014-03-25 10:40:18 +01001982}
Linus Walleijd245b3f2016-11-24 10:57:25 +01001983
1984/**
Linus Walleijd245b3f2016-11-24 10:57:25 +01001985 * gpiochip_set_nested_irqchip() - connects a nested irqchip to a gpiochip
1986 * @gpiochip: the gpiochip to set the irqchip nested handler to
1987 * @irqchip: the irqchip to nest to the gpiochip
1988 * @parent_irq: the irq number corresponding to the parent IRQ for this
1989 * nested irqchip
1990 */
1991void gpiochip_set_nested_irqchip(struct gpio_chip *gpiochip,
1992 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001993 unsigned int parent_irq)
Linus Walleijd245b3f2016-11-24 10:57:25 +01001994{
Stephen Boyd3c1f6b22018-10-08 09:32:15 -07001995 gpiochip_set_cascaded_irqchip(gpiochip, parent_irq, NULL);
Linus Walleijd245b3f2016-11-24 10:57:25 +01001996}
1997EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip);
1998
Linus Walleijfdd61a02019-08-08 14:32:37 +02001999#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
2000
2001/**
2002 * gpiochip_set_hierarchical_irqchip() - connects a hierarchical irqchip
2003 * to a gpiochip
2004 * @gc: the gpiochip to set the irqchip hierarchical handler to
2005 * @irqchip: the irqchip to handle this level of the hierarchy, the interrupt
2006 * will then percolate up to the parent
2007 */
2008static void gpiochip_set_hierarchical_irqchip(struct gpio_chip *gc,
2009 struct irq_chip *irqchip)
2010{
2011 /* DT will deal with mapping each IRQ as we go along */
2012 if (is_of_node(gc->irq.fwnode))
2013 return;
2014
2015 /*
2016 * This is for legacy and boardfile "irqchip" fwnodes: allocate
2017 * irqs upfront instead of dynamically since we don't have the
2018 * dynamic type of allocation that hardware description languages
2019 * provide. Once all GPIO drivers using board files are gone from
2020 * the kernel we can delete this code, but for a transitional period
2021 * it is necessary to keep this around.
2022 */
2023 if (is_fwnode_irqchip(gc->irq.fwnode)) {
2024 int i;
2025 int ret;
2026
2027 for (i = 0; i < gc->ngpio; i++) {
2028 struct irq_fwspec fwspec;
2029 unsigned int parent_hwirq;
2030 unsigned int parent_type;
2031 struct gpio_irq_chip *girq = &gc->irq;
2032
2033 /*
2034 * We call the child to parent translation function
2035 * only to check if the child IRQ is valid or not.
2036 * Just pick the rising edge type here as that is what
2037 * we likely need to support.
2038 */
2039 ret = girq->child_to_parent_hwirq(gc, i,
2040 IRQ_TYPE_EDGE_RISING,
2041 &parent_hwirq,
2042 &parent_type);
2043 if (ret) {
2044 chip_err(gc, "skip set-up on hwirq %d\n",
2045 i);
2046 continue;
2047 }
2048
2049 fwspec.fwnode = gc->irq.fwnode;
2050 /* This is the hwirq for the GPIO line side of things */
2051 fwspec.param[0] = girq->child_offset_to_irq(gc, i);
2052 /* Just pick something */
2053 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
2054 fwspec.param_count = 2;
2055 ret = __irq_domain_alloc_irqs(gc->irq.domain,
2056 /* just pick something */
2057 -1,
2058 1,
2059 NUMA_NO_NODE,
2060 &fwspec,
2061 false,
2062 NULL);
2063 if (ret < 0) {
2064 chip_err(gc,
2065 "can not allocate irq for GPIO line %d parent hwirq %d in hierarchy domain: %d\n",
2066 i, parent_hwirq,
2067 ret);
2068 }
2069 }
2070 }
2071
2072 chip_err(gc, "%s unknown fwnode type proceed anyway\n", __func__);
2073
2074 return;
2075}
2076
2077static int gpiochip_hierarchy_irq_domain_translate(struct irq_domain *d,
2078 struct irq_fwspec *fwspec,
2079 unsigned long *hwirq,
2080 unsigned int *type)
2081{
2082 /* We support standard DT translation */
2083 if (is_of_node(fwspec->fwnode) && fwspec->param_count == 2) {
2084 return irq_domain_translate_twocell(d, fwspec, hwirq, type);
2085 }
2086
2087 /* This is for board files and others not using DT */
2088 if (is_fwnode_irqchip(fwspec->fwnode)) {
2089 int ret;
2090
2091 ret = irq_domain_translate_twocell(d, fwspec, hwirq, type);
2092 if (ret)
2093 return ret;
2094 WARN_ON(*type == IRQ_TYPE_NONE);
2095 return 0;
2096 }
2097 return -EINVAL;
2098}
2099
2100static int gpiochip_hierarchy_irq_domain_alloc(struct irq_domain *d,
2101 unsigned int irq,
2102 unsigned int nr_irqs,
2103 void *data)
2104{
2105 struct gpio_chip *gc = d->host_data;
2106 irq_hw_number_t hwirq;
2107 unsigned int type = IRQ_TYPE_NONE;
2108 struct irq_fwspec *fwspec = data;
Kevin Hao24258762020-01-14 16:28:19 +08002109 void *parent_arg;
Linus Walleijfdd61a02019-08-08 14:32:37 +02002110 unsigned int parent_hwirq;
2111 unsigned int parent_type;
2112 struct gpio_irq_chip *girq = &gc->irq;
2113 int ret;
2114
2115 /*
2116 * The nr_irqs parameter is always one except for PCI multi-MSI
2117 * so this should not happen.
2118 */
2119 WARN_ON(nr_irqs != 1);
2120
2121 ret = gc->irq.child_irq_domain_ops.translate(d, fwspec, &hwirq, &type);
2122 if (ret)
2123 return ret;
2124
Kevin Hao366950e2020-01-20 17:56:25 +08002125 chip_dbg(gc, "allocate IRQ %d, hwirq %lu\n", irq, hwirq);
Linus Walleijfdd61a02019-08-08 14:32:37 +02002126
2127 ret = girq->child_to_parent_hwirq(gc, hwirq, type,
2128 &parent_hwirq, &parent_type);
2129 if (ret) {
2130 chip_err(gc, "can't look up hwirq %lu\n", hwirq);
2131 return ret;
2132 }
Kevin Hao366950e2020-01-20 17:56:25 +08002133 chip_dbg(gc, "found parent hwirq %u\n", parent_hwirq);
Linus Walleijfdd61a02019-08-08 14:32:37 +02002134
2135 /*
2136 * We set handle_bad_irq because the .set_type() should
2137 * always be invoked and set the right type of handler.
2138 */
2139 irq_domain_set_info(d,
2140 irq,
2141 hwirq,
2142 gc->irq.chip,
2143 gc,
2144 girq->handler,
2145 NULL, NULL);
2146 irq_set_probe(irq);
2147
Linus Walleijfdd61a02019-08-08 14:32:37 +02002148 /* This parent only handles asserted level IRQs */
Kevin Hao24258762020-01-14 16:28:19 +08002149 parent_arg = girq->populate_parent_alloc_arg(gc, parent_hwirq, parent_type);
2150 if (!parent_arg)
2151 return -ENOMEM;
2152
Kevin Hao366950e2020-01-20 17:56:25 +08002153 chip_dbg(gc, "alloc_irqs_parent for %d parent hwirq %d\n",
Linus Walleijfdd61a02019-08-08 14:32:37 +02002154 irq, parent_hwirq);
Stephen Boydc34f6dc2020-01-14 15:11:03 -08002155 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key);
Kevin Hao24258762020-01-14 16:28:19 +08002156 ret = irq_domain_alloc_irqs_parent(d, irq, 1, parent_arg);
Kevin Hao880b7cf2020-01-14 16:28:20 +08002157 /*
2158 * If the parent irqdomain is msi, the interrupts have already
2159 * been allocated, so the EEXIST is good.
2160 */
2161 if (irq_domain_is_msi(d->parent) && (ret == -EEXIST))
2162 ret = 0;
Linus Walleijfdd61a02019-08-08 14:32:37 +02002163 if (ret)
2164 chip_err(gc,
2165 "failed to allocate parent hwirq %d for hwirq %lu\n",
2166 parent_hwirq, hwirq);
2167
Kevin Hao24258762020-01-14 16:28:19 +08002168 kfree(parent_arg);
Linus Walleijfdd61a02019-08-08 14:32:37 +02002169 return ret;
2170}
2171
2172static unsigned int gpiochip_child_offset_to_irq_noop(struct gpio_chip *chip,
2173 unsigned int offset)
2174{
2175 return offset;
2176}
2177
2178static void gpiochip_hierarchy_setup_domain_ops(struct irq_domain_ops *ops)
2179{
2180 ops->activate = gpiochip_irq_domain_activate;
2181 ops->deactivate = gpiochip_irq_domain_deactivate;
2182 ops->alloc = gpiochip_hierarchy_irq_domain_alloc;
2183 ops->free = irq_domain_free_irqs_common;
2184
2185 /*
2186 * We only allow overriding the translate() function for
2187 * hierarchical chips, and this should only be done if the user
2188 * really need something other than 1:1 translation.
2189 */
2190 if (!ops->translate)
2191 ops->translate = gpiochip_hierarchy_irq_domain_translate;
2192}
2193
2194static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
2195{
2196 if (!gc->irq.child_to_parent_hwirq ||
2197 !gc->irq.fwnode) {
2198 chip_err(gc, "missing irqdomain vital data\n");
2199 return -EINVAL;
2200 }
2201
2202 if (!gc->irq.child_offset_to_irq)
2203 gc->irq.child_offset_to_irq = gpiochip_child_offset_to_irq_noop;
2204
Kevin Hao24258762020-01-14 16:28:19 +08002205 if (!gc->irq.populate_parent_alloc_arg)
2206 gc->irq.populate_parent_alloc_arg =
Linus Walleijfdd61a02019-08-08 14:32:37 +02002207 gpiochip_populate_parent_fwspec_twocell;
2208
2209 gpiochip_hierarchy_setup_domain_ops(&gc->irq.child_irq_domain_ops);
2210
2211 gc->irq.domain = irq_domain_create_hierarchy(
2212 gc->irq.parent_domain,
2213 0,
2214 gc->ngpio,
2215 gc->irq.fwnode,
2216 &gc->irq.child_irq_domain_ops,
2217 gc);
2218
2219 if (!gc->irq.domain)
2220 return -ENOMEM;
2221
2222 gpiochip_set_hierarchical_irqchip(gc, gc->irq.chip);
2223
2224 return 0;
2225}
2226
2227static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
2228{
2229 return !!gc->irq.parent_domain;
2230}
2231
Kevin Hao24258762020-01-14 16:28:19 +08002232void *gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *chip,
Linus Walleijfdd61a02019-08-08 14:32:37 +02002233 unsigned int parent_hwirq,
2234 unsigned int parent_type)
2235{
Kevin Hao24258762020-01-14 16:28:19 +08002236 struct irq_fwspec *fwspec;
2237
2238 fwspec = kmalloc(sizeof(*fwspec), GFP_KERNEL);
2239 if (!fwspec)
2240 return NULL;
2241
2242 fwspec->fwnode = chip->irq.parent_domain->fwnode;
Linus Walleijfdd61a02019-08-08 14:32:37 +02002243 fwspec->param_count = 2;
2244 fwspec->param[0] = parent_hwirq;
2245 fwspec->param[1] = parent_type;
Kevin Hao24258762020-01-14 16:28:19 +08002246
2247 return fwspec;
Linus Walleijfdd61a02019-08-08 14:32:37 +02002248}
2249EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_twocell);
2250
Kevin Hao24258762020-01-14 16:28:19 +08002251void *gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *chip,
Linus Walleijfdd61a02019-08-08 14:32:37 +02002252 unsigned int parent_hwirq,
2253 unsigned int parent_type)
2254{
Kevin Hao24258762020-01-14 16:28:19 +08002255 struct irq_fwspec *fwspec;
2256
2257 fwspec = kmalloc(sizeof(*fwspec), GFP_KERNEL);
2258 if (!fwspec)
2259 return NULL;
2260
2261 fwspec->fwnode = chip->irq.parent_domain->fwnode;
Linus Walleijfdd61a02019-08-08 14:32:37 +02002262 fwspec->param_count = 4;
2263 fwspec->param[0] = 0;
2264 fwspec->param[1] = parent_hwirq;
2265 fwspec->param[2] = 0;
2266 fwspec->param[3] = parent_type;
Kevin Hao24258762020-01-14 16:28:19 +08002267
2268 return fwspec;
Linus Walleijfdd61a02019-08-08 14:32:37 +02002269}
2270EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_fourcell);
2271
2272#else
2273
2274static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
2275{
2276 return -EINVAL;
2277}
2278
2279static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
2280{
2281 return false;
2282}
2283
2284#endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
2285
Linus Walleijd245b3f2016-11-24 10:57:25 +01002286/**
Linus Walleij14250522014-03-25 10:40:18 +01002287 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
2288 * @d: the irqdomain used by this irqchip
2289 * @irq: the global irq number used by this GPIO irqchip irq
2290 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
2291 *
2292 * This function will set up the mapping for a certain IRQ line on a
2293 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
2294 * stored inside the gpiochip.
2295 */
Thierry Reding1b95b4e2017-11-07 19:15:55 +01002296int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
2297 irq_hw_number_t hwirq)
Linus Walleij14250522014-03-25 10:40:18 +01002298{
2299 struct gpio_chip *chip = d->host_data;
Linus Walleijd377f562019-07-16 11:11:45 +02002300 int ret = 0;
Linus Walleij14250522014-03-25 10:40:18 +01002301
Grygorii Strashkodc749a02017-07-21 11:49:00 -05002302 if (!gpiochip_irqchip_irq_valid(chip, hwirq))
2303 return -ENXIO;
2304
Linus Walleij14250522014-03-25 10:40:18 +01002305 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03002306 /*
2307 * This lock class tells lockdep that GPIO irqs are in a different
2308 * category than their parents, so it won't report false recursion.
2309 */
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002310 irq_set_lockdep_class(irq, chip->irq.lock_key, chip->irq.request_key);
Thierry Redingc7a0aa52017-11-07 19:15:48 +01002311 irq_set_chip_and_handler(irq, chip->irq.chip, chip->irq.handler);
Linus Walleijd245b3f2016-11-24 10:57:25 +01002312 /* Chips that use nested thread handlers have them marked */
Thierry Reding60ed54c2017-11-07 19:15:57 +01002313 if (chip->irq.threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02002314 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +01002315 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -05002316
Thierry Redinge0d89722017-11-07 19:15:54 +01002317 if (chip->irq.num_parents == 1)
Linus Walleijd377f562019-07-16 11:11:45 +02002318 ret = irq_set_parent(irq, chip->irq.parents[0]);
Thierry Redinge0d89722017-11-07 19:15:54 +01002319 else if (chip->irq.map)
Linus Walleijd377f562019-07-16 11:11:45 +02002320 ret = irq_set_parent(irq, chip->irq.map[hwirq]);
Thierry Redinge0d89722017-11-07 19:15:54 +01002321
Linus Walleijd377f562019-07-16 11:11:45 +02002322 if (ret < 0)
2323 return ret;
Thierry Redinge0d89722017-11-07 19:15:54 +01002324
Linus Walleij1333b902014-04-23 16:45:12 +02002325 /*
2326 * No set-up of the hardware will happen if IRQ_TYPE_NONE
2327 * is passed as default type.
2328 */
Thierry Reding3634eeb2017-11-07 19:15:49 +01002329 if (chip->irq.default_type != IRQ_TYPE_NONE)
2330 irq_set_irq_type(irq, chip->irq.default_type);
Linus Walleij14250522014-03-25 10:40:18 +01002331
2332 return 0;
2333}
Thierry Reding1b95b4e2017-11-07 19:15:55 +01002334EXPORT_SYMBOL_GPL(gpiochip_irq_map);
Linus Walleij14250522014-03-25 10:40:18 +01002335
Thierry Reding1b95b4e2017-11-07 19:15:55 +01002336void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
Linus Walleijc3626fd2014-03-28 20:42:01 +01002337{
Linus Walleij1c8732b2014-04-09 13:34:39 +02002338 struct gpio_chip *chip = d->host_data;
2339
Thierry Reding60ed54c2017-11-07 19:15:57 +01002340 if (chip->irq.threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02002341 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +01002342 irq_set_chip_and_handler(irq, NULL, NULL);
2343 irq_set_chip_data(irq, NULL);
2344}
Thierry Reding1b95b4e2017-11-07 19:15:55 +01002345EXPORT_SYMBOL_GPL(gpiochip_irq_unmap);
Linus Walleijc3626fd2014-03-28 20:42:01 +01002346
Linus Walleij14250522014-03-25 10:40:18 +01002347static const struct irq_domain_ops gpiochip_domain_ops = {
2348 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +01002349 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +01002350 /* Virtually all GPIO irqchips are twocell:ed */
2351 .xlate = irq_domain_xlate_twocell,
2352};
2353
Linus Walleijfdd61a02019-08-08 14:32:37 +02002354/*
2355 * TODO: move these activate/deactivate in under the hierarchicial
2356 * irqchip implementation as static once SPMI and SSBI (all external
2357 * users) are phased over.
2358 */
Brian Masneyef74f702019-01-19 15:42:42 -05002359/**
2360 * gpiochip_irq_domain_activate() - Lock a GPIO to be used as an IRQ
2361 * @domain: The IRQ domain used by this IRQ chip
2362 * @data: Outermost irq_data associated with the IRQ
2363 * @reserve: If set, only reserve an interrupt vector instead of assigning one
2364 *
2365 * This function is a wrapper that calls gpiochip_lock_as_irq() and is to be
2366 * used as the activate function for the &struct irq_domain_ops. The host_data
2367 * for the IRQ domain must be the &struct gpio_chip.
2368 */
2369int gpiochip_irq_domain_activate(struct irq_domain *domain,
2370 struct irq_data *data, bool reserve)
2371{
2372 struct gpio_chip *chip = domain->host_data;
2373
2374 return gpiochip_lock_as_irq(chip, data->hwirq);
2375}
2376EXPORT_SYMBOL_GPL(gpiochip_irq_domain_activate);
2377
2378/**
2379 * gpiochip_irq_domain_deactivate() - Unlock a GPIO used as an IRQ
2380 * @domain: The IRQ domain used by this IRQ chip
2381 * @data: Outermost irq_data associated with the IRQ
2382 *
2383 * This function is a wrapper that will call gpiochip_unlock_as_irq() and is to
2384 * be used as the deactivate function for the &struct irq_domain_ops. The
2385 * host_data for the IRQ domain must be the &struct gpio_chip.
2386 */
2387void gpiochip_irq_domain_deactivate(struct irq_domain *domain,
2388 struct irq_data *data)
2389{
2390 struct gpio_chip *chip = domain->host_data;
2391
2392 return gpiochip_unlock_as_irq(chip, data->hwirq);
2393}
2394EXPORT_SYMBOL_GPL(gpiochip_irq_domain_deactivate);
2395
Linus Walleij14250522014-03-25 10:40:18 +01002396static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
2397{
Linus Walleijfdd61a02019-08-08 14:32:37 +02002398 struct irq_domain *domain = chip->irq.domain;
2399
Grygorii Strashkodc749a02017-07-21 11:49:00 -05002400 if (!gpiochip_irqchip_irq_valid(chip, offset))
2401 return -ENXIO;
Thierry Redinge0d89722017-11-07 19:15:54 +01002402
Linus Walleijfdd61a02019-08-08 14:32:37 +02002403#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
2404 if (irq_domain_is_hierarchy(domain)) {
2405 struct irq_fwspec spec;
2406
2407 spec.fwnode = domain->fwnode;
2408 spec.param_count = 2;
2409 spec.param[0] = chip->irq.child_offset_to_irq(chip, offset);
2410 spec.param[1] = IRQ_TYPE_NONE;
2411
2412 return irq_create_fwspec_mapping(&spec);
2413 }
2414#endif
2415
2416 return irq_create_mapping(domain, offset);
Linus Walleij14250522014-03-25 10:40:18 +01002417}
2418
Hans Verkuil4e6b8232018-09-08 11:23:14 +02002419static int gpiochip_irq_reqres(struct irq_data *d)
2420{
2421 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
2422
2423 return gpiochip_reqres_irq(chip, d->hwirq);
2424}
2425
2426static void gpiochip_irq_relres(struct irq_data *d)
2427{
2428 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
2429
2430 gpiochip_relres_irq(chip, d->hwirq);
2431}
2432
Hans Verkuil461c1a72018-09-08 11:23:17 +02002433static void gpiochip_irq_enable(struct irq_data *d)
2434{
2435 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
2436
2437 gpiochip_enable_irq(chip, d->hwirq);
2438 if (chip->irq.irq_enable)
2439 chip->irq.irq_enable(d);
2440 else
2441 chip->irq.chip->irq_unmask(d);
2442}
2443
2444static void gpiochip_irq_disable(struct irq_data *d)
2445{
2446 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
2447
2448 if (chip->irq.irq_disable)
2449 chip->irq.irq_disable(d);
2450 else
2451 chip->irq.chip->irq_mask(d);
2452 gpiochip_disable_irq(chip, d->hwirq);
2453}
2454
Hans Verkuilca620f22018-09-08 11:23:15 +02002455static void gpiochip_set_irq_hooks(struct gpio_chip *gpiochip)
2456{
2457 struct irq_chip *irqchip = gpiochip->irq.chip;
2458
2459 if (!irqchip->irq_request_resources &&
2460 !irqchip->irq_release_resources) {
2461 irqchip->irq_request_resources = gpiochip_irq_reqres;
2462 irqchip->irq_release_resources = gpiochip_irq_relres;
2463 }
Hans Verkuil461c1a72018-09-08 11:23:17 +02002464 if (WARN_ON(gpiochip->irq.irq_enable))
2465 return;
Hans Verkuil171948e2018-09-14 10:36:39 +02002466 /* Check if the irqchip already has this hook... */
2467 if (irqchip->irq_enable == gpiochip_irq_enable) {
2468 /*
2469 * ...and if so, give a gentle warning that this is bad
2470 * practice.
2471 */
2472 chip_info(gpiochip,
2473 "detected irqchip that is shared with multiple gpiochips: please fix the driver.\n");
2474 return;
2475 }
Hans Verkuil461c1a72018-09-08 11:23:17 +02002476 gpiochip->irq.irq_enable = irqchip->irq_enable;
2477 gpiochip->irq.irq_disable = irqchip->irq_disable;
2478 irqchip->irq_enable = gpiochip_irq_enable;
2479 irqchip->irq_disable = gpiochip_irq_disable;
Hans Verkuilca620f22018-09-08 11:23:15 +02002480}
2481
Linus Walleij14250522014-03-25 10:40:18 +01002482/**
Thierry Redinge0d89722017-11-07 19:15:54 +01002483 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
2484 * @gpiochip: the GPIO chip to add the IRQ chip to
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002485 * @lock_key: lockdep class for IRQ lock
2486 * @request_key: lockdep class for IRQ request
Thierry Redinge0d89722017-11-07 19:15:54 +01002487 */
Thierry Reding959bc7b2017-11-07 19:15:59 +01002488static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002489 struct lock_class_key *lock_key,
2490 struct lock_class_key *request_key)
Thierry Redinge0d89722017-11-07 19:15:54 +01002491{
2492 struct irq_chip *irqchip = gpiochip->irq.chip;
Linus Walleijfdd61a02019-08-08 14:32:37 +02002493 const struct irq_domain_ops *ops = NULL;
Thierry Redinge0d89722017-11-07 19:15:54 +01002494 struct device_node *np;
2495 unsigned int type;
2496 unsigned int i;
2497
2498 if (!irqchip)
2499 return 0;
2500
2501 if (gpiochip->irq.parent_handler && gpiochip->can_sleep) {
Andy Shevchenkob1911712018-07-03 03:39:03 +03002502 chip_err(gpiochip, "you cannot have chained interrupts on a chip that may sleep\n");
Thierry Redinge0d89722017-11-07 19:15:54 +01002503 return -EINVAL;
2504 }
2505
2506 np = gpiochip->gpiodev->dev.of_node;
2507 type = gpiochip->irq.default_type;
2508
2509 /*
2510 * Specifying a default trigger is a terrible idea if DT or ACPI is
2511 * used to configure the interrupts, as you may end up with
2512 * conflicting triggers. Tell the user, and reset to NONE.
2513 */
2514 if (WARN(np && type != IRQ_TYPE_NONE,
2515 "%s: Ignoring %u default trigger\n", np->full_name, type))
2516 type = IRQ_TYPE_NONE;
2517
2518 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
2519 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
2520 "Ignoring %u default trigger\n", type);
2521 type = IRQ_TYPE_NONE;
2522 }
2523
2524 gpiochip->to_irq = gpiochip_to_irq;
2525 gpiochip->irq.default_type = type;
Thierry Reding959bc7b2017-11-07 19:15:59 +01002526 gpiochip->irq.lock_key = lock_key;
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002527 gpiochip->irq.request_key = request_key;
Thierry Redinge0d89722017-11-07 19:15:54 +01002528
Linus Walleijfdd61a02019-08-08 14:32:37 +02002529 /* If a parent irqdomain is provided, let's build a hierarchy */
2530 if (gpiochip_hierarchy_is_hierarchical(gpiochip)) {
2531 int ret = gpiochip_hierarchy_add_domain(gpiochip);
2532 if (ret)
2533 return ret;
2534 } else {
2535 /* Some drivers provide custom irqdomain ops */
2536 if (gpiochip->irq.domain_ops)
2537 ops = gpiochip->irq.domain_ops;
Thierry Redinge0d89722017-11-07 19:15:54 +01002538
Linus Walleijfdd61a02019-08-08 14:32:37 +02002539 if (!ops)
2540 ops = &gpiochip_domain_ops;
2541 gpiochip->irq.domain = irq_domain_add_simple(np,
2542 gpiochip->ngpio,
2543 gpiochip->irq.first,
2544 ops, gpiochip);
2545 if (!gpiochip->irq.domain)
2546 return -EINVAL;
2547 }
Thierry Redinge0d89722017-11-07 19:15:54 +01002548
Thierry Redinge0d89722017-11-07 19:15:54 +01002549 if (gpiochip->irq.parent_handler) {
2550 void *data = gpiochip->irq.parent_handler_data ?: gpiochip;
2551
2552 for (i = 0; i < gpiochip->irq.num_parents; i++) {
2553 /*
2554 * The parent IRQ chip is already using the chip_data
2555 * for this IRQ chip, so our callbacks simply use the
2556 * handler_data.
2557 */
2558 irq_set_chained_handler_and_data(gpiochip->irq.parents[i],
2559 gpiochip->irq.parent_handler,
2560 data);
2561 }
Thierry Redinge0d89722017-11-07 19:15:54 +01002562 }
2563
Hans Verkuilca620f22018-09-08 11:23:15 +02002564 gpiochip_set_irq_hooks(gpiochip);
2565
Thierry Redinge0d89722017-11-07 19:15:54 +01002566 acpi_gpiochip_request_interrupts(gpiochip);
2567
2568 return 0;
2569}
2570
2571/**
Linus Walleij14250522014-03-25 10:40:18 +01002572 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
2573 * @gpiochip: the gpiochip to remove the irqchip from
2574 *
2575 * This is called only from gpiochip_remove()
2576 */
2577static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
2578{
Hans Verkuilca620f22018-09-08 11:23:15 +02002579 struct irq_chip *irqchip = gpiochip->irq.chip;
Thierry Reding39e5f092017-11-07 19:15:50 +01002580 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +01002581
Mika Westerbergafa82fa2014-07-25 09:54:48 +03002582 acpi_gpiochip_free_interrupts(gpiochip);
2583
Hans Verkuilca620f22018-09-08 11:23:15 +02002584 if (irqchip && gpiochip->irq.parent_handler) {
Thierry Reding39e5f092017-11-07 19:15:50 +01002585 struct gpio_irq_chip *irq = &gpiochip->irq;
2586 unsigned int i;
2587
2588 for (i = 0; i < irq->num_parents; i++)
2589 irq_set_chained_handler_and_data(irq->parents[i],
2590 NULL, NULL);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03002591 }
2592
Linus Walleijc3626fd2014-03-28 20:42:01 +01002593 /* Remove all IRQ mappings and delete the domain */
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01002594 if (gpiochip->irq.domain) {
Thierry Reding39e5f092017-11-07 19:15:50 +01002595 unsigned int irq;
2596
Mika Westerberg79b804c2016-09-20 15:15:21 +03002597 for (offset = 0; offset < gpiochip->ngpio; offset++) {
2598 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
2599 continue;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01002600
2601 irq = irq_find_mapping(gpiochip->irq.domain, offset);
2602 irq_dispose_mapping(irq);
Mika Westerberg79b804c2016-09-20 15:15:21 +03002603 }
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01002604
2605 irq_domain_remove(gpiochip->irq.domain);
Linus Walleijc3626fd2014-03-28 20:42:01 +01002606 }
Linus Walleij14250522014-03-25 10:40:18 +01002607
Hans Verkuil461c1a72018-09-08 11:23:17 +02002608 if (irqchip) {
2609 if (irqchip->irq_request_resources == gpiochip_irq_reqres) {
2610 irqchip->irq_request_resources = NULL;
2611 irqchip->irq_release_resources = NULL;
2612 }
2613 if (irqchip->irq_enable == gpiochip_irq_enable) {
2614 irqchip->irq_enable = gpiochip->irq.irq_enable;
2615 irqchip->irq_disable = gpiochip->irq.irq_disable;
2616 }
Linus Walleij14250522014-03-25 10:40:18 +01002617 }
Hans Verkuil461c1a72018-09-08 11:23:17 +02002618 gpiochip->irq.irq_enable = NULL;
2619 gpiochip->irq.irq_disable = NULL;
Hans Verkuilca620f22018-09-08 11:23:15 +02002620 gpiochip->irq.chip = NULL;
Mika Westerberg79b804c2016-09-20 15:15:21 +03002621
2622 gpiochip_irqchip_free_valid_mask(gpiochip);
Linus Walleij14250522014-03-25 10:40:18 +01002623}
2624
2625/**
Linus Walleij739e6f52017-01-11 13:37:07 +01002626 * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01002627 * @gpiochip: the gpiochip to add the irqchip to
2628 * @irqchip: the irqchip to add to the gpiochip
2629 * @first_irq: if not dynamically assigned, the base (first) IRQ to
2630 * allocate gpiochip irqs from
2631 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +02002632 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
2633 * to have the core avoid setting up any default type in the hardware.
Thierry Reding60ed54c2017-11-07 19:15:57 +01002634 * @threaded: whether this irqchip uses a nested thread handler
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002635 * @lock_key: lockdep class for IRQ lock
2636 * @request_key: lockdep class for IRQ request
Linus Walleij14250522014-03-25 10:40:18 +01002637 *
2638 * This function closely associates a certain irqchip with a certain
2639 * gpiochip, providing an irq domain to translate the local IRQs to
2640 * global irqs in the gpiolib core, and making sure that the gpiochip
2641 * is passed as chip data to all related functions. Driver callbacks
Linus Walleij09dd5f92015-12-07 15:31:58 +01002642 * need to use gpiochip_get_data() to get their local state containers back
Linus Walleij14250522014-03-25 10:40:18 +01002643 * from the gpiochip passed as chip data. An irqdomain will be stored
2644 * in the gpiochip that shall be used by the driver to handle IRQ number
2645 * translation. The gpiochip will need to be initialized and registered
2646 * before calling this function.
2647 *
Linus Walleijc3626fd2014-03-28 20:42:01 +01002648 * This function will handle two cell:ed simple IRQs and assumes all
2649 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +01002650 * need to be open coded.
2651 */
Linus Walleij739e6f52017-01-11 13:37:07 +01002652int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
2653 struct irq_chip *irqchip,
2654 unsigned int first_irq,
2655 irq_flow_handler_t handler,
2656 unsigned int type,
Thierry Reding60ed54c2017-11-07 19:15:57 +01002657 bool threaded,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002658 struct lock_class_key *lock_key,
2659 struct lock_class_key *request_key)
Linus Walleij14250522014-03-25 10:40:18 +01002660{
2661 struct device_node *of_node;
Linus Walleij14250522014-03-25 10:40:18 +01002662
2663 if (!gpiochip || !irqchip)
2664 return -EINVAL;
2665
Linus Walleij58383c782015-11-04 09:56:26 +01002666 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +01002667 pr_err("missing gpiochip .dev parent pointer\n");
2668 return -EINVAL;
2669 }
Thierry Reding60ed54c2017-11-07 19:15:57 +01002670 gpiochip->irq.threaded = threaded;
Linus Walleij58383c782015-11-04 09:56:26 +01002671 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +01002672#ifdef CONFIG_OF_GPIO
2673 /*
Colin Cronin20a8a962015-05-18 11:41:43 -07002674 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08002675 * FIXME: get rid of this and use gpiochip->parent->of_node
2676 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +01002677 */
2678 if (gpiochip->of_node)
2679 of_node = gpiochip->of_node;
2680#endif
Marc Zyngier332e99d2016-09-07 09:12:11 +01002681 /*
Mika Westerberg0a1e0052016-09-12 14:29:51 +03002682 * Specifying a default trigger is a terrible idea if DT or ACPI is
Marc Zyngier332e99d2016-09-07 09:12:11 +01002683 * used to configure the interrupts, as you may end-up with
2684 * conflicting triggers. Tell the user, and reset to NONE.
2685 */
2686 if (WARN(of_node && type != IRQ_TYPE_NONE,
Rob Herring7eb6ce22017-07-18 16:43:03 -05002687 "%pOF: Ignoring %d default trigger\n", of_node, type))
Marc Zyngier332e99d2016-09-07 09:12:11 +01002688 type = IRQ_TYPE_NONE;
Mika Westerberg0a1e0052016-09-12 14:29:51 +03002689 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
2690 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
2691 "Ignoring %d default trigger\n", type);
2692 type = IRQ_TYPE_NONE;
2693 }
Marc Zyngier332e99d2016-09-07 09:12:11 +01002694
Thierry Redingda80ff82017-11-07 19:15:46 +01002695 gpiochip->irq.chip = irqchip;
Thierry Redingc7a0aa52017-11-07 19:15:48 +01002696 gpiochip->irq.handler = handler;
Thierry Reding3634eeb2017-11-07 19:15:49 +01002697 gpiochip->irq.default_type = type;
Linus Walleij14250522014-03-25 10:40:18 +01002698 gpiochip->to_irq = gpiochip_to_irq;
Thierry Redingca9df052017-11-07 19:15:53 +01002699 gpiochip->irq.lock_key = lock_key;
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002700 gpiochip->irq.request_key = request_key;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01002701 gpiochip->irq.domain = irq_domain_add_simple(of_node,
Linus Walleij14250522014-03-25 10:40:18 +01002702 gpiochip->ngpio, first_irq,
2703 &gpiochip_domain_ops, gpiochip);
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01002704 if (!gpiochip->irq.domain) {
Thierry Redingda80ff82017-11-07 19:15:46 +01002705 gpiochip->irq.chip = NULL;
Linus Walleij14250522014-03-25 10:40:18 +01002706 return -EINVAL;
2707 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +02002708
Hans Verkuilca620f22018-09-08 11:23:15 +02002709 gpiochip_set_irq_hooks(gpiochip);
Linus Walleij14250522014-03-25 10:40:18 +01002710
Mika Westerbergafa82fa2014-07-25 09:54:48 +03002711 acpi_gpiochip_request_interrupts(gpiochip);
2712
Linus Walleij14250522014-03-25 10:40:18 +01002713 return 0;
2714}
Linus Walleij739e6f52017-01-11 13:37:07 +01002715EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key);
Linus Walleij14250522014-03-25 10:40:18 +01002716
2717#else /* CONFIG_GPIOLIB_IRQCHIP */
2718
Thierry Reding959bc7b2017-11-07 19:15:59 +01002719static inline int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002720 struct lock_class_key *lock_key,
2721 struct lock_class_key *request_key)
Thierry Redinge0d89722017-11-07 19:15:54 +01002722{
2723 return 0;
2724}
Linus Walleij14250522014-03-25 10:40:18 +01002725static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
Andy Shevchenko9411e3a2019-10-09 17:34:44 +03002726
2727static inline int gpiochip_irqchip_init_hw(struct gpio_chip *gpiochip)
2728{
2729 return 0;
2730}
2731
Mika Westerberg79b804c2016-09-20 15:15:21 +03002732static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
2733{
2734 return 0;
2735}
2736static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
2737{ }
Linus Walleij14250522014-03-25 10:40:18 +01002738
2739#endif /* CONFIG_GPIOLIB_IRQCHIP */
2740
Jonas Gorskic771c2f2015-10-11 17:34:15 +02002741/**
2742 * gpiochip_generic_request() - request the gpio function for a pin
2743 * @chip: the gpiochip owning the GPIO
2744 * @offset: the offset of the GPIO to request for GPIO function
2745 */
2746int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
2747{
Thierry Reding2ab73c62020-03-19 13:27:29 +01002748 if (!list_empty(&chip->gpiodev->pin_ranges))
2749 return pinctrl_gpio_request(chip->gpiodev->base + offset);
2750
2751 return 0;
Jonas Gorskic771c2f2015-10-11 17:34:15 +02002752}
2753EXPORT_SYMBOL_GPL(gpiochip_generic_request);
2754
2755/**
2756 * gpiochip_generic_free() - free the gpio function from a pin
2757 * @chip: the gpiochip to request the gpio function for
2758 * @offset: the offset of the GPIO to free from GPIO function
2759 */
2760void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
2761{
Linus Walleija9a1d2a2017-09-22 11:02:10 +02002762 pinctrl_gpio_free(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02002763}
2764EXPORT_SYMBOL_GPL(gpiochip_generic_free);
2765
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002766/**
2767 * gpiochip_generic_config() - apply configuration for a pin
2768 * @chip: the gpiochip owning the GPIO
2769 * @offset: the offset of the GPIO to apply the configuration
2770 * @config: the configuration to be applied
2771 */
2772int gpiochip_generic_config(struct gpio_chip *chip, unsigned offset,
2773 unsigned long config)
2774{
2775 return pinctrl_gpio_set_config(chip->gpiodev->base + offset, config);
2776}
2777EXPORT_SYMBOL_GPL(gpiochip_generic_config);
2778
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302779#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01002780
Linus Walleij3f0f8672012-11-20 12:40:15 +01002781/**
Christian Ruppert586a87e2013-10-15 15:37:54 +02002782 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
2783 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +02002784 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +02002785 * @gpio_offset: the start offset in the current gpio_chip number space
2786 * @pin_group: name of the pin group inside the pin controller
Christian Lamparter973c1712018-05-21 22:57:39 +02002787 *
2788 * Calling this function directly from a DeviceTree-supported
2789 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
2790 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
2791 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
Christian Ruppert586a87e2013-10-15 15:37:54 +02002792 */
2793int gpiochip_add_pingroup_range(struct gpio_chip *chip,
2794 struct pinctrl_dev *pctldev,
2795 unsigned int gpio_offset, const char *pin_group)
2796{
2797 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002798 struct gpio_device *gdev = chip->gpiodev;
Christian Ruppert586a87e2013-10-15 15:37:54 +02002799 int ret;
2800
2801 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
2802 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002803 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +02002804 return -ENOMEM;
2805 }
2806
2807 /* Use local offset as range ID */
2808 pin_range->range.id = gpio_offset;
2809 pin_range->range.gc = chip;
2810 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002811 pin_range->range.base = gdev->base + gpio_offset;
Christian Ruppert586a87e2013-10-15 15:37:54 +02002812 pin_range->pctldev = pctldev;
2813
2814 ret = pinctrl_get_group_pins(pctldev, pin_group,
2815 &pin_range->range.pins,
2816 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +01002817 if (ret < 0) {
2818 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +02002819 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +01002820 }
Christian Ruppert586a87e2013-10-15 15:37:54 +02002821
2822 pinctrl_add_gpio_range(pctldev, &pin_range->range);
2823
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002824 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
2825 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +02002826 pinctrl_dev_get_devname(pctldev), pin_group);
2827
Linus Walleij20ec3e32016-02-11 11:03:06 +01002828 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Christian Ruppert586a87e2013-10-15 15:37:54 +02002829
2830 return 0;
2831}
2832EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
2833
2834/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01002835 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
2836 * @chip: the gpiochip to add the range for
Thierry Reding950d55f52017-07-24 16:57:22 +02002837 * @pinctl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01002838 * @gpio_offset: the start offset in the current gpio_chip number space
2839 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01002840 * @npins: the number of pins from the offset of each pin space (GPIO and
2841 * pin controller) to accumulate in this range
Thierry Reding950d55f52017-07-24 16:57:22 +02002842 *
2843 * Returns:
2844 * 0 on success, or a negative error-code on failure.
Christian Lamparter973c1712018-05-21 22:57:39 +02002845 *
2846 * Calling this function directly from a DeviceTree-supported
2847 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
2848 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
2849 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
Linus Walleij3f0f8672012-11-20 12:40:15 +01002850 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002851int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01002852 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01002853 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302854{
2855 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002856 struct gpio_device *gdev = chip->gpiodev;
Axel Linb4d4b1f2012-11-21 14:33:56 +08002857 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302858
Linus Walleij3f0f8672012-11-20 12:40:15 +01002859 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302860 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002861 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002862 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302863 }
2864
Linus Walleij3f0f8672012-11-20 12:40:15 +01002865 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01002866 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01002867 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302868 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002869 pin_range->range.base = gdev->base + gpio_offset;
Linus Walleij316511c2012-11-21 08:48:09 +01002870 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302871 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01002872 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302873 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01002874 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08002875 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002876 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +01002877 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08002878 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01002879 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002880 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
2881 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +01002882 pinctl_name,
2883 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302884
Linus Walleij20ec3e32016-02-11 11:03:06 +01002885 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002886
2887 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302888}
Linus Walleij165adc92012-11-06 14:49:39 +01002889EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302890
Linus Walleij3f0f8672012-11-20 12:40:15 +01002891/**
2892 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
2893 * @chip: the chip to remove all the mappings for
2894 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302895void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
2896{
2897 struct gpio_pin_range *pin_range, *tmp;
Linus Walleij20ec3e32016-02-11 11:03:06 +01002898 struct gpio_device *gdev = chip->gpiodev;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302899
Linus Walleij20ec3e32016-02-11 11:03:06 +01002900 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302901 list_del(&pin_range->node);
2902 pinctrl_remove_gpio_range(pin_range->pctldev,
2903 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01002904 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302905 }
2906}
Linus Walleij165adc92012-11-06 14:49:39 +01002907EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
2908
2909#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302910
David Brownelld2876d02008-02-04 22:28:20 -08002911/* These "optional" allocation calls help prevent drivers from stomping
2912 * on each other, and help provide better diagnostics in debugfs.
2913 * They're called even less than the "set direction" calls.
2914 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002915static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08002916{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002917 struct gpio_chip *chip = desc->gdev->chip;
Linus Walleijd377f562019-07-16 11:11:45 +02002918 int ret;
David Brownelld2876d02008-02-04 22:28:20 -08002919 unsigned long flags;
Biju Das3789f5a2018-08-07 08:15:18 +01002920 unsigned offset;
David Brownelld2876d02008-02-04 22:28:20 -08002921
Muchun Song18534df2018-11-01 21:12:50 +08002922 if (label) {
2923 label = kstrdup_const(label, GFP_KERNEL);
2924 if (!label)
2925 return -ENOMEM;
2926 }
2927
David Brownelld2876d02008-02-04 22:28:20 -08002928 spin_lock_irqsave(&gpio_lock, flags);
2929
David Brownelld2876d02008-02-04 22:28:20 -08002930 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07002931 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08002932 */
2933
2934 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
2935 desc_set_label(desc, label ? : "?");
Linus Walleijd377f562019-07-16 11:11:45 +02002936 ret = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07002937 } else {
Muchun Song18534df2018-11-01 21:12:50 +08002938 kfree_const(label);
Linus Walleijd377f562019-07-16 11:11:45 +02002939 ret = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -08002940 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07002941 }
2942
2943 if (chip->request) {
2944 /* chip->request may sleep */
2945 spin_unlock_irqrestore(&gpio_lock, flags);
Biju Das3789f5a2018-08-07 08:15:18 +01002946 offset = gpio_chip_hwgpio(desc);
2947 if (gpiochip_line_is_valid(chip, offset))
Linus Walleijd377f562019-07-16 11:11:45 +02002948 ret = chip->request(chip, offset);
Biju Das3789f5a2018-08-07 08:15:18 +01002949 else
Linus Walleijd377f562019-07-16 11:11:45 +02002950 ret = -EINVAL;
David Brownell35e8bb52008-10-15 22:03:16 -07002951 spin_lock_irqsave(&gpio_lock, flags);
2952
Linus Walleijd377f562019-07-16 11:11:45 +02002953 if (ret < 0) {
David Brownell35e8bb52008-10-15 22:03:16 -07002954 desc_set_label(desc, NULL);
Muchun Song18534df2018-11-01 21:12:50 +08002955 kfree_const(label);
David Brownell35e8bb52008-10-15 22:03:16 -07002956 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03002957 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07002958 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07002959 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03002960 if (chip->get_direction) {
2961 /* chip->get_direction may sleep */
2962 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002963 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03002964 spin_lock_irqsave(&gpio_lock, flags);
2965 }
David Brownelld2876d02008-02-04 22:28:20 -08002966done:
Mika Westerberg77c2d792014-03-10 14:54:50 +02002967 spin_unlock_irqrestore(&gpio_lock, flags);
Bartosz Golaszewski51c10642019-11-22 15:19:21 +01002968 atomic_notifier_call_chain(&desc->gdev->notifier,
2969 GPIOLINE_CHANGED_REQUESTED, desc);
Linus Walleijd377f562019-07-16 11:11:45 +02002970 return ret;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002971}
2972
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002973/*
2974 * This descriptor validation needs to be inserted verbatim into each
2975 * function taking a descriptor, so we need to use a preprocessor
Linus Walleij54d77192016-05-30 16:48:39 +02002976 * macro to avoid endless duplication. If the desc is NULL it is an
2977 * optional GPIO and calls should just bail out.
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002978 */
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002979static int validate_desc(const struct gpio_desc *desc, const char *func)
2980{
2981 if (!desc)
2982 return 0;
2983 if (IS_ERR(desc)) {
2984 pr_warn("%s: invalid GPIO (errorpointer)\n", func);
2985 return PTR_ERR(desc);
2986 }
2987 if (!desc->gdev) {
2988 pr_warn("%s: invalid GPIO (no device)\n", func);
2989 return -EINVAL;
2990 }
2991 if (!desc->gdev->chip) {
2992 dev_warn(&desc->gdev->dev,
2993 "%s: backing chip is gone\n", func);
2994 return 0;
2995 }
2996 return 1;
2997}
2998
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002999#define VALIDATE_DESC(desc) do { \
Rasmus Villemoesa746a232017-12-21 01:27:04 +01003000 int __valid = validate_desc(desc, __func__); \
3001 if (__valid <= 0) \
3002 return __valid; \
3003 } while (0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003004
3005#define VALIDATE_DESC_VOID(desc) do { \
Rasmus Villemoesa746a232017-12-21 01:27:04 +01003006 int __valid = validate_desc(desc, __func__); \
3007 if (__valid <= 0) \
Linus Walleij54d77192016-05-30 16:48:39 +02003008 return; \
Rasmus Villemoesa746a232017-12-21 01:27:04 +01003009 } while (0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003010
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09003011int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02003012{
Linus Walleijd377f562019-07-16 11:11:45 +02003013 int ret = -EPROBE_DEFER;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003014 struct gpio_device *gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02003015
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003016 VALIDATE_DESC(desc);
3017 gdev = desc->gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02003018
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003019 if (try_module_get(gdev->owner)) {
Linus Walleijd377f562019-07-16 11:11:45 +02003020 ret = gpiod_request_commit(desc, label);
3021 if (ret < 0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003022 module_put(gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01003023 else
3024 get_device(&gdev->dev);
Mika Westerberg77c2d792014-03-10 14:54:50 +02003025 }
3026
Linus Walleijd377f562019-07-16 11:11:45 +02003027 if (ret)
3028 gpiod_dbg(desc, "%s: status %d\n", __func__, ret);
Mika Westerberg77c2d792014-03-10 14:54:50 +02003029
Linus Walleijd377f562019-07-16 11:11:45 +02003030 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08003031}
Alexandre Courbot372e7222013-02-03 01:29:29 +09003032
Linus Walleijfac9d882017-09-26 20:58:28 +02003033static bool gpiod_free_commit(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08003034{
Mika Westerberg77c2d792014-03-10 14:54:50 +02003035 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08003036 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07003037 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08003038
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07003039 might_sleep();
3040
Alexandre Courbot372e7222013-02-03 01:29:29 +09003041 gpiod_unexport(desc);
David Brownelld8f388d82008-07-25 01:46:07 -07003042
David Brownelld2876d02008-02-04 22:28:20 -08003043 spin_lock_irqsave(&gpio_lock, flags);
3044
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003045 chip = desc->gdev->chip;
David Brownell35e8bb52008-10-15 22:03:16 -07003046 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
3047 if (chip->free) {
3048 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07003049 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003050 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07003051 spin_lock_irqsave(&gpio_lock, flags);
3052 }
Muchun Song18534df2018-11-01 21:12:50 +08003053 kfree_const(desc->label);
David Brownelld2876d02008-02-04 22:28:20 -08003054 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08003055 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07003056 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303057 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303058 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Drew Fustini9225d512019-11-05 10:04:23 +08003059 clear_bit(FLAG_PULL_UP, &desc->flags);
3060 clear_bit(FLAG_PULL_DOWN, &desc->flags);
Kent Gibson2148ad72019-11-05 10:04:25 +08003061 clear_bit(FLAG_BIAS_DISABLE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06003062 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Geert Uytterhoeven63636d92020-02-20 14:01:49 +01003063#ifdef CONFIG_OF_DYNAMIC
3064 desc->hog = NULL;
3065#endif
Mika Westerberg77c2d792014-03-10 14:54:50 +02003066 ret = true;
3067 }
David Brownelld2876d02008-02-04 22:28:20 -08003068
3069 spin_unlock_irqrestore(&gpio_lock, flags);
Bartosz Golaszewski51c10642019-11-22 15:19:21 +01003070 atomic_notifier_call_chain(&desc->gdev->notifier,
3071 GPIOLINE_CHANGED_RELEASED, desc);
3072
Mika Westerberg77c2d792014-03-10 14:54:50 +02003073 return ret;
3074}
3075
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09003076void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02003077{
Linus Walleijfac9d882017-09-26 20:58:28 +02003078 if (desc && desc->gdev && gpiod_free_commit(desc)) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003079 module_put(desc->gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01003080 put_device(&desc->gdev->dev);
3081 } else {
Mika Westerberg77c2d792014-03-10 14:54:50 +02003082 WARN_ON(extra_checks);
Linus Walleij33a68e82016-02-11 10:28:44 +01003083 }
David Brownelld2876d02008-02-04 22:28:20 -08003084}
Alexandre Courbot372e7222013-02-03 01:29:29 +09003085
David Brownelld2876d02008-02-04 22:28:20 -08003086/**
3087 * gpiochip_is_requested - return string iff signal was requested
3088 * @chip: controller managing the signal
3089 * @offset: of signal within controller's 0..(ngpio - 1) range
3090 *
3091 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09003092 * The string returned is the label passed to gpio_request(); if none has been
3093 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08003094 *
3095 * This function is for use by GPIO controller drivers. The label can
3096 * help with diagnostics, and knowing that the signal is used as a GPIO
3097 * can help avoid accidentally multiplexing it to another controller.
3098 */
3099const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
3100{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09003101 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08003102
Dirk Behme48b59532015-08-18 18:02:32 +02003103 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08003104 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09003105
Bartosz Golaszewski1739a2d2020-02-19 10:47:02 +01003106 desc = gpiochip_get_desc(chip, offset);
3107 if (IS_ERR(desc))
3108 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09003109
Alexandre Courbot372e7222013-02-03 01:29:29 +09003110 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08003111 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003112 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08003113}
3114EXPORT_SYMBOL_GPL(gpiochip_is_requested);
3115
Mika Westerberg77c2d792014-03-10 14:54:50 +02003116/**
3117 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
Thierry Reding950d55f52017-07-24 16:57:22 +02003118 * @chip: GPIO chip
3119 * @hwnum: hardware number of the GPIO for which to request the descriptor
Mika Westerberg77c2d792014-03-10 14:54:50 +02003120 * @label: label for the GPIO
Linus Walleij5923ea62019-04-26 14:40:18 +02003121 * @lflags: lookup flags for this GPIO or 0 if default, this can be used to
3122 * specify things like line inversion semantics with the machine flags
3123 * such as GPIO_OUT_LOW
3124 * @dflags: descriptor request flags for this GPIO or 0 if default, this
3125 * can be used to specify consumer semantics such as open drain
Mika Westerberg77c2d792014-03-10 14:54:50 +02003126 *
3127 * Function allows GPIO chip drivers to request and use their own GPIO
3128 * descriptors via gpiolib API. Difference to gpiod_request() is that this
3129 * function will not increase reference count of the GPIO chip module. This
3130 * allows the GPIO chip module to be unloaded as needed (we assume that the
3131 * GPIO chip driver handles freeing the GPIOs it has requested).
Thierry Reding950d55f52017-07-24 16:57:22 +02003132 *
3133 * Returns:
3134 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
3135 * code on failure.
Mika Westerberg77c2d792014-03-10 14:54:50 +02003136 */
Bartosz Golaszewski06863622019-12-24 13:06:59 +01003137struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip,
3138 unsigned int hwnum,
Linus Walleij21abf102018-09-04 13:31:45 +02003139 const char *label,
Linus Walleij5923ea62019-04-26 14:40:18 +02003140 enum gpio_lookup_flags lflags,
3141 enum gpiod_flags dflags)
Mika Westerberg77c2d792014-03-10 14:54:50 +02003142{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07003143 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
Linus Walleijd377f562019-07-16 11:11:45 +02003144 int ret;
Mika Westerberg77c2d792014-03-10 14:54:50 +02003145
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07003146 if (IS_ERR(desc)) {
3147 chip_err(chip, "failed to get GPIO descriptor\n");
3148 return desc;
3149 }
3150
Linus Walleijd377f562019-07-16 11:11:45 +02003151 ret = gpiod_request_commit(desc, label);
3152 if (ret < 0)
3153 return ERR_PTR(ret);
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07003154
Linus Walleijd377f562019-07-16 11:11:45 +02003155 ret = gpiod_configure_flags(desc, label, lflags, dflags);
3156 if (ret) {
Linus Walleij21abf102018-09-04 13:31:45 +02003157 chip_err(chip, "setup of own GPIO %s failed\n", label);
3158 gpiod_free_commit(desc);
Linus Walleijd377f562019-07-16 11:11:45 +02003159 return ERR_PTR(ret);
Linus Walleij21abf102018-09-04 13:31:45 +02003160 }
3161
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07003162 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02003163}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07003164EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02003165
3166/**
3167 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
3168 * @desc: GPIO descriptor to free
3169 *
3170 * Function frees the given GPIO requested previously with
3171 * gpiochip_request_own_desc().
3172 */
3173void gpiochip_free_own_desc(struct gpio_desc *desc)
3174{
3175 if (desc)
Linus Walleijfac9d882017-09-26 20:58:28 +02003176 gpiod_free_commit(desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02003177}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07003178EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08003179
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003180/*
3181 * Drivers MUST set GPIO direction before making get/set calls. In
David Brownelld2876d02008-02-04 22:28:20 -08003182 * some cases this is done in early boot, before IRQs are enabled.
3183 *
3184 * As a rule these aren't called more than once (except for drivers
3185 * using the open-drain emulation idiom) so these are natural places
3186 * to accumulate extra debugging checks. Note that we can't (yet)
3187 * rely on gpio_request() having been called beforehand.
3188 */
3189
Bartosz Golaszewskid99f8872020-02-03 14:03:37 +01003190static int gpio_do_set_config(struct gpio_chip *gc, unsigned int offset,
Bartosz Golaszewski62adc6f2020-02-03 14:16:16 +01003191 unsigned long config)
Thomas Petazzoni71479782019-02-07 17:28:56 +01003192{
Bartosz Golaszewskid90f3682019-12-24 13:06:58 +01003193 if (!gc->set_config)
3194 return -ENOTSUPP;
Thomas Petazzoni71479782019-02-07 17:28:56 +01003195
Bartosz Golaszewski62adc6f2020-02-03 14:16:16 +01003196 return gc->set_config(gc, offset, config);
Thomas Petazzoni71479782019-02-07 17:28:56 +01003197}
3198
Geert Uytterhoeven83522352020-03-25 11:04:38 +01003199static int gpio_set_config(struct gpio_desc *desc, enum pin_config_param mode)
Bartosz Golaszewskid99f8872020-02-03 14:03:37 +01003200{
Geert Uytterhoeven83522352020-03-25 11:04:38 +01003201 struct gpio_chip *chip = desc->gdev->chip;
Bartosz Golaszewski91b4ea52020-02-03 14:03:47 +01003202 unsigned long config;
Bartosz Golaszewskid99f8872020-02-03 14:03:37 +01003203 unsigned arg;
3204
3205 switch (mode) {
Bartosz Golaszewskid99f8872020-02-03 14:03:37 +01003206 case PIN_CONFIG_BIAS_PULL_DOWN:
3207 case PIN_CONFIG_BIAS_PULL_UP:
3208 arg = 1;
3209 break;
3210
3211 default:
3212 arg = 0;
3213 }
3214
Bartosz Golaszewski91b4ea52020-02-03 14:03:47 +01003215 config = PIN_CONF_PACKED(mode, arg);
Geert Uytterhoeven83522352020-03-25 11:04:38 +01003216 return gpio_do_set_config(chip, gpio_chip_hwgpio(desc), config);
David Brownelld2876d02008-02-04 22:28:20 -08003217}
3218
Geert Uytterhoeven5f4bf172020-03-25 11:04:39 +01003219static int gpio_set_bias(struct gpio_desc *desc)
Kent Gibson2148ad72019-11-05 10:04:25 +08003220{
3221 int bias = 0;
3222 int ret = 0;
3223
3224 if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
3225 bias = PIN_CONFIG_BIAS_DISABLE;
3226 else if (test_bit(FLAG_PULL_UP, &desc->flags))
3227 bias = PIN_CONFIG_BIAS_PULL_UP;
3228 else if (test_bit(FLAG_PULL_DOWN, &desc->flags))
3229 bias = PIN_CONFIG_BIAS_PULL_DOWN;
3230
3231 if (bias) {
Geert Uytterhoeven83522352020-03-25 11:04:38 +01003232 ret = gpio_set_config(desc, bias);
Kent Gibson2148ad72019-11-05 10:04:25 +08003233 if (ret != -ENOTSUPP)
3234 return ret;
3235 }
3236 return 0;
3237}
3238
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003239/**
3240 * gpiod_direction_input - set the GPIO direction to input
3241 * @desc: GPIO to set to input
3242 *
3243 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
3244 * be called safely on it.
3245 *
3246 * Return 0 in case of success, else an error code.
3247 */
3248int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08003249{
David Brownelld2876d02008-02-04 22:28:20 -08003250 struct gpio_chip *chip;
Linus Walleijd377f562019-07-16 11:11:45 +02003251 int ret = 0;
David Brownelld2876d02008-02-04 22:28:20 -08003252
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003253 VALIDATE_DESC(desc);
3254 chip = desc->gdev->chip;
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09003255
Linus Walleije48d1942018-09-25 09:54:14 +02003256 /*
3257 * It is legal to have no .get() and .direction_input() specified if
3258 * the chip is output-only, but you can't specify .direction_input()
3259 * and not support the .get() operation, that doesn't make sense.
3260 */
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003261 if (!chip->get && chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01003262 gpiod_warn(desc,
Linus Walleije48d1942018-09-25 09:54:14 +02003263 "%s: missing get() but have direction_input()\n",
3264 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02003265 return -EIO;
3266 }
3267
Linus Walleije48d1942018-09-25 09:54:14 +02003268 /*
3269 * If we have a .direction_input() callback, things are simple,
3270 * just call it. Else we are some input-only chip so try to check the
3271 * direction (if .get_direction() is supported) else we silently
3272 * assume we are in input mode after this.
3273 */
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003274 if (chip->direction_input) {
Linus Walleijd377f562019-07-16 11:11:45 +02003275 ret = chip->direction_input(chip, gpio_chip_hwgpio(desc));
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003276 } else if (chip->get_direction &&
3277 (chip->get_direction(chip, gpio_chip_hwgpio(desc)) != 1)) {
3278 gpiod_warn(desc,
Linus Walleije48d1942018-09-25 09:54:14 +02003279 "%s: missing direction_input() operation and line is output\n",
3280 __func__);
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003281 return -EIO;
3282 }
Kent Gibson2148ad72019-11-05 10:04:25 +08003283 if (ret == 0) {
David Brownelld2876d02008-02-04 22:28:20 -08003284 clear_bit(FLAG_IS_OUT, &desc->flags);
Geert Uytterhoeven5f4bf172020-03-25 11:04:39 +01003285 ret = gpio_set_bias(desc);
Kent Gibson2148ad72019-11-05 10:04:25 +08003286 }
Thomas Petazzonid4499912019-02-07 17:28:58 +01003287
Linus Walleijd377f562019-07-16 11:11:45 +02003288 trace_gpio_direction(desc_to_gpio(desc), 1, ret);
Alexandre Courbotd82da792014-07-22 16:17:43 +09003289
Linus Walleijd377f562019-07-16 11:11:45 +02003290 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08003291}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003292EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003293
Linus Walleijfac9d882017-09-26 20:58:28 +02003294static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08003295{
Linus Walleijc663e5f2016-03-22 10:51:16 +01003296 struct gpio_chip *gc = desc->gdev->chip;
Linus Walleijad177312016-11-13 23:02:44 +01003297 int val = !!value;
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003298 int ret = 0;
David Brownelld2876d02008-02-04 22:28:20 -08003299
Linus Walleije48d1942018-09-25 09:54:14 +02003300 /*
3301 * It's OK not to specify .direction_output() if the gpiochip is
3302 * output-only, but if there is then not even a .set() operation it
3303 * is pretty tricky to drive the output line.
3304 */
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003305 if (!gc->set && !gc->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01003306 gpiod_warn(desc,
Linus Walleije48d1942018-09-25 09:54:14 +02003307 "%s: missing set() and direction_output() operations\n",
3308 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02003309 return -EIO;
3310 }
3311
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003312 if (gc->direction_output) {
3313 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
3314 } else {
Linus Walleije48d1942018-09-25 09:54:14 +02003315 /* Check that we are in output mode if we can */
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003316 if (gc->get_direction &&
3317 gc->get_direction(gc, gpio_chip_hwgpio(desc))) {
3318 gpiod_warn(desc,
3319 "%s: missing direction_output() operation\n",
3320 __func__);
3321 return -EIO;
3322 }
Linus Walleije48d1942018-09-25 09:54:14 +02003323 /*
3324 * If we can't actively set the direction, we are some
3325 * output-only chip, so just drive the output as desired.
3326 */
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003327 gc->set(gc, gpio_chip_hwgpio(desc), val);
3328 }
3329
Linus Walleijc663e5f2016-03-22 10:51:16 +01003330 if (!ret)
David Brownelld2876d02008-02-04 22:28:20 -08003331 set_bit(FLAG_IS_OUT, &desc->flags);
Linus Walleijad177312016-11-13 23:02:44 +01003332 trace_gpio_value(desc_to_gpio(desc), 0, val);
Linus Walleijc663e5f2016-03-22 10:51:16 +01003333 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
3334 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08003335}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01003336
3337/**
3338 * gpiod_direction_output_raw - set the GPIO direction to output
3339 * @desc: GPIO to set to output
3340 * @value: initial output value of the GPIO
3341 *
3342 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
3343 * be called safely on it. The initial value of the output must be specified
3344 * as raw value on the physical line without regard for the ACTIVE_LOW status.
3345 *
3346 * Return 0 in case of success, else an error code.
3347 */
3348int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
3349{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003350 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003351 return gpiod_direction_output_raw_commit(desc, value);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01003352}
3353EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
3354
3355/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05303356 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01003357 * @desc: GPIO to set to output
3358 * @value: initial output value of the GPIO
3359 *
3360 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
3361 * be called safely on it. The initial value of the output must be specified
3362 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3363 * account.
3364 *
3365 * Return 0 in case of success, else an error code.
3366 */
3367int gpiod_direction_output(struct gpio_desc *desc, int value)
3368{
Linus Walleij02e47982017-09-26 21:20:23 +02003369 int ret;
3370
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003371 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01003372 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3373 value = !value;
Linus Walleijad177312016-11-13 23:02:44 +01003374 else
3375 value = !!value;
Linus Walleij02e47982017-09-26 21:20:23 +02003376
Hans Verkuil4e9439d2018-09-08 11:23:16 +02003377 /* GPIOs used for enabled IRQs shall not be set as output */
3378 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags) &&
3379 test_bit(FLAG_IRQ_IS_ENABLED, &desc->flags)) {
Linus Walleij02e47982017-09-26 21:20:23 +02003380 gpiod_err(desc,
3381 "%s: tried to set a GPIO tied to an IRQ as output\n",
3382 __func__);
3383 return -EIO;
3384 }
3385
3386 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
3387 /* First see if we can enable open drain in hardware */
Geert Uytterhoeven83522352020-03-25 11:04:38 +01003388 ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_DRAIN);
Linus Walleij02e47982017-09-26 21:20:23 +02003389 if (!ret)
3390 goto set_output_value;
3391 /* Emulate open drain by not actively driving the line high */
Bartosz Golaszewskie7352442019-10-01 11:44:53 +02003392 if (value) {
3393 ret = gpiod_direction_input(desc);
3394 goto set_output_flag;
3395 }
Linus Walleij02e47982017-09-26 21:20:23 +02003396 }
3397 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
Geert Uytterhoeven83522352020-03-25 11:04:38 +01003398 ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_SOURCE);
Linus Walleij02e47982017-09-26 21:20:23 +02003399 if (!ret)
3400 goto set_output_value;
3401 /* Emulate open source by not actively driving the line low */
Bartosz Golaszewskie7352442019-10-01 11:44:53 +02003402 if (!value) {
3403 ret = gpiod_direction_input(desc);
3404 goto set_output_flag;
3405 }
Linus Walleij02e47982017-09-26 21:20:23 +02003406 } else {
Geert Uytterhoeven83522352020-03-25 11:04:38 +01003407 gpio_set_config(desc, PIN_CONFIG_DRIVE_PUSH_PULL);
Linus Walleij02e47982017-09-26 21:20:23 +02003408 }
3409
3410set_output_value:
Geert Uytterhoeven5f4bf172020-03-25 11:04:39 +01003411 ret = gpio_set_bias(desc);
Kent Gibson2821ae52019-11-05 10:04:26 +08003412 if (ret)
3413 return ret;
Linus Walleijfac9d882017-09-26 20:58:28 +02003414 return gpiod_direction_output_raw_commit(desc, value);
Bartosz Golaszewskie7352442019-10-01 11:44:53 +02003415
3416set_output_flag:
3417 /*
3418 * When emulating open-source or open-drain functionalities by not
3419 * actively driving the line (setting mode to input) we still need to
3420 * set the IS_OUT flag or otherwise we won't be able to set the line
3421 * value anymore.
3422 */
3423 if (ret == 0)
3424 set_bit(FLAG_IS_OUT, &desc->flags);
3425 return ret;
Philipp Zabelef70bbe2014-01-07 12:34:11 +01003426}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003427EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08003428
Felipe Balbic4b5be92010-05-26 14:42:23 -07003429/**
Geert Uytterhoeven8ced32f2020-03-24 14:56:50 +01003430 * gpiod_set_config - sets @config for a GPIO
3431 * @desc: descriptor of the GPIO for which to set the configuration
3432 * @config: Same packed config format as generic pinconf
3433 *
3434 * Returns:
3435 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
3436 * configuration.
3437 */
3438int gpiod_set_config(struct gpio_desc *desc, unsigned long config)
3439{
3440 struct gpio_chip *chip;
3441
3442 VALIDATE_DESC(desc);
3443 chip = desc->gdev->chip;
3444
3445 return gpio_do_set_config(chip, gpio_chip_hwgpio(desc), config);
3446}
3447EXPORT_SYMBOL_GPL(gpiod_set_config);
3448
3449/**
Thierry Reding950d55f52017-07-24 16:57:22 +02003450 * gpiod_set_debounce - sets @debounce time for a GPIO
3451 * @desc: descriptor of the GPIO for which to set debounce time
3452 * @debounce: debounce time in microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02003453 *
Thierry Reding950d55f52017-07-24 16:57:22 +02003454 * Returns:
3455 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
3456 * debounce time.
Felipe Balbic4b5be92010-05-26 14:42:23 -07003457 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003458int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07003459{
Geert Uytterhoeven8ced32f2020-03-24 14:56:50 +01003460 unsigned long config;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02003461
Mika Westerberg2956b5d2017-01-23 15:34:34 +03003462 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
Geert Uytterhoeven8ced32f2020-03-24 14:56:50 +01003463 return gpiod_set_config(desc, config);
Felipe Balbic4b5be92010-05-26 14:42:23 -07003464}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003465EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003466
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003467/**
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303468 * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
3469 * @desc: descriptor of the GPIO for which to configure persistence
3470 * @transitory: True to lose state on suspend or reset, false for persistence
3471 *
3472 * Returns:
3473 * 0 on success, otherwise a negative error code.
3474 */
3475int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
3476{
3477 struct gpio_chip *chip;
3478 unsigned long packed;
3479 int gpio;
3480 int rc;
3481
Vladimir Zapolskiy156dd392017-12-21 18:37:35 +02003482 VALIDATE_DESC(desc);
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303483 /*
3484 * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
3485 * persistence state.
3486 */
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +02003487 assign_bit(FLAG_TRANSITORY, &desc->flags, transitory);
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303488
3489 /* If the driver supports it, set the persistence state now */
3490 chip = desc->gdev->chip;
3491 if (!chip->set_config)
3492 return 0;
3493
3494 packed = pinconf_to_config_packed(PIN_CONFIG_PERSIST_STATE,
3495 !transitory);
3496 gpio = gpio_chip_hwgpio(desc);
Bartosz Golaszewskid99f8872020-02-03 14:03:37 +01003497 rc = gpio_do_set_config(chip, gpio, packed);
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303498 if (rc == -ENOTSUPP) {
3499 dev_dbg(&desc->gdev->dev, "Persistence not supported for GPIO %d\n",
3500 gpio);
3501 return 0;
3502 }
3503
3504 return rc;
3505}
3506EXPORT_SYMBOL_GPL(gpiod_set_transitory);
3507
3508/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003509 * gpiod_is_active_low - test whether a GPIO is active-low or not
3510 * @desc: the gpio descriptor to test
3511 *
3512 * Returns 1 if the GPIO is active-low, 0 otherwise.
3513 */
3514int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003515{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003516 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003517 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003518}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003519EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08003520
Michał Mirosławd3a5bcb2019-12-11 03:40:55 +01003521/**
3522 * gpiod_toggle_active_low - toggle whether a GPIO is active-low or not
3523 * @desc: the gpio descriptor to change
3524 */
3525void gpiod_toggle_active_low(struct gpio_desc *desc)
3526{
3527 VALIDATE_DESC_VOID(desc);
3528 change_bit(FLAG_ACTIVE_LOW, &desc->flags);
3529}
3530EXPORT_SYMBOL_GPL(gpiod_toggle_active_low);
3531
David Brownelld2876d02008-02-04 22:28:20 -08003532/* I/O calls are only valid after configuration completed; the relevant
3533 * "is this a valid GPIO" error checks should already have been done.
3534 *
3535 * "Get" operations are often inlinable as reading a pin value register,
3536 * and masking the relevant bit in that register.
3537 *
3538 * When "set" operations are inlinable, they involve writing that mask to
3539 * one register to set a low value, or a different register to set it high.
3540 * Otherwise locking is needed, so there may be little value to inlining.
3541 *
3542 *------------------------------------------------------------------------
3543 *
3544 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
3545 * have requested the GPIO. That can include implicit requesting by
3546 * a direction setting call. Marking a gpio as requested locks its chip
3547 * in memory, guaranteeing that these table lookups need no more locking
3548 * and that gpiochip_remove() will fail.
3549 *
3550 * REVISIT when debugging, consider adding some instrumentation to ensure
3551 * that the GPIO was actually requested.
3552 */
3553
Linus Walleijfac9d882017-09-26 20:58:28 +02003554static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08003555{
3556 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003557 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003558 int value;
David Brownelld2876d02008-02-04 22:28:20 -08003559
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003560 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003561 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003562 value = chip->get ? chip->get(chip, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01003563 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003564 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06003565 return value;
David Brownelld2876d02008-02-04 22:28:20 -08003566}
Alexandre Courbot372e7222013-02-03 01:29:29 +09003567
Lukas Wunnereec1d562017-10-12 12:40:10 +02003568static int gpio_chip_get_multiple(struct gpio_chip *chip,
3569 unsigned long *mask, unsigned long *bits)
3570{
3571 if (chip->get_multiple) {
3572 return chip->get_multiple(chip, mask, bits);
3573 } else if (chip->get) {
3574 int i, value;
3575
3576 for_each_set_bit(i, mask, chip->ngpio) {
3577 value = chip->get(chip, i);
3578 if (value < 0)
3579 return value;
3580 __assign_bit(i, bits, value);
3581 }
3582 return 0;
3583 }
3584 return -EIO;
3585}
3586
3587int gpiod_get_array_value_complex(bool raw, bool can_sleep,
3588 unsigned int array_size,
3589 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003590 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003591 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02003592{
Linus Walleijd377f562019-07-16 11:11:45 +02003593 int ret, i = 0;
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003594
3595 /*
3596 * Validate array_info against desc_array and its size.
3597 * It should immediately follow desc_array if both
3598 * have been obtained from the same gpiod_get_array() call.
3599 */
3600 if (array_info && array_info->desc == desc_array &&
3601 array_size <= array_info->size &&
3602 (void *)array_info == desc_array + array_info->size) {
3603 if (!can_sleep)
3604 WARN_ON(array_info->chip->can_sleep);
3605
Linus Walleijd377f562019-07-16 11:11:45 +02003606 ret = gpio_chip_get_multiple(array_info->chip,
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003607 array_info->get_mask,
3608 value_bitmap);
Linus Walleijd377f562019-07-16 11:11:45 +02003609 if (ret)
3610 return ret;
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003611
3612 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
3613 bitmap_xor(value_bitmap, value_bitmap,
3614 array_info->invert_mask, array_size);
3615
3616 if (bitmap_full(array_info->get_mask, array_size))
3617 return 0;
3618
3619 i = find_first_zero_bit(array_info->get_mask, array_size);
3620 } else {
3621 array_info = NULL;
3622 }
Lukas Wunnereec1d562017-10-12 12:40:10 +02003623
3624 while (i < array_size) {
3625 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Laura Abbott30277432018-05-21 10:57:07 -07003626 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
3627 unsigned long *mask, *bits;
Lukas Wunnereec1d562017-10-12 12:40:10 +02003628 int first, j, ret;
3629
Laura Abbott30277432018-05-21 10:57:07 -07003630 if (likely(chip->ngpio <= FASTPATH_NGPIO)) {
3631 mask = fastpath;
3632 } else {
3633 mask = kmalloc_array(2 * BITS_TO_LONGS(chip->ngpio),
3634 sizeof(*mask),
3635 can_sleep ? GFP_KERNEL : GFP_ATOMIC);
3636 if (!mask)
3637 return -ENOMEM;
3638 }
3639
3640 bits = mask + BITS_TO_LONGS(chip->ngpio);
3641 bitmap_zero(mask, chip->ngpio);
3642
Lukas Wunnereec1d562017-10-12 12:40:10 +02003643 if (!can_sleep)
3644 WARN_ON(chip->can_sleep);
3645
3646 /* collect all inputs belonging to the same chip */
3647 first = i;
Lukas Wunnereec1d562017-10-12 12:40:10 +02003648 do {
3649 const struct gpio_desc *desc = desc_array[i];
3650 int hwgpio = gpio_chip_hwgpio(desc);
3651
3652 __set_bit(hwgpio, mask);
3653 i++;
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003654
3655 if (array_info)
Janusz Krzysztofik35ae7f92018-09-24 01:53:35 +02003656 i = find_next_zero_bit(array_info->get_mask,
3657 array_size, i);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003658 } while ((i < array_size) &&
3659 (desc_array[i]->gdev->chip == chip));
3660
3661 ret = gpio_chip_get_multiple(chip, mask, bits);
Laura Abbott30277432018-05-21 10:57:07 -07003662 if (ret) {
3663 if (mask != fastpath)
3664 kfree(mask);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003665 return ret;
Laura Abbott30277432018-05-21 10:57:07 -07003666 }
Lukas Wunnereec1d562017-10-12 12:40:10 +02003667
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003668 for (j = first; j < i; ) {
Lukas Wunnereec1d562017-10-12 12:40:10 +02003669 const struct gpio_desc *desc = desc_array[j];
3670 int hwgpio = gpio_chip_hwgpio(desc);
3671 int value = test_bit(hwgpio, bits);
3672
3673 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3674 value = !value;
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003675 __assign_bit(j, value_bitmap, value);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003676 trace_gpio_value(desc_to_gpio(desc), 1, value);
Janusz Krzysztofik799d5eb2018-09-29 14:20:22 +02003677 j++;
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003678
3679 if (array_info)
Janusz Krzysztofik35ae7f92018-09-24 01:53:35 +02003680 j = find_next_zero_bit(array_info->get_mask, i,
3681 j);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003682 }
Laura Abbott30277432018-05-21 10:57:07 -07003683
3684 if (mask != fastpath)
3685 kfree(mask);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003686 }
3687 return 0;
3688}
3689
David Brownelld2876d02008-02-04 22:28:20 -08003690/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003691 * gpiod_get_raw_value() - return a gpio's raw value
3692 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08003693 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003694 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003695 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003696 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003697 * This function can be called from contexts where we cannot sleep, and will
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003698 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003699 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003700int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003701{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003702 VALIDATE_DESC(desc);
Geert Uytterhoeven3285170f2019-07-01 16:27:38 +02003703 /* Should be using gpiod_get_raw_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003704 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02003705 return gpiod_get_raw_value_commit(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003706}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003707EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08003708
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003709/**
3710 * gpiod_get_value() - return a gpio's value
3711 * @desc: gpio whose value will be returned
3712 *
3713 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003714 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003715 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003716 * This function can be called from contexts where we cannot sleep, and will
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003717 * complain if the GPIO chip functions potentially sleep.
3718 */
3719int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08003720{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003721 int value;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003722
3723 VALIDATE_DESC(desc);
Geert Uytterhoeven3285170f2019-07-01 16:27:38 +02003724 /* Should be using gpiod_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003725 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003726
Linus Walleijfac9d882017-09-26 20:58:28 +02003727 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003728 if (value < 0)
3729 return value;
3730
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003731 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3732 value = !value;
3733
3734 return value;
David Brownelld2876d02008-02-04 22:28:20 -08003735}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003736EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08003737
Lukas Wunnereec1d562017-10-12 12:40:10 +02003738/**
3739 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003740 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02003741 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003742 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003743 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02003744 *
3745 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3746 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3747 * else an error code.
3748 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003749 * This function can be called from contexts where we cannot sleep,
Lukas Wunnereec1d562017-10-12 12:40:10 +02003750 * and it will complain if the GPIO chip functions potentially sleep.
3751 */
3752int gpiod_get_raw_array_value(unsigned int array_size,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003753 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003754 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003755 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02003756{
3757 if (!desc_array)
3758 return -EINVAL;
3759 return gpiod_get_array_value_complex(true, false, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003760 desc_array, array_info,
3761 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003762}
3763EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
3764
3765/**
3766 * gpiod_get_array_value() - read values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003767 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02003768 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003769 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003770 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02003771 *
3772 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3773 * into account. Return 0 in case of success, else an error code.
3774 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003775 * This function can be called from contexts where we cannot sleep,
Lukas Wunnereec1d562017-10-12 12:40:10 +02003776 * and it will complain if the GPIO chip functions potentially sleep.
3777 */
3778int gpiod_get_array_value(unsigned int array_size,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003779 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003780 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003781 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02003782{
3783 if (!desc_array)
3784 return -EINVAL;
3785 return gpiod_get_array_value_complex(false, false, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003786 desc_array, array_info,
3787 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003788}
3789EXPORT_SYMBOL_GPL(gpiod_get_array_value);
3790
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303791/*
Linus Walleijfac9d882017-09-26 20:58:28 +02003792 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003793 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07003794 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303795 */
Linus Walleijfac9d882017-09-26 20:58:28 +02003796static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303797{
Linus Walleijd377f562019-07-16 11:11:45 +02003798 int ret = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003799 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003800 int offset = gpio_chip_hwgpio(desc);
3801
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303802 if (value) {
Linus Walleijd377f562019-07-16 11:11:45 +02003803 ret = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303804 } else {
Linus Walleijd377f562019-07-16 11:11:45 +02003805 ret = chip->direction_output(chip, offset, 0);
3806 if (!ret)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003807 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303808 }
Linus Walleijd377f562019-07-16 11:11:45 +02003809 trace_gpio_direction(desc_to_gpio(desc), value, ret);
3810 if (ret < 0)
Mark Brown6424de52013-09-09 10:33:49 +01003811 gpiod_err(desc,
3812 "%s: Error in set_value for open drain err %d\n",
Linus Walleijd377f562019-07-16 11:11:45 +02003813 __func__, ret);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303814}
3815
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303816/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003817 * _gpio_set_open_source_value() - Set the open source gpio's value.
3818 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07003819 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303820 */
Linus Walleijfac9d882017-09-26 20:58:28 +02003821static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303822{
Linus Walleijd377f562019-07-16 11:11:45 +02003823 int ret = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003824 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003825 int offset = gpio_chip_hwgpio(desc);
3826
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303827 if (value) {
Linus Walleijd377f562019-07-16 11:11:45 +02003828 ret = chip->direction_output(chip, offset, 1);
3829 if (!ret)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003830 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303831 } else {
Linus Walleijd377f562019-07-16 11:11:45 +02003832 ret = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303833 }
Linus Walleijd377f562019-07-16 11:11:45 +02003834 trace_gpio_direction(desc_to_gpio(desc), !value, ret);
3835 if (ret < 0)
Mark Brown6424de52013-09-09 10:33:49 +01003836 gpiod_err(desc,
3837 "%s: Error in set_value for open source err %d\n",
Linus Walleijd377f562019-07-16 11:11:45 +02003838 __func__, ret);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303839}
3840
Linus Walleijfac9d882017-09-26 20:58:28 +02003841static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08003842{
3843 struct gpio_chip *chip;
3844
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003845 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003846 trace_gpio_value(desc_to_gpio(desc), 0, value);
Linus Walleij02e47982017-09-26 21:20:23 +02003847 chip->set(chip, gpio_chip_hwgpio(desc), value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003848}
3849
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003850/*
3851 * set multiple outputs on the same chip;
3852 * use the chip's set_multiple function if available;
3853 * otherwise set the outputs sequentially;
3854 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
3855 * defines which outputs are to be changed
3856 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
3857 * defines the values the outputs specified by mask are to be set to
3858 */
3859static void gpio_chip_set_multiple(struct gpio_chip *chip,
3860 unsigned long *mask, unsigned long *bits)
3861{
3862 if (chip->set_multiple) {
3863 chip->set_multiple(chip, mask, bits);
3864 } else {
Andy Shevchenko5e4e6fb2017-01-03 19:01:17 +02003865 unsigned int i;
3866
3867 /* set outputs if the corresponding mask bit is set */
3868 for_each_set_bit(i, mask, chip->ngpio)
3869 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003870 }
3871}
3872
Laura Abbott30277432018-05-21 10:57:07 -07003873int gpiod_set_array_value_complex(bool raw, bool can_sleep,
Geert Uytterhoeven3c940662018-09-27 13:38:10 +02003874 unsigned int array_size,
3875 struct gpio_desc **desc_array,
3876 struct gpio_array *array_info,
3877 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003878{
3879 int i = 0;
3880
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003881 /*
3882 * Validate array_info against desc_array and its size.
3883 * It should immediately follow desc_array if both
3884 * have been obtained from the same gpiod_get_array() call.
3885 */
3886 if (array_info && array_info->desc == desc_array &&
3887 array_size <= array_info->size &&
3888 (void *)array_info == desc_array + array_info->size) {
3889 if (!can_sleep)
3890 WARN_ON(array_info->chip->can_sleep);
3891
3892 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
3893 bitmap_xor(value_bitmap, value_bitmap,
3894 array_info->invert_mask, array_size);
3895
3896 gpio_chip_set_multiple(array_info->chip, array_info->set_mask,
3897 value_bitmap);
3898
3899 if (bitmap_full(array_info->set_mask, array_size))
3900 return 0;
3901
3902 i = find_first_zero_bit(array_info->set_mask, array_size);
3903 } else {
3904 array_info = NULL;
3905 }
3906
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003907 while (i < array_size) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003908 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Laura Abbott30277432018-05-21 10:57:07 -07003909 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
3910 unsigned long *mask, *bits;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003911 int count = 0;
3912
Laura Abbott30277432018-05-21 10:57:07 -07003913 if (likely(chip->ngpio <= FASTPATH_NGPIO)) {
3914 mask = fastpath;
3915 } else {
3916 mask = kmalloc_array(2 * BITS_TO_LONGS(chip->ngpio),
3917 sizeof(*mask),
3918 can_sleep ? GFP_KERNEL : GFP_ATOMIC);
3919 if (!mask)
3920 return -ENOMEM;
3921 }
3922
3923 bits = mask + BITS_TO_LONGS(chip->ngpio);
3924 bitmap_zero(mask, chip->ngpio);
3925
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003926 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003927 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003928
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003929 do {
3930 struct gpio_desc *desc = desc_array[i];
3931 int hwgpio = gpio_chip_hwgpio(desc);
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003932 int value = test_bit(i, value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003933
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003934 /*
3935 * Pins applicable for fast input but not for
3936 * fast output processing may have been already
3937 * inverted inside the fast path, skip them.
3938 */
3939 if (!raw && !(array_info &&
3940 test_bit(i, array_info->invert_mask)) &&
3941 test_bit(FLAG_ACTIVE_LOW, &desc->flags))
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003942 value = !value;
3943 trace_gpio_value(desc_to_gpio(desc), 0, value);
3944 /*
3945 * collect all normal outputs belonging to the same chip
3946 * open drain and open source outputs are set individually
3947 */
Linus Walleij02e47982017-09-26 21:20:23 +02003948 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02003949 gpio_set_open_drain_value_commit(desc, value);
Linus Walleij02e47982017-09-26 21:20:23 +02003950 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02003951 gpio_set_open_source_value_commit(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003952 } else {
3953 __set_bit(hwgpio, mask);
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +02003954 __assign_bit(hwgpio, bits, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003955 count++;
3956 }
3957 i++;
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003958
3959 if (array_info)
Janusz Krzysztofik35ae7f92018-09-24 01:53:35 +02003960 i = find_next_zero_bit(array_info->set_mask,
3961 array_size, i);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003962 } while ((i < array_size) &&
3963 (desc_array[i]->gdev->chip == chip));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003964 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003965 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003966 gpio_chip_set_multiple(chip, mask, bits);
Laura Abbott30277432018-05-21 10:57:07 -07003967
3968 if (mask != fastpath)
3969 kfree(mask);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003970 }
Laura Abbott30277432018-05-21 10:57:07 -07003971 return 0;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003972}
3973
David Brownelld2876d02008-02-04 22:28:20 -08003974/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003975 * gpiod_set_raw_value() - assign a gpio's raw value
3976 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08003977 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08003978 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003979 * Set the raw value of the GPIO, i.e. the value of its physical line without
3980 * regard for its ACTIVE_LOW status.
3981 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003982 * This function can be called from contexts where we cannot sleep, and will
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003983 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003984 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003985void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003986{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003987 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven3285170f2019-07-01 16:27:38 +02003988 /* Should be using gpiod_set_raw_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003989 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02003990 gpiod_set_raw_value_commit(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08003991}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003992EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08003993
3994/**
Geert Uytterhoeven1e77fc82018-01-09 19:08:21 +01003995 * gpiod_set_value_nocheck() - set a GPIO line value without checking
3996 * @desc: the descriptor to set the value on
3997 * @value: value to set
3998 *
3999 * This sets the value of a GPIO line backing a descriptor, applying
4000 * different semantic quirks like active low and open drain/source
4001 * handling.
4002 */
4003static void gpiod_set_value_nocheck(struct gpio_desc *desc, int value)
4004{
4005 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
4006 value = !value;
4007 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
4008 gpio_set_open_drain_value_commit(desc, value);
4009 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
4010 gpio_set_open_source_value_commit(desc, value);
4011 else
4012 gpiod_set_raw_value_commit(desc, value);
4013}
4014
4015/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004016 * gpiod_set_value() - assign a gpio's value
4017 * @desc: gpio whose value will be assigned
4018 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08004019 *
Linus Walleij02e47982017-09-26 21:20:23 +02004020 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
4021 * OPEN_DRAIN and OPEN_SOURCE flags into account.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004022 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02004023 * This function can be called from contexts where we cannot sleep, and will
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004024 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08004025 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004026void gpiod_set_value(struct gpio_desc *desc, int value)
4027{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004028 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven3285170f2019-07-01 16:27:38 +02004029 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004030 WARN_ON(desc->gdev->chip->can_sleep);
Geert Uytterhoeven1e77fc82018-01-09 19:08:21 +01004031 gpiod_set_value_nocheck(desc, value);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004032}
4033EXPORT_SYMBOL_GPL(gpiod_set_value);
4034
4035/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02004036 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004037 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004038 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004039 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004040 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004041 *
4042 * Set the raw values of the GPIOs, i.e. the values of the physical lines
4043 * without regard for their ACTIVE_LOW status.
4044 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02004045 * This function can be called from contexts where we cannot sleep, and will
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004046 * complain if the GPIO chip functions potentially sleep.
4047 */
Laura Abbott30277432018-05-21 10:57:07 -07004048int gpiod_set_raw_array_value(unsigned int array_size,
Geert Uytterhoeven3c940662018-09-27 13:38:10 +02004049 struct gpio_desc **desc_array,
4050 struct gpio_array *array_info,
4051 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004052{
4053 if (!desc_array)
Laura Abbott30277432018-05-21 10:57:07 -07004054 return -EINVAL;
4055 return gpiod_set_array_value_complex(true, false, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004056 desc_array, array_info, value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004057}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02004058EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004059
4060/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02004061 * gpiod_set_array_value() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004062 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004063 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004064 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004065 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004066 *
4067 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
4068 * into account.
4069 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02004070 * This function can be called from contexts where we cannot sleep, and will
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004071 * complain if the GPIO chip functions potentially sleep.
4072 */
Geert Uytterhoevencf9af0d2018-09-27 13:38:09 +02004073int gpiod_set_array_value(unsigned int array_size,
4074 struct gpio_desc **desc_array,
4075 struct gpio_array *array_info,
4076 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004077{
4078 if (!desc_array)
Geert Uytterhoevencf9af0d2018-09-27 13:38:09 +02004079 return -EINVAL;
4080 return gpiod_set_array_value_complex(false, false, array_size,
4081 desc_array, array_info,
4082 value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004083}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02004084EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004085
4086/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004087 * gpiod_cansleep() - report whether gpio value access may sleep
4088 * @desc: gpio to check
4089 *
4090 */
4091int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09004092{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004093 VALIDATE_DESC(desc);
4094 return desc->gdev->chip->can_sleep;
Alexandre Courbot372e7222013-02-03 01:29:29 +09004095}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004096EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08004097
David Brownell0f6d5042008-10-15 22:03:14 -07004098/**
Linus Walleij90b39402e2018-06-01 13:21:27 +02004099 * gpiod_set_consumer_name() - set the consumer name for the descriptor
4100 * @desc: gpio to set the consumer name on
4101 * @name: the new consumer name
4102 */
Muchun Song18534df2018-11-01 21:12:50 +08004103int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name)
Linus Walleij90b39402e2018-06-01 13:21:27 +02004104{
Muchun Song18534df2018-11-01 21:12:50 +08004105 VALIDATE_DESC(desc);
4106 if (name) {
4107 name = kstrdup_const(name, GFP_KERNEL);
4108 if (!name)
4109 return -ENOMEM;
4110 }
4111
4112 kfree_const(desc->label);
4113 desc_set_label(desc, name);
4114
4115 return 0;
Linus Walleij90b39402e2018-06-01 13:21:27 +02004116}
4117EXPORT_SYMBOL_GPL(gpiod_set_consumer_name);
4118
4119/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004120 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
4121 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07004122 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004123 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
4124 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07004125 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004126int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07004127{
Linus Walleij4c37ce82016-05-02 13:13:10 +02004128 struct gpio_chip *chip;
4129 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07004130
Linus Walleij79bb71b2016-06-15 22:57:38 +02004131 /*
4132 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
4133 * requires this function to not return zero on an invalid descriptor
4134 * but rather a negative error number.
4135 */
Linus Walleijbfbbe442016-06-16 11:55:55 +02004136 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
Linus Walleij79bb71b2016-06-15 22:57:38 +02004137 return -EINVAL;
4138
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004139 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09004140 offset = gpio_chip_hwgpio(desc);
Linus Walleij4c37ce82016-05-02 13:13:10 +02004141 if (chip->to_irq) {
4142 int retirq = chip->to_irq(chip, offset);
4143
4144 /* Zero means NO_IRQ */
4145 if (!retirq)
4146 return -ENXIO;
4147
4148 return retirq;
4149 }
4150 return -ENXIO;
Alexandre Courbot372e7222013-02-03 01:29:29 +09004151}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004152EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09004153
Linus Walleijd468bf92013-09-24 11:54:38 +02004154/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09004155 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09004156 * @chip: the chip the GPIO to lock belongs to
4157 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02004158 *
4159 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08004160 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08004161 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09004162int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08004163{
Linus Walleij9c102802016-05-25 10:56:03 +02004164 struct gpio_desc *desc;
Linus Walleijd468bf92013-09-24 11:54:38 +02004165
Linus Walleij9c102802016-05-25 10:56:03 +02004166 desc = gpiochip_get_desc(chip, offset);
4167 if (IS_ERR(desc))
4168 return PTR_ERR(desc);
4169
Linus Walleij60f83392016-11-12 15:01:09 +01004170 /*
4171 * If it's fast: flush the direction setting if something changed
4172 * behind our back
4173 */
4174 if (!chip->can_sleep && chip->get_direction) {
Andy Shevchenko80956792018-07-09 21:47:21 +03004175 int dir = gpiod_get_direction(desc);
Linus Walleij9c102802016-05-25 10:56:03 +02004176
Andy Shevchenko36b31272018-07-03 03:38:31 +03004177 if (dir < 0) {
4178 chip_err(chip, "%s: cannot get GPIO direction\n",
4179 __func__);
4180 return dir;
4181 }
Linus Walleij9c102802016-05-25 10:56:03 +02004182 }
4183
4184 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09004185 chip_err(chip,
Andy Shevchenkob1911712018-07-03 03:39:03 +03004186 "%s: tried to flag a GPIO set as output for IRQ\n",
4187 __func__);
Linus Walleijd468bf92013-09-24 11:54:38 +02004188 return -EIO;
4189 }
4190
Linus Walleij9c102802016-05-25 10:56:03 +02004191 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
Hans Verkuil4e9439d2018-09-08 11:23:16 +02004192 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
Linus Walleij3940c342016-11-14 00:09:07 +01004193
4194 /*
4195 * If the consumer has not set up a label (such as when the
4196 * IRQ is referenced from .to_irq()) we set up a label here
4197 * so it is clear this is used as an interrupt.
4198 */
4199 if (!desc->label)
4200 desc_set_label(desc, "interrupt");
4201
Linus Walleijd468bf92013-09-24 11:54:38 +02004202 return 0;
4203}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09004204EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02004205
Linus Walleijd468bf92013-09-24 11:54:38 +02004206/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09004207 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09004208 * @chip: the chip the GPIO to lock belongs to
4209 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02004210 *
4211 * This is used directly by GPIO drivers that want to indicate
4212 * that a certain GPIO is no longer used exclusively for IRQ.
4213 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09004214void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02004215{
Linus Walleij3940c342016-11-14 00:09:07 +01004216 struct gpio_desc *desc;
4217
4218 desc = gpiochip_get_desc(chip, offset);
4219 if (IS_ERR(desc))
Linus Walleijd468bf92013-09-24 11:54:38 +02004220 return;
4221
Linus Walleij3940c342016-11-14 00:09:07 +01004222 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
Hans Verkuil4e9439d2018-09-08 11:23:16 +02004223 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
Linus Walleij3940c342016-11-14 00:09:07 +01004224
4225 /* If we only had this marking, erase it */
4226 if (desc->label && !strcmp(desc->label, "interrupt"))
4227 desc_set_label(desc, NULL);
Linus Walleijd468bf92013-09-24 11:54:38 +02004228}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09004229EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02004230
Hans Verkuil4e9439d2018-09-08 11:23:16 +02004231void gpiochip_disable_irq(struct gpio_chip *chip, unsigned int offset)
4232{
4233 struct gpio_desc *desc = gpiochip_get_desc(chip, offset);
4234
4235 if (!IS_ERR(desc) &&
4236 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags)))
4237 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
4238}
4239EXPORT_SYMBOL_GPL(gpiochip_disable_irq);
4240
4241void gpiochip_enable_irq(struct gpio_chip *chip, unsigned int offset)
4242{
4243 struct gpio_desc *desc = gpiochip_get_desc(chip, offset);
4244
4245 if (!IS_ERR(desc) &&
4246 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) {
4247 WARN_ON(test_bit(FLAG_IS_OUT, &desc->flags));
4248 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
4249 }
4250}
4251EXPORT_SYMBOL_GPL(gpiochip_enable_irq);
4252
Linus Walleij6cee3822016-02-11 20:16:45 +01004253bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
4254{
4255 if (offset >= chip->ngpio)
4256 return false;
4257
4258 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
4259}
4260EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
4261
Hans Verkuil4e6b8232018-09-08 11:23:14 +02004262int gpiochip_reqres_irq(struct gpio_chip *chip, unsigned int offset)
4263{
4264 int ret;
4265
4266 if (!try_module_get(chip->gpiodev->owner))
4267 return -ENODEV;
4268
4269 ret = gpiochip_lock_as_irq(chip, offset);
4270 if (ret) {
4271 chip_err(chip, "unable to lock HW IRQ %u for IRQ\n", offset);
4272 module_put(chip->gpiodev->owner);
4273 return ret;
4274 }
4275 return 0;
4276}
4277EXPORT_SYMBOL_GPL(gpiochip_reqres_irq);
4278
4279void gpiochip_relres_irq(struct gpio_chip *chip, unsigned int offset)
4280{
4281 gpiochip_unlock_as_irq(chip, offset);
4282 module_put(chip->gpiodev->owner);
4283}
4284EXPORT_SYMBOL_GPL(gpiochip_relres_irq);
4285
Linus Walleij143b65d2016-02-16 15:41:42 +01004286bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
4287{
4288 if (offset >= chip->ngpio)
4289 return false;
4290
4291 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
4292}
4293EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
4294
4295bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
4296{
4297 if (offset >= chip->ngpio)
4298 return false;
4299
4300 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
4301}
4302EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
4303
Charles Keepax05f479b2017-05-23 15:47:29 +01004304bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset)
4305{
4306 if (offset >= chip->ngpio)
4307 return false;
4308
Andrew Jefferye10f72b2017-11-30 14:25:24 +10304309 return !test_bit(FLAG_TRANSITORY, &chip->gpiodev->descs[offset].flags);
Charles Keepax05f479b2017-05-23 15:47:29 +01004310}
4311EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
4312
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004313/**
4314 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
4315 * @desc: gpio whose value will be returned
4316 *
4317 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07004318 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004319 *
4320 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08004321 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004322int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08004323{
David Brownelld2876d02008-02-04 22:28:20 -08004324 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004325 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02004326 return gpiod_get_raw_value_commit(desc);
David Brownelld2876d02008-02-04 22:28:20 -08004327}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004328EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09004329
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004330/**
4331 * gpiod_get_value_cansleep() - return a gpio's value
4332 * @desc: gpio whose value will be returned
4333 *
4334 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07004335 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004336 *
4337 * This function is to be called from contexts that can sleep.
4338 */
4339int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09004340{
David Brownelld2876d02008-02-04 22:28:20 -08004341 int value;
David Brownelld2876d02008-02-04 22:28:20 -08004342
4343 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004344 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02004345 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07004346 if (value < 0)
4347 return value;
4348
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004349 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
4350 value = !value;
4351
David Brownelld2876d02008-02-04 22:28:20 -08004352 return value;
4353}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004354EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09004355
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004356/**
Lukas Wunnereec1d562017-10-12 12:40:10 +02004357 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004358 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02004359 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004360 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004361 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02004362 *
4363 * Read the raw values of the GPIOs, i.e. the values of the physical lines
4364 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
4365 * else an error code.
4366 *
4367 * This function is to be called from contexts that can sleep.
4368 */
4369int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
4370 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004371 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004372 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02004373{
4374 might_sleep_if(extra_checks);
4375 if (!desc_array)
4376 return -EINVAL;
4377 return gpiod_get_array_value_complex(true, true, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004378 desc_array, array_info,
4379 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02004380}
4381EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
4382
4383/**
4384 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004385 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02004386 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004387 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004388 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02004389 *
4390 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
4391 * into account. Return 0 in case of success, else an error code.
4392 *
4393 * This function is to be called from contexts that can sleep.
4394 */
4395int gpiod_get_array_value_cansleep(unsigned int array_size,
4396 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004397 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004398 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02004399{
4400 might_sleep_if(extra_checks);
4401 if (!desc_array)
4402 return -EINVAL;
4403 return gpiod_get_array_value_complex(false, true, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004404 desc_array, array_info,
4405 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02004406}
4407EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
4408
4409/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004410 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
4411 * @desc: gpio whose value will be assigned
4412 * @value: value to assign
4413 *
4414 * Set the raw value of the GPIO, i.e. the value of its physical line without
4415 * regard for its ACTIVE_LOW status.
4416 *
4417 * This function is to be called from contexts that can sleep.
4418 */
4419void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09004420{
David Brownelld2876d02008-02-04 22:28:20 -08004421 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004422 VALIDATE_DESC_VOID(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02004423 gpiod_set_raw_value_commit(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09004424}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004425EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09004426
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004427/**
4428 * gpiod_set_value_cansleep() - assign a gpio's value
4429 * @desc: gpio whose value will be assigned
4430 * @value: value to assign
4431 *
4432 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
4433 * account
4434 *
4435 * This function is to be called from contexts that can sleep.
4436 */
4437void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09004438{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004439 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004440 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1e77fc82018-01-09 19:08:21 +01004441 gpiod_set_value_nocheck(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08004442}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004443EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08004444
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004445/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02004446 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004447 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004448 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004449 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004450 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004451 *
4452 * Set the raw values of the GPIOs, i.e. the values of the physical lines
4453 * without regard for their ACTIVE_LOW status.
4454 *
4455 * This function is to be called from contexts that can sleep.
4456 */
Laura Abbott30277432018-05-21 10:57:07 -07004457int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
Geert Uytterhoeven3c940662018-09-27 13:38:10 +02004458 struct gpio_desc **desc_array,
4459 struct gpio_array *array_info,
4460 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004461{
4462 might_sleep_if(extra_checks);
4463 if (!desc_array)
Laura Abbott30277432018-05-21 10:57:07 -07004464 return -EINVAL;
4465 return gpiod_set_array_value_complex(true, true, array_size, desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004466 array_info, value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004467}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02004468EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004469
4470/**
Dmitry Torokhov3946d182017-08-14 21:59:55 -07004471 * gpiod_add_lookup_tables() - register GPIO device consumers
4472 * @tables: list of tables of consumers to register
4473 * @n: number of tables in the list
4474 */
4475void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
4476{
4477 unsigned int i;
4478
4479 mutex_lock(&gpio_lookup_lock);
4480
4481 for (i = 0; i < n; i++)
4482 list_add_tail(&tables[i]->list, &gpio_lookup_list);
4483
4484 mutex_unlock(&gpio_lookup_lock);
4485}
4486
4487/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02004488 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004489 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004490 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004491 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004492 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004493 *
4494 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
4495 * into account.
4496 *
4497 * This function is to be called from contexts that can sleep.
4498 */
Geert Uytterhoevencf9af0d2018-09-27 13:38:09 +02004499int gpiod_set_array_value_cansleep(unsigned int array_size,
4500 struct gpio_desc **desc_array,
4501 struct gpio_array *array_info,
4502 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004503{
4504 might_sleep_if(extra_checks);
4505 if (!desc_array)
Geert Uytterhoevencf9af0d2018-09-27 13:38:09 +02004506 return -EINVAL;
4507 return gpiod_set_array_value_complex(false, true, array_size,
4508 desc_array, array_info,
4509 value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004510}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02004511EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004512
4513/**
Alexandre Courbotad824782013-12-03 12:20:11 +09004514 * gpiod_add_lookup_table() - register GPIO device consumers
4515 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004516 */
Alexandre Courbotad824782013-12-03 12:20:11 +09004517void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004518{
4519 mutex_lock(&gpio_lookup_lock);
4520
Alexandre Courbotad824782013-12-03 12:20:11 +09004521 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004522
4523 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08004524}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02004525EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
David Brownelld2876d02008-02-04 22:28:20 -08004526
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05304527/**
4528 * gpiod_remove_lookup_table() - unregister GPIO device consumers
4529 * @table: table of consumers to unregister
4530 */
4531void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
4532{
4533 mutex_lock(&gpio_lookup_lock);
4534
4535 list_del(&table->list);
4536
4537 mutex_unlock(&gpio_lookup_lock);
4538}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02004539EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05304540
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02004541/**
4542 * gpiod_add_hogs() - register a set of GPIO hogs from machine code
4543 * @hogs: table of gpio hog entries with a zeroed sentinel at the end
4544 */
4545void gpiod_add_hogs(struct gpiod_hog *hogs)
4546{
4547 struct gpio_chip *chip;
4548 struct gpiod_hog *hog;
4549
4550 mutex_lock(&gpio_machine_hogs_mutex);
4551
4552 for (hog = &hogs[0]; hog->chip_label; hog++) {
4553 list_add_tail(&hog->list, &gpio_machine_hogs);
4554
4555 /*
4556 * The chip may have been registered earlier, so check if it
4557 * exists and, if so, try to hog the line now.
4558 */
4559 chip = find_chip_by_name(hog->chip_label);
4560 if (chip)
4561 gpiochip_machine_hog(chip, hog);
4562 }
4563
4564 mutex_unlock(&gpio_machine_hogs_mutex);
4565}
4566EXPORT_SYMBOL_GPL(gpiod_add_hogs);
4567
Alexandre Courbotad824782013-12-03 12:20:11 +09004568static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
4569{
4570 const char *dev_id = dev ? dev_name(dev) : NULL;
4571 struct gpiod_lookup_table *table;
4572
4573 mutex_lock(&gpio_lookup_lock);
4574
4575 list_for_each_entry(table, &gpio_lookup_list, list) {
4576 if (table->dev_id && dev_id) {
4577 /*
4578 * Valid strings on both ends, must be identical to have
4579 * a match
4580 */
4581 if (!strcmp(table->dev_id, dev_id))
4582 goto found;
4583 } else {
4584 /*
4585 * One of the pointers is NULL, so both must be to have
4586 * a match
4587 */
4588 if (dev_id == table->dev_id)
4589 goto found;
4590 }
4591 }
4592 table = NULL;
4593
4594found:
4595 mutex_unlock(&gpio_lookup_lock);
4596 return table;
4597}
4598
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004599static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Andy Shevchenkofed70262019-04-10 18:39:16 +03004600 unsigned int idx, unsigned long *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004601{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004602 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09004603 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004604 struct gpiod_lookup *p;
4605
Alexandre Courbotad824782013-12-03 12:20:11 +09004606 table = gpiod_find_lookup_table(dev);
4607 if (!table)
4608 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004609
Alexandre Courbotad824782013-12-03 12:20:11 +09004610 for (p = &table->table[0]; p->chip_label; p++) {
4611 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004612
Alexandre Courbotad824782013-12-03 12:20:11 +09004613 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004614 if (p->idx != idx)
4615 continue;
4616
Alexandre Courbotad824782013-12-03 12:20:11 +09004617 /* If the lookup entry has a con_id, require exact match */
4618 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
4619 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004620
Alexandre Courbotad824782013-12-03 12:20:11 +09004621 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004622
Alexandre Courbotad824782013-12-03 12:20:11 +09004623 if (!chip) {
Janusz Krzysztofik8853daf2018-07-04 00:18:19 +02004624 /*
4625 * As the lookup table indicates a chip with
4626 * p->chip_label should exist, assume it may
4627 * still appear later and let the interested
4628 * consumer be probed again or let the Deferred
4629 * Probe infrastructure handle the error.
4630 */
4631 dev_warn(dev, "cannot find GPIO chip %s, deferring\n",
4632 p->chip_label);
4633 return ERR_PTR(-EPROBE_DEFER);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004634 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004635
Alexandre Courbotad824782013-12-03 12:20:11 +09004636 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004637 dev_err(dev,
Geert Uytterhoevend935bd52019-11-27 10:59:19 +01004638 "requested GPIO %u (%u) is out of range [0..%u] for chip %s\n",
4639 idx, p->chip_hwnum, chip->ngpio - 1,
4640 chip->label);
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004641 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09004642 }
4643
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09004644 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09004645 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004646
4647 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09004648 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004649
4650 return desc;
4651}
4652
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004653static int platform_gpio_count(struct device *dev, const char *con_id)
4654{
4655 struct gpiod_lookup_table *table;
4656 struct gpiod_lookup *p;
4657 unsigned int count = 0;
4658
4659 table = gpiod_find_lookup_table(dev);
4660 if (!table)
4661 return -ENOENT;
4662
4663 for (p = &table->table[0]; p->chip_label; p++) {
4664 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
4665 (!con_id && !p->con_id))
4666 count++;
4667 }
4668 if (!count)
4669 return -ENOENT;
4670
4671 return count;
4672}
4673
4674/**
Dmitry Torokhov13949fa2019-09-12 20:22:39 -07004675 * fwnode_gpiod_get_index - obtain a GPIO from firmware node
4676 * @fwnode: handle of the firmware node
4677 * @con_id: function within the GPIO consumer
4678 * @index: index of the GPIO to obtain for the consumer
4679 * @flags: GPIO initialization flags
4680 * @label: label to attach to the requested GPIO
4681 *
4682 * This function can be used for drivers that get their configuration
4683 * from opaque firmware.
4684 *
4685 * The function properly finds the corresponding GPIO using whatever is the
4686 * underlying firmware interface and then makes sure that the GPIO
4687 * descriptor is requested before it is returned to the caller.
4688 *
4689 * Returns:
4690 * On successful request the GPIO pin is configured in accordance with
4691 * provided @flags.
4692 *
4693 * In case of error an ERR_PTR() is returned.
4694 */
4695struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
4696 const char *con_id, int index,
4697 enum gpiod_flags flags,
4698 const char *label)
4699{
4700 struct gpio_desc *desc;
4701 char prop_name[32]; /* 32 is max size of property name */
4702 unsigned int i;
4703
4704 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
4705 if (con_id)
4706 snprintf(prop_name, sizeof(prop_name), "%s-%s",
4707 con_id, gpio_suffixes[i]);
4708 else
4709 snprintf(prop_name, sizeof(prop_name), "%s",
4710 gpio_suffixes[i]);
4711
4712 desc = fwnode_get_named_gpiod(fwnode, prop_name, index, flags,
4713 label);
4714 if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT))
4715 break;
4716 }
4717
4718 return desc;
4719}
4720EXPORT_SYMBOL_GPL(fwnode_gpiod_get_index);
4721
4722/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004723 * gpiod_count - return the number of GPIOs associated with a device / function
4724 * or -ENOENT if no GPIO has been assigned to the requested function
4725 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4726 * @con_id: function within the GPIO consumer
4727 */
4728int gpiod_count(struct device *dev, const char *con_id)
4729{
4730 int count = -ENOENT;
4731
4732 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
Linus Walleijf626d6d2019-07-17 09:10:01 +02004733 count = of_gpio_get_count(dev, con_id);
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004734 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
4735 count = acpi_gpio_count(dev, con_id);
4736
4737 if (count < 0)
4738 count = platform_gpio_count(dev, con_id);
4739
4740 return count;
4741}
4742EXPORT_SYMBOL_GPL(gpiod_count);
4743
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004744/**
Thierry Reding08791622014-04-25 16:54:22 +02004745 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09004746 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004747 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004748 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004749 *
4750 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004751 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07004752 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004753 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004754struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004755 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004756{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004757 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004758}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004759EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004760
4761/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02004762 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
4763 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4764 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004765 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02004766 *
4767 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
4768 * the requested function it will return NULL. This is convenient for drivers
4769 * that need to handle optional GPIOs.
4770 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004771struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004772 const char *con_id,
4773 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02004774{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004775 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02004776}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004777EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02004778
Benoit Parrotf625d462015-02-02 11:44:44 -06004779
4780/**
4781 * gpiod_configure_flags - helper function to configure a given GPIO
4782 * @desc: gpio whose value will be assigned
4783 * @con_id: function within the GPIO consumer
Andy Shevchenkofed70262019-04-10 18:39:16 +03004784 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4785 * of_find_gpio() or of_get_gpio_hog()
Benoit Parrotf625d462015-02-02 11:44:44 -06004786 * @dflags: gpiod_flags - optional GPIO initialization flags
4787 *
4788 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
4789 * requested function and/or index, or another IS_ERR() code if an error
4790 * occurred while trying to acquire the GPIO.
4791 */
Andy Shevchenkoc29fd9e2017-05-23 20:03:16 +03004792int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Johan Hovold85b03b32016-07-03 18:32:05 +02004793 unsigned long lflags, enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06004794{
Linus Walleijd377f562019-07-16 11:11:45 +02004795 int ret;
Benoit Parrotf625d462015-02-02 11:44:44 -06004796
Johan Hovold85b03b32016-07-03 18:32:05 +02004797 if (lflags & GPIO_ACTIVE_LOW)
4798 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
Linus Walleijf926dfc2017-09-10 19:26:22 +02004799
Johan Hovold85b03b32016-07-03 18:32:05 +02004800 if (lflags & GPIO_OPEN_DRAIN)
4801 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
Linus Walleijf926dfc2017-09-10 19:26:22 +02004802 else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) {
4803 /*
4804 * This enforces open drain mode from the consumer side.
4805 * This is necessary for some busses like I2C, but the lookup
4806 * should *REALLY* have specified them as open drain in the
4807 * first place, so print a little warning here.
4808 */
4809 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
4810 gpiod_warn(desc,
4811 "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
4812 }
4813
Johan Hovold85b03b32016-07-03 18:32:05 +02004814 if (lflags & GPIO_OPEN_SOURCE)
4815 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Andrew Jefferye10f72b2017-11-30 14:25:24 +10304816
Thomas Petazzonid4499912019-02-07 17:28:58 +01004817 if ((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DOWN)) {
4818 gpiod_err(desc,
4819 "both pull-up and pull-down enabled, invalid configuration\n");
4820 return -EINVAL;
4821 }
4822
4823 if (lflags & GPIO_PULL_UP)
4824 set_bit(FLAG_PULL_UP, &desc->flags);
4825 else if (lflags & GPIO_PULL_DOWN)
4826 set_bit(FLAG_PULL_DOWN, &desc->flags);
4827
Linus Walleijd377f562019-07-16 11:11:45 +02004828 ret = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
4829 if (ret < 0)
4830 return ret;
Johan Hovold85b03b32016-07-03 18:32:05 +02004831
Benoit Parrotf625d462015-02-02 11:44:44 -06004832 /* No particular flag request, return here... */
4833 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
4834 pr_debug("no flags found for %s\n", con_id);
4835 return 0;
4836 }
4837
4838 /* Process flags */
4839 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
Linus Walleijd377f562019-07-16 11:11:45 +02004840 ret = gpiod_direction_output(desc,
Linus Walleijad177312016-11-13 23:02:44 +01004841 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
Benoit Parrotf625d462015-02-02 11:44:44 -06004842 else
Linus Walleijd377f562019-07-16 11:11:45 +02004843 ret = gpiod_direction_input(desc);
Benoit Parrotf625d462015-02-02 11:44:44 -06004844
Linus Walleijd377f562019-07-16 11:11:45 +02004845 return ret;
Benoit Parrotf625d462015-02-02 11:44:44 -06004846}
4847
Thierry Reding29a1f2332014-04-25 17:10:06 +02004848/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004849 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02004850 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004851 * @con_id: function within the GPIO consumer
4852 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004853 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004854 *
4855 * This variant of gpiod_get() allows to access GPIOs other than the first
4856 * defined one for functions that define several GPIOs.
4857 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004858 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
4859 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07004860 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004861 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004862struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004863 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004864 unsigned int idx,
4865 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004866{
Andy Shevchenko2d6c06f2019-04-10 18:39:17 +03004867 unsigned long lookupflags = GPIO_LOOKUP_FLAGS_DEFAULT;
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09004868 struct gpio_desc *desc = NULL;
Linus Walleijd377f562019-07-16 11:11:45 +02004869 int ret;
Linus Walleij7d18f0a2018-01-16 08:29:50 +01004870 /* Maybe we have a device name, maybe not */
4871 const char *devname = dev ? dev_name(dev) : "?";
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004872
4873 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
4874
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01004875 if (dev) {
4876 /* Using device tree? */
4877 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
4878 dev_dbg(dev, "using device tree for GPIO lookup\n");
4879 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
4880 } else if (ACPI_COMPANION(dev)) {
4881 dev_dbg(dev, "using ACPI for GPIO lookup\n");
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +03004882 desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags);
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01004883 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09004884 }
4885
4886 /*
4887 * Either we are not using DT or ACPI, or their lookup did not return
4888 * a result. In that case, use platform lookup as a fallback.
4889 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004890 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04004891 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004892 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004893 }
4894
4895 if (IS_ERR(desc)) {
Wang Dongsheng9d5a1f22018-02-27 00:12:13 -08004896 dev_dbg(dev, "No GPIO consumer %s found\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004897 return desc;
4898 }
4899
Linus Walleij7d18f0a2018-01-16 08:29:50 +01004900 /*
4901 * If a connection label was passed use that, else attempt to use
4902 * the device name as label
4903 */
Linus Walleijd377f562019-07-16 11:11:45 +02004904 ret = gpiod_request(desc, con_id ? con_id : devname);
4905 if (ret < 0) {
4906 if (ret == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE) {
Linus Walleijb0ce7b292018-10-12 14:54:12 +02004907 /*
4908 * This happens when there are several consumers for
4909 * the same GPIO line: we just return here without
4910 * further initialization. It is a bit if a hack.
4911 * This is necessary to support fixed regulators.
4912 *
4913 * FIXME: Make this more sane and safe.
4914 */
4915 dev_info(dev, "nonexclusive access to GPIO for %s\n",
4916 con_id ? con_id : devname);
4917 return desc;
4918 } else {
Linus Walleijd377f562019-07-16 11:11:45 +02004919 return ERR_PTR(ret);
Linus Walleijb0ce7b292018-10-12 14:54:12 +02004920 }
4921 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004922
Linus Walleijd377f562019-07-16 11:11:45 +02004923 ret = gpiod_configure_flags(desc, con_id, lookupflags, flags);
4924 if (ret < 0) {
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004925 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
4926 gpiod_put(desc);
Linus Walleijd377f562019-07-16 11:11:45 +02004927 return ERR_PTR(ret);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004928 }
4929
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004930 return desc;
4931}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004932EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004933
4934/**
Mika Westerberg40b73182014-10-21 13:33:59 +02004935 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
4936 * @fwnode: handle of the firmware node
4937 * @propname: name of the firmware property representing the GPIO
Linus Walleij6392cca2017-12-29 02:07:54 +01004938 * @index: index of the GPIO to obtain for the consumer
Andy Shevchenkoa264d102017-01-09 16:02:28 +02004939 * @dflags: GPIO initialization flags
Thierry Reding950d55f52017-07-24 16:57:22 +02004940 * @label: label to attach to the requested GPIO
Mika Westerberg40b73182014-10-21 13:33:59 +02004941 *
4942 * This function can be used for drivers that get their configuration
Linus Walleij6392cca2017-12-29 02:07:54 +01004943 * from opaque firmware.
Mika Westerberg40b73182014-10-21 13:33:59 +02004944 *
Linus Walleij6392cca2017-12-29 02:07:54 +01004945 * The function properly finds the corresponding GPIO using whatever is the
Mika Westerberg40b73182014-10-21 13:33:59 +02004946 * underlying firmware interface and then makes sure that the GPIO
4947 * descriptor is requested before it is returned to the caller.
4948 *
Thierry Reding950d55f52017-07-24 16:57:22 +02004949 * Returns:
Andy Shevchenkoff213782017-02-28 17:03:12 +02004950 * On successful request the GPIO pin is configured in accordance with
Andy Shevchenkoa264d102017-01-09 16:02:28 +02004951 * provided @dflags.
4952 *
Mika Westerberg40b73182014-10-21 13:33:59 +02004953 * In case of error an ERR_PTR() is returned.
4954 */
4955struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
Boris Brezillon537b94d2017-02-02 14:53:11 +01004956 const char *propname, int index,
Alexander Steinb2987d72017-01-12 17:39:24 +01004957 enum gpiod_flags dflags,
4958 const char *label)
Mika Westerberg40b73182014-10-21 13:33:59 +02004959{
Andy Shevchenko2d6c06f2019-04-10 18:39:17 +03004960 unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
Mika Westerberg40b73182014-10-21 13:33:59 +02004961 struct gpio_desc *desc = ERR_PTR(-ENODEV);
Mika Westerberg40b73182014-10-21 13:33:59 +02004962 int ret;
4963
4964 if (!fwnode)
4965 return ERR_PTR(-EINVAL);
4966
4967 if (is_of_node(fwnode)) {
Linus Walleij6392cca2017-12-29 02:07:54 +01004968 desc = gpiod_get_from_of_node(to_of_node(fwnode),
4969 propname, index,
4970 dflags,
4971 label);
4972 return desc;
Mika Westerberg40b73182014-10-21 13:33:59 +02004973 } else if (is_acpi_node(fwnode)) {
4974 struct acpi_gpio_info info;
4975
Boris Brezillon537b94d2017-02-02 14:53:11 +01004976 desc = acpi_node_get_gpiod(fwnode, propname, index, &info);
Linus Walleij6392cca2017-12-29 02:07:54 +01004977 if (IS_ERR(desc))
4978 return desc;
4979
4980 acpi_gpio_update_gpiod_flags(&dflags, &info);
Andy Shevchenko606be342019-04-10 18:39:20 +03004981 acpi_gpio_update_gpiod_lookup_flags(&lflags, &info);
Mika Westerberg40b73182014-10-21 13:33:59 +02004982 }
4983
Linus Walleij6392cca2017-12-29 02:07:54 +01004984 /* Currently only ACPI takes this path */
Alexander Steinb2987d72017-01-12 17:39:24 +01004985 ret = gpiod_request(desc, label);
Johan Hovold85b03b32016-07-03 18:32:05 +02004986 if (ret)
4987 return ERR_PTR(ret);
4988
Andy Shevchenkoa264d102017-01-09 16:02:28 +02004989 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
4990 if (ret < 0) {
4991 gpiod_put(desc);
4992 return ERR_PTR(ret);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03004993 }
4994
Mika Westerberg40b73182014-10-21 13:33:59 +02004995 return desc;
4996}
4997EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
4998
4999/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02005000 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
5001 * function
5002 * @dev: GPIO consumer, can be NULL for system-global GPIOs
5003 * @con_id: function within the GPIO consumer
5004 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09005005 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02005006 *
5007 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
5008 * specified index was assigned to the requested function it will return NULL.
5009 * This is convenient for drivers that need to handle optional GPIOs.
5010 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01005011struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02005012 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09005013 unsigned int index,
5014 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02005015{
5016 struct gpio_desc *desc;
5017
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09005018 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02005019 if (IS_ERR(desc)) {
5020 if (PTR_ERR(desc) == -ENOENT)
5021 return NULL;
5022 }
5023
5024 return desc;
5025}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01005026EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02005027
5028/**
Benoit Parrotf625d462015-02-02 11:44:44 -06005029 * gpiod_hog - Hog the specified GPIO desc given the provided flags
5030 * @desc: gpio whose value will be assigned
5031 * @name: gpio line name
Andy Shevchenkofed70262019-04-10 18:39:16 +03005032 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
5033 * of_find_gpio() or of_get_gpio_hog()
Benoit Parrotf625d462015-02-02 11:44:44 -06005034 * @dflags: gpiod_flags - optional GPIO initialization flags
5035 */
5036int gpiod_hog(struct gpio_desc *desc, const char *name,
5037 unsigned long lflags, enum gpiod_flags dflags)
5038{
5039 struct gpio_chip *chip;
5040 struct gpio_desc *local_desc;
5041 int hwnum;
Linus Walleijd377f562019-07-16 11:11:45 +02005042 int ret;
Benoit Parrotf625d462015-02-02 11:44:44 -06005043
5044 chip = gpiod_to_chip(desc);
5045 hwnum = gpio_chip_hwgpio(desc);
5046
Linus Walleij5923ea62019-04-26 14:40:18 +02005047 local_desc = gpiochip_request_own_desc(chip, hwnum, name,
5048 lflags, dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -06005049 if (IS_ERR(local_desc)) {
Linus Walleijd377f562019-07-16 11:11:45 +02005050 ret = PTR_ERR(local_desc);
Laxman Dewanganc31a5712016-03-11 19:13:21 +05305051 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
Linus Walleijd377f562019-07-16 11:11:45 +02005052 name, chip->label, hwnum, ret);
5053 return ret;
Benoit Parrotf625d462015-02-02 11:44:44 -06005054 }
5055
Benoit Parrotf625d462015-02-02 11:44:44 -06005056 /* Mark GPIO as hogged so it can be identified and removed later */
5057 set_bit(FLAG_IS_HOGGED, &desc->flags);
5058
5059 pr_info("GPIO line %d (%s) hogged as %s%s\n",
5060 desc_to_gpio(desc), name,
Bartosz Golaszewskib27f3002019-11-13 14:16:29 +01005061 (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
5062 (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ?
5063 (dflags & GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low" : "");
Benoit Parrotf625d462015-02-02 11:44:44 -06005064
5065 return 0;
5066}
5067
5068/**
5069 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
5070 * @chip: gpio chip to act on
Benoit Parrotf625d462015-02-02 11:44:44 -06005071 */
5072static void gpiochip_free_hogs(struct gpio_chip *chip)
5073{
5074 int id;
5075
5076 for (id = 0; id < chip->ngpio; id++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01005077 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
5078 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
Benoit Parrotf625d462015-02-02 11:44:44 -06005079 }
5080}
5081
5082/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01005083 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
5084 * @dev: GPIO consumer, can be NULL for system-global GPIOs
5085 * @con_id: function within the GPIO consumer
5086 * @flags: optional GPIO initialization flags
5087 *
5088 * This function acquires all the GPIOs defined under a given function.
5089 *
5090 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
5091 * no GPIO has been assigned to the requested function, or another IS_ERR()
5092 * code if an error occurred while trying to acquire the GPIOs.
5093 */
5094struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
5095 const char *con_id,
5096 enum gpiod_flags flags)
5097{
5098 struct gpio_desc *desc;
5099 struct gpio_descs *descs;
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02005100 struct gpio_array *array_info = NULL;
5101 struct gpio_chip *chip;
5102 int count, bitmap_size;
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01005103
5104 count = gpiod_count(dev, con_id);
5105 if (count < 0)
5106 return ERR_PTR(count);
5107
Kees Cookacafe7e2018-05-08 13:45:50 -07005108 descs = kzalloc(struct_size(descs, desc, count), GFP_KERNEL);
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01005109 if (!descs)
5110 return ERR_PTR(-ENOMEM);
5111
5112 for (descs->ndescs = 0; descs->ndescs < count; ) {
5113 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
5114 if (IS_ERR(desc)) {
5115 gpiod_put_array(descs);
5116 return ERR_CAST(desc);
5117 }
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02005118
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01005119 descs->desc[descs->ndescs] = desc;
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02005120
5121 chip = gpiod_to_chip(desc);
5122 /*
Janusz Krzysztofikc4c958a2018-09-24 01:53:36 +02005123 * If pin hardware number of array member 0 is also 0, select
5124 * its chip as a candidate for fast bitmap processing path.
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02005125 */
Janusz Krzysztofikc4c958a2018-09-24 01:53:36 +02005126 if (descs->ndescs == 0 && gpio_chip_hwgpio(desc) == 0) {
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02005127 struct gpio_descs *array;
5128
5129 bitmap_size = BITS_TO_LONGS(chip->ngpio > count ?
5130 chip->ngpio : count);
5131
5132 array = kzalloc(struct_size(descs, desc, count) +
5133 struct_size(array_info, invert_mask,
5134 3 * bitmap_size), GFP_KERNEL);
5135 if (!array) {
5136 gpiod_put_array(descs);
5137 return ERR_PTR(-ENOMEM);
5138 }
5139
5140 memcpy(array, descs,
5141 struct_size(descs, desc, descs->ndescs + 1));
5142 kfree(descs);
5143
5144 descs = array;
5145 array_info = (void *)(descs->desc + count);
5146 array_info->get_mask = array_info->invert_mask +
5147 bitmap_size;
5148 array_info->set_mask = array_info->get_mask +
5149 bitmap_size;
5150
5151 array_info->desc = descs->desc;
5152 array_info->size = count;
5153 array_info->chip = chip;
5154 bitmap_set(array_info->get_mask, descs->ndescs,
5155 count - descs->ndescs);
5156 bitmap_set(array_info->set_mask, descs->ndescs,
5157 count - descs->ndescs);
5158 descs->info = array_info;
5159 }
Janusz Krzysztofikc4c958a2018-09-24 01:53:36 +02005160 /* Unmark array members which don't belong to the 'fast' chip */
5161 if (array_info && array_info->chip != chip) {
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02005162 __clear_bit(descs->ndescs, array_info->get_mask);
5163 __clear_bit(descs->ndescs, array_info->set_mask);
Janusz Krzysztofikc4c958a2018-09-24 01:53:36 +02005164 }
5165 /*
5166 * Detect array members which belong to the 'fast' chip
5167 * but their pins are not in hardware order.
5168 */
5169 else if (array_info &&
5170 gpio_chip_hwgpio(desc) != descs->ndescs) {
5171 /*
5172 * Don't use fast path if all array members processed so
5173 * far belong to the same chip as this one but its pin
5174 * hardware number is different from its array index.
5175 */
5176 if (bitmap_full(array_info->get_mask, descs->ndescs)) {
5177 array_info = NULL;
5178 } else {
5179 __clear_bit(descs->ndescs,
5180 array_info->get_mask);
5181 __clear_bit(descs->ndescs,
5182 array_info->set_mask);
5183 }
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02005184 } else if (array_info) {
5185 /* Exclude open drain or open source from fast output */
5186 if (gpiochip_line_is_open_drain(chip, descs->ndescs) ||
5187 gpiochip_line_is_open_source(chip, descs->ndescs))
5188 __clear_bit(descs->ndescs,
5189 array_info->set_mask);
5190 /* Identify 'fast' pins which require invertion */
5191 if (gpiod_is_active_low(desc))
5192 __set_bit(descs->ndescs,
5193 array_info->invert_mask);
5194 }
5195
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01005196 descs->ndescs++;
5197 }
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02005198 if (array_info)
5199 dev_dbg(dev,
5200 "GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n",
5201 array_info->chip->label, array_info->size,
5202 *array_info->get_mask, *array_info->set_mask,
5203 *array_info->invert_mask);
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01005204 return descs;
5205}
5206EXPORT_SYMBOL_GPL(gpiod_get_array);
5207
5208/**
5209 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
5210 * function
5211 * @dev: GPIO consumer, can be NULL for system-global GPIOs
5212 * @con_id: function within the GPIO consumer
5213 * @flags: optional GPIO initialization flags
5214 *
5215 * This is equivalent to gpiod_get_array(), except that when no GPIO was
5216 * assigned to the requested function it will return NULL.
5217 */
5218struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
5219 const char *con_id,
5220 enum gpiod_flags flags)
5221{
5222 struct gpio_descs *descs;
5223
5224 descs = gpiod_get_array(dev, con_id, flags);
Masahiro Yamada45586c72020-02-03 17:37:45 -08005225 if (PTR_ERR(descs) == -ENOENT)
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01005226 return NULL;
5227
5228 return descs;
5229}
5230EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
5231
5232/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07005233 * gpiod_put - dispose of a GPIO descriptor
5234 * @desc: GPIO descriptor to dispose of
5235 *
5236 * No descriptor can be used after gpiod_put() has been called on it.
5237 */
5238void gpiod_put(struct gpio_desc *desc)
5239{
Andy Shevchenko1d7765b2019-03-26 17:21:14 +02005240 if (desc)
5241 gpiod_free(desc);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07005242}
5243EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08005244
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01005245/**
5246 * gpiod_put_array - dispose of multiple GPIO descriptors
5247 * @descs: struct gpio_descs containing an array of descriptors
5248 */
5249void gpiod_put_array(struct gpio_descs *descs)
5250{
5251 unsigned int i;
5252
5253 for (i = 0; i < descs->ndescs; i++)
5254 gpiod_put(descs->desc[i]);
5255
5256 kfree(descs);
5257}
5258EXPORT_SYMBOL_GPL(gpiod_put_array);
5259
Linus Walleij3c702e92015-10-21 15:29:53 +02005260static int __init gpiolib_dev_init(void)
5261{
5262 int ret;
5263
5264 /* Register GPIO sysfs bus */
Andy Shevchenkob1911712018-07-03 03:39:03 +03005265 ret = bus_register(&gpio_bus_type);
Linus Walleij3c702e92015-10-21 15:29:53 +02005266 if (ret < 0) {
5267 pr_err("gpiolib: could not register GPIO bus type\n");
5268 return ret;
5269 }
5270
Geert Uytterhoevenddd88912019-11-27 09:42:47 +01005271 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, GPIOCHIP_NAME);
Linus Walleij3c702e92015-10-21 15:29:53 +02005272 if (ret < 0) {
5273 pr_err("gpiolib: failed to allocate char dev region\n");
5274 bus_unregister(&gpio_bus_type);
Geert Uytterhoeven63636d92020-02-20 14:01:49 +01005275 return ret;
Linus Walleij3c702e92015-10-21 15:29:53 +02005276 }
Geert Uytterhoeven63636d92020-02-20 14:01:49 +01005277
5278 gpiolib_initialized = true;
5279 gpiochip_setup_devs();
5280
5281 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
5282 WARN_ON(of_reconfig_notifier_register(&gpio_of_notifier));
5283
Linus Walleij3c702e92015-10-21 15:29:53 +02005284 return ret;
5285}
5286core_initcall(gpiolib_dev_init);
5287
David Brownelld2876d02008-02-04 22:28:20 -08005288#ifdef CONFIG_DEBUG_FS
5289
Linus Walleijfdeb8e12016-02-10 10:57:36 +01005290static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
David Brownelld2876d02008-02-04 22:28:20 -08005291{
5292 unsigned i;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01005293 struct gpio_chip *chip = gdev->chip;
5294 unsigned gpio = gdev->base;
5295 struct gpio_desc *gdesc = &gdev->descs[0];
Linus Walleij90fd2272018-10-01 23:06:17 +02005296 bool is_out;
5297 bool is_irq;
5298 bool active_low;
David Brownelld2876d02008-02-04 22:28:20 -08005299
Linus Walleijfdeb8e12016-02-10 10:57:36 +01005300 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02005301 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
5302 if (gdesc->name) {
5303 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
5304 gpio, gdesc->name);
5305 }
David Brownelld2876d02008-02-04 22:28:20 -08005306 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02005307 }
David Brownelld2876d02008-02-04 22:28:20 -08005308
Alexandre Courbot372e7222013-02-03 01:29:29 +09005309 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08005310 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02005311 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Linus Walleij90fd2272018-10-01 23:06:17 +02005312 active_low = test_bit(FLAG_ACTIVE_LOW, &gdesc->flags);
5313 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s%s",
Markus Pargmannced433e2015-08-14 16:11:02 +02005314 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08005315 is_out ? "out" : "in ",
Andy Shevchenko1c22a252018-07-12 20:36:42 +03005316 chip->get ? (chip->get(chip, i) ? "hi" : "lo") : "? ",
Linus Walleij90fd2272018-10-01 23:06:17 +02005317 is_irq ? "IRQ " : "",
5318 active_low ? "ACTIVE LOW" : "");
David Brownelld2876d02008-02-04 22:28:20 -08005319 seq_printf(s, "\n");
5320 }
5321}
5322
Thierry Redingf9c4a312012-04-12 13:26:01 +02005323static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08005324{
Grant Likely362432a2013-02-09 09:41:49 +00005325 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02005326 struct gpio_device *gdev = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09005327 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02005328
5329 s->private = "";
5330
Grant Likely362432a2013-02-09 09:41:49 +00005331 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02005332 list_for_each_entry(gdev, &gpio_devices, list)
Grant Likely362432a2013-02-09 09:41:49 +00005333 if (index-- == 0) {
5334 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02005335 return gdev;
Grant Likely362432a2013-02-09 09:41:49 +00005336 }
5337 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09005338
5339 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02005340}
5341
5342static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
5343{
Grant Likely362432a2013-02-09 09:41:49 +00005344 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02005345 struct gpio_device *gdev = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02005346 void *ret = NULL;
5347
Grant Likely362432a2013-02-09 09:41:49 +00005348 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02005349 if (list_is_last(&gdev->list, &gpio_devices))
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09005350 ret = NULL;
5351 else
Linus Walleijff2b1352015-10-20 11:10:38 +02005352 ret = list_entry(gdev->list.next, struct gpio_device, list);
Grant Likely362432a2013-02-09 09:41:49 +00005353 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02005354
5355 s->private = "\n";
5356 ++*pos;
5357
5358 return ret;
5359}
5360
5361static void gpiolib_seq_stop(struct seq_file *s, void *v)
5362{
5363}
5364
5365static int gpiolib_seq_show(struct seq_file *s, void *v)
5366{
Linus Walleijff2b1352015-10-20 11:10:38 +02005367 struct gpio_device *gdev = v;
5368 struct gpio_chip *chip = gdev->chip;
5369 struct device *parent;
Thierry Redingf9c4a312012-04-12 13:26:01 +02005370
Linus Walleijff2b1352015-10-20 11:10:38 +02005371 if (!chip) {
5372 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
5373 dev_name(&gdev->dev));
5374 return 0;
5375 }
5376
5377 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
5378 dev_name(&gdev->dev),
Linus Walleijfdeb8e12016-02-10 10:57:36 +01005379 gdev->base, gdev->base + gdev->ngpio - 1);
Linus Walleijff2b1352015-10-20 11:10:38 +02005380 parent = chip->parent;
5381 if (parent)
5382 seq_printf(s, ", parent: %s/%s",
5383 parent->bus ? parent->bus->name : "no-bus",
5384 dev_name(parent));
Thierry Redingf9c4a312012-04-12 13:26:01 +02005385 if (chip->label)
5386 seq_printf(s, ", %s", chip->label);
5387 if (chip->can_sleep)
5388 seq_printf(s, ", can sleep");
5389 seq_printf(s, ":\n");
5390
5391 if (chip->dbg_show)
5392 chip->dbg_show(s, chip);
5393 else
Linus Walleijfdeb8e12016-02-10 10:57:36 +01005394 gpiolib_dbg_show(s, gdev);
Thierry Redingf9c4a312012-04-12 13:26:01 +02005395
David Brownelld2876d02008-02-04 22:28:20 -08005396 return 0;
5397}
5398
Thierry Redingf9c4a312012-04-12 13:26:01 +02005399static const struct seq_operations gpiolib_seq_ops = {
5400 .start = gpiolib_seq_start,
5401 .next = gpiolib_seq_next,
5402 .stop = gpiolib_seq_stop,
5403 .show = gpiolib_seq_show,
5404};
5405
David Brownelld2876d02008-02-04 22:28:20 -08005406static int gpiolib_open(struct inode *inode, struct file *file)
5407{
Thierry Redingf9c4a312012-04-12 13:26:01 +02005408 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08005409}
5410
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005411static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02005412 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08005413 .open = gpiolib_open,
5414 .read = seq_read,
5415 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02005416 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08005417};
5418
5419static int __init gpiolib_debugfs_init(void)
5420{
5421 /* /sys/kernel/debug/gpio */
Greg Kroah-Hartmanacc68b02019-06-18 17:50:45 +02005422 debugfs_create_file("gpio", S_IFREG | S_IRUGO, NULL, NULL,
5423 &gpiolib_operations);
David Brownelld2876d02008-02-04 22:28:20 -08005424 return 0;
5425}
5426subsys_initcall(gpiolib_debugfs_init);
5427
5428#endif /* DEBUG_FS */