blob: d3608cf511de2a4beeabec6385eba7239f04b75d [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 Walleijf881bab02015-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 Walleijf881bab02015-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 Walleijf881bab02015-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 Walleijf881bab02015-09-23 16:20:43 -0700289
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100290 if (!desc->name || !name)
Linus Walleijf881bab02015-09-23 16:20:43 -0700291 continue;
292
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100293 if (!strcmp(desc->name, name)) {
Linus Walleijf881bab02015-09-23 16:20:43 -0700294 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100295 return desc;
Linus Walleijf881bab02015-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 Walleijf881bab02015-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 Walleijf881bab02015-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;
Lukas Wunnereec1d562017-10-12 12:40:10 +0200368 int vals[GPIOHANDLES_MAX];
Linus Walleijd7c51b42016-04-26 10:35:29 +0200369 int i;
370
371 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
Lukas Wunnereec1d562017-10-12 12:40:10 +0200372 /* TODO: check if descriptors are really input */
373 int ret = gpiod_get_array_value_complex(false,
374 true,
375 lh->numdescs,
376 lh->descs,
377 vals);
378 if (ret)
379 return ret;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200380
Lars-Peter Clausen3eded5d2016-10-18 16:54:02 +0200381 memset(&ghd, 0, sizeof(ghd));
Lukas Wunnereec1d562017-10-12 12:40:10 +0200382 for (i = 0; i < lh->numdescs; i++)
383 ghd.values[i] = vals[i];
Linus Walleijd7c51b42016-04-26 10:35:29 +0200384
385 if (copy_to_user(ip, &ghd, sizeof(ghd)))
386 return -EFAULT;
387
388 return 0;
389 } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
Linus Walleijd7c51b42016-04-26 10:35:29 +0200390 /* 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;
Bartosz Golaszewski418ee8e2017-10-16 11:32:29 +0200447 u32 lflags;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200448
449 if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
450 return -EFAULT;
451 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
452 return -EINVAL;
453
Bartosz Golaszewski418ee8e2017-10-16 11:32:29 +0200454 lflags = handlereq.flags;
455
456 /* Return an error if an unknown flag is set */
457 if (lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
458 return -EINVAL;
459
Bartosz Golaszewski609aaf62017-10-16 11:32:30 +0200460 /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
461 if (!(lflags & GPIOHANDLE_REQUEST_OUTPUT) &&
462 ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
463 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
464 return -EINVAL;
465
Linus Walleijd7c51b42016-04-26 10:35:29 +0200466 lh = kzalloc(sizeof(*lh), GFP_KERNEL);
467 if (!lh)
468 return -ENOMEM;
469 lh->gdev = gdev;
470 get_device(&gdev->dev);
471
472 /* Make sure this is terminated */
473 handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
474 if (strlen(handlereq.consumer_label)) {
475 lh->label = kstrdup(handlereq.consumer_label,
476 GFP_KERNEL);
477 if (!lh->label) {
478 ret = -ENOMEM;
479 goto out_free_lh;
480 }
481 }
482
483 /* Request each GPIO */
484 for (i = 0; i < handlereq.lines; i++) {
485 u32 offset = handlereq.lineoffsets[i];
Linus Walleijd7c51b42016-04-26 10:35:29 +0200486 struct gpio_desc *desc;
487
Lars-Peter Clausene405f9f2016-10-18 16:54:01 +0200488 if (offset >= gdev->ngpio) {
489 ret = -EINVAL;
490 goto out_free_descs;
491 }
492
Linus Walleijd7c51b42016-04-26 10:35:29 +0200493 desc = &gdev->descs[offset];
494 ret = gpiod_request(desc, lh->label);
495 if (ret)
496 goto out_free_descs;
497 lh->descs[i] = desc;
498
499 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
500 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
501 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
502 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
503 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
504 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
505
506 /*
507 * Lines have to be requested explicitly for input
508 * or output, else the line will be treated "as is".
509 */
510 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
511 int val = !!handlereq.default_values[i];
512
513 ret = gpiod_direction_output(desc, val);
514 if (ret)
515 goto out_free_descs;
516 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
517 ret = gpiod_direction_input(desc);
518 if (ret)
519 goto out_free_descs;
520 }
521 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
522 offset);
523 }
Linus Walleije2f608b2016-06-18 10:56:43 +0200524 /* Let i point at the last handle */
525 i--;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200526 lh->numdescs = handlereq.lines;
527
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200528 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200529 if (fd < 0) {
530 ret = fd;
531 goto out_free_descs;
532 }
533
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200534 file = anon_inode_getfile("gpio-linehandle",
535 &linehandle_fileops,
536 lh,
537 O_RDONLY | O_CLOEXEC);
538 if (IS_ERR(file)) {
539 ret = PTR_ERR(file);
540 goto out_put_unused_fd;
541 }
542
Linus Walleijd7c51b42016-04-26 10:35:29 +0200543 handlereq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200544 if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200545 /*
546 * fput() will trigger the release() callback, so do not go onto
547 * the regular error cleanup path here.
548 */
549 fput(file);
550 put_unused_fd(fd);
551 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +0200552 }
Linus Walleijd7c51b42016-04-26 10:35:29 +0200553
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200554 fd_install(fd, file);
555
Linus Walleijd7c51b42016-04-26 10:35:29 +0200556 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
557 lh->numdescs);
558
559 return 0;
560
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200561out_put_unused_fd:
562 put_unused_fd(fd);
Linus Walleijd7c51b42016-04-26 10:35:29 +0200563out_free_descs:
564 for (; i >= 0; i--)
565 gpiod_free(lh->descs[i]);
566 kfree(lh->label);
567out_free_lh:
568 kfree(lh);
569 put_device(&gdev->dev);
570 return ret;
571}
572
Linus Walleij61f922d2016-06-02 11:30:15 +0200573/*
574 * GPIO line event management
575 */
576
577/**
578 * struct lineevent_state - contains the state of a userspace event
579 * @gdev: the GPIO device the event pertains to
580 * @label: consumer label used to tag descriptors
581 * @desc: the GPIO descriptor held by this event
582 * @eflags: the event flags this line was requested with
583 * @irq: the interrupt that trigger in response to events on this GPIO
584 * @wait: wait queue that handles blocking reads of events
585 * @events: KFIFO for the GPIO events
586 * @read_lock: mutex lock to protect reads from colliding with adding
587 * new events to the FIFO
588 */
589struct lineevent_state {
590 struct gpio_device *gdev;
591 const char *label;
592 struct gpio_desc *desc;
593 u32 eflags;
594 int irq;
595 wait_queue_head_t wait;
596 DECLARE_KFIFO(events, struct gpioevent_data, 16);
597 struct mutex read_lock;
598};
599
Lars-Peter Clausenac7dbb92016-10-18 16:54:06 +0200600#define GPIOEVENT_REQUEST_VALID_FLAGS \
601 (GPIOEVENT_REQUEST_RISING_EDGE | \
602 GPIOEVENT_REQUEST_FALLING_EDGE)
603
Linus Walleij61f922d2016-06-02 11:30:15 +0200604static unsigned int lineevent_poll(struct file *filep,
605 struct poll_table_struct *wait)
606{
607 struct lineevent_state *le = filep->private_data;
608 unsigned int events = 0;
609
610 poll_wait(filep, &le->wait, wait);
611
612 if (!kfifo_is_empty(&le->events))
613 events = POLLIN | POLLRDNORM;
614
615 return events;
616}
617
618
619static ssize_t lineevent_read(struct file *filep,
620 char __user *buf,
621 size_t count,
622 loff_t *f_ps)
623{
624 struct lineevent_state *le = filep->private_data;
625 unsigned int copied;
626 int ret;
627
628 if (count < sizeof(struct gpioevent_data))
629 return -EINVAL;
630
631 do {
632 if (kfifo_is_empty(&le->events)) {
633 if (filep->f_flags & O_NONBLOCK)
634 return -EAGAIN;
635
636 ret = wait_event_interruptible(le->wait,
637 !kfifo_is_empty(&le->events));
638 if (ret)
639 return ret;
640 }
641
642 if (mutex_lock_interruptible(&le->read_lock))
643 return -ERESTARTSYS;
644 ret = kfifo_to_user(&le->events, buf, count, &copied);
645 mutex_unlock(&le->read_lock);
646
647 if (ret)
648 return ret;
649
650 /*
651 * If we couldn't read anything from the fifo (a different
652 * thread might have been faster) we either return -EAGAIN if
653 * the file descriptor is non-blocking, otherwise we go back to
654 * sleep and wait for more data to arrive.
655 */
656 if (copied == 0 && (filep->f_flags & O_NONBLOCK))
657 return -EAGAIN;
658
659 } while (copied == 0);
660
661 return copied;
662}
663
664static int lineevent_release(struct inode *inode, struct file *filep)
665{
666 struct lineevent_state *le = filep->private_data;
667 struct gpio_device *gdev = le->gdev;
668
669 free_irq(le->irq, le);
670 gpiod_free(le->desc);
671 kfree(le->label);
672 kfree(le);
673 put_device(&gdev->dev);
674 return 0;
675}
676
677static long lineevent_ioctl(struct file *filep, unsigned int cmd,
678 unsigned long arg)
679{
680 struct lineevent_state *le = filep->private_data;
681 void __user *ip = (void __user *)arg;
682 struct gpiohandle_data ghd;
683
684 /*
685 * We can get the value for an event line but not set it,
686 * because it is input by definition.
687 */
688 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
689 int val;
690
Lars-Peter Clausend82aa4a2016-10-18 16:54:04 +0200691 memset(&ghd, 0, sizeof(ghd));
692
Linus Walleij61f922d2016-06-02 11:30:15 +0200693 val = gpiod_get_value_cansleep(le->desc);
694 if (val < 0)
695 return val;
696 ghd.values[0] = val;
697
698 if (copy_to_user(ip, &ghd, sizeof(ghd)))
699 return -EFAULT;
700
701 return 0;
702 }
703 return -EINVAL;
704}
705
706#ifdef CONFIG_COMPAT
707static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd,
708 unsigned long arg)
709{
710 return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
711}
712#endif
713
714static const struct file_operations lineevent_fileops = {
715 .release = lineevent_release,
716 .read = lineevent_read,
717 .poll = lineevent_poll,
718 .owner = THIS_MODULE,
719 .llseek = noop_llseek,
720 .unlocked_ioctl = lineevent_ioctl,
721#ifdef CONFIG_COMPAT
722 .compat_ioctl = lineevent_ioctl_compat,
723#endif
724};
725
Ben Dooks33265b12016-06-17 16:03:13 +0100726static irqreturn_t lineevent_irq_thread(int irq, void *p)
Linus Walleij61f922d2016-06-02 11:30:15 +0200727{
728 struct lineevent_state *le = p;
729 struct gpioevent_data ge;
Bartosz Golaszewskidf1e76f2017-07-03 11:12:03 +0200730 int ret, level;
Linus Walleij61f922d2016-06-02 11:30:15 +0200731
732 ge.timestamp = ktime_get_real_ns();
Bartosz Golaszewskidf1e76f2017-07-03 11:12:03 +0200733 level = gpiod_get_value_cansleep(le->desc);
Linus Walleij61f922d2016-06-02 11:30:15 +0200734
Bartosz Golaszewskiad537b82017-06-23 13:45:16 +0200735 if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
736 && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200737 if (level)
738 /* Emit low-to-high event */
739 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
740 else
741 /* Emit high-to-low event */
742 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Bartosz Golaszewskidf1e76f2017-07-03 11:12:03 +0200743 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE && level) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200744 /* Emit low-to-high event */
745 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
Bartosz Golaszewskidf1e76f2017-07-03 11:12:03 +0200746 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE && !level) {
Linus Walleij61f922d2016-06-02 11:30:15 +0200747 /* Emit high-to-low event */
748 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Arnd Bergmannbc0207a2016-06-16 11:02:41 +0200749 } else {
750 return IRQ_NONE;
Linus Walleij61f922d2016-06-02 11:30:15 +0200751 }
752
753 ret = kfifo_put(&le->events, ge);
754 if (ret != 0)
755 wake_up_poll(&le->wait, POLLIN);
756
757 return IRQ_HANDLED;
758}
759
760static int lineevent_create(struct gpio_device *gdev, void __user *ip)
761{
762 struct gpioevent_request eventreq;
763 struct lineevent_state *le;
764 struct gpio_desc *desc;
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200765 struct file *file;
Linus Walleij61f922d2016-06-02 11:30:15 +0200766 u32 offset;
767 u32 lflags;
768 u32 eflags;
769 int fd;
770 int ret;
771 int irqflags = 0;
772
773 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
774 return -EFAULT;
775
776 le = kzalloc(sizeof(*le), GFP_KERNEL);
777 if (!le)
778 return -ENOMEM;
779 le->gdev = gdev;
780 get_device(&gdev->dev);
781
782 /* Make sure this is terminated */
783 eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
784 if (strlen(eventreq.consumer_label)) {
785 le->label = kstrdup(eventreq.consumer_label,
786 GFP_KERNEL);
787 if (!le->label) {
788 ret = -ENOMEM;
789 goto out_free_le;
790 }
791 }
792
793 offset = eventreq.lineoffset;
794 lflags = eventreq.handleflags;
795 eflags = eventreq.eventflags;
796
Lars-Peter Clausenb8b0e3d2016-10-18 16:54:03 +0200797 if (offset >= gdev->ngpio) {
798 ret = -EINVAL;
799 goto out_free_label;
800 }
801
Lars-Peter Clausenac7dbb92016-10-18 16:54:06 +0200802 /* Return an error if a unknown flag is set */
803 if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
804 (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) {
805 ret = -EINVAL;
806 goto out_free_label;
807 }
808
Linus Walleij61f922d2016-06-02 11:30:15 +0200809 /* This is just wrong: we don't look for events on output lines */
810 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
811 ret = -EINVAL;
812 goto out_free_label;
813 }
814
815 desc = &gdev->descs[offset];
816 ret = gpiod_request(desc, le->label);
817 if (ret)
818 goto out_free_desc;
819 le->desc = desc;
820 le->eflags = eflags;
821
822 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
823 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
824 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
825 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
826 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
827 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
828
829 ret = gpiod_direction_input(desc);
830 if (ret)
831 goto out_free_desc;
832
833 le->irq = gpiod_to_irq(desc);
834 if (le->irq <= 0) {
835 ret = -ENODEV;
836 goto out_free_desc;
837 }
838
839 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
840 irqflags |= IRQF_TRIGGER_RISING;
841 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
842 irqflags |= IRQF_TRIGGER_FALLING;
843 irqflags |= IRQF_ONESHOT;
844 irqflags |= IRQF_SHARED;
845
846 INIT_KFIFO(le->events);
847 init_waitqueue_head(&le->wait);
848 mutex_init(&le->read_lock);
849
850 /* Request a thread to read the events */
851 ret = request_threaded_irq(le->irq,
852 NULL,
853 lineevent_irq_thread,
854 irqflags,
855 le->label,
856 le);
857 if (ret)
858 goto out_free_desc;
859
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200860 fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
Linus Walleij61f922d2016-06-02 11:30:15 +0200861 if (fd < 0) {
862 ret = fd;
863 goto out_free_irq;
864 }
865
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200866 file = anon_inode_getfile("gpio-event",
867 &lineevent_fileops,
868 le,
869 O_RDONLY | O_CLOEXEC);
870 if (IS_ERR(file)) {
871 ret = PTR_ERR(file);
872 goto out_put_unused_fd;
873 }
874
Linus Walleij61f922d2016-06-02 11:30:15 +0200875 eventreq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200876 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200877 /*
878 * fput() will trigger the release() callback, so do not go onto
879 * the regular error cleanup path here.
880 */
881 fput(file);
882 put_unused_fd(fd);
883 return -EFAULT;
Linus Walleijd932cd42016-07-04 13:13:04 +0200884 }
Linus Walleij61f922d2016-06-02 11:30:15 +0200885
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200886 fd_install(fd, file);
887
Linus Walleij61f922d2016-06-02 11:30:15 +0200888 return 0;
889
Lars-Peter Clausen953b9562016-10-24 13:59:15 +0200890out_put_unused_fd:
891 put_unused_fd(fd);
Linus Walleij61f922d2016-06-02 11:30:15 +0200892out_free_irq:
893 free_irq(le->irq, le);
894out_free_desc:
895 gpiod_free(le->desc);
896out_free_label:
897 kfree(le->label);
898out_free_le:
899 kfree(le);
900 put_device(&gdev->dev);
901 return ret;
902}
903
Thierry Reding950d55f52017-07-24 16:57:22 +0200904/*
Linus Walleij3c702e92015-10-21 15:29:53 +0200905 * gpio_ioctl() - ioctl handler for the GPIO chardev
906 */
907static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
908{
909 struct gpio_device *gdev = filp->private_data;
910 struct gpio_chip *chip = gdev->chip;
Linus Walleij8b92e172016-05-27 14:24:04 +0200911 void __user *ip = (void __user *)arg;
Linus Walleij3c702e92015-10-21 15:29:53 +0200912
913 /* We fail any subsequent ioctl():s when the chip is gone */
914 if (!chip)
915 return -ENODEV;
916
Linus Walleij521a2ad2016-02-12 22:25:22 +0100917 /* Fill in the struct and pass to userspace */
Linus Walleij3c702e92015-10-21 15:29:53 +0200918 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
Linus Walleij521a2ad2016-02-12 22:25:22 +0100919 struct gpiochip_info chipinfo;
920
Lars-Peter Clausen0f4bbb22016-10-18 16:54:00 +0200921 memset(&chipinfo, 0, sizeof(chipinfo));
922
Linus Walleij3c702e92015-10-21 15:29:53 +0200923 strncpy(chipinfo.name, dev_name(&gdev->dev),
924 sizeof(chipinfo.name));
925 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
Linus Walleijdf4878e2016-02-12 14:48:23 +0100926 strncpy(chipinfo.label, gdev->label,
927 sizeof(chipinfo.label));
928 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100929 chipinfo.lines = gdev->ngpio;
Linus Walleij3c702e92015-10-21 15:29:53 +0200930 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
931 return -EFAULT;
932 return 0;
Linus Walleij521a2ad2016-02-12 22:25:22 +0100933 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
934 struct gpioline_info lineinfo;
935 struct gpio_desc *desc;
936
937 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
938 return -EFAULT;
Lars-Peter Clausen1f1cc452016-10-18 16:53:59 +0200939 if (lineinfo.line_offset >= gdev->ngpio)
Linus Walleij521a2ad2016-02-12 22:25:22 +0100940 return -EINVAL;
941
942 desc = &gdev->descs[lineinfo.line_offset];
943 if (desc->name) {
944 strncpy(lineinfo.name, desc->name,
945 sizeof(lineinfo.name));
946 lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
947 } else {
948 lineinfo.name[0] = '\0';
949 }
950 if (desc->label) {
Linus Walleij214338e2016-02-25 21:01:48 +0100951 strncpy(lineinfo.consumer, desc->label,
952 sizeof(lineinfo.consumer));
953 lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +0100954 } else {
Linus Walleij214338e2016-02-25 21:01:48 +0100955 lineinfo.consumer[0] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +0100956 }
957
958 /*
959 * Userspace only need to know that the kernel is using
960 * this GPIO so it can't use it.
961 */
962 lineinfo.flags = 0;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100963 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
964 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
965 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
966 test_bit(FLAG_EXPORT, &desc->flags) ||
967 test_bit(FLAG_SYSFS, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100968 lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100969 if (test_bit(FLAG_IS_OUT, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100970 lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100971 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100972 lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100973 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100974 lineinfo.flags |= GPIOLINE_FLAG_OPEN_DRAIN;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100975 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100976 lineinfo.flags |= GPIOLINE_FLAG_OPEN_SOURCE;
977
978 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
979 return -EFAULT;
980 return 0;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200981 } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
982 return linehandle_create(gdev, ip);
Linus Walleij61f922d2016-06-02 11:30:15 +0200983 } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
984 return lineevent_create(gdev, ip);
Linus Walleij3c702e92015-10-21 15:29:53 +0200985 }
986 return -EINVAL;
987}
988
Linus Walleij8b92e172016-05-27 14:24:04 +0200989#ifdef CONFIG_COMPAT
990static long gpio_ioctl_compat(struct file *filp, unsigned int cmd,
991 unsigned long arg)
992{
993 return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
994}
995#endif
996
Linus Walleij3c702e92015-10-21 15:29:53 +0200997/**
998 * gpio_chrdev_open() - open the chardev for ioctl operations
999 * @inode: inode for this chardev
1000 * @filp: file struct for storing private data
1001 * Returns 0 on success
1002 */
1003static int gpio_chrdev_open(struct inode *inode, struct file *filp)
1004{
1005 struct gpio_device *gdev = container_of(inode->i_cdev,
1006 struct gpio_device, chrdev);
1007
1008 /* Fail on open if the backing gpiochip is gone */
Stephen Boydfb505742017-01-09 11:47:44 -08001009 if (!gdev->chip)
Linus Walleij3c702e92015-10-21 15:29:53 +02001010 return -ENODEV;
1011 get_device(&gdev->dev);
1012 filp->private_data = gdev;
Lars-Peter Clausenf4e81c52016-11-30 13:05:21 +01001013
1014 return nonseekable_open(inode, filp);
Linus Walleij3c702e92015-10-21 15:29:53 +02001015}
1016
1017/**
1018 * gpio_chrdev_release() - close chardev after ioctl operations
1019 * @inode: inode for this chardev
1020 * @filp: file struct for storing private data
1021 * Returns 0 on success
1022 */
1023static int gpio_chrdev_release(struct inode *inode, struct file *filp)
1024{
1025 struct gpio_device *gdev = container_of(inode->i_cdev,
1026 struct gpio_device, chrdev);
1027
Linus Walleij3c702e92015-10-21 15:29:53 +02001028 put_device(&gdev->dev);
1029 return 0;
1030}
1031
1032
1033static const struct file_operations gpio_fileops = {
1034 .release = gpio_chrdev_release,
1035 .open = gpio_chrdev_open,
1036 .owner = THIS_MODULE,
Lars-Peter Clausenf4e81c52016-11-30 13:05:21 +01001037 .llseek = no_llseek,
Linus Walleij3c702e92015-10-21 15:29:53 +02001038 .unlocked_ioctl = gpio_ioctl,
Linus Walleij8b92e172016-05-27 14:24:04 +02001039#ifdef CONFIG_COMPAT
1040 .compat_ioctl = gpio_ioctl_compat,
1041#endif
Linus Walleij3c702e92015-10-21 15:29:53 +02001042};
1043
Linus Walleijff2b1352015-10-20 11:10:38 +02001044static void gpiodevice_release(struct device *dev)
1045{
1046 struct gpio_device *gdev = dev_get_drvdata(dev);
1047
1048 list_del(&gdev->list);
1049 ida_simple_remove(&gpio_ida, gdev->id);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001050 kfree(gdev->label);
1051 kfree(gdev->descs);
Linus Walleij9efd9e62016-02-09 14:27:42 +01001052 kfree(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001053}
1054
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001055static int gpiochip_setup_dev(struct gpio_device *gdev)
1056{
1057 int status;
1058
1059 cdev_init(&gdev->chrdev, &gpio_fileops);
1060 gdev->chrdev.owner = THIS_MODULE;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001061 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001062
1063 status = cdev_device_add(&gdev->chrdev, &gdev->dev);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001064 if (status)
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001065 return status;
1066
1067 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
1068 MAJOR(gpio_devt), gdev->id);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001069
1070 status = gpiochip_sysfs_register(gdev);
1071 if (status)
1072 goto err_remove_device;
1073
1074 /* From this point, the .release() function cleans up gpio_device */
1075 gdev->dev.release = gpiodevice_release;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001076 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
1077 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
1078 dev_name(&gdev->dev), gdev->chip->label ? : "generic");
1079
1080 return 0;
1081
1082err_remove_device:
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001083 cdev_device_del(&gdev->chrdev, &gdev->dev);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001084 return status;
1085}
1086
1087static void gpiochip_setup_devs(void)
1088{
1089 struct gpio_device *gdev;
1090 int err;
1091
1092 list_for_each_entry(gdev, &gpio_devices, list) {
1093 err = gpiochip_setup_dev(gdev);
1094 if (err)
1095 pr_err("%s: Failed to initialize gpio device (%d)\n",
1096 dev_name(&gdev->dev), err);
1097 }
1098}
1099
Anton Vorontsov169b6a72008-04-28 02:14:47 -07001100/**
Linus Walleijb08ea352015-12-03 15:14:13 +01001101 * gpiochip_add_data() - register a gpio_chip
David Brownelld2876d02008-02-04 22:28:20 -08001102 * @chip: the chip to register, with chip->base initialized
Thierry Reding950d55f52017-07-24 16:57:22 +02001103 * @data: driver-private data associated with this chip
David Brownelld2876d02008-02-04 22:28:20 -08001104 *
Thierry Reding950d55f52017-07-24 16:57:22 +02001105 * Context: potentially before irqs will work
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001106 *
Linus Walleijb08ea352015-12-03 15:14:13 +01001107 * When gpiochip_add_data() is called very early during boot, so that GPIOs
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08001108 * can be freely used, the chip->parent device must be registered before
David Brownelld8f388d82008-07-25 01:46:07 -07001109 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1110 * for GPIOs will fail rudely.
1111 *
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001112 * gpiochip_add_data() must only be called after gpiolib initialization,
1113 * ie after core_initcall().
1114 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001115 * If chip->base is negative, this requests dynamic assignment of
1116 * a range of valid GPIOs.
Thierry Reding950d55f52017-07-24 16:57:22 +02001117 *
1118 * Returns:
1119 * A negative errno if the chip can't be registered, such as because the
1120 * chip->base is invalid or already associated with a different chip.
1121 * Otherwise it returns zero as a success code.
David Brownelld2876d02008-02-04 22:28:20 -08001122 */
Linus Walleijb08ea352015-12-03 15:14:13 +01001123int gpiochip_add_data(struct gpio_chip *chip, void *data)
David Brownelld2876d02008-02-04 22:28:20 -08001124{
1125 unsigned long flags;
1126 int status = 0;
Linus Walleijff2b1352015-10-20 11:10:38 +02001127 unsigned i;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001128 int base = chip->base;
Linus Walleijff2b1352015-10-20 11:10:38 +02001129 struct gpio_device *gdev;
David Brownelld2876d02008-02-04 22:28:20 -08001130
Linus Walleijff2b1352015-10-20 11:10:38 +02001131 /*
1132 * First: allocate and populate the internal stat container, and
1133 * set up the struct device.
1134 */
Josh Cartwright969f07b2016-02-17 16:44:15 -06001135 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
Linus Walleijff2b1352015-10-20 11:10:38 +02001136 if (!gdev)
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001137 return -ENOMEM;
Linus Walleij3c702e92015-10-21 15:29:53 +02001138 gdev->dev.bus = &gpio_bus_type;
Linus Walleijff2b1352015-10-20 11:10:38 +02001139 gdev->chip = chip;
1140 chip->gpiodev = gdev;
1141 if (chip->parent) {
1142 gdev->dev.parent = chip->parent;
1143 gdev->dev.of_node = chip->parent->of_node;
Thierry Redingacc6e332016-07-05 14:11:14 +02001144 }
1145
Linus Walleijff2b1352015-10-20 11:10:38 +02001146#ifdef CONFIG_OF_GPIO
1147 /* If the gpiochip has an assigned OF node this takes precedence */
Thierry Redingacc6e332016-07-05 14:11:14 +02001148 if (chip->of_node)
1149 gdev->dev.of_node = chip->of_node;
Linus Walleijff2b1352015-10-20 11:10:38 +02001150#endif
Thierry Redingacc6e332016-07-05 14:11:14 +02001151
Linus Walleijff2b1352015-10-20 11:10:38 +02001152 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
1153 if (gdev->id < 0) {
1154 status = gdev->id;
1155 goto err_free_gdev;
1156 }
1157 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
1158 device_initialize(&gdev->dev);
1159 dev_set_drvdata(&gdev->dev, gdev);
1160 if (chip->parent && chip->parent->driver)
1161 gdev->owner = chip->parent->driver->owner;
1162 else if (chip->owner)
1163 /* TODO: remove chip->owner */
1164 gdev->owner = chip->owner;
1165 else
1166 gdev->owner = THIS_MODULE;
David Brownelld2876d02008-02-04 22:28:20 -08001167
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001168 gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001169 if (!gdev->descs) {
Linus Walleijff2b1352015-10-20 11:10:38 +02001170 status = -ENOMEM;
1171 goto err_free_gdev;
1172 }
1173
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001174 if (chip->ngpio == 0) {
1175 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
Linus Walleijff2b1352015-10-20 11:10:38 +02001176 status = -EINVAL;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001177 goto err_free_descs;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001178 }
Linus Walleijdf4878e2016-02-12 14:48:23 +01001179
1180 if (chip->label)
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001181 gdev->label = kstrdup(chip->label, GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001182 else
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001183 gdev->label = kstrdup("unknown", GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001184 if (!gdev->label) {
1185 status = -ENOMEM;
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001186 goto err_free_descs;
Linus Walleijdf4878e2016-02-12 14:48:23 +01001187 }
1188
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001189 gdev->ngpio = chip->ngpio;
Linus Walleij43c54ec2016-02-11 11:37:48 +01001190 gdev->data = data;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001191
David Brownelld2876d02008-02-04 22:28:20 -08001192 spin_lock_irqsave(&gpio_lock, flags);
1193
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001194 /*
1195 * TODO: this allocates a Linux GPIO number base in the global
1196 * GPIO numberspace for this chip. In the long run we want to
1197 * get *rid* of this numberspace and use only descriptors, but
1198 * it may be a pipe dream. It will not happen before we get rid
1199 * of the sysfs interface anyways.
1200 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001201 if (base < 0) {
1202 base = gpiochip_find_base(chip->ngpio);
1203 if (base < 0) {
1204 status = base;
Johan Hovold225fce82015-01-12 17:12:25 +01001205 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001206 goto err_free_label;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001207 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001208 /*
1209 * TODO: it should not be necessary to reflect the assigned
1210 * base outside of the GPIO subsystem. Go over drivers and
1211 * see if anyone makes use of this, else drop this and assign
1212 * a poison instead.
1213 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001214 chip->base = base;
1215 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001216 gdev->base = base;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001217
Linus Walleijff2b1352015-10-20 11:10:38 +02001218 status = gpiodev_add_to_list(gdev);
Johan Hovold05aa5202015-01-12 17:12:26 +01001219 if (status) {
1220 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001221 goto err_free_label;
Johan Hovold05aa5202015-01-12 17:12:26 +01001222 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001223
Linus Walleij545ebd92016-05-30 17:11:59 +02001224 spin_unlock_irqrestore(&gpio_lock, flags);
1225
Linus Walleijff2b1352015-10-20 11:10:38 +02001226 for (i = 0; i < chip->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001227 struct gpio_desc *desc = &gdev->descs[i];
David Brownelld8f388d82008-07-25 01:46:07 -07001228
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001229 desc->gdev = gdev;
Linus Walleij72d32002016-04-28 13:33:59 +02001230 /*
1231 * REVISIT: most hardware initializes GPIOs as inputs
1232 * (often with pullups enabled) so power usage is
1233 * minimized. Linux code should set the gpio direction
1234 * first thing; but until it does, and in case
1235 * chip->get_direction is not set, we may expose the
1236 * wrong direction in sysfs.
Johan Hovold05aa5202015-01-12 17:12:26 +01001237 */
Linus Walleij72d32002016-04-28 13:33:59 +02001238
1239 if (chip->get_direction) {
1240 /*
1241 * If we have .get_direction, set up the initial
1242 * direction flag from the hardware.
1243 */
1244 int dir = chip->get_direction(chip, i);
1245
1246 if (!dir)
1247 set_bit(FLAG_IS_OUT, &desc->flags);
1248 } else if (!chip->direction_input) {
1249 /*
1250 * If the chip lacks the .direction_input callback
1251 * we logically assume all lines are outputs.
1252 */
1253 set_bit(FLAG_IS_OUT, &desc->flags);
1254 }
David Brownelld2876d02008-02-04 22:28:20 -08001255 }
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001256
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301257#ifdef CONFIG_PINCTRL
Linus Walleij20ec3e32016-02-11 11:03:06 +01001258 INIT_LIST_HEAD(&gdev->pin_ranges);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301259#endif
1260
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001261 status = gpiochip_set_desc_names(chip);
1262 if (status)
1263 goto err_remove_from_list;
1264
Mika Westerberg79b804c2016-09-20 15:15:21 +03001265 status = gpiochip_irqchip_init_valid_mask(chip);
1266 if (status)
1267 goto err_remove_from_list;
1268
Tomeu Vizoso28355f82015-07-14 10:29:54 +02001269 status = of_gpiochip_add(chip);
1270 if (status)
1271 goto err_remove_chip;
1272
Mika Westerberg664e3e52014-01-08 12:40:54 +02001273 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001274
Linus Walleij3c702e92015-10-21 15:29:53 +02001275 /*
1276 * By first adding the chardev, and then adding the device,
1277 * we get a device node entry in sysfs under
1278 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1279 * coldplug of device nodes and other udev business.
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001280 * We can do this only if gpiolib has been initialized.
1281 * Otherwise, defer until later.
Linus Walleij3c702e92015-10-21 15:29:53 +02001282 */
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001283 if (gpiolib_initialized) {
1284 status = gpiochip_setup_dev(gdev);
1285 if (status)
1286 goto err_remove_chip;
1287 }
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001288 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001289
Johan Hovold225fce82015-01-12 17:12:25 +01001290err_remove_chip:
1291 acpi_gpiochip_remove(chip);
Johan Hovold6d867502015-05-04 17:23:25 +02001292 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +01001293 of_gpiochip_remove(chip);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001294 gpiochip_irqchip_free_valid_mask(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001295err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +01001296 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001297 list_del(&gdev->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001298 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001299err_free_label:
1300 kfree(gdev->label);
1301err_free_descs:
1302 kfree(gdev->descs);
Linus Walleijff2b1352015-10-20 11:10:38 +02001303err_free_gdev:
1304 ida_simple_remove(&gpio_ida, gdev->id);
David Brownelld2876d02008-02-04 22:28:20 -08001305 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +02001306 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001307 gdev->base, gdev->base + gdev->ngpio - 1,
1308 chip->label ? : "generic");
1309 kfree(gdev);
David Brownelld2876d02008-02-04 22:28:20 -08001310 return status;
1311}
Linus Walleijb08ea352015-12-03 15:14:13 +01001312EXPORT_SYMBOL_GPL(gpiochip_add_data);
David Brownelld2876d02008-02-04 22:28:20 -08001313
1314/**
Linus Walleij43c54ec2016-02-11 11:37:48 +01001315 * gpiochip_get_data() - get per-subdriver data for the chip
Thierry Reding950d55f52017-07-24 16:57:22 +02001316 * @chip: GPIO chip
1317 *
1318 * Returns:
1319 * The per-subdriver data for the chip.
Linus Walleij43c54ec2016-02-11 11:37:48 +01001320 */
1321void *gpiochip_get_data(struct gpio_chip *chip)
1322{
1323 return chip->gpiodev->data;
1324}
1325EXPORT_SYMBOL_GPL(gpiochip_get_data);
1326
1327/**
David Brownelld2876d02008-02-04 22:28:20 -08001328 * gpiochip_remove() - unregister a gpio_chip
1329 * @chip: the chip to unregister
1330 *
1331 * A gpio_chip with any GPIOs still requested may not be removed.
1332 */
abdoulaye berthee1db1702014-07-05 18:28:50 +02001333void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -08001334{
Linus Walleijff2b1352015-10-20 11:10:38 +02001335 struct gpio_device *gdev = chip->gpiodev;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001336 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001337 unsigned long flags;
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001338 unsigned i;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001339 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -08001340
Linus Walleijff2b1352015-10-20 11:10:38 +02001341 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
Linus Walleijafbc4f32016-02-09 13:21:06 +01001342 gpiochip_sysfs_unregister(gdev);
Geert Uytterhoeven5018ada2016-12-19 18:29:23 +01001343 gpiochip_free_hogs(chip);
Bamvor Jian Zhangbd203bd2016-02-20 13:13:19 +08001344 /* Numb the device, cancelling all outstanding operations */
1345 gdev->chip = NULL;
Johan Hovold00acc3d2015-01-12 17:12:27 +01001346 gpiochip_irqchip_remove(chip);
Mika Westerberg6072b9d2014-03-10 14:54:53 +02001347 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +01001348 gpiochip_remove_pin_ranges(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001349 of_gpiochip_remove(chip);
Linus Walleij43c54ec2016-02-11 11:37:48 +01001350 /*
1351 * We accept no more calls into the driver from this point, so
1352 * NULL the driver data pointer
1353 */
1354 gdev->data = NULL;
Anton Vorontsov391c9702010-06-08 07:48:17 -06001355
Johan Hovold6798aca2015-01-12 17:12:28 +01001356 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001357 for (i = 0; i < gdev->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001358 desc = &gdev->descs[i];
Johan Hovoldfab28b82015-05-04 17:10:27 +02001359 if (test_bit(FLAG_REQUESTED, &desc->flags))
1360 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -08001361 }
David Brownelld2876d02008-02-04 22:28:20 -08001362 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001363
Johan Hovoldfab28b82015-05-04 17:10:27 +02001364 if (requested)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001365 dev_crit(&gdev->dev,
Linus Walleij58383c782015-11-04 09:56:26 +01001366 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +02001367
Linus Walleijff2b1352015-10-20 11:10:38 +02001368 /*
1369 * The gpiochip side puts its use of the device to rest here:
1370 * if there are no userspace clients, the chardev and device will
1371 * be removed, else it will be dangling until the last user is
1372 * gone.
1373 */
Logan Gunthorpe111379d2017-03-17 12:48:12 -06001374 cdev_device_del(&gdev->chrdev, &gdev->dev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001375 put_device(&gdev->dev);
David Brownelld2876d02008-02-04 22:28:20 -08001376}
1377EXPORT_SYMBOL_GPL(gpiochip_remove);
1378
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301379static void devm_gpio_chip_release(struct device *dev, void *res)
1380{
1381 struct gpio_chip *chip = *(struct gpio_chip **)res;
1382
1383 gpiochip_remove(chip);
1384}
1385
1386static int devm_gpio_chip_match(struct device *dev, void *res, void *data)
1387
1388{
1389 struct gpio_chip **r = res;
1390
1391 if (!r || !*r) {
1392 WARN_ON(!r || !*r);
1393 return 0;
1394 }
1395
1396 return *r == data;
1397}
1398
1399/**
1400 * devm_gpiochip_add_data() - Resource manager piochip_add_data()
1401 * @dev: the device pointer on which irq_chip belongs to.
1402 * @chip: the chip to register, with chip->base initialized
Thierry Reding950d55f52017-07-24 16:57:22 +02001403 * @data: driver-private data associated with this chip
1404 *
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301405 * Context: potentially before irqs will work
1406 *
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301407 * The gpio chip automatically be released when the device is unbound.
Thierry Reding950d55f52017-07-24 16:57:22 +02001408 *
1409 * Returns:
1410 * A negative errno if the chip can't be registered, such as because the
1411 * chip->base is invalid or already associated with a different chip.
1412 * Otherwise it returns zero as a success code.
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301413 */
1414int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
1415 void *data)
1416{
1417 struct gpio_chip **ptr;
1418 int ret;
1419
1420 ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
1421 GFP_KERNEL);
1422 if (!ptr)
1423 return -ENOMEM;
1424
1425 ret = gpiochip_add_data(chip, data);
1426 if (ret < 0) {
1427 devres_free(ptr);
1428 return ret;
1429 }
1430
1431 *ptr = chip;
1432 devres_add(dev, ptr);
1433
1434 return 0;
1435}
1436EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
1437
1438/**
1439 * devm_gpiochip_remove() - Resource manager of gpiochip_remove()
1440 * @dev: device for which which resource was allocated
1441 * @chip: the chip to remove
1442 *
1443 * A gpio_chip with any GPIOs still requested may not be removed.
1444 */
1445void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip)
1446{
1447 int ret;
1448
1449 ret = devres_release(dev, devm_gpio_chip_release,
1450 devm_gpio_chip_match, chip);
Christophe JAILLET3988d662017-01-27 12:56:42 +01001451 WARN_ON(ret);
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301452}
1453EXPORT_SYMBOL_GPL(devm_gpiochip_remove);
1454
Grant Likely594fa262010-06-08 07:48:16 -06001455/**
1456 * gpiochip_find() - iterator for locating a specific gpio_chip
1457 * @data: data to pass to match function
Thierry Reding950d55f52017-07-24 16:57:22 +02001458 * @match: Callback function to check gpio_chip
Grant Likely594fa262010-06-08 07:48:16 -06001459 *
1460 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1461 * determined by a user supplied @match callback. The callback should return
1462 * 0 if the device doesn't match and non-zero if it does. If the callback is
1463 * non-zero, this function will return to the caller and not iterate over any
1464 * more gpio_chips.
1465 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001466struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001467 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001468 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001469{
Linus Walleijff2b1352015-10-20 11:10:38 +02001470 struct gpio_device *gdev;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001471 struct gpio_chip *chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -06001472 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001473
1474 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001475 list_for_each_entry(gdev, &gpio_devices, list)
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001476 if (gdev->chip && match(gdev->chip, data)) {
1477 chip = gdev->chip;
Grant Likely594fa262010-06-08 07:48:16 -06001478 break;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001479 }
Linus Walleijff2b1352015-10-20 11:10:38 +02001480
Grant Likely594fa262010-06-08 07:48:16 -06001481 spin_unlock_irqrestore(&gpio_lock, flags);
1482
1483 return chip;
1484}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001485EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001486
Alexandre Courbot79697ef2013-11-16 21:39:32 +09001487static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1488{
1489 const char *name = data;
1490
1491 return !strcmp(chip->label, name);
1492}
1493
1494static struct gpio_chip *find_chip_by_name(const char *name)
1495{
1496 return gpiochip_find((void *)name, gpiochip_match_name);
1497}
1498
Linus Walleij14250522014-03-25 10:40:18 +01001499#ifdef CONFIG_GPIOLIB_IRQCHIP
1500
1501/*
1502 * The following is irqchip helper code for gpiochips.
1503 */
1504
Mika Westerberg79b804c2016-09-20 15:15:21 +03001505static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1506{
Mika Westerberg79b804c2016-09-20 15:15:21 +03001507 if (!gpiochip->irq_need_valid_mask)
1508 return 0;
1509
1510 gpiochip->irq_valid_mask = kcalloc(BITS_TO_LONGS(gpiochip->ngpio),
1511 sizeof(long), GFP_KERNEL);
1512 if (!gpiochip->irq_valid_mask)
1513 return -ENOMEM;
1514
1515 /* Assume by default all GPIOs are valid */
Andy Shevchenko923a6542017-05-25 16:08:38 +03001516 bitmap_fill(gpiochip->irq_valid_mask, gpiochip->ngpio);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001517
1518 return 0;
1519}
1520
1521static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1522{
1523 kfree(gpiochip->irq_valid_mask);
1524 gpiochip->irq_valid_mask = NULL;
1525}
1526
1527static bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
1528 unsigned int offset)
1529{
1530 /* No mask means all valid */
1531 if (likely(!gpiochip->irq_valid_mask))
1532 return true;
1533 return test_bit(offset, gpiochip->irq_valid_mask);
1534}
1535
Linus Walleij14250522014-03-25 10:40:18 +01001536/**
Linus Walleijd245b3f2016-11-24 10:57:25 +01001537 * gpiochip_set_cascaded_irqchip() - connects a cascaded irqchip to a gpiochip
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001538 * @gpiochip: the gpiochip to set the irqchip chain to
1539 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01001540 * @parent_irq: the irq number corresponding to the parent IRQ for this
1541 * chained irqchip
1542 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001543 * coming out of the gpiochip. If the interrupt is nested rather than
1544 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +01001545 */
Linus Walleijd245b3f2016-11-24 10:57:25 +01001546static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gpiochip,
1547 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001548 unsigned int parent_irq,
Linus Walleijd245b3f2016-11-24 10:57:25 +01001549 irq_flow_handler_t parent_handler)
Linus Walleij14250522014-03-25 10:40:18 +01001550{
Linus Walleij83141a72014-09-26 13:50:12 +02001551 unsigned int offset;
1552
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001553 if (!gpiochip->irq.domain) {
Linus Walleij83141a72014-09-26 13:50:12 +02001554 chip_err(gpiochip, "called %s before setting up irqchip\n",
1555 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +02001556 return;
1557 }
1558
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001559 if (parent_handler) {
1560 if (gpiochip->can_sleep) {
1561 chip_err(gpiochip,
1562 "you cannot have chained interrupts on a "
1563 "chip that may sleep\n");
1564 return;
1565 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001566 /*
1567 * The parent irqchip is already using the chip_data for this
1568 * irqchip, so our callbacks simply use the handler_data.
1569 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +02001570 irq_set_chained_handler_and_data(parent_irq, parent_handler,
1571 gpiochip);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001572
Linus Walleijd245b3f2016-11-24 10:57:25 +01001573 gpiochip->irq_chained_parent = parent_irq;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001574 }
Linus Walleij83141a72014-09-26 13:50:12 +02001575
1576 /* Set the parent IRQ for all affected IRQs */
Mika Westerberg79b804c2016-09-20 15:15:21 +03001577 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1578 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1579 continue;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001580 irq_set_parent(irq_find_mapping(gpiochip->irq.domain, offset),
Linus Walleij83141a72014-09-26 13:50:12 +02001581 parent_irq);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001582 }
Linus Walleij14250522014-03-25 10:40:18 +01001583}
Linus Walleijd245b3f2016-11-24 10:57:25 +01001584
1585/**
1586 * gpiochip_set_chained_irqchip() - connects a chained irqchip to a gpiochip
1587 * @gpiochip: the gpiochip to set the irqchip chain to
1588 * @irqchip: the irqchip to chain to the gpiochip
1589 * @parent_irq: the irq number corresponding to the parent IRQ for this
1590 * chained irqchip
1591 * @parent_handler: the parent interrupt handler for the accumulated IRQ
1592 * coming out of the gpiochip. If the interrupt is nested rather than
1593 * cascaded, pass NULL in this handler argument
1594 */
1595void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
1596 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001597 unsigned int parent_irq,
Linus Walleijd245b3f2016-11-24 10:57:25 +01001598 irq_flow_handler_t parent_handler)
1599{
1600 gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
1601 parent_handler);
1602}
Linus Walleij14250522014-03-25 10:40:18 +01001603EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
1604
1605/**
Linus Walleijd245b3f2016-11-24 10:57:25 +01001606 * gpiochip_set_nested_irqchip() - connects a nested irqchip to a gpiochip
1607 * @gpiochip: the gpiochip to set the irqchip nested handler to
1608 * @irqchip: the irqchip to nest to the gpiochip
1609 * @parent_irq: the irq number corresponding to the parent IRQ for this
1610 * nested irqchip
1611 */
1612void gpiochip_set_nested_irqchip(struct gpio_chip *gpiochip,
1613 struct irq_chip *irqchip,
Thierry Reding6f793092017-04-03 18:05:21 +02001614 unsigned int parent_irq)
Linus Walleijd245b3f2016-11-24 10:57:25 +01001615{
1616 if (!gpiochip->irq_nested) {
1617 chip_err(gpiochip, "tried to nest a chained gpiochip\n");
1618 return;
1619 }
1620 gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
1621 NULL);
1622}
1623EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip);
1624
1625/**
Linus Walleij14250522014-03-25 10:40:18 +01001626 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1627 * @d: the irqdomain used by this irqchip
1628 * @irq: the global irq number used by this GPIO irqchip irq
1629 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1630 *
1631 * This function will set up the mapping for a certain IRQ line on a
1632 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1633 * stored inside the gpiochip.
1634 */
1635static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1636 irq_hw_number_t hwirq)
1637{
1638 struct gpio_chip *chip = d->host_data;
1639
Grygorii Strashkodc749a02017-07-21 11:49:00 -05001640 if (!gpiochip_irqchip_irq_valid(chip, hwirq))
1641 return -ENXIO;
1642
Linus Walleij14250522014-03-25 10:40:18 +01001643 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001644 /*
1645 * This lock class tells lockdep that GPIO irqs are in a different
1646 * category than their parents, so it won't report false recursion.
1647 */
1648 irq_set_lockdep_class(irq, chip->lock_key);
Thierry Redingc7a0aa52017-11-07 19:15:48 +01001649 irq_set_chip_and_handler(irq, chip->irq.chip, chip->irq.handler);
Linus Walleijd245b3f2016-11-24 10:57:25 +01001650 /* Chips that use nested thread handlers have them marked */
1651 if (chip->irq_nested)
Linus Walleij1c8732b2014-04-09 13:34:39 +02001652 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +01001653 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -05001654
Linus Walleij1333b902014-04-23 16:45:12 +02001655 /*
1656 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1657 * is passed as default type.
1658 */
Thierry Reding3634eeb2017-11-07 19:15:49 +01001659 if (chip->irq.default_type != IRQ_TYPE_NONE)
1660 irq_set_irq_type(irq, chip->irq.default_type);
Linus Walleij14250522014-03-25 10:40:18 +01001661
1662 return 0;
1663}
1664
Linus Walleijc3626fd2014-03-28 20:42:01 +01001665static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
1666{
Linus Walleij1c8732b2014-04-09 13:34:39 +02001667 struct gpio_chip *chip = d->host_data;
1668
Linus Walleijd245b3f2016-11-24 10:57:25 +01001669 if (chip->irq_nested)
Linus Walleij1c8732b2014-04-09 13:34:39 +02001670 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001671 irq_set_chip_and_handler(irq, NULL, NULL);
1672 irq_set_chip_data(irq, NULL);
1673}
1674
Linus Walleij14250522014-03-25 10:40:18 +01001675static const struct irq_domain_ops gpiochip_domain_ops = {
1676 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +01001677 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +01001678 /* Virtually all GPIO irqchips are twocell:ed */
1679 .xlate = irq_domain_xlate_twocell,
1680};
1681
1682static int gpiochip_irq_reqres(struct irq_data *d)
1683{
1684 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1685
Linus Walleijff2b1352015-10-20 11:10:38 +02001686 if (!try_module_get(chip->gpiodev->owner))
Grygorii Strashko5b76e792015-06-25 20:30:50 +03001687 return -ENODEV;
1688
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001689 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +01001690 chip_err(chip,
1691 "unable to lock HW IRQ %lu for IRQ\n",
1692 d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001693 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001694 return -EINVAL;
1695 }
1696 return 0;
1697}
1698
1699static void gpiochip_irq_relres(struct irq_data *d)
1700{
1701 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1702
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001703 gpiochip_unlock_as_irq(chip, d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001704 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001705}
1706
1707static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
1708{
Grygorii Strashkodc749a02017-07-21 11:49:00 -05001709 if (!gpiochip_irqchip_irq_valid(chip, offset))
1710 return -ENXIO;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001711 return irq_create_mapping(chip->irq.domain, offset);
Linus Walleij14250522014-03-25 10:40:18 +01001712}
1713
1714/**
1715 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1716 * @gpiochip: the gpiochip to remove the irqchip from
1717 *
1718 * This is called only from gpiochip_remove()
1719 */
1720static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
1721{
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001722 unsigned int offset, irq;
Linus Walleijc3626fd2014-03-28 20:42:01 +01001723
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001724 acpi_gpiochip_free_interrupts(gpiochip);
1725
Linus Walleijd245b3f2016-11-24 10:57:25 +01001726 if (gpiochip->irq_chained_parent) {
Martin Kaiser5048f0a2017-10-18 18:32:47 +02001727 irq_set_chained_handler_and_data(
1728 gpiochip->irq_chained_parent, NULL, NULL);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001729 }
1730
Linus Walleijc3626fd2014-03-28 20:42:01 +01001731 /* Remove all IRQ mappings and delete the domain */
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001732 if (gpiochip->irq.domain) {
Mika Westerberg79b804c2016-09-20 15:15:21 +03001733 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1734 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1735 continue;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001736
1737 irq = irq_find_mapping(gpiochip->irq.domain, offset);
1738 irq_dispose_mapping(irq);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001739 }
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001740
1741 irq_domain_remove(gpiochip->irq.domain);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001742 }
Linus Walleij14250522014-03-25 10:40:18 +01001743
Thierry Redingda80ff82017-11-07 19:15:46 +01001744 if (gpiochip->irq.chip) {
1745 gpiochip->irq.chip->irq_request_resources = NULL;
1746 gpiochip->irq.chip->irq_release_resources = NULL;
1747 gpiochip->irq.chip = NULL;
Linus Walleij14250522014-03-25 10:40:18 +01001748 }
Mika Westerberg79b804c2016-09-20 15:15:21 +03001749
1750 gpiochip_irqchip_free_valid_mask(gpiochip);
Linus Walleij14250522014-03-25 10:40:18 +01001751}
1752
1753/**
Linus Walleij739e6f52017-01-11 13:37:07 +01001754 * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01001755 * @gpiochip: the gpiochip to add the irqchip to
1756 * @irqchip: the irqchip to add to the gpiochip
1757 * @first_irq: if not dynamically assigned, the base (first) IRQ to
1758 * allocate gpiochip irqs from
1759 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +02001760 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
1761 * to have the core avoid setting up any default type in the hardware.
Linus Walleijd245b3f2016-11-24 10:57:25 +01001762 * @nested: whether this is a nested irqchip calling handle_nested_irq()
1763 * in its IRQ handler
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001764 * @lock_key: lockdep class
Linus Walleij14250522014-03-25 10:40:18 +01001765 *
1766 * This function closely associates a certain irqchip with a certain
1767 * gpiochip, providing an irq domain to translate the local IRQs to
1768 * global irqs in the gpiolib core, and making sure that the gpiochip
1769 * is passed as chip data to all related functions. Driver callbacks
Linus Walleij09dd5f92015-12-07 15:31:58 +01001770 * need to use gpiochip_get_data() to get their local state containers back
Linus Walleij14250522014-03-25 10:40:18 +01001771 * from the gpiochip passed as chip data. An irqdomain will be stored
1772 * in the gpiochip that shall be used by the driver to handle IRQ number
1773 * translation. The gpiochip will need to be initialized and registered
1774 * before calling this function.
1775 *
Linus Walleijc3626fd2014-03-28 20:42:01 +01001776 * This function will handle two cell:ed simple IRQs and assumes all
1777 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +01001778 * need to be open coded.
1779 */
Linus Walleij739e6f52017-01-11 13:37:07 +01001780int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
1781 struct irq_chip *irqchip,
1782 unsigned int first_irq,
1783 irq_flow_handler_t handler,
1784 unsigned int type,
1785 bool nested,
1786 struct lock_class_key *lock_key)
Linus Walleij14250522014-03-25 10:40:18 +01001787{
1788 struct device_node *of_node;
Linus Walleij14250522014-03-25 10:40:18 +01001789
1790 if (!gpiochip || !irqchip)
1791 return -EINVAL;
1792
Linus Walleij58383c782015-11-04 09:56:26 +01001793 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +01001794 pr_err("missing gpiochip .dev parent pointer\n");
1795 return -EINVAL;
1796 }
Linus Walleijd245b3f2016-11-24 10:57:25 +01001797 gpiochip->irq_nested = nested;
Linus Walleij58383c782015-11-04 09:56:26 +01001798 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +01001799#ifdef CONFIG_OF_GPIO
1800 /*
Colin Cronin20a8a962015-05-18 11:41:43 -07001801 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08001802 * FIXME: get rid of this and use gpiochip->parent->of_node
1803 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +01001804 */
1805 if (gpiochip->of_node)
1806 of_node = gpiochip->of_node;
1807#endif
Marc Zyngier332e99d2016-09-07 09:12:11 +01001808 /*
Mika Westerberg0a1e0052016-09-12 14:29:51 +03001809 * Specifying a default trigger is a terrible idea if DT or ACPI is
Marc Zyngier332e99d2016-09-07 09:12:11 +01001810 * used to configure the interrupts, as you may end-up with
1811 * conflicting triggers. Tell the user, and reset to NONE.
1812 */
1813 if (WARN(of_node && type != IRQ_TYPE_NONE,
Rob Herring7eb6ce22017-07-18 16:43:03 -05001814 "%pOF: Ignoring %d default trigger\n", of_node, type))
Marc Zyngier332e99d2016-09-07 09:12:11 +01001815 type = IRQ_TYPE_NONE;
Mika Westerberg0a1e0052016-09-12 14:29:51 +03001816 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
1817 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1818 "Ignoring %d default trigger\n", type);
1819 type = IRQ_TYPE_NONE;
1820 }
Marc Zyngier332e99d2016-09-07 09:12:11 +01001821
Thierry Redingda80ff82017-11-07 19:15:46 +01001822 gpiochip->irq.chip = irqchip;
Thierry Redingc7a0aa52017-11-07 19:15:48 +01001823 gpiochip->irq.handler = handler;
Thierry Reding3634eeb2017-11-07 19:15:49 +01001824 gpiochip->irq.default_type = type;
Linus Walleij14250522014-03-25 10:40:18 +01001825 gpiochip->to_irq = gpiochip_to_irq;
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001826 gpiochip->lock_key = lock_key;
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001827 gpiochip->irq.domain = irq_domain_add_simple(of_node,
Linus Walleij14250522014-03-25 10:40:18 +01001828 gpiochip->ngpio, first_irq,
1829 &gpiochip_domain_ops, gpiochip);
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001830 if (!gpiochip->irq.domain) {
Thierry Redingda80ff82017-11-07 19:15:46 +01001831 gpiochip->irq.chip = NULL;
Linus Walleij14250522014-03-25 10:40:18 +01001832 return -EINVAL;
1833 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +02001834
1835 /*
1836 * It is possible for a driver to override this, but only if the
1837 * alternative functions are both implemented.
1838 */
1839 if (!irqchip->irq_request_resources &&
1840 !irqchip->irq_release_resources) {
1841 irqchip->irq_request_resources = gpiochip_irq_reqres;
1842 irqchip->irq_release_resources = gpiochip_irq_relres;
1843 }
Linus Walleij14250522014-03-25 10:40:18 +01001844
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001845 acpi_gpiochip_request_interrupts(gpiochip);
1846
Linus Walleij14250522014-03-25 10:40:18 +01001847 return 0;
1848}
Linus Walleij739e6f52017-01-11 13:37:07 +01001849EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key);
Linus Walleij14250522014-03-25 10:40:18 +01001850
1851#else /* CONFIG_GPIOLIB_IRQCHIP */
1852
1853static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
Mika Westerberg79b804c2016-09-20 15:15:21 +03001854static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1855{
1856 return 0;
1857}
1858static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1859{ }
Linus Walleij14250522014-03-25 10:40:18 +01001860
1861#endif /* CONFIG_GPIOLIB_IRQCHIP */
1862
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001863/**
1864 * gpiochip_generic_request() - request the gpio function for a pin
1865 * @chip: the gpiochip owning the GPIO
1866 * @offset: the offset of the GPIO to request for GPIO function
1867 */
1868int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
1869{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001870 return pinctrl_request_gpio(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001871}
1872EXPORT_SYMBOL_GPL(gpiochip_generic_request);
1873
1874/**
1875 * gpiochip_generic_free() - free the gpio function from a pin
1876 * @chip: the gpiochip to request the gpio function for
1877 * @offset: the offset of the GPIO to free from GPIO function
1878 */
1879void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
1880{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001881 pinctrl_free_gpio(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001882}
1883EXPORT_SYMBOL_GPL(gpiochip_generic_free);
1884
Mika Westerberg2956b5d2017-01-23 15:34:34 +03001885/**
1886 * gpiochip_generic_config() - apply configuration for a pin
1887 * @chip: the gpiochip owning the GPIO
1888 * @offset: the offset of the GPIO to apply the configuration
1889 * @config: the configuration to be applied
1890 */
1891int gpiochip_generic_config(struct gpio_chip *chip, unsigned offset,
1892 unsigned long config)
1893{
1894 return pinctrl_gpio_set_config(chip->gpiodev->base + offset, config);
1895}
1896EXPORT_SYMBOL_GPL(gpiochip_generic_config);
1897
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301898#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01001899
Linus Walleij3f0f8672012-11-20 12:40:15 +01001900/**
Christian Ruppert586a87e2013-10-15 15:37:54 +02001901 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1902 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +02001903 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +02001904 * @gpio_offset: the start offset in the current gpio_chip number space
1905 * @pin_group: name of the pin group inside the pin controller
1906 */
1907int gpiochip_add_pingroup_range(struct gpio_chip *chip,
1908 struct pinctrl_dev *pctldev,
1909 unsigned int gpio_offset, const char *pin_group)
1910{
1911 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001912 struct gpio_device *gdev = chip->gpiodev;
Christian Ruppert586a87e2013-10-15 15:37:54 +02001913 int ret;
1914
1915 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1916 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001917 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +02001918 return -ENOMEM;
1919 }
1920
1921 /* Use local offset as range ID */
1922 pin_range->range.id = gpio_offset;
1923 pin_range->range.gc = chip;
1924 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001925 pin_range->range.base = gdev->base + gpio_offset;
Christian Ruppert586a87e2013-10-15 15:37:54 +02001926 pin_range->pctldev = pctldev;
1927
1928 ret = pinctrl_get_group_pins(pctldev, pin_group,
1929 &pin_range->range.pins,
1930 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001931 if (ret < 0) {
1932 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001933 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001934 }
Christian Ruppert586a87e2013-10-15 15:37:54 +02001935
1936 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1937
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001938 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1939 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +02001940 pinctrl_dev_get_devname(pctldev), pin_group);
1941
Linus Walleij20ec3e32016-02-11 11:03:06 +01001942 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001943
1944 return 0;
1945}
1946EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1947
1948/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01001949 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1950 * @chip: the gpiochip to add the range for
Thierry Reding950d55f52017-07-24 16:57:22 +02001951 * @pinctl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01001952 * @gpio_offset: the start offset in the current gpio_chip number space
1953 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01001954 * @npins: the number of pins from the offset of each pin space (GPIO and
1955 * pin controller) to accumulate in this range
Thierry Reding950d55f52017-07-24 16:57:22 +02001956 *
1957 * Returns:
1958 * 0 on success, or a negative error-code on failure.
Linus Walleij3f0f8672012-11-20 12:40:15 +01001959 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001960int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01001961 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01001962 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301963{
1964 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001965 struct gpio_device *gdev = chip->gpiodev;
Axel Linb4d4b1f2012-11-21 14:33:56 +08001966 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301967
Linus Walleij3f0f8672012-11-20 12:40:15 +01001968 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301969 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001970 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001971 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301972 }
1973
Linus Walleij3f0f8672012-11-20 12:40:15 +01001974 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01001975 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001976 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301977 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001978 pin_range->range.base = gdev->base + gpio_offset;
Linus Walleij316511c2012-11-21 08:48:09 +01001979 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301980 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01001981 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301982 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01001983 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08001984 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001985 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +01001986 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08001987 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001988 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001989 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1990 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +01001991 pinctl_name,
1992 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301993
Linus Walleij20ec3e32016-02-11 11:03:06 +01001994 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001995
1996 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301997}
Linus Walleij165adc92012-11-06 14:49:39 +01001998EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301999
Linus Walleij3f0f8672012-11-20 12:40:15 +01002000/**
2001 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
2002 * @chip: the chip to remove all the mappings for
2003 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302004void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
2005{
2006 struct gpio_pin_range *pin_range, *tmp;
Linus Walleij20ec3e32016-02-11 11:03:06 +01002007 struct gpio_device *gdev = chip->gpiodev;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302008
Linus Walleij20ec3e32016-02-11 11:03:06 +01002009 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302010 list_del(&pin_range->node);
2011 pinctrl_remove_gpio_range(pin_range->pctldev,
2012 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01002013 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302014 }
2015}
Linus Walleij165adc92012-11-06 14:49:39 +01002016EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
2017
2018#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05302019
David Brownelld2876d02008-02-04 22:28:20 -08002020/* These "optional" allocation calls help prevent drivers from stomping
2021 * on each other, and help provide better diagnostics in debugfs.
2022 * They're called even less than the "set direction" calls.
2023 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002024static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08002025{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002026 struct gpio_chip *chip = desc->gdev->chip;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002027 int status;
David Brownelld2876d02008-02-04 22:28:20 -08002028 unsigned long flags;
2029
2030 spin_lock_irqsave(&gpio_lock, flags);
2031
David Brownelld2876d02008-02-04 22:28:20 -08002032 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07002033 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08002034 */
2035
2036 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
2037 desc_set_label(desc, label ? : "?");
2038 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07002039 } else {
David Brownelld2876d02008-02-04 22:28:20 -08002040 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -08002041 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07002042 }
2043
2044 if (chip->request) {
2045 /* chip->request may sleep */
2046 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002047 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07002048 spin_lock_irqsave(&gpio_lock, flags);
2049
2050 if (status < 0) {
2051 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -07002052 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03002053 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07002054 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07002055 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03002056 if (chip->get_direction) {
2057 /* chip->get_direction may sleep */
2058 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002059 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03002060 spin_lock_irqsave(&gpio_lock, flags);
2061 }
David Brownelld2876d02008-02-04 22:28:20 -08002062done:
Mika Westerberg77c2d792014-03-10 14:54:50 +02002063 spin_unlock_irqrestore(&gpio_lock, flags);
2064 return status;
2065}
2066
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002067/*
2068 * This descriptor validation needs to be inserted verbatim into each
2069 * function taking a descriptor, so we need to use a preprocessor
Linus Walleij54d77192016-05-30 16:48:39 +02002070 * macro to avoid endless duplication. If the desc is NULL it is an
2071 * optional GPIO and calls should just bail out.
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002072 */
2073#define VALIDATE_DESC(desc) do { \
Linus Walleij54d77192016-05-30 16:48:39 +02002074 if (!desc) \
2075 return 0; \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002076 if (IS_ERR(desc)) { \
2077 pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
2078 return PTR_ERR(desc); \
2079 } \
Linus Walleij54d77192016-05-30 16:48:39 +02002080 if (!desc->gdev) { \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002081 pr_warn("%s: invalid GPIO (no device)\n", __func__); \
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002082 return -EINVAL; \
2083 } \
2084 if ( !desc->gdev->chip ) { \
2085 dev_warn(&desc->gdev->dev, \
2086 "%s: backing chip is gone\n", __func__); \
2087 return 0; \
2088 } } while (0)
2089
2090#define VALIDATE_DESC_VOID(desc) do { \
Linus Walleij54d77192016-05-30 16:48:39 +02002091 if (!desc) \
2092 return; \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002093 if (IS_ERR(desc)) { \
2094 pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
2095 return; \
2096 } \
Linus Walleij54d77192016-05-30 16:48:39 +02002097 if (!desc->gdev) { \
Linus Walleijbfbbe442016-06-16 11:55:55 +02002098 pr_warn("%s: invalid GPIO (no device)\n", __func__); \
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002099 return; \
2100 } \
2101 if (!desc->gdev->chip) { \
2102 dev_warn(&desc->gdev->dev, \
2103 "%s: backing chip is gone\n", __func__); \
2104 return; \
2105 } } while (0)
2106
2107
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002108int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002109{
2110 int status = -EPROBE_DEFER;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002111 struct gpio_device *gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002112
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002113 VALIDATE_DESC(desc);
2114 gdev = desc->gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002115
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002116 if (try_module_get(gdev->owner)) {
Linus Walleijfac9d882017-09-26 20:58:28 +02002117 status = gpiod_request_commit(desc, label);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002118 if (status < 0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002119 module_put(gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002120 else
2121 get_device(&gdev->dev);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002122 }
2123
David Brownelld2876d02008-02-04 22:28:20 -08002124 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +02002125 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002126
David Brownelld2876d02008-02-04 22:28:20 -08002127 return status;
2128}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002129
Linus Walleijfac9d882017-09-26 20:58:28 +02002130static bool gpiod_free_commit(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002131{
Mika Westerberg77c2d792014-03-10 14:54:50 +02002132 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08002133 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07002134 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002135
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07002136 might_sleep();
2137
Alexandre Courbot372e7222013-02-03 01:29:29 +09002138 gpiod_unexport(desc);
David Brownelld8f388d82008-07-25 01:46:07 -07002139
David Brownelld2876d02008-02-04 22:28:20 -08002140 spin_lock_irqsave(&gpio_lock, flags);
2141
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002142 chip = desc->gdev->chip;
David Brownell35e8bb52008-10-15 22:03:16 -07002143 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
2144 if (chip->free) {
2145 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07002146 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002147 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07002148 spin_lock_irqsave(&gpio_lock, flags);
2149 }
David Brownelld2876d02008-02-04 22:28:20 -08002150 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08002151 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07002152 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302153 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302154 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06002155 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002156 ret = true;
2157 }
David Brownelld2876d02008-02-04 22:28:20 -08002158
2159 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002160 return ret;
2161}
2162
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002163void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002164{
Linus Walleijfac9d882017-09-26 20:58:28 +02002165 if (desc && desc->gdev && gpiod_free_commit(desc)) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002166 module_put(desc->gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002167 put_device(&desc->gdev->dev);
2168 } else {
Mika Westerberg77c2d792014-03-10 14:54:50 +02002169 WARN_ON(extra_checks);
Linus Walleij33a68e82016-02-11 10:28:44 +01002170 }
David Brownelld2876d02008-02-04 22:28:20 -08002171}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002172
David Brownelld2876d02008-02-04 22:28:20 -08002173/**
2174 * gpiochip_is_requested - return string iff signal was requested
2175 * @chip: controller managing the signal
2176 * @offset: of signal within controller's 0..(ngpio - 1) range
2177 *
2178 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09002179 * The string returned is the label passed to gpio_request(); if none has been
2180 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08002181 *
2182 * This function is for use by GPIO controller drivers. The label can
2183 * help with diagnostics, and knowing that the signal is used as a GPIO
2184 * can help avoid accidentally multiplexing it to another controller.
2185 */
2186const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
2187{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002188 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08002189
Dirk Behme48b59532015-08-18 18:02:32 +02002190 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08002191 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002192
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002193 desc = &chip->gpiodev->descs[offset];
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002194
Alexandre Courbot372e7222013-02-03 01:29:29 +09002195 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08002196 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002197 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08002198}
2199EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2200
Mika Westerberg77c2d792014-03-10 14:54:50 +02002201/**
2202 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
Thierry Reding950d55f52017-07-24 16:57:22 +02002203 * @chip: GPIO chip
2204 * @hwnum: hardware number of the GPIO for which to request the descriptor
Mika Westerberg77c2d792014-03-10 14:54:50 +02002205 * @label: label for the GPIO
2206 *
2207 * Function allows GPIO chip drivers to request and use their own GPIO
2208 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2209 * function will not increase reference count of the GPIO chip module. This
2210 * allows the GPIO chip module to be unloaded as needed (we assume that the
2211 * GPIO chip driver handles freeing the GPIOs it has requested).
Thierry Reding950d55f52017-07-24 16:57:22 +02002212 *
2213 * Returns:
2214 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
2215 * code on failure.
Mika Westerberg77c2d792014-03-10 14:54:50 +02002216 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002217struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
2218 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002219{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002220 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
2221 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002222
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002223 if (IS_ERR(desc)) {
2224 chip_err(chip, "failed to get GPIO descriptor\n");
2225 return desc;
2226 }
2227
Linus Walleijfac9d882017-09-26 20:58:28 +02002228 err = gpiod_request_commit(desc, label);
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002229 if (err < 0)
2230 return ERR_PTR(err);
2231
2232 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002233}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002234EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002235
2236/**
2237 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2238 * @desc: GPIO descriptor to free
2239 *
2240 * Function frees the given GPIO requested previously with
2241 * gpiochip_request_own_desc().
2242 */
2243void gpiochip_free_own_desc(struct gpio_desc *desc)
2244{
2245 if (desc)
Linus Walleijfac9d882017-09-26 20:58:28 +02002246 gpiod_free_commit(desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002247}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002248EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08002249
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002250/*
2251 * Drivers MUST set GPIO direction before making get/set calls. In
David Brownelld2876d02008-02-04 22:28:20 -08002252 * some cases this is done in early boot, before IRQs are enabled.
2253 *
2254 * As a rule these aren't called more than once (except for drivers
2255 * using the open-drain emulation idiom) so these are natural places
2256 * to accumulate extra debugging checks. Note that we can't (yet)
2257 * rely on gpio_request() having been called beforehand.
2258 */
2259
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002260/**
2261 * gpiod_direction_input - set the GPIO direction to input
2262 * @desc: GPIO to set to input
2263 *
2264 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2265 * be called safely on it.
2266 *
2267 * Return 0 in case of success, else an error code.
2268 */
2269int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002270{
David Brownelld2876d02008-02-04 22:28:20 -08002271 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002272 int status = -EINVAL;
2273
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002274 VALIDATE_DESC(desc);
2275 chip = desc->gdev->chip;
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002276
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002277 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01002278 gpiod_warn(desc,
2279 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02002280 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002281 return -EIO;
2282 }
2283
Alexandre Courbotd82da792014-07-22 16:17:43 +09002284 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -08002285 if (status == 0)
2286 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002287
Alexandre Courbot372e7222013-02-03 01:29:29 +09002288 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09002289
David Brownelld2876d02008-02-04 22:28:20 -08002290 return status;
2291}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002292EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002293
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002294static int gpio_set_drive_single_ended(struct gpio_chip *gc, unsigned offset,
2295 enum pin_config_param mode)
2296{
2297 unsigned long config = { PIN_CONF_PACKED(mode, 0) };
2298
2299 return gc->set_config ? gc->set_config(gc, offset, config) : -ENOTSUPP;
2300}
2301
Linus Walleijfac9d882017-09-26 20:58:28 +02002302static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08002303{
Linus Walleijc663e5f2016-03-22 10:51:16 +01002304 struct gpio_chip *gc = desc->gdev->chip;
Linus Walleijad177312016-11-13 23:02:44 +01002305 int val = !!value;
Linus Walleijc663e5f2016-03-22 10:51:16 +01002306 int ret;
David Brownelld2876d02008-02-04 22:28:20 -08002307
Linus Walleijc663e5f2016-03-22 10:51:16 +01002308 if (!gc->set || !gc->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01002309 gpiod_warn(desc,
2310 "%s: missing set() or direction_output() operations\n",
2311 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002312 return -EIO;
2313 }
2314
Linus Walleijad177312016-11-13 23:02:44 +01002315 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
Linus Walleijc663e5f2016-03-22 10:51:16 +01002316 if (!ret)
David Brownelld2876d02008-02-04 22:28:20 -08002317 set_bit(FLAG_IS_OUT, &desc->flags);
Linus Walleijad177312016-11-13 23:02:44 +01002318 trace_gpio_value(desc_to_gpio(desc), 0, val);
Linus Walleijc663e5f2016-03-22 10:51:16 +01002319 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2320 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08002321}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002322
2323/**
2324 * gpiod_direction_output_raw - set the GPIO direction to output
2325 * @desc: GPIO to set to output
2326 * @value: initial output value of the GPIO
2327 *
2328 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2329 * be called safely on it. The initial value of the output must be specified
2330 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2331 *
2332 * Return 0 in case of success, else an error code.
2333 */
2334int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2335{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002336 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02002337 return gpiod_direction_output_raw_commit(desc, value);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002338}
2339EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2340
2341/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05302342 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002343 * @desc: GPIO to set to output
2344 * @value: initial output value of the GPIO
2345 *
2346 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2347 * be called safely on it. The initial value of the output must be specified
2348 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2349 * account.
2350 *
2351 * Return 0 in case of success, else an error code.
2352 */
2353int gpiod_direction_output(struct gpio_desc *desc, int value)
2354{
Linus Walleij02e47982017-09-26 21:20:23 +02002355 struct gpio_chip *gc = desc->gdev->chip;
2356 int ret;
2357
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002358 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002359 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2360 value = !value;
Linus Walleijad177312016-11-13 23:02:44 +01002361 else
2362 value = !!value;
Linus Walleij02e47982017-09-26 21:20:23 +02002363
2364 /* GPIOs used for IRQs shall not be set as output */
2365 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
2366 gpiod_err(desc,
2367 "%s: tried to set a GPIO tied to an IRQ as output\n",
2368 __func__);
2369 return -EIO;
2370 }
2371
2372 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2373 /* First see if we can enable open drain in hardware */
2374 ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2375 PIN_CONFIG_DRIVE_OPEN_DRAIN);
2376 if (!ret)
2377 goto set_output_value;
2378 /* Emulate open drain by not actively driving the line high */
2379 if (value)
2380 return gpiod_direction_input(desc);
2381 }
2382 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2383 ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2384 PIN_CONFIG_DRIVE_OPEN_SOURCE);
2385 if (!ret)
2386 goto set_output_value;
2387 /* Emulate open source by not actively driving the line low */
2388 if (!value)
2389 return gpiod_direction_input(desc);
2390 } else {
2391 gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2392 PIN_CONFIG_DRIVE_PUSH_PULL);
2393 }
2394
2395set_output_value:
Linus Walleijfac9d882017-09-26 20:58:28 +02002396 return gpiod_direction_output_raw_commit(desc, value);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002397}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002398EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08002399
Felipe Balbic4b5be92010-05-26 14:42:23 -07002400/**
Thierry Reding950d55f52017-07-24 16:57:22 +02002401 * gpiod_set_debounce - sets @debounce time for a GPIO
2402 * @desc: descriptor of the GPIO for which to set debounce time
2403 * @debounce: debounce time in microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02002404 *
Thierry Reding950d55f52017-07-24 16:57:22 +02002405 * Returns:
2406 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
2407 * debounce time.
Felipe Balbic4b5be92010-05-26 14:42:23 -07002408 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002409int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07002410{
Felipe Balbic4b5be92010-05-26 14:42:23 -07002411 struct gpio_chip *chip;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002412 unsigned long config;
Felipe Balbic4b5be92010-05-26 14:42:23 -07002413
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002414 VALIDATE_DESC(desc);
2415 chip = desc->gdev->chip;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002416 if (!chip->set || !chip->set_config) {
Mark Brown6424de52013-09-09 10:33:49 +01002417 gpiod_dbg(desc,
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002418 "%s: missing set() or set_config() operations\n",
Mark Brown6424de52013-09-09 10:33:49 +01002419 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02002420 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002421 }
2422
Mika Westerberg2956b5d2017-01-23 15:34:34 +03002423 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
2424 return chip->set_config(chip, gpio_chip_hwgpio(desc), config);
Felipe Balbic4b5be92010-05-26 14:42:23 -07002425}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002426EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002427
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002428/**
2429 * gpiod_is_active_low - test whether a GPIO is active-low or not
2430 * @desc: the gpio descriptor to test
2431 *
2432 * Returns 1 if the GPIO is active-low, 0 otherwise.
2433 */
2434int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002435{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002436 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002437 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002438}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002439EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08002440
2441/* I/O calls are only valid after configuration completed; the relevant
2442 * "is this a valid GPIO" error checks should already have been done.
2443 *
2444 * "Get" operations are often inlinable as reading a pin value register,
2445 * and masking the relevant bit in that register.
2446 *
2447 * When "set" operations are inlinable, they involve writing that mask to
2448 * one register to set a low value, or a different register to set it high.
2449 * Otherwise locking is needed, so there may be little value to inlining.
2450 *
2451 *------------------------------------------------------------------------
2452 *
2453 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2454 * have requested the GPIO. That can include implicit requesting by
2455 * a direction setting call. Marking a gpio as requested locks its chip
2456 * in memory, guaranteeing that these table lookups need no more locking
2457 * and that gpiochip_remove() will fail.
2458 *
2459 * REVISIT when debugging, consider adding some instrumentation to ensure
2460 * that the GPIO was actually requested.
2461 */
2462
Linus Walleijfac9d882017-09-26 20:58:28 +02002463static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002464{
2465 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002466 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002467 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002468
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002469 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002470 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002471 value = chip->get ? chip->get(chip, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01002472 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002473 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002474 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002475}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002476
Lukas Wunnereec1d562017-10-12 12:40:10 +02002477static int gpio_chip_get_multiple(struct gpio_chip *chip,
2478 unsigned long *mask, unsigned long *bits)
2479{
2480 if (chip->get_multiple) {
2481 return chip->get_multiple(chip, mask, bits);
2482 } else if (chip->get) {
2483 int i, value;
2484
2485 for_each_set_bit(i, mask, chip->ngpio) {
2486 value = chip->get(chip, i);
2487 if (value < 0)
2488 return value;
2489 __assign_bit(i, bits, value);
2490 }
2491 return 0;
2492 }
2493 return -EIO;
2494}
2495
2496int gpiod_get_array_value_complex(bool raw, bool can_sleep,
2497 unsigned int array_size,
2498 struct gpio_desc **desc_array,
2499 int *value_array)
2500{
2501 int i = 0;
2502
2503 while (i < array_size) {
2504 struct gpio_chip *chip = desc_array[i]->gdev->chip;
2505 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2506 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2507 int first, j, ret;
2508
2509 if (!can_sleep)
2510 WARN_ON(chip->can_sleep);
2511
2512 /* collect all inputs belonging to the same chip */
2513 first = i;
2514 memset(mask, 0, sizeof(mask));
2515 do {
2516 const struct gpio_desc *desc = desc_array[i];
2517 int hwgpio = gpio_chip_hwgpio(desc);
2518
2519 __set_bit(hwgpio, mask);
2520 i++;
2521 } while ((i < array_size) &&
2522 (desc_array[i]->gdev->chip == chip));
2523
2524 ret = gpio_chip_get_multiple(chip, mask, bits);
2525 if (ret)
2526 return ret;
2527
2528 for (j = first; j < i; j++) {
2529 const struct gpio_desc *desc = desc_array[j];
2530 int hwgpio = gpio_chip_hwgpio(desc);
2531 int value = test_bit(hwgpio, bits);
2532
2533 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2534 value = !value;
2535 value_array[j] = value;
2536 trace_gpio_value(desc_to_gpio(desc), 1, value);
2537 }
2538 }
2539 return 0;
2540}
2541
David Brownelld2876d02008-02-04 22:28:20 -08002542/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002543 * gpiod_get_raw_value() - return a gpio's raw value
2544 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08002545 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002546 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002547 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002548 *
2549 * This function should be called from contexts where we cannot sleep, and will
2550 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002551 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002552int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002553{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002554 VALIDATE_DESC(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002555 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002556 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02002557 return gpiod_get_raw_value_commit(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002558}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002559EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002560
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002561/**
2562 * gpiod_get_value() - return a gpio's value
2563 * @desc: gpio whose value will be returned
2564 *
2565 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002566 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002567 *
2568 * This function should be called from contexts where we cannot sleep, and will
2569 * complain if the GPIO chip functions potentially sleep.
2570 */
2571int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002572{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002573 int value;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002574
2575 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002576 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002577 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002578
Linus Walleijfac9d882017-09-26 20:58:28 +02002579 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002580 if (value < 0)
2581 return value;
2582
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002583 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2584 value = !value;
2585
2586 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002587}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002588EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08002589
Lukas Wunnereec1d562017-10-12 12:40:10 +02002590/**
2591 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
2592 * @array_size: number of elements in the descriptor / value arrays
2593 * @desc_array: array of GPIO descriptors whose values will be read
2594 * @value_array: array to store the read values
2595 *
2596 * Read the raw values of the GPIOs, i.e. the values of the physical lines
2597 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
2598 * else an error code.
2599 *
2600 * This function should be called from contexts where we cannot sleep,
2601 * and it will complain if the GPIO chip functions potentially sleep.
2602 */
2603int gpiod_get_raw_array_value(unsigned int array_size,
2604 struct gpio_desc **desc_array, int *value_array)
2605{
2606 if (!desc_array)
2607 return -EINVAL;
2608 return gpiod_get_array_value_complex(true, false, array_size,
2609 desc_array, value_array);
2610}
2611EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
2612
2613/**
2614 * gpiod_get_array_value() - read values from an array of GPIOs
2615 * @array_size: number of elements in the descriptor / value arrays
2616 * @desc_array: array of GPIO descriptors whose values will be read
2617 * @value_array: array to store the read values
2618 *
2619 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2620 * into account. Return 0 in case of success, else an error code.
2621 *
2622 * This function should be called from contexts where we cannot sleep,
2623 * and it will complain if the GPIO chip functions potentially sleep.
2624 */
2625int gpiod_get_array_value(unsigned int array_size,
2626 struct gpio_desc **desc_array, int *value_array)
2627{
2628 if (!desc_array)
2629 return -EINVAL;
2630 return gpiod_get_array_value_complex(false, false, array_size,
2631 desc_array, value_array);
2632}
2633EXPORT_SYMBOL_GPL(gpiod_get_array_value);
2634
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302635/*
Linus Walleijfac9d882017-09-26 20:58:28 +02002636 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002637 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002638 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302639 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002640static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302641{
2642 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002643 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002644 int offset = gpio_chip_hwgpio(desc);
2645
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302646 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002647 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302648 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002649 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302650 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002651 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302652 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002653 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302654 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002655 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302656 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002657 gpiod_err(desc,
2658 "%s: Error in set_value for open drain err %d\n",
2659 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302660}
2661
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302662/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002663 * _gpio_set_open_source_value() - Set the open source gpio's value.
2664 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002665 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302666 */
Linus Walleijfac9d882017-09-26 20:58:28 +02002667static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302668{
2669 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002670 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002671 int offset = gpio_chip_hwgpio(desc);
2672
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302673 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002674 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302675 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002676 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302677 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002678 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302679 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002680 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302681 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002682 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302683 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002684 gpiod_err(desc,
2685 "%s: Error in set_value for open source err %d\n",
2686 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302687}
2688
Linus Walleijfac9d882017-09-26 20:58:28 +02002689static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08002690{
2691 struct gpio_chip *chip;
2692
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002693 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002694 trace_gpio_value(desc_to_gpio(desc), 0, value);
Linus Walleij02e47982017-09-26 21:20:23 +02002695 chip->set(chip, gpio_chip_hwgpio(desc), value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002696}
2697
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002698/*
2699 * set multiple outputs on the same chip;
2700 * use the chip's set_multiple function if available;
2701 * otherwise set the outputs sequentially;
2702 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
2703 * defines which outputs are to be changed
2704 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
2705 * defines the values the outputs specified by mask are to be set to
2706 */
2707static void gpio_chip_set_multiple(struct gpio_chip *chip,
2708 unsigned long *mask, unsigned long *bits)
2709{
2710 if (chip->set_multiple) {
2711 chip->set_multiple(chip, mask, bits);
2712 } else {
Andy Shevchenko5e4e6fb2017-01-03 19:01:17 +02002713 unsigned int i;
2714
2715 /* set outputs if the corresponding mask bit is set */
2716 for_each_set_bit(i, mask, chip->ngpio)
2717 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002718 }
2719}
2720
Linus Walleij44c72882016-04-24 11:36:59 +02002721void gpiod_set_array_value_complex(bool raw, bool can_sleep,
2722 unsigned int array_size,
2723 struct gpio_desc **desc_array,
2724 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002725{
2726 int i = 0;
2727
2728 while (i < array_size) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002729 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002730 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2731 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2732 int count = 0;
2733
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002734 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002735 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002736
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002737 memset(mask, 0, sizeof(mask));
2738 do {
2739 struct gpio_desc *desc = desc_array[i];
2740 int hwgpio = gpio_chip_hwgpio(desc);
2741 int value = value_array[i];
2742
2743 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2744 value = !value;
2745 trace_gpio_value(desc_to_gpio(desc), 0, value);
2746 /*
2747 * collect all normal outputs belonging to the same chip
2748 * open drain and open source outputs are set individually
2749 */
Linus Walleij02e47982017-09-26 21:20:23 +02002750 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02002751 gpio_set_open_drain_value_commit(desc, value);
Linus Walleij02e47982017-09-26 21:20:23 +02002752 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
Linus Walleijfac9d882017-09-26 20:58:28 +02002753 gpio_set_open_source_value_commit(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002754 } else {
2755 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002756 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002757 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002758 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002759 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002760 count++;
2761 }
2762 i++;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002763 } while ((i < array_size) &&
2764 (desc_array[i]->gdev->chip == chip));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002765 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002766 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002767 gpio_chip_set_multiple(chip, mask, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002768 }
2769}
2770
David Brownelld2876d02008-02-04 22:28:20 -08002771/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002772 * gpiod_set_raw_value() - assign a gpio's raw value
2773 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08002774 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002775 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002776 * Set the raw value of the GPIO, i.e. the value of its physical line without
2777 * regard for its ACTIVE_LOW status.
2778 *
2779 * This function should be called from contexts where we cannot sleep, and will
2780 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002781 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002782void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002783{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002784 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01002785 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002786 WARN_ON(desc->gdev->chip->can_sleep);
Linus Walleijfac9d882017-09-26 20:58:28 +02002787 gpiod_set_raw_value_commit(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002788}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002789EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002790
2791/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002792 * gpiod_set_value() - assign a gpio's value
2793 * @desc: gpio whose value will be assigned
2794 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002795 *
Linus Walleij02e47982017-09-26 21:20:23 +02002796 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
2797 * OPEN_DRAIN and OPEN_SOURCE flags into account.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002798 *
2799 * This function should be called from contexts where we cannot sleep, and will
2800 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002801 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002802void gpiod_set_value(struct gpio_desc *desc, int value)
2803{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002804 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01002805 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002806 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002807 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2808 value = !value;
Linus Walleij02e47982017-09-26 21:20:23 +02002809 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2810 gpio_set_open_drain_value_commit(desc, value);
2811 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2812 gpio_set_open_source_value_commit(desc, value);
2813 else
2814 gpiod_set_raw_value_commit(desc, value);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002815}
2816EXPORT_SYMBOL_GPL(gpiod_set_value);
2817
2818/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002819 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002820 * @array_size: number of elements in the descriptor / value arrays
2821 * @desc_array: array of GPIO descriptors whose values will be assigned
2822 * @value_array: array of values to assign
2823 *
2824 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2825 * without regard for their ACTIVE_LOW status.
2826 *
2827 * This function should be called from contexts where we cannot sleep, and will
2828 * complain if the GPIO chip functions potentially sleep.
2829 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002830void gpiod_set_raw_array_value(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002831 struct gpio_desc **desc_array, int *value_array)
2832{
2833 if (!desc_array)
2834 return;
Linus Walleij44c72882016-04-24 11:36:59 +02002835 gpiod_set_array_value_complex(true, false, array_size, desc_array,
2836 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002837}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002838EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002839
2840/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002841 * gpiod_set_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002842 * @array_size: number of elements in the descriptor / value arrays
2843 * @desc_array: array of GPIO descriptors whose values will be assigned
2844 * @value_array: array of values to assign
2845 *
2846 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2847 * into account.
2848 *
2849 * This function should be called from contexts where we cannot sleep, and will
2850 * complain if the GPIO chip functions potentially sleep.
2851 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002852void gpiod_set_array_value(unsigned int array_size,
2853 struct gpio_desc **desc_array, int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002854{
2855 if (!desc_array)
2856 return;
Linus Walleij44c72882016-04-24 11:36:59 +02002857 gpiod_set_array_value_complex(false, false, array_size, desc_array,
2858 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002859}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002860EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002861
2862/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002863 * gpiod_cansleep() - report whether gpio value access may sleep
2864 * @desc: gpio to check
2865 *
2866 */
2867int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002868{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002869 VALIDATE_DESC(desc);
2870 return desc->gdev->chip->can_sleep;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002871}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002872EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002873
David Brownell0f6d5042008-10-15 22:03:14 -07002874/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002875 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
2876 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07002877 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002878 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
2879 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07002880 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002881int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07002882{
Linus Walleij4c37ce82016-05-02 13:13:10 +02002883 struct gpio_chip *chip;
2884 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07002885
Linus Walleij79bb71b2016-06-15 22:57:38 +02002886 /*
2887 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
2888 * requires this function to not return zero on an invalid descriptor
2889 * but rather a negative error number.
2890 */
Linus Walleijbfbbe442016-06-16 11:55:55 +02002891 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
Linus Walleij79bb71b2016-06-15 22:57:38 +02002892 return -EINVAL;
2893
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002894 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002895 offset = gpio_chip_hwgpio(desc);
Linus Walleij4c37ce82016-05-02 13:13:10 +02002896 if (chip->to_irq) {
2897 int retirq = chip->to_irq(chip, offset);
2898
2899 /* Zero means NO_IRQ */
2900 if (!retirq)
2901 return -ENXIO;
2902
2903 return retirq;
2904 }
2905 return -ENXIO;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002906}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002907EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002908
Linus Walleijd468bf92013-09-24 11:54:38 +02002909/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002910 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002911 * @chip: the chip the GPIO to lock belongs to
2912 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02002913 *
2914 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08002915 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08002916 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002917int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08002918{
Linus Walleij9c102802016-05-25 10:56:03 +02002919 struct gpio_desc *desc;
Linus Walleijd468bf92013-09-24 11:54:38 +02002920
Linus Walleij9c102802016-05-25 10:56:03 +02002921 desc = gpiochip_get_desc(chip, offset);
2922 if (IS_ERR(desc))
2923 return PTR_ERR(desc);
2924
Linus Walleij60f83392016-11-12 15:01:09 +01002925 /*
2926 * If it's fast: flush the direction setting if something changed
2927 * behind our back
2928 */
2929 if (!chip->can_sleep && chip->get_direction) {
Linus Walleij9c102802016-05-25 10:56:03 +02002930 int dir = chip->get_direction(chip, offset);
2931
2932 if (dir)
2933 clear_bit(FLAG_IS_OUT, &desc->flags);
2934 else
2935 set_bit(FLAG_IS_OUT, &desc->flags);
2936 }
2937
2938 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002939 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02002940 "%s: tried to flag a GPIO set as output for IRQ\n",
2941 __func__);
2942 return -EIO;
2943 }
2944
Linus Walleij9c102802016-05-25 10:56:03 +02002945 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
Linus Walleij3940c342016-11-14 00:09:07 +01002946
2947 /*
2948 * If the consumer has not set up a label (such as when the
2949 * IRQ is referenced from .to_irq()) we set up a label here
2950 * so it is clear this is used as an interrupt.
2951 */
2952 if (!desc->label)
2953 desc_set_label(desc, "interrupt");
2954
Linus Walleijd468bf92013-09-24 11:54:38 +02002955 return 0;
2956}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002957EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02002958
Linus Walleijd468bf92013-09-24 11:54:38 +02002959/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002960 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002961 * @chip: the chip the GPIO to lock belongs to
2962 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02002963 *
2964 * This is used directly by GPIO drivers that want to indicate
2965 * that a certain GPIO is no longer used exclusively for IRQ.
2966 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002967void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02002968{
Linus Walleij3940c342016-11-14 00:09:07 +01002969 struct gpio_desc *desc;
2970
2971 desc = gpiochip_get_desc(chip, offset);
2972 if (IS_ERR(desc))
Linus Walleijd468bf92013-09-24 11:54:38 +02002973 return;
2974
Linus Walleij3940c342016-11-14 00:09:07 +01002975 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
2976
2977 /* If we only had this marking, erase it */
2978 if (desc->label && !strcmp(desc->label, "interrupt"))
2979 desc_set_label(desc, NULL);
Linus Walleijd468bf92013-09-24 11:54:38 +02002980}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002981EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02002982
Linus Walleij6cee3822016-02-11 20:16:45 +01002983bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
2984{
2985 if (offset >= chip->ngpio)
2986 return false;
2987
2988 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
2989}
2990EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
2991
Linus Walleij143b65d2016-02-16 15:41:42 +01002992bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
2993{
2994 if (offset >= chip->ngpio)
2995 return false;
2996
2997 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
2998}
2999EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
3000
3001bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
3002{
3003 if (offset >= chip->ngpio)
3004 return false;
3005
3006 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
3007}
3008EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
3009
Charles Keepax05f479b2017-05-23 15:47:29 +01003010bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset)
3011{
3012 if (offset >= chip->ngpio)
3013 return false;
3014
Andrew Jeffery2cbfca62017-10-20 13:27:58 +10303015 return !test_bit(FLAG_SLEEP_MAY_LOSE_VALUE,
Charles Keepax05f479b2017-05-23 15:47:29 +01003016 &chip->gpiodev->descs[offset].flags);
3017}
3018EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
3019
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003020/**
3021 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
3022 * @desc: gpio whose value will be returned
3023 *
3024 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003025 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003026 *
3027 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08003028 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003029int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08003030{
David Brownelld2876d02008-02-04 22:28:20 -08003031 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003032 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003033 return gpiod_get_raw_value_commit(desc);
David Brownelld2876d02008-02-04 22:28:20 -08003034}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003035EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003036
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003037/**
3038 * gpiod_get_value_cansleep() - return a gpio's value
3039 * @desc: gpio whose value will be returned
3040 *
3041 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003042 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003043 *
3044 * This function is to be called from contexts that can sleep.
3045 */
3046int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003047{
David Brownelld2876d02008-02-04 22:28:20 -08003048 int value;
David Brownelld2876d02008-02-04 22:28:20 -08003049
3050 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003051 VALIDATE_DESC(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003052 value = gpiod_get_raw_value_commit(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07003053 if (value < 0)
3054 return value;
3055
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003056 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3057 value = !value;
3058
David Brownelld2876d02008-02-04 22:28:20 -08003059 return value;
3060}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003061EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003062
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003063/**
Lukas Wunnereec1d562017-10-12 12:40:10 +02003064 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
3065 * @array_size: number of elements in the descriptor / value arrays
3066 * @desc_array: array of GPIO descriptors whose values will be read
3067 * @value_array: array to store the read values
3068 *
3069 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3070 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3071 * else an error code.
3072 *
3073 * This function is to be called from contexts that can sleep.
3074 */
3075int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
3076 struct gpio_desc **desc_array,
3077 int *value_array)
3078{
3079 might_sleep_if(extra_checks);
3080 if (!desc_array)
3081 return -EINVAL;
3082 return gpiod_get_array_value_complex(true, true, array_size,
3083 desc_array, value_array);
3084}
3085EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
3086
3087/**
3088 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
3089 * @array_size: number of elements in the descriptor / value arrays
3090 * @desc_array: array of GPIO descriptors whose values will be read
3091 * @value_array: array to store the read values
3092 *
3093 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3094 * into account. Return 0 in case of success, else an error code.
3095 *
3096 * This function is to be called from contexts that can sleep.
3097 */
3098int gpiod_get_array_value_cansleep(unsigned int array_size,
3099 struct gpio_desc **desc_array,
3100 int *value_array)
3101{
3102 might_sleep_if(extra_checks);
3103 if (!desc_array)
3104 return -EINVAL;
3105 return gpiod_get_array_value_complex(false, true, array_size,
3106 desc_array, value_array);
3107}
3108EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
3109
3110/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003111 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
3112 * @desc: gpio whose value will be assigned
3113 * @value: value to assign
3114 *
3115 * Set the raw value of the GPIO, i.e. the value of its physical line without
3116 * regard for its ACTIVE_LOW status.
3117 *
3118 * This function is to be called from contexts that can sleep.
3119 */
3120void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003121{
David Brownelld2876d02008-02-04 22:28:20 -08003122 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003123 VALIDATE_DESC_VOID(desc);
Linus Walleijfac9d882017-09-26 20:58:28 +02003124 gpiod_set_raw_value_commit(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003125}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003126EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09003127
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003128/**
3129 * gpiod_set_value_cansleep() - assign a gpio's value
3130 * @desc: gpio whose value will be assigned
3131 * @value: value to assign
3132 *
3133 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
3134 * account
3135 *
3136 * This function is to be called from contexts that can sleep.
3137 */
3138void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09003139{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003140 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003141 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003142 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
3143 value = !value;
Linus Walleijfac9d882017-09-26 20:58:28 +02003144 gpiod_set_raw_value_commit(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08003145}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07003146EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08003147
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003148/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003149 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003150 * @array_size: number of elements in the descriptor / value arrays
3151 * @desc_array: array of GPIO descriptors whose values will be assigned
3152 * @value_array: array of values to assign
3153 *
3154 * Set the raw values of the GPIOs, i.e. the values of the physical lines
3155 * without regard for their ACTIVE_LOW status.
3156 *
3157 * This function is to be called from contexts that can sleep.
3158 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003159void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
3160 struct gpio_desc **desc_array,
3161 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003162{
3163 might_sleep_if(extra_checks);
3164 if (!desc_array)
3165 return;
Linus Walleij44c72882016-04-24 11:36:59 +02003166 gpiod_set_array_value_complex(true, true, array_size, desc_array,
3167 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003168}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003169EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003170
3171/**
Dmitry Torokhov3946d182017-08-14 21:59:55 -07003172 * gpiod_add_lookup_tables() - register GPIO device consumers
3173 * @tables: list of tables of consumers to register
3174 * @n: number of tables in the list
3175 */
3176void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
3177{
3178 unsigned int i;
3179
3180 mutex_lock(&gpio_lookup_lock);
3181
3182 for (i = 0; i < n; i++)
3183 list_add_tail(&tables[i]->list, &gpio_lookup_list);
3184
3185 mutex_unlock(&gpio_lookup_lock);
3186}
3187
3188/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003189 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003190 * @array_size: number of elements in the descriptor / value arrays
3191 * @desc_array: array of GPIO descriptors whose values will be assigned
3192 * @value_array: array of values to assign
3193 *
3194 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3195 * into account.
3196 *
3197 * This function is to be called from contexts that can sleep.
3198 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003199void gpiod_set_array_value_cansleep(unsigned int array_size,
3200 struct gpio_desc **desc_array,
3201 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003202{
3203 might_sleep_if(extra_checks);
3204 if (!desc_array)
3205 return;
Linus Walleij44c72882016-04-24 11:36:59 +02003206 gpiod_set_array_value_complex(false, true, array_size, desc_array,
3207 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003208}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02003209EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01003210
3211/**
Alexandre Courbotad824782013-12-03 12:20:11 +09003212 * gpiod_add_lookup_table() - register GPIO device consumers
3213 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003214 */
Alexandre Courbotad824782013-12-03 12:20:11 +09003215void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003216{
3217 mutex_lock(&gpio_lookup_lock);
3218
Alexandre Courbotad824782013-12-03 12:20:11 +09003219 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003220
3221 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08003222}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02003223EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
David Brownelld2876d02008-02-04 22:28:20 -08003224
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05303225/**
3226 * gpiod_remove_lookup_table() - unregister GPIO device consumers
3227 * @table: table of consumers to unregister
3228 */
3229void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
3230{
3231 mutex_lock(&gpio_lookup_lock);
3232
3233 list_del(&table->list);
3234
3235 mutex_unlock(&gpio_lookup_lock);
3236}
Anatolij Gustschin226b2242017-04-20 23:23:20 +02003237EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05303238
Alexandre Courbotad824782013-12-03 12:20:11 +09003239static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
3240{
3241 const char *dev_id = dev ? dev_name(dev) : NULL;
3242 struct gpiod_lookup_table *table;
3243
3244 mutex_lock(&gpio_lookup_lock);
3245
3246 list_for_each_entry(table, &gpio_lookup_list, list) {
3247 if (table->dev_id && dev_id) {
3248 /*
3249 * Valid strings on both ends, must be identical to have
3250 * a match
3251 */
3252 if (!strcmp(table->dev_id, dev_id))
3253 goto found;
3254 } else {
3255 /*
3256 * One of the pointers is NULL, so both must be to have
3257 * a match
3258 */
3259 if (dev_id == table->dev_id)
3260 goto found;
3261 }
3262 }
3263 table = NULL;
3264
3265found:
3266 mutex_unlock(&gpio_lookup_lock);
3267 return table;
3268}
3269
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003270static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09003271 unsigned int idx,
3272 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003273{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003274 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09003275 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003276 struct gpiod_lookup *p;
3277
Alexandre Courbotad824782013-12-03 12:20:11 +09003278 table = gpiod_find_lookup_table(dev);
3279 if (!table)
3280 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003281
Alexandre Courbotad824782013-12-03 12:20:11 +09003282 for (p = &table->table[0]; p->chip_label; p++) {
3283 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003284
Alexandre Courbotad824782013-12-03 12:20:11 +09003285 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003286 if (p->idx != idx)
3287 continue;
3288
Alexandre Courbotad824782013-12-03 12:20:11 +09003289 /* If the lookup entry has a con_id, require exact match */
3290 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
3291 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003292
Alexandre Courbotad824782013-12-03 12:20:11 +09003293 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003294
Alexandre Courbotad824782013-12-03 12:20:11 +09003295 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003296 dev_err(dev, "cannot find GPIO chip %s\n",
3297 p->chip_label);
3298 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003299 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003300
Alexandre Courbotad824782013-12-03 12:20:11 +09003301 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003302 dev_err(dev,
3303 "requested GPIO %d is out of range [0..%d] for chip %s\n",
3304 idx, chip->ngpio, chip->label);
3305 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09003306 }
3307
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09003308 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09003309 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003310
3311 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09003312 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003313
3314 return desc;
3315}
3316
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003317static int dt_gpio_count(struct device *dev, const char *con_id)
3318{
3319 int ret;
3320 char propname[32];
3321 unsigned int i;
3322
3323 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
3324 if (con_id)
3325 snprintf(propname, sizeof(propname), "%s-%s",
3326 con_id, gpio_suffixes[i]);
3327 else
3328 snprintf(propname, sizeof(propname), "%s",
3329 gpio_suffixes[i]);
3330
3331 ret = of_gpio_named_count(dev->of_node, propname);
Andy Shevchenko4033d4a2017-02-20 18:15:47 +02003332 if (ret > 0)
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003333 break;
3334 }
Andy Shevchenko4033d4a2017-02-20 18:15:47 +02003335 return ret ? ret : -ENOENT;
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003336}
3337
3338static int platform_gpio_count(struct device *dev, const char *con_id)
3339{
3340 struct gpiod_lookup_table *table;
3341 struct gpiod_lookup *p;
3342 unsigned int count = 0;
3343
3344 table = gpiod_find_lookup_table(dev);
3345 if (!table)
3346 return -ENOENT;
3347
3348 for (p = &table->table[0]; p->chip_label; p++) {
3349 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3350 (!con_id && !p->con_id))
3351 count++;
3352 }
3353 if (!count)
3354 return -ENOENT;
3355
3356 return count;
3357}
3358
3359/**
3360 * gpiod_count - return the number of GPIOs associated with a device / function
3361 * or -ENOENT if no GPIO has been assigned to the requested function
3362 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3363 * @con_id: function within the GPIO consumer
3364 */
3365int gpiod_count(struct device *dev, const char *con_id)
3366{
3367 int count = -ENOENT;
3368
3369 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
3370 count = dt_gpio_count(dev, con_id);
3371 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
3372 count = acpi_gpio_count(dev, con_id);
3373
3374 if (count < 0)
3375 count = platform_gpio_count(dev, con_id);
3376
3377 return count;
3378}
3379EXPORT_SYMBOL_GPL(gpiod_count);
3380
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003381/**
Thierry Reding08791622014-04-25 16:54:22 +02003382 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09003383 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003384 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003385 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003386 *
3387 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003388 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07003389 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003390 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003391struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003392 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003393{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003394 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003395}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003396EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003397
3398/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02003399 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
3400 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3401 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003402 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02003403 *
3404 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
3405 * the requested function it will return NULL. This is convenient for drivers
3406 * that need to handle optional GPIOs.
3407 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003408struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003409 const char *con_id,
3410 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003411{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003412 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003413}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003414EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003415
Benoit Parrotf625d462015-02-02 11:44:44 -06003416
3417/**
3418 * gpiod_configure_flags - helper function to configure a given GPIO
3419 * @desc: gpio whose value will be assigned
3420 * @con_id: function within the GPIO consumer
Johan Hovold85b03b32016-07-03 18:32:05 +02003421 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3422 * of_get_gpio_hog()
Benoit Parrotf625d462015-02-02 11:44:44 -06003423 * @dflags: gpiod_flags - optional GPIO initialization flags
3424 *
3425 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
3426 * requested function and/or index, or another IS_ERR() code if an error
3427 * occurred while trying to acquire the GPIO.
3428 */
Andy Shevchenkoc29fd9e2017-05-23 20:03:16 +03003429int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Johan Hovold85b03b32016-07-03 18:32:05 +02003430 unsigned long lflags, enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06003431{
3432 int status;
3433
Johan Hovold85b03b32016-07-03 18:32:05 +02003434 if (lflags & GPIO_ACTIVE_LOW)
3435 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
3436 if (lflags & GPIO_OPEN_DRAIN)
3437 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3438 if (lflags & GPIO_OPEN_SOURCE)
3439 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
Andrew Jeffery2cbfca62017-10-20 13:27:58 +10303440 if (lflags & GPIO_SLEEP_MAY_LOSE_VALUE)
3441 set_bit(FLAG_SLEEP_MAY_LOSE_VALUE, &desc->flags);
Johan Hovold85b03b32016-07-03 18:32:05 +02003442
Benoit Parrotf625d462015-02-02 11:44:44 -06003443 /* No particular flag request, return here... */
3444 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
3445 pr_debug("no flags found for %s\n", con_id);
3446 return 0;
3447 }
3448
3449 /* Process flags */
3450 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
3451 status = gpiod_direction_output(desc,
Linus Walleijad177312016-11-13 23:02:44 +01003452 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
Benoit Parrotf625d462015-02-02 11:44:44 -06003453 else
3454 status = gpiod_direction_input(desc);
3455
3456 return status;
3457}
3458
Thierry Reding29a1f2332014-04-25 17:10:06 +02003459/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003460 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02003461 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003462 * @con_id: function within the GPIO consumer
3463 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003464 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003465 *
3466 * This variant of gpiod_get() allows to access GPIOs other than the first
3467 * defined one for functions that define several GPIOs.
3468 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003469 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
3470 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07003471 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003472 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003473struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003474 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003475 unsigned int idx,
3476 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003477{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003478 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003479 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003480 enum gpio_lookup_flags lookupflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003481
3482 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
3483
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003484 if (dev) {
3485 /* Using device tree? */
3486 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
3487 dev_dbg(dev, "using device tree for GPIO lookup\n");
3488 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
3489 } else if (ACPI_COMPANION(dev)) {
3490 dev_dbg(dev, "using ACPI for GPIO lookup\n");
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +03003491 desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags);
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003492 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003493 }
3494
3495 /*
3496 * Either we are not using DT or ACPI, or their lookup did not return
3497 * a result. In that case, use platform lookup as a fallback.
3498 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003499 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04003500 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003501 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003502 }
3503
3504 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02003505 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003506 return desc;
3507 }
3508
3509 status = gpiod_request(desc, con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003510 if (status < 0)
3511 return ERR_PTR(status);
3512
Johan Hovold85b03b32016-07-03 18:32:05 +02003513 status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003514 if (status < 0) {
3515 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
3516 gpiod_put(desc);
3517 return ERR_PTR(status);
3518 }
3519
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003520 return desc;
3521}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003522EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003523
3524/**
Mika Westerberg40b73182014-10-21 13:33:59 +02003525 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
3526 * @fwnode: handle of the firmware node
3527 * @propname: name of the firmware property representing the GPIO
Boris Brezillon537b94d2017-02-02 14:53:11 +01003528 * @index: index of the GPIO to obtain in the consumer
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003529 * @dflags: GPIO initialization flags
Thierry Reding950d55f52017-07-24 16:57:22 +02003530 * @label: label to attach to the requested GPIO
Mika Westerberg40b73182014-10-21 13:33:59 +02003531 *
3532 * This function can be used for drivers that get their configuration
3533 * from firmware.
3534 *
3535 * Function properly finds the corresponding GPIO using whatever is the
3536 * underlying firmware interface and then makes sure that the GPIO
3537 * descriptor is requested before it is returned to the caller.
3538 *
Thierry Reding950d55f52017-07-24 16:57:22 +02003539 * Returns:
Andy Shevchenkoff213782017-02-28 17:03:12 +02003540 * On successful request the GPIO pin is configured in accordance with
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003541 * provided @dflags.
3542 *
Mika Westerberg40b73182014-10-21 13:33:59 +02003543 * In case of error an ERR_PTR() is returned.
3544 */
3545struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
Boris Brezillon537b94d2017-02-02 14:53:11 +01003546 const char *propname, int index,
Alexander Steinb2987d72017-01-12 17:39:24 +01003547 enum gpiod_flags dflags,
3548 const char *label)
Mika Westerberg40b73182014-10-21 13:33:59 +02003549{
3550 struct gpio_desc *desc = ERR_PTR(-ENODEV);
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003551 unsigned long lflags = 0;
Mika Westerberg40b73182014-10-21 13:33:59 +02003552 bool active_low = false;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003553 bool single_ended = false;
Laxman Dewangan4c0facd2017-04-06 19:05:52 +05303554 bool open_drain = false;
Mika Westerberg40b73182014-10-21 13:33:59 +02003555 int ret;
3556
3557 if (!fwnode)
3558 return ERR_PTR(-EINVAL);
3559
3560 if (is_of_node(fwnode)) {
3561 enum of_gpio_flags flags;
3562
Boris Brezillon537b94d2017-02-02 14:53:11 +01003563 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname,
3564 index, &flags);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003565 if (!IS_ERR(desc)) {
Mika Westerberg40b73182014-10-21 13:33:59 +02003566 active_low = flags & OF_GPIO_ACTIVE_LOW;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003567 single_ended = flags & OF_GPIO_SINGLE_ENDED;
Laxman Dewangan4c0facd2017-04-06 19:05:52 +05303568 open_drain = flags & OF_GPIO_OPEN_DRAIN;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003569 }
Mika Westerberg40b73182014-10-21 13:33:59 +02003570 } else if (is_acpi_node(fwnode)) {
3571 struct acpi_gpio_info info;
3572
Boris Brezillon537b94d2017-02-02 14:53:11 +01003573 desc = acpi_node_get_gpiod(fwnode, propname, index, &info);
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +03003574 if (!IS_ERR(desc)) {
Christophe RICARD52044722015-12-23 23:25:34 +01003575 active_low = info.polarity == GPIO_ACTIVE_LOW;
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +03003576 ret = acpi_gpio_update_gpiod_flags(&dflags, info.flags);
3577 if (ret)
3578 pr_debug("Override GPIO initialization flags\n");
3579 }
Mika Westerberg40b73182014-10-21 13:33:59 +02003580 }
3581
3582 if (IS_ERR(desc))
3583 return desc;
3584
Alexander Steinb2987d72017-01-12 17:39:24 +01003585 ret = gpiod_request(desc, label);
Johan Hovold85b03b32016-07-03 18:32:05 +02003586 if (ret)
3587 return ERR_PTR(ret);
3588
Mika Westerberg40b73182014-10-21 13:33:59 +02003589 if (active_low)
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003590 lflags |= GPIO_ACTIVE_LOW;
Mika Westerberg40b73182014-10-21 13:33:59 +02003591
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003592 if (single_ended) {
Laxman Dewangan4c0facd2017-04-06 19:05:52 +05303593 if (open_drain)
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003594 lflags |= GPIO_OPEN_DRAIN;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003595 else
Andy Shevchenkoa264d102017-01-09 16:02:28 +02003596 lflags |= GPIO_OPEN_SOURCE;
3597 }
3598
3599 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
3600 if (ret < 0) {
3601 gpiod_put(desc);
3602 return ERR_PTR(ret);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003603 }
3604
Mika Westerberg40b73182014-10-21 13:33:59 +02003605 return desc;
3606}
3607EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
3608
3609/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02003610 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
3611 * function
3612 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3613 * @con_id: function within the GPIO consumer
3614 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003615 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02003616 *
3617 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
3618 * specified index was assigned to the requested function it will return NULL.
3619 * This is convenient for drivers that need to handle optional GPIOs.
3620 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003621struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02003622 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003623 unsigned int index,
3624 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003625{
3626 struct gpio_desc *desc;
3627
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003628 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003629 if (IS_ERR(desc)) {
3630 if (PTR_ERR(desc) == -ENOENT)
3631 return NULL;
3632 }
3633
3634 return desc;
3635}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003636EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003637
3638/**
Benoit Parrotf625d462015-02-02 11:44:44 -06003639 * gpiod_hog - Hog the specified GPIO desc given the provided flags
3640 * @desc: gpio whose value will be assigned
3641 * @name: gpio line name
3642 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3643 * of_get_gpio_hog()
3644 * @dflags: gpiod_flags - optional GPIO initialization flags
3645 */
3646int gpiod_hog(struct gpio_desc *desc, const char *name,
3647 unsigned long lflags, enum gpiod_flags dflags)
3648{
3649 struct gpio_chip *chip;
3650 struct gpio_desc *local_desc;
3651 int hwnum;
3652 int status;
3653
3654 chip = gpiod_to_chip(desc);
3655 hwnum = gpio_chip_hwgpio(desc);
3656
3657 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
3658 if (IS_ERR(local_desc)) {
Laxman Dewanganc31a5712016-03-11 19:13:21 +05303659 status = PTR_ERR(local_desc);
3660 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
3661 name, chip->label, hwnum, status);
3662 return status;
Benoit Parrotf625d462015-02-02 11:44:44 -06003663 }
3664
Johan Hovold85b03b32016-07-03 18:32:05 +02003665 status = gpiod_configure_flags(desc, name, lflags, dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -06003666 if (status < 0) {
Laxman Dewanganc31a5712016-03-11 19:13:21 +05303667 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed, %d\n",
3668 name, chip->label, hwnum, status);
Benoit Parrotf625d462015-02-02 11:44:44 -06003669 gpiochip_free_own_desc(desc);
3670 return status;
3671 }
3672
3673 /* Mark GPIO as hogged so it can be identified and removed later */
3674 set_bit(FLAG_IS_HOGGED, &desc->flags);
3675
3676 pr_info("GPIO line %d (%s) hogged as %s%s\n",
3677 desc_to_gpio(desc), name,
3678 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
3679 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
3680 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
3681
3682 return 0;
3683}
3684
3685/**
3686 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
3687 * @chip: gpio chip to act on
3688 *
3689 * This is only used by of_gpiochip_remove to free hogged gpios
3690 */
3691static void gpiochip_free_hogs(struct gpio_chip *chip)
3692{
3693 int id;
3694
3695 for (id = 0; id < chip->ngpio; id++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01003696 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
3697 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
Benoit Parrotf625d462015-02-02 11:44:44 -06003698 }
3699}
3700
3701/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003702 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
3703 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3704 * @con_id: function within the GPIO consumer
3705 * @flags: optional GPIO initialization flags
3706 *
3707 * This function acquires all the GPIOs defined under a given function.
3708 *
3709 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
3710 * no GPIO has been assigned to the requested function, or another IS_ERR()
3711 * code if an error occurred while trying to acquire the GPIOs.
3712 */
3713struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
3714 const char *con_id,
3715 enum gpiod_flags flags)
3716{
3717 struct gpio_desc *desc;
3718 struct gpio_descs *descs;
3719 int count;
3720
3721 count = gpiod_count(dev, con_id);
3722 if (count < 0)
3723 return ERR_PTR(count);
3724
3725 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
3726 GFP_KERNEL);
3727 if (!descs)
3728 return ERR_PTR(-ENOMEM);
3729
3730 for (descs->ndescs = 0; descs->ndescs < count; ) {
3731 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
3732 if (IS_ERR(desc)) {
3733 gpiod_put_array(descs);
3734 return ERR_CAST(desc);
3735 }
3736 descs->desc[descs->ndescs] = desc;
3737 descs->ndescs++;
3738 }
3739 return descs;
3740}
3741EXPORT_SYMBOL_GPL(gpiod_get_array);
3742
3743/**
3744 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
3745 * function
3746 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3747 * @con_id: function within the GPIO consumer
3748 * @flags: optional GPIO initialization flags
3749 *
3750 * This is equivalent to gpiod_get_array(), except that when no GPIO was
3751 * assigned to the requested function it will return NULL.
3752 */
3753struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
3754 const char *con_id,
3755 enum gpiod_flags flags)
3756{
3757 struct gpio_descs *descs;
3758
3759 descs = gpiod_get_array(dev, con_id, flags);
3760 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
3761 return NULL;
3762
3763 return descs;
3764}
3765EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
3766
3767/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003768 * gpiod_put - dispose of a GPIO descriptor
3769 * @desc: GPIO descriptor to dispose of
3770 *
3771 * No descriptor can be used after gpiod_put() has been called on it.
3772 */
3773void gpiod_put(struct gpio_desc *desc)
3774{
3775 gpiod_free(desc);
3776}
3777EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08003778
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003779/**
3780 * gpiod_put_array - dispose of multiple GPIO descriptors
3781 * @descs: struct gpio_descs containing an array of descriptors
3782 */
3783void gpiod_put_array(struct gpio_descs *descs)
3784{
3785 unsigned int i;
3786
3787 for (i = 0; i < descs->ndescs; i++)
3788 gpiod_put(descs->desc[i]);
3789
3790 kfree(descs);
3791}
3792EXPORT_SYMBOL_GPL(gpiod_put_array);
3793
Linus Walleij3c702e92015-10-21 15:29:53 +02003794static int __init gpiolib_dev_init(void)
3795{
3796 int ret;
3797
3798 /* Register GPIO sysfs bus */
3799 ret = bus_register(&gpio_bus_type);
3800 if (ret < 0) {
3801 pr_err("gpiolib: could not register GPIO bus type\n");
3802 return ret;
3803 }
3804
3805 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
3806 if (ret < 0) {
3807 pr_err("gpiolib: failed to allocate char dev region\n");
3808 bus_unregister(&gpio_bus_type);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07003809 } else {
3810 gpiolib_initialized = true;
3811 gpiochip_setup_devs();
Linus Walleij3c702e92015-10-21 15:29:53 +02003812 }
3813 return ret;
3814}
3815core_initcall(gpiolib_dev_init);
3816
David Brownelld2876d02008-02-04 22:28:20 -08003817#ifdef CONFIG_DEBUG_FS
3818
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003819static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
David Brownelld2876d02008-02-04 22:28:20 -08003820{
3821 unsigned i;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003822 struct gpio_chip *chip = gdev->chip;
3823 unsigned gpio = gdev->base;
3824 struct gpio_desc *gdesc = &gdev->descs[0];
David Brownelld2876d02008-02-04 22:28:20 -08003825 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02003826 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08003827
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003828 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02003829 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
3830 if (gdesc->name) {
3831 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
3832 gpio, gdesc->name);
3833 }
David Brownelld2876d02008-02-04 22:28:20 -08003834 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02003835 }
David Brownelld2876d02008-02-04 22:28:20 -08003836
Alexandre Courbot372e7222013-02-03 01:29:29 +09003837 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08003838 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02003839 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Markus Pargmannced433e2015-08-14 16:11:02 +02003840 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
3841 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08003842 is_out ? "out" : "in ",
3843 chip->get
3844 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02003845 : "? ",
3846 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08003847 seq_printf(s, "\n");
3848 }
3849}
3850
Thierry Redingf9c4a312012-04-12 13:26:01 +02003851static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08003852{
Grant Likely362432a2013-02-09 09:41:49 +00003853 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02003854 struct gpio_device *gdev = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003855 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003856
3857 s->private = "";
3858
Grant Likely362432a2013-02-09 09:41:49 +00003859 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003860 list_for_each_entry(gdev, &gpio_devices, list)
Grant Likely362432a2013-02-09 09:41:49 +00003861 if (index-- == 0) {
3862 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003863 return gdev;
Grant Likely362432a2013-02-09 09:41:49 +00003864 }
3865 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003866
3867 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003868}
3869
3870static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
3871{
Grant Likely362432a2013-02-09 09:41:49 +00003872 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02003873 struct gpio_device *gdev = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003874 void *ret = NULL;
3875
Grant Likely362432a2013-02-09 09:41:49 +00003876 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003877 if (list_is_last(&gdev->list, &gpio_devices))
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003878 ret = NULL;
3879 else
Linus Walleijff2b1352015-10-20 11:10:38 +02003880 ret = list_entry(gdev->list.next, struct gpio_device, list);
Grant Likely362432a2013-02-09 09:41:49 +00003881 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02003882
3883 s->private = "\n";
3884 ++*pos;
3885
3886 return ret;
3887}
3888
3889static void gpiolib_seq_stop(struct seq_file *s, void *v)
3890{
3891}
3892
3893static int gpiolib_seq_show(struct seq_file *s, void *v)
3894{
Linus Walleijff2b1352015-10-20 11:10:38 +02003895 struct gpio_device *gdev = v;
3896 struct gpio_chip *chip = gdev->chip;
3897 struct device *parent;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003898
Linus Walleijff2b1352015-10-20 11:10:38 +02003899 if (!chip) {
3900 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
3901 dev_name(&gdev->dev));
3902 return 0;
3903 }
3904
3905 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
3906 dev_name(&gdev->dev),
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003907 gdev->base, gdev->base + gdev->ngpio - 1);
Linus Walleijff2b1352015-10-20 11:10:38 +02003908 parent = chip->parent;
3909 if (parent)
3910 seq_printf(s, ", parent: %s/%s",
3911 parent->bus ? parent->bus->name : "no-bus",
3912 dev_name(parent));
Thierry Redingf9c4a312012-04-12 13:26:01 +02003913 if (chip->label)
3914 seq_printf(s, ", %s", chip->label);
3915 if (chip->can_sleep)
3916 seq_printf(s, ", can sleep");
3917 seq_printf(s, ":\n");
3918
3919 if (chip->dbg_show)
3920 chip->dbg_show(s, chip);
3921 else
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003922 gpiolib_dbg_show(s, gdev);
Thierry Redingf9c4a312012-04-12 13:26:01 +02003923
David Brownelld2876d02008-02-04 22:28:20 -08003924 return 0;
3925}
3926
Thierry Redingf9c4a312012-04-12 13:26:01 +02003927static const struct seq_operations gpiolib_seq_ops = {
3928 .start = gpiolib_seq_start,
3929 .next = gpiolib_seq_next,
3930 .stop = gpiolib_seq_stop,
3931 .show = gpiolib_seq_show,
3932};
3933
David Brownelld2876d02008-02-04 22:28:20 -08003934static int gpiolib_open(struct inode *inode, struct file *file)
3935{
Thierry Redingf9c4a312012-04-12 13:26:01 +02003936 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08003937}
3938
Alexey Dobriyan828c0952009-10-01 15:43:56 -07003939static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02003940 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08003941 .open = gpiolib_open,
3942 .read = seq_read,
3943 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02003944 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08003945};
3946
3947static int __init gpiolib_debugfs_init(void)
3948{
3949 /* /sys/kernel/debug/gpio */
3950 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
3951 NULL, NULL, &gpiolib_operations);
3952 return 0;
3953}
3954subsys_initcall(gpiolib_debugfs_init);
3955
3956#endif /* DEBUG_FS */