blob: d1171db66c30ccd6daa8d717aee25790a9bd2602 [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
Bartosz Golaszewskia411e812018-04-10 22:30:28 +020074static DEFINE_MUTEX(gpio_machine_hogs_mutex);
75static LIST_HEAD(gpio_machine_hogs);
76
Johan Hovold6d867502015-05-04 17:23:25 +020077static void gpiochip_free_hogs(struct gpio_chip *chip);
Thierry Reding959bc7b2017-11-07 19:15:59 +010078static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
Andrew Lunn39c3fd52017-12-02 18:11:04 +010079 struct lock_class_key *lock_key,
80 struct lock_class_key *request_key);
Johan Hovold6d867502015-05-04 17:23:25 +020081static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
Mika Westerberg79b804c2016-09-20 15:15:21 +030082static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip);
83static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip);
Johan Hovold6d867502015-05-04 17:23:25 +020084
Guenter Roeck159f3cd2016-03-31 08:11:30 -070085static bool gpiolib_initialized;
Johan Hovold6d867502015-05-04 17:23:25 +020086
David Brownelld2876d02008-02-04 22:28:20 -080087static inline void desc_set_label(struct gpio_desc *d, const char *label)
88{
David Brownelld2876d02008-02-04 22:28:20 -080089 d->label = label;
David Brownelld2876d02008-02-04 22:28:20 -080090}
91
Alexandre Courbot372e7222013-02-03 01:29:29 +090092/**
Thierry Reding950d55f52017-07-24 16:57:22 +020093 * gpio_to_desc - Convert a GPIO number to its descriptor
94 * @gpio: global GPIO number
95 *
96 * Returns:
97 * The GPIO descriptor associated with the given GPIO, or %NULL if no GPIO
98 * with the given number exists in the system.
Alexandre Courbot372e7222013-02-03 01:29:29 +090099 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700100struct gpio_desc *gpio_to_desc(unsigned gpio)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900101{
Linus Walleijff2b1352015-10-20 11:10:38 +0200102 struct gpio_device *gdev;
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900103 unsigned long flags;
104
105 spin_lock_irqsave(&gpio_lock, flags);
106
Linus Walleijff2b1352015-10-20 11:10:38 +0200107 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100108 if (gdev->base <= gpio &&
109 gdev->base + gdev->ngpio > gpio) {
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900110 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100111 return &gdev->descs[gpio - gdev->base];
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900112 }
113 }
114
115 spin_unlock_irqrestore(&gpio_lock, flags);
116
Alexandre Courbot0e9a5ed2014-12-02 23:15:05 +0900117 if (!gpio_is_valid(gpio))
118 WARN(1, "invalid GPIO %d\n", gpio);
119
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900120 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900121}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700122EXPORT_SYMBOL_GPL(gpio_to_desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900123
124/**
Thierry Reding950d55f52017-07-24 16:57:22 +0200125 * gpiochip_get_desc - get the GPIO descriptor corresponding to the given
126 * hardware number for this chip
127 * @chip: GPIO chip
128 * @hwnum: hardware number of the GPIO for this chip
129 *
130 * Returns:
131 * A pointer to the GPIO descriptor or %ERR_PTR(-EINVAL) if no GPIO exists
132 * in the given chip for the specified hardware number.
Linus Walleijd468bf92013-09-24 11:54:38 +0200133 */
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900134struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
135 u16 hwnum)
Linus Walleijd468bf92013-09-24 11:54:38 +0200136{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100137 struct gpio_device *gdev = chip->gpiodev;
138
139 if (hwnum >= gdev->ngpio)
Alexandre Courbotb7d0a282013-12-03 12:31:11 +0900140 return ERR_PTR(-EINVAL);
Linus Walleijd468bf92013-09-24 11:54:38 +0200141
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100142 return &gdev->descs[hwnum];
Linus Walleijd468bf92013-09-24 11:54:38 +0200143}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900144
145/**
Thierry Reding950d55f52017-07-24 16:57:22 +0200146 * desc_to_gpio - convert a GPIO descriptor to the integer namespace
147 * @desc: GPIO descriptor
148 *
Alexandre Courbot372e7222013-02-03 01:29:29 +0900149 * This should disappear in the future but is needed since we still
Thierry Reding950d55f52017-07-24 16:57:22 +0200150 * use GPIO numbers for error messages and sysfs nodes.
151 *
152 * Returns:
153 * The global GPIO number for the GPIO specified by its descriptor.
Alexandre Courbot372e7222013-02-03 01:29:29 +0900154 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700155int desc_to_gpio(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900156{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100157 return desc->gdev->base + (desc - &desc->gdev->descs[0]);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900158}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700159EXPORT_SYMBOL_GPL(desc_to_gpio);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900160
161
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700162/**
163 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
164 * @desc: descriptor to return the chip of
165 */
166struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900167{
Vladimir Zapolskiydd3b9a42017-12-21 18:37:30 +0200168 if (!desc || !desc->gdev)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100169 return NULL;
170 return desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900171}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700172EXPORT_SYMBOL_GPL(gpiod_to_chip);
David Brownelld2876d02008-02-04 22:28:20 -0800173
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700174/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
175static int gpiochip_find_base(int ngpio)
176{
Linus Walleijff2b1352015-10-20 11:10:38 +0200177 struct gpio_device *gdev;
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900178 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700179
Linus Walleijff2b1352015-10-20 11:10:38 +0200180 list_for_each_entry_reverse(gdev, &gpio_devices, list) {
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900181 /* found a free space? */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100182 if (gdev->base + gdev->ngpio <= base)
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900183 break;
184 else
185 /* nope, check the space right before the chip */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100186 base = gdev->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700187 }
188
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900189 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700190 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900191 return base;
192 } else {
193 pr_err("%s: cannot find free range\n", __func__);
194 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700195 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700196}
197
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700198/**
199 * gpiod_get_direction - return the current direction of a GPIO
200 * @desc: GPIO to get the direction of
201 *
Wolfram Sang94fc7302018-01-09 12:35:53 +0100202 * Returns 0 for output, 1 for input, or an error code in case of error.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700203 *
204 * This function may sleep if gpiod_cansleep() is true.
205 */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900206int gpiod_get_direction(struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300207{
208 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900209 unsigned offset;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300210 int status = -EINVAL;
211
Alexandre Courbot372e7222013-02-03 01:29:29 +0900212 chip = gpiod_to_chip(desc);
213 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300214
215 if (!chip->get_direction)
216 return status;
217
Alexandre Courbot372e7222013-02-03 01:29:29 +0900218 status = chip->get_direction(chip, offset);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300219 if (status > 0) {
220 /* GPIOF_DIR_IN, or other positive */
221 status = 1;
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900222 clear_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300223 }
224 if (status == 0) {
225 /* GPIOF_DIR_OUT */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900226 set_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300227 }
228 return status;
229}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700230EXPORT_SYMBOL_GPL(gpiod_get_direction);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300231
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900232/*
233 * Add a new chip to the global chips list, keeping the list of chips sorted
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800234 * by range(means [base, base + ngpio - 1]) order.
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900235 *
236 * Return -EBUSY if the new chip overlaps with some other chip's integer
237 * space.
238 */
Linus Walleijff2b1352015-10-20 11:10:38 +0200239static int gpiodev_add_to_list(struct gpio_device *gdev)
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900240{
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800241 struct gpio_device *prev, *next;
Linus Walleijff2b1352015-10-20 11:10:38 +0200242
243 if (list_empty(&gpio_devices)) {
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800244 /* initial entry in list */
Linus Walleijff2b1352015-10-20 11:10:38 +0200245 list_add_tail(&gdev->list, &gpio_devices);
Sudip Mukherjeee28ecca2015-12-27 19:06:50 +0530246 return 0;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900247 }
248
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800249 next = list_entry(gpio_devices.next, struct gpio_device, list);
250 if (gdev->base + gdev->ngpio <= next->base) {
251 /* add before first entry */
252 list_add(&gdev->list, &gpio_devices);
Julien Grossholtz96098df2016-01-07 16:46:45 -0500253 return 0;
254 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900255
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800256 prev = list_entry(gpio_devices.prev, struct gpio_device, list);
257 if (prev->base + prev->ngpio <= gdev->base) {
258 /* add behind last entry */
259 list_add_tail(&gdev->list, &gpio_devices);
260 return 0;
261 }
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800262
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800263 list_for_each_entry_safe(prev, next, &gpio_devices, list) {
264 /* at the end of the list */
265 if (&next->list == &gpio_devices)
266 break;
267
268 /* add between prev and next */
269 if (prev->base + prev->ngpio <= gdev->base
270 && gdev->base + gdev->ngpio <= next->base) {
271 list_add(&gdev->list, &prev->list);
272 return 0;
273 }
274 }
275
276 dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n");
277 return -EBUSY;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900278}
279
Thierry Reding950d55f52017-07-24 16:57:22 +0200280/*
Linus Walleijf881bab2015-09-23 16:20:43 -0700281 * Convert a GPIO name to its descriptor
282 */
283static struct gpio_desc *gpio_name_to_desc(const char * const name)
284{
Linus Walleijff2b1352015-10-20 11:10:38 +0200285 struct gpio_device *gdev;
Linus Walleijf881bab2015-09-23 16:20:43 -0700286 unsigned long flags;
287
288 spin_lock_irqsave(&gpio_lock, flags);
289
Linus Walleijff2b1352015-10-20 11:10:38 +0200290 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijf881bab2015-09-23 16:20:43 -0700291 int i;
292
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100293 for (i = 0; i != gdev->ngpio; ++i) {
294 struct gpio_desc *desc = &gdev->descs[i];
Linus Walleijf881bab2015-09-23 16:20:43 -0700295
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100296 if (!desc->name || !name)
Linus Walleijf881bab2015-09-23 16:20:43 -0700297 continue;
298
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100299 if (!strcmp(desc->name, name)) {
Linus Walleijf881bab2015-09-23 16:20:43 -0700300 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100301 return desc;
Linus Walleijf881bab2015-09-23 16:20:43 -0700302 }
303 }
304 }
305
306 spin_unlock_irqrestore(&gpio_lock, flags);
307
308 return NULL;
309}
310
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200311/*
312 * Takes the names from gc->names and checks if they are all unique. If they
313 * are, they are assigned to their gpio descriptors.
314 *
Bamvor Jian Zhanged379152015-11-14 16:43:20 +0800315 * Warning if one of the names is already used for a different GPIO.
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200316 */
317static int gpiochip_set_desc_names(struct gpio_chip *gc)
318{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100319 struct gpio_device *gdev = gc->gpiodev;
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200320 int i;
321
322 if (!gc->names)
323 return 0;
324
325 /* First check all names if they are unique */
326 for (i = 0; i != gc->ngpio; ++i) {
327 struct gpio_desc *gpio;
328
329 gpio = gpio_name_to_desc(gc->names[i]);
Linus Walleijf881bab2015-09-23 16:20:43 -0700330 if (gpio)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100331 dev_warn(&gdev->dev,
Linus Walleij34ffd852015-10-20 11:31:54 +0200332 "Detected name collision for GPIO name '%s'\n",
Linus Walleijf881bab2015-09-23 16:20:43 -0700333 gc->names[i]);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200334 }
335
336 /* Then add all names to the GPIO descriptors */
337 for (i = 0; i != gc->ngpio; ++i)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100338 gdev->descs[i].name = gc->names[i];
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200339
340 return 0;
341}
342
Stephen Boyde4371f6e2018-03-23 09:34:50 -0700343static unsigned long *gpiochip_allocate_mask(struct gpio_chip *chip)
344{
345 unsigned long *p;
346
Stephen Boydace569352018-03-23 09:34:51 -0700347 p = kmalloc_array(BITS_TO_LONGS(chip->ngpio), sizeof(*p), GFP_KERNEL);
Stephen Boyde4371f6e2018-03-23 09:34:50 -0700348 if (!p)
349 return NULL;
350
351 /* Assume by default all GPIOs are valid */
352 bitmap_fill(p, chip->ngpio);
353
354 return p;
355}
356
Stephen Boyd726cb3b2018-03-23 09:34:52 -0700357static int gpiochip_init_valid_mask(struct gpio_chip *gpiochip)
358{
359#ifdef CONFIG_OF_GPIO
360 int size;
361 struct device_node *np = gpiochip->of_node;
362
363 size = of_property_count_u32_elems(np, "gpio-reserved-ranges");
364 if (size > 0 && size % 2 == 0)
365 gpiochip->need_valid_mask = true;
366#endif
367
368 if (!gpiochip->need_valid_mask)
369 return 0;
370
371 gpiochip->valid_mask = gpiochip_allocate_mask(gpiochip);
372 if (!gpiochip->valid_mask)
373 return -ENOMEM;
374
375 return 0;
376}
377
378static void gpiochip_free_valid_mask(struct gpio_chip *gpiochip)
379{
380 kfree(gpiochip->valid_mask);
381 gpiochip->valid_mask = NULL;
382}
383
384bool gpiochip_line_is_valid(const struct gpio_chip *gpiochip,
385 unsigned int offset)
386{
387 /* No mask means all valid */
388 if (likely(!gpiochip->valid_mask))
389 return true;
390 return test_bit(offset, gpiochip->valid_mask);
391}
392EXPORT_SYMBOL_GPL(gpiochip_line_is_valid);
393
Linus Walleijd7c51b42016-04-26 10:35:29 +0200394/*
395 * GPIO line handle management
396 */
397
398/**
399 * struct linehandle_state - contains the state of a userspace handle
400 * @gdev: the GPIO device the handle pertains to
401 * @label: consumer label used to tag descriptors
402 * @descs: the GPIO descriptors held by this handle
403 * @numdescs: the number of descriptors held in the descs array
404 */
405struct linehandle_state {
406 struct gpio_device *gdev;
407 const char *label;
408 struct gpio_desc *descs[GPIOHANDLES_MAX];
409 u32 numdescs;
410};
411
Lars-Peter Clausene3e847c2016-10-18 16:54:05 +0200412#define GPIOHANDLE_REQUEST_VALID_FLAGS \
413 (GPIOHANDLE_REQUEST_INPUT | \
414 GPIOHANDLE_REQUEST_OUTPUT | \
415 GPIOHANDLE_REQUEST_ACTIVE_LOW | \
416 GPIOHANDLE_REQUEST_OPEN_DRAIN | \
417 GPIOHANDLE_REQUEST_OPEN_SOURCE)
418
Linus Walleijd7c51b42016-04-26 10:35:29 +0200419static long linehandle_ioctl(struct file *filep, unsigned int cmd,
420 unsigned long arg)
421{
422 struct linehandle_state *lh = filep->private_data;
423 void __user *ip = (void __user *)arg;
424 struct gpiohandle_data ghd;
Lukas Wunnereec1d562017-10-12 12:40:10 +0200425 int vals[GPIOHANDLES_MAX];
Linus Walleijd7c51b42016-04-26 10:35:29 +0200426 int i;
427
428 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
Lukas Wunnereec1d562017-10-12 12:40:10 +0200429 /* TODO: check if descriptors are really input */
430 int ret = gpiod_get_array_value_complex(false,
431 true,
432 lh->numdescs,
433 lh->descs,
434 vals);
435 if (ret)
436 return ret;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200437
Lars-Peter Clausen3eded5d2016-10-18 16:54:02 +0200438 memset(&ghd, 0, sizeof(ghd));
Lukas Wunnereec1d562017-10-12 12:40:10 +0200439 for (i = 0; i < lh->numdescs; i++)
440 ghd.values[i] = vals[i];
Linus Walleijd7c51b42016-04-26 10:35:29 +0200441
442 if (copy_to_user(ip, &ghd, sizeof(ghd)))
443 return -EFAULT;
444
445 return 0;
446 } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
Linus Walleijd7c51b42016-04-26 10:35:29 +0200447 /* TODO: check if descriptors are really output */
448 if (copy_from_user(&ghd, ip, sizeof(ghd)))
449 return -EFAULT;
450
451 /* Clamp all values to [0,1] */
452 for (i = 0; i < lh->numdescs; i++)
453 vals[i] = !!ghd.values[i];
454
455 /* Reuse the array setting function */
456 gpiod_set_array_value_complex(false,
457 true,
458 lh->numdescs,
459 lh->descs,
460 vals);
461 return 0;
462 }
463 return -EINVAL;
464}
465
466#ifdef CONFIG_COMPAT
467static long linehandle_ioctl_compat(struct file *filep, unsigned int cmd,
468 unsigned long arg)
469{
470 return linehandle_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
471}
472#endif
473
474static int linehandle_release(struct inode *inode, struct file *filep)
475{
476 struct linehandle_state *lh = filep->private_data;
477 struct gpio_device *gdev = lh->gdev;
478 int i;
479
480 for (i = 0; i < lh->numdescs; i++)
481 gpiod_free(lh->descs[i]);
482 kfree(lh->label);
483 kfree(lh);
484 put_device(&gdev->dev);
485 return 0;
486}
487
488static const struct file_operations linehandle_fileops = {
489 .release = linehandle_release,
490 .owner = THIS_MODULE,
491 .llseek = noop_llseek,
492 .unlocked_ioctl = linehandle_ioctl,
493#ifdef CONFIG_COMPAT
494 .compat_ioctl = linehandle_ioctl_compat,
495#endif
496};
497
498static int linehandle_create(struct gpio_device *gdev, void __user *ip)
499{
500 struct gpiohandle_request handlereq;
501 struct linehandle_state *lh;
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200502 struct file *file;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200503 int fd, i, ret;
Bartosz Golaszewski418ee8e2017-10-16 11:32:29 +0200504 u32 lflags;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200505
506 if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
507 return -EFAULT;
508 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
509 return -EINVAL;
510
Bartosz Golaszewski418ee8e2017-10-16 11:32:29 +0200511 lflags = handlereq.flags;
512
513 /* Return an error if an unknown flag is set */
514 if (lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
515 return -EINVAL;
516
Bartosz Golaszewski588fc3b2017-11-15 16:47:43 +0100517 /*
518 * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
519 * the hardware actually supports enabling both at the same time the
520 * electrical result would be disastrous.
521 */
522 if ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) &&
523 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
524 return -EINVAL;
525
Bartosz Golaszewski609aaf62017-10-16 11:32:30 +0200526 /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
527 if (!(lflags & GPIOHANDLE_REQUEST_OUTPUT) &&
528 ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
529 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
530 return -EINVAL;
531
Linus Walleijd7c51b42016-04-26 10:35:29 +0200532 lh = kzalloc(sizeof(*lh), GFP_KERNEL);
533 if (!lh)
534 return -ENOMEM;
535 lh->gdev = gdev;
536 get_device(&gdev->dev);
537
538 /* Make sure this is terminated */
539 handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
540 if (strlen(handlereq.consumer_label)) {
541 lh->label = kstrdup(handlereq.consumer_label,
542 GFP_KERNEL);
543 if (!lh->label) {
544 ret = -ENOMEM;
545 goto out_free_lh;
546 }
547 }
548
549 /* Request each GPIO */
550 for (i = 0; i < handlereq.lines; i++) {
551 u32 offset = handlereq.lineoffsets[i];
Linus Walleijd7c51b42016-04-26 10:35:29 +0200552 struct gpio_desc *desc;
553
Lars-Peter Clausene405f9f2016-10-18 16:54:01 +0200554 if (offset >= gdev->ngpio) {
555 ret = -EINVAL;
556 goto out_free_descs;
557 }
558
Linus Walleijd7c51b42016-04-26 10:35:29 +0200559 desc = &gdev->descs[offset];
560 ret = gpiod_request(desc, lh->label);
561 if (ret)
562 goto out_free_descs;
563 lh->descs[i] = desc;
564
565 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
566 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
567 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
568 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
569 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
570 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
571
Andrew Jefferye10f72b2017-11-30 14:25:24 +1030572 ret = gpiod_set_transitory(desc, false);
573 if (ret < 0)
574 goto out_free_descs;
575
Linus Walleijd7c51b42016-04-26 10:35:29 +0200576 /*
577 * Lines have to be requested explicitly for input
578 * or output, else the line will be treated "as is".
579 */
580 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
581 int val = !!handlereq.default_values[i];
582
583 ret = gpiod_direction_output(desc, val);
584 if (ret)
585 goto out_free_descs;
586 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
587 ret = gpiod_direction_input(desc);
588 if (ret)
589 goto out_free_descs;
590 }
591 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
592 offset);
593 }
Linus Walleije2f608b2016-06-18 10:56:43 +0200594 /* Let i point at the last handle */
595 i--;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200596 lh->numdescs = handlereq.lines;
597
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200598 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200599 if (fd < 0) {
600 ret = fd;
601 goto out_free_descs;
602 }
603
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200604 file = anon_inode_getfile("gpio-linehandle",
605 &linehandle_fileops,
606 lh,
607 O_RDONLY | O_CLOEXEC);
608 if (IS_ERR(file)) {
609 ret = PTR_ERR(file);
610 goto out_put_unused_fd;
611 }
612
Linus Walleijd7c51b42016-04-26 10:35:29 +0200613 handlereq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200614 if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200615 /*
616 * fput() will trigger the release() callback, so do not go onto
617 * the regular error cleanup path here.
618 */
619 fput(file);
620 put_unused_fd(fd);
621 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +0200622 }
Linus Walleijd7c51b42016-04-26 10:35:29 +0200623
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200624 fd_install(fd, file);
625
Linus Walleijd7c51b42016-04-26 10:35:29 +0200626 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
627 lh->numdescs);
628
629 return 0;
630
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200631out_put_unused_fd:
632 put_unused_fd(fd);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200633out_free_descs:
634 for (; i >= 0; i--)
635 gpiod_free(lh->descs[i]);
636 kfree(lh->label);
637out_free_lh:
638 kfree(lh);
639 put_device(&gdev->dev);
640 return ret;
641}
642
Linus Walleij61f922d2016-06-02 11:30:15 +0200643/*
644 * GPIO line event management
645 */
646
647/**
648 * struct lineevent_state - contains the state of a userspace event
649 * @gdev: the GPIO device the event pertains to
650 * @label: consumer label used to tag descriptors
651 * @desc: the GPIO descriptor held by this event
652 * @eflags: the event flags this line was requested with
653 * @irq: the interrupt that trigger in response to events on this GPIO
654 * @wait: wait queue that handles blocking reads of events
655 * @events: KFIFO for the GPIO events
656 * @read_lock: mutex lock to protect reads from colliding with adding
657 * new events to the FIFO
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100658 * @timestamp: cache for the timestamp storing it between hardirq
659 * and IRQ thread, used to bring the timestamp close to the actual
660 * event
Linus Walleij61f922d2016-06-02 11:30:15 +0200661 */
662struct lineevent_state {
663 struct gpio_device *gdev;
664 const char *label;
665 struct gpio_desc *desc;
666 u32 eflags;
667 int irq;
668 wait_queue_head_t wait;
669 DECLARE_KFIFO(events, struct gpioevent_data, 16);
670 struct mutex read_lock;
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100671 u64 timestamp;
Linus Walleij61f922d2016-06-02 11:30:15 +0200672};
673
Lars-Peter Clausenac7dbb92016-10-18 16:54:06 +0200674#define GPIOEVENT_REQUEST_VALID_FLAGS \
675 (GPIOEVENT_REQUEST_RISING_EDGE | \
676 GPIOEVENT_REQUEST_FALLING_EDGE)
677
Al Viroafc9a422017-07-03 06:39:46 -0400678static __poll_t lineevent_poll(struct file *filep,
Linus Walleij61f922d2016-06-02 11:30:15 +0200679 struct poll_table_struct *wait)
680{
681 struct lineevent_state *le = filep->private_data;
Al Viroafc9a422017-07-03 06:39:46 -0400682 __poll_t events = 0;
Linus Walleij61f922d2016-06-02 11:30:15 +0200683
684 poll_wait(filep, &le->wait, wait);
685
686 if (!kfifo_is_empty(&le->events))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800687 events = EPOLLIN | EPOLLRDNORM;
Linus Walleij61f922d2016-06-02 11:30:15 +0200688
689 return events;
690}
691
692
693static ssize_t lineevent_read(struct file *filep,
694 char __user *buf,
695 size_t count,
696 loff_t *f_ps)
697{
698 struct lineevent_state *le = filep->private_data;
699 unsigned int copied;
700 int ret;
701
702 if (count < sizeof(struct gpioevent_data))
703 return -EINVAL;
704
705 do {
706 if (kfifo_is_empty(&le->events)) {
707 if (filep->f_flags & O_NONBLOCK)
708 return -EAGAIN;
709
710 ret = wait_event_interruptible(le->wait,
711 !kfifo_is_empty(&le->events));
712 if (ret)
713 return ret;
714 }
715
716 if (mutex_lock_interruptible(&le->read_lock))
717 return -ERESTARTSYS;
718 ret = kfifo_to_user(&le->events, buf, count, &copied);
719 mutex_unlock(&le->read_lock);
720
721 if (ret)
722 return ret;
723
724 /*
725 * If we couldn't read anything from the fifo (a different
726 * thread might have been faster) we either return -EAGAIN if
727 * the file descriptor is non-blocking, otherwise we go back to
728 * sleep and wait for more data to arrive.
729 */
730 if (copied == 0 && (filep->f_flags & O_NONBLOCK))
731 return -EAGAIN;
732
733 } while (copied == 0);
734
735 return copied;
736}
737
738static int lineevent_release(struct inode *inode, struct file *filep)
739{
740 struct lineevent_state *le = filep->private_data;
741 struct gpio_device *gdev = le->gdev;
742
743 free_irq(le->irq, le);
744 gpiod_free(le->desc);
745 kfree(le->label);
746 kfree(le);
747 put_device(&gdev->dev);
748 return 0;
749}
750
751static long lineevent_ioctl(struct file *filep, unsigned int cmd,
752 unsigned long arg)
753{
754 struct lineevent_state *le = filep->private_data;
755 void __user *ip = (void __user *)arg;
756 struct gpiohandle_data ghd;
757
758 /*
759 * We can get the value for an event line but not set it,
760 * because it is input by definition.
761 */
762 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
763 int val;
764
Lars-Peter Clausend82aa4a2016-10-18 16:54:04 +0200765 memset(&ghd, 0, sizeof(ghd));
766
Linus Walleij61f922d2016-06-02 11:30:15 +0200767 val = gpiod_get_value_cansleep(le->desc);
768 if (val < 0)
769 return val;
770 ghd.values[0] = val;
771
772 if (copy_to_user(ip, &ghd, sizeof(ghd)))
773 return -EFAULT;
774
775 return 0;
776 }
777 return -EINVAL;
778}
779
780#ifdef CONFIG_COMPAT
781static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd,
782 unsigned long arg)
783{
784 return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
785}
786#endif
787
788static const struct file_operations lineevent_fileops = {
789 .release = lineevent_release,
790 .read = lineevent_read,
791 .poll = lineevent_poll,
792 .owner = THIS_MODULE,
793 .llseek = noop_llseek,
794 .unlocked_ioctl = lineevent_ioctl,
795#ifdef CONFIG_COMPAT
796 .compat_ioctl = lineevent_ioctl_compat,
797#endif
798};
799
Ben Dooks33265b12016-06-17 16:03:13 +0100800static irqreturn_t lineevent_irq_thread(int irq, void *p)
Linus Walleij61f922d2016-06-02 11:30:15 +0200801{
802 struct lineevent_state *le = p;
803 struct gpioevent_data ge;
Bartosz Golaszewskidf1e76f2017-07-03 11:12:03 +0200804 int ret, level;
Linus Walleij61f922d2016-06-02 11:30:15 +0200805
Linus Walleij24bd3ef2018-01-22 13:19:28 +0100806 /* Do not leak kernel stack to userspace */
807 memset(&ge, 0, sizeof(ge));
808
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100809 ge.timestamp = le->timestamp;
Bartosz Golaszewskidf1e76f2017-07-03 11:12:03 +0200810 level = gpiod_get_value_cansleep(le->desc);
Linus Walleij61f922d2016-06-02 11:30:15 +0200811
Bartosz Golaszewskiad537b82017-06-23 13:45:16 +0200812 if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
813 && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200814 if (level)
815 /* Emit low-to-high event */
816 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
817 else
818 /* Emit high-to-low event */
819 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Bartosz Golaszewskidf1e76f2017-07-03 11:12:03 +0200820 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE && level) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200821 /* Emit low-to-high event */
822 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
Bartosz Golaszewskidf1e76f2017-07-03 11:12:03 +0200823 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE && !level) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200824 /* Emit high-to-low event */
825 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Arnd Bergmannbc0207a2016-06-16 11:02:41 +0200826 } else {
827 return IRQ_NONE;
Linus Walleij61f922d2016-06-02 11:30:15 +0200828 }
829
830 ret = kfifo_put(&le->events, ge);
831 if (ret != 0)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800832 wake_up_poll(&le->wait, EPOLLIN);
Linus Walleij61f922d2016-06-02 11:30:15 +0200833
834 return IRQ_HANDLED;
835}
836
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100837static irqreturn_t lineevent_irq_handler(int irq, void *p)
838{
839 struct lineevent_state *le = p;
840
841 /*
842 * Just store the timestamp in hardirq context so we get it as
843 * close in time as possible to the actual event.
844 */
845 le->timestamp = ktime_get_real_ns();
846
847 return IRQ_WAKE_THREAD;
848}
849
Linus Walleij61f922d2016-06-02 11:30:15 +0200850static int lineevent_create(struct gpio_device *gdev, void __user *ip)
851{
852 struct gpioevent_request eventreq;
853 struct lineevent_state *le;
854 struct gpio_desc *desc;
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200855 struct file *file;
Linus Walleij61f922d2016-06-02 11:30:15 +0200856 u32 offset;
857 u32 lflags;
858 u32 eflags;
859 int fd;
860 int ret;
861 int irqflags = 0;
862
863 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
864 return -EFAULT;
865
866 le = kzalloc(sizeof(*le), GFP_KERNEL);
867 if (!le)
868 return -ENOMEM;
869 le->gdev = gdev;
870 get_device(&gdev->dev);
871
872 /* Make sure this is terminated */
873 eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
874 if (strlen(eventreq.consumer_label)) {
875 le->label = kstrdup(eventreq.consumer_label,
876 GFP_KERNEL);
877 if (!le->label) {
878 ret = -ENOMEM;
879 goto out_free_le;
880 }
881 }
882
883 offset = eventreq.lineoffset;
884 lflags = eventreq.handleflags;
885 eflags = eventreq.eventflags;
886
Lars-Peter Clausenb8b0e3d2016-10-18 16:54:03 +0200887 if (offset >= gdev->ngpio) {
888 ret = -EINVAL;
889 goto out_free_label;
890 }
891
Lars-Peter Clausenac7dbb92016-10-18 16:54:06 +0200892 /* Return an error if a unknown flag is set */
893 if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
894 (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) {
895 ret = -EINVAL;
896 goto out_free_label;
897 }
898
Linus Walleij61f922d2016-06-02 11:30:15 +0200899 /* This is just wrong: we don't look for events on output lines */
900 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
901 ret = -EINVAL;
902 goto out_free_label;
903 }
904
905 desc = &gdev->descs[offset];
906 ret = gpiod_request(desc, le->label);
907 if (ret)
908 goto out_free_desc;
909 le->desc = desc;
910 le->eflags = eflags;
911
912 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
913 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
914 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
915 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
916 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
917 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
918
919 ret = gpiod_direction_input(desc);
920 if (ret)
921 goto out_free_desc;
922
923 le->irq = gpiod_to_irq(desc);
924 if (le->irq <= 0) {
925 ret = -ENODEV;
926 goto out_free_desc;
927 }
928
929 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
930 irqflags |= IRQF_TRIGGER_RISING;
931 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
932 irqflags |= IRQF_TRIGGER_FALLING;
933 irqflags |= IRQF_ONESHOT;
934 irqflags |= IRQF_SHARED;
935
936 INIT_KFIFO(le->events);
937 init_waitqueue_head(&le->wait);
938 mutex_init(&le->read_lock);
939
940 /* Request a thread to read the events */
941 ret = request_threaded_irq(le->irq,
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100942 lineevent_irq_handler,
Linus Walleij61f922d2016-06-02 11:30:15 +0200943 lineevent_irq_thread,
944 irqflags,
945 le->label,
946 le);
947 if (ret)
948 goto out_free_desc;
949
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200950 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleij61f922d2016-06-02 11:30:15 +0200951 if (fd < 0) {
952 ret = fd;
953 goto out_free_irq;
954 }
955
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200956 file = anon_inode_getfile("gpio-event",
957 &lineevent_fileops,
958 le,
959 O_RDONLY | O_CLOEXEC);
960 if (IS_ERR(file)) {
961 ret = PTR_ERR(file);
962 goto out_put_unused_fd;
963 }
964
Linus Walleij61f922d2016-06-02 11:30:15 +0200965 eventreq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200966 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200967 /*
968 * fput() will trigger the release() callback, so do not go onto
969 * the regular error cleanup path here.
970 */
971 fput(file);
972 put_unused_fd(fd);
973 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +0200974 }
Linus Walleij61f922d2016-06-02 11:30:15 +0200975
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200976 fd_install(fd, file);
977
Linus Walleij61f922d2016-06-02 11:30:15 +0200978 return 0;
979
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200980out_put_unused_fd:
981 put_unused_fd(fd);
Linus Walleij61f922d2016-06-02 11:30:15 +0200982out_free_irq:
983 free_irq(le->irq, le);
984out_free_desc:
985 gpiod_free(le->desc);
986out_free_label:
987 kfree(le->label);
988out_free_le:
989 kfree(le);
990 put_device(&gdev->dev);
991 return ret;
992}
993
Thierry Reding950d55f52017-07-24 16:57:22 +0200994/*
Linus Walleij3c702e92015-10-21 15:29:53 +0200995 * gpio_ioctl() - ioctl handler for the GPIO chardev
996 */
997static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
998{
999 struct gpio_device *gdev = filp->private_data;
1000 struct gpio_chip *chip = gdev->chip;
Linus Walleij8b92e172016-05-27 14:24:04 +02001001 void __user *ip = (void __user *)arg;
Linus Walleij3c702e92015-10-21 15:29:53 +02001002
1003 /* We fail any subsequent ioctl():s when the chip is gone */
1004 if (!chip)
1005 return -ENODEV;
1006
Linus Walleij521a2ad2016-02-12 22:25:22 +01001007 /* Fill in the struct and pass to userspace */
Linus Walleij3c702e92015-10-21 15:29:53 +02001008 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
Linus Walleij521a2ad2016-02-12 22:25:22 +01001009 struct gpiochip_info chipinfo;
1010
Lars-Peter Clausen0f4bbb22016-10-18 16:54:00 +02001011 memset(&chipinfo, 0, sizeof(chipinfo));
1012
Linus Walleij3c702e92015-10-21 15:29:53 +02001013 strncpy(chipinfo.name, dev_name(&gdev->dev),
1014 sizeof(chipinfo.name));
1015 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
Linus Walleijdf4878e2016-02-12 14:48:23 +01001016 strncpy(chipinfo.label, gdev->label,
1017 sizeof(chipinfo.label));
1018 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001019 chipinfo.lines = gdev->ngpio;
Linus Walleij3c702e92015-10-21 15:29:53 +02001020 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
1021 return -EFAULT;
1022 return 0;
Linus Walleij521a2ad2016-02-12 22:25:22 +01001023 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
1024 struct gpioline_info lineinfo;
1025 struct gpio_desc *desc;
1026
1027 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
1028 return -EFAULT;
Lars-Peter Clausen1f1cc452016-10-18 16:53:59 +02001029 if (lineinfo.line_offset >= gdev->ngpio)
Linus Walleij521a2ad2016-02-12 22:25:22 +01001030 return -EINVAL;
1031
1032 desc = &gdev->descs[lineinfo.line_offset];
1033 if (desc->name) {
1034 strncpy(lineinfo.name, desc->name,
1035 sizeof(lineinfo.name));
1036 lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
1037 } else {
1038 lineinfo.name[0] = '\0';
1039 }
1040 if (desc->label) {
Linus Walleij214338e2016-02-25 21:01:48 +01001041 strncpy(lineinfo.consumer, desc->label,
1042 sizeof(lineinfo.consumer));
1043 lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +01001044 } else {
Linus Walleij214338e2016-02-25 21:01:48 +01001045 lineinfo.consumer[0] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +01001046 }
1047
1048 /*
1049 * Userspace only need to know that the kernel is using
1050 * this GPIO so it can't use it.
1051 */
1052 lineinfo.flags = 0;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001053 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
1054 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
1055 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
1056 test_bit(FLAG_EXPORT, &desc->flags) ||
1057 test_bit(FLAG_SYSFS, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001058 lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001059 if (test_bit(FLAG_IS_OUT, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001060 lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001061 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001062 lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001063 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001064 lineinfo.flags |= GPIOLINE_FLAG_OPEN_DRAIN;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001065 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001066 lineinfo.flags |= GPIOLINE_FLAG_OPEN_SOURCE;
1067
1068 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
1069 return -EFAULT;
1070 return 0;
Linus Walleijd7c51b42016-04-26 10:35:29 +02001071 } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
1072 return linehandle_create(gdev, ip);
Linus Walleij61f922d2016-06-02 11:30:15 +02001073 } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
1074 return lineevent_create(gdev, ip);
Linus Walleij3c702e92015-10-21 15:29:53 +02001075 }
1076 return -EINVAL;
1077}
1078
Linus Walleij8b92e172016-05-27 14:24:04 +02001079#ifdef CONFIG_COMPAT
1080static long gpio_ioctl_compat(struct file *filp, unsigned int cmd,
1081 unsigned long arg)
1082{
1083 return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
1084}
1085#endif
1086
Linus Walleij3c702e92015-10-21 15:29:53 +02001087/**
1088 * gpio_chrdev_open() - open the chardev for ioctl operations
1089 * @inode: inode for this chardev
1090 * @filp: file struct for storing private data
1091 * Returns 0 on success
1092 */
1093static int gpio_chrdev_open(struct inode *inode, struct file *filp)
1094{
1095 struct gpio_device *gdev = container_of(inode->i_cdev,
1096 struct gpio_device, chrdev);
1097
1098 /* Fail on open if the backing gpiochip is gone */
Stephen Boydfb505742017-01-09 11:47:44 -08001099 if (!gdev->chip)
Linus Walleij3c702e92015-10-21 15:29:53 +02001100 return -ENODEV;
1101 get_device(&gdev->dev);
1102 filp->private_data = gdev;
Lars-Peter Clausenf4e81c52016-11-30 13:05:21 +01001103
1104 return nonseekable_open(inode, filp);
Linus Walleij3c702e92015-10-21 15:29:53 +02001105}
1106
1107/**
1108 * gpio_chrdev_release() - close chardev after ioctl operations
1109 * @inode: inode for this chardev
1110 * @filp: file struct for storing private data
1111 * Returns 0 on success
1112 */
1113static int gpio_chrdev_release(struct inode *inode, struct file *filp)
1114{
1115 struct gpio_device *gdev = container_of(inode->i_cdev,
1116 struct gpio_device, chrdev);
1117
Linus Walleij3c702e92015-10-21 15:29:53 +02001118 put_device(&gdev->dev);
1119 return 0;
1120}
1121
1122
1123static const struct file_operations gpio_fileops = {
1124 .release = gpio_chrdev_release,
1125 .open = gpio_chrdev_open,
1126 .owner = THIS_MODULE,
Lars-Peter Clausenf4e81c52016-11-30 13:05:21 +01001127 .llseek = no_llseek,
Linus Walleij3c702e92015-10-21 15:29:53 +02001128 .unlocked_ioctl = gpio_ioctl,
Linus Walleij8b92e172016-05-27 14:24:04 +02001129#ifdef CONFIG_COMPAT
1130 .compat_ioctl = gpio_ioctl_compat,
1131#endif
Linus Walleij3c702e92015-10-21 15:29:53 +02001132};
1133
Linus Walleijff2b1352015-10-20 11:10:38 +02001134static void gpiodevice_release(struct device *dev)
1135{
1136 struct gpio_device *gdev = dev_get_drvdata(dev);
1137
1138 list_del(&gdev->list);
1139 ida_simple_remove(&gpio_ida, gdev->id);
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +01001140 kfree_const(gdev->label);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001141 kfree(gdev->descs);
Linus Walleij9efd9e62016-02-09 14:27:42 +01001142 kfree(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001143}
1144
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001145static int gpiochip_setup_dev(struct gpio_device *gdev)
1146{
1147 int status;
1148
1149 cdev_init(&gdev->chrdev, &gpio_fileops);
1150 gdev->chrdev.owner = THIS_MODULE;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001151 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001152
1153 status = cdev_device_add(&gdev->chrdev, &gdev->dev);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001154 if (status)
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001155 return status;
1156
1157 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
1158 MAJOR(gpio_devt), gdev->id);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001159
1160 status = gpiochip_sysfs_register(gdev);
1161 if (status)
1162 goto err_remove_device;
1163
1164 /* From this point, the .release() function cleans up gpio_device */
1165 gdev->dev.release = gpiodevice_release;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001166 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
1167 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
1168 dev_name(&gdev->dev), gdev->chip->label ? : "generic");
1169
1170 return 0;
1171
1172err_remove_device:
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001173 cdev_device_del(&gdev->chrdev, &gdev->dev);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001174 return status;
1175}
1176
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02001177static void gpiochip_machine_hog(struct gpio_chip *chip, struct gpiod_hog *hog)
1178{
1179 struct gpio_desc *desc;
1180 int rv;
1181
1182 desc = gpiochip_get_desc(chip, hog->chip_hwnum);
1183 if (IS_ERR(desc)) {
1184 pr_err("%s: unable to get GPIO desc: %ld\n",
1185 __func__, PTR_ERR(desc));
1186 return;
1187 }
1188
Dan Carpenterba3efdf2018-05-01 10:36:39 +03001189 if (test_bit(FLAG_IS_HOGGED, &desc->flags))
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02001190 return;
1191
1192 rv = gpiod_hog(desc, hog->line_name, hog->lflags, hog->dflags);
1193 if (rv)
1194 pr_err("%s: unable to hog GPIO line (%s:%u): %d\n",
1195 __func__, chip->label, hog->chip_hwnum, rv);
1196}
1197
1198static void machine_gpiochip_add(struct gpio_chip *chip)
1199{
1200 struct gpiod_hog *hog;
1201
1202 mutex_lock(&gpio_machine_hogs_mutex);
1203
1204 list_for_each_entry(hog, &gpio_machine_hogs, list) {
1205 if (!strcmp(chip->label, hog->chip_label))
1206 gpiochip_machine_hog(chip, hog);
1207 }
1208
1209 mutex_unlock(&gpio_machine_hogs_mutex);
1210}
1211
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001212static void gpiochip_setup_devs(void)
1213{
1214 struct gpio_device *gdev;
1215 int err;
1216
1217 list_for_each_entry(gdev, &gpio_devices, list) {
1218 err = gpiochip_setup_dev(gdev);
1219 if (err)
1220 pr_err("%s: Failed to initialize gpio device (%d)\n",
1221 dev_name(&gdev->dev), err);
1222 }
1223}
1224
Thierry Reding959bc7b2017-11-07 19:15:59 +01001225int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001226 struct lock_class_key *lock_key,
1227 struct lock_class_key *request_key)
David Brownelld2876d02008-02-04 22:28:20 -08001228{
1229 unsigned long flags;
1230 int status = 0;
Linus Walleijff2b1352015-10-20 11:10:38 +02001231 unsigned i;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001232 int base = chip->base;
Linus Walleijff2b1352015-10-20 11:10:38 +02001233 struct gpio_device *gdev;
David Brownelld2876d02008-02-04 22:28:20 -08001234
Linus Walleijff2b1352015-10-20 11:10:38 +02001235 /*
1236 * First: allocate and populate the internal stat container, and
1237 * set up the struct device.
1238 */
Josh Cartwright969f07b2016-02-17 16:44:15 -06001239 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
Linus Walleijff2b1352015-10-20 11:10:38 +02001240 if (!gdev)
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001241 return -ENOMEM;
Linus Walleij3c702e92015-10-21 15:29:53 +02001242 gdev->dev.bus = &gpio_bus_type;
Linus Walleijff2b1352015-10-20 11:10:38 +02001243 gdev->chip = chip;
1244 chip->gpiodev = gdev;
1245 if (chip->parent) {
1246 gdev->dev.parent = chip->parent;
1247 gdev->dev.of_node = chip->parent->of_node;
Thierry Redingacc6e332016-07-05 14:11:14 +02001248 }
1249
Linus Walleijff2b1352015-10-20 11:10:38 +02001250#ifdef CONFIG_OF_GPIO
1251 /* If the gpiochip has an assigned OF node this takes precedence */
Thierry Redingacc6e332016-07-05 14:11:14 +02001252 if (chip->of_node)
1253 gdev->dev.of_node = chip->of_node;
Linus Walleijff2b1352015-10-20 11:10:38 +02001254#endif
Thierry Redingacc6e332016-07-05 14:11:14 +02001255
Linus Walleijff2b1352015-10-20 11:10:38 +02001256 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
1257 if (gdev->id < 0) {
1258 status = gdev->id;
1259 goto err_free_gdev;
1260 }
1261 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
1262 device_initialize(&gdev->dev);
1263 dev_set_drvdata(&gdev->dev, gdev);
1264 if (chip->parent && chip->parent->driver)
1265 gdev->owner = chip->parent->driver->owner;
1266 else if (chip->owner)
1267 /* TODO: remove chip->owner */
1268 gdev->owner = chip->owner;
1269 else
1270 gdev->owner = THIS_MODULE;
David Brownelld2876d02008-02-04 22:28:20 -08001271
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001272 gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001273 if (!gdev->descs) {
Linus Walleijff2b1352015-10-20 11:10:38 +02001274 status = -ENOMEM;
1275 goto err_free_gdev;
1276 }
1277
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001278 if (chip->ngpio == 0) {
1279 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
Linus Walleijff2b1352015-10-20 11:10:38 +02001280 status = -EINVAL;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001281 goto err_free_descs;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001282 }
Linus Walleijdf4878e2016-02-12 14:48:23 +01001283
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +01001284 gdev->label = kstrdup_const(chip->label ?: "unknown", GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001285 if (!gdev->label) {
1286 status = -ENOMEM;
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001287 goto err_free_descs;
Linus Walleijdf4878e2016-02-12 14:48:23 +01001288 }
1289
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001290 gdev->ngpio = chip->ngpio;
Linus Walleij43c54ec2016-02-11 11:37:48 +01001291 gdev->data = data;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001292
David Brownelld2876d02008-02-04 22:28:20 -08001293 spin_lock_irqsave(&gpio_lock, flags);
1294
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001295 /*
1296 * TODO: this allocates a Linux GPIO number base in the global
1297 * GPIO numberspace for this chip. In the long run we want to
1298 * get *rid* of this numberspace and use only descriptors, but
1299 * it may be a pipe dream. It will not happen before we get rid
1300 * of the sysfs interface anyways.
1301 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001302 if (base < 0) {
1303 base = gpiochip_find_base(chip->ngpio);
1304 if (base < 0) {
1305 status = base;
Johan Hovold225fce82015-01-12 17:12:25 +01001306 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001307 goto err_free_label;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001308 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001309 /*
1310 * TODO: it should not be necessary to reflect the assigned
1311 * base outside of the GPIO subsystem. Go over drivers and
1312 * see if anyone makes use of this, else drop this and assign
1313 * a poison instead.
1314 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001315 chip->base = base;
1316 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001317 gdev->base = base;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001318
Linus Walleijff2b1352015-10-20 11:10:38 +02001319 status = gpiodev_add_to_list(gdev);
Johan Hovold05aa5202015-01-12 17:12:26 +01001320 if (status) {
1321 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001322 goto err_free_label;
Johan Hovold05aa5202015-01-12 17:12:26 +01001323 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001324
Linus Walleij545ebd92016-05-30 17:11:59 +02001325 spin_unlock_irqrestore(&gpio_lock, flags);
1326
Linus Walleijff2b1352015-10-20 11:10:38 +02001327 for (i = 0; i < chip->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001328 struct gpio_desc *desc = &gdev->descs[i];
David Brownelld8f388d82008-07-25 01:46:07 -07001329
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001330 desc->gdev = gdev;
Timur Tabi1ca2a922017-12-20 13:10:31 -06001331
1332 /* REVISIT: most hardware initializes GPIOs as inputs (often
1333 * with pullups enabled) so power usage is minimized. Linux
1334 * code should set the gpio direction first thing; but until
1335 * it does, and in case chip->get_direction is not set, we may
1336 * expose the wrong direction in sysfs.
Johan Hovold05aa5202015-01-12 17:12:26 +01001337 */
Timur Tabi1ca2a922017-12-20 13:10:31 -06001338 desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0;
David Brownelld2876d02008-02-04 22:28:20 -08001339 }
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001340
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301341#ifdef CONFIG_PINCTRL
Linus Walleij20ec3e32016-02-11 11:03:06 +01001342 INIT_LIST_HEAD(&gdev->pin_ranges);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301343#endif
1344
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001345 status = gpiochip_set_desc_names(chip);
1346 if (status)
1347 goto err_remove_from_list;
1348
Mika Westerberg79b804c2016-09-20 15:15:21 +03001349 status = gpiochip_irqchip_init_valid_mask(chip);
1350 if (status)
1351 goto err_remove_from_list;
1352
Stephen Boyd726cb3b2018-03-23 09:34:52 -07001353 status = gpiochip_init_valid_mask(chip);
1354 if (status)
1355 goto err_remove_irqchip_mask;
1356
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001357 status = gpiochip_add_irqchip(chip, lock_key, request_key);
Thierry Redinge0d89722017-11-07 19:15:54 +01001358 if (status)
1359 goto err_remove_chip;
1360
Tomeu Vizoso28355f82015-07-14 10:29:54 +02001361 status = of_gpiochip_add(chip);
1362 if (status)
1363 goto err_remove_chip;
1364
Mika Westerberg664e3e52014-01-08 12:40:54 +02001365 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001366
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02001367 machine_gpiochip_add(chip);
1368
Linus Walleij3c702e92015-10-21 15:29:53 +02001369 /*
1370 * By first adding the chardev, and then adding the device,
1371 * we get a device node entry in sysfs under
1372 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1373 * coldplug of device nodes and other udev business.
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001374 * We can do this only if gpiolib has been initialized.
1375 * Otherwise, defer until later.
Linus Walleij3c702e92015-10-21 15:29:53 +02001376 */
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001377 if (gpiolib_initialized) {
1378 status = gpiochip_setup_dev(gdev);
1379 if (status)
1380 goto err_remove_chip;
1381 }
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001382 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001383
Johan Hovold225fce82015-01-12 17:12:25 +01001384err_remove_chip:
1385 acpi_gpiochip_remove(chip);
Johan Hovold6d867502015-05-04 17:23:25 +02001386 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +01001387 of_gpiochip_remove(chip);
Stephen Boyd726cb3b2018-03-23 09:34:52 -07001388 gpiochip_free_valid_mask(chip);
1389err_remove_irqchip_mask:
Mika Westerberg79b804c2016-09-20 15:15:21 +03001390 gpiochip_irqchip_free_valid_mask(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001391err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +01001392 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001393 list_del(&gdev->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001394 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001395err_free_label:
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +01001396 kfree_const(gdev->label);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001397err_free_descs:
1398 kfree(gdev->descs);
Linus Walleijff2b1352015-10-20 11:10:38 +02001399err_free_gdev:
1400 ida_simple_remove(&gpio_ida, gdev->id);
David Brownelld2876d02008-02-04 22:28:20 -08001401 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +02001402 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001403 gdev->base, gdev->base + gdev->ngpio - 1,
1404 chip->label ? : "generic");
1405 kfree(gdev);
David Brownelld2876d02008-02-04 22:28:20 -08001406 return status;
1407}
Thierry Reding959bc7b2017-11-07 19:15:59 +01001408EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);
David Brownelld2876d02008-02-04 22:28:20 -08001409
1410/**
Linus Walleij43c54ec2016-02-11 11:37:48 +01001411 * gpiochip_get_data() - get per-subdriver data for the chip
Thierry Reding950d55f52017-07-24 16:57:22 +02001412 * @chip: GPIO chip
1413 *
1414 * Returns:
1415 * The per-subdriver data for the chip.
Linus Walleij43c54ec2016-02-11 11:37:48 +01001416 */
1417void *gpiochip_get_data(struct gpio_chip *chip)
1418{
1419 return chip->gpiodev->data;
1420}
1421EXPORT_SYMBOL_GPL(gpiochip_get_data);
1422
1423/**
David Brownelld2876d02008-02-04 22:28:20 -08001424 * gpiochip_remove() - unregister a gpio_chip
1425 * @chip: the chip to unregister
1426 *
1427 * A gpio_chip with any GPIOs still requested may not be removed.
1428 */
abdoulaye berthee1db1702014-07-05 18:28:50 +02001429void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -08001430{
Linus Walleijff2b1352015-10-20 11:10:38 +02001431 struct gpio_device *gdev = chip->gpiodev;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001432 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001433 unsigned long flags;
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001434 unsigned i;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001435 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -08001436
Linus Walleijff2b1352015-10-20 11:10:38 +02001437 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
Linus Walleijafbc4f32016-02-09 13:21:06 +01001438 gpiochip_sysfs_unregister(gdev);
Geert Uytterhoeven5018ada2016-12-19 18:29:23 +01001439 gpiochip_free_hogs(chip);
Bamvor Jian Zhangbd203bd2016-02-20 13:13:19 +08001440 /* Numb the device, cancelling all outstanding operations */
1441 gdev->chip = NULL;
Johan Hovold00acc3d2015-01-12 17:12:27 +01001442 gpiochip_irqchip_remove(chip);
Mika Westerberg6072b9d2014-03-10 14:54:53 +02001443 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +01001444 gpiochip_remove_pin_ranges(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001445 of_gpiochip_remove(chip);
Stephen Boyd726cb3b2018-03-23 09:34:52 -07001446 gpiochip_free_valid_mask(chip);
Linus Walleij43c54ec2016-02-11 11:37:48 +01001447 /*
1448 * We accept no more calls into the driver from this point, so
1449 * NULL the driver data pointer
1450 */
1451 gdev->data = NULL;
Anton Vorontsov391c9702010-06-08 07:48:17 -06001452
Johan Hovold6798aca2015-01-12 17:12:28 +01001453 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001454 for (i = 0; i < gdev->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001455 desc = &gdev->descs[i];
Johan Hovoldfab28b82015-05-04 17:10:27 +02001456 if (test_bit(FLAG_REQUESTED, &desc->flags))
1457 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -08001458 }
David Brownelld2876d02008-02-04 22:28:20 -08001459 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001460
Johan Hovoldfab28b82015-05-04 17:10:27 +02001461 if (requested)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001462 dev_crit(&gdev->dev,
Linus Walleij58383c782015-11-04 09:56:26 +01001463 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +02001464
Linus Walleijff2b1352015-10-20 11:10:38 +02001465 /*
1466 * The gpiochip side puts its use of the device to rest here:
1467 * if there are no userspace clients, the chardev and device will
1468 * be removed, else it will be dangling until the last user is
1469 * gone.
1470 */
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001471 cdev_device_del(&gdev->chrdev, &gdev->dev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001472 put_device(&gdev->dev);
David Brownelld2876d02008-02-04 22:28:20 -08001473}
1474EXPORT_SYMBOL_GPL(gpiochip_remove);
1475
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301476static void devm_gpio_chip_release(struct device *dev, void *res)
1477{
1478 struct gpio_chip *chip = *(struct gpio_chip **)res;
1479
1480 gpiochip_remove(chip);
1481}
1482
1483static int devm_gpio_chip_match(struct device *dev, void *res, void *data)
1484
1485{
1486 struct gpio_chip **r = res;
1487
1488 if (!r || !*r) {
1489 WARN_ON(!r || !*r);
1490 return 0;
1491 }
1492
1493 return *r == data;
1494}
1495
1496/**
Jonathan Neuschäfer689fd022017-12-21 17:56:34 +01001497 * devm_gpiochip_add_data() - Resource manager gpiochip_add_data()
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301498 * @dev: the device pointer on which irq_chip belongs to.
1499 * @chip: the chip to register, with chip->base initialized
Thierry Reding950d55f52017-07-24 16:57:22 +02001500 * @data: driver-private data associated with this chip
1501 *
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301502 * Context: potentially before irqs will work
1503 *
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301504 * The gpio chip automatically be released when the device is unbound.
Thierry Reding950d55f52017-07-24 16:57:22 +02001505 *
1506 * Returns:
1507 * A negative errno if the chip can't be registered, such as because the
1508 * chip->base is invalid or already associated with a different chip.
1509 * Otherwise it returns zero as a success code.
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301510 */
1511int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
1512 void *data)
1513{
1514 struct gpio_chip **ptr;
1515 int ret;
1516
1517 ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
1518 GFP_KERNEL);
1519 if (!ptr)
1520 return -ENOMEM;
1521
1522 ret = gpiochip_add_data(chip, data);
1523 if (ret < 0) {
1524 devres_free(ptr);
1525 return ret;
1526 }
1527
1528 *ptr = chip;
1529 devres_add(dev, ptr);
1530
1531 return 0;
1532}
1533EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
1534
1535/**
1536 * devm_gpiochip_remove() - Resource manager of gpiochip_remove()
1537 * @dev: device for which which resource was allocated
1538 * @chip: the chip to remove
1539 *
1540 * A gpio_chip with any GPIOs still requested may not be removed.
1541 */
1542void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip)
1543{
1544 int ret;
1545
1546 ret = devres_release(dev, devm_gpio_chip_release,
1547 devm_gpio_chip_match, chip);
Christophe JAILLET3988d662017-01-27 12:56:42 +01001548 WARN_ON(ret);
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301549}
1550EXPORT_SYMBOL_GPL(devm_gpiochip_remove);
1551
Grant Likely594fa262010-06-08 07:48:16 -06001552/**
1553 * gpiochip_find() - iterator for locating a specific gpio_chip
1554 * @data: data to pass to match function
Thierry Reding950d55f52017-07-24 16:57:22 +02001555 * @match: Callback function to check gpio_chip
Grant Likely594fa262010-06-08 07:48:16 -06001556 *
1557 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1558 * determined by a user supplied @match callback. The callback should return
1559 * 0 if the device doesn't match and non-zero if it does. If the callback is
1560 * non-zero, this function will return to the caller and not iterate over any
1561 * more gpio_chips.
1562 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001563struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001564 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001565 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001566{
Linus Walleijff2b1352015-10-20 11:10:38 +02001567 struct gpio_device *gdev;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001568 struct gpio_chip *chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -06001569 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001570
1571 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001572 list_for_each_entry(gdev, &gpio_devices, list)
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001573 if (gdev->chip && match(gdev->chip, data)) {
1574 chip = gdev->chip;
Grant Likely594fa262010-06-08 07:48:16 -06001575 break;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001576 }
Linus Walleijff2b1352015-10-20 11:10:38 +02001577
Grant Likely594fa262010-06-08 07:48:16 -06001578 spin_unlock_irqrestore(&gpio_lock, flags);
1579
1580 return chip;
1581}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001582EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001583
Alexandre Courbot79697ef2013-11-16 21:39:32 +09001584static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1585{
1586 const char *name = data;
1587
1588 return !strcmp(chip->label, name);
1589}
1590
1591static struct gpio_chip *find_chip_by_name(const char *name)
1592{
1593 return gpiochip_find((void *)name, gpiochip_match_name);
1594}
1595
Linus Walleij14250522014-03-25 10:40:18 +01001596#ifdef CONFIG_GPIOLIB_IRQCHIP
1597
1598/*
1599 * The following is irqchip helper code for gpiochips.
1600 */
1601
Mika Westerberg79b804c2016-09-20 15:15:21 +03001602static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1603{
Thierry Redingdc7b0382017-11-07 19:15:52 +01001604 if (!gpiochip->irq.need_valid_mask)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001605 return 0;
1606
Stephen Boyde4371f6e2018-03-23 09:34:50 -07001607 gpiochip->irq.valid_mask = gpiochip_allocate_mask(gpiochip);
Thierry Redingdc7b0382017-11-07 19:15:52 +01001608 if (!gpiochip->irq.valid_mask)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001609 return -ENOMEM;
1610
Mika Westerberg79b804c2016-09-20 15:15:21 +03001611 return 0;
1612}
1613
1614static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1615{
Thierry Redingdc7b0382017-11-07 19:15:52 +01001616 kfree(gpiochip->irq.valid_mask);
1617 gpiochip->irq.valid_mask = NULL;
Mika Westerberg79b804c2016-09-20 15:15:21 +03001618}
1619
Stephen Boyd64ff2c82018-01-09 17:58:46 -08001620bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
1621 unsigned int offset)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001622{
Stephen Boyd726cb3b2018-03-23 09:34:52 -07001623 if (!gpiochip_line_is_valid(gpiochip, offset))
1624 return false;
Mika Westerberg79b804c2016-09-20 15:15:21 +03001625 /* No mask means all valid */
Thierry Redingdc7b0382017-11-07 19:15:52 +01001626 if (likely(!gpiochip->irq.valid_mask))
Mika Westerberg79b804c2016-09-20 15:15:21 +03001627 return true;
Thierry Redingdc7b0382017-11-07 19:15:52 +01001628 return test_bit(offset, gpiochip->irq.valid_mask);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001629}
Stephen Boyd64ff2c82018-01-09 17:58:46 -08001630EXPORT_SYMBOL_GPL(gpiochip_irqchip_irq_valid);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001631
Linus Walleij14250522014-03-25 10:40:18 +01001632/**
Linus Walleijd245b3f2016-11-24 10:57:25 +01001633 * gpiochip_set_cascaded_irqchip() - connects a cascaded irqchip to a gpiochip
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001634 * @gpiochip: the gpiochip to set the irqchip chain to
1635 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01001636 * @parent_irq: the irq number corresponding to the parent IRQ for this
1637 * chained irqchip
1638 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001639 * coming out of the gpiochip. If the interrupt is nested rather than
1640 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +01001641 */
Linus Walleijd245b3f2016-11-24 10:57:25 +01001642static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gpiochip,
1643 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001644 unsigned int parent_irq,
Linus Walleijd245b3f2016-11-24 10:57:25 +01001645 irq_flow_handler_t parent_handler)
Linus Walleij14250522014-03-25 10:40:18 +01001646{
Linus Walleij83141a72014-09-26 13:50:12 +02001647 unsigned int offset;
1648
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001649 if (!gpiochip->irq.domain) {
Linus Walleij83141a72014-09-26 13:50:12 +02001650 chip_err(gpiochip, "called %s before setting up irqchip\n",
1651 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +02001652 return;
1653 }
1654
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001655 if (parent_handler) {
1656 if (gpiochip->can_sleep) {
1657 chip_err(gpiochip,
1658 "you cannot have chained interrupts on a "
1659 "chip that may sleep\n");
1660 return;
1661 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001662 /*
1663 * The parent irqchip is already using the chip_data for this
1664 * irqchip, so our callbacks simply use the handler_data.
1665 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +02001666 irq_set_chained_handler_and_data(parent_irq, parent_handler,
1667 gpiochip);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001668
Thierry Reding39e5f092017-11-07 19:15:50 +01001669 gpiochip->irq.parents = &parent_irq;
1670 gpiochip->irq.num_parents = 1;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001671 }
Linus Walleij83141a72014-09-26 13:50:12 +02001672
1673 /* Set the parent IRQ for all affected IRQs */
Mika Westerberg79b804c2016-09-20 15:15:21 +03001674 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1675 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1676 continue;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001677 irq_set_parent(irq_find_mapping(gpiochip->irq.domain, offset),
Linus Walleij83141a72014-09-26 13:50:12 +02001678 parent_irq);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001679 }
Linus Walleij14250522014-03-25 10:40:18 +01001680}
Linus Walleijd245b3f2016-11-24 10:57:25 +01001681
1682/**
1683 * gpiochip_set_chained_irqchip() - connects a chained irqchip to a gpiochip
1684 * @gpiochip: the gpiochip to set the irqchip chain to
1685 * @irqchip: the irqchip to chain to the gpiochip
1686 * @parent_irq: the irq number corresponding to the parent IRQ for this
1687 * chained irqchip
1688 * @parent_handler: the parent interrupt handler for the accumulated IRQ
1689 * coming out of the gpiochip. If the interrupt is nested rather than
1690 * cascaded, pass NULL in this handler argument
1691 */
1692void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
1693 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001694 unsigned int parent_irq,
Linus Walleijd245b3f2016-11-24 10:57:25 +01001695 irq_flow_handler_t parent_handler)
1696{
Thierry Reding60ed54c2017-11-07 19:15:57 +01001697 if (gpiochip->irq.threaded) {
1698 chip_err(gpiochip, "tried to chain a threaded gpiochip\n");
1699 return;
1700 }
1701
Linus Walleijd245b3f2016-11-24 10:57:25 +01001702 gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
1703 parent_handler);
1704}
Linus Walleij14250522014-03-25 10:40:18 +01001705EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
1706
1707/**
Linus Walleijd245b3f2016-11-24 10:57:25 +01001708 * gpiochip_set_nested_irqchip() - connects a nested irqchip to a gpiochip
1709 * @gpiochip: the gpiochip to set the irqchip nested handler to
1710 * @irqchip: the irqchip to nest to the gpiochip
1711 * @parent_irq: the irq number corresponding to the parent IRQ for this
1712 * nested irqchip
1713 */
1714void gpiochip_set_nested_irqchip(struct gpio_chip *gpiochip,
1715 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001716 unsigned int parent_irq)
Linus Walleijd245b3f2016-11-24 10:57:25 +01001717{
Linus Walleijd245b3f2016-11-24 10:57:25 +01001718 gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
1719 NULL);
1720}
1721EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip);
1722
1723/**
Linus Walleij14250522014-03-25 10:40:18 +01001724 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1725 * @d: the irqdomain used by this irqchip
1726 * @irq: the global irq number used by this GPIO irqchip irq
1727 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1728 *
1729 * This function will set up the mapping for a certain IRQ line on a
1730 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1731 * stored inside the gpiochip.
1732 */
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001733int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1734 irq_hw_number_t hwirq)
Linus Walleij14250522014-03-25 10:40:18 +01001735{
1736 struct gpio_chip *chip = d->host_data;
Thierry Redinge0d89722017-11-07 19:15:54 +01001737 int err = 0;
Linus Walleij14250522014-03-25 10:40:18 +01001738
Grygorii Strashkodc749a02017-07-21 11:49:00 -05001739 if (!gpiochip_irqchip_irq_valid(chip, hwirq))
1740 return -ENXIO;
1741
Linus Walleij14250522014-03-25 10:40:18 +01001742 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001743 /*
1744 * This lock class tells lockdep that GPIO irqs are in a different
1745 * category than their parents, so it won't report false recursion.
1746 */
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001747 irq_set_lockdep_class(irq, chip->irq.lock_key, chip->irq.request_key);
Thierry Redingc7a0aa52017-11-07 19:15:48 +01001748 irq_set_chip_and_handler(irq, chip->irq.chip, chip->irq.handler);
Linus Walleijd245b3f2016-11-24 10:57:25 +01001749 /* Chips that use nested thread handlers have them marked */
Thierry Reding60ed54c2017-11-07 19:15:57 +01001750 if (chip->irq.threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02001751 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +01001752 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -05001753
Thierry Redinge0d89722017-11-07 19:15:54 +01001754 if (chip->irq.num_parents == 1)
1755 err = irq_set_parent(irq, chip->irq.parents[0]);
1756 else if (chip->irq.map)
1757 err = irq_set_parent(irq, chip->irq.map[hwirq]);
1758
1759 if (err < 0)
1760 return err;
1761
Linus Walleij1333b902014-04-23 16:45:12 +02001762 /*
1763 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1764 * is passed as default type.
1765 */
Thierry Reding3634eeb2017-11-07 19:15:49 +01001766 if (chip->irq.default_type != IRQ_TYPE_NONE)
1767 irq_set_irq_type(irq, chip->irq.default_type);
Linus Walleij14250522014-03-25 10:40:18 +01001768
1769 return 0;
1770}
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001771EXPORT_SYMBOL_GPL(gpiochip_irq_map);
Linus Walleij14250522014-03-25 10:40:18 +01001772
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001773void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
Linus Walleijc3626fd2014-03-28 20:42:01 +01001774{
Linus Walleij1c8732b2014-04-09 13:34:39 +02001775 struct gpio_chip *chip = d->host_data;
1776
Thierry Reding60ed54c2017-11-07 19:15:57 +01001777 if (chip->irq.threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02001778 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001779 irq_set_chip_and_handler(irq, NULL, NULL);
1780 irq_set_chip_data(irq, NULL);
1781}
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001782EXPORT_SYMBOL_GPL(gpiochip_irq_unmap);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001783
Linus Walleij14250522014-03-25 10:40:18 +01001784static const struct irq_domain_ops gpiochip_domain_ops = {
1785 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +01001786 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +01001787 /* Virtually all GPIO irqchips are twocell:ed */
1788 .xlate = irq_domain_xlate_twocell,
1789};
1790
1791static int gpiochip_irq_reqres(struct irq_data *d)
1792{
1793 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1794
Linus Walleijff2b1352015-10-20 11:10:38 +02001795 if (!try_module_get(chip->gpiodev->owner))
Grygorii Strashko5b76e792015-06-25 20:30:50 +03001796 return -ENODEV;
1797
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001798 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +01001799 chip_err(chip,
1800 "unable to lock HW IRQ %lu for IRQ\n",
1801 d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001802 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001803 return -EINVAL;
1804 }
1805 return 0;
1806}
1807
1808static void gpiochip_irq_relres(struct irq_data *d)
1809{
1810 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1811
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001812 gpiochip_unlock_as_irq(chip, d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001813 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001814}
1815
1816static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
1817{
Grygorii Strashkodc749a02017-07-21 11:49:00 -05001818 if (!gpiochip_irqchip_irq_valid(chip, offset))
1819 return -ENXIO;
Thierry Redinge0d89722017-11-07 19:15:54 +01001820
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001821 return irq_create_mapping(chip->irq.domain, offset);
Linus Walleij14250522014-03-25 10:40:18 +01001822}
1823
1824/**
Thierry Redinge0d89722017-11-07 19:15:54 +01001825 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
1826 * @gpiochip: the GPIO chip to add the IRQ chip to
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001827 * @lock_key: lockdep class for IRQ lock
1828 * @request_key: lockdep class for IRQ request
Thierry Redinge0d89722017-11-07 19:15:54 +01001829 */
Thierry Reding959bc7b2017-11-07 19:15:59 +01001830static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001831 struct lock_class_key *lock_key,
1832 struct lock_class_key *request_key)
Thierry Redinge0d89722017-11-07 19:15:54 +01001833{
1834 struct irq_chip *irqchip = gpiochip->irq.chip;
1835 const struct irq_domain_ops *ops;
1836 struct device_node *np;
1837 unsigned int type;
1838 unsigned int i;
1839
1840 if (!irqchip)
1841 return 0;
1842
1843 if (gpiochip->irq.parent_handler && gpiochip->can_sleep) {
1844 chip_err(gpiochip, "you cannot have chained interrupts on a "
1845 "chip that may sleep\n");
1846 return -EINVAL;
1847 }
1848
1849 np = gpiochip->gpiodev->dev.of_node;
1850 type = gpiochip->irq.default_type;
1851
1852 /*
1853 * Specifying a default trigger is a terrible idea if DT or ACPI is
1854 * used to configure the interrupts, as you may end up with
1855 * conflicting triggers. Tell the user, and reset to NONE.
1856 */
1857 if (WARN(np && type != IRQ_TYPE_NONE,
1858 "%s: Ignoring %u default trigger\n", np->full_name, type))
1859 type = IRQ_TYPE_NONE;
1860
1861 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
1862 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1863 "Ignoring %u default trigger\n", type);
1864 type = IRQ_TYPE_NONE;
1865 }
1866
1867 gpiochip->to_irq = gpiochip_to_irq;
1868 gpiochip->irq.default_type = type;
Thierry Reding959bc7b2017-11-07 19:15:59 +01001869 gpiochip->irq.lock_key = lock_key;
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001870 gpiochip->irq.request_key = request_key;
Thierry Redinge0d89722017-11-07 19:15:54 +01001871
1872 if (gpiochip->irq.domain_ops)
1873 ops = gpiochip->irq.domain_ops;
1874 else
1875 ops = &gpiochip_domain_ops;
1876
1877 gpiochip->irq.domain = irq_domain_add_simple(np, gpiochip->ngpio,
Thierry Reding8302cf52017-11-07 19:15:58 +01001878 gpiochip->irq.first,
1879 ops, gpiochip);
Thierry Redinge0d89722017-11-07 19:15:54 +01001880 if (!gpiochip->irq.domain)
1881 return -EINVAL;
1882
1883 /*
1884 * It is possible for a driver to override this, but only if the
1885 * alternative functions are both implemented.
1886 */
1887 if (!irqchip->irq_request_resources &&
1888 !irqchip->irq_release_resources) {
1889 irqchip->irq_request_resources = gpiochip_irq_reqres;
1890 irqchip->irq_release_resources = gpiochip_irq_relres;
1891 }
1892
1893 if (gpiochip->irq.parent_handler) {
1894 void *data = gpiochip->irq.parent_handler_data ?: gpiochip;
1895
1896 for (i = 0; i < gpiochip->irq.num_parents; i++) {
1897 /*
1898 * The parent IRQ chip is already using the chip_data
1899 * for this IRQ chip, so our callbacks simply use the
1900 * handler_data.
1901 */
1902 irq_set_chained_handler_and_data(gpiochip->irq.parents[i],
1903 gpiochip->irq.parent_handler,
1904 data);
1905 }
Thierry Redinge0d89722017-11-07 19:15:54 +01001906 }
1907
1908 acpi_gpiochip_request_interrupts(gpiochip);
1909
1910 return 0;
1911}
1912
1913/**
Linus Walleij14250522014-03-25 10:40:18 +01001914 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1915 * @gpiochip: the gpiochip to remove the irqchip from
1916 *
1917 * This is called only from gpiochip_remove()
1918 */
1919static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
1920{
Thierry Reding39e5f092017-11-07 19:15:50 +01001921 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +01001922
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001923 acpi_gpiochip_free_interrupts(gpiochip);
1924
Thierry Redinge0d89722017-11-07 19:15:54 +01001925 if (gpiochip->irq.chip && gpiochip->irq.parent_handler) {
Thierry Reding39e5f092017-11-07 19:15:50 +01001926 struct gpio_irq_chip *irq = &gpiochip->irq;
1927 unsigned int i;
1928
1929 for (i = 0; i < irq->num_parents; i++)
1930 irq_set_chained_handler_and_data(irq->parents[i],
1931 NULL, NULL);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001932 }
1933
Linus Walleijc3626fd2014-03-28 20:42:01 +01001934 /* Remove all IRQ mappings and delete the domain */
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001935 if (gpiochip->irq.domain) {
Thierry Reding39e5f092017-11-07 19:15:50 +01001936 unsigned int irq;
1937
Mika Westerberg79b804c2016-09-20 15:15:21 +03001938 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1939 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1940 continue;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001941
1942 irq = irq_find_mapping(gpiochip->irq.domain, offset);
1943 irq_dispose_mapping(irq);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001944 }
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001945
1946 irq_domain_remove(gpiochip->irq.domain);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001947 }
Linus Walleij14250522014-03-25 10:40:18 +01001948
Thierry Redingda80ff82017-11-07 19:15:46 +01001949 if (gpiochip->irq.chip) {
1950 gpiochip->irq.chip->irq_request_resources = NULL;
1951 gpiochip->irq.chip->irq_release_resources = NULL;
1952 gpiochip->irq.chip = NULL;
Linus Walleij14250522014-03-25 10:40:18 +01001953 }
Mika Westerberg79b804c2016-09-20 15:15:21 +03001954
1955 gpiochip_irqchip_free_valid_mask(gpiochip);
Linus Walleij14250522014-03-25 10:40:18 +01001956}
1957
1958/**
Linus Walleij739e6f52017-01-11 13:37:07 +01001959 * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01001960 * @gpiochip: the gpiochip to add the irqchip to
1961 * @irqchip: the irqchip to add to the gpiochip
1962 * @first_irq: if not dynamically assigned, the base (first) IRQ to
1963 * allocate gpiochip irqs from
1964 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +02001965 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
1966 * to have the core avoid setting up any default type in the hardware.
Thierry Reding60ed54c2017-11-07 19:15:57 +01001967 * @threaded: whether this irqchip uses a nested thread handler
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001968 * @lock_key: lockdep class for IRQ lock
1969 * @request_key: lockdep class for IRQ request
Linus Walleij14250522014-03-25 10:40:18 +01001970 *
1971 * This function closely associates a certain irqchip with a certain
1972 * gpiochip, providing an irq domain to translate the local IRQs to
1973 * global irqs in the gpiolib core, and making sure that the gpiochip
1974 * is passed as chip data to all related functions. Driver callbacks
Linus Walleij09dd5f92015-12-07 15:31:58 +01001975 * need to use gpiochip_get_data() to get their local state containers back
Linus Walleij14250522014-03-25 10:40:18 +01001976 * from the gpiochip passed as chip data. An irqdomain will be stored
1977 * in the gpiochip that shall be used by the driver to handle IRQ number
1978 * translation. The gpiochip will need to be initialized and registered
1979 * before calling this function.
1980 *
Linus Walleijc3626fd2014-03-28 20:42:01 +01001981 * This function will handle two cell:ed simple IRQs and assumes all
1982 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +01001983 * need to be open coded.
1984 */
Linus Walleij739e6f52017-01-11 13:37:07 +01001985int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
1986 struct irq_chip *irqchip,
1987 unsigned int first_irq,
1988 irq_flow_handler_t handler,
1989 unsigned int type,
Thierry Reding60ed54c2017-11-07 19:15:57 +01001990 bool threaded,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001991 struct lock_class_key *lock_key,
1992 struct lock_class_key *request_key)
Linus Walleij14250522014-03-25 10:40:18 +01001993{
1994 struct device_node *of_node;
Linus Walleij14250522014-03-25 10:40:18 +01001995
1996 if (!gpiochip || !irqchip)
1997 return -EINVAL;
1998
Linus Walleij58383c782015-11-04 09:56:26 +01001999 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +01002000 pr_err("missing gpiochip .dev parent pointer\n");
2001 return -EINVAL;
2002 }
Thierry Reding60ed54c2017-11-07 19:15:57 +01002003 gpiochip->irq.threaded = threaded;
Linus Walleij58383c782015-11-04 09:56:26 +01002004 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +01002005#ifdef CONFIG_OF_GPIO
2006 /*
Colin Cronin20a8a962015-05-18 11:41:43 -07002007 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08002008 * FIXME: get rid of this and use gpiochip->parent->of_node
2009 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +01002010 */
2011 if (gpiochip->of_node)
2012 of_node = gpiochip->of_node;
2013#endif
Marc Zyngier332e99d2016-09-07 09:12:11 +01002014 /*
Mika Westerberg0a1e0052016-09-12 14:29:51 +03002015 * Specifying a default trigger is a terrible idea if DT or ACPI is
Marc Zyngier332e99d2016-09-07 09:12:11 +01002016 * used to configure the interrupts, as you may end-up with
2017 * conflicting triggers. Tell the user, and reset to NONE.
2018 */
2019 if (WARN(of_node && type != IRQ_TYPE_NONE,
Rob Herring7eb6ce22017-07-18 16:43:03 -05002020 "%pOF: Ignoring %d default trigger\n", of_node, type))
Marc Zyngier332e99d2016-09-07 09:12:11 +01002021 type = IRQ_TYPE_NONE;
Mika Westerberg0a1e0052016-09-12 14:29:51 +03002022 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
2023 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
2024 "Ignoring %d default trigger\n", type);
2025 type = IRQ_TYPE_NONE;
2026 }
Marc Zyngier332e99d2016-09-07 09:12:11 +01002027
Thierry Redingda80ff82017-11-07 19:15:46 +01002028 gpiochip->irq.chip = irqchip;
Thierry Redingc7a0aa52017-11-07 19:15:48 +01002029 gpiochip->irq.handler = handler;
Thierry Reding3634eeb2017-11-07 19:15:49 +01002030 gpiochip->irq.default_type = type;
Linus Walleij14250522014-03-25 10:40:18 +01002031 gpiochip->to_irq = gpiochip_to_irq;
Thierry Redingca9df052017-11-07 19:15:53 +01002032 gpiochip->irq.lock_key = lock_key;
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002033 gpiochip->irq.request_key = request_key;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01002034 gpiochip->irq.domain = irq_domain_add_simple(of_node,
Linus Walleij14250522014-03-25 10:40:18 +01002035 gpiochip->ngpio, first_irq,
2036 &gpiochip_domain_ops, gpiochip);
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01002037 if (!gpiochip->irq.domain) {
Thierry Redingda80ff82017-11-07 19:15:46 +01002038 gpiochip->irq.chip = NULL;
Linus Walleij14250522014-03-25 10:40:18 +01002039 return -EINVAL;
2040 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +02002041
2042 /*
2043 * It is possible for a driver to override this, but only if the
2044 * alternative functions are both implemented.
2045 */
2046 if (!irqchip->irq_request_resources &&
2047 !irqchip->irq_release_resources) {
2048 irqchip->irq_request_resources = gpiochip_irq_reqres;
2049 irqchip->irq_release_resources = gpiochip_irq_relres;
2050 }
Linus Walleij14250522014-03-25 10:40:18 +01002051
Mika Westerbergafa82fa2014-07-25 09:54:48 +03002052 acpi_gpiochip_request_interrupts(gpiochip);
2053
Linus Walleij14250522014-03-25 10:40:18 +01002054 return 0;
2055}
Linus Walleij739e6f52017-01-11 13:37:07 +01002056EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key);
Linus Walleij14250522014-03-25 10:40:18 +01002057
2058#else /* CONFIG_GPIOLIB_IRQCHIP */
2059
Thierry Reding959bc7b2017-11-07 19:15:59 +01002060static inline int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01002061 struct lock_class_key *lock_key,
2062 struct lock_class_key *request_key)
Thierry Redinge0d89722017-11-07 19:15:54 +01002063{
2064 return 0;
2065}
2066
Linus Walleij14250522014-03-25 10:40:18 +01002067static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
Mika Westerberg79b804c2016-09-20 15:15:21 +03002068static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
2069{
2070 return 0;
2071}
2072static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
2073{ }
Linus Walleij14250522014-03-25 10:40:18 +01002074
2075#endif /* CONFIG_GPIOLIB_IRQCHIP */
2076
Jonas Gorskic771c2f2015-10-11 17:34:15 +02002077/**
2078 * gpiochip_generic_request() - request the gpio function for a pin
2079 * @chip: the gpiochip owning the GPIO
2080 * @offset: the offset of the GPIO to request for GPIO function
2081 */
2082int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
2083{
Linus Walleija9a1d2a2017-09-22 11:02:10 +02002084 return pinctrl_gpio_request(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02002085}
2086EXPORT_SYMBOL_GPL(gpiochip_generic_request);
2087
2088/**
2089 * gpiochip_generic_free() - free the gpio function from a pin
2090 * @chip: the gpiochip to request the gpio function for
2091 * @offset: the offset of the GPIO to free from GPIO function
2092 */
2093void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
2094{
Linus Walleija9a1d2a2017-09-22 11:02:10 +02002095 pinctrl_gpio_free(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02002096}
2097EXPORT_SYMBOL_GPL(gpiochip_generic_free);
2098
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002099/**
2100 * gpiochip_generic_config() - apply configuration for a pin
2101 * @chip: the gpiochip owning the GPIO
2102 * @offset: the offset of the GPIO to apply the configuration
2103 * @config: the configuration to be applied
2104 */
2105int gpiochip_generic_config(struct gpio_chip *chip, unsigned offset,
2106 unsigned long config)
2107{
2108 return pinctrl_gpio_set_config(chip->gpiodev->base + offset, config);
2109}
2110EXPORT_SYMBOL_GPL(gpiochip_generic_config);
2111
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302112#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01002113
Linus Walleij3f0f8672012-11-20 12:40:15 +01002114/**
Christian Ruppert586a87e2013-10-15 15:37:54 +02002115 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
2116 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +02002117 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +02002118 * @gpio_offset: the start offset in the current gpio_chip number space
2119 * @pin_group: name of the pin group inside the pin controller
2120 */
2121int gpiochip_add_pingroup_range(struct gpio_chip *chip,
2122 struct pinctrl_dev *pctldev,
2123 unsigned int gpio_offset, const char *pin_group)
2124{
2125 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002126 struct gpio_device *gdev = chip->gpiodev;
Christian Ruppert586a87e2013-10-15 15:37:54 +02002127 int ret;
2128
2129 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
2130 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002131 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +02002132 return -ENOMEM;
2133 }
2134
2135 /* Use local offset as range ID */
2136 pin_range->range.id = gpio_offset;
2137 pin_range->range.gc = chip;
2138 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002139 pin_range->range.base = gdev->base + gpio_offset;
Christian Ruppert586a87e2013-10-15 15:37:54 +02002140 pin_range->pctldev = pctldev;
2141
2142 ret = pinctrl_get_group_pins(pctldev, pin_group,
2143 &pin_range->range.pins,
2144 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +01002145 if (ret < 0) {
2146 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +02002147 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +01002148 }
Christian Ruppert586a87e2013-10-15 15:37:54 +02002149
2150 pinctrl_add_gpio_range(pctldev, &pin_range->range);
2151
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002152 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
2153 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +02002154 pinctrl_dev_get_devname(pctldev), pin_group);
2155
Linus Walleij20ec3e32016-02-11 11:03:06 +01002156 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Christian Ruppert586a87e2013-10-15 15:37:54 +02002157
2158 return 0;
2159}
2160EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
2161
2162/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01002163 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
2164 * @chip: the gpiochip to add the range for
Thierry Reding950d55f52017-07-24 16:57:22 +02002165 * @pinctl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01002166 * @gpio_offset: the start offset in the current gpio_chip number space
2167 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01002168 * @npins: the number of pins from the offset of each pin space (GPIO and
2169 * pin controller) to accumulate in this range
Thierry Reding950d55f52017-07-24 16:57:22 +02002170 *
2171 * Returns:
2172 * 0 on success, or a negative error-code on failure.
Linus Walleij3f0f8672012-11-20 12:40:15 +01002173 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002174int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01002175 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01002176 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302177{
2178 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002179 struct gpio_device *gdev = chip->gpiodev;
Axel Linb4d4b1f2012-11-21 14:33:56 +08002180 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302181
Linus Walleij3f0f8672012-11-20 12:40:15 +01002182 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302183 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002184 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002185 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302186 }
2187
Linus Walleij3f0f8672012-11-20 12:40:15 +01002188 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01002189 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01002190 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302191 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002192 pin_range->range.base = gdev->base + gpio_offset;
Linus Walleij316511c2012-11-21 08:48:09 +01002193 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302194 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01002195 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302196 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01002197 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08002198 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002199 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +01002200 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08002201 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01002202 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002203 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
2204 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +01002205 pinctl_name,
2206 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302207
Linus Walleij20ec3e32016-02-11 11:03:06 +01002208 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002209
2210 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302211}
Linus Walleij165adc92012-11-06 14:49:39 +01002212EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302213
Linus Walleij3f0f8672012-11-20 12:40:15 +01002214/**
2215 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
2216 * @chip: the chip to remove all the mappings for
2217 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302218void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
2219{
2220 struct gpio_pin_range *pin_range, *tmp;
Linus Walleij20ec3e32016-02-11 11:03:06 +01002221 struct gpio_device *gdev = chip->gpiodev;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302222
Linus Walleij20ec3e32016-02-11 11:03:06 +01002223 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302224 list_del(&pin_range->node);
2225 pinctrl_remove_gpio_range(pin_range->pctldev,
2226 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01002227 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302228 }
2229}
Linus Walleij165adc92012-11-06 14:49:39 +01002230EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
2231
2232#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302233
David Brownelld2876d02008-02-04 22:28:20 -08002234/* These "optional" allocation calls help prevent drivers from stomping
2235 * on each other, and help provide better diagnostics in debugfs.
2236 * They're called even less than the "set direction" calls.
2237 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002238static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08002239{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002240 struct gpio_chip *chip = desc->gdev->chip;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002241 int status;
David Brownelld2876d02008-02-04 22:28:20 -08002242 unsigned long flags;
2243
2244 spin_lock_irqsave(&gpio_lock, flags);
2245
David Brownelld2876d02008-02-04 22:28:20 -08002246 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07002247 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08002248 */
2249
2250 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
2251 desc_set_label(desc, label ? : "?");
2252 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07002253 } else {
David Brownelld2876d02008-02-04 22:28:20 -08002254 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -08002255 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07002256 }
2257
2258 if (chip->request) {
2259 /* chip->request may sleep */
2260 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002261 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07002262 spin_lock_irqsave(&gpio_lock, flags);
2263
2264 if (status < 0) {
2265 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -07002266 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03002267 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07002268 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07002269 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03002270 if (chip->get_direction) {
2271 /* chip->get_direction may sleep */
2272 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002273 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03002274 spin_lock_irqsave(&gpio_lock, flags);
2275 }
David Brownelld2876d02008-02-04 22:28:20 -08002276done:
Mika Westerberg77c2d792014-03-10 14:54:50 +02002277 spin_unlock_irqrestore(&gpio_lock, flags);
2278 return status;
2279}
2280
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002281/*
2282 * This descriptor validation needs to be inserted verbatim into each
2283 * function taking a descriptor, so we need to use a preprocessor
Linus Walleij54d77192016-05-30 16:48:39 +02002284 * macro to avoid endless duplication. If the desc is NULL it is an
2285 * optional GPIO and calls should just bail out.
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002286 */
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002287static int validate_desc(const struct gpio_desc *desc, const char *func)
2288{
2289 if (!desc)
2290 return 0;
2291 if (IS_ERR(desc)) {
2292 pr_warn("%s: invalid GPIO (errorpointer)\n", func);
2293 return PTR_ERR(desc);
2294 }
2295 if (!desc->gdev) {
2296 pr_warn("%s: invalid GPIO (no device)\n", func);
2297 return -EINVAL;
2298 }
2299 if (!desc->gdev->chip) {
2300 dev_warn(&desc->gdev->dev,
2301 "%s: backing chip is gone\n", func);
2302 return 0;
2303 }
2304 return 1;
2305}
2306
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002307#define VALIDATE_DESC(desc) do { \
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002308 int __valid = validate_desc(desc, __func__); \
2309 if (__valid <= 0) \
2310 return __valid; \
2311 } while (0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002312
2313#define VALIDATE_DESC_VOID(desc) do { \
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002314 int __valid = validate_desc(desc, __func__); \
2315 if (__valid <= 0) \
Linus Walleij54d77192016-05-30 16:48:39 +02002316 return; \
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002317 } while (0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002318
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002319int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002320{
2321 int status = -EPROBE_DEFER;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002322 struct gpio_device *gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002323
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002324 VALIDATE_DESC(desc);
2325 gdev = desc->gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002326
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002327 if (try_module_get(gdev->owner)) {
Linus Walleijfac9d882017-09-26 20:58:28 +02002328 status = gpiod_request_commit(desc, label);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002329 if (status < 0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002330 module_put(gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002331 else
2332 get_device(&gdev->dev);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002333 }
2334
David Brownelld2876d02008-02-04 22:28:20 -08002335 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +02002336 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002337
David Brownelld2876d02008-02-04 22:28:20 -08002338 return status;
2339}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002340
Linus Walleijfac9d882017-09-26 20:58:28 +02002341static bool gpiod_free_commit(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002342{
Mika Westerberg77c2d792014-03-10 14:54:50 +02002343 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08002344 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07002345 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002346
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07002347 might_sleep();
2348
Alexandre Courbot372e7222013-02-03 01:29:29 +09002349 gpiod_unexport(desc);
David Brownelld8f388d82008-07-25 01:46:07 -07002350
David Brownelld2876d02008-02-04 22:28:20 -08002351 spin_lock_irqsave(&gpio_lock, flags);
2352
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002353 chip = desc->gdev->chip;
David Brownell35e8bb52008-10-15 22:03:16 -07002354 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
2355 if (chip->free) {
2356 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07002357 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002358 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07002359 spin_lock_irqsave(&gpio_lock, flags);
2360 }
David Brownelld2876d02008-02-04 22:28:20 -08002361 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08002362 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07002363 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302364 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302365 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06002366 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002367 ret = true;
2368 }
David Brownelld2876d02008-02-04 22:28:20 -08002369
2370 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002371 return ret;
2372}
2373
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002374void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002375{
Linus Walleijfac9d882017-09-26 20:58:28 +02002376 if (desc && desc->gdev && gpiod_free_commit(desc)) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002377 module_put(desc->gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002378 put_device(&desc->gdev->dev);
2379 } else {
Mika Westerberg77c2d792014-03-10 14:54:50 +02002380 WARN_ON(extra_checks);
Linus Walleij33a68e82016-02-11 10:28:44 +01002381 }
David Brownelld2876d02008-02-04 22:28:20 -08002382}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002383
David Brownelld2876d02008-02-04 22:28:20 -08002384/**
2385 * gpiochip_is_requested - return string iff signal was requested
2386 * @chip: controller managing the signal
2387 * @offset: of signal within controller's 0..(ngpio - 1) range
2388 *
2389 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09002390 * The string returned is the label passed to gpio_request(); if none has been
2391 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08002392 *
2393 * This function is for use by GPIO controller drivers. The label can
2394 * help with diagnostics, and knowing that the signal is used as a GPIO
2395 * can help avoid accidentally multiplexing it to another controller.
2396 */
2397const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
2398{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002399 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08002400
Dirk Behme48b59532015-08-18 18:02:32 +02002401 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08002402 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002403
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002404 desc = &chip->gpiodev->descs[offset];
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002405
Alexandre Courbot372e7222013-02-03 01:29:29 +09002406 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08002407 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002408 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08002409}
2410EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2411
Mika Westerberg77c2d792014-03-10 14:54:50 +02002412/**
2413 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
Thierry Reding950d55f52017-07-24 16:57:22 +02002414 * @chip: GPIO chip
2415 * @hwnum: hardware number of the GPIO for which to request the descriptor
Mika Westerberg77c2d792014-03-10 14:54:50 +02002416 * @label: label for the GPIO
2417 *
2418 * Function allows GPIO chip drivers to request and use their own GPIO
2419 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2420 * function will not increase reference count of the GPIO chip module. This
2421 * allows the GPIO chip module to be unloaded as needed (we assume that the
2422 * GPIO chip driver handles freeing the GPIOs it has requested).
Thierry Reding950d55f52017-07-24 16:57:22 +02002423 *
2424 * Returns:
2425 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2426 * code on failure.
Mika Westerberg77c2d792014-03-10 14:54:50 +02002427 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002428struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
2429 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002430{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002431 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
2432 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002433
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002434 if (IS_ERR(desc)) {
2435 chip_err(chip, "failed to get GPIO descriptor\n");
2436 return desc;
2437 }
2438
Linus Walleijfac9d882017-09-26 20:58:28 +02002439 err = gpiod_request_commit(desc, label);
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002440 if (err < 0)
2441 return ERR_PTR(err);
2442
2443 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002444}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002445EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002446
2447/**
2448 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2449 * @desc: GPIO descriptor to free
2450 *
2451 * Function frees the given GPIO requested previously with
2452 * gpiochip_request_own_desc().
2453 */
2454void gpiochip_free_own_desc(struct gpio_desc *desc)
2455{
2456 if (desc)
Linus Walleijfac9d882017-09-26 20:58:28 +02002457 gpiod_free_commit(desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002458}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002459EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08002460
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002461/*
2462 * Drivers MUST set GPIO direction before making get/set calls. In
David Brownelld2876d02008-02-04 22:28:20 -08002463 * some cases this is done in early boot, before IRQs are enabled.
2464 *
2465 * As a rule these aren't called more than once (except for drivers
2466 * using the open-drain emulation idiom) so these are natural places
2467 * to accumulate extra debugging checks. Note that we can't (yet)
2468 * rely on gpio_request() having been called beforehand.
2469 */
2470
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002471/**
2472 * gpiod_direction_input - set the GPIO direction to input
2473 * @desc: GPIO to set to input
2474 *
2475 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2476 * be called safely on it.
2477 *
2478 * Return 0 in case of success, else an error code.
2479 */
2480int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002481{
David Brownelld2876d02008-02-04 22:28:20 -08002482 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002483 int status = -EINVAL;
2484
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002485 VALIDATE_DESC(desc);
2486 chip = desc->gdev->chip;
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002487
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002488 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01002489 gpiod_warn(desc,
2490 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02002491 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002492 return -EIO;
2493 }
2494
Alexandre Courbotd82da792014-07-22 16:17:43 +09002495 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -08002496 if (status == 0)
2497 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002498
Alexandre Courbot372e7222013-02-03 01:29:29 +09002499 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09002500
David Brownelld2876d02008-02-04 22:28:20 -08002501 return status;
2502}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002503EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002504
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002505static int gpio_set_drive_single_ended(struct gpio_chip *gc, unsigned offset,
2506 enum pin_config_param mode)
2507{
2508 unsigned long config = { PIN_CONF_PACKED(mode, 0) };
2509
2510 return gc->set_config ? gc->set_config(gc, offset, config) : -ENOTSUPP;
2511}
2512
Linus Walleijfac9d882017-09-26 20:58:28 +02002513static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08002514{
Linus Walleijc663e5f2016-03-22 10:51:16 +01002515 struct gpio_chip *gc = desc->gdev->chip;
Linus Walleijad177312016-11-13 23:02:44 +01002516 int val = !!value;
Linus Walleijc663e5f2016-03-22 10:51:16 +01002517 int ret;
David Brownelld2876d02008-02-04 22:28:20 -08002518
Linus Walleijc663e5f2016-03-22 10:51:16 +01002519 if (!gc->set || !gc->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01002520 gpiod_warn(desc,
2521 "%s: missing set() or direction_output() operations\n",
2522 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002523 return -EIO;
2524 }
2525
Linus Walleijad177312016-11-13 23:02:44 +01002526 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
Linus Walleijc663e5f2016-03-22 10:51:16 +01002527 if (!ret)
David Brownelld2876d02008-02-04 22:28:20 -08002528 set_bit(FLAG_IS_OUT, &desc->flags);
Linus Walleijad177312016-11-13 23:02:44 +01002529 trace_gpio_value(desc_to_gpio(desc), 0, val);
Linus Walleijc663e5f2016-03-22 10:51:16 +01002530 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2531 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08002532}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002533
2534/**
2535 * gpiod_direction_output_raw - set the GPIO direction to output
2536 * @desc: GPIO to set to output
2537 * @value: initial output value of the GPIO
2538 *
2539 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2540 * be called safely on it. The initial value of the output must be specified
2541 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2542 *
2543 * Return 0 in case of success, else an error code.
2544 */
2545int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2546{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002547 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02002548 return gpiod_direction_output_raw_commit(desc, value);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002549}
2550EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2551
2552/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05302553 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002554 * @desc: GPIO to set to output
2555 * @value: initial output value of the GPIO
2556 *
2557 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2558 * be called safely on it. The initial value of the output must be specified
2559 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2560 * account.
2561 *
2562 * Return 0 in case of success, else an error code.
2563 */
2564int gpiod_direction_output(struct gpio_desc *desc, int value)
2565{
Vladimir Zapolskiy30322bc2017-12-21 18:37:24 +02002566 struct gpio_chip *gc;
Linus Walleij02e47982017-09-26 21:20:23 +02002567 int ret;
2568
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002569 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002570 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2571 value = !value;
Linus Walleijad177312016-11-13 23:02:44 +01002572 else
2573 value = !!value;
Linus Walleij02e47982017-09-26 21:20:23 +02002574
2575 /* GPIOs used for IRQs shall not be set as output */
2576 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
2577 gpiod_err(desc,
2578 "%s: tried to set a GPIO tied to an IRQ as output\n",
2579 __func__);
2580 return -EIO;
2581 }
2582
Vladimir Zapolskiy30322bc2017-12-21 18:37:24 +02002583 gc = desc->gdev->chip;
Linus Walleij02e47982017-09-26 21:20:23 +02002584 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2585 /* First see if we can enable open drain in hardware */
2586 ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2587 PIN_CONFIG_DRIVE_OPEN_DRAIN);
2588 if (!ret)
2589 goto set_output_value;
2590 /* Emulate open drain by not actively driving the line high */
2591 if (value)
2592 return gpiod_direction_input(desc);
2593 }
2594 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2595 ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2596 PIN_CONFIG_DRIVE_OPEN_SOURCE);
2597 if (!ret)
2598 goto set_output_value;
2599 /* Emulate open source by not actively driving the line low */
2600 if (!value)
2601 return gpiod_direction_input(desc);
2602 } else {
2603 gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2604 PIN_CONFIG_DRIVE_PUSH_PULL);
2605 }
2606
2607set_output_value:
Linus Walleijfac9d882017-09-26 20:58:28 +02002608 return gpiod_direction_output_raw_commit(desc, value);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002609}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002610EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08002611
Felipe Balbic4b5be92010-05-26 14:42:23 -07002612/**
Thierry Reding950d55f52017-07-24 16:57:22 +02002613 * gpiod_set_debounce - sets @debounce time for a GPIO
2614 * @desc: descriptor of the GPIO for which to set debounce time
2615 * @debounce: debounce time in microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02002616 *
Thierry Reding950d55f52017-07-24 16:57:22 +02002617 * Returns:
2618 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2619 * debounce time.
Felipe Balbic4b5be92010-05-26 14:42:23 -07002620 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002621int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07002622{
Felipe Balbic4b5be92010-05-26 14:42:23 -07002623 struct gpio_chip *chip;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002624 unsigned long config;
Felipe Balbic4b5be92010-05-26 14:42:23 -07002625
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002626 VALIDATE_DESC(desc);
2627 chip = desc->gdev->chip;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002628 if (!chip->set || !chip->set_config) {
Mark Brown6424de52013-09-09 10:33:49 +01002629 gpiod_dbg(desc,
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002630 "%s: missing set() or set_config() operations\n",
Mark Brown6424de52013-09-09 10:33:49 +01002631 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02002632 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002633 }
2634
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002635 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
2636 return chip->set_config(chip, gpio_chip_hwgpio(desc), config);
Felipe Balbic4b5be92010-05-26 14:42:23 -07002637}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002638EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002639
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002640/**
Andrew Jefferye10f72b2017-11-30 14:25:24 +10302641 * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
2642 * @desc: descriptor of the GPIO for which to configure persistence
2643 * @transitory: True to lose state on suspend or reset, false for persistence
2644 *
2645 * Returns:
2646 * 0 on success, otherwise a negative error code.
2647 */
2648int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
2649{
2650 struct gpio_chip *chip;
2651 unsigned long packed;
2652 int gpio;
2653 int rc;
2654
Vladimir Zapolskiy156dd392017-12-21 18:37:35 +02002655 VALIDATE_DESC(desc);
Andrew Jefferye10f72b2017-11-30 14:25:24 +10302656 /*
2657 * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
2658 * persistence state.
2659 */
2660 if (transitory)
2661 set_bit(FLAG_TRANSITORY, &desc->flags);
2662 else
2663 clear_bit(FLAG_TRANSITORY, &desc->flags);
2664
2665 /* If the driver supports it, set the persistence state now */
2666 chip = desc->gdev->chip;
2667 if (!chip->set_config)
2668 return 0;
2669
2670 packed = pinconf_to_config_packed(PIN_CONFIG_PERSIST_STATE,
2671 !transitory);
2672 gpio = gpio_chip_hwgpio(desc);
2673 rc = chip->set_config(chip, gpio, packed);
2674 if (rc == -ENOTSUPP) {
2675 dev_dbg(&desc->gdev->dev, "Persistence not supported for GPIO %d\n",
2676 gpio);
2677 return 0;
2678 }
2679
2680 return rc;
2681}
2682EXPORT_SYMBOL_GPL(gpiod_set_transitory);
2683
2684/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002685 * gpiod_is_active_low - test whether a GPIO is active-low or not
2686 * @desc: the gpio descriptor to test
2687 *
2688 * Returns 1 if the GPIO is active-low, 0 otherwise.
2689 */
2690int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002691{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002692 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002693 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002694}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002695EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08002696
2697/* I/O calls are only valid after configuration completed; the relevant
2698 * "is this a valid GPIO" error checks should already have been done.
2699 *
2700 * "Get" operations are often inlinable as reading a pin value register,
2701 * and masking the relevant bit in that register.
2702 *
2703 * When "set" operations are inlinable, they involve writing that mask to
2704 * one register to set a low value, or a different register to set it high.
2705 * Otherwise locking is needed, so there may be little value to inlining.
2706 *
2707 *------------------------------------------------------------------------
2708 *
2709 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2710 * have requested the GPIO. That can include implicit requesting by
2711 * a direction setting call. Marking a gpio as requested locks its chip
2712 * in memory, guaranteeing that these table lookups need no more locking
2713 * and that gpiochip_remove() will fail.
2714 *
2715 * REVISIT when debugging, consider adding some instrumentation to ensure
2716 * that the GPIO was actually requested.
2717 */
2718
Linus Walleijfac9d882017-09-26 20:58:28 +02002719static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002720{
2721 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002722 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002723 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002724
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002725 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002726 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002727 value = chip->get ? chip->get(chip, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01002728 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002729 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002730 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002731}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002732
Lukas Wunnereec1d562017-10-12 12:40:10 +02002733static int gpio_chip_get_multiple(struct gpio_chip *chip,
2734 unsigned long *mask, unsigned long *bits)
2735{
2736 if (chip->get_multiple) {
2737 return chip->get_multiple(chip, mask, bits);
2738 } else if (chip->get) {
2739 int i, value;
2740
2741 for_each_set_bit(i, mask, chip->ngpio) {
2742 value = chip->get(chip, i);
2743 if (value < 0)
2744 return value;
2745 __assign_bit(i, bits, value);
2746 }
2747 return 0;
2748 }
2749 return -EIO;
2750}
2751
2752int gpiod_get_array_value_complex(bool raw, bool can_sleep,
2753 unsigned int array_size,
2754 struct gpio_desc **desc_array,
2755 int *value_array)
2756{
2757 int i = 0;
2758
2759 while (i < array_size) {
2760 struct gpio_chip *chip = desc_array[i]->gdev->chip;
2761 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2762 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2763 int first, j, ret;
2764
2765 if (!can_sleep)
2766 WARN_ON(chip->can_sleep);
2767
2768 /* collect all inputs belonging to the same chip */
2769 first = i;
2770 memset(mask, 0, sizeof(mask));
2771 do {
2772 const struct gpio_desc *desc = desc_array[i];
2773 int hwgpio = gpio_chip_hwgpio(desc);
2774
2775 __set_bit(hwgpio, mask);
2776 i++;
2777 } while ((i < array_size) &&
2778 (desc_array[i]->gdev->chip == chip));
2779
2780 ret = gpio_chip_get_multiple(chip, mask, bits);
2781 if (ret)
2782 return ret;
2783
2784 for (j = first; j < i; j++) {
2785 const struct gpio_desc *desc = desc_array[j];
2786 int hwgpio = gpio_chip_hwgpio(desc);
2787 int value = test_bit(hwgpio, bits);
2788
2789 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2790 value = !value;
2791 value_array[j] = value;
2792 trace_gpio_value(desc_to_gpio(desc), 1, value);
2793 }
2794 }
2795 return 0;
2796}
2797
David Brownelld2876d02008-02-04 22:28:20 -08002798/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002799 * gpiod_get_raw_value() - return a gpio's raw value
2800 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08002801 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002802 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002803 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002804 *
2805 * This function should be called from contexts where we cannot sleep, and will
2806 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002807 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002808int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002809{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002810 VALIDATE_DESC(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002811 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002812 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02002813 return gpiod_get_raw_value_commit(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002814}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002815EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002816
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002817/**
2818 * gpiod_get_value() - return a gpio's value
2819 * @desc: gpio whose value will be returned
2820 *
2821 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002822 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002823 *
2824 * This function should be called from contexts where we cannot sleep, and will
2825 * complain if the GPIO chip functions potentially sleep.
2826 */
2827int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002828{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002829 int value;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002830
2831 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002832 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002833 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002834
Linus Walleijfac9d882017-09-26 20:58:28 +02002835 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002836 if (value < 0)
2837 return value;
2838
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002839 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2840 value = !value;
2841
2842 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002843}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002844EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08002845
Lukas Wunnereec1d562017-10-12 12:40:10 +02002846/**
2847 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
2848 * @array_size: number of elements in the descriptor / value arrays
2849 * @desc_array: array of GPIO descriptors whose values will be read
2850 * @value_array: array to store the read values
2851 *
2852 * Read the raw values of the GPIOs, i.e. the values of the physical lines
2853 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
2854 * else an error code.
2855 *
2856 * This function should be called from contexts where we cannot sleep,
2857 * and it will complain if the GPIO chip functions potentially sleep.
2858 */
2859int gpiod_get_raw_array_value(unsigned int array_size,
2860 struct gpio_desc **desc_array, int *value_array)
2861{
2862 if (!desc_array)
2863 return -EINVAL;
2864 return gpiod_get_array_value_complex(true, false, array_size,
2865 desc_array, value_array);
2866}
2867EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
2868
2869/**
2870 * gpiod_get_array_value() - read values from an array of GPIOs
2871 * @array_size: number of elements in the descriptor / value arrays
2872 * @desc_array: array of GPIO descriptors whose values will be read
2873 * @value_array: array to store the read values
2874 *
2875 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2876 * into account. Return 0 in case of success, else an error code.
2877 *
2878 * This function should be called from contexts where we cannot sleep,
2879 * and it will complain if the GPIO chip functions potentially sleep.
2880 */
2881int gpiod_get_array_value(unsigned int array_size,
2882 struct gpio_desc **desc_array, int *value_array)
2883{
2884 if (!desc_array)
2885 return -EINVAL;
2886 return gpiod_get_array_value_complex(false, false, array_size,
2887 desc_array, value_array);
2888}
2889EXPORT_SYMBOL_GPL(gpiod_get_array_value);
2890
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302891/*
Linus Walleijfac9d882017-09-26 20:58:28 +02002892 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002893 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002894 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302895 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002896static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302897{
2898 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002899 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002900 int offset = gpio_chip_hwgpio(desc);
2901
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302902 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002903 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302904 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002905 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302906 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002907 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302908 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002909 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302910 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002911 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302912 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002913 gpiod_err(desc,
2914 "%s: Error in set_value for open drain err %d\n",
2915 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302916}
2917
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302918/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002919 * _gpio_set_open_source_value() - Set the open source gpio's value.
2920 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002921 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302922 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002923static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302924{
2925 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002926 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002927 int offset = gpio_chip_hwgpio(desc);
2928
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302929 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002930 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302931 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002932 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302933 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002934 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302935 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002936 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302937 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002938 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302939 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002940 gpiod_err(desc,
2941 "%s: Error in set_value for open source err %d\n",
2942 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302943}
2944
Linus Walleijfac9d882017-09-26 20:58:28 +02002945static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08002946{
2947 struct gpio_chip *chip;
2948
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002949 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002950 trace_gpio_value(desc_to_gpio(desc), 0, value);
Linus Walleij02e47982017-09-26 21:20:23 +02002951 chip->set(chip, gpio_chip_hwgpio(desc), value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002952}
2953
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002954/*
2955 * set multiple outputs on the same chip;
2956 * use the chip's set_multiple function if available;
2957 * otherwise set the outputs sequentially;
2958 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
2959 * defines which outputs are to be changed
2960 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
2961 * defines the values the outputs specified by mask are to be set to
2962 */
2963static void gpio_chip_set_multiple(struct gpio_chip *chip,
2964 unsigned long *mask, unsigned long *bits)
2965{
2966 if (chip->set_multiple) {
2967 chip->set_multiple(chip, mask, bits);
2968 } else {
Andy Shevchenko5e4e6fb2017-01-03 19:01:17 +02002969 unsigned int i;
2970
2971 /* set outputs if the corresponding mask bit is set */
2972 for_each_set_bit(i, mask, chip->ngpio)
2973 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002974 }
2975}
2976
Linus Walleij44c72882016-04-24 11:36:59 +02002977void gpiod_set_array_value_complex(bool raw, bool can_sleep,
2978 unsigned int array_size,
2979 struct gpio_desc **desc_array,
2980 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002981{
2982 int i = 0;
2983
2984 while (i < array_size) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002985 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002986 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2987 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2988 int count = 0;
2989
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002990 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002991 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002992
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002993 memset(mask, 0, sizeof(mask));
2994 do {
2995 struct gpio_desc *desc = desc_array[i];
2996 int hwgpio = gpio_chip_hwgpio(desc);
2997 int value = value_array[i];
2998
2999 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3000 value = !value;
3001 trace_gpio_value(desc_to_gpio(desc), 0, value);
3002 /*
3003 * collect all normal outputs belonging to the same chip
3004 * open drain and open source outputs are set individually
3005 */
Linus Walleij02e47982017-09-26 21:20:23 +02003006 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02003007 gpio_set_open_drain_value_commit(desc, value);
Linus Walleij02e47982017-09-26 21:20:23 +02003008 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02003009 gpio_set_open_source_value_commit(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003010 } else {
3011 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003012 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003013 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003014 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003015 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003016 count++;
3017 }
3018 i++;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003019 } while ((i < array_size) &&
3020 (desc_array[i]->gdev->chip == chip));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003021 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01003022 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003023 gpio_chip_set_multiple(chip, mask, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003024 }
3025}
3026
David Brownelld2876d02008-02-04 22:28:20 -08003027/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003028 * gpiod_set_raw_value() - assign a gpio's raw value
3029 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08003030 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08003031 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003032 * Set the raw value of the GPIO, i.e. the value of its physical line without
3033 * regard for its ACTIVE_LOW status.
3034 *
3035 * This function should be called from contexts where we cannot sleep, and will
3036 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003037 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003038void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003039{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003040 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01003041 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003042 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02003043 gpiod_set_raw_value_commit(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08003044}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003045EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08003046
3047/**
Geert Uytterhoeven1e77fc82018-01-09 19:08:21 +01003048 * gpiod_set_value_nocheck() - set a GPIO line value without checking
3049 * @desc: the descriptor to set the value on
3050 * @value: value to set
3051 *
3052 * This sets the value of a GPIO line backing a descriptor, applying
3053 * different semantic quirks like active low and open drain/source
3054 * handling.
3055 */
3056static void gpiod_set_value_nocheck(struct gpio_desc *desc, int value)
3057{
3058 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3059 value = !value;
3060 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
3061 gpio_set_open_drain_value_commit(desc, value);
3062 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
3063 gpio_set_open_source_value_commit(desc, value);
3064 else
3065 gpiod_set_raw_value_commit(desc, value);
3066}
3067
3068/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003069 * gpiod_set_value() - assign a gpio's value
3070 * @desc: gpio whose value will be assigned
3071 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08003072 *
Linus Walleij02e47982017-09-26 21:20:23 +02003073 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
3074 * OPEN_DRAIN and OPEN_SOURCE flags into account.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003075 *
3076 * This function should be called from contexts where we cannot sleep, and will
3077 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003078 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003079void gpiod_set_value(struct gpio_desc *desc, int value)
3080{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003081 VALIDATE_DESC_VOID(desc);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003082 WARN_ON(desc->gdev->chip->can_sleep);
Geert Uytterhoeven1e77fc82018-01-09 19:08:21 +01003083 gpiod_set_value_nocheck(desc, value);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003084}
3085EXPORT_SYMBOL_GPL(gpiod_set_value);
3086
3087/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003088 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003089 * @array_size: number of elements in the descriptor / value arrays
3090 * @desc_array: array of GPIO descriptors whose values will be assigned
3091 * @value_array: array of values to assign
3092 *
3093 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3094 * without regard for their ACTIVE_LOW status.
3095 *
3096 * This function should be called from contexts where we cannot sleep, and will
3097 * complain if the GPIO chip functions potentially sleep.
3098 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003099void gpiod_set_raw_array_value(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003100 struct gpio_desc **desc_array, int *value_array)
3101{
3102 if (!desc_array)
3103 return;
Linus Walleij44c72882016-04-24 11:36:59 +02003104 gpiod_set_array_value_complex(true, false, array_size, desc_array,
3105 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003106}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003107EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003108
3109/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003110 * gpiod_set_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003111 * @array_size: number of elements in the descriptor / value arrays
3112 * @desc_array: array of GPIO descriptors whose values will be assigned
3113 * @value_array: array of values to assign
3114 *
3115 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3116 * into account.
3117 *
3118 * This function should be called from contexts where we cannot sleep, and will
3119 * complain if the GPIO chip functions potentially sleep.
3120 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003121void gpiod_set_array_value(unsigned int array_size,
3122 struct gpio_desc **desc_array, int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003123{
3124 if (!desc_array)
3125 return;
Linus Walleij44c72882016-04-24 11:36:59 +02003126 gpiod_set_array_value_complex(false, false, array_size, desc_array,
3127 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003128}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003129EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003130
3131/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003132 * gpiod_cansleep() - report whether gpio value access may sleep
3133 * @desc: gpio to check
3134 *
3135 */
3136int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003137{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003138 VALIDATE_DESC(desc);
3139 return desc->gdev->chip->can_sleep;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003140}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003141EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08003142
David Brownell0f6d5042008-10-15 22:03:14 -07003143/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003144 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
3145 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07003146 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003147 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
3148 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07003149 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003150int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07003151{
Linus Walleij4c37ce82016-05-02 13:13:10 +02003152 struct gpio_chip *chip;
3153 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07003154
Linus Walleij79bb71b2016-06-15 22:57:38 +02003155 /*
3156 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
3157 * requires this function to not return zero on an invalid descriptor
3158 * but rather a negative error number.
3159 */
Linus Walleijbfbbe442016-06-16 11:55:55 +02003160 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
Linus Walleij79bb71b2016-06-15 22:57:38 +02003161 return -EINVAL;
3162
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003163 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003164 offset = gpio_chip_hwgpio(desc);
Linus Walleij4c37ce82016-05-02 13:13:10 +02003165 if (chip->to_irq) {
3166 int retirq = chip->to_irq(chip, offset);
3167
3168 /* Zero means NO_IRQ */
3169 if (!retirq)
3170 return -ENXIO;
3171
3172 return retirq;
3173 }
3174 return -ENXIO;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003175}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003176EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003177
Linus Walleijd468bf92013-09-24 11:54:38 +02003178/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003179 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003180 * @chip: the chip the GPIO to lock belongs to
3181 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02003182 *
3183 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08003184 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08003185 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003186int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08003187{
Linus Walleij9c102802016-05-25 10:56:03 +02003188 struct gpio_desc *desc;
Linus Walleijd468bf92013-09-24 11:54:38 +02003189
Linus Walleij9c102802016-05-25 10:56:03 +02003190 desc = gpiochip_get_desc(chip, offset);
3191 if (IS_ERR(desc))
3192 return PTR_ERR(desc);
3193
Linus Walleij60f83392016-11-12 15:01:09 +01003194 /*
3195 * If it's fast: flush the direction setting if something changed
3196 * behind our back
3197 */
3198 if (!chip->can_sleep && chip->get_direction) {
Linus Walleij9c102802016-05-25 10:56:03 +02003199 int dir = chip->get_direction(chip, offset);
3200
3201 if (dir)
3202 clear_bit(FLAG_IS_OUT, &desc->flags);
3203 else
3204 set_bit(FLAG_IS_OUT, &desc->flags);
3205 }
3206
3207 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003208 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02003209 "%s: tried to flag a GPIO set as output for IRQ\n",
3210 __func__);
3211 return -EIO;
3212 }
3213
Linus Walleij9c102802016-05-25 10:56:03 +02003214 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
Linus Walleij3940c342016-11-14 00:09:07 +01003215
3216 /*
3217 * If the consumer has not set up a label (such as when the
3218 * IRQ is referenced from .to_irq()) we set up a label here
3219 * so it is clear this is used as an interrupt.
3220 */
3221 if (!desc->label)
3222 desc_set_label(desc, "interrupt");
3223
Linus Walleijd468bf92013-09-24 11:54:38 +02003224 return 0;
3225}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003226EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02003227
Linus Walleijd468bf92013-09-24 11:54:38 +02003228/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003229 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003230 * @chip: the chip the GPIO to lock belongs to
3231 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02003232 *
3233 * This is used directly by GPIO drivers that want to indicate
3234 * that a certain GPIO is no longer used exclusively for IRQ.
3235 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003236void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02003237{
Linus Walleij3940c342016-11-14 00:09:07 +01003238 struct gpio_desc *desc;
3239
3240 desc = gpiochip_get_desc(chip, offset);
3241 if (IS_ERR(desc))
Linus Walleijd468bf92013-09-24 11:54:38 +02003242 return;
3243
Linus Walleij3940c342016-11-14 00:09:07 +01003244 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
3245
3246 /* If we only had this marking, erase it */
3247 if (desc->label && !strcmp(desc->label, "interrupt"))
3248 desc_set_label(desc, NULL);
Linus Walleijd468bf92013-09-24 11:54:38 +02003249}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003250EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02003251
Linus Walleij6cee3822016-02-11 20:16:45 +01003252bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
3253{
3254 if (offset >= chip->ngpio)
3255 return false;
3256
3257 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
3258}
3259EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
3260
Linus Walleij143b65d2016-02-16 15:41:42 +01003261bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
3262{
3263 if (offset >= chip->ngpio)
3264 return false;
3265
3266 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
3267}
3268EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
3269
3270bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
3271{
3272 if (offset >= chip->ngpio)
3273 return false;
3274
3275 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
3276}
3277EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
3278
Charles Keepax05f479b2017-05-23 15:47:29 +01003279bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset)
3280{
3281 if (offset >= chip->ngpio)
3282 return false;
3283
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303284 return !test_bit(FLAG_TRANSITORY, &chip->gpiodev->descs[offset].flags);
Charles Keepax05f479b2017-05-23 15:47:29 +01003285}
3286EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
3287
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003288/**
3289 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
3290 * @desc: gpio whose value will be returned
3291 *
3292 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003293 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003294 *
3295 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003296 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003297int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08003298{
David Brownelld2876d02008-02-04 22:28:20 -08003299 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003300 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003301 return gpiod_get_raw_value_commit(desc);
David Brownelld2876d02008-02-04 22:28:20 -08003302}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003303EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003304
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003305/**
3306 * gpiod_get_value_cansleep() - return a gpio's value
3307 * @desc: gpio whose value will be returned
3308 *
3309 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003310 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003311 *
3312 * This function is to be called from contexts that can sleep.
3313 */
3314int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003315{
David Brownelld2876d02008-02-04 22:28:20 -08003316 int value;
David Brownelld2876d02008-02-04 22:28:20 -08003317
3318 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003319 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003320 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003321 if (value < 0)
3322 return value;
3323
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003324 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3325 value = !value;
3326
David Brownelld2876d02008-02-04 22:28:20 -08003327 return value;
3328}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003329EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003330
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003331/**
Lukas Wunnereec1d562017-10-12 12:40:10 +02003332 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
3333 * @array_size: number of elements in the descriptor / value arrays
3334 * @desc_array: array of GPIO descriptors whose values will be read
3335 * @value_array: array to store the read values
3336 *
3337 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3338 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3339 * else an error code.
3340 *
3341 * This function is to be called from contexts that can sleep.
3342 */
3343int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
3344 struct gpio_desc **desc_array,
3345 int *value_array)
3346{
3347 might_sleep_if(extra_checks);
3348 if (!desc_array)
3349 return -EINVAL;
3350 return gpiod_get_array_value_complex(true, true, array_size,
3351 desc_array, value_array);
3352}
3353EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
3354
3355/**
3356 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
3357 * @array_size: number of elements in the descriptor / value arrays
3358 * @desc_array: array of GPIO descriptors whose values will be read
3359 * @value_array: array to store the read values
3360 *
3361 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3362 * into account. Return 0 in case of success, else an error code.
3363 *
3364 * This function is to be called from contexts that can sleep.
3365 */
3366int gpiod_get_array_value_cansleep(unsigned int array_size,
3367 struct gpio_desc **desc_array,
3368 int *value_array)
3369{
3370 might_sleep_if(extra_checks);
3371 if (!desc_array)
3372 return -EINVAL;
3373 return gpiod_get_array_value_complex(false, true, array_size,
3374 desc_array, value_array);
3375}
3376EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
3377
3378/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003379 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
3380 * @desc: gpio whose value will be assigned
3381 * @value: value to assign
3382 *
3383 * Set the raw value of the GPIO, i.e. the value of its physical line without
3384 * regard for its ACTIVE_LOW status.
3385 *
3386 * This function is to be called from contexts that can sleep.
3387 */
3388void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003389{
David Brownelld2876d02008-02-04 22:28:20 -08003390 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003391 VALIDATE_DESC_VOID(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003392 gpiod_set_raw_value_commit(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003393}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003394EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003395
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003396/**
3397 * gpiod_set_value_cansleep() - assign a gpio's value
3398 * @desc: gpio whose value will be assigned
3399 * @value: value to assign
3400 *
3401 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3402 * account
3403 *
3404 * This function is to be called from contexts that can sleep.
3405 */
3406void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003407{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003408 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003409 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1e77fc82018-01-09 19:08:21 +01003410 gpiod_set_value_nocheck(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08003411}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003412EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08003413
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003414/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003415 * gpiod_set_raw_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 raw values of the GPIOs, i.e. the values of the physical lines
3421 * without regard for their ACTIVE_LOW status.
3422 *
3423 * This function is to be called from contexts that can sleep.
3424 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003425void gpiod_set_raw_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(true, 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_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003436
3437/**
Dmitry Torokhov3946d182017-08-14 21:59:55 -07003438 * gpiod_add_lookup_tables() - register GPIO device consumers
3439 * @tables: list of tables of consumers to register
3440 * @n: number of tables in the list
3441 */
3442void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
3443{
3444 unsigned int i;
3445
3446 mutex_lock(&gpio_lookup_lock);
3447
3448 for (i = 0; i < n; i++)
3449 list_add_tail(&tables[i]->list, &gpio_lookup_list);
3450
3451 mutex_unlock(&gpio_lookup_lock);
3452}
3453
3454/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003455 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003456 * @array_size: number of elements in the descriptor / value arrays
3457 * @desc_array: array of GPIO descriptors whose values will be assigned
3458 * @value_array: array of values to assign
3459 *
3460 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3461 * into account.
3462 *
3463 * This function is to be called from contexts that can sleep.
3464 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003465void gpiod_set_array_value_cansleep(unsigned int array_size,
3466 struct gpio_desc **desc_array,
3467 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003468{
3469 might_sleep_if(extra_checks);
3470 if (!desc_array)
3471 return;
Linus Walleij44c72882016-04-24 11:36:59 +02003472 gpiod_set_array_value_complex(false, true, array_size, desc_array,
3473 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003474}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003475EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003476
3477/**
Alexandre Courbotad824782013-12-03 12:20:11 +09003478 * gpiod_add_lookup_table() - register GPIO device consumers
3479 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003480 */
Alexandre Courbotad824782013-12-03 12:20:11 +09003481void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003482{
3483 mutex_lock(&gpio_lookup_lock);
3484
Alexandre Courbotad824782013-12-03 12:20:11 +09003485 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003486
3487 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08003488}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02003489EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
David Brownelld2876d02008-02-04 22:28:20 -08003490
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05303491/**
3492 * gpiod_remove_lookup_table() - unregister GPIO device consumers
3493 * @table: table of consumers to unregister
3494 */
3495void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
3496{
3497 mutex_lock(&gpio_lookup_lock);
3498
3499 list_del(&table->list);
3500
3501 mutex_unlock(&gpio_lookup_lock);
3502}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02003503EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05303504
Bartosz Golaszewskia411e812018-04-10 22:30:28 +02003505/**
3506 * gpiod_add_hogs() - register a set of GPIO hogs from machine code
3507 * @hogs: table of gpio hog entries with a zeroed sentinel at the end
3508 */
3509void gpiod_add_hogs(struct gpiod_hog *hogs)
3510{
3511 struct gpio_chip *chip;
3512 struct gpiod_hog *hog;
3513
3514 mutex_lock(&gpio_machine_hogs_mutex);
3515
3516 for (hog = &hogs[0]; hog->chip_label; hog++) {
3517 list_add_tail(&hog->list, &gpio_machine_hogs);
3518
3519 /*
3520 * The chip may have been registered earlier, so check if it
3521 * exists and, if so, try to hog the line now.
3522 */
3523 chip = find_chip_by_name(hog->chip_label);
3524 if (chip)
3525 gpiochip_machine_hog(chip, hog);
3526 }
3527
3528 mutex_unlock(&gpio_machine_hogs_mutex);
3529}
3530EXPORT_SYMBOL_GPL(gpiod_add_hogs);
3531
Alexandre Courbotad824782013-12-03 12:20:11 +09003532static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
3533{
3534 const char *dev_id = dev ? dev_name(dev) : NULL;
3535 struct gpiod_lookup_table *table;
3536
3537 mutex_lock(&gpio_lookup_lock);
3538
3539 list_for_each_entry(table, &gpio_lookup_list, list) {
3540 if (table->dev_id && dev_id) {
3541 /*
3542 * Valid strings on both ends, must be identical to have
3543 * a match
3544 */
3545 if (!strcmp(table->dev_id, dev_id))
3546 goto found;
3547 } else {
3548 /*
3549 * One of the pointers is NULL, so both must be to have
3550 * a match
3551 */
3552 if (dev_id == table->dev_id)
3553 goto found;
3554 }
3555 }
3556 table = NULL;
3557
3558found:
3559 mutex_unlock(&gpio_lookup_lock);
3560 return table;
3561}
3562
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003563static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09003564 unsigned int idx,
3565 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003566{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003567 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09003568 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003569 struct gpiod_lookup *p;
3570
Alexandre Courbotad824782013-12-03 12:20:11 +09003571 table = gpiod_find_lookup_table(dev);
3572 if (!table)
3573 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003574
Alexandre Courbotad824782013-12-03 12:20:11 +09003575 for (p = &table->table[0]; p->chip_label; p++) {
3576 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003577
Alexandre Courbotad824782013-12-03 12:20:11 +09003578 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003579 if (p->idx != idx)
3580 continue;
3581
Alexandre Courbotad824782013-12-03 12:20:11 +09003582 /* If the lookup entry has a con_id, require exact match */
3583 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
3584 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003585
Alexandre Courbotad824782013-12-03 12:20:11 +09003586 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003587
Alexandre Courbotad824782013-12-03 12:20:11 +09003588 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003589 dev_err(dev, "cannot find GPIO chip %s\n",
3590 p->chip_label);
3591 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003592 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003593
Alexandre Courbotad824782013-12-03 12:20:11 +09003594 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003595 dev_err(dev,
3596 "requested GPIO %d is out of range [0..%d] for chip %s\n",
3597 idx, chip->ngpio, chip->label);
3598 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09003599 }
3600
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09003601 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09003602 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003603
3604 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09003605 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003606
3607 return desc;
3608}
3609
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003610static int dt_gpio_count(struct device *dev, const char *con_id)
3611{
3612 int ret;
3613 char propname[32];
3614 unsigned int i;
3615
3616 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
3617 if (con_id)
3618 snprintf(propname, sizeof(propname), "%s-%s",
3619 con_id, gpio_suffixes[i]);
3620 else
3621 snprintf(propname, sizeof(propname), "%s",
3622 gpio_suffixes[i]);
3623
3624 ret = of_gpio_named_count(dev->of_node, propname);
Andy Shevchenko4033d4a2017-02-20 18:15:47 +02003625 if (ret > 0)
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003626 break;
3627 }
Andy Shevchenko4033d4a2017-02-20 18:15:47 +02003628 return ret ? ret : -ENOENT;
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003629}
3630
3631static int platform_gpio_count(struct device *dev, const char *con_id)
3632{
3633 struct gpiod_lookup_table *table;
3634 struct gpiod_lookup *p;
3635 unsigned int count = 0;
3636
3637 table = gpiod_find_lookup_table(dev);
3638 if (!table)
3639 return -ENOENT;
3640
3641 for (p = &table->table[0]; p->chip_label; p++) {
3642 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3643 (!con_id && !p->con_id))
3644 count++;
3645 }
3646 if (!count)
3647 return -ENOENT;
3648
3649 return count;
3650}
3651
3652/**
3653 * gpiod_count - return the number of GPIOs associated with a device / function
3654 * or -ENOENT if no GPIO has been assigned to the requested function
3655 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3656 * @con_id: function within the GPIO consumer
3657 */
3658int gpiod_count(struct device *dev, const char *con_id)
3659{
3660 int count = -ENOENT;
3661
3662 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
3663 count = dt_gpio_count(dev, con_id);
3664 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
3665 count = acpi_gpio_count(dev, con_id);
3666
3667 if (count < 0)
3668 count = platform_gpio_count(dev, con_id);
3669
3670 return count;
3671}
3672EXPORT_SYMBOL_GPL(gpiod_count);
3673
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003674/**
Thierry Reding08791622014-04-25 16:54:22 +02003675 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09003676 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003677 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003678 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003679 *
3680 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003681 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07003682 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003683 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003684struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003685 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003686{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003687 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003688}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003689EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003690
3691/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02003692 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
3693 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3694 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003695 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02003696 *
3697 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
3698 * the requested function it will return NULL. This is convenient for drivers
3699 * that need to handle optional GPIOs.
3700 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003701struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003702 const char *con_id,
3703 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003704{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003705 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003706}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003707EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003708
Benoit Parrotf625d462015-02-02 11:44:44 -06003709
3710/**
3711 * gpiod_configure_flags - helper function to configure a given GPIO
3712 * @desc: gpio whose value will be assigned
3713 * @con_id: function within the GPIO consumer
Johan Hovold85b03b32016-07-03 18:32:05 +02003714 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3715 * of_get_gpio_hog()
Benoit Parrotf625d462015-02-02 11:44:44 -06003716 * @dflags: gpiod_flags - optional GPIO initialization flags
3717 *
3718 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
3719 * requested function and/or index, or another IS_ERR() code if an error
3720 * occurred while trying to acquire the GPIO.
3721 */
Andy Shevchenkoc29fd9e2017-05-23 20:03:16 +03003722int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Johan Hovold85b03b32016-07-03 18:32:05 +02003723 unsigned long lflags, enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06003724{
3725 int status;
3726
Johan Hovold85b03b32016-07-03 18:32:05 +02003727 if (lflags & GPIO_ACTIVE_LOW)
3728 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
Linus Walleijf926dfc2017-09-10 19:26:22 +02003729
Johan Hovold85b03b32016-07-03 18:32:05 +02003730 if (lflags & GPIO_OPEN_DRAIN)
3731 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
Linus Walleijf926dfc2017-09-10 19:26:22 +02003732 else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) {
3733 /*
3734 * This enforces open drain mode from the consumer side.
3735 * This is necessary for some busses like I2C, but the lookup
3736 * should *REALLY* have specified them as open drain in the
3737 * first place, so print a little warning here.
3738 */
3739 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3740 gpiod_warn(desc,
3741 "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
3742 }
3743
Johan Hovold85b03b32016-07-03 18:32:05 +02003744 if (lflags & GPIO_OPEN_SOURCE)
3745 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303746
3747 status = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
3748 if (status < 0)
3749 return status;
Johan Hovold85b03b32016-07-03 18:32:05 +02003750
Benoit Parrotf625d462015-02-02 11:44:44 -06003751 /* No particular flag request, return here... */
3752 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
3753 pr_debug("no flags found for %s\n", con_id);
3754 return 0;
3755 }
3756
3757 /* Process flags */
3758 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
3759 status = gpiod_direction_output(desc,
Linus Walleijad177312016-11-13 23:02:44 +01003760 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
Benoit Parrotf625d462015-02-02 11:44:44 -06003761 else
3762 status = gpiod_direction_input(desc);
3763
3764 return status;
3765}
3766
Thierry Reding29a1f2332014-04-25 17:10:06 +02003767/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003768 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02003769 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003770 * @con_id: function within the GPIO consumer
3771 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003772 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003773 *
3774 * This variant of gpiod_get() allows to access GPIOs other than the first
3775 * defined one for functions that define several GPIOs.
3776 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003777 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
3778 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07003779 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003780 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003781struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003782 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003783 unsigned int idx,
3784 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003785{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003786 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003787 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003788 enum gpio_lookup_flags lookupflags = 0;
Linus Walleij7d18f0a2018-01-16 08:29:50 +01003789 /* Maybe we have a device name, maybe not */
3790 const char *devname = dev ? dev_name(dev) : "?";
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003791
3792 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
3793
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003794 if (dev) {
3795 /* Using device tree? */
3796 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
3797 dev_dbg(dev, "using device tree for GPIO lookup\n");
3798 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
3799 } else if (ACPI_COMPANION(dev)) {
3800 dev_dbg(dev, "using ACPI for GPIO lookup\n");
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +03003801 desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags);
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003802 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003803 }
3804
3805 /*
3806 * Either we are not using DT or ACPI, or their lookup did not return
3807 * a result. In that case, use platform lookup as a fallback.
3808 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003809 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04003810 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003811 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003812 }
3813
3814 if (IS_ERR(desc)) {
Wang Dongsheng9d5a1f22018-02-27 00:12:13 -08003815 dev_dbg(dev, "No GPIO consumer %s found\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003816 return desc;
3817 }
3818
Linus Walleij7d18f0a2018-01-16 08:29:50 +01003819 /*
3820 * If a connection label was passed use that, else attempt to use
3821 * the device name as label
3822 */
3823 status = gpiod_request(desc, con_id ? con_id : devname);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003824 if (status < 0)
3825 return ERR_PTR(status);
3826
Johan Hovold85b03b32016-07-03 18:32:05 +02003827 status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003828 if (status < 0) {
3829 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
3830 gpiod_put(desc);
3831 return ERR_PTR(status);
3832 }
3833
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003834 return desc;
3835}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003836EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003837
3838/**
Linus Walleij6392cca2017-12-29 02:07:54 +01003839 * gpiod_get_from_of_node() - obtain a GPIO from an OF node
3840 * @node: handle of the OF node
3841 * @propname: name of the DT property representing the GPIO
3842 * @index: index of the GPIO to obtain for the consumer
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003843 * @dflags: GPIO initialization flags
Thierry Reding950d55f52017-07-24 16:57:22 +02003844 * @label: label to attach to the requested GPIO
Mika Westerberg40b73182014-10-21 13:33:59 +02003845 *
Thierry Reding950d55f52017-07-24 16:57:22 +02003846 * Returns:
Andy Shevchenkoff213782017-02-28 17:03:12 +02003847 * On successful request the GPIO pin is configured in accordance with
Linus Walleij6392cca2017-12-29 02:07:54 +01003848 * provided @dflags. If the node does not have the requested GPIO
3849 * property, NULL is returned.
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003850 *
Mika Westerberg40b73182014-10-21 13:33:59 +02003851 * In case of error an ERR_PTR() is returned.
3852 */
Linus Walleij92542ed2017-12-29 22:52:02 +01003853struct gpio_desc *gpiod_get_from_of_node(struct device_node *node,
3854 const char *propname, int index,
3855 enum gpiod_flags dflags,
3856 const char *label)
Mika Westerberg40b73182014-10-21 13:33:59 +02003857{
Colin Ian King40a3c9d2018-01-16 11:56:11 +00003858 struct gpio_desc *desc;
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003859 unsigned long lflags = 0;
Linus Walleij6392cca2017-12-29 02:07:54 +01003860 enum of_gpio_flags flags;
Mika Westerberg40b73182014-10-21 13:33:59 +02003861 bool active_low = false;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003862 bool single_ended = false;
Laxman Dewangan4c0facd2017-04-06 19:05:52 +05303863 bool open_drain = false;
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303864 bool transitory = false;
Mika Westerberg40b73182014-10-21 13:33:59 +02003865 int ret;
3866
Linus Walleij6392cca2017-12-29 02:07:54 +01003867 desc = of_get_named_gpiod_flags(node, propname,
3868 index, &flags);
Mika Westerberg40b73182014-10-21 13:33:59 +02003869
Linus Walleij6392cca2017-12-29 02:07:54 +01003870 if (!desc || IS_ERR(desc)) {
3871 /* If it is not there, just return NULL */
3872 if (PTR_ERR(desc) == -ENOENT)
3873 return NULL;
3874 return desc;
Mika Westerberg40b73182014-10-21 13:33:59 +02003875 }
3876
Linus Walleij6392cca2017-12-29 02:07:54 +01003877 active_low = flags & OF_GPIO_ACTIVE_LOW;
3878 single_ended = flags & OF_GPIO_SINGLE_ENDED;
3879 open_drain = flags & OF_GPIO_OPEN_DRAIN;
3880 transitory = flags & OF_GPIO_TRANSITORY;
Mika Westerberg40b73182014-10-21 13:33:59 +02003881
Alexander Steinb2987d72017-01-12 17:39:24 +01003882 ret = gpiod_request(desc, label);
Johan Hovold85b03b32016-07-03 18:32:05 +02003883 if (ret)
3884 return ERR_PTR(ret);
3885
Mika Westerberg40b73182014-10-21 13:33:59 +02003886 if (active_low)
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003887 lflags |= GPIO_ACTIVE_LOW;
Mika Westerberg40b73182014-10-21 13:33:59 +02003888
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003889 if (single_ended) {
Laxman Dewangan4c0facd2017-04-06 19:05:52 +05303890 if (open_drain)
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003891 lflags |= GPIO_OPEN_DRAIN;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003892 else
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003893 lflags |= GPIO_OPEN_SOURCE;
3894 }
3895
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303896 if (transitory)
3897 lflags |= GPIO_TRANSITORY;
3898
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003899 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
3900 if (ret < 0) {
3901 gpiod_put(desc);
3902 return ERR_PTR(ret);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003903 }
3904
Mika Westerberg40b73182014-10-21 13:33:59 +02003905 return desc;
3906}
Linus Walleij92542ed2017-12-29 22:52:02 +01003907EXPORT_SYMBOL(gpiod_get_from_of_node);
Linus Walleij6392cca2017-12-29 02:07:54 +01003908
3909/**
Mika Westerberg40b73182014-10-21 13:33:59 +02003910 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
3911 * @fwnode: handle of the firmware node
3912 * @propname: name of the firmware property representing the GPIO
Linus Walleij6392cca2017-12-29 02:07:54 +01003913 * @index: index of the GPIO to obtain for the consumer
Mika Westerberg40b73182014-10-21 13:33:59 +02003914 * @dflags: GPIO initialization flags
3915 * @label: label to attach to the requested GPIO
3916 *
3917 * This function can be used for drivers that get their configuration
Linus Walleij6392cca2017-12-29 02:07:54 +01003918 * from opaque firmware.
Thierry Reding29a1f2332014-04-25 17:10:06 +02003919 *
Linus Walleij6392cca2017-12-29 02:07:54 +01003920 * The function properly finds the corresponding GPIO using whatever is the
Thierry Reding29a1f2332014-04-25 17:10:06 +02003921 * underlying firmware interface and then makes sure that the GPIO
3922 * descriptor is requested before it is returned to the caller.
3923 *
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003924 * Returns:
Thierry Reding29a1f2332014-04-25 17:10:06 +02003925 * On successful request the GPIO pin is configured in accordance with
3926 * provided @dflags.
3927 *
3928 * In case of error an ERR_PTR() is returned.
3929 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003930struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
Thierry Reding29a1f2332014-04-25 17:10:06 +02003931 const char *propname, int index,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003932 enum gpiod_flags dflags,
3933 const char *label)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003934{
3935 struct gpio_desc *desc = ERR_PTR(-ENODEV);
3936 unsigned long lflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003937 int ret;
3938
David Brownelld2876d02008-02-04 22:28:20 -08003939 if (!fwnode)
David Brownelld2876d02008-02-04 22:28:20 -08003940 return ERR_PTR(-EINVAL);
3941
3942 if (is_of_node(fwnode)) {
Linus Walleij6392cca2017-12-29 02:07:54 +01003943 desc = gpiod_get_from_of_node(to_of_node(fwnode),
3944 propname, index,
3945 dflags,
3946 label);
3947 return desc;
David Brownelld2876d02008-02-04 22:28:20 -08003948 } else if (is_acpi_node(fwnode)) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09003949 struct acpi_gpio_info info;
David Brownelld2876d02008-02-04 22:28:20 -08003950
Linus Walleijd468bf92013-09-24 11:54:38 +02003951 desc = acpi_node_get_gpiod(fwnode, propname, index, &info);
Linus Walleij6392cca2017-12-29 02:07:54 +01003952 if (IS_ERR(desc))
3953 return desc;
3954
3955 acpi_gpio_update_gpiod_flags(&dflags, &info);
3956
3957 if (info.polarity == GPIO_ACTIVE_LOW)
3958 lflags |= GPIO_ACTIVE_LOW;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003959 }
3960
Linus Walleij6392cca2017-12-29 02:07:54 +01003961 /* Currently only ACPI takes this path */
Thierry Redingf9c4a312012-04-12 13:26:01 +02003962 ret = gpiod_request(desc, label);
3963 if (ret)
3964 return ERR_PTR(ret);
Linus Walleijd468bf92013-09-24 11:54:38 +02003965
Thierry Redingf9c4a312012-04-12 13:26:01 +02003966 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
3967 if (ret < 0) {
3968 gpiod_put(desc);
3969 return ERR_PTR(ret);
3970 }
3971
3972 return desc;
Linus Walleijd468bf92013-09-24 11:54:38 +02003973}
3974EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
David Brownelld2876d02008-02-04 22:28:20 -08003975
3976/**
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07003977 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
David Brownelld8f388d82008-07-25 01:46:07 -07003978 * function
Linus Walleijd468bf92013-09-24 11:54:38 +02003979 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3980 * @con_id: function within the GPIO consumer
David Brownelld2876d02008-02-04 22:28:20 -08003981 * @index: index of the GPIO to obtain in the consumer
3982 * @flags: optional GPIO initialization flags
3983 *
3984 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
3985 * specified index was assigned to the requested function it will return NULL.
3986 * This is convenient for drivers that need to handle optional GPIOs.
Grant Likely362432a2013-02-09 09:41:49 +00003987 */
David Brownelld8f388d82008-07-25 01:46:07 -07003988struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003989 const char *con_id,
Thierry Redingf9c4a312012-04-12 13:26:01 +02003990 unsigned int index,
3991 enum gpiod_flags flags)
3992{
Grant Likely362432a2013-02-09 09:41:49 +00003993 struct gpio_desc *desc;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003994
Grant Likely362432a2013-02-09 09:41:49 +00003995 desc = gpiod_get_index(dev, con_id, index, flags);
3996 if (IS_ERR(desc)) {
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003997 if (PTR_ERR(desc) == -ENOENT)
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003998 return NULL;
David Brownelld2876d02008-02-04 22:28:20 -08003999 }
4000
Benoit Parrotf625d462015-02-02 11:44:44 -06004001 return desc;
4002}
4003EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
4004
4005/**
4006 * gpiod_hog - Hog the specified GPIO desc given the provided flags
4007 * @desc: gpio whose value will be assigned
4008 * @name: gpio line name
4009 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
4010 * of_get_gpio_hog()
4011 * @dflags: gpiod_flags - optional GPIO initialization flags
4012 */
4013int gpiod_hog(struct gpio_desc *desc, const char *name,
4014 unsigned long lflags, enum gpiod_flags dflags)
4015{
4016 struct gpio_chip *chip;
4017 struct gpio_desc *local_desc;
4018 int hwnum;
4019 int status;
4020
Laxman Dewanganc31a5712016-03-11 19:13:21 +05304021 chip = gpiod_to_chip(desc);
4022 hwnum = gpio_chip_hwgpio(desc);
4023
4024 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
Benoit Parrotf625d462015-02-02 11:44:44 -06004025 if (IS_ERR(local_desc)) {
4026 status = PTR_ERR(local_desc);
Johan Hovold85b03b32016-07-03 18:32:05 +02004027 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
Benoit Parrotf625d462015-02-02 11:44:44 -06004028 name, chip->label, hwnum, status);
Laxman Dewanganc31a5712016-03-11 19:13:21 +05304029 return status;
4030 }
Benoit Parrotf625d462015-02-02 11:44:44 -06004031
4032 status = gpiod_configure_flags(desc, name, lflags, dflags);
4033 if (status < 0) {
4034 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed, %d\n",
4035 name, chip->label, hwnum, status);
4036 gpiochip_free_own_desc(desc);
4037 return status;
4038 }
4039
4040 /* Mark GPIO as hogged so it can be identified and removed later */
4041 set_bit(FLAG_IS_HOGGED, &desc->flags);
4042
4043 pr_info("GPIO line %d (%s) hogged as %s%s\n",
4044 desc_to_gpio(desc), name,
4045 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
4046 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
4047 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
4048
4049 return 0;
4050}
4051
4052/**
4053 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
4054 * @chip: gpio chip to act on
4055 *
4056 * This is only used by of_gpiochip_remove to free hogged gpios
4057 */
Linus Walleij1c3cdb12016-02-09 13:51:59 +01004058static void gpiochip_free_hogs(struct gpio_chip *chip)
4059{
Benoit Parrotf625d462015-02-02 11:44:44 -06004060 int id;
4061
4062 for (id = 0; id < chip->ngpio; id++) {
4063 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004064 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
4065 }
4066}
4067
4068/**
4069 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
4070 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4071 * @con_id: function within the GPIO consumer
4072 * @flags: optional GPIO initialization flags
4073 *
4074 * This function acquires all the GPIOs defined under a given function.
4075 *
4076 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
4077 * no GPIO has been assigned to the requested function, or another IS_ERR()
4078 * code if an error occurred while trying to acquire the GPIOs.
4079 */
4080struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
4081 const char *con_id,
4082 enum gpiod_flags flags)
4083{
4084 struct gpio_desc *desc;
4085 struct gpio_descs *descs;
4086 int count;
4087
4088 count = gpiod_count(dev, con_id);
4089 if (count < 0)
4090 return ERR_PTR(count);
4091
4092 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
4093 GFP_KERNEL);
4094 if (!descs)
4095 return ERR_PTR(-ENOMEM);
4096
4097 for (descs->ndescs = 0; descs->ndescs < count; ) {
4098 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
4099 if (IS_ERR(desc)) {
4100 gpiod_put_array(descs);
4101 return ERR_CAST(desc);
4102 }
4103 descs->desc[descs->ndescs] = desc;
4104 descs->ndescs++;
4105 }
4106 return descs;
4107}
4108EXPORT_SYMBOL_GPL(gpiod_get_array);
4109
4110/**
4111 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
4112 * function
4113 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4114 * @con_id: function within the GPIO consumer
4115 * @flags: optional GPIO initialization flags
4116 *
4117 * This is equivalent to gpiod_get_array(), except that when no GPIO was
4118 * assigned to the requested function it will return NULL.
4119 */
4120struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
4121 const char *con_id,
4122 enum gpiod_flags flags)
4123{
4124 struct gpio_descs *descs;
4125
4126 descs = gpiod_get_array(dev, con_id, flags);
4127 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
4128 return NULL;
4129
David Brownelld2876d02008-02-04 22:28:20 -08004130 return descs;
4131}
4132EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
4133
4134/**
4135 * gpiod_put - dispose of a GPIO descriptor
4136 * @desc: GPIO descriptor to dispose of
4137 *
4138 * No descriptor can be used after gpiod_put() has been called on it.
4139 */
4140void gpiod_put(struct gpio_desc *desc)
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004141{
4142 gpiod_free(desc);
4143}
4144EXPORT_SYMBOL_GPL(gpiod_put);
4145
4146/**
4147 * gpiod_put_array - dispose of multiple GPIO descriptors
4148 * @descs: struct gpio_descs containing an array of descriptors
4149 */
4150void gpiod_put_array(struct gpio_descs *descs)
4151{
4152 unsigned int i;
4153
4154 for (i = 0; i < descs->ndescs; i++)
4155 gpiod_put(descs->desc[i]);
Linus Walleij3c702e92015-10-21 15:29:53 +02004156
4157 kfree(descs);
4158}
4159EXPORT_SYMBOL_GPL(gpiod_put_array);
4160
4161static int __init gpiolib_dev_init(void)
4162{
4163 int ret;
4164
4165 /* Register GPIO sysfs bus */
4166 ret = bus_register(&gpio_bus_type);
4167 if (ret < 0) {
4168 pr_err("gpiolib: could not register GPIO bus type\n");
4169 return ret;
4170 }
Guenter Roeck159f3cd2016-03-31 08:11:30 -07004171
4172 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
4173 if (ret < 0) {
Linus Walleij3c702e92015-10-21 15:29:53 +02004174 pr_err("gpiolib: failed to allocate char dev region\n");
4175 bus_unregister(&gpio_bus_type);
4176 } else {
4177 gpiolib_initialized = true;
4178 gpiochip_setup_devs();
David Brownelld2876d02008-02-04 22:28:20 -08004179 }
4180 return ret;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004181}
David Brownelld2876d02008-02-04 22:28:20 -08004182core_initcall(gpiolib_dev_init);
4183
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004184#ifdef CONFIG_DEBUG_FS
4185
4186static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
David Brownelld2876d02008-02-04 22:28:20 -08004187{
4188 unsigned i;
4189 struct gpio_chip *chip = gdev->chip;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004190 unsigned gpio = gdev->base;
Markus Pargmannced433e2015-08-14 16:11:02 +02004191 struct gpio_desc *gdesc = &gdev->descs[0];
4192 int is_out;
4193 int is_irq;
4194
4195 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
David Brownelld2876d02008-02-04 22:28:20 -08004196 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
Markus Pargmannced433e2015-08-14 16:11:02 +02004197 if (gdesc->name) {
David Brownelld2876d02008-02-04 22:28:20 -08004198 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
4199 gpio, gdesc->name);
4200 }
4201 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02004202 }
4203
David Brownelld2876d02008-02-04 22:28:20 -08004204 gpiod_get_direction(gdesc);
4205 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
4206 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
4207 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
4208 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
4209 is_out ? "out" : "in ",
4210 chip->get
4211 ? (chip->get(chip, i) ? "hi" : "lo")
4212 : "? ",
4213 is_irq ? "IRQ" : " ");
4214 seq_printf(s, "\n");
4215 }
Linus Walleijff2b1352015-10-20 11:10:38 +02004216}
David Brownelld2876d02008-02-04 22:28:20 -08004217
4218static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
4219{
4220 unsigned long flags;
4221 struct gpio_device *gdev = NULL;
Linus Walleijff2b1352015-10-20 11:10:38 +02004222 loff_t index = *pos;
David Brownelld2876d02008-02-04 22:28:20 -08004223
4224 s->private = "";
Linus Walleijff2b1352015-10-20 11:10:38 +02004225
David Brownelld2876d02008-02-04 22:28:20 -08004226 spin_lock_irqsave(&gpio_lock, flags);
4227 list_for_each_entry(gdev, &gpio_devices, list)
4228 if (index-- == 0) {
4229 spin_unlock_irqrestore(&gpio_lock, flags);
4230 return gdev;
4231 }
4232 spin_unlock_irqrestore(&gpio_lock, flags);
4233
4234 return NULL;
Linus Walleijff2b1352015-10-20 11:10:38 +02004235}
David Brownelld2876d02008-02-04 22:28:20 -08004236
4237static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
4238{
Linus Walleijff2b1352015-10-20 11:10:38 +02004239 unsigned long flags;
David Brownelld2876d02008-02-04 22:28:20 -08004240 struct gpio_device *gdev = v;
4241 void *ret = NULL;
Linus Walleijff2b1352015-10-20 11:10:38 +02004242
David Brownelld2876d02008-02-04 22:28:20 -08004243 spin_lock_irqsave(&gpio_lock, flags);
4244 if (list_is_last(&gdev->list, &gpio_devices))
4245 ret = NULL;
4246 else
4247 ret = list_entry(gdev->list.next, struct gpio_device, list);
4248 spin_unlock_irqrestore(&gpio_lock, flags);
4249
4250 s->private = "\n";
4251 ++*pos;
4252
4253 return ret;
4254}
4255
4256static void gpiolib_seq_stop(struct seq_file *s, void *v)
Linus Walleijff2b1352015-10-20 11:10:38 +02004257{
4258}
4259
David Brownelld2876d02008-02-04 22:28:20 -08004260static int gpiolib_seq_show(struct seq_file *s, void *v)
Linus Walleijff2b1352015-10-20 11:10:38 +02004261{
4262 struct gpio_device *gdev = v;
4263 struct gpio_chip *chip = gdev->chip;
4264 struct device *parent;
4265
4266 if (!chip) {
4267 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
4268 dev_name(&gdev->dev));
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004269 return 0;
Linus Walleijff2b1352015-10-20 11:10:38 +02004270 }
4271
4272 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
4273 dev_name(&gdev->dev),
4274 gdev->base, gdev->base + gdev->ngpio - 1);
David Brownelld2876d02008-02-04 22:28:20 -08004275 parent = chip->parent;
4276 if (parent)
4277 seq_printf(s, ", parent: %s/%s",
4278 parent->bus ? parent->bus->name : "no-bus",
4279 dev_name(parent));
4280 if (chip->label)
4281 seq_printf(s, ", %s", chip->label);
4282 if (chip->can_sleep)
4283 seq_printf(s, ", can sleep");
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004284 seq_printf(s, ":\n");
David Brownelld2876d02008-02-04 22:28:20 -08004285
4286 if (chip->dbg_show)
4287 chip->dbg_show(s, chip);
4288 else
4289 gpiolib_dbg_show(s, gdev);
4290
4291 return 0;
4292}
4293
4294static const struct seq_operations gpiolib_seq_ops = {
4295 .start = gpiolib_seq_start,
4296 .next = gpiolib_seq_next,
4297 .stop = gpiolib_seq_stop,
4298 .show = gpiolib_seq_show,
4299};
4300
4301static int gpiolib_open(struct inode *inode, struct file *file)
4302{
4303 return seq_open(file, &gpiolib_seq_ops);
4304}
4305
4306static const struct file_operations gpiolib_operations = {
4307 .owner = THIS_MODULE,
4308 .open = gpiolib_open,
4309 .read = seq_read,
4310 .llseek = seq_lseek,
4311 .release = seq_release,
4312};
4313
4314static int __init gpiolib_debugfs_init(void)
4315{
4316 /* /sys/kernel/debug/gpio */
4317 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
4318 NULL, NULL, &gpiolib_operations);
4319 return 0;
4320}
4321subsys_initcall(gpiolib_debugfs_init);
4322
4323#endif /* DEBUG_FS */