blob: 505dead076196ba210066171bc017eaf11641a3a [file] [log] [blame]
David Brownelld2876d02008-02-04 22:28:20 -08001#include <linux/kernel.h>
2#include <linux/module.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -07003#include <linux/interrupt.h>
David Brownelld2876d02008-02-04 22:28:20 -08004#include <linux/irq.h>
5#include <linux/spinlock.h>
Alexandre Courbot1a989d02013-02-03 01:29:24 +09006#include <linux/list.h>
David Brownelld8f388d2008-07-25 01:46:07 -07007#include <linux/device.h>
8#include <linux/err.h>
9#include <linux/debugfs.h>
10#include <linux/seq_file.h>
11#include <linux/gpio.h>
Anton Vorontsov391c9702010-06-08 07:48:17 -060012#include <linux/of_gpio.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -070013#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Rafael J. Wysocki7b199812013-11-11 22:41:56 +010015#include <linux/acpi.h>
Alexandre Courbot53e7cac2013-11-16 21:44:52 +090016#include <linux/gpio/driver.h>
Linus Walleij0a6d3152014-07-24 20:08:55 +020017#include <linux/gpio/machine.h>
Jonas Gorskic771c2f2015-10-11 17:34:15 +020018#include <linux/pinctrl/consumer.h>
Linus Walleij3c702e92015-10-21 15:29:53 +020019#include <linux/cdev.h>
20#include <linux/fs.h>
21#include <linux/uaccess.h>
Linus Walleij8b92e172016-05-27 14:24:04 +020022#include <linux/compat.h>
Linus Walleijd7c51b42016-04-26 10:35:29 +020023#include <linux/anon_inodes.h>
Lars-Peter Clausen953b9562016-10-24 13:59:15 +020024#include <linux/file.h>
Linus Walleij61f922d2016-06-02 11:30:15 +020025#include <linux/kfifo.h>
26#include <linux/poll.h>
27#include <linux/timekeeping.h>
Linus Walleij3c702e92015-10-21 15:29:53 +020028#include <uapi/linux/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080029
Mika Westerberg664e3e52014-01-08 12:40:54 +020030#include "gpiolib.h"
31
Uwe Kleine-König3f397c212011-05-20 00:40:19 -060032#define CREATE_TRACE_POINTS
33#include <trace/events/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080034
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070035/* Implementation infrastructure for GPIO interfaces.
David Brownelld2876d02008-02-04 22:28:20 -080036 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070037 * The GPIO programming interface allows for inlining speed-critical
38 * get/set operations for common cases, so that access to SOC-integrated
39 * GPIOs can sometimes cost only an instruction or two per bit.
David Brownelld2876d02008-02-04 22:28:20 -080040 */
41
42
43/* When debugging, extend minimal trust to callers and platform code.
44 * Also emit diagnostic messages that may help initial bringup, when
45 * board setup or driver bugs are most common.
46 *
47 * Otherwise, minimize overhead in what may be bitbanging codepaths.
48 */
49#ifdef DEBUG
50#define extra_checks 1
51#else
52#define extra_checks 0
53#endif
54
Linus Walleijff2b1352015-10-20 11:10:38 +020055/* Device and char device-related information */
56static DEFINE_IDA(gpio_ida);
Linus Walleij3c702e92015-10-21 15:29:53 +020057static dev_t gpio_devt;
58#define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
59static struct bus_type gpio_bus_type = {
60 .name = "gpio",
61};
Linus Walleijff2b1352015-10-20 11:10:38 +020062
David Brownelld2876d02008-02-04 22:28:20 -080063/* gpio_lock prevents conflicts during gpio_desc[] table updates.
64 * While any GPIO is requested, its gpio_chip is not removable;
65 * each GPIO's "requested" flag serves as a lock and refcount.
66 */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090067DEFINE_SPINLOCK(gpio_lock);
David Brownelld2876d02008-02-04 22:28:20 -080068
Alexandre Courbotbae48da2013-10-17 10:21:38 -070069static DEFINE_MUTEX(gpio_lookup_lock);
70static LIST_HEAD(gpio_lookup_list);
Linus Walleijff2b1352015-10-20 11:10:38 +020071LIST_HEAD(gpio_devices);
Johan Hovold6d867502015-05-04 17:23:25 +020072
73static void gpiochip_free_hogs(struct gpio_chip *chip);
74static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
Mika Westerberg79b804c2016-09-20 15:15:21 +030075static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip);
76static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip);
Johan Hovold6d867502015-05-04 17:23:25 +020077
Guenter Roeck159f3cd2016-03-31 08:11:30 -070078static bool gpiolib_initialized;
Johan Hovold6d867502015-05-04 17:23:25 +020079
David Brownelld2876d02008-02-04 22:28:20 -080080static inline void desc_set_label(struct gpio_desc *d, const char *label)
81{
David Brownelld2876d02008-02-04 22:28:20 -080082 d->label = label;
David Brownelld2876d02008-02-04 22:28:20 -080083}
84
Alexandre Courbot372e7222013-02-03 01:29:29 +090085/**
86 * Convert a GPIO number to its descriptor
87 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070088struct gpio_desc *gpio_to_desc(unsigned gpio)
Alexandre Courbot372e7222013-02-03 01:29:29 +090089{
Linus Walleijff2b1352015-10-20 11:10:38 +020090 struct gpio_device *gdev;
Alexandre Courbot14e85c02014-11-19 16:51:27 +090091 unsigned long flags;
92
93 spin_lock_irqsave(&gpio_lock, flags);
94
Linus Walleijff2b1352015-10-20 11:10:38 +020095 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +010096 if (gdev->base <= gpio &&
97 gdev->base + gdev->ngpio > gpio) {
Alexandre Courbot14e85c02014-11-19 16:51:27 +090098 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +010099 return &gdev->descs[gpio - gdev->base];
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900100 }
101 }
102
103 spin_unlock_irqrestore(&gpio_lock, flags);
104
Alexandre Courbot0e9a5ed2014-12-02 23:15:05 +0900105 if (!gpio_is_valid(gpio))
106 WARN(1, "invalid GPIO %d\n", gpio);
107
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900108 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900109}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700110EXPORT_SYMBOL_GPL(gpio_to_desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900111
112/**
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900113 * Get the GPIO descriptor corresponding to the given hw number for this chip.
Linus Walleijd468bf92013-09-24 11:54:38 +0200114 */
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900115struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
116 u16 hwnum)
Linus Walleijd468bf92013-09-24 11:54:38 +0200117{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100118 struct gpio_device *gdev = chip->gpiodev;
119
120 if (hwnum >= gdev->ngpio)
Alexandre Courbotb7d0a282013-12-03 12:31:11 +0900121 return ERR_PTR(-EINVAL);
Linus Walleijd468bf92013-09-24 11:54:38 +0200122
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100123 return &gdev->descs[hwnum];
Linus Walleijd468bf92013-09-24 11:54:38 +0200124}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900125
126/**
127 * Convert a GPIO descriptor to the integer namespace.
128 * This should disappear in the future but is needed since we still
129 * use GPIO numbers for error messages and sysfs nodes
130 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700131int desc_to_gpio(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900132{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100133 return desc->gdev->base + (desc - &desc->gdev->descs[0]);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900134}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700135EXPORT_SYMBOL_GPL(desc_to_gpio);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900136
137
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700138/**
139 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
140 * @desc: descriptor to return the chip of
141 */
142struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900143{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100144 if (!desc || !desc->gdev || !desc->gdev->chip)
145 return NULL;
146 return desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900147}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700148EXPORT_SYMBOL_GPL(gpiod_to_chip);
David Brownelld2876d02008-02-04 22:28:20 -0800149
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700150/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
151static int gpiochip_find_base(int ngpio)
152{
Linus Walleijff2b1352015-10-20 11:10:38 +0200153 struct gpio_device *gdev;
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900154 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700155
Linus Walleijff2b1352015-10-20 11:10:38 +0200156 list_for_each_entry_reverse(gdev, &gpio_devices, list) {
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900157 /* found a free space? */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100158 if (gdev->base + gdev->ngpio <= base)
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900159 break;
160 else
161 /* nope, check the space right before the chip */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100162 base = gdev->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700163 }
164
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900165 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700166 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900167 return base;
168 } else {
169 pr_err("%s: cannot find free range\n", __func__);
170 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700171 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700172}
173
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700174/**
175 * gpiod_get_direction - return the current direction of a GPIO
176 * @desc: GPIO to get the direction of
177 *
178 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
179 *
180 * This function may sleep if gpiod_cansleep() is true.
181 */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900182int gpiod_get_direction(struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300183{
184 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900185 unsigned offset;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300186 int status = -EINVAL;
187
Alexandre Courbot372e7222013-02-03 01:29:29 +0900188 chip = gpiod_to_chip(desc);
189 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300190
Russell King284c8422019-12-07 16:20:18 +0000191 /*
192 * Open drain emulation using input mode may incorrectly report
193 * input here, fix that up.
194 */
195 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) &&
196 test_bit(FLAG_IS_OUT, &desc->flags))
197 return 0;
198
Mathias Nyman80b0a602012-10-24 17:25:27 +0300199 if (!chip->get_direction)
200 return status;
201
Alexandre Courbot372e7222013-02-03 01:29:29 +0900202 status = chip->get_direction(chip, offset);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300203 if (status > 0) {
204 /* GPIOF_DIR_IN, or other positive */
205 status = 1;
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900206 clear_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300207 }
208 if (status == 0) {
209 /* GPIOF_DIR_OUT */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900210 set_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300211 }
212 return status;
213}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700214EXPORT_SYMBOL_GPL(gpiod_get_direction);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300215
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900216/*
217 * Add a new chip to the global chips list, keeping the list of chips sorted
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800218 * by range(means [base, base + ngpio - 1]) order.
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900219 *
220 * Return -EBUSY if the new chip overlaps with some other chip's integer
221 * space.
222 */
Linus Walleijff2b1352015-10-20 11:10:38 +0200223static int gpiodev_add_to_list(struct gpio_device *gdev)
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900224{
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800225 struct gpio_device *prev, *next;
Linus Walleijff2b1352015-10-20 11:10:38 +0200226
227 if (list_empty(&gpio_devices)) {
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800228 /* initial entry in list */
Linus Walleijff2b1352015-10-20 11:10:38 +0200229 list_add_tail(&gdev->list, &gpio_devices);
Sudip Mukherjeee28ecca2015-12-27 19:06:50 +0530230 return 0;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900231 }
232
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800233 next = list_entry(gpio_devices.next, struct gpio_device, list);
234 if (gdev->base + gdev->ngpio <= next->base) {
235 /* add before first entry */
236 list_add(&gdev->list, &gpio_devices);
Julien Grossholtz96098df2016-01-07 16:46:45 -0500237 return 0;
238 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900239
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800240 prev = list_entry(gpio_devices.prev, struct gpio_device, list);
241 if (prev->base + prev->ngpio <= gdev->base) {
242 /* add behind last entry */
243 list_add_tail(&gdev->list, &gpio_devices);
244 return 0;
245 }
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800246
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800247 list_for_each_entry_safe(prev, next, &gpio_devices, list) {
248 /* at the end of the list */
249 if (&next->list == &gpio_devices)
250 break;
251
252 /* add between prev and next */
253 if (prev->base + prev->ngpio <= gdev->base
254 && gdev->base + gdev->ngpio <= next->base) {
255 list_add(&gdev->list, &prev->list);
256 return 0;
257 }
258 }
259
260 dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n");
261 return -EBUSY;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900262}
263
Linus Walleijf881bab2015-09-23 16:20:43 -0700264/**
265 * Convert a GPIO name to its descriptor
266 */
267static struct gpio_desc *gpio_name_to_desc(const char * const name)
268{
Linus Walleijff2b1352015-10-20 11:10:38 +0200269 struct gpio_device *gdev;
Linus Walleijf881bab2015-09-23 16:20:43 -0700270 unsigned long flags;
271
272 spin_lock_irqsave(&gpio_lock, flags);
273
Linus Walleijff2b1352015-10-20 11:10:38 +0200274 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijf881bab2015-09-23 16:20:43 -0700275 int i;
276
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100277 for (i = 0; i != gdev->ngpio; ++i) {
278 struct gpio_desc *desc = &gdev->descs[i];
Linus Walleijf881bab2015-09-23 16:20:43 -0700279
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100280 if (!desc->name || !name)
Linus Walleijf881bab2015-09-23 16:20:43 -0700281 continue;
282
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100283 if (!strcmp(desc->name, name)) {
Linus Walleijf881bab2015-09-23 16:20:43 -0700284 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100285 return desc;
Linus Walleijf881bab2015-09-23 16:20:43 -0700286 }
287 }
288 }
289
290 spin_unlock_irqrestore(&gpio_lock, flags);
291
292 return NULL;
293}
294
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200295/*
296 * Takes the names from gc->names and checks if they are all unique. If they
297 * are, they are assigned to their gpio descriptors.
298 *
Bamvor Jian Zhanged379152015-11-14 16:43:20 +0800299 * Warning if one of the names is already used for a different GPIO.
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200300 */
301static int gpiochip_set_desc_names(struct gpio_chip *gc)
302{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100303 struct gpio_device *gdev = gc->gpiodev;
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200304 int i;
305
306 if (!gc->names)
307 return 0;
308
309 /* First check all names if they are unique */
310 for (i = 0; i != gc->ngpio; ++i) {
311 struct gpio_desc *gpio;
312
313 gpio = gpio_name_to_desc(gc->names[i]);
Linus Walleijf881bab2015-09-23 16:20:43 -0700314 if (gpio)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100315 dev_warn(&gdev->dev,
Linus Walleij34ffd852015-10-20 11:31:54 +0200316 "Detected name collision for GPIO name '%s'\n",
Linus Walleijf881bab2015-09-23 16:20:43 -0700317 gc->names[i]);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200318 }
319
320 /* Then add all names to the GPIO descriptors */
321 for (i = 0; i != gc->ngpio; ++i)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100322 gdev->descs[i].name = gc->names[i];
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200323
324 return 0;
325}
326
Linus Walleijd7c51b42016-04-26 10:35:29 +0200327/*
328 * GPIO line handle management
329 */
330
331/**
332 * struct linehandle_state - contains the state of a userspace handle
333 * @gdev: the GPIO device the handle pertains to
334 * @label: consumer label used to tag descriptors
335 * @descs: the GPIO descriptors held by this handle
336 * @numdescs: the number of descriptors held in the descs array
337 */
338struct linehandle_state {
339 struct gpio_device *gdev;
340 const char *label;
341 struct gpio_desc *descs[GPIOHANDLES_MAX];
342 u32 numdescs;
343};
344
Lars-Peter Clausene3e847c2016-10-18 16:54:05 +0200345#define GPIOHANDLE_REQUEST_VALID_FLAGS \
346 (GPIOHANDLE_REQUEST_INPUT | \
347 GPIOHANDLE_REQUEST_OUTPUT | \
348 GPIOHANDLE_REQUEST_ACTIVE_LOW | \
349 GPIOHANDLE_REQUEST_OPEN_DRAIN | \
350 GPIOHANDLE_REQUEST_OPEN_SOURCE)
351
Linus Walleijd7c51b42016-04-26 10:35:29 +0200352static long linehandle_ioctl(struct file *filep, unsigned int cmd,
353 unsigned long arg)
354{
355 struct linehandle_state *lh = filep->private_data;
356 void __user *ip = (void __user *)arg;
357 struct gpiohandle_data ghd;
358 int i;
359
360 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
361 int val;
362
Lars-Peter Clausen3eded5d2016-10-18 16:54:02 +0200363 memset(&ghd, 0, sizeof(ghd));
364
Linus Walleijd7c51b42016-04-26 10:35:29 +0200365 /* TODO: check if descriptors are really input */
366 for (i = 0; i < lh->numdescs; i++) {
367 val = gpiod_get_value_cansleep(lh->descs[i]);
368 if (val < 0)
369 return val;
370 ghd.values[i] = val;
371 }
372
373 if (copy_to_user(ip, &ghd, sizeof(ghd)))
374 return -EFAULT;
375
376 return 0;
377 } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
378 int vals[GPIOHANDLES_MAX];
379
380 /* TODO: check if descriptors are really output */
381 if (copy_from_user(&ghd, ip, sizeof(ghd)))
382 return -EFAULT;
383
384 /* Clamp all values to [0,1] */
385 for (i = 0; i < lh->numdescs; i++)
386 vals[i] = !!ghd.values[i];
387
388 /* Reuse the array setting function */
389 gpiod_set_array_value_complex(false,
390 true,
391 lh->numdescs,
392 lh->descs,
393 vals);
394 return 0;
395 }
396 return -EINVAL;
397}
398
399#ifdef CONFIG_COMPAT
400static long linehandle_ioctl_compat(struct file *filep, unsigned int cmd,
401 unsigned long arg)
402{
403 return linehandle_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
404}
405#endif
406
407static int linehandle_release(struct inode *inode, struct file *filep)
408{
409 struct linehandle_state *lh = filep->private_data;
410 struct gpio_device *gdev = lh->gdev;
411 int i;
412
413 for (i = 0; i < lh->numdescs; i++)
414 gpiod_free(lh->descs[i]);
415 kfree(lh->label);
416 kfree(lh);
417 put_device(&gdev->dev);
418 return 0;
419}
420
421static const struct file_operations linehandle_fileops = {
422 .release = linehandle_release,
423 .owner = THIS_MODULE,
424 .llseek = noop_llseek,
425 .unlocked_ioctl = linehandle_ioctl,
426#ifdef CONFIG_COMPAT
427 .compat_ioctl = linehandle_ioctl_compat,
428#endif
429};
430
431static int linehandle_create(struct gpio_device *gdev, void __user *ip)
432{
433 struct gpiohandle_request handlereq;
434 struct linehandle_state *lh;
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200435 struct file *file;
Timur Tabi31d04ca2018-03-29 13:29:12 -0500436 int fd, i, count = 0, ret;
Kent Gibson71bbfc92019-09-09 03:22:18 +0000437 u32 lflags;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200438
439 if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
440 return -EFAULT;
441 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
442 return -EINVAL;
443
Kent Gibson71bbfc92019-09-09 03:22:18 +0000444 lflags = handlereq.flags;
445
446 /*
447 * Do not allow both INPUT & OUTPUT flags to be set as they are
448 * contradictory.
449 */
450 if ((lflags & GPIOHANDLE_REQUEST_INPUT) &&
451 (lflags & GPIOHANDLE_REQUEST_OUTPUT))
452 return -EINVAL;
453
Linus Walleijd7c51b42016-04-26 10:35:29 +0200454 lh = kzalloc(sizeof(*lh), GFP_KERNEL);
455 if (!lh)
456 return -ENOMEM;
457 lh->gdev = gdev;
458 get_device(&gdev->dev);
459
460 /* Make sure this is terminated */
461 handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
462 if (strlen(handlereq.consumer_label)) {
463 lh->label = kstrdup(handlereq.consumer_label,
464 GFP_KERNEL);
465 if (!lh->label) {
466 ret = -ENOMEM;
467 goto out_free_lh;
468 }
469 }
470
471 /* Request each GPIO */
472 for (i = 0; i < handlereq.lines; i++) {
473 u32 offset = handlereq.lineoffsets[i];
Linus Walleijd7c51b42016-04-26 10:35:29 +0200474 struct gpio_desc *desc;
475
Lars-Peter Clausene405f9f2016-10-18 16:54:01 +0200476 if (offset >= gdev->ngpio) {
477 ret = -EINVAL;
478 goto out_free_descs;
479 }
480
Lars-Peter Clausene3e847c2016-10-18 16:54:05 +0200481 /* Return an error if a unknown flag is set */
482 if (lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) {
483 ret = -EINVAL;
484 goto out_free_descs;
485 }
486
Linus Walleijd7c51b42016-04-26 10:35:29 +0200487 desc = &gdev->descs[offset];
488 ret = gpiod_request(desc, lh->label);
489 if (ret)
490 goto out_free_descs;
491 lh->descs[i] = desc;
Ricardo Ribalda Delgadoe88ca672018-09-13 15:37:04 +0200492 count = i + 1;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200493
494 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
495 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
496 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
497 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
498 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
499 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
500
501 /*
502 * Lines have to be requested explicitly for input
503 * or output, else the line will be treated "as is".
504 */
505 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
506 int val = !!handlereq.default_values[i];
507
508 ret = gpiod_direction_output(desc, val);
509 if (ret)
510 goto out_free_descs;
511 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
512 ret = gpiod_direction_input(desc);
513 if (ret)
514 goto out_free_descs;
515 }
516 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
517 offset);
518 }
Linus Walleije2f608b2016-06-18 10:56:43 +0200519 /* Let i point at the last handle */
520 i--;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200521 lh->numdescs = handlereq.lines;
522
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200523 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200524 if (fd < 0) {
525 ret = fd;
526 goto out_free_descs;
527 }
528
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200529 file = anon_inode_getfile("gpio-linehandle",
530 &linehandle_fileops,
531 lh,
532 O_RDONLY | O_CLOEXEC);
533 if (IS_ERR(file)) {
534 ret = PTR_ERR(file);
535 goto out_put_unused_fd;
536 }
537
Linus Walleijd7c51b42016-04-26 10:35:29 +0200538 handlereq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200539 if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200540 /*
541 * fput() will trigger the release() callback, so do not go onto
542 * the regular error cleanup path here.
543 */
544 fput(file);
545 put_unused_fd(fd);
546 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +0200547 }
Linus Walleijd7c51b42016-04-26 10:35:29 +0200548
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200549 fd_install(fd, file);
550
Linus Walleijd7c51b42016-04-26 10:35:29 +0200551 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
552 lh->numdescs);
553
554 return 0;
555
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200556out_put_unused_fd:
557 put_unused_fd(fd);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200558out_free_descs:
Timur Tabi31d04ca2018-03-29 13:29:12 -0500559 for (i = 0; i < count; i++)
Linus Walleijd7c51b42016-04-26 10:35:29 +0200560 gpiod_free(lh->descs[i]);
561 kfree(lh->label);
562out_free_lh:
563 kfree(lh);
564 put_device(&gdev->dev);
565 return ret;
566}
567
Linus Walleij61f922d2016-06-02 11:30:15 +0200568/*
569 * GPIO line event management
570 */
571
572/**
573 * struct lineevent_state - contains the state of a userspace event
574 * @gdev: the GPIO device the event pertains to
575 * @label: consumer label used to tag descriptors
576 * @desc: the GPIO descriptor held by this event
577 * @eflags: the event flags this line was requested with
578 * @irq: the interrupt that trigger in response to events on this GPIO
579 * @wait: wait queue that handles blocking reads of events
580 * @events: KFIFO for the GPIO events
581 * @read_lock: mutex lock to protect reads from colliding with adding
582 * new events to the FIFO
583 */
584struct lineevent_state {
585 struct gpio_device *gdev;
586 const char *label;
587 struct gpio_desc *desc;
588 u32 eflags;
589 int irq;
590 wait_queue_head_t wait;
591 DECLARE_KFIFO(events, struct gpioevent_data, 16);
592 struct mutex read_lock;
593};
594
Lars-Peter Clausenac7dbb92016-10-18 16:54:06 +0200595#define GPIOEVENT_REQUEST_VALID_FLAGS \
596 (GPIOEVENT_REQUEST_RISING_EDGE | \
597 GPIOEVENT_REQUEST_FALLING_EDGE)
598
Linus Walleij61f922d2016-06-02 11:30:15 +0200599static unsigned int lineevent_poll(struct file *filep,
600 struct poll_table_struct *wait)
601{
602 struct lineevent_state *le = filep->private_data;
603 unsigned int events = 0;
604
605 poll_wait(filep, &le->wait, wait);
606
607 if (!kfifo_is_empty(&le->events))
608 events = POLLIN | POLLRDNORM;
609
610 return events;
611}
612
613
614static ssize_t lineevent_read(struct file *filep,
615 char __user *buf,
616 size_t count,
617 loff_t *f_ps)
618{
619 struct lineevent_state *le = filep->private_data;
620 unsigned int copied;
621 int ret;
622
623 if (count < sizeof(struct gpioevent_data))
624 return -EINVAL;
625
626 do {
627 if (kfifo_is_empty(&le->events)) {
628 if (filep->f_flags & O_NONBLOCK)
629 return -EAGAIN;
630
631 ret = wait_event_interruptible(le->wait,
632 !kfifo_is_empty(&le->events));
633 if (ret)
634 return ret;
635 }
636
637 if (mutex_lock_interruptible(&le->read_lock))
638 return -ERESTARTSYS;
639 ret = kfifo_to_user(&le->events, buf, count, &copied);
640 mutex_unlock(&le->read_lock);
641
642 if (ret)
643 return ret;
644
645 /*
646 * If we couldn't read anything from the fifo (a different
647 * thread might have been faster) we either return -EAGAIN if
648 * the file descriptor is non-blocking, otherwise we go back to
649 * sleep and wait for more data to arrive.
650 */
651 if (copied == 0 && (filep->f_flags & O_NONBLOCK))
652 return -EAGAIN;
653
654 } while (copied == 0);
655
656 return copied;
657}
658
659static int lineevent_release(struct inode *inode, struct file *filep)
660{
661 struct lineevent_state *le = filep->private_data;
662 struct gpio_device *gdev = le->gdev;
663
664 free_irq(le->irq, le);
665 gpiod_free(le->desc);
666 kfree(le->label);
667 kfree(le);
668 put_device(&gdev->dev);
669 return 0;
670}
671
672static long lineevent_ioctl(struct file *filep, unsigned int cmd,
673 unsigned long arg)
674{
675 struct lineevent_state *le = filep->private_data;
676 void __user *ip = (void __user *)arg;
677 struct gpiohandle_data ghd;
678
679 /*
680 * We can get the value for an event line but not set it,
681 * because it is input by definition.
682 */
683 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
684 int val;
685
Lars-Peter Clausend82aa4a2016-10-18 16:54:04 +0200686 memset(&ghd, 0, sizeof(ghd));
687
Linus Walleij61f922d2016-06-02 11:30:15 +0200688 val = gpiod_get_value_cansleep(le->desc);
689 if (val < 0)
690 return val;
691 ghd.values[0] = val;
692
693 if (copy_to_user(ip, &ghd, sizeof(ghd)))
694 return -EFAULT;
695
696 return 0;
697 }
698 return -EINVAL;
699}
700
701#ifdef CONFIG_COMPAT
702static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd,
703 unsigned long arg)
704{
705 return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
706}
707#endif
708
709static const struct file_operations lineevent_fileops = {
710 .release = lineevent_release,
711 .read = lineevent_read,
712 .poll = lineevent_poll,
713 .owner = THIS_MODULE,
714 .llseek = noop_llseek,
715 .unlocked_ioctl = lineevent_ioctl,
716#ifdef CONFIG_COMPAT
717 .compat_ioctl = lineevent_ioctl_compat,
718#endif
719};
720
Ben Dooks33265b12016-06-17 16:03:13 +0100721static irqreturn_t lineevent_irq_thread(int irq, void *p)
Linus Walleij61f922d2016-06-02 11:30:15 +0200722{
723 struct lineevent_state *le = p;
724 struct gpioevent_data ge;
Bartosz Golaszewskib680e222017-07-03 11:12:03 +0200725 int ret, level;
Linus Walleij61f922d2016-06-02 11:30:15 +0200726
Linus Walleijcc1fa4a2018-01-22 13:19:28 +0100727 /* Do not leak kernel stack to userspace */
728 memset(&ge, 0, sizeof(ge));
729
Linus Walleij61f922d2016-06-02 11:30:15 +0200730 ge.timestamp = ktime_get_real_ns();
Bartosz Golaszewskib680e222017-07-03 11:12:03 +0200731 level = gpiod_get_value_cansleep(le->desc);
Linus Walleij61f922d2016-06-02 11:30:15 +0200732
Bartosz Golaszewski78c42442017-06-23 13:45:16 +0200733 if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
734 && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200735 if (level)
736 /* Emit low-to-high event */
737 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
738 else
739 /* Emit high-to-low event */
740 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Bartosz Golaszewskib680e222017-07-03 11:12:03 +0200741 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE && level) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200742 /* Emit low-to-high event */
743 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
Bartosz Golaszewskib680e222017-07-03 11:12:03 +0200744 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE && !level) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200745 /* Emit high-to-low event */
746 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Arnd Bergmannbc0207a2016-06-16 11:02:41 +0200747 } else {
748 return IRQ_NONE;
Linus Walleij61f922d2016-06-02 11:30:15 +0200749 }
750
751 ret = kfifo_put(&le->events, ge);
752 if (ret != 0)
753 wake_up_poll(&le->wait, POLLIN);
754
755 return IRQ_HANDLED;
756}
757
758static int lineevent_create(struct gpio_device *gdev, void __user *ip)
759{
760 struct gpioevent_request eventreq;
761 struct lineevent_state *le;
762 struct gpio_desc *desc;
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200763 struct file *file;
Linus Walleij61f922d2016-06-02 11:30:15 +0200764 u32 offset;
765 u32 lflags;
766 u32 eflags;
767 int fd;
768 int ret;
769 int irqflags = 0;
770
771 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
772 return -EFAULT;
773
774 le = kzalloc(sizeof(*le), GFP_KERNEL);
775 if (!le)
776 return -ENOMEM;
777 le->gdev = gdev;
778 get_device(&gdev->dev);
779
780 /* Make sure this is terminated */
781 eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
782 if (strlen(eventreq.consumer_label)) {
783 le->label = kstrdup(eventreq.consumer_label,
784 GFP_KERNEL);
785 if (!le->label) {
786 ret = -ENOMEM;
787 goto out_free_le;
788 }
789 }
790
791 offset = eventreq.lineoffset;
792 lflags = eventreq.handleflags;
793 eflags = eventreq.eventflags;
794
Lars-Peter Clausenb8b0e3d2016-10-18 16:54:03 +0200795 if (offset >= gdev->ngpio) {
796 ret = -EINVAL;
797 goto out_free_label;
798 }
799
Lars-Peter Clausenac7dbb92016-10-18 16:54:06 +0200800 /* Return an error if a unknown flag is set */
801 if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
802 (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) {
803 ret = -EINVAL;
804 goto out_free_label;
805 }
806
Linus Walleij61f922d2016-06-02 11:30:15 +0200807 /* This is just wrong: we don't look for events on output lines */
Kent Gibsonc166e7d2019-09-09 03:24:06 +0000808 if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) ||
809 (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
810 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200811 ret = -EINVAL;
812 goto out_free_label;
813 }
814
815 desc = &gdev->descs[offset];
816 ret = gpiod_request(desc, le->label);
817 if (ret)
Uwe Kleine-König63e2ae92018-04-16 13:17:53 +0200818 goto out_free_label;
Linus Walleij61f922d2016-06-02 11:30:15 +0200819 le->desc = desc;
820 le->eflags = eflags;
821
822 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
823 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
Linus Walleij61f922d2016-06-02 11:30:15 +0200824
825 ret = gpiod_direction_input(desc);
826 if (ret)
827 goto out_free_desc;
828
829 le->irq = gpiod_to_irq(desc);
830 if (le->irq <= 0) {
831 ret = -ENODEV;
832 goto out_free_desc;
833 }
834
835 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
Michael Wu7b185852019-07-08 13:23:08 +0800836 irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
837 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
Linus Walleij61f922d2016-06-02 11:30:15 +0200838 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
Michael Wu7b185852019-07-08 13:23:08 +0800839 irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
840 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
Linus Walleij61f922d2016-06-02 11:30:15 +0200841 irqflags |= IRQF_ONESHOT;
842 irqflags |= IRQF_SHARED;
843
844 INIT_KFIFO(le->events);
845 init_waitqueue_head(&le->wait);
846 mutex_init(&le->read_lock);
847
848 /* Request a thread to read the events */
849 ret = request_threaded_irq(le->irq,
850 NULL,
851 lineevent_irq_thread,
852 irqflags,
853 le->label,
854 le);
855 if (ret)
856 goto out_free_desc;
857
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200858 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleij61f922d2016-06-02 11:30:15 +0200859 if (fd < 0) {
860 ret = fd;
861 goto out_free_irq;
862 }
863
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200864 file = anon_inode_getfile("gpio-event",
865 &lineevent_fileops,
866 le,
867 O_RDONLY | O_CLOEXEC);
868 if (IS_ERR(file)) {
869 ret = PTR_ERR(file);
870 goto out_put_unused_fd;
871 }
872
Linus Walleij61f922d2016-06-02 11:30:15 +0200873 eventreq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200874 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200875 /*
876 * fput() will trigger the release() callback, so do not go onto
877 * the regular error cleanup path here.
878 */
879 fput(file);
880 put_unused_fd(fd);
881 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +0200882 }
Linus Walleij61f922d2016-06-02 11:30:15 +0200883
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200884 fd_install(fd, file);
885
Linus Walleij61f922d2016-06-02 11:30:15 +0200886 return 0;
887
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200888out_put_unused_fd:
889 put_unused_fd(fd);
Linus Walleij61f922d2016-06-02 11:30:15 +0200890out_free_irq:
891 free_irq(le->irq, le);
892out_free_desc:
893 gpiod_free(le->desc);
894out_free_label:
895 kfree(le->label);
896out_free_le:
897 kfree(le);
898 put_device(&gdev->dev);
899 return ret;
900}
901
Linus Walleij3c702e92015-10-21 15:29:53 +0200902/**
903 * gpio_ioctl() - ioctl handler for the GPIO chardev
904 */
905static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
906{
907 struct gpio_device *gdev = filp->private_data;
908 struct gpio_chip *chip = gdev->chip;
Linus Walleij8b92e172016-05-27 14:24:04 +0200909 void __user *ip = (void __user *)arg;
Linus Walleij3c702e92015-10-21 15:29:53 +0200910
911 /* We fail any subsequent ioctl():s when the chip is gone */
912 if (!chip)
913 return -ENODEV;
914
Linus Walleij521a2ad2016-02-12 22:25:22 +0100915 /* Fill in the struct and pass to userspace */
Linus Walleij3c702e92015-10-21 15:29:53 +0200916 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
Linus Walleij521a2ad2016-02-12 22:25:22 +0100917 struct gpiochip_info chipinfo;
918
Lars-Peter Clausen0f4bbb22016-10-18 16:54:00 +0200919 memset(&chipinfo, 0, sizeof(chipinfo));
920
Linus Walleij3c702e92015-10-21 15:29:53 +0200921 strncpy(chipinfo.name, dev_name(&gdev->dev),
922 sizeof(chipinfo.name));
923 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
Linus Walleijdf4878e2016-02-12 14:48:23 +0100924 strncpy(chipinfo.label, gdev->label,
925 sizeof(chipinfo.label));
926 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100927 chipinfo.lines = gdev->ngpio;
Linus Walleij3c702e92015-10-21 15:29:53 +0200928 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
929 return -EFAULT;
930 return 0;
Linus Walleij521a2ad2016-02-12 22:25:22 +0100931 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
932 struct gpioline_info lineinfo;
933 struct gpio_desc *desc;
934
935 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
936 return -EFAULT;
Lars-Peter Clausen1f1cc452016-10-18 16:53:59 +0200937 if (lineinfo.line_offset >= gdev->ngpio)
Linus Walleij521a2ad2016-02-12 22:25:22 +0100938 return -EINVAL;
939
940 desc = &gdev->descs[lineinfo.line_offset];
941 if (desc->name) {
942 strncpy(lineinfo.name, desc->name,
943 sizeof(lineinfo.name));
944 lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
945 } else {
946 lineinfo.name[0] = '\0';
947 }
948 if (desc->label) {
Linus Walleij214338e2016-02-25 21:01:48 +0100949 strncpy(lineinfo.consumer, desc->label,
950 sizeof(lineinfo.consumer));
951 lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +0100952 } else {
Linus Walleij214338e2016-02-25 21:01:48 +0100953 lineinfo.consumer[0] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +0100954 }
955
956 /*
957 * Userspace only need to know that the kernel is using
958 * this GPIO so it can't use it.
959 */
960 lineinfo.flags = 0;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100961 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
962 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
963 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
964 test_bit(FLAG_EXPORT, &desc->flags) ||
965 test_bit(FLAG_SYSFS, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100966 lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100967 if (test_bit(FLAG_IS_OUT, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100968 lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100969 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100970 lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100971 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Bartosz Golaszewski640d77c2019-08-06 13:41:51 +0200972 lineinfo.flags |= (GPIOLINE_FLAG_OPEN_DRAIN |
973 GPIOLINE_FLAG_IS_OUT);
Linus Walleij9d8cc892016-02-22 13:44:53 +0100974 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Bartosz Golaszewski640d77c2019-08-06 13:41:51 +0200975 lineinfo.flags |= (GPIOLINE_FLAG_OPEN_SOURCE |
976 GPIOLINE_FLAG_IS_OUT);
Linus Walleij521a2ad2016-02-12 22:25:22 +0100977
978 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
979 return -EFAULT;
980 return 0;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200981 } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
982 return linehandle_create(gdev, ip);
Linus Walleij61f922d2016-06-02 11:30:15 +0200983 } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
984 return lineevent_create(gdev, ip);
Linus Walleij3c702e92015-10-21 15:29:53 +0200985 }
986 return -EINVAL;
987}
988
Linus Walleij8b92e172016-05-27 14:24:04 +0200989#ifdef CONFIG_COMPAT
990static long gpio_ioctl_compat(struct file *filp, unsigned int cmd,
991 unsigned long arg)
992{
993 return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
994}
995#endif
996
Linus Walleij3c702e92015-10-21 15:29:53 +0200997/**
998 * gpio_chrdev_open() - open the chardev for ioctl operations
999 * @inode: inode for this chardev
1000 * @filp: file struct for storing private data
1001 * Returns 0 on success
1002 */
1003static int gpio_chrdev_open(struct inode *inode, struct file *filp)
1004{
1005 struct gpio_device *gdev = container_of(inode->i_cdev,
1006 struct gpio_device, chrdev);
1007
1008 /* Fail on open if the backing gpiochip is gone */
1009 if (!gdev || !gdev->chip)
1010 return -ENODEV;
1011 get_device(&gdev->dev);
1012 filp->private_data = gdev;
Lars-Peter Clausen5f654072016-11-30 13:05:21 +01001013
1014 return nonseekable_open(inode, filp);
Linus Walleij3c702e92015-10-21 15:29:53 +02001015}
1016
1017/**
1018 * gpio_chrdev_release() - close chardev after ioctl operations
1019 * @inode: inode for this chardev
1020 * @filp: file struct for storing private data
1021 * Returns 0 on success
1022 */
1023static int gpio_chrdev_release(struct inode *inode, struct file *filp)
1024{
1025 struct gpio_device *gdev = container_of(inode->i_cdev,
1026 struct gpio_device, chrdev);
1027
1028 if (!gdev)
1029 return -ENODEV;
1030 put_device(&gdev->dev);
1031 return 0;
1032}
1033
1034
1035static const struct file_operations gpio_fileops = {
1036 .release = gpio_chrdev_release,
1037 .open = gpio_chrdev_open,
1038 .owner = THIS_MODULE,
Lars-Peter Clausen5f654072016-11-30 13:05:21 +01001039 .llseek = no_llseek,
Linus Walleij3c702e92015-10-21 15:29:53 +02001040 .unlocked_ioctl = gpio_ioctl,
Linus Walleij8b92e172016-05-27 14:24:04 +02001041#ifdef CONFIG_COMPAT
1042 .compat_ioctl = gpio_ioctl_compat,
1043#endif
Linus Walleij3c702e92015-10-21 15:29:53 +02001044};
1045
Linus Walleijff2b1352015-10-20 11:10:38 +02001046static void gpiodevice_release(struct device *dev)
1047{
1048 struct gpio_device *gdev = dev_get_drvdata(dev);
1049
1050 list_del(&gdev->list);
1051 ida_simple_remove(&gpio_ida, gdev->id);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001052 kfree(gdev->label);
1053 kfree(gdev->descs);
Linus Walleij9efd9e62016-02-09 14:27:42 +01001054 kfree(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001055}
1056
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001057static int gpiochip_setup_dev(struct gpio_device *gdev)
1058{
1059 int status;
1060
1061 cdev_init(&gdev->chrdev, &gpio_fileops);
1062 gdev->chrdev.owner = THIS_MODULE;
1063 gdev->chrdev.kobj.parent = &gdev->dev.kobj;
1064 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
1065 status = cdev_add(&gdev->chrdev, gdev->dev.devt, 1);
1066 if (status < 0)
1067 chip_warn(gdev->chip, "failed to add char device %d:%d\n",
1068 MAJOR(gpio_devt), gdev->id);
1069 else
1070 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
1071 MAJOR(gpio_devt), gdev->id);
1072 status = device_add(&gdev->dev);
1073 if (status)
1074 goto err_remove_chardev;
1075
1076 status = gpiochip_sysfs_register(gdev);
1077 if (status)
1078 goto err_remove_device;
1079
1080 /* From this point, the .release() function cleans up gpio_device */
1081 gdev->dev.release = gpiodevice_release;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001082 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
1083 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
1084 dev_name(&gdev->dev), gdev->chip->label ? : "generic");
1085
1086 return 0;
1087
1088err_remove_device:
1089 device_del(&gdev->dev);
1090err_remove_chardev:
1091 cdev_del(&gdev->chrdev);
1092 return status;
1093}
1094
1095static void gpiochip_setup_devs(void)
1096{
1097 struct gpio_device *gdev;
1098 int err;
1099
1100 list_for_each_entry(gdev, &gpio_devices, list) {
1101 err = gpiochip_setup_dev(gdev);
1102 if (err)
1103 pr_err("%s: Failed to initialize gpio device (%d)\n",
1104 dev_name(&gdev->dev), err);
1105 }
1106}
1107
Anton Vorontsov169b6a72008-04-28 02:14:47 -07001108/**
Linus Walleijb08ea352015-12-03 15:14:13 +01001109 * gpiochip_add_data() - register a gpio_chip
David Brownelld2876d02008-02-04 22:28:20 -08001110 * @chip: the chip to register, with chip->base initialized
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001111 * Context: potentially before irqs will work
David Brownelld2876d02008-02-04 22:28:20 -08001112 *
1113 * Returns a negative errno if the chip can't be registered, such as
1114 * because the chip->base is invalid or already associated with a
1115 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001116 *
Linus Walleijb08ea352015-12-03 15:14:13 +01001117 * When gpiochip_add_data() is called very early during boot, so that GPIOs
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08001118 * can be freely used, the chip->parent device must be registered before
David Brownelld8f388d2008-07-25 01:46:07 -07001119 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1120 * for GPIOs will fail rudely.
1121 *
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001122 * gpiochip_add_data() must only be called after gpiolib initialization,
1123 * ie after core_initcall().
1124 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001125 * If chip->base is negative, this requests dynamic assignment of
1126 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001127 */
Linus Walleijb08ea352015-12-03 15:14:13 +01001128int gpiochip_add_data(struct gpio_chip *chip, void *data)
David Brownelld2876d02008-02-04 22:28:20 -08001129{
1130 unsigned long flags;
1131 int status = 0;
Linus Walleijff2b1352015-10-20 11:10:38 +02001132 unsigned i;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001133 int base = chip->base;
Linus Walleijff2b1352015-10-20 11:10:38 +02001134 struct gpio_device *gdev;
David Brownelld2876d02008-02-04 22:28:20 -08001135
Linus Walleijff2b1352015-10-20 11:10:38 +02001136 /*
1137 * First: allocate and populate the internal stat container, and
1138 * set up the struct device.
1139 */
Josh Cartwright969f07b2016-02-17 16:44:15 -06001140 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
Linus Walleijff2b1352015-10-20 11:10:38 +02001141 if (!gdev)
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001142 return -ENOMEM;
Linus Walleij3c702e92015-10-21 15:29:53 +02001143 gdev->dev.bus = &gpio_bus_type;
Linus Walleijff2b1352015-10-20 11:10:38 +02001144 gdev->chip = chip;
1145 chip->gpiodev = gdev;
1146 if (chip->parent) {
1147 gdev->dev.parent = chip->parent;
1148 gdev->dev.of_node = chip->parent->of_node;
Thierry Redingacc6e332016-07-05 14:11:14 +02001149 }
1150
Linus Walleijff2b1352015-10-20 11:10:38 +02001151#ifdef CONFIG_OF_GPIO
1152 /* If the gpiochip has an assigned OF node this takes precedence */
Thierry Redingacc6e332016-07-05 14:11:14 +02001153 if (chip->of_node)
1154 gdev->dev.of_node = chip->of_node;
Linus Walleijff2b1352015-10-20 11:10:38 +02001155#endif
Thierry Redingacc6e332016-07-05 14:11:14 +02001156
Linus Walleijff2b1352015-10-20 11:10:38 +02001157 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
1158 if (gdev->id < 0) {
1159 status = gdev->id;
1160 goto err_free_gdev;
1161 }
1162 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
1163 device_initialize(&gdev->dev);
1164 dev_set_drvdata(&gdev->dev, gdev);
1165 if (chip->parent && chip->parent->driver)
1166 gdev->owner = chip->parent->driver->owner;
1167 else if (chip->owner)
1168 /* TODO: remove chip->owner */
1169 gdev->owner = chip->owner;
1170 else
1171 gdev->owner = THIS_MODULE;
David Brownelld2876d02008-02-04 22:28:20 -08001172
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001173 gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001174 if (!gdev->descs) {
Linus Walleijff2b1352015-10-20 11:10:38 +02001175 status = -ENOMEM;
Vladimir Zapolskiy2e668d72018-11-02 15:39:43 +02001176 goto err_free_ida;
Linus Walleijff2b1352015-10-20 11:10:38 +02001177 }
1178
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001179 if (chip->ngpio == 0) {
1180 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
Linus Walleijff2b1352015-10-20 11:10:38 +02001181 status = -EINVAL;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001182 goto err_free_descs;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001183 }
Linus Walleijdf4878e2016-02-12 14:48:23 +01001184
1185 if (chip->label)
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001186 gdev->label = kstrdup(chip->label, GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001187 else
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001188 gdev->label = kstrdup("unknown", GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001189 if (!gdev->label) {
1190 status = -ENOMEM;
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001191 goto err_free_descs;
Linus Walleijdf4878e2016-02-12 14:48:23 +01001192 }
1193
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001194 gdev->ngpio = chip->ngpio;
Linus Walleij43c54ec2016-02-11 11:37:48 +01001195 gdev->data = data;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001196
David Brownelld2876d02008-02-04 22:28:20 -08001197 spin_lock_irqsave(&gpio_lock, flags);
1198
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001199 /*
1200 * TODO: this allocates a Linux GPIO number base in the global
1201 * GPIO numberspace for this chip. In the long run we want to
1202 * get *rid* of this numberspace and use only descriptors, but
1203 * it may be a pipe dream. It will not happen before we get rid
1204 * of the sysfs interface anyways.
1205 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001206 if (base < 0) {
1207 base = gpiochip_find_base(chip->ngpio);
1208 if (base < 0) {
1209 status = base;
Johan Hovold225fce82015-01-12 17:12:25 +01001210 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001211 goto err_free_label;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001212 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001213 /*
1214 * TODO: it should not be necessary to reflect the assigned
1215 * base outside of the GPIO subsystem. Go over drivers and
1216 * see if anyone makes use of this, else drop this and assign
1217 * a poison instead.
1218 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001219 chip->base = base;
1220 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001221 gdev->base = base;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001222
Linus Walleijff2b1352015-10-20 11:10:38 +02001223 status = gpiodev_add_to_list(gdev);
Johan Hovold05aa5202015-01-12 17:12:26 +01001224 if (status) {
1225 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001226 goto err_free_label;
Johan Hovold05aa5202015-01-12 17:12:26 +01001227 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001228
Linus Walleij545ebd92016-05-30 17:11:59 +02001229 spin_unlock_irqrestore(&gpio_lock, flags);
1230
Linus Walleijff2b1352015-10-20 11:10:38 +02001231 for (i = 0; i < chip->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001232 struct gpio_desc *desc = &gdev->descs[i];
David Brownelld8f388d2008-07-25 01:46:07 -07001233
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001234 desc->gdev = gdev;
Linus Walleij72d32002016-04-28 13:33:59 +02001235 /*
1236 * REVISIT: most hardware initializes GPIOs as inputs
1237 * (often with pullups enabled) so power usage is
1238 * minimized. Linux code should set the gpio direction
1239 * first thing; but until it does, and in case
1240 * chip->get_direction is not set, we may expose the
1241 * wrong direction in sysfs.
Johan Hovold05aa5202015-01-12 17:12:26 +01001242 */
Linus Walleij72d32002016-04-28 13:33:59 +02001243
1244 if (chip->get_direction) {
1245 /*
1246 * If we have .get_direction, set up the initial
1247 * direction flag from the hardware.
1248 */
1249 int dir = chip->get_direction(chip, i);
1250
1251 if (!dir)
1252 set_bit(FLAG_IS_OUT, &desc->flags);
1253 } else if (!chip->direction_input) {
1254 /*
1255 * If the chip lacks the .direction_input callback
1256 * we logically assume all lines are outputs.
1257 */
1258 set_bit(FLAG_IS_OUT, &desc->flags);
1259 }
David Brownelld2876d02008-02-04 22:28:20 -08001260 }
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001261
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301262#ifdef CONFIG_PINCTRL
Linus Walleij20ec3e32016-02-11 11:03:06 +01001263 INIT_LIST_HEAD(&gdev->pin_ranges);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301264#endif
1265
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001266 status = gpiochip_set_desc_names(chip);
1267 if (status)
1268 goto err_remove_from_list;
1269
Mika Westerberg79b804c2016-09-20 15:15:21 +03001270 status = gpiochip_irqchip_init_valid_mask(chip);
1271 if (status)
1272 goto err_remove_from_list;
1273
Tomeu Vizoso28355f82015-07-14 10:29:54 +02001274 status = of_gpiochip_add(chip);
1275 if (status)
1276 goto err_remove_chip;
1277
Mika Westerberg664e3e52014-01-08 12:40:54 +02001278 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001279
Linus Walleij3c702e92015-10-21 15:29:53 +02001280 /*
1281 * By first adding the chardev, and then adding the device,
1282 * we get a device node entry in sysfs under
1283 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1284 * coldplug of device nodes and other udev business.
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001285 * We can do this only if gpiolib has been initialized.
1286 * Otherwise, defer until later.
Linus Walleij3c702e92015-10-21 15:29:53 +02001287 */
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001288 if (gpiolib_initialized) {
1289 status = gpiochip_setup_dev(gdev);
1290 if (status)
1291 goto err_remove_chip;
1292 }
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001293 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001294
Johan Hovold225fce82015-01-12 17:12:25 +01001295err_remove_chip:
1296 acpi_gpiochip_remove(chip);
Johan Hovold6d867502015-05-04 17:23:25 +02001297 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +01001298 of_gpiochip_remove(chip);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001299 gpiochip_irqchip_free_valid_mask(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001300err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +01001301 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001302 list_del(&gdev->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001303 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001304err_free_label:
1305 kfree(gdev->label);
1306err_free_descs:
1307 kfree(gdev->descs);
Vladimir Zapolskiy2e668d72018-11-02 15:39:43 +02001308err_free_ida:
Linus Walleijff2b1352015-10-20 11:10:38 +02001309 ida_simple_remove(&gpio_ida, gdev->id);
Vladimir Zapolskiy2e668d72018-11-02 15:39:43 +02001310err_free_gdev:
David Brownelld2876d02008-02-04 22:28:20 -08001311 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +02001312 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001313 gdev->base, gdev->base + gdev->ngpio - 1,
1314 chip->label ? : "generic");
1315 kfree(gdev);
David Brownelld2876d02008-02-04 22:28:20 -08001316 return status;
1317}
Linus Walleijb08ea352015-12-03 15:14:13 +01001318EXPORT_SYMBOL_GPL(gpiochip_add_data);
David Brownelld2876d02008-02-04 22:28:20 -08001319
1320/**
Linus Walleij43c54ec2016-02-11 11:37:48 +01001321 * gpiochip_get_data() - get per-subdriver data for the chip
1322 */
1323void *gpiochip_get_data(struct gpio_chip *chip)
1324{
1325 return chip->gpiodev->data;
1326}
1327EXPORT_SYMBOL_GPL(gpiochip_get_data);
1328
1329/**
David Brownelld2876d02008-02-04 22:28:20 -08001330 * gpiochip_remove() - unregister a gpio_chip
1331 * @chip: the chip to unregister
1332 *
1333 * A gpio_chip with any GPIOs still requested may not be removed.
1334 */
abdoulaye berthee1db1702014-07-05 18:28:50 +02001335void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -08001336{
Linus Walleijff2b1352015-10-20 11:10:38 +02001337 struct gpio_device *gdev = chip->gpiodev;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001338 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001339 unsigned long flags;
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001340 unsigned i;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001341 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -08001342
Linus Walleijff2b1352015-10-20 11:10:38 +02001343 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
Linus Walleijafbc4f32016-02-09 13:21:06 +01001344 gpiochip_sysfs_unregister(gdev);
Geert Uytterhoeven86673e92016-12-19 18:29:23 +01001345 gpiochip_free_hogs(chip);
Bamvor Jian Zhangbd203bd2016-02-20 13:13:19 +08001346 /* Numb the device, cancelling all outstanding operations */
1347 gdev->chip = NULL;
Johan Hovold00acc3d2015-01-12 17:12:27 +01001348 gpiochip_irqchip_remove(chip);
Mika Westerberg6072b9d2014-03-10 14:54:53 +02001349 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +01001350 gpiochip_remove_pin_ranges(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001351 of_gpiochip_remove(chip);
Linus Walleij43c54ec2016-02-11 11:37:48 +01001352 /*
1353 * We accept no more calls into the driver from this point, so
1354 * NULL the driver data pointer
1355 */
1356 gdev->data = NULL;
Anton Vorontsov391c9702010-06-08 07:48:17 -06001357
Johan Hovold6798aca2015-01-12 17:12:28 +01001358 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001359 for (i = 0; i < gdev->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001360 desc = &gdev->descs[i];
Johan Hovoldfab28b82015-05-04 17:10:27 +02001361 if (test_bit(FLAG_REQUESTED, &desc->flags))
1362 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -08001363 }
David Brownelld2876d02008-02-04 22:28:20 -08001364 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001365
Johan Hovoldfab28b82015-05-04 17:10:27 +02001366 if (requested)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001367 dev_crit(&gdev->dev,
Linus Walleij58383c72015-11-04 09:56:26 +01001368 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +02001369
Linus Walleijff2b1352015-10-20 11:10:38 +02001370 /*
1371 * The gpiochip side puts its use of the device to rest here:
1372 * if there are no userspace clients, the chardev and device will
1373 * be removed, else it will be dangling until the last user is
1374 * gone.
1375 */
Ricardo Ribalda Delgadof4833b82016-06-03 19:10:02 +02001376 cdev_del(&gdev->chrdev);
1377 device_del(&gdev->dev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001378 put_device(&gdev->dev);
David Brownelld2876d02008-02-04 22:28:20 -08001379}
1380EXPORT_SYMBOL_GPL(gpiochip_remove);
1381
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301382static void devm_gpio_chip_release(struct device *dev, void *res)
1383{
1384 struct gpio_chip *chip = *(struct gpio_chip **)res;
1385
1386 gpiochip_remove(chip);
1387}
1388
1389static int devm_gpio_chip_match(struct device *dev, void *res, void *data)
1390
1391{
1392 struct gpio_chip **r = res;
1393
1394 if (!r || !*r) {
1395 WARN_ON(!r || !*r);
1396 return 0;
1397 }
1398
1399 return *r == data;
1400}
1401
1402/**
1403 * devm_gpiochip_add_data() - Resource manager piochip_add_data()
1404 * @dev: the device pointer on which irq_chip belongs to.
1405 * @chip: the chip to register, with chip->base initialized
1406 * Context: potentially before irqs will work
1407 *
1408 * Returns a negative errno if the chip can't be registered, such as
1409 * because the chip->base is invalid or already associated with a
1410 * different chip. Otherwise it returns zero as a success code.
1411 *
1412 * The gpio chip automatically be released when the device is unbound.
1413 */
1414int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
1415 void *data)
1416{
1417 struct gpio_chip **ptr;
1418 int ret;
1419
1420 ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
1421 GFP_KERNEL);
1422 if (!ptr)
1423 return -ENOMEM;
1424
1425 ret = gpiochip_add_data(chip, data);
1426 if (ret < 0) {
1427 devres_free(ptr);
1428 return ret;
1429 }
1430
1431 *ptr = chip;
1432 devres_add(dev, ptr);
1433
1434 return 0;
1435}
1436EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
1437
1438/**
1439 * devm_gpiochip_remove() - Resource manager of gpiochip_remove()
1440 * @dev: device for which which resource was allocated
1441 * @chip: the chip to remove
1442 *
1443 * A gpio_chip with any GPIOs still requested may not be removed.
1444 */
1445void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip)
1446{
1447 int ret;
1448
1449 ret = devres_release(dev, devm_gpio_chip_release,
1450 devm_gpio_chip_match, chip);
1451 if (!ret)
1452 WARN_ON(ret);
1453}
1454EXPORT_SYMBOL_GPL(devm_gpiochip_remove);
1455
Grant Likely594fa262010-06-08 07:48:16 -06001456/**
1457 * gpiochip_find() - iterator for locating a specific gpio_chip
1458 * @data: data to pass to match function
1459 * @callback: Callback function to check gpio_chip
1460 *
1461 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1462 * determined by a user supplied @match callback. The callback should return
1463 * 0 if the device doesn't match and non-zero if it does. If the callback is
1464 * non-zero, this function will return to the caller and not iterate over any
1465 * more gpio_chips.
1466 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001467struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001468 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001469 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001470{
Linus Walleijff2b1352015-10-20 11:10:38 +02001471 struct gpio_device *gdev;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001472 struct gpio_chip *chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -06001473 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001474
1475 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001476 list_for_each_entry(gdev, &gpio_devices, list)
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001477 if (gdev->chip && match(gdev->chip, data)) {
1478 chip = gdev->chip;
Grant Likely594fa262010-06-08 07:48:16 -06001479 break;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001480 }
Linus Walleijff2b1352015-10-20 11:10:38 +02001481
Grant Likely594fa262010-06-08 07:48:16 -06001482 spin_unlock_irqrestore(&gpio_lock, flags);
1483
1484 return chip;
1485}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001486EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001487
Alexandre Courbot79697ef2013-11-16 21:39:32 +09001488static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1489{
1490 const char *name = data;
1491
1492 return !strcmp(chip->label, name);
1493}
1494
1495static struct gpio_chip *find_chip_by_name(const char *name)
1496{
1497 return gpiochip_find((void *)name, gpiochip_match_name);
1498}
1499
Linus Walleij14250522014-03-25 10:40:18 +01001500#ifdef CONFIG_GPIOLIB_IRQCHIP
1501
1502/*
1503 * The following is irqchip helper code for gpiochips.
1504 */
1505
Mika Westerberg79b804c2016-09-20 15:15:21 +03001506static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1507{
1508 int i;
1509
1510 if (!gpiochip->irq_need_valid_mask)
1511 return 0;
1512
1513 gpiochip->irq_valid_mask = kcalloc(BITS_TO_LONGS(gpiochip->ngpio),
1514 sizeof(long), GFP_KERNEL);
1515 if (!gpiochip->irq_valid_mask)
1516 return -ENOMEM;
1517
1518 /* Assume by default all GPIOs are valid */
1519 for (i = 0; i < gpiochip->ngpio; i++)
1520 set_bit(i, gpiochip->irq_valid_mask);
1521
1522 return 0;
1523}
1524
1525static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1526{
1527 kfree(gpiochip->irq_valid_mask);
1528 gpiochip->irq_valid_mask = NULL;
1529}
1530
1531static bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
1532 unsigned int offset)
1533{
1534 /* No mask means all valid */
1535 if (likely(!gpiochip->irq_valid_mask))
1536 return true;
1537 return test_bit(offset, gpiochip->irq_valid_mask);
1538}
1539
Linus Walleij14250522014-03-25 10:40:18 +01001540/**
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001541 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
1542 * @gpiochip: the gpiochip to set the irqchip chain to
1543 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01001544 * @parent_irq: the irq number corresponding to the parent IRQ for this
1545 * chained irqchip
1546 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001547 * coming out of the gpiochip. If the interrupt is nested rather than
1548 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +01001549 */
1550void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
1551 struct irq_chip *irqchip,
1552 int parent_irq,
1553 irq_flow_handler_t parent_handler)
1554{
Linus Walleij83141a72014-09-26 13:50:12 +02001555 unsigned int offset;
1556
Linus Walleij83141a72014-09-26 13:50:12 +02001557 if (!gpiochip->irqdomain) {
1558 chip_err(gpiochip, "called %s before setting up irqchip\n",
1559 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +02001560 return;
1561 }
1562
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001563 if (parent_handler) {
1564 if (gpiochip->can_sleep) {
1565 chip_err(gpiochip,
1566 "you cannot have chained interrupts on a "
1567 "chip that may sleep\n");
1568 return;
1569 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001570 /*
1571 * The parent irqchip is already using the chip_data for this
1572 * irqchip, so our callbacks simply use the handler_data.
1573 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +02001574 irq_set_chained_handler_and_data(parent_irq, parent_handler,
1575 gpiochip);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001576
1577 gpiochip->irq_parent = parent_irq;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001578 }
Linus Walleij83141a72014-09-26 13:50:12 +02001579
1580 /* Set the parent IRQ for all affected IRQs */
Mika Westerberg79b804c2016-09-20 15:15:21 +03001581 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1582 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1583 continue;
Linus Walleij83141a72014-09-26 13:50:12 +02001584 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
1585 parent_irq);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001586 }
Linus Walleij14250522014-03-25 10:40:18 +01001587}
1588EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
1589
1590/**
1591 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1592 * @d: the irqdomain used by this irqchip
1593 * @irq: the global irq number used by this GPIO irqchip irq
1594 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1595 *
1596 * This function will set up the mapping for a certain IRQ line on a
1597 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1598 * stored inside the gpiochip.
1599 */
1600static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1601 irq_hw_number_t hwirq)
1602{
1603 struct gpio_chip *chip = d->host_data;
1604
Linus Walleij14250522014-03-25 10:40:18 +01001605 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001606 /*
1607 * This lock class tells lockdep that GPIO irqs are in a different
1608 * category than their parents, so it won't report false recursion.
1609 */
1610 irq_set_lockdep_class(irq, chip->lock_key);
Linus Walleij7633fb92014-04-09 13:20:38 +02001611 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
Linus Walleij1c8732b2014-04-09 13:34:39 +02001612 /* Chips that can sleep need nested thread handlers */
Octavian Purdila295494a2014-09-19 23:22:44 +03001613 if (chip->can_sleep && !chip->irq_not_threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02001614 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +01001615 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -05001616
Linus Walleij1333b902014-04-23 16:45:12 +02001617 /*
1618 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1619 * is passed as default type.
1620 */
1621 if (chip->irq_default_type != IRQ_TYPE_NONE)
1622 irq_set_irq_type(irq, chip->irq_default_type);
Linus Walleij14250522014-03-25 10:40:18 +01001623
1624 return 0;
1625}
1626
Linus Walleijc3626fd2014-03-28 20:42:01 +01001627static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
1628{
Linus Walleij1c8732b2014-04-09 13:34:39 +02001629 struct gpio_chip *chip = d->host_data;
1630
Linus Walleij1c8732b2014-04-09 13:34:39 +02001631 if (chip->can_sleep)
1632 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001633 irq_set_chip_and_handler(irq, NULL, NULL);
1634 irq_set_chip_data(irq, NULL);
1635}
1636
Linus Walleij14250522014-03-25 10:40:18 +01001637static const struct irq_domain_ops gpiochip_domain_ops = {
1638 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +01001639 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +01001640 /* Virtually all GPIO irqchips are twocell:ed */
1641 .xlate = irq_domain_xlate_twocell,
1642};
1643
1644static int gpiochip_irq_reqres(struct irq_data *d)
1645{
1646 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1647
Linus Walleijff2b1352015-10-20 11:10:38 +02001648 if (!try_module_get(chip->gpiodev->owner))
Grygorii Strashko5b76e792015-06-25 20:30:50 +03001649 return -ENODEV;
1650
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001651 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +01001652 chip_err(chip,
1653 "unable to lock HW IRQ %lu for IRQ\n",
1654 d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001655 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001656 return -EINVAL;
1657 }
1658 return 0;
1659}
1660
1661static void gpiochip_irq_relres(struct irq_data *d)
1662{
1663 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1664
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001665 gpiochip_unlock_as_irq(chip, d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001666 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001667}
1668
1669static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
1670{
1671 return irq_find_mapping(chip->irqdomain, offset);
1672}
1673
1674/**
1675 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1676 * @gpiochip: the gpiochip to remove the irqchip from
1677 *
1678 * This is called only from gpiochip_remove()
1679 */
1680static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
1681{
Linus Walleijc3626fd2014-03-28 20:42:01 +01001682 unsigned int offset;
1683
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001684 acpi_gpiochip_free_interrupts(gpiochip);
1685
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001686 if (gpiochip->irq_parent) {
1687 irq_set_chained_handler(gpiochip->irq_parent, NULL);
1688 irq_set_handler_data(gpiochip->irq_parent, NULL);
1689 }
1690
Linus Walleijc3626fd2014-03-28 20:42:01 +01001691 /* Remove all IRQ mappings and delete the domain */
1692 if (gpiochip->irqdomain) {
Mika Westerberg79b804c2016-09-20 15:15:21 +03001693 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1694 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1695 continue;
Grygorii Strashkoe3893382014-09-25 19:09:23 +03001696 irq_dispose_mapping(
1697 irq_find_mapping(gpiochip->irqdomain, offset));
Mika Westerberg79b804c2016-09-20 15:15:21 +03001698 }
Linus Walleij14250522014-03-25 10:40:18 +01001699 irq_domain_remove(gpiochip->irqdomain);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001700 }
Linus Walleij14250522014-03-25 10:40:18 +01001701
1702 if (gpiochip->irqchip) {
1703 gpiochip->irqchip->irq_request_resources = NULL;
1704 gpiochip->irqchip->irq_release_resources = NULL;
1705 gpiochip->irqchip = NULL;
1706 }
Mika Westerberg79b804c2016-09-20 15:15:21 +03001707
1708 gpiochip_irqchip_free_valid_mask(gpiochip);
Linus Walleij14250522014-03-25 10:40:18 +01001709}
1710
1711/**
1712 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
1713 * @gpiochip: the gpiochip to add the irqchip to
1714 * @irqchip: the irqchip to add to the gpiochip
1715 * @first_irq: if not dynamically assigned, the base (first) IRQ to
1716 * allocate gpiochip irqs from
1717 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +02001718 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
1719 * to have the core avoid setting up any default type in the hardware.
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001720 * @lock_key: lockdep class
Linus Walleij14250522014-03-25 10:40:18 +01001721 *
1722 * This function closely associates a certain irqchip with a certain
1723 * gpiochip, providing an irq domain to translate the local IRQs to
1724 * global irqs in the gpiolib core, and making sure that the gpiochip
1725 * is passed as chip data to all related functions. Driver callbacks
Linus Walleij09dd5f92015-12-07 15:31:58 +01001726 * need to use gpiochip_get_data() to get their local state containers back
Linus Walleij14250522014-03-25 10:40:18 +01001727 * from the gpiochip passed as chip data. An irqdomain will be stored
1728 * in the gpiochip that shall be used by the driver to handle IRQ number
1729 * translation. The gpiochip will need to be initialized and registered
1730 * before calling this function.
1731 *
Linus Walleijc3626fd2014-03-28 20:42:01 +01001732 * This function will handle two cell:ed simple IRQs and assumes all
1733 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +01001734 * need to be open coded.
1735 */
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001736int _gpiochip_irqchip_add(struct gpio_chip *gpiochip,
1737 struct irq_chip *irqchip,
1738 unsigned int first_irq,
1739 irq_flow_handler_t handler,
1740 unsigned int type,
1741 struct lock_class_key *lock_key)
Linus Walleij14250522014-03-25 10:40:18 +01001742{
1743 struct device_node *of_node;
Mika Westerberg79b804c2016-09-20 15:15:21 +03001744 bool irq_base_set = false;
Linus Walleij14250522014-03-25 10:40:18 +01001745 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +01001746 unsigned irq_base = 0;
Linus Walleij14250522014-03-25 10:40:18 +01001747
1748 if (!gpiochip || !irqchip)
1749 return -EINVAL;
1750
Linus Walleij58383c72015-11-04 09:56:26 +01001751 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +01001752 pr_err("missing gpiochip .dev parent pointer\n");
1753 return -EINVAL;
1754 }
Linus Walleij58383c72015-11-04 09:56:26 +01001755 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +01001756#ifdef CONFIG_OF_GPIO
1757 /*
Colin Cronin20a8a962015-05-18 11:41:43 -07001758 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08001759 * FIXME: get rid of this and use gpiochip->parent->of_node
1760 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +01001761 */
1762 if (gpiochip->of_node)
1763 of_node = gpiochip->of_node;
1764#endif
Marc Zyngier332e99d2016-09-07 09:12:11 +01001765 /*
Mika Westerberg0a1e0052016-09-12 14:29:51 +03001766 * Specifying a default trigger is a terrible idea if DT or ACPI is
Marc Zyngier332e99d2016-09-07 09:12:11 +01001767 * used to configure the interrupts, as you may end-up with
1768 * conflicting triggers. Tell the user, and reset to NONE.
1769 */
1770 if (WARN(of_node && type != IRQ_TYPE_NONE,
1771 "%s: Ignoring %d default trigger\n", of_node->full_name, type))
1772 type = IRQ_TYPE_NONE;
Mika Westerberg0a1e0052016-09-12 14:29:51 +03001773 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
1774 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1775 "Ignoring %d default trigger\n", type);
1776 type = IRQ_TYPE_NONE;
1777 }
Marc Zyngier332e99d2016-09-07 09:12:11 +01001778
Linus Walleij14250522014-03-25 10:40:18 +01001779 gpiochip->irqchip = irqchip;
1780 gpiochip->irq_handler = handler;
1781 gpiochip->irq_default_type = type;
1782 gpiochip->to_irq = gpiochip_to_irq;
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001783 gpiochip->lock_key = lock_key;
Linus Walleij14250522014-03-25 10:40:18 +01001784 gpiochip->irqdomain = irq_domain_add_simple(of_node,
1785 gpiochip->ngpio, first_irq,
1786 &gpiochip_domain_ops, gpiochip);
1787 if (!gpiochip->irqdomain) {
1788 gpiochip->irqchip = NULL;
1789 return -EINVAL;
1790 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +02001791
1792 /*
1793 * It is possible for a driver to override this, but only if the
1794 * alternative functions are both implemented.
1795 */
1796 if (!irqchip->irq_request_resources &&
1797 !irqchip->irq_release_resources) {
1798 irqchip->irq_request_resources = gpiochip_irq_reqres;
1799 irqchip->irq_release_resources = gpiochip_irq_relres;
1800 }
Linus Walleij14250522014-03-25 10:40:18 +01001801
1802 /*
1803 * Prepare the mapping since the irqchip shall be orthogonal to
1804 * any gpiochip calls. If the first_irq was zero, this is
1805 * necessary to allocate descriptors for all IRQs.
1806 */
Linus Walleijc3626fd2014-03-28 20:42:01 +01001807 for (offset = 0; offset < gpiochip->ngpio; offset++) {
Mika Westerberg79b804c2016-09-20 15:15:21 +03001808 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1809 continue;
Linus Walleijc3626fd2014-03-28 20:42:01 +01001810 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001811 if (!irq_base_set) {
Linus Walleijc3626fd2014-03-28 20:42:01 +01001812 /*
1813 * Store the base into the gpiochip to be used when
1814 * unmapping the irqs.
1815 */
1816 gpiochip->irq_base = irq_base;
Mika Westerberg79b804c2016-09-20 15:15:21 +03001817 irq_base_set = true;
1818 }
Linus Walleijc3626fd2014-03-28 20:42:01 +01001819 }
Linus Walleij14250522014-03-25 10:40:18 +01001820
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001821 acpi_gpiochip_request_interrupts(gpiochip);
1822
Linus Walleij14250522014-03-25 10:40:18 +01001823 return 0;
1824}
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001825EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add);
Linus Walleij14250522014-03-25 10:40:18 +01001826
1827#else /* CONFIG_GPIOLIB_IRQCHIP */
1828
1829static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
Mika Westerberg79b804c2016-09-20 15:15:21 +03001830static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1831{
1832 return 0;
1833}
1834static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1835{ }
Linus Walleij14250522014-03-25 10:40:18 +01001836
1837#endif /* CONFIG_GPIOLIB_IRQCHIP */
1838
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001839/**
1840 * gpiochip_generic_request() - request the gpio function for a pin
1841 * @chip: the gpiochip owning the GPIO
1842 * @offset: the offset of the GPIO to request for GPIO function
1843 */
1844int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
1845{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001846 return pinctrl_request_gpio(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001847}
1848EXPORT_SYMBOL_GPL(gpiochip_generic_request);
1849
1850/**
1851 * gpiochip_generic_free() - free the gpio function from a pin
1852 * @chip: the gpiochip to request the gpio function for
1853 * @offset: the offset of the GPIO to free from GPIO function
1854 */
1855void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
1856{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001857 pinctrl_free_gpio(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001858}
1859EXPORT_SYMBOL_GPL(gpiochip_generic_free);
1860
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301861#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01001862
Linus Walleij3f0f8672012-11-20 12:40:15 +01001863/**
Christian Ruppert586a87e2013-10-15 15:37:54 +02001864 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1865 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +02001866 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +02001867 * @gpio_offset: the start offset in the current gpio_chip number space
1868 * @pin_group: name of the pin group inside the pin controller
1869 */
1870int gpiochip_add_pingroup_range(struct gpio_chip *chip,
1871 struct pinctrl_dev *pctldev,
1872 unsigned int gpio_offset, const char *pin_group)
1873{
1874 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001875 struct gpio_device *gdev = chip->gpiodev;
Christian Ruppert586a87e2013-10-15 15:37:54 +02001876 int ret;
1877
1878 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1879 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001880 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +02001881 return -ENOMEM;
1882 }
1883
1884 /* Use local offset as range ID */
1885 pin_range->range.id = gpio_offset;
1886 pin_range->range.gc = chip;
1887 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001888 pin_range->range.base = gdev->base + gpio_offset;
Christian Ruppert586a87e2013-10-15 15:37:54 +02001889 pin_range->pctldev = pctldev;
1890
1891 ret = pinctrl_get_group_pins(pctldev, pin_group,
1892 &pin_range->range.pins,
1893 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001894 if (ret < 0) {
1895 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001896 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001897 }
Christian Ruppert586a87e2013-10-15 15:37:54 +02001898
1899 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1900
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001901 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1902 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +02001903 pinctrl_dev_get_devname(pctldev), pin_group);
1904
Linus Walleij20ec3e32016-02-11 11:03:06 +01001905 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001906
1907 return 0;
1908}
1909EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1910
1911/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01001912 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1913 * @chip: the gpiochip to add the range for
1914 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01001915 * @gpio_offset: the start offset in the current gpio_chip number space
1916 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01001917 * @npins: the number of pins from the offset of each pin space (GPIO and
1918 * pin controller) to accumulate in this range
1919 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001920int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01001921 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01001922 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301923{
1924 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001925 struct gpio_device *gdev = chip->gpiodev;
Axel Linb4d4b1f2012-11-21 14:33:56 +08001926 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301927
Linus Walleij3f0f8672012-11-20 12:40:15 +01001928 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301929 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001930 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001931 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301932 }
1933
Linus Walleij3f0f8672012-11-20 12:40:15 +01001934 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01001935 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001936 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301937 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001938 pin_range->range.base = gdev->base + gpio_offset;
Linus Walleij316511c2012-11-21 08:48:09 +01001939 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301940 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01001941 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301942 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01001943 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08001944 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001945 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +01001946 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08001947 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001948 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001949 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1950 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +01001951 pinctl_name,
1952 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301953
Linus Walleij20ec3e32016-02-11 11:03:06 +01001954 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001955
1956 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301957}
Linus Walleij165adc92012-11-06 14:49:39 +01001958EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301959
Linus Walleij3f0f8672012-11-20 12:40:15 +01001960/**
1961 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1962 * @chip: the chip to remove all the mappings for
1963 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301964void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1965{
1966 struct gpio_pin_range *pin_range, *tmp;
Linus Walleij20ec3e32016-02-11 11:03:06 +01001967 struct gpio_device *gdev = chip->gpiodev;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301968
Linus Walleij20ec3e32016-02-11 11:03:06 +01001969 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301970 list_del(&pin_range->node);
1971 pinctrl_remove_gpio_range(pin_range->pctldev,
1972 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001973 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301974 }
1975}
Linus Walleij165adc92012-11-06 14:49:39 +01001976EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1977
1978#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301979
David Brownelld2876d02008-02-04 22:28:20 -08001980/* These "optional" allocation calls help prevent drivers from stomping
1981 * on each other, and help provide better diagnostics in debugfs.
1982 * They're called even less than the "set direction" calls.
1983 */
Mika Westerberg77c2d792014-03-10 14:54:50 +02001984static int __gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08001985{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001986 struct gpio_chip *chip = desc->gdev->chip;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001987 int status;
David Brownelld2876d02008-02-04 22:28:20 -08001988 unsigned long flags;
1989
1990 spin_lock_irqsave(&gpio_lock, flags);
1991
David Brownelld2876d02008-02-04 22:28:20 -08001992 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07001993 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001994 */
1995
1996 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1997 desc_set_label(desc, label ? : "?");
1998 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001999 } else {
David Brownelld2876d02008-02-04 22:28:20 -08002000 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -08002001 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07002002 }
2003
2004 if (chip->request) {
2005 /* chip->request may sleep */
2006 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002007 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07002008 spin_lock_irqsave(&gpio_lock, flags);
2009
2010 if (status < 0) {
2011 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -07002012 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03002013 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07002014 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07002015 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03002016 if (chip->get_direction) {
2017 /* chip->get_direction may sleep */
2018 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002019 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03002020 spin_lock_irqsave(&gpio_lock, flags);
2021 }
David Brownelld2876d02008-02-04 22:28:20 -08002022done:
Mika Westerberg77c2d792014-03-10 14:54:50 +02002023 spin_unlock_irqrestore(&gpio_lock, flags);
2024 return status;
2025}
2026
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002027/*
2028 * This descriptor validation needs to be inserted verbatim into each
2029 * function taking a descriptor, so we need to use a preprocessor
Linus Walleij54d77192016-05-30 16:48:39 +02002030 * macro to avoid endless duplication. If the desc is NULL it is an
2031 * optional GPIO and calls should just bail out.
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002032 */
2033#define VALIDATE_DESC(desc) do { \
Linus Walleij54d77192016-05-30 16:48:39 +02002034 if (!desc) \
2035 return 0; \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002036 if (IS_ERR(desc)) { \
2037 pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
2038 return PTR_ERR(desc); \
2039 } \
Linus Walleij54d77192016-05-30 16:48:39 +02002040 if (!desc->gdev) { \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002041 pr_warn("%s: invalid GPIO (no device)\n", __func__); \
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002042 return -EINVAL; \
2043 } \
2044 if ( !desc->gdev->chip ) { \
2045 dev_warn(&desc->gdev->dev, \
2046 "%s: backing chip is gone\n", __func__); \
2047 return 0; \
2048 } } while (0)
2049
2050#define VALIDATE_DESC_VOID(desc) do { \
Linus Walleij54d77192016-05-30 16:48:39 +02002051 if (!desc) \
2052 return; \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002053 if (IS_ERR(desc)) { \
2054 pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
2055 return; \
2056 } \
Linus Walleij54d77192016-05-30 16:48:39 +02002057 if (!desc->gdev) { \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002058 pr_warn("%s: invalid GPIO (no device)\n", __func__); \
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002059 return; \
2060 } \
2061 if (!desc->gdev->chip) { \
2062 dev_warn(&desc->gdev->dev, \
2063 "%s: backing chip is gone\n", __func__); \
2064 return; \
2065 } } while (0)
2066
2067
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002068int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002069{
2070 int status = -EPROBE_DEFER;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002071 struct gpio_device *gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002072
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002073 VALIDATE_DESC(desc);
2074 gdev = desc->gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002075
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002076 if (try_module_get(gdev->owner)) {
Mika Westerberg77c2d792014-03-10 14:54:50 +02002077 status = __gpiod_request(desc, label);
2078 if (status < 0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002079 module_put(gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002080 else
2081 get_device(&gdev->dev);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002082 }
2083
David Brownelld2876d02008-02-04 22:28:20 -08002084 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +02002085 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002086
David Brownelld2876d02008-02-04 22:28:20 -08002087 return status;
2088}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002089
Mika Westerberg77c2d792014-03-10 14:54:50 +02002090static bool __gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002091{
Mika Westerberg77c2d792014-03-10 14:54:50 +02002092 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08002093 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07002094 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002095
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07002096 might_sleep();
2097
Alexandre Courbot372e7222013-02-03 01:29:29 +09002098 gpiod_unexport(desc);
David Brownelld8f388d2008-07-25 01:46:07 -07002099
David Brownelld2876d02008-02-04 22:28:20 -08002100 spin_lock_irqsave(&gpio_lock, flags);
2101
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002102 chip = desc->gdev->chip;
David Brownell35e8bb52008-10-15 22:03:16 -07002103 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
2104 if (chip->free) {
2105 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07002106 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002107 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07002108 spin_lock_irqsave(&gpio_lock, flags);
2109 }
David Brownelld2876d02008-02-04 22:28:20 -08002110 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08002111 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07002112 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302113 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302114 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06002115 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002116 ret = true;
2117 }
David Brownelld2876d02008-02-04 22:28:20 -08002118
2119 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002120 return ret;
2121}
2122
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002123void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002124{
Linus Walleij33a68e82016-02-11 10:28:44 +01002125 if (desc && desc->gdev && __gpiod_free(desc)) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002126 module_put(desc->gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002127 put_device(&desc->gdev->dev);
2128 } else {
Mika Westerberg77c2d792014-03-10 14:54:50 +02002129 WARN_ON(extra_checks);
Linus Walleij33a68e82016-02-11 10:28:44 +01002130 }
David Brownelld2876d02008-02-04 22:28:20 -08002131}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002132
David Brownelld2876d02008-02-04 22:28:20 -08002133/**
2134 * gpiochip_is_requested - return string iff signal was requested
2135 * @chip: controller managing the signal
2136 * @offset: of signal within controller's 0..(ngpio - 1) range
2137 *
2138 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09002139 * The string returned is the label passed to gpio_request(); if none has been
2140 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08002141 *
2142 * This function is for use by GPIO controller drivers. The label can
2143 * help with diagnostics, and knowing that the signal is used as a GPIO
2144 * can help avoid accidentally multiplexing it to another controller.
2145 */
2146const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
2147{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002148 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08002149
Dirk Behme48b59532015-08-18 18:02:32 +02002150 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08002151 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002152
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002153 desc = &chip->gpiodev->descs[offset];
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002154
Alexandre Courbot372e7222013-02-03 01:29:29 +09002155 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08002156 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002157 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08002158}
2159EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2160
Mika Westerberg77c2d792014-03-10 14:54:50 +02002161/**
2162 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
2163 * @desc: GPIO descriptor to request
2164 * @label: label for the GPIO
2165 *
2166 * Function allows GPIO chip drivers to request and use their own GPIO
2167 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2168 * function will not increase reference count of the GPIO chip module. This
2169 * allows the GPIO chip module to be unloaded as needed (we assume that the
2170 * GPIO chip driver handles freeing the GPIOs it has requested).
2171 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002172struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
2173 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002174{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002175 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
2176 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002177
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002178 if (IS_ERR(desc)) {
2179 chip_err(chip, "failed to get GPIO descriptor\n");
2180 return desc;
2181 }
2182
2183 err = __gpiod_request(desc, label);
2184 if (err < 0)
2185 return ERR_PTR(err);
2186
2187 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002188}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002189EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002190
2191/**
2192 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2193 * @desc: GPIO descriptor to free
2194 *
2195 * Function frees the given GPIO requested previously with
2196 * gpiochip_request_own_desc().
2197 */
2198void gpiochip_free_own_desc(struct gpio_desc *desc)
2199{
2200 if (desc)
2201 __gpiod_free(desc);
2202}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002203EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08002204
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002205/*
2206 * Drivers MUST set GPIO direction before making get/set calls. In
David Brownelld2876d02008-02-04 22:28:20 -08002207 * some cases this is done in early boot, before IRQs are enabled.
2208 *
2209 * As a rule these aren't called more than once (except for drivers
2210 * using the open-drain emulation idiom) so these are natural places
2211 * to accumulate extra debugging checks. Note that we can't (yet)
2212 * rely on gpio_request() having been called beforehand.
2213 */
2214
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002215/**
2216 * gpiod_direction_input - set the GPIO direction to input
2217 * @desc: GPIO to set to input
2218 *
2219 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2220 * be called safely on it.
2221 *
2222 * Return 0 in case of success, else an error code.
2223 */
2224int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002225{
David Brownelld2876d02008-02-04 22:28:20 -08002226 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002227 int status = -EINVAL;
2228
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002229 VALIDATE_DESC(desc);
2230 chip = desc->gdev->chip;
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002231
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002232 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01002233 gpiod_warn(desc,
2234 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02002235 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002236 return -EIO;
2237 }
2238
Alexandre Courbotd82da792014-07-22 16:17:43 +09002239 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -08002240 if (status == 0)
2241 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002242
Alexandre Courbot372e7222013-02-03 01:29:29 +09002243 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09002244
David Brownelld2876d02008-02-04 22:28:20 -08002245 return status;
2246}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002247EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002248
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002249static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08002250{
Linus Walleijc663e5f2016-03-22 10:51:16 +01002251 struct gpio_chip *gc = desc->gdev->chip;
2252 int ret;
David Brownelld2876d02008-02-04 22:28:20 -08002253
Linus Walleijd468bf92013-09-24 11:54:38 +02002254 /* GPIOs used for IRQs shall not be set as output */
2255 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
2256 gpiod_err(desc,
2257 "%s: tried to set a GPIO tied to an IRQ as output\n",
2258 __func__);
2259 return -EIO;
2260 }
2261
Linus Walleijc663e5f2016-03-22 10:51:16 +01002262 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2263 /* First see if we can enable open drain in hardware */
2264 if (gc->set_single_ended) {
2265 ret = gc->set_single_ended(gc, gpio_chip_hwgpio(desc),
2266 LINE_MODE_OPEN_DRAIN);
2267 if (!ret)
2268 goto set_output_value;
2269 }
2270 /* Emulate open drain by not actively driving the line high */
2271 if (value)
2272 return gpiod_direction_input(desc);
2273 }
2274 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2275 if (gc->set_single_ended) {
2276 ret = gc->set_single_ended(gc, gpio_chip_hwgpio(desc),
2277 LINE_MODE_OPEN_SOURCE);
2278 if (!ret)
2279 goto set_output_value;
2280 }
2281 /* Emulate open source by not actively driving the line low */
2282 if (!value)
2283 return gpiod_direction_input(desc);
2284 } else {
2285 /* Make sure to disable open drain/source hardware, if any */
2286 if (gc->set_single_ended)
2287 gc->set_single_ended(gc,
2288 gpio_chip_hwgpio(desc),
2289 LINE_MODE_PUSH_PULL);
2290 }
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302291
Linus Walleijc663e5f2016-03-22 10:51:16 +01002292set_output_value:
2293 if (!gc->set || !gc->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01002294 gpiod_warn(desc,
2295 "%s: missing set() or direction_output() operations\n",
2296 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002297 return -EIO;
2298 }
2299
Linus Walleijc663e5f2016-03-22 10:51:16 +01002300 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), value);
2301 if (!ret)
David Brownelld2876d02008-02-04 22:28:20 -08002302 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002303 trace_gpio_value(desc_to_gpio(desc), 0, value);
Linus Walleijc663e5f2016-03-22 10:51:16 +01002304 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2305 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08002306}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002307
2308/**
2309 * gpiod_direction_output_raw - set the GPIO direction to output
2310 * @desc: GPIO to set to output
2311 * @value: initial output value of the GPIO
2312 *
2313 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2314 * be called safely on it. The initial value of the output must be specified
2315 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2316 *
2317 * Return 0 in case of success, else an error code.
2318 */
2319int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2320{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002321 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002322 return _gpiod_direction_output_raw(desc, value);
2323}
2324EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2325
2326/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05302327 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002328 * @desc: GPIO to set to output
2329 * @value: initial output value of the GPIO
2330 *
2331 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2332 * be called safely on it. The initial value of the output must be specified
2333 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2334 * account.
2335 *
2336 * Return 0 in case of success, else an error code.
2337 */
2338int gpiod_direction_output(struct gpio_desc *desc, int value)
2339{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002340 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002341 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2342 value = !value;
2343 return _gpiod_direction_output_raw(desc, value);
2344}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002345EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08002346
Felipe Balbic4b5be92010-05-26 14:42:23 -07002347/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002348 * gpiod_set_debounce - sets @debounce time for a @gpio
Felipe Balbic4b5be92010-05-26 14:42:23 -07002349 * @gpio: the gpio to set debounce time
2350 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02002351 *
2352 * returns -ENOTSUPP if the controller does not support setting
2353 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07002354 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002355int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07002356{
Felipe Balbic4b5be92010-05-26 14:42:23 -07002357 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07002358
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002359 VALIDATE_DESC(desc);
2360 chip = desc->gdev->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002361 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01002362 gpiod_dbg(desc,
2363 "%s: missing set() or set_debounce() operations\n",
2364 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02002365 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002366 }
2367
Alexandre Courbotd82da792014-07-22 16:17:43 +09002368 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07002369}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002370EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002371
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002372/**
2373 * gpiod_is_active_low - test whether a GPIO is active-low or not
2374 * @desc: the gpio descriptor to test
2375 *
2376 * Returns 1 if the GPIO is active-low, 0 otherwise.
2377 */
2378int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002379{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002380 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002381 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002382}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002383EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08002384
2385/* I/O calls are only valid after configuration completed; the relevant
2386 * "is this a valid GPIO" error checks should already have been done.
2387 *
2388 * "Get" operations are often inlinable as reading a pin value register,
2389 * and masking the relevant bit in that register.
2390 *
2391 * When "set" operations are inlinable, they involve writing that mask to
2392 * one register to set a low value, or a different register to set it high.
2393 * Otherwise locking is needed, so there may be little value to inlining.
2394 *
2395 *------------------------------------------------------------------------
2396 *
2397 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2398 * have requested the GPIO. That can include implicit requesting by
2399 * a direction setting call. Marking a gpio as requested locks its chip
2400 * in memory, guaranteeing that these table lookups need no more locking
2401 * and that gpiochip_remove() will fail.
2402 *
2403 * REVISIT when debugging, consider adding some instrumentation to ensure
2404 * that the GPIO was actually requested.
2405 */
2406
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002407static int _gpiod_get_raw_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002408{
2409 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002410 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002411 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002412
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002413 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002414 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002415 value = chip->get ? chip->get(chip, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01002416 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002417 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002418 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002419}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002420
David Brownelld2876d02008-02-04 22:28:20 -08002421/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002422 * gpiod_get_raw_value() - return a gpio's raw value
2423 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08002424 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002425 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002426 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002427 *
2428 * This function should be called from contexts where we cannot sleep, and will
2429 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002430 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002431int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002432{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002433 VALIDATE_DESC(desc);
Geert Uytterhoevend66de412019-07-01 16:27:38 +02002434 /* Should be using gpiod_get_raw_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002435 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002436 return _gpiod_get_raw_value(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002437}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002438EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002439
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002440/**
2441 * gpiod_get_value() - return a gpio's value
2442 * @desc: gpio whose value will be returned
2443 *
2444 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002445 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002446 *
2447 * This function should be called from contexts where we cannot sleep, and will
2448 * complain if the GPIO chip functions potentially sleep.
2449 */
2450int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002451{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002452 int value;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002453
2454 VALIDATE_DESC(desc);
Geert Uytterhoevend66de412019-07-01 16:27:38 +02002455 /* Should be using gpiod_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002456 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002457
2458 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002459 if (value < 0)
2460 return value;
2461
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002462 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2463 value = !value;
2464
2465 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002466}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002467EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08002468
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302469/*
2470 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002471 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002472 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302473 */
Alexandre Courbot23600962014-03-11 15:52:09 +09002474static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302475{
2476 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002477 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002478 int offset = gpio_chip_hwgpio(desc);
2479
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302480 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002481 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302482 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002483 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302484 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002485 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302486 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002487 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302488 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002489 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302490 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002491 gpiod_err(desc,
2492 "%s: Error in set_value for open drain err %d\n",
2493 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302494}
2495
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302496/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002497 * _gpio_set_open_source_value() - Set the open source gpio's value.
2498 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002499 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302500 */
Alexandre Courbot23600962014-03-11 15:52:09 +09002501static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302502{
2503 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002504 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002505 int offset = gpio_chip_hwgpio(desc);
2506
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302507 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002508 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302509 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002510 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302511 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002512 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302513 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002514 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302515 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002516 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302517 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002518 gpiod_err(desc,
2519 "%s: Error in set_value for open source err %d\n",
2520 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302521}
2522
Alexandre Courbot23600962014-03-11 15:52:09 +09002523static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08002524{
2525 struct gpio_chip *chip;
2526
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002527 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002528 trace_gpio_value(desc_to_gpio(desc), 0, value);
2529 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2530 _gpio_set_open_drain_value(desc, value);
2531 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2532 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302533 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09002534 chip->set(chip, gpio_chip_hwgpio(desc), value);
2535}
2536
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002537/*
2538 * set multiple outputs on the same chip;
2539 * use the chip's set_multiple function if available;
2540 * otherwise set the outputs sequentially;
2541 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
2542 * defines which outputs are to be changed
2543 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
2544 * defines the values the outputs specified by mask are to be set to
2545 */
2546static void gpio_chip_set_multiple(struct gpio_chip *chip,
2547 unsigned long *mask, unsigned long *bits)
2548{
2549 if (chip->set_multiple) {
2550 chip->set_multiple(chip, mask, bits);
2551 } else {
2552 int i;
2553 for (i = 0; i < chip->ngpio; i++) {
2554 if (mask[BIT_WORD(i)] == 0) {
2555 /* no more set bits in this mask word;
2556 * skip ahead to the next word */
2557 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
2558 continue;
2559 }
2560 /* set outputs if the corresponding mask bit is set */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002561 if (__test_and_clear_bit(i, mask))
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002562 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002563 }
2564 }
2565}
2566
Linus Walleij44c7288f2016-04-24 11:36:59 +02002567void gpiod_set_array_value_complex(bool raw, bool can_sleep,
2568 unsigned int array_size,
2569 struct gpio_desc **desc_array,
2570 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002571{
2572 int i = 0;
2573
2574 while (i < array_size) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002575 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002576 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2577 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2578 int count = 0;
2579
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002580 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002581 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002582
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002583 memset(mask, 0, sizeof(mask));
2584 do {
2585 struct gpio_desc *desc = desc_array[i];
2586 int hwgpio = gpio_chip_hwgpio(desc);
2587 int value = value_array[i];
2588
2589 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2590 value = !value;
2591 trace_gpio_value(desc_to_gpio(desc), 0, value);
2592 /*
2593 * collect all normal outputs belonging to the same chip
2594 * open drain and open source outputs are set individually
2595 */
2596 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002597 _gpio_set_open_drain_value(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002598 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2599 _gpio_set_open_source_value(desc, value);
2600 } else {
2601 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002602 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002603 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002604 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002605 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002606 count++;
2607 }
2608 i++;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002609 } while ((i < array_size) &&
2610 (desc_array[i]->gdev->chip == chip));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002611 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002612 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002613 gpio_chip_set_multiple(chip, mask, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002614 }
2615}
2616
David Brownelld2876d02008-02-04 22:28:20 -08002617/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002618 * gpiod_set_raw_value() - assign a gpio's raw value
2619 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08002620 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002621 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002622 * Set the raw value of the GPIO, i.e. the value of its physical line without
2623 * regard for its ACTIVE_LOW status.
2624 *
2625 * This function should be called from contexts where we cannot sleep, and will
2626 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002627 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002628void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002629{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002630 VALIDATE_DESC_VOID(desc);
Geert Uytterhoevend66de412019-07-01 16:27:38 +02002631 /* Should be using gpiod_set_raw_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002632 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002633 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002634}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002635EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002636
2637/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002638 * gpiod_set_value() - assign a gpio's value
2639 * @desc: gpio whose value will be assigned
2640 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002641 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002642 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2643 * account
2644 *
2645 * This function should be called from contexts where we cannot sleep, and will
2646 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002647 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002648void gpiod_set_value(struct gpio_desc *desc, int value)
2649{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002650 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01002651 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002652 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002653 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2654 value = !value;
2655 _gpiod_set_raw_value(desc, value);
2656}
2657EXPORT_SYMBOL_GPL(gpiod_set_value);
2658
2659/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002660 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002661 * @array_size: number of elements in the descriptor / value arrays
2662 * @desc_array: array of GPIO descriptors whose values will be assigned
2663 * @value_array: array of values to assign
2664 *
2665 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2666 * without regard for their ACTIVE_LOW status.
2667 *
2668 * This function should be called from contexts where we cannot sleep, and will
2669 * complain if the GPIO chip functions potentially sleep.
2670 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002671void gpiod_set_raw_array_value(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002672 struct gpio_desc **desc_array, int *value_array)
2673{
2674 if (!desc_array)
2675 return;
Linus Walleij44c7288f2016-04-24 11:36:59 +02002676 gpiod_set_array_value_complex(true, false, array_size, desc_array,
2677 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002678}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002679EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002680
2681/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002682 * gpiod_set_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002683 * @array_size: number of elements in the descriptor / value arrays
2684 * @desc_array: array of GPIO descriptors whose values will be assigned
2685 * @value_array: array of values to assign
2686 *
2687 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2688 * into account.
2689 *
2690 * This function should be called from contexts where we cannot sleep, and will
2691 * complain if the GPIO chip functions potentially sleep.
2692 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002693void gpiod_set_array_value(unsigned int array_size,
2694 struct gpio_desc **desc_array, int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002695{
2696 if (!desc_array)
2697 return;
Linus Walleij44c7288f2016-04-24 11:36:59 +02002698 gpiod_set_array_value_complex(false, false, array_size, desc_array,
2699 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002700}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002701EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002702
2703/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002704 * gpiod_cansleep() - report whether gpio value access may sleep
2705 * @desc: gpio to check
2706 *
2707 */
2708int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002709{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002710 VALIDATE_DESC(desc);
2711 return desc->gdev->chip->can_sleep;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002712}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002713EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002714
David Brownell0f6d5042008-10-15 22:03:14 -07002715/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002716 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
2717 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07002718 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002719 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
2720 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07002721 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002722int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07002723{
Linus Walleij4c37ce82016-05-02 13:13:10 +02002724 struct gpio_chip *chip;
2725 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07002726
Linus Walleij79bb71b2016-06-15 22:57:38 +02002727 /*
2728 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
2729 * requires this function to not return zero on an invalid descriptor
2730 * but rather a negative error number.
2731 */
Linus Walleijbfbbe442016-06-16 11:55:55 +02002732 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
Linus Walleij79bb71b2016-06-15 22:57:38 +02002733 return -EINVAL;
2734
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002735 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002736 offset = gpio_chip_hwgpio(desc);
Linus Walleij4c37ce82016-05-02 13:13:10 +02002737 if (chip->to_irq) {
2738 int retirq = chip->to_irq(chip, offset);
2739
2740 /* Zero means NO_IRQ */
2741 if (!retirq)
2742 return -ENXIO;
2743
2744 return retirq;
2745 }
2746 return -ENXIO;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002747}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002748EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002749
Linus Walleijd468bf92013-09-24 11:54:38 +02002750/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002751 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002752 * @chip: the chip the GPIO to lock belongs to
2753 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02002754 *
2755 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08002756 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08002757 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002758int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08002759{
Linus Walleij9c102802016-05-25 10:56:03 +02002760 struct gpio_desc *desc;
Linus Walleijd468bf92013-09-24 11:54:38 +02002761
Linus Walleij9c102802016-05-25 10:56:03 +02002762 desc = gpiochip_get_desc(chip, offset);
2763 if (IS_ERR(desc))
2764 return PTR_ERR(desc);
2765
Linus Walleij60f83392016-11-12 15:01:09 +01002766 /*
2767 * If it's fast: flush the direction setting if something changed
2768 * behind our back
2769 */
2770 if (!chip->can_sleep && chip->get_direction) {
Linus Walleij9c102802016-05-25 10:56:03 +02002771 int dir = chip->get_direction(chip, offset);
2772
2773 if (dir)
2774 clear_bit(FLAG_IS_OUT, &desc->flags);
2775 else
2776 set_bit(FLAG_IS_OUT, &desc->flags);
2777 }
2778
2779 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002780 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02002781 "%s: tried to flag a GPIO set as output for IRQ\n",
2782 __func__);
2783 return -EIO;
2784 }
2785
Linus Walleij9c102802016-05-25 10:56:03 +02002786 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002787 return 0;
2788}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002789EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02002790
Linus Walleijd468bf92013-09-24 11:54:38 +02002791/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002792 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002793 * @chip: the chip the GPIO to lock belongs to
2794 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02002795 *
2796 * This is used directly by GPIO drivers that want to indicate
2797 * that a certain GPIO is no longer used exclusively for IRQ.
2798 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002799void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02002800{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002801 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02002802 return;
2803
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002804 clear_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002805}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002806EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02002807
Linus Walleij6cee3822016-02-11 20:16:45 +01002808bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
2809{
2810 if (offset >= chip->ngpio)
2811 return false;
2812
2813 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
2814}
2815EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
2816
Linus Walleij143b65d2016-02-16 15:41:42 +01002817bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
2818{
2819 if (offset >= chip->ngpio)
2820 return false;
2821
2822 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
2823}
2824EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
2825
2826bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
2827{
2828 if (offset >= chip->ngpio)
2829 return false;
2830
2831 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
2832}
2833EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
2834
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002835/**
2836 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
2837 * @desc: gpio whose value will be returned
2838 *
2839 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002840 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002841 *
2842 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002843 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002844int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002845{
David Brownelld2876d02008-02-04 22:28:20 -08002846 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002847 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002848 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002849}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002850EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002851
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002852/**
2853 * gpiod_get_value_cansleep() - return a gpio's value
2854 * @desc: gpio whose value will be returned
2855 *
2856 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002857 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002858 *
2859 * This function is to be called from contexts that can sleep.
2860 */
2861int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002862{
David Brownelld2876d02008-02-04 22:28:20 -08002863 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002864
2865 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002866 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002867 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002868 if (value < 0)
2869 return value;
2870
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002871 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2872 value = !value;
2873
David Brownelld2876d02008-02-04 22:28:20 -08002874 return value;
2875}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002876EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002877
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002878/**
2879 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
2880 * @desc: gpio whose value will be assigned
2881 * @value: value to assign
2882 *
2883 * Set the raw value of the GPIO, i.e. the value of its physical line without
2884 * regard for its ACTIVE_LOW status.
2885 *
2886 * This function is to be called from contexts that can sleep.
2887 */
2888void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002889{
David Brownelld2876d02008-02-04 22:28:20 -08002890 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002891 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002892 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002893}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002894EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002895
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002896/**
2897 * gpiod_set_value_cansleep() - assign a gpio's value
2898 * @desc: gpio whose value will be assigned
2899 * @value: value to assign
2900 *
2901 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2902 * account
2903 *
2904 * This function is to be called from contexts that can sleep.
2905 */
2906void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002907{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002908 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002909 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002910 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2911 value = !value;
2912 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002913}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002914EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002915
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002916/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002917 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002918 * @array_size: number of elements in the descriptor / value arrays
2919 * @desc_array: array of GPIO descriptors whose values will be assigned
2920 * @value_array: array of values to assign
2921 *
2922 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2923 * without regard for their ACTIVE_LOW status.
2924 *
2925 * This function is to be called from contexts that can sleep.
2926 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002927void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
2928 struct gpio_desc **desc_array,
2929 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002930{
2931 might_sleep_if(extra_checks);
2932 if (!desc_array)
2933 return;
Linus Walleij44c7288f2016-04-24 11:36:59 +02002934 gpiod_set_array_value_complex(true, true, array_size, desc_array,
2935 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002936}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002937EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002938
2939/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002940 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002941 * @array_size: number of elements in the descriptor / value arrays
2942 * @desc_array: array of GPIO descriptors whose values will be assigned
2943 * @value_array: array of values to assign
2944 *
2945 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2946 * into account.
2947 *
2948 * This function is to be called from contexts that can sleep.
2949 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002950void gpiod_set_array_value_cansleep(unsigned int array_size,
2951 struct gpio_desc **desc_array,
2952 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002953{
2954 might_sleep_if(extra_checks);
2955 if (!desc_array)
2956 return;
Linus Walleij44c7288f2016-04-24 11:36:59 +02002957 gpiod_set_array_value_complex(false, true, array_size, desc_array,
2958 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002959}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002960EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002961
2962/**
Alexandre Courbotad824782013-12-03 12:20:11 +09002963 * gpiod_add_lookup_table() - register GPIO device consumers
2964 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002965 */
Alexandre Courbotad824782013-12-03 12:20:11 +09002966void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002967{
2968 mutex_lock(&gpio_lookup_lock);
2969
Alexandre Courbotad824782013-12-03 12:20:11 +09002970 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002971
2972 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08002973}
2974
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05302975/**
2976 * gpiod_remove_lookup_table() - unregister GPIO device consumers
2977 * @table: table of consumers to unregister
2978 */
2979void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
2980{
2981 mutex_lock(&gpio_lookup_lock);
2982
2983 list_del(&table->list);
2984
2985 mutex_unlock(&gpio_lookup_lock);
2986}
2987
Alexandre Courbotad824782013-12-03 12:20:11 +09002988static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
2989{
2990 const char *dev_id = dev ? dev_name(dev) : NULL;
2991 struct gpiod_lookup_table *table;
2992
2993 mutex_lock(&gpio_lookup_lock);
2994
2995 list_for_each_entry(table, &gpio_lookup_list, list) {
2996 if (table->dev_id && dev_id) {
2997 /*
2998 * Valid strings on both ends, must be identical to have
2999 * a match
3000 */
3001 if (!strcmp(table->dev_id, dev_id))
3002 goto found;
3003 } else {
3004 /*
3005 * One of the pointers is NULL, so both must be to have
3006 * a match
3007 */
3008 if (dev_id == table->dev_id)
3009 goto found;
3010 }
3011 }
3012 table = NULL;
3013
3014found:
3015 mutex_unlock(&gpio_lookup_lock);
3016 return table;
3017}
3018
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003019static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09003020 unsigned int idx,
3021 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003022{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003023 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09003024 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003025 struct gpiod_lookup *p;
3026
Alexandre Courbotad824782013-12-03 12:20:11 +09003027 table = gpiod_find_lookup_table(dev);
3028 if (!table)
3029 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003030
Alexandre Courbotad824782013-12-03 12:20:11 +09003031 for (p = &table->table[0]; p->chip_label; p++) {
3032 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003033
Alexandre Courbotad824782013-12-03 12:20:11 +09003034 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003035 if (p->idx != idx)
3036 continue;
3037
Alexandre Courbotad824782013-12-03 12:20:11 +09003038 /* If the lookup entry has a con_id, require exact match */
3039 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
3040 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003041
Alexandre Courbotad824782013-12-03 12:20:11 +09003042 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003043
Alexandre Courbotad824782013-12-03 12:20:11 +09003044 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003045 dev_err(dev, "cannot find GPIO chip %s\n",
3046 p->chip_label);
3047 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003048 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003049
Alexandre Courbotad824782013-12-03 12:20:11 +09003050 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003051 dev_err(dev,
Geert Uytterhoeven699c96b2019-11-27 10:59:19 +01003052 "requested GPIO %u (%u) is out of range [0..%u] for chip %s\n",
3053 idx, p->chip_hwnum, chip->ngpio - 1,
3054 chip->label);
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003055 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09003056 }
3057
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09003058 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09003059 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003060
3061 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09003062 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003063
3064 return desc;
3065}
3066
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003067static int dt_gpio_count(struct device *dev, const char *con_id)
3068{
3069 int ret;
3070 char propname[32];
3071 unsigned int i;
3072
3073 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
3074 if (con_id)
3075 snprintf(propname, sizeof(propname), "%s-%s",
3076 con_id, gpio_suffixes[i]);
3077 else
3078 snprintf(propname, sizeof(propname), "%s",
3079 gpio_suffixes[i]);
3080
3081 ret = of_gpio_named_count(dev->of_node, propname);
3082 if (ret >= 0)
3083 break;
3084 }
3085 return ret;
3086}
3087
3088static int platform_gpio_count(struct device *dev, const char *con_id)
3089{
3090 struct gpiod_lookup_table *table;
3091 struct gpiod_lookup *p;
3092 unsigned int count = 0;
3093
3094 table = gpiod_find_lookup_table(dev);
3095 if (!table)
3096 return -ENOENT;
3097
3098 for (p = &table->table[0]; p->chip_label; p++) {
3099 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3100 (!con_id && !p->con_id))
3101 count++;
3102 }
3103 if (!count)
3104 return -ENOENT;
3105
3106 return count;
3107}
3108
3109/**
3110 * gpiod_count - return the number of GPIOs associated with a device / function
3111 * or -ENOENT if no GPIO has been assigned to the requested function
3112 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3113 * @con_id: function within the GPIO consumer
3114 */
3115int gpiod_count(struct device *dev, const char *con_id)
3116{
3117 int count = -ENOENT;
3118
3119 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
3120 count = dt_gpio_count(dev, con_id);
3121 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
3122 count = acpi_gpio_count(dev, con_id);
3123
3124 if (count < 0)
3125 count = platform_gpio_count(dev, con_id);
3126
3127 return count;
3128}
3129EXPORT_SYMBOL_GPL(gpiod_count);
3130
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003131/**
Thierry Reding08791622014-04-25 16:54:22 +02003132 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09003133 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003134 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003135 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003136 *
3137 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003138 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07003139 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003140 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003141struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003142 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003143{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003144 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003145}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003146EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003147
3148/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02003149 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
3150 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3151 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003152 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02003153 *
3154 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
3155 * the requested function it will return NULL. This is convenient for drivers
3156 * that need to handle optional GPIOs.
3157 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003158struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003159 const char *con_id,
3160 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003161{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003162 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003163}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003164EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003165
Benoit Parrotf625d462015-02-02 11:44:44 -06003166
3167/**
3168 * gpiod_configure_flags - helper function to configure a given GPIO
3169 * @desc: gpio whose value will be assigned
3170 * @con_id: function within the GPIO consumer
Johan Hovold85b03b32016-07-03 18:32:05 +02003171 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3172 * of_get_gpio_hog()
Benoit Parrotf625d462015-02-02 11:44:44 -06003173 * @dflags: gpiod_flags - optional GPIO initialization flags
3174 *
3175 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
3176 * requested function and/or index, or another IS_ERR() code if an error
3177 * occurred while trying to acquire the GPIO.
3178 */
3179static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Johan Hovold85b03b32016-07-03 18:32:05 +02003180 unsigned long lflags, enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06003181{
3182 int status;
3183
Johan Hovold85b03b32016-07-03 18:32:05 +02003184 if (lflags & GPIO_ACTIVE_LOW)
3185 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
3186 if (lflags & GPIO_OPEN_DRAIN)
3187 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3188 if (lflags & GPIO_OPEN_SOURCE)
3189 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
3190
Benoit Parrotf625d462015-02-02 11:44:44 -06003191 /* No particular flag request, return here... */
3192 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
3193 pr_debug("no flags found for %s\n", con_id);
3194 return 0;
3195 }
3196
3197 /* Process flags */
3198 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
3199 status = gpiod_direction_output(desc,
3200 dflags & GPIOD_FLAGS_BIT_DIR_VAL);
3201 else
3202 status = gpiod_direction_input(desc);
3203
3204 return status;
3205}
3206
Thierry Reding29a1f2332014-04-25 17:10:06 +02003207/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003208 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02003209 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003210 * @con_id: function within the GPIO consumer
3211 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003212 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003213 *
3214 * This variant of gpiod_get() allows to access GPIOs other than the first
3215 * defined one for functions that define several GPIOs.
3216 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003217 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
3218 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07003219 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003220 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003221struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003222 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003223 unsigned int idx,
3224 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003225{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003226 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003227 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003228 enum gpio_lookup_flags lookupflags = 0;
Linus Walleijbe1f6052018-01-16 08:29:50 +01003229 /* Maybe we have a device name, maybe not */
3230 const char *devname = dev ? dev_name(dev) : "?";
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003231
3232 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
3233
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003234 if (dev) {
3235 /* Using device tree? */
3236 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
3237 dev_dbg(dev, "using device tree for GPIO lookup\n");
3238 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
3239 } else if (ACPI_COMPANION(dev)) {
3240 dev_dbg(dev, "using ACPI for GPIO lookup\n");
Dmitry Torokhov25487532016-03-24 10:50:25 -07003241 desc = acpi_find_gpio(dev, con_id, idx, flags, &lookupflags);
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003242 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003243 }
3244
3245 /*
3246 * Either we are not using DT or ACPI, or their lookup did not return
3247 * a result. In that case, use platform lookup as a fallback.
3248 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003249 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04003250 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003251 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003252 }
3253
3254 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02003255 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003256 return desc;
3257 }
3258
Linus Walleijbe1f6052018-01-16 08:29:50 +01003259 /*
3260 * If a connection label was passed use that, else attempt to use
3261 * the device name as label
3262 */
3263 status = gpiod_request(desc, con_id ? con_id : devname);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003264 if (status < 0)
3265 return ERR_PTR(status);
3266
Johan Hovold85b03b32016-07-03 18:32:05 +02003267 status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003268 if (status < 0) {
3269 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
3270 gpiod_put(desc);
3271 return ERR_PTR(status);
3272 }
3273
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003274 return desc;
3275}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003276EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003277
3278/**
Mika Westerberg40b73182014-10-21 13:33:59 +02003279 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
3280 * @fwnode: handle of the firmware node
3281 * @propname: name of the firmware property representing the GPIO
3282 *
3283 * This function can be used for drivers that get their configuration
3284 * from firmware.
3285 *
3286 * Function properly finds the corresponding GPIO using whatever is the
3287 * underlying firmware interface and then makes sure that the GPIO
3288 * descriptor is requested before it is returned to the caller.
3289 *
3290 * In case of error an ERR_PTR() is returned.
3291 */
3292struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
3293 const char *propname)
3294{
3295 struct gpio_desc *desc = ERR_PTR(-ENODEV);
3296 bool active_low = false;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003297 bool single_ended = false;
Mika Westerberg40b73182014-10-21 13:33:59 +02003298 int ret;
3299
3300 if (!fwnode)
3301 return ERR_PTR(-EINVAL);
3302
3303 if (is_of_node(fwnode)) {
3304 enum of_gpio_flags flags;
3305
Alexander Sverdlinc181fb32015-06-22 22:38:53 +02003306 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, 0,
Mika Westerberg40b73182014-10-21 13:33:59 +02003307 &flags);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003308 if (!IS_ERR(desc)) {
Mika Westerberg40b73182014-10-21 13:33:59 +02003309 active_low = flags & OF_GPIO_ACTIVE_LOW;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003310 single_ended = flags & OF_GPIO_SINGLE_ENDED;
3311 }
Mika Westerberg40b73182014-10-21 13:33:59 +02003312 } else if (is_acpi_node(fwnode)) {
3313 struct acpi_gpio_info info;
3314
Rafael J. Wysocki504a3372015-08-27 04:42:33 +02003315 desc = acpi_node_get_gpiod(fwnode, propname, 0, &info);
Mika Westerberg40b73182014-10-21 13:33:59 +02003316 if (!IS_ERR(desc))
Christophe RICARD52044722015-12-23 23:25:34 +01003317 active_low = info.polarity == GPIO_ACTIVE_LOW;
Mika Westerberg40b73182014-10-21 13:33:59 +02003318 }
3319
3320 if (IS_ERR(desc))
3321 return desc;
3322
Johan Hovold85b03b32016-07-03 18:32:05 +02003323 ret = gpiod_request(desc, NULL);
3324 if (ret)
3325 return ERR_PTR(ret);
3326
Mika Westerberg40b73182014-10-21 13:33:59 +02003327 if (active_low)
3328 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
3329
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003330 if (single_ended) {
3331 if (active_low)
3332 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3333 else
3334 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
3335 }
3336
Mika Westerberg40b73182014-10-21 13:33:59 +02003337 return desc;
3338}
3339EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
3340
3341/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02003342 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
3343 * function
3344 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3345 * @con_id: function within the GPIO consumer
3346 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003347 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02003348 *
3349 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
3350 * specified index was assigned to the requested function it will return NULL.
3351 * This is convenient for drivers that need to handle optional GPIOs.
3352 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003353struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02003354 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003355 unsigned int index,
3356 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003357{
3358 struct gpio_desc *desc;
3359
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003360 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003361 if (IS_ERR(desc)) {
3362 if (PTR_ERR(desc) == -ENOENT)
3363 return NULL;
3364 }
3365
3366 return desc;
3367}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003368EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003369
3370/**
Benoit Parrotf625d462015-02-02 11:44:44 -06003371 * gpiod_hog - Hog the specified GPIO desc given the provided flags
3372 * @desc: gpio whose value will be assigned
3373 * @name: gpio line name
3374 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3375 * of_get_gpio_hog()
3376 * @dflags: gpiod_flags - optional GPIO initialization flags
3377 */
3378int gpiod_hog(struct gpio_desc *desc, const char *name,
3379 unsigned long lflags, enum gpiod_flags dflags)
3380{
3381 struct gpio_chip *chip;
3382 struct gpio_desc *local_desc;
3383 int hwnum;
3384 int status;
3385
3386 chip = gpiod_to_chip(desc);
3387 hwnum = gpio_chip_hwgpio(desc);
3388
3389 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
3390 if (IS_ERR(local_desc)) {
Laxman Dewanganc31a5712016-03-11 19:13:21 +05303391 status = PTR_ERR(local_desc);
3392 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
3393 name, chip->label, hwnum, status);
3394 return status;
Benoit Parrotf625d462015-02-02 11:44:44 -06003395 }
3396
Johan Hovold85b03b32016-07-03 18:32:05 +02003397 status = gpiod_configure_flags(desc, name, lflags, dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -06003398 if (status < 0) {
Laxman Dewanganc31a5712016-03-11 19:13:21 +05303399 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed, %d\n",
3400 name, chip->label, hwnum, status);
Benoit Parrotf625d462015-02-02 11:44:44 -06003401 gpiochip_free_own_desc(desc);
3402 return status;
3403 }
3404
3405 /* Mark GPIO as hogged so it can be identified and removed later */
3406 set_bit(FLAG_IS_HOGGED, &desc->flags);
3407
3408 pr_info("GPIO line %d (%s) hogged as %s%s\n",
3409 desc_to_gpio(desc), name,
3410 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
3411 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
3412 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
3413
3414 return 0;
3415}
3416
3417/**
3418 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
3419 * @chip: gpio chip to act on
3420 *
3421 * This is only used by of_gpiochip_remove to free hogged gpios
3422 */
3423static void gpiochip_free_hogs(struct gpio_chip *chip)
3424{
3425 int id;
3426
3427 for (id = 0; id < chip->ngpio; id++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01003428 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
3429 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
Benoit Parrotf625d462015-02-02 11:44:44 -06003430 }
3431}
3432
3433/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003434 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
3435 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3436 * @con_id: function within the GPIO consumer
3437 * @flags: optional GPIO initialization flags
3438 *
3439 * This function acquires all the GPIOs defined under a given function.
3440 *
3441 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
3442 * no GPIO has been assigned to the requested function, or another IS_ERR()
3443 * code if an error occurred while trying to acquire the GPIOs.
3444 */
3445struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
3446 const char *con_id,
3447 enum gpiod_flags flags)
3448{
3449 struct gpio_desc *desc;
3450 struct gpio_descs *descs;
3451 int count;
3452
3453 count = gpiod_count(dev, con_id);
3454 if (count < 0)
3455 return ERR_PTR(count);
3456
3457 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
3458 GFP_KERNEL);
3459 if (!descs)
3460 return ERR_PTR(-ENOMEM);
3461
3462 for (descs->ndescs = 0; descs->ndescs < count; ) {
3463 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
3464 if (IS_ERR(desc)) {
3465 gpiod_put_array(descs);
3466 return ERR_CAST(desc);
3467 }
3468 descs->desc[descs->ndescs] = desc;
3469 descs->ndescs++;
3470 }
3471 return descs;
3472}
3473EXPORT_SYMBOL_GPL(gpiod_get_array);
3474
3475/**
3476 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
3477 * function
3478 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3479 * @con_id: function within the GPIO consumer
3480 * @flags: optional GPIO initialization flags
3481 *
3482 * This is equivalent to gpiod_get_array(), except that when no GPIO was
3483 * assigned to the requested function it will return NULL.
3484 */
3485struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
3486 const char *con_id,
3487 enum gpiod_flags flags)
3488{
3489 struct gpio_descs *descs;
3490
3491 descs = gpiod_get_array(dev, con_id, flags);
3492 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
3493 return NULL;
3494
3495 return descs;
3496}
3497EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
3498
3499/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003500 * gpiod_put - dispose of a GPIO descriptor
3501 * @desc: GPIO descriptor to dispose of
3502 *
3503 * No descriptor can be used after gpiod_put() has been called on it.
3504 */
3505void gpiod_put(struct gpio_desc *desc)
3506{
3507 gpiod_free(desc);
3508}
3509EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08003510
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003511/**
3512 * gpiod_put_array - dispose of multiple GPIO descriptors
3513 * @descs: struct gpio_descs containing an array of descriptors
3514 */
3515void gpiod_put_array(struct gpio_descs *descs)
3516{
3517 unsigned int i;
3518
3519 for (i = 0; i < descs->ndescs; i++)
3520 gpiod_put(descs->desc[i]);
3521
3522 kfree(descs);
3523}
3524EXPORT_SYMBOL_GPL(gpiod_put_array);
3525
Linus Walleij3c702e92015-10-21 15:29:53 +02003526static int __init gpiolib_dev_init(void)
3527{
3528 int ret;
3529
3530 /* Register GPIO sysfs bus */
3531 ret = bus_register(&gpio_bus_type);
3532 if (ret < 0) {
3533 pr_err("gpiolib: could not register GPIO bus type\n");
3534 return ret;
3535 }
3536
3537 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
3538 if (ret < 0) {
3539 pr_err("gpiolib: failed to allocate char dev region\n");
3540 bus_unregister(&gpio_bus_type);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07003541 } else {
3542 gpiolib_initialized = true;
3543 gpiochip_setup_devs();
Linus Walleij3c702e92015-10-21 15:29:53 +02003544 }
3545 return ret;
3546}
3547core_initcall(gpiolib_dev_init);
3548
David Brownelld2876d02008-02-04 22:28:20 -08003549#ifdef CONFIG_DEBUG_FS
3550
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003551static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
David Brownelld2876d02008-02-04 22:28:20 -08003552{
3553 unsigned i;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003554 struct gpio_chip *chip = gdev->chip;
3555 unsigned gpio = gdev->base;
3556 struct gpio_desc *gdesc = &gdev->descs[0];
David Brownelld2876d02008-02-04 22:28:20 -08003557 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02003558 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08003559
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003560 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02003561 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
3562 if (gdesc->name) {
3563 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
3564 gpio, gdesc->name);
3565 }
David Brownelld2876d02008-02-04 22:28:20 -08003566 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02003567 }
David Brownelld2876d02008-02-04 22:28:20 -08003568
Alexandre Courbot372e7222013-02-03 01:29:29 +09003569 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08003570 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02003571 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Markus Pargmannced433e2015-08-14 16:11:02 +02003572 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
3573 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08003574 is_out ? "out" : "in ",
3575 chip->get
3576 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02003577 : "? ",
3578 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08003579 seq_printf(s, "\n");
3580 }
3581}
3582
Thierry Redingf9c4a312012-04-12 13:26:01 +02003583static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08003584{
Grant Likely362432a2013-02-09 09:41:49 +00003585 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02003586 struct gpio_device *gdev = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003587 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003588
3589 s->private = "";
3590
Grant Likely362432a2013-02-09 09:41:49 +00003591 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003592 list_for_each_entry(gdev, &gpio_devices, list)
Grant Likely362432a2013-02-09 09:41:49 +00003593 if (index-- == 0) {
3594 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003595 return gdev;
Grant Likely362432a2013-02-09 09:41:49 +00003596 }
3597 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003598
3599 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003600}
3601
3602static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
3603{
Grant Likely362432a2013-02-09 09:41:49 +00003604 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02003605 struct gpio_device *gdev = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003606 void *ret = NULL;
3607
Grant Likely362432a2013-02-09 09:41:49 +00003608 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003609 if (list_is_last(&gdev->list, &gpio_devices))
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003610 ret = NULL;
3611 else
Linus Walleijff2b1352015-10-20 11:10:38 +02003612 ret = list_entry(gdev->list.next, struct gpio_device, list);
Grant Likely362432a2013-02-09 09:41:49 +00003613 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02003614
3615 s->private = "\n";
3616 ++*pos;
3617
3618 return ret;
3619}
3620
3621static void gpiolib_seq_stop(struct seq_file *s, void *v)
3622{
3623}
3624
3625static int gpiolib_seq_show(struct seq_file *s, void *v)
3626{
Linus Walleijff2b1352015-10-20 11:10:38 +02003627 struct gpio_device *gdev = v;
3628 struct gpio_chip *chip = gdev->chip;
3629 struct device *parent;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003630
Linus Walleijff2b1352015-10-20 11:10:38 +02003631 if (!chip) {
3632 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
3633 dev_name(&gdev->dev));
3634 return 0;
3635 }
3636
3637 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
3638 dev_name(&gdev->dev),
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003639 gdev->base, gdev->base + gdev->ngpio - 1);
Linus Walleijff2b1352015-10-20 11:10:38 +02003640 parent = chip->parent;
3641 if (parent)
3642 seq_printf(s, ", parent: %s/%s",
3643 parent->bus ? parent->bus->name : "no-bus",
3644 dev_name(parent));
Thierry Redingf9c4a312012-04-12 13:26:01 +02003645 if (chip->label)
3646 seq_printf(s, ", %s", chip->label);
3647 if (chip->can_sleep)
3648 seq_printf(s, ", can sleep");
3649 seq_printf(s, ":\n");
3650
3651 if (chip->dbg_show)
3652 chip->dbg_show(s, chip);
3653 else
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003654 gpiolib_dbg_show(s, gdev);
Thierry Redingf9c4a312012-04-12 13:26:01 +02003655
David Brownelld2876d02008-02-04 22:28:20 -08003656 return 0;
3657}
3658
Thierry Redingf9c4a312012-04-12 13:26:01 +02003659static const struct seq_operations gpiolib_seq_ops = {
3660 .start = gpiolib_seq_start,
3661 .next = gpiolib_seq_next,
3662 .stop = gpiolib_seq_stop,
3663 .show = gpiolib_seq_show,
3664};
3665
David Brownelld2876d02008-02-04 22:28:20 -08003666static int gpiolib_open(struct inode *inode, struct file *file)
3667{
Thierry Redingf9c4a312012-04-12 13:26:01 +02003668 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08003669}
3670
Alexey Dobriyan828c0952009-10-01 15:43:56 -07003671static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02003672 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08003673 .open = gpiolib_open,
3674 .read = seq_read,
3675 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02003676 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08003677};
3678
3679static int __init gpiolib_debugfs_init(void)
3680{
3681 /* /sys/kernel/debug/gpio */
3682 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
3683 NULL, NULL, &gpiolib_operations);
3684 return 0;
3685}
3686subsys_initcall(gpiolib_debugfs_init);
3687
3688#endif /* DEBUG_FS */