blob: df9f6ed96b647f235bea209161c6c78ee2a3dcd9 [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/fs.h>
Linus Walleij8b92e172016-05-27 14:24:04 +020021#include <linux/compat.h>
Lars-Peter Clausen953b9562016-10-24 13:59:15 +020022#include <linux/file.h>
Linus Walleij3c702e92015-10-21 15:29:53 +020023#include <uapi/linux/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080024
Mika Westerberg664e3e52014-01-08 12:40:54 +020025#include "gpiolib.h"
Linus Walleijf626d6d2019-07-17 09:10:01 +020026#include "gpiolib-of.h"
Andy Shevchenko77cb9072019-07-30 13:43:36 +030027#include "gpiolib-acpi.h"
Kent Gibson925ca362020-06-16 17:36:15 +080028#include "gpiolib-cdev.h"
Kent Gibsonef087d82020-07-08 12:15:44 +080029#include "gpiolib-sysfs.h"
Mika Westerberg664e3e52014-01-08 12:40:54 +020030
Uwe Kleine-König3f397c212011-05-20 00:40:19 -060031#define CREATE_TRACE_POINTS
32#include <trace/events/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080033
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070034/* Implementation infrastructure for GPIO interfaces.
David Brownelld2876d02008-02-04 22:28:20 -080035 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070036 * The GPIO programming interface allows for inlining speed-critical
37 * get/set operations for common cases, so that access to SOC-integrated
38 * GPIOs can sometimes cost only an instruction or two per bit.
David Brownelld2876d02008-02-04 22:28:20 -080039 */
40
41
42/* When debugging, extend minimal trust to callers and platform code.
43 * Also emit diagnostic messages that may help initial bringup, when
44 * board setup or driver bugs are most common.
45 *
46 * Otherwise, minimize overhead in what may be bitbanging codepaths.
47 */
48#ifdef DEBUG
49#define extra_checks 1
50#else
51#define extra_checks 0
52#endif
53
Linus Walleijff2b1352015-10-20 11:10:38 +020054/* Device and char device-related information */
55static DEFINE_IDA(gpio_ida);
Linus Walleij3c702e92015-10-21 15:29:53 +020056static dev_t gpio_devt;
57#define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
58static struct bus_type gpio_bus_type = {
59 .name = "gpio",
60};
Linus Walleijff2b1352015-10-20 11:10:38 +020061
Laura Abbott30277432018-05-21 10:57:07 -070062/*
63 * Number of GPIOs to use for the fast path in set array
64 */
65#define FASTPATH_NGPIO CONFIG_GPIOLIB_FASTPATH_LIMIT
66
David Brownelld2876d02008-02-04 22:28:20 -080067/* gpio_lock prevents conflicts during gpio_desc[] table updates.
68 * While any GPIO is requested, its gpio_chip is not removable;
69 * each GPIO's "requested" flag serves as a lock and refcount.
70 */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090071DEFINE_SPINLOCK(gpio_lock);
David Brownelld2876d02008-02-04 22:28:20 -080072
Alexandre Courbotbae48da2013-10-17 10:21:38 -070073static DEFINE_MUTEX(gpio_lookup_lock);
74static LIST_HEAD(gpio_lookup_list);
Linus Walleijff2b1352015-10-20 11:10:38 +020075LIST_HEAD(gpio_devices);
Johan Hovold6d867502015-05-04 17:23:25 +020076
Bartosz Golaszewskia411e812018-04-10 22:30:28 +020077static DEFINE_MUTEX(gpio_machine_hogs_mutex);
78static LIST_HEAD(gpio_machine_hogs);
79
Linus Walleija0b66a72020-03-29 16:04:05 +020080static void gpiochip_free_hogs(struct gpio_chip *gc);
81static int gpiochip_add_irqchip(struct gpio_chip *gc,
Andrew Lunn39c3fd52017-12-02 18:11:04 +010082 struct lock_class_key *lock_key,
83 struct lock_class_key *request_key);
Linus Walleija0b66a72020-03-29 16:04:05 +020084static void gpiochip_irqchip_remove(struct gpio_chip *gc);
85static int gpiochip_irqchip_init_hw(struct gpio_chip *gc);
86static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc);
87static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc);
Johan Hovold6d867502015-05-04 17:23:25 +020088
Guenter Roeck159f3cd2016-03-31 08:11:30 -070089static bool gpiolib_initialized;
Johan Hovold6d867502015-05-04 17:23:25 +020090
David Brownelld2876d02008-02-04 22:28:20 -080091static inline void desc_set_label(struct gpio_desc *d, const char *label)
92{
David Brownelld2876d02008-02-04 22:28:20 -080093 d->label = label;
David Brownelld2876d02008-02-04 22:28:20 -080094}
95
Alexandre Courbot372e7222013-02-03 01:29:29 +090096/**
Thierry Reding950d55f52017-07-24 16:57:22 +020097 * gpio_to_desc - Convert a GPIO number to its descriptor
98 * @gpio: global GPIO number
99 *
100 * Returns:
101 * The GPIO descriptor associated with the given GPIO, or %NULL if no GPIO
102 * with the given number exists in the system.
Alexandre Courbot372e7222013-02-03 01:29:29 +0900103 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700104struct gpio_desc *gpio_to_desc(unsigned gpio)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900105{
Linus Walleijff2b1352015-10-20 11:10:38 +0200106 struct gpio_device *gdev;
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900107 unsigned long flags;
108
109 spin_lock_irqsave(&gpio_lock, flags);
110
Linus Walleijff2b1352015-10-20 11:10:38 +0200111 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100112 if (gdev->base <= gpio &&
113 gdev->base + gdev->ngpio > gpio) {
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900114 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100115 return &gdev->descs[gpio - gdev->base];
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900116 }
117 }
118
119 spin_unlock_irqrestore(&gpio_lock, flags);
120
Alexandre Courbot0e9a5ed2014-12-02 23:15:05 +0900121 if (!gpio_is_valid(gpio))
122 WARN(1, "invalid GPIO %d\n", gpio);
123
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900124 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900125}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700126EXPORT_SYMBOL_GPL(gpio_to_desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900127
128/**
Thierry Reding950d55f52017-07-24 16:57:22 +0200129 * gpiochip_get_desc - get the GPIO descriptor corresponding to the given
130 * hardware number for this chip
Linus Walleija0b66a72020-03-29 16:04:05 +0200131 * @gc: GPIO chip
Thierry Reding950d55f52017-07-24 16:57:22 +0200132 * @hwnum: hardware number of the GPIO for this chip
133 *
134 * Returns:
Mauro Carvalho Chehab35c6cfb2020-03-17 15:54:21 +0100135 * A pointer to the GPIO descriptor or ``ERR_PTR(-EINVAL)`` if no GPIO exists
Thierry Reding950d55f52017-07-24 16:57:22 +0200136 * in the given chip for the specified hardware number.
Linus Walleijd468bf92013-09-24 11:54:38 +0200137 */
Linus Walleija0b66a72020-03-29 16:04:05 +0200138struct gpio_desc *gpiochip_get_desc(struct gpio_chip *gc,
Bartosz Golaszewski06863622019-12-24 13:06:59 +0100139 unsigned int hwnum)
Linus Walleijd468bf92013-09-24 11:54:38 +0200140{
Linus Walleija0b66a72020-03-29 16:04:05 +0200141 struct gpio_device *gdev = gc->gpiodev;
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100142
143 if (hwnum >= gdev->ngpio)
Alexandre Courbotb7d0a282013-12-03 12:31:11 +0900144 return ERR_PTR(-EINVAL);
Linus Walleijd468bf92013-09-24 11:54:38 +0200145
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100146 return &gdev->descs[hwnum];
Linus Walleijd468bf92013-09-24 11:54:38 +0200147}
Marco Felsch97795422020-02-25 10:31:02 +0100148EXPORT_SYMBOL_GPL(gpiochip_get_desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900149
150/**
Thierry Reding950d55f52017-07-24 16:57:22 +0200151 * desc_to_gpio - convert a GPIO descriptor to the integer namespace
152 * @desc: GPIO descriptor
153 *
Alexandre Courbot372e7222013-02-03 01:29:29 +0900154 * This should disappear in the future but is needed since we still
Thierry Reding950d55f52017-07-24 16:57:22 +0200155 * use GPIO numbers for error messages and sysfs nodes.
156 *
157 * Returns:
158 * The global GPIO number for the GPIO specified by its descriptor.
Alexandre Courbot372e7222013-02-03 01:29:29 +0900159 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700160int desc_to_gpio(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900161{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100162 return desc->gdev->base + (desc - &desc->gdev->descs[0]);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900163}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700164EXPORT_SYMBOL_GPL(desc_to_gpio);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900165
166
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700167/**
168 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
169 * @desc: descriptor to return the chip of
170 */
171struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900172{
Vladimir Zapolskiydd3b9a42017-12-21 18:37:30 +0200173 if (!desc || !desc->gdev)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100174 return NULL;
175 return desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900176}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700177EXPORT_SYMBOL_GPL(gpiod_to_chip);
David Brownelld2876d02008-02-04 22:28:20 -0800178
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700179/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
180static int gpiochip_find_base(int ngpio)
181{
Linus Walleijff2b1352015-10-20 11:10:38 +0200182 struct gpio_device *gdev;
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900183 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700184
Linus Walleijff2b1352015-10-20 11:10:38 +0200185 list_for_each_entry_reverse(gdev, &gpio_devices, list) {
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900186 /* found a free space? */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100187 if (gdev->base + gdev->ngpio <= base)
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900188 break;
189 else
190 /* nope, check the space right before the chip */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100191 base = gdev->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700192 }
193
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900194 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700195 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900196 return base;
197 } else {
198 pr_err("%s: cannot find free range\n", __func__);
199 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700200 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700201}
202
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700203/**
204 * gpiod_get_direction - return the current direction of a GPIO
205 * @desc: GPIO to get the direction of
206 *
Wolfram Sang94fc7302018-01-09 12:35:53 +0100207 * Returns 0 for output, 1 for input, or an error code in case of error.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700208 *
209 * This function may sleep if gpiod_cansleep() is true.
210 */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900211int gpiod_get_direction(struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300212{
Linus Walleija0b66a72020-03-29 16:04:05 +0200213 struct gpio_chip *gc;
Andy Shevchenko13daf482020-11-09 22:53:16 +0200214 unsigned int offset;
Linus Walleijd377f562019-07-16 11:11:45 +0200215 int ret;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300216
Linus Walleija0b66a72020-03-29 16:04:05 +0200217 gc = gpiod_to_chip(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900218 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300219
Russell King256efae2019-12-07 16:20:18 +0000220 /*
221 * Open drain emulation using input mode may incorrectly report
222 * input here, fix that up.
223 */
224 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) &&
225 test_bit(FLAG_IS_OUT, &desc->flags))
226 return 0;
227
Linus Walleija0b66a72020-03-29 16:04:05 +0200228 if (!gc->get_direction)
Wolfram Sangd0121b82018-07-11 18:33:19 +0200229 return -ENOTSUPP;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300230
Linus Walleija0b66a72020-03-29 16:04:05 +0200231 ret = gc->get_direction(gc, offset);
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +0200232 if (ret < 0)
233 return ret;
234
235 /* GPIOF_DIR_IN or other positive, otherwise GPIOF_DIR_OUT */
236 if (ret > 0)
Linus Walleijd377f562019-07-16 11:11:45 +0200237 ret = 1;
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +0200238
239 assign_bit(FLAG_IS_OUT, &desc->flags, !ret);
240
Linus Walleijd377f562019-07-16 11:11:45 +0200241 return ret;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300242}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700243EXPORT_SYMBOL_GPL(gpiod_get_direction);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300244
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900245/*
246 * Add a new chip to the global chips list, keeping the list of chips sorted
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800247 * by range(means [base, base + ngpio - 1]) order.
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900248 *
249 * Return -EBUSY if the new chip overlaps with some other chip's integer
250 * space.
251 */
Linus Walleijff2b1352015-10-20 11:10:38 +0200252static int gpiodev_add_to_list(struct gpio_device *gdev)
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900253{
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800254 struct gpio_device *prev, *next;
Linus Walleijff2b1352015-10-20 11:10:38 +0200255
256 if (list_empty(&gpio_devices)) {
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800257 /* initial entry in list */
Linus Walleijff2b1352015-10-20 11:10:38 +0200258 list_add_tail(&gdev->list, &gpio_devices);
Sudip Mukherjeee28ecca2015-12-27 19:06:50 +0530259 return 0;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900260 }
261
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800262 next = list_entry(gpio_devices.next, struct gpio_device, list);
263 if (gdev->base + gdev->ngpio <= next->base) {
264 /* add before first entry */
265 list_add(&gdev->list, &gpio_devices);
Julien Grossholtz96098df2016-01-07 16:46:45 -0500266 return 0;
267 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900268
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800269 prev = list_entry(gpio_devices.prev, struct gpio_device, list);
270 if (prev->base + prev->ngpio <= gdev->base) {
271 /* add behind last entry */
272 list_add_tail(&gdev->list, &gpio_devices);
273 return 0;
274 }
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800275
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800276 list_for_each_entry_safe(prev, next, &gpio_devices, list) {
277 /* at the end of the list */
278 if (&next->list == &gpio_devices)
279 break;
280
281 /* add between prev and next */
282 if (prev->base + prev->ngpio <= gdev->base
283 && gdev->base + gdev->ngpio <= next->base) {
284 list_add(&gdev->list, &prev->list);
285 return 0;
286 }
287 }
288
289 dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n");
290 return -EBUSY;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900291}
292
Thierry Reding950d55f52017-07-24 16:57:22 +0200293/*
Linus Walleijf881bab02015-09-23 16:20:43 -0700294 * Convert a GPIO name to its descriptor
Geert Uytterhoeven582838e2020-05-11 12:18:28 +0200295 * Note that there is no guarantee that GPIO names are globally unique!
296 * Hence this function will return, if it exists, a reference to the first GPIO
297 * line found that matches the given name.
Linus Walleijf881bab02015-09-23 16:20:43 -0700298 */
299static struct gpio_desc *gpio_name_to_desc(const char * const name)
300{
Linus Walleijff2b1352015-10-20 11:10:38 +0200301 struct gpio_device *gdev;
Linus Walleijf881bab02015-09-23 16:20:43 -0700302 unsigned long flags;
303
Michał Mirosławee203bb2020-03-15 17:34:34 +0100304 if (!name)
305 return NULL;
306
Linus Walleijf881bab02015-09-23 16:20:43 -0700307 spin_lock_irqsave(&gpio_lock, flags);
308
Linus Walleijff2b1352015-10-20 11:10:38 +0200309 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijf881bab02015-09-23 16:20:43 -0700310 int i;
311
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100312 for (i = 0; i != gdev->ngpio; ++i) {
313 struct gpio_desc *desc = &gdev->descs[i];
Linus Walleijf881bab02015-09-23 16:20:43 -0700314
Michał Mirosławee203bb2020-03-15 17:34:34 +0100315 if (!desc->name)
Linus Walleijf881bab02015-09-23 16:20:43 -0700316 continue;
317
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100318 if (!strcmp(desc->name, name)) {
Linus Walleijf881bab02015-09-23 16:20:43 -0700319 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100320 return desc;
Linus Walleijf881bab02015-09-23 16:20:43 -0700321 }
322 }
323 }
324
325 spin_unlock_irqrestore(&gpio_lock, flags);
326
327 return NULL;
328}
329
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200330/*
Geert Uytterhoeven582838e2020-05-11 12:18:28 +0200331 * Take the names from gc->names and assign them to their GPIO descriptors.
332 * Warn if a name is already used for a GPIO line on a different GPIO chip.
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200333 *
Geert Uytterhoeven582838e2020-05-11 12:18:28 +0200334 * Note that:
335 * 1. Non-unique names are still accepted,
336 * 2. Name collisions within the same GPIO chip are not reported.
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200337 */
338static int gpiochip_set_desc_names(struct gpio_chip *gc)
339{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100340 struct gpio_device *gdev = gc->gpiodev;
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200341 int i;
342
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200343 /* First check all names if they are unique */
344 for (i = 0; i != gc->ngpio; ++i) {
345 struct gpio_desc *gpio;
346
347 gpio = gpio_name_to_desc(gc->names[i]);
Linus Walleijf881bab02015-09-23 16:20:43 -0700348 if (gpio)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100349 dev_warn(&gdev->dev,
Linus Walleij34ffd852015-10-20 11:31:54 +0200350 "Detected name collision for GPIO name '%s'\n",
Linus Walleijf881bab02015-09-23 16:20:43 -0700351 gc->names[i]);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200352 }
353
354 /* Then add all names to the GPIO descriptors */
355 for (i = 0; i != gc->ngpio; ++i)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100356 gdev->descs[i].name = gc->names[i];
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200357
358 return 0;
359}
360
Bartosz Golaszewski32fc5aa2020-09-09 10:54:26 +0200361/*
362 * devprop_gpiochip_set_names - Set GPIO line names using device properties
363 * @chip: GPIO chip whose lines should be named, if possible
364 *
365 * Looks for device property "gpio-line-names" and if it exists assigns
366 * GPIO line names for the chip. The memory allocated for the assigned
367 * names belong to the underlying software node and should not be released
368 * by the caller.
369 */
370static int devprop_gpiochip_set_names(struct gpio_chip *chip)
371{
372 struct gpio_device *gdev = chip->gpiodev;
373 struct device *dev = chip->parent;
374 const char **names;
375 int ret, i;
376 int count;
377
Bartosz Golaszewski587823d2020-09-17 09:48:57 +0200378 /* GPIO chip may not have a parent device whose properties we inspect. */
379 if (!dev)
380 return 0;
381
Bartosz Golaszewski32fc5aa2020-09-09 10:54:26 +0200382 count = device_property_string_array_count(dev, "gpio-line-names");
383 if (count < 0)
384 return 0;
385
386 if (count > gdev->ngpio) {
387 dev_warn(&gdev->dev, "gpio-line-names is length %d but should be at most length %d",
388 count, gdev->ngpio);
389 count = gdev->ngpio;
390 }
391
392 names = kcalloc(count, sizeof(*names), GFP_KERNEL);
393 if (!names)
394 return -ENOMEM;
395
396 ret = device_property_read_string_array(dev, "gpio-line-names",
397 names, count);
398 if (ret < 0) {
399 dev_warn(&gdev->dev, "failed to read GPIO line names\n");
400 kfree(names);
401 return ret;
402 }
403
404 for (i = 0; i < count; i++)
405 gdev->descs[i].name = names[i];
406
407 kfree(names);
408
409 return 0;
410}
411
Linus Walleija0b66a72020-03-29 16:04:05 +0200412static unsigned long *gpiochip_allocate_mask(struct gpio_chip *gc)
Stephen Boyde4371f6e2018-03-23 09:34:50 -0700413{
414 unsigned long *p;
415
Linus Walleija0b66a72020-03-29 16:04:05 +0200416 p = bitmap_alloc(gc->ngpio, GFP_KERNEL);
Stephen Boyde4371f6e2018-03-23 09:34:50 -0700417 if (!p)
418 return NULL;
419
420 /* Assume by default all GPIOs are valid */
Linus Walleija0b66a72020-03-29 16:04:05 +0200421 bitmap_fill(p, gc->ngpio);
Stephen Boyde4371f6e2018-03-23 09:34:50 -0700422
423 return p;
424}
425
Linus Walleijf626d6d2019-07-17 09:10:01 +0200426static int gpiochip_alloc_valid_mask(struct gpio_chip *gc)
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700427{
Linus Walleijeb1e8bd2019-08-19 11:30:58 +0200428 if (!(of_gpio_need_valid_mask(gc) || gc->init_valid_mask))
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700429 return 0;
430
Linus Walleijf626d6d2019-07-17 09:10:01 +0200431 gc->valid_mask = gpiochip_allocate_mask(gc);
432 if (!gc->valid_mask)
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700433 return -ENOMEM;
434
435 return 0;
436}
437
Linus Walleijc9fc5af2019-08-19 10:49:04 +0200438static int gpiochip_init_valid_mask(struct gpio_chip *gc)
Ricardo Ribalda Delgadof8ec92a2018-10-05 08:52:58 +0200439{
Linus Walleijc9fc5af2019-08-19 10:49:04 +0200440 if (gc->init_valid_mask)
441 return gc->init_valid_mask(gc,
442 gc->valid_mask,
443 gc->ngpio);
Ricardo Ribalda Delgadof8ec92a2018-10-05 08:52:58 +0200444
445 return 0;
446}
447
Linus Walleija0b66a72020-03-29 16:04:05 +0200448static void gpiochip_free_valid_mask(struct gpio_chip *gc)
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700449{
Linus Walleija0b66a72020-03-29 16:04:05 +0200450 bitmap_free(gc->valid_mask);
451 gc->valid_mask = NULL;
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700452}
453
Andy Shevchenkob056ca12019-11-04 18:09:39 +0200454static int gpiochip_add_pin_ranges(struct gpio_chip *gc)
455{
456 if (gc->add_pin_ranges)
457 return gc->add_pin_ranges(gc);
458
459 return 0;
460}
461
Linus Walleija0b66a72020-03-29 16:04:05 +0200462bool gpiochip_line_is_valid(const struct gpio_chip *gc,
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700463 unsigned int offset)
464{
465 /* No mask means all valid */
Linus Walleija0b66a72020-03-29 16:04:05 +0200466 if (likely(!gc->valid_mask))
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700467 return true;
Linus Walleija0b66a72020-03-29 16:04:05 +0200468 return test_bit(offset, gc->valid_mask);
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700469}
470EXPORT_SYMBOL_GPL(gpiochip_line_is_valid);
471
Linus Walleijff2b1352015-10-20 11:10:38 +0200472static void gpiodevice_release(struct device *dev)
473{
474 struct gpio_device *gdev = dev_get_drvdata(dev);
475
476 list_del(&gdev->list);
Bartosz Golaszewski8d4a85b2020-09-08 15:12:25 +0200477 ida_free(&gpio_ida, gdev->id);
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +0100478 kfree_const(gdev->label);
Guenter Roeck476e2fc2016-03-31 08:11:29 -0700479 kfree(gdev->descs);
Linus Walleij9efd9e62016-02-09 14:27:42 +0100480 kfree(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +0200481}
482
Kent Gibson1f5eb8b2020-11-05 18:40:49 +0800483#ifdef CONFIG_GPIO_CDEV
484#define gcdev_register(gdev, devt) gpiolib_cdev_register((gdev), (devt))
485#define gcdev_unregister(gdev) gpiolib_cdev_unregister((gdev))
486#else
487/*
488 * gpiolib_cdev_register() indirectly calls device_add(), which is still
489 * required even when cdev is not selected.
490 */
491#define gcdev_register(gdev, devt) device_add(&(gdev)->dev)
492#define gcdev_unregister(gdev) device_del(&(gdev)->dev)
493#endif
494
Guenter Roeck159f3cd2016-03-31 08:11:30 -0700495static int gpiochip_setup_dev(struct gpio_device *gdev)
496{
Linus Walleijd377f562019-07-16 11:11:45 +0200497 int ret;
Guenter Roeck159f3cd2016-03-31 08:11:30 -0700498
Kent Gibson1f5eb8b2020-11-05 18:40:49 +0800499 ret = gcdev_register(gdev, gpio_devt);
Linus Walleijd377f562019-07-16 11:11:45 +0200500 if (ret)
501 return ret;
Logan Gunthorpe111379d2017-03-17 12:48:12 -0600502
Linus Walleijd377f562019-07-16 11:11:45 +0200503 ret = gpiochip_sysfs_register(gdev);
504 if (ret)
Guenter Roeck159f3cd2016-03-31 08:11:30 -0700505 goto err_remove_device;
506
507 /* From this point, the .release() function cleans up gpio_device */
508 gdev->dev.release = gpiodevice_release;
Geert Uytterhoeven262b9012020-04-24 16:14:32 +0200509 dev_dbg(&gdev->dev, "registered GPIOs %d to %d on %s\n", gdev->base,
510 gdev->base + gdev->ngpio - 1, gdev->chip->label ? : "generic");
Guenter Roeck159f3cd2016-03-31 08:11:30 -0700511
512 return 0;
513
514err_remove_device:
Kent Gibson1f5eb8b2020-11-05 18:40:49 +0800515 gcdev_unregister(gdev);
Linus Walleijd377f562019-07-16 11:11:45 +0200516 return ret;
Guenter Roeck159f3cd2016-03-31 08:11:30 -0700517}
518
Linus Walleija0b66a72020-03-29 16:04:05 +0200519static void gpiochip_machine_hog(struct gpio_chip *gc, struct gpiod_hog *hog)
Bartosz Golaszewskia411e812018-04-10 22:30:28 +0200520{
521 struct gpio_desc *desc;
522 int rv;
523
Linus Walleija0b66a72020-03-29 16:04:05 +0200524 desc = gpiochip_get_desc(gc, hog->chip_hwnum);
Bartosz Golaszewskia411e812018-04-10 22:30:28 +0200525 if (IS_ERR(desc)) {
Geert Uytterhoeven262b9012020-04-24 16:14:32 +0200526 chip_err(gc, "%s: unable to get GPIO desc: %ld\n", __func__,
527 PTR_ERR(desc));
Bartosz Golaszewskia411e812018-04-10 22:30:28 +0200528 return;
529 }
530
Dan Carpenterba3efdf2018-05-01 10:36:39 +0300531 if (test_bit(FLAG_IS_HOGGED, &desc->flags))
Bartosz Golaszewskia411e812018-04-10 22:30:28 +0200532 return;
533
534 rv = gpiod_hog(desc, hog->line_name, hog->lflags, hog->dflags);
535 if (rv)
Geert Uytterhoeven262b9012020-04-24 16:14:32 +0200536 gpiod_err(desc, "%s: unable to hog GPIO line (%s:%u): %d\n",
537 __func__, gc->label, hog->chip_hwnum, rv);
Bartosz Golaszewskia411e812018-04-10 22:30:28 +0200538}
539
Linus Walleija0b66a72020-03-29 16:04:05 +0200540static void machine_gpiochip_add(struct gpio_chip *gc)
Bartosz Golaszewskia411e812018-04-10 22:30:28 +0200541{
542 struct gpiod_hog *hog;
543
544 mutex_lock(&gpio_machine_hogs_mutex);
545
546 list_for_each_entry(hog, &gpio_machine_hogs, list) {
Linus Walleija0b66a72020-03-29 16:04:05 +0200547 if (!strcmp(gc->label, hog->chip_label))
548 gpiochip_machine_hog(gc, hog);
Bartosz Golaszewskia411e812018-04-10 22:30:28 +0200549 }
550
551 mutex_unlock(&gpio_machine_hogs_mutex);
552}
553
Guenter Roeck159f3cd2016-03-31 08:11:30 -0700554static void gpiochip_setup_devs(void)
555{
556 struct gpio_device *gdev;
Linus Walleijd377f562019-07-16 11:11:45 +0200557 int ret;
Guenter Roeck159f3cd2016-03-31 08:11:30 -0700558
559 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijd377f562019-07-16 11:11:45 +0200560 ret = gpiochip_setup_dev(gdev);
561 if (ret)
Geert Uytterhoeven262b9012020-04-24 16:14:32 +0200562 dev_err(&gdev->dev,
563 "Failed to initialize gpio device (%d)\n", ret);
Guenter Roeck159f3cd2016-03-31 08:11:30 -0700564 }
565}
566
Linus Walleija0b66a72020-03-29 16:04:05 +0200567int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
Andrew Lunn39c3fd52017-12-02 18:11:04 +0100568 struct lock_class_key *lock_key,
569 struct lock_class_key *request_key)
David Brownelld2876d02008-02-04 22:28:20 -0800570{
571 unsigned long flags;
Linus Walleijd377f562019-07-16 11:11:45 +0200572 int ret = 0;
Linus Walleijff2b1352015-10-20 11:10:38 +0200573 unsigned i;
Linus Walleija0b66a72020-03-29 16:04:05 +0200574 int base = gc->base;
Linus Walleijff2b1352015-10-20 11:10:38 +0200575 struct gpio_device *gdev;
David Brownelld2876d02008-02-04 22:28:20 -0800576
Linus Walleijff2b1352015-10-20 11:10:38 +0200577 /*
578 * First: allocate and populate the internal stat container, and
579 * set up the struct device.
580 */
Josh Cartwright969f07b2016-02-17 16:44:15 -0600581 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
Linus Walleijff2b1352015-10-20 11:10:38 +0200582 if (!gdev)
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900583 return -ENOMEM;
Linus Walleij3c702e92015-10-21 15:29:53 +0200584 gdev->dev.bus = &gpio_bus_type;
Linus Walleija0b66a72020-03-29 16:04:05 +0200585 gdev->chip = gc;
586 gc->gpiodev = gdev;
587 if (gc->parent) {
588 gdev->dev.parent = gc->parent;
589 gdev->dev.of_node = gc->parent->of_node;
Thierry Redingacc6e332016-07-05 14:11:14 +0200590 }
591
Linus Walleijff2b1352015-10-20 11:10:38 +0200592#ifdef CONFIG_OF_GPIO
593 /* If the gpiochip has an assigned OF node this takes precedence */
Linus Walleija0b66a72020-03-29 16:04:05 +0200594 if (gc->of_node)
595 gdev->dev.of_node = gc->of_node;
Biju Das6ff04972018-08-06 10:48:01 +0100596 else
Linus Walleija0b66a72020-03-29 16:04:05 +0200597 gc->of_node = gdev->dev.of_node;
Linus Walleijff2b1352015-10-20 11:10:38 +0200598#endif
Thierry Redingacc6e332016-07-05 14:11:14 +0200599
Bartosz Golaszewski8d4a85b2020-09-08 15:12:25 +0200600 gdev->id = ida_alloc(&gpio_ida, GFP_KERNEL);
Linus Walleijff2b1352015-10-20 11:10:38 +0200601 if (gdev->id < 0) {
Linus Walleijd377f562019-07-16 11:11:45 +0200602 ret = gdev->id;
Linus Walleijff2b1352015-10-20 11:10:38 +0200603 goto err_free_gdev;
604 }
Geert Uytterhoevenddd88912019-11-27 09:42:47 +0100605 dev_set_name(&gdev->dev, GPIOCHIP_NAME "%d", gdev->id);
Linus Walleijff2b1352015-10-20 11:10:38 +0200606 device_initialize(&gdev->dev);
607 dev_set_drvdata(&gdev->dev, gdev);
Linus Walleija0b66a72020-03-29 16:04:05 +0200608 if (gc->parent && gc->parent->driver)
609 gdev->owner = gc->parent->driver->owner;
610 else if (gc->owner)
Linus Walleijff2b1352015-10-20 11:10:38 +0200611 /* TODO: remove chip->owner */
Linus Walleija0b66a72020-03-29 16:04:05 +0200612 gdev->owner = gc->owner;
Linus Walleijff2b1352015-10-20 11:10:38 +0200613 else
614 gdev->owner = THIS_MODULE;
David Brownelld2876d02008-02-04 22:28:20 -0800615
Linus Walleija0b66a72020-03-29 16:04:05 +0200616 gdev->descs = kcalloc(gc->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100617 if (!gdev->descs) {
Linus Walleijd377f562019-07-16 11:11:45 +0200618 ret = -ENOMEM;
Vladimir Zapolskiya05a1402018-11-02 15:39:43 +0200619 goto err_free_ida;
Linus Walleijff2b1352015-10-20 11:10:38 +0200620 }
621
Linus Walleija0b66a72020-03-29 16:04:05 +0200622 if (gc->ngpio == 0) {
623 chip_err(gc, "tried to insert a GPIO chip with zero lines\n");
Linus Walleijd377f562019-07-16 11:11:45 +0200624 ret = -EINVAL;
Guenter Roeck159f3cd2016-03-31 08:11:30 -0700625 goto err_free_descs;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +0800626 }
Linus Walleijdf4878e2016-02-12 14:48:23 +0100627
Linus Walleija0b66a72020-03-29 16:04:05 +0200628 if (gc->ngpio > FASTPATH_NGPIO)
629 chip_warn(gc, "line cnt %u is greater than fast path cnt %u\n",
630 gc->ngpio, FASTPATH_NGPIO);
Laura Abbott30277432018-05-21 10:57:07 -0700631
Linus Walleija0b66a72020-03-29 16:04:05 +0200632 gdev->label = kstrdup_const(gc->label ?: "unknown", GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +0100633 if (!gdev->label) {
Linus Walleijd377f562019-07-16 11:11:45 +0200634 ret = -ENOMEM;
Guenter Roeck476e2fc2016-03-31 08:11:29 -0700635 goto err_free_descs;
Linus Walleijdf4878e2016-02-12 14:48:23 +0100636 }
637
Linus Walleija0b66a72020-03-29 16:04:05 +0200638 gdev->ngpio = gc->ngpio;
Linus Walleij43c54ec2016-02-11 11:37:48 +0100639 gdev->data = data;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +0800640
David Brownelld2876d02008-02-04 22:28:20 -0800641 spin_lock_irqsave(&gpio_lock, flags);
642
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100643 /*
644 * TODO: this allocates a Linux GPIO number base in the global
645 * GPIO numberspace for this chip. In the long run we want to
646 * get *rid* of this numberspace and use only descriptors, but
647 * it may be a pipe dream. It will not happen before we get rid
648 * of the sysfs interface anyways.
649 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700650 if (base < 0) {
Linus Walleija0b66a72020-03-29 16:04:05 +0200651 base = gpiochip_find_base(gc->ngpio);
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700652 if (base < 0) {
Linus Walleijd377f562019-07-16 11:11:45 +0200653 ret = base;
Johan Hovold225fce82015-01-12 17:12:25 +0100654 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -0700655 goto err_free_label;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700656 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100657 /*
658 * TODO: it should not be necessary to reflect the assigned
659 * base outside of the GPIO subsystem. Go over drivers and
660 * see if anyone makes use of this, else drop this and assign
661 * a poison instead.
662 */
Linus Walleija0b66a72020-03-29 16:04:05 +0200663 gc->base = base;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700664 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100665 gdev->base = base;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700666
Linus Walleijd377f562019-07-16 11:11:45 +0200667 ret = gpiodev_add_to_list(gdev);
668 if (ret) {
Johan Hovold05aa5202015-01-12 17:12:26 +0100669 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -0700670 goto err_free_label;
Johan Hovold05aa5202015-01-12 17:12:26 +0100671 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900672
Linus Walleija0b66a72020-03-29 16:04:05 +0200673 for (i = 0; i < gc->ngpio; i++)
Ricardo Ribalda Delgado767cd172018-10-12 08:11:36 +0200674 gdev->descs[i].gdev = gdev;
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900675
Dan Callaghan207270d2020-01-21 10:12:17 +1000676 spin_unlock_irqrestore(&gpio_lock, flags);
677
Kent Gibson6accc372020-07-08 12:15:51 +0800678 BLOCKING_INIT_NOTIFIER_HEAD(&gdev->notifier);
Bartosz Golaszewski51c10642019-11-22 15:19:21 +0100679
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530680#ifdef CONFIG_PINCTRL
Linus Walleij20ec3e32016-02-11 11:03:06 +0100681 INIT_LIST_HEAD(&gdev->pin_ranges);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530682#endif
683
Bartosz Golaszewski7cba1a42020-09-09 10:54:25 +0200684 if (gc->names)
685 ret = gpiochip_set_desc_names(gc);
686 else
687 ret = devprop_gpiochip_set_names(gc);
Linus Walleijd377f562019-07-16 11:11:45 +0200688 if (ret)
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200689 goto err_remove_from_list;
690
Linus Walleija0b66a72020-03-29 16:04:05 +0200691 ret = gpiochip_alloc_valid_mask(gc);
Linus Walleijd377f562019-07-16 11:11:45 +0200692 if (ret)
Mika Westerberg79b804c2016-09-20 15:15:21 +0300693 goto err_remove_from_list;
694
Linus Walleija0b66a72020-03-29 16:04:05 +0200695 ret = of_gpiochip_add(gc);
Linus Walleijd377f562019-07-16 11:11:45 +0200696 if (ret)
Linus Walleij48057ed2019-08-20 10:05:27 +0200697 goto err_free_gpiochip_mask;
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200698
Linus Walleija0b66a72020-03-29 16:04:05 +0200699 ret = gpiochip_init_valid_mask(gc);
Linus Walleijd377f562019-07-16 11:11:45 +0200700 if (ret)
Geert Uytterhoeven35779892019-04-24 15:59:33 +0200701 goto err_remove_of_chip;
Ricardo Ribalda Delgadof8ec92a2018-10-05 08:52:58 +0200702
Linus Walleija0b66a72020-03-29 16:04:05 +0200703 for (i = 0; i < gc->ngpio; i++) {
Ricardo Ribalda Delgado3edfb7b2018-10-05 08:53:00 +0200704 struct gpio_desc *desc = &gdev->descs[i];
705
Linus Walleija0b66a72020-03-29 16:04:05 +0200706 if (gc->get_direction && gpiochip_line_is_valid(gc, i)) {
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +0200707 assign_bit(FLAG_IS_OUT,
Linus Walleija0b66a72020-03-29 16:04:05 +0200708 &desc->flags, !gc->get_direction(gc, i));
Chris Packhamd95da992019-07-08 08:35:58 +1200709 } else {
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +0200710 assign_bit(FLAG_IS_OUT,
Linus Walleija0b66a72020-03-29 16:04:05 +0200711 &desc->flags, !gc->direction_input);
Chris Packhamd95da992019-07-08 08:35:58 +1200712 }
Ricardo Ribalda Delgado3edfb7b2018-10-05 08:53:00 +0200713 }
714
Linus Walleija0b66a72020-03-29 16:04:05 +0200715 ret = gpiochip_add_pin_ranges(gc);
Andy Shevchenkob056ca12019-11-04 18:09:39 +0200716 if (ret)
717 goto err_remove_of_chip;
718
Linus Walleija0b66a72020-03-29 16:04:05 +0200719 acpi_gpiochip_add(gc);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600720
Linus Walleija0b66a72020-03-29 16:04:05 +0200721 machine_gpiochip_add(gc);
Bartosz Golaszewskia411e812018-04-10 22:30:28 +0200722
Linus Walleija0b66a72020-03-29 16:04:05 +0200723 ret = gpiochip_irqchip_init_valid_mask(gc);
Andy Shevchenko9411e3a2019-10-09 17:34:44 +0300724 if (ret)
725 goto err_remove_acpi_chip;
726
Linus Walleija0b66a72020-03-29 16:04:05 +0200727 ret = gpiochip_irqchip_init_hw(gc);
Linus Walleijfbdf8d42019-09-06 12:05:35 +0200728 if (ret)
Linus Walleij48057ed2019-08-20 10:05:27 +0200729 goto err_remove_acpi_chip;
730
Linus Walleija0b66a72020-03-29 16:04:05 +0200731 ret = gpiochip_add_irqchip(gc, lock_key, request_key);
Linus Walleijfbdf8d42019-09-06 12:05:35 +0200732 if (ret)
Linus Walleij48057ed2019-08-20 10:05:27 +0200733 goto err_remove_irqchip_mask;
734
Linus Walleij3c702e92015-10-21 15:29:53 +0200735 /*
736 * By first adding the chardev, and then adding the device,
737 * we get a device node entry in sysfs under
738 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
739 * coldplug of device nodes and other udev business.
Guenter Roeck159f3cd2016-03-31 08:11:30 -0700740 * We can do this only if gpiolib has been initialized.
741 * Otherwise, defer until later.
Linus Walleij3c702e92015-10-21 15:29:53 +0200742 */
Guenter Roeck159f3cd2016-03-31 08:11:30 -0700743 if (gpiolib_initialized) {
Linus Walleijd377f562019-07-16 11:11:45 +0200744 ret = gpiochip_setup_dev(gdev);
745 if (ret)
Linus Walleij48057ed2019-08-20 10:05:27 +0200746 goto err_remove_irqchip;
Guenter Roeck159f3cd2016-03-31 08:11:30 -0700747 }
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600748 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800749
Linus Walleij48057ed2019-08-20 10:05:27 +0200750err_remove_irqchip:
Linus Walleija0b66a72020-03-29 16:04:05 +0200751 gpiochip_irqchip_remove(gc);
Linus Walleij48057ed2019-08-20 10:05:27 +0200752err_remove_irqchip_mask:
Linus Walleija0b66a72020-03-29 16:04:05 +0200753 gpiochip_irqchip_free_valid_mask(gc);
Geert Uytterhoeven35779892019-04-24 15:59:33 +0200754err_remove_acpi_chip:
Linus Walleija0b66a72020-03-29 16:04:05 +0200755 acpi_gpiochip_remove(gc);
Geert Uytterhoeven35779892019-04-24 15:59:33 +0200756err_remove_of_chip:
Linus Walleija0b66a72020-03-29 16:04:05 +0200757 gpiochip_free_hogs(gc);
758 of_gpiochip_remove(gc);
Geert Uytterhoeven35779892019-04-24 15:59:33 +0200759err_free_gpiochip_mask:
Linus Walleija0b66a72020-03-29 16:04:05 +0200760 gpiochip_remove_pin_ranges(gc);
761 gpiochip_free_valid_mask(gc);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200762err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +0100763 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +0200764 list_del(&gdev->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800765 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -0700766err_free_label:
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +0100767 kfree_const(gdev->label);
Guenter Roeck476e2fc2016-03-31 08:11:29 -0700768err_free_descs:
769 kfree(gdev->descs);
Vladimir Zapolskiya05a1402018-11-02 15:39:43 +0200770err_free_ida:
Bartosz Golaszewski8d4a85b2020-09-08 15:12:25 +0200771 ida_free(&gpio_ida, gdev->id);
Vladimir Zapolskiya05a1402018-11-02 15:39:43 +0200772err_free_gdev:
David Brownelld2876d02008-02-04 22:28:20 -0800773 /* failures here can mean systems won't boot... */
Marcel Ziswiler1777fc92018-07-20 09:54:49 +0200774 pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__,
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100775 gdev->base, gdev->base + gdev->ngpio - 1,
Linus Walleija0b66a72020-03-29 16:04:05 +0200776 gc->label ? : "generic", ret);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100777 kfree(gdev);
Linus Walleijd377f562019-07-16 11:11:45 +0200778 return ret;
David Brownelld2876d02008-02-04 22:28:20 -0800779}
Thierry Reding959bc7b2017-11-07 19:15:59 +0100780EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);
David Brownelld2876d02008-02-04 22:28:20 -0800781
782/**
Linus Walleij43c54ec2016-02-11 11:37:48 +0100783 * gpiochip_get_data() - get per-subdriver data for the chip
Linus Walleija0b66a72020-03-29 16:04:05 +0200784 * @gc: GPIO chip
Thierry Reding950d55f52017-07-24 16:57:22 +0200785 *
786 * Returns:
787 * The per-subdriver data for the chip.
Linus Walleij43c54ec2016-02-11 11:37:48 +0100788 */
Linus Walleija0b66a72020-03-29 16:04:05 +0200789void *gpiochip_get_data(struct gpio_chip *gc)
Linus Walleij43c54ec2016-02-11 11:37:48 +0100790{
Linus Walleija0b66a72020-03-29 16:04:05 +0200791 return gc->gpiodev->data;
Linus Walleij43c54ec2016-02-11 11:37:48 +0100792}
793EXPORT_SYMBOL_GPL(gpiochip_get_data);
794
795/**
David Brownelld2876d02008-02-04 22:28:20 -0800796 * gpiochip_remove() - unregister a gpio_chip
Linus Walleija0b66a72020-03-29 16:04:05 +0200797 * @gc: the chip to unregister
David Brownelld2876d02008-02-04 22:28:20 -0800798 *
799 * A gpio_chip with any GPIOs still requested may not be removed.
800 */
Linus Walleija0b66a72020-03-29 16:04:05 +0200801void gpiochip_remove(struct gpio_chip *gc)
David Brownelld2876d02008-02-04 22:28:20 -0800802{
Linus Walleija0b66a72020-03-29 16:04:05 +0200803 struct gpio_device *gdev = gc->gpiodev;
David Brownelld2876d02008-02-04 22:28:20 -0800804 unsigned long flags;
Andy Shevchenko869233f2020-02-25 13:47:25 +0200805 unsigned int i;
David Brownelld2876d02008-02-04 22:28:20 -0800806
Linus Walleijff2b1352015-10-20 11:10:38 +0200807 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
Linus Walleijafbc4f32016-02-09 13:21:06 +0100808 gpiochip_sysfs_unregister(gdev);
Linus Walleija0b66a72020-03-29 16:04:05 +0200809 gpiochip_free_hogs(gc);
Bamvor Jian Zhangbd203bd2016-02-20 13:13:19 +0800810 /* Numb the device, cancelling all outstanding operations */
811 gdev->chip = NULL;
Linus Walleija0b66a72020-03-29 16:04:05 +0200812 gpiochip_irqchip_remove(gc);
813 acpi_gpiochip_remove(gc);
814 of_gpiochip_remove(gc);
815 gpiochip_remove_pin_ranges(gc);
816 gpiochip_free_valid_mask(gc);
Linus Walleij43c54ec2016-02-11 11:37:48 +0100817 /*
818 * We accept no more calls into the driver from this point, so
819 * NULL the driver data pointer
820 */
821 gdev->data = NULL;
Anton Vorontsov391c9702010-06-08 07:48:17 -0600822
Johan Hovold6798aca2015-01-12 17:12:28 +0100823 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100824 for (i = 0; i < gdev->ngpio; i++) {
Linus Walleija0b66a72020-03-29 16:04:05 +0200825 if (gpiochip_is_requested(gc, i))
Andy Shevchenko869233f2020-02-25 13:47:25 +0200826 break;
David Brownelld2876d02008-02-04 22:28:20 -0800827 }
David Brownelld2876d02008-02-04 22:28:20 -0800828 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900829
Geert Uytterhoevenca18a852020-03-02 09:24:48 +0100830 if (i != gdev->ngpio)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100831 dev_crit(&gdev->dev,
Linus Walleij58383c782015-11-04 09:56:26 +0100832 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +0200833
Linus Walleijff2b1352015-10-20 11:10:38 +0200834 /*
835 * The gpiochip side puts its use of the device to rest here:
836 * if there are no userspace clients, the chardev and device will
837 * be removed, else it will be dangling until the last user is
838 * gone.
839 */
Kent Gibson1f5eb8b2020-11-05 18:40:49 +0800840 gcdev_unregister(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +0200841 put_device(&gdev->dev);
David Brownelld2876d02008-02-04 22:28:20 -0800842}
843EXPORT_SYMBOL_GPL(gpiochip_remove);
844
Laxman Dewangan0cf32922016-02-15 16:32:09 +0530845/**
Grant Likely594fa262010-06-08 07:48:16 -0600846 * gpiochip_find() - iterator for locating a specific gpio_chip
847 * @data: data to pass to match function
Thierry Reding950d55f52017-07-24 16:57:22 +0200848 * @match: Callback function to check gpio_chip
Grant Likely594fa262010-06-08 07:48:16 -0600849 *
850 * Similar to bus_find_device. It returns a reference to a gpio_chip as
851 * determined by a user supplied @match callback. The callback should return
852 * 0 if the device doesn't match and non-zero if it does. If the callback is
853 * non-zero, this function will return to the caller and not iterate over any
854 * more gpio_chips.
855 */
Grant Likely07ce8ec2012-05-18 23:01:05 -0600856struct gpio_chip *gpiochip_find(void *data,
Linus Walleija0b66a72020-03-29 16:04:05 +0200857 int (*match)(struct gpio_chip *gc,
Grant Likely3d0f7cf2012-05-17 13:54:40 -0600858 void *data))
Grant Likely594fa262010-06-08 07:48:16 -0600859{
Linus Walleijff2b1352015-10-20 11:10:38 +0200860 struct gpio_device *gdev;
Linus Walleija0b66a72020-03-29 16:04:05 +0200861 struct gpio_chip *gc = NULL;
Grant Likely594fa262010-06-08 07:48:16 -0600862 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -0600863
864 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +0200865 list_for_each_entry(gdev, &gpio_devices, list)
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +0900866 if (gdev->chip && match(gdev->chip, data)) {
Linus Walleija0b66a72020-03-29 16:04:05 +0200867 gc = gdev->chip;
Grant Likely594fa262010-06-08 07:48:16 -0600868 break;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +0900869 }
Linus Walleijff2b1352015-10-20 11:10:38 +0200870
Grant Likely594fa262010-06-08 07:48:16 -0600871 spin_unlock_irqrestore(&gpio_lock, flags);
872
Linus Walleija0b66a72020-03-29 16:04:05 +0200873 return gc;
Grant Likely594fa262010-06-08 07:48:16 -0600874}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -0600875EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -0800876
Linus Walleija0b66a72020-03-29 16:04:05 +0200877static int gpiochip_match_name(struct gpio_chip *gc, void *data)
Alexandre Courbot79697ef2013-11-16 21:39:32 +0900878{
879 const char *name = data;
880
Linus Walleija0b66a72020-03-29 16:04:05 +0200881 return !strcmp(gc->label, name);
Alexandre Courbot79697ef2013-11-16 21:39:32 +0900882}
883
884static struct gpio_chip *find_chip_by_name(const char *name)
885{
886 return gpiochip_find((void *)name, gpiochip_match_name);
887}
888
Linus Walleij14250522014-03-25 10:40:18 +0100889#ifdef CONFIG_GPIOLIB_IRQCHIP
890
891/*
892 * The following is irqchip helper code for gpiochips.
893 */
894
Andy Shevchenko9411e3a2019-10-09 17:34:44 +0300895static int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
896{
897 struct gpio_irq_chip *girq = &gc->irq;
898
899 if (!girq->init_hw)
900 return 0;
901
902 return girq->init_hw(gc);
903}
904
Linus Walleij5fbe5b52019-09-04 16:01:04 +0200905static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
Mika Westerberg79b804c2016-09-20 15:15:21 +0300906{
Linus Walleij5fbe5b52019-09-04 16:01:04 +0200907 struct gpio_irq_chip *girq = &gc->irq;
908
909 if (!girq->init_valid_mask)
Mika Westerberg79b804c2016-09-20 15:15:21 +0300910 return 0;
911
Linus Walleij5fbe5b52019-09-04 16:01:04 +0200912 girq->valid_mask = gpiochip_allocate_mask(gc);
913 if (!girq->valid_mask)
Mika Westerberg79b804c2016-09-20 15:15:21 +0300914 return -ENOMEM;
915
Linus Walleij5fbe5b52019-09-04 16:01:04 +0200916 girq->init_valid_mask(gc, girq->valid_mask, gc->ngpio);
917
Mika Westerberg79b804c2016-09-20 15:15:21 +0300918 return 0;
919}
920
Linus Walleija0b66a72020-03-29 16:04:05 +0200921static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc)
Mika Westerberg79b804c2016-09-20 15:15:21 +0300922{
Linus Walleija0b66a72020-03-29 16:04:05 +0200923 bitmap_free(gc->irq.valid_mask);
924 gc->irq.valid_mask = NULL;
Mika Westerberg79b804c2016-09-20 15:15:21 +0300925}
926
Linus Walleija0b66a72020-03-29 16:04:05 +0200927bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gc,
Stephen Boyd64ff2c82018-01-09 17:58:46 -0800928 unsigned int offset)
Mika Westerberg79b804c2016-09-20 15:15:21 +0300929{
Linus Walleija0b66a72020-03-29 16:04:05 +0200930 if (!gpiochip_line_is_valid(gc, offset))
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700931 return false;
Mika Westerberg79b804c2016-09-20 15:15:21 +0300932 /* No mask means all valid */
Linus Walleija0b66a72020-03-29 16:04:05 +0200933 if (likely(!gc->irq.valid_mask))
Mika Westerberg79b804c2016-09-20 15:15:21 +0300934 return true;
Linus Walleija0b66a72020-03-29 16:04:05 +0200935 return test_bit(offset, gc->irq.valid_mask);
Mika Westerberg79b804c2016-09-20 15:15:21 +0300936}
Stephen Boyd64ff2c82018-01-09 17:58:46 -0800937EXPORT_SYMBOL_GPL(gpiochip_irqchip_irq_valid);
Mika Westerberg79b804c2016-09-20 15:15:21 +0300938
Linus Walleijfdd61a02019-08-08 14:32:37 +0200939#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
940
941/**
942 * gpiochip_set_hierarchical_irqchip() - connects a hierarchical irqchip
943 * to a gpiochip
944 * @gc: the gpiochip to set the irqchip hierarchical handler to
945 * @irqchip: the irqchip to handle this level of the hierarchy, the interrupt
946 * will then percolate up to the parent
947 */
948static void gpiochip_set_hierarchical_irqchip(struct gpio_chip *gc,
949 struct irq_chip *irqchip)
950{
951 /* DT will deal with mapping each IRQ as we go along */
952 if (is_of_node(gc->irq.fwnode))
953 return;
954
955 /*
956 * This is for legacy and boardfile "irqchip" fwnodes: allocate
957 * irqs upfront instead of dynamically since we don't have the
958 * dynamic type of allocation that hardware description languages
959 * provide. Once all GPIO drivers using board files are gone from
960 * the kernel we can delete this code, but for a transitional period
961 * it is necessary to keep this around.
962 */
963 if (is_fwnode_irqchip(gc->irq.fwnode)) {
964 int i;
965 int ret;
966
967 for (i = 0; i < gc->ngpio; i++) {
968 struct irq_fwspec fwspec;
969 unsigned int parent_hwirq;
970 unsigned int parent_type;
971 struct gpio_irq_chip *girq = &gc->irq;
972
973 /*
974 * We call the child to parent translation function
975 * only to check if the child IRQ is valid or not.
976 * Just pick the rising edge type here as that is what
977 * we likely need to support.
978 */
979 ret = girq->child_to_parent_hwirq(gc, i,
980 IRQ_TYPE_EDGE_RISING,
981 &parent_hwirq,
982 &parent_type);
983 if (ret) {
984 chip_err(gc, "skip set-up on hwirq %d\n",
985 i);
986 continue;
987 }
988
989 fwspec.fwnode = gc->irq.fwnode;
990 /* This is the hwirq for the GPIO line side of things */
991 fwspec.param[0] = girq->child_offset_to_irq(gc, i);
992 /* Just pick something */
993 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
994 fwspec.param_count = 2;
995 ret = __irq_domain_alloc_irqs(gc->irq.domain,
996 /* just pick something */
997 -1,
998 1,
999 NUMA_NO_NODE,
1000 &fwspec,
1001 false,
1002 NULL);
1003 if (ret < 0) {
1004 chip_err(gc,
1005 "can not allocate irq for GPIO line %d parent hwirq %d in hierarchy domain: %d\n",
1006 i, parent_hwirq,
1007 ret);
1008 }
1009 }
1010 }
1011
1012 chip_err(gc, "%s unknown fwnode type proceed anyway\n", __func__);
1013
1014 return;
1015}
1016
1017static int gpiochip_hierarchy_irq_domain_translate(struct irq_domain *d,
1018 struct irq_fwspec *fwspec,
1019 unsigned long *hwirq,
1020 unsigned int *type)
1021{
1022 /* We support standard DT translation */
1023 if (is_of_node(fwspec->fwnode) && fwspec->param_count == 2) {
1024 return irq_domain_translate_twocell(d, fwspec, hwirq, type);
1025 }
1026
1027 /* This is for board files and others not using DT */
1028 if (is_fwnode_irqchip(fwspec->fwnode)) {
1029 int ret;
1030
1031 ret = irq_domain_translate_twocell(d, fwspec, hwirq, type);
1032 if (ret)
1033 return ret;
1034 WARN_ON(*type == IRQ_TYPE_NONE);
1035 return 0;
1036 }
1037 return -EINVAL;
1038}
1039
1040static int gpiochip_hierarchy_irq_domain_alloc(struct irq_domain *d,
1041 unsigned int irq,
1042 unsigned int nr_irqs,
1043 void *data)
1044{
1045 struct gpio_chip *gc = d->host_data;
1046 irq_hw_number_t hwirq;
1047 unsigned int type = IRQ_TYPE_NONE;
1048 struct irq_fwspec *fwspec = data;
Kevin Hao24258762020-01-14 16:28:19 +08001049 void *parent_arg;
Linus Walleijfdd61a02019-08-08 14:32:37 +02001050 unsigned int parent_hwirq;
1051 unsigned int parent_type;
1052 struct gpio_irq_chip *girq = &gc->irq;
1053 int ret;
1054
1055 /*
1056 * The nr_irqs parameter is always one except for PCI multi-MSI
1057 * so this should not happen.
1058 */
1059 WARN_ON(nr_irqs != 1);
1060
1061 ret = gc->irq.child_irq_domain_ops.translate(d, fwspec, &hwirq, &type);
1062 if (ret)
1063 return ret;
1064
Kevin Hao366950e2020-01-20 17:56:25 +08001065 chip_dbg(gc, "allocate IRQ %d, hwirq %lu\n", irq, hwirq);
Linus Walleijfdd61a02019-08-08 14:32:37 +02001066
1067 ret = girq->child_to_parent_hwirq(gc, hwirq, type,
1068 &parent_hwirq, &parent_type);
1069 if (ret) {
1070 chip_err(gc, "can't look up hwirq %lu\n", hwirq);
1071 return ret;
1072 }
Kevin Hao366950e2020-01-20 17:56:25 +08001073 chip_dbg(gc, "found parent hwirq %u\n", parent_hwirq);
Linus Walleijfdd61a02019-08-08 14:32:37 +02001074
1075 /*
1076 * We set handle_bad_irq because the .set_type() should
1077 * always be invoked and set the right type of handler.
1078 */
1079 irq_domain_set_info(d,
1080 irq,
1081 hwirq,
1082 gc->irq.chip,
1083 gc,
1084 girq->handler,
1085 NULL, NULL);
1086 irq_set_probe(irq);
1087
Linus Walleijfdd61a02019-08-08 14:32:37 +02001088 /* This parent only handles asserted level IRQs */
Kevin Hao24258762020-01-14 16:28:19 +08001089 parent_arg = girq->populate_parent_alloc_arg(gc, parent_hwirq, parent_type);
1090 if (!parent_arg)
1091 return -ENOMEM;
1092
Kevin Hao366950e2020-01-20 17:56:25 +08001093 chip_dbg(gc, "alloc_irqs_parent for %d parent hwirq %d\n",
Linus Walleijfdd61a02019-08-08 14:32:37 +02001094 irq, parent_hwirq);
Stephen Boydc34f6dc2020-01-14 15:11:03 -08001095 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key);
Kevin Hao24258762020-01-14 16:28:19 +08001096 ret = irq_domain_alloc_irqs_parent(d, irq, 1, parent_arg);
Kevin Hao880b7cf2020-01-14 16:28:20 +08001097 /*
1098 * If the parent irqdomain is msi, the interrupts have already
1099 * been allocated, so the EEXIST is good.
1100 */
1101 if (irq_domain_is_msi(d->parent) && (ret == -EEXIST))
1102 ret = 0;
Linus Walleijfdd61a02019-08-08 14:32:37 +02001103 if (ret)
1104 chip_err(gc,
1105 "failed to allocate parent hwirq %d for hwirq %lu\n",
1106 parent_hwirq, hwirq);
1107
Kevin Hao24258762020-01-14 16:28:19 +08001108 kfree(parent_arg);
Linus Walleijfdd61a02019-08-08 14:32:37 +02001109 return ret;
1110}
1111
Linus Walleija0b66a72020-03-29 16:04:05 +02001112static unsigned int gpiochip_child_offset_to_irq_noop(struct gpio_chip *gc,
Linus Walleijfdd61a02019-08-08 14:32:37 +02001113 unsigned int offset)
1114{
1115 return offset;
1116}
1117
1118static void gpiochip_hierarchy_setup_domain_ops(struct irq_domain_ops *ops)
1119{
1120 ops->activate = gpiochip_irq_domain_activate;
1121 ops->deactivate = gpiochip_irq_domain_deactivate;
1122 ops->alloc = gpiochip_hierarchy_irq_domain_alloc;
1123 ops->free = irq_domain_free_irqs_common;
1124
1125 /*
1126 * We only allow overriding the translate() function for
1127 * hierarchical chips, and this should only be done if the user
1128 * really need something other than 1:1 translation.
1129 */
1130 if (!ops->translate)
1131 ops->translate = gpiochip_hierarchy_irq_domain_translate;
1132}
1133
1134static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
1135{
1136 if (!gc->irq.child_to_parent_hwirq ||
1137 !gc->irq.fwnode) {
1138 chip_err(gc, "missing irqdomain vital data\n");
1139 return -EINVAL;
1140 }
1141
1142 if (!gc->irq.child_offset_to_irq)
1143 gc->irq.child_offset_to_irq = gpiochip_child_offset_to_irq_noop;
1144
Kevin Hao24258762020-01-14 16:28:19 +08001145 if (!gc->irq.populate_parent_alloc_arg)
1146 gc->irq.populate_parent_alloc_arg =
Linus Walleijfdd61a02019-08-08 14:32:37 +02001147 gpiochip_populate_parent_fwspec_twocell;
1148
1149 gpiochip_hierarchy_setup_domain_ops(&gc->irq.child_irq_domain_ops);
1150
1151 gc->irq.domain = irq_domain_create_hierarchy(
1152 gc->irq.parent_domain,
1153 0,
1154 gc->ngpio,
1155 gc->irq.fwnode,
1156 &gc->irq.child_irq_domain_ops,
1157 gc);
1158
1159 if (!gc->irq.domain)
1160 return -ENOMEM;
1161
1162 gpiochip_set_hierarchical_irqchip(gc, gc->irq.chip);
1163
1164 return 0;
1165}
1166
1167static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
1168{
1169 return !!gc->irq.parent_domain;
1170}
1171
Linus Walleija0b66a72020-03-29 16:04:05 +02001172void *gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *gc,
Linus Walleijfdd61a02019-08-08 14:32:37 +02001173 unsigned int parent_hwirq,
1174 unsigned int parent_type)
1175{
Kevin Hao24258762020-01-14 16:28:19 +08001176 struct irq_fwspec *fwspec;
1177
1178 fwspec = kmalloc(sizeof(*fwspec), GFP_KERNEL);
1179 if (!fwspec)
1180 return NULL;
1181
Linus Walleija0b66a72020-03-29 16:04:05 +02001182 fwspec->fwnode = gc->irq.parent_domain->fwnode;
Linus Walleijfdd61a02019-08-08 14:32:37 +02001183 fwspec->param_count = 2;
1184 fwspec->param[0] = parent_hwirq;
1185 fwspec->param[1] = parent_type;
Kevin Hao24258762020-01-14 16:28:19 +08001186
1187 return fwspec;
Linus Walleijfdd61a02019-08-08 14:32:37 +02001188}
1189EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_twocell);
1190
Linus Walleija0b66a72020-03-29 16:04:05 +02001191void *gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *gc,
Linus Walleijfdd61a02019-08-08 14:32:37 +02001192 unsigned int parent_hwirq,
1193 unsigned int parent_type)
1194{
Kevin Hao24258762020-01-14 16:28:19 +08001195 struct irq_fwspec *fwspec;
1196
1197 fwspec = kmalloc(sizeof(*fwspec), GFP_KERNEL);
1198 if (!fwspec)
1199 return NULL;
1200
Linus Walleija0b66a72020-03-29 16:04:05 +02001201 fwspec->fwnode = gc->irq.parent_domain->fwnode;
Linus Walleijfdd61a02019-08-08 14:32:37 +02001202 fwspec->param_count = 4;
1203 fwspec->param[0] = 0;
1204 fwspec->param[1] = parent_hwirq;
1205 fwspec->param[2] = 0;
1206 fwspec->param[3] = parent_type;
Kevin Hao24258762020-01-14 16:28:19 +08001207
1208 return fwspec;
Linus Walleijfdd61a02019-08-08 14:32:37 +02001209}
1210EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_fourcell);
1211
1212#else
1213
1214static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
1215{
1216 return -EINVAL;
1217}
1218
1219static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
1220{
1221 return false;
1222}
1223
1224#endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
1225
Linus Walleijd245b3f2016-11-24 10:57:25 +01001226/**
Linus Walleij14250522014-03-25 10:40:18 +01001227 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1228 * @d: the irqdomain used by this irqchip
1229 * @irq: the global irq number used by this GPIO irqchip irq
1230 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1231 *
1232 * This function will set up the mapping for a certain IRQ line on a
1233 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1234 * stored inside the gpiochip.
1235 */
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001236int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1237 irq_hw_number_t hwirq)
Linus Walleij14250522014-03-25 10:40:18 +01001238{
Linus Walleija0b66a72020-03-29 16:04:05 +02001239 struct gpio_chip *gc = d->host_data;
Linus Walleijd377f562019-07-16 11:11:45 +02001240 int ret = 0;
Linus Walleij14250522014-03-25 10:40:18 +01001241
Linus Walleija0b66a72020-03-29 16:04:05 +02001242 if (!gpiochip_irqchip_irq_valid(gc, hwirq))
Grygorii Strashkodc749a02017-07-21 11:49:00 -05001243 return -ENXIO;
1244
Linus Walleija0b66a72020-03-29 16:04:05 +02001245 irq_set_chip_data(irq, gc);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001246 /*
1247 * This lock class tells lockdep that GPIO irqs are in a different
1248 * category than their parents, so it won't report false recursion.
1249 */
Linus Walleija0b66a72020-03-29 16:04:05 +02001250 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key);
1251 irq_set_chip_and_handler(irq, gc->irq.chip, gc->irq.handler);
Linus Walleijd245b3f2016-11-24 10:57:25 +01001252 /* Chips that use nested thread handlers have them marked */
Linus Walleija0b66a72020-03-29 16:04:05 +02001253 if (gc->irq.threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02001254 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +01001255 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -05001256
Linus Walleija0b66a72020-03-29 16:04:05 +02001257 if (gc->irq.num_parents == 1)
1258 ret = irq_set_parent(irq, gc->irq.parents[0]);
1259 else if (gc->irq.map)
1260 ret = irq_set_parent(irq, gc->irq.map[hwirq]);
Thierry Redinge0d89722017-11-07 19:15:54 +01001261
Linus Walleijd377f562019-07-16 11:11:45 +02001262 if (ret < 0)
1263 return ret;
Thierry Redinge0d89722017-11-07 19:15:54 +01001264
Linus Walleij1333b902014-04-23 16:45:12 +02001265 /*
1266 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1267 * is passed as default type.
1268 */
Linus Walleija0b66a72020-03-29 16:04:05 +02001269 if (gc->irq.default_type != IRQ_TYPE_NONE)
1270 irq_set_irq_type(irq, gc->irq.default_type);
Linus Walleij14250522014-03-25 10:40:18 +01001271
1272 return 0;
1273}
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001274EXPORT_SYMBOL_GPL(gpiochip_irq_map);
Linus Walleij14250522014-03-25 10:40:18 +01001275
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001276void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
Linus Walleijc3626fd2014-03-28 20:42:01 +01001277{
Linus Walleija0b66a72020-03-29 16:04:05 +02001278 struct gpio_chip *gc = d->host_data;
Linus Walleij1c8732b2014-04-09 13:34:39 +02001279
Linus Walleija0b66a72020-03-29 16:04:05 +02001280 if (gc->irq.threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02001281 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001282 irq_set_chip_and_handler(irq, NULL, NULL);
1283 irq_set_chip_data(irq, NULL);
1284}
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001285EXPORT_SYMBOL_GPL(gpiochip_irq_unmap);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001286
Linus Walleij14250522014-03-25 10:40:18 +01001287static const struct irq_domain_ops gpiochip_domain_ops = {
1288 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +01001289 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +01001290 /* Virtually all GPIO irqchips are twocell:ed */
1291 .xlate = irq_domain_xlate_twocell,
1292};
1293
Linus Walleijfdd61a02019-08-08 14:32:37 +02001294/*
1295 * TODO: move these activate/deactivate in under the hierarchicial
1296 * irqchip implementation as static once SPMI and SSBI (all external
1297 * users) are phased over.
1298 */
Brian Masneyef74f702019-01-19 15:42:42 -05001299/**
1300 * gpiochip_irq_domain_activate() - Lock a GPIO to be used as an IRQ
1301 * @domain: The IRQ domain used by this IRQ chip
1302 * @data: Outermost irq_data associated with the IRQ
1303 * @reserve: If set, only reserve an interrupt vector instead of assigning one
1304 *
1305 * This function is a wrapper that calls gpiochip_lock_as_irq() and is to be
1306 * used as the activate function for the &struct irq_domain_ops. The host_data
1307 * for the IRQ domain must be the &struct gpio_chip.
1308 */
1309int gpiochip_irq_domain_activate(struct irq_domain *domain,
1310 struct irq_data *data, bool reserve)
1311{
Linus Walleija0b66a72020-03-29 16:04:05 +02001312 struct gpio_chip *gc = domain->host_data;
Brian Masneyef74f702019-01-19 15:42:42 -05001313
Linus Walleija0b66a72020-03-29 16:04:05 +02001314 return gpiochip_lock_as_irq(gc, data->hwirq);
Brian Masneyef74f702019-01-19 15:42:42 -05001315}
1316EXPORT_SYMBOL_GPL(gpiochip_irq_domain_activate);
1317
1318/**
1319 * gpiochip_irq_domain_deactivate() - Unlock a GPIO used as an IRQ
1320 * @domain: The IRQ domain used by this IRQ chip
1321 * @data: Outermost irq_data associated with the IRQ
1322 *
1323 * This function is a wrapper that will call gpiochip_unlock_as_irq() and is to
1324 * be used as the deactivate function for the &struct irq_domain_ops. The
1325 * host_data for the IRQ domain must be the &struct gpio_chip.
1326 */
1327void gpiochip_irq_domain_deactivate(struct irq_domain *domain,
1328 struct irq_data *data)
1329{
Linus Walleija0b66a72020-03-29 16:04:05 +02001330 struct gpio_chip *gc = domain->host_data;
Brian Masneyef74f702019-01-19 15:42:42 -05001331
Linus Walleija0b66a72020-03-29 16:04:05 +02001332 return gpiochip_unlock_as_irq(gc, data->hwirq);
Brian Masneyef74f702019-01-19 15:42:42 -05001333}
1334EXPORT_SYMBOL_GPL(gpiochip_irq_domain_deactivate);
1335
Andy Shevchenko13daf482020-11-09 22:53:16 +02001336static int gpiochip_to_irq(struct gpio_chip *gc, unsigned int offset)
Linus Walleij14250522014-03-25 10:40:18 +01001337{
Linus Walleija0b66a72020-03-29 16:04:05 +02001338 struct irq_domain *domain = gc->irq.domain;
Linus Walleijfdd61a02019-08-08 14:32:37 +02001339
Linus Walleija0b66a72020-03-29 16:04:05 +02001340 if (!gpiochip_irqchip_irq_valid(gc, offset))
Grygorii Strashkodc749a02017-07-21 11:49:00 -05001341 return -ENXIO;
Thierry Redinge0d89722017-11-07 19:15:54 +01001342
Linus Walleijfdd61a02019-08-08 14:32:37 +02001343#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1344 if (irq_domain_is_hierarchy(domain)) {
1345 struct irq_fwspec spec;
1346
1347 spec.fwnode = domain->fwnode;
1348 spec.param_count = 2;
Linus Walleija0b66a72020-03-29 16:04:05 +02001349 spec.param[0] = gc->irq.child_offset_to_irq(gc, offset);
Linus Walleijfdd61a02019-08-08 14:32:37 +02001350 spec.param[1] = IRQ_TYPE_NONE;
1351
1352 return irq_create_fwspec_mapping(&spec);
1353 }
1354#endif
1355
1356 return irq_create_mapping(domain, offset);
Linus Walleij14250522014-03-25 10:40:18 +01001357}
1358
Hans Verkuil4e6b8232018-09-08 11:23:14 +02001359static int gpiochip_irq_reqres(struct irq_data *d)
1360{
Linus Walleija0b66a72020-03-29 16:04:05 +02001361 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Hans Verkuil4e6b8232018-09-08 11:23:14 +02001362
Linus Walleija0b66a72020-03-29 16:04:05 +02001363 return gpiochip_reqres_irq(gc, d->hwirq);
Hans Verkuil4e6b8232018-09-08 11:23:14 +02001364}
1365
1366static void gpiochip_irq_relres(struct irq_data *d)
1367{
Linus Walleija0b66a72020-03-29 16:04:05 +02001368 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Hans Verkuil4e6b8232018-09-08 11:23:14 +02001369
Linus Walleija0b66a72020-03-29 16:04:05 +02001370 gpiochip_relres_irq(gc, d->hwirq);
Hans Verkuil4e6b8232018-09-08 11:23:14 +02001371}
1372
Maulik Shaha8173822020-05-23 22:41:10 +05301373static void gpiochip_irq_mask(struct irq_data *d)
1374{
1375 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1376
1377 if (gc->irq.irq_mask)
1378 gc->irq.irq_mask(d);
1379 gpiochip_disable_irq(gc, d->hwirq);
1380}
1381
1382static void gpiochip_irq_unmask(struct irq_data *d)
1383{
1384 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1385
1386 gpiochip_enable_irq(gc, d->hwirq);
1387 if (gc->irq.irq_unmask)
1388 gc->irq.irq_unmask(d);
1389}
1390
Hans Verkuil461c1a72018-09-08 11:23:17 +02001391static void gpiochip_irq_enable(struct irq_data *d)
1392{
Linus Walleija0b66a72020-03-29 16:04:05 +02001393 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Hans Verkuil461c1a72018-09-08 11:23:17 +02001394
Linus Walleija0b66a72020-03-29 16:04:05 +02001395 gpiochip_enable_irq(gc, d->hwirq);
Maulik Shaha8173822020-05-23 22:41:10 +05301396 gc->irq.irq_enable(d);
Hans Verkuil461c1a72018-09-08 11:23:17 +02001397}
1398
1399static void gpiochip_irq_disable(struct irq_data *d)
1400{
Linus Walleija0b66a72020-03-29 16:04:05 +02001401 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Hans Verkuil461c1a72018-09-08 11:23:17 +02001402
Maulik Shaha8173822020-05-23 22:41:10 +05301403 gc->irq.irq_disable(d);
Linus Walleija0b66a72020-03-29 16:04:05 +02001404 gpiochip_disable_irq(gc, d->hwirq);
Hans Verkuil461c1a72018-09-08 11:23:17 +02001405}
1406
Linus Walleija0b66a72020-03-29 16:04:05 +02001407static void gpiochip_set_irq_hooks(struct gpio_chip *gc)
Hans Verkuilca620f22018-09-08 11:23:15 +02001408{
Linus Walleija0b66a72020-03-29 16:04:05 +02001409 struct irq_chip *irqchip = gc->irq.chip;
Hans Verkuilca620f22018-09-08 11:23:15 +02001410
1411 if (!irqchip->irq_request_resources &&
1412 !irqchip->irq_release_resources) {
1413 irqchip->irq_request_resources = gpiochip_irq_reqres;
1414 irqchip->irq_release_resources = gpiochip_irq_relres;
1415 }
Linus Walleija0b66a72020-03-29 16:04:05 +02001416 if (WARN_ON(gc->irq.irq_enable))
Hans Verkuil461c1a72018-09-08 11:23:17 +02001417 return;
Hans Verkuil171948e2018-09-14 10:36:39 +02001418 /* Check if the irqchip already has this hook... */
1419 if (irqchip->irq_enable == gpiochip_irq_enable) {
1420 /*
1421 * ...and if so, give a gentle warning that this is bad
1422 * practice.
1423 */
Linus Walleija0b66a72020-03-29 16:04:05 +02001424 chip_info(gc,
Hans Verkuil171948e2018-09-14 10:36:39 +02001425 "detected irqchip that is shared with multiple gpiochips: please fix the driver.\n");
1426 return;
1427 }
Maulik Shaha8173822020-05-23 22:41:10 +05301428
1429 if (irqchip->irq_disable) {
1430 gc->irq.irq_disable = irqchip->irq_disable;
1431 irqchip->irq_disable = gpiochip_irq_disable;
1432 } else {
1433 gc->irq.irq_mask = irqchip->irq_mask;
1434 irqchip->irq_mask = gpiochip_irq_mask;
1435 }
1436
1437 if (irqchip->irq_enable) {
1438 gc->irq.irq_enable = irqchip->irq_enable;
1439 irqchip->irq_enable = gpiochip_irq_enable;
1440 } else {
1441 gc->irq.irq_unmask = irqchip->irq_unmask;
1442 irqchip->irq_unmask = gpiochip_irq_unmask;
1443 }
Hans Verkuilca620f22018-09-08 11:23:15 +02001444}
1445
Linus Walleij14250522014-03-25 10:40:18 +01001446/**
Thierry Redinge0d89722017-11-07 19:15:54 +01001447 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
Linus Walleija0b66a72020-03-29 16:04:05 +02001448 * @gc: the GPIO chip to add the IRQ chip to
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001449 * @lock_key: lockdep class for IRQ lock
1450 * @request_key: lockdep class for IRQ request
Thierry Redinge0d89722017-11-07 19:15:54 +01001451 */
Linus Walleija0b66a72020-03-29 16:04:05 +02001452static int gpiochip_add_irqchip(struct gpio_chip *gc,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001453 struct lock_class_key *lock_key,
1454 struct lock_class_key *request_key)
Thierry Redinge0d89722017-11-07 19:15:54 +01001455{
Linus Walleija0b66a72020-03-29 16:04:05 +02001456 struct irq_chip *irqchip = gc->irq.chip;
Linus Walleijfdd61a02019-08-08 14:32:37 +02001457 const struct irq_domain_ops *ops = NULL;
Thierry Redinge0d89722017-11-07 19:15:54 +01001458 struct device_node *np;
1459 unsigned int type;
1460 unsigned int i;
1461
1462 if (!irqchip)
1463 return 0;
1464
Linus Walleija0b66a72020-03-29 16:04:05 +02001465 if (gc->irq.parent_handler && gc->can_sleep) {
1466 chip_err(gc, "you cannot have chained interrupts on a chip that may sleep\n");
Thierry Redinge0d89722017-11-07 19:15:54 +01001467 return -EINVAL;
1468 }
1469
Linus Walleija0b66a72020-03-29 16:04:05 +02001470 np = gc->gpiodev->dev.of_node;
1471 type = gc->irq.default_type;
Thierry Redinge0d89722017-11-07 19:15:54 +01001472
1473 /*
1474 * Specifying a default trigger is a terrible idea if DT or ACPI is
1475 * used to configure the interrupts, as you may end up with
1476 * conflicting triggers. Tell the user, and reset to NONE.
1477 */
1478 if (WARN(np && type != IRQ_TYPE_NONE,
1479 "%s: Ignoring %u default trigger\n", np->full_name, type))
1480 type = IRQ_TYPE_NONE;
1481
Linus Walleija0b66a72020-03-29 16:04:05 +02001482 if (has_acpi_companion(gc->parent) && type != IRQ_TYPE_NONE) {
1483 acpi_handle_warn(ACPI_HANDLE(gc->parent),
Thierry Redinge0d89722017-11-07 19:15:54 +01001484 "Ignoring %u default trigger\n", type);
1485 type = IRQ_TYPE_NONE;
1486 }
1487
Linus Walleija0b66a72020-03-29 16:04:05 +02001488 gc->to_irq = gpiochip_to_irq;
1489 gc->irq.default_type = type;
1490 gc->irq.lock_key = lock_key;
1491 gc->irq.request_key = request_key;
Thierry Redinge0d89722017-11-07 19:15:54 +01001492
Linus Walleijfdd61a02019-08-08 14:32:37 +02001493 /* If a parent irqdomain is provided, let's build a hierarchy */
Linus Walleija0b66a72020-03-29 16:04:05 +02001494 if (gpiochip_hierarchy_is_hierarchical(gc)) {
1495 int ret = gpiochip_hierarchy_add_domain(gc);
Linus Walleijfdd61a02019-08-08 14:32:37 +02001496 if (ret)
1497 return ret;
1498 } else {
1499 /* Some drivers provide custom irqdomain ops */
Linus Walleija0b66a72020-03-29 16:04:05 +02001500 if (gc->irq.domain_ops)
1501 ops = gc->irq.domain_ops;
Thierry Redinge0d89722017-11-07 19:15:54 +01001502
Linus Walleijfdd61a02019-08-08 14:32:37 +02001503 if (!ops)
1504 ops = &gpiochip_domain_ops;
Linus Walleija0b66a72020-03-29 16:04:05 +02001505 gc->irq.domain = irq_domain_add_simple(np,
1506 gc->ngpio,
1507 gc->irq.first,
1508 ops, gc);
1509 if (!gc->irq.domain)
Linus Walleijfdd61a02019-08-08 14:32:37 +02001510 return -EINVAL;
1511 }
Thierry Redinge0d89722017-11-07 19:15:54 +01001512
Linus Walleija0b66a72020-03-29 16:04:05 +02001513 if (gc->irq.parent_handler) {
1514 void *data = gc->irq.parent_handler_data ?: gc;
Thierry Redinge0d89722017-11-07 19:15:54 +01001515
Linus Walleija0b66a72020-03-29 16:04:05 +02001516 for (i = 0; i < gc->irq.num_parents; i++) {
Thierry Redinge0d89722017-11-07 19:15:54 +01001517 /*
1518 * The parent IRQ chip is already using the chip_data
1519 * for this IRQ chip, so our callbacks simply use the
1520 * handler_data.
1521 */
Linus Walleija0b66a72020-03-29 16:04:05 +02001522 irq_set_chained_handler_and_data(gc->irq.parents[i],
1523 gc->irq.parent_handler,
Thierry Redinge0d89722017-11-07 19:15:54 +01001524 data);
1525 }
Thierry Redinge0d89722017-11-07 19:15:54 +01001526 }
1527
Linus Walleija0b66a72020-03-29 16:04:05 +02001528 gpiochip_set_irq_hooks(gc);
Hans Verkuilca620f22018-09-08 11:23:15 +02001529
Linus Walleija0b66a72020-03-29 16:04:05 +02001530 acpi_gpiochip_request_interrupts(gc);
Thierry Redinge0d89722017-11-07 19:15:54 +01001531
1532 return 0;
1533}
1534
1535/**
Linus Walleij14250522014-03-25 10:40:18 +01001536 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
Linus Walleija0b66a72020-03-29 16:04:05 +02001537 * @gc: the gpiochip to remove the irqchip from
Linus Walleij14250522014-03-25 10:40:18 +01001538 *
1539 * This is called only from gpiochip_remove()
1540 */
Linus Walleija0b66a72020-03-29 16:04:05 +02001541static void gpiochip_irqchip_remove(struct gpio_chip *gc)
Linus Walleij14250522014-03-25 10:40:18 +01001542{
Linus Walleija0b66a72020-03-29 16:04:05 +02001543 struct irq_chip *irqchip = gc->irq.chip;
Thierry Reding39e5f092017-11-07 19:15:50 +01001544 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +01001545
Linus Walleija0b66a72020-03-29 16:04:05 +02001546 acpi_gpiochip_free_interrupts(gc);
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001547
Linus Walleija0b66a72020-03-29 16:04:05 +02001548 if (irqchip && gc->irq.parent_handler) {
1549 struct gpio_irq_chip *irq = &gc->irq;
Thierry Reding39e5f092017-11-07 19:15:50 +01001550 unsigned int i;
1551
1552 for (i = 0; i < irq->num_parents; i++)
1553 irq_set_chained_handler_and_data(irq->parents[i],
1554 NULL, NULL);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001555 }
1556
Linus Walleijc3626fd2014-03-28 20:42:01 +01001557 /* Remove all IRQ mappings and delete the domain */
Linus Walleija0b66a72020-03-29 16:04:05 +02001558 if (gc->irq.domain) {
Thierry Reding39e5f092017-11-07 19:15:50 +01001559 unsigned int irq;
1560
Linus Walleija0b66a72020-03-29 16:04:05 +02001561 for (offset = 0; offset < gc->ngpio; offset++) {
1562 if (!gpiochip_irqchip_irq_valid(gc, offset))
Mika Westerberg79b804c2016-09-20 15:15:21 +03001563 continue;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001564
Linus Walleija0b66a72020-03-29 16:04:05 +02001565 irq = irq_find_mapping(gc->irq.domain, offset);
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001566 irq_dispose_mapping(irq);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001567 }
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001568
Linus Walleija0b66a72020-03-29 16:04:05 +02001569 irq_domain_remove(gc->irq.domain);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001570 }
Linus Walleij14250522014-03-25 10:40:18 +01001571
Hans Verkuil461c1a72018-09-08 11:23:17 +02001572 if (irqchip) {
1573 if (irqchip->irq_request_resources == gpiochip_irq_reqres) {
1574 irqchip->irq_request_resources = NULL;
1575 irqchip->irq_release_resources = NULL;
1576 }
1577 if (irqchip->irq_enable == gpiochip_irq_enable) {
Linus Walleija0b66a72020-03-29 16:04:05 +02001578 irqchip->irq_enable = gc->irq.irq_enable;
1579 irqchip->irq_disable = gc->irq.irq_disable;
Hans Verkuil461c1a72018-09-08 11:23:17 +02001580 }
Linus Walleij14250522014-03-25 10:40:18 +01001581 }
Linus Walleija0b66a72020-03-29 16:04:05 +02001582 gc->irq.irq_enable = NULL;
1583 gc->irq.irq_disable = NULL;
1584 gc->irq.chip = NULL;
Mika Westerberg79b804c2016-09-20 15:15:21 +03001585
Linus Walleija0b66a72020-03-29 16:04:05 +02001586 gpiochip_irqchip_free_valid_mask(gc);
Linus Walleij14250522014-03-25 10:40:18 +01001587}
1588
1589/**
Michael Walle6a45b0e2020-05-28 16:58:43 +02001590 * gpiochip_irqchip_add_domain() - adds an irqdomain to a gpiochip
1591 * @gc: the gpiochip to add the irqchip to
1592 * @domain: the irqdomain to add to the gpiochip
1593 *
1594 * This function adds an IRQ domain to the gpiochip.
1595 */
1596int gpiochip_irqchip_add_domain(struct gpio_chip *gc,
1597 struct irq_domain *domain)
1598{
1599 if (!domain)
1600 return -EINVAL;
1601
1602 gc->to_irq = gpiochip_to_irq;
1603 gc->irq.domain = domain;
1604
1605 return 0;
1606}
1607EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_domain);
1608
Linus Walleij14250522014-03-25 10:40:18 +01001609#else /* CONFIG_GPIOLIB_IRQCHIP */
1610
Linus Walleija0b66a72020-03-29 16:04:05 +02001611static inline int gpiochip_add_irqchip(struct gpio_chip *gc,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001612 struct lock_class_key *lock_key,
1613 struct lock_class_key *request_key)
Thierry Redinge0d89722017-11-07 19:15:54 +01001614{
1615 return 0;
1616}
Linus Walleija0b66a72020-03-29 16:04:05 +02001617static void gpiochip_irqchip_remove(struct gpio_chip *gc) {}
Andy Shevchenko9411e3a2019-10-09 17:34:44 +03001618
Linus Walleija0b66a72020-03-29 16:04:05 +02001619static inline int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
Andy Shevchenko9411e3a2019-10-09 17:34:44 +03001620{
1621 return 0;
1622}
1623
Linus Walleija0b66a72020-03-29 16:04:05 +02001624static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001625{
1626 return 0;
1627}
Linus Walleija0b66a72020-03-29 16:04:05 +02001628static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001629{ }
Linus Walleij14250522014-03-25 10:40:18 +01001630
1631#endif /* CONFIG_GPIOLIB_IRQCHIP */
1632
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001633/**
1634 * gpiochip_generic_request() - request the gpio function for a pin
Linus Walleija0b66a72020-03-29 16:04:05 +02001635 * @gc: the gpiochip owning the GPIO
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001636 * @offset: the offset of the GPIO to request for GPIO function
1637 */
Andy Shevchenko13daf482020-11-09 22:53:16 +02001638int gpiochip_generic_request(struct gpio_chip *gc, unsigned int offset)
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001639{
Thierry Reding89ad5562020-03-30 11:02:57 +02001640#ifdef CONFIG_PINCTRL
Linus Walleija0b66a72020-03-29 16:04:05 +02001641 if (list_empty(&gc->gpiodev->pin_ranges))
Thierry Reding89ad5562020-03-30 11:02:57 +02001642 return 0;
1643#endif
Thierry Reding2ab73c62020-03-19 13:27:29 +01001644
Linus Walleija0b66a72020-03-29 16:04:05 +02001645 return pinctrl_gpio_request(gc->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001646}
1647EXPORT_SYMBOL_GPL(gpiochip_generic_request);
1648
1649/**
1650 * gpiochip_generic_free() - free the gpio function from a pin
Linus Walleija0b66a72020-03-29 16:04:05 +02001651 * @gc: the gpiochip to request the gpio function for
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001652 * @offset: the offset of the GPIO to free from GPIO function
1653 */
Andy Shevchenko13daf482020-11-09 22:53:16 +02001654void gpiochip_generic_free(struct gpio_chip *gc, unsigned int offset)
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001655{
Linus Walleija0b66a72020-03-29 16:04:05 +02001656 pinctrl_gpio_free(gc->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001657}
1658EXPORT_SYMBOL_GPL(gpiochip_generic_free);
1659
Mika Westerberg2956b5d2017-01-23 15:34:34 +03001660/**
1661 * gpiochip_generic_config() - apply configuration for a pin
Linus Walleija0b66a72020-03-29 16:04:05 +02001662 * @gc: the gpiochip owning the GPIO
Mika Westerberg2956b5d2017-01-23 15:34:34 +03001663 * @offset: the offset of the GPIO to apply the configuration
1664 * @config: the configuration to be applied
1665 */
Andy Shevchenko13daf482020-11-09 22:53:16 +02001666int gpiochip_generic_config(struct gpio_chip *gc, unsigned int offset,
Mika Westerberg2956b5d2017-01-23 15:34:34 +03001667 unsigned long config)
1668{
Linus Walleija0b66a72020-03-29 16:04:05 +02001669 return pinctrl_gpio_set_config(gc->gpiodev->base + offset, config);
Mika Westerberg2956b5d2017-01-23 15:34:34 +03001670}
1671EXPORT_SYMBOL_GPL(gpiochip_generic_config);
1672
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301673#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01001674
Linus Walleij3f0f8672012-11-20 12:40:15 +01001675/**
Christian Ruppert586a87e2013-10-15 15:37:54 +02001676 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
Linus Walleija0b66a72020-03-29 16:04:05 +02001677 * @gc: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +02001678 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +02001679 * @gpio_offset: the start offset in the current gpio_chip number space
1680 * @pin_group: name of the pin group inside the pin controller
Christian Lamparter973c1712018-05-21 22:57:39 +02001681 *
1682 * Calling this function directly from a DeviceTree-supported
1683 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
1684 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
1685 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
Christian Ruppert586a87e2013-10-15 15:37:54 +02001686 */
Linus Walleija0b66a72020-03-29 16:04:05 +02001687int gpiochip_add_pingroup_range(struct gpio_chip *gc,
Christian Ruppert586a87e2013-10-15 15:37:54 +02001688 struct pinctrl_dev *pctldev,
1689 unsigned int gpio_offset, const char *pin_group)
1690{
1691 struct gpio_pin_range *pin_range;
Linus Walleija0b66a72020-03-29 16:04:05 +02001692 struct gpio_device *gdev = gc->gpiodev;
Christian Ruppert586a87e2013-10-15 15:37:54 +02001693 int ret;
1694
1695 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1696 if (!pin_range) {
Linus Walleija0b66a72020-03-29 16:04:05 +02001697 chip_err(gc, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +02001698 return -ENOMEM;
1699 }
1700
1701 /* Use local offset as range ID */
1702 pin_range->range.id = gpio_offset;
Linus Walleija0b66a72020-03-29 16:04:05 +02001703 pin_range->range.gc = gc;
1704 pin_range->range.name = gc->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001705 pin_range->range.base = gdev->base + gpio_offset;
Christian Ruppert586a87e2013-10-15 15:37:54 +02001706 pin_range->pctldev = pctldev;
1707
1708 ret = pinctrl_get_group_pins(pctldev, pin_group,
1709 &pin_range->range.pins,
1710 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001711 if (ret < 0) {
1712 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001713 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001714 }
Christian Ruppert586a87e2013-10-15 15:37:54 +02001715
1716 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1717
Linus Walleija0b66a72020-03-29 16:04:05 +02001718 chip_dbg(gc, "created GPIO range %d->%d ==> %s PINGRP %s\n",
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001719 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +02001720 pinctrl_dev_get_devname(pctldev), pin_group);
1721
Linus Walleij20ec3e32016-02-11 11:03:06 +01001722 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001723
1724 return 0;
1725}
1726EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1727
1728/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01001729 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
Linus Walleija0b66a72020-03-29 16:04:05 +02001730 * @gc: the gpiochip to add the range for
Thierry Reding950d55f52017-07-24 16:57:22 +02001731 * @pinctl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01001732 * @gpio_offset: the start offset in the current gpio_chip number space
1733 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01001734 * @npins: the number of pins from the offset of each pin space (GPIO and
1735 * pin controller) to accumulate in this range
Thierry Reding950d55f52017-07-24 16:57:22 +02001736 *
1737 * Returns:
1738 * 0 on success, or a negative error-code on failure.
Christian Lamparter973c1712018-05-21 22:57:39 +02001739 *
1740 * Calling this function directly from a DeviceTree-supported
1741 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
1742 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
1743 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
Linus Walleij3f0f8672012-11-20 12:40:15 +01001744 */
Linus Walleija0b66a72020-03-29 16:04:05 +02001745int gpiochip_add_pin_range(struct gpio_chip *gc, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01001746 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01001747 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301748{
1749 struct gpio_pin_range *pin_range;
Linus Walleija0b66a72020-03-29 16:04:05 +02001750 struct gpio_device *gdev = gc->gpiodev;
Axel Linb4d4b1f2012-11-21 14:33:56 +08001751 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301752
Linus Walleij3f0f8672012-11-20 12:40:15 +01001753 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301754 if (!pin_range) {
Linus Walleija0b66a72020-03-29 16:04:05 +02001755 chip_err(gc, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001756 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301757 }
1758
Linus Walleij3f0f8672012-11-20 12:40:15 +01001759 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01001760 pin_range->range.id = gpio_offset;
Linus Walleija0b66a72020-03-29 16:04:05 +02001761 pin_range->range.gc = gc;
1762 pin_range->range.name = gc->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001763 pin_range->range.base = gdev->base + gpio_offset;
Linus Walleij316511c2012-11-21 08:48:09 +01001764 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301765 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01001766 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301767 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01001768 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08001769 ret = PTR_ERR(pin_range->pctldev);
Linus Walleija0b66a72020-03-29 16:04:05 +02001770 chip_err(gc, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +01001771 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08001772 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001773 }
Linus Walleija0b66a72020-03-29 16:04:05 +02001774 chip_dbg(gc, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001775 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +01001776 pinctl_name,
1777 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301778
Linus Walleij20ec3e32016-02-11 11:03:06 +01001779 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001780
1781 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301782}
Linus Walleij165adc92012-11-06 14:49:39 +01001783EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301784
Linus Walleij3f0f8672012-11-20 12:40:15 +01001785/**
1786 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
Linus Walleija0b66a72020-03-29 16:04:05 +02001787 * @gc: the chip to remove all the mappings for
Linus Walleij3f0f8672012-11-20 12:40:15 +01001788 */
Linus Walleija0b66a72020-03-29 16:04:05 +02001789void gpiochip_remove_pin_ranges(struct gpio_chip *gc)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301790{
1791 struct gpio_pin_range *pin_range, *tmp;
Linus Walleija0b66a72020-03-29 16:04:05 +02001792 struct gpio_device *gdev = gc->gpiodev;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301793
Linus Walleij20ec3e32016-02-11 11:03:06 +01001794 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301795 list_del(&pin_range->node);
1796 pinctrl_remove_gpio_range(pin_range->pctldev,
1797 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001798 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301799 }
1800}
Linus Walleij165adc92012-11-06 14:49:39 +01001801EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1802
1803#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301804
David Brownelld2876d02008-02-04 22:28:20 -08001805/* These "optional" allocation calls help prevent drivers from stomping
1806 * on each other, and help provide better diagnostics in debugfs.
1807 * They're called even less than the "set direction" calls.
1808 */
Linus Walleijfac9d882017-09-26 20:58:28 +02001809static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08001810{
Linus Walleija0b66a72020-03-29 16:04:05 +02001811 struct gpio_chip *gc = desc->gdev->chip;
Linus Walleijd377f562019-07-16 11:11:45 +02001812 int ret;
David Brownelld2876d02008-02-04 22:28:20 -08001813 unsigned long flags;
Biju Das3789f5a2018-08-07 08:15:18 +01001814 unsigned offset;
David Brownelld2876d02008-02-04 22:28:20 -08001815
Muchun Song18534df2018-11-01 21:12:50 +08001816 if (label) {
1817 label = kstrdup_const(label, GFP_KERNEL);
1818 if (!label)
1819 return -ENOMEM;
1820 }
1821
David Brownelld2876d02008-02-04 22:28:20 -08001822 spin_lock_irqsave(&gpio_lock, flags);
1823
David Brownelld2876d02008-02-04 22:28:20 -08001824 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07001825 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001826 */
1827
1828 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1829 desc_set_label(desc, label ? : "?");
Linus Walleijd377f562019-07-16 11:11:45 +02001830 ret = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001831 } else {
Muchun Song18534df2018-11-01 21:12:50 +08001832 kfree_const(label);
Linus Walleijd377f562019-07-16 11:11:45 +02001833 ret = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -08001834 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001835 }
1836
Linus Walleija0b66a72020-03-29 16:04:05 +02001837 if (gc->request) {
1838 /* gc->request may sleep */
David Brownell35e8bb52008-10-15 22:03:16 -07001839 spin_unlock_irqrestore(&gpio_lock, flags);
Biju Das3789f5a2018-08-07 08:15:18 +01001840 offset = gpio_chip_hwgpio(desc);
Linus Walleija0b66a72020-03-29 16:04:05 +02001841 if (gpiochip_line_is_valid(gc, offset))
1842 ret = gc->request(gc, offset);
Biju Das3789f5a2018-08-07 08:15:18 +01001843 else
Linus Walleijd377f562019-07-16 11:11:45 +02001844 ret = -EINVAL;
David Brownell35e8bb52008-10-15 22:03:16 -07001845 spin_lock_irqsave(&gpio_lock, flags);
1846
Linus Walleijd377f562019-07-16 11:11:45 +02001847 if (ret < 0) {
David Brownell35e8bb52008-10-15 22:03:16 -07001848 desc_set_label(desc, NULL);
Muchun Song18534df2018-11-01 21:12:50 +08001849 kfree_const(label);
David Brownell35e8bb52008-10-15 22:03:16 -07001850 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001851 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001852 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001853 }
Linus Walleija0b66a72020-03-29 16:04:05 +02001854 if (gc->get_direction) {
1855 /* gc->get_direction may sleep */
Mathias Nyman80b0a602012-10-24 17:25:27 +03001856 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001857 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001858 spin_lock_irqsave(&gpio_lock, flags);
1859 }
David Brownelld2876d02008-02-04 22:28:20 -08001860done:
Mika Westerberg77c2d792014-03-10 14:54:50 +02001861 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijd377f562019-07-16 11:11:45 +02001862 return ret;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001863}
1864
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001865/*
1866 * This descriptor validation needs to be inserted verbatim into each
1867 * function taking a descriptor, so we need to use a preprocessor
Linus Walleij54d77192016-05-30 16:48:39 +02001868 * macro to avoid endless duplication. If the desc is NULL it is an
1869 * optional GPIO and calls should just bail out.
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001870 */
Rasmus Villemoesa746a232017-12-21 01:27:04 +01001871static int validate_desc(const struct gpio_desc *desc, const char *func)
1872{
1873 if (!desc)
1874 return 0;
1875 if (IS_ERR(desc)) {
1876 pr_warn("%s: invalid GPIO (errorpointer)\n", func);
1877 return PTR_ERR(desc);
1878 }
1879 if (!desc->gdev) {
1880 pr_warn("%s: invalid GPIO (no device)\n", func);
1881 return -EINVAL;
1882 }
1883 if (!desc->gdev->chip) {
1884 dev_warn(&desc->gdev->dev,
1885 "%s: backing chip is gone\n", func);
1886 return 0;
1887 }
1888 return 1;
1889}
1890
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001891#define VALIDATE_DESC(desc) do { \
Rasmus Villemoesa746a232017-12-21 01:27:04 +01001892 int __valid = validate_desc(desc, __func__); \
1893 if (__valid <= 0) \
1894 return __valid; \
1895 } while (0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001896
1897#define VALIDATE_DESC_VOID(desc) do { \
Rasmus Villemoesa746a232017-12-21 01:27:04 +01001898 int __valid = validate_desc(desc, __func__); \
1899 if (__valid <= 0) \
Linus Walleij54d77192016-05-30 16:48:39 +02001900 return; \
Rasmus Villemoesa746a232017-12-21 01:27:04 +01001901 } while (0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001902
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09001903int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001904{
Linus Walleijd377f562019-07-16 11:11:45 +02001905 int ret = -EPROBE_DEFER;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001906 struct gpio_device *gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001907
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001908 VALIDATE_DESC(desc);
1909 gdev = desc->gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001910
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001911 if (try_module_get(gdev->owner)) {
Linus Walleijd377f562019-07-16 11:11:45 +02001912 ret = gpiod_request_commit(desc, label);
1913 if (ret < 0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001914 module_put(gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01001915 else
1916 get_device(&gdev->dev);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001917 }
1918
Linus Walleijd377f562019-07-16 11:11:45 +02001919 if (ret)
1920 gpiod_dbg(desc, "%s: status %d\n", __func__, ret);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001921
Linus Walleijd377f562019-07-16 11:11:45 +02001922 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08001923}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001924
Linus Walleijfac9d882017-09-26 20:58:28 +02001925static bool gpiod_free_commit(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001926{
Mika Westerberg77c2d792014-03-10 14:54:50 +02001927 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08001928 unsigned long flags;
Linus Walleija0b66a72020-03-29 16:04:05 +02001929 struct gpio_chip *gc;
David Brownelld2876d02008-02-04 22:28:20 -08001930
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07001931 might_sleep();
1932
Alexandre Courbot372e7222013-02-03 01:29:29 +09001933 gpiod_unexport(desc);
David Brownelld8f388d82008-07-25 01:46:07 -07001934
David Brownelld2876d02008-02-04 22:28:20 -08001935 spin_lock_irqsave(&gpio_lock, flags);
1936
Linus Walleija0b66a72020-03-29 16:04:05 +02001937 gc = desc->gdev->chip;
1938 if (gc && test_bit(FLAG_REQUESTED, &desc->flags)) {
1939 if (gc->free) {
David Brownell35e8bb52008-10-15 22:03:16 -07001940 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleija0b66a72020-03-29 16:04:05 +02001941 might_sleep_if(gc->can_sleep);
1942 gc->free(gc, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001943 spin_lock_irqsave(&gpio_lock, flags);
1944 }
Muchun Song18534df2018-11-01 21:12:50 +08001945 kfree_const(desc->label);
David Brownelld2876d02008-02-04 22:28:20 -08001946 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08001947 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001948 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301949 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301950 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Drew Fustini9225d512019-11-05 10:04:23 +08001951 clear_bit(FLAG_PULL_UP, &desc->flags);
1952 clear_bit(FLAG_PULL_DOWN, &desc->flags);
Kent Gibson2148ad72019-11-05 10:04:25 +08001953 clear_bit(FLAG_BIAS_DISABLE, &desc->flags);
Kent Gibson73e03412020-09-28 08:27:56 +08001954 clear_bit(FLAG_EDGE_RISING, &desc->flags);
1955 clear_bit(FLAG_EDGE_FALLING, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06001956 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Geert Uytterhoeven63636d92020-02-20 14:01:49 +01001957#ifdef CONFIG_OF_DYNAMIC
1958 desc->hog = NULL;
1959#endif
Kent Gibson65cff702020-09-28 08:27:59 +08001960#ifdef CONFIG_GPIO_CDEV
1961 WRITE_ONCE(desc->debounce_period_us, 0);
1962#endif
Mika Westerberg77c2d792014-03-10 14:54:50 +02001963 ret = true;
1964 }
David Brownelld2876d02008-02-04 22:28:20 -08001965
1966 spin_unlock_irqrestore(&gpio_lock, flags);
Kent Gibson6accc372020-07-08 12:15:51 +08001967 blocking_notifier_call_chain(&desc->gdev->notifier,
1968 GPIOLINE_CHANGED_RELEASED, desc);
Bartosz Golaszewski51c10642019-11-22 15:19:21 +01001969
Mika Westerberg77c2d792014-03-10 14:54:50 +02001970 return ret;
1971}
1972
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09001973void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001974{
Linus Walleijfac9d882017-09-26 20:58:28 +02001975 if (desc && desc->gdev && gpiod_free_commit(desc)) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001976 module_put(desc->gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01001977 put_device(&desc->gdev->dev);
1978 } else {
Mika Westerberg77c2d792014-03-10 14:54:50 +02001979 WARN_ON(extra_checks);
Linus Walleij33a68e82016-02-11 10:28:44 +01001980 }
David Brownelld2876d02008-02-04 22:28:20 -08001981}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001982
David Brownelld2876d02008-02-04 22:28:20 -08001983/**
1984 * gpiochip_is_requested - return string iff signal was requested
Linus Walleija0b66a72020-03-29 16:04:05 +02001985 * @gc: controller managing the signal
David Brownelld2876d02008-02-04 22:28:20 -08001986 * @offset: of signal within controller's 0..(ngpio - 1) range
1987 *
1988 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09001989 * The string returned is the label passed to gpio_request(); if none has been
1990 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08001991 *
1992 * This function is for use by GPIO controller drivers. The label can
1993 * help with diagnostics, and knowing that the signal is used as a GPIO
1994 * can help avoid accidentally multiplexing it to another controller.
1995 */
Andy Shevchenko13daf482020-11-09 22:53:16 +02001996const char *gpiochip_is_requested(struct gpio_chip *gc, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08001997{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001998 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001999
Linus Walleija0b66a72020-03-29 16:04:05 +02002000 if (offset >= gc->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08002001 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002002
Linus Walleija0b66a72020-03-29 16:04:05 +02002003 desc = gpiochip_get_desc(gc, offset);
Bartosz Golaszewski1739a2d2020-02-19 10:47:02 +01002004 if (IS_ERR(desc))
2005 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002006
Alexandre Courbot372e7222013-02-03 01:29:29 +09002007 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08002008 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002009 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08002010}
2011EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2012
Mika Westerberg77c2d792014-03-10 14:54:50 +02002013/**
2014 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
Linus Walleija0b66a72020-03-29 16:04:05 +02002015 * @gc: GPIO chip
Thierry Reding950d55f52017-07-24 16:57:22 +02002016 * @hwnum: hardware number of the GPIO for which to request the descriptor
Mika Westerberg77c2d792014-03-10 14:54:50 +02002017 * @label: label for the GPIO
Linus Walleij5923ea62019-04-26 14:40:18 +02002018 * @lflags: lookup flags for this GPIO or 0 if default, this can be used to
2019 * specify things like line inversion semantics with the machine flags
2020 * such as GPIO_OUT_LOW
2021 * @dflags: descriptor request flags for this GPIO or 0 if default, this
2022 * can be used to specify consumer semantics such as open drain
Mika Westerberg77c2d792014-03-10 14:54:50 +02002023 *
2024 * Function allows GPIO chip drivers to request and use their own GPIO
2025 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2026 * function will not increase reference count of the GPIO chip module. This
2027 * allows the GPIO chip module to be unloaded as needed (we assume that the
2028 * GPIO chip driver handles freeing the GPIOs it has requested).
Thierry Reding950d55f52017-07-24 16:57:22 +02002029 *
2030 * Returns:
2031 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2032 * code on failure.
Mika Westerberg77c2d792014-03-10 14:54:50 +02002033 */
Linus Walleija0b66a72020-03-29 16:04:05 +02002034struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *gc,
Bartosz Golaszewski06863622019-12-24 13:06:59 +01002035 unsigned int hwnum,
Linus Walleij21abf102018-09-04 13:31:45 +02002036 const char *label,
Linus Walleij5923ea62019-04-26 14:40:18 +02002037 enum gpio_lookup_flags lflags,
2038 enum gpiod_flags dflags)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002039{
Linus Walleija0b66a72020-03-29 16:04:05 +02002040 struct gpio_desc *desc = gpiochip_get_desc(gc, hwnum);
Linus Walleijd377f562019-07-16 11:11:45 +02002041 int ret;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002042
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002043 if (IS_ERR(desc)) {
Linus Walleija0b66a72020-03-29 16:04:05 +02002044 chip_err(gc, "failed to get GPIO descriptor\n");
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002045 return desc;
2046 }
2047
Linus Walleijd377f562019-07-16 11:11:45 +02002048 ret = gpiod_request_commit(desc, label);
2049 if (ret < 0)
2050 return ERR_PTR(ret);
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002051
Linus Walleijd377f562019-07-16 11:11:45 +02002052 ret = gpiod_configure_flags(desc, label, lflags, dflags);
2053 if (ret) {
Linus Walleija0b66a72020-03-29 16:04:05 +02002054 chip_err(gc, "setup of own GPIO %s failed\n", label);
Linus Walleij21abf102018-09-04 13:31:45 +02002055 gpiod_free_commit(desc);
Linus Walleijd377f562019-07-16 11:11:45 +02002056 return ERR_PTR(ret);
Linus Walleij21abf102018-09-04 13:31:45 +02002057 }
2058
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002059 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002060}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002061EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002062
2063/**
2064 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2065 * @desc: GPIO descriptor to free
2066 *
2067 * Function frees the given GPIO requested previously with
2068 * gpiochip_request_own_desc().
2069 */
2070void gpiochip_free_own_desc(struct gpio_desc *desc)
2071{
2072 if (desc)
Linus Walleijfac9d882017-09-26 20:58:28 +02002073 gpiod_free_commit(desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002074}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002075EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08002076
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002077/*
2078 * Drivers MUST set GPIO direction before making get/set calls. In
David Brownelld2876d02008-02-04 22:28:20 -08002079 * some cases this is done in early boot, before IRQs are enabled.
2080 *
2081 * As a rule these aren't called more than once (except for drivers
2082 * using the open-drain emulation idiom) so these are natural places
2083 * to accumulate extra debugging checks. Note that we can't (yet)
2084 * rely on gpio_request() having been called beforehand.
2085 */
2086
Bartosz Golaszewskid99f8872020-02-03 14:03:37 +01002087static int gpio_do_set_config(struct gpio_chip *gc, unsigned int offset,
Bartosz Golaszewski62adc6f2020-02-03 14:16:16 +01002088 unsigned long config)
Thomas Petazzoni71479782019-02-07 17:28:56 +01002089{
Bartosz Golaszewskid90f3682019-12-24 13:06:58 +01002090 if (!gc->set_config)
2091 return -ENOTSUPP;
Thomas Petazzoni71479782019-02-07 17:28:56 +01002092
Bartosz Golaszewski62adc6f2020-02-03 14:16:16 +01002093 return gc->set_config(gc, offset, config);
Thomas Petazzoni71479782019-02-07 17:28:56 +01002094}
2095
Andy Shevchenko0c4d8662020-11-09 22:53:20 +02002096static int gpio_set_config_with_argument(struct gpio_desc *desc,
2097 enum pin_config_param mode,
2098 u32 argument)
Bartosz Golaszewskid99f8872020-02-03 14:03:37 +01002099{
Linus Walleija0b66a72020-03-29 16:04:05 +02002100 struct gpio_chip *gc = desc->gdev->chip;
Bartosz Golaszewski91b4ea52020-02-03 14:03:47 +01002101 unsigned long config;
Andy Shevchenko0c4d8662020-11-09 22:53:20 +02002102
2103 config = pinconf_to_config_packed(mode, argument);
2104 return gpio_do_set_config(gc, gpio_chip_hwgpio(desc), config);
2105}
2106
2107static int gpio_set_config(struct gpio_desc *desc, enum pin_config_param mode)
2108{
Andy Shevchenko13daf482020-11-09 22:53:16 +02002109 unsigned int arg;
Bartosz Golaszewskid99f8872020-02-03 14:03:37 +01002110
2111 switch (mode) {
Bartosz Golaszewskid99f8872020-02-03 14:03:37 +01002112 case PIN_CONFIG_BIAS_PULL_DOWN:
2113 case PIN_CONFIG_BIAS_PULL_UP:
2114 arg = 1;
2115 break;
2116
2117 default:
2118 arg = 0;
Andy Shevchenko6900fad2020-11-09 22:53:17 +02002119 break;
Bartosz Golaszewskid99f8872020-02-03 14:03:37 +01002120 }
2121
Andy Shevchenko0c4d8662020-11-09 22:53:20 +02002122 return gpio_set_config_with_argument(desc, mode, arg);
Bartosz Golaszewskid99f8872020-02-03 14:03:37 +01002123}
2124
Geert Uytterhoeven5f4bf172020-03-25 11:04:39 +01002125static int gpio_set_bias(struct gpio_desc *desc)
Kent Gibson2148ad72019-11-05 10:04:25 +08002126{
Andy Shevchenko9ef62932020-10-09 21:43:59 +03002127 enum pin_config_param bias;
2128 int ret;
Kent Gibson2148ad72019-11-05 10:04:25 +08002129
2130 if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
2131 bias = PIN_CONFIG_BIAS_DISABLE;
2132 else if (test_bit(FLAG_PULL_UP, &desc->flags))
2133 bias = PIN_CONFIG_BIAS_PULL_UP;
2134 else if (test_bit(FLAG_PULL_DOWN, &desc->flags))
2135 bias = PIN_CONFIG_BIAS_PULL_DOWN;
Andy Shevchenko9ef62932020-10-09 21:43:59 +03002136 else
2137 return 0;
Kent Gibson2148ad72019-11-05 10:04:25 +08002138
Andy Shevchenko9ef62932020-10-09 21:43:59 +03002139 ret = gpio_set_config(desc, bias);
2140 if (ret != -ENOTSUPP)
2141 return ret;
2142
Kent Gibson2148ad72019-11-05 10:04:25 +08002143 return 0;
2144}
2145
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002146/**
2147 * gpiod_direction_input - set the GPIO direction to input
2148 * @desc: GPIO to set to input
2149 *
2150 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2151 * be called safely on it.
2152 *
2153 * Return 0 in case of success, else an error code.
2154 */
2155int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002156{
Linus Walleija0b66a72020-03-29 16:04:05 +02002157 struct gpio_chip *gc;
Linus Walleijd377f562019-07-16 11:11:45 +02002158 int ret = 0;
David Brownelld2876d02008-02-04 22:28:20 -08002159
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002160 VALIDATE_DESC(desc);
Linus Walleija0b66a72020-03-29 16:04:05 +02002161 gc = desc->gdev->chip;
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002162
Linus Walleije48d1942018-09-25 09:54:14 +02002163 /*
2164 * It is legal to have no .get() and .direction_input() specified if
2165 * the chip is output-only, but you can't specify .direction_input()
2166 * and not support the .get() operation, that doesn't make sense.
2167 */
Linus Walleija0b66a72020-03-29 16:04:05 +02002168 if (!gc->get && gc->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01002169 gpiod_warn(desc,
Linus Walleije48d1942018-09-25 09:54:14 +02002170 "%s: missing get() but have direction_input()\n",
2171 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002172 return -EIO;
2173 }
2174
Linus Walleije48d1942018-09-25 09:54:14 +02002175 /*
2176 * If we have a .direction_input() callback, things are simple,
2177 * just call it. Else we are some input-only chip so try to check the
2178 * direction (if .get_direction() is supported) else we silently
2179 * assume we are in input mode after this.
2180 */
Linus Walleija0b66a72020-03-29 16:04:05 +02002181 if (gc->direction_input) {
2182 ret = gc->direction_input(gc, gpio_chip_hwgpio(desc));
2183 } else if (gc->get_direction &&
2184 (gc->get_direction(gc, gpio_chip_hwgpio(desc)) != 1)) {
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02002185 gpiod_warn(desc,
Linus Walleije48d1942018-09-25 09:54:14 +02002186 "%s: missing direction_input() operation and line is output\n",
2187 __func__);
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02002188 return -EIO;
2189 }
Kent Gibson2148ad72019-11-05 10:04:25 +08002190 if (ret == 0) {
David Brownelld2876d02008-02-04 22:28:20 -08002191 clear_bit(FLAG_IS_OUT, &desc->flags);
Geert Uytterhoeven5f4bf172020-03-25 11:04:39 +01002192 ret = gpio_set_bias(desc);
Kent Gibson2148ad72019-11-05 10:04:25 +08002193 }
Thomas Petazzonid4499912019-02-07 17:28:58 +01002194
Linus Walleijd377f562019-07-16 11:11:45 +02002195 trace_gpio_direction(desc_to_gpio(desc), 1, ret);
Alexandre Courbotd82da792014-07-22 16:17:43 +09002196
Linus Walleijd377f562019-07-16 11:11:45 +02002197 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08002198}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002199EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002200
Linus Walleijfac9d882017-09-26 20:58:28 +02002201static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08002202{
Linus Walleijc663e5f2016-03-22 10:51:16 +01002203 struct gpio_chip *gc = desc->gdev->chip;
Linus Walleijad177312016-11-13 23:02:44 +01002204 int val = !!value;
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02002205 int ret = 0;
David Brownelld2876d02008-02-04 22:28:20 -08002206
Linus Walleije48d1942018-09-25 09:54:14 +02002207 /*
2208 * It's OK not to specify .direction_output() if the gpiochip is
2209 * output-only, but if there is then not even a .set() operation it
2210 * is pretty tricky to drive the output line.
2211 */
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02002212 if (!gc->set && !gc->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01002213 gpiod_warn(desc,
Linus Walleije48d1942018-09-25 09:54:14 +02002214 "%s: missing set() and direction_output() operations\n",
2215 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002216 return -EIO;
2217 }
2218
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02002219 if (gc->direction_output) {
2220 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
2221 } else {
Linus Walleije48d1942018-09-25 09:54:14 +02002222 /* Check that we are in output mode if we can */
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02002223 if (gc->get_direction &&
2224 gc->get_direction(gc, gpio_chip_hwgpio(desc))) {
2225 gpiod_warn(desc,
2226 "%s: missing direction_output() operation\n",
2227 __func__);
2228 return -EIO;
2229 }
Linus Walleije48d1942018-09-25 09:54:14 +02002230 /*
2231 * If we can't actively set the direction, we are some
2232 * output-only chip, so just drive the output as desired.
2233 */
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02002234 gc->set(gc, gpio_chip_hwgpio(desc), val);
2235 }
2236
Linus Walleijc663e5f2016-03-22 10:51:16 +01002237 if (!ret)
David Brownelld2876d02008-02-04 22:28:20 -08002238 set_bit(FLAG_IS_OUT, &desc->flags);
Linus Walleijad177312016-11-13 23:02:44 +01002239 trace_gpio_value(desc_to_gpio(desc), 0, val);
Linus Walleijc663e5f2016-03-22 10:51:16 +01002240 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2241 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08002242}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002243
2244/**
2245 * gpiod_direction_output_raw - set the GPIO direction to output
2246 * @desc: GPIO to set to output
2247 * @value: initial output value of the GPIO
2248 *
2249 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2250 * be called safely on it. The initial value of the output must be specified
2251 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2252 *
2253 * Return 0 in case of success, else an error code.
2254 */
2255int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2256{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002257 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02002258 return gpiod_direction_output_raw_commit(desc, value);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002259}
2260EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2261
2262/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05302263 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002264 * @desc: GPIO to set to output
2265 * @value: initial output value of the GPIO
2266 *
2267 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2268 * be called safely on it. The initial value of the output must be specified
2269 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2270 * account.
2271 *
2272 * Return 0 in case of success, else an error code.
2273 */
2274int gpiod_direction_output(struct gpio_desc *desc, int value)
2275{
Linus Walleij02e47982017-09-26 21:20:23 +02002276 int ret;
2277
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002278 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002279 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2280 value = !value;
Linus Walleijad177312016-11-13 23:02:44 +01002281 else
2282 value = !!value;
Linus Walleij02e47982017-09-26 21:20:23 +02002283
Hans Verkuil4e9439d2018-09-08 11:23:16 +02002284 /* GPIOs used for enabled IRQs shall not be set as output */
2285 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags) &&
2286 test_bit(FLAG_IRQ_IS_ENABLED, &desc->flags)) {
Linus Walleij02e47982017-09-26 21:20:23 +02002287 gpiod_err(desc,
2288 "%s: tried to set a GPIO tied to an IRQ as output\n",
2289 __func__);
2290 return -EIO;
2291 }
2292
2293 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2294 /* First see if we can enable open drain in hardware */
Geert Uytterhoeven83522352020-03-25 11:04:38 +01002295 ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_DRAIN);
Linus Walleij02e47982017-09-26 21:20:23 +02002296 if (!ret)
2297 goto set_output_value;
2298 /* Emulate open drain by not actively driving the line high */
Bartosz Golaszewskie7352442019-10-01 11:44:53 +02002299 if (value) {
2300 ret = gpiod_direction_input(desc);
2301 goto set_output_flag;
2302 }
Linus Walleij02e47982017-09-26 21:20:23 +02002303 }
2304 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
Geert Uytterhoeven83522352020-03-25 11:04:38 +01002305 ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_SOURCE);
Linus Walleij02e47982017-09-26 21:20:23 +02002306 if (!ret)
2307 goto set_output_value;
2308 /* Emulate open source by not actively driving the line low */
Bartosz Golaszewskie7352442019-10-01 11:44:53 +02002309 if (!value) {
2310 ret = gpiod_direction_input(desc);
2311 goto set_output_flag;
2312 }
Linus Walleij02e47982017-09-26 21:20:23 +02002313 } else {
Geert Uytterhoeven83522352020-03-25 11:04:38 +01002314 gpio_set_config(desc, PIN_CONFIG_DRIVE_PUSH_PULL);
Linus Walleij02e47982017-09-26 21:20:23 +02002315 }
2316
2317set_output_value:
Geert Uytterhoeven5f4bf172020-03-25 11:04:39 +01002318 ret = gpio_set_bias(desc);
Kent Gibson2821ae52019-11-05 10:04:26 +08002319 if (ret)
2320 return ret;
Linus Walleijfac9d882017-09-26 20:58:28 +02002321 return gpiod_direction_output_raw_commit(desc, value);
Bartosz Golaszewskie7352442019-10-01 11:44:53 +02002322
2323set_output_flag:
2324 /*
2325 * When emulating open-source or open-drain functionalities by not
2326 * actively driving the line (setting mode to input) we still need to
2327 * set the IS_OUT flag or otherwise we won't be able to set the line
2328 * value anymore.
2329 */
2330 if (ret == 0)
2331 set_bit(FLAG_IS_OUT, &desc->flags);
2332 return ret;
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002333}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002334EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08002335
Felipe Balbic4b5be92010-05-26 14:42:23 -07002336/**
Geert Uytterhoeven8ced32f2020-03-24 14:56:50 +01002337 * gpiod_set_config - sets @config for a GPIO
2338 * @desc: descriptor of the GPIO for which to set the configuration
2339 * @config: Same packed config format as generic pinconf
2340 *
2341 * Returns:
2342 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2343 * configuration.
2344 */
2345int gpiod_set_config(struct gpio_desc *desc, unsigned long config)
2346{
Linus Walleija0b66a72020-03-29 16:04:05 +02002347 struct gpio_chip *gc;
Geert Uytterhoeven8ced32f2020-03-24 14:56:50 +01002348
2349 VALIDATE_DESC(desc);
Linus Walleija0b66a72020-03-29 16:04:05 +02002350 gc = desc->gdev->chip;
Geert Uytterhoeven8ced32f2020-03-24 14:56:50 +01002351
Linus Walleija0b66a72020-03-29 16:04:05 +02002352 return gpio_do_set_config(gc, gpio_chip_hwgpio(desc), config);
Geert Uytterhoeven8ced32f2020-03-24 14:56:50 +01002353}
2354EXPORT_SYMBOL_GPL(gpiod_set_config);
2355
2356/**
Thierry Reding950d55f52017-07-24 16:57:22 +02002357 * gpiod_set_debounce - sets @debounce time for a GPIO
2358 * @desc: descriptor of the GPIO for which to set debounce time
2359 * @debounce: debounce time in microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02002360 *
Thierry Reding950d55f52017-07-24 16:57:22 +02002361 * Returns:
2362 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2363 * debounce time.
Felipe Balbic4b5be92010-05-26 14:42:23 -07002364 */
Andy Shevchenko13daf482020-11-09 22:53:16 +02002365int gpiod_set_debounce(struct gpio_desc *desc, unsigned int debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07002366{
Geert Uytterhoeven8ced32f2020-03-24 14:56:50 +01002367 unsigned long config;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002368
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002369 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
Geert Uytterhoeven8ced32f2020-03-24 14:56:50 +01002370 return gpiod_set_config(desc, config);
Felipe Balbic4b5be92010-05-26 14:42:23 -07002371}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002372EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002373
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002374/**
Andrew Jefferye10f72b2017-11-30 14:25:24 +10302375 * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
2376 * @desc: descriptor of the GPIO for which to configure persistence
2377 * @transitory: True to lose state on suspend or reset, false for persistence
2378 *
2379 * Returns:
2380 * 0 on success, otherwise a negative error code.
2381 */
2382int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
2383{
Linus Walleija0b66a72020-03-29 16:04:05 +02002384 struct gpio_chip *gc;
Andrew Jefferye10f72b2017-11-30 14:25:24 +10302385 unsigned long packed;
2386 int gpio;
2387 int rc;
2388
Vladimir Zapolskiy156dd392017-12-21 18:37:35 +02002389 VALIDATE_DESC(desc);
Andrew Jefferye10f72b2017-11-30 14:25:24 +10302390 /*
2391 * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
2392 * persistence state.
2393 */
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +02002394 assign_bit(FLAG_TRANSITORY, &desc->flags, transitory);
Andrew Jefferye10f72b2017-11-30 14:25:24 +10302395
2396 /* If the driver supports it, set the persistence state now */
Linus Walleija0b66a72020-03-29 16:04:05 +02002397 gc = desc->gdev->chip;
2398 if (!gc->set_config)
Andrew Jefferye10f72b2017-11-30 14:25:24 +10302399 return 0;
2400
2401 packed = pinconf_to_config_packed(PIN_CONFIG_PERSIST_STATE,
2402 !transitory);
2403 gpio = gpio_chip_hwgpio(desc);
Linus Walleija0b66a72020-03-29 16:04:05 +02002404 rc = gpio_do_set_config(gc, gpio, packed);
Andrew Jefferye10f72b2017-11-30 14:25:24 +10302405 if (rc == -ENOTSUPP) {
2406 dev_dbg(&desc->gdev->dev, "Persistence not supported for GPIO %d\n",
2407 gpio);
2408 return 0;
2409 }
2410
2411 return rc;
2412}
2413EXPORT_SYMBOL_GPL(gpiod_set_transitory);
2414
2415/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002416 * gpiod_is_active_low - test whether a GPIO is active-low or not
2417 * @desc: the gpio descriptor to test
2418 *
2419 * Returns 1 if the GPIO is active-low, 0 otherwise.
2420 */
2421int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002422{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002423 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002424 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002425}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002426EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08002427
Michał Mirosławd3a5bcb2019-12-11 03:40:55 +01002428/**
2429 * gpiod_toggle_active_low - toggle whether a GPIO is active-low or not
2430 * @desc: the gpio descriptor to change
2431 */
2432void gpiod_toggle_active_low(struct gpio_desc *desc)
2433{
2434 VALIDATE_DESC_VOID(desc);
2435 change_bit(FLAG_ACTIVE_LOW, &desc->flags);
2436}
2437EXPORT_SYMBOL_GPL(gpiod_toggle_active_low);
2438
David Brownelld2876d02008-02-04 22:28:20 -08002439/* I/O calls are only valid after configuration completed; the relevant
2440 * "is this a valid GPIO" error checks should already have been done.
2441 *
2442 * "Get" operations are often inlinable as reading a pin value register,
2443 * and masking the relevant bit in that register.
2444 *
2445 * When "set" operations are inlinable, they involve writing that mask to
2446 * one register to set a low value, or a different register to set it high.
2447 * Otherwise locking is needed, so there may be little value to inlining.
2448 *
2449 *------------------------------------------------------------------------
2450 *
2451 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2452 * have requested the GPIO. That can include implicit requesting by
2453 * a direction setting call. Marking a gpio as requested locks its chip
2454 * in memory, guaranteeing that these table lookups need no more locking
2455 * and that gpiochip_remove() will fail.
2456 *
2457 * REVISIT when debugging, consider adding some instrumentation to ensure
2458 * that the GPIO was actually requested.
2459 */
2460
Linus Walleijfac9d882017-09-26 20:58:28 +02002461static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002462{
Linus Walleija0b66a72020-03-29 16:04:05 +02002463 struct gpio_chip *gc;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002464 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002465 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002466
Linus Walleija0b66a72020-03-29 16:04:05 +02002467 gc = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002468 offset = gpio_chip_hwgpio(desc);
Linus Walleija0b66a72020-03-29 16:04:05 +02002469 value = gc->get ? gc->get(gc, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01002470 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002471 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002472 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002473}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002474
Linus Walleija0b66a72020-03-29 16:04:05 +02002475static int gpio_chip_get_multiple(struct gpio_chip *gc,
Lukas Wunnereec1d562017-10-12 12:40:10 +02002476 unsigned long *mask, unsigned long *bits)
2477{
Linus Walleija0b66a72020-03-29 16:04:05 +02002478 if (gc->get_multiple) {
2479 return gc->get_multiple(gc, mask, bits);
2480 } else if (gc->get) {
Lukas Wunnereec1d562017-10-12 12:40:10 +02002481 int i, value;
2482
Linus Walleija0b66a72020-03-29 16:04:05 +02002483 for_each_set_bit(i, mask, gc->ngpio) {
2484 value = gc->get(gc, i);
Lukas Wunnereec1d562017-10-12 12:40:10 +02002485 if (value < 0)
2486 return value;
2487 __assign_bit(i, bits, value);
2488 }
2489 return 0;
2490 }
2491 return -EIO;
2492}
2493
2494int gpiod_get_array_value_complex(bool raw, bool can_sleep,
2495 unsigned int array_size,
2496 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02002497 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02002498 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02002499{
Linus Walleijd377f562019-07-16 11:11:45 +02002500 int ret, i = 0;
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02002501
2502 /*
2503 * Validate array_info against desc_array and its size.
2504 * It should immediately follow desc_array if both
2505 * have been obtained from the same gpiod_get_array() call.
2506 */
2507 if (array_info && array_info->desc == desc_array &&
2508 array_size <= array_info->size &&
2509 (void *)array_info == desc_array + array_info->size) {
2510 if (!can_sleep)
2511 WARN_ON(array_info->chip->can_sleep);
2512
Linus Walleijd377f562019-07-16 11:11:45 +02002513 ret = gpio_chip_get_multiple(array_info->chip,
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02002514 array_info->get_mask,
2515 value_bitmap);
Linus Walleijd377f562019-07-16 11:11:45 +02002516 if (ret)
2517 return ret;
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02002518
2519 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
2520 bitmap_xor(value_bitmap, value_bitmap,
2521 array_info->invert_mask, array_size);
2522
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02002523 i = find_first_zero_bit(array_info->get_mask, array_size);
Andy Shevchenkoae66eca2020-06-30 12:21:46 +03002524 if (i == array_size)
2525 return 0;
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02002526 } else {
2527 array_info = NULL;
2528 }
Lukas Wunnereec1d562017-10-12 12:40:10 +02002529
2530 while (i < array_size) {
Linus Walleija0b66a72020-03-29 16:04:05 +02002531 struct gpio_chip *gc = desc_array[i]->gdev->chip;
Laura Abbott30277432018-05-21 10:57:07 -07002532 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
2533 unsigned long *mask, *bits;
Lukas Wunnereec1d562017-10-12 12:40:10 +02002534 int first, j, ret;
2535
Linus Walleija0b66a72020-03-29 16:04:05 +02002536 if (likely(gc->ngpio <= FASTPATH_NGPIO)) {
Laura Abbott30277432018-05-21 10:57:07 -07002537 mask = fastpath;
2538 } else {
Linus Walleija0b66a72020-03-29 16:04:05 +02002539 mask = kmalloc_array(2 * BITS_TO_LONGS(gc->ngpio),
Laura Abbott30277432018-05-21 10:57:07 -07002540 sizeof(*mask),
2541 can_sleep ? GFP_KERNEL : GFP_ATOMIC);
2542 if (!mask)
2543 return -ENOMEM;
2544 }
2545
Linus Walleija0b66a72020-03-29 16:04:05 +02002546 bits = mask + BITS_TO_LONGS(gc->ngpio);
2547 bitmap_zero(mask, gc->ngpio);
Laura Abbott30277432018-05-21 10:57:07 -07002548
Lukas Wunnereec1d562017-10-12 12:40:10 +02002549 if (!can_sleep)
Linus Walleija0b66a72020-03-29 16:04:05 +02002550 WARN_ON(gc->can_sleep);
Lukas Wunnereec1d562017-10-12 12:40:10 +02002551
2552 /* collect all inputs belonging to the same chip */
2553 first = i;
Lukas Wunnereec1d562017-10-12 12:40:10 +02002554 do {
2555 const struct gpio_desc *desc = desc_array[i];
2556 int hwgpio = gpio_chip_hwgpio(desc);
2557
2558 __set_bit(hwgpio, mask);
2559 i++;
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02002560
2561 if (array_info)
Janusz Krzysztofik35ae7f92018-09-24 01:53:35 +02002562 i = find_next_zero_bit(array_info->get_mask,
2563 array_size, i);
Lukas Wunnereec1d562017-10-12 12:40:10 +02002564 } while ((i < array_size) &&
Linus Walleija0b66a72020-03-29 16:04:05 +02002565 (desc_array[i]->gdev->chip == gc));
Lukas Wunnereec1d562017-10-12 12:40:10 +02002566
Linus Walleija0b66a72020-03-29 16:04:05 +02002567 ret = gpio_chip_get_multiple(gc, mask, bits);
Laura Abbott30277432018-05-21 10:57:07 -07002568 if (ret) {
2569 if (mask != fastpath)
2570 kfree(mask);
Lukas Wunnereec1d562017-10-12 12:40:10 +02002571 return ret;
Laura Abbott30277432018-05-21 10:57:07 -07002572 }
Lukas Wunnereec1d562017-10-12 12:40:10 +02002573
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02002574 for (j = first; j < i; ) {
Lukas Wunnereec1d562017-10-12 12:40:10 +02002575 const struct gpio_desc *desc = desc_array[j];
2576 int hwgpio = gpio_chip_hwgpio(desc);
2577 int value = test_bit(hwgpio, bits);
2578
2579 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2580 value = !value;
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02002581 __assign_bit(j, value_bitmap, value);
Lukas Wunnereec1d562017-10-12 12:40:10 +02002582 trace_gpio_value(desc_to_gpio(desc), 1, value);
Janusz Krzysztofik799d5eb2018-09-29 14:20:22 +02002583 j++;
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02002584
2585 if (array_info)
Janusz Krzysztofik35ae7f92018-09-24 01:53:35 +02002586 j = find_next_zero_bit(array_info->get_mask, i,
2587 j);
Lukas Wunnereec1d562017-10-12 12:40:10 +02002588 }
Laura Abbott30277432018-05-21 10:57:07 -07002589
2590 if (mask != fastpath)
2591 kfree(mask);
Lukas Wunnereec1d562017-10-12 12:40:10 +02002592 }
2593 return 0;
2594}
2595
David Brownelld2876d02008-02-04 22:28:20 -08002596/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002597 * gpiod_get_raw_value() - return a gpio's raw value
2598 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08002599 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002600 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002601 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002602 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02002603 * This function can be called from contexts where we cannot sleep, and will
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002604 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002605 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002606int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002607{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002608 VALIDATE_DESC(desc);
Geert Uytterhoeven3285170f2019-07-01 16:27:38 +02002609 /* Should be using gpiod_get_raw_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002610 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02002611 return gpiod_get_raw_value_commit(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002612}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002613EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002614
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002615/**
2616 * gpiod_get_value() - return a gpio's value
2617 * @desc: gpio whose value will be returned
2618 *
2619 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002620 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002621 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02002622 * This function can be called from contexts where we cannot sleep, and will
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002623 * complain if the GPIO chip functions potentially sleep.
2624 */
2625int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002626{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002627 int value;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002628
2629 VALIDATE_DESC(desc);
Geert Uytterhoeven3285170f2019-07-01 16:27:38 +02002630 /* Should be using gpiod_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002631 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002632
Linus Walleijfac9d882017-09-26 20:58:28 +02002633 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002634 if (value < 0)
2635 return value;
2636
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002637 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2638 value = !value;
2639
2640 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002641}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002642EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08002643
Lukas Wunnereec1d562017-10-12 12:40:10 +02002644/**
2645 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02002646 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02002647 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02002648 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02002649 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02002650 *
2651 * Read the raw values of the GPIOs, i.e. the values of the physical lines
2652 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
2653 * else an error code.
2654 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02002655 * This function can be called from contexts where we cannot sleep,
Lukas Wunnereec1d562017-10-12 12:40:10 +02002656 * and it will complain if the GPIO chip functions potentially sleep.
2657 */
2658int gpiod_get_raw_array_value(unsigned int array_size,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02002659 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02002660 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02002661 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02002662{
2663 if (!desc_array)
2664 return -EINVAL;
2665 return gpiod_get_array_value_complex(true, false, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02002666 desc_array, array_info,
2667 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02002668}
2669EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
2670
2671/**
2672 * gpiod_get_array_value() - read values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02002673 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02002674 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02002675 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02002676 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02002677 *
2678 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2679 * into account. Return 0 in case of success, else an error code.
2680 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02002681 * This function can be called from contexts where we cannot sleep,
Lukas Wunnereec1d562017-10-12 12:40:10 +02002682 * and it will complain if the GPIO chip functions potentially sleep.
2683 */
2684int gpiod_get_array_value(unsigned int array_size,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02002685 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02002686 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02002687 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02002688{
2689 if (!desc_array)
2690 return -EINVAL;
2691 return gpiod_get_array_value_complex(false, false, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02002692 desc_array, array_info,
2693 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02002694}
2695EXPORT_SYMBOL_GPL(gpiod_get_array_value);
2696
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302697/*
Linus Walleijfac9d882017-09-26 20:58:28 +02002698 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002699 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002700 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302701 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002702static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302703{
Linus Walleijd377f562019-07-16 11:11:45 +02002704 int ret = 0;
Linus Walleija0b66a72020-03-29 16:04:05 +02002705 struct gpio_chip *gc = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002706 int offset = gpio_chip_hwgpio(desc);
2707
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302708 if (value) {
Linus Walleija0b66a72020-03-29 16:04:05 +02002709 ret = gc->direction_input(gc, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302710 } else {
Linus Walleija0b66a72020-03-29 16:04:05 +02002711 ret = gc->direction_output(gc, offset, 0);
Linus Walleijd377f562019-07-16 11:11:45 +02002712 if (!ret)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002713 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302714 }
Linus Walleijd377f562019-07-16 11:11:45 +02002715 trace_gpio_direction(desc_to_gpio(desc), value, ret);
2716 if (ret < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002717 gpiod_err(desc,
2718 "%s: Error in set_value for open drain err %d\n",
Linus Walleijd377f562019-07-16 11:11:45 +02002719 __func__, ret);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302720}
2721
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302722/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002723 * _gpio_set_open_source_value() - Set the open source gpio's value.
2724 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002725 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302726 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002727static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302728{
Linus Walleijd377f562019-07-16 11:11:45 +02002729 int ret = 0;
Linus Walleija0b66a72020-03-29 16:04:05 +02002730 struct gpio_chip *gc = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002731 int offset = gpio_chip_hwgpio(desc);
2732
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302733 if (value) {
Linus Walleija0b66a72020-03-29 16:04:05 +02002734 ret = gc->direction_output(gc, offset, 1);
Linus Walleijd377f562019-07-16 11:11:45 +02002735 if (!ret)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002736 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302737 } else {
Linus Walleija0b66a72020-03-29 16:04:05 +02002738 ret = gc->direction_input(gc, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302739 }
Linus Walleijd377f562019-07-16 11:11:45 +02002740 trace_gpio_direction(desc_to_gpio(desc), !value, ret);
2741 if (ret < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002742 gpiod_err(desc,
2743 "%s: Error in set_value for open source err %d\n",
Linus Walleijd377f562019-07-16 11:11:45 +02002744 __func__, ret);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302745}
2746
Linus Walleijfac9d882017-09-26 20:58:28 +02002747static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08002748{
Linus Walleija0b66a72020-03-29 16:04:05 +02002749 struct gpio_chip *gc;
David Brownelld2876d02008-02-04 22:28:20 -08002750
Linus Walleija0b66a72020-03-29 16:04:05 +02002751 gc = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002752 trace_gpio_value(desc_to_gpio(desc), 0, value);
Linus Walleija0b66a72020-03-29 16:04:05 +02002753 gc->set(gc, gpio_chip_hwgpio(desc), value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002754}
2755
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002756/*
2757 * set multiple outputs on the same chip;
2758 * use the chip's set_multiple function if available;
2759 * otherwise set the outputs sequentially;
Linus Walleija0b66a72020-03-29 16:04:05 +02002760 * @chip: the GPIO chip we operate on
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002761 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
2762 * defines which outputs are to be changed
2763 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
2764 * defines the values the outputs specified by mask are to be set to
2765 */
Linus Walleija0b66a72020-03-29 16:04:05 +02002766static void gpio_chip_set_multiple(struct gpio_chip *gc,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002767 unsigned long *mask, unsigned long *bits)
2768{
Linus Walleija0b66a72020-03-29 16:04:05 +02002769 if (gc->set_multiple) {
2770 gc->set_multiple(gc, mask, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002771 } else {
Andy Shevchenko5e4e6fb2017-01-03 19:01:17 +02002772 unsigned int i;
2773
2774 /* set outputs if the corresponding mask bit is set */
Linus Walleija0b66a72020-03-29 16:04:05 +02002775 for_each_set_bit(i, mask, gc->ngpio)
2776 gc->set(gc, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002777 }
2778}
2779
Laura Abbott30277432018-05-21 10:57:07 -07002780int gpiod_set_array_value_complex(bool raw, bool can_sleep,
Geert Uytterhoeven3c940662018-09-27 13:38:10 +02002781 unsigned int array_size,
2782 struct gpio_desc **desc_array,
2783 struct gpio_array *array_info,
2784 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002785{
2786 int i = 0;
2787
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02002788 /*
2789 * Validate array_info against desc_array and its size.
2790 * It should immediately follow desc_array if both
2791 * have been obtained from the same gpiod_get_array() call.
2792 */
2793 if (array_info && array_info->desc == desc_array &&
2794 array_size <= array_info->size &&
2795 (void *)array_info == desc_array + array_info->size) {
2796 if (!can_sleep)
2797 WARN_ON(array_info->chip->can_sleep);
2798
2799 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
2800 bitmap_xor(value_bitmap, value_bitmap,
2801 array_info->invert_mask, array_size);
2802
2803 gpio_chip_set_multiple(array_info->chip, array_info->set_mask,
2804 value_bitmap);
2805
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02002806 i = find_first_zero_bit(array_info->set_mask, array_size);
Andy Shevchenkoae66eca2020-06-30 12:21:46 +03002807 if (i == array_size)
2808 return 0;
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02002809 } else {
2810 array_info = NULL;
2811 }
2812
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002813 while (i < array_size) {
Linus Walleija0b66a72020-03-29 16:04:05 +02002814 struct gpio_chip *gc = desc_array[i]->gdev->chip;
Laura Abbott30277432018-05-21 10:57:07 -07002815 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
2816 unsigned long *mask, *bits;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002817 int count = 0;
2818
Linus Walleija0b66a72020-03-29 16:04:05 +02002819 if (likely(gc->ngpio <= FASTPATH_NGPIO)) {
Laura Abbott30277432018-05-21 10:57:07 -07002820 mask = fastpath;
2821 } else {
Linus Walleija0b66a72020-03-29 16:04:05 +02002822 mask = kmalloc_array(2 * BITS_TO_LONGS(gc->ngpio),
Laura Abbott30277432018-05-21 10:57:07 -07002823 sizeof(*mask),
2824 can_sleep ? GFP_KERNEL : GFP_ATOMIC);
2825 if (!mask)
2826 return -ENOMEM;
2827 }
2828
Linus Walleija0b66a72020-03-29 16:04:05 +02002829 bits = mask + BITS_TO_LONGS(gc->ngpio);
2830 bitmap_zero(mask, gc->ngpio);
Laura Abbott30277432018-05-21 10:57:07 -07002831
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002832 if (!can_sleep)
Linus Walleija0b66a72020-03-29 16:04:05 +02002833 WARN_ON(gc->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002834
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002835 do {
2836 struct gpio_desc *desc = desc_array[i];
2837 int hwgpio = gpio_chip_hwgpio(desc);
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02002838 int value = test_bit(i, value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002839
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02002840 /*
2841 * Pins applicable for fast input but not for
2842 * fast output processing may have been already
2843 * inverted inside the fast path, skip them.
2844 */
2845 if (!raw && !(array_info &&
2846 test_bit(i, array_info->invert_mask)) &&
2847 test_bit(FLAG_ACTIVE_LOW, &desc->flags))
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002848 value = !value;
2849 trace_gpio_value(desc_to_gpio(desc), 0, value);
2850 /*
2851 * collect all normal outputs belonging to the same chip
2852 * open drain and open source outputs are set individually
2853 */
Linus Walleij02e47982017-09-26 21:20:23 +02002854 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02002855 gpio_set_open_drain_value_commit(desc, value);
Linus Walleij02e47982017-09-26 21:20:23 +02002856 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02002857 gpio_set_open_source_value_commit(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002858 } else {
2859 __set_bit(hwgpio, mask);
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +02002860 __assign_bit(hwgpio, bits, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002861 count++;
2862 }
2863 i++;
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02002864
2865 if (array_info)
Janusz Krzysztofik35ae7f92018-09-24 01:53:35 +02002866 i = find_next_zero_bit(array_info->set_mask,
2867 array_size, i);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002868 } while ((i < array_size) &&
Linus Walleija0b66a72020-03-29 16:04:05 +02002869 (desc_array[i]->gdev->chip == gc));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002870 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002871 if (count != 0)
Linus Walleija0b66a72020-03-29 16:04:05 +02002872 gpio_chip_set_multiple(gc, mask, bits);
Laura Abbott30277432018-05-21 10:57:07 -07002873
2874 if (mask != fastpath)
2875 kfree(mask);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002876 }
Laura Abbott30277432018-05-21 10:57:07 -07002877 return 0;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002878}
2879
David Brownelld2876d02008-02-04 22:28:20 -08002880/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002881 * gpiod_set_raw_value() - assign a gpio's raw value
2882 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08002883 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002884 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002885 * Set the raw value of the GPIO, i.e. the value of its physical line without
2886 * regard for its ACTIVE_LOW status.
2887 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02002888 * This function can be called from contexts where we cannot sleep, and will
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002889 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002890 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002891void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002892{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002893 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven3285170f2019-07-01 16:27:38 +02002894 /* Should be using gpiod_set_raw_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002895 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02002896 gpiod_set_raw_value_commit(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002897}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002898EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002899
2900/**
Geert Uytterhoeven1e77fc82018-01-09 19:08:21 +01002901 * gpiod_set_value_nocheck() - set a GPIO line value without checking
2902 * @desc: the descriptor to set the value on
2903 * @value: value to set
2904 *
2905 * This sets the value of a GPIO line backing a descriptor, applying
2906 * different semantic quirks like active low and open drain/source
2907 * handling.
2908 */
2909static void gpiod_set_value_nocheck(struct gpio_desc *desc, int value)
2910{
2911 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2912 value = !value;
2913 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2914 gpio_set_open_drain_value_commit(desc, value);
2915 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2916 gpio_set_open_source_value_commit(desc, value);
2917 else
2918 gpiod_set_raw_value_commit(desc, value);
2919}
2920
2921/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002922 * gpiod_set_value() - assign a gpio's value
2923 * @desc: gpio whose value will be assigned
2924 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002925 *
Linus Walleij02e47982017-09-26 21:20:23 +02002926 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
2927 * OPEN_DRAIN and OPEN_SOURCE flags into account.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002928 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02002929 * This function can be called from contexts where we cannot sleep, and will
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002930 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002931 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002932void gpiod_set_value(struct gpio_desc *desc, int value)
2933{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002934 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven3285170f2019-07-01 16:27:38 +02002935 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002936 WARN_ON(desc->gdev->chip->can_sleep);
Geert Uytterhoeven1e77fc82018-01-09 19:08:21 +01002937 gpiod_set_value_nocheck(desc, value);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002938}
2939EXPORT_SYMBOL_GPL(gpiod_set_value);
2940
2941/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002942 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02002943 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002944 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02002945 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02002946 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002947 *
2948 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2949 * without regard for their ACTIVE_LOW status.
2950 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02002951 * This function can be called from contexts where we cannot sleep, and will
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002952 * complain if the GPIO chip functions potentially sleep.
2953 */
Laura Abbott30277432018-05-21 10:57:07 -07002954int gpiod_set_raw_array_value(unsigned int array_size,
Geert Uytterhoeven3c940662018-09-27 13:38:10 +02002955 struct gpio_desc **desc_array,
2956 struct gpio_array *array_info,
2957 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002958{
2959 if (!desc_array)
Laura Abbott30277432018-05-21 10:57:07 -07002960 return -EINVAL;
2961 return gpiod_set_array_value_complex(true, false, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02002962 desc_array, array_info, value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002963}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002964EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002965
2966/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002967 * gpiod_set_array_value() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02002968 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002969 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02002970 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02002971 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002972 *
2973 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2974 * into account.
2975 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02002976 * This function can be called from contexts where we cannot sleep, and will
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002977 * complain if the GPIO chip functions potentially sleep.
2978 */
Geert Uytterhoevencf9af0d2018-09-27 13:38:09 +02002979int gpiod_set_array_value(unsigned int array_size,
2980 struct gpio_desc **desc_array,
2981 struct gpio_array *array_info,
2982 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002983{
2984 if (!desc_array)
Geert Uytterhoevencf9af0d2018-09-27 13:38:09 +02002985 return -EINVAL;
2986 return gpiod_set_array_value_complex(false, false, array_size,
2987 desc_array, array_info,
2988 value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002989}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002990EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002991
2992/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002993 * gpiod_cansleep() - report whether gpio value access may sleep
2994 * @desc: gpio to check
2995 *
2996 */
2997int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002998{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002999 VALIDATE_DESC(desc);
3000 return desc->gdev->chip->can_sleep;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003001}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003002EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08003003
David Brownell0f6d5042008-10-15 22:03:14 -07003004/**
Linus Walleij90b39402e2018-06-01 13:21:27 +02003005 * gpiod_set_consumer_name() - set the consumer name for the descriptor
3006 * @desc: gpio to set the consumer name on
3007 * @name: the new consumer name
3008 */
Muchun Song18534df2018-11-01 21:12:50 +08003009int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name)
Linus Walleij90b39402e2018-06-01 13:21:27 +02003010{
Muchun Song18534df2018-11-01 21:12:50 +08003011 VALIDATE_DESC(desc);
3012 if (name) {
3013 name = kstrdup_const(name, GFP_KERNEL);
3014 if (!name)
3015 return -ENOMEM;
3016 }
3017
3018 kfree_const(desc->label);
3019 desc_set_label(desc, name);
3020
3021 return 0;
Linus Walleij90b39402e2018-06-01 13:21:27 +02003022}
3023EXPORT_SYMBOL_GPL(gpiod_set_consumer_name);
3024
3025/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003026 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
3027 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07003028 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003029 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
3030 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07003031 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003032int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07003033{
Linus Walleija0b66a72020-03-29 16:04:05 +02003034 struct gpio_chip *gc;
Linus Walleij4c37ce82016-05-02 13:13:10 +02003035 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07003036
Linus Walleij79bb71b2016-06-15 22:57:38 +02003037 /*
3038 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
3039 * requires this function to not return zero on an invalid descriptor
3040 * but rather a negative error number.
3041 */
Linus Walleijbfbbe442016-06-16 11:55:55 +02003042 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
Linus Walleij79bb71b2016-06-15 22:57:38 +02003043 return -EINVAL;
3044
Linus Walleija0b66a72020-03-29 16:04:05 +02003045 gc = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003046 offset = gpio_chip_hwgpio(desc);
Linus Walleija0b66a72020-03-29 16:04:05 +02003047 if (gc->to_irq) {
3048 int retirq = gc->to_irq(gc, offset);
Linus Walleij4c37ce82016-05-02 13:13:10 +02003049
3050 /* Zero means NO_IRQ */
3051 if (!retirq)
3052 return -ENXIO;
3053
3054 return retirq;
3055 }
3056 return -ENXIO;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003057}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003058EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003059
Linus Walleijd468bf92013-09-24 11:54:38 +02003060/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003061 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Linus Walleija0b66a72020-03-29 16:04:05 +02003062 * @gc: the chip the GPIO to lock belongs to
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003063 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02003064 *
3065 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08003066 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08003067 */
Linus Walleija0b66a72020-03-29 16:04:05 +02003068int gpiochip_lock_as_irq(struct gpio_chip *gc, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08003069{
Linus Walleij9c102802016-05-25 10:56:03 +02003070 struct gpio_desc *desc;
Linus Walleijd468bf92013-09-24 11:54:38 +02003071
Linus Walleija0b66a72020-03-29 16:04:05 +02003072 desc = gpiochip_get_desc(gc, offset);
Linus Walleij9c102802016-05-25 10:56:03 +02003073 if (IS_ERR(desc))
3074 return PTR_ERR(desc);
3075
Linus Walleij60f83392016-11-12 15:01:09 +01003076 /*
3077 * If it's fast: flush the direction setting if something changed
3078 * behind our back
3079 */
Linus Walleija0b66a72020-03-29 16:04:05 +02003080 if (!gc->can_sleep && gc->get_direction) {
Andy Shevchenko80956792018-07-09 21:47:21 +03003081 int dir = gpiod_get_direction(desc);
Linus Walleij9c102802016-05-25 10:56:03 +02003082
Andy Shevchenko36b31272018-07-03 03:38:31 +03003083 if (dir < 0) {
Linus Walleija0b66a72020-03-29 16:04:05 +02003084 chip_err(gc, "%s: cannot get GPIO direction\n",
Andy Shevchenko36b31272018-07-03 03:38:31 +03003085 __func__);
3086 return dir;
3087 }
Linus Walleij9c102802016-05-25 10:56:03 +02003088 }
3089
Linus Walleije9bdf7e2020-05-27 16:07:58 +02003090 /* To be valid for IRQ the line needs to be input or open drain */
3091 if (test_bit(FLAG_IS_OUT, &desc->flags) &&
3092 !test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
Linus Walleija0b66a72020-03-29 16:04:05 +02003093 chip_err(gc,
Andy Shevchenkob1911712018-07-03 03:39:03 +03003094 "%s: tried to flag a GPIO set as output for IRQ\n",
3095 __func__);
Linus Walleijd468bf92013-09-24 11:54:38 +02003096 return -EIO;
3097 }
3098
Linus Walleij9c102802016-05-25 10:56:03 +02003099 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
Hans Verkuil4e9439d2018-09-08 11:23:16 +02003100 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
Linus Walleij3940c342016-11-14 00:09:07 +01003101
3102 /*
3103 * If the consumer has not set up a label (such as when the
3104 * IRQ is referenced from .to_irq()) we set up a label here
3105 * so it is clear this is used as an interrupt.
3106 */
3107 if (!desc->label)
3108 desc_set_label(desc, "interrupt");
3109
Linus Walleijd468bf92013-09-24 11:54:38 +02003110 return 0;
3111}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003112EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02003113
Linus Walleijd468bf92013-09-24 11:54:38 +02003114/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003115 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Linus Walleija0b66a72020-03-29 16:04:05 +02003116 * @gc: the chip the GPIO to lock belongs to
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003117 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02003118 *
3119 * This is used directly by GPIO drivers that want to indicate
3120 * that a certain GPIO is no longer used exclusively for IRQ.
3121 */
Linus Walleija0b66a72020-03-29 16:04:05 +02003122void gpiochip_unlock_as_irq(struct gpio_chip *gc, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02003123{
Linus Walleij3940c342016-11-14 00:09:07 +01003124 struct gpio_desc *desc;
3125
Linus Walleija0b66a72020-03-29 16:04:05 +02003126 desc = gpiochip_get_desc(gc, offset);
Linus Walleij3940c342016-11-14 00:09:07 +01003127 if (IS_ERR(desc))
Linus Walleijd468bf92013-09-24 11:54:38 +02003128 return;
3129
Linus Walleij3940c342016-11-14 00:09:07 +01003130 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
Hans Verkuil4e9439d2018-09-08 11:23:16 +02003131 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
Linus Walleij3940c342016-11-14 00:09:07 +01003132
3133 /* If we only had this marking, erase it */
3134 if (desc->label && !strcmp(desc->label, "interrupt"))
3135 desc_set_label(desc, NULL);
Linus Walleijd468bf92013-09-24 11:54:38 +02003136}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003137EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02003138
Linus Walleija0b66a72020-03-29 16:04:05 +02003139void gpiochip_disable_irq(struct gpio_chip *gc, unsigned int offset)
Hans Verkuil4e9439d2018-09-08 11:23:16 +02003140{
Linus Walleija0b66a72020-03-29 16:04:05 +02003141 struct gpio_desc *desc = gpiochip_get_desc(gc, offset);
Hans Verkuil4e9439d2018-09-08 11:23:16 +02003142
3143 if (!IS_ERR(desc) &&
3144 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags)))
3145 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3146}
3147EXPORT_SYMBOL_GPL(gpiochip_disable_irq);
3148
Linus Walleija0b66a72020-03-29 16:04:05 +02003149void gpiochip_enable_irq(struct gpio_chip *gc, unsigned int offset)
Hans Verkuil4e9439d2018-09-08 11:23:16 +02003150{
Linus Walleija0b66a72020-03-29 16:04:05 +02003151 struct gpio_desc *desc = gpiochip_get_desc(gc, offset);
Hans Verkuil4e9439d2018-09-08 11:23:16 +02003152
3153 if (!IS_ERR(desc) &&
3154 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) {
Linus Walleije9bdf7e2020-05-27 16:07:58 +02003155 /*
3156 * We must not be output when using IRQ UNLESS we are
3157 * open drain.
3158 */
3159 WARN_ON(test_bit(FLAG_IS_OUT, &desc->flags) &&
3160 !test_bit(FLAG_OPEN_DRAIN, &desc->flags));
Hans Verkuil4e9439d2018-09-08 11:23:16 +02003161 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
3162 }
3163}
3164EXPORT_SYMBOL_GPL(gpiochip_enable_irq);
3165
Linus Walleija0b66a72020-03-29 16:04:05 +02003166bool gpiochip_line_is_irq(struct gpio_chip *gc, unsigned int offset)
Linus Walleij6cee3822016-02-11 20:16:45 +01003167{
Linus Walleija0b66a72020-03-29 16:04:05 +02003168 if (offset >= gc->ngpio)
Linus Walleij6cee3822016-02-11 20:16:45 +01003169 return false;
3170
Linus Walleija0b66a72020-03-29 16:04:05 +02003171 return test_bit(FLAG_USED_AS_IRQ, &gc->gpiodev->descs[offset].flags);
Linus Walleij6cee3822016-02-11 20:16:45 +01003172}
3173EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
3174
Linus Walleija0b66a72020-03-29 16:04:05 +02003175int gpiochip_reqres_irq(struct gpio_chip *gc, unsigned int offset)
Hans Verkuil4e6b8232018-09-08 11:23:14 +02003176{
3177 int ret;
3178
Linus Walleija0b66a72020-03-29 16:04:05 +02003179 if (!try_module_get(gc->gpiodev->owner))
Hans Verkuil4e6b8232018-09-08 11:23:14 +02003180 return -ENODEV;
3181
Linus Walleija0b66a72020-03-29 16:04:05 +02003182 ret = gpiochip_lock_as_irq(gc, offset);
Hans Verkuil4e6b8232018-09-08 11:23:14 +02003183 if (ret) {
Linus Walleija0b66a72020-03-29 16:04:05 +02003184 chip_err(gc, "unable to lock HW IRQ %u for IRQ\n", offset);
3185 module_put(gc->gpiodev->owner);
Hans Verkuil4e6b8232018-09-08 11:23:14 +02003186 return ret;
3187 }
3188 return 0;
3189}
3190EXPORT_SYMBOL_GPL(gpiochip_reqres_irq);
3191
Linus Walleija0b66a72020-03-29 16:04:05 +02003192void gpiochip_relres_irq(struct gpio_chip *gc, unsigned int offset)
Hans Verkuil4e6b8232018-09-08 11:23:14 +02003193{
Linus Walleija0b66a72020-03-29 16:04:05 +02003194 gpiochip_unlock_as_irq(gc, offset);
3195 module_put(gc->gpiodev->owner);
Hans Verkuil4e6b8232018-09-08 11:23:14 +02003196}
3197EXPORT_SYMBOL_GPL(gpiochip_relres_irq);
3198
Linus Walleija0b66a72020-03-29 16:04:05 +02003199bool gpiochip_line_is_open_drain(struct gpio_chip *gc, unsigned int offset)
Linus Walleij143b65d2016-02-16 15:41:42 +01003200{
Linus Walleija0b66a72020-03-29 16:04:05 +02003201 if (offset >= gc->ngpio)
Linus Walleij143b65d2016-02-16 15:41:42 +01003202 return false;
3203
Linus Walleija0b66a72020-03-29 16:04:05 +02003204 return test_bit(FLAG_OPEN_DRAIN, &gc->gpiodev->descs[offset].flags);
Linus Walleij143b65d2016-02-16 15:41:42 +01003205}
3206EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
3207
Linus Walleija0b66a72020-03-29 16:04:05 +02003208bool gpiochip_line_is_open_source(struct gpio_chip *gc, unsigned int offset)
Linus Walleij143b65d2016-02-16 15:41:42 +01003209{
Linus Walleija0b66a72020-03-29 16:04:05 +02003210 if (offset >= gc->ngpio)
Linus Walleij143b65d2016-02-16 15:41:42 +01003211 return false;
3212
Linus Walleija0b66a72020-03-29 16:04:05 +02003213 return test_bit(FLAG_OPEN_SOURCE, &gc->gpiodev->descs[offset].flags);
Linus Walleij143b65d2016-02-16 15:41:42 +01003214}
3215EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
3216
Linus Walleija0b66a72020-03-29 16:04:05 +02003217bool gpiochip_line_is_persistent(struct gpio_chip *gc, unsigned int offset)
Charles Keepax05f479b2017-05-23 15:47:29 +01003218{
Linus Walleija0b66a72020-03-29 16:04:05 +02003219 if (offset >= gc->ngpio)
Charles Keepax05f479b2017-05-23 15:47:29 +01003220 return false;
3221
Linus Walleija0b66a72020-03-29 16:04:05 +02003222 return !test_bit(FLAG_TRANSITORY, &gc->gpiodev->descs[offset].flags);
Charles Keepax05f479b2017-05-23 15:47:29 +01003223}
3224EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
3225
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003226/**
3227 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
3228 * @desc: gpio whose value will be returned
3229 *
3230 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003231 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003232 *
3233 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003234 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003235int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08003236{
David Brownelld2876d02008-02-04 22:28:20 -08003237 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003238 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003239 return gpiod_get_raw_value_commit(desc);
David Brownelld2876d02008-02-04 22:28:20 -08003240}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003241EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003242
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003243/**
3244 * gpiod_get_value_cansleep() - return a gpio's value
3245 * @desc: gpio whose value will be returned
3246 *
3247 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003248 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003249 *
3250 * This function is to be called from contexts that can sleep.
3251 */
3252int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003253{
David Brownelld2876d02008-02-04 22:28:20 -08003254 int value;
David Brownelld2876d02008-02-04 22:28:20 -08003255
3256 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003257 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003258 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003259 if (value < 0)
3260 return value;
3261
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003262 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3263 value = !value;
3264
David Brownelld2876d02008-02-04 22:28:20 -08003265 return value;
3266}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003267EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003268
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003269/**
Lukas Wunnereec1d562017-10-12 12:40:10 +02003270 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003271 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02003272 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003273 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003274 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02003275 *
3276 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3277 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3278 * else an error code.
3279 *
3280 * This function is to be called from contexts that can sleep.
3281 */
3282int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
3283 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003284 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003285 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02003286{
3287 might_sleep_if(extra_checks);
3288 if (!desc_array)
3289 return -EINVAL;
3290 return gpiod_get_array_value_complex(true, true, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003291 desc_array, array_info,
3292 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003293}
3294EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
3295
3296/**
3297 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003298 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02003299 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003300 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003301 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02003302 *
3303 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3304 * into account. Return 0 in case of success, else an error code.
3305 *
3306 * This function is to be called from contexts that can sleep.
3307 */
3308int gpiod_get_array_value_cansleep(unsigned int array_size,
3309 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003310 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003311 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02003312{
3313 might_sleep_if(extra_checks);
3314 if (!desc_array)
3315 return -EINVAL;
3316 return gpiod_get_array_value_complex(false, true, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003317 desc_array, array_info,
3318 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003319}
3320EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
3321
3322/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003323 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
3324 * @desc: gpio whose value will be assigned
3325 * @value: value to assign
3326 *
3327 * Set the raw value of the GPIO, i.e. the value of its physical line without
3328 * regard for its ACTIVE_LOW status.
3329 *
3330 * This function is to be called from contexts that can sleep.
3331 */
3332void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003333{
David Brownelld2876d02008-02-04 22:28:20 -08003334 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003335 VALIDATE_DESC_VOID(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003336 gpiod_set_raw_value_commit(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003337}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003338EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003339
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003340/**
3341 * gpiod_set_value_cansleep() - assign a gpio's value
3342 * @desc: gpio whose value will be assigned
3343 * @value: value to assign
3344 *
3345 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3346 * account
3347 *
3348 * This function is to be called from contexts that can sleep.
3349 */
3350void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003351{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003352 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003353 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1e77fc82018-01-09 19:08:21 +01003354 gpiod_set_value_nocheck(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08003355}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003356EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08003357
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003358/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003359 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003360 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003361 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003362 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003363 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003364 *
3365 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3366 * without regard for their ACTIVE_LOW status.
3367 *
3368 * This function is to be called from contexts that can sleep.
3369 */
Laura Abbott30277432018-05-21 10:57:07 -07003370int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
Geert Uytterhoeven3c940662018-09-27 13:38:10 +02003371 struct gpio_desc **desc_array,
3372 struct gpio_array *array_info,
3373 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003374{
3375 might_sleep_if(extra_checks);
3376 if (!desc_array)
Laura Abbott30277432018-05-21 10:57:07 -07003377 return -EINVAL;
3378 return gpiod_set_array_value_complex(true, true, array_size, desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003379 array_info, value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003380}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003381EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003382
3383/**
Dmitry Torokhov3946d182017-08-14 21:59:55 -07003384 * gpiod_add_lookup_tables() - register GPIO device consumers
3385 * @tables: list of tables of consumers to register
3386 * @n: number of tables in the list
3387 */
3388void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
3389{
3390 unsigned int i;
3391
3392 mutex_lock(&gpio_lookup_lock);
3393
3394 for (i = 0; i < n; i++)
3395 list_add_tail(&tables[i]->list, &gpio_lookup_list);
3396
3397 mutex_unlock(&gpio_lookup_lock);
3398}
3399
3400/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003401 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003402 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003403 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003404 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003405 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003406 *
3407 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3408 * into account.
3409 *
3410 * This function is to be called from contexts that can sleep.
3411 */
Geert Uytterhoevencf9af0d2018-09-27 13:38:09 +02003412int gpiod_set_array_value_cansleep(unsigned int array_size,
3413 struct gpio_desc **desc_array,
3414 struct gpio_array *array_info,
3415 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003416{
3417 might_sleep_if(extra_checks);
3418 if (!desc_array)
Geert Uytterhoevencf9af0d2018-09-27 13:38:09 +02003419 return -EINVAL;
3420 return gpiod_set_array_value_complex(false, true, array_size,
3421 desc_array, array_info,
3422 value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003423}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003424EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003425
3426/**
Alexandre Courbotad824782013-12-03 12:20:11 +09003427 * gpiod_add_lookup_table() - register GPIO device consumers
3428 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003429 */
Alexandre Courbotad824782013-12-03 12:20:11 +09003430void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003431{
3432 mutex_lock(&gpio_lookup_lock);
3433
Alexandre Courbotad824782013-12-03 12:20:11 +09003434 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003435
3436 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08003437}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02003438EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
David Brownelld2876d02008-02-04 22:28:20 -08003439
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05303440/**
3441 * gpiod_remove_lookup_table() - unregister GPIO device consumers
3442 * @table: table of consumers to unregister
3443 */
3444void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
3445{
3446 mutex_lock(&gpio_lookup_lock);
3447
3448 list_del(&table->list);
3449
3450 mutex_unlock(&gpio_lookup_lock);
3451}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02003452EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05303453
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02003454/**
3455 * gpiod_add_hogs() - register a set of GPIO hogs from machine code
3456 * @hogs: table of gpio hog entries with a zeroed sentinel at the end
3457 */
3458void gpiod_add_hogs(struct gpiod_hog *hogs)
3459{
Linus Walleija0b66a72020-03-29 16:04:05 +02003460 struct gpio_chip *gc;
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02003461 struct gpiod_hog *hog;
3462
3463 mutex_lock(&gpio_machine_hogs_mutex);
3464
3465 for (hog = &hogs[0]; hog->chip_label; hog++) {
3466 list_add_tail(&hog->list, &gpio_machine_hogs);
3467
3468 /*
3469 * The chip may have been registered earlier, so check if it
3470 * exists and, if so, try to hog the line now.
3471 */
Linus Walleija0b66a72020-03-29 16:04:05 +02003472 gc = find_chip_by_name(hog->chip_label);
3473 if (gc)
3474 gpiochip_machine_hog(gc, hog);
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02003475 }
3476
3477 mutex_unlock(&gpio_machine_hogs_mutex);
3478}
3479EXPORT_SYMBOL_GPL(gpiod_add_hogs);
3480
Alexandre Courbotad824782013-12-03 12:20:11 +09003481static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
3482{
3483 const char *dev_id = dev ? dev_name(dev) : NULL;
3484 struct gpiod_lookup_table *table;
3485
3486 mutex_lock(&gpio_lookup_lock);
3487
3488 list_for_each_entry(table, &gpio_lookup_list, list) {
3489 if (table->dev_id && dev_id) {
3490 /*
3491 * Valid strings on both ends, must be identical to have
3492 * a match
3493 */
3494 if (!strcmp(table->dev_id, dev_id))
3495 goto found;
3496 } else {
3497 /*
3498 * One of the pointers is NULL, so both must be to have
3499 * a match
3500 */
3501 if (dev_id == table->dev_id)
3502 goto found;
3503 }
3504 }
3505 table = NULL;
3506
3507found:
3508 mutex_unlock(&gpio_lookup_lock);
3509 return table;
3510}
3511
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003512static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Andy Shevchenkofed70262019-04-10 18:39:16 +03003513 unsigned int idx, unsigned long *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003514{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003515 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09003516 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003517 struct gpiod_lookup *p;
3518
Alexandre Courbotad824782013-12-03 12:20:11 +09003519 table = gpiod_find_lookup_table(dev);
3520 if (!table)
3521 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003522
Geert Uytterhoeven4c033b52020-05-11 16:52:54 +02003523 for (p = &table->table[0]; p->key; p++) {
Linus Walleija0b66a72020-03-29 16:04:05 +02003524 struct gpio_chip *gc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003525
Alexandre Courbotad824782013-12-03 12:20:11 +09003526 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003527 if (p->idx != idx)
3528 continue;
3529
Alexandre Courbotad824782013-12-03 12:20:11 +09003530 /* If the lookup entry has a con_id, require exact match */
3531 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
3532 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003533
Geert Uytterhoeven4c033b52020-05-11 16:52:54 +02003534 if (p->chip_hwnum == U16_MAX) {
3535 desc = gpio_name_to_desc(p->key);
3536 if (desc) {
3537 *flags = p->flags;
3538 return desc;
3539 }
3540
3541 dev_warn(dev, "cannot find GPIO line %s, deferring\n",
3542 p->key);
3543 return ERR_PTR(-EPROBE_DEFER);
3544 }
3545
3546 gc = find_chip_by_name(p->key);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003547
Linus Walleija0b66a72020-03-29 16:04:05 +02003548 if (!gc) {
Janusz Krzysztofik8853daf2018-07-04 00:18:19 +02003549 /*
3550 * As the lookup table indicates a chip with
Geert Uytterhoeven4c033b52020-05-11 16:52:54 +02003551 * p->key should exist, assume it may
Janusz Krzysztofik8853daf2018-07-04 00:18:19 +02003552 * still appear later and let the interested
3553 * consumer be probed again or let the Deferred
3554 * Probe infrastructure handle the error.
3555 */
3556 dev_warn(dev, "cannot find GPIO chip %s, deferring\n",
Geert Uytterhoeven4c033b52020-05-11 16:52:54 +02003557 p->key);
Janusz Krzysztofik8853daf2018-07-04 00:18:19 +02003558 return ERR_PTR(-EPROBE_DEFER);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003559 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003560
Linus Walleija0b66a72020-03-29 16:04:05 +02003561 if (gc->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003562 dev_err(dev,
Geert Uytterhoevend935bd52019-11-27 10:59:19 +01003563 "requested GPIO %u (%u) is out of range [0..%u] for chip %s\n",
Linus Walleija0b66a72020-03-29 16:04:05 +02003564 idx, p->chip_hwnum, gc->ngpio - 1,
3565 gc->label);
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003566 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09003567 }
3568
Linus Walleija0b66a72020-03-29 16:04:05 +02003569 desc = gpiochip_get_desc(gc, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09003570 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003571
3572 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09003573 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003574
3575 return desc;
3576}
3577
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003578static int platform_gpio_count(struct device *dev, const char *con_id)
3579{
3580 struct gpiod_lookup_table *table;
3581 struct gpiod_lookup *p;
3582 unsigned int count = 0;
3583
3584 table = gpiod_find_lookup_table(dev);
3585 if (!table)
3586 return -ENOENT;
3587
Geert Uytterhoeven4c033b52020-05-11 16:52:54 +02003588 for (p = &table->table[0]; p->key; p++) {
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003589 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3590 (!con_id && !p->con_id))
3591 count++;
3592 }
3593 if (!count)
3594 return -ENOENT;
3595
3596 return count;
3597}
3598
3599/**
Dmitry Torokhov13949fa2019-09-12 20:22:39 -07003600 * fwnode_gpiod_get_index - obtain a GPIO from firmware node
3601 * @fwnode: handle of the firmware node
3602 * @con_id: function within the GPIO consumer
3603 * @index: index of the GPIO to obtain for the consumer
3604 * @flags: GPIO initialization flags
3605 * @label: label to attach to the requested GPIO
3606 *
3607 * This function can be used for drivers that get their configuration
3608 * from opaque firmware.
3609 *
3610 * The function properly finds the corresponding GPIO using whatever is the
3611 * underlying firmware interface and then makes sure that the GPIO
3612 * descriptor is requested before it is returned to the caller.
3613 *
3614 * Returns:
3615 * On successful request the GPIO pin is configured in accordance with
3616 * provided @flags.
3617 *
3618 * In case of error an ERR_PTR() is returned.
3619 */
3620struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
3621 const char *con_id, int index,
3622 enum gpiod_flags flags,
3623 const char *label)
3624{
3625 struct gpio_desc *desc;
3626 char prop_name[32]; /* 32 is max size of property name */
3627 unsigned int i;
3628
3629 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
3630 if (con_id)
3631 snprintf(prop_name, sizeof(prop_name), "%s-%s",
3632 con_id, gpio_suffixes[i]);
3633 else
3634 snprintf(prop_name, sizeof(prop_name), "%s",
3635 gpio_suffixes[i]);
3636
3637 desc = fwnode_get_named_gpiod(fwnode, prop_name, index, flags,
3638 label);
3639 if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT))
3640 break;
3641 }
3642
3643 return desc;
3644}
3645EXPORT_SYMBOL_GPL(fwnode_gpiod_get_index);
3646
3647/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003648 * gpiod_count - return the number of GPIOs associated with a device / function
3649 * or -ENOENT if no GPIO has been assigned to the requested function
3650 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3651 * @con_id: function within the GPIO consumer
3652 */
3653int gpiod_count(struct device *dev, const char *con_id)
3654{
3655 int count = -ENOENT;
3656
3657 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
Linus Walleijf626d6d2019-07-17 09:10:01 +02003658 count = of_gpio_get_count(dev, con_id);
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003659 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
3660 count = acpi_gpio_count(dev, con_id);
3661
3662 if (count < 0)
3663 count = platform_gpio_count(dev, con_id);
3664
3665 return count;
3666}
3667EXPORT_SYMBOL_GPL(gpiod_count);
3668
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003669/**
Thierry Reding08791622014-04-25 16:54:22 +02003670 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09003671 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003672 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003673 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003674 *
3675 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003676 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07003677 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003678 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003679struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003680 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003681{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003682 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003683}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003684EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003685
3686/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02003687 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
3688 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3689 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003690 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02003691 *
3692 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
3693 * the requested function it will return NULL. This is convenient for drivers
3694 * that need to handle optional GPIOs.
3695 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003696struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003697 const char *con_id,
3698 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003699{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003700 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003701}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003702EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003703
Benoit Parrotf625d462015-02-02 11:44:44 -06003704
3705/**
3706 * gpiod_configure_flags - helper function to configure a given GPIO
3707 * @desc: gpio whose value will be assigned
3708 * @con_id: function within the GPIO consumer
Andy Shevchenkofed70262019-04-10 18:39:16 +03003709 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
3710 * of_find_gpio() or of_get_gpio_hog()
Benoit Parrotf625d462015-02-02 11:44:44 -06003711 * @dflags: gpiod_flags - optional GPIO initialization flags
3712 *
3713 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
3714 * requested function and/or index, or another IS_ERR() code if an error
3715 * occurred while trying to acquire the GPIO.
3716 */
Andy Shevchenkoc29fd9e2017-05-23 20:03:16 +03003717int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Johan Hovold85b03b32016-07-03 18:32:05 +02003718 unsigned long lflags, enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06003719{
Linus Walleijd377f562019-07-16 11:11:45 +02003720 int ret;
Benoit Parrotf625d462015-02-02 11:44:44 -06003721
Johan Hovold85b03b32016-07-03 18:32:05 +02003722 if (lflags & GPIO_ACTIVE_LOW)
3723 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
Linus Walleijf926dfc2017-09-10 19:26:22 +02003724
Johan Hovold85b03b32016-07-03 18:32:05 +02003725 if (lflags & GPIO_OPEN_DRAIN)
3726 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
Linus Walleijf926dfc2017-09-10 19:26:22 +02003727 else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) {
3728 /*
3729 * This enforces open drain mode from the consumer side.
3730 * This is necessary for some busses like I2C, but the lookup
3731 * should *REALLY* have specified them as open drain in the
3732 * first place, so print a little warning here.
3733 */
3734 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3735 gpiod_warn(desc,
3736 "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
3737 }
3738
Johan Hovold85b03b32016-07-03 18:32:05 +02003739 if (lflags & GPIO_OPEN_SOURCE)
3740 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303741
Thomas Petazzonid4499912019-02-07 17:28:58 +01003742 if ((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DOWN)) {
3743 gpiod_err(desc,
3744 "both pull-up and pull-down enabled, invalid configuration\n");
3745 return -EINVAL;
3746 }
3747
3748 if (lflags & GPIO_PULL_UP)
3749 set_bit(FLAG_PULL_UP, &desc->flags);
3750 else if (lflags & GPIO_PULL_DOWN)
3751 set_bit(FLAG_PULL_DOWN, &desc->flags);
3752
Linus Walleijd377f562019-07-16 11:11:45 +02003753 ret = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
3754 if (ret < 0)
3755 return ret;
Johan Hovold85b03b32016-07-03 18:32:05 +02003756
Benoit Parrotf625d462015-02-02 11:44:44 -06003757 /* No particular flag request, return here... */
3758 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
Geert Uytterhoeven262b9012020-04-24 16:14:32 +02003759 gpiod_dbg(desc, "no flags found for %s\n", con_id);
Benoit Parrotf625d462015-02-02 11:44:44 -06003760 return 0;
3761 }
3762
3763 /* Process flags */
3764 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
Linus Walleijd377f562019-07-16 11:11:45 +02003765 ret = gpiod_direction_output(desc,
Linus Walleijad177312016-11-13 23:02:44 +01003766 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
Benoit Parrotf625d462015-02-02 11:44:44 -06003767 else
Linus Walleijd377f562019-07-16 11:11:45 +02003768 ret = gpiod_direction_input(desc);
Benoit Parrotf625d462015-02-02 11:44:44 -06003769
Linus Walleijd377f562019-07-16 11:11:45 +02003770 return ret;
Benoit Parrotf625d462015-02-02 11:44:44 -06003771}
3772
Thierry Reding29a1f2332014-04-25 17:10:06 +02003773/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003774 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02003775 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003776 * @con_id: function within the GPIO consumer
3777 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003778 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003779 *
3780 * This variant of gpiod_get() allows to access GPIOs other than the first
3781 * defined one for functions that define several GPIOs.
3782 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003783 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
3784 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07003785 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003786 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003787struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003788 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003789 unsigned int idx,
3790 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003791{
Andy Shevchenko2d6c06f2019-04-10 18:39:17 +03003792 unsigned long lookupflags = GPIO_LOOKUP_FLAGS_DEFAULT;
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003793 struct gpio_desc *desc = NULL;
Linus Walleijd377f562019-07-16 11:11:45 +02003794 int ret;
Linus Walleij7d18f0a2018-01-16 08:29:50 +01003795 /* Maybe we have a device name, maybe not */
3796 const char *devname = dev ? dev_name(dev) : "?";
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003797
3798 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
3799
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003800 if (dev) {
3801 /* Using device tree? */
3802 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
3803 dev_dbg(dev, "using device tree for GPIO lookup\n");
3804 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
3805 } else if (ACPI_COMPANION(dev)) {
3806 dev_dbg(dev, "using ACPI for GPIO lookup\n");
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +03003807 desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags);
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003808 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003809 }
3810
3811 /*
3812 * Either we are not using DT or ACPI, or their lookup did not return
3813 * a result. In that case, use platform lookup as a fallback.
3814 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003815 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04003816 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003817 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003818 }
3819
3820 if (IS_ERR(desc)) {
Wang Dongsheng9d5a1f22018-02-27 00:12:13 -08003821 dev_dbg(dev, "No GPIO consumer %s found\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003822 return desc;
3823 }
3824
Linus Walleij7d18f0a2018-01-16 08:29:50 +01003825 /*
3826 * If a connection label was passed use that, else attempt to use
3827 * the device name as label
3828 */
Linus Walleijd377f562019-07-16 11:11:45 +02003829 ret = gpiod_request(desc, con_id ? con_id : devname);
3830 if (ret < 0) {
3831 if (ret == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE) {
Linus Walleijb0ce7b292018-10-12 14:54:12 +02003832 /*
3833 * This happens when there are several consumers for
3834 * the same GPIO line: we just return here without
3835 * further initialization. It is a bit if a hack.
3836 * This is necessary to support fixed regulators.
3837 *
3838 * FIXME: Make this more sane and safe.
3839 */
3840 dev_info(dev, "nonexclusive access to GPIO for %s\n",
3841 con_id ? con_id : devname);
3842 return desc;
3843 } else {
Linus Walleijd377f562019-07-16 11:11:45 +02003844 return ERR_PTR(ret);
Linus Walleijb0ce7b292018-10-12 14:54:12 +02003845 }
3846 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003847
Linus Walleijd377f562019-07-16 11:11:45 +02003848 ret = gpiod_configure_flags(desc, con_id, lookupflags, flags);
3849 if (ret < 0) {
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003850 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
3851 gpiod_put(desc);
Linus Walleijd377f562019-07-16 11:11:45 +02003852 return ERR_PTR(ret);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003853 }
3854
Kent Gibson6accc372020-07-08 12:15:51 +08003855 blocking_notifier_call_chain(&desc->gdev->notifier,
3856 GPIOLINE_CHANGED_REQUESTED, desc);
Bartosz Golaszewski9fefca72020-05-09 16:08:13 +02003857
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003858 return desc;
3859}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003860EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003861
3862/**
Mika Westerberg40b73182014-10-21 13:33:59 +02003863 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
3864 * @fwnode: handle of the firmware node
3865 * @propname: name of the firmware property representing the GPIO
Linus Walleij6392cca2017-12-29 02:07:54 +01003866 * @index: index of the GPIO to obtain for the consumer
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003867 * @dflags: GPIO initialization flags
Thierry Reding950d55f52017-07-24 16:57:22 +02003868 * @label: label to attach to the requested GPIO
Mika Westerberg40b73182014-10-21 13:33:59 +02003869 *
3870 * This function can be used for drivers that get their configuration
Linus Walleij6392cca2017-12-29 02:07:54 +01003871 * from opaque firmware.
Mika Westerberg40b73182014-10-21 13:33:59 +02003872 *
Linus Walleij6392cca2017-12-29 02:07:54 +01003873 * The function properly finds the corresponding GPIO using whatever is the
Mika Westerberg40b73182014-10-21 13:33:59 +02003874 * underlying firmware interface and then makes sure that the GPIO
3875 * descriptor is requested before it is returned to the caller.
3876 *
Thierry Reding950d55f52017-07-24 16:57:22 +02003877 * Returns:
Andy Shevchenkoff213782017-02-28 17:03:12 +02003878 * On successful request the GPIO pin is configured in accordance with
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003879 * provided @dflags.
3880 *
Mika Westerberg40b73182014-10-21 13:33:59 +02003881 * In case of error an ERR_PTR() is returned.
3882 */
3883struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
Boris Brezillon537b94d2017-02-02 14:53:11 +01003884 const char *propname, int index,
Alexander Steinb2987d72017-01-12 17:39:24 +01003885 enum gpiod_flags dflags,
3886 const char *label)
Mika Westerberg40b73182014-10-21 13:33:59 +02003887{
Andy Shevchenko2d6c06f2019-04-10 18:39:17 +03003888 unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
Mika Westerberg40b73182014-10-21 13:33:59 +02003889 struct gpio_desc *desc = ERR_PTR(-ENODEV);
Mika Westerberg40b73182014-10-21 13:33:59 +02003890 int ret;
3891
3892 if (!fwnode)
3893 return ERR_PTR(-EINVAL);
3894
3895 if (is_of_node(fwnode)) {
Linus Walleij6392cca2017-12-29 02:07:54 +01003896 desc = gpiod_get_from_of_node(to_of_node(fwnode),
3897 propname, index,
3898 dflags,
3899 label);
3900 return desc;
Mika Westerberg40b73182014-10-21 13:33:59 +02003901 } else if (is_acpi_node(fwnode)) {
3902 struct acpi_gpio_info info;
3903
Boris Brezillon537b94d2017-02-02 14:53:11 +01003904 desc = acpi_node_get_gpiod(fwnode, propname, index, &info);
Linus Walleij6392cca2017-12-29 02:07:54 +01003905 if (IS_ERR(desc))
3906 return desc;
3907
3908 acpi_gpio_update_gpiod_flags(&dflags, &info);
Andy Shevchenko606be342019-04-10 18:39:20 +03003909 acpi_gpio_update_gpiod_lookup_flags(&lflags, &info);
Mika Westerberg40b73182014-10-21 13:33:59 +02003910 }
3911
Linus Walleij6392cca2017-12-29 02:07:54 +01003912 /* Currently only ACPI takes this path */
Alexander Steinb2987d72017-01-12 17:39:24 +01003913 ret = gpiod_request(desc, label);
Johan Hovold85b03b32016-07-03 18:32:05 +02003914 if (ret)
3915 return ERR_PTR(ret);
3916
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003917 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
3918 if (ret < 0) {
3919 gpiod_put(desc);
3920 return ERR_PTR(ret);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003921 }
3922
Kent Gibson6accc372020-07-08 12:15:51 +08003923 blocking_notifier_call_chain(&desc->gdev->notifier,
3924 GPIOLINE_CHANGED_REQUESTED, desc);
Bartosz Golaszewski9fefca72020-05-09 16:08:13 +02003925
Mika Westerberg40b73182014-10-21 13:33:59 +02003926 return desc;
3927}
3928EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
3929
3930/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02003931 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
3932 * function
3933 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3934 * @con_id: function within the GPIO consumer
3935 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003936 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02003937 *
3938 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
3939 * specified index was assigned to the requested function it will return NULL.
3940 * This is convenient for drivers that need to handle optional GPIOs.
3941 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003942struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02003943 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003944 unsigned int index,
3945 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003946{
3947 struct gpio_desc *desc;
3948
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003949 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003950 if (IS_ERR(desc)) {
3951 if (PTR_ERR(desc) == -ENOENT)
3952 return NULL;
3953 }
3954
3955 return desc;
3956}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003957EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003958
3959/**
Benoit Parrotf625d462015-02-02 11:44:44 -06003960 * gpiod_hog - Hog the specified GPIO desc given the provided flags
3961 * @desc: gpio whose value will be assigned
3962 * @name: gpio line name
Andy Shevchenkofed70262019-04-10 18:39:16 +03003963 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
3964 * of_find_gpio() or of_get_gpio_hog()
Benoit Parrotf625d462015-02-02 11:44:44 -06003965 * @dflags: gpiod_flags - optional GPIO initialization flags
3966 */
3967int gpiod_hog(struct gpio_desc *desc, const char *name,
3968 unsigned long lflags, enum gpiod_flags dflags)
3969{
Linus Walleija0b66a72020-03-29 16:04:05 +02003970 struct gpio_chip *gc;
Benoit Parrotf625d462015-02-02 11:44:44 -06003971 struct gpio_desc *local_desc;
3972 int hwnum;
Linus Walleijd377f562019-07-16 11:11:45 +02003973 int ret;
Benoit Parrotf625d462015-02-02 11:44:44 -06003974
Linus Walleija0b66a72020-03-29 16:04:05 +02003975 gc = gpiod_to_chip(desc);
Benoit Parrotf625d462015-02-02 11:44:44 -06003976 hwnum = gpio_chip_hwgpio(desc);
3977
Linus Walleija0b66a72020-03-29 16:04:05 +02003978 local_desc = gpiochip_request_own_desc(gc, hwnum, name,
Linus Walleij5923ea62019-04-26 14:40:18 +02003979 lflags, dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -06003980 if (IS_ERR(local_desc)) {
Linus Walleijd377f562019-07-16 11:11:45 +02003981 ret = PTR_ERR(local_desc);
Laxman Dewanganc31a5712016-03-11 19:13:21 +05303982 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
Linus Walleija0b66a72020-03-29 16:04:05 +02003983 name, gc->label, hwnum, ret);
Linus Walleijd377f562019-07-16 11:11:45 +02003984 return ret;
Benoit Parrotf625d462015-02-02 11:44:44 -06003985 }
3986
Benoit Parrotf625d462015-02-02 11:44:44 -06003987 /* Mark GPIO as hogged so it can be identified and removed later */
3988 set_bit(FLAG_IS_HOGGED, &desc->flags);
3989
Geert Uytterhoeven262b9012020-04-24 16:14:32 +02003990 gpiod_info(desc, "hogged as %s%s\n",
Bartosz Golaszewskib27f3002019-11-13 14:16:29 +01003991 (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
3992 (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ?
3993 (dflags & GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low" : "");
Benoit Parrotf625d462015-02-02 11:44:44 -06003994
3995 return 0;
3996}
3997
3998/**
3999 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
Linus Walleija0b66a72020-03-29 16:04:05 +02004000 * @gc: gpio chip to act on
Benoit Parrotf625d462015-02-02 11:44:44 -06004001 */
Linus Walleija0b66a72020-03-29 16:04:05 +02004002static void gpiochip_free_hogs(struct gpio_chip *gc)
Benoit Parrotf625d462015-02-02 11:44:44 -06004003{
4004 int id;
4005
Linus Walleija0b66a72020-03-29 16:04:05 +02004006 for (id = 0; id < gc->ngpio; id++) {
4007 if (test_bit(FLAG_IS_HOGGED, &gc->gpiodev->descs[id].flags))
4008 gpiochip_free_own_desc(&gc->gpiodev->descs[id]);
Benoit Parrotf625d462015-02-02 11:44:44 -06004009 }
4010}
4011
4012/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004013 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
4014 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4015 * @con_id: function within the GPIO consumer
4016 * @flags: optional GPIO initialization flags
4017 *
4018 * This function acquires all the GPIOs defined under a given function.
4019 *
4020 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
4021 * no GPIO has been assigned to the requested function, or another IS_ERR()
4022 * code if an error occurred while trying to acquire the GPIOs.
4023 */
4024struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
4025 const char *con_id,
4026 enum gpiod_flags flags)
4027{
4028 struct gpio_desc *desc;
4029 struct gpio_descs *descs;
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004030 struct gpio_array *array_info = NULL;
Linus Walleija0b66a72020-03-29 16:04:05 +02004031 struct gpio_chip *gc;
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004032 int count, bitmap_size;
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004033
4034 count = gpiod_count(dev, con_id);
4035 if (count < 0)
4036 return ERR_PTR(count);
4037
Kees Cookacafe7e2018-05-08 13:45:50 -07004038 descs = kzalloc(struct_size(descs, desc, count), GFP_KERNEL);
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004039 if (!descs)
4040 return ERR_PTR(-ENOMEM);
4041
4042 for (descs->ndescs = 0; descs->ndescs < count; ) {
4043 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
4044 if (IS_ERR(desc)) {
4045 gpiod_put_array(descs);
4046 return ERR_CAST(desc);
4047 }
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004048
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004049 descs->desc[descs->ndescs] = desc;
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004050
Linus Walleija0b66a72020-03-29 16:04:05 +02004051 gc = gpiod_to_chip(desc);
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004052 /*
Janusz Krzysztofikc4c958a2018-09-24 01:53:36 +02004053 * If pin hardware number of array member 0 is also 0, select
4054 * its chip as a candidate for fast bitmap processing path.
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004055 */
Janusz Krzysztofikc4c958a2018-09-24 01:53:36 +02004056 if (descs->ndescs == 0 && gpio_chip_hwgpio(desc) == 0) {
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004057 struct gpio_descs *array;
4058
Linus Walleija0b66a72020-03-29 16:04:05 +02004059 bitmap_size = BITS_TO_LONGS(gc->ngpio > count ?
4060 gc->ngpio : count);
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004061
4062 array = kzalloc(struct_size(descs, desc, count) +
4063 struct_size(array_info, invert_mask,
4064 3 * bitmap_size), GFP_KERNEL);
4065 if (!array) {
4066 gpiod_put_array(descs);
4067 return ERR_PTR(-ENOMEM);
4068 }
4069
4070 memcpy(array, descs,
4071 struct_size(descs, desc, descs->ndescs + 1));
4072 kfree(descs);
4073
4074 descs = array;
4075 array_info = (void *)(descs->desc + count);
4076 array_info->get_mask = array_info->invert_mask +
4077 bitmap_size;
4078 array_info->set_mask = array_info->get_mask +
4079 bitmap_size;
4080
4081 array_info->desc = descs->desc;
4082 array_info->size = count;
Linus Walleija0b66a72020-03-29 16:04:05 +02004083 array_info->chip = gc;
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004084 bitmap_set(array_info->get_mask, descs->ndescs,
4085 count - descs->ndescs);
4086 bitmap_set(array_info->set_mask, descs->ndescs,
4087 count - descs->ndescs);
4088 descs->info = array_info;
4089 }
Janusz Krzysztofikc4c958a2018-09-24 01:53:36 +02004090 /* Unmark array members which don't belong to the 'fast' chip */
Linus Walleija0b66a72020-03-29 16:04:05 +02004091 if (array_info && array_info->chip != gc) {
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004092 __clear_bit(descs->ndescs, array_info->get_mask);
4093 __clear_bit(descs->ndescs, array_info->set_mask);
Janusz Krzysztofikc4c958a2018-09-24 01:53:36 +02004094 }
4095 /*
4096 * Detect array members which belong to the 'fast' chip
4097 * but their pins are not in hardware order.
4098 */
4099 else if (array_info &&
4100 gpio_chip_hwgpio(desc) != descs->ndescs) {
4101 /*
4102 * Don't use fast path if all array members processed so
4103 * far belong to the same chip as this one but its pin
4104 * hardware number is different from its array index.
4105 */
4106 if (bitmap_full(array_info->get_mask, descs->ndescs)) {
4107 array_info = NULL;
4108 } else {
4109 __clear_bit(descs->ndescs,
4110 array_info->get_mask);
4111 __clear_bit(descs->ndescs,
4112 array_info->set_mask);
4113 }
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004114 } else if (array_info) {
4115 /* Exclude open drain or open source from fast output */
Linus Walleija0b66a72020-03-29 16:04:05 +02004116 if (gpiochip_line_is_open_drain(gc, descs->ndescs) ||
4117 gpiochip_line_is_open_source(gc, descs->ndescs))
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004118 __clear_bit(descs->ndescs,
4119 array_info->set_mask);
4120 /* Identify 'fast' pins which require invertion */
4121 if (gpiod_is_active_low(desc))
4122 __set_bit(descs->ndescs,
4123 array_info->invert_mask);
4124 }
4125
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004126 descs->ndescs++;
4127 }
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004128 if (array_info)
4129 dev_dbg(dev,
4130 "GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n",
4131 array_info->chip->label, array_info->size,
4132 *array_info->get_mask, *array_info->set_mask,
4133 *array_info->invert_mask);
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004134 return descs;
4135}
4136EXPORT_SYMBOL_GPL(gpiod_get_array);
4137
4138/**
4139 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
4140 * function
4141 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4142 * @con_id: function within the GPIO consumer
4143 * @flags: optional GPIO initialization flags
4144 *
4145 * This is equivalent to gpiod_get_array(), except that when no GPIO was
4146 * assigned to the requested function it will return NULL.
4147 */
4148struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
4149 const char *con_id,
4150 enum gpiod_flags flags)
4151{
4152 struct gpio_descs *descs;
4153
4154 descs = gpiod_get_array(dev, con_id, flags);
Masahiro Yamada45586c72020-02-03 17:37:45 -08004155 if (PTR_ERR(descs) == -ENOENT)
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004156 return NULL;
4157
4158 return descs;
4159}
4160EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
4161
4162/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004163 * gpiod_put - dispose of a GPIO descriptor
4164 * @desc: GPIO descriptor to dispose of
4165 *
4166 * No descriptor can be used after gpiod_put() has been called on it.
4167 */
4168void gpiod_put(struct gpio_desc *desc)
4169{
Andy Shevchenko1d7765b2019-03-26 17:21:14 +02004170 if (desc)
4171 gpiod_free(desc);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004172}
4173EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08004174
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004175/**
4176 * gpiod_put_array - dispose of multiple GPIO descriptors
4177 * @descs: struct gpio_descs containing an array of descriptors
4178 */
4179void gpiod_put_array(struct gpio_descs *descs)
4180{
4181 unsigned int i;
4182
4183 for (i = 0; i < descs->ndescs; i++)
4184 gpiod_put(descs->desc[i]);
4185
4186 kfree(descs);
4187}
4188EXPORT_SYMBOL_GPL(gpiod_put_array);
4189
Linus Walleij3c702e92015-10-21 15:29:53 +02004190static int __init gpiolib_dev_init(void)
4191{
4192 int ret;
4193
4194 /* Register GPIO sysfs bus */
Andy Shevchenkob1911712018-07-03 03:39:03 +03004195 ret = bus_register(&gpio_bus_type);
Linus Walleij3c702e92015-10-21 15:29:53 +02004196 if (ret < 0) {
4197 pr_err("gpiolib: could not register GPIO bus type\n");
4198 return ret;
4199 }
4200
Geert Uytterhoevenddd88912019-11-27 09:42:47 +01004201 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, GPIOCHIP_NAME);
Linus Walleij3c702e92015-10-21 15:29:53 +02004202 if (ret < 0) {
4203 pr_err("gpiolib: failed to allocate char dev region\n");
4204 bus_unregister(&gpio_bus_type);
Geert Uytterhoeven63636d92020-02-20 14:01:49 +01004205 return ret;
Linus Walleij3c702e92015-10-21 15:29:53 +02004206 }
Geert Uytterhoeven63636d92020-02-20 14:01:49 +01004207
4208 gpiolib_initialized = true;
4209 gpiochip_setup_devs();
4210
David Gow8650b602020-04-24 21:46:55 -07004211#if IS_ENABLED(CONFIG_OF_DYNAMIC) && IS_ENABLED(CONFIG_OF_GPIO)
4212 WARN_ON(of_reconfig_notifier_register(&gpio_of_notifier));
4213#endif /* CONFIG_OF_DYNAMIC && CONFIG_OF_GPIO */
Geert Uytterhoeven63636d92020-02-20 14:01:49 +01004214
Linus Walleij3c702e92015-10-21 15:29:53 +02004215 return ret;
4216}
4217core_initcall(gpiolib_dev_init);
4218
David Brownelld2876d02008-02-04 22:28:20 -08004219#ifdef CONFIG_DEBUG_FS
4220
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004221static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
David Brownelld2876d02008-02-04 22:28:20 -08004222{
4223 unsigned i;
Linus Walleija0b66a72020-03-29 16:04:05 +02004224 struct gpio_chip *gc = gdev->chip;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004225 unsigned gpio = gdev->base;
4226 struct gpio_desc *gdesc = &gdev->descs[0];
Linus Walleij90fd2272018-10-01 23:06:17 +02004227 bool is_out;
4228 bool is_irq;
4229 bool active_low;
David Brownelld2876d02008-02-04 22:28:20 -08004230
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004231 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02004232 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
4233 if (gdesc->name) {
4234 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
4235 gpio, gdesc->name);
4236 }
David Brownelld2876d02008-02-04 22:28:20 -08004237 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02004238 }
David Brownelld2876d02008-02-04 22:28:20 -08004239
Alexandre Courbot372e7222013-02-03 01:29:29 +09004240 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08004241 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02004242 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Linus Walleij90fd2272018-10-01 23:06:17 +02004243 active_low = test_bit(FLAG_ACTIVE_LOW, &gdesc->flags);
4244 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s%s",
Markus Pargmannced433e2015-08-14 16:11:02 +02004245 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08004246 is_out ? "out" : "in ",
Linus Walleija0b66a72020-03-29 16:04:05 +02004247 gc->get ? (gc->get(gc, i) ? "hi" : "lo") : "? ",
Linus Walleij90fd2272018-10-01 23:06:17 +02004248 is_irq ? "IRQ " : "",
4249 active_low ? "ACTIVE LOW" : "");
David Brownelld2876d02008-02-04 22:28:20 -08004250 seq_printf(s, "\n");
4251 }
4252}
4253
Thierry Redingf9c4a312012-04-12 13:26:01 +02004254static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08004255{
Grant Likely362432a2013-02-09 09:41:49 +00004256 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02004257 struct gpio_device *gdev = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09004258 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02004259
4260 s->private = "";
4261
Grant Likely362432a2013-02-09 09:41:49 +00004262 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02004263 list_for_each_entry(gdev, &gpio_devices, list)
Grant Likely362432a2013-02-09 09:41:49 +00004264 if (index-- == 0) {
4265 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02004266 return gdev;
Grant Likely362432a2013-02-09 09:41:49 +00004267 }
4268 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09004269
4270 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02004271}
4272
4273static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
4274{
Grant Likely362432a2013-02-09 09:41:49 +00004275 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02004276 struct gpio_device *gdev = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02004277 void *ret = NULL;
4278
Grant Likely362432a2013-02-09 09:41:49 +00004279 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02004280 if (list_is_last(&gdev->list, &gpio_devices))
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09004281 ret = NULL;
4282 else
Linus Walleijff2b1352015-10-20 11:10:38 +02004283 ret = list_entry(gdev->list.next, struct gpio_device, list);
Grant Likely362432a2013-02-09 09:41:49 +00004284 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02004285
4286 s->private = "\n";
4287 ++*pos;
4288
4289 return ret;
4290}
4291
4292static void gpiolib_seq_stop(struct seq_file *s, void *v)
4293{
4294}
4295
4296static int gpiolib_seq_show(struct seq_file *s, void *v)
4297{
Linus Walleijff2b1352015-10-20 11:10:38 +02004298 struct gpio_device *gdev = v;
Linus Walleija0b66a72020-03-29 16:04:05 +02004299 struct gpio_chip *gc = gdev->chip;
Linus Walleijff2b1352015-10-20 11:10:38 +02004300 struct device *parent;
Thierry Redingf9c4a312012-04-12 13:26:01 +02004301
Linus Walleija0b66a72020-03-29 16:04:05 +02004302 if (!gc) {
Linus Walleijff2b1352015-10-20 11:10:38 +02004303 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
4304 dev_name(&gdev->dev));
4305 return 0;
4306 }
4307
4308 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
4309 dev_name(&gdev->dev),
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004310 gdev->base, gdev->base + gdev->ngpio - 1);
Linus Walleija0b66a72020-03-29 16:04:05 +02004311 parent = gc->parent;
Linus Walleijff2b1352015-10-20 11:10:38 +02004312 if (parent)
4313 seq_printf(s, ", parent: %s/%s",
4314 parent->bus ? parent->bus->name : "no-bus",
4315 dev_name(parent));
Linus Walleija0b66a72020-03-29 16:04:05 +02004316 if (gc->label)
4317 seq_printf(s, ", %s", gc->label);
4318 if (gc->can_sleep)
Thierry Redingf9c4a312012-04-12 13:26:01 +02004319 seq_printf(s, ", can sleep");
4320 seq_printf(s, ":\n");
4321
Linus Walleija0b66a72020-03-29 16:04:05 +02004322 if (gc->dbg_show)
4323 gc->dbg_show(s, gc);
Thierry Redingf9c4a312012-04-12 13:26:01 +02004324 else
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004325 gpiolib_dbg_show(s, gdev);
Thierry Redingf9c4a312012-04-12 13:26:01 +02004326
David Brownelld2876d02008-02-04 22:28:20 -08004327 return 0;
4328}
4329
Andy Shevchenko425c5b32020-09-10 13:19:35 +03004330static const struct seq_operations gpiolib_sops = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02004331 .start = gpiolib_seq_start,
4332 .next = gpiolib_seq_next,
4333 .stop = gpiolib_seq_stop,
4334 .show = gpiolib_seq_show,
4335};
Andy Shevchenko425c5b32020-09-10 13:19:35 +03004336DEFINE_SEQ_ATTRIBUTE(gpiolib);
David Brownelld2876d02008-02-04 22:28:20 -08004337
4338static int __init gpiolib_debugfs_init(void)
4339{
4340 /* /sys/kernel/debug/gpio */
Andy Shevchenko425c5b32020-09-10 13:19:35 +03004341 debugfs_create_file("gpio", 0444, NULL, NULL, &gpiolib_fops);
David Brownelld2876d02008-02-04 22:28:20 -08004342 return 0;
4343}
4344subsys_initcall(gpiolib_debugfs_init);
4345
4346#endif /* DEBUG_FS */