blob: 8113929eb2bdfe5adc9be407e58675386ba39e7e [file] [log] [blame]
Andy Shevchenko923a6542017-05-25 16:08:38 +03001#include <linux/bitmap.h>
David Brownelld2876d02008-02-04 22:28:20 -08002#include <linux/kernel.h>
3#include <linux/module.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -07004#include <linux/interrupt.h>
David Brownelld2876d02008-02-04 22:28:20 -08005#include <linux/irq.h>
6#include <linux/spinlock.h>
Alexandre Courbot1a989d02013-02-03 01:29:24 +09007#include <linux/list.h>
David Brownelld8f388d82008-07-25 01:46:07 -07008#include <linux/device.h>
9#include <linux/err.h>
10#include <linux/debugfs.h>
11#include <linux/seq_file.h>
12#include <linux/gpio.h>
Anton Vorontsov391c9702010-06-08 07:48:17 -060013#include <linux/of_gpio.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -070014#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Rafael J. Wysocki7b199812013-11-11 22:41:56 +010016#include <linux/acpi.h>
Alexandre Courbot53e7cac2013-11-16 21:44:52 +090017#include <linux/gpio/driver.h>
Linus Walleij0a6d3152014-07-24 20:08:55 +020018#include <linux/gpio/machine.h>
Jonas Gorskic771c2f2015-10-11 17:34:15 +020019#include <linux/pinctrl/consumer.h>
Linus Walleij3c702e92015-10-21 15:29:53 +020020#include <linux/cdev.h>
21#include <linux/fs.h>
22#include <linux/uaccess.h>
Linus Walleij8b92e172016-05-27 14:24:04 +020023#include <linux/compat.h>
Linus Walleijd7c51b42016-04-26 10:35:29 +020024#include <linux/anon_inodes.h>
Lars-Peter Clausen953b9562016-10-24 13:59:15 +020025#include <linux/file.h>
Linus Walleij61f922d2016-06-02 11:30:15 +020026#include <linux/kfifo.h>
27#include <linux/poll.h>
28#include <linux/timekeeping.h>
Linus Walleij3c702e92015-10-21 15:29:53 +020029#include <uapi/linux/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080030
Mika Westerberg664e3e52014-01-08 12:40:54 +020031#include "gpiolib.h"
32
Uwe Kleine-König3f397c212011-05-20 00:40:19 -060033#define CREATE_TRACE_POINTS
34#include <trace/events/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080035
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070036/* Implementation infrastructure for GPIO interfaces.
David Brownelld2876d02008-02-04 22:28:20 -080037 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070038 * The GPIO programming interface allows for inlining speed-critical
39 * get/set operations for common cases, so that access to SOC-integrated
40 * GPIOs can sometimes cost only an instruction or two per bit.
David Brownelld2876d02008-02-04 22:28:20 -080041 */
42
43
44/* When debugging, extend minimal trust to callers and platform code.
45 * Also emit diagnostic messages that may help initial bringup, when
46 * board setup or driver bugs are most common.
47 *
48 * Otherwise, minimize overhead in what may be bitbanging codepaths.
49 */
50#ifdef DEBUG
51#define extra_checks 1
52#else
53#define extra_checks 0
54#endif
55
Linus Walleijff2b1352015-10-20 11:10:38 +020056/* Device and char device-related information */
57static DEFINE_IDA(gpio_ida);
Linus Walleij3c702e92015-10-21 15:29:53 +020058static dev_t gpio_devt;
59#define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
60static struct bus_type gpio_bus_type = {
61 .name = "gpio",
62};
Linus Walleijff2b1352015-10-20 11:10:38 +020063
David Brownelld2876d02008-02-04 22:28:20 -080064/* gpio_lock prevents conflicts during gpio_desc[] table updates.
65 * While any GPIO is requested, its gpio_chip is not removable;
66 * each GPIO's "requested" flag serves as a lock and refcount.
67 */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090068DEFINE_SPINLOCK(gpio_lock);
David Brownelld2876d02008-02-04 22:28:20 -080069
Alexandre Courbotbae48da2013-10-17 10:21:38 -070070static DEFINE_MUTEX(gpio_lookup_lock);
71static LIST_HEAD(gpio_lookup_list);
Linus Walleijff2b1352015-10-20 11:10:38 +020072LIST_HEAD(gpio_devices);
Johan Hovold6d867502015-05-04 17:23:25 +020073
74static void gpiochip_free_hogs(struct gpio_chip *chip);
75static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
Mika Westerberg79b804c2016-09-20 15:15:21 +030076static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip);
77static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip);
Johan Hovold6d867502015-05-04 17:23:25 +020078
Guenter Roeck159f3cd2016-03-31 08:11:30 -070079static bool gpiolib_initialized;
Johan Hovold6d867502015-05-04 17:23:25 +020080
David Brownelld2876d02008-02-04 22:28:20 -080081static inline void desc_set_label(struct gpio_desc *d, const char *label)
82{
David Brownelld2876d02008-02-04 22:28:20 -080083 d->label = label;
David Brownelld2876d02008-02-04 22:28:20 -080084}
85
Alexandre Courbot372e7222013-02-03 01:29:29 +090086/**
Thierry Reding950d55f52017-07-24 16:57:22 +020087 * gpio_to_desc - Convert a GPIO number to its descriptor
88 * @gpio: global GPIO number
89 *
90 * Returns:
91 * The GPIO descriptor associated with the given GPIO, or %NULL if no GPIO
92 * with the given number exists in the system.
Alexandre Courbot372e7222013-02-03 01:29:29 +090093 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070094struct gpio_desc *gpio_to_desc(unsigned gpio)
Alexandre Courbot372e7222013-02-03 01:29:29 +090095{
Linus Walleijff2b1352015-10-20 11:10:38 +020096 struct gpio_device *gdev;
Alexandre Courbot14e85c02014-11-19 16:51:27 +090097 unsigned long flags;
98
99 spin_lock_irqsave(&gpio_lock, flags);
100
Linus Walleijff2b1352015-10-20 11:10:38 +0200101 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100102 if (gdev->base <= gpio &&
103 gdev->base + gdev->ngpio > gpio) {
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900104 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100105 return &gdev->descs[gpio - gdev->base];
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900106 }
107 }
108
109 spin_unlock_irqrestore(&gpio_lock, flags);
110
Alexandre Courbot0e9a5ed2014-12-02 23:15:05 +0900111 if (!gpio_is_valid(gpio))
112 WARN(1, "invalid GPIO %d\n", gpio);
113
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900114 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900115}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700116EXPORT_SYMBOL_GPL(gpio_to_desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900117
118/**
Thierry Reding950d55f52017-07-24 16:57:22 +0200119 * gpiochip_get_desc - get the GPIO descriptor corresponding to the given
120 * hardware number for this chip
121 * @chip: GPIO chip
122 * @hwnum: hardware number of the GPIO for this chip
123 *
124 * Returns:
125 * A pointer to the GPIO descriptor or %ERR_PTR(-EINVAL) if no GPIO exists
126 * in the given chip for the specified hardware number.
Linus Walleijd468bf92013-09-24 11:54:38 +0200127 */
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900128struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
129 u16 hwnum)
Linus Walleijd468bf92013-09-24 11:54:38 +0200130{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100131 struct gpio_device *gdev = chip->gpiodev;
132
133 if (hwnum >= gdev->ngpio)
Alexandre Courbotb7d0a282013-12-03 12:31:11 +0900134 return ERR_PTR(-EINVAL);
Linus Walleijd468bf92013-09-24 11:54:38 +0200135
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100136 return &gdev->descs[hwnum];
Linus Walleijd468bf92013-09-24 11:54:38 +0200137}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900138
139/**
Thierry Reding950d55f52017-07-24 16:57:22 +0200140 * desc_to_gpio - convert a GPIO descriptor to the integer namespace
141 * @desc: GPIO descriptor
142 *
Alexandre Courbot372e7222013-02-03 01:29:29 +0900143 * This should disappear in the future but is needed since we still
Thierry Reding950d55f52017-07-24 16:57:22 +0200144 * use GPIO numbers for error messages and sysfs nodes.
145 *
146 * Returns:
147 * The global GPIO number for the GPIO specified by its descriptor.
Alexandre Courbot372e7222013-02-03 01:29:29 +0900148 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700149int desc_to_gpio(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900150{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100151 return desc->gdev->base + (desc - &desc->gdev->descs[0]);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900152}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700153EXPORT_SYMBOL_GPL(desc_to_gpio);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900154
155
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700156/**
157 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
158 * @desc: descriptor to return the chip of
159 */
160struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900161{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100162 if (!desc || !desc->gdev || !desc->gdev->chip)
163 return NULL;
164 return desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900165}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700166EXPORT_SYMBOL_GPL(gpiod_to_chip);
David Brownelld2876d02008-02-04 22:28:20 -0800167
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700168/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
169static int gpiochip_find_base(int ngpio)
170{
Linus Walleijff2b1352015-10-20 11:10:38 +0200171 struct gpio_device *gdev;
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900172 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700173
Linus Walleijff2b1352015-10-20 11:10:38 +0200174 list_for_each_entry_reverse(gdev, &gpio_devices, list) {
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900175 /* found a free space? */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100176 if (gdev->base + gdev->ngpio <= base)
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900177 break;
178 else
179 /* nope, check the space right before the chip */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100180 base = gdev->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700181 }
182
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900183 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700184 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900185 return base;
186 } else {
187 pr_err("%s: cannot find free range\n", __func__);
188 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700189 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700190}
191
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700192/**
193 * gpiod_get_direction - return the current direction of a GPIO
194 * @desc: GPIO to get the direction of
195 *
196 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
197 *
198 * This function may sleep if gpiod_cansleep() is true.
199 */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900200int gpiod_get_direction(struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300201{
202 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900203 unsigned offset;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300204 int status = -EINVAL;
205
Alexandre Courbot372e7222013-02-03 01:29:29 +0900206 chip = gpiod_to_chip(desc);
207 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300208
209 if (!chip->get_direction)
210 return status;
211
Alexandre Courbot372e7222013-02-03 01:29:29 +0900212 status = chip->get_direction(chip, offset);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300213 if (status > 0) {
214 /* GPIOF_DIR_IN, or other positive */
215 status = 1;
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900216 clear_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300217 }
218 if (status == 0) {
219 /* GPIOF_DIR_OUT */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900220 set_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300221 }
222 return status;
223}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700224EXPORT_SYMBOL_GPL(gpiod_get_direction);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300225
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900226/*
227 * Add a new chip to the global chips list, keeping the list of chips sorted
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800228 * by range(means [base, base + ngpio - 1]) order.
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900229 *
230 * Return -EBUSY if the new chip overlaps with some other chip's integer
231 * space.
232 */
Linus Walleijff2b1352015-10-20 11:10:38 +0200233static int gpiodev_add_to_list(struct gpio_device *gdev)
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900234{
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800235 struct gpio_device *prev, *next;
Linus Walleijff2b1352015-10-20 11:10:38 +0200236
237 if (list_empty(&gpio_devices)) {
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800238 /* initial entry in list */
Linus Walleijff2b1352015-10-20 11:10:38 +0200239 list_add_tail(&gdev->list, &gpio_devices);
Sudip Mukherjeee28ecca2015-12-27 19:06:50 +0530240 return 0;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900241 }
242
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800243 next = list_entry(gpio_devices.next, struct gpio_device, list);
244 if (gdev->base + gdev->ngpio <= next->base) {
245 /* add before first entry */
246 list_add(&gdev->list, &gpio_devices);
Julien Grossholtz96098df2016-01-07 16:46:45 -0500247 return 0;
248 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900249
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800250 prev = list_entry(gpio_devices.prev, struct gpio_device, list);
251 if (prev->base + prev->ngpio <= gdev->base) {
252 /* add behind last entry */
253 list_add_tail(&gdev->list, &gpio_devices);
254 return 0;
255 }
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800256
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800257 list_for_each_entry_safe(prev, next, &gpio_devices, list) {
258 /* at the end of the list */
259 if (&next->list == &gpio_devices)
260 break;
261
262 /* add between prev and next */
263 if (prev->base + prev->ngpio <= gdev->base
264 && gdev->base + gdev->ngpio <= next->base) {
265 list_add(&gdev->list, &prev->list);
266 return 0;
267 }
268 }
269
270 dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n");
271 return -EBUSY;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900272}
273
Thierry Reding950d55f52017-07-24 16:57:22 +0200274/*
Linus Walleijf881bab2015-09-23 16:20:43 -0700275 * Convert a GPIO name to its descriptor
276 */
277static struct gpio_desc *gpio_name_to_desc(const char * const name)
278{
Linus Walleijff2b1352015-10-20 11:10:38 +0200279 struct gpio_device *gdev;
Linus Walleijf881bab2015-09-23 16:20:43 -0700280 unsigned long flags;
281
282 spin_lock_irqsave(&gpio_lock, flags);
283
Linus Walleijff2b1352015-10-20 11:10:38 +0200284 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijf881bab2015-09-23 16:20:43 -0700285 int i;
286
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100287 for (i = 0; i != gdev->ngpio; ++i) {
288 struct gpio_desc *desc = &gdev->descs[i];
Linus Walleijf881bab2015-09-23 16:20:43 -0700289
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100290 if (!desc->name || !name)
Linus Walleijf881bab2015-09-23 16:20:43 -0700291 continue;
292
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100293 if (!strcmp(desc->name, name)) {
Linus Walleijf881bab2015-09-23 16:20:43 -0700294 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100295 return desc;
Linus Walleijf881bab2015-09-23 16:20:43 -0700296 }
297 }
298 }
299
300 spin_unlock_irqrestore(&gpio_lock, flags);
301
302 return NULL;
303}
304
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200305/*
306 * Takes the names from gc->names and checks if they are all unique. If they
307 * are, they are assigned to their gpio descriptors.
308 *
Bamvor Jian Zhanged379152015-11-14 16:43:20 +0800309 * Warning if one of the names is already used for a different GPIO.
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200310 */
311static int gpiochip_set_desc_names(struct gpio_chip *gc)
312{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100313 struct gpio_device *gdev = gc->gpiodev;
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200314 int i;
315
316 if (!gc->names)
317 return 0;
318
319 /* First check all names if they are unique */
320 for (i = 0; i != gc->ngpio; ++i) {
321 struct gpio_desc *gpio;
322
323 gpio = gpio_name_to_desc(gc->names[i]);
Linus Walleijf881bab2015-09-23 16:20:43 -0700324 if (gpio)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100325 dev_warn(&gdev->dev,
Linus Walleij34ffd852015-10-20 11:31:54 +0200326 "Detected name collision for GPIO name '%s'\n",
Linus Walleijf881bab2015-09-23 16:20:43 -0700327 gc->names[i]);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200328 }
329
330 /* Then add all names to the GPIO descriptors */
331 for (i = 0; i != gc->ngpio; ++i)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100332 gdev->descs[i].name = gc->names[i];
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200333
334 return 0;
335}
336
Linus Walleijd7c51b42016-04-26 10:35:29 +0200337/*
338 * GPIO line handle management
339 */
340
341/**
342 * struct linehandle_state - contains the state of a userspace handle
343 * @gdev: the GPIO device the handle pertains to
344 * @label: consumer label used to tag descriptors
345 * @descs: the GPIO descriptors held by this handle
346 * @numdescs: the number of descriptors held in the descs array
347 */
348struct linehandle_state {
349 struct gpio_device *gdev;
350 const char *label;
351 struct gpio_desc *descs[GPIOHANDLES_MAX];
352 u32 numdescs;
353};
354
Lars-Peter Clausene3e847c2016-10-18 16:54:05 +0200355#define GPIOHANDLE_REQUEST_VALID_FLAGS \
356 (GPIOHANDLE_REQUEST_INPUT | \
357 GPIOHANDLE_REQUEST_OUTPUT | \
358 GPIOHANDLE_REQUEST_ACTIVE_LOW | \
359 GPIOHANDLE_REQUEST_OPEN_DRAIN | \
360 GPIOHANDLE_REQUEST_OPEN_SOURCE)
361
Linus Walleijd7c51b42016-04-26 10:35:29 +0200362static long linehandle_ioctl(struct file *filep, unsigned int cmd,
363 unsigned long arg)
364{
365 struct linehandle_state *lh = filep->private_data;
366 void __user *ip = (void __user *)arg;
367 struct gpiohandle_data ghd;
368 int i;
369
370 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
371 int val;
372
Lars-Peter Clausen3eded5d2016-10-18 16:54:02 +0200373 memset(&ghd, 0, sizeof(ghd));
374
Linus Walleijd7c51b42016-04-26 10:35:29 +0200375 /* TODO: check if descriptors are really input */
376 for (i = 0; i < lh->numdescs; i++) {
377 val = gpiod_get_value_cansleep(lh->descs[i]);
378 if (val < 0)
379 return val;
380 ghd.values[i] = val;
381 }
382
383 if (copy_to_user(ip, &ghd, sizeof(ghd)))
384 return -EFAULT;
385
386 return 0;
387 } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
388 int vals[GPIOHANDLES_MAX];
389
390 /* TODO: check if descriptors are really output */
391 if (copy_from_user(&ghd, ip, sizeof(ghd)))
392 return -EFAULT;
393
394 /* Clamp all values to [0,1] */
395 for (i = 0; i < lh->numdescs; i++)
396 vals[i] = !!ghd.values[i];
397
398 /* Reuse the array setting function */
399 gpiod_set_array_value_complex(false,
400 true,
401 lh->numdescs,
402 lh->descs,
403 vals);
404 return 0;
405 }
406 return -EINVAL;
407}
408
409#ifdef CONFIG_COMPAT
410static long linehandle_ioctl_compat(struct file *filep, unsigned int cmd,
411 unsigned long arg)
412{
413 return linehandle_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
414}
415#endif
416
417static int linehandle_release(struct inode *inode, struct file *filep)
418{
419 struct linehandle_state *lh = filep->private_data;
420 struct gpio_device *gdev = lh->gdev;
421 int i;
422
423 for (i = 0; i < lh->numdescs; i++)
424 gpiod_free(lh->descs[i]);
425 kfree(lh->label);
426 kfree(lh);
427 put_device(&gdev->dev);
428 return 0;
429}
430
431static const struct file_operations linehandle_fileops = {
432 .release = linehandle_release,
433 .owner = THIS_MODULE,
434 .llseek = noop_llseek,
435 .unlocked_ioctl = linehandle_ioctl,
436#ifdef CONFIG_COMPAT
437 .compat_ioctl = linehandle_ioctl_compat,
438#endif
439};
440
441static int linehandle_create(struct gpio_device *gdev, void __user *ip)
442{
443 struct gpiohandle_request handlereq;
444 struct linehandle_state *lh;
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200445 struct file *file;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200446 int fd, i, ret;
447
448 if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
449 return -EFAULT;
450 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
451 return -EINVAL;
452
453 lh = kzalloc(sizeof(*lh), GFP_KERNEL);
454 if (!lh)
455 return -ENOMEM;
456 lh->gdev = gdev;
457 get_device(&gdev->dev);
458
459 /* Make sure this is terminated */
460 handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
461 if (strlen(handlereq.consumer_label)) {
462 lh->label = kstrdup(handlereq.consumer_label,
463 GFP_KERNEL);
464 if (!lh->label) {
465 ret = -ENOMEM;
466 goto out_free_lh;
467 }
468 }
469
470 /* Request each GPIO */
471 for (i = 0; i < handlereq.lines; i++) {
472 u32 offset = handlereq.lineoffsets[i];
473 u32 lflags = handlereq.flags;
474 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;
492
493 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
494 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
495 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
496 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
497 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
498 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
499
500 /*
501 * Lines have to be requested explicitly for input
502 * or output, else the line will be treated "as is".
503 */
504 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
505 int val = !!handlereq.default_values[i];
506
507 ret = gpiod_direction_output(desc, val);
508 if (ret)
509 goto out_free_descs;
510 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
511 ret = gpiod_direction_input(desc);
512 if (ret)
513 goto out_free_descs;
514 }
515 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
516 offset);
517 }
Linus Walleije2f608b2016-06-18 10:56:43 +0200518 /* Let i point at the last handle */
519 i--;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200520 lh->numdescs = handlereq.lines;
521
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200522 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200523 if (fd < 0) {
524 ret = fd;
525 goto out_free_descs;
526 }
527
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200528 file = anon_inode_getfile("gpio-linehandle",
529 &linehandle_fileops,
530 lh,
531 O_RDONLY | O_CLOEXEC);
532 if (IS_ERR(file)) {
533 ret = PTR_ERR(file);
534 goto out_put_unused_fd;
535 }
536
Linus Walleijd7c51b42016-04-26 10:35:29 +0200537 handlereq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200538 if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200539 /*
540 * fput() will trigger the release() callback, so do not go onto
541 * the regular error cleanup path here.
542 */
543 fput(file);
544 put_unused_fd(fd);
545 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +0200546 }
Linus Walleijd7c51b42016-04-26 10:35:29 +0200547
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200548 fd_install(fd, file);
549
Linus Walleijd7c51b42016-04-26 10:35:29 +0200550 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
551 lh->numdescs);
552
553 return 0;
554
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200555out_put_unused_fd:
556 put_unused_fd(fd);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200557out_free_descs:
558 for (; i >= 0; i--)
559 gpiod_free(lh->descs[i]);
560 kfree(lh->label);
561out_free_lh:
562 kfree(lh);
563 put_device(&gdev->dev);
564 return ret;
565}
566
Linus Walleij61f922d2016-06-02 11:30:15 +0200567/*
568 * GPIO line event management
569 */
570
571/**
572 * struct lineevent_state - contains the state of a userspace event
573 * @gdev: the GPIO device the event pertains to
574 * @label: consumer label used to tag descriptors
575 * @desc: the GPIO descriptor held by this event
576 * @eflags: the event flags this line was requested with
577 * @irq: the interrupt that trigger in response to events on this GPIO
578 * @wait: wait queue that handles blocking reads of events
579 * @events: KFIFO for the GPIO events
580 * @read_lock: mutex lock to protect reads from colliding with adding
581 * new events to the FIFO
582 */
583struct lineevent_state {
584 struct gpio_device *gdev;
585 const char *label;
586 struct gpio_desc *desc;
587 u32 eflags;
588 int irq;
589 wait_queue_head_t wait;
590 DECLARE_KFIFO(events, struct gpioevent_data, 16);
591 struct mutex read_lock;
592};
593
Lars-Peter Clausenac7dbb92016-10-18 16:54:06 +0200594#define GPIOEVENT_REQUEST_VALID_FLAGS \
595 (GPIOEVENT_REQUEST_RISING_EDGE | \
596 GPIOEVENT_REQUEST_FALLING_EDGE)
597
Linus Walleij61f922d2016-06-02 11:30:15 +0200598static unsigned int lineevent_poll(struct file *filep,
599 struct poll_table_struct *wait)
600{
601 struct lineevent_state *le = filep->private_data;
602 unsigned int events = 0;
603
604 poll_wait(filep, &le->wait, wait);
605
606 if (!kfifo_is_empty(&le->events))
607 events = POLLIN | POLLRDNORM;
608
609 return events;
610}
611
612
613static ssize_t lineevent_read(struct file *filep,
614 char __user *buf,
615 size_t count,
616 loff_t *f_ps)
617{
618 struct lineevent_state *le = filep->private_data;
619 unsigned int copied;
620 int ret;
621
622 if (count < sizeof(struct gpioevent_data))
623 return -EINVAL;
624
625 do {
626 if (kfifo_is_empty(&le->events)) {
627 if (filep->f_flags & O_NONBLOCK)
628 return -EAGAIN;
629
630 ret = wait_event_interruptible(le->wait,
631 !kfifo_is_empty(&le->events));
632 if (ret)
633 return ret;
634 }
635
636 if (mutex_lock_interruptible(&le->read_lock))
637 return -ERESTARTSYS;
638 ret = kfifo_to_user(&le->events, buf, count, &copied);
639 mutex_unlock(&le->read_lock);
640
641 if (ret)
642 return ret;
643
644 /*
645 * If we couldn't read anything from the fifo (a different
646 * thread might have been faster) we either return -EAGAIN if
647 * the file descriptor is non-blocking, otherwise we go back to
648 * sleep and wait for more data to arrive.
649 */
650 if (copied == 0 && (filep->f_flags & O_NONBLOCK))
651 return -EAGAIN;
652
653 } while (copied == 0);
654
655 return copied;
656}
657
658static int lineevent_release(struct inode *inode, struct file *filep)
659{
660 struct lineevent_state *le = filep->private_data;
661 struct gpio_device *gdev = le->gdev;
662
663 free_irq(le->irq, le);
664 gpiod_free(le->desc);
665 kfree(le->label);
666 kfree(le);
667 put_device(&gdev->dev);
668 return 0;
669}
670
671static long lineevent_ioctl(struct file *filep, unsigned int cmd,
672 unsigned long arg)
673{
674 struct lineevent_state *le = filep->private_data;
675 void __user *ip = (void __user *)arg;
676 struct gpiohandle_data ghd;
677
678 /*
679 * We can get the value for an event line but not set it,
680 * because it is input by definition.
681 */
682 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
683 int val;
684
Lars-Peter Clausend82aa4a2016-10-18 16:54:04 +0200685 memset(&ghd, 0, sizeof(ghd));
686
Linus Walleij61f922d2016-06-02 11:30:15 +0200687 val = gpiod_get_value_cansleep(le->desc);
688 if (val < 0)
689 return val;
690 ghd.values[0] = val;
691
692 if (copy_to_user(ip, &ghd, sizeof(ghd)))
693 return -EFAULT;
694
695 return 0;
696 }
697 return -EINVAL;
698}
699
700#ifdef CONFIG_COMPAT
701static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd,
702 unsigned long arg)
703{
704 return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
705}
706#endif
707
708static const struct file_operations lineevent_fileops = {
709 .release = lineevent_release,
710 .read = lineevent_read,
711 .poll = lineevent_poll,
712 .owner = THIS_MODULE,
713 .llseek = noop_llseek,
714 .unlocked_ioctl = lineevent_ioctl,
715#ifdef CONFIG_COMPAT
716 .compat_ioctl = lineevent_ioctl_compat,
717#endif
718};
719
Ben Dooks33265b12016-06-17 16:03:13 +0100720static irqreturn_t lineevent_irq_thread(int irq, void *p)
Linus Walleij61f922d2016-06-02 11:30:15 +0200721{
722 struct lineevent_state *le = p;
723 struct gpioevent_data ge;
Bartosz Golaszewskidf1e76f2017-07-03 11:12:03 +0200724 int ret, level;
Linus Walleij61f922d2016-06-02 11:30:15 +0200725
726 ge.timestamp = ktime_get_real_ns();
Bartosz Golaszewskidf1e76f2017-07-03 11:12:03 +0200727 level = gpiod_get_value_cansleep(le->desc);
Linus Walleij61f922d2016-06-02 11:30:15 +0200728
Bartosz Golaszewskiad537b82017-06-23 13:45:16 +0200729 if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
730 && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200731 if (level)
732 /* Emit low-to-high event */
733 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
734 else
735 /* Emit high-to-low event */
736 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Bartosz Golaszewskidf1e76f2017-07-03 11:12:03 +0200737 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE && level) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200738 /* Emit low-to-high event */
739 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
Bartosz Golaszewskidf1e76f2017-07-03 11:12:03 +0200740 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE && !level) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200741 /* Emit high-to-low event */
742 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Arnd Bergmannbc0207a2016-06-16 11:02:41 +0200743 } else {
744 return IRQ_NONE;
Linus Walleij61f922d2016-06-02 11:30:15 +0200745 }
746
747 ret = kfifo_put(&le->events, ge);
748 if (ret != 0)
749 wake_up_poll(&le->wait, POLLIN);
750
751 return IRQ_HANDLED;
752}
753
754static int lineevent_create(struct gpio_device *gdev, void __user *ip)
755{
756 struct gpioevent_request eventreq;
757 struct lineevent_state *le;
758 struct gpio_desc *desc;
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200759 struct file *file;
Linus Walleij61f922d2016-06-02 11:30:15 +0200760 u32 offset;
761 u32 lflags;
762 u32 eflags;
763 int fd;
764 int ret;
765 int irqflags = 0;
766
767 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
768 return -EFAULT;
769
770 le = kzalloc(sizeof(*le), GFP_KERNEL);
771 if (!le)
772 return -ENOMEM;
773 le->gdev = gdev;
774 get_device(&gdev->dev);
775
776 /* Make sure this is terminated */
777 eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
778 if (strlen(eventreq.consumer_label)) {
779 le->label = kstrdup(eventreq.consumer_label,
780 GFP_KERNEL);
781 if (!le->label) {
782 ret = -ENOMEM;
783 goto out_free_le;
784 }
785 }
786
787 offset = eventreq.lineoffset;
788 lflags = eventreq.handleflags;
789 eflags = eventreq.eventflags;
790
Lars-Peter Clausenb8b0e3d2016-10-18 16:54:03 +0200791 if (offset >= gdev->ngpio) {
792 ret = -EINVAL;
793 goto out_free_label;
794 }
795
Lars-Peter Clausenac7dbb92016-10-18 16:54:06 +0200796 /* Return an error if a unknown flag is set */
797 if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
798 (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) {
799 ret = -EINVAL;
800 goto out_free_label;
801 }
802
Linus Walleij61f922d2016-06-02 11:30:15 +0200803 /* This is just wrong: we don't look for events on output lines */
804 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
805 ret = -EINVAL;
806 goto out_free_label;
807 }
808
809 desc = &gdev->descs[offset];
810 ret = gpiod_request(desc, le->label);
811 if (ret)
812 goto out_free_desc;
813 le->desc = desc;
814 le->eflags = eflags;
815
816 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
817 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
818 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
819 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
820 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
821 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
822
823 ret = gpiod_direction_input(desc);
824 if (ret)
825 goto out_free_desc;
826
827 le->irq = gpiod_to_irq(desc);
828 if (le->irq <= 0) {
829 ret = -ENODEV;
830 goto out_free_desc;
831 }
832
833 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
834 irqflags |= IRQF_TRIGGER_RISING;
835 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
836 irqflags |= IRQF_TRIGGER_FALLING;
837 irqflags |= IRQF_ONESHOT;
838 irqflags |= IRQF_SHARED;
839
840 INIT_KFIFO(le->events);
841 init_waitqueue_head(&le->wait);
842 mutex_init(&le->read_lock);
843
844 /* Request a thread to read the events */
845 ret = request_threaded_irq(le->irq,
846 NULL,
847 lineevent_irq_thread,
848 irqflags,
849 le->label,
850 le);
851 if (ret)
852 goto out_free_desc;
853
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200854 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleij61f922d2016-06-02 11:30:15 +0200855 if (fd < 0) {
856 ret = fd;
857 goto out_free_irq;
858 }
859
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200860 file = anon_inode_getfile("gpio-event",
861 &lineevent_fileops,
862 le,
863 O_RDONLY | O_CLOEXEC);
864 if (IS_ERR(file)) {
865 ret = PTR_ERR(file);
866 goto out_put_unused_fd;
867 }
868
Linus Walleij61f922d2016-06-02 11:30:15 +0200869 eventreq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200870 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200871 /*
872 * fput() will trigger the release() callback, so do not go onto
873 * the regular error cleanup path here.
874 */
875 fput(file);
876 put_unused_fd(fd);
877 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +0200878 }
Linus Walleij61f922d2016-06-02 11:30:15 +0200879
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200880 fd_install(fd, file);
881
Linus Walleij61f922d2016-06-02 11:30:15 +0200882 return 0;
883
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200884out_put_unused_fd:
885 put_unused_fd(fd);
Linus Walleij61f922d2016-06-02 11:30:15 +0200886out_free_irq:
887 free_irq(le->irq, le);
888out_free_desc:
889 gpiod_free(le->desc);
890out_free_label:
891 kfree(le->label);
892out_free_le:
893 kfree(le);
894 put_device(&gdev->dev);
895 return ret;
896}
897
Thierry Reding950d55f52017-07-24 16:57:22 +0200898/*
Linus Walleij3c702e92015-10-21 15:29:53 +0200899 * gpio_ioctl() - ioctl handler for the GPIO chardev
900 */
901static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
902{
903 struct gpio_device *gdev = filp->private_data;
904 struct gpio_chip *chip = gdev->chip;
Linus Walleij8b92e172016-05-27 14:24:04 +0200905 void __user *ip = (void __user *)arg;
Linus Walleij3c702e92015-10-21 15:29:53 +0200906
907 /* We fail any subsequent ioctl():s when the chip is gone */
908 if (!chip)
909 return -ENODEV;
910
Linus Walleij521a2ad2016-02-12 22:25:22 +0100911 /* Fill in the struct and pass to userspace */
Linus Walleij3c702e92015-10-21 15:29:53 +0200912 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
Linus Walleij521a2ad2016-02-12 22:25:22 +0100913 struct gpiochip_info chipinfo;
914
Lars-Peter Clausen0f4bbb22016-10-18 16:54:00 +0200915 memset(&chipinfo, 0, sizeof(chipinfo));
916
Linus Walleij3c702e92015-10-21 15:29:53 +0200917 strncpy(chipinfo.name, dev_name(&gdev->dev),
918 sizeof(chipinfo.name));
919 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
Linus Walleijdf4878e2016-02-12 14:48:23 +0100920 strncpy(chipinfo.label, gdev->label,
921 sizeof(chipinfo.label));
922 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100923 chipinfo.lines = gdev->ngpio;
Linus Walleij3c702e92015-10-21 15:29:53 +0200924 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
925 return -EFAULT;
926 return 0;
Linus Walleij521a2ad2016-02-12 22:25:22 +0100927 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
928 struct gpioline_info lineinfo;
929 struct gpio_desc *desc;
930
931 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
932 return -EFAULT;
Lars-Peter Clausen1f1cc452016-10-18 16:53:59 +0200933 if (lineinfo.line_offset >= gdev->ngpio)
Linus Walleij521a2ad2016-02-12 22:25:22 +0100934 return -EINVAL;
935
936 desc = &gdev->descs[lineinfo.line_offset];
937 if (desc->name) {
938 strncpy(lineinfo.name, desc->name,
939 sizeof(lineinfo.name));
940 lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
941 } else {
942 lineinfo.name[0] = '\0';
943 }
944 if (desc->label) {
Linus Walleij214338e2016-02-25 21:01:48 +0100945 strncpy(lineinfo.consumer, desc->label,
946 sizeof(lineinfo.consumer));
947 lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +0100948 } else {
Linus Walleij214338e2016-02-25 21:01:48 +0100949 lineinfo.consumer[0] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +0100950 }
951
952 /*
953 * Userspace only need to know that the kernel is using
954 * this GPIO so it can't use it.
955 */
956 lineinfo.flags = 0;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100957 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
958 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
959 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
960 test_bit(FLAG_EXPORT, &desc->flags) ||
961 test_bit(FLAG_SYSFS, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100962 lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100963 if (test_bit(FLAG_IS_OUT, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100964 lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100965 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100966 lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100967 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100968 lineinfo.flags |= GPIOLINE_FLAG_OPEN_DRAIN;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100969 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100970 lineinfo.flags |= GPIOLINE_FLAG_OPEN_SOURCE;
971
972 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
973 return -EFAULT;
974 return 0;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200975 } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
976 return linehandle_create(gdev, ip);
Linus Walleij61f922d2016-06-02 11:30:15 +0200977 } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
978 return lineevent_create(gdev, ip);
Linus Walleij3c702e92015-10-21 15:29:53 +0200979 }
980 return -EINVAL;
981}
982
Linus Walleij8b92e172016-05-27 14:24:04 +0200983#ifdef CONFIG_COMPAT
984static long gpio_ioctl_compat(struct file *filp, unsigned int cmd,
985 unsigned long arg)
986{
987 return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
988}
989#endif
990
Linus Walleij3c702e92015-10-21 15:29:53 +0200991/**
992 * gpio_chrdev_open() - open the chardev for ioctl operations
993 * @inode: inode for this chardev
994 * @filp: file struct for storing private data
995 * Returns 0 on success
996 */
997static int gpio_chrdev_open(struct inode *inode, struct file *filp)
998{
999 struct gpio_device *gdev = container_of(inode->i_cdev,
1000 struct gpio_device, chrdev);
1001
1002 /* Fail on open if the backing gpiochip is gone */
Stephen Boydfb505742017-01-09 11:47:44 -08001003 if (!gdev->chip)
Linus Walleij3c702e92015-10-21 15:29:53 +02001004 return -ENODEV;
1005 get_device(&gdev->dev);
1006 filp->private_data = gdev;
Lars-Peter Clausenf4e81c52016-11-30 13:05:21 +01001007
1008 return nonseekable_open(inode, filp);
Linus Walleij3c702e92015-10-21 15:29:53 +02001009}
1010
1011/**
1012 * gpio_chrdev_release() - close chardev after ioctl operations
1013 * @inode: inode for this chardev
1014 * @filp: file struct for storing private data
1015 * Returns 0 on success
1016 */
1017static int gpio_chrdev_release(struct inode *inode, struct file *filp)
1018{
1019 struct gpio_device *gdev = container_of(inode->i_cdev,
1020 struct gpio_device, chrdev);
1021
Linus Walleij3c702e92015-10-21 15:29:53 +02001022 put_device(&gdev->dev);
1023 return 0;
1024}
1025
1026
1027static const struct file_operations gpio_fileops = {
1028 .release = gpio_chrdev_release,
1029 .open = gpio_chrdev_open,
1030 .owner = THIS_MODULE,
Lars-Peter Clausenf4e81c52016-11-30 13:05:21 +01001031 .llseek = no_llseek,
Linus Walleij3c702e92015-10-21 15:29:53 +02001032 .unlocked_ioctl = gpio_ioctl,
Linus Walleij8b92e172016-05-27 14:24:04 +02001033#ifdef CONFIG_COMPAT
1034 .compat_ioctl = gpio_ioctl_compat,
1035#endif
Linus Walleij3c702e92015-10-21 15:29:53 +02001036};
1037
Linus Walleijff2b1352015-10-20 11:10:38 +02001038static void gpiodevice_release(struct device *dev)
1039{
1040 struct gpio_device *gdev = dev_get_drvdata(dev);
1041
1042 list_del(&gdev->list);
1043 ida_simple_remove(&gpio_ida, gdev->id);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001044 kfree(gdev->label);
1045 kfree(gdev->descs);
Linus Walleij9efd9e62016-02-09 14:27:42 +01001046 kfree(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001047}
1048
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001049static int gpiochip_setup_dev(struct gpio_device *gdev)
1050{
1051 int status;
1052
1053 cdev_init(&gdev->chrdev, &gpio_fileops);
1054 gdev->chrdev.owner = THIS_MODULE;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001055 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001056
1057 status = cdev_device_add(&gdev->chrdev, &gdev->dev);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001058 if (status)
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001059 return status;
1060
1061 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
1062 MAJOR(gpio_devt), gdev->id);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001063
1064 status = gpiochip_sysfs_register(gdev);
1065 if (status)
1066 goto err_remove_device;
1067
1068 /* From this point, the .release() function cleans up gpio_device */
1069 gdev->dev.release = gpiodevice_release;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001070 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
1071 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
1072 dev_name(&gdev->dev), gdev->chip->label ? : "generic");
1073
1074 return 0;
1075
1076err_remove_device:
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001077 cdev_device_del(&gdev->chrdev, &gdev->dev);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001078 return status;
1079}
1080
1081static void gpiochip_setup_devs(void)
1082{
1083 struct gpio_device *gdev;
1084 int err;
1085
1086 list_for_each_entry(gdev, &gpio_devices, list) {
1087 err = gpiochip_setup_dev(gdev);
1088 if (err)
1089 pr_err("%s: Failed to initialize gpio device (%d)\n",
1090 dev_name(&gdev->dev), err);
1091 }
1092}
1093
Anton Vorontsov169b6a72008-04-28 02:14:47 -07001094/**
Linus Walleijb08ea352015-12-03 15:14:13 +01001095 * gpiochip_add_data() - register a gpio_chip
David Brownelld2876d02008-02-04 22:28:20 -08001096 * @chip: the chip to register, with chip->base initialized
Thierry Reding950d55f52017-07-24 16:57:22 +02001097 * @data: driver-private data associated with this chip
David Brownelld2876d02008-02-04 22:28:20 -08001098 *
Thierry Reding950d55f52017-07-24 16:57:22 +02001099 * Context: potentially before irqs will work
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001100 *
Linus Walleijb08ea352015-12-03 15:14:13 +01001101 * When gpiochip_add_data() is called very early during boot, so that GPIOs
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08001102 * can be freely used, the chip->parent device must be registered before
David Brownelld8f388d82008-07-25 01:46:07 -07001103 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1104 * for GPIOs will fail rudely.
1105 *
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001106 * gpiochip_add_data() must only be called after gpiolib initialization,
1107 * ie after core_initcall().
1108 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001109 * If chip->base is negative, this requests dynamic assignment of
1110 * a range of valid GPIOs.
Thierry Reding950d55f52017-07-24 16:57:22 +02001111 *
1112 * Returns:
1113 * A negative errno if the chip can't be registered, such as because the
1114 * chip->base is invalid or already associated with a different chip.
1115 * Otherwise it returns zero as a success code.
David Brownelld2876d02008-02-04 22:28:20 -08001116 */
Linus Walleijb08ea352015-12-03 15:14:13 +01001117int gpiochip_add_data(struct gpio_chip *chip, void *data)
David Brownelld2876d02008-02-04 22:28:20 -08001118{
1119 unsigned long flags;
1120 int status = 0;
Linus Walleijff2b1352015-10-20 11:10:38 +02001121 unsigned i;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001122 int base = chip->base;
Linus Walleijff2b1352015-10-20 11:10:38 +02001123 struct gpio_device *gdev;
David Brownelld2876d02008-02-04 22:28:20 -08001124
Linus Walleijff2b1352015-10-20 11:10:38 +02001125 /*
1126 * First: allocate and populate the internal stat container, and
1127 * set up the struct device.
1128 */
Josh Cartwright969f07b2016-02-17 16:44:15 -06001129 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
Linus Walleijff2b1352015-10-20 11:10:38 +02001130 if (!gdev)
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001131 return -ENOMEM;
Linus Walleij3c702e92015-10-21 15:29:53 +02001132 gdev->dev.bus = &gpio_bus_type;
Linus Walleijff2b1352015-10-20 11:10:38 +02001133 gdev->chip = chip;
1134 chip->gpiodev = gdev;
1135 if (chip->parent) {
1136 gdev->dev.parent = chip->parent;
1137 gdev->dev.of_node = chip->parent->of_node;
Thierry Redingacc6e332016-07-05 14:11:14 +02001138 }
1139
Linus Walleijff2b1352015-10-20 11:10:38 +02001140#ifdef CONFIG_OF_GPIO
1141 /* If the gpiochip has an assigned OF node this takes precedence */
Thierry Redingacc6e332016-07-05 14:11:14 +02001142 if (chip->of_node)
1143 gdev->dev.of_node = chip->of_node;
Linus Walleijff2b1352015-10-20 11:10:38 +02001144#endif
Thierry Redingacc6e332016-07-05 14:11:14 +02001145
Linus Walleijff2b1352015-10-20 11:10:38 +02001146 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
1147 if (gdev->id < 0) {
1148 status = gdev->id;
1149 goto err_free_gdev;
1150 }
1151 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
1152 device_initialize(&gdev->dev);
1153 dev_set_drvdata(&gdev->dev, gdev);
1154 if (chip->parent && chip->parent->driver)
1155 gdev->owner = chip->parent->driver->owner;
1156 else if (chip->owner)
1157 /* TODO: remove chip->owner */
1158 gdev->owner = chip->owner;
1159 else
1160 gdev->owner = THIS_MODULE;
David Brownelld2876d02008-02-04 22:28:20 -08001161
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001162 gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001163 if (!gdev->descs) {
Linus Walleijff2b1352015-10-20 11:10:38 +02001164 status = -ENOMEM;
1165 goto err_free_gdev;
1166 }
1167
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001168 if (chip->ngpio == 0) {
1169 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
Linus Walleijff2b1352015-10-20 11:10:38 +02001170 status = -EINVAL;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001171 goto err_free_descs;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001172 }
Linus Walleijdf4878e2016-02-12 14:48:23 +01001173
1174 if (chip->label)
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001175 gdev->label = kstrdup(chip->label, GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001176 else
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001177 gdev->label = kstrdup("unknown", GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001178 if (!gdev->label) {
1179 status = -ENOMEM;
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001180 goto err_free_descs;
Linus Walleijdf4878e2016-02-12 14:48:23 +01001181 }
1182
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001183 gdev->ngpio = chip->ngpio;
Linus Walleij43c54ec2016-02-11 11:37:48 +01001184 gdev->data = data;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001185
David Brownelld2876d02008-02-04 22:28:20 -08001186 spin_lock_irqsave(&gpio_lock, flags);
1187
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001188 /*
1189 * TODO: this allocates a Linux GPIO number base in the global
1190 * GPIO numberspace for this chip. In the long run we want to
1191 * get *rid* of this numberspace and use only descriptors, but
1192 * it may be a pipe dream. It will not happen before we get rid
1193 * of the sysfs interface anyways.
1194 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001195 if (base < 0) {
1196 base = gpiochip_find_base(chip->ngpio);
1197 if (base < 0) {
1198 status = base;
Johan Hovold225fce82015-01-12 17:12:25 +01001199 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001200 goto err_free_label;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001201 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001202 /*
1203 * TODO: it should not be necessary to reflect the assigned
1204 * base outside of the GPIO subsystem. Go over drivers and
1205 * see if anyone makes use of this, else drop this and assign
1206 * a poison instead.
1207 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001208 chip->base = base;
1209 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001210 gdev->base = base;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001211
Linus Walleijff2b1352015-10-20 11:10:38 +02001212 status = gpiodev_add_to_list(gdev);
Johan Hovold05aa5202015-01-12 17:12:26 +01001213 if (status) {
1214 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001215 goto err_free_label;
Johan Hovold05aa5202015-01-12 17:12:26 +01001216 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001217
Linus Walleij545ebd92016-05-30 17:11:59 +02001218 spin_unlock_irqrestore(&gpio_lock, flags);
1219
Linus Walleijff2b1352015-10-20 11:10:38 +02001220 for (i = 0; i < chip->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001221 struct gpio_desc *desc = &gdev->descs[i];
David Brownelld8f388d82008-07-25 01:46:07 -07001222
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001223 desc->gdev = gdev;
Linus Walleij72d32002016-04-28 13:33:59 +02001224 /*
1225 * REVISIT: most hardware initializes GPIOs as inputs
1226 * (often with pullups enabled) so power usage is
1227 * minimized. Linux code should set the gpio direction
1228 * first thing; but until it does, and in case
1229 * chip->get_direction is not set, we may expose the
1230 * wrong direction in sysfs.
Johan Hovold05aa5202015-01-12 17:12:26 +01001231 */
Linus Walleij72d32002016-04-28 13:33:59 +02001232
1233 if (chip->get_direction) {
1234 /*
1235 * If we have .get_direction, set up the initial
1236 * direction flag from the hardware.
1237 */
1238 int dir = chip->get_direction(chip, i);
1239
1240 if (!dir)
1241 set_bit(FLAG_IS_OUT, &desc->flags);
1242 } else if (!chip->direction_input) {
1243 /*
1244 * If the chip lacks the .direction_input callback
1245 * we logically assume all lines are outputs.
1246 */
1247 set_bit(FLAG_IS_OUT, &desc->flags);
1248 }
David Brownelld2876d02008-02-04 22:28:20 -08001249 }
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001250
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301251#ifdef CONFIG_PINCTRL
Linus Walleij20ec3e32016-02-11 11:03:06 +01001252 INIT_LIST_HEAD(&gdev->pin_ranges);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301253#endif
1254
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001255 status = gpiochip_set_desc_names(chip);
1256 if (status)
1257 goto err_remove_from_list;
1258
Mika Westerberg79b804c2016-09-20 15:15:21 +03001259 status = gpiochip_irqchip_init_valid_mask(chip);
1260 if (status)
1261 goto err_remove_from_list;
1262
Tomeu Vizoso28355f82015-07-14 10:29:54 +02001263 status = of_gpiochip_add(chip);
1264 if (status)
1265 goto err_remove_chip;
1266
Mika Westerberg664e3e52014-01-08 12:40:54 +02001267 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001268
Linus Walleij3c702e92015-10-21 15:29:53 +02001269 /*
1270 * By first adding the chardev, and then adding the device,
1271 * we get a device node entry in sysfs under
1272 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1273 * coldplug of device nodes and other udev business.
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001274 * We can do this only if gpiolib has been initialized.
1275 * Otherwise, defer until later.
Linus Walleij3c702e92015-10-21 15:29:53 +02001276 */
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001277 if (gpiolib_initialized) {
1278 status = gpiochip_setup_dev(gdev);
1279 if (status)
1280 goto err_remove_chip;
1281 }
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001282 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001283
Johan Hovold225fce82015-01-12 17:12:25 +01001284err_remove_chip:
1285 acpi_gpiochip_remove(chip);
Johan Hovold6d867502015-05-04 17:23:25 +02001286 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +01001287 of_gpiochip_remove(chip);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001288 gpiochip_irqchip_free_valid_mask(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001289err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +01001290 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001291 list_del(&gdev->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001292 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001293err_free_label:
1294 kfree(gdev->label);
1295err_free_descs:
1296 kfree(gdev->descs);
Linus Walleijff2b1352015-10-20 11:10:38 +02001297err_free_gdev:
1298 ida_simple_remove(&gpio_ida, gdev->id);
David Brownelld2876d02008-02-04 22:28:20 -08001299 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +02001300 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001301 gdev->base, gdev->base + gdev->ngpio - 1,
1302 chip->label ? : "generic");
1303 kfree(gdev);
David Brownelld2876d02008-02-04 22:28:20 -08001304 return status;
1305}
Linus Walleijb08ea352015-12-03 15:14:13 +01001306EXPORT_SYMBOL_GPL(gpiochip_add_data);
David Brownelld2876d02008-02-04 22:28:20 -08001307
1308/**
Linus Walleij43c54ec2016-02-11 11:37:48 +01001309 * gpiochip_get_data() - get per-subdriver data for the chip
Thierry Reding950d55f52017-07-24 16:57:22 +02001310 * @chip: GPIO chip
1311 *
1312 * Returns:
1313 * The per-subdriver data for the chip.
Linus Walleij43c54ec2016-02-11 11:37:48 +01001314 */
1315void *gpiochip_get_data(struct gpio_chip *chip)
1316{
1317 return chip->gpiodev->data;
1318}
1319EXPORT_SYMBOL_GPL(gpiochip_get_data);
1320
1321/**
David Brownelld2876d02008-02-04 22:28:20 -08001322 * gpiochip_remove() - unregister a gpio_chip
1323 * @chip: the chip to unregister
1324 *
1325 * A gpio_chip with any GPIOs still requested may not be removed.
1326 */
abdoulaye berthee1db1702014-07-05 18:28:50 +02001327void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -08001328{
Linus Walleijff2b1352015-10-20 11:10:38 +02001329 struct gpio_device *gdev = chip->gpiodev;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001330 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001331 unsigned long flags;
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001332 unsigned i;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001333 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -08001334
Linus Walleijff2b1352015-10-20 11:10:38 +02001335 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
Linus Walleijafbc4f32016-02-09 13:21:06 +01001336 gpiochip_sysfs_unregister(gdev);
Geert Uytterhoeven5018ada2016-12-19 18:29:23 +01001337 gpiochip_free_hogs(chip);
Bamvor Jian Zhangbd203bd2016-02-20 13:13:19 +08001338 /* Numb the device, cancelling all outstanding operations */
1339 gdev->chip = NULL;
Johan Hovold00acc3d2015-01-12 17:12:27 +01001340 gpiochip_irqchip_remove(chip);
Mika Westerberg6072b9d2014-03-10 14:54:53 +02001341 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +01001342 gpiochip_remove_pin_ranges(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001343 of_gpiochip_remove(chip);
Linus Walleij43c54ec2016-02-11 11:37:48 +01001344 /*
1345 * We accept no more calls into the driver from this point, so
1346 * NULL the driver data pointer
1347 */
1348 gdev->data = NULL;
Anton Vorontsov391c9702010-06-08 07:48:17 -06001349
Johan Hovold6798aca2015-01-12 17:12:28 +01001350 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001351 for (i = 0; i < gdev->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001352 desc = &gdev->descs[i];
Johan Hovoldfab28b82015-05-04 17:10:27 +02001353 if (test_bit(FLAG_REQUESTED, &desc->flags))
1354 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -08001355 }
David Brownelld2876d02008-02-04 22:28:20 -08001356 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001357
Johan Hovoldfab28b82015-05-04 17:10:27 +02001358 if (requested)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001359 dev_crit(&gdev->dev,
Linus Walleij58383c782015-11-04 09:56:26 +01001360 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +02001361
Linus Walleijff2b1352015-10-20 11:10:38 +02001362 /*
1363 * The gpiochip side puts its use of the device to rest here:
1364 * if there are no userspace clients, the chardev and device will
1365 * be removed, else it will be dangling until the last user is
1366 * gone.
1367 */
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001368 cdev_device_del(&gdev->chrdev, &gdev->dev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001369 put_device(&gdev->dev);
David Brownelld2876d02008-02-04 22:28:20 -08001370}
1371EXPORT_SYMBOL_GPL(gpiochip_remove);
1372
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301373static void devm_gpio_chip_release(struct device *dev, void *res)
1374{
1375 struct gpio_chip *chip = *(struct gpio_chip **)res;
1376
1377 gpiochip_remove(chip);
1378}
1379
1380static int devm_gpio_chip_match(struct device *dev, void *res, void *data)
1381
1382{
1383 struct gpio_chip **r = res;
1384
1385 if (!r || !*r) {
1386 WARN_ON(!r || !*r);
1387 return 0;
1388 }
1389
1390 return *r == data;
1391}
1392
1393/**
1394 * devm_gpiochip_add_data() - Resource manager piochip_add_data()
1395 * @dev: the device pointer on which irq_chip belongs to.
1396 * @chip: the chip to register, with chip->base initialized
Thierry Reding950d55f52017-07-24 16:57:22 +02001397 * @data: driver-private data associated with this chip
1398 *
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301399 * Context: potentially before irqs will work
1400 *
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301401 * The gpio chip automatically be released when the device is unbound.
Thierry Reding950d55f52017-07-24 16:57:22 +02001402 *
1403 * Returns:
1404 * A negative errno if the chip can't be registered, such as because the
1405 * chip->base is invalid or already associated with a different chip.
1406 * Otherwise it returns zero as a success code.
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301407 */
1408int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
1409 void *data)
1410{
1411 struct gpio_chip **ptr;
1412 int ret;
1413
1414 ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
1415 GFP_KERNEL);
1416 if (!ptr)
1417 return -ENOMEM;
1418
1419 ret = gpiochip_add_data(chip, data);
1420 if (ret < 0) {
1421 devres_free(ptr);
1422 return ret;
1423 }
1424
1425 *ptr = chip;
1426 devres_add(dev, ptr);
1427
1428 return 0;
1429}
1430EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
1431
1432/**
1433 * devm_gpiochip_remove() - Resource manager of gpiochip_remove()
1434 * @dev: device for which which resource was allocated
1435 * @chip: the chip to remove
1436 *
1437 * A gpio_chip with any GPIOs still requested may not be removed.
1438 */
1439void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip)
1440{
1441 int ret;
1442
1443 ret = devres_release(dev, devm_gpio_chip_release,
1444 devm_gpio_chip_match, chip);
Christophe JAILLET3988d662017-01-27 12:56:42 +01001445 WARN_ON(ret);
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301446}
1447EXPORT_SYMBOL_GPL(devm_gpiochip_remove);
1448
Grant Likely594fa262010-06-08 07:48:16 -06001449/**
1450 * gpiochip_find() - iterator for locating a specific gpio_chip
1451 * @data: data to pass to match function
Thierry Reding950d55f52017-07-24 16:57:22 +02001452 * @match: Callback function to check gpio_chip
Grant Likely594fa262010-06-08 07:48:16 -06001453 *
1454 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1455 * determined by a user supplied @match callback. The callback should return
1456 * 0 if the device doesn't match and non-zero if it does. If the callback is
1457 * non-zero, this function will return to the caller and not iterate over any
1458 * more gpio_chips.
1459 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001460struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001461 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001462 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001463{
Linus Walleijff2b1352015-10-20 11:10:38 +02001464 struct gpio_device *gdev;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001465 struct gpio_chip *chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -06001466 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001467
1468 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001469 list_for_each_entry(gdev, &gpio_devices, list)
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001470 if (gdev->chip && match(gdev->chip, data)) {
1471 chip = gdev->chip;
Grant Likely594fa262010-06-08 07:48:16 -06001472 break;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001473 }
Linus Walleijff2b1352015-10-20 11:10:38 +02001474
Grant Likely594fa262010-06-08 07:48:16 -06001475 spin_unlock_irqrestore(&gpio_lock, flags);
1476
1477 return chip;
1478}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001479EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001480
Alexandre Courbot79697ef2013-11-16 21:39:32 +09001481static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1482{
1483 const char *name = data;
1484
1485 return !strcmp(chip->label, name);
1486}
1487
1488static struct gpio_chip *find_chip_by_name(const char *name)
1489{
1490 return gpiochip_find((void *)name, gpiochip_match_name);
1491}
1492
Linus Walleij14250522014-03-25 10:40:18 +01001493#ifdef CONFIG_GPIOLIB_IRQCHIP
1494
1495/*
1496 * The following is irqchip helper code for gpiochips.
1497 */
1498
Mika Westerberg79b804c2016-09-20 15:15:21 +03001499static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1500{
Mika Westerberg79b804c2016-09-20 15:15:21 +03001501 if (!gpiochip->irq_need_valid_mask)
1502 return 0;
1503
1504 gpiochip->irq_valid_mask = kcalloc(BITS_TO_LONGS(gpiochip->ngpio),
1505 sizeof(long), GFP_KERNEL);
1506 if (!gpiochip->irq_valid_mask)
1507 return -ENOMEM;
1508
1509 /* Assume by default all GPIOs are valid */
Andy Shevchenko923a6542017-05-25 16:08:38 +03001510 bitmap_fill(gpiochip->irq_valid_mask, gpiochip->ngpio);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001511
1512 return 0;
1513}
1514
1515static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1516{
1517 kfree(gpiochip->irq_valid_mask);
1518 gpiochip->irq_valid_mask = NULL;
1519}
1520
1521static bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
1522 unsigned int offset)
1523{
1524 /* No mask means all valid */
1525 if (likely(!gpiochip->irq_valid_mask))
1526 return true;
1527 return test_bit(offset, gpiochip->irq_valid_mask);
1528}
1529
Linus Walleij14250522014-03-25 10:40:18 +01001530/**
Linus Walleijd245b3f2016-11-24 10:57:25 +01001531 * gpiochip_set_cascaded_irqchip() - connects a cascaded irqchip to a gpiochip
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001532 * @gpiochip: the gpiochip to set the irqchip chain to
1533 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01001534 * @parent_irq: the irq number corresponding to the parent IRQ for this
1535 * chained irqchip
1536 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001537 * coming out of the gpiochip. If the interrupt is nested rather than
1538 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +01001539 */
Linus Walleijd245b3f2016-11-24 10:57:25 +01001540static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gpiochip,
1541 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001542 unsigned int parent_irq,
Linus Walleijd245b3f2016-11-24 10:57:25 +01001543 irq_flow_handler_t parent_handler)
Linus Walleij14250522014-03-25 10:40:18 +01001544{
Linus Walleij83141a72014-09-26 13:50:12 +02001545 unsigned int offset;
1546
Linus Walleij83141a72014-09-26 13:50:12 +02001547 if (!gpiochip->irqdomain) {
1548 chip_err(gpiochip, "called %s before setting up irqchip\n",
1549 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +02001550 return;
1551 }
1552
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001553 if (parent_handler) {
1554 if (gpiochip->can_sleep) {
1555 chip_err(gpiochip,
1556 "you cannot have chained interrupts on a "
1557 "chip that may sleep\n");
1558 return;
1559 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001560 /*
1561 * The parent irqchip is already using the chip_data for this
1562 * irqchip, so our callbacks simply use the handler_data.
1563 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +02001564 irq_set_chained_handler_and_data(parent_irq, parent_handler,
1565 gpiochip);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001566
Linus Walleijd245b3f2016-11-24 10:57:25 +01001567 gpiochip->irq_chained_parent = parent_irq;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001568 }
Linus Walleij83141a72014-09-26 13:50:12 +02001569
1570 /* Set the parent IRQ for all affected IRQs */
Mika Westerberg79b804c2016-09-20 15:15:21 +03001571 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1572 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1573 continue;
Linus Walleij83141a72014-09-26 13:50:12 +02001574 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
1575 parent_irq);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001576 }
Linus Walleij14250522014-03-25 10:40:18 +01001577}
Linus Walleijd245b3f2016-11-24 10:57:25 +01001578
1579/**
1580 * gpiochip_set_chained_irqchip() - connects a chained irqchip to a gpiochip
1581 * @gpiochip: the gpiochip to set the irqchip chain to
1582 * @irqchip: the irqchip to chain to the gpiochip
1583 * @parent_irq: the irq number corresponding to the parent IRQ for this
1584 * chained irqchip
1585 * @parent_handler: the parent interrupt handler for the accumulated IRQ
1586 * coming out of the gpiochip. If the interrupt is nested rather than
1587 * cascaded, pass NULL in this handler argument
1588 */
1589void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
1590 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001591 unsigned int parent_irq,
Linus Walleijd245b3f2016-11-24 10:57:25 +01001592 irq_flow_handler_t parent_handler)
1593{
1594 gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
1595 parent_handler);
1596}
Linus Walleij14250522014-03-25 10:40:18 +01001597EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
1598
1599/**
Linus Walleijd245b3f2016-11-24 10:57:25 +01001600 * gpiochip_set_nested_irqchip() - connects a nested irqchip to a gpiochip
1601 * @gpiochip: the gpiochip to set the irqchip nested handler to
1602 * @irqchip: the irqchip to nest to the gpiochip
1603 * @parent_irq: the irq number corresponding to the parent IRQ for this
1604 * nested irqchip
1605 */
1606void gpiochip_set_nested_irqchip(struct gpio_chip *gpiochip,
1607 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001608 unsigned int parent_irq)
Linus Walleijd245b3f2016-11-24 10:57:25 +01001609{
1610 if (!gpiochip->irq_nested) {
1611 chip_err(gpiochip, "tried to nest a chained gpiochip\n");
1612 return;
1613 }
1614 gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
1615 NULL);
1616}
1617EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip);
1618
1619/**
Linus Walleij14250522014-03-25 10:40:18 +01001620 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1621 * @d: the irqdomain used by this irqchip
1622 * @irq: the global irq number used by this GPIO irqchip irq
1623 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1624 *
1625 * This function will set up the mapping for a certain IRQ line on a
1626 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1627 * stored inside the gpiochip.
1628 */
1629static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1630 irq_hw_number_t hwirq)
1631{
1632 struct gpio_chip *chip = d->host_data;
1633
Grygorii Strashkodc749a02017-07-21 11:49:00 -05001634 if (!gpiochip_irqchip_irq_valid(chip, hwirq))
1635 return -ENXIO;
1636
Linus Walleij14250522014-03-25 10:40:18 +01001637 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001638 /*
1639 * This lock class tells lockdep that GPIO irqs are in a different
1640 * category than their parents, so it won't report false recursion.
1641 */
1642 irq_set_lockdep_class(irq, chip->lock_key);
Linus Walleij7633fb92014-04-09 13:20:38 +02001643 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
Linus Walleijd245b3f2016-11-24 10:57:25 +01001644 /* Chips that use nested thread handlers have them marked */
1645 if (chip->irq_nested)
Linus Walleij1c8732b2014-04-09 13:34:39 +02001646 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +01001647 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -05001648
Linus Walleij1333b902014-04-23 16:45:12 +02001649 /*
1650 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1651 * is passed as default type.
1652 */
1653 if (chip->irq_default_type != IRQ_TYPE_NONE)
1654 irq_set_irq_type(irq, chip->irq_default_type);
Linus Walleij14250522014-03-25 10:40:18 +01001655
1656 return 0;
1657}
1658
Linus Walleijc3626fd2014-03-28 20:42:01 +01001659static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
1660{
Linus Walleij1c8732b2014-04-09 13:34:39 +02001661 struct gpio_chip *chip = d->host_data;
1662
Linus Walleijd245b3f2016-11-24 10:57:25 +01001663 if (chip->irq_nested)
Linus Walleij1c8732b2014-04-09 13:34:39 +02001664 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001665 irq_set_chip_and_handler(irq, NULL, NULL);
1666 irq_set_chip_data(irq, NULL);
1667}
1668
Linus Walleij14250522014-03-25 10:40:18 +01001669static const struct irq_domain_ops gpiochip_domain_ops = {
1670 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +01001671 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +01001672 /* Virtually all GPIO irqchips are twocell:ed */
1673 .xlate = irq_domain_xlate_twocell,
1674};
1675
1676static int gpiochip_irq_reqres(struct irq_data *d)
1677{
1678 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1679
Linus Walleijff2b1352015-10-20 11:10:38 +02001680 if (!try_module_get(chip->gpiodev->owner))
Grygorii Strashko5b76e792015-06-25 20:30:50 +03001681 return -ENODEV;
1682
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001683 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +01001684 chip_err(chip,
1685 "unable to lock HW IRQ %lu for IRQ\n",
1686 d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001687 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001688 return -EINVAL;
1689 }
1690 return 0;
1691}
1692
1693static void gpiochip_irq_relres(struct irq_data *d)
1694{
1695 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1696
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001697 gpiochip_unlock_as_irq(chip, d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001698 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001699}
1700
1701static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
1702{
Grygorii Strashkodc749a02017-07-21 11:49:00 -05001703 if (!gpiochip_irqchip_irq_valid(chip, offset))
1704 return -ENXIO;
1705 return irq_create_mapping(chip->irqdomain, offset);
Linus Walleij14250522014-03-25 10:40:18 +01001706}
1707
1708/**
1709 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1710 * @gpiochip: the gpiochip to remove the irqchip from
1711 *
1712 * This is called only from gpiochip_remove()
1713 */
1714static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
1715{
Linus Walleijc3626fd2014-03-28 20:42:01 +01001716 unsigned int offset;
1717
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001718 acpi_gpiochip_free_interrupts(gpiochip);
1719
Linus Walleijd245b3f2016-11-24 10:57:25 +01001720 if (gpiochip->irq_chained_parent) {
1721 irq_set_chained_handler(gpiochip->irq_chained_parent, NULL);
1722 irq_set_handler_data(gpiochip->irq_chained_parent, NULL);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001723 }
1724
Linus Walleijc3626fd2014-03-28 20:42:01 +01001725 /* Remove all IRQ mappings and delete the domain */
1726 if (gpiochip->irqdomain) {
Mika Westerberg79b804c2016-09-20 15:15:21 +03001727 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1728 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1729 continue;
Grygorii Strashkoe3893382014-09-25 19:09:23 +03001730 irq_dispose_mapping(
1731 irq_find_mapping(gpiochip->irqdomain, offset));
Mika Westerberg79b804c2016-09-20 15:15:21 +03001732 }
Linus Walleij14250522014-03-25 10:40:18 +01001733 irq_domain_remove(gpiochip->irqdomain);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001734 }
Linus Walleij14250522014-03-25 10:40:18 +01001735
1736 if (gpiochip->irqchip) {
1737 gpiochip->irqchip->irq_request_resources = NULL;
1738 gpiochip->irqchip->irq_release_resources = NULL;
1739 gpiochip->irqchip = NULL;
1740 }
Mika Westerberg79b804c2016-09-20 15:15:21 +03001741
1742 gpiochip_irqchip_free_valid_mask(gpiochip);
Linus Walleij14250522014-03-25 10:40:18 +01001743}
1744
1745/**
Linus Walleij739e6f52017-01-11 13:37:07 +01001746 * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01001747 * @gpiochip: the gpiochip to add the irqchip to
1748 * @irqchip: the irqchip to add to the gpiochip
1749 * @first_irq: if not dynamically assigned, the base (first) IRQ to
1750 * allocate gpiochip irqs from
1751 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +02001752 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
1753 * to have the core avoid setting up any default type in the hardware.
Linus Walleijd245b3f2016-11-24 10:57:25 +01001754 * @nested: whether this is a nested irqchip calling handle_nested_irq()
1755 * in its IRQ handler
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001756 * @lock_key: lockdep class
Linus Walleij14250522014-03-25 10:40:18 +01001757 *
1758 * This function closely associates a certain irqchip with a certain
1759 * gpiochip, providing an irq domain to translate the local IRQs to
1760 * global irqs in the gpiolib core, and making sure that the gpiochip
1761 * is passed as chip data to all related functions. Driver callbacks
Linus Walleij09dd5f92015-12-07 15:31:58 +01001762 * need to use gpiochip_get_data() to get their local state containers back
Linus Walleij14250522014-03-25 10:40:18 +01001763 * from the gpiochip passed as chip data. An irqdomain will be stored
1764 * in the gpiochip that shall be used by the driver to handle IRQ number
1765 * translation. The gpiochip will need to be initialized and registered
1766 * before calling this function.
1767 *
Linus Walleijc3626fd2014-03-28 20:42:01 +01001768 * This function will handle two cell:ed simple IRQs and assumes all
1769 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +01001770 * need to be open coded.
1771 */
Linus Walleij739e6f52017-01-11 13:37:07 +01001772int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
1773 struct irq_chip *irqchip,
1774 unsigned int first_irq,
1775 irq_flow_handler_t handler,
1776 unsigned int type,
1777 bool nested,
1778 struct lock_class_key *lock_key)
Linus Walleij14250522014-03-25 10:40:18 +01001779{
1780 struct device_node *of_node;
Linus Walleij14250522014-03-25 10:40:18 +01001781
1782 if (!gpiochip || !irqchip)
1783 return -EINVAL;
1784
Linus Walleij58383c782015-11-04 09:56:26 +01001785 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +01001786 pr_err("missing gpiochip .dev parent pointer\n");
1787 return -EINVAL;
1788 }
Linus Walleijd245b3f2016-11-24 10:57:25 +01001789 gpiochip->irq_nested = nested;
Linus Walleij58383c782015-11-04 09:56:26 +01001790 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +01001791#ifdef CONFIG_OF_GPIO
1792 /*
Colin Cronin20a8a962015-05-18 11:41:43 -07001793 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08001794 * FIXME: get rid of this and use gpiochip->parent->of_node
1795 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +01001796 */
1797 if (gpiochip->of_node)
1798 of_node = gpiochip->of_node;
1799#endif
Marc Zyngier332e99d2016-09-07 09:12:11 +01001800 /*
Mika Westerberg0a1e0052016-09-12 14:29:51 +03001801 * Specifying a default trigger is a terrible idea if DT or ACPI is
Marc Zyngier332e99d2016-09-07 09:12:11 +01001802 * used to configure the interrupts, as you may end-up with
1803 * conflicting triggers. Tell the user, and reset to NONE.
1804 */
1805 if (WARN(of_node && type != IRQ_TYPE_NONE,
Rob Herring7eb6ce22017-07-18 16:43:03 -05001806 "%pOF: Ignoring %d default trigger\n", of_node, type))
Marc Zyngier332e99d2016-09-07 09:12:11 +01001807 type = IRQ_TYPE_NONE;
Mika Westerberg0a1e0052016-09-12 14:29:51 +03001808 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
1809 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1810 "Ignoring %d default trigger\n", type);
1811 type = IRQ_TYPE_NONE;
1812 }
Marc Zyngier332e99d2016-09-07 09:12:11 +01001813
Linus Walleij14250522014-03-25 10:40:18 +01001814 gpiochip->irqchip = irqchip;
1815 gpiochip->irq_handler = handler;
1816 gpiochip->irq_default_type = type;
1817 gpiochip->to_irq = gpiochip_to_irq;
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001818 gpiochip->lock_key = lock_key;
Linus Walleij14250522014-03-25 10:40:18 +01001819 gpiochip->irqdomain = irq_domain_add_simple(of_node,
1820 gpiochip->ngpio, first_irq,
1821 &gpiochip_domain_ops, gpiochip);
1822 if (!gpiochip->irqdomain) {
1823 gpiochip->irqchip = NULL;
1824 return -EINVAL;
1825 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +02001826
1827 /*
1828 * It is possible for a driver to override this, but only if the
1829 * alternative functions are both implemented.
1830 */
1831 if (!irqchip->irq_request_resources &&
1832 !irqchip->irq_release_resources) {
1833 irqchip->irq_request_resources = gpiochip_irq_reqres;
1834 irqchip->irq_release_resources = gpiochip_irq_relres;
1835 }
Linus Walleij14250522014-03-25 10:40:18 +01001836
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001837 acpi_gpiochip_request_interrupts(gpiochip);
1838
Linus Walleij14250522014-03-25 10:40:18 +01001839 return 0;
1840}
Linus Walleij739e6f52017-01-11 13:37:07 +01001841EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key);
Linus Walleij14250522014-03-25 10:40:18 +01001842
1843#else /* CONFIG_GPIOLIB_IRQCHIP */
1844
1845static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
Mika Westerberg79b804c2016-09-20 15:15:21 +03001846static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1847{
1848 return 0;
1849}
1850static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1851{ }
Linus Walleij14250522014-03-25 10:40:18 +01001852
1853#endif /* CONFIG_GPIOLIB_IRQCHIP */
1854
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001855/**
1856 * gpiochip_generic_request() - request the gpio function for a pin
1857 * @chip: the gpiochip owning the GPIO
1858 * @offset: the offset of the GPIO to request for GPIO function
1859 */
1860int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
1861{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001862 return pinctrl_request_gpio(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001863}
1864EXPORT_SYMBOL_GPL(gpiochip_generic_request);
1865
1866/**
1867 * gpiochip_generic_free() - free the gpio function from a pin
1868 * @chip: the gpiochip to request the gpio function for
1869 * @offset: the offset of the GPIO to free from GPIO function
1870 */
1871void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
1872{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001873 pinctrl_free_gpio(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001874}
1875EXPORT_SYMBOL_GPL(gpiochip_generic_free);
1876
Mika Westerberg2956b5d2017-01-23 15:34:34 +03001877/**
1878 * gpiochip_generic_config() - apply configuration for a pin
1879 * @chip: the gpiochip owning the GPIO
1880 * @offset: the offset of the GPIO to apply the configuration
1881 * @config: the configuration to be applied
1882 */
1883int gpiochip_generic_config(struct gpio_chip *chip, unsigned offset,
1884 unsigned long config)
1885{
1886 return pinctrl_gpio_set_config(chip->gpiodev->base + offset, config);
1887}
1888EXPORT_SYMBOL_GPL(gpiochip_generic_config);
1889
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301890#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01001891
Linus Walleij3f0f8672012-11-20 12:40:15 +01001892/**
Christian Ruppert586a87e2013-10-15 15:37:54 +02001893 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1894 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +02001895 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +02001896 * @gpio_offset: the start offset in the current gpio_chip number space
1897 * @pin_group: name of the pin group inside the pin controller
1898 */
1899int gpiochip_add_pingroup_range(struct gpio_chip *chip,
1900 struct pinctrl_dev *pctldev,
1901 unsigned int gpio_offset, const char *pin_group)
1902{
1903 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001904 struct gpio_device *gdev = chip->gpiodev;
Christian Ruppert586a87e2013-10-15 15:37:54 +02001905 int ret;
1906
1907 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1908 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001909 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +02001910 return -ENOMEM;
1911 }
1912
1913 /* Use local offset as range ID */
1914 pin_range->range.id = gpio_offset;
1915 pin_range->range.gc = chip;
1916 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001917 pin_range->range.base = gdev->base + gpio_offset;
Christian Ruppert586a87e2013-10-15 15:37:54 +02001918 pin_range->pctldev = pctldev;
1919
1920 ret = pinctrl_get_group_pins(pctldev, pin_group,
1921 &pin_range->range.pins,
1922 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001923 if (ret < 0) {
1924 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001925 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001926 }
Christian Ruppert586a87e2013-10-15 15:37:54 +02001927
1928 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1929
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001930 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1931 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +02001932 pinctrl_dev_get_devname(pctldev), pin_group);
1933
Linus Walleij20ec3e32016-02-11 11:03:06 +01001934 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001935
1936 return 0;
1937}
1938EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1939
1940/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01001941 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1942 * @chip: the gpiochip to add the range for
Thierry Reding950d55f52017-07-24 16:57:22 +02001943 * @pinctl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01001944 * @gpio_offset: the start offset in the current gpio_chip number space
1945 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01001946 * @npins: the number of pins from the offset of each pin space (GPIO and
1947 * pin controller) to accumulate in this range
Thierry Reding950d55f52017-07-24 16:57:22 +02001948 *
1949 * Returns:
1950 * 0 on success, or a negative error-code on failure.
Linus Walleij3f0f8672012-11-20 12:40:15 +01001951 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001952int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01001953 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01001954 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301955{
1956 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001957 struct gpio_device *gdev = chip->gpiodev;
Axel Linb4d4b1f2012-11-21 14:33:56 +08001958 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301959
Linus Walleij3f0f8672012-11-20 12:40:15 +01001960 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301961 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001962 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001963 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301964 }
1965
Linus Walleij3f0f8672012-11-20 12:40:15 +01001966 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01001967 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001968 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301969 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001970 pin_range->range.base = gdev->base + gpio_offset;
Linus Walleij316511c2012-11-21 08:48:09 +01001971 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301972 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01001973 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301974 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01001975 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08001976 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001977 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +01001978 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08001979 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001980 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001981 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1982 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +01001983 pinctl_name,
1984 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301985
Linus Walleij20ec3e32016-02-11 11:03:06 +01001986 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001987
1988 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301989}
Linus Walleij165adc92012-11-06 14:49:39 +01001990EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301991
Linus Walleij3f0f8672012-11-20 12:40:15 +01001992/**
1993 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1994 * @chip: the chip to remove all the mappings for
1995 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301996void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1997{
1998 struct gpio_pin_range *pin_range, *tmp;
Linus Walleij20ec3e32016-02-11 11:03:06 +01001999 struct gpio_device *gdev = chip->gpiodev;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302000
Linus Walleij20ec3e32016-02-11 11:03:06 +01002001 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302002 list_del(&pin_range->node);
2003 pinctrl_remove_gpio_range(pin_range->pctldev,
2004 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01002005 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302006 }
2007}
Linus Walleij165adc92012-11-06 14:49:39 +01002008EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
2009
2010#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302011
David Brownelld2876d02008-02-04 22:28:20 -08002012/* These "optional" allocation calls help prevent drivers from stomping
2013 * on each other, and help provide better diagnostics in debugfs.
2014 * They're called even less than the "set direction" calls.
2015 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002016static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08002017{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002018 struct gpio_chip *chip = desc->gdev->chip;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002019 int status;
David Brownelld2876d02008-02-04 22:28:20 -08002020 unsigned long flags;
2021
2022 spin_lock_irqsave(&gpio_lock, flags);
2023
David Brownelld2876d02008-02-04 22:28:20 -08002024 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07002025 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08002026 */
2027
2028 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
2029 desc_set_label(desc, label ? : "?");
2030 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07002031 } else {
David Brownelld2876d02008-02-04 22:28:20 -08002032 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -08002033 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07002034 }
2035
2036 if (chip->request) {
2037 /* chip->request may sleep */
2038 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002039 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07002040 spin_lock_irqsave(&gpio_lock, flags);
2041
2042 if (status < 0) {
2043 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -07002044 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03002045 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07002046 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07002047 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03002048 if (chip->get_direction) {
2049 /* chip->get_direction may sleep */
2050 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002051 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03002052 spin_lock_irqsave(&gpio_lock, flags);
2053 }
David Brownelld2876d02008-02-04 22:28:20 -08002054done:
Mika Westerberg77c2d792014-03-10 14:54:50 +02002055 spin_unlock_irqrestore(&gpio_lock, flags);
2056 return status;
2057}
2058
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002059/*
2060 * This descriptor validation needs to be inserted verbatim into each
2061 * function taking a descriptor, so we need to use a preprocessor
Linus Walleij54d77192016-05-30 16:48:39 +02002062 * macro to avoid endless duplication. If the desc is NULL it is an
2063 * optional GPIO and calls should just bail out.
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002064 */
2065#define VALIDATE_DESC(desc) do { \
Linus Walleij54d77192016-05-30 16:48:39 +02002066 if (!desc) \
2067 return 0; \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002068 if (IS_ERR(desc)) { \
2069 pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
2070 return PTR_ERR(desc); \
2071 } \
Linus Walleij54d77192016-05-30 16:48:39 +02002072 if (!desc->gdev) { \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002073 pr_warn("%s: invalid GPIO (no device)\n", __func__); \
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002074 return -EINVAL; \
2075 } \
2076 if ( !desc->gdev->chip ) { \
2077 dev_warn(&desc->gdev->dev, \
2078 "%s: backing chip is gone\n", __func__); \
2079 return 0; \
2080 } } while (0)
2081
2082#define VALIDATE_DESC_VOID(desc) do { \
Linus Walleij54d77192016-05-30 16:48:39 +02002083 if (!desc) \
2084 return; \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002085 if (IS_ERR(desc)) { \
2086 pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
2087 return; \
2088 } \
Linus Walleij54d77192016-05-30 16:48:39 +02002089 if (!desc->gdev) { \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002090 pr_warn("%s: invalid GPIO (no device)\n", __func__); \
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002091 return; \
2092 } \
2093 if (!desc->gdev->chip) { \
2094 dev_warn(&desc->gdev->dev, \
2095 "%s: backing chip is gone\n", __func__); \
2096 return; \
2097 } } while (0)
2098
2099
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002100int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002101{
2102 int status = -EPROBE_DEFER;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002103 struct gpio_device *gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002104
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002105 VALIDATE_DESC(desc);
2106 gdev = desc->gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002107
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002108 if (try_module_get(gdev->owner)) {
Linus Walleijfac9d882017-09-26 20:58:28 +02002109 status = gpiod_request_commit(desc, label);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002110 if (status < 0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002111 module_put(gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002112 else
2113 get_device(&gdev->dev);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002114 }
2115
David Brownelld2876d02008-02-04 22:28:20 -08002116 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +02002117 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002118
David Brownelld2876d02008-02-04 22:28:20 -08002119 return status;
2120}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002121
Linus Walleijfac9d882017-09-26 20:58:28 +02002122static bool gpiod_free_commit(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002123{
Mika Westerberg77c2d792014-03-10 14:54:50 +02002124 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08002125 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07002126 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002127
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07002128 might_sleep();
2129
Alexandre Courbot372e7222013-02-03 01:29:29 +09002130 gpiod_unexport(desc);
David Brownelld8f388d82008-07-25 01:46:07 -07002131
David Brownelld2876d02008-02-04 22:28:20 -08002132 spin_lock_irqsave(&gpio_lock, flags);
2133
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002134 chip = desc->gdev->chip;
David Brownell35e8bb52008-10-15 22:03:16 -07002135 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
2136 if (chip->free) {
2137 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07002138 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002139 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07002140 spin_lock_irqsave(&gpio_lock, flags);
2141 }
David Brownelld2876d02008-02-04 22:28:20 -08002142 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08002143 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07002144 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302145 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302146 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06002147 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002148 ret = true;
2149 }
David Brownelld2876d02008-02-04 22:28:20 -08002150
2151 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002152 return ret;
2153}
2154
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002155void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002156{
Linus Walleijfac9d882017-09-26 20:58:28 +02002157 if (desc && desc->gdev && gpiod_free_commit(desc)) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002158 module_put(desc->gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002159 put_device(&desc->gdev->dev);
2160 } else {
Mika Westerberg77c2d792014-03-10 14:54:50 +02002161 WARN_ON(extra_checks);
Linus Walleij33a68e82016-02-11 10:28:44 +01002162 }
David Brownelld2876d02008-02-04 22:28:20 -08002163}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002164
David Brownelld2876d02008-02-04 22:28:20 -08002165/**
2166 * gpiochip_is_requested - return string iff signal was requested
2167 * @chip: controller managing the signal
2168 * @offset: of signal within controller's 0..(ngpio - 1) range
2169 *
2170 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09002171 * The string returned is the label passed to gpio_request(); if none has been
2172 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08002173 *
2174 * This function is for use by GPIO controller drivers. The label can
2175 * help with diagnostics, and knowing that the signal is used as a GPIO
2176 * can help avoid accidentally multiplexing it to another controller.
2177 */
2178const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
2179{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002180 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08002181
Dirk Behme48b59532015-08-18 18:02:32 +02002182 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08002183 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002184
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002185 desc = &chip->gpiodev->descs[offset];
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002186
Alexandre Courbot372e7222013-02-03 01:29:29 +09002187 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08002188 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002189 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08002190}
2191EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2192
Mika Westerberg77c2d792014-03-10 14:54:50 +02002193/**
2194 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
Thierry Reding950d55f52017-07-24 16:57:22 +02002195 * @chip: GPIO chip
2196 * @hwnum: hardware number of the GPIO for which to request the descriptor
Mika Westerberg77c2d792014-03-10 14:54:50 +02002197 * @label: label for the GPIO
2198 *
2199 * Function allows GPIO chip drivers to request and use their own GPIO
2200 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2201 * function will not increase reference count of the GPIO chip module. This
2202 * allows the GPIO chip module to be unloaded as needed (we assume that the
2203 * GPIO chip driver handles freeing the GPIOs it has requested).
Thierry Reding950d55f52017-07-24 16:57:22 +02002204 *
2205 * Returns:
2206 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2207 * code on failure.
Mika Westerberg77c2d792014-03-10 14:54:50 +02002208 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002209struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
2210 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002211{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002212 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
2213 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002214
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002215 if (IS_ERR(desc)) {
2216 chip_err(chip, "failed to get GPIO descriptor\n");
2217 return desc;
2218 }
2219
Linus Walleijfac9d882017-09-26 20:58:28 +02002220 err = gpiod_request_commit(desc, label);
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002221 if (err < 0)
2222 return ERR_PTR(err);
2223
2224 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002225}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002226EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002227
2228/**
2229 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2230 * @desc: GPIO descriptor to free
2231 *
2232 * Function frees the given GPIO requested previously with
2233 * gpiochip_request_own_desc().
2234 */
2235void gpiochip_free_own_desc(struct gpio_desc *desc)
2236{
2237 if (desc)
Linus Walleijfac9d882017-09-26 20:58:28 +02002238 gpiod_free_commit(desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002239}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002240EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08002241
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002242/*
2243 * Drivers MUST set GPIO direction before making get/set calls. In
David Brownelld2876d02008-02-04 22:28:20 -08002244 * some cases this is done in early boot, before IRQs are enabled.
2245 *
2246 * As a rule these aren't called more than once (except for drivers
2247 * using the open-drain emulation idiom) so these are natural places
2248 * to accumulate extra debugging checks. Note that we can't (yet)
2249 * rely on gpio_request() having been called beforehand.
2250 */
2251
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002252/**
2253 * gpiod_direction_input - set the GPIO direction to input
2254 * @desc: GPIO to set to input
2255 *
2256 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2257 * be called safely on it.
2258 *
2259 * Return 0 in case of success, else an error code.
2260 */
2261int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002262{
David Brownelld2876d02008-02-04 22:28:20 -08002263 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002264 int status = -EINVAL;
2265
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002266 VALIDATE_DESC(desc);
2267 chip = desc->gdev->chip;
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002268
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002269 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01002270 gpiod_warn(desc,
2271 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02002272 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002273 return -EIO;
2274 }
2275
Alexandre Courbotd82da792014-07-22 16:17:43 +09002276 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -08002277 if (status == 0)
2278 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002279
Alexandre Courbot372e7222013-02-03 01:29:29 +09002280 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09002281
David Brownelld2876d02008-02-04 22:28:20 -08002282 return status;
2283}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002284EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002285
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002286static int gpio_set_drive_single_ended(struct gpio_chip *gc, unsigned offset,
2287 enum pin_config_param mode)
2288{
2289 unsigned long config = { PIN_CONF_PACKED(mode, 0) };
2290
2291 return gc->set_config ? gc->set_config(gc, offset, config) : -ENOTSUPP;
2292}
2293
Linus Walleijfac9d882017-09-26 20:58:28 +02002294static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08002295{
Linus Walleijc663e5f2016-03-22 10:51:16 +01002296 struct gpio_chip *gc = desc->gdev->chip;
Linus Walleijad177312016-11-13 23:02:44 +01002297 int val = !!value;
Linus Walleijc663e5f2016-03-22 10:51:16 +01002298 int ret;
David Brownelld2876d02008-02-04 22:28:20 -08002299
Linus Walleijc663e5f2016-03-22 10:51:16 +01002300 if (!gc->set || !gc->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01002301 gpiod_warn(desc,
2302 "%s: missing set() or direction_output() operations\n",
2303 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002304 return -EIO;
2305 }
2306
Linus Walleijad177312016-11-13 23:02:44 +01002307 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
Linus Walleijc663e5f2016-03-22 10:51:16 +01002308 if (!ret)
David Brownelld2876d02008-02-04 22:28:20 -08002309 set_bit(FLAG_IS_OUT, &desc->flags);
Linus Walleijad177312016-11-13 23:02:44 +01002310 trace_gpio_value(desc_to_gpio(desc), 0, val);
Linus Walleijc663e5f2016-03-22 10:51:16 +01002311 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2312 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08002313}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002314
2315/**
2316 * gpiod_direction_output_raw - set the GPIO direction to output
2317 * @desc: GPIO to set to output
2318 * @value: initial output value of the GPIO
2319 *
2320 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2321 * be called safely on it. The initial value of the output must be specified
2322 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2323 *
2324 * Return 0 in case of success, else an error code.
2325 */
2326int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2327{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002328 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02002329 return gpiod_direction_output_raw_commit(desc, value);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002330}
2331EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2332
2333/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05302334 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002335 * @desc: GPIO to set to output
2336 * @value: initial output value of the GPIO
2337 *
2338 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2339 * be called safely on it. The initial value of the output must be specified
2340 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2341 * account.
2342 *
2343 * Return 0 in case of success, else an error code.
2344 */
2345int gpiod_direction_output(struct gpio_desc *desc, int value)
2346{
Linus Walleij02e47982017-09-26 21:20:23 +02002347 struct gpio_chip *gc = desc->gdev->chip;
2348 int ret;
2349
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002350 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002351 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2352 value = !value;
Linus Walleijad177312016-11-13 23:02:44 +01002353 else
2354 value = !!value;
Linus Walleij02e47982017-09-26 21:20:23 +02002355
2356 /* GPIOs used for IRQs shall not be set as output */
2357 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
2358 gpiod_err(desc,
2359 "%s: tried to set a GPIO tied to an IRQ as output\n",
2360 __func__);
2361 return -EIO;
2362 }
2363
2364 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2365 /* First see if we can enable open drain in hardware */
2366 ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2367 PIN_CONFIG_DRIVE_OPEN_DRAIN);
2368 if (!ret)
2369 goto set_output_value;
2370 /* Emulate open drain by not actively driving the line high */
2371 if (value)
2372 return gpiod_direction_input(desc);
2373 }
2374 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2375 ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2376 PIN_CONFIG_DRIVE_OPEN_SOURCE);
2377 if (!ret)
2378 goto set_output_value;
2379 /* Emulate open source by not actively driving the line low */
2380 if (!value)
2381 return gpiod_direction_input(desc);
2382 } else {
2383 gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2384 PIN_CONFIG_DRIVE_PUSH_PULL);
2385 }
2386
2387set_output_value:
Linus Walleijfac9d882017-09-26 20:58:28 +02002388 return gpiod_direction_output_raw_commit(desc, value);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002389}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002390EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08002391
Felipe Balbic4b5be92010-05-26 14:42:23 -07002392/**
Thierry Reding950d55f52017-07-24 16:57:22 +02002393 * gpiod_set_debounce - sets @debounce time for a GPIO
2394 * @desc: descriptor of the GPIO for which to set debounce time
2395 * @debounce: debounce time in microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02002396 *
Thierry Reding950d55f52017-07-24 16:57:22 +02002397 * Returns:
2398 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2399 * debounce time.
Felipe Balbic4b5be92010-05-26 14:42:23 -07002400 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002401int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07002402{
Felipe Balbic4b5be92010-05-26 14:42:23 -07002403 struct gpio_chip *chip;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002404 unsigned long config;
Felipe Balbic4b5be92010-05-26 14:42:23 -07002405
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002406 VALIDATE_DESC(desc);
2407 chip = desc->gdev->chip;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002408 if (!chip->set || !chip->set_config) {
Mark Brown6424de52013-09-09 10:33:49 +01002409 gpiod_dbg(desc,
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002410 "%s: missing set() or set_config() operations\n",
Mark Brown6424de52013-09-09 10:33:49 +01002411 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02002412 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002413 }
2414
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002415 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
2416 return chip->set_config(chip, gpio_chip_hwgpio(desc), config);
Felipe Balbic4b5be92010-05-26 14:42:23 -07002417}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002418EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002419
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002420/**
2421 * gpiod_is_active_low - test whether a GPIO is active-low or not
2422 * @desc: the gpio descriptor to test
2423 *
2424 * Returns 1 if the GPIO is active-low, 0 otherwise.
2425 */
2426int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002427{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002428 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002429 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002430}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002431EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08002432
2433/* I/O calls are only valid after configuration completed; the relevant
2434 * "is this a valid GPIO" error checks should already have been done.
2435 *
2436 * "Get" operations are often inlinable as reading a pin value register,
2437 * and masking the relevant bit in that register.
2438 *
2439 * When "set" operations are inlinable, they involve writing that mask to
2440 * one register to set a low value, or a different register to set it high.
2441 * Otherwise locking is needed, so there may be little value to inlining.
2442 *
2443 *------------------------------------------------------------------------
2444 *
2445 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2446 * have requested the GPIO. That can include implicit requesting by
2447 * a direction setting call. Marking a gpio as requested locks its chip
2448 * in memory, guaranteeing that these table lookups need no more locking
2449 * and that gpiochip_remove() will fail.
2450 *
2451 * REVISIT when debugging, consider adding some instrumentation to ensure
2452 * that the GPIO was actually requested.
2453 */
2454
Linus Walleijfac9d882017-09-26 20:58:28 +02002455static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002456{
2457 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002458 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002459 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002460
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002461 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002462 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002463 value = chip->get ? chip->get(chip, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01002464 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002465 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002466 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002467}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002468
David Brownelld2876d02008-02-04 22:28:20 -08002469/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002470 * gpiod_get_raw_value() - return a gpio's raw value
2471 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08002472 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002473 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002474 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002475 *
2476 * This function should be called from contexts where we cannot sleep, and will
2477 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002478 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002479int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002480{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002481 VALIDATE_DESC(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002482 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002483 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02002484 return gpiod_get_raw_value_commit(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002485}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002486EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002487
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002488/**
2489 * gpiod_get_value() - return a gpio's value
2490 * @desc: gpio whose value will be returned
2491 *
2492 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002493 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002494 *
2495 * This function should be called from contexts where we cannot sleep, and will
2496 * complain if the GPIO chip functions potentially sleep.
2497 */
2498int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002499{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002500 int value;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002501
2502 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002503 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002504 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002505
Linus Walleijfac9d882017-09-26 20:58:28 +02002506 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002507 if (value < 0)
2508 return value;
2509
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002510 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2511 value = !value;
2512
2513 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002514}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002515EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08002516
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302517/*
Linus Walleijfac9d882017-09-26 20:58:28 +02002518 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002519 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002520 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302521 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002522static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302523{
2524 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002525 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002526 int offset = gpio_chip_hwgpio(desc);
2527
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302528 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002529 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302530 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002531 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302532 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002533 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302534 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002535 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302536 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002537 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302538 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002539 gpiod_err(desc,
2540 "%s: Error in set_value for open drain err %d\n",
2541 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302542}
2543
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302544/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002545 * _gpio_set_open_source_value() - Set the open source gpio's value.
2546 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002547 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302548 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002549static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302550{
2551 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002552 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002553 int offset = gpio_chip_hwgpio(desc);
2554
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302555 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002556 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302557 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002558 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302559 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002560 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302561 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002562 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302563 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002564 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302565 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002566 gpiod_err(desc,
2567 "%s: Error in set_value for open source err %d\n",
2568 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302569}
2570
Linus Walleijfac9d882017-09-26 20:58:28 +02002571static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08002572{
2573 struct gpio_chip *chip;
2574
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002575 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002576 trace_gpio_value(desc_to_gpio(desc), 0, value);
Linus Walleij02e47982017-09-26 21:20:23 +02002577 chip->set(chip, gpio_chip_hwgpio(desc), value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002578}
2579
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002580/*
2581 * set multiple outputs on the same chip;
2582 * use the chip's set_multiple function if available;
2583 * otherwise set the outputs sequentially;
2584 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
2585 * defines which outputs are to be changed
2586 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
2587 * defines the values the outputs specified by mask are to be set to
2588 */
2589static void gpio_chip_set_multiple(struct gpio_chip *chip,
2590 unsigned long *mask, unsigned long *bits)
2591{
2592 if (chip->set_multiple) {
2593 chip->set_multiple(chip, mask, bits);
2594 } else {
Andy Shevchenko5e4e6fb2017-01-03 19:01:17 +02002595 unsigned int i;
2596
2597 /* set outputs if the corresponding mask bit is set */
2598 for_each_set_bit(i, mask, chip->ngpio)
2599 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002600 }
2601}
2602
Linus Walleij44c72882016-04-24 11:36:59 +02002603void gpiod_set_array_value_complex(bool raw, bool can_sleep,
2604 unsigned int array_size,
2605 struct gpio_desc **desc_array,
2606 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002607{
2608 int i = 0;
2609
2610 while (i < array_size) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002611 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002612 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2613 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2614 int count = 0;
2615
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002616 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002617 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002618
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002619 memset(mask, 0, sizeof(mask));
2620 do {
2621 struct gpio_desc *desc = desc_array[i];
2622 int hwgpio = gpio_chip_hwgpio(desc);
2623 int value = value_array[i];
2624
2625 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2626 value = !value;
2627 trace_gpio_value(desc_to_gpio(desc), 0, value);
2628 /*
2629 * collect all normal outputs belonging to the same chip
2630 * open drain and open source outputs are set individually
2631 */
Linus Walleij02e47982017-09-26 21:20:23 +02002632 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02002633 gpio_set_open_drain_value_commit(desc, value);
Linus Walleij02e47982017-09-26 21:20:23 +02002634 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02002635 gpio_set_open_source_value_commit(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002636 } else {
2637 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002638 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002639 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002640 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002641 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002642 count++;
2643 }
2644 i++;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002645 } while ((i < array_size) &&
2646 (desc_array[i]->gdev->chip == chip));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002647 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002648 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002649 gpio_chip_set_multiple(chip, mask, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002650 }
2651}
2652
David Brownelld2876d02008-02-04 22:28:20 -08002653/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002654 * gpiod_set_raw_value() - assign a gpio's raw value
2655 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08002656 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002657 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002658 * Set the raw value of the GPIO, i.e. the value of its physical line without
2659 * regard for its ACTIVE_LOW status.
2660 *
2661 * This function should be called from contexts where we cannot sleep, and will
2662 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002663 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002664void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002665{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002666 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01002667 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002668 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02002669 gpiod_set_raw_value_commit(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002670}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002671EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002672
2673/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002674 * gpiod_set_value() - assign a gpio's value
2675 * @desc: gpio whose value will be assigned
2676 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002677 *
Linus Walleij02e47982017-09-26 21:20:23 +02002678 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
2679 * OPEN_DRAIN and OPEN_SOURCE flags into account.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002680 *
2681 * This function should be called from contexts where we cannot sleep, and will
2682 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002683 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002684void gpiod_set_value(struct gpio_desc *desc, int value)
2685{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002686 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01002687 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002688 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002689 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2690 value = !value;
Linus Walleij02e47982017-09-26 21:20:23 +02002691 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2692 gpio_set_open_drain_value_commit(desc, value);
2693 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2694 gpio_set_open_source_value_commit(desc, value);
2695 else
2696 gpiod_set_raw_value_commit(desc, value);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002697}
2698EXPORT_SYMBOL_GPL(gpiod_set_value);
2699
2700/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002701 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002702 * @array_size: number of elements in the descriptor / value arrays
2703 * @desc_array: array of GPIO descriptors whose values will be assigned
2704 * @value_array: array of values to assign
2705 *
2706 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2707 * without regard for their ACTIVE_LOW status.
2708 *
2709 * This function should be called from contexts where we cannot sleep, and will
2710 * complain if the GPIO chip functions potentially sleep.
2711 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002712void gpiod_set_raw_array_value(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002713 struct gpio_desc **desc_array, int *value_array)
2714{
2715 if (!desc_array)
2716 return;
Linus Walleij44c72882016-04-24 11:36:59 +02002717 gpiod_set_array_value_complex(true, false, array_size, desc_array,
2718 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002719}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002720EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002721
2722/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002723 * gpiod_set_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002724 * @array_size: number of elements in the descriptor / value arrays
2725 * @desc_array: array of GPIO descriptors whose values will be assigned
2726 * @value_array: array of values to assign
2727 *
2728 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2729 * into account.
2730 *
2731 * This function should be called from contexts where we cannot sleep, and will
2732 * complain if the GPIO chip functions potentially sleep.
2733 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002734void gpiod_set_array_value(unsigned int array_size,
2735 struct gpio_desc **desc_array, int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002736{
2737 if (!desc_array)
2738 return;
Linus Walleij44c72882016-04-24 11:36:59 +02002739 gpiod_set_array_value_complex(false, false, array_size, desc_array,
2740 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002741}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002742EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002743
2744/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002745 * gpiod_cansleep() - report whether gpio value access may sleep
2746 * @desc: gpio to check
2747 *
2748 */
2749int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002750{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002751 VALIDATE_DESC(desc);
2752 return desc->gdev->chip->can_sleep;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002753}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002754EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002755
David Brownell0f6d5042008-10-15 22:03:14 -07002756/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002757 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
2758 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07002759 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002760 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
2761 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07002762 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002763int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07002764{
Linus Walleij4c37ce82016-05-02 13:13:10 +02002765 struct gpio_chip *chip;
2766 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07002767
Linus Walleij79bb71b2016-06-15 22:57:38 +02002768 /*
2769 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
2770 * requires this function to not return zero on an invalid descriptor
2771 * but rather a negative error number.
2772 */
Linus Walleijbfbbe442016-06-16 11:55:55 +02002773 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
Linus Walleij79bb71b2016-06-15 22:57:38 +02002774 return -EINVAL;
2775
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002776 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002777 offset = gpio_chip_hwgpio(desc);
Linus Walleij4c37ce82016-05-02 13:13:10 +02002778 if (chip->to_irq) {
2779 int retirq = chip->to_irq(chip, offset);
2780
2781 /* Zero means NO_IRQ */
2782 if (!retirq)
2783 return -ENXIO;
2784
2785 return retirq;
2786 }
2787 return -ENXIO;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002788}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002789EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002790
Linus Walleijd468bf92013-09-24 11:54:38 +02002791/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002792 * gpiochip_lock_as_irq() - lock a GPIO to be 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 lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08002797 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08002798 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002799int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08002800{
Linus Walleij9c102802016-05-25 10:56:03 +02002801 struct gpio_desc *desc;
Linus Walleijd468bf92013-09-24 11:54:38 +02002802
Linus Walleij9c102802016-05-25 10:56:03 +02002803 desc = gpiochip_get_desc(chip, offset);
2804 if (IS_ERR(desc))
2805 return PTR_ERR(desc);
2806
Linus Walleij60f83392016-11-12 15:01:09 +01002807 /*
2808 * If it's fast: flush the direction setting if something changed
2809 * behind our back
2810 */
2811 if (!chip->can_sleep && chip->get_direction) {
Linus Walleij9c102802016-05-25 10:56:03 +02002812 int dir = chip->get_direction(chip, offset);
2813
2814 if (dir)
2815 clear_bit(FLAG_IS_OUT, &desc->flags);
2816 else
2817 set_bit(FLAG_IS_OUT, &desc->flags);
2818 }
2819
2820 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002821 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02002822 "%s: tried to flag a GPIO set as output for IRQ\n",
2823 __func__);
2824 return -EIO;
2825 }
2826
Linus Walleij9c102802016-05-25 10:56:03 +02002827 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
Linus Walleij3940c342016-11-14 00:09:07 +01002828
2829 /*
2830 * If the consumer has not set up a label (such as when the
2831 * IRQ is referenced from .to_irq()) we set up a label here
2832 * so it is clear this is used as an interrupt.
2833 */
2834 if (!desc->label)
2835 desc_set_label(desc, "interrupt");
2836
Linus Walleijd468bf92013-09-24 11:54:38 +02002837 return 0;
2838}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002839EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02002840
Linus Walleijd468bf92013-09-24 11:54:38 +02002841/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002842 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002843 * @chip: the chip the GPIO to lock belongs to
2844 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02002845 *
2846 * This is used directly by GPIO drivers that want to indicate
2847 * that a certain GPIO is no longer used exclusively for IRQ.
2848 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002849void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02002850{
Linus Walleij3940c342016-11-14 00:09:07 +01002851 struct gpio_desc *desc;
2852
2853 desc = gpiochip_get_desc(chip, offset);
2854 if (IS_ERR(desc))
Linus Walleijd468bf92013-09-24 11:54:38 +02002855 return;
2856
Linus Walleij3940c342016-11-14 00:09:07 +01002857 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
2858
2859 /* If we only had this marking, erase it */
2860 if (desc->label && !strcmp(desc->label, "interrupt"))
2861 desc_set_label(desc, NULL);
Linus Walleijd468bf92013-09-24 11:54:38 +02002862}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002863EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02002864
Linus Walleij6cee3822016-02-11 20:16:45 +01002865bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
2866{
2867 if (offset >= chip->ngpio)
2868 return false;
2869
2870 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
2871}
2872EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
2873
Linus Walleij143b65d2016-02-16 15:41:42 +01002874bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
2875{
2876 if (offset >= chip->ngpio)
2877 return false;
2878
2879 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
2880}
2881EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
2882
2883bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
2884{
2885 if (offset >= chip->ngpio)
2886 return false;
2887
2888 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
2889}
2890EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
2891
Charles Keepax05f479b2017-05-23 15:47:29 +01002892bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset)
2893{
2894 if (offset >= chip->ngpio)
2895 return false;
2896
2897 return !test_bit(FLAG_SLEEP_MAY_LOOSE_VALUE,
2898 &chip->gpiodev->descs[offset].flags);
2899}
2900EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
2901
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002902/**
2903 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
2904 * @desc: gpio whose value will be returned
2905 *
2906 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002907 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002908 *
2909 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002910 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002911int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002912{
David Brownelld2876d02008-02-04 22:28:20 -08002913 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002914 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02002915 return gpiod_get_raw_value_commit(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002916}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002917EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002918
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002919/**
2920 * gpiod_get_value_cansleep() - return a gpio's value
2921 * @desc: gpio whose value will be returned
2922 *
2923 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002924 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002925 *
2926 * This function is to be called from contexts that can sleep.
2927 */
2928int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002929{
David Brownelld2876d02008-02-04 22:28:20 -08002930 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002931
2932 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002933 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02002934 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002935 if (value < 0)
2936 return value;
2937
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002938 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2939 value = !value;
2940
David Brownelld2876d02008-02-04 22:28:20 -08002941 return value;
2942}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002943EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002944
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002945/**
2946 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
2947 * @desc: gpio whose value will be assigned
2948 * @value: value to assign
2949 *
2950 * Set the raw value of the GPIO, i.e. the value of its physical line without
2951 * regard for its ACTIVE_LOW status.
2952 *
2953 * This function is to be called from contexts that can sleep.
2954 */
2955void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002956{
David Brownelld2876d02008-02-04 22:28:20 -08002957 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002958 VALIDATE_DESC_VOID(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02002959 gpiod_set_raw_value_commit(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002960}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002961EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002962
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002963/**
2964 * gpiod_set_value_cansleep() - assign a gpio's value
2965 * @desc: gpio whose value will be assigned
2966 * @value: value to assign
2967 *
2968 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2969 * account
2970 *
2971 * This function is to be called from contexts that can sleep.
2972 */
2973void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002974{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002975 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002976 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002977 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2978 value = !value;
Linus Walleijfac9d882017-09-26 20:58:28 +02002979 gpiod_set_raw_value_commit(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002980}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002981EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002982
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002983/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002984 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002985 * @array_size: number of elements in the descriptor / value arrays
2986 * @desc_array: array of GPIO descriptors whose values will be assigned
2987 * @value_array: array of values to assign
2988 *
2989 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2990 * without regard for their ACTIVE_LOW status.
2991 *
2992 * This function is to be called from contexts that can sleep.
2993 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002994void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
2995 struct gpio_desc **desc_array,
2996 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002997{
2998 might_sleep_if(extra_checks);
2999 if (!desc_array)
3000 return;
Linus Walleij44c72882016-04-24 11:36:59 +02003001 gpiod_set_array_value_complex(true, true, array_size, desc_array,
3002 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003003}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003004EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003005
3006/**
Dmitry Torokhov3946d182017-08-14 21:59:55 -07003007 * gpiod_add_lookup_tables() - register GPIO device consumers
3008 * @tables: list of tables of consumers to register
3009 * @n: number of tables in the list
3010 */
3011void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
3012{
3013 unsigned int i;
3014
3015 mutex_lock(&gpio_lookup_lock);
3016
3017 for (i = 0; i < n; i++)
3018 list_add_tail(&tables[i]->list, &gpio_lookup_list);
3019
3020 mutex_unlock(&gpio_lookup_lock);
3021}
3022
3023/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003024 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003025 * @array_size: number of elements in the descriptor / value arrays
3026 * @desc_array: array of GPIO descriptors whose values will be assigned
3027 * @value_array: array of values to assign
3028 *
3029 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3030 * into account.
3031 *
3032 * This function is to be called from contexts that can sleep.
3033 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003034void gpiod_set_array_value_cansleep(unsigned int array_size,
3035 struct gpio_desc **desc_array,
3036 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003037{
3038 might_sleep_if(extra_checks);
3039 if (!desc_array)
3040 return;
Linus Walleij44c72882016-04-24 11:36:59 +02003041 gpiod_set_array_value_complex(false, true, array_size, desc_array,
3042 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003043}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003044EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003045
3046/**
Alexandre Courbotad824782013-12-03 12:20:11 +09003047 * gpiod_add_lookup_table() - register GPIO device consumers
3048 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003049 */
Alexandre Courbotad824782013-12-03 12:20:11 +09003050void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003051{
3052 mutex_lock(&gpio_lookup_lock);
3053
Alexandre Courbotad824782013-12-03 12:20:11 +09003054 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003055
3056 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08003057}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02003058EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
David Brownelld2876d02008-02-04 22:28:20 -08003059
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05303060/**
3061 * gpiod_remove_lookup_table() - unregister GPIO device consumers
3062 * @table: table of consumers to unregister
3063 */
3064void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
3065{
3066 mutex_lock(&gpio_lookup_lock);
3067
3068 list_del(&table->list);
3069
3070 mutex_unlock(&gpio_lookup_lock);
3071}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02003072EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05303073
Alexandre Courbotad824782013-12-03 12:20:11 +09003074static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
3075{
3076 const char *dev_id = dev ? dev_name(dev) : NULL;
3077 struct gpiod_lookup_table *table;
3078
3079 mutex_lock(&gpio_lookup_lock);
3080
3081 list_for_each_entry(table, &gpio_lookup_list, list) {
3082 if (table->dev_id && dev_id) {
3083 /*
3084 * Valid strings on both ends, must be identical to have
3085 * a match
3086 */
3087 if (!strcmp(table->dev_id, dev_id))
3088 goto found;
3089 } else {
3090 /*
3091 * One of the pointers is NULL, so both must be to have
3092 * a match
3093 */
3094 if (dev_id == table->dev_id)
3095 goto found;
3096 }
3097 }
3098 table = NULL;
3099
3100found:
3101 mutex_unlock(&gpio_lookup_lock);
3102 return table;
3103}
3104
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003105static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09003106 unsigned int idx,
3107 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003108{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003109 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09003110 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003111 struct gpiod_lookup *p;
3112
Alexandre Courbotad824782013-12-03 12:20:11 +09003113 table = gpiod_find_lookup_table(dev);
3114 if (!table)
3115 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003116
Alexandre Courbotad824782013-12-03 12:20:11 +09003117 for (p = &table->table[0]; p->chip_label; p++) {
3118 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003119
Alexandre Courbotad824782013-12-03 12:20:11 +09003120 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003121 if (p->idx != idx)
3122 continue;
3123
Alexandre Courbotad824782013-12-03 12:20:11 +09003124 /* If the lookup entry has a con_id, require exact match */
3125 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
3126 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003127
Alexandre Courbotad824782013-12-03 12:20:11 +09003128 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003129
Alexandre Courbotad824782013-12-03 12:20:11 +09003130 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003131 dev_err(dev, "cannot find GPIO chip %s\n",
3132 p->chip_label);
3133 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003134 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003135
Alexandre Courbotad824782013-12-03 12:20:11 +09003136 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003137 dev_err(dev,
3138 "requested GPIO %d is out of range [0..%d] for chip %s\n",
3139 idx, chip->ngpio, chip->label);
3140 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09003141 }
3142
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09003143 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09003144 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003145
3146 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09003147 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003148
3149 return desc;
3150}
3151
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003152static int dt_gpio_count(struct device *dev, const char *con_id)
3153{
3154 int ret;
3155 char propname[32];
3156 unsigned int i;
3157
3158 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
3159 if (con_id)
3160 snprintf(propname, sizeof(propname), "%s-%s",
3161 con_id, gpio_suffixes[i]);
3162 else
3163 snprintf(propname, sizeof(propname), "%s",
3164 gpio_suffixes[i]);
3165
3166 ret = of_gpio_named_count(dev->of_node, propname);
Andy Shevchenko4033d4a2017-02-20 18:15:47 +02003167 if (ret > 0)
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003168 break;
3169 }
Andy Shevchenko4033d4a2017-02-20 18:15:47 +02003170 return ret ? ret : -ENOENT;
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003171}
3172
3173static int platform_gpio_count(struct device *dev, const char *con_id)
3174{
3175 struct gpiod_lookup_table *table;
3176 struct gpiod_lookup *p;
3177 unsigned int count = 0;
3178
3179 table = gpiod_find_lookup_table(dev);
3180 if (!table)
3181 return -ENOENT;
3182
3183 for (p = &table->table[0]; p->chip_label; p++) {
3184 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3185 (!con_id && !p->con_id))
3186 count++;
3187 }
3188 if (!count)
3189 return -ENOENT;
3190
3191 return count;
3192}
3193
3194/**
3195 * gpiod_count - return the number of GPIOs associated with a device / function
3196 * or -ENOENT if no GPIO has been assigned to the requested function
3197 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3198 * @con_id: function within the GPIO consumer
3199 */
3200int gpiod_count(struct device *dev, const char *con_id)
3201{
3202 int count = -ENOENT;
3203
3204 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
3205 count = dt_gpio_count(dev, con_id);
3206 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
3207 count = acpi_gpio_count(dev, con_id);
3208
3209 if (count < 0)
3210 count = platform_gpio_count(dev, con_id);
3211
3212 return count;
3213}
3214EXPORT_SYMBOL_GPL(gpiod_count);
3215
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003216/**
Thierry Reding08791622014-04-25 16:54:22 +02003217 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09003218 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003219 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003220 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003221 *
3222 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003223 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07003224 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003225 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003226struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003227 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003228{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003229 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003230}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003231EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003232
3233/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02003234 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
3235 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3236 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003237 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02003238 *
3239 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
3240 * the requested function it will return NULL. This is convenient for drivers
3241 * that need to handle optional GPIOs.
3242 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003243struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003244 const char *con_id,
3245 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003246{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003247 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003248}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003249EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003250
Benoit Parrotf625d462015-02-02 11:44:44 -06003251
3252/**
3253 * gpiod_configure_flags - helper function to configure a given GPIO
3254 * @desc: gpio whose value will be assigned
3255 * @con_id: function within the GPIO consumer
Johan Hovold85b03b32016-07-03 18:32:05 +02003256 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3257 * of_get_gpio_hog()
Benoit Parrotf625d462015-02-02 11:44:44 -06003258 * @dflags: gpiod_flags - optional GPIO initialization flags
3259 *
3260 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
3261 * requested function and/or index, or another IS_ERR() code if an error
3262 * occurred while trying to acquire the GPIO.
3263 */
Andy Shevchenkoc29fd9e2017-05-23 20:03:16 +03003264int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Johan Hovold85b03b32016-07-03 18:32:05 +02003265 unsigned long lflags, enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06003266{
3267 int status;
3268
Johan Hovold85b03b32016-07-03 18:32:05 +02003269 if (lflags & GPIO_ACTIVE_LOW)
3270 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
3271 if (lflags & GPIO_OPEN_DRAIN)
3272 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3273 if (lflags & GPIO_OPEN_SOURCE)
3274 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Charles Keepax05f479b2017-05-23 15:47:29 +01003275 if (lflags & GPIO_SLEEP_MAY_LOOSE_VALUE)
3276 set_bit(FLAG_SLEEP_MAY_LOOSE_VALUE, &desc->flags);
Johan Hovold85b03b32016-07-03 18:32:05 +02003277
Benoit Parrotf625d462015-02-02 11:44:44 -06003278 /* No particular flag request, return here... */
3279 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
3280 pr_debug("no flags found for %s\n", con_id);
3281 return 0;
3282 }
3283
3284 /* Process flags */
3285 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
3286 status = gpiod_direction_output(desc,
Linus Walleijad177312016-11-13 23:02:44 +01003287 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
Benoit Parrotf625d462015-02-02 11:44:44 -06003288 else
3289 status = gpiod_direction_input(desc);
3290
3291 return status;
3292}
3293
Thierry Reding29a1f2332014-04-25 17:10:06 +02003294/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003295 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02003296 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003297 * @con_id: function within the GPIO consumer
3298 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003299 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003300 *
3301 * This variant of gpiod_get() allows to access GPIOs other than the first
3302 * defined one for functions that define several GPIOs.
3303 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003304 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
3305 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07003306 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003307 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003308struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003309 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003310 unsigned int idx,
3311 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003312{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003313 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003314 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003315 enum gpio_lookup_flags lookupflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003316
3317 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
3318
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003319 if (dev) {
3320 /* Using device tree? */
3321 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
3322 dev_dbg(dev, "using device tree for GPIO lookup\n");
3323 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
3324 } else if (ACPI_COMPANION(dev)) {
3325 dev_dbg(dev, "using ACPI for GPIO lookup\n");
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +03003326 desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags);
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003327 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003328 }
3329
3330 /*
3331 * Either we are not using DT or ACPI, or their lookup did not return
3332 * a result. In that case, use platform lookup as a fallback.
3333 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003334 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04003335 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003336 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003337 }
3338
3339 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02003340 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003341 return desc;
3342 }
3343
3344 status = gpiod_request(desc, con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003345 if (status < 0)
3346 return ERR_PTR(status);
3347
Johan Hovold85b03b32016-07-03 18:32:05 +02003348 status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003349 if (status < 0) {
3350 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
3351 gpiod_put(desc);
3352 return ERR_PTR(status);
3353 }
3354
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003355 return desc;
3356}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003357EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003358
3359/**
Mika Westerberg40b73182014-10-21 13:33:59 +02003360 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
3361 * @fwnode: handle of the firmware node
3362 * @propname: name of the firmware property representing the GPIO
Boris Brezillon537b94d2017-02-02 14:53:11 +01003363 * @index: index of the GPIO to obtain in the consumer
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003364 * @dflags: GPIO initialization flags
Thierry Reding950d55f52017-07-24 16:57:22 +02003365 * @label: label to attach to the requested GPIO
Mika Westerberg40b73182014-10-21 13:33:59 +02003366 *
3367 * This function can be used for drivers that get their configuration
3368 * from firmware.
3369 *
3370 * Function properly finds the corresponding GPIO using whatever is the
3371 * underlying firmware interface and then makes sure that the GPIO
3372 * descriptor is requested before it is returned to the caller.
3373 *
Thierry Reding950d55f52017-07-24 16:57:22 +02003374 * Returns:
Andy Shevchenkoff213782017-02-28 17:03:12 +02003375 * On successful request the GPIO pin is configured in accordance with
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003376 * provided @dflags.
3377 *
Mika Westerberg40b73182014-10-21 13:33:59 +02003378 * In case of error an ERR_PTR() is returned.
3379 */
3380struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
Boris Brezillon537b94d2017-02-02 14:53:11 +01003381 const char *propname, int index,
Alexander Steinb2987d72017-01-12 17:39:24 +01003382 enum gpiod_flags dflags,
3383 const char *label)
Mika Westerberg40b73182014-10-21 13:33:59 +02003384{
3385 struct gpio_desc *desc = ERR_PTR(-ENODEV);
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003386 unsigned long lflags = 0;
Mika Westerberg40b73182014-10-21 13:33:59 +02003387 bool active_low = false;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003388 bool single_ended = false;
Laxman Dewangan4c0facd2017-04-06 19:05:52 +05303389 bool open_drain = false;
Mika Westerberg40b73182014-10-21 13:33:59 +02003390 int ret;
3391
3392 if (!fwnode)
3393 return ERR_PTR(-EINVAL);
3394
3395 if (is_of_node(fwnode)) {
3396 enum of_gpio_flags flags;
3397
Boris Brezillon537b94d2017-02-02 14:53:11 +01003398 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname,
3399 index, &flags);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003400 if (!IS_ERR(desc)) {
Mika Westerberg40b73182014-10-21 13:33:59 +02003401 active_low = flags & OF_GPIO_ACTIVE_LOW;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003402 single_ended = flags & OF_GPIO_SINGLE_ENDED;
Laxman Dewangan4c0facd2017-04-06 19:05:52 +05303403 open_drain = flags & OF_GPIO_OPEN_DRAIN;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003404 }
Mika Westerberg40b73182014-10-21 13:33:59 +02003405 } else if (is_acpi_node(fwnode)) {
3406 struct acpi_gpio_info info;
3407
Boris Brezillon537b94d2017-02-02 14:53:11 +01003408 desc = acpi_node_get_gpiod(fwnode, propname, index, &info);
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +03003409 if (!IS_ERR(desc)) {
Christophe RICARD52044722015-12-23 23:25:34 +01003410 active_low = info.polarity == GPIO_ACTIVE_LOW;
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +03003411 ret = acpi_gpio_update_gpiod_flags(&dflags, info.flags);
3412 if (ret)
3413 pr_debug("Override GPIO initialization flags\n");
3414 }
Mika Westerberg40b73182014-10-21 13:33:59 +02003415 }
3416
3417 if (IS_ERR(desc))
3418 return desc;
3419
Alexander Steinb2987d72017-01-12 17:39:24 +01003420 ret = gpiod_request(desc, label);
Johan Hovold85b03b32016-07-03 18:32:05 +02003421 if (ret)
3422 return ERR_PTR(ret);
3423
Mika Westerberg40b73182014-10-21 13:33:59 +02003424 if (active_low)
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003425 lflags |= GPIO_ACTIVE_LOW;
Mika Westerberg40b73182014-10-21 13:33:59 +02003426
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003427 if (single_ended) {
Laxman Dewangan4c0facd2017-04-06 19:05:52 +05303428 if (open_drain)
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003429 lflags |= GPIO_OPEN_DRAIN;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003430 else
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003431 lflags |= GPIO_OPEN_SOURCE;
3432 }
3433
3434 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
3435 if (ret < 0) {
3436 gpiod_put(desc);
3437 return ERR_PTR(ret);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003438 }
3439
Mika Westerberg40b73182014-10-21 13:33:59 +02003440 return desc;
3441}
3442EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
3443
3444/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02003445 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
3446 * function
3447 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3448 * @con_id: function within the GPIO consumer
3449 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003450 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02003451 *
3452 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
3453 * specified index was assigned to the requested function it will return NULL.
3454 * This is convenient for drivers that need to handle optional GPIOs.
3455 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003456struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02003457 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003458 unsigned int index,
3459 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003460{
3461 struct gpio_desc *desc;
3462
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003463 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003464 if (IS_ERR(desc)) {
3465 if (PTR_ERR(desc) == -ENOENT)
3466 return NULL;
3467 }
3468
3469 return desc;
3470}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003471EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003472
3473/**
Benoit Parrotf625d462015-02-02 11:44:44 -06003474 * gpiod_hog - Hog the specified GPIO desc given the provided flags
3475 * @desc: gpio whose value will be assigned
3476 * @name: gpio line name
3477 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3478 * of_get_gpio_hog()
3479 * @dflags: gpiod_flags - optional GPIO initialization flags
3480 */
3481int gpiod_hog(struct gpio_desc *desc, const char *name,
3482 unsigned long lflags, enum gpiod_flags dflags)
3483{
3484 struct gpio_chip *chip;
3485 struct gpio_desc *local_desc;
3486 int hwnum;
3487 int status;
3488
3489 chip = gpiod_to_chip(desc);
3490 hwnum = gpio_chip_hwgpio(desc);
3491
3492 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
3493 if (IS_ERR(local_desc)) {
Laxman Dewanganc31a5712016-03-11 19:13:21 +05303494 status = PTR_ERR(local_desc);
3495 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
3496 name, chip->label, hwnum, status);
3497 return status;
Benoit Parrotf625d462015-02-02 11:44:44 -06003498 }
3499
Johan Hovold85b03b32016-07-03 18:32:05 +02003500 status = gpiod_configure_flags(desc, name, lflags, dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -06003501 if (status < 0) {
Laxman Dewanganc31a5712016-03-11 19:13:21 +05303502 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed, %d\n",
3503 name, chip->label, hwnum, status);
Benoit Parrotf625d462015-02-02 11:44:44 -06003504 gpiochip_free_own_desc(desc);
3505 return status;
3506 }
3507
3508 /* Mark GPIO as hogged so it can be identified and removed later */
3509 set_bit(FLAG_IS_HOGGED, &desc->flags);
3510
3511 pr_info("GPIO line %d (%s) hogged as %s%s\n",
3512 desc_to_gpio(desc), name,
3513 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
3514 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
3515 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
3516
3517 return 0;
3518}
3519
3520/**
3521 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
3522 * @chip: gpio chip to act on
3523 *
3524 * This is only used by of_gpiochip_remove to free hogged gpios
3525 */
3526static void gpiochip_free_hogs(struct gpio_chip *chip)
3527{
3528 int id;
3529
3530 for (id = 0; id < chip->ngpio; id++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01003531 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
3532 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
Benoit Parrotf625d462015-02-02 11:44:44 -06003533 }
3534}
3535
3536/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003537 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
3538 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3539 * @con_id: function within the GPIO consumer
3540 * @flags: optional GPIO initialization flags
3541 *
3542 * This function acquires all the GPIOs defined under a given function.
3543 *
3544 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
3545 * no GPIO has been assigned to the requested function, or another IS_ERR()
3546 * code if an error occurred while trying to acquire the GPIOs.
3547 */
3548struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
3549 const char *con_id,
3550 enum gpiod_flags flags)
3551{
3552 struct gpio_desc *desc;
3553 struct gpio_descs *descs;
3554 int count;
3555
3556 count = gpiod_count(dev, con_id);
3557 if (count < 0)
3558 return ERR_PTR(count);
3559
3560 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
3561 GFP_KERNEL);
3562 if (!descs)
3563 return ERR_PTR(-ENOMEM);
3564
3565 for (descs->ndescs = 0; descs->ndescs < count; ) {
3566 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
3567 if (IS_ERR(desc)) {
3568 gpiod_put_array(descs);
3569 return ERR_CAST(desc);
3570 }
3571 descs->desc[descs->ndescs] = desc;
3572 descs->ndescs++;
3573 }
3574 return descs;
3575}
3576EXPORT_SYMBOL_GPL(gpiod_get_array);
3577
3578/**
3579 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
3580 * function
3581 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3582 * @con_id: function within the GPIO consumer
3583 * @flags: optional GPIO initialization flags
3584 *
3585 * This is equivalent to gpiod_get_array(), except that when no GPIO was
3586 * assigned to the requested function it will return NULL.
3587 */
3588struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
3589 const char *con_id,
3590 enum gpiod_flags flags)
3591{
3592 struct gpio_descs *descs;
3593
3594 descs = gpiod_get_array(dev, con_id, flags);
3595 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
3596 return NULL;
3597
3598 return descs;
3599}
3600EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
3601
3602/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003603 * gpiod_put - dispose of a GPIO descriptor
3604 * @desc: GPIO descriptor to dispose of
3605 *
3606 * No descriptor can be used after gpiod_put() has been called on it.
3607 */
3608void gpiod_put(struct gpio_desc *desc)
3609{
3610 gpiod_free(desc);
3611}
3612EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08003613
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003614/**
3615 * gpiod_put_array - dispose of multiple GPIO descriptors
3616 * @descs: struct gpio_descs containing an array of descriptors
3617 */
3618void gpiod_put_array(struct gpio_descs *descs)
3619{
3620 unsigned int i;
3621
3622 for (i = 0; i < descs->ndescs; i++)
3623 gpiod_put(descs->desc[i]);
3624
3625 kfree(descs);
3626}
3627EXPORT_SYMBOL_GPL(gpiod_put_array);
3628
Linus Walleij3c702e92015-10-21 15:29:53 +02003629static int __init gpiolib_dev_init(void)
3630{
3631 int ret;
3632
3633 /* Register GPIO sysfs bus */
3634 ret = bus_register(&gpio_bus_type);
3635 if (ret < 0) {
3636 pr_err("gpiolib: could not register GPIO bus type\n");
3637 return ret;
3638 }
3639
3640 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
3641 if (ret < 0) {
3642 pr_err("gpiolib: failed to allocate char dev region\n");
3643 bus_unregister(&gpio_bus_type);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07003644 } else {
3645 gpiolib_initialized = true;
3646 gpiochip_setup_devs();
Linus Walleij3c702e92015-10-21 15:29:53 +02003647 }
3648 return ret;
3649}
3650core_initcall(gpiolib_dev_init);
3651
David Brownelld2876d02008-02-04 22:28:20 -08003652#ifdef CONFIG_DEBUG_FS
3653
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003654static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
David Brownelld2876d02008-02-04 22:28:20 -08003655{
3656 unsigned i;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003657 struct gpio_chip *chip = gdev->chip;
3658 unsigned gpio = gdev->base;
3659 struct gpio_desc *gdesc = &gdev->descs[0];
David Brownelld2876d02008-02-04 22:28:20 -08003660 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02003661 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08003662
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003663 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02003664 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
3665 if (gdesc->name) {
3666 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
3667 gpio, gdesc->name);
3668 }
David Brownelld2876d02008-02-04 22:28:20 -08003669 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02003670 }
David Brownelld2876d02008-02-04 22:28:20 -08003671
Alexandre Courbot372e7222013-02-03 01:29:29 +09003672 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08003673 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02003674 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Markus Pargmannced433e2015-08-14 16:11:02 +02003675 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
3676 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08003677 is_out ? "out" : "in ",
3678 chip->get
3679 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02003680 : "? ",
3681 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08003682 seq_printf(s, "\n");
3683 }
3684}
3685
Thierry Redingf9c4a312012-04-12 13:26:01 +02003686static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08003687{
Grant Likely362432a2013-02-09 09:41:49 +00003688 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02003689 struct gpio_device *gdev = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003690 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003691
3692 s->private = "";
3693
Grant Likely362432a2013-02-09 09:41:49 +00003694 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003695 list_for_each_entry(gdev, &gpio_devices, list)
Grant Likely362432a2013-02-09 09:41:49 +00003696 if (index-- == 0) {
3697 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003698 return gdev;
Grant Likely362432a2013-02-09 09:41:49 +00003699 }
3700 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003701
3702 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003703}
3704
3705static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
3706{
Grant Likely362432a2013-02-09 09:41:49 +00003707 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02003708 struct gpio_device *gdev = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003709 void *ret = NULL;
3710
Grant Likely362432a2013-02-09 09:41:49 +00003711 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003712 if (list_is_last(&gdev->list, &gpio_devices))
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003713 ret = NULL;
3714 else
Linus Walleijff2b1352015-10-20 11:10:38 +02003715 ret = list_entry(gdev->list.next, struct gpio_device, list);
Grant Likely362432a2013-02-09 09:41:49 +00003716 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02003717
3718 s->private = "\n";
3719 ++*pos;
3720
3721 return ret;
3722}
3723
3724static void gpiolib_seq_stop(struct seq_file *s, void *v)
3725{
3726}
3727
3728static int gpiolib_seq_show(struct seq_file *s, void *v)
3729{
Linus Walleijff2b1352015-10-20 11:10:38 +02003730 struct gpio_device *gdev = v;
3731 struct gpio_chip *chip = gdev->chip;
3732 struct device *parent;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003733
Linus Walleijff2b1352015-10-20 11:10:38 +02003734 if (!chip) {
3735 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
3736 dev_name(&gdev->dev));
3737 return 0;
3738 }
3739
3740 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
3741 dev_name(&gdev->dev),
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003742 gdev->base, gdev->base + gdev->ngpio - 1);
Linus Walleijff2b1352015-10-20 11:10:38 +02003743 parent = chip->parent;
3744 if (parent)
3745 seq_printf(s, ", parent: %s/%s",
3746 parent->bus ? parent->bus->name : "no-bus",
3747 dev_name(parent));
Thierry Redingf9c4a312012-04-12 13:26:01 +02003748 if (chip->label)
3749 seq_printf(s, ", %s", chip->label);
3750 if (chip->can_sleep)
3751 seq_printf(s, ", can sleep");
3752 seq_printf(s, ":\n");
3753
3754 if (chip->dbg_show)
3755 chip->dbg_show(s, chip);
3756 else
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003757 gpiolib_dbg_show(s, gdev);
Thierry Redingf9c4a312012-04-12 13:26:01 +02003758
David Brownelld2876d02008-02-04 22:28:20 -08003759 return 0;
3760}
3761
Thierry Redingf9c4a312012-04-12 13:26:01 +02003762static const struct seq_operations gpiolib_seq_ops = {
3763 .start = gpiolib_seq_start,
3764 .next = gpiolib_seq_next,
3765 .stop = gpiolib_seq_stop,
3766 .show = gpiolib_seq_show,
3767};
3768
David Brownelld2876d02008-02-04 22:28:20 -08003769static int gpiolib_open(struct inode *inode, struct file *file)
3770{
Thierry Redingf9c4a312012-04-12 13:26:01 +02003771 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08003772}
3773
Alexey Dobriyan828c0952009-10-01 15:43:56 -07003774static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02003775 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08003776 .open = gpiolib_open,
3777 .read = seq_read,
3778 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02003779 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08003780};
3781
3782static int __init gpiolib_debugfs_init(void)
3783{
3784 /* /sys/kernel/debug/gpio */
3785 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
3786 NULL, NULL, &gpiolib_operations);
3787 return 0;
3788}
3789subsys_initcall(gpiolib_debugfs_init);
3790
3791#endif /* DEBUG_FS */