blob: 43aeb07343ec76fe3eeef0283d8d52327530bfdf [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
David Brownelld2876d02008-02-04 22:28:20 -080064/* gpio_lock prevents conflicts during gpio_desc[] table updates.
65 * While any GPIO is requested, its gpio_chip is not removable;
66 * each GPIO's "requested" flag serves as a lock and refcount.
67 */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090068DEFINE_SPINLOCK(gpio_lock);
David Brownelld2876d02008-02-04 22:28:20 -080069
Alexandre Courbotbae48da2013-10-17 10:21:38 -070070static DEFINE_MUTEX(gpio_lookup_lock);
71static LIST_HEAD(gpio_lookup_list);
Linus Walleijff2b1352015-10-20 11:10:38 +020072LIST_HEAD(gpio_devices);
Johan Hovold6d867502015-05-04 17:23:25 +020073
74static void gpiochip_free_hogs(struct gpio_chip *chip);
Thierry Reding959bc7b2017-11-07 19:15:59 +010075static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
Andrew Lunn39c3fd52017-12-02 18:11:04 +010076 struct lock_class_key *lock_key,
77 struct lock_class_key *request_key);
Johan Hovold6d867502015-05-04 17:23:25 +020078static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
Mika Westerberg79b804c2016-09-20 15:15:21 +030079static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip);
80static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip);
Johan Hovold6d867502015-05-04 17:23:25 +020081
Guenter Roeck159f3cd2016-03-31 08:11:30 -070082static bool gpiolib_initialized;
Johan Hovold6d867502015-05-04 17:23:25 +020083
David Brownelld2876d02008-02-04 22:28:20 -080084static inline void desc_set_label(struct gpio_desc *d, const char *label)
85{
David Brownelld2876d02008-02-04 22:28:20 -080086 d->label = label;
David Brownelld2876d02008-02-04 22:28:20 -080087}
88
Alexandre Courbot372e7222013-02-03 01:29:29 +090089/**
Thierry Reding950d55f52017-07-24 16:57:22 +020090 * gpio_to_desc - Convert a GPIO number to its descriptor
91 * @gpio: global GPIO number
92 *
93 * Returns:
94 * The GPIO descriptor associated with the given GPIO, or %NULL if no GPIO
95 * with the given number exists in the system.
Alexandre Courbot372e7222013-02-03 01:29:29 +090096 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070097struct gpio_desc *gpio_to_desc(unsigned gpio)
Alexandre Courbot372e7222013-02-03 01:29:29 +090098{
Linus Walleijff2b1352015-10-20 11:10:38 +020099 struct gpio_device *gdev;
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900100 unsigned long flags;
101
102 spin_lock_irqsave(&gpio_lock, flags);
103
Linus Walleijff2b1352015-10-20 11:10:38 +0200104 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100105 if (gdev->base <= gpio &&
106 gdev->base + gdev->ngpio > gpio) {
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900107 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100108 return &gdev->descs[gpio - gdev->base];
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900109 }
110 }
111
112 spin_unlock_irqrestore(&gpio_lock, flags);
113
Alexandre Courbot0e9a5ed2014-12-02 23:15:05 +0900114 if (!gpio_is_valid(gpio))
115 WARN(1, "invalid GPIO %d\n", gpio);
116
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900117 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900118}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700119EXPORT_SYMBOL_GPL(gpio_to_desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900120
121/**
Thierry Reding950d55f52017-07-24 16:57:22 +0200122 * gpiochip_get_desc - get the GPIO descriptor corresponding to the given
123 * hardware number for this chip
124 * @chip: GPIO chip
125 * @hwnum: hardware number of the GPIO for this chip
126 *
127 * Returns:
128 * A pointer to the GPIO descriptor or %ERR_PTR(-EINVAL) if no GPIO exists
129 * in the given chip for the specified hardware number.
Linus Walleijd468bf92013-09-24 11:54:38 +0200130 */
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900131struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
132 u16 hwnum)
Linus Walleijd468bf92013-09-24 11:54:38 +0200133{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100134 struct gpio_device *gdev = chip->gpiodev;
135
136 if (hwnum >= gdev->ngpio)
Alexandre Courbotb7d0a282013-12-03 12:31:11 +0900137 return ERR_PTR(-EINVAL);
Linus Walleijd468bf92013-09-24 11:54:38 +0200138
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100139 return &gdev->descs[hwnum];
Linus Walleijd468bf92013-09-24 11:54:38 +0200140}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900141
142/**
Thierry Reding950d55f52017-07-24 16:57:22 +0200143 * desc_to_gpio - convert a GPIO descriptor to the integer namespace
144 * @desc: GPIO descriptor
145 *
Alexandre Courbot372e7222013-02-03 01:29:29 +0900146 * This should disappear in the future but is needed since we still
Thierry Reding950d55f52017-07-24 16:57:22 +0200147 * use GPIO numbers for error messages and sysfs nodes.
148 *
149 * Returns:
150 * The global GPIO number for the GPIO specified by its descriptor.
Alexandre Courbot372e7222013-02-03 01:29:29 +0900151 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700152int desc_to_gpio(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900153{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100154 return desc->gdev->base + (desc - &desc->gdev->descs[0]);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900155}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700156EXPORT_SYMBOL_GPL(desc_to_gpio);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900157
158
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700159/**
160 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
161 * @desc: descriptor to return the chip of
162 */
163struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900164{
Vladimir Zapolskiydd3b9a42017-12-21 18:37:30 +0200165 if (!desc || !desc->gdev)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100166 return NULL;
167 return desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900168}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700169EXPORT_SYMBOL_GPL(gpiod_to_chip);
David Brownelld2876d02008-02-04 22:28:20 -0800170
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700171/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
172static int gpiochip_find_base(int ngpio)
173{
Linus Walleijff2b1352015-10-20 11:10:38 +0200174 struct gpio_device *gdev;
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900175 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700176
Linus Walleijff2b1352015-10-20 11:10:38 +0200177 list_for_each_entry_reverse(gdev, &gpio_devices, list) {
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900178 /* found a free space? */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100179 if (gdev->base + gdev->ngpio <= base)
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900180 break;
181 else
182 /* nope, check the space right before the chip */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100183 base = gdev->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700184 }
185
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900186 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700187 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900188 return base;
189 } else {
190 pr_err("%s: cannot find free range\n", __func__);
191 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700192 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700193}
194
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700195/**
196 * gpiod_get_direction - return the current direction of a GPIO
197 * @desc: GPIO to get the direction of
198 *
Wolfram Sang94fc7302018-01-09 12:35:53 +0100199 * Returns 0 for output, 1 for input, or an error code in case of error.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700200 *
201 * This function may sleep if gpiod_cansleep() is true.
202 */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900203int gpiod_get_direction(struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300204{
205 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900206 unsigned offset;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300207 int status = -EINVAL;
208
Alexandre Courbot372e7222013-02-03 01:29:29 +0900209 chip = gpiod_to_chip(desc);
210 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300211
212 if (!chip->get_direction)
213 return status;
214
Alexandre Courbot372e7222013-02-03 01:29:29 +0900215 status = chip->get_direction(chip, offset);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300216 if (status > 0) {
217 /* GPIOF_DIR_IN, or other positive */
218 status = 1;
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900219 clear_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300220 }
221 if (status == 0) {
222 /* GPIOF_DIR_OUT */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900223 set_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300224 }
225 return status;
226}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700227EXPORT_SYMBOL_GPL(gpiod_get_direction);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300228
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900229/*
230 * Add a new chip to the global chips list, keeping the list of chips sorted
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800231 * by range(means [base, base + ngpio - 1]) order.
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900232 *
233 * Return -EBUSY if the new chip overlaps with some other chip's integer
234 * space.
235 */
Linus Walleijff2b1352015-10-20 11:10:38 +0200236static int gpiodev_add_to_list(struct gpio_device *gdev)
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900237{
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800238 struct gpio_device *prev, *next;
Linus Walleijff2b1352015-10-20 11:10:38 +0200239
240 if (list_empty(&gpio_devices)) {
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800241 /* initial entry in list */
Linus Walleijff2b1352015-10-20 11:10:38 +0200242 list_add_tail(&gdev->list, &gpio_devices);
Sudip Mukherjeee28ecca2015-12-27 19:06:50 +0530243 return 0;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900244 }
245
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800246 next = list_entry(gpio_devices.next, struct gpio_device, list);
247 if (gdev->base + gdev->ngpio <= next->base) {
248 /* add before first entry */
249 list_add(&gdev->list, &gpio_devices);
Julien Grossholtz96098df2016-01-07 16:46:45 -0500250 return 0;
251 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900252
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800253 prev = list_entry(gpio_devices.prev, struct gpio_device, list);
254 if (prev->base + prev->ngpio <= gdev->base) {
255 /* add behind last entry */
256 list_add_tail(&gdev->list, &gpio_devices);
257 return 0;
258 }
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800259
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800260 list_for_each_entry_safe(prev, next, &gpio_devices, list) {
261 /* at the end of the list */
262 if (&next->list == &gpio_devices)
263 break;
264
265 /* add between prev and next */
266 if (prev->base + prev->ngpio <= gdev->base
267 && gdev->base + gdev->ngpio <= next->base) {
268 list_add(&gdev->list, &prev->list);
269 return 0;
270 }
271 }
272
273 dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n");
274 return -EBUSY;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900275}
276
Thierry Reding950d55f52017-07-24 16:57:22 +0200277/*
Linus Walleijf881bab2015-09-23 16:20:43 -0700278 * Convert a GPIO name to its descriptor
279 */
280static struct gpio_desc *gpio_name_to_desc(const char * const name)
281{
Linus Walleijff2b1352015-10-20 11:10:38 +0200282 struct gpio_device *gdev;
Linus Walleijf881bab2015-09-23 16:20:43 -0700283 unsigned long flags;
284
285 spin_lock_irqsave(&gpio_lock, flags);
286
Linus Walleijff2b1352015-10-20 11:10:38 +0200287 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijf881bab2015-09-23 16:20:43 -0700288 int i;
289
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100290 for (i = 0; i != gdev->ngpio; ++i) {
291 struct gpio_desc *desc = &gdev->descs[i];
Linus Walleijf881bab2015-09-23 16:20:43 -0700292
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100293 if (!desc->name || !name)
Linus Walleijf881bab2015-09-23 16:20:43 -0700294 continue;
295
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100296 if (!strcmp(desc->name, name)) {
Linus Walleijf881bab2015-09-23 16:20:43 -0700297 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100298 return desc;
Linus Walleijf881bab2015-09-23 16:20:43 -0700299 }
300 }
301 }
302
303 spin_unlock_irqrestore(&gpio_lock, flags);
304
305 return NULL;
306}
307
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200308/*
309 * Takes the names from gc->names and checks if they are all unique. If they
310 * are, they are assigned to their gpio descriptors.
311 *
Bamvor Jian Zhanged379152015-11-14 16:43:20 +0800312 * Warning if one of the names is already used for a different GPIO.
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200313 */
314static int gpiochip_set_desc_names(struct gpio_chip *gc)
315{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100316 struct gpio_device *gdev = gc->gpiodev;
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200317 int i;
318
319 if (!gc->names)
320 return 0;
321
322 /* First check all names if they are unique */
323 for (i = 0; i != gc->ngpio; ++i) {
324 struct gpio_desc *gpio;
325
326 gpio = gpio_name_to_desc(gc->names[i]);
Linus Walleijf881bab2015-09-23 16:20:43 -0700327 if (gpio)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100328 dev_warn(&gdev->dev,
Linus Walleij34ffd852015-10-20 11:31:54 +0200329 "Detected name collision for GPIO name '%s'\n",
Linus Walleijf881bab2015-09-23 16:20:43 -0700330 gc->names[i]);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200331 }
332
333 /* Then add all names to the GPIO descriptors */
334 for (i = 0; i != gc->ngpio; ++i)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100335 gdev->descs[i].name = gc->names[i];
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200336
337 return 0;
338}
339
Stephen Boyde4371f6e2018-03-23 09:34:50 -0700340static unsigned long *gpiochip_allocate_mask(struct gpio_chip *chip)
341{
342 unsigned long *p;
343
Stephen Boydace569352018-03-23 09:34:51 -0700344 p = kmalloc_array(BITS_TO_LONGS(chip->ngpio), sizeof(*p), GFP_KERNEL);
Stephen Boyde4371f6e2018-03-23 09:34:50 -0700345 if (!p)
346 return NULL;
347
348 /* Assume by default all GPIOs are valid */
349 bitmap_fill(p, chip->ngpio);
350
351 return p;
352}
353
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700354static int gpiochip_init_valid_mask(struct gpio_chip *gpiochip)
355{
356#ifdef CONFIG_OF_GPIO
357 int size;
358 struct device_node *np = gpiochip->of_node;
359
360 size = of_property_count_u32_elems(np, "gpio-reserved-ranges");
361 if (size > 0 && size % 2 == 0)
362 gpiochip->need_valid_mask = true;
363#endif
364
365 if (!gpiochip->need_valid_mask)
366 return 0;
367
368 gpiochip->valid_mask = gpiochip_allocate_mask(gpiochip);
369 if (!gpiochip->valid_mask)
370 return -ENOMEM;
371
372 return 0;
373}
374
375static void gpiochip_free_valid_mask(struct gpio_chip *gpiochip)
376{
377 kfree(gpiochip->valid_mask);
378 gpiochip->valid_mask = NULL;
379}
380
381bool gpiochip_line_is_valid(const struct gpio_chip *gpiochip,
382 unsigned int offset)
383{
384 /* No mask means all valid */
385 if (likely(!gpiochip->valid_mask))
386 return true;
387 return test_bit(offset, gpiochip->valid_mask);
388}
389EXPORT_SYMBOL_GPL(gpiochip_line_is_valid);
390
Linus Walleijd7c51b42016-04-26 10:35:29 +0200391/*
392 * GPIO line handle management
393 */
394
395/**
396 * struct linehandle_state - contains the state of a userspace handle
397 * @gdev: the GPIO device the handle pertains to
398 * @label: consumer label used to tag descriptors
399 * @descs: the GPIO descriptors held by this handle
400 * @numdescs: the number of descriptors held in the descs array
401 */
402struct linehandle_state {
403 struct gpio_device *gdev;
404 const char *label;
405 struct gpio_desc *descs[GPIOHANDLES_MAX];
406 u32 numdescs;
407};
408
Lars-Peter Clausene3e847c2016-10-18 16:54:05 +0200409#define GPIOHANDLE_REQUEST_VALID_FLAGS \
410 (GPIOHANDLE_REQUEST_INPUT | \
411 GPIOHANDLE_REQUEST_OUTPUT | \
412 GPIOHANDLE_REQUEST_ACTIVE_LOW | \
413 GPIOHANDLE_REQUEST_OPEN_DRAIN | \
414 GPIOHANDLE_REQUEST_OPEN_SOURCE)
415
Linus Walleijd7c51b42016-04-26 10:35:29 +0200416static long linehandle_ioctl(struct file *filep, unsigned int cmd,
417 unsigned long arg)
418{
419 struct linehandle_state *lh = filep->private_data;
420 void __user *ip = (void __user *)arg;
421 struct gpiohandle_data ghd;
Lukas Wunnereec1d562017-10-12 12:40:10 +0200422 int vals[GPIOHANDLES_MAX];
Linus Walleijd7c51b42016-04-26 10:35:29 +0200423 int i;
424
425 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
Lukas Wunnereec1d562017-10-12 12:40:10 +0200426 /* TODO: check if descriptors are really input */
427 int ret = gpiod_get_array_value_complex(false,
428 true,
429 lh->numdescs,
430 lh->descs,
431 vals);
432 if (ret)
433 return ret;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200434
Lars-Peter Clausen3eded5d2016-10-18 16:54:02 +0200435 memset(&ghd, 0, sizeof(ghd));
Lukas Wunnereec1d562017-10-12 12:40:10 +0200436 for (i = 0; i < lh->numdescs; i++)
437 ghd.values[i] = vals[i];
Linus Walleijd7c51b42016-04-26 10:35:29 +0200438
439 if (copy_to_user(ip, &ghd, sizeof(ghd)))
440 return -EFAULT;
441
442 return 0;
443 } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
Linus Walleijd7c51b42016-04-26 10:35:29 +0200444 /* TODO: check if descriptors are really output */
445 if (copy_from_user(&ghd, ip, sizeof(ghd)))
446 return -EFAULT;
447
448 /* Clamp all values to [0,1] */
449 for (i = 0; i < lh->numdescs; i++)
450 vals[i] = !!ghd.values[i];
451
452 /* Reuse the array setting function */
453 gpiod_set_array_value_complex(false,
454 true,
455 lh->numdescs,
456 lh->descs,
457 vals);
458 return 0;
459 }
460 return -EINVAL;
461}
462
463#ifdef CONFIG_COMPAT
464static long linehandle_ioctl_compat(struct file *filep, unsigned int cmd,
465 unsigned long arg)
466{
467 return linehandle_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
468}
469#endif
470
471static int linehandle_release(struct inode *inode, struct file *filep)
472{
473 struct linehandle_state *lh = filep->private_data;
474 struct gpio_device *gdev = lh->gdev;
475 int i;
476
477 for (i = 0; i < lh->numdescs; i++)
478 gpiod_free(lh->descs[i]);
479 kfree(lh->label);
480 kfree(lh);
481 put_device(&gdev->dev);
482 return 0;
483}
484
485static const struct file_operations linehandle_fileops = {
486 .release = linehandle_release,
487 .owner = THIS_MODULE,
488 .llseek = noop_llseek,
489 .unlocked_ioctl = linehandle_ioctl,
490#ifdef CONFIG_COMPAT
491 .compat_ioctl = linehandle_ioctl_compat,
492#endif
493};
494
495static int linehandle_create(struct gpio_device *gdev, void __user *ip)
496{
497 struct gpiohandle_request handlereq;
498 struct linehandle_state *lh;
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200499 struct file *file;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200500 int fd, i, ret;
Bartosz Golaszewski418ee8e2017-10-16 11:32:29 +0200501 u32 lflags;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200502
503 if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
504 return -EFAULT;
505 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
506 return -EINVAL;
507
Bartosz Golaszewski418ee8e2017-10-16 11:32:29 +0200508 lflags = handlereq.flags;
509
510 /* Return an error if an unknown flag is set */
511 if (lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
512 return -EINVAL;
513
Bartosz Golaszewski588fc3b2017-11-15 16:47:43 +0100514 /*
515 * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
516 * the hardware actually supports enabling both at the same time the
517 * electrical result would be disastrous.
518 */
519 if ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) &&
520 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
521 return -EINVAL;
522
Bartosz Golaszewski609aaf62017-10-16 11:32:30 +0200523 /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
524 if (!(lflags & GPIOHANDLE_REQUEST_OUTPUT) &&
525 ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
526 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
527 return -EINVAL;
528
Linus Walleijd7c51b42016-04-26 10:35:29 +0200529 lh = kzalloc(sizeof(*lh), GFP_KERNEL);
530 if (!lh)
531 return -ENOMEM;
532 lh->gdev = gdev;
533 get_device(&gdev->dev);
534
535 /* Make sure this is terminated */
536 handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
537 if (strlen(handlereq.consumer_label)) {
538 lh->label = kstrdup(handlereq.consumer_label,
539 GFP_KERNEL);
540 if (!lh->label) {
541 ret = -ENOMEM;
542 goto out_free_lh;
543 }
544 }
545
546 /* Request each GPIO */
547 for (i = 0; i < handlereq.lines; i++) {
548 u32 offset = handlereq.lineoffsets[i];
Linus Walleijd7c51b42016-04-26 10:35:29 +0200549 struct gpio_desc *desc;
550
Lars-Peter Clausene405f9f2016-10-18 16:54:01 +0200551 if (offset >= gdev->ngpio) {
552 ret = -EINVAL;
553 goto out_free_descs;
554 }
555
Linus Walleijd7c51b42016-04-26 10:35:29 +0200556 desc = &gdev->descs[offset];
557 ret = gpiod_request(desc, lh->label);
558 if (ret)
559 goto out_free_descs;
560 lh->descs[i] = desc;
561
562 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
563 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
564 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
565 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
566 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
567 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
568
Andrew Jefferye10f72b2017-11-30 14:25:24 +1030569 ret = gpiod_set_transitory(desc, false);
570 if (ret < 0)
571 goto out_free_descs;
572
Linus Walleijd7c51b42016-04-26 10:35:29 +0200573 /*
574 * Lines have to be requested explicitly for input
575 * or output, else the line will be treated "as is".
576 */
577 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
578 int val = !!handlereq.default_values[i];
579
580 ret = gpiod_direction_output(desc, val);
581 if (ret)
582 goto out_free_descs;
583 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
584 ret = gpiod_direction_input(desc);
585 if (ret)
586 goto out_free_descs;
587 }
588 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
589 offset);
590 }
Linus Walleije2f608b2016-06-18 10:56:43 +0200591 /* Let i point at the last handle */
592 i--;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200593 lh->numdescs = handlereq.lines;
594
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200595 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200596 if (fd < 0) {
597 ret = fd;
598 goto out_free_descs;
599 }
600
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200601 file = anon_inode_getfile("gpio-linehandle",
602 &linehandle_fileops,
603 lh,
604 O_RDONLY | O_CLOEXEC);
605 if (IS_ERR(file)) {
606 ret = PTR_ERR(file);
607 goto out_put_unused_fd;
608 }
609
Linus Walleijd7c51b42016-04-26 10:35:29 +0200610 handlereq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200611 if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200612 /*
613 * fput() will trigger the release() callback, so do not go onto
614 * the regular error cleanup path here.
615 */
616 fput(file);
617 put_unused_fd(fd);
618 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +0200619 }
Linus Walleijd7c51b42016-04-26 10:35:29 +0200620
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200621 fd_install(fd, file);
622
Linus Walleijd7c51b42016-04-26 10:35:29 +0200623 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
624 lh->numdescs);
625
626 return 0;
627
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200628out_put_unused_fd:
629 put_unused_fd(fd);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200630out_free_descs:
631 for (; i >= 0; i--)
632 gpiod_free(lh->descs[i]);
633 kfree(lh->label);
634out_free_lh:
635 kfree(lh);
636 put_device(&gdev->dev);
637 return ret;
638}
639
Linus Walleij61f922d2016-06-02 11:30:15 +0200640/*
641 * GPIO line event management
642 */
643
644/**
645 * struct lineevent_state - contains the state of a userspace event
646 * @gdev: the GPIO device the event pertains to
647 * @label: consumer label used to tag descriptors
648 * @desc: the GPIO descriptor held by this event
649 * @eflags: the event flags this line was requested with
650 * @irq: the interrupt that trigger in response to events on this GPIO
651 * @wait: wait queue that handles blocking reads of events
652 * @events: KFIFO for the GPIO events
653 * @read_lock: mutex lock to protect reads from colliding with adding
654 * new events to the FIFO
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100655 * @timestamp: cache for the timestamp storing it between hardirq
656 * and IRQ thread, used to bring the timestamp close to the actual
657 * event
Linus Walleij61f922d2016-06-02 11:30:15 +0200658 */
659struct lineevent_state {
660 struct gpio_device *gdev;
661 const char *label;
662 struct gpio_desc *desc;
663 u32 eflags;
664 int irq;
665 wait_queue_head_t wait;
666 DECLARE_KFIFO(events, struct gpioevent_data, 16);
667 struct mutex read_lock;
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100668 u64 timestamp;
Linus Walleij61f922d2016-06-02 11:30:15 +0200669};
670
Lars-Peter Clausenac7dbb92016-10-18 16:54:06 +0200671#define GPIOEVENT_REQUEST_VALID_FLAGS \
672 (GPIOEVENT_REQUEST_RISING_EDGE | \
673 GPIOEVENT_REQUEST_FALLING_EDGE)
674
Al Viroafc9a422017-07-03 06:39:46 -0400675static __poll_t lineevent_poll(struct file *filep,
Linus Walleij61f922d2016-06-02 11:30:15 +0200676 struct poll_table_struct *wait)
677{
678 struct lineevent_state *le = filep->private_data;
Al Viroafc9a422017-07-03 06:39:46 -0400679 __poll_t events = 0;
Linus Walleij61f922d2016-06-02 11:30:15 +0200680
681 poll_wait(filep, &le->wait, wait);
682
683 if (!kfifo_is_empty(&le->events))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800684 events = EPOLLIN | EPOLLRDNORM;
Linus Walleij61f922d2016-06-02 11:30:15 +0200685
686 return events;
687}
688
689
690static ssize_t lineevent_read(struct file *filep,
691 char __user *buf,
692 size_t count,
693 loff_t *f_ps)
694{
695 struct lineevent_state *le = filep->private_data;
696 unsigned int copied;
697 int ret;
698
699 if (count < sizeof(struct gpioevent_data))
700 return -EINVAL;
701
702 do {
703 if (kfifo_is_empty(&le->events)) {
704 if (filep->f_flags & O_NONBLOCK)
705 return -EAGAIN;
706
707 ret = wait_event_interruptible(le->wait,
708 !kfifo_is_empty(&le->events));
709 if (ret)
710 return ret;
711 }
712
713 if (mutex_lock_interruptible(&le->read_lock))
714 return -ERESTARTSYS;
715 ret = kfifo_to_user(&le->events, buf, count, &copied);
716 mutex_unlock(&le->read_lock);
717
718 if (ret)
719 return ret;
720
721 /*
722 * If we couldn't read anything from the fifo (a different
723 * thread might have been faster) we either return -EAGAIN if
724 * the file descriptor is non-blocking, otherwise we go back to
725 * sleep and wait for more data to arrive.
726 */
727 if (copied == 0 && (filep->f_flags & O_NONBLOCK))
728 return -EAGAIN;
729
730 } while (copied == 0);
731
732 return copied;
733}
734
735static int lineevent_release(struct inode *inode, struct file *filep)
736{
737 struct lineevent_state *le = filep->private_data;
738 struct gpio_device *gdev = le->gdev;
739
740 free_irq(le->irq, le);
741 gpiod_free(le->desc);
742 kfree(le->label);
743 kfree(le);
744 put_device(&gdev->dev);
745 return 0;
746}
747
748static long lineevent_ioctl(struct file *filep, unsigned int cmd,
749 unsigned long arg)
750{
751 struct lineevent_state *le = filep->private_data;
752 void __user *ip = (void __user *)arg;
753 struct gpiohandle_data ghd;
754
755 /*
756 * We can get the value for an event line but not set it,
757 * because it is input by definition.
758 */
759 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
760 int val;
761
Lars-Peter Clausend82aa4a2016-10-18 16:54:04 +0200762 memset(&ghd, 0, sizeof(ghd));
763
Linus Walleij61f922d2016-06-02 11:30:15 +0200764 val = gpiod_get_value_cansleep(le->desc);
765 if (val < 0)
766 return val;
767 ghd.values[0] = val;
768
769 if (copy_to_user(ip, &ghd, sizeof(ghd)))
770 return -EFAULT;
771
772 return 0;
773 }
774 return -EINVAL;
775}
776
777#ifdef CONFIG_COMPAT
778static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd,
779 unsigned long arg)
780{
781 return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
782}
783#endif
784
785static const struct file_operations lineevent_fileops = {
786 .release = lineevent_release,
787 .read = lineevent_read,
788 .poll = lineevent_poll,
789 .owner = THIS_MODULE,
790 .llseek = noop_llseek,
791 .unlocked_ioctl = lineevent_ioctl,
792#ifdef CONFIG_COMPAT
793 .compat_ioctl = lineevent_ioctl_compat,
794#endif
795};
796
Ben Dooks33265b12016-06-17 16:03:13 +0100797static irqreturn_t lineevent_irq_thread(int irq, void *p)
Linus Walleij61f922d2016-06-02 11:30:15 +0200798{
799 struct lineevent_state *le = p;
800 struct gpioevent_data ge;
Bartosz Golaszewskidf1e76f2017-07-03 11:12:03 +0200801 int ret, level;
Linus Walleij61f922d2016-06-02 11:30:15 +0200802
Linus Walleij24bd3ef2018-01-22 13:19:28 +0100803 /* Do not leak kernel stack to userspace */
804 memset(&ge, 0, sizeof(ge));
805
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100806 ge.timestamp = le->timestamp;
Bartosz Golaszewskidf1e76f2017-07-03 11:12:03 +0200807 level = gpiod_get_value_cansleep(le->desc);
Linus Walleij61f922d2016-06-02 11:30:15 +0200808
Bartosz Golaszewskiad537b82017-06-23 13:45:16 +0200809 if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
810 && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200811 if (level)
812 /* Emit low-to-high event */
813 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
814 else
815 /* Emit high-to-low event */
816 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Bartosz Golaszewskidf1e76f2017-07-03 11:12:03 +0200817 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE && level) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200818 /* Emit low-to-high event */
819 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
Bartosz Golaszewskidf1e76f2017-07-03 11:12:03 +0200820 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE && !level) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200821 /* Emit high-to-low event */
822 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Arnd Bergmannbc0207a2016-06-16 11:02:41 +0200823 } else {
824 return IRQ_NONE;
Linus Walleij61f922d2016-06-02 11:30:15 +0200825 }
826
827 ret = kfifo_put(&le->events, ge);
828 if (ret != 0)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800829 wake_up_poll(&le->wait, EPOLLIN);
Linus Walleij61f922d2016-06-02 11:30:15 +0200830
831 return IRQ_HANDLED;
832}
833
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100834static irqreturn_t lineevent_irq_handler(int irq, void *p)
835{
836 struct lineevent_state *le = p;
837
838 /*
839 * Just store the timestamp in hardirq context so we get it as
840 * close in time as possible to the actual event.
841 */
842 le->timestamp = ktime_get_real_ns();
843
844 return IRQ_WAKE_THREAD;
845}
846
Linus Walleij61f922d2016-06-02 11:30:15 +0200847static int lineevent_create(struct gpio_device *gdev, void __user *ip)
848{
849 struct gpioevent_request eventreq;
850 struct lineevent_state *le;
851 struct gpio_desc *desc;
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200852 struct file *file;
Linus Walleij61f922d2016-06-02 11:30:15 +0200853 u32 offset;
854 u32 lflags;
855 u32 eflags;
856 int fd;
857 int ret;
858 int irqflags = 0;
859
860 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
861 return -EFAULT;
862
863 le = kzalloc(sizeof(*le), GFP_KERNEL);
864 if (!le)
865 return -ENOMEM;
866 le->gdev = gdev;
867 get_device(&gdev->dev);
868
869 /* Make sure this is terminated */
870 eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
871 if (strlen(eventreq.consumer_label)) {
872 le->label = kstrdup(eventreq.consumer_label,
873 GFP_KERNEL);
874 if (!le->label) {
875 ret = -ENOMEM;
876 goto out_free_le;
877 }
878 }
879
880 offset = eventreq.lineoffset;
881 lflags = eventreq.handleflags;
882 eflags = eventreq.eventflags;
883
Lars-Peter Clausenb8b0e3d2016-10-18 16:54:03 +0200884 if (offset >= gdev->ngpio) {
885 ret = -EINVAL;
886 goto out_free_label;
887 }
888
Lars-Peter Clausenac7dbb92016-10-18 16:54:06 +0200889 /* Return an error if a unknown flag is set */
890 if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
891 (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) {
892 ret = -EINVAL;
893 goto out_free_label;
894 }
895
Linus Walleij61f922d2016-06-02 11:30:15 +0200896 /* This is just wrong: we don't look for events on output lines */
897 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
898 ret = -EINVAL;
899 goto out_free_label;
900 }
901
902 desc = &gdev->descs[offset];
903 ret = gpiod_request(desc, le->label);
904 if (ret)
905 goto out_free_desc;
906 le->desc = desc;
907 le->eflags = eflags;
908
909 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
910 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
911 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
912 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
913 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
914 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
915
916 ret = gpiod_direction_input(desc);
917 if (ret)
918 goto out_free_desc;
919
920 le->irq = gpiod_to_irq(desc);
921 if (le->irq <= 0) {
922 ret = -ENODEV;
923 goto out_free_desc;
924 }
925
926 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
927 irqflags |= IRQF_TRIGGER_RISING;
928 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
929 irqflags |= IRQF_TRIGGER_FALLING;
930 irqflags |= IRQF_ONESHOT;
931 irqflags |= IRQF_SHARED;
932
933 INIT_KFIFO(le->events);
934 init_waitqueue_head(&le->wait);
935 mutex_init(&le->read_lock);
936
937 /* Request a thread to read the events */
938 ret = request_threaded_irq(le->irq,
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100939 lineevent_irq_handler,
Linus Walleij61f922d2016-06-02 11:30:15 +0200940 lineevent_irq_thread,
941 irqflags,
942 le->label,
943 le);
944 if (ret)
945 goto out_free_desc;
946
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200947 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleij61f922d2016-06-02 11:30:15 +0200948 if (fd < 0) {
949 ret = fd;
950 goto out_free_irq;
951 }
952
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200953 file = anon_inode_getfile("gpio-event",
954 &lineevent_fileops,
955 le,
956 O_RDONLY | O_CLOEXEC);
957 if (IS_ERR(file)) {
958 ret = PTR_ERR(file);
959 goto out_put_unused_fd;
960 }
961
Linus Walleij61f922d2016-06-02 11:30:15 +0200962 eventreq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200963 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200964 /*
965 * fput() will trigger the release() callback, so do not go onto
966 * the regular error cleanup path here.
967 */
968 fput(file);
969 put_unused_fd(fd);
970 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +0200971 }
Linus Walleij61f922d2016-06-02 11:30:15 +0200972
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200973 fd_install(fd, file);
974
Linus Walleij61f922d2016-06-02 11:30:15 +0200975 return 0;
976
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200977out_put_unused_fd:
978 put_unused_fd(fd);
Linus Walleij61f922d2016-06-02 11:30:15 +0200979out_free_irq:
980 free_irq(le->irq, le);
981out_free_desc:
982 gpiod_free(le->desc);
983out_free_label:
984 kfree(le->label);
985out_free_le:
986 kfree(le);
987 put_device(&gdev->dev);
988 return ret;
989}
990
Thierry Reding950d55f52017-07-24 16:57:22 +0200991/*
Linus Walleij3c702e92015-10-21 15:29:53 +0200992 * gpio_ioctl() - ioctl handler for the GPIO chardev
993 */
994static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
995{
996 struct gpio_device *gdev = filp->private_data;
997 struct gpio_chip *chip = gdev->chip;
Linus Walleij8b92e172016-05-27 14:24:04 +0200998 void __user *ip = (void __user *)arg;
Linus Walleij3c702e92015-10-21 15:29:53 +0200999
1000 /* We fail any subsequent ioctl():s when the chip is gone */
1001 if (!chip)
1002 return -ENODEV;
1003
Linus Walleij521a2ad2016-02-12 22:25:22 +01001004 /* Fill in the struct and pass to userspace */
Linus Walleij3c702e92015-10-21 15:29:53 +02001005 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
Linus Walleij521a2ad2016-02-12 22:25:22 +01001006 struct gpiochip_info chipinfo;
1007
Lars-Peter Clausen0f4bbb22016-10-18 16:54:00 +02001008 memset(&chipinfo, 0, sizeof(chipinfo));
1009
Linus Walleij3c702e92015-10-21 15:29:53 +02001010 strncpy(chipinfo.name, dev_name(&gdev->dev),
1011 sizeof(chipinfo.name));
1012 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
Linus Walleijdf4878e2016-02-12 14:48:23 +01001013 strncpy(chipinfo.label, gdev->label,
1014 sizeof(chipinfo.label));
1015 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001016 chipinfo.lines = gdev->ngpio;
Linus Walleij3c702e92015-10-21 15:29:53 +02001017 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
1018 return -EFAULT;
1019 return 0;
Linus Walleij521a2ad2016-02-12 22:25:22 +01001020 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
1021 struct gpioline_info lineinfo;
1022 struct gpio_desc *desc;
1023
1024 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
1025 return -EFAULT;
Lars-Peter Clausen1f1cc452016-10-18 16:53:59 +02001026 if (lineinfo.line_offset >= gdev->ngpio)
Linus Walleij521a2ad2016-02-12 22:25:22 +01001027 return -EINVAL;
1028
1029 desc = &gdev->descs[lineinfo.line_offset];
1030 if (desc->name) {
1031 strncpy(lineinfo.name, desc->name,
1032 sizeof(lineinfo.name));
1033 lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
1034 } else {
1035 lineinfo.name[0] = '\0';
1036 }
1037 if (desc->label) {
Linus Walleij214338e2016-02-25 21:01:48 +01001038 strncpy(lineinfo.consumer, desc->label,
1039 sizeof(lineinfo.consumer));
1040 lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +01001041 } else {
Linus Walleij214338e2016-02-25 21:01:48 +01001042 lineinfo.consumer[0] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +01001043 }
1044
1045 /*
1046 * Userspace only need to know that the kernel is using
1047 * this GPIO so it can't use it.
1048 */
1049 lineinfo.flags = 0;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001050 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
1051 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
1052 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
1053 test_bit(FLAG_EXPORT, &desc->flags) ||
1054 test_bit(FLAG_SYSFS, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001055 lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001056 if (test_bit(FLAG_IS_OUT, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001057 lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001058 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001059 lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001060 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001061 lineinfo.flags |= GPIOLINE_FLAG_OPEN_DRAIN;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001062 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001063 lineinfo.flags |= GPIOLINE_FLAG_OPEN_SOURCE;
1064
1065 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
1066 return -EFAULT;
1067 return 0;
Linus Walleijd7c51b42016-04-26 10:35:29 +02001068 } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
1069 return linehandle_create(gdev, ip);
Linus Walleij61f922d2016-06-02 11:30:15 +02001070 } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
1071 return lineevent_create(gdev, ip);
Linus Walleij3c702e92015-10-21 15:29:53 +02001072 }
1073 return -EINVAL;
1074}
1075
Linus Walleij8b92e172016-05-27 14:24:04 +02001076#ifdef CONFIG_COMPAT
1077static long gpio_ioctl_compat(struct file *filp, unsigned int cmd,
1078 unsigned long arg)
1079{
1080 return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
1081}
1082#endif
1083
Linus Walleij3c702e92015-10-21 15:29:53 +02001084/**
1085 * gpio_chrdev_open() - open the chardev for ioctl operations
1086 * @inode: inode for this chardev
1087 * @filp: file struct for storing private data
1088 * Returns 0 on success
1089 */
1090static int gpio_chrdev_open(struct inode *inode, struct file *filp)
1091{
1092 struct gpio_device *gdev = container_of(inode->i_cdev,
1093 struct gpio_device, chrdev);
1094
1095 /* Fail on open if the backing gpiochip is gone */
Stephen Boydfb505742017-01-09 11:47:44 -08001096 if (!gdev->chip)
Linus Walleij3c702e92015-10-21 15:29:53 +02001097 return -ENODEV;
1098 get_device(&gdev->dev);
1099 filp->private_data = gdev;
Lars-Peter Clausenf4e81c52016-11-30 13:05:21 +01001100
1101 return nonseekable_open(inode, filp);
Linus Walleij3c702e92015-10-21 15:29:53 +02001102}
1103
1104/**
1105 * gpio_chrdev_release() - close chardev after ioctl operations
1106 * @inode: inode for this chardev
1107 * @filp: file struct for storing private data
1108 * Returns 0 on success
1109 */
1110static int gpio_chrdev_release(struct inode *inode, struct file *filp)
1111{
1112 struct gpio_device *gdev = container_of(inode->i_cdev,
1113 struct gpio_device, chrdev);
1114
Linus Walleij3c702e92015-10-21 15:29:53 +02001115 put_device(&gdev->dev);
1116 return 0;
1117}
1118
1119
1120static const struct file_operations gpio_fileops = {
1121 .release = gpio_chrdev_release,
1122 .open = gpio_chrdev_open,
1123 .owner = THIS_MODULE,
Lars-Peter Clausenf4e81c52016-11-30 13:05:21 +01001124 .llseek = no_llseek,
Linus Walleij3c702e92015-10-21 15:29:53 +02001125 .unlocked_ioctl = gpio_ioctl,
Linus Walleij8b92e172016-05-27 14:24:04 +02001126#ifdef CONFIG_COMPAT
1127 .compat_ioctl = gpio_ioctl_compat,
1128#endif
Linus Walleij3c702e92015-10-21 15:29:53 +02001129};
1130
Linus Walleijff2b1352015-10-20 11:10:38 +02001131static void gpiodevice_release(struct device *dev)
1132{
1133 struct gpio_device *gdev = dev_get_drvdata(dev);
1134
1135 list_del(&gdev->list);
1136 ida_simple_remove(&gpio_ida, gdev->id);
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +01001137 kfree_const(gdev->label);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001138 kfree(gdev->descs);
Linus Walleij9efd9e62016-02-09 14:27:42 +01001139 kfree(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001140}
1141
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001142static int gpiochip_setup_dev(struct gpio_device *gdev)
1143{
1144 int status;
1145
1146 cdev_init(&gdev->chrdev, &gpio_fileops);
1147 gdev->chrdev.owner = THIS_MODULE;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001148 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001149
1150 status = cdev_device_add(&gdev->chrdev, &gdev->dev);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001151 if (status)
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001152 return status;
1153
1154 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
1155 MAJOR(gpio_devt), gdev->id);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001156
1157 status = gpiochip_sysfs_register(gdev);
1158 if (status)
1159 goto err_remove_device;
1160
1161 /* From this point, the .release() function cleans up gpio_device */
1162 gdev->dev.release = gpiodevice_release;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001163 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
1164 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
1165 dev_name(&gdev->dev), gdev->chip->label ? : "generic");
1166
1167 return 0;
1168
1169err_remove_device:
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001170 cdev_device_del(&gdev->chrdev, &gdev->dev);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001171 return status;
1172}
1173
1174static void gpiochip_setup_devs(void)
1175{
1176 struct gpio_device *gdev;
1177 int err;
1178
1179 list_for_each_entry(gdev, &gpio_devices, list) {
1180 err = gpiochip_setup_dev(gdev);
1181 if (err)
1182 pr_err("%s: Failed to initialize gpio device (%d)\n",
1183 dev_name(&gdev->dev), err);
1184 }
1185}
1186
Thierry Reding959bc7b2017-11-07 19:15:59 +01001187int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001188 struct lock_class_key *lock_key,
1189 struct lock_class_key *request_key)
David Brownelld2876d02008-02-04 22:28:20 -08001190{
1191 unsigned long flags;
1192 int status = 0;
Linus Walleijff2b1352015-10-20 11:10:38 +02001193 unsigned i;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001194 int base = chip->base;
Linus Walleijff2b1352015-10-20 11:10:38 +02001195 struct gpio_device *gdev;
David Brownelld2876d02008-02-04 22:28:20 -08001196
Linus Walleijff2b1352015-10-20 11:10:38 +02001197 /*
1198 * First: allocate and populate the internal stat container, and
1199 * set up the struct device.
1200 */
Josh Cartwright969f07b2016-02-17 16:44:15 -06001201 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
Linus Walleijff2b1352015-10-20 11:10:38 +02001202 if (!gdev)
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001203 return -ENOMEM;
Linus Walleij3c702e92015-10-21 15:29:53 +02001204 gdev->dev.bus = &gpio_bus_type;
Linus Walleijff2b1352015-10-20 11:10:38 +02001205 gdev->chip = chip;
1206 chip->gpiodev = gdev;
1207 if (chip->parent) {
1208 gdev->dev.parent = chip->parent;
1209 gdev->dev.of_node = chip->parent->of_node;
Thierry Redingacc6e332016-07-05 14:11:14 +02001210 }
1211
Linus Walleijff2b1352015-10-20 11:10:38 +02001212#ifdef CONFIG_OF_GPIO
1213 /* If the gpiochip has an assigned OF node this takes precedence */
Thierry Redingacc6e332016-07-05 14:11:14 +02001214 if (chip->of_node)
1215 gdev->dev.of_node = chip->of_node;
Linus Walleijff2b1352015-10-20 11:10:38 +02001216#endif
Thierry Redingacc6e332016-07-05 14:11:14 +02001217
Linus Walleijff2b1352015-10-20 11:10:38 +02001218 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
1219 if (gdev->id < 0) {
1220 status = gdev->id;
1221 goto err_free_gdev;
1222 }
1223 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
1224 device_initialize(&gdev->dev);
1225 dev_set_drvdata(&gdev->dev, gdev);
1226 if (chip->parent && chip->parent->driver)
1227 gdev->owner = chip->parent->driver->owner;
1228 else if (chip->owner)
1229 /* TODO: remove chip->owner */
1230 gdev->owner = chip->owner;
1231 else
1232 gdev->owner = THIS_MODULE;
David Brownelld2876d02008-02-04 22:28:20 -08001233
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001234 gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001235 if (!gdev->descs) {
Linus Walleijff2b1352015-10-20 11:10:38 +02001236 status = -ENOMEM;
1237 goto err_free_gdev;
1238 }
1239
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001240 if (chip->ngpio == 0) {
1241 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
Linus Walleijff2b1352015-10-20 11:10:38 +02001242 status = -EINVAL;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001243 goto err_free_descs;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001244 }
Linus Walleijdf4878e2016-02-12 14:48:23 +01001245
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +01001246 gdev->label = kstrdup_const(chip->label ?: "unknown", GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001247 if (!gdev->label) {
1248 status = -ENOMEM;
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001249 goto err_free_descs;
Linus Walleijdf4878e2016-02-12 14:48:23 +01001250 }
1251
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001252 gdev->ngpio = chip->ngpio;
Linus Walleij43c54ec2016-02-11 11:37:48 +01001253 gdev->data = data;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001254
David Brownelld2876d02008-02-04 22:28:20 -08001255 spin_lock_irqsave(&gpio_lock, flags);
1256
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001257 /*
1258 * TODO: this allocates a Linux GPIO number base in the global
1259 * GPIO numberspace for this chip. In the long run we want to
1260 * get *rid* of this numberspace and use only descriptors, but
1261 * it may be a pipe dream. It will not happen before we get rid
1262 * of the sysfs interface anyways.
1263 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001264 if (base < 0) {
1265 base = gpiochip_find_base(chip->ngpio);
1266 if (base < 0) {
1267 status = base;
Johan Hovold225fce82015-01-12 17:12:25 +01001268 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001269 goto err_free_label;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001270 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001271 /*
1272 * TODO: it should not be necessary to reflect the assigned
1273 * base outside of the GPIO subsystem. Go over drivers and
1274 * see if anyone makes use of this, else drop this and assign
1275 * a poison instead.
1276 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001277 chip->base = base;
1278 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001279 gdev->base = base;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001280
Linus Walleijff2b1352015-10-20 11:10:38 +02001281 status = gpiodev_add_to_list(gdev);
Johan Hovold05aa5202015-01-12 17:12:26 +01001282 if (status) {
1283 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001284 goto err_free_label;
Johan Hovold05aa5202015-01-12 17:12:26 +01001285 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001286
Linus Walleij545ebd92016-05-30 17:11:59 +02001287 spin_unlock_irqrestore(&gpio_lock, flags);
1288
Linus Walleijff2b1352015-10-20 11:10:38 +02001289 for (i = 0; i < chip->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001290 struct gpio_desc *desc = &gdev->descs[i];
David Brownelld8f388d82008-07-25 01:46:07 -07001291
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001292 desc->gdev = gdev;
Timur Tabi1ca2a922017-12-20 13:10:31 -06001293
1294 /* REVISIT: most hardware initializes GPIOs as inputs (often
1295 * with pullups enabled) so power usage is minimized. Linux
1296 * code should set the gpio direction first thing; but until
1297 * it does, and in case chip->get_direction is not set, we may
1298 * expose the wrong direction in sysfs.
Johan Hovold05aa5202015-01-12 17:12:26 +01001299 */
Timur Tabi1ca2a922017-12-20 13:10:31 -06001300 desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0;
David Brownelld2876d02008-02-04 22:28:20 -08001301 }
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001302
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301303#ifdef CONFIG_PINCTRL
Linus Walleij20ec3e32016-02-11 11:03:06 +01001304 INIT_LIST_HEAD(&gdev->pin_ranges);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301305#endif
1306
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001307 status = gpiochip_set_desc_names(chip);
1308 if (status)
1309 goto err_remove_from_list;
1310
Mika Westerberg79b804c2016-09-20 15:15:21 +03001311 status = gpiochip_irqchip_init_valid_mask(chip);
1312 if (status)
1313 goto err_remove_from_list;
1314
Stephen Boyd726cb3b2018-03-23 09:34:52 -07001315 status = gpiochip_init_valid_mask(chip);
1316 if (status)
1317 goto err_remove_irqchip_mask;
1318
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001319 status = gpiochip_add_irqchip(chip, lock_key, request_key);
Thierry Redinge0d89722017-11-07 19:15:54 +01001320 if (status)
1321 goto err_remove_chip;
1322
Tomeu Vizoso28355f82015-07-14 10:29:54 +02001323 status = of_gpiochip_add(chip);
1324 if (status)
1325 goto err_remove_chip;
1326
Mika Westerberg664e3e52014-01-08 12:40:54 +02001327 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001328
Linus Walleij3c702e92015-10-21 15:29:53 +02001329 /*
1330 * By first adding the chardev, and then adding the device,
1331 * we get a device node entry in sysfs under
1332 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1333 * coldplug of device nodes and other udev business.
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001334 * We can do this only if gpiolib has been initialized.
1335 * Otherwise, defer until later.
Linus Walleij3c702e92015-10-21 15:29:53 +02001336 */
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001337 if (gpiolib_initialized) {
1338 status = gpiochip_setup_dev(gdev);
1339 if (status)
1340 goto err_remove_chip;
1341 }
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001342 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001343
Johan Hovold225fce82015-01-12 17:12:25 +01001344err_remove_chip:
1345 acpi_gpiochip_remove(chip);
Johan Hovold6d867502015-05-04 17:23:25 +02001346 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +01001347 of_gpiochip_remove(chip);
Stephen Boyd726cb3b2018-03-23 09:34:52 -07001348 gpiochip_free_valid_mask(chip);
1349err_remove_irqchip_mask:
Mika Westerberg79b804c2016-09-20 15:15:21 +03001350 gpiochip_irqchip_free_valid_mask(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001351err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +01001352 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001353 list_del(&gdev->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001354 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001355err_free_label:
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +01001356 kfree_const(gdev->label);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001357err_free_descs:
1358 kfree(gdev->descs);
Linus Walleijff2b1352015-10-20 11:10:38 +02001359err_free_gdev:
1360 ida_simple_remove(&gpio_ida, gdev->id);
David Brownelld2876d02008-02-04 22:28:20 -08001361 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +02001362 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001363 gdev->base, gdev->base + gdev->ngpio - 1,
1364 chip->label ? : "generic");
1365 kfree(gdev);
David Brownelld2876d02008-02-04 22:28:20 -08001366 return status;
1367}
Thierry Reding959bc7b2017-11-07 19:15:59 +01001368EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);
David Brownelld2876d02008-02-04 22:28:20 -08001369
1370/**
Linus Walleij43c54ec2016-02-11 11:37:48 +01001371 * gpiochip_get_data() - get per-subdriver data for the chip
Thierry Reding950d55f52017-07-24 16:57:22 +02001372 * @chip: GPIO chip
1373 *
1374 * Returns:
1375 * The per-subdriver data for the chip.
Linus Walleij43c54ec2016-02-11 11:37:48 +01001376 */
1377void *gpiochip_get_data(struct gpio_chip *chip)
1378{
1379 return chip->gpiodev->data;
1380}
1381EXPORT_SYMBOL_GPL(gpiochip_get_data);
1382
1383/**
David Brownelld2876d02008-02-04 22:28:20 -08001384 * gpiochip_remove() - unregister a gpio_chip
1385 * @chip: the chip to unregister
1386 *
1387 * A gpio_chip with any GPIOs still requested may not be removed.
1388 */
abdoulaye berthee1db1702014-07-05 18:28:50 +02001389void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -08001390{
Linus Walleijff2b1352015-10-20 11:10:38 +02001391 struct gpio_device *gdev = chip->gpiodev;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001392 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001393 unsigned long flags;
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001394 unsigned i;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001395 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -08001396
Linus Walleijff2b1352015-10-20 11:10:38 +02001397 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
Linus Walleijafbc4f32016-02-09 13:21:06 +01001398 gpiochip_sysfs_unregister(gdev);
Geert Uytterhoeven5018ada2016-12-19 18:29:23 +01001399 gpiochip_free_hogs(chip);
Bamvor Jian Zhangbd203bd2016-02-20 13:13:19 +08001400 /* Numb the device, cancelling all outstanding operations */
1401 gdev->chip = NULL;
Johan Hovold00acc3d2015-01-12 17:12:27 +01001402 gpiochip_irqchip_remove(chip);
Mika Westerberg6072b9d2014-03-10 14:54:53 +02001403 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +01001404 gpiochip_remove_pin_ranges(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001405 of_gpiochip_remove(chip);
Stephen Boyd726cb3b2018-03-23 09:34:52 -07001406 gpiochip_free_valid_mask(chip);
Linus Walleij43c54ec2016-02-11 11:37:48 +01001407 /*
1408 * We accept no more calls into the driver from this point, so
1409 * NULL the driver data pointer
1410 */
1411 gdev->data = NULL;
Anton Vorontsov391c9702010-06-08 07:48:17 -06001412
Johan Hovold6798aca2015-01-12 17:12:28 +01001413 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001414 for (i = 0; i < gdev->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001415 desc = &gdev->descs[i];
Johan Hovoldfab28b82015-05-04 17:10:27 +02001416 if (test_bit(FLAG_REQUESTED, &desc->flags))
1417 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -08001418 }
David Brownelld2876d02008-02-04 22:28:20 -08001419 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001420
Johan Hovoldfab28b82015-05-04 17:10:27 +02001421 if (requested)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001422 dev_crit(&gdev->dev,
Linus Walleij58383c782015-11-04 09:56:26 +01001423 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +02001424
Linus Walleijff2b1352015-10-20 11:10:38 +02001425 /*
1426 * The gpiochip side puts its use of the device to rest here:
1427 * if there are no userspace clients, the chardev and device will
1428 * be removed, else it will be dangling until the last user is
1429 * gone.
1430 */
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001431 cdev_device_del(&gdev->chrdev, &gdev->dev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001432 put_device(&gdev->dev);
David Brownelld2876d02008-02-04 22:28:20 -08001433}
1434EXPORT_SYMBOL_GPL(gpiochip_remove);
1435
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301436static void devm_gpio_chip_release(struct device *dev, void *res)
1437{
1438 struct gpio_chip *chip = *(struct gpio_chip **)res;
1439
1440 gpiochip_remove(chip);
1441}
1442
1443static int devm_gpio_chip_match(struct device *dev, void *res, void *data)
1444
1445{
1446 struct gpio_chip **r = res;
1447
1448 if (!r || !*r) {
1449 WARN_ON(!r || !*r);
1450 return 0;
1451 }
1452
1453 return *r == data;
1454}
1455
1456/**
Jonathan Neuschäfer689fd022017-12-21 17:56:34 +01001457 * devm_gpiochip_add_data() - Resource manager gpiochip_add_data()
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301458 * @dev: the device pointer on which irq_chip belongs to.
1459 * @chip: the chip to register, with chip->base initialized
Thierry Reding950d55f52017-07-24 16:57:22 +02001460 * @data: driver-private data associated with this chip
1461 *
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301462 * Context: potentially before irqs will work
1463 *
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301464 * The gpio chip automatically be released when the device is unbound.
Thierry Reding950d55f52017-07-24 16:57:22 +02001465 *
1466 * Returns:
1467 * A negative errno if the chip can't be registered, such as because the
1468 * chip->base is invalid or already associated with a different chip.
1469 * Otherwise it returns zero as a success code.
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301470 */
1471int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
1472 void *data)
1473{
1474 struct gpio_chip **ptr;
1475 int ret;
1476
1477 ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
1478 GFP_KERNEL);
1479 if (!ptr)
1480 return -ENOMEM;
1481
1482 ret = gpiochip_add_data(chip, data);
1483 if (ret < 0) {
1484 devres_free(ptr);
1485 return ret;
1486 }
1487
1488 *ptr = chip;
1489 devres_add(dev, ptr);
1490
1491 return 0;
1492}
1493EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
1494
1495/**
1496 * devm_gpiochip_remove() - Resource manager of gpiochip_remove()
1497 * @dev: device for which which resource was allocated
1498 * @chip: the chip to remove
1499 *
1500 * A gpio_chip with any GPIOs still requested may not be removed.
1501 */
1502void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip)
1503{
1504 int ret;
1505
1506 ret = devres_release(dev, devm_gpio_chip_release,
1507 devm_gpio_chip_match, chip);
Christophe JAILLET3988d662017-01-27 12:56:42 +01001508 WARN_ON(ret);
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301509}
1510EXPORT_SYMBOL_GPL(devm_gpiochip_remove);
1511
Grant Likely594fa262010-06-08 07:48:16 -06001512/**
1513 * gpiochip_find() - iterator for locating a specific gpio_chip
1514 * @data: data to pass to match function
Thierry Reding950d55f52017-07-24 16:57:22 +02001515 * @match: Callback function to check gpio_chip
Grant Likely594fa262010-06-08 07:48:16 -06001516 *
1517 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1518 * determined by a user supplied @match callback. The callback should return
1519 * 0 if the device doesn't match and non-zero if it does. If the callback is
1520 * non-zero, this function will return to the caller and not iterate over any
1521 * more gpio_chips.
1522 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001523struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001524 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001525 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001526{
Linus Walleijff2b1352015-10-20 11:10:38 +02001527 struct gpio_device *gdev;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001528 struct gpio_chip *chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -06001529 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001530
1531 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001532 list_for_each_entry(gdev, &gpio_devices, list)
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001533 if (gdev->chip && match(gdev->chip, data)) {
1534 chip = gdev->chip;
Grant Likely594fa262010-06-08 07:48:16 -06001535 break;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001536 }
Linus Walleijff2b1352015-10-20 11:10:38 +02001537
Grant Likely594fa262010-06-08 07:48:16 -06001538 spin_unlock_irqrestore(&gpio_lock, flags);
1539
1540 return chip;
1541}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001542EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001543
Alexandre Courbot79697ef2013-11-16 21:39:32 +09001544static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1545{
1546 const char *name = data;
1547
1548 return !strcmp(chip->label, name);
1549}
1550
1551static struct gpio_chip *find_chip_by_name(const char *name)
1552{
1553 return gpiochip_find((void *)name, gpiochip_match_name);
1554}
1555
Linus Walleij14250522014-03-25 10:40:18 +01001556#ifdef CONFIG_GPIOLIB_IRQCHIP
1557
1558/*
1559 * The following is irqchip helper code for gpiochips.
1560 */
1561
Mika Westerberg79b804c2016-09-20 15:15:21 +03001562static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1563{
Thierry Redingdc7b0382017-11-07 19:15:52 +01001564 if (!gpiochip->irq.need_valid_mask)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001565 return 0;
1566
Stephen Boyde4371f6e2018-03-23 09:34:50 -07001567 gpiochip->irq.valid_mask = gpiochip_allocate_mask(gpiochip);
Thierry Redingdc7b0382017-11-07 19:15:52 +01001568 if (!gpiochip->irq.valid_mask)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001569 return -ENOMEM;
1570
Mika Westerberg79b804c2016-09-20 15:15:21 +03001571 return 0;
1572}
1573
1574static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1575{
Thierry Redingdc7b0382017-11-07 19:15:52 +01001576 kfree(gpiochip->irq.valid_mask);
1577 gpiochip->irq.valid_mask = NULL;
Mika Westerberg79b804c2016-09-20 15:15:21 +03001578}
1579
Stephen Boyd64ff2c82018-01-09 17:58:46 -08001580bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
1581 unsigned int offset)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001582{
Stephen Boyd726cb3b2018-03-23 09:34:52 -07001583 if (!gpiochip_line_is_valid(gpiochip, offset))
1584 return false;
Mika Westerberg79b804c2016-09-20 15:15:21 +03001585 /* No mask means all valid */
Thierry Redingdc7b0382017-11-07 19:15:52 +01001586 if (likely(!gpiochip->irq.valid_mask))
Mika Westerberg79b804c2016-09-20 15:15:21 +03001587 return true;
Thierry Redingdc7b0382017-11-07 19:15:52 +01001588 return test_bit(offset, gpiochip->irq.valid_mask);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001589}
Stephen Boyd64ff2c82018-01-09 17:58:46 -08001590EXPORT_SYMBOL_GPL(gpiochip_irqchip_irq_valid);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001591
Linus Walleij14250522014-03-25 10:40:18 +01001592/**
Linus Walleijd245b3f2016-11-24 10:57:25 +01001593 * gpiochip_set_cascaded_irqchip() - connects a cascaded irqchip to a gpiochip
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001594 * @gpiochip: the gpiochip to set the irqchip chain to
1595 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01001596 * @parent_irq: the irq number corresponding to the parent IRQ for this
1597 * chained irqchip
1598 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001599 * coming out of the gpiochip. If the interrupt is nested rather than
1600 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +01001601 */
Linus Walleijd245b3f2016-11-24 10:57:25 +01001602static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gpiochip,
1603 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001604 unsigned int parent_irq,
Linus Walleijd245b3f2016-11-24 10:57:25 +01001605 irq_flow_handler_t parent_handler)
Linus Walleij14250522014-03-25 10:40:18 +01001606{
Linus Walleij83141a72014-09-26 13:50:12 +02001607 unsigned int offset;
1608
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001609 if (!gpiochip->irq.domain) {
Linus Walleij83141a72014-09-26 13:50:12 +02001610 chip_err(gpiochip, "called %s before setting up irqchip\n",
1611 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +02001612 return;
1613 }
1614
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001615 if (parent_handler) {
1616 if (gpiochip->can_sleep) {
1617 chip_err(gpiochip,
1618 "you cannot have chained interrupts on a "
1619 "chip that may sleep\n");
1620 return;
1621 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001622 /*
1623 * The parent irqchip is already using the chip_data for this
1624 * irqchip, so our callbacks simply use the handler_data.
1625 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +02001626 irq_set_chained_handler_and_data(parent_irq, parent_handler,
1627 gpiochip);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001628
Thierry Reding39e5f092017-11-07 19:15:50 +01001629 gpiochip->irq.parents = &parent_irq;
1630 gpiochip->irq.num_parents = 1;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001631 }
Linus Walleij83141a72014-09-26 13:50:12 +02001632
1633 /* Set the parent IRQ for all affected IRQs */
Mika Westerberg79b804c2016-09-20 15:15:21 +03001634 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1635 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1636 continue;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001637 irq_set_parent(irq_find_mapping(gpiochip->irq.domain, offset),
Linus Walleij83141a72014-09-26 13:50:12 +02001638 parent_irq);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001639 }
Linus Walleij14250522014-03-25 10:40:18 +01001640}
Linus Walleijd245b3f2016-11-24 10:57:25 +01001641
1642/**
1643 * gpiochip_set_chained_irqchip() - connects a chained irqchip to a gpiochip
1644 * @gpiochip: the gpiochip to set the irqchip chain to
1645 * @irqchip: the irqchip to chain to the gpiochip
1646 * @parent_irq: the irq number corresponding to the parent IRQ for this
1647 * chained irqchip
1648 * @parent_handler: the parent interrupt handler for the accumulated IRQ
1649 * coming out of the gpiochip. If the interrupt is nested rather than
1650 * cascaded, pass NULL in this handler argument
1651 */
1652void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
1653 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001654 unsigned int parent_irq,
Linus Walleijd245b3f2016-11-24 10:57:25 +01001655 irq_flow_handler_t parent_handler)
1656{
Thierry Reding60ed54c2017-11-07 19:15:57 +01001657 if (gpiochip->irq.threaded) {
1658 chip_err(gpiochip, "tried to chain a threaded gpiochip\n");
1659 return;
1660 }
1661
Linus Walleijd245b3f2016-11-24 10:57:25 +01001662 gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
1663 parent_handler);
1664}
Linus Walleij14250522014-03-25 10:40:18 +01001665EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
1666
1667/**
Linus Walleijd245b3f2016-11-24 10:57:25 +01001668 * gpiochip_set_nested_irqchip() - connects a nested irqchip to a gpiochip
1669 * @gpiochip: the gpiochip to set the irqchip nested handler to
1670 * @irqchip: the irqchip to nest to the gpiochip
1671 * @parent_irq: the irq number corresponding to the parent IRQ for this
1672 * nested irqchip
1673 */
1674void gpiochip_set_nested_irqchip(struct gpio_chip *gpiochip,
1675 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001676 unsigned int parent_irq)
Linus Walleijd245b3f2016-11-24 10:57:25 +01001677{
Linus Walleijd245b3f2016-11-24 10:57:25 +01001678 gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
1679 NULL);
1680}
1681EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip);
1682
1683/**
Linus Walleij14250522014-03-25 10:40:18 +01001684 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1685 * @d: the irqdomain used by this irqchip
1686 * @irq: the global irq number used by this GPIO irqchip irq
1687 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1688 *
1689 * This function will set up the mapping for a certain IRQ line on a
1690 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1691 * stored inside the gpiochip.
1692 */
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001693int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1694 irq_hw_number_t hwirq)
Linus Walleij14250522014-03-25 10:40:18 +01001695{
1696 struct gpio_chip *chip = d->host_data;
Thierry Redinge0d89722017-11-07 19:15:54 +01001697 int err = 0;
Linus Walleij14250522014-03-25 10:40:18 +01001698
Grygorii Strashkodc749a02017-07-21 11:49:00 -05001699 if (!gpiochip_irqchip_irq_valid(chip, hwirq))
1700 return -ENXIO;
1701
Linus Walleij14250522014-03-25 10:40:18 +01001702 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001703 /*
1704 * This lock class tells lockdep that GPIO irqs are in a different
1705 * category than their parents, so it won't report false recursion.
1706 */
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001707 irq_set_lockdep_class(irq, chip->irq.lock_key, chip->irq.request_key);
Thierry Redingc7a0aa52017-11-07 19:15:48 +01001708 irq_set_chip_and_handler(irq, chip->irq.chip, chip->irq.handler);
Linus Walleijd245b3f2016-11-24 10:57:25 +01001709 /* Chips that use nested thread handlers have them marked */
Thierry Reding60ed54c2017-11-07 19:15:57 +01001710 if (chip->irq.threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02001711 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +01001712 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -05001713
Thierry Redinge0d89722017-11-07 19:15:54 +01001714 if (chip->irq.num_parents == 1)
1715 err = irq_set_parent(irq, chip->irq.parents[0]);
1716 else if (chip->irq.map)
1717 err = irq_set_parent(irq, chip->irq.map[hwirq]);
1718
1719 if (err < 0)
1720 return err;
1721
Linus Walleij1333b902014-04-23 16:45:12 +02001722 /*
1723 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1724 * is passed as default type.
1725 */
Thierry Reding3634eeb2017-11-07 19:15:49 +01001726 if (chip->irq.default_type != IRQ_TYPE_NONE)
1727 irq_set_irq_type(irq, chip->irq.default_type);
Linus Walleij14250522014-03-25 10:40:18 +01001728
1729 return 0;
1730}
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001731EXPORT_SYMBOL_GPL(gpiochip_irq_map);
Linus Walleij14250522014-03-25 10:40:18 +01001732
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001733void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
Linus Walleijc3626fd2014-03-28 20:42:01 +01001734{
Linus Walleij1c8732b2014-04-09 13:34:39 +02001735 struct gpio_chip *chip = d->host_data;
1736
Thierry Reding60ed54c2017-11-07 19:15:57 +01001737 if (chip->irq.threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02001738 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001739 irq_set_chip_and_handler(irq, NULL, NULL);
1740 irq_set_chip_data(irq, NULL);
1741}
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001742EXPORT_SYMBOL_GPL(gpiochip_irq_unmap);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001743
Linus Walleij14250522014-03-25 10:40:18 +01001744static const struct irq_domain_ops gpiochip_domain_ops = {
1745 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +01001746 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +01001747 /* Virtually all GPIO irqchips are twocell:ed */
1748 .xlate = irq_domain_xlate_twocell,
1749};
1750
1751static int gpiochip_irq_reqres(struct irq_data *d)
1752{
1753 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1754
Linus Walleijff2b1352015-10-20 11:10:38 +02001755 if (!try_module_get(chip->gpiodev->owner))
Grygorii Strashko5b76e792015-06-25 20:30:50 +03001756 return -ENODEV;
1757
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001758 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +01001759 chip_err(chip,
1760 "unable to lock HW IRQ %lu for IRQ\n",
1761 d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001762 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001763 return -EINVAL;
1764 }
1765 return 0;
1766}
1767
1768static void gpiochip_irq_relres(struct irq_data *d)
1769{
1770 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1771
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001772 gpiochip_unlock_as_irq(chip, d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001773 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001774}
1775
1776static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
1777{
Grygorii Strashkodc749a02017-07-21 11:49:00 -05001778 if (!gpiochip_irqchip_irq_valid(chip, offset))
1779 return -ENXIO;
Thierry Redinge0d89722017-11-07 19:15:54 +01001780
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001781 return irq_create_mapping(chip->irq.domain, offset);
Linus Walleij14250522014-03-25 10:40:18 +01001782}
1783
1784/**
Thierry Redinge0d89722017-11-07 19:15:54 +01001785 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
1786 * @gpiochip: the GPIO chip to add the IRQ chip to
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001787 * @lock_key: lockdep class for IRQ lock
1788 * @request_key: lockdep class for IRQ request
Thierry Redinge0d89722017-11-07 19:15:54 +01001789 */
Thierry Reding959bc7b2017-11-07 19:15:59 +01001790static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001791 struct lock_class_key *lock_key,
1792 struct lock_class_key *request_key)
Thierry Redinge0d89722017-11-07 19:15:54 +01001793{
1794 struct irq_chip *irqchip = gpiochip->irq.chip;
1795 const struct irq_domain_ops *ops;
1796 struct device_node *np;
1797 unsigned int type;
1798 unsigned int i;
1799
1800 if (!irqchip)
1801 return 0;
1802
1803 if (gpiochip->irq.parent_handler && gpiochip->can_sleep) {
1804 chip_err(gpiochip, "you cannot have chained interrupts on a "
1805 "chip that may sleep\n");
1806 return -EINVAL;
1807 }
1808
1809 np = gpiochip->gpiodev->dev.of_node;
1810 type = gpiochip->irq.default_type;
1811
1812 /*
1813 * Specifying a default trigger is a terrible idea if DT or ACPI is
1814 * used to configure the interrupts, as you may end up with
1815 * conflicting triggers. Tell the user, and reset to NONE.
1816 */
1817 if (WARN(np && type != IRQ_TYPE_NONE,
1818 "%s: Ignoring %u default trigger\n", np->full_name, type))
1819 type = IRQ_TYPE_NONE;
1820
1821 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
1822 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1823 "Ignoring %u default trigger\n", type);
1824 type = IRQ_TYPE_NONE;
1825 }
1826
1827 gpiochip->to_irq = gpiochip_to_irq;
1828 gpiochip->irq.default_type = type;
Thierry Reding959bc7b2017-11-07 19:15:59 +01001829 gpiochip->irq.lock_key = lock_key;
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001830 gpiochip->irq.request_key = request_key;
Thierry Redinge0d89722017-11-07 19:15:54 +01001831
1832 if (gpiochip->irq.domain_ops)
1833 ops = gpiochip->irq.domain_ops;
1834 else
1835 ops = &gpiochip_domain_ops;
1836
1837 gpiochip->irq.domain = irq_domain_add_simple(np, gpiochip->ngpio,
Thierry Reding8302cf52017-11-07 19:15:58 +01001838 gpiochip->irq.first,
1839 ops, gpiochip);
Thierry Redinge0d89722017-11-07 19:15:54 +01001840 if (!gpiochip->irq.domain)
1841 return -EINVAL;
1842
1843 /*
1844 * It is possible for a driver to override this, but only if the
1845 * alternative functions are both implemented.
1846 */
1847 if (!irqchip->irq_request_resources &&
1848 !irqchip->irq_release_resources) {
1849 irqchip->irq_request_resources = gpiochip_irq_reqres;
1850 irqchip->irq_release_resources = gpiochip_irq_relres;
1851 }
1852
1853 if (gpiochip->irq.parent_handler) {
1854 void *data = gpiochip->irq.parent_handler_data ?: gpiochip;
1855
1856 for (i = 0; i < gpiochip->irq.num_parents; i++) {
1857 /*
1858 * The parent IRQ chip is already using the chip_data
1859 * for this IRQ chip, so our callbacks simply use the
1860 * handler_data.
1861 */
1862 irq_set_chained_handler_and_data(gpiochip->irq.parents[i],
1863 gpiochip->irq.parent_handler,
1864 data);
1865 }
Thierry Redinge0d89722017-11-07 19:15:54 +01001866 }
1867
1868 acpi_gpiochip_request_interrupts(gpiochip);
1869
1870 return 0;
1871}
1872
1873/**
Linus Walleij14250522014-03-25 10:40:18 +01001874 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1875 * @gpiochip: the gpiochip to remove the irqchip from
1876 *
1877 * This is called only from gpiochip_remove()
1878 */
1879static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
1880{
Thierry Reding39e5f092017-11-07 19:15:50 +01001881 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +01001882
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001883 acpi_gpiochip_free_interrupts(gpiochip);
1884
Thierry Redinge0d89722017-11-07 19:15:54 +01001885 if (gpiochip->irq.chip && gpiochip->irq.parent_handler) {
Thierry Reding39e5f092017-11-07 19:15:50 +01001886 struct gpio_irq_chip *irq = &gpiochip->irq;
1887 unsigned int i;
1888
1889 for (i = 0; i < irq->num_parents; i++)
1890 irq_set_chained_handler_and_data(irq->parents[i],
1891 NULL, NULL);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001892 }
1893
Linus Walleijc3626fd2014-03-28 20:42:01 +01001894 /* Remove all IRQ mappings and delete the domain */
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001895 if (gpiochip->irq.domain) {
Thierry Reding39e5f092017-11-07 19:15:50 +01001896 unsigned int irq;
1897
Mika Westerberg79b804c2016-09-20 15:15:21 +03001898 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1899 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1900 continue;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001901
1902 irq = irq_find_mapping(gpiochip->irq.domain, offset);
1903 irq_dispose_mapping(irq);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001904 }
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001905
1906 irq_domain_remove(gpiochip->irq.domain);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001907 }
Linus Walleij14250522014-03-25 10:40:18 +01001908
Thierry Redingda80ff82017-11-07 19:15:46 +01001909 if (gpiochip->irq.chip) {
1910 gpiochip->irq.chip->irq_request_resources = NULL;
1911 gpiochip->irq.chip->irq_release_resources = NULL;
1912 gpiochip->irq.chip = NULL;
Linus Walleij14250522014-03-25 10:40:18 +01001913 }
Mika Westerberg79b804c2016-09-20 15:15:21 +03001914
1915 gpiochip_irqchip_free_valid_mask(gpiochip);
Linus Walleij14250522014-03-25 10:40:18 +01001916}
1917
1918/**
Linus Walleij739e6f52017-01-11 13:37:07 +01001919 * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01001920 * @gpiochip: the gpiochip to add the irqchip to
1921 * @irqchip: the irqchip to add to the gpiochip
1922 * @first_irq: if not dynamically assigned, the base (first) IRQ to
1923 * allocate gpiochip irqs from
1924 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +02001925 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
1926 * to have the core avoid setting up any default type in the hardware.
Thierry Reding60ed54c2017-11-07 19:15:57 +01001927 * @threaded: whether this irqchip uses a nested thread handler
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001928 * @lock_key: lockdep class for IRQ lock
1929 * @request_key: lockdep class for IRQ request
Linus Walleij14250522014-03-25 10:40:18 +01001930 *
1931 * This function closely associates a certain irqchip with a certain
1932 * gpiochip, providing an irq domain to translate the local IRQs to
1933 * global irqs in the gpiolib core, and making sure that the gpiochip
1934 * is passed as chip data to all related functions. Driver callbacks
Linus Walleij09dd5f92015-12-07 15:31:58 +01001935 * need to use gpiochip_get_data() to get their local state containers back
Linus Walleij14250522014-03-25 10:40:18 +01001936 * from the gpiochip passed as chip data. An irqdomain will be stored
1937 * in the gpiochip that shall be used by the driver to handle IRQ number
1938 * translation. The gpiochip will need to be initialized and registered
1939 * before calling this function.
1940 *
Linus Walleijc3626fd2014-03-28 20:42:01 +01001941 * This function will handle two cell:ed simple IRQs and assumes all
1942 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +01001943 * need to be open coded.
1944 */
Linus Walleij739e6f52017-01-11 13:37:07 +01001945int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
1946 struct irq_chip *irqchip,
1947 unsigned int first_irq,
1948 irq_flow_handler_t handler,
1949 unsigned int type,
Thierry Reding60ed54c2017-11-07 19:15:57 +01001950 bool threaded,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001951 struct lock_class_key *lock_key,
1952 struct lock_class_key *request_key)
Linus Walleij14250522014-03-25 10:40:18 +01001953{
1954 struct device_node *of_node;
Linus Walleij14250522014-03-25 10:40:18 +01001955
1956 if (!gpiochip || !irqchip)
1957 return -EINVAL;
1958
Linus Walleij58383c782015-11-04 09:56:26 +01001959 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +01001960 pr_err("missing gpiochip .dev parent pointer\n");
1961 return -EINVAL;
1962 }
Thierry Reding60ed54c2017-11-07 19:15:57 +01001963 gpiochip->irq.threaded = threaded;
Linus Walleij58383c782015-11-04 09:56:26 +01001964 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +01001965#ifdef CONFIG_OF_GPIO
1966 /*
Colin Cronin20a8a962015-05-18 11:41:43 -07001967 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08001968 * FIXME: get rid of this and use gpiochip->parent->of_node
1969 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +01001970 */
1971 if (gpiochip->of_node)
1972 of_node = gpiochip->of_node;
1973#endif
Marc Zyngier332e99d2016-09-07 09:12:11 +01001974 /*
Mika Westerberg0a1e0052016-09-12 14:29:51 +03001975 * Specifying a default trigger is a terrible idea if DT or ACPI is
Marc Zyngier332e99d2016-09-07 09:12:11 +01001976 * used to configure the interrupts, as you may end-up with
1977 * conflicting triggers. Tell the user, and reset to NONE.
1978 */
1979 if (WARN(of_node && type != IRQ_TYPE_NONE,
Rob Herring7eb6ce22017-07-18 16:43:03 -05001980 "%pOF: Ignoring %d default trigger\n", of_node, type))
Marc Zyngier332e99d2016-09-07 09:12:11 +01001981 type = IRQ_TYPE_NONE;
Mika Westerberg0a1e0052016-09-12 14:29:51 +03001982 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
1983 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1984 "Ignoring %d default trigger\n", type);
1985 type = IRQ_TYPE_NONE;
1986 }
Marc Zyngier332e99d2016-09-07 09:12:11 +01001987
Thierry Redingda80ff82017-11-07 19:15:46 +01001988 gpiochip->irq.chip = irqchip;
Thierry Redingc7a0aa52017-11-07 19:15:48 +01001989 gpiochip->irq.handler = handler;
Thierry Reding3634eeb2017-11-07 19:15:49 +01001990 gpiochip->irq.default_type = type;
Linus Walleij14250522014-03-25 10:40:18 +01001991 gpiochip->to_irq = gpiochip_to_irq;
Thierry Redingca9df052017-11-07 19:15:53 +01001992 gpiochip->irq.lock_key = lock_key;
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001993 gpiochip->irq.request_key = request_key;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001994 gpiochip->irq.domain = irq_domain_add_simple(of_node,
Linus Walleij14250522014-03-25 10:40:18 +01001995 gpiochip->ngpio, first_irq,
1996 &gpiochip_domain_ops, gpiochip);
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001997 if (!gpiochip->irq.domain) {
Thierry Redingda80ff82017-11-07 19:15:46 +01001998 gpiochip->irq.chip = NULL;
Linus Walleij14250522014-03-25 10:40:18 +01001999 return -EINVAL;
2000 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +02002001
2002 /*
2003 * It is possible for a driver to override this, but only if the
2004 * alternative functions are both implemented.
2005 */
2006 if (!irqchip->irq_request_resources &&
2007 !irqchip->irq_release_resources) {
2008 irqchip->irq_request_resources = gpiochip_irq_reqres;
2009 irqchip->irq_release_resources = gpiochip_irq_relres;
2010 }
Linus Walleij14250522014-03-25 10:40:18 +01002011
Mika Westerbergafa82fa2014-07-25 09:54:48 +03002012 acpi_gpiochip_request_interrupts(gpiochip);
2013
Linus Walleij14250522014-03-25 10:40:18 +01002014 return 0;
2015}
Linus Walleij739e6f52017-01-11 13:37:07 +01002016EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key);
Linus Walleij14250522014-03-25 10:40:18 +01002017
2018#else /* CONFIG_GPIOLIB_IRQCHIP */
2019
Thierry Reding959bc7b2017-11-07 19:15:59 +01002020static inline int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002021 struct lock_class_key *lock_key,
2022 struct lock_class_key *request_key)
Thierry Redinge0d89722017-11-07 19:15:54 +01002023{
2024 return 0;
2025}
2026
Linus Walleij14250522014-03-25 10:40:18 +01002027static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
Mika Westerberg79b804c2016-09-20 15:15:21 +03002028static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
2029{
2030 return 0;
2031}
2032static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
2033{ }
Linus Walleij14250522014-03-25 10:40:18 +01002034
2035#endif /* CONFIG_GPIOLIB_IRQCHIP */
2036
Jonas Gorskic771c2f2015-10-11 17:34:15 +02002037/**
2038 * gpiochip_generic_request() - request the gpio function for a pin
2039 * @chip: the gpiochip owning the GPIO
2040 * @offset: the offset of the GPIO to request for GPIO function
2041 */
2042int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
2043{
Linus Walleija9a1d2a2017-09-22 11:02:10 +02002044 return pinctrl_gpio_request(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02002045}
2046EXPORT_SYMBOL_GPL(gpiochip_generic_request);
2047
2048/**
2049 * gpiochip_generic_free() - free the gpio function from a pin
2050 * @chip: the gpiochip to request the gpio function for
2051 * @offset: the offset of the GPIO to free from GPIO function
2052 */
2053void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
2054{
Linus Walleija9a1d2a2017-09-22 11:02:10 +02002055 pinctrl_gpio_free(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02002056}
2057EXPORT_SYMBOL_GPL(gpiochip_generic_free);
2058
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002059/**
2060 * gpiochip_generic_config() - apply configuration for a pin
2061 * @chip: the gpiochip owning the GPIO
2062 * @offset: the offset of the GPIO to apply the configuration
2063 * @config: the configuration to be applied
2064 */
2065int gpiochip_generic_config(struct gpio_chip *chip, unsigned offset,
2066 unsigned long config)
2067{
2068 return pinctrl_gpio_set_config(chip->gpiodev->base + offset, config);
2069}
2070EXPORT_SYMBOL_GPL(gpiochip_generic_config);
2071
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302072#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01002073
Linus Walleij3f0f8672012-11-20 12:40:15 +01002074/**
Christian Ruppert586a87e2013-10-15 15:37:54 +02002075 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
2076 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +02002077 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +02002078 * @gpio_offset: the start offset in the current gpio_chip number space
2079 * @pin_group: name of the pin group inside the pin controller
2080 */
2081int gpiochip_add_pingroup_range(struct gpio_chip *chip,
2082 struct pinctrl_dev *pctldev,
2083 unsigned int gpio_offset, const char *pin_group)
2084{
2085 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002086 struct gpio_device *gdev = chip->gpiodev;
Christian Ruppert586a87e2013-10-15 15:37:54 +02002087 int ret;
2088
2089 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
2090 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002091 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +02002092 return -ENOMEM;
2093 }
2094
2095 /* Use local offset as range ID */
2096 pin_range->range.id = gpio_offset;
2097 pin_range->range.gc = chip;
2098 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002099 pin_range->range.base = gdev->base + gpio_offset;
Christian Ruppert586a87e2013-10-15 15:37:54 +02002100 pin_range->pctldev = pctldev;
2101
2102 ret = pinctrl_get_group_pins(pctldev, pin_group,
2103 &pin_range->range.pins,
2104 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +01002105 if (ret < 0) {
2106 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +02002107 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +01002108 }
Christian Ruppert586a87e2013-10-15 15:37:54 +02002109
2110 pinctrl_add_gpio_range(pctldev, &pin_range->range);
2111
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002112 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
2113 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +02002114 pinctrl_dev_get_devname(pctldev), pin_group);
2115
Linus Walleij20ec3e32016-02-11 11:03:06 +01002116 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Christian Ruppert586a87e2013-10-15 15:37:54 +02002117
2118 return 0;
2119}
2120EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
2121
2122/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01002123 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
2124 * @chip: the gpiochip to add the range for
Thierry Reding950d55f52017-07-24 16:57:22 +02002125 * @pinctl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01002126 * @gpio_offset: the start offset in the current gpio_chip number space
2127 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01002128 * @npins: the number of pins from the offset of each pin space (GPIO and
2129 * pin controller) to accumulate in this range
Thierry Reding950d55f52017-07-24 16:57:22 +02002130 *
2131 * Returns:
2132 * 0 on success, or a negative error-code on failure.
Linus Walleij3f0f8672012-11-20 12:40:15 +01002133 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002134int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01002135 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01002136 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302137{
2138 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002139 struct gpio_device *gdev = chip->gpiodev;
Axel Linb4d4b1f2012-11-21 14:33:56 +08002140 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302141
Linus Walleij3f0f8672012-11-20 12:40:15 +01002142 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302143 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002144 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002145 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302146 }
2147
Linus Walleij3f0f8672012-11-20 12:40:15 +01002148 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01002149 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01002150 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302151 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002152 pin_range->range.base = gdev->base + gpio_offset;
Linus Walleij316511c2012-11-21 08:48:09 +01002153 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302154 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01002155 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302156 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01002157 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08002158 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002159 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +01002160 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08002161 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01002162 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002163 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
2164 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +01002165 pinctl_name,
2166 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302167
Linus Walleij20ec3e32016-02-11 11:03:06 +01002168 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002169
2170 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302171}
Linus Walleij165adc92012-11-06 14:49:39 +01002172EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302173
Linus Walleij3f0f8672012-11-20 12:40:15 +01002174/**
2175 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
2176 * @chip: the chip to remove all the mappings for
2177 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302178void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
2179{
2180 struct gpio_pin_range *pin_range, *tmp;
Linus Walleij20ec3e32016-02-11 11:03:06 +01002181 struct gpio_device *gdev = chip->gpiodev;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302182
Linus Walleij20ec3e32016-02-11 11:03:06 +01002183 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302184 list_del(&pin_range->node);
2185 pinctrl_remove_gpio_range(pin_range->pctldev,
2186 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01002187 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302188 }
2189}
Linus Walleij165adc92012-11-06 14:49:39 +01002190EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
2191
2192#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302193
David Brownelld2876d02008-02-04 22:28:20 -08002194/* These "optional" allocation calls help prevent drivers from stomping
2195 * on each other, and help provide better diagnostics in debugfs.
2196 * They're called even less than the "set direction" calls.
2197 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002198static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08002199{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002200 struct gpio_chip *chip = desc->gdev->chip;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002201 int status;
David Brownelld2876d02008-02-04 22:28:20 -08002202 unsigned long flags;
2203
2204 spin_lock_irqsave(&gpio_lock, flags);
2205
David Brownelld2876d02008-02-04 22:28:20 -08002206 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07002207 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08002208 */
2209
2210 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
2211 desc_set_label(desc, label ? : "?");
2212 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07002213 } else {
David Brownelld2876d02008-02-04 22:28:20 -08002214 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -08002215 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07002216 }
2217
2218 if (chip->request) {
2219 /* chip->request may sleep */
2220 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002221 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07002222 spin_lock_irqsave(&gpio_lock, flags);
2223
2224 if (status < 0) {
2225 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -07002226 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03002227 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07002228 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07002229 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03002230 if (chip->get_direction) {
2231 /* chip->get_direction may sleep */
2232 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002233 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03002234 spin_lock_irqsave(&gpio_lock, flags);
2235 }
David Brownelld2876d02008-02-04 22:28:20 -08002236done:
Mika Westerberg77c2d792014-03-10 14:54:50 +02002237 spin_unlock_irqrestore(&gpio_lock, flags);
2238 return status;
2239}
2240
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002241/*
2242 * This descriptor validation needs to be inserted verbatim into each
2243 * function taking a descriptor, so we need to use a preprocessor
Linus Walleij54d77192016-05-30 16:48:39 +02002244 * macro to avoid endless duplication. If the desc is NULL it is an
2245 * optional GPIO and calls should just bail out.
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002246 */
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002247static int validate_desc(const struct gpio_desc *desc, const char *func)
2248{
2249 if (!desc)
2250 return 0;
2251 if (IS_ERR(desc)) {
2252 pr_warn("%s: invalid GPIO (errorpointer)\n", func);
2253 return PTR_ERR(desc);
2254 }
2255 if (!desc->gdev) {
2256 pr_warn("%s: invalid GPIO (no device)\n", func);
2257 return -EINVAL;
2258 }
2259 if (!desc->gdev->chip) {
2260 dev_warn(&desc->gdev->dev,
2261 "%s: backing chip is gone\n", func);
2262 return 0;
2263 }
2264 return 1;
2265}
2266
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002267#define VALIDATE_DESC(desc) do { \
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002268 int __valid = validate_desc(desc, __func__); \
2269 if (__valid <= 0) \
2270 return __valid; \
2271 } while (0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002272
2273#define VALIDATE_DESC_VOID(desc) do { \
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002274 int __valid = validate_desc(desc, __func__); \
2275 if (__valid <= 0) \
Linus Walleij54d77192016-05-30 16:48:39 +02002276 return; \
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002277 } while (0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002278
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002279int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002280{
2281 int status = -EPROBE_DEFER;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002282 struct gpio_device *gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002283
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002284 VALIDATE_DESC(desc);
2285 gdev = desc->gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002286
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002287 if (try_module_get(gdev->owner)) {
Linus Walleijfac9d882017-09-26 20:58:28 +02002288 status = gpiod_request_commit(desc, label);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002289 if (status < 0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002290 module_put(gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002291 else
2292 get_device(&gdev->dev);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002293 }
2294
David Brownelld2876d02008-02-04 22:28:20 -08002295 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +02002296 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002297
David Brownelld2876d02008-02-04 22:28:20 -08002298 return status;
2299}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002300
Linus Walleijfac9d882017-09-26 20:58:28 +02002301static bool gpiod_free_commit(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002302{
Mika Westerberg77c2d792014-03-10 14:54:50 +02002303 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08002304 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07002305 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002306
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07002307 might_sleep();
2308
Alexandre Courbot372e7222013-02-03 01:29:29 +09002309 gpiod_unexport(desc);
David Brownelld8f388d82008-07-25 01:46:07 -07002310
David Brownelld2876d02008-02-04 22:28:20 -08002311 spin_lock_irqsave(&gpio_lock, flags);
2312
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002313 chip = desc->gdev->chip;
David Brownell35e8bb52008-10-15 22:03:16 -07002314 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
2315 if (chip->free) {
2316 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07002317 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002318 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07002319 spin_lock_irqsave(&gpio_lock, flags);
2320 }
David Brownelld2876d02008-02-04 22:28:20 -08002321 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08002322 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07002323 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302324 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302325 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06002326 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002327 ret = true;
2328 }
David Brownelld2876d02008-02-04 22:28:20 -08002329
2330 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002331 return ret;
2332}
2333
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002334void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002335{
Linus Walleijfac9d882017-09-26 20:58:28 +02002336 if (desc && desc->gdev && gpiod_free_commit(desc)) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002337 module_put(desc->gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002338 put_device(&desc->gdev->dev);
2339 } else {
Mika Westerberg77c2d792014-03-10 14:54:50 +02002340 WARN_ON(extra_checks);
Linus Walleij33a68e82016-02-11 10:28:44 +01002341 }
David Brownelld2876d02008-02-04 22:28:20 -08002342}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002343
David Brownelld2876d02008-02-04 22:28:20 -08002344/**
2345 * gpiochip_is_requested - return string iff signal was requested
2346 * @chip: controller managing the signal
2347 * @offset: of signal within controller's 0..(ngpio - 1) range
2348 *
2349 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09002350 * The string returned is the label passed to gpio_request(); if none has been
2351 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08002352 *
2353 * This function is for use by GPIO controller drivers. The label can
2354 * help with diagnostics, and knowing that the signal is used as a GPIO
2355 * can help avoid accidentally multiplexing it to another controller.
2356 */
2357const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
2358{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002359 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08002360
Dirk Behme48b59532015-08-18 18:02:32 +02002361 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08002362 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002363
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002364 desc = &chip->gpiodev->descs[offset];
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002365
Alexandre Courbot372e7222013-02-03 01:29:29 +09002366 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08002367 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002368 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08002369}
2370EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2371
Mika Westerberg77c2d792014-03-10 14:54:50 +02002372/**
2373 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
Thierry Reding950d55f52017-07-24 16:57:22 +02002374 * @chip: GPIO chip
2375 * @hwnum: hardware number of the GPIO for which to request the descriptor
Mika Westerberg77c2d792014-03-10 14:54:50 +02002376 * @label: label for the GPIO
2377 *
2378 * Function allows GPIO chip drivers to request and use their own GPIO
2379 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2380 * function will not increase reference count of the GPIO chip module. This
2381 * allows the GPIO chip module to be unloaded as needed (we assume that the
2382 * GPIO chip driver handles freeing the GPIOs it has requested).
Thierry Reding950d55f52017-07-24 16:57:22 +02002383 *
2384 * Returns:
2385 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2386 * code on failure.
Mika Westerberg77c2d792014-03-10 14:54:50 +02002387 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002388struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
2389 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002390{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002391 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
2392 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002393
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002394 if (IS_ERR(desc)) {
2395 chip_err(chip, "failed to get GPIO descriptor\n");
2396 return desc;
2397 }
2398
Linus Walleijfac9d882017-09-26 20:58:28 +02002399 err = gpiod_request_commit(desc, label);
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002400 if (err < 0)
2401 return ERR_PTR(err);
2402
2403 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002404}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002405EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002406
2407/**
2408 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2409 * @desc: GPIO descriptor to free
2410 *
2411 * Function frees the given GPIO requested previously with
2412 * gpiochip_request_own_desc().
2413 */
2414void gpiochip_free_own_desc(struct gpio_desc *desc)
2415{
2416 if (desc)
Linus Walleijfac9d882017-09-26 20:58:28 +02002417 gpiod_free_commit(desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002418}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002419EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08002420
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002421/*
2422 * Drivers MUST set GPIO direction before making get/set calls. In
David Brownelld2876d02008-02-04 22:28:20 -08002423 * some cases this is done in early boot, before IRQs are enabled.
2424 *
2425 * As a rule these aren't called more than once (except for drivers
2426 * using the open-drain emulation idiom) so these are natural places
2427 * to accumulate extra debugging checks. Note that we can't (yet)
2428 * rely on gpio_request() having been called beforehand.
2429 */
2430
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002431/**
2432 * gpiod_direction_input - set the GPIO direction to input
2433 * @desc: GPIO to set to input
2434 *
2435 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2436 * be called safely on it.
2437 *
2438 * Return 0 in case of success, else an error code.
2439 */
2440int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002441{
David Brownelld2876d02008-02-04 22:28:20 -08002442 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002443 int status = -EINVAL;
2444
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002445 VALIDATE_DESC(desc);
2446 chip = desc->gdev->chip;
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002447
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002448 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01002449 gpiod_warn(desc,
2450 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02002451 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002452 return -EIO;
2453 }
2454
Alexandre Courbotd82da792014-07-22 16:17:43 +09002455 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -08002456 if (status == 0)
2457 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002458
Alexandre Courbot372e7222013-02-03 01:29:29 +09002459 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09002460
David Brownelld2876d02008-02-04 22:28:20 -08002461 return status;
2462}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002463EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002464
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002465static int gpio_set_drive_single_ended(struct gpio_chip *gc, unsigned offset,
2466 enum pin_config_param mode)
2467{
2468 unsigned long config = { PIN_CONF_PACKED(mode, 0) };
2469
2470 return gc->set_config ? gc->set_config(gc, offset, config) : -ENOTSUPP;
2471}
2472
Linus Walleijfac9d882017-09-26 20:58:28 +02002473static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08002474{
Linus Walleijc663e5f2016-03-22 10:51:16 +01002475 struct gpio_chip *gc = desc->gdev->chip;
Linus Walleijad177312016-11-13 23:02:44 +01002476 int val = !!value;
Linus Walleijc663e5f2016-03-22 10:51:16 +01002477 int ret;
David Brownelld2876d02008-02-04 22:28:20 -08002478
Linus Walleijc663e5f2016-03-22 10:51:16 +01002479 if (!gc->set || !gc->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01002480 gpiod_warn(desc,
2481 "%s: missing set() or direction_output() operations\n",
2482 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002483 return -EIO;
2484 }
2485
Linus Walleijad177312016-11-13 23:02:44 +01002486 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
Linus Walleijc663e5f2016-03-22 10:51:16 +01002487 if (!ret)
David Brownelld2876d02008-02-04 22:28:20 -08002488 set_bit(FLAG_IS_OUT, &desc->flags);
Linus Walleijad177312016-11-13 23:02:44 +01002489 trace_gpio_value(desc_to_gpio(desc), 0, val);
Linus Walleijc663e5f2016-03-22 10:51:16 +01002490 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2491 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08002492}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002493
2494/**
2495 * gpiod_direction_output_raw - set the GPIO direction to output
2496 * @desc: GPIO to set to output
2497 * @value: initial output value of the GPIO
2498 *
2499 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2500 * be called safely on it. The initial value of the output must be specified
2501 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2502 *
2503 * Return 0 in case of success, else an error code.
2504 */
2505int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2506{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002507 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02002508 return gpiod_direction_output_raw_commit(desc, value);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002509}
2510EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2511
2512/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05302513 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002514 * @desc: GPIO to set to output
2515 * @value: initial output value of the GPIO
2516 *
2517 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2518 * be called safely on it. The initial value of the output must be specified
2519 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2520 * account.
2521 *
2522 * Return 0 in case of success, else an error code.
2523 */
2524int gpiod_direction_output(struct gpio_desc *desc, int value)
2525{
Vladimir Zapolskiy30322bc2017-12-21 18:37:24 +02002526 struct gpio_chip *gc;
Linus Walleij02e47982017-09-26 21:20:23 +02002527 int ret;
2528
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002529 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002530 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2531 value = !value;
Linus Walleijad177312016-11-13 23:02:44 +01002532 else
2533 value = !!value;
Linus Walleij02e47982017-09-26 21:20:23 +02002534
2535 /* GPIOs used for IRQs shall not be set as output */
2536 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
2537 gpiod_err(desc,
2538 "%s: tried to set a GPIO tied to an IRQ as output\n",
2539 __func__);
2540 return -EIO;
2541 }
2542
Vladimir Zapolskiy30322bc2017-12-21 18:37:24 +02002543 gc = desc->gdev->chip;
Linus Walleij02e47982017-09-26 21:20:23 +02002544 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2545 /* First see if we can enable open drain in hardware */
2546 ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2547 PIN_CONFIG_DRIVE_OPEN_DRAIN);
2548 if (!ret)
2549 goto set_output_value;
2550 /* Emulate open drain by not actively driving the line high */
2551 if (value)
2552 return gpiod_direction_input(desc);
2553 }
2554 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2555 ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2556 PIN_CONFIG_DRIVE_OPEN_SOURCE);
2557 if (!ret)
2558 goto set_output_value;
2559 /* Emulate open source by not actively driving the line low */
2560 if (!value)
2561 return gpiod_direction_input(desc);
2562 } else {
2563 gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2564 PIN_CONFIG_DRIVE_PUSH_PULL);
2565 }
2566
2567set_output_value:
Linus Walleijfac9d882017-09-26 20:58:28 +02002568 return gpiod_direction_output_raw_commit(desc, value);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002569}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002570EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08002571
Felipe Balbic4b5be92010-05-26 14:42:23 -07002572/**
Thierry Reding950d55f52017-07-24 16:57:22 +02002573 * gpiod_set_debounce - sets @debounce time for a GPIO
2574 * @desc: descriptor of the GPIO for which to set debounce time
2575 * @debounce: debounce time in microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02002576 *
Thierry Reding950d55f52017-07-24 16:57:22 +02002577 * Returns:
2578 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2579 * debounce time.
Felipe Balbic4b5be92010-05-26 14:42:23 -07002580 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002581int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07002582{
Felipe Balbic4b5be92010-05-26 14:42:23 -07002583 struct gpio_chip *chip;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002584 unsigned long config;
Felipe Balbic4b5be92010-05-26 14:42:23 -07002585
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002586 VALIDATE_DESC(desc);
2587 chip = desc->gdev->chip;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002588 if (!chip->set || !chip->set_config) {
Mark Brown6424de52013-09-09 10:33:49 +01002589 gpiod_dbg(desc,
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002590 "%s: missing set() or set_config() operations\n",
Mark Brown6424de52013-09-09 10:33:49 +01002591 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02002592 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002593 }
2594
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002595 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
2596 return chip->set_config(chip, gpio_chip_hwgpio(desc), config);
Felipe Balbic4b5be92010-05-26 14:42:23 -07002597}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002598EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002599
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002600/**
Andrew Jefferye10f72b2017-11-30 14:25:24 +10302601 * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
2602 * @desc: descriptor of the GPIO for which to configure persistence
2603 * @transitory: True to lose state on suspend or reset, false for persistence
2604 *
2605 * Returns:
2606 * 0 on success, otherwise a negative error code.
2607 */
2608int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
2609{
2610 struct gpio_chip *chip;
2611 unsigned long packed;
2612 int gpio;
2613 int rc;
2614
Vladimir Zapolskiy156dd392017-12-21 18:37:35 +02002615 VALIDATE_DESC(desc);
Andrew Jefferye10f72b2017-11-30 14:25:24 +10302616 /*
2617 * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
2618 * persistence state.
2619 */
2620 if (transitory)
2621 set_bit(FLAG_TRANSITORY, &desc->flags);
2622 else
2623 clear_bit(FLAG_TRANSITORY, &desc->flags);
2624
2625 /* If the driver supports it, set the persistence state now */
2626 chip = desc->gdev->chip;
2627 if (!chip->set_config)
2628 return 0;
2629
2630 packed = pinconf_to_config_packed(PIN_CONFIG_PERSIST_STATE,
2631 !transitory);
2632 gpio = gpio_chip_hwgpio(desc);
2633 rc = chip->set_config(chip, gpio, packed);
2634 if (rc == -ENOTSUPP) {
2635 dev_dbg(&desc->gdev->dev, "Persistence not supported for GPIO %d\n",
2636 gpio);
2637 return 0;
2638 }
2639
2640 return rc;
2641}
2642EXPORT_SYMBOL_GPL(gpiod_set_transitory);
2643
2644/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002645 * gpiod_is_active_low - test whether a GPIO is active-low or not
2646 * @desc: the gpio descriptor to test
2647 *
2648 * Returns 1 if the GPIO is active-low, 0 otherwise.
2649 */
2650int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002651{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002652 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002653 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002654}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002655EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08002656
2657/* I/O calls are only valid after configuration completed; the relevant
2658 * "is this a valid GPIO" error checks should already have been done.
2659 *
2660 * "Get" operations are often inlinable as reading a pin value register,
2661 * and masking the relevant bit in that register.
2662 *
2663 * When "set" operations are inlinable, they involve writing that mask to
2664 * one register to set a low value, or a different register to set it high.
2665 * Otherwise locking is needed, so there may be little value to inlining.
2666 *
2667 *------------------------------------------------------------------------
2668 *
2669 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2670 * have requested the GPIO. That can include implicit requesting by
2671 * a direction setting call. Marking a gpio as requested locks its chip
2672 * in memory, guaranteeing that these table lookups need no more locking
2673 * and that gpiochip_remove() will fail.
2674 *
2675 * REVISIT when debugging, consider adding some instrumentation to ensure
2676 * that the GPIO was actually requested.
2677 */
2678
Linus Walleijfac9d882017-09-26 20:58:28 +02002679static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002680{
2681 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002682 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002683 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002684
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002685 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002686 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002687 value = chip->get ? chip->get(chip, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01002688 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002689 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002690 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002691}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002692
Lukas Wunnereec1d562017-10-12 12:40:10 +02002693static int gpio_chip_get_multiple(struct gpio_chip *chip,
2694 unsigned long *mask, unsigned long *bits)
2695{
2696 if (chip->get_multiple) {
2697 return chip->get_multiple(chip, mask, bits);
2698 } else if (chip->get) {
2699 int i, value;
2700
2701 for_each_set_bit(i, mask, chip->ngpio) {
2702 value = chip->get(chip, i);
2703 if (value < 0)
2704 return value;
2705 __assign_bit(i, bits, value);
2706 }
2707 return 0;
2708 }
2709 return -EIO;
2710}
2711
2712int gpiod_get_array_value_complex(bool raw, bool can_sleep,
2713 unsigned int array_size,
2714 struct gpio_desc **desc_array,
2715 int *value_array)
2716{
2717 int i = 0;
2718
2719 while (i < array_size) {
2720 struct gpio_chip *chip = desc_array[i]->gdev->chip;
2721 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2722 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2723 int first, j, ret;
2724
2725 if (!can_sleep)
2726 WARN_ON(chip->can_sleep);
2727
2728 /* collect all inputs belonging to the same chip */
2729 first = i;
2730 memset(mask, 0, sizeof(mask));
2731 do {
2732 const struct gpio_desc *desc = desc_array[i];
2733 int hwgpio = gpio_chip_hwgpio(desc);
2734
2735 __set_bit(hwgpio, mask);
2736 i++;
2737 } while ((i < array_size) &&
2738 (desc_array[i]->gdev->chip == chip));
2739
2740 ret = gpio_chip_get_multiple(chip, mask, bits);
2741 if (ret)
2742 return ret;
2743
2744 for (j = first; j < i; j++) {
2745 const struct gpio_desc *desc = desc_array[j];
2746 int hwgpio = gpio_chip_hwgpio(desc);
2747 int value = test_bit(hwgpio, bits);
2748
2749 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2750 value = !value;
2751 value_array[j] = value;
2752 trace_gpio_value(desc_to_gpio(desc), 1, value);
2753 }
2754 }
2755 return 0;
2756}
2757
David Brownelld2876d02008-02-04 22:28:20 -08002758/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002759 * gpiod_get_raw_value() - return a gpio's raw value
2760 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08002761 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002762 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002763 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002764 *
2765 * This function should be called from contexts where we cannot sleep, and will
2766 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002767 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002768int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002769{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002770 VALIDATE_DESC(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002771 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002772 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02002773 return gpiod_get_raw_value_commit(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002774}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002775EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002776
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002777/**
2778 * gpiod_get_value() - return a gpio's value
2779 * @desc: gpio whose value will be returned
2780 *
2781 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002782 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002783 *
2784 * This function should be called from contexts where we cannot sleep, and will
2785 * complain if the GPIO chip functions potentially sleep.
2786 */
2787int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002788{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002789 int value;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002790
2791 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002792 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002793 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002794
Linus Walleijfac9d882017-09-26 20:58:28 +02002795 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002796 if (value < 0)
2797 return value;
2798
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002799 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2800 value = !value;
2801
2802 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002803}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002804EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08002805
Lukas Wunnereec1d562017-10-12 12:40:10 +02002806/**
2807 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
2808 * @array_size: number of elements in the descriptor / value arrays
2809 * @desc_array: array of GPIO descriptors whose values will be read
2810 * @value_array: array to store the read values
2811 *
2812 * Read the raw values of the GPIOs, i.e. the values of the physical lines
2813 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
2814 * else an error code.
2815 *
2816 * This function should be called from contexts where we cannot sleep,
2817 * and it will complain if the GPIO chip functions potentially sleep.
2818 */
2819int gpiod_get_raw_array_value(unsigned int array_size,
2820 struct gpio_desc **desc_array, int *value_array)
2821{
2822 if (!desc_array)
2823 return -EINVAL;
2824 return gpiod_get_array_value_complex(true, false, array_size,
2825 desc_array, value_array);
2826}
2827EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
2828
2829/**
2830 * gpiod_get_array_value() - read values from an array of GPIOs
2831 * @array_size: number of elements in the descriptor / value arrays
2832 * @desc_array: array of GPIO descriptors whose values will be read
2833 * @value_array: array to store the read values
2834 *
2835 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2836 * into account. Return 0 in case of success, else an error code.
2837 *
2838 * This function should be called from contexts where we cannot sleep,
2839 * and it will complain if the GPIO chip functions potentially sleep.
2840 */
2841int gpiod_get_array_value(unsigned int array_size,
2842 struct gpio_desc **desc_array, int *value_array)
2843{
2844 if (!desc_array)
2845 return -EINVAL;
2846 return gpiod_get_array_value_complex(false, false, array_size,
2847 desc_array, value_array);
2848}
2849EXPORT_SYMBOL_GPL(gpiod_get_array_value);
2850
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302851/*
Linus Walleijfac9d882017-09-26 20:58:28 +02002852 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002853 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002854 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302855 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002856static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302857{
2858 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002859 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002860 int offset = gpio_chip_hwgpio(desc);
2861
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302862 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002863 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302864 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002865 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302866 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002867 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302868 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002869 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302870 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002871 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302872 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002873 gpiod_err(desc,
2874 "%s: Error in set_value for open drain err %d\n",
2875 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302876}
2877
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302878/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002879 * _gpio_set_open_source_value() - Set the open source gpio's value.
2880 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002881 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302882 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002883static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302884{
2885 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002886 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002887 int offset = gpio_chip_hwgpio(desc);
2888
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302889 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002890 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302891 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002892 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302893 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002894 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302895 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002896 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302897 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002898 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302899 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002900 gpiod_err(desc,
2901 "%s: Error in set_value for open source err %d\n",
2902 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302903}
2904
Linus Walleijfac9d882017-09-26 20:58:28 +02002905static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08002906{
2907 struct gpio_chip *chip;
2908
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002909 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002910 trace_gpio_value(desc_to_gpio(desc), 0, value);
Linus Walleij02e47982017-09-26 21:20:23 +02002911 chip->set(chip, gpio_chip_hwgpio(desc), value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002912}
2913
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002914/*
2915 * set multiple outputs on the same chip;
2916 * use the chip's set_multiple function if available;
2917 * otherwise set the outputs sequentially;
2918 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
2919 * defines which outputs are to be changed
2920 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
2921 * defines the values the outputs specified by mask are to be set to
2922 */
2923static void gpio_chip_set_multiple(struct gpio_chip *chip,
2924 unsigned long *mask, unsigned long *bits)
2925{
2926 if (chip->set_multiple) {
2927 chip->set_multiple(chip, mask, bits);
2928 } else {
Andy Shevchenko5e4e6fb2017-01-03 19:01:17 +02002929 unsigned int i;
2930
2931 /* set outputs if the corresponding mask bit is set */
2932 for_each_set_bit(i, mask, chip->ngpio)
2933 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002934 }
2935}
2936
Linus Walleij44c72882016-04-24 11:36:59 +02002937void gpiod_set_array_value_complex(bool raw, bool can_sleep,
2938 unsigned int array_size,
2939 struct gpio_desc **desc_array,
2940 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002941{
2942 int i = 0;
2943
2944 while (i < array_size) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002945 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002946 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2947 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2948 int count = 0;
2949
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002950 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002951 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002952
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002953 memset(mask, 0, sizeof(mask));
2954 do {
2955 struct gpio_desc *desc = desc_array[i];
2956 int hwgpio = gpio_chip_hwgpio(desc);
2957 int value = value_array[i];
2958
2959 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2960 value = !value;
2961 trace_gpio_value(desc_to_gpio(desc), 0, value);
2962 /*
2963 * collect all normal outputs belonging to the same chip
2964 * open drain and open source outputs are set individually
2965 */
Linus Walleij02e47982017-09-26 21:20:23 +02002966 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02002967 gpio_set_open_drain_value_commit(desc, value);
Linus Walleij02e47982017-09-26 21:20:23 +02002968 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02002969 gpio_set_open_source_value_commit(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002970 } else {
2971 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002972 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002973 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002974 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002975 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002976 count++;
2977 }
2978 i++;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002979 } while ((i < array_size) &&
2980 (desc_array[i]->gdev->chip == chip));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002981 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002982 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002983 gpio_chip_set_multiple(chip, mask, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002984 }
2985}
2986
David Brownelld2876d02008-02-04 22:28:20 -08002987/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002988 * gpiod_set_raw_value() - assign a gpio's raw value
2989 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08002990 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002991 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002992 * Set the raw value of the GPIO, i.e. the value of its physical line without
2993 * regard for its ACTIVE_LOW status.
2994 *
2995 * This function should be called from contexts where we cannot sleep, and will
2996 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002997 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002998void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002999{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003000 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01003001 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003002 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02003003 gpiod_set_raw_value_commit(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08003004}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003005EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08003006
3007/**
Geert Uytterhoeven1e77fc82018-01-09 19:08:21 +01003008 * gpiod_set_value_nocheck() - set a GPIO line value without checking
3009 * @desc: the descriptor to set the value on
3010 * @value: value to set
3011 *
3012 * This sets the value of a GPIO line backing a descriptor, applying
3013 * different semantic quirks like active low and open drain/source
3014 * handling.
3015 */
3016static void gpiod_set_value_nocheck(struct gpio_desc *desc, int value)
3017{
3018 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3019 value = !value;
3020 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
3021 gpio_set_open_drain_value_commit(desc, value);
3022 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
3023 gpio_set_open_source_value_commit(desc, value);
3024 else
3025 gpiod_set_raw_value_commit(desc, value);
3026}
3027
3028/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003029 * gpiod_set_value() - assign a gpio's value
3030 * @desc: gpio whose value will be assigned
3031 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08003032 *
Linus Walleij02e47982017-09-26 21:20:23 +02003033 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
3034 * OPEN_DRAIN and OPEN_SOURCE flags into account.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003035 *
3036 * This function should be called from contexts where we cannot sleep, and will
3037 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003038 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003039void gpiod_set_value(struct gpio_desc *desc, int value)
3040{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003041 VALIDATE_DESC_VOID(desc);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003042 WARN_ON(desc->gdev->chip->can_sleep);
Geert Uytterhoeven1e77fc82018-01-09 19:08:21 +01003043 gpiod_set_value_nocheck(desc, value);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003044}
3045EXPORT_SYMBOL_GPL(gpiod_set_value);
3046
3047/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003048 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003049 * @array_size: number of elements in the descriptor / value arrays
3050 * @desc_array: array of GPIO descriptors whose values will be assigned
3051 * @value_array: array of values to assign
3052 *
3053 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3054 * without regard for their ACTIVE_LOW status.
3055 *
3056 * This function should be called from contexts where we cannot sleep, and will
3057 * complain if the GPIO chip functions potentially sleep.
3058 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003059void gpiod_set_raw_array_value(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003060 struct gpio_desc **desc_array, int *value_array)
3061{
3062 if (!desc_array)
3063 return;
Linus Walleij44c72882016-04-24 11:36:59 +02003064 gpiod_set_array_value_complex(true, false, array_size, desc_array,
3065 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003066}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003067EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003068
3069/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003070 * gpiod_set_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003071 * @array_size: number of elements in the descriptor / value arrays
3072 * @desc_array: array of GPIO descriptors whose values will be assigned
3073 * @value_array: array of values to assign
3074 *
3075 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3076 * into account.
3077 *
3078 * This function should be called from contexts where we cannot sleep, and will
3079 * complain if the GPIO chip functions potentially sleep.
3080 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003081void gpiod_set_array_value(unsigned int array_size,
3082 struct gpio_desc **desc_array, int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003083{
3084 if (!desc_array)
3085 return;
Linus Walleij44c72882016-04-24 11:36:59 +02003086 gpiod_set_array_value_complex(false, false, array_size, desc_array,
3087 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003088}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003089EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003090
3091/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003092 * gpiod_cansleep() - report whether gpio value access may sleep
3093 * @desc: gpio to check
3094 *
3095 */
3096int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003097{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003098 VALIDATE_DESC(desc);
3099 return desc->gdev->chip->can_sleep;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003100}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003101EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08003102
David Brownell0f6d5042008-10-15 22:03:14 -07003103/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003104 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
3105 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07003106 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003107 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
3108 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07003109 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003110int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07003111{
Linus Walleij4c37ce82016-05-02 13:13:10 +02003112 struct gpio_chip *chip;
3113 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07003114
Linus Walleij79bb71b2016-06-15 22:57:38 +02003115 /*
3116 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
3117 * requires this function to not return zero on an invalid descriptor
3118 * but rather a negative error number.
3119 */
Linus Walleijbfbbe442016-06-16 11:55:55 +02003120 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
Linus Walleij79bb71b2016-06-15 22:57:38 +02003121 return -EINVAL;
3122
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003123 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003124 offset = gpio_chip_hwgpio(desc);
Linus Walleij4c37ce82016-05-02 13:13:10 +02003125 if (chip->to_irq) {
3126 int retirq = chip->to_irq(chip, offset);
3127
3128 /* Zero means NO_IRQ */
3129 if (!retirq)
3130 return -ENXIO;
3131
3132 return retirq;
3133 }
3134 return -ENXIO;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003135}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003136EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003137
Linus Walleijd468bf92013-09-24 11:54:38 +02003138/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003139 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003140 * @chip: the chip the GPIO to lock belongs to
3141 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02003142 *
3143 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08003144 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08003145 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003146int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08003147{
Linus Walleij9c102802016-05-25 10:56:03 +02003148 struct gpio_desc *desc;
Linus Walleijd468bf92013-09-24 11:54:38 +02003149
Linus Walleij9c102802016-05-25 10:56:03 +02003150 desc = gpiochip_get_desc(chip, offset);
3151 if (IS_ERR(desc))
3152 return PTR_ERR(desc);
3153
Linus Walleij60f83392016-11-12 15:01:09 +01003154 /*
3155 * If it's fast: flush the direction setting if something changed
3156 * behind our back
3157 */
3158 if (!chip->can_sleep && chip->get_direction) {
Linus Walleij9c102802016-05-25 10:56:03 +02003159 int dir = chip->get_direction(chip, offset);
3160
3161 if (dir)
3162 clear_bit(FLAG_IS_OUT, &desc->flags);
3163 else
3164 set_bit(FLAG_IS_OUT, &desc->flags);
3165 }
3166
3167 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003168 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02003169 "%s: tried to flag a GPIO set as output for IRQ\n",
3170 __func__);
3171 return -EIO;
3172 }
3173
Linus Walleij9c102802016-05-25 10:56:03 +02003174 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
Linus Walleij3940c342016-11-14 00:09:07 +01003175
3176 /*
3177 * If the consumer has not set up a label (such as when the
3178 * IRQ is referenced from .to_irq()) we set up a label here
3179 * so it is clear this is used as an interrupt.
3180 */
3181 if (!desc->label)
3182 desc_set_label(desc, "interrupt");
3183
Linus Walleijd468bf92013-09-24 11:54:38 +02003184 return 0;
3185}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003186EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02003187
Linus Walleijd468bf92013-09-24 11:54:38 +02003188/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003189 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003190 * @chip: the chip the GPIO to lock belongs to
3191 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02003192 *
3193 * This is used directly by GPIO drivers that want to indicate
3194 * that a certain GPIO is no longer used exclusively for IRQ.
3195 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003196void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02003197{
Linus Walleij3940c342016-11-14 00:09:07 +01003198 struct gpio_desc *desc;
3199
3200 desc = gpiochip_get_desc(chip, offset);
3201 if (IS_ERR(desc))
Linus Walleijd468bf92013-09-24 11:54:38 +02003202 return;
3203
Linus Walleij3940c342016-11-14 00:09:07 +01003204 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
3205
3206 /* If we only had this marking, erase it */
3207 if (desc->label && !strcmp(desc->label, "interrupt"))
3208 desc_set_label(desc, NULL);
Linus Walleijd468bf92013-09-24 11:54:38 +02003209}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003210EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02003211
Linus Walleij6cee3822016-02-11 20:16:45 +01003212bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
3213{
3214 if (offset >= chip->ngpio)
3215 return false;
3216
3217 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
3218}
3219EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
3220
Linus Walleij143b65d2016-02-16 15:41:42 +01003221bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
3222{
3223 if (offset >= chip->ngpio)
3224 return false;
3225
3226 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
3227}
3228EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
3229
3230bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
3231{
3232 if (offset >= chip->ngpio)
3233 return false;
3234
3235 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
3236}
3237EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
3238
Charles Keepax05f479b2017-05-23 15:47:29 +01003239bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset)
3240{
3241 if (offset >= chip->ngpio)
3242 return false;
3243
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303244 return !test_bit(FLAG_TRANSITORY, &chip->gpiodev->descs[offset].flags);
Charles Keepax05f479b2017-05-23 15:47:29 +01003245}
3246EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
3247
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003248/**
3249 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
3250 * @desc: gpio whose value will be returned
3251 *
3252 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003253 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003254 *
3255 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003256 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003257int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08003258{
David Brownelld2876d02008-02-04 22:28:20 -08003259 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003260 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003261 return gpiod_get_raw_value_commit(desc);
David Brownelld2876d02008-02-04 22:28:20 -08003262}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003263EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003264
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003265/**
3266 * gpiod_get_value_cansleep() - return a gpio's value
3267 * @desc: gpio whose value will be returned
3268 *
3269 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003270 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003271 *
3272 * This function is to be called from contexts that can sleep.
3273 */
3274int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003275{
David Brownelld2876d02008-02-04 22:28:20 -08003276 int value;
David Brownelld2876d02008-02-04 22:28:20 -08003277
3278 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003279 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003280 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003281 if (value < 0)
3282 return value;
3283
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003284 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3285 value = !value;
3286
David Brownelld2876d02008-02-04 22:28:20 -08003287 return value;
3288}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003289EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003290
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003291/**
Lukas Wunnereec1d562017-10-12 12:40:10 +02003292 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
3293 * @array_size: number of elements in the descriptor / value arrays
3294 * @desc_array: array of GPIO descriptors whose values will be read
3295 * @value_array: array to store the read values
3296 *
3297 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3298 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3299 * else an error code.
3300 *
3301 * This function is to be called from contexts that can sleep.
3302 */
3303int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
3304 struct gpio_desc **desc_array,
3305 int *value_array)
3306{
3307 might_sleep_if(extra_checks);
3308 if (!desc_array)
3309 return -EINVAL;
3310 return gpiod_get_array_value_complex(true, true, array_size,
3311 desc_array, value_array);
3312}
3313EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
3314
3315/**
3316 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
3317 * @array_size: number of elements in the descriptor / value arrays
3318 * @desc_array: array of GPIO descriptors whose values will be read
3319 * @value_array: array to store the read values
3320 *
3321 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3322 * into account. Return 0 in case of success, else an error code.
3323 *
3324 * This function is to be called from contexts that can sleep.
3325 */
3326int gpiod_get_array_value_cansleep(unsigned int array_size,
3327 struct gpio_desc **desc_array,
3328 int *value_array)
3329{
3330 might_sleep_if(extra_checks);
3331 if (!desc_array)
3332 return -EINVAL;
3333 return gpiod_get_array_value_complex(false, true, array_size,
3334 desc_array, value_array);
3335}
3336EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
3337
3338/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003339 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
3340 * @desc: gpio whose value will be assigned
3341 * @value: value to assign
3342 *
3343 * Set the raw value of the GPIO, i.e. the value of its physical line without
3344 * regard for its ACTIVE_LOW status.
3345 *
3346 * This function is to be called from contexts that can sleep.
3347 */
3348void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003349{
David Brownelld2876d02008-02-04 22:28:20 -08003350 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003351 VALIDATE_DESC_VOID(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003352 gpiod_set_raw_value_commit(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003353}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003354EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003355
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003356/**
3357 * gpiod_set_value_cansleep() - assign a gpio's value
3358 * @desc: gpio whose value will be assigned
3359 * @value: value to assign
3360 *
3361 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3362 * account
3363 *
3364 * This function is to be called from contexts that can sleep.
3365 */
3366void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003367{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003368 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003369 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1e77fc82018-01-09 19:08:21 +01003370 gpiod_set_value_nocheck(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08003371}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003372EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08003373
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003374/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003375 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003376 * @array_size: number of elements in the descriptor / value arrays
3377 * @desc_array: array of GPIO descriptors whose values will be assigned
3378 * @value_array: array of values to assign
3379 *
3380 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3381 * without regard for their ACTIVE_LOW status.
3382 *
3383 * This function is to be called from contexts that can sleep.
3384 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003385void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
3386 struct gpio_desc **desc_array,
3387 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003388{
3389 might_sleep_if(extra_checks);
3390 if (!desc_array)
3391 return;
Linus Walleij44c72882016-04-24 11:36:59 +02003392 gpiod_set_array_value_complex(true, true, array_size, desc_array,
3393 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003394}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003395EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003396
3397/**
Dmitry Torokhov3946d182017-08-14 21:59:55 -07003398 * gpiod_add_lookup_tables() - register GPIO device consumers
3399 * @tables: list of tables of consumers to register
3400 * @n: number of tables in the list
3401 */
3402void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
3403{
3404 unsigned int i;
3405
3406 mutex_lock(&gpio_lookup_lock);
3407
3408 for (i = 0; i < n; i++)
3409 list_add_tail(&tables[i]->list, &gpio_lookup_list);
3410
3411 mutex_unlock(&gpio_lookup_lock);
3412}
3413
3414/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003415 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003416 * @array_size: number of elements in the descriptor / value arrays
3417 * @desc_array: array of GPIO descriptors whose values will be assigned
3418 * @value_array: array of values to assign
3419 *
3420 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3421 * into account.
3422 *
3423 * This function is to be called from contexts that can sleep.
3424 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003425void gpiod_set_array_value_cansleep(unsigned int array_size,
3426 struct gpio_desc **desc_array,
3427 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003428{
3429 might_sleep_if(extra_checks);
3430 if (!desc_array)
3431 return;
Linus Walleij44c72882016-04-24 11:36:59 +02003432 gpiod_set_array_value_complex(false, true, array_size, desc_array,
3433 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003434}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003435EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003436
3437/**
Alexandre Courbotad824782013-12-03 12:20:11 +09003438 * gpiod_add_lookup_table() - register GPIO device consumers
3439 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003440 */
Alexandre Courbotad824782013-12-03 12:20:11 +09003441void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003442{
3443 mutex_lock(&gpio_lookup_lock);
3444
Alexandre Courbotad824782013-12-03 12:20:11 +09003445 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003446
3447 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08003448}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02003449EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
David Brownelld2876d02008-02-04 22:28:20 -08003450
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05303451/**
3452 * gpiod_remove_lookup_table() - unregister GPIO device consumers
3453 * @table: table of consumers to unregister
3454 */
3455void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
3456{
3457 mutex_lock(&gpio_lookup_lock);
3458
3459 list_del(&table->list);
3460
3461 mutex_unlock(&gpio_lookup_lock);
3462}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02003463EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05303464
Alexandre Courbotad824782013-12-03 12:20:11 +09003465static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
3466{
3467 const char *dev_id = dev ? dev_name(dev) : NULL;
3468 struct gpiod_lookup_table *table;
3469
3470 mutex_lock(&gpio_lookup_lock);
3471
3472 list_for_each_entry(table, &gpio_lookup_list, list) {
3473 if (table->dev_id && dev_id) {
3474 /*
3475 * Valid strings on both ends, must be identical to have
3476 * a match
3477 */
3478 if (!strcmp(table->dev_id, dev_id))
3479 goto found;
3480 } else {
3481 /*
3482 * One of the pointers is NULL, so both must be to have
3483 * a match
3484 */
3485 if (dev_id == table->dev_id)
3486 goto found;
3487 }
3488 }
3489 table = NULL;
3490
3491found:
3492 mutex_unlock(&gpio_lookup_lock);
3493 return table;
3494}
3495
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003496static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09003497 unsigned int idx,
3498 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003499{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003500 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09003501 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003502 struct gpiod_lookup *p;
3503
Alexandre Courbotad824782013-12-03 12:20:11 +09003504 table = gpiod_find_lookup_table(dev);
3505 if (!table)
3506 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003507
Alexandre Courbotad824782013-12-03 12:20:11 +09003508 for (p = &table->table[0]; p->chip_label; p++) {
3509 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003510
Alexandre Courbotad824782013-12-03 12:20:11 +09003511 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003512 if (p->idx != idx)
3513 continue;
3514
Alexandre Courbotad824782013-12-03 12:20:11 +09003515 /* If the lookup entry has a con_id, require exact match */
3516 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
3517 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003518
Alexandre Courbotad824782013-12-03 12:20:11 +09003519 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003520
Alexandre Courbotad824782013-12-03 12:20:11 +09003521 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003522 dev_err(dev, "cannot find GPIO chip %s\n",
3523 p->chip_label);
3524 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003525 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003526
Alexandre Courbotad824782013-12-03 12:20:11 +09003527 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003528 dev_err(dev,
3529 "requested GPIO %d is out of range [0..%d] for chip %s\n",
3530 idx, chip->ngpio, chip->label);
3531 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09003532 }
3533
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09003534 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09003535 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003536
3537 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09003538 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003539
3540 return desc;
3541}
3542
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003543static int dt_gpio_count(struct device *dev, const char *con_id)
3544{
3545 int ret;
3546 char propname[32];
3547 unsigned int i;
3548
3549 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
3550 if (con_id)
3551 snprintf(propname, sizeof(propname), "%s-%s",
3552 con_id, gpio_suffixes[i]);
3553 else
3554 snprintf(propname, sizeof(propname), "%s",
3555 gpio_suffixes[i]);
3556
3557 ret = of_gpio_named_count(dev->of_node, propname);
Andy Shevchenko4033d4a2017-02-20 18:15:47 +02003558 if (ret > 0)
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003559 break;
3560 }
Andy Shevchenko4033d4a2017-02-20 18:15:47 +02003561 return ret ? ret : -ENOENT;
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003562}
3563
3564static int platform_gpio_count(struct device *dev, const char *con_id)
3565{
3566 struct gpiod_lookup_table *table;
3567 struct gpiod_lookup *p;
3568 unsigned int count = 0;
3569
3570 table = gpiod_find_lookup_table(dev);
3571 if (!table)
3572 return -ENOENT;
3573
3574 for (p = &table->table[0]; p->chip_label; p++) {
3575 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3576 (!con_id && !p->con_id))
3577 count++;
3578 }
3579 if (!count)
3580 return -ENOENT;
3581
3582 return count;
3583}
3584
3585/**
3586 * gpiod_count - return the number of GPIOs associated with a device / function
3587 * or -ENOENT if no GPIO has been assigned to the requested function
3588 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3589 * @con_id: function within the GPIO consumer
3590 */
3591int gpiod_count(struct device *dev, const char *con_id)
3592{
3593 int count = -ENOENT;
3594
3595 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
3596 count = dt_gpio_count(dev, con_id);
3597 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
3598 count = acpi_gpio_count(dev, con_id);
3599
3600 if (count < 0)
3601 count = platform_gpio_count(dev, con_id);
3602
3603 return count;
3604}
3605EXPORT_SYMBOL_GPL(gpiod_count);
3606
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003607/**
Thierry Reding08791622014-04-25 16:54:22 +02003608 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09003609 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003610 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003611 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003612 *
3613 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003614 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07003615 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003616 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003617struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003618 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003619{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003620 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003621}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003622EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003623
3624/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02003625 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
3626 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3627 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003628 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02003629 *
3630 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
3631 * the requested function it will return NULL. This is convenient for drivers
3632 * that need to handle optional GPIOs.
3633 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003634struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003635 const char *con_id,
3636 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003637{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003638 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003639}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003640EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003641
Benoit Parrotf625d462015-02-02 11:44:44 -06003642
3643/**
3644 * gpiod_configure_flags - helper function to configure a given GPIO
3645 * @desc: gpio whose value will be assigned
3646 * @con_id: function within the GPIO consumer
Johan Hovold85b03b32016-07-03 18:32:05 +02003647 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3648 * of_get_gpio_hog()
Benoit Parrotf625d462015-02-02 11:44:44 -06003649 * @dflags: gpiod_flags - optional GPIO initialization flags
3650 *
3651 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
3652 * requested function and/or index, or another IS_ERR() code if an error
3653 * occurred while trying to acquire the GPIO.
3654 */
Andy Shevchenkoc29fd9e2017-05-23 20:03:16 +03003655int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Johan Hovold85b03b32016-07-03 18:32:05 +02003656 unsigned long lflags, enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06003657{
3658 int status;
3659
Johan Hovold85b03b32016-07-03 18:32:05 +02003660 if (lflags & GPIO_ACTIVE_LOW)
3661 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
Linus Walleijf926dfc2017-09-10 19:26:22 +02003662
Johan Hovold85b03b32016-07-03 18:32:05 +02003663 if (lflags & GPIO_OPEN_DRAIN)
3664 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
Linus Walleijf926dfc2017-09-10 19:26:22 +02003665 else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) {
3666 /*
3667 * This enforces open drain mode from the consumer side.
3668 * This is necessary for some busses like I2C, but the lookup
3669 * should *REALLY* have specified them as open drain in the
3670 * first place, so print a little warning here.
3671 */
3672 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3673 gpiod_warn(desc,
3674 "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
3675 }
3676
Johan Hovold85b03b32016-07-03 18:32:05 +02003677 if (lflags & GPIO_OPEN_SOURCE)
3678 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303679
3680 status = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
3681 if (status < 0)
3682 return status;
Johan Hovold85b03b32016-07-03 18:32:05 +02003683
Benoit Parrotf625d462015-02-02 11:44:44 -06003684 /* No particular flag request, return here... */
3685 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
3686 pr_debug("no flags found for %s\n", con_id);
3687 return 0;
3688 }
3689
3690 /* Process flags */
3691 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
3692 status = gpiod_direction_output(desc,
Linus Walleijad177312016-11-13 23:02:44 +01003693 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
Benoit Parrotf625d462015-02-02 11:44:44 -06003694 else
3695 status = gpiod_direction_input(desc);
3696
3697 return status;
3698}
3699
Thierry Reding29a1f2332014-04-25 17:10:06 +02003700/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003701 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02003702 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003703 * @con_id: function within the GPIO consumer
3704 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003705 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003706 *
3707 * This variant of gpiod_get() allows to access GPIOs other than the first
3708 * defined one for functions that define several GPIOs.
3709 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003710 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
3711 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07003712 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003713 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003714struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003715 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003716 unsigned int idx,
3717 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003718{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003719 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003720 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003721 enum gpio_lookup_flags lookupflags = 0;
Linus Walleij7d18f0a2018-01-16 08:29:50 +01003722 /* Maybe we have a device name, maybe not */
3723 const char *devname = dev ? dev_name(dev) : "?";
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003724
3725 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
3726
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003727 if (dev) {
3728 /* Using device tree? */
3729 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
3730 dev_dbg(dev, "using device tree for GPIO lookup\n");
3731 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
3732 } else if (ACPI_COMPANION(dev)) {
3733 dev_dbg(dev, "using ACPI for GPIO lookup\n");
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +03003734 desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags);
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003735 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003736 }
3737
3738 /*
3739 * Either we are not using DT or ACPI, or their lookup did not return
3740 * a result. In that case, use platform lookup as a fallback.
3741 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003742 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04003743 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003744 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003745 }
3746
3747 if (IS_ERR(desc)) {
Wang Dongsheng9d5a1f22018-02-27 00:12:13 -08003748 dev_dbg(dev, "No GPIO consumer %s found\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003749 return desc;
3750 }
3751
Linus Walleij7d18f0a2018-01-16 08:29:50 +01003752 /*
3753 * If a connection label was passed use that, else attempt to use
3754 * the device name as label
3755 */
3756 status = gpiod_request(desc, con_id ? con_id : devname);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003757 if (status < 0)
3758 return ERR_PTR(status);
3759
Johan Hovold85b03b32016-07-03 18:32:05 +02003760 status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003761 if (status < 0) {
3762 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
3763 gpiod_put(desc);
3764 return ERR_PTR(status);
3765 }
3766
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003767 return desc;
3768}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003769EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003770
3771/**
Linus Walleij6392cca2017-12-29 02:07:54 +01003772 * gpiod_get_from_of_node() - obtain a GPIO from an OF node
3773 * @node: handle of the OF node
3774 * @propname: name of the DT property representing the GPIO
3775 * @index: index of the GPIO to obtain for the consumer
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003776 * @dflags: GPIO initialization flags
Thierry Reding950d55f52017-07-24 16:57:22 +02003777 * @label: label to attach to the requested GPIO
Mika Westerberg40b73182014-10-21 13:33:59 +02003778 *
Thierry Reding950d55f52017-07-24 16:57:22 +02003779 * Returns:
Andy Shevchenkoff213782017-02-28 17:03:12 +02003780 * On successful request the GPIO pin is configured in accordance with
Linus Walleij6392cca2017-12-29 02:07:54 +01003781 * provided @dflags. If the node does not have the requested GPIO
3782 * property, NULL is returned.
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003783 *
Mika Westerberg40b73182014-10-21 13:33:59 +02003784 * In case of error an ERR_PTR() is returned.
3785 */
Linus Walleij92542ed2017-12-29 22:52:02 +01003786struct gpio_desc *gpiod_get_from_of_node(struct device_node *node,
3787 const char *propname, int index,
3788 enum gpiod_flags dflags,
3789 const char *label)
Mika Westerberg40b73182014-10-21 13:33:59 +02003790{
Colin Ian King40a3c9d2018-01-16 11:56:11 +00003791 struct gpio_desc *desc;
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003792 unsigned long lflags = 0;
Linus Walleij6392cca2017-12-29 02:07:54 +01003793 enum of_gpio_flags flags;
Mika Westerberg40b73182014-10-21 13:33:59 +02003794 bool active_low = false;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003795 bool single_ended = false;
Laxman Dewangan4c0facd2017-04-06 19:05:52 +05303796 bool open_drain = false;
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303797 bool transitory = false;
Mika Westerberg40b73182014-10-21 13:33:59 +02003798 int ret;
3799
Linus Walleij6392cca2017-12-29 02:07:54 +01003800 desc = of_get_named_gpiod_flags(node, propname,
3801 index, &flags);
Mika Westerberg40b73182014-10-21 13:33:59 +02003802
Linus Walleij6392cca2017-12-29 02:07:54 +01003803 if (!desc || IS_ERR(desc)) {
3804 /* If it is not there, just return NULL */
3805 if (PTR_ERR(desc) == -ENOENT)
3806 return NULL;
3807 return desc;
Mika Westerberg40b73182014-10-21 13:33:59 +02003808 }
3809
Linus Walleij6392cca2017-12-29 02:07:54 +01003810 active_low = flags & OF_GPIO_ACTIVE_LOW;
3811 single_ended = flags & OF_GPIO_SINGLE_ENDED;
3812 open_drain = flags & OF_GPIO_OPEN_DRAIN;
3813 transitory = flags & OF_GPIO_TRANSITORY;
Mika Westerberg40b73182014-10-21 13:33:59 +02003814
Alexander Steinb2987d72017-01-12 17:39:24 +01003815 ret = gpiod_request(desc, label);
Johan Hovold85b03b32016-07-03 18:32:05 +02003816 if (ret)
3817 return ERR_PTR(ret);
3818
Mika Westerberg40b73182014-10-21 13:33:59 +02003819 if (active_low)
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003820 lflags |= GPIO_ACTIVE_LOW;
Mika Westerberg40b73182014-10-21 13:33:59 +02003821
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003822 if (single_ended) {
Laxman Dewangan4c0facd2017-04-06 19:05:52 +05303823 if (open_drain)
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003824 lflags |= GPIO_OPEN_DRAIN;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003825 else
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003826 lflags |= GPIO_OPEN_SOURCE;
3827 }
3828
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303829 if (transitory)
3830 lflags |= GPIO_TRANSITORY;
3831
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003832 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
3833 if (ret < 0) {
3834 gpiod_put(desc);
3835 return ERR_PTR(ret);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003836 }
3837
Mika Westerberg40b73182014-10-21 13:33:59 +02003838 return desc;
3839}
Linus Walleij92542ed2017-12-29 22:52:02 +01003840EXPORT_SYMBOL(gpiod_get_from_of_node);
Linus Walleij6392cca2017-12-29 02:07:54 +01003841
3842/**
Mika Westerberg40b73182014-10-21 13:33:59 +02003843 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
3844 * @fwnode: handle of the firmware node
3845 * @propname: name of the firmware property representing the GPIO
Linus Walleij6392cca2017-12-29 02:07:54 +01003846 * @index: index of the GPIO to obtain for the consumer
Mika Westerberg40b73182014-10-21 13:33:59 +02003847 * @dflags: GPIO initialization flags
3848 * @label: label to attach to the requested GPIO
3849 *
3850 * This function can be used for drivers that get their configuration
Linus Walleij6392cca2017-12-29 02:07:54 +01003851 * from opaque firmware.
Thierry Reding29a1f2332014-04-25 17:10:06 +02003852 *
Linus Walleij6392cca2017-12-29 02:07:54 +01003853 * The function properly finds the corresponding GPIO using whatever is the
Thierry Reding29a1f2332014-04-25 17:10:06 +02003854 * underlying firmware interface and then makes sure that the GPIO
3855 * descriptor is requested before it is returned to the caller.
3856 *
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003857 * Returns:
Thierry Reding29a1f2332014-04-25 17:10:06 +02003858 * On successful request the GPIO pin is configured in accordance with
3859 * provided @dflags.
3860 *
3861 * In case of error an ERR_PTR() is returned.
3862 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003863struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
Thierry Reding29a1f2332014-04-25 17:10:06 +02003864 const char *propname, int index,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003865 enum gpiod_flags dflags,
3866 const char *label)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003867{
3868 struct gpio_desc *desc = ERR_PTR(-ENODEV);
3869 unsigned long lflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003870 int ret;
3871
David Brownelld2876d02008-02-04 22:28:20 -08003872 if (!fwnode)
David Brownelld2876d02008-02-04 22:28:20 -08003873 return ERR_PTR(-EINVAL);
3874
3875 if (is_of_node(fwnode)) {
Linus Walleij6392cca2017-12-29 02:07:54 +01003876 desc = gpiod_get_from_of_node(to_of_node(fwnode),
3877 propname, index,
3878 dflags,
3879 label);
3880 return desc;
David Brownelld2876d02008-02-04 22:28:20 -08003881 } else if (is_acpi_node(fwnode)) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09003882 struct acpi_gpio_info info;
David Brownelld2876d02008-02-04 22:28:20 -08003883
Linus Walleijd468bf92013-09-24 11:54:38 +02003884 desc = acpi_node_get_gpiod(fwnode, propname, index, &info);
Linus Walleij6392cca2017-12-29 02:07:54 +01003885 if (IS_ERR(desc))
3886 return desc;
3887
3888 acpi_gpio_update_gpiod_flags(&dflags, &info);
3889
3890 if (info.polarity == GPIO_ACTIVE_LOW)
3891 lflags |= GPIO_ACTIVE_LOW;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003892 }
3893
Linus Walleij6392cca2017-12-29 02:07:54 +01003894 /* Currently only ACPI takes this path */
Thierry Redingf9c4a312012-04-12 13:26:01 +02003895 ret = gpiod_request(desc, label);
3896 if (ret)
3897 return ERR_PTR(ret);
Linus Walleijd468bf92013-09-24 11:54:38 +02003898
Thierry Redingf9c4a312012-04-12 13:26:01 +02003899 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
3900 if (ret < 0) {
3901 gpiod_put(desc);
3902 return ERR_PTR(ret);
3903 }
3904
3905 return desc;
Linus Walleijd468bf92013-09-24 11:54:38 +02003906}
3907EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
David Brownelld2876d02008-02-04 22:28:20 -08003908
3909/**
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07003910 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
David Brownelld8f388d82008-07-25 01:46:07 -07003911 * function
Linus Walleijd468bf92013-09-24 11:54:38 +02003912 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3913 * @con_id: function within the GPIO consumer
David Brownelld2876d02008-02-04 22:28:20 -08003914 * @index: index of the GPIO to obtain in the consumer
3915 * @flags: optional GPIO initialization flags
3916 *
3917 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
3918 * specified index was assigned to the requested function it will return NULL.
3919 * This is convenient for drivers that need to handle optional GPIOs.
Grant Likely362432a2013-02-09 09:41:49 +00003920 */
David Brownelld8f388d82008-07-25 01:46:07 -07003921struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003922 const char *con_id,
Thierry Redingf9c4a312012-04-12 13:26:01 +02003923 unsigned int index,
3924 enum gpiod_flags flags)
3925{
Grant Likely362432a2013-02-09 09:41:49 +00003926 struct gpio_desc *desc;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003927
Grant Likely362432a2013-02-09 09:41:49 +00003928 desc = gpiod_get_index(dev, con_id, index, flags);
3929 if (IS_ERR(desc)) {
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003930 if (PTR_ERR(desc) == -ENOENT)
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003931 return NULL;
David Brownelld2876d02008-02-04 22:28:20 -08003932 }
3933
Benoit Parrotf625d462015-02-02 11:44:44 -06003934 return desc;
3935}
3936EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
3937
3938/**
3939 * gpiod_hog - Hog the specified GPIO desc given the provided flags
3940 * @desc: gpio whose value will be assigned
3941 * @name: gpio line name
3942 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3943 * of_get_gpio_hog()
3944 * @dflags: gpiod_flags - optional GPIO initialization flags
3945 */
3946int gpiod_hog(struct gpio_desc *desc, const char *name,
3947 unsigned long lflags, enum gpiod_flags dflags)
3948{
3949 struct gpio_chip *chip;
3950 struct gpio_desc *local_desc;
3951 int hwnum;
3952 int status;
3953
Laxman Dewanganc31a5712016-03-11 19:13:21 +05303954 chip = gpiod_to_chip(desc);
3955 hwnum = gpio_chip_hwgpio(desc);
3956
3957 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
Benoit Parrotf625d462015-02-02 11:44:44 -06003958 if (IS_ERR(local_desc)) {
3959 status = PTR_ERR(local_desc);
Johan Hovold85b03b32016-07-03 18:32:05 +02003960 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
Benoit Parrotf625d462015-02-02 11:44:44 -06003961 name, chip->label, hwnum, status);
Laxman Dewanganc31a5712016-03-11 19:13:21 +05303962 return status;
3963 }
Benoit Parrotf625d462015-02-02 11:44:44 -06003964
3965 status = gpiod_configure_flags(desc, name, lflags, dflags);
3966 if (status < 0) {
3967 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed, %d\n",
3968 name, chip->label, hwnum, status);
3969 gpiochip_free_own_desc(desc);
3970 return status;
3971 }
3972
3973 /* Mark GPIO as hogged so it can be identified and removed later */
3974 set_bit(FLAG_IS_HOGGED, &desc->flags);
3975
3976 pr_info("GPIO line %d (%s) hogged as %s%s\n",
3977 desc_to_gpio(desc), name,
3978 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
3979 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
3980 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
3981
3982 return 0;
3983}
3984
3985/**
3986 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
3987 * @chip: gpio chip to act on
3988 *
3989 * This is only used by of_gpiochip_remove to free hogged gpios
3990 */
Linus Walleij1c3cdb12016-02-09 13:51:59 +01003991static void gpiochip_free_hogs(struct gpio_chip *chip)
3992{
Benoit Parrotf625d462015-02-02 11:44:44 -06003993 int id;
3994
3995 for (id = 0; id < chip->ngpio; id++) {
3996 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003997 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
3998 }
3999}
4000
4001/**
4002 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
4003 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4004 * @con_id: function within the GPIO consumer
4005 * @flags: optional GPIO initialization flags
4006 *
4007 * This function acquires all the GPIOs defined under a given function.
4008 *
4009 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
4010 * no GPIO has been assigned to the requested function, or another IS_ERR()
4011 * code if an error occurred while trying to acquire the GPIOs.
4012 */
4013struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
4014 const char *con_id,
4015 enum gpiod_flags flags)
4016{
4017 struct gpio_desc *desc;
4018 struct gpio_descs *descs;
4019 int count;
4020
4021 count = gpiod_count(dev, con_id);
4022 if (count < 0)
4023 return ERR_PTR(count);
4024
4025 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
4026 GFP_KERNEL);
4027 if (!descs)
4028 return ERR_PTR(-ENOMEM);
4029
4030 for (descs->ndescs = 0; descs->ndescs < count; ) {
4031 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
4032 if (IS_ERR(desc)) {
4033 gpiod_put_array(descs);
4034 return ERR_CAST(desc);
4035 }
4036 descs->desc[descs->ndescs] = desc;
4037 descs->ndescs++;
4038 }
4039 return descs;
4040}
4041EXPORT_SYMBOL_GPL(gpiod_get_array);
4042
4043/**
4044 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
4045 * function
4046 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4047 * @con_id: function within the GPIO consumer
4048 * @flags: optional GPIO initialization flags
4049 *
4050 * This is equivalent to gpiod_get_array(), except that when no GPIO was
4051 * assigned to the requested function it will return NULL.
4052 */
4053struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
4054 const char *con_id,
4055 enum gpiod_flags flags)
4056{
4057 struct gpio_descs *descs;
4058
4059 descs = gpiod_get_array(dev, con_id, flags);
4060 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
4061 return NULL;
4062
David Brownelld2876d02008-02-04 22:28:20 -08004063 return descs;
4064}
4065EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
4066
4067/**
4068 * gpiod_put - dispose of a GPIO descriptor
4069 * @desc: GPIO descriptor to dispose of
4070 *
4071 * No descriptor can be used after gpiod_put() has been called on it.
4072 */
4073void gpiod_put(struct gpio_desc *desc)
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004074{
4075 gpiod_free(desc);
4076}
4077EXPORT_SYMBOL_GPL(gpiod_put);
4078
4079/**
4080 * gpiod_put_array - dispose of multiple GPIO descriptors
4081 * @descs: struct gpio_descs containing an array of descriptors
4082 */
4083void gpiod_put_array(struct gpio_descs *descs)
4084{
4085 unsigned int i;
4086
4087 for (i = 0; i < descs->ndescs; i++)
4088 gpiod_put(descs->desc[i]);
Linus Walleij3c702e92015-10-21 15:29:53 +02004089
4090 kfree(descs);
4091}
4092EXPORT_SYMBOL_GPL(gpiod_put_array);
4093
4094static int __init gpiolib_dev_init(void)
4095{
4096 int ret;
4097
4098 /* Register GPIO sysfs bus */
4099 ret = bus_register(&gpio_bus_type);
4100 if (ret < 0) {
4101 pr_err("gpiolib: could not register GPIO bus type\n");
4102 return ret;
4103 }
Guenter Roeck159f3cd2016-03-31 08:11:30 -07004104
4105 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
4106 if (ret < 0) {
Linus Walleij3c702e92015-10-21 15:29:53 +02004107 pr_err("gpiolib: failed to allocate char dev region\n");
4108 bus_unregister(&gpio_bus_type);
4109 } else {
4110 gpiolib_initialized = true;
4111 gpiochip_setup_devs();
David Brownelld2876d02008-02-04 22:28:20 -08004112 }
4113 return ret;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004114}
David Brownelld2876d02008-02-04 22:28:20 -08004115core_initcall(gpiolib_dev_init);
4116
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004117#ifdef CONFIG_DEBUG_FS
4118
4119static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
David Brownelld2876d02008-02-04 22:28:20 -08004120{
4121 unsigned i;
4122 struct gpio_chip *chip = gdev->chip;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004123 unsigned gpio = gdev->base;
Markus Pargmannced433e2015-08-14 16:11:02 +02004124 struct gpio_desc *gdesc = &gdev->descs[0];
4125 int is_out;
4126 int is_irq;
4127
4128 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
David Brownelld2876d02008-02-04 22:28:20 -08004129 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
Markus Pargmannced433e2015-08-14 16:11:02 +02004130 if (gdesc->name) {
David Brownelld2876d02008-02-04 22:28:20 -08004131 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
4132 gpio, gdesc->name);
4133 }
4134 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02004135 }
4136
David Brownelld2876d02008-02-04 22:28:20 -08004137 gpiod_get_direction(gdesc);
4138 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
4139 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
4140 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
4141 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
4142 is_out ? "out" : "in ",
4143 chip->get
4144 ? (chip->get(chip, i) ? "hi" : "lo")
4145 : "? ",
4146 is_irq ? "IRQ" : " ");
4147 seq_printf(s, "\n");
4148 }
Linus Walleijff2b1352015-10-20 11:10:38 +02004149}
David Brownelld2876d02008-02-04 22:28:20 -08004150
4151static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
4152{
4153 unsigned long flags;
4154 struct gpio_device *gdev = NULL;
Linus Walleijff2b1352015-10-20 11:10:38 +02004155 loff_t index = *pos;
David Brownelld2876d02008-02-04 22:28:20 -08004156
4157 s->private = "";
Linus Walleijff2b1352015-10-20 11:10:38 +02004158
David Brownelld2876d02008-02-04 22:28:20 -08004159 spin_lock_irqsave(&gpio_lock, flags);
4160 list_for_each_entry(gdev, &gpio_devices, list)
4161 if (index-- == 0) {
4162 spin_unlock_irqrestore(&gpio_lock, flags);
4163 return gdev;
4164 }
4165 spin_unlock_irqrestore(&gpio_lock, flags);
4166
4167 return NULL;
Linus Walleijff2b1352015-10-20 11:10:38 +02004168}
David Brownelld2876d02008-02-04 22:28:20 -08004169
4170static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
4171{
Linus Walleijff2b1352015-10-20 11:10:38 +02004172 unsigned long flags;
David Brownelld2876d02008-02-04 22:28:20 -08004173 struct gpio_device *gdev = v;
4174 void *ret = NULL;
Linus Walleijff2b1352015-10-20 11:10:38 +02004175
David Brownelld2876d02008-02-04 22:28:20 -08004176 spin_lock_irqsave(&gpio_lock, flags);
4177 if (list_is_last(&gdev->list, &gpio_devices))
4178 ret = NULL;
4179 else
4180 ret = list_entry(gdev->list.next, struct gpio_device, list);
4181 spin_unlock_irqrestore(&gpio_lock, flags);
4182
4183 s->private = "\n";
4184 ++*pos;
4185
4186 return ret;
4187}
4188
4189static void gpiolib_seq_stop(struct seq_file *s, void *v)
Linus Walleijff2b1352015-10-20 11:10:38 +02004190{
4191}
4192
David Brownelld2876d02008-02-04 22:28:20 -08004193static int gpiolib_seq_show(struct seq_file *s, void *v)
Linus Walleijff2b1352015-10-20 11:10:38 +02004194{
4195 struct gpio_device *gdev = v;
4196 struct gpio_chip *chip = gdev->chip;
4197 struct device *parent;
4198
4199 if (!chip) {
4200 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
4201 dev_name(&gdev->dev));
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004202 return 0;
Linus Walleijff2b1352015-10-20 11:10:38 +02004203 }
4204
4205 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
4206 dev_name(&gdev->dev),
4207 gdev->base, gdev->base + gdev->ngpio - 1);
David Brownelld2876d02008-02-04 22:28:20 -08004208 parent = chip->parent;
4209 if (parent)
4210 seq_printf(s, ", parent: %s/%s",
4211 parent->bus ? parent->bus->name : "no-bus",
4212 dev_name(parent));
4213 if (chip->label)
4214 seq_printf(s, ", %s", chip->label);
4215 if (chip->can_sleep)
4216 seq_printf(s, ", can sleep");
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004217 seq_printf(s, ":\n");
David Brownelld2876d02008-02-04 22:28:20 -08004218
4219 if (chip->dbg_show)
4220 chip->dbg_show(s, chip);
4221 else
4222 gpiolib_dbg_show(s, gdev);
4223
4224 return 0;
4225}
4226
4227static const struct seq_operations gpiolib_seq_ops = {
4228 .start = gpiolib_seq_start,
4229 .next = gpiolib_seq_next,
4230 .stop = gpiolib_seq_stop,
4231 .show = gpiolib_seq_show,
4232};
4233
4234static int gpiolib_open(struct inode *inode, struct file *file)
4235{
4236 return seq_open(file, &gpiolib_seq_ops);
4237}
4238
4239static const struct file_operations gpiolib_operations = {
4240 .owner = THIS_MODULE,
4241 .open = gpiolib_open,
4242 .read = seq_read,
4243 .llseek = seq_lseek,
4244 .release = seq_release,
4245};
4246
4247static int __init gpiolib_debugfs_init(void)
4248{
4249 /* /sys/kernel/debug/gpio */
4250 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
4251 NULL, NULL, &gpiolib_operations);
4252 return 0;
4253}
4254subsys_initcall(gpiolib_debugfs_init);
4255
4256#endif /* DEBUG_FS */