blob: 23be4daefeed7404ed6abe0cb6f80b426f165628 [file] [log] [blame]
David Brownelld2876d02008-02-04 22:28:20 -08001#include <linux/kernel.h>
2#include <linux/module.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -07003#include <linux/interrupt.h>
David Brownelld2876d02008-02-04 22:28:20 -08004#include <linux/irq.h>
5#include <linux/spinlock.h>
Alexandre Courbot1a989d02013-02-03 01:29:24 +09006#include <linux/list.h>
David Brownelld8f388d2008-07-25 01:46:07 -07007#include <linux/device.h>
8#include <linux/err.h>
9#include <linux/debugfs.h>
10#include <linux/seq_file.h>
11#include <linux/gpio.h>
Anton Vorontsov391c9702010-06-08 07:48:17 -060012#include <linux/of_gpio.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -070013#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Rafael J. Wysocki7b199812013-11-11 22:41:56 +010015#include <linux/acpi.h>
Alexandre Courbot53e7cac2013-11-16 21:44:52 +090016#include <linux/gpio/driver.h>
Linus Walleij0a6d3152014-07-24 20:08:55 +020017#include <linux/gpio/machine.h>
Jonas Gorskic771c2f2015-10-11 17:34:15 +020018#include <linux/pinctrl/consumer.h>
Linus Walleij3c702e92015-10-21 15:29:53 +020019#include <linux/cdev.h>
20#include <linux/fs.h>
21#include <linux/uaccess.h>
Linus Walleij8b92e172016-05-27 14:24:04 +020022#include <linux/compat.h>
Linus Walleijd7c51b42016-04-26 10:35:29 +020023#include <linux/anon_inodes.h>
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 Walleijf881bab2015-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 Walleijf881bab2015-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 Walleijf881bab2015-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 Walleijf881bab2015-09-23 16:20:43 -0700270
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100271 if (!desc->name || !name)
Linus Walleijf881bab2015-09-23 16:20:43 -0700272 continue;
273
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100274 if (!strcmp(desc->name, name)) {
Linus Walleijf881bab2015-09-23 16:20:43 -0700275 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100276 return desc;
Linus Walleijf881bab2015-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 Walleijf881bab2015-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 Walleijf881bab2015-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;
916 return 0;
917}
918
919/**
920 * gpio_chrdev_release() - close chardev after ioctl operations
921 * @inode: inode for this chardev
922 * @filp: file struct for storing private data
923 * Returns 0 on success
924 */
925static int gpio_chrdev_release(struct inode *inode, struct file *filp)
926{
927 struct gpio_device *gdev = container_of(inode->i_cdev,
928 struct gpio_device, chrdev);
929
930 if (!gdev)
931 return -ENODEV;
932 put_device(&gdev->dev);
933 return 0;
934}
935
936
937static const struct file_operations gpio_fileops = {
938 .release = gpio_chrdev_release,
939 .open = gpio_chrdev_open,
940 .owner = THIS_MODULE,
941 .llseek = noop_llseek,
942 .unlocked_ioctl = gpio_ioctl,
Linus Walleij8b92e172016-05-27 14:24:04 +0200943#ifdef CONFIG_COMPAT
944 .compat_ioctl = gpio_ioctl_compat,
945#endif
Linus Walleij3c702e92015-10-21 15:29:53 +0200946};
947
Linus Walleijff2b1352015-10-20 11:10:38 +0200948static void gpiodevice_release(struct device *dev)
949{
950 struct gpio_device *gdev = dev_get_drvdata(dev);
951
952 list_del(&gdev->list);
953 ida_simple_remove(&gpio_ida, gdev->id);
Guenter Roeck476e2fc2016-03-31 08:11:29 -0700954 kfree(gdev->label);
955 kfree(gdev->descs);
Linus Walleij9efd9e62016-02-09 14:27:42 +0100956 kfree(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +0200957}
958
Guenter Roeck159f3cd2016-03-31 08:11:30 -0700959static int gpiochip_setup_dev(struct gpio_device *gdev)
960{
961 int status;
962
963 cdev_init(&gdev->chrdev, &gpio_fileops);
964 gdev->chrdev.owner = THIS_MODULE;
965 gdev->chrdev.kobj.parent = &gdev->dev.kobj;
966 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
967 status = cdev_add(&gdev->chrdev, gdev->dev.devt, 1);
968 if (status < 0)
969 chip_warn(gdev->chip, "failed to add char device %d:%d\n",
970 MAJOR(gpio_devt), gdev->id);
971 else
972 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
973 MAJOR(gpio_devt), gdev->id);
974 status = device_add(&gdev->dev);
975 if (status)
976 goto err_remove_chardev;
977
978 status = gpiochip_sysfs_register(gdev);
979 if (status)
980 goto err_remove_device;
981
982 /* From this point, the .release() function cleans up gpio_device */
983 gdev->dev.release = gpiodevice_release;
Guenter Roeck159f3cd2016-03-31 08:11:30 -0700984 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
985 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
986 dev_name(&gdev->dev), gdev->chip->label ? : "generic");
987
988 return 0;
989
990err_remove_device:
991 device_del(&gdev->dev);
992err_remove_chardev:
993 cdev_del(&gdev->chrdev);
994 return status;
995}
996
997static void gpiochip_setup_devs(void)
998{
999 struct gpio_device *gdev;
1000 int err;
1001
1002 list_for_each_entry(gdev, &gpio_devices, list) {
1003 err = gpiochip_setup_dev(gdev);
1004 if (err)
1005 pr_err("%s: Failed to initialize gpio device (%d)\n",
1006 dev_name(&gdev->dev), err);
1007 }
1008}
1009
Anton Vorontsov169b6a72008-04-28 02:14:47 -07001010/**
Linus Walleijb08ea352015-12-03 15:14:13 +01001011 * gpiochip_add_data() - register a gpio_chip
David Brownelld2876d02008-02-04 22:28:20 -08001012 * @chip: the chip to register, with chip->base initialized
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001013 * Context: potentially before irqs will work
David Brownelld2876d02008-02-04 22:28:20 -08001014 *
1015 * Returns a negative errno if the chip can't be registered, such as
1016 * because the chip->base is invalid or already associated with a
1017 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001018 *
Linus Walleijb08ea352015-12-03 15:14:13 +01001019 * When gpiochip_add_data() is called very early during boot, so that GPIOs
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08001020 * can be freely used, the chip->parent device must be registered before
David Brownelld8f388d2008-07-25 01:46:07 -07001021 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1022 * for GPIOs will fail rudely.
1023 *
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001024 * gpiochip_add_data() must only be called after gpiolib initialization,
1025 * ie after core_initcall().
1026 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001027 * If chip->base is negative, this requests dynamic assignment of
1028 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001029 */
Linus Walleijb08ea352015-12-03 15:14:13 +01001030int gpiochip_add_data(struct gpio_chip *chip, void *data)
David Brownelld2876d02008-02-04 22:28:20 -08001031{
1032 unsigned long flags;
1033 int status = 0;
Linus Walleijff2b1352015-10-20 11:10:38 +02001034 unsigned i;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001035 int base = chip->base;
Linus Walleijff2b1352015-10-20 11:10:38 +02001036 struct gpio_device *gdev;
David Brownelld2876d02008-02-04 22:28:20 -08001037
Linus Walleijff2b1352015-10-20 11:10:38 +02001038 /*
1039 * First: allocate and populate the internal stat container, and
1040 * set up the struct device.
1041 */
Josh Cartwright969f07b2016-02-17 16:44:15 -06001042 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
Linus Walleijff2b1352015-10-20 11:10:38 +02001043 if (!gdev)
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001044 return -ENOMEM;
Linus Walleij3c702e92015-10-21 15:29:53 +02001045 gdev->dev.bus = &gpio_bus_type;
Linus Walleijff2b1352015-10-20 11:10:38 +02001046 gdev->chip = chip;
1047 chip->gpiodev = gdev;
1048 if (chip->parent) {
1049 gdev->dev.parent = chip->parent;
1050 gdev->dev.of_node = chip->parent->of_node;
Thierry Redingacc6e332016-07-05 14:11:14 +02001051 }
1052
Linus Walleijff2b1352015-10-20 11:10:38 +02001053#ifdef CONFIG_OF_GPIO
1054 /* If the gpiochip has an assigned OF node this takes precedence */
Thierry Redingacc6e332016-07-05 14:11:14 +02001055 if (chip->of_node)
1056 gdev->dev.of_node = chip->of_node;
Linus Walleijff2b1352015-10-20 11:10:38 +02001057#endif
Thierry Redingacc6e332016-07-05 14:11:14 +02001058
Linus Walleijff2b1352015-10-20 11:10:38 +02001059 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
1060 if (gdev->id < 0) {
1061 status = gdev->id;
1062 goto err_free_gdev;
1063 }
1064 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
1065 device_initialize(&gdev->dev);
1066 dev_set_drvdata(&gdev->dev, gdev);
1067 if (chip->parent && chip->parent->driver)
1068 gdev->owner = chip->parent->driver->owner;
1069 else if (chip->owner)
1070 /* TODO: remove chip->owner */
1071 gdev->owner = chip->owner;
1072 else
1073 gdev->owner = THIS_MODULE;
David Brownelld2876d02008-02-04 22:28:20 -08001074
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001075 gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001076 if (!gdev->descs) {
Linus Walleijff2b1352015-10-20 11:10:38 +02001077 status = -ENOMEM;
1078 goto err_free_gdev;
1079 }
1080
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001081 if (chip->ngpio == 0) {
1082 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
Linus Walleijff2b1352015-10-20 11:10:38 +02001083 status = -EINVAL;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001084 goto err_free_descs;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001085 }
Linus Walleijdf4878e2016-02-12 14:48:23 +01001086
1087 if (chip->label)
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001088 gdev->label = kstrdup(chip->label, GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001089 else
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001090 gdev->label = kstrdup("unknown", GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001091 if (!gdev->label) {
1092 status = -ENOMEM;
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001093 goto err_free_descs;
Linus Walleijdf4878e2016-02-12 14:48:23 +01001094 }
1095
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001096 gdev->ngpio = chip->ngpio;
Linus Walleij43c54ec2016-02-11 11:37:48 +01001097 gdev->data = data;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001098
David Brownelld2876d02008-02-04 22:28:20 -08001099 spin_lock_irqsave(&gpio_lock, flags);
1100
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001101 /*
1102 * TODO: this allocates a Linux GPIO number base in the global
1103 * GPIO numberspace for this chip. In the long run we want to
1104 * get *rid* of this numberspace and use only descriptors, but
1105 * it may be a pipe dream. It will not happen before we get rid
1106 * of the sysfs interface anyways.
1107 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001108 if (base < 0) {
1109 base = gpiochip_find_base(chip->ngpio);
1110 if (base < 0) {
1111 status = base;
Johan Hovold225fce82015-01-12 17:12:25 +01001112 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001113 goto err_free_label;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001114 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001115 /*
1116 * TODO: it should not be necessary to reflect the assigned
1117 * base outside of the GPIO subsystem. Go over drivers and
1118 * see if anyone makes use of this, else drop this and assign
1119 * a poison instead.
1120 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001121 chip->base = base;
1122 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001123 gdev->base = base;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001124
Linus Walleijff2b1352015-10-20 11:10:38 +02001125 status = gpiodev_add_to_list(gdev);
Johan Hovold05aa5202015-01-12 17:12:26 +01001126 if (status) {
1127 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001128 goto err_free_label;
Johan Hovold05aa5202015-01-12 17:12:26 +01001129 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001130
Linus Walleij545ebd92016-05-30 17:11:59 +02001131 spin_unlock_irqrestore(&gpio_lock, flags);
1132
Linus Walleijff2b1352015-10-20 11:10:38 +02001133 for (i = 0; i < chip->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001134 struct gpio_desc *desc = &gdev->descs[i];
David Brownelld8f388d2008-07-25 01:46:07 -07001135
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001136 desc->gdev = gdev;
Linus Walleij72d32002016-04-28 13:33:59 +02001137 /*
1138 * REVISIT: most hardware initializes GPIOs as inputs
1139 * (often with pullups enabled) so power usage is
1140 * minimized. Linux code should set the gpio direction
1141 * first thing; but until it does, and in case
1142 * chip->get_direction is not set, we may expose the
1143 * wrong direction in sysfs.
Johan Hovold05aa5202015-01-12 17:12:26 +01001144 */
Linus Walleij72d32002016-04-28 13:33:59 +02001145
1146 if (chip->get_direction) {
1147 /*
1148 * If we have .get_direction, set up the initial
1149 * direction flag from the hardware.
1150 */
1151 int dir = chip->get_direction(chip, i);
1152
1153 if (!dir)
1154 set_bit(FLAG_IS_OUT, &desc->flags);
1155 } else if (!chip->direction_input) {
1156 /*
1157 * If the chip lacks the .direction_input callback
1158 * we logically assume all lines are outputs.
1159 */
1160 set_bit(FLAG_IS_OUT, &desc->flags);
1161 }
David Brownelld2876d02008-02-04 22:28:20 -08001162 }
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001163
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301164#ifdef CONFIG_PINCTRL
Linus Walleij20ec3e32016-02-11 11:03:06 +01001165 INIT_LIST_HEAD(&gdev->pin_ranges);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301166#endif
1167
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001168 status = gpiochip_set_desc_names(chip);
1169 if (status)
1170 goto err_remove_from_list;
1171
Mika Westerberg79b804c2016-09-20 15:15:21 +03001172 status = gpiochip_irqchip_init_valid_mask(chip);
1173 if (status)
1174 goto err_remove_from_list;
1175
Tomeu Vizoso28355f82015-07-14 10:29:54 +02001176 status = of_gpiochip_add(chip);
1177 if (status)
1178 goto err_remove_chip;
1179
Mika Westerberg664e3e52014-01-08 12:40:54 +02001180 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001181
Linus Walleij3c702e92015-10-21 15:29:53 +02001182 /*
1183 * By first adding the chardev, and then adding the device,
1184 * we get a device node entry in sysfs under
1185 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1186 * coldplug of device nodes and other udev business.
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001187 * We can do this only if gpiolib has been initialized.
1188 * Otherwise, defer until later.
Linus Walleij3c702e92015-10-21 15:29:53 +02001189 */
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001190 if (gpiolib_initialized) {
1191 status = gpiochip_setup_dev(gdev);
1192 if (status)
1193 goto err_remove_chip;
1194 }
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001195 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001196
Johan Hovold225fce82015-01-12 17:12:25 +01001197err_remove_chip:
1198 acpi_gpiochip_remove(chip);
Johan Hovold6d867502015-05-04 17:23:25 +02001199 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +01001200 of_gpiochip_remove(chip);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001201 gpiochip_irqchip_free_valid_mask(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001202err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +01001203 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001204 list_del(&gdev->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001205 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001206err_free_label:
1207 kfree(gdev->label);
1208err_free_descs:
1209 kfree(gdev->descs);
Linus Walleijff2b1352015-10-20 11:10:38 +02001210err_free_gdev:
1211 ida_simple_remove(&gpio_ida, gdev->id);
David Brownelld2876d02008-02-04 22:28:20 -08001212 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +02001213 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001214 gdev->base, gdev->base + gdev->ngpio - 1,
1215 chip->label ? : "generic");
1216 kfree(gdev);
David Brownelld2876d02008-02-04 22:28:20 -08001217 return status;
1218}
Linus Walleijb08ea352015-12-03 15:14:13 +01001219EXPORT_SYMBOL_GPL(gpiochip_add_data);
David Brownelld2876d02008-02-04 22:28:20 -08001220
1221/**
Linus Walleij43c54ec2016-02-11 11:37:48 +01001222 * gpiochip_get_data() - get per-subdriver data for the chip
1223 */
1224void *gpiochip_get_data(struct gpio_chip *chip)
1225{
1226 return chip->gpiodev->data;
1227}
1228EXPORT_SYMBOL_GPL(gpiochip_get_data);
1229
1230/**
David Brownelld2876d02008-02-04 22:28:20 -08001231 * gpiochip_remove() - unregister a gpio_chip
1232 * @chip: the chip to unregister
1233 *
1234 * A gpio_chip with any GPIOs still requested may not be removed.
1235 */
abdoulaye berthee1db1702014-07-05 18:28:50 +02001236void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -08001237{
Linus Walleijff2b1352015-10-20 11:10:38 +02001238 struct gpio_device *gdev = chip->gpiodev;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001239 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001240 unsigned long flags;
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001241 unsigned i;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001242 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -08001243
Linus Walleijff2b1352015-10-20 11:10:38 +02001244 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
Linus Walleijafbc4f32016-02-09 13:21:06 +01001245 gpiochip_sysfs_unregister(gdev);
Bamvor Jian Zhangbd203bd2016-02-20 13:13:19 +08001246 /* Numb the device, cancelling all outstanding operations */
1247 gdev->chip = NULL;
Johan Hovold00acc3d2015-01-12 17:12:27 +01001248 gpiochip_irqchip_remove(chip);
Mika Westerberg6072b9d2014-03-10 14:54:53 +02001249 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +01001250 gpiochip_remove_pin_ranges(chip);
Benoit Parrotf625d462015-02-02 11:44:44 -06001251 gpiochip_free_hogs(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001252 of_gpiochip_remove(chip);
Linus Walleij43c54ec2016-02-11 11:37:48 +01001253 /*
1254 * We accept no more calls into the driver from this point, so
1255 * NULL the driver data pointer
1256 */
1257 gdev->data = NULL;
Anton Vorontsov391c9702010-06-08 07:48:17 -06001258
Johan Hovold6798aca2015-01-12 17:12:28 +01001259 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001260 for (i = 0; i < gdev->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001261 desc = &gdev->descs[i];
Johan Hovoldfab28b82015-05-04 17:10:27 +02001262 if (test_bit(FLAG_REQUESTED, &desc->flags))
1263 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -08001264 }
David Brownelld2876d02008-02-04 22:28:20 -08001265 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001266
Johan Hovoldfab28b82015-05-04 17:10:27 +02001267 if (requested)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001268 dev_crit(&gdev->dev,
Linus Walleij58383c72015-11-04 09:56:26 +01001269 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +02001270
Linus Walleijff2b1352015-10-20 11:10:38 +02001271 /*
1272 * The gpiochip side puts its use of the device to rest here:
1273 * if there are no userspace clients, the chardev and device will
1274 * be removed, else it will be dangling until the last user is
1275 * gone.
1276 */
Ricardo Ribalda Delgadof4833b82016-06-03 19:10:02 +02001277 cdev_del(&gdev->chrdev);
1278 device_del(&gdev->dev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001279 put_device(&gdev->dev);
David Brownelld2876d02008-02-04 22:28:20 -08001280}
1281EXPORT_SYMBOL_GPL(gpiochip_remove);
1282
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301283static void devm_gpio_chip_release(struct device *dev, void *res)
1284{
1285 struct gpio_chip *chip = *(struct gpio_chip **)res;
1286
1287 gpiochip_remove(chip);
1288}
1289
1290static int devm_gpio_chip_match(struct device *dev, void *res, void *data)
1291
1292{
1293 struct gpio_chip **r = res;
1294
1295 if (!r || !*r) {
1296 WARN_ON(!r || !*r);
1297 return 0;
1298 }
1299
1300 return *r == data;
1301}
1302
1303/**
1304 * devm_gpiochip_add_data() - Resource manager piochip_add_data()
1305 * @dev: the device pointer on which irq_chip belongs to.
1306 * @chip: the chip to register, with chip->base initialized
1307 * Context: potentially before irqs will work
1308 *
1309 * Returns a negative errno if the chip can't be registered, such as
1310 * because the chip->base is invalid or already associated with a
1311 * different chip. Otherwise it returns zero as a success code.
1312 *
1313 * The gpio chip automatically be released when the device is unbound.
1314 */
1315int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
1316 void *data)
1317{
1318 struct gpio_chip **ptr;
1319 int ret;
1320
1321 ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
1322 GFP_KERNEL);
1323 if (!ptr)
1324 return -ENOMEM;
1325
1326 ret = gpiochip_add_data(chip, data);
1327 if (ret < 0) {
1328 devres_free(ptr);
1329 return ret;
1330 }
1331
1332 *ptr = chip;
1333 devres_add(dev, ptr);
1334
1335 return 0;
1336}
1337EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
1338
1339/**
1340 * devm_gpiochip_remove() - Resource manager of gpiochip_remove()
1341 * @dev: device for which which resource was allocated
1342 * @chip: the chip to remove
1343 *
1344 * A gpio_chip with any GPIOs still requested may not be removed.
1345 */
1346void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip)
1347{
1348 int ret;
1349
1350 ret = devres_release(dev, devm_gpio_chip_release,
1351 devm_gpio_chip_match, chip);
1352 if (!ret)
1353 WARN_ON(ret);
1354}
1355EXPORT_SYMBOL_GPL(devm_gpiochip_remove);
1356
Grant Likely594fa262010-06-08 07:48:16 -06001357/**
1358 * gpiochip_find() - iterator for locating a specific gpio_chip
1359 * @data: data to pass to match function
1360 * @callback: Callback function to check gpio_chip
1361 *
1362 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1363 * determined by a user supplied @match callback. The callback should return
1364 * 0 if the device doesn't match and non-zero if it does. If the callback is
1365 * non-zero, this function will return to the caller and not iterate over any
1366 * more gpio_chips.
1367 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001368struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001369 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001370 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001371{
Linus Walleijff2b1352015-10-20 11:10:38 +02001372 struct gpio_device *gdev;
Alexandre Courbot125eef92013-02-03 01:29:26 +09001373 struct gpio_chip *chip;
Grant Likely594fa262010-06-08 07:48:16 -06001374 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001375
1376 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001377 list_for_each_entry(gdev, &gpio_devices, list)
Ricardo Ribalda Delgado11f33a62016-06-03 19:10:01 +02001378 if (gdev->chip && match(gdev->chip, data))
Grant Likely594fa262010-06-08 07:48:16 -06001379 break;
Alexandre Courbot125eef92013-02-03 01:29:26 +09001380
1381 /* No match? */
Linus Walleijff2b1352015-10-20 11:10:38 +02001382 if (&gdev->list == &gpio_devices)
Alexandre Courbot125eef92013-02-03 01:29:26 +09001383 chip = NULL;
Linus Walleijff2b1352015-10-20 11:10:38 +02001384 else
1385 chip = gdev->chip;
1386
Grant Likely594fa262010-06-08 07:48:16 -06001387 spin_unlock_irqrestore(&gpio_lock, flags);
1388
1389 return chip;
1390}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001391EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001392
Alexandre Courbot79697ef2013-11-16 21:39:32 +09001393static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1394{
1395 const char *name = data;
1396
1397 return !strcmp(chip->label, name);
1398}
1399
1400static struct gpio_chip *find_chip_by_name(const char *name)
1401{
1402 return gpiochip_find((void *)name, gpiochip_match_name);
1403}
1404
Linus Walleij14250522014-03-25 10:40:18 +01001405#ifdef CONFIG_GPIOLIB_IRQCHIP
1406
1407/*
1408 * The following is irqchip helper code for gpiochips.
1409 */
1410
Mika Westerberg79b804c2016-09-20 15:15:21 +03001411static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1412{
1413 int i;
1414
1415 if (!gpiochip->irq_need_valid_mask)
1416 return 0;
1417
1418 gpiochip->irq_valid_mask = kcalloc(BITS_TO_LONGS(gpiochip->ngpio),
1419 sizeof(long), GFP_KERNEL);
1420 if (!gpiochip->irq_valid_mask)
1421 return -ENOMEM;
1422
1423 /* Assume by default all GPIOs are valid */
1424 for (i = 0; i < gpiochip->ngpio; i++)
1425 set_bit(i, gpiochip->irq_valid_mask);
1426
1427 return 0;
1428}
1429
1430static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1431{
1432 kfree(gpiochip->irq_valid_mask);
1433 gpiochip->irq_valid_mask = NULL;
1434}
1435
1436static bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
1437 unsigned int offset)
1438{
1439 /* No mask means all valid */
1440 if (likely(!gpiochip->irq_valid_mask))
1441 return true;
1442 return test_bit(offset, gpiochip->irq_valid_mask);
1443}
1444
Linus Walleij14250522014-03-25 10:40:18 +01001445/**
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001446 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
1447 * @gpiochip: the gpiochip to set the irqchip chain to
1448 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01001449 * @parent_irq: the irq number corresponding to the parent IRQ for this
1450 * chained irqchip
1451 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001452 * coming out of the gpiochip. If the interrupt is nested rather than
1453 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +01001454 */
1455void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
1456 struct irq_chip *irqchip,
1457 int parent_irq,
1458 irq_flow_handler_t parent_handler)
1459{
Linus Walleij83141a72014-09-26 13:50:12 +02001460 unsigned int offset;
1461
Linus Walleij83141a72014-09-26 13:50:12 +02001462 if (!gpiochip->irqdomain) {
1463 chip_err(gpiochip, "called %s before setting up irqchip\n",
1464 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +02001465 return;
1466 }
1467
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001468 if (parent_handler) {
1469 if (gpiochip->can_sleep) {
1470 chip_err(gpiochip,
1471 "you cannot have chained interrupts on a "
1472 "chip that may sleep\n");
1473 return;
1474 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001475 /*
1476 * The parent irqchip is already using the chip_data for this
1477 * irqchip, so our callbacks simply use the handler_data.
1478 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +02001479 irq_set_chained_handler_and_data(parent_irq, parent_handler,
1480 gpiochip);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001481
1482 gpiochip->irq_parent = parent_irq;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001483 }
Linus Walleij83141a72014-09-26 13:50:12 +02001484
1485 /* Set the parent IRQ for all affected IRQs */
Mika Westerberg79b804c2016-09-20 15:15:21 +03001486 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1487 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1488 continue;
Linus Walleij83141a72014-09-26 13:50:12 +02001489 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
1490 parent_irq);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001491 }
Linus Walleij14250522014-03-25 10:40:18 +01001492}
1493EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
1494
1495/**
1496 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1497 * @d: the irqdomain used by this irqchip
1498 * @irq: the global irq number used by this GPIO irqchip irq
1499 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1500 *
1501 * This function will set up the mapping for a certain IRQ line on a
1502 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1503 * stored inside the gpiochip.
1504 */
1505static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1506 irq_hw_number_t hwirq)
1507{
1508 struct gpio_chip *chip = d->host_data;
1509
Linus Walleij14250522014-03-25 10:40:18 +01001510 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001511 /*
1512 * This lock class tells lockdep that GPIO irqs are in a different
1513 * category than their parents, so it won't report false recursion.
1514 */
1515 irq_set_lockdep_class(irq, chip->lock_key);
Linus Walleij7633fb92014-04-09 13:20:38 +02001516 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
Linus Walleij1c8732b2014-04-09 13:34:39 +02001517 /* Chips that can sleep need nested thread handlers */
Octavian Purdila295494a2014-09-19 23:22:44 +03001518 if (chip->can_sleep && !chip->irq_not_threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02001519 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +01001520 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -05001521
Linus Walleij1333b902014-04-23 16:45:12 +02001522 /*
1523 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1524 * is passed as default type.
1525 */
1526 if (chip->irq_default_type != IRQ_TYPE_NONE)
1527 irq_set_irq_type(irq, chip->irq_default_type);
Linus Walleij14250522014-03-25 10:40:18 +01001528
1529 return 0;
1530}
1531
Linus Walleijc3626fd2014-03-28 20:42:01 +01001532static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
1533{
Linus Walleij1c8732b2014-04-09 13:34:39 +02001534 struct gpio_chip *chip = d->host_data;
1535
Linus Walleij1c8732b2014-04-09 13:34:39 +02001536 if (chip->can_sleep)
1537 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001538 irq_set_chip_and_handler(irq, NULL, NULL);
1539 irq_set_chip_data(irq, NULL);
1540}
1541
Linus Walleij14250522014-03-25 10:40:18 +01001542static const struct irq_domain_ops gpiochip_domain_ops = {
1543 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +01001544 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +01001545 /* Virtually all GPIO irqchips are twocell:ed */
1546 .xlate = irq_domain_xlate_twocell,
1547};
1548
1549static int gpiochip_irq_reqres(struct irq_data *d)
1550{
1551 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1552
Linus Walleijff2b1352015-10-20 11:10:38 +02001553 if (!try_module_get(chip->gpiodev->owner))
Grygorii Strashko5b76e792015-06-25 20:30:50 +03001554 return -ENODEV;
1555
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001556 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +01001557 chip_err(chip,
1558 "unable to lock HW IRQ %lu for IRQ\n",
1559 d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001560 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001561 return -EINVAL;
1562 }
1563 return 0;
1564}
1565
1566static void gpiochip_irq_relres(struct irq_data *d)
1567{
1568 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1569
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001570 gpiochip_unlock_as_irq(chip, d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001571 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001572}
1573
1574static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
1575{
1576 return irq_find_mapping(chip->irqdomain, offset);
1577}
1578
1579/**
1580 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1581 * @gpiochip: the gpiochip to remove the irqchip from
1582 *
1583 * This is called only from gpiochip_remove()
1584 */
1585static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
1586{
Linus Walleijc3626fd2014-03-28 20:42:01 +01001587 unsigned int offset;
1588
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001589 acpi_gpiochip_free_interrupts(gpiochip);
1590
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001591 if (gpiochip->irq_parent) {
1592 irq_set_chained_handler(gpiochip->irq_parent, NULL);
1593 irq_set_handler_data(gpiochip->irq_parent, NULL);
1594 }
1595
Linus Walleijc3626fd2014-03-28 20:42:01 +01001596 /* Remove all IRQ mappings and delete the domain */
1597 if (gpiochip->irqdomain) {
Mika Westerberg79b804c2016-09-20 15:15:21 +03001598 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1599 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1600 continue;
Grygorii Strashkoe3893382014-09-25 19:09:23 +03001601 irq_dispose_mapping(
1602 irq_find_mapping(gpiochip->irqdomain, offset));
Mika Westerberg79b804c2016-09-20 15:15:21 +03001603 }
Linus Walleij14250522014-03-25 10:40:18 +01001604 irq_domain_remove(gpiochip->irqdomain);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001605 }
Linus Walleij14250522014-03-25 10:40:18 +01001606
1607 if (gpiochip->irqchip) {
1608 gpiochip->irqchip->irq_request_resources = NULL;
1609 gpiochip->irqchip->irq_release_resources = NULL;
1610 gpiochip->irqchip = NULL;
1611 }
Mika Westerberg79b804c2016-09-20 15:15:21 +03001612
1613 gpiochip_irqchip_free_valid_mask(gpiochip);
Linus Walleij14250522014-03-25 10:40:18 +01001614}
1615
1616/**
1617 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
1618 * @gpiochip: the gpiochip to add the irqchip to
1619 * @irqchip: the irqchip to add to the gpiochip
1620 * @first_irq: if not dynamically assigned, the base (first) IRQ to
1621 * allocate gpiochip irqs from
1622 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +02001623 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
1624 * to have the core avoid setting up any default type in the hardware.
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001625 * @lock_key: lockdep class
Linus Walleij14250522014-03-25 10:40:18 +01001626 *
1627 * This function closely associates a certain irqchip with a certain
1628 * gpiochip, providing an irq domain to translate the local IRQs to
1629 * global irqs in the gpiolib core, and making sure that the gpiochip
1630 * is passed as chip data to all related functions. Driver callbacks
Linus Walleij09dd5f92015-12-07 15:31:58 +01001631 * need to use gpiochip_get_data() to get their local state containers back
Linus Walleij14250522014-03-25 10:40:18 +01001632 * from the gpiochip passed as chip data. An irqdomain will be stored
1633 * in the gpiochip that shall be used by the driver to handle IRQ number
1634 * translation. The gpiochip will need to be initialized and registered
1635 * before calling this function.
1636 *
Linus Walleijc3626fd2014-03-28 20:42:01 +01001637 * This function will handle two cell:ed simple IRQs and assumes all
1638 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +01001639 * need to be open coded.
1640 */
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001641int _gpiochip_irqchip_add(struct gpio_chip *gpiochip,
1642 struct irq_chip *irqchip,
1643 unsigned int first_irq,
1644 irq_flow_handler_t handler,
1645 unsigned int type,
1646 struct lock_class_key *lock_key)
Linus Walleij14250522014-03-25 10:40:18 +01001647{
1648 struct device_node *of_node;
Mika Westerberg79b804c2016-09-20 15:15:21 +03001649 bool irq_base_set = false;
Linus Walleij14250522014-03-25 10:40:18 +01001650 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +01001651 unsigned irq_base = 0;
Linus Walleij14250522014-03-25 10:40:18 +01001652
1653 if (!gpiochip || !irqchip)
1654 return -EINVAL;
1655
Linus Walleij58383c72015-11-04 09:56:26 +01001656 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +01001657 pr_err("missing gpiochip .dev parent pointer\n");
1658 return -EINVAL;
1659 }
Linus Walleij58383c72015-11-04 09:56:26 +01001660 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +01001661#ifdef CONFIG_OF_GPIO
1662 /*
Colin Cronin20a8a962015-05-18 11:41:43 -07001663 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08001664 * FIXME: get rid of this and use gpiochip->parent->of_node
1665 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +01001666 */
1667 if (gpiochip->of_node)
1668 of_node = gpiochip->of_node;
1669#endif
1670 gpiochip->irqchip = irqchip;
1671 gpiochip->irq_handler = handler;
1672 gpiochip->irq_default_type = type;
1673 gpiochip->to_irq = gpiochip_to_irq;
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001674 gpiochip->lock_key = lock_key;
Linus Walleij14250522014-03-25 10:40:18 +01001675 gpiochip->irqdomain = irq_domain_add_simple(of_node,
1676 gpiochip->ngpio, first_irq,
1677 &gpiochip_domain_ops, gpiochip);
1678 if (!gpiochip->irqdomain) {
1679 gpiochip->irqchip = NULL;
1680 return -EINVAL;
1681 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +02001682
1683 /*
1684 * It is possible for a driver to override this, but only if the
1685 * alternative functions are both implemented.
1686 */
1687 if (!irqchip->irq_request_resources &&
1688 !irqchip->irq_release_resources) {
1689 irqchip->irq_request_resources = gpiochip_irq_reqres;
1690 irqchip->irq_release_resources = gpiochip_irq_relres;
1691 }
Linus Walleij14250522014-03-25 10:40:18 +01001692
1693 /*
1694 * Prepare the mapping since the irqchip shall be orthogonal to
1695 * any gpiochip calls. If the first_irq was zero, this is
1696 * necessary to allocate descriptors for all IRQs.
1697 */
Linus Walleijc3626fd2014-03-28 20:42:01 +01001698 for (offset = 0; offset < gpiochip->ngpio; offset++) {
Mika Westerberg79b804c2016-09-20 15:15:21 +03001699 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1700 continue;
Linus Walleijc3626fd2014-03-28 20:42:01 +01001701 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001702 if (!irq_base_set) {
Linus Walleijc3626fd2014-03-28 20:42:01 +01001703 /*
1704 * Store the base into the gpiochip to be used when
1705 * unmapping the irqs.
1706 */
1707 gpiochip->irq_base = irq_base;
Mika Westerberg79b804c2016-09-20 15:15:21 +03001708 irq_base_set = true;
1709 }
Linus Walleijc3626fd2014-03-28 20:42:01 +01001710 }
Linus Walleij14250522014-03-25 10:40:18 +01001711
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001712 acpi_gpiochip_request_interrupts(gpiochip);
1713
Linus Walleij14250522014-03-25 10:40:18 +01001714 return 0;
1715}
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001716EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add);
Linus Walleij14250522014-03-25 10:40:18 +01001717
1718#else /* CONFIG_GPIOLIB_IRQCHIP */
1719
1720static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
Mika Westerberg79b804c2016-09-20 15:15:21 +03001721static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1722{
1723 return 0;
1724}
1725static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1726{ }
Linus Walleij14250522014-03-25 10:40:18 +01001727
1728#endif /* CONFIG_GPIOLIB_IRQCHIP */
1729
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001730/**
1731 * gpiochip_generic_request() - request the gpio function for a pin
1732 * @chip: the gpiochip owning the GPIO
1733 * @offset: the offset of the GPIO to request for GPIO function
1734 */
1735int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
1736{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001737 return pinctrl_request_gpio(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001738}
1739EXPORT_SYMBOL_GPL(gpiochip_generic_request);
1740
1741/**
1742 * gpiochip_generic_free() - free the gpio function from a pin
1743 * @chip: the gpiochip to request the gpio function for
1744 * @offset: the offset of the GPIO to free from GPIO function
1745 */
1746void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
1747{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001748 pinctrl_free_gpio(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001749}
1750EXPORT_SYMBOL_GPL(gpiochip_generic_free);
1751
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301752#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01001753
Linus Walleij3f0f8672012-11-20 12:40:15 +01001754/**
Christian Ruppert586a87e2013-10-15 15:37:54 +02001755 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1756 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +02001757 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +02001758 * @gpio_offset: the start offset in the current gpio_chip number space
1759 * @pin_group: name of the pin group inside the pin controller
1760 */
1761int gpiochip_add_pingroup_range(struct gpio_chip *chip,
1762 struct pinctrl_dev *pctldev,
1763 unsigned int gpio_offset, const char *pin_group)
1764{
1765 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001766 struct gpio_device *gdev = chip->gpiodev;
Christian Ruppert586a87e2013-10-15 15:37:54 +02001767 int ret;
1768
1769 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1770 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001771 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +02001772 return -ENOMEM;
1773 }
1774
1775 /* Use local offset as range ID */
1776 pin_range->range.id = gpio_offset;
1777 pin_range->range.gc = chip;
1778 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001779 pin_range->range.base = gdev->base + gpio_offset;
Christian Ruppert586a87e2013-10-15 15:37:54 +02001780 pin_range->pctldev = pctldev;
1781
1782 ret = pinctrl_get_group_pins(pctldev, pin_group,
1783 &pin_range->range.pins,
1784 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001785 if (ret < 0) {
1786 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001787 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001788 }
Christian Ruppert586a87e2013-10-15 15:37:54 +02001789
1790 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1791
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001792 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1793 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +02001794 pinctrl_dev_get_devname(pctldev), pin_group);
1795
Linus Walleij20ec3e32016-02-11 11:03:06 +01001796 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001797
1798 return 0;
1799}
1800EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1801
1802/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01001803 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1804 * @chip: the gpiochip to add the range for
1805 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01001806 * @gpio_offset: the start offset in the current gpio_chip number space
1807 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01001808 * @npins: the number of pins from the offset of each pin space (GPIO and
1809 * pin controller) to accumulate in this range
1810 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001811int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01001812 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01001813 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301814{
1815 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001816 struct gpio_device *gdev = chip->gpiodev;
Axel Linb4d4b1f2012-11-21 14:33:56 +08001817 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301818
Linus Walleij3f0f8672012-11-20 12:40:15 +01001819 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301820 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001821 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001822 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301823 }
1824
Linus Walleij3f0f8672012-11-20 12:40:15 +01001825 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01001826 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001827 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301828 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001829 pin_range->range.base = gdev->base + gpio_offset;
Linus Walleij316511c2012-11-21 08:48:09 +01001830 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301831 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01001832 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301833 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01001834 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08001835 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001836 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +01001837 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08001838 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001839 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001840 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1841 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +01001842 pinctl_name,
1843 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301844
Linus Walleij20ec3e32016-02-11 11:03:06 +01001845 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001846
1847 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301848}
Linus Walleij165adc92012-11-06 14:49:39 +01001849EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301850
Linus Walleij3f0f8672012-11-20 12:40:15 +01001851/**
1852 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1853 * @chip: the chip to remove all the mappings for
1854 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301855void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1856{
1857 struct gpio_pin_range *pin_range, *tmp;
Linus Walleij20ec3e32016-02-11 11:03:06 +01001858 struct gpio_device *gdev = chip->gpiodev;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301859
Linus Walleij20ec3e32016-02-11 11:03:06 +01001860 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301861 list_del(&pin_range->node);
1862 pinctrl_remove_gpio_range(pin_range->pctldev,
1863 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001864 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301865 }
1866}
Linus Walleij165adc92012-11-06 14:49:39 +01001867EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1868
1869#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301870
David Brownelld2876d02008-02-04 22:28:20 -08001871/* These "optional" allocation calls help prevent drivers from stomping
1872 * on each other, and help provide better diagnostics in debugfs.
1873 * They're called even less than the "set direction" calls.
1874 */
Mika Westerberg77c2d792014-03-10 14:54:50 +02001875static int __gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08001876{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001877 struct gpio_chip *chip = desc->gdev->chip;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001878 int status;
David Brownelld2876d02008-02-04 22:28:20 -08001879 unsigned long flags;
1880
1881 spin_lock_irqsave(&gpio_lock, flags);
1882
David Brownelld2876d02008-02-04 22:28:20 -08001883 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07001884 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001885 */
1886
1887 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1888 desc_set_label(desc, label ? : "?");
1889 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001890 } else {
David Brownelld2876d02008-02-04 22:28:20 -08001891 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -08001892 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001893 }
1894
1895 if (chip->request) {
1896 /* chip->request may sleep */
1897 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001898 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001899 spin_lock_irqsave(&gpio_lock, flags);
1900
1901 if (status < 0) {
1902 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -07001903 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001904 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001905 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001906 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03001907 if (chip->get_direction) {
1908 /* chip->get_direction may sleep */
1909 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001910 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001911 spin_lock_irqsave(&gpio_lock, flags);
1912 }
David Brownelld2876d02008-02-04 22:28:20 -08001913done:
Mika Westerberg77c2d792014-03-10 14:54:50 +02001914 spin_unlock_irqrestore(&gpio_lock, flags);
1915 return status;
1916}
1917
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001918/*
1919 * This descriptor validation needs to be inserted verbatim into each
1920 * function taking a descriptor, so we need to use a preprocessor
Linus Walleij54d77192016-05-30 16:48:39 +02001921 * macro to avoid endless duplication. If the desc is NULL it is an
1922 * optional GPIO and calls should just bail out.
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001923 */
1924#define VALIDATE_DESC(desc) do { \
Linus Walleij54d77192016-05-30 16:48:39 +02001925 if (!desc) \
1926 return 0; \
Linus Walleijbfbbe442016-06-16 11:55:55 +02001927 if (IS_ERR(desc)) { \
1928 pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
1929 return PTR_ERR(desc); \
1930 } \
Linus Walleij54d77192016-05-30 16:48:39 +02001931 if (!desc->gdev) { \
Linus Walleijbfbbe442016-06-16 11:55:55 +02001932 pr_warn("%s: invalid GPIO (no device)\n", __func__); \
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001933 return -EINVAL; \
1934 } \
1935 if ( !desc->gdev->chip ) { \
1936 dev_warn(&desc->gdev->dev, \
1937 "%s: backing chip is gone\n", __func__); \
1938 return 0; \
1939 } } while (0)
1940
1941#define VALIDATE_DESC_VOID(desc) do { \
Linus Walleij54d77192016-05-30 16:48:39 +02001942 if (!desc) \
1943 return; \
Linus Walleijbfbbe442016-06-16 11:55:55 +02001944 if (IS_ERR(desc)) { \
1945 pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
1946 return; \
1947 } \
Linus Walleij54d77192016-05-30 16:48:39 +02001948 if (!desc->gdev) { \
Linus Walleijbfbbe442016-06-16 11:55:55 +02001949 pr_warn("%s: invalid GPIO (no device)\n", __func__); \
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001950 return; \
1951 } \
1952 if (!desc->gdev->chip) { \
1953 dev_warn(&desc->gdev->dev, \
1954 "%s: backing chip is gone\n", __func__); \
1955 return; \
1956 } } while (0)
1957
1958
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09001959int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001960{
1961 int status = -EPROBE_DEFER;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001962 struct gpio_device *gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001963
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001964 VALIDATE_DESC(desc);
1965 gdev = desc->gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001966
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001967 if (try_module_get(gdev->owner)) {
Mika Westerberg77c2d792014-03-10 14:54:50 +02001968 status = __gpiod_request(desc, label);
1969 if (status < 0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001970 module_put(gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01001971 else
1972 get_device(&gdev->dev);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001973 }
1974
David Brownelld2876d02008-02-04 22:28:20 -08001975 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +02001976 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001977
David Brownelld2876d02008-02-04 22:28:20 -08001978 return status;
1979}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001980
Mika Westerberg77c2d792014-03-10 14:54:50 +02001981static bool __gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001982{
Mika Westerberg77c2d792014-03-10 14:54:50 +02001983 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08001984 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07001985 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001986
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07001987 might_sleep();
1988
Alexandre Courbot372e7222013-02-03 01:29:29 +09001989 gpiod_unexport(desc);
David Brownelld8f388d2008-07-25 01:46:07 -07001990
David Brownelld2876d02008-02-04 22:28:20 -08001991 spin_lock_irqsave(&gpio_lock, flags);
1992
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001993 chip = desc->gdev->chip;
David Brownell35e8bb52008-10-15 22:03:16 -07001994 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1995 if (chip->free) {
1996 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07001997 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001998 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001999 spin_lock_irqsave(&gpio_lock, flags);
2000 }
David Brownelld2876d02008-02-04 22:28:20 -08002001 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08002002 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07002003 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302004 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302005 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06002006 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002007 ret = true;
2008 }
David Brownelld2876d02008-02-04 22:28:20 -08002009
2010 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002011 return ret;
2012}
2013
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002014void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002015{
Linus Walleij33a68e82016-02-11 10:28:44 +01002016 if (desc && desc->gdev && __gpiod_free(desc)) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002017 module_put(desc->gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002018 put_device(&desc->gdev->dev);
2019 } else {
Mika Westerberg77c2d792014-03-10 14:54:50 +02002020 WARN_ON(extra_checks);
Linus Walleij33a68e82016-02-11 10:28:44 +01002021 }
David Brownelld2876d02008-02-04 22:28:20 -08002022}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002023
David Brownelld2876d02008-02-04 22:28:20 -08002024/**
2025 * gpiochip_is_requested - return string iff signal was requested
2026 * @chip: controller managing the signal
2027 * @offset: of signal within controller's 0..(ngpio - 1) range
2028 *
2029 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09002030 * The string returned is the label passed to gpio_request(); if none has been
2031 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08002032 *
2033 * This function is for use by GPIO controller drivers. The label can
2034 * help with diagnostics, and knowing that the signal is used as a GPIO
2035 * can help avoid accidentally multiplexing it to another controller.
2036 */
2037const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
2038{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002039 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08002040
Dirk Behme48b59532015-08-18 18:02:32 +02002041 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08002042 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002043
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002044 desc = &chip->gpiodev->descs[offset];
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002045
Alexandre Courbot372e7222013-02-03 01:29:29 +09002046 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08002047 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002048 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08002049}
2050EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2051
Mika Westerberg77c2d792014-03-10 14:54:50 +02002052/**
2053 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
2054 * @desc: GPIO descriptor to request
2055 * @label: label for the GPIO
2056 *
2057 * Function allows GPIO chip drivers to request and use their own GPIO
2058 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2059 * function will not increase reference count of the GPIO chip module. This
2060 * allows the GPIO chip module to be unloaded as needed (we assume that the
2061 * GPIO chip driver handles freeing the GPIOs it has requested).
2062 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002063struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
2064 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002065{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002066 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
2067 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002068
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002069 if (IS_ERR(desc)) {
2070 chip_err(chip, "failed to get GPIO descriptor\n");
2071 return desc;
2072 }
2073
2074 err = __gpiod_request(desc, label);
2075 if (err < 0)
2076 return ERR_PTR(err);
2077
2078 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002079}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002080EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002081
2082/**
2083 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2084 * @desc: GPIO descriptor to free
2085 *
2086 * Function frees the given GPIO requested previously with
2087 * gpiochip_request_own_desc().
2088 */
2089void gpiochip_free_own_desc(struct gpio_desc *desc)
2090{
2091 if (desc)
2092 __gpiod_free(desc);
2093}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002094EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08002095
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002096/*
2097 * Drivers MUST set GPIO direction before making get/set calls. In
David Brownelld2876d02008-02-04 22:28:20 -08002098 * some cases this is done in early boot, before IRQs are enabled.
2099 *
2100 * As a rule these aren't called more than once (except for drivers
2101 * using the open-drain emulation idiom) so these are natural places
2102 * to accumulate extra debugging checks. Note that we can't (yet)
2103 * rely on gpio_request() having been called beforehand.
2104 */
2105
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002106/**
2107 * gpiod_direction_input - set the GPIO direction to input
2108 * @desc: GPIO to set to input
2109 *
2110 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2111 * be called safely on it.
2112 *
2113 * Return 0 in case of success, else an error code.
2114 */
2115int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002116{
David Brownelld2876d02008-02-04 22:28:20 -08002117 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002118 int status = -EINVAL;
2119
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002120 VALIDATE_DESC(desc);
2121 chip = desc->gdev->chip;
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002122
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002123 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01002124 gpiod_warn(desc,
2125 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02002126 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002127 return -EIO;
2128 }
2129
Alexandre Courbotd82da792014-07-22 16:17:43 +09002130 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -08002131 if (status == 0)
2132 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002133
Alexandre Courbot372e7222013-02-03 01:29:29 +09002134 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09002135
David Brownelld2876d02008-02-04 22:28:20 -08002136 return status;
2137}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002138EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002139
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002140static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08002141{
Linus Walleijc663e5f2016-03-22 10:51:16 +01002142 struct gpio_chip *gc = desc->gdev->chip;
2143 int ret;
David Brownelld2876d02008-02-04 22:28:20 -08002144
Linus Walleijd468bf92013-09-24 11:54:38 +02002145 /* GPIOs used for IRQs shall not be set as output */
2146 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
2147 gpiod_err(desc,
2148 "%s: tried to set a GPIO tied to an IRQ as output\n",
2149 __func__);
2150 return -EIO;
2151 }
2152
Linus Walleijc663e5f2016-03-22 10:51:16 +01002153 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2154 /* First see if we can enable open drain in hardware */
2155 if (gc->set_single_ended) {
2156 ret = gc->set_single_ended(gc, gpio_chip_hwgpio(desc),
2157 LINE_MODE_OPEN_DRAIN);
2158 if (!ret)
2159 goto set_output_value;
2160 }
2161 /* Emulate open drain by not actively driving the line high */
2162 if (value)
2163 return gpiod_direction_input(desc);
2164 }
2165 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2166 if (gc->set_single_ended) {
2167 ret = gc->set_single_ended(gc, gpio_chip_hwgpio(desc),
2168 LINE_MODE_OPEN_SOURCE);
2169 if (!ret)
2170 goto set_output_value;
2171 }
2172 /* Emulate open source by not actively driving the line low */
2173 if (!value)
2174 return gpiod_direction_input(desc);
2175 } else {
2176 /* Make sure to disable open drain/source hardware, if any */
2177 if (gc->set_single_ended)
2178 gc->set_single_ended(gc,
2179 gpio_chip_hwgpio(desc),
2180 LINE_MODE_PUSH_PULL);
2181 }
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302182
Linus Walleijc663e5f2016-03-22 10:51:16 +01002183set_output_value:
2184 if (!gc->set || !gc->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01002185 gpiod_warn(desc,
2186 "%s: missing set() or direction_output() operations\n",
2187 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002188 return -EIO;
2189 }
2190
Linus Walleijc663e5f2016-03-22 10:51:16 +01002191 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), value);
2192 if (!ret)
David Brownelld2876d02008-02-04 22:28:20 -08002193 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002194 trace_gpio_value(desc_to_gpio(desc), 0, value);
Linus Walleijc663e5f2016-03-22 10:51:16 +01002195 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2196 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08002197}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002198
2199/**
2200 * gpiod_direction_output_raw - set the GPIO direction to output
2201 * @desc: GPIO to set to output
2202 * @value: initial output value of the GPIO
2203 *
2204 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2205 * be called safely on it. The initial value of the output must be specified
2206 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2207 *
2208 * Return 0 in case of success, else an error code.
2209 */
2210int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2211{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002212 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002213 return _gpiod_direction_output_raw(desc, value);
2214}
2215EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2216
2217/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05302218 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002219 * @desc: GPIO to set to output
2220 * @value: initial output value of the GPIO
2221 *
2222 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2223 * be called safely on it. The initial value of the output must be specified
2224 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2225 * account.
2226 *
2227 * Return 0 in case of success, else an error code.
2228 */
2229int gpiod_direction_output(struct gpio_desc *desc, int value)
2230{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002231 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002232 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2233 value = !value;
2234 return _gpiod_direction_output_raw(desc, value);
2235}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002236EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08002237
Felipe Balbic4b5be92010-05-26 14:42:23 -07002238/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002239 * gpiod_set_debounce - sets @debounce time for a @gpio
Felipe Balbic4b5be92010-05-26 14:42:23 -07002240 * @gpio: the gpio to set debounce time
2241 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02002242 *
2243 * returns -ENOTSUPP if the controller does not support setting
2244 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07002245 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002246int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07002247{
Felipe Balbic4b5be92010-05-26 14:42:23 -07002248 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07002249
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002250 VALIDATE_DESC(desc);
2251 chip = desc->gdev->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002252 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01002253 gpiod_dbg(desc,
2254 "%s: missing set() or set_debounce() operations\n",
2255 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02002256 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002257 }
2258
Alexandre Courbotd82da792014-07-22 16:17:43 +09002259 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07002260}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002261EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002262
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002263/**
2264 * gpiod_is_active_low - test whether a GPIO is active-low or not
2265 * @desc: the gpio descriptor to test
2266 *
2267 * Returns 1 if the GPIO is active-low, 0 otherwise.
2268 */
2269int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002270{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002271 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002272 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002273}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002274EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08002275
2276/* I/O calls are only valid after configuration completed; the relevant
2277 * "is this a valid GPIO" error checks should already have been done.
2278 *
2279 * "Get" operations are often inlinable as reading a pin value register,
2280 * and masking the relevant bit in that register.
2281 *
2282 * When "set" operations are inlinable, they involve writing that mask to
2283 * one register to set a low value, or a different register to set it high.
2284 * Otherwise locking is needed, so there may be little value to inlining.
2285 *
2286 *------------------------------------------------------------------------
2287 *
2288 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2289 * have requested the GPIO. That can include implicit requesting by
2290 * a direction setting call. Marking a gpio as requested locks its chip
2291 * in memory, guaranteeing that these table lookups need no more locking
2292 * and that gpiochip_remove() will fail.
2293 *
2294 * REVISIT when debugging, consider adding some instrumentation to ensure
2295 * that the GPIO was actually requested.
2296 */
2297
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002298static int _gpiod_get_raw_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002299{
2300 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002301 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002302 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002303
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002304 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002305 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002306 value = chip->get ? chip->get(chip, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01002307 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002308 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002309 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002310}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002311
David Brownelld2876d02008-02-04 22:28:20 -08002312/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002313 * gpiod_get_raw_value() - return a gpio's raw value
2314 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08002315 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002316 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002317 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002318 *
2319 * This function should be called from contexts where we cannot sleep, and will
2320 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002321 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002322int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002323{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002324 VALIDATE_DESC(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002325 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002326 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002327 return _gpiod_get_raw_value(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002328}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002329EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002330
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002331/**
2332 * gpiod_get_value() - return a gpio's value
2333 * @desc: gpio whose value will be returned
2334 *
2335 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002336 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002337 *
2338 * This function should be called from contexts where we cannot sleep, and will
2339 * complain if the GPIO chip functions potentially sleep.
2340 */
2341int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002342{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002343 int value;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002344
2345 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002346 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002347 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002348
2349 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002350 if (value < 0)
2351 return value;
2352
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002353 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2354 value = !value;
2355
2356 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002357}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002358EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08002359
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302360/*
2361 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002362 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002363 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302364 */
Alexandre Courbot23600962014-03-11 15:52:09 +09002365static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302366{
2367 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002368 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002369 int offset = gpio_chip_hwgpio(desc);
2370
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302371 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002372 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302373 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002374 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302375 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002376 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302377 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002378 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302379 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002380 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302381 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002382 gpiod_err(desc,
2383 "%s: Error in set_value for open drain err %d\n",
2384 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302385}
2386
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302387/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002388 * _gpio_set_open_source_value() - Set the open source gpio's value.
2389 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002390 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302391 */
Alexandre Courbot23600962014-03-11 15:52:09 +09002392static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302393{
2394 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002395 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002396 int offset = gpio_chip_hwgpio(desc);
2397
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302398 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002399 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302400 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002401 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302402 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002403 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302404 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002405 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302406 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002407 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302408 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002409 gpiod_err(desc,
2410 "%s: Error in set_value for open source err %d\n",
2411 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302412}
2413
Alexandre Courbot23600962014-03-11 15:52:09 +09002414static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08002415{
2416 struct gpio_chip *chip;
2417
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002418 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002419 trace_gpio_value(desc_to_gpio(desc), 0, value);
2420 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2421 _gpio_set_open_drain_value(desc, value);
2422 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2423 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302424 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09002425 chip->set(chip, gpio_chip_hwgpio(desc), value);
2426}
2427
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002428/*
2429 * set multiple outputs on the same chip;
2430 * use the chip's set_multiple function if available;
2431 * otherwise set the outputs sequentially;
2432 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
2433 * defines which outputs are to be changed
2434 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
2435 * defines the values the outputs specified by mask are to be set to
2436 */
2437static void gpio_chip_set_multiple(struct gpio_chip *chip,
2438 unsigned long *mask, unsigned long *bits)
2439{
2440 if (chip->set_multiple) {
2441 chip->set_multiple(chip, mask, bits);
2442 } else {
2443 int i;
2444 for (i = 0; i < chip->ngpio; i++) {
2445 if (mask[BIT_WORD(i)] == 0) {
2446 /* no more set bits in this mask word;
2447 * skip ahead to the next word */
2448 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
2449 continue;
2450 }
2451 /* set outputs if the corresponding mask bit is set */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002452 if (__test_and_clear_bit(i, mask))
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002453 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002454 }
2455 }
2456}
2457
Linus Walleij44c7288f2016-04-24 11:36:59 +02002458void gpiod_set_array_value_complex(bool raw, bool can_sleep,
2459 unsigned int array_size,
2460 struct gpio_desc **desc_array,
2461 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002462{
2463 int i = 0;
2464
2465 while (i < array_size) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002466 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002467 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2468 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2469 int count = 0;
2470
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002471 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002472 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002473
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002474 memset(mask, 0, sizeof(mask));
2475 do {
2476 struct gpio_desc *desc = desc_array[i];
2477 int hwgpio = gpio_chip_hwgpio(desc);
2478 int value = value_array[i];
2479
2480 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2481 value = !value;
2482 trace_gpio_value(desc_to_gpio(desc), 0, value);
2483 /*
2484 * collect all normal outputs belonging to the same chip
2485 * open drain and open source outputs are set individually
2486 */
2487 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002488 _gpio_set_open_drain_value(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002489 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2490 _gpio_set_open_source_value(desc, value);
2491 } else {
2492 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002493 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002494 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002495 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002496 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002497 count++;
2498 }
2499 i++;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002500 } while ((i < array_size) &&
2501 (desc_array[i]->gdev->chip == chip));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002502 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002503 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002504 gpio_chip_set_multiple(chip, mask, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002505 }
2506}
2507
David Brownelld2876d02008-02-04 22:28:20 -08002508/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002509 * gpiod_set_raw_value() - assign a gpio's raw value
2510 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08002511 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002512 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002513 * Set the raw value of the GPIO, i.e. the value of its physical line without
2514 * regard for its ACTIVE_LOW status.
2515 *
2516 * This function should be called from contexts where we cannot sleep, and will
2517 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002518 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002519void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002520{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002521 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01002522 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002523 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002524 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002525}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002526EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002527
2528/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002529 * gpiod_set_value() - assign a gpio's value
2530 * @desc: gpio whose value will be assigned
2531 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002532 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002533 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2534 * account
2535 *
2536 * This function should be called from contexts where we cannot sleep, and will
2537 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002538 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002539void gpiod_set_value(struct gpio_desc *desc, int value)
2540{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002541 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01002542 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002543 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002544 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2545 value = !value;
2546 _gpiod_set_raw_value(desc, value);
2547}
2548EXPORT_SYMBOL_GPL(gpiod_set_value);
2549
2550/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002551 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002552 * @array_size: number of elements in the descriptor / value arrays
2553 * @desc_array: array of GPIO descriptors whose values will be assigned
2554 * @value_array: array of values to assign
2555 *
2556 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2557 * without regard for their ACTIVE_LOW status.
2558 *
2559 * This function should be called from contexts where we cannot sleep, and will
2560 * complain if the GPIO chip functions potentially sleep.
2561 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002562void gpiod_set_raw_array_value(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002563 struct gpio_desc **desc_array, int *value_array)
2564{
2565 if (!desc_array)
2566 return;
Linus Walleij44c7288f2016-04-24 11:36:59 +02002567 gpiod_set_array_value_complex(true, false, array_size, desc_array,
2568 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002569}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002570EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002571
2572/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002573 * gpiod_set_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002574 * @array_size: number of elements in the descriptor / value arrays
2575 * @desc_array: array of GPIO descriptors whose values will be assigned
2576 * @value_array: array of values to assign
2577 *
2578 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2579 * into account.
2580 *
2581 * This function should be called from contexts where we cannot sleep, and will
2582 * complain if the GPIO chip functions potentially sleep.
2583 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002584void gpiod_set_array_value(unsigned int array_size,
2585 struct gpio_desc **desc_array, int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002586{
2587 if (!desc_array)
2588 return;
Linus Walleij44c7288f2016-04-24 11:36:59 +02002589 gpiod_set_array_value_complex(false, false, array_size, desc_array,
2590 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002591}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002592EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002593
2594/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002595 * gpiod_cansleep() - report whether gpio value access may sleep
2596 * @desc: gpio to check
2597 *
2598 */
2599int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002600{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002601 VALIDATE_DESC(desc);
2602 return desc->gdev->chip->can_sleep;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002603}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002604EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002605
David Brownell0f6d5042008-10-15 22:03:14 -07002606/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002607 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
2608 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07002609 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002610 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
2611 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07002612 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002613int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07002614{
Linus Walleij4c37ce82016-05-02 13:13:10 +02002615 struct gpio_chip *chip;
2616 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07002617
Linus Walleij79bb71b2016-06-15 22:57:38 +02002618 /*
2619 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
2620 * requires this function to not return zero on an invalid descriptor
2621 * but rather a negative error number.
2622 */
Linus Walleijbfbbe442016-06-16 11:55:55 +02002623 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
Linus Walleij79bb71b2016-06-15 22:57:38 +02002624 return -EINVAL;
2625
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002626 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002627 offset = gpio_chip_hwgpio(desc);
Linus Walleij4c37ce82016-05-02 13:13:10 +02002628 if (chip->to_irq) {
2629 int retirq = chip->to_irq(chip, offset);
2630
2631 /* Zero means NO_IRQ */
2632 if (!retirq)
2633 return -ENXIO;
2634
2635 return retirq;
2636 }
2637 return -ENXIO;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002638}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002639EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002640
Linus Walleijd468bf92013-09-24 11:54:38 +02002641/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002642 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002643 * @chip: the chip the GPIO to lock belongs to
2644 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02002645 *
2646 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08002647 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08002648 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002649int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08002650{
Linus Walleij9c102802016-05-25 10:56:03 +02002651 struct gpio_desc *desc;
Linus Walleijd468bf92013-09-24 11:54:38 +02002652
Linus Walleij9c102802016-05-25 10:56:03 +02002653 desc = gpiochip_get_desc(chip, offset);
2654 if (IS_ERR(desc))
2655 return PTR_ERR(desc);
2656
2657 /* Flush direction if something changed behind our back */
2658 if (chip->get_direction) {
2659 int dir = chip->get_direction(chip, offset);
2660
2661 if (dir)
2662 clear_bit(FLAG_IS_OUT, &desc->flags);
2663 else
2664 set_bit(FLAG_IS_OUT, &desc->flags);
2665 }
2666
2667 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002668 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02002669 "%s: tried to flag a GPIO set as output for IRQ\n",
2670 __func__);
2671 return -EIO;
2672 }
2673
Linus Walleij9c102802016-05-25 10:56:03 +02002674 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002675 return 0;
2676}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002677EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02002678
Linus Walleijd468bf92013-09-24 11:54:38 +02002679/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002680 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002681 * @chip: the chip the GPIO to lock belongs to
2682 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02002683 *
2684 * This is used directly by GPIO drivers that want to indicate
2685 * that a certain GPIO is no longer used exclusively for IRQ.
2686 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002687void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02002688{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002689 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02002690 return;
2691
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002692 clear_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002693}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002694EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02002695
Linus Walleij6cee3822016-02-11 20:16:45 +01002696bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
2697{
2698 if (offset >= chip->ngpio)
2699 return false;
2700
2701 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
2702}
2703EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
2704
Linus Walleij143b65d2016-02-16 15:41:42 +01002705bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
2706{
2707 if (offset >= chip->ngpio)
2708 return false;
2709
2710 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
2711}
2712EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
2713
2714bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
2715{
2716 if (offset >= chip->ngpio)
2717 return false;
2718
2719 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
2720}
2721EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
2722
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002723/**
2724 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
2725 * @desc: gpio whose value will be returned
2726 *
2727 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002728 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002729 *
2730 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002731 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002732int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002733{
David Brownelld2876d02008-02-04 22:28:20 -08002734 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002735 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002736 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002737}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002738EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002739
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002740/**
2741 * gpiod_get_value_cansleep() - return a gpio's value
2742 * @desc: gpio whose value will be returned
2743 *
2744 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002745 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002746 *
2747 * This function is to be called from contexts that can sleep.
2748 */
2749int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002750{
David Brownelld2876d02008-02-04 22:28:20 -08002751 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002752
2753 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002754 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002755 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002756 if (value < 0)
2757 return value;
2758
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002759 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2760 value = !value;
2761
David Brownelld2876d02008-02-04 22:28:20 -08002762 return value;
2763}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002764EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002765
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002766/**
2767 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
2768 * @desc: gpio whose value will be assigned
2769 * @value: value to assign
2770 *
2771 * Set the raw value of the GPIO, i.e. the value of its physical line without
2772 * regard for its ACTIVE_LOW status.
2773 *
2774 * This function is to be called from contexts that can sleep.
2775 */
2776void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002777{
David Brownelld2876d02008-02-04 22:28:20 -08002778 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002779 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002780 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002781}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002782EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002783
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002784/**
2785 * gpiod_set_value_cansleep() - assign a gpio's value
2786 * @desc: gpio whose value will be assigned
2787 * @value: value to assign
2788 *
2789 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2790 * account
2791 *
2792 * This function is to be called from contexts that can sleep.
2793 */
2794void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002795{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002796 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002797 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002798 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2799 value = !value;
2800 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002801}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002802EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002803
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002804/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002805 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002806 * @array_size: number of elements in the descriptor / value arrays
2807 * @desc_array: array of GPIO descriptors whose values will be assigned
2808 * @value_array: array of values to assign
2809 *
2810 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2811 * without regard for their ACTIVE_LOW status.
2812 *
2813 * This function is to be called from contexts that can sleep.
2814 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002815void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
2816 struct gpio_desc **desc_array,
2817 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002818{
2819 might_sleep_if(extra_checks);
2820 if (!desc_array)
2821 return;
Linus Walleij44c7288f2016-04-24 11:36:59 +02002822 gpiod_set_array_value_complex(true, true, array_size, desc_array,
2823 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002824}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002825EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002826
2827/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002828 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002829 * @array_size: number of elements in the descriptor / value arrays
2830 * @desc_array: array of GPIO descriptors whose values will be assigned
2831 * @value_array: array of values to assign
2832 *
2833 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2834 * into account.
2835 *
2836 * This function is to be called from contexts that can sleep.
2837 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002838void gpiod_set_array_value_cansleep(unsigned int array_size,
2839 struct gpio_desc **desc_array,
2840 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002841{
2842 might_sleep_if(extra_checks);
2843 if (!desc_array)
2844 return;
Linus Walleij44c7288f2016-04-24 11:36:59 +02002845 gpiod_set_array_value_complex(false, true, array_size, desc_array,
2846 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002847}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002848EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002849
2850/**
Alexandre Courbotad824782013-12-03 12:20:11 +09002851 * gpiod_add_lookup_table() - register GPIO device consumers
2852 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002853 */
Alexandre Courbotad824782013-12-03 12:20:11 +09002854void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002855{
2856 mutex_lock(&gpio_lookup_lock);
2857
Alexandre Courbotad824782013-12-03 12:20:11 +09002858 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002859
2860 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08002861}
2862
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05302863/**
2864 * gpiod_remove_lookup_table() - unregister GPIO device consumers
2865 * @table: table of consumers to unregister
2866 */
2867void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
2868{
2869 mutex_lock(&gpio_lookup_lock);
2870
2871 list_del(&table->list);
2872
2873 mutex_unlock(&gpio_lookup_lock);
2874}
2875
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002876static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002877 unsigned int idx,
2878 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002879{
2880 char prop_name[32]; /* 32 is max size of property name */
2881 enum of_gpio_flags of_flags;
2882 struct gpio_desc *desc;
Thierry Redingdd34c372014-04-23 17:28:09 +02002883 unsigned int i;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002884
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002885 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Thierry Redingdd34c372014-04-23 17:28:09 +02002886 if (con_id)
Olliver Schinagl9e089242015-01-21 22:33:45 +01002887 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002888 gpio_suffixes[i]);
Thierry Redingdd34c372014-04-23 17:28:09 +02002889 else
Olliver Schinagl9e089242015-01-21 22:33:45 +01002890 snprintf(prop_name, sizeof(prop_name), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002891 gpio_suffixes[i]);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002892
Thierry Redingdd34c372014-04-23 17:28:09 +02002893 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
2894 &of_flags);
Lars-Peter Clausenda17f8a2016-07-01 17:40:09 +02002895 if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT))
Thierry Redingdd34c372014-04-23 17:28:09 +02002896 break;
2897 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002898
2899 if (IS_ERR(desc))
2900 return desc;
2901
2902 if (of_flags & OF_GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002903 *flags |= GPIO_ACTIVE_LOW;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002904
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002905 if (of_flags & OF_GPIO_SINGLE_ENDED) {
2906 if (of_flags & OF_GPIO_ACTIVE_LOW)
2907 *flags |= GPIO_OPEN_DRAIN;
2908 else
2909 *flags |= GPIO_OPEN_SOURCE;
2910 }
2911
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002912 return desc;
2913}
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002914
Dmitry Torokhov25487532016-03-24 10:50:25 -07002915static struct gpio_desc *acpi_find_gpio(struct device *dev,
2916 const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002917 unsigned int idx,
Dmitry Torokhov25487532016-03-24 10:50:25 -07002918 enum gpiod_flags flags,
2919 enum gpio_lookup_flags *lookupflags)
Mika Westerberg81f59e92013-10-10 11:01:09 +03002920{
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002921 struct acpi_device *adev = ACPI_COMPANION(dev);
Mika Westerberge01f4402013-10-10 11:01:10 +03002922 struct acpi_gpio_info info;
2923 struct gpio_desc *desc;
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002924 char propname[32];
2925 int i;
Mika Westerberge01f4402013-10-10 11:01:10 +03002926
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002927 /* Try first from _DSD */
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002928 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002929 if (con_id && strcmp(con_id, "gpios")) {
2930 snprintf(propname, sizeof(propname), "%s-%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002931 con_id, gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002932 } else {
2933 snprintf(propname, sizeof(propname), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002934 gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002935 }
Mika Westerberge01f4402013-10-10 11:01:10 +03002936
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002937 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
2938 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
2939 break;
2940 }
2941
2942 /* Then from plain _CRS GPIOs */
2943 if (IS_ERR(desc)) {
Dmitry Torokhov10cf4892015-11-11 11:45:30 -08002944 if (!acpi_can_fallback_to_crs(adev, con_id))
2945 return ERR_PTR(-ENOENT);
2946
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002947 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
2948 if (IS_ERR(desc))
2949 return desc;
Dmitry Torokhov25487532016-03-24 10:50:25 -07002950
2951 if ((flags == GPIOD_OUT_LOW || flags == GPIOD_OUT_HIGH) &&
2952 info.gpioint) {
2953 dev_dbg(dev, "refusing GpioInt() entry when doing GPIOD_OUT_* lookup\n");
2954 return ERR_PTR(-ENOENT);
2955 }
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002956 }
2957
Christophe RICARD52044722015-12-23 23:25:34 +01002958 if (info.polarity == GPIO_ACTIVE_LOW)
Dmitry Torokhov25487532016-03-24 10:50:25 -07002959 *lookupflags |= GPIO_ACTIVE_LOW;
Mika Westerberge01f4402013-10-10 11:01:10 +03002960
2961 return desc;
Mika Westerberg81f59e92013-10-10 11:01:09 +03002962}
2963
Alexandre Courbotad824782013-12-03 12:20:11 +09002964static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
2965{
2966 const char *dev_id = dev ? dev_name(dev) : NULL;
2967 struct gpiod_lookup_table *table;
2968
2969 mutex_lock(&gpio_lookup_lock);
2970
2971 list_for_each_entry(table, &gpio_lookup_list, list) {
2972 if (table->dev_id && dev_id) {
2973 /*
2974 * Valid strings on both ends, must be identical to have
2975 * a match
2976 */
2977 if (!strcmp(table->dev_id, dev_id))
2978 goto found;
2979 } else {
2980 /*
2981 * One of the pointers is NULL, so both must be to have
2982 * a match
2983 */
2984 if (dev_id == table->dev_id)
2985 goto found;
2986 }
2987 }
2988 table = NULL;
2989
2990found:
2991 mutex_unlock(&gpio_lookup_lock);
2992 return table;
2993}
2994
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002995static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002996 unsigned int idx,
2997 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002998{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002999 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09003000 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003001 struct gpiod_lookup *p;
3002
Alexandre Courbotad824782013-12-03 12:20:11 +09003003 table = gpiod_find_lookup_table(dev);
3004 if (!table)
3005 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003006
Alexandre Courbotad824782013-12-03 12:20:11 +09003007 for (p = &table->table[0]; p->chip_label; p++) {
3008 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003009
Alexandre Courbotad824782013-12-03 12:20:11 +09003010 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003011 if (p->idx != idx)
3012 continue;
3013
Alexandre Courbotad824782013-12-03 12:20:11 +09003014 /* If the lookup entry has a con_id, require exact match */
3015 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
3016 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003017
Alexandre Courbotad824782013-12-03 12:20:11 +09003018 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003019
Alexandre Courbotad824782013-12-03 12:20:11 +09003020 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003021 dev_err(dev, "cannot find GPIO chip %s\n",
3022 p->chip_label);
3023 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003024 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003025
Alexandre Courbotad824782013-12-03 12:20:11 +09003026 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003027 dev_err(dev,
3028 "requested GPIO %d is out of range [0..%d] for chip %s\n",
3029 idx, chip->ngpio, chip->label);
3030 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09003031 }
3032
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09003033 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09003034 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003035
3036 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09003037 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003038
3039 return desc;
3040}
3041
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003042static int dt_gpio_count(struct device *dev, const char *con_id)
3043{
3044 int ret;
3045 char propname[32];
3046 unsigned int i;
3047
3048 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
3049 if (con_id)
3050 snprintf(propname, sizeof(propname), "%s-%s",
3051 con_id, gpio_suffixes[i]);
3052 else
3053 snprintf(propname, sizeof(propname), "%s",
3054 gpio_suffixes[i]);
3055
3056 ret = of_gpio_named_count(dev->of_node, propname);
3057 if (ret >= 0)
3058 break;
3059 }
3060 return ret;
3061}
3062
3063static int platform_gpio_count(struct device *dev, const char *con_id)
3064{
3065 struct gpiod_lookup_table *table;
3066 struct gpiod_lookup *p;
3067 unsigned int count = 0;
3068
3069 table = gpiod_find_lookup_table(dev);
3070 if (!table)
3071 return -ENOENT;
3072
3073 for (p = &table->table[0]; p->chip_label; p++) {
3074 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3075 (!con_id && !p->con_id))
3076 count++;
3077 }
3078 if (!count)
3079 return -ENOENT;
3080
3081 return count;
3082}
3083
3084/**
3085 * gpiod_count - return the number of GPIOs associated with a device / function
3086 * or -ENOENT if no GPIO has been assigned to the requested function
3087 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3088 * @con_id: function within the GPIO consumer
3089 */
3090int gpiod_count(struct device *dev, const char *con_id)
3091{
3092 int count = -ENOENT;
3093
3094 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
3095 count = dt_gpio_count(dev, con_id);
3096 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
3097 count = acpi_gpio_count(dev, con_id);
3098
3099 if (count < 0)
3100 count = platform_gpio_count(dev, con_id);
3101
3102 return count;
3103}
3104EXPORT_SYMBOL_GPL(gpiod_count);
3105
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003106/**
Thierry Reding08791622014-04-25 16:54:22 +02003107 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09003108 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003109 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003110 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003111 *
3112 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003113 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07003114 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003115 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003116struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003117 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003118{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003119 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003120}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003121EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003122
3123/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02003124 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
3125 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3126 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003127 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02003128 *
3129 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
3130 * the requested function it will return NULL. This is convenient for drivers
3131 * that need to handle optional GPIOs.
3132 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003133struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003134 const char *con_id,
3135 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003136{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003137 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003138}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003139EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003140
Benoit Parrotf625d462015-02-02 11:44:44 -06003141
3142/**
3143 * gpiod_configure_flags - helper function to configure a given GPIO
3144 * @desc: gpio whose value will be assigned
3145 * @con_id: function within the GPIO consumer
Johan Hovold85b03b32016-07-03 18:32:05 +02003146 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3147 * of_get_gpio_hog()
Benoit Parrotf625d462015-02-02 11:44:44 -06003148 * @dflags: gpiod_flags - optional GPIO initialization flags
3149 *
3150 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
3151 * requested function and/or index, or another IS_ERR() code if an error
3152 * occurred while trying to acquire the GPIO.
3153 */
3154static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Johan Hovold85b03b32016-07-03 18:32:05 +02003155 unsigned long lflags, enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06003156{
3157 int status;
3158
Johan Hovold85b03b32016-07-03 18:32:05 +02003159 if (lflags & GPIO_ACTIVE_LOW)
3160 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
3161 if (lflags & GPIO_OPEN_DRAIN)
3162 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3163 if (lflags & GPIO_OPEN_SOURCE)
3164 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
3165
Benoit Parrotf625d462015-02-02 11:44:44 -06003166 /* No particular flag request, return here... */
3167 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
3168 pr_debug("no flags found for %s\n", con_id);
3169 return 0;
3170 }
3171
3172 /* Process flags */
3173 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
3174 status = gpiod_direction_output(desc,
3175 dflags & GPIOD_FLAGS_BIT_DIR_VAL);
3176 else
3177 status = gpiod_direction_input(desc);
3178
3179 return status;
3180}
3181
Thierry Reding29a1f2332014-04-25 17:10:06 +02003182/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003183 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02003184 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003185 * @con_id: function within the GPIO consumer
3186 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003187 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003188 *
3189 * This variant of gpiod_get() allows to access GPIOs other than the first
3190 * defined one for functions that define several GPIOs.
3191 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003192 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
3193 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07003194 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003195 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003196struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003197 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003198 unsigned int idx,
3199 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003200{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003201 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003202 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003203 enum gpio_lookup_flags lookupflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003204
3205 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
3206
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003207 if (dev) {
3208 /* Using device tree? */
3209 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
3210 dev_dbg(dev, "using device tree for GPIO lookup\n");
3211 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
3212 } else if (ACPI_COMPANION(dev)) {
3213 dev_dbg(dev, "using ACPI for GPIO lookup\n");
Dmitry Torokhov25487532016-03-24 10:50:25 -07003214 desc = acpi_find_gpio(dev, con_id, idx, flags, &lookupflags);
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003215 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003216 }
3217
3218 /*
3219 * Either we are not using DT or ACPI, or their lookup did not return
3220 * a result. In that case, use platform lookup as a fallback.
3221 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003222 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04003223 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003224 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003225 }
3226
3227 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02003228 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003229 return desc;
3230 }
3231
3232 status = gpiod_request(desc, con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003233 if (status < 0)
3234 return ERR_PTR(status);
3235
Johan Hovold85b03b32016-07-03 18:32:05 +02003236 status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003237 if (status < 0) {
3238 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
3239 gpiod_put(desc);
3240 return ERR_PTR(status);
3241 }
3242
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003243 return desc;
3244}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003245EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003246
3247/**
Mika Westerberg40b73182014-10-21 13:33:59 +02003248 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
3249 * @fwnode: handle of the firmware node
3250 * @propname: name of the firmware property representing the GPIO
3251 *
3252 * This function can be used for drivers that get their configuration
3253 * from firmware.
3254 *
3255 * Function properly finds the corresponding GPIO using whatever is the
3256 * underlying firmware interface and then makes sure that the GPIO
3257 * descriptor is requested before it is returned to the caller.
3258 *
3259 * In case of error an ERR_PTR() is returned.
3260 */
3261struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
3262 const char *propname)
3263{
3264 struct gpio_desc *desc = ERR_PTR(-ENODEV);
3265 bool active_low = false;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003266 bool single_ended = false;
Mika Westerberg40b73182014-10-21 13:33:59 +02003267 int ret;
3268
3269 if (!fwnode)
3270 return ERR_PTR(-EINVAL);
3271
3272 if (is_of_node(fwnode)) {
3273 enum of_gpio_flags flags;
3274
Alexander Sverdlinc181fb32015-06-22 22:38:53 +02003275 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, 0,
Mika Westerberg40b73182014-10-21 13:33:59 +02003276 &flags);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003277 if (!IS_ERR(desc)) {
Mika Westerberg40b73182014-10-21 13:33:59 +02003278 active_low = flags & OF_GPIO_ACTIVE_LOW;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003279 single_ended = flags & OF_GPIO_SINGLE_ENDED;
3280 }
Mika Westerberg40b73182014-10-21 13:33:59 +02003281 } else if (is_acpi_node(fwnode)) {
3282 struct acpi_gpio_info info;
3283
Rafael J. Wysocki504a3372015-08-27 04:42:33 +02003284 desc = acpi_node_get_gpiod(fwnode, propname, 0, &info);
Mika Westerberg40b73182014-10-21 13:33:59 +02003285 if (!IS_ERR(desc))
Christophe RICARD52044722015-12-23 23:25:34 +01003286 active_low = info.polarity == GPIO_ACTIVE_LOW;
Mika Westerberg40b73182014-10-21 13:33:59 +02003287 }
3288
3289 if (IS_ERR(desc))
3290 return desc;
3291
Johan Hovold85b03b32016-07-03 18:32:05 +02003292 ret = gpiod_request(desc, NULL);
3293 if (ret)
3294 return ERR_PTR(ret);
3295
Mika Westerberg40b73182014-10-21 13:33:59 +02003296 if (active_low)
3297 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
3298
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003299 if (single_ended) {
3300 if (active_low)
3301 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3302 else
3303 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
3304 }
3305
Mika Westerberg40b73182014-10-21 13:33:59 +02003306 return desc;
3307}
3308EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
3309
3310/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02003311 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
3312 * function
3313 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3314 * @con_id: function within the GPIO consumer
3315 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003316 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02003317 *
3318 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
3319 * specified index was assigned to the requested function it will return NULL.
3320 * This is convenient for drivers that need to handle optional GPIOs.
3321 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003322struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02003323 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003324 unsigned int index,
3325 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003326{
3327 struct gpio_desc *desc;
3328
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003329 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003330 if (IS_ERR(desc)) {
3331 if (PTR_ERR(desc) == -ENOENT)
3332 return NULL;
3333 }
3334
3335 return desc;
3336}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003337EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003338
3339/**
Benoit Parrotf625d462015-02-02 11:44:44 -06003340 * gpiod_hog - Hog the specified GPIO desc given the provided flags
3341 * @desc: gpio whose value will be assigned
3342 * @name: gpio line name
3343 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3344 * of_get_gpio_hog()
3345 * @dflags: gpiod_flags - optional GPIO initialization flags
3346 */
3347int gpiod_hog(struct gpio_desc *desc, const char *name,
3348 unsigned long lflags, enum gpiod_flags dflags)
3349{
3350 struct gpio_chip *chip;
3351 struct gpio_desc *local_desc;
3352 int hwnum;
3353 int status;
3354
3355 chip = gpiod_to_chip(desc);
3356 hwnum = gpio_chip_hwgpio(desc);
3357
3358 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
3359 if (IS_ERR(local_desc)) {
Laxman Dewanganc31a5712016-03-11 19:13:21 +05303360 status = PTR_ERR(local_desc);
3361 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
3362 name, chip->label, hwnum, status);
3363 return status;
Benoit Parrotf625d462015-02-02 11:44:44 -06003364 }
3365
Johan Hovold85b03b32016-07-03 18:32:05 +02003366 status = gpiod_configure_flags(desc, name, lflags, dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -06003367 if (status < 0) {
Laxman Dewanganc31a5712016-03-11 19:13:21 +05303368 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed, %d\n",
3369 name, chip->label, hwnum, status);
Benoit Parrotf625d462015-02-02 11:44:44 -06003370 gpiochip_free_own_desc(desc);
3371 return status;
3372 }
3373
3374 /* Mark GPIO as hogged so it can be identified and removed later */
3375 set_bit(FLAG_IS_HOGGED, &desc->flags);
3376
3377 pr_info("GPIO line %d (%s) hogged as %s%s\n",
3378 desc_to_gpio(desc), name,
3379 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
3380 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
3381 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
3382
3383 return 0;
3384}
3385
3386/**
3387 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
3388 * @chip: gpio chip to act on
3389 *
3390 * This is only used by of_gpiochip_remove to free hogged gpios
3391 */
3392static void gpiochip_free_hogs(struct gpio_chip *chip)
3393{
3394 int id;
3395
3396 for (id = 0; id < chip->ngpio; id++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01003397 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
3398 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
Benoit Parrotf625d462015-02-02 11:44:44 -06003399 }
3400}
3401
3402/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003403 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
3404 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3405 * @con_id: function within the GPIO consumer
3406 * @flags: optional GPIO initialization flags
3407 *
3408 * This function acquires all the GPIOs defined under a given function.
3409 *
3410 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
3411 * no GPIO has been assigned to the requested function, or another IS_ERR()
3412 * code if an error occurred while trying to acquire the GPIOs.
3413 */
3414struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
3415 const char *con_id,
3416 enum gpiod_flags flags)
3417{
3418 struct gpio_desc *desc;
3419 struct gpio_descs *descs;
3420 int count;
3421
3422 count = gpiod_count(dev, con_id);
3423 if (count < 0)
3424 return ERR_PTR(count);
3425
3426 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
3427 GFP_KERNEL);
3428 if (!descs)
3429 return ERR_PTR(-ENOMEM);
3430
3431 for (descs->ndescs = 0; descs->ndescs < count; ) {
3432 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
3433 if (IS_ERR(desc)) {
3434 gpiod_put_array(descs);
3435 return ERR_CAST(desc);
3436 }
3437 descs->desc[descs->ndescs] = desc;
3438 descs->ndescs++;
3439 }
3440 return descs;
3441}
3442EXPORT_SYMBOL_GPL(gpiod_get_array);
3443
3444/**
3445 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
3446 * function
3447 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3448 * @con_id: function within the GPIO consumer
3449 * @flags: optional GPIO initialization flags
3450 *
3451 * This is equivalent to gpiod_get_array(), except that when no GPIO was
3452 * assigned to the requested function it will return NULL.
3453 */
3454struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
3455 const char *con_id,
3456 enum gpiod_flags flags)
3457{
3458 struct gpio_descs *descs;
3459
3460 descs = gpiod_get_array(dev, con_id, flags);
3461 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
3462 return NULL;
3463
3464 return descs;
3465}
3466EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
3467
3468/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003469 * gpiod_put - dispose of a GPIO descriptor
3470 * @desc: GPIO descriptor to dispose of
3471 *
3472 * No descriptor can be used after gpiod_put() has been called on it.
3473 */
3474void gpiod_put(struct gpio_desc *desc)
3475{
3476 gpiod_free(desc);
3477}
3478EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08003479
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003480/**
3481 * gpiod_put_array - dispose of multiple GPIO descriptors
3482 * @descs: struct gpio_descs containing an array of descriptors
3483 */
3484void gpiod_put_array(struct gpio_descs *descs)
3485{
3486 unsigned int i;
3487
3488 for (i = 0; i < descs->ndescs; i++)
3489 gpiod_put(descs->desc[i]);
3490
3491 kfree(descs);
3492}
3493EXPORT_SYMBOL_GPL(gpiod_put_array);
3494
Linus Walleij3c702e92015-10-21 15:29:53 +02003495static int __init gpiolib_dev_init(void)
3496{
3497 int ret;
3498
3499 /* Register GPIO sysfs bus */
3500 ret = bus_register(&gpio_bus_type);
3501 if (ret < 0) {
3502 pr_err("gpiolib: could not register GPIO bus type\n");
3503 return ret;
3504 }
3505
3506 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
3507 if (ret < 0) {
3508 pr_err("gpiolib: failed to allocate char dev region\n");
3509 bus_unregister(&gpio_bus_type);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07003510 } else {
3511 gpiolib_initialized = true;
3512 gpiochip_setup_devs();
Linus Walleij3c702e92015-10-21 15:29:53 +02003513 }
3514 return ret;
3515}
3516core_initcall(gpiolib_dev_init);
3517
David Brownelld2876d02008-02-04 22:28:20 -08003518#ifdef CONFIG_DEBUG_FS
3519
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003520static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
David Brownelld2876d02008-02-04 22:28:20 -08003521{
3522 unsigned i;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003523 struct gpio_chip *chip = gdev->chip;
3524 unsigned gpio = gdev->base;
3525 struct gpio_desc *gdesc = &gdev->descs[0];
David Brownelld2876d02008-02-04 22:28:20 -08003526 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02003527 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08003528
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003529 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02003530 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
3531 if (gdesc->name) {
3532 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
3533 gpio, gdesc->name);
3534 }
David Brownelld2876d02008-02-04 22:28:20 -08003535 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02003536 }
David Brownelld2876d02008-02-04 22:28:20 -08003537
Alexandre Courbot372e7222013-02-03 01:29:29 +09003538 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08003539 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02003540 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Markus Pargmannced433e2015-08-14 16:11:02 +02003541 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
3542 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08003543 is_out ? "out" : "in ",
3544 chip->get
3545 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02003546 : "? ",
3547 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08003548 seq_printf(s, "\n");
3549 }
3550}
3551
Thierry Redingf9c4a312012-04-12 13:26:01 +02003552static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08003553{
Grant Likely362432a2013-02-09 09:41:49 +00003554 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02003555 struct gpio_device *gdev = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003556 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003557
3558 s->private = "";
3559
Grant Likely362432a2013-02-09 09:41:49 +00003560 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003561 list_for_each_entry(gdev, &gpio_devices, list)
Grant Likely362432a2013-02-09 09:41:49 +00003562 if (index-- == 0) {
3563 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003564 return gdev;
Grant Likely362432a2013-02-09 09:41:49 +00003565 }
3566 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003567
3568 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003569}
3570
3571static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
3572{
Grant Likely362432a2013-02-09 09:41:49 +00003573 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02003574 struct gpio_device *gdev = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003575 void *ret = NULL;
3576
Grant Likely362432a2013-02-09 09:41:49 +00003577 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003578 if (list_is_last(&gdev->list, &gpio_devices))
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003579 ret = NULL;
3580 else
Linus Walleijff2b1352015-10-20 11:10:38 +02003581 ret = list_entry(gdev->list.next, struct gpio_device, list);
Grant Likely362432a2013-02-09 09:41:49 +00003582 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02003583
3584 s->private = "\n";
3585 ++*pos;
3586
3587 return ret;
3588}
3589
3590static void gpiolib_seq_stop(struct seq_file *s, void *v)
3591{
3592}
3593
3594static int gpiolib_seq_show(struct seq_file *s, void *v)
3595{
Linus Walleijff2b1352015-10-20 11:10:38 +02003596 struct gpio_device *gdev = v;
3597 struct gpio_chip *chip = gdev->chip;
3598 struct device *parent;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003599
Linus Walleijff2b1352015-10-20 11:10:38 +02003600 if (!chip) {
3601 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
3602 dev_name(&gdev->dev));
3603 return 0;
3604 }
3605
3606 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
3607 dev_name(&gdev->dev),
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003608 gdev->base, gdev->base + gdev->ngpio - 1);
Linus Walleijff2b1352015-10-20 11:10:38 +02003609 parent = chip->parent;
3610 if (parent)
3611 seq_printf(s, ", parent: %s/%s",
3612 parent->bus ? parent->bus->name : "no-bus",
3613 dev_name(parent));
Thierry Redingf9c4a312012-04-12 13:26:01 +02003614 if (chip->label)
3615 seq_printf(s, ", %s", chip->label);
3616 if (chip->can_sleep)
3617 seq_printf(s, ", can sleep");
3618 seq_printf(s, ":\n");
3619
3620 if (chip->dbg_show)
3621 chip->dbg_show(s, chip);
3622 else
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003623 gpiolib_dbg_show(s, gdev);
Thierry Redingf9c4a312012-04-12 13:26:01 +02003624
David Brownelld2876d02008-02-04 22:28:20 -08003625 return 0;
3626}
3627
Thierry Redingf9c4a312012-04-12 13:26:01 +02003628static const struct seq_operations gpiolib_seq_ops = {
3629 .start = gpiolib_seq_start,
3630 .next = gpiolib_seq_next,
3631 .stop = gpiolib_seq_stop,
3632 .show = gpiolib_seq_show,
3633};
3634
David Brownelld2876d02008-02-04 22:28:20 -08003635static int gpiolib_open(struct inode *inode, struct file *file)
3636{
Thierry Redingf9c4a312012-04-12 13:26:01 +02003637 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08003638}
3639
Alexey Dobriyan828c0952009-10-01 15:43:56 -07003640static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02003641 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08003642 .open = gpiolib_open,
3643 .read = seq_read,
3644 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02003645 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08003646};
3647
3648static int __init gpiolib_debugfs_init(void)
3649{
3650 /* /sys/kernel/debug/gpio */
3651 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
3652 NULL, NULL, &gpiolib_operations);
3653 return 0;
3654}
3655subsys_initcall(gpiolib_debugfs_init);
3656
3657#endif /* DEBUG_FS */