blob: bdcdcdefdb71bec95c4dd797ff1c35fb788f6b9f [file] [log] [blame]
Andy Shevchenko923a6542017-05-25 16:08:38 +03001#include <linux/bitmap.h>
David Brownelld2876d02008-02-04 22:28:20 -08002#include <linux/kernel.h>
3#include <linux/module.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -07004#include <linux/interrupt.h>
David Brownelld2876d02008-02-04 22:28:20 -08005#include <linux/irq.h>
6#include <linux/spinlock.h>
Alexandre Courbot1a989d02013-02-03 01:29:24 +09007#include <linux/list.h>
David Brownelld8f388d82008-07-25 01:46:07 -07008#include <linux/device.h>
9#include <linux/err.h>
10#include <linux/debugfs.h>
11#include <linux/seq_file.h>
12#include <linux/gpio.h>
Anton Vorontsov391c9702010-06-08 07:48:17 -060013#include <linux/of_gpio.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -070014#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Rafael J. Wysocki7b199812013-11-11 22:41:56 +010016#include <linux/acpi.h>
Alexandre Courbot53e7cac2013-11-16 21:44:52 +090017#include <linux/gpio/driver.h>
Linus Walleij0a6d3152014-07-24 20:08:55 +020018#include <linux/gpio/machine.h>
Jonas Gorskic771c2f2015-10-11 17:34:15 +020019#include <linux/pinctrl/consumer.h>
Linus Walleij3c702e92015-10-21 15:29:53 +020020#include <linux/cdev.h>
21#include <linux/fs.h>
22#include <linux/uaccess.h>
Linus Walleij8b92e172016-05-27 14:24:04 +020023#include <linux/compat.h>
Linus Walleijd7c51b42016-04-26 10:35:29 +020024#include <linux/anon_inodes.h>
Lars-Peter Clausen953b9562016-10-24 13:59:15 +020025#include <linux/file.h>
Linus Walleij61f922d2016-06-02 11:30:15 +020026#include <linux/kfifo.h>
27#include <linux/poll.h>
28#include <linux/timekeeping.h>
Linus Walleij3c702e92015-10-21 15:29:53 +020029#include <uapi/linux/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080030
Mika Westerberg664e3e52014-01-08 12:40:54 +020031#include "gpiolib.h"
32
Uwe Kleine-König3f397c212011-05-20 00:40:19 -060033#define CREATE_TRACE_POINTS
34#include <trace/events/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080035
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070036/* Implementation infrastructure for GPIO interfaces.
David Brownelld2876d02008-02-04 22:28:20 -080037 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070038 * The GPIO programming interface allows for inlining speed-critical
39 * get/set operations for common cases, so that access to SOC-integrated
40 * GPIOs can sometimes cost only an instruction or two per bit.
David Brownelld2876d02008-02-04 22:28:20 -080041 */
42
43
44/* When debugging, extend minimal trust to callers and platform code.
45 * Also emit diagnostic messages that may help initial bringup, when
46 * board setup or driver bugs are most common.
47 *
48 * Otherwise, minimize overhead in what may be bitbanging codepaths.
49 */
50#ifdef DEBUG
51#define extra_checks 1
52#else
53#define extra_checks 0
54#endif
55
Linus Walleijff2b1352015-10-20 11:10:38 +020056/* Device and char device-related information */
57static DEFINE_IDA(gpio_ida);
Linus Walleij3c702e92015-10-21 15:29:53 +020058static dev_t gpio_devt;
59#define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
60static struct bus_type gpio_bus_type = {
61 .name = "gpio",
62};
Linus Walleijff2b1352015-10-20 11:10:38 +020063
David Brownelld2876d02008-02-04 22:28:20 -080064/* gpio_lock prevents conflicts during gpio_desc[] table updates.
65 * While any GPIO is requested, its gpio_chip is not removable;
66 * each GPIO's "requested" flag serves as a lock and refcount.
67 */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090068DEFINE_SPINLOCK(gpio_lock);
David Brownelld2876d02008-02-04 22:28:20 -080069
Alexandre Courbotbae48da2013-10-17 10:21:38 -070070static DEFINE_MUTEX(gpio_lookup_lock);
71static LIST_HEAD(gpio_lookup_list);
Linus Walleijff2b1352015-10-20 11:10:38 +020072LIST_HEAD(gpio_devices);
Johan Hovold6d867502015-05-04 17:23:25 +020073
74static void gpiochip_free_hogs(struct gpio_chip *chip);
Thierry Reding959bc7b2017-11-07 19:15:59 +010075static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
76 struct lock_class_key *key);
Johan Hovold6d867502015-05-04 17:23:25 +020077static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
Mika Westerberg79b804c2016-09-20 15:15:21 +030078static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip);
79static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip);
Johan Hovold6d867502015-05-04 17:23:25 +020080
Guenter Roeck159f3cd2016-03-31 08:11:30 -070081static bool gpiolib_initialized;
Johan Hovold6d867502015-05-04 17:23:25 +020082
David Brownelld2876d02008-02-04 22:28:20 -080083static inline void desc_set_label(struct gpio_desc *d, const char *label)
84{
David Brownelld2876d02008-02-04 22:28:20 -080085 d->label = label;
David Brownelld2876d02008-02-04 22:28:20 -080086}
87
Alexandre Courbot372e7222013-02-03 01:29:29 +090088/**
Thierry Reding950d55f52017-07-24 16:57:22 +020089 * gpio_to_desc - Convert a GPIO number to its descriptor
90 * @gpio: global GPIO number
91 *
92 * Returns:
93 * The GPIO descriptor associated with the given GPIO, or %NULL if no GPIO
94 * with the given number exists in the system.
Alexandre Courbot372e7222013-02-03 01:29:29 +090095 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070096struct gpio_desc *gpio_to_desc(unsigned gpio)
Alexandre Courbot372e7222013-02-03 01:29:29 +090097{
Linus Walleijff2b1352015-10-20 11:10:38 +020098 struct gpio_device *gdev;
Alexandre Courbot14e85c02014-11-19 16:51:27 +090099 unsigned long flags;
100
101 spin_lock_irqsave(&gpio_lock, flags);
102
Linus Walleijff2b1352015-10-20 11:10:38 +0200103 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100104 if (gdev->base <= gpio &&
105 gdev->base + gdev->ngpio > gpio) {
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900106 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100107 return &gdev->descs[gpio - gdev->base];
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900108 }
109 }
110
111 spin_unlock_irqrestore(&gpio_lock, flags);
112
Alexandre Courbot0e9a5ed2014-12-02 23:15:05 +0900113 if (!gpio_is_valid(gpio))
114 WARN(1, "invalid GPIO %d\n", gpio);
115
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900116 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900117}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700118EXPORT_SYMBOL_GPL(gpio_to_desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900119
120/**
Thierry Reding950d55f52017-07-24 16:57:22 +0200121 * gpiochip_get_desc - get the GPIO descriptor corresponding to the given
122 * hardware number for this chip
123 * @chip: GPIO chip
124 * @hwnum: hardware number of the GPIO for this chip
125 *
126 * Returns:
127 * A pointer to the GPIO descriptor or %ERR_PTR(-EINVAL) if no GPIO exists
128 * in the given chip for the specified hardware number.
Linus Walleijd468bf92013-09-24 11:54:38 +0200129 */
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900130struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
131 u16 hwnum)
Linus Walleijd468bf92013-09-24 11:54:38 +0200132{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100133 struct gpio_device *gdev = chip->gpiodev;
134
135 if (hwnum >= gdev->ngpio)
Alexandre Courbotb7d0a282013-12-03 12:31:11 +0900136 return ERR_PTR(-EINVAL);
Linus Walleijd468bf92013-09-24 11:54:38 +0200137
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100138 return &gdev->descs[hwnum];
Linus Walleijd468bf92013-09-24 11:54:38 +0200139}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900140
141/**
Thierry Reding950d55f52017-07-24 16:57:22 +0200142 * desc_to_gpio - convert a GPIO descriptor to the integer namespace
143 * @desc: GPIO descriptor
144 *
Alexandre Courbot372e7222013-02-03 01:29:29 +0900145 * This should disappear in the future but is needed since we still
Thierry Reding950d55f52017-07-24 16:57:22 +0200146 * use GPIO numbers for error messages and sysfs nodes.
147 *
148 * Returns:
149 * The global GPIO number for the GPIO specified by its descriptor.
Alexandre Courbot372e7222013-02-03 01:29:29 +0900150 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700151int desc_to_gpio(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900152{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100153 return desc->gdev->base + (desc - &desc->gdev->descs[0]);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900154}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700155EXPORT_SYMBOL_GPL(desc_to_gpio);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900156
157
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700158/**
159 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
160 * @desc: descriptor to return the chip of
161 */
162struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900163{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100164 if (!desc || !desc->gdev || !desc->gdev->chip)
165 return NULL;
166 return desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900167}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700168EXPORT_SYMBOL_GPL(gpiod_to_chip);
David Brownelld2876d02008-02-04 22:28:20 -0800169
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700170/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
171static int gpiochip_find_base(int ngpio)
172{
Linus Walleijff2b1352015-10-20 11:10:38 +0200173 struct gpio_device *gdev;
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900174 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700175
Linus Walleijff2b1352015-10-20 11:10:38 +0200176 list_for_each_entry_reverse(gdev, &gpio_devices, list) {
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900177 /* found a free space? */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100178 if (gdev->base + gdev->ngpio <= base)
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900179 break;
180 else
181 /* nope, check the space right before the chip */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100182 base = gdev->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700183 }
184
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900185 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700186 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900187 return base;
188 } else {
189 pr_err("%s: cannot find free range\n", __func__);
190 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700191 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700192}
193
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700194/**
195 * gpiod_get_direction - return the current direction of a GPIO
196 * @desc: GPIO to get the direction of
197 *
198 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
199 *
200 * This function may sleep if gpiod_cansleep() is true.
201 */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900202int gpiod_get_direction(struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300203{
204 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900205 unsigned offset;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300206 int status = -EINVAL;
207
Alexandre Courbot372e7222013-02-03 01:29:29 +0900208 chip = gpiod_to_chip(desc);
209 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300210
211 if (!chip->get_direction)
212 return status;
213
Alexandre Courbot372e7222013-02-03 01:29:29 +0900214 status = chip->get_direction(chip, offset);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300215 if (status > 0) {
216 /* GPIOF_DIR_IN, or other positive */
217 status = 1;
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900218 clear_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300219 }
220 if (status == 0) {
221 /* GPIOF_DIR_OUT */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900222 set_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300223 }
224 return status;
225}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700226EXPORT_SYMBOL_GPL(gpiod_get_direction);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300227
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900228/*
229 * Add a new chip to the global chips list, keeping the list of chips sorted
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800230 * by range(means [base, base + ngpio - 1]) order.
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900231 *
232 * Return -EBUSY if the new chip overlaps with some other chip's integer
233 * space.
234 */
Linus Walleijff2b1352015-10-20 11:10:38 +0200235static int gpiodev_add_to_list(struct gpio_device *gdev)
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900236{
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800237 struct gpio_device *prev, *next;
Linus Walleijff2b1352015-10-20 11:10:38 +0200238
239 if (list_empty(&gpio_devices)) {
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800240 /* initial entry in list */
Linus Walleijff2b1352015-10-20 11:10:38 +0200241 list_add_tail(&gdev->list, &gpio_devices);
Sudip Mukherjeee28ecca2015-12-27 19:06:50 +0530242 return 0;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900243 }
244
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800245 next = list_entry(gpio_devices.next, struct gpio_device, list);
246 if (gdev->base + gdev->ngpio <= next->base) {
247 /* add before first entry */
248 list_add(&gdev->list, &gpio_devices);
Julien Grossholtz96098df2016-01-07 16:46:45 -0500249 return 0;
250 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900251
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800252 prev = list_entry(gpio_devices.prev, struct gpio_device, list);
253 if (prev->base + prev->ngpio <= gdev->base) {
254 /* add behind last entry */
255 list_add_tail(&gdev->list, &gpio_devices);
256 return 0;
257 }
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800258
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800259 list_for_each_entry_safe(prev, next, &gpio_devices, list) {
260 /* at the end of the list */
261 if (&next->list == &gpio_devices)
262 break;
263
264 /* add between prev and next */
265 if (prev->base + prev->ngpio <= gdev->base
266 && gdev->base + gdev->ngpio <= next->base) {
267 list_add(&gdev->list, &prev->list);
268 return 0;
269 }
270 }
271
272 dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n");
273 return -EBUSY;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900274}
275
Thierry Reding950d55f52017-07-24 16:57:22 +0200276/*
Linus Walleijf881bab02015-09-23 16:20:43 -0700277 * Convert a GPIO name to its descriptor
278 */
279static struct gpio_desc *gpio_name_to_desc(const char * const name)
280{
Linus Walleijff2b1352015-10-20 11:10:38 +0200281 struct gpio_device *gdev;
Linus Walleijf881bab02015-09-23 16:20:43 -0700282 unsigned long flags;
283
284 spin_lock_irqsave(&gpio_lock, flags);
285
Linus Walleijff2b1352015-10-20 11:10:38 +0200286 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijf881bab02015-09-23 16:20:43 -0700287 int i;
288
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100289 for (i = 0; i != gdev->ngpio; ++i) {
290 struct gpio_desc *desc = &gdev->descs[i];
Linus Walleijf881bab02015-09-23 16:20:43 -0700291
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100292 if (!desc->name || !name)
Linus Walleijf881bab02015-09-23 16:20:43 -0700293 continue;
294
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100295 if (!strcmp(desc->name, name)) {
Linus Walleijf881bab02015-09-23 16:20:43 -0700296 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100297 return desc;
Linus Walleijf881bab02015-09-23 16:20:43 -0700298 }
299 }
300 }
301
302 spin_unlock_irqrestore(&gpio_lock, flags);
303
304 return NULL;
305}
306
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200307/*
308 * Takes the names from gc->names and checks if they are all unique. If they
309 * are, they are assigned to their gpio descriptors.
310 *
Bamvor Jian Zhanged379152015-11-14 16:43:20 +0800311 * Warning if one of the names is already used for a different GPIO.
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200312 */
313static int gpiochip_set_desc_names(struct gpio_chip *gc)
314{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100315 struct gpio_device *gdev = gc->gpiodev;
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200316 int i;
317
318 if (!gc->names)
319 return 0;
320
321 /* First check all names if they are unique */
322 for (i = 0; i != gc->ngpio; ++i) {
323 struct gpio_desc *gpio;
324
325 gpio = gpio_name_to_desc(gc->names[i]);
Linus Walleijf881bab02015-09-23 16:20:43 -0700326 if (gpio)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100327 dev_warn(&gdev->dev,
Linus Walleij34ffd852015-10-20 11:31:54 +0200328 "Detected name collision for GPIO name '%s'\n",
Linus Walleijf881bab02015-09-23 16:20:43 -0700329 gc->names[i]);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200330 }
331
332 /* Then add all names to the GPIO descriptors */
333 for (i = 0; i != gc->ngpio; ++i)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100334 gdev->descs[i].name = gc->names[i];
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200335
336 return 0;
337}
338
Linus Walleijd7c51b42016-04-26 10:35:29 +0200339/*
340 * GPIO line handle management
341 */
342
343/**
344 * struct linehandle_state - contains the state of a userspace handle
345 * @gdev: the GPIO device the handle pertains to
346 * @label: consumer label used to tag descriptors
347 * @descs: the GPIO descriptors held by this handle
348 * @numdescs: the number of descriptors held in the descs array
349 */
350struct linehandle_state {
351 struct gpio_device *gdev;
352 const char *label;
353 struct gpio_desc *descs[GPIOHANDLES_MAX];
354 u32 numdescs;
355};
356
Lars-Peter Clausene3e847c2016-10-18 16:54:05 +0200357#define GPIOHANDLE_REQUEST_VALID_FLAGS \
358 (GPIOHANDLE_REQUEST_INPUT | \
359 GPIOHANDLE_REQUEST_OUTPUT | \
360 GPIOHANDLE_REQUEST_ACTIVE_LOW | \
361 GPIOHANDLE_REQUEST_OPEN_DRAIN | \
362 GPIOHANDLE_REQUEST_OPEN_SOURCE)
363
Linus Walleijd7c51b42016-04-26 10:35:29 +0200364static long linehandle_ioctl(struct file *filep, unsigned int cmd,
365 unsigned long arg)
366{
367 struct linehandle_state *lh = filep->private_data;
368 void __user *ip = (void __user *)arg;
369 struct gpiohandle_data ghd;
Lukas Wunnereec1d562017-10-12 12:40:10 +0200370 int vals[GPIOHANDLES_MAX];
Linus Walleijd7c51b42016-04-26 10:35:29 +0200371 int i;
372
373 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
Lukas Wunnereec1d562017-10-12 12:40:10 +0200374 /* TODO: check if descriptors are really input */
375 int ret = gpiod_get_array_value_complex(false,
376 true,
377 lh->numdescs,
378 lh->descs,
379 vals);
380 if (ret)
381 return ret;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200382
Lars-Peter Clausen3eded5d2016-10-18 16:54:02 +0200383 memset(&ghd, 0, sizeof(ghd));
Lukas Wunnereec1d562017-10-12 12:40:10 +0200384 for (i = 0; i < lh->numdescs; i++)
385 ghd.values[i] = vals[i];
Linus Walleijd7c51b42016-04-26 10:35:29 +0200386
387 if (copy_to_user(ip, &ghd, sizeof(ghd)))
388 return -EFAULT;
389
390 return 0;
391 } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
Linus Walleijd7c51b42016-04-26 10:35:29 +0200392 /* TODO: check if descriptors are really output */
393 if (copy_from_user(&ghd, ip, sizeof(ghd)))
394 return -EFAULT;
395
396 /* Clamp all values to [0,1] */
397 for (i = 0; i < lh->numdescs; i++)
398 vals[i] = !!ghd.values[i];
399
400 /* Reuse the array setting function */
401 gpiod_set_array_value_complex(false,
402 true,
403 lh->numdescs,
404 lh->descs,
405 vals);
406 return 0;
407 }
408 return -EINVAL;
409}
410
411#ifdef CONFIG_COMPAT
412static long linehandle_ioctl_compat(struct file *filep, unsigned int cmd,
413 unsigned long arg)
414{
415 return linehandle_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
416}
417#endif
418
419static int linehandle_release(struct inode *inode, struct file *filep)
420{
421 struct linehandle_state *lh = filep->private_data;
422 struct gpio_device *gdev = lh->gdev;
423 int i;
424
425 for (i = 0; i < lh->numdescs; i++)
426 gpiod_free(lh->descs[i]);
427 kfree(lh->label);
428 kfree(lh);
429 put_device(&gdev->dev);
430 return 0;
431}
432
433static const struct file_operations linehandle_fileops = {
434 .release = linehandle_release,
435 .owner = THIS_MODULE,
436 .llseek = noop_llseek,
437 .unlocked_ioctl = linehandle_ioctl,
438#ifdef CONFIG_COMPAT
439 .compat_ioctl = linehandle_ioctl_compat,
440#endif
441};
442
443static int linehandle_create(struct gpio_device *gdev, void __user *ip)
444{
445 struct gpiohandle_request handlereq;
446 struct linehandle_state *lh;
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200447 struct file *file;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200448 int fd, i, ret;
Bartosz Golaszewski418ee8e2017-10-16 11:32:29 +0200449 u32 lflags;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200450
451 if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
452 return -EFAULT;
453 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
454 return -EINVAL;
455
Bartosz Golaszewski418ee8e2017-10-16 11:32:29 +0200456 lflags = handlereq.flags;
457
458 /* Return an error if an unknown flag is set */
459 if (lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
460 return -EINVAL;
461
Bartosz Golaszewski588fc3b2017-11-15 16:47:43 +0100462 /*
463 * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
464 * the hardware actually supports enabling both at the same time the
465 * electrical result would be disastrous.
466 */
467 if ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) &&
468 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
469 return -EINVAL;
470
Bartosz Golaszewski609aaf62017-10-16 11:32:30 +0200471 /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
472 if (!(lflags & GPIOHANDLE_REQUEST_OUTPUT) &&
473 ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
474 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
475 return -EINVAL;
476
Linus Walleijd7c51b42016-04-26 10:35:29 +0200477 lh = kzalloc(sizeof(*lh), GFP_KERNEL);
478 if (!lh)
479 return -ENOMEM;
480 lh->gdev = gdev;
481 get_device(&gdev->dev);
482
483 /* Make sure this is terminated */
484 handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
485 if (strlen(handlereq.consumer_label)) {
486 lh->label = kstrdup(handlereq.consumer_label,
487 GFP_KERNEL);
488 if (!lh->label) {
489 ret = -ENOMEM;
490 goto out_free_lh;
491 }
492 }
493
494 /* Request each GPIO */
495 for (i = 0; i < handlereq.lines; i++) {
496 u32 offset = handlereq.lineoffsets[i];
Linus Walleijd7c51b42016-04-26 10:35:29 +0200497 struct gpio_desc *desc;
498
Lars-Peter Clausene405f9f2016-10-18 16:54:01 +0200499 if (offset >= gdev->ngpio) {
500 ret = -EINVAL;
501 goto out_free_descs;
502 }
503
Linus Walleijd7c51b42016-04-26 10:35:29 +0200504 desc = &gdev->descs[offset];
505 ret = gpiod_request(desc, lh->label);
506 if (ret)
507 goto out_free_descs;
508 lh->descs[i] = desc;
509
510 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
511 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
512 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
513 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
514 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
515 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
516
517 /*
518 * Lines have to be requested explicitly for input
519 * or output, else the line will be treated "as is".
520 */
521 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
522 int val = !!handlereq.default_values[i];
523
524 ret = gpiod_direction_output(desc, val);
525 if (ret)
526 goto out_free_descs;
527 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
528 ret = gpiod_direction_input(desc);
529 if (ret)
530 goto out_free_descs;
531 }
532 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
533 offset);
534 }
Linus Walleije2f608b2016-06-18 10:56:43 +0200535 /* Let i point at the last handle */
536 i--;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200537 lh->numdescs = handlereq.lines;
538
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200539 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200540 if (fd < 0) {
541 ret = fd;
542 goto out_free_descs;
543 }
544
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200545 file = anon_inode_getfile("gpio-linehandle",
546 &linehandle_fileops,
547 lh,
548 O_RDONLY | O_CLOEXEC);
549 if (IS_ERR(file)) {
550 ret = PTR_ERR(file);
551 goto out_put_unused_fd;
552 }
553
Linus Walleijd7c51b42016-04-26 10:35:29 +0200554 handlereq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200555 if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200556 /*
557 * fput() will trigger the release() callback, so do not go onto
558 * the regular error cleanup path here.
559 */
560 fput(file);
561 put_unused_fd(fd);
562 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +0200563 }
Linus Walleijd7c51b42016-04-26 10:35:29 +0200564
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200565 fd_install(fd, file);
566
Linus Walleijd7c51b42016-04-26 10:35:29 +0200567 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
568 lh->numdescs);
569
570 return 0;
571
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200572out_put_unused_fd:
573 put_unused_fd(fd);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200574out_free_descs:
575 for (; i >= 0; i--)
576 gpiod_free(lh->descs[i]);
577 kfree(lh->label);
578out_free_lh:
579 kfree(lh);
580 put_device(&gdev->dev);
581 return ret;
582}
583
Linus Walleij61f922d2016-06-02 11:30:15 +0200584/*
585 * GPIO line event management
586 */
587
588/**
589 * struct lineevent_state - contains the state of a userspace event
590 * @gdev: the GPIO device the event pertains to
591 * @label: consumer label used to tag descriptors
592 * @desc: the GPIO descriptor held by this event
593 * @eflags: the event flags this line was requested with
594 * @irq: the interrupt that trigger in response to events on this GPIO
595 * @wait: wait queue that handles blocking reads of events
596 * @events: KFIFO for the GPIO events
597 * @read_lock: mutex lock to protect reads from colliding with adding
598 * new events to the FIFO
599 */
600struct lineevent_state {
601 struct gpio_device *gdev;
602 const char *label;
603 struct gpio_desc *desc;
604 u32 eflags;
605 int irq;
606 wait_queue_head_t wait;
607 DECLARE_KFIFO(events, struct gpioevent_data, 16);
608 struct mutex read_lock;
609};
610
Lars-Peter Clausenac7dbb92016-10-18 16:54:06 +0200611#define GPIOEVENT_REQUEST_VALID_FLAGS \
612 (GPIOEVENT_REQUEST_RISING_EDGE | \
613 GPIOEVENT_REQUEST_FALLING_EDGE)
614
Linus Walleij61f922d2016-06-02 11:30:15 +0200615static unsigned int lineevent_poll(struct file *filep,
616 struct poll_table_struct *wait)
617{
618 struct lineevent_state *le = filep->private_data;
619 unsigned int events = 0;
620
621 poll_wait(filep, &le->wait, wait);
622
623 if (!kfifo_is_empty(&le->events))
624 events = POLLIN | POLLRDNORM;
625
626 return events;
627}
628
629
630static ssize_t lineevent_read(struct file *filep,
631 char __user *buf,
632 size_t count,
633 loff_t *f_ps)
634{
635 struct lineevent_state *le = filep->private_data;
636 unsigned int copied;
637 int ret;
638
639 if (count < sizeof(struct gpioevent_data))
640 return -EINVAL;
641
642 do {
643 if (kfifo_is_empty(&le->events)) {
644 if (filep->f_flags & O_NONBLOCK)
645 return -EAGAIN;
646
647 ret = wait_event_interruptible(le->wait,
648 !kfifo_is_empty(&le->events));
649 if (ret)
650 return ret;
651 }
652
653 if (mutex_lock_interruptible(&le->read_lock))
654 return -ERESTARTSYS;
655 ret = kfifo_to_user(&le->events, buf, count, &copied);
656 mutex_unlock(&le->read_lock);
657
658 if (ret)
659 return ret;
660
661 /*
662 * If we couldn't read anything from the fifo (a different
663 * thread might have been faster) we either return -EAGAIN if
664 * the file descriptor is non-blocking, otherwise we go back to
665 * sleep and wait for more data to arrive.
666 */
667 if (copied == 0 && (filep->f_flags & O_NONBLOCK))
668 return -EAGAIN;
669
670 } while (copied == 0);
671
672 return copied;
673}
674
675static int lineevent_release(struct inode *inode, struct file *filep)
676{
677 struct lineevent_state *le = filep->private_data;
678 struct gpio_device *gdev = le->gdev;
679
680 free_irq(le->irq, le);
681 gpiod_free(le->desc);
682 kfree(le->label);
683 kfree(le);
684 put_device(&gdev->dev);
685 return 0;
686}
687
688static long lineevent_ioctl(struct file *filep, unsigned int cmd,
689 unsigned long arg)
690{
691 struct lineevent_state *le = filep->private_data;
692 void __user *ip = (void __user *)arg;
693 struct gpiohandle_data ghd;
694
695 /*
696 * We can get the value for an event line but not set it,
697 * because it is input by definition.
698 */
699 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
700 int val;
701
Lars-Peter Clausend82aa4a2016-10-18 16:54:04 +0200702 memset(&ghd, 0, sizeof(ghd));
703
Linus Walleij61f922d2016-06-02 11:30:15 +0200704 val = gpiod_get_value_cansleep(le->desc);
705 if (val < 0)
706 return val;
707 ghd.values[0] = val;
708
709 if (copy_to_user(ip, &ghd, sizeof(ghd)))
710 return -EFAULT;
711
712 return 0;
713 }
714 return -EINVAL;
715}
716
717#ifdef CONFIG_COMPAT
718static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd,
719 unsigned long arg)
720{
721 return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
722}
723#endif
724
725static const struct file_operations lineevent_fileops = {
726 .release = lineevent_release,
727 .read = lineevent_read,
728 .poll = lineevent_poll,
729 .owner = THIS_MODULE,
730 .llseek = noop_llseek,
731 .unlocked_ioctl = lineevent_ioctl,
732#ifdef CONFIG_COMPAT
733 .compat_ioctl = lineevent_ioctl_compat,
734#endif
735};
736
Ben Dooks33265b12016-06-17 16:03:13 +0100737static irqreturn_t lineevent_irq_thread(int irq, void *p)
Linus Walleij61f922d2016-06-02 11:30:15 +0200738{
739 struct lineevent_state *le = p;
740 struct gpioevent_data ge;
Bartosz Golaszewskidf1e76f2017-07-03 11:12:03 +0200741 int ret, level;
Linus Walleij61f922d2016-06-02 11:30:15 +0200742
743 ge.timestamp = ktime_get_real_ns();
Bartosz Golaszewskidf1e76f2017-07-03 11:12:03 +0200744 level = gpiod_get_value_cansleep(le->desc);
Linus Walleij61f922d2016-06-02 11:30:15 +0200745
Bartosz Golaszewskiad537b82017-06-23 13:45:16 +0200746 if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
747 && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200748 if (level)
749 /* Emit low-to-high event */
750 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
751 else
752 /* Emit high-to-low event */
753 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Bartosz Golaszewskidf1e76f2017-07-03 11:12:03 +0200754 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE && level) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200755 /* Emit low-to-high event */
756 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
Bartosz Golaszewskidf1e76f2017-07-03 11:12:03 +0200757 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE && !level) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200758 /* Emit high-to-low event */
759 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Arnd Bergmannbc0207a2016-06-16 11:02:41 +0200760 } else {
761 return IRQ_NONE;
Linus Walleij61f922d2016-06-02 11:30:15 +0200762 }
763
764 ret = kfifo_put(&le->events, ge);
765 if (ret != 0)
766 wake_up_poll(&le->wait, POLLIN);
767
768 return IRQ_HANDLED;
769}
770
771static int lineevent_create(struct gpio_device *gdev, void __user *ip)
772{
773 struct gpioevent_request eventreq;
774 struct lineevent_state *le;
775 struct gpio_desc *desc;
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200776 struct file *file;
Linus Walleij61f922d2016-06-02 11:30:15 +0200777 u32 offset;
778 u32 lflags;
779 u32 eflags;
780 int fd;
781 int ret;
782 int irqflags = 0;
783
784 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
785 return -EFAULT;
786
787 le = kzalloc(sizeof(*le), GFP_KERNEL);
788 if (!le)
789 return -ENOMEM;
790 le->gdev = gdev;
791 get_device(&gdev->dev);
792
793 /* Make sure this is terminated */
794 eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
795 if (strlen(eventreq.consumer_label)) {
796 le->label = kstrdup(eventreq.consumer_label,
797 GFP_KERNEL);
798 if (!le->label) {
799 ret = -ENOMEM;
800 goto out_free_le;
801 }
802 }
803
804 offset = eventreq.lineoffset;
805 lflags = eventreq.handleflags;
806 eflags = eventreq.eventflags;
807
Lars-Peter Clausenb8b0e3d2016-10-18 16:54:03 +0200808 if (offset >= gdev->ngpio) {
809 ret = -EINVAL;
810 goto out_free_label;
811 }
812
Lars-Peter Clausenac7dbb92016-10-18 16:54:06 +0200813 /* Return an error if a unknown flag is set */
814 if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
815 (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) {
816 ret = -EINVAL;
817 goto out_free_label;
818 }
819
Linus Walleij61f922d2016-06-02 11:30:15 +0200820 /* This is just wrong: we don't look for events on output lines */
821 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
822 ret = -EINVAL;
823 goto out_free_label;
824 }
825
826 desc = &gdev->descs[offset];
827 ret = gpiod_request(desc, le->label);
828 if (ret)
829 goto out_free_desc;
830 le->desc = desc;
831 le->eflags = eflags;
832
833 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
834 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
835 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
836 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
837 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
838 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
839
840 ret = gpiod_direction_input(desc);
841 if (ret)
842 goto out_free_desc;
843
844 le->irq = gpiod_to_irq(desc);
845 if (le->irq <= 0) {
846 ret = -ENODEV;
847 goto out_free_desc;
848 }
849
850 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
851 irqflags |= IRQF_TRIGGER_RISING;
852 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
853 irqflags |= IRQF_TRIGGER_FALLING;
854 irqflags |= IRQF_ONESHOT;
855 irqflags |= IRQF_SHARED;
856
857 INIT_KFIFO(le->events);
858 init_waitqueue_head(&le->wait);
859 mutex_init(&le->read_lock);
860
861 /* Request a thread to read the events */
862 ret = request_threaded_irq(le->irq,
863 NULL,
864 lineevent_irq_thread,
865 irqflags,
866 le->label,
867 le);
868 if (ret)
869 goto out_free_desc;
870
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200871 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleij61f922d2016-06-02 11:30:15 +0200872 if (fd < 0) {
873 ret = fd;
874 goto out_free_irq;
875 }
876
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200877 file = anon_inode_getfile("gpio-event",
878 &lineevent_fileops,
879 le,
880 O_RDONLY | O_CLOEXEC);
881 if (IS_ERR(file)) {
882 ret = PTR_ERR(file);
883 goto out_put_unused_fd;
884 }
885
Linus Walleij61f922d2016-06-02 11:30:15 +0200886 eventreq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200887 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200888 /*
889 * fput() will trigger the release() callback, so do not go onto
890 * the regular error cleanup path here.
891 */
892 fput(file);
893 put_unused_fd(fd);
894 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +0200895 }
Linus Walleij61f922d2016-06-02 11:30:15 +0200896
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200897 fd_install(fd, file);
898
Linus Walleij61f922d2016-06-02 11:30:15 +0200899 return 0;
900
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200901out_put_unused_fd:
902 put_unused_fd(fd);
Linus Walleij61f922d2016-06-02 11:30:15 +0200903out_free_irq:
904 free_irq(le->irq, le);
905out_free_desc:
906 gpiod_free(le->desc);
907out_free_label:
908 kfree(le->label);
909out_free_le:
910 kfree(le);
911 put_device(&gdev->dev);
912 return ret;
913}
914
Thierry Reding950d55f52017-07-24 16:57:22 +0200915/*
Linus Walleij3c702e92015-10-21 15:29:53 +0200916 * gpio_ioctl() - ioctl handler for the GPIO chardev
917 */
918static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
919{
920 struct gpio_device *gdev = filp->private_data;
921 struct gpio_chip *chip = gdev->chip;
Linus Walleij8b92e172016-05-27 14:24:04 +0200922 void __user *ip = (void __user *)arg;
Linus Walleij3c702e92015-10-21 15:29:53 +0200923
924 /* We fail any subsequent ioctl():s when the chip is gone */
925 if (!chip)
926 return -ENODEV;
927
Linus Walleij521a2ad2016-02-12 22:25:22 +0100928 /* Fill in the struct and pass to userspace */
Linus Walleij3c702e92015-10-21 15:29:53 +0200929 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
Linus Walleij521a2ad2016-02-12 22:25:22 +0100930 struct gpiochip_info chipinfo;
931
Lars-Peter Clausen0f4bbb22016-10-18 16:54:00 +0200932 memset(&chipinfo, 0, sizeof(chipinfo));
933
Linus Walleij3c702e92015-10-21 15:29:53 +0200934 strncpy(chipinfo.name, dev_name(&gdev->dev),
935 sizeof(chipinfo.name));
936 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
Linus Walleijdf4878e2016-02-12 14:48:23 +0100937 strncpy(chipinfo.label, gdev->label,
938 sizeof(chipinfo.label));
939 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100940 chipinfo.lines = gdev->ngpio;
Linus Walleij3c702e92015-10-21 15:29:53 +0200941 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
942 return -EFAULT;
943 return 0;
Linus Walleij521a2ad2016-02-12 22:25:22 +0100944 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
945 struct gpioline_info lineinfo;
946 struct gpio_desc *desc;
947
948 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
949 return -EFAULT;
Lars-Peter Clausen1f1cc452016-10-18 16:53:59 +0200950 if (lineinfo.line_offset >= gdev->ngpio)
Linus Walleij521a2ad2016-02-12 22:25:22 +0100951 return -EINVAL;
952
953 desc = &gdev->descs[lineinfo.line_offset];
954 if (desc->name) {
955 strncpy(lineinfo.name, desc->name,
956 sizeof(lineinfo.name));
957 lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
958 } else {
959 lineinfo.name[0] = '\0';
960 }
961 if (desc->label) {
Linus Walleij214338e2016-02-25 21:01:48 +0100962 strncpy(lineinfo.consumer, desc->label,
963 sizeof(lineinfo.consumer));
964 lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +0100965 } else {
Linus Walleij214338e2016-02-25 21:01:48 +0100966 lineinfo.consumer[0] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +0100967 }
968
969 /*
970 * Userspace only need to know that the kernel is using
971 * this GPIO so it can't use it.
972 */
973 lineinfo.flags = 0;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100974 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
975 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
976 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
977 test_bit(FLAG_EXPORT, &desc->flags) ||
978 test_bit(FLAG_SYSFS, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100979 lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100980 if (test_bit(FLAG_IS_OUT, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100981 lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100982 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100983 lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100984 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100985 lineinfo.flags |= GPIOLINE_FLAG_OPEN_DRAIN;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100986 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100987 lineinfo.flags |= GPIOLINE_FLAG_OPEN_SOURCE;
988
989 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
990 return -EFAULT;
991 return 0;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200992 } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
993 return linehandle_create(gdev, ip);
Linus Walleij61f922d2016-06-02 11:30:15 +0200994 } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
995 return lineevent_create(gdev, ip);
Linus Walleij3c702e92015-10-21 15:29:53 +0200996 }
997 return -EINVAL;
998}
999
Linus Walleij8b92e172016-05-27 14:24:04 +02001000#ifdef CONFIG_COMPAT
1001static long gpio_ioctl_compat(struct file *filp, unsigned int cmd,
1002 unsigned long arg)
1003{
1004 return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
1005}
1006#endif
1007
Linus Walleij3c702e92015-10-21 15:29:53 +02001008/**
1009 * gpio_chrdev_open() - open the chardev for ioctl operations
1010 * @inode: inode for this chardev
1011 * @filp: file struct for storing private data
1012 * Returns 0 on success
1013 */
1014static int gpio_chrdev_open(struct inode *inode, struct file *filp)
1015{
1016 struct gpio_device *gdev = container_of(inode->i_cdev,
1017 struct gpio_device, chrdev);
1018
1019 /* Fail on open if the backing gpiochip is gone */
Stephen Boydfb505742017-01-09 11:47:44 -08001020 if (!gdev->chip)
Linus Walleij3c702e92015-10-21 15:29:53 +02001021 return -ENODEV;
1022 get_device(&gdev->dev);
1023 filp->private_data = gdev;
Lars-Peter Clausenf4e81c52016-11-30 13:05:21 +01001024
1025 return nonseekable_open(inode, filp);
Linus Walleij3c702e92015-10-21 15:29:53 +02001026}
1027
1028/**
1029 * gpio_chrdev_release() - close chardev after ioctl operations
1030 * @inode: inode for this chardev
1031 * @filp: file struct for storing private data
1032 * Returns 0 on success
1033 */
1034static int gpio_chrdev_release(struct inode *inode, struct file *filp)
1035{
1036 struct gpio_device *gdev = container_of(inode->i_cdev,
1037 struct gpio_device, chrdev);
1038
Linus Walleij3c702e92015-10-21 15:29:53 +02001039 put_device(&gdev->dev);
1040 return 0;
1041}
1042
1043
1044static const struct file_operations gpio_fileops = {
1045 .release = gpio_chrdev_release,
1046 .open = gpio_chrdev_open,
1047 .owner = THIS_MODULE,
Lars-Peter Clausenf4e81c52016-11-30 13:05:21 +01001048 .llseek = no_llseek,
Linus Walleij3c702e92015-10-21 15:29:53 +02001049 .unlocked_ioctl = gpio_ioctl,
Linus Walleij8b92e172016-05-27 14:24:04 +02001050#ifdef CONFIG_COMPAT
1051 .compat_ioctl = gpio_ioctl_compat,
1052#endif
Linus Walleij3c702e92015-10-21 15:29:53 +02001053};
1054
Linus Walleijff2b1352015-10-20 11:10:38 +02001055static void gpiodevice_release(struct device *dev)
1056{
1057 struct gpio_device *gdev = dev_get_drvdata(dev);
1058
1059 list_del(&gdev->list);
1060 ida_simple_remove(&gpio_ida, gdev->id);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001061 kfree(gdev->label);
1062 kfree(gdev->descs);
Linus Walleij9efd9e62016-02-09 14:27:42 +01001063 kfree(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001064}
1065
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001066static int gpiochip_setup_dev(struct gpio_device *gdev)
1067{
1068 int status;
1069
1070 cdev_init(&gdev->chrdev, &gpio_fileops);
1071 gdev->chrdev.owner = THIS_MODULE;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001072 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001073
1074 status = cdev_device_add(&gdev->chrdev, &gdev->dev);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001075 if (status)
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001076 return status;
1077
1078 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
1079 MAJOR(gpio_devt), gdev->id);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001080
1081 status = gpiochip_sysfs_register(gdev);
1082 if (status)
1083 goto err_remove_device;
1084
1085 /* From this point, the .release() function cleans up gpio_device */
1086 gdev->dev.release = gpiodevice_release;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001087 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
1088 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
1089 dev_name(&gdev->dev), gdev->chip->label ? : "generic");
1090
1091 return 0;
1092
1093err_remove_device:
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001094 cdev_device_del(&gdev->chrdev, &gdev->dev);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001095 return status;
1096}
1097
1098static void gpiochip_setup_devs(void)
1099{
1100 struct gpio_device *gdev;
1101 int err;
1102
1103 list_for_each_entry(gdev, &gpio_devices, list) {
1104 err = gpiochip_setup_dev(gdev);
1105 if (err)
1106 pr_err("%s: Failed to initialize gpio device (%d)\n",
1107 dev_name(&gdev->dev), err);
1108 }
1109}
1110
Thierry Reding959bc7b2017-11-07 19:15:59 +01001111int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
1112 struct lock_class_key *key)
David Brownelld2876d02008-02-04 22:28:20 -08001113{
1114 unsigned long flags;
1115 int status = 0;
Linus Walleijff2b1352015-10-20 11:10:38 +02001116 unsigned i;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001117 int base = chip->base;
Linus Walleijff2b1352015-10-20 11:10:38 +02001118 struct gpio_device *gdev;
David Brownelld2876d02008-02-04 22:28:20 -08001119
Linus Walleijff2b1352015-10-20 11:10:38 +02001120 /*
1121 * First: allocate and populate the internal stat container, and
1122 * set up the struct device.
1123 */
Josh Cartwright969f07b2016-02-17 16:44:15 -06001124 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
Linus Walleijff2b1352015-10-20 11:10:38 +02001125 if (!gdev)
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001126 return -ENOMEM;
Linus Walleij3c702e92015-10-21 15:29:53 +02001127 gdev->dev.bus = &gpio_bus_type;
Linus Walleijff2b1352015-10-20 11:10:38 +02001128 gdev->chip = chip;
1129 chip->gpiodev = gdev;
1130 if (chip->parent) {
1131 gdev->dev.parent = chip->parent;
1132 gdev->dev.of_node = chip->parent->of_node;
Thierry Redingacc6e332016-07-05 14:11:14 +02001133 }
1134
Linus Walleijff2b1352015-10-20 11:10:38 +02001135#ifdef CONFIG_OF_GPIO
1136 /* If the gpiochip has an assigned OF node this takes precedence */
Thierry Redingacc6e332016-07-05 14:11:14 +02001137 if (chip->of_node)
1138 gdev->dev.of_node = chip->of_node;
Linus Walleijff2b1352015-10-20 11:10:38 +02001139#endif
Thierry Redingacc6e332016-07-05 14:11:14 +02001140
Linus Walleijff2b1352015-10-20 11:10:38 +02001141 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
1142 if (gdev->id < 0) {
1143 status = gdev->id;
1144 goto err_free_gdev;
1145 }
1146 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
1147 device_initialize(&gdev->dev);
1148 dev_set_drvdata(&gdev->dev, gdev);
1149 if (chip->parent && chip->parent->driver)
1150 gdev->owner = chip->parent->driver->owner;
1151 else if (chip->owner)
1152 /* TODO: remove chip->owner */
1153 gdev->owner = chip->owner;
1154 else
1155 gdev->owner = THIS_MODULE;
David Brownelld2876d02008-02-04 22:28:20 -08001156
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001157 gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001158 if (!gdev->descs) {
Linus Walleijff2b1352015-10-20 11:10:38 +02001159 status = -ENOMEM;
1160 goto err_free_gdev;
1161 }
1162
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001163 if (chip->ngpio == 0) {
1164 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
Linus Walleijff2b1352015-10-20 11:10:38 +02001165 status = -EINVAL;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001166 goto err_free_descs;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001167 }
Linus Walleijdf4878e2016-02-12 14:48:23 +01001168
1169 if (chip->label)
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001170 gdev->label = kstrdup(chip->label, GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001171 else
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001172 gdev->label = kstrdup("unknown", GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001173 if (!gdev->label) {
1174 status = -ENOMEM;
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001175 goto err_free_descs;
Linus Walleijdf4878e2016-02-12 14:48:23 +01001176 }
1177
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001178 gdev->ngpio = chip->ngpio;
Linus Walleij43c54ec2016-02-11 11:37:48 +01001179 gdev->data = data;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001180
David Brownelld2876d02008-02-04 22:28:20 -08001181 spin_lock_irqsave(&gpio_lock, flags);
1182
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001183 /*
1184 * TODO: this allocates a Linux GPIO number base in the global
1185 * GPIO numberspace for this chip. In the long run we want to
1186 * get *rid* of this numberspace and use only descriptors, but
1187 * it may be a pipe dream. It will not happen before we get rid
1188 * of the sysfs interface anyways.
1189 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001190 if (base < 0) {
1191 base = gpiochip_find_base(chip->ngpio);
1192 if (base < 0) {
1193 status = base;
Johan Hovold225fce82015-01-12 17:12:25 +01001194 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001195 goto err_free_label;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001196 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001197 /*
1198 * TODO: it should not be necessary to reflect the assigned
1199 * base outside of the GPIO subsystem. Go over drivers and
1200 * see if anyone makes use of this, else drop this and assign
1201 * a poison instead.
1202 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001203 chip->base = base;
1204 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001205 gdev->base = base;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001206
Linus Walleijff2b1352015-10-20 11:10:38 +02001207 status = gpiodev_add_to_list(gdev);
Johan Hovold05aa5202015-01-12 17:12:26 +01001208 if (status) {
1209 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001210 goto err_free_label;
Johan Hovold05aa5202015-01-12 17:12:26 +01001211 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001212
Linus Walleij545ebd92016-05-30 17:11:59 +02001213 spin_unlock_irqrestore(&gpio_lock, flags);
1214
Linus Walleijff2b1352015-10-20 11:10:38 +02001215 for (i = 0; i < chip->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001216 struct gpio_desc *desc = &gdev->descs[i];
David Brownelld8f388d82008-07-25 01:46:07 -07001217
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001218 desc->gdev = gdev;
Linus Walleij72d32002016-04-28 13:33:59 +02001219 /*
1220 * REVISIT: most hardware initializes GPIOs as inputs
1221 * (often with pullups enabled) so power usage is
1222 * minimized. Linux code should set the gpio direction
1223 * first thing; but until it does, and in case
1224 * chip->get_direction is not set, we may expose the
1225 * wrong direction in sysfs.
Johan Hovold05aa5202015-01-12 17:12:26 +01001226 */
Linus Walleij72d32002016-04-28 13:33:59 +02001227
1228 if (chip->get_direction) {
1229 /*
1230 * If we have .get_direction, set up the initial
1231 * direction flag from the hardware.
1232 */
1233 int dir = chip->get_direction(chip, i);
1234
1235 if (!dir)
1236 set_bit(FLAG_IS_OUT, &desc->flags);
1237 } else if (!chip->direction_input) {
1238 /*
1239 * If the chip lacks the .direction_input callback
1240 * we logically assume all lines are outputs.
1241 */
1242 set_bit(FLAG_IS_OUT, &desc->flags);
1243 }
David Brownelld2876d02008-02-04 22:28:20 -08001244 }
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001245
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301246#ifdef CONFIG_PINCTRL
Linus Walleij20ec3e32016-02-11 11:03:06 +01001247 INIT_LIST_HEAD(&gdev->pin_ranges);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301248#endif
1249
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001250 status = gpiochip_set_desc_names(chip);
1251 if (status)
1252 goto err_remove_from_list;
1253
Mika Westerberg79b804c2016-09-20 15:15:21 +03001254 status = gpiochip_irqchip_init_valid_mask(chip);
1255 if (status)
1256 goto err_remove_from_list;
1257
Thierry Reding959bc7b2017-11-07 19:15:59 +01001258 status = gpiochip_add_irqchip(chip, key);
Thierry Redinge0d89722017-11-07 19:15:54 +01001259 if (status)
1260 goto err_remove_chip;
1261
Tomeu Vizoso28355f82015-07-14 10:29:54 +02001262 status = of_gpiochip_add(chip);
1263 if (status)
1264 goto err_remove_chip;
1265
Mika Westerberg664e3e52014-01-08 12:40:54 +02001266 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001267
Linus Walleij3c702e92015-10-21 15:29:53 +02001268 /*
1269 * By first adding the chardev, and then adding the device,
1270 * we get a device node entry in sysfs under
1271 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1272 * coldplug of device nodes and other udev business.
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001273 * We can do this only if gpiolib has been initialized.
1274 * Otherwise, defer until later.
Linus Walleij3c702e92015-10-21 15:29:53 +02001275 */
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001276 if (gpiolib_initialized) {
1277 status = gpiochip_setup_dev(gdev);
1278 if (status)
1279 goto err_remove_chip;
1280 }
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001281 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001282
Johan Hovold225fce82015-01-12 17:12:25 +01001283err_remove_chip:
1284 acpi_gpiochip_remove(chip);
Johan Hovold6d867502015-05-04 17:23:25 +02001285 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +01001286 of_gpiochip_remove(chip);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001287 gpiochip_irqchip_free_valid_mask(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001288err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +01001289 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001290 list_del(&gdev->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001291 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001292err_free_label:
1293 kfree(gdev->label);
1294err_free_descs:
1295 kfree(gdev->descs);
Linus Walleijff2b1352015-10-20 11:10:38 +02001296err_free_gdev:
1297 ida_simple_remove(&gpio_ida, gdev->id);
David Brownelld2876d02008-02-04 22:28:20 -08001298 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +02001299 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001300 gdev->base, gdev->base + gdev->ngpio - 1,
1301 chip->label ? : "generic");
1302 kfree(gdev);
David Brownelld2876d02008-02-04 22:28:20 -08001303 return status;
1304}
Thierry Reding959bc7b2017-11-07 19:15:59 +01001305EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);
David Brownelld2876d02008-02-04 22:28:20 -08001306
1307/**
Linus Walleij43c54ec2016-02-11 11:37:48 +01001308 * gpiochip_get_data() - get per-subdriver data for the chip
Thierry Reding950d55f52017-07-24 16:57:22 +02001309 * @chip: GPIO chip
1310 *
1311 * Returns:
1312 * The per-subdriver data for the chip.
Linus Walleij43c54ec2016-02-11 11:37:48 +01001313 */
1314void *gpiochip_get_data(struct gpio_chip *chip)
1315{
1316 return chip->gpiodev->data;
1317}
1318EXPORT_SYMBOL_GPL(gpiochip_get_data);
1319
1320/**
David Brownelld2876d02008-02-04 22:28:20 -08001321 * gpiochip_remove() - unregister a gpio_chip
1322 * @chip: the chip to unregister
1323 *
1324 * A gpio_chip with any GPIOs still requested may not be removed.
1325 */
abdoulaye berthee1db1702014-07-05 18:28:50 +02001326void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -08001327{
Linus Walleijff2b1352015-10-20 11:10:38 +02001328 struct gpio_device *gdev = chip->gpiodev;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001329 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001330 unsigned long flags;
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001331 unsigned i;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001332 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -08001333
Linus Walleijff2b1352015-10-20 11:10:38 +02001334 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
Linus Walleijafbc4f32016-02-09 13:21:06 +01001335 gpiochip_sysfs_unregister(gdev);
Geert Uytterhoeven5018ada2016-12-19 18:29:23 +01001336 gpiochip_free_hogs(chip);
Bamvor Jian Zhangbd203bd2016-02-20 13:13:19 +08001337 /* Numb the device, cancelling all outstanding operations */
1338 gdev->chip = NULL;
Johan Hovold00acc3d2015-01-12 17:12:27 +01001339 gpiochip_irqchip_remove(chip);
Mika Westerberg6072b9d2014-03-10 14:54:53 +02001340 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +01001341 gpiochip_remove_pin_ranges(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001342 of_gpiochip_remove(chip);
Linus Walleij43c54ec2016-02-11 11:37:48 +01001343 /*
1344 * We accept no more calls into the driver from this point, so
1345 * NULL the driver data pointer
1346 */
1347 gdev->data = NULL;
Anton Vorontsov391c9702010-06-08 07:48:17 -06001348
Johan Hovold6798aca2015-01-12 17:12:28 +01001349 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001350 for (i = 0; i < gdev->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001351 desc = &gdev->descs[i];
Johan Hovoldfab28b82015-05-04 17:10:27 +02001352 if (test_bit(FLAG_REQUESTED, &desc->flags))
1353 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -08001354 }
David Brownelld2876d02008-02-04 22:28:20 -08001355 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001356
Johan Hovoldfab28b82015-05-04 17:10:27 +02001357 if (requested)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001358 dev_crit(&gdev->dev,
Linus Walleij58383c782015-11-04 09:56:26 +01001359 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +02001360
Linus Walleijff2b1352015-10-20 11:10:38 +02001361 /*
1362 * The gpiochip side puts its use of the device to rest here:
1363 * if there are no userspace clients, the chardev and device will
1364 * be removed, else it will be dangling until the last user is
1365 * gone.
1366 */
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001367 cdev_device_del(&gdev->chrdev, &gdev->dev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001368 put_device(&gdev->dev);
David Brownelld2876d02008-02-04 22:28:20 -08001369}
1370EXPORT_SYMBOL_GPL(gpiochip_remove);
1371
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301372static void devm_gpio_chip_release(struct device *dev, void *res)
1373{
1374 struct gpio_chip *chip = *(struct gpio_chip **)res;
1375
1376 gpiochip_remove(chip);
1377}
1378
1379static int devm_gpio_chip_match(struct device *dev, void *res, void *data)
1380
1381{
1382 struct gpio_chip **r = res;
1383
1384 if (!r || !*r) {
1385 WARN_ON(!r || !*r);
1386 return 0;
1387 }
1388
1389 return *r == data;
1390}
1391
1392/**
1393 * devm_gpiochip_add_data() - Resource manager piochip_add_data()
1394 * @dev: the device pointer on which irq_chip belongs to.
1395 * @chip: the chip to register, with chip->base initialized
Thierry Reding950d55f52017-07-24 16:57:22 +02001396 * @data: driver-private data associated with this chip
1397 *
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301398 * Context: potentially before irqs will work
1399 *
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301400 * The gpio chip automatically be released when the device is unbound.
Thierry Reding950d55f52017-07-24 16:57:22 +02001401 *
1402 * Returns:
1403 * A negative errno if the chip can't be registered, such as because the
1404 * chip->base is invalid or already associated with a different chip.
1405 * Otherwise it returns zero as a success code.
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301406 */
1407int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
1408 void *data)
1409{
1410 struct gpio_chip **ptr;
1411 int ret;
1412
1413 ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
1414 GFP_KERNEL);
1415 if (!ptr)
1416 return -ENOMEM;
1417
1418 ret = gpiochip_add_data(chip, data);
1419 if (ret < 0) {
1420 devres_free(ptr);
1421 return ret;
1422 }
1423
1424 *ptr = chip;
1425 devres_add(dev, ptr);
1426
1427 return 0;
1428}
1429EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
1430
1431/**
1432 * devm_gpiochip_remove() - Resource manager of gpiochip_remove()
1433 * @dev: device for which which resource was allocated
1434 * @chip: the chip to remove
1435 *
1436 * A gpio_chip with any GPIOs still requested may not be removed.
1437 */
1438void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip)
1439{
1440 int ret;
1441
1442 ret = devres_release(dev, devm_gpio_chip_release,
1443 devm_gpio_chip_match, chip);
Christophe JAILLET3988d662017-01-27 12:56:42 +01001444 WARN_ON(ret);
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301445}
1446EXPORT_SYMBOL_GPL(devm_gpiochip_remove);
1447
Grant Likely594fa262010-06-08 07:48:16 -06001448/**
1449 * gpiochip_find() - iterator for locating a specific gpio_chip
1450 * @data: data to pass to match function
Thierry Reding950d55f52017-07-24 16:57:22 +02001451 * @match: Callback function to check gpio_chip
Grant Likely594fa262010-06-08 07:48:16 -06001452 *
1453 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1454 * determined by a user supplied @match callback. The callback should return
1455 * 0 if the device doesn't match and non-zero if it does. If the callback is
1456 * non-zero, this function will return to the caller and not iterate over any
1457 * more gpio_chips.
1458 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001459struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001460 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001461 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001462{
Linus Walleijff2b1352015-10-20 11:10:38 +02001463 struct gpio_device *gdev;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001464 struct gpio_chip *chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -06001465 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001466
1467 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001468 list_for_each_entry(gdev, &gpio_devices, list)
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001469 if (gdev->chip && match(gdev->chip, data)) {
1470 chip = gdev->chip;
Grant Likely594fa262010-06-08 07:48:16 -06001471 break;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001472 }
Linus Walleijff2b1352015-10-20 11:10:38 +02001473
Grant Likely594fa262010-06-08 07:48:16 -06001474 spin_unlock_irqrestore(&gpio_lock, flags);
1475
1476 return chip;
1477}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001478EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001479
Alexandre Courbot79697ef2013-11-16 21:39:32 +09001480static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1481{
1482 const char *name = data;
1483
1484 return !strcmp(chip->label, name);
1485}
1486
1487static struct gpio_chip *find_chip_by_name(const char *name)
1488{
1489 return gpiochip_find((void *)name, gpiochip_match_name);
1490}
1491
Linus Walleij14250522014-03-25 10:40:18 +01001492#ifdef CONFIG_GPIOLIB_IRQCHIP
1493
1494/*
1495 * The following is irqchip helper code for gpiochips.
1496 */
1497
Mika Westerberg79b804c2016-09-20 15:15:21 +03001498static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1499{
Thierry Redingdc7b0382017-11-07 19:15:52 +01001500 if (!gpiochip->irq.need_valid_mask)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001501 return 0;
1502
Thierry Redingdc7b0382017-11-07 19:15:52 +01001503 gpiochip->irq.valid_mask = kcalloc(BITS_TO_LONGS(gpiochip->ngpio),
Mika Westerberg79b804c2016-09-20 15:15:21 +03001504 sizeof(long), GFP_KERNEL);
Thierry Redingdc7b0382017-11-07 19:15:52 +01001505 if (!gpiochip->irq.valid_mask)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001506 return -ENOMEM;
1507
1508 /* Assume by default all GPIOs are valid */
Thierry Redingdc7b0382017-11-07 19:15:52 +01001509 bitmap_fill(gpiochip->irq.valid_mask, gpiochip->ngpio);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001510
1511 return 0;
1512}
1513
1514static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1515{
Thierry Redingdc7b0382017-11-07 19:15:52 +01001516 kfree(gpiochip->irq.valid_mask);
1517 gpiochip->irq.valid_mask = NULL;
Mika Westerberg79b804c2016-09-20 15:15:21 +03001518}
1519
1520static bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
1521 unsigned int offset)
1522{
1523 /* No mask means all valid */
Thierry Redingdc7b0382017-11-07 19:15:52 +01001524 if (likely(!gpiochip->irq.valid_mask))
Mika Westerberg79b804c2016-09-20 15:15:21 +03001525 return true;
Thierry Redingdc7b0382017-11-07 19:15:52 +01001526 return test_bit(offset, gpiochip->irq.valid_mask);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001527}
1528
Linus Walleij14250522014-03-25 10:40:18 +01001529/**
Linus Walleijd245b3f2016-11-24 10:57:25 +01001530 * gpiochip_set_cascaded_irqchip() - connects a cascaded irqchip to a gpiochip
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001531 * @gpiochip: the gpiochip to set the irqchip chain to
1532 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01001533 * @parent_irq: the irq number corresponding to the parent IRQ for this
1534 * chained irqchip
1535 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001536 * coming out of the gpiochip. If the interrupt is nested rather than
1537 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +01001538 */
Linus Walleijd245b3f2016-11-24 10:57:25 +01001539static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gpiochip,
1540 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001541 unsigned int parent_irq,
Linus Walleijd245b3f2016-11-24 10:57:25 +01001542 irq_flow_handler_t parent_handler)
Linus Walleij14250522014-03-25 10:40:18 +01001543{
Linus Walleij83141a72014-09-26 13:50:12 +02001544 unsigned int offset;
1545
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001546 if (!gpiochip->irq.domain) {
Linus Walleij83141a72014-09-26 13:50:12 +02001547 chip_err(gpiochip, "called %s before setting up irqchip\n",
1548 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +02001549 return;
1550 }
1551
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001552 if (parent_handler) {
1553 if (gpiochip->can_sleep) {
1554 chip_err(gpiochip,
1555 "you cannot have chained interrupts on a "
1556 "chip that may sleep\n");
1557 return;
1558 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001559 /*
1560 * The parent irqchip is already using the chip_data for this
1561 * irqchip, so our callbacks simply use the handler_data.
1562 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +02001563 irq_set_chained_handler_and_data(parent_irq, parent_handler,
1564 gpiochip);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001565
Thierry Reding39e5f092017-11-07 19:15:50 +01001566 gpiochip->irq.parents = &parent_irq;
1567 gpiochip->irq.num_parents = 1;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001568 }
Linus Walleij83141a72014-09-26 13:50:12 +02001569
1570 /* Set the parent IRQ for all affected IRQs */
Mika Westerberg79b804c2016-09-20 15:15:21 +03001571 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1572 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1573 continue;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001574 irq_set_parent(irq_find_mapping(gpiochip->irq.domain, offset),
Linus Walleij83141a72014-09-26 13:50:12 +02001575 parent_irq);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001576 }
Linus Walleij14250522014-03-25 10:40:18 +01001577}
Linus Walleijd245b3f2016-11-24 10:57:25 +01001578
1579/**
1580 * gpiochip_set_chained_irqchip() - connects a chained irqchip to a gpiochip
1581 * @gpiochip: the gpiochip to set the irqchip chain to
1582 * @irqchip: the irqchip to chain to the gpiochip
1583 * @parent_irq: the irq number corresponding to the parent IRQ for this
1584 * chained irqchip
1585 * @parent_handler: the parent interrupt handler for the accumulated IRQ
1586 * coming out of the gpiochip. If the interrupt is nested rather than
1587 * cascaded, pass NULL in this handler argument
1588 */
1589void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
1590 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001591 unsigned int parent_irq,
Linus Walleijd245b3f2016-11-24 10:57:25 +01001592 irq_flow_handler_t parent_handler)
1593{
Thierry Reding60ed54c2017-11-07 19:15:57 +01001594 if (gpiochip->irq.threaded) {
1595 chip_err(gpiochip, "tried to chain a threaded gpiochip\n");
1596 return;
1597 }
1598
Linus Walleijd245b3f2016-11-24 10:57:25 +01001599 gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
1600 parent_handler);
1601}
Linus Walleij14250522014-03-25 10:40:18 +01001602EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
1603
1604/**
Linus Walleijd245b3f2016-11-24 10:57:25 +01001605 * gpiochip_set_nested_irqchip() - connects a nested irqchip to a gpiochip
1606 * @gpiochip: the gpiochip to set the irqchip nested handler to
1607 * @irqchip: the irqchip to nest to the gpiochip
1608 * @parent_irq: the irq number corresponding to the parent IRQ for this
1609 * nested irqchip
1610 */
1611void gpiochip_set_nested_irqchip(struct gpio_chip *gpiochip,
1612 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001613 unsigned int parent_irq)
Linus Walleijd245b3f2016-11-24 10:57:25 +01001614{
Linus Walleijd245b3f2016-11-24 10:57:25 +01001615 gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
1616 NULL);
1617}
1618EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip);
1619
1620/**
Linus Walleij14250522014-03-25 10:40:18 +01001621 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1622 * @d: the irqdomain used by this irqchip
1623 * @irq: the global irq number used by this GPIO irqchip irq
1624 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1625 *
1626 * This function will set up the mapping for a certain IRQ line on a
1627 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1628 * stored inside the gpiochip.
1629 */
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001630int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1631 irq_hw_number_t hwirq)
Linus Walleij14250522014-03-25 10:40:18 +01001632{
1633 struct gpio_chip *chip = d->host_data;
Thierry Redinge0d89722017-11-07 19:15:54 +01001634 int err = 0;
Linus Walleij14250522014-03-25 10:40:18 +01001635
Grygorii Strashkodc749a02017-07-21 11:49:00 -05001636 if (!gpiochip_irqchip_irq_valid(chip, hwirq))
1637 return -ENXIO;
1638
Linus Walleij14250522014-03-25 10:40:18 +01001639 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001640 /*
1641 * This lock class tells lockdep that GPIO irqs are in a different
1642 * category than their parents, so it won't report false recursion.
1643 */
Thierry Redingca9df052017-11-07 19:15:53 +01001644 irq_set_lockdep_class(irq, chip->irq.lock_key);
Thierry Redingc7a0aa52017-11-07 19:15:48 +01001645 irq_set_chip_and_handler(irq, chip->irq.chip, chip->irq.handler);
Linus Walleijd245b3f2016-11-24 10:57:25 +01001646 /* Chips that use nested thread handlers have them marked */
Thierry Reding60ed54c2017-11-07 19:15:57 +01001647 if (chip->irq.threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02001648 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +01001649 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -05001650
Thierry Redinge0d89722017-11-07 19:15:54 +01001651 if (chip->irq.num_parents == 1)
1652 err = irq_set_parent(irq, chip->irq.parents[0]);
1653 else if (chip->irq.map)
1654 err = irq_set_parent(irq, chip->irq.map[hwirq]);
1655
1656 if (err < 0)
1657 return err;
1658
Linus Walleij1333b902014-04-23 16:45:12 +02001659 /*
1660 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1661 * is passed as default type.
1662 */
Thierry Reding3634eeb2017-11-07 19:15:49 +01001663 if (chip->irq.default_type != IRQ_TYPE_NONE)
1664 irq_set_irq_type(irq, chip->irq.default_type);
Linus Walleij14250522014-03-25 10:40:18 +01001665
1666 return 0;
1667}
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001668EXPORT_SYMBOL_GPL(gpiochip_irq_map);
Linus Walleij14250522014-03-25 10:40:18 +01001669
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001670void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
Linus Walleijc3626fd2014-03-28 20:42:01 +01001671{
Linus Walleij1c8732b2014-04-09 13:34:39 +02001672 struct gpio_chip *chip = d->host_data;
1673
Thierry Reding60ed54c2017-11-07 19:15:57 +01001674 if (chip->irq.threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02001675 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001676 irq_set_chip_and_handler(irq, NULL, NULL);
1677 irq_set_chip_data(irq, NULL);
1678}
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001679EXPORT_SYMBOL_GPL(gpiochip_irq_unmap);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001680
Linus Walleij14250522014-03-25 10:40:18 +01001681static const struct irq_domain_ops gpiochip_domain_ops = {
1682 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +01001683 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +01001684 /* Virtually all GPIO irqchips are twocell:ed */
1685 .xlate = irq_domain_xlate_twocell,
1686};
1687
1688static int gpiochip_irq_reqres(struct irq_data *d)
1689{
1690 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1691
Linus Walleijff2b1352015-10-20 11:10:38 +02001692 if (!try_module_get(chip->gpiodev->owner))
Grygorii Strashko5b76e792015-06-25 20:30:50 +03001693 return -ENODEV;
1694
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001695 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +01001696 chip_err(chip,
1697 "unable to lock HW IRQ %lu for IRQ\n",
1698 d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001699 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001700 return -EINVAL;
1701 }
1702 return 0;
1703}
1704
1705static void gpiochip_irq_relres(struct irq_data *d)
1706{
1707 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1708
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001709 gpiochip_unlock_as_irq(chip, d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001710 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001711}
1712
1713static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
1714{
Grygorii Strashkodc749a02017-07-21 11:49:00 -05001715 if (!gpiochip_irqchip_irq_valid(chip, offset))
1716 return -ENXIO;
Thierry Redinge0d89722017-11-07 19:15:54 +01001717
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001718 return irq_create_mapping(chip->irq.domain, offset);
Linus Walleij14250522014-03-25 10:40:18 +01001719}
1720
1721/**
Thierry Redinge0d89722017-11-07 19:15:54 +01001722 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
1723 * @gpiochip: the GPIO chip to add the IRQ chip to
Thierry Reding959bc7b2017-11-07 19:15:59 +01001724 * @lock_key: lockdep class
Thierry Redinge0d89722017-11-07 19:15:54 +01001725 */
Thierry Reding959bc7b2017-11-07 19:15:59 +01001726static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
1727 struct lock_class_key *lock_key)
Thierry Redinge0d89722017-11-07 19:15:54 +01001728{
1729 struct irq_chip *irqchip = gpiochip->irq.chip;
1730 const struct irq_domain_ops *ops;
1731 struct device_node *np;
1732 unsigned int type;
1733 unsigned int i;
1734
1735 if (!irqchip)
1736 return 0;
1737
1738 if (gpiochip->irq.parent_handler && gpiochip->can_sleep) {
1739 chip_err(gpiochip, "you cannot have chained interrupts on a "
1740 "chip that may sleep\n");
1741 return -EINVAL;
1742 }
1743
1744 np = gpiochip->gpiodev->dev.of_node;
1745 type = gpiochip->irq.default_type;
1746
1747 /*
1748 * Specifying a default trigger is a terrible idea if DT or ACPI is
1749 * used to configure the interrupts, as you may end up with
1750 * conflicting triggers. Tell the user, and reset to NONE.
1751 */
1752 if (WARN(np && type != IRQ_TYPE_NONE,
1753 "%s: Ignoring %u default trigger\n", np->full_name, type))
1754 type = IRQ_TYPE_NONE;
1755
1756 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
1757 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1758 "Ignoring %u default trigger\n", type);
1759 type = IRQ_TYPE_NONE;
1760 }
1761
1762 gpiochip->to_irq = gpiochip_to_irq;
1763 gpiochip->irq.default_type = type;
Thierry Reding959bc7b2017-11-07 19:15:59 +01001764 gpiochip->irq.lock_key = lock_key;
Thierry Redinge0d89722017-11-07 19:15:54 +01001765
1766 if (gpiochip->irq.domain_ops)
1767 ops = gpiochip->irq.domain_ops;
1768 else
1769 ops = &gpiochip_domain_ops;
1770
1771 gpiochip->irq.domain = irq_domain_add_simple(np, gpiochip->ngpio,
Thierry Reding8302cf52017-11-07 19:15:58 +01001772 gpiochip->irq.first,
1773 ops, gpiochip);
Thierry Redinge0d89722017-11-07 19:15:54 +01001774 if (!gpiochip->irq.domain)
1775 return -EINVAL;
1776
1777 /*
1778 * It is possible for a driver to override this, but only if the
1779 * alternative functions are both implemented.
1780 */
1781 if (!irqchip->irq_request_resources &&
1782 !irqchip->irq_release_resources) {
1783 irqchip->irq_request_resources = gpiochip_irq_reqres;
1784 irqchip->irq_release_resources = gpiochip_irq_relres;
1785 }
1786
1787 if (gpiochip->irq.parent_handler) {
1788 void *data = gpiochip->irq.parent_handler_data ?: gpiochip;
1789
1790 for (i = 0; i < gpiochip->irq.num_parents; i++) {
1791 /*
1792 * The parent IRQ chip is already using the chip_data
1793 * for this IRQ chip, so our callbacks simply use the
1794 * handler_data.
1795 */
1796 irq_set_chained_handler_and_data(gpiochip->irq.parents[i],
1797 gpiochip->irq.parent_handler,
1798 data);
1799 }
Thierry Redinge0d89722017-11-07 19:15:54 +01001800 }
1801
1802 acpi_gpiochip_request_interrupts(gpiochip);
1803
1804 return 0;
1805}
1806
1807/**
Linus Walleij14250522014-03-25 10:40:18 +01001808 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1809 * @gpiochip: the gpiochip to remove the irqchip from
1810 *
1811 * This is called only from gpiochip_remove()
1812 */
1813static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
1814{
Thierry Reding39e5f092017-11-07 19:15:50 +01001815 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +01001816
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001817 acpi_gpiochip_free_interrupts(gpiochip);
1818
Thierry Redinge0d89722017-11-07 19:15:54 +01001819 if (gpiochip->irq.chip && gpiochip->irq.parent_handler) {
Thierry Reding39e5f092017-11-07 19:15:50 +01001820 struct gpio_irq_chip *irq = &gpiochip->irq;
1821 unsigned int i;
1822
1823 for (i = 0; i < irq->num_parents; i++)
1824 irq_set_chained_handler_and_data(irq->parents[i],
1825 NULL, NULL);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001826 }
1827
Linus Walleijc3626fd2014-03-28 20:42:01 +01001828 /* Remove all IRQ mappings and delete the domain */
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001829 if (gpiochip->irq.domain) {
Thierry Reding39e5f092017-11-07 19:15:50 +01001830 unsigned int irq;
1831
Mika Westerberg79b804c2016-09-20 15:15:21 +03001832 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1833 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1834 continue;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001835
1836 irq = irq_find_mapping(gpiochip->irq.domain, offset);
1837 irq_dispose_mapping(irq);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001838 }
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001839
1840 irq_domain_remove(gpiochip->irq.domain);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001841 }
Linus Walleij14250522014-03-25 10:40:18 +01001842
Thierry Redingda80ff82017-11-07 19:15:46 +01001843 if (gpiochip->irq.chip) {
1844 gpiochip->irq.chip->irq_request_resources = NULL;
1845 gpiochip->irq.chip->irq_release_resources = NULL;
1846 gpiochip->irq.chip = NULL;
Linus Walleij14250522014-03-25 10:40:18 +01001847 }
Mika Westerberg79b804c2016-09-20 15:15:21 +03001848
1849 gpiochip_irqchip_free_valid_mask(gpiochip);
Linus Walleij14250522014-03-25 10:40:18 +01001850}
1851
1852/**
Linus Walleij739e6f52017-01-11 13:37:07 +01001853 * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01001854 * @gpiochip: the gpiochip to add the irqchip to
1855 * @irqchip: the irqchip to add to the gpiochip
1856 * @first_irq: if not dynamically assigned, the base (first) IRQ to
1857 * allocate gpiochip irqs from
1858 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +02001859 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
1860 * to have the core avoid setting up any default type in the hardware.
Thierry Reding60ed54c2017-11-07 19:15:57 +01001861 * @threaded: whether this irqchip uses a nested thread handler
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001862 * @lock_key: lockdep class
Linus Walleij14250522014-03-25 10:40:18 +01001863 *
1864 * This function closely associates a certain irqchip with a certain
1865 * gpiochip, providing an irq domain to translate the local IRQs to
1866 * global irqs in the gpiolib core, and making sure that the gpiochip
1867 * is passed as chip data to all related functions. Driver callbacks
Linus Walleij09dd5f92015-12-07 15:31:58 +01001868 * need to use gpiochip_get_data() to get their local state containers back
Linus Walleij14250522014-03-25 10:40:18 +01001869 * from the gpiochip passed as chip data. An irqdomain will be stored
1870 * in the gpiochip that shall be used by the driver to handle IRQ number
1871 * translation. The gpiochip will need to be initialized and registered
1872 * before calling this function.
1873 *
Linus Walleijc3626fd2014-03-28 20:42:01 +01001874 * This function will handle two cell:ed simple IRQs and assumes all
1875 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +01001876 * need to be open coded.
1877 */
Linus Walleij739e6f52017-01-11 13:37:07 +01001878int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
1879 struct irq_chip *irqchip,
1880 unsigned int first_irq,
1881 irq_flow_handler_t handler,
1882 unsigned int type,
Thierry Reding60ed54c2017-11-07 19:15:57 +01001883 bool threaded,
Linus Walleij739e6f52017-01-11 13:37:07 +01001884 struct lock_class_key *lock_key)
Linus Walleij14250522014-03-25 10:40:18 +01001885{
1886 struct device_node *of_node;
Linus Walleij14250522014-03-25 10:40:18 +01001887
1888 if (!gpiochip || !irqchip)
1889 return -EINVAL;
1890
Linus Walleij58383c782015-11-04 09:56:26 +01001891 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +01001892 pr_err("missing gpiochip .dev parent pointer\n");
1893 return -EINVAL;
1894 }
Thierry Reding60ed54c2017-11-07 19:15:57 +01001895 gpiochip->irq.threaded = threaded;
Linus Walleij58383c782015-11-04 09:56:26 +01001896 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +01001897#ifdef CONFIG_OF_GPIO
1898 /*
Colin Cronin20a8a962015-05-18 11:41:43 -07001899 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08001900 * FIXME: get rid of this and use gpiochip->parent->of_node
1901 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +01001902 */
1903 if (gpiochip->of_node)
1904 of_node = gpiochip->of_node;
1905#endif
Marc Zyngier332e99d2016-09-07 09:12:11 +01001906 /*
Mika Westerberg0a1e0052016-09-12 14:29:51 +03001907 * Specifying a default trigger is a terrible idea if DT or ACPI is
Marc Zyngier332e99d2016-09-07 09:12:11 +01001908 * used to configure the interrupts, as you may end-up with
1909 * conflicting triggers. Tell the user, and reset to NONE.
1910 */
1911 if (WARN(of_node && type != IRQ_TYPE_NONE,
Rob Herring7eb6ce22017-07-18 16:43:03 -05001912 "%pOF: Ignoring %d default trigger\n", of_node, type))
Marc Zyngier332e99d2016-09-07 09:12:11 +01001913 type = IRQ_TYPE_NONE;
Mika Westerberg0a1e0052016-09-12 14:29:51 +03001914 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
1915 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1916 "Ignoring %d default trigger\n", type);
1917 type = IRQ_TYPE_NONE;
1918 }
Marc Zyngier332e99d2016-09-07 09:12:11 +01001919
Thierry Redingda80ff82017-11-07 19:15:46 +01001920 gpiochip->irq.chip = irqchip;
Thierry Redingc7a0aa52017-11-07 19:15:48 +01001921 gpiochip->irq.handler = handler;
Thierry Reding3634eeb2017-11-07 19:15:49 +01001922 gpiochip->irq.default_type = type;
Linus Walleij14250522014-03-25 10:40:18 +01001923 gpiochip->to_irq = gpiochip_to_irq;
Thierry Redingca9df052017-11-07 19:15:53 +01001924 gpiochip->irq.lock_key = lock_key;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001925 gpiochip->irq.domain = irq_domain_add_simple(of_node,
Linus Walleij14250522014-03-25 10:40:18 +01001926 gpiochip->ngpio, first_irq,
1927 &gpiochip_domain_ops, gpiochip);
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001928 if (!gpiochip->irq.domain) {
Thierry Redingda80ff82017-11-07 19:15:46 +01001929 gpiochip->irq.chip = NULL;
Linus Walleij14250522014-03-25 10:40:18 +01001930 return -EINVAL;
1931 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +02001932
1933 /*
1934 * It is possible for a driver to override this, but only if the
1935 * alternative functions are both implemented.
1936 */
1937 if (!irqchip->irq_request_resources &&
1938 !irqchip->irq_release_resources) {
1939 irqchip->irq_request_resources = gpiochip_irq_reqres;
1940 irqchip->irq_release_resources = gpiochip_irq_relres;
1941 }
Linus Walleij14250522014-03-25 10:40:18 +01001942
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001943 acpi_gpiochip_request_interrupts(gpiochip);
1944
Linus Walleij14250522014-03-25 10:40:18 +01001945 return 0;
1946}
Linus Walleij739e6f52017-01-11 13:37:07 +01001947EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key);
Linus Walleij14250522014-03-25 10:40:18 +01001948
1949#else /* CONFIG_GPIOLIB_IRQCHIP */
1950
Thierry Reding959bc7b2017-11-07 19:15:59 +01001951static inline int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
Linus Walleijec478732017-11-08 21:40:41 +01001952 struct lock_class_key *key)
Thierry Redinge0d89722017-11-07 19:15:54 +01001953{
1954 return 0;
1955}
1956
Linus Walleij14250522014-03-25 10:40:18 +01001957static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
Mika Westerberg79b804c2016-09-20 15:15:21 +03001958static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1959{
1960 return 0;
1961}
1962static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1963{ }
Linus Walleij14250522014-03-25 10:40:18 +01001964
1965#endif /* CONFIG_GPIOLIB_IRQCHIP */
1966
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001967/**
1968 * gpiochip_generic_request() - request the gpio function for a pin
1969 * @chip: the gpiochip owning the GPIO
1970 * @offset: the offset of the GPIO to request for GPIO function
1971 */
1972int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
1973{
Linus Walleija9a1d2a2017-09-22 11:02:10 +02001974 return pinctrl_gpio_request(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001975}
1976EXPORT_SYMBOL_GPL(gpiochip_generic_request);
1977
1978/**
1979 * gpiochip_generic_free() - free the gpio function from a pin
1980 * @chip: the gpiochip to request the gpio function for
1981 * @offset: the offset of the GPIO to free from GPIO function
1982 */
1983void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
1984{
Linus Walleija9a1d2a2017-09-22 11:02:10 +02001985 pinctrl_gpio_free(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001986}
1987EXPORT_SYMBOL_GPL(gpiochip_generic_free);
1988
Mika Westerberg2956b5d2017-01-23 15:34:34 +03001989/**
1990 * gpiochip_generic_config() - apply configuration for a pin
1991 * @chip: the gpiochip owning the GPIO
1992 * @offset: the offset of the GPIO to apply the configuration
1993 * @config: the configuration to be applied
1994 */
1995int gpiochip_generic_config(struct gpio_chip *chip, unsigned offset,
1996 unsigned long config)
1997{
1998 return pinctrl_gpio_set_config(chip->gpiodev->base + offset, config);
1999}
2000EXPORT_SYMBOL_GPL(gpiochip_generic_config);
2001
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302002#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01002003
Linus Walleij3f0f8672012-11-20 12:40:15 +01002004/**
Christian Ruppert586a87e2013-10-15 15:37:54 +02002005 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
2006 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +02002007 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +02002008 * @gpio_offset: the start offset in the current gpio_chip number space
2009 * @pin_group: name of the pin group inside the pin controller
2010 */
2011int gpiochip_add_pingroup_range(struct gpio_chip *chip,
2012 struct pinctrl_dev *pctldev,
2013 unsigned int gpio_offset, const char *pin_group)
2014{
2015 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002016 struct gpio_device *gdev = chip->gpiodev;
Christian Ruppert586a87e2013-10-15 15:37:54 +02002017 int ret;
2018
2019 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
2020 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002021 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +02002022 return -ENOMEM;
2023 }
2024
2025 /* Use local offset as range ID */
2026 pin_range->range.id = gpio_offset;
2027 pin_range->range.gc = chip;
2028 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002029 pin_range->range.base = gdev->base + gpio_offset;
Christian Ruppert586a87e2013-10-15 15:37:54 +02002030 pin_range->pctldev = pctldev;
2031
2032 ret = pinctrl_get_group_pins(pctldev, pin_group,
2033 &pin_range->range.pins,
2034 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +01002035 if (ret < 0) {
2036 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +02002037 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +01002038 }
Christian Ruppert586a87e2013-10-15 15:37:54 +02002039
2040 pinctrl_add_gpio_range(pctldev, &pin_range->range);
2041
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002042 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
2043 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +02002044 pinctrl_dev_get_devname(pctldev), pin_group);
2045
Linus Walleij20ec3e32016-02-11 11:03:06 +01002046 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Christian Ruppert586a87e2013-10-15 15:37:54 +02002047
2048 return 0;
2049}
2050EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
2051
2052/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01002053 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
2054 * @chip: the gpiochip to add the range for
Thierry Reding950d55f52017-07-24 16:57:22 +02002055 * @pinctl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01002056 * @gpio_offset: the start offset in the current gpio_chip number space
2057 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01002058 * @npins: the number of pins from the offset of each pin space (GPIO and
2059 * pin controller) to accumulate in this range
Thierry Reding950d55f52017-07-24 16:57:22 +02002060 *
2061 * Returns:
2062 * 0 on success, or a negative error-code on failure.
Linus Walleij3f0f8672012-11-20 12:40:15 +01002063 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002064int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01002065 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01002066 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302067{
2068 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002069 struct gpio_device *gdev = chip->gpiodev;
Axel Linb4d4b1f2012-11-21 14:33:56 +08002070 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302071
Linus Walleij3f0f8672012-11-20 12:40:15 +01002072 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302073 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002074 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002075 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302076 }
2077
Linus Walleij3f0f8672012-11-20 12:40:15 +01002078 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01002079 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01002080 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302081 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002082 pin_range->range.base = gdev->base + gpio_offset;
Linus Walleij316511c2012-11-21 08:48:09 +01002083 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302084 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01002085 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302086 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01002087 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08002088 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002089 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +01002090 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08002091 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01002092 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02002093 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
2094 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +01002095 pinctl_name,
2096 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302097
Linus Walleij20ec3e32016-02-11 11:03:06 +01002098 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01002099
2100 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302101}
Linus Walleij165adc92012-11-06 14:49:39 +01002102EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302103
Linus Walleij3f0f8672012-11-20 12:40:15 +01002104/**
2105 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
2106 * @chip: the chip to remove all the mappings for
2107 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302108void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
2109{
2110 struct gpio_pin_range *pin_range, *tmp;
Linus Walleij20ec3e32016-02-11 11:03:06 +01002111 struct gpio_device *gdev = chip->gpiodev;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302112
Linus Walleij20ec3e32016-02-11 11:03:06 +01002113 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302114 list_del(&pin_range->node);
2115 pinctrl_remove_gpio_range(pin_range->pctldev,
2116 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01002117 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302118 }
2119}
Linus Walleij165adc92012-11-06 14:49:39 +01002120EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
2121
2122#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302123
David Brownelld2876d02008-02-04 22:28:20 -08002124/* These "optional" allocation calls help prevent drivers from stomping
2125 * on each other, and help provide better diagnostics in debugfs.
2126 * They're called even less than the "set direction" calls.
2127 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002128static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08002129{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002130 struct gpio_chip *chip = desc->gdev->chip;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002131 int status;
David Brownelld2876d02008-02-04 22:28:20 -08002132 unsigned long flags;
2133
2134 spin_lock_irqsave(&gpio_lock, flags);
2135
David Brownelld2876d02008-02-04 22:28:20 -08002136 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07002137 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08002138 */
2139
2140 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
2141 desc_set_label(desc, label ? : "?");
2142 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07002143 } else {
David Brownelld2876d02008-02-04 22:28:20 -08002144 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -08002145 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07002146 }
2147
2148 if (chip->request) {
2149 /* chip->request may sleep */
2150 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002151 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07002152 spin_lock_irqsave(&gpio_lock, flags);
2153
2154 if (status < 0) {
2155 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -07002156 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03002157 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07002158 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07002159 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03002160 if (chip->get_direction) {
2161 /* chip->get_direction may sleep */
2162 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002163 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03002164 spin_lock_irqsave(&gpio_lock, flags);
2165 }
David Brownelld2876d02008-02-04 22:28:20 -08002166done:
Mika Westerberg77c2d792014-03-10 14:54:50 +02002167 spin_unlock_irqrestore(&gpio_lock, flags);
2168 return status;
2169}
2170
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002171/*
2172 * This descriptor validation needs to be inserted verbatim into each
2173 * function taking a descriptor, so we need to use a preprocessor
Linus Walleij54d77192016-05-30 16:48:39 +02002174 * macro to avoid endless duplication. If the desc is NULL it is an
2175 * optional GPIO and calls should just bail out.
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002176 */
2177#define VALIDATE_DESC(desc) do { \
Linus Walleij54d77192016-05-30 16:48:39 +02002178 if (!desc) \
2179 return 0; \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002180 if (IS_ERR(desc)) { \
2181 pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
2182 return PTR_ERR(desc); \
2183 } \
Linus Walleij54d77192016-05-30 16:48:39 +02002184 if (!desc->gdev) { \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002185 pr_warn("%s: invalid GPIO (no device)\n", __func__); \
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002186 return -EINVAL; \
2187 } \
2188 if ( !desc->gdev->chip ) { \
2189 dev_warn(&desc->gdev->dev, \
2190 "%s: backing chip is gone\n", __func__); \
2191 return 0; \
2192 } } while (0)
2193
2194#define VALIDATE_DESC_VOID(desc) do { \
Linus Walleij54d77192016-05-30 16:48:39 +02002195 if (!desc) \
2196 return; \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002197 if (IS_ERR(desc)) { \
2198 pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
2199 return; \
2200 } \
Linus Walleij54d77192016-05-30 16:48:39 +02002201 if (!desc->gdev) { \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002202 pr_warn("%s: invalid GPIO (no device)\n", __func__); \
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002203 return; \
2204 } \
2205 if (!desc->gdev->chip) { \
2206 dev_warn(&desc->gdev->dev, \
2207 "%s: backing chip is gone\n", __func__); \
2208 return; \
2209 } } while (0)
2210
2211
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002212int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002213{
2214 int status = -EPROBE_DEFER;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002215 struct gpio_device *gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002216
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002217 VALIDATE_DESC(desc);
2218 gdev = desc->gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002219
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002220 if (try_module_get(gdev->owner)) {
Linus Walleijfac9d882017-09-26 20:58:28 +02002221 status = gpiod_request_commit(desc, label);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002222 if (status < 0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002223 module_put(gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002224 else
2225 get_device(&gdev->dev);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002226 }
2227
David Brownelld2876d02008-02-04 22:28:20 -08002228 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +02002229 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002230
David Brownelld2876d02008-02-04 22:28:20 -08002231 return status;
2232}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002233
Linus Walleijfac9d882017-09-26 20:58:28 +02002234static bool gpiod_free_commit(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002235{
Mika Westerberg77c2d792014-03-10 14:54:50 +02002236 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08002237 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07002238 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002239
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07002240 might_sleep();
2241
Alexandre Courbot372e7222013-02-03 01:29:29 +09002242 gpiod_unexport(desc);
David Brownelld8f388d82008-07-25 01:46:07 -07002243
David Brownelld2876d02008-02-04 22:28:20 -08002244 spin_lock_irqsave(&gpio_lock, flags);
2245
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002246 chip = desc->gdev->chip;
David Brownell35e8bb52008-10-15 22:03:16 -07002247 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
2248 if (chip->free) {
2249 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07002250 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002251 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07002252 spin_lock_irqsave(&gpio_lock, flags);
2253 }
David Brownelld2876d02008-02-04 22:28:20 -08002254 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08002255 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07002256 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302257 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302258 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06002259 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002260 ret = true;
2261 }
David Brownelld2876d02008-02-04 22:28:20 -08002262
2263 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002264 return ret;
2265}
2266
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002267void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002268{
Linus Walleijfac9d882017-09-26 20:58:28 +02002269 if (desc && desc->gdev && gpiod_free_commit(desc)) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002270 module_put(desc->gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002271 put_device(&desc->gdev->dev);
2272 } else {
Mika Westerberg77c2d792014-03-10 14:54:50 +02002273 WARN_ON(extra_checks);
Linus Walleij33a68e82016-02-11 10:28:44 +01002274 }
David Brownelld2876d02008-02-04 22:28:20 -08002275}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002276
David Brownelld2876d02008-02-04 22:28:20 -08002277/**
2278 * gpiochip_is_requested - return string iff signal was requested
2279 * @chip: controller managing the signal
2280 * @offset: of signal within controller's 0..(ngpio - 1) range
2281 *
2282 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09002283 * The string returned is the label passed to gpio_request(); if none has been
2284 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08002285 *
2286 * This function is for use by GPIO controller drivers. The label can
2287 * help with diagnostics, and knowing that the signal is used as a GPIO
2288 * can help avoid accidentally multiplexing it to another controller.
2289 */
2290const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
2291{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002292 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08002293
Dirk Behme48b59532015-08-18 18:02:32 +02002294 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08002295 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002296
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002297 desc = &chip->gpiodev->descs[offset];
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002298
Alexandre Courbot372e7222013-02-03 01:29:29 +09002299 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08002300 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002301 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08002302}
2303EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2304
Mika Westerberg77c2d792014-03-10 14:54:50 +02002305/**
2306 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
Thierry Reding950d55f52017-07-24 16:57:22 +02002307 * @chip: GPIO chip
2308 * @hwnum: hardware number of the GPIO for which to request the descriptor
Mika Westerberg77c2d792014-03-10 14:54:50 +02002309 * @label: label for the GPIO
2310 *
2311 * Function allows GPIO chip drivers to request and use their own GPIO
2312 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2313 * function will not increase reference count of the GPIO chip module. This
2314 * allows the GPIO chip module to be unloaded as needed (we assume that the
2315 * GPIO chip driver handles freeing the GPIOs it has requested).
Thierry Reding950d55f52017-07-24 16:57:22 +02002316 *
2317 * Returns:
2318 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2319 * code on failure.
Mika Westerberg77c2d792014-03-10 14:54:50 +02002320 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002321struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
2322 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002323{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002324 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
2325 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002326
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002327 if (IS_ERR(desc)) {
2328 chip_err(chip, "failed to get GPIO descriptor\n");
2329 return desc;
2330 }
2331
Linus Walleijfac9d882017-09-26 20:58:28 +02002332 err = gpiod_request_commit(desc, label);
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002333 if (err < 0)
2334 return ERR_PTR(err);
2335
2336 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002337}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002338EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002339
2340/**
2341 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2342 * @desc: GPIO descriptor to free
2343 *
2344 * Function frees the given GPIO requested previously with
2345 * gpiochip_request_own_desc().
2346 */
2347void gpiochip_free_own_desc(struct gpio_desc *desc)
2348{
2349 if (desc)
Linus Walleijfac9d882017-09-26 20:58:28 +02002350 gpiod_free_commit(desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002351}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002352EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08002353
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002354/*
2355 * Drivers MUST set GPIO direction before making get/set calls. In
David Brownelld2876d02008-02-04 22:28:20 -08002356 * some cases this is done in early boot, before IRQs are enabled.
2357 *
2358 * As a rule these aren't called more than once (except for drivers
2359 * using the open-drain emulation idiom) so these are natural places
2360 * to accumulate extra debugging checks. Note that we can't (yet)
2361 * rely on gpio_request() having been called beforehand.
2362 */
2363
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002364/**
2365 * gpiod_direction_input - set the GPIO direction to input
2366 * @desc: GPIO to set to input
2367 *
2368 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2369 * be called safely on it.
2370 *
2371 * Return 0 in case of success, else an error code.
2372 */
2373int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002374{
David Brownelld2876d02008-02-04 22:28:20 -08002375 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002376 int status = -EINVAL;
2377
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002378 VALIDATE_DESC(desc);
2379 chip = desc->gdev->chip;
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002380
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002381 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01002382 gpiod_warn(desc,
2383 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02002384 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002385 return -EIO;
2386 }
2387
Alexandre Courbotd82da792014-07-22 16:17:43 +09002388 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -08002389 if (status == 0)
2390 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002391
Alexandre Courbot372e7222013-02-03 01:29:29 +09002392 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09002393
David Brownelld2876d02008-02-04 22:28:20 -08002394 return status;
2395}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002396EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002397
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002398static int gpio_set_drive_single_ended(struct gpio_chip *gc, unsigned offset,
2399 enum pin_config_param mode)
2400{
2401 unsigned long config = { PIN_CONF_PACKED(mode, 0) };
2402
2403 return gc->set_config ? gc->set_config(gc, offset, config) : -ENOTSUPP;
2404}
2405
Linus Walleijfac9d882017-09-26 20:58:28 +02002406static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08002407{
Linus Walleijc663e5f2016-03-22 10:51:16 +01002408 struct gpio_chip *gc = desc->gdev->chip;
Linus Walleijad177312016-11-13 23:02:44 +01002409 int val = !!value;
Linus Walleijc663e5f2016-03-22 10:51:16 +01002410 int ret;
David Brownelld2876d02008-02-04 22:28:20 -08002411
Linus Walleijc663e5f2016-03-22 10:51:16 +01002412 if (!gc->set || !gc->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01002413 gpiod_warn(desc,
2414 "%s: missing set() or direction_output() operations\n",
2415 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002416 return -EIO;
2417 }
2418
Linus Walleijad177312016-11-13 23:02:44 +01002419 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
Linus Walleijc663e5f2016-03-22 10:51:16 +01002420 if (!ret)
David Brownelld2876d02008-02-04 22:28:20 -08002421 set_bit(FLAG_IS_OUT, &desc->flags);
Linus Walleijad177312016-11-13 23:02:44 +01002422 trace_gpio_value(desc_to_gpio(desc), 0, val);
Linus Walleijc663e5f2016-03-22 10:51:16 +01002423 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2424 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08002425}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002426
2427/**
2428 * gpiod_direction_output_raw - set the GPIO direction to output
2429 * @desc: GPIO to set to output
2430 * @value: initial output value of the GPIO
2431 *
2432 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2433 * be called safely on it. The initial value of the output must be specified
2434 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2435 *
2436 * Return 0 in case of success, else an error code.
2437 */
2438int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2439{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002440 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02002441 return gpiod_direction_output_raw_commit(desc, value);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002442}
2443EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2444
2445/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05302446 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002447 * @desc: GPIO to set to output
2448 * @value: initial output value of the GPIO
2449 *
2450 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2451 * be called safely on it. The initial value of the output must be specified
2452 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2453 * account.
2454 *
2455 * Return 0 in case of success, else an error code.
2456 */
2457int gpiod_direction_output(struct gpio_desc *desc, int value)
2458{
Linus Walleij02e47982017-09-26 21:20:23 +02002459 struct gpio_chip *gc = desc->gdev->chip;
2460 int ret;
2461
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002462 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002463 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2464 value = !value;
Linus Walleijad177312016-11-13 23:02:44 +01002465 else
2466 value = !!value;
Linus Walleij02e47982017-09-26 21:20:23 +02002467
2468 /* GPIOs used for IRQs shall not be set as output */
2469 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
2470 gpiod_err(desc,
2471 "%s: tried to set a GPIO tied to an IRQ as output\n",
2472 __func__);
2473 return -EIO;
2474 }
2475
2476 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2477 /* First see if we can enable open drain in hardware */
2478 ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2479 PIN_CONFIG_DRIVE_OPEN_DRAIN);
2480 if (!ret)
2481 goto set_output_value;
2482 /* Emulate open drain by not actively driving the line high */
2483 if (value)
2484 return gpiod_direction_input(desc);
2485 }
2486 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2487 ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2488 PIN_CONFIG_DRIVE_OPEN_SOURCE);
2489 if (!ret)
2490 goto set_output_value;
2491 /* Emulate open source by not actively driving the line low */
2492 if (!value)
2493 return gpiod_direction_input(desc);
2494 } else {
2495 gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2496 PIN_CONFIG_DRIVE_PUSH_PULL);
2497 }
2498
2499set_output_value:
Linus Walleijfac9d882017-09-26 20:58:28 +02002500 return gpiod_direction_output_raw_commit(desc, value);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002501}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002502EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08002503
Felipe Balbic4b5be92010-05-26 14:42:23 -07002504/**
Thierry Reding950d55f52017-07-24 16:57:22 +02002505 * gpiod_set_debounce - sets @debounce time for a GPIO
2506 * @desc: descriptor of the GPIO for which to set debounce time
2507 * @debounce: debounce time in microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02002508 *
Thierry Reding950d55f52017-07-24 16:57:22 +02002509 * Returns:
2510 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2511 * debounce time.
Felipe Balbic4b5be92010-05-26 14:42:23 -07002512 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002513int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07002514{
Felipe Balbic4b5be92010-05-26 14:42:23 -07002515 struct gpio_chip *chip;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002516 unsigned long config;
Felipe Balbic4b5be92010-05-26 14:42:23 -07002517
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002518 VALIDATE_DESC(desc);
2519 chip = desc->gdev->chip;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002520 if (!chip->set || !chip->set_config) {
Mark Brown6424de52013-09-09 10:33:49 +01002521 gpiod_dbg(desc,
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002522 "%s: missing set() or set_config() operations\n",
Mark Brown6424de52013-09-09 10:33:49 +01002523 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02002524 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002525 }
2526
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002527 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
2528 return chip->set_config(chip, gpio_chip_hwgpio(desc), config);
Felipe Balbic4b5be92010-05-26 14:42:23 -07002529}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002530EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002531
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002532/**
2533 * gpiod_is_active_low - test whether a GPIO is active-low or not
2534 * @desc: the gpio descriptor to test
2535 *
2536 * Returns 1 if the GPIO is active-low, 0 otherwise.
2537 */
2538int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002539{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002540 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002541 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002542}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002543EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08002544
2545/* I/O calls are only valid after configuration completed; the relevant
2546 * "is this a valid GPIO" error checks should already have been done.
2547 *
2548 * "Get" operations are often inlinable as reading a pin value register,
2549 * and masking the relevant bit in that register.
2550 *
2551 * When "set" operations are inlinable, they involve writing that mask to
2552 * one register to set a low value, or a different register to set it high.
2553 * Otherwise locking is needed, so there may be little value to inlining.
2554 *
2555 *------------------------------------------------------------------------
2556 *
2557 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2558 * have requested the GPIO. That can include implicit requesting by
2559 * a direction setting call. Marking a gpio as requested locks its chip
2560 * in memory, guaranteeing that these table lookups need no more locking
2561 * and that gpiochip_remove() will fail.
2562 *
2563 * REVISIT when debugging, consider adding some instrumentation to ensure
2564 * that the GPIO was actually requested.
2565 */
2566
Linus Walleijfac9d882017-09-26 20:58:28 +02002567static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002568{
2569 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002570 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002571 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002572
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002573 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002574 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002575 value = chip->get ? chip->get(chip, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01002576 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002577 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002578 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002579}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002580
Lukas Wunnereec1d562017-10-12 12:40:10 +02002581static int gpio_chip_get_multiple(struct gpio_chip *chip,
2582 unsigned long *mask, unsigned long *bits)
2583{
2584 if (chip->get_multiple) {
2585 return chip->get_multiple(chip, mask, bits);
2586 } else if (chip->get) {
2587 int i, value;
2588
2589 for_each_set_bit(i, mask, chip->ngpio) {
2590 value = chip->get(chip, i);
2591 if (value < 0)
2592 return value;
2593 __assign_bit(i, bits, value);
2594 }
2595 return 0;
2596 }
2597 return -EIO;
2598}
2599
2600int gpiod_get_array_value_complex(bool raw, bool can_sleep,
2601 unsigned int array_size,
2602 struct gpio_desc **desc_array,
2603 int *value_array)
2604{
2605 int i = 0;
2606
2607 while (i < array_size) {
2608 struct gpio_chip *chip = desc_array[i]->gdev->chip;
2609 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2610 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2611 int first, j, ret;
2612
2613 if (!can_sleep)
2614 WARN_ON(chip->can_sleep);
2615
2616 /* collect all inputs belonging to the same chip */
2617 first = i;
2618 memset(mask, 0, sizeof(mask));
2619 do {
2620 const struct gpio_desc *desc = desc_array[i];
2621 int hwgpio = gpio_chip_hwgpio(desc);
2622
2623 __set_bit(hwgpio, mask);
2624 i++;
2625 } while ((i < array_size) &&
2626 (desc_array[i]->gdev->chip == chip));
2627
2628 ret = gpio_chip_get_multiple(chip, mask, bits);
2629 if (ret)
2630 return ret;
2631
2632 for (j = first; j < i; j++) {
2633 const struct gpio_desc *desc = desc_array[j];
2634 int hwgpio = gpio_chip_hwgpio(desc);
2635 int value = test_bit(hwgpio, bits);
2636
2637 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2638 value = !value;
2639 value_array[j] = value;
2640 trace_gpio_value(desc_to_gpio(desc), 1, value);
2641 }
2642 }
2643 return 0;
2644}
2645
David Brownelld2876d02008-02-04 22:28:20 -08002646/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002647 * gpiod_get_raw_value() - return a gpio's raw value
2648 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08002649 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002650 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002651 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002652 *
2653 * This function should be called from contexts where we cannot sleep, and will
2654 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002655 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002656int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002657{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002658 VALIDATE_DESC(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002659 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002660 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02002661 return gpiod_get_raw_value_commit(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002662}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002663EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002664
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002665/**
2666 * gpiod_get_value() - return a gpio's value
2667 * @desc: gpio whose value will be returned
2668 *
2669 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002670 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002671 *
2672 * This function should be called from contexts where we cannot sleep, and will
2673 * complain if the GPIO chip functions potentially sleep.
2674 */
2675int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002676{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002677 int value;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002678
2679 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002680 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002681 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002682
Linus Walleijfac9d882017-09-26 20:58:28 +02002683 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002684 if (value < 0)
2685 return value;
2686
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002687 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2688 value = !value;
2689
2690 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002691}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002692EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08002693
Lukas Wunnereec1d562017-10-12 12:40:10 +02002694/**
2695 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
2696 * @array_size: number of elements in the descriptor / value arrays
2697 * @desc_array: array of GPIO descriptors whose values will be read
2698 * @value_array: array to store the read values
2699 *
2700 * Read the raw values of the GPIOs, i.e. the values of the physical lines
2701 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
2702 * else an error code.
2703 *
2704 * This function should be called from contexts where we cannot sleep,
2705 * and it will complain if the GPIO chip functions potentially sleep.
2706 */
2707int gpiod_get_raw_array_value(unsigned int array_size,
2708 struct gpio_desc **desc_array, int *value_array)
2709{
2710 if (!desc_array)
2711 return -EINVAL;
2712 return gpiod_get_array_value_complex(true, false, array_size,
2713 desc_array, value_array);
2714}
2715EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
2716
2717/**
2718 * gpiod_get_array_value() - read values from an array of GPIOs
2719 * @array_size: number of elements in the descriptor / value arrays
2720 * @desc_array: array of GPIO descriptors whose values will be read
2721 * @value_array: array to store the read values
2722 *
2723 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2724 * into account. Return 0 in case of success, else an error code.
2725 *
2726 * This function should be called from contexts where we cannot sleep,
2727 * and it will complain if the GPIO chip functions potentially sleep.
2728 */
2729int gpiod_get_array_value(unsigned int array_size,
2730 struct gpio_desc **desc_array, int *value_array)
2731{
2732 if (!desc_array)
2733 return -EINVAL;
2734 return gpiod_get_array_value_complex(false, false, array_size,
2735 desc_array, value_array);
2736}
2737EXPORT_SYMBOL_GPL(gpiod_get_array_value);
2738
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302739/*
Linus Walleijfac9d882017-09-26 20:58:28 +02002740 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002741 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002742 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302743 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002744static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302745{
2746 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002747 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002748 int offset = gpio_chip_hwgpio(desc);
2749
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302750 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002751 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302752 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002753 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302754 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002755 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302756 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002757 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302758 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002759 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302760 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002761 gpiod_err(desc,
2762 "%s: Error in set_value for open drain err %d\n",
2763 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302764}
2765
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302766/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002767 * _gpio_set_open_source_value() - Set the open source gpio's value.
2768 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002769 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302770 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002771static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302772{
2773 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002774 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002775 int offset = gpio_chip_hwgpio(desc);
2776
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302777 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002778 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302779 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002780 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302781 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002782 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302783 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002784 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302785 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002786 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302787 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002788 gpiod_err(desc,
2789 "%s: Error in set_value for open source err %d\n",
2790 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302791}
2792
Linus Walleijfac9d882017-09-26 20:58:28 +02002793static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08002794{
2795 struct gpio_chip *chip;
2796
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002797 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002798 trace_gpio_value(desc_to_gpio(desc), 0, value);
Linus Walleij02e47982017-09-26 21:20:23 +02002799 chip->set(chip, gpio_chip_hwgpio(desc), value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002800}
2801
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002802/*
2803 * set multiple outputs on the same chip;
2804 * use the chip's set_multiple function if available;
2805 * otherwise set the outputs sequentially;
2806 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
2807 * defines which outputs are to be changed
2808 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
2809 * defines the values the outputs specified by mask are to be set to
2810 */
2811static void gpio_chip_set_multiple(struct gpio_chip *chip,
2812 unsigned long *mask, unsigned long *bits)
2813{
2814 if (chip->set_multiple) {
2815 chip->set_multiple(chip, mask, bits);
2816 } else {
Andy Shevchenko5e4e6fb2017-01-03 19:01:17 +02002817 unsigned int i;
2818
2819 /* set outputs if the corresponding mask bit is set */
2820 for_each_set_bit(i, mask, chip->ngpio)
2821 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002822 }
2823}
2824
Linus Walleij44c72882016-04-24 11:36:59 +02002825void gpiod_set_array_value_complex(bool raw, bool can_sleep,
2826 unsigned int array_size,
2827 struct gpio_desc **desc_array,
2828 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002829{
2830 int i = 0;
2831
2832 while (i < array_size) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002833 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002834 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2835 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2836 int count = 0;
2837
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002838 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002839 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002840
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002841 memset(mask, 0, sizeof(mask));
2842 do {
2843 struct gpio_desc *desc = desc_array[i];
2844 int hwgpio = gpio_chip_hwgpio(desc);
2845 int value = value_array[i];
2846
2847 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2848 value = !value;
2849 trace_gpio_value(desc_to_gpio(desc), 0, value);
2850 /*
2851 * collect all normal outputs belonging to the same chip
2852 * open drain and open source outputs are set individually
2853 */
Linus Walleij02e47982017-09-26 21:20:23 +02002854 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02002855 gpio_set_open_drain_value_commit(desc, value);
Linus Walleij02e47982017-09-26 21:20:23 +02002856 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02002857 gpio_set_open_source_value_commit(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002858 } else {
2859 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002860 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002861 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002862 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002863 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002864 count++;
2865 }
2866 i++;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002867 } while ((i < array_size) &&
2868 (desc_array[i]->gdev->chip == chip));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002869 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002870 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002871 gpio_chip_set_multiple(chip, mask, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002872 }
2873}
2874
David Brownelld2876d02008-02-04 22:28:20 -08002875/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002876 * gpiod_set_raw_value() - assign a gpio's raw value
2877 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08002878 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002879 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002880 * Set the raw value of the GPIO, i.e. the value of its physical line without
2881 * regard for its ACTIVE_LOW status.
2882 *
2883 * This function should be called from contexts where we cannot sleep, and will
2884 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002885 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002886void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002887{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002888 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01002889 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002890 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02002891 gpiod_set_raw_value_commit(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002892}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002893EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002894
2895/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002896 * gpiod_set_value() - assign a gpio's value
2897 * @desc: gpio whose value will be assigned
2898 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002899 *
Linus Walleij02e47982017-09-26 21:20:23 +02002900 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
2901 * OPEN_DRAIN and OPEN_SOURCE flags into account.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002902 *
2903 * This function should be called from contexts where we cannot sleep, and will
2904 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002905 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002906void gpiod_set_value(struct gpio_desc *desc, int value)
2907{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002908 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01002909 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002910 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002911 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2912 value = !value;
Linus Walleij02e47982017-09-26 21:20:23 +02002913 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2914 gpio_set_open_drain_value_commit(desc, value);
2915 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2916 gpio_set_open_source_value_commit(desc, value);
2917 else
2918 gpiod_set_raw_value_commit(desc, value);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002919}
2920EXPORT_SYMBOL_GPL(gpiod_set_value);
2921
2922/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002923 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002924 * @array_size: number of elements in the descriptor / value arrays
2925 * @desc_array: array of GPIO descriptors whose values will be assigned
2926 * @value_array: array of values to assign
2927 *
2928 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2929 * without regard for their ACTIVE_LOW status.
2930 *
2931 * This function should be called from contexts where we cannot sleep, and will
2932 * complain if the GPIO chip functions potentially sleep.
2933 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002934void gpiod_set_raw_array_value(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002935 struct gpio_desc **desc_array, int *value_array)
2936{
2937 if (!desc_array)
2938 return;
Linus Walleij44c72882016-04-24 11:36:59 +02002939 gpiod_set_array_value_complex(true, false, array_size, desc_array,
2940 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002941}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002942EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002943
2944/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002945 * gpiod_set_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002946 * @array_size: number of elements in the descriptor / value arrays
2947 * @desc_array: array of GPIO descriptors whose values will be assigned
2948 * @value_array: array of values to assign
2949 *
2950 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2951 * into account.
2952 *
2953 * This function should be called from contexts where we cannot sleep, and will
2954 * complain if the GPIO chip functions potentially sleep.
2955 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002956void gpiod_set_array_value(unsigned int array_size,
2957 struct gpio_desc **desc_array, int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002958{
2959 if (!desc_array)
2960 return;
Linus Walleij44c72882016-04-24 11:36:59 +02002961 gpiod_set_array_value_complex(false, false, array_size, desc_array,
2962 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002963}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002964EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002965
2966/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002967 * gpiod_cansleep() - report whether gpio value access may sleep
2968 * @desc: gpio to check
2969 *
2970 */
2971int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002972{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002973 VALIDATE_DESC(desc);
2974 return desc->gdev->chip->can_sleep;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002975}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002976EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002977
David Brownell0f6d5042008-10-15 22:03:14 -07002978/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002979 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
2980 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07002981 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002982 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
2983 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07002984 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002985int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07002986{
Linus Walleij4c37ce82016-05-02 13:13:10 +02002987 struct gpio_chip *chip;
2988 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07002989
Linus Walleij79bb71b2016-06-15 22:57:38 +02002990 /*
2991 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
2992 * requires this function to not return zero on an invalid descriptor
2993 * but rather a negative error number.
2994 */
Linus Walleijbfbbe442016-06-16 11:55:55 +02002995 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
Linus Walleij79bb71b2016-06-15 22:57:38 +02002996 return -EINVAL;
2997
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002998 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002999 offset = gpio_chip_hwgpio(desc);
Linus Walleij4c37ce82016-05-02 13:13:10 +02003000 if (chip->to_irq) {
3001 int retirq = chip->to_irq(chip, offset);
3002
3003 /* Zero means NO_IRQ */
3004 if (!retirq)
3005 return -ENXIO;
3006
3007 return retirq;
3008 }
3009 return -ENXIO;
Alexandre Courbot372e7222013-02-03 01:29:29 +09003010}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003011EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003012
Linus Walleijd468bf92013-09-24 11:54:38 +02003013/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003014 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003015 * @chip: the chip the GPIO to lock belongs to
3016 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02003017 *
3018 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08003019 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08003020 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003021int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08003022{
Linus Walleij9c102802016-05-25 10:56:03 +02003023 struct gpio_desc *desc;
Linus Walleijd468bf92013-09-24 11:54:38 +02003024
Linus Walleij9c102802016-05-25 10:56:03 +02003025 desc = gpiochip_get_desc(chip, offset);
3026 if (IS_ERR(desc))
3027 return PTR_ERR(desc);
3028
Linus Walleij60f83392016-11-12 15:01:09 +01003029 /*
3030 * If it's fast: flush the direction setting if something changed
3031 * behind our back
3032 */
3033 if (!chip->can_sleep && chip->get_direction) {
Linus Walleij9c102802016-05-25 10:56:03 +02003034 int dir = chip->get_direction(chip, offset);
3035
3036 if (dir)
3037 clear_bit(FLAG_IS_OUT, &desc->flags);
3038 else
3039 set_bit(FLAG_IS_OUT, &desc->flags);
3040 }
3041
3042 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003043 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02003044 "%s: tried to flag a GPIO set as output for IRQ\n",
3045 __func__);
3046 return -EIO;
3047 }
3048
Linus Walleij9c102802016-05-25 10:56:03 +02003049 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
Linus Walleij3940c342016-11-14 00:09:07 +01003050
3051 /*
3052 * If the consumer has not set up a label (such as when the
3053 * IRQ is referenced from .to_irq()) we set up a label here
3054 * so it is clear this is used as an interrupt.
3055 */
3056 if (!desc->label)
3057 desc_set_label(desc, "interrupt");
3058
Linus Walleijd468bf92013-09-24 11:54:38 +02003059 return 0;
3060}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003061EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02003062
Linus Walleijd468bf92013-09-24 11:54:38 +02003063/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003064 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09003065 * @chip: the chip the GPIO to lock belongs to
3066 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02003067 *
3068 * This is used directly by GPIO drivers that want to indicate
3069 * that a certain GPIO is no longer used exclusively for IRQ.
3070 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003071void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02003072{
Linus Walleij3940c342016-11-14 00:09:07 +01003073 struct gpio_desc *desc;
3074
3075 desc = gpiochip_get_desc(chip, offset);
3076 if (IS_ERR(desc))
Linus Walleijd468bf92013-09-24 11:54:38 +02003077 return;
3078
Linus Walleij3940c342016-11-14 00:09:07 +01003079 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
3080
3081 /* If we only had this marking, erase it */
3082 if (desc->label && !strcmp(desc->label, "interrupt"))
3083 desc_set_label(desc, NULL);
Linus Walleijd468bf92013-09-24 11:54:38 +02003084}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09003085EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02003086
Linus Walleij6cee3822016-02-11 20:16:45 +01003087bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
3088{
3089 if (offset >= chip->ngpio)
3090 return false;
3091
3092 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
3093}
3094EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
3095
Linus Walleij143b65d2016-02-16 15:41:42 +01003096bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
3097{
3098 if (offset >= chip->ngpio)
3099 return false;
3100
3101 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
3102}
3103EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
3104
3105bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
3106{
3107 if (offset >= chip->ngpio)
3108 return false;
3109
3110 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
3111}
3112EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
3113
Charles Keepax05f479b2017-05-23 15:47:29 +01003114bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset)
3115{
3116 if (offset >= chip->ngpio)
3117 return false;
3118
Andrew Jeffery2cbfca62017-10-20 13:27:58 +10303119 return !test_bit(FLAG_SLEEP_MAY_LOSE_VALUE,
Charles Keepax05f479b2017-05-23 15:47:29 +01003120 &chip->gpiodev->descs[offset].flags);
3121}
3122EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
3123
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003124/**
3125 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
3126 * @desc: gpio whose value will be returned
3127 *
3128 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003129 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003130 *
3131 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003132 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003133int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08003134{
David Brownelld2876d02008-02-04 22:28:20 -08003135 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003136 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003137 return gpiod_get_raw_value_commit(desc);
David Brownelld2876d02008-02-04 22:28:20 -08003138}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003139EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003140
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003141/**
3142 * gpiod_get_value_cansleep() - return a gpio's value
3143 * @desc: gpio whose value will be returned
3144 *
3145 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003146 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003147 *
3148 * This function is to be called from contexts that can sleep.
3149 */
3150int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003151{
David Brownelld2876d02008-02-04 22:28:20 -08003152 int value;
David Brownelld2876d02008-02-04 22:28:20 -08003153
3154 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003155 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003156 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003157 if (value < 0)
3158 return value;
3159
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003160 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3161 value = !value;
3162
David Brownelld2876d02008-02-04 22:28:20 -08003163 return value;
3164}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003165EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003166
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003167/**
Lukas Wunnereec1d562017-10-12 12:40:10 +02003168 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
3169 * @array_size: number of elements in the descriptor / value arrays
3170 * @desc_array: array of GPIO descriptors whose values will be read
3171 * @value_array: array to store the read values
3172 *
3173 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3174 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3175 * else an error code.
3176 *
3177 * This function is to be called from contexts that can sleep.
3178 */
3179int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
3180 struct gpio_desc **desc_array,
3181 int *value_array)
3182{
3183 might_sleep_if(extra_checks);
3184 if (!desc_array)
3185 return -EINVAL;
3186 return gpiod_get_array_value_complex(true, true, array_size,
3187 desc_array, value_array);
3188}
3189EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
3190
3191/**
3192 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
3193 * @array_size: number of elements in the descriptor / value arrays
3194 * @desc_array: array of GPIO descriptors whose values will be read
3195 * @value_array: array to store the read values
3196 *
3197 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3198 * into account. Return 0 in case of success, else an error code.
3199 *
3200 * This function is to be called from contexts that can sleep.
3201 */
3202int gpiod_get_array_value_cansleep(unsigned int array_size,
3203 struct gpio_desc **desc_array,
3204 int *value_array)
3205{
3206 might_sleep_if(extra_checks);
3207 if (!desc_array)
3208 return -EINVAL;
3209 return gpiod_get_array_value_complex(false, true, array_size,
3210 desc_array, value_array);
3211}
3212EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
3213
3214/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003215 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
3216 * @desc: gpio whose value will be assigned
3217 * @value: value to assign
3218 *
3219 * Set the raw value of the GPIO, i.e. the value of its physical line without
3220 * regard for its ACTIVE_LOW status.
3221 *
3222 * This function is to be called from contexts that can sleep.
3223 */
3224void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003225{
David Brownelld2876d02008-02-04 22:28:20 -08003226 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003227 VALIDATE_DESC_VOID(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003228 gpiod_set_raw_value_commit(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003229}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003230EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003231
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003232/**
3233 * gpiod_set_value_cansleep() - assign a gpio's value
3234 * @desc: gpio whose value will be assigned
3235 * @value: value to assign
3236 *
3237 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3238 * account
3239 *
3240 * This function is to be called from contexts that can sleep.
3241 */
3242void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003243{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003244 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003245 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003246 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3247 value = !value;
Linus Walleijfac9d882017-09-26 20:58:28 +02003248 gpiod_set_raw_value_commit(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08003249}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003250EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08003251
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003252/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003253 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003254 * @array_size: number of elements in the descriptor / value arrays
3255 * @desc_array: array of GPIO descriptors whose values will be assigned
3256 * @value_array: array of values to assign
3257 *
3258 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3259 * without regard for their ACTIVE_LOW status.
3260 *
3261 * This function is to be called from contexts that can sleep.
3262 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003263void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
3264 struct gpio_desc **desc_array,
3265 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003266{
3267 might_sleep_if(extra_checks);
3268 if (!desc_array)
3269 return;
Linus Walleij44c72882016-04-24 11:36:59 +02003270 gpiod_set_array_value_complex(true, true, array_size, desc_array,
3271 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003272}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003273EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003274
3275/**
Dmitry Torokhov3946d182017-08-14 21:59:55 -07003276 * gpiod_add_lookup_tables() - register GPIO device consumers
3277 * @tables: list of tables of consumers to register
3278 * @n: number of tables in the list
3279 */
3280void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
3281{
3282 unsigned int i;
3283
3284 mutex_lock(&gpio_lookup_lock);
3285
3286 for (i = 0; i < n; i++)
3287 list_add_tail(&tables[i]->list, &gpio_lookup_list);
3288
3289 mutex_unlock(&gpio_lookup_lock);
3290}
3291
3292/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003293 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003294 * @array_size: number of elements in the descriptor / value arrays
3295 * @desc_array: array of GPIO descriptors whose values will be assigned
3296 * @value_array: array of values to assign
3297 *
3298 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3299 * into account.
3300 *
3301 * This function is to be called from contexts that can sleep.
3302 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003303void gpiod_set_array_value_cansleep(unsigned int array_size,
3304 struct gpio_desc **desc_array,
3305 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003306{
3307 might_sleep_if(extra_checks);
3308 if (!desc_array)
3309 return;
Linus Walleij44c72882016-04-24 11:36:59 +02003310 gpiod_set_array_value_complex(false, true, array_size, desc_array,
3311 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003312}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003313EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003314
3315/**
Alexandre Courbotad824782013-12-03 12:20:11 +09003316 * gpiod_add_lookup_table() - register GPIO device consumers
3317 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003318 */
Alexandre Courbotad824782013-12-03 12:20:11 +09003319void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003320{
3321 mutex_lock(&gpio_lookup_lock);
3322
Alexandre Courbotad824782013-12-03 12:20:11 +09003323 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003324
3325 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08003326}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02003327EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
David Brownelld2876d02008-02-04 22:28:20 -08003328
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05303329/**
3330 * gpiod_remove_lookup_table() - unregister GPIO device consumers
3331 * @table: table of consumers to unregister
3332 */
3333void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
3334{
3335 mutex_lock(&gpio_lookup_lock);
3336
3337 list_del(&table->list);
3338
3339 mutex_unlock(&gpio_lookup_lock);
3340}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02003341EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05303342
Alexandre Courbotad824782013-12-03 12:20:11 +09003343static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
3344{
3345 const char *dev_id = dev ? dev_name(dev) : NULL;
3346 struct gpiod_lookup_table *table;
3347
3348 mutex_lock(&gpio_lookup_lock);
3349
3350 list_for_each_entry(table, &gpio_lookup_list, list) {
3351 if (table->dev_id && dev_id) {
3352 /*
3353 * Valid strings on both ends, must be identical to have
3354 * a match
3355 */
3356 if (!strcmp(table->dev_id, dev_id))
3357 goto found;
3358 } else {
3359 /*
3360 * One of the pointers is NULL, so both must be to have
3361 * a match
3362 */
3363 if (dev_id == table->dev_id)
3364 goto found;
3365 }
3366 }
3367 table = NULL;
3368
3369found:
3370 mutex_unlock(&gpio_lookup_lock);
3371 return table;
3372}
3373
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003374static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09003375 unsigned int idx,
3376 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003377{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003378 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09003379 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003380 struct gpiod_lookup *p;
3381
Alexandre Courbotad824782013-12-03 12:20:11 +09003382 table = gpiod_find_lookup_table(dev);
3383 if (!table)
3384 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003385
Alexandre Courbotad824782013-12-03 12:20:11 +09003386 for (p = &table->table[0]; p->chip_label; p++) {
3387 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003388
Alexandre Courbotad824782013-12-03 12:20:11 +09003389 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003390 if (p->idx != idx)
3391 continue;
3392
Alexandre Courbotad824782013-12-03 12:20:11 +09003393 /* If the lookup entry has a con_id, require exact match */
3394 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
3395 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003396
Alexandre Courbotad824782013-12-03 12:20:11 +09003397 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003398
Alexandre Courbotad824782013-12-03 12:20:11 +09003399 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003400 dev_err(dev, "cannot find GPIO chip %s\n",
3401 p->chip_label);
3402 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003403 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003404
Alexandre Courbotad824782013-12-03 12:20:11 +09003405 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003406 dev_err(dev,
3407 "requested GPIO %d is out of range [0..%d] for chip %s\n",
3408 idx, chip->ngpio, chip->label);
3409 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09003410 }
3411
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09003412 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09003413 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003414
3415 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09003416 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003417
3418 return desc;
3419}
3420
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003421static int dt_gpio_count(struct device *dev, const char *con_id)
3422{
3423 int ret;
3424 char propname[32];
3425 unsigned int i;
3426
3427 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
3428 if (con_id)
3429 snprintf(propname, sizeof(propname), "%s-%s",
3430 con_id, gpio_suffixes[i]);
3431 else
3432 snprintf(propname, sizeof(propname), "%s",
3433 gpio_suffixes[i]);
3434
3435 ret = of_gpio_named_count(dev->of_node, propname);
Andy Shevchenko4033d4a2017-02-20 18:15:47 +02003436 if (ret > 0)
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003437 break;
3438 }
Andy Shevchenko4033d4a2017-02-20 18:15:47 +02003439 return ret ? ret : -ENOENT;
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003440}
3441
3442static int platform_gpio_count(struct device *dev, const char *con_id)
3443{
3444 struct gpiod_lookup_table *table;
3445 struct gpiod_lookup *p;
3446 unsigned int count = 0;
3447
3448 table = gpiod_find_lookup_table(dev);
3449 if (!table)
3450 return -ENOENT;
3451
3452 for (p = &table->table[0]; p->chip_label; p++) {
3453 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3454 (!con_id && !p->con_id))
3455 count++;
3456 }
3457 if (!count)
3458 return -ENOENT;
3459
3460 return count;
3461}
3462
3463/**
3464 * gpiod_count - return the number of GPIOs associated with a device / function
3465 * or -ENOENT if no GPIO has been assigned to the requested function
3466 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3467 * @con_id: function within the GPIO consumer
3468 */
3469int gpiod_count(struct device *dev, const char *con_id)
3470{
3471 int count = -ENOENT;
3472
3473 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
3474 count = dt_gpio_count(dev, con_id);
3475 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
3476 count = acpi_gpio_count(dev, con_id);
3477
3478 if (count < 0)
3479 count = platform_gpio_count(dev, con_id);
3480
3481 return count;
3482}
3483EXPORT_SYMBOL_GPL(gpiod_count);
3484
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003485/**
Thierry Reding08791622014-04-25 16:54:22 +02003486 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09003487 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003488 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003489 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003490 *
3491 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003492 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07003493 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003494 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003495struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003496 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003497{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003498 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003499}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003500EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003501
3502/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02003503 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
3504 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3505 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003506 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02003507 *
3508 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
3509 * the requested function it will return NULL. This is convenient for drivers
3510 * that need to handle optional GPIOs.
3511 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003512struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003513 const char *con_id,
3514 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003515{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003516 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003517}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003518EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003519
Benoit Parrotf625d462015-02-02 11:44:44 -06003520
3521/**
3522 * gpiod_configure_flags - helper function to configure a given GPIO
3523 * @desc: gpio whose value will be assigned
3524 * @con_id: function within the GPIO consumer
Johan Hovold85b03b32016-07-03 18:32:05 +02003525 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3526 * of_get_gpio_hog()
Benoit Parrotf625d462015-02-02 11:44:44 -06003527 * @dflags: gpiod_flags - optional GPIO initialization flags
3528 *
3529 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
3530 * requested function and/or index, or another IS_ERR() code if an error
3531 * occurred while trying to acquire the GPIO.
3532 */
Andy Shevchenkoc29fd9e2017-05-23 20:03:16 +03003533int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Johan Hovold85b03b32016-07-03 18:32:05 +02003534 unsigned long lflags, enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06003535{
3536 int status;
3537
Johan Hovold85b03b32016-07-03 18:32:05 +02003538 if (lflags & GPIO_ACTIVE_LOW)
3539 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
Linus Walleijf926dfc2017-09-10 19:26:22 +02003540
Johan Hovold85b03b32016-07-03 18:32:05 +02003541 if (lflags & GPIO_OPEN_DRAIN)
3542 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
Linus Walleijf926dfc2017-09-10 19:26:22 +02003543 else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) {
3544 /*
3545 * This enforces open drain mode from the consumer side.
3546 * This is necessary for some busses like I2C, but the lookup
3547 * should *REALLY* have specified them as open drain in the
3548 * first place, so print a little warning here.
3549 */
3550 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3551 gpiod_warn(desc,
3552 "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
3553 }
3554
Johan Hovold85b03b32016-07-03 18:32:05 +02003555 if (lflags & GPIO_OPEN_SOURCE)
3556 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Andrew Jeffery2cbfca62017-10-20 13:27:58 +10303557 if (lflags & GPIO_SLEEP_MAY_LOSE_VALUE)
3558 set_bit(FLAG_SLEEP_MAY_LOSE_VALUE, &desc->flags);
Johan Hovold85b03b32016-07-03 18:32:05 +02003559
Benoit Parrotf625d462015-02-02 11:44:44 -06003560 /* No particular flag request, return here... */
3561 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
3562 pr_debug("no flags found for %s\n", con_id);
3563 return 0;
3564 }
3565
3566 /* Process flags */
3567 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
3568 status = gpiod_direction_output(desc,
Linus Walleijad177312016-11-13 23:02:44 +01003569 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
Benoit Parrotf625d462015-02-02 11:44:44 -06003570 else
3571 status = gpiod_direction_input(desc);
3572
3573 return status;
3574}
3575
Thierry Reding29a1f2332014-04-25 17:10:06 +02003576/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003577 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02003578 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003579 * @con_id: function within the GPIO consumer
3580 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003581 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003582 *
3583 * This variant of gpiod_get() allows to access GPIOs other than the first
3584 * defined one for functions that define several GPIOs.
3585 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003586 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
3587 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07003588 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003589 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003590struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003591 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003592 unsigned int idx,
3593 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003594{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003595 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003596 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003597 enum gpio_lookup_flags lookupflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003598
3599 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
3600
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003601 if (dev) {
3602 /* Using device tree? */
3603 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
3604 dev_dbg(dev, "using device tree for GPIO lookup\n");
3605 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
3606 } else if (ACPI_COMPANION(dev)) {
3607 dev_dbg(dev, "using ACPI for GPIO lookup\n");
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +03003608 desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags);
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003609 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003610 }
3611
3612 /*
3613 * Either we are not using DT or ACPI, or their lookup did not return
3614 * a result. In that case, use platform lookup as a fallback.
3615 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003616 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04003617 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003618 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003619 }
3620
3621 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02003622 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003623 return desc;
3624 }
3625
3626 status = gpiod_request(desc, con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003627 if (status < 0)
3628 return ERR_PTR(status);
3629
Johan Hovold85b03b32016-07-03 18:32:05 +02003630 status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003631 if (status < 0) {
3632 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
3633 gpiod_put(desc);
3634 return ERR_PTR(status);
3635 }
3636
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003637 return desc;
3638}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003639EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003640
3641/**
Mika Westerberg40b73182014-10-21 13:33:59 +02003642 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
3643 * @fwnode: handle of the firmware node
3644 * @propname: name of the firmware property representing the GPIO
Boris Brezillon537b94d2017-02-02 14:53:11 +01003645 * @index: index of the GPIO to obtain in the consumer
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003646 * @dflags: GPIO initialization flags
Thierry Reding950d55f52017-07-24 16:57:22 +02003647 * @label: label to attach to the requested GPIO
Mika Westerberg40b73182014-10-21 13:33:59 +02003648 *
3649 * This function can be used for drivers that get their configuration
3650 * from firmware.
3651 *
3652 * Function properly finds the corresponding GPIO using whatever is the
3653 * underlying firmware interface and then makes sure that the GPIO
3654 * descriptor is requested before it is returned to the caller.
3655 *
Thierry Reding950d55f52017-07-24 16:57:22 +02003656 * Returns:
Andy Shevchenkoff213782017-02-28 17:03:12 +02003657 * On successful request the GPIO pin is configured in accordance with
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003658 * provided @dflags.
3659 *
Mika Westerberg40b73182014-10-21 13:33:59 +02003660 * In case of error an ERR_PTR() is returned.
3661 */
3662struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
Boris Brezillon537b94d2017-02-02 14:53:11 +01003663 const char *propname, int index,
Alexander Steinb2987d72017-01-12 17:39:24 +01003664 enum gpiod_flags dflags,
3665 const char *label)
Mika Westerberg40b73182014-10-21 13:33:59 +02003666{
3667 struct gpio_desc *desc = ERR_PTR(-ENODEV);
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003668 unsigned long lflags = 0;
Mika Westerberg40b73182014-10-21 13:33:59 +02003669 bool active_low = false;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003670 bool single_ended = false;
Laxman Dewangan4c0facd2017-04-06 19:05:52 +05303671 bool open_drain = false;
Mika Westerberg40b73182014-10-21 13:33:59 +02003672 int ret;
3673
3674 if (!fwnode)
3675 return ERR_PTR(-EINVAL);
3676
3677 if (is_of_node(fwnode)) {
3678 enum of_gpio_flags flags;
3679
Boris Brezillon537b94d2017-02-02 14:53:11 +01003680 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname,
3681 index, &flags);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003682 if (!IS_ERR(desc)) {
Mika Westerberg40b73182014-10-21 13:33:59 +02003683 active_low = flags & OF_GPIO_ACTIVE_LOW;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003684 single_ended = flags & OF_GPIO_SINGLE_ENDED;
Laxman Dewangan4c0facd2017-04-06 19:05:52 +05303685 open_drain = flags & OF_GPIO_OPEN_DRAIN;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003686 }
Mika Westerberg40b73182014-10-21 13:33:59 +02003687 } else if (is_acpi_node(fwnode)) {
3688 struct acpi_gpio_info info;
3689
Boris Brezillon537b94d2017-02-02 14:53:11 +01003690 desc = acpi_node_get_gpiod(fwnode, propname, index, &info);
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +03003691 if (!IS_ERR(desc)) {
Christophe RICARD52044722015-12-23 23:25:34 +01003692 active_low = info.polarity == GPIO_ACTIVE_LOW;
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +03003693 ret = acpi_gpio_update_gpiod_flags(&dflags, info.flags);
3694 if (ret)
3695 pr_debug("Override GPIO initialization flags\n");
3696 }
Mika Westerberg40b73182014-10-21 13:33:59 +02003697 }
3698
3699 if (IS_ERR(desc))
3700 return desc;
3701
Alexander Steinb2987d72017-01-12 17:39:24 +01003702 ret = gpiod_request(desc, label);
Johan Hovold85b03b32016-07-03 18:32:05 +02003703 if (ret)
3704 return ERR_PTR(ret);
3705
Mika Westerberg40b73182014-10-21 13:33:59 +02003706 if (active_low)
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003707 lflags |= GPIO_ACTIVE_LOW;
Mika Westerberg40b73182014-10-21 13:33:59 +02003708
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003709 if (single_ended) {
Laxman Dewangan4c0facd2017-04-06 19:05:52 +05303710 if (open_drain)
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003711 lflags |= GPIO_OPEN_DRAIN;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003712 else
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003713 lflags |= GPIO_OPEN_SOURCE;
3714 }
3715
3716 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
3717 if (ret < 0) {
3718 gpiod_put(desc);
3719 return ERR_PTR(ret);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003720 }
3721
Mika Westerberg40b73182014-10-21 13:33:59 +02003722 return desc;
3723}
3724EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
3725
3726/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02003727 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
3728 * function
3729 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3730 * @con_id: function within the GPIO consumer
3731 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003732 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02003733 *
3734 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
3735 * specified index was assigned to the requested function it will return NULL.
3736 * This is convenient for drivers that need to handle optional GPIOs.
3737 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003738struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02003739 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003740 unsigned int index,
3741 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003742{
3743 struct gpio_desc *desc;
3744
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003745 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003746 if (IS_ERR(desc)) {
3747 if (PTR_ERR(desc) == -ENOENT)
3748 return NULL;
3749 }
3750
3751 return desc;
3752}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003753EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003754
3755/**
Benoit Parrotf625d462015-02-02 11:44:44 -06003756 * gpiod_hog - Hog the specified GPIO desc given the provided flags
3757 * @desc: gpio whose value will be assigned
3758 * @name: gpio line name
3759 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3760 * of_get_gpio_hog()
3761 * @dflags: gpiod_flags - optional GPIO initialization flags
3762 */
3763int gpiod_hog(struct gpio_desc *desc, const char *name,
3764 unsigned long lflags, enum gpiod_flags dflags)
3765{
3766 struct gpio_chip *chip;
3767 struct gpio_desc *local_desc;
3768 int hwnum;
3769 int status;
3770
3771 chip = gpiod_to_chip(desc);
3772 hwnum = gpio_chip_hwgpio(desc);
3773
3774 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
3775 if (IS_ERR(local_desc)) {
Laxman Dewanganc31a5712016-03-11 19:13:21 +05303776 status = PTR_ERR(local_desc);
3777 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
3778 name, chip->label, hwnum, status);
3779 return status;
Benoit Parrotf625d462015-02-02 11:44:44 -06003780 }
3781
Johan Hovold85b03b32016-07-03 18:32:05 +02003782 status = gpiod_configure_flags(desc, name, lflags, dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -06003783 if (status < 0) {
Laxman Dewanganc31a5712016-03-11 19:13:21 +05303784 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed, %d\n",
3785 name, chip->label, hwnum, status);
Benoit Parrotf625d462015-02-02 11:44:44 -06003786 gpiochip_free_own_desc(desc);
3787 return status;
3788 }
3789
3790 /* Mark GPIO as hogged so it can be identified and removed later */
3791 set_bit(FLAG_IS_HOGGED, &desc->flags);
3792
3793 pr_info("GPIO line %d (%s) hogged as %s%s\n",
3794 desc_to_gpio(desc), name,
3795 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
3796 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
3797 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
3798
3799 return 0;
3800}
3801
3802/**
3803 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
3804 * @chip: gpio chip to act on
3805 *
3806 * This is only used by of_gpiochip_remove to free hogged gpios
3807 */
3808static void gpiochip_free_hogs(struct gpio_chip *chip)
3809{
3810 int id;
3811
3812 for (id = 0; id < chip->ngpio; id++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01003813 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
3814 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
Benoit Parrotf625d462015-02-02 11:44:44 -06003815 }
3816}
3817
3818/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003819 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
3820 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3821 * @con_id: function within the GPIO consumer
3822 * @flags: optional GPIO initialization flags
3823 *
3824 * This function acquires all the GPIOs defined under a given function.
3825 *
3826 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
3827 * no GPIO has been assigned to the requested function, or another IS_ERR()
3828 * code if an error occurred while trying to acquire the GPIOs.
3829 */
3830struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
3831 const char *con_id,
3832 enum gpiod_flags flags)
3833{
3834 struct gpio_desc *desc;
3835 struct gpio_descs *descs;
3836 int count;
3837
3838 count = gpiod_count(dev, con_id);
3839 if (count < 0)
3840 return ERR_PTR(count);
3841
3842 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
3843 GFP_KERNEL);
3844 if (!descs)
3845 return ERR_PTR(-ENOMEM);
3846
3847 for (descs->ndescs = 0; descs->ndescs < count; ) {
3848 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
3849 if (IS_ERR(desc)) {
3850 gpiod_put_array(descs);
3851 return ERR_CAST(desc);
3852 }
3853 descs->desc[descs->ndescs] = desc;
3854 descs->ndescs++;
3855 }
3856 return descs;
3857}
3858EXPORT_SYMBOL_GPL(gpiod_get_array);
3859
3860/**
3861 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
3862 * function
3863 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3864 * @con_id: function within the GPIO consumer
3865 * @flags: optional GPIO initialization flags
3866 *
3867 * This is equivalent to gpiod_get_array(), except that when no GPIO was
3868 * assigned to the requested function it will return NULL.
3869 */
3870struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
3871 const char *con_id,
3872 enum gpiod_flags flags)
3873{
3874 struct gpio_descs *descs;
3875
3876 descs = gpiod_get_array(dev, con_id, flags);
3877 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
3878 return NULL;
3879
3880 return descs;
3881}
3882EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
3883
3884/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003885 * gpiod_put - dispose of a GPIO descriptor
3886 * @desc: GPIO descriptor to dispose of
3887 *
3888 * No descriptor can be used after gpiod_put() has been called on it.
3889 */
3890void gpiod_put(struct gpio_desc *desc)
3891{
3892 gpiod_free(desc);
3893}
3894EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08003895
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003896/**
3897 * gpiod_put_array - dispose of multiple GPIO descriptors
3898 * @descs: struct gpio_descs containing an array of descriptors
3899 */
3900void gpiod_put_array(struct gpio_descs *descs)
3901{
3902 unsigned int i;
3903
3904 for (i = 0; i < descs->ndescs; i++)
3905 gpiod_put(descs->desc[i]);
3906
3907 kfree(descs);
3908}
3909EXPORT_SYMBOL_GPL(gpiod_put_array);
3910
Linus Walleij3c702e92015-10-21 15:29:53 +02003911static int __init gpiolib_dev_init(void)
3912{
3913 int ret;
3914
3915 /* Register GPIO sysfs bus */
3916 ret = bus_register(&gpio_bus_type);
3917 if (ret < 0) {
3918 pr_err("gpiolib: could not register GPIO bus type\n");
3919 return ret;
3920 }
3921
3922 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
3923 if (ret < 0) {
3924 pr_err("gpiolib: failed to allocate char dev region\n");
3925 bus_unregister(&gpio_bus_type);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07003926 } else {
3927 gpiolib_initialized = true;
3928 gpiochip_setup_devs();
Linus Walleij3c702e92015-10-21 15:29:53 +02003929 }
3930 return ret;
3931}
3932core_initcall(gpiolib_dev_init);
3933
David Brownelld2876d02008-02-04 22:28:20 -08003934#ifdef CONFIG_DEBUG_FS
3935
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003936static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
David Brownelld2876d02008-02-04 22:28:20 -08003937{
3938 unsigned i;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003939 struct gpio_chip *chip = gdev->chip;
3940 unsigned gpio = gdev->base;
3941 struct gpio_desc *gdesc = &gdev->descs[0];
David Brownelld2876d02008-02-04 22:28:20 -08003942 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02003943 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08003944
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003945 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02003946 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
3947 if (gdesc->name) {
3948 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
3949 gpio, gdesc->name);
3950 }
David Brownelld2876d02008-02-04 22:28:20 -08003951 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02003952 }
David Brownelld2876d02008-02-04 22:28:20 -08003953
Alexandre Courbot372e7222013-02-03 01:29:29 +09003954 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08003955 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02003956 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Markus Pargmannced433e2015-08-14 16:11:02 +02003957 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
3958 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08003959 is_out ? "out" : "in ",
3960 chip->get
3961 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02003962 : "? ",
3963 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08003964 seq_printf(s, "\n");
3965 }
3966}
3967
Thierry Redingf9c4a312012-04-12 13:26:01 +02003968static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08003969{
Grant Likely362432a2013-02-09 09:41:49 +00003970 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02003971 struct gpio_device *gdev = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003972 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003973
3974 s->private = "";
3975
Grant Likely362432a2013-02-09 09:41:49 +00003976 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003977 list_for_each_entry(gdev, &gpio_devices, list)
Grant Likely362432a2013-02-09 09:41:49 +00003978 if (index-- == 0) {
3979 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003980 return gdev;
Grant Likely362432a2013-02-09 09:41:49 +00003981 }
3982 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003983
3984 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003985}
3986
3987static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
3988{
Grant Likely362432a2013-02-09 09:41:49 +00003989 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02003990 struct gpio_device *gdev = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003991 void *ret = NULL;
3992
Grant Likely362432a2013-02-09 09:41:49 +00003993 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003994 if (list_is_last(&gdev->list, &gpio_devices))
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003995 ret = NULL;
3996 else
Linus Walleijff2b1352015-10-20 11:10:38 +02003997 ret = list_entry(gdev->list.next, struct gpio_device, list);
Grant Likely362432a2013-02-09 09:41:49 +00003998 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02003999
4000 s->private = "\n";
4001 ++*pos;
4002
4003 return ret;
4004}
4005
4006static void gpiolib_seq_stop(struct seq_file *s, void *v)
4007{
4008}
4009
4010static int gpiolib_seq_show(struct seq_file *s, void *v)
4011{
Linus Walleijff2b1352015-10-20 11:10:38 +02004012 struct gpio_device *gdev = v;
4013 struct gpio_chip *chip = gdev->chip;
4014 struct device *parent;
Thierry Redingf9c4a312012-04-12 13:26:01 +02004015
Linus Walleijff2b1352015-10-20 11:10:38 +02004016 if (!chip) {
4017 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
4018 dev_name(&gdev->dev));
4019 return 0;
4020 }
4021
4022 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
4023 dev_name(&gdev->dev),
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004024 gdev->base, gdev->base + gdev->ngpio - 1);
Linus Walleijff2b1352015-10-20 11:10:38 +02004025 parent = chip->parent;
4026 if (parent)
4027 seq_printf(s, ", parent: %s/%s",
4028 parent->bus ? parent->bus->name : "no-bus",
4029 dev_name(parent));
Thierry Redingf9c4a312012-04-12 13:26:01 +02004030 if (chip->label)
4031 seq_printf(s, ", %s", chip->label);
4032 if (chip->can_sleep)
4033 seq_printf(s, ", can sleep");
4034 seq_printf(s, ":\n");
4035
4036 if (chip->dbg_show)
4037 chip->dbg_show(s, chip);
4038 else
Linus Walleijfdeb8e12016-02-10 10:57:36 +01004039 gpiolib_dbg_show(s, gdev);
Thierry Redingf9c4a312012-04-12 13:26:01 +02004040
David Brownelld2876d02008-02-04 22:28:20 -08004041 return 0;
4042}
4043
Thierry Redingf9c4a312012-04-12 13:26:01 +02004044static const struct seq_operations gpiolib_seq_ops = {
4045 .start = gpiolib_seq_start,
4046 .next = gpiolib_seq_next,
4047 .stop = gpiolib_seq_stop,
4048 .show = gpiolib_seq_show,
4049};
4050
David Brownelld2876d02008-02-04 22:28:20 -08004051static int gpiolib_open(struct inode *inode, struct file *file)
4052{
Thierry Redingf9c4a312012-04-12 13:26:01 +02004053 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08004054}
4055
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004056static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02004057 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08004058 .open = gpiolib_open,
4059 .read = seq_read,
4060 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02004061 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08004062};
4063
4064static int __init gpiolib_debugfs_init(void)
4065{
4066 /* /sys/kernel/debug/gpio */
4067 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
4068 NULL, NULL, &gpiolib_operations);
4069 return 0;
4070}
4071subsys_initcall(gpiolib_debugfs_init);
4072
4073#endif /* DEBUG_FS */