blob: 1dbd0e6d240b3028b4fdd9bdcef0d790356aa354 [file] [log] [blame]
Linus Walleijdae5f0a2018-09-25 09:08:48 +02001// SPDX-License-Identifier: GPL-2.0
Andy Shevchenko923a6542017-05-25 16:08:38 +03002#include <linux/bitmap.h>
David Brownelld2876d02008-02-04 22:28:20 -08003#include <linux/kernel.h>
4#include <linux/module.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -07005#include <linux/interrupt.h>
David Brownelld2876d02008-02-04 22:28:20 -08006#include <linux/irq.h>
7#include <linux/spinlock.h>
Alexandre Courbot1a989d02013-02-03 01:29:24 +09008#include <linux/list.h>
David Brownelld8f388d82008-07-25 01:46:07 -07009#include <linux/device.h>
10#include <linux/err.h>
11#include <linux/debugfs.h>
12#include <linux/seq_file.h>
13#include <linux/gpio.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -070014#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Rafael J. Wysocki7b199812013-11-11 22:41:56 +010016#include <linux/acpi.h>
Alexandre Courbot53e7cac2013-11-16 21:44:52 +090017#include <linux/gpio/driver.h>
Linus Walleij0a6d3152014-07-24 20:08:55 +020018#include <linux/gpio/machine.h>
Jonas Gorskic771c2f2015-10-11 17:34:15 +020019#include <linux/pinctrl/consumer.h>
Linus Walleij3c702e92015-10-21 15:29:53 +020020#include <linux/cdev.h>
21#include <linux/fs.h>
22#include <linux/uaccess.h>
Linus Walleij8b92e172016-05-27 14:24:04 +020023#include <linux/compat.h>
Linus Walleijd7c51b42016-04-26 10:35:29 +020024#include <linux/anon_inodes.h>
Lars-Peter Clausen953b9562016-10-24 13:59:15 +020025#include <linux/file.h>
Linus Walleij61f922d2016-06-02 11:30:15 +020026#include <linux/kfifo.h>
27#include <linux/poll.h>
28#include <linux/timekeeping.h>
Linus Walleij3c702e92015-10-21 15:29:53 +020029#include <uapi/linux/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080030
Mika Westerberg664e3e52014-01-08 12:40:54 +020031#include "gpiolib.h"
Linus Walleijf626d6d2019-07-17 09:10:01 +020032#include "gpiolib-of.h"
Andy Shevchenko77cb9072019-07-30 13:43:36 +030033#include "gpiolib-acpi.h"
Mika Westerberg664e3e52014-01-08 12:40:54 +020034
Uwe Kleine-König3f397c212011-05-20 00:40:19 -060035#define CREATE_TRACE_POINTS
36#include <trace/events/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080037
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070038/* Implementation infrastructure for GPIO interfaces.
David Brownelld2876d02008-02-04 22:28:20 -080039 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070040 * The GPIO programming interface allows for inlining speed-critical
41 * get/set operations for common cases, so that access to SOC-integrated
42 * GPIOs can sometimes cost only an instruction or two per bit.
David Brownelld2876d02008-02-04 22:28:20 -080043 */
44
45
46/* When debugging, extend minimal trust to callers and platform code.
47 * Also emit diagnostic messages that may help initial bringup, when
48 * board setup or driver bugs are most common.
49 *
50 * Otherwise, minimize overhead in what may be bitbanging codepaths.
51 */
52#ifdef DEBUG
53#define extra_checks 1
54#else
55#define extra_checks 0
56#endif
57
Linus Walleijff2b1352015-10-20 11:10:38 +020058/* Device and char device-related information */
59static DEFINE_IDA(gpio_ida);
Linus Walleij3c702e92015-10-21 15:29:53 +020060static dev_t gpio_devt;
61#define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
62static struct bus_type gpio_bus_type = {
63 .name = "gpio",
64};
Linus Walleijff2b1352015-10-20 11:10:38 +020065
Laura Abbott30277432018-05-21 10:57:07 -070066/*
67 * Number of GPIOs to use for the fast path in set array
68 */
69#define FASTPATH_NGPIO CONFIG_GPIOLIB_FASTPATH_LIMIT
70
David Brownelld2876d02008-02-04 22:28:20 -080071/* gpio_lock prevents conflicts during gpio_desc[] table updates.
72 * While any GPIO is requested, its gpio_chip is not removable;
73 * each GPIO's "requested" flag serves as a lock and refcount.
74 */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090075DEFINE_SPINLOCK(gpio_lock);
David Brownelld2876d02008-02-04 22:28:20 -080076
Alexandre Courbotbae48da2013-10-17 10:21:38 -070077static DEFINE_MUTEX(gpio_lookup_lock);
78static LIST_HEAD(gpio_lookup_list);
Linus Walleijff2b1352015-10-20 11:10:38 +020079LIST_HEAD(gpio_devices);
Johan Hovold6d867502015-05-04 17:23:25 +020080
Bartosz Golaszewskia411e812018-04-10 22:30:28 +020081static DEFINE_MUTEX(gpio_machine_hogs_mutex);
82static LIST_HEAD(gpio_machine_hogs);
83
Johan Hovold6d867502015-05-04 17:23:25 +020084static void gpiochip_free_hogs(struct gpio_chip *chip);
Thierry Reding959bc7b2017-11-07 19:15:59 +010085static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
Andrew Lunn39c3fd52017-12-02 18:11:04 +010086 struct lock_class_key *lock_key,
87 struct lock_class_key *request_key);
Johan Hovold6d867502015-05-04 17:23:25 +020088static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
Andy Shevchenko9411e3a2019-10-09 17:34:44 +030089static int gpiochip_irqchip_init_hw(struct gpio_chip *gpiochip);
Mika Westerberg79b804c2016-09-20 15:15:21 +030090static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip);
91static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip);
Johan Hovold6d867502015-05-04 17:23:25 +020092
Guenter Roeck159f3cd2016-03-31 08:11:30 -070093static bool gpiolib_initialized;
Johan Hovold6d867502015-05-04 17:23:25 +020094
David Brownelld2876d02008-02-04 22:28:20 -080095static inline void desc_set_label(struct gpio_desc *d, const char *label)
96{
David Brownelld2876d02008-02-04 22:28:20 -080097 d->label = label;
David Brownelld2876d02008-02-04 22:28:20 -080098}
99
Alexandre Courbot372e7222013-02-03 01:29:29 +0900100/**
Thierry Reding950d55f52017-07-24 16:57:22 +0200101 * gpio_to_desc - Convert a GPIO number to its descriptor
102 * @gpio: global GPIO number
103 *
104 * Returns:
105 * The GPIO descriptor associated with the given GPIO, or %NULL if no GPIO
106 * with the given number exists in the system.
Alexandre Courbot372e7222013-02-03 01:29:29 +0900107 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700108struct gpio_desc *gpio_to_desc(unsigned gpio)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900109{
Linus Walleijff2b1352015-10-20 11:10:38 +0200110 struct gpio_device *gdev;
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900111 unsigned long flags;
112
113 spin_lock_irqsave(&gpio_lock, flags);
114
Linus Walleijff2b1352015-10-20 11:10:38 +0200115 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100116 if (gdev->base <= gpio &&
117 gdev->base + gdev->ngpio > gpio) {
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900118 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100119 return &gdev->descs[gpio - gdev->base];
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900120 }
121 }
122
123 spin_unlock_irqrestore(&gpio_lock, flags);
124
Alexandre Courbot0e9a5ed2014-12-02 23:15:05 +0900125 if (!gpio_is_valid(gpio))
126 WARN(1, "invalid GPIO %d\n", gpio);
127
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900128 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900129}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700130EXPORT_SYMBOL_GPL(gpio_to_desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900131
132/**
Thierry Reding950d55f52017-07-24 16:57:22 +0200133 * gpiochip_get_desc - get the GPIO descriptor corresponding to the given
134 * hardware number for this chip
135 * @chip: GPIO chip
136 * @hwnum: hardware number of the GPIO for this chip
137 *
138 * Returns:
139 * A pointer to the GPIO descriptor or %ERR_PTR(-EINVAL) if no GPIO exists
140 * in the given chip for the specified hardware number.
Linus Walleijd468bf92013-09-24 11:54:38 +0200141 */
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900142struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
Bartosz Golaszewski06863622019-12-24 13:06:59 +0100143 unsigned int hwnum)
Linus Walleijd468bf92013-09-24 11:54:38 +0200144{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100145 struct gpio_device *gdev = chip->gpiodev;
146
147 if (hwnum >= gdev->ngpio)
Alexandre Courbotb7d0a282013-12-03 12:31:11 +0900148 return ERR_PTR(-EINVAL);
Linus Walleijd468bf92013-09-24 11:54:38 +0200149
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100150 return &gdev->descs[hwnum];
Linus Walleijd468bf92013-09-24 11:54:38 +0200151}
Marco Felsch97795422020-02-25 10:31:02 +0100152EXPORT_SYMBOL_GPL(gpiochip_get_desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900153
154/**
Thierry Reding950d55f52017-07-24 16:57:22 +0200155 * desc_to_gpio - convert a GPIO descriptor to the integer namespace
156 * @desc: GPIO descriptor
157 *
Alexandre Courbot372e7222013-02-03 01:29:29 +0900158 * This should disappear in the future but is needed since we still
Thierry Reding950d55f52017-07-24 16:57:22 +0200159 * use GPIO numbers for error messages and sysfs nodes.
160 *
161 * Returns:
162 * The global GPIO number for the GPIO specified by its descriptor.
Alexandre Courbot372e7222013-02-03 01:29:29 +0900163 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700164int desc_to_gpio(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900165{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100166 return desc->gdev->base + (desc - &desc->gdev->descs[0]);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900167}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700168EXPORT_SYMBOL_GPL(desc_to_gpio);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900169
170
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700171/**
172 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
173 * @desc: descriptor to return the chip of
174 */
175struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900176{
Vladimir Zapolskiydd3b9a42017-12-21 18:37:30 +0200177 if (!desc || !desc->gdev)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100178 return NULL;
179 return desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900180}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700181EXPORT_SYMBOL_GPL(gpiod_to_chip);
David Brownelld2876d02008-02-04 22:28:20 -0800182
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700183/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
184static int gpiochip_find_base(int ngpio)
185{
Linus Walleijff2b1352015-10-20 11:10:38 +0200186 struct gpio_device *gdev;
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900187 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700188
Linus Walleijff2b1352015-10-20 11:10:38 +0200189 list_for_each_entry_reverse(gdev, &gpio_devices, list) {
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900190 /* found a free space? */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100191 if (gdev->base + gdev->ngpio <= base)
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900192 break;
193 else
194 /* nope, check the space right before the chip */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100195 base = gdev->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700196 }
197
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900198 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700199 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900200 return base;
201 } else {
202 pr_err("%s: cannot find free range\n", __func__);
203 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700204 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700205}
206
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700207/**
208 * gpiod_get_direction - return the current direction of a GPIO
209 * @desc: GPIO to get the direction of
210 *
Wolfram Sang94fc7302018-01-09 12:35:53 +0100211 * Returns 0 for output, 1 for input, or an error code in case of error.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700212 *
213 * This function may sleep if gpiod_cansleep() is true.
214 */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900215int gpiod_get_direction(struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300216{
Wolfram Sangd0121b82018-07-11 18:33:19 +0200217 struct gpio_chip *chip;
218 unsigned offset;
Linus Walleijd377f562019-07-16 11:11:45 +0200219 int ret;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300220
Alexandre Courbot372e7222013-02-03 01:29:29 +0900221 chip = gpiod_to_chip(desc);
222 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300223
Russell King256efae2019-12-07 16:20:18 +0000224 /*
225 * Open drain emulation using input mode may incorrectly report
226 * input here, fix that up.
227 */
228 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) &&
229 test_bit(FLAG_IS_OUT, &desc->flags))
230 return 0;
231
Mathias Nyman80b0a602012-10-24 17:25:27 +0300232 if (!chip->get_direction)
Wolfram Sangd0121b82018-07-11 18:33:19 +0200233 return -ENOTSUPP;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300234
Linus Walleijd377f562019-07-16 11:11:45 +0200235 ret = chip->get_direction(chip, offset);
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +0200236 if (ret < 0)
237 return ret;
238
239 /* GPIOF_DIR_IN or other positive, otherwise GPIOF_DIR_OUT */
240 if (ret > 0)
Linus Walleijd377f562019-07-16 11:11:45 +0200241 ret = 1;
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +0200242
243 assign_bit(FLAG_IS_OUT, &desc->flags, !ret);
244
Linus Walleijd377f562019-07-16 11:11:45 +0200245 return ret;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300246}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700247EXPORT_SYMBOL_GPL(gpiod_get_direction);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300248
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900249/*
250 * Add a new chip to the global chips list, keeping the list of chips sorted
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800251 * by range(means [base, base + ngpio - 1]) order.
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900252 *
253 * Return -EBUSY if the new chip overlaps with some other chip's integer
254 * space.
255 */
Linus Walleijff2b1352015-10-20 11:10:38 +0200256static int gpiodev_add_to_list(struct gpio_device *gdev)
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900257{
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800258 struct gpio_device *prev, *next;
Linus Walleijff2b1352015-10-20 11:10:38 +0200259
260 if (list_empty(&gpio_devices)) {
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800261 /* initial entry in list */
Linus Walleijff2b1352015-10-20 11:10:38 +0200262 list_add_tail(&gdev->list, &gpio_devices);
Sudip Mukherjeee28ecca2015-12-27 19:06:50 +0530263 return 0;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900264 }
265
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800266 next = list_entry(gpio_devices.next, struct gpio_device, list);
267 if (gdev->base + gdev->ngpio <= next->base) {
268 /* add before first entry */
269 list_add(&gdev->list, &gpio_devices);
Julien Grossholtz96098df2016-01-07 16:46:45 -0500270 return 0;
271 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900272
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800273 prev = list_entry(gpio_devices.prev, struct gpio_device, list);
274 if (prev->base + prev->ngpio <= gdev->base) {
275 /* add behind last entry */
276 list_add_tail(&gdev->list, &gpio_devices);
277 return 0;
278 }
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800279
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800280 list_for_each_entry_safe(prev, next, &gpio_devices, list) {
281 /* at the end of the list */
282 if (&next->list == &gpio_devices)
283 break;
284
285 /* add between prev and next */
286 if (prev->base + prev->ngpio <= gdev->base
287 && gdev->base + gdev->ngpio <= next->base) {
288 list_add(&gdev->list, &prev->list);
289 return 0;
290 }
291 }
292
293 dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n");
294 return -EBUSY;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900295}
296
Thierry Reding950d55f52017-07-24 16:57:22 +0200297/*
Linus Walleijf881bab02015-09-23 16:20:43 -0700298 * Convert a GPIO name to its descriptor
299 */
300static struct gpio_desc *gpio_name_to_desc(const char * const name)
301{
Linus Walleijff2b1352015-10-20 11:10:38 +0200302 struct gpio_device *gdev;
Linus Walleijf881bab02015-09-23 16:20:43 -0700303 unsigned long flags;
304
305 spin_lock_irqsave(&gpio_lock, flags);
306
Linus Walleijff2b1352015-10-20 11:10:38 +0200307 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijf881bab02015-09-23 16:20:43 -0700308 int i;
309
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100310 for (i = 0; i != gdev->ngpio; ++i) {
311 struct gpio_desc *desc = &gdev->descs[i];
Linus Walleijf881bab02015-09-23 16:20:43 -0700312
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100313 if (!desc->name || !name)
Linus Walleijf881bab02015-09-23 16:20:43 -0700314 continue;
315
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100316 if (!strcmp(desc->name, name)) {
Linus Walleijf881bab02015-09-23 16:20:43 -0700317 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100318 return desc;
Linus Walleijf881bab02015-09-23 16:20:43 -0700319 }
320 }
321 }
322
323 spin_unlock_irqrestore(&gpio_lock, flags);
324
325 return NULL;
326}
327
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200328/*
329 * Takes the names from gc->names and checks if they are all unique. If they
330 * are, they are assigned to their gpio descriptors.
331 *
Bamvor Jian Zhanged379152015-11-14 16:43:20 +0800332 * Warning if one of the names is already used for a different GPIO.
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200333 */
334static int gpiochip_set_desc_names(struct gpio_chip *gc)
335{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100336 struct gpio_device *gdev = gc->gpiodev;
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200337 int i;
338
339 if (!gc->names)
340 return 0;
341
342 /* First check all names if they are unique */
343 for (i = 0; i != gc->ngpio; ++i) {
344 struct gpio_desc *gpio;
345
346 gpio = gpio_name_to_desc(gc->names[i]);
Linus Walleijf881bab02015-09-23 16:20:43 -0700347 if (gpio)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100348 dev_warn(&gdev->dev,
Linus Walleij34ffd852015-10-20 11:31:54 +0200349 "Detected name collision for GPIO name '%s'\n",
Linus Walleijf881bab02015-09-23 16:20:43 -0700350 gc->names[i]);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200351 }
352
353 /* Then add all names to the GPIO descriptors */
354 for (i = 0; i != gc->ngpio; ++i)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100355 gdev->descs[i].name = gc->names[i];
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200356
357 return 0;
358}
359
Stephen Boyde4371f6e2018-03-23 09:34:50 -0700360static unsigned long *gpiochip_allocate_mask(struct gpio_chip *chip)
361{
362 unsigned long *p;
363
Masahiro Yamada7bdbd1ec2019-07-18 15:51:01 +0900364 p = bitmap_alloc(chip->ngpio, GFP_KERNEL);
Stephen Boyde4371f6e2018-03-23 09:34:50 -0700365 if (!p)
366 return NULL;
367
368 /* Assume by default all GPIOs are valid */
369 bitmap_fill(p, chip->ngpio);
370
371 return p;
372}
373
Linus Walleijf626d6d2019-07-17 09:10:01 +0200374static int gpiochip_alloc_valid_mask(struct gpio_chip *gc)
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700375{
Linus Walleijeb1e8bd2019-08-19 11:30:58 +0200376 if (!(of_gpio_need_valid_mask(gc) || gc->init_valid_mask))
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700377 return 0;
378
Linus Walleijf626d6d2019-07-17 09:10:01 +0200379 gc->valid_mask = gpiochip_allocate_mask(gc);
380 if (!gc->valid_mask)
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700381 return -ENOMEM;
382
383 return 0;
384}
385
Linus Walleijc9fc5af2019-08-19 10:49:04 +0200386static int gpiochip_init_valid_mask(struct gpio_chip *gc)
Ricardo Ribalda Delgadof8ec92a2018-10-05 08:52:58 +0200387{
Linus Walleijc9fc5af2019-08-19 10:49:04 +0200388 if (gc->init_valid_mask)
389 return gc->init_valid_mask(gc,
390 gc->valid_mask,
391 gc->ngpio);
Ricardo Ribalda Delgadof8ec92a2018-10-05 08:52:58 +0200392
393 return 0;
394}
395
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700396static void gpiochip_free_valid_mask(struct gpio_chip *gpiochip)
397{
Masahiro Yamada7bdbd1ec2019-07-18 15:51:01 +0900398 bitmap_free(gpiochip->valid_mask);
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700399 gpiochip->valid_mask = NULL;
400}
401
Andy Shevchenkob056ca12019-11-04 18:09:39 +0200402static int gpiochip_add_pin_ranges(struct gpio_chip *gc)
403{
404 if (gc->add_pin_ranges)
405 return gc->add_pin_ranges(gc);
406
407 return 0;
408}
409
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700410bool gpiochip_line_is_valid(const struct gpio_chip *gpiochip,
411 unsigned int offset)
412{
413 /* No mask means all valid */
414 if (likely(!gpiochip->valid_mask))
415 return true;
416 return test_bit(offset, gpiochip->valid_mask);
417}
418EXPORT_SYMBOL_GPL(gpiochip_line_is_valid);
419
Linus Walleijd7c51b42016-04-26 10:35:29 +0200420/*
421 * GPIO line handle management
422 */
423
424/**
425 * struct linehandle_state - contains the state of a userspace handle
426 * @gdev: the GPIO device the handle pertains to
427 * @label: consumer label used to tag descriptors
428 * @descs: the GPIO descriptors held by this handle
429 * @numdescs: the number of descriptors held in the descs array
430 */
431struct linehandle_state {
432 struct gpio_device *gdev;
433 const char *label;
434 struct gpio_desc *descs[GPIOHANDLES_MAX];
435 u32 numdescs;
436};
437
Lars-Peter Clausene3e847c2016-10-18 16:54:05 +0200438#define GPIOHANDLE_REQUEST_VALID_FLAGS \
439 (GPIOHANDLE_REQUEST_INPUT | \
440 GPIOHANDLE_REQUEST_OUTPUT | \
441 GPIOHANDLE_REQUEST_ACTIVE_LOW | \
Drew Fustini9225d512019-11-05 10:04:23 +0800442 GPIOHANDLE_REQUEST_BIAS_PULL_UP | \
443 GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | \
Kent Gibson2148ad72019-11-05 10:04:25 +0800444 GPIOHANDLE_REQUEST_BIAS_DISABLE | \
Lars-Peter Clausene3e847c2016-10-18 16:54:05 +0200445 GPIOHANDLE_REQUEST_OPEN_DRAIN | \
446 GPIOHANDLE_REQUEST_OPEN_SOURCE)
447
Kent Gibsonb043ed72019-11-05 10:04:28 +0800448static int linehandle_validate_flags(u32 flags)
449{
450 /* Return an error if an unknown flag is set */
451 if (flags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
452 return -EINVAL;
453
454 /*
455 * Do not allow both INPUT & OUTPUT flags to be set as they are
456 * contradictory.
457 */
458 if ((flags & GPIOHANDLE_REQUEST_INPUT) &&
459 (flags & GPIOHANDLE_REQUEST_OUTPUT))
460 return -EINVAL;
461
462 /*
463 * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
464 * the hardware actually supports enabling both at the same time the
465 * electrical result would be disastrous.
466 */
467 if ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) &&
468 (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
469 return -EINVAL;
470
471 /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
472 if (!(flags & GPIOHANDLE_REQUEST_OUTPUT) &&
473 ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
474 (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
475 return -EINVAL;
476
477 /* Bias flags only allowed for input or output mode. */
478 if (!((flags & GPIOHANDLE_REQUEST_INPUT) ||
479 (flags & GPIOHANDLE_REQUEST_OUTPUT)) &&
480 ((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) ||
481 (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP) ||
482 (flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN)))
483 return -EINVAL;
484
485 /* Only one bias flag can be set. */
486 if (((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
487 (flags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
488 GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
489 ((flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
490 (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
491 return -EINVAL;
492
493 return 0;
494}
495
Kent Gibsone588bb12019-11-05 10:04:29 +0800496static long linehandle_set_config(struct linehandle_state *lh,
497 void __user *ip)
498{
499 struct gpiohandle_config gcnf;
500 struct gpio_desc *desc;
501 int i, ret;
502 u32 lflags;
503 unsigned long *flagsp;
504
505 if (copy_from_user(&gcnf, ip, sizeof(gcnf)))
506 return -EFAULT;
507
508 lflags = gcnf.flags;
509 ret = linehandle_validate_flags(lflags);
510 if (ret)
511 return ret;
512
513 for (i = 0; i < lh->numdescs; i++) {
514 desc = lh->descs[i];
515 flagsp = &desc->flags;
516
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +0200517 assign_bit(FLAG_ACTIVE_LOW, flagsp,
Kent Gibsone588bb12019-11-05 10:04:29 +0800518 lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW);
519
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +0200520 assign_bit(FLAG_OPEN_DRAIN, flagsp,
Kent Gibsone588bb12019-11-05 10:04:29 +0800521 lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN);
522
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +0200523 assign_bit(FLAG_OPEN_SOURCE, flagsp,
Kent Gibsone588bb12019-11-05 10:04:29 +0800524 lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE);
525
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +0200526 assign_bit(FLAG_PULL_UP, flagsp,
Kent Gibsone588bb12019-11-05 10:04:29 +0800527 lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP);
528
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +0200529 assign_bit(FLAG_PULL_DOWN, flagsp,
Kent Gibsone588bb12019-11-05 10:04:29 +0800530 lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN);
531
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +0200532 assign_bit(FLAG_BIAS_DISABLE, flagsp,
Kent Gibsone588bb12019-11-05 10:04:29 +0800533 lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE);
534
535 /*
536 * Lines have to be requested explicitly for input
537 * or output, else the line will be treated "as is".
538 */
539 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
540 int val = !!gcnf.default_values[i];
541
542 ret = gpiod_direction_output(desc, val);
543 if (ret)
544 return ret;
545 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
546 ret = gpiod_direction_input(desc);
547 if (ret)
548 return ret;
549 }
550 }
551 return 0;
552}
553
Linus Walleijd7c51b42016-04-26 10:35:29 +0200554static long linehandle_ioctl(struct file *filep, unsigned int cmd,
555 unsigned long arg)
556{
557 struct linehandle_state *lh = filep->private_data;
558 void __user *ip = (void __user *)arg;
559 struct gpiohandle_data ghd;
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +0200560 DECLARE_BITMAP(vals, GPIOHANDLES_MAX);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200561 int i;
562
563 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
Bartosz Golaszewski2b955b32018-07-16 10:34:24 +0200564 /* NOTE: It's ok to read values of output lines. */
Lukas Wunnereec1d562017-10-12 12:40:10 +0200565 int ret = gpiod_get_array_value_complex(false,
566 true,
567 lh->numdescs,
568 lh->descs,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +0200569 NULL,
Lukas Wunnereec1d562017-10-12 12:40:10 +0200570 vals);
571 if (ret)
572 return ret;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200573
Lars-Peter Clausen3eded5d2016-10-18 16:54:02 +0200574 memset(&ghd, 0, sizeof(ghd));
Lukas Wunnereec1d562017-10-12 12:40:10 +0200575 for (i = 0; i < lh->numdescs; i++)
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +0200576 ghd.values[i] = test_bit(i, vals);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200577
578 if (copy_to_user(ip, &ghd, sizeof(ghd)))
579 return -EFAULT;
580
581 return 0;
582 } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
Bartosz Golaszewskie5332d52018-07-16 10:34:23 +0200583 /*
584 * All line descriptors were created at once with the same
585 * flags so just check if the first one is really output.
586 */
587 if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags))
588 return -EPERM;
589
Linus Walleijd7c51b42016-04-26 10:35:29 +0200590 if (copy_from_user(&ghd, ip, sizeof(ghd)))
591 return -EFAULT;
592
593 /* Clamp all values to [0,1] */
594 for (i = 0; i < lh->numdescs; i++)
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +0200595 __assign_bit(i, vals, ghd.values[i]);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200596
597 /* Reuse the array setting function */
Laura Abbott30277432018-05-21 10:57:07 -0700598 return gpiod_set_array_value_complex(false,
Linus Walleijd7c51b42016-04-26 10:35:29 +0200599 true,
600 lh->numdescs,
601 lh->descs,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +0200602 NULL,
Linus Walleijd7c51b42016-04-26 10:35:29 +0200603 vals);
Kent Gibsone588bb12019-11-05 10:04:29 +0800604 } else if (cmd == GPIOHANDLE_SET_CONFIG_IOCTL) {
605 return linehandle_set_config(lh, ip);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200606 }
607 return -EINVAL;
608}
609
610#ifdef CONFIG_COMPAT
611static long linehandle_ioctl_compat(struct file *filep, unsigned int cmd,
612 unsigned long arg)
613{
614 return linehandle_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
615}
616#endif
617
618static int linehandle_release(struct inode *inode, struct file *filep)
619{
620 struct linehandle_state *lh = filep->private_data;
621 struct gpio_device *gdev = lh->gdev;
622 int i;
623
624 for (i = 0; i < lh->numdescs; i++)
625 gpiod_free(lh->descs[i]);
626 kfree(lh->label);
627 kfree(lh);
628 put_device(&gdev->dev);
629 return 0;
630}
631
632static const struct file_operations linehandle_fileops = {
633 .release = linehandle_release,
634 .owner = THIS_MODULE,
635 .llseek = noop_llseek,
636 .unlocked_ioctl = linehandle_ioctl,
637#ifdef CONFIG_COMPAT
638 .compat_ioctl = linehandle_ioctl_compat,
639#endif
640};
641
642static int linehandle_create(struct gpio_device *gdev, void __user *ip)
643{
644 struct gpiohandle_request handlereq;
645 struct linehandle_state *lh;
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200646 struct file *file;
Timur Tabiab3dbcf2018-03-29 13:29:12 -0500647 int fd, i, count = 0, ret;
Bartosz Golaszewski418ee8e2017-10-16 11:32:29 +0200648 u32 lflags;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200649
650 if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
651 return -EFAULT;
652 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
653 return -EINVAL;
654
Bartosz Golaszewski418ee8e2017-10-16 11:32:29 +0200655 lflags = handlereq.flags;
656
Kent Gibsonb043ed72019-11-05 10:04:28 +0800657 ret = linehandle_validate_flags(lflags);
658 if (ret)
659 return ret;
Kent Gibson2148ad72019-11-05 10:04:25 +0800660
Linus Walleijd7c51b42016-04-26 10:35:29 +0200661 lh = kzalloc(sizeof(*lh), GFP_KERNEL);
662 if (!lh)
663 return -ENOMEM;
664 lh->gdev = gdev;
665 get_device(&gdev->dev);
666
667 /* Make sure this is terminated */
668 handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
669 if (strlen(handlereq.consumer_label)) {
670 lh->label = kstrdup(handlereq.consumer_label,
671 GFP_KERNEL);
672 if (!lh->label) {
673 ret = -ENOMEM;
674 goto out_free_lh;
675 }
676 }
677
678 /* Request each GPIO */
679 for (i = 0; i < handlereq.lines; i++) {
680 u32 offset = handlereq.lineoffsets[i];
Bartosz Golaszewski0f41dab2019-12-24 13:07:00 +0100681 struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200682
Bartosz Golaszewski0f41dab2019-12-24 13:07:00 +0100683 if (IS_ERR(desc)) {
684 ret = PTR_ERR(desc);
Lars-Peter Clausene405f9f2016-10-18 16:54:01 +0200685 goto out_free_descs;
686 }
687
Linus Walleijd7c51b42016-04-26 10:35:29 +0200688 ret = gpiod_request(desc, lh->label);
689 if (ret)
690 goto out_free_descs;
691 lh->descs[i] = desc;
Ricardo Ribalda Delgado19a4fbf2018-09-13 15:37:04 +0200692 count = i + 1;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200693
694 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
695 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
696 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
697 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
698 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
699 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Kent Gibson2148ad72019-11-05 10:04:25 +0800700 if (lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE)
701 set_bit(FLAG_BIAS_DISABLE, &desc->flags);
Drew Fustini9225d512019-11-05 10:04:23 +0800702 if (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN)
703 set_bit(FLAG_PULL_DOWN, &desc->flags);
704 if (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)
705 set_bit(FLAG_PULL_UP, &desc->flags);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200706
Andrew Jefferye10f72b2017-11-30 14:25:24 +1030707 ret = gpiod_set_transitory(desc, false);
708 if (ret < 0)
709 goto out_free_descs;
710
Linus Walleijd7c51b42016-04-26 10:35:29 +0200711 /*
712 * Lines have to be requested explicitly for input
713 * or output, else the line will be treated "as is".
714 */
715 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
716 int val = !!handlereq.default_values[i];
717
718 ret = gpiod_direction_output(desc, val);
719 if (ret)
720 goto out_free_descs;
721 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
722 ret = gpiod_direction_input(desc);
723 if (ret)
724 goto out_free_descs;
725 }
726 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
727 offset);
728 }
Linus Walleije2f608b2016-06-18 10:56:43 +0200729 /* Let i point at the last handle */
730 i--;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200731 lh->numdescs = handlereq.lines;
732
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200733 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200734 if (fd < 0) {
735 ret = fd;
736 goto out_free_descs;
737 }
738
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200739 file = anon_inode_getfile("gpio-linehandle",
740 &linehandle_fileops,
741 lh,
742 O_RDONLY | O_CLOEXEC);
743 if (IS_ERR(file)) {
744 ret = PTR_ERR(file);
745 goto out_put_unused_fd;
746 }
747
Linus Walleijd7c51b42016-04-26 10:35:29 +0200748 handlereq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200749 if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200750 /*
751 * fput() will trigger the release() callback, so do not go onto
752 * the regular error cleanup path here.
753 */
754 fput(file);
755 put_unused_fd(fd);
756 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +0200757 }
Linus Walleijd7c51b42016-04-26 10:35:29 +0200758
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200759 fd_install(fd, file);
760
Linus Walleijd7c51b42016-04-26 10:35:29 +0200761 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
762 lh->numdescs);
763
764 return 0;
765
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200766out_put_unused_fd:
767 put_unused_fd(fd);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200768out_free_descs:
Timur Tabiab3dbcf2018-03-29 13:29:12 -0500769 for (i = 0; i < count; i++)
Linus Walleijd7c51b42016-04-26 10:35:29 +0200770 gpiod_free(lh->descs[i]);
771 kfree(lh->label);
772out_free_lh:
773 kfree(lh);
774 put_device(&gdev->dev);
775 return ret;
776}
777
Linus Walleij61f922d2016-06-02 11:30:15 +0200778/*
779 * GPIO line event management
780 */
781
782/**
783 * struct lineevent_state - contains the state of a userspace event
784 * @gdev: the GPIO device the event pertains to
785 * @label: consumer label used to tag descriptors
786 * @desc: the GPIO descriptor held by this event
787 * @eflags: the event flags this line was requested with
788 * @irq: the interrupt that trigger in response to events on this GPIO
789 * @wait: wait queue that handles blocking reads of events
790 * @events: KFIFO for the GPIO events
791 * @read_lock: mutex lock to protect reads from colliding with adding
792 * new events to the FIFO
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100793 * @timestamp: cache for the timestamp storing it between hardirq
794 * and IRQ thread, used to bring the timestamp close to the actual
795 * event
Linus Walleij61f922d2016-06-02 11:30:15 +0200796 */
797struct lineevent_state {
798 struct gpio_device *gdev;
799 const char *label;
800 struct gpio_desc *desc;
801 u32 eflags;
802 int irq;
803 wait_queue_head_t wait;
804 DECLARE_KFIFO(events, struct gpioevent_data, 16);
805 struct mutex read_lock;
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100806 u64 timestamp;
Linus Walleij61f922d2016-06-02 11:30:15 +0200807};
808
Lars-Peter Clausenac7dbb92016-10-18 16:54:06 +0200809#define GPIOEVENT_REQUEST_VALID_FLAGS \
810 (GPIOEVENT_REQUEST_RISING_EDGE | \
811 GPIOEVENT_REQUEST_FALLING_EDGE)
812
Al Viroafc9a422017-07-03 06:39:46 -0400813static __poll_t lineevent_poll(struct file *filep,
Linus Walleij61f922d2016-06-02 11:30:15 +0200814 struct poll_table_struct *wait)
815{
816 struct lineevent_state *le = filep->private_data;
Al Viroafc9a422017-07-03 06:39:46 -0400817 __poll_t events = 0;
Linus Walleij61f922d2016-06-02 11:30:15 +0200818
819 poll_wait(filep, &le->wait, wait);
820
821 if (!kfifo_is_empty(&le->events))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800822 events = EPOLLIN | EPOLLRDNORM;
Linus Walleij61f922d2016-06-02 11:30:15 +0200823
824 return events;
825}
826
827
828static ssize_t lineevent_read(struct file *filep,
829 char __user *buf,
830 size_t count,
831 loff_t *f_ps)
832{
833 struct lineevent_state *le = filep->private_data;
834 unsigned int copied;
835 int ret;
836
837 if (count < sizeof(struct gpioevent_data))
838 return -EINVAL;
839
840 do {
841 if (kfifo_is_empty(&le->events)) {
842 if (filep->f_flags & O_NONBLOCK)
843 return -EAGAIN;
844
845 ret = wait_event_interruptible(le->wait,
846 !kfifo_is_empty(&le->events));
847 if (ret)
848 return ret;
849 }
850
851 if (mutex_lock_interruptible(&le->read_lock))
852 return -ERESTARTSYS;
853 ret = kfifo_to_user(&le->events, buf, count, &copied);
854 mutex_unlock(&le->read_lock);
855
856 if (ret)
857 return ret;
858
859 /*
860 * If we couldn't read anything from the fifo (a different
861 * thread might have been faster) we either return -EAGAIN if
862 * the file descriptor is non-blocking, otherwise we go back to
863 * sleep and wait for more data to arrive.
864 */
865 if (copied == 0 && (filep->f_flags & O_NONBLOCK))
866 return -EAGAIN;
867
868 } while (copied == 0);
869
870 return copied;
871}
872
873static int lineevent_release(struct inode *inode, struct file *filep)
874{
875 struct lineevent_state *le = filep->private_data;
876 struct gpio_device *gdev = le->gdev;
877
878 free_irq(le->irq, le);
879 gpiod_free(le->desc);
880 kfree(le->label);
881 kfree(le);
882 put_device(&gdev->dev);
883 return 0;
884}
885
886static long lineevent_ioctl(struct file *filep, unsigned int cmd,
887 unsigned long arg)
888{
889 struct lineevent_state *le = filep->private_data;
890 void __user *ip = (void __user *)arg;
891 struct gpiohandle_data ghd;
892
893 /*
894 * We can get the value for an event line but not set it,
895 * because it is input by definition.
896 */
897 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
898 int val;
899
Lars-Peter Clausend82aa4a2016-10-18 16:54:04 +0200900 memset(&ghd, 0, sizeof(ghd));
901
Linus Walleij61f922d2016-06-02 11:30:15 +0200902 val = gpiod_get_value_cansleep(le->desc);
903 if (val < 0)
904 return val;
905 ghd.values[0] = val;
906
907 if (copy_to_user(ip, &ghd, sizeof(ghd)))
908 return -EFAULT;
909
910 return 0;
911 }
912 return -EINVAL;
913}
914
915#ifdef CONFIG_COMPAT
916static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd,
917 unsigned long arg)
918{
919 return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
920}
921#endif
922
923static const struct file_operations lineevent_fileops = {
924 .release = lineevent_release,
925 .read = lineevent_read,
926 .poll = lineevent_poll,
927 .owner = THIS_MODULE,
928 .llseek = noop_llseek,
929 .unlocked_ioctl = lineevent_ioctl,
930#ifdef CONFIG_COMPAT
931 .compat_ioctl = lineevent_ioctl_compat,
932#endif
933};
934
Ben Dooks33265b12016-06-17 16:03:13 +0100935static irqreturn_t lineevent_irq_thread(int irq, void *p)
Linus Walleij61f922d2016-06-02 11:30:15 +0200936{
937 struct lineevent_state *le = p;
938 struct gpioevent_data ge;
Uwe Kleine-Königfa388692018-08-20 08:32:53 +0200939 int ret;
Linus Walleij61f922d2016-06-02 11:30:15 +0200940
Linus Walleij24bd3ef2018-01-22 13:19:28 +0100941 /* Do not leak kernel stack to userspace */
942 memset(&ge, 0, sizeof(ge));
943
Bartosz Golaszewski1033be52019-01-04 11:24:20 +0100944 /*
945 * We may be running from a nested threaded interrupt in which case
946 * we didn't get the timestamp from lineevent_irq_handler().
947 */
948 if (!le->timestamp)
949 ge.timestamp = ktime_get_real_ns();
950 else
951 ge.timestamp = le->timestamp;
Linus Walleij61f922d2016-06-02 11:30:15 +0200952
Bartosz Golaszewskiad537b82017-06-23 13:45:16 +0200953 if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
954 && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
Uwe Kleine-Königfa388692018-08-20 08:32:53 +0200955 int level = gpiod_get_value_cansleep(le->desc);
Linus Walleij61f922d2016-06-02 11:30:15 +0200956 if (level)
957 /* Emit low-to-high event */
958 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
959 else
960 /* Emit high-to-low event */
961 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Uwe Kleine-Königfa388692018-08-20 08:32:53 +0200962 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200963 /* Emit low-to-high event */
964 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
Uwe Kleine-Königfa388692018-08-20 08:32:53 +0200965 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200966 /* Emit high-to-low event */
967 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Arnd Bergmannbc0207a2016-06-16 11:02:41 +0200968 } else {
969 return IRQ_NONE;
Linus Walleij61f922d2016-06-02 11:30:15 +0200970 }
971
972 ret = kfifo_put(&le->events, ge);
Saiyam Doshi2efc6bf2019-09-07 23:09:10 +0530973 if (ret)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800974 wake_up_poll(&le->wait, EPOLLIN);
Linus Walleij61f922d2016-06-02 11:30:15 +0200975
976 return IRQ_HANDLED;
977}
978
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100979static irqreturn_t lineevent_irq_handler(int irq, void *p)
980{
981 struct lineevent_state *le = p;
982
983 /*
984 * Just store the timestamp in hardirq context so we get it as
985 * close in time as possible to the actual event.
986 */
987 le->timestamp = ktime_get_real_ns();
988
989 return IRQ_WAKE_THREAD;
990}
991
Linus Walleij61f922d2016-06-02 11:30:15 +0200992static int lineevent_create(struct gpio_device *gdev, void __user *ip)
993{
994 struct gpioevent_request eventreq;
995 struct lineevent_state *le;
996 struct gpio_desc *desc;
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200997 struct file *file;
Linus Walleij61f922d2016-06-02 11:30:15 +0200998 u32 offset;
999 u32 lflags;
1000 u32 eflags;
1001 int fd;
1002 int ret;
1003 int irqflags = 0;
1004
1005 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
1006 return -EFAULT;
1007
Bartosz Golaszewskibcc6d992019-09-16 11:46:23 +02001008 offset = eventreq.lineoffset;
1009 lflags = eventreq.handleflags;
1010 eflags = eventreq.eventflags;
1011
Bartosz Golaszewski45e23602019-12-24 13:07:01 +01001012 desc = gpiochip_get_desc(gdev->chip, offset);
1013 if (IS_ERR(desc))
1014 return PTR_ERR(desc);
Bartosz Golaszewskibcc6d992019-09-16 11:46:23 +02001015
1016 /* Return an error if a unknown flag is set */
1017 if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
1018 (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS))
1019 return -EINVAL;
1020
1021 /* This is just wrong: we don't look for events on output lines */
1022 if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) ||
1023 (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
1024 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
1025 return -EINVAL;
1026
Kent Gibson2148ad72019-11-05 10:04:25 +08001027 /* Only one bias flag can be set. */
1028 if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
1029 (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
1030 GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
1031 ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
1032 (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
1033 return -EINVAL;
1034
Linus Walleij61f922d2016-06-02 11:30:15 +02001035 le = kzalloc(sizeof(*le), GFP_KERNEL);
1036 if (!le)
1037 return -ENOMEM;
1038 le->gdev = gdev;
1039 get_device(&gdev->dev);
1040
1041 /* Make sure this is terminated */
1042 eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
1043 if (strlen(eventreq.consumer_label)) {
1044 le->label = kstrdup(eventreq.consumer_label,
1045 GFP_KERNEL);
1046 if (!le->label) {
1047 ret = -ENOMEM;
1048 goto out_free_le;
1049 }
1050 }
1051
Linus Walleij61f922d2016-06-02 11:30:15 +02001052 ret = gpiod_request(desc, le->label);
1053 if (ret)
Uwe Kleine-Königf001cc32018-04-16 13:17:53 +02001054 goto out_free_label;
Linus Walleij61f922d2016-06-02 11:30:15 +02001055 le->desc = desc;
1056 le->eflags = eflags;
1057
1058 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
1059 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
Kent Gibson2148ad72019-11-05 10:04:25 +08001060 if (lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE)
1061 set_bit(FLAG_BIAS_DISABLE, &desc->flags);
Kent Gibson7b479a82019-11-05 10:04:24 +08001062 if (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN)
1063 set_bit(FLAG_PULL_DOWN, &desc->flags);
1064 if (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)
1065 set_bit(FLAG_PULL_UP, &desc->flags);
Linus Walleij61f922d2016-06-02 11:30:15 +02001066
1067 ret = gpiod_direction_input(desc);
1068 if (ret)
1069 goto out_free_desc;
1070
1071 le->irq = gpiod_to_irq(desc);
1072 if (le->irq <= 0) {
1073 ret = -ENODEV;
1074 goto out_free_desc;
1075 }
1076
1077 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
Michael Wu223ecaf2019-07-08 13:23:08 +08001078 irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
1079 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
Linus Walleij61f922d2016-06-02 11:30:15 +02001080 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
Michael Wu223ecaf2019-07-08 13:23:08 +08001081 irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
1082 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
Linus Walleij61f922d2016-06-02 11:30:15 +02001083 irqflags |= IRQF_ONESHOT;
Linus Walleij61f922d2016-06-02 11:30:15 +02001084
1085 INIT_KFIFO(le->events);
1086 init_waitqueue_head(&le->wait);
1087 mutex_init(&le->read_lock);
1088
1089 /* Request a thread to read the events */
1090 ret = request_threaded_irq(le->irq,
Linus Walleijd58f2bf2017-11-30 10:23:27 +01001091 lineevent_irq_handler,
Linus Walleij61f922d2016-06-02 11:30:15 +02001092 lineevent_irq_thread,
1093 irqflags,
1094 le->label,
1095 le);
1096 if (ret)
1097 goto out_free_desc;
1098
Lars-Peter Clausen953b9562016-10-24 13:59:15 +02001099 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleij61f922d2016-06-02 11:30:15 +02001100 if (fd < 0) {
1101 ret = fd;
1102 goto out_free_irq;
1103 }
1104
Lars-Peter Clausen953b9562016-10-24 13:59:15 +02001105 file = anon_inode_getfile("gpio-event",
1106 &lineevent_fileops,
1107 le,
1108 O_RDONLY | O_CLOEXEC);
1109 if (IS_ERR(file)) {
1110 ret = PTR_ERR(file);
1111 goto out_put_unused_fd;
1112 }
1113
Linus Walleij61f922d2016-06-02 11:30:15 +02001114 eventreq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +02001115 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +02001116 /*
1117 * fput() will trigger the release() callback, so do not go onto
1118 * the regular error cleanup path here.
1119 */
1120 fput(file);
1121 put_unused_fd(fd);
1122 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +02001123 }
Linus Walleij61f922d2016-06-02 11:30:15 +02001124
Lars-Peter Clausen953b9562016-10-24 13:59:15 +02001125 fd_install(fd, file);
1126
Linus Walleij61f922d2016-06-02 11:30:15 +02001127 return 0;
1128
Lars-Peter Clausen953b9562016-10-24 13:59:15 +02001129out_put_unused_fd:
1130 put_unused_fd(fd);
Linus Walleij61f922d2016-06-02 11:30:15 +02001131out_free_irq:
1132 free_irq(le->irq, le);
1133out_free_desc:
1134 gpiod_free(le->desc);
1135out_free_label:
1136 kfree(le->label);
1137out_free_le:
1138 kfree(le);
1139 put_device(&gdev->dev);
1140 return ret;
1141}
1142
Thierry Reding950d55f52017-07-24 16:57:22 +02001143/*
Linus Walleij3c702e92015-10-21 15:29:53 +02001144 * gpio_ioctl() - ioctl handler for the GPIO chardev
1145 */
1146static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1147{
1148 struct gpio_device *gdev = filp->private_data;
1149 struct gpio_chip *chip = gdev->chip;
Linus Walleij8b92e172016-05-27 14:24:04 +02001150 void __user *ip = (void __user *)arg;
Linus Walleij3c702e92015-10-21 15:29:53 +02001151
1152 /* We fail any subsequent ioctl():s when the chip is gone */
1153 if (!chip)
1154 return -ENODEV;
1155
Linus Walleij521a2ad2016-02-12 22:25:22 +01001156 /* Fill in the struct and pass to userspace */
Linus Walleij3c702e92015-10-21 15:29:53 +02001157 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
Linus Walleij521a2ad2016-02-12 22:25:22 +01001158 struct gpiochip_info chipinfo;
1159
Lars-Peter Clausen0f4bbb22016-10-18 16:54:00 +02001160 memset(&chipinfo, 0, sizeof(chipinfo));
1161
Linus Walleij3c702e92015-10-21 15:29:53 +02001162 strncpy(chipinfo.name, dev_name(&gdev->dev),
1163 sizeof(chipinfo.name));
1164 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
Linus Walleijdf4878e2016-02-12 14:48:23 +01001165 strncpy(chipinfo.label, gdev->label,
1166 sizeof(chipinfo.label));
1167 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001168 chipinfo.lines = gdev->ngpio;
Linus Walleij3c702e92015-10-21 15:29:53 +02001169 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
1170 return -EFAULT;
1171 return 0;
Linus Walleij521a2ad2016-02-12 22:25:22 +01001172 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
1173 struct gpioline_info lineinfo;
1174 struct gpio_desc *desc;
1175
1176 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
1177 return -EFAULT;
Linus Walleij521a2ad2016-02-12 22:25:22 +01001178
Bartosz Golaszewski2a2cabd2019-12-24 13:07:02 +01001179 desc = gpiochip_get_desc(chip, lineinfo.line_offset);
1180 if (IS_ERR(desc))
1181 return PTR_ERR(desc);
1182
Linus Walleij521a2ad2016-02-12 22:25:22 +01001183 if (desc->name) {
1184 strncpy(lineinfo.name, desc->name,
1185 sizeof(lineinfo.name));
1186 lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
1187 } else {
1188 lineinfo.name[0] = '\0';
1189 }
1190 if (desc->label) {
Linus Walleij214338e2016-02-25 21:01:48 +01001191 strncpy(lineinfo.consumer, desc->label,
1192 sizeof(lineinfo.consumer));
1193 lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +01001194 } else {
Linus Walleij214338e2016-02-25 21:01:48 +01001195 lineinfo.consumer[0] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +01001196 }
1197
1198 /*
1199 * Userspace only need to know that the kernel is using
1200 * this GPIO so it can't use it.
1201 */
1202 lineinfo.flags = 0;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001203 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
1204 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
1205 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
1206 test_bit(FLAG_EXPORT, &desc->flags) ||
Stefan Wahren472a61e2019-08-14 14:00:35 +03001207 test_bit(FLAG_SYSFS, &desc->flags) ||
1208 !pinctrl_gpio_can_use_line(chip->base + lineinfo.line_offset))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001209 lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001210 if (test_bit(FLAG_IS_OUT, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001211 lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001212 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001213 lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001214 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Bartosz Golaszewski2c60e6b2019-08-06 13:41:51 +02001215 lineinfo.flags |= (GPIOLINE_FLAG_OPEN_DRAIN |
1216 GPIOLINE_FLAG_IS_OUT);
Linus Walleij9d8cc892016-02-22 13:44:53 +01001217 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Bartosz Golaszewski2c60e6b2019-08-06 13:41:51 +02001218 lineinfo.flags |= (GPIOLINE_FLAG_OPEN_SOURCE |
1219 GPIOLINE_FLAG_IS_OUT);
Kent Gibson2148ad72019-11-05 10:04:25 +08001220 if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
1221 lineinfo.flags |= GPIOLINE_FLAG_BIAS_DISABLE;
Drew Fustini9225d512019-11-05 10:04:23 +08001222 if (test_bit(FLAG_PULL_DOWN, &desc->flags))
1223 lineinfo.flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN;
1224 if (test_bit(FLAG_PULL_UP, &desc->flags))
1225 lineinfo.flags |= GPIOLINE_FLAG_BIAS_PULL_UP;
Linus Walleij521a2ad2016-02-12 22:25:22 +01001226
1227 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
1228 return -EFAULT;
1229 return 0;
Linus Walleijd7c51b42016-04-26 10:35:29 +02001230 } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
1231 return linehandle_create(gdev, ip);
Linus Walleij61f922d2016-06-02 11:30:15 +02001232 } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
1233 return lineevent_create(gdev, ip);
Linus Walleij3c702e92015-10-21 15:29:53 +02001234 }
1235 return -EINVAL;
1236}
1237
Linus Walleij8b92e172016-05-27 14:24:04 +02001238#ifdef CONFIG_COMPAT
1239static long gpio_ioctl_compat(struct file *filp, unsigned int cmd,
1240 unsigned long arg)
1241{
1242 return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
1243}
1244#endif
1245
Linus Walleij3c702e92015-10-21 15:29:53 +02001246/**
1247 * gpio_chrdev_open() - open the chardev for ioctl operations
1248 * @inode: inode for this chardev
1249 * @filp: file struct for storing private data
1250 * Returns 0 on success
1251 */
1252static int gpio_chrdev_open(struct inode *inode, struct file *filp)
1253{
1254 struct gpio_device *gdev = container_of(inode->i_cdev,
1255 struct gpio_device, chrdev);
1256
1257 /* Fail on open if the backing gpiochip is gone */
Stephen Boydfb505742017-01-09 11:47:44 -08001258 if (!gdev->chip)
Linus Walleij3c702e92015-10-21 15:29:53 +02001259 return -ENODEV;
1260 get_device(&gdev->dev);
1261 filp->private_data = gdev;
Lars-Peter Clausenf4e81c52016-11-30 13:05:21 +01001262
1263 return nonseekable_open(inode, filp);
Linus Walleij3c702e92015-10-21 15:29:53 +02001264}
1265
1266/**
1267 * gpio_chrdev_release() - close chardev after ioctl operations
1268 * @inode: inode for this chardev
1269 * @filp: file struct for storing private data
1270 * Returns 0 on success
1271 */
1272static int gpio_chrdev_release(struct inode *inode, struct file *filp)
1273{
1274 struct gpio_device *gdev = container_of(inode->i_cdev,
1275 struct gpio_device, chrdev);
1276
Linus Walleij3c702e92015-10-21 15:29:53 +02001277 put_device(&gdev->dev);
1278 return 0;
1279}
1280
1281
1282static const struct file_operations gpio_fileops = {
1283 .release = gpio_chrdev_release,
1284 .open = gpio_chrdev_open,
1285 .owner = THIS_MODULE,
Lars-Peter Clausenf4e81c52016-11-30 13:05:21 +01001286 .llseek = no_llseek,
Linus Walleij3c702e92015-10-21 15:29:53 +02001287 .unlocked_ioctl = gpio_ioctl,
Linus Walleij8b92e172016-05-27 14:24:04 +02001288#ifdef CONFIG_COMPAT
1289 .compat_ioctl = gpio_ioctl_compat,
1290#endif
Linus Walleij3c702e92015-10-21 15:29:53 +02001291};
1292
Linus Walleijff2b1352015-10-20 11:10:38 +02001293static void gpiodevice_release(struct device *dev)
1294{
1295 struct gpio_device *gdev = dev_get_drvdata(dev);
1296
1297 list_del(&gdev->list);
1298 ida_simple_remove(&gpio_ida, gdev->id);
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +01001299 kfree_const(gdev->label);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001300 kfree(gdev->descs);
Linus Walleij9efd9e62016-02-09 14:27:42 +01001301 kfree(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001302}
1303
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001304static int gpiochip_setup_dev(struct gpio_device *gdev)
1305{
Linus Walleijd377f562019-07-16 11:11:45 +02001306 int ret;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001307
1308 cdev_init(&gdev->chrdev, &gpio_fileops);
1309 gdev->chrdev.owner = THIS_MODULE;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001310 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001311
Linus Walleijd377f562019-07-16 11:11:45 +02001312 ret = cdev_device_add(&gdev->chrdev, &gdev->dev);
1313 if (ret)
1314 return ret;
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001315
1316 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
1317 MAJOR(gpio_devt), gdev->id);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001318
Linus Walleijd377f562019-07-16 11:11:45 +02001319 ret = gpiochip_sysfs_register(gdev);
1320 if (ret)
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001321 goto err_remove_device;
1322
1323 /* From this point, the .release() function cleans up gpio_device */
1324 gdev->dev.release = gpiodevice_release;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001325 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
1326 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
1327 dev_name(&gdev->dev), gdev->chip->label ? : "generic");
1328
1329 return 0;
1330
1331err_remove_device:
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001332 cdev_device_del(&gdev->chrdev, &gdev->dev);
Linus Walleijd377f562019-07-16 11:11:45 +02001333 return ret;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001334}
1335
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02001336static void gpiochip_machine_hog(struct gpio_chip *chip, struct gpiod_hog *hog)
1337{
1338 struct gpio_desc *desc;
1339 int rv;
1340
1341 desc = gpiochip_get_desc(chip, hog->chip_hwnum);
1342 if (IS_ERR(desc)) {
1343 pr_err("%s: unable to get GPIO desc: %ld\n",
1344 __func__, PTR_ERR(desc));
1345 return;
1346 }
1347
Dan Carpenterba3efdf2018-05-01 10:36:39 +03001348 if (test_bit(FLAG_IS_HOGGED, &desc->flags))
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02001349 return;
1350
1351 rv = gpiod_hog(desc, hog->line_name, hog->lflags, hog->dflags);
1352 if (rv)
1353 pr_err("%s: unable to hog GPIO line (%s:%u): %d\n",
1354 __func__, chip->label, hog->chip_hwnum, rv);
1355}
1356
1357static void machine_gpiochip_add(struct gpio_chip *chip)
1358{
1359 struct gpiod_hog *hog;
1360
1361 mutex_lock(&gpio_machine_hogs_mutex);
1362
1363 list_for_each_entry(hog, &gpio_machine_hogs, list) {
1364 if (!strcmp(chip->label, hog->chip_label))
1365 gpiochip_machine_hog(chip, hog);
1366 }
1367
1368 mutex_unlock(&gpio_machine_hogs_mutex);
1369}
1370
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001371static void gpiochip_setup_devs(void)
1372{
1373 struct gpio_device *gdev;
Linus Walleijd377f562019-07-16 11:11:45 +02001374 int ret;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001375
1376 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijd377f562019-07-16 11:11:45 +02001377 ret = gpiochip_setup_dev(gdev);
1378 if (ret)
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001379 pr_err("%s: Failed to initialize gpio device (%d)\n",
Linus Walleijd377f562019-07-16 11:11:45 +02001380 dev_name(&gdev->dev), ret);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001381 }
1382}
1383
Thierry Reding959bc7b2017-11-07 19:15:59 +01001384int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001385 struct lock_class_key *lock_key,
1386 struct lock_class_key *request_key)
David Brownelld2876d02008-02-04 22:28:20 -08001387{
1388 unsigned long flags;
Linus Walleijd377f562019-07-16 11:11:45 +02001389 int ret = 0;
Linus Walleijff2b1352015-10-20 11:10:38 +02001390 unsigned i;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001391 int base = chip->base;
Linus Walleijff2b1352015-10-20 11:10:38 +02001392 struct gpio_device *gdev;
David Brownelld2876d02008-02-04 22:28:20 -08001393
Linus Walleijff2b1352015-10-20 11:10:38 +02001394 /*
1395 * First: allocate and populate the internal stat container, and
1396 * set up the struct device.
1397 */
Josh Cartwright969f07b2016-02-17 16:44:15 -06001398 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
Linus Walleijff2b1352015-10-20 11:10:38 +02001399 if (!gdev)
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001400 return -ENOMEM;
Linus Walleij3c702e92015-10-21 15:29:53 +02001401 gdev->dev.bus = &gpio_bus_type;
Linus Walleijff2b1352015-10-20 11:10:38 +02001402 gdev->chip = chip;
1403 chip->gpiodev = gdev;
1404 if (chip->parent) {
1405 gdev->dev.parent = chip->parent;
1406 gdev->dev.of_node = chip->parent->of_node;
Thierry Redingacc6e332016-07-05 14:11:14 +02001407 }
1408
Linus Walleijff2b1352015-10-20 11:10:38 +02001409#ifdef CONFIG_OF_GPIO
1410 /* If the gpiochip has an assigned OF node this takes precedence */
Thierry Redingacc6e332016-07-05 14:11:14 +02001411 if (chip->of_node)
1412 gdev->dev.of_node = chip->of_node;
Biju Das6ff04972018-08-06 10:48:01 +01001413 else
1414 chip->of_node = gdev->dev.of_node;
Linus Walleijff2b1352015-10-20 11:10:38 +02001415#endif
Thierry Redingacc6e332016-07-05 14:11:14 +02001416
Linus Walleijff2b1352015-10-20 11:10:38 +02001417 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
1418 if (gdev->id < 0) {
Linus Walleijd377f562019-07-16 11:11:45 +02001419 ret = gdev->id;
Linus Walleijff2b1352015-10-20 11:10:38 +02001420 goto err_free_gdev;
1421 }
Geert Uytterhoevenddd88912019-11-27 09:42:47 +01001422 dev_set_name(&gdev->dev, GPIOCHIP_NAME "%d", gdev->id);
Linus Walleijff2b1352015-10-20 11:10:38 +02001423 device_initialize(&gdev->dev);
1424 dev_set_drvdata(&gdev->dev, gdev);
1425 if (chip->parent && chip->parent->driver)
1426 gdev->owner = chip->parent->driver->owner;
1427 else if (chip->owner)
1428 /* TODO: remove chip->owner */
1429 gdev->owner = chip->owner;
1430 else
1431 gdev->owner = THIS_MODULE;
David Brownelld2876d02008-02-04 22:28:20 -08001432
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001433 gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001434 if (!gdev->descs) {
Linus Walleijd377f562019-07-16 11:11:45 +02001435 ret = -ENOMEM;
Vladimir Zapolskiya05a1402018-11-02 15:39:43 +02001436 goto err_free_ida;
Linus Walleijff2b1352015-10-20 11:10:38 +02001437 }
1438
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001439 if (chip->ngpio == 0) {
1440 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
Linus Walleijd377f562019-07-16 11:11:45 +02001441 ret = -EINVAL;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001442 goto err_free_descs;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001443 }
Linus Walleijdf4878e2016-02-12 14:48:23 +01001444
Laura Abbott30277432018-05-21 10:57:07 -07001445 if (chip->ngpio > FASTPATH_NGPIO)
1446 chip_warn(chip, "line cnt %u is greater than fast path cnt %u\n",
Enrico Weigelt, metux IT consult2ddac5a2020-01-04 20:43:34 +01001447 chip->ngpio, FASTPATH_NGPIO);
Laura Abbott30277432018-05-21 10:57:07 -07001448
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +01001449 gdev->label = kstrdup_const(chip->label ?: "unknown", GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001450 if (!gdev->label) {
Linus Walleijd377f562019-07-16 11:11:45 +02001451 ret = -ENOMEM;
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001452 goto err_free_descs;
Linus Walleijdf4878e2016-02-12 14:48:23 +01001453 }
1454
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001455 gdev->ngpio = chip->ngpio;
Linus Walleij43c54ec2016-02-11 11:37:48 +01001456 gdev->data = data;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001457
David Brownelld2876d02008-02-04 22:28:20 -08001458 spin_lock_irqsave(&gpio_lock, flags);
1459
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001460 /*
1461 * TODO: this allocates a Linux GPIO number base in the global
1462 * GPIO numberspace for this chip. In the long run we want to
1463 * get *rid* of this numberspace and use only descriptors, but
1464 * it may be a pipe dream. It will not happen before we get rid
1465 * of the sysfs interface anyways.
1466 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001467 if (base < 0) {
1468 base = gpiochip_find_base(chip->ngpio);
1469 if (base < 0) {
Linus Walleijd377f562019-07-16 11:11:45 +02001470 ret = base;
Johan Hovold225fce82015-01-12 17:12:25 +01001471 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001472 goto err_free_label;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001473 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001474 /*
1475 * TODO: it should not be necessary to reflect the assigned
1476 * base outside of the GPIO subsystem. Go over drivers and
1477 * see if anyone makes use of this, else drop this and assign
1478 * a poison instead.
1479 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001480 chip->base = base;
1481 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001482 gdev->base = base;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001483
Linus Walleijd377f562019-07-16 11:11:45 +02001484 ret = gpiodev_add_to_list(gdev);
1485 if (ret) {
Johan Hovold05aa5202015-01-12 17:12:26 +01001486 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001487 goto err_free_label;
Johan Hovold05aa5202015-01-12 17:12:26 +01001488 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001489
Ricardo Ribalda Delgado767cd172018-10-12 08:11:36 +02001490 for (i = 0; i < chip->ngpio; i++)
1491 gdev->descs[i].gdev = gdev;
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001492
Dan Callaghan207270d2020-01-21 10:12:17 +10001493 spin_unlock_irqrestore(&gpio_lock, flags);
1494
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301495#ifdef CONFIG_PINCTRL
Linus Walleij20ec3e32016-02-11 11:03:06 +01001496 INIT_LIST_HEAD(&gdev->pin_ranges);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301497#endif
1498
Linus Walleijd377f562019-07-16 11:11:45 +02001499 ret = gpiochip_set_desc_names(chip);
1500 if (ret)
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001501 goto err_remove_from_list;
1502
Linus Walleij151a4102019-09-05 11:40:54 +02001503 ret = gpiochip_alloc_valid_mask(chip);
Linus Walleijd377f562019-07-16 11:11:45 +02001504 if (ret)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001505 goto err_remove_from_list;
1506
Linus Walleijd377f562019-07-16 11:11:45 +02001507 ret = of_gpiochip_add(chip);
1508 if (ret)
Linus Walleij48057ed2019-08-20 10:05:27 +02001509 goto err_free_gpiochip_mask;
Tomeu Vizoso28355f82015-07-14 10:29:54 +02001510
Linus Walleijd377f562019-07-16 11:11:45 +02001511 ret = gpiochip_init_valid_mask(chip);
1512 if (ret)
Geert Uytterhoeven35779892019-04-24 15:59:33 +02001513 goto err_remove_of_chip;
Ricardo Ribalda Delgadof8ec92a2018-10-05 08:52:58 +02001514
Ricardo Ribalda Delgado3edfb7b2018-10-05 08:53:00 +02001515 for (i = 0; i < chip->ngpio; i++) {
1516 struct gpio_desc *desc = &gdev->descs[i];
1517
Chris Packhamd95da992019-07-08 08:35:58 +12001518 if (chip->get_direction && gpiochip_line_is_valid(chip, i)) {
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +02001519 assign_bit(FLAG_IS_OUT,
1520 &desc->flags, !chip->get_direction(chip, i));
Chris Packhamd95da992019-07-08 08:35:58 +12001521 } else {
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +02001522 assign_bit(FLAG_IS_OUT,
1523 &desc->flags, !chip->direction_input);
Chris Packhamd95da992019-07-08 08:35:58 +12001524 }
Ricardo Ribalda Delgado3edfb7b2018-10-05 08:53:00 +02001525 }
1526
Andy Shevchenkob056ca12019-11-04 18:09:39 +02001527 ret = gpiochip_add_pin_ranges(chip);
1528 if (ret)
1529 goto err_remove_of_chip;
1530
Mika Westerberg664e3e52014-01-08 12:40:54 +02001531 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001532
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02001533 machine_gpiochip_add(chip);
1534
Linus Walleij504369c2019-10-30 13:29:14 +01001535 ret = gpiochip_irqchip_init_valid_mask(chip);
Andy Shevchenko9411e3a2019-10-09 17:34:44 +03001536 if (ret)
1537 goto err_remove_acpi_chip;
1538
Linus Walleij504369c2019-10-30 13:29:14 +01001539 ret = gpiochip_irqchip_init_hw(chip);
Linus Walleijfbdf8d42019-09-06 12:05:35 +02001540 if (ret)
Linus Walleij48057ed2019-08-20 10:05:27 +02001541 goto err_remove_acpi_chip;
1542
Linus Walleijfbdf8d42019-09-06 12:05:35 +02001543 ret = gpiochip_add_irqchip(chip, lock_key, request_key);
1544 if (ret)
Linus Walleij48057ed2019-08-20 10:05:27 +02001545 goto err_remove_irqchip_mask;
1546
Linus Walleij3c702e92015-10-21 15:29:53 +02001547 /*
1548 * By first adding the chardev, and then adding the device,
1549 * we get a device node entry in sysfs under
1550 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1551 * coldplug of device nodes and other udev business.
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001552 * We can do this only if gpiolib has been initialized.
1553 * Otherwise, defer until later.
Linus Walleij3c702e92015-10-21 15:29:53 +02001554 */
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001555 if (gpiolib_initialized) {
Linus Walleijd377f562019-07-16 11:11:45 +02001556 ret = gpiochip_setup_dev(gdev);
1557 if (ret)
Linus Walleij48057ed2019-08-20 10:05:27 +02001558 goto err_remove_irqchip;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001559 }
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001560 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001561
Linus Walleij48057ed2019-08-20 10:05:27 +02001562err_remove_irqchip:
1563 gpiochip_irqchip_remove(chip);
1564err_remove_irqchip_mask:
1565 gpiochip_irqchip_free_valid_mask(chip);
Geert Uytterhoeven35779892019-04-24 15:59:33 +02001566err_remove_acpi_chip:
Johan Hovold225fce82015-01-12 17:12:25 +01001567 acpi_gpiochip_remove(chip);
Geert Uytterhoeven35779892019-04-24 15:59:33 +02001568err_remove_of_chip:
Johan Hovold6d867502015-05-04 17:23:25 +02001569 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +01001570 of_gpiochip_remove(chip);
Geert Uytterhoeven35779892019-04-24 15:59:33 +02001571err_free_gpiochip_mask:
Andy Shevchenko2f4133b2019-11-05 20:06:54 +02001572 gpiochip_remove_pin_ranges(chip);
Stephen Boyd726cb3b2018-03-23 09:34:52 -07001573 gpiochip_free_valid_mask(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001574err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +01001575 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001576 list_del(&gdev->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001577 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001578err_free_label:
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +01001579 kfree_const(gdev->label);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001580err_free_descs:
1581 kfree(gdev->descs);
Vladimir Zapolskiya05a1402018-11-02 15:39:43 +02001582err_free_ida:
Linus Walleijff2b1352015-10-20 11:10:38 +02001583 ida_simple_remove(&gpio_ida, gdev->id);
Vladimir Zapolskiya05a1402018-11-02 15:39:43 +02001584err_free_gdev:
David Brownelld2876d02008-02-04 22:28:20 -08001585 /* failures here can mean systems won't boot... */
Marcel Ziswiler1777fc92018-07-20 09:54:49 +02001586 pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__,
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001587 gdev->base, gdev->base + gdev->ngpio - 1,
Linus Walleijd377f562019-07-16 11:11:45 +02001588 chip->label ? : "generic", ret);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001589 kfree(gdev);
Linus Walleijd377f562019-07-16 11:11:45 +02001590 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08001591}
Thierry Reding959bc7b2017-11-07 19:15:59 +01001592EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);
David Brownelld2876d02008-02-04 22:28:20 -08001593
1594/**
Linus Walleij43c54ec2016-02-11 11:37:48 +01001595 * gpiochip_get_data() - get per-subdriver data for the chip
Thierry Reding950d55f52017-07-24 16:57:22 +02001596 * @chip: GPIO chip
1597 *
1598 * Returns:
1599 * The per-subdriver data for the chip.
Linus Walleij43c54ec2016-02-11 11:37:48 +01001600 */
1601void *gpiochip_get_data(struct gpio_chip *chip)
1602{
1603 return chip->gpiodev->data;
1604}
1605EXPORT_SYMBOL_GPL(gpiochip_get_data);
1606
1607/**
David Brownelld2876d02008-02-04 22:28:20 -08001608 * gpiochip_remove() - unregister a gpio_chip
1609 * @chip: the chip to unregister
1610 *
1611 * A gpio_chip with any GPIOs still requested may not be removed.
1612 */
abdoulaye berthee1db1702014-07-05 18:28:50 +02001613void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -08001614{
Linus Walleijff2b1352015-10-20 11:10:38 +02001615 struct gpio_device *gdev = chip->gpiodev;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001616 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001617 unsigned long flags;
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001618 unsigned i;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001619 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -08001620
Linus Walleijff2b1352015-10-20 11:10:38 +02001621 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
Linus Walleijafbc4f32016-02-09 13:21:06 +01001622 gpiochip_sysfs_unregister(gdev);
Geert Uytterhoeven5018ada2016-12-19 18:29:23 +01001623 gpiochip_free_hogs(chip);
Bamvor Jian Zhangbd203bd2016-02-20 13:13:19 +08001624 /* Numb the device, cancelling all outstanding operations */
1625 gdev->chip = NULL;
Johan Hovold00acc3d2015-01-12 17:12:27 +01001626 gpiochip_irqchip_remove(chip);
Mika Westerberg6072b9d2014-03-10 14:54:53 +02001627 acpi_gpiochip_remove(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001628 of_gpiochip_remove(chip);
Andy Shevchenko2f4133b2019-11-05 20:06:54 +02001629 gpiochip_remove_pin_ranges(chip);
Stephen Boyd726cb3b2018-03-23 09:34:52 -07001630 gpiochip_free_valid_mask(chip);
Linus Walleij43c54ec2016-02-11 11:37:48 +01001631 /*
1632 * We accept no more calls into the driver from this point, so
1633 * NULL the driver data pointer
1634 */
1635 gdev->data = NULL;
Anton Vorontsov391c9702010-06-08 07:48:17 -06001636
Johan Hovold6798aca2015-01-12 17:12:28 +01001637 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001638 for (i = 0; i < gdev->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001639 desc = &gdev->descs[i];
Johan Hovoldfab28b82015-05-04 17:10:27 +02001640 if (test_bit(FLAG_REQUESTED, &desc->flags))
1641 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -08001642 }
David Brownelld2876d02008-02-04 22:28:20 -08001643 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001644
Johan Hovoldfab28b82015-05-04 17:10:27 +02001645 if (requested)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001646 dev_crit(&gdev->dev,
Linus Walleij58383c782015-11-04 09:56:26 +01001647 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +02001648
Linus Walleijff2b1352015-10-20 11:10:38 +02001649 /*
1650 * The gpiochip side puts its use of the device to rest here:
1651 * if there are no userspace clients, the chardev and device will
1652 * be removed, else it will be dangling until the last user is
1653 * gone.
1654 */
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001655 cdev_device_del(&gdev->chrdev, &gdev->dev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001656 put_device(&gdev->dev);
David Brownelld2876d02008-02-04 22:28:20 -08001657}
1658EXPORT_SYMBOL_GPL(gpiochip_remove);
1659
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301660static void devm_gpio_chip_release(struct device *dev, void *res)
1661{
1662 struct gpio_chip *chip = *(struct gpio_chip **)res;
1663
1664 gpiochip_remove(chip);
1665}
1666
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301667/**
Geert Uytterhoeven51158412019-11-20 15:37:45 +01001668 * devm_gpiochip_add_data() - Resource managed gpiochip_add_data()
Uwe Kleine-König3925b90f2018-10-08 11:34:03 +02001669 * @dev: pointer to the device that gpio_chip belongs to.
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301670 * @chip: the chip to register, with chip->base initialized
Thierry Reding950d55f52017-07-24 16:57:22 +02001671 * @data: driver-private data associated with this chip
1672 *
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301673 * Context: potentially before irqs will work
1674 *
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301675 * The gpio chip automatically be released when the device is unbound.
Thierry Reding950d55f52017-07-24 16:57:22 +02001676 *
1677 * Returns:
1678 * A negative errno if the chip can't be registered, such as because the
1679 * chip->base is invalid or already associated with a different chip.
1680 * Otherwise it returns zero as a success code.
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301681 */
1682int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
1683 void *data)
1684{
1685 struct gpio_chip **ptr;
1686 int ret;
1687
1688 ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
1689 GFP_KERNEL);
1690 if (!ptr)
1691 return -ENOMEM;
1692
1693 ret = gpiochip_add_data(chip, data);
1694 if (ret < 0) {
1695 devres_free(ptr);
1696 return ret;
1697 }
1698
1699 *ptr = chip;
1700 devres_add(dev, ptr);
1701
1702 return 0;
1703}
1704EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
1705
1706/**
Grant Likely594fa262010-06-08 07:48:16 -06001707 * gpiochip_find() - iterator for locating a specific gpio_chip
1708 * @data: data to pass to match function
Thierry Reding950d55f52017-07-24 16:57:22 +02001709 * @match: Callback function to check gpio_chip
Grant Likely594fa262010-06-08 07:48:16 -06001710 *
1711 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1712 * determined by a user supplied @match callback. The callback should return
1713 * 0 if the device doesn't match and non-zero if it does. If the callback is
1714 * non-zero, this function will return to the caller and not iterate over any
1715 * more gpio_chips.
1716 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001717struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001718 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001719 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001720{
Linus Walleijff2b1352015-10-20 11:10:38 +02001721 struct gpio_device *gdev;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001722 struct gpio_chip *chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -06001723 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001724
1725 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001726 list_for_each_entry(gdev, &gpio_devices, list)
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001727 if (gdev->chip && match(gdev->chip, data)) {
1728 chip = gdev->chip;
Grant Likely594fa262010-06-08 07:48:16 -06001729 break;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001730 }
Linus Walleijff2b1352015-10-20 11:10:38 +02001731
Grant Likely594fa262010-06-08 07:48:16 -06001732 spin_unlock_irqrestore(&gpio_lock, flags);
1733
1734 return chip;
1735}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001736EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001737
Alexandre Courbot79697ef2013-11-16 21:39:32 +09001738static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1739{
1740 const char *name = data;
1741
1742 return !strcmp(chip->label, name);
1743}
1744
1745static struct gpio_chip *find_chip_by_name(const char *name)
1746{
1747 return gpiochip_find((void *)name, gpiochip_match_name);
1748}
1749
Linus Walleij14250522014-03-25 10:40:18 +01001750#ifdef CONFIG_GPIOLIB_IRQCHIP
1751
1752/*
1753 * The following is irqchip helper code for gpiochips.
1754 */
1755
Andy Shevchenko9411e3a2019-10-09 17:34:44 +03001756static int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
1757{
1758 struct gpio_irq_chip *girq = &gc->irq;
1759
1760 if (!girq->init_hw)
1761 return 0;
1762
1763 return girq->init_hw(gc);
1764}
1765
Linus Walleij5fbe5b52019-09-04 16:01:04 +02001766static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001767{
Linus Walleij5fbe5b52019-09-04 16:01:04 +02001768 struct gpio_irq_chip *girq = &gc->irq;
1769
1770 if (!girq->init_valid_mask)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001771 return 0;
1772
Linus Walleij5fbe5b52019-09-04 16:01:04 +02001773 girq->valid_mask = gpiochip_allocate_mask(gc);
1774 if (!girq->valid_mask)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001775 return -ENOMEM;
1776
Linus Walleij5fbe5b52019-09-04 16:01:04 +02001777 girq->init_valid_mask(gc, girq->valid_mask, gc->ngpio);
1778
Mika Westerberg79b804c2016-09-20 15:15:21 +03001779 return 0;
1780}
1781
1782static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1783{
Masahiro Yamada7bdbd1ec2019-07-18 15:51:01 +09001784 bitmap_free(gpiochip->irq.valid_mask);
Thierry Redingdc7b0382017-11-07 19:15:52 +01001785 gpiochip->irq.valid_mask = NULL;
Mika Westerberg79b804c2016-09-20 15:15:21 +03001786}
1787
Stephen Boyd64ff2c82018-01-09 17:58:46 -08001788bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
1789 unsigned int offset)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001790{
Stephen Boyd726cb3b2018-03-23 09:34:52 -07001791 if (!gpiochip_line_is_valid(gpiochip, offset))
1792 return false;
Mika Westerberg79b804c2016-09-20 15:15:21 +03001793 /* No mask means all valid */
Thierry Redingdc7b0382017-11-07 19:15:52 +01001794 if (likely(!gpiochip->irq.valid_mask))
Mika Westerberg79b804c2016-09-20 15:15:21 +03001795 return true;
Thierry Redingdc7b0382017-11-07 19:15:52 +01001796 return test_bit(offset, gpiochip->irq.valid_mask);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001797}
Stephen Boyd64ff2c82018-01-09 17:58:46 -08001798EXPORT_SYMBOL_GPL(gpiochip_irqchip_irq_valid);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001799
Linus Walleij14250522014-03-25 10:40:18 +01001800/**
Linus Walleijd245b3f2016-11-24 10:57:25 +01001801 * gpiochip_set_cascaded_irqchip() - connects a cascaded irqchip to a gpiochip
Linus Walleij4892d3a2019-06-14 10:12:26 +02001802 * @gc: the gpiochip to set the irqchip chain to
Linus Walleij14250522014-03-25 10:40:18 +01001803 * @parent_irq: the irq number corresponding to the parent IRQ for this
Linus Walleij72780ce2020-01-13 23:08:00 +01001804 * cascaded irqchip
Linus Walleij14250522014-03-25 10:40:18 +01001805 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001806 * coming out of the gpiochip. If the interrupt is nested rather than
1807 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +01001808 */
Linus Walleij4892d3a2019-06-14 10:12:26 +02001809static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gc,
Thierry Reding6f793092017-04-03 18:05:21 +02001810 unsigned int parent_irq,
Linus Walleijd245b3f2016-11-24 10:57:25 +01001811 irq_flow_handler_t parent_handler)
Linus Walleij14250522014-03-25 10:40:18 +01001812{
Linus Walleij4892d3a2019-06-14 10:12:26 +02001813 struct gpio_irq_chip *girq = &gc->irq;
1814 struct device *dev = &gc->gpiodev->dev;
1815
1816 if (!girq->domain) {
1817 chip_err(gc, "called %s before setting up irqchip\n",
Linus Walleij83141a72014-09-26 13:50:12 +02001818 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +02001819 return;
1820 }
1821
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001822 if (parent_handler) {
Linus Walleij4892d3a2019-06-14 10:12:26 +02001823 if (gc->can_sleep) {
1824 chip_err(gc,
Andy Shevchenkob1911712018-07-03 03:39:03 +03001825 "you cannot have chained interrupts on a chip that may sleep\n");
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001826 return;
1827 }
Linus Walleij4892d3a2019-06-14 10:12:26 +02001828 girq->parents = devm_kcalloc(dev, 1,
1829 sizeof(*girq->parents),
1830 GFP_KERNEL);
1831 if (!girq->parents) {
1832 chip_err(gc, "out of memory allocating parent IRQ\n");
1833 return;
1834 }
1835 girq->parents[0] = parent_irq;
1836 girq->num_parents = 1;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001837 /*
1838 * The parent irqchip is already using the chip_data for this
1839 * irqchip, so our callbacks simply use the handler_data.
1840 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +02001841 irq_set_chained_handler_and_data(parent_irq, parent_handler,
Linus Walleij4892d3a2019-06-14 10:12:26 +02001842 gc);
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001843 }
Linus Walleij14250522014-03-25 10:40:18 +01001844}
Linus Walleijd245b3f2016-11-24 10:57:25 +01001845
1846/**
Linus Walleijd245b3f2016-11-24 10:57:25 +01001847 * gpiochip_set_nested_irqchip() - connects a nested irqchip to a gpiochip
1848 * @gpiochip: the gpiochip to set the irqchip nested handler to
1849 * @irqchip: the irqchip to nest to the gpiochip
1850 * @parent_irq: the irq number corresponding to the parent IRQ for this
1851 * nested irqchip
1852 */
1853void gpiochip_set_nested_irqchip(struct gpio_chip *gpiochip,
1854 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001855 unsigned int parent_irq)
Linus Walleijd245b3f2016-11-24 10:57:25 +01001856{
Stephen Boyd3c1f6b22018-10-08 09:32:15 -07001857 gpiochip_set_cascaded_irqchip(gpiochip, parent_irq, NULL);
Linus Walleijd245b3f2016-11-24 10:57:25 +01001858}
1859EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip);
1860
Linus Walleijfdd61a02019-08-08 14:32:37 +02001861#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1862
1863/**
1864 * gpiochip_set_hierarchical_irqchip() - connects a hierarchical irqchip
1865 * to a gpiochip
1866 * @gc: the gpiochip to set the irqchip hierarchical handler to
1867 * @irqchip: the irqchip to handle this level of the hierarchy, the interrupt
1868 * will then percolate up to the parent
1869 */
1870static void gpiochip_set_hierarchical_irqchip(struct gpio_chip *gc,
1871 struct irq_chip *irqchip)
1872{
1873 /* DT will deal with mapping each IRQ as we go along */
1874 if (is_of_node(gc->irq.fwnode))
1875 return;
1876
1877 /*
1878 * This is for legacy and boardfile "irqchip" fwnodes: allocate
1879 * irqs upfront instead of dynamically since we don't have the
1880 * dynamic type of allocation that hardware description languages
1881 * provide. Once all GPIO drivers using board files are gone from
1882 * the kernel we can delete this code, but for a transitional period
1883 * it is necessary to keep this around.
1884 */
1885 if (is_fwnode_irqchip(gc->irq.fwnode)) {
1886 int i;
1887 int ret;
1888
1889 for (i = 0; i < gc->ngpio; i++) {
1890 struct irq_fwspec fwspec;
1891 unsigned int parent_hwirq;
1892 unsigned int parent_type;
1893 struct gpio_irq_chip *girq = &gc->irq;
1894
1895 /*
1896 * We call the child to parent translation function
1897 * only to check if the child IRQ is valid or not.
1898 * Just pick the rising edge type here as that is what
1899 * we likely need to support.
1900 */
1901 ret = girq->child_to_parent_hwirq(gc, i,
1902 IRQ_TYPE_EDGE_RISING,
1903 &parent_hwirq,
1904 &parent_type);
1905 if (ret) {
1906 chip_err(gc, "skip set-up on hwirq %d\n",
1907 i);
1908 continue;
1909 }
1910
1911 fwspec.fwnode = gc->irq.fwnode;
1912 /* This is the hwirq for the GPIO line side of things */
1913 fwspec.param[0] = girq->child_offset_to_irq(gc, i);
1914 /* Just pick something */
1915 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
1916 fwspec.param_count = 2;
1917 ret = __irq_domain_alloc_irqs(gc->irq.domain,
1918 /* just pick something */
1919 -1,
1920 1,
1921 NUMA_NO_NODE,
1922 &fwspec,
1923 false,
1924 NULL);
1925 if (ret < 0) {
1926 chip_err(gc,
1927 "can not allocate irq for GPIO line %d parent hwirq %d in hierarchy domain: %d\n",
1928 i, parent_hwirq,
1929 ret);
1930 }
1931 }
1932 }
1933
1934 chip_err(gc, "%s unknown fwnode type proceed anyway\n", __func__);
1935
1936 return;
1937}
1938
1939static int gpiochip_hierarchy_irq_domain_translate(struct irq_domain *d,
1940 struct irq_fwspec *fwspec,
1941 unsigned long *hwirq,
1942 unsigned int *type)
1943{
1944 /* We support standard DT translation */
1945 if (is_of_node(fwspec->fwnode) && fwspec->param_count == 2) {
1946 return irq_domain_translate_twocell(d, fwspec, hwirq, type);
1947 }
1948
1949 /* This is for board files and others not using DT */
1950 if (is_fwnode_irqchip(fwspec->fwnode)) {
1951 int ret;
1952
1953 ret = irq_domain_translate_twocell(d, fwspec, hwirq, type);
1954 if (ret)
1955 return ret;
1956 WARN_ON(*type == IRQ_TYPE_NONE);
1957 return 0;
1958 }
1959 return -EINVAL;
1960}
1961
1962static int gpiochip_hierarchy_irq_domain_alloc(struct irq_domain *d,
1963 unsigned int irq,
1964 unsigned int nr_irqs,
1965 void *data)
1966{
1967 struct gpio_chip *gc = d->host_data;
1968 irq_hw_number_t hwirq;
1969 unsigned int type = IRQ_TYPE_NONE;
1970 struct irq_fwspec *fwspec = data;
Kevin Hao24258762020-01-14 16:28:19 +08001971 void *parent_arg;
Linus Walleijfdd61a02019-08-08 14:32:37 +02001972 unsigned int parent_hwirq;
1973 unsigned int parent_type;
1974 struct gpio_irq_chip *girq = &gc->irq;
1975 int ret;
1976
1977 /*
1978 * The nr_irqs parameter is always one except for PCI multi-MSI
1979 * so this should not happen.
1980 */
1981 WARN_ON(nr_irqs != 1);
1982
1983 ret = gc->irq.child_irq_domain_ops.translate(d, fwspec, &hwirq, &type);
1984 if (ret)
1985 return ret;
1986
Kevin Hao366950e2020-01-20 17:56:25 +08001987 chip_dbg(gc, "allocate IRQ %d, hwirq %lu\n", irq, hwirq);
Linus Walleijfdd61a02019-08-08 14:32:37 +02001988
1989 ret = girq->child_to_parent_hwirq(gc, hwirq, type,
1990 &parent_hwirq, &parent_type);
1991 if (ret) {
1992 chip_err(gc, "can't look up hwirq %lu\n", hwirq);
1993 return ret;
1994 }
Kevin Hao366950e2020-01-20 17:56:25 +08001995 chip_dbg(gc, "found parent hwirq %u\n", parent_hwirq);
Linus Walleijfdd61a02019-08-08 14:32:37 +02001996
1997 /*
1998 * We set handle_bad_irq because the .set_type() should
1999 * always be invoked and set the right type of handler.
2000 */
2001 irq_domain_set_info(d,
2002 irq,
2003 hwirq,
2004 gc->irq.chip,
2005 gc,
2006 girq->handler,
2007 NULL, NULL);
2008 irq_set_probe(irq);
2009
Linus Walleijfdd61a02019-08-08 14:32:37 +02002010 /* This parent only handles asserted level IRQs */
Kevin Hao24258762020-01-14 16:28:19 +08002011 parent_arg = girq->populate_parent_alloc_arg(gc, parent_hwirq, parent_type);
2012 if (!parent_arg)
2013 return -ENOMEM;
2014
Kevin Hao366950e2020-01-20 17:56:25 +08002015 chip_dbg(gc, "alloc_irqs_parent for %d parent hwirq %d\n",
Linus Walleijfdd61a02019-08-08 14:32:37 +02002016 irq, parent_hwirq);
Stephen Boydc34f6dc2020-01-14 15:11:03 -08002017 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key);
Kevin Hao24258762020-01-14 16:28:19 +08002018 ret = irq_domain_alloc_irqs_parent(d, irq, 1, parent_arg);
Kevin Hao880b7cf2020-01-14 16:28:20 +08002019 /*
2020 * If the parent irqdomain is msi, the interrupts have already
2021 * been allocated, so the EEXIST is good.
2022 */
2023 if (irq_domain_is_msi(d->parent) && (ret == -EEXIST))
2024 ret = 0;
Linus Walleijfdd61a02019-08-08 14:32:37 +02002025 if (ret)
2026 chip_err(gc,
2027 "failed to allocate parent hwirq %d for hwirq %lu\n",
2028 parent_hwirq, hwirq);
2029
Kevin Hao24258762020-01-14 16:28:19 +08002030 kfree(parent_arg);
Linus Walleijfdd61a02019-08-08 14:32:37 +02002031 return ret;
2032}
2033
2034static unsigned int gpiochip_child_offset_to_irq_noop(struct gpio_chip *chip,
2035 unsigned int offset)
2036{
2037 return offset;
2038}
2039
2040static void gpiochip_hierarchy_setup_domain_ops(struct irq_domain_ops *ops)
2041{
2042 ops->activate = gpiochip_irq_domain_activate;
2043 ops->deactivate = gpiochip_irq_domain_deactivate;
2044 ops->alloc = gpiochip_hierarchy_irq_domain_alloc;
2045 ops->free = irq_domain_free_irqs_common;
2046
2047 /*
2048 * We only allow overriding the translate() function for
2049 * hierarchical chips, and this should only be done if the user
2050 * really need something other than 1:1 translation.
2051 */
2052 if (!ops->translate)
2053 ops->translate = gpiochip_hierarchy_irq_domain_translate;
2054}
2055
2056static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
2057{
2058 if (!gc->irq.child_to_parent_hwirq ||
2059 !gc->irq.fwnode) {
2060 chip_err(gc, "missing irqdomain vital data\n");
2061 return -EINVAL;
2062 }
2063
2064 if (!gc->irq.child_offset_to_irq)
2065 gc->irq.child_offset_to_irq = gpiochip_child_offset_to_irq_noop;
2066
Kevin Hao24258762020-01-14 16:28:19 +08002067 if (!gc->irq.populate_parent_alloc_arg)
2068 gc->irq.populate_parent_alloc_arg =
Linus Walleijfdd61a02019-08-08 14:32:37 +02002069 gpiochip_populate_parent_fwspec_twocell;
2070
2071 gpiochip_hierarchy_setup_domain_ops(&gc->irq.child_irq_domain_ops);
2072
2073 gc->irq.domain = irq_domain_create_hierarchy(
2074 gc->irq.parent_domain,
2075 0,
2076 gc->ngpio,
2077 gc->irq.fwnode,
2078 &gc->irq.child_irq_domain_ops,
2079 gc);
2080
2081 if (!gc->irq.domain)
2082 return -ENOMEM;
2083
2084 gpiochip_set_hierarchical_irqchip(gc, gc->irq.chip);
2085
2086 return 0;
2087}
2088
2089static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
2090{
2091 return !!gc->irq.parent_domain;
2092}
2093
Kevin Hao24258762020-01-14 16:28:19 +08002094void *gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *chip,
Linus Walleijfdd61a02019-08-08 14:32:37 +02002095 unsigned int parent_hwirq,
2096 unsigned int parent_type)
2097{
Kevin Hao24258762020-01-14 16:28:19 +08002098 struct irq_fwspec *fwspec;
2099
2100 fwspec = kmalloc(sizeof(*fwspec), GFP_KERNEL);
2101 if (!fwspec)
2102 return NULL;
2103
2104 fwspec->fwnode = chip->irq.parent_domain->fwnode;
Linus Walleijfdd61a02019-08-08 14:32:37 +02002105 fwspec->param_count = 2;
2106 fwspec->param[0] = parent_hwirq;
2107 fwspec->param[1] = parent_type;
Kevin Hao24258762020-01-14 16:28:19 +08002108
2109 return fwspec;
Linus Walleijfdd61a02019-08-08 14:32:37 +02002110}
2111EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_twocell);
2112
Kevin Hao24258762020-01-14 16:28:19 +08002113void *gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *chip,
Linus Walleijfdd61a02019-08-08 14:32:37 +02002114 unsigned int parent_hwirq,
2115 unsigned int parent_type)
2116{
Kevin Hao24258762020-01-14 16:28:19 +08002117 struct irq_fwspec *fwspec;
2118
2119 fwspec = kmalloc(sizeof(*fwspec), GFP_KERNEL);
2120 if (!fwspec)
2121 return NULL;
2122
2123 fwspec->fwnode = chip->irq.parent_domain->fwnode;
Linus Walleijfdd61a02019-08-08 14:32:37 +02002124 fwspec->param_count = 4;
2125 fwspec->param[0] = 0;
2126 fwspec->param[1] = parent_hwirq;
2127 fwspec->param[2] = 0;
2128 fwspec->param[3] = parent_type;
Kevin Hao24258762020-01-14 16:28:19 +08002129
2130 return fwspec;
Linus Walleijfdd61a02019-08-08 14:32:37 +02002131}
2132EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_fourcell);
2133
2134#else
2135
2136static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
2137{
2138 return -EINVAL;
2139}
2140
2141static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
2142{
2143 return false;
2144}
2145
2146#endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
2147
Linus Walleijd245b3f2016-11-24 10:57:25 +01002148/**
Linus Walleij14250522014-03-25 10:40:18 +01002149 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
2150 * @d: the irqdomain used by this irqchip
2151 * @irq: the global irq number used by this GPIO irqchip irq
2152 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
2153 *
2154 * This function will set up the mapping for a certain IRQ line on a
2155 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
2156 * stored inside the gpiochip.
2157 */
Thierry Reding1b95b4e2017-11-07 19:15:55 +01002158int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
2159 irq_hw_number_t hwirq)
Linus Walleij14250522014-03-25 10:40:18 +01002160{
2161 struct gpio_chip *chip = d->host_data;
Linus Walleijd377f562019-07-16 11:11:45 +02002162 int ret = 0;
Linus Walleij14250522014-03-25 10:40:18 +01002163
Grygorii Strashkodc749a02017-07-21 11:49:00 -05002164 if (!gpiochip_irqchip_irq_valid(chip, hwirq))
2165 return -ENXIO;
2166
Linus Walleij14250522014-03-25 10:40:18 +01002167 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03002168 /*
2169 * This lock class tells lockdep that GPIO irqs are in a different
2170 * category than their parents, so it won't report false recursion.
2171 */
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002172 irq_set_lockdep_class(irq, chip->irq.lock_key, chip->irq.request_key);
Thierry Redingc7a0aa52017-11-07 19:15:48 +01002173 irq_set_chip_and_handler(irq, chip->irq.chip, chip->irq.handler);
Linus Walleijd245b3f2016-11-24 10:57:25 +01002174 /* Chips that use nested thread handlers have them marked */
Thierry Reding60ed54c2017-11-07 19:15:57 +01002175 if (chip->irq.threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02002176 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +01002177 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -05002178
Thierry Redinge0d89722017-11-07 19:15:54 +01002179 if (chip->irq.num_parents == 1)
Linus Walleijd377f562019-07-16 11:11:45 +02002180 ret = irq_set_parent(irq, chip->irq.parents[0]);
Thierry Redinge0d89722017-11-07 19:15:54 +01002181 else if (chip->irq.map)
Linus Walleijd377f562019-07-16 11:11:45 +02002182 ret = irq_set_parent(irq, chip->irq.map[hwirq]);
Thierry Redinge0d89722017-11-07 19:15:54 +01002183
Linus Walleijd377f562019-07-16 11:11:45 +02002184 if (ret < 0)
2185 return ret;
Thierry Redinge0d89722017-11-07 19:15:54 +01002186
Linus Walleij1333b902014-04-23 16:45:12 +02002187 /*
2188 * No set-up of the hardware will happen if IRQ_TYPE_NONE
2189 * is passed as default type.
2190 */
Thierry Reding3634eeb2017-11-07 19:15:49 +01002191 if (chip->irq.default_type != IRQ_TYPE_NONE)
2192 irq_set_irq_type(irq, chip->irq.default_type);
Linus Walleij14250522014-03-25 10:40:18 +01002193
2194 return 0;
2195}
Thierry Reding1b95b4e2017-11-07 19:15:55 +01002196EXPORT_SYMBOL_GPL(gpiochip_irq_map);
Linus Walleij14250522014-03-25 10:40:18 +01002197
Thierry Reding1b95b4e2017-11-07 19:15:55 +01002198void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
Linus Walleijc3626fd2014-03-28 20:42:01 +01002199{
Linus Walleij1c8732b2014-04-09 13:34:39 +02002200 struct gpio_chip *chip = d->host_data;
2201
Thierry Reding60ed54c2017-11-07 19:15:57 +01002202 if (chip->irq.threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02002203 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +01002204 irq_set_chip_and_handler(irq, NULL, NULL);
2205 irq_set_chip_data(irq, NULL);
2206}
Thierry Reding1b95b4e2017-11-07 19:15:55 +01002207EXPORT_SYMBOL_GPL(gpiochip_irq_unmap);
Linus Walleijc3626fd2014-03-28 20:42:01 +01002208
Linus Walleij14250522014-03-25 10:40:18 +01002209static const struct irq_domain_ops gpiochip_domain_ops = {
2210 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +01002211 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +01002212 /* Virtually all GPIO irqchips are twocell:ed */
2213 .xlate = irq_domain_xlate_twocell,
2214};
2215
Linus Walleijfdd61a02019-08-08 14:32:37 +02002216/*
2217 * TODO: move these activate/deactivate in under the hierarchicial
2218 * irqchip implementation as static once SPMI and SSBI (all external
2219 * users) are phased over.
2220 */
Brian Masneyef74f702019-01-19 15:42:42 -05002221/**
2222 * gpiochip_irq_domain_activate() - Lock a GPIO to be used as an IRQ
2223 * @domain: The IRQ domain used by this IRQ chip
2224 * @data: Outermost irq_data associated with the IRQ
2225 * @reserve: If set, only reserve an interrupt vector instead of assigning one
2226 *
2227 * This function is a wrapper that calls gpiochip_lock_as_irq() and is to be
2228 * used as the activate function for the &struct irq_domain_ops. The host_data
2229 * for the IRQ domain must be the &struct gpio_chip.
2230 */
2231int gpiochip_irq_domain_activate(struct irq_domain *domain,
2232 struct irq_data *data, bool reserve)
2233{
2234 struct gpio_chip *chip = domain->host_data;
2235
2236 return gpiochip_lock_as_irq(chip, data->hwirq);
2237}
2238EXPORT_SYMBOL_GPL(gpiochip_irq_domain_activate);
2239
2240/**
2241 * gpiochip_irq_domain_deactivate() - Unlock a GPIO used as an IRQ
2242 * @domain: The IRQ domain used by this IRQ chip
2243 * @data: Outermost irq_data associated with the IRQ
2244 *
2245 * This function is a wrapper that will call gpiochip_unlock_as_irq() and is to
2246 * be used as the deactivate function for the &struct irq_domain_ops. The
2247 * host_data for the IRQ domain must be the &struct gpio_chip.
2248 */
2249void gpiochip_irq_domain_deactivate(struct irq_domain *domain,
2250 struct irq_data *data)
2251{
2252 struct gpio_chip *chip = domain->host_data;
2253
2254 return gpiochip_unlock_as_irq(chip, data->hwirq);
2255}
2256EXPORT_SYMBOL_GPL(gpiochip_irq_domain_deactivate);
2257
Linus Walleij14250522014-03-25 10:40:18 +01002258static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
2259{
Linus Walleijfdd61a02019-08-08 14:32:37 +02002260 struct irq_domain *domain = chip->irq.domain;
2261
Grygorii Strashkodc749a02017-07-21 11:49:00 -05002262 if (!gpiochip_irqchip_irq_valid(chip, offset))
2263 return -ENXIO;
Thierry Redinge0d89722017-11-07 19:15:54 +01002264
Linus Walleijfdd61a02019-08-08 14:32:37 +02002265#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
2266 if (irq_domain_is_hierarchy(domain)) {
2267 struct irq_fwspec spec;
2268
2269 spec.fwnode = domain->fwnode;
2270 spec.param_count = 2;
2271 spec.param[0] = chip->irq.child_offset_to_irq(chip, offset);
2272 spec.param[1] = IRQ_TYPE_NONE;
2273
2274 return irq_create_fwspec_mapping(&spec);
2275 }
2276#endif
2277
2278 return irq_create_mapping(domain, offset);
Linus Walleij14250522014-03-25 10:40:18 +01002279}
2280
Hans Verkuil4e6b8232018-09-08 11:23:14 +02002281static int gpiochip_irq_reqres(struct irq_data *d)
2282{
2283 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
2284
2285 return gpiochip_reqres_irq(chip, d->hwirq);
2286}
2287
2288static void gpiochip_irq_relres(struct irq_data *d)
2289{
2290 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
2291
2292 gpiochip_relres_irq(chip, d->hwirq);
2293}
2294
Hans Verkuil461c1a72018-09-08 11:23:17 +02002295static void gpiochip_irq_enable(struct irq_data *d)
2296{
2297 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
2298
2299 gpiochip_enable_irq(chip, d->hwirq);
2300 if (chip->irq.irq_enable)
2301 chip->irq.irq_enable(d);
2302 else
2303 chip->irq.chip->irq_unmask(d);
2304}
2305
2306static void gpiochip_irq_disable(struct irq_data *d)
2307{
2308 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
2309
2310 if (chip->irq.irq_disable)
2311 chip->irq.irq_disable(d);
2312 else
2313 chip->irq.chip->irq_mask(d);
2314 gpiochip_disable_irq(chip, d->hwirq);
2315}
2316
Hans Verkuilca620f22018-09-08 11:23:15 +02002317static void gpiochip_set_irq_hooks(struct gpio_chip *gpiochip)
2318{
2319 struct irq_chip *irqchip = gpiochip->irq.chip;
2320
2321 if (!irqchip->irq_request_resources &&
2322 !irqchip->irq_release_resources) {
2323 irqchip->irq_request_resources = gpiochip_irq_reqres;
2324 irqchip->irq_release_resources = gpiochip_irq_relres;
2325 }
Hans Verkuil461c1a72018-09-08 11:23:17 +02002326 if (WARN_ON(gpiochip->irq.irq_enable))
2327 return;
Hans Verkuil171948e2018-09-14 10:36:39 +02002328 /* Check if the irqchip already has this hook... */
2329 if (irqchip->irq_enable == gpiochip_irq_enable) {
2330 /*
2331 * ...and if so, give a gentle warning that this is bad
2332 * practice.
2333 */
2334 chip_info(gpiochip,
2335 "detected irqchip that is shared with multiple gpiochips: please fix the driver.\n");
2336 return;
2337 }
Hans Verkuil461c1a72018-09-08 11:23:17 +02002338 gpiochip->irq.irq_enable = irqchip->irq_enable;
2339 gpiochip->irq.irq_disable = irqchip->irq_disable;
2340 irqchip->irq_enable = gpiochip_irq_enable;
2341 irqchip->irq_disable = gpiochip_irq_disable;
Hans Verkuilca620f22018-09-08 11:23:15 +02002342}
2343
Linus Walleij14250522014-03-25 10:40:18 +01002344/**
Thierry Redinge0d89722017-11-07 19:15:54 +01002345 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
2346 * @gpiochip: the GPIO chip to add the IRQ chip to
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002347 * @lock_key: lockdep class for IRQ lock
2348 * @request_key: lockdep class for IRQ request
Thierry Redinge0d89722017-11-07 19:15:54 +01002349 */
Thierry Reding959bc7b2017-11-07 19:15:59 +01002350static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002351 struct lock_class_key *lock_key,
2352 struct lock_class_key *request_key)
Thierry Redinge0d89722017-11-07 19:15:54 +01002353{
2354 struct irq_chip *irqchip = gpiochip->irq.chip;
Linus Walleijfdd61a02019-08-08 14:32:37 +02002355 const struct irq_domain_ops *ops = NULL;
Thierry Redinge0d89722017-11-07 19:15:54 +01002356 struct device_node *np;
2357 unsigned int type;
2358 unsigned int i;
2359
2360 if (!irqchip)
2361 return 0;
2362
2363 if (gpiochip->irq.parent_handler && gpiochip->can_sleep) {
Andy Shevchenkob1911712018-07-03 03:39:03 +03002364 chip_err(gpiochip, "you cannot have chained interrupts on a chip that may sleep\n");
Thierry Redinge0d89722017-11-07 19:15:54 +01002365 return -EINVAL;
2366 }
2367
2368 np = gpiochip->gpiodev->dev.of_node;
2369 type = gpiochip->irq.default_type;
2370
2371 /*
2372 * Specifying a default trigger is a terrible idea if DT or ACPI is
2373 * used to configure the interrupts, as you may end up with
2374 * conflicting triggers. Tell the user, and reset to NONE.
2375 */
2376 if (WARN(np && type != IRQ_TYPE_NONE,
2377 "%s: Ignoring %u default trigger\n", np->full_name, type))
2378 type = IRQ_TYPE_NONE;
2379
2380 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
2381 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
2382 "Ignoring %u default trigger\n", type);
2383 type = IRQ_TYPE_NONE;
2384 }
2385
2386 gpiochip->to_irq = gpiochip_to_irq;
2387 gpiochip->irq.default_type = type;
Thierry Reding959bc7b2017-11-07 19:15:59 +01002388 gpiochip->irq.lock_key = lock_key;
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002389 gpiochip->irq.request_key = request_key;
Thierry Redinge0d89722017-11-07 19:15:54 +01002390
Linus Walleijfdd61a02019-08-08 14:32:37 +02002391 /* If a parent irqdomain is provided, let's build a hierarchy */
2392 if (gpiochip_hierarchy_is_hierarchical(gpiochip)) {
2393 int ret = gpiochip_hierarchy_add_domain(gpiochip);
2394 if (ret)
2395 return ret;
2396 } else {
2397 /* Some drivers provide custom irqdomain ops */
2398 if (gpiochip->irq.domain_ops)
2399 ops = gpiochip->irq.domain_ops;
Thierry Redinge0d89722017-11-07 19:15:54 +01002400
Linus Walleijfdd61a02019-08-08 14:32:37 +02002401 if (!ops)
2402 ops = &gpiochip_domain_ops;
2403 gpiochip->irq.domain = irq_domain_add_simple(np,
2404 gpiochip->ngpio,
2405 gpiochip->irq.first,
2406 ops, gpiochip);
2407 if (!gpiochip->irq.domain)
2408 return -EINVAL;
2409 }
Thierry Redinge0d89722017-11-07 19:15:54 +01002410
Thierry Redinge0d89722017-11-07 19:15:54 +01002411 if (gpiochip->irq.parent_handler) {
2412 void *data = gpiochip->irq.parent_handler_data ?: gpiochip;
2413
2414 for (i = 0; i < gpiochip->irq.num_parents; i++) {
2415 /*
2416 * The parent IRQ chip is already using the chip_data
2417 * for this IRQ chip, so our callbacks simply use the
2418 * handler_data.
2419 */
2420 irq_set_chained_handler_and_data(gpiochip->irq.parents[i],
2421 gpiochip->irq.parent_handler,
2422 data);
2423 }
Thierry Redinge0d89722017-11-07 19:15:54 +01002424 }
2425
Hans Verkuilca620f22018-09-08 11:23:15 +02002426 gpiochip_set_irq_hooks(gpiochip);
2427
Thierry Redinge0d89722017-11-07 19:15:54 +01002428 acpi_gpiochip_request_interrupts(gpiochip);
2429
2430 return 0;
2431}
2432
2433/**
Linus Walleij14250522014-03-25 10:40:18 +01002434 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
2435 * @gpiochip: the gpiochip to remove the irqchip from
2436 *
2437 * This is called only from gpiochip_remove()
2438 */
2439static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
2440{
Hans Verkuilca620f22018-09-08 11:23:15 +02002441 struct irq_chip *irqchip = gpiochip->irq.chip;
Thierry Reding39e5f092017-11-07 19:15:50 +01002442 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +01002443
Mika Westerbergafa82fa2014-07-25 09:54:48 +03002444 acpi_gpiochip_free_interrupts(gpiochip);
2445
Hans Verkuilca620f22018-09-08 11:23:15 +02002446 if (irqchip && gpiochip->irq.parent_handler) {
Thierry Reding39e5f092017-11-07 19:15:50 +01002447 struct gpio_irq_chip *irq = &gpiochip->irq;
2448 unsigned int i;
2449
2450 for (i = 0; i < irq->num_parents; i++)
2451 irq_set_chained_handler_and_data(irq->parents[i],
2452 NULL, NULL);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03002453 }
2454
Linus Walleijc3626fd2014-03-28 20:42:01 +01002455 /* Remove all IRQ mappings and delete the domain */
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01002456 if (gpiochip->irq.domain) {
Thierry Reding39e5f092017-11-07 19:15:50 +01002457 unsigned int irq;
2458
Mika Westerberg79b804c2016-09-20 15:15:21 +03002459 for (offset = 0; offset < gpiochip->ngpio; offset++) {
2460 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
2461 continue;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01002462
2463 irq = irq_find_mapping(gpiochip->irq.domain, offset);
2464 irq_dispose_mapping(irq);
Mika Westerberg79b804c2016-09-20 15:15:21 +03002465 }
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01002466
2467 irq_domain_remove(gpiochip->irq.domain);
Linus Walleijc3626fd2014-03-28 20:42:01 +01002468 }
Linus Walleij14250522014-03-25 10:40:18 +01002469
Hans Verkuil461c1a72018-09-08 11:23:17 +02002470 if (irqchip) {
2471 if (irqchip->irq_request_resources == gpiochip_irq_reqres) {
2472 irqchip->irq_request_resources = NULL;
2473 irqchip->irq_release_resources = NULL;
2474 }
2475 if (irqchip->irq_enable == gpiochip_irq_enable) {
2476 irqchip->irq_enable = gpiochip->irq.irq_enable;
2477 irqchip->irq_disable = gpiochip->irq.irq_disable;
2478 }
Linus Walleij14250522014-03-25 10:40:18 +01002479 }
Hans Verkuil461c1a72018-09-08 11:23:17 +02002480 gpiochip->irq.irq_enable = NULL;
2481 gpiochip->irq.irq_disable = NULL;
Hans Verkuilca620f22018-09-08 11:23:15 +02002482 gpiochip->irq.chip = NULL;
Mika Westerberg79b804c2016-09-20 15:15:21 +03002483
2484 gpiochip_irqchip_free_valid_mask(gpiochip);
Linus Walleij14250522014-03-25 10:40:18 +01002485}
2486
2487/**
Linus Walleij739e6f52017-01-11 13:37:07 +01002488 * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01002489 * @gpiochip: the gpiochip to add the irqchip to
2490 * @irqchip: the irqchip to add to the gpiochip
2491 * @first_irq: if not dynamically assigned, the base (first) IRQ to
2492 * allocate gpiochip irqs from
2493 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +02002494 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
2495 * to have the core avoid setting up any default type in the hardware.
Thierry Reding60ed54c2017-11-07 19:15:57 +01002496 * @threaded: whether this irqchip uses a nested thread handler
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002497 * @lock_key: lockdep class for IRQ lock
2498 * @request_key: lockdep class for IRQ request
Linus Walleij14250522014-03-25 10:40:18 +01002499 *
2500 * This function closely associates a certain irqchip with a certain
2501 * gpiochip, providing an irq domain to translate the local IRQs to
2502 * global irqs in the gpiolib core, and making sure that the gpiochip
2503 * is passed as chip data to all related functions. Driver callbacks
Linus Walleij09dd5f92015-12-07 15:31:58 +01002504 * need to use gpiochip_get_data() to get their local state containers back
Linus Walleij14250522014-03-25 10:40:18 +01002505 * from the gpiochip passed as chip data. An irqdomain will be stored
2506 * in the gpiochip that shall be used by the driver to handle IRQ number
2507 * translation. The gpiochip will need to be initialized and registered
2508 * before calling this function.
2509 *
Linus Walleijc3626fd2014-03-28 20:42:01 +01002510 * This function will handle two cell:ed simple IRQs and assumes all
2511 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +01002512 * need to be open coded.
2513 */
Linus Walleij739e6f52017-01-11 13:37:07 +01002514int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
2515 struct irq_chip *irqchip,
2516 unsigned int first_irq,
2517 irq_flow_handler_t handler,
2518 unsigned int type,
Thierry Reding60ed54c2017-11-07 19:15:57 +01002519 bool threaded,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002520 struct lock_class_key *lock_key,
2521 struct lock_class_key *request_key)
Linus Walleij14250522014-03-25 10:40:18 +01002522{
2523 struct device_node *of_node;
Linus Walleij14250522014-03-25 10:40:18 +01002524
2525 if (!gpiochip || !irqchip)
2526 return -EINVAL;
2527
Linus Walleij58383c782015-11-04 09:56:26 +01002528 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +01002529 pr_err("missing gpiochip .dev parent pointer\n");
2530 return -EINVAL;
2531 }
Thierry Reding60ed54c2017-11-07 19:15:57 +01002532 gpiochip->irq.threaded = threaded;
Linus Walleij58383c782015-11-04 09:56:26 +01002533 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +01002534#ifdef CONFIG_OF_GPIO
2535 /*
Colin Cronin20a8a962015-05-18 11:41:43 -07002536 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08002537 * FIXME: get rid of this and use gpiochip->parent->of_node
2538 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +01002539 */
2540 if (gpiochip->of_node)
2541 of_node = gpiochip->of_node;
2542#endif
Marc Zyngier332e99d2016-09-07 09:12:11 +01002543 /*
Mika Westerberg0a1e0052016-09-12 14:29:51 +03002544 * Specifying a default trigger is a terrible idea if DT or ACPI is
Marc Zyngier332e99d2016-09-07 09:12:11 +01002545 * used to configure the interrupts, as you may end-up with
2546 * conflicting triggers. Tell the user, and reset to NONE.
2547 */
2548 if (WARN(of_node && type != IRQ_TYPE_NONE,
Rob Herring7eb6ce22017-07-18 16:43:03 -05002549 "%pOF: Ignoring %d default trigger\n", of_node, type))
Marc Zyngier332e99d2016-09-07 09:12:11 +01002550 type = IRQ_TYPE_NONE;
Mika Westerberg0a1e0052016-09-12 14:29:51 +03002551 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
2552 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
2553 "Ignoring %d default trigger\n", type);
2554 type = IRQ_TYPE_NONE;
2555 }
Marc Zyngier332e99d2016-09-07 09:12:11 +01002556
Thierry Redingda80ff82017-11-07 19:15:46 +01002557 gpiochip->irq.chip = irqchip;
Thierry Redingc7a0aa52017-11-07 19:15:48 +01002558 gpiochip->irq.handler = handler;
Thierry Reding3634eeb2017-11-07 19:15:49 +01002559 gpiochip->irq.default_type = type;
Linus Walleij14250522014-03-25 10:40:18 +01002560 gpiochip->to_irq = gpiochip_to_irq;
Thierry Redingca9df052017-11-07 19:15:53 +01002561 gpiochip->irq.lock_key = lock_key;
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002562 gpiochip->irq.request_key = request_key;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01002563 gpiochip->irq.domain = irq_domain_add_simple(of_node,
Linus Walleij14250522014-03-25 10:40:18 +01002564 gpiochip->ngpio, first_irq,
2565 &gpiochip_domain_ops, gpiochip);
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01002566 if (!gpiochip->irq.domain) {
Thierry Redingda80ff82017-11-07 19:15:46 +01002567 gpiochip->irq.chip = NULL;
Linus Walleij14250522014-03-25 10:40:18 +01002568 return -EINVAL;
2569 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +02002570
Hans Verkuilca620f22018-09-08 11:23:15 +02002571 gpiochip_set_irq_hooks(gpiochip);
Linus Walleij14250522014-03-25 10:40:18 +01002572
Mika Westerbergafa82fa2014-07-25 09:54:48 +03002573 acpi_gpiochip_request_interrupts(gpiochip);
2574
Linus Walleij14250522014-03-25 10:40:18 +01002575 return 0;
2576}
Linus Walleij739e6f52017-01-11 13:37:07 +01002577EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key);
Linus Walleij14250522014-03-25 10:40:18 +01002578
2579#else /* CONFIG_GPIOLIB_IRQCHIP */
2580
Thierry Reding959bc7b2017-11-07 19:15:59 +01002581static inline int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002582 struct lock_class_key *lock_key,
2583 struct lock_class_key *request_key)
Thierry Redinge0d89722017-11-07 19:15:54 +01002584{
2585 return 0;
2586}
Linus Walleij14250522014-03-25 10:40:18 +01002587static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
Andy Shevchenko9411e3a2019-10-09 17:34:44 +03002588
2589static inline int gpiochip_irqchip_init_hw(struct gpio_chip *gpiochip)
2590{
2591 return 0;
2592}
2593
Mika Westerberg79b804c2016-09-20 15:15:21 +03002594static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
2595{
2596 return 0;
2597}
2598static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
2599{ }
Linus Walleij14250522014-03-25 10:40:18 +01002600
2601#endif /* CONFIG_GPIOLIB_IRQCHIP */
2602
Jonas Gorskic771c2f2015-10-11 17:34:15 +02002603/**
2604 * gpiochip_generic_request() - request the gpio function for a pin
2605 * @chip: the gpiochip owning the GPIO
2606 * @offset: the offset of the GPIO to request for GPIO function
2607 */
2608int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
2609{
Linus Walleija9a1d2a2017-09-22 11:02:10 +02002610 return pinctrl_gpio_request(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02002611}
2612EXPORT_SYMBOL_GPL(gpiochip_generic_request);
2613
2614/**
2615 * gpiochip_generic_free() - free the gpio function from a pin
2616 * @chip: the gpiochip to request the gpio function for
2617 * @offset: the offset of the GPIO to free from GPIO function
2618 */
2619void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
2620{
Linus Walleija9a1d2a2017-09-22 11:02:10 +02002621 pinctrl_gpio_free(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02002622}
2623EXPORT_SYMBOL_GPL(gpiochip_generic_free);
2624
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002625/**
2626 * gpiochip_generic_config() - apply configuration for a pin
2627 * @chip: the gpiochip owning the GPIO
2628 * @offset: the offset of the GPIO to apply the configuration
2629 * @config: the configuration to be applied
2630 */
2631int gpiochip_generic_config(struct gpio_chip *chip, unsigned offset,
2632 unsigned long config)
2633{
2634 return pinctrl_gpio_set_config(chip->gpiodev->base + offset, config);
2635}
2636EXPORT_SYMBOL_GPL(gpiochip_generic_config);
2637
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302638#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01002639
Linus Walleij3f0f8672012-11-20 12:40:15 +01002640/**
Christian Ruppert586a87e2013-10-15 15:37:54 +02002641 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
2642 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +02002643 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +02002644 * @gpio_offset: the start offset in the current gpio_chip number space
2645 * @pin_group: name of the pin group inside the pin controller
Christian Lamparter973c1712018-05-21 22:57:39 +02002646 *
2647 * Calling this function directly from a DeviceTree-supported
2648 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
2649 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
2650 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
Christian Ruppert586a87e2013-10-15 15:37:54 +02002651 */
2652int gpiochip_add_pingroup_range(struct gpio_chip *chip,
2653 struct pinctrl_dev *pctldev,
2654 unsigned int gpio_offset, const char *pin_group)
2655{
2656 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002657 struct gpio_device *gdev = chip->gpiodev;
Christian Ruppert586a87e2013-10-15 15:37:54 +02002658 int ret;
2659
2660 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
2661 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002662 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +02002663 return -ENOMEM;
2664 }
2665
2666 /* Use local offset as range ID */
2667 pin_range->range.id = gpio_offset;
2668 pin_range->range.gc = chip;
2669 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002670 pin_range->range.base = gdev->base + gpio_offset;
Christian Ruppert586a87e2013-10-15 15:37:54 +02002671 pin_range->pctldev = pctldev;
2672
2673 ret = pinctrl_get_group_pins(pctldev, pin_group,
2674 &pin_range->range.pins,
2675 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +01002676 if (ret < 0) {
2677 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +02002678 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +01002679 }
Christian Ruppert586a87e2013-10-15 15:37:54 +02002680
2681 pinctrl_add_gpio_range(pctldev, &pin_range->range);
2682
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002683 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
2684 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +02002685 pinctrl_dev_get_devname(pctldev), pin_group);
2686
Linus Walleij20ec3e32016-02-11 11:03:06 +01002687 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Christian Ruppert586a87e2013-10-15 15:37:54 +02002688
2689 return 0;
2690}
2691EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
2692
2693/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01002694 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
2695 * @chip: the gpiochip to add the range for
Thierry Reding950d55f52017-07-24 16:57:22 +02002696 * @pinctl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01002697 * @gpio_offset: the start offset in the current gpio_chip number space
2698 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01002699 * @npins: the number of pins from the offset of each pin space (GPIO and
2700 * pin controller) to accumulate in this range
Thierry Reding950d55f52017-07-24 16:57:22 +02002701 *
2702 * Returns:
2703 * 0 on success, or a negative error-code on failure.
Christian Lamparter973c1712018-05-21 22:57:39 +02002704 *
2705 * Calling this function directly from a DeviceTree-supported
2706 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
2707 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
2708 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
Linus Walleij3f0f8672012-11-20 12:40:15 +01002709 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002710int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01002711 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01002712 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302713{
2714 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002715 struct gpio_device *gdev = chip->gpiodev;
Axel Linb4d4b1f2012-11-21 14:33:56 +08002716 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302717
Linus Walleij3f0f8672012-11-20 12:40:15 +01002718 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302719 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002720 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002721 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302722 }
2723
Linus Walleij3f0f8672012-11-20 12:40:15 +01002724 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01002725 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01002726 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302727 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002728 pin_range->range.base = gdev->base + gpio_offset;
Linus Walleij316511c2012-11-21 08:48:09 +01002729 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302730 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01002731 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302732 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01002733 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08002734 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002735 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +01002736 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08002737 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01002738 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002739 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
2740 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +01002741 pinctl_name,
2742 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302743
Linus Walleij20ec3e32016-02-11 11:03:06 +01002744 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002745
2746 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302747}
Linus Walleij165adc92012-11-06 14:49:39 +01002748EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302749
Linus Walleij3f0f8672012-11-20 12:40:15 +01002750/**
2751 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
2752 * @chip: the chip to remove all the mappings for
2753 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302754void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
2755{
2756 struct gpio_pin_range *pin_range, *tmp;
Linus Walleij20ec3e32016-02-11 11:03:06 +01002757 struct gpio_device *gdev = chip->gpiodev;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302758
Linus Walleij20ec3e32016-02-11 11:03:06 +01002759 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302760 list_del(&pin_range->node);
2761 pinctrl_remove_gpio_range(pin_range->pctldev,
2762 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01002763 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302764 }
2765}
Linus Walleij165adc92012-11-06 14:49:39 +01002766EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
2767
2768#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302769
David Brownelld2876d02008-02-04 22:28:20 -08002770/* These "optional" allocation calls help prevent drivers from stomping
2771 * on each other, and help provide better diagnostics in debugfs.
2772 * They're called even less than the "set direction" calls.
2773 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002774static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08002775{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002776 struct gpio_chip *chip = desc->gdev->chip;
Linus Walleijd377f562019-07-16 11:11:45 +02002777 int ret;
David Brownelld2876d02008-02-04 22:28:20 -08002778 unsigned long flags;
Biju Das3789f5a2018-08-07 08:15:18 +01002779 unsigned offset;
David Brownelld2876d02008-02-04 22:28:20 -08002780
Muchun Song18534df2018-11-01 21:12:50 +08002781 if (label) {
2782 label = kstrdup_const(label, GFP_KERNEL);
2783 if (!label)
2784 return -ENOMEM;
2785 }
2786
David Brownelld2876d02008-02-04 22:28:20 -08002787 spin_lock_irqsave(&gpio_lock, flags);
2788
David Brownelld2876d02008-02-04 22:28:20 -08002789 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07002790 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08002791 */
2792
2793 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
2794 desc_set_label(desc, label ? : "?");
Linus Walleijd377f562019-07-16 11:11:45 +02002795 ret = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07002796 } else {
Muchun Song18534df2018-11-01 21:12:50 +08002797 kfree_const(label);
Linus Walleijd377f562019-07-16 11:11:45 +02002798 ret = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -08002799 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07002800 }
2801
2802 if (chip->request) {
2803 /* chip->request may sleep */
2804 spin_unlock_irqrestore(&gpio_lock, flags);
Biju Das3789f5a2018-08-07 08:15:18 +01002805 offset = gpio_chip_hwgpio(desc);
2806 if (gpiochip_line_is_valid(chip, offset))
Linus Walleijd377f562019-07-16 11:11:45 +02002807 ret = chip->request(chip, offset);
Biju Das3789f5a2018-08-07 08:15:18 +01002808 else
Linus Walleijd377f562019-07-16 11:11:45 +02002809 ret = -EINVAL;
David Brownell35e8bb52008-10-15 22:03:16 -07002810 spin_lock_irqsave(&gpio_lock, flags);
2811
Linus Walleijd377f562019-07-16 11:11:45 +02002812 if (ret < 0) {
David Brownell35e8bb52008-10-15 22:03:16 -07002813 desc_set_label(desc, NULL);
Muchun Song18534df2018-11-01 21:12:50 +08002814 kfree_const(label);
David Brownell35e8bb52008-10-15 22:03:16 -07002815 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03002816 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07002817 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07002818 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03002819 if (chip->get_direction) {
2820 /* chip->get_direction may sleep */
2821 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002822 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03002823 spin_lock_irqsave(&gpio_lock, flags);
2824 }
David Brownelld2876d02008-02-04 22:28:20 -08002825done:
Mika Westerberg77c2d792014-03-10 14:54:50 +02002826 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijd377f562019-07-16 11:11:45 +02002827 return ret;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002828}
2829
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002830/*
2831 * This descriptor validation needs to be inserted verbatim into each
2832 * function taking a descriptor, so we need to use a preprocessor
Linus Walleij54d77192016-05-30 16:48:39 +02002833 * macro to avoid endless duplication. If the desc is NULL it is an
2834 * optional GPIO and calls should just bail out.
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002835 */
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002836static int validate_desc(const struct gpio_desc *desc, const char *func)
2837{
2838 if (!desc)
2839 return 0;
2840 if (IS_ERR(desc)) {
2841 pr_warn("%s: invalid GPIO (errorpointer)\n", func);
2842 return PTR_ERR(desc);
2843 }
2844 if (!desc->gdev) {
2845 pr_warn("%s: invalid GPIO (no device)\n", func);
2846 return -EINVAL;
2847 }
2848 if (!desc->gdev->chip) {
2849 dev_warn(&desc->gdev->dev,
2850 "%s: backing chip is gone\n", func);
2851 return 0;
2852 }
2853 return 1;
2854}
2855
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002856#define VALIDATE_DESC(desc) do { \
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002857 int __valid = validate_desc(desc, __func__); \
2858 if (__valid <= 0) \
2859 return __valid; \
2860 } while (0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002861
2862#define VALIDATE_DESC_VOID(desc) do { \
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002863 int __valid = validate_desc(desc, __func__); \
2864 if (__valid <= 0) \
Linus Walleij54d77192016-05-30 16:48:39 +02002865 return; \
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002866 } while (0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002867
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002868int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002869{
Linus Walleijd377f562019-07-16 11:11:45 +02002870 int ret = -EPROBE_DEFER;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002871 struct gpio_device *gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002872
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002873 VALIDATE_DESC(desc);
2874 gdev = desc->gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002875
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002876 if (try_module_get(gdev->owner)) {
Linus Walleijd377f562019-07-16 11:11:45 +02002877 ret = gpiod_request_commit(desc, label);
2878 if (ret < 0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002879 module_put(gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002880 else
2881 get_device(&gdev->dev);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002882 }
2883
Linus Walleijd377f562019-07-16 11:11:45 +02002884 if (ret)
2885 gpiod_dbg(desc, "%s: status %d\n", __func__, ret);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002886
Linus Walleijd377f562019-07-16 11:11:45 +02002887 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08002888}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002889
Linus Walleijfac9d882017-09-26 20:58:28 +02002890static bool gpiod_free_commit(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002891{
Mika Westerberg77c2d792014-03-10 14:54:50 +02002892 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08002893 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07002894 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002895
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07002896 might_sleep();
2897
Alexandre Courbot372e7222013-02-03 01:29:29 +09002898 gpiod_unexport(desc);
David Brownelld8f388d82008-07-25 01:46:07 -07002899
David Brownelld2876d02008-02-04 22:28:20 -08002900 spin_lock_irqsave(&gpio_lock, flags);
2901
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002902 chip = desc->gdev->chip;
David Brownell35e8bb52008-10-15 22:03:16 -07002903 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
2904 if (chip->free) {
2905 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07002906 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002907 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07002908 spin_lock_irqsave(&gpio_lock, flags);
2909 }
Muchun Song18534df2018-11-01 21:12:50 +08002910 kfree_const(desc->label);
David Brownelld2876d02008-02-04 22:28:20 -08002911 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08002912 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07002913 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302914 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302915 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Drew Fustini9225d512019-11-05 10:04:23 +08002916 clear_bit(FLAG_PULL_UP, &desc->flags);
2917 clear_bit(FLAG_PULL_DOWN, &desc->flags);
Kent Gibson2148ad72019-11-05 10:04:25 +08002918 clear_bit(FLAG_BIAS_DISABLE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06002919 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002920 ret = true;
2921 }
David Brownelld2876d02008-02-04 22:28:20 -08002922
2923 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002924 return ret;
2925}
2926
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002927void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002928{
Linus Walleijfac9d882017-09-26 20:58:28 +02002929 if (desc && desc->gdev && gpiod_free_commit(desc)) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002930 module_put(desc->gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002931 put_device(&desc->gdev->dev);
2932 } else {
Mika Westerberg77c2d792014-03-10 14:54:50 +02002933 WARN_ON(extra_checks);
Linus Walleij33a68e82016-02-11 10:28:44 +01002934 }
David Brownelld2876d02008-02-04 22:28:20 -08002935}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002936
David Brownelld2876d02008-02-04 22:28:20 -08002937/**
2938 * gpiochip_is_requested - return string iff signal was requested
2939 * @chip: controller managing the signal
2940 * @offset: of signal within controller's 0..(ngpio - 1) range
2941 *
2942 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09002943 * The string returned is the label passed to gpio_request(); if none has been
2944 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08002945 *
2946 * This function is for use by GPIO controller drivers. The label can
2947 * help with diagnostics, and knowing that the signal is used as a GPIO
2948 * can help avoid accidentally multiplexing it to another controller.
2949 */
2950const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
2951{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002952 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08002953
Dirk Behme48b59532015-08-18 18:02:32 +02002954 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08002955 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002956
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002957 desc = &chip->gpiodev->descs[offset];
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002958
Alexandre Courbot372e7222013-02-03 01:29:29 +09002959 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08002960 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002961 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08002962}
2963EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2964
Mika Westerberg77c2d792014-03-10 14:54:50 +02002965/**
2966 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
Thierry Reding950d55f52017-07-24 16:57:22 +02002967 * @chip: GPIO chip
2968 * @hwnum: hardware number of the GPIO for which to request the descriptor
Mika Westerberg77c2d792014-03-10 14:54:50 +02002969 * @label: label for the GPIO
Linus Walleij5923ea62019-04-26 14:40:18 +02002970 * @lflags: lookup flags for this GPIO or 0 if default, this can be used to
2971 * specify things like line inversion semantics with the machine flags
2972 * such as GPIO_OUT_LOW
2973 * @dflags: descriptor request flags for this GPIO or 0 if default, this
2974 * can be used to specify consumer semantics such as open drain
Mika Westerberg77c2d792014-03-10 14:54:50 +02002975 *
2976 * Function allows GPIO chip drivers to request and use their own GPIO
2977 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2978 * function will not increase reference count of the GPIO chip module. This
2979 * allows the GPIO chip module to be unloaded as needed (we assume that the
2980 * GPIO chip driver handles freeing the GPIOs it has requested).
Thierry Reding950d55f52017-07-24 16:57:22 +02002981 *
2982 * Returns:
2983 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2984 * code on failure.
Mika Westerberg77c2d792014-03-10 14:54:50 +02002985 */
Bartosz Golaszewski06863622019-12-24 13:06:59 +01002986struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip,
2987 unsigned int hwnum,
Linus Walleij21abf102018-09-04 13:31:45 +02002988 const char *label,
Linus Walleij5923ea62019-04-26 14:40:18 +02002989 enum gpio_lookup_flags lflags,
2990 enum gpiod_flags dflags)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002991{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002992 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
Linus Walleijd377f562019-07-16 11:11:45 +02002993 int ret;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002994
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002995 if (IS_ERR(desc)) {
2996 chip_err(chip, "failed to get GPIO descriptor\n");
2997 return desc;
2998 }
2999
Linus Walleijd377f562019-07-16 11:11:45 +02003000 ret = gpiod_request_commit(desc, label);
3001 if (ret < 0)
3002 return ERR_PTR(ret);
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07003003
Linus Walleijd377f562019-07-16 11:11:45 +02003004 ret = gpiod_configure_flags(desc, label, lflags, dflags);
3005 if (ret) {
Linus Walleij21abf102018-09-04 13:31:45 +02003006 chip_err(chip, "setup of own GPIO %s failed\n", label);
3007 gpiod_free_commit(desc);
Linus Walleijd377f562019-07-16 11:11:45 +02003008 return ERR_PTR(ret);
Linus Walleij21abf102018-09-04 13:31:45 +02003009 }
3010
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07003011 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02003012}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07003013EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02003014
3015/**
3016 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
3017 * @desc: GPIO descriptor to free
3018 *
3019 * Function frees the given GPIO requested previously with
3020 * gpiochip_request_own_desc().
3021 */
3022void gpiochip_free_own_desc(struct gpio_desc *desc)
3023{
3024 if (desc)
Linus Walleijfac9d882017-09-26 20:58:28 +02003025 gpiod_free_commit(desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02003026}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07003027EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08003028
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003029/*
3030 * Drivers MUST set GPIO direction before making get/set calls. In
David Brownelld2876d02008-02-04 22:28:20 -08003031 * some cases this is done in early boot, before IRQs are enabled.
3032 *
3033 * As a rule these aren't called more than once (except for drivers
3034 * using the open-drain emulation idiom) so these are natural places
3035 * to accumulate extra debugging checks. Note that we can't (yet)
3036 * rely on gpio_request() having been called beforehand.
3037 */
3038
YueHaibingd18fddf2020-01-16 22:29:27 +08003039static int gpio_set_config(struct gpio_chip *gc, unsigned int offset,
Thomas Petazzoni71479782019-02-07 17:28:56 +01003040 enum pin_config_param mode)
3041{
Bartosz Golaszewskid90f3682019-12-24 13:06:58 +01003042 if (!gc->set_config)
3043 return -ENOTSUPP;
Thomas Petazzoni71479782019-02-07 17:28:56 +01003044
Bartosz Golaszewskid90f3682019-12-24 13:06:58 +01003045 return gc->set_config(gc, offset, mode);
Thomas Petazzoni71479782019-02-07 17:28:56 +01003046}
3047
Kent Gibson2148ad72019-11-05 10:04:25 +08003048static int gpio_set_bias(struct gpio_chip *chip, struct gpio_desc *desc)
3049{
3050 int bias = 0;
3051 int ret = 0;
3052
3053 if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
3054 bias = PIN_CONFIG_BIAS_DISABLE;
3055 else if (test_bit(FLAG_PULL_UP, &desc->flags))
3056 bias = PIN_CONFIG_BIAS_PULL_UP;
3057 else if (test_bit(FLAG_PULL_DOWN, &desc->flags))
3058 bias = PIN_CONFIG_BIAS_PULL_DOWN;
3059
3060 if (bias) {
3061 ret = gpio_set_config(chip, gpio_chip_hwgpio(desc), bias);
3062 if (ret != -ENOTSUPP)
3063 return ret;
3064 }
3065 return 0;
3066}
3067
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003068/**
3069 * gpiod_direction_input - set the GPIO direction to input
3070 * @desc: GPIO to set to input
3071 *
3072 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
3073 * be called safely on it.
3074 *
3075 * Return 0 in case of success, else an error code.
3076 */
3077int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08003078{
David Brownelld2876d02008-02-04 22:28:20 -08003079 struct gpio_chip *chip;
Linus Walleijd377f562019-07-16 11:11:45 +02003080 int ret = 0;
David Brownelld2876d02008-02-04 22:28:20 -08003081
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003082 VALIDATE_DESC(desc);
3083 chip = desc->gdev->chip;
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09003084
Linus Walleije48d1942018-09-25 09:54:14 +02003085 /*
3086 * It is legal to have no .get() and .direction_input() specified if
3087 * the chip is output-only, but you can't specify .direction_input()
3088 * and not support the .get() operation, that doesn't make sense.
3089 */
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003090 if (!chip->get && chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01003091 gpiod_warn(desc,
Linus Walleije48d1942018-09-25 09:54:14 +02003092 "%s: missing get() but have direction_input()\n",
3093 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02003094 return -EIO;
3095 }
3096
Linus Walleije48d1942018-09-25 09:54:14 +02003097 /*
3098 * If we have a .direction_input() callback, things are simple,
3099 * just call it. Else we are some input-only chip so try to check the
3100 * direction (if .get_direction() is supported) else we silently
3101 * assume we are in input mode after this.
3102 */
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003103 if (chip->direction_input) {
Linus Walleijd377f562019-07-16 11:11:45 +02003104 ret = chip->direction_input(chip, gpio_chip_hwgpio(desc));
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003105 } else if (chip->get_direction &&
3106 (chip->get_direction(chip, gpio_chip_hwgpio(desc)) != 1)) {
3107 gpiod_warn(desc,
Linus Walleije48d1942018-09-25 09:54:14 +02003108 "%s: missing direction_input() operation and line is output\n",
3109 __func__);
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003110 return -EIO;
3111 }
Kent Gibson2148ad72019-11-05 10:04:25 +08003112 if (ret == 0) {
David Brownelld2876d02008-02-04 22:28:20 -08003113 clear_bit(FLAG_IS_OUT, &desc->flags);
Kent Gibson2148ad72019-11-05 10:04:25 +08003114 ret = gpio_set_bias(chip, desc);
3115 }
Thomas Petazzonid4499912019-02-07 17:28:58 +01003116
Linus Walleijd377f562019-07-16 11:11:45 +02003117 trace_gpio_direction(desc_to_gpio(desc), 1, ret);
Alexandre Courbotd82da792014-07-22 16:17:43 +09003118
Linus Walleijd377f562019-07-16 11:11:45 +02003119 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08003120}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003121EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003122
Linus Walleijfac9d882017-09-26 20:58:28 +02003123static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08003124{
Linus Walleijc663e5f2016-03-22 10:51:16 +01003125 struct gpio_chip *gc = desc->gdev->chip;
Linus Walleijad177312016-11-13 23:02:44 +01003126 int val = !!value;
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003127 int ret = 0;
David Brownelld2876d02008-02-04 22:28:20 -08003128
Linus Walleije48d1942018-09-25 09:54:14 +02003129 /*
3130 * It's OK not to specify .direction_output() if the gpiochip is
3131 * output-only, but if there is then not even a .set() operation it
3132 * is pretty tricky to drive the output line.
3133 */
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003134 if (!gc->set && !gc->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01003135 gpiod_warn(desc,
Linus Walleije48d1942018-09-25 09:54:14 +02003136 "%s: missing set() and direction_output() operations\n",
3137 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02003138 return -EIO;
3139 }
3140
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003141 if (gc->direction_output) {
3142 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
3143 } else {
Linus Walleije48d1942018-09-25 09:54:14 +02003144 /* Check that we are in output mode if we can */
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003145 if (gc->get_direction &&
3146 gc->get_direction(gc, gpio_chip_hwgpio(desc))) {
3147 gpiod_warn(desc,
3148 "%s: missing direction_output() operation\n",
3149 __func__);
3150 return -EIO;
3151 }
Linus Walleije48d1942018-09-25 09:54:14 +02003152 /*
3153 * If we can't actively set the direction, we are some
3154 * output-only chip, so just drive the output as desired.
3155 */
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003156 gc->set(gc, gpio_chip_hwgpio(desc), val);
3157 }
3158
Linus Walleijc663e5f2016-03-22 10:51:16 +01003159 if (!ret)
David Brownelld2876d02008-02-04 22:28:20 -08003160 set_bit(FLAG_IS_OUT, &desc->flags);
Linus Walleijad177312016-11-13 23:02:44 +01003161 trace_gpio_value(desc_to_gpio(desc), 0, val);
Linus Walleijc663e5f2016-03-22 10:51:16 +01003162 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
3163 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08003164}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01003165
3166/**
3167 * gpiod_direction_output_raw - set the GPIO direction to output
3168 * @desc: GPIO to set to output
3169 * @value: initial output value of the GPIO
3170 *
3171 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
3172 * be called safely on it. The initial value of the output must be specified
3173 * as raw value on the physical line without regard for the ACTIVE_LOW status.
3174 *
3175 * Return 0 in case of success, else an error code.
3176 */
3177int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
3178{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003179 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003180 return gpiod_direction_output_raw_commit(desc, value);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01003181}
3182EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
3183
3184/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05303185 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01003186 * @desc: GPIO to set to output
3187 * @value: initial output value of the GPIO
3188 *
3189 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
3190 * be called safely on it. The initial value of the output must be specified
3191 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3192 * account.
3193 *
3194 * Return 0 in case of success, else an error code.
3195 */
3196int gpiod_direction_output(struct gpio_desc *desc, int value)
3197{
Vladimir Zapolskiy30322bc2017-12-21 18:37:24 +02003198 struct gpio_chip *gc;
Linus Walleij02e47982017-09-26 21:20:23 +02003199 int ret;
3200
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003201 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01003202 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3203 value = !value;
Linus Walleijad177312016-11-13 23:02:44 +01003204 else
3205 value = !!value;
Linus Walleij02e47982017-09-26 21:20:23 +02003206
Hans Verkuil4e9439d2018-09-08 11:23:16 +02003207 /* GPIOs used for enabled IRQs shall not be set as output */
3208 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags) &&
3209 test_bit(FLAG_IRQ_IS_ENABLED, &desc->flags)) {
Linus Walleij02e47982017-09-26 21:20:23 +02003210 gpiod_err(desc,
3211 "%s: tried to set a GPIO tied to an IRQ as output\n",
3212 __func__);
3213 return -EIO;
3214 }
3215
Vladimir Zapolskiy30322bc2017-12-21 18:37:24 +02003216 gc = desc->gdev->chip;
Linus Walleij02e47982017-09-26 21:20:23 +02003217 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
3218 /* First see if we can enable open drain in hardware */
Thomas Petazzoni71479782019-02-07 17:28:56 +01003219 ret = gpio_set_config(gc, gpio_chip_hwgpio(desc),
3220 PIN_CONFIG_DRIVE_OPEN_DRAIN);
Linus Walleij02e47982017-09-26 21:20:23 +02003221 if (!ret)
3222 goto set_output_value;
3223 /* Emulate open drain by not actively driving the line high */
Bartosz Golaszewskie7352442019-10-01 11:44:53 +02003224 if (value) {
3225 ret = gpiod_direction_input(desc);
3226 goto set_output_flag;
3227 }
Linus Walleij02e47982017-09-26 21:20:23 +02003228 }
3229 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
Thomas Petazzoni71479782019-02-07 17:28:56 +01003230 ret = gpio_set_config(gc, gpio_chip_hwgpio(desc),
3231 PIN_CONFIG_DRIVE_OPEN_SOURCE);
Linus Walleij02e47982017-09-26 21:20:23 +02003232 if (!ret)
3233 goto set_output_value;
3234 /* Emulate open source by not actively driving the line low */
Bartosz Golaszewskie7352442019-10-01 11:44:53 +02003235 if (!value) {
3236 ret = gpiod_direction_input(desc);
3237 goto set_output_flag;
3238 }
Linus Walleij02e47982017-09-26 21:20:23 +02003239 } else {
Thomas Petazzoni71479782019-02-07 17:28:56 +01003240 gpio_set_config(gc, gpio_chip_hwgpio(desc),
3241 PIN_CONFIG_DRIVE_PUSH_PULL);
Linus Walleij02e47982017-09-26 21:20:23 +02003242 }
3243
3244set_output_value:
Kent Gibson2821ae52019-11-05 10:04:26 +08003245 ret = gpio_set_bias(gc, desc);
3246 if (ret)
3247 return ret;
Linus Walleijfac9d882017-09-26 20:58:28 +02003248 return gpiod_direction_output_raw_commit(desc, value);
Bartosz Golaszewskie7352442019-10-01 11:44:53 +02003249
3250set_output_flag:
3251 /*
3252 * When emulating open-source or open-drain functionalities by not
3253 * actively driving the line (setting mode to input) we still need to
3254 * set the IS_OUT flag or otherwise we won't be able to set the line
3255 * value anymore.
3256 */
3257 if (ret == 0)
3258 set_bit(FLAG_IS_OUT, &desc->flags);
3259 return ret;
Philipp Zabelef70bbe2014-01-07 12:34:11 +01003260}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003261EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08003262
Felipe Balbic4b5be92010-05-26 14:42:23 -07003263/**
Thierry Reding950d55f52017-07-24 16:57:22 +02003264 * gpiod_set_debounce - sets @debounce time for a GPIO
3265 * @desc: descriptor of the GPIO for which to set debounce time
3266 * @debounce: debounce time in microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02003267 *
Thierry Reding950d55f52017-07-24 16:57:22 +02003268 * Returns:
3269 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
3270 * debounce time.
Felipe Balbic4b5be92010-05-26 14:42:23 -07003271 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003272int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07003273{
Felipe Balbic4b5be92010-05-26 14:42:23 -07003274 struct gpio_chip *chip;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03003275 unsigned long config;
Felipe Balbic4b5be92010-05-26 14:42:23 -07003276
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003277 VALIDATE_DESC(desc);
3278 chip = desc->gdev->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02003279
Mika Westerberg2956b5d2017-01-23 15:34:34 +03003280 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
YueHaibingd18fddf2020-01-16 22:29:27 +08003281 return gpio_set_config(chip, gpio_chip_hwgpio(desc), config);
Felipe Balbic4b5be92010-05-26 14:42:23 -07003282}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003283EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003284
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003285/**
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303286 * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
3287 * @desc: descriptor of the GPIO for which to configure persistence
3288 * @transitory: True to lose state on suspend or reset, false for persistence
3289 *
3290 * Returns:
3291 * 0 on success, otherwise a negative error code.
3292 */
3293int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
3294{
3295 struct gpio_chip *chip;
3296 unsigned long packed;
3297 int gpio;
3298 int rc;
3299
Vladimir Zapolskiy156dd392017-12-21 18:37:35 +02003300 VALIDATE_DESC(desc);
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303301 /*
3302 * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
3303 * persistence state.
3304 */
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +02003305 assign_bit(FLAG_TRANSITORY, &desc->flags, transitory);
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303306
3307 /* If the driver supports it, set the persistence state now */
3308 chip = desc->gdev->chip;
3309 if (!chip->set_config)
3310 return 0;
3311
3312 packed = pinconf_to_config_packed(PIN_CONFIG_PERSIST_STATE,
3313 !transitory);
3314 gpio = gpio_chip_hwgpio(desc);
YueHaibingd18fddf2020-01-16 22:29:27 +08003315 rc = gpio_set_config(chip, gpio, packed);
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303316 if (rc == -ENOTSUPP) {
3317 dev_dbg(&desc->gdev->dev, "Persistence not supported for GPIO %d\n",
3318 gpio);
3319 return 0;
3320 }
3321
3322 return rc;
3323}
3324EXPORT_SYMBOL_GPL(gpiod_set_transitory);
3325
3326/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003327 * gpiod_is_active_low - test whether a GPIO is active-low or not
3328 * @desc: the gpio descriptor to test
3329 *
3330 * Returns 1 if the GPIO is active-low, 0 otherwise.
3331 */
3332int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003333{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003334 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003335 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003336}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003337EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08003338
Michał Mirosławd3a5bcb2019-12-11 03:40:55 +01003339/**
3340 * gpiod_toggle_active_low - toggle whether a GPIO is active-low or not
3341 * @desc: the gpio descriptor to change
3342 */
3343void gpiod_toggle_active_low(struct gpio_desc *desc)
3344{
3345 VALIDATE_DESC_VOID(desc);
3346 change_bit(FLAG_ACTIVE_LOW, &desc->flags);
3347}
3348EXPORT_SYMBOL_GPL(gpiod_toggle_active_low);
3349
David Brownelld2876d02008-02-04 22:28:20 -08003350/* I/O calls are only valid after configuration completed; the relevant
3351 * "is this a valid GPIO" error checks should already have been done.
3352 *
3353 * "Get" operations are often inlinable as reading a pin value register,
3354 * and masking the relevant bit in that register.
3355 *
3356 * When "set" operations are inlinable, they involve writing that mask to
3357 * one register to set a low value, or a different register to set it high.
3358 * Otherwise locking is needed, so there may be little value to inlining.
3359 *
3360 *------------------------------------------------------------------------
3361 *
3362 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
3363 * have requested the GPIO. That can include implicit requesting by
3364 * a direction setting call. Marking a gpio as requested locks its chip
3365 * in memory, guaranteeing that these table lookups need no more locking
3366 * and that gpiochip_remove() will fail.
3367 *
3368 * REVISIT when debugging, consider adding some instrumentation to ensure
3369 * that the GPIO was actually requested.
3370 */
3371
Linus Walleijfac9d882017-09-26 20:58:28 +02003372static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08003373{
3374 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003375 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003376 int value;
David Brownelld2876d02008-02-04 22:28:20 -08003377
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003378 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003379 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003380 value = chip->get ? chip->get(chip, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01003381 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003382 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06003383 return value;
David Brownelld2876d02008-02-04 22:28:20 -08003384}
Alexandre Courbot372e7222013-02-03 01:29:29 +09003385
Lukas Wunnereec1d562017-10-12 12:40:10 +02003386static int gpio_chip_get_multiple(struct gpio_chip *chip,
3387 unsigned long *mask, unsigned long *bits)
3388{
3389 if (chip->get_multiple) {
3390 return chip->get_multiple(chip, mask, bits);
3391 } else if (chip->get) {
3392 int i, value;
3393
3394 for_each_set_bit(i, mask, chip->ngpio) {
3395 value = chip->get(chip, i);
3396 if (value < 0)
3397 return value;
3398 __assign_bit(i, bits, value);
3399 }
3400 return 0;
3401 }
3402 return -EIO;
3403}
3404
3405int gpiod_get_array_value_complex(bool raw, bool can_sleep,
3406 unsigned int array_size,
3407 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003408 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003409 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02003410{
Linus Walleijd377f562019-07-16 11:11:45 +02003411 int ret, i = 0;
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003412
3413 /*
3414 * Validate array_info against desc_array and its size.
3415 * It should immediately follow desc_array if both
3416 * have been obtained from the same gpiod_get_array() call.
3417 */
3418 if (array_info && array_info->desc == desc_array &&
3419 array_size <= array_info->size &&
3420 (void *)array_info == desc_array + array_info->size) {
3421 if (!can_sleep)
3422 WARN_ON(array_info->chip->can_sleep);
3423
Linus Walleijd377f562019-07-16 11:11:45 +02003424 ret = gpio_chip_get_multiple(array_info->chip,
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003425 array_info->get_mask,
3426 value_bitmap);
Linus Walleijd377f562019-07-16 11:11:45 +02003427 if (ret)
3428 return ret;
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003429
3430 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
3431 bitmap_xor(value_bitmap, value_bitmap,
3432 array_info->invert_mask, array_size);
3433
3434 if (bitmap_full(array_info->get_mask, array_size))
3435 return 0;
3436
3437 i = find_first_zero_bit(array_info->get_mask, array_size);
3438 } else {
3439 array_info = NULL;
3440 }
Lukas Wunnereec1d562017-10-12 12:40:10 +02003441
3442 while (i < array_size) {
3443 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Laura Abbott30277432018-05-21 10:57:07 -07003444 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
3445 unsigned long *mask, *bits;
Lukas Wunnereec1d562017-10-12 12:40:10 +02003446 int first, j, ret;
3447
Laura Abbott30277432018-05-21 10:57:07 -07003448 if (likely(chip->ngpio <= FASTPATH_NGPIO)) {
3449 mask = fastpath;
3450 } else {
3451 mask = kmalloc_array(2 * BITS_TO_LONGS(chip->ngpio),
3452 sizeof(*mask),
3453 can_sleep ? GFP_KERNEL : GFP_ATOMIC);
3454 if (!mask)
3455 return -ENOMEM;
3456 }
3457
3458 bits = mask + BITS_TO_LONGS(chip->ngpio);
3459 bitmap_zero(mask, chip->ngpio);
3460
Lukas Wunnereec1d562017-10-12 12:40:10 +02003461 if (!can_sleep)
3462 WARN_ON(chip->can_sleep);
3463
3464 /* collect all inputs belonging to the same chip */
3465 first = i;
Lukas Wunnereec1d562017-10-12 12:40:10 +02003466 do {
3467 const struct gpio_desc *desc = desc_array[i];
3468 int hwgpio = gpio_chip_hwgpio(desc);
3469
3470 __set_bit(hwgpio, mask);
3471 i++;
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003472
3473 if (array_info)
Janusz Krzysztofik35ae7f92018-09-24 01:53:35 +02003474 i = find_next_zero_bit(array_info->get_mask,
3475 array_size, i);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003476 } while ((i < array_size) &&
3477 (desc_array[i]->gdev->chip == chip));
3478
3479 ret = gpio_chip_get_multiple(chip, mask, bits);
Laura Abbott30277432018-05-21 10:57:07 -07003480 if (ret) {
3481 if (mask != fastpath)
3482 kfree(mask);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003483 return ret;
Laura Abbott30277432018-05-21 10:57:07 -07003484 }
Lukas Wunnereec1d562017-10-12 12:40:10 +02003485
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003486 for (j = first; j < i; ) {
Lukas Wunnereec1d562017-10-12 12:40:10 +02003487 const struct gpio_desc *desc = desc_array[j];
3488 int hwgpio = gpio_chip_hwgpio(desc);
3489 int value = test_bit(hwgpio, bits);
3490
3491 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3492 value = !value;
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003493 __assign_bit(j, value_bitmap, value);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003494 trace_gpio_value(desc_to_gpio(desc), 1, value);
Janusz Krzysztofik799d5eb2018-09-29 14:20:22 +02003495 j++;
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003496
3497 if (array_info)
Janusz Krzysztofik35ae7f92018-09-24 01:53:35 +02003498 j = find_next_zero_bit(array_info->get_mask, i,
3499 j);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003500 }
Laura Abbott30277432018-05-21 10:57:07 -07003501
3502 if (mask != fastpath)
3503 kfree(mask);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003504 }
3505 return 0;
3506}
3507
David Brownelld2876d02008-02-04 22:28:20 -08003508/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003509 * gpiod_get_raw_value() - return a gpio's raw value
3510 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08003511 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003512 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003513 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003514 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003515 * This function can be called from contexts where we cannot sleep, and will
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003516 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003517 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003518int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003519{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003520 VALIDATE_DESC(desc);
Geert Uytterhoeven3285170f2019-07-01 16:27:38 +02003521 /* Should be using gpiod_get_raw_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003522 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02003523 return gpiod_get_raw_value_commit(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003524}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003525EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08003526
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003527/**
3528 * gpiod_get_value() - return a gpio's value
3529 * @desc: gpio whose value will be returned
3530 *
3531 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003532 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003533 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003534 * This function can be called from contexts where we cannot sleep, and will
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003535 * complain if the GPIO chip functions potentially sleep.
3536 */
3537int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08003538{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003539 int value;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003540
3541 VALIDATE_DESC(desc);
Geert Uytterhoeven3285170f2019-07-01 16:27:38 +02003542 /* Should be using gpiod_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003543 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003544
Linus Walleijfac9d882017-09-26 20:58:28 +02003545 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003546 if (value < 0)
3547 return value;
3548
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003549 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3550 value = !value;
3551
3552 return value;
David Brownelld2876d02008-02-04 22:28:20 -08003553}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003554EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08003555
Lukas Wunnereec1d562017-10-12 12:40:10 +02003556/**
3557 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003558 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02003559 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003560 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003561 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02003562 *
3563 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3564 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3565 * else an error code.
3566 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003567 * This function can be called from contexts where we cannot sleep,
Lukas Wunnereec1d562017-10-12 12:40:10 +02003568 * and it will complain if the GPIO chip functions potentially sleep.
3569 */
3570int gpiod_get_raw_array_value(unsigned int array_size,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003571 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003572 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003573 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02003574{
3575 if (!desc_array)
3576 return -EINVAL;
3577 return gpiod_get_array_value_complex(true, false, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003578 desc_array, array_info,
3579 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003580}
3581EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
3582
3583/**
3584 * gpiod_get_array_value() - read values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003585 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02003586 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003587 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003588 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02003589 *
3590 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3591 * into account. Return 0 in case of success, else an error code.
3592 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003593 * This function can be called from contexts where we cannot sleep,
Lukas Wunnereec1d562017-10-12 12:40:10 +02003594 * and it will complain if the GPIO chip functions potentially sleep.
3595 */
3596int gpiod_get_array_value(unsigned int array_size,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003597 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003598 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003599 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02003600{
3601 if (!desc_array)
3602 return -EINVAL;
3603 return gpiod_get_array_value_complex(false, false, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003604 desc_array, array_info,
3605 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003606}
3607EXPORT_SYMBOL_GPL(gpiod_get_array_value);
3608
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303609/*
Linus Walleijfac9d882017-09-26 20:58:28 +02003610 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003611 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07003612 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303613 */
Linus Walleijfac9d882017-09-26 20:58:28 +02003614static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303615{
Linus Walleijd377f562019-07-16 11:11:45 +02003616 int ret = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003617 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003618 int offset = gpio_chip_hwgpio(desc);
3619
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303620 if (value) {
Linus Walleijd377f562019-07-16 11:11:45 +02003621 ret = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303622 } else {
Linus Walleijd377f562019-07-16 11:11:45 +02003623 ret = chip->direction_output(chip, offset, 0);
3624 if (!ret)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003625 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303626 }
Linus Walleijd377f562019-07-16 11:11:45 +02003627 trace_gpio_direction(desc_to_gpio(desc), value, ret);
3628 if (ret < 0)
Mark Brown6424de52013-09-09 10:33:49 +01003629 gpiod_err(desc,
3630 "%s: Error in set_value for open drain err %d\n",
Linus Walleijd377f562019-07-16 11:11:45 +02003631 __func__, ret);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303632}
3633
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303634/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003635 * _gpio_set_open_source_value() - Set the open source gpio's value.
3636 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07003637 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303638 */
Linus Walleijfac9d882017-09-26 20:58:28 +02003639static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303640{
Linus Walleijd377f562019-07-16 11:11:45 +02003641 int ret = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003642 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003643 int offset = gpio_chip_hwgpio(desc);
3644
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303645 if (value) {
Linus Walleijd377f562019-07-16 11:11:45 +02003646 ret = chip->direction_output(chip, offset, 1);
3647 if (!ret)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003648 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303649 } else {
Linus Walleijd377f562019-07-16 11:11:45 +02003650 ret = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303651 }
Linus Walleijd377f562019-07-16 11:11:45 +02003652 trace_gpio_direction(desc_to_gpio(desc), !value, ret);
3653 if (ret < 0)
Mark Brown6424de52013-09-09 10:33:49 +01003654 gpiod_err(desc,
3655 "%s: Error in set_value for open source err %d\n",
Linus Walleijd377f562019-07-16 11:11:45 +02003656 __func__, ret);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303657}
3658
Linus Walleijfac9d882017-09-26 20:58:28 +02003659static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08003660{
3661 struct gpio_chip *chip;
3662
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003663 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003664 trace_gpio_value(desc_to_gpio(desc), 0, value);
Linus Walleij02e47982017-09-26 21:20:23 +02003665 chip->set(chip, gpio_chip_hwgpio(desc), value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003666}
3667
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003668/*
3669 * set multiple outputs on the same chip;
3670 * use the chip's set_multiple function if available;
3671 * otherwise set the outputs sequentially;
3672 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
3673 * defines which outputs are to be changed
3674 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
3675 * defines the values the outputs specified by mask are to be set to
3676 */
3677static void gpio_chip_set_multiple(struct gpio_chip *chip,
3678 unsigned long *mask, unsigned long *bits)
3679{
3680 if (chip->set_multiple) {
3681 chip->set_multiple(chip, mask, bits);
3682 } else {
Andy Shevchenko5e4e6fb2017-01-03 19:01:17 +02003683 unsigned int i;
3684
3685 /* set outputs if the corresponding mask bit is set */
3686 for_each_set_bit(i, mask, chip->ngpio)
3687 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003688 }
3689}
3690
Laura Abbott30277432018-05-21 10:57:07 -07003691int gpiod_set_array_value_complex(bool raw, bool can_sleep,
Geert Uytterhoeven3c940662018-09-27 13:38:10 +02003692 unsigned int array_size,
3693 struct gpio_desc **desc_array,
3694 struct gpio_array *array_info,
3695 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003696{
3697 int i = 0;
3698
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003699 /*
3700 * Validate array_info against desc_array and its size.
3701 * It should immediately follow desc_array if both
3702 * have been obtained from the same gpiod_get_array() call.
3703 */
3704 if (array_info && array_info->desc == desc_array &&
3705 array_size <= array_info->size &&
3706 (void *)array_info == desc_array + array_info->size) {
3707 if (!can_sleep)
3708 WARN_ON(array_info->chip->can_sleep);
3709
3710 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
3711 bitmap_xor(value_bitmap, value_bitmap,
3712 array_info->invert_mask, array_size);
3713
3714 gpio_chip_set_multiple(array_info->chip, array_info->set_mask,
3715 value_bitmap);
3716
3717 if (bitmap_full(array_info->set_mask, array_size))
3718 return 0;
3719
3720 i = find_first_zero_bit(array_info->set_mask, array_size);
3721 } else {
3722 array_info = NULL;
3723 }
3724
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003725 while (i < array_size) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003726 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Laura Abbott30277432018-05-21 10:57:07 -07003727 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
3728 unsigned long *mask, *bits;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003729 int count = 0;
3730
Laura Abbott30277432018-05-21 10:57:07 -07003731 if (likely(chip->ngpio <= FASTPATH_NGPIO)) {
3732 mask = fastpath;
3733 } else {
3734 mask = kmalloc_array(2 * BITS_TO_LONGS(chip->ngpio),
3735 sizeof(*mask),
3736 can_sleep ? GFP_KERNEL : GFP_ATOMIC);
3737 if (!mask)
3738 return -ENOMEM;
3739 }
3740
3741 bits = mask + BITS_TO_LONGS(chip->ngpio);
3742 bitmap_zero(mask, chip->ngpio);
3743
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003744 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003745 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003746
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003747 do {
3748 struct gpio_desc *desc = desc_array[i];
3749 int hwgpio = gpio_chip_hwgpio(desc);
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003750 int value = test_bit(i, value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003751
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003752 /*
3753 * Pins applicable for fast input but not for
3754 * fast output processing may have been already
3755 * inverted inside the fast path, skip them.
3756 */
3757 if (!raw && !(array_info &&
3758 test_bit(i, array_info->invert_mask)) &&
3759 test_bit(FLAG_ACTIVE_LOW, &desc->flags))
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003760 value = !value;
3761 trace_gpio_value(desc_to_gpio(desc), 0, value);
3762 /*
3763 * collect all normal outputs belonging to the same chip
3764 * open drain and open source outputs are set individually
3765 */
Linus Walleij02e47982017-09-26 21:20:23 +02003766 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02003767 gpio_set_open_drain_value_commit(desc, value);
Linus Walleij02e47982017-09-26 21:20:23 +02003768 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02003769 gpio_set_open_source_value_commit(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003770 } else {
3771 __set_bit(hwgpio, mask);
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +02003772 __assign_bit(hwgpio, bits, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003773 count++;
3774 }
3775 i++;
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003776
3777 if (array_info)
Janusz Krzysztofik35ae7f92018-09-24 01:53:35 +02003778 i = find_next_zero_bit(array_info->set_mask,
3779 array_size, i);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003780 } while ((i < array_size) &&
3781 (desc_array[i]->gdev->chip == chip));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003782 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003783 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003784 gpio_chip_set_multiple(chip, mask, bits);
Laura Abbott30277432018-05-21 10:57:07 -07003785
3786 if (mask != fastpath)
3787 kfree(mask);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003788 }
Laura Abbott30277432018-05-21 10:57:07 -07003789 return 0;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003790}
3791
David Brownelld2876d02008-02-04 22:28:20 -08003792/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003793 * gpiod_set_raw_value() - assign a gpio's raw value
3794 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08003795 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08003796 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003797 * Set the raw value of the GPIO, i.e. the value of its physical line without
3798 * regard for its ACTIVE_LOW status.
3799 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003800 * This function can be called from contexts where we cannot sleep, and will
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003801 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003802 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003803void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003804{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003805 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven3285170f2019-07-01 16:27:38 +02003806 /* Should be using gpiod_set_raw_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003807 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02003808 gpiod_set_raw_value_commit(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08003809}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003810EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08003811
3812/**
Geert Uytterhoeven1e77fc82018-01-09 19:08:21 +01003813 * gpiod_set_value_nocheck() - set a GPIO line value without checking
3814 * @desc: the descriptor to set the value on
3815 * @value: value to set
3816 *
3817 * This sets the value of a GPIO line backing a descriptor, applying
3818 * different semantic quirks like active low and open drain/source
3819 * handling.
3820 */
3821static void gpiod_set_value_nocheck(struct gpio_desc *desc, int value)
3822{
3823 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3824 value = !value;
3825 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
3826 gpio_set_open_drain_value_commit(desc, value);
3827 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
3828 gpio_set_open_source_value_commit(desc, value);
3829 else
3830 gpiod_set_raw_value_commit(desc, value);
3831}
3832
3833/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003834 * gpiod_set_value() - assign a gpio's value
3835 * @desc: gpio whose value will be assigned
3836 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08003837 *
Linus Walleij02e47982017-09-26 21:20:23 +02003838 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
3839 * OPEN_DRAIN and OPEN_SOURCE flags into account.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003840 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003841 * This function can be called from contexts where we cannot sleep, and will
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003842 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003843 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003844void gpiod_set_value(struct gpio_desc *desc, int value)
3845{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003846 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven3285170f2019-07-01 16:27:38 +02003847 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003848 WARN_ON(desc->gdev->chip->can_sleep);
Geert Uytterhoeven1e77fc82018-01-09 19:08:21 +01003849 gpiod_set_value_nocheck(desc, value);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003850}
3851EXPORT_SYMBOL_GPL(gpiod_set_value);
3852
3853/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003854 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003855 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003856 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003857 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003858 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003859 *
3860 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3861 * without regard for their ACTIVE_LOW status.
3862 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003863 * This function can be called from contexts where we cannot sleep, and will
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003864 * complain if the GPIO chip functions potentially sleep.
3865 */
Laura Abbott30277432018-05-21 10:57:07 -07003866int gpiod_set_raw_array_value(unsigned int array_size,
Geert Uytterhoeven3c940662018-09-27 13:38:10 +02003867 struct gpio_desc **desc_array,
3868 struct gpio_array *array_info,
3869 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003870{
3871 if (!desc_array)
Laura Abbott30277432018-05-21 10:57:07 -07003872 return -EINVAL;
3873 return gpiod_set_array_value_complex(true, false, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003874 desc_array, array_info, value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003875}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003876EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003877
3878/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003879 * gpiod_set_array_value() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003880 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003881 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003882 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003883 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003884 *
3885 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3886 * into account.
3887 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003888 * This function can be called from contexts where we cannot sleep, and will
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003889 * complain if the GPIO chip functions potentially sleep.
3890 */
Geert Uytterhoevencf9af0d2018-09-27 13:38:09 +02003891int gpiod_set_array_value(unsigned int array_size,
3892 struct gpio_desc **desc_array,
3893 struct gpio_array *array_info,
3894 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003895{
3896 if (!desc_array)
Geert Uytterhoevencf9af0d2018-09-27 13:38:09 +02003897 return -EINVAL;
3898 return gpiod_set_array_value_complex(false, false, array_size,
3899 desc_array, array_info,
3900 value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003901}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003902EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003903
3904/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003905 * gpiod_cansleep() - report whether gpio value access may sleep
3906 * @desc: gpio to check
3907 *
3908 */
3909int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003910{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003911 VALIDATE_DESC(desc);
3912 return desc->gdev->chip->can_sleep;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003913}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003914EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08003915
David Brownell0f6d5042008-10-15 22:03:14 -07003916/**
Linus Walleij90b39402e2018-06-01 13:21:27 +02003917 * gpiod_set_consumer_name() - set the consumer name for the descriptor
3918 * @desc: gpio to set the consumer name on
3919 * @name: the new consumer name
3920 */
Muchun Song18534df2018-11-01 21:12:50 +08003921int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name)
Linus Walleij90b39402e2018-06-01 13:21:27 +02003922{
Muchun Song18534df2018-11-01 21:12:50 +08003923 VALIDATE_DESC(desc);
3924 if (name) {
3925 name = kstrdup_const(name, GFP_KERNEL);
3926 if (!name)
3927 return -ENOMEM;
3928 }
3929
3930 kfree_const(desc->label);
3931 desc_set_label(desc, name);
3932
3933 return 0;
Linus Walleij90b39402e2018-06-01 13:21:27 +02003934}
3935EXPORT_SYMBOL_GPL(gpiod_set_consumer_name);
3936
3937/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003938 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
3939 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07003940 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003941 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
3942 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07003943 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003944int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07003945{
Linus Walleij4c37ce82016-05-02 13:13:10 +02003946 struct gpio_chip *chip;
3947 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07003948
Linus Walleij79bb71b2016-06-15 22:57:38 +02003949 /*
3950 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
3951 * requires this function to not return zero on an invalid descriptor
3952 * but rather a negative error number.
3953 */
Linus Walleijbfbbe442016-06-16 11:55:55 +02003954 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
Linus Walleij79bb71b2016-06-15 22:57:38 +02003955 return -EINVAL;
3956
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003957 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003958 offset = gpio_chip_hwgpio(desc);
Linus Walleij4c37ce82016-05-02 13:13:10 +02003959 if (chip->to_irq) {
3960 int retirq = chip->to_irq(chip, offset);
3961
3962 /* Zero means NO_IRQ */
3963 if (!retirq)
3964 return -ENXIO;
3965
3966 return retirq;
3967 }
3968 return -ENXIO;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003969}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003970EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003971
Linus Walleijd468bf92013-09-24 11:54:38 +02003972/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003973 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003974 * @chip: the chip the GPIO to lock belongs to
3975 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02003976 *
3977 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08003978 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08003979 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003980int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08003981{
Linus Walleij9c102802016-05-25 10:56:03 +02003982 struct gpio_desc *desc;
Linus Walleijd468bf92013-09-24 11:54:38 +02003983
Linus Walleij9c102802016-05-25 10:56:03 +02003984 desc = gpiochip_get_desc(chip, offset);
3985 if (IS_ERR(desc))
3986 return PTR_ERR(desc);
3987
Linus Walleij60f83392016-11-12 15:01:09 +01003988 /*
3989 * If it's fast: flush the direction setting if something changed
3990 * behind our back
3991 */
3992 if (!chip->can_sleep && chip->get_direction) {
Andy Shevchenko80956792018-07-09 21:47:21 +03003993 int dir = gpiod_get_direction(desc);
Linus Walleij9c102802016-05-25 10:56:03 +02003994
Andy Shevchenko36b31272018-07-03 03:38:31 +03003995 if (dir < 0) {
3996 chip_err(chip, "%s: cannot get GPIO direction\n",
3997 __func__);
3998 return dir;
3999 }
Linus Walleij9c102802016-05-25 10:56:03 +02004000 }
4001
4002 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09004003 chip_err(chip,
Andy Shevchenkob1911712018-07-03 03:39:03 +03004004 "%s: tried to flag a GPIO set as output for IRQ\n",
4005 __func__);
Linus Walleijd468bf92013-09-24 11:54:38 +02004006 return -EIO;
4007 }
4008
Linus Walleij9c102802016-05-25 10:56:03 +02004009 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
Hans Verkuil4e9439d2018-09-08 11:23:16 +02004010 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
Linus Walleij3940c342016-11-14 00:09:07 +01004011
4012 /*
4013 * If the consumer has not set up a label (such as when the
4014 * IRQ is referenced from .to_irq()) we set up a label here
4015 * so it is clear this is used as an interrupt.
4016 */
4017 if (!desc->label)
4018 desc_set_label(desc, "interrupt");
4019
Linus Walleijd468bf92013-09-24 11:54:38 +02004020 return 0;
4021}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09004022EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02004023
Linus Walleijd468bf92013-09-24 11:54:38 +02004024/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09004025 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09004026 * @chip: the chip the GPIO to lock belongs to
4027 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02004028 *
4029 * This is used directly by GPIO drivers that want to indicate
4030 * that a certain GPIO is no longer used exclusively for IRQ.
4031 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09004032void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02004033{
Linus Walleij3940c342016-11-14 00:09:07 +01004034 struct gpio_desc *desc;
4035
4036 desc = gpiochip_get_desc(chip, offset);
4037 if (IS_ERR(desc))
Linus Walleijd468bf92013-09-24 11:54:38 +02004038 return;
4039
Linus Walleij3940c342016-11-14 00:09:07 +01004040 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
Hans Verkuil4e9439d2018-09-08 11:23:16 +02004041 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
Linus Walleij3940c342016-11-14 00:09:07 +01004042
4043 /* If we only had this marking, erase it */
4044 if (desc->label && !strcmp(desc->label, "interrupt"))
4045 desc_set_label(desc, NULL);
Linus Walleijd468bf92013-09-24 11:54:38 +02004046}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09004047EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02004048
Hans Verkuil4e9439d2018-09-08 11:23:16 +02004049void gpiochip_disable_irq(struct gpio_chip *chip, unsigned int offset)
4050{
4051 struct gpio_desc *desc = gpiochip_get_desc(chip, offset);
4052
4053 if (!IS_ERR(desc) &&
4054 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags)))
4055 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
4056}
4057EXPORT_SYMBOL_GPL(gpiochip_disable_irq);
4058
4059void gpiochip_enable_irq(struct gpio_chip *chip, unsigned int offset)
4060{
4061 struct gpio_desc *desc = gpiochip_get_desc(chip, offset);
4062
4063 if (!IS_ERR(desc) &&
4064 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) {
4065 WARN_ON(test_bit(FLAG_IS_OUT, &desc->flags));
4066 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
4067 }
4068}
4069EXPORT_SYMBOL_GPL(gpiochip_enable_irq);
4070
Linus Walleij6cee3822016-02-11 20:16:45 +01004071bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
4072{
4073 if (offset >= chip->ngpio)
4074 return false;
4075
4076 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
4077}
4078EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
4079
Hans Verkuil4e6b8232018-09-08 11:23:14 +02004080int gpiochip_reqres_irq(struct gpio_chip *chip, unsigned int offset)
4081{
4082 int ret;
4083
4084 if (!try_module_get(chip->gpiodev->owner))
4085 return -ENODEV;
4086
4087 ret = gpiochip_lock_as_irq(chip, offset);
4088 if (ret) {
4089 chip_err(chip, "unable to lock HW IRQ %u for IRQ\n", offset);
4090 module_put(chip->gpiodev->owner);
4091 return ret;
4092 }
4093 return 0;
4094}
4095EXPORT_SYMBOL_GPL(gpiochip_reqres_irq);
4096
4097void gpiochip_relres_irq(struct gpio_chip *chip, unsigned int offset)
4098{
4099 gpiochip_unlock_as_irq(chip, offset);
4100 module_put(chip->gpiodev->owner);
4101}
4102EXPORT_SYMBOL_GPL(gpiochip_relres_irq);
4103
Linus Walleij143b65d2016-02-16 15:41:42 +01004104bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
4105{
4106 if (offset >= chip->ngpio)
4107 return false;
4108
4109 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
4110}
4111EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
4112
4113bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
4114{
4115 if (offset >= chip->ngpio)
4116 return false;
4117
4118 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
4119}
4120EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
4121
Charles Keepax05f479b2017-05-23 15:47:29 +01004122bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset)
4123{
4124 if (offset >= chip->ngpio)
4125 return false;
4126
Andrew Jefferye10f72b2017-11-30 14:25:24 +10304127 return !test_bit(FLAG_TRANSITORY, &chip->gpiodev->descs[offset].flags);
Charles Keepax05f479b2017-05-23 15:47:29 +01004128}
4129EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
4130
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004131/**
4132 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
4133 * @desc: gpio whose value will be returned
4134 *
4135 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07004136 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004137 *
4138 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08004139 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004140int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08004141{
David Brownelld2876d02008-02-04 22:28:20 -08004142 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004143 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02004144 return gpiod_get_raw_value_commit(desc);
David Brownelld2876d02008-02-04 22:28:20 -08004145}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004146EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09004147
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004148/**
4149 * gpiod_get_value_cansleep() - return a gpio's value
4150 * @desc: gpio whose value will be returned
4151 *
4152 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07004153 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004154 *
4155 * This function is to be called from contexts that can sleep.
4156 */
4157int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09004158{
David Brownelld2876d02008-02-04 22:28:20 -08004159 int value;
David Brownelld2876d02008-02-04 22:28:20 -08004160
4161 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004162 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02004163 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07004164 if (value < 0)
4165 return value;
4166
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004167 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
4168 value = !value;
4169
David Brownelld2876d02008-02-04 22:28:20 -08004170 return value;
4171}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004172EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09004173
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004174/**
Lukas Wunnereec1d562017-10-12 12:40:10 +02004175 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004176 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02004177 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004178 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004179 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02004180 *
4181 * Read the raw values of the GPIOs, i.e. the values of the physical lines
4182 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
4183 * else an error code.
4184 *
4185 * This function is to be called from contexts that can sleep.
4186 */
4187int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
4188 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004189 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004190 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02004191{
4192 might_sleep_if(extra_checks);
4193 if (!desc_array)
4194 return -EINVAL;
4195 return gpiod_get_array_value_complex(true, true, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004196 desc_array, array_info,
4197 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02004198}
4199EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
4200
4201/**
4202 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004203 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02004204 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004205 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004206 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02004207 *
4208 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
4209 * into account. Return 0 in case of success, else an error code.
4210 *
4211 * This function is to be called from contexts that can sleep.
4212 */
4213int gpiod_get_array_value_cansleep(unsigned int array_size,
4214 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004215 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004216 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02004217{
4218 might_sleep_if(extra_checks);
4219 if (!desc_array)
4220 return -EINVAL;
4221 return gpiod_get_array_value_complex(false, true, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004222 desc_array, array_info,
4223 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02004224}
4225EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
4226
4227/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004228 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
4229 * @desc: gpio whose value will be assigned
4230 * @value: value to assign
4231 *
4232 * Set the raw value of the GPIO, i.e. the value of its physical line without
4233 * regard for its ACTIVE_LOW status.
4234 *
4235 * This function is to be called from contexts that can sleep.
4236 */
4237void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09004238{
David Brownelld2876d02008-02-04 22:28:20 -08004239 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004240 VALIDATE_DESC_VOID(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02004241 gpiod_set_raw_value_commit(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09004242}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004243EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09004244
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004245/**
4246 * gpiod_set_value_cansleep() - assign a gpio's value
4247 * @desc: gpio whose value will be assigned
4248 * @value: value to assign
4249 *
4250 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
4251 * account
4252 *
4253 * This function is to be called from contexts that can sleep.
4254 */
4255void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09004256{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004257 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004258 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1e77fc82018-01-09 19:08:21 +01004259 gpiod_set_value_nocheck(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08004260}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004261EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08004262
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004263/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02004264 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004265 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004266 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004267 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004268 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004269 *
4270 * Set the raw values of the GPIOs, i.e. the values of the physical lines
4271 * without regard for their ACTIVE_LOW status.
4272 *
4273 * This function is to be called from contexts that can sleep.
4274 */
Laura Abbott30277432018-05-21 10:57:07 -07004275int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
Geert Uytterhoeven3c940662018-09-27 13:38:10 +02004276 struct gpio_desc **desc_array,
4277 struct gpio_array *array_info,
4278 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004279{
4280 might_sleep_if(extra_checks);
4281 if (!desc_array)
Laura Abbott30277432018-05-21 10:57:07 -07004282 return -EINVAL;
4283 return gpiod_set_array_value_complex(true, true, array_size, desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004284 array_info, value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004285}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02004286EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004287
4288/**
Dmitry Torokhov3946d182017-08-14 21:59:55 -07004289 * gpiod_add_lookup_tables() - register GPIO device consumers
4290 * @tables: list of tables of consumers to register
4291 * @n: number of tables in the list
4292 */
4293void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
4294{
4295 unsigned int i;
4296
4297 mutex_lock(&gpio_lookup_lock);
4298
4299 for (i = 0; i < n; i++)
4300 list_add_tail(&tables[i]->list, &gpio_lookup_list);
4301
4302 mutex_unlock(&gpio_lookup_lock);
4303}
4304
4305/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02004306 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004307 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004308 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004309 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004310 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004311 *
4312 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
4313 * into account.
4314 *
4315 * This function is to be called from contexts that can sleep.
4316 */
Geert Uytterhoevencf9af0d2018-09-27 13:38:09 +02004317int gpiod_set_array_value_cansleep(unsigned int array_size,
4318 struct gpio_desc **desc_array,
4319 struct gpio_array *array_info,
4320 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004321{
4322 might_sleep_if(extra_checks);
4323 if (!desc_array)
Geert Uytterhoevencf9af0d2018-09-27 13:38:09 +02004324 return -EINVAL;
4325 return gpiod_set_array_value_complex(false, true, array_size,
4326 desc_array, array_info,
4327 value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004328}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02004329EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004330
4331/**
Alexandre Courbotad824782013-12-03 12:20:11 +09004332 * gpiod_add_lookup_table() - register GPIO device consumers
4333 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004334 */
Alexandre Courbotad824782013-12-03 12:20:11 +09004335void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004336{
4337 mutex_lock(&gpio_lookup_lock);
4338
Alexandre Courbotad824782013-12-03 12:20:11 +09004339 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004340
4341 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08004342}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02004343EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
David Brownelld2876d02008-02-04 22:28:20 -08004344
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05304345/**
4346 * gpiod_remove_lookup_table() - unregister GPIO device consumers
4347 * @table: table of consumers to unregister
4348 */
4349void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
4350{
4351 mutex_lock(&gpio_lookup_lock);
4352
4353 list_del(&table->list);
4354
4355 mutex_unlock(&gpio_lookup_lock);
4356}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02004357EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05304358
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02004359/**
4360 * gpiod_add_hogs() - register a set of GPIO hogs from machine code
4361 * @hogs: table of gpio hog entries with a zeroed sentinel at the end
4362 */
4363void gpiod_add_hogs(struct gpiod_hog *hogs)
4364{
4365 struct gpio_chip *chip;
4366 struct gpiod_hog *hog;
4367
4368 mutex_lock(&gpio_machine_hogs_mutex);
4369
4370 for (hog = &hogs[0]; hog->chip_label; hog++) {
4371 list_add_tail(&hog->list, &gpio_machine_hogs);
4372
4373 /*
4374 * The chip may have been registered earlier, so check if it
4375 * exists and, if so, try to hog the line now.
4376 */
4377 chip = find_chip_by_name(hog->chip_label);
4378 if (chip)
4379 gpiochip_machine_hog(chip, hog);
4380 }
4381
4382 mutex_unlock(&gpio_machine_hogs_mutex);
4383}
4384EXPORT_SYMBOL_GPL(gpiod_add_hogs);
4385
Alexandre Courbotad824782013-12-03 12:20:11 +09004386static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
4387{
4388 const char *dev_id = dev ? dev_name(dev) : NULL;
4389 struct gpiod_lookup_table *table;
4390
4391 mutex_lock(&gpio_lookup_lock);
4392
4393 list_for_each_entry(table, &gpio_lookup_list, list) {
4394 if (table->dev_id && dev_id) {
4395 /*
4396 * Valid strings on both ends, must be identical to have
4397 * a match
4398 */
4399 if (!strcmp(table->dev_id, dev_id))
4400 goto found;
4401 } else {
4402 /*
4403 * One of the pointers is NULL, so both must be to have
4404 * a match
4405 */
4406 if (dev_id == table->dev_id)
4407 goto found;
4408 }
4409 }
4410 table = NULL;
4411
4412found:
4413 mutex_unlock(&gpio_lookup_lock);
4414 return table;
4415}
4416
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004417static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Andy Shevchenkofed70262019-04-10 18:39:16 +03004418 unsigned int idx, unsigned long *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004419{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004420 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09004421 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004422 struct gpiod_lookup *p;
4423
Alexandre Courbotad824782013-12-03 12:20:11 +09004424 table = gpiod_find_lookup_table(dev);
4425 if (!table)
4426 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004427
Alexandre Courbotad824782013-12-03 12:20:11 +09004428 for (p = &table->table[0]; p->chip_label; p++) {
4429 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004430
Alexandre Courbotad824782013-12-03 12:20:11 +09004431 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004432 if (p->idx != idx)
4433 continue;
4434
Alexandre Courbotad824782013-12-03 12:20:11 +09004435 /* If the lookup entry has a con_id, require exact match */
4436 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
4437 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004438
Alexandre Courbotad824782013-12-03 12:20:11 +09004439 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004440
Alexandre Courbotad824782013-12-03 12:20:11 +09004441 if (!chip) {
Janusz Krzysztofik8853daf2018-07-04 00:18:19 +02004442 /*
4443 * As the lookup table indicates a chip with
4444 * p->chip_label should exist, assume it may
4445 * still appear later and let the interested
4446 * consumer be probed again or let the Deferred
4447 * Probe infrastructure handle the error.
4448 */
4449 dev_warn(dev, "cannot find GPIO chip %s, deferring\n",
4450 p->chip_label);
4451 return ERR_PTR(-EPROBE_DEFER);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004452 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004453
Alexandre Courbotad824782013-12-03 12:20:11 +09004454 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004455 dev_err(dev,
Geert Uytterhoevend935bd52019-11-27 10:59:19 +01004456 "requested GPIO %u (%u) is out of range [0..%u] for chip %s\n",
4457 idx, p->chip_hwnum, chip->ngpio - 1,
4458 chip->label);
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004459 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09004460 }
4461
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09004462 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09004463 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004464
4465 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09004466 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004467
4468 return desc;
4469}
4470
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004471static int platform_gpio_count(struct device *dev, const char *con_id)
4472{
4473 struct gpiod_lookup_table *table;
4474 struct gpiod_lookup *p;
4475 unsigned int count = 0;
4476
4477 table = gpiod_find_lookup_table(dev);
4478 if (!table)
4479 return -ENOENT;
4480
4481 for (p = &table->table[0]; p->chip_label; p++) {
4482 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
4483 (!con_id && !p->con_id))
4484 count++;
4485 }
4486 if (!count)
4487 return -ENOENT;
4488
4489 return count;
4490}
4491
4492/**
Dmitry Torokhov13949fa2019-09-12 20:22:39 -07004493 * fwnode_gpiod_get_index - obtain a GPIO from firmware node
4494 * @fwnode: handle of the firmware node
4495 * @con_id: function within the GPIO consumer
4496 * @index: index of the GPIO to obtain for the consumer
4497 * @flags: GPIO initialization flags
4498 * @label: label to attach to the requested GPIO
4499 *
4500 * This function can be used for drivers that get their configuration
4501 * from opaque firmware.
4502 *
4503 * The function properly finds the corresponding GPIO using whatever is the
4504 * underlying firmware interface and then makes sure that the GPIO
4505 * descriptor is requested before it is returned to the caller.
4506 *
4507 * Returns:
4508 * On successful request the GPIO pin is configured in accordance with
4509 * provided @flags.
4510 *
4511 * In case of error an ERR_PTR() is returned.
4512 */
4513struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
4514 const char *con_id, int index,
4515 enum gpiod_flags flags,
4516 const char *label)
4517{
4518 struct gpio_desc *desc;
4519 char prop_name[32]; /* 32 is max size of property name */
4520 unsigned int i;
4521
4522 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
4523 if (con_id)
4524 snprintf(prop_name, sizeof(prop_name), "%s-%s",
4525 con_id, gpio_suffixes[i]);
4526 else
4527 snprintf(prop_name, sizeof(prop_name), "%s",
4528 gpio_suffixes[i]);
4529
4530 desc = fwnode_get_named_gpiod(fwnode, prop_name, index, flags,
4531 label);
4532 if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT))
4533 break;
4534 }
4535
4536 return desc;
4537}
4538EXPORT_SYMBOL_GPL(fwnode_gpiod_get_index);
4539
4540/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004541 * gpiod_count - return the number of GPIOs associated with a device / function
4542 * or -ENOENT if no GPIO has been assigned to the requested function
4543 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4544 * @con_id: function within the GPIO consumer
4545 */
4546int gpiod_count(struct device *dev, const char *con_id)
4547{
4548 int count = -ENOENT;
4549
4550 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
Linus Walleijf626d6d2019-07-17 09:10:01 +02004551 count = of_gpio_get_count(dev, con_id);
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004552 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
4553 count = acpi_gpio_count(dev, con_id);
4554
4555 if (count < 0)
4556 count = platform_gpio_count(dev, con_id);
4557
4558 return count;
4559}
4560EXPORT_SYMBOL_GPL(gpiod_count);
4561
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004562/**
Thierry Reding08791622014-04-25 16:54:22 +02004563 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09004564 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004565 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004566 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004567 *
4568 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004569 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07004570 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004571 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004572struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004573 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004574{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004575 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004576}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004577EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004578
4579/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02004580 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
4581 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4582 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004583 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02004584 *
4585 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
4586 * the requested function it will return NULL. This is convenient for drivers
4587 * that need to handle optional GPIOs.
4588 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004589struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004590 const char *con_id,
4591 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02004592{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004593 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02004594}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004595EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02004596
Benoit Parrotf625d462015-02-02 11:44:44 -06004597
4598/**
4599 * gpiod_configure_flags - helper function to configure a given GPIO
4600 * @desc: gpio whose value will be assigned
4601 * @con_id: function within the GPIO consumer
Andy Shevchenkofed70262019-04-10 18:39:16 +03004602 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4603 * of_find_gpio() or of_get_gpio_hog()
Benoit Parrotf625d462015-02-02 11:44:44 -06004604 * @dflags: gpiod_flags - optional GPIO initialization flags
4605 *
4606 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
4607 * requested function and/or index, or another IS_ERR() code if an error
4608 * occurred while trying to acquire the GPIO.
4609 */
Andy Shevchenkoc29fd9e2017-05-23 20:03:16 +03004610int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Johan Hovold85b03b32016-07-03 18:32:05 +02004611 unsigned long lflags, enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06004612{
Linus Walleijd377f562019-07-16 11:11:45 +02004613 int ret;
Benoit Parrotf625d462015-02-02 11:44:44 -06004614
Johan Hovold85b03b32016-07-03 18:32:05 +02004615 if (lflags & GPIO_ACTIVE_LOW)
4616 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
Linus Walleijf926dfc2017-09-10 19:26:22 +02004617
Johan Hovold85b03b32016-07-03 18:32:05 +02004618 if (lflags & GPIO_OPEN_DRAIN)
4619 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
Linus Walleijf926dfc2017-09-10 19:26:22 +02004620 else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) {
4621 /*
4622 * This enforces open drain mode from the consumer side.
4623 * This is necessary for some busses like I2C, but the lookup
4624 * should *REALLY* have specified them as open drain in the
4625 * first place, so print a little warning here.
4626 */
4627 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
4628 gpiod_warn(desc,
4629 "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
4630 }
4631
Johan Hovold85b03b32016-07-03 18:32:05 +02004632 if (lflags & GPIO_OPEN_SOURCE)
4633 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Andrew Jefferye10f72b2017-11-30 14:25:24 +10304634
Thomas Petazzonid4499912019-02-07 17:28:58 +01004635 if ((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DOWN)) {
4636 gpiod_err(desc,
4637 "both pull-up and pull-down enabled, invalid configuration\n");
4638 return -EINVAL;
4639 }
4640
4641 if (lflags & GPIO_PULL_UP)
4642 set_bit(FLAG_PULL_UP, &desc->flags);
4643 else if (lflags & GPIO_PULL_DOWN)
4644 set_bit(FLAG_PULL_DOWN, &desc->flags);
4645
Linus Walleijd377f562019-07-16 11:11:45 +02004646 ret = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
4647 if (ret < 0)
4648 return ret;
Johan Hovold85b03b32016-07-03 18:32:05 +02004649
Benoit Parrotf625d462015-02-02 11:44:44 -06004650 /* No particular flag request, return here... */
4651 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
4652 pr_debug("no flags found for %s\n", con_id);
4653 return 0;
4654 }
4655
4656 /* Process flags */
4657 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
Linus Walleijd377f562019-07-16 11:11:45 +02004658 ret = gpiod_direction_output(desc,
Linus Walleijad177312016-11-13 23:02:44 +01004659 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
Benoit Parrotf625d462015-02-02 11:44:44 -06004660 else
Linus Walleijd377f562019-07-16 11:11:45 +02004661 ret = gpiod_direction_input(desc);
Benoit Parrotf625d462015-02-02 11:44:44 -06004662
Linus Walleijd377f562019-07-16 11:11:45 +02004663 return ret;
Benoit Parrotf625d462015-02-02 11:44:44 -06004664}
4665
Thierry Reding29a1f2332014-04-25 17:10:06 +02004666/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004667 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02004668 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004669 * @con_id: function within the GPIO consumer
4670 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004671 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004672 *
4673 * This variant of gpiod_get() allows to access GPIOs other than the first
4674 * defined one for functions that define several GPIOs.
4675 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004676 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
4677 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07004678 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004679 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004680struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004681 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004682 unsigned int idx,
4683 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004684{
Andy Shevchenko2d6c06f2019-04-10 18:39:17 +03004685 unsigned long lookupflags = GPIO_LOOKUP_FLAGS_DEFAULT;
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09004686 struct gpio_desc *desc = NULL;
Linus Walleijd377f562019-07-16 11:11:45 +02004687 int ret;
Linus Walleij7d18f0a2018-01-16 08:29:50 +01004688 /* Maybe we have a device name, maybe not */
4689 const char *devname = dev ? dev_name(dev) : "?";
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004690
4691 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
4692
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01004693 if (dev) {
4694 /* Using device tree? */
4695 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
4696 dev_dbg(dev, "using device tree for GPIO lookup\n");
4697 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
4698 } else if (ACPI_COMPANION(dev)) {
4699 dev_dbg(dev, "using ACPI for GPIO lookup\n");
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +03004700 desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags);
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01004701 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09004702 }
4703
4704 /*
4705 * Either we are not using DT or ACPI, or their lookup did not return
4706 * a result. In that case, use platform lookup as a fallback.
4707 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004708 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04004709 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004710 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004711 }
4712
4713 if (IS_ERR(desc)) {
Wang Dongsheng9d5a1f22018-02-27 00:12:13 -08004714 dev_dbg(dev, "No GPIO consumer %s found\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004715 return desc;
4716 }
4717
Linus Walleij7d18f0a2018-01-16 08:29:50 +01004718 /*
4719 * If a connection label was passed use that, else attempt to use
4720 * the device name as label
4721 */
Linus Walleijd377f562019-07-16 11:11:45 +02004722 ret = gpiod_request(desc, con_id ? con_id : devname);
4723 if (ret < 0) {
4724 if (ret == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE) {
Linus Walleijb0ce7b292018-10-12 14:54:12 +02004725 /*
4726 * This happens when there are several consumers for
4727 * the same GPIO line: we just return here without
4728 * further initialization. It is a bit if a hack.
4729 * This is necessary to support fixed regulators.
4730 *
4731 * FIXME: Make this more sane and safe.
4732 */
4733 dev_info(dev, "nonexclusive access to GPIO for %s\n",
4734 con_id ? con_id : devname);
4735 return desc;
4736 } else {
Linus Walleijd377f562019-07-16 11:11:45 +02004737 return ERR_PTR(ret);
Linus Walleijb0ce7b292018-10-12 14:54:12 +02004738 }
4739 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004740
Linus Walleijd377f562019-07-16 11:11:45 +02004741 ret = gpiod_configure_flags(desc, con_id, lookupflags, flags);
4742 if (ret < 0) {
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004743 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
4744 gpiod_put(desc);
Linus Walleijd377f562019-07-16 11:11:45 +02004745 return ERR_PTR(ret);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004746 }
4747
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004748 return desc;
4749}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004750EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004751
4752/**
Mika Westerberg40b73182014-10-21 13:33:59 +02004753 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
4754 * @fwnode: handle of the firmware node
4755 * @propname: name of the firmware property representing the GPIO
Linus Walleij6392cca2017-12-29 02:07:54 +01004756 * @index: index of the GPIO to obtain for the consumer
Andy Shevchenkoa264d102017-01-09 16:02:28 +02004757 * @dflags: GPIO initialization flags
Thierry Reding950d55f52017-07-24 16:57:22 +02004758 * @label: label to attach to the requested GPIO
Mika Westerberg40b73182014-10-21 13:33:59 +02004759 *
4760 * This function can be used for drivers that get their configuration
Linus Walleij6392cca2017-12-29 02:07:54 +01004761 * from opaque firmware.
Mika Westerberg40b73182014-10-21 13:33:59 +02004762 *
Linus Walleij6392cca2017-12-29 02:07:54 +01004763 * The function properly finds the corresponding GPIO using whatever is the
Mika Westerberg40b73182014-10-21 13:33:59 +02004764 * underlying firmware interface and then makes sure that the GPIO
4765 * descriptor is requested before it is returned to the caller.
4766 *
Thierry Reding950d55f52017-07-24 16:57:22 +02004767 * Returns:
Andy Shevchenkoff213782017-02-28 17:03:12 +02004768 * On successful request the GPIO pin is configured in accordance with
Andy Shevchenkoa264d102017-01-09 16:02:28 +02004769 * provided @dflags.
4770 *
Mika Westerberg40b73182014-10-21 13:33:59 +02004771 * In case of error an ERR_PTR() is returned.
4772 */
4773struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
Boris Brezillon537b94d2017-02-02 14:53:11 +01004774 const char *propname, int index,
Alexander Steinb2987d72017-01-12 17:39:24 +01004775 enum gpiod_flags dflags,
4776 const char *label)
Mika Westerberg40b73182014-10-21 13:33:59 +02004777{
Andy Shevchenko2d6c06f2019-04-10 18:39:17 +03004778 unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
Mika Westerberg40b73182014-10-21 13:33:59 +02004779 struct gpio_desc *desc = ERR_PTR(-ENODEV);
Mika Westerberg40b73182014-10-21 13:33:59 +02004780 int ret;
4781
4782 if (!fwnode)
4783 return ERR_PTR(-EINVAL);
4784
4785 if (is_of_node(fwnode)) {
Linus Walleij6392cca2017-12-29 02:07:54 +01004786 desc = gpiod_get_from_of_node(to_of_node(fwnode),
4787 propname, index,
4788 dflags,
4789 label);
4790 return desc;
Mika Westerberg40b73182014-10-21 13:33:59 +02004791 } else if (is_acpi_node(fwnode)) {
4792 struct acpi_gpio_info info;
4793
Boris Brezillon537b94d2017-02-02 14:53:11 +01004794 desc = acpi_node_get_gpiod(fwnode, propname, index, &info);
Linus Walleij6392cca2017-12-29 02:07:54 +01004795 if (IS_ERR(desc))
4796 return desc;
4797
4798 acpi_gpio_update_gpiod_flags(&dflags, &info);
Andy Shevchenko606be342019-04-10 18:39:20 +03004799 acpi_gpio_update_gpiod_lookup_flags(&lflags, &info);
Mika Westerberg40b73182014-10-21 13:33:59 +02004800 }
4801
Linus Walleij6392cca2017-12-29 02:07:54 +01004802 /* Currently only ACPI takes this path */
Alexander Steinb2987d72017-01-12 17:39:24 +01004803 ret = gpiod_request(desc, label);
Johan Hovold85b03b32016-07-03 18:32:05 +02004804 if (ret)
4805 return ERR_PTR(ret);
4806
Andy Shevchenkoa264d102017-01-09 16:02:28 +02004807 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
4808 if (ret < 0) {
4809 gpiod_put(desc);
4810 return ERR_PTR(ret);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03004811 }
4812
Mika Westerberg40b73182014-10-21 13:33:59 +02004813 return desc;
4814}
4815EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
4816
4817/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02004818 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
4819 * function
4820 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4821 * @con_id: function within the GPIO consumer
4822 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004823 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02004824 *
4825 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
4826 * specified index was assigned to the requested function it will return NULL.
4827 * This is convenient for drivers that need to handle optional GPIOs.
4828 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004829struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02004830 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004831 unsigned int index,
4832 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02004833{
4834 struct gpio_desc *desc;
4835
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004836 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02004837 if (IS_ERR(desc)) {
4838 if (PTR_ERR(desc) == -ENOENT)
4839 return NULL;
4840 }
4841
4842 return desc;
4843}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004844EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02004845
4846/**
Benoit Parrotf625d462015-02-02 11:44:44 -06004847 * gpiod_hog - Hog the specified GPIO desc given the provided flags
4848 * @desc: gpio whose value will be assigned
4849 * @name: gpio line name
Andy Shevchenkofed70262019-04-10 18:39:16 +03004850 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4851 * of_find_gpio() or of_get_gpio_hog()
Benoit Parrotf625d462015-02-02 11:44:44 -06004852 * @dflags: gpiod_flags - optional GPIO initialization flags
4853 */
4854int gpiod_hog(struct gpio_desc *desc, const char *name,
4855 unsigned long lflags, enum gpiod_flags dflags)
4856{
4857 struct gpio_chip *chip;
4858 struct gpio_desc *local_desc;
4859 int hwnum;
Linus Walleijd377f562019-07-16 11:11:45 +02004860 int ret;
Benoit Parrotf625d462015-02-02 11:44:44 -06004861
4862 chip = gpiod_to_chip(desc);
4863 hwnum = gpio_chip_hwgpio(desc);
4864
Linus Walleij5923ea62019-04-26 14:40:18 +02004865 local_desc = gpiochip_request_own_desc(chip, hwnum, name,
4866 lflags, dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -06004867 if (IS_ERR(local_desc)) {
Linus Walleijd377f562019-07-16 11:11:45 +02004868 ret = PTR_ERR(local_desc);
Laxman Dewanganc31a5712016-03-11 19:13:21 +05304869 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
Linus Walleijd377f562019-07-16 11:11:45 +02004870 name, chip->label, hwnum, ret);
4871 return ret;
Benoit Parrotf625d462015-02-02 11:44:44 -06004872 }
4873
Benoit Parrotf625d462015-02-02 11:44:44 -06004874 /* Mark GPIO as hogged so it can be identified and removed later */
4875 set_bit(FLAG_IS_HOGGED, &desc->flags);
4876
4877 pr_info("GPIO line %d (%s) hogged as %s%s\n",
4878 desc_to_gpio(desc), name,
Bartosz Golaszewskib27f3002019-11-13 14:16:29 +01004879 (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
4880 (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ?
4881 (dflags & GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low" : "");
Benoit Parrotf625d462015-02-02 11:44:44 -06004882
4883 return 0;
4884}
4885
4886/**
4887 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
4888 * @chip: gpio chip to act on
Benoit Parrotf625d462015-02-02 11:44:44 -06004889 */
4890static void gpiochip_free_hogs(struct gpio_chip *chip)
4891{
4892 int id;
4893
4894 for (id = 0; id < chip->ngpio; id++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01004895 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
4896 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
Benoit Parrotf625d462015-02-02 11:44:44 -06004897 }
4898}
4899
4900/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004901 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
4902 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4903 * @con_id: function within the GPIO consumer
4904 * @flags: optional GPIO initialization flags
4905 *
4906 * This function acquires all the GPIOs defined under a given function.
4907 *
4908 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
4909 * no GPIO has been assigned to the requested function, or another IS_ERR()
4910 * code if an error occurred while trying to acquire the GPIOs.
4911 */
4912struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
4913 const char *con_id,
4914 enum gpiod_flags flags)
4915{
4916 struct gpio_desc *desc;
4917 struct gpio_descs *descs;
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004918 struct gpio_array *array_info = NULL;
4919 struct gpio_chip *chip;
4920 int count, bitmap_size;
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004921
4922 count = gpiod_count(dev, con_id);
4923 if (count < 0)
4924 return ERR_PTR(count);
4925
Kees Cookacafe7e2018-05-08 13:45:50 -07004926 descs = kzalloc(struct_size(descs, desc, count), GFP_KERNEL);
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004927 if (!descs)
4928 return ERR_PTR(-ENOMEM);
4929
4930 for (descs->ndescs = 0; descs->ndescs < count; ) {
4931 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
4932 if (IS_ERR(desc)) {
4933 gpiod_put_array(descs);
4934 return ERR_CAST(desc);
4935 }
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004936
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004937 descs->desc[descs->ndescs] = desc;
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004938
4939 chip = gpiod_to_chip(desc);
4940 /*
Janusz Krzysztofikc4c958a2018-09-24 01:53:36 +02004941 * If pin hardware number of array member 0 is also 0, select
4942 * its chip as a candidate for fast bitmap processing path.
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004943 */
Janusz Krzysztofikc4c958a2018-09-24 01:53:36 +02004944 if (descs->ndescs == 0 && gpio_chip_hwgpio(desc) == 0) {
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004945 struct gpio_descs *array;
4946
4947 bitmap_size = BITS_TO_LONGS(chip->ngpio > count ?
4948 chip->ngpio : count);
4949
4950 array = kzalloc(struct_size(descs, desc, count) +
4951 struct_size(array_info, invert_mask,
4952 3 * bitmap_size), GFP_KERNEL);
4953 if (!array) {
4954 gpiod_put_array(descs);
4955 return ERR_PTR(-ENOMEM);
4956 }
4957
4958 memcpy(array, descs,
4959 struct_size(descs, desc, descs->ndescs + 1));
4960 kfree(descs);
4961
4962 descs = array;
4963 array_info = (void *)(descs->desc + count);
4964 array_info->get_mask = array_info->invert_mask +
4965 bitmap_size;
4966 array_info->set_mask = array_info->get_mask +
4967 bitmap_size;
4968
4969 array_info->desc = descs->desc;
4970 array_info->size = count;
4971 array_info->chip = chip;
4972 bitmap_set(array_info->get_mask, descs->ndescs,
4973 count - descs->ndescs);
4974 bitmap_set(array_info->set_mask, descs->ndescs,
4975 count - descs->ndescs);
4976 descs->info = array_info;
4977 }
Janusz Krzysztofikc4c958a2018-09-24 01:53:36 +02004978 /* Unmark array members which don't belong to the 'fast' chip */
4979 if (array_info && array_info->chip != chip) {
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004980 __clear_bit(descs->ndescs, array_info->get_mask);
4981 __clear_bit(descs->ndescs, array_info->set_mask);
Janusz Krzysztofikc4c958a2018-09-24 01:53:36 +02004982 }
4983 /*
4984 * Detect array members which belong to the 'fast' chip
4985 * but their pins are not in hardware order.
4986 */
4987 else if (array_info &&
4988 gpio_chip_hwgpio(desc) != descs->ndescs) {
4989 /*
4990 * Don't use fast path if all array members processed so
4991 * far belong to the same chip as this one but its pin
4992 * hardware number is different from its array index.
4993 */
4994 if (bitmap_full(array_info->get_mask, descs->ndescs)) {
4995 array_info = NULL;
4996 } else {
4997 __clear_bit(descs->ndescs,
4998 array_info->get_mask);
4999 __clear_bit(descs->ndescs,
5000 array_info->set_mask);
5001 }
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02005002 } else if (array_info) {
5003 /* Exclude open drain or open source from fast output */
5004 if (gpiochip_line_is_open_drain(chip, descs->ndescs) ||
5005 gpiochip_line_is_open_source(chip, descs->ndescs))
5006 __clear_bit(descs->ndescs,
5007 array_info->set_mask);
5008 /* Identify 'fast' pins which require invertion */
5009 if (gpiod_is_active_low(desc))
5010 __set_bit(descs->ndescs,
5011 array_info->invert_mask);
5012 }
5013
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01005014 descs->ndescs++;
5015 }
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02005016 if (array_info)
5017 dev_dbg(dev,
5018 "GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n",
5019 array_info->chip->label, array_info->size,
5020 *array_info->get_mask, *array_info->set_mask,
5021 *array_info->invert_mask);
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01005022 return descs;
5023}
5024EXPORT_SYMBOL_GPL(gpiod_get_array);
5025
5026/**
5027 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
5028 * function
5029 * @dev: GPIO consumer, can be NULL for system-global GPIOs
5030 * @con_id: function within the GPIO consumer
5031 * @flags: optional GPIO initialization flags
5032 *
5033 * This is equivalent to gpiod_get_array(), except that when no GPIO was
5034 * assigned to the requested function it will return NULL.
5035 */
5036struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
5037 const char *con_id,
5038 enum gpiod_flags flags)
5039{
5040 struct gpio_descs *descs;
5041
5042 descs = gpiod_get_array(dev, con_id, flags);
Masahiro Yamada45586c72020-02-03 17:37:45 -08005043 if (PTR_ERR(descs) == -ENOENT)
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01005044 return NULL;
5045
5046 return descs;
5047}
5048EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
5049
5050/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07005051 * gpiod_put - dispose of a GPIO descriptor
5052 * @desc: GPIO descriptor to dispose of
5053 *
5054 * No descriptor can be used after gpiod_put() has been called on it.
5055 */
5056void gpiod_put(struct gpio_desc *desc)
5057{
Andy Shevchenko1d7765b2019-03-26 17:21:14 +02005058 if (desc)
5059 gpiod_free(desc);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07005060}
5061EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08005062
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01005063/**
5064 * gpiod_put_array - dispose of multiple GPIO descriptors
5065 * @descs: struct gpio_descs containing an array of descriptors
5066 */
5067void gpiod_put_array(struct gpio_descs *descs)
5068{
5069 unsigned int i;
5070
5071 for (i = 0; i < descs->ndescs; i++)
5072 gpiod_put(descs->desc[i]);
5073
5074 kfree(descs);
5075}
5076EXPORT_SYMBOL_GPL(gpiod_put_array);
5077
Linus Walleij3c702e92015-10-21 15:29:53 +02005078static int __init gpiolib_dev_init(void)
5079{
5080 int ret;
5081
5082 /* Register GPIO sysfs bus */
Andy Shevchenkob1911712018-07-03 03:39:03 +03005083 ret = bus_register(&gpio_bus_type);
Linus Walleij3c702e92015-10-21 15:29:53 +02005084 if (ret < 0) {
5085 pr_err("gpiolib: could not register GPIO bus type\n");
5086 return ret;
5087 }
5088
Geert Uytterhoevenddd88912019-11-27 09:42:47 +01005089 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, GPIOCHIP_NAME);
Linus Walleij3c702e92015-10-21 15:29:53 +02005090 if (ret < 0) {
5091 pr_err("gpiolib: failed to allocate char dev region\n");
5092 bus_unregister(&gpio_bus_type);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07005093 } else {
5094 gpiolib_initialized = true;
5095 gpiochip_setup_devs();
Linus Walleij3c702e92015-10-21 15:29:53 +02005096 }
5097 return ret;
5098}
5099core_initcall(gpiolib_dev_init);
5100
David Brownelld2876d02008-02-04 22:28:20 -08005101#ifdef CONFIG_DEBUG_FS
5102
Linus Walleijfdeb8e12016-02-10 10:57:36 +01005103static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
David Brownelld2876d02008-02-04 22:28:20 -08005104{
5105 unsigned i;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01005106 struct gpio_chip *chip = gdev->chip;
5107 unsigned gpio = gdev->base;
5108 struct gpio_desc *gdesc = &gdev->descs[0];
Linus Walleij90fd2272018-10-01 23:06:17 +02005109 bool is_out;
5110 bool is_irq;
5111 bool active_low;
David Brownelld2876d02008-02-04 22:28:20 -08005112
Linus Walleijfdeb8e12016-02-10 10:57:36 +01005113 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02005114 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
5115 if (gdesc->name) {
5116 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
5117 gpio, gdesc->name);
5118 }
David Brownelld2876d02008-02-04 22:28:20 -08005119 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02005120 }
David Brownelld2876d02008-02-04 22:28:20 -08005121
Alexandre Courbot372e7222013-02-03 01:29:29 +09005122 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08005123 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02005124 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Linus Walleij90fd2272018-10-01 23:06:17 +02005125 active_low = test_bit(FLAG_ACTIVE_LOW, &gdesc->flags);
5126 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s%s",
Markus Pargmannced433e2015-08-14 16:11:02 +02005127 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08005128 is_out ? "out" : "in ",
Andy Shevchenko1c22a252018-07-12 20:36:42 +03005129 chip->get ? (chip->get(chip, i) ? "hi" : "lo") : "? ",
Linus Walleij90fd2272018-10-01 23:06:17 +02005130 is_irq ? "IRQ " : "",
5131 active_low ? "ACTIVE LOW" : "");
David Brownelld2876d02008-02-04 22:28:20 -08005132 seq_printf(s, "\n");
5133 }
5134}
5135
Thierry Redingf9c4a312012-04-12 13:26:01 +02005136static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08005137{
Grant Likely362432a2013-02-09 09:41:49 +00005138 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02005139 struct gpio_device *gdev = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09005140 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02005141
5142 s->private = "";
5143
Grant Likely362432a2013-02-09 09:41:49 +00005144 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02005145 list_for_each_entry(gdev, &gpio_devices, list)
Grant Likely362432a2013-02-09 09:41:49 +00005146 if (index-- == 0) {
5147 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02005148 return gdev;
Grant Likely362432a2013-02-09 09:41:49 +00005149 }
5150 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09005151
5152 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02005153}
5154
5155static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
5156{
Grant Likely362432a2013-02-09 09:41:49 +00005157 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02005158 struct gpio_device *gdev = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02005159 void *ret = NULL;
5160
Grant Likely362432a2013-02-09 09:41:49 +00005161 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02005162 if (list_is_last(&gdev->list, &gpio_devices))
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09005163 ret = NULL;
5164 else
Linus Walleijff2b1352015-10-20 11:10:38 +02005165 ret = list_entry(gdev->list.next, struct gpio_device, list);
Grant Likely362432a2013-02-09 09:41:49 +00005166 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02005167
5168 s->private = "\n";
5169 ++*pos;
5170
5171 return ret;
5172}
5173
5174static void gpiolib_seq_stop(struct seq_file *s, void *v)
5175{
5176}
5177
5178static int gpiolib_seq_show(struct seq_file *s, void *v)
5179{
Linus Walleijff2b1352015-10-20 11:10:38 +02005180 struct gpio_device *gdev = v;
5181 struct gpio_chip *chip = gdev->chip;
5182 struct device *parent;
Thierry Redingf9c4a312012-04-12 13:26:01 +02005183
Linus Walleijff2b1352015-10-20 11:10:38 +02005184 if (!chip) {
5185 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
5186 dev_name(&gdev->dev));
5187 return 0;
5188 }
5189
5190 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
5191 dev_name(&gdev->dev),
Linus Walleijfdeb8e12016-02-10 10:57:36 +01005192 gdev->base, gdev->base + gdev->ngpio - 1);
Linus Walleijff2b1352015-10-20 11:10:38 +02005193 parent = chip->parent;
5194 if (parent)
5195 seq_printf(s, ", parent: %s/%s",
5196 parent->bus ? parent->bus->name : "no-bus",
5197 dev_name(parent));
Thierry Redingf9c4a312012-04-12 13:26:01 +02005198 if (chip->label)
5199 seq_printf(s, ", %s", chip->label);
5200 if (chip->can_sleep)
5201 seq_printf(s, ", can sleep");
5202 seq_printf(s, ":\n");
5203
5204 if (chip->dbg_show)
5205 chip->dbg_show(s, chip);
5206 else
Linus Walleijfdeb8e12016-02-10 10:57:36 +01005207 gpiolib_dbg_show(s, gdev);
Thierry Redingf9c4a312012-04-12 13:26:01 +02005208
David Brownelld2876d02008-02-04 22:28:20 -08005209 return 0;
5210}
5211
Thierry Redingf9c4a312012-04-12 13:26:01 +02005212static const struct seq_operations gpiolib_seq_ops = {
5213 .start = gpiolib_seq_start,
5214 .next = gpiolib_seq_next,
5215 .stop = gpiolib_seq_stop,
5216 .show = gpiolib_seq_show,
5217};
5218
David Brownelld2876d02008-02-04 22:28:20 -08005219static int gpiolib_open(struct inode *inode, struct file *file)
5220{
Thierry Redingf9c4a312012-04-12 13:26:01 +02005221 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08005222}
5223
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005224static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02005225 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08005226 .open = gpiolib_open,
5227 .read = seq_read,
5228 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02005229 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08005230};
5231
5232static int __init gpiolib_debugfs_init(void)
5233{
5234 /* /sys/kernel/debug/gpio */
Greg Kroah-Hartmanacc68b02019-06-18 17:50:45 +02005235 debugfs_create_file("gpio", S_IFREG | S_IRUGO, NULL, NULL,
5236 &gpiolib_operations);
David Brownelld2876d02008-02-04 22:28:20 -08005237 return 0;
5238}
5239subsys_initcall(gpiolib_debugfs_init);
5240
5241#endif /* DEBUG_FS */