blob: db3788d17ba0e91ceb28cfa947df7fa18c470191 [file] [log] [blame]
Andy Shevchenko923a6542017-05-25 16:08:38 +03001#include <linux/bitmap.h>
David Brownelld2876d02008-02-04 22:28:20 -08002#include <linux/kernel.h>
3#include <linux/module.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -07004#include <linux/interrupt.h>
David Brownelld2876d02008-02-04 22:28:20 -08005#include <linux/irq.h>
6#include <linux/spinlock.h>
Alexandre Courbot1a989d02013-02-03 01:29:24 +09007#include <linux/list.h>
David Brownelld8f388d82008-07-25 01:46:07 -07008#include <linux/device.h>
9#include <linux/err.h>
10#include <linux/debugfs.h>
11#include <linux/seq_file.h>
12#include <linux/gpio.h>
Anton Vorontsov391c9702010-06-08 07:48:17 -060013#include <linux/of_gpio.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -070014#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Rafael J. Wysocki7b199812013-11-11 22:41:56 +010016#include <linux/acpi.h>
Alexandre Courbot53e7cac2013-11-16 21:44:52 +090017#include <linux/gpio/driver.h>
Linus Walleij0a6d3152014-07-24 20:08:55 +020018#include <linux/gpio/machine.h>
Jonas Gorskic771c2f2015-10-11 17:34:15 +020019#include <linux/pinctrl/consumer.h>
Linus Walleij3c702e92015-10-21 15:29:53 +020020#include <linux/cdev.h>
21#include <linux/fs.h>
22#include <linux/uaccess.h>
Linus Walleij8b92e172016-05-27 14:24:04 +020023#include <linux/compat.h>
Linus Walleijd7c51b42016-04-26 10:35:29 +020024#include <linux/anon_inodes.h>
Lars-Peter Clausen953b9562016-10-24 13:59:15 +020025#include <linux/file.h>
Linus Walleij61f922d2016-06-02 11:30:15 +020026#include <linux/kfifo.h>
27#include <linux/poll.h>
28#include <linux/timekeeping.h>
Linus Walleij3c702e92015-10-21 15:29:53 +020029#include <uapi/linux/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080030
Mika Westerberg664e3e52014-01-08 12:40:54 +020031#include "gpiolib.h"
32
Uwe Kleine-König3f397c212011-05-20 00:40:19 -060033#define CREATE_TRACE_POINTS
34#include <trace/events/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080035
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070036/* Implementation infrastructure for GPIO interfaces.
David Brownelld2876d02008-02-04 22:28:20 -080037 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070038 * The GPIO programming interface allows for inlining speed-critical
39 * get/set operations for common cases, so that access to SOC-integrated
40 * GPIOs can sometimes cost only an instruction or two per bit.
David Brownelld2876d02008-02-04 22:28:20 -080041 */
42
43
44/* When debugging, extend minimal trust to callers and platform code.
45 * Also emit diagnostic messages that may help initial bringup, when
46 * board setup or driver bugs are most common.
47 *
48 * Otherwise, minimize overhead in what may be bitbanging codepaths.
49 */
50#ifdef DEBUG
51#define extra_checks 1
52#else
53#define extra_checks 0
54#endif
55
Linus Walleijff2b1352015-10-20 11:10:38 +020056/* Device and char device-related information */
57static DEFINE_IDA(gpio_ida);
Linus Walleij3c702e92015-10-21 15:29:53 +020058static dev_t gpio_devt;
59#define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
60static struct bus_type gpio_bus_type = {
61 .name = "gpio",
62};
Linus Walleijff2b1352015-10-20 11:10:38 +020063
David Brownelld2876d02008-02-04 22:28:20 -080064/* gpio_lock prevents conflicts during gpio_desc[] table updates.
65 * While any GPIO is requested, its gpio_chip is not removable;
66 * each GPIO's "requested" flag serves as a lock and refcount.
67 */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090068DEFINE_SPINLOCK(gpio_lock);
David Brownelld2876d02008-02-04 22:28:20 -080069
Alexandre Courbotbae48da2013-10-17 10:21:38 -070070static DEFINE_MUTEX(gpio_lookup_lock);
71static LIST_HEAD(gpio_lookup_list);
Linus Walleijff2b1352015-10-20 11:10:38 +020072LIST_HEAD(gpio_devices);
Johan Hovold6d867502015-05-04 17:23:25 +020073
74static void gpiochip_free_hogs(struct gpio_chip *chip);
Thierry Reding959bc7b2017-11-07 19:15:59 +010075static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
Andrew Lunn39c3fd52017-12-02 18:11:04 +010076 struct lock_class_key *lock_key,
77 struct lock_class_key *request_key);
Johan Hovold6d867502015-05-04 17:23:25 +020078static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
Mika Westerberg79b804c2016-09-20 15:15:21 +030079static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip);
80static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip);
Johan Hovold6d867502015-05-04 17:23:25 +020081
Guenter Roeck159f3cd2016-03-31 08:11:30 -070082static bool gpiolib_initialized;
Johan Hovold6d867502015-05-04 17:23:25 +020083
David Brownelld2876d02008-02-04 22:28:20 -080084static inline void desc_set_label(struct gpio_desc *d, const char *label)
85{
David Brownelld2876d02008-02-04 22:28:20 -080086 d->label = label;
David Brownelld2876d02008-02-04 22:28:20 -080087}
88
Alexandre Courbot372e7222013-02-03 01:29:29 +090089/**
Thierry Reding950d55f52017-07-24 16:57:22 +020090 * gpio_to_desc - Convert a GPIO number to its descriptor
91 * @gpio: global GPIO number
92 *
93 * Returns:
94 * The GPIO descriptor associated with the given GPIO, or %NULL if no GPIO
95 * with the given number exists in the system.
Alexandre Courbot372e7222013-02-03 01:29:29 +090096 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070097struct gpio_desc *gpio_to_desc(unsigned gpio)
Alexandre Courbot372e7222013-02-03 01:29:29 +090098{
Linus Walleijff2b1352015-10-20 11:10:38 +020099 struct gpio_device *gdev;
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900100 unsigned long flags;
101
102 spin_lock_irqsave(&gpio_lock, flags);
103
Linus Walleijff2b1352015-10-20 11:10:38 +0200104 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100105 if (gdev->base <= gpio &&
106 gdev->base + gdev->ngpio > gpio) {
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900107 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100108 return &gdev->descs[gpio - gdev->base];
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900109 }
110 }
111
112 spin_unlock_irqrestore(&gpio_lock, flags);
113
Alexandre Courbot0e9a5ed2014-12-02 23:15:05 +0900114 if (!gpio_is_valid(gpio))
115 WARN(1, "invalid GPIO %d\n", gpio);
116
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900117 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900118}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700119EXPORT_SYMBOL_GPL(gpio_to_desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900120
121/**
Thierry Reding950d55f52017-07-24 16:57:22 +0200122 * gpiochip_get_desc - get the GPIO descriptor corresponding to the given
123 * hardware number for this chip
124 * @chip: GPIO chip
125 * @hwnum: hardware number of the GPIO for this chip
126 *
127 * Returns:
128 * A pointer to the GPIO descriptor or %ERR_PTR(-EINVAL) if no GPIO exists
129 * in the given chip for the specified hardware number.
Linus Walleijd468bf92013-09-24 11:54:38 +0200130 */
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900131struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
132 u16 hwnum)
Linus Walleijd468bf92013-09-24 11:54:38 +0200133{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100134 struct gpio_device *gdev = chip->gpiodev;
135
136 if (hwnum >= gdev->ngpio)
Alexandre Courbotb7d0a282013-12-03 12:31:11 +0900137 return ERR_PTR(-EINVAL);
Linus Walleijd468bf92013-09-24 11:54:38 +0200138
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100139 return &gdev->descs[hwnum];
Linus Walleijd468bf92013-09-24 11:54:38 +0200140}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900141
142/**
Thierry Reding950d55f52017-07-24 16:57:22 +0200143 * desc_to_gpio - convert a GPIO descriptor to the integer namespace
144 * @desc: GPIO descriptor
145 *
Alexandre Courbot372e7222013-02-03 01:29:29 +0900146 * This should disappear in the future but is needed since we still
Thierry Reding950d55f52017-07-24 16:57:22 +0200147 * use GPIO numbers for error messages and sysfs nodes.
148 *
149 * Returns:
150 * The global GPIO number for the GPIO specified by its descriptor.
Alexandre Courbot372e7222013-02-03 01:29:29 +0900151 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700152int desc_to_gpio(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900153{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100154 return desc->gdev->base + (desc - &desc->gdev->descs[0]);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900155}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700156EXPORT_SYMBOL_GPL(desc_to_gpio);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900157
158
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700159/**
160 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
161 * @desc: descriptor to return the chip of
162 */
163struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900164{
Vladimir Zapolskiydd3b9a42017-12-21 18:37:30 +0200165 if (!desc || !desc->gdev)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100166 return NULL;
167 return desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900168}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700169EXPORT_SYMBOL_GPL(gpiod_to_chip);
David Brownelld2876d02008-02-04 22:28:20 -0800170
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700171/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
172static int gpiochip_find_base(int ngpio)
173{
Linus Walleijff2b1352015-10-20 11:10:38 +0200174 struct gpio_device *gdev;
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900175 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700176
Linus Walleijff2b1352015-10-20 11:10:38 +0200177 list_for_each_entry_reverse(gdev, &gpio_devices, list) {
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900178 /* found a free space? */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100179 if (gdev->base + gdev->ngpio <= base)
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900180 break;
181 else
182 /* nope, check the space right before the chip */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100183 base = gdev->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700184 }
185
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900186 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700187 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900188 return base;
189 } else {
190 pr_err("%s: cannot find free range\n", __func__);
191 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700192 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700193}
194
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700195/**
196 * gpiod_get_direction - return the current direction of a GPIO
197 * @desc: GPIO to get the direction of
198 *
Wolfram Sang94fc7302018-01-09 12:35:53 +0100199 * Returns 0 for output, 1 for input, or an error code in case of error.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700200 *
201 * This function may sleep if gpiod_cansleep() is true.
202 */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900203int gpiod_get_direction(struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300204{
205 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900206 unsigned offset;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300207 int status = -EINVAL;
208
Alexandre Courbot372e7222013-02-03 01:29:29 +0900209 chip = gpiod_to_chip(desc);
210 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300211
212 if (!chip->get_direction)
213 return status;
214
Alexandre Courbot372e7222013-02-03 01:29:29 +0900215 status = chip->get_direction(chip, offset);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300216 if (status > 0) {
217 /* GPIOF_DIR_IN, or other positive */
218 status = 1;
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900219 clear_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300220 }
221 if (status == 0) {
222 /* GPIOF_DIR_OUT */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900223 set_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300224 }
225 return status;
226}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700227EXPORT_SYMBOL_GPL(gpiod_get_direction);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300228
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900229/*
230 * Add a new chip to the global chips list, keeping the list of chips sorted
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800231 * by range(means [base, base + ngpio - 1]) order.
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900232 *
233 * Return -EBUSY if the new chip overlaps with some other chip's integer
234 * space.
235 */
Linus Walleijff2b1352015-10-20 11:10:38 +0200236static int gpiodev_add_to_list(struct gpio_device *gdev)
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900237{
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800238 struct gpio_device *prev, *next;
Linus Walleijff2b1352015-10-20 11:10:38 +0200239
240 if (list_empty(&gpio_devices)) {
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800241 /* initial entry in list */
Linus Walleijff2b1352015-10-20 11:10:38 +0200242 list_add_tail(&gdev->list, &gpio_devices);
Sudip Mukherjeee28ecca2015-12-27 19:06:50 +0530243 return 0;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900244 }
245
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800246 next = list_entry(gpio_devices.next, struct gpio_device, list);
247 if (gdev->base + gdev->ngpio <= next->base) {
248 /* add before first entry */
249 list_add(&gdev->list, &gpio_devices);
Julien Grossholtz96098df2016-01-07 16:46:45 -0500250 return 0;
251 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900252
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800253 prev = list_entry(gpio_devices.prev, struct gpio_device, list);
254 if (prev->base + prev->ngpio <= gdev->base) {
255 /* add behind last entry */
256 list_add_tail(&gdev->list, &gpio_devices);
257 return 0;
258 }
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800259
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800260 list_for_each_entry_safe(prev, next, &gpio_devices, list) {
261 /* at the end of the list */
262 if (&next->list == &gpio_devices)
263 break;
264
265 /* add between prev and next */
266 if (prev->base + prev->ngpio <= gdev->base
267 && gdev->base + gdev->ngpio <= next->base) {
268 list_add(&gdev->list, &prev->list);
269 return 0;
270 }
271 }
272
273 dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n");
274 return -EBUSY;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900275}
276
Thierry Reding950d55f52017-07-24 16:57:22 +0200277/*
Linus Walleijf881bab2015-09-23 16:20:43 -0700278 * Convert a GPIO name to its descriptor
279 */
280static struct gpio_desc *gpio_name_to_desc(const char * const name)
281{
Linus Walleijff2b1352015-10-20 11:10:38 +0200282 struct gpio_device *gdev;
Linus Walleijf881bab2015-09-23 16:20:43 -0700283 unsigned long flags;
284
285 spin_lock_irqsave(&gpio_lock, flags);
286
Linus Walleijff2b1352015-10-20 11:10:38 +0200287 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijf881bab2015-09-23 16:20:43 -0700288 int i;
289
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100290 for (i = 0; i != gdev->ngpio; ++i) {
291 struct gpio_desc *desc = &gdev->descs[i];
Linus Walleijf881bab2015-09-23 16:20:43 -0700292
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100293 if (!desc->name || !name)
Linus Walleijf881bab2015-09-23 16:20:43 -0700294 continue;
295
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100296 if (!strcmp(desc->name, name)) {
Linus Walleijf881bab2015-09-23 16:20:43 -0700297 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100298 return desc;
Linus Walleijf881bab2015-09-23 16:20:43 -0700299 }
300 }
301 }
302
303 spin_unlock_irqrestore(&gpio_lock, flags);
304
305 return NULL;
306}
307
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200308/*
309 * Takes the names from gc->names and checks if they are all unique. If they
310 * are, they are assigned to their gpio descriptors.
311 *
Bamvor Jian Zhanged379152015-11-14 16:43:20 +0800312 * Warning if one of the names is already used for a different GPIO.
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200313 */
314static int gpiochip_set_desc_names(struct gpio_chip *gc)
315{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100316 struct gpio_device *gdev = gc->gpiodev;
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200317 int i;
318
319 if (!gc->names)
320 return 0;
321
322 /* First check all names if they are unique */
323 for (i = 0; i != gc->ngpio; ++i) {
324 struct gpio_desc *gpio;
325
326 gpio = gpio_name_to_desc(gc->names[i]);
Linus Walleijf881bab2015-09-23 16:20:43 -0700327 if (gpio)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100328 dev_warn(&gdev->dev,
Linus Walleij34ffd852015-10-20 11:31:54 +0200329 "Detected name collision for GPIO name '%s'\n",
Linus Walleijf881bab2015-09-23 16:20:43 -0700330 gc->names[i]);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200331 }
332
333 /* Then add all names to the GPIO descriptors */
334 for (i = 0; i != gc->ngpio; ++i)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100335 gdev->descs[i].name = gc->names[i];
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200336
337 return 0;
338}
339
Stephen Boyde4371f6e2018-03-23 09:34:50 -0700340static unsigned long *gpiochip_allocate_mask(struct gpio_chip *chip)
341{
342 unsigned long *p;
343
Stephen Boydace569352018-03-23 09:34:51 -0700344 p = kmalloc_array(BITS_TO_LONGS(chip->ngpio), sizeof(*p), GFP_KERNEL);
Stephen Boyde4371f6e2018-03-23 09:34:50 -0700345 if (!p)
346 return NULL;
347
348 /* Assume by default all GPIOs are valid */
349 bitmap_fill(p, chip->ngpio);
350
351 return p;
352}
353
Linus Walleijd7c51b42016-04-26 10:35:29 +0200354/*
355 * GPIO line handle management
356 */
357
358/**
359 * struct linehandle_state - contains the state of a userspace handle
360 * @gdev: the GPIO device the handle pertains to
361 * @label: consumer label used to tag descriptors
362 * @descs: the GPIO descriptors held by this handle
363 * @numdescs: the number of descriptors held in the descs array
364 */
365struct linehandle_state {
366 struct gpio_device *gdev;
367 const char *label;
368 struct gpio_desc *descs[GPIOHANDLES_MAX];
369 u32 numdescs;
370};
371
Lars-Peter Clausene3e847c2016-10-18 16:54:05 +0200372#define GPIOHANDLE_REQUEST_VALID_FLAGS \
373 (GPIOHANDLE_REQUEST_INPUT | \
374 GPIOHANDLE_REQUEST_OUTPUT | \
375 GPIOHANDLE_REQUEST_ACTIVE_LOW | \
376 GPIOHANDLE_REQUEST_OPEN_DRAIN | \
377 GPIOHANDLE_REQUEST_OPEN_SOURCE)
378
Linus Walleijd7c51b42016-04-26 10:35:29 +0200379static long linehandle_ioctl(struct file *filep, unsigned int cmd,
380 unsigned long arg)
381{
382 struct linehandle_state *lh = filep->private_data;
383 void __user *ip = (void __user *)arg;
384 struct gpiohandle_data ghd;
Lukas Wunnereec1d562017-10-12 12:40:10 +0200385 int vals[GPIOHANDLES_MAX];
Linus Walleijd7c51b42016-04-26 10:35:29 +0200386 int i;
387
388 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
Lukas Wunnereec1d562017-10-12 12:40:10 +0200389 /* TODO: check if descriptors are really input */
390 int ret = gpiod_get_array_value_complex(false,
391 true,
392 lh->numdescs,
393 lh->descs,
394 vals);
395 if (ret)
396 return ret;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200397
Lars-Peter Clausen3eded5d2016-10-18 16:54:02 +0200398 memset(&ghd, 0, sizeof(ghd));
Lukas Wunnereec1d562017-10-12 12:40:10 +0200399 for (i = 0; i < lh->numdescs; i++)
400 ghd.values[i] = vals[i];
Linus Walleijd7c51b42016-04-26 10:35:29 +0200401
402 if (copy_to_user(ip, &ghd, sizeof(ghd)))
403 return -EFAULT;
404
405 return 0;
406 } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
Linus Walleijd7c51b42016-04-26 10:35:29 +0200407 /* TODO: check if descriptors are really output */
408 if (copy_from_user(&ghd, ip, sizeof(ghd)))
409 return -EFAULT;
410
411 /* Clamp all values to [0,1] */
412 for (i = 0; i < lh->numdescs; i++)
413 vals[i] = !!ghd.values[i];
414
415 /* Reuse the array setting function */
416 gpiod_set_array_value_complex(false,
417 true,
418 lh->numdescs,
419 lh->descs,
420 vals);
421 return 0;
422 }
423 return -EINVAL;
424}
425
426#ifdef CONFIG_COMPAT
427static long linehandle_ioctl_compat(struct file *filep, unsigned int cmd,
428 unsigned long arg)
429{
430 return linehandle_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
431}
432#endif
433
434static int linehandle_release(struct inode *inode, struct file *filep)
435{
436 struct linehandle_state *lh = filep->private_data;
437 struct gpio_device *gdev = lh->gdev;
438 int i;
439
440 for (i = 0; i < lh->numdescs; i++)
441 gpiod_free(lh->descs[i]);
442 kfree(lh->label);
443 kfree(lh);
444 put_device(&gdev->dev);
445 return 0;
446}
447
448static const struct file_operations linehandle_fileops = {
449 .release = linehandle_release,
450 .owner = THIS_MODULE,
451 .llseek = noop_llseek,
452 .unlocked_ioctl = linehandle_ioctl,
453#ifdef CONFIG_COMPAT
454 .compat_ioctl = linehandle_ioctl_compat,
455#endif
456};
457
458static int linehandle_create(struct gpio_device *gdev, void __user *ip)
459{
460 struct gpiohandle_request handlereq;
461 struct linehandle_state *lh;
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200462 struct file *file;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200463 int fd, i, ret;
Bartosz Golaszewski418ee8e2017-10-16 11:32:29 +0200464 u32 lflags;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200465
466 if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
467 return -EFAULT;
468 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
469 return -EINVAL;
470
Bartosz Golaszewski418ee8e2017-10-16 11:32:29 +0200471 lflags = handlereq.flags;
472
473 /* Return an error if an unknown flag is set */
474 if (lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
475 return -EINVAL;
476
Bartosz Golaszewski588fc3b2017-11-15 16:47:43 +0100477 /*
478 * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
479 * the hardware actually supports enabling both at the same time the
480 * electrical result would be disastrous.
481 */
482 if ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) &&
483 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
484 return -EINVAL;
485
Bartosz Golaszewski609aaf62017-10-16 11:32:30 +0200486 /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
487 if (!(lflags & GPIOHANDLE_REQUEST_OUTPUT) &&
488 ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
489 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
490 return -EINVAL;
491
Linus Walleijd7c51b42016-04-26 10:35:29 +0200492 lh = kzalloc(sizeof(*lh), GFP_KERNEL);
493 if (!lh)
494 return -ENOMEM;
495 lh->gdev = gdev;
496 get_device(&gdev->dev);
497
498 /* Make sure this is terminated */
499 handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
500 if (strlen(handlereq.consumer_label)) {
501 lh->label = kstrdup(handlereq.consumer_label,
502 GFP_KERNEL);
503 if (!lh->label) {
504 ret = -ENOMEM;
505 goto out_free_lh;
506 }
507 }
508
509 /* Request each GPIO */
510 for (i = 0; i < handlereq.lines; i++) {
511 u32 offset = handlereq.lineoffsets[i];
Linus Walleijd7c51b42016-04-26 10:35:29 +0200512 struct gpio_desc *desc;
513
Lars-Peter Clausene405f9f2016-10-18 16:54:01 +0200514 if (offset >= gdev->ngpio) {
515 ret = -EINVAL;
516 goto out_free_descs;
517 }
518
Linus Walleijd7c51b42016-04-26 10:35:29 +0200519 desc = &gdev->descs[offset];
520 ret = gpiod_request(desc, lh->label);
521 if (ret)
522 goto out_free_descs;
523 lh->descs[i] = desc;
524
525 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
526 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
527 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
528 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
529 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
530 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
531
Andrew Jefferye10f72b2017-11-30 14:25:24 +1030532 ret = gpiod_set_transitory(desc, false);
533 if (ret < 0)
534 goto out_free_descs;
535
Linus Walleijd7c51b42016-04-26 10:35:29 +0200536 /*
537 * Lines have to be requested explicitly for input
538 * or output, else the line will be treated "as is".
539 */
540 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
541 int val = !!handlereq.default_values[i];
542
543 ret = gpiod_direction_output(desc, val);
544 if (ret)
545 goto out_free_descs;
546 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
547 ret = gpiod_direction_input(desc);
548 if (ret)
549 goto out_free_descs;
550 }
551 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
552 offset);
553 }
Linus Walleije2f608b2016-06-18 10:56:43 +0200554 /* Let i point at the last handle */
555 i--;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200556 lh->numdescs = handlereq.lines;
557
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200558 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200559 if (fd < 0) {
560 ret = fd;
561 goto out_free_descs;
562 }
563
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200564 file = anon_inode_getfile("gpio-linehandle",
565 &linehandle_fileops,
566 lh,
567 O_RDONLY | O_CLOEXEC);
568 if (IS_ERR(file)) {
569 ret = PTR_ERR(file);
570 goto out_put_unused_fd;
571 }
572
Linus Walleijd7c51b42016-04-26 10:35:29 +0200573 handlereq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200574 if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200575 /*
576 * fput() will trigger the release() callback, so do not go onto
577 * the regular error cleanup path here.
578 */
579 fput(file);
580 put_unused_fd(fd);
581 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +0200582 }
Linus Walleijd7c51b42016-04-26 10:35:29 +0200583
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200584 fd_install(fd, file);
585
Linus Walleijd7c51b42016-04-26 10:35:29 +0200586 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
587 lh->numdescs);
588
589 return 0;
590
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200591out_put_unused_fd:
592 put_unused_fd(fd);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200593out_free_descs:
594 for (; i >= 0; i--)
595 gpiod_free(lh->descs[i]);
596 kfree(lh->label);
597out_free_lh:
598 kfree(lh);
599 put_device(&gdev->dev);
600 return ret;
601}
602
Linus Walleij61f922d2016-06-02 11:30:15 +0200603/*
604 * GPIO line event management
605 */
606
607/**
608 * struct lineevent_state - contains the state of a userspace event
609 * @gdev: the GPIO device the event pertains to
610 * @label: consumer label used to tag descriptors
611 * @desc: the GPIO descriptor held by this event
612 * @eflags: the event flags this line was requested with
613 * @irq: the interrupt that trigger in response to events on this GPIO
614 * @wait: wait queue that handles blocking reads of events
615 * @events: KFIFO for the GPIO events
616 * @read_lock: mutex lock to protect reads from colliding with adding
617 * new events to the FIFO
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100618 * @timestamp: cache for the timestamp storing it between hardirq
619 * and IRQ thread, used to bring the timestamp close to the actual
620 * event
Linus Walleij61f922d2016-06-02 11:30:15 +0200621 */
622struct lineevent_state {
623 struct gpio_device *gdev;
624 const char *label;
625 struct gpio_desc *desc;
626 u32 eflags;
627 int irq;
628 wait_queue_head_t wait;
629 DECLARE_KFIFO(events, struct gpioevent_data, 16);
630 struct mutex read_lock;
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100631 u64 timestamp;
Linus Walleij61f922d2016-06-02 11:30:15 +0200632};
633
Lars-Peter Clausenac7dbb92016-10-18 16:54:06 +0200634#define GPIOEVENT_REQUEST_VALID_FLAGS \
635 (GPIOEVENT_REQUEST_RISING_EDGE | \
636 GPIOEVENT_REQUEST_FALLING_EDGE)
637
Al Viroafc9a422017-07-03 06:39:46 -0400638static __poll_t lineevent_poll(struct file *filep,
Linus Walleij61f922d2016-06-02 11:30:15 +0200639 struct poll_table_struct *wait)
640{
641 struct lineevent_state *le = filep->private_data;
Al Viroafc9a422017-07-03 06:39:46 -0400642 __poll_t events = 0;
Linus Walleij61f922d2016-06-02 11:30:15 +0200643
644 poll_wait(filep, &le->wait, wait);
645
646 if (!kfifo_is_empty(&le->events))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800647 events = EPOLLIN | EPOLLRDNORM;
Linus Walleij61f922d2016-06-02 11:30:15 +0200648
649 return events;
650}
651
652
653static ssize_t lineevent_read(struct file *filep,
654 char __user *buf,
655 size_t count,
656 loff_t *f_ps)
657{
658 struct lineevent_state *le = filep->private_data;
659 unsigned int copied;
660 int ret;
661
662 if (count < sizeof(struct gpioevent_data))
663 return -EINVAL;
664
665 do {
666 if (kfifo_is_empty(&le->events)) {
667 if (filep->f_flags & O_NONBLOCK)
668 return -EAGAIN;
669
670 ret = wait_event_interruptible(le->wait,
671 !kfifo_is_empty(&le->events));
672 if (ret)
673 return ret;
674 }
675
676 if (mutex_lock_interruptible(&le->read_lock))
677 return -ERESTARTSYS;
678 ret = kfifo_to_user(&le->events, buf, count, &copied);
679 mutex_unlock(&le->read_lock);
680
681 if (ret)
682 return ret;
683
684 /*
685 * If we couldn't read anything from the fifo (a different
686 * thread might have been faster) we either return -EAGAIN if
687 * the file descriptor is non-blocking, otherwise we go back to
688 * sleep and wait for more data to arrive.
689 */
690 if (copied == 0 && (filep->f_flags & O_NONBLOCK))
691 return -EAGAIN;
692
693 } while (copied == 0);
694
695 return copied;
696}
697
698static int lineevent_release(struct inode *inode, struct file *filep)
699{
700 struct lineevent_state *le = filep->private_data;
701 struct gpio_device *gdev = le->gdev;
702
703 free_irq(le->irq, le);
704 gpiod_free(le->desc);
705 kfree(le->label);
706 kfree(le);
707 put_device(&gdev->dev);
708 return 0;
709}
710
711static long lineevent_ioctl(struct file *filep, unsigned int cmd,
712 unsigned long arg)
713{
714 struct lineevent_state *le = filep->private_data;
715 void __user *ip = (void __user *)arg;
716 struct gpiohandle_data ghd;
717
718 /*
719 * We can get the value for an event line but not set it,
720 * because it is input by definition.
721 */
722 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
723 int val;
724
Lars-Peter Clausend82aa4a2016-10-18 16:54:04 +0200725 memset(&ghd, 0, sizeof(ghd));
726
Linus Walleij61f922d2016-06-02 11:30:15 +0200727 val = gpiod_get_value_cansleep(le->desc);
728 if (val < 0)
729 return val;
730 ghd.values[0] = val;
731
732 if (copy_to_user(ip, &ghd, sizeof(ghd)))
733 return -EFAULT;
734
735 return 0;
736 }
737 return -EINVAL;
738}
739
740#ifdef CONFIG_COMPAT
741static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd,
742 unsigned long arg)
743{
744 return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
745}
746#endif
747
748static const struct file_operations lineevent_fileops = {
749 .release = lineevent_release,
750 .read = lineevent_read,
751 .poll = lineevent_poll,
752 .owner = THIS_MODULE,
753 .llseek = noop_llseek,
754 .unlocked_ioctl = lineevent_ioctl,
755#ifdef CONFIG_COMPAT
756 .compat_ioctl = lineevent_ioctl_compat,
757#endif
758};
759
Ben Dooks33265b12016-06-17 16:03:13 +0100760static irqreturn_t lineevent_irq_thread(int irq, void *p)
Linus Walleij61f922d2016-06-02 11:30:15 +0200761{
762 struct lineevent_state *le = p;
763 struct gpioevent_data ge;
Bartosz Golaszewskidf1e76f2017-07-03 11:12:03 +0200764 int ret, level;
Linus Walleij61f922d2016-06-02 11:30:15 +0200765
Linus Walleij24bd3ef2018-01-22 13:19:28 +0100766 /* Do not leak kernel stack to userspace */
767 memset(&ge, 0, sizeof(ge));
768
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100769 ge.timestamp = le->timestamp;
Bartosz Golaszewskidf1e76f2017-07-03 11:12:03 +0200770 level = gpiod_get_value_cansleep(le->desc);
Linus Walleij61f922d2016-06-02 11:30:15 +0200771
Bartosz Golaszewskiad537b82017-06-23 13:45:16 +0200772 if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
773 && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200774 if (level)
775 /* Emit low-to-high event */
776 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
777 else
778 /* Emit high-to-low event */
779 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Bartosz Golaszewskidf1e76f2017-07-03 11:12:03 +0200780 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE && level) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200781 /* Emit low-to-high event */
782 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
Bartosz Golaszewskidf1e76f2017-07-03 11:12:03 +0200783 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE && !level) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200784 /* Emit high-to-low event */
785 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Arnd Bergmannbc0207a2016-06-16 11:02:41 +0200786 } else {
787 return IRQ_NONE;
Linus Walleij61f922d2016-06-02 11:30:15 +0200788 }
789
790 ret = kfifo_put(&le->events, ge);
791 if (ret != 0)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800792 wake_up_poll(&le->wait, EPOLLIN);
Linus Walleij61f922d2016-06-02 11:30:15 +0200793
794 return IRQ_HANDLED;
795}
796
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100797static irqreturn_t lineevent_irq_handler(int irq, void *p)
798{
799 struct lineevent_state *le = p;
800
801 /*
802 * Just store the timestamp in hardirq context so we get it as
803 * close in time as possible to the actual event.
804 */
805 le->timestamp = ktime_get_real_ns();
806
807 return IRQ_WAKE_THREAD;
808}
809
Linus Walleij61f922d2016-06-02 11:30:15 +0200810static int lineevent_create(struct gpio_device *gdev, void __user *ip)
811{
812 struct gpioevent_request eventreq;
813 struct lineevent_state *le;
814 struct gpio_desc *desc;
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200815 struct file *file;
Linus Walleij61f922d2016-06-02 11:30:15 +0200816 u32 offset;
817 u32 lflags;
818 u32 eflags;
819 int fd;
820 int ret;
821 int irqflags = 0;
822
823 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
824 return -EFAULT;
825
826 le = kzalloc(sizeof(*le), GFP_KERNEL);
827 if (!le)
828 return -ENOMEM;
829 le->gdev = gdev;
830 get_device(&gdev->dev);
831
832 /* Make sure this is terminated */
833 eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
834 if (strlen(eventreq.consumer_label)) {
835 le->label = kstrdup(eventreq.consumer_label,
836 GFP_KERNEL);
837 if (!le->label) {
838 ret = -ENOMEM;
839 goto out_free_le;
840 }
841 }
842
843 offset = eventreq.lineoffset;
844 lflags = eventreq.handleflags;
845 eflags = eventreq.eventflags;
846
Lars-Peter Clausenb8b0e3d2016-10-18 16:54:03 +0200847 if (offset >= gdev->ngpio) {
848 ret = -EINVAL;
849 goto out_free_label;
850 }
851
Lars-Peter Clausenac7dbb92016-10-18 16:54:06 +0200852 /* Return an error if a unknown flag is set */
853 if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
854 (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) {
855 ret = -EINVAL;
856 goto out_free_label;
857 }
858
Linus Walleij61f922d2016-06-02 11:30:15 +0200859 /* This is just wrong: we don't look for events on output lines */
860 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
861 ret = -EINVAL;
862 goto out_free_label;
863 }
864
865 desc = &gdev->descs[offset];
866 ret = gpiod_request(desc, le->label);
867 if (ret)
868 goto out_free_desc;
869 le->desc = desc;
870 le->eflags = eflags;
871
872 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
873 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
874 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
875 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
876 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
877 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
878
879 ret = gpiod_direction_input(desc);
880 if (ret)
881 goto out_free_desc;
882
883 le->irq = gpiod_to_irq(desc);
884 if (le->irq <= 0) {
885 ret = -ENODEV;
886 goto out_free_desc;
887 }
888
889 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
890 irqflags |= IRQF_TRIGGER_RISING;
891 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
892 irqflags |= IRQF_TRIGGER_FALLING;
893 irqflags |= IRQF_ONESHOT;
894 irqflags |= IRQF_SHARED;
895
896 INIT_KFIFO(le->events);
897 init_waitqueue_head(&le->wait);
898 mutex_init(&le->read_lock);
899
900 /* Request a thread to read the events */
901 ret = request_threaded_irq(le->irq,
Linus Walleijd58f2bf2017-11-30 10:23:27 +0100902 lineevent_irq_handler,
Linus Walleij61f922d2016-06-02 11:30:15 +0200903 lineevent_irq_thread,
904 irqflags,
905 le->label,
906 le);
907 if (ret)
908 goto out_free_desc;
909
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200910 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleij61f922d2016-06-02 11:30:15 +0200911 if (fd < 0) {
912 ret = fd;
913 goto out_free_irq;
914 }
915
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200916 file = anon_inode_getfile("gpio-event",
917 &lineevent_fileops,
918 le,
919 O_RDONLY | O_CLOEXEC);
920 if (IS_ERR(file)) {
921 ret = PTR_ERR(file);
922 goto out_put_unused_fd;
923 }
924
Linus Walleij61f922d2016-06-02 11:30:15 +0200925 eventreq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200926 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200927 /*
928 * fput() will trigger the release() callback, so do not go onto
929 * the regular error cleanup path here.
930 */
931 fput(file);
932 put_unused_fd(fd);
933 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +0200934 }
Linus Walleij61f922d2016-06-02 11:30:15 +0200935
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200936 fd_install(fd, file);
937
Linus Walleij61f922d2016-06-02 11:30:15 +0200938 return 0;
939
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200940out_put_unused_fd:
941 put_unused_fd(fd);
Linus Walleij61f922d2016-06-02 11:30:15 +0200942out_free_irq:
943 free_irq(le->irq, le);
944out_free_desc:
945 gpiod_free(le->desc);
946out_free_label:
947 kfree(le->label);
948out_free_le:
949 kfree(le);
950 put_device(&gdev->dev);
951 return ret;
952}
953
Thierry Reding950d55f52017-07-24 16:57:22 +0200954/*
Linus Walleij3c702e92015-10-21 15:29:53 +0200955 * gpio_ioctl() - ioctl handler for the GPIO chardev
956 */
957static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
958{
959 struct gpio_device *gdev = filp->private_data;
960 struct gpio_chip *chip = gdev->chip;
Linus Walleij8b92e172016-05-27 14:24:04 +0200961 void __user *ip = (void __user *)arg;
Linus Walleij3c702e92015-10-21 15:29:53 +0200962
963 /* We fail any subsequent ioctl():s when the chip is gone */
964 if (!chip)
965 return -ENODEV;
966
Linus Walleij521a2ad2016-02-12 22:25:22 +0100967 /* Fill in the struct and pass to userspace */
Linus Walleij3c702e92015-10-21 15:29:53 +0200968 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
Linus Walleij521a2ad2016-02-12 22:25:22 +0100969 struct gpiochip_info chipinfo;
970
Lars-Peter Clausen0f4bbb22016-10-18 16:54:00 +0200971 memset(&chipinfo, 0, sizeof(chipinfo));
972
Linus Walleij3c702e92015-10-21 15:29:53 +0200973 strncpy(chipinfo.name, dev_name(&gdev->dev),
974 sizeof(chipinfo.name));
975 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
Linus Walleijdf4878e2016-02-12 14:48:23 +0100976 strncpy(chipinfo.label, gdev->label,
977 sizeof(chipinfo.label));
978 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100979 chipinfo.lines = gdev->ngpio;
Linus Walleij3c702e92015-10-21 15:29:53 +0200980 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
981 return -EFAULT;
982 return 0;
Linus Walleij521a2ad2016-02-12 22:25:22 +0100983 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
984 struct gpioline_info lineinfo;
985 struct gpio_desc *desc;
986
987 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
988 return -EFAULT;
Lars-Peter Clausen1f1cc452016-10-18 16:53:59 +0200989 if (lineinfo.line_offset >= gdev->ngpio)
Linus Walleij521a2ad2016-02-12 22:25:22 +0100990 return -EINVAL;
991
992 desc = &gdev->descs[lineinfo.line_offset];
993 if (desc->name) {
994 strncpy(lineinfo.name, desc->name,
995 sizeof(lineinfo.name));
996 lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
997 } else {
998 lineinfo.name[0] = '\0';
999 }
1000 if (desc->label) {
Linus Walleij214338e2016-02-25 21:01:48 +01001001 strncpy(lineinfo.consumer, desc->label,
1002 sizeof(lineinfo.consumer));
1003 lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +01001004 } else {
Linus Walleij214338e2016-02-25 21:01:48 +01001005 lineinfo.consumer[0] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +01001006 }
1007
1008 /*
1009 * Userspace only need to know that the kernel is using
1010 * this GPIO so it can't use it.
1011 */
1012 lineinfo.flags = 0;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001013 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
1014 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
1015 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
1016 test_bit(FLAG_EXPORT, &desc->flags) ||
1017 test_bit(FLAG_SYSFS, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001018 lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001019 if (test_bit(FLAG_IS_OUT, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001020 lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001021 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001022 lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001023 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001024 lineinfo.flags |= GPIOLINE_FLAG_OPEN_DRAIN;
Linus Walleij9d8cc892016-02-22 13:44:53 +01001025 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +01001026 lineinfo.flags |= GPIOLINE_FLAG_OPEN_SOURCE;
1027
1028 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
1029 return -EFAULT;
1030 return 0;
Linus Walleijd7c51b42016-04-26 10:35:29 +02001031 } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
1032 return linehandle_create(gdev, ip);
Linus Walleij61f922d2016-06-02 11:30:15 +02001033 } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
1034 return lineevent_create(gdev, ip);
Linus Walleij3c702e92015-10-21 15:29:53 +02001035 }
1036 return -EINVAL;
1037}
1038
Linus Walleij8b92e172016-05-27 14:24:04 +02001039#ifdef CONFIG_COMPAT
1040static long gpio_ioctl_compat(struct file *filp, unsigned int cmd,
1041 unsigned long arg)
1042{
1043 return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
1044}
1045#endif
1046
Linus Walleij3c702e92015-10-21 15:29:53 +02001047/**
1048 * gpio_chrdev_open() - open the chardev for ioctl operations
1049 * @inode: inode for this chardev
1050 * @filp: file struct for storing private data
1051 * Returns 0 on success
1052 */
1053static int gpio_chrdev_open(struct inode *inode, struct file *filp)
1054{
1055 struct gpio_device *gdev = container_of(inode->i_cdev,
1056 struct gpio_device, chrdev);
1057
1058 /* Fail on open if the backing gpiochip is gone */
Stephen Boydfb505742017-01-09 11:47:44 -08001059 if (!gdev->chip)
Linus Walleij3c702e92015-10-21 15:29:53 +02001060 return -ENODEV;
1061 get_device(&gdev->dev);
1062 filp->private_data = gdev;
Lars-Peter Clausenf4e81c52016-11-30 13:05:21 +01001063
1064 return nonseekable_open(inode, filp);
Linus Walleij3c702e92015-10-21 15:29:53 +02001065}
1066
1067/**
1068 * gpio_chrdev_release() - close chardev after ioctl operations
1069 * @inode: inode for this chardev
1070 * @filp: file struct for storing private data
1071 * Returns 0 on success
1072 */
1073static int gpio_chrdev_release(struct inode *inode, struct file *filp)
1074{
1075 struct gpio_device *gdev = container_of(inode->i_cdev,
1076 struct gpio_device, chrdev);
1077
Linus Walleij3c702e92015-10-21 15:29:53 +02001078 put_device(&gdev->dev);
1079 return 0;
1080}
1081
1082
1083static const struct file_operations gpio_fileops = {
1084 .release = gpio_chrdev_release,
1085 .open = gpio_chrdev_open,
1086 .owner = THIS_MODULE,
Lars-Peter Clausenf4e81c52016-11-30 13:05:21 +01001087 .llseek = no_llseek,
Linus Walleij3c702e92015-10-21 15:29:53 +02001088 .unlocked_ioctl = gpio_ioctl,
Linus Walleij8b92e172016-05-27 14:24:04 +02001089#ifdef CONFIG_COMPAT
1090 .compat_ioctl = gpio_ioctl_compat,
1091#endif
Linus Walleij3c702e92015-10-21 15:29:53 +02001092};
1093
Linus Walleijff2b1352015-10-20 11:10:38 +02001094static void gpiodevice_release(struct device *dev)
1095{
1096 struct gpio_device *gdev = dev_get_drvdata(dev);
1097
1098 list_del(&gdev->list);
1099 ida_simple_remove(&gpio_ida, gdev->id);
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +01001100 kfree_const(gdev->label);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001101 kfree(gdev->descs);
Linus Walleij9efd9e62016-02-09 14:27:42 +01001102 kfree(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001103}
1104
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001105static int gpiochip_setup_dev(struct gpio_device *gdev)
1106{
1107 int status;
1108
1109 cdev_init(&gdev->chrdev, &gpio_fileops);
1110 gdev->chrdev.owner = THIS_MODULE;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001111 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001112
1113 status = cdev_device_add(&gdev->chrdev, &gdev->dev);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001114 if (status)
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001115 return status;
1116
1117 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
1118 MAJOR(gpio_devt), gdev->id);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001119
1120 status = gpiochip_sysfs_register(gdev);
1121 if (status)
1122 goto err_remove_device;
1123
1124 /* From this point, the .release() function cleans up gpio_device */
1125 gdev->dev.release = gpiodevice_release;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001126 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
1127 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
1128 dev_name(&gdev->dev), gdev->chip->label ? : "generic");
1129
1130 return 0;
1131
1132err_remove_device:
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001133 cdev_device_del(&gdev->chrdev, &gdev->dev);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001134 return status;
1135}
1136
1137static void gpiochip_setup_devs(void)
1138{
1139 struct gpio_device *gdev;
1140 int err;
1141
1142 list_for_each_entry(gdev, &gpio_devices, list) {
1143 err = gpiochip_setup_dev(gdev);
1144 if (err)
1145 pr_err("%s: Failed to initialize gpio device (%d)\n",
1146 dev_name(&gdev->dev), err);
1147 }
1148}
1149
Thierry Reding959bc7b2017-11-07 19:15:59 +01001150int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001151 struct lock_class_key *lock_key,
1152 struct lock_class_key *request_key)
David Brownelld2876d02008-02-04 22:28:20 -08001153{
1154 unsigned long flags;
1155 int status = 0;
Linus Walleijff2b1352015-10-20 11:10:38 +02001156 unsigned i;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001157 int base = chip->base;
Linus Walleijff2b1352015-10-20 11:10:38 +02001158 struct gpio_device *gdev;
David Brownelld2876d02008-02-04 22:28:20 -08001159
Linus Walleijff2b1352015-10-20 11:10:38 +02001160 /*
1161 * First: allocate and populate the internal stat container, and
1162 * set up the struct device.
1163 */
Josh Cartwright969f07b2016-02-17 16:44:15 -06001164 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
Linus Walleijff2b1352015-10-20 11:10:38 +02001165 if (!gdev)
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001166 return -ENOMEM;
Linus Walleij3c702e92015-10-21 15:29:53 +02001167 gdev->dev.bus = &gpio_bus_type;
Linus Walleijff2b1352015-10-20 11:10:38 +02001168 gdev->chip = chip;
1169 chip->gpiodev = gdev;
1170 if (chip->parent) {
1171 gdev->dev.parent = chip->parent;
1172 gdev->dev.of_node = chip->parent->of_node;
Thierry Redingacc6e332016-07-05 14:11:14 +02001173 }
1174
Linus Walleijff2b1352015-10-20 11:10:38 +02001175#ifdef CONFIG_OF_GPIO
1176 /* If the gpiochip has an assigned OF node this takes precedence */
Thierry Redingacc6e332016-07-05 14:11:14 +02001177 if (chip->of_node)
1178 gdev->dev.of_node = chip->of_node;
Linus Walleijff2b1352015-10-20 11:10:38 +02001179#endif
Thierry Redingacc6e332016-07-05 14:11:14 +02001180
Linus Walleijff2b1352015-10-20 11:10:38 +02001181 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
1182 if (gdev->id < 0) {
1183 status = gdev->id;
1184 goto err_free_gdev;
1185 }
1186 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
1187 device_initialize(&gdev->dev);
1188 dev_set_drvdata(&gdev->dev, gdev);
1189 if (chip->parent && chip->parent->driver)
1190 gdev->owner = chip->parent->driver->owner;
1191 else if (chip->owner)
1192 /* TODO: remove chip->owner */
1193 gdev->owner = chip->owner;
1194 else
1195 gdev->owner = THIS_MODULE;
David Brownelld2876d02008-02-04 22:28:20 -08001196
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001197 gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001198 if (!gdev->descs) {
Linus Walleijff2b1352015-10-20 11:10:38 +02001199 status = -ENOMEM;
1200 goto err_free_gdev;
1201 }
1202
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001203 if (chip->ngpio == 0) {
1204 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
Linus Walleijff2b1352015-10-20 11:10:38 +02001205 status = -EINVAL;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001206 goto err_free_descs;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001207 }
Linus Walleijdf4878e2016-02-12 14:48:23 +01001208
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +01001209 gdev->label = kstrdup_const(chip->label ?: "unknown", GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001210 if (!gdev->label) {
1211 status = -ENOMEM;
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001212 goto err_free_descs;
Linus Walleijdf4878e2016-02-12 14:48:23 +01001213 }
1214
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001215 gdev->ngpio = chip->ngpio;
Linus Walleij43c54ec2016-02-11 11:37:48 +01001216 gdev->data = data;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001217
David Brownelld2876d02008-02-04 22:28:20 -08001218 spin_lock_irqsave(&gpio_lock, flags);
1219
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001220 /*
1221 * TODO: this allocates a Linux GPIO number base in the global
1222 * GPIO numberspace for this chip. In the long run we want to
1223 * get *rid* of this numberspace and use only descriptors, but
1224 * it may be a pipe dream. It will not happen before we get rid
1225 * of the sysfs interface anyways.
1226 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001227 if (base < 0) {
1228 base = gpiochip_find_base(chip->ngpio);
1229 if (base < 0) {
1230 status = base;
Johan Hovold225fce82015-01-12 17:12:25 +01001231 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001232 goto err_free_label;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001233 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001234 /*
1235 * TODO: it should not be necessary to reflect the assigned
1236 * base outside of the GPIO subsystem. Go over drivers and
1237 * see if anyone makes use of this, else drop this and assign
1238 * a poison instead.
1239 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001240 chip->base = base;
1241 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001242 gdev->base = base;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001243
Linus Walleijff2b1352015-10-20 11:10:38 +02001244 status = gpiodev_add_to_list(gdev);
Johan Hovold05aa5202015-01-12 17:12:26 +01001245 if (status) {
1246 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001247 goto err_free_label;
Johan Hovold05aa5202015-01-12 17:12:26 +01001248 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001249
Linus Walleij545ebd92016-05-30 17:11:59 +02001250 spin_unlock_irqrestore(&gpio_lock, flags);
1251
Linus Walleijff2b1352015-10-20 11:10:38 +02001252 for (i = 0; i < chip->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001253 struct gpio_desc *desc = &gdev->descs[i];
David Brownelld8f388d82008-07-25 01:46:07 -07001254
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001255 desc->gdev = gdev;
Timur Tabi1ca2a922017-12-20 13:10:31 -06001256
1257 /* REVISIT: most hardware initializes GPIOs as inputs (often
1258 * with pullups enabled) so power usage is minimized. Linux
1259 * code should set the gpio direction first thing; but until
1260 * it does, and in case chip->get_direction is not set, we may
1261 * expose the wrong direction in sysfs.
Johan Hovold05aa5202015-01-12 17:12:26 +01001262 */
Timur Tabi1ca2a922017-12-20 13:10:31 -06001263 desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0;
David Brownelld2876d02008-02-04 22:28:20 -08001264 }
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001265
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301266#ifdef CONFIG_PINCTRL
Linus Walleij20ec3e32016-02-11 11:03:06 +01001267 INIT_LIST_HEAD(&gdev->pin_ranges);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301268#endif
1269
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001270 status = gpiochip_set_desc_names(chip);
1271 if (status)
1272 goto err_remove_from_list;
1273
Mika Westerberg79b804c2016-09-20 15:15:21 +03001274 status = gpiochip_irqchip_init_valid_mask(chip);
1275 if (status)
1276 goto err_remove_from_list;
1277
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001278 status = gpiochip_add_irqchip(chip, lock_key, request_key);
Thierry Redinge0d89722017-11-07 19:15:54 +01001279 if (status)
1280 goto err_remove_chip;
1281
Tomeu Vizoso28355f82015-07-14 10:29:54 +02001282 status = of_gpiochip_add(chip);
1283 if (status)
1284 goto err_remove_chip;
1285
Mika Westerberg664e3e52014-01-08 12:40:54 +02001286 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001287
Linus Walleij3c702e92015-10-21 15:29:53 +02001288 /*
1289 * By first adding the chardev, and then adding the device,
1290 * we get a device node entry in sysfs under
1291 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1292 * coldplug of device nodes and other udev business.
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001293 * We can do this only if gpiolib has been initialized.
1294 * Otherwise, defer until later.
Linus Walleij3c702e92015-10-21 15:29:53 +02001295 */
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001296 if (gpiolib_initialized) {
1297 status = gpiochip_setup_dev(gdev);
1298 if (status)
1299 goto err_remove_chip;
1300 }
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001301 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001302
Johan Hovold225fce82015-01-12 17:12:25 +01001303err_remove_chip:
1304 acpi_gpiochip_remove(chip);
Johan Hovold6d867502015-05-04 17:23:25 +02001305 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +01001306 of_gpiochip_remove(chip);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001307 gpiochip_irqchip_free_valid_mask(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001308err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +01001309 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001310 list_del(&gdev->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001311 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001312err_free_label:
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +01001313 kfree_const(gdev->label);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001314err_free_descs:
1315 kfree(gdev->descs);
Linus Walleijff2b1352015-10-20 11:10:38 +02001316err_free_gdev:
1317 ida_simple_remove(&gpio_ida, gdev->id);
David Brownelld2876d02008-02-04 22:28:20 -08001318 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +02001319 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001320 gdev->base, gdev->base + gdev->ngpio - 1,
1321 chip->label ? : "generic");
1322 kfree(gdev);
David Brownelld2876d02008-02-04 22:28:20 -08001323 return status;
1324}
Thierry Reding959bc7b2017-11-07 19:15:59 +01001325EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);
David Brownelld2876d02008-02-04 22:28:20 -08001326
1327/**
Linus Walleij43c54ec2016-02-11 11:37:48 +01001328 * gpiochip_get_data() - get per-subdriver data for the chip
Thierry Reding950d55f52017-07-24 16:57:22 +02001329 * @chip: GPIO chip
1330 *
1331 * Returns:
1332 * The per-subdriver data for the chip.
Linus Walleij43c54ec2016-02-11 11:37:48 +01001333 */
1334void *gpiochip_get_data(struct gpio_chip *chip)
1335{
1336 return chip->gpiodev->data;
1337}
1338EXPORT_SYMBOL_GPL(gpiochip_get_data);
1339
1340/**
David Brownelld2876d02008-02-04 22:28:20 -08001341 * gpiochip_remove() - unregister a gpio_chip
1342 * @chip: the chip to unregister
1343 *
1344 * A gpio_chip with any GPIOs still requested may not be removed.
1345 */
abdoulaye berthee1db1702014-07-05 18:28:50 +02001346void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -08001347{
Linus Walleijff2b1352015-10-20 11:10:38 +02001348 struct gpio_device *gdev = chip->gpiodev;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001349 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001350 unsigned long flags;
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001351 unsigned i;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001352 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -08001353
Linus Walleijff2b1352015-10-20 11:10:38 +02001354 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
Linus Walleijafbc4f32016-02-09 13:21:06 +01001355 gpiochip_sysfs_unregister(gdev);
Geert Uytterhoeven5018ada2016-12-19 18:29:23 +01001356 gpiochip_free_hogs(chip);
Bamvor Jian Zhangbd203bd2016-02-20 13:13:19 +08001357 /* Numb the device, cancelling all outstanding operations */
1358 gdev->chip = NULL;
Johan Hovold00acc3d2015-01-12 17:12:27 +01001359 gpiochip_irqchip_remove(chip);
Mika Westerberg6072b9d2014-03-10 14:54:53 +02001360 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +01001361 gpiochip_remove_pin_ranges(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001362 of_gpiochip_remove(chip);
Linus Walleij43c54ec2016-02-11 11:37:48 +01001363 /*
1364 * We accept no more calls into the driver from this point, so
1365 * NULL the driver data pointer
1366 */
1367 gdev->data = NULL;
Anton Vorontsov391c9702010-06-08 07:48:17 -06001368
Johan Hovold6798aca2015-01-12 17:12:28 +01001369 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001370 for (i = 0; i < gdev->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001371 desc = &gdev->descs[i];
Johan Hovoldfab28b82015-05-04 17:10:27 +02001372 if (test_bit(FLAG_REQUESTED, &desc->flags))
1373 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -08001374 }
David Brownelld2876d02008-02-04 22:28:20 -08001375 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001376
Johan Hovoldfab28b82015-05-04 17:10:27 +02001377 if (requested)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001378 dev_crit(&gdev->dev,
Linus Walleij58383c782015-11-04 09:56:26 +01001379 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +02001380
Linus Walleijff2b1352015-10-20 11:10:38 +02001381 /*
1382 * The gpiochip side puts its use of the device to rest here:
1383 * if there are no userspace clients, the chardev and device will
1384 * be removed, else it will be dangling until the last user is
1385 * gone.
1386 */
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001387 cdev_device_del(&gdev->chrdev, &gdev->dev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001388 put_device(&gdev->dev);
David Brownelld2876d02008-02-04 22:28:20 -08001389}
1390EXPORT_SYMBOL_GPL(gpiochip_remove);
1391
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301392static void devm_gpio_chip_release(struct device *dev, void *res)
1393{
1394 struct gpio_chip *chip = *(struct gpio_chip **)res;
1395
1396 gpiochip_remove(chip);
1397}
1398
1399static int devm_gpio_chip_match(struct device *dev, void *res, void *data)
1400
1401{
1402 struct gpio_chip **r = res;
1403
1404 if (!r || !*r) {
1405 WARN_ON(!r || !*r);
1406 return 0;
1407 }
1408
1409 return *r == data;
1410}
1411
1412/**
Jonathan Neuschäfer689fd022017-12-21 17:56:34 +01001413 * devm_gpiochip_add_data() - Resource manager gpiochip_add_data()
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301414 * @dev: the device pointer on which irq_chip belongs to.
1415 * @chip: the chip to register, with chip->base initialized
Thierry Reding950d55f52017-07-24 16:57:22 +02001416 * @data: driver-private data associated with this chip
1417 *
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301418 * Context: potentially before irqs will work
1419 *
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301420 * The gpio chip automatically be released when the device is unbound.
Thierry Reding950d55f52017-07-24 16:57:22 +02001421 *
1422 * Returns:
1423 * A negative errno if the chip can't be registered, such as because the
1424 * chip->base is invalid or already associated with a different chip.
1425 * Otherwise it returns zero as a success code.
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301426 */
1427int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
1428 void *data)
1429{
1430 struct gpio_chip **ptr;
1431 int ret;
1432
1433 ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
1434 GFP_KERNEL);
1435 if (!ptr)
1436 return -ENOMEM;
1437
1438 ret = gpiochip_add_data(chip, data);
1439 if (ret < 0) {
1440 devres_free(ptr);
1441 return ret;
1442 }
1443
1444 *ptr = chip;
1445 devres_add(dev, ptr);
1446
1447 return 0;
1448}
1449EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
1450
1451/**
1452 * devm_gpiochip_remove() - Resource manager of gpiochip_remove()
1453 * @dev: device for which which resource was allocated
1454 * @chip: the chip to remove
1455 *
1456 * A gpio_chip with any GPIOs still requested may not be removed.
1457 */
1458void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip)
1459{
1460 int ret;
1461
1462 ret = devres_release(dev, devm_gpio_chip_release,
1463 devm_gpio_chip_match, chip);
Christophe JAILLET3988d662017-01-27 12:56:42 +01001464 WARN_ON(ret);
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301465}
1466EXPORT_SYMBOL_GPL(devm_gpiochip_remove);
1467
Grant Likely594fa262010-06-08 07:48:16 -06001468/**
1469 * gpiochip_find() - iterator for locating a specific gpio_chip
1470 * @data: data to pass to match function
Thierry Reding950d55f52017-07-24 16:57:22 +02001471 * @match: Callback function to check gpio_chip
Grant Likely594fa262010-06-08 07:48:16 -06001472 *
1473 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1474 * determined by a user supplied @match callback. The callback should return
1475 * 0 if the device doesn't match and non-zero if it does. If the callback is
1476 * non-zero, this function will return to the caller and not iterate over any
1477 * more gpio_chips.
1478 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001479struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001480 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001481 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001482{
Linus Walleijff2b1352015-10-20 11:10:38 +02001483 struct gpio_device *gdev;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001484 struct gpio_chip *chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -06001485 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001486
1487 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001488 list_for_each_entry(gdev, &gpio_devices, list)
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001489 if (gdev->chip && match(gdev->chip, data)) {
1490 chip = gdev->chip;
Grant Likely594fa262010-06-08 07:48:16 -06001491 break;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001492 }
Linus Walleijff2b1352015-10-20 11:10:38 +02001493
Grant Likely594fa262010-06-08 07:48:16 -06001494 spin_unlock_irqrestore(&gpio_lock, flags);
1495
1496 return chip;
1497}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001498EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001499
Alexandre Courbot79697ef2013-11-16 21:39:32 +09001500static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1501{
1502 const char *name = data;
1503
1504 return !strcmp(chip->label, name);
1505}
1506
1507static struct gpio_chip *find_chip_by_name(const char *name)
1508{
1509 return gpiochip_find((void *)name, gpiochip_match_name);
1510}
1511
Linus Walleij14250522014-03-25 10:40:18 +01001512#ifdef CONFIG_GPIOLIB_IRQCHIP
1513
1514/*
1515 * The following is irqchip helper code for gpiochips.
1516 */
1517
Mika Westerberg79b804c2016-09-20 15:15:21 +03001518static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1519{
Thierry Redingdc7b0382017-11-07 19:15:52 +01001520 if (!gpiochip->irq.need_valid_mask)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001521 return 0;
1522
Stephen Boyde4371f6e2018-03-23 09:34:50 -07001523 gpiochip->irq.valid_mask = gpiochip_allocate_mask(gpiochip);
Thierry Redingdc7b0382017-11-07 19:15:52 +01001524 if (!gpiochip->irq.valid_mask)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001525 return -ENOMEM;
1526
Mika Westerberg79b804c2016-09-20 15:15:21 +03001527 return 0;
1528}
1529
1530static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1531{
Thierry Redingdc7b0382017-11-07 19:15:52 +01001532 kfree(gpiochip->irq.valid_mask);
1533 gpiochip->irq.valid_mask = NULL;
Mika Westerberg79b804c2016-09-20 15:15:21 +03001534}
1535
Stephen Boyd64ff2c82018-01-09 17:58:46 -08001536bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
1537 unsigned int offset)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001538{
1539 /* No mask means all valid */
Thierry Redingdc7b0382017-11-07 19:15:52 +01001540 if (likely(!gpiochip->irq.valid_mask))
Mika Westerberg79b804c2016-09-20 15:15:21 +03001541 return true;
Thierry Redingdc7b0382017-11-07 19:15:52 +01001542 return test_bit(offset, gpiochip->irq.valid_mask);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001543}
Stephen Boyd64ff2c82018-01-09 17:58:46 -08001544EXPORT_SYMBOL_GPL(gpiochip_irqchip_irq_valid);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001545
Linus Walleij14250522014-03-25 10:40:18 +01001546/**
Linus Walleijd245b3f2016-11-24 10:57:25 +01001547 * gpiochip_set_cascaded_irqchip() - connects a cascaded irqchip to a gpiochip
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001548 * @gpiochip: the gpiochip to set the irqchip chain to
1549 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01001550 * @parent_irq: the irq number corresponding to the parent IRQ for this
1551 * chained irqchip
1552 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001553 * coming out of the gpiochip. If the interrupt is nested rather than
1554 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +01001555 */
Linus Walleijd245b3f2016-11-24 10:57:25 +01001556static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gpiochip,
1557 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001558 unsigned int parent_irq,
Linus Walleijd245b3f2016-11-24 10:57:25 +01001559 irq_flow_handler_t parent_handler)
Linus Walleij14250522014-03-25 10:40:18 +01001560{
Linus Walleij83141a72014-09-26 13:50:12 +02001561 unsigned int offset;
1562
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001563 if (!gpiochip->irq.domain) {
Linus Walleij83141a72014-09-26 13:50:12 +02001564 chip_err(gpiochip, "called %s before setting up irqchip\n",
1565 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +02001566 return;
1567 }
1568
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001569 if (parent_handler) {
1570 if (gpiochip->can_sleep) {
1571 chip_err(gpiochip,
1572 "you cannot have chained interrupts on a "
1573 "chip that may sleep\n");
1574 return;
1575 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001576 /*
1577 * The parent irqchip is already using the chip_data for this
1578 * irqchip, so our callbacks simply use the handler_data.
1579 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +02001580 irq_set_chained_handler_and_data(parent_irq, parent_handler,
1581 gpiochip);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001582
Thierry Reding39e5f092017-11-07 19:15:50 +01001583 gpiochip->irq.parents = &parent_irq;
1584 gpiochip->irq.num_parents = 1;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001585 }
Linus Walleij83141a72014-09-26 13:50:12 +02001586
1587 /* Set the parent IRQ for all affected IRQs */
Mika Westerberg79b804c2016-09-20 15:15:21 +03001588 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1589 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1590 continue;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001591 irq_set_parent(irq_find_mapping(gpiochip->irq.domain, offset),
Linus Walleij83141a72014-09-26 13:50:12 +02001592 parent_irq);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001593 }
Linus Walleij14250522014-03-25 10:40:18 +01001594}
Linus Walleijd245b3f2016-11-24 10:57:25 +01001595
1596/**
1597 * gpiochip_set_chained_irqchip() - connects a chained irqchip to a gpiochip
1598 * @gpiochip: the gpiochip to set the irqchip chain to
1599 * @irqchip: the irqchip to chain to the gpiochip
1600 * @parent_irq: the irq number corresponding to the parent IRQ for this
1601 * chained irqchip
1602 * @parent_handler: the parent interrupt handler for the accumulated IRQ
1603 * coming out of the gpiochip. If the interrupt is nested rather than
1604 * cascaded, pass NULL in this handler argument
1605 */
1606void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
1607 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001608 unsigned int parent_irq,
Linus Walleijd245b3f2016-11-24 10:57:25 +01001609 irq_flow_handler_t parent_handler)
1610{
Thierry Reding60ed54c2017-11-07 19:15:57 +01001611 if (gpiochip->irq.threaded) {
1612 chip_err(gpiochip, "tried to chain a threaded gpiochip\n");
1613 return;
1614 }
1615
Linus Walleijd245b3f2016-11-24 10:57:25 +01001616 gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
1617 parent_handler);
1618}
Linus Walleij14250522014-03-25 10:40:18 +01001619EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
1620
1621/**
Linus Walleijd245b3f2016-11-24 10:57:25 +01001622 * gpiochip_set_nested_irqchip() - connects a nested irqchip to a gpiochip
1623 * @gpiochip: the gpiochip to set the irqchip nested handler to
1624 * @irqchip: the irqchip to nest to the gpiochip
1625 * @parent_irq: the irq number corresponding to the parent IRQ for this
1626 * nested irqchip
1627 */
1628void gpiochip_set_nested_irqchip(struct gpio_chip *gpiochip,
1629 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001630 unsigned int parent_irq)
Linus Walleijd245b3f2016-11-24 10:57:25 +01001631{
Linus Walleijd245b3f2016-11-24 10:57:25 +01001632 gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
1633 NULL);
1634}
1635EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip);
1636
1637/**
Linus Walleij14250522014-03-25 10:40:18 +01001638 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1639 * @d: the irqdomain used by this irqchip
1640 * @irq: the global irq number used by this GPIO irqchip irq
1641 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1642 *
1643 * This function will set up the mapping for a certain IRQ line on a
1644 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1645 * stored inside the gpiochip.
1646 */
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001647int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1648 irq_hw_number_t hwirq)
Linus Walleij14250522014-03-25 10:40:18 +01001649{
1650 struct gpio_chip *chip = d->host_data;
Thierry Redinge0d89722017-11-07 19:15:54 +01001651 int err = 0;
Linus Walleij14250522014-03-25 10:40:18 +01001652
Grygorii Strashkodc749a02017-07-21 11:49:00 -05001653 if (!gpiochip_irqchip_irq_valid(chip, hwirq))
1654 return -ENXIO;
1655
Linus Walleij14250522014-03-25 10:40:18 +01001656 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001657 /*
1658 * This lock class tells lockdep that GPIO irqs are in a different
1659 * category than their parents, so it won't report false recursion.
1660 */
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001661 irq_set_lockdep_class(irq, chip->irq.lock_key, chip->irq.request_key);
Thierry Redingc7a0aa52017-11-07 19:15:48 +01001662 irq_set_chip_and_handler(irq, chip->irq.chip, chip->irq.handler);
Linus Walleijd245b3f2016-11-24 10:57:25 +01001663 /* Chips that use nested thread handlers have them marked */
Thierry Reding60ed54c2017-11-07 19:15:57 +01001664 if (chip->irq.threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02001665 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +01001666 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -05001667
Thierry Redinge0d89722017-11-07 19:15:54 +01001668 if (chip->irq.num_parents == 1)
1669 err = irq_set_parent(irq, chip->irq.parents[0]);
1670 else if (chip->irq.map)
1671 err = irq_set_parent(irq, chip->irq.map[hwirq]);
1672
1673 if (err < 0)
1674 return err;
1675
Linus Walleij1333b902014-04-23 16:45:12 +02001676 /*
1677 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1678 * is passed as default type.
1679 */
Thierry Reding3634eeb2017-11-07 19:15:49 +01001680 if (chip->irq.default_type != IRQ_TYPE_NONE)
1681 irq_set_irq_type(irq, chip->irq.default_type);
Linus Walleij14250522014-03-25 10:40:18 +01001682
1683 return 0;
1684}
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001685EXPORT_SYMBOL_GPL(gpiochip_irq_map);
Linus Walleij14250522014-03-25 10:40:18 +01001686
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001687void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
Linus Walleijc3626fd2014-03-28 20:42:01 +01001688{
Linus Walleij1c8732b2014-04-09 13:34:39 +02001689 struct gpio_chip *chip = d->host_data;
1690
Thierry Reding60ed54c2017-11-07 19:15:57 +01001691 if (chip->irq.threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02001692 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001693 irq_set_chip_and_handler(irq, NULL, NULL);
1694 irq_set_chip_data(irq, NULL);
1695}
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001696EXPORT_SYMBOL_GPL(gpiochip_irq_unmap);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001697
Linus Walleij14250522014-03-25 10:40:18 +01001698static const struct irq_domain_ops gpiochip_domain_ops = {
1699 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +01001700 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +01001701 /* Virtually all GPIO irqchips are twocell:ed */
1702 .xlate = irq_domain_xlate_twocell,
1703};
1704
1705static int gpiochip_irq_reqres(struct irq_data *d)
1706{
1707 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1708
Linus Walleijff2b1352015-10-20 11:10:38 +02001709 if (!try_module_get(chip->gpiodev->owner))
Grygorii Strashko5b76e792015-06-25 20:30:50 +03001710 return -ENODEV;
1711
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001712 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +01001713 chip_err(chip,
1714 "unable to lock HW IRQ %lu for IRQ\n",
1715 d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001716 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001717 return -EINVAL;
1718 }
1719 return 0;
1720}
1721
1722static void gpiochip_irq_relres(struct irq_data *d)
1723{
1724 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1725
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001726 gpiochip_unlock_as_irq(chip, d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001727 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001728}
1729
1730static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
1731{
Grygorii Strashkodc749a02017-07-21 11:49:00 -05001732 if (!gpiochip_irqchip_irq_valid(chip, offset))
1733 return -ENXIO;
Thierry Redinge0d89722017-11-07 19:15:54 +01001734
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001735 return irq_create_mapping(chip->irq.domain, offset);
Linus Walleij14250522014-03-25 10:40:18 +01001736}
1737
1738/**
Thierry Redinge0d89722017-11-07 19:15:54 +01001739 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
1740 * @gpiochip: the GPIO chip to add the IRQ chip to
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001741 * @lock_key: lockdep class for IRQ lock
1742 * @request_key: lockdep class for IRQ request
Thierry Redinge0d89722017-11-07 19:15:54 +01001743 */
Thierry Reding959bc7b2017-11-07 19:15:59 +01001744static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001745 struct lock_class_key *lock_key,
1746 struct lock_class_key *request_key)
Thierry Redinge0d89722017-11-07 19:15:54 +01001747{
1748 struct irq_chip *irqchip = gpiochip->irq.chip;
1749 const struct irq_domain_ops *ops;
1750 struct device_node *np;
1751 unsigned int type;
1752 unsigned int i;
1753
1754 if (!irqchip)
1755 return 0;
1756
1757 if (gpiochip->irq.parent_handler && gpiochip->can_sleep) {
1758 chip_err(gpiochip, "you cannot have chained interrupts on a "
1759 "chip that may sleep\n");
1760 return -EINVAL;
1761 }
1762
1763 np = gpiochip->gpiodev->dev.of_node;
1764 type = gpiochip->irq.default_type;
1765
1766 /*
1767 * Specifying a default trigger is a terrible idea if DT or ACPI is
1768 * used to configure the interrupts, as you may end up with
1769 * conflicting triggers. Tell the user, and reset to NONE.
1770 */
1771 if (WARN(np && type != IRQ_TYPE_NONE,
1772 "%s: Ignoring %u default trigger\n", np->full_name, type))
1773 type = IRQ_TYPE_NONE;
1774
1775 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
1776 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1777 "Ignoring %u default trigger\n", type);
1778 type = IRQ_TYPE_NONE;
1779 }
1780
1781 gpiochip->to_irq = gpiochip_to_irq;
1782 gpiochip->irq.default_type = type;
Thierry Reding959bc7b2017-11-07 19:15:59 +01001783 gpiochip->irq.lock_key = lock_key;
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001784 gpiochip->irq.request_key = request_key;
Thierry Redinge0d89722017-11-07 19:15:54 +01001785
1786 if (gpiochip->irq.domain_ops)
1787 ops = gpiochip->irq.domain_ops;
1788 else
1789 ops = &gpiochip_domain_ops;
1790
1791 gpiochip->irq.domain = irq_domain_add_simple(np, gpiochip->ngpio,
Thierry Reding8302cf52017-11-07 19:15:58 +01001792 gpiochip->irq.first,
1793 ops, gpiochip);
Thierry Redinge0d89722017-11-07 19:15:54 +01001794 if (!gpiochip->irq.domain)
1795 return -EINVAL;
1796
1797 /*
1798 * It is possible for a driver to override this, but only if the
1799 * alternative functions are both implemented.
1800 */
1801 if (!irqchip->irq_request_resources &&
1802 !irqchip->irq_release_resources) {
1803 irqchip->irq_request_resources = gpiochip_irq_reqres;
1804 irqchip->irq_release_resources = gpiochip_irq_relres;
1805 }
1806
1807 if (gpiochip->irq.parent_handler) {
1808 void *data = gpiochip->irq.parent_handler_data ?: gpiochip;
1809
1810 for (i = 0; i < gpiochip->irq.num_parents; i++) {
1811 /*
1812 * The parent IRQ chip is already using the chip_data
1813 * for this IRQ chip, so our callbacks simply use the
1814 * handler_data.
1815 */
1816 irq_set_chained_handler_and_data(gpiochip->irq.parents[i],
1817 gpiochip->irq.parent_handler,
1818 data);
1819 }
Thierry Redinge0d89722017-11-07 19:15:54 +01001820 }
1821
1822 acpi_gpiochip_request_interrupts(gpiochip);
1823
1824 return 0;
1825}
1826
1827/**
Linus Walleij14250522014-03-25 10:40:18 +01001828 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1829 * @gpiochip: the gpiochip to remove the irqchip from
1830 *
1831 * This is called only from gpiochip_remove()
1832 */
1833static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
1834{
Thierry Reding39e5f092017-11-07 19:15:50 +01001835 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +01001836
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001837 acpi_gpiochip_free_interrupts(gpiochip);
1838
Thierry Redinge0d89722017-11-07 19:15:54 +01001839 if (gpiochip->irq.chip && gpiochip->irq.parent_handler) {
Thierry Reding39e5f092017-11-07 19:15:50 +01001840 struct gpio_irq_chip *irq = &gpiochip->irq;
1841 unsigned int i;
1842
1843 for (i = 0; i < irq->num_parents; i++)
1844 irq_set_chained_handler_and_data(irq->parents[i],
1845 NULL, NULL);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001846 }
1847
Linus Walleijc3626fd2014-03-28 20:42:01 +01001848 /* Remove all IRQ mappings and delete the domain */
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001849 if (gpiochip->irq.domain) {
Thierry Reding39e5f092017-11-07 19:15:50 +01001850 unsigned int irq;
1851
Mika Westerberg79b804c2016-09-20 15:15:21 +03001852 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1853 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1854 continue;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001855
1856 irq = irq_find_mapping(gpiochip->irq.domain, offset);
1857 irq_dispose_mapping(irq);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001858 }
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001859
1860 irq_domain_remove(gpiochip->irq.domain);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001861 }
Linus Walleij14250522014-03-25 10:40:18 +01001862
Thierry Redingda80ff82017-11-07 19:15:46 +01001863 if (gpiochip->irq.chip) {
1864 gpiochip->irq.chip->irq_request_resources = NULL;
1865 gpiochip->irq.chip->irq_release_resources = NULL;
1866 gpiochip->irq.chip = NULL;
Linus Walleij14250522014-03-25 10:40:18 +01001867 }
Mika Westerberg79b804c2016-09-20 15:15:21 +03001868
1869 gpiochip_irqchip_free_valid_mask(gpiochip);
Linus Walleij14250522014-03-25 10:40:18 +01001870}
1871
1872/**
Linus Walleij739e6f52017-01-11 13:37:07 +01001873 * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01001874 * @gpiochip: the gpiochip to add the irqchip to
1875 * @irqchip: the irqchip to add to the gpiochip
1876 * @first_irq: if not dynamically assigned, the base (first) IRQ to
1877 * allocate gpiochip irqs from
1878 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +02001879 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
1880 * to have the core avoid setting up any default type in the hardware.
Thierry Reding60ed54c2017-11-07 19:15:57 +01001881 * @threaded: whether this irqchip uses a nested thread handler
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001882 * @lock_key: lockdep class for IRQ lock
1883 * @request_key: lockdep class for IRQ request
Linus Walleij14250522014-03-25 10:40:18 +01001884 *
1885 * This function closely associates a certain irqchip with a certain
1886 * gpiochip, providing an irq domain to translate the local IRQs to
1887 * global irqs in the gpiolib core, and making sure that the gpiochip
1888 * is passed as chip data to all related functions. Driver callbacks
Linus Walleij09dd5f92015-12-07 15:31:58 +01001889 * need to use gpiochip_get_data() to get their local state containers back
Linus Walleij14250522014-03-25 10:40:18 +01001890 * from the gpiochip passed as chip data. An irqdomain will be stored
1891 * in the gpiochip that shall be used by the driver to handle IRQ number
1892 * translation. The gpiochip will need to be initialized and registered
1893 * before calling this function.
1894 *
Linus Walleijc3626fd2014-03-28 20:42:01 +01001895 * This function will handle two cell:ed simple IRQs and assumes all
1896 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +01001897 * need to be open coded.
1898 */
Linus Walleij739e6f52017-01-11 13:37:07 +01001899int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
1900 struct irq_chip *irqchip,
1901 unsigned int first_irq,
1902 irq_flow_handler_t handler,
1903 unsigned int type,
Thierry Reding60ed54c2017-11-07 19:15:57 +01001904 bool threaded,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001905 struct lock_class_key *lock_key,
1906 struct lock_class_key *request_key)
Linus Walleij14250522014-03-25 10:40:18 +01001907{
1908 struct device_node *of_node;
Linus Walleij14250522014-03-25 10:40:18 +01001909
1910 if (!gpiochip || !irqchip)
1911 return -EINVAL;
1912
Linus Walleij58383c782015-11-04 09:56:26 +01001913 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +01001914 pr_err("missing gpiochip .dev parent pointer\n");
1915 return -EINVAL;
1916 }
Thierry Reding60ed54c2017-11-07 19:15:57 +01001917 gpiochip->irq.threaded = threaded;
Linus Walleij58383c782015-11-04 09:56:26 +01001918 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +01001919#ifdef CONFIG_OF_GPIO
1920 /*
Colin Cronin20a8a962015-05-18 11:41:43 -07001921 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08001922 * FIXME: get rid of this and use gpiochip->parent->of_node
1923 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +01001924 */
1925 if (gpiochip->of_node)
1926 of_node = gpiochip->of_node;
1927#endif
Marc Zyngier332e99d2016-09-07 09:12:11 +01001928 /*
Mika Westerberg0a1e0052016-09-12 14:29:51 +03001929 * Specifying a default trigger is a terrible idea if DT or ACPI is
Marc Zyngier332e99d2016-09-07 09:12:11 +01001930 * used to configure the interrupts, as you may end-up with
1931 * conflicting triggers. Tell the user, and reset to NONE.
1932 */
1933 if (WARN(of_node && type != IRQ_TYPE_NONE,
Rob Herring7eb6ce22017-07-18 16:43:03 -05001934 "%pOF: Ignoring %d default trigger\n", of_node, type))
Marc Zyngier332e99d2016-09-07 09:12:11 +01001935 type = IRQ_TYPE_NONE;
Mika Westerberg0a1e0052016-09-12 14:29:51 +03001936 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
1937 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1938 "Ignoring %d default trigger\n", type);
1939 type = IRQ_TYPE_NONE;
1940 }
Marc Zyngier332e99d2016-09-07 09:12:11 +01001941
Thierry Redingda80ff82017-11-07 19:15:46 +01001942 gpiochip->irq.chip = irqchip;
Thierry Redingc7a0aa52017-11-07 19:15:48 +01001943 gpiochip->irq.handler = handler;
Thierry Reding3634eeb2017-11-07 19:15:49 +01001944 gpiochip->irq.default_type = type;
Linus Walleij14250522014-03-25 10:40:18 +01001945 gpiochip->to_irq = gpiochip_to_irq;
Thierry Redingca9df052017-11-07 19:15:53 +01001946 gpiochip->irq.lock_key = lock_key;
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001947 gpiochip->irq.request_key = request_key;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001948 gpiochip->irq.domain = irq_domain_add_simple(of_node,
Linus Walleij14250522014-03-25 10:40:18 +01001949 gpiochip->ngpio, first_irq,
1950 &gpiochip_domain_ops, gpiochip);
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001951 if (!gpiochip->irq.domain) {
Thierry Redingda80ff82017-11-07 19:15:46 +01001952 gpiochip->irq.chip = NULL;
Linus Walleij14250522014-03-25 10:40:18 +01001953 return -EINVAL;
1954 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +02001955
1956 /*
1957 * It is possible for a driver to override this, but only if the
1958 * alternative functions are both implemented.
1959 */
1960 if (!irqchip->irq_request_resources &&
1961 !irqchip->irq_release_resources) {
1962 irqchip->irq_request_resources = gpiochip_irq_reqres;
1963 irqchip->irq_release_resources = gpiochip_irq_relres;
1964 }
Linus Walleij14250522014-03-25 10:40:18 +01001965
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001966 acpi_gpiochip_request_interrupts(gpiochip);
1967
Linus Walleij14250522014-03-25 10:40:18 +01001968 return 0;
1969}
Linus Walleij739e6f52017-01-11 13:37:07 +01001970EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key);
Linus Walleij14250522014-03-25 10:40:18 +01001971
1972#else /* CONFIG_GPIOLIB_IRQCHIP */
1973
Thierry Reding959bc7b2017-11-07 19:15:59 +01001974static inline int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001975 struct lock_class_key *lock_key,
1976 struct lock_class_key *request_key)
Thierry Redinge0d89722017-11-07 19:15:54 +01001977{
1978 return 0;
1979}
1980
Linus Walleij14250522014-03-25 10:40:18 +01001981static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
Mika Westerberg79b804c2016-09-20 15:15:21 +03001982static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1983{
1984 return 0;
1985}
1986static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1987{ }
Linus Walleij14250522014-03-25 10:40:18 +01001988
1989#endif /* CONFIG_GPIOLIB_IRQCHIP */
1990
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001991/**
1992 * gpiochip_generic_request() - request the gpio function for a pin
1993 * @chip: the gpiochip owning the GPIO
1994 * @offset: the offset of the GPIO to request for GPIO function
1995 */
1996int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
1997{
Linus Walleija9a1d2a2017-09-22 11:02:10 +02001998 return pinctrl_gpio_request(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001999}
2000EXPORT_SYMBOL_GPL(gpiochip_generic_request);
2001
2002/**
2003 * gpiochip_generic_free() - free the gpio function from a pin
2004 * @chip: the gpiochip to request the gpio function for
2005 * @offset: the offset of the GPIO to free from GPIO function
2006 */
2007void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
2008{
Linus Walleija9a1d2a2017-09-22 11:02:10 +02002009 pinctrl_gpio_free(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02002010}
2011EXPORT_SYMBOL_GPL(gpiochip_generic_free);
2012
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002013/**
2014 * gpiochip_generic_config() - apply configuration for a pin
2015 * @chip: the gpiochip owning the GPIO
2016 * @offset: the offset of the GPIO to apply the configuration
2017 * @config: the configuration to be applied
2018 */
2019int gpiochip_generic_config(struct gpio_chip *chip, unsigned offset,
2020 unsigned long config)
2021{
2022 return pinctrl_gpio_set_config(chip->gpiodev->base + offset, config);
2023}
2024EXPORT_SYMBOL_GPL(gpiochip_generic_config);
2025
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302026#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01002027
Linus Walleij3f0f8672012-11-20 12:40:15 +01002028/**
Christian Ruppert586a87e2013-10-15 15:37:54 +02002029 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
2030 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +02002031 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +02002032 * @gpio_offset: the start offset in the current gpio_chip number space
2033 * @pin_group: name of the pin group inside the pin controller
2034 */
2035int gpiochip_add_pingroup_range(struct gpio_chip *chip,
2036 struct pinctrl_dev *pctldev,
2037 unsigned int gpio_offset, const char *pin_group)
2038{
2039 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002040 struct gpio_device *gdev = chip->gpiodev;
Christian Ruppert586a87e2013-10-15 15:37:54 +02002041 int ret;
2042
2043 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
2044 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002045 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +02002046 return -ENOMEM;
2047 }
2048
2049 /* Use local offset as range ID */
2050 pin_range->range.id = gpio_offset;
2051 pin_range->range.gc = chip;
2052 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002053 pin_range->range.base = gdev->base + gpio_offset;
Christian Ruppert586a87e2013-10-15 15:37:54 +02002054 pin_range->pctldev = pctldev;
2055
2056 ret = pinctrl_get_group_pins(pctldev, pin_group,
2057 &pin_range->range.pins,
2058 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +01002059 if (ret < 0) {
2060 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +02002061 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +01002062 }
Christian Ruppert586a87e2013-10-15 15:37:54 +02002063
2064 pinctrl_add_gpio_range(pctldev, &pin_range->range);
2065
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002066 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
2067 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +02002068 pinctrl_dev_get_devname(pctldev), pin_group);
2069
Linus Walleij20ec3e32016-02-11 11:03:06 +01002070 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Christian Ruppert586a87e2013-10-15 15:37:54 +02002071
2072 return 0;
2073}
2074EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
2075
2076/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01002077 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
2078 * @chip: the gpiochip to add the range for
Thierry Reding950d55f52017-07-24 16:57:22 +02002079 * @pinctl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01002080 * @gpio_offset: the start offset in the current gpio_chip number space
2081 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01002082 * @npins: the number of pins from the offset of each pin space (GPIO and
2083 * pin controller) to accumulate in this range
Thierry Reding950d55f52017-07-24 16:57:22 +02002084 *
2085 * Returns:
2086 * 0 on success, or a negative error-code on failure.
Linus Walleij3f0f8672012-11-20 12:40:15 +01002087 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002088int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01002089 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01002090 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302091{
2092 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002093 struct gpio_device *gdev = chip->gpiodev;
Axel Linb4d4b1f2012-11-21 14:33:56 +08002094 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302095
Linus Walleij3f0f8672012-11-20 12:40:15 +01002096 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302097 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002098 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002099 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302100 }
2101
Linus Walleij3f0f8672012-11-20 12:40:15 +01002102 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01002103 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01002104 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302105 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002106 pin_range->range.base = gdev->base + gpio_offset;
Linus Walleij316511c2012-11-21 08:48:09 +01002107 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302108 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01002109 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302110 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01002111 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08002112 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002113 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +01002114 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08002115 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01002116 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002117 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
2118 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +01002119 pinctl_name,
2120 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302121
Linus Walleij20ec3e32016-02-11 11:03:06 +01002122 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002123
2124 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302125}
Linus Walleij165adc92012-11-06 14:49:39 +01002126EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302127
Linus Walleij3f0f8672012-11-20 12:40:15 +01002128/**
2129 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
2130 * @chip: the chip to remove all the mappings for
2131 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302132void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
2133{
2134 struct gpio_pin_range *pin_range, *tmp;
Linus Walleij20ec3e32016-02-11 11:03:06 +01002135 struct gpio_device *gdev = chip->gpiodev;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302136
Linus Walleij20ec3e32016-02-11 11:03:06 +01002137 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302138 list_del(&pin_range->node);
2139 pinctrl_remove_gpio_range(pin_range->pctldev,
2140 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01002141 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302142 }
2143}
Linus Walleij165adc92012-11-06 14:49:39 +01002144EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
2145
2146#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302147
David Brownelld2876d02008-02-04 22:28:20 -08002148/* These "optional" allocation calls help prevent drivers from stomping
2149 * on each other, and help provide better diagnostics in debugfs.
2150 * They're called even less than the "set direction" calls.
2151 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002152static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08002153{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002154 struct gpio_chip *chip = desc->gdev->chip;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002155 int status;
David Brownelld2876d02008-02-04 22:28:20 -08002156 unsigned long flags;
2157
2158 spin_lock_irqsave(&gpio_lock, flags);
2159
David Brownelld2876d02008-02-04 22:28:20 -08002160 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07002161 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08002162 */
2163
2164 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
2165 desc_set_label(desc, label ? : "?");
2166 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07002167 } else {
David Brownelld2876d02008-02-04 22:28:20 -08002168 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -08002169 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07002170 }
2171
2172 if (chip->request) {
2173 /* chip->request may sleep */
2174 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002175 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07002176 spin_lock_irqsave(&gpio_lock, flags);
2177
2178 if (status < 0) {
2179 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -07002180 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03002181 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07002182 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07002183 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03002184 if (chip->get_direction) {
2185 /* chip->get_direction may sleep */
2186 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002187 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03002188 spin_lock_irqsave(&gpio_lock, flags);
2189 }
David Brownelld2876d02008-02-04 22:28:20 -08002190done:
Mika Westerberg77c2d792014-03-10 14:54:50 +02002191 spin_unlock_irqrestore(&gpio_lock, flags);
2192 return status;
2193}
2194
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002195/*
2196 * This descriptor validation needs to be inserted verbatim into each
2197 * function taking a descriptor, so we need to use a preprocessor
Linus Walleij54d77192016-05-30 16:48:39 +02002198 * macro to avoid endless duplication. If the desc is NULL it is an
2199 * optional GPIO and calls should just bail out.
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002200 */
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002201static int validate_desc(const struct gpio_desc *desc, const char *func)
2202{
2203 if (!desc)
2204 return 0;
2205 if (IS_ERR(desc)) {
2206 pr_warn("%s: invalid GPIO (errorpointer)\n", func);
2207 return PTR_ERR(desc);
2208 }
2209 if (!desc->gdev) {
2210 pr_warn("%s: invalid GPIO (no device)\n", func);
2211 return -EINVAL;
2212 }
2213 if (!desc->gdev->chip) {
2214 dev_warn(&desc->gdev->dev,
2215 "%s: backing chip is gone\n", func);
2216 return 0;
2217 }
2218 return 1;
2219}
2220
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002221#define VALIDATE_DESC(desc) do { \
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002222 int __valid = validate_desc(desc, __func__); \
2223 if (__valid <= 0) \
2224 return __valid; \
2225 } while (0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002226
2227#define VALIDATE_DESC_VOID(desc) do { \
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002228 int __valid = validate_desc(desc, __func__); \
2229 if (__valid <= 0) \
Linus Walleij54d77192016-05-30 16:48:39 +02002230 return; \
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002231 } while (0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002232
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002233int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002234{
2235 int status = -EPROBE_DEFER;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002236 struct gpio_device *gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002237
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002238 VALIDATE_DESC(desc);
2239 gdev = desc->gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002240
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002241 if (try_module_get(gdev->owner)) {
Linus Walleijfac9d882017-09-26 20:58:28 +02002242 status = gpiod_request_commit(desc, label);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002243 if (status < 0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002244 module_put(gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002245 else
2246 get_device(&gdev->dev);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002247 }
2248
David Brownelld2876d02008-02-04 22:28:20 -08002249 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +02002250 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002251
David Brownelld2876d02008-02-04 22:28:20 -08002252 return status;
2253}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002254
Linus Walleijfac9d882017-09-26 20:58:28 +02002255static bool gpiod_free_commit(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002256{
Mika Westerberg77c2d792014-03-10 14:54:50 +02002257 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08002258 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07002259 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002260
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07002261 might_sleep();
2262
Alexandre Courbot372e7222013-02-03 01:29:29 +09002263 gpiod_unexport(desc);
David Brownelld8f388d82008-07-25 01:46:07 -07002264
David Brownelld2876d02008-02-04 22:28:20 -08002265 spin_lock_irqsave(&gpio_lock, flags);
2266
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002267 chip = desc->gdev->chip;
David Brownell35e8bb52008-10-15 22:03:16 -07002268 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
2269 if (chip->free) {
2270 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07002271 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002272 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07002273 spin_lock_irqsave(&gpio_lock, flags);
2274 }
David Brownelld2876d02008-02-04 22:28:20 -08002275 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08002276 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07002277 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302278 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302279 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06002280 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002281 ret = true;
2282 }
David Brownelld2876d02008-02-04 22:28:20 -08002283
2284 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002285 return ret;
2286}
2287
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002288void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002289{
Linus Walleijfac9d882017-09-26 20:58:28 +02002290 if (desc && desc->gdev && gpiod_free_commit(desc)) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002291 module_put(desc->gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002292 put_device(&desc->gdev->dev);
2293 } else {
Mika Westerberg77c2d792014-03-10 14:54:50 +02002294 WARN_ON(extra_checks);
Linus Walleij33a68e82016-02-11 10:28:44 +01002295 }
David Brownelld2876d02008-02-04 22:28:20 -08002296}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002297
David Brownelld2876d02008-02-04 22:28:20 -08002298/**
2299 * gpiochip_is_requested - return string iff signal was requested
2300 * @chip: controller managing the signal
2301 * @offset: of signal within controller's 0..(ngpio - 1) range
2302 *
2303 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09002304 * The string returned is the label passed to gpio_request(); if none has been
2305 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08002306 *
2307 * This function is for use by GPIO controller drivers. The label can
2308 * help with diagnostics, and knowing that the signal is used as a GPIO
2309 * can help avoid accidentally multiplexing it to another controller.
2310 */
2311const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
2312{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002313 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08002314
Dirk Behme48b59532015-08-18 18:02:32 +02002315 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08002316 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002317
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002318 desc = &chip->gpiodev->descs[offset];
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002319
Alexandre Courbot372e7222013-02-03 01:29:29 +09002320 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08002321 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002322 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08002323}
2324EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2325
Mika Westerberg77c2d792014-03-10 14:54:50 +02002326/**
2327 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
Thierry Reding950d55f52017-07-24 16:57:22 +02002328 * @chip: GPIO chip
2329 * @hwnum: hardware number of the GPIO for which to request the descriptor
Mika Westerberg77c2d792014-03-10 14:54:50 +02002330 * @label: label for the GPIO
2331 *
2332 * Function allows GPIO chip drivers to request and use their own GPIO
2333 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2334 * function will not increase reference count of the GPIO chip module. This
2335 * allows the GPIO chip module to be unloaded as needed (we assume that the
2336 * GPIO chip driver handles freeing the GPIOs it has requested).
Thierry Reding950d55f52017-07-24 16:57:22 +02002337 *
2338 * Returns:
2339 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2340 * code on failure.
Mika Westerberg77c2d792014-03-10 14:54:50 +02002341 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002342struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
2343 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002344{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002345 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
2346 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002347
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002348 if (IS_ERR(desc)) {
2349 chip_err(chip, "failed to get GPIO descriptor\n");
2350 return desc;
2351 }
2352
Linus Walleijfac9d882017-09-26 20:58:28 +02002353 err = gpiod_request_commit(desc, label);
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002354 if (err < 0)
2355 return ERR_PTR(err);
2356
2357 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002358}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002359EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002360
2361/**
2362 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2363 * @desc: GPIO descriptor to free
2364 *
2365 * Function frees the given GPIO requested previously with
2366 * gpiochip_request_own_desc().
2367 */
2368void gpiochip_free_own_desc(struct gpio_desc *desc)
2369{
2370 if (desc)
Linus Walleijfac9d882017-09-26 20:58:28 +02002371 gpiod_free_commit(desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002372}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002373EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08002374
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002375/*
2376 * Drivers MUST set GPIO direction before making get/set calls. In
David Brownelld2876d02008-02-04 22:28:20 -08002377 * some cases this is done in early boot, before IRQs are enabled.
2378 *
2379 * As a rule these aren't called more than once (except for drivers
2380 * using the open-drain emulation idiom) so these are natural places
2381 * to accumulate extra debugging checks. Note that we can't (yet)
2382 * rely on gpio_request() having been called beforehand.
2383 */
2384
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002385/**
2386 * gpiod_direction_input - set the GPIO direction to input
2387 * @desc: GPIO to set to input
2388 *
2389 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2390 * be called safely on it.
2391 *
2392 * Return 0 in case of success, else an error code.
2393 */
2394int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002395{
David Brownelld2876d02008-02-04 22:28:20 -08002396 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002397 int status = -EINVAL;
2398
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002399 VALIDATE_DESC(desc);
2400 chip = desc->gdev->chip;
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002401
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002402 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01002403 gpiod_warn(desc,
2404 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02002405 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002406 return -EIO;
2407 }
2408
Alexandre Courbotd82da792014-07-22 16:17:43 +09002409 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -08002410 if (status == 0)
2411 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002412
Alexandre Courbot372e7222013-02-03 01:29:29 +09002413 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09002414
David Brownelld2876d02008-02-04 22:28:20 -08002415 return status;
2416}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002417EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002418
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002419static int gpio_set_drive_single_ended(struct gpio_chip *gc, unsigned offset,
2420 enum pin_config_param mode)
2421{
2422 unsigned long config = { PIN_CONF_PACKED(mode, 0) };
2423
2424 return gc->set_config ? gc->set_config(gc, offset, config) : -ENOTSUPP;
2425}
2426
Linus Walleijfac9d882017-09-26 20:58:28 +02002427static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08002428{
Linus Walleijc663e5f2016-03-22 10:51:16 +01002429 struct gpio_chip *gc = desc->gdev->chip;
Linus Walleijad177312016-11-13 23:02:44 +01002430 int val = !!value;
Linus Walleijc663e5f2016-03-22 10:51:16 +01002431 int ret;
David Brownelld2876d02008-02-04 22:28:20 -08002432
Linus Walleijc663e5f2016-03-22 10:51:16 +01002433 if (!gc->set || !gc->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01002434 gpiod_warn(desc,
2435 "%s: missing set() or direction_output() operations\n",
2436 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002437 return -EIO;
2438 }
2439
Linus Walleijad177312016-11-13 23:02:44 +01002440 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
Linus Walleijc663e5f2016-03-22 10:51:16 +01002441 if (!ret)
David Brownelld2876d02008-02-04 22:28:20 -08002442 set_bit(FLAG_IS_OUT, &desc->flags);
Linus Walleijad177312016-11-13 23:02:44 +01002443 trace_gpio_value(desc_to_gpio(desc), 0, val);
Linus Walleijc663e5f2016-03-22 10:51:16 +01002444 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2445 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08002446}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002447
2448/**
2449 * gpiod_direction_output_raw - set the GPIO direction to output
2450 * @desc: GPIO to set to output
2451 * @value: initial output value of the GPIO
2452 *
2453 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2454 * be called safely on it. The initial value of the output must be specified
2455 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2456 *
2457 * Return 0 in case of success, else an error code.
2458 */
2459int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2460{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002461 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02002462 return gpiod_direction_output_raw_commit(desc, value);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002463}
2464EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2465
2466/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05302467 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002468 * @desc: GPIO to set to output
2469 * @value: initial output value of the GPIO
2470 *
2471 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2472 * be called safely on it. The initial value of the output must be specified
2473 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2474 * account.
2475 *
2476 * Return 0 in case of success, else an error code.
2477 */
2478int gpiod_direction_output(struct gpio_desc *desc, int value)
2479{
Vladimir Zapolskiy30322bc2017-12-21 18:37:24 +02002480 struct gpio_chip *gc;
Linus Walleij02e47982017-09-26 21:20:23 +02002481 int ret;
2482
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002483 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002484 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2485 value = !value;
Linus Walleijad177312016-11-13 23:02:44 +01002486 else
2487 value = !!value;
Linus Walleij02e47982017-09-26 21:20:23 +02002488
2489 /* GPIOs used for IRQs shall not be set as output */
2490 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
2491 gpiod_err(desc,
2492 "%s: tried to set a GPIO tied to an IRQ as output\n",
2493 __func__);
2494 return -EIO;
2495 }
2496
Vladimir Zapolskiy30322bc2017-12-21 18:37:24 +02002497 gc = desc->gdev->chip;
Linus Walleij02e47982017-09-26 21:20:23 +02002498 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2499 /* First see if we can enable open drain in hardware */
2500 ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2501 PIN_CONFIG_DRIVE_OPEN_DRAIN);
2502 if (!ret)
2503 goto set_output_value;
2504 /* Emulate open drain by not actively driving the line high */
2505 if (value)
2506 return gpiod_direction_input(desc);
2507 }
2508 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2509 ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2510 PIN_CONFIG_DRIVE_OPEN_SOURCE);
2511 if (!ret)
2512 goto set_output_value;
2513 /* Emulate open source by not actively driving the line low */
2514 if (!value)
2515 return gpiod_direction_input(desc);
2516 } else {
2517 gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2518 PIN_CONFIG_DRIVE_PUSH_PULL);
2519 }
2520
2521set_output_value:
Linus Walleijfac9d882017-09-26 20:58:28 +02002522 return gpiod_direction_output_raw_commit(desc, value);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002523}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002524EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08002525
Felipe Balbic4b5be92010-05-26 14:42:23 -07002526/**
Thierry Reding950d55f52017-07-24 16:57:22 +02002527 * gpiod_set_debounce - sets @debounce time for a GPIO
2528 * @desc: descriptor of the GPIO for which to set debounce time
2529 * @debounce: debounce time in microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02002530 *
Thierry Reding950d55f52017-07-24 16:57:22 +02002531 * Returns:
2532 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2533 * debounce time.
Felipe Balbic4b5be92010-05-26 14:42:23 -07002534 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002535int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07002536{
Felipe Balbic4b5be92010-05-26 14:42:23 -07002537 struct gpio_chip *chip;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002538 unsigned long config;
Felipe Balbic4b5be92010-05-26 14:42:23 -07002539
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002540 VALIDATE_DESC(desc);
2541 chip = desc->gdev->chip;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002542 if (!chip->set || !chip->set_config) {
Mark Brown6424de52013-09-09 10:33:49 +01002543 gpiod_dbg(desc,
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002544 "%s: missing set() or set_config() operations\n",
Mark Brown6424de52013-09-09 10:33:49 +01002545 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02002546 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002547 }
2548
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002549 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
2550 return chip->set_config(chip, gpio_chip_hwgpio(desc), config);
Felipe Balbic4b5be92010-05-26 14:42:23 -07002551}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002552EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002553
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002554/**
Andrew Jefferye10f72b2017-11-30 14:25:24 +10302555 * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
2556 * @desc: descriptor of the GPIO for which to configure persistence
2557 * @transitory: True to lose state on suspend or reset, false for persistence
2558 *
2559 * Returns:
2560 * 0 on success, otherwise a negative error code.
2561 */
2562int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
2563{
2564 struct gpio_chip *chip;
2565 unsigned long packed;
2566 int gpio;
2567 int rc;
2568
Vladimir Zapolskiy156dd392017-12-21 18:37:35 +02002569 VALIDATE_DESC(desc);
Andrew Jefferye10f72b2017-11-30 14:25:24 +10302570 /*
2571 * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
2572 * persistence state.
2573 */
2574 if (transitory)
2575 set_bit(FLAG_TRANSITORY, &desc->flags);
2576 else
2577 clear_bit(FLAG_TRANSITORY, &desc->flags);
2578
2579 /* If the driver supports it, set the persistence state now */
2580 chip = desc->gdev->chip;
2581 if (!chip->set_config)
2582 return 0;
2583
2584 packed = pinconf_to_config_packed(PIN_CONFIG_PERSIST_STATE,
2585 !transitory);
2586 gpio = gpio_chip_hwgpio(desc);
2587 rc = chip->set_config(chip, gpio, packed);
2588 if (rc == -ENOTSUPP) {
2589 dev_dbg(&desc->gdev->dev, "Persistence not supported for GPIO %d\n",
2590 gpio);
2591 return 0;
2592 }
2593
2594 return rc;
2595}
2596EXPORT_SYMBOL_GPL(gpiod_set_transitory);
2597
2598/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002599 * gpiod_is_active_low - test whether a GPIO is active-low or not
2600 * @desc: the gpio descriptor to test
2601 *
2602 * Returns 1 if the GPIO is active-low, 0 otherwise.
2603 */
2604int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002605{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002606 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002607 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002608}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002609EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08002610
2611/* I/O calls are only valid after configuration completed; the relevant
2612 * "is this a valid GPIO" error checks should already have been done.
2613 *
2614 * "Get" operations are often inlinable as reading a pin value register,
2615 * and masking the relevant bit in that register.
2616 *
2617 * When "set" operations are inlinable, they involve writing that mask to
2618 * one register to set a low value, or a different register to set it high.
2619 * Otherwise locking is needed, so there may be little value to inlining.
2620 *
2621 *------------------------------------------------------------------------
2622 *
2623 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2624 * have requested the GPIO. That can include implicit requesting by
2625 * a direction setting call. Marking a gpio as requested locks its chip
2626 * in memory, guaranteeing that these table lookups need no more locking
2627 * and that gpiochip_remove() will fail.
2628 *
2629 * REVISIT when debugging, consider adding some instrumentation to ensure
2630 * that the GPIO was actually requested.
2631 */
2632
Linus Walleijfac9d882017-09-26 20:58:28 +02002633static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002634{
2635 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002636 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002637 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002638
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002639 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002640 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002641 value = chip->get ? chip->get(chip, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01002642 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002643 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002644 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002645}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002646
Lukas Wunnereec1d562017-10-12 12:40:10 +02002647static int gpio_chip_get_multiple(struct gpio_chip *chip,
2648 unsigned long *mask, unsigned long *bits)
2649{
2650 if (chip->get_multiple) {
2651 return chip->get_multiple(chip, mask, bits);
2652 } else if (chip->get) {
2653 int i, value;
2654
2655 for_each_set_bit(i, mask, chip->ngpio) {
2656 value = chip->get(chip, i);
2657 if (value < 0)
2658 return value;
2659 __assign_bit(i, bits, value);
2660 }
2661 return 0;
2662 }
2663 return -EIO;
2664}
2665
2666int gpiod_get_array_value_complex(bool raw, bool can_sleep,
2667 unsigned int array_size,
2668 struct gpio_desc **desc_array,
2669 int *value_array)
2670{
2671 int i = 0;
2672
2673 while (i < array_size) {
2674 struct gpio_chip *chip = desc_array[i]->gdev->chip;
2675 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2676 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2677 int first, j, ret;
2678
2679 if (!can_sleep)
2680 WARN_ON(chip->can_sleep);
2681
2682 /* collect all inputs belonging to the same chip */
2683 first = i;
2684 memset(mask, 0, sizeof(mask));
2685 do {
2686 const struct gpio_desc *desc = desc_array[i];
2687 int hwgpio = gpio_chip_hwgpio(desc);
2688
2689 __set_bit(hwgpio, mask);
2690 i++;
2691 } while ((i < array_size) &&
2692 (desc_array[i]->gdev->chip == chip));
2693
2694 ret = gpio_chip_get_multiple(chip, mask, bits);
2695 if (ret)
2696 return ret;
2697
2698 for (j = first; j < i; j++) {
2699 const struct gpio_desc *desc = desc_array[j];
2700 int hwgpio = gpio_chip_hwgpio(desc);
2701 int value = test_bit(hwgpio, bits);
2702
2703 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2704 value = !value;
2705 value_array[j] = value;
2706 trace_gpio_value(desc_to_gpio(desc), 1, value);
2707 }
2708 }
2709 return 0;
2710}
2711
David Brownelld2876d02008-02-04 22:28:20 -08002712/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002713 * gpiod_get_raw_value() - return a gpio's raw value
2714 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08002715 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002716 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002717 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002718 *
2719 * This function should be called from contexts where we cannot sleep, and will
2720 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002721 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002722int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002723{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002724 VALIDATE_DESC(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002725 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002726 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02002727 return gpiod_get_raw_value_commit(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002728}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002729EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002730
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002731/**
2732 * gpiod_get_value() - return a gpio's value
2733 * @desc: gpio whose value will be returned
2734 *
2735 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002736 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002737 *
2738 * This function should be called from contexts where we cannot sleep, and will
2739 * complain if the GPIO chip functions potentially sleep.
2740 */
2741int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002742{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002743 int value;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002744
2745 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002746 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002747 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002748
Linus Walleijfac9d882017-09-26 20:58:28 +02002749 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002750 if (value < 0)
2751 return value;
2752
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002753 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2754 value = !value;
2755
2756 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002757}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002758EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08002759
Lukas Wunnereec1d562017-10-12 12:40:10 +02002760/**
2761 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
2762 * @array_size: number of elements in the descriptor / value arrays
2763 * @desc_array: array of GPIO descriptors whose values will be read
2764 * @value_array: array to store the read values
2765 *
2766 * Read the raw values of the GPIOs, i.e. the values of the physical lines
2767 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
2768 * else an error code.
2769 *
2770 * This function should be called from contexts where we cannot sleep,
2771 * and it will complain if the GPIO chip functions potentially sleep.
2772 */
2773int gpiod_get_raw_array_value(unsigned int array_size,
2774 struct gpio_desc **desc_array, int *value_array)
2775{
2776 if (!desc_array)
2777 return -EINVAL;
2778 return gpiod_get_array_value_complex(true, false, array_size,
2779 desc_array, value_array);
2780}
2781EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
2782
2783/**
2784 * gpiod_get_array_value() - read values from an array of GPIOs
2785 * @array_size: number of elements in the descriptor / value arrays
2786 * @desc_array: array of GPIO descriptors whose values will be read
2787 * @value_array: array to store the read values
2788 *
2789 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2790 * into account. Return 0 in case of success, else an error code.
2791 *
2792 * This function should be called from contexts where we cannot sleep,
2793 * and it will complain if the GPIO chip functions potentially sleep.
2794 */
2795int gpiod_get_array_value(unsigned int array_size,
2796 struct gpio_desc **desc_array, int *value_array)
2797{
2798 if (!desc_array)
2799 return -EINVAL;
2800 return gpiod_get_array_value_complex(false, false, array_size,
2801 desc_array, value_array);
2802}
2803EXPORT_SYMBOL_GPL(gpiod_get_array_value);
2804
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302805/*
Linus Walleijfac9d882017-09-26 20:58:28 +02002806 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002807 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002808 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302809 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002810static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302811{
2812 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002813 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002814 int offset = gpio_chip_hwgpio(desc);
2815
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302816 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002817 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302818 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002819 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302820 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002821 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302822 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002823 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302824 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002825 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302826 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002827 gpiod_err(desc,
2828 "%s: Error in set_value for open drain err %d\n",
2829 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302830}
2831
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302832/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002833 * _gpio_set_open_source_value() - Set the open source gpio's value.
2834 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002835 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302836 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002837static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302838{
2839 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002840 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002841 int offset = gpio_chip_hwgpio(desc);
2842
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302843 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002844 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302845 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002846 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302847 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002848 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302849 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002850 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302851 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002852 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302853 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002854 gpiod_err(desc,
2855 "%s: Error in set_value for open source err %d\n",
2856 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302857}
2858
Linus Walleijfac9d882017-09-26 20:58:28 +02002859static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08002860{
2861 struct gpio_chip *chip;
2862
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002863 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002864 trace_gpio_value(desc_to_gpio(desc), 0, value);
Linus Walleij02e47982017-09-26 21:20:23 +02002865 chip->set(chip, gpio_chip_hwgpio(desc), value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002866}
2867
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002868/*
2869 * set multiple outputs on the same chip;
2870 * use the chip's set_multiple function if available;
2871 * otherwise set the outputs sequentially;
2872 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
2873 * defines which outputs are to be changed
2874 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
2875 * defines the values the outputs specified by mask are to be set to
2876 */
2877static void gpio_chip_set_multiple(struct gpio_chip *chip,
2878 unsigned long *mask, unsigned long *bits)
2879{
2880 if (chip->set_multiple) {
2881 chip->set_multiple(chip, mask, bits);
2882 } else {
Andy Shevchenko5e4e6fb2017-01-03 19:01:17 +02002883 unsigned int i;
2884
2885 /* set outputs if the corresponding mask bit is set */
2886 for_each_set_bit(i, mask, chip->ngpio)
2887 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002888 }
2889}
2890
Linus Walleij44c72882016-04-24 11:36:59 +02002891void gpiod_set_array_value_complex(bool raw, bool can_sleep,
2892 unsigned int array_size,
2893 struct gpio_desc **desc_array,
2894 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002895{
2896 int i = 0;
2897
2898 while (i < array_size) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002899 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002900 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2901 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2902 int count = 0;
2903
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002904 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002905 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002906
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002907 memset(mask, 0, sizeof(mask));
2908 do {
2909 struct gpio_desc *desc = desc_array[i];
2910 int hwgpio = gpio_chip_hwgpio(desc);
2911 int value = value_array[i];
2912
2913 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2914 value = !value;
2915 trace_gpio_value(desc_to_gpio(desc), 0, value);
2916 /*
2917 * collect all normal outputs belonging to the same chip
2918 * open drain and open source outputs are set individually
2919 */
Linus Walleij02e47982017-09-26 21:20:23 +02002920 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02002921 gpio_set_open_drain_value_commit(desc, value);
Linus Walleij02e47982017-09-26 21:20:23 +02002922 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02002923 gpio_set_open_source_value_commit(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002924 } else {
2925 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002926 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002927 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002928 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002929 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002930 count++;
2931 }
2932 i++;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002933 } while ((i < array_size) &&
2934 (desc_array[i]->gdev->chip == chip));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002935 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002936 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002937 gpio_chip_set_multiple(chip, mask, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002938 }
2939}
2940
David Brownelld2876d02008-02-04 22:28:20 -08002941/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002942 * gpiod_set_raw_value() - assign a gpio's raw value
2943 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08002944 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002945 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002946 * Set the raw value of the GPIO, i.e. the value of its physical line without
2947 * regard for its ACTIVE_LOW status.
2948 *
2949 * This function should be called from contexts where we cannot sleep, and will
2950 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002951 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002952void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002953{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002954 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01002955 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002956 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02002957 gpiod_set_raw_value_commit(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002958}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002959EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002960
2961/**
Geert Uytterhoeven1e77fc82018-01-09 19:08:21 +01002962 * gpiod_set_value_nocheck() - set a GPIO line value without checking
2963 * @desc: the descriptor to set the value on
2964 * @value: value to set
2965 *
2966 * This sets the value of a GPIO line backing a descriptor, applying
2967 * different semantic quirks like active low and open drain/source
2968 * handling.
2969 */
2970static void gpiod_set_value_nocheck(struct gpio_desc *desc, int value)
2971{
2972 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2973 value = !value;
2974 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2975 gpio_set_open_drain_value_commit(desc, value);
2976 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2977 gpio_set_open_source_value_commit(desc, value);
2978 else
2979 gpiod_set_raw_value_commit(desc, value);
2980}
2981
2982/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002983 * gpiod_set_value() - assign a gpio's value
2984 * @desc: gpio whose value will be assigned
2985 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002986 *
Linus Walleij02e47982017-09-26 21:20:23 +02002987 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
2988 * OPEN_DRAIN and OPEN_SOURCE flags into account.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002989 *
2990 * This function should be called from contexts where we cannot sleep, and will
2991 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002992 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002993void gpiod_set_value(struct gpio_desc *desc, int value)
2994{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002995 VALIDATE_DESC_VOID(desc);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002996 WARN_ON(desc->gdev->chip->can_sleep);
Geert Uytterhoeven1e77fc82018-01-09 19:08:21 +01002997 gpiod_set_value_nocheck(desc, value);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002998}
2999EXPORT_SYMBOL_GPL(gpiod_set_value);
3000
3001/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003002 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003003 * @array_size: number of elements in the descriptor / value arrays
3004 * @desc_array: array of GPIO descriptors whose values will be assigned
3005 * @value_array: array of values to assign
3006 *
3007 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3008 * without regard for their ACTIVE_LOW status.
3009 *
3010 * This function should be called from contexts where we cannot sleep, and will
3011 * complain if the GPIO chip functions potentially sleep.
3012 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003013void gpiod_set_raw_array_value(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003014 struct gpio_desc **desc_array, int *value_array)
3015{
3016 if (!desc_array)
3017 return;
Linus Walleij44c72882016-04-24 11:36:59 +02003018 gpiod_set_array_value_complex(true, false, array_size, desc_array,
3019 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003020}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003021EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003022
3023/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003024 * gpiod_set_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003025 * @array_size: number of elements in the descriptor / value arrays
3026 * @desc_array: array of GPIO descriptors whose values will be assigned
3027 * @value_array: array of values to assign
3028 *
3029 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3030 * into account.
3031 *
3032 * This function should be called from contexts where we cannot sleep, and will
3033 * complain if the GPIO chip functions potentially sleep.
3034 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003035void gpiod_set_array_value(unsigned int array_size,
3036 struct gpio_desc **desc_array, int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003037{
3038 if (!desc_array)
3039 return;
Linus Walleij44c72882016-04-24 11:36:59 +02003040 gpiod_set_array_value_complex(false, false, array_size, desc_array,
3041 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003042}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003043EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003044
3045/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003046 * gpiod_cansleep() - report whether gpio value access may sleep
3047 * @desc: gpio to check
3048 *
3049 */
3050int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003051{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003052 VALIDATE_DESC(desc);
3053 return desc->gdev->chip->can_sleep;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003054}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003055EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08003056
David Brownell0f6d5042008-10-15 22:03:14 -07003057/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003058 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
3059 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07003060 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003061 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
3062 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07003063 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003064int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07003065{
Linus Walleij4c37ce82016-05-02 13:13:10 +02003066 struct gpio_chip *chip;
3067 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07003068
Linus Walleij79bb71b2016-06-15 22:57:38 +02003069 /*
3070 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
3071 * requires this function to not return zero on an invalid descriptor
3072 * but rather a negative error number.
3073 */
Linus Walleijbfbbe442016-06-16 11:55:55 +02003074 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
Linus Walleij79bb71b2016-06-15 22:57:38 +02003075 return -EINVAL;
3076
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003077 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003078 offset = gpio_chip_hwgpio(desc);
Linus Walleij4c37ce82016-05-02 13:13:10 +02003079 if (chip->to_irq) {
3080 int retirq = chip->to_irq(chip, offset);
3081
3082 /* Zero means NO_IRQ */
3083 if (!retirq)
3084 return -ENXIO;
3085
3086 return retirq;
3087 }
3088 return -ENXIO;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003089}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003090EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003091
Linus Walleijd468bf92013-09-24 11:54:38 +02003092/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003093 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003094 * @chip: the chip the GPIO to lock belongs to
3095 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02003096 *
3097 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08003098 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08003099 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003100int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08003101{
Linus Walleij9c102802016-05-25 10:56:03 +02003102 struct gpio_desc *desc;
Linus Walleijd468bf92013-09-24 11:54:38 +02003103
Linus Walleij9c102802016-05-25 10:56:03 +02003104 desc = gpiochip_get_desc(chip, offset);
3105 if (IS_ERR(desc))
3106 return PTR_ERR(desc);
3107
Linus Walleij60f83392016-11-12 15:01:09 +01003108 /*
3109 * If it's fast: flush the direction setting if something changed
3110 * behind our back
3111 */
3112 if (!chip->can_sleep && chip->get_direction) {
Linus Walleij9c102802016-05-25 10:56:03 +02003113 int dir = chip->get_direction(chip, offset);
3114
3115 if (dir)
3116 clear_bit(FLAG_IS_OUT, &desc->flags);
3117 else
3118 set_bit(FLAG_IS_OUT, &desc->flags);
3119 }
3120
3121 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003122 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02003123 "%s: tried to flag a GPIO set as output for IRQ\n",
3124 __func__);
3125 return -EIO;
3126 }
3127
Linus Walleij9c102802016-05-25 10:56:03 +02003128 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
Linus Walleij3940c342016-11-14 00:09:07 +01003129
3130 /*
3131 * If the consumer has not set up a label (such as when the
3132 * IRQ is referenced from .to_irq()) we set up a label here
3133 * so it is clear this is used as an interrupt.
3134 */
3135 if (!desc->label)
3136 desc_set_label(desc, "interrupt");
3137
Linus Walleijd468bf92013-09-24 11:54:38 +02003138 return 0;
3139}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003140EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02003141
Linus Walleijd468bf92013-09-24 11:54:38 +02003142/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003143 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003144 * @chip: the chip the GPIO to lock belongs to
3145 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02003146 *
3147 * This is used directly by GPIO drivers that want to indicate
3148 * that a certain GPIO is no longer used exclusively for IRQ.
3149 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003150void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02003151{
Linus Walleij3940c342016-11-14 00:09:07 +01003152 struct gpio_desc *desc;
3153
3154 desc = gpiochip_get_desc(chip, offset);
3155 if (IS_ERR(desc))
Linus Walleijd468bf92013-09-24 11:54:38 +02003156 return;
3157
Linus Walleij3940c342016-11-14 00:09:07 +01003158 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
3159
3160 /* If we only had this marking, erase it */
3161 if (desc->label && !strcmp(desc->label, "interrupt"))
3162 desc_set_label(desc, NULL);
Linus Walleijd468bf92013-09-24 11:54:38 +02003163}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003164EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02003165
Linus Walleij6cee3822016-02-11 20:16:45 +01003166bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
3167{
3168 if (offset >= chip->ngpio)
3169 return false;
3170
3171 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
3172}
3173EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
3174
Linus Walleij143b65d2016-02-16 15:41:42 +01003175bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
3176{
3177 if (offset >= chip->ngpio)
3178 return false;
3179
3180 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
3181}
3182EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
3183
3184bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
3185{
3186 if (offset >= chip->ngpio)
3187 return false;
3188
3189 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
3190}
3191EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
3192
Charles Keepax05f479b2017-05-23 15:47:29 +01003193bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset)
3194{
3195 if (offset >= chip->ngpio)
3196 return false;
3197
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303198 return !test_bit(FLAG_TRANSITORY, &chip->gpiodev->descs[offset].flags);
Charles Keepax05f479b2017-05-23 15:47:29 +01003199}
3200EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
3201
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003202/**
3203 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
3204 * @desc: gpio whose value will be returned
3205 *
3206 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003207 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003208 *
3209 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003210 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003211int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08003212{
David Brownelld2876d02008-02-04 22:28:20 -08003213 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003214 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003215 return gpiod_get_raw_value_commit(desc);
David Brownelld2876d02008-02-04 22:28:20 -08003216}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003217EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003218
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003219/**
3220 * gpiod_get_value_cansleep() - return a gpio's value
3221 * @desc: gpio whose value will be returned
3222 *
3223 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003224 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003225 *
3226 * This function is to be called from contexts that can sleep.
3227 */
3228int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003229{
David Brownelld2876d02008-02-04 22:28:20 -08003230 int value;
David Brownelld2876d02008-02-04 22:28:20 -08003231
3232 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003233 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003234 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003235 if (value < 0)
3236 return value;
3237
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003238 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3239 value = !value;
3240
David Brownelld2876d02008-02-04 22:28:20 -08003241 return value;
3242}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003243EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003244
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003245/**
Lukas Wunnereec1d562017-10-12 12:40:10 +02003246 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
3247 * @array_size: number of elements in the descriptor / value arrays
3248 * @desc_array: array of GPIO descriptors whose values will be read
3249 * @value_array: array to store the read values
3250 *
3251 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3252 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3253 * else an error code.
3254 *
3255 * This function is to be called from contexts that can sleep.
3256 */
3257int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
3258 struct gpio_desc **desc_array,
3259 int *value_array)
3260{
3261 might_sleep_if(extra_checks);
3262 if (!desc_array)
3263 return -EINVAL;
3264 return gpiod_get_array_value_complex(true, true, array_size,
3265 desc_array, value_array);
3266}
3267EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
3268
3269/**
3270 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
3271 * @array_size: number of elements in the descriptor / value arrays
3272 * @desc_array: array of GPIO descriptors whose values will be read
3273 * @value_array: array to store the read values
3274 *
3275 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3276 * into account. Return 0 in case of success, else an error code.
3277 *
3278 * This function is to be called from contexts that can sleep.
3279 */
3280int gpiod_get_array_value_cansleep(unsigned int array_size,
3281 struct gpio_desc **desc_array,
3282 int *value_array)
3283{
3284 might_sleep_if(extra_checks);
3285 if (!desc_array)
3286 return -EINVAL;
3287 return gpiod_get_array_value_complex(false, true, array_size,
3288 desc_array, value_array);
3289}
3290EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
3291
3292/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003293 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
3294 * @desc: gpio whose value will be assigned
3295 * @value: value to assign
3296 *
3297 * Set the raw value of the GPIO, i.e. the value of its physical line without
3298 * regard for its ACTIVE_LOW status.
3299 *
3300 * This function is to be called from contexts that can sleep.
3301 */
3302void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003303{
David Brownelld2876d02008-02-04 22:28:20 -08003304 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003305 VALIDATE_DESC_VOID(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003306 gpiod_set_raw_value_commit(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003307}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003308EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003309
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003310/**
3311 * gpiod_set_value_cansleep() - assign a gpio's value
3312 * @desc: gpio whose value will be assigned
3313 * @value: value to assign
3314 *
3315 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3316 * account
3317 *
3318 * This function is to be called from contexts that can sleep.
3319 */
3320void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003321{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003322 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003323 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1e77fc82018-01-09 19:08:21 +01003324 gpiod_set_value_nocheck(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08003325}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003326EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08003327
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003328/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003329 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003330 * @array_size: number of elements in the descriptor / value arrays
3331 * @desc_array: array of GPIO descriptors whose values will be assigned
3332 * @value_array: array of values to assign
3333 *
3334 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3335 * without regard for their ACTIVE_LOW status.
3336 *
3337 * This function is to be called from contexts that can sleep.
3338 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003339void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
3340 struct gpio_desc **desc_array,
3341 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003342{
3343 might_sleep_if(extra_checks);
3344 if (!desc_array)
3345 return;
Linus Walleij44c72882016-04-24 11:36:59 +02003346 gpiod_set_array_value_complex(true, true, array_size, desc_array,
3347 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003348}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003349EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003350
3351/**
Dmitry Torokhov3946d182017-08-14 21:59:55 -07003352 * gpiod_add_lookup_tables() - register GPIO device consumers
3353 * @tables: list of tables of consumers to register
3354 * @n: number of tables in the list
3355 */
3356void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
3357{
3358 unsigned int i;
3359
3360 mutex_lock(&gpio_lookup_lock);
3361
3362 for (i = 0; i < n; i++)
3363 list_add_tail(&tables[i]->list, &gpio_lookup_list);
3364
3365 mutex_unlock(&gpio_lookup_lock);
3366}
3367
3368/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003369 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003370 * @array_size: number of elements in the descriptor / value arrays
3371 * @desc_array: array of GPIO descriptors whose values will be assigned
3372 * @value_array: array of values to assign
3373 *
3374 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3375 * into account.
3376 *
3377 * This function is to be called from contexts that can sleep.
3378 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003379void gpiod_set_array_value_cansleep(unsigned int array_size,
3380 struct gpio_desc **desc_array,
3381 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003382{
3383 might_sleep_if(extra_checks);
3384 if (!desc_array)
3385 return;
Linus Walleij44c72882016-04-24 11:36:59 +02003386 gpiod_set_array_value_complex(false, true, array_size, desc_array,
3387 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003388}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003389EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003390
3391/**
Alexandre Courbotad824782013-12-03 12:20:11 +09003392 * gpiod_add_lookup_table() - register GPIO device consumers
3393 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003394 */
Alexandre Courbotad824782013-12-03 12:20:11 +09003395void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003396{
3397 mutex_lock(&gpio_lookup_lock);
3398
Alexandre Courbotad824782013-12-03 12:20:11 +09003399 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003400
3401 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08003402}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02003403EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
David Brownelld2876d02008-02-04 22:28:20 -08003404
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05303405/**
3406 * gpiod_remove_lookup_table() - unregister GPIO device consumers
3407 * @table: table of consumers to unregister
3408 */
3409void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
3410{
3411 mutex_lock(&gpio_lookup_lock);
3412
3413 list_del(&table->list);
3414
3415 mutex_unlock(&gpio_lookup_lock);
3416}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02003417EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05303418
Alexandre Courbotad824782013-12-03 12:20:11 +09003419static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
3420{
3421 const char *dev_id = dev ? dev_name(dev) : NULL;
3422 struct gpiod_lookup_table *table;
3423
3424 mutex_lock(&gpio_lookup_lock);
3425
3426 list_for_each_entry(table, &gpio_lookup_list, list) {
3427 if (table->dev_id && dev_id) {
3428 /*
3429 * Valid strings on both ends, must be identical to have
3430 * a match
3431 */
3432 if (!strcmp(table->dev_id, dev_id))
3433 goto found;
3434 } else {
3435 /*
3436 * One of the pointers is NULL, so both must be to have
3437 * a match
3438 */
3439 if (dev_id == table->dev_id)
3440 goto found;
3441 }
3442 }
3443 table = NULL;
3444
3445found:
3446 mutex_unlock(&gpio_lookup_lock);
3447 return table;
3448}
3449
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003450static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09003451 unsigned int idx,
3452 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003453{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003454 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09003455 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003456 struct gpiod_lookup *p;
3457
Alexandre Courbotad824782013-12-03 12:20:11 +09003458 table = gpiod_find_lookup_table(dev);
3459 if (!table)
3460 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003461
Alexandre Courbotad824782013-12-03 12:20:11 +09003462 for (p = &table->table[0]; p->chip_label; p++) {
3463 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003464
Alexandre Courbotad824782013-12-03 12:20:11 +09003465 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003466 if (p->idx != idx)
3467 continue;
3468
Alexandre Courbotad824782013-12-03 12:20:11 +09003469 /* If the lookup entry has a con_id, require exact match */
3470 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
3471 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003472
Alexandre Courbotad824782013-12-03 12:20:11 +09003473 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003474
Alexandre Courbotad824782013-12-03 12:20:11 +09003475 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003476 dev_err(dev, "cannot find GPIO chip %s\n",
3477 p->chip_label);
3478 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003479 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003480
Alexandre Courbotad824782013-12-03 12:20:11 +09003481 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003482 dev_err(dev,
3483 "requested GPIO %d is out of range [0..%d] for chip %s\n",
3484 idx, chip->ngpio, chip->label);
3485 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09003486 }
3487
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09003488 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09003489 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003490
3491 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09003492 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003493
3494 return desc;
3495}
3496
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003497static int dt_gpio_count(struct device *dev, const char *con_id)
3498{
3499 int ret;
3500 char propname[32];
3501 unsigned int i;
3502
3503 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
3504 if (con_id)
3505 snprintf(propname, sizeof(propname), "%s-%s",
3506 con_id, gpio_suffixes[i]);
3507 else
3508 snprintf(propname, sizeof(propname), "%s",
3509 gpio_suffixes[i]);
3510
3511 ret = of_gpio_named_count(dev->of_node, propname);
Andy Shevchenko4033d4a2017-02-20 18:15:47 +02003512 if (ret > 0)
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003513 break;
3514 }
Andy Shevchenko4033d4a2017-02-20 18:15:47 +02003515 return ret ? ret : -ENOENT;
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003516}
3517
3518static int platform_gpio_count(struct device *dev, const char *con_id)
3519{
3520 struct gpiod_lookup_table *table;
3521 struct gpiod_lookup *p;
3522 unsigned int count = 0;
3523
3524 table = gpiod_find_lookup_table(dev);
3525 if (!table)
3526 return -ENOENT;
3527
3528 for (p = &table->table[0]; p->chip_label; p++) {
3529 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3530 (!con_id && !p->con_id))
3531 count++;
3532 }
3533 if (!count)
3534 return -ENOENT;
3535
3536 return count;
3537}
3538
3539/**
3540 * gpiod_count - return the number of GPIOs associated with a device / function
3541 * or -ENOENT if no GPIO has been assigned to the requested function
3542 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3543 * @con_id: function within the GPIO consumer
3544 */
3545int gpiod_count(struct device *dev, const char *con_id)
3546{
3547 int count = -ENOENT;
3548
3549 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
3550 count = dt_gpio_count(dev, con_id);
3551 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
3552 count = acpi_gpio_count(dev, con_id);
3553
3554 if (count < 0)
3555 count = platform_gpio_count(dev, con_id);
3556
3557 return count;
3558}
3559EXPORT_SYMBOL_GPL(gpiod_count);
3560
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003561/**
Thierry Reding08791622014-04-25 16:54:22 +02003562 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09003563 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003564 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003565 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003566 *
3567 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003568 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07003569 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003570 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003571struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003572 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003573{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003574 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003575}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003576EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003577
3578/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02003579 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
3580 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3581 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003582 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02003583 *
3584 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
3585 * the requested function it will return NULL. This is convenient for drivers
3586 * that need to handle optional GPIOs.
3587 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003588struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003589 const char *con_id,
3590 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003591{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003592 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003593}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003594EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003595
Benoit Parrotf625d462015-02-02 11:44:44 -06003596
3597/**
3598 * gpiod_configure_flags - helper function to configure a given GPIO
3599 * @desc: gpio whose value will be assigned
3600 * @con_id: function within the GPIO consumer
Johan Hovold85b03b32016-07-03 18:32:05 +02003601 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3602 * of_get_gpio_hog()
Benoit Parrotf625d462015-02-02 11:44:44 -06003603 * @dflags: gpiod_flags - optional GPIO initialization flags
3604 *
3605 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
3606 * requested function and/or index, or another IS_ERR() code if an error
3607 * occurred while trying to acquire the GPIO.
3608 */
Andy Shevchenkoc29fd9e2017-05-23 20:03:16 +03003609int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Johan Hovold85b03b32016-07-03 18:32:05 +02003610 unsigned long lflags, enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06003611{
3612 int status;
3613
Johan Hovold85b03b32016-07-03 18:32:05 +02003614 if (lflags & GPIO_ACTIVE_LOW)
3615 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
Linus Walleijf926dfc2017-09-10 19:26:22 +02003616
Johan Hovold85b03b32016-07-03 18:32:05 +02003617 if (lflags & GPIO_OPEN_DRAIN)
3618 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
Linus Walleijf926dfc2017-09-10 19:26:22 +02003619 else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) {
3620 /*
3621 * This enforces open drain mode from the consumer side.
3622 * This is necessary for some busses like I2C, but the lookup
3623 * should *REALLY* have specified them as open drain in the
3624 * first place, so print a little warning here.
3625 */
3626 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3627 gpiod_warn(desc,
3628 "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
3629 }
3630
Johan Hovold85b03b32016-07-03 18:32:05 +02003631 if (lflags & GPIO_OPEN_SOURCE)
3632 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303633
3634 status = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
3635 if (status < 0)
3636 return status;
Johan Hovold85b03b32016-07-03 18:32:05 +02003637
Benoit Parrotf625d462015-02-02 11:44:44 -06003638 /* No particular flag request, return here... */
3639 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
3640 pr_debug("no flags found for %s\n", con_id);
3641 return 0;
3642 }
3643
3644 /* Process flags */
3645 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
3646 status = gpiod_direction_output(desc,
Linus Walleijad177312016-11-13 23:02:44 +01003647 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
Benoit Parrotf625d462015-02-02 11:44:44 -06003648 else
3649 status = gpiod_direction_input(desc);
3650
3651 return status;
3652}
3653
Thierry Reding29a1f2332014-04-25 17:10:06 +02003654/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003655 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02003656 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003657 * @con_id: function within the GPIO consumer
3658 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003659 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003660 *
3661 * This variant of gpiod_get() allows to access GPIOs other than the first
3662 * defined one for functions that define several GPIOs.
3663 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003664 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
3665 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07003666 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003667 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003668struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003669 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003670 unsigned int idx,
3671 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003672{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003673 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003674 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003675 enum gpio_lookup_flags lookupflags = 0;
Linus Walleij7d18f0a2018-01-16 08:29:50 +01003676 /* Maybe we have a device name, maybe not */
3677 const char *devname = dev ? dev_name(dev) : "?";
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003678
3679 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
3680
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003681 if (dev) {
3682 /* Using device tree? */
3683 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
3684 dev_dbg(dev, "using device tree for GPIO lookup\n");
3685 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
3686 } else if (ACPI_COMPANION(dev)) {
3687 dev_dbg(dev, "using ACPI for GPIO lookup\n");
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +03003688 desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags);
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003689 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003690 }
3691
3692 /*
3693 * Either we are not using DT or ACPI, or their lookup did not return
3694 * a result. In that case, use platform lookup as a fallback.
3695 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003696 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04003697 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003698 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003699 }
3700
3701 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02003702 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003703 return desc;
3704 }
3705
Linus Walleij7d18f0a2018-01-16 08:29:50 +01003706 /*
3707 * If a connection label was passed use that, else attempt to use
3708 * the device name as label
3709 */
3710 status = gpiod_request(desc, con_id ? con_id : devname);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003711 if (status < 0)
3712 return ERR_PTR(status);
3713
Johan Hovold85b03b32016-07-03 18:32:05 +02003714 status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003715 if (status < 0) {
3716 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
3717 gpiod_put(desc);
3718 return ERR_PTR(status);
3719 }
3720
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003721 return desc;
3722}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003723EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003724
3725/**
Linus Walleij6392cca2017-12-29 02:07:54 +01003726 * gpiod_get_from_of_node() - obtain a GPIO from an OF node
3727 * @node: handle of the OF node
3728 * @propname: name of the DT property representing the GPIO
3729 * @index: index of the GPIO to obtain for the consumer
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003730 * @dflags: GPIO initialization flags
Thierry Reding950d55f52017-07-24 16:57:22 +02003731 * @label: label to attach to the requested GPIO
Mika Westerberg40b73182014-10-21 13:33:59 +02003732 *
Thierry Reding950d55f52017-07-24 16:57:22 +02003733 * Returns:
Andy Shevchenkoff213782017-02-28 17:03:12 +02003734 * On successful request the GPIO pin is configured in accordance with
Linus Walleij6392cca2017-12-29 02:07:54 +01003735 * provided @dflags. If the node does not have the requested GPIO
3736 * property, NULL is returned.
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003737 *
Mika Westerberg40b73182014-10-21 13:33:59 +02003738 * In case of error an ERR_PTR() is returned.
3739 */
Linus Walleij92542ed2017-12-29 22:52:02 +01003740struct gpio_desc *gpiod_get_from_of_node(struct device_node *node,
3741 const char *propname, int index,
3742 enum gpiod_flags dflags,
3743 const char *label)
Mika Westerberg40b73182014-10-21 13:33:59 +02003744{
Colin Ian King40a3c9d2018-01-16 11:56:11 +00003745 struct gpio_desc *desc;
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003746 unsigned long lflags = 0;
Linus Walleij6392cca2017-12-29 02:07:54 +01003747 enum of_gpio_flags flags;
Mika Westerberg40b73182014-10-21 13:33:59 +02003748 bool active_low = false;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003749 bool single_ended = false;
Laxman Dewangan4c0facd2017-04-06 19:05:52 +05303750 bool open_drain = false;
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303751 bool transitory = false;
Mika Westerberg40b73182014-10-21 13:33:59 +02003752 int ret;
3753
Linus Walleij6392cca2017-12-29 02:07:54 +01003754 desc = of_get_named_gpiod_flags(node, propname,
3755 index, &flags);
Mika Westerberg40b73182014-10-21 13:33:59 +02003756
Linus Walleij6392cca2017-12-29 02:07:54 +01003757 if (!desc || IS_ERR(desc)) {
3758 /* If it is not there, just return NULL */
3759 if (PTR_ERR(desc) == -ENOENT)
3760 return NULL;
3761 return desc;
Mika Westerberg40b73182014-10-21 13:33:59 +02003762 }
3763
Linus Walleij6392cca2017-12-29 02:07:54 +01003764 active_low = flags & OF_GPIO_ACTIVE_LOW;
3765 single_ended = flags & OF_GPIO_SINGLE_ENDED;
3766 open_drain = flags & OF_GPIO_OPEN_DRAIN;
3767 transitory = flags & OF_GPIO_TRANSITORY;
Mika Westerberg40b73182014-10-21 13:33:59 +02003768
Alexander Steinb2987d72017-01-12 17:39:24 +01003769 ret = gpiod_request(desc, label);
Johan Hovold85b03b32016-07-03 18:32:05 +02003770 if (ret)
3771 return ERR_PTR(ret);
3772
Mika Westerberg40b73182014-10-21 13:33:59 +02003773 if (active_low)
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003774 lflags |= GPIO_ACTIVE_LOW;
Mika Westerberg40b73182014-10-21 13:33:59 +02003775
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003776 if (single_ended) {
Laxman Dewangan4c0facd2017-04-06 19:05:52 +05303777 if (open_drain)
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003778 lflags |= GPIO_OPEN_DRAIN;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003779 else
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003780 lflags |= GPIO_OPEN_SOURCE;
3781 }
3782
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303783 if (transitory)
3784 lflags |= GPIO_TRANSITORY;
3785
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003786 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
3787 if (ret < 0) {
3788 gpiod_put(desc);
3789 return ERR_PTR(ret);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003790 }
3791
Mika Westerberg40b73182014-10-21 13:33:59 +02003792 return desc;
3793}
Linus Walleij92542ed2017-12-29 22:52:02 +01003794EXPORT_SYMBOL(gpiod_get_from_of_node);
Linus Walleij6392cca2017-12-29 02:07:54 +01003795
3796/**
Mika Westerberg40b73182014-10-21 13:33:59 +02003797 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
3798 * @fwnode: handle of the firmware node
3799 * @propname: name of the firmware property representing the GPIO
Linus Walleij6392cca2017-12-29 02:07:54 +01003800 * @index: index of the GPIO to obtain for the consumer
Mika Westerberg40b73182014-10-21 13:33:59 +02003801 * @dflags: GPIO initialization flags
3802 * @label: label to attach to the requested GPIO
3803 *
3804 * This function can be used for drivers that get their configuration
Linus Walleij6392cca2017-12-29 02:07:54 +01003805 * from opaque firmware.
Thierry Reding29a1f2332014-04-25 17:10:06 +02003806 *
Linus Walleij6392cca2017-12-29 02:07:54 +01003807 * The function properly finds the corresponding GPIO using whatever is the
Thierry Reding29a1f2332014-04-25 17:10:06 +02003808 * underlying firmware interface and then makes sure that the GPIO
3809 * descriptor is requested before it is returned to the caller.
3810 *
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003811 * Returns:
Thierry Reding29a1f2332014-04-25 17:10:06 +02003812 * On successful request the GPIO pin is configured in accordance with
3813 * provided @dflags.
3814 *
3815 * In case of error an ERR_PTR() is returned.
3816 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003817struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
Thierry Reding29a1f2332014-04-25 17:10:06 +02003818 const char *propname, int index,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003819 enum gpiod_flags dflags,
3820 const char *label)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003821{
3822 struct gpio_desc *desc = ERR_PTR(-ENODEV);
3823 unsigned long lflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003824 int ret;
3825
David Brownelld2876d02008-02-04 22:28:20 -08003826 if (!fwnode)
David Brownelld2876d02008-02-04 22:28:20 -08003827 return ERR_PTR(-EINVAL);
3828
3829 if (is_of_node(fwnode)) {
Linus Walleij6392cca2017-12-29 02:07:54 +01003830 desc = gpiod_get_from_of_node(to_of_node(fwnode),
3831 propname, index,
3832 dflags,
3833 label);
3834 return desc;
David Brownelld2876d02008-02-04 22:28:20 -08003835 } else if (is_acpi_node(fwnode)) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09003836 struct acpi_gpio_info info;
David Brownelld2876d02008-02-04 22:28:20 -08003837
Linus Walleijd468bf92013-09-24 11:54:38 +02003838 desc = acpi_node_get_gpiod(fwnode, propname, index, &info);
Linus Walleij6392cca2017-12-29 02:07:54 +01003839 if (IS_ERR(desc))
3840 return desc;
3841
3842 acpi_gpio_update_gpiod_flags(&dflags, &info);
3843
3844 if (info.polarity == GPIO_ACTIVE_LOW)
3845 lflags |= GPIO_ACTIVE_LOW;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003846 }
3847
Linus Walleij6392cca2017-12-29 02:07:54 +01003848 /* Currently only ACPI takes this path */
Thierry Redingf9c4a312012-04-12 13:26:01 +02003849 ret = gpiod_request(desc, label);
3850 if (ret)
3851 return ERR_PTR(ret);
Linus Walleijd468bf92013-09-24 11:54:38 +02003852
Thierry Redingf9c4a312012-04-12 13:26:01 +02003853 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
3854 if (ret < 0) {
3855 gpiod_put(desc);
3856 return ERR_PTR(ret);
3857 }
3858
3859 return desc;
Linus Walleijd468bf92013-09-24 11:54:38 +02003860}
3861EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
David Brownelld2876d02008-02-04 22:28:20 -08003862
3863/**
Guennadi Liakhovetskie6de1802008-04-28 02:14:46 -07003864 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
David Brownelld8f388d82008-07-25 01:46:07 -07003865 * function
Linus Walleijd468bf92013-09-24 11:54:38 +02003866 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3867 * @con_id: function within the GPIO consumer
David Brownelld2876d02008-02-04 22:28:20 -08003868 * @index: index of the GPIO to obtain in the consumer
3869 * @flags: optional GPIO initialization flags
3870 *
3871 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
3872 * specified index was assigned to the requested function it will return NULL.
3873 * This is convenient for drivers that need to handle optional GPIOs.
Grant Likely362432a2013-02-09 09:41:49 +00003874 */
David Brownelld8f388d82008-07-25 01:46:07 -07003875struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003876 const char *con_id,
Thierry Redingf9c4a312012-04-12 13:26:01 +02003877 unsigned int index,
3878 enum gpiod_flags flags)
3879{
Grant Likely362432a2013-02-09 09:41:49 +00003880 struct gpio_desc *desc;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003881
Grant Likely362432a2013-02-09 09:41:49 +00003882 desc = gpiod_get_index(dev, con_id, index, flags);
3883 if (IS_ERR(desc)) {
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003884 if (PTR_ERR(desc) == -ENOENT)
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003885 return NULL;
David Brownelld2876d02008-02-04 22:28:20 -08003886 }
3887
Benoit Parrotf625d462015-02-02 11:44:44 -06003888 return desc;
3889}
3890EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
3891
3892/**
3893 * gpiod_hog - Hog the specified GPIO desc given the provided flags
3894 * @desc: gpio whose value will be assigned
3895 * @name: gpio line name
3896 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3897 * of_get_gpio_hog()
3898 * @dflags: gpiod_flags - optional GPIO initialization flags
3899 */
3900int gpiod_hog(struct gpio_desc *desc, const char *name,
3901 unsigned long lflags, enum gpiod_flags dflags)
3902{
3903 struct gpio_chip *chip;
3904 struct gpio_desc *local_desc;
3905 int hwnum;
3906 int status;
3907
Laxman Dewanganc31a5712016-03-11 19:13:21 +05303908 chip = gpiod_to_chip(desc);
3909 hwnum = gpio_chip_hwgpio(desc);
3910
3911 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
Benoit Parrotf625d462015-02-02 11:44:44 -06003912 if (IS_ERR(local_desc)) {
3913 status = PTR_ERR(local_desc);
Johan Hovold85b03b32016-07-03 18:32:05 +02003914 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
Benoit Parrotf625d462015-02-02 11:44:44 -06003915 name, chip->label, hwnum, status);
Laxman Dewanganc31a5712016-03-11 19:13:21 +05303916 return status;
3917 }
Benoit Parrotf625d462015-02-02 11:44:44 -06003918
3919 status = gpiod_configure_flags(desc, name, lflags, dflags);
3920 if (status < 0) {
3921 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed, %d\n",
3922 name, chip->label, hwnum, status);
3923 gpiochip_free_own_desc(desc);
3924 return status;
3925 }
3926
3927 /* Mark GPIO as hogged so it can be identified and removed later */
3928 set_bit(FLAG_IS_HOGGED, &desc->flags);
3929
3930 pr_info("GPIO line %d (%s) hogged as %s%s\n",
3931 desc_to_gpio(desc), name,
3932 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
3933 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
3934 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
3935
3936 return 0;
3937}
3938
3939/**
3940 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
3941 * @chip: gpio chip to act on
3942 *
3943 * This is only used by of_gpiochip_remove to free hogged gpios
3944 */
Linus Walleij1c3cdb12016-02-09 13:51:59 +01003945static void gpiochip_free_hogs(struct gpio_chip *chip)
3946{
Benoit Parrotf625d462015-02-02 11:44:44 -06003947 int id;
3948
3949 for (id = 0; id < chip->ngpio; id++) {
3950 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003951 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
3952 }
3953}
3954
3955/**
3956 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
3957 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3958 * @con_id: function within the GPIO consumer
3959 * @flags: optional GPIO initialization flags
3960 *
3961 * This function acquires all the GPIOs defined under a given function.
3962 *
3963 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
3964 * no GPIO has been assigned to the requested function, or another IS_ERR()
3965 * code if an error occurred while trying to acquire the GPIOs.
3966 */
3967struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
3968 const char *con_id,
3969 enum gpiod_flags flags)
3970{
3971 struct gpio_desc *desc;
3972 struct gpio_descs *descs;
3973 int count;
3974
3975 count = gpiod_count(dev, con_id);
3976 if (count < 0)
3977 return ERR_PTR(count);
3978
3979 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
3980 GFP_KERNEL);
3981 if (!descs)
3982 return ERR_PTR(-ENOMEM);
3983
3984 for (descs->ndescs = 0; descs->ndescs < count; ) {
3985 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
3986 if (IS_ERR(desc)) {
3987 gpiod_put_array(descs);
3988 return ERR_CAST(desc);
3989 }
3990 descs->desc[descs->ndescs] = desc;
3991 descs->ndescs++;
3992 }
3993 return descs;
3994}
3995EXPORT_SYMBOL_GPL(gpiod_get_array);
3996
3997/**
3998 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
3999 * function
4000 * @dev: GPIO consumer, can be NULL for system-global GPIOs
4001 * @con_id: function within the GPIO consumer
4002 * @flags: optional GPIO initialization flags
4003 *
4004 * This is equivalent to gpiod_get_array(), except that when no GPIO was
4005 * assigned to the requested function it will return NULL.
4006 */
4007struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
4008 const char *con_id,
4009 enum gpiod_flags flags)
4010{
4011 struct gpio_descs *descs;
4012
4013 descs = gpiod_get_array(dev, con_id, flags);
4014 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
4015 return NULL;
4016
David Brownelld2876d02008-02-04 22:28:20 -08004017 return descs;
4018}
4019EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
4020
4021/**
4022 * gpiod_put - dispose of a GPIO descriptor
4023 * @desc: GPIO descriptor to dispose of
4024 *
4025 * No descriptor can be used after gpiod_put() has been called on it.
4026 */
4027void gpiod_put(struct gpio_desc *desc)
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01004028{
4029 gpiod_free(desc);
4030}
4031EXPORT_SYMBOL_GPL(gpiod_put);
4032
4033/**
4034 * gpiod_put_array - dispose of multiple GPIO descriptors
4035 * @descs: struct gpio_descs containing an array of descriptors
4036 */
4037void gpiod_put_array(struct gpio_descs *descs)
4038{
4039 unsigned int i;
4040
4041 for (i = 0; i < descs->ndescs; i++)
4042 gpiod_put(descs->desc[i]);
Linus Walleij3c702e92015-10-21 15:29:53 +02004043
4044 kfree(descs);
4045}
4046EXPORT_SYMBOL_GPL(gpiod_put_array);
4047
4048static int __init gpiolib_dev_init(void)
4049{
4050 int ret;
4051
4052 /* Register GPIO sysfs bus */
4053 ret = bus_register(&gpio_bus_type);
4054 if (ret < 0) {
4055 pr_err("gpiolib: could not register GPIO bus type\n");
4056 return ret;
4057 }
Guenter Roeck159f3cd2016-03-31 08:11:30 -07004058
4059 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
4060 if (ret < 0) {
Linus Walleij3c702e92015-10-21 15:29:53 +02004061 pr_err("gpiolib: failed to allocate char dev region\n");
4062 bus_unregister(&gpio_bus_type);
4063 } else {
4064 gpiolib_initialized = true;
4065 gpiochip_setup_devs();
David Brownelld2876d02008-02-04 22:28:20 -08004066 }
4067 return ret;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004068}
David Brownelld2876d02008-02-04 22:28:20 -08004069core_initcall(gpiolib_dev_init);
4070
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004071#ifdef CONFIG_DEBUG_FS
4072
4073static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
David Brownelld2876d02008-02-04 22:28:20 -08004074{
4075 unsigned i;
4076 struct gpio_chip *chip = gdev->chip;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004077 unsigned gpio = gdev->base;
Markus Pargmannced433e2015-08-14 16:11:02 +02004078 struct gpio_desc *gdesc = &gdev->descs[0];
4079 int is_out;
4080 int is_irq;
4081
4082 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
David Brownelld2876d02008-02-04 22:28:20 -08004083 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
Markus Pargmannced433e2015-08-14 16:11:02 +02004084 if (gdesc->name) {
David Brownelld2876d02008-02-04 22:28:20 -08004085 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
4086 gpio, gdesc->name);
4087 }
4088 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02004089 }
4090
David Brownelld2876d02008-02-04 22:28:20 -08004091 gpiod_get_direction(gdesc);
4092 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
4093 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
4094 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
4095 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
4096 is_out ? "out" : "in ",
4097 chip->get
4098 ? (chip->get(chip, i) ? "hi" : "lo")
4099 : "? ",
4100 is_irq ? "IRQ" : " ");
4101 seq_printf(s, "\n");
4102 }
Linus Walleijff2b1352015-10-20 11:10:38 +02004103}
David Brownelld2876d02008-02-04 22:28:20 -08004104
4105static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
4106{
4107 unsigned long flags;
4108 struct gpio_device *gdev = NULL;
Linus Walleijff2b1352015-10-20 11:10:38 +02004109 loff_t index = *pos;
David Brownelld2876d02008-02-04 22:28:20 -08004110
4111 s->private = "";
Linus Walleijff2b1352015-10-20 11:10:38 +02004112
David Brownelld2876d02008-02-04 22:28:20 -08004113 spin_lock_irqsave(&gpio_lock, flags);
4114 list_for_each_entry(gdev, &gpio_devices, list)
4115 if (index-- == 0) {
4116 spin_unlock_irqrestore(&gpio_lock, flags);
4117 return gdev;
4118 }
4119 spin_unlock_irqrestore(&gpio_lock, flags);
4120
4121 return NULL;
Linus Walleijff2b1352015-10-20 11:10:38 +02004122}
David Brownelld2876d02008-02-04 22:28:20 -08004123
4124static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
4125{
Linus Walleijff2b1352015-10-20 11:10:38 +02004126 unsigned long flags;
David Brownelld2876d02008-02-04 22:28:20 -08004127 struct gpio_device *gdev = v;
4128 void *ret = NULL;
Linus Walleijff2b1352015-10-20 11:10:38 +02004129
David Brownelld2876d02008-02-04 22:28:20 -08004130 spin_lock_irqsave(&gpio_lock, flags);
4131 if (list_is_last(&gdev->list, &gpio_devices))
4132 ret = NULL;
4133 else
4134 ret = list_entry(gdev->list.next, struct gpio_device, list);
4135 spin_unlock_irqrestore(&gpio_lock, flags);
4136
4137 s->private = "\n";
4138 ++*pos;
4139
4140 return ret;
4141}
4142
4143static void gpiolib_seq_stop(struct seq_file *s, void *v)
Linus Walleijff2b1352015-10-20 11:10:38 +02004144{
4145}
4146
David Brownelld2876d02008-02-04 22:28:20 -08004147static int gpiolib_seq_show(struct seq_file *s, void *v)
Linus Walleijff2b1352015-10-20 11:10:38 +02004148{
4149 struct gpio_device *gdev = v;
4150 struct gpio_chip *chip = gdev->chip;
4151 struct device *parent;
4152
4153 if (!chip) {
4154 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
4155 dev_name(&gdev->dev));
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004156 return 0;
Linus Walleijff2b1352015-10-20 11:10:38 +02004157 }
4158
4159 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
4160 dev_name(&gdev->dev),
4161 gdev->base, gdev->base + gdev->ngpio - 1);
David Brownelld2876d02008-02-04 22:28:20 -08004162 parent = chip->parent;
4163 if (parent)
4164 seq_printf(s, ", parent: %s/%s",
4165 parent->bus ? parent->bus->name : "no-bus",
4166 dev_name(parent));
4167 if (chip->label)
4168 seq_printf(s, ", %s", chip->label);
4169 if (chip->can_sleep)
4170 seq_printf(s, ", can sleep");
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004171 seq_printf(s, ":\n");
David Brownelld2876d02008-02-04 22:28:20 -08004172
4173 if (chip->dbg_show)
4174 chip->dbg_show(s, chip);
4175 else
4176 gpiolib_dbg_show(s, gdev);
4177
4178 return 0;
4179}
4180
4181static const struct seq_operations gpiolib_seq_ops = {
4182 .start = gpiolib_seq_start,
4183 .next = gpiolib_seq_next,
4184 .stop = gpiolib_seq_stop,
4185 .show = gpiolib_seq_show,
4186};
4187
4188static int gpiolib_open(struct inode *inode, struct file *file)
4189{
4190 return seq_open(file, &gpiolib_seq_ops);
4191}
4192
4193static const struct file_operations gpiolib_operations = {
4194 .owner = THIS_MODULE,
4195 .open = gpiolib_open,
4196 .read = seq_read,
4197 .llseek = seq_lseek,
4198 .release = seq_release,
4199};
4200
4201static int __init gpiolib_debugfs_init(void)
4202{
4203 /* /sys/kernel/debug/gpio */
4204 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
4205 NULL, NULL, &gpiolib_operations);
4206 return 0;
4207}
4208subsys_initcall(gpiolib_debugfs_init);
4209
4210#endif /* DEBUG_FS */