blob: 9ad86477af9c4f7cde939912ac9763a493d3856a [file] [log] [blame]
Linus Walleijdae5f0a2018-09-25 09:08:48 +02001// SPDX-License-Identifier: GPL-2.0
Andy Shevchenko923a6542017-05-25 16:08:38 +03002#include <linux/bitmap.h>
David Brownelld2876d02008-02-04 22:28:20 -08003#include <linux/kernel.h>
4#include <linux/module.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -07005#include <linux/interrupt.h>
David Brownelld2876d02008-02-04 22:28:20 -08006#include <linux/irq.h>
7#include <linux/spinlock.h>
Alexandre Courbot1a989d02013-02-03 01:29:24 +09008#include <linux/list.h>
David Brownelld8f388d82008-07-25 01:46:07 -07009#include <linux/device.h>
10#include <linux/err.h>
11#include <linux/debugfs.h>
12#include <linux/seq_file.h>
13#include <linux/gpio.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -070014#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Rafael J. Wysocki7b199812013-11-11 22:41:56 +010016#include <linux/acpi.h>
Alexandre Courbot53e7cac2013-11-16 21:44:52 +090017#include <linux/gpio/driver.h>
Linus Walleij0a6d3152014-07-24 20:08:55 +020018#include <linux/gpio/machine.h>
Jonas Gorskic771c2f2015-10-11 17:34:15 +020019#include <linux/pinctrl/consumer.h>
Linus Walleij3c702e92015-10-21 15:29:53 +020020#include <linux/cdev.h>
21#include <linux/fs.h>
22#include <linux/uaccess.h>
Linus Walleij8b92e172016-05-27 14:24:04 +020023#include <linux/compat.h>
Linus Walleijd7c51b42016-04-26 10:35:29 +020024#include <linux/anon_inodes.h>
Lars-Peter Clausen953b9562016-10-24 13:59:15 +020025#include <linux/file.h>
Linus Walleij61f922d2016-06-02 11:30:15 +020026#include <linux/kfifo.h>
27#include <linux/poll.h>
28#include <linux/timekeeping.h>
Linus Walleij3c702e92015-10-21 15:29:53 +020029#include <uapi/linux/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080030
Mika Westerberg664e3e52014-01-08 12:40:54 +020031#include "gpiolib.h"
Linus Walleijf626d6d2019-07-17 09:10:01 +020032#include "gpiolib-of.h"
Andy Shevchenko77cb9072019-07-30 13:43:36 +030033#include "gpiolib-acpi.h"
Mika Westerberg664e3e52014-01-08 12:40:54 +020034
Uwe Kleine-König3f397c212011-05-20 00:40:19 -060035#define CREATE_TRACE_POINTS
36#include <trace/events/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080037
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070038/* Implementation infrastructure for GPIO interfaces.
David Brownelld2876d02008-02-04 22:28:20 -080039 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070040 * The GPIO programming interface allows for inlining speed-critical
41 * get/set operations for common cases, so that access to SOC-integrated
42 * GPIOs can sometimes cost only an instruction or two per bit.
David Brownelld2876d02008-02-04 22:28:20 -080043 */
44
45
46/* When debugging, extend minimal trust to callers and platform code.
47 * Also emit diagnostic messages that may help initial bringup, when
48 * board setup or driver bugs are most common.
49 *
50 * Otherwise, minimize overhead in what may be bitbanging codepaths.
51 */
52#ifdef DEBUG
53#define extra_checks 1
54#else
55#define extra_checks 0
56#endif
57
Linus Walleijff2b1352015-10-20 11:10:38 +020058/* Device and char device-related information */
59static DEFINE_IDA(gpio_ida);
Linus Walleij3c702e92015-10-21 15:29:53 +020060static dev_t gpio_devt;
61#define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
62static struct bus_type gpio_bus_type = {
63 .name = "gpio",
64};
Linus Walleijff2b1352015-10-20 11:10:38 +020065
Laura Abbott30277432018-05-21 10:57:07 -070066/*
67 * Number of GPIOs to use for the fast path in set array
68 */
69#define FASTPATH_NGPIO CONFIG_GPIOLIB_FASTPATH_LIMIT
70
David Brownelld2876d02008-02-04 22:28:20 -080071/* gpio_lock prevents conflicts during gpio_desc[] table updates.
72 * While any GPIO is requested, its gpio_chip is not removable;
73 * each GPIO's "requested" flag serves as a lock and refcount.
74 */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090075DEFINE_SPINLOCK(gpio_lock);
David Brownelld2876d02008-02-04 22:28:20 -080076
Alexandre Courbotbae48da2013-10-17 10:21:38 -070077static DEFINE_MUTEX(gpio_lookup_lock);
78static LIST_HEAD(gpio_lookup_list);
Linus Walleijff2b1352015-10-20 11:10:38 +020079LIST_HEAD(gpio_devices);
Johan Hovold6d867502015-05-04 17:23:25 +020080
Bartosz Golaszewskia411e812018-04-10 22:30:28 +020081static DEFINE_MUTEX(gpio_machine_hogs_mutex);
82static LIST_HEAD(gpio_machine_hogs);
83
Johan Hovold6d867502015-05-04 17:23:25 +020084static void gpiochip_free_hogs(struct gpio_chip *chip);
Thierry Reding959bc7b2017-11-07 19:15:59 +010085static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
Andrew Lunn39c3fd52017-12-02 18:11:04 +010086 struct lock_class_key *lock_key,
87 struct lock_class_key *request_key);
Johan Hovold6d867502015-05-04 17:23:25 +020088static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
Andy Shevchenko9411e3a2019-10-09 17:34:44 +030089static int gpiochip_irqchip_init_hw(struct gpio_chip *gpiochip);
Mika Westerberg79b804c2016-09-20 15:15:21 +030090static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip);
91static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip);
Johan Hovold6d867502015-05-04 17:23:25 +020092
Guenter Roeck159f3cd2016-03-31 08:11:30 -070093static bool gpiolib_initialized;
Johan Hovold6d867502015-05-04 17:23:25 +020094
David Brownelld2876d02008-02-04 22:28:20 -080095static inline void desc_set_label(struct gpio_desc *d, const char *label)
96{
David Brownelld2876d02008-02-04 22:28:20 -080097 d->label = label;
David Brownelld2876d02008-02-04 22:28:20 -080098}
99
Alexandre Courbot372e7222013-02-03 01:29:29 +0900100/**
Thierry Reding950d55f52017-07-24 16:57:22 +0200101 * gpio_to_desc - Convert a GPIO number to its descriptor
102 * @gpio: global GPIO number
103 *
104 * Returns:
105 * The GPIO descriptor associated with the given GPIO, or %NULL if no GPIO
106 * with the given number exists in the system.
Alexandre Courbot372e7222013-02-03 01:29:29 +0900107 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700108struct gpio_desc *gpio_to_desc(unsigned gpio)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900109{
Linus Walleijff2b1352015-10-20 11:10:38 +0200110 struct gpio_device *gdev;
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900111 unsigned long flags;
112
113 spin_lock_irqsave(&gpio_lock, flags);
114
Linus Walleijff2b1352015-10-20 11:10:38 +0200115 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100116 if (gdev->base <= gpio &&
117 gdev->base + gdev->ngpio > gpio) {
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900118 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100119 return &gdev->descs[gpio - gdev->base];
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900120 }
121 }
122
123 spin_unlock_irqrestore(&gpio_lock, flags);
124
Alexandre Courbot0e9a5ed2014-12-02 23:15:05 +0900125 if (!gpio_is_valid(gpio))
126 WARN(1, "invalid GPIO %d\n", gpio);
127
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900128 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900129}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700130EXPORT_SYMBOL_GPL(gpio_to_desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900131
132/**
Thierry Reding950d55f52017-07-24 16:57:22 +0200133 * gpiochip_get_desc - get the GPIO descriptor corresponding to the given
134 * hardware number for this chip
135 * @chip: GPIO chip
136 * @hwnum: hardware number of the GPIO for this chip
137 *
138 * Returns:
139 * A pointer to the GPIO descriptor or %ERR_PTR(-EINVAL) if no GPIO exists
140 * in the given chip for the specified hardware number.
Linus Walleijd468bf92013-09-24 11:54:38 +0200141 */
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900142struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
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
304 spin_lock_irqsave(&gpio_lock, flags);
305
Linus Walleijff2b1352015-10-20 11:10:38 +0200306 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijf881bab02015-09-23 16:20:43 -0700307 int i;
308
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100309 for (i = 0; i != gdev->ngpio; ++i) {
310 struct gpio_desc *desc = &gdev->descs[i];
Linus Walleijf881bab02015-09-23 16:20:43 -0700311
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100312 if (!desc->name || !name)
Linus Walleijf881bab02015-09-23 16:20:43 -0700313 continue;
314
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100315 if (!strcmp(desc->name, name)) {
Linus Walleijf881bab02015-09-23 16:20:43 -0700316 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100317 return desc;
Linus Walleijf881bab02015-09-23 16:20:43 -0700318 }
319 }
320 }
321
322 spin_unlock_irqrestore(&gpio_lock, flags);
323
324 return NULL;
325}
326
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200327/*
328 * Takes the names from gc->names and checks if they are all unique. If they
329 * are, they are assigned to their gpio descriptors.
330 *
Bamvor Jian Zhanged379152015-11-14 16:43:20 +0800331 * Warning if one of the names is already used for a different GPIO.
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200332 */
333static int gpiochip_set_desc_names(struct gpio_chip *gc)
334{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100335 struct gpio_device *gdev = gc->gpiodev;
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200336 int i;
337
338 if (!gc->names)
339 return 0;
340
341 /* First check all names if they are unique */
342 for (i = 0; i != gc->ngpio; ++i) {
343 struct gpio_desc *gpio;
344
345 gpio = gpio_name_to_desc(gc->names[i]);
Linus Walleijf881bab02015-09-23 16:20:43 -0700346 if (gpio)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100347 dev_warn(&gdev->dev,
Linus Walleij34ffd852015-10-20 11:31:54 +0200348 "Detected name collision for GPIO name '%s'\n",
Linus Walleijf881bab02015-09-23 16:20:43 -0700349 gc->names[i]);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200350 }
351
352 /* Then add all names to the GPIO descriptors */
353 for (i = 0; i != gc->ngpio; ++i)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100354 gdev->descs[i].name = gc->names[i];
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200355
356 return 0;
357}
358
Stephen Boyde4371f6e2018-03-23 09:34:50 -0700359static unsigned long *gpiochip_allocate_mask(struct gpio_chip *chip)
360{
361 unsigned long *p;
362
Masahiro Yamada7bdbd1ec2019-07-18 15:51:01 +0900363 p = bitmap_alloc(chip->ngpio, GFP_KERNEL);
Stephen Boyde4371f6e2018-03-23 09:34:50 -0700364 if (!p)
365 return NULL;
366
367 /* Assume by default all GPIOs are valid */
368 bitmap_fill(p, chip->ngpio);
369
370 return p;
371}
372
Linus Walleijf626d6d2019-07-17 09:10:01 +0200373static int gpiochip_alloc_valid_mask(struct gpio_chip *gc)
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700374{
Linus Walleijeb1e8bd2019-08-19 11:30:58 +0200375 if (!(of_gpio_need_valid_mask(gc) || gc->init_valid_mask))
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700376 return 0;
377
Linus Walleijf626d6d2019-07-17 09:10:01 +0200378 gc->valid_mask = gpiochip_allocate_mask(gc);
379 if (!gc->valid_mask)
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700380 return -ENOMEM;
381
382 return 0;
383}
384
Linus Walleijc9fc5af2019-08-19 10:49:04 +0200385static int gpiochip_init_valid_mask(struct gpio_chip *gc)
Ricardo Ribalda Delgadof8ec92a2018-10-05 08:52:58 +0200386{
Linus Walleijc9fc5af2019-08-19 10:49:04 +0200387 if (gc->init_valid_mask)
388 return gc->init_valid_mask(gc,
389 gc->valid_mask,
390 gc->ngpio);
Ricardo Ribalda Delgadof8ec92a2018-10-05 08:52:58 +0200391
392 return 0;
393}
394
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700395static void gpiochip_free_valid_mask(struct gpio_chip *gpiochip)
396{
Masahiro Yamada7bdbd1ec2019-07-18 15:51:01 +0900397 bitmap_free(gpiochip->valid_mask);
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700398 gpiochip->valid_mask = NULL;
399}
400
Andy Shevchenkob056ca12019-11-04 18:09:39 +0200401static int gpiochip_add_pin_ranges(struct gpio_chip *gc)
402{
403 if (gc->add_pin_ranges)
404 return gc->add_pin_ranges(gc);
405
406 return 0;
407}
408
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700409bool gpiochip_line_is_valid(const struct gpio_chip *gpiochip,
410 unsigned int offset)
411{
412 /* No mask means all valid */
413 if (likely(!gpiochip->valid_mask))
414 return true;
415 return test_bit(offset, gpiochip->valid_mask);
416}
417EXPORT_SYMBOL_GPL(gpiochip_line_is_valid);
418
Linus Walleijd7c51b42016-04-26 10:35:29 +0200419/*
420 * GPIO line handle management
421 */
422
423/**
424 * struct linehandle_state - contains the state of a userspace handle
425 * @gdev: the GPIO device the handle pertains to
426 * @label: consumer label used to tag descriptors
427 * @descs: the GPIO descriptors held by this handle
428 * @numdescs: the number of descriptors held in the descs array
429 */
430struct linehandle_state {
431 struct gpio_device *gdev;
432 const char *label;
433 struct gpio_desc *descs[GPIOHANDLES_MAX];
434 u32 numdescs;
435};
436
Lars-Peter Clausene3e847c2016-10-18 16:54:05 +0200437#define GPIOHANDLE_REQUEST_VALID_FLAGS \
438 (GPIOHANDLE_REQUEST_INPUT | \
439 GPIOHANDLE_REQUEST_OUTPUT | \
440 GPIOHANDLE_REQUEST_ACTIVE_LOW | \
Drew Fustini9225d512019-11-05 10:04:23 +0800441 GPIOHANDLE_REQUEST_BIAS_PULL_UP | \
442 GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | \
Kent Gibson2148ad72019-11-05 10:04:25 +0800443 GPIOHANDLE_REQUEST_BIAS_DISABLE | \
Lars-Peter Clausene3e847c2016-10-18 16:54:05 +0200444 GPIOHANDLE_REQUEST_OPEN_DRAIN | \
445 GPIOHANDLE_REQUEST_OPEN_SOURCE)
446
Kent Gibsonb043ed72019-11-05 10:04:28 +0800447static int linehandle_validate_flags(u32 flags)
448{
449 /* Return an error if an unknown flag is set */
450 if (flags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
451 return -EINVAL;
452
453 /*
454 * Do not allow both INPUT & OUTPUT flags to be set as they are
455 * contradictory.
456 */
457 if ((flags & GPIOHANDLE_REQUEST_INPUT) &&
458 (flags & GPIOHANDLE_REQUEST_OUTPUT))
459 return -EINVAL;
460
461 /*
462 * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
463 * the hardware actually supports enabling both at the same time the
464 * electrical result would be disastrous.
465 */
466 if ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) &&
467 (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
468 return -EINVAL;
469
470 /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
471 if (!(flags & GPIOHANDLE_REQUEST_OUTPUT) &&
472 ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
473 (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
474 return -EINVAL;
475
476 /* Bias flags only allowed for input or output mode. */
477 if (!((flags & GPIOHANDLE_REQUEST_INPUT) ||
478 (flags & GPIOHANDLE_REQUEST_OUTPUT)) &&
479 ((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) ||
480 (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP) ||
481 (flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN)))
482 return -EINVAL;
483
484 /* Only one bias flag can be set. */
485 if (((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
486 (flags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
487 GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
488 ((flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
489 (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
490 return -EINVAL;
491
492 return 0;
493}
494
Kent Gibsone588bb12019-11-05 10:04:29 +0800495static long linehandle_set_config(struct linehandle_state *lh,
496 void __user *ip)
497{
498 struct gpiohandle_config gcnf;
499 struct gpio_desc *desc;
500 int i, ret;
501 u32 lflags;
502 unsigned long *flagsp;
503
504 if (copy_from_user(&gcnf, ip, sizeof(gcnf)))
505 return -EFAULT;
506
507 lflags = gcnf.flags;
508 ret = linehandle_validate_flags(lflags);
509 if (ret)
510 return ret;
511
512 for (i = 0; i < lh->numdescs; i++) {
513 desc = lh->descs[i];
514 flagsp = &desc->flags;
515
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +0200516 assign_bit(FLAG_ACTIVE_LOW, flagsp,
Kent Gibsone588bb12019-11-05 10:04:29 +0800517 lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW);
518
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +0200519 assign_bit(FLAG_OPEN_DRAIN, flagsp,
Kent Gibsone588bb12019-11-05 10:04:29 +0800520 lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN);
521
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +0200522 assign_bit(FLAG_OPEN_SOURCE, flagsp,
Kent Gibsone588bb12019-11-05 10:04:29 +0800523 lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE);
524
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +0200525 assign_bit(FLAG_PULL_UP, flagsp,
Kent Gibsone588bb12019-11-05 10:04:29 +0800526 lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP);
527
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +0200528 assign_bit(FLAG_PULL_DOWN, flagsp,
Kent Gibsone588bb12019-11-05 10:04:29 +0800529 lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN);
530
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +0200531 assign_bit(FLAG_BIAS_DISABLE, flagsp,
Kent Gibsone588bb12019-11-05 10:04:29 +0800532 lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE);
533
534 /*
535 * Lines have to be requested explicitly for input
536 * or output, else the line will be treated "as is".
537 */
538 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
539 int val = !!gcnf.default_values[i];
540
541 ret = gpiod_direction_output(desc, val);
542 if (ret)
543 return ret;
544 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
545 ret = gpiod_direction_input(desc);
546 if (ret)
547 return ret;
548 }
Bartosz Golaszewski51c10642019-11-22 15:19:21 +0100549
550 atomic_notifier_call_chain(&desc->gdev->notifier,
551 GPIOLINE_CHANGED_CONFIG, desc);
Kent Gibsone588bb12019-11-05 10:04:29 +0800552 }
553 return 0;
554}
555
Linus Walleijd7c51b42016-04-26 10:35:29 +0200556static long linehandle_ioctl(struct file *filep, unsigned int cmd,
557 unsigned long arg)
558{
559 struct linehandle_state *lh = filep->private_data;
560 void __user *ip = (void __user *)arg;
561 struct gpiohandle_data ghd;
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +0200562 DECLARE_BITMAP(vals, GPIOHANDLES_MAX);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200563 int i;
564
565 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
Bartosz Golaszewski2b955b32018-07-16 10:34:24 +0200566 /* NOTE: It's ok to read values of output lines. */
Lukas Wunnereec1d562017-10-12 12:40:10 +0200567 int ret = gpiod_get_array_value_complex(false,
568 true,
569 lh->numdescs,
570 lh->descs,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +0200571 NULL,
Lukas Wunnereec1d562017-10-12 12:40:10 +0200572 vals);
573 if (ret)
574 return ret;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200575
Lars-Peter Clausen3eded5d2016-10-18 16:54:02 +0200576 memset(&ghd, 0, sizeof(ghd));
Lukas Wunnereec1d562017-10-12 12:40:10 +0200577 for (i = 0; i < lh->numdescs; i++)
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +0200578 ghd.values[i] = test_bit(i, vals);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200579
580 if (copy_to_user(ip, &ghd, sizeof(ghd)))
581 return -EFAULT;
582
583 return 0;
584 } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
Bartosz Golaszewskie5332d52018-07-16 10:34:23 +0200585 /*
586 * All line descriptors were created at once with the same
587 * flags so just check if the first one is really output.
588 */
589 if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags))
590 return -EPERM;
591
Linus Walleijd7c51b42016-04-26 10:35:29 +0200592 if (copy_from_user(&ghd, ip, sizeof(ghd)))
593 return -EFAULT;
594
595 /* Clamp all values to [0,1] */
596 for (i = 0; i < lh->numdescs; i++)
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +0200597 __assign_bit(i, vals, ghd.values[i]);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200598
599 /* Reuse the array setting function */
Laura Abbott30277432018-05-21 10:57:07 -0700600 return gpiod_set_array_value_complex(false,
Linus Walleijd7c51b42016-04-26 10:35:29 +0200601 true,
602 lh->numdescs,
603 lh->descs,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +0200604 NULL,
Linus Walleijd7c51b42016-04-26 10:35:29 +0200605 vals);
Kent Gibsone588bb12019-11-05 10:04:29 +0800606 } else if (cmd == GPIOHANDLE_SET_CONFIG_IOCTL) {
607 return linehandle_set_config(lh, ip);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200608 }
609 return -EINVAL;
610}
611
612#ifdef CONFIG_COMPAT
613static long linehandle_ioctl_compat(struct file *filep, unsigned int cmd,
614 unsigned long arg)
615{
616 return linehandle_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
617}
618#endif
619
620static int linehandle_release(struct inode *inode, struct file *filep)
621{
622 struct linehandle_state *lh = filep->private_data;
623 struct gpio_device *gdev = lh->gdev;
624 int i;
625
626 for (i = 0; i < lh->numdescs; i++)
627 gpiod_free(lh->descs[i]);
628 kfree(lh->label);
629 kfree(lh);
630 put_device(&gdev->dev);
631 return 0;
632}
633
634static const struct file_operations linehandle_fileops = {
635 .release = linehandle_release,
636 .owner = THIS_MODULE,
637 .llseek = noop_llseek,
638 .unlocked_ioctl = linehandle_ioctl,
639#ifdef CONFIG_COMPAT
640 .compat_ioctl = linehandle_ioctl_compat,
641#endif
642};
643
644static int linehandle_create(struct gpio_device *gdev, void __user *ip)
645{
646 struct gpiohandle_request handlereq;
647 struct linehandle_state *lh;
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200648 struct file *file;
Timur Tabiab3dbcf2018-03-29 13:29:12 -0500649 int fd, i, count = 0, ret;
Bartosz Golaszewski418ee8e2017-10-16 11:32:29 +0200650 u32 lflags;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200651
652 if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
653 return -EFAULT;
654 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
655 return -EINVAL;
656
Bartosz Golaszewski418ee8e2017-10-16 11:32:29 +0200657 lflags = handlereq.flags;
658
Kent Gibsonb043ed72019-11-05 10:04:28 +0800659 ret = linehandle_validate_flags(lflags);
660 if (ret)
661 return ret;
Kent Gibson2148ad72019-11-05 10:04:25 +0800662
Linus Walleijd7c51b42016-04-26 10:35:29 +0200663 lh = kzalloc(sizeof(*lh), GFP_KERNEL);
664 if (!lh)
665 return -ENOMEM;
666 lh->gdev = gdev;
667 get_device(&gdev->dev);
668
669 /* Make sure this is terminated */
670 handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
671 if (strlen(handlereq.consumer_label)) {
672 lh->label = kstrdup(handlereq.consumer_label,
673 GFP_KERNEL);
674 if (!lh->label) {
675 ret = -ENOMEM;
676 goto out_free_lh;
677 }
678 }
679
680 /* Request each GPIO */
681 for (i = 0; i < handlereq.lines; i++) {
682 u32 offset = handlereq.lineoffsets[i];
Bartosz Golaszewski0f41dab2019-12-24 13:07:00 +0100683 struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200684
Bartosz Golaszewski0f41dab2019-12-24 13:07:00 +0100685 if (IS_ERR(desc)) {
686 ret = PTR_ERR(desc);
Lars-Peter Clausene405f9f2016-10-18 16:54:01 +0200687 goto out_free_descs;
688 }
689
Linus Walleijd7c51b42016-04-26 10:35:29 +0200690 ret = gpiod_request(desc, lh->label);
691 if (ret)
692 goto out_free_descs;
693 lh->descs[i] = desc;
Ricardo Ribalda Delgado19a4fbf2018-09-13 15:37:04 +0200694 count = i + 1;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200695
696 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
697 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
698 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
699 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
700 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
701 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Kent Gibson2148ad72019-11-05 10:04:25 +0800702 if (lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE)
703 set_bit(FLAG_BIAS_DISABLE, &desc->flags);
Drew Fustini9225d512019-11-05 10:04:23 +0800704 if (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN)
705 set_bit(FLAG_PULL_DOWN, &desc->flags);
706 if (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)
707 set_bit(FLAG_PULL_UP, &desc->flags);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200708
Andrew Jefferye10f72b2017-11-30 14:25:24 +1030709 ret = gpiod_set_transitory(desc, false);
710 if (ret < 0)
711 goto out_free_descs;
712
Linus Walleijd7c51b42016-04-26 10:35:29 +0200713 /*
714 * Lines have to be requested explicitly for input
715 * or output, else the line will be treated "as is".
716 */
717 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
718 int val = !!handlereq.default_values[i];
719
720 ret = gpiod_direction_output(desc, val);
721 if (ret)
722 goto out_free_descs;
723 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
724 ret = gpiod_direction_input(desc);
725 if (ret)
726 goto out_free_descs;
727 }
728 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
729 offset);
730 }
Linus Walleije2f608b2016-06-18 10:56:43 +0200731 /* Let i point at the last handle */
732 i--;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200733 lh->numdescs = handlereq.lines;
734
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200735 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200736 if (fd < 0) {
737 ret = fd;
738 goto out_free_descs;
739 }
740
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200741 file = anon_inode_getfile("gpio-linehandle",
742 &linehandle_fileops,
743 lh,
744 O_RDONLY | O_CLOEXEC);
745 if (IS_ERR(file)) {
746 ret = PTR_ERR(file);
747 goto out_put_unused_fd;
748 }
749
Linus Walleijd7c51b42016-04-26 10:35:29 +0200750 handlereq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200751 if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200752 /*
753 * fput() will trigger the release() callback, so do not go onto
754 * the regular error cleanup path here.
755 */
756 fput(file);
757 put_unused_fd(fd);
758 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +0200759 }
Linus Walleijd7c51b42016-04-26 10:35:29 +0200760
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200761 fd_install(fd, file);
762
Linus Walleijd7c51b42016-04-26 10:35:29 +0200763 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
764 lh->numdescs);
765
766 return 0;
767
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200768out_put_unused_fd:
769 put_unused_fd(fd);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200770out_free_descs:
Timur Tabiab3dbcf2018-03-29 13:29:12 -0500771 for (i = 0; i < count; i++)
Linus Walleijd7c51b42016-04-26 10:35:29 +0200772 gpiod_free(lh->descs[i]);
773 kfree(lh->label);
774out_free_lh:
775 kfree(lh);
776 put_device(&gdev->dev);
777 return ret;
778}
779
Linus Walleij61f922d2016-06-02 11:30:15 +0200780/*
781 * GPIO line event management
782 */
783
784/**
785 * struct lineevent_state - contains the state of a userspace event
786 * @gdev: the GPIO device the event pertains to
787 * @label: consumer label used to tag descriptors
788 * @desc: the GPIO descriptor held by this event
789 * @eflags: the event flags this line was requested with
790 * @irq: the interrupt that trigger in response to events on this GPIO
791 * @wait: wait queue that handles blocking reads of events
792 * @events: KFIFO for the GPIO events
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100793 * @timestamp: cache for the timestamp storing it between hardirq
794 * and IRQ thread, used to bring the timestamp close to the actual
795 * event
Linus Walleij61f922d2016-06-02 11:30:15 +0200796 */
797struct lineevent_state {
798 struct gpio_device *gdev;
799 const char *label;
800 struct gpio_desc *desc;
801 u32 eflags;
802 int irq;
803 wait_queue_head_t wait;
804 DECLARE_KFIFO(events, struct gpioevent_data, 16);
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100805 u64 timestamp;
Linus Walleij61f922d2016-06-02 11:30:15 +0200806};
807
Lars-Peter Clausenac7dbb92016-10-18 16:54:06 +0200808#define GPIOEVENT_REQUEST_VALID_FLAGS \
809 (GPIOEVENT_REQUEST_RISING_EDGE | \
810 GPIOEVENT_REQUEST_FALLING_EDGE)
811
Al Viroafc9a422017-07-03 06:39:46 -0400812static __poll_t lineevent_poll(struct file *filep,
Linus Walleij61f922d2016-06-02 11:30:15 +0200813 struct poll_table_struct *wait)
814{
815 struct lineevent_state *le = filep->private_data;
Al Viroafc9a422017-07-03 06:39:46 -0400816 __poll_t events = 0;
Linus Walleij61f922d2016-06-02 11:30:15 +0200817
818 poll_wait(filep, &le->wait, wait);
819
Bartosz Golaszewskidea9c802019-11-27 12:19:21 +0100820 if (!kfifo_is_empty_spinlocked_noirqsave(&le->events, &le->wait.lock))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800821 events = EPOLLIN | EPOLLRDNORM;
Linus Walleij61f922d2016-06-02 11:30:15 +0200822
823 return events;
824}
825
826
827static ssize_t lineevent_read(struct file *filep,
828 char __user *buf,
829 size_t count,
830 loff_t *f_ps)
831{
832 struct lineevent_state *le = filep->private_data;
Bartosz Golaszewskidea9c802019-11-27 12:19:21 +0100833 struct gpioevent_data event;
834 ssize_t bytes_read = 0;
Linus Walleij61f922d2016-06-02 11:30:15 +0200835 int ret;
836
Bartosz Golaszewskidea9c802019-11-27 12:19:21 +0100837 if (count < sizeof(event))
Linus Walleij61f922d2016-06-02 11:30:15 +0200838 return -EINVAL;
839
840 do {
Bartosz Golaszewskidea9c802019-11-27 12:19:21 +0100841 spin_lock(&le->wait.lock);
Linus Walleij61f922d2016-06-02 11:30:15 +0200842 if (kfifo_is_empty(&le->events)) {
Bartosz Golaszewskidea9c802019-11-27 12:19:21 +0100843 if (bytes_read) {
844 spin_unlock(&le->wait.lock);
845 return bytes_read;
846 }
Linus Walleij61f922d2016-06-02 11:30:15 +0200847
Bartosz Golaszewskidea9c802019-11-27 12:19:21 +0100848 if (filep->f_flags & O_NONBLOCK) {
849 spin_unlock(&le->wait.lock);
850 return -EAGAIN;
851 }
852
853 ret = wait_event_interruptible_locked(le->wait,
Linus Walleij61f922d2016-06-02 11:30:15 +0200854 !kfifo_is_empty(&le->events));
Bartosz Golaszewskidea9c802019-11-27 12:19:21 +0100855 if (ret) {
856 spin_unlock(&le->wait.lock);
Linus Walleij61f922d2016-06-02 11:30:15 +0200857 return ret;
Bartosz Golaszewskidea9c802019-11-27 12:19:21 +0100858 }
Linus Walleij61f922d2016-06-02 11:30:15 +0200859 }
860
Bartosz Golaszewskidea9c802019-11-27 12:19:21 +0100861 ret = kfifo_out(&le->events, &event, 1);
862 spin_unlock(&le->wait.lock);
863 if (ret != 1) {
864 /*
865 * This should never happen - we were holding the lock
866 * from the moment we learned the fifo is no longer
867 * empty until now.
868 */
869 ret = -EIO;
870 break;
871 }
Linus Walleij61f922d2016-06-02 11:30:15 +0200872
Bartosz Golaszewskidea9c802019-11-27 12:19:21 +0100873 if (copy_to_user(buf + bytes_read, &event, sizeof(event)))
874 return -EFAULT;
875 bytes_read += sizeof(event);
876 } while (count >= bytes_read + sizeof(event));
Linus Walleij61f922d2016-06-02 11:30:15 +0200877
Bartosz Golaszewskidea9c802019-11-27 12:19:21 +0100878 return bytes_read;
Linus Walleij61f922d2016-06-02 11:30:15 +0200879}
880
881static int lineevent_release(struct inode *inode, struct file *filep)
882{
883 struct lineevent_state *le = filep->private_data;
884 struct gpio_device *gdev = le->gdev;
885
886 free_irq(le->irq, le);
887 gpiod_free(le->desc);
888 kfree(le->label);
889 kfree(le);
890 put_device(&gdev->dev);
891 return 0;
892}
893
894static long lineevent_ioctl(struct file *filep, unsigned int cmd,
895 unsigned long arg)
896{
897 struct lineevent_state *le = filep->private_data;
898 void __user *ip = (void __user *)arg;
899 struct gpiohandle_data ghd;
900
901 /*
902 * We can get the value for an event line but not set it,
903 * because it is input by definition.
904 */
905 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
906 int val;
907
Lars-Peter Clausend82aa4a2016-10-18 16:54:04 +0200908 memset(&ghd, 0, sizeof(ghd));
909
Linus Walleij61f922d2016-06-02 11:30:15 +0200910 val = gpiod_get_value_cansleep(le->desc);
911 if (val < 0)
912 return val;
913 ghd.values[0] = val;
914
915 if (copy_to_user(ip, &ghd, sizeof(ghd)))
916 return -EFAULT;
917
918 return 0;
919 }
920 return -EINVAL;
921}
922
923#ifdef CONFIG_COMPAT
924static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd,
925 unsigned long arg)
926{
927 return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
928}
929#endif
930
931static const struct file_operations lineevent_fileops = {
932 .release = lineevent_release,
933 .read = lineevent_read,
934 .poll = lineevent_poll,
935 .owner = THIS_MODULE,
936 .llseek = noop_llseek,
937 .unlocked_ioctl = lineevent_ioctl,
938#ifdef CONFIG_COMPAT
939 .compat_ioctl = lineevent_ioctl_compat,
940#endif
941};
942
Ben Dooks33265b12016-06-17 16:03:13 +0100943static irqreturn_t lineevent_irq_thread(int irq, void *p)
Linus Walleij61f922d2016-06-02 11:30:15 +0200944{
945 struct lineevent_state *le = p;
946 struct gpioevent_data ge;
Uwe Kleine-Königfa388692018-08-20 08:32:53 +0200947 int ret;
Linus Walleij61f922d2016-06-02 11:30:15 +0200948
Linus Walleij24bd3ef2018-01-22 13:19:28 +0100949 /* Do not leak kernel stack to userspace */
950 memset(&ge, 0, sizeof(ge));
951
Bartosz Golaszewski1033be52019-01-04 11:24:20 +0100952 /*
953 * We may be running from a nested threaded interrupt in which case
954 * we didn't get the timestamp from lineevent_irq_handler().
955 */
956 if (!le->timestamp)
Linus Walleijf8850202020-02-20 15:56:24 +0100957 ge.timestamp = ktime_get_ns();
Bartosz Golaszewski1033be52019-01-04 11:24:20 +0100958 else
959 ge.timestamp = le->timestamp;
Linus Walleij61f922d2016-06-02 11:30:15 +0200960
Bartosz Golaszewskiad537b82017-06-23 13:45:16 +0200961 if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
962 && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
Uwe Kleine-Königfa388692018-08-20 08:32:53 +0200963 int level = gpiod_get_value_cansleep(le->desc);
Linus Walleij61f922d2016-06-02 11:30:15 +0200964 if (level)
965 /* Emit low-to-high event */
966 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
967 else
968 /* Emit high-to-low event */
969 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Uwe Kleine-Königfa388692018-08-20 08:32:53 +0200970 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200971 /* Emit low-to-high event */
972 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
Uwe Kleine-Königfa388692018-08-20 08:32:53 +0200973 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200974 /* Emit high-to-low event */
975 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Arnd Bergmannbc0207a2016-06-16 11:02:41 +0200976 } else {
977 return IRQ_NONE;
Linus Walleij61f922d2016-06-02 11:30:15 +0200978 }
979
Bartosz Golaszewskidea9c802019-11-27 12:19:21 +0100980 ret = kfifo_in_spinlocked_noirqsave(&le->events, &ge,
981 1, &le->wait.lock);
Saiyam Doshi2efc6bf2019-09-07 23:09:10 +0530982 if (ret)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800983 wake_up_poll(&le->wait, EPOLLIN);
Bartosz Golaszewski248ae172019-11-29 11:22:18 +0100984 else
985 pr_debug_ratelimited("event FIFO is full - event dropped\n");
Linus Walleij61f922d2016-06-02 11:30:15 +0200986
987 return IRQ_HANDLED;
988}
989
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100990static irqreturn_t lineevent_irq_handler(int irq, void *p)
991{
992 struct lineevent_state *le = p;
993
994 /*
995 * Just store the timestamp in hardirq context so we get it as
996 * close in time as possible to the actual event.
997 */
Linus Walleijf8850202020-02-20 15:56:24 +0100998 le->timestamp = ktime_get_ns();
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100999
1000 return IRQ_WAKE_THREAD;
1001}
1002
Linus Walleij61f922d2016-06-02 11:30:15 +02001003static int lineevent_create(struct gpio_device *gdev, void __user *ip)
1004{
1005 struct gpioevent_request eventreq;
1006 struct lineevent_state *le;
1007 struct gpio_desc *desc;
Lars-Peter Clausen953b9562016-10-24 13:59:15 +02001008 struct file *file;
Linus Walleij61f922d2016-06-02 11:30:15 +02001009 u32 offset;
1010 u32 lflags;
1011 u32 eflags;
1012 int fd;
1013 int ret;
1014 int irqflags = 0;
1015
1016 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
1017 return -EFAULT;
1018
Bartosz Golaszewskibcc6d992019-09-16 11:46:23 +02001019 offset = eventreq.lineoffset;
1020 lflags = eventreq.handleflags;
1021 eflags = eventreq.eventflags;
1022
Bartosz Golaszewski45e23602019-12-24 13:07:01 +01001023 desc = gpiochip_get_desc(gdev->chip, offset);
1024 if (IS_ERR(desc))
1025 return PTR_ERR(desc);
Bartosz Golaszewskibcc6d992019-09-16 11:46:23 +02001026
1027 /* Return an error if a unknown flag is set */
1028 if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
1029 (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS))
1030 return -EINVAL;
1031
1032 /* This is just wrong: we don't look for events on output lines */
1033 if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) ||
1034 (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
1035 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
1036 return -EINVAL;
1037
Kent Gibson2148ad72019-11-05 10:04:25 +08001038 /* Only one bias flag can be set. */
1039 if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
1040 (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
1041 GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
1042 ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
1043 (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
1044 return -EINVAL;
1045
Linus Walleij61f922d2016-06-02 11:30:15 +02001046 le = kzalloc(sizeof(*le), GFP_KERNEL);
1047 if (!le)
1048 return -ENOMEM;
1049 le->gdev = gdev;
1050 get_device(&gdev->dev);
1051
1052 /* Make sure this is terminated */
1053 eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
1054 if (strlen(eventreq.consumer_label)) {
1055 le->label = kstrdup(eventreq.consumer_label,
1056 GFP_KERNEL);
1057 if (!le->label) {
1058 ret = -ENOMEM;
1059 goto out_free_le;
1060 }
1061 }
1062
Linus Walleij61f922d2016-06-02 11:30:15 +02001063 ret = gpiod_request(desc, le->label);
1064 if (ret)
Uwe Kleine-Königf001cc32018-04-16 13:17:53 +02001065 goto out_free_label;
Linus Walleij61f922d2016-06-02 11:30:15 +02001066 le->desc = desc;
1067 le->eflags = eflags;
1068
1069 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
1070 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
Kent Gibson2148ad72019-11-05 10:04:25 +08001071 if (lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE)
1072 set_bit(FLAG_BIAS_DISABLE, &desc->flags);
Kent Gibson7b479a82019-11-05 10:04:24 +08001073 if (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN)
1074 set_bit(FLAG_PULL_DOWN, &desc->flags);
1075 if (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)
1076 set_bit(FLAG_PULL_UP, &desc->flags);
Linus Walleij61f922d2016-06-02 11:30:15 +02001077
1078 ret = gpiod_direction_input(desc);
1079 if (ret)
1080 goto out_free_desc;
1081
1082 le->irq = gpiod_to_irq(desc);
1083 if (le->irq <= 0) {
1084 ret = -ENODEV;
1085 goto out_free_desc;
1086 }
1087
1088 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
Michael Wu223ecaf2019-07-08 13:23:08 +08001089 irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
1090 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
Linus Walleij61f922d2016-06-02 11:30:15 +02001091 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
Michael Wu223ecaf2019-07-08 13:23:08 +08001092 irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
1093 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
Linus Walleij61f922d2016-06-02 11:30:15 +02001094 irqflags |= IRQF_ONESHOT;
Linus Walleij61f922d2016-06-02 11:30:15 +02001095
1096 INIT_KFIFO(le->events);
1097 init_waitqueue_head(&le->wait);
Linus Walleij61f922d2016-06-02 11:30:15 +02001098
1099 /* Request a thread to read the events */
1100 ret = request_threaded_irq(le->irq,
Linus Walleijd58f2bf2017-11-30 10:23:27 +01001101 lineevent_irq_handler,
Linus Walleij61f922d2016-06-02 11:30:15 +02001102 lineevent_irq_thread,
1103 irqflags,
1104 le->label,
1105 le);
1106 if (ret)
1107 goto out_free_desc;
1108
Lars-Peter Clausen953b9562016-10-24 13:59:15 +02001109 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleij61f922d2016-06-02 11:30:15 +02001110 if (fd < 0) {
1111 ret = fd;
1112 goto out_free_irq;
1113 }
1114
Lars-Peter Clausen953b9562016-10-24 13:59:15 +02001115 file = anon_inode_getfile("gpio-event",
1116 &lineevent_fileops,
1117 le,
1118 O_RDONLY | O_CLOEXEC);
1119 if (IS_ERR(file)) {
1120 ret = PTR_ERR(file);
1121 goto out_put_unused_fd;
1122 }
1123
Linus Walleij61f922d2016-06-02 11:30:15 +02001124 eventreq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +02001125 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +02001126 /*
1127 * fput() will trigger the release() callback, so do not go onto
1128 * the regular error cleanup path here.
1129 */
1130 fput(file);
1131 put_unused_fd(fd);
1132 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +02001133 }
Linus Walleij61f922d2016-06-02 11:30:15 +02001134
Lars-Peter Clausen953b9562016-10-24 13:59:15 +02001135 fd_install(fd, file);
1136
Linus Walleij61f922d2016-06-02 11:30:15 +02001137 return 0;
1138
Lars-Peter Clausen953b9562016-10-24 13:59:15 +02001139out_put_unused_fd:
1140 put_unused_fd(fd);
Linus Walleij61f922d2016-06-02 11:30:15 +02001141out_free_irq:
1142 free_irq(le->irq, le);
1143out_free_desc:
1144 gpiod_free(le->desc);
1145out_free_label:
1146 kfree(le->label);
1147out_free_le:
1148 kfree(le);
1149 put_device(&gdev->dev);
1150 return ret;
1151}
1152
Bartosz Golaszewskid2ac2572019-12-03 14:08:44 +01001153static void gpio_desc_to_lineinfo(struct gpio_desc *desc,
1154 struct gpioline_info *info)
1155{
1156 struct gpio_chip *chip = desc->gdev->chip;
1157 unsigned long flags;
1158
1159 spin_lock_irqsave(&gpio_lock, flags);
1160
1161 if (desc->name) {
1162 strncpy(info->name, desc->name, sizeof(info->name));
1163 info->name[sizeof(info->name) - 1] = '\0';
1164 } else {
1165 info->name[0] = '\0';
1166 }
1167
1168 if (desc->label) {
1169 strncpy(info->consumer, desc->label, sizeof(info->consumer));
1170 info->consumer[sizeof(info->consumer) - 1] = '\0';
1171 } else {
1172 info->consumer[0] = '\0';
1173 }
1174
1175 /*
1176 * Userspace only need to know that the kernel is using this GPIO so
1177 * it can't use it.
1178 */
1179 info->flags = 0;
1180 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
1181 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
1182 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
1183 test_bit(FLAG_EXPORT, &desc->flags) ||
1184 test_bit(FLAG_SYSFS, &desc->flags) ||
1185 !pinctrl_gpio_can_use_line(chip->base + info->line_offset))
1186 info->flags |= GPIOLINE_FLAG_KERNEL;
1187 if (test_bit(FLAG_IS_OUT, &desc->flags))
1188 info->flags |= GPIOLINE_FLAG_IS_OUT;
1189 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1190 info->flags |= GPIOLINE_FLAG_ACTIVE_LOW;
1191 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1192 info->flags |= (GPIOLINE_FLAG_OPEN_DRAIN |
1193 GPIOLINE_FLAG_IS_OUT);
1194 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1195 info->flags |= (GPIOLINE_FLAG_OPEN_SOURCE |
1196 GPIOLINE_FLAG_IS_OUT);
1197 if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
1198 info->flags |= GPIOLINE_FLAG_BIAS_DISABLE;
1199 if (test_bit(FLAG_PULL_DOWN, &desc->flags))
1200 info->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN;
1201 if (test_bit(FLAG_PULL_UP, &desc->flags))
1202 info->flags |= GPIOLINE_FLAG_BIAS_PULL_UP;
1203
1204 spin_unlock_irqrestore(&gpio_lock, flags);
1205}
1206
Bartosz Golaszewski51c10642019-11-22 15:19:21 +01001207struct gpio_chardev_data {
1208 struct gpio_device *gdev;
1209 wait_queue_head_t wait;
1210 DECLARE_KFIFO(events, struct gpioline_info_changed, 32);
1211 struct notifier_block lineinfo_changed_nb;
1212 unsigned long *watched_lines;
1213};
1214
Thierry Reding950d55f52017-07-24 16:57:22 +02001215/*
Linus Walleij3c702e92015-10-21 15:29:53 +02001216 * gpio_ioctl() - ioctl handler for the GPIO chardev
1217 */
1218static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1219{
Bartosz Golaszewski51c10642019-11-22 15:19:21 +01001220 struct gpio_chardev_data *priv = filp->private_data;
1221 struct gpio_device *gdev = priv->gdev;
Linus Walleij3c702e92015-10-21 15:29:53 +02001222 struct gpio_chip *chip = gdev->chip;
Linus Walleij8b92e172016-05-27 14:24:04 +02001223 void __user *ip = (void __user *)arg;
Bartosz Golaszewski51c10642019-11-22 15:19:21 +01001224 struct gpio_desc *desc;
1225 __u32 offset;
Linus Walleij3c702e92015-10-21 15:29:53 +02001226
1227 /* We fail any subsequent ioctl():s when the chip is gone */
1228 if (!chip)
1229 return -ENODEV;
1230
Linus Walleij521a2ad2016-02-12 22:25:22 +01001231 /* Fill in the struct and pass to userspace */
Linus Walleij3c702e92015-10-21 15:29:53 +02001232 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
Linus Walleij521a2ad2016-02-12 22:25:22 +01001233 struct gpiochip_info chipinfo;
1234
Lars-Peter Clausen0f4bbb22016-10-18 16:54:00 +02001235 memset(&chipinfo, 0, sizeof(chipinfo));
1236
Linus Walleij3c702e92015-10-21 15:29:53 +02001237 strncpy(chipinfo.name, dev_name(&gdev->dev),
1238 sizeof(chipinfo.name));
1239 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
Linus Walleijdf4878e2016-02-12 14:48:23 +01001240 strncpy(chipinfo.label, gdev->label,
1241 sizeof(chipinfo.label));
1242 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001243 chipinfo.lines = gdev->ngpio;
Linus Walleij3c702e92015-10-21 15:29:53 +02001244 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
1245 return -EFAULT;
1246 return 0;
Bartosz Golaszewski51c10642019-11-22 15:19:21 +01001247 } else if (cmd == GPIO_GET_LINEINFO_IOCTL ||
1248 cmd == GPIO_GET_LINEINFO_WATCH_IOCTL) {
Linus Walleij521a2ad2016-02-12 22:25:22 +01001249 struct gpioline_info lineinfo;
Linus Walleij521a2ad2016-02-12 22:25:22 +01001250
1251 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
1252 return -EFAULT;
Linus Walleij521a2ad2016-02-12 22:25:22 +01001253
Bartosz Golaszewski2a2cabd2019-12-24 13:07:02 +01001254 desc = gpiochip_get_desc(chip, lineinfo.line_offset);
1255 if (IS_ERR(desc))
1256 return PTR_ERR(desc);
1257
Bartosz Golaszewskid2ac2572019-12-03 14:08:44 +01001258 gpio_desc_to_lineinfo(desc, &lineinfo);
Linus Walleij521a2ad2016-02-12 22:25:22 +01001259
1260 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
1261 return -EFAULT;
Bartosz Golaszewski51c10642019-11-22 15:19:21 +01001262
1263 if (cmd == GPIO_GET_LINEINFO_WATCH_IOCTL)
1264 set_bit(desc_to_gpio(desc), priv->watched_lines);
1265
Linus Walleij521a2ad2016-02-12 22:25:22 +01001266 return 0;
Linus Walleijd7c51b42016-04-26 10:35:29 +02001267 } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
1268 return linehandle_create(gdev, ip);
Linus Walleij61f922d2016-06-02 11:30:15 +02001269 } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
1270 return lineevent_create(gdev, ip);
Bartosz Golaszewski51c10642019-11-22 15:19:21 +01001271 } else if (cmd == GPIO_GET_LINEINFO_UNWATCH_IOCTL) {
1272 if (copy_from_user(&offset, ip, sizeof(offset)))
1273 return -EFAULT;
1274
1275 desc = gpiochip_get_desc(chip, offset);
1276 if (IS_ERR(desc))
1277 return PTR_ERR(desc);
1278
1279 clear_bit(desc_to_gpio(desc), &desc->flags);
1280 return 0;
Linus Walleij3c702e92015-10-21 15:29:53 +02001281 }
1282 return -EINVAL;
1283}
1284
Linus Walleij8b92e172016-05-27 14:24:04 +02001285#ifdef CONFIG_COMPAT
1286static long gpio_ioctl_compat(struct file *filp, unsigned int cmd,
1287 unsigned long arg)
1288{
1289 return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
1290}
1291#endif
1292
Bartosz Golaszewski51c10642019-11-22 15:19:21 +01001293static struct gpio_chardev_data *
1294to_gpio_chardev_data(struct notifier_block *nb)
1295{
1296 return container_of(nb, struct gpio_chardev_data, lineinfo_changed_nb);
1297}
1298
1299static int lineinfo_changed_notify(struct notifier_block *nb,
1300 unsigned long action, void *data)
1301{
1302 struct gpio_chardev_data *priv = to_gpio_chardev_data(nb);
1303 struct gpioline_info_changed chg;
1304 struct gpio_desc *desc = data;
1305 int ret;
1306
1307 if (!test_bit(desc_to_gpio(desc), priv->watched_lines))
1308 return NOTIFY_DONE;
1309
1310 memset(&chg, 0, sizeof(chg));
1311 chg.info.line_offset = gpio_chip_hwgpio(desc);
1312 chg.event_type = action;
1313 chg.timestamp = ktime_get_ns();
1314 gpio_desc_to_lineinfo(desc, &chg.info);
1315
1316 ret = kfifo_in_spinlocked(&priv->events, &chg, 1, &priv->wait.lock);
1317 if (ret)
1318 wake_up_poll(&priv->wait, EPOLLIN);
1319 else
1320 pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n");
1321
1322 return NOTIFY_OK;
1323}
1324
1325static __poll_t lineinfo_watch_poll(struct file *filep,
1326 struct poll_table_struct *pollt)
1327{
1328 struct gpio_chardev_data *priv = filep->private_data;
1329 __poll_t events = 0;
1330
1331 poll_wait(filep, &priv->wait, pollt);
1332
1333 if (!kfifo_is_empty_spinlocked_noirqsave(&priv->events,
1334 &priv->wait.lock))
1335 events = EPOLLIN | EPOLLRDNORM;
1336
1337 return events;
1338}
1339
1340static ssize_t lineinfo_watch_read(struct file *filep, char __user *buf,
1341 size_t count, loff_t *off)
1342{
1343 struct gpio_chardev_data *priv = filep->private_data;
1344 struct gpioline_info_changed event;
1345 ssize_t bytes_read = 0;
1346 int ret;
1347
1348 if (count < sizeof(event))
1349 return -EINVAL;
1350
1351 do {
1352 spin_lock(&priv->wait.lock);
1353 if (kfifo_is_empty(&priv->events)) {
1354 if (bytes_read) {
1355 spin_unlock(&priv->wait.lock);
1356 return bytes_read;
1357 }
1358
1359 if (filep->f_flags & O_NONBLOCK) {
1360 spin_unlock(&priv->wait.lock);
1361 return -EAGAIN;
1362 }
1363
1364 ret = wait_event_interruptible_locked(priv->wait,
1365 !kfifo_is_empty(&priv->events));
1366 if (ret) {
1367 spin_unlock(&priv->wait.lock);
1368 return ret;
1369 }
1370 }
1371
1372 ret = kfifo_out(&priv->events, &event, 1);
1373 spin_unlock(&priv->wait.lock);
1374 if (ret != 1) {
1375 ret = -EIO;
1376 break;
1377 /* We should never get here. See lineevent_read(). */
1378 }
1379
1380 if (copy_to_user(buf + bytes_read, &event, sizeof(event)))
1381 return -EFAULT;
1382 bytes_read += sizeof(event);
1383 } while (count >= bytes_read + sizeof(event));
1384
1385 return bytes_read;
1386}
1387
Linus Walleij3c702e92015-10-21 15:29:53 +02001388/**
1389 * gpio_chrdev_open() - open the chardev for ioctl operations
1390 * @inode: inode for this chardev
1391 * @filp: file struct for storing private data
1392 * Returns 0 on success
1393 */
1394static int gpio_chrdev_open(struct inode *inode, struct file *filp)
1395{
1396 struct gpio_device *gdev = container_of(inode->i_cdev,
1397 struct gpio_device, chrdev);
Bartosz Golaszewski51c10642019-11-22 15:19:21 +01001398 struct gpio_chardev_data *priv;
1399 int ret = -ENOMEM;
Linus Walleij3c702e92015-10-21 15:29:53 +02001400
1401 /* Fail on open if the backing gpiochip is gone */
Stephen Boydfb505742017-01-09 11:47:44 -08001402 if (!gdev->chip)
Linus Walleij3c702e92015-10-21 15:29:53 +02001403 return -ENODEV;
Lars-Peter Clausenf4e81c52016-11-30 13:05:21 +01001404
Bartosz Golaszewski51c10642019-11-22 15:19:21 +01001405 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1406 if (!priv)
1407 return -ENOMEM;
1408
1409 priv->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL);
1410 if (!priv->watched_lines)
1411 goto out_free_priv;
1412
1413 init_waitqueue_head(&priv->wait);
1414 INIT_KFIFO(priv->events);
1415 priv->gdev = gdev;
1416
1417 priv->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify;
1418 ret = atomic_notifier_chain_register(&gdev->notifier,
1419 &priv->lineinfo_changed_nb);
1420 if (ret)
1421 goto out_free_bitmap;
1422
1423 get_device(&gdev->dev);
1424 filp->private_data = priv;
1425
1426 ret = nonseekable_open(inode, filp);
1427 if (ret)
1428 goto out_unregister_notifier;
1429
1430 return ret;
1431
1432out_unregister_notifier:
1433 atomic_notifier_chain_unregister(&gdev->notifier,
1434 &priv->lineinfo_changed_nb);
1435out_free_bitmap:
1436 bitmap_free(priv->watched_lines);
1437out_free_priv:
1438 kfree(priv);
1439 return ret;
Linus Walleij3c702e92015-10-21 15:29:53 +02001440}
1441
1442/**
1443 * gpio_chrdev_release() - close chardev after ioctl operations
1444 * @inode: inode for this chardev
1445 * @filp: file struct for storing private data
1446 * Returns 0 on success
1447 */
1448static int gpio_chrdev_release(struct inode *inode, struct file *filp)
1449{
Bartosz Golaszewski51c10642019-11-22 15:19:21 +01001450 struct gpio_chardev_data *priv = filp->private_data;
1451 struct gpio_device *gdev = priv->gdev;
Linus Walleij3c702e92015-10-21 15:29:53 +02001452
Bartosz Golaszewski51c10642019-11-22 15:19:21 +01001453 bitmap_free(priv->watched_lines);
1454 atomic_notifier_chain_unregister(&gdev->notifier,
1455 &priv->lineinfo_changed_nb);
Linus Walleij3c702e92015-10-21 15:29:53 +02001456 put_device(&gdev->dev);
Bartosz Golaszewski51c10642019-11-22 15:19:21 +01001457 kfree(priv);
1458
Linus Walleij3c702e92015-10-21 15:29:53 +02001459 return 0;
1460}
1461
Linus Walleij3c702e92015-10-21 15:29:53 +02001462static const struct file_operations gpio_fileops = {
1463 .release = gpio_chrdev_release,
1464 .open = gpio_chrdev_open,
Bartosz Golaszewski51c10642019-11-22 15:19:21 +01001465 .poll = lineinfo_watch_poll,
1466 .read = lineinfo_watch_read,
Linus Walleij3c702e92015-10-21 15:29:53 +02001467 .owner = THIS_MODULE,
Lars-Peter Clausenf4e81c52016-11-30 13:05:21 +01001468 .llseek = no_llseek,
Linus Walleij3c702e92015-10-21 15:29:53 +02001469 .unlocked_ioctl = gpio_ioctl,
Linus Walleij8b92e172016-05-27 14:24:04 +02001470#ifdef CONFIG_COMPAT
1471 .compat_ioctl = gpio_ioctl_compat,
1472#endif
Linus Walleij3c702e92015-10-21 15:29:53 +02001473};
1474
Linus Walleijff2b1352015-10-20 11:10:38 +02001475static void gpiodevice_release(struct device *dev)
1476{
1477 struct gpio_device *gdev = dev_get_drvdata(dev);
1478
1479 list_del(&gdev->list);
1480 ida_simple_remove(&gpio_ida, gdev->id);
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +01001481 kfree_const(gdev->label);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001482 kfree(gdev->descs);
Linus Walleij9efd9e62016-02-09 14:27:42 +01001483 kfree(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001484}
1485
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001486static int gpiochip_setup_dev(struct gpio_device *gdev)
1487{
Linus Walleijd377f562019-07-16 11:11:45 +02001488 int ret;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001489
1490 cdev_init(&gdev->chrdev, &gpio_fileops);
1491 gdev->chrdev.owner = THIS_MODULE;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001492 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001493
Linus Walleijd377f562019-07-16 11:11:45 +02001494 ret = cdev_device_add(&gdev->chrdev, &gdev->dev);
1495 if (ret)
1496 return ret;
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001497
1498 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
1499 MAJOR(gpio_devt), gdev->id);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001500
Linus Walleijd377f562019-07-16 11:11:45 +02001501 ret = gpiochip_sysfs_register(gdev);
1502 if (ret)
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001503 goto err_remove_device;
1504
1505 /* From this point, the .release() function cleans up gpio_device */
1506 gdev->dev.release = gpiodevice_release;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001507 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
1508 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
1509 dev_name(&gdev->dev), gdev->chip->label ? : "generic");
1510
1511 return 0;
1512
1513err_remove_device:
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001514 cdev_device_del(&gdev->chrdev, &gdev->dev);
Linus Walleijd377f562019-07-16 11:11:45 +02001515 return ret;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001516}
1517
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02001518static void gpiochip_machine_hog(struct gpio_chip *chip, struct gpiod_hog *hog)
1519{
1520 struct gpio_desc *desc;
1521 int rv;
1522
1523 desc = gpiochip_get_desc(chip, hog->chip_hwnum);
1524 if (IS_ERR(desc)) {
1525 pr_err("%s: unable to get GPIO desc: %ld\n",
1526 __func__, PTR_ERR(desc));
1527 return;
1528 }
1529
Dan Carpenterba3efdf2018-05-01 10:36:39 +03001530 if (test_bit(FLAG_IS_HOGGED, &desc->flags))
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02001531 return;
1532
1533 rv = gpiod_hog(desc, hog->line_name, hog->lflags, hog->dflags);
1534 if (rv)
1535 pr_err("%s: unable to hog GPIO line (%s:%u): %d\n",
1536 __func__, chip->label, hog->chip_hwnum, rv);
1537}
1538
1539static void machine_gpiochip_add(struct gpio_chip *chip)
1540{
1541 struct gpiod_hog *hog;
1542
1543 mutex_lock(&gpio_machine_hogs_mutex);
1544
1545 list_for_each_entry(hog, &gpio_machine_hogs, list) {
1546 if (!strcmp(chip->label, hog->chip_label))
1547 gpiochip_machine_hog(chip, hog);
1548 }
1549
1550 mutex_unlock(&gpio_machine_hogs_mutex);
1551}
1552
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001553static void gpiochip_setup_devs(void)
1554{
1555 struct gpio_device *gdev;
Linus Walleijd377f562019-07-16 11:11:45 +02001556 int ret;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001557
1558 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijd377f562019-07-16 11:11:45 +02001559 ret = gpiochip_setup_dev(gdev);
1560 if (ret)
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001561 pr_err("%s: Failed to initialize gpio device (%d)\n",
Linus Walleijd377f562019-07-16 11:11:45 +02001562 dev_name(&gdev->dev), ret);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001563 }
1564}
1565
Thierry Reding959bc7b2017-11-07 19:15:59 +01001566int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001567 struct lock_class_key *lock_key,
1568 struct lock_class_key *request_key)
David Brownelld2876d02008-02-04 22:28:20 -08001569{
1570 unsigned long flags;
Linus Walleijd377f562019-07-16 11:11:45 +02001571 int ret = 0;
Linus Walleijff2b1352015-10-20 11:10:38 +02001572 unsigned i;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001573 int base = chip->base;
Linus Walleijff2b1352015-10-20 11:10:38 +02001574 struct gpio_device *gdev;
David Brownelld2876d02008-02-04 22:28:20 -08001575
Linus Walleijff2b1352015-10-20 11:10:38 +02001576 /*
1577 * First: allocate and populate the internal stat container, and
1578 * set up the struct device.
1579 */
Josh Cartwright969f07b2016-02-17 16:44:15 -06001580 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
Linus Walleijff2b1352015-10-20 11:10:38 +02001581 if (!gdev)
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001582 return -ENOMEM;
Linus Walleij3c702e92015-10-21 15:29:53 +02001583 gdev->dev.bus = &gpio_bus_type;
Linus Walleijff2b1352015-10-20 11:10:38 +02001584 gdev->chip = chip;
1585 chip->gpiodev = gdev;
1586 if (chip->parent) {
1587 gdev->dev.parent = chip->parent;
1588 gdev->dev.of_node = chip->parent->of_node;
Thierry Redingacc6e332016-07-05 14:11:14 +02001589 }
1590
Linus Walleijff2b1352015-10-20 11:10:38 +02001591#ifdef CONFIG_OF_GPIO
1592 /* If the gpiochip has an assigned OF node this takes precedence */
Thierry Redingacc6e332016-07-05 14:11:14 +02001593 if (chip->of_node)
1594 gdev->dev.of_node = chip->of_node;
Biju Das6ff04972018-08-06 10:48:01 +01001595 else
1596 chip->of_node = gdev->dev.of_node;
Linus Walleijff2b1352015-10-20 11:10:38 +02001597#endif
Thierry Redingacc6e332016-07-05 14:11:14 +02001598
Linus Walleijff2b1352015-10-20 11:10:38 +02001599 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
1600 if (gdev->id < 0) {
Linus Walleijd377f562019-07-16 11:11:45 +02001601 ret = gdev->id;
Linus Walleijff2b1352015-10-20 11:10:38 +02001602 goto err_free_gdev;
1603 }
Geert Uytterhoevenddd88912019-11-27 09:42:47 +01001604 dev_set_name(&gdev->dev, GPIOCHIP_NAME "%d", gdev->id);
Linus Walleijff2b1352015-10-20 11:10:38 +02001605 device_initialize(&gdev->dev);
1606 dev_set_drvdata(&gdev->dev, gdev);
1607 if (chip->parent && chip->parent->driver)
1608 gdev->owner = chip->parent->driver->owner;
1609 else if (chip->owner)
1610 /* TODO: remove chip->owner */
1611 gdev->owner = chip->owner;
1612 else
1613 gdev->owner = THIS_MODULE;
David Brownelld2876d02008-02-04 22:28:20 -08001614
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001615 gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001616 if (!gdev->descs) {
Linus Walleijd377f562019-07-16 11:11:45 +02001617 ret = -ENOMEM;
Vladimir Zapolskiya05a1402018-11-02 15:39:43 +02001618 goto err_free_ida;
Linus Walleijff2b1352015-10-20 11:10:38 +02001619 }
1620
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001621 if (chip->ngpio == 0) {
1622 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
Linus Walleijd377f562019-07-16 11:11:45 +02001623 ret = -EINVAL;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001624 goto err_free_descs;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001625 }
Linus Walleijdf4878e2016-02-12 14:48:23 +01001626
Laura Abbott30277432018-05-21 10:57:07 -07001627 if (chip->ngpio > FASTPATH_NGPIO)
1628 chip_warn(chip, "line cnt %u is greater than fast path cnt %u\n",
Enrico Weigelt, metux IT consult2ddac5a2020-01-04 20:43:34 +01001629 chip->ngpio, FASTPATH_NGPIO);
Laura Abbott30277432018-05-21 10:57:07 -07001630
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +01001631 gdev->label = kstrdup_const(chip->label ?: "unknown", GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001632 if (!gdev->label) {
Linus Walleijd377f562019-07-16 11:11:45 +02001633 ret = -ENOMEM;
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001634 goto err_free_descs;
Linus Walleijdf4878e2016-02-12 14:48:23 +01001635 }
1636
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001637 gdev->ngpio = chip->ngpio;
Linus Walleij43c54ec2016-02-11 11:37:48 +01001638 gdev->data = data;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001639
David Brownelld2876d02008-02-04 22:28:20 -08001640 spin_lock_irqsave(&gpio_lock, flags);
1641
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001642 /*
1643 * TODO: this allocates a Linux GPIO number base in the global
1644 * GPIO numberspace for this chip. In the long run we want to
1645 * get *rid* of this numberspace and use only descriptors, but
1646 * it may be a pipe dream. It will not happen before we get rid
1647 * of the sysfs interface anyways.
1648 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001649 if (base < 0) {
1650 base = gpiochip_find_base(chip->ngpio);
1651 if (base < 0) {
Linus Walleijd377f562019-07-16 11:11:45 +02001652 ret = base;
Johan Hovold225fce82015-01-12 17:12:25 +01001653 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001654 goto err_free_label;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001655 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001656 /*
1657 * TODO: it should not be necessary to reflect the assigned
1658 * base outside of the GPIO subsystem. Go over drivers and
1659 * see if anyone makes use of this, else drop this and assign
1660 * a poison instead.
1661 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001662 chip->base = base;
1663 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001664 gdev->base = base;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001665
Linus Walleijd377f562019-07-16 11:11:45 +02001666 ret = gpiodev_add_to_list(gdev);
1667 if (ret) {
Johan Hovold05aa5202015-01-12 17:12:26 +01001668 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001669 goto err_free_label;
Johan Hovold05aa5202015-01-12 17:12:26 +01001670 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001671
Ricardo Ribalda Delgado767cd172018-10-12 08:11:36 +02001672 for (i = 0; i < chip->ngpio; i++)
1673 gdev->descs[i].gdev = gdev;
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001674
Dan Callaghan207270d2020-01-21 10:12:17 +10001675 spin_unlock_irqrestore(&gpio_lock, flags);
1676
Bartosz Golaszewski51c10642019-11-22 15:19:21 +01001677 ATOMIC_INIT_NOTIFIER_HEAD(&gdev->notifier);
1678
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301679#ifdef CONFIG_PINCTRL
Linus Walleij20ec3e32016-02-11 11:03:06 +01001680 INIT_LIST_HEAD(&gdev->pin_ranges);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301681#endif
1682
Linus Walleijd377f562019-07-16 11:11:45 +02001683 ret = gpiochip_set_desc_names(chip);
1684 if (ret)
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001685 goto err_remove_from_list;
1686
Linus Walleij151a4102019-09-05 11:40:54 +02001687 ret = gpiochip_alloc_valid_mask(chip);
Linus Walleijd377f562019-07-16 11:11:45 +02001688 if (ret)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001689 goto err_remove_from_list;
1690
Linus Walleijd377f562019-07-16 11:11:45 +02001691 ret = of_gpiochip_add(chip);
1692 if (ret)
Linus Walleij48057ed2019-08-20 10:05:27 +02001693 goto err_free_gpiochip_mask;
Tomeu Vizoso28355f82015-07-14 10:29:54 +02001694
Linus Walleijd377f562019-07-16 11:11:45 +02001695 ret = gpiochip_init_valid_mask(chip);
1696 if (ret)
Geert Uytterhoeven35779892019-04-24 15:59:33 +02001697 goto err_remove_of_chip;
Ricardo Ribalda Delgadof8ec92a2018-10-05 08:52:58 +02001698
Ricardo Ribalda Delgado3edfb7b2018-10-05 08:53:00 +02001699 for (i = 0; i < chip->ngpio; i++) {
1700 struct gpio_desc *desc = &gdev->descs[i];
1701
Chris Packhamd95da992019-07-08 08:35:58 +12001702 if (chip->get_direction && gpiochip_line_is_valid(chip, i)) {
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +02001703 assign_bit(FLAG_IS_OUT,
1704 &desc->flags, !chip->get_direction(chip, i));
Chris Packhamd95da992019-07-08 08:35:58 +12001705 } else {
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +02001706 assign_bit(FLAG_IS_OUT,
1707 &desc->flags, !chip->direction_input);
Chris Packhamd95da992019-07-08 08:35:58 +12001708 }
Ricardo Ribalda Delgado3edfb7b2018-10-05 08:53:00 +02001709 }
1710
Andy Shevchenkob056ca12019-11-04 18:09:39 +02001711 ret = gpiochip_add_pin_ranges(chip);
1712 if (ret)
1713 goto err_remove_of_chip;
1714
Mika Westerberg664e3e52014-01-08 12:40:54 +02001715 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001716
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02001717 machine_gpiochip_add(chip);
1718
Linus Walleij504369c2019-10-30 13:29:14 +01001719 ret = gpiochip_irqchip_init_valid_mask(chip);
Andy Shevchenko9411e3a2019-10-09 17:34:44 +03001720 if (ret)
1721 goto err_remove_acpi_chip;
1722
Linus Walleij504369c2019-10-30 13:29:14 +01001723 ret = gpiochip_irqchip_init_hw(chip);
Linus Walleijfbdf8d42019-09-06 12:05:35 +02001724 if (ret)
Linus Walleij48057ed2019-08-20 10:05:27 +02001725 goto err_remove_acpi_chip;
1726
Linus Walleijfbdf8d42019-09-06 12:05:35 +02001727 ret = gpiochip_add_irqchip(chip, lock_key, request_key);
1728 if (ret)
Linus Walleij48057ed2019-08-20 10:05:27 +02001729 goto err_remove_irqchip_mask;
1730
Linus Walleij3c702e92015-10-21 15:29:53 +02001731 /*
1732 * By first adding the chardev, and then adding the device,
1733 * we get a device node entry in sysfs under
1734 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1735 * coldplug of device nodes and other udev business.
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001736 * We can do this only if gpiolib has been initialized.
1737 * Otherwise, defer until later.
Linus Walleij3c702e92015-10-21 15:29:53 +02001738 */
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001739 if (gpiolib_initialized) {
Linus Walleijd377f562019-07-16 11:11:45 +02001740 ret = gpiochip_setup_dev(gdev);
1741 if (ret)
Linus Walleij48057ed2019-08-20 10:05:27 +02001742 goto err_remove_irqchip;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001743 }
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001744 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001745
Linus Walleij48057ed2019-08-20 10:05:27 +02001746err_remove_irqchip:
1747 gpiochip_irqchip_remove(chip);
1748err_remove_irqchip_mask:
1749 gpiochip_irqchip_free_valid_mask(chip);
Geert Uytterhoeven35779892019-04-24 15:59:33 +02001750err_remove_acpi_chip:
Johan Hovold225fce82015-01-12 17:12:25 +01001751 acpi_gpiochip_remove(chip);
Geert Uytterhoeven35779892019-04-24 15:59:33 +02001752err_remove_of_chip:
Johan Hovold6d867502015-05-04 17:23:25 +02001753 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +01001754 of_gpiochip_remove(chip);
Geert Uytterhoeven35779892019-04-24 15:59:33 +02001755err_free_gpiochip_mask:
Andy Shevchenko2f4133b2019-11-05 20:06:54 +02001756 gpiochip_remove_pin_ranges(chip);
Stephen Boyd726cb3b2018-03-23 09:34:52 -07001757 gpiochip_free_valid_mask(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001758err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +01001759 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001760 list_del(&gdev->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001761 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001762err_free_label:
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +01001763 kfree_const(gdev->label);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001764err_free_descs:
1765 kfree(gdev->descs);
Vladimir Zapolskiya05a1402018-11-02 15:39:43 +02001766err_free_ida:
Linus Walleijff2b1352015-10-20 11:10:38 +02001767 ida_simple_remove(&gpio_ida, gdev->id);
Vladimir Zapolskiya05a1402018-11-02 15:39:43 +02001768err_free_gdev:
David Brownelld2876d02008-02-04 22:28:20 -08001769 /* failures here can mean systems won't boot... */
Marcel Ziswiler1777fc92018-07-20 09:54:49 +02001770 pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__,
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001771 gdev->base, gdev->base + gdev->ngpio - 1,
Linus Walleijd377f562019-07-16 11:11:45 +02001772 chip->label ? : "generic", ret);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001773 kfree(gdev);
Linus Walleijd377f562019-07-16 11:11:45 +02001774 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08001775}
Thierry Reding959bc7b2017-11-07 19:15:59 +01001776EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);
David Brownelld2876d02008-02-04 22:28:20 -08001777
1778/**
Linus Walleij43c54ec2016-02-11 11:37:48 +01001779 * gpiochip_get_data() - get per-subdriver data for the chip
Thierry Reding950d55f52017-07-24 16:57:22 +02001780 * @chip: GPIO chip
1781 *
1782 * Returns:
1783 * The per-subdriver data for the chip.
Linus Walleij43c54ec2016-02-11 11:37:48 +01001784 */
1785void *gpiochip_get_data(struct gpio_chip *chip)
1786{
1787 return chip->gpiodev->data;
1788}
1789EXPORT_SYMBOL_GPL(gpiochip_get_data);
1790
1791/**
David Brownelld2876d02008-02-04 22:28:20 -08001792 * gpiochip_remove() - unregister a gpio_chip
1793 * @chip: the chip to unregister
1794 *
1795 * A gpio_chip with any GPIOs still requested may not be removed.
1796 */
abdoulaye berthee1db1702014-07-05 18:28:50 +02001797void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -08001798{
Linus Walleijff2b1352015-10-20 11:10:38 +02001799 struct gpio_device *gdev = chip->gpiodev;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001800 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001801 unsigned long flags;
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001802 unsigned i;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001803 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -08001804
Linus Walleijff2b1352015-10-20 11:10:38 +02001805 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
Linus Walleijafbc4f32016-02-09 13:21:06 +01001806 gpiochip_sysfs_unregister(gdev);
Geert Uytterhoeven5018ada2016-12-19 18:29:23 +01001807 gpiochip_free_hogs(chip);
Bamvor Jian Zhangbd203bd2016-02-20 13:13:19 +08001808 /* Numb the device, cancelling all outstanding operations */
1809 gdev->chip = NULL;
Johan Hovold00acc3d2015-01-12 17:12:27 +01001810 gpiochip_irqchip_remove(chip);
Mika Westerberg6072b9d2014-03-10 14:54:53 +02001811 acpi_gpiochip_remove(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001812 of_gpiochip_remove(chip);
Andy Shevchenko2f4133b2019-11-05 20:06:54 +02001813 gpiochip_remove_pin_ranges(chip);
Stephen Boyd726cb3b2018-03-23 09:34:52 -07001814 gpiochip_free_valid_mask(chip);
Linus Walleij43c54ec2016-02-11 11:37:48 +01001815 /*
1816 * We accept no more calls into the driver from this point, so
1817 * NULL the driver data pointer
1818 */
1819 gdev->data = NULL;
Anton Vorontsov391c9702010-06-08 07:48:17 -06001820
Johan Hovold6798aca2015-01-12 17:12:28 +01001821 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001822 for (i = 0; i < gdev->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001823 desc = &gdev->descs[i];
Johan Hovoldfab28b82015-05-04 17:10:27 +02001824 if (test_bit(FLAG_REQUESTED, &desc->flags))
1825 requested = true;
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
Johan Hovoldfab28b82015-05-04 17:10:27 +02001829 if (requested)
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 +05301844static void devm_gpio_chip_release(struct device *dev, void *res)
1845{
1846 struct gpio_chip *chip = *(struct gpio_chip **)res;
1847
1848 gpiochip_remove(chip);
1849}
1850
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301851/**
Geert Uytterhoeven51158412019-11-20 15:37:45 +01001852 * devm_gpiochip_add_data() - Resource managed gpiochip_add_data()
Uwe Kleine-König3925b90f2018-10-08 11:34:03 +02001853 * @dev: pointer to the device that gpio_chip belongs to.
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301854 * @chip: the chip to register, with chip->base initialized
Thierry Reding950d55f52017-07-24 16:57:22 +02001855 * @data: driver-private data associated with this chip
1856 *
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301857 * Context: potentially before irqs will work
1858 *
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301859 * The gpio chip automatically be released when the device is unbound.
Thierry Reding950d55f52017-07-24 16:57:22 +02001860 *
1861 * Returns:
1862 * A negative errno if the chip can't be registered, such as because the
1863 * chip->base is invalid or already associated with a different chip.
1864 * Otherwise it returns zero as a success code.
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301865 */
1866int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
1867 void *data)
1868{
1869 struct gpio_chip **ptr;
1870 int ret;
1871
1872 ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
1873 GFP_KERNEL);
1874 if (!ptr)
1875 return -ENOMEM;
1876
1877 ret = gpiochip_add_data(chip, data);
1878 if (ret < 0) {
1879 devres_free(ptr);
1880 return ret;
1881 }
1882
1883 *ptr = chip;
1884 devres_add(dev, ptr);
1885
1886 return 0;
1887}
1888EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
1889
1890/**
Grant Likely594fa262010-06-08 07:48:16 -06001891 * gpiochip_find() - iterator for locating a specific gpio_chip
1892 * @data: data to pass to match function
Thierry Reding950d55f52017-07-24 16:57:22 +02001893 * @match: Callback function to check gpio_chip
Grant Likely594fa262010-06-08 07:48:16 -06001894 *
1895 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1896 * determined by a user supplied @match callback. The callback should return
1897 * 0 if the device doesn't match and non-zero if it does. If the callback is
1898 * non-zero, this function will return to the caller and not iterate over any
1899 * more gpio_chips.
1900 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001901struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001902 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001903 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001904{
Linus Walleijff2b1352015-10-20 11:10:38 +02001905 struct gpio_device *gdev;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001906 struct gpio_chip *chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -06001907 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001908
1909 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001910 list_for_each_entry(gdev, &gpio_devices, list)
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001911 if (gdev->chip && match(gdev->chip, data)) {
1912 chip = gdev->chip;
Grant Likely594fa262010-06-08 07:48:16 -06001913 break;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001914 }
Linus Walleijff2b1352015-10-20 11:10:38 +02001915
Grant Likely594fa262010-06-08 07:48:16 -06001916 spin_unlock_irqrestore(&gpio_lock, flags);
1917
1918 return chip;
1919}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001920EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001921
Alexandre Courbot79697ef2013-11-16 21:39:32 +09001922static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1923{
1924 const char *name = data;
1925
1926 return !strcmp(chip->label, name);
1927}
1928
1929static struct gpio_chip *find_chip_by_name(const char *name)
1930{
1931 return gpiochip_find((void *)name, gpiochip_match_name);
1932}
1933
Linus Walleij14250522014-03-25 10:40:18 +01001934#ifdef CONFIG_GPIOLIB_IRQCHIP
1935
1936/*
1937 * The following is irqchip helper code for gpiochips.
1938 */
1939
Andy Shevchenko9411e3a2019-10-09 17:34:44 +03001940static int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
1941{
1942 struct gpio_irq_chip *girq = &gc->irq;
1943
1944 if (!girq->init_hw)
1945 return 0;
1946
1947 return girq->init_hw(gc);
1948}
1949
Linus Walleij5fbe5b52019-09-04 16:01:04 +02001950static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001951{
Linus Walleij5fbe5b52019-09-04 16:01:04 +02001952 struct gpio_irq_chip *girq = &gc->irq;
1953
1954 if (!girq->init_valid_mask)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001955 return 0;
1956
Linus Walleij5fbe5b52019-09-04 16:01:04 +02001957 girq->valid_mask = gpiochip_allocate_mask(gc);
1958 if (!girq->valid_mask)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001959 return -ENOMEM;
1960
Linus Walleij5fbe5b52019-09-04 16:01:04 +02001961 girq->init_valid_mask(gc, girq->valid_mask, gc->ngpio);
1962
Mika Westerberg79b804c2016-09-20 15:15:21 +03001963 return 0;
1964}
1965
1966static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1967{
Masahiro Yamada7bdbd1ec2019-07-18 15:51:01 +09001968 bitmap_free(gpiochip->irq.valid_mask);
Thierry Redingdc7b0382017-11-07 19:15:52 +01001969 gpiochip->irq.valid_mask = NULL;
Mika Westerberg79b804c2016-09-20 15:15:21 +03001970}
1971
Stephen Boyd64ff2c82018-01-09 17:58:46 -08001972bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
1973 unsigned int offset)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001974{
Stephen Boyd726cb3b2018-03-23 09:34:52 -07001975 if (!gpiochip_line_is_valid(gpiochip, offset))
1976 return false;
Mika Westerberg79b804c2016-09-20 15:15:21 +03001977 /* No mask means all valid */
Thierry Redingdc7b0382017-11-07 19:15:52 +01001978 if (likely(!gpiochip->irq.valid_mask))
Mika Westerberg79b804c2016-09-20 15:15:21 +03001979 return true;
Thierry Redingdc7b0382017-11-07 19:15:52 +01001980 return test_bit(offset, gpiochip->irq.valid_mask);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001981}
Stephen Boyd64ff2c82018-01-09 17:58:46 -08001982EXPORT_SYMBOL_GPL(gpiochip_irqchip_irq_valid);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001983
Linus Walleij14250522014-03-25 10:40:18 +01001984/**
Linus Walleijd245b3f2016-11-24 10:57:25 +01001985 * gpiochip_set_cascaded_irqchip() - connects a cascaded irqchip to a gpiochip
Linus Walleij4892d3a2019-06-14 10:12:26 +02001986 * @gc: the gpiochip to set the irqchip chain to
Linus Walleij14250522014-03-25 10:40:18 +01001987 * @parent_irq: the irq number corresponding to the parent IRQ for this
Linus Walleij72780ce2020-01-13 23:08:00 +01001988 * cascaded irqchip
Linus Walleij14250522014-03-25 10:40:18 +01001989 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001990 * coming out of the gpiochip. If the interrupt is nested rather than
1991 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +01001992 */
Linus Walleij4892d3a2019-06-14 10:12:26 +02001993static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gc,
Thierry Reding6f793092017-04-03 18:05:21 +02001994 unsigned int parent_irq,
Linus Walleijd245b3f2016-11-24 10:57:25 +01001995 irq_flow_handler_t parent_handler)
Linus Walleij14250522014-03-25 10:40:18 +01001996{
Linus Walleij4892d3a2019-06-14 10:12:26 +02001997 struct gpio_irq_chip *girq = &gc->irq;
1998 struct device *dev = &gc->gpiodev->dev;
1999
2000 if (!girq->domain) {
2001 chip_err(gc, "called %s before setting up irqchip\n",
Linus Walleij83141a72014-09-26 13:50:12 +02002002 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +02002003 return;
2004 }
2005
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02002006 if (parent_handler) {
Linus Walleij4892d3a2019-06-14 10:12:26 +02002007 if (gc->can_sleep) {
2008 chip_err(gc,
Andy Shevchenkob1911712018-07-03 03:39:03 +03002009 "you cannot have chained interrupts on a chip that may sleep\n");
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02002010 return;
2011 }
Linus Walleij4892d3a2019-06-14 10:12:26 +02002012 girq->parents = devm_kcalloc(dev, 1,
2013 sizeof(*girq->parents),
2014 GFP_KERNEL);
2015 if (!girq->parents) {
2016 chip_err(gc, "out of memory allocating parent IRQ\n");
2017 return;
2018 }
2019 girq->parents[0] = parent_irq;
2020 girq->num_parents = 1;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02002021 /*
2022 * The parent irqchip is already using the chip_data for this
2023 * irqchip, so our callbacks simply use the handler_data.
2024 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +02002025 irq_set_chained_handler_and_data(parent_irq, parent_handler,
Linus Walleij4892d3a2019-06-14 10:12:26 +02002026 gc);
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02002027 }
Linus Walleij14250522014-03-25 10:40:18 +01002028}
Linus Walleijd245b3f2016-11-24 10:57:25 +01002029
2030/**
Linus Walleijd245b3f2016-11-24 10:57:25 +01002031 * gpiochip_set_nested_irqchip() - connects a nested irqchip to a gpiochip
2032 * @gpiochip: the gpiochip to set the irqchip nested handler to
2033 * @irqchip: the irqchip to nest to the gpiochip
2034 * @parent_irq: the irq number corresponding to the parent IRQ for this
2035 * nested irqchip
2036 */
2037void gpiochip_set_nested_irqchip(struct gpio_chip *gpiochip,
2038 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02002039 unsigned int parent_irq)
Linus Walleijd245b3f2016-11-24 10:57:25 +01002040{
Stephen Boyd3c1f6b22018-10-08 09:32:15 -07002041 gpiochip_set_cascaded_irqchip(gpiochip, parent_irq, NULL);
Linus Walleijd245b3f2016-11-24 10:57:25 +01002042}
2043EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip);
2044
Linus Walleijfdd61a02019-08-08 14:32:37 +02002045#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
2046
2047/**
2048 * gpiochip_set_hierarchical_irqchip() - connects a hierarchical irqchip
2049 * to a gpiochip
2050 * @gc: the gpiochip to set the irqchip hierarchical handler to
2051 * @irqchip: the irqchip to handle this level of the hierarchy, the interrupt
2052 * will then percolate up to the parent
2053 */
2054static void gpiochip_set_hierarchical_irqchip(struct gpio_chip *gc,
2055 struct irq_chip *irqchip)
2056{
2057 /* DT will deal with mapping each IRQ as we go along */
2058 if (is_of_node(gc->irq.fwnode))
2059 return;
2060
2061 /*
2062 * This is for legacy and boardfile "irqchip" fwnodes: allocate
2063 * irqs upfront instead of dynamically since we don't have the
2064 * dynamic type of allocation that hardware description languages
2065 * provide. Once all GPIO drivers using board files are gone from
2066 * the kernel we can delete this code, but for a transitional period
2067 * it is necessary to keep this around.
2068 */
2069 if (is_fwnode_irqchip(gc->irq.fwnode)) {
2070 int i;
2071 int ret;
2072
2073 for (i = 0; i < gc->ngpio; i++) {
2074 struct irq_fwspec fwspec;
2075 unsigned int parent_hwirq;
2076 unsigned int parent_type;
2077 struct gpio_irq_chip *girq = &gc->irq;
2078
2079 /*
2080 * We call the child to parent translation function
2081 * only to check if the child IRQ is valid or not.
2082 * Just pick the rising edge type here as that is what
2083 * we likely need to support.
2084 */
2085 ret = girq->child_to_parent_hwirq(gc, i,
2086 IRQ_TYPE_EDGE_RISING,
2087 &parent_hwirq,
2088 &parent_type);
2089 if (ret) {
2090 chip_err(gc, "skip set-up on hwirq %d\n",
2091 i);
2092 continue;
2093 }
2094
2095 fwspec.fwnode = gc->irq.fwnode;
2096 /* This is the hwirq for the GPIO line side of things */
2097 fwspec.param[0] = girq->child_offset_to_irq(gc, i);
2098 /* Just pick something */
2099 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
2100 fwspec.param_count = 2;
2101 ret = __irq_domain_alloc_irqs(gc->irq.domain,
2102 /* just pick something */
2103 -1,
2104 1,
2105 NUMA_NO_NODE,
2106 &fwspec,
2107 false,
2108 NULL);
2109 if (ret < 0) {
2110 chip_err(gc,
2111 "can not allocate irq for GPIO line %d parent hwirq %d in hierarchy domain: %d\n",
2112 i, parent_hwirq,
2113 ret);
2114 }
2115 }
2116 }
2117
2118 chip_err(gc, "%s unknown fwnode type proceed anyway\n", __func__);
2119
2120 return;
2121}
2122
2123static int gpiochip_hierarchy_irq_domain_translate(struct irq_domain *d,
2124 struct irq_fwspec *fwspec,
2125 unsigned long *hwirq,
2126 unsigned int *type)
2127{
2128 /* We support standard DT translation */
2129 if (is_of_node(fwspec->fwnode) && fwspec->param_count == 2) {
2130 return irq_domain_translate_twocell(d, fwspec, hwirq, type);
2131 }
2132
2133 /* This is for board files and others not using DT */
2134 if (is_fwnode_irqchip(fwspec->fwnode)) {
2135 int ret;
2136
2137 ret = irq_domain_translate_twocell(d, fwspec, hwirq, type);
2138 if (ret)
2139 return ret;
2140 WARN_ON(*type == IRQ_TYPE_NONE);
2141 return 0;
2142 }
2143 return -EINVAL;
2144}
2145
2146static int gpiochip_hierarchy_irq_domain_alloc(struct irq_domain *d,
2147 unsigned int irq,
2148 unsigned int nr_irqs,
2149 void *data)
2150{
2151 struct gpio_chip *gc = d->host_data;
2152 irq_hw_number_t hwirq;
2153 unsigned int type = IRQ_TYPE_NONE;
2154 struct irq_fwspec *fwspec = data;
Kevin Hao24258762020-01-14 16:28:19 +08002155 void *parent_arg;
Linus Walleijfdd61a02019-08-08 14:32:37 +02002156 unsigned int parent_hwirq;
2157 unsigned int parent_type;
2158 struct gpio_irq_chip *girq = &gc->irq;
2159 int ret;
2160
2161 /*
2162 * The nr_irqs parameter is always one except for PCI multi-MSI
2163 * so this should not happen.
2164 */
2165 WARN_ON(nr_irqs != 1);
2166
2167 ret = gc->irq.child_irq_domain_ops.translate(d, fwspec, &hwirq, &type);
2168 if (ret)
2169 return ret;
2170
Kevin Hao366950e2020-01-20 17:56:25 +08002171 chip_dbg(gc, "allocate IRQ %d, hwirq %lu\n", irq, hwirq);
Linus Walleijfdd61a02019-08-08 14:32:37 +02002172
2173 ret = girq->child_to_parent_hwirq(gc, hwirq, type,
2174 &parent_hwirq, &parent_type);
2175 if (ret) {
2176 chip_err(gc, "can't look up hwirq %lu\n", hwirq);
2177 return ret;
2178 }
Kevin Hao366950e2020-01-20 17:56:25 +08002179 chip_dbg(gc, "found parent hwirq %u\n", parent_hwirq);
Linus Walleijfdd61a02019-08-08 14:32:37 +02002180
2181 /*
2182 * We set handle_bad_irq because the .set_type() should
2183 * always be invoked and set the right type of handler.
2184 */
2185 irq_domain_set_info(d,
2186 irq,
2187 hwirq,
2188 gc->irq.chip,
2189 gc,
2190 girq->handler,
2191 NULL, NULL);
2192 irq_set_probe(irq);
2193
Linus Walleijfdd61a02019-08-08 14:32:37 +02002194 /* This parent only handles asserted level IRQs */
Kevin Hao24258762020-01-14 16:28:19 +08002195 parent_arg = girq->populate_parent_alloc_arg(gc, parent_hwirq, parent_type);
2196 if (!parent_arg)
2197 return -ENOMEM;
2198
Kevin Hao366950e2020-01-20 17:56:25 +08002199 chip_dbg(gc, "alloc_irqs_parent for %d parent hwirq %d\n",
Linus Walleijfdd61a02019-08-08 14:32:37 +02002200 irq, parent_hwirq);
Stephen Boydc34f6dc2020-01-14 15:11:03 -08002201 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key);
Kevin Hao24258762020-01-14 16:28:19 +08002202 ret = irq_domain_alloc_irqs_parent(d, irq, 1, parent_arg);
Kevin Hao880b7cf2020-01-14 16:28:20 +08002203 /*
2204 * If the parent irqdomain is msi, the interrupts have already
2205 * been allocated, so the EEXIST is good.
2206 */
2207 if (irq_domain_is_msi(d->parent) && (ret == -EEXIST))
2208 ret = 0;
Linus Walleijfdd61a02019-08-08 14:32:37 +02002209 if (ret)
2210 chip_err(gc,
2211 "failed to allocate parent hwirq %d for hwirq %lu\n",
2212 parent_hwirq, hwirq);
2213
Kevin Hao24258762020-01-14 16:28:19 +08002214 kfree(parent_arg);
Linus Walleijfdd61a02019-08-08 14:32:37 +02002215 return ret;
2216}
2217
2218static unsigned int gpiochip_child_offset_to_irq_noop(struct gpio_chip *chip,
2219 unsigned int offset)
2220{
2221 return offset;
2222}
2223
2224static void gpiochip_hierarchy_setup_domain_ops(struct irq_domain_ops *ops)
2225{
2226 ops->activate = gpiochip_irq_domain_activate;
2227 ops->deactivate = gpiochip_irq_domain_deactivate;
2228 ops->alloc = gpiochip_hierarchy_irq_domain_alloc;
2229 ops->free = irq_domain_free_irqs_common;
2230
2231 /*
2232 * We only allow overriding the translate() function for
2233 * hierarchical chips, and this should only be done if the user
2234 * really need something other than 1:1 translation.
2235 */
2236 if (!ops->translate)
2237 ops->translate = gpiochip_hierarchy_irq_domain_translate;
2238}
2239
2240static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
2241{
2242 if (!gc->irq.child_to_parent_hwirq ||
2243 !gc->irq.fwnode) {
2244 chip_err(gc, "missing irqdomain vital data\n");
2245 return -EINVAL;
2246 }
2247
2248 if (!gc->irq.child_offset_to_irq)
2249 gc->irq.child_offset_to_irq = gpiochip_child_offset_to_irq_noop;
2250
Kevin Hao24258762020-01-14 16:28:19 +08002251 if (!gc->irq.populate_parent_alloc_arg)
2252 gc->irq.populate_parent_alloc_arg =
Linus Walleijfdd61a02019-08-08 14:32:37 +02002253 gpiochip_populate_parent_fwspec_twocell;
2254
2255 gpiochip_hierarchy_setup_domain_ops(&gc->irq.child_irq_domain_ops);
2256
2257 gc->irq.domain = irq_domain_create_hierarchy(
2258 gc->irq.parent_domain,
2259 0,
2260 gc->ngpio,
2261 gc->irq.fwnode,
2262 &gc->irq.child_irq_domain_ops,
2263 gc);
2264
2265 if (!gc->irq.domain)
2266 return -ENOMEM;
2267
2268 gpiochip_set_hierarchical_irqchip(gc, gc->irq.chip);
2269
2270 return 0;
2271}
2272
2273static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
2274{
2275 return !!gc->irq.parent_domain;
2276}
2277
Kevin Hao24258762020-01-14 16:28:19 +08002278void *gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *chip,
Linus Walleijfdd61a02019-08-08 14:32:37 +02002279 unsigned int parent_hwirq,
2280 unsigned int parent_type)
2281{
Kevin Hao24258762020-01-14 16:28:19 +08002282 struct irq_fwspec *fwspec;
2283
2284 fwspec = kmalloc(sizeof(*fwspec), GFP_KERNEL);
2285 if (!fwspec)
2286 return NULL;
2287
2288 fwspec->fwnode = chip->irq.parent_domain->fwnode;
Linus Walleijfdd61a02019-08-08 14:32:37 +02002289 fwspec->param_count = 2;
2290 fwspec->param[0] = parent_hwirq;
2291 fwspec->param[1] = parent_type;
Kevin Hao24258762020-01-14 16:28:19 +08002292
2293 return fwspec;
Linus Walleijfdd61a02019-08-08 14:32:37 +02002294}
2295EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_twocell);
2296
Kevin Hao24258762020-01-14 16:28:19 +08002297void *gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *chip,
Linus Walleijfdd61a02019-08-08 14:32:37 +02002298 unsigned int parent_hwirq,
2299 unsigned int parent_type)
2300{
Kevin Hao24258762020-01-14 16:28:19 +08002301 struct irq_fwspec *fwspec;
2302
2303 fwspec = kmalloc(sizeof(*fwspec), GFP_KERNEL);
2304 if (!fwspec)
2305 return NULL;
2306
2307 fwspec->fwnode = chip->irq.parent_domain->fwnode;
Linus Walleijfdd61a02019-08-08 14:32:37 +02002308 fwspec->param_count = 4;
2309 fwspec->param[0] = 0;
2310 fwspec->param[1] = parent_hwirq;
2311 fwspec->param[2] = 0;
2312 fwspec->param[3] = parent_type;
Kevin Hao24258762020-01-14 16:28:19 +08002313
2314 return fwspec;
Linus Walleijfdd61a02019-08-08 14:32:37 +02002315}
2316EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_fourcell);
2317
2318#else
2319
2320static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
2321{
2322 return -EINVAL;
2323}
2324
2325static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
2326{
2327 return false;
2328}
2329
2330#endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
2331
Linus Walleijd245b3f2016-11-24 10:57:25 +01002332/**
Linus Walleij14250522014-03-25 10:40:18 +01002333 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
2334 * @d: the irqdomain used by this irqchip
2335 * @irq: the global irq number used by this GPIO irqchip irq
2336 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
2337 *
2338 * This function will set up the mapping for a certain IRQ line on a
2339 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
2340 * stored inside the gpiochip.
2341 */
Thierry Reding1b95b4e2017-11-07 19:15:55 +01002342int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
2343 irq_hw_number_t hwirq)
Linus Walleij14250522014-03-25 10:40:18 +01002344{
2345 struct gpio_chip *chip = d->host_data;
Linus Walleijd377f562019-07-16 11:11:45 +02002346 int ret = 0;
Linus Walleij14250522014-03-25 10:40:18 +01002347
Grygorii Strashkodc749a02017-07-21 11:49:00 -05002348 if (!gpiochip_irqchip_irq_valid(chip, hwirq))
2349 return -ENXIO;
2350
Linus Walleij14250522014-03-25 10:40:18 +01002351 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03002352 /*
2353 * This lock class tells lockdep that GPIO irqs are in a different
2354 * category than their parents, so it won't report false recursion.
2355 */
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002356 irq_set_lockdep_class(irq, chip->irq.lock_key, chip->irq.request_key);
Thierry Redingc7a0aa52017-11-07 19:15:48 +01002357 irq_set_chip_and_handler(irq, chip->irq.chip, chip->irq.handler);
Linus Walleijd245b3f2016-11-24 10:57:25 +01002358 /* Chips that use nested thread handlers have them marked */
Thierry Reding60ed54c2017-11-07 19:15:57 +01002359 if (chip->irq.threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02002360 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +01002361 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -05002362
Thierry Redinge0d89722017-11-07 19:15:54 +01002363 if (chip->irq.num_parents == 1)
Linus Walleijd377f562019-07-16 11:11:45 +02002364 ret = irq_set_parent(irq, chip->irq.parents[0]);
Thierry Redinge0d89722017-11-07 19:15:54 +01002365 else if (chip->irq.map)
Linus Walleijd377f562019-07-16 11:11:45 +02002366 ret = irq_set_parent(irq, chip->irq.map[hwirq]);
Thierry Redinge0d89722017-11-07 19:15:54 +01002367
Linus Walleijd377f562019-07-16 11:11:45 +02002368 if (ret < 0)
2369 return ret;
Thierry Redinge0d89722017-11-07 19:15:54 +01002370
Linus Walleij1333b902014-04-23 16:45:12 +02002371 /*
2372 * No set-up of the hardware will happen if IRQ_TYPE_NONE
2373 * is passed as default type.
2374 */
Thierry Reding3634eeb2017-11-07 19:15:49 +01002375 if (chip->irq.default_type != IRQ_TYPE_NONE)
2376 irq_set_irq_type(irq, chip->irq.default_type);
Linus Walleij14250522014-03-25 10:40:18 +01002377
2378 return 0;
2379}
Thierry Reding1b95b4e2017-11-07 19:15:55 +01002380EXPORT_SYMBOL_GPL(gpiochip_irq_map);
Linus Walleij14250522014-03-25 10:40:18 +01002381
Thierry Reding1b95b4e2017-11-07 19:15:55 +01002382void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
Linus Walleijc3626fd2014-03-28 20:42:01 +01002383{
Linus Walleij1c8732b2014-04-09 13:34:39 +02002384 struct gpio_chip *chip = d->host_data;
2385
Thierry Reding60ed54c2017-11-07 19:15:57 +01002386 if (chip->irq.threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02002387 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +01002388 irq_set_chip_and_handler(irq, NULL, NULL);
2389 irq_set_chip_data(irq, NULL);
2390}
Thierry Reding1b95b4e2017-11-07 19:15:55 +01002391EXPORT_SYMBOL_GPL(gpiochip_irq_unmap);
Linus Walleijc3626fd2014-03-28 20:42:01 +01002392
Linus Walleij14250522014-03-25 10:40:18 +01002393static const struct irq_domain_ops gpiochip_domain_ops = {
2394 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +01002395 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +01002396 /* Virtually all GPIO irqchips are twocell:ed */
2397 .xlate = irq_domain_xlate_twocell,
2398};
2399
Linus Walleijfdd61a02019-08-08 14:32:37 +02002400/*
2401 * TODO: move these activate/deactivate in under the hierarchicial
2402 * irqchip implementation as static once SPMI and SSBI (all external
2403 * users) are phased over.
2404 */
Brian Masneyef74f702019-01-19 15:42:42 -05002405/**
2406 * gpiochip_irq_domain_activate() - Lock a GPIO to be used as an IRQ
2407 * @domain: The IRQ domain used by this IRQ chip
2408 * @data: Outermost irq_data associated with the IRQ
2409 * @reserve: If set, only reserve an interrupt vector instead of assigning one
2410 *
2411 * This function is a wrapper that calls gpiochip_lock_as_irq() and is to be
2412 * used as the activate function for the &struct irq_domain_ops. The host_data
2413 * for the IRQ domain must be the &struct gpio_chip.
2414 */
2415int gpiochip_irq_domain_activate(struct irq_domain *domain,
2416 struct irq_data *data, bool reserve)
2417{
2418 struct gpio_chip *chip = domain->host_data;
2419
2420 return gpiochip_lock_as_irq(chip, data->hwirq);
2421}
2422EXPORT_SYMBOL_GPL(gpiochip_irq_domain_activate);
2423
2424/**
2425 * gpiochip_irq_domain_deactivate() - Unlock a GPIO used as an IRQ
2426 * @domain: The IRQ domain used by this IRQ chip
2427 * @data: Outermost irq_data associated with the IRQ
2428 *
2429 * This function is a wrapper that will call gpiochip_unlock_as_irq() and is to
2430 * be used as the deactivate function for the &struct irq_domain_ops. The
2431 * host_data for the IRQ domain must be the &struct gpio_chip.
2432 */
2433void gpiochip_irq_domain_deactivate(struct irq_domain *domain,
2434 struct irq_data *data)
2435{
2436 struct gpio_chip *chip = domain->host_data;
2437
2438 return gpiochip_unlock_as_irq(chip, data->hwirq);
2439}
2440EXPORT_SYMBOL_GPL(gpiochip_irq_domain_deactivate);
2441
Linus Walleij14250522014-03-25 10:40:18 +01002442static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
2443{
Linus Walleijfdd61a02019-08-08 14:32:37 +02002444 struct irq_domain *domain = chip->irq.domain;
2445
Grygorii Strashkodc749a02017-07-21 11:49:00 -05002446 if (!gpiochip_irqchip_irq_valid(chip, offset))
2447 return -ENXIO;
Thierry Redinge0d89722017-11-07 19:15:54 +01002448
Linus Walleijfdd61a02019-08-08 14:32:37 +02002449#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
2450 if (irq_domain_is_hierarchy(domain)) {
2451 struct irq_fwspec spec;
2452
2453 spec.fwnode = domain->fwnode;
2454 spec.param_count = 2;
2455 spec.param[0] = chip->irq.child_offset_to_irq(chip, offset);
2456 spec.param[1] = IRQ_TYPE_NONE;
2457
2458 return irq_create_fwspec_mapping(&spec);
2459 }
2460#endif
2461
2462 return irq_create_mapping(domain, offset);
Linus Walleij14250522014-03-25 10:40:18 +01002463}
2464
Hans Verkuil4e6b8232018-09-08 11:23:14 +02002465static int gpiochip_irq_reqres(struct irq_data *d)
2466{
2467 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
2468
2469 return gpiochip_reqres_irq(chip, d->hwirq);
2470}
2471
2472static void gpiochip_irq_relres(struct irq_data *d)
2473{
2474 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
2475
2476 gpiochip_relres_irq(chip, d->hwirq);
2477}
2478
Hans Verkuil461c1a72018-09-08 11:23:17 +02002479static void gpiochip_irq_enable(struct irq_data *d)
2480{
2481 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
2482
2483 gpiochip_enable_irq(chip, d->hwirq);
2484 if (chip->irq.irq_enable)
2485 chip->irq.irq_enable(d);
2486 else
2487 chip->irq.chip->irq_unmask(d);
2488}
2489
2490static void gpiochip_irq_disable(struct irq_data *d)
2491{
2492 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
2493
2494 if (chip->irq.irq_disable)
2495 chip->irq.irq_disable(d);
2496 else
2497 chip->irq.chip->irq_mask(d);
2498 gpiochip_disable_irq(chip, d->hwirq);
2499}
2500
Hans Verkuilca620f22018-09-08 11:23:15 +02002501static void gpiochip_set_irq_hooks(struct gpio_chip *gpiochip)
2502{
2503 struct irq_chip *irqchip = gpiochip->irq.chip;
2504
2505 if (!irqchip->irq_request_resources &&
2506 !irqchip->irq_release_resources) {
2507 irqchip->irq_request_resources = gpiochip_irq_reqres;
2508 irqchip->irq_release_resources = gpiochip_irq_relres;
2509 }
Hans Verkuil461c1a72018-09-08 11:23:17 +02002510 if (WARN_ON(gpiochip->irq.irq_enable))
2511 return;
Hans Verkuil171948e2018-09-14 10:36:39 +02002512 /* Check if the irqchip already has this hook... */
2513 if (irqchip->irq_enable == gpiochip_irq_enable) {
2514 /*
2515 * ...and if so, give a gentle warning that this is bad
2516 * practice.
2517 */
2518 chip_info(gpiochip,
2519 "detected irqchip that is shared with multiple gpiochips: please fix the driver.\n");
2520 return;
2521 }
Hans Verkuil461c1a72018-09-08 11:23:17 +02002522 gpiochip->irq.irq_enable = irqchip->irq_enable;
2523 gpiochip->irq.irq_disable = irqchip->irq_disable;
2524 irqchip->irq_enable = gpiochip_irq_enable;
2525 irqchip->irq_disable = gpiochip_irq_disable;
Hans Verkuilca620f22018-09-08 11:23:15 +02002526}
2527
Linus Walleij14250522014-03-25 10:40:18 +01002528/**
Thierry Redinge0d89722017-11-07 19:15:54 +01002529 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
2530 * @gpiochip: the GPIO chip to add the IRQ chip to
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002531 * @lock_key: lockdep class for IRQ lock
2532 * @request_key: lockdep class for IRQ request
Thierry Redinge0d89722017-11-07 19:15:54 +01002533 */
Thierry Reding959bc7b2017-11-07 19:15:59 +01002534static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002535 struct lock_class_key *lock_key,
2536 struct lock_class_key *request_key)
Thierry Redinge0d89722017-11-07 19:15:54 +01002537{
2538 struct irq_chip *irqchip = gpiochip->irq.chip;
Linus Walleijfdd61a02019-08-08 14:32:37 +02002539 const struct irq_domain_ops *ops = NULL;
Thierry Redinge0d89722017-11-07 19:15:54 +01002540 struct device_node *np;
2541 unsigned int type;
2542 unsigned int i;
2543
2544 if (!irqchip)
2545 return 0;
2546
2547 if (gpiochip->irq.parent_handler && gpiochip->can_sleep) {
Andy Shevchenkob1911712018-07-03 03:39:03 +03002548 chip_err(gpiochip, "you cannot have chained interrupts on a chip that may sleep\n");
Thierry Redinge0d89722017-11-07 19:15:54 +01002549 return -EINVAL;
2550 }
2551
2552 np = gpiochip->gpiodev->dev.of_node;
2553 type = gpiochip->irq.default_type;
2554
2555 /*
2556 * Specifying a default trigger is a terrible idea if DT or ACPI is
2557 * used to configure the interrupts, as you may end up with
2558 * conflicting triggers. Tell the user, and reset to NONE.
2559 */
2560 if (WARN(np && type != IRQ_TYPE_NONE,
2561 "%s: Ignoring %u default trigger\n", np->full_name, type))
2562 type = IRQ_TYPE_NONE;
2563
2564 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
2565 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
2566 "Ignoring %u default trigger\n", type);
2567 type = IRQ_TYPE_NONE;
2568 }
2569
2570 gpiochip->to_irq = gpiochip_to_irq;
2571 gpiochip->irq.default_type = type;
Thierry Reding959bc7b2017-11-07 19:15:59 +01002572 gpiochip->irq.lock_key = lock_key;
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002573 gpiochip->irq.request_key = request_key;
Thierry Redinge0d89722017-11-07 19:15:54 +01002574
Linus Walleijfdd61a02019-08-08 14:32:37 +02002575 /* If a parent irqdomain is provided, let's build a hierarchy */
2576 if (gpiochip_hierarchy_is_hierarchical(gpiochip)) {
2577 int ret = gpiochip_hierarchy_add_domain(gpiochip);
2578 if (ret)
2579 return ret;
2580 } else {
2581 /* Some drivers provide custom irqdomain ops */
2582 if (gpiochip->irq.domain_ops)
2583 ops = gpiochip->irq.domain_ops;
Thierry Redinge0d89722017-11-07 19:15:54 +01002584
Linus Walleijfdd61a02019-08-08 14:32:37 +02002585 if (!ops)
2586 ops = &gpiochip_domain_ops;
2587 gpiochip->irq.domain = irq_domain_add_simple(np,
2588 gpiochip->ngpio,
2589 gpiochip->irq.first,
2590 ops, gpiochip);
2591 if (!gpiochip->irq.domain)
2592 return -EINVAL;
2593 }
Thierry Redinge0d89722017-11-07 19:15:54 +01002594
Thierry Redinge0d89722017-11-07 19:15:54 +01002595 if (gpiochip->irq.parent_handler) {
2596 void *data = gpiochip->irq.parent_handler_data ?: gpiochip;
2597
2598 for (i = 0; i < gpiochip->irq.num_parents; i++) {
2599 /*
2600 * The parent IRQ chip is already using the chip_data
2601 * for this IRQ chip, so our callbacks simply use the
2602 * handler_data.
2603 */
2604 irq_set_chained_handler_and_data(gpiochip->irq.parents[i],
2605 gpiochip->irq.parent_handler,
2606 data);
2607 }
Thierry Redinge0d89722017-11-07 19:15:54 +01002608 }
2609
Hans Verkuilca620f22018-09-08 11:23:15 +02002610 gpiochip_set_irq_hooks(gpiochip);
2611
Thierry Redinge0d89722017-11-07 19:15:54 +01002612 acpi_gpiochip_request_interrupts(gpiochip);
2613
2614 return 0;
2615}
2616
2617/**
Linus Walleij14250522014-03-25 10:40:18 +01002618 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
2619 * @gpiochip: the gpiochip to remove the irqchip from
2620 *
2621 * This is called only from gpiochip_remove()
2622 */
2623static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
2624{
Hans Verkuilca620f22018-09-08 11:23:15 +02002625 struct irq_chip *irqchip = gpiochip->irq.chip;
Thierry Reding39e5f092017-11-07 19:15:50 +01002626 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +01002627
Mika Westerbergafa82fa2014-07-25 09:54:48 +03002628 acpi_gpiochip_free_interrupts(gpiochip);
2629
Hans Verkuilca620f22018-09-08 11:23:15 +02002630 if (irqchip && gpiochip->irq.parent_handler) {
Thierry Reding39e5f092017-11-07 19:15:50 +01002631 struct gpio_irq_chip *irq = &gpiochip->irq;
2632 unsigned int i;
2633
2634 for (i = 0; i < irq->num_parents; i++)
2635 irq_set_chained_handler_and_data(irq->parents[i],
2636 NULL, NULL);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03002637 }
2638
Linus Walleijc3626fd2014-03-28 20:42:01 +01002639 /* Remove all IRQ mappings and delete the domain */
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01002640 if (gpiochip->irq.domain) {
Thierry Reding39e5f092017-11-07 19:15:50 +01002641 unsigned int irq;
2642
Mika Westerberg79b804c2016-09-20 15:15:21 +03002643 for (offset = 0; offset < gpiochip->ngpio; offset++) {
2644 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
2645 continue;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01002646
2647 irq = irq_find_mapping(gpiochip->irq.domain, offset);
2648 irq_dispose_mapping(irq);
Mika Westerberg79b804c2016-09-20 15:15:21 +03002649 }
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01002650
2651 irq_domain_remove(gpiochip->irq.domain);
Linus Walleijc3626fd2014-03-28 20:42:01 +01002652 }
Linus Walleij14250522014-03-25 10:40:18 +01002653
Hans Verkuil461c1a72018-09-08 11:23:17 +02002654 if (irqchip) {
2655 if (irqchip->irq_request_resources == gpiochip_irq_reqres) {
2656 irqchip->irq_request_resources = NULL;
2657 irqchip->irq_release_resources = NULL;
2658 }
2659 if (irqchip->irq_enable == gpiochip_irq_enable) {
2660 irqchip->irq_enable = gpiochip->irq.irq_enable;
2661 irqchip->irq_disable = gpiochip->irq.irq_disable;
2662 }
Linus Walleij14250522014-03-25 10:40:18 +01002663 }
Hans Verkuil461c1a72018-09-08 11:23:17 +02002664 gpiochip->irq.irq_enable = NULL;
2665 gpiochip->irq.irq_disable = NULL;
Hans Verkuilca620f22018-09-08 11:23:15 +02002666 gpiochip->irq.chip = NULL;
Mika Westerberg79b804c2016-09-20 15:15:21 +03002667
2668 gpiochip_irqchip_free_valid_mask(gpiochip);
Linus Walleij14250522014-03-25 10:40:18 +01002669}
2670
2671/**
Linus Walleij739e6f52017-01-11 13:37:07 +01002672 * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01002673 * @gpiochip: the gpiochip to add the irqchip to
2674 * @irqchip: the irqchip to add to the gpiochip
2675 * @first_irq: if not dynamically assigned, the base (first) IRQ to
2676 * allocate gpiochip irqs from
2677 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +02002678 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
2679 * to have the core avoid setting up any default type in the hardware.
Thierry Reding60ed54c2017-11-07 19:15:57 +01002680 * @threaded: whether this irqchip uses a nested thread handler
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002681 * @lock_key: lockdep class for IRQ lock
2682 * @request_key: lockdep class for IRQ request
Linus Walleij14250522014-03-25 10:40:18 +01002683 *
2684 * This function closely associates a certain irqchip with a certain
2685 * gpiochip, providing an irq domain to translate the local IRQs to
2686 * global irqs in the gpiolib core, and making sure that the gpiochip
2687 * is passed as chip data to all related functions. Driver callbacks
Linus Walleij09dd5f92015-12-07 15:31:58 +01002688 * need to use gpiochip_get_data() to get their local state containers back
Linus Walleij14250522014-03-25 10:40:18 +01002689 * from the gpiochip passed as chip data. An irqdomain will be stored
2690 * in the gpiochip that shall be used by the driver to handle IRQ number
2691 * translation. The gpiochip will need to be initialized and registered
2692 * before calling this function.
2693 *
Linus Walleijc3626fd2014-03-28 20:42:01 +01002694 * This function will handle two cell:ed simple IRQs and assumes all
2695 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +01002696 * need to be open coded.
2697 */
Linus Walleij739e6f52017-01-11 13:37:07 +01002698int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
2699 struct irq_chip *irqchip,
2700 unsigned int first_irq,
2701 irq_flow_handler_t handler,
2702 unsigned int type,
Thierry Reding60ed54c2017-11-07 19:15:57 +01002703 bool threaded,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002704 struct lock_class_key *lock_key,
2705 struct lock_class_key *request_key)
Linus Walleij14250522014-03-25 10:40:18 +01002706{
2707 struct device_node *of_node;
Linus Walleij14250522014-03-25 10:40:18 +01002708
2709 if (!gpiochip || !irqchip)
2710 return -EINVAL;
2711
Linus Walleij58383c782015-11-04 09:56:26 +01002712 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +01002713 pr_err("missing gpiochip .dev parent pointer\n");
2714 return -EINVAL;
2715 }
Thierry Reding60ed54c2017-11-07 19:15:57 +01002716 gpiochip->irq.threaded = threaded;
Linus Walleij58383c782015-11-04 09:56:26 +01002717 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +01002718#ifdef CONFIG_OF_GPIO
2719 /*
Colin Cronin20a8a962015-05-18 11:41:43 -07002720 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08002721 * FIXME: get rid of this and use gpiochip->parent->of_node
2722 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +01002723 */
2724 if (gpiochip->of_node)
2725 of_node = gpiochip->of_node;
2726#endif
Marc Zyngier332e99d2016-09-07 09:12:11 +01002727 /*
Mika Westerberg0a1e0052016-09-12 14:29:51 +03002728 * Specifying a default trigger is a terrible idea if DT or ACPI is
Marc Zyngier332e99d2016-09-07 09:12:11 +01002729 * used to configure the interrupts, as you may end-up with
2730 * conflicting triggers. Tell the user, and reset to NONE.
2731 */
2732 if (WARN(of_node && type != IRQ_TYPE_NONE,
Rob Herring7eb6ce22017-07-18 16:43:03 -05002733 "%pOF: Ignoring %d default trigger\n", of_node, type))
Marc Zyngier332e99d2016-09-07 09:12:11 +01002734 type = IRQ_TYPE_NONE;
Mika Westerberg0a1e0052016-09-12 14:29:51 +03002735 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
2736 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
2737 "Ignoring %d default trigger\n", type);
2738 type = IRQ_TYPE_NONE;
2739 }
Marc Zyngier332e99d2016-09-07 09:12:11 +01002740
Thierry Redingda80ff82017-11-07 19:15:46 +01002741 gpiochip->irq.chip = irqchip;
Thierry Redingc7a0aa52017-11-07 19:15:48 +01002742 gpiochip->irq.handler = handler;
Thierry Reding3634eeb2017-11-07 19:15:49 +01002743 gpiochip->irq.default_type = type;
Linus Walleij14250522014-03-25 10:40:18 +01002744 gpiochip->to_irq = gpiochip_to_irq;
Thierry Redingca9df052017-11-07 19:15:53 +01002745 gpiochip->irq.lock_key = lock_key;
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002746 gpiochip->irq.request_key = request_key;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01002747 gpiochip->irq.domain = irq_domain_add_simple(of_node,
Linus Walleij14250522014-03-25 10:40:18 +01002748 gpiochip->ngpio, first_irq,
2749 &gpiochip_domain_ops, gpiochip);
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01002750 if (!gpiochip->irq.domain) {
Thierry Redingda80ff82017-11-07 19:15:46 +01002751 gpiochip->irq.chip = NULL;
Linus Walleij14250522014-03-25 10:40:18 +01002752 return -EINVAL;
2753 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +02002754
Hans Verkuilca620f22018-09-08 11:23:15 +02002755 gpiochip_set_irq_hooks(gpiochip);
Linus Walleij14250522014-03-25 10:40:18 +01002756
Mika Westerbergafa82fa2014-07-25 09:54:48 +03002757 acpi_gpiochip_request_interrupts(gpiochip);
2758
Linus Walleij14250522014-03-25 10:40:18 +01002759 return 0;
2760}
Linus Walleij739e6f52017-01-11 13:37:07 +01002761EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key);
Linus Walleij14250522014-03-25 10:40:18 +01002762
2763#else /* CONFIG_GPIOLIB_IRQCHIP */
2764
Thierry Reding959bc7b2017-11-07 19:15:59 +01002765static inline int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002766 struct lock_class_key *lock_key,
2767 struct lock_class_key *request_key)
Thierry Redinge0d89722017-11-07 19:15:54 +01002768{
2769 return 0;
2770}
Linus Walleij14250522014-03-25 10:40:18 +01002771static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
Andy Shevchenko9411e3a2019-10-09 17:34:44 +03002772
2773static inline int gpiochip_irqchip_init_hw(struct gpio_chip *gpiochip)
2774{
2775 return 0;
2776}
2777
Mika Westerberg79b804c2016-09-20 15:15:21 +03002778static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
2779{
2780 return 0;
2781}
2782static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
2783{ }
Linus Walleij14250522014-03-25 10:40:18 +01002784
2785#endif /* CONFIG_GPIOLIB_IRQCHIP */
2786
Jonas Gorskic771c2f2015-10-11 17:34:15 +02002787/**
2788 * gpiochip_generic_request() - request the gpio function for a pin
2789 * @chip: the gpiochip owning the GPIO
2790 * @offset: the offset of the GPIO to request for GPIO function
2791 */
2792int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
2793{
Linus Walleija9a1d2a2017-09-22 11:02:10 +02002794 return pinctrl_gpio_request(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02002795}
2796EXPORT_SYMBOL_GPL(gpiochip_generic_request);
2797
2798/**
2799 * gpiochip_generic_free() - free the gpio function from a pin
2800 * @chip: the gpiochip to request the gpio function for
2801 * @offset: the offset of the GPIO to free from GPIO function
2802 */
2803void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
2804{
Linus Walleija9a1d2a2017-09-22 11:02:10 +02002805 pinctrl_gpio_free(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02002806}
2807EXPORT_SYMBOL_GPL(gpiochip_generic_free);
2808
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002809/**
2810 * gpiochip_generic_config() - apply configuration for a pin
2811 * @chip: the gpiochip owning the GPIO
2812 * @offset: the offset of the GPIO to apply the configuration
2813 * @config: the configuration to be applied
2814 */
2815int gpiochip_generic_config(struct gpio_chip *chip, unsigned offset,
2816 unsigned long config)
2817{
2818 return pinctrl_gpio_set_config(chip->gpiodev->base + offset, config);
2819}
2820EXPORT_SYMBOL_GPL(gpiochip_generic_config);
2821
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302822#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01002823
Linus Walleij3f0f8672012-11-20 12:40:15 +01002824/**
Christian Ruppert586a87e2013-10-15 15:37:54 +02002825 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
2826 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +02002827 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +02002828 * @gpio_offset: the start offset in the current gpio_chip number space
2829 * @pin_group: name of the pin group inside the pin controller
Christian Lamparter973c1712018-05-21 22:57:39 +02002830 *
2831 * Calling this function directly from a DeviceTree-supported
2832 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
2833 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
2834 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
Christian Ruppert586a87e2013-10-15 15:37:54 +02002835 */
2836int gpiochip_add_pingroup_range(struct gpio_chip *chip,
2837 struct pinctrl_dev *pctldev,
2838 unsigned int gpio_offset, const char *pin_group)
2839{
2840 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002841 struct gpio_device *gdev = chip->gpiodev;
Christian Ruppert586a87e2013-10-15 15:37:54 +02002842 int ret;
2843
2844 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
2845 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002846 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +02002847 return -ENOMEM;
2848 }
2849
2850 /* Use local offset as range ID */
2851 pin_range->range.id = gpio_offset;
2852 pin_range->range.gc = chip;
2853 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002854 pin_range->range.base = gdev->base + gpio_offset;
Christian Ruppert586a87e2013-10-15 15:37:54 +02002855 pin_range->pctldev = pctldev;
2856
2857 ret = pinctrl_get_group_pins(pctldev, pin_group,
2858 &pin_range->range.pins,
2859 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +01002860 if (ret < 0) {
2861 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +02002862 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +01002863 }
Christian Ruppert586a87e2013-10-15 15:37:54 +02002864
2865 pinctrl_add_gpio_range(pctldev, &pin_range->range);
2866
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002867 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
2868 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +02002869 pinctrl_dev_get_devname(pctldev), pin_group);
2870
Linus Walleij20ec3e32016-02-11 11:03:06 +01002871 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Christian Ruppert586a87e2013-10-15 15:37:54 +02002872
2873 return 0;
2874}
2875EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
2876
2877/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01002878 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
2879 * @chip: the gpiochip to add the range for
Thierry Reding950d55f52017-07-24 16:57:22 +02002880 * @pinctl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01002881 * @gpio_offset: the start offset in the current gpio_chip number space
2882 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01002883 * @npins: the number of pins from the offset of each pin space (GPIO and
2884 * pin controller) to accumulate in this range
Thierry Reding950d55f52017-07-24 16:57:22 +02002885 *
2886 * Returns:
2887 * 0 on success, or a negative error-code on failure.
Christian Lamparter973c1712018-05-21 22:57:39 +02002888 *
2889 * Calling this function directly from a DeviceTree-supported
2890 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
2891 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
2892 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
Linus Walleij3f0f8672012-11-20 12:40:15 +01002893 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002894int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01002895 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01002896 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302897{
2898 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002899 struct gpio_device *gdev = chip->gpiodev;
Axel Linb4d4b1f2012-11-21 14:33:56 +08002900 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302901
Linus Walleij3f0f8672012-11-20 12:40:15 +01002902 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302903 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002904 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002905 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302906 }
2907
Linus Walleij3f0f8672012-11-20 12:40:15 +01002908 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01002909 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01002910 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302911 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002912 pin_range->range.base = gdev->base + gpio_offset;
Linus Walleij316511c2012-11-21 08:48:09 +01002913 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302914 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01002915 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302916 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01002917 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08002918 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002919 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +01002920 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08002921 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01002922 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002923 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
2924 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +01002925 pinctl_name,
2926 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302927
Linus Walleij20ec3e32016-02-11 11:03:06 +01002928 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002929
2930 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302931}
Linus Walleij165adc92012-11-06 14:49:39 +01002932EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302933
Linus Walleij3f0f8672012-11-20 12:40:15 +01002934/**
2935 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
2936 * @chip: the chip to remove all the mappings for
2937 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302938void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
2939{
2940 struct gpio_pin_range *pin_range, *tmp;
Linus Walleij20ec3e32016-02-11 11:03:06 +01002941 struct gpio_device *gdev = chip->gpiodev;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302942
Linus Walleij20ec3e32016-02-11 11:03:06 +01002943 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302944 list_del(&pin_range->node);
2945 pinctrl_remove_gpio_range(pin_range->pctldev,
2946 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01002947 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302948 }
2949}
Linus Walleij165adc92012-11-06 14:49:39 +01002950EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
2951
2952#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302953
David Brownelld2876d02008-02-04 22:28:20 -08002954/* These "optional" allocation calls help prevent drivers from stomping
2955 * on each other, and help provide better diagnostics in debugfs.
2956 * They're called even less than the "set direction" calls.
2957 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002958static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08002959{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002960 struct gpio_chip *chip = desc->gdev->chip;
Linus Walleijd377f562019-07-16 11:11:45 +02002961 int ret;
David Brownelld2876d02008-02-04 22:28:20 -08002962 unsigned long flags;
Biju Das3789f5a2018-08-07 08:15:18 +01002963 unsigned offset;
David Brownelld2876d02008-02-04 22:28:20 -08002964
Muchun Song18534df2018-11-01 21:12:50 +08002965 if (label) {
2966 label = kstrdup_const(label, GFP_KERNEL);
2967 if (!label)
2968 return -ENOMEM;
2969 }
2970
David Brownelld2876d02008-02-04 22:28:20 -08002971 spin_lock_irqsave(&gpio_lock, flags);
2972
David Brownelld2876d02008-02-04 22:28:20 -08002973 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07002974 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08002975 */
2976
2977 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
2978 desc_set_label(desc, label ? : "?");
Linus Walleijd377f562019-07-16 11:11:45 +02002979 ret = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07002980 } else {
Muchun Song18534df2018-11-01 21:12:50 +08002981 kfree_const(label);
Linus Walleijd377f562019-07-16 11:11:45 +02002982 ret = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -08002983 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07002984 }
2985
2986 if (chip->request) {
2987 /* chip->request may sleep */
2988 spin_unlock_irqrestore(&gpio_lock, flags);
Biju Das3789f5a2018-08-07 08:15:18 +01002989 offset = gpio_chip_hwgpio(desc);
2990 if (gpiochip_line_is_valid(chip, offset))
Linus Walleijd377f562019-07-16 11:11:45 +02002991 ret = chip->request(chip, offset);
Biju Das3789f5a2018-08-07 08:15:18 +01002992 else
Linus Walleijd377f562019-07-16 11:11:45 +02002993 ret = -EINVAL;
David Brownell35e8bb52008-10-15 22:03:16 -07002994 spin_lock_irqsave(&gpio_lock, flags);
2995
Linus Walleijd377f562019-07-16 11:11:45 +02002996 if (ret < 0) {
David Brownell35e8bb52008-10-15 22:03:16 -07002997 desc_set_label(desc, NULL);
Muchun Song18534df2018-11-01 21:12:50 +08002998 kfree_const(label);
David Brownell35e8bb52008-10-15 22:03:16 -07002999 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03003000 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07003001 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07003002 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03003003 if (chip->get_direction) {
3004 /* chip->get_direction may sleep */
3005 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003006 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03003007 spin_lock_irqsave(&gpio_lock, flags);
3008 }
David Brownelld2876d02008-02-04 22:28:20 -08003009done:
Mika Westerberg77c2d792014-03-10 14:54:50 +02003010 spin_unlock_irqrestore(&gpio_lock, flags);
Bartosz Golaszewski51c10642019-11-22 15:19:21 +01003011 atomic_notifier_call_chain(&desc->gdev->notifier,
3012 GPIOLINE_CHANGED_REQUESTED, desc);
Linus Walleijd377f562019-07-16 11:11:45 +02003013 return ret;
Mika Westerberg77c2d792014-03-10 14:54:50 +02003014}
3015
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003016/*
3017 * This descriptor validation needs to be inserted verbatim into each
3018 * function taking a descriptor, so we need to use a preprocessor
Linus Walleij54d77192016-05-30 16:48:39 +02003019 * macro to avoid endless duplication. If the desc is NULL it is an
3020 * optional GPIO and calls should just bail out.
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003021 */
Rasmus Villemoesa746a232017-12-21 01:27:04 +01003022static int validate_desc(const struct gpio_desc *desc, const char *func)
3023{
3024 if (!desc)
3025 return 0;
3026 if (IS_ERR(desc)) {
3027 pr_warn("%s: invalid GPIO (errorpointer)\n", func);
3028 return PTR_ERR(desc);
3029 }
3030 if (!desc->gdev) {
3031 pr_warn("%s: invalid GPIO (no device)\n", func);
3032 return -EINVAL;
3033 }
3034 if (!desc->gdev->chip) {
3035 dev_warn(&desc->gdev->dev,
3036 "%s: backing chip is gone\n", func);
3037 return 0;
3038 }
3039 return 1;
3040}
3041
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003042#define VALIDATE_DESC(desc) do { \
Rasmus Villemoesa746a232017-12-21 01:27:04 +01003043 int __valid = validate_desc(desc, __func__); \
3044 if (__valid <= 0) \
3045 return __valid; \
3046 } while (0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003047
3048#define VALIDATE_DESC_VOID(desc) do { \
Rasmus Villemoesa746a232017-12-21 01:27:04 +01003049 int __valid = validate_desc(desc, __func__); \
3050 if (__valid <= 0) \
Linus Walleij54d77192016-05-30 16:48:39 +02003051 return; \
Rasmus Villemoesa746a232017-12-21 01:27:04 +01003052 } while (0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003053
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09003054int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02003055{
Linus Walleijd377f562019-07-16 11:11:45 +02003056 int ret = -EPROBE_DEFER;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003057 struct gpio_device *gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02003058
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003059 VALIDATE_DESC(desc);
3060 gdev = desc->gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02003061
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003062 if (try_module_get(gdev->owner)) {
Linus Walleijd377f562019-07-16 11:11:45 +02003063 ret = gpiod_request_commit(desc, label);
3064 if (ret < 0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003065 module_put(gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01003066 else
3067 get_device(&gdev->dev);
Mika Westerberg77c2d792014-03-10 14:54:50 +02003068 }
3069
Linus Walleijd377f562019-07-16 11:11:45 +02003070 if (ret)
3071 gpiod_dbg(desc, "%s: status %d\n", __func__, ret);
Mika Westerberg77c2d792014-03-10 14:54:50 +02003072
Linus Walleijd377f562019-07-16 11:11:45 +02003073 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08003074}
Alexandre Courbot372e7222013-02-03 01:29:29 +09003075
Linus Walleijfac9d882017-09-26 20:58:28 +02003076static bool gpiod_free_commit(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08003077{
Mika Westerberg77c2d792014-03-10 14:54:50 +02003078 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08003079 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07003080 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08003081
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07003082 might_sleep();
3083
Alexandre Courbot372e7222013-02-03 01:29:29 +09003084 gpiod_unexport(desc);
David Brownelld8f388d82008-07-25 01:46:07 -07003085
David Brownelld2876d02008-02-04 22:28:20 -08003086 spin_lock_irqsave(&gpio_lock, flags);
3087
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003088 chip = desc->gdev->chip;
David Brownell35e8bb52008-10-15 22:03:16 -07003089 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
3090 if (chip->free) {
3091 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07003092 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003093 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07003094 spin_lock_irqsave(&gpio_lock, flags);
3095 }
Muchun Song18534df2018-11-01 21:12:50 +08003096 kfree_const(desc->label);
David Brownelld2876d02008-02-04 22:28:20 -08003097 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08003098 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07003099 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303100 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303101 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Drew Fustini9225d512019-11-05 10:04:23 +08003102 clear_bit(FLAG_PULL_UP, &desc->flags);
3103 clear_bit(FLAG_PULL_DOWN, &desc->flags);
Kent Gibson2148ad72019-11-05 10:04:25 +08003104 clear_bit(FLAG_BIAS_DISABLE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06003105 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Geert Uytterhoeven63636d92020-02-20 14:01:49 +01003106#ifdef CONFIG_OF_DYNAMIC
3107 desc->hog = NULL;
3108#endif
Mika Westerberg77c2d792014-03-10 14:54:50 +02003109 ret = true;
3110 }
David Brownelld2876d02008-02-04 22:28:20 -08003111
3112 spin_unlock_irqrestore(&gpio_lock, flags);
Bartosz Golaszewski51c10642019-11-22 15:19:21 +01003113 atomic_notifier_call_chain(&desc->gdev->notifier,
3114 GPIOLINE_CHANGED_RELEASED, desc);
3115
Mika Westerberg77c2d792014-03-10 14:54:50 +02003116 return ret;
3117}
3118
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09003119void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02003120{
Linus Walleijfac9d882017-09-26 20:58:28 +02003121 if (desc && desc->gdev && gpiod_free_commit(desc)) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003122 module_put(desc->gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01003123 put_device(&desc->gdev->dev);
3124 } else {
Mika Westerberg77c2d792014-03-10 14:54:50 +02003125 WARN_ON(extra_checks);
Linus Walleij33a68e82016-02-11 10:28:44 +01003126 }
David Brownelld2876d02008-02-04 22:28:20 -08003127}
Alexandre Courbot372e7222013-02-03 01:29:29 +09003128
David Brownelld2876d02008-02-04 22:28:20 -08003129/**
3130 * gpiochip_is_requested - return string iff signal was requested
3131 * @chip: controller managing the signal
3132 * @offset: of signal within controller's 0..(ngpio - 1) range
3133 *
3134 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09003135 * The string returned is the label passed to gpio_request(); if none has been
3136 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08003137 *
3138 * This function is for use by GPIO controller drivers. The label can
3139 * help with diagnostics, and knowing that the signal is used as a GPIO
3140 * can help avoid accidentally multiplexing it to another controller.
3141 */
3142const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
3143{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09003144 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08003145
Dirk Behme48b59532015-08-18 18:02:32 +02003146 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08003147 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09003148
Bartosz Golaszewski1739a2d2020-02-19 10:47:02 +01003149 desc = gpiochip_get_desc(chip, offset);
3150 if (IS_ERR(desc))
3151 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09003152
Alexandre Courbot372e7222013-02-03 01:29:29 +09003153 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08003154 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003155 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08003156}
3157EXPORT_SYMBOL_GPL(gpiochip_is_requested);
3158
Mika Westerberg77c2d792014-03-10 14:54:50 +02003159/**
3160 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
Thierry Reding950d55f52017-07-24 16:57:22 +02003161 * @chip: GPIO chip
3162 * @hwnum: hardware number of the GPIO for which to request the descriptor
Mika Westerberg77c2d792014-03-10 14:54:50 +02003163 * @label: label for the GPIO
Linus Walleij5923ea62019-04-26 14:40:18 +02003164 * @lflags: lookup flags for this GPIO or 0 if default, this can be used to
3165 * specify things like line inversion semantics with the machine flags
3166 * such as GPIO_OUT_LOW
3167 * @dflags: descriptor request flags for this GPIO or 0 if default, this
3168 * can be used to specify consumer semantics such as open drain
Mika Westerberg77c2d792014-03-10 14:54:50 +02003169 *
3170 * Function allows GPIO chip drivers to request and use their own GPIO
3171 * descriptors via gpiolib API. Difference to gpiod_request() is that this
3172 * function will not increase reference count of the GPIO chip module. This
3173 * allows the GPIO chip module to be unloaded as needed (we assume that the
3174 * GPIO chip driver handles freeing the GPIOs it has requested).
Thierry Reding950d55f52017-07-24 16:57:22 +02003175 *
3176 * Returns:
3177 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
3178 * code on failure.
Mika Westerberg77c2d792014-03-10 14:54:50 +02003179 */
Bartosz Golaszewski06863622019-12-24 13:06:59 +01003180struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip,
3181 unsigned int hwnum,
Linus Walleij21abf102018-09-04 13:31:45 +02003182 const char *label,
Linus Walleij5923ea62019-04-26 14:40:18 +02003183 enum gpio_lookup_flags lflags,
3184 enum gpiod_flags dflags)
Mika Westerberg77c2d792014-03-10 14:54:50 +02003185{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07003186 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
Linus Walleijd377f562019-07-16 11:11:45 +02003187 int ret;
Mika Westerberg77c2d792014-03-10 14:54:50 +02003188
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07003189 if (IS_ERR(desc)) {
3190 chip_err(chip, "failed to get GPIO descriptor\n");
3191 return desc;
3192 }
3193
Linus Walleijd377f562019-07-16 11:11:45 +02003194 ret = gpiod_request_commit(desc, label);
3195 if (ret < 0)
3196 return ERR_PTR(ret);
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07003197
Linus Walleijd377f562019-07-16 11:11:45 +02003198 ret = gpiod_configure_flags(desc, label, lflags, dflags);
3199 if (ret) {
Linus Walleij21abf102018-09-04 13:31:45 +02003200 chip_err(chip, "setup of own GPIO %s failed\n", label);
3201 gpiod_free_commit(desc);
Linus Walleijd377f562019-07-16 11:11:45 +02003202 return ERR_PTR(ret);
Linus Walleij21abf102018-09-04 13:31:45 +02003203 }
3204
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07003205 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02003206}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07003207EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02003208
3209/**
3210 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
3211 * @desc: GPIO descriptor to free
3212 *
3213 * Function frees the given GPIO requested previously with
3214 * gpiochip_request_own_desc().
3215 */
3216void gpiochip_free_own_desc(struct gpio_desc *desc)
3217{
3218 if (desc)
Linus Walleijfac9d882017-09-26 20:58:28 +02003219 gpiod_free_commit(desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02003220}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07003221EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08003222
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003223/*
3224 * Drivers MUST set GPIO direction before making get/set calls. In
David Brownelld2876d02008-02-04 22:28:20 -08003225 * some cases this is done in early boot, before IRQs are enabled.
3226 *
3227 * As a rule these aren't called more than once (except for drivers
3228 * using the open-drain emulation idiom) so these are natural places
3229 * to accumulate extra debugging checks. Note that we can't (yet)
3230 * rely on gpio_request() having been called beforehand.
3231 */
3232
YueHaibingd18fddf2020-01-16 22:29:27 +08003233static int gpio_set_config(struct gpio_chip *gc, unsigned int offset,
Thomas Petazzoni71479782019-02-07 17:28:56 +01003234 enum pin_config_param mode)
3235{
Bartosz Golaszewskid90f3682019-12-24 13:06:58 +01003236 if (!gc->set_config)
3237 return -ENOTSUPP;
Thomas Petazzoni71479782019-02-07 17:28:56 +01003238
Bartosz Golaszewskid90f3682019-12-24 13:06:58 +01003239 return gc->set_config(gc, offset, mode);
Thomas Petazzoni71479782019-02-07 17:28:56 +01003240}
3241
Kent Gibson2148ad72019-11-05 10:04:25 +08003242static int gpio_set_bias(struct gpio_chip *chip, struct gpio_desc *desc)
3243{
3244 int bias = 0;
3245 int ret = 0;
3246
3247 if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
3248 bias = PIN_CONFIG_BIAS_DISABLE;
3249 else if (test_bit(FLAG_PULL_UP, &desc->flags))
3250 bias = PIN_CONFIG_BIAS_PULL_UP;
3251 else if (test_bit(FLAG_PULL_DOWN, &desc->flags))
3252 bias = PIN_CONFIG_BIAS_PULL_DOWN;
3253
3254 if (bias) {
3255 ret = gpio_set_config(chip, gpio_chip_hwgpio(desc), bias);
3256 if (ret != -ENOTSUPP)
3257 return ret;
3258 }
3259 return 0;
3260}
3261
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003262/**
3263 * gpiod_direction_input - set the GPIO direction to input
3264 * @desc: GPIO to set to input
3265 *
3266 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
3267 * be called safely on it.
3268 *
3269 * Return 0 in case of success, else an error code.
3270 */
3271int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08003272{
David Brownelld2876d02008-02-04 22:28:20 -08003273 struct gpio_chip *chip;
Linus Walleijd377f562019-07-16 11:11:45 +02003274 int ret = 0;
David Brownelld2876d02008-02-04 22:28:20 -08003275
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003276 VALIDATE_DESC(desc);
3277 chip = desc->gdev->chip;
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09003278
Linus Walleije48d1942018-09-25 09:54:14 +02003279 /*
3280 * It is legal to have no .get() and .direction_input() specified if
3281 * the chip is output-only, but you can't specify .direction_input()
3282 * and not support the .get() operation, that doesn't make sense.
3283 */
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003284 if (!chip->get && chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01003285 gpiod_warn(desc,
Linus Walleije48d1942018-09-25 09:54:14 +02003286 "%s: missing get() but have direction_input()\n",
3287 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02003288 return -EIO;
3289 }
3290
Linus Walleije48d1942018-09-25 09:54:14 +02003291 /*
3292 * If we have a .direction_input() callback, things are simple,
3293 * just call it. Else we are some input-only chip so try to check the
3294 * direction (if .get_direction() is supported) else we silently
3295 * assume we are in input mode after this.
3296 */
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003297 if (chip->direction_input) {
Linus Walleijd377f562019-07-16 11:11:45 +02003298 ret = chip->direction_input(chip, gpio_chip_hwgpio(desc));
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003299 } else if (chip->get_direction &&
3300 (chip->get_direction(chip, gpio_chip_hwgpio(desc)) != 1)) {
3301 gpiod_warn(desc,
Linus Walleije48d1942018-09-25 09:54:14 +02003302 "%s: missing direction_input() operation and line is output\n",
3303 __func__);
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003304 return -EIO;
3305 }
Kent Gibson2148ad72019-11-05 10:04:25 +08003306 if (ret == 0) {
David Brownelld2876d02008-02-04 22:28:20 -08003307 clear_bit(FLAG_IS_OUT, &desc->flags);
Kent Gibson2148ad72019-11-05 10:04:25 +08003308 ret = gpio_set_bias(chip, desc);
3309 }
Thomas Petazzonid4499912019-02-07 17:28:58 +01003310
Linus Walleijd377f562019-07-16 11:11:45 +02003311 trace_gpio_direction(desc_to_gpio(desc), 1, ret);
Alexandre Courbotd82da792014-07-22 16:17:43 +09003312
Linus Walleijd377f562019-07-16 11:11:45 +02003313 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08003314}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003315EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003316
Linus Walleijfac9d882017-09-26 20:58:28 +02003317static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08003318{
Linus Walleijc663e5f2016-03-22 10:51:16 +01003319 struct gpio_chip *gc = desc->gdev->chip;
Linus Walleijad177312016-11-13 23:02:44 +01003320 int val = !!value;
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003321 int ret = 0;
David Brownelld2876d02008-02-04 22:28:20 -08003322
Linus Walleije48d1942018-09-25 09:54:14 +02003323 /*
3324 * It's OK not to specify .direction_output() if the gpiochip is
3325 * output-only, but if there is then not even a .set() operation it
3326 * is pretty tricky to drive the output line.
3327 */
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003328 if (!gc->set && !gc->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01003329 gpiod_warn(desc,
Linus Walleije48d1942018-09-25 09:54:14 +02003330 "%s: missing set() and direction_output() operations\n",
3331 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02003332 return -EIO;
3333 }
3334
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003335 if (gc->direction_output) {
3336 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
3337 } else {
Linus Walleije48d1942018-09-25 09:54:14 +02003338 /* Check that we are in output mode if we can */
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003339 if (gc->get_direction &&
3340 gc->get_direction(gc, gpio_chip_hwgpio(desc))) {
3341 gpiod_warn(desc,
3342 "%s: missing direction_output() operation\n",
3343 __func__);
3344 return -EIO;
3345 }
Linus Walleije48d1942018-09-25 09:54:14 +02003346 /*
3347 * If we can't actively set the direction, we are some
3348 * output-only chip, so just drive the output as desired.
3349 */
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003350 gc->set(gc, gpio_chip_hwgpio(desc), val);
3351 }
3352
Linus Walleijc663e5f2016-03-22 10:51:16 +01003353 if (!ret)
David Brownelld2876d02008-02-04 22:28:20 -08003354 set_bit(FLAG_IS_OUT, &desc->flags);
Linus Walleijad177312016-11-13 23:02:44 +01003355 trace_gpio_value(desc_to_gpio(desc), 0, val);
Linus Walleijc663e5f2016-03-22 10:51:16 +01003356 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
3357 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08003358}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01003359
3360/**
3361 * gpiod_direction_output_raw - set the GPIO direction to output
3362 * @desc: GPIO to set to output
3363 * @value: initial output value of the GPIO
3364 *
3365 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
3366 * be called safely on it. The initial value of the output must be specified
3367 * as raw value on the physical line without regard for the ACTIVE_LOW status.
3368 *
3369 * Return 0 in case of success, else an error code.
3370 */
3371int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
3372{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003373 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003374 return gpiod_direction_output_raw_commit(desc, value);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01003375}
3376EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
3377
3378/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05303379 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01003380 * @desc: GPIO to set to output
3381 * @value: initial output value of the GPIO
3382 *
3383 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
3384 * be called safely on it. The initial value of the output must be specified
3385 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3386 * account.
3387 *
3388 * Return 0 in case of success, else an error code.
3389 */
3390int gpiod_direction_output(struct gpio_desc *desc, int value)
3391{
Vladimir Zapolskiy30322bc2017-12-21 18:37:24 +02003392 struct gpio_chip *gc;
Linus Walleij02e47982017-09-26 21:20:23 +02003393 int ret;
3394
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003395 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01003396 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3397 value = !value;
Linus Walleijad177312016-11-13 23:02:44 +01003398 else
3399 value = !!value;
Linus Walleij02e47982017-09-26 21:20:23 +02003400
Hans Verkuil4e9439d2018-09-08 11:23:16 +02003401 /* GPIOs used for enabled IRQs shall not be set as output */
3402 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags) &&
3403 test_bit(FLAG_IRQ_IS_ENABLED, &desc->flags)) {
Linus Walleij02e47982017-09-26 21:20:23 +02003404 gpiod_err(desc,
3405 "%s: tried to set a GPIO tied to an IRQ as output\n",
3406 __func__);
3407 return -EIO;
3408 }
3409
Vladimir Zapolskiy30322bc2017-12-21 18:37:24 +02003410 gc = desc->gdev->chip;
Linus Walleij02e47982017-09-26 21:20:23 +02003411 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
3412 /* First see if we can enable open drain in hardware */
Thomas Petazzoni71479782019-02-07 17:28:56 +01003413 ret = gpio_set_config(gc, gpio_chip_hwgpio(desc),
3414 PIN_CONFIG_DRIVE_OPEN_DRAIN);
Linus Walleij02e47982017-09-26 21:20:23 +02003415 if (!ret)
3416 goto set_output_value;
3417 /* Emulate open drain by not actively driving the line high */
Bartosz Golaszewskie7352442019-10-01 11:44:53 +02003418 if (value) {
3419 ret = gpiod_direction_input(desc);
3420 goto set_output_flag;
3421 }
Linus Walleij02e47982017-09-26 21:20:23 +02003422 }
3423 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
Thomas Petazzoni71479782019-02-07 17:28:56 +01003424 ret = gpio_set_config(gc, gpio_chip_hwgpio(desc),
3425 PIN_CONFIG_DRIVE_OPEN_SOURCE);
Linus Walleij02e47982017-09-26 21:20:23 +02003426 if (!ret)
3427 goto set_output_value;
3428 /* Emulate open source by not actively driving the line low */
Bartosz Golaszewskie7352442019-10-01 11:44:53 +02003429 if (!value) {
3430 ret = gpiod_direction_input(desc);
3431 goto set_output_flag;
3432 }
Linus Walleij02e47982017-09-26 21:20:23 +02003433 } else {
Thomas Petazzoni71479782019-02-07 17:28:56 +01003434 gpio_set_config(gc, gpio_chip_hwgpio(desc),
3435 PIN_CONFIG_DRIVE_PUSH_PULL);
Linus Walleij02e47982017-09-26 21:20:23 +02003436 }
3437
3438set_output_value:
Kent Gibson2821ae52019-11-05 10:04:26 +08003439 ret = gpio_set_bias(gc, desc);
3440 if (ret)
3441 return ret;
Linus Walleijfac9d882017-09-26 20:58:28 +02003442 return gpiod_direction_output_raw_commit(desc, value);
Bartosz Golaszewskie7352442019-10-01 11:44:53 +02003443
3444set_output_flag:
3445 /*
3446 * When emulating open-source or open-drain functionalities by not
3447 * actively driving the line (setting mode to input) we still need to
3448 * set the IS_OUT flag or otherwise we won't be able to set the line
3449 * value anymore.
3450 */
3451 if (ret == 0)
3452 set_bit(FLAG_IS_OUT, &desc->flags);
3453 return ret;
Philipp Zabelef70bbe2014-01-07 12:34:11 +01003454}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003455EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08003456
Felipe Balbic4b5be92010-05-26 14:42:23 -07003457/**
Thierry Reding950d55f52017-07-24 16:57:22 +02003458 * gpiod_set_debounce - sets @debounce time for a GPIO
3459 * @desc: descriptor of the GPIO for which to set debounce time
3460 * @debounce: debounce time in microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02003461 *
Thierry Reding950d55f52017-07-24 16:57:22 +02003462 * Returns:
3463 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
3464 * debounce time.
Felipe Balbic4b5be92010-05-26 14:42:23 -07003465 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003466int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07003467{
Felipe Balbic4b5be92010-05-26 14:42:23 -07003468 struct gpio_chip *chip;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03003469 unsigned long config;
Felipe Balbic4b5be92010-05-26 14:42:23 -07003470
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003471 VALIDATE_DESC(desc);
3472 chip = desc->gdev->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02003473
Mika Westerberg2956b5d2017-01-23 15:34:34 +03003474 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
YueHaibingd18fddf2020-01-16 22:29:27 +08003475 return gpio_set_config(chip, gpio_chip_hwgpio(desc), config);
Felipe Balbic4b5be92010-05-26 14:42:23 -07003476}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003477EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003478
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003479/**
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303480 * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
3481 * @desc: descriptor of the GPIO for which to configure persistence
3482 * @transitory: True to lose state on suspend or reset, false for persistence
3483 *
3484 * Returns:
3485 * 0 on success, otherwise a negative error code.
3486 */
3487int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
3488{
3489 struct gpio_chip *chip;
3490 unsigned long packed;
3491 int gpio;
3492 int rc;
3493
Vladimir Zapolskiy156dd392017-12-21 18:37:35 +02003494 VALIDATE_DESC(desc);
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303495 /*
3496 * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
3497 * persistence state.
3498 */
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +02003499 assign_bit(FLAG_TRANSITORY, &desc->flags, transitory);
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303500
3501 /* If the driver supports it, set the persistence state now */
3502 chip = desc->gdev->chip;
3503 if (!chip->set_config)
3504 return 0;
3505
3506 packed = pinconf_to_config_packed(PIN_CONFIG_PERSIST_STATE,
3507 !transitory);
3508 gpio = gpio_chip_hwgpio(desc);
YueHaibingd18fddf2020-01-16 22:29:27 +08003509 rc = gpio_set_config(chip, gpio, packed);
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303510 if (rc == -ENOTSUPP) {
3511 dev_dbg(&desc->gdev->dev, "Persistence not supported for GPIO %d\n",
3512 gpio);
3513 return 0;
3514 }
3515
3516 return rc;
3517}
3518EXPORT_SYMBOL_GPL(gpiod_set_transitory);
3519
3520/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003521 * gpiod_is_active_low - test whether a GPIO is active-low or not
3522 * @desc: the gpio descriptor to test
3523 *
3524 * Returns 1 if the GPIO is active-low, 0 otherwise.
3525 */
3526int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003527{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003528 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003529 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003530}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003531EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08003532
Michał Mirosławd3a5bcb2019-12-11 03:40:55 +01003533/**
3534 * gpiod_toggle_active_low - toggle whether a GPIO is active-low or not
3535 * @desc: the gpio descriptor to change
3536 */
3537void gpiod_toggle_active_low(struct gpio_desc *desc)
3538{
3539 VALIDATE_DESC_VOID(desc);
3540 change_bit(FLAG_ACTIVE_LOW, &desc->flags);
3541}
3542EXPORT_SYMBOL_GPL(gpiod_toggle_active_low);
3543
David Brownelld2876d02008-02-04 22:28:20 -08003544/* I/O calls are only valid after configuration completed; the relevant
3545 * "is this a valid GPIO" error checks should already have been done.
3546 *
3547 * "Get" operations are often inlinable as reading a pin value register,
3548 * and masking the relevant bit in that register.
3549 *
3550 * When "set" operations are inlinable, they involve writing that mask to
3551 * one register to set a low value, or a different register to set it high.
3552 * Otherwise locking is needed, so there may be little value to inlining.
3553 *
3554 *------------------------------------------------------------------------
3555 *
3556 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
3557 * have requested the GPIO. That can include implicit requesting by
3558 * a direction setting call. Marking a gpio as requested locks its chip
3559 * in memory, guaranteeing that these table lookups need no more locking
3560 * and that gpiochip_remove() will fail.
3561 *
3562 * REVISIT when debugging, consider adding some instrumentation to ensure
3563 * that the GPIO was actually requested.
3564 */
3565
Linus Walleijfac9d882017-09-26 20:58:28 +02003566static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08003567{
3568 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003569 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003570 int value;
David Brownelld2876d02008-02-04 22:28:20 -08003571
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003572 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003573 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003574 value = chip->get ? chip->get(chip, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01003575 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003576 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06003577 return value;
David Brownelld2876d02008-02-04 22:28:20 -08003578}
Alexandre Courbot372e7222013-02-03 01:29:29 +09003579
Lukas Wunnereec1d562017-10-12 12:40:10 +02003580static int gpio_chip_get_multiple(struct gpio_chip *chip,
3581 unsigned long *mask, unsigned long *bits)
3582{
3583 if (chip->get_multiple) {
3584 return chip->get_multiple(chip, mask, bits);
3585 } else if (chip->get) {
3586 int i, value;
3587
3588 for_each_set_bit(i, mask, chip->ngpio) {
3589 value = chip->get(chip, i);
3590 if (value < 0)
3591 return value;
3592 __assign_bit(i, bits, value);
3593 }
3594 return 0;
3595 }
3596 return -EIO;
3597}
3598
3599int gpiod_get_array_value_complex(bool raw, bool can_sleep,
3600 unsigned int array_size,
3601 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003602 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003603 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02003604{
Linus Walleijd377f562019-07-16 11:11:45 +02003605 int ret, i = 0;
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003606
3607 /*
3608 * Validate array_info against desc_array and its size.
3609 * It should immediately follow desc_array if both
3610 * have been obtained from the same gpiod_get_array() call.
3611 */
3612 if (array_info && array_info->desc == desc_array &&
3613 array_size <= array_info->size &&
3614 (void *)array_info == desc_array + array_info->size) {
3615 if (!can_sleep)
3616 WARN_ON(array_info->chip->can_sleep);
3617
Linus Walleijd377f562019-07-16 11:11:45 +02003618 ret = gpio_chip_get_multiple(array_info->chip,
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003619 array_info->get_mask,
3620 value_bitmap);
Linus Walleijd377f562019-07-16 11:11:45 +02003621 if (ret)
3622 return ret;
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003623
3624 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
3625 bitmap_xor(value_bitmap, value_bitmap,
3626 array_info->invert_mask, array_size);
3627
3628 if (bitmap_full(array_info->get_mask, array_size))
3629 return 0;
3630
3631 i = find_first_zero_bit(array_info->get_mask, array_size);
3632 } else {
3633 array_info = NULL;
3634 }
Lukas Wunnereec1d562017-10-12 12:40:10 +02003635
3636 while (i < array_size) {
3637 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Laura Abbott30277432018-05-21 10:57:07 -07003638 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
3639 unsigned long *mask, *bits;
Lukas Wunnereec1d562017-10-12 12:40:10 +02003640 int first, j, ret;
3641
Laura Abbott30277432018-05-21 10:57:07 -07003642 if (likely(chip->ngpio <= FASTPATH_NGPIO)) {
3643 mask = fastpath;
3644 } else {
3645 mask = kmalloc_array(2 * BITS_TO_LONGS(chip->ngpio),
3646 sizeof(*mask),
3647 can_sleep ? GFP_KERNEL : GFP_ATOMIC);
3648 if (!mask)
3649 return -ENOMEM;
3650 }
3651
3652 bits = mask + BITS_TO_LONGS(chip->ngpio);
3653 bitmap_zero(mask, chip->ngpio);
3654
Lukas Wunnereec1d562017-10-12 12:40:10 +02003655 if (!can_sleep)
3656 WARN_ON(chip->can_sleep);
3657
3658 /* collect all inputs belonging to the same chip */
3659 first = i;
Lukas Wunnereec1d562017-10-12 12:40:10 +02003660 do {
3661 const struct gpio_desc *desc = desc_array[i];
3662 int hwgpio = gpio_chip_hwgpio(desc);
3663
3664 __set_bit(hwgpio, mask);
3665 i++;
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003666
3667 if (array_info)
Janusz Krzysztofik35ae7f92018-09-24 01:53:35 +02003668 i = find_next_zero_bit(array_info->get_mask,
3669 array_size, i);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003670 } while ((i < array_size) &&
3671 (desc_array[i]->gdev->chip == chip));
3672
3673 ret = gpio_chip_get_multiple(chip, mask, bits);
Laura Abbott30277432018-05-21 10:57:07 -07003674 if (ret) {
3675 if (mask != fastpath)
3676 kfree(mask);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003677 return ret;
Laura Abbott30277432018-05-21 10:57:07 -07003678 }
Lukas Wunnereec1d562017-10-12 12:40:10 +02003679
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003680 for (j = first; j < i; ) {
Lukas Wunnereec1d562017-10-12 12:40:10 +02003681 const struct gpio_desc *desc = desc_array[j];
3682 int hwgpio = gpio_chip_hwgpio(desc);
3683 int value = test_bit(hwgpio, bits);
3684
3685 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3686 value = !value;
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003687 __assign_bit(j, value_bitmap, value);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003688 trace_gpio_value(desc_to_gpio(desc), 1, value);
Janusz Krzysztofik799d5eb2018-09-29 14:20:22 +02003689 j++;
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003690
3691 if (array_info)
Janusz Krzysztofik35ae7f92018-09-24 01:53:35 +02003692 j = find_next_zero_bit(array_info->get_mask, i,
3693 j);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003694 }
Laura Abbott30277432018-05-21 10:57:07 -07003695
3696 if (mask != fastpath)
3697 kfree(mask);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003698 }
3699 return 0;
3700}
3701
David Brownelld2876d02008-02-04 22:28:20 -08003702/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003703 * gpiod_get_raw_value() - return a gpio's raw value
3704 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08003705 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003706 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003707 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003708 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003709 * This function can be called from contexts where we cannot sleep, and will
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003710 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003711 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003712int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003713{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003714 VALIDATE_DESC(desc);
Geert Uytterhoeven3285170f2019-07-01 16:27:38 +02003715 /* Should be using gpiod_get_raw_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003716 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02003717 return gpiod_get_raw_value_commit(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003718}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003719EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08003720
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003721/**
3722 * gpiod_get_value() - return a gpio's value
3723 * @desc: gpio whose value will be returned
3724 *
3725 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003726 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003727 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003728 * This function can be called from contexts where we cannot sleep, and will
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003729 * complain if the GPIO chip functions potentially sleep.
3730 */
3731int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08003732{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003733 int value;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003734
3735 VALIDATE_DESC(desc);
Geert Uytterhoeven3285170f2019-07-01 16:27:38 +02003736 /* Should be using gpiod_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003737 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003738
Linus Walleijfac9d882017-09-26 20:58:28 +02003739 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003740 if (value < 0)
3741 return value;
3742
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003743 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3744 value = !value;
3745
3746 return value;
David Brownelld2876d02008-02-04 22:28:20 -08003747}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003748EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08003749
Lukas Wunnereec1d562017-10-12 12:40:10 +02003750/**
3751 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003752 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02003753 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003754 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003755 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02003756 *
3757 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3758 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3759 * else an error code.
3760 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003761 * This function can be called from contexts where we cannot sleep,
Lukas Wunnereec1d562017-10-12 12:40:10 +02003762 * and it will complain if the GPIO chip functions potentially sleep.
3763 */
3764int gpiod_get_raw_array_value(unsigned int array_size,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003765 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003766 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003767 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02003768{
3769 if (!desc_array)
3770 return -EINVAL;
3771 return gpiod_get_array_value_complex(true, false, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003772 desc_array, array_info,
3773 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003774}
3775EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
3776
3777/**
3778 * gpiod_get_array_value() - read values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003779 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02003780 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003781 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003782 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02003783 *
3784 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3785 * into account. Return 0 in case of success, else an error code.
3786 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003787 * This function can be called from contexts where we cannot sleep,
Lukas Wunnereec1d562017-10-12 12:40:10 +02003788 * and it will complain if the GPIO chip functions potentially sleep.
3789 */
3790int gpiod_get_array_value(unsigned int array_size,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003791 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003792 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003793 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02003794{
3795 if (!desc_array)
3796 return -EINVAL;
3797 return gpiod_get_array_value_complex(false, false, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003798 desc_array, array_info,
3799 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003800}
3801EXPORT_SYMBOL_GPL(gpiod_get_array_value);
3802
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303803/*
Linus Walleijfac9d882017-09-26 20:58:28 +02003804 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003805 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07003806 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303807 */
Linus Walleijfac9d882017-09-26 20:58:28 +02003808static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303809{
Linus Walleijd377f562019-07-16 11:11:45 +02003810 int ret = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003811 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003812 int offset = gpio_chip_hwgpio(desc);
3813
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303814 if (value) {
Linus Walleijd377f562019-07-16 11:11:45 +02003815 ret = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303816 } else {
Linus Walleijd377f562019-07-16 11:11:45 +02003817 ret = chip->direction_output(chip, offset, 0);
3818 if (!ret)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003819 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303820 }
Linus Walleijd377f562019-07-16 11:11:45 +02003821 trace_gpio_direction(desc_to_gpio(desc), value, ret);
3822 if (ret < 0)
Mark Brown6424de52013-09-09 10:33:49 +01003823 gpiod_err(desc,
3824 "%s: Error in set_value for open drain err %d\n",
Linus Walleijd377f562019-07-16 11:11:45 +02003825 __func__, ret);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303826}
3827
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303828/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003829 * _gpio_set_open_source_value() - Set the open source gpio's value.
3830 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07003831 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303832 */
Linus Walleijfac9d882017-09-26 20:58:28 +02003833static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303834{
Linus Walleijd377f562019-07-16 11:11:45 +02003835 int ret = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003836 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003837 int offset = gpio_chip_hwgpio(desc);
3838
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303839 if (value) {
Linus Walleijd377f562019-07-16 11:11:45 +02003840 ret = chip->direction_output(chip, offset, 1);
3841 if (!ret)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003842 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303843 } else {
Linus Walleijd377f562019-07-16 11:11:45 +02003844 ret = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303845 }
Linus Walleijd377f562019-07-16 11:11:45 +02003846 trace_gpio_direction(desc_to_gpio(desc), !value, ret);
3847 if (ret < 0)
Mark Brown6424de52013-09-09 10:33:49 +01003848 gpiod_err(desc,
3849 "%s: Error in set_value for open source err %d\n",
Linus Walleijd377f562019-07-16 11:11:45 +02003850 __func__, ret);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303851}
3852
Linus Walleijfac9d882017-09-26 20:58:28 +02003853static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08003854{
3855 struct gpio_chip *chip;
3856
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003857 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003858 trace_gpio_value(desc_to_gpio(desc), 0, value);
Linus Walleij02e47982017-09-26 21:20:23 +02003859 chip->set(chip, gpio_chip_hwgpio(desc), value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003860}
3861
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003862/*
3863 * set multiple outputs on the same chip;
3864 * use the chip's set_multiple function if available;
3865 * otherwise set the outputs sequentially;
3866 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
3867 * defines which outputs are to be changed
3868 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
3869 * defines the values the outputs specified by mask are to be set to
3870 */
3871static void gpio_chip_set_multiple(struct gpio_chip *chip,
3872 unsigned long *mask, unsigned long *bits)
3873{
3874 if (chip->set_multiple) {
3875 chip->set_multiple(chip, mask, bits);
3876 } else {
Andy Shevchenko5e4e6fb2017-01-03 19:01:17 +02003877 unsigned int i;
3878
3879 /* set outputs if the corresponding mask bit is set */
3880 for_each_set_bit(i, mask, chip->ngpio)
3881 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003882 }
3883}
3884
Laura Abbott30277432018-05-21 10:57:07 -07003885int gpiod_set_array_value_complex(bool raw, bool can_sleep,
Geert Uytterhoeven3c940662018-09-27 13:38:10 +02003886 unsigned int array_size,
3887 struct gpio_desc **desc_array,
3888 struct gpio_array *array_info,
3889 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003890{
3891 int i = 0;
3892
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003893 /*
3894 * Validate array_info against desc_array and its size.
3895 * It should immediately follow desc_array if both
3896 * have been obtained from the same gpiod_get_array() call.
3897 */
3898 if (array_info && array_info->desc == desc_array &&
3899 array_size <= array_info->size &&
3900 (void *)array_info == desc_array + array_info->size) {
3901 if (!can_sleep)
3902 WARN_ON(array_info->chip->can_sleep);
3903
3904 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
3905 bitmap_xor(value_bitmap, value_bitmap,
3906 array_info->invert_mask, array_size);
3907
3908 gpio_chip_set_multiple(array_info->chip, array_info->set_mask,
3909 value_bitmap);
3910
3911 if (bitmap_full(array_info->set_mask, array_size))
3912 return 0;
3913
3914 i = find_first_zero_bit(array_info->set_mask, array_size);
3915 } else {
3916 array_info = NULL;
3917 }
3918
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003919 while (i < array_size) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003920 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Laura Abbott30277432018-05-21 10:57:07 -07003921 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
3922 unsigned long *mask, *bits;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003923 int count = 0;
3924
Laura Abbott30277432018-05-21 10:57:07 -07003925 if (likely(chip->ngpio <= FASTPATH_NGPIO)) {
3926 mask = fastpath;
3927 } else {
3928 mask = kmalloc_array(2 * BITS_TO_LONGS(chip->ngpio),
3929 sizeof(*mask),
3930 can_sleep ? GFP_KERNEL : GFP_ATOMIC);
3931 if (!mask)
3932 return -ENOMEM;
3933 }
3934
3935 bits = mask + BITS_TO_LONGS(chip->ngpio);
3936 bitmap_zero(mask, chip->ngpio);
3937
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003938 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003939 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003940
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003941 do {
3942 struct gpio_desc *desc = desc_array[i];
3943 int hwgpio = gpio_chip_hwgpio(desc);
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003944 int value = test_bit(i, value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003945
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003946 /*
3947 * Pins applicable for fast input but not for
3948 * fast output processing may have been already
3949 * inverted inside the fast path, skip them.
3950 */
3951 if (!raw && !(array_info &&
3952 test_bit(i, array_info->invert_mask)) &&
3953 test_bit(FLAG_ACTIVE_LOW, &desc->flags))
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003954 value = !value;
3955 trace_gpio_value(desc_to_gpio(desc), 0, value);
3956 /*
3957 * collect all normal outputs belonging to the same chip
3958 * open drain and open source outputs are set individually
3959 */
Linus Walleij02e47982017-09-26 21:20:23 +02003960 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02003961 gpio_set_open_drain_value_commit(desc, value);
Linus Walleij02e47982017-09-26 21:20:23 +02003962 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02003963 gpio_set_open_source_value_commit(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003964 } else {
3965 __set_bit(hwgpio, mask);
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +02003966 __assign_bit(hwgpio, bits, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003967 count++;
3968 }
3969 i++;
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003970
3971 if (array_info)
Janusz Krzysztofik35ae7f92018-09-24 01:53:35 +02003972 i = find_next_zero_bit(array_info->set_mask,
3973 array_size, i);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003974 } while ((i < array_size) &&
3975 (desc_array[i]->gdev->chip == chip));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003976 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003977 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003978 gpio_chip_set_multiple(chip, mask, bits);
Laura Abbott30277432018-05-21 10:57:07 -07003979
3980 if (mask != fastpath)
3981 kfree(mask);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003982 }
Laura Abbott30277432018-05-21 10:57:07 -07003983 return 0;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003984}
3985
David Brownelld2876d02008-02-04 22:28:20 -08003986/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003987 * gpiod_set_raw_value() - assign a gpio's raw value
3988 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08003989 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08003990 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003991 * Set the raw value of the GPIO, i.e. the value of its physical line without
3992 * regard for its ACTIVE_LOW status.
3993 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003994 * This function can be called from contexts where we cannot sleep, and will
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003995 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003996 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003997void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003998{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003999 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven3285170f2019-07-01 16:27:38 +02004000 /* Should be using gpiod_set_raw_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004001 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02004002 gpiod_set_raw_value_commit(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08004003}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004004EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08004005
4006/**
Geert Uytterhoeven1e77fc82018-01-09 19:08:21 +01004007 * gpiod_set_value_nocheck() - set a GPIO line value without checking
4008 * @desc: the descriptor to set the value on
4009 * @value: value to set
4010 *
4011 * This sets the value of a GPIO line backing a descriptor, applying
4012 * different semantic quirks like active low and open drain/source
4013 * handling.
4014 */
4015static void gpiod_set_value_nocheck(struct gpio_desc *desc, int value)
4016{
4017 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
4018 value = !value;
4019 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
4020 gpio_set_open_drain_value_commit(desc, value);
4021 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
4022 gpio_set_open_source_value_commit(desc, value);
4023 else
4024 gpiod_set_raw_value_commit(desc, value);
4025}
4026
4027/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004028 * gpiod_set_value() - assign a gpio's value
4029 * @desc: gpio whose value will be assigned
4030 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08004031 *
Linus Walleij02e47982017-09-26 21:20:23 +02004032 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
4033 * OPEN_DRAIN and OPEN_SOURCE flags into account.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004034 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02004035 * This function can be called from contexts where we cannot sleep, and will
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004036 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08004037 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004038void gpiod_set_value(struct gpio_desc *desc, int value)
4039{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004040 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven3285170f2019-07-01 16:27:38 +02004041 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004042 WARN_ON(desc->gdev->chip->can_sleep);
Geert Uytterhoeven1e77fc82018-01-09 19:08:21 +01004043 gpiod_set_value_nocheck(desc, value);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004044}
4045EXPORT_SYMBOL_GPL(gpiod_set_value);
4046
4047/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02004048 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004049 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004050 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004051 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004052 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004053 *
4054 * Set the raw values of the GPIOs, i.e. the values of the physical lines
4055 * without regard for their ACTIVE_LOW status.
4056 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02004057 * This function can be called from contexts where we cannot sleep, and will
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004058 * complain if the GPIO chip functions potentially sleep.
4059 */
Laura Abbott30277432018-05-21 10:57:07 -07004060int gpiod_set_raw_array_value(unsigned int array_size,
Geert Uytterhoeven3c940662018-09-27 13:38:10 +02004061 struct gpio_desc **desc_array,
4062 struct gpio_array *array_info,
4063 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004064{
4065 if (!desc_array)
Laura Abbott30277432018-05-21 10:57:07 -07004066 return -EINVAL;
4067 return gpiod_set_array_value_complex(true, false, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004068 desc_array, array_info, value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004069}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02004070EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004071
4072/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02004073 * gpiod_set_array_value() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004074 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004075 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004076 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004077 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004078 *
4079 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
4080 * into account.
4081 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02004082 * This function can be called from contexts where we cannot sleep, and will
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004083 * complain if the GPIO chip functions potentially sleep.
4084 */
Geert Uytterhoevencf9af0d2018-09-27 13:38:09 +02004085int gpiod_set_array_value(unsigned int array_size,
4086 struct gpio_desc **desc_array,
4087 struct gpio_array *array_info,
4088 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004089{
4090 if (!desc_array)
Geert Uytterhoevencf9af0d2018-09-27 13:38:09 +02004091 return -EINVAL;
4092 return gpiod_set_array_value_complex(false, false, array_size,
4093 desc_array, array_info,
4094 value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004095}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02004096EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004097
4098/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004099 * gpiod_cansleep() - report whether gpio value access may sleep
4100 * @desc: gpio to check
4101 *
4102 */
4103int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09004104{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004105 VALIDATE_DESC(desc);
4106 return desc->gdev->chip->can_sleep;
Alexandre Courbot372e7222013-02-03 01:29:29 +09004107}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004108EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08004109
David Brownell0f6d5042008-10-15 22:03:14 -07004110/**
Linus Walleij90b39402e2018-06-01 13:21:27 +02004111 * gpiod_set_consumer_name() - set the consumer name for the descriptor
4112 * @desc: gpio to set the consumer name on
4113 * @name: the new consumer name
4114 */
Muchun Song18534df2018-11-01 21:12:50 +08004115int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name)
Linus Walleij90b39402e2018-06-01 13:21:27 +02004116{
Muchun Song18534df2018-11-01 21:12:50 +08004117 VALIDATE_DESC(desc);
4118 if (name) {
4119 name = kstrdup_const(name, GFP_KERNEL);
4120 if (!name)
4121 return -ENOMEM;
4122 }
4123
4124 kfree_const(desc->label);
4125 desc_set_label(desc, name);
4126
4127 return 0;
Linus Walleij90b39402e2018-06-01 13:21:27 +02004128}
4129EXPORT_SYMBOL_GPL(gpiod_set_consumer_name);
4130
4131/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004132 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
4133 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07004134 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004135 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
4136 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07004137 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004138int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07004139{
Linus Walleij4c37ce82016-05-02 13:13:10 +02004140 struct gpio_chip *chip;
4141 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07004142
Linus Walleij79bb71b2016-06-15 22:57:38 +02004143 /*
4144 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
4145 * requires this function to not return zero on an invalid descriptor
4146 * but rather a negative error number.
4147 */
Linus Walleijbfbbe442016-06-16 11:55:55 +02004148 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
Linus Walleij79bb71b2016-06-15 22:57:38 +02004149 return -EINVAL;
4150
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004151 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09004152 offset = gpio_chip_hwgpio(desc);
Linus Walleij4c37ce82016-05-02 13:13:10 +02004153 if (chip->to_irq) {
4154 int retirq = chip->to_irq(chip, offset);
4155
4156 /* Zero means NO_IRQ */
4157 if (!retirq)
4158 return -ENXIO;
4159
4160 return retirq;
4161 }
4162 return -ENXIO;
Alexandre Courbot372e7222013-02-03 01:29:29 +09004163}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004164EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09004165
Linus Walleijd468bf92013-09-24 11:54:38 +02004166/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09004167 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09004168 * @chip: the chip the GPIO to lock belongs to
4169 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02004170 *
4171 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08004172 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08004173 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09004174int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08004175{
Linus Walleij9c102802016-05-25 10:56:03 +02004176 struct gpio_desc *desc;
Linus Walleijd468bf92013-09-24 11:54:38 +02004177
Linus Walleij9c102802016-05-25 10:56:03 +02004178 desc = gpiochip_get_desc(chip, offset);
4179 if (IS_ERR(desc))
4180 return PTR_ERR(desc);
4181
Linus Walleij60f83392016-11-12 15:01:09 +01004182 /*
4183 * If it's fast: flush the direction setting if something changed
4184 * behind our back
4185 */
4186 if (!chip->can_sleep && chip->get_direction) {
Andy Shevchenko80956792018-07-09 21:47:21 +03004187 int dir = gpiod_get_direction(desc);
Linus Walleij9c102802016-05-25 10:56:03 +02004188
Andy Shevchenko36b31272018-07-03 03:38:31 +03004189 if (dir < 0) {
4190 chip_err(chip, "%s: cannot get GPIO direction\n",
4191 __func__);
4192 return dir;
4193 }
Linus Walleij9c102802016-05-25 10:56:03 +02004194 }
4195
4196 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09004197 chip_err(chip,
Andy Shevchenkob1911712018-07-03 03:39:03 +03004198 "%s: tried to flag a GPIO set as output for IRQ\n",
4199 __func__);
Linus Walleijd468bf92013-09-24 11:54:38 +02004200 return -EIO;
4201 }
4202
Linus Walleij9c102802016-05-25 10:56:03 +02004203 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
Hans Verkuil4e9439d2018-09-08 11:23:16 +02004204 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
Linus Walleij3940c342016-11-14 00:09:07 +01004205
4206 /*
4207 * If the consumer has not set up a label (such as when the
4208 * IRQ is referenced from .to_irq()) we set up a label here
4209 * so it is clear this is used as an interrupt.
4210 */
4211 if (!desc->label)
4212 desc_set_label(desc, "interrupt");
4213
Linus Walleijd468bf92013-09-24 11:54:38 +02004214 return 0;
4215}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09004216EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02004217
Linus Walleijd468bf92013-09-24 11:54:38 +02004218/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09004219 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09004220 * @chip: the chip the GPIO to lock belongs to
4221 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02004222 *
4223 * This is used directly by GPIO drivers that want to indicate
4224 * that a certain GPIO is no longer used exclusively for IRQ.
4225 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09004226void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02004227{
Linus Walleij3940c342016-11-14 00:09:07 +01004228 struct gpio_desc *desc;
4229
4230 desc = gpiochip_get_desc(chip, offset);
4231 if (IS_ERR(desc))
Linus Walleijd468bf92013-09-24 11:54:38 +02004232 return;
4233
Linus Walleij3940c342016-11-14 00:09:07 +01004234 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
Hans Verkuil4e9439d2018-09-08 11:23:16 +02004235 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
Linus Walleij3940c342016-11-14 00:09:07 +01004236
4237 /* If we only had this marking, erase it */
4238 if (desc->label && !strcmp(desc->label, "interrupt"))
4239 desc_set_label(desc, NULL);
Linus Walleijd468bf92013-09-24 11:54:38 +02004240}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09004241EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02004242
Hans Verkuil4e9439d2018-09-08 11:23:16 +02004243void gpiochip_disable_irq(struct gpio_chip *chip, unsigned int offset)
4244{
4245 struct gpio_desc *desc = gpiochip_get_desc(chip, offset);
4246
4247 if (!IS_ERR(desc) &&
4248 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags)))
4249 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
4250}
4251EXPORT_SYMBOL_GPL(gpiochip_disable_irq);
4252
4253void gpiochip_enable_irq(struct gpio_chip *chip, unsigned int offset)
4254{
4255 struct gpio_desc *desc = gpiochip_get_desc(chip, offset);
4256
4257 if (!IS_ERR(desc) &&
4258 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) {
4259 WARN_ON(test_bit(FLAG_IS_OUT, &desc->flags));
4260 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
4261 }
4262}
4263EXPORT_SYMBOL_GPL(gpiochip_enable_irq);
4264
Linus Walleij6cee3822016-02-11 20:16:45 +01004265bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
4266{
4267 if (offset >= chip->ngpio)
4268 return false;
4269
4270 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
4271}
4272EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
4273
Hans Verkuil4e6b8232018-09-08 11:23:14 +02004274int gpiochip_reqres_irq(struct gpio_chip *chip, unsigned int offset)
4275{
4276 int ret;
4277
4278 if (!try_module_get(chip->gpiodev->owner))
4279 return -ENODEV;
4280
4281 ret = gpiochip_lock_as_irq(chip, offset);
4282 if (ret) {
4283 chip_err(chip, "unable to lock HW IRQ %u for IRQ\n", offset);
4284 module_put(chip->gpiodev->owner);
4285 return ret;
4286 }
4287 return 0;
4288}
4289EXPORT_SYMBOL_GPL(gpiochip_reqres_irq);
4290
4291void gpiochip_relres_irq(struct gpio_chip *chip, unsigned int offset)
4292{
4293 gpiochip_unlock_as_irq(chip, offset);
4294 module_put(chip->gpiodev->owner);
4295}
4296EXPORT_SYMBOL_GPL(gpiochip_relres_irq);
4297
Linus Walleij143b65d2016-02-16 15:41:42 +01004298bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
4299{
4300 if (offset >= chip->ngpio)
4301 return false;
4302
4303 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
4304}
4305EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
4306
4307bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
4308{
4309 if (offset >= chip->ngpio)
4310 return false;
4311
4312 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
4313}
4314EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
4315
Charles Keepax05f479b2017-05-23 15:47:29 +01004316bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset)
4317{
4318 if (offset >= chip->ngpio)
4319 return false;
4320
Andrew Jefferye10f72b2017-11-30 14:25:24 +10304321 return !test_bit(FLAG_TRANSITORY, &chip->gpiodev->descs[offset].flags);
Charles Keepax05f479b2017-05-23 15:47:29 +01004322}
4323EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
4324
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004325/**
4326 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
4327 * @desc: gpio whose value will be returned
4328 *
4329 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07004330 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004331 *
4332 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08004333 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004334int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08004335{
David Brownelld2876d02008-02-04 22:28:20 -08004336 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004337 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02004338 return gpiod_get_raw_value_commit(desc);
David Brownelld2876d02008-02-04 22:28:20 -08004339}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004340EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09004341
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004342/**
4343 * gpiod_get_value_cansleep() - return a gpio's value
4344 * @desc: gpio whose value will be returned
4345 *
4346 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07004347 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004348 *
4349 * This function is to be called from contexts that can sleep.
4350 */
4351int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09004352{
David Brownelld2876d02008-02-04 22:28:20 -08004353 int value;
David Brownelld2876d02008-02-04 22:28:20 -08004354
4355 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004356 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02004357 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07004358 if (value < 0)
4359 return value;
4360
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004361 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
4362 value = !value;
4363
David Brownelld2876d02008-02-04 22:28:20 -08004364 return value;
4365}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004366EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09004367
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004368/**
Lukas Wunnereec1d562017-10-12 12:40:10 +02004369 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004370 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02004371 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004372 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004373 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02004374 *
4375 * Read the raw values of the GPIOs, i.e. the values of the physical lines
4376 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
4377 * else an error code.
4378 *
4379 * This function is to be called from contexts that can sleep.
4380 */
4381int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
4382 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004383 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004384 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02004385{
4386 might_sleep_if(extra_checks);
4387 if (!desc_array)
4388 return -EINVAL;
4389 return gpiod_get_array_value_complex(true, true, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004390 desc_array, array_info,
4391 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02004392}
4393EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
4394
4395/**
4396 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004397 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02004398 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004399 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004400 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02004401 *
4402 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
4403 * into account. Return 0 in case of success, else an error code.
4404 *
4405 * This function is to be called from contexts that can sleep.
4406 */
4407int gpiod_get_array_value_cansleep(unsigned int array_size,
4408 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004409 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004410 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02004411{
4412 might_sleep_if(extra_checks);
4413 if (!desc_array)
4414 return -EINVAL;
4415 return gpiod_get_array_value_complex(false, true, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004416 desc_array, array_info,
4417 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02004418}
4419EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
4420
4421/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004422 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
4423 * @desc: gpio whose value will be assigned
4424 * @value: value to assign
4425 *
4426 * Set the raw value of the GPIO, i.e. the value of its physical line without
4427 * regard for its ACTIVE_LOW status.
4428 *
4429 * This function is to be called from contexts that can sleep.
4430 */
4431void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09004432{
David Brownelld2876d02008-02-04 22:28:20 -08004433 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004434 VALIDATE_DESC_VOID(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02004435 gpiod_set_raw_value_commit(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09004436}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004437EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09004438
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004439/**
4440 * gpiod_set_value_cansleep() - assign a gpio's value
4441 * @desc: gpio whose value will be assigned
4442 * @value: value to assign
4443 *
4444 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
4445 * account
4446 *
4447 * This function is to be called from contexts that can sleep.
4448 */
4449void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09004450{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004451 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004452 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1e77fc82018-01-09 19:08:21 +01004453 gpiod_set_value_nocheck(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08004454}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004455EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08004456
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004457/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02004458 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004459 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004460 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004461 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004462 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004463 *
4464 * Set the raw values of the GPIOs, i.e. the values of the physical lines
4465 * without regard for their ACTIVE_LOW status.
4466 *
4467 * This function is to be called from contexts that can sleep.
4468 */
Laura Abbott30277432018-05-21 10:57:07 -07004469int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
Geert Uytterhoeven3c940662018-09-27 13:38:10 +02004470 struct gpio_desc **desc_array,
4471 struct gpio_array *array_info,
4472 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004473{
4474 might_sleep_if(extra_checks);
4475 if (!desc_array)
Laura Abbott30277432018-05-21 10:57:07 -07004476 return -EINVAL;
4477 return gpiod_set_array_value_complex(true, true, array_size, desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004478 array_info, value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004479}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02004480EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004481
4482/**
Dmitry Torokhov3946d182017-08-14 21:59:55 -07004483 * gpiod_add_lookup_tables() - register GPIO device consumers
4484 * @tables: list of tables of consumers to register
4485 * @n: number of tables in the list
4486 */
4487void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
4488{
4489 unsigned int i;
4490
4491 mutex_lock(&gpio_lookup_lock);
4492
4493 for (i = 0; i < n; i++)
4494 list_add_tail(&tables[i]->list, &gpio_lookup_list);
4495
4496 mutex_unlock(&gpio_lookup_lock);
4497}
4498
4499/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02004500 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004501 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004502 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004503 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004504 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004505 *
4506 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
4507 * into account.
4508 *
4509 * This function is to be called from contexts that can sleep.
4510 */
Geert Uytterhoevencf9af0d2018-09-27 13:38:09 +02004511int gpiod_set_array_value_cansleep(unsigned int array_size,
4512 struct gpio_desc **desc_array,
4513 struct gpio_array *array_info,
4514 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004515{
4516 might_sleep_if(extra_checks);
4517 if (!desc_array)
Geert Uytterhoevencf9af0d2018-09-27 13:38:09 +02004518 return -EINVAL;
4519 return gpiod_set_array_value_complex(false, true, array_size,
4520 desc_array, array_info,
4521 value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004522}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02004523EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004524
4525/**
Alexandre Courbotad824782013-12-03 12:20:11 +09004526 * gpiod_add_lookup_table() - register GPIO device consumers
4527 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004528 */
Alexandre Courbotad824782013-12-03 12:20:11 +09004529void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004530{
4531 mutex_lock(&gpio_lookup_lock);
4532
Alexandre Courbotad824782013-12-03 12:20:11 +09004533 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004534
4535 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08004536}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02004537EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
David Brownelld2876d02008-02-04 22:28:20 -08004538
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05304539/**
4540 * gpiod_remove_lookup_table() - unregister GPIO device consumers
4541 * @table: table of consumers to unregister
4542 */
4543void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
4544{
4545 mutex_lock(&gpio_lookup_lock);
4546
4547 list_del(&table->list);
4548
4549 mutex_unlock(&gpio_lookup_lock);
4550}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02004551EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05304552
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02004553/**
4554 * gpiod_add_hogs() - register a set of GPIO hogs from machine code
4555 * @hogs: table of gpio hog entries with a zeroed sentinel at the end
4556 */
4557void gpiod_add_hogs(struct gpiod_hog *hogs)
4558{
4559 struct gpio_chip *chip;
4560 struct gpiod_hog *hog;
4561
4562 mutex_lock(&gpio_machine_hogs_mutex);
4563
4564 for (hog = &hogs[0]; hog->chip_label; hog++) {
4565 list_add_tail(&hog->list, &gpio_machine_hogs);
4566
4567 /*
4568 * The chip may have been registered earlier, so check if it
4569 * exists and, if so, try to hog the line now.
4570 */
4571 chip = find_chip_by_name(hog->chip_label);
4572 if (chip)
4573 gpiochip_machine_hog(chip, hog);
4574 }
4575
4576 mutex_unlock(&gpio_machine_hogs_mutex);
4577}
4578EXPORT_SYMBOL_GPL(gpiod_add_hogs);
4579
Alexandre Courbotad824782013-12-03 12:20:11 +09004580static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
4581{
4582 const char *dev_id = dev ? dev_name(dev) : NULL;
4583 struct gpiod_lookup_table *table;
4584
4585 mutex_lock(&gpio_lookup_lock);
4586
4587 list_for_each_entry(table, &gpio_lookup_list, list) {
4588 if (table->dev_id && dev_id) {
4589 /*
4590 * Valid strings on both ends, must be identical to have
4591 * a match
4592 */
4593 if (!strcmp(table->dev_id, dev_id))
4594 goto found;
4595 } else {
4596 /*
4597 * One of the pointers is NULL, so both must be to have
4598 * a match
4599 */
4600 if (dev_id == table->dev_id)
4601 goto found;
4602 }
4603 }
4604 table = NULL;
4605
4606found:
4607 mutex_unlock(&gpio_lookup_lock);
4608 return table;
4609}
4610
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004611static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Andy Shevchenkofed70262019-04-10 18:39:16 +03004612 unsigned int idx, unsigned long *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004613{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004614 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09004615 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004616 struct gpiod_lookup *p;
4617
Alexandre Courbotad824782013-12-03 12:20:11 +09004618 table = gpiod_find_lookup_table(dev);
4619 if (!table)
4620 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004621
Alexandre Courbotad824782013-12-03 12:20:11 +09004622 for (p = &table->table[0]; p->chip_label; p++) {
4623 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004624
Alexandre Courbotad824782013-12-03 12:20:11 +09004625 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004626 if (p->idx != idx)
4627 continue;
4628
Alexandre Courbotad824782013-12-03 12:20:11 +09004629 /* If the lookup entry has a con_id, require exact match */
4630 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
4631 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004632
Alexandre Courbotad824782013-12-03 12:20:11 +09004633 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004634
Alexandre Courbotad824782013-12-03 12:20:11 +09004635 if (!chip) {
Janusz Krzysztofik8853daf2018-07-04 00:18:19 +02004636 /*
4637 * As the lookup table indicates a chip with
4638 * p->chip_label should exist, assume it may
4639 * still appear later and let the interested
4640 * consumer be probed again or let the Deferred
4641 * Probe infrastructure handle the error.
4642 */
4643 dev_warn(dev, "cannot find GPIO chip %s, deferring\n",
4644 p->chip_label);
4645 return ERR_PTR(-EPROBE_DEFER);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004646 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004647
Alexandre Courbotad824782013-12-03 12:20:11 +09004648 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004649 dev_err(dev,
Geert Uytterhoevend935bd52019-11-27 10:59:19 +01004650 "requested GPIO %u (%u) is out of range [0..%u] for chip %s\n",
4651 idx, p->chip_hwnum, chip->ngpio - 1,
4652 chip->label);
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004653 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09004654 }
4655
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09004656 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09004657 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004658
4659 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09004660 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004661
4662 return desc;
4663}
4664
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004665static int platform_gpio_count(struct device *dev, const char *con_id)
4666{
4667 struct gpiod_lookup_table *table;
4668 struct gpiod_lookup *p;
4669 unsigned int count = 0;
4670
4671 table = gpiod_find_lookup_table(dev);
4672 if (!table)
4673 return -ENOENT;
4674
4675 for (p = &table->table[0]; p->chip_label; p++) {
4676 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
4677 (!con_id && !p->con_id))
4678 count++;
4679 }
4680 if (!count)
4681 return -ENOENT;
4682
4683 return count;
4684}
4685
4686/**
Dmitry Torokhov13949fa2019-09-12 20:22:39 -07004687 * fwnode_gpiod_get_index - obtain a GPIO from firmware node
4688 * @fwnode: handle of the firmware node
4689 * @con_id: function within the GPIO consumer
4690 * @index: index of the GPIO to obtain for the consumer
4691 * @flags: GPIO initialization flags
4692 * @label: label to attach to the requested GPIO
4693 *
4694 * This function can be used for drivers that get their configuration
4695 * from opaque firmware.
4696 *
4697 * The function properly finds the corresponding GPIO using whatever is the
4698 * underlying firmware interface and then makes sure that the GPIO
4699 * descriptor is requested before it is returned to the caller.
4700 *
4701 * Returns:
4702 * On successful request the GPIO pin is configured in accordance with
4703 * provided @flags.
4704 *
4705 * In case of error an ERR_PTR() is returned.
4706 */
4707struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
4708 const char *con_id, int index,
4709 enum gpiod_flags flags,
4710 const char *label)
4711{
4712 struct gpio_desc *desc;
4713 char prop_name[32]; /* 32 is max size of property name */
4714 unsigned int i;
4715
4716 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
4717 if (con_id)
4718 snprintf(prop_name, sizeof(prop_name), "%s-%s",
4719 con_id, gpio_suffixes[i]);
4720 else
4721 snprintf(prop_name, sizeof(prop_name), "%s",
4722 gpio_suffixes[i]);
4723
4724 desc = fwnode_get_named_gpiod(fwnode, prop_name, index, flags,
4725 label);
4726 if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT))
4727 break;
4728 }
4729
4730 return desc;
4731}
4732EXPORT_SYMBOL_GPL(fwnode_gpiod_get_index);
4733
4734/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004735 * gpiod_count - return the number of GPIOs associated with a device / function
4736 * or -ENOENT if no GPIO has been assigned to the requested function
4737 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4738 * @con_id: function within the GPIO consumer
4739 */
4740int gpiod_count(struct device *dev, const char *con_id)
4741{
4742 int count = -ENOENT;
4743
4744 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
Linus Walleijf626d6d2019-07-17 09:10:01 +02004745 count = of_gpio_get_count(dev, con_id);
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004746 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
4747 count = acpi_gpio_count(dev, con_id);
4748
4749 if (count < 0)
4750 count = platform_gpio_count(dev, con_id);
4751
4752 return count;
4753}
4754EXPORT_SYMBOL_GPL(gpiod_count);
4755
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004756/**
Thierry Reding08791622014-04-25 16:54:22 +02004757 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09004758 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004759 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004760 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004761 *
4762 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004763 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07004764 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004765 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004766struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004767 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004768{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004769 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004770}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004771EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004772
4773/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02004774 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
4775 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4776 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004777 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02004778 *
4779 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
4780 * the requested function it will return NULL. This is convenient for drivers
4781 * that need to handle optional GPIOs.
4782 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004783struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004784 const char *con_id,
4785 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02004786{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004787 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02004788}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004789EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02004790
Benoit Parrotf625d462015-02-02 11:44:44 -06004791
4792/**
4793 * gpiod_configure_flags - helper function to configure a given GPIO
4794 * @desc: gpio whose value will be assigned
4795 * @con_id: function within the GPIO consumer
Andy Shevchenkofed70262019-04-10 18:39:16 +03004796 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4797 * of_find_gpio() or of_get_gpio_hog()
Benoit Parrotf625d462015-02-02 11:44:44 -06004798 * @dflags: gpiod_flags - optional GPIO initialization flags
4799 *
4800 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
4801 * requested function and/or index, or another IS_ERR() code if an error
4802 * occurred while trying to acquire the GPIO.
4803 */
Andy Shevchenkoc29fd9e2017-05-23 20:03:16 +03004804int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Johan Hovold85b03b32016-07-03 18:32:05 +02004805 unsigned long lflags, enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06004806{
Linus Walleijd377f562019-07-16 11:11:45 +02004807 int ret;
Benoit Parrotf625d462015-02-02 11:44:44 -06004808
Johan Hovold85b03b32016-07-03 18:32:05 +02004809 if (lflags & GPIO_ACTIVE_LOW)
4810 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
Linus Walleijf926dfc2017-09-10 19:26:22 +02004811
Johan Hovold85b03b32016-07-03 18:32:05 +02004812 if (lflags & GPIO_OPEN_DRAIN)
4813 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
Linus Walleijf926dfc2017-09-10 19:26:22 +02004814 else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) {
4815 /*
4816 * This enforces open drain mode from the consumer side.
4817 * This is necessary for some busses like I2C, but the lookup
4818 * should *REALLY* have specified them as open drain in the
4819 * first place, so print a little warning here.
4820 */
4821 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
4822 gpiod_warn(desc,
4823 "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
4824 }
4825
Johan Hovold85b03b32016-07-03 18:32:05 +02004826 if (lflags & GPIO_OPEN_SOURCE)
4827 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Andrew Jefferye10f72b2017-11-30 14:25:24 +10304828
Thomas Petazzonid4499912019-02-07 17:28:58 +01004829 if ((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DOWN)) {
4830 gpiod_err(desc,
4831 "both pull-up and pull-down enabled, invalid configuration\n");
4832 return -EINVAL;
4833 }
4834
4835 if (lflags & GPIO_PULL_UP)
4836 set_bit(FLAG_PULL_UP, &desc->flags);
4837 else if (lflags & GPIO_PULL_DOWN)
4838 set_bit(FLAG_PULL_DOWN, &desc->flags);
4839
Linus Walleijd377f562019-07-16 11:11:45 +02004840 ret = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
4841 if (ret < 0)
4842 return ret;
Johan Hovold85b03b32016-07-03 18:32:05 +02004843
Benoit Parrotf625d462015-02-02 11:44:44 -06004844 /* No particular flag request, return here... */
4845 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
4846 pr_debug("no flags found for %s\n", con_id);
4847 return 0;
4848 }
4849
4850 /* Process flags */
4851 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
Linus Walleijd377f562019-07-16 11:11:45 +02004852 ret = gpiod_direction_output(desc,
Linus Walleijad177312016-11-13 23:02:44 +01004853 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
Benoit Parrotf625d462015-02-02 11:44:44 -06004854 else
Linus Walleijd377f562019-07-16 11:11:45 +02004855 ret = gpiod_direction_input(desc);
Benoit Parrotf625d462015-02-02 11:44:44 -06004856
Linus Walleijd377f562019-07-16 11:11:45 +02004857 return ret;
Benoit Parrotf625d462015-02-02 11:44:44 -06004858}
4859
Thierry Reding29a1f2332014-04-25 17:10:06 +02004860/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004861 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02004862 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004863 * @con_id: function within the GPIO consumer
4864 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004865 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004866 *
4867 * This variant of gpiod_get() allows to access GPIOs other than the first
4868 * defined one for functions that define several GPIOs.
4869 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004870 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
4871 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07004872 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004873 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004874struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004875 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004876 unsigned int idx,
4877 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004878{
Andy Shevchenko2d6c06f2019-04-10 18:39:17 +03004879 unsigned long lookupflags = GPIO_LOOKUP_FLAGS_DEFAULT;
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09004880 struct gpio_desc *desc = NULL;
Linus Walleijd377f562019-07-16 11:11:45 +02004881 int ret;
Linus Walleij7d18f0a2018-01-16 08:29:50 +01004882 /* Maybe we have a device name, maybe not */
4883 const char *devname = dev ? dev_name(dev) : "?";
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004884
4885 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
4886
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01004887 if (dev) {
4888 /* Using device tree? */
4889 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
4890 dev_dbg(dev, "using device tree for GPIO lookup\n");
4891 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
4892 } else if (ACPI_COMPANION(dev)) {
4893 dev_dbg(dev, "using ACPI for GPIO lookup\n");
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +03004894 desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags);
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01004895 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09004896 }
4897
4898 /*
4899 * Either we are not using DT or ACPI, or their lookup did not return
4900 * a result. In that case, use platform lookup as a fallback.
4901 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004902 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04004903 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004904 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004905 }
4906
4907 if (IS_ERR(desc)) {
Wang Dongsheng9d5a1f22018-02-27 00:12:13 -08004908 dev_dbg(dev, "No GPIO consumer %s found\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004909 return desc;
4910 }
4911
Linus Walleij7d18f0a2018-01-16 08:29:50 +01004912 /*
4913 * If a connection label was passed use that, else attempt to use
4914 * the device name as label
4915 */
Linus Walleijd377f562019-07-16 11:11:45 +02004916 ret = gpiod_request(desc, con_id ? con_id : devname);
4917 if (ret < 0) {
4918 if (ret == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE) {
Linus Walleijb0ce7b292018-10-12 14:54:12 +02004919 /*
4920 * This happens when there are several consumers for
4921 * the same GPIO line: we just return here without
4922 * further initialization. It is a bit if a hack.
4923 * This is necessary to support fixed regulators.
4924 *
4925 * FIXME: Make this more sane and safe.
4926 */
4927 dev_info(dev, "nonexclusive access to GPIO for %s\n",
4928 con_id ? con_id : devname);
4929 return desc;
4930 } else {
Linus Walleijd377f562019-07-16 11:11:45 +02004931 return ERR_PTR(ret);
Linus Walleijb0ce7b292018-10-12 14:54:12 +02004932 }
4933 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004934
Linus Walleijd377f562019-07-16 11:11:45 +02004935 ret = gpiod_configure_flags(desc, con_id, lookupflags, flags);
4936 if (ret < 0) {
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004937 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
4938 gpiod_put(desc);
Linus Walleijd377f562019-07-16 11:11:45 +02004939 return ERR_PTR(ret);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004940 }
4941
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004942 return desc;
4943}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004944EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004945
4946/**
Mika Westerberg40b73182014-10-21 13:33:59 +02004947 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
4948 * @fwnode: handle of the firmware node
4949 * @propname: name of the firmware property representing the GPIO
Linus Walleij6392cca2017-12-29 02:07:54 +01004950 * @index: index of the GPIO to obtain for the consumer
Andy Shevchenkoa264d102017-01-09 16:02:28 +02004951 * @dflags: GPIO initialization flags
Thierry Reding950d55f52017-07-24 16:57:22 +02004952 * @label: label to attach to the requested GPIO
Mika Westerberg40b73182014-10-21 13:33:59 +02004953 *
4954 * This function can be used for drivers that get their configuration
Linus Walleij6392cca2017-12-29 02:07:54 +01004955 * from opaque firmware.
Mika Westerberg40b73182014-10-21 13:33:59 +02004956 *
Linus Walleij6392cca2017-12-29 02:07:54 +01004957 * The function properly finds the corresponding GPIO using whatever is the
Mika Westerberg40b73182014-10-21 13:33:59 +02004958 * underlying firmware interface and then makes sure that the GPIO
4959 * descriptor is requested before it is returned to the caller.
4960 *
Thierry Reding950d55f52017-07-24 16:57:22 +02004961 * Returns:
Andy Shevchenkoff213782017-02-28 17:03:12 +02004962 * On successful request the GPIO pin is configured in accordance with
Andy Shevchenkoa264d102017-01-09 16:02:28 +02004963 * provided @dflags.
4964 *
Mika Westerberg40b73182014-10-21 13:33:59 +02004965 * In case of error an ERR_PTR() is returned.
4966 */
4967struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
Boris Brezillon537b94d2017-02-02 14:53:11 +01004968 const char *propname, int index,
Alexander Steinb2987d72017-01-12 17:39:24 +01004969 enum gpiod_flags dflags,
4970 const char *label)
Mika Westerberg40b73182014-10-21 13:33:59 +02004971{
Andy Shevchenko2d6c06f2019-04-10 18:39:17 +03004972 unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
Mika Westerberg40b73182014-10-21 13:33:59 +02004973 struct gpio_desc *desc = ERR_PTR(-ENODEV);
Mika Westerberg40b73182014-10-21 13:33:59 +02004974 int ret;
4975
4976 if (!fwnode)
4977 return ERR_PTR(-EINVAL);
4978
4979 if (is_of_node(fwnode)) {
Linus Walleij6392cca2017-12-29 02:07:54 +01004980 desc = gpiod_get_from_of_node(to_of_node(fwnode),
4981 propname, index,
4982 dflags,
4983 label);
4984 return desc;
Mika Westerberg40b73182014-10-21 13:33:59 +02004985 } else if (is_acpi_node(fwnode)) {
4986 struct acpi_gpio_info info;
4987
Boris Brezillon537b94d2017-02-02 14:53:11 +01004988 desc = acpi_node_get_gpiod(fwnode, propname, index, &info);
Linus Walleij6392cca2017-12-29 02:07:54 +01004989 if (IS_ERR(desc))
4990 return desc;
4991
4992 acpi_gpio_update_gpiod_flags(&dflags, &info);
Andy Shevchenko606be342019-04-10 18:39:20 +03004993 acpi_gpio_update_gpiod_lookup_flags(&lflags, &info);
Mika Westerberg40b73182014-10-21 13:33:59 +02004994 }
4995
Linus Walleij6392cca2017-12-29 02:07:54 +01004996 /* Currently only ACPI takes this path */
Alexander Steinb2987d72017-01-12 17:39:24 +01004997 ret = gpiod_request(desc, label);
Johan Hovold85b03b32016-07-03 18:32:05 +02004998 if (ret)
4999 return ERR_PTR(ret);
5000
Andy Shevchenkoa264d102017-01-09 16:02:28 +02005001 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
5002 if (ret < 0) {
5003 gpiod_put(desc);
5004 return ERR_PTR(ret);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03005005 }
5006
Mika Westerberg40b73182014-10-21 13:33:59 +02005007 return desc;
5008}
5009EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
5010
5011/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02005012 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
5013 * function
5014 * @dev: GPIO consumer, can be NULL for system-global GPIOs
5015 * @con_id: function within the GPIO consumer
5016 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09005017 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02005018 *
5019 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
5020 * specified index was assigned to the requested function it will return NULL.
5021 * This is convenient for drivers that need to handle optional GPIOs.
5022 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01005023struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02005024 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09005025 unsigned int index,
5026 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02005027{
5028 struct gpio_desc *desc;
5029
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09005030 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02005031 if (IS_ERR(desc)) {
5032 if (PTR_ERR(desc) == -ENOENT)
5033 return NULL;
5034 }
5035
5036 return desc;
5037}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01005038EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02005039
5040/**
Benoit Parrotf625d462015-02-02 11:44:44 -06005041 * gpiod_hog - Hog the specified GPIO desc given the provided flags
5042 * @desc: gpio whose value will be assigned
5043 * @name: gpio line name
Andy Shevchenkofed70262019-04-10 18:39:16 +03005044 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
5045 * of_find_gpio() or of_get_gpio_hog()
Benoit Parrotf625d462015-02-02 11:44:44 -06005046 * @dflags: gpiod_flags - optional GPIO initialization flags
5047 */
5048int gpiod_hog(struct gpio_desc *desc, const char *name,
5049 unsigned long lflags, enum gpiod_flags dflags)
5050{
5051 struct gpio_chip *chip;
5052 struct gpio_desc *local_desc;
5053 int hwnum;
Linus Walleijd377f562019-07-16 11:11:45 +02005054 int ret;
Benoit Parrotf625d462015-02-02 11:44:44 -06005055
5056 chip = gpiod_to_chip(desc);
5057 hwnum = gpio_chip_hwgpio(desc);
5058
Linus Walleij5923ea62019-04-26 14:40:18 +02005059 local_desc = gpiochip_request_own_desc(chip, hwnum, name,
5060 lflags, dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -06005061 if (IS_ERR(local_desc)) {
Linus Walleijd377f562019-07-16 11:11:45 +02005062 ret = PTR_ERR(local_desc);
Laxman Dewanganc31a5712016-03-11 19:13:21 +05305063 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
Linus Walleijd377f562019-07-16 11:11:45 +02005064 name, chip->label, hwnum, ret);
5065 return ret;
Benoit Parrotf625d462015-02-02 11:44:44 -06005066 }
5067
Benoit Parrotf625d462015-02-02 11:44:44 -06005068 /* Mark GPIO as hogged so it can be identified and removed later */
5069 set_bit(FLAG_IS_HOGGED, &desc->flags);
5070
5071 pr_info("GPIO line %d (%s) hogged as %s%s\n",
5072 desc_to_gpio(desc), name,
Bartosz Golaszewskib27f3002019-11-13 14:16:29 +01005073 (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
5074 (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ?
5075 (dflags & GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low" : "");
Benoit Parrotf625d462015-02-02 11:44:44 -06005076
5077 return 0;
5078}
5079
5080/**
5081 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
5082 * @chip: gpio chip to act on
Benoit Parrotf625d462015-02-02 11:44:44 -06005083 */
5084static void gpiochip_free_hogs(struct gpio_chip *chip)
5085{
5086 int id;
5087
5088 for (id = 0; id < chip->ngpio; id++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01005089 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
5090 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
Benoit Parrotf625d462015-02-02 11:44:44 -06005091 }
5092}
5093
5094/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01005095 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
5096 * @dev: GPIO consumer, can be NULL for system-global GPIOs
5097 * @con_id: function within the GPIO consumer
5098 * @flags: optional GPIO initialization flags
5099 *
5100 * This function acquires all the GPIOs defined under a given function.
5101 *
5102 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
5103 * no GPIO has been assigned to the requested function, or another IS_ERR()
5104 * code if an error occurred while trying to acquire the GPIOs.
5105 */
5106struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
5107 const char *con_id,
5108 enum gpiod_flags flags)
5109{
5110 struct gpio_desc *desc;
5111 struct gpio_descs *descs;
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02005112 struct gpio_array *array_info = NULL;
5113 struct gpio_chip *chip;
5114 int count, bitmap_size;
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01005115
5116 count = gpiod_count(dev, con_id);
5117 if (count < 0)
5118 return ERR_PTR(count);
5119
Kees Cookacafe7e2018-05-08 13:45:50 -07005120 descs = kzalloc(struct_size(descs, desc, count), GFP_KERNEL);
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01005121 if (!descs)
5122 return ERR_PTR(-ENOMEM);
5123
5124 for (descs->ndescs = 0; descs->ndescs < count; ) {
5125 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
5126 if (IS_ERR(desc)) {
5127 gpiod_put_array(descs);
5128 return ERR_CAST(desc);
5129 }
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02005130
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01005131 descs->desc[descs->ndescs] = desc;
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02005132
5133 chip = gpiod_to_chip(desc);
5134 /*
Janusz Krzysztofikc4c958a2018-09-24 01:53:36 +02005135 * If pin hardware number of array member 0 is also 0, select
5136 * its chip as a candidate for fast bitmap processing path.
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02005137 */
Janusz Krzysztofikc4c958a2018-09-24 01:53:36 +02005138 if (descs->ndescs == 0 && gpio_chip_hwgpio(desc) == 0) {
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02005139 struct gpio_descs *array;
5140
5141 bitmap_size = BITS_TO_LONGS(chip->ngpio > count ?
5142 chip->ngpio : count);
5143
5144 array = kzalloc(struct_size(descs, desc, count) +
5145 struct_size(array_info, invert_mask,
5146 3 * bitmap_size), GFP_KERNEL);
5147 if (!array) {
5148 gpiod_put_array(descs);
5149 return ERR_PTR(-ENOMEM);
5150 }
5151
5152 memcpy(array, descs,
5153 struct_size(descs, desc, descs->ndescs + 1));
5154 kfree(descs);
5155
5156 descs = array;
5157 array_info = (void *)(descs->desc + count);
5158 array_info->get_mask = array_info->invert_mask +
5159 bitmap_size;
5160 array_info->set_mask = array_info->get_mask +
5161 bitmap_size;
5162
5163 array_info->desc = descs->desc;
5164 array_info->size = count;
5165 array_info->chip = chip;
5166 bitmap_set(array_info->get_mask, descs->ndescs,
5167 count - descs->ndescs);
5168 bitmap_set(array_info->set_mask, descs->ndescs,
5169 count - descs->ndescs);
5170 descs->info = array_info;
5171 }
Janusz Krzysztofikc4c958a2018-09-24 01:53:36 +02005172 /* Unmark array members which don't belong to the 'fast' chip */
5173 if (array_info && array_info->chip != chip) {
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02005174 __clear_bit(descs->ndescs, array_info->get_mask);
5175 __clear_bit(descs->ndescs, array_info->set_mask);
Janusz Krzysztofikc4c958a2018-09-24 01:53:36 +02005176 }
5177 /*
5178 * Detect array members which belong to the 'fast' chip
5179 * but their pins are not in hardware order.
5180 */
5181 else if (array_info &&
5182 gpio_chip_hwgpio(desc) != descs->ndescs) {
5183 /*
5184 * Don't use fast path if all array members processed so
5185 * far belong to the same chip as this one but its pin
5186 * hardware number is different from its array index.
5187 */
5188 if (bitmap_full(array_info->get_mask, descs->ndescs)) {
5189 array_info = NULL;
5190 } else {
5191 __clear_bit(descs->ndescs,
5192 array_info->get_mask);
5193 __clear_bit(descs->ndescs,
5194 array_info->set_mask);
5195 }
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02005196 } else if (array_info) {
5197 /* Exclude open drain or open source from fast output */
5198 if (gpiochip_line_is_open_drain(chip, descs->ndescs) ||
5199 gpiochip_line_is_open_source(chip, descs->ndescs))
5200 __clear_bit(descs->ndescs,
5201 array_info->set_mask);
5202 /* Identify 'fast' pins which require invertion */
5203 if (gpiod_is_active_low(desc))
5204 __set_bit(descs->ndescs,
5205 array_info->invert_mask);
5206 }
5207
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01005208 descs->ndescs++;
5209 }
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02005210 if (array_info)
5211 dev_dbg(dev,
5212 "GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n",
5213 array_info->chip->label, array_info->size,
5214 *array_info->get_mask, *array_info->set_mask,
5215 *array_info->invert_mask);
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01005216 return descs;
5217}
5218EXPORT_SYMBOL_GPL(gpiod_get_array);
5219
5220/**
5221 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
5222 * function
5223 * @dev: GPIO consumer, can be NULL for system-global GPIOs
5224 * @con_id: function within the GPIO consumer
5225 * @flags: optional GPIO initialization flags
5226 *
5227 * This is equivalent to gpiod_get_array(), except that when no GPIO was
5228 * assigned to the requested function it will return NULL.
5229 */
5230struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
5231 const char *con_id,
5232 enum gpiod_flags flags)
5233{
5234 struct gpio_descs *descs;
5235
5236 descs = gpiod_get_array(dev, con_id, flags);
Masahiro Yamada45586c72020-02-03 17:37:45 -08005237 if (PTR_ERR(descs) == -ENOENT)
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01005238 return NULL;
5239
5240 return descs;
5241}
5242EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
5243
5244/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07005245 * gpiod_put - dispose of a GPIO descriptor
5246 * @desc: GPIO descriptor to dispose of
5247 *
5248 * No descriptor can be used after gpiod_put() has been called on it.
5249 */
5250void gpiod_put(struct gpio_desc *desc)
5251{
Andy Shevchenko1d7765b2019-03-26 17:21:14 +02005252 if (desc)
5253 gpiod_free(desc);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07005254}
5255EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08005256
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01005257/**
5258 * gpiod_put_array - dispose of multiple GPIO descriptors
5259 * @descs: struct gpio_descs containing an array of descriptors
5260 */
5261void gpiod_put_array(struct gpio_descs *descs)
5262{
5263 unsigned int i;
5264
5265 for (i = 0; i < descs->ndescs; i++)
5266 gpiod_put(descs->desc[i]);
5267
5268 kfree(descs);
5269}
5270EXPORT_SYMBOL_GPL(gpiod_put_array);
5271
Linus Walleij3c702e92015-10-21 15:29:53 +02005272static int __init gpiolib_dev_init(void)
5273{
5274 int ret;
5275
5276 /* Register GPIO sysfs bus */
Andy Shevchenkob1911712018-07-03 03:39:03 +03005277 ret = bus_register(&gpio_bus_type);
Linus Walleij3c702e92015-10-21 15:29:53 +02005278 if (ret < 0) {
5279 pr_err("gpiolib: could not register GPIO bus type\n");
5280 return ret;
5281 }
5282
Geert Uytterhoevenddd88912019-11-27 09:42:47 +01005283 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, GPIOCHIP_NAME);
Linus Walleij3c702e92015-10-21 15:29:53 +02005284 if (ret < 0) {
5285 pr_err("gpiolib: failed to allocate char dev region\n");
5286 bus_unregister(&gpio_bus_type);
Geert Uytterhoeven63636d92020-02-20 14:01:49 +01005287 return ret;
Linus Walleij3c702e92015-10-21 15:29:53 +02005288 }
Geert Uytterhoeven63636d92020-02-20 14:01:49 +01005289
5290 gpiolib_initialized = true;
5291 gpiochip_setup_devs();
5292
5293 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
5294 WARN_ON(of_reconfig_notifier_register(&gpio_of_notifier));
5295
Linus Walleij3c702e92015-10-21 15:29:53 +02005296 return ret;
5297}
5298core_initcall(gpiolib_dev_init);
5299
David Brownelld2876d02008-02-04 22:28:20 -08005300#ifdef CONFIG_DEBUG_FS
5301
Linus Walleijfdeb8e12016-02-10 10:57:36 +01005302static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
David Brownelld2876d02008-02-04 22:28:20 -08005303{
5304 unsigned i;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01005305 struct gpio_chip *chip = gdev->chip;
5306 unsigned gpio = gdev->base;
5307 struct gpio_desc *gdesc = &gdev->descs[0];
Linus Walleij90fd2272018-10-01 23:06:17 +02005308 bool is_out;
5309 bool is_irq;
5310 bool active_low;
David Brownelld2876d02008-02-04 22:28:20 -08005311
Linus Walleijfdeb8e12016-02-10 10:57:36 +01005312 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02005313 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
5314 if (gdesc->name) {
5315 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
5316 gpio, gdesc->name);
5317 }
David Brownelld2876d02008-02-04 22:28:20 -08005318 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02005319 }
David Brownelld2876d02008-02-04 22:28:20 -08005320
Alexandre Courbot372e7222013-02-03 01:29:29 +09005321 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08005322 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02005323 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Linus Walleij90fd2272018-10-01 23:06:17 +02005324 active_low = test_bit(FLAG_ACTIVE_LOW, &gdesc->flags);
5325 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s%s",
Markus Pargmannced433e2015-08-14 16:11:02 +02005326 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08005327 is_out ? "out" : "in ",
Andy Shevchenko1c22a252018-07-12 20:36:42 +03005328 chip->get ? (chip->get(chip, i) ? "hi" : "lo") : "? ",
Linus Walleij90fd2272018-10-01 23:06:17 +02005329 is_irq ? "IRQ " : "",
5330 active_low ? "ACTIVE LOW" : "");
David Brownelld2876d02008-02-04 22:28:20 -08005331 seq_printf(s, "\n");
5332 }
5333}
5334
Thierry Redingf9c4a312012-04-12 13:26:01 +02005335static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08005336{
Grant Likely362432a2013-02-09 09:41:49 +00005337 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02005338 struct gpio_device *gdev = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09005339 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02005340
5341 s->private = "";
5342
Grant Likely362432a2013-02-09 09:41:49 +00005343 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02005344 list_for_each_entry(gdev, &gpio_devices, list)
Grant Likely362432a2013-02-09 09:41:49 +00005345 if (index-- == 0) {
5346 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02005347 return gdev;
Grant Likely362432a2013-02-09 09:41:49 +00005348 }
5349 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09005350
5351 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02005352}
5353
5354static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
5355{
Grant Likely362432a2013-02-09 09:41:49 +00005356 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02005357 struct gpio_device *gdev = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02005358 void *ret = NULL;
5359
Grant Likely362432a2013-02-09 09:41:49 +00005360 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02005361 if (list_is_last(&gdev->list, &gpio_devices))
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09005362 ret = NULL;
5363 else
Linus Walleijff2b1352015-10-20 11:10:38 +02005364 ret = list_entry(gdev->list.next, struct gpio_device, list);
Grant Likely362432a2013-02-09 09:41:49 +00005365 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02005366
5367 s->private = "\n";
5368 ++*pos;
5369
5370 return ret;
5371}
5372
5373static void gpiolib_seq_stop(struct seq_file *s, void *v)
5374{
5375}
5376
5377static int gpiolib_seq_show(struct seq_file *s, void *v)
5378{
Linus Walleijff2b1352015-10-20 11:10:38 +02005379 struct gpio_device *gdev = v;
5380 struct gpio_chip *chip = gdev->chip;
5381 struct device *parent;
Thierry Redingf9c4a312012-04-12 13:26:01 +02005382
Linus Walleijff2b1352015-10-20 11:10:38 +02005383 if (!chip) {
5384 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
5385 dev_name(&gdev->dev));
5386 return 0;
5387 }
5388
5389 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
5390 dev_name(&gdev->dev),
Linus Walleijfdeb8e12016-02-10 10:57:36 +01005391 gdev->base, gdev->base + gdev->ngpio - 1);
Linus Walleijff2b1352015-10-20 11:10:38 +02005392 parent = chip->parent;
5393 if (parent)
5394 seq_printf(s, ", parent: %s/%s",
5395 parent->bus ? parent->bus->name : "no-bus",
5396 dev_name(parent));
Thierry Redingf9c4a312012-04-12 13:26:01 +02005397 if (chip->label)
5398 seq_printf(s, ", %s", chip->label);
5399 if (chip->can_sleep)
5400 seq_printf(s, ", can sleep");
5401 seq_printf(s, ":\n");
5402
5403 if (chip->dbg_show)
5404 chip->dbg_show(s, chip);
5405 else
Linus Walleijfdeb8e12016-02-10 10:57:36 +01005406 gpiolib_dbg_show(s, gdev);
Thierry Redingf9c4a312012-04-12 13:26:01 +02005407
David Brownelld2876d02008-02-04 22:28:20 -08005408 return 0;
5409}
5410
Thierry Redingf9c4a312012-04-12 13:26:01 +02005411static const struct seq_operations gpiolib_seq_ops = {
5412 .start = gpiolib_seq_start,
5413 .next = gpiolib_seq_next,
5414 .stop = gpiolib_seq_stop,
5415 .show = gpiolib_seq_show,
5416};
5417
David Brownelld2876d02008-02-04 22:28:20 -08005418static int gpiolib_open(struct inode *inode, struct file *file)
5419{
Thierry Redingf9c4a312012-04-12 13:26:01 +02005420 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08005421}
5422
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005423static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02005424 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08005425 .open = gpiolib_open,
5426 .read = seq_read,
5427 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02005428 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08005429};
5430
5431static int __init gpiolib_debugfs_init(void)
5432{
5433 /* /sys/kernel/debug/gpio */
Greg Kroah-Hartmanacc68b02019-06-18 17:50:45 +02005434 debugfs_create_file("gpio", S_IFREG | S_IRUGO, NULL, NULL,
5435 &gpiolib_operations);
David Brownelld2876d02008-02-04 22:28:20 -08005436 return 0;
5437}
5438subsys_initcall(gpiolib_debugfs_init);
5439
5440#endif /* DEBUG_FS */