blob: d7532aa6c0bcff608c2acb677ce026adee960c1c [file] [log] [blame]
Andy Shevchenko923a6542017-05-25 16:08:38 +03001#include <linux/bitmap.h>
David Brownelld2876d02008-02-04 22:28:20 -08002#include <linux/kernel.h>
3#include <linux/module.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -07004#include <linux/interrupt.h>
David Brownelld2876d02008-02-04 22:28:20 -08005#include <linux/irq.h>
6#include <linux/spinlock.h>
Alexandre Courbot1a989d02013-02-03 01:29:24 +09007#include <linux/list.h>
David Brownelld8f388d82008-07-25 01:46:07 -07008#include <linux/device.h>
9#include <linux/err.h>
10#include <linux/debugfs.h>
11#include <linux/seq_file.h>
12#include <linux/gpio.h>
Anton Vorontsov391c9702010-06-08 07:48:17 -060013#include <linux/of_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"
32
Uwe Kleine-König3f397c212011-05-20 00:40:19 -060033#define CREATE_TRACE_POINTS
34#include <trace/events/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080035
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070036/* Implementation infrastructure for GPIO interfaces.
David Brownelld2876d02008-02-04 22:28:20 -080037 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070038 * The GPIO programming interface allows for inlining speed-critical
39 * get/set operations for common cases, so that access to SOC-integrated
40 * GPIOs can sometimes cost only an instruction or two per bit.
David Brownelld2876d02008-02-04 22:28:20 -080041 */
42
43
44/* When debugging, extend minimal trust to callers and platform code.
45 * Also emit diagnostic messages that may help initial bringup, when
46 * board setup or driver bugs are most common.
47 *
48 * Otherwise, minimize overhead in what may be bitbanging codepaths.
49 */
50#ifdef DEBUG
51#define extra_checks 1
52#else
53#define extra_checks 0
54#endif
55
Linus Walleijff2b1352015-10-20 11:10:38 +020056/* Device and char device-related information */
57static DEFINE_IDA(gpio_ida);
Linus Walleij3c702e92015-10-21 15:29:53 +020058static dev_t gpio_devt;
59#define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
60static struct bus_type gpio_bus_type = {
61 .name = "gpio",
62};
Linus Walleijff2b1352015-10-20 11:10:38 +020063
Laura Abbott30277432018-05-21 10:57:07 -070064/*
65 * Number of GPIOs to use for the fast path in set array
66 */
67#define FASTPATH_NGPIO CONFIG_GPIOLIB_FASTPATH_LIMIT
68
David Brownelld2876d02008-02-04 22:28:20 -080069/* gpio_lock prevents conflicts during gpio_desc[] table updates.
70 * While any GPIO is requested, its gpio_chip is not removable;
71 * each GPIO's "requested" flag serves as a lock and refcount.
72 */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090073DEFINE_SPINLOCK(gpio_lock);
David Brownelld2876d02008-02-04 22:28:20 -080074
Alexandre Courbotbae48da2013-10-17 10:21:38 -070075static DEFINE_MUTEX(gpio_lookup_lock);
76static LIST_HEAD(gpio_lookup_list);
Linus Walleijff2b1352015-10-20 11:10:38 +020077LIST_HEAD(gpio_devices);
Johan Hovold6d867502015-05-04 17:23:25 +020078
Bartosz Golaszewskia411e812018-04-10 22:30:28 +020079static DEFINE_MUTEX(gpio_machine_hogs_mutex);
80static LIST_HEAD(gpio_machine_hogs);
81
Johan Hovold6d867502015-05-04 17:23:25 +020082static void gpiochip_free_hogs(struct gpio_chip *chip);
Thierry Reding959bc7b2017-11-07 19:15:59 +010083static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
Andrew Lunn39c3fd52017-12-02 18:11:04 +010084 struct lock_class_key *lock_key,
85 struct lock_class_key *request_key);
Johan Hovold6d867502015-05-04 17:23:25 +020086static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
Mika Westerberg79b804c2016-09-20 15:15:21 +030087static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip);
88static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip);
Johan Hovold6d867502015-05-04 17:23:25 +020089
Guenter Roeck159f3cd2016-03-31 08:11:30 -070090static bool gpiolib_initialized;
Johan Hovold6d867502015-05-04 17:23:25 +020091
David Brownelld2876d02008-02-04 22:28:20 -080092static inline void desc_set_label(struct gpio_desc *d, const char *label)
93{
David Brownelld2876d02008-02-04 22:28:20 -080094 d->label = label;
David Brownelld2876d02008-02-04 22:28:20 -080095}
96
Alexandre Courbot372e7222013-02-03 01:29:29 +090097/**
Thierry Reding950d55f52017-07-24 16:57:22 +020098 * gpio_to_desc - Convert a GPIO number to its descriptor
99 * @gpio: global GPIO number
100 *
101 * Returns:
102 * The GPIO descriptor associated with the given GPIO, or %NULL if no GPIO
103 * with the given number exists in the system.
Alexandre Courbot372e7222013-02-03 01:29:29 +0900104 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700105struct gpio_desc *gpio_to_desc(unsigned gpio)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900106{
Linus Walleijff2b1352015-10-20 11:10:38 +0200107 struct gpio_device *gdev;
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900108 unsigned long flags;
109
110 spin_lock_irqsave(&gpio_lock, flags);
111
Linus Walleijff2b1352015-10-20 11:10:38 +0200112 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100113 if (gdev->base <= gpio &&
114 gdev->base + gdev->ngpio > gpio) {
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900115 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100116 return &gdev->descs[gpio - gdev->base];
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900117 }
118 }
119
120 spin_unlock_irqrestore(&gpio_lock, flags);
121
Alexandre Courbot0e9a5ed2014-12-02 23:15:05 +0900122 if (!gpio_is_valid(gpio))
123 WARN(1, "invalid GPIO %d\n", gpio);
124
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900125 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900126}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700127EXPORT_SYMBOL_GPL(gpio_to_desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900128
129/**
Thierry Reding950d55f52017-07-24 16:57:22 +0200130 * gpiochip_get_desc - get the GPIO descriptor corresponding to the given
131 * hardware number for this chip
132 * @chip: GPIO chip
133 * @hwnum: hardware number of the GPIO for this chip
134 *
135 * Returns:
136 * A pointer to the GPIO descriptor or %ERR_PTR(-EINVAL) if no GPIO exists
137 * in the given chip for the specified hardware number.
Linus Walleijd468bf92013-09-24 11:54:38 +0200138 */
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900139struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
140 u16 hwnum)
Linus Walleijd468bf92013-09-24 11:54:38 +0200141{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100142 struct gpio_device *gdev = chip->gpiodev;
143
144 if (hwnum >= gdev->ngpio)
Alexandre Courbotb7d0a282013-12-03 12:31:11 +0900145 return ERR_PTR(-EINVAL);
Linus Walleijd468bf92013-09-24 11:54:38 +0200146
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100147 return &gdev->descs[hwnum];
Linus Walleijd468bf92013-09-24 11:54:38 +0200148}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900149
150/**
Thierry Reding950d55f52017-07-24 16:57:22 +0200151 * desc_to_gpio - convert a GPIO descriptor to the integer namespace
152 * @desc: GPIO descriptor
153 *
Alexandre Courbot372e7222013-02-03 01:29:29 +0900154 * This should disappear in the future but is needed since we still
Thierry Reding950d55f52017-07-24 16:57:22 +0200155 * use GPIO numbers for error messages and sysfs nodes.
156 *
157 * Returns:
158 * The global GPIO number for the GPIO specified by its descriptor.
Alexandre Courbot372e7222013-02-03 01:29:29 +0900159 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700160int desc_to_gpio(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900161{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100162 return desc->gdev->base + (desc - &desc->gdev->descs[0]);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900163}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700164EXPORT_SYMBOL_GPL(desc_to_gpio);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900165
166
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700167/**
168 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
169 * @desc: descriptor to return the chip of
170 */
171struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900172{
Vladimir Zapolskiydd3b9a42017-12-21 18:37:30 +0200173 if (!desc || !desc->gdev)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100174 return NULL;
175 return desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900176}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700177EXPORT_SYMBOL_GPL(gpiod_to_chip);
David Brownelld2876d02008-02-04 22:28:20 -0800178
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700179/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
180static int gpiochip_find_base(int ngpio)
181{
Linus Walleijff2b1352015-10-20 11:10:38 +0200182 struct gpio_device *gdev;
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900183 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700184
Linus Walleijff2b1352015-10-20 11:10:38 +0200185 list_for_each_entry_reverse(gdev, &gpio_devices, list) {
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900186 /* found a free space? */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100187 if (gdev->base + gdev->ngpio <= base)
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900188 break;
189 else
190 /* nope, check the space right before the chip */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100191 base = gdev->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700192 }
193
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900194 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700195 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900196 return base;
197 } else {
198 pr_err("%s: cannot find free range\n", __func__);
199 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700200 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700201}
202
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700203/**
204 * gpiod_get_direction - return the current direction of a GPIO
205 * @desc: GPIO to get the direction of
206 *
Wolfram Sang94fc7302018-01-09 12:35:53 +0100207 * Returns 0 for output, 1 for input, or an error code in case of error.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700208 *
209 * This function may sleep if gpiod_cansleep() is true.
210 */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900211int gpiod_get_direction(struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300212{
213 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900214 unsigned offset;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300215 int status = -EINVAL;
216
Alexandre Courbot372e7222013-02-03 01:29:29 +0900217 chip = gpiod_to_chip(desc);
218 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300219
220 if (!chip->get_direction)
221 return status;
222
Alexandre Courbot372e7222013-02-03 01:29:29 +0900223 status = chip->get_direction(chip, offset);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300224 if (status > 0) {
225 /* GPIOF_DIR_IN, or other positive */
226 status = 1;
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900227 clear_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300228 }
229 if (status == 0) {
230 /* GPIOF_DIR_OUT */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900231 set_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300232 }
233 return status;
234}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700235EXPORT_SYMBOL_GPL(gpiod_get_direction);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300236
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900237/*
238 * Add a new chip to the global chips list, keeping the list of chips sorted
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800239 * by range(means [base, base + ngpio - 1]) order.
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900240 *
241 * Return -EBUSY if the new chip overlaps with some other chip's integer
242 * space.
243 */
Linus Walleijff2b1352015-10-20 11:10:38 +0200244static int gpiodev_add_to_list(struct gpio_device *gdev)
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900245{
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800246 struct gpio_device *prev, *next;
Linus Walleijff2b1352015-10-20 11:10:38 +0200247
248 if (list_empty(&gpio_devices)) {
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800249 /* initial entry in list */
Linus Walleijff2b1352015-10-20 11:10:38 +0200250 list_add_tail(&gdev->list, &gpio_devices);
Sudip Mukherjeee28ecca2015-12-27 19:06:50 +0530251 return 0;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900252 }
253
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800254 next = list_entry(gpio_devices.next, struct gpio_device, list);
255 if (gdev->base + gdev->ngpio <= next->base) {
256 /* add before first entry */
257 list_add(&gdev->list, &gpio_devices);
Julien Grossholtz96098df2016-01-07 16:46:45 -0500258 return 0;
259 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900260
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800261 prev = list_entry(gpio_devices.prev, struct gpio_device, list);
262 if (prev->base + prev->ngpio <= gdev->base) {
263 /* add behind last entry */
264 list_add_tail(&gdev->list, &gpio_devices);
265 return 0;
266 }
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800267
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800268 list_for_each_entry_safe(prev, next, &gpio_devices, list) {
269 /* at the end of the list */
270 if (&next->list == &gpio_devices)
271 break;
272
273 /* add between prev and next */
274 if (prev->base + prev->ngpio <= gdev->base
275 && gdev->base + gdev->ngpio <= next->base) {
276 list_add(&gdev->list, &prev->list);
277 return 0;
278 }
279 }
280
281 dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n");
282 return -EBUSY;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900283}
284
Thierry Reding950d55f52017-07-24 16:57:22 +0200285/*
Linus Walleijf881bab02015-09-23 16:20:43 -0700286 * Convert a GPIO name to its descriptor
287 */
288static struct gpio_desc *gpio_name_to_desc(const char * const name)
289{
Linus Walleijff2b1352015-10-20 11:10:38 +0200290 struct gpio_device *gdev;
Linus Walleijf881bab02015-09-23 16:20:43 -0700291 unsigned long flags;
292
293 spin_lock_irqsave(&gpio_lock, flags);
294
Linus Walleijff2b1352015-10-20 11:10:38 +0200295 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijf881bab02015-09-23 16:20:43 -0700296 int i;
297
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100298 for (i = 0; i != gdev->ngpio; ++i) {
299 struct gpio_desc *desc = &gdev->descs[i];
Linus Walleijf881bab02015-09-23 16:20:43 -0700300
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100301 if (!desc->name || !name)
Linus Walleijf881bab02015-09-23 16:20:43 -0700302 continue;
303
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100304 if (!strcmp(desc->name, name)) {
Linus Walleijf881bab02015-09-23 16:20:43 -0700305 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100306 return desc;
Linus Walleijf881bab02015-09-23 16:20:43 -0700307 }
308 }
309 }
310
311 spin_unlock_irqrestore(&gpio_lock, flags);
312
313 return NULL;
314}
315
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200316/*
317 * Takes the names from gc->names and checks if they are all unique. If they
318 * are, they are assigned to their gpio descriptors.
319 *
Bamvor Jian Zhanged379152015-11-14 16:43:20 +0800320 * Warning if one of the names is already used for a different GPIO.
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200321 */
322static int gpiochip_set_desc_names(struct gpio_chip *gc)
323{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100324 struct gpio_device *gdev = gc->gpiodev;
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200325 int i;
326
327 if (!gc->names)
328 return 0;
329
330 /* First check all names if they are unique */
331 for (i = 0; i != gc->ngpio; ++i) {
332 struct gpio_desc *gpio;
333
334 gpio = gpio_name_to_desc(gc->names[i]);
Linus Walleijf881bab02015-09-23 16:20:43 -0700335 if (gpio)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100336 dev_warn(&gdev->dev,
Linus Walleij34ffd852015-10-20 11:31:54 +0200337 "Detected name collision for GPIO name '%s'\n",
Linus Walleijf881bab02015-09-23 16:20:43 -0700338 gc->names[i]);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200339 }
340
341 /* Then add all names to the GPIO descriptors */
342 for (i = 0; i != gc->ngpio; ++i)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100343 gdev->descs[i].name = gc->names[i];
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200344
345 return 0;
346}
347
Stephen Boyde4371f6e2018-03-23 09:34:50 -0700348static unsigned long *gpiochip_allocate_mask(struct gpio_chip *chip)
349{
350 unsigned long *p;
351
Stephen Boydace569352018-03-23 09:34:51 -0700352 p = kmalloc_array(BITS_TO_LONGS(chip->ngpio), sizeof(*p), GFP_KERNEL);
Stephen Boyde4371f6e2018-03-23 09:34:50 -0700353 if (!p)
354 return NULL;
355
356 /* Assume by default all GPIOs are valid */
357 bitmap_fill(p, chip->ngpio);
358
359 return p;
360}
361
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700362static int gpiochip_init_valid_mask(struct gpio_chip *gpiochip)
363{
364#ifdef CONFIG_OF_GPIO
365 int size;
366 struct device_node *np = gpiochip->of_node;
367
368 size = of_property_count_u32_elems(np, "gpio-reserved-ranges");
369 if (size > 0 && size % 2 == 0)
370 gpiochip->need_valid_mask = true;
371#endif
372
373 if (!gpiochip->need_valid_mask)
374 return 0;
375
376 gpiochip->valid_mask = gpiochip_allocate_mask(gpiochip);
377 if (!gpiochip->valid_mask)
378 return -ENOMEM;
379
380 return 0;
381}
382
383static void gpiochip_free_valid_mask(struct gpio_chip *gpiochip)
384{
385 kfree(gpiochip->valid_mask);
386 gpiochip->valid_mask = NULL;
387}
388
389bool gpiochip_line_is_valid(const struct gpio_chip *gpiochip,
390 unsigned int offset)
391{
392 /* No mask means all valid */
393 if (likely(!gpiochip->valid_mask))
394 return true;
395 return test_bit(offset, gpiochip->valid_mask);
396}
397EXPORT_SYMBOL_GPL(gpiochip_line_is_valid);
398
Linus Walleijd7c51b42016-04-26 10:35:29 +0200399/*
400 * GPIO line handle management
401 */
402
403/**
404 * struct linehandle_state - contains the state of a userspace handle
405 * @gdev: the GPIO device the handle pertains to
406 * @label: consumer label used to tag descriptors
407 * @descs: the GPIO descriptors held by this handle
408 * @numdescs: the number of descriptors held in the descs array
409 */
410struct linehandle_state {
411 struct gpio_device *gdev;
412 const char *label;
413 struct gpio_desc *descs[GPIOHANDLES_MAX];
414 u32 numdescs;
415};
416
Lars-Peter Clausene3e847c2016-10-18 16:54:05 +0200417#define GPIOHANDLE_REQUEST_VALID_FLAGS \
418 (GPIOHANDLE_REQUEST_INPUT | \
419 GPIOHANDLE_REQUEST_OUTPUT | \
420 GPIOHANDLE_REQUEST_ACTIVE_LOW | \
421 GPIOHANDLE_REQUEST_OPEN_DRAIN | \
422 GPIOHANDLE_REQUEST_OPEN_SOURCE)
423
Linus Walleijd7c51b42016-04-26 10:35:29 +0200424static long linehandle_ioctl(struct file *filep, unsigned int cmd,
425 unsigned long arg)
426{
427 struct linehandle_state *lh = filep->private_data;
428 void __user *ip = (void __user *)arg;
429 struct gpiohandle_data ghd;
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +0200430 DECLARE_BITMAP(vals, GPIOHANDLES_MAX);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200431 int i;
432
433 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
Bartosz Golaszewski2b955b32018-07-16 10:34:24 +0200434 /* NOTE: It's ok to read values of output lines. */
Lukas Wunnereec1d562017-10-12 12:40:10 +0200435 int ret = gpiod_get_array_value_complex(false,
436 true,
437 lh->numdescs,
438 lh->descs,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +0200439 NULL,
Lukas Wunnereec1d562017-10-12 12:40:10 +0200440 vals);
441 if (ret)
442 return ret;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200443
Lars-Peter Clausen3eded5d2016-10-18 16:54:02 +0200444 memset(&ghd, 0, sizeof(ghd));
Lukas Wunnereec1d562017-10-12 12:40:10 +0200445 for (i = 0; i < lh->numdescs; i++)
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +0200446 ghd.values[i] = test_bit(i, vals);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200447
448 if (copy_to_user(ip, &ghd, sizeof(ghd)))
449 return -EFAULT;
450
451 return 0;
452 } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
Bartosz Golaszewskie5332d52018-07-16 10:34:23 +0200453 /*
454 * All line descriptors were created at once with the same
455 * flags so just check if the first one is really output.
456 */
457 if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags))
458 return -EPERM;
459
Linus Walleijd7c51b42016-04-26 10:35:29 +0200460 if (copy_from_user(&ghd, ip, sizeof(ghd)))
461 return -EFAULT;
462
463 /* Clamp all values to [0,1] */
464 for (i = 0; i < lh->numdescs; i++)
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +0200465 __assign_bit(i, vals, ghd.values[i]);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200466
467 /* Reuse the array setting function */
Laura Abbott30277432018-05-21 10:57:07 -0700468 return gpiod_set_array_value_complex(false,
Linus Walleijd7c51b42016-04-26 10:35:29 +0200469 true,
470 lh->numdescs,
471 lh->descs,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +0200472 NULL,
Linus Walleijd7c51b42016-04-26 10:35:29 +0200473 vals);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200474 }
475 return -EINVAL;
476}
477
478#ifdef CONFIG_COMPAT
479static long linehandle_ioctl_compat(struct file *filep, unsigned int cmd,
480 unsigned long arg)
481{
482 return linehandle_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
483}
484#endif
485
486static int linehandle_release(struct inode *inode, struct file *filep)
487{
488 struct linehandle_state *lh = filep->private_data;
489 struct gpio_device *gdev = lh->gdev;
490 int i;
491
492 for (i = 0; i < lh->numdescs; i++)
493 gpiod_free(lh->descs[i]);
494 kfree(lh->label);
495 kfree(lh);
496 put_device(&gdev->dev);
497 return 0;
498}
499
500static const struct file_operations linehandle_fileops = {
501 .release = linehandle_release,
502 .owner = THIS_MODULE,
503 .llseek = noop_llseek,
504 .unlocked_ioctl = linehandle_ioctl,
505#ifdef CONFIG_COMPAT
506 .compat_ioctl = linehandle_ioctl_compat,
507#endif
508};
509
510static int linehandle_create(struct gpio_device *gdev, void __user *ip)
511{
512 struct gpiohandle_request handlereq;
513 struct linehandle_state *lh;
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200514 struct file *file;
Timur Tabiab3dbcf2018-03-29 13:29:12 -0500515 int fd, i, count = 0, ret;
Bartosz Golaszewski418ee8e2017-10-16 11:32:29 +0200516 u32 lflags;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200517
518 if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
519 return -EFAULT;
520 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
521 return -EINVAL;
522
Bartosz Golaszewski418ee8e2017-10-16 11:32:29 +0200523 lflags = handlereq.flags;
524
525 /* Return an error if an unknown flag is set */
526 if (lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
527 return -EINVAL;
528
Bartosz Golaszewski588fc3b2017-11-15 16:47:43 +0100529 /*
530 * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
531 * the hardware actually supports enabling both at the same time the
532 * electrical result would be disastrous.
533 */
534 if ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) &&
535 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
536 return -EINVAL;
537
Bartosz Golaszewski609aaf62017-10-16 11:32:30 +0200538 /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
539 if (!(lflags & GPIOHANDLE_REQUEST_OUTPUT) &&
540 ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
541 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
542 return -EINVAL;
543
Linus Walleijd7c51b42016-04-26 10:35:29 +0200544 lh = kzalloc(sizeof(*lh), GFP_KERNEL);
545 if (!lh)
546 return -ENOMEM;
547 lh->gdev = gdev;
548 get_device(&gdev->dev);
549
550 /* Make sure this is terminated */
551 handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
552 if (strlen(handlereq.consumer_label)) {
553 lh->label = kstrdup(handlereq.consumer_label,
554 GFP_KERNEL);
555 if (!lh->label) {
556 ret = -ENOMEM;
557 goto out_free_lh;
558 }
559 }
560
561 /* Request each GPIO */
562 for (i = 0; i < handlereq.lines; i++) {
563 u32 offset = handlereq.lineoffsets[i];
Linus Walleijd7c51b42016-04-26 10:35:29 +0200564 struct gpio_desc *desc;
565
Lars-Peter Clausene405f9f2016-10-18 16:54:01 +0200566 if (offset >= gdev->ngpio) {
567 ret = -EINVAL;
568 goto out_free_descs;
569 }
570
Linus Walleijd7c51b42016-04-26 10:35:29 +0200571 desc = &gdev->descs[offset];
572 ret = gpiod_request(desc, lh->label);
573 if (ret)
574 goto out_free_descs;
575 lh->descs[i] = desc;
Timur Tabiab3dbcf2018-03-29 13:29:12 -0500576 count = i;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200577
578 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
579 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
580 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
581 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
582 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
583 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
584
Andrew Jefferye10f72b2017-11-30 14:25:24 +1030585 ret = gpiod_set_transitory(desc, false);
586 if (ret < 0)
587 goto out_free_descs;
588
Linus Walleijd7c51b42016-04-26 10:35:29 +0200589 /*
590 * Lines have to be requested explicitly for input
591 * or output, else the line will be treated "as is".
592 */
593 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
594 int val = !!handlereq.default_values[i];
595
596 ret = gpiod_direction_output(desc, val);
597 if (ret)
598 goto out_free_descs;
599 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
600 ret = gpiod_direction_input(desc);
601 if (ret)
602 goto out_free_descs;
603 }
604 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
605 offset);
606 }
Linus Walleije2f608b2016-06-18 10:56:43 +0200607 /* Let i point at the last handle */
608 i--;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200609 lh->numdescs = handlereq.lines;
610
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200611 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200612 if (fd < 0) {
613 ret = fd;
614 goto out_free_descs;
615 }
616
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200617 file = anon_inode_getfile("gpio-linehandle",
618 &linehandle_fileops,
619 lh,
620 O_RDONLY | O_CLOEXEC);
621 if (IS_ERR(file)) {
622 ret = PTR_ERR(file);
623 goto out_put_unused_fd;
624 }
625
Linus Walleijd7c51b42016-04-26 10:35:29 +0200626 handlereq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200627 if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200628 /*
629 * fput() will trigger the release() callback, so do not go onto
630 * the regular error cleanup path here.
631 */
632 fput(file);
633 put_unused_fd(fd);
634 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +0200635 }
Linus Walleijd7c51b42016-04-26 10:35:29 +0200636
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200637 fd_install(fd, file);
638
Linus Walleijd7c51b42016-04-26 10:35:29 +0200639 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
640 lh->numdescs);
641
642 return 0;
643
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200644out_put_unused_fd:
645 put_unused_fd(fd);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200646out_free_descs:
Timur Tabiab3dbcf2018-03-29 13:29:12 -0500647 for (i = 0; i < count; i++)
Linus Walleijd7c51b42016-04-26 10:35:29 +0200648 gpiod_free(lh->descs[i]);
649 kfree(lh->label);
650out_free_lh:
651 kfree(lh);
652 put_device(&gdev->dev);
653 return ret;
654}
655
Linus Walleij61f922d2016-06-02 11:30:15 +0200656/*
657 * GPIO line event management
658 */
659
660/**
661 * struct lineevent_state - contains the state of a userspace event
662 * @gdev: the GPIO device the event pertains to
663 * @label: consumer label used to tag descriptors
664 * @desc: the GPIO descriptor held by this event
665 * @eflags: the event flags this line was requested with
666 * @irq: the interrupt that trigger in response to events on this GPIO
667 * @wait: wait queue that handles blocking reads of events
668 * @events: KFIFO for the GPIO events
669 * @read_lock: mutex lock to protect reads from colliding with adding
670 * new events to the FIFO
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100671 * @timestamp: cache for the timestamp storing it between hardirq
672 * and IRQ thread, used to bring the timestamp close to the actual
673 * event
Linus Walleij61f922d2016-06-02 11:30:15 +0200674 */
675struct lineevent_state {
676 struct gpio_device *gdev;
677 const char *label;
678 struct gpio_desc *desc;
679 u32 eflags;
680 int irq;
681 wait_queue_head_t wait;
682 DECLARE_KFIFO(events, struct gpioevent_data, 16);
683 struct mutex read_lock;
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100684 u64 timestamp;
Linus Walleij61f922d2016-06-02 11:30:15 +0200685};
686
Lars-Peter Clausenac7dbb92016-10-18 16:54:06 +0200687#define GPIOEVENT_REQUEST_VALID_FLAGS \
688 (GPIOEVENT_REQUEST_RISING_EDGE | \
689 GPIOEVENT_REQUEST_FALLING_EDGE)
690
Al Viroafc9a422017-07-03 06:39:46 -0400691static __poll_t lineevent_poll(struct file *filep,
Linus Walleij61f922d2016-06-02 11:30:15 +0200692 struct poll_table_struct *wait)
693{
694 struct lineevent_state *le = filep->private_data;
Al Viroafc9a422017-07-03 06:39:46 -0400695 __poll_t events = 0;
Linus Walleij61f922d2016-06-02 11:30:15 +0200696
697 poll_wait(filep, &le->wait, wait);
698
699 if (!kfifo_is_empty(&le->events))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800700 events = EPOLLIN | EPOLLRDNORM;
Linus Walleij61f922d2016-06-02 11:30:15 +0200701
702 return events;
703}
704
705
706static ssize_t lineevent_read(struct file *filep,
707 char __user *buf,
708 size_t count,
709 loff_t *f_ps)
710{
711 struct lineevent_state *le = filep->private_data;
712 unsigned int copied;
713 int ret;
714
715 if (count < sizeof(struct gpioevent_data))
716 return -EINVAL;
717
718 do {
719 if (kfifo_is_empty(&le->events)) {
720 if (filep->f_flags & O_NONBLOCK)
721 return -EAGAIN;
722
723 ret = wait_event_interruptible(le->wait,
724 !kfifo_is_empty(&le->events));
725 if (ret)
726 return ret;
727 }
728
729 if (mutex_lock_interruptible(&le->read_lock))
730 return -ERESTARTSYS;
731 ret = kfifo_to_user(&le->events, buf, count, &copied);
732 mutex_unlock(&le->read_lock);
733
734 if (ret)
735 return ret;
736
737 /*
738 * If we couldn't read anything from the fifo (a different
739 * thread might have been faster) we either return -EAGAIN if
740 * the file descriptor is non-blocking, otherwise we go back to
741 * sleep and wait for more data to arrive.
742 */
743 if (copied == 0 && (filep->f_flags & O_NONBLOCK))
744 return -EAGAIN;
745
746 } while (copied == 0);
747
748 return copied;
749}
750
751static int lineevent_release(struct inode *inode, struct file *filep)
752{
753 struct lineevent_state *le = filep->private_data;
754 struct gpio_device *gdev = le->gdev;
755
756 free_irq(le->irq, le);
757 gpiod_free(le->desc);
758 kfree(le->label);
759 kfree(le);
760 put_device(&gdev->dev);
761 return 0;
762}
763
764static long lineevent_ioctl(struct file *filep, unsigned int cmd,
765 unsigned long arg)
766{
767 struct lineevent_state *le = filep->private_data;
768 void __user *ip = (void __user *)arg;
769 struct gpiohandle_data ghd;
770
771 /*
772 * We can get the value for an event line but not set it,
773 * because it is input by definition.
774 */
775 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
776 int val;
777
Lars-Peter Clausend82aa4a2016-10-18 16:54:04 +0200778 memset(&ghd, 0, sizeof(ghd));
779
Linus Walleij61f922d2016-06-02 11:30:15 +0200780 val = gpiod_get_value_cansleep(le->desc);
781 if (val < 0)
782 return val;
783 ghd.values[0] = val;
784
785 if (copy_to_user(ip, &ghd, sizeof(ghd)))
786 return -EFAULT;
787
788 return 0;
789 }
790 return -EINVAL;
791}
792
793#ifdef CONFIG_COMPAT
794static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd,
795 unsigned long arg)
796{
797 return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
798}
799#endif
800
801static const struct file_operations lineevent_fileops = {
802 .release = lineevent_release,
803 .read = lineevent_read,
804 .poll = lineevent_poll,
805 .owner = THIS_MODULE,
806 .llseek = noop_llseek,
807 .unlocked_ioctl = lineevent_ioctl,
808#ifdef CONFIG_COMPAT
809 .compat_ioctl = lineevent_ioctl_compat,
810#endif
811};
812
Ben Dooks33265b12016-06-17 16:03:13 +0100813static irqreturn_t lineevent_irq_thread(int irq, void *p)
Linus Walleij61f922d2016-06-02 11:30:15 +0200814{
815 struct lineevent_state *le = p;
816 struct gpioevent_data ge;
Bartosz Golaszewskidf1e76f2017-07-03 11:12:03 +0200817 int ret, level;
Linus Walleij61f922d2016-06-02 11:30:15 +0200818
Linus Walleij24bd3ef2018-01-22 13:19:28 +0100819 /* Do not leak kernel stack to userspace */
820 memset(&ge, 0, sizeof(ge));
821
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100822 ge.timestamp = le->timestamp;
Bartosz Golaszewskidf1e76f2017-07-03 11:12:03 +0200823 level = gpiod_get_value_cansleep(le->desc);
Linus Walleij61f922d2016-06-02 11:30:15 +0200824
Bartosz Golaszewskiad537b82017-06-23 13:45:16 +0200825 if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
826 && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200827 if (level)
828 /* Emit low-to-high event */
829 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
830 else
831 /* Emit high-to-low event */
832 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Bartosz Golaszewskidf1e76f2017-07-03 11:12:03 +0200833 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE && level) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200834 /* Emit low-to-high event */
835 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
Bartosz Golaszewskidf1e76f2017-07-03 11:12:03 +0200836 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE && !level) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200837 /* Emit high-to-low event */
838 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Arnd Bergmannbc0207a2016-06-16 11:02:41 +0200839 } else {
840 return IRQ_NONE;
Linus Walleij61f922d2016-06-02 11:30:15 +0200841 }
842
843 ret = kfifo_put(&le->events, ge);
844 if (ret != 0)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800845 wake_up_poll(&le->wait, EPOLLIN);
Linus Walleij61f922d2016-06-02 11:30:15 +0200846
847 return IRQ_HANDLED;
848}
849
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100850static irqreturn_t lineevent_irq_handler(int irq, void *p)
851{
852 struct lineevent_state *le = p;
853
854 /*
855 * Just store the timestamp in hardirq context so we get it as
856 * close in time as possible to the actual event.
857 */
858 le->timestamp = ktime_get_real_ns();
859
860 return IRQ_WAKE_THREAD;
861}
862
Linus Walleij61f922d2016-06-02 11:30:15 +0200863static int lineevent_create(struct gpio_device *gdev, void __user *ip)
864{
865 struct gpioevent_request eventreq;
866 struct lineevent_state *le;
867 struct gpio_desc *desc;
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200868 struct file *file;
Linus Walleij61f922d2016-06-02 11:30:15 +0200869 u32 offset;
870 u32 lflags;
871 u32 eflags;
872 int fd;
873 int ret;
874 int irqflags = 0;
875
876 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
877 return -EFAULT;
878
879 le = kzalloc(sizeof(*le), GFP_KERNEL);
880 if (!le)
881 return -ENOMEM;
882 le->gdev = gdev;
883 get_device(&gdev->dev);
884
885 /* Make sure this is terminated */
886 eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
887 if (strlen(eventreq.consumer_label)) {
888 le->label = kstrdup(eventreq.consumer_label,
889 GFP_KERNEL);
890 if (!le->label) {
891 ret = -ENOMEM;
892 goto out_free_le;
893 }
894 }
895
896 offset = eventreq.lineoffset;
897 lflags = eventreq.handleflags;
898 eflags = eventreq.eventflags;
899
Lars-Peter Clausenb8b0e3d2016-10-18 16:54:03 +0200900 if (offset >= gdev->ngpio) {
901 ret = -EINVAL;
902 goto out_free_label;
903 }
904
Lars-Peter Clausenac7dbb92016-10-18 16:54:06 +0200905 /* Return an error if a unknown flag is set */
906 if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
907 (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) {
908 ret = -EINVAL;
909 goto out_free_label;
910 }
911
Linus Walleij61f922d2016-06-02 11:30:15 +0200912 /* This is just wrong: we don't look for events on output lines */
913 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
914 ret = -EINVAL;
915 goto out_free_label;
916 }
917
918 desc = &gdev->descs[offset];
919 ret = gpiod_request(desc, le->label);
920 if (ret)
Uwe Kleine-Königf001cc32018-04-16 13:17:53 +0200921 goto out_free_label;
Linus Walleij61f922d2016-06-02 11:30:15 +0200922 le->desc = desc;
923 le->eflags = eflags;
924
925 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
926 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
927 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
928 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
929 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
930 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
931
932 ret = gpiod_direction_input(desc);
933 if (ret)
934 goto out_free_desc;
935
936 le->irq = gpiod_to_irq(desc);
937 if (le->irq <= 0) {
938 ret = -ENODEV;
939 goto out_free_desc;
940 }
941
942 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
943 irqflags |= IRQF_TRIGGER_RISING;
944 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
945 irqflags |= IRQF_TRIGGER_FALLING;
946 irqflags |= IRQF_ONESHOT;
947 irqflags |= IRQF_SHARED;
948
949 INIT_KFIFO(le->events);
950 init_waitqueue_head(&le->wait);
951 mutex_init(&le->read_lock);
952
953 /* Request a thread to read the events */
954 ret = request_threaded_irq(le->irq,
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100955 lineevent_irq_handler,
Linus Walleij61f922d2016-06-02 11:30:15 +0200956 lineevent_irq_thread,
957 irqflags,
958 le->label,
959 le);
960 if (ret)
961 goto out_free_desc;
962
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200963 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleij61f922d2016-06-02 11:30:15 +0200964 if (fd < 0) {
965 ret = fd;
966 goto out_free_irq;
967 }
968
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200969 file = anon_inode_getfile("gpio-event",
970 &lineevent_fileops,
971 le,
972 O_RDONLY | O_CLOEXEC);
973 if (IS_ERR(file)) {
974 ret = PTR_ERR(file);
975 goto out_put_unused_fd;
976 }
977
Linus Walleij61f922d2016-06-02 11:30:15 +0200978 eventreq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200979 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200980 /*
981 * fput() will trigger the release() callback, so do not go onto
982 * the regular error cleanup path here.
983 */
984 fput(file);
985 put_unused_fd(fd);
986 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +0200987 }
Linus Walleij61f922d2016-06-02 11:30:15 +0200988
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200989 fd_install(fd, file);
990
Linus Walleij61f922d2016-06-02 11:30:15 +0200991 return 0;
992
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200993out_put_unused_fd:
994 put_unused_fd(fd);
Linus Walleij61f922d2016-06-02 11:30:15 +0200995out_free_irq:
996 free_irq(le->irq, le);
997out_free_desc:
998 gpiod_free(le->desc);
999out_free_label:
1000 kfree(le->label);
1001out_free_le:
1002 kfree(le);
1003 put_device(&gdev->dev);
1004 return ret;
1005}
1006
Thierry Reding950d55f52017-07-24 16:57:22 +02001007/*
Linus Walleij3c702e92015-10-21 15:29:53 +02001008 * gpio_ioctl() - ioctl handler for the GPIO chardev
1009 */
1010static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1011{
1012 struct gpio_device *gdev = filp->private_data;
1013 struct gpio_chip *chip = gdev->chip;
Linus Walleij8b92e172016-05-27 14:24:04 +02001014 void __user *ip = (void __user *)arg;
Linus Walleij3c702e92015-10-21 15:29:53 +02001015
1016 /* We fail any subsequent ioctl():s when the chip is gone */
1017 if (!chip)
1018 return -ENODEV;
1019
Linus Walleij521a2ad2016-02-12 22:25:22 +01001020 /* Fill in the struct and pass to userspace */
Linus Walleij3c702e92015-10-21 15:29:53 +02001021 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
Linus Walleij521a2ad2016-02-12 22:25:22 +01001022 struct gpiochip_info chipinfo;
1023
Lars-Peter Clausen0f4bbb22016-10-18 16:54:00 +02001024 memset(&chipinfo, 0, sizeof(chipinfo));
1025
Linus Walleij3c702e92015-10-21 15:29:53 +02001026 strncpy(chipinfo.name, dev_name(&gdev->dev),
1027 sizeof(chipinfo.name));
1028 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
Linus Walleijdf4878e2016-02-12 14:48:23 +01001029 strncpy(chipinfo.label, gdev->label,
1030 sizeof(chipinfo.label));
1031 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001032 chipinfo.lines = gdev->ngpio;
Linus Walleij3c702e92015-10-21 15:29:53 +02001033 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
1034 return -EFAULT;
1035 return 0;
Linus Walleij521a2ad2016-02-12 22:25:22 +01001036 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
1037 struct gpioline_info lineinfo;
1038 struct gpio_desc *desc;
1039
1040 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
1041 return -EFAULT;
Lars-Peter Clausen1f1cc452016-10-18 16:53:59 +02001042 if (lineinfo.line_offset >= gdev->ngpio)
Linus Walleij521a2ad2016-02-12 22:25:22 +01001043 return -EINVAL;
1044
1045 desc = &gdev->descs[lineinfo.line_offset];
1046 if (desc->name) {
1047 strncpy(lineinfo.name, desc->name,
1048 sizeof(lineinfo.name));
1049 lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
1050 } else {
1051 lineinfo.name[0] = '\0';
1052 }
1053 if (desc->label) {
Linus Walleij214338e2016-02-25 21:01:48 +01001054 strncpy(lineinfo.consumer, desc->label,
1055 sizeof(lineinfo.consumer));
1056 lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +01001057 } else {
Linus Walleij214338e2016-02-25 21:01:48 +01001058 lineinfo.consumer[0] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +01001059 }
1060
1061 /*
1062 * Userspace only need to know that the kernel is using
1063 * this GPIO so it can't use it.
1064 */
1065 lineinfo.flags = 0;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001066 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
1067 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
1068 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
1069 test_bit(FLAG_EXPORT, &desc->flags) ||
1070 test_bit(FLAG_SYSFS, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001071 lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001072 if (test_bit(FLAG_IS_OUT, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001073 lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001074 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001075 lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001076 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001077 lineinfo.flags |= GPIOLINE_FLAG_OPEN_DRAIN;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001078 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001079 lineinfo.flags |= GPIOLINE_FLAG_OPEN_SOURCE;
1080
1081 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
1082 return -EFAULT;
1083 return 0;
Linus Walleijd7c51b42016-04-26 10:35:29 +02001084 } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
1085 return linehandle_create(gdev, ip);
Linus Walleij61f922d2016-06-02 11:30:15 +02001086 } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
1087 return lineevent_create(gdev, ip);
Linus Walleij3c702e92015-10-21 15:29:53 +02001088 }
1089 return -EINVAL;
1090}
1091
Linus Walleij8b92e172016-05-27 14:24:04 +02001092#ifdef CONFIG_COMPAT
1093static long gpio_ioctl_compat(struct file *filp, unsigned int cmd,
1094 unsigned long arg)
1095{
1096 return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
1097}
1098#endif
1099
Linus Walleij3c702e92015-10-21 15:29:53 +02001100/**
1101 * gpio_chrdev_open() - open the chardev for ioctl operations
1102 * @inode: inode for this chardev
1103 * @filp: file struct for storing private data
1104 * Returns 0 on success
1105 */
1106static int gpio_chrdev_open(struct inode *inode, struct file *filp)
1107{
1108 struct gpio_device *gdev = container_of(inode->i_cdev,
1109 struct gpio_device, chrdev);
1110
1111 /* Fail on open if the backing gpiochip is gone */
Stephen Boydfb505742017-01-09 11:47:44 -08001112 if (!gdev->chip)
Linus Walleij3c702e92015-10-21 15:29:53 +02001113 return -ENODEV;
1114 get_device(&gdev->dev);
1115 filp->private_data = gdev;
Lars-Peter Clausenf4e81c52016-11-30 13:05:21 +01001116
1117 return nonseekable_open(inode, filp);
Linus Walleij3c702e92015-10-21 15:29:53 +02001118}
1119
1120/**
1121 * gpio_chrdev_release() - close chardev after ioctl operations
1122 * @inode: inode for this chardev
1123 * @filp: file struct for storing private data
1124 * Returns 0 on success
1125 */
1126static int gpio_chrdev_release(struct inode *inode, struct file *filp)
1127{
1128 struct gpio_device *gdev = container_of(inode->i_cdev,
1129 struct gpio_device, chrdev);
1130
Linus Walleij3c702e92015-10-21 15:29:53 +02001131 put_device(&gdev->dev);
1132 return 0;
1133}
1134
1135
1136static const struct file_operations gpio_fileops = {
1137 .release = gpio_chrdev_release,
1138 .open = gpio_chrdev_open,
1139 .owner = THIS_MODULE,
Lars-Peter Clausenf4e81c52016-11-30 13:05:21 +01001140 .llseek = no_llseek,
Linus Walleij3c702e92015-10-21 15:29:53 +02001141 .unlocked_ioctl = gpio_ioctl,
Linus Walleij8b92e172016-05-27 14:24:04 +02001142#ifdef CONFIG_COMPAT
1143 .compat_ioctl = gpio_ioctl_compat,
1144#endif
Linus Walleij3c702e92015-10-21 15:29:53 +02001145};
1146
Linus Walleijff2b1352015-10-20 11:10:38 +02001147static void gpiodevice_release(struct device *dev)
1148{
1149 struct gpio_device *gdev = dev_get_drvdata(dev);
1150
1151 list_del(&gdev->list);
1152 ida_simple_remove(&gpio_ida, gdev->id);
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +01001153 kfree_const(gdev->label);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001154 kfree(gdev->descs);
Linus Walleij9efd9e62016-02-09 14:27:42 +01001155 kfree(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001156}
1157
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001158static int gpiochip_setup_dev(struct gpio_device *gdev)
1159{
1160 int status;
1161
1162 cdev_init(&gdev->chrdev, &gpio_fileops);
1163 gdev->chrdev.owner = THIS_MODULE;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001164 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001165
1166 status = cdev_device_add(&gdev->chrdev, &gdev->dev);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001167 if (status)
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001168 return status;
1169
1170 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
1171 MAJOR(gpio_devt), gdev->id);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001172
1173 status = gpiochip_sysfs_register(gdev);
1174 if (status)
1175 goto err_remove_device;
1176
1177 /* From this point, the .release() function cleans up gpio_device */
1178 gdev->dev.release = gpiodevice_release;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001179 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
1180 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
1181 dev_name(&gdev->dev), gdev->chip->label ? : "generic");
1182
1183 return 0;
1184
1185err_remove_device:
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001186 cdev_device_del(&gdev->chrdev, &gdev->dev);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001187 return status;
1188}
1189
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02001190static void gpiochip_machine_hog(struct gpio_chip *chip, struct gpiod_hog *hog)
1191{
1192 struct gpio_desc *desc;
1193 int rv;
1194
1195 desc = gpiochip_get_desc(chip, hog->chip_hwnum);
1196 if (IS_ERR(desc)) {
1197 pr_err("%s: unable to get GPIO desc: %ld\n",
1198 __func__, PTR_ERR(desc));
1199 return;
1200 }
1201
Dan Carpenterba3efdf2018-05-01 10:36:39 +03001202 if (test_bit(FLAG_IS_HOGGED, &desc->flags))
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02001203 return;
1204
1205 rv = gpiod_hog(desc, hog->line_name, hog->lflags, hog->dflags);
1206 if (rv)
1207 pr_err("%s: unable to hog GPIO line (%s:%u): %d\n",
1208 __func__, chip->label, hog->chip_hwnum, rv);
1209}
1210
1211static void machine_gpiochip_add(struct gpio_chip *chip)
1212{
1213 struct gpiod_hog *hog;
1214
1215 mutex_lock(&gpio_machine_hogs_mutex);
1216
1217 list_for_each_entry(hog, &gpio_machine_hogs, list) {
1218 if (!strcmp(chip->label, hog->chip_label))
1219 gpiochip_machine_hog(chip, hog);
1220 }
1221
1222 mutex_unlock(&gpio_machine_hogs_mutex);
1223}
1224
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001225static void gpiochip_setup_devs(void)
1226{
1227 struct gpio_device *gdev;
1228 int err;
1229
1230 list_for_each_entry(gdev, &gpio_devices, list) {
1231 err = gpiochip_setup_dev(gdev);
1232 if (err)
1233 pr_err("%s: Failed to initialize gpio device (%d)\n",
1234 dev_name(&gdev->dev), err);
1235 }
1236}
1237
Thierry Reding959bc7b2017-11-07 19:15:59 +01001238int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001239 struct lock_class_key *lock_key,
1240 struct lock_class_key *request_key)
David Brownelld2876d02008-02-04 22:28:20 -08001241{
1242 unsigned long flags;
1243 int status = 0;
Linus Walleijff2b1352015-10-20 11:10:38 +02001244 unsigned i;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001245 int base = chip->base;
Linus Walleijff2b1352015-10-20 11:10:38 +02001246 struct gpio_device *gdev;
David Brownelld2876d02008-02-04 22:28:20 -08001247
Linus Walleijff2b1352015-10-20 11:10:38 +02001248 /*
1249 * First: allocate and populate the internal stat container, and
1250 * set up the struct device.
1251 */
Josh Cartwright969f07b2016-02-17 16:44:15 -06001252 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
Linus Walleijff2b1352015-10-20 11:10:38 +02001253 if (!gdev)
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001254 return -ENOMEM;
Linus Walleij3c702e92015-10-21 15:29:53 +02001255 gdev->dev.bus = &gpio_bus_type;
Linus Walleijff2b1352015-10-20 11:10:38 +02001256 gdev->chip = chip;
1257 chip->gpiodev = gdev;
1258 if (chip->parent) {
1259 gdev->dev.parent = chip->parent;
1260 gdev->dev.of_node = chip->parent->of_node;
Thierry Redingacc6e332016-07-05 14:11:14 +02001261 }
1262
Linus Walleijff2b1352015-10-20 11:10:38 +02001263#ifdef CONFIG_OF_GPIO
1264 /* If the gpiochip has an assigned OF node this takes precedence */
Thierry Redingacc6e332016-07-05 14:11:14 +02001265 if (chip->of_node)
1266 gdev->dev.of_node = chip->of_node;
Biju Das6ff04972018-08-06 10:48:01 +01001267 else
1268 chip->of_node = gdev->dev.of_node;
Linus Walleijff2b1352015-10-20 11:10:38 +02001269#endif
Thierry Redingacc6e332016-07-05 14:11:14 +02001270
Linus Walleijff2b1352015-10-20 11:10:38 +02001271 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
1272 if (gdev->id < 0) {
1273 status = gdev->id;
1274 goto err_free_gdev;
1275 }
1276 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
1277 device_initialize(&gdev->dev);
1278 dev_set_drvdata(&gdev->dev, gdev);
1279 if (chip->parent && chip->parent->driver)
1280 gdev->owner = chip->parent->driver->owner;
1281 else if (chip->owner)
1282 /* TODO: remove chip->owner */
1283 gdev->owner = chip->owner;
1284 else
1285 gdev->owner = THIS_MODULE;
David Brownelld2876d02008-02-04 22:28:20 -08001286
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001287 gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001288 if (!gdev->descs) {
Linus Walleijff2b1352015-10-20 11:10:38 +02001289 status = -ENOMEM;
1290 goto err_free_gdev;
1291 }
1292
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001293 if (chip->ngpio == 0) {
1294 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
Linus Walleijff2b1352015-10-20 11:10:38 +02001295 status = -EINVAL;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001296 goto err_free_descs;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001297 }
Linus Walleijdf4878e2016-02-12 14:48:23 +01001298
Laura Abbott30277432018-05-21 10:57:07 -07001299 if (chip->ngpio > FASTPATH_NGPIO)
1300 chip_warn(chip, "line cnt %u is greater than fast path cnt %u\n",
1301 chip->ngpio, FASTPATH_NGPIO);
1302
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +01001303 gdev->label = kstrdup_const(chip->label ?: "unknown", GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001304 if (!gdev->label) {
1305 status = -ENOMEM;
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001306 goto err_free_descs;
Linus Walleijdf4878e2016-02-12 14:48:23 +01001307 }
1308
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001309 gdev->ngpio = chip->ngpio;
Linus Walleij43c54ec2016-02-11 11:37:48 +01001310 gdev->data = data;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001311
David Brownelld2876d02008-02-04 22:28:20 -08001312 spin_lock_irqsave(&gpio_lock, flags);
1313
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001314 /*
1315 * TODO: this allocates a Linux GPIO number base in the global
1316 * GPIO numberspace for this chip. In the long run we want to
1317 * get *rid* of this numberspace and use only descriptors, but
1318 * it may be a pipe dream. It will not happen before we get rid
1319 * of the sysfs interface anyways.
1320 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001321 if (base < 0) {
1322 base = gpiochip_find_base(chip->ngpio);
1323 if (base < 0) {
1324 status = base;
Johan Hovold225fce82015-01-12 17:12:25 +01001325 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001326 goto err_free_label;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001327 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001328 /*
1329 * TODO: it should not be necessary to reflect the assigned
1330 * base outside of the GPIO subsystem. Go over drivers and
1331 * see if anyone makes use of this, else drop this and assign
1332 * a poison instead.
1333 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001334 chip->base = base;
1335 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001336 gdev->base = base;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001337
Linus Walleijff2b1352015-10-20 11:10:38 +02001338 status = gpiodev_add_to_list(gdev);
Johan Hovold05aa5202015-01-12 17:12:26 +01001339 if (status) {
1340 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001341 goto err_free_label;
Johan Hovold05aa5202015-01-12 17:12:26 +01001342 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001343
Linus Walleij545ebd92016-05-30 17:11:59 +02001344 spin_unlock_irqrestore(&gpio_lock, flags);
1345
Linus Walleijff2b1352015-10-20 11:10:38 +02001346 for (i = 0; i < chip->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001347 struct gpio_desc *desc = &gdev->descs[i];
David Brownelld8f388d82008-07-25 01:46:07 -07001348
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001349 desc->gdev = gdev;
Timur Tabi1ca2a922017-12-20 13:10:31 -06001350
1351 /* REVISIT: most hardware initializes GPIOs as inputs (often
1352 * with pullups enabled) so power usage is minimized. Linux
1353 * code should set the gpio direction first thing; but until
1354 * it does, and in case chip->get_direction is not set, we may
1355 * expose the wrong direction in sysfs.
Johan Hovold05aa5202015-01-12 17:12:26 +01001356 */
Timur Tabi1ca2a922017-12-20 13:10:31 -06001357 desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0;
David Brownelld2876d02008-02-04 22:28:20 -08001358 }
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001359
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301360#ifdef CONFIG_PINCTRL
Linus Walleij20ec3e32016-02-11 11:03:06 +01001361 INIT_LIST_HEAD(&gdev->pin_ranges);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301362#endif
1363
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001364 status = gpiochip_set_desc_names(chip);
1365 if (status)
1366 goto err_remove_from_list;
1367
Mika Westerberg79b804c2016-09-20 15:15:21 +03001368 status = gpiochip_irqchip_init_valid_mask(chip);
1369 if (status)
1370 goto err_remove_from_list;
1371
Stephen Boyd726cb3b2018-03-23 09:34:52 -07001372 status = gpiochip_init_valid_mask(chip);
1373 if (status)
1374 goto err_remove_irqchip_mask;
1375
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001376 status = gpiochip_add_irqchip(chip, lock_key, request_key);
Thierry Redinge0d89722017-11-07 19:15:54 +01001377 if (status)
1378 goto err_remove_chip;
1379
Tomeu Vizoso28355f82015-07-14 10:29:54 +02001380 status = of_gpiochip_add(chip);
1381 if (status)
1382 goto err_remove_chip;
1383
Mika Westerberg664e3e52014-01-08 12:40:54 +02001384 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001385
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02001386 machine_gpiochip_add(chip);
1387
Linus Walleij3c702e92015-10-21 15:29:53 +02001388 /*
1389 * By first adding the chardev, and then adding the device,
1390 * we get a device node entry in sysfs under
1391 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1392 * coldplug of device nodes and other udev business.
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001393 * We can do this only if gpiolib has been initialized.
1394 * Otherwise, defer until later.
Linus Walleij3c702e92015-10-21 15:29:53 +02001395 */
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001396 if (gpiolib_initialized) {
1397 status = gpiochip_setup_dev(gdev);
1398 if (status)
1399 goto err_remove_chip;
1400 }
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001401 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001402
Johan Hovold225fce82015-01-12 17:12:25 +01001403err_remove_chip:
1404 acpi_gpiochip_remove(chip);
Johan Hovold6d867502015-05-04 17:23:25 +02001405 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +01001406 of_gpiochip_remove(chip);
Stephen Boyd726cb3b2018-03-23 09:34:52 -07001407 gpiochip_free_valid_mask(chip);
1408err_remove_irqchip_mask:
Mika Westerberg79b804c2016-09-20 15:15:21 +03001409 gpiochip_irqchip_free_valid_mask(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001410err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +01001411 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001412 list_del(&gdev->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001413 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001414err_free_label:
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +01001415 kfree_const(gdev->label);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001416err_free_descs:
1417 kfree(gdev->descs);
Linus Walleijff2b1352015-10-20 11:10:38 +02001418err_free_gdev:
1419 ida_simple_remove(&gpio_ida, gdev->id);
David Brownelld2876d02008-02-04 22:28:20 -08001420 /* failures here can mean systems won't boot... */
Marcel Ziswiler1777fc92018-07-20 09:54:49 +02001421 pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__,
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001422 gdev->base, gdev->base + gdev->ngpio - 1,
Marcel Ziswiler1777fc92018-07-20 09:54:49 +02001423 chip->label ? : "generic", status);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001424 kfree(gdev);
David Brownelld2876d02008-02-04 22:28:20 -08001425 return status;
1426}
Thierry Reding959bc7b2017-11-07 19:15:59 +01001427EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);
David Brownelld2876d02008-02-04 22:28:20 -08001428
1429/**
Linus Walleij43c54ec2016-02-11 11:37:48 +01001430 * gpiochip_get_data() - get per-subdriver data for the chip
Thierry Reding950d55f52017-07-24 16:57:22 +02001431 * @chip: GPIO chip
1432 *
1433 * Returns:
1434 * The per-subdriver data for the chip.
Linus Walleij43c54ec2016-02-11 11:37:48 +01001435 */
1436void *gpiochip_get_data(struct gpio_chip *chip)
1437{
1438 return chip->gpiodev->data;
1439}
1440EXPORT_SYMBOL_GPL(gpiochip_get_data);
1441
1442/**
David Brownelld2876d02008-02-04 22:28:20 -08001443 * gpiochip_remove() - unregister a gpio_chip
1444 * @chip: the chip to unregister
1445 *
1446 * A gpio_chip with any GPIOs still requested may not be removed.
1447 */
abdoulaye berthee1db1702014-07-05 18:28:50 +02001448void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -08001449{
Linus Walleijff2b1352015-10-20 11:10:38 +02001450 struct gpio_device *gdev = chip->gpiodev;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001451 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001452 unsigned long flags;
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001453 unsigned i;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001454 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -08001455
Linus Walleijff2b1352015-10-20 11:10:38 +02001456 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
Linus Walleijafbc4f32016-02-09 13:21:06 +01001457 gpiochip_sysfs_unregister(gdev);
Geert Uytterhoeven5018ada2016-12-19 18:29:23 +01001458 gpiochip_free_hogs(chip);
Bamvor Jian Zhangbd203bd2016-02-20 13:13:19 +08001459 /* Numb the device, cancelling all outstanding operations */
1460 gdev->chip = NULL;
Johan Hovold00acc3d2015-01-12 17:12:27 +01001461 gpiochip_irqchip_remove(chip);
Mika Westerberg6072b9d2014-03-10 14:54:53 +02001462 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +01001463 gpiochip_remove_pin_ranges(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001464 of_gpiochip_remove(chip);
Stephen Boyd726cb3b2018-03-23 09:34:52 -07001465 gpiochip_free_valid_mask(chip);
Linus Walleij43c54ec2016-02-11 11:37:48 +01001466 /*
1467 * We accept no more calls into the driver from this point, so
1468 * NULL the driver data pointer
1469 */
1470 gdev->data = NULL;
Anton Vorontsov391c9702010-06-08 07:48:17 -06001471
Johan Hovold6798aca2015-01-12 17:12:28 +01001472 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001473 for (i = 0; i < gdev->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001474 desc = &gdev->descs[i];
Johan Hovoldfab28b82015-05-04 17:10:27 +02001475 if (test_bit(FLAG_REQUESTED, &desc->flags))
1476 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -08001477 }
David Brownelld2876d02008-02-04 22:28:20 -08001478 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001479
Johan Hovoldfab28b82015-05-04 17:10:27 +02001480 if (requested)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001481 dev_crit(&gdev->dev,
Linus Walleij58383c782015-11-04 09:56:26 +01001482 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +02001483
Linus Walleijff2b1352015-10-20 11:10:38 +02001484 /*
1485 * The gpiochip side puts its use of the device to rest here:
1486 * if there are no userspace clients, the chardev and device will
1487 * be removed, else it will be dangling until the last user is
1488 * gone.
1489 */
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001490 cdev_device_del(&gdev->chrdev, &gdev->dev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001491 put_device(&gdev->dev);
David Brownelld2876d02008-02-04 22:28:20 -08001492}
1493EXPORT_SYMBOL_GPL(gpiochip_remove);
1494
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301495static void devm_gpio_chip_release(struct device *dev, void *res)
1496{
1497 struct gpio_chip *chip = *(struct gpio_chip **)res;
1498
1499 gpiochip_remove(chip);
1500}
1501
1502static int devm_gpio_chip_match(struct device *dev, void *res, void *data)
1503
1504{
1505 struct gpio_chip **r = res;
1506
1507 if (!r || !*r) {
1508 WARN_ON(!r || !*r);
1509 return 0;
1510 }
1511
1512 return *r == data;
1513}
1514
1515/**
Jonathan Neuschäfer689fd022017-12-21 17:56:34 +01001516 * devm_gpiochip_add_data() - Resource manager gpiochip_add_data()
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301517 * @dev: the device pointer on which irq_chip belongs to.
1518 * @chip: the chip to register, with chip->base initialized
Thierry Reding950d55f52017-07-24 16:57:22 +02001519 * @data: driver-private data associated with this chip
1520 *
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301521 * Context: potentially before irqs will work
1522 *
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301523 * The gpio chip automatically be released when the device is unbound.
Thierry Reding950d55f52017-07-24 16:57:22 +02001524 *
1525 * Returns:
1526 * A negative errno if the chip can't be registered, such as because the
1527 * chip->base is invalid or already associated with a different chip.
1528 * Otherwise it returns zero as a success code.
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301529 */
1530int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
1531 void *data)
1532{
1533 struct gpio_chip **ptr;
1534 int ret;
1535
1536 ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
1537 GFP_KERNEL);
1538 if (!ptr)
1539 return -ENOMEM;
1540
1541 ret = gpiochip_add_data(chip, data);
1542 if (ret < 0) {
1543 devres_free(ptr);
1544 return ret;
1545 }
1546
1547 *ptr = chip;
1548 devres_add(dev, ptr);
1549
1550 return 0;
1551}
1552EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
1553
1554/**
1555 * devm_gpiochip_remove() - Resource manager of gpiochip_remove()
1556 * @dev: device for which which resource was allocated
1557 * @chip: the chip to remove
1558 *
1559 * A gpio_chip with any GPIOs still requested may not be removed.
1560 */
1561void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip)
1562{
1563 int ret;
1564
1565 ret = devres_release(dev, devm_gpio_chip_release,
1566 devm_gpio_chip_match, chip);
Christophe JAILLET3988d662017-01-27 12:56:42 +01001567 WARN_ON(ret);
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301568}
1569EXPORT_SYMBOL_GPL(devm_gpiochip_remove);
1570
Grant Likely594fa262010-06-08 07:48:16 -06001571/**
1572 * gpiochip_find() - iterator for locating a specific gpio_chip
1573 * @data: data to pass to match function
Thierry Reding950d55f52017-07-24 16:57:22 +02001574 * @match: Callback function to check gpio_chip
Grant Likely594fa262010-06-08 07:48:16 -06001575 *
1576 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1577 * determined by a user supplied @match callback. The callback should return
1578 * 0 if the device doesn't match and non-zero if it does. If the callback is
1579 * non-zero, this function will return to the caller and not iterate over any
1580 * more gpio_chips.
1581 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001582struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001583 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001584 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001585{
Linus Walleijff2b1352015-10-20 11:10:38 +02001586 struct gpio_device *gdev;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001587 struct gpio_chip *chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -06001588 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001589
1590 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001591 list_for_each_entry(gdev, &gpio_devices, list)
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001592 if (gdev->chip && match(gdev->chip, data)) {
1593 chip = gdev->chip;
Grant Likely594fa262010-06-08 07:48:16 -06001594 break;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001595 }
Linus Walleijff2b1352015-10-20 11:10:38 +02001596
Grant Likely594fa262010-06-08 07:48:16 -06001597 spin_unlock_irqrestore(&gpio_lock, flags);
1598
1599 return chip;
1600}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001601EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001602
Alexandre Courbot79697ef2013-11-16 21:39:32 +09001603static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1604{
1605 const char *name = data;
1606
1607 return !strcmp(chip->label, name);
1608}
1609
1610static struct gpio_chip *find_chip_by_name(const char *name)
1611{
1612 return gpiochip_find((void *)name, gpiochip_match_name);
1613}
1614
Linus Walleij14250522014-03-25 10:40:18 +01001615#ifdef CONFIG_GPIOLIB_IRQCHIP
1616
1617/*
1618 * The following is irqchip helper code for gpiochips.
1619 */
1620
Mika Westerberg79b804c2016-09-20 15:15:21 +03001621static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1622{
Thierry Redingdc7b0382017-11-07 19:15:52 +01001623 if (!gpiochip->irq.need_valid_mask)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001624 return 0;
1625
Stephen Boyde4371f6e2018-03-23 09:34:50 -07001626 gpiochip->irq.valid_mask = gpiochip_allocate_mask(gpiochip);
Thierry Redingdc7b0382017-11-07 19:15:52 +01001627 if (!gpiochip->irq.valid_mask)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001628 return -ENOMEM;
1629
Mika Westerberg79b804c2016-09-20 15:15:21 +03001630 return 0;
1631}
1632
1633static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1634{
Thierry Redingdc7b0382017-11-07 19:15:52 +01001635 kfree(gpiochip->irq.valid_mask);
1636 gpiochip->irq.valid_mask = NULL;
Mika Westerberg79b804c2016-09-20 15:15:21 +03001637}
1638
Stephen Boyd64ff2c82018-01-09 17:58:46 -08001639bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
1640 unsigned int offset)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001641{
Stephen Boyd726cb3b2018-03-23 09:34:52 -07001642 if (!gpiochip_line_is_valid(gpiochip, offset))
1643 return false;
Mika Westerberg79b804c2016-09-20 15:15:21 +03001644 /* No mask means all valid */
Thierry Redingdc7b0382017-11-07 19:15:52 +01001645 if (likely(!gpiochip->irq.valid_mask))
Mika Westerberg79b804c2016-09-20 15:15:21 +03001646 return true;
Thierry Redingdc7b0382017-11-07 19:15:52 +01001647 return test_bit(offset, gpiochip->irq.valid_mask);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001648}
Stephen Boyd64ff2c82018-01-09 17:58:46 -08001649EXPORT_SYMBOL_GPL(gpiochip_irqchip_irq_valid);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001650
Linus Walleij14250522014-03-25 10:40:18 +01001651/**
Linus Walleijd245b3f2016-11-24 10:57:25 +01001652 * gpiochip_set_cascaded_irqchip() - connects a cascaded irqchip to a gpiochip
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001653 * @gpiochip: the gpiochip to set the irqchip chain to
1654 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01001655 * @parent_irq: the irq number corresponding to the parent IRQ for this
1656 * chained irqchip
1657 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001658 * coming out of the gpiochip. If the interrupt is nested rather than
1659 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +01001660 */
Linus Walleijd245b3f2016-11-24 10:57:25 +01001661static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gpiochip,
1662 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001663 unsigned int parent_irq,
Linus Walleijd245b3f2016-11-24 10:57:25 +01001664 irq_flow_handler_t parent_handler)
Linus Walleij14250522014-03-25 10:40:18 +01001665{
Linus Walleij83141a72014-09-26 13:50:12 +02001666 unsigned int offset;
1667
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001668 if (!gpiochip->irq.domain) {
Linus Walleij83141a72014-09-26 13:50:12 +02001669 chip_err(gpiochip, "called %s before setting up irqchip\n",
1670 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +02001671 return;
1672 }
1673
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001674 if (parent_handler) {
1675 if (gpiochip->can_sleep) {
1676 chip_err(gpiochip,
Andy Shevchenkob1911712018-07-03 03:39:03 +03001677 "you cannot have chained interrupts on a chip that may sleep\n");
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001678 return;
1679 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001680 /*
1681 * The parent irqchip is already using the chip_data for this
1682 * irqchip, so our callbacks simply use the handler_data.
1683 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +02001684 irq_set_chained_handler_and_data(parent_irq, parent_handler,
1685 gpiochip);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001686
Thierry Reding39e5f092017-11-07 19:15:50 +01001687 gpiochip->irq.parents = &parent_irq;
1688 gpiochip->irq.num_parents = 1;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001689 }
Linus Walleij83141a72014-09-26 13:50:12 +02001690
1691 /* Set the parent IRQ for all affected IRQs */
Mika Westerberg79b804c2016-09-20 15:15:21 +03001692 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1693 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1694 continue;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001695 irq_set_parent(irq_find_mapping(gpiochip->irq.domain, offset),
Linus Walleij83141a72014-09-26 13:50:12 +02001696 parent_irq);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001697 }
Linus Walleij14250522014-03-25 10:40:18 +01001698}
Linus Walleijd245b3f2016-11-24 10:57:25 +01001699
1700/**
1701 * gpiochip_set_chained_irqchip() - connects a chained irqchip to a gpiochip
1702 * @gpiochip: the gpiochip to set the irqchip chain to
1703 * @irqchip: the irqchip to chain to the gpiochip
1704 * @parent_irq: the irq number corresponding to the parent IRQ for this
1705 * chained irqchip
1706 * @parent_handler: the parent interrupt handler for the accumulated IRQ
1707 * coming out of the gpiochip. If the interrupt is nested rather than
1708 * cascaded, pass NULL in this handler argument
1709 */
1710void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
1711 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001712 unsigned int parent_irq,
Linus Walleijd245b3f2016-11-24 10:57:25 +01001713 irq_flow_handler_t parent_handler)
1714{
Thierry Reding60ed54c2017-11-07 19:15:57 +01001715 if (gpiochip->irq.threaded) {
1716 chip_err(gpiochip, "tried to chain a threaded gpiochip\n");
1717 return;
1718 }
1719
Linus Walleijd245b3f2016-11-24 10:57:25 +01001720 gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
1721 parent_handler);
1722}
Linus Walleij14250522014-03-25 10:40:18 +01001723EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
1724
1725/**
Linus Walleijd245b3f2016-11-24 10:57:25 +01001726 * gpiochip_set_nested_irqchip() - connects a nested irqchip to a gpiochip
1727 * @gpiochip: the gpiochip to set the irqchip nested handler to
1728 * @irqchip: the irqchip to nest to the gpiochip
1729 * @parent_irq: the irq number corresponding to the parent IRQ for this
1730 * nested irqchip
1731 */
1732void gpiochip_set_nested_irqchip(struct gpio_chip *gpiochip,
1733 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001734 unsigned int parent_irq)
Linus Walleijd245b3f2016-11-24 10:57:25 +01001735{
Linus Walleijd245b3f2016-11-24 10:57:25 +01001736 gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
1737 NULL);
1738}
1739EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip);
1740
1741/**
Linus Walleij14250522014-03-25 10:40:18 +01001742 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1743 * @d: the irqdomain used by this irqchip
1744 * @irq: the global irq number used by this GPIO irqchip irq
1745 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1746 *
1747 * This function will set up the mapping for a certain IRQ line on a
1748 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1749 * stored inside the gpiochip.
1750 */
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001751int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1752 irq_hw_number_t hwirq)
Linus Walleij14250522014-03-25 10:40:18 +01001753{
1754 struct gpio_chip *chip = d->host_data;
Thierry Redinge0d89722017-11-07 19:15:54 +01001755 int err = 0;
Linus Walleij14250522014-03-25 10:40:18 +01001756
Grygorii Strashkodc749a02017-07-21 11:49:00 -05001757 if (!gpiochip_irqchip_irq_valid(chip, hwirq))
1758 return -ENXIO;
1759
Linus Walleij14250522014-03-25 10:40:18 +01001760 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001761 /*
1762 * This lock class tells lockdep that GPIO irqs are in a different
1763 * category than their parents, so it won't report false recursion.
1764 */
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001765 irq_set_lockdep_class(irq, chip->irq.lock_key, chip->irq.request_key);
Thierry Redingc7a0aa52017-11-07 19:15:48 +01001766 irq_set_chip_and_handler(irq, chip->irq.chip, chip->irq.handler);
Linus Walleijd245b3f2016-11-24 10:57:25 +01001767 /* Chips that use nested thread handlers have them marked */
Thierry Reding60ed54c2017-11-07 19:15:57 +01001768 if (chip->irq.threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02001769 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +01001770 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -05001771
Thierry Redinge0d89722017-11-07 19:15:54 +01001772 if (chip->irq.num_parents == 1)
1773 err = irq_set_parent(irq, chip->irq.parents[0]);
1774 else if (chip->irq.map)
1775 err = irq_set_parent(irq, chip->irq.map[hwirq]);
1776
1777 if (err < 0)
1778 return err;
1779
Linus Walleij1333b902014-04-23 16:45:12 +02001780 /*
1781 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1782 * is passed as default type.
1783 */
Thierry Reding3634eeb2017-11-07 19:15:49 +01001784 if (chip->irq.default_type != IRQ_TYPE_NONE)
1785 irq_set_irq_type(irq, chip->irq.default_type);
Linus Walleij14250522014-03-25 10:40:18 +01001786
1787 return 0;
1788}
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001789EXPORT_SYMBOL_GPL(gpiochip_irq_map);
Linus Walleij14250522014-03-25 10:40:18 +01001790
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001791void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
Linus Walleijc3626fd2014-03-28 20:42:01 +01001792{
Linus Walleij1c8732b2014-04-09 13:34:39 +02001793 struct gpio_chip *chip = d->host_data;
1794
Thierry Reding60ed54c2017-11-07 19:15:57 +01001795 if (chip->irq.threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02001796 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001797 irq_set_chip_and_handler(irq, NULL, NULL);
1798 irq_set_chip_data(irq, NULL);
1799}
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001800EXPORT_SYMBOL_GPL(gpiochip_irq_unmap);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001801
Linus Walleij14250522014-03-25 10:40:18 +01001802static const struct irq_domain_ops gpiochip_domain_ops = {
1803 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +01001804 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +01001805 /* Virtually all GPIO irqchips are twocell:ed */
1806 .xlate = irq_domain_xlate_twocell,
1807};
1808
1809static int gpiochip_irq_reqres(struct irq_data *d)
1810{
1811 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
Andy Shevchenko3a2f3352018-07-30 15:38:31 +03001812 int ret;
Linus Walleij14250522014-03-25 10:40:18 +01001813
Linus Walleijff2b1352015-10-20 11:10:38 +02001814 if (!try_module_get(chip->gpiodev->owner))
Grygorii Strashko5b76e792015-06-25 20:30:50 +03001815 return -ENODEV;
1816
Andy Shevchenko3a2f3352018-07-30 15:38:31 +03001817 ret = gpiochip_lock_as_irq(chip, d->hwirq);
1818 if (ret) {
Linus Walleij14250522014-03-25 10:40:18 +01001819 chip_err(chip,
1820 "unable to lock HW IRQ %lu for IRQ\n",
1821 d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001822 module_put(chip->gpiodev->owner);
Andy Shevchenko3a2f3352018-07-30 15:38:31 +03001823 return ret;
Linus Walleij14250522014-03-25 10:40:18 +01001824 }
1825 return 0;
1826}
1827
1828static void gpiochip_irq_relres(struct irq_data *d)
1829{
1830 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1831
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001832 gpiochip_unlock_as_irq(chip, d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001833 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001834}
1835
1836static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
1837{
Grygorii Strashkodc749a02017-07-21 11:49:00 -05001838 if (!gpiochip_irqchip_irq_valid(chip, offset))
1839 return -ENXIO;
Thierry Redinge0d89722017-11-07 19:15:54 +01001840
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001841 return irq_create_mapping(chip->irq.domain, offset);
Linus Walleij14250522014-03-25 10:40:18 +01001842}
1843
1844/**
Thierry Redinge0d89722017-11-07 19:15:54 +01001845 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
1846 * @gpiochip: the GPIO chip to add the IRQ chip to
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001847 * @lock_key: lockdep class for IRQ lock
1848 * @request_key: lockdep class for IRQ request
Thierry Redinge0d89722017-11-07 19:15:54 +01001849 */
Thierry Reding959bc7b2017-11-07 19:15:59 +01001850static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001851 struct lock_class_key *lock_key,
1852 struct lock_class_key *request_key)
Thierry Redinge0d89722017-11-07 19:15:54 +01001853{
1854 struct irq_chip *irqchip = gpiochip->irq.chip;
1855 const struct irq_domain_ops *ops;
1856 struct device_node *np;
1857 unsigned int type;
1858 unsigned int i;
1859
1860 if (!irqchip)
1861 return 0;
1862
1863 if (gpiochip->irq.parent_handler && gpiochip->can_sleep) {
Andy Shevchenkob1911712018-07-03 03:39:03 +03001864 chip_err(gpiochip, "you cannot have chained interrupts on a chip that may sleep\n");
Thierry Redinge0d89722017-11-07 19:15:54 +01001865 return -EINVAL;
1866 }
1867
1868 np = gpiochip->gpiodev->dev.of_node;
1869 type = gpiochip->irq.default_type;
1870
1871 /*
1872 * Specifying a default trigger is a terrible idea if DT or ACPI is
1873 * used to configure the interrupts, as you may end up with
1874 * conflicting triggers. Tell the user, and reset to NONE.
1875 */
1876 if (WARN(np && type != IRQ_TYPE_NONE,
1877 "%s: Ignoring %u default trigger\n", np->full_name, type))
1878 type = IRQ_TYPE_NONE;
1879
1880 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
1881 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1882 "Ignoring %u default trigger\n", type);
1883 type = IRQ_TYPE_NONE;
1884 }
1885
1886 gpiochip->to_irq = gpiochip_to_irq;
1887 gpiochip->irq.default_type = type;
Thierry Reding959bc7b2017-11-07 19:15:59 +01001888 gpiochip->irq.lock_key = lock_key;
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001889 gpiochip->irq.request_key = request_key;
Thierry Redinge0d89722017-11-07 19:15:54 +01001890
1891 if (gpiochip->irq.domain_ops)
1892 ops = gpiochip->irq.domain_ops;
1893 else
1894 ops = &gpiochip_domain_ops;
1895
1896 gpiochip->irq.domain = irq_domain_add_simple(np, gpiochip->ngpio,
Thierry Reding8302cf52017-11-07 19:15:58 +01001897 gpiochip->irq.first,
1898 ops, gpiochip);
Thierry Redinge0d89722017-11-07 19:15:54 +01001899 if (!gpiochip->irq.domain)
1900 return -EINVAL;
1901
1902 /*
1903 * It is possible for a driver to override this, but only if the
1904 * alternative functions are both implemented.
1905 */
1906 if (!irqchip->irq_request_resources &&
1907 !irqchip->irq_release_resources) {
1908 irqchip->irq_request_resources = gpiochip_irq_reqres;
1909 irqchip->irq_release_resources = gpiochip_irq_relres;
1910 }
1911
1912 if (gpiochip->irq.parent_handler) {
1913 void *data = gpiochip->irq.parent_handler_data ?: gpiochip;
1914
1915 for (i = 0; i < gpiochip->irq.num_parents; i++) {
1916 /*
1917 * The parent IRQ chip is already using the chip_data
1918 * for this IRQ chip, so our callbacks simply use the
1919 * handler_data.
1920 */
1921 irq_set_chained_handler_and_data(gpiochip->irq.parents[i],
1922 gpiochip->irq.parent_handler,
1923 data);
1924 }
Thierry Redinge0d89722017-11-07 19:15:54 +01001925 }
1926
1927 acpi_gpiochip_request_interrupts(gpiochip);
1928
1929 return 0;
1930}
1931
1932/**
Linus Walleij14250522014-03-25 10:40:18 +01001933 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1934 * @gpiochip: the gpiochip to remove the irqchip from
1935 *
1936 * This is called only from gpiochip_remove()
1937 */
1938static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
1939{
Thierry Reding39e5f092017-11-07 19:15:50 +01001940 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +01001941
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001942 acpi_gpiochip_free_interrupts(gpiochip);
1943
Thierry Redinge0d89722017-11-07 19:15:54 +01001944 if (gpiochip->irq.chip && gpiochip->irq.parent_handler) {
Thierry Reding39e5f092017-11-07 19:15:50 +01001945 struct gpio_irq_chip *irq = &gpiochip->irq;
1946 unsigned int i;
1947
1948 for (i = 0; i < irq->num_parents; i++)
1949 irq_set_chained_handler_and_data(irq->parents[i],
1950 NULL, NULL);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001951 }
1952
Linus Walleijc3626fd2014-03-28 20:42:01 +01001953 /* Remove all IRQ mappings and delete the domain */
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001954 if (gpiochip->irq.domain) {
Thierry Reding39e5f092017-11-07 19:15:50 +01001955 unsigned int irq;
1956
Mika Westerberg79b804c2016-09-20 15:15:21 +03001957 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1958 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1959 continue;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001960
1961 irq = irq_find_mapping(gpiochip->irq.domain, offset);
1962 irq_dispose_mapping(irq);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001963 }
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001964
1965 irq_domain_remove(gpiochip->irq.domain);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001966 }
Linus Walleij14250522014-03-25 10:40:18 +01001967
Thierry Redingda80ff82017-11-07 19:15:46 +01001968 if (gpiochip->irq.chip) {
1969 gpiochip->irq.chip->irq_request_resources = NULL;
1970 gpiochip->irq.chip->irq_release_resources = NULL;
1971 gpiochip->irq.chip = NULL;
Linus Walleij14250522014-03-25 10:40:18 +01001972 }
Mika Westerberg79b804c2016-09-20 15:15:21 +03001973
1974 gpiochip_irqchip_free_valid_mask(gpiochip);
Linus Walleij14250522014-03-25 10:40:18 +01001975}
1976
1977/**
Linus Walleij739e6f52017-01-11 13:37:07 +01001978 * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01001979 * @gpiochip: the gpiochip to add the irqchip to
1980 * @irqchip: the irqchip to add to the gpiochip
1981 * @first_irq: if not dynamically assigned, the base (first) IRQ to
1982 * allocate gpiochip irqs from
1983 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +02001984 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
1985 * to have the core avoid setting up any default type in the hardware.
Thierry Reding60ed54c2017-11-07 19:15:57 +01001986 * @threaded: whether this irqchip uses a nested thread handler
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001987 * @lock_key: lockdep class for IRQ lock
1988 * @request_key: lockdep class for IRQ request
Linus Walleij14250522014-03-25 10:40:18 +01001989 *
1990 * This function closely associates a certain irqchip with a certain
1991 * gpiochip, providing an irq domain to translate the local IRQs to
1992 * global irqs in the gpiolib core, and making sure that the gpiochip
1993 * is passed as chip data to all related functions. Driver callbacks
Linus Walleij09dd5f92015-12-07 15:31:58 +01001994 * need to use gpiochip_get_data() to get their local state containers back
Linus Walleij14250522014-03-25 10:40:18 +01001995 * from the gpiochip passed as chip data. An irqdomain will be stored
1996 * in the gpiochip that shall be used by the driver to handle IRQ number
1997 * translation. The gpiochip will need to be initialized and registered
1998 * before calling this function.
1999 *
Linus Walleijc3626fd2014-03-28 20:42:01 +01002000 * This function will handle two cell:ed simple IRQs and assumes all
2001 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +01002002 * need to be open coded.
2003 */
Linus Walleij739e6f52017-01-11 13:37:07 +01002004int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
2005 struct irq_chip *irqchip,
2006 unsigned int first_irq,
2007 irq_flow_handler_t handler,
2008 unsigned int type,
Thierry Reding60ed54c2017-11-07 19:15:57 +01002009 bool threaded,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002010 struct lock_class_key *lock_key,
2011 struct lock_class_key *request_key)
Linus Walleij14250522014-03-25 10:40:18 +01002012{
2013 struct device_node *of_node;
Linus Walleij14250522014-03-25 10:40:18 +01002014
2015 if (!gpiochip || !irqchip)
2016 return -EINVAL;
2017
Linus Walleij58383c782015-11-04 09:56:26 +01002018 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +01002019 pr_err("missing gpiochip .dev parent pointer\n");
2020 return -EINVAL;
2021 }
Thierry Reding60ed54c2017-11-07 19:15:57 +01002022 gpiochip->irq.threaded = threaded;
Linus Walleij58383c782015-11-04 09:56:26 +01002023 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +01002024#ifdef CONFIG_OF_GPIO
2025 /*
Colin Cronin20a8a962015-05-18 11:41:43 -07002026 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08002027 * FIXME: get rid of this and use gpiochip->parent->of_node
2028 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +01002029 */
2030 if (gpiochip->of_node)
2031 of_node = gpiochip->of_node;
2032#endif
Marc Zyngier332e99d2016-09-07 09:12:11 +01002033 /*
Mika Westerberg0a1e0052016-09-12 14:29:51 +03002034 * Specifying a default trigger is a terrible idea if DT or ACPI is
Marc Zyngier332e99d2016-09-07 09:12:11 +01002035 * used to configure the interrupts, as you may end-up with
2036 * conflicting triggers. Tell the user, and reset to NONE.
2037 */
2038 if (WARN(of_node && type != IRQ_TYPE_NONE,
Rob Herring7eb6ce22017-07-18 16:43:03 -05002039 "%pOF: Ignoring %d default trigger\n", of_node, type))
Marc Zyngier332e99d2016-09-07 09:12:11 +01002040 type = IRQ_TYPE_NONE;
Mika Westerberg0a1e0052016-09-12 14:29:51 +03002041 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
2042 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
2043 "Ignoring %d default trigger\n", type);
2044 type = IRQ_TYPE_NONE;
2045 }
Marc Zyngier332e99d2016-09-07 09:12:11 +01002046
Thierry Redingda80ff82017-11-07 19:15:46 +01002047 gpiochip->irq.chip = irqchip;
Thierry Redingc7a0aa52017-11-07 19:15:48 +01002048 gpiochip->irq.handler = handler;
Thierry Reding3634eeb2017-11-07 19:15:49 +01002049 gpiochip->irq.default_type = type;
Linus Walleij14250522014-03-25 10:40:18 +01002050 gpiochip->to_irq = gpiochip_to_irq;
Thierry Redingca9df052017-11-07 19:15:53 +01002051 gpiochip->irq.lock_key = lock_key;
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002052 gpiochip->irq.request_key = request_key;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01002053 gpiochip->irq.domain = irq_domain_add_simple(of_node,
Linus Walleij14250522014-03-25 10:40:18 +01002054 gpiochip->ngpio, first_irq,
2055 &gpiochip_domain_ops, gpiochip);
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01002056 if (!gpiochip->irq.domain) {
Thierry Redingda80ff82017-11-07 19:15:46 +01002057 gpiochip->irq.chip = NULL;
Linus Walleij14250522014-03-25 10:40:18 +01002058 return -EINVAL;
2059 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +02002060
2061 /*
2062 * It is possible for a driver to override this, but only if the
2063 * alternative functions are both implemented.
2064 */
2065 if (!irqchip->irq_request_resources &&
2066 !irqchip->irq_release_resources) {
2067 irqchip->irq_request_resources = gpiochip_irq_reqres;
2068 irqchip->irq_release_resources = gpiochip_irq_relres;
2069 }
Linus Walleij14250522014-03-25 10:40:18 +01002070
Mika Westerbergafa82fa2014-07-25 09:54:48 +03002071 acpi_gpiochip_request_interrupts(gpiochip);
2072
Linus Walleij14250522014-03-25 10:40:18 +01002073 return 0;
2074}
Linus Walleij739e6f52017-01-11 13:37:07 +01002075EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key);
Linus Walleij14250522014-03-25 10:40:18 +01002076
2077#else /* CONFIG_GPIOLIB_IRQCHIP */
2078
Thierry Reding959bc7b2017-11-07 19:15:59 +01002079static inline int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002080 struct lock_class_key *lock_key,
2081 struct lock_class_key *request_key)
Thierry Redinge0d89722017-11-07 19:15:54 +01002082{
2083 return 0;
2084}
2085
Linus Walleij14250522014-03-25 10:40:18 +01002086static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
Mika Westerberg79b804c2016-09-20 15:15:21 +03002087static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
2088{
2089 return 0;
2090}
2091static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
2092{ }
Linus Walleij14250522014-03-25 10:40:18 +01002093
2094#endif /* CONFIG_GPIOLIB_IRQCHIP */
2095
Jonas Gorskic771c2f2015-10-11 17:34:15 +02002096/**
2097 * gpiochip_generic_request() - request the gpio function for a pin
2098 * @chip: the gpiochip owning the GPIO
2099 * @offset: the offset of the GPIO to request for GPIO function
2100 */
2101int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
2102{
Linus Walleija9a1d2a2017-09-22 11:02:10 +02002103 return pinctrl_gpio_request(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02002104}
2105EXPORT_SYMBOL_GPL(gpiochip_generic_request);
2106
2107/**
2108 * gpiochip_generic_free() - free the gpio function from a pin
2109 * @chip: the gpiochip to request the gpio function for
2110 * @offset: the offset of the GPIO to free from GPIO function
2111 */
2112void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
2113{
Linus Walleija9a1d2a2017-09-22 11:02:10 +02002114 pinctrl_gpio_free(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02002115}
2116EXPORT_SYMBOL_GPL(gpiochip_generic_free);
2117
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002118/**
2119 * gpiochip_generic_config() - apply configuration for a pin
2120 * @chip: the gpiochip owning the GPIO
2121 * @offset: the offset of the GPIO to apply the configuration
2122 * @config: the configuration to be applied
2123 */
2124int gpiochip_generic_config(struct gpio_chip *chip, unsigned offset,
2125 unsigned long config)
2126{
2127 return pinctrl_gpio_set_config(chip->gpiodev->base + offset, config);
2128}
2129EXPORT_SYMBOL_GPL(gpiochip_generic_config);
2130
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302131#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01002132
Linus Walleij3f0f8672012-11-20 12:40:15 +01002133/**
Christian Ruppert586a87e2013-10-15 15:37:54 +02002134 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
2135 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +02002136 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +02002137 * @gpio_offset: the start offset in the current gpio_chip number space
2138 * @pin_group: name of the pin group inside the pin controller
Christian Lamparter973c1712018-05-21 22:57:39 +02002139 *
2140 * Calling this function directly from a DeviceTree-supported
2141 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
2142 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
2143 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
Christian Ruppert586a87e2013-10-15 15:37:54 +02002144 */
2145int gpiochip_add_pingroup_range(struct gpio_chip *chip,
2146 struct pinctrl_dev *pctldev,
2147 unsigned int gpio_offset, const char *pin_group)
2148{
2149 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002150 struct gpio_device *gdev = chip->gpiodev;
Christian Ruppert586a87e2013-10-15 15:37:54 +02002151 int ret;
2152
2153 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
2154 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002155 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +02002156 return -ENOMEM;
2157 }
2158
2159 /* Use local offset as range ID */
2160 pin_range->range.id = gpio_offset;
2161 pin_range->range.gc = chip;
2162 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002163 pin_range->range.base = gdev->base + gpio_offset;
Christian Ruppert586a87e2013-10-15 15:37:54 +02002164 pin_range->pctldev = pctldev;
2165
2166 ret = pinctrl_get_group_pins(pctldev, pin_group,
2167 &pin_range->range.pins,
2168 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +01002169 if (ret < 0) {
2170 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +02002171 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +01002172 }
Christian Ruppert586a87e2013-10-15 15:37:54 +02002173
2174 pinctrl_add_gpio_range(pctldev, &pin_range->range);
2175
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002176 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
2177 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +02002178 pinctrl_dev_get_devname(pctldev), pin_group);
2179
Linus Walleij20ec3e32016-02-11 11:03:06 +01002180 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Christian Ruppert586a87e2013-10-15 15:37:54 +02002181
2182 return 0;
2183}
2184EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
2185
2186/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01002187 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
2188 * @chip: the gpiochip to add the range for
Thierry Reding950d55f52017-07-24 16:57:22 +02002189 * @pinctl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01002190 * @gpio_offset: the start offset in the current gpio_chip number space
2191 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01002192 * @npins: the number of pins from the offset of each pin space (GPIO and
2193 * pin controller) to accumulate in this range
Thierry Reding950d55f52017-07-24 16:57:22 +02002194 *
2195 * Returns:
2196 * 0 on success, or a negative error-code on failure.
Christian Lamparter973c1712018-05-21 22:57:39 +02002197 *
2198 * Calling this function directly from a DeviceTree-supported
2199 * pinctrl driver is DEPRECATED. Please see Section 2.1 of
2200 * Documentation/devicetree/bindings/gpio/gpio.txt on how to
2201 * bind pinctrl and gpio drivers via the "gpio-ranges" property.
Linus Walleij3f0f8672012-11-20 12:40:15 +01002202 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002203int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01002204 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01002205 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302206{
2207 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002208 struct gpio_device *gdev = chip->gpiodev;
Axel Linb4d4b1f2012-11-21 14:33:56 +08002209 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302210
Linus Walleij3f0f8672012-11-20 12:40:15 +01002211 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302212 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002213 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002214 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302215 }
2216
Linus Walleij3f0f8672012-11-20 12:40:15 +01002217 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01002218 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01002219 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302220 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002221 pin_range->range.base = gdev->base + gpio_offset;
Linus Walleij316511c2012-11-21 08:48:09 +01002222 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302223 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01002224 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302225 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01002226 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08002227 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002228 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +01002229 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08002230 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01002231 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002232 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
2233 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +01002234 pinctl_name,
2235 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302236
Linus Walleij20ec3e32016-02-11 11:03:06 +01002237 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002238
2239 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302240}
Linus Walleij165adc92012-11-06 14:49:39 +01002241EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302242
Linus Walleij3f0f8672012-11-20 12:40:15 +01002243/**
2244 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
2245 * @chip: the chip to remove all the mappings for
2246 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302247void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
2248{
2249 struct gpio_pin_range *pin_range, *tmp;
Linus Walleij20ec3e32016-02-11 11:03:06 +01002250 struct gpio_device *gdev = chip->gpiodev;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302251
Linus Walleij20ec3e32016-02-11 11:03:06 +01002252 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302253 list_del(&pin_range->node);
2254 pinctrl_remove_gpio_range(pin_range->pctldev,
2255 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01002256 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302257 }
2258}
Linus Walleij165adc92012-11-06 14:49:39 +01002259EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
2260
2261#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302262
David Brownelld2876d02008-02-04 22:28:20 -08002263/* These "optional" allocation calls help prevent drivers from stomping
2264 * on each other, and help provide better diagnostics in debugfs.
2265 * They're called even less than the "set direction" calls.
2266 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002267static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08002268{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002269 struct gpio_chip *chip = desc->gdev->chip;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002270 int status;
David Brownelld2876d02008-02-04 22:28:20 -08002271 unsigned long flags;
Biju Das3789f5a2018-08-07 08:15:18 +01002272 unsigned offset;
David Brownelld2876d02008-02-04 22:28:20 -08002273
2274 spin_lock_irqsave(&gpio_lock, flags);
2275
David Brownelld2876d02008-02-04 22:28:20 -08002276 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07002277 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08002278 */
2279
2280 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
2281 desc_set_label(desc, label ? : "?");
2282 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07002283 } else {
David Brownelld2876d02008-02-04 22:28:20 -08002284 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -08002285 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07002286 }
2287
2288 if (chip->request) {
2289 /* chip->request may sleep */
2290 spin_unlock_irqrestore(&gpio_lock, flags);
Biju Das3789f5a2018-08-07 08:15:18 +01002291 offset = gpio_chip_hwgpio(desc);
2292 if (gpiochip_line_is_valid(chip, offset))
2293 status = chip->request(chip, offset);
2294 else
2295 status = -EINVAL;
David Brownell35e8bb52008-10-15 22:03:16 -07002296 spin_lock_irqsave(&gpio_lock, flags);
2297
2298 if (status < 0) {
2299 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -07002300 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03002301 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07002302 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07002303 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03002304 if (chip->get_direction) {
2305 /* chip->get_direction may sleep */
2306 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002307 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03002308 spin_lock_irqsave(&gpio_lock, flags);
2309 }
David Brownelld2876d02008-02-04 22:28:20 -08002310done:
Mika Westerberg77c2d792014-03-10 14:54:50 +02002311 spin_unlock_irqrestore(&gpio_lock, flags);
2312 return status;
2313}
2314
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002315/*
2316 * This descriptor validation needs to be inserted verbatim into each
2317 * function taking a descriptor, so we need to use a preprocessor
Linus Walleij54d77192016-05-30 16:48:39 +02002318 * macro to avoid endless duplication. If the desc is NULL it is an
2319 * optional GPIO and calls should just bail out.
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002320 */
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002321static int validate_desc(const struct gpio_desc *desc, const char *func)
2322{
2323 if (!desc)
2324 return 0;
2325 if (IS_ERR(desc)) {
2326 pr_warn("%s: invalid GPIO (errorpointer)\n", func);
2327 return PTR_ERR(desc);
2328 }
2329 if (!desc->gdev) {
2330 pr_warn("%s: invalid GPIO (no device)\n", func);
2331 return -EINVAL;
2332 }
2333 if (!desc->gdev->chip) {
2334 dev_warn(&desc->gdev->dev,
2335 "%s: backing chip is gone\n", func);
2336 return 0;
2337 }
2338 return 1;
2339}
2340
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002341#define VALIDATE_DESC(desc) do { \
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002342 int __valid = validate_desc(desc, __func__); \
2343 if (__valid <= 0) \
2344 return __valid; \
2345 } while (0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002346
2347#define VALIDATE_DESC_VOID(desc) do { \
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002348 int __valid = validate_desc(desc, __func__); \
2349 if (__valid <= 0) \
Linus Walleij54d77192016-05-30 16:48:39 +02002350 return; \
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002351 } while (0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002352
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002353int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002354{
2355 int status = -EPROBE_DEFER;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002356 struct gpio_device *gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002357
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002358 VALIDATE_DESC(desc);
2359 gdev = desc->gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002360
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002361 if (try_module_get(gdev->owner)) {
Linus Walleijfac9d882017-09-26 20:58:28 +02002362 status = gpiod_request_commit(desc, label);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002363 if (status < 0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002364 module_put(gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002365 else
2366 get_device(&gdev->dev);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002367 }
2368
David Brownelld2876d02008-02-04 22:28:20 -08002369 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +02002370 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002371
David Brownelld2876d02008-02-04 22:28:20 -08002372 return status;
2373}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002374
Linus Walleijfac9d882017-09-26 20:58:28 +02002375static bool gpiod_free_commit(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002376{
Mika Westerberg77c2d792014-03-10 14:54:50 +02002377 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08002378 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07002379 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002380
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07002381 might_sleep();
2382
Alexandre Courbot372e7222013-02-03 01:29:29 +09002383 gpiod_unexport(desc);
David Brownelld8f388d82008-07-25 01:46:07 -07002384
David Brownelld2876d02008-02-04 22:28:20 -08002385 spin_lock_irqsave(&gpio_lock, flags);
2386
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002387 chip = desc->gdev->chip;
David Brownell35e8bb52008-10-15 22:03:16 -07002388 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
2389 if (chip->free) {
2390 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07002391 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002392 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07002393 spin_lock_irqsave(&gpio_lock, flags);
2394 }
David Brownelld2876d02008-02-04 22:28:20 -08002395 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08002396 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07002397 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302398 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302399 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06002400 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002401 ret = true;
2402 }
David Brownelld2876d02008-02-04 22:28:20 -08002403
2404 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002405 return ret;
2406}
2407
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002408void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002409{
Linus Walleijfac9d882017-09-26 20:58:28 +02002410 if (desc && desc->gdev && gpiod_free_commit(desc)) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002411 module_put(desc->gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002412 put_device(&desc->gdev->dev);
2413 } else {
Mika Westerberg77c2d792014-03-10 14:54:50 +02002414 WARN_ON(extra_checks);
Linus Walleij33a68e82016-02-11 10:28:44 +01002415 }
David Brownelld2876d02008-02-04 22:28:20 -08002416}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002417
David Brownelld2876d02008-02-04 22:28:20 -08002418/**
2419 * gpiochip_is_requested - return string iff signal was requested
2420 * @chip: controller managing the signal
2421 * @offset: of signal within controller's 0..(ngpio - 1) range
2422 *
2423 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09002424 * The string returned is the label passed to gpio_request(); if none has been
2425 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08002426 *
2427 * This function is for use by GPIO controller drivers. The label can
2428 * help with diagnostics, and knowing that the signal is used as a GPIO
2429 * can help avoid accidentally multiplexing it to another controller.
2430 */
2431const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
2432{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002433 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08002434
Dirk Behme48b59532015-08-18 18:02:32 +02002435 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08002436 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002437
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002438 desc = &chip->gpiodev->descs[offset];
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002439
Alexandre Courbot372e7222013-02-03 01:29:29 +09002440 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08002441 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002442 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08002443}
2444EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2445
Mika Westerberg77c2d792014-03-10 14:54:50 +02002446/**
2447 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
Thierry Reding950d55f52017-07-24 16:57:22 +02002448 * @chip: GPIO chip
2449 * @hwnum: hardware number of the GPIO for which to request the descriptor
Mika Westerberg77c2d792014-03-10 14:54:50 +02002450 * @label: label for the GPIO
2451 *
2452 * Function allows GPIO chip drivers to request and use their own GPIO
2453 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2454 * function will not increase reference count of the GPIO chip module. This
2455 * allows the GPIO chip module to be unloaded as needed (we assume that the
2456 * GPIO chip driver handles freeing the GPIOs it has requested).
Thierry Reding950d55f52017-07-24 16:57:22 +02002457 *
2458 * Returns:
2459 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2460 * code on failure.
Mika Westerberg77c2d792014-03-10 14:54:50 +02002461 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002462struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
2463 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002464{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002465 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
2466 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002467
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002468 if (IS_ERR(desc)) {
2469 chip_err(chip, "failed to get GPIO descriptor\n");
2470 return desc;
2471 }
2472
Linus Walleijfac9d882017-09-26 20:58:28 +02002473 err = gpiod_request_commit(desc, label);
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002474 if (err < 0)
2475 return ERR_PTR(err);
2476
2477 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002478}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002479EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002480
2481/**
2482 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2483 * @desc: GPIO descriptor to free
2484 *
2485 * Function frees the given GPIO requested previously with
2486 * gpiochip_request_own_desc().
2487 */
2488void gpiochip_free_own_desc(struct gpio_desc *desc)
2489{
2490 if (desc)
Linus Walleijfac9d882017-09-26 20:58:28 +02002491 gpiod_free_commit(desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002492}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002493EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08002494
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002495/*
2496 * Drivers MUST set GPIO direction before making get/set calls. In
David Brownelld2876d02008-02-04 22:28:20 -08002497 * some cases this is done in early boot, before IRQs are enabled.
2498 *
2499 * As a rule these aren't called more than once (except for drivers
2500 * using the open-drain emulation idiom) so these are natural places
2501 * to accumulate extra debugging checks. Note that we can't (yet)
2502 * rely on gpio_request() having been called beforehand.
2503 */
2504
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002505/**
2506 * gpiod_direction_input - set the GPIO direction to input
2507 * @desc: GPIO to set to input
2508 *
2509 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2510 * be called safely on it.
2511 *
2512 * Return 0 in case of success, else an error code.
2513 */
2514int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002515{
David Brownelld2876d02008-02-04 22:28:20 -08002516 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002517 int status = -EINVAL;
2518
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002519 VALIDATE_DESC(desc);
2520 chip = desc->gdev->chip;
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002521
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002522 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01002523 gpiod_warn(desc,
2524 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02002525 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002526 return -EIO;
2527 }
2528
Alexandre Courbotd82da792014-07-22 16:17:43 +09002529 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -08002530 if (status == 0)
2531 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002532
Alexandre Courbot372e7222013-02-03 01:29:29 +09002533 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09002534
David Brownelld2876d02008-02-04 22:28:20 -08002535 return status;
2536}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002537EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002538
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002539static int gpio_set_drive_single_ended(struct gpio_chip *gc, unsigned offset,
2540 enum pin_config_param mode)
2541{
2542 unsigned long config = { PIN_CONF_PACKED(mode, 0) };
2543
2544 return gc->set_config ? gc->set_config(gc, offset, config) : -ENOTSUPP;
2545}
2546
Linus Walleijfac9d882017-09-26 20:58:28 +02002547static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08002548{
Linus Walleijc663e5f2016-03-22 10:51:16 +01002549 struct gpio_chip *gc = desc->gdev->chip;
Linus Walleijad177312016-11-13 23:02:44 +01002550 int val = !!value;
Linus Walleijc663e5f2016-03-22 10:51:16 +01002551 int ret;
David Brownelld2876d02008-02-04 22:28:20 -08002552
Linus Walleijc663e5f2016-03-22 10:51:16 +01002553 if (!gc->set || !gc->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01002554 gpiod_warn(desc,
2555 "%s: missing set() or direction_output() operations\n",
2556 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002557 return -EIO;
2558 }
2559
Linus Walleijad177312016-11-13 23:02:44 +01002560 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
Linus Walleijc663e5f2016-03-22 10:51:16 +01002561 if (!ret)
David Brownelld2876d02008-02-04 22:28:20 -08002562 set_bit(FLAG_IS_OUT, &desc->flags);
Linus Walleijad177312016-11-13 23:02:44 +01002563 trace_gpio_value(desc_to_gpio(desc), 0, val);
Linus Walleijc663e5f2016-03-22 10:51:16 +01002564 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2565 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08002566}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002567
2568/**
2569 * gpiod_direction_output_raw - set the GPIO direction to output
2570 * @desc: GPIO to set to output
2571 * @value: initial output value of the GPIO
2572 *
2573 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2574 * be called safely on it. The initial value of the output must be specified
2575 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2576 *
2577 * Return 0 in case of success, else an error code.
2578 */
2579int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2580{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002581 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02002582 return gpiod_direction_output_raw_commit(desc, value);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002583}
2584EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2585
2586/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05302587 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002588 * @desc: GPIO to set to output
2589 * @value: initial output value of the GPIO
2590 *
2591 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2592 * be called safely on it. The initial value of the output must be specified
2593 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2594 * account.
2595 *
2596 * Return 0 in case of success, else an error code.
2597 */
2598int gpiod_direction_output(struct gpio_desc *desc, int value)
2599{
Vladimir Zapolskiy30322bc2017-12-21 18:37:24 +02002600 struct gpio_chip *gc;
Linus Walleij02e47982017-09-26 21:20:23 +02002601 int ret;
2602
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002603 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002604 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2605 value = !value;
Linus Walleijad177312016-11-13 23:02:44 +01002606 else
2607 value = !!value;
Linus Walleij02e47982017-09-26 21:20:23 +02002608
2609 /* GPIOs used for IRQs shall not be set as output */
2610 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
2611 gpiod_err(desc,
2612 "%s: tried to set a GPIO tied to an IRQ as output\n",
2613 __func__);
2614 return -EIO;
2615 }
2616
Vladimir Zapolskiy30322bc2017-12-21 18:37:24 +02002617 gc = desc->gdev->chip;
Linus Walleij02e47982017-09-26 21:20:23 +02002618 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2619 /* First see if we can enable open drain in hardware */
2620 ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2621 PIN_CONFIG_DRIVE_OPEN_DRAIN);
2622 if (!ret)
2623 goto set_output_value;
2624 /* Emulate open drain by not actively driving the line high */
2625 if (value)
2626 return gpiod_direction_input(desc);
2627 }
2628 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2629 ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2630 PIN_CONFIG_DRIVE_OPEN_SOURCE);
2631 if (!ret)
2632 goto set_output_value;
2633 /* Emulate open source by not actively driving the line low */
2634 if (!value)
2635 return gpiod_direction_input(desc);
2636 } else {
2637 gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2638 PIN_CONFIG_DRIVE_PUSH_PULL);
2639 }
2640
2641set_output_value:
Linus Walleijfac9d882017-09-26 20:58:28 +02002642 return gpiod_direction_output_raw_commit(desc, value);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002643}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002644EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08002645
Felipe Balbic4b5be92010-05-26 14:42:23 -07002646/**
Thierry Reding950d55f52017-07-24 16:57:22 +02002647 * gpiod_set_debounce - sets @debounce time for a GPIO
2648 * @desc: descriptor of the GPIO for which to set debounce time
2649 * @debounce: debounce time in microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02002650 *
Thierry Reding950d55f52017-07-24 16:57:22 +02002651 * Returns:
2652 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2653 * debounce time.
Felipe Balbic4b5be92010-05-26 14:42:23 -07002654 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002655int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07002656{
Felipe Balbic4b5be92010-05-26 14:42:23 -07002657 struct gpio_chip *chip;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002658 unsigned long config;
Felipe Balbic4b5be92010-05-26 14:42:23 -07002659
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002660 VALIDATE_DESC(desc);
2661 chip = desc->gdev->chip;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002662 if (!chip->set || !chip->set_config) {
Mark Brown6424de52013-09-09 10:33:49 +01002663 gpiod_dbg(desc,
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002664 "%s: missing set() or set_config() operations\n",
Mark Brown6424de52013-09-09 10:33:49 +01002665 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02002666 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002667 }
2668
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002669 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
2670 return chip->set_config(chip, gpio_chip_hwgpio(desc), config);
Felipe Balbic4b5be92010-05-26 14:42:23 -07002671}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002672EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002673
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002674/**
Andrew Jefferye10f72b2017-11-30 14:25:24 +10302675 * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
2676 * @desc: descriptor of the GPIO for which to configure persistence
2677 * @transitory: True to lose state on suspend or reset, false for persistence
2678 *
2679 * Returns:
2680 * 0 on success, otherwise a negative error code.
2681 */
2682int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
2683{
2684 struct gpio_chip *chip;
2685 unsigned long packed;
2686 int gpio;
2687 int rc;
2688
Vladimir Zapolskiy156dd392017-12-21 18:37:35 +02002689 VALIDATE_DESC(desc);
Andrew Jefferye10f72b2017-11-30 14:25:24 +10302690 /*
2691 * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
2692 * persistence state.
2693 */
2694 if (transitory)
2695 set_bit(FLAG_TRANSITORY, &desc->flags);
2696 else
2697 clear_bit(FLAG_TRANSITORY, &desc->flags);
2698
2699 /* If the driver supports it, set the persistence state now */
2700 chip = desc->gdev->chip;
2701 if (!chip->set_config)
2702 return 0;
2703
2704 packed = pinconf_to_config_packed(PIN_CONFIG_PERSIST_STATE,
2705 !transitory);
2706 gpio = gpio_chip_hwgpio(desc);
2707 rc = chip->set_config(chip, gpio, packed);
2708 if (rc == -ENOTSUPP) {
2709 dev_dbg(&desc->gdev->dev, "Persistence not supported for GPIO %d\n",
2710 gpio);
2711 return 0;
2712 }
2713
2714 return rc;
2715}
2716EXPORT_SYMBOL_GPL(gpiod_set_transitory);
2717
2718/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002719 * gpiod_is_active_low - test whether a GPIO is active-low or not
2720 * @desc: the gpio descriptor to test
2721 *
2722 * Returns 1 if the GPIO is active-low, 0 otherwise.
2723 */
2724int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002725{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002726 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002727 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002728}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002729EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08002730
2731/* I/O calls are only valid after configuration completed; the relevant
2732 * "is this a valid GPIO" error checks should already have been done.
2733 *
2734 * "Get" operations are often inlinable as reading a pin value register,
2735 * and masking the relevant bit in that register.
2736 *
2737 * When "set" operations are inlinable, they involve writing that mask to
2738 * one register to set a low value, or a different register to set it high.
2739 * Otherwise locking is needed, so there may be little value to inlining.
2740 *
2741 *------------------------------------------------------------------------
2742 *
2743 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2744 * have requested the GPIO. That can include implicit requesting by
2745 * a direction setting call. Marking a gpio as requested locks its chip
2746 * in memory, guaranteeing that these table lookups need no more locking
2747 * and that gpiochip_remove() will fail.
2748 *
2749 * REVISIT when debugging, consider adding some instrumentation to ensure
2750 * that the GPIO was actually requested.
2751 */
2752
Linus Walleijfac9d882017-09-26 20:58:28 +02002753static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002754{
2755 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002756 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002757 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002758
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002759 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002760 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002761 value = chip->get ? chip->get(chip, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01002762 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002763 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002764 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002765}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002766
Lukas Wunnereec1d562017-10-12 12:40:10 +02002767static int gpio_chip_get_multiple(struct gpio_chip *chip,
2768 unsigned long *mask, unsigned long *bits)
2769{
2770 if (chip->get_multiple) {
2771 return chip->get_multiple(chip, mask, bits);
2772 } else if (chip->get) {
2773 int i, value;
2774
2775 for_each_set_bit(i, mask, chip->ngpio) {
2776 value = chip->get(chip, i);
2777 if (value < 0)
2778 return value;
2779 __assign_bit(i, bits, value);
2780 }
2781 return 0;
2782 }
2783 return -EIO;
2784}
2785
2786int gpiod_get_array_value_complex(bool raw, bool can_sleep,
2787 unsigned int array_size,
2788 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02002789 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02002790 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02002791{
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02002792 int err, i = 0;
2793
2794 /*
2795 * Validate array_info against desc_array and its size.
2796 * It should immediately follow desc_array if both
2797 * have been obtained from the same gpiod_get_array() call.
2798 */
2799 if (array_info && array_info->desc == desc_array &&
2800 array_size <= array_info->size &&
2801 (void *)array_info == desc_array + array_info->size) {
2802 if (!can_sleep)
2803 WARN_ON(array_info->chip->can_sleep);
2804
2805 err = gpio_chip_get_multiple(array_info->chip,
2806 array_info->get_mask,
2807 value_bitmap);
2808 if (err)
2809 return err;
2810
2811 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
2812 bitmap_xor(value_bitmap, value_bitmap,
2813 array_info->invert_mask, array_size);
2814
2815 if (bitmap_full(array_info->get_mask, array_size))
2816 return 0;
2817
2818 i = find_first_zero_bit(array_info->get_mask, array_size);
2819 } else {
2820 array_info = NULL;
2821 }
Lukas Wunnereec1d562017-10-12 12:40:10 +02002822
2823 while (i < array_size) {
2824 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Laura Abbott30277432018-05-21 10:57:07 -07002825 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
2826 unsigned long *mask, *bits;
Lukas Wunnereec1d562017-10-12 12:40:10 +02002827 int first, j, ret;
2828
Laura Abbott30277432018-05-21 10:57:07 -07002829 if (likely(chip->ngpio <= FASTPATH_NGPIO)) {
2830 mask = fastpath;
2831 } else {
2832 mask = kmalloc_array(2 * BITS_TO_LONGS(chip->ngpio),
2833 sizeof(*mask),
2834 can_sleep ? GFP_KERNEL : GFP_ATOMIC);
2835 if (!mask)
2836 return -ENOMEM;
2837 }
2838
2839 bits = mask + BITS_TO_LONGS(chip->ngpio);
2840 bitmap_zero(mask, chip->ngpio);
2841
Lukas Wunnereec1d562017-10-12 12:40:10 +02002842 if (!can_sleep)
2843 WARN_ON(chip->can_sleep);
2844
2845 /* collect all inputs belonging to the same chip */
2846 first = i;
Lukas Wunnereec1d562017-10-12 12:40:10 +02002847 do {
2848 const struct gpio_desc *desc = desc_array[i];
2849 int hwgpio = gpio_chip_hwgpio(desc);
2850
2851 __set_bit(hwgpio, mask);
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02002852
2853 if (array_info)
2854 find_next_zero_bit(array_info->get_mask,
2855 array_size, i);
2856 else
2857 i++;
Lukas Wunnereec1d562017-10-12 12:40:10 +02002858 } while ((i < array_size) &&
2859 (desc_array[i]->gdev->chip == chip));
2860
2861 ret = gpio_chip_get_multiple(chip, mask, bits);
Laura Abbott30277432018-05-21 10:57:07 -07002862 if (ret) {
2863 if (mask != fastpath)
2864 kfree(mask);
Lukas Wunnereec1d562017-10-12 12:40:10 +02002865 return ret;
Laura Abbott30277432018-05-21 10:57:07 -07002866 }
Lukas Wunnereec1d562017-10-12 12:40:10 +02002867
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02002868 for (j = first; j < i; ) {
Lukas Wunnereec1d562017-10-12 12:40:10 +02002869 const struct gpio_desc *desc = desc_array[j];
2870 int hwgpio = gpio_chip_hwgpio(desc);
2871 int value = test_bit(hwgpio, bits);
2872
2873 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2874 value = !value;
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02002875 __assign_bit(j, value_bitmap, value);
Lukas Wunnereec1d562017-10-12 12:40:10 +02002876 trace_gpio_value(desc_to_gpio(desc), 1, value);
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02002877
2878 if (array_info)
2879 find_next_zero_bit(array_info->get_mask, i, j);
2880 else
2881 j++;
Lukas Wunnereec1d562017-10-12 12:40:10 +02002882 }
Laura Abbott30277432018-05-21 10:57:07 -07002883
2884 if (mask != fastpath)
2885 kfree(mask);
Lukas Wunnereec1d562017-10-12 12:40:10 +02002886 }
2887 return 0;
2888}
2889
David Brownelld2876d02008-02-04 22:28:20 -08002890/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002891 * gpiod_get_raw_value() - return a gpio's raw value
2892 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08002893 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002894 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002895 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002896 *
2897 * This function should be called from contexts where we cannot sleep, and will
2898 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002899 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002900int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002901{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002902 VALIDATE_DESC(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002903 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002904 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02002905 return gpiod_get_raw_value_commit(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002906}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002907EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002908
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002909/**
2910 * gpiod_get_value() - return a gpio's value
2911 * @desc: gpio whose value will be returned
2912 *
2913 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002914 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002915 *
2916 * This function should be called from contexts where we cannot sleep, and will
2917 * complain if the GPIO chip functions potentially sleep.
2918 */
2919int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002920{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002921 int value;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002922
2923 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002924 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002925 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002926
Linus Walleijfac9d882017-09-26 20:58:28 +02002927 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002928 if (value < 0)
2929 return value;
2930
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002931 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2932 value = !value;
2933
2934 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002935}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002936EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08002937
Lukas Wunnereec1d562017-10-12 12:40:10 +02002938/**
2939 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02002940 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02002941 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02002942 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02002943 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02002944 *
2945 * Read the raw values of the GPIOs, i.e. the values of the physical lines
2946 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
2947 * else an error code.
2948 *
2949 * This function should be called from contexts where we cannot sleep,
2950 * and it will complain if the GPIO chip functions potentially sleep.
2951 */
2952int gpiod_get_raw_array_value(unsigned int array_size,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02002953 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02002954 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02002955 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02002956{
2957 if (!desc_array)
2958 return -EINVAL;
2959 return gpiod_get_array_value_complex(true, false, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02002960 desc_array, array_info,
2961 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02002962}
2963EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
2964
2965/**
2966 * gpiod_get_array_value() - read values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02002967 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02002968 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02002969 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02002970 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02002971 *
2972 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2973 * into account. Return 0 in case of success, else an error code.
2974 *
2975 * This function should be called from contexts where we cannot sleep,
2976 * and it will complain if the GPIO chip functions potentially sleep.
2977 */
2978int gpiod_get_array_value(unsigned int array_size,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02002979 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02002980 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02002981 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02002982{
2983 if (!desc_array)
2984 return -EINVAL;
2985 return gpiod_get_array_value_complex(false, false, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02002986 desc_array, array_info,
2987 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02002988}
2989EXPORT_SYMBOL_GPL(gpiod_get_array_value);
2990
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302991/*
Linus Walleijfac9d882017-09-26 20:58:28 +02002992 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002993 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002994 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302995 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002996static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302997{
2998 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002999 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003000 int offset = gpio_chip_hwgpio(desc);
3001
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303002 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09003003 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303004 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003005 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303006 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09003007 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303008 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003009 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303010 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09003011 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303012 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01003013 gpiod_err(desc,
3014 "%s: Error in set_value for open drain err %d\n",
3015 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05303016}
3017
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303018/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003019 * _gpio_set_open_source_value() - Set the open source gpio's value.
3020 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07003021 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303022 */
Linus Walleijfac9d882017-09-26 20:58:28 +02003023static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303024{
3025 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003026 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003027 int offset = gpio_chip_hwgpio(desc);
3028
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303029 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09003030 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303031 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003032 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303033 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09003034 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303035 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003036 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303037 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09003038 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303039 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01003040 gpiod_err(desc,
3041 "%s: Error in set_value for open source err %d\n",
3042 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303043}
3044
Linus Walleijfac9d882017-09-26 20:58:28 +02003045static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08003046{
3047 struct gpio_chip *chip;
3048
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003049 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003050 trace_gpio_value(desc_to_gpio(desc), 0, value);
Linus Walleij02e47982017-09-26 21:20:23 +02003051 chip->set(chip, gpio_chip_hwgpio(desc), value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003052}
3053
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003054/*
3055 * set multiple outputs on the same chip;
3056 * use the chip's set_multiple function if available;
3057 * otherwise set the outputs sequentially;
3058 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
3059 * defines which outputs are to be changed
3060 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
3061 * defines the values the outputs specified by mask are to be set to
3062 */
3063static void gpio_chip_set_multiple(struct gpio_chip *chip,
3064 unsigned long *mask, unsigned long *bits)
3065{
3066 if (chip->set_multiple) {
3067 chip->set_multiple(chip, mask, bits);
3068 } else {
Andy Shevchenko5e4e6fb2017-01-03 19:01:17 +02003069 unsigned int i;
3070
3071 /* set outputs if the corresponding mask bit is set */
3072 for_each_set_bit(i, mask, chip->ngpio)
3073 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003074 }
3075}
3076
Laura Abbott30277432018-05-21 10:57:07 -07003077int gpiod_set_array_value_complex(bool raw, bool can_sleep,
Linus Walleij44c72882016-04-24 11:36:59 +02003078 unsigned int array_size,
3079 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003080 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003081 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003082{
3083 int i = 0;
3084
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003085 /*
3086 * Validate array_info against desc_array and its size.
3087 * It should immediately follow desc_array if both
3088 * have been obtained from the same gpiod_get_array() call.
3089 */
3090 if (array_info && array_info->desc == desc_array &&
3091 array_size <= array_info->size &&
3092 (void *)array_info == desc_array + array_info->size) {
3093 if (!can_sleep)
3094 WARN_ON(array_info->chip->can_sleep);
3095
3096 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
3097 bitmap_xor(value_bitmap, value_bitmap,
3098 array_info->invert_mask, array_size);
3099
3100 gpio_chip_set_multiple(array_info->chip, array_info->set_mask,
3101 value_bitmap);
3102
3103 if (bitmap_full(array_info->set_mask, array_size))
3104 return 0;
3105
3106 i = find_first_zero_bit(array_info->set_mask, array_size);
3107 } else {
3108 array_info = NULL;
3109 }
3110
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003111 while (i < array_size) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003112 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Laura Abbott30277432018-05-21 10:57:07 -07003113 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
3114 unsigned long *mask, *bits;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003115 int count = 0;
3116
Laura Abbott30277432018-05-21 10:57:07 -07003117 if (likely(chip->ngpio <= FASTPATH_NGPIO)) {
3118 mask = fastpath;
3119 } else {
3120 mask = kmalloc_array(2 * BITS_TO_LONGS(chip->ngpio),
3121 sizeof(*mask),
3122 can_sleep ? GFP_KERNEL : GFP_ATOMIC);
3123 if (!mask)
3124 return -ENOMEM;
3125 }
3126
3127 bits = mask + BITS_TO_LONGS(chip->ngpio);
3128 bitmap_zero(mask, chip->ngpio);
3129
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003130 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003131 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003132
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003133 do {
3134 struct gpio_desc *desc = desc_array[i];
3135 int hwgpio = gpio_chip_hwgpio(desc);
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003136 int value = test_bit(i, value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003137
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003138 /*
3139 * Pins applicable for fast input but not for
3140 * fast output processing may have been already
3141 * inverted inside the fast path, skip them.
3142 */
3143 if (!raw && !(array_info &&
3144 test_bit(i, array_info->invert_mask)) &&
3145 test_bit(FLAG_ACTIVE_LOW, &desc->flags))
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003146 value = !value;
3147 trace_gpio_value(desc_to_gpio(desc), 0, value);
3148 /*
3149 * collect all normal outputs belonging to the same chip
3150 * open drain and open source outputs are set individually
3151 */
Linus Walleij02e47982017-09-26 21:20:23 +02003152 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02003153 gpio_set_open_drain_value_commit(desc, value);
Linus Walleij02e47982017-09-26 21:20:23 +02003154 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02003155 gpio_set_open_source_value_commit(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003156 } else {
3157 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003158 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003159 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003160 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003161 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003162 count++;
3163 }
Janusz Krzysztofikb17566a2018-09-05 23:50:08 +02003164
3165 if (array_info)
3166 find_next_zero_bit(array_info->set_mask,
3167 array_size, i);
3168 else
3169 i++;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003170 } while ((i < array_size) &&
3171 (desc_array[i]->gdev->chip == chip));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003172 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003173 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003174 gpio_chip_set_multiple(chip, mask, bits);
Laura Abbott30277432018-05-21 10:57:07 -07003175
3176 if (mask != fastpath)
3177 kfree(mask);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003178 }
Laura Abbott30277432018-05-21 10:57:07 -07003179 return 0;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003180}
3181
David Brownelld2876d02008-02-04 22:28:20 -08003182/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003183 * gpiod_set_raw_value() - assign a gpio's raw value
3184 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08003185 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08003186 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003187 * Set the raw value of the GPIO, i.e. the value of its physical line without
3188 * regard for its ACTIVE_LOW status.
3189 *
3190 * This function should be called from contexts where we cannot sleep, and will
3191 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003192 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003193void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003194{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003195 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01003196 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003197 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02003198 gpiod_set_raw_value_commit(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08003199}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003200EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08003201
3202/**
Geert Uytterhoeven1e77fc82018-01-09 19:08:21 +01003203 * gpiod_set_value_nocheck() - set a GPIO line value without checking
3204 * @desc: the descriptor to set the value on
3205 * @value: value to set
3206 *
3207 * This sets the value of a GPIO line backing a descriptor, applying
3208 * different semantic quirks like active low and open drain/source
3209 * handling.
3210 */
3211static void gpiod_set_value_nocheck(struct gpio_desc *desc, int value)
3212{
3213 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3214 value = !value;
3215 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
3216 gpio_set_open_drain_value_commit(desc, value);
3217 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
3218 gpio_set_open_source_value_commit(desc, value);
3219 else
3220 gpiod_set_raw_value_commit(desc, value);
3221}
3222
3223/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003224 * gpiod_set_value() - assign a gpio's value
3225 * @desc: gpio whose value will be assigned
3226 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08003227 *
Linus Walleij02e47982017-09-26 21:20:23 +02003228 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
3229 * OPEN_DRAIN and OPEN_SOURCE flags into account.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003230 *
3231 * This function should be called from contexts where we cannot sleep, and will
3232 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003233 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003234void gpiod_set_value(struct gpio_desc *desc, int value)
3235{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003236 VALIDATE_DESC_VOID(desc);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003237 WARN_ON(desc->gdev->chip->can_sleep);
Geert Uytterhoeven1e77fc82018-01-09 19:08:21 +01003238 gpiod_set_value_nocheck(desc, value);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003239}
3240EXPORT_SYMBOL_GPL(gpiod_set_value);
3241
3242/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003243 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003244 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003245 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003246 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003247 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003248 *
3249 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3250 * without regard for their ACTIVE_LOW status.
3251 *
3252 * This function should be called from contexts where we cannot sleep, and will
3253 * complain if the GPIO chip functions potentially sleep.
3254 */
Laura Abbott30277432018-05-21 10:57:07 -07003255int gpiod_set_raw_array_value(unsigned int array_size,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003256 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003257 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003258 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003259{
3260 if (!desc_array)
Laura Abbott30277432018-05-21 10:57:07 -07003261 return -EINVAL;
3262 return gpiod_set_array_value_complex(true, false, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003263 desc_array, array_info, value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003264}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003265EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003266
3267/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003268 * gpiod_set_array_value() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003269 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003270 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003271 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003272 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003273 *
3274 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3275 * into account.
3276 *
3277 * This function should be called from contexts where we cannot sleep, and will
3278 * complain if the GPIO chip functions potentially sleep.
3279 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003280void gpiod_set_array_value(unsigned int array_size,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003281 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003282 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003283 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003284{
3285 if (!desc_array)
3286 return;
Linus Walleij44c72882016-04-24 11:36:59 +02003287 gpiod_set_array_value_complex(false, false, array_size, desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003288 array_info, value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003289}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003290EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003291
3292/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003293 * gpiod_cansleep() - report whether gpio value access may sleep
3294 * @desc: gpio to check
3295 *
3296 */
3297int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003298{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003299 VALIDATE_DESC(desc);
3300 return desc->gdev->chip->can_sleep;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003301}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003302EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08003303
David Brownell0f6d5042008-10-15 22:03:14 -07003304/**
Linus Walleij90b39402e2018-06-01 13:21:27 +02003305 * gpiod_set_consumer_name() - set the consumer name for the descriptor
3306 * @desc: gpio to set the consumer name on
3307 * @name: the new consumer name
3308 */
3309void gpiod_set_consumer_name(struct gpio_desc *desc, const char *name)
3310{
3311 VALIDATE_DESC_VOID(desc);
3312 /* Just overwrite whatever the previous name was */
3313 desc->label = name;
3314}
3315EXPORT_SYMBOL_GPL(gpiod_set_consumer_name);
3316
3317/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003318 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
3319 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07003320 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003321 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
3322 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07003323 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003324int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07003325{
Linus Walleij4c37ce82016-05-02 13:13:10 +02003326 struct gpio_chip *chip;
3327 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07003328
Linus Walleij79bb71b2016-06-15 22:57:38 +02003329 /*
3330 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
3331 * requires this function to not return zero on an invalid descriptor
3332 * but rather a negative error number.
3333 */
Linus Walleijbfbbe442016-06-16 11:55:55 +02003334 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
Linus Walleij79bb71b2016-06-15 22:57:38 +02003335 return -EINVAL;
3336
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003337 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003338 offset = gpio_chip_hwgpio(desc);
Linus Walleij4c37ce82016-05-02 13:13:10 +02003339 if (chip->to_irq) {
3340 int retirq = chip->to_irq(chip, offset);
3341
3342 /* Zero means NO_IRQ */
3343 if (!retirq)
3344 return -ENXIO;
3345
3346 return retirq;
3347 }
3348 return -ENXIO;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003349}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003350EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003351
Linus Walleijd468bf92013-09-24 11:54:38 +02003352/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003353 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003354 * @chip: the chip the GPIO to lock belongs to
3355 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02003356 *
3357 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08003358 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08003359 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003360int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08003361{
Linus Walleij9c102802016-05-25 10:56:03 +02003362 struct gpio_desc *desc;
Linus Walleijd468bf92013-09-24 11:54:38 +02003363
Linus Walleij9c102802016-05-25 10:56:03 +02003364 desc = gpiochip_get_desc(chip, offset);
3365 if (IS_ERR(desc))
3366 return PTR_ERR(desc);
3367
Linus Walleij60f83392016-11-12 15:01:09 +01003368 /*
3369 * If it's fast: flush the direction setting if something changed
3370 * behind our back
3371 */
3372 if (!chip->can_sleep && chip->get_direction) {
Andy Shevchenko80956792018-07-09 21:47:21 +03003373 int dir = gpiod_get_direction(desc);
Linus Walleij9c102802016-05-25 10:56:03 +02003374
Andy Shevchenko36b31272018-07-03 03:38:31 +03003375 if (dir < 0) {
3376 chip_err(chip, "%s: cannot get GPIO direction\n",
3377 __func__);
3378 return dir;
3379 }
Linus Walleij9c102802016-05-25 10:56:03 +02003380 }
3381
3382 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003383 chip_err(chip,
Andy Shevchenkob1911712018-07-03 03:39:03 +03003384 "%s: tried to flag a GPIO set as output for IRQ\n",
3385 __func__);
Linus Walleijd468bf92013-09-24 11:54:38 +02003386 return -EIO;
3387 }
3388
Linus Walleij9c102802016-05-25 10:56:03 +02003389 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
Linus Walleij3940c342016-11-14 00:09:07 +01003390
3391 /*
3392 * If the consumer has not set up a label (such as when the
3393 * IRQ is referenced from .to_irq()) we set up a label here
3394 * so it is clear this is used as an interrupt.
3395 */
3396 if (!desc->label)
3397 desc_set_label(desc, "interrupt");
3398
Linus Walleijd468bf92013-09-24 11:54:38 +02003399 return 0;
3400}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003401EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02003402
Linus Walleijd468bf92013-09-24 11:54:38 +02003403/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003404 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003405 * @chip: the chip the GPIO to lock belongs to
3406 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02003407 *
3408 * This is used directly by GPIO drivers that want to indicate
3409 * that a certain GPIO is no longer used exclusively for IRQ.
3410 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003411void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02003412{
Linus Walleij3940c342016-11-14 00:09:07 +01003413 struct gpio_desc *desc;
3414
3415 desc = gpiochip_get_desc(chip, offset);
3416 if (IS_ERR(desc))
Linus Walleijd468bf92013-09-24 11:54:38 +02003417 return;
3418
Linus Walleij3940c342016-11-14 00:09:07 +01003419 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
3420
3421 /* If we only had this marking, erase it */
3422 if (desc->label && !strcmp(desc->label, "interrupt"))
3423 desc_set_label(desc, NULL);
Linus Walleijd468bf92013-09-24 11:54:38 +02003424}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003425EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02003426
Linus Walleij6cee3822016-02-11 20:16:45 +01003427bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
3428{
3429 if (offset >= chip->ngpio)
3430 return false;
3431
3432 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
3433}
3434EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
3435
Linus Walleij143b65d2016-02-16 15:41:42 +01003436bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
3437{
3438 if (offset >= chip->ngpio)
3439 return false;
3440
3441 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
3442}
3443EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
3444
3445bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
3446{
3447 if (offset >= chip->ngpio)
3448 return false;
3449
3450 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
3451}
3452EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
3453
Charles Keepax05f479b2017-05-23 15:47:29 +01003454bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset)
3455{
3456 if (offset >= chip->ngpio)
3457 return false;
3458
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303459 return !test_bit(FLAG_TRANSITORY, &chip->gpiodev->descs[offset].flags);
Charles Keepax05f479b2017-05-23 15:47:29 +01003460}
3461EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
3462
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003463/**
3464 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
3465 * @desc: gpio whose value will be returned
3466 *
3467 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003468 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003469 *
3470 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003471 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003472int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08003473{
David Brownelld2876d02008-02-04 22:28:20 -08003474 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003475 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003476 return gpiod_get_raw_value_commit(desc);
David Brownelld2876d02008-02-04 22:28:20 -08003477}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003478EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003479
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003480/**
3481 * gpiod_get_value_cansleep() - return a gpio's value
3482 * @desc: gpio whose value will be returned
3483 *
3484 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003485 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003486 *
3487 * This function is to be called from contexts that can sleep.
3488 */
3489int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003490{
David Brownelld2876d02008-02-04 22:28:20 -08003491 int value;
David Brownelld2876d02008-02-04 22:28:20 -08003492
3493 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003494 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003495 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003496 if (value < 0)
3497 return value;
3498
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003499 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3500 value = !value;
3501
David Brownelld2876d02008-02-04 22:28:20 -08003502 return value;
3503}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003504EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003505
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003506/**
Lukas Wunnereec1d562017-10-12 12:40:10 +02003507 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003508 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02003509 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003510 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003511 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02003512 *
3513 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3514 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3515 * else an error code.
3516 *
3517 * This function is to be called from contexts that can sleep.
3518 */
3519int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
3520 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003521 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003522 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02003523{
3524 might_sleep_if(extra_checks);
3525 if (!desc_array)
3526 return -EINVAL;
3527 return gpiod_get_array_value_complex(true, true, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003528 desc_array, array_info,
3529 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003530}
3531EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
3532
3533/**
3534 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003535 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02003536 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003537 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003538 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02003539 *
3540 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3541 * into account. Return 0 in case of success, else an error code.
3542 *
3543 * This function is to be called from contexts that can sleep.
3544 */
3545int gpiod_get_array_value_cansleep(unsigned int array_size,
3546 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003547 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003548 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02003549{
3550 might_sleep_if(extra_checks);
3551 if (!desc_array)
3552 return -EINVAL;
3553 return gpiod_get_array_value_complex(false, true, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003554 desc_array, array_info,
3555 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003556}
3557EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
3558
3559/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003560 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
3561 * @desc: gpio whose value will be assigned
3562 * @value: value to assign
3563 *
3564 * Set the raw value of the GPIO, i.e. the value of its physical line without
3565 * regard for its ACTIVE_LOW status.
3566 *
3567 * This function is to be called from contexts that can sleep.
3568 */
3569void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003570{
David Brownelld2876d02008-02-04 22:28:20 -08003571 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003572 VALIDATE_DESC_VOID(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003573 gpiod_set_raw_value_commit(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003574}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003575EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003576
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003577/**
3578 * gpiod_set_value_cansleep() - assign a gpio's value
3579 * @desc: gpio whose value will be assigned
3580 * @value: value to assign
3581 *
3582 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3583 * account
3584 *
3585 * This function is to be called from contexts that can sleep.
3586 */
3587void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003588{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003589 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003590 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1e77fc82018-01-09 19:08:21 +01003591 gpiod_set_value_nocheck(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08003592}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003593EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08003594
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003595/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003596 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003597 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003598 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003599 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003600 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003601 *
3602 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3603 * without regard for their ACTIVE_LOW status.
3604 *
3605 * This function is to be called from contexts that can sleep.
3606 */
Laura Abbott30277432018-05-21 10:57:07 -07003607int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +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)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003611{
3612 might_sleep_if(extra_checks);
3613 if (!desc_array)
Laura Abbott30277432018-05-21 10:57:07 -07003614 return -EINVAL;
3615 return gpiod_set_array_value_complex(true, true, array_size, desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003616 array_info, value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003617}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003618EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003619
3620/**
Dmitry Torokhov3946d182017-08-14 21:59:55 -07003621 * gpiod_add_lookup_tables() - register GPIO device consumers
3622 * @tables: list of tables of consumers to register
3623 * @n: number of tables in the list
3624 */
3625void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
3626{
3627 unsigned int i;
3628
3629 mutex_lock(&gpio_lookup_lock);
3630
3631 for (i = 0; i < n; i++)
3632 list_add_tail(&tables[i]->list, &gpio_lookup_list);
3633
3634 mutex_unlock(&gpio_lookup_lock);
3635}
3636
3637/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003638 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003639 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003640 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003641 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003642 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003643 *
3644 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3645 * into account.
3646 *
3647 * This function is to be called from contexts that can sleep.
3648 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003649void gpiod_set_array_value_cansleep(unsigned int array_size,
3650 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003651 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003652 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003653{
3654 might_sleep_if(extra_checks);
3655 if (!desc_array)
3656 return;
Linus Walleij44c72882016-04-24 11:36:59 +02003657 gpiod_set_array_value_complex(false, true, array_size, desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003658 array_info, value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003659}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003660EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003661
3662/**
Alexandre Courbotad824782013-12-03 12:20:11 +09003663 * gpiod_add_lookup_table() - register GPIO device consumers
3664 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003665 */
Alexandre Courbotad824782013-12-03 12:20:11 +09003666void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003667{
3668 mutex_lock(&gpio_lookup_lock);
3669
Alexandre Courbotad824782013-12-03 12:20:11 +09003670 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003671
3672 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08003673}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02003674EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
David Brownelld2876d02008-02-04 22:28:20 -08003675
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05303676/**
3677 * gpiod_remove_lookup_table() - unregister GPIO device consumers
3678 * @table: table of consumers to unregister
3679 */
3680void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
3681{
3682 mutex_lock(&gpio_lookup_lock);
3683
3684 list_del(&table->list);
3685
3686 mutex_unlock(&gpio_lookup_lock);
3687}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02003688EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05303689
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02003690/**
3691 * gpiod_add_hogs() - register a set of GPIO hogs from machine code
3692 * @hogs: table of gpio hog entries with a zeroed sentinel at the end
3693 */
3694void gpiod_add_hogs(struct gpiod_hog *hogs)
3695{
3696 struct gpio_chip *chip;
3697 struct gpiod_hog *hog;
3698
3699 mutex_lock(&gpio_machine_hogs_mutex);
3700
3701 for (hog = &hogs[0]; hog->chip_label; hog++) {
3702 list_add_tail(&hog->list, &gpio_machine_hogs);
3703
3704 /*
3705 * The chip may have been registered earlier, so check if it
3706 * exists and, if so, try to hog the line now.
3707 */
3708 chip = find_chip_by_name(hog->chip_label);
3709 if (chip)
3710 gpiochip_machine_hog(chip, hog);
3711 }
3712
3713 mutex_unlock(&gpio_machine_hogs_mutex);
3714}
3715EXPORT_SYMBOL_GPL(gpiod_add_hogs);
3716
Alexandre Courbotad824782013-12-03 12:20:11 +09003717static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
3718{
3719 const char *dev_id = dev ? dev_name(dev) : NULL;
3720 struct gpiod_lookup_table *table;
3721
3722 mutex_lock(&gpio_lookup_lock);
3723
3724 list_for_each_entry(table, &gpio_lookup_list, list) {
3725 if (table->dev_id && dev_id) {
3726 /*
3727 * Valid strings on both ends, must be identical to have
3728 * a match
3729 */
3730 if (!strcmp(table->dev_id, dev_id))
3731 goto found;
3732 } else {
3733 /*
3734 * One of the pointers is NULL, so both must be to have
3735 * a match
3736 */
3737 if (dev_id == table->dev_id)
3738 goto found;
3739 }
3740 }
3741 table = NULL;
3742
3743found:
3744 mutex_unlock(&gpio_lookup_lock);
3745 return table;
3746}
3747
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003748static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09003749 unsigned int idx,
3750 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003751{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003752 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09003753 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003754 struct gpiod_lookup *p;
3755
Alexandre Courbotad824782013-12-03 12:20:11 +09003756 table = gpiod_find_lookup_table(dev);
3757 if (!table)
3758 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003759
Alexandre Courbotad824782013-12-03 12:20:11 +09003760 for (p = &table->table[0]; p->chip_label; p++) {
3761 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003762
Alexandre Courbotad824782013-12-03 12:20:11 +09003763 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003764 if (p->idx != idx)
3765 continue;
3766
Alexandre Courbotad824782013-12-03 12:20:11 +09003767 /* If the lookup entry has a con_id, require exact match */
3768 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
3769 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003770
Alexandre Courbotad824782013-12-03 12:20:11 +09003771 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003772
Alexandre Courbotad824782013-12-03 12:20:11 +09003773 if (!chip) {
Janusz Krzysztofik8853daf2018-07-04 00:18:19 +02003774 /*
3775 * As the lookup table indicates a chip with
3776 * p->chip_label should exist, assume it may
3777 * still appear later and let the interested
3778 * consumer be probed again or let the Deferred
3779 * Probe infrastructure handle the error.
3780 */
3781 dev_warn(dev, "cannot find GPIO chip %s, deferring\n",
3782 p->chip_label);
3783 return ERR_PTR(-EPROBE_DEFER);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003784 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003785
Alexandre Courbotad824782013-12-03 12:20:11 +09003786 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003787 dev_err(dev,
3788 "requested GPIO %d is out of range [0..%d] for chip %s\n",
3789 idx, chip->ngpio, chip->label);
3790 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09003791 }
3792
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09003793 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09003794 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003795
3796 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09003797 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003798
3799 return desc;
3800}
3801
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003802static int dt_gpio_count(struct device *dev, const char *con_id)
3803{
3804 int ret;
3805 char propname[32];
3806 unsigned int i;
3807
3808 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
3809 if (con_id)
3810 snprintf(propname, sizeof(propname), "%s-%s",
3811 con_id, gpio_suffixes[i]);
3812 else
3813 snprintf(propname, sizeof(propname), "%s",
3814 gpio_suffixes[i]);
3815
3816 ret = of_gpio_named_count(dev->of_node, propname);
Andy Shevchenko4033d4a2017-02-20 18:15:47 +02003817 if (ret > 0)
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003818 break;
3819 }
Andy Shevchenko4033d4a2017-02-20 18:15:47 +02003820 return ret ? ret : -ENOENT;
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003821}
3822
3823static int platform_gpio_count(struct device *dev, const char *con_id)
3824{
3825 struct gpiod_lookup_table *table;
3826 struct gpiod_lookup *p;
3827 unsigned int count = 0;
3828
3829 table = gpiod_find_lookup_table(dev);
3830 if (!table)
3831 return -ENOENT;
3832
3833 for (p = &table->table[0]; p->chip_label; p++) {
3834 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3835 (!con_id && !p->con_id))
3836 count++;
3837 }
3838 if (!count)
3839 return -ENOENT;
3840
3841 return count;
3842}
3843
3844/**
3845 * gpiod_count - return the number of GPIOs associated with a device / function
3846 * or -ENOENT if no GPIO has been assigned to the requested function
3847 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3848 * @con_id: function within the GPIO consumer
3849 */
3850int gpiod_count(struct device *dev, const char *con_id)
3851{
3852 int count = -ENOENT;
3853
3854 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
3855 count = dt_gpio_count(dev, con_id);
3856 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
3857 count = acpi_gpio_count(dev, con_id);
3858
3859 if (count < 0)
3860 count = platform_gpio_count(dev, con_id);
3861
3862 return count;
3863}
3864EXPORT_SYMBOL_GPL(gpiod_count);
3865
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003866/**
Thierry Reding08791622014-04-25 16:54:22 +02003867 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09003868 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003869 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003870 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003871 *
3872 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003873 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07003874 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003875 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003876struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003877 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003878{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003879 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003880}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003881EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003882
3883/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02003884 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
3885 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3886 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003887 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02003888 *
3889 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
3890 * the requested function it will return NULL. This is convenient for drivers
3891 * that need to handle optional GPIOs.
3892 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003893struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003894 const char *con_id,
3895 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003896{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003897 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003898}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003899EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003900
Benoit Parrotf625d462015-02-02 11:44:44 -06003901
3902/**
3903 * gpiod_configure_flags - helper function to configure a given GPIO
3904 * @desc: gpio whose value will be assigned
3905 * @con_id: function within the GPIO consumer
Johan Hovold85b03b32016-07-03 18:32:05 +02003906 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3907 * of_get_gpio_hog()
Benoit Parrotf625d462015-02-02 11:44:44 -06003908 * @dflags: gpiod_flags - optional GPIO initialization flags
3909 *
3910 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
3911 * requested function and/or index, or another IS_ERR() code if an error
3912 * occurred while trying to acquire the GPIO.
3913 */
Andy Shevchenkoc29fd9e2017-05-23 20:03:16 +03003914int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Johan Hovold85b03b32016-07-03 18:32:05 +02003915 unsigned long lflags, enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06003916{
3917 int status;
3918
Johan Hovold85b03b32016-07-03 18:32:05 +02003919 if (lflags & GPIO_ACTIVE_LOW)
3920 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
Linus Walleijf926dfc2017-09-10 19:26:22 +02003921
Johan Hovold85b03b32016-07-03 18:32:05 +02003922 if (lflags & GPIO_OPEN_DRAIN)
3923 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
Linus Walleijf926dfc2017-09-10 19:26:22 +02003924 else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) {
3925 /*
3926 * This enforces open drain mode from the consumer side.
3927 * This is necessary for some busses like I2C, but the lookup
3928 * should *REALLY* have specified them as open drain in the
3929 * first place, so print a little warning here.
3930 */
3931 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3932 gpiod_warn(desc,
3933 "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
3934 }
3935
Johan Hovold85b03b32016-07-03 18:32:05 +02003936 if (lflags & GPIO_OPEN_SOURCE)
3937 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303938
3939 status = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
3940 if (status < 0)
3941 return status;
Johan Hovold85b03b32016-07-03 18:32:05 +02003942
Benoit Parrotf625d462015-02-02 11:44:44 -06003943 /* No particular flag request, return here... */
3944 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
3945 pr_debug("no flags found for %s\n", con_id);
3946 return 0;
3947 }
3948
3949 /* Process flags */
3950 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
3951 status = gpiod_direction_output(desc,
Linus Walleijad177312016-11-13 23:02:44 +01003952 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
Benoit Parrotf625d462015-02-02 11:44:44 -06003953 else
3954 status = gpiod_direction_input(desc);
3955
3956 return status;
3957}
3958
Thierry Reding29a1f2332014-04-25 17:10:06 +02003959/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003960 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02003961 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003962 * @con_id: function within the GPIO consumer
3963 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003964 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003965 *
3966 * This variant of gpiod_get() allows to access GPIOs other than the first
3967 * defined one for functions that define several GPIOs.
3968 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003969 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
3970 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07003971 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003972 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003973struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003974 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003975 unsigned int idx,
3976 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003977{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003978 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003979 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003980 enum gpio_lookup_flags lookupflags = 0;
Linus Walleij7d18f0a2018-01-16 08:29:50 +01003981 /* Maybe we have a device name, maybe not */
3982 const char *devname = dev ? dev_name(dev) : "?";
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003983
3984 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
3985
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003986 if (dev) {
3987 /* Using device tree? */
3988 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
3989 dev_dbg(dev, "using device tree for GPIO lookup\n");
3990 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
3991 } else if (ACPI_COMPANION(dev)) {
3992 dev_dbg(dev, "using ACPI for GPIO lookup\n");
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +03003993 desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags);
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003994 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003995 }
3996
3997 /*
3998 * Either we are not using DT or ACPI, or their lookup did not return
3999 * a result. In that case, use platform lookup as a fallback.
4000 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09004001 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04004002 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004003 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004004 }
4005
4006 if (IS_ERR(desc)) {
Wang Dongsheng9d5a1f22018-02-27 00:12:13 -08004007 dev_dbg(dev, "No GPIO consumer %s found\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004008 return desc;
4009 }
4010
Linus Walleij7d18f0a2018-01-16 08:29:50 +01004011 /*
4012 * If a connection label was passed use that, else attempt to use
4013 * the device name as label
4014 */
4015 status = gpiod_request(desc, con_id ? con_id : devname);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004016 if (status < 0)
4017 return ERR_PTR(status);
4018
Johan Hovold85b03b32016-07-03 18:32:05 +02004019 status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004020 if (status < 0) {
4021 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
4022 gpiod_put(desc);
4023 return ERR_PTR(status);
4024 }
4025
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004026 return desc;
4027}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004028EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004029
4030/**
Linus Walleij6392cca2017-12-29 02:07:54 +01004031 * gpiod_get_from_of_node() - obtain a GPIO from an OF node
4032 * @node: handle of the OF node
4033 * @propname: name of the DT property representing the GPIO
4034 * @index: index of the GPIO to obtain for the consumer
Andy Shevchenkoa264d102017-01-09 16:02:28 +02004035 * @dflags: GPIO initialization flags
Thierry Reding950d55f52017-07-24 16:57:22 +02004036 * @label: label to attach to the requested GPIO
Mika Westerberg40b73182014-10-21 13:33:59 +02004037 *
Thierry Reding950d55f52017-07-24 16:57:22 +02004038 * Returns:
Andy Shevchenkoff213782017-02-28 17:03:12 +02004039 * On successful request the GPIO pin is configured in accordance with
Linus Walleij6392cca2017-12-29 02:07:54 +01004040 * provided @dflags. If the node does not have the requested GPIO
4041 * property, NULL is returned.
Andy Shevchenkoa264d102017-01-09 16:02:28 +02004042 *
Mika Westerberg40b73182014-10-21 13:33:59 +02004043 * In case of error an ERR_PTR() is returned.
4044 */
Linus Walleij92542ed2017-12-29 22:52:02 +01004045struct gpio_desc *gpiod_get_from_of_node(struct device_node *node,
4046 const char *propname, int index,
4047 enum gpiod_flags dflags,
4048 const char *label)
Mika Westerberg40b73182014-10-21 13:33:59 +02004049{
Colin Ian King40a3c9d2018-01-16 11:56:11 +00004050 struct gpio_desc *desc;
Andy Shevchenkoa264d102017-01-09 16:02:28 +02004051 unsigned long lflags = 0;
Linus Walleij6392cca2017-12-29 02:07:54 +01004052 enum of_gpio_flags flags;
Mika Westerberg40b73182014-10-21 13:33:59 +02004053 bool active_low = false;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03004054 bool single_ended = false;
Laxman Dewangan4c0facd2017-04-06 19:05:52 +05304055 bool open_drain = false;
Andrew Jefferye10f72b2017-11-30 14:25:24 +10304056 bool transitory = false;
Mika Westerberg40b73182014-10-21 13:33:59 +02004057 int ret;
4058
Linus Walleij6392cca2017-12-29 02:07:54 +01004059 desc = of_get_named_gpiod_flags(node, propname,
4060 index, &flags);
Mika Westerberg40b73182014-10-21 13:33:59 +02004061
Linus Walleij6392cca2017-12-29 02:07:54 +01004062 if (!desc || IS_ERR(desc)) {
4063 /* If it is not there, just return NULL */
4064 if (PTR_ERR(desc) == -ENOENT)
4065 return NULL;
4066 return desc;
Mika Westerberg40b73182014-10-21 13:33:59 +02004067 }
4068
Linus Walleij6392cca2017-12-29 02:07:54 +01004069 active_low = flags & OF_GPIO_ACTIVE_LOW;
4070 single_ended = flags & OF_GPIO_SINGLE_ENDED;
4071 open_drain = flags & OF_GPIO_OPEN_DRAIN;
4072 transitory = flags & OF_GPIO_TRANSITORY;
Mika Westerberg40b73182014-10-21 13:33:59 +02004073
Alexander Steinb2987d72017-01-12 17:39:24 +01004074 ret = gpiod_request(desc, label);
Johan Hovold85b03b32016-07-03 18:32:05 +02004075 if (ret)
4076 return ERR_PTR(ret);
4077
Mika Westerberg40b73182014-10-21 13:33:59 +02004078 if (active_low)
Andy Shevchenkoa264d102017-01-09 16:02:28 +02004079 lflags |= GPIO_ACTIVE_LOW;
Mika Westerberg40b73182014-10-21 13:33:59 +02004080
Laurent Pinchart90b665f2015-10-13 00:20:21 +03004081 if (single_ended) {
Laxman Dewangan4c0facd2017-04-06 19:05:52 +05304082 if (open_drain)
Andy Shevchenkoa264d102017-01-09 16:02:28 +02004083 lflags |= GPIO_OPEN_DRAIN;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03004084 else
Andy Shevchenkoa264d102017-01-09 16:02:28 +02004085 lflags |= GPIO_OPEN_SOURCE;
4086 }
4087
Andrew Jefferye10f72b2017-11-30 14:25:24 +10304088 if (transitory)
4089 lflags |= GPIO_TRANSITORY;
4090
Andy Shevchenkoa264d102017-01-09 16:02:28 +02004091 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
4092 if (ret < 0) {
4093 gpiod_put(desc);
4094 return ERR_PTR(ret);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03004095 }
4096
Mika Westerberg40b73182014-10-21 13:33:59 +02004097 return desc;
4098}
Linus Walleij92542ed2017-12-29 22:52:02 +01004099EXPORT_SYMBOL(gpiod_get_from_of_node);
Linus Walleij6392cca2017-12-29 02:07:54 +01004100
4101/**
Mika Westerberg40b73182014-10-21 13:33:59 +02004102 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
4103 * @fwnode: handle of the firmware node
4104 * @propname: name of the firmware property representing the GPIO
Linus Walleij6392cca2017-12-29 02:07:54 +01004105 * @index: index of the GPIO to obtain for the consumer
Mika Westerberg40b73182014-10-21 13:33:59 +02004106 * @dflags: GPIO initialization flags
4107 * @label: label to attach to the requested GPIO
4108 *
4109 * This function can be used for drivers that get their configuration
Linus Walleij6392cca2017-12-29 02:07:54 +01004110 * from opaque firmware.
Thierry Reding29a1f2332014-04-25 17:10:06 +02004111 *
Linus Walleij6392cca2017-12-29 02:07:54 +01004112 * The function properly finds the corresponding GPIO using whatever is the
Thierry Reding29a1f2332014-04-25 17:10:06 +02004113 * underlying firmware interface and then makes sure that the GPIO
4114 * descriptor is requested before it is returned to the caller.
4115 *
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004116 * Returns:
Thierry Reding29a1f2332014-04-25 17:10:06 +02004117 * On successful request the GPIO pin is configured in accordance with
4118 * provided @dflags.
4119 *
4120 * In case of error an ERR_PTR() is returned.
4121 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004122struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
Thierry Reding29a1f2332014-04-25 17:10:06 +02004123 const char *propname, int index,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004124 enum gpiod_flags dflags,
4125 const char *label)
Thierry Reding29a1f2332014-04-25 17:10:06 +02004126{
4127 struct gpio_desc *desc = ERR_PTR(-ENODEV);
4128 unsigned long lflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004129 int ret;
4130
David Brownelld2876d02008-02-04 22:28:20 -08004131 if (!fwnode)
David Brownelld2876d02008-02-04 22:28:20 -08004132 return ERR_PTR(-EINVAL);
4133
4134 if (is_of_node(fwnode)) {
Linus Walleij6392cca2017-12-29 02:07:54 +01004135 desc = gpiod_get_from_of_node(to_of_node(fwnode),
4136 propname, index,
4137 dflags,
4138 label);
4139 return desc;
David Brownelld2876d02008-02-04 22:28:20 -08004140 } else if (is_acpi_node(fwnode)) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09004141 struct acpi_gpio_info info;
David Brownelld2876d02008-02-04 22:28:20 -08004142
Linus Walleijd468bf92013-09-24 11:54:38 +02004143 desc = acpi_node_get_gpiod(fwnode, propname, index, &info);
Linus Walleij6392cca2017-12-29 02:07:54 +01004144 if (IS_ERR(desc))
4145 return desc;
4146
4147 acpi_gpio_update_gpiod_flags(&dflags, &info);
4148
4149 if (info.polarity == GPIO_ACTIVE_LOW)
4150 lflags |= GPIO_ACTIVE_LOW;
Thierry Redingf9c4a312012-04-12 13:26:01 +02004151 }
4152
Linus Walleij6392cca2017-12-29 02:07:54 +01004153 /* Currently only ACPI takes this path */
Thierry Redingf9c4a312012-04-12 13:26:01 +02004154 ret = gpiod_request(desc, label);
4155 if (ret)
4156 return ERR_PTR(ret);
Linus Walleijd468bf92013-09-24 11:54:38 +02004157
Thierry Redingf9c4a312012-04-12 13:26:01 +02004158 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
4159 if (ret < 0) {
4160 gpiod_put(desc);
4161 return ERR_PTR(ret);
4162 }
4163
4164 return desc;
Linus Walleijd468bf92013-09-24 11:54:38 +02004165}
4166EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
David Brownelld2876d02008-02-04 22:28:20 -08004167
4168/**
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07004169 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
David Brownelld8f388d82008-07-25 01:46:07 -07004170 * function
Linus Walleijd468bf92013-09-24 11:54:38 +02004171 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4172 * @con_id: function within the GPIO consumer
David Brownelld2876d02008-02-04 22:28:20 -08004173 * @index: index of the GPIO to obtain in the consumer
4174 * @flags: optional GPIO initialization flags
4175 *
4176 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
4177 * specified index was assigned to the requested function it will return NULL.
4178 * This is convenient for drivers that need to handle optional GPIOs.
Grant Likely362432a2013-02-09 09:41:49 +00004179 */
David Brownelld8f388d82008-07-25 01:46:07 -07004180struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09004181 const char *con_id,
Thierry Redingf9c4a312012-04-12 13:26:01 +02004182 unsigned int index,
4183 enum gpiod_flags flags)
4184{
Grant Likely362432a2013-02-09 09:41:49 +00004185 struct gpio_desc *desc;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09004186
Grant Likely362432a2013-02-09 09:41:49 +00004187 desc = gpiod_get_index(dev, con_id, index, flags);
4188 if (IS_ERR(desc)) {
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09004189 if (PTR_ERR(desc) == -ENOENT)
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004190 return NULL;
David Brownelld2876d02008-02-04 22:28:20 -08004191 }
4192
Benoit Parrotf625d462015-02-02 11:44:44 -06004193 return desc;
4194}
4195EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
4196
4197/**
4198 * gpiod_hog - Hog the specified GPIO desc given the provided flags
4199 * @desc: gpio whose value will be assigned
4200 * @name: gpio line name
4201 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
4202 * of_get_gpio_hog()
4203 * @dflags: gpiod_flags - optional GPIO initialization flags
4204 */
4205int gpiod_hog(struct gpio_desc *desc, const char *name,
4206 unsigned long lflags, enum gpiod_flags dflags)
4207{
4208 struct gpio_chip *chip;
4209 struct gpio_desc *local_desc;
4210 int hwnum;
4211 int status;
4212
Laxman Dewanganc31a5712016-03-11 19:13:21 +05304213 chip = gpiod_to_chip(desc);
4214 hwnum = gpio_chip_hwgpio(desc);
4215
4216 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
Benoit Parrotf625d462015-02-02 11:44:44 -06004217 if (IS_ERR(local_desc)) {
4218 status = PTR_ERR(local_desc);
Johan Hovold85b03b32016-07-03 18:32:05 +02004219 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
Benoit Parrotf625d462015-02-02 11:44:44 -06004220 name, chip->label, hwnum, status);
Laxman Dewanganc31a5712016-03-11 19:13:21 +05304221 return status;
4222 }
Benoit Parrotf625d462015-02-02 11:44:44 -06004223
4224 status = gpiod_configure_flags(desc, name, lflags, dflags);
4225 if (status < 0) {
4226 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed, %d\n",
4227 name, chip->label, hwnum, status);
4228 gpiochip_free_own_desc(desc);
4229 return status;
4230 }
4231
4232 /* Mark GPIO as hogged so it can be identified and removed later */
4233 set_bit(FLAG_IS_HOGGED, &desc->flags);
4234
4235 pr_info("GPIO line %d (%s) hogged as %s%s\n",
4236 desc_to_gpio(desc), name,
4237 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
4238 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
4239 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
4240
4241 return 0;
4242}
4243
4244/**
4245 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
4246 * @chip: gpio chip to act on
4247 *
4248 * This is only used by of_gpiochip_remove to free hogged gpios
4249 */
Linus Walleij1c3cdb12016-02-09 13:51:59 +01004250static void gpiochip_free_hogs(struct gpio_chip *chip)
4251{
Benoit Parrotf625d462015-02-02 11:44:44 -06004252 int id;
4253
4254 for (id = 0; id < chip->ngpio; id++) {
4255 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004256 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
4257 }
4258}
4259
4260/**
4261 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
4262 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4263 * @con_id: function within the GPIO consumer
4264 * @flags: optional GPIO initialization flags
4265 *
4266 * This function acquires all the GPIOs defined under a given function.
4267 *
4268 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
4269 * no GPIO has been assigned to the requested function, or another IS_ERR()
4270 * code if an error occurred while trying to acquire the GPIOs.
4271 */
4272struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
4273 const char *con_id,
4274 enum gpiod_flags flags)
4275{
4276 struct gpio_desc *desc;
4277 struct gpio_descs *descs;
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004278 struct gpio_array *array_info = NULL;
4279 struct gpio_chip *chip;
4280 int count, bitmap_size;
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004281
4282 count = gpiod_count(dev, con_id);
4283 if (count < 0)
4284 return ERR_PTR(count);
4285
Kees Cookacafe7e2018-05-08 13:45:50 -07004286 descs = kzalloc(struct_size(descs, desc, count), GFP_KERNEL);
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004287 if (!descs)
4288 return ERR_PTR(-ENOMEM);
4289
4290 for (descs->ndescs = 0; descs->ndescs < count; ) {
4291 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
4292 if (IS_ERR(desc)) {
4293 gpiod_put_array(descs);
4294 return ERR_CAST(desc);
4295 }
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004296
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004297 descs->desc[descs->ndescs] = desc;
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004298
4299 chip = gpiod_to_chip(desc);
4300 /*
4301 * Select a chip of first array member
4302 * whose index matches its pin hardware number
4303 * as a candidate for fast bitmap processing.
4304 */
4305 if (!array_info && gpio_chip_hwgpio(desc) == descs->ndescs) {
4306 struct gpio_descs *array;
4307
4308 bitmap_size = BITS_TO_LONGS(chip->ngpio > count ?
4309 chip->ngpio : count);
4310
4311 array = kzalloc(struct_size(descs, desc, count) +
4312 struct_size(array_info, invert_mask,
4313 3 * bitmap_size), GFP_KERNEL);
4314 if (!array) {
4315 gpiod_put_array(descs);
4316 return ERR_PTR(-ENOMEM);
4317 }
4318
4319 memcpy(array, descs,
4320 struct_size(descs, desc, descs->ndescs + 1));
4321 kfree(descs);
4322
4323 descs = array;
4324 array_info = (void *)(descs->desc + count);
4325 array_info->get_mask = array_info->invert_mask +
4326 bitmap_size;
4327 array_info->set_mask = array_info->get_mask +
4328 bitmap_size;
4329
4330 array_info->desc = descs->desc;
4331 array_info->size = count;
4332 array_info->chip = chip;
4333 bitmap_set(array_info->get_mask, descs->ndescs,
4334 count - descs->ndescs);
4335 bitmap_set(array_info->set_mask, descs->ndescs,
4336 count - descs->ndescs);
4337 descs->info = array_info;
4338 }
4339 /*
4340 * Unmark members which don't qualify for fast bitmap
4341 * processing (different chip, not in hardware order)
4342 */
4343 if (array_info && (chip != array_info->chip ||
4344 gpio_chip_hwgpio(desc) != descs->ndescs)) {
4345 __clear_bit(descs->ndescs, array_info->get_mask);
4346 __clear_bit(descs->ndescs, array_info->set_mask);
4347 } else if (array_info) {
4348 /* Exclude open drain or open source from fast output */
4349 if (gpiochip_line_is_open_drain(chip, descs->ndescs) ||
4350 gpiochip_line_is_open_source(chip, descs->ndescs))
4351 __clear_bit(descs->ndescs,
4352 array_info->set_mask);
4353 /* Identify 'fast' pins which require invertion */
4354 if (gpiod_is_active_low(desc))
4355 __set_bit(descs->ndescs,
4356 array_info->invert_mask);
4357 }
4358
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004359 descs->ndescs++;
4360 }
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004361 if (array_info)
4362 dev_dbg(dev,
4363 "GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n",
4364 array_info->chip->label, array_info->size,
4365 *array_info->get_mask, *array_info->set_mask,
4366 *array_info->invert_mask);
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004367 return descs;
4368}
4369EXPORT_SYMBOL_GPL(gpiod_get_array);
4370
4371/**
4372 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
4373 * function
4374 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4375 * @con_id: function within the GPIO consumer
4376 * @flags: optional GPIO initialization flags
4377 *
4378 * This is equivalent to gpiod_get_array(), except that when no GPIO was
4379 * assigned to the requested function it will return NULL.
4380 */
4381struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
4382 const char *con_id,
4383 enum gpiod_flags flags)
4384{
4385 struct gpio_descs *descs;
4386
4387 descs = gpiod_get_array(dev, con_id, flags);
4388 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
4389 return NULL;
4390
4391 return descs;
4392}
4393EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
4394
4395/**
David Brownelld2876d02008-02-04 22:28:20 -08004396 * gpiod_put - dispose of a GPIO descriptor
4397 * @desc: GPIO descriptor to dispose of
4398 *
4399 * No descriptor can be used after gpiod_put() has been called on it.
4400 */
4401void gpiod_put(struct gpio_desc *desc)
4402{
4403 gpiod_free(desc);
4404}
4405EXPORT_SYMBOL_GPL(gpiod_put);
4406
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004407/**
4408 * gpiod_put_array - dispose of multiple GPIO descriptors
4409 * @descs: struct gpio_descs containing an array of descriptors
4410 */
4411void gpiod_put_array(struct gpio_descs *descs)
4412{
4413 unsigned int i;
4414
4415 for (i = 0; i < descs->ndescs; i++)
4416 gpiod_put(descs->desc[i]);
4417
4418 kfree(descs);
4419}
4420EXPORT_SYMBOL_GPL(gpiod_put_array);
4421
Linus Walleij3c702e92015-10-21 15:29:53 +02004422static int __init gpiolib_dev_init(void)
4423{
4424 int ret;
4425
4426 /* Register GPIO sysfs bus */
Andy Shevchenkob1911712018-07-03 03:39:03 +03004427 ret = bus_register(&gpio_bus_type);
Linus Walleij3c702e92015-10-21 15:29:53 +02004428 if (ret < 0) {
4429 pr_err("gpiolib: could not register GPIO bus type\n");
4430 return ret;
4431 }
4432
4433 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
4434 if (ret < 0) {
4435 pr_err("gpiolib: failed to allocate char dev region\n");
4436 bus_unregister(&gpio_bus_type);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07004437 } else {
4438 gpiolib_initialized = true;
4439 gpiochip_setup_devs();
Linus Walleij3c702e92015-10-21 15:29:53 +02004440 }
4441 return ret;
4442}
4443core_initcall(gpiolib_dev_init);
4444
David Brownelld2876d02008-02-04 22:28:20 -08004445#ifdef CONFIG_DEBUG_FS
4446
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004447static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
David Brownelld2876d02008-02-04 22:28:20 -08004448{
4449 unsigned i;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004450 struct gpio_chip *chip = gdev->chip;
4451 unsigned gpio = gdev->base;
4452 struct gpio_desc *gdesc = &gdev->descs[0];
David Brownelld2876d02008-02-04 22:28:20 -08004453 int is_out;
4454 int is_irq;
4455
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004456 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02004457 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
4458 if (gdesc->name) {
4459 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
4460 gpio, gdesc->name);
4461 }
David Brownelld2876d02008-02-04 22:28:20 -08004462 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02004463 }
David Brownelld2876d02008-02-04 22:28:20 -08004464
4465 gpiod_get_direction(gdesc);
4466 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
4467 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Markus Pargmannced433e2015-08-14 16:11:02 +02004468 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
4469 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08004470 is_out ? "out" : "in ",
Andy Shevchenko1c22a252018-07-12 20:36:42 +03004471 chip->get ? (chip->get(chip, i) ? "hi" : "lo") : "? ",
David Brownelld2876d02008-02-04 22:28:20 -08004472 is_irq ? "IRQ" : " ");
4473 seq_printf(s, "\n");
4474 }
4475}
4476
4477static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
4478{
4479 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02004480 struct gpio_device *gdev = NULL;
David Brownelld2876d02008-02-04 22:28:20 -08004481 loff_t index = *pos;
4482
4483 s->private = "";
4484
4485 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02004486 list_for_each_entry(gdev, &gpio_devices, list)
David Brownelld2876d02008-02-04 22:28:20 -08004487 if (index-- == 0) {
4488 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02004489 return gdev;
David Brownelld2876d02008-02-04 22:28:20 -08004490 }
4491 spin_unlock_irqrestore(&gpio_lock, flags);
4492
4493 return NULL;
4494}
4495
4496static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
4497{
4498 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02004499 struct gpio_device *gdev = v;
David Brownelld2876d02008-02-04 22:28:20 -08004500 void *ret = NULL;
4501
4502 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02004503 if (list_is_last(&gdev->list, &gpio_devices))
David Brownelld2876d02008-02-04 22:28:20 -08004504 ret = NULL;
4505 else
Linus Walleijff2b1352015-10-20 11:10:38 +02004506 ret = list_entry(gdev->list.next, struct gpio_device, list);
David Brownelld2876d02008-02-04 22:28:20 -08004507 spin_unlock_irqrestore(&gpio_lock, flags);
4508
4509 s->private = "\n";
4510 ++*pos;
4511
4512 return ret;
4513}
4514
4515static void gpiolib_seq_stop(struct seq_file *s, void *v)
4516{
4517}
4518
4519static int gpiolib_seq_show(struct seq_file *s, void *v)
4520{
Linus Walleijff2b1352015-10-20 11:10:38 +02004521 struct gpio_device *gdev = v;
4522 struct gpio_chip *chip = gdev->chip;
4523 struct device *parent;
David Brownelld2876d02008-02-04 22:28:20 -08004524
Linus Walleijff2b1352015-10-20 11:10:38 +02004525 if (!chip) {
4526 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
4527 dev_name(&gdev->dev));
4528 return 0;
4529 }
4530
4531 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
4532 dev_name(&gdev->dev),
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004533 gdev->base, gdev->base + gdev->ngpio - 1);
Linus Walleijff2b1352015-10-20 11:10:38 +02004534 parent = chip->parent;
4535 if (parent)
4536 seq_printf(s, ", parent: %s/%s",
4537 parent->bus ? parent->bus->name : "no-bus",
4538 dev_name(parent));
David Brownelld2876d02008-02-04 22:28:20 -08004539 if (chip->label)
4540 seq_printf(s, ", %s", chip->label);
4541 if (chip->can_sleep)
4542 seq_printf(s, ", can sleep");
4543 seq_printf(s, ":\n");
4544
4545 if (chip->dbg_show)
4546 chip->dbg_show(s, chip);
4547 else
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004548 gpiolib_dbg_show(s, gdev);
David Brownelld2876d02008-02-04 22:28:20 -08004549
4550 return 0;
4551}
4552
4553static const struct seq_operations gpiolib_seq_ops = {
4554 .start = gpiolib_seq_start,
4555 .next = gpiolib_seq_next,
4556 .stop = gpiolib_seq_stop,
4557 .show = gpiolib_seq_show,
4558};
4559
4560static int gpiolib_open(struct inode *inode, struct file *file)
4561{
4562 return seq_open(file, &gpiolib_seq_ops);
4563}
4564
4565static const struct file_operations gpiolib_operations = {
4566 .owner = THIS_MODULE,
4567 .open = gpiolib_open,
4568 .read = seq_read,
4569 .llseek = seq_lseek,
4570 .release = seq_release,
4571};
4572
4573static int __init gpiolib_debugfs_init(void)
4574{
4575 /* /sys/kernel/debug/gpio */
4576 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
4577 NULL, NULL, &gpiolib_operations);
4578 return 0;
4579}
4580subsys_initcall(gpiolib_debugfs_init);
4581
4582#endif /* DEBUG_FS */