blob: 45c20307630ad3d72736b577568c366c712e5cf5 [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{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100164 if (!desc || !desc->gdev || !desc->gdev->chip)
165 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 *
198 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
199 *
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 Walleijf881bab02015-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 Walleijf881bab02015-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 Walleijf881bab02015-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 Walleijf881bab02015-09-23 16:20:43 -0700291
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100292 if (!desc->name || !name)
Linus Walleijf881bab02015-09-23 16:20:43 -0700293 continue;
294
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100295 if (!strcmp(desc->name, name)) {
Linus Walleijf881bab02015-09-23 16:20:43 -0700296 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100297 return desc;
Linus Walleijf881bab02015-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 Walleijf881bab02015-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 Walleijf881bab02015-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
747 ge.timestamp = ktime_get_real_ns();
Bartosz Golaszewskidf1e76f2017-07-03 11:12:03 +0200748 level = gpiod_get_value_cansleep(le->desc);
Linus Walleij61f922d2016-06-02 11:30:15 +0200749
Bartosz Golaszewskiad537b82017-06-23 13:45:16 +0200750 if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
751 && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200752 if (level)
753 /* Emit low-to-high event */
754 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
755 else
756 /* Emit high-to-low event */
757 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Bartosz Golaszewskidf1e76f2017-07-03 11:12:03 +0200758 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE && level) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200759 /* Emit low-to-high event */
760 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
Bartosz Golaszewskidf1e76f2017-07-03 11:12:03 +0200761 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE && !level) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200762 /* Emit high-to-low event */
763 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Arnd Bergmannbc0207a2016-06-16 11:02:41 +0200764 } else {
765 return IRQ_NONE;
Linus Walleij61f922d2016-06-02 11:30:15 +0200766 }
767
768 ret = kfifo_put(&le->events, ge);
769 if (ret != 0)
770 wake_up_poll(&le->wait, POLLIN);
771
772 return IRQ_HANDLED;
773}
774
775static int lineevent_create(struct gpio_device *gdev, void __user *ip)
776{
777 struct gpioevent_request eventreq;
778 struct lineevent_state *le;
779 struct gpio_desc *desc;
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200780 struct file *file;
Linus Walleij61f922d2016-06-02 11:30:15 +0200781 u32 offset;
782 u32 lflags;
783 u32 eflags;
784 int fd;
785 int ret;
786 int irqflags = 0;
787
788 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
789 return -EFAULT;
790
791 le = kzalloc(sizeof(*le), GFP_KERNEL);
792 if (!le)
793 return -ENOMEM;
794 le->gdev = gdev;
795 get_device(&gdev->dev);
796
797 /* Make sure this is terminated */
798 eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
799 if (strlen(eventreq.consumer_label)) {
800 le->label = kstrdup(eventreq.consumer_label,
801 GFP_KERNEL);
802 if (!le->label) {
803 ret = -ENOMEM;
804 goto out_free_le;
805 }
806 }
807
808 offset = eventreq.lineoffset;
809 lflags = eventreq.handleflags;
810 eflags = eventreq.eventflags;
811
Lars-Peter Clausenb8b0e3d2016-10-18 16:54:03 +0200812 if (offset >= gdev->ngpio) {
813 ret = -EINVAL;
814 goto out_free_label;
815 }
816
Lars-Peter Clausenac7dbb92016-10-18 16:54:06 +0200817 /* Return an error if a unknown flag is set */
818 if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
819 (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) {
820 ret = -EINVAL;
821 goto out_free_label;
822 }
823
Linus Walleij61f922d2016-06-02 11:30:15 +0200824 /* This is just wrong: we don't look for events on output lines */
825 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
826 ret = -EINVAL;
827 goto out_free_label;
828 }
829
830 desc = &gdev->descs[offset];
831 ret = gpiod_request(desc, le->label);
832 if (ret)
833 goto out_free_desc;
834 le->desc = desc;
835 le->eflags = eflags;
836
837 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
838 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
839 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
840 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
841 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
842 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
843
844 ret = gpiod_direction_input(desc);
845 if (ret)
846 goto out_free_desc;
847
848 le->irq = gpiod_to_irq(desc);
849 if (le->irq <= 0) {
850 ret = -ENODEV;
851 goto out_free_desc;
852 }
853
854 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
855 irqflags |= IRQF_TRIGGER_RISING;
856 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
857 irqflags |= IRQF_TRIGGER_FALLING;
858 irqflags |= IRQF_ONESHOT;
859 irqflags |= IRQF_SHARED;
860
861 INIT_KFIFO(le->events);
862 init_waitqueue_head(&le->wait);
863 mutex_init(&le->read_lock);
864
865 /* Request a thread to read the events */
866 ret = request_threaded_irq(le->irq,
867 NULL,
868 lineevent_irq_thread,
869 irqflags,
870 le->label,
871 le);
872 if (ret)
873 goto out_free_desc;
874
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200875 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleij61f922d2016-06-02 11:30:15 +0200876 if (fd < 0) {
877 ret = fd;
878 goto out_free_irq;
879 }
880
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200881 file = anon_inode_getfile("gpio-event",
882 &lineevent_fileops,
883 le,
884 O_RDONLY | O_CLOEXEC);
885 if (IS_ERR(file)) {
886 ret = PTR_ERR(file);
887 goto out_put_unused_fd;
888 }
889
Linus Walleij61f922d2016-06-02 11:30:15 +0200890 eventreq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200891 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200892 /*
893 * fput() will trigger the release() callback, so do not go onto
894 * the regular error cleanup path here.
895 */
896 fput(file);
897 put_unused_fd(fd);
898 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +0200899 }
Linus Walleij61f922d2016-06-02 11:30:15 +0200900
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200901 fd_install(fd, file);
902
Linus Walleij61f922d2016-06-02 11:30:15 +0200903 return 0;
904
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200905out_put_unused_fd:
906 put_unused_fd(fd);
Linus Walleij61f922d2016-06-02 11:30:15 +0200907out_free_irq:
908 free_irq(le->irq, le);
909out_free_desc:
910 gpiod_free(le->desc);
911out_free_label:
912 kfree(le->label);
913out_free_le:
914 kfree(le);
915 put_device(&gdev->dev);
916 return ret;
917}
918
Thierry Reding950d55f52017-07-24 16:57:22 +0200919/*
Linus Walleij3c702e92015-10-21 15:29:53 +0200920 * gpio_ioctl() - ioctl handler for the GPIO chardev
921 */
922static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
923{
924 struct gpio_device *gdev = filp->private_data;
925 struct gpio_chip *chip = gdev->chip;
Linus Walleij8b92e172016-05-27 14:24:04 +0200926 void __user *ip = (void __user *)arg;
Linus Walleij3c702e92015-10-21 15:29:53 +0200927
928 /* We fail any subsequent ioctl():s when the chip is gone */
929 if (!chip)
930 return -ENODEV;
931
Linus Walleij521a2ad2016-02-12 22:25:22 +0100932 /* Fill in the struct and pass to userspace */
Linus Walleij3c702e92015-10-21 15:29:53 +0200933 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
Linus Walleij521a2ad2016-02-12 22:25:22 +0100934 struct gpiochip_info chipinfo;
935
Lars-Peter Clausen0f4bbb22016-10-18 16:54:00 +0200936 memset(&chipinfo, 0, sizeof(chipinfo));
937
Linus Walleij3c702e92015-10-21 15:29:53 +0200938 strncpy(chipinfo.name, dev_name(&gdev->dev),
939 sizeof(chipinfo.name));
940 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
Linus Walleijdf4878e2016-02-12 14:48:23 +0100941 strncpy(chipinfo.label, gdev->label,
942 sizeof(chipinfo.label));
943 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100944 chipinfo.lines = gdev->ngpio;
Linus Walleij3c702e92015-10-21 15:29:53 +0200945 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
946 return -EFAULT;
947 return 0;
Linus Walleij521a2ad2016-02-12 22:25:22 +0100948 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
949 struct gpioline_info lineinfo;
950 struct gpio_desc *desc;
951
952 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
953 return -EFAULT;
Lars-Peter Clausen1f1cc452016-10-18 16:53:59 +0200954 if (lineinfo.line_offset >= gdev->ngpio)
Linus Walleij521a2ad2016-02-12 22:25:22 +0100955 return -EINVAL;
956
957 desc = &gdev->descs[lineinfo.line_offset];
958 if (desc->name) {
959 strncpy(lineinfo.name, desc->name,
960 sizeof(lineinfo.name));
961 lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
962 } else {
963 lineinfo.name[0] = '\0';
964 }
965 if (desc->label) {
Linus Walleij214338e2016-02-25 21:01:48 +0100966 strncpy(lineinfo.consumer, desc->label,
967 sizeof(lineinfo.consumer));
968 lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +0100969 } else {
Linus Walleij214338e2016-02-25 21:01:48 +0100970 lineinfo.consumer[0] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +0100971 }
972
973 /*
974 * Userspace only need to know that the kernel is using
975 * this GPIO so it can't use it.
976 */
977 lineinfo.flags = 0;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100978 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
979 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
980 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
981 test_bit(FLAG_EXPORT, &desc->flags) ||
982 test_bit(FLAG_SYSFS, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100983 lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100984 if (test_bit(FLAG_IS_OUT, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100985 lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100986 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100987 lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100988 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100989 lineinfo.flags |= GPIOLINE_FLAG_OPEN_DRAIN;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100990 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100991 lineinfo.flags |= GPIOLINE_FLAG_OPEN_SOURCE;
992
993 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
994 return -EFAULT;
995 return 0;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200996 } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
997 return linehandle_create(gdev, ip);
Linus Walleij61f922d2016-06-02 11:30:15 +0200998 } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
999 return lineevent_create(gdev, ip);
Linus Walleij3c702e92015-10-21 15:29:53 +02001000 }
1001 return -EINVAL;
1002}
1003
Linus Walleij8b92e172016-05-27 14:24:04 +02001004#ifdef CONFIG_COMPAT
1005static long gpio_ioctl_compat(struct file *filp, unsigned int cmd,
1006 unsigned long arg)
1007{
1008 return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
1009}
1010#endif
1011
Linus Walleij3c702e92015-10-21 15:29:53 +02001012/**
1013 * gpio_chrdev_open() - open the chardev for ioctl operations
1014 * @inode: inode for this chardev
1015 * @filp: file struct for storing private data
1016 * Returns 0 on success
1017 */
1018static int gpio_chrdev_open(struct inode *inode, struct file *filp)
1019{
1020 struct gpio_device *gdev = container_of(inode->i_cdev,
1021 struct gpio_device, chrdev);
1022
1023 /* Fail on open if the backing gpiochip is gone */
Stephen Boydfb505742017-01-09 11:47:44 -08001024 if (!gdev->chip)
Linus Walleij3c702e92015-10-21 15:29:53 +02001025 return -ENODEV;
1026 get_device(&gdev->dev);
1027 filp->private_data = gdev;
Lars-Peter Clausenf4e81c52016-11-30 13:05:21 +01001028
1029 return nonseekable_open(inode, filp);
Linus Walleij3c702e92015-10-21 15:29:53 +02001030}
1031
1032/**
1033 * gpio_chrdev_release() - close chardev after ioctl operations
1034 * @inode: inode for this chardev
1035 * @filp: file struct for storing private data
1036 * Returns 0 on success
1037 */
1038static int gpio_chrdev_release(struct inode *inode, struct file *filp)
1039{
1040 struct gpio_device *gdev = container_of(inode->i_cdev,
1041 struct gpio_device, chrdev);
1042
Linus Walleij3c702e92015-10-21 15:29:53 +02001043 put_device(&gdev->dev);
1044 return 0;
1045}
1046
1047
1048static const struct file_operations gpio_fileops = {
1049 .release = gpio_chrdev_release,
1050 .open = gpio_chrdev_open,
1051 .owner = THIS_MODULE,
Lars-Peter Clausenf4e81c52016-11-30 13:05:21 +01001052 .llseek = no_llseek,
Linus Walleij3c702e92015-10-21 15:29:53 +02001053 .unlocked_ioctl = gpio_ioctl,
Linus Walleij8b92e172016-05-27 14:24:04 +02001054#ifdef CONFIG_COMPAT
1055 .compat_ioctl = gpio_ioctl_compat,
1056#endif
Linus Walleij3c702e92015-10-21 15:29:53 +02001057};
1058
Linus Walleijff2b1352015-10-20 11:10:38 +02001059static void gpiodevice_release(struct device *dev)
1060{
1061 struct gpio_device *gdev = dev_get_drvdata(dev);
1062
1063 list_del(&gdev->list);
1064 ida_simple_remove(&gpio_ida, gdev->id);
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +01001065 kfree_const(gdev->label);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001066 kfree(gdev->descs);
Linus Walleij9efd9e62016-02-09 14:27:42 +01001067 kfree(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001068}
1069
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001070static int gpiochip_setup_dev(struct gpio_device *gdev)
1071{
1072 int status;
1073
1074 cdev_init(&gdev->chrdev, &gpio_fileops);
1075 gdev->chrdev.owner = THIS_MODULE;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001076 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001077
1078 status = cdev_device_add(&gdev->chrdev, &gdev->dev);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001079 if (status)
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001080 return status;
1081
1082 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
1083 MAJOR(gpio_devt), gdev->id);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001084
1085 status = gpiochip_sysfs_register(gdev);
1086 if (status)
1087 goto err_remove_device;
1088
1089 /* From this point, the .release() function cleans up gpio_device */
1090 gdev->dev.release = gpiodevice_release;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001091 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
1092 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
1093 dev_name(&gdev->dev), gdev->chip->label ? : "generic");
1094
1095 return 0;
1096
1097err_remove_device:
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001098 cdev_device_del(&gdev->chrdev, &gdev->dev);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001099 return status;
1100}
1101
1102static void gpiochip_setup_devs(void)
1103{
1104 struct gpio_device *gdev;
1105 int err;
1106
1107 list_for_each_entry(gdev, &gpio_devices, list) {
1108 err = gpiochip_setup_dev(gdev);
1109 if (err)
1110 pr_err("%s: Failed to initialize gpio device (%d)\n",
1111 dev_name(&gdev->dev), err);
1112 }
1113}
1114
Thierry Reding959bc7b2017-11-07 19:15:59 +01001115int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
1116 struct lock_class_key *key)
David Brownelld2876d02008-02-04 22:28:20 -08001117{
1118 unsigned long flags;
1119 int status = 0;
Linus Walleijff2b1352015-10-20 11:10:38 +02001120 unsigned i;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001121 int base = chip->base;
Linus Walleijff2b1352015-10-20 11:10:38 +02001122 struct gpio_device *gdev;
David Brownelld2876d02008-02-04 22:28:20 -08001123
Linus Walleijff2b1352015-10-20 11:10:38 +02001124 /*
1125 * First: allocate and populate the internal stat container, and
1126 * set up the struct device.
1127 */
Josh Cartwright969f07b2016-02-17 16:44:15 -06001128 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
Linus Walleijff2b1352015-10-20 11:10:38 +02001129 if (!gdev)
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001130 return -ENOMEM;
Linus Walleij3c702e92015-10-21 15:29:53 +02001131 gdev->dev.bus = &gpio_bus_type;
Linus Walleijff2b1352015-10-20 11:10:38 +02001132 gdev->chip = chip;
1133 chip->gpiodev = gdev;
1134 if (chip->parent) {
1135 gdev->dev.parent = chip->parent;
1136 gdev->dev.of_node = chip->parent->of_node;
Thierry Redingacc6e332016-07-05 14:11:14 +02001137 }
1138
Linus Walleijff2b1352015-10-20 11:10:38 +02001139#ifdef CONFIG_OF_GPIO
1140 /* If the gpiochip has an assigned OF node this takes precedence */
Thierry Redingacc6e332016-07-05 14:11:14 +02001141 if (chip->of_node)
1142 gdev->dev.of_node = chip->of_node;
Linus Walleijff2b1352015-10-20 11:10:38 +02001143#endif
Thierry Redingacc6e332016-07-05 14:11:14 +02001144
Linus Walleijff2b1352015-10-20 11:10:38 +02001145 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
1146 if (gdev->id < 0) {
1147 status = gdev->id;
1148 goto err_free_gdev;
1149 }
1150 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
1151 device_initialize(&gdev->dev);
1152 dev_set_drvdata(&gdev->dev, gdev);
1153 if (chip->parent && chip->parent->driver)
1154 gdev->owner = chip->parent->driver->owner;
1155 else if (chip->owner)
1156 /* TODO: remove chip->owner */
1157 gdev->owner = chip->owner;
1158 else
1159 gdev->owner = THIS_MODULE;
David Brownelld2876d02008-02-04 22:28:20 -08001160
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001161 gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001162 if (!gdev->descs) {
Linus Walleijff2b1352015-10-20 11:10:38 +02001163 status = -ENOMEM;
1164 goto err_free_gdev;
1165 }
1166
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001167 if (chip->ngpio == 0) {
1168 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
Linus Walleijff2b1352015-10-20 11:10:38 +02001169 status = -EINVAL;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001170 goto err_free_descs;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001171 }
Linus Walleijdf4878e2016-02-12 14:48:23 +01001172
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +01001173 gdev->label = kstrdup_const(chip->label ?: "unknown", GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001174 if (!gdev->label) {
1175 status = -ENOMEM;
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001176 goto err_free_descs;
Linus Walleijdf4878e2016-02-12 14:48:23 +01001177 }
1178
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001179 gdev->ngpio = chip->ngpio;
Linus Walleij43c54ec2016-02-11 11:37:48 +01001180 gdev->data = data;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001181
David Brownelld2876d02008-02-04 22:28:20 -08001182 spin_lock_irqsave(&gpio_lock, flags);
1183
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001184 /*
1185 * TODO: this allocates a Linux GPIO number base in the global
1186 * GPIO numberspace for this chip. In the long run we want to
1187 * get *rid* of this numberspace and use only descriptors, but
1188 * it may be a pipe dream. It will not happen before we get rid
1189 * of the sysfs interface anyways.
1190 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001191 if (base < 0) {
1192 base = gpiochip_find_base(chip->ngpio);
1193 if (base < 0) {
1194 status = base;
Johan Hovold225fce82015-01-12 17:12:25 +01001195 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001196 goto err_free_label;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001197 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001198 /*
1199 * TODO: it should not be necessary to reflect the assigned
1200 * base outside of the GPIO subsystem. Go over drivers and
1201 * see if anyone makes use of this, else drop this and assign
1202 * a poison instead.
1203 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001204 chip->base = base;
1205 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001206 gdev->base = base;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001207
Linus Walleijff2b1352015-10-20 11:10:38 +02001208 status = gpiodev_add_to_list(gdev);
Johan Hovold05aa5202015-01-12 17:12:26 +01001209 if (status) {
1210 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001211 goto err_free_label;
Johan Hovold05aa5202015-01-12 17:12:26 +01001212 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001213
Linus Walleij545ebd92016-05-30 17:11:59 +02001214 spin_unlock_irqrestore(&gpio_lock, flags);
1215
Linus Walleijff2b1352015-10-20 11:10:38 +02001216 for (i = 0; i < chip->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001217 struct gpio_desc *desc = &gdev->descs[i];
David Brownelld8f388d82008-07-25 01:46:07 -07001218
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001219 desc->gdev = gdev;
Timur Tabi1ca2a922017-12-20 13:10:31 -06001220
1221 /* REVISIT: most hardware initializes GPIOs as inputs (often
1222 * with pullups enabled) so power usage is minimized. Linux
1223 * code should set the gpio direction first thing; but until
1224 * it does, and in case chip->get_direction is not set, we may
1225 * expose the wrong direction in sysfs.
Johan Hovold05aa5202015-01-12 17:12:26 +01001226 */
Timur Tabi1ca2a922017-12-20 13:10:31 -06001227 desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0;
David Brownelld2876d02008-02-04 22:28:20 -08001228 }
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001229
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301230#ifdef CONFIG_PINCTRL
Linus Walleij20ec3e32016-02-11 11:03:06 +01001231 INIT_LIST_HEAD(&gdev->pin_ranges);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301232#endif
1233
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001234 status = gpiochip_set_desc_names(chip);
1235 if (status)
1236 goto err_remove_from_list;
1237
Mika Westerberg79b804c2016-09-20 15:15:21 +03001238 status = gpiochip_irqchip_init_valid_mask(chip);
1239 if (status)
1240 goto err_remove_from_list;
1241
Thierry Reding959bc7b2017-11-07 19:15:59 +01001242 status = gpiochip_add_irqchip(chip, key);
Thierry Redinge0d89722017-11-07 19:15:54 +01001243 if (status)
1244 goto err_remove_chip;
1245
Tomeu Vizoso28355f82015-07-14 10:29:54 +02001246 status = of_gpiochip_add(chip);
1247 if (status)
1248 goto err_remove_chip;
1249
Mika Westerberg664e3e52014-01-08 12:40:54 +02001250 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001251
Linus Walleij3c702e92015-10-21 15:29:53 +02001252 /*
1253 * By first adding the chardev, and then adding the device,
1254 * we get a device node entry in sysfs under
1255 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1256 * coldplug of device nodes and other udev business.
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001257 * We can do this only if gpiolib has been initialized.
1258 * Otherwise, defer until later.
Linus Walleij3c702e92015-10-21 15:29:53 +02001259 */
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001260 if (gpiolib_initialized) {
1261 status = gpiochip_setup_dev(gdev);
1262 if (status)
1263 goto err_remove_chip;
1264 }
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001265 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001266
Johan Hovold225fce82015-01-12 17:12:25 +01001267err_remove_chip:
1268 acpi_gpiochip_remove(chip);
Johan Hovold6d867502015-05-04 17:23:25 +02001269 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +01001270 of_gpiochip_remove(chip);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001271 gpiochip_irqchip_free_valid_mask(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001272err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +01001273 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001274 list_del(&gdev->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001275 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001276err_free_label:
Bartosz Golaszewskifcf273e52017-12-14 15:29:20 +01001277 kfree_const(gdev->label);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001278err_free_descs:
1279 kfree(gdev->descs);
Linus Walleijff2b1352015-10-20 11:10:38 +02001280err_free_gdev:
1281 ida_simple_remove(&gpio_ida, gdev->id);
David Brownelld2876d02008-02-04 22:28:20 -08001282 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +02001283 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001284 gdev->base, gdev->base + gdev->ngpio - 1,
1285 chip->label ? : "generic");
1286 kfree(gdev);
David Brownelld2876d02008-02-04 22:28:20 -08001287 return status;
1288}
Thierry Reding959bc7b2017-11-07 19:15:59 +01001289EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);
David Brownelld2876d02008-02-04 22:28:20 -08001290
1291/**
Linus Walleij43c54ec2016-02-11 11:37:48 +01001292 * gpiochip_get_data() - get per-subdriver data for the chip
Thierry Reding950d55f52017-07-24 16:57:22 +02001293 * @chip: GPIO chip
1294 *
1295 * Returns:
1296 * The per-subdriver data for the chip.
Linus Walleij43c54ec2016-02-11 11:37:48 +01001297 */
1298void *gpiochip_get_data(struct gpio_chip *chip)
1299{
1300 return chip->gpiodev->data;
1301}
1302EXPORT_SYMBOL_GPL(gpiochip_get_data);
1303
1304/**
David Brownelld2876d02008-02-04 22:28:20 -08001305 * gpiochip_remove() - unregister a gpio_chip
1306 * @chip: the chip to unregister
1307 *
1308 * A gpio_chip with any GPIOs still requested may not be removed.
1309 */
abdoulaye berthee1db1702014-07-05 18:28:50 +02001310void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -08001311{
Linus Walleijff2b1352015-10-20 11:10:38 +02001312 struct gpio_device *gdev = chip->gpiodev;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001313 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001314 unsigned long flags;
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001315 unsigned i;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001316 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -08001317
Linus Walleijff2b1352015-10-20 11:10:38 +02001318 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
Linus Walleijafbc4f32016-02-09 13:21:06 +01001319 gpiochip_sysfs_unregister(gdev);
Geert Uytterhoeven5018ada2016-12-19 18:29:23 +01001320 gpiochip_free_hogs(chip);
Bamvor Jian Zhangbd203bd2016-02-20 13:13:19 +08001321 /* Numb the device, cancelling all outstanding operations */
1322 gdev->chip = NULL;
Johan Hovold00acc3d2015-01-12 17:12:27 +01001323 gpiochip_irqchip_remove(chip);
Mika Westerberg6072b9d2014-03-10 14:54:53 +02001324 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +01001325 gpiochip_remove_pin_ranges(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001326 of_gpiochip_remove(chip);
Linus Walleij43c54ec2016-02-11 11:37:48 +01001327 /*
1328 * We accept no more calls into the driver from this point, so
1329 * NULL the driver data pointer
1330 */
1331 gdev->data = NULL;
Anton Vorontsov391c9702010-06-08 07:48:17 -06001332
Johan Hovold6798aca2015-01-12 17:12:28 +01001333 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001334 for (i = 0; i < gdev->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001335 desc = &gdev->descs[i];
Johan Hovoldfab28b82015-05-04 17:10:27 +02001336 if (test_bit(FLAG_REQUESTED, &desc->flags))
1337 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -08001338 }
David Brownelld2876d02008-02-04 22:28:20 -08001339 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001340
Johan Hovoldfab28b82015-05-04 17:10:27 +02001341 if (requested)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001342 dev_crit(&gdev->dev,
Linus Walleij58383c782015-11-04 09:56:26 +01001343 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +02001344
Linus Walleijff2b1352015-10-20 11:10:38 +02001345 /*
1346 * The gpiochip side puts its use of the device to rest here:
1347 * if there are no userspace clients, the chardev and device will
1348 * be removed, else it will be dangling until the last user is
1349 * gone.
1350 */
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001351 cdev_device_del(&gdev->chrdev, &gdev->dev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001352 put_device(&gdev->dev);
David Brownelld2876d02008-02-04 22:28:20 -08001353}
1354EXPORT_SYMBOL_GPL(gpiochip_remove);
1355
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301356static void devm_gpio_chip_release(struct device *dev, void *res)
1357{
1358 struct gpio_chip *chip = *(struct gpio_chip **)res;
1359
1360 gpiochip_remove(chip);
1361}
1362
1363static int devm_gpio_chip_match(struct device *dev, void *res, void *data)
1364
1365{
1366 struct gpio_chip **r = res;
1367
1368 if (!r || !*r) {
1369 WARN_ON(!r || !*r);
1370 return 0;
1371 }
1372
1373 return *r == data;
1374}
1375
1376/**
1377 * devm_gpiochip_add_data() - Resource manager piochip_add_data()
1378 * @dev: the device pointer on which irq_chip belongs to.
1379 * @chip: the chip to register, with chip->base initialized
Thierry Reding950d55f52017-07-24 16:57:22 +02001380 * @data: driver-private data associated with this chip
1381 *
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301382 * Context: potentially before irqs will work
1383 *
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301384 * The gpio chip automatically be released when the device is unbound.
Thierry Reding950d55f52017-07-24 16:57:22 +02001385 *
1386 * Returns:
1387 * A negative errno if the chip can't be registered, such as because the
1388 * chip->base is invalid or already associated with a different chip.
1389 * Otherwise it returns zero as a success code.
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301390 */
1391int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
1392 void *data)
1393{
1394 struct gpio_chip **ptr;
1395 int ret;
1396
1397 ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
1398 GFP_KERNEL);
1399 if (!ptr)
1400 return -ENOMEM;
1401
1402 ret = gpiochip_add_data(chip, data);
1403 if (ret < 0) {
1404 devres_free(ptr);
1405 return ret;
1406 }
1407
1408 *ptr = chip;
1409 devres_add(dev, ptr);
1410
1411 return 0;
1412}
1413EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
1414
1415/**
1416 * devm_gpiochip_remove() - Resource manager of gpiochip_remove()
1417 * @dev: device for which which resource was allocated
1418 * @chip: the chip to remove
1419 *
1420 * A gpio_chip with any GPIOs still requested may not be removed.
1421 */
1422void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip)
1423{
1424 int ret;
1425
1426 ret = devres_release(dev, devm_gpio_chip_release,
1427 devm_gpio_chip_match, chip);
Christophe JAILLET3988d662017-01-27 12:56:42 +01001428 WARN_ON(ret);
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301429}
1430EXPORT_SYMBOL_GPL(devm_gpiochip_remove);
1431
Grant Likely594fa262010-06-08 07:48:16 -06001432/**
1433 * gpiochip_find() - iterator for locating a specific gpio_chip
1434 * @data: data to pass to match function
Thierry Reding950d55f52017-07-24 16:57:22 +02001435 * @match: Callback function to check gpio_chip
Grant Likely594fa262010-06-08 07:48:16 -06001436 *
1437 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1438 * determined by a user supplied @match callback. The callback should return
1439 * 0 if the device doesn't match and non-zero if it does. If the callback is
1440 * non-zero, this function will return to the caller and not iterate over any
1441 * more gpio_chips.
1442 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001443struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001444 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001445 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001446{
Linus Walleijff2b1352015-10-20 11:10:38 +02001447 struct gpio_device *gdev;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001448 struct gpio_chip *chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -06001449 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001450
1451 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001452 list_for_each_entry(gdev, &gpio_devices, list)
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001453 if (gdev->chip && match(gdev->chip, data)) {
1454 chip = gdev->chip;
Grant Likely594fa262010-06-08 07:48:16 -06001455 break;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001456 }
Linus Walleijff2b1352015-10-20 11:10:38 +02001457
Grant Likely594fa262010-06-08 07:48:16 -06001458 spin_unlock_irqrestore(&gpio_lock, flags);
1459
1460 return chip;
1461}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001462EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001463
Alexandre Courbot79697ef2013-11-16 21:39:32 +09001464static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1465{
1466 const char *name = data;
1467
1468 return !strcmp(chip->label, name);
1469}
1470
1471static struct gpio_chip *find_chip_by_name(const char *name)
1472{
1473 return gpiochip_find((void *)name, gpiochip_match_name);
1474}
1475
Linus Walleij14250522014-03-25 10:40:18 +01001476#ifdef CONFIG_GPIOLIB_IRQCHIP
1477
1478/*
1479 * The following is irqchip helper code for gpiochips.
1480 */
1481
Mika Westerberg79b804c2016-09-20 15:15:21 +03001482static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1483{
Thierry Redingdc7b0382017-11-07 19:15:52 +01001484 if (!gpiochip->irq.need_valid_mask)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001485 return 0;
1486
Thierry Redingdc7b0382017-11-07 19:15:52 +01001487 gpiochip->irq.valid_mask = kcalloc(BITS_TO_LONGS(gpiochip->ngpio),
Mika Westerberg79b804c2016-09-20 15:15:21 +03001488 sizeof(long), GFP_KERNEL);
Thierry Redingdc7b0382017-11-07 19:15:52 +01001489 if (!gpiochip->irq.valid_mask)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001490 return -ENOMEM;
1491
1492 /* Assume by default all GPIOs are valid */
Thierry Redingdc7b0382017-11-07 19:15:52 +01001493 bitmap_fill(gpiochip->irq.valid_mask, gpiochip->ngpio);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001494
1495 return 0;
1496}
1497
1498static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1499{
Thierry Redingdc7b0382017-11-07 19:15:52 +01001500 kfree(gpiochip->irq.valid_mask);
1501 gpiochip->irq.valid_mask = NULL;
Mika Westerberg79b804c2016-09-20 15:15:21 +03001502}
1503
1504static bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
1505 unsigned int offset)
1506{
1507 /* No mask means all valid */
Thierry Redingdc7b0382017-11-07 19:15:52 +01001508 if (likely(!gpiochip->irq.valid_mask))
Mika Westerberg79b804c2016-09-20 15:15:21 +03001509 return true;
Thierry Redingdc7b0382017-11-07 19:15:52 +01001510 return test_bit(offset, gpiochip->irq.valid_mask);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001511}
1512
Linus Walleij14250522014-03-25 10:40:18 +01001513/**
Linus Walleijd245b3f2016-11-24 10:57:25 +01001514 * gpiochip_set_cascaded_irqchip() - connects a cascaded irqchip to a gpiochip
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001515 * @gpiochip: the gpiochip to set the irqchip chain to
1516 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01001517 * @parent_irq: the irq number corresponding to the parent IRQ for this
1518 * chained irqchip
1519 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001520 * coming out of the gpiochip. If the interrupt is nested rather than
1521 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +01001522 */
Linus Walleijd245b3f2016-11-24 10:57:25 +01001523static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gpiochip,
1524 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001525 unsigned int parent_irq,
Linus Walleijd245b3f2016-11-24 10:57:25 +01001526 irq_flow_handler_t parent_handler)
Linus Walleij14250522014-03-25 10:40:18 +01001527{
Linus Walleij83141a72014-09-26 13:50:12 +02001528 unsigned int offset;
1529
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001530 if (!gpiochip->irq.domain) {
Linus Walleij83141a72014-09-26 13:50:12 +02001531 chip_err(gpiochip, "called %s before setting up irqchip\n",
1532 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +02001533 return;
1534 }
1535
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001536 if (parent_handler) {
1537 if (gpiochip->can_sleep) {
1538 chip_err(gpiochip,
1539 "you cannot have chained interrupts on a "
1540 "chip that may sleep\n");
1541 return;
1542 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001543 /*
1544 * The parent irqchip is already using the chip_data for this
1545 * irqchip, so our callbacks simply use the handler_data.
1546 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +02001547 irq_set_chained_handler_and_data(parent_irq, parent_handler,
1548 gpiochip);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001549
Thierry Reding39e5f092017-11-07 19:15:50 +01001550 gpiochip->irq.parents = &parent_irq;
1551 gpiochip->irq.num_parents = 1;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001552 }
Linus Walleij83141a72014-09-26 13:50:12 +02001553
1554 /* Set the parent IRQ for all affected IRQs */
Mika Westerberg79b804c2016-09-20 15:15:21 +03001555 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1556 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1557 continue;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001558 irq_set_parent(irq_find_mapping(gpiochip->irq.domain, offset),
Linus Walleij83141a72014-09-26 13:50:12 +02001559 parent_irq);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001560 }
Linus Walleij14250522014-03-25 10:40:18 +01001561}
Linus Walleijd245b3f2016-11-24 10:57:25 +01001562
1563/**
1564 * gpiochip_set_chained_irqchip() - connects a chained irqchip to a gpiochip
1565 * @gpiochip: the gpiochip to set the irqchip chain to
1566 * @irqchip: the irqchip to chain to the gpiochip
1567 * @parent_irq: the irq number corresponding to the parent IRQ for this
1568 * chained irqchip
1569 * @parent_handler: the parent interrupt handler for the accumulated IRQ
1570 * coming out of the gpiochip. If the interrupt is nested rather than
1571 * cascaded, pass NULL in this handler argument
1572 */
1573void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
1574 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001575 unsigned int parent_irq,
Linus Walleijd245b3f2016-11-24 10:57:25 +01001576 irq_flow_handler_t parent_handler)
1577{
Thierry Reding60ed54c2017-11-07 19:15:57 +01001578 if (gpiochip->irq.threaded) {
1579 chip_err(gpiochip, "tried to chain a threaded gpiochip\n");
1580 return;
1581 }
1582
Linus Walleijd245b3f2016-11-24 10:57:25 +01001583 gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
1584 parent_handler);
1585}
Linus Walleij14250522014-03-25 10:40:18 +01001586EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
1587
1588/**
Linus Walleijd245b3f2016-11-24 10:57:25 +01001589 * gpiochip_set_nested_irqchip() - connects a nested irqchip to a gpiochip
1590 * @gpiochip: the gpiochip to set the irqchip nested handler to
1591 * @irqchip: the irqchip to nest to the gpiochip
1592 * @parent_irq: the irq number corresponding to the parent IRQ for this
1593 * nested irqchip
1594 */
1595void gpiochip_set_nested_irqchip(struct gpio_chip *gpiochip,
1596 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001597 unsigned int parent_irq)
Linus Walleijd245b3f2016-11-24 10:57:25 +01001598{
Linus Walleijd245b3f2016-11-24 10:57:25 +01001599 gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
1600 NULL);
1601}
1602EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip);
1603
1604/**
Linus Walleij14250522014-03-25 10:40:18 +01001605 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1606 * @d: the irqdomain used by this irqchip
1607 * @irq: the global irq number used by this GPIO irqchip irq
1608 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1609 *
1610 * This function will set up the mapping for a certain IRQ line on a
1611 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1612 * stored inside the gpiochip.
1613 */
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001614int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1615 irq_hw_number_t hwirq)
Linus Walleij14250522014-03-25 10:40:18 +01001616{
1617 struct gpio_chip *chip = d->host_data;
Thierry Redinge0d89722017-11-07 19:15:54 +01001618 int err = 0;
Linus Walleij14250522014-03-25 10:40:18 +01001619
Grygorii Strashkodc749a02017-07-21 11:49:00 -05001620 if (!gpiochip_irqchip_irq_valid(chip, hwirq))
1621 return -ENXIO;
1622
Linus Walleij14250522014-03-25 10:40:18 +01001623 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001624 /*
1625 * This lock class tells lockdep that GPIO irqs are in a different
1626 * category than their parents, so it won't report false recursion.
1627 */
Thierry Redingca9df052017-11-07 19:15:53 +01001628 irq_set_lockdep_class(irq, chip->irq.lock_key);
Thierry Redingc7a0aa52017-11-07 19:15:48 +01001629 irq_set_chip_and_handler(irq, chip->irq.chip, chip->irq.handler);
Linus Walleijd245b3f2016-11-24 10:57:25 +01001630 /* Chips that use nested thread handlers have them marked */
Thierry Reding60ed54c2017-11-07 19:15:57 +01001631 if (chip->irq.threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02001632 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +01001633 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -05001634
Thierry Redinge0d89722017-11-07 19:15:54 +01001635 if (chip->irq.num_parents == 1)
1636 err = irq_set_parent(irq, chip->irq.parents[0]);
1637 else if (chip->irq.map)
1638 err = irq_set_parent(irq, chip->irq.map[hwirq]);
1639
1640 if (err < 0)
1641 return err;
1642
Linus Walleij1333b902014-04-23 16:45:12 +02001643 /*
1644 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1645 * is passed as default type.
1646 */
Thierry Reding3634eeb2017-11-07 19:15:49 +01001647 if (chip->irq.default_type != IRQ_TYPE_NONE)
1648 irq_set_irq_type(irq, chip->irq.default_type);
Linus Walleij14250522014-03-25 10:40:18 +01001649
1650 return 0;
1651}
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001652EXPORT_SYMBOL_GPL(gpiochip_irq_map);
Linus Walleij14250522014-03-25 10:40:18 +01001653
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001654void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
Linus Walleijc3626fd2014-03-28 20:42:01 +01001655{
Linus Walleij1c8732b2014-04-09 13:34:39 +02001656 struct gpio_chip *chip = d->host_data;
1657
Thierry Reding60ed54c2017-11-07 19:15:57 +01001658 if (chip->irq.threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02001659 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001660 irq_set_chip_and_handler(irq, NULL, NULL);
1661 irq_set_chip_data(irq, NULL);
1662}
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001663EXPORT_SYMBOL_GPL(gpiochip_irq_unmap);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001664
Linus Walleij14250522014-03-25 10:40:18 +01001665static const struct irq_domain_ops gpiochip_domain_ops = {
1666 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +01001667 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +01001668 /* Virtually all GPIO irqchips are twocell:ed */
1669 .xlate = irq_domain_xlate_twocell,
1670};
1671
1672static int gpiochip_irq_reqres(struct irq_data *d)
1673{
1674 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1675
Linus Walleijff2b1352015-10-20 11:10:38 +02001676 if (!try_module_get(chip->gpiodev->owner))
Grygorii Strashko5b76e792015-06-25 20:30:50 +03001677 return -ENODEV;
1678
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001679 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +01001680 chip_err(chip,
1681 "unable to lock HW IRQ %lu for IRQ\n",
1682 d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001683 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001684 return -EINVAL;
1685 }
1686 return 0;
1687}
1688
1689static void gpiochip_irq_relres(struct irq_data *d)
1690{
1691 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1692
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001693 gpiochip_unlock_as_irq(chip, d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001694 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001695}
1696
1697static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
1698{
Grygorii Strashkodc749a02017-07-21 11:49:00 -05001699 if (!gpiochip_irqchip_irq_valid(chip, offset))
1700 return -ENXIO;
Thierry Redinge0d89722017-11-07 19:15:54 +01001701
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001702 return irq_create_mapping(chip->irq.domain, offset);
Linus Walleij14250522014-03-25 10:40:18 +01001703}
1704
1705/**
Thierry Redinge0d89722017-11-07 19:15:54 +01001706 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
1707 * @gpiochip: the GPIO chip to add the IRQ chip to
Thierry Reding959bc7b2017-11-07 19:15:59 +01001708 * @lock_key: lockdep class
Thierry Redinge0d89722017-11-07 19:15:54 +01001709 */
Thierry Reding959bc7b2017-11-07 19:15:59 +01001710static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
1711 struct lock_class_key *lock_key)
Thierry Redinge0d89722017-11-07 19:15:54 +01001712{
1713 struct irq_chip *irqchip = gpiochip->irq.chip;
1714 const struct irq_domain_ops *ops;
1715 struct device_node *np;
1716 unsigned int type;
1717 unsigned int i;
1718
1719 if (!irqchip)
1720 return 0;
1721
1722 if (gpiochip->irq.parent_handler && gpiochip->can_sleep) {
1723 chip_err(gpiochip, "you cannot have chained interrupts on a "
1724 "chip that may sleep\n");
1725 return -EINVAL;
1726 }
1727
1728 np = gpiochip->gpiodev->dev.of_node;
1729 type = gpiochip->irq.default_type;
1730
1731 /*
1732 * Specifying a default trigger is a terrible idea if DT or ACPI is
1733 * used to configure the interrupts, as you may end up with
1734 * conflicting triggers. Tell the user, and reset to NONE.
1735 */
1736 if (WARN(np && type != IRQ_TYPE_NONE,
1737 "%s: Ignoring %u default trigger\n", np->full_name, type))
1738 type = IRQ_TYPE_NONE;
1739
1740 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
1741 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1742 "Ignoring %u default trigger\n", type);
1743 type = IRQ_TYPE_NONE;
1744 }
1745
1746 gpiochip->to_irq = gpiochip_to_irq;
1747 gpiochip->irq.default_type = type;
Thierry Reding959bc7b2017-11-07 19:15:59 +01001748 gpiochip->irq.lock_key = lock_key;
Thierry Redinge0d89722017-11-07 19:15:54 +01001749
1750 if (gpiochip->irq.domain_ops)
1751 ops = gpiochip->irq.domain_ops;
1752 else
1753 ops = &gpiochip_domain_ops;
1754
1755 gpiochip->irq.domain = irq_domain_add_simple(np, gpiochip->ngpio,
Thierry Reding8302cf52017-11-07 19:15:58 +01001756 gpiochip->irq.first,
1757 ops, gpiochip);
Thierry Redinge0d89722017-11-07 19:15:54 +01001758 if (!gpiochip->irq.domain)
1759 return -EINVAL;
1760
1761 /*
1762 * It is possible for a driver to override this, but only if the
1763 * alternative functions are both implemented.
1764 */
1765 if (!irqchip->irq_request_resources &&
1766 !irqchip->irq_release_resources) {
1767 irqchip->irq_request_resources = gpiochip_irq_reqres;
1768 irqchip->irq_release_resources = gpiochip_irq_relres;
1769 }
1770
1771 if (gpiochip->irq.parent_handler) {
1772 void *data = gpiochip->irq.parent_handler_data ?: gpiochip;
1773
1774 for (i = 0; i < gpiochip->irq.num_parents; i++) {
1775 /*
1776 * The parent IRQ chip is already using the chip_data
1777 * for this IRQ chip, so our callbacks simply use the
1778 * handler_data.
1779 */
1780 irq_set_chained_handler_and_data(gpiochip->irq.parents[i],
1781 gpiochip->irq.parent_handler,
1782 data);
1783 }
Thierry Redinge0d89722017-11-07 19:15:54 +01001784 }
1785
1786 acpi_gpiochip_request_interrupts(gpiochip);
1787
1788 return 0;
1789}
1790
1791/**
Linus Walleij14250522014-03-25 10:40:18 +01001792 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1793 * @gpiochip: the gpiochip to remove the irqchip from
1794 *
1795 * This is called only from gpiochip_remove()
1796 */
1797static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
1798{
Thierry Reding39e5f092017-11-07 19:15:50 +01001799 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +01001800
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001801 acpi_gpiochip_free_interrupts(gpiochip);
1802
Thierry Redinge0d89722017-11-07 19:15:54 +01001803 if (gpiochip->irq.chip && gpiochip->irq.parent_handler) {
Thierry Reding39e5f092017-11-07 19:15:50 +01001804 struct gpio_irq_chip *irq = &gpiochip->irq;
1805 unsigned int i;
1806
1807 for (i = 0; i < irq->num_parents; i++)
1808 irq_set_chained_handler_and_data(irq->parents[i],
1809 NULL, NULL);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001810 }
1811
Linus Walleijc3626fd2014-03-28 20:42:01 +01001812 /* Remove all IRQ mappings and delete the domain */
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001813 if (gpiochip->irq.domain) {
Thierry Reding39e5f092017-11-07 19:15:50 +01001814 unsigned int irq;
1815
Mika Westerberg79b804c2016-09-20 15:15:21 +03001816 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1817 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1818 continue;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001819
1820 irq = irq_find_mapping(gpiochip->irq.domain, offset);
1821 irq_dispose_mapping(irq);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001822 }
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001823
1824 irq_domain_remove(gpiochip->irq.domain);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001825 }
Linus Walleij14250522014-03-25 10:40:18 +01001826
Thierry Redingda80ff82017-11-07 19:15:46 +01001827 if (gpiochip->irq.chip) {
1828 gpiochip->irq.chip->irq_request_resources = NULL;
1829 gpiochip->irq.chip->irq_release_resources = NULL;
1830 gpiochip->irq.chip = NULL;
Linus Walleij14250522014-03-25 10:40:18 +01001831 }
Mika Westerberg79b804c2016-09-20 15:15:21 +03001832
1833 gpiochip_irqchip_free_valid_mask(gpiochip);
Linus Walleij14250522014-03-25 10:40:18 +01001834}
1835
1836/**
Linus Walleij739e6f52017-01-11 13:37:07 +01001837 * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01001838 * @gpiochip: the gpiochip to add the irqchip to
1839 * @irqchip: the irqchip to add to the gpiochip
1840 * @first_irq: if not dynamically assigned, the base (first) IRQ to
1841 * allocate gpiochip irqs from
1842 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +02001843 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
1844 * to have the core avoid setting up any default type in the hardware.
Thierry Reding60ed54c2017-11-07 19:15:57 +01001845 * @threaded: whether this irqchip uses a nested thread handler
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001846 * @lock_key: lockdep class
Linus Walleij14250522014-03-25 10:40:18 +01001847 *
1848 * This function closely associates a certain irqchip with a certain
1849 * gpiochip, providing an irq domain to translate the local IRQs to
1850 * global irqs in the gpiolib core, and making sure that the gpiochip
1851 * is passed as chip data to all related functions. Driver callbacks
Linus Walleij09dd5f92015-12-07 15:31:58 +01001852 * need to use gpiochip_get_data() to get their local state containers back
Linus Walleij14250522014-03-25 10:40:18 +01001853 * from the gpiochip passed as chip data. An irqdomain will be stored
1854 * in the gpiochip that shall be used by the driver to handle IRQ number
1855 * translation. The gpiochip will need to be initialized and registered
1856 * before calling this function.
1857 *
Linus Walleijc3626fd2014-03-28 20:42:01 +01001858 * This function will handle two cell:ed simple IRQs and assumes all
1859 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +01001860 * need to be open coded.
1861 */
Linus Walleij739e6f52017-01-11 13:37:07 +01001862int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
1863 struct irq_chip *irqchip,
1864 unsigned int first_irq,
1865 irq_flow_handler_t handler,
1866 unsigned int type,
Thierry Reding60ed54c2017-11-07 19:15:57 +01001867 bool threaded,
Linus Walleij739e6f52017-01-11 13:37:07 +01001868 struct lock_class_key *lock_key)
Linus Walleij14250522014-03-25 10:40:18 +01001869{
1870 struct device_node *of_node;
Linus Walleij14250522014-03-25 10:40:18 +01001871
1872 if (!gpiochip || !irqchip)
1873 return -EINVAL;
1874
Linus Walleij58383c782015-11-04 09:56:26 +01001875 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +01001876 pr_err("missing gpiochip .dev parent pointer\n");
1877 return -EINVAL;
1878 }
Thierry Reding60ed54c2017-11-07 19:15:57 +01001879 gpiochip->irq.threaded = threaded;
Linus Walleij58383c782015-11-04 09:56:26 +01001880 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +01001881#ifdef CONFIG_OF_GPIO
1882 /*
Colin Cronin20a8a962015-05-18 11:41:43 -07001883 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08001884 * FIXME: get rid of this and use gpiochip->parent->of_node
1885 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +01001886 */
1887 if (gpiochip->of_node)
1888 of_node = gpiochip->of_node;
1889#endif
Marc Zyngier332e99d2016-09-07 09:12:11 +01001890 /*
Mika Westerberg0a1e0052016-09-12 14:29:51 +03001891 * Specifying a default trigger is a terrible idea if DT or ACPI is
Marc Zyngier332e99d2016-09-07 09:12:11 +01001892 * used to configure the interrupts, as you may end-up with
1893 * conflicting triggers. Tell the user, and reset to NONE.
1894 */
1895 if (WARN(of_node && type != IRQ_TYPE_NONE,
Rob Herring7eb6ce22017-07-18 16:43:03 -05001896 "%pOF: Ignoring %d default trigger\n", of_node, type))
Marc Zyngier332e99d2016-09-07 09:12:11 +01001897 type = IRQ_TYPE_NONE;
Mika Westerberg0a1e0052016-09-12 14:29:51 +03001898 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
1899 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1900 "Ignoring %d default trigger\n", type);
1901 type = IRQ_TYPE_NONE;
1902 }
Marc Zyngier332e99d2016-09-07 09:12:11 +01001903
Thierry Redingda80ff82017-11-07 19:15:46 +01001904 gpiochip->irq.chip = irqchip;
Thierry Redingc7a0aa52017-11-07 19:15:48 +01001905 gpiochip->irq.handler = handler;
Thierry Reding3634eeb2017-11-07 19:15:49 +01001906 gpiochip->irq.default_type = type;
Linus Walleij14250522014-03-25 10:40:18 +01001907 gpiochip->to_irq = gpiochip_to_irq;
Thierry Redingca9df052017-11-07 19:15:53 +01001908 gpiochip->irq.lock_key = lock_key;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001909 gpiochip->irq.domain = irq_domain_add_simple(of_node,
Linus Walleij14250522014-03-25 10:40:18 +01001910 gpiochip->ngpio, first_irq,
1911 &gpiochip_domain_ops, gpiochip);
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001912 if (!gpiochip->irq.domain) {
Thierry Redingda80ff82017-11-07 19:15:46 +01001913 gpiochip->irq.chip = NULL;
Linus Walleij14250522014-03-25 10:40:18 +01001914 return -EINVAL;
1915 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +02001916
1917 /*
1918 * It is possible for a driver to override this, but only if the
1919 * alternative functions are both implemented.
1920 */
1921 if (!irqchip->irq_request_resources &&
1922 !irqchip->irq_release_resources) {
1923 irqchip->irq_request_resources = gpiochip_irq_reqres;
1924 irqchip->irq_release_resources = gpiochip_irq_relres;
1925 }
Linus Walleij14250522014-03-25 10:40:18 +01001926
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001927 acpi_gpiochip_request_interrupts(gpiochip);
1928
Linus Walleij14250522014-03-25 10:40:18 +01001929 return 0;
1930}
Linus Walleij739e6f52017-01-11 13:37:07 +01001931EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key);
Linus Walleij14250522014-03-25 10:40:18 +01001932
1933#else /* CONFIG_GPIOLIB_IRQCHIP */
1934
Thierry Reding959bc7b2017-11-07 19:15:59 +01001935static inline int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
Linus Walleijec478732017-11-08 21:40:41 +01001936 struct lock_class_key *key)
Thierry Redinge0d89722017-11-07 19:15:54 +01001937{
1938 return 0;
1939}
1940
Linus Walleij14250522014-03-25 10:40:18 +01001941static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
Mika Westerberg79b804c2016-09-20 15:15:21 +03001942static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1943{
1944 return 0;
1945}
1946static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1947{ }
Linus Walleij14250522014-03-25 10:40:18 +01001948
1949#endif /* CONFIG_GPIOLIB_IRQCHIP */
1950
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001951/**
1952 * gpiochip_generic_request() - request the gpio function for a pin
1953 * @chip: the gpiochip owning the GPIO
1954 * @offset: the offset of the GPIO to request for GPIO function
1955 */
1956int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
1957{
Linus Walleija9a1d2a2017-09-22 11:02:10 +02001958 return pinctrl_gpio_request(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001959}
1960EXPORT_SYMBOL_GPL(gpiochip_generic_request);
1961
1962/**
1963 * gpiochip_generic_free() - free the gpio function from a pin
1964 * @chip: the gpiochip to request the gpio function for
1965 * @offset: the offset of the GPIO to free from GPIO function
1966 */
1967void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
1968{
Linus Walleija9a1d2a2017-09-22 11:02:10 +02001969 pinctrl_gpio_free(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001970}
1971EXPORT_SYMBOL_GPL(gpiochip_generic_free);
1972
Mika Westerberg2956b5d2017-01-23 15:34:34 +03001973/**
1974 * gpiochip_generic_config() - apply configuration for a pin
1975 * @chip: the gpiochip owning the GPIO
1976 * @offset: the offset of the GPIO to apply the configuration
1977 * @config: the configuration to be applied
1978 */
1979int gpiochip_generic_config(struct gpio_chip *chip, unsigned offset,
1980 unsigned long config)
1981{
1982 return pinctrl_gpio_set_config(chip->gpiodev->base + offset, config);
1983}
1984EXPORT_SYMBOL_GPL(gpiochip_generic_config);
1985
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301986#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01001987
Linus Walleij3f0f8672012-11-20 12:40:15 +01001988/**
Christian Ruppert586a87e2013-10-15 15:37:54 +02001989 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1990 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +02001991 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +02001992 * @gpio_offset: the start offset in the current gpio_chip number space
1993 * @pin_group: name of the pin group inside the pin controller
1994 */
1995int gpiochip_add_pingroup_range(struct gpio_chip *chip,
1996 struct pinctrl_dev *pctldev,
1997 unsigned int gpio_offset, const char *pin_group)
1998{
1999 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002000 struct gpio_device *gdev = chip->gpiodev;
Christian Ruppert586a87e2013-10-15 15:37:54 +02002001 int ret;
2002
2003 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
2004 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002005 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +02002006 return -ENOMEM;
2007 }
2008
2009 /* Use local offset as range ID */
2010 pin_range->range.id = gpio_offset;
2011 pin_range->range.gc = chip;
2012 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002013 pin_range->range.base = gdev->base + gpio_offset;
Christian Ruppert586a87e2013-10-15 15:37:54 +02002014 pin_range->pctldev = pctldev;
2015
2016 ret = pinctrl_get_group_pins(pctldev, pin_group,
2017 &pin_range->range.pins,
2018 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +01002019 if (ret < 0) {
2020 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +02002021 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +01002022 }
Christian Ruppert586a87e2013-10-15 15:37:54 +02002023
2024 pinctrl_add_gpio_range(pctldev, &pin_range->range);
2025
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002026 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
2027 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +02002028 pinctrl_dev_get_devname(pctldev), pin_group);
2029
Linus Walleij20ec3e32016-02-11 11:03:06 +01002030 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Christian Ruppert586a87e2013-10-15 15:37:54 +02002031
2032 return 0;
2033}
2034EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
2035
2036/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01002037 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
2038 * @chip: the gpiochip to add the range for
Thierry Reding950d55f52017-07-24 16:57:22 +02002039 * @pinctl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01002040 * @gpio_offset: the start offset in the current gpio_chip number space
2041 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01002042 * @npins: the number of pins from the offset of each pin space (GPIO and
2043 * pin controller) to accumulate in this range
Thierry Reding950d55f52017-07-24 16:57:22 +02002044 *
2045 * Returns:
2046 * 0 on success, or a negative error-code on failure.
Linus Walleij3f0f8672012-11-20 12:40:15 +01002047 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002048int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01002049 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01002050 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302051{
2052 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002053 struct gpio_device *gdev = chip->gpiodev;
Axel Linb4d4b1f2012-11-21 14:33:56 +08002054 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302055
Linus Walleij3f0f8672012-11-20 12:40:15 +01002056 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302057 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002058 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002059 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302060 }
2061
Linus Walleij3f0f8672012-11-20 12:40:15 +01002062 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01002063 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01002064 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302065 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002066 pin_range->range.base = gdev->base + gpio_offset;
Linus Walleij316511c2012-11-21 08:48:09 +01002067 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302068 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01002069 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302070 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01002071 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08002072 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002073 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +01002074 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08002075 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01002076 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002077 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
2078 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +01002079 pinctl_name,
2080 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302081
Linus Walleij20ec3e32016-02-11 11:03:06 +01002082 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002083
2084 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302085}
Linus Walleij165adc92012-11-06 14:49:39 +01002086EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302087
Linus Walleij3f0f8672012-11-20 12:40:15 +01002088/**
2089 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
2090 * @chip: the chip to remove all the mappings for
2091 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302092void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
2093{
2094 struct gpio_pin_range *pin_range, *tmp;
Linus Walleij20ec3e32016-02-11 11:03:06 +01002095 struct gpio_device *gdev = chip->gpiodev;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302096
Linus Walleij20ec3e32016-02-11 11:03:06 +01002097 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302098 list_del(&pin_range->node);
2099 pinctrl_remove_gpio_range(pin_range->pctldev,
2100 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01002101 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302102 }
2103}
Linus Walleij165adc92012-11-06 14:49:39 +01002104EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
2105
2106#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302107
David Brownelld2876d02008-02-04 22:28:20 -08002108/* These "optional" allocation calls help prevent drivers from stomping
2109 * on each other, and help provide better diagnostics in debugfs.
2110 * They're called even less than the "set direction" calls.
2111 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002112static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08002113{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002114 struct gpio_chip *chip = desc->gdev->chip;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002115 int status;
David Brownelld2876d02008-02-04 22:28:20 -08002116 unsigned long flags;
2117
2118 spin_lock_irqsave(&gpio_lock, flags);
2119
David Brownelld2876d02008-02-04 22:28:20 -08002120 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07002121 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08002122 */
2123
2124 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
2125 desc_set_label(desc, label ? : "?");
2126 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07002127 } else {
David Brownelld2876d02008-02-04 22:28:20 -08002128 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -08002129 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07002130 }
2131
2132 if (chip->request) {
2133 /* chip->request may sleep */
2134 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002135 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07002136 spin_lock_irqsave(&gpio_lock, flags);
2137
2138 if (status < 0) {
2139 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -07002140 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03002141 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07002142 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07002143 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03002144 if (chip->get_direction) {
2145 /* chip->get_direction may sleep */
2146 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002147 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03002148 spin_lock_irqsave(&gpio_lock, flags);
2149 }
David Brownelld2876d02008-02-04 22:28:20 -08002150done:
Mika Westerberg77c2d792014-03-10 14:54:50 +02002151 spin_unlock_irqrestore(&gpio_lock, flags);
2152 return status;
2153}
2154
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002155/*
2156 * This descriptor validation needs to be inserted verbatim into each
2157 * function taking a descriptor, so we need to use a preprocessor
Linus Walleij54d77192016-05-30 16:48:39 +02002158 * macro to avoid endless duplication. If the desc is NULL it is an
2159 * optional GPIO and calls should just bail out.
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002160 */
2161#define VALIDATE_DESC(desc) do { \
Linus Walleij54d77192016-05-30 16:48:39 +02002162 if (!desc) \
2163 return 0; \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002164 if (IS_ERR(desc)) { \
2165 pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
2166 return PTR_ERR(desc); \
2167 } \
Linus Walleij54d77192016-05-30 16:48:39 +02002168 if (!desc->gdev) { \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002169 pr_warn("%s: invalid GPIO (no device)\n", __func__); \
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002170 return -EINVAL; \
2171 } \
2172 if ( !desc->gdev->chip ) { \
2173 dev_warn(&desc->gdev->dev, \
2174 "%s: backing chip is gone\n", __func__); \
2175 return 0; \
2176 } } while (0)
2177
2178#define VALIDATE_DESC_VOID(desc) do { \
Linus Walleij54d77192016-05-30 16:48:39 +02002179 if (!desc) \
2180 return; \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002181 if (IS_ERR(desc)) { \
2182 pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
2183 return; \
2184 } \
Linus Walleij54d77192016-05-30 16:48:39 +02002185 if (!desc->gdev) { \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002186 pr_warn("%s: invalid GPIO (no device)\n", __func__); \
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002187 return; \
2188 } \
2189 if (!desc->gdev->chip) { \
2190 dev_warn(&desc->gdev->dev, \
2191 "%s: backing chip is gone\n", __func__); \
2192 return; \
2193 } } while (0)
2194
2195
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002196int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002197{
2198 int status = -EPROBE_DEFER;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002199 struct gpio_device *gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002200
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002201 VALIDATE_DESC(desc);
2202 gdev = desc->gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002203
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002204 if (try_module_get(gdev->owner)) {
Linus Walleijfac9d882017-09-26 20:58:28 +02002205 status = gpiod_request_commit(desc, label);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002206 if (status < 0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002207 module_put(gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002208 else
2209 get_device(&gdev->dev);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002210 }
2211
David Brownelld2876d02008-02-04 22:28:20 -08002212 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +02002213 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002214
David Brownelld2876d02008-02-04 22:28:20 -08002215 return status;
2216}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002217
Linus Walleijfac9d882017-09-26 20:58:28 +02002218static bool gpiod_free_commit(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002219{
Mika Westerberg77c2d792014-03-10 14:54:50 +02002220 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08002221 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07002222 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002223
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07002224 might_sleep();
2225
Alexandre Courbot372e7222013-02-03 01:29:29 +09002226 gpiod_unexport(desc);
David Brownelld8f388d82008-07-25 01:46:07 -07002227
David Brownelld2876d02008-02-04 22:28:20 -08002228 spin_lock_irqsave(&gpio_lock, flags);
2229
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002230 chip = desc->gdev->chip;
David Brownell35e8bb52008-10-15 22:03:16 -07002231 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
2232 if (chip->free) {
2233 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07002234 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002235 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07002236 spin_lock_irqsave(&gpio_lock, flags);
2237 }
David Brownelld2876d02008-02-04 22:28:20 -08002238 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08002239 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07002240 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302241 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302242 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06002243 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002244 ret = true;
2245 }
David Brownelld2876d02008-02-04 22:28:20 -08002246
2247 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002248 return ret;
2249}
2250
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002251void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002252{
Linus Walleijfac9d882017-09-26 20:58:28 +02002253 if (desc && desc->gdev && gpiod_free_commit(desc)) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002254 module_put(desc->gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002255 put_device(&desc->gdev->dev);
2256 } else {
Mika Westerberg77c2d792014-03-10 14:54:50 +02002257 WARN_ON(extra_checks);
Linus Walleij33a68e82016-02-11 10:28:44 +01002258 }
David Brownelld2876d02008-02-04 22:28:20 -08002259}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002260
David Brownelld2876d02008-02-04 22:28:20 -08002261/**
2262 * gpiochip_is_requested - return string iff signal was requested
2263 * @chip: controller managing the signal
2264 * @offset: of signal within controller's 0..(ngpio - 1) range
2265 *
2266 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09002267 * The string returned is the label passed to gpio_request(); if none has been
2268 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08002269 *
2270 * This function is for use by GPIO controller drivers. The label can
2271 * help with diagnostics, and knowing that the signal is used as a GPIO
2272 * can help avoid accidentally multiplexing it to another controller.
2273 */
2274const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
2275{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002276 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08002277
Dirk Behme48b59532015-08-18 18:02:32 +02002278 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08002279 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002280
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002281 desc = &chip->gpiodev->descs[offset];
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002282
Alexandre Courbot372e7222013-02-03 01:29:29 +09002283 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08002284 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002285 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08002286}
2287EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2288
Mika Westerberg77c2d792014-03-10 14:54:50 +02002289/**
2290 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
Thierry Reding950d55f52017-07-24 16:57:22 +02002291 * @chip: GPIO chip
2292 * @hwnum: hardware number of the GPIO for which to request the descriptor
Mika Westerberg77c2d792014-03-10 14:54:50 +02002293 * @label: label for the GPIO
2294 *
2295 * Function allows GPIO chip drivers to request and use their own GPIO
2296 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2297 * function will not increase reference count of the GPIO chip module. This
2298 * allows the GPIO chip module to be unloaded as needed (we assume that the
2299 * GPIO chip driver handles freeing the GPIOs it has requested).
Thierry Reding950d55f52017-07-24 16:57:22 +02002300 *
2301 * Returns:
2302 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2303 * code on failure.
Mika Westerberg77c2d792014-03-10 14:54:50 +02002304 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002305struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
2306 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002307{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002308 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
2309 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002310
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002311 if (IS_ERR(desc)) {
2312 chip_err(chip, "failed to get GPIO descriptor\n");
2313 return desc;
2314 }
2315
Linus Walleijfac9d882017-09-26 20:58:28 +02002316 err = gpiod_request_commit(desc, label);
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002317 if (err < 0)
2318 return ERR_PTR(err);
2319
2320 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002321}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002322EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002323
2324/**
2325 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2326 * @desc: GPIO descriptor to free
2327 *
2328 * Function frees the given GPIO requested previously with
2329 * gpiochip_request_own_desc().
2330 */
2331void gpiochip_free_own_desc(struct gpio_desc *desc)
2332{
2333 if (desc)
Linus Walleijfac9d882017-09-26 20:58:28 +02002334 gpiod_free_commit(desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002335}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002336EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08002337
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002338/*
2339 * Drivers MUST set GPIO direction before making get/set calls. In
David Brownelld2876d02008-02-04 22:28:20 -08002340 * some cases this is done in early boot, before IRQs are enabled.
2341 *
2342 * As a rule these aren't called more than once (except for drivers
2343 * using the open-drain emulation idiom) so these are natural places
2344 * to accumulate extra debugging checks. Note that we can't (yet)
2345 * rely on gpio_request() having been called beforehand.
2346 */
2347
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002348/**
2349 * gpiod_direction_input - set the GPIO direction to input
2350 * @desc: GPIO to set to input
2351 *
2352 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2353 * be called safely on it.
2354 *
2355 * Return 0 in case of success, else an error code.
2356 */
2357int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002358{
David Brownelld2876d02008-02-04 22:28:20 -08002359 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002360 int status = -EINVAL;
2361
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002362 VALIDATE_DESC(desc);
2363 chip = desc->gdev->chip;
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002364
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002365 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01002366 gpiod_warn(desc,
2367 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02002368 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002369 return -EIO;
2370 }
2371
Alexandre Courbotd82da792014-07-22 16:17:43 +09002372 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -08002373 if (status == 0)
2374 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002375
Alexandre Courbot372e7222013-02-03 01:29:29 +09002376 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09002377
David Brownelld2876d02008-02-04 22:28:20 -08002378 return status;
2379}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002380EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002381
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002382static int gpio_set_drive_single_ended(struct gpio_chip *gc, unsigned offset,
2383 enum pin_config_param mode)
2384{
2385 unsigned long config = { PIN_CONF_PACKED(mode, 0) };
2386
2387 return gc->set_config ? gc->set_config(gc, offset, config) : -ENOTSUPP;
2388}
2389
Linus Walleijfac9d882017-09-26 20:58:28 +02002390static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08002391{
Linus Walleijc663e5f2016-03-22 10:51:16 +01002392 struct gpio_chip *gc = desc->gdev->chip;
Linus Walleijad177312016-11-13 23:02:44 +01002393 int val = !!value;
Linus Walleijc663e5f2016-03-22 10:51:16 +01002394 int ret;
David Brownelld2876d02008-02-04 22:28:20 -08002395
Linus Walleijc663e5f2016-03-22 10:51:16 +01002396 if (!gc->set || !gc->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01002397 gpiod_warn(desc,
2398 "%s: missing set() or direction_output() operations\n",
2399 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002400 return -EIO;
2401 }
2402
Linus Walleijad177312016-11-13 23:02:44 +01002403 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
Linus Walleijc663e5f2016-03-22 10:51:16 +01002404 if (!ret)
David Brownelld2876d02008-02-04 22:28:20 -08002405 set_bit(FLAG_IS_OUT, &desc->flags);
Linus Walleijad177312016-11-13 23:02:44 +01002406 trace_gpio_value(desc_to_gpio(desc), 0, val);
Linus Walleijc663e5f2016-03-22 10:51:16 +01002407 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2408 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08002409}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002410
2411/**
2412 * gpiod_direction_output_raw - set the GPIO direction to output
2413 * @desc: GPIO to set to output
2414 * @value: initial output value of the GPIO
2415 *
2416 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2417 * be called safely on it. The initial value of the output must be specified
2418 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2419 *
2420 * Return 0 in case of success, else an error code.
2421 */
2422int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2423{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002424 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02002425 return gpiod_direction_output_raw_commit(desc, value);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002426}
2427EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2428
2429/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05302430 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002431 * @desc: GPIO to set to output
2432 * @value: initial output value of the GPIO
2433 *
2434 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2435 * be called safely on it. The initial value of the output must be specified
2436 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2437 * account.
2438 *
2439 * Return 0 in case of success, else an error code.
2440 */
2441int gpiod_direction_output(struct gpio_desc *desc, int value)
2442{
Linus Walleij02e47982017-09-26 21:20:23 +02002443 struct gpio_chip *gc = desc->gdev->chip;
2444 int ret;
2445
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002446 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002447 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2448 value = !value;
Linus Walleijad177312016-11-13 23:02:44 +01002449 else
2450 value = !!value;
Linus Walleij02e47982017-09-26 21:20:23 +02002451
2452 /* GPIOs used for IRQs shall not be set as output */
2453 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
2454 gpiod_err(desc,
2455 "%s: tried to set a GPIO tied to an IRQ as output\n",
2456 __func__);
2457 return -EIO;
2458 }
2459
2460 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2461 /* First see if we can enable open drain in hardware */
2462 ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2463 PIN_CONFIG_DRIVE_OPEN_DRAIN);
2464 if (!ret)
2465 goto set_output_value;
2466 /* Emulate open drain by not actively driving the line high */
2467 if (value)
2468 return gpiod_direction_input(desc);
2469 }
2470 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2471 ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2472 PIN_CONFIG_DRIVE_OPEN_SOURCE);
2473 if (!ret)
2474 goto set_output_value;
2475 /* Emulate open source by not actively driving the line low */
2476 if (!value)
2477 return gpiod_direction_input(desc);
2478 } else {
2479 gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2480 PIN_CONFIG_DRIVE_PUSH_PULL);
2481 }
2482
2483set_output_value:
Linus Walleijfac9d882017-09-26 20:58:28 +02002484 return gpiod_direction_output_raw_commit(desc, value);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002485}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002486EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08002487
Felipe Balbic4b5be92010-05-26 14:42:23 -07002488/**
Thierry Reding950d55f52017-07-24 16:57:22 +02002489 * gpiod_set_debounce - sets @debounce time for a GPIO
2490 * @desc: descriptor of the GPIO for which to set debounce time
2491 * @debounce: debounce time in microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02002492 *
Thierry Reding950d55f52017-07-24 16:57:22 +02002493 * Returns:
2494 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2495 * debounce time.
Felipe Balbic4b5be92010-05-26 14:42:23 -07002496 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002497int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07002498{
Felipe Balbic4b5be92010-05-26 14:42:23 -07002499 struct gpio_chip *chip;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002500 unsigned long config;
Felipe Balbic4b5be92010-05-26 14:42:23 -07002501
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002502 VALIDATE_DESC(desc);
2503 chip = desc->gdev->chip;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002504 if (!chip->set || !chip->set_config) {
Mark Brown6424de52013-09-09 10:33:49 +01002505 gpiod_dbg(desc,
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002506 "%s: missing set() or set_config() operations\n",
Mark Brown6424de52013-09-09 10:33:49 +01002507 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02002508 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002509 }
2510
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002511 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
2512 return chip->set_config(chip, gpio_chip_hwgpio(desc), config);
Felipe Balbic4b5be92010-05-26 14:42:23 -07002513}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002514EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002515
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002516/**
Andrew Jefferye10f72b2017-11-30 14:25:24 +10302517 * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
2518 * @desc: descriptor of the GPIO for which to configure persistence
2519 * @transitory: True to lose state on suspend or reset, false for persistence
2520 *
2521 * Returns:
2522 * 0 on success, otherwise a negative error code.
2523 */
2524int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
2525{
2526 struct gpio_chip *chip;
2527 unsigned long packed;
2528 int gpio;
2529 int rc;
2530
2531 /*
2532 * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
2533 * persistence state.
2534 */
2535 if (transitory)
2536 set_bit(FLAG_TRANSITORY, &desc->flags);
2537 else
2538 clear_bit(FLAG_TRANSITORY, &desc->flags);
2539
2540 /* If the driver supports it, set the persistence state now */
2541 chip = desc->gdev->chip;
2542 if (!chip->set_config)
2543 return 0;
2544
2545 packed = pinconf_to_config_packed(PIN_CONFIG_PERSIST_STATE,
2546 !transitory);
2547 gpio = gpio_chip_hwgpio(desc);
2548 rc = chip->set_config(chip, gpio, packed);
2549 if (rc == -ENOTSUPP) {
2550 dev_dbg(&desc->gdev->dev, "Persistence not supported for GPIO %d\n",
2551 gpio);
2552 return 0;
2553 }
2554
2555 return rc;
2556}
2557EXPORT_SYMBOL_GPL(gpiod_set_transitory);
2558
2559/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002560 * gpiod_is_active_low - test whether a GPIO is active-low or not
2561 * @desc: the gpio descriptor to test
2562 *
2563 * Returns 1 if the GPIO is active-low, 0 otherwise.
2564 */
2565int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002566{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002567 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002568 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002569}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002570EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08002571
2572/* I/O calls are only valid after configuration completed; the relevant
2573 * "is this a valid GPIO" error checks should already have been done.
2574 *
2575 * "Get" operations are often inlinable as reading a pin value register,
2576 * and masking the relevant bit in that register.
2577 *
2578 * When "set" operations are inlinable, they involve writing that mask to
2579 * one register to set a low value, or a different register to set it high.
2580 * Otherwise locking is needed, so there may be little value to inlining.
2581 *
2582 *------------------------------------------------------------------------
2583 *
2584 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2585 * have requested the GPIO. That can include implicit requesting by
2586 * a direction setting call. Marking a gpio as requested locks its chip
2587 * in memory, guaranteeing that these table lookups need no more locking
2588 * and that gpiochip_remove() will fail.
2589 *
2590 * REVISIT when debugging, consider adding some instrumentation to ensure
2591 * that the GPIO was actually requested.
2592 */
2593
Linus Walleijfac9d882017-09-26 20:58:28 +02002594static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002595{
2596 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002597 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002598 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002599
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002600 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002601 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002602 value = chip->get ? chip->get(chip, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01002603 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002604 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002605 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002606}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002607
Lukas Wunnereec1d562017-10-12 12:40:10 +02002608static int gpio_chip_get_multiple(struct gpio_chip *chip,
2609 unsigned long *mask, unsigned long *bits)
2610{
2611 if (chip->get_multiple) {
2612 return chip->get_multiple(chip, mask, bits);
2613 } else if (chip->get) {
2614 int i, value;
2615
2616 for_each_set_bit(i, mask, chip->ngpio) {
2617 value = chip->get(chip, i);
2618 if (value < 0)
2619 return value;
2620 __assign_bit(i, bits, value);
2621 }
2622 return 0;
2623 }
2624 return -EIO;
2625}
2626
2627int gpiod_get_array_value_complex(bool raw, bool can_sleep,
2628 unsigned int array_size,
2629 struct gpio_desc **desc_array,
2630 int *value_array)
2631{
2632 int i = 0;
2633
2634 while (i < array_size) {
2635 struct gpio_chip *chip = desc_array[i]->gdev->chip;
2636 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2637 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2638 int first, j, ret;
2639
2640 if (!can_sleep)
2641 WARN_ON(chip->can_sleep);
2642
2643 /* collect all inputs belonging to the same chip */
2644 first = i;
2645 memset(mask, 0, sizeof(mask));
2646 do {
2647 const struct gpio_desc *desc = desc_array[i];
2648 int hwgpio = gpio_chip_hwgpio(desc);
2649
2650 __set_bit(hwgpio, mask);
2651 i++;
2652 } while ((i < array_size) &&
2653 (desc_array[i]->gdev->chip == chip));
2654
2655 ret = gpio_chip_get_multiple(chip, mask, bits);
2656 if (ret)
2657 return ret;
2658
2659 for (j = first; j < i; j++) {
2660 const struct gpio_desc *desc = desc_array[j];
2661 int hwgpio = gpio_chip_hwgpio(desc);
2662 int value = test_bit(hwgpio, bits);
2663
2664 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2665 value = !value;
2666 value_array[j] = value;
2667 trace_gpio_value(desc_to_gpio(desc), 1, value);
2668 }
2669 }
2670 return 0;
2671}
2672
David Brownelld2876d02008-02-04 22:28:20 -08002673/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002674 * gpiod_get_raw_value() - return a gpio's raw value
2675 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08002676 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002677 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002678 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002679 *
2680 * This function should be called from contexts where we cannot sleep, and will
2681 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002682 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002683int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002684{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002685 VALIDATE_DESC(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002686 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002687 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02002688 return gpiod_get_raw_value_commit(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002689}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002690EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002691
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002692/**
2693 * gpiod_get_value() - return a gpio's value
2694 * @desc: gpio whose value will be returned
2695 *
2696 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002697 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002698 *
2699 * This function should be called from contexts where we cannot sleep, and will
2700 * complain if the GPIO chip functions potentially sleep.
2701 */
2702int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002703{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002704 int value;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002705
2706 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002707 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002708 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002709
Linus Walleijfac9d882017-09-26 20:58:28 +02002710 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002711 if (value < 0)
2712 return value;
2713
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002714 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2715 value = !value;
2716
2717 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002718}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002719EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08002720
Lukas Wunnereec1d562017-10-12 12:40:10 +02002721/**
2722 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
2723 * @array_size: number of elements in the descriptor / value arrays
2724 * @desc_array: array of GPIO descriptors whose values will be read
2725 * @value_array: array to store the read values
2726 *
2727 * Read the raw values of the GPIOs, i.e. the values of the physical lines
2728 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
2729 * else an error code.
2730 *
2731 * This function should be called from contexts where we cannot sleep,
2732 * and it will complain if the GPIO chip functions potentially sleep.
2733 */
2734int gpiod_get_raw_array_value(unsigned int array_size,
2735 struct gpio_desc **desc_array, int *value_array)
2736{
2737 if (!desc_array)
2738 return -EINVAL;
2739 return gpiod_get_array_value_complex(true, false, array_size,
2740 desc_array, value_array);
2741}
2742EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
2743
2744/**
2745 * gpiod_get_array_value() - read values from an array of GPIOs
2746 * @array_size: number of elements in the descriptor / value arrays
2747 * @desc_array: array of GPIO descriptors whose values will be read
2748 * @value_array: array to store the read values
2749 *
2750 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2751 * into account. Return 0 in case of success, else an error code.
2752 *
2753 * This function should be called from contexts where we cannot sleep,
2754 * and it will complain if the GPIO chip functions potentially sleep.
2755 */
2756int gpiod_get_array_value(unsigned int array_size,
2757 struct gpio_desc **desc_array, int *value_array)
2758{
2759 if (!desc_array)
2760 return -EINVAL;
2761 return gpiod_get_array_value_complex(false, false, array_size,
2762 desc_array, value_array);
2763}
2764EXPORT_SYMBOL_GPL(gpiod_get_array_value);
2765
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302766/*
Linus Walleijfac9d882017-09-26 20:58:28 +02002767 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002768 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002769 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302770 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002771static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302772{
2773 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002774 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002775 int offset = gpio_chip_hwgpio(desc);
2776
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302777 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002778 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302779 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002780 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302781 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002782 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302783 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002784 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302785 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002786 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302787 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002788 gpiod_err(desc,
2789 "%s: Error in set_value for open drain err %d\n",
2790 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302791}
2792
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302793/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002794 * _gpio_set_open_source_value() - Set the open source gpio's value.
2795 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002796 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302797 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002798static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302799{
2800 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002801 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002802 int offset = gpio_chip_hwgpio(desc);
2803
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302804 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002805 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302806 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002807 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302808 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002809 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302810 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002811 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302812 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002813 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302814 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002815 gpiod_err(desc,
2816 "%s: Error in set_value for open source err %d\n",
2817 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302818}
2819
Linus Walleijfac9d882017-09-26 20:58:28 +02002820static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08002821{
2822 struct gpio_chip *chip;
2823
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002824 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002825 trace_gpio_value(desc_to_gpio(desc), 0, value);
Linus Walleij02e47982017-09-26 21:20:23 +02002826 chip->set(chip, gpio_chip_hwgpio(desc), value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002827}
2828
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002829/*
2830 * set multiple outputs on the same chip;
2831 * use the chip's set_multiple function if available;
2832 * otherwise set the outputs sequentially;
2833 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
2834 * defines which outputs are to be changed
2835 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
2836 * defines the values the outputs specified by mask are to be set to
2837 */
2838static void gpio_chip_set_multiple(struct gpio_chip *chip,
2839 unsigned long *mask, unsigned long *bits)
2840{
2841 if (chip->set_multiple) {
2842 chip->set_multiple(chip, mask, bits);
2843 } else {
Andy Shevchenko5e4e6fb2017-01-03 19:01:17 +02002844 unsigned int i;
2845
2846 /* set outputs if the corresponding mask bit is set */
2847 for_each_set_bit(i, mask, chip->ngpio)
2848 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002849 }
2850}
2851
Linus Walleij44c72882016-04-24 11:36:59 +02002852void gpiod_set_array_value_complex(bool raw, bool can_sleep,
2853 unsigned int array_size,
2854 struct gpio_desc **desc_array,
2855 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002856{
2857 int i = 0;
2858
2859 while (i < array_size) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002860 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002861 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2862 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2863 int count = 0;
2864
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002865 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002866 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002867
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002868 memset(mask, 0, sizeof(mask));
2869 do {
2870 struct gpio_desc *desc = desc_array[i];
2871 int hwgpio = gpio_chip_hwgpio(desc);
2872 int value = value_array[i];
2873
2874 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2875 value = !value;
2876 trace_gpio_value(desc_to_gpio(desc), 0, value);
2877 /*
2878 * collect all normal outputs belonging to the same chip
2879 * open drain and open source outputs are set individually
2880 */
Linus Walleij02e47982017-09-26 21:20:23 +02002881 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02002882 gpio_set_open_drain_value_commit(desc, value);
Linus Walleij02e47982017-09-26 21:20:23 +02002883 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02002884 gpio_set_open_source_value_commit(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002885 } else {
2886 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002887 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002888 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002889 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002890 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002891 count++;
2892 }
2893 i++;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002894 } while ((i < array_size) &&
2895 (desc_array[i]->gdev->chip == chip));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002896 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002897 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002898 gpio_chip_set_multiple(chip, mask, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002899 }
2900}
2901
David Brownelld2876d02008-02-04 22:28:20 -08002902/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002903 * gpiod_set_raw_value() - assign a gpio's raw value
2904 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08002905 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002906 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002907 * Set the raw value of the GPIO, i.e. the value of its physical line without
2908 * regard for its ACTIVE_LOW status.
2909 *
2910 * This function should be called from contexts where we cannot sleep, and will
2911 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002912 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002913void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002914{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002915 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01002916 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002917 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02002918 gpiod_set_raw_value_commit(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002919}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002920EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002921
2922/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002923 * gpiod_set_value() - assign a gpio's value
2924 * @desc: gpio whose value will be assigned
2925 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002926 *
Linus Walleij02e47982017-09-26 21:20:23 +02002927 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
2928 * OPEN_DRAIN and OPEN_SOURCE flags into account.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002929 *
2930 * This function should be called from contexts where we cannot sleep, and will
2931 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002932 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002933void gpiod_set_value(struct gpio_desc *desc, int value)
2934{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002935 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01002936 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002937 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002938 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2939 value = !value;
Linus Walleij02e47982017-09-26 21:20:23 +02002940 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2941 gpio_set_open_drain_value_commit(desc, value);
2942 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2943 gpio_set_open_source_value_commit(desc, value);
2944 else
2945 gpiod_set_raw_value_commit(desc, value);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002946}
2947EXPORT_SYMBOL_GPL(gpiod_set_value);
2948
2949/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002950 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002951 * @array_size: number of elements in the descriptor / value arrays
2952 * @desc_array: array of GPIO descriptors whose values will be assigned
2953 * @value_array: array of values to assign
2954 *
2955 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2956 * without regard for their ACTIVE_LOW status.
2957 *
2958 * This function should be called from contexts where we cannot sleep, and will
2959 * complain if the GPIO chip functions potentially sleep.
2960 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002961void gpiod_set_raw_array_value(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002962 struct gpio_desc **desc_array, int *value_array)
2963{
2964 if (!desc_array)
2965 return;
Linus Walleij44c72882016-04-24 11:36:59 +02002966 gpiod_set_array_value_complex(true, false, array_size, desc_array,
2967 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002968}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002969EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002970
2971/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002972 * gpiod_set_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002973 * @array_size: number of elements in the descriptor / value arrays
2974 * @desc_array: array of GPIO descriptors whose values will be assigned
2975 * @value_array: array of values to assign
2976 *
2977 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2978 * into account.
2979 *
2980 * This function should be called from contexts where we cannot sleep, and will
2981 * complain if the GPIO chip functions potentially sleep.
2982 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002983void gpiod_set_array_value(unsigned int array_size,
2984 struct gpio_desc **desc_array, int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002985{
2986 if (!desc_array)
2987 return;
Linus Walleij44c72882016-04-24 11:36:59 +02002988 gpiod_set_array_value_complex(false, false, array_size, desc_array,
2989 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002990}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002991EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002992
2993/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002994 * gpiod_cansleep() - report whether gpio value access may sleep
2995 * @desc: gpio to check
2996 *
2997 */
2998int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002999{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003000 VALIDATE_DESC(desc);
3001 return desc->gdev->chip->can_sleep;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003002}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003003EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08003004
David Brownell0f6d5042008-10-15 22:03:14 -07003005/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003006 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
3007 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07003008 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003009 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
3010 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07003011 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003012int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07003013{
Linus Walleij4c37ce82016-05-02 13:13:10 +02003014 struct gpio_chip *chip;
3015 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07003016
Linus Walleij79bb71b2016-06-15 22:57:38 +02003017 /*
3018 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
3019 * requires this function to not return zero on an invalid descriptor
3020 * but rather a negative error number.
3021 */
Linus Walleijbfbbe442016-06-16 11:55:55 +02003022 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
Linus Walleij79bb71b2016-06-15 22:57:38 +02003023 return -EINVAL;
3024
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003025 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003026 offset = gpio_chip_hwgpio(desc);
Linus Walleij4c37ce82016-05-02 13:13:10 +02003027 if (chip->to_irq) {
3028 int retirq = chip->to_irq(chip, offset);
3029
3030 /* Zero means NO_IRQ */
3031 if (!retirq)
3032 return -ENXIO;
3033
3034 return retirq;
3035 }
3036 return -ENXIO;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003037}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003038EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003039
Linus Walleijd468bf92013-09-24 11:54:38 +02003040/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003041 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003042 * @chip: the chip the GPIO to lock belongs to
3043 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02003044 *
3045 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08003046 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08003047 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003048int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08003049{
Linus Walleij9c102802016-05-25 10:56:03 +02003050 struct gpio_desc *desc;
Linus Walleijd468bf92013-09-24 11:54:38 +02003051
Linus Walleij9c102802016-05-25 10:56:03 +02003052 desc = gpiochip_get_desc(chip, offset);
3053 if (IS_ERR(desc))
3054 return PTR_ERR(desc);
3055
Linus Walleij60f83392016-11-12 15:01:09 +01003056 /*
3057 * If it's fast: flush the direction setting if something changed
3058 * behind our back
3059 */
3060 if (!chip->can_sleep && chip->get_direction) {
Linus Walleij9c102802016-05-25 10:56:03 +02003061 int dir = chip->get_direction(chip, offset);
3062
3063 if (dir)
3064 clear_bit(FLAG_IS_OUT, &desc->flags);
3065 else
3066 set_bit(FLAG_IS_OUT, &desc->flags);
3067 }
3068
3069 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003070 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02003071 "%s: tried to flag a GPIO set as output for IRQ\n",
3072 __func__);
3073 return -EIO;
3074 }
3075
Linus Walleij9c102802016-05-25 10:56:03 +02003076 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
Linus Walleij3940c342016-11-14 00:09:07 +01003077
3078 /*
3079 * If the consumer has not set up a label (such as when the
3080 * IRQ is referenced from .to_irq()) we set up a label here
3081 * so it is clear this is used as an interrupt.
3082 */
3083 if (!desc->label)
3084 desc_set_label(desc, "interrupt");
3085
Linus Walleijd468bf92013-09-24 11:54:38 +02003086 return 0;
3087}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003088EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02003089
Linus Walleijd468bf92013-09-24 11:54:38 +02003090/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003091 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003092 * @chip: the chip the GPIO to lock belongs to
3093 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02003094 *
3095 * This is used directly by GPIO drivers that want to indicate
3096 * that a certain GPIO is no longer used exclusively for IRQ.
3097 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003098void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02003099{
Linus Walleij3940c342016-11-14 00:09:07 +01003100 struct gpio_desc *desc;
3101
3102 desc = gpiochip_get_desc(chip, offset);
3103 if (IS_ERR(desc))
Linus Walleijd468bf92013-09-24 11:54:38 +02003104 return;
3105
Linus Walleij3940c342016-11-14 00:09:07 +01003106 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
3107
3108 /* If we only had this marking, erase it */
3109 if (desc->label && !strcmp(desc->label, "interrupt"))
3110 desc_set_label(desc, NULL);
Linus Walleijd468bf92013-09-24 11:54:38 +02003111}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003112EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02003113
Linus Walleij6cee3822016-02-11 20:16:45 +01003114bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
3115{
3116 if (offset >= chip->ngpio)
3117 return false;
3118
3119 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
3120}
3121EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
3122
Linus Walleij143b65d2016-02-16 15:41:42 +01003123bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
3124{
3125 if (offset >= chip->ngpio)
3126 return false;
3127
3128 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
3129}
3130EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
3131
3132bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
3133{
3134 if (offset >= chip->ngpio)
3135 return false;
3136
3137 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
3138}
3139EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
3140
Charles Keepax05f479b2017-05-23 15:47:29 +01003141bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset)
3142{
3143 if (offset >= chip->ngpio)
3144 return false;
3145
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303146 return !test_bit(FLAG_TRANSITORY, &chip->gpiodev->descs[offset].flags);
Charles Keepax05f479b2017-05-23 15:47:29 +01003147}
3148EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
3149
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003150/**
3151 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
3152 * @desc: gpio whose value will be returned
3153 *
3154 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003155 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003156 *
3157 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003158 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003159int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08003160{
David Brownelld2876d02008-02-04 22:28:20 -08003161 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003162 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003163 return gpiod_get_raw_value_commit(desc);
David Brownelld2876d02008-02-04 22:28:20 -08003164}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003165EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003166
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003167/**
3168 * gpiod_get_value_cansleep() - return a gpio's value
3169 * @desc: gpio whose value will be returned
3170 *
3171 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003172 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003173 *
3174 * This function is to be called from contexts that can sleep.
3175 */
3176int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003177{
David Brownelld2876d02008-02-04 22:28:20 -08003178 int value;
David Brownelld2876d02008-02-04 22:28:20 -08003179
3180 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003181 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003182 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003183 if (value < 0)
3184 return value;
3185
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003186 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3187 value = !value;
3188
David Brownelld2876d02008-02-04 22:28:20 -08003189 return value;
3190}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003191EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003192
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003193/**
Lukas Wunnereec1d562017-10-12 12:40:10 +02003194 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
3195 * @array_size: number of elements in the descriptor / value arrays
3196 * @desc_array: array of GPIO descriptors whose values will be read
3197 * @value_array: array to store the read values
3198 *
3199 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3200 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3201 * else an error code.
3202 *
3203 * This function is to be called from contexts that can sleep.
3204 */
3205int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
3206 struct gpio_desc **desc_array,
3207 int *value_array)
3208{
3209 might_sleep_if(extra_checks);
3210 if (!desc_array)
3211 return -EINVAL;
3212 return gpiod_get_array_value_complex(true, true, array_size,
3213 desc_array, value_array);
3214}
3215EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
3216
3217/**
3218 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
3219 * @array_size: number of elements in the descriptor / value arrays
3220 * @desc_array: array of GPIO descriptors whose values will be read
3221 * @value_array: array to store the read values
3222 *
3223 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3224 * into account. Return 0 in case of success, else an error code.
3225 *
3226 * This function is to be called from contexts that can sleep.
3227 */
3228int gpiod_get_array_value_cansleep(unsigned int array_size,
3229 struct gpio_desc **desc_array,
3230 int *value_array)
3231{
3232 might_sleep_if(extra_checks);
3233 if (!desc_array)
3234 return -EINVAL;
3235 return gpiod_get_array_value_complex(false, true, array_size,
3236 desc_array, value_array);
3237}
3238EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
3239
3240/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003241 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
3242 * @desc: gpio whose value will be assigned
3243 * @value: value to assign
3244 *
3245 * Set the raw value of the GPIO, i.e. the value of its physical line without
3246 * regard for its ACTIVE_LOW status.
3247 *
3248 * This function is to be called from contexts that can sleep.
3249 */
3250void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003251{
David Brownelld2876d02008-02-04 22:28:20 -08003252 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003253 VALIDATE_DESC_VOID(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003254 gpiod_set_raw_value_commit(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003255}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003256EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003257
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003258/**
3259 * gpiod_set_value_cansleep() - assign a gpio's value
3260 * @desc: gpio whose value will be assigned
3261 * @value: value to assign
3262 *
3263 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3264 * account
3265 *
3266 * This function is to be called from contexts that can sleep.
3267 */
3268void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003269{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003270 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003271 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003272 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3273 value = !value;
Linus Walleijfac9d882017-09-26 20:58:28 +02003274 gpiod_set_raw_value_commit(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08003275}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003276EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08003277
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003278/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003279 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003280 * @array_size: number of elements in the descriptor / value arrays
3281 * @desc_array: array of GPIO descriptors whose values will be assigned
3282 * @value_array: array of values to assign
3283 *
3284 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3285 * without regard for their ACTIVE_LOW status.
3286 *
3287 * This function is to be called from contexts that can sleep.
3288 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003289void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
3290 struct gpio_desc **desc_array,
3291 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003292{
3293 might_sleep_if(extra_checks);
3294 if (!desc_array)
3295 return;
Linus Walleij44c72882016-04-24 11:36:59 +02003296 gpiod_set_array_value_complex(true, true, array_size, desc_array,
3297 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003298}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003299EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003300
3301/**
Dmitry Torokhov3946d182017-08-14 21:59:55 -07003302 * gpiod_add_lookup_tables() - register GPIO device consumers
3303 * @tables: list of tables of consumers to register
3304 * @n: number of tables in the list
3305 */
3306void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
3307{
3308 unsigned int i;
3309
3310 mutex_lock(&gpio_lookup_lock);
3311
3312 for (i = 0; i < n; i++)
3313 list_add_tail(&tables[i]->list, &gpio_lookup_list);
3314
3315 mutex_unlock(&gpio_lookup_lock);
3316}
3317
3318/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003319 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003320 * @array_size: number of elements in the descriptor / value arrays
3321 * @desc_array: array of GPIO descriptors whose values will be assigned
3322 * @value_array: array of values to assign
3323 *
3324 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3325 * into account.
3326 *
3327 * This function is to be called from contexts that can sleep.
3328 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003329void gpiod_set_array_value_cansleep(unsigned int array_size,
3330 struct gpio_desc **desc_array,
3331 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003332{
3333 might_sleep_if(extra_checks);
3334 if (!desc_array)
3335 return;
Linus Walleij44c72882016-04-24 11:36:59 +02003336 gpiod_set_array_value_complex(false, true, array_size, desc_array,
3337 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003338}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003339EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003340
3341/**
Alexandre Courbotad824782013-12-03 12:20:11 +09003342 * gpiod_add_lookup_table() - register GPIO device consumers
3343 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003344 */
Alexandre Courbotad824782013-12-03 12:20:11 +09003345void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003346{
3347 mutex_lock(&gpio_lookup_lock);
3348
Alexandre Courbotad824782013-12-03 12:20:11 +09003349 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003350
3351 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08003352}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02003353EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
David Brownelld2876d02008-02-04 22:28:20 -08003354
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05303355/**
3356 * gpiod_remove_lookup_table() - unregister GPIO device consumers
3357 * @table: table of consumers to unregister
3358 */
3359void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
3360{
3361 mutex_lock(&gpio_lookup_lock);
3362
3363 list_del(&table->list);
3364
3365 mutex_unlock(&gpio_lookup_lock);
3366}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02003367EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05303368
Alexandre Courbotad824782013-12-03 12:20:11 +09003369static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
3370{
3371 const char *dev_id = dev ? dev_name(dev) : NULL;
3372 struct gpiod_lookup_table *table;
3373
3374 mutex_lock(&gpio_lookup_lock);
3375
3376 list_for_each_entry(table, &gpio_lookup_list, list) {
3377 if (table->dev_id && dev_id) {
3378 /*
3379 * Valid strings on both ends, must be identical to have
3380 * a match
3381 */
3382 if (!strcmp(table->dev_id, dev_id))
3383 goto found;
3384 } else {
3385 /*
3386 * One of the pointers is NULL, so both must be to have
3387 * a match
3388 */
3389 if (dev_id == table->dev_id)
3390 goto found;
3391 }
3392 }
3393 table = NULL;
3394
3395found:
3396 mutex_unlock(&gpio_lookup_lock);
3397 return table;
3398}
3399
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003400static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09003401 unsigned int idx,
3402 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003403{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003404 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09003405 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003406 struct gpiod_lookup *p;
3407
Alexandre Courbotad824782013-12-03 12:20:11 +09003408 table = gpiod_find_lookup_table(dev);
3409 if (!table)
3410 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003411
Alexandre Courbotad824782013-12-03 12:20:11 +09003412 for (p = &table->table[0]; p->chip_label; p++) {
3413 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003414
Alexandre Courbotad824782013-12-03 12:20:11 +09003415 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003416 if (p->idx != idx)
3417 continue;
3418
Alexandre Courbotad824782013-12-03 12:20:11 +09003419 /* If the lookup entry has a con_id, require exact match */
3420 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
3421 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003422
Alexandre Courbotad824782013-12-03 12:20:11 +09003423 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003424
Alexandre Courbotad824782013-12-03 12:20:11 +09003425 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003426 dev_err(dev, "cannot find GPIO chip %s\n",
3427 p->chip_label);
3428 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003429 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003430
Alexandre Courbotad824782013-12-03 12:20:11 +09003431 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003432 dev_err(dev,
3433 "requested GPIO %d is out of range [0..%d] for chip %s\n",
3434 idx, chip->ngpio, chip->label);
3435 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09003436 }
3437
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09003438 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09003439 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003440
3441 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09003442 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003443
3444 return desc;
3445}
3446
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003447static int dt_gpio_count(struct device *dev, const char *con_id)
3448{
3449 int ret;
3450 char propname[32];
3451 unsigned int i;
3452
3453 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
3454 if (con_id)
3455 snprintf(propname, sizeof(propname), "%s-%s",
3456 con_id, gpio_suffixes[i]);
3457 else
3458 snprintf(propname, sizeof(propname), "%s",
3459 gpio_suffixes[i]);
3460
3461 ret = of_gpio_named_count(dev->of_node, propname);
Andy Shevchenko4033d4a2017-02-20 18:15:47 +02003462 if (ret > 0)
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003463 break;
3464 }
Andy Shevchenko4033d4a2017-02-20 18:15:47 +02003465 return ret ? ret : -ENOENT;
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003466}
3467
3468static int platform_gpio_count(struct device *dev, const char *con_id)
3469{
3470 struct gpiod_lookup_table *table;
3471 struct gpiod_lookup *p;
3472 unsigned int count = 0;
3473
3474 table = gpiod_find_lookup_table(dev);
3475 if (!table)
3476 return -ENOENT;
3477
3478 for (p = &table->table[0]; p->chip_label; p++) {
3479 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3480 (!con_id && !p->con_id))
3481 count++;
3482 }
3483 if (!count)
3484 return -ENOENT;
3485
3486 return count;
3487}
3488
3489/**
3490 * gpiod_count - return the number of GPIOs associated with a device / function
3491 * or -ENOENT if no GPIO has been assigned to the requested function
3492 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3493 * @con_id: function within the GPIO consumer
3494 */
3495int gpiod_count(struct device *dev, const char *con_id)
3496{
3497 int count = -ENOENT;
3498
3499 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
3500 count = dt_gpio_count(dev, con_id);
3501 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
3502 count = acpi_gpio_count(dev, con_id);
3503
3504 if (count < 0)
3505 count = platform_gpio_count(dev, con_id);
3506
3507 return count;
3508}
3509EXPORT_SYMBOL_GPL(gpiod_count);
3510
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003511/**
Thierry Reding08791622014-04-25 16:54:22 +02003512 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09003513 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003514 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003515 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003516 *
3517 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003518 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07003519 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003520 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003521struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003522 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003523{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003524 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003525}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003526EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003527
3528/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02003529 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
3530 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3531 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003532 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02003533 *
3534 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
3535 * the requested function it will return NULL. This is convenient for drivers
3536 * that need to handle optional GPIOs.
3537 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003538struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003539 const char *con_id,
3540 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003541{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003542 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003543}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003544EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003545
Benoit Parrotf625d462015-02-02 11:44:44 -06003546
3547/**
3548 * gpiod_configure_flags - helper function to configure a given GPIO
3549 * @desc: gpio whose value will be assigned
3550 * @con_id: function within the GPIO consumer
Johan Hovold85b03b32016-07-03 18:32:05 +02003551 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3552 * of_get_gpio_hog()
Benoit Parrotf625d462015-02-02 11:44:44 -06003553 * @dflags: gpiod_flags - optional GPIO initialization flags
3554 *
3555 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
3556 * requested function and/or index, or another IS_ERR() code if an error
3557 * occurred while trying to acquire the GPIO.
3558 */
Andy Shevchenkoc29fd9e2017-05-23 20:03:16 +03003559int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Johan Hovold85b03b32016-07-03 18:32:05 +02003560 unsigned long lflags, enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06003561{
3562 int status;
3563
Johan Hovold85b03b32016-07-03 18:32:05 +02003564 if (lflags & GPIO_ACTIVE_LOW)
3565 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
Linus Walleijf926dfc2017-09-10 19:26:22 +02003566
Johan Hovold85b03b32016-07-03 18:32:05 +02003567 if (lflags & GPIO_OPEN_DRAIN)
3568 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
Linus Walleijf926dfc2017-09-10 19:26:22 +02003569 else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) {
3570 /*
3571 * This enforces open drain mode from the consumer side.
3572 * This is necessary for some busses like I2C, but the lookup
3573 * should *REALLY* have specified them as open drain in the
3574 * first place, so print a little warning here.
3575 */
3576 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3577 gpiod_warn(desc,
3578 "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
3579 }
3580
Johan Hovold85b03b32016-07-03 18:32:05 +02003581 if (lflags & GPIO_OPEN_SOURCE)
3582 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303583
3584 status = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
3585 if (status < 0)
3586 return status;
Johan Hovold85b03b32016-07-03 18:32:05 +02003587
Benoit Parrotf625d462015-02-02 11:44:44 -06003588 /* No particular flag request, return here... */
3589 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
3590 pr_debug("no flags found for %s\n", con_id);
3591 return 0;
3592 }
3593
3594 /* Process flags */
3595 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
3596 status = gpiod_direction_output(desc,
Linus Walleijad177312016-11-13 23:02:44 +01003597 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
Benoit Parrotf625d462015-02-02 11:44:44 -06003598 else
3599 status = gpiod_direction_input(desc);
3600
3601 return status;
3602}
3603
Thierry Reding29a1f2332014-04-25 17:10:06 +02003604/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003605 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02003606 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003607 * @con_id: function within the GPIO consumer
3608 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003609 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003610 *
3611 * This variant of gpiod_get() allows to access GPIOs other than the first
3612 * defined one for functions that define several GPIOs.
3613 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003614 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
3615 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07003616 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003617 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003618struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003619 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003620 unsigned int idx,
3621 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003622{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003623 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003624 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003625 enum gpio_lookup_flags lookupflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003626
3627 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
3628
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003629 if (dev) {
3630 /* Using device tree? */
3631 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
3632 dev_dbg(dev, "using device tree for GPIO lookup\n");
3633 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
3634 } else if (ACPI_COMPANION(dev)) {
3635 dev_dbg(dev, "using ACPI for GPIO lookup\n");
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +03003636 desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags);
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003637 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003638 }
3639
3640 /*
3641 * Either we are not using DT or ACPI, or their lookup did not return
3642 * a result. In that case, use platform lookup as a fallback.
3643 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003644 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04003645 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003646 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003647 }
3648
3649 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02003650 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003651 return desc;
3652 }
3653
3654 status = gpiod_request(desc, con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003655 if (status < 0)
3656 return ERR_PTR(status);
3657
Johan Hovold85b03b32016-07-03 18:32:05 +02003658 status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003659 if (status < 0) {
3660 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
3661 gpiod_put(desc);
3662 return ERR_PTR(status);
3663 }
3664
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003665 return desc;
3666}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003667EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003668
3669/**
Mika Westerberg40b73182014-10-21 13:33:59 +02003670 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
3671 * @fwnode: handle of the firmware node
3672 * @propname: name of the firmware property representing the GPIO
Boris Brezillon537b94d2017-02-02 14:53:11 +01003673 * @index: index of the GPIO to obtain in the consumer
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003674 * @dflags: GPIO initialization flags
Thierry Reding950d55f52017-07-24 16:57:22 +02003675 * @label: label to attach to the requested GPIO
Mika Westerberg40b73182014-10-21 13:33:59 +02003676 *
3677 * This function can be used for drivers that get their configuration
3678 * from firmware.
3679 *
3680 * Function properly finds the corresponding GPIO using whatever is the
3681 * underlying firmware interface and then makes sure that the GPIO
3682 * descriptor is requested before it is returned to the caller.
3683 *
Thierry Reding950d55f52017-07-24 16:57:22 +02003684 * Returns:
Andy Shevchenkoff213782017-02-28 17:03:12 +02003685 * On successful request the GPIO pin is configured in accordance with
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003686 * provided @dflags.
3687 *
Mika Westerberg40b73182014-10-21 13:33:59 +02003688 * In case of error an ERR_PTR() is returned.
3689 */
3690struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
Boris Brezillon537b94d2017-02-02 14:53:11 +01003691 const char *propname, int index,
Alexander Steinb2987d72017-01-12 17:39:24 +01003692 enum gpiod_flags dflags,
3693 const char *label)
Mika Westerberg40b73182014-10-21 13:33:59 +02003694{
3695 struct gpio_desc *desc = ERR_PTR(-ENODEV);
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003696 unsigned long lflags = 0;
Mika Westerberg40b73182014-10-21 13:33:59 +02003697 bool active_low = false;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003698 bool single_ended = false;
Laxman Dewangan4c0facd2017-04-06 19:05:52 +05303699 bool open_drain = false;
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303700 bool transitory = false;
Mika Westerberg40b73182014-10-21 13:33:59 +02003701 int ret;
3702
3703 if (!fwnode)
3704 return ERR_PTR(-EINVAL);
3705
3706 if (is_of_node(fwnode)) {
3707 enum of_gpio_flags flags;
3708
Boris Brezillon537b94d2017-02-02 14:53:11 +01003709 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname,
3710 index, &flags);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003711 if (!IS_ERR(desc)) {
Mika Westerberg40b73182014-10-21 13:33:59 +02003712 active_low = flags & OF_GPIO_ACTIVE_LOW;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003713 single_ended = flags & OF_GPIO_SINGLE_ENDED;
Laxman Dewangan4c0facd2017-04-06 19:05:52 +05303714 open_drain = flags & OF_GPIO_OPEN_DRAIN;
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303715 transitory = flags & OF_GPIO_TRANSITORY;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003716 }
Mika Westerberg40b73182014-10-21 13:33:59 +02003717 } else if (is_acpi_node(fwnode)) {
3718 struct acpi_gpio_info info;
3719
Boris Brezillon537b94d2017-02-02 14:53:11 +01003720 desc = acpi_node_get_gpiod(fwnode, propname, index, &info);
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +03003721 if (!IS_ERR(desc)) {
Christophe RICARD52044722015-12-23 23:25:34 +01003722 active_low = info.polarity == GPIO_ACTIVE_LOW;
Andy Shevchenko5c34b6c2017-11-10 15:40:31 +02003723 acpi_gpio_update_gpiod_flags(&dflags, &info);
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +03003724 }
Mika Westerberg40b73182014-10-21 13:33:59 +02003725 }
3726
3727 if (IS_ERR(desc))
3728 return desc;
3729
Alexander Steinb2987d72017-01-12 17:39:24 +01003730 ret = gpiod_request(desc, label);
Johan Hovold85b03b32016-07-03 18:32:05 +02003731 if (ret)
3732 return ERR_PTR(ret);
3733
Mika Westerberg40b73182014-10-21 13:33:59 +02003734 if (active_low)
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003735 lflags |= GPIO_ACTIVE_LOW;
Mika Westerberg40b73182014-10-21 13:33:59 +02003736
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003737 if (single_ended) {
Laxman Dewangan4c0facd2017-04-06 19:05:52 +05303738 if (open_drain)
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003739 lflags |= GPIO_OPEN_DRAIN;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003740 else
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003741 lflags |= GPIO_OPEN_SOURCE;
3742 }
3743
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303744 if (transitory)
3745 lflags |= GPIO_TRANSITORY;
3746
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003747 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
3748 if (ret < 0) {
3749 gpiod_put(desc);
3750 return ERR_PTR(ret);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003751 }
3752
Mika Westerberg40b73182014-10-21 13:33:59 +02003753 return desc;
3754}
3755EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
3756
3757/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02003758 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
3759 * function
3760 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3761 * @con_id: function within the GPIO consumer
3762 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003763 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02003764 *
3765 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
3766 * specified index was assigned to the requested function it will return NULL.
3767 * This is convenient for drivers that need to handle optional GPIOs.
3768 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003769struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02003770 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003771 unsigned int index,
3772 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003773{
3774 struct gpio_desc *desc;
3775
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003776 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003777 if (IS_ERR(desc)) {
3778 if (PTR_ERR(desc) == -ENOENT)
3779 return NULL;
3780 }
3781
3782 return desc;
3783}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003784EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003785
3786/**
Benoit Parrotf625d462015-02-02 11:44:44 -06003787 * gpiod_hog - Hog the specified GPIO desc given the provided flags
3788 * @desc: gpio whose value will be assigned
3789 * @name: gpio line name
3790 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3791 * of_get_gpio_hog()
3792 * @dflags: gpiod_flags - optional GPIO initialization flags
3793 */
3794int gpiod_hog(struct gpio_desc *desc, const char *name,
3795 unsigned long lflags, enum gpiod_flags dflags)
3796{
3797 struct gpio_chip *chip;
3798 struct gpio_desc *local_desc;
3799 int hwnum;
3800 int status;
3801
3802 chip = gpiod_to_chip(desc);
3803 hwnum = gpio_chip_hwgpio(desc);
3804
3805 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
3806 if (IS_ERR(local_desc)) {
Laxman Dewanganc31a5712016-03-11 19:13:21 +05303807 status = PTR_ERR(local_desc);
3808 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
3809 name, chip->label, hwnum, status);
3810 return status;
Benoit Parrotf625d462015-02-02 11:44:44 -06003811 }
3812
Johan Hovold85b03b32016-07-03 18:32:05 +02003813 status = gpiod_configure_flags(desc, name, lflags, dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -06003814 if (status < 0) {
Laxman Dewanganc31a5712016-03-11 19:13:21 +05303815 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed, %d\n",
3816 name, chip->label, hwnum, status);
Benoit Parrotf625d462015-02-02 11:44:44 -06003817 gpiochip_free_own_desc(desc);
3818 return status;
3819 }
3820
3821 /* Mark GPIO as hogged so it can be identified and removed later */
3822 set_bit(FLAG_IS_HOGGED, &desc->flags);
3823
3824 pr_info("GPIO line %d (%s) hogged as %s%s\n",
3825 desc_to_gpio(desc), name,
3826 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
3827 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
3828 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
3829
3830 return 0;
3831}
3832
3833/**
3834 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
3835 * @chip: gpio chip to act on
3836 *
3837 * This is only used by of_gpiochip_remove to free hogged gpios
3838 */
3839static void gpiochip_free_hogs(struct gpio_chip *chip)
3840{
3841 int id;
3842
3843 for (id = 0; id < chip->ngpio; id++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01003844 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
3845 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
Benoit Parrotf625d462015-02-02 11:44:44 -06003846 }
3847}
3848
3849/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003850 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
3851 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3852 * @con_id: function within the GPIO consumer
3853 * @flags: optional GPIO initialization flags
3854 *
3855 * This function acquires all the GPIOs defined under a given function.
3856 *
3857 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
3858 * no GPIO has been assigned to the requested function, or another IS_ERR()
3859 * code if an error occurred while trying to acquire the GPIOs.
3860 */
3861struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
3862 const char *con_id,
3863 enum gpiod_flags flags)
3864{
3865 struct gpio_desc *desc;
3866 struct gpio_descs *descs;
3867 int count;
3868
3869 count = gpiod_count(dev, con_id);
3870 if (count < 0)
3871 return ERR_PTR(count);
3872
3873 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
3874 GFP_KERNEL);
3875 if (!descs)
3876 return ERR_PTR(-ENOMEM);
3877
3878 for (descs->ndescs = 0; descs->ndescs < count; ) {
3879 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
3880 if (IS_ERR(desc)) {
3881 gpiod_put_array(descs);
3882 return ERR_CAST(desc);
3883 }
3884 descs->desc[descs->ndescs] = desc;
3885 descs->ndescs++;
3886 }
3887 return descs;
3888}
3889EXPORT_SYMBOL_GPL(gpiod_get_array);
3890
3891/**
3892 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
3893 * function
3894 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3895 * @con_id: function within the GPIO consumer
3896 * @flags: optional GPIO initialization flags
3897 *
3898 * This is equivalent to gpiod_get_array(), except that when no GPIO was
3899 * assigned to the requested function it will return NULL.
3900 */
3901struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
3902 const char *con_id,
3903 enum gpiod_flags flags)
3904{
3905 struct gpio_descs *descs;
3906
3907 descs = gpiod_get_array(dev, con_id, flags);
3908 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
3909 return NULL;
3910
3911 return descs;
3912}
3913EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
3914
3915/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003916 * gpiod_put - dispose of a GPIO descriptor
3917 * @desc: GPIO descriptor to dispose of
3918 *
3919 * No descriptor can be used after gpiod_put() has been called on it.
3920 */
3921void gpiod_put(struct gpio_desc *desc)
3922{
3923 gpiod_free(desc);
3924}
3925EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08003926
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003927/**
3928 * gpiod_put_array - dispose of multiple GPIO descriptors
3929 * @descs: struct gpio_descs containing an array of descriptors
3930 */
3931void gpiod_put_array(struct gpio_descs *descs)
3932{
3933 unsigned int i;
3934
3935 for (i = 0; i < descs->ndescs; i++)
3936 gpiod_put(descs->desc[i]);
3937
3938 kfree(descs);
3939}
3940EXPORT_SYMBOL_GPL(gpiod_put_array);
3941
Linus Walleij3c702e92015-10-21 15:29:53 +02003942static int __init gpiolib_dev_init(void)
3943{
3944 int ret;
3945
3946 /* Register GPIO sysfs bus */
3947 ret = bus_register(&gpio_bus_type);
3948 if (ret < 0) {
3949 pr_err("gpiolib: could not register GPIO bus type\n");
3950 return ret;
3951 }
3952
3953 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
3954 if (ret < 0) {
3955 pr_err("gpiolib: failed to allocate char dev region\n");
3956 bus_unregister(&gpio_bus_type);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07003957 } else {
3958 gpiolib_initialized = true;
3959 gpiochip_setup_devs();
Linus Walleij3c702e92015-10-21 15:29:53 +02003960 }
3961 return ret;
3962}
3963core_initcall(gpiolib_dev_init);
3964
David Brownelld2876d02008-02-04 22:28:20 -08003965#ifdef CONFIG_DEBUG_FS
3966
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003967static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
David Brownelld2876d02008-02-04 22:28:20 -08003968{
3969 unsigned i;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003970 struct gpio_chip *chip = gdev->chip;
3971 unsigned gpio = gdev->base;
3972 struct gpio_desc *gdesc = &gdev->descs[0];
David Brownelld2876d02008-02-04 22:28:20 -08003973 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02003974 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08003975
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003976 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02003977 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
3978 if (gdesc->name) {
3979 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
3980 gpio, gdesc->name);
3981 }
David Brownelld2876d02008-02-04 22:28:20 -08003982 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02003983 }
David Brownelld2876d02008-02-04 22:28:20 -08003984
Alexandre Courbot372e7222013-02-03 01:29:29 +09003985 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08003986 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02003987 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Markus Pargmannced433e2015-08-14 16:11:02 +02003988 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
3989 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08003990 is_out ? "out" : "in ",
3991 chip->get
3992 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02003993 : "? ",
3994 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08003995 seq_printf(s, "\n");
3996 }
3997}
3998
Thierry Redingf9c4a312012-04-12 13:26:01 +02003999static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08004000{
Grant Likely362432a2013-02-09 09:41:49 +00004001 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02004002 struct gpio_device *gdev = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09004003 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02004004
4005 s->private = "";
4006
Grant Likely362432a2013-02-09 09:41:49 +00004007 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02004008 list_for_each_entry(gdev, &gpio_devices, list)
Grant Likely362432a2013-02-09 09:41:49 +00004009 if (index-- == 0) {
4010 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02004011 return gdev;
Grant Likely362432a2013-02-09 09:41:49 +00004012 }
4013 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09004014
4015 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02004016}
4017
4018static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
4019{
Grant Likely362432a2013-02-09 09:41:49 +00004020 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02004021 struct gpio_device *gdev = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02004022 void *ret = NULL;
4023
Grant Likely362432a2013-02-09 09:41:49 +00004024 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02004025 if (list_is_last(&gdev->list, &gpio_devices))
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09004026 ret = NULL;
4027 else
Linus Walleijff2b1352015-10-20 11:10:38 +02004028 ret = list_entry(gdev->list.next, struct gpio_device, list);
Grant Likely362432a2013-02-09 09:41:49 +00004029 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02004030
4031 s->private = "\n";
4032 ++*pos;
4033
4034 return ret;
4035}
4036
4037static void gpiolib_seq_stop(struct seq_file *s, void *v)
4038{
4039}
4040
4041static int gpiolib_seq_show(struct seq_file *s, void *v)
4042{
Linus Walleijff2b1352015-10-20 11:10:38 +02004043 struct gpio_device *gdev = v;
4044 struct gpio_chip *chip = gdev->chip;
4045 struct device *parent;
Thierry Redingf9c4a312012-04-12 13:26:01 +02004046
Linus Walleijff2b1352015-10-20 11:10:38 +02004047 if (!chip) {
4048 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
4049 dev_name(&gdev->dev));
4050 return 0;
4051 }
4052
4053 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
4054 dev_name(&gdev->dev),
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004055 gdev->base, gdev->base + gdev->ngpio - 1);
Linus Walleijff2b1352015-10-20 11:10:38 +02004056 parent = chip->parent;
4057 if (parent)
4058 seq_printf(s, ", parent: %s/%s",
4059 parent->bus ? parent->bus->name : "no-bus",
4060 dev_name(parent));
Thierry Redingf9c4a312012-04-12 13:26:01 +02004061 if (chip->label)
4062 seq_printf(s, ", %s", chip->label);
4063 if (chip->can_sleep)
4064 seq_printf(s, ", can sleep");
4065 seq_printf(s, ":\n");
4066
4067 if (chip->dbg_show)
4068 chip->dbg_show(s, chip);
4069 else
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004070 gpiolib_dbg_show(s, gdev);
Thierry Redingf9c4a312012-04-12 13:26:01 +02004071
David Brownelld2876d02008-02-04 22:28:20 -08004072 return 0;
4073}
4074
Thierry Redingf9c4a312012-04-12 13:26:01 +02004075static const struct seq_operations gpiolib_seq_ops = {
4076 .start = gpiolib_seq_start,
4077 .next = gpiolib_seq_next,
4078 .stop = gpiolib_seq_stop,
4079 .show = gpiolib_seq_show,
4080};
4081
David Brownelld2876d02008-02-04 22:28:20 -08004082static int gpiolib_open(struct inode *inode, struct file *file)
4083{
Thierry Redingf9c4a312012-04-12 13:26:01 +02004084 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08004085}
4086
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004087static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02004088 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08004089 .open = gpiolib_open,
4090 .read = seq_read,
4091 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02004092 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08004093};
4094
4095static int __init gpiolib_debugfs_init(void)
4096{
4097 /* /sys/kernel/debug/gpio */
4098 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
4099 NULL, NULL, &gpiolib_operations);
4100 return 0;
4101}
4102subsys_initcall(gpiolib_debugfs_init);
4103
4104#endif /* DEBUG_FS */