blob: 44332b793718afe1092f5c1a1f42a088b52ab6fb [file] [log] [blame]
Andy Shevchenko923a6542017-05-25 16:08:38 +03001#include <linux/bitmap.h>
David Brownelld2876d02008-02-04 22:28:20 -08002#include <linux/kernel.h>
3#include <linux/module.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -07004#include <linux/interrupt.h>
David Brownelld2876d02008-02-04 22:28:20 -08005#include <linux/irq.h>
6#include <linux/spinlock.h>
Alexandre Courbot1a989d02013-02-03 01:29:24 +09007#include <linux/list.h>
David Brownelld8f388d82008-07-25 01:46:07 -07008#include <linux/device.h>
9#include <linux/err.h>
10#include <linux/debugfs.h>
11#include <linux/seq_file.h>
12#include <linux/gpio.h>
Anton Vorontsov391c9702010-06-08 07:48:17 -060013#include <linux/of_gpio.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -070014#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Rafael J. Wysocki7b199812013-11-11 22:41:56 +010016#include <linux/acpi.h>
Alexandre Courbot53e7cac2013-11-16 21:44:52 +090017#include <linux/gpio/driver.h>
Linus Walleij0a6d3152014-07-24 20:08:55 +020018#include <linux/gpio/machine.h>
Jonas Gorskic771c2f2015-10-11 17:34:15 +020019#include <linux/pinctrl/consumer.h>
Linus Walleij3c702e92015-10-21 15:29:53 +020020#include <linux/cdev.h>
21#include <linux/fs.h>
22#include <linux/uaccess.h>
Linus Walleij8b92e172016-05-27 14:24:04 +020023#include <linux/compat.h>
Linus Walleijd7c51b42016-04-26 10:35:29 +020024#include <linux/anon_inodes.h>
Lars-Peter Clausen953b9562016-10-24 13:59:15 +020025#include <linux/file.h>
Linus Walleij61f922d2016-06-02 11:30:15 +020026#include <linux/kfifo.h>
27#include <linux/poll.h>
28#include <linux/timekeeping.h>
Linus Walleij3c702e92015-10-21 15:29:53 +020029#include <uapi/linux/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080030
Mika Westerberg664e3e52014-01-08 12:40:54 +020031#include "gpiolib.h"
32
Uwe Kleine-König3f397c212011-05-20 00:40:19 -060033#define CREATE_TRACE_POINTS
34#include <trace/events/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080035
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070036/* Implementation infrastructure for GPIO interfaces.
David Brownelld2876d02008-02-04 22:28:20 -080037 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070038 * The GPIO programming interface allows for inlining speed-critical
39 * get/set operations for common cases, so that access to SOC-integrated
40 * GPIOs can sometimes cost only an instruction or two per bit.
David Brownelld2876d02008-02-04 22:28:20 -080041 */
42
43
44/* When debugging, extend minimal trust to callers and platform code.
45 * Also emit diagnostic messages that may help initial bringup, when
46 * board setup or driver bugs are most common.
47 *
48 * Otherwise, minimize overhead in what may be bitbanging codepaths.
49 */
50#ifdef DEBUG
51#define extra_checks 1
52#else
53#define extra_checks 0
54#endif
55
Linus Walleijff2b1352015-10-20 11:10:38 +020056/* Device and char device-related information */
57static DEFINE_IDA(gpio_ida);
Linus Walleij3c702e92015-10-21 15:29:53 +020058static dev_t gpio_devt;
59#define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
60static struct bus_type gpio_bus_type = {
61 .name = "gpio",
62};
Linus Walleijff2b1352015-10-20 11:10:38 +020063
David Brownelld2876d02008-02-04 22:28:20 -080064/* gpio_lock prevents conflicts during gpio_desc[] table updates.
65 * While any GPIO is requested, its gpio_chip is not removable;
66 * each GPIO's "requested" flag serves as a lock and refcount.
67 */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090068DEFINE_SPINLOCK(gpio_lock);
David Brownelld2876d02008-02-04 22:28:20 -080069
Alexandre Courbotbae48da2013-10-17 10:21:38 -070070static DEFINE_MUTEX(gpio_lookup_lock);
71static LIST_HEAD(gpio_lookup_list);
Linus Walleijff2b1352015-10-20 11:10:38 +020072LIST_HEAD(gpio_devices);
Johan Hovold6d867502015-05-04 17:23:25 +020073
74static void gpiochip_free_hogs(struct gpio_chip *chip);
Thierry Reding959bc7b2017-11-07 19:15:59 +010075static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
Andrew Lunn39c3fd52017-12-02 18:11:04 +010076 struct lock_class_key *lock_key,
77 struct lock_class_key *request_key);
Johan Hovold6d867502015-05-04 17:23:25 +020078static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
Mika Westerberg79b804c2016-09-20 15:15:21 +030079static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip);
80static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip);
Johan Hovold6d867502015-05-04 17:23:25 +020081
Guenter Roeck159f3cd2016-03-31 08:11:30 -070082static bool gpiolib_initialized;
Johan Hovold6d867502015-05-04 17:23:25 +020083
David Brownelld2876d02008-02-04 22:28:20 -080084static inline void desc_set_label(struct gpio_desc *d, const char *label)
85{
David Brownelld2876d02008-02-04 22:28:20 -080086 d->label = label;
David Brownelld2876d02008-02-04 22:28:20 -080087}
88
Alexandre Courbot372e7222013-02-03 01:29:29 +090089/**
Thierry Reding950d55f52017-07-24 16:57:22 +020090 * gpio_to_desc - Convert a GPIO number to its descriptor
91 * @gpio: global GPIO number
92 *
93 * Returns:
94 * The GPIO descriptor associated with the given GPIO, or %NULL if no GPIO
95 * with the given number exists in the system.
Alexandre Courbot372e7222013-02-03 01:29:29 +090096 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070097struct gpio_desc *gpio_to_desc(unsigned gpio)
Alexandre Courbot372e7222013-02-03 01:29:29 +090098{
Linus Walleijff2b1352015-10-20 11:10:38 +020099 struct gpio_device *gdev;
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900100 unsigned long flags;
101
102 spin_lock_irqsave(&gpio_lock, flags);
103
Linus Walleijff2b1352015-10-20 11:10:38 +0200104 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100105 if (gdev->base <= gpio &&
106 gdev->base + gdev->ngpio > gpio) {
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900107 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100108 return &gdev->descs[gpio - gdev->base];
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900109 }
110 }
111
112 spin_unlock_irqrestore(&gpio_lock, flags);
113
Alexandre Courbot0e9a5ed2014-12-02 23:15:05 +0900114 if (!gpio_is_valid(gpio))
115 WARN(1, "invalid GPIO %d\n", gpio);
116
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900117 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900118}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700119EXPORT_SYMBOL_GPL(gpio_to_desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900120
121/**
Thierry Reding950d55f52017-07-24 16:57:22 +0200122 * gpiochip_get_desc - get the GPIO descriptor corresponding to the given
123 * hardware number for this chip
124 * @chip: GPIO chip
125 * @hwnum: hardware number of the GPIO for this chip
126 *
127 * Returns:
128 * A pointer to the GPIO descriptor or %ERR_PTR(-EINVAL) if no GPIO exists
129 * in the given chip for the specified hardware number.
Linus Walleijd468bf92013-09-24 11:54:38 +0200130 */
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900131struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
132 u16 hwnum)
Linus Walleijd468bf92013-09-24 11:54:38 +0200133{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100134 struct gpio_device *gdev = chip->gpiodev;
135
136 if (hwnum >= gdev->ngpio)
Alexandre Courbotb7d0a282013-12-03 12:31:11 +0900137 return ERR_PTR(-EINVAL);
Linus Walleijd468bf92013-09-24 11:54:38 +0200138
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100139 return &gdev->descs[hwnum];
Linus Walleijd468bf92013-09-24 11:54:38 +0200140}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900141
142/**
Thierry Reding950d55f52017-07-24 16:57:22 +0200143 * desc_to_gpio - convert a GPIO descriptor to the integer namespace
144 * @desc: GPIO descriptor
145 *
Alexandre Courbot372e7222013-02-03 01:29:29 +0900146 * This should disappear in the future but is needed since we still
Thierry Reding950d55f52017-07-24 16:57:22 +0200147 * use GPIO numbers for error messages and sysfs nodes.
148 *
149 * Returns:
150 * The global GPIO number for the GPIO specified by its descriptor.
Alexandre Courbot372e7222013-02-03 01:29:29 +0900151 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700152int desc_to_gpio(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900153{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100154 return desc->gdev->base + (desc - &desc->gdev->descs[0]);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900155}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700156EXPORT_SYMBOL_GPL(desc_to_gpio);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900157
158
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700159/**
160 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
161 * @desc: descriptor to return the chip of
162 */
163struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900164{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100165 if (!desc || !desc->gdev || !desc->gdev->chip)
166 return NULL;
167 return desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900168}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700169EXPORT_SYMBOL_GPL(gpiod_to_chip);
David Brownelld2876d02008-02-04 22:28:20 -0800170
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700171/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
172static int gpiochip_find_base(int ngpio)
173{
Linus Walleijff2b1352015-10-20 11:10:38 +0200174 struct gpio_device *gdev;
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900175 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700176
Linus Walleijff2b1352015-10-20 11:10:38 +0200177 list_for_each_entry_reverse(gdev, &gpio_devices, list) {
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900178 /* found a free space? */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100179 if (gdev->base + gdev->ngpio <= base)
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900180 break;
181 else
182 /* nope, check the space right before the chip */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100183 base = gdev->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700184 }
185
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900186 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700187 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900188 return base;
189 } else {
190 pr_err("%s: cannot find free range\n", __func__);
191 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700192 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700193}
194
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700195/**
196 * gpiod_get_direction - return the current direction of a GPIO
197 * @desc: GPIO to get the direction of
198 *
199 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
200 *
201 * This function may sleep if gpiod_cansleep() is true.
202 */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900203int gpiod_get_direction(struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300204{
205 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900206 unsigned offset;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300207 int status = -EINVAL;
208
Alexandre Courbot372e7222013-02-03 01:29:29 +0900209 chip = gpiod_to_chip(desc);
210 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300211
212 if (!chip->get_direction)
213 return status;
214
Alexandre Courbot372e7222013-02-03 01:29:29 +0900215 status = chip->get_direction(chip, offset);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300216 if (status > 0) {
217 /* GPIOF_DIR_IN, or other positive */
218 status = 1;
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900219 clear_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300220 }
221 if (status == 0) {
222 /* GPIOF_DIR_OUT */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900223 set_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300224 }
225 return status;
226}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700227EXPORT_SYMBOL_GPL(gpiod_get_direction);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300228
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900229/*
230 * Add a new chip to the global chips list, keeping the list of chips sorted
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800231 * by range(means [base, base + ngpio - 1]) order.
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900232 *
233 * Return -EBUSY if the new chip overlaps with some other chip's integer
234 * space.
235 */
Linus Walleijff2b1352015-10-20 11:10:38 +0200236static int gpiodev_add_to_list(struct gpio_device *gdev)
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900237{
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800238 struct gpio_device *prev, *next;
Linus Walleijff2b1352015-10-20 11:10:38 +0200239
240 if (list_empty(&gpio_devices)) {
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800241 /* initial entry in list */
Linus Walleijff2b1352015-10-20 11:10:38 +0200242 list_add_tail(&gdev->list, &gpio_devices);
Sudip Mukherjeee28ecca2015-12-27 19:06:50 +0530243 return 0;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900244 }
245
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800246 next = list_entry(gpio_devices.next, struct gpio_device, list);
247 if (gdev->base + gdev->ngpio <= next->base) {
248 /* add before first entry */
249 list_add(&gdev->list, &gpio_devices);
Julien Grossholtz96098df2016-01-07 16:46:45 -0500250 return 0;
251 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900252
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800253 prev = list_entry(gpio_devices.prev, struct gpio_device, list);
254 if (prev->base + prev->ngpio <= gdev->base) {
255 /* add behind last entry */
256 list_add_tail(&gdev->list, &gpio_devices);
257 return 0;
258 }
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800259
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800260 list_for_each_entry_safe(prev, next, &gpio_devices, list) {
261 /* at the end of the list */
262 if (&next->list == &gpio_devices)
263 break;
264
265 /* add between prev and next */
266 if (prev->base + prev->ngpio <= gdev->base
267 && gdev->base + gdev->ngpio <= next->base) {
268 list_add(&gdev->list, &prev->list);
269 return 0;
270 }
271 }
272
273 dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n");
274 return -EBUSY;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900275}
276
Thierry Reding950d55f52017-07-24 16:57:22 +0200277/*
Linus Walleijf881bab02015-09-23 16:20:43 -0700278 * Convert a GPIO name to its descriptor
279 */
280static struct gpio_desc *gpio_name_to_desc(const char * const name)
281{
Linus Walleijff2b1352015-10-20 11:10:38 +0200282 struct gpio_device *gdev;
Linus Walleijf881bab02015-09-23 16:20:43 -0700283 unsigned long flags;
284
285 spin_lock_irqsave(&gpio_lock, flags);
286
Linus Walleijff2b1352015-10-20 11:10:38 +0200287 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijf881bab02015-09-23 16:20:43 -0700288 int i;
289
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100290 for (i = 0; i != gdev->ngpio; ++i) {
291 struct gpio_desc *desc = &gdev->descs[i];
Linus Walleijf881bab02015-09-23 16:20:43 -0700292
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100293 if (!desc->name || !name)
Linus Walleijf881bab02015-09-23 16:20:43 -0700294 continue;
295
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100296 if (!strcmp(desc->name, name)) {
Linus Walleijf881bab02015-09-23 16:20:43 -0700297 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100298 return desc;
Linus Walleijf881bab02015-09-23 16:20:43 -0700299 }
300 }
301 }
302
303 spin_unlock_irqrestore(&gpio_lock, flags);
304
305 return NULL;
306}
307
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200308/*
309 * Takes the names from gc->names and checks if they are all unique. If they
310 * are, they are assigned to their gpio descriptors.
311 *
Bamvor Jian Zhanged379152015-11-14 16:43:20 +0800312 * Warning if one of the names is already used for a different GPIO.
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200313 */
314static int gpiochip_set_desc_names(struct gpio_chip *gc)
315{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100316 struct gpio_device *gdev = gc->gpiodev;
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200317 int i;
318
319 if (!gc->names)
320 return 0;
321
322 /* First check all names if they are unique */
323 for (i = 0; i != gc->ngpio; ++i) {
324 struct gpio_desc *gpio;
325
326 gpio = gpio_name_to_desc(gc->names[i]);
Linus Walleijf881bab02015-09-23 16:20:43 -0700327 if (gpio)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100328 dev_warn(&gdev->dev,
Linus Walleij34ffd852015-10-20 11:31:54 +0200329 "Detected name collision for GPIO name '%s'\n",
Linus Walleijf881bab02015-09-23 16:20:43 -0700330 gc->names[i]);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200331 }
332
333 /* Then add all names to the GPIO descriptors */
334 for (i = 0; i != gc->ngpio; ++i)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100335 gdev->descs[i].name = gc->names[i];
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200336
337 return 0;
338}
339
Linus Walleijd7c51b42016-04-26 10:35:29 +0200340/*
341 * GPIO line handle management
342 */
343
344/**
345 * struct linehandle_state - contains the state of a userspace handle
346 * @gdev: the GPIO device the handle pertains to
347 * @label: consumer label used to tag descriptors
348 * @descs: the GPIO descriptors held by this handle
349 * @numdescs: the number of descriptors held in the descs array
350 */
351struct linehandle_state {
352 struct gpio_device *gdev;
353 const char *label;
354 struct gpio_desc *descs[GPIOHANDLES_MAX];
355 u32 numdescs;
356};
357
Lars-Peter Clausene3e847c2016-10-18 16:54:05 +0200358#define GPIOHANDLE_REQUEST_VALID_FLAGS \
359 (GPIOHANDLE_REQUEST_INPUT | \
360 GPIOHANDLE_REQUEST_OUTPUT | \
361 GPIOHANDLE_REQUEST_ACTIVE_LOW | \
362 GPIOHANDLE_REQUEST_OPEN_DRAIN | \
363 GPIOHANDLE_REQUEST_OPEN_SOURCE)
364
Linus Walleijd7c51b42016-04-26 10:35:29 +0200365static long linehandle_ioctl(struct file *filep, unsigned int cmd,
366 unsigned long arg)
367{
368 struct linehandle_state *lh = filep->private_data;
369 void __user *ip = (void __user *)arg;
370 struct gpiohandle_data ghd;
Lukas Wunnereec1d562017-10-12 12:40:10 +0200371 int vals[GPIOHANDLES_MAX];
Linus Walleijd7c51b42016-04-26 10:35:29 +0200372 int i;
373
374 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
Lukas Wunnereec1d562017-10-12 12:40:10 +0200375 /* TODO: check if descriptors are really input */
376 int ret = gpiod_get_array_value_complex(false,
377 true,
378 lh->numdescs,
379 lh->descs,
380 vals);
381 if (ret)
382 return ret;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200383
Lars-Peter Clausen3eded5d2016-10-18 16:54:02 +0200384 memset(&ghd, 0, sizeof(ghd));
Lukas Wunnereec1d562017-10-12 12:40:10 +0200385 for (i = 0; i < lh->numdescs; i++)
386 ghd.values[i] = vals[i];
Linus Walleijd7c51b42016-04-26 10:35:29 +0200387
388 if (copy_to_user(ip, &ghd, sizeof(ghd)))
389 return -EFAULT;
390
391 return 0;
392 } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
Linus Walleijd7c51b42016-04-26 10:35:29 +0200393 /* TODO: check if descriptors are really output */
394 if (copy_from_user(&ghd, ip, sizeof(ghd)))
395 return -EFAULT;
396
397 /* Clamp all values to [0,1] */
398 for (i = 0; i < lh->numdescs; i++)
399 vals[i] = !!ghd.values[i];
400
401 /* Reuse the array setting function */
402 gpiod_set_array_value_complex(false,
403 true,
404 lh->numdescs,
405 lh->descs,
406 vals);
407 return 0;
408 }
409 return -EINVAL;
410}
411
412#ifdef CONFIG_COMPAT
413static long linehandle_ioctl_compat(struct file *filep, unsigned int cmd,
414 unsigned long arg)
415{
416 return linehandle_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
417}
418#endif
419
420static int linehandle_release(struct inode *inode, struct file *filep)
421{
422 struct linehandle_state *lh = filep->private_data;
423 struct gpio_device *gdev = lh->gdev;
424 int i;
425
426 for (i = 0; i < lh->numdescs; i++)
427 gpiod_free(lh->descs[i]);
428 kfree(lh->label);
429 kfree(lh);
430 put_device(&gdev->dev);
431 return 0;
432}
433
434static const struct file_operations linehandle_fileops = {
435 .release = linehandle_release,
436 .owner = THIS_MODULE,
437 .llseek = noop_llseek,
438 .unlocked_ioctl = linehandle_ioctl,
439#ifdef CONFIG_COMPAT
440 .compat_ioctl = linehandle_ioctl_compat,
441#endif
442};
443
444static int linehandle_create(struct gpio_device *gdev, void __user *ip)
445{
446 struct gpiohandle_request handlereq;
447 struct linehandle_state *lh;
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200448 struct file *file;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200449 int fd, i, ret;
Bartosz Golaszewski418ee8e2017-10-16 11:32:29 +0200450 u32 lflags;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200451
452 if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
453 return -EFAULT;
454 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
455 return -EINVAL;
456
Bartosz Golaszewski418ee8e2017-10-16 11:32:29 +0200457 lflags = handlereq.flags;
458
459 /* Return an error if an unknown flag is set */
460 if (lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
461 return -EINVAL;
462
Bartosz Golaszewski609aaf62017-10-16 11:32:30 +0200463 /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
464 if (!(lflags & GPIOHANDLE_REQUEST_OUTPUT) &&
465 ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
466 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
467 return -EINVAL;
468
Linus Walleijd7c51b42016-04-26 10:35:29 +0200469 lh = kzalloc(sizeof(*lh), GFP_KERNEL);
470 if (!lh)
471 return -ENOMEM;
472 lh->gdev = gdev;
473 get_device(&gdev->dev);
474
475 /* Make sure this is terminated */
476 handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
477 if (strlen(handlereq.consumer_label)) {
478 lh->label = kstrdup(handlereq.consumer_label,
479 GFP_KERNEL);
480 if (!lh->label) {
481 ret = -ENOMEM;
482 goto out_free_lh;
483 }
484 }
485
486 /* Request each GPIO */
487 for (i = 0; i < handlereq.lines; i++) {
488 u32 offset = handlereq.lineoffsets[i];
Linus Walleijd7c51b42016-04-26 10:35:29 +0200489 struct gpio_desc *desc;
490
Lars-Peter Clausene405f9f2016-10-18 16:54:01 +0200491 if (offset >= gdev->ngpio) {
492 ret = -EINVAL;
493 goto out_free_descs;
494 }
495
Linus Walleijd7c51b42016-04-26 10:35:29 +0200496 desc = &gdev->descs[offset];
497 ret = gpiod_request(desc, lh->label);
498 if (ret)
499 goto out_free_descs;
500 lh->descs[i] = desc;
501
502 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
503 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
504 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
505 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
506 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
507 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
508
509 /*
510 * Lines have to be requested explicitly for input
511 * or output, else the line will be treated "as is".
512 */
513 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
514 int val = !!handlereq.default_values[i];
515
516 ret = gpiod_direction_output(desc, val);
517 if (ret)
518 goto out_free_descs;
519 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
520 ret = gpiod_direction_input(desc);
521 if (ret)
522 goto out_free_descs;
523 }
524 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
525 offset);
526 }
Linus Walleije2f608b2016-06-18 10:56:43 +0200527 /* Let i point at the last handle */
528 i--;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200529 lh->numdescs = handlereq.lines;
530
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200531 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200532 if (fd < 0) {
533 ret = fd;
534 goto out_free_descs;
535 }
536
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200537 file = anon_inode_getfile("gpio-linehandle",
538 &linehandle_fileops,
539 lh,
540 O_RDONLY | O_CLOEXEC);
541 if (IS_ERR(file)) {
542 ret = PTR_ERR(file);
543 goto out_put_unused_fd;
544 }
545
Linus Walleijd7c51b42016-04-26 10:35:29 +0200546 handlereq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200547 if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200548 /*
549 * fput() will trigger the release() callback, so do not go onto
550 * the regular error cleanup path here.
551 */
552 fput(file);
553 put_unused_fd(fd);
554 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +0200555 }
Linus Walleijd7c51b42016-04-26 10:35:29 +0200556
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200557 fd_install(fd, file);
558
Linus Walleijd7c51b42016-04-26 10:35:29 +0200559 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
560 lh->numdescs);
561
562 return 0;
563
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200564out_put_unused_fd:
565 put_unused_fd(fd);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200566out_free_descs:
567 for (; i >= 0; i--)
568 gpiod_free(lh->descs[i]);
569 kfree(lh->label);
570out_free_lh:
571 kfree(lh);
572 put_device(&gdev->dev);
573 return ret;
574}
575
Linus Walleij61f922d2016-06-02 11:30:15 +0200576/*
577 * GPIO line event management
578 */
579
580/**
581 * struct lineevent_state - contains the state of a userspace event
582 * @gdev: the GPIO device the event pertains to
583 * @label: consumer label used to tag descriptors
584 * @desc: the GPIO descriptor held by this event
585 * @eflags: the event flags this line was requested with
586 * @irq: the interrupt that trigger in response to events on this GPIO
587 * @wait: wait queue that handles blocking reads of events
588 * @events: KFIFO for the GPIO events
589 * @read_lock: mutex lock to protect reads from colliding with adding
590 * new events to the FIFO
591 */
592struct lineevent_state {
593 struct gpio_device *gdev;
594 const char *label;
595 struct gpio_desc *desc;
596 u32 eflags;
597 int irq;
598 wait_queue_head_t wait;
599 DECLARE_KFIFO(events, struct gpioevent_data, 16);
600 struct mutex read_lock;
601};
602
Lars-Peter Clausenac7dbb92016-10-18 16:54:06 +0200603#define GPIOEVENT_REQUEST_VALID_FLAGS \
604 (GPIOEVENT_REQUEST_RISING_EDGE | \
605 GPIOEVENT_REQUEST_FALLING_EDGE)
606
Linus Walleij61f922d2016-06-02 11:30:15 +0200607static unsigned int lineevent_poll(struct file *filep,
608 struct poll_table_struct *wait)
609{
610 struct lineevent_state *le = filep->private_data;
611 unsigned int events = 0;
612
613 poll_wait(filep, &le->wait, wait);
614
615 if (!kfifo_is_empty(&le->events))
616 events = POLLIN | POLLRDNORM;
617
618 return events;
619}
620
621
622static ssize_t lineevent_read(struct file *filep,
623 char __user *buf,
624 size_t count,
625 loff_t *f_ps)
626{
627 struct lineevent_state *le = filep->private_data;
628 unsigned int copied;
629 int ret;
630
631 if (count < sizeof(struct gpioevent_data))
632 return -EINVAL;
633
634 do {
635 if (kfifo_is_empty(&le->events)) {
636 if (filep->f_flags & O_NONBLOCK)
637 return -EAGAIN;
638
639 ret = wait_event_interruptible(le->wait,
640 !kfifo_is_empty(&le->events));
641 if (ret)
642 return ret;
643 }
644
645 if (mutex_lock_interruptible(&le->read_lock))
646 return -ERESTARTSYS;
647 ret = kfifo_to_user(&le->events, buf, count, &copied);
648 mutex_unlock(&le->read_lock);
649
650 if (ret)
651 return ret;
652
653 /*
654 * If we couldn't read anything from the fifo (a different
655 * thread might have been faster) we either return -EAGAIN if
656 * the file descriptor is non-blocking, otherwise we go back to
657 * sleep and wait for more data to arrive.
658 */
659 if (copied == 0 && (filep->f_flags & O_NONBLOCK))
660 return -EAGAIN;
661
662 } while (copied == 0);
663
664 return copied;
665}
666
667static int lineevent_release(struct inode *inode, struct file *filep)
668{
669 struct lineevent_state *le = filep->private_data;
670 struct gpio_device *gdev = le->gdev;
671
672 free_irq(le->irq, le);
673 gpiod_free(le->desc);
674 kfree(le->label);
675 kfree(le);
676 put_device(&gdev->dev);
677 return 0;
678}
679
680static long lineevent_ioctl(struct file *filep, unsigned int cmd,
681 unsigned long arg)
682{
683 struct lineevent_state *le = filep->private_data;
684 void __user *ip = (void __user *)arg;
685 struct gpiohandle_data ghd;
686
687 /*
688 * We can get the value for an event line but not set it,
689 * because it is input by definition.
690 */
691 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
692 int val;
693
Lars-Peter Clausend82aa4a2016-10-18 16:54:04 +0200694 memset(&ghd, 0, sizeof(ghd));
695
Linus Walleij61f922d2016-06-02 11:30:15 +0200696 val = gpiod_get_value_cansleep(le->desc);
697 if (val < 0)
698 return val;
699 ghd.values[0] = val;
700
701 if (copy_to_user(ip, &ghd, sizeof(ghd)))
702 return -EFAULT;
703
704 return 0;
705 }
706 return -EINVAL;
707}
708
709#ifdef CONFIG_COMPAT
710static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd,
711 unsigned long arg)
712{
713 return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
714}
715#endif
716
717static const struct file_operations lineevent_fileops = {
718 .release = lineevent_release,
719 .read = lineevent_read,
720 .poll = lineevent_poll,
721 .owner = THIS_MODULE,
722 .llseek = noop_llseek,
723 .unlocked_ioctl = lineevent_ioctl,
724#ifdef CONFIG_COMPAT
725 .compat_ioctl = lineevent_ioctl_compat,
726#endif
727};
728
Ben Dooks33265b12016-06-17 16:03:13 +0100729static irqreturn_t lineevent_irq_thread(int irq, void *p)
Linus Walleij61f922d2016-06-02 11:30:15 +0200730{
731 struct lineevent_state *le = p;
732 struct gpioevent_data ge;
Bartosz Golaszewskidf1e76f2017-07-03 11:12:03 +0200733 int ret, level;
Linus Walleij61f922d2016-06-02 11:30:15 +0200734
735 ge.timestamp = ktime_get_real_ns();
Bartosz Golaszewskidf1e76f2017-07-03 11:12:03 +0200736 level = gpiod_get_value_cansleep(le->desc);
Linus Walleij61f922d2016-06-02 11:30:15 +0200737
Bartosz Golaszewskiad537b82017-06-23 13:45:16 +0200738 if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
739 && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200740 if (level)
741 /* Emit low-to-high event */
742 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
743 else
744 /* Emit high-to-low event */
745 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Bartosz Golaszewskidf1e76f2017-07-03 11:12:03 +0200746 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE && level) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200747 /* Emit low-to-high event */
748 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
Bartosz Golaszewskidf1e76f2017-07-03 11:12:03 +0200749 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE && !level) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200750 /* Emit high-to-low event */
751 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Arnd Bergmannbc0207a2016-06-16 11:02:41 +0200752 } else {
753 return IRQ_NONE;
Linus Walleij61f922d2016-06-02 11:30:15 +0200754 }
755
756 ret = kfifo_put(&le->events, ge);
757 if (ret != 0)
758 wake_up_poll(&le->wait, POLLIN);
759
760 return IRQ_HANDLED;
761}
762
763static int lineevent_create(struct gpio_device *gdev, void __user *ip)
764{
765 struct gpioevent_request eventreq;
766 struct lineevent_state *le;
767 struct gpio_desc *desc;
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200768 struct file *file;
Linus Walleij61f922d2016-06-02 11:30:15 +0200769 u32 offset;
770 u32 lflags;
771 u32 eflags;
772 int fd;
773 int ret;
774 int irqflags = 0;
775
776 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
777 return -EFAULT;
778
779 le = kzalloc(sizeof(*le), GFP_KERNEL);
780 if (!le)
781 return -ENOMEM;
782 le->gdev = gdev;
783 get_device(&gdev->dev);
784
785 /* Make sure this is terminated */
786 eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
787 if (strlen(eventreq.consumer_label)) {
788 le->label = kstrdup(eventreq.consumer_label,
789 GFP_KERNEL);
790 if (!le->label) {
791 ret = -ENOMEM;
792 goto out_free_le;
793 }
794 }
795
796 offset = eventreq.lineoffset;
797 lflags = eventreq.handleflags;
798 eflags = eventreq.eventflags;
799
Lars-Peter Clausenb8b0e3d2016-10-18 16:54:03 +0200800 if (offset >= gdev->ngpio) {
801 ret = -EINVAL;
802 goto out_free_label;
803 }
804
Lars-Peter Clausenac7dbb92016-10-18 16:54:06 +0200805 /* Return an error if a unknown flag is set */
806 if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
807 (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) {
808 ret = -EINVAL;
809 goto out_free_label;
810 }
811
Linus Walleij61f922d2016-06-02 11:30:15 +0200812 /* This is just wrong: we don't look for events on output lines */
813 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
814 ret = -EINVAL;
815 goto out_free_label;
816 }
817
818 desc = &gdev->descs[offset];
819 ret = gpiod_request(desc, le->label);
820 if (ret)
821 goto out_free_desc;
822 le->desc = desc;
823 le->eflags = eflags;
824
825 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
826 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
827 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
828 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
829 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
830 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
831
832 ret = gpiod_direction_input(desc);
833 if (ret)
834 goto out_free_desc;
835
836 le->irq = gpiod_to_irq(desc);
837 if (le->irq <= 0) {
838 ret = -ENODEV;
839 goto out_free_desc;
840 }
841
842 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
843 irqflags |= IRQF_TRIGGER_RISING;
844 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
845 irqflags |= IRQF_TRIGGER_FALLING;
846 irqflags |= IRQF_ONESHOT;
847 irqflags |= IRQF_SHARED;
848
849 INIT_KFIFO(le->events);
850 init_waitqueue_head(&le->wait);
851 mutex_init(&le->read_lock);
852
853 /* Request a thread to read the events */
854 ret = request_threaded_irq(le->irq,
855 NULL,
856 lineevent_irq_thread,
857 irqflags,
858 le->label,
859 le);
860 if (ret)
861 goto out_free_desc;
862
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200863 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleij61f922d2016-06-02 11:30:15 +0200864 if (fd < 0) {
865 ret = fd;
866 goto out_free_irq;
867 }
868
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200869 file = anon_inode_getfile("gpio-event",
870 &lineevent_fileops,
871 le,
872 O_RDONLY | O_CLOEXEC);
873 if (IS_ERR(file)) {
874 ret = PTR_ERR(file);
875 goto out_put_unused_fd;
876 }
877
Linus Walleij61f922d2016-06-02 11:30:15 +0200878 eventreq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200879 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200880 /*
881 * fput() will trigger the release() callback, so do not go onto
882 * the regular error cleanup path here.
883 */
884 fput(file);
885 put_unused_fd(fd);
886 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +0200887 }
Linus Walleij61f922d2016-06-02 11:30:15 +0200888
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200889 fd_install(fd, file);
890
Linus Walleij61f922d2016-06-02 11:30:15 +0200891 return 0;
892
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200893out_put_unused_fd:
894 put_unused_fd(fd);
Linus Walleij61f922d2016-06-02 11:30:15 +0200895out_free_irq:
896 free_irq(le->irq, le);
897out_free_desc:
898 gpiod_free(le->desc);
899out_free_label:
900 kfree(le->label);
901out_free_le:
902 kfree(le);
903 put_device(&gdev->dev);
904 return ret;
905}
906
Thierry Reding950d55f52017-07-24 16:57:22 +0200907/*
Linus Walleij3c702e92015-10-21 15:29:53 +0200908 * gpio_ioctl() - ioctl handler for the GPIO chardev
909 */
910static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
911{
912 struct gpio_device *gdev = filp->private_data;
913 struct gpio_chip *chip = gdev->chip;
Linus Walleij8b92e172016-05-27 14:24:04 +0200914 void __user *ip = (void __user *)arg;
Linus Walleij3c702e92015-10-21 15:29:53 +0200915
916 /* We fail any subsequent ioctl():s when the chip is gone */
917 if (!chip)
918 return -ENODEV;
919
Linus Walleij521a2ad2016-02-12 22:25:22 +0100920 /* Fill in the struct and pass to userspace */
Linus Walleij3c702e92015-10-21 15:29:53 +0200921 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
Linus Walleij521a2ad2016-02-12 22:25:22 +0100922 struct gpiochip_info chipinfo;
923
Lars-Peter Clausen0f4bbb22016-10-18 16:54:00 +0200924 memset(&chipinfo, 0, sizeof(chipinfo));
925
Linus Walleij3c702e92015-10-21 15:29:53 +0200926 strncpy(chipinfo.name, dev_name(&gdev->dev),
927 sizeof(chipinfo.name));
928 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
Linus Walleijdf4878e2016-02-12 14:48:23 +0100929 strncpy(chipinfo.label, gdev->label,
930 sizeof(chipinfo.label));
931 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100932 chipinfo.lines = gdev->ngpio;
Linus Walleij3c702e92015-10-21 15:29:53 +0200933 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
934 return -EFAULT;
935 return 0;
Linus Walleij521a2ad2016-02-12 22:25:22 +0100936 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
937 struct gpioline_info lineinfo;
938 struct gpio_desc *desc;
939
940 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
941 return -EFAULT;
Lars-Peter Clausen1f1cc452016-10-18 16:53:59 +0200942 if (lineinfo.line_offset >= gdev->ngpio)
Linus Walleij521a2ad2016-02-12 22:25:22 +0100943 return -EINVAL;
944
945 desc = &gdev->descs[lineinfo.line_offset];
946 if (desc->name) {
947 strncpy(lineinfo.name, desc->name,
948 sizeof(lineinfo.name));
949 lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
950 } else {
951 lineinfo.name[0] = '\0';
952 }
953 if (desc->label) {
Linus Walleij214338e2016-02-25 21:01:48 +0100954 strncpy(lineinfo.consumer, desc->label,
955 sizeof(lineinfo.consumer));
956 lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +0100957 } else {
Linus Walleij214338e2016-02-25 21:01:48 +0100958 lineinfo.consumer[0] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +0100959 }
960
961 /*
962 * Userspace only need to know that the kernel is using
963 * this GPIO so it can't use it.
964 */
965 lineinfo.flags = 0;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100966 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
967 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
968 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
969 test_bit(FLAG_EXPORT, &desc->flags) ||
970 test_bit(FLAG_SYSFS, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100971 lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100972 if (test_bit(FLAG_IS_OUT, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100973 lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100974 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100975 lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100976 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100977 lineinfo.flags |= GPIOLINE_FLAG_OPEN_DRAIN;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100978 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100979 lineinfo.flags |= GPIOLINE_FLAG_OPEN_SOURCE;
980
981 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
982 return -EFAULT;
983 return 0;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200984 } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
985 return linehandle_create(gdev, ip);
Linus Walleij61f922d2016-06-02 11:30:15 +0200986 } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
987 return lineevent_create(gdev, ip);
Linus Walleij3c702e92015-10-21 15:29:53 +0200988 }
989 return -EINVAL;
990}
991
Linus Walleij8b92e172016-05-27 14:24:04 +0200992#ifdef CONFIG_COMPAT
993static long gpio_ioctl_compat(struct file *filp, unsigned int cmd,
994 unsigned long arg)
995{
996 return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
997}
998#endif
999
Linus Walleij3c702e92015-10-21 15:29:53 +02001000/**
1001 * gpio_chrdev_open() - open the chardev for ioctl operations
1002 * @inode: inode for this chardev
1003 * @filp: file struct for storing private data
1004 * Returns 0 on success
1005 */
1006static int gpio_chrdev_open(struct inode *inode, struct file *filp)
1007{
1008 struct gpio_device *gdev = container_of(inode->i_cdev,
1009 struct gpio_device, chrdev);
1010
1011 /* Fail on open if the backing gpiochip is gone */
Stephen Boydfb505742017-01-09 11:47:44 -08001012 if (!gdev->chip)
Linus Walleij3c702e92015-10-21 15:29:53 +02001013 return -ENODEV;
1014 get_device(&gdev->dev);
1015 filp->private_data = gdev;
Lars-Peter Clausenf4e81c52016-11-30 13:05:21 +01001016
1017 return nonseekable_open(inode, filp);
Linus Walleij3c702e92015-10-21 15:29:53 +02001018}
1019
1020/**
1021 * gpio_chrdev_release() - close chardev after ioctl operations
1022 * @inode: inode for this chardev
1023 * @filp: file struct for storing private data
1024 * Returns 0 on success
1025 */
1026static int gpio_chrdev_release(struct inode *inode, struct file *filp)
1027{
1028 struct gpio_device *gdev = container_of(inode->i_cdev,
1029 struct gpio_device, chrdev);
1030
Linus Walleij3c702e92015-10-21 15:29:53 +02001031 put_device(&gdev->dev);
1032 return 0;
1033}
1034
1035
1036static const struct file_operations gpio_fileops = {
1037 .release = gpio_chrdev_release,
1038 .open = gpio_chrdev_open,
1039 .owner = THIS_MODULE,
Lars-Peter Clausenf4e81c52016-11-30 13:05:21 +01001040 .llseek = no_llseek,
Linus Walleij3c702e92015-10-21 15:29:53 +02001041 .unlocked_ioctl = gpio_ioctl,
Linus Walleij8b92e172016-05-27 14:24:04 +02001042#ifdef CONFIG_COMPAT
1043 .compat_ioctl = gpio_ioctl_compat,
1044#endif
Linus Walleij3c702e92015-10-21 15:29:53 +02001045};
1046
Linus Walleijff2b1352015-10-20 11:10:38 +02001047static void gpiodevice_release(struct device *dev)
1048{
1049 struct gpio_device *gdev = dev_get_drvdata(dev);
1050
1051 list_del(&gdev->list);
1052 ida_simple_remove(&gpio_ida, gdev->id);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001053 kfree(gdev->label);
1054 kfree(gdev->descs);
Linus Walleij9efd9e62016-02-09 14:27:42 +01001055 kfree(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001056}
1057
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001058static int gpiochip_setup_dev(struct gpio_device *gdev)
1059{
1060 int status;
1061
1062 cdev_init(&gdev->chrdev, &gpio_fileops);
1063 gdev->chrdev.owner = THIS_MODULE;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001064 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001065
1066 status = cdev_device_add(&gdev->chrdev, &gdev->dev);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001067 if (status)
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001068 return status;
1069
1070 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
1071 MAJOR(gpio_devt), gdev->id);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001072
1073 status = gpiochip_sysfs_register(gdev);
1074 if (status)
1075 goto err_remove_device;
1076
1077 /* From this point, the .release() function cleans up gpio_device */
1078 gdev->dev.release = gpiodevice_release;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001079 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
1080 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
1081 dev_name(&gdev->dev), gdev->chip->label ? : "generic");
1082
1083 return 0;
1084
1085err_remove_device:
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001086 cdev_device_del(&gdev->chrdev, &gdev->dev);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001087 return status;
1088}
1089
1090static void gpiochip_setup_devs(void)
1091{
1092 struct gpio_device *gdev;
1093 int err;
1094
1095 list_for_each_entry(gdev, &gpio_devices, list) {
1096 err = gpiochip_setup_dev(gdev);
1097 if (err)
1098 pr_err("%s: Failed to initialize gpio device (%d)\n",
1099 dev_name(&gdev->dev), err);
1100 }
1101}
1102
Thierry Reding959bc7b2017-11-07 19:15:59 +01001103int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001104 struct lock_class_key *lock_key,
1105 struct lock_class_key *request_key)
David Brownelld2876d02008-02-04 22:28:20 -08001106{
1107 unsigned long flags;
1108 int status = 0;
Linus Walleijff2b1352015-10-20 11:10:38 +02001109 unsigned i;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001110 int base = chip->base;
Linus Walleijff2b1352015-10-20 11:10:38 +02001111 struct gpio_device *gdev;
David Brownelld2876d02008-02-04 22:28:20 -08001112
Linus Walleijff2b1352015-10-20 11:10:38 +02001113 /*
1114 * First: allocate and populate the internal stat container, and
1115 * set up the struct device.
1116 */
Josh Cartwright969f07b2016-02-17 16:44:15 -06001117 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
Linus Walleijff2b1352015-10-20 11:10:38 +02001118 if (!gdev)
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001119 return -ENOMEM;
Linus Walleij3c702e92015-10-21 15:29:53 +02001120 gdev->dev.bus = &gpio_bus_type;
Linus Walleijff2b1352015-10-20 11:10:38 +02001121 gdev->chip = chip;
1122 chip->gpiodev = gdev;
1123 if (chip->parent) {
1124 gdev->dev.parent = chip->parent;
1125 gdev->dev.of_node = chip->parent->of_node;
Thierry Redingacc6e332016-07-05 14:11:14 +02001126 }
1127
Linus Walleijff2b1352015-10-20 11:10:38 +02001128#ifdef CONFIG_OF_GPIO
1129 /* If the gpiochip has an assigned OF node this takes precedence */
Thierry Redingacc6e332016-07-05 14:11:14 +02001130 if (chip->of_node)
1131 gdev->dev.of_node = chip->of_node;
Linus Walleijff2b1352015-10-20 11:10:38 +02001132#endif
Thierry Redingacc6e332016-07-05 14:11:14 +02001133
Linus Walleijff2b1352015-10-20 11:10:38 +02001134 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
1135 if (gdev->id < 0) {
1136 status = gdev->id;
1137 goto err_free_gdev;
1138 }
1139 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
1140 device_initialize(&gdev->dev);
1141 dev_set_drvdata(&gdev->dev, gdev);
1142 if (chip->parent && chip->parent->driver)
1143 gdev->owner = chip->parent->driver->owner;
1144 else if (chip->owner)
1145 /* TODO: remove chip->owner */
1146 gdev->owner = chip->owner;
1147 else
1148 gdev->owner = THIS_MODULE;
David Brownelld2876d02008-02-04 22:28:20 -08001149
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001150 gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001151 if (!gdev->descs) {
Linus Walleijff2b1352015-10-20 11:10:38 +02001152 status = -ENOMEM;
1153 goto err_free_gdev;
1154 }
1155
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001156 if (chip->ngpio == 0) {
1157 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
Linus Walleijff2b1352015-10-20 11:10:38 +02001158 status = -EINVAL;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001159 goto err_free_descs;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001160 }
Linus Walleijdf4878e2016-02-12 14:48:23 +01001161
1162 if (chip->label)
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001163 gdev->label = kstrdup(chip->label, GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001164 else
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001165 gdev->label = kstrdup("unknown", GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001166 if (!gdev->label) {
1167 status = -ENOMEM;
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001168 goto err_free_descs;
Linus Walleijdf4878e2016-02-12 14:48:23 +01001169 }
1170
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001171 gdev->ngpio = chip->ngpio;
Linus Walleij43c54ec2016-02-11 11:37:48 +01001172 gdev->data = data;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001173
David Brownelld2876d02008-02-04 22:28:20 -08001174 spin_lock_irqsave(&gpio_lock, flags);
1175
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001176 /*
1177 * TODO: this allocates a Linux GPIO number base in the global
1178 * GPIO numberspace for this chip. In the long run we want to
1179 * get *rid* of this numberspace and use only descriptors, but
1180 * it may be a pipe dream. It will not happen before we get rid
1181 * of the sysfs interface anyways.
1182 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001183 if (base < 0) {
1184 base = gpiochip_find_base(chip->ngpio);
1185 if (base < 0) {
1186 status = base;
Johan Hovold225fce82015-01-12 17:12:25 +01001187 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001188 goto err_free_label;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001189 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001190 /*
1191 * TODO: it should not be necessary to reflect the assigned
1192 * base outside of the GPIO subsystem. Go over drivers and
1193 * see if anyone makes use of this, else drop this and assign
1194 * a poison instead.
1195 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001196 chip->base = base;
1197 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001198 gdev->base = base;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001199
Linus Walleijff2b1352015-10-20 11:10:38 +02001200 status = gpiodev_add_to_list(gdev);
Johan Hovold05aa5202015-01-12 17:12:26 +01001201 if (status) {
1202 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001203 goto err_free_label;
Johan Hovold05aa5202015-01-12 17:12:26 +01001204 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001205
Linus Walleij545ebd92016-05-30 17:11:59 +02001206 spin_unlock_irqrestore(&gpio_lock, flags);
1207
Linus Walleijff2b1352015-10-20 11:10:38 +02001208 for (i = 0; i < chip->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001209 struct gpio_desc *desc = &gdev->descs[i];
David Brownelld8f388d82008-07-25 01:46:07 -07001210
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001211 desc->gdev = gdev;
Linus Walleij72d32002016-04-28 13:33:59 +02001212 /*
1213 * REVISIT: most hardware initializes GPIOs as inputs
1214 * (often with pullups enabled) so power usage is
1215 * minimized. Linux code should set the gpio direction
1216 * first thing; but until it does, and in case
1217 * chip->get_direction is not set, we may expose the
1218 * wrong direction in sysfs.
Johan Hovold05aa5202015-01-12 17:12:26 +01001219 */
Linus Walleij72d32002016-04-28 13:33:59 +02001220
1221 if (chip->get_direction) {
1222 /*
1223 * If we have .get_direction, set up the initial
1224 * direction flag from the hardware.
1225 */
1226 int dir = chip->get_direction(chip, i);
1227
1228 if (!dir)
1229 set_bit(FLAG_IS_OUT, &desc->flags);
1230 } else if (!chip->direction_input) {
1231 /*
1232 * If the chip lacks the .direction_input callback
1233 * we logically assume all lines are outputs.
1234 */
1235 set_bit(FLAG_IS_OUT, &desc->flags);
1236 }
David Brownelld2876d02008-02-04 22:28:20 -08001237 }
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001238
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301239#ifdef CONFIG_PINCTRL
Linus Walleij20ec3e32016-02-11 11:03:06 +01001240 INIT_LIST_HEAD(&gdev->pin_ranges);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301241#endif
1242
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001243 status = gpiochip_set_desc_names(chip);
1244 if (status)
1245 goto err_remove_from_list;
1246
Mika Westerberg79b804c2016-09-20 15:15:21 +03001247 status = gpiochip_irqchip_init_valid_mask(chip);
1248 if (status)
1249 goto err_remove_from_list;
1250
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001251 status = gpiochip_add_irqchip(chip, lock_key, request_key);
Thierry Redinge0d89722017-11-07 19:15:54 +01001252 if (status)
1253 goto err_remove_chip;
1254
Tomeu Vizoso28355f82015-07-14 10:29:54 +02001255 status = of_gpiochip_add(chip);
1256 if (status)
1257 goto err_remove_chip;
1258
Mika Westerberg664e3e52014-01-08 12:40:54 +02001259 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001260
Linus Walleij3c702e92015-10-21 15:29:53 +02001261 /*
1262 * By first adding the chardev, and then adding the device,
1263 * we get a device node entry in sysfs under
1264 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1265 * coldplug of device nodes and other udev business.
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001266 * We can do this only if gpiolib has been initialized.
1267 * Otherwise, defer until later.
Linus Walleij3c702e92015-10-21 15:29:53 +02001268 */
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001269 if (gpiolib_initialized) {
1270 status = gpiochip_setup_dev(gdev);
1271 if (status)
1272 goto err_remove_chip;
1273 }
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001274 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001275
Johan Hovold225fce82015-01-12 17:12:25 +01001276err_remove_chip:
1277 acpi_gpiochip_remove(chip);
Johan Hovold6d867502015-05-04 17:23:25 +02001278 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +01001279 of_gpiochip_remove(chip);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001280 gpiochip_irqchip_free_valid_mask(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001281err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +01001282 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001283 list_del(&gdev->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001284 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001285err_free_label:
1286 kfree(gdev->label);
1287err_free_descs:
1288 kfree(gdev->descs);
Linus Walleijff2b1352015-10-20 11:10:38 +02001289err_free_gdev:
1290 ida_simple_remove(&gpio_ida, gdev->id);
David Brownelld2876d02008-02-04 22:28:20 -08001291 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +02001292 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001293 gdev->base, gdev->base + gdev->ngpio - 1,
1294 chip->label ? : "generic");
1295 kfree(gdev);
David Brownelld2876d02008-02-04 22:28:20 -08001296 return status;
1297}
Thierry Reding959bc7b2017-11-07 19:15:59 +01001298EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);
David Brownelld2876d02008-02-04 22:28:20 -08001299
1300/**
Linus Walleij43c54ec2016-02-11 11:37:48 +01001301 * gpiochip_get_data() - get per-subdriver data for the chip
Thierry Reding950d55f52017-07-24 16:57:22 +02001302 * @chip: GPIO chip
1303 *
1304 * Returns:
1305 * The per-subdriver data for the chip.
Linus Walleij43c54ec2016-02-11 11:37:48 +01001306 */
1307void *gpiochip_get_data(struct gpio_chip *chip)
1308{
1309 return chip->gpiodev->data;
1310}
1311EXPORT_SYMBOL_GPL(gpiochip_get_data);
1312
1313/**
David Brownelld2876d02008-02-04 22:28:20 -08001314 * gpiochip_remove() - unregister a gpio_chip
1315 * @chip: the chip to unregister
1316 *
1317 * A gpio_chip with any GPIOs still requested may not be removed.
1318 */
abdoulaye berthee1db1702014-07-05 18:28:50 +02001319void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -08001320{
Linus Walleijff2b1352015-10-20 11:10:38 +02001321 struct gpio_device *gdev = chip->gpiodev;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001322 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001323 unsigned long flags;
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001324 unsigned i;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001325 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -08001326
Linus Walleijff2b1352015-10-20 11:10:38 +02001327 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
Linus Walleijafbc4f32016-02-09 13:21:06 +01001328 gpiochip_sysfs_unregister(gdev);
Geert Uytterhoeven5018ada2016-12-19 18:29:23 +01001329 gpiochip_free_hogs(chip);
Bamvor Jian Zhangbd203bd2016-02-20 13:13:19 +08001330 /* Numb the device, cancelling all outstanding operations */
1331 gdev->chip = NULL;
Johan Hovold00acc3d2015-01-12 17:12:27 +01001332 gpiochip_irqchip_remove(chip);
Mika Westerberg6072b9d2014-03-10 14:54:53 +02001333 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +01001334 gpiochip_remove_pin_ranges(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001335 of_gpiochip_remove(chip);
Linus Walleij43c54ec2016-02-11 11:37:48 +01001336 /*
1337 * We accept no more calls into the driver from this point, so
1338 * NULL the driver data pointer
1339 */
1340 gdev->data = NULL;
Anton Vorontsov391c9702010-06-08 07:48:17 -06001341
Johan Hovold6798aca2015-01-12 17:12:28 +01001342 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001343 for (i = 0; i < gdev->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001344 desc = &gdev->descs[i];
Johan Hovoldfab28b82015-05-04 17:10:27 +02001345 if (test_bit(FLAG_REQUESTED, &desc->flags))
1346 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -08001347 }
David Brownelld2876d02008-02-04 22:28:20 -08001348 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001349
Johan Hovoldfab28b82015-05-04 17:10:27 +02001350 if (requested)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001351 dev_crit(&gdev->dev,
Linus Walleij58383c782015-11-04 09:56:26 +01001352 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +02001353
Linus Walleijff2b1352015-10-20 11:10:38 +02001354 /*
1355 * The gpiochip side puts its use of the device to rest here:
1356 * if there are no userspace clients, the chardev and device will
1357 * be removed, else it will be dangling until the last user is
1358 * gone.
1359 */
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001360 cdev_device_del(&gdev->chrdev, &gdev->dev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001361 put_device(&gdev->dev);
David Brownelld2876d02008-02-04 22:28:20 -08001362}
1363EXPORT_SYMBOL_GPL(gpiochip_remove);
1364
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301365static void devm_gpio_chip_release(struct device *dev, void *res)
1366{
1367 struct gpio_chip *chip = *(struct gpio_chip **)res;
1368
1369 gpiochip_remove(chip);
1370}
1371
1372static int devm_gpio_chip_match(struct device *dev, void *res, void *data)
1373
1374{
1375 struct gpio_chip **r = res;
1376
1377 if (!r || !*r) {
1378 WARN_ON(!r || !*r);
1379 return 0;
1380 }
1381
1382 return *r == data;
1383}
1384
1385/**
1386 * devm_gpiochip_add_data() - Resource manager piochip_add_data()
1387 * @dev: the device pointer on which irq_chip belongs to.
1388 * @chip: the chip to register, with chip->base initialized
Thierry Reding950d55f52017-07-24 16:57:22 +02001389 * @data: driver-private data associated with this chip
1390 *
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301391 * Context: potentially before irqs will work
1392 *
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301393 * The gpio chip automatically be released when the device is unbound.
Thierry Reding950d55f52017-07-24 16:57:22 +02001394 *
1395 * Returns:
1396 * A negative errno if the chip can't be registered, such as because the
1397 * chip->base is invalid or already associated with a different chip.
1398 * Otherwise it returns zero as a success code.
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301399 */
1400int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
1401 void *data)
1402{
1403 struct gpio_chip **ptr;
1404 int ret;
1405
1406 ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
1407 GFP_KERNEL);
1408 if (!ptr)
1409 return -ENOMEM;
1410
1411 ret = gpiochip_add_data(chip, data);
1412 if (ret < 0) {
1413 devres_free(ptr);
1414 return ret;
1415 }
1416
1417 *ptr = chip;
1418 devres_add(dev, ptr);
1419
1420 return 0;
1421}
1422EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
1423
1424/**
1425 * devm_gpiochip_remove() - Resource manager of gpiochip_remove()
1426 * @dev: device for which which resource was allocated
1427 * @chip: the chip to remove
1428 *
1429 * A gpio_chip with any GPIOs still requested may not be removed.
1430 */
1431void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip)
1432{
1433 int ret;
1434
1435 ret = devres_release(dev, devm_gpio_chip_release,
1436 devm_gpio_chip_match, chip);
Christophe JAILLET3988d662017-01-27 12:56:42 +01001437 WARN_ON(ret);
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301438}
1439EXPORT_SYMBOL_GPL(devm_gpiochip_remove);
1440
Grant Likely594fa262010-06-08 07:48:16 -06001441/**
1442 * gpiochip_find() - iterator for locating a specific gpio_chip
1443 * @data: data to pass to match function
Thierry Reding950d55f52017-07-24 16:57:22 +02001444 * @match: Callback function to check gpio_chip
Grant Likely594fa262010-06-08 07:48:16 -06001445 *
1446 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1447 * determined by a user supplied @match callback. The callback should return
1448 * 0 if the device doesn't match and non-zero if it does. If the callback is
1449 * non-zero, this function will return to the caller and not iterate over any
1450 * more gpio_chips.
1451 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001452struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001453 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001454 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001455{
Linus Walleijff2b1352015-10-20 11:10:38 +02001456 struct gpio_device *gdev;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001457 struct gpio_chip *chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -06001458 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001459
1460 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001461 list_for_each_entry(gdev, &gpio_devices, list)
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001462 if (gdev->chip && match(gdev->chip, data)) {
1463 chip = gdev->chip;
Grant Likely594fa262010-06-08 07:48:16 -06001464 break;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001465 }
Linus Walleijff2b1352015-10-20 11:10:38 +02001466
Grant Likely594fa262010-06-08 07:48:16 -06001467 spin_unlock_irqrestore(&gpio_lock, flags);
1468
1469 return chip;
1470}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001471EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001472
Alexandre Courbot79697ef2013-11-16 21:39:32 +09001473static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1474{
1475 const char *name = data;
1476
1477 return !strcmp(chip->label, name);
1478}
1479
1480static struct gpio_chip *find_chip_by_name(const char *name)
1481{
1482 return gpiochip_find((void *)name, gpiochip_match_name);
1483}
1484
Linus Walleij14250522014-03-25 10:40:18 +01001485#ifdef CONFIG_GPIOLIB_IRQCHIP
1486
1487/*
1488 * The following is irqchip helper code for gpiochips.
1489 */
1490
Mika Westerberg79b804c2016-09-20 15:15:21 +03001491static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1492{
Thierry Redingdc7b0382017-11-07 19:15:52 +01001493 if (!gpiochip->irq.need_valid_mask)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001494 return 0;
1495
Thierry Redingdc7b0382017-11-07 19:15:52 +01001496 gpiochip->irq.valid_mask = kcalloc(BITS_TO_LONGS(gpiochip->ngpio),
Mika Westerberg79b804c2016-09-20 15:15:21 +03001497 sizeof(long), GFP_KERNEL);
Thierry Redingdc7b0382017-11-07 19:15:52 +01001498 if (!gpiochip->irq.valid_mask)
Mika Westerberg79b804c2016-09-20 15:15:21 +03001499 return -ENOMEM;
1500
1501 /* Assume by default all GPIOs are valid */
Thierry Redingdc7b0382017-11-07 19:15:52 +01001502 bitmap_fill(gpiochip->irq.valid_mask, gpiochip->ngpio);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001503
1504 return 0;
1505}
1506
1507static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1508{
Thierry Redingdc7b0382017-11-07 19:15:52 +01001509 kfree(gpiochip->irq.valid_mask);
1510 gpiochip->irq.valid_mask = NULL;
Mika Westerberg79b804c2016-09-20 15:15:21 +03001511}
1512
1513static bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
1514 unsigned int offset)
1515{
1516 /* No mask means all valid */
Thierry Redingdc7b0382017-11-07 19:15:52 +01001517 if (likely(!gpiochip->irq.valid_mask))
Mika Westerberg79b804c2016-09-20 15:15:21 +03001518 return true;
Thierry Redingdc7b0382017-11-07 19:15:52 +01001519 return test_bit(offset, gpiochip->irq.valid_mask);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001520}
1521
Linus Walleij14250522014-03-25 10:40:18 +01001522/**
Linus Walleijd245b3f2016-11-24 10:57:25 +01001523 * gpiochip_set_cascaded_irqchip() - connects a cascaded irqchip to a gpiochip
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001524 * @gpiochip: the gpiochip to set the irqchip chain to
1525 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01001526 * @parent_irq: the irq number corresponding to the parent IRQ for this
1527 * chained irqchip
1528 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001529 * coming out of the gpiochip. If the interrupt is nested rather than
1530 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +01001531 */
Linus Walleijd245b3f2016-11-24 10:57:25 +01001532static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gpiochip,
1533 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001534 unsigned int parent_irq,
Linus Walleijd245b3f2016-11-24 10:57:25 +01001535 irq_flow_handler_t parent_handler)
Linus Walleij14250522014-03-25 10:40:18 +01001536{
Linus Walleij83141a72014-09-26 13:50:12 +02001537 unsigned int offset;
1538
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001539 if (!gpiochip->irq.domain) {
Linus Walleij83141a72014-09-26 13:50:12 +02001540 chip_err(gpiochip, "called %s before setting up irqchip\n",
1541 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +02001542 return;
1543 }
1544
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001545 if (parent_handler) {
1546 if (gpiochip->can_sleep) {
1547 chip_err(gpiochip,
1548 "you cannot have chained interrupts on a "
1549 "chip that may sleep\n");
1550 return;
1551 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001552 /*
1553 * The parent irqchip is already using the chip_data for this
1554 * irqchip, so our callbacks simply use the handler_data.
1555 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +02001556 irq_set_chained_handler_and_data(parent_irq, parent_handler,
1557 gpiochip);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001558
Thierry Reding39e5f092017-11-07 19:15:50 +01001559 gpiochip->irq.parents = &parent_irq;
1560 gpiochip->irq.num_parents = 1;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001561 }
Linus Walleij83141a72014-09-26 13:50:12 +02001562
1563 /* Set the parent IRQ for all affected IRQs */
Mika Westerberg79b804c2016-09-20 15:15:21 +03001564 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1565 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1566 continue;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001567 irq_set_parent(irq_find_mapping(gpiochip->irq.domain, offset),
Linus Walleij83141a72014-09-26 13:50:12 +02001568 parent_irq);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001569 }
Linus Walleij14250522014-03-25 10:40:18 +01001570}
Linus Walleijd245b3f2016-11-24 10:57:25 +01001571
1572/**
1573 * gpiochip_set_chained_irqchip() - connects a chained irqchip to a gpiochip
1574 * @gpiochip: the gpiochip to set the irqchip chain to
1575 * @irqchip: the irqchip to chain to the gpiochip
1576 * @parent_irq: the irq number corresponding to the parent IRQ for this
1577 * chained irqchip
1578 * @parent_handler: the parent interrupt handler for the accumulated IRQ
1579 * coming out of the gpiochip. If the interrupt is nested rather than
1580 * cascaded, pass NULL in this handler argument
1581 */
1582void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
1583 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001584 unsigned int parent_irq,
Linus Walleijd245b3f2016-11-24 10:57:25 +01001585 irq_flow_handler_t parent_handler)
1586{
Thierry Reding60ed54c2017-11-07 19:15:57 +01001587 if (gpiochip->irq.threaded) {
1588 chip_err(gpiochip, "tried to chain a threaded gpiochip\n");
1589 return;
1590 }
1591
Linus Walleijd245b3f2016-11-24 10:57:25 +01001592 gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
1593 parent_handler);
1594}
Linus Walleij14250522014-03-25 10:40:18 +01001595EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
1596
1597/**
Linus Walleijd245b3f2016-11-24 10:57:25 +01001598 * gpiochip_set_nested_irqchip() - connects a nested irqchip to a gpiochip
1599 * @gpiochip: the gpiochip to set the irqchip nested handler to
1600 * @irqchip: the irqchip to nest to the gpiochip
1601 * @parent_irq: the irq number corresponding to the parent IRQ for this
1602 * nested irqchip
1603 */
1604void gpiochip_set_nested_irqchip(struct gpio_chip *gpiochip,
1605 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001606 unsigned int parent_irq)
Linus Walleijd245b3f2016-11-24 10:57:25 +01001607{
Linus Walleijd245b3f2016-11-24 10:57:25 +01001608 gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
1609 NULL);
1610}
1611EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip);
1612
1613/**
Linus Walleij14250522014-03-25 10:40:18 +01001614 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1615 * @d: the irqdomain used by this irqchip
1616 * @irq: the global irq number used by this GPIO irqchip irq
1617 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1618 *
1619 * This function will set up the mapping for a certain IRQ line on a
1620 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1621 * stored inside the gpiochip.
1622 */
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001623int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1624 irq_hw_number_t hwirq)
Linus Walleij14250522014-03-25 10:40:18 +01001625{
1626 struct gpio_chip *chip = d->host_data;
Thierry Redinge0d89722017-11-07 19:15:54 +01001627 int err = 0;
Linus Walleij14250522014-03-25 10:40:18 +01001628
Grygorii Strashkodc749a02017-07-21 11:49:00 -05001629 if (!gpiochip_irqchip_irq_valid(chip, hwirq))
1630 return -ENXIO;
1631
Linus Walleij14250522014-03-25 10:40:18 +01001632 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001633 /*
1634 * This lock class tells lockdep that GPIO irqs are in a different
1635 * category than their parents, so it won't report false recursion.
1636 */
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001637 irq_set_lockdep_class(irq, chip->irq.lock_key, chip->irq.request_key);
Thierry Redingc7a0aa52017-11-07 19:15:48 +01001638 irq_set_chip_and_handler(irq, chip->irq.chip, chip->irq.handler);
Linus Walleijd245b3f2016-11-24 10:57:25 +01001639 /* Chips that use nested thread handlers have them marked */
Thierry Reding60ed54c2017-11-07 19:15:57 +01001640 if (chip->irq.threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02001641 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +01001642 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -05001643
Thierry Redinge0d89722017-11-07 19:15:54 +01001644 if (chip->irq.num_parents == 1)
1645 err = irq_set_parent(irq, chip->irq.parents[0]);
1646 else if (chip->irq.map)
1647 err = irq_set_parent(irq, chip->irq.map[hwirq]);
1648
1649 if (err < 0)
1650 return err;
1651
Linus Walleij1333b902014-04-23 16:45:12 +02001652 /*
1653 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1654 * is passed as default type.
1655 */
Thierry Reding3634eeb2017-11-07 19:15:49 +01001656 if (chip->irq.default_type != IRQ_TYPE_NONE)
1657 irq_set_irq_type(irq, chip->irq.default_type);
Linus Walleij14250522014-03-25 10:40:18 +01001658
1659 return 0;
1660}
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001661EXPORT_SYMBOL_GPL(gpiochip_irq_map);
Linus Walleij14250522014-03-25 10:40:18 +01001662
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001663void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
Linus Walleijc3626fd2014-03-28 20:42:01 +01001664{
Linus Walleij1c8732b2014-04-09 13:34:39 +02001665 struct gpio_chip *chip = d->host_data;
1666
Thierry Reding60ed54c2017-11-07 19:15:57 +01001667 if (chip->irq.threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02001668 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001669 irq_set_chip_and_handler(irq, NULL, NULL);
1670 irq_set_chip_data(irq, NULL);
1671}
Thierry Reding1b95b4e2017-11-07 19:15:55 +01001672EXPORT_SYMBOL_GPL(gpiochip_irq_unmap);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001673
Linus Walleij14250522014-03-25 10:40:18 +01001674static const struct irq_domain_ops gpiochip_domain_ops = {
1675 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +01001676 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +01001677 /* Virtually all GPIO irqchips are twocell:ed */
1678 .xlate = irq_domain_xlate_twocell,
1679};
1680
1681static int gpiochip_irq_reqres(struct irq_data *d)
1682{
1683 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1684
Linus Walleijff2b1352015-10-20 11:10:38 +02001685 if (!try_module_get(chip->gpiodev->owner))
Grygorii Strashko5b76e792015-06-25 20:30:50 +03001686 return -ENODEV;
1687
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001688 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +01001689 chip_err(chip,
1690 "unable to lock HW IRQ %lu for IRQ\n",
1691 d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001692 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001693 return -EINVAL;
1694 }
1695 return 0;
1696}
1697
1698static void gpiochip_irq_relres(struct irq_data *d)
1699{
1700 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1701
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001702 gpiochip_unlock_as_irq(chip, d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001703 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001704}
1705
1706static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
1707{
Grygorii Strashkodc749a02017-07-21 11:49:00 -05001708 if (!gpiochip_irqchip_irq_valid(chip, offset))
1709 return -ENXIO;
Thierry Redinge0d89722017-11-07 19:15:54 +01001710
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001711 return irq_create_mapping(chip->irq.domain, offset);
Linus Walleij14250522014-03-25 10:40:18 +01001712}
1713
1714/**
Thierry Redinge0d89722017-11-07 19:15:54 +01001715 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
1716 * @gpiochip: the GPIO chip to add the IRQ chip to
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001717 * @lock_key: lockdep class for IRQ lock
1718 * @request_key: lockdep class for IRQ request
Thierry Redinge0d89722017-11-07 19:15:54 +01001719 */
Thierry Reding959bc7b2017-11-07 19:15:59 +01001720static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001721 struct lock_class_key *lock_key,
1722 struct lock_class_key *request_key)
Thierry Redinge0d89722017-11-07 19:15:54 +01001723{
1724 struct irq_chip *irqchip = gpiochip->irq.chip;
1725 const struct irq_domain_ops *ops;
1726 struct device_node *np;
1727 unsigned int type;
1728 unsigned int i;
1729
1730 if (!irqchip)
1731 return 0;
1732
1733 if (gpiochip->irq.parent_handler && gpiochip->can_sleep) {
1734 chip_err(gpiochip, "you cannot have chained interrupts on a "
1735 "chip that may sleep\n");
1736 return -EINVAL;
1737 }
1738
1739 np = gpiochip->gpiodev->dev.of_node;
1740 type = gpiochip->irq.default_type;
1741
1742 /*
1743 * Specifying a default trigger is a terrible idea if DT or ACPI is
1744 * used to configure the interrupts, as you may end up with
1745 * conflicting triggers. Tell the user, and reset to NONE.
1746 */
1747 if (WARN(np && type != IRQ_TYPE_NONE,
1748 "%s: Ignoring %u default trigger\n", np->full_name, type))
1749 type = IRQ_TYPE_NONE;
1750
1751 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
1752 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1753 "Ignoring %u default trigger\n", type);
1754 type = IRQ_TYPE_NONE;
1755 }
1756
1757 gpiochip->to_irq = gpiochip_to_irq;
1758 gpiochip->irq.default_type = type;
Thierry Reding959bc7b2017-11-07 19:15:59 +01001759 gpiochip->irq.lock_key = lock_key;
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001760 gpiochip->irq.request_key = request_key;
Thierry Redinge0d89722017-11-07 19:15:54 +01001761
1762 if (gpiochip->irq.domain_ops)
1763 ops = gpiochip->irq.domain_ops;
1764 else
1765 ops = &gpiochip_domain_ops;
1766
1767 gpiochip->irq.domain = irq_domain_add_simple(np, gpiochip->ngpio,
Thierry Reding8302cf52017-11-07 19:15:58 +01001768 gpiochip->irq.first,
1769 ops, gpiochip);
Thierry Redinge0d89722017-11-07 19:15:54 +01001770 if (!gpiochip->irq.domain)
1771 return -EINVAL;
1772
1773 /*
1774 * It is possible for a driver to override this, but only if the
1775 * alternative functions are both implemented.
1776 */
1777 if (!irqchip->irq_request_resources &&
1778 !irqchip->irq_release_resources) {
1779 irqchip->irq_request_resources = gpiochip_irq_reqres;
1780 irqchip->irq_release_resources = gpiochip_irq_relres;
1781 }
1782
1783 if (gpiochip->irq.parent_handler) {
1784 void *data = gpiochip->irq.parent_handler_data ?: gpiochip;
1785
1786 for (i = 0; i < gpiochip->irq.num_parents; i++) {
1787 /*
1788 * The parent IRQ chip is already using the chip_data
1789 * for this IRQ chip, so our callbacks simply use the
1790 * handler_data.
1791 */
1792 irq_set_chained_handler_and_data(gpiochip->irq.parents[i],
1793 gpiochip->irq.parent_handler,
1794 data);
1795 }
Thierry Redinge0d89722017-11-07 19:15:54 +01001796 }
1797
1798 acpi_gpiochip_request_interrupts(gpiochip);
1799
1800 return 0;
1801}
1802
1803/**
Linus Walleij14250522014-03-25 10:40:18 +01001804 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1805 * @gpiochip: the gpiochip to remove the irqchip from
1806 *
1807 * This is called only from gpiochip_remove()
1808 */
1809static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
1810{
Thierry Reding39e5f092017-11-07 19:15:50 +01001811 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +01001812
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001813 acpi_gpiochip_free_interrupts(gpiochip);
1814
Thierry Redinge0d89722017-11-07 19:15:54 +01001815 if (gpiochip->irq.chip && gpiochip->irq.parent_handler) {
Thierry Reding39e5f092017-11-07 19:15:50 +01001816 struct gpio_irq_chip *irq = &gpiochip->irq;
1817 unsigned int i;
1818
1819 for (i = 0; i < irq->num_parents; i++)
1820 irq_set_chained_handler_and_data(irq->parents[i],
1821 NULL, NULL);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001822 }
1823
Linus Walleijc3626fd2014-03-28 20:42:01 +01001824 /* Remove all IRQ mappings and delete the domain */
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001825 if (gpiochip->irq.domain) {
Thierry Reding39e5f092017-11-07 19:15:50 +01001826 unsigned int irq;
1827
Mika Westerberg79b804c2016-09-20 15:15:21 +03001828 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1829 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1830 continue;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001831
1832 irq = irq_find_mapping(gpiochip->irq.domain, offset);
1833 irq_dispose_mapping(irq);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001834 }
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001835
1836 irq_domain_remove(gpiochip->irq.domain);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001837 }
Linus Walleij14250522014-03-25 10:40:18 +01001838
Thierry Redingda80ff82017-11-07 19:15:46 +01001839 if (gpiochip->irq.chip) {
1840 gpiochip->irq.chip->irq_request_resources = NULL;
1841 gpiochip->irq.chip->irq_release_resources = NULL;
1842 gpiochip->irq.chip = NULL;
Linus Walleij14250522014-03-25 10:40:18 +01001843 }
Mika Westerberg79b804c2016-09-20 15:15:21 +03001844
1845 gpiochip_irqchip_free_valid_mask(gpiochip);
Linus Walleij14250522014-03-25 10:40:18 +01001846}
1847
1848/**
Linus Walleij739e6f52017-01-11 13:37:07 +01001849 * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01001850 * @gpiochip: the gpiochip to add the irqchip to
1851 * @irqchip: the irqchip to add to the gpiochip
1852 * @first_irq: if not dynamically assigned, the base (first) IRQ to
1853 * allocate gpiochip irqs from
1854 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +02001855 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
1856 * to have the core avoid setting up any default type in the hardware.
Thierry Reding60ed54c2017-11-07 19:15:57 +01001857 * @threaded: whether this irqchip uses a nested thread handler
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001858 * @lock_key: lockdep class for IRQ lock
1859 * @request_key: lockdep class for IRQ request
Linus Walleij14250522014-03-25 10:40:18 +01001860 *
1861 * This function closely associates a certain irqchip with a certain
1862 * gpiochip, providing an irq domain to translate the local IRQs to
1863 * global irqs in the gpiolib core, and making sure that the gpiochip
1864 * is passed as chip data to all related functions. Driver callbacks
Linus Walleij09dd5f92015-12-07 15:31:58 +01001865 * need to use gpiochip_get_data() to get their local state containers back
Linus Walleij14250522014-03-25 10:40:18 +01001866 * from the gpiochip passed as chip data. An irqdomain will be stored
1867 * in the gpiochip that shall be used by the driver to handle IRQ number
1868 * translation. The gpiochip will need to be initialized and registered
1869 * before calling this function.
1870 *
Linus Walleijc3626fd2014-03-28 20:42:01 +01001871 * This function will handle two cell:ed simple IRQs and assumes all
1872 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +01001873 * need to be open coded.
1874 */
Linus Walleij739e6f52017-01-11 13:37:07 +01001875int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
1876 struct irq_chip *irqchip,
1877 unsigned int first_irq,
1878 irq_flow_handler_t handler,
1879 unsigned int type,
Thierry Reding60ed54c2017-11-07 19:15:57 +01001880 bool threaded,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001881 struct lock_class_key *lock_key,
1882 struct lock_class_key *request_key)
Linus Walleij14250522014-03-25 10:40:18 +01001883{
1884 struct device_node *of_node;
Linus Walleij14250522014-03-25 10:40:18 +01001885
1886 if (!gpiochip || !irqchip)
1887 return -EINVAL;
1888
Linus Walleij58383c782015-11-04 09:56:26 +01001889 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +01001890 pr_err("missing gpiochip .dev parent pointer\n");
1891 return -EINVAL;
1892 }
Thierry Reding60ed54c2017-11-07 19:15:57 +01001893 gpiochip->irq.threaded = threaded;
Linus Walleij58383c782015-11-04 09:56:26 +01001894 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +01001895#ifdef CONFIG_OF_GPIO
1896 /*
Colin Cronin20a8a962015-05-18 11:41:43 -07001897 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08001898 * FIXME: get rid of this and use gpiochip->parent->of_node
1899 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +01001900 */
1901 if (gpiochip->of_node)
1902 of_node = gpiochip->of_node;
1903#endif
Marc Zyngier332e99d2016-09-07 09:12:11 +01001904 /*
Mika Westerberg0a1e0052016-09-12 14:29:51 +03001905 * Specifying a default trigger is a terrible idea if DT or ACPI is
Marc Zyngier332e99d2016-09-07 09:12:11 +01001906 * used to configure the interrupts, as you may end-up with
1907 * conflicting triggers. Tell the user, and reset to NONE.
1908 */
1909 if (WARN(of_node && type != IRQ_TYPE_NONE,
Rob Herring7eb6ce22017-07-18 16:43:03 -05001910 "%pOF: Ignoring %d default trigger\n", of_node, type))
Marc Zyngier332e99d2016-09-07 09:12:11 +01001911 type = IRQ_TYPE_NONE;
Mika Westerberg0a1e0052016-09-12 14:29:51 +03001912 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
1913 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1914 "Ignoring %d default trigger\n", type);
1915 type = IRQ_TYPE_NONE;
1916 }
Marc Zyngier332e99d2016-09-07 09:12:11 +01001917
Thierry Redingda80ff82017-11-07 19:15:46 +01001918 gpiochip->irq.chip = irqchip;
Thierry Redingc7a0aa52017-11-07 19:15:48 +01001919 gpiochip->irq.handler = handler;
Thierry Reding3634eeb2017-11-07 19:15:49 +01001920 gpiochip->irq.default_type = type;
Linus Walleij14250522014-03-25 10:40:18 +01001921 gpiochip->to_irq = gpiochip_to_irq;
Thierry Redingca9df052017-11-07 19:15:53 +01001922 gpiochip->irq.lock_key = lock_key;
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001923 gpiochip->irq.request_key = request_key;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001924 gpiochip->irq.domain = irq_domain_add_simple(of_node,
Linus Walleij14250522014-03-25 10:40:18 +01001925 gpiochip->ngpio, first_irq,
1926 &gpiochip_domain_ops, gpiochip);
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001927 if (!gpiochip->irq.domain) {
Thierry Redingda80ff82017-11-07 19:15:46 +01001928 gpiochip->irq.chip = NULL;
Linus Walleij14250522014-03-25 10:40:18 +01001929 return -EINVAL;
1930 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +02001931
1932 /*
1933 * It is possible for a driver to override this, but only if the
1934 * alternative functions are both implemented.
1935 */
1936 if (!irqchip->irq_request_resources &&
1937 !irqchip->irq_release_resources) {
1938 irqchip->irq_request_resources = gpiochip_irq_reqres;
1939 irqchip->irq_release_resources = gpiochip_irq_relres;
1940 }
Linus Walleij14250522014-03-25 10:40:18 +01001941
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001942 acpi_gpiochip_request_interrupts(gpiochip);
1943
Linus Walleij14250522014-03-25 10:40:18 +01001944 return 0;
1945}
Linus Walleij739e6f52017-01-11 13:37:07 +01001946EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key);
Linus Walleij14250522014-03-25 10:40:18 +01001947
1948#else /* CONFIG_GPIOLIB_IRQCHIP */
1949
Thierry Reding959bc7b2017-11-07 19:15:59 +01001950static inline int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
Andrew Lunn39c3fd52017-12-02 18:11:04 +01001951 struct lock_class_key *lock_key,
1952 struct lock_class_key *request_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 */