blob: 56eec094184cd86d80baa91c1ae9118988718fea [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 Walleijf881bab2015-09-23 16:20:43 -0700277 * Convert a GPIO name to its descriptor
278 */
279static struct gpio_desc *gpio_name_to_desc(const char * const name)
280{
Linus Walleijff2b1352015-10-20 11:10:38 +0200281 struct gpio_device *gdev;
Linus Walleijf881bab2015-09-23 16:20:43 -0700282 unsigned long flags;
283
284 spin_lock_irqsave(&gpio_lock, flags);
285
Linus Walleijff2b1352015-10-20 11:10:38 +0200286 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijf881bab2015-09-23 16:20:43 -0700287 int i;
288
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100289 for (i = 0; i != gdev->ngpio; ++i) {
290 struct gpio_desc *desc = &gdev->descs[i];
Linus Walleijf881bab2015-09-23 16:20:43 -0700291
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100292 if (!desc->name || !name)
Linus Walleijf881bab2015-09-23 16:20:43 -0700293 continue;
294
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100295 if (!strcmp(desc->name, name)) {
Linus Walleijf881bab2015-09-23 16:20:43 -0700296 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100297 return desc;
Linus Walleijf881bab2015-09-23 16:20:43 -0700298 }
299 }
300 }
301
302 spin_unlock_irqrestore(&gpio_lock, flags);
303
304 return NULL;
305}
306
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200307/*
308 * Takes the names from gc->names and checks if they are all unique. If they
309 * are, they are assigned to their gpio descriptors.
310 *
Bamvor Jian Zhanged379152015-11-14 16:43:20 +0800311 * Warning if one of the names is already used for a different GPIO.
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200312 */
313static int gpiochip_set_desc_names(struct gpio_chip *gc)
314{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100315 struct gpio_device *gdev = gc->gpiodev;
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200316 int i;
317
318 if (!gc->names)
319 return 0;
320
321 /* First check all names if they are unique */
322 for (i = 0; i != gc->ngpio; ++i) {
323 struct gpio_desc *gpio;
324
325 gpio = gpio_name_to_desc(gc->names[i]);
Linus Walleijf881bab2015-09-23 16:20:43 -0700326 if (gpio)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100327 dev_warn(&gdev->dev,
Linus Walleij34ffd852015-10-20 11:31:54 +0200328 "Detected name collision for GPIO name '%s'\n",
Linus Walleijf881bab2015-09-23 16:20:43 -0700329 gc->names[i]);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200330 }
331
332 /* Then add all names to the GPIO descriptors */
333 for (i = 0; i != gc->ngpio; ++i)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100334 gdev->descs[i].name = gc->names[i];
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200335
336 return 0;
337}
338
Linus Walleijd7c51b42016-04-26 10:35:29 +0200339/*
340 * GPIO line handle management
341 */
342
343/**
344 * struct linehandle_state - contains the state of a userspace handle
345 * @gdev: the GPIO device the handle pertains to
346 * @label: consumer label used to tag descriptors
347 * @descs: the GPIO descriptors held by this handle
348 * @numdescs: the number of descriptors held in the descs array
349 */
350struct linehandle_state {
351 struct gpio_device *gdev;
352 const char *label;
353 struct gpio_desc *descs[GPIOHANDLES_MAX];
354 u32 numdescs;
355};
356
Lars-Peter Clausene3e847c2016-10-18 16:54:05 +0200357#define GPIOHANDLE_REQUEST_VALID_FLAGS \
358 (GPIOHANDLE_REQUEST_INPUT | \
359 GPIOHANDLE_REQUEST_OUTPUT | \
360 GPIOHANDLE_REQUEST_ACTIVE_LOW | \
361 GPIOHANDLE_REQUEST_OPEN_DRAIN | \
362 GPIOHANDLE_REQUEST_OPEN_SOURCE)
363
Linus Walleijd7c51b42016-04-26 10:35:29 +0200364static long linehandle_ioctl(struct file *filep, unsigned int cmd,
365 unsigned long arg)
366{
367 struct linehandle_state *lh = filep->private_data;
368 void __user *ip = (void __user *)arg;
369 struct gpiohandle_data ghd;
Lukas Wunnereec1d562017-10-12 12:40:10 +0200370 int vals[GPIOHANDLES_MAX];
Linus Walleijd7c51b42016-04-26 10:35:29 +0200371 int i;
372
373 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
Lukas Wunnereec1d562017-10-12 12:40:10 +0200374 /* TODO: check if descriptors are really input */
375 int ret = gpiod_get_array_value_complex(false,
376 true,
377 lh->numdescs,
378 lh->descs,
379 vals);
380 if (ret)
381 return ret;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200382
Lars-Peter Clausen3eded5d2016-10-18 16:54:02 +0200383 memset(&ghd, 0, sizeof(ghd));
Lukas Wunnereec1d562017-10-12 12:40:10 +0200384 for (i = 0; i < lh->numdescs; i++)
385 ghd.values[i] = vals[i];
Linus Walleijd7c51b42016-04-26 10:35:29 +0200386
387 if (copy_to_user(ip, &ghd, sizeof(ghd)))
388 return -EFAULT;
389
390 return 0;
391 } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
Linus Walleijd7c51b42016-04-26 10:35:29 +0200392 /* TODO: check if descriptors are really output */
393 if (copy_from_user(&ghd, ip, sizeof(ghd)))
394 return -EFAULT;
395
396 /* Clamp all values to [0,1] */
397 for (i = 0; i < lh->numdescs; i++)
398 vals[i] = !!ghd.values[i];
399
400 /* Reuse the array setting function */
401 gpiod_set_array_value_complex(false,
402 true,
403 lh->numdescs,
404 lh->descs,
405 vals);
406 return 0;
407 }
408 return -EINVAL;
409}
410
411#ifdef CONFIG_COMPAT
412static long linehandle_ioctl_compat(struct file *filep, unsigned int cmd,
413 unsigned long arg)
414{
415 return linehandle_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
416}
417#endif
418
419static int linehandle_release(struct inode *inode, struct file *filep)
420{
421 struct linehandle_state *lh = filep->private_data;
422 struct gpio_device *gdev = lh->gdev;
423 int i;
424
425 for (i = 0; i < lh->numdescs; i++)
426 gpiod_free(lh->descs[i]);
427 kfree(lh->label);
428 kfree(lh);
429 put_device(&gdev->dev);
430 return 0;
431}
432
433static const struct file_operations linehandle_fileops = {
434 .release = linehandle_release,
435 .owner = THIS_MODULE,
436 .llseek = noop_llseek,
437 .unlocked_ioctl = linehandle_ioctl,
438#ifdef CONFIG_COMPAT
439 .compat_ioctl = linehandle_ioctl_compat,
440#endif
441};
442
443static int linehandle_create(struct gpio_device *gdev, void __user *ip)
444{
445 struct gpiohandle_request handlereq;
446 struct linehandle_state *lh;
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200447 struct file *file;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200448 int fd, i, ret;
Bartosz Golaszewski418ee8e2017-10-16 11:32:29 +0200449 u32 lflags;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200450
451 if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
452 return -EFAULT;
453 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
454 return -EINVAL;
455
Bartosz Golaszewski418ee8e2017-10-16 11:32:29 +0200456 lflags = handlereq.flags;
457
458 /* Return an error if an unknown flag is set */
459 if (lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
460 return -EINVAL;
461
Bartosz Golaszewski588fc3b2017-11-15 16:47:43 +0100462 /*
463 * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
464 * the hardware actually supports enabling both at the same time the
465 * electrical result would be disastrous.
466 */
467 if ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) &&
468 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
469 return -EINVAL;
470
Bartosz Golaszewski609aaf62017-10-16 11:32:30 +0200471 /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
472 if (!(lflags & GPIOHANDLE_REQUEST_OUTPUT) &&
473 ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
474 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
475 return -EINVAL;
476
Linus Walleijd7c51b42016-04-26 10:35:29 +0200477 lh = kzalloc(sizeof(*lh), GFP_KERNEL);
478 if (!lh)
479 return -ENOMEM;
480 lh->gdev = gdev;
481 get_device(&gdev->dev);
482
483 /* Make sure this is terminated */
484 handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
485 if (strlen(handlereq.consumer_label)) {
486 lh->label = kstrdup(handlereq.consumer_label,
487 GFP_KERNEL);
488 if (!lh->label) {
489 ret = -ENOMEM;
490 goto out_free_lh;
491 }
492 }
493
494 /* Request each GPIO */
495 for (i = 0; i < handlereq.lines; i++) {
496 u32 offset = handlereq.lineoffsets[i];
Linus Walleijd7c51b42016-04-26 10:35:29 +0200497 struct gpio_desc *desc;
498
Lars-Peter Clausene405f9f2016-10-18 16:54:01 +0200499 if (offset >= gdev->ngpio) {
500 ret = -EINVAL;
501 goto out_free_descs;
502 }
503
Linus Walleijd7c51b42016-04-26 10:35:29 +0200504 desc = &gdev->descs[offset];
505 ret = gpiod_request(desc, lh->label);
506 if (ret)
507 goto out_free_descs;
508 lh->descs[i] = desc;
509
510 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
511 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
512 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
513 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
514 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
515 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
516
Andrew Jefferye10f72b2017-11-30 14:25:24 +1030517 ret = gpiod_set_transitory(desc, false);
518 if (ret < 0)
519 goto out_free_descs;
520
Linus Walleijd7c51b42016-04-26 10:35:29 +0200521 /*
522 * Lines have to be requested explicitly for input
523 * or output, else the line will be treated "as is".
524 */
525 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
526 int val = !!handlereq.default_values[i];
527
528 ret = gpiod_direction_output(desc, val);
529 if (ret)
530 goto out_free_descs;
531 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
532 ret = gpiod_direction_input(desc);
533 if (ret)
534 goto out_free_descs;
535 }
536 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
537 offset);
538 }
Linus Walleije2f608b2016-06-18 10:56:43 +0200539 /* Let i point at the last handle */
540 i--;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200541 lh->numdescs = handlereq.lines;
542
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200543 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200544 if (fd < 0) {
545 ret = fd;
546 goto out_free_descs;
547 }
548
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200549 file = anon_inode_getfile("gpio-linehandle",
550 &linehandle_fileops,
551 lh,
552 O_RDONLY | O_CLOEXEC);
553 if (IS_ERR(file)) {
554 ret = PTR_ERR(file);
555 goto out_put_unused_fd;
556 }
557
Linus Walleijd7c51b42016-04-26 10:35:29 +0200558 handlereq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200559 if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200560 /*
561 * fput() will trigger the release() callback, so do not go onto
562 * the regular error cleanup path here.
563 */
564 fput(file);
565 put_unused_fd(fd);
566 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +0200567 }
Linus Walleijd7c51b42016-04-26 10:35:29 +0200568
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200569 fd_install(fd, file);
570
Linus Walleijd7c51b42016-04-26 10:35:29 +0200571 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
572 lh->numdescs);
573
574 return 0;
575
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200576out_put_unused_fd:
577 put_unused_fd(fd);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200578out_free_descs:
579 for (; i >= 0; i--)
580 gpiod_free(lh->descs[i]);
581 kfree(lh->label);
582out_free_lh:
583 kfree(lh);
584 put_device(&gdev->dev);
585 return ret;
586}
587
Linus Walleij61f922d2016-06-02 11:30:15 +0200588/*
589 * GPIO line event management
590 */
591
592/**
593 * struct lineevent_state - contains the state of a userspace event
594 * @gdev: the GPIO device the event pertains to
595 * @label: consumer label used to tag descriptors
596 * @desc: the GPIO descriptor held by this event
597 * @eflags: the event flags this line was requested with
598 * @irq: the interrupt that trigger in response to events on this GPIO
599 * @wait: wait queue that handles blocking reads of events
600 * @events: KFIFO for the GPIO events
601 * @read_lock: mutex lock to protect reads from colliding with adding
602 * new events to the FIFO
603 */
604struct lineevent_state {
605 struct gpio_device *gdev;
606 const char *label;
607 struct gpio_desc *desc;
608 u32 eflags;
609 int irq;
610 wait_queue_head_t wait;
611 DECLARE_KFIFO(events, struct gpioevent_data, 16);
612 struct mutex read_lock;
613};
614
Lars-Peter Clausenac7dbb92016-10-18 16:54:06 +0200615#define GPIOEVENT_REQUEST_VALID_FLAGS \
616 (GPIOEVENT_REQUEST_RISING_EDGE | \
617 GPIOEVENT_REQUEST_FALLING_EDGE)
618
Linus Walleij61f922d2016-06-02 11:30:15 +0200619static unsigned int lineevent_poll(struct file *filep,
620 struct poll_table_struct *wait)
621{
622 struct lineevent_state *le = filep->private_data;
623 unsigned int events = 0;
624
625 poll_wait(filep, &le->wait, wait);
626
627 if (!kfifo_is_empty(&le->events))
628 events = POLLIN | POLLRDNORM;
629
630 return events;
631}
632
633
634static ssize_t lineevent_read(struct file *filep,
635 char __user *buf,
636 size_t count,
637 loff_t *f_ps)
638{
639 struct lineevent_state *le = filep->private_data;
640 unsigned int copied;
641 int ret;
642
643 if (count < sizeof(struct gpioevent_data))
644 return -EINVAL;
645
646 do {
647 if (kfifo_is_empty(&le->events)) {
648 if (filep->f_flags & O_NONBLOCK)
649 return -EAGAIN;
650
651 ret = wait_event_interruptible(le->wait,
652 !kfifo_is_empty(&le->events));
653 if (ret)
654 return ret;
655 }
656
657 if (mutex_lock_interruptible(&le->read_lock))
658 return -ERESTARTSYS;
659 ret = kfifo_to_user(&le->events, buf, count, &copied);
660 mutex_unlock(&le->read_lock);
661
662 if (ret)
663 return ret;
664
665 /*
666 * If we couldn't read anything from the fifo (a different
667 * thread might have been faster) we either return -EAGAIN if
668 * the file descriptor is non-blocking, otherwise we go back to
669 * sleep and wait for more data to arrive.
670 */
671 if (copied == 0 && (filep->f_flags & O_NONBLOCK))
672 return -EAGAIN;
673
674 } while (copied == 0);
675
676 return copied;
677}
678
679static int lineevent_release(struct inode *inode, struct file *filep)
680{
681 struct lineevent_state *le = filep->private_data;
682 struct gpio_device *gdev = le->gdev;
683
684 free_irq(le->irq, le);
685 gpiod_free(le->desc);
686 kfree(le->label);
687 kfree(le);
688 put_device(&gdev->dev);
689 return 0;
690}
691
692static long lineevent_ioctl(struct file *filep, unsigned int cmd,
693 unsigned long arg)
694{
695 struct lineevent_state *le = filep->private_data;
696 void __user *ip = (void __user *)arg;
697 struct gpiohandle_data ghd;
698
699 /*
700 * We can get the value for an event line but not set it,
701 * because it is input by definition.
702 */
703 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
704 int val;
705
Lars-Peter Clausend82aa4a2016-10-18 16:54:04 +0200706 memset(&ghd, 0, sizeof(ghd));
707
Linus Walleij61f922d2016-06-02 11:30:15 +0200708 val = gpiod_get_value_cansleep(le->desc);
709 if (val < 0)
710 return val;
711 ghd.values[0] = val;
712
713 if (copy_to_user(ip, &ghd, sizeof(ghd)))
714 return -EFAULT;
715
716 return 0;
717 }
718 return -EINVAL;
719}
720
721#ifdef CONFIG_COMPAT
722static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd,
723 unsigned long arg)
724{
725 return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
726}
727#endif
728
729static const struct file_operations lineevent_fileops = {
730 .release = lineevent_release,
731 .read = lineevent_read,
732 .poll = lineevent_poll,
733 .owner = THIS_MODULE,
734 .llseek = noop_llseek,
735 .unlocked_ioctl = lineevent_ioctl,
736#ifdef CONFIG_COMPAT
737 .compat_ioctl = lineevent_ioctl_compat,
738#endif
739};
740
Ben Dooks33265b12016-06-17 16:03:13 +0100741static irqreturn_t lineevent_irq_thread(int irq, void *p)
Linus Walleij61f922d2016-06-02 11:30:15 +0200742{
743 struct lineevent_state *le = p;
744 struct gpioevent_data ge;
Bartosz Golaszewskidf1e76f2017-07-03 11:12:03 +0200745 int ret, level;
Linus Walleij61f922d2016-06-02 11:30:15 +0200746
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);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001065 kfree(gdev->label);
1066 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
1173 if (chip->label)
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001174 gdev->label = kstrdup(chip->label, GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001175 else
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001176 gdev->label = kstrdup("unknown", GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001177 if (!gdev->label) {
1178 status = -ENOMEM;
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001179 goto err_free_descs;
Linus Walleijdf4878e2016-02-12 14:48:23 +01001180 }
1181
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001182 gdev->ngpio = chip->ngpio;
Linus Walleij43c54ec2016-02-11 11:37:48 +01001183 gdev->data = data;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001184
David Brownelld2876d02008-02-04 22:28:20 -08001185 spin_lock_irqsave(&gpio_lock, flags);
1186
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001187 /*
1188 * TODO: this allocates a Linux GPIO number base in the global
1189 * GPIO numberspace for this chip. In the long run we want to
1190 * get *rid* of this numberspace and use only descriptors, but
1191 * it may be a pipe dream. It will not happen before we get rid
1192 * of the sysfs interface anyways.
1193 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001194 if (base < 0) {
1195 base = gpiochip_find_base(chip->ngpio);
1196 if (base < 0) {
1197 status = base;
Johan Hovold225fce82015-01-12 17:12:25 +01001198 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001199 goto err_free_label;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001200 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001201 /*
1202 * TODO: it should not be necessary to reflect the assigned
1203 * base outside of the GPIO subsystem. Go over drivers and
1204 * see if anyone makes use of this, else drop this and assign
1205 * a poison instead.
1206 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001207 chip->base = base;
1208 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001209 gdev->base = base;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001210
Linus Walleijff2b1352015-10-20 11:10:38 +02001211 status = gpiodev_add_to_list(gdev);
Johan Hovold05aa5202015-01-12 17:12:26 +01001212 if (status) {
1213 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001214 goto err_free_label;
Johan Hovold05aa5202015-01-12 17:12:26 +01001215 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001216
Linus Walleij545ebd92016-05-30 17:11:59 +02001217 spin_unlock_irqrestore(&gpio_lock, flags);
1218
Linus Walleijff2b1352015-10-20 11:10:38 +02001219 for (i = 0; i < chip->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001220 struct gpio_desc *desc = &gdev->descs[i];
David Brownelld8f388d82008-07-25 01:46:07 -07001221
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001222 desc->gdev = gdev;
Linus Walleij72d32002016-04-28 13:33:59 +02001223 /*
1224 * REVISIT: most hardware initializes GPIOs as inputs
1225 * (often with pullups enabled) so power usage is
1226 * minimized. Linux code should set the gpio direction
1227 * first thing; but until it does, and in case
1228 * chip->get_direction is not set, we may expose the
1229 * wrong direction in sysfs.
Johan Hovold05aa5202015-01-12 17:12:26 +01001230 */
Linus Walleij72d32002016-04-28 13:33:59 +02001231
1232 if (chip->get_direction) {
1233 /*
1234 * If we have .get_direction, set up the initial
1235 * direction flag from the hardware.
1236 */
1237 int dir = chip->get_direction(chip, i);
1238
1239 if (!dir)
1240 set_bit(FLAG_IS_OUT, &desc->flags);
1241 } else if (!chip->direction_input) {
1242 /*
1243 * If the chip lacks the .direction_input callback
1244 * we logically assume all lines are outputs.
1245 */
1246 set_bit(FLAG_IS_OUT, &desc->flags);
1247 }
David Brownelld2876d02008-02-04 22:28:20 -08001248 }
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001249
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301250#ifdef CONFIG_PINCTRL
Linus Walleij20ec3e32016-02-11 11:03:06 +01001251 INIT_LIST_HEAD(&gdev->pin_ranges);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301252#endif
1253
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001254 status = gpiochip_set_desc_names(chip);
1255 if (status)
1256 goto err_remove_from_list;
1257
Mika Westerberg79b804c2016-09-20 15:15:21 +03001258 status = gpiochip_irqchip_init_valid_mask(chip);
1259 if (status)
1260 goto err_remove_from_list;
1261
Thierry Reding959bc7b2017-11-07 19:15:59 +01001262 status = gpiochip_add_irqchip(chip, key);
Thierry Redinge0d89722017-11-07 19:15:54 +01001263 if (status)
1264 goto err_remove_chip;
1265
Tomeu Vizoso28355f82015-07-14 10:29:54 +02001266 status = of_gpiochip_add(chip);
1267 if (status)
1268 goto err_remove_chip;
1269
Mika Westerberg664e3e52014-01-08 12:40:54 +02001270 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001271
Linus Walleij3c702e92015-10-21 15:29:53 +02001272 /*
1273 * By first adding the chardev, and then adding the device,
1274 * we get a device node entry in sysfs under
1275 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1276 * coldplug of device nodes and other udev business.
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001277 * We can do this only if gpiolib has been initialized.
1278 * Otherwise, defer until later.
Linus Walleij3c702e92015-10-21 15:29:53 +02001279 */
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001280 if (gpiolib_initialized) {
1281 status = gpiochip_setup_dev(gdev);
1282 if (status)
1283 goto err_remove_chip;
1284 }
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001285 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001286
Johan Hovold225fce82015-01-12 17:12:25 +01001287err_remove_chip:
1288 acpi_gpiochip_remove(chip);
Johan Hovold6d867502015-05-04 17:23:25 +02001289 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +01001290 of_gpiochip_remove(chip);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001291 gpiochip_irqchip_free_valid_mask(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001292err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +01001293 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001294 list_del(&gdev->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001295 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001296err_free_label:
1297 kfree(gdev->label);
1298err_free_descs:
1299 kfree(gdev->descs);
Linus Walleijff2b1352015-10-20 11:10:38 +02001300err_free_gdev:
1301 ida_simple_remove(&gpio_ida, gdev->id);
David Brownelld2876d02008-02-04 22:28:20 -08001302 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +02001303 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001304 gdev->base, gdev->base + gdev->ngpio - 1,
1305 chip->label ? : "generic");
1306 kfree(gdev);
David Brownelld2876d02008-02-04 22:28:20 -08001307 return status;
1308}
Thierry Reding959bc7b2017-11-07 19:15:59 +01001309EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);
David Brownelld2876d02008-02-04 22:28:20 -08001310
1311/**
Linus Walleij43c54ec2016-02-11 11:37:48 +01001312 * gpiochip_get_data() - get per-subdriver data for the chip
Thierry Reding950d55f52017-07-24 16:57:22 +02001313 * @chip: GPIO chip
1314 *
1315 * Returns:
1316 * The per-subdriver data for the chip.
Linus Walleij43c54ec2016-02-11 11:37:48 +01001317 */
1318void *gpiochip_get_data(struct gpio_chip *chip)
1319{
1320 return chip->gpiodev->data;
1321}
1322EXPORT_SYMBOL_GPL(gpiochip_get_data);
1323
1324/**
David Brownelld2876d02008-02-04 22:28:20 -08001325 * gpiochip_remove() - unregister a gpio_chip
1326 * @chip: the chip to unregister
1327 *
1328 * A gpio_chip with any GPIOs still requested may not be removed.
1329 */
abdoulaye berthee1db1702014-07-05 18:28:50 +02001330void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -08001331{
Linus Walleijff2b1352015-10-20 11:10:38 +02001332 struct gpio_device *gdev = chip->gpiodev;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001333 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001334 unsigned long flags;
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001335 unsigned i;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001336 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -08001337
Linus Walleijff2b1352015-10-20 11:10:38 +02001338 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
Linus Walleijafbc4f32016-02-09 13:21:06 +01001339 gpiochip_sysfs_unregister(gdev);
Geert Uytterhoeven5018ada2016-12-19 18:29:23 +01001340 gpiochip_free_hogs(chip);
Bamvor Jian Zhangbd203bd2016-02-20 13:13:19 +08001341 /* Numb the device, cancelling all outstanding operations */
1342 gdev->chip = NULL;
Johan Hovold00acc3d2015-01-12 17:12:27 +01001343 gpiochip_irqchip_remove(chip);
Mika Westerberg6072b9d2014-03-10 14:54:53 +02001344 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +01001345 gpiochip_remove_pin_ranges(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001346 of_gpiochip_remove(chip);
Linus Walleij43c54ec2016-02-11 11:37:48 +01001347 /*
1348 * We accept no more calls into the driver from this point, so
1349 * NULL the driver data pointer
1350 */
1351 gdev->data = NULL;
Anton Vorontsov391c9702010-06-08 07:48:17 -06001352
Johan Hovold6798aca2015-01-12 17:12:28 +01001353 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001354 for (i = 0; i < gdev->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001355 desc = &gdev->descs[i];
Johan Hovoldfab28b82015-05-04 17:10:27 +02001356 if (test_bit(FLAG_REQUESTED, &desc->flags))
1357 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -08001358 }
David Brownelld2876d02008-02-04 22:28:20 -08001359 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001360
Johan Hovoldfab28b82015-05-04 17:10:27 +02001361 if (requested)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001362 dev_crit(&gdev->dev,
Linus Walleij58383c782015-11-04 09:56:26 +01001363 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +02001364
Linus Walleijff2b1352015-10-20 11:10:38 +02001365 /*
1366 * The gpiochip side puts its use of the device to rest here:
1367 * if there are no userspace clients, the chardev and device will
1368 * be removed, else it will be dangling until the last user is
1369 * gone.
1370 */
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001371 cdev_device_del(&gdev->chrdev, &gdev->dev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001372 put_device(&gdev->dev);
David Brownelld2876d02008-02-04 22:28:20 -08001373}
1374EXPORT_SYMBOL_GPL(gpiochip_remove);
1375
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301376static void devm_gpio_chip_release(struct device *dev, void *res)
1377{
1378 struct gpio_chip *chip = *(struct gpio_chip **)res;
1379
1380 gpiochip_remove(chip);
1381}
1382
1383static int devm_gpio_chip_match(struct device *dev, void *res, void *data)
1384
1385{
1386 struct gpio_chip **r = res;
1387
1388 if (!r || !*r) {
1389 WARN_ON(!r || !*r);
1390 return 0;
1391 }
1392
1393 return *r == data;
1394}
1395
1396/**
1397 * devm_gpiochip_add_data() - Resource manager piochip_add_data()
1398 * @dev: the device pointer on which irq_chip belongs to.
1399 * @chip: the chip to register, with chip->base initialized
Thierry Reding950d55f52017-07-24 16:57:22 +02001400 * @data: driver-private data associated with this chip
1401 *
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301402 * Context: potentially before irqs will work
1403 *
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301404 * The gpio chip automatically be released when the device is unbound.
Thierry Reding950d55f52017-07-24 16:57:22 +02001405 *
1406 * Returns:
1407 * A negative errno if the chip can't be registered, such as because the
1408 * chip->base is invalid or already associated with a different chip.
1409 * Otherwise it returns zero as a success code.
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301410 */
1411int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
1412 void *data)
1413{
1414 struct gpio_chip **ptr;
1415 int ret;
1416
1417 ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
1418 GFP_KERNEL);
1419 if (!ptr)
1420 return -ENOMEM;
1421
1422 ret = gpiochip_add_data(chip, data);
1423 if (ret < 0) {
1424 devres_free(ptr);
1425 return ret;
1426 }
1427
1428 *ptr = chip;
1429 devres_add(dev, ptr);
1430
1431 return 0;
1432}
1433EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
1434
1435/**
1436 * devm_gpiochip_remove() - Resource manager of gpiochip_remove()
1437 * @dev: device for which which resource was allocated
1438 * @chip: the chip to remove
1439 *
1440 * A gpio_chip with any GPIOs still requested may not be removed.
1441 */
1442void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip)
1443{
1444 int ret;
1445
1446 ret = devres_release(dev, devm_gpio_chip_release,
1447 devm_gpio_chip_match, chip);
Christophe JAILLET3988d662017-01-27 12:56:42 +01001448 WARN_ON(ret);
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301449}
1450EXPORT_SYMBOL_GPL(devm_gpiochip_remove);
1451
Grant Likely594fa262010-06-08 07:48:16 -06001452/**
1453 * gpiochip_find() - iterator for locating a specific gpio_chip
1454 * @data: data to pass to match function
Thierry Reding950d55f52017-07-24 16:57:22 +02001455 * @match: Callback function to check gpio_chip
Grant Likely594fa262010-06-08 07:48:16 -06001456 *
1457 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1458 * determined by a user supplied @match callback. The callback should return
1459 * 0 if the device doesn't match and non-zero if it does. If the callback is
1460 * non-zero, this function will return to the caller and not iterate over any
1461 * more gpio_chips.
1462 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001463struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001464 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001465 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001466{
Linus Walleijff2b1352015-10-20 11:10:38 +02001467 struct gpio_device *gdev;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001468 struct gpio_chip *chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -06001469 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001470
1471 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001472 list_for_each_entry(gdev, &gpio_devices, list)
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001473 if (gdev->chip && match(gdev->chip, data)) {
1474 chip = gdev->chip;
Grant Likely594fa262010-06-08 07:48:16 -06001475 break;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001476 }
Linus Walleijff2b1352015-10-20 11:10:38 +02001477
Grant Likely594fa262010-06-08 07:48:16 -06001478 spin_unlock_irqrestore(&gpio_lock, flags);
1479
1480 return chip;
1481}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001482EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001483
Alexandre Courbot79697ef2013-11-16 21:39:32 +09001484static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1485{
1486 const char *name = data;
1487
1488 return !strcmp(chip->label, name);
1489}
1490
1491static struct gpio_chip *find_chip_by_name(const char *name)
1492{
1493 return gpiochip_find((void *)name, gpiochip_match_name);
1494}
1495
Linus Walleij14250522014-03-25 10:40:18 +01001496#ifdef CONFIG_GPIOLIB_IRQCHIP
1497
1498/*
1499 * The following is irqchip helper code for gpiochips.
1500 */
1501
Mika Westerberg79b804c2016-09-20 15:15:21 +03001502static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1503{
Thierry Redingdc7b0382017-11-07 19:15:52 +01001504 if (!gpiochip->irq.need_valid_mask)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001505 return 0;
1506
Thierry Redingdc7b0382017-11-07 19:15:52 +01001507 gpiochip->irq.valid_mask = kcalloc(BITS_TO_LONGS(gpiochip->ngpio),
Mika Westerberg79b804c2016-09-20 15:15:21 +03001508 sizeof(long), GFP_KERNEL);
Thierry Redingdc7b0382017-11-07 19:15:52 +01001509 if (!gpiochip->irq.valid_mask)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001510 return -ENOMEM;
1511
1512 /* Assume by default all GPIOs are valid */
Thierry Redingdc7b0382017-11-07 19:15:52 +01001513 bitmap_fill(gpiochip->irq.valid_mask, gpiochip->ngpio);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001514
1515 return 0;
1516}
1517
1518static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1519{
Thierry Redingdc7b0382017-11-07 19:15:52 +01001520 kfree(gpiochip->irq.valid_mask);
1521 gpiochip->irq.valid_mask = NULL;
Mika Westerberg79b804c2016-09-20 15:15:21 +03001522}
1523
1524static bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
1525 unsigned int offset)
1526{
1527 /* No mask means all valid */
Thierry Redingdc7b0382017-11-07 19:15:52 +01001528 if (likely(!gpiochip->irq.valid_mask))
Mika Westerberg79b804c2016-09-20 15:15:21 +03001529 return true;
Thierry Redingdc7b0382017-11-07 19:15:52 +01001530 return test_bit(offset, gpiochip->irq.valid_mask);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001531}
1532
Linus Walleij14250522014-03-25 10:40:18 +01001533/**
Linus Walleijd245b3f2016-11-24 10:57:25 +01001534 * gpiochip_set_cascaded_irqchip() - connects a cascaded irqchip to a gpiochip
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001535 * @gpiochip: the gpiochip to set the irqchip chain to
1536 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01001537 * @parent_irq: the irq number corresponding to the parent IRQ for this
1538 * chained irqchip
1539 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001540 * coming out of the gpiochip. If the interrupt is nested rather than
1541 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +01001542 */
Linus Walleijd245b3f2016-11-24 10:57:25 +01001543static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gpiochip,
1544 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001545 unsigned int parent_irq,
Linus Walleijd245b3f2016-11-24 10:57:25 +01001546 irq_flow_handler_t parent_handler)
Linus Walleij14250522014-03-25 10:40:18 +01001547{
Linus Walleij83141a72014-09-26 13:50:12 +02001548 unsigned int offset;
1549
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001550 if (!gpiochip->irq.domain) {
Linus Walleij83141a72014-09-26 13:50:12 +02001551 chip_err(gpiochip, "called %s before setting up irqchip\n",
1552 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +02001553 return;
1554 }
1555
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001556 if (parent_handler) {
1557 if (gpiochip->can_sleep) {
1558 chip_err(gpiochip,
1559 "you cannot have chained interrupts on a "
1560 "chip that may sleep\n");
1561 return;
1562 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001563 /*
1564 * The parent irqchip is already using the chip_data for this
1565 * irqchip, so our callbacks simply use the handler_data.
1566 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +02001567 irq_set_chained_handler_and_data(parent_irq, parent_handler,
1568 gpiochip);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001569
Thierry Reding39e5f092017-11-07 19:15:50 +01001570 gpiochip->irq.parents = &parent_irq;
1571 gpiochip->irq.num_parents = 1;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001572 }
Linus Walleij83141a72014-09-26 13:50:12 +02001573
1574 /* Set the parent IRQ for all affected IRQs */
Mika Westerberg79b804c2016-09-20 15:15:21 +03001575 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1576 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1577 continue;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001578 irq_set_parent(irq_find_mapping(gpiochip->irq.domain, offset),
Linus Walleij83141a72014-09-26 13:50:12 +02001579 parent_irq);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001580 }
Linus Walleij14250522014-03-25 10:40:18 +01001581}
Linus Walleijd245b3f2016-11-24 10:57:25 +01001582
1583/**
1584 * gpiochip_set_chained_irqchip() - connects a chained irqchip to a gpiochip
1585 * @gpiochip: the gpiochip to set the irqchip chain to
1586 * @irqchip: the irqchip to chain to the gpiochip
1587 * @parent_irq: the irq number corresponding to the parent IRQ for this
1588 * chained irqchip
1589 * @parent_handler: the parent interrupt handler for the accumulated IRQ
1590 * coming out of the gpiochip. If the interrupt is nested rather than
1591 * cascaded, pass NULL in this handler argument
1592 */
1593void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
1594 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001595 unsigned int parent_irq,
Linus Walleijd245b3f2016-11-24 10:57:25 +01001596 irq_flow_handler_t parent_handler)
1597{
Thierry Reding60ed54c2017-11-07 19:15:57 +01001598 if (gpiochip->irq.threaded) {
1599 chip_err(gpiochip, "tried to chain a threaded gpiochip\n");
1600 return;
1601 }
1602
Linus Walleijd245b3f2016-11-24 10:57:25 +01001603 gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
1604 parent_handler);
1605}
Linus Walleij14250522014-03-25 10:40:18 +01001606EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
1607
1608/**
Linus Walleijd245b3f2016-11-24 10:57:25 +01001609 * gpiochip_set_nested_irqchip() - connects a nested irqchip to a gpiochip
1610 * @gpiochip: the gpiochip to set the irqchip nested handler to
1611 * @irqchip: the irqchip to nest to the gpiochip
1612 * @parent_irq: the irq number corresponding to the parent IRQ for this
1613 * nested irqchip
1614 */
1615void gpiochip_set_nested_irqchip(struct gpio_chip *gpiochip,
1616 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001617 unsigned int parent_irq)
Linus Walleijd245b3f2016-11-24 10:57:25 +01001618{
Linus Walleijd245b3f2016-11-24 10:57:25 +01001619 gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
1620 NULL);
1621}
1622EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip);
1623
1624/**
Linus Walleij14250522014-03-25 10:40:18 +01001625 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1626 * @d: the irqdomain used by this irqchip
1627 * @irq: the global irq number used by this GPIO irqchip irq
1628 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1629 *
1630 * This function will set up the mapping for a certain IRQ line on a
1631 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1632 * stored inside the gpiochip.
1633 */
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001634int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1635 irq_hw_number_t hwirq)
Linus Walleij14250522014-03-25 10:40:18 +01001636{
1637 struct gpio_chip *chip = d->host_data;
Thierry Redinge0d89722017-11-07 19:15:54 +01001638 int err = 0;
Linus Walleij14250522014-03-25 10:40:18 +01001639
Grygorii Strashkodc749a02017-07-21 11:49:00 -05001640 if (!gpiochip_irqchip_irq_valid(chip, hwirq))
1641 return -ENXIO;
1642
Linus Walleij14250522014-03-25 10:40:18 +01001643 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001644 /*
1645 * This lock class tells lockdep that GPIO irqs are in a different
1646 * category than their parents, so it won't report false recursion.
1647 */
Thierry Redingca9df052017-11-07 19:15:53 +01001648 irq_set_lockdep_class(irq, chip->irq.lock_key);
Thierry Redingc7a0aa52017-11-07 19:15:48 +01001649 irq_set_chip_and_handler(irq, chip->irq.chip, chip->irq.handler);
Linus Walleijd245b3f2016-11-24 10:57:25 +01001650 /* Chips that use nested thread handlers have them marked */
Thierry Reding60ed54c2017-11-07 19:15:57 +01001651 if (chip->irq.threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02001652 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +01001653 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -05001654
Thierry Redinge0d89722017-11-07 19:15:54 +01001655 if (chip->irq.num_parents == 1)
1656 err = irq_set_parent(irq, chip->irq.parents[0]);
1657 else if (chip->irq.map)
1658 err = irq_set_parent(irq, chip->irq.map[hwirq]);
1659
1660 if (err < 0)
1661 return err;
1662
Linus Walleij1333b902014-04-23 16:45:12 +02001663 /*
1664 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1665 * is passed as default type.
1666 */
Thierry Reding3634eeb2017-11-07 19:15:49 +01001667 if (chip->irq.default_type != IRQ_TYPE_NONE)
1668 irq_set_irq_type(irq, chip->irq.default_type);
Linus Walleij14250522014-03-25 10:40:18 +01001669
1670 return 0;
1671}
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001672EXPORT_SYMBOL_GPL(gpiochip_irq_map);
Linus Walleij14250522014-03-25 10:40:18 +01001673
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001674void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
Linus Walleijc3626fd2014-03-28 20:42:01 +01001675{
Linus Walleij1c8732b2014-04-09 13:34:39 +02001676 struct gpio_chip *chip = d->host_data;
1677
Thierry Reding60ed54c2017-11-07 19:15:57 +01001678 if (chip->irq.threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02001679 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001680 irq_set_chip_and_handler(irq, NULL, NULL);
1681 irq_set_chip_data(irq, NULL);
1682}
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001683EXPORT_SYMBOL_GPL(gpiochip_irq_unmap);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001684
Linus Walleij14250522014-03-25 10:40:18 +01001685static const struct irq_domain_ops gpiochip_domain_ops = {
1686 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +01001687 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +01001688 /* Virtually all GPIO irqchips are twocell:ed */
1689 .xlate = irq_domain_xlate_twocell,
1690};
1691
1692static int gpiochip_irq_reqres(struct irq_data *d)
1693{
1694 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1695
Linus Walleijff2b1352015-10-20 11:10:38 +02001696 if (!try_module_get(chip->gpiodev->owner))
Grygorii Strashko5b76e792015-06-25 20:30:50 +03001697 return -ENODEV;
1698
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001699 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +01001700 chip_err(chip,
1701 "unable to lock HW IRQ %lu for IRQ\n",
1702 d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001703 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001704 return -EINVAL;
1705 }
1706 return 0;
1707}
1708
1709static void gpiochip_irq_relres(struct irq_data *d)
1710{
1711 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1712
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001713 gpiochip_unlock_as_irq(chip, d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001714 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001715}
1716
1717static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
1718{
Grygorii Strashkodc749a02017-07-21 11:49:00 -05001719 if (!gpiochip_irqchip_irq_valid(chip, offset))
1720 return -ENXIO;
Thierry Redinge0d89722017-11-07 19:15:54 +01001721
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001722 return irq_create_mapping(chip->irq.domain, offset);
Linus Walleij14250522014-03-25 10:40:18 +01001723}
1724
1725/**
Thierry Redinge0d89722017-11-07 19:15:54 +01001726 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
1727 * @gpiochip: the GPIO chip to add the IRQ chip to
Thierry Reding959bc7b2017-11-07 19:15:59 +01001728 * @lock_key: lockdep class
Thierry Redinge0d89722017-11-07 19:15:54 +01001729 */
Thierry Reding959bc7b2017-11-07 19:15:59 +01001730static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
1731 struct lock_class_key *lock_key)
Thierry Redinge0d89722017-11-07 19:15:54 +01001732{
1733 struct irq_chip *irqchip = gpiochip->irq.chip;
1734 const struct irq_domain_ops *ops;
1735 struct device_node *np;
1736 unsigned int type;
1737 unsigned int i;
1738
1739 if (!irqchip)
1740 return 0;
1741
1742 if (gpiochip->irq.parent_handler && gpiochip->can_sleep) {
1743 chip_err(gpiochip, "you cannot have chained interrupts on a "
1744 "chip that may sleep\n");
1745 return -EINVAL;
1746 }
1747
1748 np = gpiochip->gpiodev->dev.of_node;
1749 type = gpiochip->irq.default_type;
1750
1751 /*
1752 * Specifying a default trigger is a terrible idea if DT or ACPI is
1753 * used to configure the interrupts, as you may end up with
1754 * conflicting triggers. Tell the user, and reset to NONE.
1755 */
1756 if (WARN(np && type != IRQ_TYPE_NONE,
1757 "%s: Ignoring %u default trigger\n", np->full_name, type))
1758 type = IRQ_TYPE_NONE;
1759
1760 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
1761 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1762 "Ignoring %u default trigger\n", type);
1763 type = IRQ_TYPE_NONE;
1764 }
1765
1766 gpiochip->to_irq = gpiochip_to_irq;
1767 gpiochip->irq.default_type = type;
Thierry Reding959bc7b2017-11-07 19:15:59 +01001768 gpiochip->irq.lock_key = lock_key;
Thierry Redinge0d89722017-11-07 19:15:54 +01001769
1770 if (gpiochip->irq.domain_ops)
1771 ops = gpiochip->irq.domain_ops;
1772 else
1773 ops = &gpiochip_domain_ops;
1774
1775 gpiochip->irq.domain = irq_domain_add_simple(np, gpiochip->ngpio,
Thierry Reding8302cf52017-11-07 19:15:58 +01001776 gpiochip->irq.first,
1777 ops, gpiochip);
Thierry Redinge0d89722017-11-07 19:15:54 +01001778 if (!gpiochip->irq.domain)
1779 return -EINVAL;
1780
1781 /*
1782 * It is possible for a driver to override this, but only if the
1783 * alternative functions are both implemented.
1784 */
1785 if (!irqchip->irq_request_resources &&
1786 !irqchip->irq_release_resources) {
1787 irqchip->irq_request_resources = gpiochip_irq_reqres;
1788 irqchip->irq_release_resources = gpiochip_irq_relres;
1789 }
1790
1791 if (gpiochip->irq.parent_handler) {
1792 void *data = gpiochip->irq.parent_handler_data ?: gpiochip;
1793
1794 for (i = 0; i < gpiochip->irq.num_parents; i++) {
1795 /*
1796 * The parent IRQ chip is already using the chip_data
1797 * for this IRQ chip, so our callbacks simply use the
1798 * handler_data.
1799 */
1800 irq_set_chained_handler_and_data(gpiochip->irq.parents[i],
1801 gpiochip->irq.parent_handler,
1802 data);
1803 }
Thierry Redinge0d89722017-11-07 19:15:54 +01001804 }
1805
1806 acpi_gpiochip_request_interrupts(gpiochip);
1807
1808 return 0;
1809}
1810
1811/**
Linus Walleij14250522014-03-25 10:40:18 +01001812 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1813 * @gpiochip: the gpiochip to remove the irqchip from
1814 *
1815 * This is called only from gpiochip_remove()
1816 */
1817static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
1818{
Thierry Reding39e5f092017-11-07 19:15:50 +01001819 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +01001820
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001821 acpi_gpiochip_free_interrupts(gpiochip);
1822
Thierry Redinge0d89722017-11-07 19:15:54 +01001823 if (gpiochip->irq.chip && gpiochip->irq.parent_handler) {
Thierry Reding39e5f092017-11-07 19:15:50 +01001824 struct gpio_irq_chip *irq = &gpiochip->irq;
1825 unsigned int i;
1826
1827 for (i = 0; i < irq->num_parents; i++)
1828 irq_set_chained_handler_and_data(irq->parents[i],
1829 NULL, NULL);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001830 }
1831
Linus Walleijc3626fd2014-03-28 20:42:01 +01001832 /* Remove all IRQ mappings and delete the domain */
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001833 if (gpiochip->irq.domain) {
Thierry Reding39e5f092017-11-07 19:15:50 +01001834 unsigned int irq;
1835
Mika Westerberg79b804c2016-09-20 15:15:21 +03001836 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1837 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1838 continue;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001839
1840 irq = irq_find_mapping(gpiochip->irq.domain, offset);
1841 irq_dispose_mapping(irq);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001842 }
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001843
1844 irq_domain_remove(gpiochip->irq.domain);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001845 }
Linus Walleij14250522014-03-25 10:40:18 +01001846
Thierry Redingda80ff82017-11-07 19:15:46 +01001847 if (gpiochip->irq.chip) {
1848 gpiochip->irq.chip->irq_request_resources = NULL;
1849 gpiochip->irq.chip->irq_release_resources = NULL;
1850 gpiochip->irq.chip = NULL;
Linus Walleij14250522014-03-25 10:40:18 +01001851 }
Mika Westerberg79b804c2016-09-20 15:15:21 +03001852
1853 gpiochip_irqchip_free_valid_mask(gpiochip);
Linus Walleij14250522014-03-25 10:40:18 +01001854}
1855
1856/**
Linus Walleij739e6f52017-01-11 13:37:07 +01001857 * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01001858 * @gpiochip: the gpiochip to add the irqchip to
1859 * @irqchip: the irqchip to add to the gpiochip
1860 * @first_irq: if not dynamically assigned, the base (first) IRQ to
1861 * allocate gpiochip irqs from
1862 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +02001863 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
1864 * to have the core avoid setting up any default type in the hardware.
Thierry Reding60ed54c2017-11-07 19:15:57 +01001865 * @threaded: whether this irqchip uses a nested thread handler
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001866 * @lock_key: lockdep class
Linus Walleij14250522014-03-25 10:40:18 +01001867 *
1868 * This function closely associates a certain irqchip with a certain
1869 * gpiochip, providing an irq domain to translate the local IRQs to
1870 * global irqs in the gpiolib core, and making sure that the gpiochip
1871 * is passed as chip data to all related functions. Driver callbacks
Linus Walleij09dd5f92015-12-07 15:31:58 +01001872 * need to use gpiochip_get_data() to get their local state containers back
Linus Walleij14250522014-03-25 10:40:18 +01001873 * from the gpiochip passed as chip data. An irqdomain will be stored
1874 * in the gpiochip that shall be used by the driver to handle IRQ number
1875 * translation. The gpiochip will need to be initialized and registered
1876 * before calling this function.
1877 *
Linus Walleijc3626fd2014-03-28 20:42:01 +01001878 * This function will handle two cell:ed simple IRQs and assumes all
1879 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +01001880 * need to be open coded.
1881 */
Linus Walleij739e6f52017-01-11 13:37:07 +01001882int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
1883 struct irq_chip *irqchip,
1884 unsigned int first_irq,
1885 irq_flow_handler_t handler,
1886 unsigned int type,
Thierry Reding60ed54c2017-11-07 19:15:57 +01001887 bool threaded,
Linus Walleij739e6f52017-01-11 13:37:07 +01001888 struct lock_class_key *lock_key)
Linus Walleij14250522014-03-25 10:40:18 +01001889{
1890 struct device_node *of_node;
Linus Walleij14250522014-03-25 10:40:18 +01001891
1892 if (!gpiochip || !irqchip)
1893 return -EINVAL;
1894
Linus Walleij58383c782015-11-04 09:56:26 +01001895 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +01001896 pr_err("missing gpiochip .dev parent pointer\n");
1897 return -EINVAL;
1898 }
Thierry Reding60ed54c2017-11-07 19:15:57 +01001899 gpiochip->irq.threaded = threaded;
Linus Walleij58383c782015-11-04 09:56:26 +01001900 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +01001901#ifdef CONFIG_OF_GPIO
1902 /*
Colin Cronin20a8a962015-05-18 11:41:43 -07001903 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08001904 * FIXME: get rid of this and use gpiochip->parent->of_node
1905 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +01001906 */
1907 if (gpiochip->of_node)
1908 of_node = gpiochip->of_node;
1909#endif
Marc Zyngier332e99d2016-09-07 09:12:11 +01001910 /*
Mika Westerberg0a1e0052016-09-12 14:29:51 +03001911 * Specifying a default trigger is a terrible idea if DT or ACPI is
Marc Zyngier332e99d2016-09-07 09:12:11 +01001912 * used to configure the interrupts, as you may end-up with
1913 * conflicting triggers. Tell the user, and reset to NONE.
1914 */
1915 if (WARN(of_node && type != IRQ_TYPE_NONE,
Rob Herring7eb6ce22017-07-18 16:43:03 -05001916 "%pOF: Ignoring %d default trigger\n", of_node, type))
Marc Zyngier332e99d2016-09-07 09:12:11 +01001917 type = IRQ_TYPE_NONE;
Mika Westerberg0a1e0052016-09-12 14:29:51 +03001918 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
1919 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1920 "Ignoring %d default trigger\n", type);
1921 type = IRQ_TYPE_NONE;
1922 }
Marc Zyngier332e99d2016-09-07 09:12:11 +01001923
Thierry Redingda80ff82017-11-07 19:15:46 +01001924 gpiochip->irq.chip = irqchip;
Thierry Redingc7a0aa52017-11-07 19:15:48 +01001925 gpiochip->irq.handler = handler;
Thierry Reding3634eeb2017-11-07 19:15:49 +01001926 gpiochip->irq.default_type = type;
Linus Walleij14250522014-03-25 10:40:18 +01001927 gpiochip->to_irq = gpiochip_to_irq;
Thierry Redingca9df052017-11-07 19:15:53 +01001928 gpiochip->irq.lock_key = lock_key;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001929 gpiochip->irq.domain = irq_domain_add_simple(of_node,
Linus Walleij14250522014-03-25 10:40:18 +01001930 gpiochip->ngpio, first_irq,
1931 &gpiochip_domain_ops, gpiochip);
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001932 if (!gpiochip->irq.domain) {
Thierry Redingda80ff82017-11-07 19:15:46 +01001933 gpiochip->irq.chip = NULL;
Linus Walleij14250522014-03-25 10:40:18 +01001934 return -EINVAL;
1935 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +02001936
1937 /*
1938 * It is possible for a driver to override this, but only if the
1939 * alternative functions are both implemented.
1940 */
1941 if (!irqchip->irq_request_resources &&
1942 !irqchip->irq_release_resources) {
1943 irqchip->irq_request_resources = gpiochip_irq_reqres;
1944 irqchip->irq_release_resources = gpiochip_irq_relres;
1945 }
Linus Walleij14250522014-03-25 10:40:18 +01001946
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001947 acpi_gpiochip_request_interrupts(gpiochip);
1948
Linus Walleij14250522014-03-25 10:40:18 +01001949 return 0;
1950}
Linus Walleij739e6f52017-01-11 13:37:07 +01001951EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key);
Linus Walleij14250522014-03-25 10:40:18 +01001952
1953#else /* CONFIG_GPIOLIB_IRQCHIP */
1954
Thierry Reding959bc7b2017-11-07 19:15:59 +01001955static inline int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
Linus Walleijec478732017-11-08 21:40:41 +01001956 struct lock_class_key *key)
Thierry Redinge0d89722017-11-07 19:15:54 +01001957{
1958 return 0;
1959}
1960
Linus Walleij14250522014-03-25 10:40:18 +01001961static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
Mika Westerberg79b804c2016-09-20 15:15:21 +03001962static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1963{
1964 return 0;
1965}
1966static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1967{ }
Linus Walleij14250522014-03-25 10:40:18 +01001968
1969#endif /* CONFIG_GPIOLIB_IRQCHIP */
1970
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001971/**
1972 * gpiochip_generic_request() - request the gpio function for a pin
1973 * @chip: the gpiochip owning the GPIO
1974 * @offset: the offset of the GPIO to request for GPIO function
1975 */
1976int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
1977{
Linus Walleija9a1d2a2017-09-22 11:02:10 +02001978 return pinctrl_gpio_request(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001979}
1980EXPORT_SYMBOL_GPL(gpiochip_generic_request);
1981
1982/**
1983 * gpiochip_generic_free() - free the gpio function from a pin
1984 * @chip: the gpiochip to request the gpio function for
1985 * @offset: the offset of the GPIO to free from GPIO function
1986 */
1987void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
1988{
Linus Walleija9a1d2a2017-09-22 11:02:10 +02001989 pinctrl_gpio_free(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001990}
1991EXPORT_SYMBOL_GPL(gpiochip_generic_free);
1992
Mika Westerberg2956b5d2017-01-23 15:34:34 +03001993/**
1994 * gpiochip_generic_config() - apply configuration for a pin
1995 * @chip: the gpiochip owning the GPIO
1996 * @offset: the offset of the GPIO to apply the configuration
1997 * @config: the configuration to be applied
1998 */
1999int gpiochip_generic_config(struct gpio_chip *chip, unsigned offset,
2000 unsigned long config)
2001{
2002 return pinctrl_gpio_set_config(chip->gpiodev->base + offset, config);
2003}
2004EXPORT_SYMBOL_GPL(gpiochip_generic_config);
2005
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302006#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01002007
Linus Walleij3f0f8672012-11-20 12:40:15 +01002008/**
Christian Ruppert586a87e2013-10-15 15:37:54 +02002009 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
2010 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +02002011 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +02002012 * @gpio_offset: the start offset in the current gpio_chip number space
2013 * @pin_group: name of the pin group inside the pin controller
2014 */
2015int gpiochip_add_pingroup_range(struct gpio_chip *chip,
2016 struct pinctrl_dev *pctldev,
2017 unsigned int gpio_offset, const char *pin_group)
2018{
2019 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002020 struct gpio_device *gdev = chip->gpiodev;
Christian Ruppert586a87e2013-10-15 15:37:54 +02002021 int ret;
2022
2023 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
2024 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002025 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +02002026 return -ENOMEM;
2027 }
2028
2029 /* Use local offset as range ID */
2030 pin_range->range.id = gpio_offset;
2031 pin_range->range.gc = chip;
2032 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002033 pin_range->range.base = gdev->base + gpio_offset;
Christian Ruppert586a87e2013-10-15 15:37:54 +02002034 pin_range->pctldev = pctldev;
2035
2036 ret = pinctrl_get_group_pins(pctldev, pin_group,
2037 &pin_range->range.pins,
2038 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +01002039 if (ret < 0) {
2040 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +02002041 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +01002042 }
Christian Ruppert586a87e2013-10-15 15:37:54 +02002043
2044 pinctrl_add_gpio_range(pctldev, &pin_range->range);
2045
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002046 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
2047 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +02002048 pinctrl_dev_get_devname(pctldev), pin_group);
2049
Linus Walleij20ec3e32016-02-11 11:03:06 +01002050 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Christian Ruppert586a87e2013-10-15 15:37:54 +02002051
2052 return 0;
2053}
2054EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
2055
2056/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01002057 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
2058 * @chip: the gpiochip to add the range for
Thierry Reding950d55f52017-07-24 16:57:22 +02002059 * @pinctl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01002060 * @gpio_offset: the start offset in the current gpio_chip number space
2061 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01002062 * @npins: the number of pins from the offset of each pin space (GPIO and
2063 * pin controller) to accumulate in this range
Thierry Reding950d55f52017-07-24 16:57:22 +02002064 *
2065 * Returns:
2066 * 0 on success, or a negative error-code on failure.
Linus Walleij3f0f8672012-11-20 12:40:15 +01002067 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002068int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01002069 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01002070 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302071{
2072 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002073 struct gpio_device *gdev = chip->gpiodev;
Axel Linb4d4b1f2012-11-21 14:33:56 +08002074 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302075
Linus Walleij3f0f8672012-11-20 12:40:15 +01002076 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302077 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002078 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002079 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302080 }
2081
Linus Walleij3f0f8672012-11-20 12:40:15 +01002082 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01002083 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01002084 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302085 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002086 pin_range->range.base = gdev->base + gpio_offset;
Linus Walleij316511c2012-11-21 08:48:09 +01002087 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302088 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01002089 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302090 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01002091 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08002092 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002093 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +01002094 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08002095 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01002096 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002097 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
2098 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +01002099 pinctl_name,
2100 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302101
Linus Walleij20ec3e32016-02-11 11:03:06 +01002102 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002103
2104 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302105}
Linus Walleij165adc92012-11-06 14:49:39 +01002106EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302107
Linus Walleij3f0f8672012-11-20 12:40:15 +01002108/**
2109 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
2110 * @chip: the chip to remove all the mappings for
2111 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302112void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
2113{
2114 struct gpio_pin_range *pin_range, *tmp;
Linus Walleij20ec3e32016-02-11 11:03:06 +01002115 struct gpio_device *gdev = chip->gpiodev;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302116
Linus Walleij20ec3e32016-02-11 11:03:06 +01002117 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302118 list_del(&pin_range->node);
2119 pinctrl_remove_gpio_range(pin_range->pctldev,
2120 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01002121 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302122 }
2123}
Linus Walleij165adc92012-11-06 14:49:39 +01002124EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
2125
2126#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302127
David Brownelld2876d02008-02-04 22:28:20 -08002128/* These "optional" allocation calls help prevent drivers from stomping
2129 * on each other, and help provide better diagnostics in debugfs.
2130 * They're called even less than the "set direction" calls.
2131 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002132static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08002133{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002134 struct gpio_chip *chip = desc->gdev->chip;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002135 int status;
David Brownelld2876d02008-02-04 22:28:20 -08002136 unsigned long flags;
2137
2138 spin_lock_irqsave(&gpio_lock, flags);
2139
David Brownelld2876d02008-02-04 22:28:20 -08002140 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07002141 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08002142 */
2143
2144 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
2145 desc_set_label(desc, label ? : "?");
2146 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07002147 } else {
David Brownelld2876d02008-02-04 22:28:20 -08002148 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -08002149 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07002150 }
2151
2152 if (chip->request) {
2153 /* chip->request may sleep */
2154 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002155 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07002156 spin_lock_irqsave(&gpio_lock, flags);
2157
2158 if (status < 0) {
2159 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -07002160 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03002161 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07002162 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07002163 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03002164 if (chip->get_direction) {
2165 /* chip->get_direction may sleep */
2166 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002167 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03002168 spin_lock_irqsave(&gpio_lock, flags);
2169 }
David Brownelld2876d02008-02-04 22:28:20 -08002170done:
Mika Westerberg77c2d792014-03-10 14:54:50 +02002171 spin_unlock_irqrestore(&gpio_lock, flags);
2172 return status;
2173}
2174
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002175/*
2176 * This descriptor validation needs to be inserted verbatim into each
2177 * function taking a descriptor, so we need to use a preprocessor
Linus Walleij54d77192016-05-30 16:48:39 +02002178 * macro to avoid endless duplication. If the desc is NULL it is an
2179 * optional GPIO and calls should just bail out.
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002180 */
2181#define VALIDATE_DESC(desc) do { \
Linus Walleij54d77192016-05-30 16:48:39 +02002182 if (!desc) \
2183 return 0; \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002184 if (IS_ERR(desc)) { \
2185 pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
2186 return PTR_ERR(desc); \
2187 } \
Linus Walleij54d77192016-05-30 16:48:39 +02002188 if (!desc->gdev) { \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002189 pr_warn("%s: invalid GPIO (no device)\n", __func__); \
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002190 return -EINVAL; \
2191 } \
2192 if ( !desc->gdev->chip ) { \
2193 dev_warn(&desc->gdev->dev, \
2194 "%s: backing chip is gone\n", __func__); \
2195 return 0; \
2196 } } while (0)
2197
2198#define VALIDATE_DESC_VOID(desc) do { \
Linus Walleij54d77192016-05-30 16:48:39 +02002199 if (!desc) \
2200 return; \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002201 if (IS_ERR(desc)) { \
2202 pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
2203 return; \
2204 } \
Linus Walleij54d77192016-05-30 16:48:39 +02002205 if (!desc->gdev) { \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002206 pr_warn("%s: invalid GPIO (no device)\n", __func__); \
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002207 return; \
2208 } \
2209 if (!desc->gdev->chip) { \
2210 dev_warn(&desc->gdev->dev, \
2211 "%s: backing chip is gone\n", __func__); \
2212 return; \
2213 } } while (0)
2214
2215
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002216int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002217{
2218 int status = -EPROBE_DEFER;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002219 struct gpio_device *gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002220
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002221 VALIDATE_DESC(desc);
2222 gdev = desc->gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002223
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002224 if (try_module_get(gdev->owner)) {
Linus Walleijfac9d882017-09-26 20:58:28 +02002225 status = gpiod_request_commit(desc, label);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002226 if (status < 0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002227 module_put(gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002228 else
2229 get_device(&gdev->dev);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002230 }
2231
David Brownelld2876d02008-02-04 22:28:20 -08002232 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +02002233 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002234
David Brownelld2876d02008-02-04 22:28:20 -08002235 return status;
2236}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002237
Linus Walleijfac9d882017-09-26 20:58:28 +02002238static bool gpiod_free_commit(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002239{
Mika Westerberg77c2d792014-03-10 14:54:50 +02002240 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08002241 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07002242 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002243
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07002244 might_sleep();
2245
Alexandre Courbot372e7222013-02-03 01:29:29 +09002246 gpiod_unexport(desc);
David Brownelld8f388d82008-07-25 01:46:07 -07002247
David Brownelld2876d02008-02-04 22:28:20 -08002248 spin_lock_irqsave(&gpio_lock, flags);
2249
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002250 chip = desc->gdev->chip;
David Brownell35e8bb52008-10-15 22:03:16 -07002251 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
2252 if (chip->free) {
2253 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07002254 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002255 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07002256 spin_lock_irqsave(&gpio_lock, flags);
2257 }
David Brownelld2876d02008-02-04 22:28:20 -08002258 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08002259 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07002260 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302261 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302262 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06002263 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002264 ret = true;
2265 }
David Brownelld2876d02008-02-04 22:28:20 -08002266
2267 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002268 return ret;
2269}
2270
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002271void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002272{
Linus Walleijfac9d882017-09-26 20:58:28 +02002273 if (desc && desc->gdev && gpiod_free_commit(desc)) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002274 module_put(desc->gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002275 put_device(&desc->gdev->dev);
2276 } else {
Mika Westerberg77c2d792014-03-10 14:54:50 +02002277 WARN_ON(extra_checks);
Linus Walleij33a68e82016-02-11 10:28:44 +01002278 }
David Brownelld2876d02008-02-04 22:28:20 -08002279}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002280
David Brownelld2876d02008-02-04 22:28:20 -08002281/**
2282 * gpiochip_is_requested - return string iff signal was requested
2283 * @chip: controller managing the signal
2284 * @offset: of signal within controller's 0..(ngpio - 1) range
2285 *
2286 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09002287 * The string returned is the label passed to gpio_request(); if none has been
2288 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08002289 *
2290 * This function is for use by GPIO controller drivers. The label can
2291 * help with diagnostics, and knowing that the signal is used as a GPIO
2292 * can help avoid accidentally multiplexing it to another controller.
2293 */
2294const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
2295{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002296 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08002297
Dirk Behme48b59532015-08-18 18:02:32 +02002298 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08002299 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002300
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002301 desc = &chip->gpiodev->descs[offset];
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002302
Alexandre Courbot372e7222013-02-03 01:29:29 +09002303 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08002304 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002305 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08002306}
2307EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2308
Mika Westerberg77c2d792014-03-10 14:54:50 +02002309/**
2310 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
Thierry Reding950d55f52017-07-24 16:57:22 +02002311 * @chip: GPIO chip
2312 * @hwnum: hardware number of the GPIO for which to request the descriptor
Mika Westerberg77c2d792014-03-10 14:54:50 +02002313 * @label: label for the GPIO
2314 *
2315 * Function allows GPIO chip drivers to request and use their own GPIO
2316 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2317 * function will not increase reference count of the GPIO chip module. This
2318 * allows the GPIO chip module to be unloaded as needed (we assume that the
2319 * GPIO chip driver handles freeing the GPIOs it has requested).
Thierry Reding950d55f52017-07-24 16:57:22 +02002320 *
2321 * Returns:
2322 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2323 * code on failure.
Mika Westerberg77c2d792014-03-10 14:54:50 +02002324 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002325struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
2326 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002327{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002328 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
2329 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002330
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002331 if (IS_ERR(desc)) {
2332 chip_err(chip, "failed to get GPIO descriptor\n");
2333 return desc;
2334 }
2335
Linus Walleijfac9d882017-09-26 20:58:28 +02002336 err = gpiod_request_commit(desc, label);
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002337 if (err < 0)
2338 return ERR_PTR(err);
2339
2340 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002341}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002342EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002343
2344/**
2345 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2346 * @desc: GPIO descriptor to free
2347 *
2348 * Function frees the given GPIO requested previously with
2349 * gpiochip_request_own_desc().
2350 */
2351void gpiochip_free_own_desc(struct gpio_desc *desc)
2352{
2353 if (desc)
Linus Walleijfac9d882017-09-26 20:58:28 +02002354 gpiod_free_commit(desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002355}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002356EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08002357
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002358/*
2359 * Drivers MUST set GPIO direction before making get/set calls. In
David Brownelld2876d02008-02-04 22:28:20 -08002360 * some cases this is done in early boot, before IRQs are enabled.
2361 *
2362 * As a rule these aren't called more than once (except for drivers
2363 * using the open-drain emulation idiom) so these are natural places
2364 * to accumulate extra debugging checks. Note that we can't (yet)
2365 * rely on gpio_request() having been called beforehand.
2366 */
2367
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002368/**
2369 * gpiod_direction_input - set the GPIO direction to input
2370 * @desc: GPIO to set to input
2371 *
2372 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2373 * be called safely on it.
2374 *
2375 * Return 0 in case of success, else an error code.
2376 */
2377int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002378{
David Brownelld2876d02008-02-04 22:28:20 -08002379 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002380 int status = -EINVAL;
2381
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002382 VALIDATE_DESC(desc);
2383 chip = desc->gdev->chip;
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002384
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002385 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01002386 gpiod_warn(desc,
2387 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02002388 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002389 return -EIO;
2390 }
2391
Alexandre Courbotd82da792014-07-22 16:17:43 +09002392 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -08002393 if (status == 0)
2394 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002395
Alexandre Courbot372e7222013-02-03 01:29:29 +09002396 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09002397
David Brownelld2876d02008-02-04 22:28:20 -08002398 return status;
2399}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002400EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002401
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002402static int gpio_set_drive_single_ended(struct gpio_chip *gc, unsigned offset,
2403 enum pin_config_param mode)
2404{
2405 unsigned long config = { PIN_CONF_PACKED(mode, 0) };
2406
2407 return gc->set_config ? gc->set_config(gc, offset, config) : -ENOTSUPP;
2408}
2409
Linus Walleijfac9d882017-09-26 20:58:28 +02002410static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08002411{
Linus Walleijc663e5f2016-03-22 10:51:16 +01002412 struct gpio_chip *gc = desc->gdev->chip;
Linus Walleijad177312016-11-13 23:02:44 +01002413 int val = !!value;
Linus Walleijc663e5f2016-03-22 10:51:16 +01002414 int ret;
David Brownelld2876d02008-02-04 22:28:20 -08002415
Linus Walleijc663e5f2016-03-22 10:51:16 +01002416 if (!gc->set || !gc->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01002417 gpiod_warn(desc,
2418 "%s: missing set() or direction_output() operations\n",
2419 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002420 return -EIO;
2421 }
2422
Linus Walleijad177312016-11-13 23:02:44 +01002423 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
Linus Walleijc663e5f2016-03-22 10:51:16 +01002424 if (!ret)
David Brownelld2876d02008-02-04 22:28:20 -08002425 set_bit(FLAG_IS_OUT, &desc->flags);
Linus Walleijad177312016-11-13 23:02:44 +01002426 trace_gpio_value(desc_to_gpio(desc), 0, val);
Linus Walleijc663e5f2016-03-22 10:51:16 +01002427 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2428 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08002429}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002430
2431/**
2432 * gpiod_direction_output_raw - set the GPIO direction to output
2433 * @desc: GPIO to set to output
2434 * @value: initial output value of the GPIO
2435 *
2436 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2437 * be called safely on it. The initial value of the output must be specified
2438 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2439 *
2440 * Return 0 in case of success, else an error code.
2441 */
2442int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2443{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002444 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02002445 return gpiod_direction_output_raw_commit(desc, value);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002446}
2447EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2448
2449/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05302450 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002451 * @desc: GPIO to set to output
2452 * @value: initial output value of the GPIO
2453 *
2454 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2455 * be called safely on it. The initial value of the output must be specified
2456 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2457 * account.
2458 *
2459 * Return 0 in case of success, else an error code.
2460 */
2461int gpiod_direction_output(struct gpio_desc *desc, int value)
2462{
Linus Walleij02e47982017-09-26 21:20:23 +02002463 struct gpio_chip *gc = desc->gdev->chip;
2464 int ret;
2465
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002466 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002467 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2468 value = !value;
Linus Walleijad177312016-11-13 23:02:44 +01002469 else
2470 value = !!value;
Linus Walleij02e47982017-09-26 21:20:23 +02002471
2472 /* GPIOs used for IRQs shall not be set as output */
2473 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
2474 gpiod_err(desc,
2475 "%s: tried to set a GPIO tied to an IRQ as output\n",
2476 __func__);
2477 return -EIO;
2478 }
2479
2480 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2481 /* First see if we can enable open drain in hardware */
2482 ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2483 PIN_CONFIG_DRIVE_OPEN_DRAIN);
2484 if (!ret)
2485 goto set_output_value;
2486 /* Emulate open drain by not actively driving the line high */
2487 if (value)
2488 return gpiod_direction_input(desc);
2489 }
2490 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2491 ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2492 PIN_CONFIG_DRIVE_OPEN_SOURCE);
2493 if (!ret)
2494 goto set_output_value;
2495 /* Emulate open source by not actively driving the line low */
2496 if (!value)
2497 return gpiod_direction_input(desc);
2498 } else {
2499 gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2500 PIN_CONFIG_DRIVE_PUSH_PULL);
2501 }
2502
2503set_output_value:
Linus Walleijfac9d882017-09-26 20:58:28 +02002504 return gpiod_direction_output_raw_commit(desc, value);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002505}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002506EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08002507
Felipe Balbic4b5be92010-05-26 14:42:23 -07002508/**
Thierry Reding950d55f52017-07-24 16:57:22 +02002509 * gpiod_set_debounce - sets @debounce time for a GPIO
2510 * @desc: descriptor of the GPIO for which to set debounce time
2511 * @debounce: debounce time in microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02002512 *
Thierry Reding950d55f52017-07-24 16:57:22 +02002513 * Returns:
2514 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2515 * debounce time.
Felipe Balbic4b5be92010-05-26 14:42:23 -07002516 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002517int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07002518{
Felipe Balbic4b5be92010-05-26 14:42:23 -07002519 struct gpio_chip *chip;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002520 unsigned long config;
Felipe Balbic4b5be92010-05-26 14:42:23 -07002521
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002522 VALIDATE_DESC(desc);
2523 chip = desc->gdev->chip;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002524 if (!chip->set || !chip->set_config) {
Mark Brown6424de52013-09-09 10:33:49 +01002525 gpiod_dbg(desc,
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002526 "%s: missing set() or set_config() operations\n",
Mark Brown6424de52013-09-09 10:33:49 +01002527 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02002528 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002529 }
2530
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002531 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
2532 return chip->set_config(chip, gpio_chip_hwgpio(desc), config);
Felipe Balbic4b5be92010-05-26 14:42:23 -07002533}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002534EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002535
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002536/**
Andrew Jefferye10f72b2017-11-30 14:25:24 +10302537 * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
2538 * @desc: descriptor of the GPIO for which to configure persistence
2539 * @transitory: True to lose state on suspend or reset, false for persistence
2540 *
2541 * Returns:
2542 * 0 on success, otherwise a negative error code.
2543 */
2544int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
2545{
2546 struct gpio_chip *chip;
2547 unsigned long packed;
2548 int gpio;
2549 int rc;
2550
2551 /*
2552 * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
2553 * persistence state.
2554 */
2555 if (transitory)
2556 set_bit(FLAG_TRANSITORY, &desc->flags);
2557 else
2558 clear_bit(FLAG_TRANSITORY, &desc->flags);
2559
2560 /* If the driver supports it, set the persistence state now */
2561 chip = desc->gdev->chip;
2562 if (!chip->set_config)
2563 return 0;
2564
2565 packed = pinconf_to_config_packed(PIN_CONFIG_PERSIST_STATE,
2566 !transitory);
2567 gpio = gpio_chip_hwgpio(desc);
2568 rc = chip->set_config(chip, gpio, packed);
2569 if (rc == -ENOTSUPP) {
2570 dev_dbg(&desc->gdev->dev, "Persistence not supported for GPIO %d\n",
2571 gpio);
2572 return 0;
2573 }
2574
2575 return rc;
2576}
2577EXPORT_SYMBOL_GPL(gpiod_set_transitory);
2578
2579/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002580 * gpiod_is_active_low - test whether a GPIO is active-low or not
2581 * @desc: the gpio descriptor to test
2582 *
2583 * Returns 1 if the GPIO is active-low, 0 otherwise.
2584 */
2585int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002586{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002587 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002588 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002589}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002590EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08002591
2592/* I/O calls are only valid after configuration completed; the relevant
2593 * "is this a valid GPIO" error checks should already have been done.
2594 *
2595 * "Get" operations are often inlinable as reading a pin value register,
2596 * and masking the relevant bit in that register.
2597 *
2598 * When "set" operations are inlinable, they involve writing that mask to
2599 * one register to set a low value, or a different register to set it high.
2600 * Otherwise locking is needed, so there may be little value to inlining.
2601 *
2602 *------------------------------------------------------------------------
2603 *
2604 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2605 * have requested the GPIO. That can include implicit requesting by
2606 * a direction setting call. Marking a gpio as requested locks its chip
2607 * in memory, guaranteeing that these table lookups need no more locking
2608 * and that gpiochip_remove() will fail.
2609 *
2610 * REVISIT when debugging, consider adding some instrumentation to ensure
2611 * that the GPIO was actually requested.
2612 */
2613
Linus Walleijfac9d882017-09-26 20:58:28 +02002614static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002615{
2616 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002617 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002618 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002619
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002620 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002621 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002622 value = chip->get ? chip->get(chip, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01002623 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002624 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002625 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002626}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002627
Lukas Wunnereec1d562017-10-12 12:40:10 +02002628static int gpio_chip_get_multiple(struct gpio_chip *chip,
2629 unsigned long *mask, unsigned long *bits)
2630{
2631 if (chip->get_multiple) {
2632 return chip->get_multiple(chip, mask, bits);
2633 } else if (chip->get) {
2634 int i, value;
2635
2636 for_each_set_bit(i, mask, chip->ngpio) {
2637 value = chip->get(chip, i);
2638 if (value < 0)
2639 return value;
2640 __assign_bit(i, bits, value);
2641 }
2642 return 0;
2643 }
2644 return -EIO;
2645}
2646
2647int gpiod_get_array_value_complex(bool raw, bool can_sleep,
2648 unsigned int array_size,
2649 struct gpio_desc **desc_array,
2650 int *value_array)
2651{
2652 int i = 0;
2653
2654 while (i < array_size) {
2655 struct gpio_chip *chip = desc_array[i]->gdev->chip;
2656 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2657 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2658 int first, j, ret;
2659
2660 if (!can_sleep)
2661 WARN_ON(chip->can_sleep);
2662
2663 /* collect all inputs belonging to the same chip */
2664 first = i;
2665 memset(mask, 0, sizeof(mask));
2666 do {
2667 const struct gpio_desc *desc = desc_array[i];
2668 int hwgpio = gpio_chip_hwgpio(desc);
2669
2670 __set_bit(hwgpio, mask);
2671 i++;
2672 } while ((i < array_size) &&
2673 (desc_array[i]->gdev->chip == chip));
2674
2675 ret = gpio_chip_get_multiple(chip, mask, bits);
2676 if (ret)
2677 return ret;
2678
2679 for (j = first; j < i; j++) {
2680 const struct gpio_desc *desc = desc_array[j];
2681 int hwgpio = gpio_chip_hwgpio(desc);
2682 int value = test_bit(hwgpio, bits);
2683
2684 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2685 value = !value;
2686 value_array[j] = value;
2687 trace_gpio_value(desc_to_gpio(desc), 1, value);
2688 }
2689 }
2690 return 0;
2691}
2692
David Brownelld2876d02008-02-04 22:28:20 -08002693/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002694 * gpiod_get_raw_value() - return a gpio's raw value
2695 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08002696 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002697 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002698 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002699 *
2700 * This function should be called from contexts where we cannot sleep, and will
2701 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002702 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002703int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002704{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002705 VALIDATE_DESC(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002706 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002707 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02002708 return gpiod_get_raw_value_commit(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002709}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002710EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002711
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002712/**
2713 * gpiod_get_value() - return a gpio's value
2714 * @desc: gpio whose value will be returned
2715 *
2716 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002717 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002718 *
2719 * This function should be called from contexts where we cannot sleep, and will
2720 * complain if the GPIO chip functions potentially sleep.
2721 */
2722int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002723{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002724 int value;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002725
2726 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002727 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002728 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002729
Linus Walleijfac9d882017-09-26 20:58:28 +02002730 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002731 if (value < 0)
2732 return value;
2733
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002734 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2735 value = !value;
2736
2737 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002738}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002739EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08002740
Lukas Wunnereec1d562017-10-12 12:40:10 +02002741/**
2742 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
2743 * @array_size: number of elements in the descriptor / value arrays
2744 * @desc_array: array of GPIO descriptors whose values will be read
2745 * @value_array: array to store the read values
2746 *
2747 * Read the raw values of the GPIOs, i.e. the values of the physical lines
2748 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
2749 * else an error code.
2750 *
2751 * This function should be called from contexts where we cannot sleep,
2752 * and it will complain if the GPIO chip functions potentially sleep.
2753 */
2754int gpiod_get_raw_array_value(unsigned int array_size,
2755 struct gpio_desc **desc_array, int *value_array)
2756{
2757 if (!desc_array)
2758 return -EINVAL;
2759 return gpiod_get_array_value_complex(true, false, array_size,
2760 desc_array, value_array);
2761}
2762EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
2763
2764/**
2765 * gpiod_get_array_value() - read values from an array of GPIOs
2766 * @array_size: number of elements in the descriptor / value arrays
2767 * @desc_array: array of GPIO descriptors whose values will be read
2768 * @value_array: array to store the read values
2769 *
2770 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2771 * into account. Return 0 in case of success, else an error code.
2772 *
2773 * This function should be called from contexts where we cannot sleep,
2774 * and it will complain if the GPIO chip functions potentially sleep.
2775 */
2776int gpiod_get_array_value(unsigned int array_size,
2777 struct gpio_desc **desc_array, int *value_array)
2778{
2779 if (!desc_array)
2780 return -EINVAL;
2781 return gpiod_get_array_value_complex(false, false, array_size,
2782 desc_array, value_array);
2783}
2784EXPORT_SYMBOL_GPL(gpiod_get_array_value);
2785
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302786/*
Linus Walleijfac9d882017-09-26 20:58:28 +02002787 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002788 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002789 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302790 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002791static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302792{
2793 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002794 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002795 int offset = gpio_chip_hwgpio(desc);
2796
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302797 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002798 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302799 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002800 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302801 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002802 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302803 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002804 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302805 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002806 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302807 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002808 gpiod_err(desc,
2809 "%s: Error in set_value for open drain err %d\n",
2810 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302811}
2812
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302813/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002814 * _gpio_set_open_source_value() - Set the open source gpio's value.
2815 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002816 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302817 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002818static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302819{
2820 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002821 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002822 int offset = gpio_chip_hwgpio(desc);
2823
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302824 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002825 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302826 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002827 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302828 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002829 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302830 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002831 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302832 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002833 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302834 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002835 gpiod_err(desc,
2836 "%s: Error in set_value for open source err %d\n",
2837 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302838}
2839
Linus Walleijfac9d882017-09-26 20:58:28 +02002840static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08002841{
2842 struct gpio_chip *chip;
2843
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002844 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002845 trace_gpio_value(desc_to_gpio(desc), 0, value);
Linus Walleij02e47982017-09-26 21:20:23 +02002846 chip->set(chip, gpio_chip_hwgpio(desc), value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002847}
2848
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002849/*
2850 * set multiple outputs on the same chip;
2851 * use the chip's set_multiple function if available;
2852 * otherwise set the outputs sequentially;
2853 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
2854 * defines which outputs are to be changed
2855 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
2856 * defines the values the outputs specified by mask are to be set to
2857 */
2858static void gpio_chip_set_multiple(struct gpio_chip *chip,
2859 unsigned long *mask, unsigned long *bits)
2860{
2861 if (chip->set_multiple) {
2862 chip->set_multiple(chip, mask, bits);
2863 } else {
Andy Shevchenko5e4e6fb2017-01-03 19:01:17 +02002864 unsigned int i;
2865
2866 /* set outputs if the corresponding mask bit is set */
2867 for_each_set_bit(i, mask, chip->ngpio)
2868 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002869 }
2870}
2871
Linus Walleij44c72882016-04-24 11:36:59 +02002872void gpiod_set_array_value_complex(bool raw, bool can_sleep,
2873 unsigned int array_size,
2874 struct gpio_desc **desc_array,
2875 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002876{
2877 int i = 0;
2878
2879 while (i < array_size) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002880 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002881 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2882 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2883 int count = 0;
2884
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002885 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002886 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002887
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002888 memset(mask, 0, sizeof(mask));
2889 do {
2890 struct gpio_desc *desc = desc_array[i];
2891 int hwgpio = gpio_chip_hwgpio(desc);
2892 int value = value_array[i];
2893
2894 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2895 value = !value;
2896 trace_gpio_value(desc_to_gpio(desc), 0, value);
2897 /*
2898 * collect all normal outputs belonging to the same chip
2899 * open drain and open source outputs are set individually
2900 */
Linus Walleij02e47982017-09-26 21:20:23 +02002901 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02002902 gpio_set_open_drain_value_commit(desc, value);
Linus Walleij02e47982017-09-26 21:20:23 +02002903 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02002904 gpio_set_open_source_value_commit(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002905 } else {
2906 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002907 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002908 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002909 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002910 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002911 count++;
2912 }
2913 i++;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002914 } while ((i < array_size) &&
2915 (desc_array[i]->gdev->chip == chip));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002916 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002917 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002918 gpio_chip_set_multiple(chip, mask, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002919 }
2920}
2921
David Brownelld2876d02008-02-04 22:28:20 -08002922/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002923 * gpiod_set_raw_value() - assign a gpio's raw value
2924 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08002925 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002926 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002927 * Set the raw value of the GPIO, i.e. the value of its physical line without
2928 * regard for its ACTIVE_LOW status.
2929 *
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_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002934{
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);
Linus Walleijfac9d882017-09-26 20:58:28 +02002938 gpiod_set_raw_value_commit(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002939}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002940EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002941
2942/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002943 * gpiod_set_value() - assign a gpio's value
2944 * @desc: gpio whose value will be assigned
2945 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002946 *
Linus Walleij02e47982017-09-26 21:20:23 +02002947 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
2948 * OPEN_DRAIN and OPEN_SOURCE flags into account.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002949 *
2950 * This function should be called from contexts where we cannot sleep, and will
2951 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002952 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002953void gpiod_set_value(struct gpio_desc *desc, int value)
2954{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002955 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01002956 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002957 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002958 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2959 value = !value;
Linus Walleij02e47982017-09-26 21:20:23 +02002960 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2961 gpio_set_open_drain_value_commit(desc, value);
2962 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2963 gpio_set_open_source_value_commit(desc, value);
2964 else
2965 gpiod_set_raw_value_commit(desc, value);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002966}
2967EXPORT_SYMBOL_GPL(gpiod_set_value);
2968
2969/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002970 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002971 * @array_size: number of elements in the descriptor / value arrays
2972 * @desc_array: array of GPIO descriptors whose values will be assigned
2973 * @value_array: array of values to assign
2974 *
2975 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2976 * without regard for their ACTIVE_LOW status.
2977 *
2978 * This function should be called from contexts where we cannot sleep, and will
2979 * complain if the GPIO chip functions potentially sleep.
2980 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002981void gpiod_set_raw_array_value(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002982 struct gpio_desc **desc_array, int *value_array)
2983{
2984 if (!desc_array)
2985 return;
Linus Walleij44c72882016-04-24 11:36:59 +02002986 gpiod_set_array_value_complex(true, false, array_size, desc_array,
2987 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002988}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002989EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002990
2991/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002992 * gpiod_set_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002993 * @array_size: number of elements in the descriptor / value arrays
2994 * @desc_array: array of GPIO descriptors whose values will be assigned
2995 * @value_array: array of values to assign
2996 *
2997 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2998 * into account.
2999 *
3000 * This function should be called from contexts where we cannot sleep, and will
3001 * complain if the GPIO chip functions potentially sleep.
3002 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003003void gpiod_set_array_value(unsigned int array_size,
3004 struct gpio_desc **desc_array, int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003005{
3006 if (!desc_array)
3007 return;
Linus Walleij44c72882016-04-24 11:36:59 +02003008 gpiod_set_array_value_complex(false, false, array_size, desc_array,
3009 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003010}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003011EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003012
3013/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003014 * gpiod_cansleep() - report whether gpio value access may sleep
3015 * @desc: gpio to check
3016 *
3017 */
3018int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003019{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003020 VALIDATE_DESC(desc);
3021 return desc->gdev->chip->can_sleep;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003022}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003023EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08003024
David Brownell0f6d5042008-10-15 22:03:14 -07003025/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003026 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
3027 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07003028 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003029 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
3030 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07003031 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003032int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07003033{
Linus Walleij4c37ce82016-05-02 13:13:10 +02003034 struct gpio_chip *chip;
3035 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07003036
Linus Walleij79bb71b2016-06-15 22:57:38 +02003037 /*
3038 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
3039 * requires this function to not return zero on an invalid descriptor
3040 * but rather a negative error number.
3041 */
Linus Walleijbfbbe442016-06-16 11:55:55 +02003042 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
Linus Walleij79bb71b2016-06-15 22:57:38 +02003043 return -EINVAL;
3044
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003045 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003046 offset = gpio_chip_hwgpio(desc);
Linus Walleij4c37ce82016-05-02 13:13:10 +02003047 if (chip->to_irq) {
3048 int retirq = chip->to_irq(chip, offset);
3049
3050 /* Zero means NO_IRQ */
3051 if (!retirq)
3052 return -ENXIO;
3053
3054 return retirq;
3055 }
3056 return -ENXIO;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003057}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003058EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003059
Linus Walleijd468bf92013-09-24 11:54:38 +02003060/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003061 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003062 * @chip: the chip the GPIO to lock belongs to
3063 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02003064 *
3065 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08003066 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08003067 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003068int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08003069{
Linus Walleij9c102802016-05-25 10:56:03 +02003070 struct gpio_desc *desc;
Linus Walleijd468bf92013-09-24 11:54:38 +02003071
Linus Walleij9c102802016-05-25 10:56:03 +02003072 desc = gpiochip_get_desc(chip, offset);
3073 if (IS_ERR(desc))
3074 return PTR_ERR(desc);
3075
Linus Walleij60f83392016-11-12 15:01:09 +01003076 /*
3077 * If it's fast: flush the direction setting if something changed
3078 * behind our back
3079 */
3080 if (!chip->can_sleep && chip->get_direction) {
Linus Walleij9c102802016-05-25 10:56:03 +02003081 int dir = chip->get_direction(chip, offset);
3082
3083 if (dir)
3084 clear_bit(FLAG_IS_OUT, &desc->flags);
3085 else
3086 set_bit(FLAG_IS_OUT, &desc->flags);
3087 }
3088
3089 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003090 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02003091 "%s: tried to flag a GPIO set as output for IRQ\n",
3092 __func__);
3093 return -EIO;
3094 }
3095
Linus Walleij9c102802016-05-25 10:56:03 +02003096 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
Linus Walleij3940c342016-11-14 00:09:07 +01003097
3098 /*
3099 * If the consumer has not set up a label (such as when the
3100 * IRQ is referenced from .to_irq()) we set up a label here
3101 * so it is clear this is used as an interrupt.
3102 */
3103 if (!desc->label)
3104 desc_set_label(desc, "interrupt");
3105
Linus Walleijd468bf92013-09-24 11:54:38 +02003106 return 0;
3107}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003108EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02003109
Linus Walleijd468bf92013-09-24 11:54:38 +02003110/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003111 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003112 * @chip: the chip the GPIO to lock belongs to
3113 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02003114 *
3115 * This is used directly by GPIO drivers that want to indicate
3116 * that a certain GPIO is no longer used exclusively for IRQ.
3117 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003118void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02003119{
Linus Walleij3940c342016-11-14 00:09:07 +01003120 struct gpio_desc *desc;
3121
3122 desc = gpiochip_get_desc(chip, offset);
3123 if (IS_ERR(desc))
Linus Walleijd468bf92013-09-24 11:54:38 +02003124 return;
3125
Linus Walleij3940c342016-11-14 00:09:07 +01003126 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
3127
3128 /* If we only had this marking, erase it */
3129 if (desc->label && !strcmp(desc->label, "interrupt"))
3130 desc_set_label(desc, NULL);
Linus Walleijd468bf92013-09-24 11:54:38 +02003131}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003132EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02003133
Linus Walleij6cee3822016-02-11 20:16:45 +01003134bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
3135{
3136 if (offset >= chip->ngpio)
3137 return false;
3138
3139 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
3140}
3141EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
3142
Linus Walleij143b65d2016-02-16 15:41:42 +01003143bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
3144{
3145 if (offset >= chip->ngpio)
3146 return false;
3147
3148 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
3149}
3150EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
3151
3152bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
3153{
3154 if (offset >= chip->ngpio)
3155 return false;
3156
3157 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
3158}
3159EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
3160
Charles Keepax05f479b2017-05-23 15:47:29 +01003161bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset)
3162{
3163 if (offset >= chip->ngpio)
3164 return false;
3165
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303166 return !test_bit(FLAG_TRANSITORY, &chip->gpiodev->descs[offset].flags);
Charles Keepax05f479b2017-05-23 15:47:29 +01003167}
3168EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
3169
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003170/**
3171 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
3172 * @desc: gpio whose value will be returned
3173 *
3174 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003175 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003176 *
3177 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003178 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003179int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08003180{
David Brownelld2876d02008-02-04 22:28:20 -08003181 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003182 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003183 return gpiod_get_raw_value_commit(desc);
David Brownelld2876d02008-02-04 22:28:20 -08003184}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003185EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003186
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003187/**
3188 * gpiod_get_value_cansleep() - return a gpio's value
3189 * @desc: gpio whose value will be returned
3190 *
3191 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003192 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003193 *
3194 * This function is to be called from contexts that can sleep.
3195 */
3196int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003197{
David Brownelld2876d02008-02-04 22:28:20 -08003198 int value;
David Brownelld2876d02008-02-04 22:28:20 -08003199
3200 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003201 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003202 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003203 if (value < 0)
3204 return value;
3205
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003206 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3207 value = !value;
3208
David Brownelld2876d02008-02-04 22:28:20 -08003209 return value;
3210}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003211EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003212
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003213/**
Lukas Wunnereec1d562017-10-12 12:40:10 +02003214 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
3215 * @array_size: number of elements in the descriptor / value arrays
3216 * @desc_array: array of GPIO descriptors whose values will be read
3217 * @value_array: array to store the read values
3218 *
3219 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3220 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3221 * else an error code.
3222 *
3223 * This function is to be called from contexts that can sleep.
3224 */
3225int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
3226 struct gpio_desc **desc_array,
3227 int *value_array)
3228{
3229 might_sleep_if(extra_checks);
3230 if (!desc_array)
3231 return -EINVAL;
3232 return gpiod_get_array_value_complex(true, true, array_size,
3233 desc_array, value_array);
3234}
3235EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
3236
3237/**
3238 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
3239 * @array_size: number of elements in the descriptor / value arrays
3240 * @desc_array: array of GPIO descriptors whose values will be read
3241 * @value_array: array to store the read values
3242 *
3243 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3244 * into account. Return 0 in case of success, else an error code.
3245 *
3246 * This function is to be called from contexts that can sleep.
3247 */
3248int gpiod_get_array_value_cansleep(unsigned int array_size,
3249 struct gpio_desc **desc_array,
3250 int *value_array)
3251{
3252 might_sleep_if(extra_checks);
3253 if (!desc_array)
3254 return -EINVAL;
3255 return gpiod_get_array_value_complex(false, true, array_size,
3256 desc_array, value_array);
3257}
3258EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
3259
3260/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003261 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
3262 * @desc: gpio whose value will be assigned
3263 * @value: value to assign
3264 *
3265 * Set the raw value of the GPIO, i.e. the value of its physical line without
3266 * regard for its ACTIVE_LOW status.
3267 *
3268 * This function is to be called from contexts that can sleep.
3269 */
3270void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003271{
David Brownelld2876d02008-02-04 22:28:20 -08003272 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003273 VALIDATE_DESC_VOID(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003274 gpiod_set_raw_value_commit(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003275}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003276EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003277
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003278/**
3279 * gpiod_set_value_cansleep() - assign a gpio's value
3280 * @desc: gpio whose value will be assigned
3281 * @value: value to assign
3282 *
3283 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3284 * account
3285 *
3286 * This function is to be called from contexts that can sleep.
3287 */
3288void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003289{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003290 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003291 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003292 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3293 value = !value;
Linus Walleijfac9d882017-09-26 20:58:28 +02003294 gpiod_set_raw_value_commit(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08003295}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003296EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08003297
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003298/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003299 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003300 * @array_size: number of elements in the descriptor / value arrays
3301 * @desc_array: array of GPIO descriptors whose values will be assigned
3302 * @value_array: array of values to assign
3303 *
3304 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3305 * without regard for their ACTIVE_LOW status.
3306 *
3307 * This function is to be called from contexts that can sleep.
3308 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003309void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
3310 struct gpio_desc **desc_array,
3311 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003312{
3313 might_sleep_if(extra_checks);
3314 if (!desc_array)
3315 return;
Linus Walleij44c72882016-04-24 11:36:59 +02003316 gpiod_set_array_value_complex(true, true, array_size, desc_array,
3317 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003318}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003319EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003320
3321/**
Dmitry Torokhov3946d182017-08-14 21:59:55 -07003322 * gpiod_add_lookup_tables() - register GPIO device consumers
3323 * @tables: list of tables of consumers to register
3324 * @n: number of tables in the list
3325 */
3326void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
3327{
3328 unsigned int i;
3329
3330 mutex_lock(&gpio_lookup_lock);
3331
3332 for (i = 0; i < n; i++)
3333 list_add_tail(&tables[i]->list, &gpio_lookup_list);
3334
3335 mutex_unlock(&gpio_lookup_lock);
3336}
3337
3338/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003339 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003340 * @array_size: number of elements in the descriptor / value arrays
3341 * @desc_array: array of GPIO descriptors whose values will be assigned
3342 * @value_array: array of values to assign
3343 *
3344 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3345 * into account.
3346 *
3347 * This function is to be called from contexts that can sleep.
3348 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003349void gpiod_set_array_value_cansleep(unsigned int array_size,
3350 struct gpio_desc **desc_array,
3351 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003352{
3353 might_sleep_if(extra_checks);
3354 if (!desc_array)
3355 return;
Linus Walleij44c72882016-04-24 11:36:59 +02003356 gpiod_set_array_value_complex(false, true, array_size, desc_array,
3357 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003358}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003359EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003360
3361/**
Alexandre Courbotad824782013-12-03 12:20:11 +09003362 * gpiod_add_lookup_table() - register GPIO device consumers
3363 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003364 */
Alexandre Courbotad824782013-12-03 12:20:11 +09003365void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003366{
3367 mutex_lock(&gpio_lookup_lock);
3368
Alexandre Courbotad824782013-12-03 12:20:11 +09003369 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003370
3371 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08003372}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02003373EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
David Brownelld2876d02008-02-04 22:28:20 -08003374
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05303375/**
3376 * gpiod_remove_lookup_table() - unregister GPIO device consumers
3377 * @table: table of consumers to unregister
3378 */
3379void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
3380{
3381 mutex_lock(&gpio_lookup_lock);
3382
3383 list_del(&table->list);
3384
3385 mutex_unlock(&gpio_lookup_lock);
3386}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02003387EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05303388
Alexandre Courbotad824782013-12-03 12:20:11 +09003389static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
3390{
3391 const char *dev_id = dev ? dev_name(dev) : NULL;
3392 struct gpiod_lookup_table *table;
3393
3394 mutex_lock(&gpio_lookup_lock);
3395
3396 list_for_each_entry(table, &gpio_lookup_list, list) {
3397 if (table->dev_id && dev_id) {
3398 /*
3399 * Valid strings on both ends, must be identical to have
3400 * a match
3401 */
3402 if (!strcmp(table->dev_id, dev_id))
3403 goto found;
3404 } else {
3405 /*
3406 * One of the pointers is NULL, so both must be to have
3407 * a match
3408 */
3409 if (dev_id == table->dev_id)
3410 goto found;
3411 }
3412 }
3413 table = NULL;
3414
3415found:
3416 mutex_unlock(&gpio_lookup_lock);
3417 return table;
3418}
3419
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003420static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09003421 unsigned int idx,
3422 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003423{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003424 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09003425 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003426 struct gpiod_lookup *p;
3427
Alexandre Courbotad824782013-12-03 12:20:11 +09003428 table = gpiod_find_lookup_table(dev);
3429 if (!table)
3430 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003431
Alexandre Courbotad824782013-12-03 12:20:11 +09003432 for (p = &table->table[0]; p->chip_label; p++) {
3433 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003434
Alexandre Courbotad824782013-12-03 12:20:11 +09003435 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003436 if (p->idx != idx)
3437 continue;
3438
Alexandre Courbotad824782013-12-03 12:20:11 +09003439 /* If the lookup entry has a con_id, require exact match */
3440 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
3441 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003442
Alexandre Courbotad824782013-12-03 12:20:11 +09003443 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003444
Alexandre Courbotad824782013-12-03 12:20:11 +09003445 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003446 dev_err(dev, "cannot find GPIO chip %s\n",
3447 p->chip_label);
3448 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003449 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003450
Alexandre Courbotad824782013-12-03 12:20:11 +09003451 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003452 dev_err(dev,
3453 "requested GPIO %d is out of range [0..%d] for chip %s\n",
3454 idx, chip->ngpio, chip->label);
3455 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09003456 }
3457
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09003458 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09003459 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003460
3461 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09003462 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003463
3464 return desc;
3465}
3466
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003467static int dt_gpio_count(struct device *dev, const char *con_id)
3468{
3469 int ret;
3470 char propname[32];
3471 unsigned int i;
3472
3473 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
3474 if (con_id)
3475 snprintf(propname, sizeof(propname), "%s-%s",
3476 con_id, gpio_suffixes[i]);
3477 else
3478 snprintf(propname, sizeof(propname), "%s",
3479 gpio_suffixes[i]);
3480
3481 ret = of_gpio_named_count(dev->of_node, propname);
Andy Shevchenko4033d4a2017-02-20 18:15:47 +02003482 if (ret > 0)
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003483 break;
3484 }
Andy Shevchenko4033d4a2017-02-20 18:15:47 +02003485 return ret ? ret : -ENOENT;
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003486}
3487
3488static int platform_gpio_count(struct device *dev, const char *con_id)
3489{
3490 struct gpiod_lookup_table *table;
3491 struct gpiod_lookup *p;
3492 unsigned int count = 0;
3493
3494 table = gpiod_find_lookup_table(dev);
3495 if (!table)
3496 return -ENOENT;
3497
3498 for (p = &table->table[0]; p->chip_label; p++) {
3499 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3500 (!con_id && !p->con_id))
3501 count++;
3502 }
3503 if (!count)
3504 return -ENOENT;
3505
3506 return count;
3507}
3508
3509/**
3510 * gpiod_count - return the number of GPIOs associated with a device / function
3511 * or -ENOENT if no GPIO has been assigned to the requested function
3512 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3513 * @con_id: function within the GPIO consumer
3514 */
3515int gpiod_count(struct device *dev, const char *con_id)
3516{
3517 int count = -ENOENT;
3518
3519 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
3520 count = dt_gpio_count(dev, con_id);
3521 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
3522 count = acpi_gpio_count(dev, con_id);
3523
3524 if (count < 0)
3525 count = platform_gpio_count(dev, con_id);
3526
3527 return count;
3528}
3529EXPORT_SYMBOL_GPL(gpiod_count);
3530
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003531/**
Thierry Reding08791622014-04-25 16:54:22 +02003532 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09003533 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003534 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003535 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003536 *
3537 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003538 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07003539 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003540 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003541struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003542 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003543{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003544 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003545}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003546EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003547
3548/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02003549 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
3550 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3551 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003552 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02003553 *
3554 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
3555 * the requested function it will return NULL. This is convenient for drivers
3556 * that need to handle optional GPIOs.
3557 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003558struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003559 const char *con_id,
3560 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003561{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003562 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003563}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003564EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003565
Benoit Parrotf625d462015-02-02 11:44:44 -06003566
3567/**
3568 * gpiod_configure_flags - helper function to configure a given GPIO
3569 * @desc: gpio whose value will be assigned
3570 * @con_id: function within the GPIO consumer
Johan Hovold85b03b32016-07-03 18:32:05 +02003571 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3572 * of_get_gpio_hog()
Benoit Parrotf625d462015-02-02 11:44:44 -06003573 * @dflags: gpiod_flags - optional GPIO initialization flags
3574 *
3575 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
3576 * requested function and/or index, or another IS_ERR() code if an error
3577 * occurred while trying to acquire the GPIO.
3578 */
Andy Shevchenkoc29fd9e2017-05-23 20:03:16 +03003579int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Johan Hovold85b03b32016-07-03 18:32:05 +02003580 unsigned long lflags, enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06003581{
3582 int status;
3583
Johan Hovold85b03b32016-07-03 18:32:05 +02003584 if (lflags & GPIO_ACTIVE_LOW)
3585 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
Linus Walleijf926dfc2017-09-10 19:26:22 +02003586
Johan Hovold85b03b32016-07-03 18:32:05 +02003587 if (lflags & GPIO_OPEN_DRAIN)
3588 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
Linus Walleijf926dfc2017-09-10 19:26:22 +02003589 else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) {
3590 /*
3591 * This enforces open drain mode from the consumer side.
3592 * This is necessary for some busses like I2C, but the lookup
3593 * should *REALLY* have specified them as open drain in the
3594 * first place, so print a little warning here.
3595 */
3596 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3597 gpiod_warn(desc,
3598 "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
3599 }
3600
Johan Hovold85b03b32016-07-03 18:32:05 +02003601 if (lflags & GPIO_OPEN_SOURCE)
3602 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303603
3604 status = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
3605 if (status < 0)
3606 return status;
Johan Hovold85b03b32016-07-03 18:32:05 +02003607
Benoit Parrotf625d462015-02-02 11:44:44 -06003608 /* No particular flag request, return here... */
3609 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
3610 pr_debug("no flags found for %s\n", con_id);
3611 return 0;
3612 }
3613
3614 /* Process flags */
3615 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
3616 status = gpiod_direction_output(desc,
Linus Walleijad177312016-11-13 23:02:44 +01003617 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
Benoit Parrotf625d462015-02-02 11:44:44 -06003618 else
3619 status = gpiod_direction_input(desc);
3620
3621 return status;
3622}
3623
Thierry Reding29a1f2332014-04-25 17:10:06 +02003624/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003625 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02003626 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003627 * @con_id: function within the GPIO consumer
3628 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003629 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003630 *
3631 * This variant of gpiod_get() allows to access GPIOs other than the first
3632 * defined one for functions that define several GPIOs.
3633 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003634 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
3635 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07003636 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003637 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003638struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003639 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003640 unsigned int idx,
3641 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003642{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003643 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003644 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003645 enum gpio_lookup_flags lookupflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003646
3647 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
3648
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003649 if (dev) {
3650 /* Using device tree? */
3651 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
3652 dev_dbg(dev, "using device tree for GPIO lookup\n");
3653 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
3654 } else if (ACPI_COMPANION(dev)) {
3655 dev_dbg(dev, "using ACPI for GPIO lookup\n");
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +03003656 desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags);
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003657 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003658 }
3659
3660 /*
3661 * Either we are not using DT or ACPI, or their lookup did not return
3662 * a result. In that case, use platform lookup as a fallback.
3663 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003664 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04003665 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003666 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003667 }
3668
3669 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02003670 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003671 return desc;
3672 }
3673
3674 status = gpiod_request(desc, con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003675 if (status < 0)
3676 return ERR_PTR(status);
3677
Johan Hovold85b03b32016-07-03 18:32:05 +02003678 status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003679 if (status < 0) {
3680 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
3681 gpiod_put(desc);
3682 return ERR_PTR(status);
3683 }
3684
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003685 return desc;
3686}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003687EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003688
3689/**
Mika Westerberg40b73182014-10-21 13:33:59 +02003690 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
3691 * @fwnode: handle of the firmware node
3692 * @propname: name of the firmware property representing the GPIO
Boris Brezillon537b94d2017-02-02 14:53:11 +01003693 * @index: index of the GPIO to obtain in the consumer
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003694 * @dflags: GPIO initialization flags
Thierry Reding950d55f52017-07-24 16:57:22 +02003695 * @label: label to attach to the requested GPIO
Mika Westerberg40b73182014-10-21 13:33:59 +02003696 *
3697 * This function can be used for drivers that get their configuration
3698 * from firmware.
3699 *
3700 * Function properly finds the corresponding GPIO using whatever is the
3701 * underlying firmware interface and then makes sure that the GPIO
3702 * descriptor is requested before it is returned to the caller.
3703 *
Thierry Reding950d55f52017-07-24 16:57:22 +02003704 * Returns:
Andy Shevchenkoff213782017-02-28 17:03:12 +02003705 * On successful request the GPIO pin is configured in accordance with
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003706 * provided @dflags.
3707 *
Mika Westerberg40b73182014-10-21 13:33:59 +02003708 * In case of error an ERR_PTR() is returned.
3709 */
3710struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
Boris Brezillon537b94d2017-02-02 14:53:11 +01003711 const char *propname, int index,
Alexander Steinb2987d72017-01-12 17:39:24 +01003712 enum gpiod_flags dflags,
3713 const char *label)
Mika Westerberg40b73182014-10-21 13:33:59 +02003714{
3715 struct gpio_desc *desc = ERR_PTR(-ENODEV);
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003716 unsigned long lflags = 0;
Mika Westerberg40b73182014-10-21 13:33:59 +02003717 bool active_low = false;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003718 bool single_ended = false;
Laxman Dewangan4c0facd2017-04-06 19:05:52 +05303719 bool open_drain = false;
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303720 bool transitory = false;
Mika Westerberg40b73182014-10-21 13:33:59 +02003721 int ret;
3722
3723 if (!fwnode)
3724 return ERR_PTR(-EINVAL);
3725
3726 if (is_of_node(fwnode)) {
3727 enum of_gpio_flags flags;
3728
Boris Brezillon537b94d2017-02-02 14:53:11 +01003729 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname,
3730 index, &flags);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003731 if (!IS_ERR(desc)) {
Mika Westerberg40b73182014-10-21 13:33:59 +02003732 active_low = flags & OF_GPIO_ACTIVE_LOW;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003733 single_ended = flags & OF_GPIO_SINGLE_ENDED;
Laxman Dewangan4c0facd2017-04-06 19:05:52 +05303734 open_drain = flags & OF_GPIO_OPEN_DRAIN;
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303735 transitory = flags & OF_GPIO_TRANSITORY;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003736 }
Mika Westerberg40b73182014-10-21 13:33:59 +02003737 } else if (is_acpi_node(fwnode)) {
3738 struct acpi_gpio_info info;
3739
Boris Brezillon537b94d2017-02-02 14:53:11 +01003740 desc = acpi_node_get_gpiod(fwnode, propname, index, &info);
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +03003741 if (!IS_ERR(desc)) {
Christophe RICARD52044722015-12-23 23:25:34 +01003742 active_low = info.polarity == GPIO_ACTIVE_LOW;
Andy Shevchenko5c34b6c2017-11-10 15:40:31 +02003743 acpi_gpio_update_gpiod_flags(&dflags, &info);
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +03003744 }
Mika Westerberg40b73182014-10-21 13:33:59 +02003745 }
3746
3747 if (IS_ERR(desc))
3748 return desc;
3749
Alexander Steinb2987d72017-01-12 17:39:24 +01003750 ret = gpiod_request(desc, label);
Johan Hovold85b03b32016-07-03 18:32:05 +02003751 if (ret)
3752 return ERR_PTR(ret);
3753
Mika Westerberg40b73182014-10-21 13:33:59 +02003754 if (active_low)
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003755 lflags |= GPIO_ACTIVE_LOW;
Mika Westerberg40b73182014-10-21 13:33:59 +02003756
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003757 if (single_ended) {
Laxman Dewangan4c0facd2017-04-06 19:05:52 +05303758 if (open_drain)
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003759 lflags |= GPIO_OPEN_DRAIN;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003760 else
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003761 lflags |= GPIO_OPEN_SOURCE;
3762 }
3763
Andrew Jefferye10f72b2017-11-30 14:25:24 +10303764 if (transitory)
3765 lflags |= GPIO_TRANSITORY;
3766
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003767 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
3768 if (ret < 0) {
3769 gpiod_put(desc);
3770 return ERR_PTR(ret);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003771 }
3772
Mika Westerberg40b73182014-10-21 13:33:59 +02003773 return desc;
3774}
3775EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
3776
3777/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02003778 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
3779 * function
3780 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3781 * @con_id: function within the GPIO consumer
3782 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003783 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02003784 *
3785 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
3786 * specified index was assigned to the requested function it will return NULL.
3787 * This is convenient for drivers that need to handle optional GPIOs.
3788 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003789struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02003790 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003791 unsigned int index,
3792 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003793{
3794 struct gpio_desc *desc;
3795
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003796 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003797 if (IS_ERR(desc)) {
3798 if (PTR_ERR(desc) == -ENOENT)
3799 return NULL;
3800 }
3801
3802 return desc;
3803}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003804EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003805
3806/**
Benoit Parrotf625d462015-02-02 11:44:44 -06003807 * gpiod_hog - Hog the specified GPIO desc given the provided flags
3808 * @desc: gpio whose value will be assigned
3809 * @name: gpio line name
3810 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3811 * of_get_gpio_hog()
3812 * @dflags: gpiod_flags - optional GPIO initialization flags
3813 */
3814int gpiod_hog(struct gpio_desc *desc, const char *name,
3815 unsigned long lflags, enum gpiod_flags dflags)
3816{
3817 struct gpio_chip *chip;
3818 struct gpio_desc *local_desc;
3819 int hwnum;
3820 int status;
3821
3822 chip = gpiod_to_chip(desc);
3823 hwnum = gpio_chip_hwgpio(desc);
3824
3825 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
3826 if (IS_ERR(local_desc)) {
Laxman Dewanganc31a5712016-03-11 19:13:21 +05303827 status = PTR_ERR(local_desc);
3828 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
3829 name, chip->label, hwnum, status);
3830 return status;
Benoit Parrotf625d462015-02-02 11:44:44 -06003831 }
3832
Johan Hovold85b03b32016-07-03 18:32:05 +02003833 status = gpiod_configure_flags(desc, name, lflags, dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -06003834 if (status < 0) {
Laxman Dewanganc31a5712016-03-11 19:13:21 +05303835 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed, %d\n",
3836 name, chip->label, hwnum, status);
Benoit Parrotf625d462015-02-02 11:44:44 -06003837 gpiochip_free_own_desc(desc);
3838 return status;
3839 }
3840
3841 /* Mark GPIO as hogged so it can be identified and removed later */
3842 set_bit(FLAG_IS_HOGGED, &desc->flags);
3843
3844 pr_info("GPIO line %d (%s) hogged as %s%s\n",
3845 desc_to_gpio(desc), name,
3846 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
3847 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
3848 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
3849
3850 return 0;
3851}
3852
3853/**
3854 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
3855 * @chip: gpio chip to act on
3856 *
3857 * This is only used by of_gpiochip_remove to free hogged gpios
3858 */
3859static void gpiochip_free_hogs(struct gpio_chip *chip)
3860{
3861 int id;
3862
3863 for (id = 0; id < chip->ngpio; id++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01003864 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
3865 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
Benoit Parrotf625d462015-02-02 11:44:44 -06003866 }
3867}
3868
3869/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003870 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
3871 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3872 * @con_id: function within the GPIO consumer
3873 * @flags: optional GPIO initialization flags
3874 *
3875 * This function acquires all the GPIOs defined under a given function.
3876 *
3877 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
3878 * no GPIO has been assigned to the requested function, or another IS_ERR()
3879 * code if an error occurred while trying to acquire the GPIOs.
3880 */
3881struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
3882 const char *con_id,
3883 enum gpiod_flags flags)
3884{
3885 struct gpio_desc *desc;
3886 struct gpio_descs *descs;
3887 int count;
3888
3889 count = gpiod_count(dev, con_id);
3890 if (count < 0)
3891 return ERR_PTR(count);
3892
3893 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
3894 GFP_KERNEL);
3895 if (!descs)
3896 return ERR_PTR(-ENOMEM);
3897
3898 for (descs->ndescs = 0; descs->ndescs < count; ) {
3899 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
3900 if (IS_ERR(desc)) {
3901 gpiod_put_array(descs);
3902 return ERR_CAST(desc);
3903 }
3904 descs->desc[descs->ndescs] = desc;
3905 descs->ndescs++;
3906 }
3907 return descs;
3908}
3909EXPORT_SYMBOL_GPL(gpiod_get_array);
3910
3911/**
3912 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
3913 * function
3914 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3915 * @con_id: function within the GPIO consumer
3916 * @flags: optional GPIO initialization flags
3917 *
3918 * This is equivalent to gpiod_get_array(), except that when no GPIO was
3919 * assigned to the requested function it will return NULL.
3920 */
3921struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
3922 const char *con_id,
3923 enum gpiod_flags flags)
3924{
3925 struct gpio_descs *descs;
3926
3927 descs = gpiod_get_array(dev, con_id, flags);
3928 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
3929 return NULL;
3930
3931 return descs;
3932}
3933EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
3934
3935/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003936 * gpiod_put - dispose of a GPIO descriptor
3937 * @desc: GPIO descriptor to dispose of
3938 *
3939 * No descriptor can be used after gpiod_put() has been called on it.
3940 */
3941void gpiod_put(struct gpio_desc *desc)
3942{
3943 gpiod_free(desc);
3944}
3945EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08003946
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003947/**
3948 * gpiod_put_array - dispose of multiple GPIO descriptors
3949 * @descs: struct gpio_descs containing an array of descriptors
3950 */
3951void gpiod_put_array(struct gpio_descs *descs)
3952{
3953 unsigned int i;
3954
3955 for (i = 0; i < descs->ndescs; i++)
3956 gpiod_put(descs->desc[i]);
3957
3958 kfree(descs);
3959}
3960EXPORT_SYMBOL_GPL(gpiod_put_array);
3961
Linus Walleij3c702e92015-10-21 15:29:53 +02003962static int __init gpiolib_dev_init(void)
3963{
3964 int ret;
3965
3966 /* Register GPIO sysfs bus */
3967 ret = bus_register(&gpio_bus_type);
3968 if (ret < 0) {
3969 pr_err("gpiolib: could not register GPIO bus type\n");
3970 return ret;
3971 }
3972
3973 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
3974 if (ret < 0) {
3975 pr_err("gpiolib: failed to allocate char dev region\n");
3976 bus_unregister(&gpio_bus_type);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07003977 } else {
3978 gpiolib_initialized = true;
3979 gpiochip_setup_devs();
Linus Walleij3c702e92015-10-21 15:29:53 +02003980 }
3981 return ret;
3982}
3983core_initcall(gpiolib_dev_init);
3984
David Brownelld2876d02008-02-04 22:28:20 -08003985#ifdef CONFIG_DEBUG_FS
3986
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003987static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
David Brownelld2876d02008-02-04 22:28:20 -08003988{
3989 unsigned i;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003990 struct gpio_chip *chip = gdev->chip;
3991 unsigned gpio = gdev->base;
3992 struct gpio_desc *gdesc = &gdev->descs[0];
David Brownelld2876d02008-02-04 22:28:20 -08003993 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02003994 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08003995
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003996 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02003997 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
3998 if (gdesc->name) {
3999 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
4000 gpio, gdesc->name);
4001 }
David Brownelld2876d02008-02-04 22:28:20 -08004002 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02004003 }
David Brownelld2876d02008-02-04 22:28:20 -08004004
Alexandre Courbot372e7222013-02-03 01:29:29 +09004005 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08004006 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02004007 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Markus Pargmannced433e2015-08-14 16:11:02 +02004008 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
4009 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08004010 is_out ? "out" : "in ",
4011 chip->get
4012 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02004013 : "? ",
4014 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08004015 seq_printf(s, "\n");
4016 }
4017}
4018
Thierry Redingf9c4a312012-04-12 13:26:01 +02004019static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08004020{
Grant Likely362432a2013-02-09 09:41:49 +00004021 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02004022 struct gpio_device *gdev = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09004023 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02004024
4025 s->private = "";
4026
Grant Likely362432a2013-02-09 09:41:49 +00004027 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02004028 list_for_each_entry(gdev, &gpio_devices, list)
Grant Likely362432a2013-02-09 09:41:49 +00004029 if (index-- == 0) {
4030 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02004031 return gdev;
Grant Likely362432a2013-02-09 09:41:49 +00004032 }
4033 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09004034
4035 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02004036}
4037
4038static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
4039{
Grant Likely362432a2013-02-09 09:41:49 +00004040 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02004041 struct gpio_device *gdev = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02004042 void *ret = NULL;
4043
Grant Likely362432a2013-02-09 09:41:49 +00004044 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02004045 if (list_is_last(&gdev->list, &gpio_devices))
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09004046 ret = NULL;
4047 else
Linus Walleijff2b1352015-10-20 11:10:38 +02004048 ret = list_entry(gdev->list.next, struct gpio_device, list);
Grant Likely362432a2013-02-09 09:41:49 +00004049 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02004050
4051 s->private = "\n";
4052 ++*pos;
4053
4054 return ret;
4055}
4056
4057static void gpiolib_seq_stop(struct seq_file *s, void *v)
4058{
4059}
4060
4061static int gpiolib_seq_show(struct seq_file *s, void *v)
4062{
Linus Walleijff2b1352015-10-20 11:10:38 +02004063 struct gpio_device *gdev = v;
4064 struct gpio_chip *chip = gdev->chip;
4065 struct device *parent;
Thierry Redingf9c4a312012-04-12 13:26:01 +02004066
Linus Walleijff2b1352015-10-20 11:10:38 +02004067 if (!chip) {
4068 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
4069 dev_name(&gdev->dev));
4070 return 0;
4071 }
4072
4073 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
4074 dev_name(&gdev->dev),
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004075 gdev->base, gdev->base + gdev->ngpio - 1);
Linus Walleijff2b1352015-10-20 11:10:38 +02004076 parent = chip->parent;
4077 if (parent)
4078 seq_printf(s, ", parent: %s/%s",
4079 parent->bus ? parent->bus->name : "no-bus",
4080 dev_name(parent));
Thierry Redingf9c4a312012-04-12 13:26:01 +02004081 if (chip->label)
4082 seq_printf(s, ", %s", chip->label);
4083 if (chip->can_sleep)
4084 seq_printf(s, ", can sleep");
4085 seq_printf(s, ":\n");
4086
4087 if (chip->dbg_show)
4088 chip->dbg_show(s, chip);
4089 else
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004090 gpiolib_dbg_show(s, gdev);
Thierry Redingf9c4a312012-04-12 13:26:01 +02004091
David Brownelld2876d02008-02-04 22:28:20 -08004092 return 0;
4093}
4094
Thierry Redingf9c4a312012-04-12 13:26:01 +02004095static const struct seq_operations gpiolib_seq_ops = {
4096 .start = gpiolib_seq_start,
4097 .next = gpiolib_seq_next,
4098 .stop = gpiolib_seq_stop,
4099 .show = gpiolib_seq_show,
4100};
4101
David Brownelld2876d02008-02-04 22:28:20 -08004102static int gpiolib_open(struct inode *inode, struct file *file)
4103{
Thierry Redingf9c4a312012-04-12 13:26:01 +02004104 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08004105}
4106
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004107static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02004108 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08004109 .open = gpiolib_open,
4110 .read = seq_read,
4111 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02004112 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08004113};
4114
4115static int __init gpiolib_debugfs_init(void)
4116{
4117 /* /sys/kernel/debug/gpio */
4118 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
4119 NULL, NULL, &gpiolib_operations);
4120 return 0;
4121}
4122subsys_initcall(gpiolib_debugfs_init);
4123
4124#endif /* DEBUG_FS */