blob: b5b1a842590707e09bed04c1b266578254ab0beb [file] [log] [blame]
David Brownelld2876d02008-02-04 22:28:20 -08001#include <linux/kernel.h>
2#include <linux/module.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -07003#include <linux/interrupt.h>
David Brownelld2876d02008-02-04 22:28:20 -08004#include <linux/irq.h>
5#include <linux/spinlock.h>
Alexandre Courbot1a989d02013-02-03 01:29:24 +09006#include <linux/list.h>
David Brownelld8f388d82008-07-25 01:46:07 -07007#include <linux/device.h>
8#include <linux/err.h>
9#include <linux/debugfs.h>
10#include <linux/seq_file.h>
11#include <linux/gpio.h>
Anton Vorontsov391c9702010-06-08 07:48:17 -060012#include <linux/of_gpio.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -070013#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Rafael J. Wysocki7b199812013-11-11 22:41:56 +010015#include <linux/acpi.h>
Alexandre Courbot53e7cac2013-11-16 21:44:52 +090016#include <linux/gpio/driver.h>
Linus Walleij0a6d3152014-07-24 20:08:55 +020017#include <linux/gpio/machine.h>
Jonas Gorskic771c2f2015-10-11 17:34:15 +020018#include <linux/pinctrl/consumer.h>
Linus Walleij3c702e92015-10-21 15:29:53 +020019#include <linux/cdev.h>
20#include <linux/fs.h>
21#include <linux/uaccess.h>
Linus Walleij8b92e172016-05-27 14:24:04 +020022#include <linux/compat.h>
Linus Walleijd7c51b42016-04-26 10:35:29 +020023#include <linux/anon_inodes.h>
Linus Walleij61f922d2016-06-02 11:30:15 +020024#include <linux/kfifo.h>
25#include <linux/poll.h>
26#include <linux/timekeeping.h>
Linus Walleij3c702e92015-10-21 15:29:53 +020027#include <uapi/linux/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080028
Mika Westerberg664e3e52014-01-08 12:40:54 +020029#include "gpiolib.h"
30
Uwe Kleine-König3f397c212011-05-20 00:40:19 -060031#define CREATE_TRACE_POINTS
32#include <trace/events/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080033
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070034/* Implementation infrastructure for GPIO interfaces.
David Brownelld2876d02008-02-04 22:28:20 -080035 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070036 * The GPIO programming interface allows for inlining speed-critical
37 * get/set operations for common cases, so that access to SOC-integrated
38 * GPIOs can sometimes cost only an instruction or two per bit.
David Brownelld2876d02008-02-04 22:28:20 -080039 */
40
41
42/* When debugging, extend minimal trust to callers and platform code.
43 * Also emit diagnostic messages that may help initial bringup, when
44 * board setup or driver bugs are most common.
45 *
46 * Otherwise, minimize overhead in what may be bitbanging codepaths.
47 */
48#ifdef DEBUG
49#define extra_checks 1
50#else
51#define extra_checks 0
52#endif
53
Linus Walleijff2b1352015-10-20 11:10:38 +020054/* Device and char device-related information */
55static DEFINE_IDA(gpio_ida);
Linus Walleij3c702e92015-10-21 15:29:53 +020056static dev_t gpio_devt;
57#define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
58static struct bus_type gpio_bus_type = {
59 .name = "gpio",
60};
Linus Walleijff2b1352015-10-20 11:10:38 +020061
David Brownelld2876d02008-02-04 22:28:20 -080062/* gpio_lock prevents conflicts during gpio_desc[] table updates.
63 * While any GPIO is requested, its gpio_chip is not removable;
64 * each GPIO's "requested" flag serves as a lock and refcount.
65 */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090066DEFINE_SPINLOCK(gpio_lock);
David Brownelld2876d02008-02-04 22:28:20 -080067
Alexandre Courbotbae48da2013-10-17 10:21:38 -070068static DEFINE_MUTEX(gpio_lookup_lock);
69static LIST_HEAD(gpio_lookup_list);
Linus Walleijff2b1352015-10-20 11:10:38 +020070LIST_HEAD(gpio_devices);
Johan Hovold6d867502015-05-04 17:23:25 +020071
72static void gpiochip_free_hogs(struct gpio_chip *chip);
73static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
Mika Westerberg79b804c2016-09-20 15:15:21 +030074static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip);
75static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip);
Johan Hovold6d867502015-05-04 17:23:25 +020076
Guenter Roeck159f3cd2016-03-31 08:11:30 -070077static bool gpiolib_initialized;
Johan Hovold6d867502015-05-04 17:23:25 +020078
David Brownelld2876d02008-02-04 22:28:20 -080079static inline void desc_set_label(struct gpio_desc *d, const char *label)
80{
David Brownelld2876d02008-02-04 22:28:20 -080081 d->label = label;
David Brownelld2876d02008-02-04 22:28:20 -080082}
83
Alexandre Courbot372e7222013-02-03 01:29:29 +090084/**
85 * Convert a GPIO number to its descriptor
86 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070087struct gpio_desc *gpio_to_desc(unsigned gpio)
Alexandre Courbot372e7222013-02-03 01:29:29 +090088{
Linus Walleijff2b1352015-10-20 11:10:38 +020089 struct gpio_device *gdev;
Alexandre Courbot14e85c02014-11-19 16:51:27 +090090 unsigned long flags;
91
92 spin_lock_irqsave(&gpio_lock, flags);
93
Linus Walleijff2b1352015-10-20 11:10:38 +020094 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +010095 if (gdev->base <= gpio &&
96 gdev->base + gdev->ngpio > gpio) {
Alexandre Courbot14e85c02014-11-19 16:51:27 +090097 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +010098 return &gdev->descs[gpio - gdev->base];
Alexandre Courbot14e85c02014-11-19 16:51:27 +090099 }
100 }
101
102 spin_unlock_irqrestore(&gpio_lock, flags);
103
Alexandre Courbot0e9a5ed2014-12-02 23:15:05 +0900104 if (!gpio_is_valid(gpio))
105 WARN(1, "invalid GPIO %d\n", gpio);
106
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900107 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900108}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700109EXPORT_SYMBOL_GPL(gpio_to_desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900110
111/**
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900112 * Get the GPIO descriptor corresponding to the given hw number for this chip.
Linus Walleijd468bf92013-09-24 11:54:38 +0200113 */
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900114struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
115 u16 hwnum)
Linus Walleijd468bf92013-09-24 11:54:38 +0200116{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100117 struct gpio_device *gdev = chip->gpiodev;
118
119 if (hwnum >= gdev->ngpio)
Alexandre Courbotb7d0a282013-12-03 12:31:11 +0900120 return ERR_PTR(-EINVAL);
Linus Walleijd468bf92013-09-24 11:54:38 +0200121
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100122 return &gdev->descs[hwnum];
Linus Walleijd468bf92013-09-24 11:54:38 +0200123}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900124
125/**
126 * Convert a GPIO descriptor to the integer namespace.
127 * This should disappear in the future but is needed since we still
128 * use GPIO numbers for error messages and sysfs nodes
129 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700130int desc_to_gpio(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900131{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100132 return desc->gdev->base + (desc - &desc->gdev->descs[0]);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900133}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700134EXPORT_SYMBOL_GPL(desc_to_gpio);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900135
136
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700137/**
138 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
139 * @desc: descriptor to return the chip of
140 */
141struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900142{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100143 if (!desc || !desc->gdev || !desc->gdev->chip)
144 return NULL;
145 return desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900146}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700147EXPORT_SYMBOL_GPL(gpiod_to_chip);
David Brownelld2876d02008-02-04 22:28:20 -0800148
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700149/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
150static int gpiochip_find_base(int ngpio)
151{
Linus Walleijff2b1352015-10-20 11:10:38 +0200152 struct gpio_device *gdev;
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900153 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700154
Linus Walleijff2b1352015-10-20 11:10:38 +0200155 list_for_each_entry_reverse(gdev, &gpio_devices, list) {
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900156 /* found a free space? */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100157 if (gdev->base + gdev->ngpio <= base)
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900158 break;
159 else
160 /* nope, check the space right before the chip */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100161 base = gdev->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700162 }
163
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900164 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700165 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900166 return base;
167 } else {
168 pr_err("%s: cannot find free range\n", __func__);
169 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700170 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700171}
172
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700173/**
174 * gpiod_get_direction - return the current direction of a GPIO
175 * @desc: GPIO to get the direction of
176 *
177 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
178 *
179 * This function may sleep if gpiod_cansleep() is true.
180 */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900181int gpiod_get_direction(struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300182{
183 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900184 unsigned offset;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300185 int status = -EINVAL;
186
Alexandre Courbot372e7222013-02-03 01:29:29 +0900187 chip = gpiod_to_chip(desc);
188 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300189
190 if (!chip->get_direction)
191 return status;
192
Alexandre Courbot372e7222013-02-03 01:29:29 +0900193 status = chip->get_direction(chip, offset);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300194 if (status > 0) {
195 /* GPIOF_DIR_IN, or other positive */
196 status = 1;
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900197 clear_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300198 }
199 if (status == 0) {
200 /* GPIOF_DIR_OUT */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900201 set_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300202 }
203 return status;
204}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700205EXPORT_SYMBOL_GPL(gpiod_get_direction);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300206
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900207/*
208 * Add a new chip to the global chips list, keeping the list of chips sorted
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800209 * by range(means [base, base + ngpio - 1]) order.
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900210 *
211 * Return -EBUSY if the new chip overlaps with some other chip's integer
212 * space.
213 */
Linus Walleijff2b1352015-10-20 11:10:38 +0200214static int gpiodev_add_to_list(struct gpio_device *gdev)
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900215{
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800216 struct gpio_device *prev, *next;
Linus Walleijff2b1352015-10-20 11:10:38 +0200217
218 if (list_empty(&gpio_devices)) {
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800219 /* initial entry in list */
Linus Walleijff2b1352015-10-20 11:10:38 +0200220 list_add_tail(&gdev->list, &gpio_devices);
Sudip Mukherjeee28ecca2015-12-27 19:06:50 +0530221 return 0;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900222 }
223
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800224 next = list_entry(gpio_devices.next, struct gpio_device, list);
225 if (gdev->base + gdev->ngpio <= next->base) {
226 /* add before first entry */
227 list_add(&gdev->list, &gpio_devices);
Julien Grossholtz96098df2016-01-07 16:46:45 -0500228 return 0;
229 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900230
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800231 prev = list_entry(gpio_devices.prev, struct gpio_device, list);
232 if (prev->base + prev->ngpio <= gdev->base) {
233 /* add behind last entry */
234 list_add_tail(&gdev->list, &gpio_devices);
235 return 0;
236 }
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800237
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800238 list_for_each_entry_safe(prev, next, &gpio_devices, list) {
239 /* at the end of the list */
240 if (&next->list == &gpio_devices)
241 break;
242
243 /* add between prev and next */
244 if (prev->base + prev->ngpio <= gdev->base
245 && gdev->base + gdev->ngpio <= next->base) {
246 list_add(&gdev->list, &prev->list);
247 return 0;
248 }
249 }
250
251 dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n");
252 return -EBUSY;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900253}
254
Linus Walleijf881bab02015-09-23 16:20:43 -0700255/**
256 * Convert a GPIO name to its descriptor
257 */
258static struct gpio_desc *gpio_name_to_desc(const char * const name)
259{
Linus Walleijff2b1352015-10-20 11:10:38 +0200260 struct gpio_device *gdev;
Linus Walleijf881bab02015-09-23 16:20:43 -0700261 unsigned long flags;
262
263 spin_lock_irqsave(&gpio_lock, flags);
264
Linus Walleijff2b1352015-10-20 11:10:38 +0200265 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijf881bab02015-09-23 16:20:43 -0700266 int i;
267
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100268 for (i = 0; i != gdev->ngpio; ++i) {
269 struct gpio_desc *desc = &gdev->descs[i];
Linus Walleijf881bab02015-09-23 16:20:43 -0700270
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100271 if (!desc->name || !name)
Linus Walleijf881bab02015-09-23 16:20:43 -0700272 continue;
273
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100274 if (!strcmp(desc->name, name)) {
Linus Walleijf881bab02015-09-23 16:20:43 -0700275 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100276 return desc;
Linus Walleijf881bab02015-09-23 16:20:43 -0700277 }
278 }
279 }
280
281 spin_unlock_irqrestore(&gpio_lock, flags);
282
283 return NULL;
284}
285
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200286/*
287 * Takes the names from gc->names and checks if they are all unique. If they
288 * are, they are assigned to their gpio descriptors.
289 *
Bamvor Jian Zhanged379152015-11-14 16:43:20 +0800290 * Warning if one of the names is already used for a different GPIO.
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200291 */
292static int gpiochip_set_desc_names(struct gpio_chip *gc)
293{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100294 struct gpio_device *gdev = gc->gpiodev;
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200295 int i;
296
297 if (!gc->names)
298 return 0;
299
300 /* First check all names if they are unique */
301 for (i = 0; i != gc->ngpio; ++i) {
302 struct gpio_desc *gpio;
303
304 gpio = gpio_name_to_desc(gc->names[i]);
Linus Walleijf881bab02015-09-23 16:20:43 -0700305 if (gpio)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100306 dev_warn(&gdev->dev,
Linus Walleij34ffd852015-10-20 11:31:54 +0200307 "Detected name collision for GPIO name '%s'\n",
Linus Walleijf881bab02015-09-23 16:20:43 -0700308 gc->names[i]);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200309 }
310
311 /* Then add all names to the GPIO descriptors */
312 for (i = 0; i != gc->ngpio; ++i)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100313 gdev->descs[i].name = gc->names[i];
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200314
315 return 0;
316}
317
Linus Walleijd7c51b42016-04-26 10:35:29 +0200318/*
319 * GPIO line handle management
320 */
321
322/**
323 * struct linehandle_state - contains the state of a userspace handle
324 * @gdev: the GPIO device the handle pertains to
325 * @label: consumer label used to tag descriptors
326 * @descs: the GPIO descriptors held by this handle
327 * @numdescs: the number of descriptors held in the descs array
328 */
329struct linehandle_state {
330 struct gpio_device *gdev;
331 const char *label;
332 struct gpio_desc *descs[GPIOHANDLES_MAX];
333 u32 numdescs;
334};
335
Lars-Peter Clausene3e847c2016-10-18 16:54:05 +0200336#define GPIOHANDLE_REQUEST_VALID_FLAGS \
337 (GPIOHANDLE_REQUEST_INPUT | \
338 GPIOHANDLE_REQUEST_OUTPUT | \
339 GPIOHANDLE_REQUEST_ACTIVE_LOW | \
340 GPIOHANDLE_REQUEST_OPEN_DRAIN | \
341 GPIOHANDLE_REQUEST_OPEN_SOURCE)
342
Linus Walleijd7c51b42016-04-26 10:35:29 +0200343static long linehandle_ioctl(struct file *filep, unsigned int cmd,
344 unsigned long arg)
345{
346 struct linehandle_state *lh = filep->private_data;
347 void __user *ip = (void __user *)arg;
348 struct gpiohandle_data ghd;
349 int i;
350
351 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
352 int val;
353
Lars-Peter Clausen3eded5d2016-10-18 16:54:02 +0200354 memset(&ghd, 0, sizeof(ghd));
355
Linus Walleijd7c51b42016-04-26 10:35:29 +0200356 /* TODO: check if descriptors are really input */
357 for (i = 0; i < lh->numdescs; i++) {
358 val = gpiod_get_value_cansleep(lh->descs[i]);
359 if (val < 0)
360 return val;
361 ghd.values[i] = val;
362 }
363
364 if (copy_to_user(ip, &ghd, sizeof(ghd)))
365 return -EFAULT;
366
367 return 0;
368 } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
369 int vals[GPIOHANDLES_MAX];
370
371 /* TODO: check if descriptors are really output */
372 if (copy_from_user(&ghd, ip, sizeof(ghd)))
373 return -EFAULT;
374
375 /* Clamp all values to [0,1] */
376 for (i = 0; i < lh->numdescs; i++)
377 vals[i] = !!ghd.values[i];
378
379 /* Reuse the array setting function */
380 gpiod_set_array_value_complex(false,
381 true,
382 lh->numdescs,
383 lh->descs,
384 vals);
385 return 0;
386 }
387 return -EINVAL;
388}
389
390#ifdef CONFIG_COMPAT
391static long linehandle_ioctl_compat(struct file *filep, unsigned int cmd,
392 unsigned long arg)
393{
394 return linehandle_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
395}
396#endif
397
398static int linehandle_release(struct inode *inode, struct file *filep)
399{
400 struct linehandle_state *lh = filep->private_data;
401 struct gpio_device *gdev = lh->gdev;
402 int i;
403
404 for (i = 0; i < lh->numdescs; i++)
405 gpiod_free(lh->descs[i]);
406 kfree(lh->label);
407 kfree(lh);
408 put_device(&gdev->dev);
409 return 0;
410}
411
412static const struct file_operations linehandle_fileops = {
413 .release = linehandle_release,
414 .owner = THIS_MODULE,
415 .llseek = noop_llseek,
416 .unlocked_ioctl = linehandle_ioctl,
417#ifdef CONFIG_COMPAT
418 .compat_ioctl = linehandle_ioctl_compat,
419#endif
420};
421
422static int linehandle_create(struct gpio_device *gdev, void __user *ip)
423{
424 struct gpiohandle_request handlereq;
425 struct linehandle_state *lh;
426 int fd, i, ret;
427
428 if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
429 return -EFAULT;
430 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
431 return -EINVAL;
432
433 lh = kzalloc(sizeof(*lh), GFP_KERNEL);
434 if (!lh)
435 return -ENOMEM;
436 lh->gdev = gdev;
437 get_device(&gdev->dev);
438
439 /* Make sure this is terminated */
440 handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
441 if (strlen(handlereq.consumer_label)) {
442 lh->label = kstrdup(handlereq.consumer_label,
443 GFP_KERNEL);
444 if (!lh->label) {
445 ret = -ENOMEM;
446 goto out_free_lh;
447 }
448 }
449
450 /* Request each GPIO */
451 for (i = 0; i < handlereq.lines; i++) {
452 u32 offset = handlereq.lineoffsets[i];
453 u32 lflags = handlereq.flags;
454 struct gpio_desc *desc;
455
Lars-Peter Clausene405f9f2016-10-18 16:54:01 +0200456 if (offset >= gdev->ngpio) {
457 ret = -EINVAL;
458 goto out_free_descs;
459 }
460
Lars-Peter Clausene3e847c2016-10-18 16:54:05 +0200461 /* Return an error if a unknown flag is set */
462 if (lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) {
463 ret = -EINVAL;
464 goto out_free_descs;
465 }
466
Linus Walleijd7c51b42016-04-26 10:35:29 +0200467 desc = &gdev->descs[offset];
468 ret = gpiod_request(desc, lh->label);
469 if (ret)
470 goto out_free_descs;
471 lh->descs[i] = desc;
472
473 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
474 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
475 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
476 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
477 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
478 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
479
480 /*
481 * Lines have to be requested explicitly for input
482 * or output, else the line will be treated "as is".
483 */
484 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
485 int val = !!handlereq.default_values[i];
486
487 ret = gpiod_direction_output(desc, val);
488 if (ret)
489 goto out_free_descs;
490 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
491 ret = gpiod_direction_input(desc);
492 if (ret)
493 goto out_free_descs;
494 }
495 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
496 offset);
497 }
Linus Walleije2f608b2016-06-18 10:56:43 +0200498 /* Let i point at the last handle */
499 i--;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200500 lh->numdescs = handlereq.lines;
501
502 fd = anon_inode_getfd("gpio-linehandle",
503 &linehandle_fileops,
504 lh,
505 O_RDONLY | O_CLOEXEC);
506 if (fd < 0) {
507 ret = fd;
508 goto out_free_descs;
509 }
510
511 handlereq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200512 if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
513 ret = -EFAULT;
514 goto out_free_descs;
515 }
Linus Walleijd7c51b42016-04-26 10:35:29 +0200516
517 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
518 lh->numdescs);
519
520 return 0;
521
522out_free_descs:
523 for (; i >= 0; i--)
524 gpiod_free(lh->descs[i]);
525 kfree(lh->label);
526out_free_lh:
527 kfree(lh);
528 put_device(&gdev->dev);
529 return ret;
530}
531
Linus Walleij61f922d2016-06-02 11:30:15 +0200532/*
533 * GPIO line event management
534 */
535
536/**
537 * struct lineevent_state - contains the state of a userspace event
538 * @gdev: the GPIO device the event pertains to
539 * @label: consumer label used to tag descriptors
540 * @desc: the GPIO descriptor held by this event
541 * @eflags: the event flags this line was requested with
542 * @irq: the interrupt that trigger in response to events on this GPIO
543 * @wait: wait queue that handles blocking reads of events
544 * @events: KFIFO for the GPIO events
545 * @read_lock: mutex lock to protect reads from colliding with adding
546 * new events to the FIFO
547 */
548struct lineevent_state {
549 struct gpio_device *gdev;
550 const char *label;
551 struct gpio_desc *desc;
552 u32 eflags;
553 int irq;
554 wait_queue_head_t wait;
555 DECLARE_KFIFO(events, struct gpioevent_data, 16);
556 struct mutex read_lock;
557};
558
559static unsigned int lineevent_poll(struct file *filep,
560 struct poll_table_struct *wait)
561{
562 struct lineevent_state *le = filep->private_data;
563 unsigned int events = 0;
564
565 poll_wait(filep, &le->wait, wait);
566
567 if (!kfifo_is_empty(&le->events))
568 events = POLLIN | POLLRDNORM;
569
570 return events;
571}
572
573
574static ssize_t lineevent_read(struct file *filep,
575 char __user *buf,
576 size_t count,
577 loff_t *f_ps)
578{
579 struct lineevent_state *le = filep->private_data;
580 unsigned int copied;
581 int ret;
582
583 if (count < sizeof(struct gpioevent_data))
584 return -EINVAL;
585
586 do {
587 if (kfifo_is_empty(&le->events)) {
588 if (filep->f_flags & O_NONBLOCK)
589 return -EAGAIN;
590
591 ret = wait_event_interruptible(le->wait,
592 !kfifo_is_empty(&le->events));
593 if (ret)
594 return ret;
595 }
596
597 if (mutex_lock_interruptible(&le->read_lock))
598 return -ERESTARTSYS;
599 ret = kfifo_to_user(&le->events, buf, count, &copied);
600 mutex_unlock(&le->read_lock);
601
602 if (ret)
603 return ret;
604
605 /*
606 * If we couldn't read anything from the fifo (a different
607 * thread might have been faster) we either return -EAGAIN if
608 * the file descriptor is non-blocking, otherwise we go back to
609 * sleep and wait for more data to arrive.
610 */
611 if (copied == 0 && (filep->f_flags & O_NONBLOCK))
612 return -EAGAIN;
613
614 } while (copied == 0);
615
616 return copied;
617}
618
619static int lineevent_release(struct inode *inode, struct file *filep)
620{
621 struct lineevent_state *le = filep->private_data;
622 struct gpio_device *gdev = le->gdev;
623
624 free_irq(le->irq, le);
625 gpiod_free(le->desc);
626 kfree(le->label);
627 kfree(le);
628 put_device(&gdev->dev);
629 return 0;
630}
631
632static long lineevent_ioctl(struct file *filep, unsigned int cmd,
633 unsigned long arg)
634{
635 struct lineevent_state *le = filep->private_data;
636 void __user *ip = (void __user *)arg;
637 struct gpiohandle_data ghd;
638
639 /*
640 * We can get the value for an event line but not set it,
641 * because it is input by definition.
642 */
643 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
644 int val;
645
Lars-Peter Clausend82aa4a2016-10-18 16:54:04 +0200646 memset(&ghd, 0, sizeof(ghd));
647
Linus Walleij61f922d2016-06-02 11:30:15 +0200648 val = gpiod_get_value_cansleep(le->desc);
649 if (val < 0)
650 return val;
651 ghd.values[0] = val;
652
653 if (copy_to_user(ip, &ghd, sizeof(ghd)))
654 return -EFAULT;
655
656 return 0;
657 }
658 return -EINVAL;
659}
660
661#ifdef CONFIG_COMPAT
662static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd,
663 unsigned long arg)
664{
665 return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
666}
667#endif
668
669static const struct file_operations lineevent_fileops = {
670 .release = lineevent_release,
671 .read = lineevent_read,
672 .poll = lineevent_poll,
673 .owner = THIS_MODULE,
674 .llseek = noop_llseek,
675 .unlocked_ioctl = lineevent_ioctl,
676#ifdef CONFIG_COMPAT
677 .compat_ioctl = lineevent_ioctl_compat,
678#endif
679};
680
Ben Dooks33265b12016-06-17 16:03:13 +0100681static irqreturn_t lineevent_irq_thread(int irq, void *p)
Linus Walleij61f922d2016-06-02 11:30:15 +0200682{
683 struct lineevent_state *le = p;
684 struct gpioevent_data ge;
685 int ret;
686
687 ge.timestamp = ktime_get_real_ns();
688
689 if (le->eflags & GPIOEVENT_REQUEST_BOTH_EDGES) {
690 int level = gpiod_get_value_cansleep(le->desc);
691
692 if (level)
693 /* Emit low-to-high event */
694 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
695 else
696 /* Emit high-to-low event */
697 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
698 } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
699 /* Emit low-to-high event */
700 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
701 } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
702 /* Emit high-to-low event */
703 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
Arnd Bergmannbc0207a2016-06-16 11:02:41 +0200704 } else {
705 return IRQ_NONE;
Linus Walleij61f922d2016-06-02 11:30:15 +0200706 }
707
708 ret = kfifo_put(&le->events, ge);
709 if (ret != 0)
710 wake_up_poll(&le->wait, POLLIN);
711
712 return IRQ_HANDLED;
713}
714
715static int lineevent_create(struct gpio_device *gdev, void __user *ip)
716{
717 struct gpioevent_request eventreq;
718 struct lineevent_state *le;
719 struct gpio_desc *desc;
720 u32 offset;
721 u32 lflags;
722 u32 eflags;
723 int fd;
724 int ret;
725 int irqflags = 0;
726
727 if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
728 return -EFAULT;
729
730 le = kzalloc(sizeof(*le), GFP_KERNEL);
731 if (!le)
732 return -ENOMEM;
733 le->gdev = gdev;
734 get_device(&gdev->dev);
735
736 /* Make sure this is terminated */
737 eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
738 if (strlen(eventreq.consumer_label)) {
739 le->label = kstrdup(eventreq.consumer_label,
740 GFP_KERNEL);
741 if (!le->label) {
742 ret = -ENOMEM;
743 goto out_free_le;
744 }
745 }
746
747 offset = eventreq.lineoffset;
748 lflags = eventreq.handleflags;
749 eflags = eventreq.eventflags;
750
Lars-Peter Clausenb8b0e3d2016-10-18 16:54:03 +0200751 if (offset >= gdev->ngpio) {
752 ret = -EINVAL;
753 goto out_free_label;
754 }
755
Linus Walleij61f922d2016-06-02 11:30:15 +0200756 /* This is just wrong: we don't look for events on output lines */
757 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
758 ret = -EINVAL;
759 goto out_free_label;
760 }
761
762 desc = &gdev->descs[offset];
763 ret = gpiod_request(desc, le->label);
764 if (ret)
765 goto out_free_desc;
766 le->desc = desc;
767 le->eflags = eflags;
768
769 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
770 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
771 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
772 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
773 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
774 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
775
776 ret = gpiod_direction_input(desc);
777 if (ret)
778 goto out_free_desc;
779
780 le->irq = gpiod_to_irq(desc);
781 if (le->irq <= 0) {
782 ret = -ENODEV;
783 goto out_free_desc;
784 }
785
786 if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
787 irqflags |= IRQF_TRIGGER_RISING;
788 if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
789 irqflags |= IRQF_TRIGGER_FALLING;
790 irqflags |= IRQF_ONESHOT;
791 irqflags |= IRQF_SHARED;
792
793 INIT_KFIFO(le->events);
794 init_waitqueue_head(&le->wait);
795 mutex_init(&le->read_lock);
796
797 /* Request a thread to read the events */
798 ret = request_threaded_irq(le->irq,
799 NULL,
800 lineevent_irq_thread,
801 irqflags,
802 le->label,
803 le);
804 if (ret)
805 goto out_free_desc;
806
807 fd = anon_inode_getfd("gpio-event",
808 &lineevent_fileops,
809 le,
810 O_RDONLY | O_CLOEXEC);
811 if (fd < 0) {
812 ret = fd;
813 goto out_free_irq;
814 }
815
816 eventreq.fd = fd;
Linus Walleijd932cd42016-07-04 13:13:04 +0200817 if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
818 ret = -EFAULT;
819 goto out_free_irq;
820 }
Linus Walleij61f922d2016-06-02 11:30:15 +0200821
822 return 0;
823
824out_free_irq:
825 free_irq(le->irq, le);
826out_free_desc:
827 gpiod_free(le->desc);
828out_free_label:
829 kfree(le->label);
830out_free_le:
831 kfree(le);
832 put_device(&gdev->dev);
833 return ret;
834}
835
Linus Walleij3c702e92015-10-21 15:29:53 +0200836/**
837 * gpio_ioctl() - ioctl handler for the GPIO chardev
838 */
839static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
840{
841 struct gpio_device *gdev = filp->private_data;
842 struct gpio_chip *chip = gdev->chip;
Linus Walleij8b92e172016-05-27 14:24:04 +0200843 void __user *ip = (void __user *)arg;
Linus Walleij3c702e92015-10-21 15:29:53 +0200844
845 /* We fail any subsequent ioctl():s when the chip is gone */
846 if (!chip)
847 return -ENODEV;
848
Linus Walleij521a2ad2016-02-12 22:25:22 +0100849 /* Fill in the struct and pass to userspace */
Linus Walleij3c702e92015-10-21 15:29:53 +0200850 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
Linus Walleij521a2ad2016-02-12 22:25:22 +0100851 struct gpiochip_info chipinfo;
852
Lars-Peter Clausen0f4bbb22016-10-18 16:54:00 +0200853 memset(&chipinfo, 0, sizeof(chipinfo));
854
Linus Walleij3c702e92015-10-21 15:29:53 +0200855 strncpy(chipinfo.name, dev_name(&gdev->dev),
856 sizeof(chipinfo.name));
857 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
Linus Walleijdf4878e2016-02-12 14:48:23 +0100858 strncpy(chipinfo.label, gdev->label,
859 sizeof(chipinfo.label));
860 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100861 chipinfo.lines = gdev->ngpio;
Linus Walleij3c702e92015-10-21 15:29:53 +0200862 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
863 return -EFAULT;
864 return 0;
Linus Walleij521a2ad2016-02-12 22:25:22 +0100865 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
866 struct gpioline_info lineinfo;
867 struct gpio_desc *desc;
868
869 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
870 return -EFAULT;
Lars-Peter Clausen1f1cc452016-10-18 16:53:59 +0200871 if (lineinfo.line_offset >= gdev->ngpio)
Linus Walleij521a2ad2016-02-12 22:25:22 +0100872 return -EINVAL;
873
874 desc = &gdev->descs[lineinfo.line_offset];
875 if (desc->name) {
876 strncpy(lineinfo.name, desc->name,
877 sizeof(lineinfo.name));
878 lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
879 } else {
880 lineinfo.name[0] = '\0';
881 }
882 if (desc->label) {
Linus Walleij214338e2016-02-25 21:01:48 +0100883 strncpy(lineinfo.consumer, desc->label,
884 sizeof(lineinfo.consumer));
885 lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +0100886 } else {
Linus Walleij214338e2016-02-25 21:01:48 +0100887 lineinfo.consumer[0] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +0100888 }
889
890 /*
891 * Userspace only need to know that the kernel is using
892 * this GPIO so it can't use it.
893 */
894 lineinfo.flags = 0;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100895 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
896 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
897 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
898 test_bit(FLAG_EXPORT, &desc->flags) ||
899 test_bit(FLAG_SYSFS, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100900 lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100901 if (test_bit(FLAG_IS_OUT, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100902 lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100903 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100904 lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100905 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100906 lineinfo.flags |= GPIOLINE_FLAG_OPEN_DRAIN;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100907 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100908 lineinfo.flags |= GPIOLINE_FLAG_OPEN_SOURCE;
909
910 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
911 return -EFAULT;
912 return 0;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200913 } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
914 return linehandle_create(gdev, ip);
Linus Walleij61f922d2016-06-02 11:30:15 +0200915 } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
916 return lineevent_create(gdev, ip);
Linus Walleij3c702e92015-10-21 15:29:53 +0200917 }
918 return -EINVAL;
919}
920
Linus Walleij8b92e172016-05-27 14:24:04 +0200921#ifdef CONFIG_COMPAT
922static long gpio_ioctl_compat(struct file *filp, unsigned int cmd,
923 unsigned long arg)
924{
925 return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
926}
927#endif
928
Linus Walleij3c702e92015-10-21 15:29:53 +0200929/**
930 * gpio_chrdev_open() - open the chardev for ioctl operations
931 * @inode: inode for this chardev
932 * @filp: file struct for storing private data
933 * Returns 0 on success
934 */
935static int gpio_chrdev_open(struct inode *inode, struct file *filp)
936{
937 struct gpio_device *gdev = container_of(inode->i_cdev,
938 struct gpio_device, chrdev);
939
940 /* Fail on open if the backing gpiochip is gone */
941 if (!gdev || !gdev->chip)
942 return -ENODEV;
943 get_device(&gdev->dev);
944 filp->private_data = gdev;
945 return 0;
946}
947
948/**
949 * gpio_chrdev_release() - close chardev after ioctl operations
950 * @inode: inode for this chardev
951 * @filp: file struct for storing private data
952 * Returns 0 on success
953 */
954static int gpio_chrdev_release(struct inode *inode, struct file *filp)
955{
956 struct gpio_device *gdev = container_of(inode->i_cdev,
957 struct gpio_device, chrdev);
958
959 if (!gdev)
960 return -ENODEV;
961 put_device(&gdev->dev);
962 return 0;
963}
964
965
966static const struct file_operations gpio_fileops = {
967 .release = gpio_chrdev_release,
968 .open = gpio_chrdev_open,
969 .owner = THIS_MODULE,
970 .llseek = noop_llseek,
971 .unlocked_ioctl = gpio_ioctl,
Linus Walleij8b92e172016-05-27 14:24:04 +0200972#ifdef CONFIG_COMPAT
973 .compat_ioctl = gpio_ioctl_compat,
974#endif
Linus Walleij3c702e92015-10-21 15:29:53 +0200975};
976
Linus Walleijff2b1352015-10-20 11:10:38 +0200977static void gpiodevice_release(struct device *dev)
978{
979 struct gpio_device *gdev = dev_get_drvdata(dev);
980
981 list_del(&gdev->list);
982 ida_simple_remove(&gpio_ida, gdev->id);
Guenter Roeck476e2fc2016-03-31 08:11:29 -0700983 kfree(gdev->label);
984 kfree(gdev->descs);
Linus Walleij9efd9e62016-02-09 14:27:42 +0100985 kfree(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +0200986}
987
Guenter Roeck159f3cd2016-03-31 08:11:30 -0700988static int gpiochip_setup_dev(struct gpio_device *gdev)
989{
990 int status;
991
992 cdev_init(&gdev->chrdev, &gpio_fileops);
993 gdev->chrdev.owner = THIS_MODULE;
994 gdev->chrdev.kobj.parent = &gdev->dev.kobj;
995 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
996 status = cdev_add(&gdev->chrdev, gdev->dev.devt, 1);
997 if (status < 0)
998 chip_warn(gdev->chip, "failed to add char device %d:%d\n",
999 MAJOR(gpio_devt), gdev->id);
1000 else
1001 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
1002 MAJOR(gpio_devt), gdev->id);
1003 status = device_add(&gdev->dev);
1004 if (status)
1005 goto err_remove_chardev;
1006
1007 status = gpiochip_sysfs_register(gdev);
1008 if (status)
1009 goto err_remove_device;
1010
1011 /* From this point, the .release() function cleans up gpio_device */
1012 gdev->dev.release = gpiodevice_release;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001013 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
1014 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
1015 dev_name(&gdev->dev), gdev->chip->label ? : "generic");
1016
1017 return 0;
1018
1019err_remove_device:
1020 device_del(&gdev->dev);
1021err_remove_chardev:
1022 cdev_del(&gdev->chrdev);
1023 return status;
1024}
1025
1026static void gpiochip_setup_devs(void)
1027{
1028 struct gpio_device *gdev;
1029 int err;
1030
1031 list_for_each_entry(gdev, &gpio_devices, list) {
1032 err = gpiochip_setup_dev(gdev);
1033 if (err)
1034 pr_err("%s: Failed to initialize gpio device (%d)\n",
1035 dev_name(&gdev->dev), err);
1036 }
1037}
1038
Anton Vorontsov169b6a72008-04-28 02:14:47 -07001039/**
Linus Walleijb08ea352015-12-03 15:14:13 +01001040 * gpiochip_add_data() - register a gpio_chip
David Brownelld2876d02008-02-04 22:28:20 -08001041 * @chip: the chip to register, with chip->base initialized
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001042 * Context: potentially before irqs will work
David Brownelld2876d02008-02-04 22:28:20 -08001043 *
1044 * Returns a negative errno if the chip can't be registered, such as
1045 * because the chip->base is invalid or already associated with a
1046 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001047 *
Linus Walleijb08ea352015-12-03 15:14:13 +01001048 * When gpiochip_add_data() is called very early during boot, so that GPIOs
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08001049 * can be freely used, the chip->parent device must be registered before
David Brownelld8f388d82008-07-25 01:46:07 -07001050 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1051 * for GPIOs will fail rudely.
1052 *
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001053 * gpiochip_add_data() must only be called after gpiolib initialization,
1054 * ie after core_initcall().
1055 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001056 * If chip->base is negative, this requests dynamic assignment of
1057 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001058 */
Linus Walleijb08ea352015-12-03 15:14:13 +01001059int gpiochip_add_data(struct gpio_chip *chip, void *data)
David Brownelld2876d02008-02-04 22:28:20 -08001060{
1061 unsigned long flags;
1062 int status = 0;
Linus Walleijff2b1352015-10-20 11:10:38 +02001063 unsigned i;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001064 int base = chip->base;
Linus Walleijff2b1352015-10-20 11:10:38 +02001065 struct gpio_device *gdev;
David Brownelld2876d02008-02-04 22:28:20 -08001066
Linus Walleijff2b1352015-10-20 11:10:38 +02001067 /*
1068 * First: allocate and populate the internal stat container, and
1069 * set up the struct device.
1070 */
Josh Cartwright969f07b2016-02-17 16:44:15 -06001071 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
Linus Walleijff2b1352015-10-20 11:10:38 +02001072 if (!gdev)
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001073 return -ENOMEM;
Linus Walleij3c702e92015-10-21 15:29:53 +02001074 gdev->dev.bus = &gpio_bus_type;
Linus Walleijff2b1352015-10-20 11:10:38 +02001075 gdev->chip = chip;
1076 chip->gpiodev = gdev;
1077 if (chip->parent) {
1078 gdev->dev.parent = chip->parent;
1079 gdev->dev.of_node = chip->parent->of_node;
Thierry Redingacc6e332016-07-05 14:11:14 +02001080 }
1081
Linus Walleijff2b1352015-10-20 11:10:38 +02001082#ifdef CONFIG_OF_GPIO
1083 /* If the gpiochip has an assigned OF node this takes precedence */
Thierry Redingacc6e332016-07-05 14:11:14 +02001084 if (chip->of_node)
1085 gdev->dev.of_node = chip->of_node;
Linus Walleijff2b1352015-10-20 11:10:38 +02001086#endif
Thierry Redingacc6e332016-07-05 14:11:14 +02001087
Linus Walleijff2b1352015-10-20 11:10:38 +02001088 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
1089 if (gdev->id < 0) {
1090 status = gdev->id;
1091 goto err_free_gdev;
1092 }
1093 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
1094 device_initialize(&gdev->dev);
1095 dev_set_drvdata(&gdev->dev, gdev);
1096 if (chip->parent && chip->parent->driver)
1097 gdev->owner = chip->parent->driver->owner;
1098 else if (chip->owner)
1099 /* TODO: remove chip->owner */
1100 gdev->owner = chip->owner;
1101 else
1102 gdev->owner = THIS_MODULE;
David Brownelld2876d02008-02-04 22:28:20 -08001103
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001104 gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001105 if (!gdev->descs) {
Linus Walleijff2b1352015-10-20 11:10:38 +02001106 status = -ENOMEM;
1107 goto err_free_gdev;
1108 }
1109
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001110 if (chip->ngpio == 0) {
1111 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
Linus Walleijff2b1352015-10-20 11:10:38 +02001112 status = -EINVAL;
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001113 goto err_free_descs;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001114 }
Linus Walleijdf4878e2016-02-12 14:48:23 +01001115
1116 if (chip->label)
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001117 gdev->label = kstrdup(chip->label, GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001118 else
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001119 gdev->label = kstrdup("unknown", GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +01001120 if (!gdev->label) {
1121 status = -ENOMEM;
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001122 goto err_free_descs;
Linus Walleijdf4878e2016-02-12 14:48:23 +01001123 }
1124
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001125 gdev->ngpio = chip->ngpio;
Linus Walleij43c54ec2016-02-11 11:37:48 +01001126 gdev->data = data;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +08001127
David Brownelld2876d02008-02-04 22:28:20 -08001128 spin_lock_irqsave(&gpio_lock, flags);
1129
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001130 /*
1131 * TODO: this allocates a Linux GPIO number base in the global
1132 * GPIO numberspace for this chip. In the long run we want to
1133 * get *rid* of this numberspace and use only descriptors, but
1134 * it may be a pipe dream. It will not happen before we get rid
1135 * of the sysfs interface anyways.
1136 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001137 if (base < 0) {
1138 base = gpiochip_find_base(chip->ngpio);
1139 if (base < 0) {
1140 status = base;
Johan Hovold225fce82015-01-12 17:12:25 +01001141 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001142 goto err_free_label;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001143 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001144 /*
1145 * TODO: it should not be necessary to reflect the assigned
1146 * base outside of the GPIO subsystem. Go over drivers and
1147 * see if anyone makes use of this, else drop this and assign
1148 * a poison instead.
1149 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001150 chip->base = base;
1151 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001152 gdev->base = base;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -07001153
Linus Walleijff2b1352015-10-20 11:10:38 +02001154 status = gpiodev_add_to_list(gdev);
Johan Hovold05aa5202015-01-12 17:12:26 +01001155 if (status) {
1156 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001157 goto err_free_label;
Johan Hovold05aa5202015-01-12 17:12:26 +01001158 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +09001159
Linus Walleij545ebd92016-05-30 17:11:59 +02001160 spin_unlock_irqrestore(&gpio_lock, flags);
1161
Linus Walleijff2b1352015-10-20 11:10:38 +02001162 for (i = 0; i < chip->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001163 struct gpio_desc *desc = &gdev->descs[i];
David Brownelld8f388d82008-07-25 01:46:07 -07001164
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001165 desc->gdev = gdev;
Linus Walleij72d32002016-04-28 13:33:59 +02001166 /*
1167 * REVISIT: most hardware initializes GPIOs as inputs
1168 * (often with pullups enabled) so power usage is
1169 * minimized. Linux code should set the gpio direction
1170 * first thing; but until it does, and in case
1171 * chip->get_direction is not set, we may expose the
1172 * wrong direction in sysfs.
Johan Hovold05aa5202015-01-12 17:12:26 +01001173 */
Linus Walleij72d32002016-04-28 13:33:59 +02001174
1175 if (chip->get_direction) {
1176 /*
1177 * If we have .get_direction, set up the initial
1178 * direction flag from the hardware.
1179 */
1180 int dir = chip->get_direction(chip, i);
1181
1182 if (!dir)
1183 set_bit(FLAG_IS_OUT, &desc->flags);
1184 } else if (!chip->direction_input) {
1185 /*
1186 * If the chip lacks the .direction_input callback
1187 * we logically assume all lines are outputs.
1188 */
1189 set_bit(FLAG_IS_OUT, &desc->flags);
1190 }
David Brownelld2876d02008-02-04 22:28:20 -08001191 }
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001192
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301193#ifdef CONFIG_PINCTRL
Linus Walleij20ec3e32016-02-11 11:03:06 +01001194 INIT_LIST_HEAD(&gdev->pin_ranges);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301195#endif
1196
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001197 status = gpiochip_set_desc_names(chip);
1198 if (status)
1199 goto err_remove_from_list;
1200
Mika Westerberg79b804c2016-09-20 15:15:21 +03001201 status = gpiochip_irqchip_init_valid_mask(chip);
1202 if (status)
1203 goto err_remove_from_list;
1204
Tomeu Vizoso28355f82015-07-14 10:29:54 +02001205 status = of_gpiochip_add(chip);
1206 if (status)
1207 goto err_remove_chip;
1208
Mika Westerberg664e3e52014-01-08 12:40:54 +02001209 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001210
Linus Walleij3c702e92015-10-21 15:29:53 +02001211 /*
1212 * By first adding the chardev, and then adding the device,
1213 * we get a device node entry in sysfs under
1214 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
1215 * coldplug of device nodes and other udev business.
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001216 * We can do this only if gpiolib has been initialized.
1217 * Otherwise, defer until later.
Linus Walleij3c702e92015-10-21 15:29:53 +02001218 */
Guenter Roeck159f3cd2016-03-31 08:11:30 -07001219 if (gpiolib_initialized) {
1220 status = gpiochip_setup_dev(gdev);
1221 if (status)
1222 goto err_remove_chip;
1223 }
Anton Vorontsovcedb1882010-06-08 07:48:15 -06001224 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001225
Johan Hovold225fce82015-01-12 17:12:25 +01001226err_remove_chip:
1227 acpi_gpiochip_remove(chip);
Johan Hovold6d867502015-05-04 17:23:25 +02001228 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +01001229 of_gpiochip_remove(chip);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001230 gpiochip_irqchip_free_valid_mask(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +02001231err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +01001232 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001233 list_del(&gdev->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +08001234 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -07001235err_free_label:
1236 kfree(gdev->label);
1237err_free_descs:
1238 kfree(gdev->descs);
Linus Walleijff2b1352015-10-20 11:10:38 +02001239err_free_gdev:
1240 ida_simple_remove(&gpio_ida, gdev->id);
David Brownelld2876d02008-02-04 22:28:20 -08001241 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +02001242 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001243 gdev->base, gdev->base + gdev->ngpio - 1,
1244 chip->label ? : "generic");
1245 kfree(gdev);
David Brownelld2876d02008-02-04 22:28:20 -08001246 return status;
1247}
Linus Walleijb08ea352015-12-03 15:14:13 +01001248EXPORT_SYMBOL_GPL(gpiochip_add_data);
David Brownelld2876d02008-02-04 22:28:20 -08001249
1250/**
Linus Walleij43c54ec2016-02-11 11:37:48 +01001251 * gpiochip_get_data() - get per-subdriver data for the chip
1252 */
1253void *gpiochip_get_data(struct gpio_chip *chip)
1254{
1255 return chip->gpiodev->data;
1256}
1257EXPORT_SYMBOL_GPL(gpiochip_get_data);
1258
1259/**
David Brownelld2876d02008-02-04 22:28:20 -08001260 * gpiochip_remove() - unregister a gpio_chip
1261 * @chip: the chip to unregister
1262 *
1263 * A gpio_chip with any GPIOs still requested may not be removed.
1264 */
abdoulaye berthee1db1702014-07-05 18:28:50 +02001265void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -08001266{
Linus Walleijff2b1352015-10-20 11:10:38 +02001267 struct gpio_device *gdev = chip->gpiodev;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001268 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001269 unsigned long flags;
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001270 unsigned i;
Johan Hovoldfab28b82015-05-04 17:10:27 +02001271 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -08001272
Linus Walleijff2b1352015-10-20 11:10:38 +02001273 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
Linus Walleijafbc4f32016-02-09 13:21:06 +01001274 gpiochip_sysfs_unregister(gdev);
Bamvor Jian Zhangbd203bd2016-02-20 13:13:19 +08001275 /* Numb the device, cancelling all outstanding operations */
1276 gdev->chip = NULL;
Johan Hovold00acc3d2015-01-12 17:12:27 +01001277 gpiochip_irqchip_remove(chip);
Mika Westerberg6072b9d2014-03-10 14:54:53 +02001278 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +01001279 gpiochip_remove_pin_ranges(chip);
Benoit Parrotf625d462015-02-02 11:44:44 -06001280 gpiochip_free_hogs(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -06001281 of_gpiochip_remove(chip);
Linus Walleij43c54ec2016-02-11 11:37:48 +01001282 /*
1283 * We accept no more calls into the driver from this point, so
1284 * NULL the driver data pointer
1285 */
1286 gdev->data = NULL;
Anton Vorontsov391c9702010-06-08 07:48:17 -06001287
Johan Hovold6798aca2015-01-12 17:12:28 +01001288 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001289 for (i = 0; i < gdev->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001290 desc = &gdev->descs[i];
Johan Hovoldfab28b82015-05-04 17:10:27 +02001291 if (test_bit(FLAG_REQUESTED, &desc->flags))
1292 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -08001293 }
David Brownelld2876d02008-02-04 22:28:20 -08001294 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +09001295
Johan Hovoldfab28b82015-05-04 17:10:27 +02001296 if (requested)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001297 dev_crit(&gdev->dev,
Linus Walleij58383c782015-11-04 09:56:26 +01001298 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +02001299
Linus Walleijff2b1352015-10-20 11:10:38 +02001300 /*
1301 * The gpiochip side puts its use of the device to rest here:
1302 * if there are no userspace clients, the chardev and device will
1303 * be removed, else it will be dangling until the last user is
1304 * gone.
1305 */
Ricardo Ribalda Delgadof4833b82016-06-03 19:10:02 +02001306 cdev_del(&gdev->chrdev);
1307 device_del(&gdev->dev);
Linus Walleijff2b1352015-10-20 11:10:38 +02001308 put_device(&gdev->dev);
David Brownelld2876d02008-02-04 22:28:20 -08001309}
1310EXPORT_SYMBOL_GPL(gpiochip_remove);
1311
Laxman Dewangan0cf32922016-02-15 16:32:09 +05301312static void devm_gpio_chip_release(struct device *dev, void *res)
1313{
1314 struct gpio_chip *chip = *(struct gpio_chip **)res;
1315
1316 gpiochip_remove(chip);
1317}
1318
1319static int devm_gpio_chip_match(struct device *dev, void *res, void *data)
1320
1321{
1322 struct gpio_chip **r = res;
1323
1324 if (!r || !*r) {
1325 WARN_ON(!r || !*r);
1326 return 0;
1327 }
1328
1329 return *r == data;
1330}
1331
1332/**
1333 * devm_gpiochip_add_data() - Resource manager piochip_add_data()
1334 * @dev: the device pointer on which irq_chip belongs to.
1335 * @chip: the chip to register, with chip->base initialized
1336 * Context: potentially before irqs will work
1337 *
1338 * Returns a negative errno if the chip can't be registered, such as
1339 * because the chip->base is invalid or already associated with a
1340 * different chip. Otherwise it returns zero as a success code.
1341 *
1342 * The gpio chip automatically be released when the device is unbound.
1343 */
1344int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
1345 void *data)
1346{
1347 struct gpio_chip **ptr;
1348 int ret;
1349
1350 ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
1351 GFP_KERNEL);
1352 if (!ptr)
1353 return -ENOMEM;
1354
1355 ret = gpiochip_add_data(chip, data);
1356 if (ret < 0) {
1357 devres_free(ptr);
1358 return ret;
1359 }
1360
1361 *ptr = chip;
1362 devres_add(dev, ptr);
1363
1364 return 0;
1365}
1366EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
1367
1368/**
1369 * devm_gpiochip_remove() - Resource manager of gpiochip_remove()
1370 * @dev: device for which which resource was allocated
1371 * @chip: the chip to remove
1372 *
1373 * A gpio_chip with any GPIOs still requested may not be removed.
1374 */
1375void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip)
1376{
1377 int ret;
1378
1379 ret = devres_release(dev, devm_gpio_chip_release,
1380 devm_gpio_chip_match, chip);
1381 if (!ret)
1382 WARN_ON(ret);
1383}
1384EXPORT_SYMBOL_GPL(devm_gpiochip_remove);
1385
Grant Likely594fa262010-06-08 07:48:16 -06001386/**
1387 * gpiochip_find() - iterator for locating a specific gpio_chip
1388 * @data: data to pass to match function
1389 * @callback: Callback function to check gpio_chip
1390 *
1391 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1392 * determined by a user supplied @match callback. The callback should return
1393 * 0 if the device doesn't match and non-zero if it does. If the callback is
1394 * non-zero, this function will return to the caller and not iterate over any
1395 * more gpio_chips.
1396 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001397struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001398 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001399 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001400{
Linus Walleijff2b1352015-10-20 11:10:38 +02001401 struct gpio_device *gdev;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001402 struct gpio_chip *chip = NULL;
Grant Likely594fa262010-06-08 07:48:16 -06001403 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001404
1405 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001406 list_for_each_entry(gdev, &gpio_devices, list)
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001407 if (gdev->chip && match(gdev->chip, data)) {
1408 chip = gdev->chip;
Grant Likely594fa262010-06-08 07:48:16 -06001409 break;
Masahiro Yamadaacf06ff2016-08-12 01:21:58 +09001410 }
Linus Walleijff2b1352015-10-20 11:10:38 +02001411
Grant Likely594fa262010-06-08 07:48:16 -06001412 spin_unlock_irqrestore(&gpio_lock, flags);
1413
1414 return chip;
1415}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001416EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001417
Alexandre Courbot79697ef2013-11-16 21:39:32 +09001418static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1419{
1420 const char *name = data;
1421
1422 return !strcmp(chip->label, name);
1423}
1424
1425static struct gpio_chip *find_chip_by_name(const char *name)
1426{
1427 return gpiochip_find((void *)name, gpiochip_match_name);
1428}
1429
Linus Walleij14250522014-03-25 10:40:18 +01001430#ifdef CONFIG_GPIOLIB_IRQCHIP
1431
1432/*
1433 * The following is irqchip helper code for gpiochips.
1434 */
1435
Mika Westerberg79b804c2016-09-20 15:15:21 +03001436static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1437{
1438 int i;
1439
1440 if (!gpiochip->irq_need_valid_mask)
1441 return 0;
1442
1443 gpiochip->irq_valid_mask = kcalloc(BITS_TO_LONGS(gpiochip->ngpio),
1444 sizeof(long), GFP_KERNEL);
1445 if (!gpiochip->irq_valid_mask)
1446 return -ENOMEM;
1447
1448 /* Assume by default all GPIOs are valid */
1449 for (i = 0; i < gpiochip->ngpio; i++)
1450 set_bit(i, gpiochip->irq_valid_mask);
1451
1452 return 0;
1453}
1454
1455static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1456{
1457 kfree(gpiochip->irq_valid_mask);
1458 gpiochip->irq_valid_mask = NULL;
1459}
1460
1461static bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
1462 unsigned int offset)
1463{
1464 /* No mask means all valid */
1465 if (likely(!gpiochip->irq_valid_mask))
1466 return true;
1467 return test_bit(offset, gpiochip->irq_valid_mask);
1468}
1469
Linus Walleij14250522014-03-25 10:40:18 +01001470/**
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001471 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
1472 * @gpiochip: the gpiochip to set the irqchip chain to
1473 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01001474 * @parent_irq: the irq number corresponding to the parent IRQ for this
1475 * chained irqchip
1476 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001477 * coming out of the gpiochip. If the interrupt is nested rather than
1478 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +01001479 */
1480void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
1481 struct irq_chip *irqchip,
1482 int parent_irq,
1483 irq_flow_handler_t parent_handler)
1484{
Linus Walleij83141a72014-09-26 13:50:12 +02001485 unsigned int offset;
1486
Linus Walleij83141a72014-09-26 13:50:12 +02001487 if (!gpiochip->irqdomain) {
1488 chip_err(gpiochip, "called %s before setting up irqchip\n",
1489 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +02001490 return;
1491 }
1492
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001493 if (parent_handler) {
1494 if (gpiochip->can_sleep) {
1495 chip_err(gpiochip,
1496 "you cannot have chained interrupts on a "
1497 "chip that may sleep\n");
1498 return;
1499 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001500 /*
1501 * The parent irqchip is already using the chip_data for this
1502 * irqchip, so our callbacks simply use the handler_data.
1503 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +02001504 irq_set_chained_handler_and_data(parent_irq, parent_handler,
1505 gpiochip);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001506
1507 gpiochip->irq_parent = parent_irq;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001508 }
Linus Walleij83141a72014-09-26 13:50:12 +02001509
1510 /* Set the parent IRQ for all affected IRQs */
Mika Westerberg79b804c2016-09-20 15:15:21 +03001511 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1512 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1513 continue;
Linus Walleij83141a72014-09-26 13:50:12 +02001514 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
1515 parent_irq);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001516 }
Linus Walleij14250522014-03-25 10:40:18 +01001517}
1518EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
1519
1520/**
1521 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1522 * @d: the irqdomain used by this irqchip
1523 * @irq: the global irq number used by this GPIO irqchip irq
1524 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1525 *
1526 * This function will set up the mapping for a certain IRQ line on a
1527 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1528 * stored inside the gpiochip.
1529 */
1530static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1531 irq_hw_number_t hwirq)
1532{
1533 struct gpio_chip *chip = d->host_data;
1534
Linus Walleij14250522014-03-25 10:40:18 +01001535 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001536 /*
1537 * This lock class tells lockdep that GPIO irqs are in a different
1538 * category than their parents, so it won't report false recursion.
1539 */
1540 irq_set_lockdep_class(irq, chip->lock_key);
Linus Walleij7633fb92014-04-09 13:20:38 +02001541 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
Linus Walleij1c8732b2014-04-09 13:34:39 +02001542 /* Chips that can sleep need nested thread handlers */
Octavian Purdila295494a2014-09-19 23:22:44 +03001543 if (chip->can_sleep && !chip->irq_not_threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02001544 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +01001545 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -05001546
Linus Walleij1333b902014-04-23 16:45:12 +02001547 /*
1548 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1549 * is passed as default type.
1550 */
1551 if (chip->irq_default_type != IRQ_TYPE_NONE)
1552 irq_set_irq_type(irq, chip->irq_default_type);
Linus Walleij14250522014-03-25 10:40:18 +01001553
1554 return 0;
1555}
1556
Linus Walleijc3626fd2014-03-28 20:42:01 +01001557static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
1558{
Linus Walleij1c8732b2014-04-09 13:34:39 +02001559 struct gpio_chip *chip = d->host_data;
1560
Linus Walleij1c8732b2014-04-09 13:34:39 +02001561 if (chip->can_sleep)
1562 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001563 irq_set_chip_and_handler(irq, NULL, NULL);
1564 irq_set_chip_data(irq, NULL);
1565}
1566
Linus Walleij14250522014-03-25 10:40:18 +01001567static const struct irq_domain_ops gpiochip_domain_ops = {
1568 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +01001569 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +01001570 /* Virtually all GPIO irqchips are twocell:ed */
1571 .xlate = irq_domain_xlate_twocell,
1572};
1573
1574static int gpiochip_irq_reqres(struct irq_data *d)
1575{
1576 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1577
Linus Walleijff2b1352015-10-20 11:10:38 +02001578 if (!try_module_get(chip->gpiodev->owner))
Grygorii Strashko5b76e792015-06-25 20:30:50 +03001579 return -ENODEV;
1580
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001581 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +01001582 chip_err(chip,
1583 "unable to lock HW IRQ %lu for IRQ\n",
1584 d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001585 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001586 return -EINVAL;
1587 }
1588 return 0;
1589}
1590
1591static void gpiochip_irq_relres(struct irq_data *d)
1592{
1593 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1594
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001595 gpiochip_unlock_as_irq(chip, d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001596 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001597}
1598
1599static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
1600{
1601 return irq_find_mapping(chip->irqdomain, offset);
1602}
1603
1604/**
1605 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1606 * @gpiochip: the gpiochip to remove the irqchip from
1607 *
1608 * This is called only from gpiochip_remove()
1609 */
1610static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
1611{
Linus Walleijc3626fd2014-03-28 20:42:01 +01001612 unsigned int offset;
1613
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001614 acpi_gpiochip_free_interrupts(gpiochip);
1615
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001616 if (gpiochip->irq_parent) {
1617 irq_set_chained_handler(gpiochip->irq_parent, NULL);
1618 irq_set_handler_data(gpiochip->irq_parent, NULL);
1619 }
1620
Linus Walleijc3626fd2014-03-28 20:42:01 +01001621 /* Remove all IRQ mappings and delete the domain */
1622 if (gpiochip->irqdomain) {
Mika Westerberg79b804c2016-09-20 15:15:21 +03001623 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1624 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1625 continue;
Grygorii Strashkoe3893382014-09-25 19:09:23 +03001626 irq_dispose_mapping(
1627 irq_find_mapping(gpiochip->irqdomain, offset));
Mika Westerberg79b804c2016-09-20 15:15:21 +03001628 }
Linus Walleij14250522014-03-25 10:40:18 +01001629 irq_domain_remove(gpiochip->irqdomain);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001630 }
Linus Walleij14250522014-03-25 10:40:18 +01001631
1632 if (gpiochip->irqchip) {
1633 gpiochip->irqchip->irq_request_resources = NULL;
1634 gpiochip->irqchip->irq_release_resources = NULL;
1635 gpiochip->irqchip = NULL;
1636 }
Mika Westerberg79b804c2016-09-20 15:15:21 +03001637
1638 gpiochip_irqchip_free_valid_mask(gpiochip);
Linus Walleij14250522014-03-25 10:40:18 +01001639}
1640
1641/**
1642 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
1643 * @gpiochip: the gpiochip to add the irqchip to
1644 * @irqchip: the irqchip to add to the gpiochip
1645 * @first_irq: if not dynamically assigned, the base (first) IRQ to
1646 * allocate gpiochip irqs from
1647 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +02001648 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
1649 * to have the core avoid setting up any default type in the hardware.
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001650 * @lock_key: lockdep class
Linus Walleij14250522014-03-25 10:40:18 +01001651 *
1652 * This function closely associates a certain irqchip with a certain
1653 * gpiochip, providing an irq domain to translate the local IRQs to
1654 * global irqs in the gpiolib core, and making sure that the gpiochip
1655 * is passed as chip data to all related functions. Driver callbacks
Linus Walleij09dd5f92015-12-07 15:31:58 +01001656 * need to use gpiochip_get_data() to get their local state containers back
Linus Walleij14250522014-03-25 10:40:18 +01001657 * from the gpiochip passed as chip data. An irqdomain will be stored
1658 * in the gpiochip that shall be used by the driver to handle IRQ number
1659 * translation. The gpiochip will need to be initialized and registered
1660 * before calling this function.
1661 *
Linus Walleijc3626fd2014-03-28 20:42:01 +01001662 * This function will handle two cell:ed simple IRQs and assumes all
1663 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +01001664 * need to be open coded.
1665 */
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001666int _gpiochip_irqchip_add(struct gpio_chip *gpiochip,
1667 struct irq_chip *irqchip,
1668 unsigned int first_irq,
1669 irq_flow_handler_t handler,
1670 unsigned int type,
1671 struct lock_class_key *lock_key)
Linus Walleij14250522014-03-25 10:40:18 +01001672{
1673 struct device_node *of_node;
Mika Westerberg79b804c2016-09-20 15:15:21 +03001674 bool irq_base_set = false;
Linus Walleij14250522014-03-25 10:40:18 +01001675 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +01001676 unsigned irq_base = 0;
Linus Walleij14250522014-03-25 10:40:18 +01001677
1678 if (!gpiochip || !irqchip)
1679 return -EINVAL;
1680
Linus Walleij58383c782015-11-04 09:56:26 +01001681 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +01001682 pr_err("missing gpiochip .dev parent pointer\n");
1683 return -EINVAL;
1684 }
Linus Walleij58383c782015-11-04 09:56:26 +01001685 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +01001686#ifdef CONFIG_OF_GPIO
1687 /*
Colin Cronin20a8a962015-05-18 11:41:43 -07001688 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08001689 * FIXME: get rid of this and use gpiochip->parent->of_node
1690 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +01001691 */
1692 if (gpiochip->of_node)
1693 of_node = gpiochip->of_node;
1694#endif
Marc Zyngier332e99d2016-09-07 09:12:11 +01001695 /*
Mika Westerberg0a1e0052016-09-12 14:29:51 +03001696 * Specifying a default trigger is a terrible idea if DT or ACPI is
Marc Zyngier332e99d2016-09-07 09:12:11 +01001697 * used to configure the interrupts, as you may end-up with
1698 * conflicting triggers. Tell the user, and reset to NONE.
1699 */
1700 if (WARN(of_node && type != IRQ_TYPE_NONE,
1701 "%s: Ignoring %d default trigger\n", of_node->full_name, type))
1702 type = IRQ_TYPE_NONE;
Mika Westerberg0a1e0052016-09-12 14:29:51 +03001703 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
1704 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1705 "Ignoring %d default trigger\n", type);
1706 type = IRQ_TYPE_NONE;
1707 }
Marc Zyngier332e99d2016-09-07 09:12:11 +01001708
Linus Walleij14250522014-03-25 10:40:18 +01001709 gpiochip->irqchip = irqchip;
1710 gpiochip->irq_handler = handler;
1711 gpiochip->irq_default_type = type;
1712 gpiochip->to_irq = gpiochip_to_irq;
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001713 gpiochip->lock_key = lock_key;
Linus Walleij14250522014-03-25 10:40:18 +01001714 gpiochip->irqdomain = irq_domain_add_simple(of_node,
1715 gpiochip->ngpio, first_irq,
1716 &gpiochip_domain_ops, gpiochip);
1717 if (!gpiochip->irqdomain) {
1718 gpiochip->irqchip = NULL;
1719 return -EINVAL;
1720 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +02001721
1722 /*
1723 * It is possible for a driver to override this, but only if the
1724 * alternative functions are both implemented.
1725 */
1726 if (!irqchip->irq_request_resources &&
1727 !irqchip->irq_release_resources) {
1728 irqchip->irq_request_resources = gpiochip_irq_reqres;
1729 irqchip->irq_release_resources = gpiochip_irq_relres;
1730 }
Linus Walleij14250522014-03-25 10:40:18 +01001731
1732 /*
1733 * Prepare the mapping since the irqchip shall be orthogonal to
1734 * any gpiochip calls. If the first_irq was zero, this is
1735 * necessary to allocate descriptors for all IRQs.
1736 */
Linus Walleijc3626fd2014-03-28 20:42:01 +01001737 for (offset = 0; offset < gpiochip->ngpio; offset++) {
Mika Westerberg79b804c2016-09-20 15:15:21 +03001738 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1739 continue;
Linus Walleijc3626fd2014-03-28 20:42:01 +01001740 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
Mika Westerberg79b804c2016-09-20 15:15:21 +03001741 if (!irq_base_set) {
Linus Walleijc3626fd2014-03-28 20:42:01 +01001742 /*
1743 * Store the base into the gpiochip to be used when
1744 * unmapping the irqs.
1745 */
1746 gpiochip->irq_base = irq_base;
Mika Westerberg79b804c2016-09-20 15:15:21 +03001747 irq_base_set = true;
1748 }
Linus Walleijc3626fd2014-03-28 20:42:01 +01001749 }
Linus Walleij14250522014-03-25 10:40:18 +01001750
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001751 acpi_gpiochip_request_interrupts(gpiochip);
1752
Linus Walleij14250522014-03-25 10:40:18 +01001753 return 0;
1754}
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001755EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add);
Linus Walleij14250522014-03-25 10:40:18 +01001756
1757#else /* CONFIG_GPIOLIB_IRQCHIP */
1758
1759static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
Mika Westerberg79b804c2016-09-20 15:15:21 +03001760static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1761{
1762 return 0;
1763}
1764static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1765{ }
Linus Walleij14250522014-03-25 10:40:18 +01001766
1767#endif /* CONFIG_GPIOLIB_IRQCHIP */
1768
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001769/**
1770 * gpiochip_generic_request() - request the gpio function for a pin
1771 * @chip: the gpiochip owning the GPIO
1772 * @offset: the offset of the GPIO to request for GPIO function
1773 */
1774int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
1775{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001776 return pinctrl_request_gpio(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001777}
1778EXPORT_SYMBOL_GPL(gpiochip_generic_request);
1779
1780/**
1781 * gpiochip_generic_free() - free the gpio function from a pin
1782 * @chip: the gpiochip to request the gpio function for
1783 * @offset: the offset of the GPIO to free from GPIO function
1784 */
1785void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
1786{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001787 pinctrl_free_gpio(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001788}
1789EXPORT_SYMBOL_GPL(gpiochip_generic_free);
1790
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301791#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01001792
Linus Walleij3f0f8672012-11-20 12:40:15 +01001793/**
Christian Ruppert586a87e2013-10-15 15:37:54 +02001794 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1795 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +02001796 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +02001797 * @gpio_offset: the start offset in the current gpio_chip number space
1798 * @pin_group: name of the pin group inside the pin controller
1799 */
1800int gpiochip_add_pingroup_range(struct gpio_chip *chip,
1801 struct pinctrl_dev *pctldev,
1802 unsigned int gpio_offset, const char *pin_group)
1803{
1804 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001805 struct gpio_device *gdev = chip->gpiodev;
Christian Ruppert586a87e2013-10-15 15:37:54 +02001806 int ret;
1807
1808 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1809 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001810 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +02001811 return -ENOMEM;
1812 }
1813
1814 /* Use local offset as range ID */
1815 pin_range->range.id = gpio_offset;
1816 pin_range->range.gc = chip;
1817 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001818 pin_range->range.base = gdev->base + gpio_offset;
Christian Ruppert586a87e2013-10-15 15:37:54 +02001819 pin_range->pctldev = pctldev;
1820
1821 ret = pinctrl_get_group_pins(pctldev, pin_group,
1822 &pin_range->range.pins,
1823 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001824 if (ret < 0) {
1825 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001826 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001827 }
Christian Ruppert586a87e2013-10-15 15:37:54 +02001828
1829 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1830
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001831 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1832 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +02001833 pinctrl_dev_get_devname(pctldev), pin_group);
1834
Linus Walleij20ec3e32016-02-11 11:03:06 +01001835 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001836
1837 return 0;
1838}
1839EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1840
1841/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01001842 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1843 * @chip: the gpiochip to add the range for
1844 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01001845 * @gpio_offset: the start offset in the current gpio_chip number space
1846 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01001847 * @npins: the number of pins from the offset of each pin space (GPIO and
1848 * pin controller) to accumulate in this range
1849 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001850int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01001851 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01001852 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301853{
1854 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001855 struct gpio_device *gdev = chip->gpiodev;
Axel Linb4d4b1f2012-11-21 14:33:56 +08001856 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301857
Linus Walleij3f0f8672012-11-20 12:40:15 +01001858 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301859 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001860 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001861 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301862 }
1863
Linus Walleij3f0f8672012-11-20 12:40:15 +01001864 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01001865 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001866 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301867 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001868 pin_range->range.base = gdev->base + gpio_offset;
Linus Walleij316511c2012-11-21 08:48:09 +01001869 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301870 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01001871 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301872 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01001873 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08001874 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001875 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +01001876 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08001877 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001878 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001879 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1880 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +01001881 pinctl_name,
1882 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301883
Linus Walleij20ec3e32016-02-11 11:03:06 +01001884 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001885
1886 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301887}
Linus Walleij165adc92012-11-06 14:49:39 +01001888EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301889
Linus Walleij3f0f8672012-11-20 12:40:15 +01001890/**
1891 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1892 * @chip: the chip to remove all the mappings for
1893 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301894void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1895{
1896 struct gpio_pin_range *pin_range, *tmp;
Linus Walleij20ec3e32016-02-11 11:03:06 +01001897 struct gpio_device *gdev = chip->gpiodev;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301898
Linus Walleij20ec3e32016-02-11 11:03:06 +01001899 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301900 list_del(&pin_range->node);
1901 pinctrl_remove_gpio_range(pin_range->pctldev,
1902 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001903 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301904 }
1905}
Linus Walleij165adc92012-11-06 14:49:39 +01001906EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1907
1908#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301909
David Brownelld2876d02008-02-04 22:28:20 -08001910/* These "optional" allocation calls help prevent drivers from stomping
1911 * on each other, and help provide better diagnostics in debugfs.
1912 * They're called even less than the "set direction" calls.
1913 */
Mika Westerberg77c2d792014-03-10 14:54:50 +02001914static int __gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08001915{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001916 struct gpio_chip *chip = desc->gdev->chip;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001917 int status;
David Brownelld2876d02008-02-04 22:28:20 -08001918 unsigned long flags;
1919
1920 spin_lock_irqsave(&gpio_lock, flags);
1921
David Brownelld2876d02008-02-04 22:28:20 -08001922 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07001923 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001924 */
1925
1926 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1927 desc_set_label(desc, label ? : "?");
1928 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001929 } else {
David Brownelld2876d02008-02-04 22:28:20 -08001930 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -08001931 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001932 }
1933
1934 if (chip->request) {
1935 /* chip->request may sleep */
1936 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001937 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001938 spin_lock_irqsave(&gpio_lock, flags);
1939
1940 if (status < 0) {
1941 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -07001942 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001943 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001944 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001945 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03001946 if (chip->get_direction) {
1947 /* chip->get_direction may sleep */
1948 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001949 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001950 spin_lock_irqsave(&gpio_lock, flags);
1951 }
David Brownelld2876d02008-02-04 22:28:20 -08001952done:
Mika Westerberg77c2d792014-03-10 14:54:50 +02001953 spin_unlock_irqrestore(&gpio_lock, flags);
1954 return status;
1955}
1956
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001957/*
1958 * This descriptor validation needs to be inserted verbatim into each
1959 * function taking a descriptor, so we need to use a preprocessor
Linus Walleij54d77192016-05-30 16:48:39 +02001960 * macro to avoid endless duplication. If the desc is NULL it is an
1961 * optional GPIO and calls should just bail out.
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001962 */
1963#define VALIDATE_DESC(desc) do { \
Linus Walleij54d77192016-05-30 16:48:39 +02001964 if (!desc) \
1965 return 0; \
Linus Walleijbfbbe442016-06-16 11:55:55 +02001966 if (IS_ERR(desc)) { \
1967 pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
1968 return PTR_ERR(desc); \
1969 } \
Linus Walleij54d77192016-05-30 16:48:39 +02001970 if (!desc->gdev) { \
Linus Walleijbfbbe442016-06-16 11:55:55 +02001971 pr_warn("%s: invalid GPIO (no device)\n", __func__); \
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001972 return -EINVAL; \
1973 } \
1974 if ( !desc->gdev->chip ) { \
1975 dev_warn(&desc->gdev->dev, \
1976 "%s: backing chip is gone\n", __func__); \
1977 return 0; \
1978 } } while (0)
1979
1980#define VALIDATE_DESC_VOID(desc) do { \
Linus Walleij54d77192016-05-30 16:48:39 +02001981 if (!desc) \
1982 return; \
Linus Walleijbfbbe442016-06-16 11:55:55 +02001983 if (IS_ERR(desc)) { \
1984 pr_warn("%s: invalid GPIO (errorpointer)\n", __func__); \
1985 return; \
1986 } \
Linus Walleij54d77192016-05-30 16:48:39 +02001987 if (!desc->gdev) { \
Linus Walleijbfbbe442016-06-16 11:55:55 +02001988 pr_warn("%s: invalid GPIO (no device)\n", __func__); \
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001989 return; \
1990 } \
1991 if (!desc->gdev->chip) { \
1992 dev_warn(&desc->gdev->dev, \
1993 "%s: backing chip is gone\n", __func__); \
1994 return; \
1995 } } while (0)
1996
1997
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09001998int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001999{
2000 int status = -EPROBE_DEFER;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002001 struct gpio_device *gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002002
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002003 VALIDATE_DESC(desc);
2004 gdev = desc->gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002005
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002006 if (try_module_get(gdev->owner)) {
Mika Westerberg77c2d792014-03-10 14:54:50 +02002007 status = __gpiod_request(desc, label);
2008 if (status < 0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002009 module_put(gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002010 else
2011 get_device(&gdev->dev);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002012 }
2013
David Brownelld2876d02008-02-04 22:28:20 -08002014 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +02002015 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002016
David Brownelld2876d02008-02-04 22:28:20 -08002017 return status;
2018}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002019
Mika Westerberg77c2d792014-03-10 14:54:50 +02002020static bool __gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002021{
Mika Westerberg77c2d792014-03-10 14:54:50 +02002022 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08002023 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07002024 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002025
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07002026 might_sleep();
2027
Alexandre Courbot372e7222013-02-03 01:29:29 +09002028 gpiod_unexport(desc);
David Brownelld8f388d82008-07-25 01:46:07 -07002029
David Brownelld2876d02008-02-04 22:28:20 -08002030 spin_lock_irqsave(&gpio_lock, flags);
2031
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002032 chip = desc->gdev->chip;
David Brownell35e8bb52008-10-15 22:03:16 -07002033 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
2034 if (chip->free) {
2035 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07002036 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002037 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07002038 spin_lock_irqsave(&gpio_lock, flags);
2039 }
David Brownelld2876d02008-02-04 22:28:20 -08002040 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08002041 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07002042 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302043 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302044 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06002045 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002046 ret = true;
2047 }
David Brownelld2876d02008-02-04 22:28:20 -08002048
2049 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002050 return ret;
2051}
2052
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09002053void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002054{
Linus Walleij33a68e82016-02-11 10:28:44 +01002055 if (desc && desc->gdev && __gpiod_free(desc)) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002056 module_put(desc->gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01002057 put_device(&desc->gdev->dev);
2058 } else {
Mika Westerberg77c2d792014-03-10 14:54:50 +02002059 WARN_ON(extra_checks);
Linus Walleij33a68e82016-02-11 10:28:44 +01002060 }
David Brownelld2876d02008-02-04 22:28:20 -08002061}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002062
David Brownelld2876d02008-02-04 22:28:20 -08002063/**
2064 * gpiochip_is_requested - return string iff signal was requested
2065 * @chip: controller managing the signal
2066 * @offset: of signal within controller's 0..(ngpio - 1) range
2067 *
2068 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09002069 * The string returned is the label passed to gpio_request(); if none has been
2070 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08002071 *
2072 * This function is for use by GPIO controller drivers. The label can
2073 * help with diagnostics, and knowing that the signal is used as a GPIO
2074 * can help avoid accidentally multiplexing it to another controller.
2075 */
2076const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
2077{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002078 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08002079
Dirk Behme48b59532015-08-18 18:02:32 +02002080 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08002081 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002082
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002083 desc = &chip->gpiodev->descs[offset];
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002084
Alexandre Courbot372e7222013-02-03 01:29:29 +09002085 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08002086 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002087 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08002088}
2089EXPORT_SYMBOL_GPL(gpiochip_is_requested);
2090
Mika Westerberg77c2d792014-03-10 14:54:50 +02002091/**
2092 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
2093 * @desc: GPIO descriptor to request
2094 * @label: label for the GPIO
2095 *
2096 * Function allows GPIO chip drivers to request and use their own GPIO
2097 * descriptors via gpiolib API. Difference to gpiod_request() is that this
2098 * function will not increase reference count of the GPIO chip module. This
2099 * allows the GPIO chip module to be unloaded as needed (we assume that the
2100 * GPIO chip driver handles freeing the GPIOs it has requested).
2101 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002102struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
2103 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02002104{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002105 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
2106 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002107
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07002108 if (IS_ERR(desc)) {
2109 chip_err(chip, "failed to get GPIO descriptor\n");
2110 return desc;
2111 }
2112
2113 err = __gpiod_request(desc, label);
2114 if (err < 0)
2115 return ERR_PTR(err);
2116
2117 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02002118}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002119EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02002120
2121/**
2122 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
2123 * @desc: GPIO descriptor to free
2124 *
2125 * Function frees the given GPIO requested previously with
2126 * gpiochip_request_own_desc().
2127 */
2128void gpiochip_free_own_desc(struct gpio_desc *desc)
2129{
2130 if (desc)
2131 __gpiod_free(desc);
2132}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07002133EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08002134
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002135/*
2136 * Drivers MUST set GPIO direction before making get/set calls. In
David Brownelld2876d02008-02-04 22:28:20 -08002137 * some cases this is done in early boot, before IRQs are enabled.
2138 *
2139 * As a rule these aren't called more than once (except for drivers
2140 * using the open-drain emulation idiom) so these are natural places
2141 * to accumulate extra debugging checks. Note that we can't (yet)
2142 * rely on gpio_request() having been called beforehand.
2143 */
2144
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002145/**
2146 * gpiod_direction_input - set the GPIO direction to input
2147 * @desc: GPIO to set to input
2148 *
2149 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
2150 * be called safely on it.
2151 *
2152 * Return 0 in case of success, else an error code.
2153 */
2154int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002155{
David Brownelld2876d02008-02-04 22:28:20 -08002156 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08002157 int status = -EINVAL;
2158
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002159 VALIDATE_DESC(desc);
2160 chip = desc->gdev->chip;
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09002161
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002162 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01002163 gpiod_warn(desc,
2164 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02002165 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002166 return -EIO;
2167 }
2168
Alexandre Courbotd82da792014-07-22 16:17:43 +09002169 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -08002170 if (status == 0)
2171 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002172
Alexandre Courbot372e7222013-02-03 01:29:29 +09002173 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09002174
David Brownelld2876d02008-02-04 22:28:20 -08002175 return status;
2176}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002177EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002178
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002179static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08002180{
Linus Walleijc663e5f2016-03-22 10:51:16 +01002181 struct gpio_chip *gc = desc->gdev->chip;
2182 int ret;
David Brownelld2876d02008-02-04 22:28:20 -08002183
Linus Walleijd468bf92013-09-24 11:54:38 +02002184 /* GPIOs used for IRQs shall not be set as output */
2185 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
2186 gpiod_err(desc,
2187 "%s: tried to set a GPIO tied to an IRQ as output\n",
2188 __func__);
2189 return -EIO;
2190 }
2191
Linus Walleijc663e5f2016-03-22 10:51:16 +01002192 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2193 /* First see if we can enable open drain in hardware */
2194 if (gc->set_single_ended) {
2195 ret = gc->set_single_ended(gc, gpio_chip_hwgpio(desc),
2196 LINE_MODE_OPEN_DRAIN);
2197 if (!ret)
2198 goto set_output_value;
2199 }
2200 /* Emulate open drain by not actively driving the line high */
2201 if (value)
2202 return gpiod_direction_input(desc);
2203 }
2204 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2205 if (gc->set_single_ended) {
2206 ret = gc->set_single_ended(gc, gpio_chip_hwgpio(desc),
2207 LINE_MODE_OPEN_SOURCE);
2208 if (!ret)
2209 goto set_output_value;
2210 }
2211 /* Emulate open source by not actively driving the line low */
2212 if (!value)
2213 return gpiod_direction_input(desc);
2214 } else {
2215 /* Make sure to disable open drain/source hardware, if any */
2216 if (gc->set_single_ended)
2217 gc->set_single_ended(gc,
2218 gpio_chip_hwgpio(desc),
2219 LINE_MODE_PUSH_PULL);
2220 }
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302221
Linus Walleijc663e5f2016-03-22 10:51:16 +01002222set_output_value:
2223 if (!gc->set || !gc->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01002224 gpiod_warn(desc,
2225 "%s: missing set() or direction_output() operations\n",
2226 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002227 return -EIO;
2228 }
2229
Linus Walleijc663e5f2016-03-22 10:51:16 +01002230 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), value);
2231 if (!ret)
David Brownelld2876d02008-02-04 22:28:20 -08002232 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002233 trace_gpio_value(desc_to_gpio(desc), 0, value);
Linus Walleijc663e5f2016-03-22 10:51:16 +01002234 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
2235 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08002236}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002237
2238/**
2239 * gpiod_direction_output_raw - set the GPIO direction to output
2240 * @desc: GPIO to set to output
2241 * @value: initial output value of the GPIO
2242 *
2243 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2244 * be called safely on it. The initial value of the output must be specified
2245 * as raw value on the physical line without regard for the ACTIVE_LOW status.
2246 *
2247 * Return 0 in case of success, else an error code.
2248 */
2249int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2250{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002251 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002252 return _gpiod_direction_output_raw(desc, value);
2253}
2254EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2255
2256/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05302257 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002258 * @desc: GPIO to set to output
2259 * @value: initial output value of the GPIO
2260 *
2261 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
2262 * be called safely on it. The initial value of the output must be specified
2263 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2264 * account.
2265 *
2266 * Return 0 in case of success, else an error code.
2267 */
2268int gpiod_direction_output(struct gpio_desc *desc, int value)
2269{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002270 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01002271 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2272 value = !value;
2273 return _gpiod_direction_output_raw(desc, value);
2274}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002275EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08002276
Felipe Balbic4b5be92010-05-26 14:42:23 -07002277/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002278 * gpiod_set_debounce - sets @debounce time for a @gpio
Felipe Balbic4b5be92010-05-26 14:42:23 -07002279 * @gpio: the gpio to set debounce time
2280 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02002281 *
2282 * returns -ENOTSUPP if the controller does not support setting
2283 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07002284 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002285int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07002286{
Felipe Balbic4b5be92010-05-26 14:42:23 -07002287 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07002288
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002289 VALIDATE_DESC(desc);
2290 chip = desc->gdev->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002291 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01002292 gpiod_dbg(desc,
2293 "%s: missing set() or set_debounce() operations\n",
2294 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02002295 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02002296 }
2297
Alexandre Courbotd82da792014-07-22 16:17:43 +09002298 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07002299}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002300EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002301
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002302/**
2303 * gpiod_is_active_low - test whether a GPIO is active-low or not
2304 * @desc: the gpio descriptor to test
2305 *
2306 * Returns 1 if the GPIO is active-low, 0 otherwise.
2307 */
2308int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002309{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002310 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002311 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002312}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002313EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08002314
2315/* I/O calls are only valid after configuration completed; the relevant
2316 * "is this a valid GPIO" error checks should already have been done.
2317 *
2318 * "Get" operations are often inlinable as reading a pin value register,
2319 * and masking the relevant bit in that register.
2320 *
2321 * When "set" operations are inlinable, they involve writing that mask to
2322 * one register to set a low value, or a different register to set it high.
2323 * Otherwise locking is needed, so there may be little value to inlining.
2324 *
2325 *------------------------------------------------------------------------
2326 *
2327 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
2328 * have requested the GPIO. That can include implicit requesting by
2329 * a direction setting call. Marking a gpio as requested locks its chip
2330 * in memory, guaranteeing that these table lookups need no more locking
2331 * and that gpiochip_remove() will fail.
2332 *
2333 * REVISIT when debugging, consider adding some instrumentation to ensure
2334 * that the GPIO was actually requested.
2335 */
2336
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002337static int _gpiod_get_raw_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002338{
2339 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002340 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002341 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002342
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002343 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002344 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002345 value = chip->get ? chip->get(chip, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01002346 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002347 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06002348 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002349}
Alexandre Courbot372e7222013-02-03 01:29:29 +09002350
David Brownelld2876d02008-02-04 22:28:20 -08002351/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002352 * gpiod_get_raw_value() - return a gpio's raw value
2353 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08002354 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002355 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002356 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002357 *
2358 * This function should be called from contexts where we cannot sleep, and will
2359 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002360 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002361int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002362{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002363 VALIDATE_DESC(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002364 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002365 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002366 return _gpiod_get_raw_value(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002367}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002368EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002369
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002370/**
2371 * gpiod_get_value() - return a gpio's value
2372 * @desc: gpio whose value will be returned
2373 *
2374 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002375 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002376 *
2377 * This function should be called from contexts where we cannot sleep, and will
2378 * complain if the GPIO chip functions potentially sleep.
2379 */
2380int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002381{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002382 int value;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002383
2384 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002385 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002386 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002387
2388 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002389 if (value < 0)
2390 return value;
2391
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002392 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2393 value = !value;
2394
2395 return value;
David Brownelld2876d02008-02-04 22:28:20 -08002396}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002397EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08002398
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302399/*
2400 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002401 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002402 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302403 */
Alexandre Courbot23600962014-03-11 15:52:09 +09002404static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302405{
2406 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002407 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002408 int offset = gpio_chip_hwgpio(desc);
2409
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302410 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002411 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302412 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002413 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302414 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002415 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302416 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002417 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302418 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002419 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302420 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002421 gpiod_err(desc,
2422 "%s: Error in set_value for open drain err %d\n",
2423 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302424}
2425
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302426/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002427 * _gpio_set_open_source_value() - Set the open source gpio's value.
2428 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002429 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302430 */
Alexandre Courbot23600962014-03-11 15:52:09 +09002431static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302432{
2433 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002434 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002435 int offset = gpio_chip_hwgpio(desc);
2436
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302437 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002438 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302439 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002440 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302441 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002442 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302443 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002444 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302445 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002446 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302447 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002448 gpiod_err(desc,
2449 "%s: Error in set_value for open source err %d\n",
2450 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302451}
2452
Alexandre Courbot23600962014-03-11 15:52:09 +09002453static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08002454{
2455 struct gpio_chip *chip;
2456
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002457 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002458 trace_gpio_value(desc_to_gpio(desc), 0, value);
2459 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2460 _gpio_set_open_drain_value(desc, value);
2461 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2462 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302463 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09002464 chip->set(chip, gpio_chip_hwgpio(desc), value);
2465}
2466
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002467/*
2468 * set multiple outputs on the same chip;
2469 * use the chip's set_multiple function if available;
2470 * otherwise set the outputs sequentially;
2471 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
2472 * defines which outputs are to be changed
2473 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
2474 * defines the values the outputs specified by mask are to be set to
2475 */
2476static void gpio_chip_set_multiple(struct gpio_chip *chip,
2477 unsigned long *mask, unsigned long *bits)
2478{
2479 if (chip->set_multiple) {
2480 chip->set_multiple(chip, mask, bits);
2481 } else {
2482 int i;
2483 for (i = 0; i < chip->ngpio; i++) {
2484 if (mask[BIT_WORD(i)] == 0) {
2485 /* no more set bits in this mask word;
2486 * skip ahead to the next word */
2487 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
2488 continue;
2489 }
2490 /* set outputs if the corresponding mask bit is set */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002491 if (__test_and_clear_bit(i, mask))
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002492 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002493 }
2494 }
2495}
2496
Linus Walleij44c72882016-04-24 11:36:59 +02002497void gpiod_set_array_value_complex(bool raw, bool can_sleep,
2498 unsigned int array_size,
2499 struct gpio_desc **desc_array,
2500 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002501{
2502 int i = 0;
2503
2504 while (i < array_size) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002505 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002506 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2507 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2508 int count = 0;
2509
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002510 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002511 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002512
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002513 memset(mask, 0, sizeof(mask));
2514 do {
2515 struct gpio_desc *desc = desc_array[i];
2516 int hwgpio = gpio_chip_hwgpio(desc);
2517 int value = value_array[i];
2518
2519 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2520 value = !value;
2521 trace_gpio_value(desc_to_gpio(desc), 0, value);
2522 /*
2523 * collect all normal outputs belonging to the same chip
2524 * open drain and open source outputs are set individually
2525 */
2526 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002527 _gpio_set_open_drain_value(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002528 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2529 _gpio_set_open_source_value(desc, value);
2530 } else {
2531 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002532 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002533 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002534 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002535 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002536 count++;
2537 }
2538 i++;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002539 } while ((i < array_size) &&
2540 (desc_array[i]->gdev->chip == chip));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002541 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002542 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002543 gpio_chip_set_multiple(chip, mask, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002544 }
2545}
2546
David Brownelld2876d02008-02-04 22:28:20 -08002547/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002548 * gpiod_set_raw_value() - assign a gpio's raw value
2549 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08002550 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002551 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002552 * Set the raw value of the GPIO, i.e. the value of its physical line without
2553 * regard for its ACTIVE_LOW status.
2554 *
2555 * This function should be called from contexts where we cannot sleep, and will
2556 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002557 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002558void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002559{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002560 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01002561 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002562 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002563 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002564}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002565EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002566
2567/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002568 * gpiod_set_value() - assign a gpio's value
2569 * @desc: gpio whose value will be assigned
2570 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002571 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002572 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2573 * account
2574 *
2575 * This function should be called from contexts where we cannot sleep, and will
2576 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002577 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002578void gpiod_set_value(struct gpio_desc *desc, int value)
2579{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002580 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01002581 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002582 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002583 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2584 value = !value;
2585 _gpiod_set_raw_value(desc, value);
2586}
2587EXPORT_SYMBOL_GPL(gpiod_set_value);
2588
2589/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002590 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002591 * @array_size: number of elements in the descriptor / value arrays
2592 * @desc_array: array of GPIO descriptors whose values will be assigned
2593 * @value_array: array of values to assign
2594 *
2595 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2596 * without regard for their ACTIVE_LOW status.
2597 *
2598 * This function should be called from contexts where we cannot sleep, and will
2599 * complain if the GPIO chip functions potentially sleep.
2600 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002601void gpiod_set_raw_array_value(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002602 struct gpio_desc **desc_array, int *value_array)
2603{
2604 if (!desc_array)
2605 return;
Linus Walleij44c72882016-04-24 11:36:59 +02002606 gpiod_set_array_value_complex(true, false, array_size, desc_array,
2607 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002608}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002609EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002610
2611/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002612 * gpiod_set_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002613 * @array_size: number of elements in the descriptor / value arrays
2614 * @desc_array: array of GPIO descriptors whose values will be assigned
2615 * @value_array: array of values to assign
2616 *
2617 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2618 * into account.
2619 *
2620 * This function should be called from contexts where we cannot sleep, and will
2621 * complain if the GPIO chip functions potentially sleep.
2622 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002623void gpiod_set_array_value(unsigned int array_size,
2624 struct gpio_desc **desc_array, int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002625{
2626 if (!desc_array)
2627 return;
Linus Walleij44c72882016-04-24 11:36:59 +02002628 gpiod_set_array_value_complex(false, false, array_size, desc_array,
2629 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002630}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002631EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002632
2633/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002634 * gpiod_cansleep() - report whether gpio value access may sleep
2635 * @desc: gpio to check
2636 *
2637 */
2638int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002639{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002640 VALIDATE_DESC(desc);
2641 return desc->gdev->chip->can_sleep;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002642}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002643EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002644
David Brownell0f6d5042008-10-15 22:03:14 -07002645/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002646 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
2647 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07002648 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002649 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
2650 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07002651 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002652int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07002653{
Linus Walleij4c37ce82016-05-02 13:13:10 +02002654 struct gpio_chip *chip;
2655 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07002656
Linus Walleij79bb71b2016-06-15 22:57:38 +02002657 /*
2658 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
2659 * requires this function to not return zero on an invalid descriptor
2660 * but rather a negative error number.
2661 */
Linus Walleijbfbbe442016-06-16 11:55:55 +02002662 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
Linus Walleij79bb71b2016-06-15 22:57:38 +02002663 return -EINVAL;
2664
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002665 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002666 offset = gpio_chip_hwgpio(desc);
Linus Walleij4c37ce82016-05-02 13:13:10 +02002667 if (chip->to_irq) {
2668 int retirq = chip->to_irq(chip, offset);
2669
2670 /* Zero means NO_IRQ */
2671 if (!retirq)
2672 return -ENXIO;
2673
2674 return retirq;
2675 }
2676 return -ENXIO;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002677}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002678EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002679
Linus Walleijd468bf92013-09-24 11:54:38 +02002680/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002681 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002682 * @chip: the chip the GPIO to lock belongs to
2683 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02002684 *
2685 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08002686 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08002687 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002688int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08002689{
Linus Walleij9c102802016-05-25 10:56:03 +02002690 struct gpio_desc *desc;
Linus Walleijd468bf92013-09-24 11:54:38 +02002691
Linus Walleij9c102802016-05-25 10:56:03 +02002692 desc = gpiochip_get_desc(chip, offset);
2693 if (IS_ERR(desc))
2694 return PTR_ERR(desc);
2695
2696 /* Flush direction if something changed behind our back */
2697 if (chip->get_direction) {
2698 int dir = chip->get_direction(chip, offset);
2699
2700 if (dir)
2701 clear_bit(FLAG_IS_OUT, &desc->flags);
2702 else
2703 set_bit(FLAG_IS_OUT, &desc->flags);
2704 }
2705
2706 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002707 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02002708 "%s: tried to flag a GPIO set as output for IRQ\n",
2709 __func__);
2710 return -EIO;
2711 }
2712
Linus Walleij9c102802016-05-25 10:56:03 +02002713 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002714 return 0;
2715}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002716EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02002717
Linus Walleijd468bf92013-09-24 11:54:38 +02002718/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002719 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002720 * @chip: the chip the GPIO to lock belongs to
2721 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02002722 *
2723 * This is used directly by GPIO drivers that want to indicate
2724 * that a certain GPIO is no longer used exclusively for IRQ.
2725 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002726void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02002727{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002728 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02002729 return;
2730
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002731 clear_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002732}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002733EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02002734
Linus Walleij6cee3822016-02-11 20:16:45 +01002735bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
2736{
2737 if (offset >= chip->ngpio)
2738 return false;
2739
2740 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
2741}
2742EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
2743
Linus Walleij143b65d2016-02-16 15:41:42 +01002744bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
2745{
2746 if (offset >= chip->ngpio)
2747 return false;
2748
2749 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
2750}
2751EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
2752
2753bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
2754{
2755 if (offset >= chip->ngpio)
2756 return false;
2757
2758 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
2759}
2760EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
2761
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002762/**
2763 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
2764 * @desc: gpio whose value will be returned
2765 *
2766 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002767 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002768 *
2769 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002770 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002771int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002772{
David Brownelld2876d02008-02-04 22:28:20 -08002773 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002774 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002775 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002776}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002777EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002778
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002779/**
2780 * gpiod_get_value_cansleep() - return a gpio's value
2781 * @desc: gpio whose value will be returned
2782 *
2783 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002784 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002785 *
2786 * This function is to be called from contexts that can sleep.
2787 */
2788int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002789{
David Brownelld2876d02008-02-04 22:28:20 -08002790 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002791
2792 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002793 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002794 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002795 if (value < 0)
2796 return value;
2797
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002798 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2799 value = !value;
2800
David Brownelld2876d02008-02-04 22:28:20 -08002801 return value;
2802}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002803EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002804
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002805/**
2806 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
2807 * @desc: gpio whose value will be assigned
2808 * @value: value to assign
2809 *
2810 * Set the raw value of the GPIO, i.e. the value of its physical line without
2811 * regard for its ACTIVE_LOW status.
2812 *
2813 * This function is to be called from contexts that can sleep.
2814 */
2815void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002816{
David Brownelld2876d02008-02-04 22:28:20 -08002817 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002818 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002819 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002820}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002821EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002822
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002823/**
2824 * gpiod_set_value_cansleep() - assign a gpio's value
2825 * @desc: gpio whose value will be assigned
2826 * @value: value to assign
2827 *
2828 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2829 * account
2830 *
2831 * This function is to be called from contexts that can sleep.
2832 */
2833void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002834{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002835 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002836 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002837 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2838 value = !value;
2839 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002840}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002841EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002842
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002843/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002844 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002845 * @array_size: number of elements in the descriptor / value arrays
2846 * @desc_array: array of GPIO descriptors whose values will be assigned
2847 * @value_array: array of values to assign
2848 *
2849 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2850 * without regard for their ACTIVE_LOW status.
2851 *
2852 * This function is to be called from contexts that can sleep.
2853 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002854void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
2855 struct gpio_desc **desc_array,
2856 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002857{
2858 might_sleep_if(extra_checks);
2859 if (!desc_array)
2860 return;
Linus Walleij44c72882016-04-24 11:36:59 +02002861 gpiod_set_array_value_complex(true, true, array_size, desc_array,
2862 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002863}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002864EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002865
2866/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002867 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002868 * @array_size: number of elements in the descriptor / value arrays
2869 * @desc_array: array of GPIO descriptors whose values will be assigned
2870 * @value_array: array of values to assign
2871 *
2872 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2873 * into account.
2874 *
2875 * This function is to be called from contexts that can sleep.
2876 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002877void gpiod_set_array_value_cansleep(unsigned int array_size,
2878 struct gpio_desc **desc_array,
2879 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002880{
2881 might_sleep_if(extra_checks);
2882 if (!desc_array)
2883 return;
Linus Walleij44c72882016-04-24 11:36:59 +02002884 gpiod_set_array_value_complex(false, true, array_size, desc_array,
2885 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002886}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002887EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002888
2889/**
Alexandre Courbotad824782013-12-03 12:20:11 +09002890 * gpiod_add_lookup_table() - register GPIO device consumers
2891 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002892 */
Alexandre Courbotad824782013-12-03 12:20:11 +09002893void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002894{
2895 mutex_lock(&gpio_lookup_lock);
2896
Alexandre Courbotad824782013-12-03 12:20:11 +09002897 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002898
2899 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08002900}
2901
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05302902/**
2903 * gpiod_remove_lookup_table() - unregister GPIO device consumers
2904 * @table: table of consumers to unregister
2905 */
2906void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
2907{
2908 mutex_lock(&gpio_lookup_lock);
2909
2910 list_del(&table->list);
2911
2912 mutex_unlock(&gpio_lookup_lock);
2913}
2914
Alexandre Courbotad824782013-12-03 12:20:11 +09002915static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
2916{
2917 const char *dev_id = dev ? dev_name(dev) : NULL;
2918 struct gpiod_lookup_table *table;
2919
2920 mutex_lock(&gpio_lookup_lock);
2921
2922 list_for_each_entry(table, &gpio_lookup_list, list) {
2923 if (table->dev_id && dev_id) {
2924 /*
2925 * Valid strings on both ends, must be identical to have
2926 * a match
2927 */
2928 if (!strcmp(table->dev_id, dev_id))
2929 goto found;
2930 } else {
2931 /*
2932 * One of the pointers is NULL, so both must be to have
2933 * a match
2934 */
2935 if (dev_id == table->dev_id)
2936 goto found;
2937 }
2938 }
2939 table = NULL;
2940
2941found:
2942 mutex_unlock(&gpio_lookup_lock);
2943 return table;
2944}
2945
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002946static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002947 unsigned int idx,
2948 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002949{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002950 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09002951 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002952 struct gpiod_lookup *p;
2953
Alexandre Courbotad824782013-12-03 12:20:11 +09002954 table = gpiod_find_lookup_table(dev);
2955 if (!table)
2956 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002957
Alexandre Courbotad824782013-12-03 12:20:11 +09002958 for (p = &table->table[0]; p->chip_label; p++) {
2959 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002960
Alexandre Courbotad824782013-12-03 12:20:11 +09002961 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002962 if (p->idx != idx)
2963 continue;
2964
Alexandre Courbotad824782013-12-03 12:20:11 +09002965 /* If the lookup entry has a con_id, require exact match */
2966 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
2967 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002968
Alexandre Courbotad824782013-12-03 12:20:11 +09002969 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002970
Alexandre Courbotad824782013-12-03 12:20:11 +09002971 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002972 dev_err(dev, "cannot find GPIO chip %s\n",
2973 p->chip_label);
2974 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002975 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002976
Alexandre Courbotad824782013-12-03 12:20:11 +09002977 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002978 dev_err(dev,
2979 "requested GPIO %d is out of range [0..%d] for chip %s\n",
2980 idx, chip->ngpio, chip->label);
2981 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09002982 }
2983
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09002984 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09002985 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002986
2987 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09002988 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002989
2990 return desc;
2991}
2992
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002993static int dt_gpio_count(struct device *dev, const char *con_id)
2994{
2995 int ret;
2996 char propname[32];
2997 unsigned int i;
2998
2999 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
3000 if (con_id)
3001 snprintf(propname, sizeof(propname), "%s-%s",
3002 con_id, gpio_suffixes[i]);
3003 else
3004 snprintf(propname, sizeof(propname), "%s",
3005 gpio_suffixes[i]);
3006
3007 ret = of_gpio_named_count(dev->of_node, propname);
3008 if (ret >= 0)
3009 break;
3010 }
3011 return ret;
3012}
3013
3014static int platform_gpio_count(struct device *dev, const char *con_id)
3015{
3016 struct gpiod_lookup_table *table;
3017 struct gpiod_lookup *p;
3018 unsigned int count = 0;
3019
3020 table = gpiod_find_lookup_table(dev);
3021 if (!table)
3022 return -ENOENT;
3023
3024 for (p = &table->table[0]; p->chip_label; p++) {
3025 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
3026 (!con_id && !p->con_id))
3027 count++;
3028 }
3029 if (!count)
3030 return -ENOENT;
3031
3032 return count;
3033}
3034
3035/**
3036 * gpiod_count - return the number of GPIOs associated with a device / function
3037 * or -ENOENT if no GPIO has been assigned to the requested function
3038 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3039 * @con_id: function within the GPIO consumer
3040 */
3041int gpiod_count(struct device *dev, const char *con_id)
3042{
3043 int count = -ENOENT;
3044
3045 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
3046 count = dt_gpio_count(dev, con_id);
3047 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
3048 count = acpi_gpio_count(dev, con_id);
3049
3050 if (count < 0)
3051 count = platform_gpio_count(dev, con_id);
3052
3053 return count;
3054}
3055EXPORT_SYMBOL_GPL(gpiod_count);
3056
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003057/**
Thierry Reding08791622014-04-25 16:54:22 +02003058 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09003059 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003060 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003061 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003062 *
3063 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003064 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07003065 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003066 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003067struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003068 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003069{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003070 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003071}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003072EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003073
3074/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02003075 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
3076 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3077 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003078 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02003079 *
3080 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
3081 * the requested function it will return NULL. This is convenient for drivers
3082 * that need to handle optional GPIOs.
3083 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003084struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003085 const char *con_id,
3086 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003087{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003088 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003089}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003090EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003091
Benoit Parrotf625d462015-02-02 11:44:44 -06003092
3093/**
3094 * gpiod_configure_flags - helper function to configure a given GPIO
3095 * @desc: gpio whose value will be assigned
3096 * @con_id: function within the GPIO consumer
Johan Hovold85b03b32016-07-03 18:32:05 +02003097 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3098 * of_get_gpio_hog()
Benoit Parrotf625d462015-02-02 11:44:44 -06003099 * @dflags: gpiod_flags - optional GPIO initialization flags
3100 *
3101 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
3102 * requested function and/or index, or another IS_ERR() code if an error
3103 * occurred while trying to acquire the GPIO.
3104 */
3105static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Johan Hovold85b03b32016-07-03 18:32:05 +02003106 unsigned long lflags, enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06003107{
3108 int status;
3109
Johan Hovold85b03b32016-07-03 18:32:05 +02003110 if (lflags & GPIO_ACTIVE_LOW)
3111 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
3112 if (lflags & GPIO_OPEN_DRAIN)
3113 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3114 if (lflags & GPIO_OPEN_SOURCE)
3115 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
3116
Benoit Parrotf625d462015-02-02 11:44:44 -06003117 /* No particular flag request, return here... */
3118 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
3119 pr_debug("no flags found for %s\n", con_id);
3120 return 0;
3121 }
3122
3123 /* Process flags */
3124 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
3125 status = gpiod_direction_output(desc,
3126 dflags & GPIOD_FLAGS_BIT_DIR_VAL);
3127 else
3128 status = gpiod_direction_input(desc);
3129
3130 return status;
3131}
3132
Thierry Reding29a1f2332014-04-25 17:10:06 +02003133/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003134 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02003135 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003136 * @con_id: function within the GPIO consumer
3137 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003138 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003139 *
3140 * This variant of gpiod_get() allows to access GPIOs other than the first
3141 * defined one for functions that define several GPIOs.
3142 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003143 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
3144 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07003145 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003146 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003147struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003148 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003149 unsigned int idx,
3150 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003151{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003152 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003153 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003154 enum gpio_lookup_flags lookupflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003155
3156 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
3157
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003158 if (dev) {
3159 /* Using device tree? */
3160 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
3161 dev_dbg(dev, "using device tree for GPIO lookup\n");
3162 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
3163 } else if (ACPI_COMPANION(dev)) {
3164 dev_dbg(dev, "using ACPI for GPIO lookup\n");
Dmitry Torokhov25487532016-03-24 10:50:25 -07003165 desc = acpi_find_gpio(dev, con_id, idx, flags, &lookupflags);
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01003166 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09003167 }
3168
3169 /*
3170 * Either we are not using DT or ACPI, or their lookup did not return
3171 * a result. In that case, use platform lookup as a fallback.
3172 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09003173 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04003174 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003175 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003176 }
3177
3178 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02003179 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003180 return desc;
3181 }
3182
3183 status = gpiod_request(desc, con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003184 if (status < 0)
3185 return ERR_PTR(status);
3186
Johan Hovold85b03b32016-07-03 18:32:05 +02003187 status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003188 if (status < 0) {
3189 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
3190 gpiod_put(desc);
3191 return ERR_PTR(status);
3192 }
3193
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003194 return desc;
3195}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003196EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003197
3198/**
Mika Westerberg40b73182014-10-21 13:33:59 +02003199 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
3200 * @fwnode: handle of the firmware node
3201 * @propname: name of the firmware property representing the GPIO
3202 *
3203 * This function can be used for drivers that get their configuration
3204 * from firmware.
3205 *
3206 * Function properly finds the corresponding GPIO using whatever is the
3207 * underlying firmware interface and then makes sure that the GPIO
3208 * descriptor is requested before it is returned to the caller.
3209 *
3210 * In case of error an ERR_PTR() is returned.
3211 */
3212struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
3213 const char *propname)
3214{
3215 struct gpio_desc *desc = ERR_PTR(-ENODEV);
3216 bool active_low = false;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003217 bool single_ended = false;
Mika Westerberg40b73182014-10-21 13:33:59 +02003218 int ret;
3219
3220 if (!fwnode)
3221 return ERR_PTR(-EINVAL);
3222
3223 if (is_of_node(fwnode)) {
3224 enum of_gpio_flags flags;
3225
Alexander Sverdlinc181fb32015-06-22 22:38:53 +02003226 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, 0,
Mika Westerberg40b73182014-10-21 13:33:59 +02003227 &flags);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003228 if (!IS_ERR(desc)) {
Mika Westerberg40b73182014-10-21 13:33:59 +02003229 active_low = flags & OF_GPIO_ACTIVE_LOW;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003230 single_ended = flags & OF_GPIO_SINGLE_ENDED;
3231 }
Mika Westerberg40b73182014-10-21 13:33:59 +02003232 } else if (is_acpi_node(fwnode)) {
3233 struct acpi_gpio_info info;
3234
Rafael J. Wysocki504a3372015-08-27 04:42:33 +02003235 desc = acpi_node_get_gpiod(fwnode, propname, 0, &info);
Mika Westerberg40b73182014-10-21 13:33:59 +02003236 if (!IS_ERR(desc))
Christophe RICARD52044722015-12-23 23:25:34 +01003237 active_low = info.polarity == GPIO_ACTIVE_LOW;
Mika Westerberg40b73182014-10-21 13:33:59 +02003238 }
3239
3240 if (IS_ERR(desc))
3241 return desc;
3242
Johan Hovold85b03b32016-07-03 18:32:05 +02003243 ret = gpiod_request(desc, NULL);
3244 if (ret)
3245 return ERR_PTR(ret);
3246
Mika Westerberg40b73182014-10-21 13:33:59 +02003247 if (active_low)
3248 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
3249
Laurent Pinchart90b665f2015-10-13 00:20:21 +03003250 if (single_ended) {
3251 if (active_low)
3252 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3253 else
3254 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
3255 }
3256
Mika Westerberg40b73182014-10-21 13:33:59 +02003257 return desc;
3258}
3259EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
3260
3261/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02003262 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
3263 * function
3264 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3265 * @con_id: function within the GPIO consumer
3266 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003267 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02003268 *
3269 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
3270 * specified index was assigned to the requested function it will return NULL.
3271 * This is convenient for drivers that need to handle optional GPIOs.
3272 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003273struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02003274 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003275 unsigned int index,
3276 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02003277{
3278 struct gpio_desc *desc;
3279
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09003280 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003281 if (IS_ERR(desc)) {
3282 if (PTR_ERR(desc) == -ENOENT)
3283 return NULL;
3284 }
3285
3286 return desc;
3287}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01003288EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02003289
3290/**
Benoit Parrotf625d462015-02-02 11:44:44 -06003291 * gpiod_hog - Hog the specified GPIO desc given the provided flags
3292 * @desc: gpio whose value will be assigned
3293 * @name: gpio line name
3294 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
3295 * of_get_gpio_hog()
3296 * @dflags: gpiod_flags - optional GPIO initialization flags
3297 */
3298int gpiod_hog(struct gpio_desc *desc, const char *name,
3299 unsigned long lflags, enum gpiod_flags dflags)
3300{
3301 struct gpio_chip *chip;
3302 struct gpio_desc *local_desc;
3303 int hwnum;
3304 int status;
3305
3306 chip = gpiod_to_chip(desc);
3307 hwnum = gpio_chip_hwgpio(desc);
3308
3309 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
3310 if (IS_ERR(local_desc)) {
Laxman Dewanganc31a5712016-03-11 19:13:21 +05303311 status = PTR_ERR(local_desc);
3312 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
3313 name, chip->label, hwnum, status);
3314 return status;
Benoit Parrotf625d462015-02-02 11:44:44 -06003315 }
3316
Johan Hovold85b03b32016-07-03 18:32:05 +02003317 status = gpiod_configure_flags(desc, name, lflags, dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -06003318 if (status < 0) {
Laxman Dewanganc31a5712016-03-11 19:13:21 +05303319 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed, %d\n",
3320 name, chip->label, hwnum, status);
Benoit Parrotf625d462015-02-02 11:44:44 -06003321 gpiochip_free_own_desc(desc);
3322 return status;
3323 }
3324
3325 /* Mark GPIO as hogged so it can be identified and removed later */
3326 set_bit(FLAG_IS_HOGGED, &desc->flags);
3327
3328 pr_info("GPIO line %d (%s) hogged as %s%s\n",
3329 desc_to_gpio(desc), name,
3330 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
3331 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
3332 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
3333
3334 return 0;
3335}
3336
3337/**
3338 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
3339 * @chip: gpio chip to act on
3340 *
3341 * This is only used by of_gpiochip_remove to free hogged gpios
3342 */
3343static void gpiochip_free_hogs(struct gpio_chip *chip)
3344{
3345 int id;
3346
3347 for (id = 0; id < chip->ngpio; id++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01003348 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
3349 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
Benoit Parrotf625d462015-02-02 11:44:44 -06003350 }
3351}
3352
3353/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003354 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
3355 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3356 * @con_id: function within the GPIO consumer
3357 * @flags: optional GPIO initialization flags
3358 *
3359 * This function acquires all the GPIOs defined under a given function.
3360 *
3361 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
3362 * no GPIO has been assigned to the requested function, or another IS_ERR()
3363 * code if an error occurred while trying to acquire the GPIOs.
3364 */
3365struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
3366 const char *con_id,
3367 enum gpiod_flags flags)
3368{
3369 struct gpio_desc *desc;
3370 struct gpio_descs *descs;
3371 int count;
3372
3373 count = gpiod_count(dev, con_id);
3374 if (count < 0)
3375 return ERR_PTR(count);
3376
3377 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
3378 GFP_KERNEL);
3379 if (!descs)
3380 return ERR_PTR(-ENOMEM);
3381
3382 for (descs->ndescs = 0; descs->ndescs < count; ) {
3383 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
3384 if (IS_ERR(desc)) {
3385 gpiod_put_array(descs);
3386 return ERR_CAST(desc);
3387 }
3388 descs->desc[descs->ndescs] = desc;
3389 descs->ndescs++;
3390 }
3391 return descs;
3392}
3393EXPORT_SYMBOL_GPL(gpiod_get_array);
3394
3395/**
3396 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
3397 * function
3398 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3399 * @con_id: function within the GPIO consumer
3400 * @flags: optional GPIO initialization flags
3401 *
3402 * This is equivalent to gpiod_get_array(), except that when no GPIO was
3403 * assigned to the requested function it will return NULL.
3404 */
3405struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
3406 const char *con_id,
3407 enum gpiod_flags flags)
3408{
3409 struct gpio_descs *descs;
3410
3411 descs = gpiod_get_array(dev, con_id, flags);
3412 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
3413 return NULL;
3414
3415 return descs;
3416}
3417EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
3418
3419/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003420 * gpiod_put - dispose of a GPIO descriptor
3421 * @desc: GPIO descriptor to dispose of
3422 *
3423 * No descriptor can be used after gpiod_put() has been called on it.
3424 */
3425void gpiod_put(struct gpio_desc *desc)
3426{
3427 gpiod_free(desc);
3428}
3429EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08003430
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003431/**
3432 * gpiod_put_array - dispose of multiple GPIO descriptors
3433 * @descs: struct gpio_descs containing an array of descriptors
3434 */
3435void gpiod_put_array(struct gpio_descs *descs)
3436{
3437 unsigned int i;
3438
3439 for (i = 0; i < descs->ndescs; i++)
3440 gpiod_put(descs->desc[i]);
3441
3442 kfree(descs);
3443}
3444EXPORT_SYMBOL_GPL(gpiod_put_array);
3445
Linus Walleij3c702e92015-10-21 15:29:53 +02003446static int __init gpiolib_dev_init(void)
3447{
3448 int ret;
3449
3450 /* Register GPIO sysfs bus */
3451 ret = bus_register(&gpio_bus_type);
3452 if (ret < 0) {
3453 pr_err("gpiolib: could not register GPIO bus type\n");
3454 return ret;
3455 }
3456
3457 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
3458 if (ret < 0) {
3459 pr_err("gpiolib: failed to allocate char dev region\n");
3460 bus_unregister(&gpio_bus_type);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07003461 } else {
3462 gpiolib_initialized = true;
3463 gpiochip_setup_devs();
Linus Walleij3c702e92015-10-21 15:29:53 +02003464 }
3465 return ret;
3466}
3467core_initcall(gpiolib_dev_init);
3468
David Brownelld2876d02008-02-04 22:28:20 -08003469#ifdef CONFIG_DEBUG_FS
3470
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003471static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
David Brownelld2876d02008-02-04 22:28:20 -08003472{
3473 unsigned i;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003474 struct gpio_chip *chip = gdev->chip;
3475 unsigned gpio = gdev->base;
3476 struct gpio_desc *gdesc = &gdev->descs[0];
David Brownelld2876d02008-02-04 22:28:20 -08003477 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02003478 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08003479
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003480 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02003481 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
3482 if (gdesc->name) {
3483 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
3484 gpio, gdesc->name);
3485 }
David Brownelld2876d02008-02-04 22:28:20 -08003486 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02003487 }
David Brownelld2876d02008-02-04 22:28:20 -08003488
Alexandre Courbot372e7222013-02-03 01:29:29 +09003489 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08003490 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02003491 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Markus Pargmannced433e2015-08-14 16:11:02 +02003492 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
3493 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08003494 is_out ? "out" : "in ",
3495 chip->get
3496 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02003497 : "? ",
3498 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08003499 seq_printf(s, "\n");
3500 }
3501}
3502
Thierry Redingf9c4a312012-04-12 13:26:01 +02003503static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08003504{
Grant Likely362432a2013-02-09 09:41:49 +00003505 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02003506 struct gpio_device *gdev = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003507 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003508
3509 s->private = "";
3510
Grant Likely362432a2013-02-09 09:41:49 +00003511 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003512 list_for_each_entry(gdev, &gpio_devices, list)
Grant Likely362432a2013-02-09 09:41:49 +00003513 if (index-- == 0) {
3514 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003515 return gdev;
Grant Likely362432a2013-02-09 09:41:49 +00003516 }
3517 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003518
3519 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003520}
3521
3522static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
3523{
Grant Likely362432a2013-02-09 09:41:49 +00003524 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02003525 struct gpio_device *gdev = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003526 void *ret = NULL;
3527
Grant Likely362432a2013-02-09 09:41:49 +00003528 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003529 if (list_is_last(&gdev->list, &gpio_devices))
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003530 ret = NULL;
3531 else
Linus Walleijff2b1352015-10-20 11:10:38 +02003532 ret = list_entry(gdev->list.next, struct gpio_device, list);
Grant Likely362432a2013-02-09 09:41:49 +00003533 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02003534
3535 s->private = "\n";
3536 ++*pos;
3537
3538 return ret;
3539}
3540
3541static void gpiolib_seq_stop(struct seq_file *s, void *v)
3542{
3543}
3544
3545static int gpiolib_seq_show(struct seq_file *s, void *v)
3546{
Linus Walleijff2b1352015-10-20 11:10:38 +02003547 struct gpio_device *gdev = v;
3548 struct gpio_chip *chip = gdev->chip;
3549 struct device *parent;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003550
Linus Walleijff2b1352015-10-20 11:10:38 +02003551 if (!chip) {
3552 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
3553 dev_name(&gdev->dev));
3554 return 0;
3555 }
3556
3557 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
3558 dev_name(&gdev->dev),
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003559 gdev->base, gdev->base + gdev->ngpio - 1);
Linus Walleijff2b1352015-10-20 11:10:38 +02003560 parent = chip->parent;
3561 if (parent)
3562 seq_printf(s, ", parent: %s/%s",
3563 parent->bus ? parent->bus->name : "no-bus",
3564 dev_name(parent));
Thierry Redingf9c4a312012-04-12 13:26:01 +02003565 if (chip->label)
3566 seq_printf(s, ", %s", chip->label);
3567 if (chip->can_sleep)
3568 seq_printf(s, ", can sleep");
3569 seq_printf(s, ":\n");
3570
3571 if (chip->dbg_show)
3572 chip->dbg_show(s, chip);
3573 else
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003574 gpiolib_dbg_show(s, gdev);
Thierry Redingf9c4a312012-04-12 13:26:01 +02003575
David Brownelld2876d02008-02-04 22:28:20 -08003576 return 0;
3577}
3578
Thierry Redingf9c4a312012-04-12 13:26:01 +02003579static const struct seq_operations gpiolib_seq_ops = {
3580 .start = gpiolib_seq_start,
3581 .next = gpiolib_seq_next,
3582 .stop = gpiolib_seq_stop,
3583 .show = gpiolib_seq_show,
3584};
3585
David Brownelld2876d02008-02-04 22:28:20 -08003586static int gpiolib_open(struct inode *inode, struct file *file)
3587{
Thierry Redingf9c4a312012-04-12 13:26:01 +02003588 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08003589}
3590
Alexey Dobriyan828c0952009-10-01 15:43:56 -07003591static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02003592 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08003593 .open = gpiolib_open,
3594 .read = seq_read,
3595 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02003596 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08003597};
3598
3599static int __init gpiolib_debugfs_init(void)
3600{
3601 /* /sys/kernel/debug/gpio */
3602 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
3603 NULL, NULL, &gpiolib_operations);
3604 return 0;
3605}
3606subsys_initcall(gpiolib_debugfs_init);
3607
3608#endif /* DEBUG_FS */