blob: 0e29cb7456484ed755008127519fedf9eb0d6e6d [file] [log] [blame]
David Brownelld2876d02008-02-04 22:28:20 -08001#include <linux/kernel.h>
2#include <linux/module.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -07003#include <linux/interrupt.h>
David Brownelld2876d02008-02-04 22:28:20 -08004#include <linux/irq.h>
5#include <linux/spinlock.h>
Alexandre Courbot1a989d02013-02-03 01:29:24 +09006#include <linux/list.h>
David Brownelld8f388d82008-07-25 01:46:07 -07007#include <linux/device.h>
8#include <linux/err.h>
9#include <linux/debugfs.h>
10#include <linux/seq_file.h>
11#include <linux/gpio.h>
Anton Vorontsov391c9702010-06-08 07:48:17 -060012#include <linux/of_gpio.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -070013#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Rafael J. Wysocki7b199812013-11-11 22:41:56 +010015#include <linux/acpi.h>
Alexandre Courbot53e7cac2013-11-16 21:44:52 +090016#include <linux/gpio/driver.h>
Linus Walleij0a6d3152014-07-24 20:08:55 +020017#include <linux/gpio/machine.h>
Jonas Gorskic771c2f2015-10-11 17:34:15 +020018#include <linux/pinctrl/consumer.h>
Linus Walleij3c702e92015-10-21 15:29:53 +020019#include <linux/cdev.h>
20#include <linux/fs.h>
21#include <linux/uaccess.h>
Linus Walleij8b92e172016-05-27 14:24:04 +020022#include <linux/compat.h>
Linus Walleijd7c51b42016-04-26 10:35:29 +020023#include <linux/anon_inodes.h>
Linus Walleij61f922d2016-06-02 11:30:15 +020024#include <linux/kfifo.h>
25#include <linux/poll.h>
26#include <linux/timekeeping.h>
Linus Walleij3c702e92015-10-21 15:29:53 +020027#include <uapi/linux/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080028
Mika Westerberg664e3e52014-01-08 12:40:54 +020029#include "gpiolib.h"
30
Uwe Kleine-König3f397c212011-05-20 00:40:19 -060031#define CREATE_TRACE_POINTS
32#include <trace/events/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080033
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070034/* Implementation infrastructure for GPIO interfaces.
David Brownelld2876d02008-02-04 22:28:20 -080035 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070036 * The GPIO programming interface allows for inlining speed-critical
37 * get/set operations for common cases, so that access to SOC-integrated
38 * GPIOs can sometimes cost only an instruction or two per bit.
David Brownelld2876d02008-02-04 22:28:20 -080039 */
40
41
42/* When debugging, extend minimal trust to callers and platform code.
43 * Also emit diagnostic messages that may help initial bringup, when
44 * board setup or driver bugs are most common.
45 *
46 * Otherwise, minimize overhead in what may be bitbanging codepaths.
47 */
48#ifdef DEBUG
49#define extra_checks 1
50#else
51#define extra_checks 0
52#endif
53
Linus Walleijff2b1352015-10-20 11:10:38 +020054/* Device and char device-related information */
55static DEFINE_IDA(gpio_ida);
Linus Walleij3c702e92015-10-21 15:29:53 +020056static dev_t gpio_devt;
57#define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
58static struct bus_type gpio_bus_type = {
59 .name = "gpio",
60};
Linus Walleijff2b1352015-10-20 11:10:38 +020061
David Brownelld2876d02008-02-04 22:28:20 -080062/* gpio_lock prevents conflicts during gpio_desc[] table updates.
63 * While any GPIO is requested, its gpio_chip is not removable;
64 * each GPIO's "requested" flag serves as a lock and refcount.
65 */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090066DEFINE_SPINLOCK(gpio_lock);
David Brownelld2876d02008-02-04 22:28:20 -080067
Alexandre Courbotbae48da2013-10-17 10:21:38 -070068static DEFINE_MUTEX(gpio_lookup_lock);
69static LIST_HEAD(gpio_lookup_list);
Linus Walleijff2b1352015-10-20 11:10:38 +020070LIST_HEAD(gpio_devices);
Johan Hovold6d867502015-05-04 17:23:25 +020071
72static void gpiochip_free_hogs(struct gpio_chip *chip);
73static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
Mika Westerberg79b804c2016-09-20 15:15:21 +030074static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip);
75static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip);
Johan Hovold6d867502015-05-04 17:23:25 +020076
Guenter Roeck159f3cd2016-03-31 08:11:30 -070077static bool gpiolib_initialized;
Johan Hovold6d867502015-05-04 17:23:25 +020078
David Brownelld2876d02008-02-04 22:28:20 -080079static inline void desc_set_label(struct gpio_desc *d, const char *label)
80{
David Brownelld2876d02008-02-04 22:28:20 -080081 d->label = label;
David Brownelld2876d02008-02-04 22:28:20 -080082}
83
Alexandre Courbot372e7222013-02-03 01:29:29 +090084/**
85 * Convert a GPIO number to its descriptor
86 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070087struct gpio_desc *gpio_to_desc(unsigned gpio)
Alexandre Courbot372e7222013-02-03 01:29:29 +090088{
Linus Walleijff2b1352015-10-20 11:10:38 +020089 struct gpio_device *gdev;
Alexandre Courbot14e85c02014-11-19 16:51:27 +090090 unsigned long flags;
91
92 spin_lock_irqsave(&gpio_lock, flags);
93
Linus Walleijff2b1352015-10-20 11:10:38 +020094 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +010095 if (gdev->base <= gpio &&
96 gdev->base + gdev->ngpio > gpio) {
Alexandre Courbot14e85c02014-11-19 16:51:27 +090097 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +010098 return &gdev->descs[gpio - gdev->base];
Alexandre Courbot14e85c02014-11-19 16:51:27 +090099 }
100 }
101
102 spin_unlock_irqrestore(&gpio_lock, flags);
103
Alexandre Courbot0e9a5ed2014-12-02 23:15:05 +0900104 if (!gpio_is_valid(gpio))
105 WARN(1, "invalid GPIO %d\n", gpio);
106
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900107 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900108}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700109EXPORT_SYMBOL_GPL(gpio_to_desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900110
111/**
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900112 * Get the GPIO descriptor corresponding to the given hw number for this chip.
Linus Walleijd468bf92013-09-24 11:54:38 +0200113 */
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900114struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
115 u16 hwnum)
Linus Walleijd468bf92013-09-24 11:54:38 +0200116{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100117 struct gpio_device *gdev = chip->gpiodev;
118
119 if (hwnum >= gdev->ngpio)
Alexandre Courbotb7d0a282013-12-03 12:31:11 +0900120 return ERR_PTR(-EINVAL);
Linus Walleijd468bf92013-09-24 11:54:38 +0200121
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100122 return &gdev->descs[hwnum];
Linus Walleijd468bf92013-09-24 11:54:38 +0200123}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900124
125/**
126 * Convert a GPIO descriptor to the integer namespace.
127 * This should disappear in the future but is needed since we still
128 * use GPIO numbers for error messages and sysfs nodes
129 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700130int desc_to_gpio(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900131{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100132 return desc->gdev->base + (desc - &desc->gdev->descs[0]);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900133}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700134EXPORT_SYMBOL_GPL(desc_to_gpio);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900135
136
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700137/**
138 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
139 * @desc: descriptor to return the chip of
140 */
141struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900142{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100143 if (!desc || !desc->gdev || !desc->gdev->chip)
144 return NULL;
145 return desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900146}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700147EXPORT_SYMBOL_GPL(gpiod_to_chip);
David Brownelld2876d02008-02-04 22:28:20 -0800148
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700149/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
150static int gpiochip_find_base(int ngpio)
151{
Linus Walleijff2b1352015-10-20 11:10:38 +0200152 struct gpio_device *gdev;
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900153 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700154
Linus Walleijff2b1352015-10-20 11:10:38 +0200155 list_for_each_entry_reverse(gdev, &gpio_devices, list) {
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900156 /* found a free space? */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100157 if (gdev->base + gdev->ngpio <= base)
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900158 break;
159 else
160 /* nope, check the space right before the chip */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100161 base = gdev->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700162 }
163
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900164 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700165 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900166 return base;
167 } else {
168 pr_err("%s: cannot find free range\n", __func__);
169 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700170 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700171}
172
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700173/**
174 * gpiod_get_direction - return the current direction of a GPIO
175 * @desc: GPIO to get the direction of
176 *
177 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
178 *
179 * This function may sleep if gpiod_cansleep() is true.
180 */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900181int gpiod_get_direction(struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300182{
183 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900184 unsigned offset;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300185 int status = -EINVAL;
186
Alexandre Courbot372e7222013-02-03 01:29:29 +0900187 chip = gpiod_to_chip(desc);
188 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300189
190 if (!chip->get_direction)
191 return status;
192
Alexandre Courbot372e7222013-02-03 01:29:29 +0900193 status = chip->get_direction(chip, offset);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300194 if (status > 0) {
195 /* GPIOF_DIR_IN, or other positive */
196 status = 1;
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900197 clear_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300198 }
199 if (status == 0) {
200 /* GPIOF_DIR_OUT */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900201 set_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300202 }
203 return status;
204}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700205EXPORT_SYMBOL_GPL(gpiod_get_direction);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300206
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900207/*
208 * Add a new chip to the global chips list, keeping the list of chips sorted
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800209 * by range(means [base, base + ngpio - 1]) order.
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900210 *
211 * Return -EBUSY if the new chip overlaps with some other chip's integer
212 * space.
213 */
Linus Walleijff2b1352015-10-20 11:10:38 +0200214static int gpiodev_add_to_list(struct gpio_device *gdev)
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900215{
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800216 struct gpio_device *prev, *next;
Linus Walleijff2b1352015-10-20 11:10:38 +0200217
218 if (list_empty(&gpio_devices)) {
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800219 /* initial entry in list */
Linus Walleijff2b1352015-10-20 11:10:38 +0200220 list_add_tail(&gdev->list, &gpio_devices);
Sudip Mukherjeee28ecca2015-12-27 19:06:50 +0530221 return 0;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900222 }
223
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800224 next = list_entry(gpio_devices.next, struct gpio_device, list);
225 if (gdev->base + gdev->ngpio <= next->base) {
226 /* add before first entry */
227 list_add(&gdev->list, &gpio_devices);
Julien Grossholtz96098df2016-01-07 16:46:45 -0500228 return 0;
229 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900230
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800231 prev = list_entry(gpio_devices.prev, struct gpio_device, list);
232 if (prev->base + prev->ngpio <= gdev->base) {
233 /* add behind last entry */
234 list_add_tail(&gdev->list, &gpio_devices);
235 return 0;
236 }
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800237
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800238 list_for_each_entry_safe(prev, next, &gpio_devices, list) {
239 /* at the end of the list */
240 if (&next->list == &gpio_devices)
241 break;
242
243 /* add between prev and next */
244 if (prev->base + prev->ngpio <= gdev->base
245 && gdev->base + gdev->ngpio <= next->base) {
246 list_add(&gdev->list, &prev->list);
247 return 0;
248 }
249 }
250
251 dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n");
252 return -EBUSY;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900253}
254
Linus Walleijf881bab02015-09-23 16:20:43 -0700255/**
256 * Convert a GPIO name to its descriptor
257 */
258static struct gpio_desc *gpio_name_to_desc(const char * const name)
259{
Linus Walleijff2b1352015-10-20 11:10:38 +0200260 struct gpio_device *gdev;
Linus Walleijf881bab02015-09-23 16:20:43 -0700261 unsigned long flags;
262
263 spin_lock_irqsave(&gpio_lock, flags);
264
Linus Walleijff2b1352015-10-20 11:10:38 +0200265 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijf881bab02015-09-23 16:20:43 -0700266 int i;
267
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100268 for (i = 0; i != gdev->ngpio; ++i) {
269 struct gpio_desc *desc = &gdev->descs[i];
Linus Walleijf881bab02015-09-23 16:20:43 -0700270
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100271 if (!desc->name || !name)
Linus Walleijf881bab02015-09-23 16:20:43 -0700272 continue;
273
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100274 if (!strcmp(desc->name, name)) {
Linus Walleijf881bab02015-09-23 16:20:43 -0700275 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100276 return desc;
Linus Walleijf881bab02015-09-23 16:20:43 -0700277 }
278 }
279 }
280
281 spin_unlock_irqrestore(&gpio_lock, flags);
282
283 return NULL;
284}
285
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200286/*
287 * Takes the names from gc->names and checks if they are all unique. If they
288 * are, they are assigned to their gpio descriptors.
289 *
Bamvor Jian Zhanged379152015-11-14 16:43:20 +0800290 * Warning if one of the names is already used for a different GPIO.
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200291 */
292static int gpiochip_set_desc_names(struct gpio_chip *gc)
293{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100294 struct gpio_device *gdev = gc->gpiodev;
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200295 int i;
296
297 if (!gc->names)
298 return 0;
299
300 /* First check all names if they are unique */
301 for (i = 0; i != gc->ngpio; ++i) {
302 struct gpio_desc *gpio;
303
304 gpio = gpio_name_to_desc(gc->names[i]);
Linus Walleijf881bab02015-09-23 16:20:43 -0700305 if (gpio)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100306 dev_warn(&gdev->dev,
Linus Walleij34ffd852015-10-20 11:31:54 +0200307 "Detected name collision for GPIO name '%s'\n",
Linus Walleijf881bab02015-09-23 16:20:43 -0700308 gc->names[i]);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200309 }
310
311 /* Then add all names to the GPIO descriptors */
312 for (i = 0; i != gc->ngpio; ++i)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100313 gdev->descs[i].name = gc->names[i];
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200314
315 return 0;
316}
317
Linus Walleijd7c51b42016-04-26 10:35:29 +0200318/*
319 * GPIO line handle management
320 */
321
322/**
323 * struct linehandle_state - contains the state of a userspace handle
324 * @gdev: the GPIO device the handle pertains to
325 * @label: consumer label used to tag descriptors
326 * @descs: the GPIO descriptors held by this handle
327 * @numdescs: the number of descriptors held in the descs array
328 */
329struct linehandle_state {
330 struct gpio_device *gdev;
331 const char *label;
332 struct gpio_desc *descs[GPIOHANDLES_MAX];
333 u32 numdescs;
334};
335
336static long linehandle_ioctl(struct file *filep, unsigned int cmd,
337 unsigned long arg)
338{
339 struct linehandle_state *lh = filep->private_data;
340 void __user *ip = (void __user *)arg;
341 struct gpiohandle_data ghd;
342 int i;
343
344 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
345 int val;
346
347 /* TODO: check if descriptors are really input */
348 for (i = 0; i < lh->numdescs; i++) {
349 val = gpiod_get_value_cansleep(lh->descs[i]);
350 if (val < 0)
351 return val;
352 ghd.values[i] = val;
353 }
354
355 if (copy_to_user(ip, &ghd, sizeof(ghd)))
356 return -EFAULT;
357
358 return 0;
359 } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
360 int vals[GPIOHANDLES_MAX];
361
362 /* TODO: check if descriptors are really output */
363 if (copy_from_user(&ghd, ip, sizeof(ghd)))
364 return -EFAULT;
365
366 /* Clamp all values to [0,1] */
367 for (i = 0; i < lh->numdescs; i++)
368 vals[i] = !!ghd.values[i];
369
370 /* Reuse the array setting function */
371 gpiod_set_array_value_complex(false,
372 true,
373 lh->numdescs,
374 lh->descs,
375 vals);
376 return 0;
377 }
378 return -EINVAL;
379}
380
381#ifdef CONFIG_COMPAT
382static long linehandle_ioctl_compat(struct file *filep, unsigned int cmd,
383 unsigned long arg)
384{
385 return linehandle_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
386}
387#endif
388
389static int linehandle_release(struct inode *inode, struct file *filep)
390{
391 struct linehandle_state *lh = filep->private_data;
392 struct gpio_device *gdev = lh->gdev;
393 int i;
394
395 for (i = 0; i < lh->numdescs; i++)
396 gpiod_free(lh->descs[i]);
397 kfree(lh->label);
398 kfree(lh);
399 put_device(&gdev->dev);
400 return 0;
401}
402
403static const struct file_operations linehandle_fileops = {
404 .release = linehandle_release,
405 .owner = THIS_MODULE,
406 .llseek = noop_llseek,
407 .unlocked_ioctl = linehandle_ioctl,
408#ifdef CONFIG_COMPAT
409 .compat_ioctl = linehandle_ioctl_compat,
410#endif
411};
412
413static int linehandle_create(struct gpio_device *gdev, void __user *ip)
414{
415 struct gpiohandle_request handlereq;
416 struct linehandle_state *lh;
417 int fd, i, ret;
418
419 if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
420 return -EFAULT;
421 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
422 return -EINVAL;
423
424 lh = kzalloc(sizeof(*lh), GFP_KERNEL);
425 if (!lh)
426 return -ENOMEM;
427 lh->gdev = gdev;
428 get_device(&gdev->dev);
429
430 /* Make sure this is terminated */
431 handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
432 if (strlen(handlereq.consumer_label)) {
433 lh->label = kstrdup(handlereq.consumer_label,
434 GFP_KERNEL);
435 if (!lh->label) {
436 ret = -ENOMEM;
437 goto out_free_lh;
438 }
439 }
440
441 /* Request each GPIO */
442 for (i = 0; i < handlereq.lines; i++) {
443 u32 offset = handlereq.lineoffsets[i];
444 u32 lflags = handlereq.flags;
445 struct gpio_desc *desc;
446
447 desc = &gdev->descs[offset];
448 ret = gpiod_request(desc, lh->label);
449 if (ret)
450 goto out_free_descs;
451 lh->descs[i] = desc;
452
453 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
454 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
455 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
456 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
457 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
458 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
459
460 /*
461 * Lines have to be requested explicitly for input
462 * or output, else the line will be treated "as is".
463 */
464 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
465 int val = !!handlereq.default_values[i];
466
467 ret = gpiod_direction_output(desc, val);
468 if (ret)
469 goto out_free_descs;
470 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
471 ret = gpiod_direction_input(desc);
472 if (ret)
473 goto out_free_descs;
474 }
475 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
476 offset);
477 }
Linus Walleije2f608b2016-06-18 10:56:43 +0200478 /* Let i point at the last handle */
479 i--;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200480 lh->numdescs = handlereq.lines;
481
482 fd = anon_inode_getfd("gpio-linehandle",
483 &linehandle_fileops,
484 lh,
485 O_RDONLY | O_CLOEXEC);
486 if (fd < 0) {
487 ret = fd;
488 goto out_free_descs;
489 }
490
491 handlereq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200492 if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
493 ret = -EFAULT;
494 goto out_free_descs;
495 }
Linus Walleijd7c51b42016-04-26 10:35:29 +0200496
497 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
498 lh->numdescs);
499
500 return 0;
501
502out_free_descs:
503 for (; i >= 0; i--)
504 gpiod_free(lh->descs[i]);
505 kfree(lh->label);
506out_free_lh:
507 kfree(lh);
508 put_device(&gdev->dev);
509 return ret;
510}
511
Linus Walleij61f922d2016-06-02 11:30:15 +0200512/*
513 * GPIO line event management
514 */
515
516/**
517 * struct lineevent_state - contains the state of a userspace event
518 * @gdev: the GPIO device the event pertains to
519 * @label: consumer label used to tag descriptors
520 * @desc: the GPIO descriptor held by this event
521 * @eflags: the event flags this line was requested with
522 * @irq: the interrupt that trigger in response to events on this GPIO
523 * @wait: wait queue that handles blocking reads of events
524 * @events: KFIFO for the GPIO events
525 * @read_lock: mutex lock to protect reads from colliding with adding
526 * new events to the FIFO
527 */
528struct lineevent_state {
529 struct gpio_device *gdev;
530 const char *label;
531 struct gpio_desc *desc;
532 u32 eflags;
533 int irq;
534 wait_queue_head_t wait;
535 DECLARE_KFIFO(events, struct gpioevent_data, 16);
536 struct mutex read_lock;
537};
538
539static unsigned int lineevent_poll(struct file *filep,
540 struct poll_table_struct *wait)
541{
542 struct lineevent_state *le = filep->private_data;
543 unsigned int events = 0;
544
545 poll_wait(filep, &le->wait, wait);
546
547 if (!kfifo_is_empty(&le->events))
548 events = POLLIN | POLLRDNORM;
549
550 return events;
551}
552
553
554static ssize_t lineevent_read(struct file *filep,
555 char __user *buf,
556 size_t count,
557 loff_t *f_ps)
558{
559 struct lineevent_state *le = filep->private_data;
560 unsigned int copied;
561 int ret;
562
563 if (count < sizeof(struct gpioevent_data))
564 return -EINVAL;
565
566 do {
567 if (kfifo_is_empty(&le->events)) {
568 if (filep->f_flags & O_NONBLOCK)
569 return -EAGAIN;
570
571 ret = wait_event_interruptible(le->wait,
572 !kfifo_is_empty(&le->events));
573 if (ret)
574 return ret;
575 }
576
577 if (mutex_lock_interruptible(&le->read_lock))
578 return -ERESTARTSYS;
579 ret = kfifo_to_user(&le->events, buf, count, &copied);
580 mutex_unlock(&le->read_lock);
581
582 if (ret)
583 return ret;
584
585 /*
586 * If we couldn't read anything from the fifo (a different
587 * thread might have been faster) we either return -EAGAIN if
588 * the file descriptor is non-blocking, otherwise we go back to
589 * sleep and wait for more data to arrive.
590 */
591 if (copied == 0 && (filep->f_flags & O_NONBLOCK))
592 return -EAGAIN;
593
594 } while (copied == 0);
595
596 return copied;
597}
598
599static int lineevent_release(struct inode *inode, struct file *filep)
600{
601 struct lineevent_state *le = filep->private_data;
602 struct gpio_device *gdev = le->gdev;
603
604 free_irq(le->irq, le);
605 gpiod_free(le->desc);
606 kfree(le->label);
607 kfree(le);
608 put_device(&gdev->dev);
609 return 0;
610}
611
612static long lineevent_ioctl(struct file *filep, unsigned int cmd,
613 unsigned long arg)
614{
615 struct lineevent_state *le = filep->private_data;
616 void __user *ip = (void __user *)arg;
617 struct gpiohandle_data ghd;
618
619 /*
620 * We can get the value for an event line but not set it,
621 * because it is input by definition.
622 */
623 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
624 int val;
625
626 val = gpiod_get_value_cansleep(le->desc);
627 if (val < 0)
628 return val;
629 ghd.values[0] = val;
630
631 if (copy_to_user(ip, &ghd, sizeof(ghd)))
632 return -EFAULT;
633
634 return 0;
635 }
636 return -EINVAL;
637}
638
639#ifdef CONFIG_COMPAT
640static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd,
641 unsigned long arg)
642{
643 return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
644}
645#endif
646
647static const struct file_operations lineevent_fileops = {
648 .release = lineevent_release,
649 .read = lineevent_read,
650 .poll = lineevent_poll,
651 .owner = THIS_MODULE,
652 .llseek = noop_llseek,
653 .unlocked_ioctl = lineevent_ioctl,
654#ifdef CONFIG_COMPAT
655 .compat_ioctl = lineevent_ioctl_compat,
656#endif
657};
658
Ben Dooks33265b12016-06-17 16:03:13 +0100659static irqreturn_t lineevent_irq_thread(int irq, void *p)
Linus Walleij61f922d2016-06-02 11:30:15 +0200660{
661 struct lineevent_state *le = p;
662 struct gpioevent_data ge;
663 int ret;
664
665 ge.timestamp = ktime_get_real_ns();
666
667 if (le->eflags & GPIOEVENT_REQUEST_BOTH_EDGES) {
668 int level = gpiod_get_value_cansleep(le->desc);
669
670 if (level)
671 /* Emit low-to-high event */
672 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
673 else
674 /* Emit high-to-low event */
675 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
676 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
677 /* Emit low-to-high event */
678 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
679 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
680 /* Emit high-to-low event */
681 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Arnd Bergmannbc0207a2016-06-16 11:02:41 +0200682 } else {
683 return IRQ_NONE;
Linus Walleij61f922d2016-06-02 11:30:15 +0200684 }
685
686 ret = kfifo_put(&le->events, ge);
687 if (ret != 0)
688 wake_up_poll(&le->wait, POLLIN);
689
690 return IRQ_HANDLED;
691}
692
693static int lineevent_create(struct gpio_device *gdev, void __user *ip)
694{
695 struct gpioevent_request eventreq;
696 struct lineevent_state *le;
697 struct gpio_desc *desc;
698 u32 offset;
699 u32 lflags;
700 u32 eflags;
701 int fd;
702 int ret;
703 int irqflags = 0;
704
705 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
706 return -EFAULT;
707
708 le = kzalloc(sizeof(*le), GFP_KERNEL);
709 if (!le)
710 return -ENOMEM;
711 le->gdev = gdev;
712 get_device(&gdev->dev);
713
714 /* Make sure this is terminated */
715 eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
716 if (strlen(eventreq.consumer_label)) {
717 le->label = kstrdup(eventreq.consumer_label,
718 GFP_KERNEL);
719 if (!le->label) {
720 ret = -ENOMEM;
721 goto out_free_le;
722 }
723 }
724
725 offset = eventreq.lineoffset;
726 lflags = eventreq.handleflags;
727 eflags = eventreq.eventflags;
728
729 /* This is just wrong: we don't look for events on output lines */
730 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
731 ret = -EINVAL;
732 goto out_free_label;
733 }
734
735 desc = &gdev->descs[offset];
736 ret = gpiod_request(desc, le->label);
737 if (ret)
738 goto out_free_desc;
739 le->desc = desc;
740 le->eflags = eflags;
741
742 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
743 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
744 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
745 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
746 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
747 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
748
749 ret = gpiod_direction_input(desc);
750 if (ret)
751 goto out_free_desc;
752
753 le->irq = gpiod_to_irq(desc);
754 if (le->irq <= 0) {
755 ret = -ENODEV;
756 goto out_free_desc;
757 }
758
759 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
760 irqflags |= IRQF_TRIGGER_RISING;
761 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
762 irqflags |= IRQF_TRIGGER_FALLING;
763 irqflags |= IRQF_ONESHOT;
764 irqflags |= IRQF_SHARED;
765
766 INIT_KFIFO(le->events);
767 init_waitqueue_head(&le->wait);
768 mutex_init(&le->read_lock);
769
770 /* Request a thread to read the events */
771 ret = request_threaded_irq(le->irq,
772 NULL,
773 lineevent_irq_thread,
774 irqflags,
775 le->label,
776 le);
777 if (ret)
778 goto out_free_desc;
779
780 fd = anon_inode_getfd("gpio-event",
781 &lineevent_fileops,
782 le,
783 O_RDONLY | O_CLOEXEC);
784 if (fd < 0) {
785 ret = fd;
786 goto out_free_irq;
787 }
788
789 eventreq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200790 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
791 ret = -EFAULT;
792 goto out_free_irq;
793 }
Linus Walleij61f922d2016-06-02 11:30:15 +0200794
795 return 0;
796
797out_free_irq:
798 free_irq(le->irq, le);
799out_free_desc:
800 gpiod_free(le->desc);
801out_free_label:
802 kfree(le->label);
803out_free_le:
804 kfree(le);
805 put_device(&gdev->dev);
806 return ret;
807}
808
Linus Walleij3c702e92015-10-21 15:29:53 +0200809/**
810 * gpio_ioctl() - ioctl handler for the GPIO chardev
811 */
812static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
813{
814 struct gpio_device *gdev = filp->private_data;
815 struct gpio_chip *chip = gdev->chip;
Linus Walleij8b92e172016-05-27 14:24:04 +0200816 void __user *ip = (void __user *)arg;
Linus Walleij3c702e92015-10-21 15:29:53 +0200817
818 /* We fail any subsequent ioctl():s when the chip is gone */
819 if (!chip)
820 return -ENODEV;
821
Linus Walleij521a2ad2016-02-12 22:25:22 +0100822 /* Fill in the struct and pass to userspace */
Linus Walleij3c702e92015-10-21 15:29:53 +0200823 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
Linus Walleij521a2ad2016-02-12 22:25:22 +0100824 struct gpiochip_info chipinfo;
825
Linus Walleij3c702e92015-10-21 15:29:53 +0200826 strncpy(chipinfo.name, dev_name(&gdev->dev),
827 sizeof(chipinfo.name));
828 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
Linus Walleijdf4878e2016-02-12 14:48:23 +0100829 strncpy(chipinfo.label, gdev->label,
830 sizeof(chipinfo.label));
831 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100832 chipinfo.lines = gdev->ngpio;
Linus Walleij3c702e92015-10-21 15:29:53 +0200833 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
834 return -EFAULT;
835 return 0;
Linus Walleij521a2ad2016-02-12 22:25:22 +0100836 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
837 struct gpioline_info lineinfo;
838 struct gpio_desc *desc;
839
840 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
841 return -EFAULT;
842 if (lineinfo.line_offset > gdev->ngpio)
843 return -EINVAL;
844
845 desc = &gdev->descs[lineinfo.line_offset];
846 if (desc->name) {
847 strncpy(lineinfo.name, desc->name,
848 sizeof(lineinfo.name));
849 lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
850 } else {
851 lineinfo.name[0] = '\0';
852 }
853 if (desc->label) {
Linus Walleij214338e2016-02-25 21:01:48 +0100854 strncpy(lineinfo.consumer, desc->label,
855 sizeof(lineinfo.consumer));
856 lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +0100857 } else {
Linus Walleij214338e2016-02-25 21:01:48 +0100858 lineinfo.consumer[0] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +0100859 }
860
861 /*
862 * Userspace only need to know that the kernel is using
863 * this GPIO so it can't use it.
864 */
865 lineinfo.flags = 0;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100866 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
867 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
868 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
869 test_bit(FLAG_EXPORT, &desc->flags) ||
870 test_bit(FLAG_SYSFS, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100871 lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100872 if (test_bit(FLAG_IS_OUT, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100873 lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100874 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100875 lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100876 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100877 lineinfo.flags |= GPIOLINE_FLAG_OPEN_DRAIN;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100878 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100879 lineinfo.flags |= GPIOLINE_FLAG_OPEN_SOURCE;
880
881 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
882 return -EFAULT;
883 return 0;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200884 } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
885 return linehandle_create(gdev, ip);
Linus Walleij61f922d2016-06-02 11:30:15 +0200886 } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
887 return lineevent_create(gdev, ip);
Linus Walleij3c702e92015-10-21 15:29:53 +0200888 }
889 return -EINVAL;
890}
891
Linus Walleij8b92e172016-05-27 14:24:04 +0200892#ifdef CONFIG_COMPAT
893static long gpio_ioctl_compat(struct file *filp, unsigned int cmd,
894 unsigned long arg)
895{
896 return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
897}
898#endif
899
Linus Walleij3c702e92015-10-21 15:29:53 +0200900/**
901 * gpio_chrdev_open() - open the chardev for ioctl operations
902 * @inode: inode for this chardev
903 * @filp: file struct for storing private data
904 * Returns 0 on success
905 */
906static int gpio_chrdev_open(struct inode *inode, struct file *filp)
907{
908 struct gpio_device *gdev = container_of(inode->i_cdev,
909 struct gpio_device, chrdev);
910
911 /* Fail on open if the backing gpiochip is gone */
912 if (!gdev || !gdev->chip)
913 return -ENODEV;
914 get_device(&gdev->dev);
915 filp->private_data = gdev;
Lars-Peter Clausenf4e81c52016-11-30 13:05:21 +0100916
917 return nonseekable_open(inode, filp);
Linus Walleij3c702e92015-10-21 15:29:53 +0200918}
919
920/**
921 * gpio_chrdev_release() - close chardev after ioctl operations
922 * @inode: inode for this chardev
923 * @filp: file struct for storing private data
924 * Returns 0 on success
925 */
926static int gpio_chrdev_release(struct inode *inode, struct file *filp)
927{
928 struct gpio_device *gdev = container_of(inode->i_cdev,
929 struct gpio_device, chrdev);
930
931 if (!gdev)
932 return -ENODEV;
933 put_device(&gdev->dev);
934 return 0;
935}
936
937
938static const struct file_operations gpio_fileops = {
939 .release = gpio_chrdev_release,
940 .open = gpio_chrdev_open,
941 .owner = THIS_MODULE,
Lars-Peter Clausenf4e81c52016-11-30 13:05:21 +0100942 .llseek = no_llseek,
Linus Walleij3c702e92015-10-21 15:29:53 +0200943 .unlocked_ioctl = gpio_ioctl,
Linus Walleij8b92e172016-05-27 14:24:04 +0200944#ifdef CONFIG_COMPAT
945 .compat_ioctl = gpio_ioctl_compat,
946#endif
Linus Walleij3c702e92015-10-21 15:29:53 +0200947};
948
Linus Walleijff2b1352015-10-20 11:10:38 +0200949static void gpiodevice_release(struct device *dev)
950{
951 struct gpio_device *gdev = dev_get_drvdata(dev);
952
953 list_del(&gdev->list);
954 ida_simple_remove(&gpio_ida, gdev->id);
Guenter Roeck476e2fc2016-03-31 08:11:29 -0700955 kfree(gdev->label);
956 kfree(gdev->descs);
Linus Walleij9efd9e62016-02-09 14:27:42 +0100957 kfree(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +0200958}
959
Guenter Roeck159f3cd2016-03-31 08:11:30 -0700960static int gpiochip_setup_dev(struct gpio_device *gdev)
961{
962 int status;
963
964 cdev_init(&gdev->chrdev, &gpio_fileops);
965 gdev->chrdev.owner = THIS_MODULE;
966 gdev->chrdev.kobj.parent = &gdev->dev.kobj;
967 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
968 status = cdev_add(&gdev->chrdev, gdev->dev.devt, 1);
969 if (status < 0)
970 chip_warn(gdev->chip, "failed to add char device %d:%d\n",
971 MAJOR(gpio_devt), gdev->id);
972 else
973 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
974 MAJOR(gpio_devt), gdev->id);
975 status = device_add(&gdev->dev);
976 if (status)
977 goto err_remove_chardev;
978
979 status = gpiochip_sysfs_register(gdev);
980 if (status)
981 goto err_remove_device;
982
983 /* From this point, the .release() function cleans up gpio_device */
984 gdev->dev.release = gpiodevice_release;
Guenter Roeck159f3cd2016-03-31 08:11:30 -0700985 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
986 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
987 dev_name(&gdev->dev), gdev->chip->label ? : "generic");
988
989 return 0;
990
991err_remove_device:
992 device_del(&gdev->dev);
993err_remove_chardev:
994 cdev_del(&gdev->chrdev);
995 return status;
996}
997
998static void gpiochip_setup_devs(void)
999{
1000 struct gpio_device *gdev;
1001 int err;
1002
1003 list_for_each_entry(gdev, &gpio_devices, list) {
1004 err = gpiochip_setup_dev(gdev);
1005 if (err)
1006 pr_err("%s: Failed to initialize gpio device (%d)\n",
1007 dev_name(&gdev->dev), err);
1008 }
1009}
1010
Anton Vorontsov169b6a72008-04-28 02:14:47 -07001011/**
Linus Walleijb08ea352015-12-03 15:14:13 +01001012 * gpiochip_add_data() - register a gpio_chip
David Brownelld2876d02008-02-04 22:28:20 -08001013 * @chip: the chip to register, with chip->base initialized
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001014 * Context: potentially before irqs will work
David Brownelld2876d02008-02-04 22:28:20 -08001015 *
1016 * Returns a negative errno if the chip can't be registered, such as
1017 * because the chip->base is invalid or already associated with a
1018 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001019 *
Linus Walleijb08ea352015-12-03 15:14:13 +01001020 * When gpiochip_add_data() is called very early during boot, so that GPIOs
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08001021 * can be freely used, the chip->parent device must be registered before
David Brownelld8f388d82008-07-25 01:46:07 -07001022 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1023 * for GPIOs will fail rudely.
1024 *
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001025 * gpiochip_add_data() must only be called after gpiolib initialization,
1026 * ie after core_initcall().
1027 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001028 * If chip->base is negative, this requests dynamic assignment of
1029 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001030 */
Linus Walleijb08ea352015-12-03 15:14:13 +01001031int gpiochip_add_data(struct gpio_chip *chip, void *data)
David Brownelld2876d02008-02-04 22:28:20 -08001032{
1033 unsigned long flags;
1034 int status = 0;
Linus Walleijff2b1352015-10-20 11:10:38 +02001035 unsigned i;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001036 int base = chip->base;
Linus Walleijff2b1352015-10-20 11:10:38 +02001037 struct gpio_device *gdev;
David Brownelld2876d02008-02-04 22:28:20 -08001038
Linus Walleijff2b1352015-10-20 11:10:38 +02001039 /*
1040 * First: allocate and populate the internal stat container, and
1041 * set up the struct device.
1042 */
Josh Cartwright969f07b2016-02-17 16:44:15 -06001043 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
Linus Walleijff2b1352015-10-20 11:10:38 +02001044 if (!gdev)
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001045 return -ENOMEM;
Linus Walleij3c702e92015-10-21 15:29:53 +02001046 gdev->dev.bus = &gpio_bus_type;
Linus Walleijff2b1352015-10-20 11:10:38 +02001047 gdev->chip = chip;
1048 chip->gpiodev = gdev;
1049 if (chip->parent) {
1050 gdev->dev.parent = chip->parent;
1051 gdev->dev.of_node = chip->parent->of_node;
Thierry Redingacc6e332016-07-05 14:11:14 +02001052 }
1053
Linus Walleijff2b1352015-10-20 11:10:38 +02001054#ifdef CONFIG_OF_GPIO
1055 /* If the gpiochip has an assigned OF node this takes precedence */
Thierry Redingacc6e332016-07-05 14:11:14 +02001056 if (chip->of_node)
1057 gdev->dev.of_node = chip->of_node;
Linus Walleijff2b1352015-10-20 11:10:38 +02001058#endif
Thierry Redingacc6e332016-07-05 14:11:14 +02001059
Linus Walleijff2b1352015-10-20 11:10:38 +02001060 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
1061 if (gdev->id < 0) {
1062 status = gdev->id;
1063 goto err_free_gdev;
1064 }
1065 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
1066 device_initialize(&gdev->dev);
1067 dev_set_drvdata(&gdev->dev, gdev);
1068 if (chip->parent && chip->parent->driver)
1069 gdev->owner = chip->parent->driver->owner;
1070 else if (chip->owner)
1071 /* TODO: remove chip->owner */
1072 gdev->owner = chip->owner;
1073 else
1074 gdev->owner = THIS_MODULE;
David Brownelld2876d02008-02-04 22:28:20 -08001075
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001076 gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001077 if (!gdev->descs) {
Linus Walleijff2b1352015-10-20 11:10:38 +02001078 status = -ENOMEM;
1079 goto err_free_gdev;
1080 }
1081
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001082 if (chip->ngpio == 0) {
1083 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
Linus Walleijff2b1352015-10-20 11:10:38 +02001084 status = -EINVAL;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001085 goto err_free_descs;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001086 }
Linus Walleijdf4878e2016-02-12 14:48:23 +01001087
1088 if (chip->label)
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001089 gdev->label = kstrdup(chip->label, GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001090 else
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001091 gdev->label = kstrdup("unknown", GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001092 if (!gdev->label) {
1093 status = -ENOMEM;
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001094 goto err_free_descs;
Linus Walleijdf4878e2016-02-12 14:48:23 +01001095 }
1096
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001097 gdev->ngpio = chip->ngpio;
Linus Walleij43c54ec2016-02-11 11:37:48 +01001098 gdev->data = data;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001099
David Brownelld2876d02008-02-04 22:28:20 -08001100 spin_lock_irqsave(&gpio_lock, flags);
1101
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001102 /*
1103 * TODO: this allocates a Linux GPIO number base in the global
1104 * GPIO numberspace for this chip. In the long run we want to
1105 * get *rid* of this numberspace and use only descriptors, but
1106 * it may be a pipe dream. It will not happen before we get rid
1107 * of the sysfs interface anyways.
1108 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001109 if (base < 0) {
1110 base = gpiochip_find_base(chip->ngpio);
1111 if (base < 0) {
1112 status = base;
Johan Hovold225fce82015-01-12 17:12:25 +01001113 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001114 goto err_free_label;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001115 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001116 /*
1117 * TODO: it should not be necessary to reflect the assigned
1118 * base outside of the GPIO subsystem. Go over drivers and
1119 * see if anyone makes use of this, else drop this and assign
1120 * a poison instead.
1121 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001122 chip->base = base;
1123 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001124 gdev->base = base;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001125
Linus Walleijff2b1352015-10-20 11:10:38 +02001126 status = gpiodev_add_to_list(gdev);
Johan Hovold05aa5202015-01-12 17:12:26 +01001127 if (status) {
1128 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001129 goto err_free_label;
Johan Hovold05aa5202015-01-12 17:12:26 +01001130 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001131
Linus Walleij545ebd92016-05-30 17:11:59 +02001132 spin_unlock_irqrestore(&gpio_lock, flags);
1133
Linus Walleijff2b1352015-10-20 11:10:38 +02001134 for (i = 0; i < chip->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001135 struct gpio_desc *desc = &gdev->descs[i];
David Brownelld8f388d82008-07-25 01:46:07 -07001136
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001137 desc->gdev = gdev;
Linus Walleij72d32002016-04-28 13:33:59 +02001138 /*
1139 * REVISIT: most hardware initializes GPIOs as inputs
1140 * (often with pullups enabled) so power usage is
1141 * minimized. Linux code should set the gpio direction
1142 * first thing; but until it does, and in case
1143 * chip->get_direction is not set, we may expose the
1144 * wrong direction in sysfs.
Johan Hovold05aa5202015-01-12 17:12:26 +01001145 */
Linus Walleij72d32002016-04-28 13:33:59 +02001146
1147 if (chip->get_direction) {
1148 /*
1149 * If we have .get_direction, set up the initial
1150 * direction flag from the hardware.
1151 */
1152 int dir = chip->get_direction(chip, i);
1153
1154 if (!dir)
1155 set_bit(FLAG_IS_OUT, &desc->flags);
1156 } else if (!chip->direction_input) {
1157 /*
1158 * If the chip lacks the .direction_input callback
1159 * we logically assume all lines are outputs.
1160 */
1161 set_bit(FLAG_IS_OUT, &desc->flags);
1162 }
David Brownelld2876d02008-02-04 22:28:20 -08001163 }
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001164
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301165#ifdef CONFIG_PINCTRL
Linus Walleij20ec3e32016-02-11 11:03:06 +01001166 INIT_LIST_HEAD(&gdev->pin_ranges);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301167#endif
1168
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001169 status = gpiochip_set_desc_names(chip);
1170 if (status)
1171 goto err_remove_from_list;
1172
Mika Westerberg79b804c2016-09-20 15:15:21 +03001173 status = gpiochip_irqchip_init_valid_mask(chip);
1174 if (status)
1175 goto err_remove_from_list;
1176
Tomeu Vizoso28355f82015-07-14 10:29:54 +02001177 status = of_gpiochip_add(chip);
1178 if (status)
1179 goto err_remove_chip;
1180
Mika Westerberg664e3e52014-01-08 12:40:54 +02001181 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001182
Linus Walleij3c702e92015-10-21 15:29:53 +02001183 /*
1184 * By first adding the chardev, and then adding the device,
1185 * we get a device node entry in sysfs under
1186 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1187 * coldplug of device nodes and other udev business.
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001188 * We can do this only if gpiolib has been initialized.
1189 * Otherwise, defer until later.
Linus Walleij3c702e92015-10-21 15:29:53 +02001190 */
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001191 if (gpiolib_initialized) {
1192 status = gpiochip_setup_dev(gdev);
1193 if (status)
1194 goto err_remove_chip;
1195 }
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001196 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001197
Johan Hovold225fce82015-01-12 17:12:25 +01001198err_remove_chip:
1199 acpi_gpiochip_remove(chip);
Johan Hovold6d867502015-05-04 17:23:25 +02001200 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +01001201 of_gpiochip_remove(chip);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001202 gpiochip_irqchip_free_valid_mask(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001203err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +01001204 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001205 list_del(&gdev->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001206 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001207err_free_label:
1208 kfree(gdev->label);
1209err_free_descs:
1210 kfree(gdev->descs);
Linus Walleijff2b1352015-10-20 11:10:38 +02001211err_free_gdev:
1212 ida_simple_remove(&gpio_ida, gdev->id);
David Brownelld2876d02008-02-04 22:28:20 -08001213 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +02001214 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001215 gdev->base, gdev->base + gdev->ngpio - 1,
1216 chip->label ? : "generic");
1217 kfree(gdev);
David Brownelld2876d02008-02-04 22:28:20 -08001218 return status;
1219}
Linus Walleijb08ea352015-12-03 15:14:13 +01001220EXPORT_SYMBOL_GPL(gpiochip_add_data);
David Brownelld2876d02008-02-04 22:28:20 -08001221
1222/**
Linus Walleij43c54ec2016-02-11 11:37:48 +01001223 * gpiochip_get_data() - get per-subdriver data for the chip
1224 */
1225void *gpiochip_get_data(struct gpio_chip *chip)
1226{
1227 return chip->gpiodev->data;
1228}
1229EXPORT_SYMBOL_GPL(gpiochip_get_data);
1230
1231/**
David Brownelld2876d02008-02-04 22:28:20 -08001232 * gpiochip_remove() - unregister a gpio_chip
1233 * @chip: the chip to unregister
1234 *
1235 * A gpio_chip with any GPIOs still requested may not be removed.
1236 */
abdoulaye berthee1db1702014-07-05 18:28:50 +02001237void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -08001238{
Linus Walleijff2b1352015-10-20 11:10:38 +02001239 struct gpio_device *gdev = chip->gpiodev;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001240 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001241 unsigned long flags;
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001242 unsigned i;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001243 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -08001244
Linus Walleijff2b1352015-10-20 11:10:38 +02001245 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
Linus Walleijafbc4f32016-02-09 13:21:06 +01001246 gpiochip_sysfs_unregister(gdev);
Bamvor Jian Zhangbd203bd2016-02-20 13:13:19 +08001247 /* Numb the device, cancelling all outstanding operations */
1248 gdev->chip = NULL;
Johan Hovold00acc3d2015-01-12 17:12:27 +01001249 gpiochip_irqchip_remove(chip);
Mika Westerberg6072b9d2014-03-10 14:54:53 +02001250 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +01001251 gpiochip_remove_pin_ranges(chip);
Benoit Parrotf625d462015-02-02 11:44:44 -06001252 gpiochip_free_hogs(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001253 of_gpiochip_remove(chip);
Linus Walleij43c54ec2016-02-11 11:37:48 +01001254 /*
1255 * We accept no more calls into the driver from this point, so
1256 * NULL the driver data pointer
1257 */
1258 gdev->data = NULL;
Anton Vorontsov391c9702010-06-08 07:48:17 -06001259
Johan Hovold6798aca2015-01-12 17:12:28 +01001260 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001261 for (i = 0; i < gdev->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001262 desc = &gdev->descs[i];
Johan Hovoldfab28b82015-05-04 17:10:27 +02001263 if (test_bit(FLAG_REQUESTED, &desc->flags))
1264 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -08001265 }
David Brownelld2876d02008-02-04 22:28:20 -08001266 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001267
Johan Hovoldfab28b82015-05-04 17:10:27 +02001268 if (requested)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001269 dev_crit(&gdev->dev,
Linus Walleij58383c782015-11-04 09:56:26 +01001270 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +02001271
Linus Walleijff2b1352015-10-20 11:10:38 +02001272 /*
1273 * The gpiochip side puts its use of the device to rest here:
1274 * if there are no userspace clients, the chardev and device will
1275 * be removed, else it will be dangling until the last user is
1276 * gone.
1277 */
Ricardo Ribalda Delgadof4833b82016-06-03 19:10:02 +02001278 cdev_del(&gdev->chrdev);
1279 device_del(&gdev->dev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001280 put_device(&gdev->dev);
David Brownelld2876d02008-02-04 22:28:20 -08001281}
1282EXPORT_SYMBOL_GPL(gpiochip_remove);
1283
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301284static void devm_gpio_chip_release(struct device *dev, void *res)
1285{
1286 struct gpio_chip *chip = *(struct gpio_chip **)res;
1287
1288 gpiochip_remove(chip);
1289}
1290
1291static int devm_gpio_chip_match(struct device *dev, void *res, void *data)
1292
1293{
1294 struct gpio_chip **r = res;
1295
1296 if (!r || !*r) {
1297 WARN_ON(!r || !*r);
1298 return 0;
1299 }
1300
1301 return *r == data;
1302}
1303
1304/**
1305 * devm_gpiochip_add_data() - Resource manager piochip_add_data()
1306 * @dev: the device pointer on which irq_chip belongs to.
1307 * @chip: the chip to register, with chip->base initialized
1308 * Context: potentially before irqs will work
1309 *
1310 * Returns a negative errno if the chip can't be registered, such as
1311 * because the chip->base is invalid or already associated with a
1312 * different chip. Otherwise it returns zero as a success code.
1313 *
1314 * The gpio chip automatically be released when the device is unbound.
1315 */
1316int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
1317 void *data)
1318{
1319 struct gpio_chip **ptr;
1320 int ret;
1321
1322 ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
1323 GFP_KERNEL);
1324 if (!ptr)
1325 return -ENOMEM;
1326
1327 ret = gpiochip_add_data(chip, data);
1328 if (ret < 0) {
1329 devres_free(ptr);
1330 return ret;
1331 }
1332
1333 *ptr = chip;
1334 devres_add(dev, ptr);
1335
1336 return 0;
1337}
1338EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
1339
1340/**
1341 * devm_gpiochip_remove() - Resource manager of gpiochip_remove()
1342 * @dev: device for which which resource was allocated
1343 * @chip: the chip to remove
1344 *
1345 * A gpio_chip with any GPIOs still requested may not be removed.
1346 */
1347void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip)
1348{
1349 int ret;
1350
1351 ret = devres_release(dev, devm_gpio_chip_release,
1352 devm_gpio_chip_match, chip);
1353 if (!ret)
1354 WARN_ON(ret);
1355}
1356EXPORT_SYMBOL_GPL(devm_gpiochip_remove);
1357
Grant Likely594fa262010-06-08 07:48:16 -06001358/**
1359 * gpiochip_find() - iterator for locating a specific gpio_chip
1360 * @data: data to pass to match function
1361 * @callback: Callback function to check gpio_chip
1362 *
1363 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1364 * determined by a user supplied @match callback. The callback should return
1365 * 0 if the device doesn't match and non-zero if it does. If the callback is
1366 * non-zero, this function will return to the caller and not iterate over any
1367 * more gpio_chips.
1368 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001369struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001370 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001371 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001372{
Linus Walleijff2b1352015-10-20 11:10:38 +02001373 struct gpio_device *gdev;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001374 struct gpio_chip *chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -06001375 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001376
1377 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001378 list_for_each_entry(gdev, &gpio_devices, list)
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001379 if (gdev->chip && match(gdev->chip, data)) {
1380 chip = gdev->chip;
Grant Likely594fa262010-06-08 07:48:16 -06001381 break;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001382 }
Linus Walleijff2b1352015-10-20 11:10:38 +02001383
Grant Likely594fa262010-06-08 07:48:16 -06001384 spin_unlock_irqrestore(&gpio_lock, flags);
1385
1386 return chip;
1387}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001388EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001389
Alexandre Courbot79697ef2013-11-16 21:39:32 +09001390static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1391{
1392 const char *name = data;
1393
1394 return !strcmp(chip->label, name);
1395}
1396
1397static struct gpio_chip *find_chip_by_name(const char *name)
1398{
1399 return gpiochip_find((void *)name, gpiochip_match_name);
1400}
1401
Linus Walleij14250522014-03-25 10:40:18 +01001402#ifdef CONFIG_GPIOLIB_IRQCHIP
1403
1404/*
1405 * The following is irqchip helper code for gpiochips.
1406 */
1407
Mika Westerberg79b804c2016-09-20 15:15:21 +03001408static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1409{
1410 int i;
1411
1412 if (!gpiochip->irq_need_valid_mask)
1413 return 0;
1414
1415 gpiochip->irq_valid_mask = kcalloc(BITS_TO_LONGS(gpiochip->ngpio),
1416 sizeof(long), GFP_KERNEL);
1417 if (!gpiochip->irq_valid_mask)
1418 return -ENOMEM;
1419
1420 /* Assume by default all GPIOs are valid */
1421 for (i = 0; i < gpiochip->ngpio; i++)
1422 set_bit(i, gpiochip->irq_valid_mask);
1423
1424 return 0;
1425}
1426
1427static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1428{
1429 kfree(gpiochip->irq_valid_mask);
1430 gpiochip->irq_valid_mask = NULL;
1431}
1432
1433static bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
1434 unsigned int offset)
1435{
1436 /* No mask means all valid */
1437 if (likely(!gpiochip->irq_valid_mask))
1438 return true;
1439 return test_bit(offset, gpiochip->irq_valid_mask);
1440}
1441
Linus Walleij14250522014-03-25 10:40:18 +01001442/**
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001443 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
1444 * @gpiochip: the gpiochip to set the irqchip chain to
1445 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01001446 * @parent_irq: the irq number corresponding to the parent IRQ for this
1447 * chained irqchip
1448 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001449 * coming out of the gpiochip. If the interrupt is nested rather than
1450 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +01001451 */
1452void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
1453 struct irq_chip *irqchip,
1454 int parent_irq,
1455 irq_flow_handler_t parent_handler)
1456{
Linus Walleij83141a72014-09-26 13:50:12 +02001457 unsigned int offset;
1458
Linus Walleij83141a72014-09-26 13:50:12 +02001459 if (!gpiochip->irqdomain) {
1460 chip_err(gpiochip, "called %s before setting up irqchip\n",
1461 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +02001462 return;
1463 }
1464
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001465 if (parent_handler) {
1466 if (gpiochip->can_sleep) {
1467 chip_err(gpiochip,
1468 "you cannot have chained interrupts on a "
1469 "chip that may sleep\n");
1470 return;
1471 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001472 /*
1473 * The parent irqchip is already using the chip_data for this
1474 * irqchip, so our callbacks simply use the handler_data.
1475 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +02001476 irq_set_chained_handler_and_data(parent_irq, parent_handler,
1477 gpiochip);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001478
1479 gpiochip->irq_parent = parent_irq;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001480 }
Linus Walleij83141a72014-09-26 13:50:12 +02001481
1482 /* Set the parent IRQ for all affected IRQs */
Mika Westerberg79b804c2016-09-20 15:15:21 +03001483 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1484 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1485 continue;
Linus Walleij83141a72014-09-26 13:50:12 +02001486 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
1487 parent_irq);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001488 }
Linus Walleij14250522014-03-25 10:40:18 +01001489}
1490EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
1491
1492/**
1493 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1494 * @d: the irqdomain used by this irqchip
1495 * @irq: the global irq number used by this GPIO irqchip irq
1496 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1497 *
1498 * This function will set up the mapping for a certain IRQ line on a
1499 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1500 * stored inside the gpiochip.
1501 */
1502static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1503 irq_hw_number_t hwirq)
1504{
1505 struct gpio_chip *chip = d->host_data;
1506
Linus Walleij14250522014-03-25 10:40:18 +01001507 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001508 /*
1509 * This lock class tells lockdep that GPIO irqs are in a different
1510 * category than their parents, so it won't report false recursion.
1511 */
1512 irq_set_lockdep_class(irq, chip->lock_key);
Linus Walleij7633fb92014-04-09 13:20:38 +02001513 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
Linus Walleij1c8732b2014-04-09 13:34:39 +02001514 /* Chips that can sleep need nested thread handlers */
Octavian Purdila295494a2014-09-19 23:22:44 +03001515 if (chip->can_sleep && !chip->irq_not_threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02001516 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +01001517 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -05001518
Linus Walleij1333b902014-04-23 16:45:12 +02001519 /*
1520 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1521 * is passed as default type.
1522 */
1523 if (chip->irq_default_type != IRQ_TYPE_NONE)
1524 irq_set_irq_type(irq, chip->irq_default_type);
Linus Walleij14250522014-03-25 10:40:18 +01001525
1526 return 0;
1527}
1528
Linus Walleijc3626fd2014-03-28 20:42:01 +01001529static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
1530{
Linus Walleij1c8732b2014-04-09 13:34:39 +02001531 struct gpio_chip *chip = d->host_data;
1532
Linus Walleij1c8732b2014-04-09 13:34:39 +02001533 if (chip->can_sleep)
1534 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001535 irq_set_chip_and_handler(irq, NULL, NULL);
1536 irq_set_chip_data(irq, NULL);
1537}
1538
Linus Walleij14250522014-03-25 10:40:18 +01001539static const struct irq_domain_ops gpiochip_domain_ops = {
1540 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +01001541 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +01001542 /* Virtually all GPIO irqchips are twocell:ed */
1543 .xlate = irq_domain_xlate_twocell,
1544};
1545
1546static int gpiochip_irq_reqres(struct irq_data *d)
1547{
1548 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1549
Linus Walleijff2b1352015-10-20 11:10:38 +02001550 if (!try_module_get(chip->gpiodev->owner))
Grygorii Strashko5b76e792015-06-25 20:30:50 +03001551 return -ENODEV;
1552
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001553 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +01001554 chip_err(chip,
1555 "unable to lock HW IRQ %lu for IRQ\n",
1556 d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001557 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001558 return -EINVAL;
1559 }
1560 return 0;
1561}
1562
1563static void gpiochip_irq_relres(struct irq_data *d)
1564{
1565 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1566
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001567 gpiochip_unlock_as_irq(chip, d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001568 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001569}
1570
1571static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
1572{
1573 return irq_find_mapping(chip->irqdomain, offset);
1574}
1575
1576/**
1577 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1578 * @gpiochip: the gpiochip to remove the irqchip from
1579 *
1580 * This is called only from gpiochip_remove()
1581 */
1582static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
1583{
Linus Walleijc3626fd2014-03-28 20:42:01 +01001584 unsigned int offset;
1585
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001586 acpi_gpiochip_free_interrupts(gpiochip);
1587
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001588 if (gpiochip->irq_parent) {
1589 irq_set_chained_handler(gpiochip->irq_parent, NULL);
1590 irq_set_handler_data(gpiochip->irq_parent, NULL);
1591 }
1592
Linus Walleijc3626fd2014-03-28 20:42:01 +01001593 /* Remove all IRQ mappings and delete the domain */
1594 if (gpiochip->irqdomain) {
Mika Westerberg79b804c2016-09-20 15:15:21 +03001595 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1596 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1597 continue;
Grygorii Strashkoe3893382014-09-25 19:09:23 +03001598 irq_dispose_mapping(
1599 irq_find_mapping(gpiochip->irqdomain, offset));
Mika Westerberg79b804c2016-09-20 15:15:21 +03001600 }
Linus Walleij14250522014-03-25 10:40:18 +01001601 irq_domain_remove(gpiochip->irqdomain);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001602 }
Linus Walleij14250522014-03-25 10:40:18 +01001603
1604 if (gpiochip->irqchip) {
1605 gpiochip->irqchip->irq_request_resources = NULL;
1606 gpiochip->irqchip->irq_release_resources = NULL;
1607 gpiochip->irqchip = NULL;
1608 }
Mika Westerberg79b804c2016-09-20 15:15:21 +03001609
1610 gpiochip_irqchip_free_valid_mask(gpiochip);
Linus Walleij14250522014-03-25 10:40:18 +01001611}
1612
1613/**
1614 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
1615 * @gpiochip: the gpiochip to add the irqchip to
1616 * @irqchip: the irqchip to add to the gpiochip
1617 * @first_irq: if not dynamically assigned, the base (first) IRQ to
1618 * allocate gpiochip irqs from
1619 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +02001620 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
1621 * to have the core avoid setting up any default type in the hardware.
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001622 * @lock_key: lockdep class
Linus Walleij14250522014-03-25 10:40:18 +01001623 *
1624 * This function closely associates a certain irqchip with a certain
1625 * gpiochip, providing an irq domain to translate the local IRQs to
1626 * global irqs in the gpiolib core, and making sure that the gpiochip
1627 * is passed as chip data to all related functions. Driver callbacks
Linus Walleij09dd5f92015-12-07 15:31:58 +01001628 * need to use gpiochip_get_data() to get their local state containers back
Linus Walleij14250522014-03-25 10:40:18 +01001629 * from the gpiochip passed as chip data. An irqdomain will be stored
1630 * in the gpiochip that shall be used by the driver to handle IRQ number
1631 * translation. The gpiochip will need to be initialized and registered
1632 * before calling this function.
1633 *
Linus Walleijc3626fd2014-03-28 20:42:01 +01001634 * This function will handle two cell:ed simple IRQs and assumes all
1635 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +01001636 * need to be open coded.
1637 */
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001638int _gpiochip_irqchip_add(struct gpio_chip *gpiochip,
1639 struct irq_chip *irqchip,
1640 unsigned int first_irq,
1641 irq_flow_handler_t handler,
1642 unsigned int type,
1643 struct lock_class_key *lock_key)
Linus Walleij14250522014-03-25 10:40:18 +01001644{
1645 struct device_node *of_node;
Mika Westerberg79b804c2016-09-20 15:15:21 +03001646 bool irq_base_set = false;
Linus Walleij14250522014-03-25 10:40:18 +01001647 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +01001648 unsigned irq_base = 0;
Linus Walleij14250522014-03-25 10:40:18 +01001649
1650 if (!gpiochip || !irqchip)
1651 return -EINVAL;
1652
Linus Walleij58383c782015-11-04 09:56:26 +01001653 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +01001654 pr_err("missing gpiochip .dev parent pointer\n");
1655 return -EINVAL;
1656 }
Linus Walleij58383c782015-11-04 09:56:26 +01001657 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +01001658#ifdef CONFIG_OF_GPIO
1659 /*
Colin Cronin20a8a962015-05-18 11:41:43 -07001660 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08001661 * FIXME: get rid of this and use gpiochip->parent->of_node
1662 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +01001663 */
1664 if (gpiochip->of_node)
1665 of_node = gpiochip->of_node;
1666#endif
Marc Zyngier332e99d2016-09-07 09:12:11 +01001667 /*
Mika Westerberg0a1e0052016-09-12 14:29:51 +03001668 * Specifying a default trigger is a terrible idea if DT or ACPI is
Marc Zyngier332e99d2016-09-07 09:12:11 +01001669 * used to configure the interrupts, as you may end-up with
1670 * conflicting triggers. Tell the user, and reset to NONE.
1671 */
1672 if (WARN(of_node && type != IRQ_TYPE_NONE,
1673 "%s: Ignoring %d default trigger\n", of_node->full_name, type))
1674 type = IRQ_TYPE_NONE;
Mika Westerberg0a1e0052016-09-12 14:29:51 +03001675 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
1676 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1677 "Ignoring %d default trigger\n", type);
1678 type = IRQ_TYPE_NONE;
1679 }
Marc Zyngier332e99d2016-09-07 09:12:11 +01001680
Linus Walleij14250522014-03-25 10:40:18 +01001681 gpiochip->irqchip = irqchip;
1682 gpiochip->irq_handler = handler;
1683 gpiochip->irq_default_type = type;
1684 gpiochip->to_irq = gpiochip_to_irq;
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001685 gpiochip->lock_key = lock_key;
Linus Walleij14250522014-03-25 10:40:18 +01001686 gpiochip->irqdomain = irq_domain_add_simple(of_node,
1687 gpiochip->ngpio, first_irq,
1688 &gpiochip_domain_ops, gpiochip);
1689 if (!gpiochip->irqdomain) {
1690 gpiochip->irqchip = NULL;
1691 return -EINVAL;
1692 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +02001693
1694 /*
1695 * It is possible for a driver to override this, but only if the
1696 * alternative functions are both implemented.
1697 */
1698 if (!irqchip->irq_request_resources &&
1699 !irqchip->irq_release_resources) {
1700 irqchip->irq_request_resources = gpiochip_irq_reqres;
1701 irqchip->irq_release_resources = gpiochip_irq_relres;
1702 }
Linus Walleij14250522014-03-25 10:40:18 +01001703
1704 /*
1705 * Prepare the mapping since the irqchip shall be orthogonal to
1706 * any gpiochip calls. If the first_irq was zero, this is
1707 * necessary to allocate descriptors for all IRQs.
1708 */
Linus Walleijc3626fd2014-03-28 20:42:01 +01001709 for (offset = 0; offset < gpiochip->ngpio; offset++) {
Mika Westerberg79b804c2016-09-20 15:15:21 +03001710 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1711 continue;
Linus Walleijc3626fd2014-03-28 20:42:01 +01001712 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001713 if (!irq_base_set) {
Linus Walleijc3626fd2014-03-28 20:42:01 +01001714 /*
1715 * Store the base into the gpiochip to be used when
1716 * unmapping the irqs.
1717 */
1718 gpiochip->irq_base = irq_base;
Mika Westerberg79b804c2016-09-20 15:15:21 +03001719 irq_base_set = true;
1720 }
Linus Walleijc3626fd2014-03-28 20:42:01 +01001721 }
Linus Walleij14250522014-03-25 10:40:18 +01001722
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001723 acpi_gpiochip_request_interrupts(gpiochip);
1724
Linus Walleij14250522014-03-25 10:40:18 +01001725 return 0;
1726}
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001727EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add);
Linus Walleij14250522014-03-25 10:40:18 +01001728
1729#else /* CONFIG_GPIOLIB_IRQCHIP */
1730
1731static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
Mika Westerberg79b804c2016-09-20 15:15:21 +03001732static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1733{
1734 return 0;
1735}
1736static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1737{ }
Linus Walleij14250522014-03-25 10:40:18 +01001738
1739#endif /* CONFIG_GPIOLIB_IRQCHIP */
1740
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001741/**
1742 * gpiochip_generic_request() - request the gpio function for a pin
1743 * @chip: the gpiochip owning the GPIO
1744 * @offset: the offset of the GPIO to request for GPIO function
1745 */
1746int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
1747{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001748 return pinctrl_request_gpio(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001749}
1750EXPORT_SYMBOL_GPL(gpiochip_generic_request);
1751
1752/**
1753 * gpiochip_generic_free() - free the gpio function from a pin
1754 * @chip: the gpiochip to request the gpio function for
1755 * @offset: the offset of the GPIO to free from GPIO function
1756 */
1757void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
1758{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001759 pinctrl_free_gpio(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001760}
1761EXPORT_SYMBOL_GPL(gpiochip_generic_free);
1762
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301763#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01001764
Linus Walleij3f0f8672012-11-20 12:40:15 +01001765/**
Christian Ruppert586a87e2013-10-15 15:37:54 +02001766 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1767 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +02001768 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +02001769 * @gpio_offset: the start offset in the current gpio_chip number space
1770 * @pin_group: name of the pin group inside the pin controller
1771 */
1772int gpiochip_add_pingroup_range(struct gpio_chip *chip,
1773 struct pinctrl_dev *pctldev,
1774 unsigned int gpio_offset, const char *pin_group)
1775{
1776 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001777 struct gpio_device *gdev = chip->gpiodev;
Christian Ruppert586a87e2013-10-15 15:37:54 +02001778 int ret;
1779
1780 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1781 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001782 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +02001783 return -ENOMEM;
1784 }
1785
1786 /* Use local offset as range ID */
1787 pin_range->range.id = gpio_offset;
1788 pin_range->range.gc = chip;
1789 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001790 pin_range->range.base = gdev->base + gpio_offset;
Christian Ruppert586a87e2013-10-15 15:37:54 +02001791 pin_range->pctldev = pctldev;
1792
1793 ret = pinctrl_get_group_pins(pctldev, pin_group,
1794 &pin_range->range.pins,
1795 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001796 if (ret < 0) {
1797 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001798 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001799 }
Christian Ruppert586a87e2013-10-15 15:37:54 +02001800
1801 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1802
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001803 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1804 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +02001805 pinctrl_dev_get_devname(pctldev), pin_group);
1806
Linus Walleij20ec3e32016-02-11 11:03:06 +01001807 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001808
1809 return 0;
1810}
1811EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1812
1813/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01001814 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1815 * @chip: the gpiochip to add the range for
1816 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01001817 * @gpio_offset: the start offset in the current gpio_chip number space
1818 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01001819 * @npins: the number of pins from the offset of each pin space (GPIO and
1820 * pin controller) to accumulate in this range
1821 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001822int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01001823 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01001824 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301825{
1826 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001827 struct gpio_device *gdev = chip->gpiodev;
Axel Linb4d4b1f2012-11-21 14:33:56 +08001828 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301829
Linus Walleij3f0f8672012-11-20 12:40:15 +01001830 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301831 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001832 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001833 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301834 }
1835
Linus Walleij3f0f8672012-11-20 12:40:15 +01001836 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01001837 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001838 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301839 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001840 pin_range->range.base = gdev->base + gpio_offset;
Linus Walleij316511c2012-11-21 08:48:09 +01001841 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301842 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01001843 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301844 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01001845 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08001846 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001847 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +01001848 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08001849 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001850 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001851 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1852 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +01001853 pinctl_name,
1854 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301855
Linus Walleij20ec3e32016-02-11 11:03:06 +01001856 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001857
1858 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301859}
Linus Walleij165adc92012-11-06 14:49:39 +01001860EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301861
Linus Walleij3f0f8672012-11-20 12:40:15 +01001862/**
1863 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1864 * @chip: the chip to remove all the mappings for
1865 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301866void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1867{
1868 struct gpio_pin_range *pin_range, *tmp;
Linus Walleij20ec3e32016-02-11 11:03:06 +01001869 struct gpio_device *gdev = chip->gpiodev;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301870
Linus Walleij20ec3e32016-02-11 11:03:06 +01001871 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301872 list_del(&pin_range->node);
1873 pinctrl_remove_gpio_range(pin_range->pctldev,
1874 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001875 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301876 }
1877}
Linus Walleij165adc92012-11-06 14:49:39 +01001878EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1879
1880#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301881
David Brownelld2876d02008-02-04 22:28:20 -08001882/* These "optional" allocation calls help prevent drivers from stomping
1883 * on each other, and help provide better diagnostics in debugfs.
1884 * They're called even less than the "set direction" calls.
1885 */
Mika Westerberg77c2d792014-03-10 14:54:50 +02001886static int __gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08001887{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001888 struct gpio_chip *chip = desc->gdev->chip;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001889 int status;
David Brownelld2876d02008-02-04 22:28:20 -08001890 unsigned long flags;
1891
1892 spin_lock_irqsave(&gpio_lock, flags);
1893
David Brownelld2876d02008-02-04 22:28:20 -08001894 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07001895 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001896 */
1897
1898 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1899 desc_set_label(desc, label ? : "?");
1900 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001901 } else {
David Brownelld2876d02008-02-04 22:28:20 -08001902 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -08001903 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001904 }
1905
1906 if (chip->request) {
1907 /* chip->request may sleep */
1908 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001909 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001910 spin_lock_irqsave(&gpio_lock, flags);
1911
1912 if (status < 0) {
1913 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -07001914 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001915 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001916 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001917 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03001918 if (chip->get_direction) {
1919 /* chip->get_direction may sleep */
1920 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001921 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001922 spin_lock_irqsave(&gpio_lock, flags);
1923 }
David Brownelld2876d02008-02-04 22:28:20 -08001924done:
Mika Westerberg77c2d792014-03-10 14:54:50 +02001925 spin_unlock_irqrestore(&gpio_lock, flags);
1926 return status;
1927}
1928
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001929/*
1930 * This descriptor validation needs to be inserted verbatim into each
1931 * function taking a descriptor, so we need to use a preprocessor
Linus Walleij54d77192016-05-30 16:48:39 +02001932 * macro to avoid endless duplication. If the desc is NULL it is an
1933 * optional GPIO and calls should just bail out.
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001934 */
1935#define VALIDATE_DESC(desc) do { \
Linus Walleij54d77192016-05-30 16:48:39 +02001936 if (!desc) \
1937 return 0; \
Linus Walleijbfbbe442016-06-16 11:55:55 +02001938 if (IS_ERR(desc)) { \
1939 pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
1940 return PTR_ERR(desc); \
1941 } \
Linus Walleij54d77192016-05-30 16:48:39 +02001942 if (!desc->gdev) { \
Linus Walleijbfbbe442016-06-16 11:55:55 +02001943 pr_warn("%s: invalid GPIO (no device)\n", __func__); \
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001944 return -EINVAL; \
1945 } \
1946 if ( !desc->gdev->chip ) { \
1947 dev_warn(&desc->gdev->dev, \
1948 "%s: backing chip is gone\n", __func__); \
1949 return 0; \
1950 } } while (0)
1951
1952#define VALIDATE_DESC_VOID(desc) do { \
Linus Walleij54d77192016-05-30 16:48:39 +02001953 if (!desc) \
1954 return; \
Linus Walleijbfbbe442016-06-16 11:55:55 +02001955 if (IS_ERR(desc)) { \
1956 pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
1957 return; \
1958 } \
Linus Walleij54d77192016-05-30 16:48:39 +02001959 if (!desc->gdev) { \
Linus Walleijbfbbe442016-06-16 11:55:55 +02001960 pr_warn("%s: invalid GPIO (no device)\n", __func__); \
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001961 return; \
1962 } \
1963 if (!desc->gdev->chip) { \
1964 dev_warn(&desc->gdev->dev, \
1965 "%s: backing chip is gone\n", __func__); \
1966 return; \
1967 } } while (0)
1968
1969
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09001970int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001971{
1972 int status = -EPROBE_DEFER;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001973 struct gpio_device *gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001974
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001975 VALIDATE_DESC(desc);
1976 gdev = desc->gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001977
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001978 if (try_module_get(gdev->owner)) {
Mika Westerberg77c2d792014-03-10 14:54:50 +02001979 status = __gpiod_request(desc, label);
1980 if (status < 0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001981 module_put(gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01001982 else
1983 get_device(&gdev->dev);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001984 }
1985
David Brownelld2876d02008-02-04 22:28:20 -08001986 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +02001987 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001988
David Brownelld2876d02008-02-04 22:28:20 -08001989 return status;
1990}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001991
Mika Westerberg77c2d792014-03-10 14:54:50 +02001992static bool __gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001993{
Mika Westerberg77c2d792014-03-10 14:54:50 +02001994 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08001995 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07001996 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001997
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07001998 might_sleep();
1999
Alexandre Courbot372e7222013-02-03 01:29:29 +09002000 gpiod_unexport(desc);
David Brownelld8f388d82008-07-25 01:46:07 -07002001
David Brownelld2876d02008-02-04 22:28:20 -08002002 spin_lock_irqsave(&gpio_lock, flags);
2003
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002004 chip = desc->gdev->chip;
David Brownell35e8bb52008-10-15 22:03:16 -07002005 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
2006 if (chip->free) {
2007 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07002008 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002009 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07002010 spin_lock_irqsave(&gpio_lock, flags);
2011 }
David Brownelld2876d02008-02-04 22:28:20 -08002012 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08002013 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07002014 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302015 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302016 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06002017 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002018 ret = true;
2019 }
David Brownelld2876d02008-02-04 22:28:20 -08002020
2021 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002022 return ret;
2023}
2024
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002025void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002026{
Linus Walleij33a68e82016-02-11 10:28:44 +01002027 if (desc && desc->gdev && __gpiod_free(desc)) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002028 module_put(desc->gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002029 put_device(&desc->gdev->dev);
2030 } else {
Mika Westerberg77c2d792014-03-10 14:54:50 +02002031 WARN_ON(extra_checks);
Linus Walleij33a68e82016-02-11 10:28:44 +01002032 }
David Brownelld2876d02008-02-04 22:28:20 -08002033}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002034
David Brownelld2876d02008-02-04 22:28:20 -08002035/**
2036 * gpiochip_is_requested - return string iff signal was requested
2037 * @chip: controller managing the signal
2038 * @offset: of signal within controller's 0..(ngpio - 1) range
2039 *
2040 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09002041 * The string returned is the label passed to gpio_request(); if none has been
2042 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08002043 *
2044 * This function is for use by GPIO controller drivers. The label can
2045 * help with diagnostics, and knowing that the signal is used as a GPIO
2046 * can help avoid accidentally multiplexing it to another controller.
2047 */
2048const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
2049{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002050 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08002051
Dirk Behme48b59532015-08-18 18:02:32 +02002052 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08002053 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002054
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002055 desc = &chip->gpiodev->descs[offset];
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002056
Alexandre Courbot372e7222013-02-03 01:29:29 +09002057 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08002058 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002059 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08002060}
2061EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2062
Mika Westerberg77c2d792014-03-10 14:54:50 +02002063/**
2064 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
2065 * @desc: GPIO descriptor to request
2066 * @label: label for the GPIO
2067 *
2068 * Function allows GPIO chip drivers to request and use their own GPIO
2069 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2070 * function will not increase reference count of the GPIO chip module. This
2071 * allows the GPIO chip module to be unloaded as needed (we assume that the
2072 * GPIO chip driver handles freeing the GPIOs it has requested).
2073 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002074struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
2075 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002076{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002077 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
2078 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002079
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002080 if (IS_ERR(desc)) {
2081 chip_err(chip, "failed to get GPIO descriptor\n");
2082 return desc;
2083 }
2084
2085 err = __gpiod_request(desc, label);
2086 if (err < 0)
2087 return ERR_PTR(err);
2088
2089 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002090}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002091EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002092
2093/**
2094 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2095 * @desc: GPIO descriptor to free
2096 *
2097 * Function frees the given GPIO requested previously with
2098 * gpiochip_request_own_desc().
2099 */
2100void gpiochip_free_own_desc(struct gpio_desc *desc)
2101{
2102 if (desc)
2103 __gpiod_free(desc);
2104}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002105EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08002106
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002107/*
2108 * Drivers MUST set GPIO direction before making get/set calls. In
David Brownelld2876d02008-02-04 22:28:20 -08002109 * some cases this is done in early boot, before IRQs are enabled.
2110 *
2111 * As a rule these aren't called more than once (except for drivers
2112 * using the open-drain emulation idiom) so these are natural places
2113 * to accumulate extra debugging checks. Note that we can't (yet)
2114 * rely on gpio_request() having been called beforehand.
2115 */
2116
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002117/**
2118 * gpiod_direction_input - set the GPIO direction to input
2119 * @desc: GPIO to set to input
2120 *
2121 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2122 * be called safely on it.
2123 *
2124 * Return 0 in case of success, else an error code.
2125 */
2126int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002127{
David Brownelld2876d02008-02-04 22:28:20 -08002128 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002129 int status = -EINVAL;
2130
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002131 VALIDATE_DESC(desc);
2132 chip = desc->gdev->chip;
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002133
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002134 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01002135 gpiod_warn(desc,
2136 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02002137 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002138 return -EIO;
2139 }
2140
Alexandre Courbotd82da792014-07-22 16:17:43 +09002141 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -08002142 if (status == 0)
2143 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002144
Alexandre Courbot372e7222013-02-03 01:29:29 +09002145 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09002146
David Brownelld2876d02008-02-04 22:28:20 -08002147 return status;
2148}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002149EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002150
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002151static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08002152{
Linus Walleijc663e5f2016-03-22 10:51:16 +01002153 struct gpio_chip *gc = desc->gdev->chip;
Linus Walleijad177312016-11-13 23:02:44 +01002154 int val = !!value;
Linus Walleijc663e5f2016-03-22 10:51:16 +01002155 int ret;
David Brownelld2876d02008-02-04 22:28:20 -08002156
Linus Walleijd468bf92013-09-24 11:54:38 +02002157 /* GPIOs used for IRQs shall not be set as output */
2158 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
2159 gpiod_err(desc,
2160 "%s: tried to set a GPIO tied to an IRQ as output\n",
2161 __func__);
2162 return -EIO;
2163 }
2164
Linus Walleijc663e5f2016-03-22 10:51:16 +01002165 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2166 /* First see if we can enable open drain in hardware */
2167 if (gc->set_single_ended) {
2168 ret = gc->set_single_ended(gc, gpio_chip_hwgpio(desc),
2169 LINE_MODE_OPEN_DRAIN);
2170 if (!ret)
2171 goto set_output_value;
2172 }
2173 /* Emulate open drain by not actively driving the line high */
Linus Walleijad177312016-11-13 23:02:44 +01002174 if (val)
Linus Walleijc663e5f2016-03-22 10:51:16 +01002175 return gpiod_direction_input(desc);
2176 }
2177 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2178 if (gc->set_single_ended) {
2179 ret = gc->set_single_ended(gc, gpio_chip_hwgpio(desc),
2180 LINE_MODE_OPEN_SOURCE);
2181 if (!ret)
2182 goto set_output_value;
2183 }
2184 /* Emulate open source by not actively driving the line low */
Linus Walleijad177312016-11-13 23:02:44 +01002185 if (!val)
Linus Walleijc663e5f2016-03-22 10:51:16 +01002186 return gpiod_direction_input(desc);
2187 } else {
2188 /* Make sure to disable open drain/source hardware, if any */
2189 if (gc->set_single_ended)
2190 gc->set_single_ended(gc,
2191 gpio_chip_hwgpio(desc),
2192 LINE_MODE_PUSH_PULL);
2193 }
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302194
Linus Walleijc663e5f2016-03-22 10:51:16 +01002195set_output_value:
2196 if (!gc->set || !gc->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01002197 gpiod_warn(desc,
2198 "%s: missing set() or direction_output() operations\n",
2199 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002200 return -EIO;
2201 }
2202
Linus Walleijad177312016-11-13 23:02:44 +01002203 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
Linus Walleijc663e5f2016-03-22 10:51:16 +01002204 if (!ret)
David Brownelld2876d02008-02-04 22:28:20 -08002205 set_bit(FLAG_IS_OUT, &desc->flags);
Linus Walleijad177312016-11-13 23:02:44 +01002206 trace_gpio_value(desc_to_gpio(desc), 0, val);
Linus Walleijc663e5f2016-03-22 10:51:16 +01002207 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2208 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08002209}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002210
2211/**
2212 * gpiod_direction_output_raw - set the GPIO direction to output
2213 * @desc: GPIO to set to output
2214 * @value: initial output value of the GPIO
2215 *
2216 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2217 * be called safely on it. The initial value of the output must be specified
2218 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2219 *
2220 * Return 0 in case of success, else an error code.
2221 */
2222int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2223{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002224 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002225 return _gpiod_direction_output_raw(desc, value);
2226}
2227EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2228
2229/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05302230 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002231 * @desc: GPIO to set to output
2232 * @value: initial output value of the GPIO
2233 *
2234 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2235 * be called safely on it. The initial value of the output must be specified
2236 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2237 * account.
2238 *
2239 * Return 0 in case of success, else an error code.
2240 */
2241int gpiod_direction_output(struct gpio_desc *desc, int value)
2242{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002243 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002244 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2245 value = !value;
Linus Walleijad177312016-11-13 23:02:44 +01002246 else
2247 value = !!value;
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002248 return _gpiod_direction_output_raw(desc, value);
2249}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002250EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08002251
Felipe Balbic4b5be92010-05-26 14:42:23 -07002252/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002253 * gpiod_set_debounce - sets @debounce time for a @gpio
Felipe Balbic4b5be92010-05-26 14:42:23 -07002254 * @gpio: the gpio to set debounce time
2255 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02002256 *
2257 * returns -ENOTSUPP if the controller does not support setting
2258 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07002259 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002260int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07002261{
Felipe Balbic4b5be92010-05-26 14:42:23 -07002262 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07002263
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002264 VALIDATE_DESC(desc);
2265 chip = desc->gdev->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002266 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01002267 gpiod_dbg(desc,
2268 "%s: missing set() or set_debounce() operations\n",
2269 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02002270 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002271 }
2272
Alexandre Courbotd82da792014-07-22 16:17:43 +09002273 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07002274}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002275EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002276
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002277/**
2278 * gpiod_is_active_low - test whether a GPIO is active-low or not
2279 * @desc: the gpio descriptor to test
2280 *
2281 * Returns 1 if the GPIO is active-low, 0 otherwise.
2282 */
2283int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002284{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002285 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002286 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002287}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002288EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08002289
2290/* I/O calls are only valid after configuration completed; the relevant
2291 * "is this a valid GPIO" error checks should already have been done.
2292 *
2293 * "Get" operations are often inlinable as reading a pin value register,
2294 * and masking the relevant bit in that register.
2295 *
2296 * When "set" operations are inlinable, they involve writing that mask to
2297 * one register to set a low value, or a different register to set it high.
2298 * Otherwise locking is needed, so there may be little value to inlining.
2299 *
2300 *------------------------------------------------------------------------
2301 *
2302 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2303 * have requested the GPIO. That can include implicit requesting by
2304 * a direction setting call. Marking a gpio as requested locks its chip
2305 * in memory, guaranteeing that these table lookups need no more locking
2306 * and that gpiochip_remove() will fail.
2307 *
2308 * REVISIT when debugging, consider adding some instrumentation to ensure
2309 * that the GPIO was actually requested.
2310 */
2311
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002312static int _gpiod_get_raw_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002313{
2314 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002315 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002316 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002317
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002318 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002319 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002320 value = chip->get ? chip->get(chip, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01002321 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002322 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002323 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002324}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002325
David Brownelld2876d02008-02-04 22:28:20 -08002326/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002327 * gpiod_get_raw_value() - return a gpio's raw value
2328 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08002329 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002330 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002331 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002332 *
2333 * This function should be called from contexts where we cannot sleep, and will
2334 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002335 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002336int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002337{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002338 VALIDATE_DESC(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002339 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002340 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002341 return _gpiod_get_raw_value(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002342}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002343EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002344
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002345/**
2346 * gpiod_get_value() - return a gpio's value
2347 * @desc: gpio whose value will be returned
2348 *
2349 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002350 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002351 *
2352 * This function should be called from contexts where we cannot sleep, and will
2353 * complain if the GPIO chip functions potentially sleep.
2354 */
2355int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002356{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002357 int value;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002358
2359 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002360 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002361 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002362
2363 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002364 if (value < 0)
2365 return value;
2366
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002367 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2368 value = !value;
2369
2370 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002371}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002372EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08002373
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302374/*
2375 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002376 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002377 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302378 */
Alexandre Courbot23600962014-03-11 15:52:09 +09002379static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302380{
2381 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002382 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002383 int offset = gpio_chip_hwgpio(desc);
2384
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302385 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002386 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302387 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002388 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302389 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002390 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302391 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002392 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302393 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002394 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302395 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002396 gpiod_err(desc,
2397 "%s: Error in set_value for open drain err %d\n",
2398 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302399}
2400
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302401/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002402 * _gpio_set_open_source_value() - Set the open source gpio's value.
2403 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002404 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302405 */
Alexandre Courbot23600962014-03-11 15:52:09 +09002406static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302407{
2408 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002409 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002410 int offset = gpio_chip_hwgpio(desc);
2411
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302412 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002413 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302414 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002415 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302416 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002417 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302418 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002419 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302420 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002421 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302422 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002423 gpiod_err(desc,
2424 "%s: Error in set_value for open source err %d\n",
2425 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302426}
2427
Alexandre Courbot23600962014-03-11 15:52:09 +09002428static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08002429{
2430 struct gpio_chip *chip;
2431
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002432 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002433 trace_gpio_value(desc_to_gpio(desc), 0, value);
2434 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2435 _gpio_set_open_drain_value(desc, value);
2436 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2437 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302438 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09002439 chip->set(chip, gpio_chip_hwgpio(desc), value);
2440}
2441
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002442/*
2443 * set multiple outputs on the same chip;
2444 * use the chip's set_multiple function if available;
2445 * otherwise set the outputs sequentially;
2446 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
2447 * defines which outputs are to be changed
2448 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
2449 * defines the values the outputs specified by mask are to be set to
2450 */
2451static void gpio_chip_set_multiple(struct gpio_chip *chip,
2452 unsigned long *mask, unsigned long *bits)
2453{
2454 if (chip->set_multiple) {
2455 chip->set_multiple(chip, mask, bits);
2456 } else {
2457 int i;
2458 for (i = 0; i < chip->ngpio; i++) {
2459 if (mask[BIT_WORD(i)] == 0) {
2460 /* no more set bits in this mask word;
2461 * skip ahead to the next word */
2462 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
2463 continue;
2464 }
2465 /* set outputs if the corresponding mask bit is set */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002466 if (__test_and_clear_bit(i, mask))
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002467 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002468 }
2469 }
2470}
2471
Linus Walleij44c72882016-04-24 11:36:59 +02002472void gpiod_set_array_value_complex(bool raw, bool can_sleep,
2473 unsigned int array_size,
2474 struct gpio_desc **desc_array,
2475 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002476{
2477 int i = 0;
2478
2479 while (i < array_size) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002480 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002481 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2482 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2483 int count = 0;
2484
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002485 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002486 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002487
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002488 memset(mask, 0, sizeof(mask));
2489 do {
2490 struct gpio_desc *desc = desc_array[i];
2491 int hwgpio = gpio_chip_hwgpio(desc);
2492 int value = value_array[i];
2493
2494 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2495 value = !value;
2496 trace_gpio_value(desc_to_gpio(desc), 0, value);
2497 /*
2498 * collect all normal outputs belonging to the same chip
2499 * open drain and open source outputs are set individually
2500 */
2501 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002502 _gpio_set_open_drain_value(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002503 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2504 _gpio_set_open_source_value(desc, value);
2505 } else {
2506 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002507 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002508 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002509 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002510 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002511 count++;
2512 }
2513 i++;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002514 } while ((i < array_size) &&
2515 (desc_array[i]->gdev->chip == chip));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002516 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002517 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002518 gpio_chip_set_multiple(chip, mask, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002519 }
2520}
2521
David Brownelld2876d02008-02-04 22:28:20 -08002522/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002523 * gpiod_set_raw_value() - assign a gpio's raw value
2524 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08002525 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002526 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002527 * Set the raw value of the GPIO, i.e. the value of its physical line without
2528 * regard for its ACTIVE_LOW status.
2529 *
2530 * This function should be called from contexts where we cannot sleep, and will
2531 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002532 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002533void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002534{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002535 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01002536 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002537 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002538 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002539}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002540EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002541
2542/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002543 * gpiod_set_value() - assign a gpio's value
2544 * @desc: gpio whose value will be assigned
2545 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002546 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002547 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2548 * account
2549 *
2550 * This function should be called from contexts where we cannot sleep, and will
2551 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002552 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002553void gpiod_set_value(struct gpio_desc *desc, int value)
2554{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002555 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01002556 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002557 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002558 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2559 value = !value;
2560 _gpiod_set_raw_value(desc, value);
2561}
2562EXPORT_SYMBOL_GPL(gpiod_set_value);
2563
2564/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002565 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002566 * @array_size: number of elements in the descriptor / value arrays
2567 * @desc_array: array of GPIO descriptors whose values will be assigned
2568 * @value_array: array of values to assign
2569 *
2570 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2571 * without regard for their ACTIVE_LOW status.
2572 *
2573 * This function should be called from contexts where we cannot sleep, and will
2574 * complain if the GPIO chip functions potentially sleep.
2575 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002576void gpiod_set_raw_array_value(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002577 struct gpio_desc **desc_array, int *value_array)
2578{
2579 if (!desc_array)
2580 return;
Linus Walleij44c72882016-04-24 11:36:59 +02002581 gpiod_set_array_value_complex(true, false, array_size, desc_array,
2582 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002583}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002584EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002585
2586/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002587 * gpiod_set_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002588 * @array_size: number of elements in the descriptor / value arrays
2589 * @desc_array: array of GPIO descriptors whose values will be assigned
2590 * @value_array: array of values to assign
2591 *
2592 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2593 * into account.
2594 *
2595 * This function should be called from contexts where we cannot sleep, and will
2596 * complain if the GPIO chip functions potentially sleep.
2597 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002598void gpiod_set_array_value(unsigned int array_size,
2599 struct gpio_desc **desc_array, int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002600{
2601 if (!desc_array)
2602 return;
Linus Walleij44c72882016-04-24 11:36:59 +02002603 gpiod_set_array_value_complex(false, false, array_size, desc_array,
2604 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002605}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002606EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002607
2608/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002609 * gpiod_cansleep() - report whether gpio value access may sleep
2610 * @desc: gpio to check
2611 *
2612 */
2613int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002614{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002615 VALIDATE_DESC(desc);
2616 return desc->gdev->chip->can_sleep;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002617}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002618EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002619
David Brownell0f6d5042008-10-15 22:03:14 -07002620/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002621 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
2622 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07002623 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002624 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
2625 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07002626 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002627int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07002628{
Linus Walleij4c37ce82016-05-02 13:13:10 +02002629 struct gpio_chip *chip;
2630 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07002631
Linus Walleij79bb71b2016-06-15 22:57:38 +02002632 /*
2633 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
2634 * requires this function to not return zero on an invalid descriptor
2635 * but rather a negative error number.
2636 */
Linus Walleijbfbbe442016-06-16 11:55:55 +02002637 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
Linus Walleij79bb71b2016-06-15 22:57:38 +02002638 return -EINVAL;
2639
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002640 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002641 offset = gpio_chip_hwgpio(desc);
Linus Walleij4c37ce82016-05-02 13:13:10 +02002642 if (chip->to_irq) {
2643 int retirq = chip->to_irq(chip, offset);
2644
2645 /* Zero means NO_IRQ */
2646 if (!retirq)
2647 return -ENXIO;
2648
2649 return retirq;
2650 }
2651 return -ENXIO;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002652}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002653EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002654
Linus Walleijd468bf92013-09-24 11:54:38 +02002655/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002656 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002657 * @chip: the chip the GPIO to lock belongs to
2658 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02002659 *
2660 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08002661 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08002662 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002663int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08002664{
Linus Walleij9c102802016-05-25 10:56:03 +02002665 struct gpio_desc *desc;
Linus Walleijd468bf92013-09-24 11:54:38 +02002666
Linus Walleij9c102802016-05-25 10:56:03 +02002667 desc = gpiochip_get_desc(chip, offset);
2668 if (IS_ERR(desc))
2669 return PTR_ERR(desc);
2670
2671 /* Flush direction if something changed behind our back */
2672 if (chip->get_direction) {
2673 int dir = chip->get_direction(chip, offset);
2674
2675 if (dir)
2676 clear_bit(FLAG_IS_OUT, &desc->flags);
2677 else
2678 set_bit(FLAG_IS_OUT, &desc->flags);
2679 }
2680
2681 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002682 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02002683 "%s: tried to flag a GPIO set as output for IRQ\n",
2684 __func__);
2685 return -EIO;
2686 }
2687
Linus Walleij9c102802016-05-25 10:56:03 +02002688 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
Linus Walleij3940c342016-11-14 00:09:07 +01002689
2690 /*
2691 * If the consumer has not set up a label (such as when the
2692 * IRQ is referenced from .to_irq()) we set up a label here
2693 * so it is clear this is used as an interrupt.
2694 */
2695 if (!desc->label)
2696 desc_set_label(desc, "interrupt");
2697
Linus Walleijd468bf92013-09-24 11:54:38 +02002698 return 0;
2699}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002700EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02002701
Linus Walleijd468bf92013-09-24 11:54:38 +02002702/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002703 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002704 * @chip: the chip the GPIO to lock belongs to
2705 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02002706 *
2707 * This is used directly by GPIO drivers that want to indicate
2708 * that a certain GPIO is no longer used exclusively for IRQ.
2709 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002710void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02002711{
Linus Walleij3940c342016-11-14 00:09:07 +01002712 struct gpio_desc *desc;
2713
2714 desc = gpiochip_get_desc(chip, offset);
2715 if (IS_ERR(desc))
Linus Walleijd468bf92013-09-24 11:54:38 +02002716 return;
2717
Linus Walleij3940c342016-11-14 00:09:07 +01002718 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
2719
2720 /* If we only had this marking, erase it */
2721 if (desc->label && !strcmp(desc->label, "interrupt"))
2722 desc_set_label(desc, NULL);
Linus Walleijd468bf92013-09-24 11:54:38 +02002723}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002724EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02002725
Linus Walleij6cee3822016-02-11 20:16:45 +01002726bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
2727{
2728 if (offset >= chip->ngpio)
2729 return false;
2730
2731 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
2732}
2733EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
2734
Linus Walleij143b65d2016-02-16 15:41:42 +01002735bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
2736{
2737 if (offset >= chip->ngpio)
2738 return false;
2739
2740 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
2741}
2742EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
2743
2744bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
2745{
2746 if (offset >= chip->ngpio)
2747 return false;
2748
2749 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
2750}
2751EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
2752
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002753/**
2754 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
2755 * @desc: gpio whose value will be returned
2756 *
2757 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002758 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002759 *
2760 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002761 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002762int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002763{
David Brownelld2876d02008-02-04 22:28:20 -08002764 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002765 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002766 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002767}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002768EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002769
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002770/**
2771 * gpiod_get_value_cansleep() - return a gpio's value
2772 * @desc: gpio whose value will be returned
2773 *
2774 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002775 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002776 *
2777 * This function is to be called from contexts that can sleep.
2778 */
2779int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002780{
David Brownelld2876d02008-02-04 22:28:20 -08002781 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002782
2783 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002784 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002785 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002786 if (value < 0)
2787 return value;
2788
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002789 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2790 value = !value;
2791
David Brownelld2876d02008-02-04 22:28:20 -08002792 return value;
2793}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002794EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002795
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002796/**
2797 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
2798 * @desc: gpio whose value will be assigned
2799 * @value: value to assign
2800 *
2801 * Set the raw value of the GPIO, i.e. the value of its physical line without
2802 * regard for its ACTIVE_LOW status.
2803 *
2804 * This function is to be called from contexts that can sleep.
2805 */
2806void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002807{
David Brownelld2876d02008-02-04 22:28:20 -08002808 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002809 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002810 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002811}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002812EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002813
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002814/**
2815 * gpiod_set_value_cansleep() - assign a gpio's value
2816 * @desc: gpio whose value will be assigned
2817 * @value: value to assign
2818 *
2819 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2820 * account
2821 *
2822 * This function is to be called from contexts that can sleep.
2823 */
2824void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002825{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002826 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002827 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002828 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2829 value = !value;
2830 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002831}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002832EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002833
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002834/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002835 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002836 * @array_size: number of elements in the descriptor / value arrays
2837 * @desc_array: array of GPIO descriptors whose values will be assigned
2838 * @value_array: array of values to assign
2839 *
2840 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2841 * without regard for their ACTIVE_LOW status.
2842 *
2843 * This function is to be called from contexts that can sleep.
2844 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002845void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
2846 struct gpio_desc **desc_array,
2847 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002848{
2849 might_sleep_if(extra_checks);
2850 if (!desc_array)
2851 return;
Linus Walleij44c72882016-04-24 11:36:59 +02002852 gpiod_set_array_value_complex(true, true, array_size, desc_array,
2853 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002854}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002855EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002856
2857/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002858 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002859 * @array_size: number of elements in the descriptor / value arrays
2860 * @desc_array: array of GPIO descriptors whose values will be assigned
2861 * @value_array: array of values to assign
2862 *
2863 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2864 * into account.
2865 *
2866 * This function is to be called from contexts that can sleep.
2867 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002868void gpiod_set_array_value_cansleep(unsigned int array_size,
2869 struct gpio_desc **desc_array,
2870 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002871{
2872 might_sleep_if(extra_checks);
2873 if (!desc_array)
2874 return;
Linus Walleij44c72882016-04-24 11:36:59 +02002875 gpiod_set_array_value_complex(false, true, array_size, desc_array,
2876 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002877}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002878EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002879
2880/**
Alexandre Courbotad824782013-12-03 12:20:11 +09002881 * gpiod_add_lookup_table() - register GPIO device consumers
2882 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002883 */
Alexandre Courbotad824782013-12-03 12:20:11 +09002884void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002885{
2886 mutex_lock(&gpio_lookup_lock);
2887
Alexandre Courbotad824782013-12-03 12:20:11 +09002888 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002889
2890 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08002891}
2892
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05302893/**
2894 * gpiod_remove_lookup_table() - unregister GPIO device consumers
2895 * @table: table of consumers to unregister
2896 */
2897void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
2898{
2899 mutex_lock(&gpio_lookup_lock);
2900
2901 list_del(&table->list);
2902
2903 mutex_unlock(&gpio_lookup_lock);
2904}
2905
Alexandre Courbotad824782013-12-03 12:20:11 +09002906static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
2907{
2908 const char *dev_id = dev ? dev_name(dev) : NULL;
2909 struct gpiod_lookup_table *table;
2910
2911 mutex_lock(&gpio_lookup_lock);
2912
2913 list_for_each_entry(table, &gpio_lookup_list, list) {
2914 if (table->dev_id && dev_id) {
2915 /*
2916 * Valid strings on both ends, must be identical to have
2917 * a match
2918 */
2919 if (!strcmp(table->dev_id, dev_id))
2920 goto found;
2921 } else {
2922 /*
2923 * One of the pointers is NULL, so both must be to have
2924 * a match
2925 */
2926 if (dev_id == table->dev_id)
2927 goto found;
2928 }
2929 }
2930 table = NULL;
2931
2932found:
2933 mutex_unlock(&gpio_lookup_lock);
2934 return table;
2935}
2936
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002937static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002938 unsigned int idx,
2939 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002940{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002941 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09002942 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002943 struct gpiod_lookup *p;
2944
Alexandre Courbotad824782013-12-03 12:20:11 +09002945 table = gpiod_find_lookup_table(dev);
2946 if (!table)
2947 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002948
Alexandre Courbotad824782013-12-03 12:20:11 +09002949 for (p = &table->table[0]; p->chip_label; p++) {
2950 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002951
Alexandre Courbotad824782013-12-03 12:20:11 +09002952 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002953 if (p->idx != idx)
2954 continue;
2955
Alexandre Courbotad824782013-12-03 12:20:11 +09002956 /* If the lookup entry has a con_id, require exact match */
2957 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
2958 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002959
Alexandre Courbotad824782013-12-03 12:20:11 +09002960 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002961
Alexandre Courbotad824782013-12-03 12:20:11 +09002962 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002963 dev_err(dev, "cannot find GPIO chip %s\n",
2964 p->chip_label);
2965 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002966 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002967
Alexandre Courbotad824782013-12-03 12:20:11 +09002968 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002969 dev_err(dev,
2970 "requested GPIO %d is out of range [0..%d] for chip %s\n",
2971 idx, chip->ngpio, chip->label);
2972 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09002973 }
2974
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09002975 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09002976 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002977
2978 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09002979 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002980
2981 return desc;
2982}
2983
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002984static int dt_gpio_count(struct device *dev, const char *con_id)
2985{
2986 int ret;
2987 char propname[32];
2988 unsigned int i;
2989
2990 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
2991 if (con_id)
2992 snprintf(propname, sizeof(propname), "%s-%s",
2993 con_id, gpio_suffixes[i]);
2994 else
2995 snprintf(propname, sizeof(propname), "%s",
2996 gpio_suffixes[i]);
2997
2998 ret = of_gpio_named_count(dev->of_node, propname);
2999 if (ret >= 0)
3000 break;
3001 }
3002 return ret;
3003}
3004
3005static int platform_gpio_count(struct device *dev, const char *con_id)
3006{
3007 struct gpiod_lookup_table *table;
3008 struct gpiod_lookup *p;
3009 unsigned int count = 0;
3010
3011 table = gpiod_find_lookup_table(dev);
3012 if (!table)
3013 return -ENOENT;
3014
3015 for (p = &table->table[0]; p->chip_label; p++) {
3016 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3017 (!con_id && !p->con_id))
3018 count++;
3019 }
3020 if (!count)
3021 return -ENOENT;
3022
3023 return count;
3024}
3025
3026/**
3027 * gpiod_count - return the number of GPIOs associated with a device / function
3028 * or -ENOENT if no GPIO has been assigned to the requested function
3029 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3030 * @con_id: function within the GPIO consumer
3031 */
3032int gpiod_count(struct device *dev, const char *con_id)
3033{
3034 int count = -ENOENT;
3035
3036 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
3037 count = dt_gpio_count(dev, con_id);
3038 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
3039 count = acpi_gpio_count(dev, con_id);
3040
3041 if (count < 0)
3042 count = platform_gpio_count(dev, con_id);
3043
3044 return count;
3045}
3046EXPORT_SYMBOL_GPL(gpiod_count);
3047
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003048/**
Thierry Reding08791622014-04-25 16:54:22 +02003049 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09003050 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003051 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003052 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003053 *
3054 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003055 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07003056 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003057 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003058struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003059 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003060{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003061 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003062}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003063EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003064
3065/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02003066 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
3067 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3068 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003069 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02003070 *
3071 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
3072 * the requested function it will return NULL. This is convenient for drivers
3073 * that need to handle optional GPIOs.
3074 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003075struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003076 const char *con_id,
3077 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003078{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003079 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003080}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003081EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003082
Benoit Parrotf625d462015-02-02 11:44:44 -06003083
3084/**
3085 * gpiod_configure_flags - helper function to configure a given GPIO
3086 * @desc: gpio whose value will be assigned
3087 * @con_id: function within the GPIO consumer
Johan Hovold85b03b32016-07-03 18:32:05 +02003088 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3089 * of_get_gpio_hog()
Benoit Parrotf625d462015-02-02 11:44:44 -06003090 * @dflags: gpiod_flags - optional GPIO initialization flags
3091 *
3092 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
3093 * requested function and/or index, or another IS_ERR() code if an error
3094 * occurred while trying to acquire the GPIO.
3095 */
3096static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Johan Hovold85b03b32016-07-03 18:32:05 +02003097 unsigned long lflags, enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06003098{
3099 int status;
3100
Johan Hovold85b03b32016-07-03 18:32:05 +02003101 if (lflags & GPIO_ACTIVE_LOW)
3102 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
3103 if (lflags & GPIO_OPEN_DRAIN)
3104 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3105 if (lflags & GPIO_OPEN_SOURCE)
3106 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
3107
Benoit Parrotf625d462015-02-02 11:44:44 -06003108 /* No particular flag request, return here... */
3109 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
3110 pr_debug("no flags found for %s\n", con_id);
3111 return 0;
3112 }
3113
3114 /* Process flags */
3115 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
3116 status = gpiod_direction_output(desc,
Linus Walleijad177312016-11-13 23:02:44 +01003117 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
Benoit Parrotf625d462015-02-02 11:44:44 -06003118 else
3119 status = gpiod_direction_input(desc);
3120
3121 return status;
3122}
3123
Thierry Reding29a1f2332014-04-25 17:10:06 +02003124/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003125 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02003126 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003127 * @con_id: function within the GPIO consumer
3128 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003129 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003130 *
3131 * This variant of gpiod_get() allows to access GPIOs other than the first
3132 * defined one for functions that define several GPIOs.
3133 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003134 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
3135 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07003136 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003137 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003138struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003139 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003140 unsigned int idx,
3141 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003142{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003143 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003144 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003145 enum gpio_lookup_flags lookupflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003146
3147 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
3148
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003149 if (dev) {
3150 /* Using device tree? */
3151 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
3152 dev_dbg(dev, "using device tree for GPIO lookup\n");
3153 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
3154 } else if (ACPI_COMPANION(dev)) {
3155 dev_dbg(dev, "using ACPI for GPIO lookup\n");
Dmitry Torokhov25487532016-03-24 10:50:25 -07003156 desc = acpi_find_gpio(dev, con_id, idx, flags, &lookupflags);
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003157 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003158 }
3159
3160 /*
3161 * Either we are not using DT or ACPI, or their lookup did not return
3162 * a result. In that case, use platform lookup as a fallback.
3163 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003164 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04003165 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003166 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003167 }
3168
3169 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02003170 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003171 return desc;
3172 }
3173
3174 status = gpiod_request(desc, con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003175 if (status < 0)
3176 return ERR_PTR(status);
3177
Johan Hovold85b03b32016-07-03 18:32:05 +02003178 status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003179 if (status < 0) {
3180 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
3181 gpiod_put(desc);
3182 return ERR_PTR(status);
3183 }
3184
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003185 return desc;
3186}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003187EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003188
3189/**
Mika Westerberg40b73182014-10-21 13:33:59 +02003190 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
3191 * @fwnode: handle of the firmware node
3192 * @propname: name of the firmware property representing the GPIO
3193 *
3194 * This function can be used for drivers that get their configuration
3195 * from firmware.
3196 *
3197 * Function properly finds the corresponding GPIO using whatever is the
3198 * underlying firmware interface and then makes sure that the GPIO
3199 * descriptor is requested before it is returned to the caller.
3200 *
3201 * In case of error an ERR_PTR() is returned.
3202 */
3203struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
3204 const char *propname)
3205{
3206 struct gpio_desc *desc = ERR_PTR(-ENODEV);
3207 bool active_low = false;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003208 bool single_ended = false;
Mika Westerberg40b73182014-10-21 13:33:59 +02003209 int ret;
3210
3211 if (!fwnode)
3212 return ERR_PTR(-EINVAL);
3213
3214 if (is_of_node(fwnode)) {
3215 enum of_gpio_flags flags;
3216
Alexander Sverdlinc181fb32015-06-22 22:38:53 +02003217 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, 0,
Mika Westerberg40b73182014-10-21 13:33:59 +02003218 &flags);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003219 if (!IS_ERR(desc)) {
Mika Westerberg40b73182014-10-21 13:33:59 +02003220 active_low = flags & OF_GPIO_ACTIVE_LOW;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003221 single_ended = flags & OF_GPIO_SINGLE_ENDED;
3222 }
Mika Westerberg40b73182014-10-21 13:33:59 +02003223 } else if (is_acpi_node(fwnode)) {
3224 struct acpi_gpio_info info;
3225
Rafael J. Wysocki504a3372015-08-27 04:42:33 +02003226 desc = acpi_node_get_gpiod(fwnode, propname, 0, &info);
Mika Westerberg40b73182014-10-21 13:33:59 +02003227 if (!IS_ERR(desc))
Christophe RICARD52044722015-12-23 23:25:34 +01003228 active_low = info.polarity == GPIO_ACTIVE_LOW;
Mika Westerberg40b73182014-10-21 13:33:59 +02003229 }
3230
3231 if (IS_ERR(desc))
3232 return desc;
3233
Johan Hovold85b03b32016-07-03 18:32:05 +02003234 ret = gpiod_request(desc, NULL);
3235 if (ret)
3236 return ERR_PTR(ret);
3237
Mika Westerberg40b73182014-10-21 13:33:59 +02003238 if (active_low)
3239 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
3240
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003241 if (single_ended) {
3242 if (active_low)
3243 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3244 else
3245 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
3246 }
3247
Mika Westerberg40b73182014-10-21 13:33:59 +02003248 return desc;
3249}
3250EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
3251
3252/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02003253 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
3254 * function
3255 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3256 * @con_id: function within the GPIO consumer
3257 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003258 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02003259 *
3260 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
3261 * specified index was assigned to the requested function it will return NULL.
3262 * This is convenient for drivers that need to handle optional GPIOs.
3263 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003264struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02003265 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003266 unsigned int index,
3267 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003268{
3269 struct gpio_desc *desc;
3270
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003271 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003272 if (IS_ERR(desc)) {
3273 if (PTR_ERR(desc) == -ENOENT)
3274 return NULL;
3275 }
3276
3277 return desc;
3278}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003279EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003280
3281/**
Benoit Parrotf625d462015-02-02 11:44:44 -06003282 * gpiod_hog - Hog the specified GPIO desc given the provided flags
3283 * @desc: gpio whose value will be assigned
3284 * @name: gpio line name
3285 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3286 * of_get_gpio_hog()
3287 * @dflags: gpiod_flags - optional GPIO initialization flags
3288 */
3289int gpiod_hog(struct gpio_desc *desc, const char *name,
3290 unsigned long lflags, enum gpiod_flags dflags)
3291{
3292 struct gpio_chip *chip;
3293 struct gpio_desc *local_desc;
3294 int hwnum;
3295 int status;
3296
3297 chip = gpiod_to_chip(desc);
3298 hwnum = gpio_chip_hwgpio(desc);
3299
3300 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
3301 if (IS_ERR(local_desc)) {
Laxman Dewanganc31a5712016-03-11 19:13:21 +05303302 status = PTR_ERR(local_desc);
3303 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
3304 name, chip->label, hwnum, status);
3305 return status;
Benoit Parrotf625d462015-02-02 11:44:44 -06003306 }
3307
Johan Hovold85b03b32016-07-03 18:32:05 +02003308 status = gpiod_configure_flags(desc, name, lflags, dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -06003309 if (status < 0) {
Laxman Dewanganc31a5712016-03-11 19:13:21 +05303310 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed, %d\n",
3311 name, chip->label, hwnum, status);
Benoit Parrotf625d462015-02-02 11:44:44 -06003312 gpiochip_free_own_desc(desc);
3313 return status;
3314 }
3315
3316 /* Mark GPIO as hogged so it can be identified and removed later */
3317 set_bit(FLAG_IS_HOGGED, &desc->flags);
3318
3319 pr_info("GPIO line %d (%s) hogged as %s%s\n",
3320 desc_to_gpio(desc), name,
3321 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
3322 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
3323 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
3324
3325 return 0;
3326}
3327
3328/**
3329 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
3330 * @chip: gpio chip to act on
3331 *
3332 * This is only used by of_gpiochip_remove to free hogged gpios
3333 */
3334static void gpiochip_free_hogs(struct gpio_chip *chip)
3335{
3336 int id;
3337
3338 for (id = 0; id < chip->ngpio; id++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01003339 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
3340 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
Benoit Parrotf625d462015-02-02 11:44:44 -06003341 }
3342}
3343
3344/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003345 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
3346 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3347 * @con_id: function within the GPIO consumer
3348 * @flags: optional GPIO initialization flags
3349 *
3350 * This function acquires all the GPIOs defined under a given function.
3351 *
3352 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
3353 * no GPIO has been assigned to the requested function, or another IS_ERR()
3354 * code if an error occurred while trying to acquire the GPIOs.
3355 */
3356struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
3357 const char *con_id,
3358 enum gpiod_flags flags)
3359{
3360 struct gpio_desc *desc;
3361 struct gpio_descs *descs;
3362 int count;
3363
3364 count = gpiod_count(dev, con_id);
3365 if (count < 0)
3366 return ERR_PTR(count);
3367
3368 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
3369 GFP_KERNEL);
3370 if (!descs)
3371 return ERR_PTR(-ENOMEM);
3372
3373 for (descs->ndescs = 0; descs->ndescs < count; ) {
3374 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
3375 if (IS_ERR(desc)) {
3376 gpiod_put_array(descs);
3377 return ERR_CAST(desc);
3378 }
3379 descs->desc[descs->ndescs] = desc;
3380 descs->ndescs++;
3381 }
3382 return descs;
3383}
3384EXPORT_SYMBOL_GPL(gpiod_get_array);
3385
3386/**
3387 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
3388 * function
3389 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3390 * @con_id: function within the GPIO consumer
3391 * @flags: optional GPIO initialization flags
3392 *
3393 * This is equivalent to gpiod_get_array(), except that when no GPIO was
3394 * assigned to the requested function it will return NULL.
3395 */
3396struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
3397 const char *con_id,
3398 enum gpiod_flags flags)
3399{
3400 struct gpio_descs *descs;
3401
3402 descs = gpiod_get_array(dev, con_id, flags);
3403 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
3404 return NULL;
3405
3406 return descs;
3407}
3408EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
3409
3410/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003411 * gpiod_put - dispose of a GPIO descriptor
3412 * @desc: GPIO descriptor to dispose of
3413 *
3414 * No descriptor can be used after gpiod_put() has been called on it.
3415 */
3416void gpiod_put(struct gpio_desc *desc)
3417{
3418 gpiod_free(desc);
3419}
3420EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08003421
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003422/**
3423 * gpiod_put_array - dispose of multiple GPIO descriptors
3424 * @descs: struct gpio_descs containing an array of descriptors
3425 */
3426void gpiod_put_array(struct gpio_descs *descs)
3427{
3428 unsigned int i;
3429
3430 for (i = 0; i < descs->ndescs; i++)
3431 gpiod_put(descs->desc[i]);
3432
3433 kfree(descs);
3434}
3435EXPORT_SYMBOL_GPL(gpiod_put_array);
3436
Linus Walleij3c702e92015-10-21 15:29:53 +02003437static int __init gpiolib_dev_init(void)
3438{
3439 int ret;
3440
3441 /* Register GPIO sysfs bus */
3442 ret = bus_register(&gpio_bus_type);
3443 if (ret < 0) {
3444 pr_err("gpiolib: could not register GPIO bus type\n");
3445 return ret;
3446 }
3447
3448 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
3449 if (ret < 0) {
3450 pr_err("gpiolib: failed to allocate char dev region\n");
3451 bus_unregister(&gpio_bus_type);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07003452 } else {
3453 gpiolib_initialized = true;
3454 gpiochip_setup_devs();
Linus Walleij3c702e92015-10-21 15:29:53 +02003455 }
3456 return ret;
3457}
3458core_initcall(gpiolib_dev_init);
3459
David Brownelld2876d02008-02-04 22:28:20 -08003460#ifdef CONFIG_DEBUG_FS
3461
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003462static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
David Brownelld2876d02008-02-04 22:28:20 -08003463{
3464 unsigned i;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003465 struct gpio_chip *chip = gdev->chip;
3466 unsigned gpio = gdev->base;
3467 struct gpio_desc *gdesc = &gdev->descs[0];
David Brownelld2876d02008-02-04 22:28:20 -08003468 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02003469 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08003470
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003471 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02003472 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
3473 if (gdesc->name) {
3474 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
3475 gpio, gdesc->name);
3476 }
David Brownelld2876d02008-02-04 22:28:20 -08003477 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02003478 }
David Brownelld2876d02008-02-04 22:28:20 -08003479
Alexandre Courbot372e7222013-02-03 01:29:29 +09003480 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08003481 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02003482 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Markus Pargmannced433e2015-08-14 16:11:02 +02003483 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
3484 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08003485 is_out ? "out" : "in ",
3486 chip->get
3487 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02003488 : "? ",
3489 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08003490 seq_printf(s, "\n");
3491 }
3492}
3493
Thierry Redingf9c4a312012-04-12 13:26:01 +02003494static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08003495{
Grant Likely362432a2013-02-09 09:41:49 +00003496 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02003497 struct gpio_device *gdev = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003498 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003499
3500 s->private = "";
3501
Grant Likely362432a2013-02-09 09:41:49 +00003502 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003503 list_for_each_entry(gdev, &gpio_devices, list)
Grant Likely362432a2013-02-09 09:41:49 +00003504 if (index-- == 0) {
3505 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003506 return gdev;
Grant Likely362432a2013-02-09 09:41:49 +00003507 }
3508 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003509
3510 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003511}
3512
3513static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
3514{
Grant Likely362432a2013-02-09 09:41:49 +00003515 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02003516 struct gpio_device *gdev = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003517 void *ret = NULL;
3518
Grant Likely362432a2013-02-09 09:41:49 +00003519 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003520 if (list_is_last(&gdev->list, &gpio_devices))
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003521 ret = NULL;
3522 else
Linus Walleijff2b1352015-10-20 11:10:38 +02003523 ret = list_entry(gdev->list.next, struct gpio_device, list);
Grant Likely362432a2013-02-09 09:41:49 +00003524 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02003525
3526 s->private = "\n";
3527 ++*pos;
3528
3529 return ret;
3530}
3531
3532static void gpiolib_seq_stop(struct seq_file *s, void *v)
3533{
3534}
3535
3536static int gpiolib_seq_show(struct seq_file *s, void *v)
3537{
Linus Walleijff2b1352015-10-20 11:10:38 +02003538 struct gpio_device *gdev = v;
3539 struct gpio_chip *chip = gdev->chip;
3540 struct device *parent;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003541
Linus Walleijff2b1352015-10-20 11:10:38 +02003542 if (!chip) {
3543 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
3544 dev_name(&gdev->dev));
3545 return 0;
3546 }
3547
3548 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
3549 dev_name(&gdev->dev),
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003550 gdev->base, gdev->base + gdev->ngpio - 1);
Linus Walleijff2b1352015-10-20 11:10:38 +02003551 parent = chip->parent;
3552 if (parent)
3553 seq_printf(s, ", parent: %s/%s",
3554 parent->bus ? parent->bus->name : "no-bus",
3555 dev_name(parent));
Thierry Redingf9c4a312012-04-12 13:26:01 +02003556 if (chip->label)
3557 seq_printf(s, ", %s", chip->label);
3558 if (chip->can_sleep)
3559 seq_printf(s, ", can sleep");
3560 seq_printf(s, ":\n");
3561
3562 if (chip->dbg_show)
3563 chip->dbg_show(s, chip);
3564 else
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003565 gpiolib_dbg_show(s, gdev);
Thierry Redingf9c4a312012-04-12 13:26:01 +02003566
David Brownelld2876d02008-02-04 22:28:20 -08003567 return 0;
3568}
3569
Thierry Redingf9c4a312012-04-12 13:26:01 +02003570static const struct seq_operations gpiolib_seq_ops = {
3571 .start = gpiolib_seq_start,
3572 .next = gpiolib_seq_next,
3573 .stop = gpiolib_seq_stop,
3574 .show = gpiolib_seq_show,
3575};
3576
David Brownelld2876d02008-02-04 22:28:20 -08003577static int gpiolib_open(struct inode *inode, struct file *file)
3578{
Thierry Redingf9c4a312012-04-12 13:26:01 +02003579 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08003580}
3581
Alexey Dobriyan828c0952009-10-01 15:43:56 -07003582static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02003583 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08003584 .open = gpiolib_open,
3585 .read = seq_read,
3586 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02003587 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08003588};
3589
3590static int __init gpiolib_debugfs_init(void)
3591{
3592 /* /sys/kernel/debug/gpio */
3593 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
3594 NULL, NULL, &gpiolib_operations);
3595 return 0;
3596}
3597subsys_initcall(gpiolib_debugfs_init);
3598
3599#endif /* DEBUG_FS */