blob: cd7c1deed132db7bc919559d289281c961d7ff4f [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{
2792 int i = 0;
2793
2794 while (i < array_size) {
2795 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Laura Abbott30277432018-05-21 10:57:07 -07002796 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
2797 unsigned long *mask, *bits;
Lukas Wunnereec1d562017-10-12 12:40:10 +02002798 int first, j, ret;
2799
Laura Abbott30277432018-05-21 10:57:07 -07002800 if (likely(chip->ngpio <= FASTPATH_NGPIO)) {
2801 mask = fastpath;
2802 } else {
2803 mask = kmalloc_array(2 * BITS_TO_LONGS(chip->ngpio),
2804 sizeof(*mask),
2805 can_sleep ? GFP_KERNEL : GFP_ATOMIC);
2806 if (!mask)
2807 return -ENOMEM;
2808 }
2809
2810 bits = mask + BITS_TO_LONGS(chip->ngpio);
2811 bitmap_zero(mask, chip->ngpio);
2812
Lukas Wunnereec1d562017-10-12 12:40:10 +02002813 if (!can_sleep)
2814 WARN_ON(chip->can_sleep);
2815
2816 /* collect all inputs belonging to the same chip */
2817 first = i;
Lukas Wunnereec1d562017-10-12 12:40:10 +02002818 do {
2819 const struct gpio_desc *desc = desc_array[i];
2820 int hwgpio = gpio_chip_hwgpio(desc);
2821
2822 __set_bit(hwgpio, mask);
2823 i++;
2824 } while ((i < array_size) &&
2825 (desc_array[i]->gdev->chip == chip));
2826
2827 ret = gpio_chip_get_multiple(chip, mask, bits);
Laura Abbott30277432018-05-21 10:57:07 -07002828 if (ret) {
2829 if (mask != fastpath)
2830 kfree(mask);
Lukas Wunnereec1d562017-10-12 12:40:10 +02002831 return ret;
Laura Abbott30277432018-05-21 10:57:07 -07002832 }
Lukas Wunnereec1d562017-10-12 12:40:10 +02002833
2834 for (j = first; j < i; j++) {
2835 const struct gpio_desc *desc = desc_array[j];
2836 int hwgpio = gpio_chip_hwgpio(desc);
2837 int value = test_bit(hwgpio, bits);
2838
2839 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2840 value = !value;
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02002841 __assign_bit(j, value_bitmap, value);
Lukas Wunnereec1d562017-10-12 12:40:10 +02002842 trace_gpio_value(desc_to_gpio(desc), 1, value);
2843 }
Laura Abbott30277432018-05-21 10:57:07 -07002844
2845 if (mask != fastpath)
2846 kfree(mask);
Lukas Wunnereec1d562017-10-12 12:40:10 +02002847 }
2848 return 0;
2849}
2850
David Brownelld2876d02008-02-04 22:28:20 -08002851/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002852 * gpiod_get_raw_value() - return a gpio's raw value
2853 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08002854 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002855 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002856 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002857 *
2858 * This function should be called from contexts where we cannot sleep, and will
2859 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002860 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002861int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002862{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002863 VALIDATE_DESC(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002864 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002865 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02002866 return gpiod_get_raw_value_commit(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002867}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002868EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002869
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002870/**
2871 * gpiod_get_value() - return a gpio's value
2872 * @desc: gpio whose value will be returned
2873 *
2874 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002875 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002876 *
2877 * This function should be called from contexts where we cannot sleep, and will
2878 * complain if the GPIO chip functions potentially sleep.
2879 */
2880int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002881{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002882 int value;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002883
2884 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002885 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002886 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002887
Linus Walleijfac9d882017-09-26 20:58:28 +02002888 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002889 if (value < 0)
2890 return value;
2891
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002892 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2893 value = !value;
2894
2895 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002896}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002897EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08002898
Lukas Wunnereec1d562017-10-12 12:40:10 +02002899/**
2900 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02002901 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02002902 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02002903 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02002904 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02002905 *
2906 * Read the raw values of the GPIOs, i.e. the values of the physical lines
2907 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
2908 * else an error code.
2909 *
2910 * This function should be called from contexts where we cannot sleep,
2911 * and it will complain if the GPIO chip functions potentially sleep.
2912 */
2913int gpiod_get_raw_array_value(unsigned int array_size,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02002914 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02002915 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02002916 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02002917{
2918 if (!desc_array)
2919 return -EINVAL;
2920 return gpiod_get_array_value_complex(true, false, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02002921 desc_array, array_info,
2922 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02002923}
2924EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
2925
2926/**
2927 * gpiod_get_array_value() - read values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02002928 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02002929 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02002930 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02002931 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02002932 *
2933 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2934 * into account. Return 0 in case of success, else an error code.
2935 *
2936 * This function should be called from contexts where we cannot sleep,
2937 * and it will complain if the GPIO chip functions potentially sleep.
2938 */
2939int gpiod_get_array_value(unsigned int array_size,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02002940 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02002941 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02002942 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02002943{
2944 if (!desc_array)
2945 return -EINVAL;
2946 return gpiod_get_array_value_complex(false, false, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02002947 desc_array, array_info,
2948 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02002949}
2950EXPORT_SYMBOL_GPL(gpiod_get_array_value);
2951
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302952/*
Linus Walleijfac9d882017-09-26 20:58:28 +02002953 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002954 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002955 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302956 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002957static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302958{
2959 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002960 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002961 int offset = gpio_chip_hwgpio(desc);
2962
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302963 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002964 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302965 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002966 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302967 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002968 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302969 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002970 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302971 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002972 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302973 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002974 gpiod_err(desc,
2975 "%s: Error in set_value for open drain err %d\n",
2976 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302977}
2978
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302979/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002980 * _gpio_set_open_source_value() - Set the open source gpio's value.
2981 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002982 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302983 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002984static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302985{
2986 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002987 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002988 int offset = gpio_chip_hwgpio(desc);
2989
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302990 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002991 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302992 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002993 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302994 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002995 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302996 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002997 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302998 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002999 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303000 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01003001 gpiod_err(desc,
3002 "%s: Error in set_value for open source err %d\n",
3003 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05303004}
3005
Linus Walleijfac9d882017-09-26 20:58:28 +02003006static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08003007{
3008 struct gpio_chip *chip;
3009
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003010 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003011 trace_gpio_value(desc_to_gpio(desc), 0, value);
Linus Walleij02e47982017-09-26 21:20:23 +02003012 chip->set(chip, gpio_chip_hwgpio(desc), value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003013}
3014
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003015/*
3016 * set multiple outputs on the same chip;
3017 * use the chip's set_multiple function if available;
3018 * otherwise set the outputs sequentially;
3019 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
3020 * defines which outputs are to be changed
3021 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
3022 * defines the values the outputs specified by mask are to be set to
3023 */
3024static void gpio_chip_set_multiple(struct gpio_chip *chip,
3025 unsigned long *mask, unsigned long *bits)
3026{
3027 if (chip->set_multiple) {
3028 chip->set_multiple(chip, mask, bits);
3029 } else {
Andy Shevchenko5e4e6fb2017-01-03 19:01:17 +02003030 unsigned int i;
3031
3032 /* set outputs if the corresponding mask bit is set */
3033 for_each_set_bit(i, mask, chip->ngpio)
3034 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003035 }
3036}
3037
Laura Abbott30277432018-05-21 10:57:07 -07003038int gpiod_set_array_value_complex(bool raw, bool can_sleep,
Linus Walleij44c72882016-04-24 11:36:59 +02003039 unsigned int array_size,
3040 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003041 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003042 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003043{
3044 int i = 0;
3045
3046 while (i < array_size) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003047 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Laura Abbott30277432018-05-21 10:57:07 -07003048 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
3049 unsigned long *mask, *bits;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003050 int count = 0;
3051
Laura Abbott30277432018-05-21 10:57:07 -07003052 if (likely(chip->ngpio <= FASTPATH_NGPIO)) {
3053 mask = fastpath;
3054 } else {
3055 mask = kmalloc_array(2 * BITS_TO_LONGS(chip->ngpio),
3056 sizeof(*mask),
3057 can_sleep ? GFP_KERNEL : GFP_ATOMIC);
3058 if (!mask)
3059 return -ENOMEM;
3060 }
3061
3062 bits = mask + BITS_TO_LONGS(chip->ngpio);
3063 bitmap_zero(mask, chip->ngpio);
3064
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003065 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003066 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003067
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003068 do {
3069 struct gpio_desc *desc = desc_array[i];
3070 int hwgpio = gpio_chip_hwgpio(desc);
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003071 int value = test_bit(i, value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003072
3073 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3074 value = !value;
3075 trace_gpio_value(desc_to_gpio(desc), 0, value);
3076 /*
3077 * collect all normal outputs belonging to the same chip
3078 * open drain and open source outputs are set individually
3079 */
Linus Walleij02e47982017-09-26 21:20:23 +02003080 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02003081 gpio_set_open_drain_value_commit(desc, value);
Linus Walleij02e47982017-09-26 21:20:23 +02003082 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02003083 gpio_set_open_source_value_commit(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003084 } else {
3085 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003086 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003087 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003088 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003089 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003090 count++;
3091 }
3092 i++;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003093 } while ((i < array_size) &&
3094 (desc_array[i]->gdev->chip == chip));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003095 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003096 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003097 gpio_chip_set_multiple(chip, mask, bits);
Laura Abbott30277432018-05-21 10:57:07 -07003098
3099 if (mask != fastpath)
3100 kfree(mask);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003101 }
Laura Abbott30277432018-05-21 10:57:07 -07003102 return 0;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003103}
3104
David Brownelld2876d02008-02-04 22:28:20 -08003105/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003106 * gpiod_set_raw_value() - assign a gpio's raw value
3107 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08003108 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08003109 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003110 * Set the raw value of the GPIO, i.e. the value of its physical line without
3111 * regard for its ACTIVE_LOW status.
3112 *
3113 * This function should be called from contexts where we cannot sleep, and will
3114 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003115 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003116void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003117{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003118 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01003119 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003120 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02003121 gpiod_set_raw_value_commit(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08003122}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003123EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08003124
3125/**
Geert Uytterhoeven1e77fc82018-01-09 19:08:21 +01003126 * gpiod_set_value_nocheck() - set a GPIO line value without checking
3127 * @desc: the descriptor to set the value on
3128 * @value: value to set
3129 *
3130 * This sets the value of a GPIO line backing a descriptor, applying
3131 * different semantic quirks like active low and open drain/source
3132 * handling.
3133 */
3134static void gpiod_set_value_nocheck(struct gpio_desc *desc, int value)
3135{
3136 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3137 value = !value;
3138 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
3139 gpio_set_open_drain_value_commit(desc, value);
3140 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
3141 gpio_set_open_source_value_commit(desc, value);
3142 else
3143 gpiod_set_raw_value_commit(desc, value);
3144}
3145
3146/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003147 * gpiod_set_value() - assign a gpio's value
3148 * @desc: gpio whose value will be assigned
3149 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08003150 *
Linus Walleij02e47982017-09-26 21:20:23 +02003151 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
3152 * OPEN_DRAIN and OPEN_SOURCE flags into account.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003153 *
3154 * This function should be called from contexts where we cannot sleep, and will
3155 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003156 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003157void gpiod_set_value(struct gpio_desc *desc, int value)
3158{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003159 VALIDATE_DESC_VOID(desc);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003160 WARN_ON(desc->gdev->chip->can_sleep);
Geert Uytterhoeven1e77fc82018-01-09 19:08:21 +01003161 gpiod_set_value_nocheck(desc, value);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003162}
3163EXPORT_SYMBOL_GPL(gpiod_set_value);
3164
3165/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003166 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003167 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003168 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003169 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003170 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003171 *
3172 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3173 * without regard for their ACTIVE_LOW status.
3174 *
3175 * This function should be called from contexts where we cannot sleep, and will
3176 * complain if the GPIO chip functions potentially sleep.
3177 */
Laura Abbott30277432018-05-21 10:57:07 -07003178int gpiod_set_raw_array_value(unsigned int array_size,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003179 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003180 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003181 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003182{
3183 if (!desc_array)
Laura Abbott30277432018-05-21 10:57:07 -07003184 return -EINVAL;
3185 return gpiod_set_array_value_complex(true, false, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003186 desc_array, array_info, value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003187}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003188EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003189
3190/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003191 * gpiod_set_array_value() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003192 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003193 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003194 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003195 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003196 *
3197 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3198 * into account.
3199 *
3200 * This function should be called from contexts where we cannot sleep, and will
3201 * complain if the GPIO chip functions potentially sleep.
3202 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003203void gpiod_set_array_value(unsigned int array_size,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003204 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003205 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003206 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003207{
3208 if (!desc_array)
3209 return;
Linus Walleij44c72882016-04-24 11:36:59 +02003210 gpiod_set_array_value_complex(false, false, array_size, desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003211 array_info, value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003212}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003213EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003214
3215/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003216 * gpiod_cansleep() - report whether gpio value access may sleep
3217 * @desc: gpio to check
3218 *
3219 */
3220int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003221{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003222 VALIDATE_DESC(desc);
3223 return desc->gdev->chip->can_sleep;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003224}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003225EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08003226
David Brownell0f6d5042008-10-15 22:03:14 -07003227/**
Linus Walleij90b39402e2018-06-01 13:21:27 +02003228 * gpiod_set_consumer_name() - set the consumer name for the descriptor
3229 * @desc: gpio to set the consumer name on
3230 * @name: the new consumer name
3231 */
3232void gpiod_set_consumer_name(struct gpio_desc *desc, const char *name)
3233{
3234 VALIDATE_DESC_VOID(desc);
3235 /* Just overwrite whatever the previous name was */
3236 desc->label = name;
3237}
3238EXPORT_SYMBOL_GPL(gpiod_set_consumer_name);
3239
3240/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003241 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
3242 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07003243 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003244 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
3245 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07003246 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003247int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07003248{
Linus Walleij4c37ce82016-05-02 13:13:10 +02003249 struct gpio_chip *chip;
3250 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07003251
Linus Walleij79bb71b2016-06-15 22:57:38 +02003252 /*
3253 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
3254 * requires this function to not return zero on an invalid descriptor
3255 * but rather a negative error number.
3256 */
Linus Walleijbfbbe442016-06-16 11:55:55 +02003257 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
Linus Walleij79bb71b2016-06-15 22:57:38 +02003258 return -EINVAL;
3259
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003260 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003261 offset = gpio_chip_hwgpio(desc);
Linus Walleij4c37ce82016-05-02 13:13:10 +02003262 if (chip->to_irq) {
3263 int retirq = chip->to_irq(chip, offset);
3264
3265 /* Zero means NO_IRQ */
3266 if (!retirq)
3267 return -ENXIO;
3268
3269 return retirq;
3270 }
3271 return -ENXIO;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003272}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003273EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003274
Linus Walleijd468bf92013-09-24 11:54:38 +02003275/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003276 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003277 * @chip: the chip the GPIO to lock belongs to
3278 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02003279 *
3280 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08003281 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08003282 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003283int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08003284{
Linus Walleij9c102802016-05-25 10:56:03 +02003285 struct gpio_desc *desc;
Linus Walleijd468bf92013-09-24 11:54:38 +02003286
Linus Walleij9c102802016-05-25 10:56:03 +02003287 desc = gpiochip_get_desc(chip, offset);
3288 if (IS_ERR(desc))
3289 return PTR_ERR(desc);
3290
Linus Walleij60f83392016-11-12 15:01:09 +01003291 /*
3292 * If it's fast: flush the direction setting if something changed
3293 * behind our back
3294 */
3295 if (!chip->can_sleep && chip->get_direction) {
Andy Shevchenko80956792018-07-09 21:47:21 +03003296 int dir = gpiod_get_direction(desc);
Linus Walleij9c102802016-05-25 10:56:03 +02003297
Andy Shevchenko36b31272018-07-03 03:38:31 +03003298 if (dir < 0) {
3299 chip_err(chip, "%s: cannot get GPIO direction\n",
3300 __func__);
3301 return dir;
3302 }
Linus Walleij9c102802016-05-25 10:56:03 +02003303 }
3304
3305 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003306 chip_err(chip,
Andy Shevchenkob1911712018-07-03 03:39:03 +03003307 "%s: tried to flag a GPIO set as output for IRQ\n",
3308 __func__);
Linus Walleijd468bf92013-09-24 11:54:38 +02003309 return -EIO;
3310 }
3311
Linus Walleij9c102802016-05-25 10:56:03 +02003312 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
Linus Walleij3940c342016-11-14 00:09:07 +01003313
3314 /*
3315 * If the consumer has not set up a label (such as when the
3316 * IRQ is referenced from .to_irq()) we set up a label here
3317 * so it is clear this is used as an interrupt.
3318 */
3319 if (!desc->label)
3320 desc_set_label(desc, "interrupt");
3321
Linus Walleijd468bf92013-09-24 11:54:38 +02003322 return 0;
3323}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003324EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02003325
Linus Walleijd468bf92013-09-24 11:54:38 +02003326/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003327 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003328 * @chip: the chip the GPIO to lock belongs to
3329 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02003330 *
3331 * This is used directly by GPIO drivers that want to indicate
3332 * that a certain GPIO is no longer used exclusively for IRQ.
3333 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003334void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02003335{
Linus Walleij3940c342016-11-14 00:09:07 +01003336 struct gpio_desc *desc;
3337
3338 desc = gpiochip_get_desc(chip, offset);
3339 if (IS_ERR(desc))
Linus Walleijd468bf92013-09-24 11:54:38 +02003340 return;
3341
Linus Walleij3940c342016-11-14 00:09:07 +01003342 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
3343
3344 /* If we only had this marking, erase it */
3345 if (desc->label && !strcmp(desc->label, "interrupt"))
3346 desc_set_label(desc, NULL);
Linus Walleijd468bf92013-09-24 11:54:38 +02003347}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003348EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02003349
Linus Walleij6cee3822016-02-11 20:16:45 +01003350bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
3351{
3352 if (offset >= chip->ngpio)
3353 return false;
3354
3355 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
3356}
3357EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
3358
Linus Walleij143b65d2016-02-16 15:41:42 +01003359bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
3360{
3361 if (offset >= chip->ngpio)
3362 return false;
3363
3364 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
3365}
3366EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
3367
3368bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
3369{
3370 if (offset >= chip->ngpio)
3371 return false;
3372
3373 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
3374}
3375EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
3376
Charles Keepax05f479b2017-05-23 15:47:29 +01003377bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset)
3378{
3379 if (offset >= chip->ngpio)
3380 return false;
3381
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303382 return !test_bit(FLAG_TRANSITORY, &chip->gpiodev->descs[offset].flags);
Charles Keepax05f479b2017-05-23 15:47:29 +01003383}
3384EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
3385
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003386/**
3387 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
3388 * @desc: gpio whose value will be returned
3389 *
3390 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003391 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003392 *
3393 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003394 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003395int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08003396{
David Brownelld2876d02008-02-04 22:28:20 -08003397 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003398 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003399 return gpiod_get_raw_value_commit(desc);
David Brownelld2876d02008-02-04 22:28:20 -08003400}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003401EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003402
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003403/**
3404 * gpiod_get_value_cansleep() - return a gpio's value
3405 * @desc: gpio whose value will be returned
3406 *
3407 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003408 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003409 *
3410 * This function is to be called from contexts that can sleep.
3411 */
3412int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003413{
David Brownelld2876d02008-02-04 22:28:20 -08003414 int value;
David Brownelld2876d02008-02-04 22:28:20 -08003415
3416 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003417 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003418 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003419 if (value < 0)
3420 return value;
3421
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003422 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3423 value = !value;
3424
David Brownelld2876d02008-02-04 22:28:20 -08003425 return value;
3426}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003427EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003428
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003429/**
Lukas Wunnereec1d562017-10-12 12:40:10 +02003430 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003431 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02003432 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003433 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003434 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02003435 *
3436 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3437 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3438 * else an error code.
3439 *
3440 * This function is to be called from contexts that can sleep.
3441 */
3442int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
3443 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003444 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003445 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02003446{
3447 might_sleep_if(extra_checks);
3448 if (!desc_array)
3449 return -EINVAL;
3450 return gpiod_get_array_value_complex(true, true, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003451 desc_array, array_info,
3452 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003453}
3454EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
3455
3456/**
3457 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003458 * @array_size: number of elements in the descriptor array / value bitmap
Lukas Wunnereec1d562017-10-12 12:40:10 +02003459 * @desc_array: array of GPIO descriptors whose values will be read
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003460 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003461 * @value_bitmap: bitmap to store the read values
Lukas Wunnereec1d562017-10-12 12:40:10 +02003462 *
3463 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3464 * into account. Return 0 in case of success, else an error code.
3465 *
3466 * This function is to be called from contexts that can sleep.
3467 */
3468int gpiod_get_array_value_cansleep(unsigned int array_size,
3469 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003470 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003471 unsigned long *value_bitmap)
Lukas Wunnereec1d562017-10-12 12:40:10 +02003472{
3473 might_sleep_if(extra_checks);
3474 if (!desc_array)
3475 return -EINVAL;
3476 return gpiod_get_array_value_complex(false, true, array_size,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003477 desc_array, array_info,
3478 value_bitmap);
Lukas Wunnereec1d562017-10-12 12:40:10 +02003479}
3480EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
3481
3482/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003483 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
3484 * @desc: gpio whose value will be assigned
3485 * @value: value to assign
3486 *
3487 * Set the raw value of the GPIO, i.e. the value of its physical line without
3488 * regard for its ACTIVE_LOW status.
3489 *
3490 * This function is to be called from contexts that can sleep.
3491 */
3492void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003493{
David Brownelld2876d02008-02-04 22:28:20 -08003494 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003495 VALIDATE_DESC_VOID(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003496 gpiod_set_raw_value_commit(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003497}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003498EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003499
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003500/**
3501 * gpiod_set_value_cansleep() - assign a gpio's value
3502 * @desc: gpio whose value will be assigned
3503 * @value: value to assign
3504 *
3505 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3506 * account
3507 *
3508 * This function is to be called from contexts that can sleep.
3509 */
3510void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003511{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003512 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003513 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1e77fc82018-01-09 19:08:21 +01003514 gpiod_set_value_nocheck(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08003515}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003516EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08003517
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003518/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003519 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003520 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003521 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003522 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003523 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003524 *
3525 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3526 * without regard for their ACTIVE_LOW status.
3527 *
3528 * This function is to be called from contexts that can sleep.
3529 */
Laura Abbott30277432018-05-21 10:57:07 -07003530int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003531 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003532 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003533 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003534{
3535 might_sleep_if(extra_checks);
3536 if (!desc_array)
Laura Abbott30277432018-05-21 10:57:07 -07003537 return -EINVAL;
3538 return gpiod_set_array_value_complex(true, true, array_size, desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003539 array_info, value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003540}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003541EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003542
3543/**
Dmitry Torokhov3946d182017-08-14 21:59:55 -07003544 * gpiod_add_lookup_tables() - register GPIO device consumers
3545 * @tables: list of tables of consumers to register
3546 * @n: number of tables in the list
3547 */
3548void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
3549{
3550 unsigned int i;
3551
3552 mutex_lock(&gpio_lookup_lock);
3553
3554 for (i = 0; i < n; i++)
3555 list_add_tail(&tables[i]->list, &gpio_lookup_list);
3556
3557 mutex_unlock(&gpio_lookup_lock);
3558}
3559
3560/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003561 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003562 * @array_size: number of elements in the descriptor array / value bitmap
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003563 * @desc_array: array of GPIO descriptors whose values will be assigned
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003564 * @array_info: information on applicability of fast bitmap processing path
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003565 * @value_bitmap: bitmap of values to assign
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003566 *
3567 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3568 * into account.
3569 *
3570 * This function is to be called from contexts that can sleep.
3571 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003572void gpiod_set_array_value_cansleep(unsigned int array_size,
3573 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003574 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +02003575 unsigned long *value_bitmap)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003576{
3577 might_sleep_if(extra_checks);
3578 if (!desc_array)
3579 return;
Linus Walleij44c72882016-04-24 11:36:59 +02003580 gpiod_set_array_value_complex(false, true, array_size, desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +02003581 array_info, value_bitmap);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003582}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003583EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003584
3585/**
Alexandre Courbotad824782013-12-03 12:20:11 +09003586 * gpiod_add_lookup_table() - register GPIO device consumers
3587 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003588 */
Alexandre Courbotad824782013-12-03 12:20:11 +09003589void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003590{
3591 mutex_lock(&gpio_lookup_lock);
3592
Alexandre Courbotad824782013-12-03 12:20:11 +09003593 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003594
3595 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08003596}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02003597EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
David Brownelld2876d02008-02-04 22:28:20 -08003598
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05303599/**
3600 * gpiod_remove_lookup_table() - unregister GPIO device consumers
3601 * @table: table of consumers to unregister
3602 */
3603void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
3604{
3605 mutex_lock(&gpio_lookup_lock);
3606
3607 list_del(&table->list);
3608
3609 mutex_unlock(&gpio_lookup_lock);
3610}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02003611EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05303612
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02003613/**
3614 * gpiod_add_hogs() - register a set of GPIO hogs from machine code
3615 * @hogs: table of gpio hog entries with a zeroed sentinel at the end
3616 */
3617void gpiod_add_hogs(struct gpiod_hog *hogs)
3618{
3619 struct gpio_chip *chip;
3620 struct gpiod_hog *hog;
3621
3622 mutex_lock(&gpio_machine_hogs_mutex);
3623
3624 for (hog = &hogs[0]; hog->chip_label; hog++) {
3625 list_add_tail(&hog->list, &gpio_machine_hogs);
3626
3627 /*
3628 * The chip may have been registered earlier, so check if it
3629 * exists and, if so, try to hog the line now.
3630 */
3631 chip = find_chip_by_name(hog->chip_label);
3632 if (chip)
3633 gpiochip_machine_hog(chip, hog);
3634 }
3635
3636 mutex_unlock(&gpio_machine_hogs_mutex);
3637}
3638EXPORT_SYMBOL_GPL(gpiod_add_hogs);
3639
Alexandre Courbotad824782013-12-03 12:20:11 +09003640static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
3641{
3642 const char *dev_id = dev ? dev_name(dev) : NULL;
3643 struct gpiod_lookup_table *table;
3644
3645 mutex_lock(&gpio_lookup_lock);
3646
3647 list_for_each_entry(table, &gpio_lookup_list, list) {
3648 if (table->dev_id && dev_id) {
3649 /*
3650 * Valid strings on both ends, must be identical to have
3651 * a match
3652 */
3653 if (!strcmp(table->dev_id, dev_id))
3654 goto found;
3655 } else {
3656 /*
3657 * One of the pointers is NULL, so both must be to have
3658 * a match
3659 */
3660 if (dev_id == table->dev_id)
3661 goto found;
3662 }
3663 }
3664 table = NULL;
3665
3666found:
3667 mutex_unlock(&gpio_lookup_lock);
3668 return table;
3669}
3670
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003671static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09003672 unsigned int idx,
3673 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003674{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003675 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09003676 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003677 struct gpiod_lookup *p;
3678
Alexandre Courbotad824782013-12-03 12:20:11 +09003679 table = gpiod_find_lookup_table(dev);
3680 if (!table)
3681 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003682
Alexandre Courbotad824782013-12-03 12:20:11 +09003683 for (p = &table->table[0]; p->chip_label; p++) {
3684 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003685
Alexandre Courbotad824782013-12-03 12:20:11 +09003686 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003687 if (p->idx != idx)
3688 continue;
3689
Alexandre Courbotad824782013-12-03 12:20:11 +09003690 /* If the lookup entry has a con_id, require exact match */
3691 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
3692 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003693
Alexandre Courbotad824782013-12-03 12:20:11 +09003694 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003695
Alexandre Courbotad824782013-12-03 12:20:11 +09003696 if (!chip) {
Janusz Krzysztofik8853daf2018-07-04 00:18:19 +02003697 /*
3698 * As the lookup table indicates a chip with
3699 * p->chip_label should exist, assume it may
3700 * still appear later and let the interested
3701 * consumer be probed again or let the Deferred
3702 * Probe infrastructure handle the error.
3703 */
3704 dev_warn(dev, "cannot find GPIO chip %s, deferring\n",
3705 p->chip_label);
3706 return ERR_PTR(-EPROBE_DEFER);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003707 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003708
Alexandre Courbotad824782013-12-03 12:20:11 +09003709 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003710 dev_err(dev,
3711 "requested GPIO %d is out of range [0..%d] for chip %s\n",
3712 idx, chip->ngpio, chip->label);
3713 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09003714 }
3715
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09003716 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09003717 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003718
3719 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09003720 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003721
3722 return desc;
3723}
3724
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003725static int dt_gpio_count(struct device *dev, const char *con_id)
3726{
3727 int ret;
3728 char propname[32];
3729 unsigned int i;
3730
3731 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
3732 if (con_id)
3733 snprintf(propname, sizeof(propname), "%s-%s",
3734 con_id, gpio_suffixes[i]);
3735 else
3736 snprintf(propname, sizeof(propname), "%s",
3737 gpio_suffixes[i]);
3738
3739 ret = of_gpio_named_count(dev->of_node, propname);
Andy Shevchenko4033d4a2017-02-20 18:15:47 +02003740 if (ret > 0)
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003741 break;
3742 }
Andy Shevchenko4033d4a2017-02-20 18:15:47 +02003743 return ret ? ret : -ENOENT;
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003744}
3745
3746static int platform_gpio_count(struct device *dev, const char *con_id)
3747{
3748 struct gpiod_lookup_table *table;
3749 struct gpiod_lookup *p;
3750 unsigned int count = 0;
3751
3752 table = gpiod_find_lookup_table(dev);
3753 if (!table)
3754 return -ENOENT;
3755
3756 for (p = &table->table[0]; p->chip_label; p++) {
3757 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3758 (!con_id && !p->con_id))
3759 count++;
3760 }
3761 if (!count)
3762 return -ENOENT;
3763
3764 return count;
3765}
3766
3767/**
3768 * gpiod_count - return the number of GPIOs associated with a device / function
3769 * or -ENOENT if no GPIO has been assigned to the requested function
3770 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3771 * @con_id: function within the GPIO consumer
3772 */
3773int gpiod_count(struct device *dev, const char *con_id)
3774{
3775 int count = -ENOENT;
3776
3777 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
3778 count = dt_gpio_count(dev, con_id);
3779 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
3780 count = acpi_gpio_count(dev, con_id);
3781
3782 if (count < 0)
3783 count = platform_gpio_count(dev, con_id);
3784
3785 return count;
3786}
3787EXPORT_SYMBOL_GPL(gpiod_count);
3788
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003789/**
Thierry Reding08791622014-04-25 16:54:22 +02003790 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09003791 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003792 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003793 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003794 *
3795 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003796 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07003797 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003798 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003799struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003800 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003801{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003802 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003803}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003804EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003805
3806/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02003807 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
3808 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3809 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003810 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02003811 *
3812 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
3813 * the requested function it will return NULL. This is convenient for drivers
3814 * that need to handle optional GPIOs.
3815 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003816struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003817 const char *con_id,
3818 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003819{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003820 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003821}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003822EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003823
Benoit Parrotf625d462015-02-02 11:44:44 -06003824
3825/**
3826 * gpiod_configure_flags - helper function to configure a given GPIO
3827 * @desc: gpio whose value will be assigned
3828 * @con_id: function within the GPIO consumer
Johan Hovold85b03b32016-07-03 18:32:05 +02003829 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3830 * of_get_gpio_hog()
Benoit Parrotf625d462015-02-02 11:44:44 -06003831 * @dflags: gpiod_flags - optional GPIO initialization flags
3832 *
3833 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
3834 * requested function and/or index, or another IS_ERR() code if an error
3835 * occurred while trying to acquire the GPIO.
3836 */
Andy Shevchenkoc29fd9e2017-05-23 20:03:16 +03003837int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Johan Hovold85b03b32016-07-03 18:32:05 +02003838 unsigned long lflags, enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06003839{
3840 int status;
3841
Johan Hovold85b03b32016-07-03 18:32:05 +02003842 if (lflags & GPIO_ACTIVE_LOW)
3843 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
Linus Walleijf926dfc2017-09-10 19:26:22 +02003844
Johan Hovold85b03b32016-07-03 18:32:05 +02003845 if (lflags & GPIO_OPEN_DRAIN)
3846 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
Linus Walleijf926dfc2017-09-10 19:26:22 +02003847 else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) {
3848 /*
3849 * This enforces open drain mode from the consumer side.
3850 * This is necessary for some busses like I2C, but the lookup
3851 * should *REALLY* have specified them as open drain in the
3852 * first place, so print a little warning here.
3853 */
3854 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3855 gpiod_warn(desc,
3856 "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
3857 }
3858
Johan Hovold85b03b32016-07-03 18:32:05 +02003859 if (lflags & GPIO_OPEN_SOURCE)
3860 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303861
3862 status = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
3863 if (status < 0)
3864 return status;
Johan Hovold85b03b32016-07-03 18:32:05 +02003865
Benoit Parrotf625d462015-02-02 11:44:44 -06003866 /* No particular flag request, return here... */
3867 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
3868 pr_debug("no flags found for %s\n", con_id);
3869 return 0;
3870 }
3871
3872 /* Process flags */
3873 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
3874 status = gpiod_direction_output(desc,
Linus Walleijad177312016-11-13 23:02:44 +01003875 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
Benoit Parrotf625d462015-02-02 11:44:44 -06003876 else
3877 status = gpiod_direction_input(desc);
3878
3879 return status;
3880}
3881
Thierry Reding29a1f2332014-04-25 17:10:06 +02003882/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003883 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02003884 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003885 * @con_id: function within the GPIO consumer
3886 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003887 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003888 *
3889 * This variant of gpiod_get() allows to access GPIOs other than the first
3890 * defined one for functions that define several GPIOs.
3891 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003892 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
3893 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07003894 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003895 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003896struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003897 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003898 unsigned int idx,
3899 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003900{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003901 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003902 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003903 enum gpio_lookup_flags lookupflags = 0;
Linus Walleij7d18f0a2018-01-16 08:29:50 +01003904 /* Maybe we have a device name, maybe not */
3905 const char *devname = dev ? dev_name(dev) : "?";
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003906
3907 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
3908
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003909 if (dev) {
3910 /* Using device tree? */
3911 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
3912 dev_dbg(dev, "using device tree for GPIO lookup\n");
3913 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
3914 } else if (ACPI_COMPANION(dev)) {
3915 dev_dbg(dev, "using ACPI for GPIO lookup\n");
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +03003916 desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags);
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003917 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003918 }
3919
3920 /*
3921 * Either we are not using DT or ACPI, or their lookup did not return
3922 * a result. In that case, use platform lookup as a fallback.
3923 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003924 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04003925 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003926 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003927 }
3928
3929 if (IS_ERR(desc)) {
Wang Dongsheng9d5a1f22018-02-27 00:12:13 -08003930 dev_dbg(dev, "No GPIO consumer %s found\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003931 return desc;
3932 }
3933
Linus Walleij7d18f0a2018-01-16 08:29:50 +01003934 /*
3935 * If a connection label was passed use that, else attempt to use
3936 * the device name as label
3937 */
3938 status = gpiod_request(desc, con_id ? con_id : devname);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003939 if (status < 0)
3940 return ERR_PTR(status);
3941
Johan Hovold85b03b32016-07-03 18:32:05 +02003942 status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003943 if (status < 0) {
3944 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
3945 gpiod_put(desc);
3946 return ERR_PTR(status);
3947 }
3948
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003949 return desc;
3950}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003951EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003952
3953/**
Linus Walleij6392cca2017-12-29 02:07:54 +01003954 * gpiod_get_from_of_node() - obtain a GPIO from an OF node
3955 * @node: handle of the OF node
3956 * @propname: name of the DT property representing the GPIO
3957 * @index: index of the GPIO to obtain for the consumer
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003958 * @dflags: GPIO initialization flags
Thierry Reding950d55f52017-07-24 16:57:22 +02003959 * @label: label to attach to the requested GPIO
Mika Westerberg40b73182014-10-21 13:33:59 +02003960 *
Thierry Reding950d55f52017-07-24 16:57:22 +02003961 * Returns:
Andy Shevchenkoff213782017-02-28 17:03:12 +02003962 * On successful request the GPIO pin is configured in accordance with
Linus Walleij6392cca2017-12-29 02:07:54 +01003963 * provided @dflags. If the node does not have the requested GPIO
3964 * property, NULL is returned.
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003965 *
Mika Westerberg40b73182014-10-21 13:33:59 +02003966 * In case of error an ERR_PTR() is returned.
3967 */
Linus Walleij92542ed2017-12-29 22:52:02 +01003968struct gpio_desc *gpiod_get_from_of_node(struct device_node *node,
3969 const char *propname, int index,
3970 enum gpiod_flags dflags,
3971 const char *label)
Mika Westerberg40b73182014-10-21 13:33:59 +02003972{
Colin Ian King40a3c9d2018-01-16 11:56:11 +00003973 struct gpio_desc *desc;
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003974 unsigned long lflags = 0;
Linus Walleij6392cca2017-12-29 02:07:54 +01003975 enum of_gpio_flags flags;
Mika Westerberg40b73182014-10-21 13:33:59 +02003976 bool active_low = false;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003977 bool single_ended = false;
Laxman Dewangan4c0facd2017-04-06 19:05:52 +05303978 bool open_drain = false;
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303979 bool transitory = false;
Mika Westerberg40b73182014-10-21 13:33:59 +02003980 int ret;
3981
Linus Walleij6392cca2017-12-29 02:07:54 +01003982 desc = of_get_named_gpiod_flags(node, propname,
3983 index, &flags);
Mika Westerberg40b73182014-10-21 13:33:59 +02003984
Linus Walleij6392cca2017-12-29 02:07:54 +01003985 if (!desc || IS_ERR(desc)) {
3986 /* If it is not there, just return NULL */
3987 if (PTR_ERR(desc) == -ENOENT)
3988 return NULL;
3989 return desc;
Mika Westerberg40b73182014-10-21 13:33:59 +02003990 }
3991
Linus Walleij6392cca2017-12-29 02:07:54 +01003992 active_low = flags & OF_GPIO_ACTIVE_LOW;
3993 single_ended = flags & OF_GPIO_SINGLE_ENDED;
3994 open_drain = flags & OF_GPIO_OPEN_DRAIN;
3995 transitory = flags & OF_GPIO_TRANSITORY;
Mika Westerberg40b73182014-10-21 13:33:59 +02003996
Alexander Steinb2987d72017-01-12 17:39:24 +01003997 ret = gpiod_request(desc, label);
Johan Hovold85b03b32016-07-03 18:32:05 +02003998 if (ret)
3999 return ERR_PTR(ret);
4000
Mika Westerberg40b73182014-10-21 13:33:59 +02004001 if (active_low)
Andy Shevchenkoa264d102017-01-09 16:02:28 +02004002 lflags |= GPIO_ACTIVE_LOW;
Mika Westerberg40b73182014-10-21 13:33:59 +02004003
Laurent Pinchart90b665f2015-10-13 00:20:21 +03004004 if (single_ended) {
Laxman Dewangan4c0facd2017-04-06 19:05:52 +05304005 if (open_drain)
Andy Shevchenkoa264d102017-01-09 16:02:28 +02004006 lflags |= GPIO_OPEN_DRAIN;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03004007 else
Andy Shevchenkoa264d102017-01-09 16:02:28 +02004008 lflags |= GPIO_OPEN_SOURCE;
4009 }
4010
Andrew Jefferye10f72b2017-11-30 14:25:24 +10304011 if (transitory)
4012 lflags |= GPIO_TRANSITORY;
4013
Andy Shevchenkoa264d102017-01-09 16:02:28 +02004014 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
4015 if (ret < 0) {
4016 gpiod_put(desc);
4017 return ERR_PTR(ret);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03004018 }
4019
Mika Westerberg40b73182014-10-21 13:33:59 +02004020 return desc;
4021}
Linus Walleij92542ed2017-12-29 22:52:02 +01004022EXPORT_SYMBOL(gpiod_get_from_of_node);
Linus Walleij6392cca2017-12-29 02:07:54 +01004023
4024/**
Mika Westerberg40b73182014-10-21 13:33:59 +02004025 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
4026 * @fwnode: handle of the firmware node
4027 * @propname: name of the firmware property representing the GPIO
Linus Walleij6392cca2017-12-29 02:07:54 +01004028 * @index: index of the GPIO to obtain for the consumer
Mika Westerberg40b73182014-10-21 13:33:59 +02004029 * @dflags: GPIO initialization flags
4030 * @label: label to attach to the requested GPIO
4031 *
4032 * This function can be used for drivers that get their configuration
Linus Walleij6392cca2017-12-29 02:07:54 +01004033 * from opaque firmware.
Thierry Reding29a1f2332014-04-25 17:10:06 +02004034 *
Linus Walleij6392cca2017-12-29 02:07:54 +01004035 * The function properly finds the corresponding GPIO using whatever is the
Thierry Reding29a1f2332014-04-25 17:10:06 +02004036 * underlying firmware interface and then makes sure that the GPIO
4037 * descriptor is requested before it is returned to the caller.
4038 *
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004039 * Returns:
Thierry Reding29a1f2332014-04-25 17:10:06 +02004040 * On successful request the GPIO pin is configured in accordance with
4041 * provided @dflags.
4042 *
4043 * In case of error an ERR_PTR() is returned.
4044 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004045struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
Thierry Reding29a1f2332014-04-25 17:10:06 +02004046 const char *propname, int index,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09004047 enum gpiod_flags dflags,
4048 const char *label)
Thierry Reding29a1f2332014-04-25 17:10:06 +02004049{
4050 struct gpio_desc *desc = ERR_PTR(-ENODEV);
4051 unsigned long lflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07004052 int ret;
4053
David Brownelld2876d02008-02-04 22:28:20 -08004054 if (!fwnode)
David Brownelld2876d02008-02-04 22:28:20 -08004055 return ERR_PTR(-EINVAL);
4056
4057 if (is_of_node(fwnode)) {
Linus Walleij6392cca2017-12-29 02:07:54 +01004058 desc = gpiod_get_from_of_node(to_of_node(fwnode),
4059 propname, index,
4060 dflags,
4061 label);
4062 return desc;
David Brownelld2876d02008-02-04 22:28:20 -08004063 } else if (is_acpi_node(fwnode)) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09004064 struct acpi_gpio_info info;
David Brownelld2876d02008-02-04 22:28:20 -08004065
Linus Walleijd468bf92013-09-24 11:54:38 +02004066 desc = acpi_node_get_gpiod(fwnode, propname, index, &info);
Linus Walleij6392cca2017-12-29 02:07:54 +01004067 if (IS_ERR(desc))
4068 return desc;
4069
4070 acpi_gpio_update_gpiod_flags(&dflags, &info);
4071
4072 if (info.polarity == GPIO_ACTIVE_LOW)
4073 lflags |= GPIO_ACTIVE_LOW;
Thierry Redingf9c4a312012-04-12 13:26:01 +02004074 }
4075
Linus Walleij6392cca2017-12-29 02:07:54 +01004076 /* Currently only ACPI takes this path */
Thierry Redingf9c4a312012-04-12 13:26:01 +02004077 ret = gpiod_request(desc, label);
4078 if (ret)
4079 return ERR_PTR(ret);
Linus Walleijd468bf92013-09-24 11:54:38 +02004080
Thierry Redingf9c4a312012-04-12 13:26:01 +02004081 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
4082 if (ret < 0) {
4083 gpiod_put(desc);
4084 return ERR_PTR(ret);
4085 }
4086
4087 return desc;
Linus Walleijd468bf92013-09-24 11:54:38 +02004088}
4089EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
David Brownelld2876d02008-02-04 22:28:20 -08004090
4091/**
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07004092 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
David Brownelld8f388d82008-07-25 01:46:07 -07004093 * function
Linus Walleijd468bf92013-09-24 11:54:38 +02004094 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4095 * @con_id: function within the GPIO consumer
David Brownelld2876d02008-02-04 22:28:20 -08004096 * @index: index of the GPIO to obtain in the consumer
4097 * @flags: optional GPIO initialization flags
4098 *
4099 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
4100 * specified index was assigned to the requested function it will return NULL.
4101 * This is convenient for drivers that need to handle optional GPIOs.
Grant Likely362432a2013-02-09 09:41:49 +00004102 */
David Brownelld8f388d82008-07-25 01:46:07 -07004103struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09004104 const char *con_id,
Thierry Redingf9c4a312012-04-12 13:26:01 +02004105 unsigned int index,
4106 enum gpiod_flags flags)
4107{
Grant Likely362432a2013-02-09 09:41:49 +00004108 struct gpio_desc *desc;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09004109
Grant Likely362432a2013-02-09 09:41:49 +00004110 desc = gpiod_get_index(dev, con_id, index, flags);
4111 if (IS_ERR(desc)) {
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09004112 if (PTR_ERR(desc) == -ENOENT)
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01004113 return NULL;
David Brownelld2876d02008-02-04 22:28:20 -08004114 }
4115
Benoit Parrotf625d462015-02-02 11:44:44 -06004116 return desc;
4117}
4118EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
4119
4120/**
4121 * gpiod_hog - Hog the specified GPIO desc given the provided flags
4122 * @desc: gpio whose value will be assigned
4123 * @name: gpio line name
4124 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
4125 * of_get_gpio_hog()
4126 * @dflags: gpiod_flags - optional GPIO initialization flags
4127 */
4128int gpiod_hog(struct gpio_desc *desc, const char *name,
4129 unsigned long lflags, enum gpiod_flags dflags)
4130{
4131 struct gpio_chip *chip;
4132 struct gpio_desc *local_desc;
4133 int hwnum;
4134 int status;
4135
Laxman Dewanganc31a5712016-03-11 19:13:21 +05304136 chip = gpiod_to_chip(desc);
4137 hwnum = gpio_chip_hwgpio(desc);
4138
4139 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
Benoit Parrotf625d462015-02-02 11:44:44 -06004140 if (IS_ERR(local_desc)) {
4141 status = PTR_ERR(local_desc);
Johan Hovold85b03b32016-07-03 18:32:05 +02004142 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
Benoit Parrotf625d462015-02-02 11:44:44 -06004143 name, chip->label, hwnum, status);
Laxman Dewanganc31a5712016-03-11 19:13:21 +05304144 return status;
4145 }
Benoit Parrotf625d462015-02-02 11:44:44 -06004146
4147 status = gpiod_configure_flags(desc, name, lflags, dflags);
4148 if (status < 0) {
4149 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed, %d\n",
4150 name, chip->label, hwnum, status);
4151 gpiochip_free_own_desc(desc);
4152 return status;
4153 }
4154
4155 /* Mark GPIO as hogged so it can be identified and removed later */
4156 set_bit(FLAG_IS_HOGGED, &desc->flags);
4157
4158 pr_info("GPIO line %d (%s) hogged as %s%s\n",
4159 desc_to_gpio(desc), name,
4160 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
4161 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
4162 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
4163
4164 return 0;
4165}
4166
4167/**
4168 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
4169 * @chip: gpio chip to act on
4170 *
4171 * This is only used by of_gpiochip_remove to free hogged gpios
4172 */
Linus Walleij1c3cdb12016-02-09 13:51:59 +01004173static void gpiochip_free_hogs(struct gpio_chip *chip)
4174{
Benoit Parrotf625d462015-02-02 11:44:44 -06004175 int id;
4176
4177 for (id = 0; id < chip->ngpio; id++) {
4178 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004179 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
4180 }
4181}
4182
4183/**
4184 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
4185 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4186 * @con_id: function within the GPIO consumer
4187 * @flags: optional GPIO initialization flags
4188 *
4189 * This function acquires all the GPIOs defined under a given function.
4190 *
4191 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
4192 * no GPIO has been assigned to the requested function, or another IS_ERR()
4193 * code if an error occurred while trying to acquire the GPIOs.
4194 */
4195struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
4196 const char *con_id,
4197 enum gpiod_flags flags)
4198{
4199 struct gpio_desc *desc;
4200 struct gpio_descs *descs;
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004201 struct gpio_array *array_info = NULL;
4202 struct gpio_chip *chip;
4203 int count, bitmap_size;
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004204
4205 count = gpiod_count(dev, con_id);
4206 if (count < 0)
4207 return ERR_PTR(count);
4208
Kees Cookacafe7e2018-05-08 13:45:50 -07004209 descs = kzalloc(struct_size(descs, desc, count), GFP_KERNEL);
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004210 if (!descs)
4211 return ERR_PTR(-ENOMEM);
4212
4213 for (descs->ndescs = 0; descs->ndescs < count; ) {
4214 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
4215 if (IS_ERR(desc)) {
4216 gpiod_put_array(descs);
4217 return ERR_CAST(desc);
4218 }
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004219
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004220 descs->desc[descs->ndescs] = desc;
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004221
4222 chip = gpiod_to_chip(desc);
4223 /*
4224 * Select a chip of first array member
4225 * whose index matches its pin hardware number
4226 * as a candidate for fast bitmap processing.
4227 */
4228 if (!array_info && gpio_chip_hwgpio(desc) == descs->ndescs) {
4229 struct gpio_descs *array;
4230
4231 bitmap_size = BITS_TO_LONGS(chip->ngpio > count ?
4232 chip->ngpio : count);
4233
4234 array = kzalloc(struct_size(descs, desc, count) +
4235 struct_size(array_info, invert_mask,
4236 3 * bitmap_size), GFP_KERNEL);
4237 if (!array) {
4238 gpiod_put_array(descs);
4239 return ERR_PTR(-ENOMEM);
4240 }
4241
4242 memcpy(array, descs,
4243 struct_size(descs, desc, descs->ndescs + 1));
4244 kfree(descs);
4245
4246 descs = array;
4247 array_info = (void *)(descs->desc + count);
4248 array_info->get_mask = array_info->invert_mask +
4249 bitmap_size;
4250 array_info->set_mask = array_info->get_mask +
4251 bitmap_size;
4252
4253 array_info->desc = descs->desc;
4254 array_info->size = count;
4255 array_info->chip = chip;
4256 bitmap_set(array_info->get_mask, descs->ndescs,
4257 count - descs->ndescs);
4258 bitmap_set(array_info->set_mask, descs->ndescs,
4259 count - descs->ndescs);
4260 descs->info = array_info;
4261 }
4262 /*
4263 * Unmark members which don't qualify for fast bitmap
4264 * processing (different chip, not in hardware order)
4265 */
4266 if (array_info && (chip != array_info->chip ||
4267 gpio_chip_hwgpio(desc) != descs->ndescs)) {
4268 __clear_bit(descs->ndescs, array_info->get_mask);
4269 __clear_bit(descs->ndescs, array_info->set_mask);
4270 } else if (array_info) {
4271 /* Exclude open drain or open source from fast output */
4272 if (gpiochip_line_is_open_drain(chip, descs->ndescs) ||
4273 gpiochip_line_is_open_source(chip, descs->ndescs))
4274 __clear_bit(descs->ndescs,
4275 array_info->set_mask);
4276 /* Identify 'fast' pins which require invertion */
4277 if (gpiod_is_active_low(desc))
4278 __set_bit(descs->ndescs,
4279 array_info->invert_mask);
4280 }
4281
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004282 descs->ndescs++;
4283 }
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +02004284 if (array_info)
4285 dev_dbg(dev,
4286 "GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n",
4287 array_info->chip->label, array_info->size,
4288 *array_info->get_mask, *array_info->set_mask,
4289 *array_info->invert_mask);
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004290 return descs;
4291}
4292EXPORT_SYMBOL_GPL(gpiod_get_array);
4293
4294/**
4295 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
4296 * function
4297 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4298 * @con_id: function within the GPIO consumer
4299 * @flags: optional GPIO initialization flags
4300 *
4301 * This is equivalent to gpiod_get_array(), except that when no GPIO was
4302 * assigned to the requested function it will return NULL.
4303 */
4304struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
4305 const char *con_id,
4306 enum gpiod_flags flags)
4307{
4308 struct gpio_descs *descs;
4309
4310 descs = gpiod_get_array(dev, con_id, flags);
4311 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
4312 return NULL;
4313
4314 return descs;
4315}
4316EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
4317
4318/**
David Brownelld2876d02008-02-04 22:28:20 -08004319 * gpiod_put - dispose of a GPIO descriptor
4320 * @desc: GPIO descriptor to dispose of
4321 *
4322 * No descriptor can be used after gpiod_put() has been called on it.
4323 */
4324void gpiod_put(struct gpio_desc *desc)
4325{
4326 gpiod_free(desc);
4327}
4328EXPORT_SYMBOL_GPL(gpiod_put);
4329
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004330/**
4331 * gpiod_put_array - dispose of multiple GPIO descriptors
4332 * @descs: struct gpio_descs containing an array of descriptors
4333 */
4334void gpiod_put_array(struct gpio_descs *descs)
4335{
4336 unsigned int i;
4337
4338 for (i = 0; i < descs->ndescs; i++)
4339 gpiod_put(descs->desc[i]);
4340
4341 kfree(descs);
4342}
4343EXPORT_SYMBOL_GPL(gpiod_put_array);
4344
Linus Walleij3c702e92015-10-21 15:29:53 +02004345static int __init gpiolib_dev_init(void)
4346{
4347 int ret;
4348
4349 /* Register GPIO sysfs bus */
Andy Shevchenkob1911712018-07-03 03:39:03 +03004350 ret = bus_register(&gpio_bus_type);
Linus Walleij3c702e92015-10-21 15:29:53 +02004351 if (ret < 0) {
4352 pr_err("gpiolib: could not register GPIO bus type\n");
4353 return ret;
4354 }
4355
4356 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
4357 if (ret < 0) {
4358 pr_err("gpiolib: failed to allocate char dev region\n");
4359 bus_unregister(&gpio_bus_type);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07004360 } else {
4361 gpiolib_initialized = true;
4362 gpiochip_setup_devs();
Linus Walleij3c702e92015-10-21 15:29:53 +02004363 }
4364 return ret;
4365}
4366core_initcall(gpiolib_dev_init);
4367
David Brownelld2876d02008-02-04 22:28:20 -08004368#ifdef CONFIG_DEBUG_FS
4369
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004370static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
David Brownelld2876d02008-02-04 22:28:20 -08004371{
4372 unsigned i;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004373 struct gpio_chip *chip = gdev->chip;
4374 unsigned gpio = gdev->base;
4375 struct gpio_desc *gdesc = &gdev->descs[0];
David Brownelld2876d02008-02-04 22:28:20 -08004376 int is_out;
4377 int is_irq;
4378
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004379 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02004380 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
4381 if (gdesc->name) {
4382 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
4383 gpio, gdesc->name);
4384 }
David Brownelld2876d02008-02-04 22:28:20 -08004385 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02004386 }
David Brownelld2876d02008-02-04 22:28:20 -08004387
4388 gpiod_get_direction(gdesc);
4389 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
4390 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Markus Pargmannced433e2015-08-14 16:11:02 +02004391 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
4392 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08004393 is_out ? "out" : "in ",
Andy Shevchenko1c22a252018-07-12 20:36:42 +03004394 chip->get ? (chip->get(chip, i) ? "hi" : "lo") : "? ",
David Brownelld2876d02008-02-04 22:28:20 -08004395 is_irq ? "IRQ" : " ");
4396 seq_printf(s, "\n");
4397 }
4398}
4399
4400static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
4401{
4402 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02004403 struct gpio_device *gdev = NULL;
David Brownelld2876d02008-02-04 22:28:20 -08004404 loff_t index = *pos;
4405
4406 s->private = "";
4407
4408 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02004409 list_for_each_entry(gdev, &gpio_devices, list)
David Brownelld2876d02008-02-04 22:28:20 -08004410 if (index-- == 0) {
4411 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02004412 return gdev;
David Brownelld2876d02008-02-04 22:28:20 -08004413 }
4414 spin_unlock_irqrestore(&gpio_lock, flags);
4415
4416 return NULL;
4417}
4418
4419static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
4420{
4421 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02004422 struct gpio_device *gdev = v;
David Brownelld2876d02008-02-04 22:28:20 -08004423 void *ret = NULL;
4424
4425 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02004426 if (list_is_last(&gdev->list, &gpio_devices))
David Brownelld2876d02008-02-04 22:28:20 -08004427 ret = NULL;
4428 else
Linus Walleijff2b1352015-10-20 11:10:38 +02004429 ret = list_entry(gdev->list.next, struct gpio_device, list);
David Brownelld2876d02008-02-04 22:28:20 -08004430 spin_unlock_irqrestore(&gpio_lock, flags);
4431
4432 s->private = "\n";
4433 ++*pos;
4434
4435 return ret;
4436}
4437
4438static void gpiolib_seq_stop(struct seq_file *s, void *v)
4439{
4440}
4441
4442static int gpiolib_seq_show(struct seq_file *s, void *v)
4443{
Linus Walleijff2b1352015-10-20 11:10:38 +02004444 struct gpio_device *gdev = v;
4445 struct gpio_chip *chip = gdev->chip;
4446 struct device *parent;
David Brownelld2876d02008-02-04 22:28:20 -08004447
Linus Walleijff2b1352015-10-20 11:10:38 +02004448 if (!chip) {
4449 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
4450 dev_name(&gdev->dev));
4451 return 0;
4452 }
4453
4454 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
4455 dev_name(&gdev->dev),
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004456 gdev->base, gdev->base + gdev->ngpio - 1);
Linus Walleijff2b1352015-10-20 11:10:38 +02004457 parent = chip->parent;
4458 if (parent)
4459 seq_printf(s, ", parent: %s/%s",
4460 parent->bus ? parent->bus->name : "no-bus",
4461 dev_name(parent));
David Brownelld2876d02008-02-04 22:28:20 -08004462 if (chip->label)
4463 seq_printf(s, ", %s", chip->label);
4464 if (chip->can_sleep)
4465 seq_printf(s, ", can sleep");
4466 seq_printf(s, ":\n");
4467
4468 if (chip->dbg_show)
4469 chip->dbg_show(s, chip);
4470 else
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004471 gpiolib_dbg_show(s, gdev);
David Brownelld2876d02008-02-04 22:28:20 -08004472
4473 return 0;
4474}
4475
4476static const struct seq_operations gpiolib_seq_ops = {
4477 .start = gpiolib_seq_start,
4478 .next = gpiolib_seq_next,
4479 .stop = gpiolib_seq_stop,
4480 .show = gpiolib_seq_show,
4481};
4482
4483static int gpiolib_open(struct inode *inode, struct file *file)
4484{
4485 return seq_open(file, &gpiolib_seq_ops);
4486}
4487
4488static const struct file_operations gpiolib_operations = {
4489 .owner = THIS_MODULE,
4490 .open = gpiolib_open,
4491 .read = seq_read,
4492 .llseek = seq_lseek,
4493 .release = seq_release,
4494};
4495
4496static int __init gpiolib_debugfs_init(void)
4497{
4498 /* /sys/kernel/debug/gpio */
4499 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
4500 NULL, NULL, &gpiolib_operations);
4501 return 0;
4502}
4503subsys_initcall(gpiolib_debugfs_init);
4504
4505#endif /* DEBUG_FS */