blob: 5999bab3ba915f1feb99b4bbe834ec9b715b6702 [file] [log] [blame]
Linus Walleijdae5f0a2018-09-25 09:08:48 +02001// SPDX-License-Identifier: GPL-2.0
Andy Shevchenko923a6542017-05-25 16:08:38 +03002#include <linux/bitmap.h>
David Brownelld2876d02008-02-04 22:28:20 -08003#include <linux/kernel.h>
4#include <linux/module.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -07005#include <linux/interrupt.h>
David Brownelld2876d02008-02-04 22:28:20 -08006#include <linux/irq.h>
7#include <linux/spinlock.h>
Alexandre Courbot1a989d02013-02-03 01:29:24 +09008#include <linux/list.h>
David Brownelld8f388d82008-07-25 01:46:07 -07009#include <linux/device.h>
10#include <linux/err.h>
11#include <linux/debugfs.h>
12#include <linux/seq_file.h>
13#include <linux/gpio.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -070014#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Rafael J. Wysocki7b199812013-11-11 22:41:56 +010016#include <linux/acpi.h>
Alexandre Courbot53e7cac2013-11-16 21:44:52 +090017#include <linux/gpio/driver.h>
Linus Walleij0a6d3152014-07-24 20:08:55 +020018#include <linux/gpio/machine.h>
Jonas Gorskic771c2f2015-10-11 17:34:15 +020019#include <linux/pinctrl/consumer.h>
Linus Walleij3c702e92015-10-21 15:29:53 +020020#include <linux/cdev.h>
21#include <linux/fs.h>
22#include <linux/uaccess.h>
Linus Walleij8b92e172016-05-27 14:24:04 +020023#include <linux/compat.h>
Linus Walleijd7c51b42016-04-26 10:35:29 +020024#include <linux/anon_inodes.h>
Lars-Peter Clausen953b9562016-10-24 13:59:15 +020025#include <linux/file.h>
Linus Walleij61f922d2016-06-02 11:30:15 +020026#include <linux/kfifo.h>
27#include <linux/poll.h>
28#include <linux/timekeeping.h>
Linus Walleij3c702e92015-10-21 15:29:53 +020029#include <uapi/linux/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080030
Mika Westerberg664e3e52014-01-08 12:40:54 +020031#include "gpiolib.h"
Linus Walleijf626d6d2019-07-17 09:10:01 +020032#include "gpiolib-of.h"
Andy Shevchenko77cb9072019-07-30 13:43:36 +030033#include "gpiolib-acpi.h"
Mika Westerberg664e3e52014-01-08 12:40:54 +020034
Uwe Kleine-König3f397c212011-05-20 00:40:19 -060035#define CREATE_TRACE_POINTS
36#include <trace/events/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080037
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070038/* Implementation infrastructure for GPIO interfaces.
David Brownelld2876d02008-02-04 22:28:20 -080039 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070040 * The GPIO programming interface allows for inlining speed-critical
41 * get/set operations for common cases, so that access to SOC-integrated
42 * GPIOs can sometimes cost only an instruction or two per bit.
David Brownelld2876d02008-02-04 22:28:20 -080043 */
44
45
46/* When debugging, extend minimal trust to callers and platform code.
47 * Also emit diagnostic messages that may help initial bringup, when
48 * board setup or driver bugs are most common.
49 *
50 * Otherwise, minimize overhead in what may be bitbanging codepaths.
51 */
52#ifdef DEBUG
53#define extra_checks 1
54#else
55#define extra_checks 0
56#endif
57
Linus Walleijff2b1352015-10-20 11:10:38 +020058/* Device and char device-related information */
59static DEFINE_IDA(gpio_ida);
Linus Walleij3c702e92015-10-21 15:29:53 +020060static dev_t gpio_devt;
61#define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
62static struct bus_type gpio_bus_type = {
63 .name = "gpio",
64};
Linus Walleijff2b1352015-10-20 11:10:38 +020065
Laura Abbott30277432018-05-21 10:57:07 -070066/*
67 * Number of GPIOs to use for the fast path in set array
68 */
69#define FASTPATH_NGPIO CONFIG_GPIOLIB_FASTPATH_LIMIT
70
David Brownelld2876d02008-02-04 22:28:20 -080071/* gpio_lock prevents conflicts during gpio_desc[] table updates.
72 * While any GPIO is requested, its gpio_chip is not removable;
73 * each GPIO's "requested" flag serves as a lock and refcount.
74 */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090075DEFINE_SPINLOCK(gpio_lock);
David Brownelld2876d02008-02-04 22:28:20 -080076
Alexandre Courbotbae48da2013-10-17 10:21:38 -070077static DEFINE_MUTEX(gpio_lookup_lock);
78static LIST_HEAD(gpio_lookup_list);
Linus Walleijff2b1352015-10-20 11:10:38 +020079LIST_HEAD(gpio_devices);
Johan Hovold6d867502015-05-04 17:23:25 +020080
Bartosz Golaszewskia411e812018-04-10 22:30:28 +020081static DEFINE_MUTEX(gpio_machine_hogs_mutex);
82static LIST_HEAD(gpio_machine_hogs);
83
Johan Hovold6d867502015-05-04 17:23:25 +020084static void gpiochip_free_hogs(struct gpio_chip *chip);
Thierry Reding959bc7b2017-11-07 19:15:59 +010085static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
Andrew Lunn39c3fd52017-12-02 18:11:04 +010086 struct lock_class_key *lock_key,
87 struct lock_class_key *request_key);
Johan Hovold6d867502015-05-04 17:23:25 +020088static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
Andy Shevchenko9411e3a2019-10-09 17:34:44 +030089static int gpiochip_irqchip_init_hw(struct gpio_chip *gpiochip);
Mika Westerberg79b804c2016-09-20 15:15:21 +030090static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip);
91static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip);
Johan Hovold6d867502015-05-04 17:23:25 +020092
Guenter Roeck159f3cd2016-03-31 08:11:30 -070093static bool gpiolib_initialized;
Johan Hovold6d867502015-05-04 17:23:25 +020094
David Brownelld2876d02008-02-04 22:28:20 -080095static inline void desc_set_label(struct gpio_desc *d, const char *label)
96{
David Brownelld2876d02008-02-04 22:28:20 -080097 d->label = label;
David Brownelld2876d02008-02-04 22:28:20 -080098}
99
Alexandre Courbot372e7222013-02-03 01:29:29 +0900100/**
Thierry Reding950d55f52017-07-24 16:57:22 +0200101 * gpio_to_desc - Convert a GPIO number to its descriptor
102 * @gpio: global GPIO number
103 *
104 * Returns:
105 * The GPIO descriptor associated with the given GPIO, or %NULL if no GPIO
106 * with the given number exists in the system.
Alexandre Courbot372e7222013-02-03 01:29:29 +0900107 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700108struct gpio_desc *gpio_to_desc(unsigned gpio)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900109{
Linus Walleijff2b1352015-10-20 11:10:38 +0200110 struct gpio_device *gdev;
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900111 unsigned long flags;
112
113 spin_lock_irqsave(&gpio_lock, flags);
114
Linus Walleijff2b1352015-10-20 11:10:38 +0200115 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100116 if (gdev->base <= gpio &&
117 gdev->base + gdev->ngpio > gpio) {
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900118 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100119 return &gdev->descs[gpio - gdev->base];
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900120 }
121 }
122
123 spin_unlock_irqrestore(&gpio_lock, flags);
124
Alexandre Courbot0e9a5ed2014-12-02 23:15:05 +0900125 if (!gpio_is_valid(gpio))
126 WARN(1, "invalid GPIO %d\n", gpio);
127
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900128 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900129}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700130EXPORT_SYMBOL_GPL(gpio_to_desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900131
132/**
Thierry Reding950d55f52017-07-24 16:57:22 +0200133 * gpiochip_get_desc - get the GPIO descriptor corresponding to the given
134 * hardware number for this chip
135 * @chip: GPIO chip
136 * @hwnum: hardware number of the GPIO for this chip
137 *
138 * Returns:
139 * A pointer to the GPIO descriptor or %ERR_PTR(-EINVAL) if no GPIO exists
140 * in the given chip for the specified hardware number.
Linus Walleijd468bf92013-09-24 11:54:38 +0200141 */
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900142struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
Bartosz Golaszewski06863622019-12-24 13:06:59 +0100143 unsigned int hwnum)
Linus Walleijd468bf92013-09-24 11:54:38 +0200144{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100145 struct gpio_device *gdev = chip->gpiodev;
146
147 if (hwnum >= gdev->ngpio)
Alexandre Courbotb7d0a282013-12-03 12:31:11 +0900148 return ERR_PTR(-EINVAL);
Linus Walleijd468bf92013-09-24 11:54:38 +0200149
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100150 return &gdev->descs[hwnum];
Linus Walleijd468bf92013-09-24 11:54:38 +0200151}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900152
153/**
Thierry Reding950d55f52017-07-24 16:57:22 +0200154 * desc_to_gpio - convert a GPIO descriptor to the integer namespace
155 * @desc: GPIO descriptor
156 *
Alexandre Courbot372e7222013-02-03 01:29:29 +0900157 * This should disappear in the future but is needed since we still
Thierry Reding950d55f52017-07-24 16:57:22 +0200158 * use GPIO numbers for error messages and sysfs nodes.
159 *
160 * Returns:
161 * The global GPIO number for the GPIO specified by its descriptor.
Alexandre Courbot372e7222013-02-03 01:29:29 +0900162 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700163int desc_to_gpio(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900164{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100165 return desc->gdev->base + (desc - &desc->gdev->descs[0]);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900166}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700167EXPORT_SYMBOL_GPL(desc_to_gpio);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900168
169
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700170/**
171 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
172 * @desc: descriptor to return the chip of
173 */
174struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900175{
Vladimir Zapolskiydd3b9a42017-12-21 18:37:30 +0200176 if (!desc || !desc->gdev)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100177 return NULL;
178 return desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900179}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700180EXPORT_SYMBOL_GPL(gpiod_to_chip);
David Brownelld2876d02008-02-04 22:28:20 -0800181
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700182/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
183static int gpiochip_find_base(int ngpio)
184{
Linus Walleijff2b1352015-10-20 11:10:38 +0200185 struct gpio_device *gdev;
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900186 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700187
Linus Walleijff2b1352015-10-20 11:10:38 +0200188 list_for_each_entry_reverse(gdev, &gpio_devices, list) {
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900189 /* found a free space? */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100190 if (gdev->base + gdev->ngpio <= base)
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900191 break;
192 else
193 /* nope, check the space right before the chip */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100194 base = gdev->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700195 }
196
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900197 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700198 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900199 return base;
200 } else {
201 pr_err("%s: cannot find free range\n", __func__);
202 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700203 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700204}
205
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700206/**
207 * gpiod_get_direction - return the current direction of a GPIO
208 * @desc: GPIO to get the direction of
209 *
Wolfram Sang94fc7302018-01-09 12:35:53 +0100210 * Returns 0 for output, 1 for input, or an error code in case of error.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700211 *
212 * This function may sleep if gpiod_cansleep() is true.
213 */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900214int gpiod_get_direction(struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300215{
Wolfram Sangd0121b82018-07-11 18:33:19 +0200216 struct gpio_chip *chip;
217 unsigned offset;
Linus Walleijd377f562019-07-16 11:11:45 +0200218 int ret;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300219
Alexandre Courbot372e7222013-02-03 01:29:29 +0900220 chip = gpiod_to_chip(desc);
221 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300222
Russell King256efae2019-12-07 16:20:18 +0000223 /*
224 * Open drain emulation using input mode may incorrectly report
225 * input here, fix that up.
226 */
227 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) &&
228 test_bit(FLAG_IS_OUT, &desc->flags))
229 return 0;
230
Mathias Nyman80b0a602012-10-24 17:25:27 +0300231 if (!chip->get_direction)
Wolfram Sangd0121b82018-07-11 18:33:19 +0200232 return -ENOTSUPP;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300233
Linus Walleijd377f562019-07-16 11:11:45 +0200234 ret = chip->get_direction(chip, offset);
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +0200235 if (ret < 0)
236 return ret;
237
238 /* GPIOF_DIR_IN or other positive, otherwise GPIOF_DIR_OUT */
239 if (ret > 0)
Linus Walleijd377f562019-07-16 11:11:45 +0200240 ret = 1;
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +0200241
242 assign_bit(FLAG_IS_OUT, &desc->flags, !ret);
243
Linus Walleijd377f562019-07-16 11:11:45 +0200244 return ret;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300245}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700246EXPORT_SYMBOL_GPL(gpiod_get_direction);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300247
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900248/*
249 * Add a new chip to the global chips list, keeping the list of chips sorted
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800250 * by range(means [base, base + ngpio - 1]) order.
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900251 *
252 * Return -EBUSY if the new chip overlaps with some other chip's integer
253 * space.
254 */
Linus Walleijff2b1352015-10-20 11:10:38 +0200255static int gpiodev_add_to_list(struct gpio_device *gdev)
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900256{
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800257 struct gpio_device *prev, *next;
Linus Walleijff2b1352015-10-20 11:10:38 +0200258
259 if (list_empty(&gpio_devices)) {
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800260 /* initial entry in list */
Linus Walleijff2b1352015-10-20 11:10:38 +0200261 list_add_tail(&gdev->list, &gpio_devices);
Sudip Mukherjeee28ecca2015-12-27 19:06:50 +0530262 return 0;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900263 }
264
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800265 next = list_entry(gpio_devices.next, struct gpio_device, list);
266 if (gdev->base + gdev->ngpio <= next->base) {
267 /* add before first entry */
268 list_add(&gdev->list, &gpio_devices);
Julien Grossholtz96098df2016-01-07 16:46:45 -0500269 return 0;
270 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900271
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800272 prev = list_entry(gpio_devices.prev, struct gpio_device, list);
273 if (prev->base + prev->ngpio <= gdev->base) {
274 /* add behind last entry */
275 list_add_tail(&gdev->list, &gpio_devices);
276 return 0;
277 }
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800278
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800279 list_for_each_entry_safe(prev, next, &gpio_devices, list) {
280 /* at the end of the list */
281 if (&next->list == &gpio_devices)
282 break;
283
284 /* add between prev and next */
285 if (prev->base + prev->ngpio <= gdev->base
286 && gdev->base + gdev->ngpio <= next->base) {
287 list_add(&gdev->list, &prev->list);
288 return 0;
289 }
290 }
291
292 dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n");
293 return -EBUSY;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900294}
295
Thierry Reding950d55f52017-07-24 16:57:22 +0200296/*
Linus Walleijf881bab02015-09-23 16:20:43 -0700297 * Convert a GPIO name to its descriptor
298 */
299static struct gpio_desc *gpio_name_to_desc(const char * const name)
300{
Linus Walleijff2b1352015-10-20 11:10:38 +0200301 struct gpio_device *gdev;
Linus Walleijf881bab02015-09-23 16:20:43 -0700302 unsigned long flags;
303
304 spin_lock_irqsave(&gpio_lock, flags);
305
Linus Walleijff2b1352015-10-20 11:10:38 +0200306 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijf881bab02015-09-23 16:20:43 -0700307 int i;
308
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100309 for (i = 0; i != gdev->ngpio; ++i) {
310 struct gpio_desc *desc = &gdev->descs[i];
Linus Walleijf881bab02015-09-23 16:20:43 -0700311
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100312 if (!desc->name || !name)
Linus Walleijf881bab02015-09-23 16:20:43 -0700313 continue;
314
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100315 if (!strcmp(desc->name, name)) {
Linus Walleijf881bab02015-09-23 16:20:43 -0700316 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100317 return desc;
Linus Walleijf881bab02015-09-23 16:20:43 -0700318 }
319 }
320 }
321
322 spin_unlock_irqrestore(&gpio_lock, flags);
323
324 return NULL;
325}
326
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200327/*
328 * Takes the names from gc->names and checks if they are all unique. If they
329 * are, they are assigned to their gpio descriptors.
330 *
Bamvor Jian Zhanged379152015-11-14 16:43:20 +0800331 * Warning if one of the names is already used for a different GPIO.
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200332 */
333static int gpiochip_set_desc_names(struct gpio_chip *gc)
334{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100335 struct gpio_device *gdev = gc->gpiodev;
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200336 int i;
337
338 if (!gc->names)
339 return 0;
340
341 /* First check all names if they are unique */
342 for (i = 0; i != gc->ngpio; ++i) {
343 struct gpio_desc *gpio;
344
345 gpio = gpio_name_to_desc(gc->names[i]);
Linus Walleijf881bab02015-09-23 16:20:43 -0700346 if (gpio)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100347 dev_warn(&gdev->dev,
Linus Walleij34ffd852015-10-20 11:31:54 +0200348 "Detected name collision for GPIO name '%s'\n",
Linus Walleijf881bab02015-09-23 16:20:43 -0700349 gc->names[i]);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200350 }
351
352 /* Then add all names to the GPIO descriptors */
353 for (i = 0; i != gc->ngpio; ++i)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100354 gdev->descs[i].name = gc->names[i];
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200355
356 return 0;
357}
358
Stephen Boyde4371f6e2018-03-23 09:34:50 -0700359static unsigned long *gpiochip_allocate_mask(struct gpio_chip *chip)
360{
361 unsigned long *p;
362
Masahiro Yamada7bdbd1ec2019-07-18 15:51:01 +0900363 p = bitmap_alloc(chip->ngpio, GFP_KERNEL);
Stephen Boyde4371f6e2018-03-23 09:34:50 -0700364 if (!p)
365 return NULL;
366
367 /* Assume by default all GPIOs are valid */
368 bitmap_fill(p, chip->ngpio);
369
370 return p;
371}
372
Linus Walleijf626d6d2019-07-17 09:10:01 +0200373static int gpiochip_alloc_valid_mask(struct gpio_chip *gc)
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700374{
Linus Walleijeb1e8bd2019-08-19 11:30:58 +0200375 if (!(of_gpio_need_valid_mask(gc) || gc->init_valid_mask))
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700376 return 0;
377
Linus Walleijf626d6d2019-07-17 09:10:01 +0200378 gc->valid_mask = gpiochip_allocate_mask(gc);
379 if (!gc->valid_mask)
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700380 return -ENOMEM;
381
382 return 0;
383}
384
Linus Walleijc9fc5af2019-08-19 10:49:04 +0200385static int gpiochip_init_valid_mask(struct gpio_chip *gc)
Ricardo Ribalda Delgadof8ec92a2018-10-05 08:52:58 +0200386{
Linus Walleijc9fc5af2019-08-19 10:49:04 +0200387 if (gc->init_valid_mask)
388 return gc->init_valid_mask(gc,
389 gc->valid_mask,
390 gc->ngpio);
Ricardo Ribalda Delgadof8ec92a2018-10-05 08:52:58 +0200391
392 return 0;
393}
394
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700395static void gpiochip_free_valid_mask(struct gpio_chip *gpiochip)
396{
Masahiro Yamada7bdbd1ec2019-07-18 15:51:01 +0900397 bitmap_free(gpiochip->valid_mask);
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700398 gpiochip->valid_mask = NULL;
399}
400
Andy Shevchenkob056ca12019-11-04 18:09:39 +0200401static int gpiochip_add_pin_ranges(struct gpio_chip *gc)
402{
403 if (gc->add_pin_ranges)
404 return gc->add_pin_ranges(gc);
405
406 return 0;
407}
408
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700409bool gpiochip_line_is_valid(const struct gpio_chip *gpiochip,
410 unsigned int offset)
411{
412 /* No mask means all valid */
413 if (likely(!gpiochip->valid_mask))
414 return true;
415 return test_bit(offset, gpiochip->valid_mask);
416}
417EXPORT_SYMBOL_GPL(gpiochip_line_is_valid);
418
Linus Walleijd7c51b42016-04-26 10:35:29 +0200419/*
420 * GPIO line handle management
421 */
422
423/**
424 * struct linehandle_state - contains the state of a userspace handle
425 * @gdev: the GPIO device the handle pertains to
426 * @label: consumer label used to tag descriptors
427 * @descs: the GPIO descriptors held by this handle
428 * @numdescs: the number of descriptors held in the descs array
429 */
430struct linehandle_state {
431 struct gpio_device *gdev;
432 const char *label;
433 struct gpio_desc *descs[GPIOHANDLES_MAX];
434 u32 numdescs;
435};
436
Lars-Peter Clausene3e847c2016-10-18 16:54:05 +0200437#define GPIOHANDLE_REQUEST_VALID_FLAGS \
438 (GPIOHANDLE_REQUEST_INPUT | \
439 GPIOHANDLE_REQUEST_OUTPUT | \
440 GPIOHANDLE_REQUEST_ACTIVE_LOW | \
Drew Fustini9225d512019-11-05 10:04:23 +0800441 GPIOHANDLE_REQUEST_BIAS_PULL_UP | \
442 GPIOHANDLE_REQUEST_BIAS_PULL_DOWN | \
Kent Gibson2148ad72019-11-05 10:04:25 +0800443 GPIOHANDLE_REQUEST_BIAS_DISABLE | \
Lars-Peter Clausene3e847c2016-10-18 16:54:05 +0200444 GPIOHANDLE_REQUEST_OPEN_DRAIN | \
445 GPIOHANDLE_REQUEST_OPEN_SOURCE)
446
Kent Gibsonb043ed72019-11-05 10:04:28 +0800447static int linehandle_validate_flags(u32 flags)
448{
449 /* Return an error if an unknown flag is set */
450 if (flags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
451 return -EINVAL;
452
453 /*
454 * Do not allow both INPUT & OUTPUT flags to be set as they are
455 * contradictory.
456 */
457 if ((flags & GPIOHANDLE_REQUEST_INPUT) &&
458 (flags & GPIOHANDLE_REQUEST_OUTPUT))
459 return -EINVAL;
460
461 /*
462 * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
463 * the hardware actually supports enabling both at the same time the
464 * electrical result would be disastrous.
465 */
466 if ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) &&
467 (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
468 return -EINVAL;
469
470 /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
471 if (!(flags & GPIOHANDLE_REQUEST_OUTPUT) &&
472 ((flags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
473 (flags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
474 return -EINVAL;
475
476 /* Bias flags only allowed for input or output mode. */
477 if (!((flags & GPIOHANDLE_REQUEST_INPUT) ||
478 (flags & GPIOHANDLE_REQUEST_OUTPUT)) &&
479 ((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) ||
480 (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP) ||
481 (flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN)))
482 return -EINVAL;
483
484 /* Only one bias flag can be set. */
485 if (((flags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
486 (flags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
487 GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
488 ((flags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
489 (flags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
490 return -EINVAL;
491
492 return 0;
493}
494
Kent Gibsone588bb12019-11-05 10:04:29 +0800495static long linehandle_set_config(struct linehandle_state *lh,
496 void __user *ip)
497{
498 struct gpiohandle_config gcnf;
499 struct gpio_desc *desc;
500 int i, ret;
501 u32 lflags;
502 unsigned long *flagsp;
503
504 if (copy_from_user(&gcnf, ip, sizeof(gcnf)))
505 return -EFAULT;
506
507 lflags = gcnf.flags;
508 ret = linehandle_validate_flags(lflags);
509 if (ret)
510 return ret;
511
512 for (i = 0; i < lh->numdescs; i++) {
513 desc = lh->descs[i];
514 flagsp = &desc->flags;
515
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +0200516 assign_bit(FLAG_ACTIVE_LOW, flagsp,
Kent Gibsone588bb12019-11-05 10:04:29 +0800517 lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW);
518
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +0200519 assign_bit(FLAG_OPEN_DRAIN, flagsp,
Kent Gibsone588bb12019-11-05 10:04:29 +0800520 lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN);
521
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +0200522 assign_bit(FLAG_OPEN_SOURCE, flagsp,
Kent Gibsone588bb12019-11-05 10:04:29 +0800523 lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE);
524
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +0200525 assign_bit(FLAG_PULL_UP, flagsp,
Kent Gibsone588bb12019-11-05 10:04:29 +0800526 lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP);
527
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +0200528 assign_bit(FLAG_PULL_DOWN, flagsp,
Kent Gibsone588bb12019-11-05 10:04:29 +0800529 lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN);
530
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +0200531 assign_bit(FLAG_BIAS_DISABLE, flagsp,
Kent Gibsone588bb12019-11-05 10:04:29 +0800532 lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE);
533
534 /*
535 * Lines have to be requested explicitly for input
536 * or output, else the line will be treated "as is".
537 */
538 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
539 int val = !!gcnf.default_values[i];
540
541 ret = gpiod_direction_output(desc, val);
542 if (ret)
543 return ret;
544 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
545 ret = gpiod_direction_input(desc);
546 if (ret)
547 return ret;
548 }
549 }
550 return 0;
551}
552
Linus Walleijd7c51b42016-04-26 10:35:29 +0200553static long linehandle_ioctl(struct file *filep, unsigned int cmd,
554 unsigned long arg)
555{
556 struct linehandle_state *lh = filep->private_data;
557 void __user *ip = (void __user *)arg;
558 struct gpiohandle_data ghd;
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +0200559 DECLARE_BITMAP(vals, GPIOHANDLES_MAX);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200560 int i;
561
562 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
Bartosz Golaszewski2b955b32018-07-16 10:34:24 +0200563 /* NOTE: It's ok to read values of output lines. */
Lukas Wunnereec1d562017-10-12 12:40:10 +0200564 int ret = gpiod_get_array_value_complex(false,
565 true,
566 lh->numdescs,
567 lh->descs,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +0200568 NULL,
Lukas Wunnereec1d562017-10-12 12:40:10 +0200569 vals);
570 if (ret)
571 return ret;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200572
Lars-Peter Clausen3eded5d2016-10-18 16:54:02 +0200573 memset(&ghd, 0, sizeof(ghd));
Lukas Wunnereec1d562017-10-12 12:40:10 +0200574 for (i = 0; i < lh->numdescs; i++)
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +0200575 ghd.values[i] = test_bit(i, vals);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200576
577 if (copy_to_user(ip, &ghd, sizeof(ghd)))
578 return -EFAULT;
579
580 return 0;
581 } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
Bartosz Golaszewskie5332d52018-07-16 10:34:23 +0200582 /*
583 * All line descriptors were created at once with the same
584 * flags so just check if the first one is really output.
585 */
586 if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags))
587 return -EPERM;
588
Linus Walleijd7c51b42016-04-26 10:35:29 +0200589 if (copy_from_user(&ghd, ip, sizeof(ghd)))
590 return -EFAULT;
591
592 /* Clamp all values to [0,1] */
593 for (i = 0; i < lh->numdescs; i++)
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +0200594 __assign_bit(i, vals, ghd.values[i]);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200595
596 /* Reuse the array setting function */
Laura Abbott30277432018-05-21 10:57:07 -0700597 return gpiod_set_array_value_complex(false,
Linus Walleijd7c51b42016-04-26 10:35:29 +0200598 true,
599 lh->numdescs,
600 lh->descs,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +0200601 NULL,
Linus Walleijd7c51b42016-04-26 10:35:29 +0200602 vals);
Kent Gibsone588bb12019-11-05 10:04:29 +0800603 } else if (cmd == GPIOHANDLE_SET_CONFIG_IOCTL) {
604 return linehandle_set_config(lh, ip);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200605 }
606 return -EINVAL;
607}
608
609#ifdef CONFIG_COMPAT
610static long linehandle_ioctl_compat(struct file *filep, unsigned int cmd,
611 unsigned long arg)
612{
613 return linehandle_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
614}
615#endif
616
617static int linehandle_release(struct inode *inode, struct file *filep)
618{
619 struct linehandle_state *lh = filep->private_data;
620 struct gpio_device *gdev = lh->gdev;
621 int i;
622
623 for (i = 0; i < lh->numdescs; i++)
624 gpiod_free(lh->descs[i]);
625 kfree(lh->label);
626 kfree(lh);
627 put_device(&gdev->dev);
628 return 0;
629}
630
631static const struct file_operations linehandle_fileops = {
632 .release = linehandle_release,
633 .owner = THIS_MODULE,
634 .llseek = noop_llseek,
635 .unlocked_ioctl = linehandle_ioctl,
636#ifdef CONFIG_COMPAT
637 .compat_ioctl = linehandle_ioctl_compat,
638#endif
639};
640
641static int linehandle_create(struct gpio_device *gdev, void __user *ip)
642{
643 struct gpiohandle_request handlereq;
644 struct linehandle_state *lh;
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200645 struct file *file;
Timur Tabiab3dbcf2018-03-29 13:29:12 -0500646 int fd, i, count = 0, ret;
Bartosz Golaszewski418ee8e2017-10-16 11:32:29 +0200647 u32 lflags;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200648
649 if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
650 return -EFAULT;
651 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
652 return -EINVAL;
653
Bartosz Golaszewski418ee8e2017-10-16 11:32:29 +0200654 lflags = handlereq.flags;
655
Kent Gibsonb043ed72019-11-05 10:04:28 +0800656 ret = linehandle_validate_flags(lflags);
657 if (ret)
658 return ret;
Kent Gibson2148ad72019-11-05 10:04:25 +0800659
Linus Walleijd7c51b42016-04-26 10:35:29 +0200660 lh = kzalloc(sizeof(*lh), GFP_KERNEL);
661 if (!lh)
662 return -ENOMEM;
663 lh->gdev = gdev;
664 get_device(&gdev->dev);
665
666 /* Make sure this is terminated */
667 handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
668 if (strlen(handlereq.consumer_label)) {
669 lh->label = kstrdup(handlereq.consumer_label,
670 GFP_KERNEL);
671 if (!lh->label) {
672 ret = -ENOMEM;
673 goto out_free_lh;
674 }
675 }
676
677 /* Request each GPIO */
678 for (i = 0; i < handlereq.lines; i++) {
679 u32 offset = handlereq.lineoffsets[i];
Bartosz Golaszewski0f41dab2019-12-24 13:07:00 +0100680 struct gpio_desc *desc = gpiochip_get_desc(gdev->chip, offset);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200681
Bartosz Golaszewski0f41dab2019-12-24 13:07:00 +0100682 if (IS_ERR(desc)) {
683 ret = PTR_ERR(desc);
Lars-Peter Clausene405f9f2016-10-18 16:54:01 +0200684 goto out_free_descs;
685 }
686
Linus Walleijd7c51b42016-04-26 10:35:29 +0200687 ret = gpiod_request(desc, lh->label);
688 if (ret)
689 goto out_free_descs;
690 lh->descs[i] = desc;
Ricardo Ribalda Delgado19a4fbf2018-09-13 15:37:04 +0200691 count = i + 1;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200692
693 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
694 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
695 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
696 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
697 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
698 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Kent Gibson2148ad72019-11-05 10:04:25 +0800699 if (lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE)
700 set_bit(FLAG_BIAS_DISABLE, &desc->flags);
Drew Fustini9225d512019-11-05 10:04:23 +0800701 if (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN)
702 set_bit(FLAG_PULL_DOWN, &desc->flags);
703 if (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)
704 set_bit(FLAG_PULL_UP, &desc->flags);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200705
Andrew Jefferye10f72b2017-11-30 14:25:24 +1030706 ret = gpiod_set_transitory(desc, false);
707 if (ret < 0)
708 goto out_free_descs;
709
Linus Walleijd7c51b42016-04-26 10:35:29 +0200710 /*
711 * Lines have to be requested explicitly for input
712 * or output, else the line will be treated "as is".
713 */
714 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
715 int val = !!handlereq.default_values[i];
716
717 ret = gpiod_direction_output(desc, val);
718 if (ret)
719 goto out_free_descs;
720 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
721 ret = gpiod_direction_input(desc);
722 if (ret)
723 goto out_free_descs;
724 }
725 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
726 offset);
727 }
Linus Walleije2f608b2016-06-18 10:56:43 +0200728 /* Let i point at the last handle */
729 i--;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200730 lh->numdescs = handlereq.lines;
731
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200732 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200733 if (fd < 0) {
734 ret = fd;
735 goto out_free_descs;
736 }
737
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200738 file = anon_inode_getfile("gpio-linehandle",
739 &linehandle_fileops,
740 lh,
741 O_RDONLY | O_CLOEXEC);
742 if (IS_ERR(file)) {
743 ret = PTR_ERR(file);
744 goto out_put_unused_fd;
745 }
746
Linus Walleijd7c51b42016-04-26 10:35:29 +0200747 handlereq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200748 if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200749 /*
750 * fput() will trigger the release() callback, so do not go onto
751 * the regular error cleanup path here.
752 */
753 fput(file);
754 put_unused_fd(fd);
755 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +0200756 }
Linus Walleijd7c51b42016-04-26 10:35:29 +0200757
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200758 fd_install(fd, file);
759
Linus Walleijd7c51b42016-04-26 10:35:29 +0200760 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
761 lh->numdescs);
762
763 return 0;
764
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200765out_put_unused_fd:
766 put_unused_fd(fd);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200767out_free_descs:
Timur Tabiab3dbcf2018-03-29 13:29:12 -0500768 for (i = 0; i < count; i++)
Linus Walleijd7c51b42016-04-26 10:35:29 +0200769 gpiod_free(lh->descs[i]);
770 kfree(lh->label);
771out_free_lh:
772 kfree(lh);
773 put_device(&gdev->dev);
774 return ret;
775}
776
Linus Walleij61f922d2016-06-02 11:30:15 +0200777/*
778 * GPIO line event management
779 */
780
781/**
782 * struct lineevent_state - contains the state of a userspace event
783 * @gdev: the GPIO device the event pertains to
784 * @label: consumer label used to tag descriptors
785 * @desc: the GPIO descriptor held by this event
786 * @eflags: the event flags this line was requested with
787 * @irq: the interrupt that trigger in response to events on this GPIO
788 * @wait: wait queue that handles blocking reads of events
789 * @events: KFIFO for the GPIO events
790 * @read_lock: mutex lock to protect reads from colliding with adding
791 * new events to the FIFO
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100792 * @timestamp: cache for the timestamp storing it between hardirq
793 * and IRQ thread, used to bring the timestamp close to the actual
794 * event
Linus Walleij61f922d2016-06-02 11:30:15 +0200795 */
796struct lineevent_state {
797 struct gpio_device *gdev;
798 const char *label;
799 struct gpio_desc *desc;
800 u32 eflags;
801 int irq;
802 wait_queue_head_t wait;
803 DECLARE_KFIFO(events, struct gpioevent_data, 16);
804 struct mutex read_lock;
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100805 u64 timestamp;
Linus Walleij61f922d2016-06-02 11:30:15 +0200806};
807
Lars-Peter Clausenac7dbb92016-10-18 16:54:06 +0200808#define GPIOEVENT_REQUEST_VALID_FLAGS \
809 (GPIOEVENT_REQUEST_RISING_EDGE | \
810 GPIOEVENT_REQUEST_FALLING_EDGE)
811
Al Viroafc9a422017-07-03 06:39:46 -0400812static __poll_t lineevent_poll(struct file *filep,
Linus Walleij61f922d2016-06-02 11:30:15 +0200813 struct poll_table_struct *wait)
814{
815 struct lineevent_state *le = filep->private_data;
Al Viroafc9a422017-07-03 06:39:46 -0400816 __poll_t events = 0;
Linus Walleij61f922d2016-06-02 11:30:15 +0200817
818 poll_wait(filep, &le->wait, wait);
819
820 if (!kfifo_is_empty(&le->events))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800821 events = EPOLLIN | EPOLLRDNORM;
Linus Walleij61f922d2016-06-02 11:30:15 +0200822
823 return events;
824}
825
826
827static ssize_t lineevent_read(struct file *filep,
828 char __user *buf,
829 size_t count,
830 loff_t *f_ps)
831{
832 struct lineevent_state *le = filep->private_data;
833 unsigned int copied;
834 int ret;
835
836 if (count < sizeof(struct gpioevent_data))
837 return -EINVAL;
838
839 do {
840 if (kfifo_is_empty(&le->events)) {
841 if (filep->f_flags & O_NONBLOCK)
842 return -EAGAIN;
843
844 ret = wait_event_interruptible(le->wait,
845 !kfifo_is_empty(&le->events));
846 if (ret)
847 return ret;
848 }
849
850 if (mutex_lock_interruptible(&le->read_lock))
851 return -ERESTARTSYS;
852 ret = kfifo_to_user(&le->events, buf, count, &copied);
853 mutex_unlock(&le->read_lock);
854
855 if (ret)
856 return ret;
857
858 /*
859 * If we couldn't read anything from the fifo (a different
860 * thread might have been faster) we either return -EAGAIN if
861 * the file descriptor is non-blocking, otherwise we go back to
862 * sleep and wait for more data to arrive.
863 */
864 if (copied == 0 && (filep->f_flags & O_NONBLOCK))
865 return -EAGAIN;
866
867 } while (copied == 0);
868
869 return copied;
870}
871
872static int lineevent_release(struct inode *inode, struct file *filep)
873{
874 struct lineevent_state *le = filep->private_data;
875 struct gpio_device *gdev = le->gdev;
876
877 free_irq(le->irq, le);
878 gpiod_free(le->desc);
879 kfree(le->label);
880 kfree(le);
881 put_device(&gdev->dev);
882 return 0;
883}
884
885static long lineevent_ioctl(struct file *filep, unsigned int cmd,
886 unsigned long arg)
887{
888 struct lineevent_state *le = filep->private_data;
889 void __user *ip = (void __user *)arg;
890 struct gpiohandle_data ghd;
891
892 /*
893 * We can get the value for an event line but not set it,
894 * because it is input by definition.
895 */
896 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
897 int val;
898
Lars-Peter Clausend82aa4a2016-10-18 16:54:04 +0200899 memset(&ghd, 0, sizeof(ghd));
900
Linus Walleij61f922d2016-06-02 11:30:15 +0200901 val = gpiod_get_value_cansleep(le->desc);
902 if (val < 0)
903 return val;
904 ghd.values[0] = val;
905
906 if (copy_to_user(ip, &ghd, sizeof(ghd)))
907 return -EFAULT;
908
909 return 0;
910 }
911 return -EINVAL;
912}
913
914#ifdef CONFIG_COMPAT
915static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd,
916 unsigned long arg)
917{
918 return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
919}
920#endif
921
922static const struct file_operations lineevent_fileops = {
923 .release = lineevent_release,
924 .read = lineevent_read,
925 .poll = lineevent_poll,
926 .owner = THIS_MODULE,
927 .llseek = noop_llseek,
928 .unlocked_ioctl = lineevent_ioctl,
929#ifdef CONFIG_COMPAT
930 .compat_ioctl = lineevent_ioctl_compat,
931#endif
932};
933
Ben Dooks33265b12016-06-17 16:03:13 +0100934static irqreturn_t lineevent_irq_thread(int irq, void *p)
Linus Walleij61f922d2016-06-02 11:30:15 +0200935{
936 struct lineevent_state *le = p;
937 struct gpioevent_data ge;
Uwe Kleine-Königfa388692018-08-20 08:32:53 +0200938 int ret;
Linus Walleij61f922d2016-06-02 11:30:15 +0200939
Linus Walleij24bd3ef2018-01-22 13:19:28 +0100940 /* Do not leak kernel stack to userspace */
941 memset(&ge, 0, sizeof(ge));
942
Bartosz Golaszewski1033be52019-01-04 11:24:20 +0100943 /*
944 * We may be running from a nested threaded interrupt in which case
945 * we didn't get the timestamp from lineevent_irq_handler().
946 */
947 if (!le->timestamp)
948 ge.timestamp = ktime_get_real_ns();
949 else
950 ge.timestamp = le->timestamp;
Linus Walleij61f922d2016-06-02 11:30:15 +0200951
Bartosz Golaszewskiad537b82017-06-23 13:45:16 +0200952 if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
953 && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
Uwe Kleine-Königfa388692018-08-20 08:32:53 +0200954 int level = gpiod_get_value_cansleep(le->desc);
Linus Walleij61f922d2016-06-02 11:30:15 +0200955 if (level)
956 /* Emit low-to-high event */
957 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
958 else
959 /* Emit high-to-low event */
960 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Uwe Kleine-Königfa388692018-08-20 08:32:53 +0200961 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200962 /* Emit low-to-high event */
963 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
Uwe Kleine-Königfa388692018-08-20 08:32:53 +0200964 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200965 /* Emit high-to-low event */
966 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Arnd Bergmannbc0207a2016-06-16 11:02:41 +0200967 } else {
968 return IRQ_NONE;
Linus Walleij61f922d2016-06-02 11:30:15 +0200969 }
970
971 ret = kfifo_put(&le->events, ge);
Saiyam Doshi2efc6bf2019-09-07 23:09:10 +0530972 if (ret)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800973 wake_up_poll(&le->wait, EPOLLIN);
Linus Walleij61f922d2016-06-02 11:30:15 +0200974
975 return IRQ_HANDLED;
976}
977
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100978static irqreturn_t lineevent_irq_handler(int irq, void *p)
979{
980 struct lineevent_state *le = p;
981
982 /*
983 * Just store the timestamp in hardirq context so we get it as
984 * close in time as possible to the actual event.
985 */
986 le->timestamp = ktime_get_real_ns();
987
988 return IRQ_WAKE_THREAD;
989}
990
Linus Walleij61f922d2016-06-02 11:30:15 +0200991static int lineevent_create(struct gpio_device *gdev, void __user *ip)
992{
993 struct gpioevent_request eventreq;
994 struct lineevent_state *le;
995 struct gpio_desc *desc;
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200996 struct file *file;
Linus Walleij61f922d2016-06-02 11:30:15 +0200997 u32 offset;
998 u32 lflags;
999 u32 eflags;
1000 int fd;
1001 int ret;
1002 int irqflags = 0;
1003
1004 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
1005 return -EFAULT;
1006
Bartosz Golaszewskibcc6d992019-09-16 11:46:23 +02001007 offset = eventreq.lineoffset;
1008 lflags = eventreq.handleflags;
1009 eflags = eventreq.eventflags;
1010
Bartosz Golaszewski45e23602019-12-24 13:07:01 +01001011 desc = gpiochip_get_desc(gdev->chip, offset);
1012 if (IS_ERR(desc))
1013 return PTR_ERR(desc);
Bartosz Golaszewskibcc6d992019-09-16 11:46:23 +02001014
1015 /* Return an error if a unknown flag is set */
1016 if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
1017 (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS))
1018 return -EINVAL;
1019
1020 /* This is just wrong: we don't look for events on output lines */
1021 if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) ||
1022 (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
1023 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
1024 return -EINVAL;
1025
Kent Gibson2148ad72019-11-05 10:04:25 +08001026 /* Only one bias flag can be set. */
1027 if (((lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE) &&
1028 (lflags & (GPIOHANDLE_REQUEST_BIAS_PULL_DOWN |
1029 GPIOHANDLE_REQUEST_BIAS_PULL_UP))) ||
1030 ((lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN) &&
1031 (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)))
1032 return -EINVAL;
1033
Linus Walleij61f922d2016-06-02 11:30:15 +02001034 le = kzalloc(sizeof(*le), GFP_KERNEL);
1035 if (!le)
1036 return -ENOMEM;
1037 le->gdev = gdev;
1038 get_device(&gdev->dev);
1039
1040 /* Make sure this is terminated */
1041 eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
1042 if (strlen(eventreq.consumer_label)) {
1043 le->label = kstrdup(eventreq.consumer_label,
1044 GFP_KERNEL);
1045 if (!le->label) {
1046 ret = -ENOMEM;
1047 goto out_free_le;
1048 }
1049 }
1050
Linus Walleij61f922d2016-06-02 11:30:15 +02001051 ret = gpiod_request(desc, le->label);
1052 if (ret)
Uwe Kleine-Königf001cc32018-04-16 13:17:53 +02001053 goto out_free_label;
Linus Walleij61f922d2016-06-02 11:30:15 +02001054 le->desc = desc;
1055 le->eflags = eflags;
1056
1057 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
1058 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
Kent Gibson2148ad72019-11-05 10:04:25 +08001059 if (lflags & GPIOHANDLE_REQUEST_BIAS_DISABLE)
1060 set_bit(FLAG_BIAS_DISABLE, &desc->flags);
Kent Gibson7b479a82019-11-05 10:04:24 +08001061 if (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_DOWN)
1062 set_bit(FLAG_PULL_DOWN, &desc->flags);
1063 if (lflags & GPIOHANDLE_REQUEST_BIAS_PULL_UP)
1064 set_bit(FLAG_PULL_UP, &desc->flags);
Linus Walleij61f922d2016-06-02 11:30:15 +02001065
1066 ret = gpiod_direction_input(desc);
1067 if (ret)
1068 goto out_free_desc;
1069
1070 le->irq = gpiod_to_irq(desc);
1071 if (le->irq <= 0) {
1072 ret = -ENODEV;
1073 goto out_free_desc;
1074 }
1075
1076 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
Michael Wu223ecaf2019-07-08 13:23:08 +08001077 irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
1078 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
Linus Walleij61f922d2016-06-02 11:30:15 +02001079 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
Michael Wu223ecaf2019-07-08 13:23:08 +08001080 irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
1081 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
Linus Walleij61f922d2016-06-02 11:30:15 +02001082 irqflags |= IRQF_ONESHOT;
Linus Walleij61f922d2016-06-02 11:30:15 +02001083
1084 INIT_KFIFO(le->events);
1085 init_waitqueue_head(&le->wait);
1086 mutex_init(&le->read_lock);
1087
1088 /* Request a thread to read the events */
1089 ret = request_threaded_irq(le->irq,
Linus Walleijd58f2bf2017-11-30 10:23:27 +01001090 lineevent_irq_handler,
Linus Walleij61f922d2016-06-02 11:30:15 +02001091 lineevent_irq_thread,
1092 irqflags,
1093 le->label,
1094 le);
1095 if (ret)
1096 goto out_free_desc;
1097
Lars-Peter Clausen953b9562016-10-24 13:59:15 +02001098 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleij61f922d2016-06-02 11:30:15 +02001099 if (fd < 0) {
1100 ret = fd;
1101 goto out_free_irq;
1102 }
1103
Lars-Peter Clausen953b9562016-10-24 13:59:15 +02001104 file = anon_inode_getfile("gpio-event",
1105 &lineevent_fileops,
1106 le,
1107 O_RDONLY | O_CLOEXEC);
1108 if (IS_ERR(file)) {
1109 ret = PTR_ERR(file);
1110 goto out_put_unused_fd;
1111 }
1112
Linus Walleij61f922d2016-06-02 11:30:15 +02001113 eventreq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +02001114 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +02001115 /*
1116 * fput() will trigger the release() callback, so do not go onto
1117 * the regular error cleanup path here.
1118 */
1119 fput(file);
1120 put_unused_fd(fd);
1121 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +02001122 }
Linus Walleij61f922d2016-06-02 11:30:15 +02001123
Lars-Peter Clausen953b9562016-10-24 13:59:15 +02001124 fd_install(fd, file);
1125
Linus Walleij61f922d2016-06-02 11:30:15 +02001126 return 0;
1127
Lars-Peter Clausen953b9562016-10-24 13:59:15 +02001128out_put_unused_fd:
1129 put_unused_fd(fd);
Linus Walleij61f922d2016-06-02 11:30:15 +02001130out_free_irq:
1131 free_irq(le->irq, le);
1132out_free_desc:
1133 gpiod_free(le->desc);
1134out_free_label:
1135 kfree(le->label);
1136out_free_le:
1137 kfree(le);
1138 put_device(&gdev->dev);
1139 return ret;
1140}
1141
Thierry Reding950d55f52017-07-24 16:57:22 +02001142/*
Linus Walleij3c702e92015-10-21 15:29:53 +02001143 * gpio_ioctl() - ioctl handler for the GPIO chardev
1144 */
1145static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1146{
1147 struct gpio_device *gdev = filp->private_data;
1148 struct gpio_chip *chip = gdev->chip;
Linus Walleij8b92e172016-05-27 14:24:04 +02001149 void __user *ip = (void __user *)arg;
Linus Walleij3c702e92015-10-21 15:29:53 +02001150
1151 /* We fail any subsequent ioctl():s when the chip is gone */
1152 if (!chip)
1153 return -ENODEV;
1154
Linus Walleij521a2ad2016-02-12 22:25:22 +01001155 /* Fill in the struct and pass to userspace */
Linus Walleij3c702e92015-10-21 15:29:53 +02001156 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
Linus Walleij521a2ad2016-02-12 22:25:22 +01001157 struct gpiochip_info chipinfo;
1158
Lars-Peter Clausen0f4bbb22016-10-18 16:54:00 +02001159 memset(&chipinfo, 0, sizeof(chipinfo));
1160
Linus Walleij3c702e92015-10-21 15:29:53 +02001161 strncpy(chipinfo.name, dev_name(&gdev->dev),
1162 sizeof(chipinfo.name));
1163 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
Linus Walleijdf4878e2016-02-12 14:48:23 +01001164 strncpy(chipinfo.label, gdev->label,
1165 sizeof(chipinfo.label));
1166 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001167 chipinfo.lines = gdev->ngpio;
Linus Walleij3c702e92015-10-21 15:29:53 +02001168 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
1169 return -EFAULT;
1170 return 0;
Linus Walleij521a2ad2016-02-12 22:25:22 +01001171 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
1172 struct gpioline_info lineinfo;
1173 struct gpio_desc *desc;
1174
1175 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
1176 return -EFAULT;
Linus Walleij521a2ad2016-02-12 22:25:22 +01001177
Bartosz Golaszewski2a2cabd2019-12-24 13:07:02 +01001178 desc = gpiochip_get_desc(chip, lineinfo.line_offset);
1179 if (IS_ERR(desc))
1180 return PTR_ERR(desc);
1181
Linus Walleij521a2ad2016-02-12 22:25:22 +01001182 if (desc->name) {
1183 strncpy(lineinfo.name, desc->name,
1184 sizeof(lineinfo.name));
1185 lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
1186 } else {
1187 lineinfo.name[0] = '\0';
1188 }
1189 if (desc->label) {
Linus Walleij214338e2016-02-25 21:01:48 +01001190 strncpy(lineinfo.consumer, desc->label,
1191 sizeof(lineinfo.consumer));
1192 lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +01001193 } else {
Linus Walleij214338e2016-02-25 21:01:48 +01001194 lineinfo.consumer[0] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +01001195 }
1196
1197 /*
1198 * Userspace only need to know that the kernel is using
1199 * this GPIO so it can't use it.
1200 */
1201 lineinfo.flags = 0;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001202 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
1203 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
1204 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
1205 test_bit(FLAG_EXPORT, &desc->flags) ||
Stefan Wahren472a61e2019-08-14 14:00:35 +03001206 test_bit(FLAG_SYSFS, &desc->flags) ||
1207 !pinctrl_gpio_can_use_line(chip->base + lineinfo.line_offset))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001208 lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001209 if (test_bit(FLAG_IS_OUT, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001210 lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001211 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001212 lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001213 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Bartosz Golaszewski2c60e6b2019-08-06 13:41:51 +02001214 lineinfo.flags |= (GPIOLINE_FLAG_OPEN_DRAIN |
1215 GPIOLINE_FLAG_IS_OUT);
Linus Walleij9d8cc892016-02-22 13:44:53 +01001216 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Bartosz Golaszewski2c60e6b2019-08-06 13:41:51 +02001217 lineinfo.flags |= (GPIOLINE_FLAG_OPEN_SOURCE |
1218 GPIOLINE_FLAG_IS_OUT);
Kent Gibson2148ad72019-11-05 10:04:25 +08001219 if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
1220 lineinfo.flags |= GPIOLINE_FLAG_BIAS_DISABLE;
Drew Fustini9225d512019-11-05 10:04:23 +08001221 if (test_bit(FLAG_PULL_DOWN, &desc->flags))
1222 lineinfo.flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN;
1223 if (test_bit(FLAG_PULL_UP, &desc->flags))
1224 lineinfo.flags |= GPIOLINE_FLAG_BIAS_PULL_UP;
Linus Walleij521a2ad2016-02-12 22:25:22 +01001225
1226 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
1227 return -EFAULT;
1228 return 0;
Linus Walleijd7c51b42016-04-26 10:35:29 +02001229 } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
1230 return linehandle_create(gdev, ip);
Linus Walleij61f922d2016-06-02 11:30:15 +02001231 } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
1232 return lineevent_create(gdev, ip);
Linus Walleij3c702e92015-10-21 15:29:53 +02001233 }
1234 return -EINVAL;
1235}
1236
Linus Walleij8b92e172016-05-27 14:24:04 +02001237#ifdef CONFIG_COMPAT
1238static long gpio_ioctl_compat(struct file *filp, unsigned int cmd,
1239 unsigned long arg)
1240{
1241 return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
1242}
1243#endif
1244
Linus Walleij3c702e92015-10-21 15:29:53 +02001245/**
1246 * gpio_chrdev_open() - open the chardev for ioctl operations
1247 * @inode: inode for this chardev
1248 * @filp: file struct for storing private data
1249 * Returns 0 on success
1250 */
1251static int gpio_chrdev_open(struct inode *inode, struct file *filp)
1252{
1253 struct gpio_device *gdev = container_of(inode->i_cdev,
1254 struct gpio_device, chrdev);
1255
1256 /* Fail on open if the backing gpiochip is gone */
Stephen Boydfb505742017-01-09 11:47:44 -08001257 if (!gdev->chip)
Linus Walleij3c702e92015-10-21 15:29:53 +02001258 return -ENODEV;
1259 get_device(&gdev->dev);
1260 filp->private_data = gdev;
Lars-Peter Clausenf4e81c52016-11-30 13:05:21 +01001261
1262 return nonseekable_open(inode, filp);
Linus Walleij3c702e92015-10-21 15:29:53 +02001263}
1264
1265/**
1266 * gpio_chrdev_release() - close chardev after ioctl operations
1267 * @inode: inode for this chardev
1268 * @filp: file struct for storing private data
1269 * Returns 0 on success
1270 */
1271static int gpio_chrdev_release(struct inode *inode, struct file *filp)
1272{
1273 struct gpio_device *gdev = container_of(inode->i_cdev,
1274 struct gpio_device, chrdev);
1275
Linus Walleij3c702e92015-10-21 15:29:53 +02001276 put_device(&gdev->dev);
1277 return 0;
1278}
1279
1280
1281static const struct file_operations gpio_fileops = {
1282 .release = gpio_chrdev_release,
1283 .open = gpio_chrdev_open,
1284 .owner = THIS_MODULE,
Lars-Peter Clausenf4e81c52016-11-30 13:05:21 +01001285 .llseek = no_llseek,
Linus Walleij3c702e92015-10-21 15:29:53 +02001286 .unlocked_ioctl = gpio_ioctl,
Linus Walleij8b92e172016-05-27 14:24:04 +02001287#ifdef CONFIG_COMPAT
1288 .compat_ioctl = gpio_ioctl_compat,
1289#endif
Linus Walleij3c702e92015-10-21 15:29:53 +02001290};
1291
Linus Walleijff2b1352015-10-20 11:10:38 +02001292static void gpiodevice_release(struct device *dev)
1293{
1294 struct gpio_device *gdev = dev_get_drvdata(dev);
1295
1296 list_del(&gdev->list);
1297 ida_simple_remove(&gpio_ida, gdev->id);
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +01001298 kfree_const(gdev->label);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001299 kfree(gdev->descs);
Linus Walleij9efd9e62016-02-09 14:27:42 +01001300 kfree(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001301}
1302
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001303static int gpiochip_setup_dev(struct gpio_device *gdev)
1304{
Linus Walleijd377f562019-07-16 11:11:45 +02001305 int ret;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001306
1307 cdev_init(&gdev->chrdev, &gpio_fileops);
1308 gdev->chrdev.owner = THIS_MODULE;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001309 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001310
Linus Walleijd377f562019-07-16 11:11:45 +02001311 ret = cdev_device_add(&gdev->chrdev, &gdev->dev);
1312 if (ret)
1313 return ret;
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001314
1315 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
1316 MAJOR(gpio_devt), gdev->id);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001317
Linus Walleijd377f562019-07-16 11:11:45 +02001318 ret = gpiochip_sysfs_register(gdev);
1319 if (ret)
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001320 goto err_remove_device;
1321
1322 /* From this point, the .release() function cleans up gpio_device */
1323 gdev->dev.release = gpiodevice_release;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001324 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
1325 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
1326 dev_name(&gdev->dev), gdev->chip->label ? : "generic");
1327
1328 return 0;
1329
1330err_remove_device:
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001331 cdev_device_del(&gdev->chrdev, &gdev->dev);
Linus Walleijd377f562019-07-16 11:11:45 +02001332 return ret;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001333}
1334
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02001335static void gpiochip_machine_hog(struct gpio_chip *chip, struct gpiod_hog *hog)
1336{
1337 struct gpio_desc *desc;
1338 int rv;
1339
1340 desc = gpiochip_get_desc(chip, hog->chip_hwnum);
1341 if (IS_ERR(desc)) {
1342 pr_err("%s: unable to get GPIO desc: %ld\n",
1343 __func__, PTR_ERR(desc));
1344 return;
1345 }
1346
Dan Carpenterba3efdf2018-05-01 10:36:39 +03001347 if (test_bit(FLAG_IS_HOGGED, &desc->flags))
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02001348 return;
1349
1350 rv = gpiod_hog(desc, hog->line_name, hog->lflags, hog->dflags);
1351 if (rv)
1352 pr_err("%s: unable to hog GPIO line (%s:%u): %d\n",
1353 __func__, chip->label, hog->chip_hwnum, rv);
1354}
1355
1356static void machine_gpiochip_add(struct gpio_chip *chip)
1357{
1358 struct gpiod_hog *hog;
1359
1360 mutex_lock(&gpio_machine_hogs_mutex);
1361
1362 list_for_each_entry(hog, &gpio_machine_hogs, list) {
1363 if (!strcmp(chip->label, hog->chip_label))
1364 gpiochip_machine_hog(chip, hog);
1365 }
1366
1367 mutex_unlock(&gpio_machine_hogs_mutex);
1368}
1369
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001370static void gpiochip_setup_devs(void)
1371{
1372 struct gpio_device *gdev;
Linus Walleijd377f562019-07-16 11:11:45 +02001373 int ret;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001374
1375 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijd377f562019-07-16 11:11:45 +02001376 ret = gpiochip_setup_dev(gdev);
1377 if (ret)
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001378 pr_err("%s: Failed to initialize gpio device (%d)\n",
Linus Walleijd377f562019-07-16 11:11:45 +02001379 dev_name(&gdev->dev), ret);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001380 }
1381}
1382
Thierry Reding959bc7b2017-11-07 19:15:59 +01001383int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001384 struct lock_class_key *lock_key,
1385 struct lock_class_key *request_key)
David Brownelld2876d02008-02-04 22:28:20 -08001386{
1387 unsigned long flags;
Linus Walleijd377f562019-07-16 11:11:45 +02001388 int ret = 0;
Linus Walleijff2b1352015-10-20 11:10:38 +02001389 unsigned i;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001390 int base = chip->base;
Linus Walleijff2b1352015-10-20 11:10:38 +02001391 struct gpio_device *gdev;
David Brownelld2876d02008-02-04 22:28:20 -08001392
Linus Walleijff2b1352015-10-20 11:10:38 +02001393 /*
1394 * First: allocate and populate the internal stat container, and
1395 * set up the struct device.
1396 */
Josh Cartwright969f07b2016-02-17 16:44:15 -06001397 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
Linus Walleijff2b1352015-10-20 11:10:38 +02001398 if (!gdev)
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001399 return -ENOMEM;
Linus Walleij3c702e92015-10-21 15:29:53 +02001400 gdev->dev.bus = &gpio_bus_type;
Linus Walleijff2b1352015-10-20 11:10:38 +02001401 gdev->chip = chip;
1402 chip->gpiodev = gdev;
1403 if (chip->parent) {
1404 gdev->dev.parent = chip->parent;
1405 gdev->dev.of_node = chip->parent->of_node;
Thierry Redingacc6e332016-07-05 14:11:14 +02001406 }
1407
Linus Walleijff2b1352015-10-20 11:10:38 +02001408#ifdef CONFIG_OF_GPIO
1409 /* If the gpiochip has an assigned OF node this takes precedence */
Thierry Redingacc6e332016-07-05 14:11:14 +02001410 if (chip->of_node)
1411 gdev->dev.of_node = chip->of_node;
Biju Das6ff04972018-08-06 10:48:01 +01001412 else
1413 chip->of_node = gdev->dev.of_node;
Linus Walleijff2b1352015-10-20 11:10:38 +02001414#endif
Thierry Redingacc6e332016-07-05 14:11:14 +02001415
Linus Walleijff2b1352015-10-20 11:10:38 +02001416 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
1417 if (gdev->id < 0) {
Linus Walleijd377f562019-07-16 11:11:45 +02001418 ret = gdev->id;
Linus Walleijff2b1352015-10-20 11:10:38 +02001419 goto err_free_gdev;
1420 }
Geert Uytterhoevenddd88912019-11-27 09:42:47 +01001421 dev_set_name(&gdev->dev, GPIOCHIP_NAME "%d", gdev->id);
Linus Walleijff2b1352015-10-20 11:10:38 +02001422 device_initialize(&gdev->dev);
1423 dev_set_drvdata(&gdev->dev, gdev);
1424 if (chip->parent && chip->parent->driver)
1425 gdev->owner = chip->parent->driver->owner;
1426 else if (chip->owner)
1427 /* TODO: remove chip->owner */
1428 gdev->owner = chip->owner;
1429 else
1430 gdev->owner = THIS_MODULE;
David Brownelld2876d02008-02-04 22:28:20 -08001431
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001432 gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001433 if (!gdev->descs) {
Linus Walleijd377f562019-07-16 11:11:45 +02001434 ret = -ENOMEM;
Vladimir Zapolskiya05a1402018-11-02 15:39:43 +02001435 goto err_free_ida;
Linus Walleijff2b1352015-10-20 11:10:38 +02001436 }
1437
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001438 if (chip->ngpio == 0) {
1439 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
Linus Walleijd377f562019-07-16 11:11:45 +02001440 ret = -EINVAL;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001441 goto err_free_descs;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001442 }
Linus Walleijdf4878e2016-02-12 14:48:23 +01001443
Laura Abbott30277432018-05-21 10:57:07 -07001444 if (chip->ngpio > FASTPATH_NGPIO)
1445 chip_warn(chip, "line cnt %u is greater than fast path cnt %u\n",
Enrico Weigelt, metux IT consult2ddac5a2020-01-04 20:43:34 +01001446 chip->ngpio, FASTPATH_NGPIO);
Laura Abbott30277432018-05-21 10:57:07 -07001447
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +01001448 gdev->label = kstrdup_const(chip->label ?: "unknown", GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001449 if (!gdev->label) {
Linus Walleijd377f562019-07-16 11:11:45 +02001450 ret = -ENOMEM;
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001451 goto err_free_descs;
Linus Walleijdf4878e2016-02-12 14:48:23 +01001452 }
1453
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001454 gdev->ngpio = chip->ngpio;
Linus Walleij43c54ec2016-02-11 11:37:48 +01001455 gdev->data = data;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001456
David Brownelld2876d02008-02-04 22:28:20 -08001457 spin_lock_irqsave(&gpio_lock, flags);
1458
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001459 /*
1460 * TODO: this allocates a Linux GPIO number base in the global
1461 * GPIO numberspace for this chip. In the long run we want to
1462 * get *rid* of this numberspace and use only descriptors, but
1463 * it may be a pipe dream. It will not happen before we get rid
1464 * of the sysfs interface anyways.
1465 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001466 if (base < 0) {
1467 base = gpiochip_find_base(chip->ngpio);
1468 if (base < 0) {
Linus Walleijd377f562019-07-16 11:11:45 +02001469 ret = base;
Johan Hovold225fce82015-01-12 17:12:25 +01001470 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001471 goto err_free_label;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001472 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001473 /*
1474 * TODO: it should not be necessary to reflect the assigned
1475 * base outside of the GPIO subsystem. Go over drivers and
1476 * see if anyone makes use of this, else drop this and assign
1477 * a poison instead.
1478 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001479 chip->base = base;
1480 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001481 gdev->base = base;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001482
Linus Walleijd377f562019-07-16 11:11:45 +02001483 ret = gpiodev_add_to_list(gdev);
1484 if (ret) {
Johan Hovold05aa5202015-01-12 17:12:26 +01001485 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001486 goto err_free_label;
Johan Hovold05aa5202015-01-12 17:12:26 +01001487 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001488
Linus Walleij545ebd92016-05-30 17:11:59 +02001489 spin_unlock_irqrestore(&gpio_lock, flags);
1490
Ricardo Ribalda Delgado767cd172018-10-12 08:11:36 +02001491 for (i = 0; i < chip->ngpio; i++)
1492 gdev->descs[i].gdev = gdev;
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001493
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301494#ifdef CONFIG_PINCTRL
Linus Walleij20ec3e32016-02-11 11:03:06 +01001495 INIT_LIST_HEAD(&gdev->pin_ranges);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301496#endif
1497
Linus Walleijd377f562019-07-16 11:11:45 +02001498 ret = gpiochip_set_desc_names(chip);
1499 if (ret)
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001500 goto err_remove_from_list;
1501
Linus Walleij151a4102019-09-05 11:40:54 +02001502 ret = gpiochip_alloc_valid_mask(chip);
Linus Walleijd377f562019-07-16 11:11:45 +02001503 if (ret)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001504 goto err_remove_from_list;
1505
Linus Walleijd377f562019-07-16 11:11:45 +02001506 ret = of_gpiochip_add(chip);
1507 if (ret)
Linus Walleij48057ed2019-08-20 10:05:27 +02001508 goto err_free_gpiochip_mask;
Tomeu Vizoso28355f82015-07-14 10:29:54 +02001509
Linus Walleijd377f562019-07-16 11:11:45 +02001510 ret = gpiochip_init_valid_mask(chip);
1511 if (ret)
Geert Uytterhoeven35779892019-04-24 15:59:33 +02001512 goto err_remove_of_chip;
Ricardo Ribalda Delgadof8ec92a2018-10-05 08:52:58 +02001513
Ricardo Ribalda Delgado3edfb7b2018-10-05 08:53:00 +02001514 for (i = 0; i < chip->ngpio; i++) {
1515 struct gpio_desc *desc = &gdev->descs[i];
1516
Chris Packhamd95da992019-07-08 08:35:58 +12001517 if (chip->get_direction && gpiochip_line_is_valid(chip, i)) {
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +02001518 assign_bit(FLAG_IS_OUT,
1519 &desc->flags, !chip->get_direction(chip, i));
Chris Packhamd95da992019-07-08 08:35:58 +12001520 } else {
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +02001521 assign_bit(FLAG_IS_OUT,
1522 &desc->flags, !chip->direction_input);
Chris Packhamd95da992019-07-08 08:35:58 +12001523 }
Ricardo Ribalda Delgado3edfb7b2018-10-05 08:53:00 +02001524 }
1525
Andy Shevchenkob056ca12019-11-04 18:09:39 +02001526 ret = gpiochip_add_pin_ranges(chip);
1527 if (ret)
1528 goto err_remove_of_chip;
1529
Mika Westerberg664e3e52014-01-08 12:40:54 +02001530 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001531
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02001532 machine_gpiochip_add(chip);
1533
Linus Walleij504369c2019-10-30 13:29:14 +01001534 ret = gpiochip_irqchip_init_valid_mask(chip);
Andy Shevchenko9411e3a2019-10-09 17:34:44 +03001535 if (ret)
1536 goto err_remove_acpi_chip;
1537
Linus Walleij504369c2019-10-30 13:29:14 +01001538 ret = gpiochip_irqchip_init_hw(chip);
Linus Walleijfbdf8d42019-09-06 12:05:35 +02001539 if (ret)
Linus Walleij48057ed2019-08-20 10:05:27 +02001540 goto err_remove_acpi_chip;
1541
Linus Walleijfbdf8d42019-09-06 12:05:35 +02001542 ret = gpiochip_add_irqchip(chip, lock_key, request_key);
1543 if (ret)
Linus Walleij48057ed2019-08-20 10:05:27 +02001544 goto err_remove_irqchip_mask;
1545
Linus Walleij3c702e92015-10-21 15:29:53 +02001546 /*
1547 * By first adding the chardev, and then adding the device,
1548 * we get a device node entry in sysfs under
1549 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1550 * coldplug of device nodes and other udev business.
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001551 * We can do this only if gpiolib has been initialized.
1552 * Otherwise, defer until later.
Linus Walleij3c702e92015-10-21 15:29:53 +02001553 */
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001554 if (gpiolib_initialized) {
Linus Walleijd377f562019-07-16 11:11:45 +02001555 ret = gpiochip_setup_dev(gdev);
1556 if (ret)
Linus Walleij48057ed2019-08-20 10:05:27 +02001557 goto err_remove_irqchip;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001558 }
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001559 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001560
Linus Walleij48057ed2019-08-20 10:05:27 +02001561err_remove_irqchip:
1562 gpiochip_irqchip_remove(chip);
1563err_remove_irqchip_mask:
1564 gpiochip_irqchip_free_valid_mask(chip);
Geert Uytterhoeven35779892019-04-24 15:59:33 +02001565err_remove_acpi_chip:
Johan Hovold225fce82015-01-12 17:12:25 +01001566 acpi_gpiochip_remove(chip);
Geert Uytterhoeven35779892019-04-24 15:59:33 +02001567err_remove_of_chip:
Johan Hovold6d867502015-05-04 17:23:25 +02001568 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +01001569 of_gpiochip_remove(chip);
Geert Uytterhoeven35779892019-04-24 15:59:33 +02001570err_free_gpiochip_mask:
Andy Shevchenko2f4133b2019-11-05 20:06:54 +02001571 gpiochip_remove_pin_ranges(chip);
Stephen Boyd726cb3b2018-03-23 09:34:52 -07001572 gpiochip_free_valid_mask(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001573err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +01001574 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001575 list_del(&gdev->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001576 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001577err_free_label:
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +01001578 kfree_const(gdev->label);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001579err_free_descs:
1580 kfree(gdev->descs);
Vladimir Zapolskiya05a1402018-11-02 15:39:43 +02001581err_free_ida:
Linus Walleijff2b1352015-10-20 11:10:38 +02001582 ida_simple_remove(&gpio_ida, gdev->id);
Vladimir Zapolskiya05a1402018-11-02 15:39:43 +02001583err_free_gdev:
David Brownelld2876d02008-02-04 22:28:20 -08001584 /* failures here can mean systems won't boot... */
Marcel Ziswiler1777fc92018-07-20 09:54:49 +02001585 pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__,
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001586 gdev->base, gdev->base + gdev->ngpio - 1,
Linus Walleijd377f562019-07-16 11:11:45 +02001587 chip->label ? : "generic", ret);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001588 kfree(gdev);
Linus Walleijd377f562019-07-16 11:11:45 +02001589 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08001590}
Thierry Reding959bc7b2017-11-07 19:15:59 +01001591EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);
David Brownelld2876d02008-02-04 22:28:20 -08001592
1593/**
Linus Walleij43c54ec2016-02-11 11:37:48 +01001594 * gpiochip_get_data() - get per-subdriver data for the chip
Thierry Reding950d55f52017-07-24 16:57:22 +02001595 * @chip: GPIO chip
1596 *
1597 * Returns:
1598 * The per-subdriver data for the chip.
Linus Walleij43c54ec2016-02-11 11:37:48 +01001599 */
1600void *gpiochip_get_data(struct gpio_chip *chip)
1601{
1602 return chip->gpiodev->data;
1603}
1604EXPORT_SYMBOL_GPL(gpiochip_get_data);
1605
1606/**
David Brownelld2876d02008-02-04 22:28:20 -08001607 * gpiochip_remove() - unregister a gpio_chip
1608 * @chip: the chip to unregister
1609 *
1610 * A gpio_chip with any GPIOs still requested may not be removed.
1611 */
abdoulaye berthee1db1702014-07-05 18:28:50 +02001612void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -08001613{
Linus Walleijff2b1352015-10-20 11:10:38 +02001614 struct gpio_device *gdev = chip->gpiodev;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001615 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001616 unsigned long flags;
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001617 unsigned i;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001618 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -08001619
Linus Walleijff2b1352015-10-20 11:10:38 +02001620 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
Linus Walleijafbc4f32016-02-09 13:21:06 +01001621 gpiochip_sysfs_unregister(gdev);
Geert Uytterhoeven5018ada2016-12-19 18:29:23 +01001622 gpiochip_free_hogs(chip);
Bamvor Jian Zhangbd203bd2016-02-20 13:13:19 +08001623 /* Numb the device, cancelling all outstanding operations */
1624 gdev->chip = NULL;
Johan Hovold00acc3d2015-01-12 17:12:27 +01001625 gpiochip_irqchip_remove(chip);
Mika Westerberg6072b9d2014-03-10 14:54:53 +02001626 acpi_gpiochip_remove(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001627 of_gpiochip_remove(chip);
Andy Shevchenko2f4133b2019-11-05 20:06:54 +02001628 gpiochip_remove_pin_ranges(chip);
Stephen Boyd726cb3b2018-03-23 09:34:52 -07001629 gpiochip_free_valid_mask(chip);
Linus Walleij43c54ec2016-02-11 11:37:48 +01001630 /*
1631 * We accept no more calls into the driver from this point, so
1632 * NULL the driver data pointer
1633 */
1634 gdev->data = NULL;
Anton Vorontsov391c9702010-06-08 07:48:17 -06001635
Johan Hovold6798aca2015-01-12 17:12:28 +01001636 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001637 for (i = 0; i < gdev->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001638 desc = &gdev->descs[i];
Johan Hovoldfab28b82015-05-04 17:10:27 +02001639 if (test_bit(FLAG_REQUESTED, &desc->flags))
1640 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -08001641 }
David Brownelld2876d02008-02-04 22:28:20 -08001642 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001643
Johan Hovoldfab28b82015-05-04 17:10:27 +02001644 if (requested)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001645 dev_crit(&gdev->dev,
Linus Walleij58383c782015-11-04 09:56:26 +01001646 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +02001647
Linus Walleijff2b1352015-10-20 11:10:38 +02001648 /*
1649 * The gpiochip side puts its use of the device to rest here:
1650 * if there are no userspace clients, the chardev and device will
1651 * be removed, else it will be dangling until the last user is
1652 * gone.
1653 */
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001654 cdev_device_del(&gdev->chrdev, &gdev->dev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001655 put_device(&gdev->dev);
David Brownelld2876d02008-02-04 22:28:20 -08001656}
1657EXPORT_SYMBOL_GPL(gpiochip_remove);
1658
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301659static void devm_gpio_chip_release(struct device *dev, void *res)
1660{
1661 struct gpio_chip *chip = *(struct gpio_chip **)res;
1662
1663 gpiochip_remove(chip);
1664}
1665
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301666/**
Geert Uytterhoeven51158412019-11-20 15:37:45 +01001667 * devm_gpiochip_add_data() - Resource managed gpiochip_add_data()
Uwe Kleine-König3925b90f2018-10-08 11:34:03 +02001668 * @dev: pointer to the device that gpio_chip belongs to.
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301669 * @chip: the chip to register, with chip->base initialized
Thierry Reding950d55f52017-07-24 16:57:22 +02001670 * @data: driver-private data associated with this chip
1671 *
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301672 * Context: potentially before irqs will work
1673 *
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301674 * The gpio chip automatically be released when the device is unbound.
Thierry Reding950d55f52017-07-24 16:57:22 +02001675 *
1676 * Returns:
1677 * A negative errno if the chip can't be registered, such as because the
1678 * chip->base is invalid or already associated with a different chip.
1679 * Otherwise it returns zero as a success code.
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301680 */
1681int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
1682 void *data)
1683{
1684 struct gpio_chip **ptr;
1685 int ret;
1686
1687 ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
1688 GFP_KERNEL);
1689 if (!ptr)
1690 return -ENOMEM;
1691
1692 ret = gpiochip_add_data(chip, data);
1693 if (ret < 0) {
1694 devres_free(ptr);
1695 return ret;
1696 }
1697
1698 *ptr = chip;
1699 devres_add(dev, ptr);
1700
1701 return 0;
1702}
1703EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
1704
1705/**
Grant Likely594fa262010-06-08 07:48:16 -06001706 * gpiochip_find() - iterator for locating a specific gpio_chip
1707 * @data: data to pass to match function
Thierry Reding950d55f52017-07-24 16:57:22 +02001708 * @match: Callback function to check gpio_chip
Grant Likely594fa262010-06-08 07:48:16 -06001709 *
1710 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1711 * determined by a user supplied @match callback. The callback should return
1712 * 0 if the device doesn't match and non-zero if it does. If the callback is
1713 * non-zero, this function will return to the caller and not iterate over any
1714 * more gpio_chips.
1715 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001716struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001717 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001718 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001719{
Linus Walleijff2b1352015-10-20 11:10:38 +02001720 struct gpio_device *gdev;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001721 struct gpio_chip *chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -06001722 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001723
1724 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001725 list_for_each_entry(gdev, &gpio_devices, list)
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001726 if (gdev->chip && match(gdev->chip, data)) {
1727 chip = gdev->chip;
Grant Likely594fa262010-06-08 07:48:16 -06001728 break;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001729 }
Linus Walleijff2b1352015-10-20 11:10:38 +02001730
Grant Likely594fa262010-06-08 07:48:16 -06001731 spin_unlock_irqrestore(&gpio_lock, flags);
1732
1733 return chip;
1734}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001735EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001736
Alexandre Courbot79697ef2013-11-16 21:39:32 +09001737static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1738{
1739 const char *name = data;
1740
1741 return !strcmp(chip->label, name);
1742}
1743
1744static struct gpio_chip *find_chip_by_name(const char *name)
1745{
1746 return gpiochip_find((void *)name, gpiochip_match_name);
1747}
1748
Linus Walleij14250522014-03-25 10:40:18 +01001749#ifdef CONFIG_GPIOLIB_IRQCHIP
1750
1751/*
1752 * The following is irqchip helper code for gpiochips.
1753 */
1754
Andy Shevchenko9411e3a2019-10-09 17:34:44 +03001755static int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
1756{
1757 struct gpio_irq_chip *girq = &gc->irq;
1758
1759 if (!girq->init_hw)
1760 return 0;
1761
1762 return girq->init_hw(gc);
1763}
1764
Linus Walleij5fbe5b52019-09-04 16:01:04 +02001765static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001766{
Linus Walleij5fbe5b52019-09-04 16:01:04 +02001767 struct gpio_irq_chip *girq = &gc->irq;
1768
1769 if (!girq->init_valid_mask)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001770 return 0;
1771
Linus Walleij5fbe5b52019-09-04 16:01:04 +02001772 girq->valid_mask = gpiochip_allocate_mask(gc);
1773 if (!girq->valid_mask)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001774 return -ENOMEM;
1775
Linus Walleij5fbe5b52019-09-04 16:01:04 +02001776 girq->init_valid_mask(gc, girq->valid_mask, gc->ngpio);
1777
Mika Westerberg79b804c2016-09-20 15:15:21 +03001778 return 0;
1779}
1780
1781static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1782{
Masahiro Yamada7bdbd1ec2019-07-18 15:51:01 +09001783 bitmap_free(gpiochip->irq.valid_mask);
Thierry Redingdc7b0382017-11-07 19:15:52 +01001784 gpiochip->irq.valid_mask = NULL;
Mika Westerberg79b804c2016-09-20 15:15:21 +03001785}
1786
Stephen Boyd64ff2c82018-01-09 17:58:46 -08001787bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
1788 unsigned int offset)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001789{
Stephen Boyd726cb3b2018-03-23 09:34:52 -07001790 if (!gpiochip_line_is_valid(gpiochip, offset))
1791 return false;
Mika Westerberg79b804c2016-09-20 15:15:21 +03001792 /* No mask means all valid */
Thierry Redingdc7b0382017-11-07 19:15:52 +01001793 if (likely(!gpiochip->irq.valid_mask))
Mika Westerberg79b804c2016-09-20 15:15:21 +03001794 return true;
Thierry Redingdc7b0382017-11-07 19:15:52 +01001795 return test_bit(offset, gpiochip->irq.valid_mask);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001796}
Stephen Boyd64ff2c82018-01-09 17:58:46 -08001797EXPORT_SYMBOL_GPL(gpiochip_irqchip_irq_valid);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001798
Linus Walleij14250522014-03-25 10:40:18 +01001799/**
Linus Walleijd245b3f2016-11-24 10:57:25 +01001800 * gpiochip_set_cascaded_irqchip() - connects a cascaded irqchip to a gpiochip
Linus Walleij4892d3a2019-06-14 10:12:26 +02001801 * @gc: the gpiochip to set the irqchip chain to
Linus Walleij14250522014-03-25 10:40:18 +01001802 * @parent_irq: the irq number corresponding to the parent IRQ for this
1803 * chained irqchip
1804 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001805 * coming out of the gpiochip. If the interrupt is nested rather than
1806 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +01001807 */
Linus Walleij4892d3a2019-06-14 10:12:26 +02001808static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gc,
Thierry Reding6f793092017-04-03 18:05:21 +02001809 unsigned int parent_irq,
Linus Walleijd245b3f2016-11-24 10:57:25 +01001810 irq_flow_handler_t parent_handler)
Linus Walleij14250522014-03-25 10:40:18 +01001811{
Linus Walleij4892d3a2019-06-14 10:12:26 +02001812 struct gpio_irq_chip *girq = &gc->irq;
1813 struct device *dev = &gc->gpiodev->dev;
1814
1815 if (!girq->domain) {
1816 chip_err(gc, "called %s before setting up irqchip\n",
Linus Walleij83141a72014-09-26 13:50:12 +02001817 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +02001818 return;
1819 }
1820
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001821 if (parent_handler) {
Linus Walleij4892d3a2019-06-14 10:12:26 +02001822 if (gc->can_sleep) {
1823 chip_err(gc,
Andy Shevchenkob1911712018-07-03 03:39:03 +03001824 "you cannot have chained interrupts on a chip that may sleep\n");
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001825 return;
1826 }
Linus Walleij4892d3a2019-06-14 10:12:26 +02001827 girq->parents = devm_kcalloc(dev, 1,
1828 sizeof(*girq->parents),
1829 GFP_KERNEL);
1830 if (!girq->parents) {
1831 chip_err(gc, "out of memory allocating parent IRQ\n");
1832 return;
1833 }
1834 girq->parents[0] = parent_irq;
1835 girq->num_parents = 1;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001836 /*
1837 * The parent irqchip is already using the chip_data for this
1838 * irqchip, so our callbacks simply use the handler_data.
1839 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +02001840 irq_set_chained_handler_and_data(parent_irq, parent_handler,
Linus Walleij4892d3a2019-06-14 10:12:26 +02001841 gc);
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001842 }
Linus Walleij14250522014-03-25 10:40:18 +01001843}
Linus Walleijd245b3f2016-11-24 10:57:25 +01001844
1845/**
1846 * gpiochip_set_chained_irqchip() - connects a chained irqchip to a gpiochip
1847 * @gpiochip: the gpiochip to set the irqchip chain to
1848 * @irqchip: the irqchip to chain to the gpiochip
1849 * @parent_irq: the irq number corresponding to the parent IRQ for this
1850 * chained irqchip
1851 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Stephen Boyd40f5ff42018-10-08 09:32:16 -07001852 * coming out of the gpiochip.
Linus Walleijd245b3f2016-11-24 10:57:25 +01001853 */
1854void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
1855 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001856 unsigned int parent_irq,
Linus Walleijd245b3f2016-11-24 10:57:25 +01001857 irq_flow_handler_t parent_handler)
1858{
Thierry Reding60ed54c2017-11-07 19:15:57 +01001859 if (gpiochip->irq.threaded) {
1860 chip_err(gpiochip, "tried to chain a threaded gpiochip\n");
1861 return;
1862 }
1863
Stephen Boyd3c1f6b22018-10-08 09:32:15 -07001864 gpiochip_set_cascaded_irqchip(gpiochip, parent_irq, parent_handler);
Linus Walleijd245b3f2016-11-24 10:57:25 +01001865}
Linus Walleij14250522014-03-25 10:40:18 +01001866EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
1867
1868/**
Linus Walleijd245b3f2016-11-24 10:57:25 +01001869 * gpiochip_set_nested_irqchip() - connects a nested irqchip to a gpiochip
1870 * @gpiochip: the gpiochip to set the irqchip nested handler to
1871 * @irqchip: the irqchip to nest to the gpiochip
1872 * @parent_irq: the irq number corresponding to the parent IRQ for this
1873 * nested irqchip
1874 */
1875void gpiochip_set_nested_irqchip(struct gpio_chip *gpiochip,
1876 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001877 unsigned int parent_irq)
Linus Walleijd245b3f2016-11-24 10:57:25 +01001878{
Stephen Boyd3c1f6b22018-10-08 09:32:15 -07001879 gpiochip_set_cascaded_irqchip(gpiochip, parent_irq, NULL);
Linus Walleijd245b3f2016-11-24 10:57:25 +01001880}
1881EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip);
1882
Linus Walleijfdd61a02019-08-08 14:32:37 +02001883#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1884
1885/**
1886 * gpiochip_set_hierarchical_irqchip() - connects a hierarchical irqchip
1887 * to a gpiochip
1888 * @gc: the gpiochip to set the irqchip hierarchical handler to
1889 * @irqchip: the irqchip to handle this level of the hierarchy, the interrupt
1890 * will then percolate up to the parent
1891 */
1892static void gpiochip_set_hierarchical_irqchip(struct gpio_chip *gc,
1893 struct irq_chip *irqchip)
1894{
1895 /* DT will deal with mapping each IRQ as we go along */
1896 if (is_of_node(gc->irq.fwnode))
1897 return;
1898
1899 /*
1900 * This is for legacy and boardfile "irqchip" fwnodes: allocate
1901 * irqs upfront instead of dynamically since we don't have the
1902 * dynamic type of allocation that hardware description languages
1903 * provide. Once all GPIO drivers using board files are gone from
1904 * the kernel we can delete this code, but for a transitional period
1905 * it is necessary to keep this around.
1906 */
1907 if (is_fwnode_irqchip(gc->irq.fwnode)) {
1908 int i;
1909 int ret;
1910
1911 for (i = 0; i < gc->ngpio; i++) {
1912 struct irq_fwspec fwspec;
1913 unsigned int parent_hwirq;
1914 unsigned int parent_type;
1915 struct gpio_irq_chip *girq = &gc->irq;
1916
1917 /*
1918 * We call the child to parent translation function
1919 * only to check if the child IRQ is valid or not.
1920 * Just pick the rising edge type here as that is what
1921 * we likely need to support.
1922 */
1923 ret = girq->child_to_parent_hwirq(gc, i,
1924 IRQ_TYPE_EDGE_RISING,
1925 &parent_hwirq,
1926 &parent_type);
1927 if (ret) {
1928 chip_err(gc, "skip set-up on hwirq %d\n",
1929 i);
1930 continue;
1931 }
1932
1933 fwspec.fwnode = gc->irq.fwnode;
1934 /* This is the hwirq for the GPIO line side of things */
1935 fwspec.param[0] = girq->child_offset_to_irq(gc, i);
1936 /* Just pick something */
1937 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
1938 fwspec.param_count = 2;
1939 ret = __irq_domain_alloc_irqs(gc->irq.domain,
1940 /* just pick something */
1941 -1,
1942 1,
1943 NUMA_NO_NODE,
1944 &fwspec,
1945 false,
1946 NULL);
1947 if (ret < 0) {
1948 chip_err(gc,
1949 "can not allocate irq for GPIO line %d parent hwirq %d in hierarchy domain: %d\n",
1950 i, parent_hwirq,
1951 ret);
1952 }
1953 }
1954 }
1955
1956 chip_err(gc, "%s unknown fwnode type proceed anyway\n", __func__);
1957
1958 return;
1959}
1960
1961static int gpiochip_hierarchy_irq_domain_translate(struct irq_domain *d,
1962 struct irq_fwspec *fwspec,
1963 unsigned long *hwirq,
1964 unsigned int *type)
1965{
1966 /* We support standard DT translation */
1967 if (is_of_node(fwspec->fwnode) && fwspec->param_count == 2) {
1968 return irq_domain_translate_twocell(d, fwspec, hwirq, type);
1969 }
1970
1971 /* This is for board files and others not using DT */
1972 if (is_fwnode_irqchip(fwspec->fwnode)) {
1973 int ret;
1974
1975 ret = irq_domain_translate_twocell(d, fwspec, hwirq, type);
1976 if (ret)
1977 return ret;
1978 WARN_ON(*type == IRQ_TYPE_NONE);
1979 return 0;
1980 }
1981 return -EINVAL;
1982}
1983
1984static int gpiochip_hierarchy_irq_domain_alloc(struct irq_domain *d,
1985 unsigned int irq,
1986 unsigned int nr_irqs,
1987 void *data)
1988{
1989 struct gpio_chip *gc = d->host_data;
1990 irq_hw_number_t hwirq;
1991 unsigned int type = IRQ_TYPE_NONE;
1992 struct irq_fwspec *fwspec = data;
Kevin Hao24258762020-01-14 16:28:19 +08001993 void *parent_arg;
Linus Walleijfdd61a02019-08-08 14:32:37 +02001994 unsigned int parent_hwirq;
1995 unsigned int parent_type;
1996 struct gpio_irq_chip *girq = &gc->irq;
1997 int ret;
1998
1999 /*
2000 * The nr_irqs parameter is always one except for PCI multi-MSI
2001 * so this should not happen.
2002 */
2003 WARN_ON(nr_irqs != 1);
2004
2005 ret = gc->irq.child_irq_domain_ops.translate(d, fwspec, &hwirq, &type);
2006 if (ret)
2007 return ret;
2008
2009 chip_info(gc, "allocate IRQ %d, hwirq %lu\n", irq, hwirq);
2010
2011 ret = girq->child_to_parent_hwirq(gc, hwirq, type,
2012 &parent_hwirq, &parent_type);
2013 if (ret) {
2014 chip_err(gc, "can't look up hwirq %lu\n", hwirq);
2015 return ret;
2016 }
2017 chip_info(gc, "found parent hwirq %u\n", parent_hwirq);
2018
2019 /*
2020 * We set handle_bad_irq because the .set_type() should
2021 * always be invoked and set the right type of handler.
2022 */
2023 irq_domain_set_info(d,
2024 irq,
2025 hwirq,
2026 gc->irq.chip,
2027 gc,
2028 girq->handler,
2029 NULL, NULL);
2030 irq_set_probe(irq);
2031
Linus Walleijfdd61a02019-08-08 14:32:37 +02002032 /* This parent only handles asserted level IRQs */
Kevin Hao24258762020-01-14 16:28:19 +08002033 parent_arg = girq->populate_parent_alloc_arg(gc, parent_hwirq, parent_type);
2034 if (!parent_arg)
2035 return -ENOMEM;
2036
Linus Walleijfdd61a02019-08-08 14:32:37 +02002037 chip_info(gc, "alloc_irqs_parent for %d parent hwirq %d\n",
2038 irq, parent_hwirq);
Stephen Boydc34f6dc2020-01-14 15:11:03 -08002039 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key);
Kevin Hao24258762020-01-14 16:28:19 +08002040 ret = irq_domain_alloc_irqs_parent(d, irq, 1, parent_arg);
Kevin Hao880b7cf2020-01-14 16:28:20 +08002041 /*
2042 * If the parent irqdomain is msi, the interrupts have already
2043 * been allocated, so the EEXIST is good.
2044 */
2045 if (irq_domain_is_msi(d->parent) && (ret == -EEXIST))
2046 ret = 0;
Linus Walleijfdd61a02019-08-08 14:32:37 +02002047 if (ret)
2048 chip_err(gc,
2049 "failed to allocate parent hwirq %d for hwirq %lu\n",
2050 parent_hwirq, hwirq);
2051
Kevin Hao24258762020-01-14 16:28:19 +08002052 kfree(parent_arg);
Linus Walleijfdd61a02019-08-08 14:32:37 +02002053 return ret;
2054}
2055
2056static unsigned int gpiochip_child_offset_to_irq_noop(struct gpio_chip *chip,
2057 unsigned int offset)
2058{
2059 return offset;
2060}
2061
2062static void gpiochip_hierarchy_setup_domain_ops(struct irq_domain_ops *ops)
2063{
2064 ops->activate = gpiochip_irq_domain_activate;
2065 ops->deactivate = gpiochip_irq_domain_deactivate;
2066 ops->alloc = gpiochip_hierarchy_irq_domain_alloc;
2067 ops->free = irq_domain_free_irqs_common;
2068
2069 /*
2070 * We only allow overriding the translate() function for
2071 * hierarchical chips, and this should only be done if the user
2072 * really need something other than 1:1 translation.
2073 */
2074 if (!ops->translate)
2075 ops->translate = gpiochip_hierarchy_irq_domain_translate;
2076}
2077
2078static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
2079{
2080 if (!gc->irq.child_to_parent_hwirq ||
2081 !gc->irq.fwnode) {
2082 chip_err(gc, "missing irqdomain vital data\n");
2083 return -EINVAL;
2084 }
2085
2086 if (!gc->irq.child_offset_to_irq)
2087 gc->irq.child_offset_to_irq = gpiochip_child_offset_to_irq_noop;
2088
Kevin Hao24258762020-01-14 16:28:19 +08002089 if (!gc->irq.populate_parent_alloc_arg)
2090 gc->irq.populate_parent_alloc_arg =
Linus Walleijfdd61a02019-08-08 14:32:37 +02002091 gpiochip_populate_parent_fwspec_twocell;
2092
2093 gpiochip_hierarchy_setup_domain_ops(&gc->irq.child_irq_domain_ops);
2094
2095 gc->irq.domain = irq_domain_create_hierarchy(
2096 gc->irq.parent_domain,
2097 0,
2098 gc->ngpio,
2099 gc->irq.fwnode,
2100 &gc->irq.child_irq_domain_ops,
2101 gc);
2102
2103 if (!gc->irq.domain)
2104 return -ENOMEM;
2105
2106 gpiochip_set_hierarchical_irqchip(gc, gc->irq.chip);
2107
2108 return 0;
2109}
2110
2111static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
2112{
2113 return !!gc->irq.parent_domain;
2114}
2115
Kevin Hao24258762020-01-14 16:28:19 +08002116void *gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *chip,
Linus Walleijfdd61a02019-08-08 14:32:37 +02002117 unsigned int parent_hwirq,
2118 unsigned int parent_type)
2119{
Kevin Hao24258762020-01-14 16:28:19 +08002120 struct irq_fwspec *fwspec;
2121
2122 fwspec = kmalloc(sizeof(*fwspec), GFP_KERNEL);
2123 if (!fwspec)
2124 return NULL;
2125
2126 fwspec->fwnode = chip->irq.parent_domain->fwnode;
Linus Walleijfdd61a02019-08-08 14:32:37 +02002127 fwspec->param_count = 2;
2128 fwspec->param[0] = parent_hwirq;
2129 fwspec->param[1] = parent_type;
Kevin Hao24258762020-01-14 16:28:19 +08002130
2131 return fwspec;
Linus Walleijfdd61a02019-08-08 14:32:37 +02002132}
2133EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_twocell);
2134
Kevin Hao24258762020-01-14 16:28:19 +08002135void *gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *chip,
Linus Walleijfdd61a02019-08-08 14:32:37 +02002136 unsigned int parent_hwirq,
2137 unsigned int parent_type)
2138{
Kevin Hao24258762020-01-14 16:28:19 +08002139 struct irq_fwspec *fwspec;
2140
2141 fwspec = kmalloc(sizeof(*fwspec), GFP_KERNEL);
2142 if (!fwspec)
2143 return NULL;
2144
2145 fwspec->fwnode = chip->irq.parent_domain->fwnode;
Linus Walleijfdd61a02019-08-08 14:32:37 +02002146 fwspec->param_count = 4;
2147 fwspec->param[0] = 0;
2148 fwspec->param[1] = parent_hwirq;
2149 fwspec->param[2] = 0;
2150 fwspec->param[3] = parent_type;
Kevin Hao24258762020-01-14 16:28:19 +08002151
2152 return fwspec;
Linus Walleijfdd61a02019-08-08 14:32:37 +02002153}
2154EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_fourcell);
2155
2156#else
2157
2158static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
2159{
2160 return -EINVAL;
2161}
2162
2163static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
2164{
2165 return false;
2166}
2167
2168#endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
2169
Linus Walleijd245b3f2016-11-24 10:57:25 +01002170/**
Linus Walleij14250522014-03-25 10:40:18 +01002171 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
2172 * @d: the irqdomain used by this irqchip
2173 * @irq: the global irq number used by this GPIO irqchip irq
2174 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
2175 *
2176 * This function will set up the mapping for a certain IRQ line on a
2177 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
2178 * stored inside the gpiochip.
2179 */
Thierry Reding1b95b4e2017-11-07 19:15:55 +01002180int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
2181 irq_hw_number_t hwirq)
Linus Walleij14250522014-03-25 10:40:18 +01002182{
2183 struct gpio_chip *chip = d->host_data;
Linus Walleijd377f562019-07-16 11:11:45 +02002184 int ret = 0;
Linus Walleij14250522014-03-25 10:40:18 +01002185
Grygorii Strashkodc749a02017-07-21 11:49:00 -05002186 if (!gpiochip_irqchip_irq_valid(chip, hwirq))
2187 return -ENXIO;
2188
Linus Walleij14250522014-03-25 10:40:18 +01002189 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03002190 /*
2191 * This lock class tells lockdep that GPIO irqs are in a different
2192 * category than their parents, so it won't report false recursion.
2193 */
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002194 irq_set_lockdep_class(irq, chip->irq.lock_key, chip->irq.request_key);
Thierry Redingc7a0aa52017-11-07 19:15:48 +01002195 irq_set_chip_and_handler(irq, chip->irq.chip, chip->irq.handler);
Linus Walleijd245b3f2016-11-24 10:57:25 +01002196 /* Chips that use nested thread handlers have them marked */
Thierry Reding60ed54c2017-11-07 19:15:57 +01002197 if (chip->irq.threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02002198 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +01002199 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -05002200
Thierry Redinge0d89722017-11-07 19:15:54 +01002201 if (chip->irq.num_parents == 1)
Linus Walleijd377f562019-07-16 11:11:45 +02002202 ret = irq_set_parent(irq, chip->irq.parents[0]);
Thierry Redinge0d89722017-11-07 19:15:54 +01002203 else if (chip->irq.map)
Linus Walleijd377f562019-07-16 11:11:45 +02002204 ret = irq_set_parent(irq, chip->irq.map[hwirq]);
Thierry Redinge0d89722017-11-07 19:15:54 +01002205
Linus Walleijd377f562019-07-16 11:11:45 +02002206 if (ret < 0)
2207 return ret;
Thierry Redinge0d89722017-11-07 19:15:54 +01002208
Linus Walleij1333b902014-04-23 16:45:12 +02002209 /*
2210 * No set-up of the hardware will happen if IRQ_TYPE_NONE
2211 * is passed as default type.
2212 */
Thierry Reding3634eeb2017-11-07 19:15:49 +01002213 if (chip->irq.default_type != IRQ_TYPE_NONE)
2214 irq_set_irq_type(irq, chip->irq.default_type);
Linus Walleij14250522014-03-25 10:40:18 +01002215
2216 return 0;
2217}
Thierry Reding1b95b4e2017-11-07 19:15:55 +01002218EXPORT_SYMBOL_GPL(gpiochip_irq_map);
Linus Walleij14250522014-03-25 10:40:18 +01002219
Thierry Reding1b95b4e2017-11-07 19:15:55 +01002220void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
Linus Walleijc3626fd2014-03-28 20:42:01 +01002221{
Linus Walleij1c8732b2014-04-09 13:34:39 +02002222 struct gpio_chip *chip = d->host_data;
2223
Thierry Reding60ed54c2017-11-07 19:15:57 +01002224 if (chip->irq.threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02002225 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +01002226 irq_set_chip_and_handler(irq, NULL, NULL);
2227 irq_set_chip_data(irq, NULL);
2228}
Thierry Reding1b95b4e2017-11-07 19:15:55 +01002229EXPORT_SYMBOL_GPL(gpiochip_irq_unmap);
Linus Walleijc3626fd2014-03-28 20:42:01 +01002230
Linus Walleij14250522014-03-25 10:40:18 +01002231static const struct irq_domain_ops gpiochip_domain_ops = {
2232 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +01002233 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +01002234 /* Virtually all GPIO irqchips are twocell:ed */
2235 .xlate = irq_domain_xlate_twocell,
2236};
2237
Linus Walleijfdd61a02019-08-08 14:32:37 +02002238/*
2239 * TODO: move these activate/deactivate in under the hierarchicial
2240 * irqchip implementation as static once SPMI and SSBI (all external
2241 * users) are phased over.
2242 */
Brian Masneyef74f702019-01-19 15:42:42 -05002243/**
2244 * gpiochip_irq_domain_activate() - Lock a GPIO to be used as an IRQ
2245 * @domain: The IRQ domain used by this IRQ chip
2246 * @data: Outermost irq_data associated with the IRQ
2247 * @reserve: If set, only reserve an interrupt vector instead of assigning one
2248 *
2249 * This function is a wrapper that calls gpiochip_lock_as_irq() and is to be
2250 * used as the activate function for the &struct irq_domain_ops. The host_data
2251 * for the IRQ domain must be the &struct gpio_chip.
2252 */
2253int gpiochip_irq_domain_activate(struct irq_domain *domain,
2254 struct irq_data *data, bool reserve)
2255{
2256 struct gpio_chip *chip = domain->host_data;
2257
2258 return gpiochip_lock_as_irq(chip, data->hwirq);
2259}
2260EXPORT_SYMBOL_GPL(gpiochip_irq_domain_activate);
2261
2262/**
2263 * gpiochip_irq_domain_deactivate() - Unlock a GPIO used as an IRQ
2264 * @domain: The IRQ domain used by this IRQ chip
2265 * @data: Outermost irq_data associated with the IRQ
2266 *
2267 * This function is a wrapper that will call gpiochip_unlock_as_irq() and is to
2268 * be used as the deactivate function for the &struct irq_domain_ops. The
2269 * host_data for the IRQ domain must be the &struct gpio_chip.
2270 */
2271void gpiochip_irq_domain_deactivate(struct irq_domain *domain,
2272 struct irq_data *data)
2273{
2274 struct gpio_chip *chip = domain->host_data;
2275
2276 return gpiochip_unlock_as_irq(chip, data->hwirq);
2277}
2278EXPORT_SYMBOL_GPL(gpiochip_irq_domain_deactivate);
2279
Linus Walleij14250522014-03-25 10:40:18 +01002280static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
2281{
Linus Walleijfdd61a02019-08-08 14:32:37 +02002282 struct irq_domain *domain = chip->irq.domain;
2283
Grygorii Strashkodc749a02017-07-21 11:49:00 -05002284 if (!gpiochip_irqchip_irq_valid(chip, offset))
2285 return -ENXIO;
Thierry Redinge0d89722017-11-07 19:15:54 +01002286
Linus Walleijfdd61a02019-08-08 14:32:37 +02002287#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
2288 if (irq_domain_is_hierarchy(domain)) {
2289 struct irq_fwspec spec;
2290
2291 spec.fwnode = domain->fwnode;
2292 spec.param_count = 2;
2293 spec.param[0] = chip->irq.child_offset_to_irq(chip, offset);
2294 spec.param[1] = IRQ_TYPE_NONE;
2295
2296 return irq_create_fwspec_mapping(&spec);
2297 }
2298#endif
2299
2300 return irq_create_mapping(domain, offset);
Linus Walleij14250522014-03-25 10:40:18 +01002301}
2302
Hans Verkuil4e6b8232018-09-08 11:23:14 +02002303static int gpiochip_irq_reqres(struct irq_data *d)
2304{
2305 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
2306
2307 return gpiochip_reqres_irq(chip, d->hwirq);
2308}
2309
2310static void gpiochip_irq_relres(struct irq_data *d)
2311{
2312 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
2313
2314 gpiochip_relres_irq(chip, d->hwirq);
2315}
2316
Hans Verkuil461c1a72018-09-08 11:23:17 +02002317static void gpiochip_irq_enable(struct irq_data *d)
2318{
2319 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
2320
2321 gpiochip_enable_irq(chip, d->hwirq);
2322 if (chip->irq.irq_enable)
2323 chip->irq.irq_enable(d);
2324 else
2325 chip->irq.chip->irq_unmask(d);
2326}
2327
2328static void gpiochip_irq_disable(struct irq_data *d)
2329{
2330 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
2331
2332 if (chip->irq.irq_disable)
2333 chip->irq.irq_disable(d);
2334 else
2335 chip->irq.chip->irq_mask(d);
2336 gpiochip_disable_irq(chip, d->hwirq);
2337}
2338
Hans Verkuilca620f22018-09-08 11:23:15 +02002339static void gpiochip_set_irq_hooks(struct gpio_chip *gpiochip)
2340{
2341 struct irq_chip *irqchip = gpiochip->irq.chip;
2342
2343 if (!irqchip->irq_request_resources &&
2344 !irqchip->irq_release_resources) {
2345 irqchip->irq_request_resources = gpiochip_irq_reqres;
2346 irqchip->irq_release_resources = gpiochip_irq_relres;
2347 }
Hans Verkuil461c1a72018-09-08 11:23:17 +02002348 if (WARN_ON(gpiochip->irq.irq_enable))
2349 return;
Hans Verkuil171948e2018-09-14 10:36:39 +02002350 /* Check if the irqchip already has this hook... */
2351 if (irqchip->irq_enable == gpiochip_irq_enable) {
2352 /*
2353 * ...and if so, give a gentle warning that this is bad
2354 * practice.
2355 */
2356 chip_info(gpiochip,
2357 "detected irqchip that is shared with multiple gpiochips: please fix the driver.\n");
2358 return;
2359 }
Hans Verkuil461c1a72018-09-08 11:23:17 +02002360 gpiochip->irq.irq_enable = irqchip->irq_enable;
2361 gpiochip->irq.irq_disable = irqchip->irq_disable;
2362 irqchip->irq_enable = gpiochip_irq_enable;
2363 irqchip->irq_disable = gpiochip_irq_disable;
Hans Verkuilca620f22018-09-08 11:23:15 +02002364}
2365
Linus Walleij14250522014-03-25 10:40:18 +01002366/**
Thierry Redinge0d89722017-11-07 19:15:54 +01002367 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
2368 * @gpiochip: the GPIO chip to add the IRQ chip to
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002369 * @lock_key: lockdep class for IRQ lock
2370 * @request_key: lockdep class for IRQ request
Thierry Redinge0d89722017-11-07 19:15:54 +01002371 */
Thierry Reding959bc7b2017-11-07 19:15:59 +01002372static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002373 struct lock_class_key *lock_key,
2374 struct lock_class_key *request_key)
Thierry Redinge0d89722017-11-07 19:15:54 +01002375{
2376 struct irq_chip *irqchip = gpiochip->irq.chip;
Linus Walleijfdd61a02019-08-08 14:32:37 +02002377 const struct irq_domain_ops *ops = NULL;
Thierry Redinge0d89722017-11-07 19:15:54 +01002378 struct device_node *np;
2379 unsigned int type;
2380 unsigned int i;
2381
2382 if (!irqchip)
2383 return 0;
2384
2385 if (gpiochip->irq.parent_handler && gpiochip->can_sleep) {
Andy Shevchenkob1911712018-07-03 03:39:03 +03002386 chip_err(gpiochip, "you cannot have chained interrupts on a chip that may sleep\n");
Thierry Redinge0d89722017-11-07 19:15:54 +01002387 return -EINVAL;
2388 }
2389
2390 np = gpiochip->gpiodev->dev.of_node;
2391 type = gpiochip->irq.default_type;
2392
2393 /*
2394 * Specifying a default trigger is a terrible idea if DT or ACPI is
2395 * used to configure the interrupts, as you may end up with
2396 * conflicting triggers. Tell the user, and reset to NONE.
2397 */
2398 if (WARN(np && type != IRQ_TYPE_NONE,
2399 "%s: Ignoring %u default trigger\n", np->full_name, type))
2400 type = IRQ_TYPE_NONE;
2401
2402 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
2403 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
2404 "Ignoring %u default trigger\n", type);
2405 type = IRQ_TYPE_NONE;
2406 }
2407
2408 gpiochip->to_irq = gpiochip_to_irq;
2409 gpiochip->irq.default_type = type;
Thierry Reding959bc7b2017-11-07 19:15:59 +01002410 gpiochip->irq.lock_key = lock_key;
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002411 gpiochip->irq.request_key = request_key;
Thierry Redinge0d89722017-11-07 19:15:54 +01002412
Linus Walleijfdd61a02019-08-08 14:32:37 +02002413 /* If a parent irqdomain is provided, let's build a hierarchy */
2414 if (gpiochip_hierarchy_is_hierarchical(gpiochip)) {
2415 int ret = gpiochip_hierarchy_add_domain(gpiochip);
2416 if (ret)
2417 return ret;
2418 } else {
2419 /* Some drivers provide custom irqdomain ops */
2420 if (gpiochip->irq.domain_ops)
2421 ops = gpiochip->irq.domain_ops;
Thierry Redinge0d89722017-11-07 19:15:54 +01002422
Linus Walleijfdd61a02019-08-08 14:32:37 +02002423 if (!ops)
2424 ops = &gpiochip_domain_ops;
2425 gpiochip->irq.domain = irq_domain_add_simple(np,
2426 gpiochip->ngpio,
2427 gpiochip->irq.first,
2428 ops, gpiochip);
2429 if (!gpiochip->irq.domain)
2430 return -EINVAL;
2431 }
Thierry Redinge0d89722017-11-07 19:15:54 +01002432
Thierry Redinge0d89722017-11-07 19:15:54 +01002433 if (gpiochip->irq.parent_handler) {
2434 void *data = gpiochip->irq.parent_handler_data ?: gpiochip;
2435
2436 for (i = 0; i < gpiochip->irq.num_parents; i++) {
2437 /*
2438 * The parent IRQ chip is already using the chip_data
2439 * for this IRQ chip, so our callbacks simply use the
2440 * handler_data.
2441 */
2442 irq_set_chained_handler_and_data(gpiochip->irq.parents[i],
2443 gpiochip->irq.parent_handler,
2444 data);
2445 }
Thierry Redinge0d89722017-11-07 19:15:54 +01002446 }
2447
Hans Verkuilca620f22018-09-08 11:23:15 +02002448 gpiochip_set_irq_hooks(gpiochip);
2449
Thierry Redinge0d89722017-11-07 19:15:54 +01002450 acpi_gpiochip_request_interrupts(gpiochip);
2451
2452 return 0;
2453}
2454
2455/**
Linus Walleij14250522014-03-25 10:40:18 +01002456 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
2457 * @gpiochip: the gpiochip to remove the irqchip from
2458 *
2459 * This is called only from gpiochip_remove()
2460 */
2461static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
2462{
Hans Verkuilca620f22018-09-08 11:23:15 +02002463 struct irq_chip *irqchip = gpiochip->irq.chip;
Thierry Reding39e5f092017-11-07 19:15:50 +01002464 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +01002465
Mika Westerbergafa82fa2014-07-25 09:54:48 +03002466 acpi_gpiochip_free_interrupts(gpiochip);
2467
Hans Verkuilca620f22018-09-08 11:23:15 +02002468 if (irqchip && gpiochip->irq.parent_handler) {
Thierry Reding39e5f092017-11-07 19:15:50 +01002469 struct gpio_irq_chip *irq = &gpiochip->irq;
2470 unsigned int i;
2471
2472 for (i = 0; i < irq->num_parents; i++)
2473 irq_set_chained_handler_and_data(irq->parents[i],
2474 NULL, NULL);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03002475 }
2476
Linus Walleijc3626fd2014-03-28 20:42:01 +01002477 /* Remove all IRQ mappings and delete the domain */
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01002478 if (gpiochip->irq.domain) {
Thierry Reding39e5f092017-11-07 19:15:50 +01002479 unsigned int irq;
2480
Mika Westerberg79b804c2016-09-20 15:15:21 +03002481 for (offset = 0; offset < gpiochip->ngpio; offset++) {
2482 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
2483 continue;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01002484
2485 irq = irq_find_mapping(gpiochip->irq.domain, offset);
2486 irq_dispose_mapping(irq);
Mika Westerberg79b804c2016-09-20 15:15:21 +03002487 }
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01002488
2489 irq_domain_remove(gpiochip->irq.domain);
Linus Walleijc3626fd2014-03-28 20:42:01 +01002490 }
Linus Walleij14250522014-03-25 10:40:18 +01002491
Hans Verkuil461c1a72018-09-08 11:23:17 +02002492 if (irqchip) {
2493 if (irqchip->irq_request_resources == gpiochip_irq_reqres) {
2494 irqchip->irq_request_resources = NULL;
2495 irqchip->irq_release_resources = NULL;
2496 }
2497 if (irqchip->irq_enable == gpiochip_irq_enable) {
2498 irqchip->irq_enable = gpiochip->irq.irq_enable;
2499 irqchip->irq_disable = gpiochip->irq.irq_disable;
2500 }
Linus Walleij14250522014-03-25 10:40:18 +01002501 }
Hans Verkuil461c1a72018-09-08 11:23:17 +02002502 gpiochip->irq.irq_enable = NULL;
2503 gpiochip->irq.irq_disable = NULL;
Hans Verkuilca620f22018-09-08 11:23:15 +02002504 gpiochip->irq.chip = NULL;
Mika Westerberg79b804c2016-09-20 15:15:21 +03002505
2506 gpiochip_irqchip_free_valid_mask(gpiochip);
Linus Walleij14250522014-03-25 10:40:18 +01002507}
2508
2509/**
Linus Walleij739e6f52017-01-11 13:37:07 +01002510 * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01002511 * @gpiochip: the gpiochip to add the irqchip to
2512 * @irqchip: the irqchip to add to the gpiochip
2513 * @first_irq: if not dynamically assigned, the base (first) IRQ to
2514 * allocate gpiochip irqs from
2515 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +02002516 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
2517 * to have the core avoid setting up any default type in the hardware.
Thierry Reding60ed54c2017-11-07 19:15:57 +01002518 * @threaded: whether this irqchip uses a nested thread handler
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002519 * @lock_key: lockdep class for IRQ lock
2520 * @request_key: lockdep class for IRQ request
Linus Walleij14250522014-03-25 10:40:18 +01002521 *
2522 * This function closely associates a certain irqchip with a certain
2523 * gpiochip, providing an irq domain to translate the local IRQs to
2524 * global irqs in the gpiolib core, and making sure that the gpiochip
2525 * is passed as chip data to all related functions. Driver callbacks
Linus Walleij09dd5f92015-12-07 15:31:58 +01002526 * need to use gpiochip_get_data() to get their local state containers back
Linus Walleij14250522014-03-25 10:40:18 +01002527 * from the gpiochip passed as chip data. An irqdomain will be stored
2528 * in the gpiochip that shall be used by the driver to handle IRQ number
2529 * translation. The gpiochip will need to be initialized and registered
2530 * before calling this function.
2531 *
Linus Walleijc3626fd2014-03-28 20:42:01 +01002532 * This function will handle two cell:ed simple IRQs and assumes all
2533 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +01002534 * need to be open coded.
2535 */
Linus Walleij739e6f52017-01-11 13:37:07 +01002536int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
2537 struct irq_chip *irqchip,
2538 unsigned int first_irq,
2539 irq_flow_handler_t handler,
2540 unsigned int type,
Thierry Reding60ed54c2017-11-07 19:15:57 +01002541 bool threaded,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002542 struct lock_class_key *lock_key,
2543 struct lock_class_key *request_key)
Linus Walleij14250522014-03-25 10:40:18 +01002544{
2545 struct device_node *of_node;
Linus Walleij14250522014-03-25 10:40:18 +01002546
2547 if (!gpiochip || !irqchip)
2548 return -EINVAL;
2549
Linus Walleij58383c782015-11-04 09:56:26 +01002550 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +01002551 pr_err("missing gpiochip .dev parent pointer\n");
2552 return -EINVAL;
2553 }
Thierry Reding60ed54c2017-11-07 19:15:57 +01002554 gpiochip->irq.threaded = threaded;
Linus Walleij58383c782015-11-04 09:56:26 +01002555 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +01002556#ifdef CONFIG_OF_GPIO
2557 /*
Colin Cronin20a8a962015-05-18 11:41:43 -07002558 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08002559 * FIXME: get rid of this and use gpiochip->parent->of_node
2560 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +01002561 */
2562 if (gpiochip->of_node)
2563 of_node = gpiochip->of_node;
2564#endif
Marc Zyngier332e99d2016-09-07 09:12:11 +01002565 /*
Mika Westerberg0a1e0052016-09-12 14:29:51 +03002566 * Specifying a default trigger is a terrible idea if DT or ACPI is
Marc Zyngier332e99d2016-09-07 09:12:11 +01002567 * used to configure the interrupts, as you may end-up with
2568 * conflicting triggers. Tell the user, and reset to NONE.
2569 */
2570 if (WARN(of_node && type != IRQ_TYPE_NONE,
Rob Herring7eb6ce22017-07-18 16:43:03 -05002571 "%pOF: Ignoring %d default trigger\n", of_node, type))
Marc Zyngier332e99d2016-09-07 09:12:11 +01002572 type = IRQ_TYPE_NONE;
Mika Westerberg0a1e0052016-09-12 14:29:51 +03002573 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
2574 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
2575 "Ignoring %d default trigger\n", type);
2576 type = IRQ_TYPE_NONE;
2577 }
Marc Zyngier332e99d2016-09-07 09:12:11 +01002578
Thierry Redingda80ff82017-11-07 19:15:46 +01002579 gpiochip->irq.chip = irqchip;
Thierry Redingc7a0aa52017-11-07 19:15:48 +01002580 gpiochip->irq.handler = handler;
Thierry Reding3634eeb2017-11-07 19:15:49 +01002581 gpiochip->irq.default_type = type;
Linus Walleij14250522014-03-25 10:40:18 +01002582 gpiochip->to_irq = gpiochip_to_irq;
Thierry Redingca9df052017-11-07 19:15:53 +01002583 gpiochip->irq.lock_key = lock_key;
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002584 gpiochip->irq.request_key = request_key;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01002585 gpiochip->irq.domain = irq_domain_add_simple(of_node,
Linus Walleij14250522014-03-25 10:40:18 +01002586 gpiochip->ngpio, first_irq,
2587 &gpiochip_domain_ops, gpiochip);
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01002588 if (!gpiochip->irq.domain) {
Thierry Redingda80ff82017-11-07 19:15:46 +01002589 gpiochip->irq.chip = NULL;
Linus Walleij14250522014-03-25 10:40:18 +01002590 return -EINVAL;
2591 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +02002592
Hans Verkuilca620f22018-09-08 11:23:15 +02002593 gpiochip_set_irq_hooks(gpiochip);
Linus Walleij14250522014-03-25 10:40:18 +01002594
Mika Westerbergafa82fa2014-07-25 09:54:48 +03002595 acpi_gpiochip_request_interrupts(gpiochip);
2596
Linus Walleij14250522014-03-25 10:40:18 +01002597 return 0;
2598}
Linus Walleij739e6f52017-01-11 13:37:07 +01002599EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key);
Linus Walleij14250522014-03-25 10:40:18 +01002600
2601#else /* CONFIG_GPIOLIB_IRQCHIP */
2602
Thierry Reding959bc7b2017-11-07 19:15:59 +01002603static inline int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002604 struct lock_class_key *lock_key,
2605 struct lock_class_key *request_key)
Thierry Redinge0d89722017-11-07 19:15:54 +01002606{
2607 return 0;
2608}
Linus Walleij14250522014-03-25 10:40:18 +01002609static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
Andy Shevchenko9411e3a2019-10-09 17:34:44 +03002610
2611static inline int gpiochip_irqchip_init_hw(struct gpio_chip *gpiochip)
2612{
2613 return 0;
2614}
2615
Mika Westerberg79b804c2016-09-20 15:15:21 +03002616static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
2617{
2618 return 0;
2619}
2620static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
2621{ }
Linus Walleij14250522014-03-25 10:40:18 +01002622
2623#endif /* CONFIG_GPIOLIB_IRQCHIP */
2624
Jonas Gorskic771c2f2015-10-11 17:34:15 +02002625/**
2626 * gpiochip_generic_request() - request the gpio function for a pin
2627 * @chip: the gpiochip owning the GPIO
2628 * @offset: the offset of the GPIO to request for GPIO function
2629 */
2630int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
2631{
Linus Walleija9a1d2a2017-09-22 11:02:10 +02002632 return pinctrl_gpio_request(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02002633}
2634EXPORT_SYMBOL_GPL(gpiochip_generic_request);
2635
2636/**
2637 * gpiochip_generic_free() - free the gpio function from a pin
2638 * @chip: the gpiochip to request the gpio function for
2639 * @offset: the offset of the GPIO to free from GPIO function
2640 */
2641void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
2642{
Linus Walleija9a1d2a2017-09-22 11:02:10 +02002643 pinctrl_gpio_free(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02002644}
2645EXPORT_SYMBOL_GPL(gpiochip_generic_free);
2646
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002647/**
2648 * gpiochip_generic_config() - apply configuration for a pin
2649 * @chip: the gpiochip owning the GPIO
2650 * @offset: the offset of the GPIO to apply the configuration
2651 * @config: the configuration to be applied
2652 */
2653int gpiochip_generic_config(struct gpio_chip *chip, unsigned offset,
2654 unsigned long config)
2655{
2656 return pinctrl_gpio_set_config(chip->gpiodev->base + offset, config);
2657}
2658EXPORT_SYMBOL_GPL(gpiochip_generic_config);
2659
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302660#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01002661
Linus Walleij3f0f8672012-11-20 12:40:15 +01002662/**
Christian Ruppert586a87e2013-10-15 15:37:54 +02002663 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
2664 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +02002665 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +02002666 * @gpio_offset: the start offset in the current gpio_chip number space
2667 * @pin_group: name of the pin group inside the pin controller
Christian Lamparter973c1712018-05-21 22:57:39 +02002668 *
2669 * Calling this function directly from a DeviceTree-supported
2670 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
2671 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
2672 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
Christian Ruppert586a87e2013-10-15 15:37:54 +02002673 */
2674int gpiochip_add_pingroup_range(struct gpio_chip *chip,
2675 struct pinctrl_dev *pctldev,
2676 unsigned int gpio_offset, const char *pin_group)
2677{
2678 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002679 struct gpio_device *gdev = chip->gpiodev;
Christian Ruppert586a87e2013-10-15 15:37:54 +02002680 int ret;
2681
2682 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
2683 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002684 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +02002685 return -ENOMEM;
2686 }
2687
2688 /* Use local offset as range ID */
2689 pin_range->range.id = gpio_offset;
2690 pin_range->range.gc = chip;
2691 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002692 pin_range->range.base = gdev->base + gpio_offset;
Christian Ruppert586a87e2013-10-15 15:37:54 +02002693 pin_range->pctldev = pctldev;
2694
2695 ret = pinctrl_get_group_pins(pctldev, pin_group,
2696 &pin_range->range.pins,
2697 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +01002698 if (ret < 0) {
2699 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +02002700 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +01002701 }
Christian Ruppert586a87e2013-10-15 15:37:54 +02002702
2703 pinctrl_add_gpio_range(pctldev, &pin_range->range);
2704
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002705 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
2706 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +02002707 pinctrl_dev_get_devname(pctldev), pin_group);
2708
Linus Walleij20ec3e32016-02-11 11:03:06 +01002709 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Christian Ruppert586a87e2013-10-15 15:37:54 +02002710
2711 return 0;
2712}
2713EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
2714
2715/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01002716 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
2717 * @chip: the gpiochip to add the range for
Thierry Reding950d55f52017-07-24 16:57:22 +02002718 * @pinctl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01002719 * @gpio_offset: the start offset in the current gpio_chip number space
2720 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01002721 * @npins: the number of pins from the offset of each pin space (GPIO and
2722 * pin controller) to accumulate in this range
Thierry Reding950d55f52017-07-24 16:57:22 +02002723 *
2724 * Returns:
2725 * 0 on success, or a negative error-code on failure.
Christian Lamparter973c1712018-05-21 22:57:39 +02002726 *
2727 * Calling this function directly from a DeviceTree-supported
2728 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
2729 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
2730 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
Linus Walleij3f0f8672012-11-20 12:40:15 +01002731 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002732int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01002733 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01002734 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302735{
2736 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002737 struct gpio_device *gdev = chip->gpiodev;
Axel Linb4d4b1f2012-11-21 14:33:56 +08002738 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302739
Linus Walleij3f0f8672012-11-20 12:40:15 +01002740 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302741 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002742 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002743 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302744 }
2745
Linus Walleij3f0f8672012-11-20 12:40:15 +01002746 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01002747 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01002748 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302749 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002750 pin_range->range.base = gdev->base + gpio_offset;
Linus Walleij316511c2012-11-21 08:48:09 +01002751 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302752 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01002753 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302754 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01002755 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08002756 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002757 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +01002758 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08002759 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01002760 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002761 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
2762 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +01002763 pinctl_name,
2764 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302765
Linus Walleij20ec3e32016-02-11 11:03:06 +01002766 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002767
2768 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302769}
Linus Walleij165adc92012-11-06 14:49:39 +01002770EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302771
Linus Walleij3f0f8672012-11-20 12:40:15 +01002772/**
2773 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
2774 * @chip: the chip to remove all the mappings for
2775 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302776void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
2777{
2778 struct gpio_pin_range *pin_range, *tmp;
Linus Walleij20ec3e32016-02-11 11:03:06 +01002779 struct gpio_device *gdev = chip->gpiodev;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302780
Linus Walleij20ec3e32016-02-11 11:03:06 +01002781 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302782 list_del(&pin_range->node);
2783 pinctrl_remove_gpio_range(pin_range->pctldev,
2784 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01002785 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302786 }
2787}
Linus Walleij165adc92012-11-06 14:49:39 +01002788EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
2789
2790#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302791
David Brownelld2876d02008-02-04 22:28:20 -08002792/* These "optional" allocation calls help prevent drivers from stomping
2793 * on each other, and help provide better diagnostics in debugfs.
2794 * They're called even less than the "set direction" calls.
2795 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002796static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08002797{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002798 struct gpio_chip *chip = desc->gdev->chip;
Linus Walleijd377f562019-07-16 11:11:45 +02002799 int ret;
David Brownelld2876d02008-02-04 22:28:20 -08002800 unsigned long flags;
Biju Das3789f5a2018-08-07 08:15:18 +01002801 unsigned offset;
David Brownelld2876d02008-02-04 22:28:20 -08002802
Muchun Song18534df2018-11-01 21:12:50 +08002803 if (label) {
2804 label = kstrdup_const(label, GFP_KERNEL);
2805 if (!label)
2806 return -ENOMEM;
2807 }
2808
David Brownelld2876d02008-02-04 22:28:20 -08002809 spin_lock_irqsave(&gpio_lock, flags);
2810
David Brownelld2876d02008-02-04 22:28:20 -08002811 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07002812 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08002813 */
2814
2815 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
2816 desc_set_label(desc, label ? : "?");
Linus Walleijd377f562019-07-16 11:11:45 +02002817 ret = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07002818 } else {
Muchun Song18534df2018-11-01 21:12:50 +08002819 kfree_const(label);
Linus Walleijd377f562019-07-16 11:11:45 +02002820 ret = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -08002821 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07002822 }
2823
2824 if (chip->request) {
2825 /* chip->request may sleep */
2826 spin_unlock_irqrestore(&gpio_lock, flags);
Biju Das3789f5a2018-08-07 08:15:18 +01002827 offset = gpio_chip_hwgpio(desc);
2828 if (gpiochip_line_is_valid(chip, offset))
Linus Walleijd377f562019-07-16 11:11:45 +02002829 ret = chip->request(chip, offset);
Biju Das3789f5a2018-08-07 08:15:18 +01002830 else
Linus Walleijd377f562019-07-16 11:11:45 +02002831 ret = -EINVAL;
David Brownell35e8bb52008-10-15 22:03:16 -07002832 spin_lock_irqsave(&gpio_lock, flags);
2833
Linus Walleijd377f562019-07-16 11:11:45 +02002834 if (ret < 0) {
David Brownell35e8bb52008-10-15 22:03:16 -07002835 desc_set_label(desc, NULL);
Muchun Song18534df2018-11-01 21:12:50 +08002836 kfree_const(label);
David Brownell35e8bb52008-10-15 22:03:16 -07002837 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03002838 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07002839 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07002840 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03002841 if (chip->get_direction) {
2842 /* chip->get_direction may sleep */
2843 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002844 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03002845 spin_lock_irqsave(&gpio_lock, flags);
2846 }
David Brownelld2876d02008-02-04 22:28:20 -08002847done:
Mika Westerberg77c2d792014-03-10 14:54:50 +02002848 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijd377f562019-07-16 11:11:45 +02002849 return ret;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002850}
2851
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002852/*
2853 * This descriptor validation needs to be inserted verbatim into each
2854 * function taking a descriptor, so we need to use a preprocessor
Linus Walleij54d77192016-05-30 16:48:39 +02002855 * macro to avoid endless duplication. If the desc is NULL it is an
2856 * optional GPIO and calls should just bail out.
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002857 */
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002858static int validate_desc(const struct gpio_desc *desc, const char *func)
2859{
2860 if (!desc)
2861 return 0;
2862 if (IS_ERR(desc)) {
2863 pr_warn("%s: invalid GPIO (errorpointer)\n", func);
2864 return PTR_ERR(desc);
2865 }
2866 if (!desc->gdev) {
2867 pr_warn("%s: invalid GPIO (no device)\n", func);
2868 return -EINVAL;
2869 }
2870 if (!desc->gdev->chip) {
2871 dev_warn(&desc->gdev->dev,
2872 "%s: backing chip is gone\n", func);
2873 return 0;
2874 }
2875 return 1;
2876}
2877
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002878#define VALIDATE_DESC(desc) do { \
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002879 int __valid = validate_desc(desc, __func__); \
2880 if (__valid <= 0) \
2881 return __valid; \
2882 } while (0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002883
2884#define VALIDATE_DESC_VOID(desc) do { \
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002885 int __valid = validate_desc(desc, __func__); \
2886 if (__valid <= 0) \
Linus Walleij54d77192016-05-30 16:48:39 +02002887 return; \
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002888 } while (0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002889
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002890int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002891{
Linus Walleijd377f562019-07-16 11:11:45 +02002892 int ret = -EPROBE_DEFER;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002893 struct gpio_device *gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002894
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002895 VALIDATE_DESC(desc);
2896 gdev = desc->gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002897
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002898 if (try_module_get(gdev->owner)) {
Linus Walleijd377f562019-07-16 11:11:45 +02002899 ret = gpiod_request_commit(desc, label);
2900 if (ret < 0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002901 module_put(gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002902 else
2903 get_device(&gdev->dev);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002904 }
2905
Linus Walleijd377f562019-07-16 11:11:45 +02002906 if (ret)
2907 gpiod_dbg(desc, "%s: status %d\n", __func__, ret);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002908
Linus Walleijd377f562019-07-16 11:11:45 +02002909 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08002910}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002911
Linus Walleijfac9d882017-09-26 20:58:28 +02002912static bool gpiod_free_commit(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002913{
Mika Westerberg77c2d792014-03-10 14:54:50 +02002914 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08002915 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07002916 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002917
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07002918 might_sleep();
2919
Alexandre Courbot372e7222013-02-03 01:29:29 +09002920 gpiod_unexport(desc);
David Brownelld8f388d82008-07-25 01:46:07 -07002921
David Brownelld2876d02008-02-04 22:28:20 -08002922 spin_lock_irqsave(&gpio_lock, flags);
2923
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002924 chip = desc->gdev->chip;
David Brownell35e8bb52008-10-15 22:03:16 -07002925 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
2926 if (chip->free) {
2927 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07002928 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002929 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07002930 spin_lock_irqsave(&gpio_lock, flags);
2931 }
Muchun Song18534df2018-11-01 21:12:50 +08002932 kfree_const(desc->label);
David Brownelld2876d02008-02-04 22:28:20 -08002933 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08002934 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07002935 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302936 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302937 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Drew Fustini9225d512019-11-05 10:04:23 +08002938 clear_bit(FLAG_PULL_UP, &desc->flags);
2939 clear_bit(FLAG_PULL_DOWN, &desc->flags);
Kent Gibson2148ad72019-11-05 10:04:25 +08002940 clear_bit(FLAG_BIAS_DISABLE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06002941 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002942 ret = true;
2943 }
David Brownelld2876d02008-02-04 22:28:20 -08002944
2945 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002946 return ret;
2947}
2948
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002949void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002950{
Linus Walleijfac9d882017-09-26 20:58:28 +02002951 if (desc && desc->gdev && gpiod_free_commit(desc)) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002952 module_put(desc->gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002953 put_device(&desc->gdev->dev);
2954 } else {
Mika Westerberg77c2d792014-03-10 14:54:50 +02002955 WARN_ON(extra_checks);
Linus Walleij33a68e82016-02-11 10:28:44 +01002956 }
David Brownelld2876d02008-02-04 22:28:20 -08002957}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002958
David Brownelld2876d02008-02-04 22:28:20 -08002959/**
2960 * gpiochip_is_requested - return string iff signal was requested
2961 * @chip: controller managing the signal
2962 * @offset: of signal within controller's 0..(ngpio - 1) range
2963 *
2964 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09002965 * The string returned is the label passed to gpio_request(); if none has been
2966 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08002967 *
2968 * This function is for use by GPIO controller drivers. The label can
2969 * help with diagnostics, and knowing that the signal is used as a GPIO
2970 * can help avoid accidentally multiplexing it to another controller.
2971 */
2972const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
2973{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002974 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08002975
Dirk Behme48b59532015-08-18 18:02:32 +02002976 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08002977 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002978
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002979 desc = &chip->gpiodev->descs[offset];
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002980
Alexandre Courbot372e7222013-02-03 01:29:29 +09002981 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08002982 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002983 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08002984}
2985EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2986
Mika Westerberg77c2d792014-03-10 14:54:50 +02002987/**
2988 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
Thierry Reding950d55f52017-07-24 16:57:22 +02002989 * @chip: GPIO chip
2990 * @hwnum: hardware number of the GPIO for which to request the descriptor
Mika Westerberg77c2d792014-03-10 14:54:50 +02002991 * @label: label for the GPIO
Linus Walleij5923ea62019-04-26 14:40:18 +02002992 * @lflags: lookup flags for this GPIO or 0 if default, this can be used to
2993 * specify things like line inversion semantics with the machine flags
2994 * such as GPIO_OUT_LOW
2995 * @dflags: descriptor request flags for this GPIO or 0 if default, this
2996 * can be used to specify consumer semantics such as open drain
Mika Westerberg77c2d792014-03-10 14:54:50 +02002997 *
2998 * Function allows GPIO chip drivers to request and use their own GPIO
2999 * descriptors via gpiolib API. Difference to gpiod_request() is that this
3000 * function will not increase reference count of the GPIO chip module. This
3001 * allows the GPIO chip module to be unloaded as needed (we assume that the
3002 * GPIO chip driver handles freeing the GPIOs it has requested).
Thierry Reding950d55f52017-07-24 16:57:22 +02003003 *
3004 * Returns:
3005 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
3006 * code on failure.
Mika Westerberg77c2d792014-03-10 14:54:50 +02003007 */
Bartosz Golaszewski06863622019-12-24 13:06:59 +01003008struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip,
3009 unsigned int hwnum,
Linus Walleij21abf102018-09-04 13:31:45 +02003010 const char *label,
Linus Walleij5923ea62019-04-26 14:40:18 +02003011 enum gpio_lookup_flags lflags,
3012 enum gpiod_flags dflags)
Mika Westerberg77c2d792014-03-10 14:54:50 +02003013{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07003014 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
Linus Walleijd377f562019-07-16 11:11:45 +02003015 int ret;
Mika Westerberg77c2d792014-03-10 14:54:50 +02003016
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07003017 if (IS_ERR(desc)) {
3018 chip_err(chip, "failed to get GPIO descriptor\n");
3019 return desc;
3020 }
3021
Linus Walleijd377f562019-07-16 11:11:45 +02003022 ret = gpiod_request_commit(desc, label);
3023 if (ret < 0)
3024 return ERR_PTR(ret);
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07003025
Linus Walleijd377f562019-07-16 11:11:45 +02003026 ret = gpiod_configure_flags(desc, label, lflags, dflags);
3027 if (ret) {
Linus Walleij21abf102018-09-04 13:31:45 +02003028 chip_err(chip, "setup of own GPIO %s failed\n", label);
3029 gpiod_free_commit(desc);
Linus Walleijd377f562019-07-16 11:11:45 +02003030 return ERR_PTR(ret);
Linus Walleij21abf102018-09-04 13:31:45 +02003031 }
3032
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07003033 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02003034}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07003035EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02003036
3037/**
3038 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
3039 * @desc: GPIO descriptor to free
3040 *
3041 * Function frees the given GPIO requested previously with
3042 * gpiochip_request_own_desc().
3043 */
3044void gpiochip_free_own_desc(struct gpio_desc *desc)
3045{
3046 if (desc)
Linus Walleijfac9d882017-09-26 20:58:28 +02003047 gpiod_free_commit(desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02003048}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07003049EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08003050
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003051/*
3052 * Drivers MUST set GPIO direction before making get/set calls. In
David Brownelld2876d02008-02-04 22:28:20 -08003053 * some cases this is done in early boot, before IRQs are enabled.
3054 *
3055 * As a rule these aren't called more than once (except for drivers
3056 * using the open-drain emulation idiom) so these are natural places
3057 * to accumulate extra debugging checks. Note that we can't (yet)
3058 * rely on gpio_request() having been called beforehand.
3059 */
3060
YueHaibingd18fddf2020-01-16 22:29:27 +08003061static int gpio_set_config(struct gpio_chip *gc, unsigned int offset,
3062 enum pin_config_param mode)
Bartosz Golaszewskid90f3682019-12-24 13:06:58 +01003063{
3064 if (!gc->set_config)
3065 return -ENOTSUPP;
3066
3067 return gc->set_config(gc, offset, mode);
3068}
3069
Kent Gibson2148ad72019-11-05 10:04:25 +08003070static int gpio_set_bias(struct gpio_chip *chip, struct gpio_desc *desc)
3071{
3072 int bias = 0;
3073 int ret = 0;
3074
3075 if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
3076 bias = PIN_CONFIG_BIAS_DISABLE;
3077 else if (test_bit(FLAG_PULL_UP, &desc->flags))
3078 bias = PIN_CONFIG_BIAS_PULL_UP;
3079 else if (test_bit(FLAG_PULL_DOWN, &desc->flags))
3080 bias = PIN_CONFIG_BIAS_PULL_DOWN;
3081
3082 if (bias) {
3083 ret = gpio_set_config(chip, gpio_chip_hwgpio(desc), bias);
3084 if (ret != -ENOTSUPP)
3085 return ret;
3086 }
3087 return 0;
3088}
3089
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003090/**
3091 * gpiod_direction_input - set the GPIO direction to input
3092 * @desc: GPIO to set to input
3093 *
3094 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
3095 * be called safely on it.
3096 *
3097 * Return 0 in case of success, else an error code.
3098 */
3099int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08003100{
David Brownelld2876d02008-02-04 22:28:20 -08003101 struct gpio_chip *chip;
Linus Walleijd377f562019-07-16 11:11:45 +02003102 int ret = 0;
David Brownelld2876d02008-02-04 22:28:20 -08003103
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003104 VALIDATE_DESC(desc);
3105 chip = desc->gdev->chip;
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09003106
Linus Walleije48d1942018-09-25 09:54:14 +02003107 /*
3108 * It is legal to have no .get() and .direction_input() specified if
3109 * the chip is output-only, but you can't specify .direction_input()
3110 * and not support the .get() operation, that doesn't make sense.
3111 */
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003112 if (!chip->get && chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01003113 gpiod_warn(desc,
Linus Walleije48d1942018-09-25 09:54:14 +02003114 "%s: missing get() but have direction_input()\n",
3115 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02003116 return -EIO;
3117 }
3118
Linus Walleije48d1942018-09-25 09:54:14 +02003119 /*
3120 * If we have a .direction_input() callback, things are simple,
3121 * just call it. Else we are some input-only chip so try to check the
3122 * direction (if .get_direction() is supported) else we silently
3123 * assume we are in input mode after this.
3124 */
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003125 if (chip->direction_input) {
Linus Walleijd377f562019-07-16 11:11:45 +02003126 ret = chip->direction_input(chip, gpio_chip_hwgpio(desc));
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003127 } else if (chip->get_direction &&
3128 (chip->get_direction(chip, gpio_chip_hwgpio(desc)) != 1)) {
3129 gpiod_warn(desc,
Linus Walleije48d1942018-09-25 09:54:14 +02003130 "%s: missing direction_input() operation and line is output\n",
3131 __func__);
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003132 return -EIO;
3133 }
Kent Gibson2148ad72019-11-05 10:04:25 +08003134 if (ret == 0) {
David Brownelld2876d02008-02-04 22:28:20 -08003135 clear_bit(FLAG_IS_OUT, &desc->flags);
Kent Gibson2148ad72019-11-05 10:04:25 +08003136 ret = gpio_set_bias(chip, desc);
3137 }
Thomas Petazzonid4499912019-02-07 17:28:58 +01003138
Linus Walleijd377f562019-07-16 11:11:45 +02003139 trace_gpio_direction(desc_to_gpio(desc), 1, ret);
Alexandre Courbotd82da792014-07-22 16:17:43 +09003140
Linus Walleijd377f562019-07-16 11:11:45 +02003141 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08003142}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003143EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003144
Linus Walleijfac9d882017-09-26 20:58:28 +02003145static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08003146{
Linus Walleijc663e5f2016-03-22 10:51:16 +01003147 struct gpio_chip *gc = desc->gdev->chip;
Linus Walleijad177312016-11-13 23:02:44 +01003148 int val = !!value;
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003149 int ret = 0;
David Brownelld2876d02008-02-04 22:28:20 -08003150
Linus Walleije48d1942018-09-25 09:54:14 +02003151 /*
3152 * It's OK not to specify .direction_output() if the gpiochip is
3153 * output-only, but if there is then not even a .set() operation it
3154 * is pretty tricky to drive the output line.
3155 */
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003156 if (!gc->set && !gc->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01003157 gpiod_warn(desc,
Linus Walleije48d1942018-09-25 09:54:14 +02003158 "%s: missing set() and direction_output() operations\n",
3159 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02003160 return -EIO;
3161 }
3162
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003163 if (gc->direction_output) {
3164 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
3165 } else {
Linus Walleije48d1942018-09-25 09:54:14 +02003166 /* Check that we are in output mode if we can */
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003167 if (gc->get_direction &&
3168 gc->get_direction(gc, gpio_chip_hwgpio(desc))) {
3169 gpiod_warn(desc,
3170 "%s: missing direction_output() operation\n",
3171 __func__);
3172 return -EIO;
3173 }
Linus Walleije48d1942018-09-25 09:54:14 +02003174 /*
3175 * If we can't actively set the direction, we are some
3176 * output-only chip, so just drive the output as desired.
3177 */
Ricardo Ribalda Delgadoae9847f2018-09-21 12:36:03 +02003178 gc->set(gc, gpio_chip_hwgpio(desc), val);
3179 }
3180
Linus Walleijc663e5f2016-03-22 10:51:16 +01003181 if (!ret)
David Brownelld2876d02008-02-04 22:28:20 -08003182 set_bit(FLAG_IS_OUT, &desc->flags);
Linus Walleijad177312016-11-13 23:02:44 +01003183 trace_gpio_value(desc_to_gpio(desc), 0, val);
Linus Walleijc663e5f2016-03-22 10:51:16 +01003184 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
3185 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08003186}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01003187
3188/**
3189 * gpiod_direction_output_raw - set the GPIO direction to output
3190 * @desc: GPIO to set to output
3191 * @value: initial output value of the GPIO
3192 *
3193 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
3194 * be called safely on it. The initial value of the output must be specified
3195 * as raw value on the physical line without regard for the ACTIVE_LOW status.
3196 *
3197 * Return 0 in case of success, else an error code.
3198 */
3199int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
3200{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003201 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003202 return gpiod_direction_output_raw_commit(desc, value);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01003203}
3204EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
3205
3206/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05303207 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01003208 * @desc: GPIO to set to output
3209 * @value: initial output value of the GPIO
3210 *
3211 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
3212 * be called safely on it. The initial value of the output must be specified
3213 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3214 * account.
3215 *
3216 * Return 0 in case of success, else an error code.
3217 */
3218int gpiod_direction_output(struct gpio_desc *desc, int value)
3219{
Vladimir Zapolskiy30322bc2017-12-21 18:37:24 +02003220 struct gpio_chip *gc;
Linus Walleij02e47982017-09-26 21:20:23 +02003221 int ret;
3222
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003223 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01003224 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3225 value = !value;
Linus Walleijad177312016-11-13 23:02:44 +01003226 else
3227 value = !!value;
Linus Walleij02e47982017-09-26 21:20:23 +02003228
Hans Verkuil4e9439d2018-09-08 11:23:16 +02003229 /* GPIOs used for enabled IRQs shall not be set as output */
3230 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags) &&
3231 test_bit(FLAG_IRQ_IS_ENABLED, &desc->flags)) {
Linus Walleij02e47982017-09-26 21:20:23 +02003232 gpiod_err(desc,
3233 "%s: tried to set a GPIO tied to an IRQ as output\n",
3234 __func__);
3235 return -EIO;
3236 }
3237
Vladimir Zapolskiy30322bc2017-12-21 18:37:24 +02003238 gc = desc->gdev->chip;
Linus Walleij02e47982017-09-26 21:20:23 +02003239 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
3240 /* First see if we can enable open drain in hardware */
Thomas Petazzoni71479782019-02-07 17:28:56 +01003241 ret = gpio_set_config(gc, gpio_chip_hwgpio(desc),
3242 PIN_CONFIG_DRIVE_OPEN_DRAIN);
Linus Walleij02e47982017-09-26 21:20:23 +02003243 if (!ret)
3244 goto set_output_value;
3245 /* Emulate open drain by not actively driving the line high */
Bartosz Golaszewskie7352442019-10-01 11:44:53 +02003246 if (value) {
3247 ret = gpiod_direction_input(desc);
3248 goto set_output_flag;
3249 }
Linus Walleij02e47982017-09-26 21:20:23 +02003250 }
3251 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
Thomas Petazzoni71479782019-02-07 17:28:56 +01003252 ret = gpio_set_config(gc, gpio_chip_hwgpio(desc),
3253 PIN_CONFIG_DRIVE_OPEN_SOURCE);
Linus Walleij02e47982017-09-26 21:20:23 +02003254 if (!ret)
3255 goto set_output_value;
3256 /* Emulate open source by not actively driving the line low */
Bartosz Golaszewskie7352442019-10-01 11:44:53 +02003257 if (!value) {
3258 ret = gpiod_direction_input(desc);
3259 goto set_output_flag;
3260 }
Linus Walleij02e47982017-09-26 21:20:23 +02003261 } else {
Thomas Petazzoni71479782019-02-07 17:28:56 +01003262 gpio_set_config(gc, gpio_chip_hwgpio(desc),
3263 PIN_CONFIG_DRIVE_PUSH_PULL);
Linus Walleij02e47982017-09-26 21:20:23 +02003264 }
3265
3266set_output_value:
Kent Gibson2821ae52019-11-05 10:04:26 +08003267 ret = gpio_set_bias(gc, desc);
3268 if (ret)
3269 return ret;
Linus Walleijfac9d882017-09-26 20:58:28 +02003270 return gpiod_direction_output_raw_commit(desc, value);
Bartosz Golaszewskie7352442019-10-01 11:44:53 +02003271
3272set_output_flag:
3273 /*
3274 * When emulating open-source or open-drain functionalities by not
3275 * actively driving the line (setting mode to input) we still need to
3276 * set the IS_OUT flag or otherwise we won't be able to set the line
3277 * value anymore.
3278 */
3279 if (ret == 0)
3280 set_bit(FLAG_IS_OUT, &desc->flags);
3281 return ret;
Philipp Zabelef70bbe2014-01-07 12:34:11 +01003282}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003283EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08003284
Felipe Balbic4b5be92010-05-26 14:42:23 -07003285/**
Thierry Reding950d55f52017-07-24 16:57:22 +02003286 * gpiod_set_debounce - sets @debounce time for a GPIO
3287 * @desc: descriptor of the GPIO for which to set debounce time
3288 * @debounce: debounce time in microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02003289 *
Thierry Reding950d55f52017-07-24 16:57:22 +02003290 * Returns:
3291 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
3292 * debounce time.
Felipe Balbic4b5be92010-05-26 14:42:23 -07003293 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003294int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07003295{
Felipe Balbic4b5be92010-05-26 14:42:23 -07003296 struct gpio_chip *chip;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03003297 unsigned long config;
Felipe Balbic4b5be92010-05-26 14:42:23 -07003298
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003299 VALIDATE_DESC(desc);
3300 chip = desc->gdev->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02003301
Mika Westerberg2956b5d2017-01-23 15:34:34 +03003302 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
YueHaibingd18fddf2020-01-16 22:29:27 +08003303 return gpio_set_config(chip, gpio_chip_hwgpio(desc), config);
Felipe Balbic4b5be92010-05-26 14:42:23 -07003304}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003305EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003306
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003307/**
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303308 * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
3309 * @desc: descriptor of the GPIO for which to configure persistence
3310 * @transitory: True to lose state on suspend or reset, false for persistence
3311 *
3312 * Returns:
3313 * 0 on success, otherwise a negative error code.
3314 */
3315int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
3316{
3317 struct gpio_chip *chip;
3318 unsigned long packed;
3319 int gpio;
3320 int rc;
3321
Vladimir Zapolskiy156dd392017-12-21 18:37:35 +02003322 VALIDATE_DESC(desc);
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303323 /*
3324 * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
3325 * persistence state.
3326 */
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +02003327 assign_bit(FLAG_TRANSITORY, &desc->flags, transitory);
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303328
3329 /* If the driver supports it, set the persistence state now */
3330 chip = desc->gdev->chip;
3331 if (!chip->set_config)
3332 return 0;
3333
3334 packed = pinconf_to_config_packed(PIN_CONFIG_PERSIST_STATE,
3335 !transitory);
3336 gpio = gpio_chip_hwgpio(desc);
YueHaibingd18fddf2020-01-16 22:29:27 +08003337 rc = gpio_set_config(chip, gpio, packed);
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303338 if (rc == -ENOTSUPP) {
3339 dev_dbg(&desc->gdev->dev, "Persistence not supported for GPIO %d\n",
3340 gpio);
3341 return 0;
3342 }
3343
3344 return rc;
3345}
3346EXPORT_SYMBOL_GPL(gpiod_set_transitory);
3347
3348/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003349 * gpiod_is_active_low - test whether a GPIO is active-low or not
3350 * @desc: the gpio descriptor to test
3351 *
3352 * Returns 1 if the GPIO is active-low, 0 otherwise.
3353 */
3354int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003355{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003356 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003357 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003358}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003359EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08003360
3361/* I/O calls are only valid after configuration completed; the relevant
3362 * "is this a valid GPIO" error checks should already have been done.
3363 *
3364 * "Get" operations are often inlinable as reading a pin value register,
3365 * and masking the relevant bit in that register.
3366 *
3367 * When "set" operations are inlinable, they involve writing that mask to
3368 * one register to set a low value, or a different register to set it high.
3369 * Otherwise locking is needed, so there may be little value to inlining.
3370 *
3371 *------------------------------------------------------------------------
3372 *
3373 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
3374 * have requested the GPIO. That can include implicit requesting by
3375 * a direction setting call. Marking a gpio as requested locks its chip
3376 * in memory, guaranteeing that these table lookups need no more locking
3377 * and that gpiochip_remove() will fail.
3378 *
3379 * REVISIT when debugging, consider adding some instrumentation to ensure
3380 * that the GPIO was actually requested.
3381 */
3382
Linus Walleijfac9d882017-09-26 20:58:28 +02003383static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08003384{
3385 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003386 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003387 int value;
David Brownelld2876d02008-02-04 22:28:20 -08003388
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003389 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003390 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003391 value = chip->get ? chip->get(chip, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01003392 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003393 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06003394 return value;
David Brownelld2876d02008-02-04 22:28:20 -08003395}
Alexandre Courbot372e7222013-02-03 01:29:29 +09003396
Lukas Wunnereec1d562017-10-12 12:40:10 +02003397static int gpio_chip_get_multiple(struct gpio_chip *chip,
3398 unsigned long *mask, unsigned long *bits)
3399{
3400 if (chip->get_multiple) {
3401 return chip->get_multiple(chip, mask, bits);
3402 } else if (chip->get) {
3403 int i, value;
3404
3405 for_each_set_bit(i, mask, chip->ngpio) {
3406 value = chip->get(chip, i);
3407 if (value < 0)
3408 return value;
3409 __assign_bit(i, bits, value);
3410 }
3411 return 0;
3412 }
3413 return -EIO;
3414}
3415
3416int gpiod_get_array_value_complex(bool raw, bool can_sleep,
3417 unsigned int array_size,
3418 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003419 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003420 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02003421{
Linus Walleijd377f562019-07-16 11:11:45 +02003422 int ret, i = 0;
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003423
3424 /*
3425 * Validate array_info against desc_array and its size.
3426 * It should immediately follow desc_array if both
3427 * have been obtained from the same gpiod_get_array() call.
3428 */
3429 if (array_info && array_info->desc == desc_array &&
3430 array_size <= array_info->size &&
3431 (void *)array_info == desc_array + array_info->size) {
3432 if (!can_sleep)
3433 WARN_ON(array_info->chip->can_sleep);
3434
Linus Walleijd377f562019-07-16 11:11:45 +02003435 ret = gpio_chip_get_multiple(array_info->chip,
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003436 array_info->get_mask,
3437 value_bitmap);
Linus Walleijd377f562019-07-16 11:11:45 +02003438 if (ret)
3439 return ret;
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003440
3441 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
3442 bitmap_xor(value_bitmap, value_bitmap,
3443 array_info->invert_mask, array_size);
3444
3445 if (bitmap_full(array_info->get_mask, array_size))
3446 return 0;
3447
3448 i = find_first_zero_bit(array_info->get_mask, array_size);
3449 } else {
3450 array_info = NULL;
3451 }
Lukas Wunnereec1d562017-10-12 12:40:10 +02003452
3453 while (i < array_size) {
3454 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Laura Abbott30277432018-05-21 10:57:07 -07003455 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
3456 unsigned long *mask, *bits;
Lukas Wunnereec1d562017-10-12 12:40:10 +02003457 int first, j, ret;
3458
Laura Abbott30277432018-05-21 10:57:07 -07003459 if (likely(chip->ngpio <= FASTPATH_NGPIO)) {
3460 mask = fastpath;
3461 } else {
3462 mask = kmalloc_array(2 * BITS_TO_LONGS(chip->ngpio),
3463 sizeof(*mask),
3464 can_sleep ? GFP_KERNEL : GFP_ATOMIC);
3465 if (!mask)
3466 return -ENOMEM;
3467 }
3468
3469 bits = mask + BITS_TO_LONGS(chip->ngpio);
3470 bitmap_zero(mask, chip->ngpio);
3471
Lukas Wunnereec1d562017-10-12 12:40:10 +02003472 if (!can_sleep)
3473 WARN_ON(chip->can_sleep);
3474
3475 /* collect all inputs belonging to the same chip */
3476 first = i;
Lukas Wunnereec1d562017-10-12 12:40:10 +02003477 do {
3478 const struct gpio_desc *desc = desc_array[i];
3479 int hwgpio = gpio_chip_hwgpio(desc);
3480
3481 __set_bit(hwgpio, mask);
3482 i++;
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003483
3484 if (array_info)
Janusz Krzysztofik35ae7f92018-09-24 01:53:35 +02003485 i = find_next_zero_bit(array_info->get_mask,
3486 array_size, i);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003487 } while ((i < array_size) &&
3488 (desc_array[i]->gdev->chip == chip));
3489
3490 ret = gpio_chip_get_multiple(chip, mask, bits);
Laura Abbott30277432018-05-21 10:57:07 -07003491 if (ret) {
3492 if (mask != fastpath)
3493 kfree(mask);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003494 return ret;
Laura Abbott30277432018-05-21 10:57:07 -07003495 }
Lukas Wunnereec1d562017-10-12 12:40:10 +02003496
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003497 for (j = first; j < i; ) {
Lukas Wunnereec1d562017-10-12 12:40:10 +02003498 const struct gpio_desc *desc = desc_array[j];
3499 int hwgpio = gpio_chip_hwgpio(desc);
3500 int value = test_bit(hwgpio, bits);
3501
3502 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3503 value = !value;
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003504 __assign_bit(j, value_bitmap, value);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003505 trace_gpio_value(desc_to_gpio(desc), 1, value);
Janusz Krzysztofik799d5eb2018-09-29 14:20:22 +02003506 j++;
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003507
3508 if (array_info)
Janusz Krzysztofik35ae7f92018-09-24 01:53:35 +02003509 j = find_next_zero_bit(array_info->get_mask, i,
3510 j);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003511 }
Laura Abbott30277432018-05-21 10:57:07 -07003512
3513 if (mask != fastpath)
3514 kfree(mask);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003515 }
3516 return 0;
3517}
3518
David Brownelld2876d02008-02-04 22:28:20 -08003519/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003520 * gpiod_get_raw_value() - return a gpio's raw value
3521 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08003522 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003523 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003524 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003525 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003526 * This function can be called from contexts where we cannot sleep, and will
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003527 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003528 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003529int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003530{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003531 VALIDATE_DESC(desc);
Geert Uytterhoeven3285170f2019-07-01 16:27:38 +02003532 /* Should be using gpiod_get_raw_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003533 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02003534 return gpiod_get_raw_value_commit(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003535}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003536EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08003537
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003538/**
3539 * gpiod_get_value() - return a gpio's value
3540 * @desc: gpio whose value will be returned
3541 *
3542 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003543 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003544 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003545 * This function can be called from contexts where we cannot sleep, and will
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003546 * complain if the GPIO chip functions potentially sleep.
3547 */
3548int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08003549{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003550 int value;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003551
3552 VALIDATE_DESC(desc);
Geert Uytterhoeven3285170f2019-07-01 16:27:38 +02003553 /* Should be using gpiod_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003554 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003555
Linus Walleijfac9d882017-09-26 20:58:28 +02003556 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003557 if (value < 0)
3558 return value;
3559
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003560 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3561 value = !value;
3562
3563 return value;
David Brownelld2876d02008-02-04 22:28:20 -08003564}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003565EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08003566
Lukas Wunnereec1d562017-10-12 12:40:10 +02003567/**
3568 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003569 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02003570 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003571 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003572 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02003573 *
3574 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3575 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3576 * else an error code.
3577 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003578 * This function can be called from contexts where we cannot sleep,
Lukas Wunnereec1d562017-10-12 12:40:10 +02003579 * and it will complain if the GPIO chip functions potentially sleep.
3580 */
3581int gpiod_get_raw_array_value(unsigned int array_size,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003582 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003583 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003584 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02003585{
3586 if (!desc_array)
3587 return -EINVAL;
3588 return gpiod_get_array_value_complex(true, false, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003589 desc_array, array_info,
3590 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003591}
3592EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
3593
3594/**
3595 * gpiod_get_array_value() - read values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003596 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02003597 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003598 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003599 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02003600 *
3601 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3602 * into account. Return 0 in case of success, else an error code.
3603 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003604 * This function can be called from contexts where we cannot sleep,
Lukas Wunnereec1d562017-10-12 12:40:10 +02003605 * and it will complain if the GPIO chip functions potentially sleep.
3606 */
3607int gpiod_get_array_value(unsigned int array_size,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003608 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003609 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003610 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02003611{
3612 if (!desc_array)
3613 return -EINVAL;
3614 return gpiod_get_array_value_complex(false, false, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003615 desc_array, array_info,
3616 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003617}
3618EXPORT_SYMBOL_GPL(gpiod_get_array_value);
3619
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303620/*
Linus Walleijfac9d882017-09-26 20:58:28 +02003621 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003622 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07003623 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303624 */
Linus Walleijfac9d882017-09-26 20:58:28 +02003625static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303626{
Linus Walleijd377f562019-07-16 11:11:45 +02003627 int ret = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003628 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003629 int offset = gpio_chip_hwgpio(desc);
3630
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303631 if (value) {
Linus Walleijd377f562019-07-16 11:11:45 +02003632 ret = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303633 } else {
Linus Walleijd377f562019-07-16 11:11:45 +02003634 ret = chip->direction_output(chip, offset, 0);
3635 if (!ret)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003636 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303637 }
Linus Walleijd377f562019-07-16 11:11:45 +02003638 trace_gpio_direction(desc_to_gpio(desc), value, ret);
3639 if (ret < 0)
Mark Brown6424de52013-09-09 10:33:49 +01003640 gpiod_err(desc,
3641 "%s: Error in set_value for open drain err %d\n",
Linus Walleijd377f562019-07-16 11:11:45 +02003642 __func__, ret);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303643}
3644
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303645/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003646 * _gpio_set_open_source_value() - Set the open source gpio's value.
3647 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07003648 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303649 */
Linus Walleijfac9d882017-09-26 20:58:28 +02003650static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303651{
Linus Walleijd377f562019-07-16 11:11:45 +02003652 int ret = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003653 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003654 int offset = gpio_chip_hwgpio(desc);
3655
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303656 if (value) {
Linus Walleijd377f562019-07-16 11:11:45 +02003657 ret = chip->direction_output(chip, offset, 1);
3658 if (!ret)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003659 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303660 } else {
Linus Walleijd377f562019-07-16 11:11:45 +02003661 ret = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303662 }
Linus Walleijd377f562019-07-16 11:11:45 +02003663 trace_gpio_direction(desc_to_gpio(desc), !value, ret);
3664 if (ret < 0)
Mark Brown6424de52013-09-09 10:33:49 +01003665 gpiod_err(desc,
3666 "%s: Error in set_value for open source err %d\n",
Linus Walleijd377f562019-07-16 11:11:45 +02003667 __func__, ret);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303668}
3669
Linus Walleijfac9d882017-09-26 20:58:28 +02003670static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08003671{
3672 struct gpio_chip *chip;
3673
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003674 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003675 trace_gpio_value(desc_to_gpio(desc), 0, value);
Linus Walleij02e47982017-09-26 21:20:23 +02003676 chip->set(chip, gpio_chip_hwgpio(desc), value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003677}
3678
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003679/*
3680 * set multiple outputs on the same chip;
3681 * use the chip's set_multiple function if available;
3682 * otherwise set the outputs sequentially;
3683 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
3684 * defines which outputs are to be changed
3685 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
3686 * defines the values the outputs specified by mask are to be set to
3687 */
3688static void gpio_chip_set_multiple(struct gpio_chip *chip,
3689 unsigned long *mask, unsigned long *bits)
3690{
3691 if (chip->set_multiple) {
3692 chip->set_multiple(chip, mask, bits);
3693 } else {
Andy Shevchenko5e4e6fb2017-01-03 19:01:17 +02003694 unsigned int i;
3695
3696 /* set outputs if the corresponding mask bit is set */
3697 for_each_set_bit(i, mask, chip->ngpio)
3698 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003699 }
3700}
3701
Laura Abbott30277432018-05-21 10:57:07 -07003702int gpiod_set_array_value_complex(bool raw, bool can_sleep,
Geert Uytterhoeven3c940662018-09-27 13:38:10 +02003703 unsigned int array_size,
3704 struct gpio_desc **desc_array,
3705 struct gpio_array *array_info,
3706 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003707{
3708 int i = 0;
3709
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003710 /*
3711 * Validate array_info against desc_array and its size.
3712 * It should immediately follow desc_array if both
3713 * have been obtained from the same gpiod_get_array() call.
3714 */
3715 if (array_info && array_info->desc == desc_array &&
3716 array_size <= array_info->size &&
3717 (void *)array_info == desc_array + array_info->size) {
3718 if (!can_sleep)
3719 WARN_ON(array_info->chip->can_sleep);
3720
3721 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
3722 bitmap_xor(value_bitmap, value_bitmap,
3723 array_info->invert_mask, array_size);
3724
3725 gpio_chip_set_multiple(array_info->chip, array_info->set_mask,
3726 value_bitmap);
3727
3728 if (bitmap_full(array_info->set_mask, array_size))
3729 return 0;
3730
3731 i = find_first_zero_bit(array_info->set_mask, array_size);
3732 } else {
3733 array_info = NULL;
3734 }
3735
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003736 while (i < array_size) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003737 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Laura Abbott30277432018-05-21 10:57:07 -07003738 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
3739 unsigned long *mask, *bits;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003740 int count = 0;
3741
Laura Abbott30277432018-05-21 10:57:07 -07003742 if (likely(chip->ngpio <= FASTPATH_NGPIO)) {
3743 mask = fastpath;
3744 } else {
3745 mask = kmalloc_array(2 * BITS_TO_LONGS(chip->ngpio),
3746 sizeof(*mask),
3747 can_sleep ? GFP_KERNEL : GFP_ATOMIC);
3748 if (!mask)
3749 return -ENOMEM;
3750 }
3751
3752 bits = mask + BITS_TO_LONGS(chip->ngpio);
3753 bitmap_zero(mask, chip->ngpio);
3754
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003755 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003756 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003757
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003758 do {
3759 struct gpio_desc *desc = desc_array[i];
3760 int hwgpio = gpio_chip_hwgpio(desc);
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003761 int value = test_bit(i, value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003762
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003763 /*
3764 * Pins applicable for fast input but not for
3765 * fast output processing may have been already
3766 * inverted inside the fast path, skip them.
3767 */
3768 if (!raw && !(array_info &&
3769 test_bit(i, array_info->invert_mask)) &&
3770 test_bit(FLAG_ACTIVE_LOW, &desc->flags))
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003771 value = !value;
3772 trace_gpio_value(desc_to_gpio(desc), 0, value);
3773 /*
3774 * collect all normal outputs belonging to the same chip
3775 * open drain and open source outputs are set individually
3776 */
Linus Walleij02e47982017-09-26 21:20:23 +02003777 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02003778 gpio_set_open_drain_value_commit(desc, value);
Linus Walleij02e47982017-09-26 21:20:23 +02003779 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02003780 gpio_set_open_source_value_commit(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003781 } else {
3782 __set_bit(hwgpio, mask);
Andy Shevchenko4fc5bfe2019-12-04 21:42:29 +02003783 __assign_bit(hwgpio, bits, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003784 count++;
3785 }
3786 i++;
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003787
3788 if (array_info)
Janusz Krzysztofik35ae7f92018-09-24 01:53:35 +02003789 i = find_next_zero_bit(array_info->set_mask,
3790 array_size, i);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003791 } while ((i < array_size) &&
3792 (desc_array[i]->gdev->chip == chip));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003793 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003794 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003795 gpio_chip_set_multiple(chip, mask, bits);
Laura Abbott30277432018-05-21 10:57:07 -07003796
3797 if (mask != fastpath)
3798 kfree(mask);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003799 }
Laura Abbott30277432018-05-21 10:57:07 -07003800 return 0;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003801}
3802
David Brownelld2876d02008-02-04 22:28:20 -08003803/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003804 * gpiod_set_raw_value() - assign a gpio's raw value
3805 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08003806 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08003807 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003808 * Set the raw value of the GPIO, i.e. the value of its physical line without
3809 * regard for its ACTIVE_LOW status.
3810 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003811 * This function can be called from contexts where we cannot sleep, and will
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003812 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003813 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003814void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003815{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003816 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven3285170f2019-07-01 16:27:38 +02003817 /* Should be using gpiod_set_raw_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003818 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02003819 gpiod_set_raw_value_commit(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08003820}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003821EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08003822
3823/**
Geert Uytterhoeven1e77fc82018-01-09 19:08:21 +01003824 * gpiod_set_value_nocheck() - set a GPIO line value without checking
3825 * @desc: the descriptor to set the value on
3826 * @value: value to set
3827 *
3828 * This sets the value of a GPIO line backing a descriptor, applying
3829 * different semantic quirks like active low and open drain/source
3830 * handling.
3831 */
3832static void gpiod_set_value_nocheck(struct gpio_desc *desc, int value)
3833{
3834 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3835 value = !value;
3836 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
3837 gpio_set_open_drain_value_commit(desc, value);
3838 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
3839 gpio_set_open_source_value_commit(desc, value);
3840 else
3841 gpiod_set_raw_value_commit(desc, value);
3842}
3843
3844/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003845 * gpiod_set_value() - assign a gpio's value
3846 * @desc: gpio whose value will be assigned
3847 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08003848 *
Linus Walleij02e47982017-09-26 21:20:23 +02003849 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
3850 * OPEN_DRAIN and OPEN_SOURCE flags into account.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003851 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003852 * This function can be called from contexts where we cannot sleep, and will
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003853 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003854 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003855void gpiod_set_value(struct gpio_desc *desc, int value)
3856{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003857 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven3285170f2019-07-01 16:27:38 +02003858 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003859 WARN_ON(desc->gdev->chip->can_sleep);
Geert Uytterhoeven1e77fc82018-01-09 19:08:21 +01003860 gpiod_set_value_nocheck(desc, value);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003861}
3862EXPORT_SYMBOL_GPL(gpiod_set_value);
3863
3864/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003865 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003866 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003867 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003868 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003869 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003870 *
3871 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3872 * without regard for their ACTIVE_LOW status.
3873 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003874 * This function can be called from contexts where we cannot sleep, and will
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003875 * complain if the GPIO chip functions potentially sleep.
3876 */
Laura Abbott30277432018-05-21 10:57:07 -07003877int gpiod_set_raw_array_value(unsigned int array_size,
Geert Uytterhoeven3c940662018-09-27 13:38:10 +02003878 struct gpio_desc **desc_array,
3879 struct gpio_array *array_info,
3880 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003881{
3882 if (!desc_array)
Laura Abbott30277432018-05-21 10:57:07 -07003883 return -EINVAL;
3884 return gpiod_set_array_value_complex(true, false, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003885 desc_array, array_info, value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003886}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003887EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003888
3889/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003890 * gpiod_set_array_value() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003891 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003892 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003893 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003894 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003895 *
3896 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3897 * into account.
3898 *
Geert Uytterhoeven827a9b82019-07-01 16:28:09 +02003899 * This function can be called from contexts where we cannot sleep, and will
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003900 * complain if the GPIO chip functions potentially sleep.
3901 */
Geert Uytterhoevencf9af0d2018-09-27 13:38:09 +02003902int gpiod_set_array_value(unsigned int array_size,
3903 struct gpio_desc **desc_array,
3904 struct gpio_array *array_info,
3905 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003906{
3907 if (!desc_array)
Geert Uytterhoevencf9af0d2018-09-27 13:38:09 +02003908 return -EINVAL;
3909 return gpiod_set_array_value_complex(false, false, array_size,
3910 desc_array, array_info,
3911 value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003912}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003913EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003914
3915/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003916 * gpiod_cansleep() - report whether gpio value access may sleep
3917 * @desc: gpio to check
3918 *
3919 */
3920int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003921{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003922 VALIDATE_DESC(desc);
3923 return desc->gdev->chip->can_sleep;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003924}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003925EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08003926
David Brownell0f6d5042008-10-15 22:03:14 -07003927/**
Linus Walleij90b39402e2018-06-01 13:21:27 +02003928 * gpiod_set_consumer_name() - set the consumer name for the descriptor
3929 * @desc: gpio to set the consumer name on
3930 * @name: the new consumer name
3931 */
Muchun Song18534df2018-11-01 21:12:50 +08003932int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name)
Linus Walleij90b39402e2018-06-01 13:21:27 +02003933{
Muchun Song18534df2018-11-01 21:12:50 +08003934 VALIDATE_DESC(desc);
3935 if (name) {
3936 name = kstrdup_const(name, GFP_KERNEL);
3937 if (!name)
3938 return -ENOMEM;
3939 }
3940
3941 kfree_const(desc->label);
3942 desc_set_label(desc, name);
3943
3944 return 0;
Linus Walleij90b39402e2018-06-01 13:21:27 +02003945}
3946EXPORT_SYMBOL_GPL(gpiod_set_consumer_name);
3947
3948/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003949 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
3950 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07003951 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003952 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
3953 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07003954 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003955int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07003956{
Linus Walleij4c37ce82016-05-02 13:13:10 +02003957 struct gpio_chip *chip;
3958 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07003959
Linus Walleij79bb71b2016-06-15 22:57:38 +02003960 /*
3961 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
3962 * requires this function to not return zero on an invalid descriptor
3963 * but rather a negative error number.
3964 */
Linus Walleijbfbbe442016-06-16 11:55:55 +02003965 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
Linus Walleij79bb71b2016-06-15 22:57:38 +02003966 return -EINVAL;
3967
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003968 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003969 offset = gpio_chip_hwgpio(desc);
Linus Walleij4c37ce82016-05-02 13:13:10 +02003970 if (chip->to_irq) {
3971 int retirq = chip->to_irq(chip, offset);
3972
3973 /* Zero means NO_IRQ */
3974 if (!retirq)
3975 return -ENXIO;
3976
3977 return retirq;
3978 }
3979 return -ENXIO;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003980}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003981EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003982
Linus Walleijd468bf92013-09-24 11:54:38 +02003983/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003984 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003985 * @chip: the chip the GPIO to lock belongs to
3986 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02003987 *
3988 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08003989 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08003990 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003991int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08003992{
Linus Walleij9c102802016-05-25 10:56:03 +02003993 struct gpio_desc *desc;
Linus Walleijd468bf92013-09-24 11:54:38 +02003994
Linus Walleij9c102802016-05-25 10:56:03 +02003995 desc = gpiochip_get_desc(chip, offset);
3996 if (IS_ERR(desc))
3997 return PTR_ERR(desc);
3998
Linus Walleij60f83392016-11-12 15:01:09 +01003999 /*
4000 * If it's fast: flush the direction setting if something changed
4001 * behind our back
4002 */
4003 if (!chip->can_sleep && chip->get_direction) {
Andy Shevchenko80956792018-07-09 21:47:21 +03004004 int dir = gpiod_get_direction(desc);
Linus Walleij9c102802016-05-25 10:56:03 +02004005
Andy Shevchenko36b31272018-07-03 03:38:31 +03004006 if (dir < 0) {
4007 chip_err(chip, "%s: cannot get GPIO direction\n",
4008 __func__);
4009 return dir;
4010 }
Linus Walleij9c102802016-05-25 10:56:03 +02004011 }
4012
4013 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09004014 chip_err(chip,
Andy Shevchenkob1911712018-07-03 03:39:03 +03004015 "%s: tried to flag a GPIO set as output for IRQ\n",
4016 __func__);
Linus Walleijd468bf92013-09-24 11:54:38 +02004017 return -EIO;
4018 }
4019
Linus Walleij9c102802016-05-25 10:56:03 +02004020 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
Hans Verkuil4e9439d2018-09-08 11:23:16 +02004021 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
Linus Walleij3940c342016-11-14 00:09:07 +01004022
4023 /*
4024 * If the consumer has not set up a label (such as when the
4025 * IRQ is referenced from .to_irq()) we set up a label here
4026 * so it is clear this is used as an interrupt.
4027 */
4028 if (!desc->label)
4029 desc_set_label(desc, "interrupt");
4030
Linus Walleijd468bf92013-09-24 11:54:38 +02004031 return 0;
4032}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09004033EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02004034
Linus Walleijd468bf92013-09-24 11:54:38 +02004035/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09004036 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09004037 * @chip: the chip the GPIO to lock belongs to
4038 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02004039 *
4040 * This is used directly by GPIO drivers that want to indicate
4041 * that a certain GPIO is no longer used exclusively for IRQ.
4042 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09004043void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02004044{
Linus Walleij3940c342016-11-14 00:09:07 +01004045 struct gpio_desc *desc;
4046
4047 desc = gpiochip_get_desc(chip, offset);
4048 if (IS_ERR(desc))
Linus Walleijd468bf92013-09-24 11:54:38 +02004049 return;
4050
Linus Walleij3940c342016-11-14 00:09:07 +01004051 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
Hans Verkuil4e9439d2018-09-08 11:23:16 +02004052 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
Linus Walleij3940c342016-11-14 00:09:07 +01004053
4054 /* If we only had this marking, erase it */
4055 if (desc->label && !strcmp(desc->label, "interrupt"))
4056 desc_set_label(desc, NULL);
Linus Walleijd468bf92013-09-24 11:54:38 +02004057}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09004058EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02004059
Hans Verkuil4e9439d2018-09-08 11:23:16 +02004060void gpiochip_disable_irq(struct gpio_chip *chip, unsigned int offset)
4061{
4062 struct gpio_desc *desc = gpiochip_get_desc(chip, offset);
4063
4064 if (!IS_ERR(desc) &&
4065 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags)))
4066 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
4067}
4068EXPORT_SYMBOL_GPL(gpiochip_disable_irq);
4069
4070void gpiochip_enable_irq(struct gpio_chip *chip, unsigned int offset)
4071{
4072 struct gpio_desc *desc = gpiochip_get_desc(chip, offset);
4073
4074 if (!IS_ERR(desc) &&
4075 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) {
4076 WARN_ON(test_bit(FLAG_IS_OUT, &desc->flags));
4077 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
4078 }
4079}
4080EXPORT_SYMBOL_GPL(gpiochip_enable_irq);
4081
Linus Walleij6cee3822016-02-11 20:16:45 +01004082bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
4083{
4084 if (offset >= chip->ngpio)
4085 return false;
4086
4087 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
4088}
4089EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
4090
Hans Verkuil4e6b8232018-09-08 11:23:14 +02004091int gpiochip_reqres_irq(struct gpio_chip *chip, unsigned int offset)
4092{
4093 int ret;
4094
4095 if (!try_module_get(chip->gpiodev->owner))
4096 return -ENODEV;
4097
4098 ret = gpiochip_lock_as_irq(chip, offset);
4099 if (ret) {
4100 chip_err(chip, "unable to lock HW IRQ %u for IRQ\n", offset);
4101 module_put(chip->gpiodev->owner);
4102 return ret;
4103 }
4104 return 0;
4105}
4106EXPORT_SYMBOL_GPL(gpiochip_reqres_irq);
4107
4108void gpiochip_relres_irq(struct gpio_chip *chip, unsigned int offset)
4109{
4110 gpiochip_unlock_as_irq(chip, offset);
4111 module_put(chip->gpiodev->owner);
4112}
4113EXPORT_SYMBOL_GPL(gpiochip_relres_irq);
4114
Linus Walleij143b65d2016-02-16 15:41:42 +01004115bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
4116{
4117 if (offset >= chip->ngpio)
4118 return false;
4119
4120 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
4121}
4122EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
4123
4124bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
4125{
4126 if (offset >= chip->ngpio)
4127 return false;
4128
4129 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
4130}
4131EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
4132
Charles Keepax05f479b2017-05-23 15:47:29 +01004133bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset)
4134{
4135 if (offset >= chip->ngpio)
4136 return false;
4137
Andrew Jefferye10f72b2017-11-30 14:25:24 +10304138 return !test_bit(FLAG_TRANSITORY, &chip->gpiodev->descs[offset].flags);
Charles Keepax05f479b2017-05-23 15:47:29 +01004139}
4140EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
4141
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004142/**
4143 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
4144 * @desc: gpio whose value will be returned
4145 *
4146 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07004147 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004148 *
4149 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08004150 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004151int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08004152{
David Brownelld2876d02008-02-04 22:28:20 -08004153 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004154 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02004155 return gpiod_get_raw_value_commit(desc);
David Brownelld2876d02008-02-04 22:28:20 -08004156}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004157EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09004158
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004159/**
4160 * gpiod_get_value_cansleep() - return a gpio's value
4161 * @desc: gpio whose value will be returned
4162 *
4163 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07004164 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004165 *
4166 * This function is to be called from contexts that can sleep.
4167 */
4168int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09004169{
David Brownelld2876d02008-02-04 22:28:20 -08004170 int value;
David Brownelld2876d02008-02-04 22:28:20 -08004171
4172 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004173 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02004174 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07004175 if (value < 0)
4176 return value;
4177
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004178 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
4179 value = !value;
4180
David Brownelld2876d02008-02-04 22:28:20 -08004181 return value;
4182}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004183EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09004184
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004185/**
Lukas Wunnereec1d562017-10-12 12:40:10 +02004186 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004187 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02004188 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004189 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004190 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02004191 *
4192 * Read the raw values of the GPIOs, i.e. the values of the physical lines
4193 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
4194 * else an error code.
4195 *
4196 * This function is to be called from contexts that can sleep.
4197 */
4198int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
4199 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004200 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004201 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02004202{
4203 might_sleep_if(extra_checks);
4204 if (!desc_array)
4205 return -EINVAL;
4206 return gpiod_get_array_value_complex(true, true, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004207 desc_array, array_info,
4208 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02004209}
4210EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
4211
4212/**
4213 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004214 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02004215 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004216 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004217 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02004218 *
4219 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
4220 * into account. Return 0 in case of success, else an error code.
4221 *
4222 * This function is to be called from contexts that can sleep.
4223 */
4224int gpiod_get_array_value_cansleep(unsigned int array_size,
4225 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004226 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004227 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02004228{
4229 might_sleep_if(extra_checks);
4230 if (!desc_array)
4231 return -EINVAL;
4232 return gpiod_get_array_value_complex(false, true, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004233 desc_array, array_info,
4234 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02004235}
4236EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
4237
4238/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004239 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
4240 * @desc: gpio whose value will be assigned
4241 * @value: value to assign
4242 *
4243 * Set the raw value of the GPIO, i.e. the value of its physical line without
4244 * regard for its ACTIVE_LOW status.
4245 *
4246 * This function is to be called from contexts that can sleep.
4247 */
4248void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09004249{
David Brownelld2876d02008-02-04 22:28:20 -08004250 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004251 VALIDATE_DESC_VOID(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02004252 gpiod_set_raw_value_commit(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09004253}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004254EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09004255
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004256/**
4257 * gpiod_set_value_cansleep() - assign a gpio's value
4258 * @desc: gpio whose value will be assigned
4259 * @value: value to assign
4260 *
4261 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
4262 * account
4263 *
4264 * This function is to be called from contexts that can sleep.
4265 */
4266void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09004267{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004268 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004269 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1e77fc82018-01-09 19:08:21 +01004270 gpiod_set_value_nocheck(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08004271}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07004272EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08004273
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004274/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02004275 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004276 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004277 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004278 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004279 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004280 *
4281 * Set the raw values of the GPIOs, i.e. the values of the physical lines
4282 * without regard for their ACTIVE_LOW status.
4283 *
4284 * This function is to be called from contexts that can sleep.
4285 */
Laura Abbott30277432018-05-21 10:57:07 -07004286int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
Geert Uytterhoeven3c940662018-09-27 13:38:10 +02004287 struct gpio_desc **desc_array,
4288 struct gpio_array *array_info,
4289 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004290{
4291 might_sleep_if(extra_checks);
4292 if (!desc_array)
Laura Abbott30277432018-05-21 10:57:07 -07004293 return -EINVAL;
4294 return gpiod_set_array_value_complex(true, true, array_size, desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004295 array_info, value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004296}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02004297EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004298
4299/**
Dmitry Torokhov3946d182017-08-14 21:59:55 -07004300 * gpiod_add_lookup_tables() - register GPIO device consumers
4301 * @tables: list of tables of consumers to register
4302 * @n: number of tables in the list
4303 */
4304void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
4305{
4306 unsigned int i;
4307
4308 mutex_lock(&gpio_lookup_lock);
4309
4310 for (i = 0; i < n; i++)
4311 list_add_tail(&tables[i]->list, &gpio_lookup_list);
4312
4313 mutex_unlock(&gpio_lookup_lock);
4314}
4315
4316/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02004317 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004318 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004319 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02004320 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02004321 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004322 *
4323 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
4324 * into account.
4325 *
4326 * This function is to be called from contexts that can sleep.
4327 */
Geert Uytterhoevencf9af0d2018-09-27 13:38:09 +02004328int gpiod_set_array_value_cansleep(unsigned int array_size,
4329 struct gpio_desc **desc_array,
4330 struct gpio_array *array_info,
4331 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004332{
4333 might_sleep_if(extra_checks);
4334 if (!desc_array)
Geert Uytterhoevencf9af0d2018-09-27 13:38:09 +02004335 return -EINVAL;
4336 return gpiod_set_array_value_complex(false, true, array_size,
4337 desc_array, array_info,
4338 value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004339}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02004340EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01004341
4342/**
Alexandre Courbotad824782013-12-03 12:20:11 +09004343 * gpiod_add_lookup_table() - register GPIO device consumers
4344 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004345 */
Alexandre Courbotad824782013-12-03 12:20:11 +09004346void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004347{
4348 mutex_lock(&gpio_lookup_lock);
4349
Alexandre Courbotad824782013-12-03 12:20:11 +09004350 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004351
4352 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08004353}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02004354EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
David Brownelld2876d02008-02-04 22:28:20 -08004355
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05304356/**
4357 * gpiod_remove_lookup_table() - unregister GPIO device consumers
4358 * @table: table of consumers to unregister
4359 */
4360void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
4361{
4362 mutex_lock(&gpio_lookup_lock);
4363
4364 list_del(&table->list);
4365
4366 mutex_unlock(&gpio_lookup_lock);
4367}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02004368EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05304369
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02004370/**
4371 * gpiod_add_hogs() - register a set of GPIO hogs from machine code
4372 * @hogs: table of gpio hog entries with a zeroed sentinel at the end
4373 */
4374void gpiod_add_hogs(struct gpiod_hog *hogs)
4375{
4376 struct gpio_chip *chip;
4377 struct gpiod_hog *hog;
4378
4379 mutex_lock(&gpio_machine_hogs_mutex);
4380
4381 for (hog = &hogs[0]; hog->chip_label; hog++) {
4382 list_add_tail(&hog->list, &gpio_machine_hogs);
4383
4384 /*
4385 * The chip may have been registered earlier, so check if it
4386 * exists and, if so, try to hog the line now.
4387 */
4388 chip = find_chip_by_name(hog->chip_label);
4389 if (chip)
4390 gpiochip_machine_hog(chip, hog);
4391 }
4392
4393 mutex_unlock(&gpio_machine_hogs_mutex);
4394}
4395EXPORT_SYMBOL_GPL(gpiod_add_hogs);
4396
Alexandre Courbotad824782013-12-03 12:20:11 +09004397static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
4398{
4399 const char *dev_id = dev ? dev_name(dev) : NULL;
4400 struct gpiod_lookup_table *table;
4401
4402 mutex_lock(&gpio_lookup_lock);
4403
4404 list_for_each_entry(table, &gpio_lookup_list, list) {
4405 if (table->dev_id && dev_id) {
4406 /*
4407 * Valid strings on both ends, must be identical to have
4408 * a match
4409 */
4410 if (!strcmp(table->dev_id, dev_id))
4411 goto found;
4412 } else {
4413 /*
4414 * One of the pointers is NULL, so both must be to have
4415 * a match
4416 */
4417 if (dev_id == table->dev_id)
4418 goto found;
4419 }
4420 }
4421 table = NULL;
4422
4423found:
4424 mutex_unlock(&gpio_lookup_lock);
4425 return table;
4426}
4427
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004428static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Andy Shevchenkofed70262019-04-10 18:39:16 +03004429 unsigned int idx, unsigned long *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004430{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004431 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09004432 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004433 struct gpiod_lookup *p;
4434
Alexandre Courbotad824782013-12-03 12:20:11 +09004435 table = gpiod_find_lookup_table(dev);
4436 if (!table)
4437 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004438
Alexandre Courbotad824782013-12-03 12:20:11 +09004439 for (p = &table->table[0]; p->chip_label; p++) {
4440 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004441
Alexandre Courbotad824782013-12-03 12:20:11 +09004442 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004443 if (p->idx != idx)
4444 continue;
4445
Alexandre Courbotad824782013-12-03 12:20:11 +09004446 /* If the lookup entry has a con_id, require exact match */
4447 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
4448 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004449
Alexandre Courbotad824782013-12-03 12:20:11 +09004450 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004451
Alexandre Courbotad824782013-12-03 12:20:11 +09004452 if (!chip) {
Janusz Krzysztofik8853daf2018-07-04 00:18:19 +02004453 /*
4454 * As the lookup table indicates a chip with
4455 * p->chip_label should exist, assume it may
4456 * still appear later and let the interested
4457 * consumer be probed again or let the Deferred
4458 * Probe infrastructure handle the error.
4459 */
4460 dev_warn(dev, "cannot find GPIO chip %s, deferring\n",
4461 p->chip_label);
4462 return ERR_PTR(-EPROBE_DEFER);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004463 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004464
Alexandre Courbotad824782013-12-03 12:20:11 +09004465 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004466 dev_err(dev,
Geert Uytterhoevend935bd52019-11-27 10:59:19 +01004467 "requested GPIO %u (%u) is out of range [0..%u] for chip %s\n",
4468 idx, p->chip_hwnum, chip->ngpio - 1,
4469 chip->label);
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004470 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09004471 }
4472
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09004473 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09004474 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004475
4476 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09004477 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004478
4479 return desc;
4480}
4481
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004482static int platform_gpio_count(struct device *dev, const char *con_id)
4483{
4484 struct gpiod_lookup_table *table;
4485 struct gpiod_lookup *p;
4486 unsigned int count = 0;
4487
4488 table = gpiod_find_lookup_table(dev);
4489 if (!table)
4490 return -ENOENT;
4491
4492 for (p = &table->table[0]; p->chip_label; p++) {
4493 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
4494 (!con_id && !p->con_id))
4495 count++;
4496 }
4497 if (!count)
4498 return -ENOENT;
4499
4500 return count;
4501}
4502
4503/**
Dmitry Torokhov13949fa2019-09-12 20:22:39 -07004504 * fwnode_gpiod_get_index - obtain a GPIO from firmware node
4505 * @fwnode: handle of the firmware node
4506 * @con_id: function within the GPIO consumer
4507 * @index: index of the GPIO to obtain for the consumer
4508 * @flags: GPIO initialization flags
4509 * @label: label to attach to the requested GPIO
4510 *
4511 * This function can be used for drivers that get their configuration
4512 * from opaque firmware.
4513 *
4514 * The function properly finds the corresponding GPIO using whatever is the
4515 * underlying firmware interface and then makes sure that the GPIO
4516 * descriptor is requested before it is returned to the caller.
4517 *
4518 * Returns:
4519 * On successful request the GPIO pin is configured in accordance with
4520 * provided @flags.
4521 *
4522 * In case of error an ERR_PTR() is returned.
4523 */
4524struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
4525 const char *con_id, int index,
4526 enum gpiod_flags flags,
4527 const char *label)
4528{
4529 struct gpio_desc *desc;
4530 char prop_name[32]; /* 32 is max size of property name */
4531 unsigned int i;
4532
4533 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
4534 if (con_id)
4535 snprintf(prop_name, sizeof(prop_name), "%s-%s",
4536 con_id, gpio_suffixes[i]);
4537 else
4538 snprintf(prop_name, sizeof(prop_name), "%s",
4539 gpio_suffixes[i]);
4540
4541 desc = fwnode_get_named_gpiod(fwnode, prop_name, index, flags,
4542 label);
4543 if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT))
4544 break;
4545 }
4546
4547 return desc;
4548}
4549EXPORT_SYMBOL_GPL(fwnode_gpiod_get_index);
4550
4551/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004552 * gpiod_count - return the number of GPIOs associated with a device / function
4553 * or -ENOENT if no GPIO has been assigned to the requested function
4554 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4555 * @con_id: function within the GPIO consumer
4556 */
4557int gpiod_count(struct device *dev, const char *con_id)
4558{
4559 int count = -ENOENT;
4560
4561 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
Linus Walleijf626d6d2019-07-17 09:10:01 +02004562 count = of_gpio_get_count(dev, con_id);
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004563 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
4564 count = acpi_gpio_count(dev, con_id);
4565
4566 if (count < 0)
4567 count = platform_gpio_count(dev, con_id);
4568
4569 return count;
4570}
4571EXPORT_SYMBOL_GPL(gpiod_count);
4572
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004573/**
Thierry Reding08791622014-04-25 16:54:22 +02004574 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09004575 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004576 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004577 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004578 *
4579 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004580 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07004581 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004582 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004583struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004584 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004585{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004586 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004587}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004588EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004589
4590/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02004591 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
4592 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4593 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004594 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02004595 *
4596 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
4597 * the requested function it will return NULL. This is convenient for drivers
4598 * that need to handle optional GPIOs.
4599 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004600struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004601 const char *con_id,
4602 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02004603{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004604 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02004605}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004606EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02004607
Benoit Parrotf625d462015-02-02 11:44:44 -06004608
4609/**
4610 * gpiod_configure_flags - helper function to configure a given GPIO
4611 * @desc: gpio whose value will be assigned
4612 * @con_id: function within the GPIO consumer
Andy Shevchenkofed70262019-04-10 18:39:16 +03004613 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4614 * of_find_gpio() or of_get_gpio_hog()
Benoit Parrotf625d462015-02-02 11:44:44 -06004615 * @dflags: gpiod_flags - optional GPIO initialization flags
4616 *
4617 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
4618 * requested function and/or index, or another IS_ERR() code if an error
4619 * occurred while trying to acquire the GPIO.
4620 */
Andy Shevchenkoc29fd9e2017-05-23 20:03:16 +03004621int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Johan Hovold85b03b32016-07-03 18:32:05 +02004622 unsigned long lflags, enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06004623{
Linus Walleijd377f562019-07-16 11:11:45 +02004624 int ret;
Benoit Parrotf625d462015-02-02 11:44:44 -06004625
Johan Hovold85b03b32016-07-03 18:32:05 +02004626 if (lflags & GPIO_ACTIVE_LOW)
4627 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
Linus Walleijf926dfc2017-09-10 19:26:22 +02004628
Johan Hovold85b03b32016-07-03 18:32:05 +02004629 if (lflags & GPIO_OPEN_DRAIN)
4630 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
Linus Walleijf926dfc2017-09-10 19:26:22 +02004631 else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) {
4632 /*
4633 * This enforces open drain mode from the consumer side.
4634 * This is necessary for some busses like I2C, but the lookup
4635 * should *REALLY* have specified them as open drain in the
4636 * first place, so print a little warning here.
4637 */
4638 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
4639 gpiod_warn(desc,
4640 "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
4641 }
4642
Johan Hovold85b03b32016-07-03 18:32:05 +02004643 if (lflags & GPIO_OPEN_SOURCE)
4644 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Andrew Jefferye10f72b2017-11-30 14:25:24 +10304645
Thomas Petazzonid4499912019-02-07 17:28:58 +01004646 if ((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DOWN)) {
4647 gpiod_err(desc,
4648 "both pull-up and pull-down enabled, invalid configuration\n");
4649 return -EINVAL;
4650 }
4651
4652 if (lflags & GPIO_PULL_UP)
4653 set_bit(FLAG_PULL_UP, &desc->flags);
4654 else if (lflags & GPIO_PULL_DOWN)
4655 set_bit(FLAG_PULL_DOWN, &desc->flags);
4656
Linus Walleijd377f562019-07-16 11:11:45 +02004657 ret = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
4658 if (ret < 0)
4659 return ret;
Johan Hovold85b03b32016-07-03 18:32:05 +02004660
Benoit Parrotf625d462015-02-02 11:44:44 -06004661 /* No particular flag request, return here... */
4662 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
4663 pr_debug("no flags found for %s\n", con_id);
4664 return 0;
4665 }
4666
4667 /* Process flags */
4668 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
Linus Walleijd377f562019-07-16 11:11:45 +02004669 ret = gpiod_direction_output(desc,
Linus Walleijad177312016-11-13 23:02:44 +01004670 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
Benoit Parrotf625d462015-02-02 11:44:44 -06004671 else
Linus Walleijd377f562019-07-16 11:11:45 +02004672 ret = gpiod_direction_input(desc);
Benoit Parrotf625d462015-02-02 11:44:44 -06004673
Linus Walleijd377f562019-07-16 11:11:45 +02004674 return ret;
Benoit Parrotf625d462015-02-02 11:44:44 -06004675}
4676
Thierry Reding29a1f2332014-04-25 17:10:06 +02004677/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004678 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02004679 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004680 * @con_id: function within the GPIO consumer
4681 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004682 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004683 *
4684 * This variant of gpiod_get() allows to access GPIOs other than the first
4685 * defined one for functions that define several GPIOs.
4686 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004687 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
4688 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07004689 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004690 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004691struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004692 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004693 unsigned int idx,
4694 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004695{
Andy Shevchenko2d6c06f2019-04-10 18:39:17 +03004696 unsigned long lookupflags = GPIO_LOOKUP_FLAGS_DEFAULT;
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09004697 struct gpio_desc *desc = NULL;
Linus Walleijd377f562019-07-16 11:11:45 +02004698 int ret;
Linus Walleij7d18f0a2018-01-16 08:29:50 +01004699 /* Maybe we have a device name, maybe not */
4700 const char *devname = dev ? dev_name(dev) : "?";
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004701
4702 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
4703
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01004704 if (dev) {
4705 /* Using device tree? */
4706 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
4707 dev_dbg(dev, "using device tree for GPIO lookup\n");
4708 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
4709 } else if (ACPI_COMPANION(dev)) {
4710 dev_dbg(dev, "using ACPI for GPIO lookup\n");
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +03004711 desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags);
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01004712 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09004713 }
4714
4715 /*
4716 * Either we are not using DT or ACPI, or their lookup did not return
4717 * a result. In that case, use platform lookup as a fallback.
4718 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004719 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04004720 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004721 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004722 }
4723
4724 if (IS_ERR(desc)) {
Wang Dongsheng9d5a1f22018-02-27 00:12:13 -08004725 dev_dbg(dev, "No GPIO consumer %s found\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004726 return desc;
4727 }
4728
Linus Walleij7d18f0a2018-01-16 08:29:50 +01004729 /*
4730 * If a connection label was passed use that, else attempt to use
4731 * the device name as label
4732 */
Linus Walleijd377f562019-07-16 11:11:45 +02004733 ret = gpiod_request(desc, con_id ? con_id : devname);
4734 if (ret < 0) {
4735 if (ret == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE) {
Linus Walleijb0ce7b292018-10-12 14:54:12 +02004736 /*
4737 * This happens when there are several consumers for
4738 * the same GPIO line: we just return here without
4739 * further initialization. It is a bit if a hack.
4740 * This is necessary to support fixed regulators.
4741 *
4742 * FIXME: Make this more sane and safe.
4743 */
4744 dev_info(dev, "nonexclusive access to GPIO for %s\n",
4745 con_id ? con_id : devname);
4746 return desc;
4747 } else {
Linus Walleijd377f562019-07-16 11:11:45 +02004748 return ERR_PTR(ret);
Linus Walleijb0ce7b292018-10-12 14:54:12 +02004749 }
4750 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004751
Linus Walleijd377f562019-07-16 11:11:45 +02004752 ret = gpiod_configure_flags(desc, con_id, lookupflags, flags);
4753 if (ret < 0) {
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004754 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
4755 gpiod_put(desc);
Linus Walleijd377f562019-07-16 11:11:45 +02004756 return ERR_PTR(ret);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004757 }
4758
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004759 return desc;
4760}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004761EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004762
4763/**
Mika Westerberg40b73182014-10-21 13:33:59 +02004764 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
4765 * @fwnode: handle of the firmware node
4766 * @propname: name of the firmware property representing the GPIO
Linus Walleij6392cca2017-12-29 02:07:54 +01004767 * @index: index of the GPIO to obtain for the consumer
Andy Shevchenkoa264d102017-01-09 16:02:28 +02004768 * @dflags: GPIO initialization flags
Thierry Reding950d55f52017-07-24 16:57:22 +02004769 * @label: label to attach to the requested GPIO
Mika Westerberg40b73182014-10-21 13:33:59 +02004770 *
4771 * This function can be used for drivers that get their configuration
Linus Walleij6392cca2017-12-29 02:07:54 +01004772 * from opaque firmware.
Mika Westerberg40b73182014-10-21 13:33:59 +02004773 *
Linus Walleij6392cca2017-12-29 02:07:54 +01004774 * The function properly finds the corresponding GPIO using whatever is the
Mika Westerberg40b73182014-10-21 13:33:59 +02004775 * underlying firmware interface and then makes sure that the GPIO
4776 * descriptor is requested before it is returned to the caller.
4777 *
Thierry Reding950d55f52017-07-24 16:57:22 +02004778 * Returns:
Andy Shevchenkoff213782017-02-28 17:03:12 +02004779 * On successful request the GPIO pin is configured in accordance with
Andy Shevchenkoa264d102017-01-09 16:02:28 +02004780 * provided @dflags.
4781 *
Mika Westerberg40b73182014-10-21 13:33:59 +02004782 * In case of error an ERR_PTR() is returned.
4783 */
4784struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
Boris Brezillon537b94d2017-02-02 14:53:11 +01004785 const char *propname, int index,
Alexander Steinb2987d72017-01-12 17:39:24 +01004786 enum gpiod_flags dflags,
4787 const char *label)
Mika Westerberg40b73182014-10-21 13:33:59 +02004788{
Andy Shevchenko2d6c06f2019-04-10 18:39:17 +03004789 unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
Mika Westerberg40b73182014-10-21 13:33:59 +02004790 struct gpio_desc *desc = ERR_PTR(-ENODEV);
Mika Westerberg40b73182014-10-21 13:33:59 +02004791 int ret;
4792
4793 if (!fwnode)
4794 return ERR_PTR(-EINVAL);
4795
4796 if (is_of_node(fwnode)) {
Linus Walleij6392cca2017-12-29 02:07:54 +01004797 desc = gpiod_get_from_of_node(to_of_node(fwnode),
4798 propname, index,
4799 dflags,
4800 label);
4801 return desc;
Mika Westerberg40b73182014-10-21 13:33:59 +02004802 } else if (is_acpi_node(fwnode)) {
4803 struct acpi_gpio_info info;
4804
Boris Brezillon537b94d2017-02-02 14:53:11 +01004805 desc = acpi_node_get_gpiod(fwnode, propname, index, &info);
Linus Walleij6392cca2017-12-29 02:07:54 +01004806 if (IS_ERR(desc))
4807 return desc;
4808
4809 acpi_gpio_update_gpiod_flags(&dflags, &info);
Andy Shevchenko606be342019-04-10 18:39:20 +03004810 acpi_gpio_update_gpiod_lookup_flags(&lflags, &info);
Mika Westerberg40b73182014-10-21 13:33:59 +02004811 }
4812
Linus Walleij6392cca2017-12-29 02:07:54 +01004813 /* Currently only ACPI takes this path */
Alexander Steinb2987d72017-01-12 17:39:24 +01004814 ret = gpiod_request(desc, label);
Johan Hovold85b03b32016-07-03 18:32:05 +02004815 if (ret)
4816 return ERR_PTR(ret);
4817
Andy Shevchenkoa264d102017-01-09 16:02:28 +02004818 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
4819 if (ret < 0) {
4820 gpiod_put(desc);
4821 return ERR_PTR(ret);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03004822 }
4823
Mika Westerberg40b73182014-10-21 13:33:59 +02004824 return desc;
4825}
4826EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
4827
4828/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02004829 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
4830 * function
4831 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4832 * @con_id: function within the GPIO consumer
4833 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004834 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02004835 *
4836 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
4837 * specified index was assigned to the requested function it will return NULL.
4838 * This is convenient for drivers that need to handle optional GPIOs.
4839 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004840struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02004841 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004842 unsigned int index,
4843 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02004844{
4845 struct gpio_desc *desc;
4846
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004847 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02004848 if (IS_ERR(desc)) {
4849 if (PTR_ERR(desc) == -ENOENT)
4850 return NULL;
4851 }
4852
4853 return desc;
4854}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004855EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02004856
4857/**
Benoit Parrotf625d462015-02-02 11:44:44 -06004858 * gpiod_hog - Hog the specified GPIO desc given the provided flags
4859 * @desc: gpio whose value will be assigned
4860 * @name: gpio line name
Andy Shevchenkofed70262019-04-10 18:39:16 +03004861 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
4862 * of_find_gpio() or of_get_gpio_hog()
Benoit Parrotf625d462015-02-02 11:44:44 -06004863 * @dflags: gpiod_flags - optional GPIO initialization flags
4864 */
4865int gpiod_hog(struct gpio_desc *desc, const char *name,
4866 unsigned long lflags, enum gpiod_flags dflags)
4867{
4868 struct gpio_chip *chip;
4869 struct gpio_desc *local_desc;
4870 int hwnum;
Linus Walleijd377f562019-07-16 11:11:45 +02004871 int ret;
Benoit Parrotf625d462015-02-02 11:44:44 -06004872
4873 chip = gpiod_to_chip(desc);
4874 hwnum = gpio_chip_hwgpio(desc);
4875
Linus Walleij5923ea62019-04-26 14:40:18 +02004876 local_desc = gpiochip_request_own_desc(chip, hwnum, name,
4877 lflags, dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -06004878 if (IS_ERR(local_desc)) {
Linus Walleijd377f562019-07-16 11:11:45 +02004879 ret = PTR_ERR(local_desc);
Laxman Dewanganc31a5712016-03-11 19:13:21 +05304880 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
Linus Walleijd377f562019-07-16 11:11:45 +02004881 name, chip->label, hwnum, ret);
4882 return ret;
Benoit Parrotf625d462015-02-02 11:44:44 -06004883 }
4884
Benoit Parrotf625d462015-02-02 11:44:44 -06004885 /* Mark GPIO as hogged so it can be identified and removed later */
4886 set_bit(FLAG_IS_HOGGED, &desc->flags);
4887
4888 pr_info("GPIO line %d (%s) hogged as %s%s\n",
4889 desc_to_gpio(desc), name,
Bartosz Golaszewskib27f3002019-11-13 14:16:29 +01004890 (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
4891 (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ?
4892 (dflags & GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low" : "");
Benoit Parrotf625d462015-02-02 11:44:44 -06004893
4894 return 0;
4895}
4896
4897/**
4898 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
4899 * @chip: gpio chip to act on
Benoit Parrotf625d462015-02-02 11:44:44 -06004900 */
4901static void gpiochip_free_hogs(struct gpio_chip *chip)
4902{
4903 int id;
4904
4905 for (id = 0; id < chip->ngpio; id++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01004906 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
4907 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
Benoit Parrotf625d462015-02-02 11:44:44 -06004908 }
4909}
4910
4911/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004912 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
4913 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4914 * @con_id: function within the GPIO consumer
4915 * @flags: optional GPIO initialization flags
4916 *
4917 * This function acquires all the GPIOs defined under a given function.
4918 *
4919 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
4920 * no GPIO has been assigned to the requested function, or another IS_ERR()
4921 * code if an error occurred while trying to acquire the GPIOs.
4922 */
4923struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
4924 const char *con_id,
4925 enum gpiod_flags flags)
4926{
4927 struct gpio_desc *desc;
4928 struct gpio_descs *descs;
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004929 struct gpio_array *array_info = NULL;
4930 struct gpio_chip *chip;
4931 int count, bitmap_size;
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004932
4933 count = gpiod_count(dev, con_id);
4934 if (count < 0)
4935 return ERR_PTR(count);
4936
Kees Cookacafe7e2018-05-08 13:45:50 -07004937 descs = kzalloc(struct_size(descs, desc, count), GFP_KERNEL);
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004938 if (!descs)
4939 return ERR_PTR(-ENOMEM);
4940
4941 for (descs->ndescs = 0; descs->ndescs < count; ) {
4942 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
4943 if (IS_ERR(desc)) {
4944 gpiod_put_array(descs);
4945 return ERR_CAST(desc);
4946 }
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004947
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004948 descs->desc[descs->ndescs] = desc;
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004949
4950 chip = gpiod_to_chip(desc);
4951 /*
Janusz Krzysztofikc4c958a2018-09-24 01:53:36 +02004952 * If pin hardware number of array member 0 is also 0, select
4953 * its chip as a candidate for fast bitmap processing path.
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004954 */
Janusz Krzysztofikc4c958a2018-09-24 01:53:36 +02004955 if (descs->ndescs == 0 && gpio_chip_hwgpio(desc) == 0) {
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004956 struct gpio_descs *array;
4957
4958 bitmap_size = BITS_TO_LONGS(chip->ngpio > count ?
4959 chip->ngpio : count);
4960
4961 array = kzalloc(struct_size(descs, desc, count) +
4962 struct_size(array_info, invert_mask,
4963 3 * bitmap_size), GFP_KERNEL);
4964 if (!array) {
4965 gpiod_put_array(descs);
4966 return ERR_PTR(-ENOMEM);
4967 }
4968
4969 memcpy(array, descs,
4970 struct_size(descs, desc, descs->ndescs + 1));
4971 kfree(descs);
4972
4973 descs = array;
4974 array_info = (void *)(descs->desc + count);
4975 array_info->get_mask = array_info->invert_mask +
4976 bitmap_size;
4977 array_info->set_mask = array_info->get_mask +
4978 bitmap_size;
4979
4980 array_info->desc = descs->desc;
4981 array_info->size = count;
4982 array_info->chip = chip;
4983 bitmap_set(array_info->get_mask, descs->ndescs,
4984 count - descs->ndescs);
4985 bitmap_set(array_info->set_mask, descs->ndescs,
4986 count - descs->ndescs);
4987 descs->info = array_info;
4988 }
Janusz Krzysztofikc4c958a2018-09-24 01:53:36 +02004989 /* Unmark array members which don't belong to the 'fast' chip */
4990 if (array_info && array_info->chip != chip) {
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004991 __clear_bit(descs->ndescs, array_info->get_mask);
4992 __clear_bit(descs->ndescs, array_info->set_mask);
Janusz Krzysztofikc4c958a2018-09-24 01:53:36 +02004993 }
4994 /*
4995 * Detect array members which belong to the 'fast' chip
4996 * but their pins are not in hardware order.
4997 */
4998 else if (array_info &&
4999 gpio_chip_hwgpio(desc) != descs->ndescs) {
5000 /*
5001 * Don't use fast path if all array members processed so
5002 * far belong to the same chip as this one but its pin
5003 * hardware number is different from its array index.
5004 */
5005 if (bitmap_full(array_info->get_mask, descs->ndescs)) {
5006 array_info = NULL;
5007 } else {
5008 __clear_bit(descs->ndescs,
5009 array_info->get_mask);
5010 __clear_bit(descs->ndescs,
5011 array_info->set_mask);
5012 }
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02005013 } else if (array_info) {
5014 /* Exclude open drain or open source from fast output */
5015 if (gpiochip_line_is_open_drain(chip, descs->ndescs) ||
5016 gpiochip_line_is_open_source(chip, descs->ndescs))
5017 __clear_bit(descs->ndescs,
5018 array_info->set_mask);
5019 /* Identify 'fast' pins which require invertion */
5020 if (gpiod_is_active_low(desc))
5021 __set_bit(descs->ndescs,
5022 array_info->invert_mask);
5023 }
5024
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01005025 descs->ndescs++;
5026 }
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02005027 if (array_info)
5028 dev_dbg(dev,
5029 "GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n",
5030 array_info->chip->label, array_info->size,
5031 *array_info->get_mask, *array_info->set_mask,
5032 *array_info->invert_mask);
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01005033 return descs;
5034}
5035EXPORT_SYMBOL_GPL(gpiod_get_array);
5036
5037/**
5038 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
5039 * function
5040 * @dev: GPIO consumer, can be NULL for system-global GPIOs
5041 * @con_id: function within the GPIO consumer
5042 * @flags: optional GPIO initialization flags
5043 *
5044 * This is equivalent to gpiod_get_array(), except that when no GPIO was
5045 * assigned to the requested function it will return NULL.
5046 */
5047struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
5048 const char *con_id,
5049 enum gpiod_flags flags)
5050{
5051 struct gpio_descs *descs;
5052
5053 descs = gpiod_get_array(dev, con_id, flags);
5054 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
5055 return NULL;
5056
5057 return descs;
5058}
5059EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
5060
5061/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07005062 * gpiod_put - dispose of a GPIO descriptor
5063 * @desc: GPIO descriptor to dispose of
5064 *
5065 * No descriptor can be used after gpiod_put() has been called on it.
5066 */
5067void gpiod_put(struct gpio_desc *desc)
5068{
Andy Shevchenko1d7765b2019-03-26 17:21:14 +02005069 if (desc)
5070 gpiod_free(desc);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07005071}
5072EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08005073
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01005074/**
5075 * gpiod_put_array - dispose of multiple GPIO descriptors
5076 * @descs: struct gpio_descs containing an array of descriptors
5077 */
5078void gpiod_put_array(struct gpio_descs *descs)
5079{
5080 unsigned int i;
5081
5082 for (i = 0; i < descs->ndescs; i++)
5083 gpiod_put(descs->desc[i]);
5084
5085 kfree(descs);
5086}
5087EXPORT_SYMBOL_GPL(gpiod_put_array);
5088
Linus Walleij3c702e92015-10-21 15:29:53 +02005089static int __init gpiolib_dev_init(void)
5090{
5091 int ret;
5092
5093 /* Register GPIO sysfs bus */
Andy Shevchenkob1911712018-07-03 03:39:03 +03005094 ret = bus_register(&gpio_bus_type);
Linus Walleij3c702e92015-10-21 15:29:53 +02005095 if (ret < 0) {
5096 pr_err("gpiolib: could not register GPIO bus type\n");
5097 return ret;
5098 }
5099
Geert Uytterhoevenddd88912019-11-27 09:42:47 +01005100 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, GPIOCHIP_NAME);
Linus Walleij3c702e92015-10-21 15:29:53 +02005101 if (ret < 0) {
5102 pr_err("gpiolib: failed to allocate char dev region\n");
5103 bus_unregister(&gpio_bus_type);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07005104 } else {
5105 gpiolib_initialized = true;
5106 gpiochip_setup_devs();
Linus Walleij3c702e92015-10-21 15:29:53 +02005107 }
5108 return ret;
5109}
5110core_initcall(gpiolib_dev_init);
5111
David Brownelld2876d02008-02-04 22:28:20 -08005112#ifdef CONFIG_DEBUG_FS
5113
Linus Walleijfdeb8e12016-02-10 10:57:36 +01005114static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
David Brownelld2876d02008-02-04 22:28:20 -08005115{
5116 unsigned i;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01005117 struct gpio_chip *chip = gdev->chip;
5118 unsigned gpio = gdev->base;
5119 struct gpio_desc *gdesc = &gdev->descs[0];
Linus Walleij90fd2272018-10-01 23:06:17 +02005120 bool is_out;
5121 bool is_irq;
5122 bool active_low;
David Brownelld2876d02008-02-04 22:28:20 -08005123
Linus Walleijfdeb8e12016-02-10 10:57:36 +01005124 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02005125 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
5126 if (gdesc->name) {
5127 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
5128 gpio, gdesc->name);
5129 }
David Brownelld2876d02008-02-04 22:28:20 -08005130 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02005131 }
David Brownelld2876d02008-02-04 22:28:20 -08005132
Alexandre Courbot372e7222013-02-03 01:29:29 +09005133 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08005134 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02005135 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Linus Walleij90fd2272018-10-01 23:06:17 +02005136 active_low = test_bit(FLAG_ACTIVE_LOW, &gdesc->flags);
5137 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s%s",
Markus Pargmannced433e2015-08-14 16:11:02 +02005138 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08005139 is_out ? "out" : "in ",
Andy Shevchenko1c22a252018-07-12 20:36:42 +03005140 chip->get ? (chip->get(chip, i) ? "hi" : "lo") : "? ",
Linus Walleij90fd2272018-10-01 23:06:17 +02005141 is_irq ? "IRQ " : "",
5142 active_low ? "ACTIVE LOW" : "");
David Brownelld2876d02008-02-04 22:28:20 -08005143 seq_printf(s, "\n");
5144 }
5145}
5146
Thierry Redingf9c4a312012-04-12 13:26:01 +02005147static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08005148{
Grant Likely362432a2013-02-09 09:41:49 +00005149 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02005150 struct gpio_device *gdev = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09005151 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02005152
5153 s->private = "";
5154
Grant Likely362432a2013-02-09 09:41:49 +00005155 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02005156 list_for_each_entry(gdev, &gpio_devices, list)
Grant Likely362432a2013-02-09 09:41:49 +00005157 if (index-- == 0) {
5158 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02005159 return gdev;
Grant Likely362432a2013-02-09 09:41:49 +00005160 }
5161 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09005162
5163 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02005164}
5165
5166static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
5167{
Grant Likely362432a2013-02-09 09:41:49 +00005168 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02005169 struct gpio_device *gdev = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02005170 void *ret = NULL;
5171
Grant Likely362432a2013-02-09 09:41:49 +00005172 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02005173 if (list_is_last(&gdev->list, &gpio_devices))
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09005174 ret = NULL;
5175 else
Linus Walleijff2b1352015-10-20 11:10:38 +02005176 ret = list_entry(gdev->list.next, struct gpio_device, list);
Grant Likely362432a2013-02-09 09:41:49 +00005177 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02005178
5179 s->private = "\n";
5180 ++*pos;
5181
5182 return ret;
5183}
5184
5185static void gpiolib_seq_stop(struct seq_file *s, void *v)
5186{
5187}
5188
5189static int gpiolib_seq_show(struct seq_file *s, void *v)
5190{
Linus Walleijff2b1352015-10-20 11:10:38 +02005191 struct gpio_device *gdev = v;
5192 struct gpio_chip *chip = gdev->chip;
5193 struct device *parent;
Thierry Redingf9c4a312012-04-12 13:26:01 +02005194
Linus Walleijff2b1352015-10-20 11:10:38 +02005195 if (!chip) {
5196 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
5197 dev_name(&gdev->dev));
5198 return 0;
5199 }
5200
5201 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
5202 dev_name(&gdev->dev),
Linus Walleijfdeb8e12016-02-10 10:57:36 +01005203 gdev->base, gdev->base + gdev->ngpio - 1);
Linus Walleijff2b1352015-10-20 11:10:38 +02005204 parent = chip->parent;
5205 if (parent)
5206 seq_printf(s, ", parent: %s/%s",
5207 parent->bus ? parent->bus->name : "no-bus",
5208 dev_name(parent));
Thierry Redingf9c4a312012-04-12 13:26:01 +02005209 if (chip->label)
5210 seq_printf(s, ", %s", chip->label);
5211 if (chip->can_sleep)
5212 seq_printf(s, ", can sleep");
5213 seq_printf(s, ":\n");
5214
5215 if (chip->dbg_show)
5216 chip->dbg_show(s, chip);
5217 else
Linus Walleijfdeb8e12016-02-10 10:57:36 +01005218 gpiolib_dbg_show(s, gdev);
Thierry Redingf9c4a312012-04-12 13:26:01 +02005219
David Brownelld2876d02008-02-04 22:28:20 -08005220 return 0;
5221}
5222
Thierry Redingf9c4a312012-04-12 13:26:01 +02005223static const struct seq_operations gpiolib_seq_ops = {
5224 .start = gpiolib_seq_start,
5225 .next = gpiolib_seq_next,
5226 .stop = gpiolib_seq_stop,
5227 .show = gpiolib_seq_show,
5228};
5229
David Brownelld2876d02008-02-04 22:28:20 -08005230static int gpiolib_open(struct inode *inode, struct file *file)
5231{
Thierry Redingf9c4a312012-04-12 13:26:01 +02005232 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08005233}
5234
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005235static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02005236 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08005237 .open = gpiolib_open,
5238 .read = seq_read,
5239 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02005240 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08005241};
5242
5243static int __init gpiolib_debugfs_init(void)
5244{
5245 /* /sys/kernel/debug/gpio */
Greg Kroah-Hartmanacc68b02019-06-18 17:50:45 +02005246 debugfs_create_file("gpio", S_IFREG | S_IRUGO, NULL, NULL,
5247 &gpiolib_operations);
David Brownelld2876d02008-02-04 22:28:20 -08005248 return 0;
5249}
5250subsys_initcall(gpiolib_debugfs_init);
5251
5252#endif /* DEBUG_FS */