blob: 754836e4ca0e0d7b7d0d862a9baf29160559cfd8 [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,
76 struct lock_class_key *key);
Johan Hovold6d867502015-05-04 17:23:25 +020077static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
Mika Westerberg79b804c2016-09-20 15:15:21 +030078static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip);
79static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip);
Johan Hovold6d867502015-05-04 17:23:25 +020080
Guenter Roeck159f3cd2016-03-31 08:11:30 -070081static bool gpiolib_initialized;
Johan Hovold6d867502015-05-04 17:23:25 +020082
David Brownelld2876d02008-02-04 22:28:20 -080083static inline void desc_set_label(struct gpio_desc *d, const char *label)
84{
David Brownelld2876d02008-02-04 22:28:20 -080085 d->label = label;
David Brownelld2876d02008-02-04 22:28:20 -080086}
87
Alexandre Courbot372e7222013-02-03 01:29:29 +090088/**
Thierry Reding950d55f52017-07-24 16:57:22 +020089 * gpio_to_desc - Convert a GPIO number to its descriptor
90 * @gpio: global GPIO number
91 *
92 * Returns:
93 * The GPIO descriptor associated with the given GPIO, or %NULL if no GPIO
94 * with the given number exists in the system.
Alexandre Courbot372e7222013-02-03 01:29:29 +090095 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070096struct gpio_desc *gpio_to_desc(unsigned gpio)
Alexandre Courbot372e7222013-02-03 01:29:29 +090097{
Linus Walleijff2b1352015-10-20 11:10:38 +020098 struct gpio_device *gdev;
Alexandre Courbot14e85c02014-11-19 16:51:27 +090099 unsigned long flags;
100
101 spin_lock_irqsave(&gpio_lock, flags);
102
Linus Walleijff2b1352015-10-20 11:10:38 +0200103 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100104 if (gdev->base <= gpio &&
105 gdev->base + gdev->ngpio > gpio) {
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900106 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100107 return &gdev->descs[gpio - gdev->base];
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900108 }
109 }
110
111 spin_unlock_irqrestore(&gpio_lock, flags);
112
Alexandre Courbot0e9a5ed2014-12-02 23:15:05 +0900113 if (!gpio_is_valid(gpio))
114 WARN(1, "invalid GPIO %d\n", gpio);
115
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900116 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900117}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700118EXPORT_SYMBOL_GPL(gpio_to_desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900119
120/**
Thierry Reding950d55f52017-07-24 16:57:22 +0200121 * gpiochip_get_desc - get the GPIO descriptor corresponding to the given
122 * hardware number for this chip
123 * @chip: GPIO chip
124 * @hwnum: hardware number of the GPIO for this chip
125 *
126 * Returns:
127 * A pointer to the GPIO descriptor or %ERR_PTR(-EINVAL) if no GPIO exists
128 * in the given chip for the specified hardware number.
Linus Walleijd468bf92013-09-24 11:54:38 +0200129 */
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900130struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
131 u16 hwnum)
Linus Walleijd468bf92013-09-24 11:54:38 +0200132{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100133 struct gpio_device *gdev = chip->gpiodev;
134
135 if (hwnum >= gdev->ngpio)
Alexandre Courbotb7d0a282013-12-03 12:31:11 +0900136 return ERR_PTR(-EINVAL);
Linus Walleijd468bf92013-09-24 11:54:38 +0200137
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100138 return &gdev->descs[hwnum];
Linus Walleijd468bf92013-09-24 11:54:38 +0200139}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900140
141/**
Thierry Reding950d55f52017-07-24 16:57:22 +0200142 * desc_to_gpio - convert a GPIO descriptor to the integer namespace
143 * @desc: GPIO descriptor
144 *
Alexandre Courbot372e7222013-02-03 01:29:29 +0900145 * This should disappear in the future but is needed since we still
Thierry Reding950d55f52017-07-24 16:57:22 +0200146 * use GPIO numbers for error messages and sysfs nodes.
147 *
148 * Returns:
149 * The global GPIO number for the GPIO specified by its descriptor.
Alexandre Courbot372e7222013-02-03 01:29:29 +0900150 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700151int desc_to_gpio(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900152{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100153 return desc->gdev->base + (desc - &desc->gdev->descs[0]);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900154}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700155EXPORT_SYMBOL_GPL(desc_to_gpio);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900156
157
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700158/**
159 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
160 * @desc: descriptor to return the chip of
161 */
162struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900163{
Vladimir Zapolskiydd3b9a42017-12-21 18:37:30 +0200164 if (!desc || !desc->gdev)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100165 return NULL;
166 return desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900167}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700168EXPORT_SYMBOL_GPL(gpiod_to_chip);
David Brownelld2876d02008-02-04 22:28:20 -0800169
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700170/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
171static int gpiochip_find_base(int ngpio)
172{
Linus Walleijff2b1352015-10-20 11:10:38 +0200173 struct gpio_device *gdev;
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900174 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700175
Linus Walleijff2b1352015-10-20 11:10:38 +0200176 list_for_each_entry_reverse(gdev, &gpio_devices, list) {
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900177 /* found a free space? */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100178 if (gdev->base + gdev->ngpio <= base)
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900179 break;
180 else
181 /* nope, check the space right before the chip */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100182 base = gdev->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700183 }
184
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900185 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700186 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900187 return base;
188 } else {
189 pr_err("%s: cannot find free range\n", __func__);
190 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700191 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700192}
193
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700194/**
195 * gpiod_get_direction - return the current direction of a GPIO
196 * @desc: GPIO to get the direction of
197 *
Wolfram Sang94fc7302018-01-09 12:35:53 +0100198 * Returns 0 for output, 1 for input, or an error code in case of error.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700199 *
200 * This function may sleep if gpiod_cansleep() is true.
201 */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900202int gpiod_get_direction(struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300203{
204 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900205 unsigned offset;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300206 int status = -EINVAL;
207
Alexandre Courbot372e7222013-02-03 01:29:29 +0900208 chip = gpiod_to_chip(desc);
209 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300210
211 if (!chip->get_direction)
212 return status;
213
Alexandre Courbot372e7222013-02-03 01:29:29 +0900214 status = chip->get_direction(chip, offset);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300215 if (status > 0) {
216 /* GPIOF_DIR_IN, or other positive */
217 status = 1;
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900218 clear_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300219 }
220 if (status == 0) {
221 /* GPIOF_DIR_OUT */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900222 set_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300223 }
224 return status;
225}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700226EXPORT_SYMBOL_GPL(gpiod_get_direction);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300227
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900228/*
229 * Add a new chip to the global chips list, keeping the list of chips sorted
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800230 * by range(means [base, base + ngpio - 1]) order.
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900231 *
232 * Return -EBUSY if the new chip overlaps with some other chip's integer
233 * space.
234 */
Linus Walleijff2b1352015-10-20 11:10:38 +0200235static int gpiodev_add_to_list(struct gpio_device *gdev)
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900236{
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800237 struct gpio_device *prev, *next;
Linus Walleijff2b1352015-10-20 11:10:38 +0200238
239 if (list_empty(&gpio_devices)) {
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800240 /* initial entry in list */
Linus Walleijff2b1352015-10-20 11:10:38 +0200241 list_add_tail(&gdev->list, &gpio_devices);
Sudip Mukherjeee28ecca2015-12-27 19:06:50 +0530242 return 0;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900243 }
244
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800245 next = list_entry(gpio_devices.next, struct gpio_device, list);
246 if (gdev->base + gdev->ngpio <= next->base) {
247 /* add before first entry */
248 list_add(&gdev->list, &gpio_devices);
Julien Grossholtz96098df2016-01-07 16:46:45 -0500249 return 0;
250 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900251
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800252 prev = list_entry(gpio_devices.prev, struct gpio_device, list);
253 if (prev->base + prev->ngpio <= gdev->base) {
254 /* add behind last entry */
255 list_add_tail(&gdev->list, &gpio_devices);
256 return 0;
257 }
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800258
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800259 list_for_each_entry_safe(prev, next, &gpio_devices, list) {
260 /* at the end of the list */
261 if (&next->list == &gpio_devices)
262 break;
263
264 /* add between prev and next */
265 if (prev->base + prev->ngpio <= gdev->base
266 && gdev->base + gdev->ngpio <= next->base) {
267 list_add(&gdev->list, &prev->list);
268 return 0;
269 }
270 }
271
272 dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n");
273 return -EBUSY;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900274}
275
Thierry Reding950d55f52017-07-24 16:57:22 +0200276/*
Linus Walleijf881bab2015-09-23 16:20:43 -0700277 * Convert a GPIO name to its descriptor
278 */
279static struct gpio_desc *gpio_name_to_desc(const char * const name)
280{
Linus Walleijff2b1352015-10-20 11:10:38 +0200281 struct gpio_device *gdev;
Linus Walleijf881bab2015-09-23 16:20:43 -0700282 unsigned long flags;
283
284 spin_lock_irqsave(&gpio_lock, flags);
285
Linus Walleijff2b1352015-10-20 11:10:38 +0200286 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijf881bab2015-09-23 16:20:43 -0700287 int i;
288
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100289 for (i = 0; i != gdev->ngpio; ++i) {
290 struct gpio_desc *desc = &gdev->descs[i];
Linus Walleijf881bab2015-09-23 16:20:43 -0700291
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100292 if (!desc->name || !name)
Linus Walleijf881bab2015-09-23 16:20:43 -0700293 continue;
294
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100295 if (!strcmp(desc->name, name)) {
Linus Walleijf881bab2015-09-23 16:20:43 -0700296 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100297 return desc;
Linus Walleijf881bab2015-09-23 16:20:43 -0700298 }
299 }
300 }
301
302 spin_unlock_irqrestore(&gpio_lock, flags);
303
304 return NULL;
305}
306
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200307/*
308 * Takes the names from gc->names and checks if they are all unique. If they
309 * are, they are assigned to their gpio descriptors.
310 *
Bamvor Jian Zhanged379152015-11-14 16:43:20 +0800311 * Warning if one of the names is already used for a different GPIO.
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200312 */
313static int gpiochip_set_desc_names(struct gpio_chip *gc)
314{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100315 struct gpio_device *gdev = gc->gpiodev;
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200316 int i;
317
318 if (!gc->names)
319 return 0;
320
321 /* First check all names if they are unique */
322 for (i = 0; i != gc->ngpio; ++i) {
323 struct gpio_desc *gpio;
324
325 gpio = gpio_name_to_desc(gc->names[i]);
Linus Walleijf881bab2015-09-23 16:20:43 -0700326 if (gpio)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100327 dev_warn(&gdev->dev,
Linus Walleij34ffd852015-10-20 11:31:54 +0200328 "Detected name collision for GPIO name '%s'\n",
Linus Walleijf881bab2015-09-23 16:20:43 -0700329 gc->names[i]);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200330 }
331
332 /* Then add all names to the GPIO descriptors */
333 for (i = 0; i != gc->ngpio; ++i)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100334 gdev->descs[i].name = gc->names[i];
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200335
336 return 0;
337}
338
Linus Walleijd7c51b42016-04-26 10:35:29 +0200339/*
340 * GPIO line handle management
341 */
342
343/**
344 * struct linehandle_state - contains the state of a userspace handle
345 * @gdev: the GPIO device the handle pertains to
346 * @label: consumer label used to tag descriptors
347 * @descs: the GPIO descriptors held by this handle
348 * @numdescs: the number of descriptors held in the descs array
349 */
350struct linehandle_state {
351 struct gpio_device *gdev;
352 const char *label;
353 struct gpio_desc *descs[GPIOHANDLES_MAX];
354 u32 numdescs;
355};
356
Lars-Peter Clausene3e847c2016-10-18 16:54:05 +0200357#define GPIOHANDLE_REQUEST_VALID_FLAGS \
358 (GPIOHANDLE_REQUEST_INPUT | \
359 GPIOHANDLE_REQUEST_OUTPUT | \
360 GPIOHANDLE_REQUEST_ACTIVE_LOW | \
361 GPIOHANDLE_REQUEST_OPEN_DRAIN | \
362 GPIOHANDLE_REQUEST_OPEN_SOURCE)
363
Linus Walleijd7c51b42016-04-26 10:35:29 +0200364static long linehandle_ioctl(struct file *filep, unsigned int cmd,
365 unsigned long arg)
366{
367 struct linehandle_state *lh = filep->private_data;
368 void __user *ip = (void __user *)arg;
369 struct gpiohandle_data ghd;
Lukas Wunnereec1d562017-10-12 12:40:10 +0200370 int vals[GPIOHANDLES_MAX];
Linus Walleijd7c51b42016-04-26 10:35:29 +0200371 int i;
372
373 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
Lukas Wunnereec1d562017-10-12 12:40:10 +0200374 /* TODO: check if descriptors are really input */
375 int ret = gpiod_get_array_value_complex(false,
376 true,
377 lh->numdescs,
378 lh->descs,
379 vals);
380 if (ret)
381 return ret;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200382
Lars-Peter Clausen3eded5d2016-10-18 16:54:02 +0200383 memset(&ghd, 0, sizeof(ghd));
Lukas Wunnereec1d562017-10-12 12:40:10 +0200384 for (i = 0; i < lh->numdescs; i++)
385 ghd.values[i] = vals[i];
Linus Walleijd7c51b42016-04-26 10:35:29 +0200386
387 if (copy_to_user(ip, &ghd, sizeof(ghd)))
388 return -EFAULT;
389
390 return 0;
391 } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
Linus Walleijd7c51b42016-04-26 10:35:29 +0200392 /* TODO: check if descriptors are really output */
393 if (copy_from_user(&ghd, ip, sizeof(ghd)))
394 return -EFAULT;
395
396 /* Clamp all values to [0,1] */
397 for (i = 0; i < lh->numdescs; i++)
398 vals[i] = !!ghd.values[i];
399
400 /* Reuse the array setting function */
401 gpiod_set_array_value_complex(false,
402 true,
403 lh->numdescs,
404 lh->descs,
405 vals);
406 return 0;
407 }
408 return -EINVAL;
409}
410
411#ifdef CONFIG_COMPAT
412static long linehandle_ioctl_compat(struct file *filep, unsigned int cmd,
413 unsigned long arg)
414{
415 return linehandle_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
416}
417#endif
418
419static int linehandle_release(struct inode *inode, struct file *filep)
420{
421 struct linehandle_state *lh = filep->private_data;
422 struct gpio_device *gdev = lh->gdev;
423 int i;
424
425 for (i = 0; i < lh->numdescs; i++)
426 gpiod_free(lh->descs[i]);
427 kfree(lh->label);
428 kfree(lh);
429 put_device(&gdev->dev);
430 return 0;
431}
432
433static const struct file_operations linehandle_fileops = {
434 .release = linehandle_release,
435 .owner = THIS_MODULE,
436 .llseek = noop_llseek,
437 .unlocked_ioctl = linehandle_ioctl,
438#ifdef CONFIG_COMPAT
439 .compat_ioctl = linehandle_ioctl_compat,
440#endif
441};
442
443static int linehandle_create(struct gpio_device *gdev, void __user *ip)
444{
445 struct gpiohandle_request handlereq;
446 struct linehandle_state *lh;
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200447 struct file *file;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200448 int fd, i, ret;
Bartosz Golaszewski418ee8e2017-10-16 11:32:29 +0200449 u32 lflags;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200450
451 if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
452 return -EFAULT;
453 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
454 return -EINVAL;
455
Bartosz Golaszewski418ee8e2017-10-16 11:32:29 +0200456 lflags = handlereq.flags;
457
458 /* Return an error if an unknown flag is set */
459 if (lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
460 return -EINVAL;
461
Bartosz Golaszewski588fc3b2017-11-15 16:47:43 +0100462 /*
463 * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
464 * the hardware actually supports enabling both at the same time the
465 * electrical result would be disastrous.
466 */
467 if ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) &&
468 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
469 return -EINVAL;
470
Bartosz Golaszewski609aaf62017-10-16 11:32:30 +0200471 /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
472 if (!(lflags & GPIOHANDLE_REQUEST_OUTPUT) &&
473 ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
474 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
475 return -EINVAL;
476
Linus Walleijd7c51b42016-04-26 10:35:29 +0200477 lh = kzalloc(sizeof(*lh), GFP_KERNEL);
478 if (!lh)
479 return -ENOMEM;
480 lh->gdev = gdev;
481 get_device(&gdev->dev);
482
483 /* Make sure this is terminated */
484 handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
485 if (strlen(handlereq.consumer_label)) {
486 lh->label = kstrdup(handlereq.consumer_label,
487 GFP_KERNEL);
488 if (!lh->label) {
489 ret = -ENOMEM;
490 goto out_free_lh;
491 }
492 }
493
494 /* Request each GPIO */
495 for (i = 0; i < handlereq.lines; i++) {
496 u32 offset = handlereq.lineoffsets[i];
Linus Walleijd7c51b42016-04-26 10:35:29 +0200497 struct gpio_desc *desc;
498
Lars-Peter Clausene405f9f2016-10-18 16:54:01 +0200499 if (offset >= gdev->ngpio) {
500 ret = -EINVAL;
501 goto out_free_descs;
502 }
503
Linus Walleijd7c51b42016-04-26 10:35:29 +0200504 desc = &gdev->descs[offset];
505 ret = gpiod_request(desc, lh->label);
506 if (ret)
507 goto out_free_descs;
508 lh->descs[i] = desc;
509
510 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
511 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
512 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
513 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
514 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
515 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
516
Andrew Jefferye10f72b2017-11-30 14:25:24 +1030517 ret = gpiod_set_transitory(desc, false);
518 if (ret < 0)
519 goto out_free_descs;
520
Linus Walleijd7c51b42016-04-26 10:35:29 +0200521 /*
522 * Lines have to be requested explicitly for input
523 * or output, else the line will be treated "as is".
524 */
525 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
526 int val = !!handlereq.default_values[i];
527
528 ret = gpiod_direction_output(desc, val);
529 if (ret)
530 goto out_free_descs;
531 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
532 ret = gpiod_direction_input(desc);
533 if (ret)
534 goto out_free_descs;
535 }
536 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
537 offset);
538 }
Linus Walleije2f608b2016-06-18 10:56:43 +0200539 /* Let i point at the last handle */
540 i--;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200541 lh->numdescs = handlereq.lines;
542
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200543 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200544 if (fd < 0) {
545 ret = fd;
546 goto out_free_descs;
547 }
548
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200549 file = anon_inode_getfile("gpio-linehandle",
550 &linehandle_fileops,
551 lh,
552 O_RDONLY | O_CLOEXEC);
553 if (IS_ERR(file)) {
554 ret = PTR_ERR(file);
555 goto out_put_unused_fd;
556 }
557
Linus Walleijd7c51b42016-04-26 10:35:29 +0200558 handlereq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200559 if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200560 /*
561 * fput() will trigger the release() callback, so do not go onto
562 * the regular error cleanup path here.
563 */
564 fput(file);
565 put_unused_fd(fd);
566 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +0200567 }
Linus Walleijd7c51b42016-04-26 10:35:29 +0200568
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200569 fd_install(fd, file);
570
Linus Walleijd7c51b42016-04-26 10:35:29 +0200571 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
572 lh->numdescs);
573
574 return 0;
575
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200576out_put_unused_fd:
577 put_unused_fd(fd);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200578out_free_descs:
579 for (; i >= 0; i--)
580 gpiod_free(lh->descs[i]);
581 kfree(lh->label);
582out_free_lh:
583 kfree(lh);
584 put_device(&gdev->dev);
585 return ret;
586}
587
Linus Walleij61f922d2016-06-02 11:30:15 +0200588/*
589 * GPIO line event management
590 */
591
592/**
593 * struct lineevent_state - contains the state of a userspace event
594 * @gdev: the GPIO device the event pertains to
595 * @label: consumer label used to tag descriptors
596 * @desc: the GPIO descriptor held by this event
597 * @eflags: the event flags this line was requested with
598 * @irq: the interrupt that trigger in response to events on this GPIO
599 * @wait: wait queue that handles blocking reads of events
600 * @events: KFIFO for the GPIO events
601 * @read_lock: mutex lock to protect reads from colliding with adding
602 * new events to the FIFO
603 */
604struct lineevent_state {
605 struct gpio_device *gdev;
606 const char *label;
607 struct gpio_desc *desc;
608 u32 eflags;
609 int irq;
610 wait_queue_head_t wait;
611 DECLARE_KFIFO(events, struct gpioevent_data, 16);
612 struct mutex read_lock;
613};
614
Lars-Peter Clausenac7dbb92016-10-18 16:54:06 +0200615#define GPIOEVENT_REQUEST_VALID_FLAGS \
616 (GPIOEVENT_REQUEST_RISING_EDGE | \
617 GPIOEVENT_REQUEST_FALLING_EDGE)
618
Linus Walleij61f922d2016-06-02 11:30:15 +0200619static unsigned int lineevent_poll(struct file *filep,
620 struct poll_table_struct *wait)
621{
622 struct lineevent_state *le = filep->private_data;
623 unsigned int events = 0;
624
625 poll_wait(filep, &le->wait, wait);
626
627 if (!kfifo_is_empty(&le->events))
628 events = POLLIN | POLLRDNORM;
629
630 return events;
631}
632
633
634static ssize_t lineevent_read(struct file *filep,
635 char __user *buf,
636 size_t count,
637 loff_t *f_ps)
638{
639 struct lineevent_state *le = filep->private_data;
640 unsigned int copied;
641 int ret;
642
643 if (count < sizeof(struct gpioevent_data))
644 return -EINVAL;
645
646 do {
647 if (kfifo_is_empty(&le->events)) {
648 if (filep->f_flags & O_NONBLOCK)
649 return -EAGAIN;
650
651 ret = wait_event_interruptible(le->wait,
652 !kfifo_is_empty(&le->events));
653 if (ret)
654 return ret;
655 }
656
657 if (mutex_lock_interruptible(&le->read_lock))
658 return -ERESTARTSYS;
659 ret = kfifo_to_user(&le->events, buf, count, &copied);
660 mutex_unlock(&le->read_lock);
661
662 if (ret)
663 return ret;
664
665 /*
666 * If we couldn't read anything from the fifo (a different
667 * thread might have been faster) we either return -EAGAIN if
668 * the file descriptor is non-blocking, otherwise we go back to
669 * sleep and wait for more data to arrive.
670 */
671 if (copied == 0 && (filep->f_flags & O_NONBLOCK))
672 return -EAGAIN;
673
674 } while (copied == 0);
675
676 return copied;
677}
678
679static int lineevent_release(struct inode *inode, struct file *filep)
680{
681 struct lineevent_state *le = filep->private_data;
682 struct gpio_device *gdev = le->gdev;
683
684 free_irq(le->irq, le);
685 gpiod_free(le->desc);
686 kfree(le->label);
687 kfree(le);
688 put_device(&gdev->dev);
689 return 0;
690}
691
692static long lineevent_ioctl(struct file *filep, unsigned int cmd,
693 unsigned long arg)
694{
695 struct lineevent_state *le = filep->private_data;
696 void __user *ip = (void __user *)arg;
697 struct gpiohandle_data ghd;
698
699 /*
700 * We can get the value for an event line but not set it,
701 * because it is input by definition.
702 */
703 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
704 int val;
705
Lars-Peter Clausend82aa4a2016-10-18 16:54:04 +0200706 memset(&ghd, 0, sizeof(ghd));
707
Linus Walleij61f922d2016-06-02 11:30:15 +0200708 val = gpiod_get_value_cansleep(le->desc);
709 if (val < 0)
710 return val;
711 ghd.values[0] = val;
712
713 if (copy_to_user(ip, &ghd, sizeof(ghd)))
714 return -EFAULT;
715
716 return 0;
717 }
718 return -EINVAL;
719}
720
721#ifdef CONFIG_COMPAT
722static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd,
723 unsigned long arg)
724{
725 return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
726}
727#endif
728
729static const struct file_operations lineevent_fileops = {
730 .release = lineevent_release,
731 .read = lineevent_read,
732 .poll = lineevent_poll,
733 .owner = THIS_MODULE,
734 .llseek = noop_llseek,
735 .unlocked_ioctl = lineevent_ioctl,
736#ifdef CONFIG_COMPAT
737 .compat_ioctl = lineevent_ioctl_compat,
738#endif
739};
740
Ben Dooks33265b12016-06-17 16:03:13 +0100741static irqreturn_t lineevent_irq_thread(int irq, void *p)
Linus Walleij61f922d2016-06-02 11:30:15 +0200742{
743 struct lineevent_state *le = p;
744 struct gpioevent_data ge;
Bartosz Golaszewskidf1e76f2017-07-03 11:12:03 +0200745 int ret, level;
Linus Walleij61f922d2016-06-02 11:30:15 +0200746
Linus Walleij24bd3ef2018-01-22 13:19:28 +0100747 /* Do not leak kernel stack to userspace */
748 memset(&ge, 0, sizeof(ge));
749
Linus Walleij61f922d2016-06-02 11:30:15 +0200750 ge.timestamp = ktime_get_real_ns();
Bartosz Golaszewskidf1e76f2017-07-03 11:12:03 +0200751 level = gpiod_get_value_cansleep(le->desc);
Linus Walleij61f922d2016-06-02 11:30:15 +0200752
Bartosz Golaszewskiad537b82017-06-23 13:45:16 +0200753 if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
754 && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200755 if (level)
756 /* Emit low-to-high event */
757 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
758 else
759 /* Emit high-to-low event */
760 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Bartosz Golaszewskidf1e76f2017-07-03 11:12:03 +0200761 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE && level) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200762 /* Emit low-to-high event */
763 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
Bartosz Golaszewskidf1e76f2017-07-03 11:12:03 +0200764 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE && !level) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200765 /* Emit high-to-low event */
766 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Arnd Bergmannbc0207a2016-06-16 11:02:41 +0200767 } else {
768 return IRQ_NONE;
Linus Walleij61f922d2016-06-02 11:30:15 +0200769 }
770
771 ret = kfifo_put(&le->events, ge);
772 if (ret != 0)
773 wake_up_poll(&le->wait, POLLIN);
774
775 return IRQ_HANDLED;
776}
777
778static int lineevent_create(struct gpio_device *gdev, void __user *ip)
779{
780 struct gpioevent_request eventreq;
781 struct lineevent_state *le;
782 struct gpio_desc *desc;
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200783 struct file *file;
Linus Walleij61f922d2016-06-02 11:30:15 +0200784 u32 offset;
785 u32 lflags;
786 u32 eflags;
787 int fd;
788 int ret;
789 int irqflags = 0;
790
791 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
792 return -EFAULT;
793
794 le = kzalloc(sizeof(*le), GFP_KERNEL);
795 if (!le)
796 return -ENOMEM;
797 le->gdev = gdev;
798 get_device(&gdev->dev);
799
800 /* Make sure this is terminated */
801 eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
802 if (strlen(eventreq.consumer_label)) {
803 le->label = kstrdup(eventreq.consumer_label,
804 GFP_KERNEL);
805 if (!le->label) {
806 ret = -ENOMEM;
807 goto out_free_le;
808 }
809 }
810
811 offset = eventreq.lineoffset;
812 lflags = eventreq.handleflags;
813 eflags = eventreq.eventflags;
814
Lars-Peter Clausenb8b0e3d2016-10-18 16:54:03 +0200815 if (offset >= gdev->ngpio) {
816 ret = -EINVAL;
817 goto out_free_label;
818 }
819
Lars-Peter Clausenac7dbb92016-10-18 16:54:06 +0200820 /* Return an error if a unknown flag is set */
821 if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
822 (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) {
823 ret = -EINVAL;
824 goto out_free_label;
825 }
826
Linus Walleij61f922d2016-06-02 11:30:15 +0200827 /* This is just wrong: we don't look for events on output lines */
828 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
829 ret = -EINVAL;
830 goto out_free_label;
831 }
832
833 desc = &gdev->descs[offset];
834 ret = gpiod_request(desc, le->label);
835 if (ret)
836 goto out_free_desc;
837 le->desc = desc;
838 le->eflags = eflags;
839
840 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
841 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
842 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
843 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
844 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
845 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
846
847 ret = gpiod_direction_input(desc);
848 if (ret)
849 goto out_free_desc;
850
851 le->irq = gpiod_to_irq(desc);
852 if (le->irq <= 0) {
853 ret = -ENODEV;
854 goto out_free_desc;
855 }
856
857 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
858 irqflags |= IRQF_TRIGGER_RISING;
859 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
860 irqflags |= IRQF_TRIGGER_FALLING;
861 irqflags |= IRQF_ONESHOT;
862 irqflags |= IRQF_SHARED;
863
864 INIT_KFIFO(le->events);
865 init_waitqueue_head(&le->wait);
866 mutex_init(&le->read_lock);
867
868 /* Request a thread to read the events */
869 ret = request_threaded_irq(le->irq,
870 NULL,
871 lineevent_irq_thread,
872 irqflags,
873 le->label,
874 le);
875 if (ret)
876 goto out_free_desc;
877
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200878 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleij61f922d2016-06-02 11:30:15 +0200879 if (fd < 0) {
880 ret = fd;
881 goto out_free_irq;
882 }
883
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200884 file = anon_inode_getfile("gpio-event",
885 &lineevent_fileops,
886 le,
887 O_RDONLY | O_CLOEXEC);
888 if (IS_ERR(file)) {
889 ret = PTR_ERR(file);
890 goto out_put_unused_fd;
891 }
892
Linus Walleij61f922d2016-06-02 11:30:15 +0200893 eventreq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200894 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200895 /*
896 * fput() will trigger the release() callback, so do not go onto
897 * the regular error cleanup path here.
898 */
899 fput(file);
900 put_unused_fd(fd);
901 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +0200902 }
Linus Walleij61f922d2016-06-02 11:30:15 +0200903
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200904 fd_install(fd, file);
905
Linus Walleij61f922d2016-06-02 11:30:15 +0200906 return 0;
907
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200908out_put_unused_fd:
909 put_unused_fd(fd);
Linus Walleij61f922d2016-06-02 11:30:15 +0200910out_free_irq:
911 free_irq(le->irq, le);
912out_free_desc:
913 gpiod_free(le->desc);
914out_free_label:
915 kfree(le->label);
916out_free_le:
917 kfree(le);
918 put_device(&gdev->dev);
919 return ret;
920}
921
Thierry Reding950d55f52017-07-24 16:57:22 +0200922/*
Linus Walleij3c702e92015-10-21 15:29:53 +0200923 * gpio_ioctl() - ioctl handler for the GPIO chardev
924 */
925static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
926{
927 struct gpio_device *gdev = filp->private_data;
928 struct gpio_chip *chip = gdev->chip;
Linus Walleij8b92e172016-05-27 14:24:04 +0200929 void __user *ip = (void __user *)arg;
Linus Walleij3c702e92015-10-21 15:29:53 +0200930
931 /* We fail any subsequent ioctl():s when the chip is gone */
932 if (!chip)
933 return -ENODEV;
934
Linus Walleij521a2ad2016-02-12 22:25:22 +0100935 /* Fill in the struct and pass to userspace */
Linus Walleij3c702e92015-10-21 15:29:53 +0200936 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
Linus Walleij521a2ad2016-02-12 22:25:22 +0100937 struct gpiochip_info chipinfo;
938
Lars-Peter Clausen0f4bbb22016-10-18 16:54:00 +0200939 memset(&chipinfo, 0, sizeof(chipinfo));
940
Linus Walleij3c702e92015-10-21 15:29:53 +0200941 strncpy(chipinfo.name, dev_name(&gdev->dev),
942 sizeof(chipinfo.name));
943 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
Linus Walleijdf4878e2016-02-12 14:48:23 +0100944 strncpy(chipinfo.label, gdev->label,
945 sizeof(chipinfo.label));
946 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100947 chipinfo.lines = gdev->ngpio;
Linus Walleij3c702e92015-10-21 15:29:53 +0200948 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
949 return -EFAULT;
950 return 0;
Linus Walleij521a2ad2016-02-12 22:25:22 +0100951 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
952 struct gpioline_info lineinfo;
953 struct gpio_desc *desc;
954
955 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
956 return -EFAULT;
Lars-Peter Clausen1f1cc452016-10-18 16:53:59 +0200957 if (lineinfo.line_offset >= gdev->ngpio)
Linus Walleij521a2ad2016-02-12 22:25:22 +0100958 return -EINVAL;
959
960 desc = &gdev->descs[lineinfo.line_offset];
961 if (desc->name) {
962 strncpy(lineinfo.name, desc->name,
963 sizeof(lineinfo.name));
964 lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
965 } else {
966 lineinfo.name[0] = '\0';
967 }
968 if (desc->label) {
Linus Walleij214338e2016-02-25 21:01:48 +0100969 strncpy(lineinfo.consumer, desc->label,
970 sizeof(lineinfo.consumer));
971 lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +0100972 } else {
Linus Walleij214338e2016-02-25 21:01:48 +0100973 lineinfo.consumer[0] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +0100974 }
975
976 /*
977 * Userspace only need to know that the kernel is using
978 * this GPIO so it can't use it.
979 */
980 lineinfo.flags = 0;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100981 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
982 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
983 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
984 test_bit(FLAG_EXPORT, &desc->flags) ||
985 test_bit(FLAG_SYSFS, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100986 lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100987 if (test_bit(FLAG_IS_OUT, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100988 lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100989 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100990 lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100991 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100992 lineinfo.flags |= GPIOLINE_FLAG_OPEN_DRAIN;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100993 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100994 lineinfo.flags |= GPIOLINE_FLAG_OPEN_SOURCE;
995
996 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
997 return -EFAULT;
998 return 0;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200999 } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
1000 return linehandle_create(gdev, ip);
Linus Walleij61f922d2016-06-02 11:30:15 +02001001 } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
1002 return lineevent_create(gdev, ip);
Linus Walleij3c702e92015-10-21 15:29:53 +02001003 }
1004 return -EINVAL;
1005}
1006
Linus Walleij8b92e172016-05-27 14:24:04 +02001007#ifdef CONFIG_COMPAT
1008static long gpio_ioctl_compat(struct file *filp, unsigned int cmd,
1009 unsigned long arg)
1010{
1011 return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
1012}
1013#endif
1014
Linus Walleij3c702e92015-10-21 15:29:53 +02001015/**
1016 * gpio_chrdev_open() - open the chardev for ioctl operations
1017 * @inode: inode for this chardev
1018 * @filp: file struct for storing private data
1019 * Returns 0 on success
1020 */
1021static int gpio_chrdev_open(struct inode *inode, struct file *filp)
1022{
1023 struct gpio_device *gdev = container_of(inode->i_cdev,
1024 struct gpio_device, chrdev);
1025
1026 /* Fail on open if the backing gpiochip is gone */
Stephen Boydfb505742017-01-09 11:47:44 -08001027 if (!gdev->chip)
Linus Walleij3c702e92015-10-21 15:29:53 +02001028 return -ENODEV;
1029 get_device(&gdev->dev);
1030 filp->private_data = gdev;
Lars-Peter Clausenf4e81c52016-11-30 13:05:21 +01001031
1032 return nonseekable_open(inode, filp);
Linus Walleij3c702e92015-10-21 15:29:53 +02001033}
1034
1035/**
1036 * gpio_chrdev_release() - close chardev after ioctl operations
1037 * @inode: inode for this chardev
1038 * @filp: file struct for storing private data
1039 * Returns 0 on success
1040 */
1041static int gpio_chrdev_release(struct inode *inode, struct file *filp)
1042{
1043 struct gpio_device *gdev = container_of(inode->i_cdev,
1044 struct gpio_device, chrdev);
1045
Linus Walleij3c702e92015-10-21 15:29:53 +02001046 put_device(&gdev->dev);
1047 return 0;
1048}
1049
1050
1051static const struct file_operations gpio_fileops = {
1052 .release = gpio_chrdev_release,
1053 .open = gpio_chrdev_open,
1054 .owner = THIS_MODULE,
Lars-Peter Clausenf4e81c52016-11-30 13:05:21 +01001055 .llseek = no_llseek,
Linus Walleij3c702e92015-10-21 15:29:53 +02001056 .unlocked_ioctl = gpio_ioctl,
Linus Walleij8b92e172016-05-27 14:24:04 +02001057#ifdef CONFIG_COMPAT
1058 .compat_ioctl = gpio_ioctl_compat,
1059#endif
Linus Walleij3c702e92015-10-21 15:29:53 +02001060};
1061
Linus Walleijff2b1352015-10-20 11:10:38 +02001062static void gpiodevice_release(struct device *dev)
1063{
1064 struct gpio_device *gdev = dev_get_drvdata(dev);
1065
1066 list_del(&gdev->list);
1067 ida_simple_remove(&gpio_ida, gdev->id);
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +01001068 kfree_const(gdev->label);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001069 kfree(gdev->descs);
Linus Walleij9efd9e62016-02-09 14:27:42 +01001070 kfree(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001071}
1072
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001073static int gpiochip_setup_dev(struct gpio_device *gdev)
1074{
1075 int status;
1076
1077 cdev_init(&gdev->chrdev, &gpio_fileops);
1078 gdev->chrdev.owner = THIS_MODULE;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001079 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001080
1081 status = cdev_device_add(&gdev->chrdev, &gdev->dev);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001082 if (status)
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001083 return status;
1084
1085 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
1086 MAJOR(gpio_devt), gdev->id);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001087
1088 status = gpiochip_sysfs_register(gdev);
1089 if (status)
1090 goto err_remove_device;
1091
1092 /* From this point, the .release() function cleans up gpio_device */
1093 gdev->dev.release = gpiodevice_release;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001094 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
1095 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
1096 dev_name(&gdev->dev), gdev->chip->label ? : "generic");
1097
1098 return 0;
1099
1100err_remove_device:
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001101 cdev_device_del(&gdev->chrdev, &gdev->dev);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001102 return status;
1103}
1104
1105static void gpiochip_setup_devs(void)
1106{
1107 struct gpio_device *gdev;
1108 int err;
1109
1110 list_for_each_entry(gdev, &gpio_devices, list) {
1111 err = gpiochip_setup_dev(gdev);
1112 if (err)
1113 pr_err("%s: Failed to initialize gpio device (%d)\n",
1114 dev_name(&gdev->dev), err);
1115 }
1116}
1117
Thierry Reding959bc7b2017-11-07 19:15:59 +01001118int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
1119 struct lock_class_key *key)
David Brownelld2876d02008-02-04 22:28:20 -08001120{
1121 unsigned long flags;
1122 int status = 0;
Linus Walleijff2b1352015-10-20 11:10:38 +02001123 unsigned i;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001124 int base = chip->base;
Linus Walleijff2b1352015-10-20 11:10:38 +02001125 struct gpio_device *gdev;
David Brownelld2876d02008-02-04 22:28:20 -08001126
Linus Walleijff2b1352015-10-20 11:10:38 +02001127 /*
1128 * First: allocate and populate the internal stat container, and
1129 * set up the struct device.
1130 */
Josh Cartwright969f07b2016-02-17 16:44:15 -06001131 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
Linus Walleijff2b1352015-10-20 11:10:38 +02001132 if (!gdev)
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001133 return -ENOMEM;
Linus Walleij3c702e92015-10-21 15:29:53 +02001134 gdev->dev.bus = &gpio_bus_type;
Linus Walleijff2b1352015-10-20 11:10:38 +02001135 gdev->chip = chip;
1136 chip->gpiodev = gdev;
1137 if (chip->parent) {
1138 gdev->dev.parent = chip->parent;
1139 gdev->dev.of_node = chip->parent->of_node;
Thierry Redingacc6e332016-07-05 14:11:14 +02001140 }
1141
Linus Walleijff2b1352015-10-20 11:10:38 +02001142#ifdef CONFIG_OF_GPIO
1143 /* If the gpiochip has an assigned OF node this takes precedence */
Thierry Redingacc6e332016-07-05 14:11:14 +02001144 if (chip->of_node)
1145 gdev->dev.of_node = chip->of_node;
Linus Walleijff2b1352015-10-20 11:10:38 +02001146#endif
Thierry Redingacc6e332016-07-05 14:11:14 +02001147
Linus Walleijff2b1352015-10-20 11:10:38 +02001148 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
1149 if (gdev->id < 0) {
1150 status = gdev->id;
1151 goto err_free_gdev;
1152 }
1153 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
1154 device_initialize(&gdev->dev);
1155 dev_set_drvdata(&gdev->dev, gdev);
1156 if (chip->parent && chip->parent->driver)
1157 gdev->owner = chip->parent->driver->owner;
1158 else if (chip->owner)
1159 /* TODO: remove chip->owner */
1160 gdev->owner = chip->owner;
1161 else
1162 gdev->owner = THIS_MODULE;
David Brownelld2876d02008-02-04 22:28:20 -08001163
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001164 gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001165 if (!gdev->descs) {
Linus Walleijff2b1352015-10-20 11:10:38 +02001166 status = -ENOMEM;
1167 goto err_free_gdev;
1168 }
1169
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001170 if (chip->ngpio == 0) {
1171 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
Linus Walleijff2b1352015-10-20 11:10:38 +02001172 status = -EINVAL;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001173 goto err_free_descs;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001174 }
Linus Walleijdf4878e2016-02-12 14:48:23 +01001175
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +01001176 gdev->label = kstrdup_const(chip->label ?: "unknown", GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001177 if (!gdev->label) {
1178 status = -ENOMEM;
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001179 goto err_free_descs;
Linus Walleijdf4878e2016-02-12 14:48:23 +01001180 }
1181
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001182 gdev->ngpio = chip->ngpio;
Linus Walleij43c54ec2016-02-11 11:37:48 +01001183 gdev->data = data;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001184
David Brownelld2876d02008-02-04 22:28:20 -08001185 spin_lock_irqsave(&gpio_lock, flags);
1186
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001187 /*
1188 * TODO: this allocates a Linux GPIO number base in the global
1189 * GPIO numberspace for this chip. In the long run we want to
1190 * get *rid* of this numberspace and use only descriptors, but
1191 * it may be a pipe dream. It will not happen before we get rid
1192 * of the sysfs interface anyways.
1193 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001194 if (base < 0) {
1195 base = gpiochip_find_base(chip->ngpio);
1196 if (base < 0) {
1197 status = base;
Johan Hovold225fce82015-01-12 17:12:25 +01001198 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001199 goto err_free_label;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001200 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001201 /*
1202 * TODO: it should not be necessary to reflect the assigned
1203 * base outside of the GPIO subsystem. Go over drivers and
1204 * see if anyone makes use of this, else drop this and assign
1205 * a poison instead.
1206 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001207 chip->base = base;
1208 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001209 gdev->base = base;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001210
Linus Walleijff2b1352015-10-20 11:10:38 +02001211 status = gpiodev_add_to_list(gdev);
Johan Hovold05aa5202015-01-12 17:12:26 +01001212 if (status) {
1213 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001214 goto err_free_label;
Johan Hovold05aa5202015-01-12 17:12:26 +01001215 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001216
Linus Walleij545ebd92016-05-30 17:11:59 +02001217 spin_unlock_irqrestore(&gpio_lock, flags);
1218
Linus Walleijff2b1352015-10-20 11:10:38 +02001219 for (i = 0; i < chip->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001220 struct gpio_desc *desc = &gdev->descs[i];
David Brownelld8f388d82008-07-25 01:46:07 -07001221
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001222 desc->gdev = gdev;
Timur Tabi1ca2a922017-12-20 13:10:31 -06001223
1224 /* REVISIT: most hardware initializes GPIOs as inputs (often
1225 * with pullups enabled) so power usage is minimized. Linux
1226 * code should set the gpio direction first thing; but until
1227 * it does, and in case chip->get_direction is not set, we may
1228 * expose the wrong direction in sysfs.
Johan Hovold05aa5202015-01-12 17:12:26 +01001229 */
Timur Tabi1ca2a922017-12-20 13:10:31 -06001230 desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0;
David Brownelld2876d02008-02-04 22:28:20 -08001231 }
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001232
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301233#ifdef CONFIG_PINCTRL
Linus Walleij20ec3e32016-02-11 11:03:06 +01001234 INIT_LIST_HEAD(&gdev->pin_ranges);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301235#endif
1236
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001237 status = gpiochip_set_desc_names(chip);
1238 if (status)
1239 goto err_remove_from_list;
1240
Mika Westerberg79b804c2016-09-20 15:15:21 +03001241 status = gpiochip_irqchip_init_valid_mask(chip);
1242 if (status)
1243 goto err_remove_from_list;
1244
Thierry Reding959bc7b2017-11-07 19:15:59 +01001245 status = gpiochip_add_irqchip(chip, key);
Thierry Redinge0d89722017-11-07 19:15:54 +01001246 if (status)
1247 goto err_remove_chip;
1248
Tomeu Vizoso28355f82015-07-14 10:29:54 +02001249 status = of_gpiochip_add(chip);
1250 if (status)
1251 goto err_remove_chip;
1252
Mika Westerberg664e3e52014-01-08 12:40:54 +02001253 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001254
Linus Walleij3c702e92015-10-21 15:29:53 +02001255 /*
1256 * By first adding the chardev, and then adding the device,
1257 * we get a device node entry in sysfs under
1258 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1259 * coldplug of device nodes and other udev business.
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001260 * We can do this only if gpiolib has been initialized.
1261 * Otherwise, defer until later.
Linus Walleij3c702e92015-10-21 15:29:53 +02001262 */
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001263 if (gpiolib_initialized) {
1264 status = gpiochip_setup_dev(gdev);
1265 if (status)
1266 goto err_remove_chip;
1267 }
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001268 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001269
Johan Hovold225fce82015-01-12 17:12:25 +01001270err_remove_chip:
1271 acpi_gpiochip_remove(chip);
Johan Hovold6d867502015-05-04 17:23:25 +02001272 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +01001273 of_gpiochip_remove(chip);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001274 gpiochip_irqchip_free_valid_mask(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001275err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +01001276 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001277 list_del(&gdev->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001278 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001279err_free_label:
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +01001280 kfree_const(gdev->label);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001281err_free_descs:
1282 kfree(gdev->descs);
Linus Walleijff2b1352015-10-20 11:10:38 +02001283err_free_gdev:
1284 ida_simple_remove(&gpio_ida, gdev->id);
David Brownelld2876d02008-02-04 22:28:20 -08001285 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +02001286 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001287 gdev->base, gdev->base + gdev->ngpio - 1,
1288 chip->label ? : "generic");
1289 kfree(gdev);
David Brownelld2876d02008-02-04 22:28:20 -08001290 return status;
1291}
Thierry Reding959bc7b2017-11-07 19:15:59 +01001292EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);
David Brownelld2876d02008-02-04 22:28:20 -08001293
1294/**
Linus Walleij43c54ec2016-02-11 11:37:48 +01001295 * gpiochip_get_data() - get per-subdriver data for the chip
Thierry Reding950d55f52017-07-24 16:57:22 +02001296 * @chip: GPIO chip
1297 *
1298 * Returns:
1299 * The per-subdriver data for the chip.
Linus Walleij43c54ec2016-02-11 11:37:48 +01001300 */
1301void *gpiochip_get_data(struct gpio_chip *chip)
1302{
1303 return chip->gpiodev->data;
1304}
1305EXPORT_SYMBOL_GPL(gpiochip_get_data);
1306
1307/**
David Brownelld2876d02008-02-04 22:28:20 -08001308 * gpiochip_remove() - unregister a gpio_chip
1309 * @chip: the chip to unregister
1310 *
1311 * A gpio_chip with any GPIOs still requested may not be removed.
1312 */
abdoulaye berthee1db1702014-07-05 18:28:50 +02001313void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -08001314{
Linus Walleijff2b1352015-10-20 11:10:38 +02001315 struct gpio_device *gdev = chip->gpiodev;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001316 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001317 unsigned long flags;
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001318 unsigned i;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001319 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -08001320
Linus Walleijff2b1352015-10-20 11:10:38 +02001321 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
Linus Walleijafbc4f32016-02-09 13:21:06 +01001322 gpiochip_sysfs_unregister(gdev);
Geert Uytterhoeven5018ada2016-12-19 18:29:23 +01001323 gpiochip_free_hogs(chip);
Bamvor Jian Zhangbd203bd2016-02-20 13:13:19 +08001324 /* Numb the device, cancelling all outstanding operations */
1325 gdev->chip = NULL;
Johan Hovold00acc3d2015-01-12 17:12:27 +01001326 gpiochip_irqchip_remove(chip);
Mika Westerberg6072b9d2014-03-10 14:54:53 +02001327 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +01001328 gpiochip_remove_pin_ranges(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001329 of_gpiochip_remove(chip);
Linus Walleij43c54ec2016-02-11 11:37:48 +01001330 /*
1331 * We accept no more calls into the driver from this point, so
1332 * NULL the driver data pointer
1333 */
1334 gdev->data = NULL;
Anton Vorontsov391c9702010-06-08 07:48:17 -06001335
Johan Hovold6798aca2015-01-12 17:12:28 +01001336 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001337 for (i = 0; i < gdev->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001338 desc = &gdev->descs[i];
Johan Hovoldfab28b82015-05-04 17:10:27 +02001339 if (test_bit(FLAG_REQUESTED, &desc->flags))
1340 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -08001341 }
David Brownelld2876d02008-02-04 22:28:20 -08001342 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001343
Johan Hovoldfab28b82015-05-04 17:10:27 +02001344 if (requested)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001345 dev_crit(&gdev->dev,
Linus Walleij58383c782015-11-04 09:56:26 +01001346 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +02001347
Linus Walleijff2b1352015-10-20 11:10:38 +02001348 /*
1349 * The gpiochip side puts its use of the device to rest here:
1350 * if there are no userspace clients, the chardev and device will
1351 * be removed, else it will be dangling until the last user is
1352 * gone.
1353 */
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001354 cdev_device_del(&gdev->chrdev, &gdev->dev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001355 put_device(&gdev->dev);
David Brownelld2876d02008-02-04 22:28:20 -08001356}
1357EXPORT_SYMBOL_GPL(gpiochip_remove);
1358
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301359static void devm_gpio_chip_release(struct device *dev, void *res)
1360{
1361 struct gpio_chip *chip = *(struct gpio_chip **)res;
1362
1363 gpiochip_remove(chip);
1364}
1365
1366static int devm_gpio_chip_match(struct device *dev, void *res, void *data)
1367
1368{
1369 struct gpio_chip **r = res;
1370
1371 if (!r || !*r) {
1372 WARN_ON(!r || !*r);
1373 return 0;
1374 }
1375
1376 return *r == data;
1377}
1378
1379/**
Jonathan Neuschäfer689fd022017-12-21 17:56:34 +01001380 * devm_gpiochip_add_data() - Resource manager gpiochip_add_data()
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301381 * @dev: the device pointer on which irq_chip belongs to.
1382 * @chip: the chip to register, with chip->base initialized
Thierry Reding950d55f52017-07-24 16:57:22 +02001383 * @data: driver-private data associated with this chip
1384 *
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301385 * Context: potentially before irqs will work
1386 *
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301387 * The gpio chip automatically be released when the device is unbound.
Thierry Reding950d55f52017-07-24 16:57:22 +02001388 *
1389 * Returns:
1390 * A negative errno if the chip can't be registered, such as because the
1391 * chip->base is invalid or already associated with a different chip.
1392 * Otherwise it returns zero as a success code.
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301393 */
1394int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
1395 void *data)
1396{
1397 struct gpio_chip **ptr;
1398 int ret;
1399
1400 ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
1401 GFP_KERNEL);
1402 if (!ptr)
1403 return -ENOMEM;
1404
1405 ret = gpiochip_add_data(chip, data);
1406 if (ret < 0) {
1407 devres_free(ptr);
1408 return ret;
1409 }
1410
1411 *ptr = chip;
1412 devres_add(dev, ptr);
1413
1414 return 0;
1415}
1416EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
1417
1418/**
1419 * devm_gpiochip_remove() - Resource manager of gpiochip_remove()
1420 * @dev: device for which which resource was allocated
1421 * @chip: the chip to remove
1422 *
1423 * A gpio_chip with any GPIOs still requested may not be removed.
1424 */
1425void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip)
1426{
1427 int ret;
1428
1429 ret = devres_release(dev, devm_gpio_chip_release,
1430 devm_gpio_chip_match, chip);
Christophe JAILLET3988d662017-01-27 12:56:42 +01001431 WARN_ON(ret);
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301432}
1433EXPORT_SYMBOL_GPL(devm_gpiochip_remove);
1434
Grant Likely594fa262010-06-08 07:48:16 -06001435/**
1436 * gpiochip_find() - iterator for locating a specific gpio_chip
1437 * @data: data to pass to match function
Thierry Reding950d55f52017-07-24 16:57:22 +02001438 * @match: Callback function to check gpio_chip
Grant Likely594fa262010-06-08 07:48:16 -06001439 *
1440 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1441 * determined by a user supplied @match callback. The callback should return
1442 * 0 if the device doesn't match and non-zero if it does. If the callback is
1443 * non-zero, this function will return to the caller and not iterate over any
1444 * more gpio_chips.
1445 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001446struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001447 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001448 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001449{
Linus Walleijff2b1352015-10-20 11:10:38 +02001450 struct gpio_device *gdev;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001451 struct gpio_chip *chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -06001452 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001453
1454 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001455 list_for_each_entry(gdev, &gpio_devices, list)
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001456 if (gdev->chip && match(gdev->chip, data)) {
1457 chip = gdev->chip;
Grant Likely594fa262010-06-08 07:48:16 -06001458 break;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001459 }
Linus Walleijff2b1352015-10-20 11:10:38 +02001460
Grant Likely594fa262010-06-08 07:48:16 -06001461 spin_unlock_irqrestore(&gpio_lock, flags);
1462
1463 return chip;
1464}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001465EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001466
Alexandre Courbot79697ef2013-11-16 21:39:32 +09001467static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1468{
1469 const char *name = data;
1470
1471 return !strcmp(chip->label, name);
1472}
1473
1474static struct gpio_chip *find_chip_by_name(const char *name)
1475{
1476 return gpiochip_find((void *)name, gpiochip_match_name);
1477}
1478
Linus Walleij14250522014-03-25 10:40:18 +01001479#ifdef CONFIG_GPIOLIB_IRQCHIP
1480
1481/*
1482 * The following is irqchip helper code for gpiochips.
1483 */
1484
Mika Westerberg79b804c2016-09-20 15:15:21 +03001485static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1486{
Thierry Redingdc7b0382017-11-07 19:15:52 +01001487 if (!gpiochip->irq.need_valid_mask)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001488 return 0;
1489
Thierry Redingdc7b0382017-11-07 19:15:52 +01001490 gpiochip->irq.valid_mask = kcalloc(BITS_TO_LONGS(gpiochip->ngpio),
Mika Westerberg79b804c2016-09-20 15:15:21 +03001491 sizeof(long), GFP_KERNEL);
Thierry Redingdc7b0382017-11-07 19:15:52 +01001492 if (!gpiochip->irq.valid_mask)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001493 return -ENOMEM;
1494
1495 /* Assume by default all GPIOs are valid */
Thierry Redingdc7b0382017-11-07 19:15:52 +01001496 bitmap_fill(gpiochip->irq.valid_mask, gpiochip->ngpio);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001497
1498 return 0;
1499}
1500
1501static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1502{
Thierry Redingdc7b0382017-11-07 19:15:52 +01001503 kfree(gpiochip->irq.valid_mask);
1504 gpiochip->irq.valid_mask = NULL;
Mika Westerberg79b804c2016-09-20 15:15:21 +03001505}
1506
Stephen Boyd64ff2c82018-01-09 17:58:46 -08001507bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
1508 unsigned int offset)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001509{
1510 /* No mask means all valid */
Thierry Redingdc7b0382017-11-07 19:15:52 +01001511 if (likely(!gpiochip->irq.valid_mask))
Mika Westerberg79b804c2016-09-20 15:15:21 +03001512 return true;
Thierry Redingdc7b0382017-11-07 19:15:52 +01001513 return test_bit(offset, gpiochip->irq.valid_mask);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001514}
Stephen Boyd64ff2c82018-01-09 17:58:46 -08001515EXPORT_SYMBOL_GPL(gpiochip_irqchip_irq_valid);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001516
Linus Walleij14250522014-03-25 10:40:18 +01001517/**
Linus Walleijd245b3f2016-11-24 10:57:25 +01001518 * gpiochip_set_cascaded_irqchip() - connects a cascaded irqchip to a gpiochip
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001519 * @gpiochip: the gpiochip to set the irqchip chain to
1520 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01001521 * @parent_irq: the irq number corresponding to the parent IRQ for this
1522 * chained irqchip
1523 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001524 * coming out of the gpiochip. If the interrupt is nested rather than
1525 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +01001526 */
Linus Walleijd245b3f2016-11-24 10:57:25 +01001527static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gpiochip,
1528 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001529 unsigned int parent_irq,
Linus Walleijd245b3f2016-11-24 10:57:25 +01001530 irq_flow_handler_t parent_handler)
Linus Walleij14250522014-03-25 10:40:18 +01001531{
Linus Walleij83141a72014-09-26 13:50:12 +02001532 unsigned int offset;
1533
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001534 if (!gpiochip->irq.domain) {
Linus Walleij83141a72014-09-26 13:50:12 +02001535 chip_err(gpiochip, "called %s before setting up irqchip\n",
1536 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +02001537 return;
1538 }
1539
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001540 if (parent_handler) {
1541 if (gpiochip->can_sleep) {
1542 chip_err(gpiochip,
1543 "you cannot have chained interrupts on a "
1544 "chip that may sleep\n");
1545 return;
1546 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001547 /*
1548 * The parent irqchip is already using the chip_data for this
1549 * irqchip, so our callbacks simply use the handler_data.
1550 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +02001551 irq_set_chained_handler_and_data(parent_irq, parent_handler,
1552 gpiochip);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001553
Thierry Reding39e5f092017-11-07 19:15:50 +01001554 gpiochip->irq.parents = &parent_irq;
1555 gpiochip->irq.num_parents = 1;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001556 }
Linus Walleij83141a72014-09-26 13:50:12 +02001557
1558 /* Set the parent IRQ for all affected IRQs */
Mika Westerberg79b804c2016-09-20 15:15:21 +03001559 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1560 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1561 continue;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001562 irq_set_parent(irq_find_mapping(gpiochip->irq.domain, offset),
Linus Walleij83141a72014-09-26 13:50:12 +02001563 parent_irq);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001564 }
Linus Walleij14250522014-03-25 10:40:18 +01001565}
Linus Walleijd245b3f2016-11-24 10:57:25 +01001566
1567/**
1568 * gpiochip_set_chained_irqchip() - connects a chained irqchip to a gpiochip
1569 * @gpiochip: the gpiochip to set the irqchip chain to
1570 * @irqchip: the irqchip to chain to the gpiochip
1571 * @parent_irq: the irq number corresponding to the parent IRQ for this
1572 * chained irqchip
1573 * @parent_handler: the parent interrupt handler for the accumulated IRQ
1574 * coming out of the gpiochip. If the interrupt is nested rather than
1575 * cascaded, pass NULL in this handler argument
1576 */
1577void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
1578 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001579 unsigned int parent_irq,
Linus Walleijd245b3f2016-11-24 10:57:25 +01001580 irq_flow_handler_t parent_handler)
1581{
Thierry Reding60ed54c2017-11-07 19:15:57 +01001582 if (gpiochip->irq.threaded) {
1583 chip_err(gpiochip, "tried to chain a threaded gpiochip\n");
1584 return;
1585 }
1586
Linus Walleijd245b3f2016-11-24 10:57:25 +01001587 gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
1588 parent_handler);
1589}
Linus Walleij14250522014-03-25 10:40:18 +01001590EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
1591
1592/**
Linus Walleijd245b3f2016-11-24 10:57:25 +01001593 * gpiochip_set_nested_irqchip() - connects a nested irqchip to a gpiochip
1594 * @gpiochip: the gpiochip to set the irqchip nested handler to
1595 * @irqchip: the irqchip to nest to the gpiochip
1596 * @parent_irq: the irq number corresponding to the parent IRQ for this
1597 * nested irqchip
1598 */
1599void gpiochip_set_nested_irqchip(struct gpio_chip *gpiochip,
1600 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001601 unsigned int parent_irq)
Linus Walleijd245b3f2016-11-24 10:57:25 +01001602{
Linus Walleijd245b3f2016-11-24 10:57:25 +01001603 gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
1604 NULL);
1605}
1606EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip);
1607
1608/**
Linus Walleij14250522014-03-25 10:40:18 +01001609 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1610 * @d: the irqdomain used by this irqchip
1611 * @irq: the global irq number used by this GPIO irqchip irq
1612 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1613 *
1614 * This function will set up the mapping for a certain IRQ line on a
1615 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1616 * stored inside the gpiochip.
1617 */
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001618int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1619 irq_hw_number_t hwirq)
Linus Walleij14250522014-03-25 10:40:18 +01001620{
1621 struct gpio_chip *chip = d->host_data;
Thierry Redinge0d89722017-11-07 19:15:54 +01001622 int err = 0;
Linus Walleij14250522014-03-25 10:40:18 +01001623
Grygorii Strashkodc749a02017-07-21 11:49:00 -05001624 if (!gpiochip_irqchip_irq_valid(chip, hwirq))
1625 return -ENXIO;
1626
Linus Walleij14250522014-03-25 10:40:18 +01001627 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001628 /*
1629 * This lock class tells lockdep that GPIO irqs are in a different
1630 * category than their parents, so it won't report false recursion.
1631 */
Thierry Redingca9df052017-11-07 19:15:53 +01001632 irq_set_lockdep_class(irq, chip->irq.lock_key);
Thierry Redingc7a0aa52017-11-07 19:15:48 +01001633 irq_set_chip_and_handler(irq, chip->irq.chip, chip->irq.handler);
Linus Walleijd245b3f2016-11-24 10:57:25 +01001634 /* Chips that use nested thread handlers have them marked */
Thierry Reding60ed54c2017-11-07 19:15:57 +01001635 if (chip->irq.threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02001636 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +01001637 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -05001638
Thierry Redinge0d89722017-11-07 19:15:54 +01001639 if (chip->irq.num_parents == 1)
1640 err = irq_set_parent(irq, chip->irq.parents[0]);
1641 else if (chip->irq.map)
1642 err = irq_set_parent(irq, chip->irq.map[hwirq]);
1643
1644 if (err < 0)
1645 return err;
1646
Linus Walleij1333b902014-04-23 16:45:12 +02001647 /*
1648 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1649 * is passed as default type.
1650 */
Thierry Reding3634eeb2017-11-07 19:15:49 +01001651 if (chip->irq.default_type != IRQ_TYPE_NONE)
1652 irq_set_irq_type(irq, chip->irq.default_type);
Linus Walleij14250522014-03-25 10:40:18 +01001653
1654 return 0;
1655}
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001656EXPORT_SYMBOL_GPL(gpiochip_irq_map);
Linus Walleij14250522014-03-25 10:40:18 +01001657
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001658void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
Linus Walleijc3626fd2014-03-28 20:42:01 +01001659{
Linus Walleij1c8732b2014-04-09 13:34:39 +02001660 struct gpio_chip *chip = d->host_data;
1661
Thierry Reding60ed54c2017-11-07 19:15:57 +01001662 if (chip->irq.threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02001663 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001664 irq_set_chip_and_handler(irq, NULL, NULL);
1665 irq_set_chip_data(irq, NULL);
1666}
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001667EXPORT_SYMBOL_GPL(gpiochip_irq_unmap);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001668
Linus Walleij14250522014-03-25 10:40:18 +01001669static const struct irq_domain_ops gpiochip_domain_ops = {
1670 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +01001671 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +01001672 /* Virtually all GPIO irqchips are twocell:ed */
1673 .xlate = irq_domain_xlate_twocell,
1674};
1675
1676static int gpiochip_irq_reqres(struct irq_data *d)
1677{
1678 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1679
Linus Walleijff2b1352015-10-20 11:10:38 +02001680 if (!try_module_get(chip->gpiodev->owner))
Grygorii Strashko5b76e792015-06-25 20:30:50 +03001681 return -ENODEV;
1682
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001683 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +01001684 chip_err(chip,
1685 "unable to lock HW IRQ %lu for IRQ\n",
1686 d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001687 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001688 return -EINVAL;
1689 }
1690 return 0;
1691}
1692
1693static void gpiochip_irq_relres(struct irq_data *d)
1694{
1695 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1696
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001697 gpiochip_unlock_as_irq(chip, d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001698 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001699}
1700
1701static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
1702{
Grygorii Strashkodc749a02017-07-21 11:49:00 -05001703 if (!gpiochip_irqchip_irq_valid(chip, offset))
1704 return -ENXIO;
Thierry Redinge0d89722017-11-07 19:15:54 +01001705
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001706 return irq_create_mapping(chip->irq.domain, offset);
Linus Walleij14250522014-03-25 10:40:18 +01001707}
1708
1709/**
Thierry Redinge0d89722017-11-07 19:15:54 +01001710 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
1711 * @gpiochip: the GPIO chip to add the IRQ chip to
Thierry Reding959bc7b2017-11-07 19:15:59 +01001712 * @lock_key: lockdep class
Thierry Redinge0d89722017-11-07 19:15:54 +01001713 */
Thierry Reding959bc7b2017-11-07 19:15:59 +01001714static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
1715 struct lock_class_key *lock_key)
Thierry Redinge0d89722017-11-07 19:15:54 +01001716{
1717 struct irq_chip *irqchip = gpiochip->irq.chip;
1718 const struct irq_domain_ops *ops;
1719 struct device_node *np;
1720 unsigned int type;
1721 unsigned int i;
1722
1723 if (!irqchip)
1724 return 0;
1725
1726 if (gpiochip->irq.parent_handler && gpiochip->can_sleep) {
1727 chip_err(gpiochip, "you cannot have chained interrupts on a "
1728 "chip that may sleep\n");
1729 return -EINVAL;
1730 }
1731
1732 np = gpiochip->gpiodev->dev.of_node;
1733 type = gpiochip->irq.default_type;
1734
1735 /*
1736 * Specifying a default trigger is a terrible idea if DT or ACPI is
1737 * used to configure the interrupts, as you may end up with
1738 * conflicting triggers. Tell the user, and reset to NONE.
1739 */
1740 if (WARN(np && type != IRQ_TYPE_NONE,
1741 "%s: Ignoring %u default trigger\n", np->full_name, type))
1742 type = IRQ_TYPE_NONE;
1743
1744 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
1745 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1746 "Ignoring %u default trigger\n", type);
1747 type = IRQ_TYPE_NONE;
1748 }
1749
1750 gpiochip->to_irq = gpiochip_to_irq;
1751 gpiochip->irq.default_type = type;
Thierry Reding959bc7b2017-11-07 19:15:59 +01001752 gpiochip->irq.lock_key = lock_key;
Thierry Redinge0d89722017-11-07 19:15:54 +01001753
1754 if (gpiochip->irq.domain_ops)
1755 ops = gpiochip->irq.domain_ops;
1756 else
1757 ops = &gpiochip_domain_ops;
1758
1759 gpiochip->irq.domain = irq_domain_add_simple(np, gpiochip->ngpio,
Thierry Reding8302cf52017-11-07 19:15:58 +01001760 gpiochip->irq.first,
1761 ops, gpiochip);
Thierry Redinge0d89722017-11-07 19:15:54 +01001762 if (!gpiochip->irq.domain)
1763 return -EINVAL;
1764
1765 /*
1766 * It is possible for a driver to override this, but only if the
1767 * alternative functions are both implemented.
1768 */
1769 if (!irqchip->irq_request_resources &&
1770 !irqchip->irq_release_resources) {
1771 irqchip->irq_request_resources = gpiochip_irq_reqres;
1772 irqchip->irq_release_resources = gpiochip_irq_relres;
1773 }
1774
1775 if (gpiochip->irq.parent_handler) {
1776 void *data = gpiochip->irq.parent_handler_data ?: gpiochip;
1777
1778 for (i = 0; i < gpiochip->irq.num_parents; i++) {
1779 /*
1780 * The parent IRQ chip is already using the chip_data
1781 * for this IRQ chip, so our callbacks simply use the
1782 * handler_data.
1783 */
1784 irq_set_chained_handler_and_data(gpiochip->irq.parents[i],
1785 gpiochip->irq.parent_handler,
1786 data);
1787 }
Thierry Redinge0d89722017-11-07 19:15:54 +01001788 }
1789
1790 acpi_gpiochip_request_interrupts(gpiochip);
1791
1792 return 0;
1793}
1794
1795/**
Linus Walleij14250522014-03-25 10:40:18 +01001796 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1797 * @gpiochip: the gpiochip to remove the irqchip from
1798 *
1799 * This is called only from gpiochip_remove()
1800 */
1801static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
1802{
Thierry Reding39e5f092017-11-07 19:15:50 +01001803 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +01001804
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001805 acpi_gpiochip_free_interrupts(gpiochip);
1806
Thierry Redinge0d89722017-11-07 19:15:54 +01001807 if (gpiochip->irq.chip && gpiochip->irq.parent_handler) {
Thierry Reding39e5f092017-11-07 19:15:50 +01001808 struct gpio_irq_chip *irq = &gpiochip->irq;
1809 unsigned int i;
1810
1811 for (i = 0; i < irq->num_parents; i++)
1812 irq_set_chained_handler_and_data(irq->parents[i],
1813 NULL, NULL);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001814 }
1815
Linus Walleijc3626fd2014-03-28 20:42:01 +01001816 /* Remove all IRQ mappings and delete the domain */
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001817 if (gpiochip->irq.domain) {
Thierry Reding39e5f092017-11-07 19:15:50 +01001818 unsigned int irq;
1819
Mika Westerberg79b804c2016-09-20 15:15:21 +03001820 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1821 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1822 continue;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001823
1824 irq = irq_find_mapping(gpiochip->irq.domain, offset);
1825 irq_dispose_mapping(irq);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001826 }
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001827
1828 irq_domain_remove(gpiochip->irq.domain);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001829 }
Linus Walleij14250522014-03-25 10:40:18 +01001830
Thierry Redingda80ff82017-11-07 19:15:46 +01001831 if (gpiochip->irq.chip) {
1832 gpiochip->irq.chip->irq_request_resources = NULL;
1833 gpiochip->irq.chip->irq_release_resources = NULL;
1834 gpiochip->irq.chip = NULL;
Linus Walleij14250522014-03-25 10:40:18 +01001835 }
Mika Westerberg79b804c2016-09-20 15:15:21 +03001836
1837 gpiochip_irqchip_free_valid_mask(gpiochip);
Linus Walleij14250522014-03-25 10:40:18 +01001838}
1839
1840/**
Linus Walleij739e6f52017-01-11 13:37:07 +01001841 * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01001842 * @gpiochip: the gpiochip to add the irqchip to
1843 * @irqchip: the irqchip to add to the gpiochip
1844 * @first_irq: if not dynamically assigned, the base (first) IRQ to
1845 * allocate gpiochip irqs from
1846 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +02001847 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
1848 * to have the core avoid setting up any default type in the hardware.
Thierry Reding60ed54c2017-11-07 19:15:57 +01001849 * @threaded: whether this irqchip uses a nested thread handler
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001850 * @lock_key: lockdep class
Linus Walleij14250522014-03-25 10:40:18 +01001851 *
1852 * This function closely associates a certain irqchip with a certain
1853 * gpiochip, providing an irq domain to translate the local IRQs to
1854 * global irqs in the gpiolib core, and making sure that the gpiochip
1855 * is passed as chip data to all related functions. Driver callbacks
Linus Walleij09dd5f92015-12-07 15:31:58 +01001856 * need to use gpiochip_get_data() to get their local state containers back
Linus Walleij14250522014-03-25 10:40:18 +01001857 * from the gpiochip passed as chip data. An irqdomain will be stored
1858 * in the gpiochip that shall be used by the driver to handle IRQ number
1859 * translation. The gpiochip will need to be initialized and registered
1860 * before calling this function.
1861 *
Linus Walleijc3626fd2014-03-28 20:42:01 +01001862 * This function will handle two cell:ed simple IRQs and assumes all
1863 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +01001864 * need to be open coded.
1865 */
Linus Walleij739e6f52017-01-11 13:37:07 +01001866int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
1867 struct irq_chip *irqchip,
1868 unsigned int first_irq,
1869 irq_flow_handler_t handler,
1870 unsigned int type,
Thierry Reding60ed54c2017-11-07 19:15:57 +01001871 bool threaded,
Linus Walleij739e6f52017-01-11 13:37:07 +01001872 struct lock_class_key *lock_key)
Linus Walleij14250522014-03-25 10:40:18 +01001873{
1874 struct device_node *of_node;
Linus Walleij14250522014-03-25 10:40:18 +01001875
1876 if (!gpiochip || !irqchip)
1877 return -EINVAL;
1878
Linus Walleij58383c782015-11-04 09:56:26 +01001879 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +01001880 pr_err("missing gpiochip .dev parent pointer\n");
1881 return -EINVAL;
1882 }
Thierry Reding60ed54c2017-11-07 19:15:57 +01001883 gpiochip->irq.threaded = threaded;
Linus Walleij58383c782015-11-04 09:56:26 +01001884 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +01001885#ifdef CONFIG_OF_GPIO
1886 /*
Colin Cronin20a8a962015-05-18 11:41:43 -07001887 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08001888 * FIXME: get rid of this and use gpiochip->parent->of_node
1889 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +01001890 */
1891 if (gpiochip->of_node)
1892 of_node = gpiochip->of_node;
1893#endif
Marc Zyngier332e99d2016-09-07 09:12:11 +01001894 /*
Mika Westerberg0a1e0052016-09-12 14:29:51 +03001895 * Specifying a default trigger is a terrible idea if DT or ACPI is
Marc Zyngier332e99d2016-09-07 09:12:11 +01001896 * used to configure the interrupts, as you may end-up with
1897 * conflicting triggers. Tell the user, and reset to NONE.
1898 */
1899 if (WARN(of_node && type != IRQ_TYPE_NONE,
Rob Herring7eb6ce22017-07-18 16:43:03 -05001900 "%pOF: Ignoring %d default trigger\n", of_node, type))
Marc Zyngier332e99d2016-09-07 09:12:11 +01001901 type = IRQ_TYPE_NONE;
Mika Westerberg0a1e0052016-09-12 14:29:51 +03001902 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
1903 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1904 "Ignoring %d default trigger\n", type);
1905 type = IRQ_TYPE_NONE;
1906 }
Marc Zyngier332e99d2016-09-07 09:12:11 +01001907
Thierry Redingda80ff82017-11-07 19:15:46 +01001908 gpiochip->irq.chip = irqchip;
Thierry Redingc7a0aa52017-11-07 19:15:48 +01001909 gpiochip->irq.handler = handler;
Thierry Reding3634eeb2017-11-07 19:15:49 +01001910 gpiochip->irq.default_type = type;
Linus Walleij14250522014-03-25 10:40:18 +01001911 gpiochip->to_irq = gpiochip_to_irq;
Thierry Redingca9df052017-11-07 19:15:53 +01001912 gpiochip->irq.lock_key = lock_key;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001913 gpiochip->irq.domain = irq_domain_add_simple(of_node,
Linus Walleij14250522014-03-25 10:40:18 +01001914 gpiochip->ngpio, first_irq,
1915 &gpiochip_domain_ops, gpiochip);
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001916 if (!gpiochip->irq.domain) {
Thierry Redingda80ff82017-11-07 19:15:46 +01001917 gpiochip->irq.chip = NULL;
Linus Walleij14250522014-03-25 10:40:18 +01001918 return -EINVAL;
1919 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +02001920
1921 /*
1922 * It is possible for a driver to override this, but only if the
1923 * alternative functions are both implemented.
1924 */
1925 if (!irqchip->irq_request_resources &&
1926 !irqchip->irq_release_resources) {
1927 irqchip->irq_request_resources = gpiochip_irq_reqres;
1928 irqchip->irq_release_resources = gpiochip_irq_relres;
1929 }
Linus Walleij14250522014-03-25 10:40:18 +01001930
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001931 acpi_gpiochip_request_interrupts(gpiochip);
1932
Linus Walleij14250522014-03-25 10:40:18 +01001933 return 0;
1934}
Linus Walleij739e6f52017-01-11 13:37:07 +01001935EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key);
Linus Walleij14250522014-03-25 10:40:18 +01001936
1937#else /* CONFIG_GPIOLIB_IRQCHIP */
1938
Thierry Reding959bc7b2017-11-07 19:15:59 +01001939static inline int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
Linus Walleijec478732017-11-08 21:40:41 +01001940 struct lock_class_key *key)
Thierry Redinge0d89722017-11-07 19:15:54 +01001941{
1942 return 0;
1943}
1944
Linus Walleij14250522014-03-25 10:40:18 +01001945static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
Mika Westerberg79b804c2016-09-20 15:15:21 +03001946static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1947{
1948 return 0;
1949}
1950static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1951{ }
Linus Walleij14250522014-03-25 10:40:18 +01001952
1953#endif /* CONFIG_GPIOLIB_IRQCHIP */
1954
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001955/**
1956 * gpiochip_generic_request() - request the gpio function for a pin
1957 * @chip: the gpiochip owning the GPIO
1958 * @offset: the offset of the GPIO to request for GPIO function
1959 */
1960int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
1961{
Linus Walleija9a1d2a2017-09-22 11:02:10 +02001962 return pinctrl_gpio_request(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001963}
1964EXPORT_SYMBOL_GPL(gpiochip_generic_request);
1965
1966/**
1967 * gpiochip_generic_free() - free the gpio function from a pin
1968 * @chip: the gpiochip to request the gpio function for
1969 * @offset: the offset of the GPIO to free from GPIO function
1970 */
1971void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
1972{
Linus Walleija9a1d2a2017-09-22 11:02:10 +02001973 pinctrl_gpio_free(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001974}
1975EXPORT_SYMBOL_GPL(gpiochip_generic_free);
1976
Mika Westerberg2956b5d2017-01-23 15:34:34 +03001977/**
1978 * gpiochip_generic_config() - apply configuration for a pin
1979 * @chip: the gpiochip owning the GPIO
1980 * @offset: the offset of the GPIO to apply the configuration
1981 * @config: the configuration to be applied
1982 */
1983int gpiochip_generic_config(struct gpio_chip *chip, unsigned offset,
1984 unsigned long config)
1985{
1986 return pinctrl_gpio_set_config(chip->gpiodev->base + offset, config);
1987}
1988EXPORT_SYMBOL_GPL(gpiochip_generic_config);
1989
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301990#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01001991
Linus Walleij3f0f8672012-11-20 12:40:15 +01001992/**
Christian Ruppert586a87e2013-10-15 15:37:54 +02001993 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1994 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +02001995 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +02001996 * @gpio_offset: the start offset in the current gpio_chip number space
1997 * @pin_group: name of the pin group inside the pin controller
1998 */
1999int gpiochip_add_pingroup_range(struct gpio_chip *chip,
2000 struct pinctrl_dev *pctldev,
2001 unsigned int gpio_offset, const char *pin_group)
2002{
2003 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002004 struct gpio_device *gdev = chip->gpiodev;
Christian Ruppert586a87e2013-10-15 15:37:54 +02002005 int ret;
2006
2007 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
2008 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002009 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +02002010 return -ENOMEM;
2011 }
2012
2013 /* Use local offset as range ID */
2014 pin_range->range.id = gpio_offset;
2015 pin_range->range.gc = chip;
2016 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002017 pin_range->range.base = gdev->base + gpio_offset;
Christian Ruppert586a87e2013-10-15 15:37:54 +02002018 pin_range->pctldev = pctldev;
2019
2020 ret = pinctrl_get_group_pins(pctldev, pin_group,
2021 &pin_range->range.pins,
2022 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +01002023 if (ret < 0) {
2024 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +02002025 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +01002026 }
Christian Ruppert586a87e2013-10-15 15:37:54 +02002027
2028 pinctrl_add_gpio_range(pctldev, &pin_range->range);
2029
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002030 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
2031 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +02002032 pinctrl_dev_get_devname(pctldev), pin_group);
2033
Linus Walleij20ec3e32016-02-11 11:03:06 +01002034 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Christian Ruppert586a87e2013-10-15 15:37:54 +02002035
2036 return 0;
2037}
2038EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
2039
2040/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01002041 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
2042 * @chip: the gpiochip to add the range for
Thierry Reding950d55f52017-07-24 16:57:22 +02002043 * @pinctl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01002044 * @gpio_offset: the start offset in the current gpio_chip number space
2045 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01002046 * @npins: the number of pins from the offset of each pin space (GPIO and
2047 * pin controller) to accumulate in this range
Thierry Reding950d55f52017-07-24 16:57:22 +02002048 *
2049 * Returns:
2050 * 0 on success, or a negative error-code on failure.
Linus Walleij3f0f8672012-11-20 12:40:15 +01002051 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002052int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01002053 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01002054 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302055{
2056 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002057 struct gpio_device *gdev = chip->gpiodev;
Axel Linb4d4b1f2012-11-21 14:33:56 +08002058 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302059
Linus Walleij3f0f8672012-11-20 12:40:15 +01002060 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302061 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002062 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002063 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302064 }
2065
Linus Walleij3f0f8672012-11-20 12:40:15 +01002066 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01002067 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01002068 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302069 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002070 pin_range->range.base = gdev->base + gpio_offset;
Linus Walleij316511c2012-11-21 08:48:09 +01002071 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302072 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01002073 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302074 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01002075 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08002076 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002077 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +01002078 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08002079 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01002080 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002081 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
2082 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +01002083 pinctl_name,
2084 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302085
Linus Walleij20ec3e32016-02-11 11:03:06 +01002086 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002087
2088 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302089}
Linus Walleij165adc92012-11-06 14:49:39 +01002090EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302091
Linus Walleij3f0f8672012-11-20 12:40:15 +01002092/**
2093 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
2094 * @chip: the chip to remove all the mappings for
2095 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302096void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
2097{
2098 struct gpio_pin_range *pin_range, *tmp;
Linus Walleij20ec3e32016-02-11 11:03:06 +01002099 struct gpio_device *gdev = chip->gpiodev;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302100
Linus Walleij20ec3e32016-02-11 11:03:06 +01002101 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302102 list_del(&pin_range->node);
2103 pinctrl_remove_gpio_range(pin_range->pctldev,
2104 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01002105 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302106 }
2107}
Linus Walleij165adc92012-11-06 14:49:39 +01002108EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
2109
2110#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302111
David Brownelld2876d02008-02-04 22:28:20 -08002112/* These "optional" allocation calls help prevent drivers from stomping
2113 * on each other, and help provide better diagnostics in debugfs.
2114 * They're called even less than the "set direction" calls.
2115 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002116static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08002117{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002118 struct gpio_chip *chip = desc->gdev->chip;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002119 int status;
David Brownelld2876d02008-02-04 22:28:20 -08002120 unsigned long flags;
2121
2122 spin_lock_irqsave(&gpio_lock, flags);
2123
David Brownelld2876d02008-02-04 22:28:20 -08002124 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07002125 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08002126 */
2127
2128 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
2129 desc_set_label(desc, label ? : "?");
2130 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07002131 } else {
David Brownelld2876d02008-02-04 22:28:20 -08002132 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -08002133 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07002134 }
2135
2136 if (chip->request) {
2137 /* chip->request may sleep */
2138 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002139 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07002140 spin_lock_irqsave(&gpio_lock, flags);
2141
2142 if (status < 0) {
2143 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -07002144 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03002145 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07002146 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07002147 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03002148 if (chip->get_direction) {
2149 /* chip->get_direction may sleep */
2150 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002151 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03002152 spin_lock_irqsave(&gpio_lock, flags);
2153 }
David Brownelld2876d02008-02-04 22:28:20 -08002154done:
Mika Westerberg77c2d792014-03-10 14:54:50 +02002155 spin_unlock_irqrestore(&gpio_lock, flags);
2156 return status;
2157}
2158
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002159/*
2160 * This descriptor validation needs to be inserted verbatim into each
2161 * function taking a descriptor, so we need to use a preprocessor
Linus Walleij54d77192016-05-30 16:48:39 +02002162 * macro to avoid endless duplication. If the desc is NULL it is an
2163 * optional GPIO and calls should just bail out.
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002164 */
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002165static int validate_desc(const struct gpio_desc *desc, const char *func)
2166{
2167 if (!desc)
2168 return 0;
2169 if (IS_ERR(desc)) {
2170 pr_warn("%s: invalid GPIO (errorpointer)\n", func);
2171 return PTR_ERR(desc);
2172 }
2173 if (!desc->gdev) {
2174 pr_warn("%s: invalid GPIO (no device)\n", func);
2175 return -EINVAL;
2176 }
2177 if (!desc->gdev->chip) {
2178 dev_warn(&desc->gdev->dev,
2179 "%s: backing chip is gone\n", func);
2180 return 0;
2181 }
2182 return 1;
2183}
2184
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002185#define VALIDATE_DESC(desc) do { \
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002186 int __valid = validate_desc(desc, __func__); \
2187 if (__valid <= 0) \
2188 return __valid; \
2189 } while (0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002190
2191#define VALIDATE_DESC_VOID(desc) do { \
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002192 int __valid = validate_desc(desc, __func__); \
2193 if (__valid <= 0) \
Linus Walleij54d77192016-05-30 16:48:39 +02002194 return; \
Rasmus Villemoesa746a232017-12-21 01:27:04 +01002195 } while (0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002196
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002197int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002198{
2199 int status = -EPROBE_DEFER;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002200 struct gpio_device *gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002201
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002202 VALIDATE_DESC(desc);
2203 gdev = desc->gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002204
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002205 if (try_module_get(gdev->owner)) {
Linus Walleijfac9d882017-09-26 20:58:28 +02002206 status = gpiod_request_commit(desc, label);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002207 if (status < 0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002208 module_put(gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002209 else
2210 get_device(&gdev->dev);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002211 }
2212
David Brownelld2876d02008-02-04 22:28:20 -08002213 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +02002214 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002215
David Brownelld2876d02008-02-04 22:28:20 -08002216 return status;
2217}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002218
Linus Walleijfac9d882017-09-26 20:58:28 +02002219static bool gpiod_free_commit(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002220{
Mika Westerberg77c2d792014-03-10 14:54:50 +02002221 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08002222 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07002223 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002224
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07002225 might_sleep();
2226
Alexandre Courbot372e7222013-02-03 01:29:29 +09002227 gpiod_unexport(desc);
David Brownelld8f388d82008-07-25 01:46:07 -07002228
David Brownelld2876d02008-02-04 22:28:20 -08002229 spin_lock_irqsave(&gpio_lock, flags);
2230
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002231 chip = desc->gdev->chip;
David Brownell35e8bb52008-10-15 22:03:16 -07002232 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
2233 if (chip->free) {
2234 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07002235 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002236 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07002237 spin_lock_irqsave(&gpio_lock, flags);
2238 }
David Brownelld2876d02008-02-04 22:28:20 -08002239 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08002240 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07002241 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302242 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302243 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06002244 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002245 ret = true;
2246 }
David Brownelld2876d02008-02-04 22:28:20 -08002247
2248 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002249 return ret;
2250}
2251
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002252void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002253{
Linus Walleijfac9d882017-09-26 20:58:28 +02002254 if (desc && desc->gdev && gpiod_free_commit(desc)) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002255 module_put(desc->gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002256 put_device(&desc->gdev->dev);
2257 } else {
Mika Westerberg77c2d792014-03-10 14:54:50 +02002258 WARN_ON(extra_checks);
Linus Walleij33a68e82016-02-11 10:28:44 +01002259 }
David Brownelld2876d02008-02-04 22:28:20 -08002260}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002261
David Brownelld2876d02008-02-04 22:28:20 -08002262/**
2263 * gpiochip_is_requested - return string iff signal was requested
2264 * @chip: controller managing the signal
2265 * @offset: of signal within controller's 0..(ngpio - 1) range
2266 *
2267 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09002268 * The string returned is the label passed to gpio_request(); if none has been
2269 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08002270 *
2271 * This function is for use by GPIO controller drivers. The label can
2272 * help with diagnostics, and knowing that the signal is used as a GPIO
2273 * can help avoid accidentally multiplexing it to another controller.
2274 */
2275const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
2276{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002277 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08002278
Dirk Behme48b59532015-08-18 18:02:32 +02002279 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08002280 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002281
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002282 desc = &chip->gpiodev->descs[offset];
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002283
Alexandre Courbot372e7222013-02-03 01:29:29 +09002284 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08002285 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002286 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08002287}
2288EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2289
Mika Westerberg77c2d792014-03-10 14:54:50 +02002290/**
2291 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
Thierry Reding950d55f52017-07-24 16:57:22 +02002292 * @chip: GPIO chip
2293 * @hwnum: hardware number of the GPIO for which to request the descriptor
Mika Westerberg77c2d792014-03-10 14:54:50 +02002294 * @label: label for the GPIO
2295 *
2296 * Function allows GPIO chip drivers to request and use their own GPIO
2297 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2298 * function will not increase reference count of the GPIO chip module. This
2299 * allows the GPIO chip module to be unloaded as needed (we assume that the
2300 * GPIO chip driver handles freeing the GPIOs it has requested).
Thierry Reding950d55f52017-07-24 16:57:22 +02002301 *
2302 * Returns:
2303 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2304 * code on failure.
Mika Westerberg77c2d792014-03-10 14:54:50 +02002305 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002306struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
2307 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002308{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002309 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
2310 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002311
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002312 if (IS_ERR(desc)) {
2313 chip_err(chip, "failed to get GPIO descriptor\n");
2314 return desc;
2315 }
2316
Linus Walleijfac9d882017-09-26 20:58:28 +02002317 err = gpiod_request_commit(desc, label);
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002318 if (err < 0)
2319 return ERR_PTR(err);
2320
2321 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002322}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002323EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002324
2325/**
2326 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2327 * @desc: GPIO descriptor to free
2328 *
2329 * Function frees the given GPIO requested previously with
2330 * gpiochip_request_own_desc().
2331 */
2332void gpiochip_free_own_desc(struct gpio_desc *desc)
2333{
2334 if (desc)
Linus Walleijfac9d882017-09-26 20:58:28 +02002335 gpiod_free_commit(desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002336}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002337EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08002338
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002339/*
2340 * Drivers MUST set GPIO direction before making get/set calls. In
David Brownelld2876d02008-02-04 22:28:20 -08002341 * some cases this is done in early boot, before IRQs are enabled.
2342 *
2343 * As a rule these aren't called more than once (except for drivers
2344 * using the open-drain emulation idiom) so these are natural places
2345 * to accumulate extra debugging checks. Note that we can't (yet)
2346 * rely on gpio_request() having been called beforehand.
2347 */
2348
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002349/**
2350 * gpiod_direction_input - set the GPIO direction to input
2351 * @desc: GPIO to set to input
2352 *
2353 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2354 * be called safely on it.
2355 *
2356 * Return 0 in case of success, else an error code.
2357 */
2358int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002359{
David Brownelld2876d02008-02-04 22:28:20 -08002360 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002361 int status = -EINVAL;
2362
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002363 VALIDATE_DESC(desc);
2364 chip = desc->gdev->chip;
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002365
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002366 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01002367 gpiod_warn(desc,
2368 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02002369 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002370 return -EIO;
2371 }
2372
Alexandre Courbotd82da792014-07-22 16:17:43 +09002373 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -08002374 if (status == 0)
2375 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002376
Alexandre Courbot372e7222013-02-03 01:29:29 +09002377 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09002378
David Brownelld2876d02008-02-04 22:28:20 -08002379 return status;
2380}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002381EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002382
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002383static int gpio_set_drive_single_ended(struct gpio_chip *gc, unsigned offset,
2384 enum pin_config_param mode)
2385{
2386 unsigned long config = { PIN_CONF_PACKED(mode, 0) };
2387
2388 return gc->set_config ? gc->set_config(gc, offset, config) : -ENOTSUPP;
2389}
2390
Linus Walleijfac9d882017-09-26 20:58:28 +02002391static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08002392{
Linus Walleijc663e5f2016-03-22 10:51:16 +01002393 struct gpio_chip *gc = desc->gdev->chip;
Linus Walleijad177312016-11-13 23:02:44 +01002394 int val = !!value;
Linus Walleijc663e5f2016-03-22 10:51:16 +01002395 int ret;
David Brownelld2876d02008-02-04 22:28:20 -08002396
Linus Walleijc663e5f2016-03-22 10:51:16 +01002397 if (!gc->set || !gc->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01002398 gpiod_warn(desc,
2399 "%s: missing set() or direction_output() operations\n",
2400 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002401 return -EIO;
2402 }
2403
Linus Walleijad177312016-11-13 23:02:44 +01002404 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
Linus Walleijc663e5f2016-03-22 10:51:16 +01002405 if (!ret)
David Brownelld2876d02008-02-04 22:28:20 -08002406 set_bit(FLAG_IS_OUT, &desc->flags);
Linus Walleijad177312016-11-13 23:02:44 +01002407 trace_gpio_value(desc_to_gpio(desc), 0, val);
Linus Walleijc663e5f2016-03-22 10:51:16 +01002408 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2409 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08002410}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002411
2412/**
2413 * gpiod_direction_output_raw - set the GPIO direction to output
2414 * @desc: GPIO to set to output
2415 * @value: initial output value of the GPIO
2416 *
2417 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2418 * be called safely on it. The initial value of the output must be specified
2419 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2420 *
2421 * Return 0 in case of success, else an error code.
2422 */
2423int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2424{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002425 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02002426 return gpiod_direction_output_raw_commit(desc, value);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002427}
2428EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2429
2430/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05302431 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002432 * @desc: GPIO to set to output
2433 * @value: initial output value of the GPIO
2434 *
2435 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2436 * be called safely on it. The initial value of the output must be specified
2437 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2438 * account.
2439 *
2440 * Return 0 in case of success, else an error code.
2441 */
2442int gpiod_direction_output(struct gpio_desc *desc, int value)
2443{
Vladimir Zapolskiy30322bc2017-12-21 18:37:24 +02002444 struct gpio_chip *gc;
Linus Walleij02e47982017-09-26 21:20:23 +02002445 int ret;
2446
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002447 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002448 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2449 value = !value;
Linus Walleijad177312016-11-13 23:02:44 +01002450 else
2451 value = !!value;
Linus Walleij02e47982017-09-26 21:20:23 +02002452
2453 /* GPIOs used for IRQs shall not be set as output */
2454 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
2455 gpiod_err(desc,
2456 "%s: tried to set a GPIO tied to an IRQ as output\n",
2457 __func__);
2458 return -EIO;
2459 }
2460
Vladimir Zapolskiy30322bc2017-12-21 18:37:24 +02002461 gc = desc->gdev->chip;
Linus Walleij02e47982017-09-26 21:20:23 +02002462 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2463 /* First see if we can enable open drain in hardware */
2464 ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2465 PIN_CONFIG_DRIVE_OPEN_DRAIN);
2466 if (!ret)
2467 goto set_output_value;
2468 /* Emulate open drain by not actively driving the line high */
2469 if (value)
2470 return gpiod_direction_input(desc);
2471 }
2472 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2473 ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2474 PIN_CONFIG_DRIVE_OPEN_SOURCE);
2475 if (!ret)
2476 goto set_output_value;
2477 /* Emulate open source by not actively driving the line low */
2478 if (!value)
2479 return gpiod_direction_input(desc);
2480 } else {
2481 gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2482 PIN_CONFIG_DRIVE_PUSH_PULL);
2483 }
2484
2485set_output_value:
Linus Walleijfac9d882017-09-26 20:58:28 +02002486 return gpiod_direction_output_raw_commit(desc, value);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002487}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002488EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08002489
Felipe Balbic4b5be92010-05-26 14:42:23 -07002490/**
Thierry Reding950d55f52017-07-24 16:57:22 +02002491 * gpiod_set_debounce - sets @debounce time for a GPIO
2492 * @desc: descriptor of the GPIO for which to set debounce time
2493 * @debounce: debounce time in microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02002494 *
Thierry Reding950d55f52017-07-24 16:57:22 +02002495 * Returns:
2496 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2497 * debounce time.
Felipe Balbic4b5be92010-05-26 14:42:23 -07002498 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002499int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07002500{
Felipe Balbic4b5be92010-05-26 14:42:23 -07002501 struct gpio_chip *chip;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002502 unsigned long config;
Felipe Balbic4b5be92010-05-26 14:42:23 -07002503
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002504 VALIDATE_DESC(desc);
2505 chip = desc->gdev->chip;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002506 if (!chip->set || !chip->set_config) {
Mark Brown6424de52013-09-09 10:33:49 +01002507 gpiod_dbg(desc,
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002508 "%s: missing set() or set_config() operations\n",
Mark Brown6424de52013-09-09 10:33:49 +01002509 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02002510 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002511 }
2512
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002513 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
2514 return chip->set_config(chip, gpio_chip_hwgpio(desc), config);
Felipe Balbic4b5be92010-05-26 14:42:23 -07002515}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002516EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002517
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002518/**
Andrew Jefferye10f72b2017-11-30 14:25:24 +10302519 * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
2520 * @desc: descriptor of the GPIO for which to configure persistence
2521 * @transitory: True to lose state on suspend or reset, false for persistence
2522 *
2523 * Returns:
2524 * 0 on success, otherwise a negative error code.
2525 */
2526int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
2527{
2528 struct gpio_chip *chip;
2529 unsigned long packed;
2530 int gpio;
2531 int rc;
2532
Vladimir Zapolskiy156dd392017-12-21 18:37:35 +02002533 VALIDATE_DESC(desc);
Andrew Jefferye10f72b2017-11-30 14:25:24 +10302534 /*
2535 * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
2536 * persistence state.
2537 */
2538 if (transitory)
2539 set_bit(FLAG_TRANSITORY, &desc->flags);
2540 else
2541 clear_bit(FLAG_TRANSITORY, &desc->flags);
2542
2543 /* If the driver supports it, set the persistence state now */
2544 chip = desc->gdev->chip;
2545 if (!chip->set_config)
2546 return 0;
2547
2548 packed = pinconf_to_config_packed(PIN_CONFIG_PERSIST_STATE,
2549 !transitory);
2550 gpio = gpio_chip_hwgpio(desc);
2551 rc = chip->set_config(chip, gpio, packed);
2552 if (rc == -ENOTSUPP) {
2553 dev_dbg(&desc->gdev->dev, "Persistence not supported for GPIO %d\n",
2554 gpio);
2555 return 0;
2556 }
2557
2558 return rc;
2559}
2560EXPORT_SYMBOL_GPL(gpiod_set_transitory);
2561
2562/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002563 * gpiod_is_active_low - test whether a GPIO is active-low or not
2564 * @desc: the gpio descriptor to test
2565 *
2566 * Returns 1 if the GPIO is active-low, 0 otherwise.
2567 */
2568int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002569{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002570 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002571 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002572}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002573EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08002574
2575/* I/O calls are only valid after configuration completed; the relevant
2576 * "is this a valid GPIO" error checks should already have been done.
2577 *
2578 * "Get" operations are often inlinable as reading a pin value register,
2579 * and masking the relevant bit in that register.
2580 *
2581 * When "set" operations are inlinable, they involve writing that mask to
2582 * one register to set a low value, or a different register to set it high.
2583 * Otherwise locking is needed, so there may be little value to inlining.
2584 *
2585 *------------------------------------------------------------------------
2586 *
2587 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2588 * have requested the GPIO. That can include implicit requesting by
2589 * a direction setting call. Marking a gpio as requested locks its chip
2590 * in memory, guaranteeing that these table lookups need no more locking
2591 * and that gpiochip_remove() will fail.
2592 *
2593 * REVISIT when debugging, consider adding some instrumentation to ensure
2594 * that the GPIO was actually requested.
2595 */
2596
Linus Walleijfac9d882017-09-26 20:58:28 +02002597static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002598{
2599 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002600 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002601 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002602
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002603 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002604 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002605 value = chip->get ? chip->get(chip, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01002606 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002607 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002608 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002609}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002610
Lukas Wunnereec1d562017-10-12 12:40:10 +02002611static int gpio_chip_get_multiple(struct gpio_chip *chip,
2612 unsigned long *mask, unsigned long *bits)
2613{
2614 if (chip->get_multiple) {
2615 return chip->get_multiple(chip, mask, bits);
2616 } else if (chip->get) {
2617 int i, value;
2618
2619 for_each_set_bit(i, mask, chip->ngpio) {
2620 value = chip->get(chip, i);
2621 if (value < 0)
2622 return value;
2623 __assign_bit(i, bits, value);
2624 }
2625 return 0;
2626 }
2627 return -EIO;
2628}
2629
2630int gpiod_get_array_value_complex(bool raw, bool can_sleep,
2631 unsigned int array_size,
2632 struct gpio_desc **desc_array,
2633 int *value_array)
2634{
2635 int i = 0;
2636
2637 while (i < array_size) {
2638 struct gpio_chip *chip = desc_array[i]->gdev->chip;
2639 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2640 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2641 int first, j, ret;
2642
2643 if (!can_sleep)
2644 WARN_ON(chip->can_sleep);
2645
2646 /* collect all inputs belonging to the same chip */
2647 first = i;
2648 memset(mask, 0, sizeof(mask));
2649 do {
2650 const struct gpio_desc *desc = desc_array[i];
2651 int hwgpio = gpio_chip_hwgpio(desc);
2652
2653 __set_bit(hwgpio, mask);
2654 i++;
2655 } while ((i < array_size) &&
2656 (desc_array[i]->gdev->chip == chip));
2657
2658 ret = gpio_chip_get_multiple(chip, mask, bits);
2659 if (ret)
2660 return ret;
2661
2662 for (j = first; j < i; j++) {
2663 const struct gpio_desc *desc = desc_array[j];
2664 int hwgpio = gpio_chip_hwgpio(desc);
2665 int value = test_bit(hwgpio, bits);
2666
2667 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2668 value = !value;
2669 value_array[j] = value;
2670 trace_gpio_value(desc_to_gpio(desc), 1, value);
2671 }
2672 }
2673 return 0;
2674}
2675
David Brownelld2876d02008-02-04 22:28:20 -08002676/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002677 * gpiod_get_raw_value() - return a gpio's raw value
2678 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08002679 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002680 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002681 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002682 *
2683 * This function should be called from contexts where we cannot sleep, and will
2684 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002685 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002686int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002687{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002688 VALIDATE_DESC(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002689 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002690 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02002691 return gpiod_get_raw_value_commit(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002692}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002693EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002694
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002695/**
2696 * gpiod_get_value() - return a gpio's value
2697 * @desc: gpio whose value will be returned
2698 *
2699 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002700 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002701 *
2702 * This function should be called from contexts where we cannot sleep, and will
2703 * complain if the GPIO chip functions potentially sleep.
2704 */
2705int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002706{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002707 int value;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002708
2709 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002710 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002711 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002712
Linus Walleijfac9d882017-09-26 20:58:28 +02002713 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002714 if (value < 0)
2715 return value;
2716
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002717 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2718 value = !value;
2719
2720 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002721}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002722EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08002723
Lukas Wunnereec1d562017-10-12 12:40:10 +02002724/**
2725 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
2726 * @array_size: number of elements in the descriptor / value arrays
2727 * @desc_array: array of GPIO descriptors whose values will be read
2728 * @value_array: array to store the read values
2729 *
2730 * Read the raw values of the GPIOs, i.e. the values of the physical lines
2731 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
2732 * else an error code.
2733 *
2734 * This function should be called from contexts where we cannot sleep,
2735 * and it will complain if the GPIO chip functions potentially sleep.
2736 */
2737int gpiod_get_raw_array_value(unsigned int array_size,
2738 struct gpio_desc **desc_array, int *value_array)
2739{
2740 if (!desc_array)
2741 return -EINVAL;
2742 return gpiod_get_array_value_complex(true, false, array_size,
2743 desc_array, value_array);
2744}
2745EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
2746
2747/**
2748 * gpiod_get_array_value() - read values from an array of GPIOs
2749 * @array_size: number of elements in the descriptor / value arrays
2750 * @desc_array: array of GPIO descriptors whose values will be read
2751 * @value_array: array to store the read values
2752 *
2753 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2754 * into account. Return 0 in case of success, else an error code.
2755 *
2756 * This function should be called from contexts where we cannot sleep,
2757 * and it will complain if the GPIO chip functions potentially sleep.
2758 */
2759int gpiod_get_array_value(unsigned int array_size,
2760 struct gpio_desc **desc_array, int *value_array)
2761{
2762 if (!desc_array)
2763 return -EINVAL;
2764 return gpiod_get_array_value_complex(false, false, array_size,
2765 desc_array, value_array);
2766}
2767EXPORT_SYMBOL_GPL(gpiod_get_array_value);
2768
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302769/*
Linus Walleijfac9d882017-09-26 20:58:28 +02002770 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002771 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002772 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302773 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002774static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302775{
2776 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002777 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002778 int offset = gpio_chip_hwgpio(desc);
2779
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302780 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002781 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302782 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002783 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302784 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002785 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302786 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002787 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302788 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002789 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302790 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002791 gpiod_err(desc,
2792 "%s: Error in set_value for open drain err %d\n",
2793 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302794}
2795
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302796/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002797 * _gpio_set_open_source_value() - Set the open source gpio's value.
2798 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002799 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302800 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002801static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302802{
2803 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002804 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002805 int offset = gpio_chip_hwgpio(desc);
2806
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302807 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002808 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302809 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002810 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302811 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002812 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302813 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002814 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302815 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002816 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302817 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002818 gpiod_err(desc,
2819 "%s: Error in set_value for open source err %d\n",
2820 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302821}
2822
Linus Walleijfac9d882017-09-26 20:58:28 +02002823static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08002824{
2825 struct gpio_chip *chip;
2826
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002827 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002828 trace_gpio_value(desc_to_gpio(desc), 0, value);
Linus Walleij02e47982017-09-26 21:20:23 +02002829 chip->set(chip, gpio_chip_hwgpio(desc), value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002830}
2831
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002832/*
2833 * set multiple outputs on the same chip;
2834 * use the chip's set_multiple function if available;
2835 * otherwise set the outputs sequentially;
2836 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
2837 * defines which outputs are to be changed
2838 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
2839 * defines the values the outputs specified by mask are to be set to
2840 */
2841static void gpio_chip_set_multiple(struct gpio_chip *chip,
2842 unsigned long *mask, unsigned long *bits)
2843{
2844 if (chip->set_multiple) {
2845 chip->set_multiple(chip, mask, bits);
2846 } else {
Andy Shevchenko5e4e6fb2017-01-03 19:01:17 +02002847 unsigned int i;
2848
2849 /* set outputs if the corresponding mask bit is set */
2850 for_each_set_bit(i, mask, chip->ngpio)
2851 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002852 }
2853}
2854
Linus Walleij44c72882016-04-24 11:36:59 +02002855void gpiod_set_array_value_complex(bool raw, bool can_sleep,
2856 unsigned int array_size,
2857 struct gpio_desc **desc_array,
2858 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002859{
2860 int i = 0;
2861
2862 while (i < array_size) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002863 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002864 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2865 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2866 int count = 0;
2867
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002868 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002869 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002870
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002871 memset(mask, 0, sizeof(mask));
2872 do {
2873 struct gpio_desc *desc = desc_array[i];
2874 int hwgpio = gpio_chip_hwgpio(desc);
2875 int value = value_array[i];
2876
2877 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2878 value = !value;
2879 trace_gpio_value(desc_to_gpio(desc), 0, value);
2880 /*
2881 * collect all normal outputs belonging to the same chip
2882 * open drain and open source outputs are set individually
2883 */
Linus Walleij02e47982017-09-26 21:20:23 +02002884 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02002885 gpio_set_open_drain_value_commit(desc, value);
Linus Walleij02e47982017-09-26 21:20:23 +02002886 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02002887 gpio_set_open_source_value_commit(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002888 } else {
2889 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002890 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002891 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002892 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002893 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002894 count++;
2895 }
2896 i++;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002897 } while ((i < array_size) &&
2898 (desc_array[i]->gdev->chip == chip));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002899 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002900 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002901 gpio_chip_set_multiple(chip, mask, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002902 }
2903}
2904
David Brownelld2876d02008-02-04 22:28:20 -08002905/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002906 * gpiod_set_raw_value() - assign a gpio's raw value
2907 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08002908 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002909 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002910 * Set the raw value of the GPIO, i.e. the value of its physical line without
2911 * regard for its ACTIVE_LOW status.
2912 *
2913 * This function should be called from contexts where we cannot sleep, and will
2914 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002915 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002916void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002917{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002918 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01002919 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002920 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02002921 gpiod_set_raw_value_commit(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002922}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002923EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002924
2925/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002926 * gpiod_set_value() - assign a gpio's value
2927 * @desc: gpio whose value will be assigned
2928 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002929 *
Linus Walleij02e47982017-09-26 21:20:23 +02002930 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
2931 * OPEN_DRAIN and OPEN_SOURCE flags into account.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002932 *
2933 * This function should be called from contexts where we cannot sleep, and will
2934 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002935 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002936void gpiod_set_value(struct gpio_desc *desc, int value)
2937{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002938 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01002939 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002940 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002941 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2942 value = !value;
Linus Walleij02e47982017-09-26 21:20:23 +02002943 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2944 gpio_set_open_drain_value_commit(desc, value);
2945 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2946 gpio_set_open_source_value_commit(desc, value);
2947 else
2948 gpiod_set_raw_value_commit(desc, value);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002949}
2950EXPORT_SYMBOL_GPL(gpiod_set_value);
2951
2952/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002953 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002954 * @array_size: number of elements in the descriptor / value arrays
2955 * @desc_array: array of GPIO descriptors whose values will be assigned
2956 * @value_array: array of values to assign
2957 *
2958 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2959 * without regard for their ACTIVE_LOW status.
2960 *
2961 * This function should be called from contexts where we cannot sleep, and will
2962 * complain if the GPIO chip functions potentially sleep.
2963 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002964void gpiod_set_raw_array_value(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002965 struct gpio_desc **desc_array, int *value_array)
2966{
2967 if (!desc_array)
2968 return;
Linus Walleij44c72882016-04-24 11:36:59 +02002969 gpiod_set_array_value_complex(true, false, array_size, desc_array,
2970 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002971}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002972EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002973
2974/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002975 * gpiod_set_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002976 * @array_size: number of elements in the descriptor / value arrays
2977 * @desc_array: array of GPIO descriptors whose values will be assigned
2978 * @value_array: array of values to assign
2979 *
2980 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2981 * into account.
2982 *
2983 * This function should be called from contexts where we cannot sleep, and will
2984 * complain if the GPIO chip functions potentially sleep.
2985 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002986void gpiod_set_array_value(unsigned int array_size,
2987 struct gpio_desc **desc_array, int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002988{
2989 if (!desc_array)
2990 return;
Linus Walleij44c72882016-04-24 11:36:59 +02002991 gpiod_set_array_value_complex(false, false, array_size, desc_array,
2992 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002993}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002994EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002995
2996/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002997 * gpiod_cansleep() - report whether gpio value access may sleep
2998 * @desc: gpio to check
2999 *
3000 */
3001int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003002{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003003 VALIDATE_DESC(desc);
3004 return desc->gdev->chip->can_sleep;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003005}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003006EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08003007
David Brownell0f6d5042008-10-15 22:03:14 -07003008/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003009 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
3010 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07003011 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003012 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
3013 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07003014 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003015int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07003016{
Linus Walleij4c37ce82016-05-02 13:13:10 +02003017 struct gpio_chip *chip;
3018 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07003019
Linus Walleij79bb71b2016-06-15 22:57:38 +02003020 /*
3021 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
3022 * requires this function to not return zero on an invalid descriptor
3023 * but rather a negative error number.
3024 */
Linus Walleijbfbbe442016-06-16 11:55:55 +02003025 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
Linus Walleij79bb71b2016-06-15 22:57:38 +02003026 return -EINVAL;
3027
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003028 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003029 offset = gpio_chip_hwgpio(desc);
Linus Walleij4c37ce82016-05-02 13:13:10 +02003030 if (chip->to_irq) {
3031 int retirq = chip->to_irq(chip, offset);
3032
3033 /* Zero means NO_IRQ */
3034 if (!retirq)
3035 return -ENXIO;
3036
3037 return retirq;
3038 }
3039 return -ENXIO;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003040}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003041EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003042
Linus Walleijd468bf92013-09-24 11:54:38 +02003043/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003044 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003045 * @chip: the chip the GPIO to lock belongs to
3046 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02003047 *
3048 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08003049 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08003050 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003051int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08003052{
Linus Walleij9c102802016-05-25 10:56:03 +02003053 struct gpio_desc *desc;
Linus Walleijd468bf92013-09-24 11:54:38 +02003054
Linus Walleij9c102802016-05-25 10:56:03 +02003055 desc = gpiochip_get_desc(chip, offset);
3056 if (IS_ERR(desc))
3057 return PTR_ERR(desc);
3058
Linus Walleij60f83392016-11-12 15:01:09 +01003059 /*
3060 * If it's fast: flush the direction setting if something changed
3061 * behind our back
3062 */
3063 if (!chip->can_sleep && chip->get_direction) {
Linus Walleij9c102802016-05-25 10:56:03 +02003064 int dir = chip->get_direction(chip, offset);
3065
3066 if (dir)
3067 clear_bit(FLAG_IS_OUT, &desc->flags);
3068 else
3069 set_bit(FLAG_IS_OUT, &desc->flags);
3070 }
3071
3072 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003073 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02003074 "%s: tried to flag a GPIO set as output for IRQ\n",
3075 __func__);
3076 return -EIO;
3077 }
3078
Linus Walleij9c102802016-05-25 10:56:03 +02003079 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
Linus Walleij3940c342016-11-14 00:09:07 +01003080
3081 /*
3082 * If the consumer has not set up a label (such as when the
3083 * IRQ is referenced from .to_irq()) we set up a label here
3084 * so it is clear this is used as an interrupt.
3085 */
3086 if (!desc->label)
3087 desc_set_label(desc, "interrupt");
3088
Linus Walleijd468bf92013-09-24 11:54:38 +02003089 return 0;
3090}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003091EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02003092
Linus Walleijd468bf92013-09-24 11:54:38 +02003093/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003094 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003095 * @chip: the chip the GPIO to lock belongs to
3096 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02003097 *
3098 * This is used directly by GPIO drivers that want to indicate
3099 * that a certain GPIO is no longer used exclusively for IRQ.
3100 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003101void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02003102{
Linus Walleij3940c342016-11-14 00:09:07 +01003103 struct gpio_desc *desc;
3104
3105 desc = gpiochip_get_desc(chip, offset);
3106 if (IS_ERR(desc))
Linus Walleijd468bf92013-09-24 11:54:38 +02003107 return;
3108
Linus Walleij3940c342016-11-14 00:09:07 +01003109 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
3110
3111 /* If we only had this marking, erase it */
3112 if (desc->label && !strcmp(desc->label, "interrupt"))
3113 desc_set_label(desc, NULL);
Linus Walleijd468bf92013-09-24 11:54:38 +02003114}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003115EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02003116
Linus Walleij6cee3822016-02-11 20:16:45 +01003117bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
3118{
3119 if (offset >= chip->ngpio)
3120 return false;
3121
3122 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
3123}
3124EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
3125
Linus Walleij143b65d2016-02-16 15:41:42 +01003126bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
3127{
3128 if (offset >= chip->ngpio)
3129 return false;
3130
3131 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
3132}
3133EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
3134
3135bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
3136{
3137 if (offset >= chip->ngpio)
3138 return false;
3139
3140 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
3141}
3142EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
3143
Charles Keepax05f479b2017-05-23 15:47:29 +01003144bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset)
3145{
3146 if (offset >= chip->ngpio)
3147 return false;
3148
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303149 return !test_bit(FLAG_TRANSITORY, &chip->gpiodev->descs[offset].flags);
Charles Keepax05f479b2017-05-23 15:47:29 +01003150}
3151EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
3152
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003153/**
3154 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
3155 * @desc: gpio whose value will be returned
3156 *
3157 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003158 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003159 *
3160 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003161 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003162int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08003163{
David Brownelld2876d02008-02-04 22:28:20 -08003164 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003165 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003166 return gpiod_get_raw_value_commit(desc);
David Brownelld2876d02008-02-04 22:28:20 -08003167}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003168EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003169
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003170/**
3171 * gpiod_get_value_cansleep() - return a gpio's value
3172 * @desc: gpio whose value will be returned
3173 *
3174 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003175 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003176 *
3177 * This function is to be called from contexts that can sleep.
3178 */
3179int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003180{
David Brownelld2876d02008-02-04 22:28:20 -08003181 int value;
David Brownelld2876d02008-02-04 22:28:20 -08003182
3183 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003184 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003185 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003186 if (value < 0)
3187 return value;
3188
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003189 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3190 value = !value;
3191
David Brownelld2876d02008-02-04 22:28:20 -08003192 return value;
3193}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003194EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003195
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003196/**
Lukas Wunnereec1d562017-10-12 12:40:10 +02003197 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
3198 * @array_size: number of elements in the descriptor / value arrays
3199 * @desc_array: array of GPIO descriptors whose values will be read
3200 * @value_array: array to store the read values
3201 *
3202 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3203 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3204 * else an error code.
3205 *
3206 * This function is to be called from contexts that can sleep.
3207 */
3208int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
3209 struct gpio_desc **desc_array,
3210 int *value_array)
3211{
3212 might_sleep_if(extra_checks);
3213 if (!desc_array)
3214 return -EINVAL;
3215 return gpiod_get_array_value_complex(true, true, array_size,
3216 desc_array, value_array);
3217}
3218EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
3219
3220/**
3221 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
3222 * @array_size: number of elements in the descriptor / value arrays
3223 * @desc_array: array of GPIO descriptors whose values will be read
3224 * @value_array: array to store the read values
3225 *
3226 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3227 * into account. Return 0 in case of success, else an error code.
3228 *
3229 * This function is to be called from contexts that can sleep.
3230 */
3231int gpiod_get_array_value_cansleep(unsigned int array_size,
3232 struct gpio_desc **desc_array,
3233 int *value_array)
3234{
3235 might_sleep_if(extra_checks);
3236 if (!desc_array)
3237 return -EINVAL;
3238 return gpiod_get_array_value_complex(false, true, array_size,
3239 desc_array, value_array);
3240}
3241EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
3242
3243/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003244 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
3245 * @desc: gpio whose value will be assigned
3246 * @value: value to assign
3247 *
3248 * Set the raw value of the GPIO, i.e. the value of its physical line without
3249 * regard for its ACTIVE_LOW status.
3250 *
3251 * This function is to be called from contexts that can sleep.
3252 */
3253void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003254{
David Brownelld2876d02008-02-04 22:28:20 -08003255 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003256 VALIDATE_DESC_VOID(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003257 gpiod_set_raw_value_commit(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003258}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003259EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003260
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003261/**
3262 * gpiod_set_value_cansleep() - assign a gpio's value
3263 * @desc: gpio whose value will be assigned
3264 * @value: value to assign
3265 *
3266 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3267 * account
3268 *
3269 * This function is to be called from contexts that can sleep.
3270 */
3271void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003272{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003273 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003274 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003275 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3276 value = !value;
Linus Walleijfac9d882017-09-26 20:58:28 +02003277 gpiod_set_raw_value_commit(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08003278}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003279EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08003280
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003281/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003282 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003283 * @array_size: number of elements in the descriptor / value arrays
3284 * @desc_array: array of GPIO descriptors whose values will be assigned
3285 * @value_array: array of values to assign
3286 *
3287 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3288 * without regard for their ACTIVE_LOW status.
3289 *
3290 * This function is to be called from contexts that can sleep.
3291 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003292void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
3293 struct gpio_desc **desc_array,
3294 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003295{
3296 might_sleep_if(extra_checks);
3297 if (!desc_array)
3298 return;
Linus Walleij44c72882016-04-24 11:36:59 +02003299 gpiod_set_array_value_complex(true, true, array_size, desc_array,
3300 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003301}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003302EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003303
3304/**
Dmitry Torokhov3946d182017-08-14 21:59:55 -07003305 * gpiod_add_lookup_tables() - register GPIO device consumers
3306 * @tables: list of tables of consumers to register
3307 * @n: number of tables in the list
3308 */
3309void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
3310{
3311 unsigned int i;
3312
3313 mutex_lock(&gpio_lookup_lock);
3314
3315 for (i = 0; i < n; i++)
3316 list_add_tail(&tables[i]->list, &gpio_lookup_list);
3317
3318 mutex_unlock(&gpio_lookup_lock);
3319}
3320
3321/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003322 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003323 * @array_size: number of elements in the descriptor / value arrays
3324 * @desc_array: array of GPIO descriptors whose values will be assigned
3325 * @value_array: array of values to assign
3326 *
3327 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3328 * into account.
3329 *
3330 * This function is to be called from contexts that can sleep.
3331 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003332void gpiod_set_array_value_cansleep(unsigned int array_size,
3333 struct gpio_desc **desc_array,
3334 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003335{
3336 might_sleep_if(extra_checks);
3337 if (!desc_array)
3338 return;
Linus Walleij44c72882016-04-24 11:36:59 +02003339 gpiod_set_array_value_complex(false, true, array_size, desc_array,
3340 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003341}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003342EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003343
3344/**
Alexandre Courbotad824782013-12-03 12:20:11 +09003345 * gpiod_add_lookup_table() - register GPIO device consumers
3346 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003347 */
Alexandre Courbotad824782013-12-03 12:20:11 +09003348void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003349{
3350 mutex_lock(&gpio_lookup_lock);
3351
Alexandre Courbotad824782013-12-03 12:20:11 +09003352 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003353
3354 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08003355}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02003356EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
David Brownelld2876d02008-02-04 22:28:20 -08003357
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05303358/**
3359 * gpiod_remove_lookup_table() - unregister GPIO device consumers
3360 * @table: table of consumers to unregister
3361 */
3362void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
3363{
3364 mutex_lock(&gpio_lookup_lock);
3365
3366 list_del(&table->list);
3367
3368 mutex_unlock(&gpio_lookup_lock);
3369}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02003370EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05303371
Alexandre Courbotad824782013-12-03 12:20:11 +09003372static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
3373{
3374 const char *dev_id = dev ? dev_name(dev) : NULL;
3375 struct gpiod_lookup_table *table;
3376
3377 mutex_lock(&gpio_lookup_lock);
3378
3379 list_for_each_entry(table, &gpio_lookup_list, list) {
3380 if (table->dev_id && dev_id) {
3381 /*
3382 * Valid strings on both ends, must be identical to have
3383 * a match
3384 */
3385 if (!strcmp(table->dev_id, dev_id))
3386 goto found;
3387 } else {
3388 /*
3389 * One of the pointers is NULL, so both must be to have
3390 * a match
3391 */
3392 if (dev_id == table->dev_id)
3393 goto found;
3394 }
3395 }
3396 table = NULL;
3397
3398found:
3399 mutex_unlock(&gpio_lookup_lock);
3400 return table;
3401}
3402
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003403static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09003404 unsigned int idx,
3405 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003406{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003407 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09003408 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003409 struct gpiod_lookup *p;
3410
Alexandre Courbotad824782013-12-03 12:20:11 +09003411 table = gpiod_find_lookup_table(dev);
3412 if (!table)
3413 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003414
Alexandre Courbotad824782013-12-03 12:20:11 +09003415 for (p = &table->table[0]; p->chip_label; p++) {
3416 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003417
Alexandre Courbotad824782013-12-03 12:20:11 +09003418 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003419 if (p->idx != idx)
3420 continue;
3421
Alexandre Courbotad824782013-12-03 12:20:11 +09003422 /* If the lookup entry has a con_id, require exact match */
3423 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
3424 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003425
Alexandre Courbotad824782013-12-03 12:20:11 +09003426 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003427
Alexandre Courbotad824782013-12-03 12:20:11 +09003428 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003429 dev_err(dev, "cannot find GPIO chip %s\n",
3430 p->chip_label);
3431 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003432 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003433
Alexandre Courbotad824782013-12-03 12:20:11 +09003434 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003435 dev_err(dev,
3436 "requested GPIO %d is out of range [0..%d] for chip %s\n",
3437 idx, chip->ngpio, chip->label);
3438 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09003439 }
3440
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09003441 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09003442 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003443
3444 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09003445 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003446
3447 return desc;
3448}
3449
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003450static int dt_gpio_count(struct device *dev, const char *con_id)
3451{
3452 int ret;
3453 char propname[32];
3454 unsigned int i;
3455
3456 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
3457 if (con_id)
3458 snprintf(propname, sizeof(propname), "%s-%s",
3459 con_id, gpio_suffixes[i]);
3460 else
3461 snprintf(propname, sizeof(propname), "%s",
3462 gpio_suffixes[i]);
3463
3464 ret = of_gpio_named_count(dev->of_node, propname);
Andy Shevchenko4033d4a2017-02-20 18:15:47 +02003465 if (ret > 0)
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003466 break;
3467 }
Andy Shevchenko4033d4a2017-02-20 18:15:47 +02003468 return ret ? ret : -ENOENT;
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003469}
3470
3471static int platform_gpio_count(struct device *dev, const char *con_id)
3472{
3473 struct gpiod_lookup_table *table;
3474 struct gpiod_lookup *p;
3475 unsigned int count = 0;
3476
3477 table = gpiod_find_lookup_table(dev);
3478 if (!table)
3479 return -ENOENT;
3480
3481 for (p = &table->table[0]; p->chip_label; p++) {
3482 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3483 (!con_id && !p->con_id))
3484 count++;
3485 }
3486 if (!count)
3487 return -ENOENT;
3488
3489 return count;
3490}
3491
3492/**
3493 * gpiod_count - return the number of GPIOs associated with a device / function
3494 * or -ENOENT if no GPIO has been assigned to the requested function
3495 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3496 * @con_id: function within the GPIO consumer
3497 */
3498int gpiod_count(struct device *dev, const char *con_id)
3499{
3500 int count = -ENOENT;
3501
3502 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
3503 count = dt_gpio_count(dev, con_id);
3504 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
3505 count = acpi_gpio_count(dev, con_id);
3506
3507 if (count < 0)
3508 count = platform_gpio_count(dev, con_id);
3509
3510 return count;
3511}
3512EXPORT_SYMBOL_GPL(gpiod_count);
3513
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003514/**
Thierry Reding08791622014-04-25 16:54:22 +02003515 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09003516 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003517 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003518 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003519 *
3520 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003521 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07003522 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003523 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003524struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003525 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003526{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003527 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003528}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003529EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003530
3531/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02003532 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
3533 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3534 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003535 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02003536 *
3537 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
3538 * the requested function it will return NULL. This is convenient for drivers
3539 * that need to handle optional GPIOs.
3540 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003541struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003542 const char *con_id,
3543 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003544{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003545 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003546}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003547EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003548
Benoit Parrotf625d462015-02-02 11:44:44 -06003549
3550/**
3551 * gpiod_configure_flags - helper function to configure a given GPIO
3552 * @desc: gpio whose value will be assigned
3553 * @con_id: function within the GPIO consumer
Johan Hovold85b03b32016-07-03 18:32:05 +02003554 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3555 * of_get_gpio_hog()
Benoit Parrotf625d462015-02-02 11:44:44 -06003556 * @dflags: gpiod_flags - optional GPIO initialization flags
3557 *
3558 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
3559 * requested function and/or index, or another IS_ERR() code if an error
3560 * occurred while trying to acquire the GPIO.
3561 */
Andy Shevchenkoc29fd9e2017-05-23 20:03:16 +03003562int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Johan Hovold85b03b32016-07-03 18:32:05 +02003563 unsigned long lflags, enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06003564{
3565 int status;
3566
Johan Hovold85b03b32016-07-03 18:32:05 +02003567 if (lflags & GPIO_ACTIVE_LOW)
3568 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
Linus Walleijf926dfc2017-09-10 19:26:22 +02003569
Johan Hovold85b03b32016-07-03 18:32:05 +02003570 if (lflags & GPIO_OPEN_DRAIN)
3571 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
Linus Walleijf926dfc2017-09-10 19:26:22 +02003572 else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) {
3573 /*
3574 * This enforces open drain mode from the consumer side.
3575 * This is necessary for some busses like I2C, but the lookup
3576 * should *REALLY* have specified them as open drain in the
3577 * first place, so print a little warning here.
3578 */
3579 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3580 gpiod_warn(desc,
3581 "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
3582 }
3583
Johan Hovold85b03b32016-07-03 18:32:05 +02003584 if (lflags & GPIO_OPEN_SOURCE)
3585 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303586
3587 status = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
3588 if (status < 0)
3589 return status;
Johan Hovold85b03b32016-07-03 18:32:05 +02003590
Benoit Parrotf625d462015-02-02 11:44:44 -06003591 /* No particular flag request, return here... */
3592 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
3593 pr_debug("no flags found for %s\n", con_id);
3594 return 0;
3595 }
3596
3597 /* Process flags */
3598 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
3599 status = gpiod_direction_output(desc,
Linus Walleijad177312016-11-13 23:02:44 +01003600 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
Benoit Parrotf625d462015-02-02 11:44:44 -06003601 else
3602 status = gpiod_direction_input(desc);
3603
3604 return status;
3605}
3606
Thierry Reding29a1f2332014-04-25 17:10:06 +02003607/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003608 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02003609 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003610 * @con_id: function within the GPIO consumer
3611 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003612 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003613 *
3614 * This variant of gpiod_get() allows to access GPIOs other than the first
3615 * defined one for functions that define several GPIOs.
3616 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003617 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
3618 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07003619 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003620 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003621struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003622 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003623 unsigned int idx,
3624 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003625{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003626 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003627 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003628 enum gpio_lookup_flags lookupflags = 0;
Linus Walleij7d18f0a2018-01-16 08:29:50 +01003629 /* Maybe we have a device name, maybe not */
3630 const char *devname = dev ? dev_name(dev) : "?";
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003631
3632 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
3633
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003634 if (dev) {
3635 /* Using device tree? */
3636 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
3637 dev_dbg(dev, "using device tree for GPIO lookup\n");
3638 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
3639 } else if (ACPI_COMPANION(dev)) {
3640 dev_dbg(dev, "using ACPI for GPIO lookup\n");
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +03003641 desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags);
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003642 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003643 }
3644
3645 /*
3646 * Either we are not using DT or ACPI, or their lookup did not return
3647 * a result. In that case, use platform lookup as a fallback.
3648 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003649 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04003650 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003651 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003652 }
3653
3654 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02003655 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003656 return desc;
3657 }
3658
Linus Walleij7d18f0a2018-01-16 08:29:50 +01003659 /*
3660 * If a connection label was passed use that, else attempt to use
3661 * the device name as label
3662 */
3663 status = gpiod_request(desc, con_id ? con_id : devname);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003664 if (status < 0)
3665 return ERR_PTR(status);
3666
Johan Hovold85b03b32016-07-03 18:32:05 +02003667 status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003668 if (status < 0) {
3669 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
3670 gpiod_put(desc);
3671 return ERR_PTR(status);
3672 }
3673
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003674 return desc;
3675}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003676EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003677
3678/**
Linus Walleij6392cca2017-12-29 02:07:54 +01003679 * gpiod_get_from_of_node() - obtain a GPIO from an OF node
3680 * @node: handle of the OF node
3681 * @propname: name of the DT property representing the GPIO
3682 * @index: index of the GPIO to obtain for the consumer
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003683 * @dflags: GPIO initialization flags
Thierry Reding950d55f52017-07-24 16:57:22 +02003684 * @label: label to attach to the requested GPIO
Mika Westerberg40b73182014-10-21 13:33:59 +02003685 *
Thierry Reding950d55f52017-07-24 16:57:22 +02003686 * Returns:
Andy Shevchenkoff213782017-02-28 17:03:12 +02003687 * On successful request the GPIO pin is configured in accordance with
Linus Walleij6392cca2017-12-29 02:07:54 +01003688 * provided @dflags. If the node does not have the requested GPIO
3689 * property, NULL is returned.
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003690 *
Mika Westerberg40b73182014-10-21 13:33:59 +02003691 * In case of error an ERR_PTR() is returned.
3692 */
Linus Walleij92542ed2017-12-29 22:52:02 +01003693struct gpio_desc *gpiod_get_from_of_node(struct device_node *node,
3694 const char *propname, int index,
3695 enum gpiod_flags dflags,
3696 const char *label)
Mika Westerberg40b73182014-10-21 13:33:59 +02003697{
Colin Ian King40a3c9d2018-01-16 11:56:11 +00003698 struct gpio_desc *desc;
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003699 unsigned long lflags = 0;
Linus Walleij6392cca2017-12-29 02:07:54 +01003700 enum of_gpio_flags flags;
Mika Westerberg40b73182014-10-21 13:33:59 +02003701 bool active_low = false;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003702 bool single_ended = false;
Laxman Dewangan4c0facd2017-04-06 19:05:52 +05303703 bool open_drain = false;
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303704 bool transitory = false;
Mika Westerberg40b73182014-10-21 13:33:59 +02003705 int ret;
3706
Linus Walleij6392cca2017-12-29 02:07:54 +01003707 desc = of_get_named_gpiod_flags(node, propname,
3708 index, &flags);
Mika Westerberg40b73182014-10-21 13:33:59 +02003709
Linus Walleij6392cca2017-12-29 02:07:54 +01003710 if (!desc || IS_ERR(desc)) {
3711 /* If it is not there, just return NULL */
3712 if (PTR_ERR(desc) == -ENOENT)
3713 return NULL;
3714 return desc;
Mika Westerberg40b73182014-10-21 13:33:59 +02003715 }
3716
Linus Walleij6392cca2017-12-29 02:07:54 +01003717 active_low = flags & OF_GPIO_ACTIVE_LOW;
3718 single_ended = flags & OF_GPIO_SINGLE_ENDED;
3719 open_drain = flags & OF_GPIO_OPEN_DRAIN;
3720 transitory = flags & OF_GPIO_TRANSITORY;
Mika Westerberg40b73182014-10-21 13:33:59 +02003721
Alexander Steinb2987d72017-01-12 17:39:24 +01003722 ret = gpiod_request(desc, label);
Johan Hovold85b03b32016-07-03 18:32:05 +02003723 if (ret)
3724 return ERR_PTR(ret);
3725
Mika Westerberg40b73182014-10-21 13:33:59 +02003726 if (active_low)
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003727 lflags |= GPIO_ACTIVE_LOW;
Mika Westerberg40b73182014-10-21 13:33:59 +02003728
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003729 if (single_ended) {
Laxman Dewangan4c0facd2017-04-06 19:05:52 +05303730 if (open_drain)
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003731 lflags |= GPIO_OPEN_DRAIN;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003732 else
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003733 lflags |= GPIO_OPEN_SOURCE;
3734 }
3735
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303736 if (transitory)
3737 lflags |= GPIO_TRANSITORY;
3738
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003739 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
3740 if (ret < 0) {
3741 gpiod_put(desc);
3742 return ERR_PTR(ret);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003743 }
3744
Mika Westerberg40b73182014-10-21 13:33:59 +02003745 return desc;
3746}
Linus Walleij92542ed2017-12-29 22:52:02 +01003747EXPORT_SYMBOL(gpiod_get_from_of_node);
Linus Walleij6392cca2017-12-29 02:07:54 +01003748
3749/**
3750 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
3751 * @fwnode: handle of the firmware node
3752 * @propname: name of the firmware property representing the GPIO
3753 * @index: index of the GPIO to obtain for the consumer
3754 * @dflags: GPIO initialization flags
3755 * @label: label to attach to the requested GPIO
3756 *
3757 * This function can be used for drivers that get their configuration
3758 * from opaque firmware.
3759 *
3760 * The function properly finds the corresponding GPIO using whatever is the
3761 * underlying firmware interface and then makes sure that the GPIO
3762 * descriptor is requested before it is returned to the caller.
3763 *
3764 * Returns:
3765 * On successful request the GPIO pin is configured in accordance with
3766 * provided @dflags.
3767 *
3768 * In case of error an ERR_PTR() is returned.
3769 */
3770struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
3771 const char *propname, int index,
3772 enum gpiod_flags dflags,
3773 const char *label)
3774{
3775 struct gpio_desc *desc = ERR_PTR(-ENODEV);
3776 unsigned long lflags = 0;
3777 int ret;
3778
3779 if (!fwnode)
3780 return ERR_PTR(-EINVAL);
3781
3782 if (is_of_node(fwnode)) {
3783 desc = gpiod_get_from_of_node(to_of_node(fwnode),
3784 propname, index,
3785 dflags,
3786 label);
3787 return desc;
3788 } else if (is_acpi_node(fwnode)) {
3789 struct acpi_gpio_info info;
3790
3791 desc = acpi_node_get_gpiod(fwnode, propname, index, &info);
3792 if (IS_ERR(desc))
3793 return desc;
3794
3795 acpi_gpio_update_gpiod_flags(&dflags, &info);
3796
3797 if (info.polarity == GPIO_ACTIVE_LOW)
3798 lflags |= GPIO_ACTIVE_LOW;
3799 }
3800
3801 /* Currently only ACPI takes this path */
3802 ret = gpiod_request(desc, label);
3803 if (ret)
3804 return ERR_PTR(ret);
3805
3806 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
3807 if (ret < 0) {
3808 gpiod_put(desc);
3809 return ERR_PTR(ret);
3810 }
3811
3812 return desc;
3813}
Mika Westerberg40b73182014-10-21 13:33:59 +02003814EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
3815
3816/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02003817 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
3818 * function
3819 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3820 * @con_id: function within the GPIO consumer
3821 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003822 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02003823 *
3824 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
3825 * specified index was assigned to the requested function it will return NULL.
3826 * This is convenient for drivers that need to handle optional GPIOs.
3827 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003828struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02003829 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003830 unsigned int index,
3831 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003832{
3833 struct gpio_desc *desc;
3834
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003835 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003836 if (IS_ERR(desc)) {
3837 if (PTR_ERR(desc) == -ENOENT)
3838 return NULL;
3839 }
3840
3841 return desc;
3842}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003843EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003844
3845/**
Benoit Parrotf625d462015-02-02 11:44:44 -06003846 * gpiod_hog - Hog the specified GPIO desc given the provided flags
3847 * @desc: gpio whose value will be assigned
3848 * @name: gpio line name
3849 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3850 * of_get_gpio_hog()
3851 * @dflags: gpiod_flags - optional GPIO initialization flags
3852 */
3853int gpiod_hog(struct gpio_desc *desc, const char *name,
3854 unsigned long lflags, enum gpiod_flags dflags)
3855{
3856 struct gpio_chip *chip;
3857 struct gpio_desc *local_desc;
3858 int hwnum;
3859 int status;
3860
3861 chip = gpiod_to_chip(desc);
3862 hwnum = gpio_chip_hwgpio(desc);
3863
3864 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
3865 if (IS_ERR(local_desc)) {
Laxman Dewanganc31a5712016-03-11 19:13:21 +05303866 status = PTR_ERR(local_desc);
3867 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
3868 name, chip->label, hwnum, status);
3869 return status;
Benoit Parrotf625d462015-02-02 11:44:44 -06003870 }
3871
Johan Hovold85b03b32016-07-03 18:32:05 +02003872 status = gpiod_configure_flags(desc, name, lflags, dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -06003873 if (status < 0) {
Laxman Dewanganc31a5712016-03-11 19:13:21 +05303874 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed, %d\n",
3875 name, chip->label, hwnum, status);
Benoit Parrotf625d462015-02-02 11:44:44 -06003876 gpiochip_free_own_desc(desc);
3877 return status;
3878 }
3879
3880 /* Mark GPIO as hogged so it can be identified and removed later */
3881 set_bit(FLAG_IS_HOGGED, &desc->flags);
3882
3883 pr_info("GPIO line %d (%s) hogged as %s%s\n",
3884 desc_to_gpio(desc), name,
3885 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
3886 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
3887 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
3888
3889 return 0;
3890}
3891
3892/**
3893 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
3894 * @chip: gpio chip to act on
3895 *
3896 * This is only used by of_gpiochip_remove to free hogged gpios
3897 */
3898static void gpiochip_free_hogs(struct gpio_chip *chip)
3899{
3900 int id;
3901
3902 for (id = 0; id < chip->ngpio; id++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01003903 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
3904 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
Benoit Parrotf625d462015-02-02 11:44:44 -06003905 }
3906}
3907
3908/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003909 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
3910 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3911 * @con_id: function within the GPIO consumer
3912 * @flags: optional GPIO initialization flags
3913 *
3914 * This function acquires all the GPIOs defined under a given function.
3915 *
3916 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
3917 * no GPIO has been assigned to the requested function, or another IS_ERR()
3918 * code if an error occurred while trying to acquire the GPIOs.
3919 */
3920struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
3921 const char *con_id,
3922 enum gpiod_flags flags)
3923{
3924 struct gpio_desc *desc;
3925 struct gpio_descs *descs;
3926 int count;
3927
3928 count = gpiod_count(dev, con_id);
3929 if (count < 0)
3930 return ERR_PTR(count);
3931
3932 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
3933 GFP_KERNEL);
3934 if (!descs)
3935 return ERR_PTR(-ENOMEM);
3936
3937 for (descs->ndescs = 0; descs->ndescs < count; ) {
3938 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
3939 if (IS_ERR(desc)) {
3940 gpiod_put_array(descs);
3941 return ERR_CAST(desc);
3942 }
3943 descs->desc[descs->ndescs] = desc;
3944 descs->ndescs++;
3945 }
3946 return descs;
3947}
3948EXPORT_SYMBOL_GPL(gpiod_get_array);
3949
3950/**
3951 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
3952 * function
3953 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3954 * @con_id: function within the GPIO consumer
3955 * @flags: optional GPIO initialization flags
3956 *
3957 * This is equivalent to gpiod_get_array(), except that when no GPIO was
3958 * assigned to the requested function it will return NULL.
3959 */
3960struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
3961 const char *con_id,
3962 enum gpiod_flags flags)
3963{
3964 struct gpio_descs *descs;
3965
3966 descs = gpiod_get_array(dev, con_id, flags);
3967 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
3968 return NULL;
3969
3970 return descs;
3971}
3972EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
3973
3974/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003975 * gpiod_put - dispose of a GPIO descriptor
3976 * @desc: GPIO descriptor to dispose of
3977 *
3978 * No descriptor can be used after gpiod_put() has been called on it.
3979 */
3980void gpiod_put(struct gpio_desc *desc)
3981{
3982 gpiod_free(desc);
3983}
3984EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08003985
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003986/**
3987 * gpiod_put_array - dispose of multiple GPIO descriptors
3988 * @descs: struct gpio_descs containing an array of descriptors
3989 */
3990void gpiod_put_array(struct gpio_descs *descs)
3991{
3992 unsigned int i;
3993
3994 for (i = 0; i < descs->ndescs; i++)
3995 gpiod_put(descs->desc[i]);
3996
3997 kfree(descs);
3998}
3999EXPORT_SYMBOL_GPL(gpiod_put_array);
4000
Linus Walleij3c702e92015-10-21 15:29:53 +02004001static int __init gpiolib_dev_init(void)
4002{
4003 int ret;
4004
4005 /* Register GPIO sysfs bus */
4006 ret = bus_register(&gpio_bus_type);
4007 if (ret < 0) {
4008 pr_err("gpiolib: could not register GPIO bus type\n");
4009 return ret;
4010 }
4011
4012 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
4013 if (ret < 0) {
4014 pr_err("gpiolib: failed to allocate char dev region\n");
4015 bus_unregister(&gpio_bus_type);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07004016 } else {
4017 gpiolib_initialized = true;
4018 gpiochip_setup_devs();
Linus Walleij3c702e92015-10-21 15:29:53 +02004019 }
4020 return ret;
4021}
4022core_initcall(gpiolib_dev_init);
4023
David Brownelld2876d02008-02-04 22:28:20 -08004024#ifdef CONFIG_DEBUG_FS
4025
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004026static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
David Brownelld2876d02008-02-04 22:28:20 -08004027{
4028 unsigned i;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004029 struct gpio_chip *chip = gdev->chip;
4030 unsigned gpio = gdev->base;
4031 struct gpio_desc *gdesc = &gdev->descs[0];
David Brownelld2876d02008-02-04 22:28:20 -08004032 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02004033 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08004034
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004035 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02004036 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
4037 if (gdesc->name) {
4038 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
4039 gpio, gdesc->name);
4040 }
David Brownelld2876d02008-02-04 22:28:20 -08004041 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02004042 }
David Brownelld2876d02008-02-04 22:28:20 -08004043
Alexandre Courbot372e7222013-02-03 01:29:29 +09004044 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08004045 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02004046 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Markus Pargmannced433e2015-08-14 16:11:02 +02004047 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
4048 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08004049 is_out ? "out" : "in ",
4050 chip->get
4051 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02004052 : "? ",
4053 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08004054 seq_printf(s, "\n");
4055 }
4056}
4057
Thierry Redingf9c4a312012-04-12 13:26:01 +02004058static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08004059{
Grant Likely362432a2013-02-09 09:41:49 +00004060 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02004061 struct gpio_device *gdev = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09004062 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02004063
4064 s->private = "";
4065
Grant Likely362432a2013-02-09 09:41:49 +00004066 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02004067 list_for_each_entry(gdev, &gpio_devices, list)
Grant Likely362432a2013-02-09 09:41:49 +00004068 if (index-- == 0) {
4069 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02004070 return gdev;
Grant Likely362432a2013-02-09 09:41:49 +00004071 }
4072 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09004073
4074 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02004075}
4076
4077static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
4078{
Grant Likely362432a2013-02-09 09:41:49 +00004079 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02004080 struct gpio_device *gdev = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02004081 void *ret = NULL;
4082
Grant Likely362432a2013-02-09 09:41:49 +00004083 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02004084 if (list_is_last(&gdev->list, &gpio_devices))
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09004085 ret = NULL;
4086 else
Linus Walleijff2b1352015-10-20 11:10:38 +02004087 ret = list_entry(gdev->list.next, struct gpio_device, list);
Grant Likely362432a2013-02-09 09:41:49 +00004088 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02004089
4090 s->private = "\n";
4091 ++*pos;
4092
4093 return ret;
4094}
4095
4096static void gpiolib_seq_stop(struct seq_file *s, void *v)
4097{
4098}
4099
4100static int gpiolib_seq_show(struct seq_file *s, void *v)
4101{
Linus Walleijff2b1352015-10-20 11:10:38 +02004102 struct gpio_device *gdev = v;
4103 struct gpio_chip *chip = gdev->chip;
4104 struct device *parent;
Thierry Redingf9c4a312012-04-12 13:26:01 +02004105
Linus Walleijff2b1352015-10-20 11:10:38 +02004106 if (!chip) {
4107 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
4108 dev_name(&gdev->dev));
4109 return 0;
4110 }
4111
4112 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
4113 dev_name(&gdev->dev),
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004114 gdev->base, gdev->base + gdev->ngpio - 1);
Linus Walleijff2b1352015-10-20 11:10:38 +02004115 parent = chip->parent;
4116 if (parent)
4117 seq_printf(s, ", parent: %s/%s",
4118 parent->bus ? parent->bus->name : "no-bus",
4119 dev_name(parent));
Thierry Redingf9c4a312012-04-12 13:26:01 +02004120 if (chip->label)
4121 seq_printf(s, ", %s", chip->label);
4122 if (chip->can_sleep)
4123 seq_printf(s, ", can sleep");
4124 seq_printf(s, ":\n");
4125
4126 if (chip->dbg_show)
4127 chip->dbg_show(s, chip);
4128 else
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004129 gpiolib_dbg_show(s, gdev);
Thierry Redingf9c4a312012-04-12 13:26:01 +02004130
David Brownelld2876d02008-02-04 22:28:20 -08004131 return 0;
4132}
4133
Thierry Redingf9c4a312012-04-12 13:26:01 +02004134static const struct seq_operations gpiolib_seq_ops = {
4135 .start = gpiolib_seq_start,
4136 .next = gpiolib_seq_next,
4137 .stop = gpiolib_seq_stop,
4138 .show = gpiolib_seq_show,
4139};
4140
David Brownelld2876d02008-02-04 22:28:20 -08004141static int gpiolib_open(struct inode *inode, struct file *file)
4142{
Thierry Redingf9c4a312012-04-12 13:26:01 +02004143 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08004144}
4145
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004146static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02004147 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08004148 .open = gpiolib_open,
4149 .read = seq_read,
4150 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02004151 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08004152};
4153
4154static int __init gpiolib_debugfs_init(void)
4155{
4156 /* /sys/kernel/debug/gpio */
4157 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
4158 NULL, NULL, &gpiolib_operations);
4159 return 0;
4160}
4161subsys_initcall(gpiolib_debugfs_init);
4162
4163#endif /* DEBUG_FS */