blob: 5f2e73e99fa1f479f0fb61478263abe1e507437b [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 Walleijff2b1352015-10-20 11:10:38 +020019#include <linux/idr.h>
Linus Walleij3c702e92015-10-21 15:29:53 +020020#include <linux/cdev.h>
21#include <linux/fs.h>
22#include <linux/uaccess.h>
Linus Walleij8b92e172016-05-27 14:24:04 +020023#include <linux/compat.h>
Linus Walleijd7c51b42016-04-26 10:35:29 +020024#include <linux/anon_inodes.h>
Linus Walleij3c702e92015-10-21 15:29:53 +020025#include <uapi/linux/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080026
Mika Westerberg664e3e52014-01-08 12:40:54 +020027#include "gpiolib.h"
28
Uwe Kleine-König3f397c212011-05-20 00:40:19 -060029#define CREATE_TRACE_POINTS
30#include <trace/events/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080031
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070032/* Implementation infrastructure for GPIO interfaces.
David Brownelld2876d02008-02-04 22:28:20 -080033 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070034 * The GPIO programming interface allows for inlining speed-critical
35 * get/set operations for common cases, so that access to SOC-integrated
36 * GPIOs can sometimes cost only an instruction or two per bit.
David Brownelld2876d02008-02-04 22:28:20 -080037 */
38
39
40/* When debugging, extend minimal trust to callers and platform code.
41 * Also emit diagnostic messages that may help initial bringup, when
42 * board setup or driver bugs are most common.
43 *
44 * Otherwise, minimize overhead in what may be bitbanging codepaths.
45 */
46#ifdef DEBUG
47#define extra_checks 1
48#else
49#define extra_checks 0
50#endif
51
Linus Walleijff2b1352015-10-20 11:10:38 +020052/* Device and char device-related information */
53static DEFINE_IDA(gpio_ida);
Linus Walleij3c702e92015-10-21 15:29:53 +020054static dev_t gpio_devt;
55#define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
56static struct bus_type gpio_bus_type = {
57 .name = "gpio",
58};
Linus Walleijff2b1352015-10-20 11:10:38 +020059
David Brownelld2876d02008-02-04 22:28:20 -080060/* gpio_lock prevents conflicts during gpio_desc[] table updates.
61 * While any GPIO is requested, its gpio_chip is not removable;
62 * each GPIO's "requested" flag serves as a lock and refcount.
63 */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090064DEFINE_SPINLOCK(gpio_lock);
David Brownelld2876d02008-02-04 22:28:20 -080065
Alexandre Courbotbae48da2013-10-17 10:21:38 -070066static DEFINE_MUTEX(gpio_lookup_lock);
67static LIST_HEAD(gpio_lookup_list);
Linus Walleijff2b1352015-10-20 11:10:38 +020068LIST_HEAD(gpio_devices);
Johan Hovold6d867502015-05-04 17:23:25 +020069
70static void gpiochip_free_hogs(struct gpio_chip *chip);
71static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
72
Guenter Roeck159f3cd2016-03-31 08:11:30 -070073static bool gpiolib_initialized;
Johan Hovold6d867502015-05-04 17:23:25 +020074
David Brownelld2876d02008-02-04 22:28:20 -080075static inline void desc_set_label(struct gpio_desc *d, const char *label)
76{
David Brownelld2876d02008-02-04 22:28:20 -080077 d->label = label;
David Brownelld2876d02008-02-04 22:28:20 -080078}
79
Alexandre Courbot372e7222013-02-03 01:29:29 +090080/**
81 * Convert a GPIO number to its descriptor
82 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070083struct gpio_desc *gpio_to_desc(unsigned gpio)
Alexandre Courbot372e7222013-02-03 01:29:29 +090084{
Linus Walleijff2b1352015-10-20 11:10:38 +020085 struct gpio_device *gdev;
Alexandre Courbot14e85c02014-11-19 16:51:27 +090086 unsigned long flags;
87
88 spin_lock_irqsave(&gpio_lock, flags);
89
Linus Walleijff2b1352015-10-20 11:10:38 +020090 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +010091 if (gdev->base <= gpio &&
92 gdev->base + gdev->ngpio > gpio) {
Alexandre Courbot14e85c02014-11-19 16:51:27 +090093 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +010094 return &gdev->descs[gpio - gdev->base];
Alexandre Courbot14e85c02014-11-19 16:51:27 +090095 }
96 }
97
98 spin_unlock_irqrestore(&gpio_lock, flags);
99
Alexandre Courbot0e9a5ed2014-12-02 23:15:05 +0900100 if (!gpio_is_valid(gpio))
101 WARN(1, "invalid GPIO %d\n", gpio);
102
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900103 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900104}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700105EXPORT_SYMBOL_GPL(gpio_to_desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900106
107/**
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900108 * Get the GPIO descriptor corresponding to the given hw number for this chip.
Linus Walleijd468bf92013-09-24 11:54:38 +0200109 */
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900110struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
111 u16 hwnum)
Linus Walleijd468bf92013-09-24 11:54:38 +0200112{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100113 struct gpio_device *gdev = chip->gpiodev;
114
115 if (hwnum >= gdev->ngpio)
Alexandre Courbotb7d0a282013-12-03 12:31:11 +0900116 return ERR_PTR(-EINVAL);
Linus Walleijd468bf92013-09-24 11:54:38 +0200117
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100118 return &gdev->descs[hwnum];
Linus Walleijd468bf92013-09-24 11:54:38 +0200119}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900120
121/**
122 * Convert a GPIO descriptor to the integer namespace.
123 * This should disappear in the future but is needed since we still
124 * use GPIO numbers for error messages and sysfs nodes
125 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700126int desc_to_gpio(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900127{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100128 return desc->gdev->base + (desc - &desc->gdev->descs[0]);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900129}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700130EXPORT_SYMBOL_GPL(desc_to_gpio);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900131
132
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700133/**
134 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
135 * @desc: descriptor to return the chip of
136 */
137struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900138{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100139 if (!desc || !desc->gdev || !desc->gdev->chip)
140 return NULL;
141 return desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900142}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700143EXPORT_SYMBOL_GPL(gpiod_to_chip);
David Brownelld2876d02008-02-04 22:28:20 -0800144
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700145/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
146static int gpiochip_find_base(int ngpio)
147{
Linus Walleijff2b1352015-10-20 11:10:38 +0200148 struct gpio_device *gdev;
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900149 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700150
Linus Walleijff2b1352015-10-20 11:10:38 +0200151 list_for_each_entry_reverse(gdev, &gpio_devices, list) {
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900152 /* found a free space? */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100153 if (gdev->base + gdev->ngpio <= base)
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900154 break;
155 else
156 /* nope, check the space right before the chip */
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100157 base = gdev->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700158 }
159
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900160 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700161 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900162 return base;
163 } else {
164 pr_err("%s: cannot find free range\n", __func__);
165 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700166 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700167}
168
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700169/**
170 * gpiod_get_direction - return the current direction of a GPIO
171 * @desc: GPIO to get the direction of
172 *
173 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
174 *
175 * This function may sleep if gpiod_cansleep() is true.
176 */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900177int gpiod_get_direction(struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300178{
179 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900180 unsigned offset;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300181 int status = -EINVAL;
182
Alexandre Courbot372e7222013-02-03 01:29:29 +0900183 chip = gpiod_to_chip(desc);
184 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300185
186 if (!chip->get_direction)
187 return status;
188
Alexandre Courbot372e7222013-02-03 01:29:29 +0900189 status = chip->get_direction(chip, offset);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300190 if (status > 0) {
191 /* GPIOF_DIR_IN, or other positive */
192 status = 1;
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900193 clear_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300194 }
195 if (status == 0) {
196 /* GPIOF_DIR_OUT */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900197 set_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300198 }
199 return status;
200}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700201EXPORT_SYMBOL_GPL(gpiod_get_direction);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300202
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900203/*
204 * Add a new chip to the global chips list, keeping the list of chips sorted
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800205 * by range(means [base, base + ngpio - 1]) order.
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900206 *
207 * Return -EBUSY if the new chip overlaps with some other chip's integer
208 * space.
209 */
Linus Walleijff2b1352015-10-20 11:10:38 +0200210static int gpiodev_add_to_list(struct gpio_device *gdev)
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900211{
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800212 struct gpio_device *prev, *next;
Linus Walleijff2b1352015-10-20 11:10:38 +0200213
214 if (list_empty(&gpio_devices)) {
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800215 /* initial entry in list */
Linus Walleijff2b1352015-10-20 11:10:38 +0200216 list_add_tail(&gdev->list, &gpio_devices);
Sudip Mukherjeee28ecca2015-12-27 19:06:50 +0530217 return 0;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900218 }
219
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800220 next = list_entry(gpio_devices.next, struct gpio_device, list);
221 if (gdev->base + gdev->ngpio <= next->base) {
222 /* add before first entry */
223 list_add(&gdev->list, &gpio_devices);
Julien Grossholtz96098df2016-01-07 16:46:45 -0500224 return 0;
225 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900226
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800227 prev = list_entry(gpio_devices.prev, struct gpio_device, list);
228 if (prev->base + prev->ngpio <= gdev->base) {
229 /* add behind last entry */
230 list_add_tail(&gdev->list, &gpio_devices);
231 return 0;
232 }
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800233
Bamvor Jian Zhanga961f9b2016-02-26 22:37:14 +0800234 list_for_each_entry_safe(prev, next, &gpio_devices, list) {
235 /* at the end of the list */
236 if (&next->list == &gpio_devices)
237 break;
238
239 /* add between prev and next */
240 if (prev->base + prev->ngpio <= gdev->base
241 && gdev->base + gdev->ngpio <= next->base) {
242 list_add(&gdev->list, &prev->list);
243 return 0;
244 }
245 }
246
247 dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n");
248 return -EBUSY;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900249}
250
Linus Walleijf881bab02015-09-23 16:20:43 -0700251/**
252 * Convert a GPIO name to its descriptor
253 */
254static struct gpio_desc *gpio_name_to_desc(const char * const name)
255{
Linus Walleijff2b1352015-10-20 11:10:38 +0200256 struct gpio_device *gdev;
Linus Walleijf881bab02015-09-23 16:20:43 -0700257 unsigned long flags;
258
259 spin_lock_irqsave(&gpio_lock, flags);
260
Linus Walleijff2b1352015-10-20 11:10:38 +0200261 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijf881bab02015-09-23 16:20:43 -0700262 int i;
263
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100264 for (i = 0; i != gdev->ngpio; ++i) {
265 struct gpio_desc *desc = &gdev->descs[i];
Linus Walleijf881bab02015-09-23 16:20:43 -0700266
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100267 if (!desc->name || !name)
Linus Walleijf881bab02015-09-23 16:20:43 -0700268 continue;
269
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100270 if (!strcmp(desc->name, name)) {
Linus Walleijf881bab02015-09-23 16:20:43 -0700271 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100272 return desc;
Linus Walleijf881bab02015-09-23 16:20:43 -0700273 }
274 }
275 }
276
277 spin_unlock_irqrestore(&gpio_lock, flags);
278
279 return NULL;
280}
281
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200282/*
283 * Takes the names from gc->names and checks if they are all unique. If they
284 * are, they are assigned to their gpio descriptors.
285 *
Bamvor Jian Zhanged379152015-11-14 16:43:20 +0800286 * Warning if one of the names is already used for a different GPIO.
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200287 */
288static int gpiochip_set_desc_names(struct gpio_chip *gc)
289{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100290 struct gpio_device *gdev = gc->gpiodev;
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200291 int i;
292
293 if (!gc->names)
294 return 0;
295
296 /* First check all names if they are unique */
297 for (i = 0; i != gc->ngpio; ++i) {
298 struct gpio_desc *gpio;
299
300 gpio = gpio_name_to_desc(gc->names[i]);
Linus Walleijf881bab02015-09-23 16:20:43 -0700301 if (gpio)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100302 dev_warn(&gdev->dev,
Linus Walleij34ffd852015-10-20 11:31:54 +0200303 "Detected name collision for GPIO name '%s'\n",
Linus Walleijf881bab02015-09-23 16:20:43 -0700304 gc->names[i]);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200305 }
306
307 /* Then add all names to the GPIO descriptors */
308 for (i = 0; i != gc->ngpio; ++i)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100309 gdev->descs[i].name = gc->names[i];
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200310
311 return 0;
312}
313
Linus Walleijd7c51b42016-04-26 10:35:29 +0200314/*
315 * GPIO line handle management
316 */
317
318/**
319 * struct linehandle_state - contains the state of a userspace handle
320 * @gdev: the GPIO device the handle pertains to
321 * @label: consumer label used to tag descriptors
322 * @descs: the GPIO descriptors held by this handle
323 * @numdescs: the number of descriptors held in the descs array
324 */
325struct linehandle_state {
326 struct gpio_device *gdev;
327 const char *label;
328 struct gpio_desc *descs[GPIOHANDLES_MAX];
329 u32 numdescs;
330};
331
332static long linehandle_ioctl(struct file *filep, unsigned int cmd,
333 unsigned long arg)
334{
335 struct linehandle_state *lh = filep->private_data;
336 void __user *ip = (void __user *)arg;
337 struct gpiohandle_data ghd;
338 int i;
339
340 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
341 int val;
342
343 /* TODO: check if descriptors are really input */
344 for (i = 0; i < lh->numdescs; i++) {
345 val = gpiod_get_value_cansleep(lh->descs[i]);
346 if (val < 0)
347 return val;
348 ghd.values[i] = val;
349 }
350
351 if (copy_to_user(ip, &ghd, sizeof(ghd)))
352 return -EFAULT;
353
354 return 0;
355 } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
356 int vals[GPIOHANDLES_MAX];
357
358 /* TODO: check if descriptors are really output */
359 if (copy_from_user(&ghd, ip, sizeof(ghd)))
360 return -EFAULT;
361
362 /* Clamp all values to [0,1] */
363 for (i = 0; i < lh->numdescs; i++)
364 vals[i] = !!ghd.values[i];
365
366 /* Reuse the array setting function */
367 gpiod_set_array_value_complex(false,
368 true,
369 lh->numdescs,
370 lh->descs,
371 vals);
372 return 0;
373 }
374 return -EINVAL;
375}
376
377#ifdef CONFIG_COMPAT
378static long linehandle_ioctl_compat(struct file *filep, unsigned int cmd,
379 unsigned long arg)
380{
381 return linehandle_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
382}
383#endif
384
385static int linehandle_release(struct inode *inode, struct file *filep)
386{
387 struct linehandle_state *lh = filep->private_data;
388 struct gpio_device *gdev = lh->gdev;
389 int i;
390
391 for (i = 0; i < lh->numdescs; i++)
392 gpiod_free(lh->descs[i]);
393 kfree(lh->label);
394 kfree(lh);
395 put_device(&gdev->dev);
396 return 0;
397}
398
399static const struct file_operations linehandle_fileops = {
400 .release = linehandle_release,
401 .owner = THIS_MODULE,
402 .llseek = noop_llseek,
403 .unlocked_ioctl = linehandle_ioctl,
404#ifdef CONFIG_COMPAT
405 .compat_ioctl = linehandle_ioctl_compat,
406#endif
407};
408
409static int linehandle_create(struct gpio_device *gdev, void __user *ip)
410{
411 struct gpiohandle_request handlereq;
412 struct linehandle_state *lh;
413 int fd, i, ret;
414
415 if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
416 return -EFAULT;
417 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
418 return -EINVAL;
419
420 lh = kzalloc(sizeof(*lh), GFP_KERNEL);
421 if (!lh)
422 return -ENOMEM;
423 lh->gdev = gdev;
424 get_device(&gdev->dev);
425
426 /* Make sure this is terminated */
427 handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
428 if (strlen(handlereq.consumer_label)) {
429 lh->label = kstrdup(handlereq.consumer_label,
430 GFP_KERNEL);
431 if (!lh->label) {
432 ret = -ENOMEM;
433 goto out_free_lh;
434 }
435 }
436
437 /* Request each GPIO */
438 for (i = 0; i < handlereq.lines; i++) {
439 u32 offset = handlereq.lineoffsets[i];
440 u32 lflags = handlereq.flags;
441 struct gpio_desc *desc;
442
443 desc = &gdev->descs[offset];
444 ret = gpiod_request(desc, lh->label);
445 if (ret)
446 goto out_free_descs;
447 lh->descs[i] = desc;
448
449 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
450 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
451 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
452 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
453 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
454 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
455
456 /*
457 * Lines have to be requested explicitly for input
458 * or output, else the line will be treated "as is".
459 */
460 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
461 int val = !!handlereq.default_values[i];
462
463 ret = gpiod_direction_output(desc, val);
464 if (ret)
465 goto out_free_descs;
466 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
467 ret = gpiod_direction_input(desc);
468 if (ret)
469 goto out_free_descs;
470 }
471 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
472 offset);
473 }
474 lh->numdescs = handlereq.lines;
475
476 fd = anon_inode_getfd("gpio-linehandle",
477 &linehandle_fileops,
478 lh,
479 O_RDONLY | O_CLOEXEC);
480 if (fd < 0) {
481 ret = fd;
482 goto out_free_descs;
483 }
484
485 handlereq.fd = fd;
486 if (copy_to_user(ip, &handlereq, sizeof(handlereq)))
487 return -EFAULT;
488
489 dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
490 lh->numdescs);
491
492 return 0;
493
494out_free_descs:
495 for (; i >= 0; i--)
496 gpiod_free(lh->descs[i]);
497 kfree(lh->label);
498out_free_lh:
499 kfree(lh);
500 put_device(&gdev->dev);
501 return ret;
502}
503
Linus Walleij3c702e92015-10-21 15:29:53 +0200504/**
505 * gpio_ioctl() - ioctl handler for the GPIO chardev
506 */
507static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
508{
509 struct gpio_device *gdev = filp->private_data;
510 struct gpio_chip *chip = gdev->chip;
Linus Walleij8b92e172016-05-27 14:24:04 +0200511 void __user *ip = (void __user *)arg;
Linus Walleij3c702e92015-10-21 15:29:53 +0200512
513 /* We fail any subsequent ioctl():s when the chip is gone */
514 if (!chip)
515 return -ENODEV;
516
Linus Walleij521a2ad2016-02-12 22:25:22 +0100517 /* Fill in the struct and pass to userspace */
Linus Walleij3c702e92015-10-21 15:29:53 +0200518 if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
Linus Walleij521a2ad2016-02-12 22:25:22 +0100519 struct gpiochip_info chipinfo;
520
Linus Walleij3c702e92015-10-21 15:29:53 +0200521 strncpy(chipinfo.name, dev_name(&gdev->dev),
522 sizeof(chipinfo.name));
523 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
Linus Walleijdf4878e2016-02-12 14:48:23 +0100524 strncpy(chipinfo.label, gdev->label,
525 sizeof(chipinfo.label));
526 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100527 chipinfo.lines = gdev->ngpio;
Linus Walleij3c702e92015-10-21 15:29:53 +0200528 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
529 return -EFAULT;
530 return 0;
Linus Walleij521a2ad2016-02-12 22:25:22 +0100531 } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
532 struct gpioline_info lineinfo;
533 struct gpio_desc *desc;
534
535 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
536 return -EFAULT;
537 if (lineinfo.line_offset > gdev->ngpio)
538 return -EINVAL;
539
540 desc = &gdev->descs[lineinfo.line_offset];
541 if (desc->name) {
542 strncpy(lineinfo.name, desc->name,
543 sizeof(lineinfo.name));
544 lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
545 } else {
546 lineinfo.name[0] = '\0';
547 }
548 if (desc->label) {
Linus Walleij214338e2016-02-25 21:01:48 +0100549 strncpy(lineinfo.consumer, desc->label,
550 sizeof(lineinfo.consumer));
551 lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +0100552 } else {
Linus Walleij214338e2016-02-25 21:01:48 +0100553 lineinfo.consumer[0] = '\0';
Linus Walleij521a2ad2016-02-12 22:25:22 +0100554 }
555
556 /*
557 * Userspace only need to know that the kernel is using
558 * this GPIO so it can't use it.
559 */
560 lineinfo.flags = 0;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100561 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
562 test_bit(FLAG_IS_HOGGED, &desc->flags) ||
563 test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
564 test_bit(FLAG_EXPORT, &desc->flags) ||
565 test_bit(FLAG_SYSFS, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100566 lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100567 if (test_bit(FLAG_IS_OUT, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100568 lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100569 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100570 lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100571 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100572 lineinfo.flags |= GPIOLINE_FLAG_OPEN_DRAIN;
Linus Walleij9d8cc892016-02-22 13:44:53 +0100573 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Linus Walleij521a2ad2016-02-12 22:25:22 +0100574 lineinfo.flags |= GPIOLINE_FLAG_OPEN_SOURCE;
575
576 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
577 return -EFAULT;
578 return 0;
Linus Walleijd7c51b42016-04-26 10:35:29 +0200579 } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
580 return linehandle_create(gdev, ip);
Linus Walleij3c702e92015-10-21 15:29:53 +0200581 }
582 return -EINVAL;
583}
584
Linus Walleij8b92e172016-05-27 14:24:04 +0200585#ifdef CONFIG_COMPAT
586static long gpio_ioctl_compat(struct file *filp, unsigned int cmd,
587 unsigned long arg)
588{
589 return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
590}
591#endif
592
Linus Walleij3c702e92015-10-21 15:29:53 +0200593/**
594 * gpio_chrdev_open() - open the chardev for ioctl operations
595 * @inode: inode for this chardev
596 * @filp: file struct for storing private data
597 * Returns 0 on success
598 */
599static int gpio_chrdev_open(struct inode *inode, struct file *filp)
600{
601 struct gpio_device *gdev = container_of(inode->i_cdev,
602 struct gpio_device, chrdev);
603
604 /* Fail on open if the backing gpiochip is gone */
605 if (!gdev || !gdev->chip)
606 return -ENODEV;
607 get_device(&gdev->dev);
608 filp->private_data = gdev;
609 return 0;
610}
611
612/**
613 * gpio_chrdev_release() - close chardev after ioctl operations
614 * @inode: inode for this chardev
615 * @filp: file struct for storing private data
616 * Returns 0 on success
617 */
618static int gpio_chrdev_release(struct inode *inode, struct file *filp)
619{
620 struct gpio_device *gdev = container_of(inode->i_cdev,
621 struct gpio_device, chrdev);
622
623 if (!gdev)
624 return -ENODEV;
625 put_device(&gdev->dev);
626 return 0;
627}
628
629
630static const struct file_operations gpio_fileops = {
631 .release = gpio_chrdev_release,
632 .open = gpio_chrdev_open,
633 .owner = THIS_MODULE,
634 .llseek = noop_llseek,
635 .unlocked_ioctl = gpio_ioctl,
Linus Walleij8b92e172016-05-27 14:24:04 +0200636#ifdef CONFIG_COMPAT
637 .compat_ioctl = gpio_ioctl_compat,
638#endif
Linus Walleij3c702e92015-10-21 15:29:53 +0200639};
640
Linus Walleijff2b1352015-10-20 11:10:38 +0200641static void gpiodevice_release(struct device *dev)
642{
643 struct gpio_device *gdev = dev_get_drvdata(dev);
644
Linus Walleij3c702e92015-10-21 15:29:53 +0200645 cdev_del(&gdev->chrdev);
Linus Walleijff2b1352015-10-20 11:10:38 +0200646 list_del(&gdev->list);
647 ida_simple_remove(&gpio_ida, gdev->id);
Guenter Roeck476e2fc2016-03-31 08:11:29 -0700648 kfree(gdev->label);
649 kfree(gdev->descs);
Linus Walleij9efd9e62016-02-09 14:27:42 +0100650 kfree(gdev);
Linus Walleijff2b1352015-10-20 11:10:38 +0200651}
652
Guenter Roeck159f3cd2016-03-31 08:11:30 -0700653static int gpiochip_setup_dev(struct gpio_device *gdev)
654{
655 int status;
656
657 cdev_init(&gdev->chrdev, &gpio_fileops);
658 gdev->chrdev.owner = THIS_MODULE;
659 gdev->chrdev.kobj.parent = &gdev->dev.kobj;
660 gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
661 status = cdev_add(&gdev->chrdev, gdev->dev.devt, 1);
662 if (status < 0)
663 chip_warn(gdev->chip, "failed to add char device %d:%d\n",
664 MAJOR(gpio_devt), gdev->id);
665 else
666 chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
667 MAJOR(gpio_devt), gdev->id);
668 status = device_add(&gdev->dev);
669 if (status)
670 goto err_remove_chardev;
671
672 status = gpiochip_sysfs_register(gdev);
673 if (status)
674 goto err_remove_device;
675
676 /* From this point, the .release() function cleans up gpio_device */
677 gdev->dev.release = gpiodevice_release;
678 get_device(&gdev->dev);
679 pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
680 __func__, gdev->base, gdev->base + gdev->ngpio - 1,
681 dev_name(&gdev->dev), gdev->chip->label ? : "generic");
682
683 return 0;
684
685err_remove_device:
686 device_del(&gdev->dev);
687err_remove_chardev:
688 cdev_del(&gdev->chrdev);
689 return status;
690}
691
692static void gpiochip_setup_devs(void)
693{
694 struct gpio_device *gdev;
695 int err;
696
697 list_for_each_entry(gdev, &gpio_devices, list) {
698 err = gpiochip_setup_dev(gdev);
699 if (err)
700 pr_err("%s: Failed to initialize gpio device (%d)\n",
701 dev_name(&gdev->dev), err);
702 }
703}
704
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700705/**
Linus Walleijb08ea352015-12-03 15:14:13 +0100706 * gpiochip_add_data() - register a gpio_chip
David Brownelld2876d02008-02-04 22:28:20 -0800707 * @chip: the chip to register, with chip->base initialized
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900708 * Context: potentially before irqs will work
David Brownelld2876d02008-02-04 22:28:20 -0800709 *
710 * Returns a negative errno if the chip can't be registered, such as
711 * because the chip->base is invalid or already associated with a
712 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700713 *
Linus Walleijb08ea352015-12-03 15:14:13 +0100714 * When gpiochip_add_data() is called very early during boot, so that GPIOs
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +0800715 * can be freely used, the chip->parent device must be registered before
David Brownelld8f388d82008-07-25 01:46:07 -0700716 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
717 * for GPIOs will fail rudely.
718 *
Guenter Roeck159f3cd2016-03-31 08:11:30 -0700719 * gpiochip_add_data() must only be called after gpiolib initialization,
720 * ie after core_initcall().
721 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700722 * If chip->base is negative, this requests dynamic assignment of
723 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -0800724 */
Linus Walleijb08ea352015-12-03 15:14:13 +0100725int gpiochip_add_data(struct gpio_chip *chip, void *data)
David Brownelld2876d02008-02-04 22:28:20 -0800726{
727 unsigned long flags;
728 int status = 0;
Linus Walleijff2b1352015-10-20 11:10:38 +0200729 unsigned i;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700730 int base = chip->base;
Linus Walleijff2b1352015-10-20 11:10:38 +0200731 struct gpio_device *gdev;
David Brownelld2876d02008-02-04 22:28:20 -0800732
Linus Walleijff2b1352015-10-20 11:10:38 +0200733 /*
734 * First: allocate and populate the internal stat container, and
735 * set up the struct device.
736 */
Josh Cartwright969f07b2016-02-17 16:44:15 -0600737 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
Linus Walleijff2b1352015-10-20 11:10:38 +0200738 if (!gdev)
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900739 return -ENOMEM;
Linus Walleij3c702e92015-10-21 15:29:53 +0200740 gdev->dev.bus = &gpio_bus_type;
Linus Walleijff2b1352015-10-20 11:10:38 +0200741 gdev->chip = chip;
742 chip->gpiodev = gdev;
743 if (chip->parent) {
744 gdev->dev.parent = chip->parent;
745 gdev->dev.of_node = chip->parent->of_node;
746 } else {
747#ifdef CONFIG_OF_GPIO
748 /* If the gpiochip has an assigned OF node this takes precedence */
749 if (chip->of_node)
750 gdev->dev.of_node = chip->of_node;
751#endif
752 }
753 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
754 if (gdev->id < 0) {
755 status = gdev->id;
756 goto err_free_gdev;
757 }
758 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
759 device_initialize(&gdev->dev);
760 dev_set_drvdata(&gdev->dev, gdev);
761 if (chip->parent && chip->parent->driver)
762 gdev->owner = chip->parent->driver->owner;
763 else if (chip->owner)
764 /* TODO: remove chip->owner */
765 gdev->owner = chip->owner;
766 else
767 gdev->owner = THIS_MODULE;
David Brownelld2876d02008-02-04 22:28:20 -0800768
Guenter Roeck476e2fc2016-03-31 08:11:29 -0700769 gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100770 if (!gdev->descs) {
Linus Walleijff2b1352015-10-20 11:10:38 +0200771 status = -ENOMEM;
772 goto err_free_gdev;
773 }
774
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +0800775 if (chip->ngpio == 0) {
776 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
Linus Walleijff2b1352015-10-20 11:10:38 +0200777 status = -EINVAL;
Guenter Roeck159f3cd2016-03-31 08:11:30 -0700778 goto err_free_descs;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +0800779 }
Linus Walleijdf4878e2016-02-12 14:48:23 +0100780
781 if (chip->label)
Guenter Roeck476e2fc2016-03-31 08:11:29 -0700782 gdev->label = kstrdup(chip->label, GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +0100783 else
Guenter Roeck476e2fc2016-03-31 08:11:29 -0700784 gdev->label = kstrdup("unknown", GFP_KERNEL);
Linus Walleijdf4878e2016-02-12 14:48:23 +0100785 if (!gdev->label) {
786 status = -ENOMEM;
Guenter Roeck476e2fc2016-03-31 08:11:29 -0700787 goto err_free_descs;
Linus Walleijdf4878e2016-02-12 14:48:23 +0100788 }
789
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100790 gdev->ngpio = chip->ngpio;
Linus Walleij43c54ec2016-02-11 11:37:48 +0100791 gdev->data = data;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +0800792
David Brownelld2876d02008-02-04 22:28:20 -0800793 spin_lock_irqsave(&gpio_lock, flags);
794
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100795 /*
796 * TODO: this allocates a Linux GPIO number base in the global
797 * GPIO numberspace for this chip. In the long run we want to
798 * get *rid* of this numberspace and use only descriptors, but
799 * it may be a pipe dream. It will not happen before we get rid
800 * of the sysfs interface anyways.
801 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700802 if (base < 0) {
803 base = gpiochip_find_base(chip->ngpio);
804 if (base < 0) {
805 status = base;
Johan Hovold225fce82015-01-12 17:12:25 +0100806 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -0700807 goto err_free_label;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700808 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100809 /*
810 * TODO: it should not be necessary to reflect the assigned
811 * base outside of the GPIO subsystem. Go over drivers and
812 * see if anyone makes use of this, else drop this and assign
813 * a poison instead.
814 */
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700815 chip->base = base;
816 }
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100817 gdev->base = base;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700818
Linus Walleijff2b1352015-10-20 11:10:38 +0200819 status = gpiodev_add_to_list(gdev);
Johan Hovold05aa5202015-01-12 17:12:26 +0100820 if (status) {
821 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -0700822 goto err_free_label;
Johan Hovold05aa5202015-01-12 17:12:26 +0100823 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900824
Linus Walleij545ebd92016-05-30 17:11:59 +0200825 spin_unlock_irqrestore(&gpio_lock, flags);
826
Linus Walleijff2b1352015-10-20 11:10:38 +0200827 for (i = 0; i < chip->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100828 struct gpio_desc *desc = &gdev->descs[i];
David Brownelld8f388d82008-07-25 01:46:07 -0700829
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100830 desc->gdev = gdev;
Linus Walleij72d32002016-04-28 13:33:59 +0200831 /*
832 * REVISIT: most hardware initializes GPIOs as inputs
833 * (often with pullups enabled) so power usage is
834 * minimized. Linux code should set the gpio direction
835 * first thing; but until it does, and in case
836 * chip->get_direction is not set, we may expose the
837 * wrong direction in sysfs.
Johan Hovold05aa5202015-01-12 17:12:26 +0100838 */
Linus Walleij72d32002016-04-28 13:33:59 +0200839
840 if (chip->get_direction) {
841 /*
842 * If we have .get_direction, set up the initial
843 * direction flag from the hardware.
844 */
845 int dir = chip->get_direction(chip, i);
846
847 if (!dir)
848 set_bit(FLAG_IS_OUT, &desc->flags);
849 } else if (!chip->direction_input) {
850 /*
851 * If the chip lacks the .direction_input callback
852 * we logically assume all lines are outputs.
853 */
854 set_bit(FLAG_IS_OUT, &desc->flags);
855 }
David Brownelld2876d02008-02-04 22:28:20 -0800856 }
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900857
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530858#ifdef CONFIG_PINCTRL
Linus Walleij20ec3e32016-02-11 11:03:06 +0100859 INIT_LIST_HEAD(&gdev->pin_ranges);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530860#endif
861
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200862 status = gpiochip_set_desc_names(chip);
863 if (status)
864 goto err_remove_from_list;
865
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200866 status = of_gpiochip_add(chip);
867 if (status)
868 goto err_remove_chip;
869
Mika Westerberg664e3e52014-01-08 12:40:54 +0200870 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600871
Linus Walleij3c702e92015-10-21 15:29:53 +0200872 /*
873 * By first adding the chardev, and then adding the device,
874 * we get a device node entry in sysfs under
875 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
876 * coldplug of device nodes and other udev business.
Guenter Roeck159f3cd2016-03-31 08:11:30 -0700877 * We can do this only if gpiolib has been initialized.
878 * Otherwise, defer until later.
Linus Walleij3c702e92015-10-21 15:29:53 +0200879 */
Guenter Roeck159f3cd2016-03-31 08:11:30 -0700880 if (gpiolib_initialized) {
881 status = gpiochip_setup_dev(gdev);
882 if (status)
883 goto err_remove_chip;
884 }
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600885 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800886
Johan Hovold225fce82015-01-12 17:12:25 +0100887err_remove_chip:
888 acpi_gpiochip_remove(chip);
Johan Hovold6d867502015-05-04 17:23:25 +0200889 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +0100890 of_gpiochip_remove(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200891err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +0100892 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +0200893 list_del(&gdev->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800894 spin_unlock_irqrestore(&gpio_lock, flags);
Guenter Roeck476e2fc2016-03-31 08:11:29 -0700895err_free_label:
896 kfree(gdev->label);
897err_free_descs:
898 kfree(gdev->descs);
Linus Walleijff2b1352015-10-20 11:10:38 +0200899err_free_gdev:
900 ida_simple_remove(&gpio_ida, gdev->id);
David Brownelld2876d02008-02-04 22:28:20 -0800901 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +0200902 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100903 gdev->base, gdev->base + gdev->ngpio - 1,
904 chip->label ? : "generic");
905 kfree(gdev);
David Brownelld2876d02008-02-04 22:28:20 -0800906 return status;
907}
Linus Walleijb08ea352015-12-03 15:14:13 +0100908EXPORT_SYMBOL_GPL(gpiochip_add_data);
David Brownelld2876d02008-02-04 22:28:20 -0800909
910/**
Linus Walleij43c54ec2016-02-11 11:37:48 +0100911 * gpiochip_get_data() - get per-subdriver data for the chip
912 */
913void *gpiochip_get_data(struct gpio_chip *chip)
914{
915 return chip->gpiodev->data;
916}
917EXPORT_SYMBOL_GPL(gpiochip_get_data);
918
919/**
David Brownelld2876d02008-02-04 22:28:20 -0800920 * gpiochip_remove() - unregister a gpio_chip
921 * @chip: the chip to unregister
922 *
923 * A gpio_chip with any GPIOs still requested may not be removed.
924 */
abdoulaye berthee1db1702014-07-05 18:28:50 +0200925void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -0800926{
Linus Walleijff2b1352015-10-20 11:10:38 +0200927 struct gpio_device *gdev = chip->gpiodev;
Johan Hovoldfab28b82015-05-04 17:10:27 +0200928 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -0800929 unsigned long flags;
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100930 unsigned i;
Johan Hovoldfab28b82015-05-04 17:10:27 +0200931 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -0800932
Linus Walleijff2b1352015-10-20 11:10:38 +0200933 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
Linus Walleijafbc4f32016-02-09 13:21:06 +0100934 gpiochip_sysfs_unregister(gdev);
Bamvor Jian Zhangbd203bd2016-02-20 13:13:19 +0800935 /* Numb the device, cancelling all outstanding operations */
936 gdev->chip = NULL;
Johan Hovold00acc3d2015-01-12 17:12:27 +0100937 gpiochip_irqchip_remove(chip);
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200938 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +0100939 gpiochip_remove_pin_ranges(chip);
Benoit Parrotf625d462015-02-02 11:44:44 -0600940 gpiochip_free_hogs(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600941 of_gpiochip_remove(chip);
Linus Walleij43c54ec2016-02-11 11:37:48 +0100942 /*
943 * We accept no more calls into the driver from this point, so
944 * NULL the driver data pointer
945 */
946 gdev->data = NULL;
Anton Vorontsov391c9702010-06-08 07:48:17 -0600947
Johan Hovold6798aca2015-01-12 17:12:28 +0100948 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100949 for (i = 0; i < gdev->ngpio; i++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +0100950 desc = &gdev->descs[i];
Johan Hovoldfab28b82015-05-04 17:10:27 +0200951 if (test_bit(FLAG_REQUESTED, &desc->flags))
952 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -0800953 }
David Brownelld2876d02008-02-04 22:28:20 -0800954 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900955
Johan Hovoldfab28b82015-05-04 17:10:27 +0200956 if (requested)
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100957 dev_crit(&gdev->dev,
Linus Walleij58383c782015-11-04 09:56:26 +0100958 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +0200959
Linus Walleijff2b1352015-10-20 11:10:38 +0200960 /*
961 * The gpiochip side puts its use of the device to rest here:
962 * if there are no userspace clients, the chardev and device will
963 * be removed, else it will be dangling until the last user is
964 * gone.
965 */
966 put_device(&gdev->dev);
David Brownelld2876d02008-02-04 22:28:20 -0800967}
968EXPORT_SYMBOL_GPL(gpiochip_remove);
969
Laxman Dewangan0cf32922016-02-15 16:32:09 +0530970static void devm_gpio_chip_release(struct device *dev, void *res)
971{
972 struct gpio_chip *chip = *(struct gpio_chip **)res;
973
974 gpiochip_remove(chip);
975}
976
977static int devm_gpio_chip_match(struct device *dev, void *res, void *data)
978
979{
980 struct gpio_chip **r = res;
981
982 if (!r || !*r) {
983 WARN_ON(!r || !*r);
984 return 0;
985 }
986
987 return *r == data;
988}
989
990/**
991 * devm_gpiochip_add_data() - Resource manager piochip_add_data()
992 * @dev: the device pointer on which irq_chip belongs to.
993 * @chip: the chip to register, with chip->base initialized
994 * Context: potentially before irqs will work
995 *
996 * Returns a negative errno if the chip can't be registered, such as
997 * because the chip->base is invalid or already associated with a
998 * different chip. Otherwise it returns zero as a success code.
999 *
1000 * The gpio chip automatically be released when the device is unbound.
1001 */
1002int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
1003 void *data)
1004{
1005 struct gpio_chip **ptr;
1006 int ret;
1007
1008 ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
1009 GFP_KERNEL);
1010 if (!ptr)
1011 return -ENOMEM;
1012
1013 ret = gpiochip_add_data(chip, data);
1014 if (ret < 0) {
1015 devres_free(ptr);
1016 return ret;
1017 }
1018
1019 *ptr = chip;
1020 devres_add(dev, ptr);
1021
1022 return 0;
1023}
1024EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
1025
1026/**
1027 * devm_gpiochip_remove() - Resource manager of gpiochip_remove()
1028 * @dev: device for which which resource was allocated
1029 * @chip: the chip to remove
1030 *
1031 * A gpio_chip with any GPIOs still requested may not be removed.
1032 */
1033void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip)
1034{
1035 int ret;
1036
1037 ret = devres_release(dev, devm_gpio_chip_release,
1038 devm_gpio_chip_match, chip);
1039 if (!ret)
1040 WARN_ON(ret);
1041}
1042EXPORT_SYMBOL_GPL(devm_gpiochip_remove);
1043
Grant Likely594fa262010-06-08 07:48:16 -06001044/**
1045 * gpiochip_find() - iterator for locating a specific gpio_chip
1046 * @data: data to pass to match function
1047 * @callback: Callback function to check gpio_chip
1048 *
1049 * Similar to bus_find_device. It returns a reference to a gpio_chip as
1050 * determined by a user supplied @match callback. The callback should return
1051 * 0 if the device doesn't match and non-zero if it does. If the callback is
1052 * non-zero, this function will return to the caller and not iterate over any
1053 * more gpio_chips.
1054 */
Grant Likely07ce8ec2012-05-18 23:01:05 -06001055struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -07001056 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -06001057 void *data))
Grant Likely594fa262010-06-08 07:48:16 -06001058{
Linus Walleijff2b1352015-10-20 11:10:38 +02001059 struct gpio_device *gdev;
Alexandre Courbot125eef92013-02-03 01:29:26 +09001060 struct gpio_chip *chip;
Grant Likely594fa262010-06-08 07:48:16 -06001061 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -06001062
1063 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02001064 list_for_each_entry(gdev, &gpio_devices, list)
1065 if (match(gdev->chip, data))
Grant Likely594fa262010-06-08 07:48:16 -06001066 break;
Alexandre Courbot125eef92013-02-03 01:29:26 +09001067
1068 /* No match? */
Linus Walleijff2b1352015-10-20 11:10:38 +02001069 if (&gdev->list == &gpio_devices)
Alexandre Courbot125eef92013-02-03 01:29:26 +09001070 chip = NULL;
Linus Walleijff2b1352015-10-20 11:10:38 +02001071 else
1072 chip = gdev->chip;
1073
Grant Likely594fa262010-06-08 07:48:16 -06001074 spin_unlock_irqrestore(&gpio_lock, flags);
1075
1076 return chip;
1077}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -06001078EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -08001079
Alexandre Courbot79697ef2013-11-16 21:39:32 +09001080static int gpiochip_match_name(struct gpio_chip *chip, void *data)
1081{
1082 const char *name = data;
1083
1084 return !strcmp(chip->label, name);
1085}
1086
1087static struct gpio_chip *find_chip_by_name(const char *name)
1088{
1089 return gpiochip_find((void *)name, gpiochip_match_name);
1090}
1091
Linus Walleij14250522014-03-25 10:40:18 +01001092#ifdef CONFIG_GPIOLIB_IRQCHIP
1093
1094/*
1095 * The following is irqchip helper code for gpiochips.
1096 */
1097
1098/**
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001099 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
1100 * @gpiochip: the gpiochip to set the irqchip chain to
1101 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +01001102 * @parent_irq: the irq number corresponding to the parent IRQ for this
1103 * chained irqchip
1104 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001105 * coming out of the gpiochip. If the interrupt is nested rather than
1106 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +01001107 */
1108void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
1109 struct irq_chip *irqchip,
1110 int parent_irq,
1111 irq_flow_handler_t parent_handler)
1112{
Linus Walleij83141a72014-09-26 13:50:12 +02001113 unsigned int offset;
1114
Linus Walleij83141a72014-09-26 13:50:12 +02001115 if (!gpiochip->irqdomain) {
1116 chip_err(gpiochip, "called %s before setting up irqchip\n",
1117 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +02001118 return;
1119 }
1120
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001121 if (parent_handler) {
1122 if (gpiochip->can_sleep) {
1123 chip_err(gpiochip,
1124 "you cannot have chained interrupts on a "
1125 "chip that may sleep\n");
1126 return;
1127 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001128 /*
1129 * The parent irqchip is already using the chip_data for this
1130 * irqchip, so our callbacks simply use the handler_data.
1131 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +02001132 irq_set_chained_handler_and_data(parent_irq, parent_handler,
1133 gpiochip);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001134
1135 gpiochip->irq_parent = parent_irq;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +02001136 }
Linus Walleij83141a72014-09-26 13:50:12 +02001137
1138 /* Set the parent IRQ for all affected IRQs */
1139 for (offset = 0; offset < gpiochip->ngpio; offset++)
1140 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
1141 parent_irq);
Linus Walleij14250522014-03-25 10:40:18 +01001142}
1143EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
1144
1145/**
1146 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
1147 * @d: the irqdomain used by this irqchip
1148 * @irq: the global irq number used by this GPIO irqchip irq
1149 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
1150 *
1151 * This function will set up the mapping for a certain IRQ line on a
1152 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1153 * stored inside the gpiochip.
1154 */
1155static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1156 irq_hw_number_t hwirq)
1157{
1158 struct gpio_chip *chip = d->host_data;
1159
Linus Walleij14250522014-03-25 10:40:18 +01001160 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001161 /*
1162 * This lock class tells lockdep that GPIO irqs are in a different
1163 * category than their parents, so it won't report false recursion.
1164 */
1165 irq_set_lockdep_class(irq, chip->lock_key);
Linus Walleij7633fb92014-04-09 13:20:38 +02001166 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
Linus Walleij1c8732b2014-04-09 13:34:39 +02001167 /* Chips that can sleep need nested thread handlers */
Octavian Purdila295494a2014-09-19 23:22:44 +03001168 if (chip->can_sleep && !chip->irq_not_threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +02001169 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +01001170 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -05001171
Linus Walleij1333b902014-04-23 16:45:12 +02001172 /*
1173 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1174 * is passed as default type.
1175 */
1176 if (chip->irq_default_type != IRQ_TYPE_NONE)
1177 irq_set_irq_type(irq, chip->irq_default_type);
Linus Walleij14250522014-03-25 10:40:18 +01001178
1179 return 0;
1180}
1181
Linus Walleijc3626fd2014-03-28 20:42:01 +01001182static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
1183{
Linus Walleij1c8732b2014-04-09 13:34:39 +02001184 struct gpio_chip *chip = d->host_data;
1185
Linus Walleij1c8732b2014-04-09 13:34:39 +02001186 if (chip->can_sleep)
1187 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001188 irq_set_chip_and_handler(irq, NULL, NULL);
1189 irq_set_chip_data(irq, NULL);
1190}
1191
Linus Walleij14250522014-03-25 10:40:18 +01001192static const struct irq_domain_ops gpiochip_domain_ops = {
1193 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +01001194 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +01001195 /* Virtually all GPIO irqchips are twocell:ed */
1196 .xlate = irq_domain_xlate_twocell,
1197};
1198
1199static int gpiochip_irq_reqres(struct irq_data *d)
1200{
1201 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1202
Linus Walleijff2b1352015-10-20 11:10:38 +02001203 if (!try_module_get(chip->gpiodev->owner))
Grygorii Strashko5b76e792015-06-25 20:30:50 +03001204 return -ENODEV;
1205
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001206 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +01001207 chip_err(chip,
1208 "unable to lock HW IRQ %lu for IRQ\n",
1209 d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001210 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001211 return -EINVAL;
1212 }
1213 return 0;
1214}
1215
1216static void gpiochip_irq_relres(struct irq_data *d)
1217{
1218 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
1219
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001220 gpiochip_unlock_as_irq(chip, d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +02001221 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +01001222}
1223
1224static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
1225{
1226 return irq_find_mapping(chip->irqdomain, offset);
1227}
1228
1229/**
1230 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
1231 * @gpiochip: the gpiochip to remove the irqchip from
1232 *
1233 * This is called only from gpiochip_remove()
1234 */
1235static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
1236{
Linus Walleijc3626fd2014-03-28 20:42:01 +01001237 unsigned int offset;
1238
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001239 acpi_gpiochip_free_interrupts(gpiochip);
1240
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +03001241 if (gpiochip->irq_parent) {
1242 irq_set_chained_handler(gpiochip->irq_parent, NULL);
1243 irq_set_handler_data(gpiochip->irq_parent, NULL);
1244 }
1245
Linus Walleijc3626fd2014-03-28 20:42:01 +01001246 /* Remove all IRQ mappings and delete the domain */
1247 if (gpiochip->irqdomain) {
1248 for (offset = 0; offset < gpiochip->ngpio; offset++)
Grygorii Strashkoe3893382014-09-25 19:09:23 +03001249 irq_dispose_mapping(
1250 irq_find_mapping(gpiochip->irqdomain, offset));
Linus Walleij14250522014-03-25 10:40:18 +01001251 irq_domain_remove(gpiochip->irqdomain);
Linus Walleijc3626fd2014-03-28 20:42:01 +01001252 }
Linus Walleij14250522014-03-25 10:40:18 +01001253
1254 if (gpiochip->irqchip) {
1255 gpiochip->irqchip->irq_request_resources = NULL;
1256 gpiochip->irqchip->irq_release_resources = NULL;
1257 gpiochip->irqchip = NULL;
1258 }
1259}
1260
1261/**
1262 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
1263 * @gpiochip: the gpiochip to add the irqchip to
1264 * @irqchip: the irqchip to add to the gpiochip
1265 * @first_irq: if not dynamically assigned, the base (first) IRQ to
1266 * allocate gpiochip irqs from
1267 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +02001268 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
1269 * to have the core avoid setting up any default type in the hardware.
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001270 * @lock_key: lockdep class
Linus Walleij14250522014-03-25 10:40:18 +01001271 *
1272 * This function closely associates a certain irqchip with a certain
1273 * gpiochip, providing an irq domain to translate the local IRQs to
1274 * global irqs in the gpiolib core, and making sure that the gpiochip
1275 * is passed as chip data to all related functions. Driver callbacks
Linus Walleij09dd5f92015-12-07 15:31:58 +01001276 * need to use gpiochip_get_data() to get their local state containers back
Linus Walleij14250522014-03-25 10:40:18 +01001277 * from the gpiochip passed as chip data. An irqdomain will be stored
1278 * in the gpiochip that shall be used by the driver to handle IRQ number
1279 * translation. The gpiochip will need to be initialized and registered
1280 * before calling this function.
1281 *
Linus Walleijc3626fd2014-03-28 20:42:01 +01001282 * This function will handle two cell:ed simple IRQs and assumes all
1283 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +01001284 * need to be open coded.
1285 */
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001286int _gpiochip_irqchip_add(struct gpio_chip *gpiochip,
1287 struct irq_chip *irqchip,
1288 unsigned int first_irq,
1289 irq_flow_handler_t handler,
1290 unsigned int type,
1291 struct lock_class_key *lock_key)
Linus Walleij14250522014-03-25 10:40:18 +01001292{
1293 struct device_node *of_node;
1294 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +01001295 unsigned irq_base = 0;
Linus Walleij14250522014-03-25 10:40:18 +01001296
1297 if (!gpiochip || !irqchip)
1298 return -EINVAL;
1299
Linus Walleij58383c782015-11-04 09:56:26 +01001300 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +01001301 pr_err("missing gpiochip .dev parent pointer\n");
1302 return -EINVAL;
1303 }
Linus Walleij58383c782015-11-04 09:56:26 +01001304 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +01001305#ifdef CONFIG_OF_GPIO
1306 /*
Colin Cronin20a8a962015-05-18 11:41:43 -07001307 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +08001308 * FIXME: get rid of this and use gpiochip->parent->of_node
1309 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +01001310 */
1311 if (gpiochip->of_node)
1312 of_node = gpiochip->of_node;
1313#endif
1314 gpiochip->irqchip = irqchip;
1315 gpiochip->irq_handler = handler;
1316 gpiochip->irq_default_type = type;
1317 gpiochip->to_irq = gpiochip_to_irq;
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001318 gpiochip->lock_key = lock_key;
Linus Walleij14250522014-03-25 10:40:18 +01001319 gpiochip->irqdomain = irq_domain_add_simple(of_node,
1320 gpiochip->ngpio, first_irq,
1321 &gpiochip_domain_ops, gpiochip);
1322 if (!gpiochip->irqdomain) {
1323 gpiochip->irqchip = NULL;
1324 return -EINVAL;
1325 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +02001326
1327 /*
1328 * It is possible for a driver to override this, but only if the
1329 * alternative functions are both implemented.
1330 */
1331 if (!irqchip->irq_request_resources &&
1332 !irqchip->irq_release_resources) {
1333 irqchip->irq_request_resources = gpiochip_irq_reqres;
1334 irqchip->irq_release_resources = gpiochip_irq_relres;
1335 }
Linus Walleij14250522014-03-25 10:40:18 +01001336
1337 /*
1338 * Prepare the mapping since the irqchip shall be orthogonal to
1339 * any gpiochip calls. If the first_irq was zero, this is
1340 * necessary to allocate descriptors for all IRQs.
1341 */
Linus Walleijc3626fd2014-03-28 20:42:01 +01001342 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1343 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
1344 if (offset == 0)
1345 /*
1346 * Store the base into the gpiochip to be used when
1347 * unmapping the irqs.
1348 */
1349 gpiochip->irq_base = irq_base;
1350 }
Linus Walleij14250522014-03-25 10:40:18 +01001351
Mika Westerbergafa82fa2014-07-25 09:54:48 +03001352 acpi_gpiochip_request_interrupts(gpiochip);
1353
Linus Walleij14250522014-03-25 10:40:18 +01001354 return 0;
1355}
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +03001356EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add);
Linus Walleij14250522014-03-25 10:40:18 +01001357
1358#else /* CONFIG_GPIOLIB_IRQCHIP */
1359
1360static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
1361
1362#endif /* CONFIG_GPIOLIB_IRQCHIP */
1363
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001364/**
1365 * gpiochip_generic_request() - request the gpio function for a pin
1366 * @chip: the gpiochip owning the GPIO
1367 * @offset: the offset of the GPIO to request for GPIO function
1368 */
1369int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
1370{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001371 return pinctrl_request_gpio(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001372}
1373EXPORT_SYMBOL_GPL(gpiochip_generic_request);
1374
1375/**
1376 * gpiochip_generic_free() - free the gpio function from a pin
1377 * @chip: the gpiochip to request the gpio function for
1378 * @offset: the offset of the GPIO to free from GPIO function
1379 */
1380void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
1381{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001382 pinctrl_free_gpio(chip->gpiodev->base + offset);
Jonas Gorskic771c2f2015-10-11 17:34:15 +02001383}
1384EXPORT_SYMBOL_GPL(gpiochip_generic_free);
1385
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301386#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +01001387
Linus Walleij3f0f8672012-11-20 12:40:15 +01001388/**
Christian Ruppert586a87e2013-10-15 15:37:54 +02001389 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
1390 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +02001391 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +02001392 * @gpio_offset: the start offset in the current gpio_chip number space
1393 * @pin_group: name of the pin group inside the pin controller
1394 */
1395int gpiochip_add_pingroup_range(struct gpio_chip *chip,
1396 struct pinctrl_dev *pctldev,
1397 unsigned int gpio_offset, const char *pin_group)
1398{
1399 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001400 struct gpio_device *gdev = chip->gpiodev;
Christian Ruppert586a87e2013-10-15 15:37:54 +02001401 int ret;
1402
1403 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1404 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001405 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +02001406 return -ENOMEM;
1407 }
1408
1409 /* Use local offset as range ID */
1410 pin_range->range.id = gpio_offset;
1411 pin_range->range.gc = chip;
1412 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001413 pin_range->range.base = gdev->base + gpio_offset;
Christian Ruppert586a87e2013-10-15 15:37:54 +02001414 pin_range->pctldev = pctldev;
1415
1416 ret = pinctrl_get_group_pins(pctldev, pin_group,
1417 &pin_range->range.pins,
1418 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001419 if (ret < 0) {
1420 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001421 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +01001422 }
Christian Ruppert586a87e2013-10-15 15:37:54 +02001423
1424 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1425
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001426 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1427 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +02001428 pinctrl_dev_get_devname(pctldev), pin_group);
1429
Linus Walleij20ec3e32016-02-11 11:03:06 +01001430 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Christian Ruppert586a87e2013-10-15 15:37:54 +02001431
1432 return 0;
1433}
1434EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
1435
1436/**
Linus Walleij3f0f8672012-11-20 12:40:15 +01001437 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
1438 * @chip: the gpiochip to add the range for
1439 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +01001440 * @gpio_offset: the start offset in the current gpio_chip number space
1441 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +01001442 * @npins: the number of pins from the offset of each pin space (GPIO and
1443 * pin controller) to accumulate in this range
1444 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001445int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +01001446 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +01001447 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301448{
1449 struct gpio_pin_range *pin_range;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001450 struct gpio_device *gdev = chip->gpiodev;
Axel Linb4d4b1f2012-11-21 14:33:56 +08001451 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301452
Linus Walleij3f0f8672012-11-20 12:40:15 +01001453 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301454 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001455 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001456 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301457 }
1458
Linus Walleij3f0f8672012-11-20 12:40:15 +01001459 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +01001460 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001461 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301462 pin_range->range.name = chip->label;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001463 pin_range->range.base = gdev->base + gpio_offset;
Linus Walleij316511c2012-11-21 08:48:09 +01001464 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301465 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +01001466 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301467 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +01001468 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +08001469 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001470 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +01001471 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +08001472 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +01001473 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +02001474 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1475 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +01001476 pinctl_name,
1477 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301478
Linus Walleij20ec3e32016-02-11 11:03:06 +01001479 list_add_tail(&pin_range->node, &gdev->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +01001480
1481 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301482}
Linus Walleij165adc92012-11-06 14:49:39 +01001483EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301484
Linus Walleij3f0f8672012-11-20 12:40:15 +01001485/**
1486 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
1487 * @chip: the chip to remove all the mappings for
1488 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301489void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
1490{
1491 struct gpio_pin_range *pin_range, *tmp;
Linus Walleij20ec3e32016-02-11 11:03:06 +01001492 struct gpio_device *gdev = chip->gpiodev;
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301493
Linus Walleij20ec3e32016-02-11 11:03:06 +01001494 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301495 list_del(&pin_range->node);
1496 pinctrl_remove_gpio_range(pin_range->pctldev,
1497 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +01001498 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301499 }
1500}
Linus Walleij165adc92012-11-06 14:49:39 +01001501EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1502
1503#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301504
David Brownelld2876d02008-02-04 22:28:20 -08001505/* These "optional" allocation calls help prevent drivers from stomping
1506 * on each other, and help provide better diagnostics in debugfs.
1507 * They're called even less than the "set direction" calls.
1508 */
Mika Westerberg77c2d792014-03-10 14:54:50 +02001509static int __gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08001510{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001511 struct gpio_chip *chip = desc->gdev->chip;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001512 int status;
David Brownelld2876d02008-02-04 22:28:20 -08001513 unsigned long flags;
1514
1515 spin_lock_irqsave(&gpio_lock, flags);
1516
David Brownelld2876d02008-02-04 22:28:20 -08001517 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07001518 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001519 */
1520
1521 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1522 desc_set_label(desc, label ? : "?");
1523 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001524 } else {
David Brownelld2876d02008-02-04 22:28:20 -08001525 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -08001526 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001527 }
1528
1529 if (chip->request) {
1530 /* chip->request may sleep */
1531 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001532 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001533 spin_lock_irqsave(&gpio_lock, flags);
1534
1535 if (status < 0) {
1536 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -07001537 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001538 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001539 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001540 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03001541 if (chip->get_direction) {
1542 /* chip->get_direction may sleep */
1543 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001544 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001545 spin_lock_irqsave(&gpio_lock, flags);
1546 }
David Brownelld2876d02008-02-04 22:28:20 -08001547done:
Laurent Pinchart923b93e2015-10-13 00:20:20 +03001548 if (status < 0) {
1549 /* Clear flags that might have been set by the caller before
1550 * requesting the GPIO.
1551 */
1552 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
1553 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
1554 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
1555 }
Mika Westerberg77c2d792014-03-10 14:54:50 +02001556 spin_unlock_irqrestore(&gpio_lock, flags);
1557 return status;
1558}
1559
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001560/*
1561 * This descriptor validation needs to be inserted verbatim into each
1562 * function taking a descriptor, so we need to use a preprocessor
Linus Walleij54d77192016-05-30 16:48:39 +02001563 * macro to avoid endless duplication. If the desc is NULL it is an
1564 * optional GPIO and calls should just bail out.
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001565 */
1566#define VALIDATE_DESC(desc) do { \
Linus Walleij54d77192016-05-30 16:48:39 +02001567 if (!desc) \
1568 return 0; \
1569 if (!desc->gdev) { \
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001570 pr_warn("%s: invalid GPIO\n", __func__); \
1571 return -EINVAL; \
1572 } \
1573 if ( !desc->gdev->chip ) { \
1574 dev_warn(&desc->gdev->dev, \
1575 "%s: backing chip is gone\n", __func__); \
1576 return 0; \
1577 } } while (0)
1578
1579#define VALIDATE_DESC_VOID(desc) do { \
Linus Walleij54d77192016-05-30 16:48:39 +02001580 if (!desc) \
1581 return; \
1582 if (!desc->gdev) { \
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001583 pr_warn("%s: invalid GPIO\n", __func__); \
1584 return; \
1585 } \
1586 if (!desc->gdev->chip) { \
1587 dev_warn(&desc->gdev->dev, \
1588 "%s: backing chip is gone\n", __func__); \
1589 return; \
1590 } } while (0)
1591
1592
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09001593int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001594{
1595 int status = -EPROBE_DEFER;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001596 struct gpio_device *gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001597
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001598 VALIDATE_DESC(desc);
1599 gdev = desc->gdev;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001600
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001601 if (try_module_get(gdev->owner)) {
Mika Westerberg77c2d792014-03-10 14:54:50 +02001602 status = __gpiod_request(desc, label);
1603 if (status < 0)
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001604 module_put(gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01001605 else
1606 get_device(&gdev->dev);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001607 }
1608
David Brownelld2876d02008-02-04 22:28:20 -08001609 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +02001610 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001611
David Brownelld2876d02008-02-04 22:28:20 -08001612 return status;
1613}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001614
Mika Westerberg77c2d792014-03-10 14:54:50 +02001615static bool __gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001616{
Mika Westerberg77c2d792014-03-10 14:54:50 +02001617 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08001618 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07001619 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001620
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07001621 might_sleep();
1622
Alexandre Courbot372e7222013-02-03 01:29:29 +09001623 gpiod_unexport(desc);
David Brownelld8f388d82008-07-25 01:46:07 -07001624
David Brownelld2876d02008-02-04 22:28:20 -08001625 spin_lock_irqsave(&gpio_lock, flags);
1626
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001627 chip = desc->gdev->chip;
David Brownell35e8bb52008-10-15 22:03:16 -07001628 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1629 if (chip->free) {
1630 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07001631 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001632 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001633 spin_lock_irqsave(&gpio_lock, flags);
1634 }
David Brownelld2876d02008-02-04 22:28:20 -08001635 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08001636 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001637 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301638 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301639 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06001640 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001641 ret = true;
1642 }
David Brownelld2876d02008-02-04 22:28:20 -08001643
1644 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001645 return ret;
1646}
1647
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09001648void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001649{
Linus Walleij33a68e82016-02-11 10:28:44 +01001650 if (desc && desc->gdev && __gpiod_free(desc)) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001651 module_put(desc->gdev->owner);
Linus Walleij33a68e82016-02-11 10:28:44 +01001652 put_device(&desc->gdev->dev);
1653 } else {
Mika Westerberg77c2d792014-03-10 14:54:50 +02001654 WARN_ON(extra_checks);
Linus Walleij33a68e82016-02-11 10:28:44 +01001655 }
David Brownelld2876d02008-02-04 22:28:20 -08001656}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001657
David Brownelld2876d02008-02-04 22:28:20 -08001658/**
1659 * gpiochip_is_requested - return string iff signal was requested
1660 * @chip: controller managing the signal
1661 * @offset: of signal within controller's 0..(ngpio - 1) range
1662 *
1663 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09001664 * The string returned is the label passed to gpio_request(); if none has been
1665 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08001666 *
1667 * This function is for use by GPIO controller drivers. The label can
1668 * help with diagnostics, and knowing that the signal is used as a GPIO
1669 * can help avoid accidentally multiplexing it to another controller.
1670 */
1671const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1672{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001673 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001674
Dirk Behme48b59532015-08-18 18:02:32 +02001675 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08001676 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001677
Linus Walleij1c3cdb12016-02-09 13:51:59 +01001678 desc = &chip->gpiodev->descs[offset];
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001679
Alexandre Courbot372e7222013-02-03 01:29:29 +09001680 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08001681 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001682 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08001683}
1684EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1685
Mika Westerberg77c2d792014-03-10 14:54:50 +02001686/**
1687 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
1688 * @desc: GPIO descriptor to request
1689 * @label: label for the GPIO
1690 *
1691 * Function allows GPIO chip drivers to request and use their own GPIO
1692 * descriptors via gpiolib API. Difference to gpiod_request() is that this
1693 * function will not increase reference count of the GPIO chip module. This
1694 * allows the GPIO chip module to be unloaded as needed (we assume that the
1695 * GPIO chip driver handles freeing the GPIOs it has requested).
1696 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001697struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
1698 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001699{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001700 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
1701 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001702
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001703 if (IS_ERR(desc)) {
1704 chip_err(chip, "failed to get GPIO descriptor\n");
1705 return desc;
1706 }
1707
1708 err = __gpiod_request(desc, label);
1709 if (err < 0)
1710 return ERR_PTR(err);
1711
1712 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001713}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07001714EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001715
1716/**
1717 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
1718 * @desc: GPIO descriptor to free
1719 *
1720 * Function frees the given GPIO requested previously with
1721 * gpiochip_request_own_desc().
1722 */
1723void gpiochip_free_own_desc(struct gpio_desc *desc)
1724{
1725 if (desc)
1726 __gpiod_free(desc);
1727}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07001728EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08001729
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001730/*
1731 * Drivers MUST set GPIO direction before making get/set calls. In
David Brownelld2876d02008-02-04 22:28:20 -08001732 * some cases this is done in early boot, before IRQs are enabled.
1733 *
1734 * As a rule these aren't called more than once (except for drivers
1735 * using the open-drain emulation idiom) so these are natural places
1736 * to accumulate extra debugging checks. Note that we can't (yet)
1737 * rely on gpio_request() having been called beforehand.
1738 */
1739
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001740/**
1741 * gpiod_direction_input - set the GPIO direction to input
1742 * @desc: GPIO to set to input
1743 *
1744 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
1745 * be called safely on it.
1746 *
1747 * Return 0 in case of success, else an error code.
1748 */
1749int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001750{
David Brownelld2876d02008-02-04 22:28:20 -08001751 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001752 int status = -EINVAL;
1753
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001754 VALIDATE_DESC(desc);
1755 chip = desc->gdev->chip;
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001756
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001757 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01001758 gpiod_warn(desc,
1759 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02001760 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001761 return -EIO;
1762 }
1763
Alexandre Courbotd82da792014-07-22 16:17:43 +09001764 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -08001765 if (status == 0)
1766 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001767
Alexandre Courbot372e7222013-02-03 01:29:29 +09001768 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09001769
David Brownelld2876d02008-02-04 22:28:20 -08001770 return status;
1771}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001772EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001773
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001774static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08001775{
Linus Walleijc663e5f2016-03-22 10:51:16 +01001776 struct gpio_chip *gc = desc->gdev->chip;
1777 int ret;
David Brownelld2876d02008-02-04 22:28:20 -08001778
Linus Walleijd468bf92013-09-24 11:54:38 +02001779 /* GPIOs used for IRQs shall not be set as output */
1780 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1781 gpiod_err(desc,
1782 "%s: tried to set a GPIO tied to an IRQ as output\n",
1783 __func__);
1784 return -EIO;
1785 }
1786
Linus Walleijc663e5f2016-03-22 10:51:16 +01001787 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
1788 /* First see if we can enable open drain in hardware */
1789 if (gc->set_single_ended) {
1790 ret = gc->set_single_ended(gc, gpio_chip_hwgpio(desc),
1791 LINE_MODE_OPEN_DRAIN);
1792 if (!ret)
1793 goto set_output_value;
1794 }
1795 /* Emulate open drain by not actively driving the line high */
1796 if (value)
1797 return gpiod_direction_input(desc);
1798 }
1799 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
1800 if (gc->set_single_ended) {
1801 ret = gc->set_single_ended(gc, gpio_chip_hwgpio(desc),
1802 LINE_MODE_OPEN_SOURCE);
1803 if (!ret)
1804 goto set_output_value;
1805 }
1806 /* Emulate open source by not actively driving the line low */
1807 if (!value)
1808 return gpiod_direction_input(desc);
1809 } else {
1810 /* Make sure to disable open drain/source hardware, if any */
1811 if (gc->set_single_ended)
1812 gc->set_single_ended(gc,
1813 gpio_chip_hwgpio(desc),
1814 LINE_MODE_PUSH_PULL);
1815 }
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301816
Linus Walleijc663e5f2016-03-22 10:51:16 +01001817set_output_value:
1818 if (!gc->set || !gc->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01001819 gpiod_warn(desc,
1820 "%s: missing set() or direction_output() operations\n",
1821 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001822 return -EIO;
1823 }
1824
Linus Walleijc663e5f2016-03-22 10:51:16 +01001825 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), value);
1826 if (!ret)
David Brownelld2876d02008-02-04 22:28:20 -08001827 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001828 trace_gpio_value(desc_to_gpio(desc), 0, value);
Linus Walleijc663e5f2016-03-22 10:51:16 +01001829 trace_gpio_direction(desc_to_gpio(desc), 0, ret);
1830 return ret;
David Brownelld2876d02008-02-04 22:28:20 -08001831}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001832
1833/**
1834 * gpiod_direction_output_raw - set the GPIO direction to output
1835 * @desc: GPIO to set to output
1836 * @value: initial output value of the GPIO
1837 *
1838 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1839 * be called safely on it. The initial value of the output must be specified
1840 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1841 *
1842 * Return 0 in case of success, else an error code.
1843 */
1844int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1845{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001846 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001847 return _gpiod_direction_output_raw(desc, value);
1848}
1849EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
1850
1851/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05301852 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001853 * @desc: GPIO to set to output
1854 * @value: initial output value of the GPIO
1855 *
1856 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1857 * be called safely on it. The initial value of the output must be specified
1858 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1859 * account.
1860 *
1861 * Return 0 in case of success, else an error code.
1862 */
1863int gpiod_direction_output(struct gpio_desc *desc, int value)
1864{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001865 VALIDATE_DESC(desc);
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001866 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1867 value = !value;
1868 return _gpiod_direction_output_raw(desc, value);
1869}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001870EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08001871
Felipe Balbic4b5be92010-05-26 14:42:23 -07001872/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001873 * gpiod_set_debounce - sets @debounce time for a @gpio
Felipe Balbic4b5be92010-05-26 14:42:23 -07001874 * @gpio: the gpio to set debounce time
1875 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02001876 *
1877 * returns -ENOTSUPP if the controller does not support setting
1878 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07001879 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001880int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07001881{
Felipe Balbic4b5be92010-05-26 14:42:23 -07001882 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001883
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001884 VALIDATE_DESC(desc);
1885 chip = desc->gdev->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001886 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01001887 gpiod_dbg(desc,
1888 "%s: missing set() or set_debounce() operations\n",
1889 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02001890 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001891 }
1892
Alexandre Courbotd82da792014-07-22 16:17:43 +09001893 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001894}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001895EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001896
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001897/**
1898 * gpiod_is_active_low - test whether a GPIO is active-low or not
1899 * @desc: the gpio descriptor to test
1900 *
1901 * Returns 1 if the GPIO is active-low, 0 otherwise.
1902 */
1903int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001904{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001905 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001906 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001907}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001908EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08001909
1910/* I/O calls are only valid after configuration completed; the relevant
1911 * "is this a valid GPIO" error checks should already have been done.
1912 *
1913 * "Get" operations are often inlinable as reading a pin value register,
1914 * and masking the relevant bit in that register.
1915 *
1916 * When "set" operations are inlinable, they involve writing that mask to
1917 * one register to set a low value, or a different register to set it high.
1918 * Otherwise locking is needed, so there may be little value to inlining.
1919 *
1920 *------------------------------------------------------------------------
1921 *
1922 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1923 * have requested the GPIO. That can include implicit requesting by
1924 * a direction setting call. Marking a gpio as requested locks its chip
1925 * in memory, guaranteeing that these table lookups need no more locking
1926 * and that gpiochip_remove() will fail.
1927 *
1928 * REVISIT when debugging, consider adding some instrumentation to ensure
1929 * that the GPIO was actually requested.
1930 */
1931
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001932static int _gpiod_get_raw_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001933{
1934 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001935 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001936 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001937
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001938 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001939 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001940 value = chip->get ? chip->get(chip, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01001941 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001942 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001943 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001944}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001945
David Brownelld2876d02008-02-04 22:28:20 -08001946/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001947 * gpiod_get_raw_value() - return a gpio's raw value
1948 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08001949 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001950 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001951 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001952 *
1953 * This function should be called from contexts where we cannot sleep, and will
1954 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001955 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001956int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001957{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001958 VALIDATE_DESC(desc);
David Brownelld2876d02008-02-04 22:28:20 -08001959 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001960 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001961 return _gpiod_get_raw_value(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001962}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001963EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001964
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001965/**
1966 * gpiod_get_value() - return a gpio's value
1967 * @desc: gpio whose value will be returned
1968 *
1969 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001970 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001971 *
1972 * This function should be called from contexts where we cannot sleep, and will
1973 * complain if the GPIO chip functions potentially sleep.
1974 */
1975int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001976{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001977 int value;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001978
1979 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001980 /* Should be using gpio_get_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01001981 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001982
1983 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001984 if (value < 0)
1985 return value;
1986
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001987 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1988 value = !value;
1989
1990 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001991}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001992EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08001993
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301994/*
1995 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001996 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07001997 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301998 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001999static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302000{
2001 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002002 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002003 int offset = gpio_chip_hwgpio(desc);
2004
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302005 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002006 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302007 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002008 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302009 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002010 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302011 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002012 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302013 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002014 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302015 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002016 gpiod_err(desc,
2017 "%s: Error in set_value for open drain err %d\n",
2018 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302019}
2020
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302021/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002022 * _gpio_set_open_source_value() - Set the open source gpio's value.
2023 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07002024 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302025 */
Alexandre Courbot23600962014-03-11 15:52:09 +09002026static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302027{
2028 int err = 0;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002029 struct gpio_chip *chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002030 int offset = gpio_chip_hwgpio(desc);
2031
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302032 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002033 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302034 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002035 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302036 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09002037 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302038 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002039 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302040 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09002041 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302042 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01002043 gpiod_err(desc,
2044 "%s: Error in set_value for open source err %d\n",
2045 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05302046}
2047
Alexandre Courbot23600962014-03-11 15:52:09 +09002048static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08002049{
2050 struct gpio_chip *chip;
2051
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002052 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002053 trace_gpio_value(desc_to_gpio(desc), 0, value);
2054 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2055 _gpio_set_open_drain_value(desc, value);
2056 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2057 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05302058 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09002059 chip->set(chip, gpio_chip_hwgpio(desc), value);
2060}
2061
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002062/*
2063 * set multiple outputs on the same chip;
2064 * use the chip's set_multiple function if available;
2065 * otherwise set the outputs sequentially;
2066 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
2067 * defines which outputs are to be changed
2068 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
2069 * defines the values the outputs specified by mask are to be set to
2070 */
2071static void gpio_chip_set_multiple(struct gpio_chip *chip,
2072 unsigned long *mask, unsigned long *bits)
2073{
2074 if (chip->set_multiple) {
2075 chip->set_multiple(chip, mask, bits);
2076 } else {
2077 int i;
2078 for (i = 0; i < chip->ngpio; i++) {
2079 if (mask[BIT_WORD(i)] == 0) {
2080 /* no more set bits in this mask word;
2081 * skip ahead to the next word */
2082 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
2083 continue;
2084 }
2085 /* set outputs if the corresponding mask bit is set */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002086 if (__test_and_clear_bit(i, mask))
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002087 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002088 }
2089 }
2090}
2091
Linus Walleij44c72882016-04-24 11:36:59 +02002092void gpiod_set_array_value_complex(bool raw, bool can_sleep,
2093 unsigned int array_size,
2094 struct gpio_desc **desc_array,
2095 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002096{
2097 int i = 0;
2098
2099 while (i < array_size) {
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002100 struct gpio_chip *chip = desc_array[i]->gdev->chip;
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002101 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2102 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2103 int count = 0;
2104
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002105 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002106 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002107
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002108 memset(mask, 0, sizeof(mask));
2109 do {
2110 struct gpio_desc *desc = desc_array[i];
2111 int hwgpio = gpio_chip_hwgpio(desc);
2112 int value = value_array[i];
2113
2114 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2115 value = !value;
2116 trace_gpio_value(desc_to_gpio(desc), 0, value);
2117 /*
2118 * collect all normal outputs belonging to the same chip
2119 * open drain and open source outputs are set individually
2120 */
2121 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002122 _gpio_set_open_drain_value(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002123 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2124 _gpio_set_open_source_value(desc, value);
2125 } else {
2126 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002127 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002128 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002129 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002130 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002131 count++;
2132 }
2133 i++;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002134 } while ((i < array_size) &&
2135 (desc_array[i]->gdev->chip == chip));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002136 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01002137 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002138 gpio_chip_set_multiple(chip, mask, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002139 }
2140}
2141
David Brownelld2876d02008-02-04 22:28:20 -08002142/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002143 * gpiod_set_raw_value() - assign a gpio's raw value
2144 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08002145 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002146 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002147 * Set the raw value of the GPIO, i.e. the value of its physical line without
2148 * regard for its ACTIVE_LOW status.
2149 *
2150 * This function should be called from contexts where we cannot sleep, and will
2151 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002152 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002153void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002154{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002155 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01002156 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002157 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002158 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002159}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002160EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08002161
2162/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002163 * gpiod_set_value() - assign a gpio's value
2164 * @desc: gpio whose value will be assigned
2165 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08002166 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002167 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2168 * account
2169 *
2170 * This function should be called from contexts where we cannot sleep, and will
2171 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002172 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002173void gpiod_set_value(struct gpio_desc *desc, int value)
2174{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002175 VALIDATE_DESC_VOID(desc);
Geert Uytterhoeven1cfab8f2016-03-14 16:24:12 +01002176 /* Should be using gpiod_set_value_cansleep() */
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002177 WARN_ON(desc->gdev->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002178 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2179 value = !value;
2180 _gpiod_set_raw_value(desc, value);
2181}
2182EXPORT_SYMBOL_GPL(gpiod_set_value);
2183
2184/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002185 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002186 * @array_size: number of elements in the descriptor / value arrays
2187 * @desc_array: array of GPIO descriptors whose values will be assigned
2188 * @value_array: array of values to assign
2189 *
2190 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2191 * without regard for their ACTIVE_LOW status.
2192 *
2193 * This function should be called from contexts where we cannot sleep, and will
2194 * complain if the GPIO chip functions potentially sleep.
2195 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002196void gpiod_set_raw_array_value(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002197 struct gpio_desc **desc_array, int *value_array)
2198{
2199 if (!desc_array)
2200 return;
Linus Walleij44c72882016-04-24 11:36:59 +02002201 gpiod_set_array_value_complex(true, false, array_size, desc_array,
2202 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002203}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002204EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002205
2206/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002207 * gpiod_set_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002208 * @array_size: number of elements in the descriptor / value arrays
2209 * @desc_array: array of GPIO descriptors whose values will be assigned
2210 * @value_array: array of values to assign
2211 *
2212 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2213 * into account.
2214 *
2215 * This function should be called from contexts where we cannot sleep, and will
2216 * complain if the GPIO chip functions potentially sleep.
2217 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002218void gpiod_set_array_value(unsigned int array_size,
2219 struct gpio_desc **desc_array, int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002220{
2221 if (!desc_array)
2222 return;
Linus Walleij44c72882016-04-24 11:36:59 +02002223 gpiod_set_array_value_complex(false, false, array_size, desc_array,
2224 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002225}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002226EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002227
2228/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002229 * gpiod_cansleep() - report whether gpio value access may sleep
2230 * @desc: gpio to check
2231 *
2232 */
2233int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002234{
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002235 VALIDATE_DESC(desc);
2236 return desc->gdev->chip->can_sleep;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002237}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002238EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002239
David Brownell0f6d5042008-10-15 22:03:14 -07002240/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002241 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
2242 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07002243 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002244 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
2245 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07002246 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002247int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07002248{
Linus Walleij4c37ce82016-05-02 13:13:10 +02002249 struct gpio_chip *chip;
2250 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07002251
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002252 VALIDATE_DESC(desc);
2253 chip = desc->gdev->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002254 offset = gpio_chip_hwgpio(desc);
Linus Walleij4c37ce82016-05-02 13:13:10 +02002255 if (chip->to_irq) {
2256 int retirq = chip->to_irq(chip, offset);
2257
2258 /* Zero means NO_IRQ */
2259 if (!retirq)
2260 return -ENXIO;
2261
2262 return retirq;
2263 }
2264 return -ENXIO;
Alexandre Courbot372e7222013-02-03 01:29:29 +09002265}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002266EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002267
Linus Walleijd468bf92013-09-24 11:54:38 +02002268/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002269 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002270 * @chip: the chip the GPIO to lock belongs to
2271 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02002272 *
2273 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08002274 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08002275 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002276int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08002277{
Linus Walleij9c102802016-05-25 10:56:03 +02002278 struct gpio_desc *desc;
Linus Walleijd468bf92013-09-24 11:54:38 +02002279
Linus Walleij9c102802016-05-25 10:56:03 +02002280 desc = gpiochip_get_desc(chip, offset);
2281 if (IS_ERR(desc))
2282 return PTR_ERR(desc);
2283
2284 /* Flush direction if something changed behind our back */
2285 if (chip->get_direction) {
2286 int dir = chip->get_direction(chip, offset);
2287
2288 if (dir)
2289 clear_bit(FLAG_IS_OUT, &desc->flags);
2290 else
2291 set_bit(FLAG_IS_OUT, &desc->flags);
2292 }
2293
2294 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002295 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02002296 "%s: tried to flag a GPIO set as output for IRQ\n",
2297 __func__);
2298 return -EIO;
2299 }
2300
Linus Walleij9c102802016-05-25 10:56:03 +02002301 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002302 return 0;
2303}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002304EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02002305
Linus Walleijd468bf92013-09-24 11:54:38 +02002306/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002307 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002308 * @chip: the chip the GPIO to lock belongs to
2309 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02002310 *
2311 * This is used directly by GPIO drivers that want to indicate
2312 * that a certain GPIO is no longer used exclusively for IRQ.
2313 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002314void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02002315{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09002316 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02002317 return;
2318
Linus Walleij1c3cdb12016-02-09 13:51:59 +01002319 clear_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002320}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09002321EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02002322
Linus Walleij6cee3822016-02-11 20:16:45 +01002323bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
2324{
2325 if (offset >= chip->ngpio)
2326 return false;
2327
2328 return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
2329}
2330EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
2331
Linus Walleij143b65d2016-02-16 15:41:42 +01002332bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
2333{
2334 if (offset >= chip->ngpio)
2335 return false;
2336
2337 return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
2338}
2339EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
2340
2341bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
2342{
2343 if (offset >= chip->ngpio)
2344 return false;
2345
2346 return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
2347}
2348EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
2349
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002350/**
2351 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
2352 * @desc: gpio whose value will be returned
2353 *
2354 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002355 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002356 *
2357 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08002358 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002359int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08002360{
David Brownelld2876d02008-02-04 22:28:20 -08002361 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002362 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002363 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08002364}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002365EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002366
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002367/**
2368 * gpiod_get_value_cansleep() - return a gpio's value
2369 * @desc: gpio whose value will be returned
2370 *
2371 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002372 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002373 *
2374 * This function is to be called from contexts that can sleep.
2375 */
2376int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002377{
David Brownelld2876d02008-02-04 22:28:20 -08002378 int value;
David Brownelld2876d02008-02-04 22:28:20 -08002379
2380 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002381 VALIDATE_DESC(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002382 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07002383 if (value < 0)
2384 return value;
2385
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002386 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2387 value = !value;
2388
David Brownelld2876d02008-02-04 22:28:20 -08002389 return value;
2390}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002391EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002392
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002393/**
2394 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
2395 * @desc: gpio whose value will be assigned
2396 * @value: value to assign
2397 *
2398 * Set the raw value of the GPIO, i.e. the value of its physical line without
2399 * regard for its ACTIVE_LOW status.
2400 *
2401 * This function is to be called from contexts that can sleep.
2402 */
2403void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002404{
David Brownelld2876d02008-02-04 22:28:20 -08002405 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002406 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002407 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002408}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002409EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09002410
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002411/**
2412 * gpiod_set_value_cansleep() - assign a gpio's value
2413 * @desc: gpio whose value will be assigned
2414 * @value: value to assign
2415 *
2416 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2417 * account
2418 *
2419 * This function is to be called from contexts that can sleep.
2420 */
2421void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09002422{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002423 might_sleep_if(extra_checks);
Linus Walleijfdeb8e12016-02-10 10:57:36 +01002424 VALIDATE_DESC_VOID(desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002425 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2426 value = !value;
2427 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08002428}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07002429EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08002430
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002431/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002432 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002433 * @array_size: number of elements in the descriptor / value arrays
2434 * @desc_array: array of GPIO descriptors whose values will be assigned
2435 * @value_array: array of values to assign
2436 *
2437 * Set the raw values of the GPIOs, i.e. the values of the physical lines
2438 * without regard for their ACTIVE_LOW status.
2439 *
2440 * This function is to be called from contexts that can sleep.
2441 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002442void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
2443 struct gpio_desc **desc_array,
2444 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002445{
2446 might_sleep_if(extra_checks);
2447 if (!desc_array)
2448 return;
Linus Walleij44c72882016-04-24 11:36:59 +02002449 gpiod_set_array_value_complex(true, true, array_size, desc_array,
2450 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002451}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002452EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002453
2454/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002455 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002456 * @array_size: number of elements in the descriptor / value arrays
2457 * @desc_array: array of GPIO descriptors whose values will be assigned
2458 * @value_array: array of values to assign
2459 *
2460 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2461 * into account.
2462 *
2463 * This function is to be called from contexts that can sleep.
2464 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002465void gpiod_set_array_value_cansleep(unsigned int array_size,
2466 struct gpio_desc **desc_array,
2467 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002468{
2469 might_sleep_if(extra_checks);
2470 if (!desc_array)
2471 return;
Linus Walleij44c72882016-04-24 11:36:59 +02002472 gpiod_set_array_value_complex(false, true, array_size, desc_array,
2473 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002474}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02002475EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01002476
2477/**
Alexandre Courbotad824782013-12-03 12:20:11 +09002478 * gpiod_add_lookup_table() - register GPIO device consumers
2479 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002480 */
Alexandre Courbotad824782013-12-03 12:20:11 +09002481void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002482{
2483 mutex_lock(&gpio_lookup_lock);
2484
Alexandre Courbotad824782013-12-03 12:20:11 +09002485 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002486
2487 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08002488}
2489
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05302490/**
2491 * gpiod_remove_lookup_table() - unregister GPIO device consumers
2492 * @table: table of consumers to unregister
2493 */
2494void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
2495{
2496 mutex_lock(&gpio_lookup_lock);
2497
2498 list_del(&table->list);
2499
2500 mutex_unlock(&gpio_lookup_lock);
2501}
2502
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002503static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002504 unsigned int idx,
2505 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002506{
2507 char prop_name[32]; /* 32 is max size of property name */
2508 enum of_gpio_flags of_flags;
2509 struct gpio_desc *desc;
Thierry Redingdd34c372014-04-23 17:28:09 +02002510 unsigned int i;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002511
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002512 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Thierry Redingdd34c372014-04-23 17:28:09 +02002513 if (con_id)
Olliver Schinagl9e089242015-01-21 22:33:45 +01002514 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002515 gpio_suffixes[i]);
Thierry Redingdd34c372014-04-23 17:28:09 +02002516 else
Olliver Schinagl9e089242015-01-21 22:33:45 +01002517 snprintf(prop_name, sizeof(prop_name), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002518 gpio_suffixes[i]);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002519
Thierry Redingdd34c372014-04-23 17:28:09 +02002520 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
2521 &of_flags);
Tony Lindgren06fc3b72014-06-02 16:13:46 -07002522 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
Thierry Redingdd34c372014-04-23 17:28:09 +02002523 break;
2524 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002525
2526 if (IS_ERR(desc))
2527 return desc;
2528
2529 if (of_flags & OF_GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002530 *flags |= GPIO_ACTIVE_LOW;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002531
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002532 if (of_flags & OF_GPIO_SINGLE_ENDED) {
2533 if (of_flags & OF_GPIO_ACTIVE_LOW)
2534 *flags |= GPIO_OPEN_DRAIN;
2535 else
2536 *flags |= GPIO_OPEN_SOURCE;
2537 }
2538
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002539 return desc;
2540}
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002541
Dmitry Torokhov25487532016-03-24 10:50:25 -07002542static struct gpio_desc *acpi_find_gpio(struct device *dev,
2543 const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002544 unsigned int idx,
Dmitry Torokhov25487532016-03-24 10:50:25 -07002545 enum gpiod_flags flags,
2546 enum gpio_lookup_flags *lookupflags)
Mika Westerberg81f59e92013-10-10 11:01:09 +03002547{
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002548 struct acpi_device *adev = ACPI_COMPANION(dev);
Mika Westerberge01f4402013-10-10 11:01:10 +03002549 struct acpi_gpio_info info;
2550 struct gpio_desc *desc;
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002551 char propname[32];
2552 int i;
Mika Westerberge01f4402013-10-10 11:01:10 +03002553
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002554 /* Try first from _DSD */
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002555 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002556 if (con_id && strcmp(con_id, "gpios")) {
2557 snprintf(propname, sizeof(propname), "%s-%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002558 con_id, gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002559 } else {
2560 snprintf(propname, sizeof(propname), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01002561 gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002562 }
Mika Westerberge01f4402013-10-10 11:01:10 +03002563
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002564 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
2565 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
2566 break;
2567 }
2568
2569 /* Then from plain _CRS GPIOs */
2570 if (IS_ERR(desc)) {
Dmitry Torokhov10cf4892015-11-11 11:45:30 -08002571 if (!acpi_can_fallback_to_crs(adev, con_id))
2572 return ERR_PTR(-ENOENT);
2573
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002574 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
2575 if (IS_ERR(desc))
2576 return desc;
Dmitry Torokhov25487532016-03-24 10:50:25 -07002577
2578 if ((flags == GPIOD_OUT_LOW || flags == GPIOD_OUT_HIGH) &&
2579 info.gpioint) {
2580 dev_dbg(dev, "refusing GpioInt() entry when doing GPIOD_OUT_* lookup\n");
2581 return ERR_PTR(-ENOENT);
2582 }
Mika Westerberg0d9a6932014-10-29 15:41:01 +01002583 }
2584
Christophe RICARD52044722015-12-23 23:25:34 +01002585 if (info.polarity == GPIO_ACTIVE_LOW)
Dmitry Torokhov25487532016-03-24 10:50:25 -07002586 *lookupflags |= GPIO_ACTIVE_LOW;
Mika Westerberge01f4402013-10-10 11:01:10 +03002587
2588 return desc;
Mika Westerberg81f59e92013-10-10 11:01:09 +03002589}
2590
Alexandre Courbotad824782013-12-03 12:20:11 +09002591static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
2592{
2593 const char *dev_id = dev ? dev_name(dev) : NULL;
2594 struct gpiod_lookup_table *table;
2595
2596 mutex_lock(&gpio_lookup_lock);
2597
2598 list_for_each_entry(table, &gpio_lookup_list, list) {
2599 if (table->dev_id && dev_id) {
2600 /*
2601 * Valid strings on both ends, must be identical to have
2602 * a match
2603 */
2604 if (!strcmp(table->dev_id, dev_id))
2605 goto found;
2606 } else {
2607 /*
2608 * One of the pointers is NULL, so both must be to have
2609 * a match
2610 */
2611 if (dev_id == table->dev_id)
2612 goto found;
2613 }
2614 }
2615 table = NULL;
2616
2617found:
2618 mutex_unlock(&gpio_lookup_lock);
2619 return table;
2620}
2621
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002622static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002623 unsigned int idx,
2624 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002625{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002626 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09002627 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002628 struct gpiod_lookup *p;
2629
Alexandre Courbotad824782013-12-03 12:20:11 +09002630 table = gpiod_find_lookup_table(dev);
2631 if (!table)
2632 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002633
Alexandre Courbotad824782013-12-03 12:20:11 +09002634 for (p = &table->table[0]; p->chip_label; p++) {
2635 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002636
Alexandre Courbotad824782013-12-03 12:20:11 +09002637 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002638 if (p->idx != idx)
2639 continue;
2640
Alexandre Courbotad824782013-12-03 12:20:11 +09002641 /* If the lookup entry has a con_id, require exact match */
2642 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
2643 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002644
Alexandre Courbotad824782013-12-03 12:20:11 +09002645 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002646
Alexandre Courbotad824782013-12-03 12:20:11 +09002647 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002648 dev_err(dev, "cannot find GPIO chip %s\n",
2649 p->chip_label);
2650 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002651 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002652
Alexandre Courbotad824782013-12-03 12:20:11 +09002653 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002654 dev_err(dev,
2655 "requested GPIO %d is out of range [0..%d] for chip %s\n",
2656 idx, chip->ngpio, chip->label);
2657 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09002658 }
2659
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09002660 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09002661 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002662
2663 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09002664 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002665
2666 return desc;
2667}
2668
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002669static int dt_gpio_count(struct device *dev, const char *con_id)
2670{
2671 int ret;
2672 char propname[32];
2673 unsigned int i;
2674
2675 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
2676 if (con_id)
2677 snprintf(propname, sizeof(propname), "%s-%s",
2678 con_id, gpio_suffixes[i]);
2679 else
2680 snprintf(propname, sizeof(propname), "%s",
2681 gpio_suffixes[i]);
2682
2683 ret = of_gpio_named_count(dev->of_node, propname);
2684 if (ret >= 0)
2685 break;
2686 }
2687 return ret;
2688}
2689
2690static int platform_gpio_count(struct device *dev, const char *con_id)
2691{
2692 struct gpiod_lookup_table *table;
2693 struct gpiod_lookup *p;
2694 unsigned int count = 0;
2695
2696 table = gpiod_find_lookup_table(dev);
2697 if (!table)
2698 return -ENOENT;
2699
2700 for (p = &table->table[0]; p->chip_label; p++) {
2701 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
2702 (!con_id && !p->con_id))
2703 count++;
2704 }
2705 if (!count)
2706 return -ENOENT;
2707
2708 return count;
2709}
2710
2711/**
2712 * gpiod_count - return the number of GPIOs associated with a device / function
2713 * or -ENOENT if no GPIO has been assigned to the requested function
2714 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2715 * @con_id: function within the GPIO consumer
2716 */
2717int gpiod_count(struct device *dev, const char *con_id)
2718{
2719 int count = -ENOENT;
2720
2721 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
2722 count = dt_gpio_count(dev, con_id);
2723 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
2724 count = acpi_gpio_count(dev, con_id);
2725
2726 if (count < 0)
2727 count = platform_gpio_count(dev, con_id);
2728
2729 return count;
2730}
2731EXPORT_SYMBOL_GPL(gpiod_count);
2732
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002733/**
Thierry Reding08791622014-04-25 16:54:22 +02002734 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09002735 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002736 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002737 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002738 *
2739 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002740 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07002741 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002742 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002743struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002744 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002745{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002746 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002747}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002748EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002749
2750/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02002751 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
2752 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2753 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002754 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02002755 *
2756 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
2757 * the requested function it will return NULL. This is convenient for drivers
2758 * that need to handle optional GPIOs.
2759 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002760struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002761 const char *con_id,
2762 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02002763{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002764 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002765}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002766EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002767
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002768/**
2769 * gpiod_parse_flags - helper function to parse GPIO lookup flags
2770 * @desc: gpio to be setup
2771 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2772 * of_get_gpio_hog()
2773 *
2774 * Set the GPIO descriptor flags based on the given GPIO lookup flags.
2775 */
2776static void gpiod_parse_flags(struct gpio_desc *desc, unsigned long lflags)
2777{
2778 if (lflags & GPIO_ACTIVE_LOW)
2779 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2780 if (lflags & GPIO_OPEN_DRAIN)
2781 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2782 if (lflags & GPIO_OPEN_SOURCE)
2783 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2784}
Benoit Parrotf625d462015-02-02 11:44:44 -06002785
2786/**
2787 * gpiod_configure_flags - helper function to configure a given GPIO
2788 * @desc: gpio whose value will be assigned
2789 * @con_id: function within the GPIO consumer
Benoit Parrotf625d462015-02-02 11:44:44 -06002790 * @dflags: gpiod_flags - optional GPIO initialization flags
2791 *
2792 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
2793 * requested function and/or index, or another IS_ERR() code if an error
2794 * occurred while trying to acquire the GPIO.
2795 */
2796static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002797 enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06002798{
2799 int status;
2800
Benoit Parrotf625d462015-02-02 11:44:44 -06002801 /* No particular flag request, return here... */
2802 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
2803 pr_debug("no flags found for %s\n", con_id);
2804 return 0;
2805 }
2806
2807 /* Process flags */
2808 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
2809 status = gpiod_direction_output(desc,
2810 dflags & GPIOD_FLAGS_BIT_DIR_VAL);
2811 else
2812 status = gpiod_direction_input(desc);
2813
2814 return status;
2815}
2816
Thierry Reding29a1f2332014-04-25 17:10:06 +02002817/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002818 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02002819 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002820 * @con_id: function within the GPIO consumer
2821 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002822 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002823 *
2824 * This variant of gpiod_get() allows to access GPIOs other than the first
2825 * defined one for functions that define several GPIOs.
2826 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002827 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
2828 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07002829 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002830 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002831struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002832 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002833 unsigned int idx,
2834 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002835{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09002836 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002837 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002838 enum gpio_lookup_flags lookupflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002839
2840 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
2841
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01002842 if (dev) {
2843 /* Using device tree? */
2844 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
2845 dev_dbg(dev, "using device tree for GPIO lookup\n");
2846 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
2847 } else if (ACPI_COMPANION(dev)) {
2848 dev_dbg(dev, "using ACPI for GPIO lookup\n");
Dmitry Torokhov25487532016-03-24 10:50:25 -07002849 desc = acpi_find_gpio(dev, con_id, idx, flags, &lookupflags);
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01002850 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09002851 }
2852
2853 /*
2854 * Either we are not using DT or ACPI, or their lookup did not return
2855 * a result. In that case, use platform lookup as a fallback.
2856 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002857 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04002858 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002859 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002860 }
2861
2862 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02002863 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002864 return desc;
2865 }
2866
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002867 gpiod_parse_flags(desc, lookupflags);
2868
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002869 status = gpiod_request(desc, con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002870 if (status < 0)
2871 return ERR_PTR(status);
2872
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002873 status = gpiod_configure_flags(desc, con_id, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002874 if (status < 0) {
2875 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
2876 gpiod_put(desc);
2877 return ERR_PTR(status);
2878 }
2879
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002880 return desc;
2881}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002882EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002883
2884/**
Mika Westerberg40b73182014-10-21 13:33:59 +02002885 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
2886 * @fwnode: handle of the firmware node
2887 * @propname: name of the firmware property representing the GPIO
2888 *
2889 * This function can be used for drivers that get their configuration
2890 * from firmware.
2891 *
2892 * Function properly finds the corresponding GPIO using whatever is the
2893 * underlying firmware interface and then makes sure that the GPIO
2894 * descriptor is requested before it is returned to the caller.
2895 *
2896 * In case of error an ERR_PTR() is returned.
2897 */
2898struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
2899 const char *propname)
2900{
2901 struct gpio_desc *desc = ERR_PTR(-ENODEV);
2902 bool active_low = false;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002903 bool single_ended = false;
Mika Westerberg40b73182014-10-21 13:33:59 +02002904 int ret;
2905
2906 if (!fwnode)
2907 return ERR_PTR(-EINVAL);
2908
2909 if (is_of_node(fwnode)) {
2910 enum of_gpio_flags flags;
2911
Alexander Sverdlinc181fb32015-06-22 22:38:53 +02002912 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, 0,
Mika Westerberg40b73182014-10-21 13:33:59 +02002913 &flags);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002914 if (!IS_ERR(desc)) {
Mika Westerberg40b73182014-10-21 13:33:59 +02002915 active_low = flags & OF_GPIO_ACTIVE_LOW;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002916 single_ended = flags & OF_GPIO_SINGLE_ENDED;
2917 }
Mika Westerberg40b73182014-10-21 13:33:59 +02002918 } else if (is_acpi_node(fwnode)) {
2919 struct acpi_gpio_info info;
2920
Rafael J. Wysocki504a3372015-08-27 04:42:33 +02002921 desc = acpi_node_get_gpiod(fwnode, propname, 0, &info);
Mika Westerberg40b73182014-10-21 13:33:59 +02002922 if (!IS_ERR(desc))
Christophe RICARD52044722015-12-23 23:25:34 +01002923 active_low = info.polarity == GPIO_ACTIVE_LOW;
Mika Westerberg40b73182014-10-21 13:33:59 +02002924 }
2925
2926 if (IS_ERR(desc))
2927 return desc;
2928
Mika Westerberg40b73182014-10-21 13:33:59 +02002929 if (active_low)
2930 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2931
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002932 if (single_ended) {
2933 if (active_low)
2934 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2935 else
2936 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2937 }
2938
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002939 ret = gpiod_request(desc, NULL);
2940 if (ret)
2941 return ERR_PTR(ret);
2942
Mika Westerberg40b73182014-10-21 13:33:59 +02002943 return desc;
2944}
2945EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
2946
2947/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02002948 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
2949 * function
2950 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2951 * @con_id: function within the GPIO consumer
2952 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002953 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02002954 *
2955 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
2956 * specified index was assigned to the requested function it will return NULL.
2957 * This is convenient for drivers that need to handle optional GPIOs.
2958 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002959struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02002960 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002961 unsigned int index,
2962 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02002963{
2964 struct gpio_desc *desc;
2965
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002966 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002967 if (IS_ERR(desc)) {
2968 if (PTR_ERR(desc) == -ENOENT)
2969 return NULL;
2970 }
2971
2972 return desc;
2973}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002974EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002975
2976/**
Benoit Parrotf625d462015-02-02 11:44:44 -06002977 * gpiod_hog - Hog the specified GPIO desc given the provided flags
2978 * @desc: gpio whose value will be assigned
2979 * @name: gpio line name
2980 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2981 * of_get_gpio_hog()
2982 * @dflags: gpiod_flags - optional GPIO initialization flags
2983 */
2984int gpiod_hog(struct gpio_desc *desc, const char *name,
2985 unsigned long lflags, enum gpiod_flags dflags)
2986{
2987 struct gpio_chip *chip;
2988 struct gpio_desc *local_desc;
2989 int hwnum;
2990 int status;
2991
2992 chip = gpiod_to_chip(desc);
2993 hwnum = gpio_chip_hwgpio(desc);
2994
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002995 gpiod_parse_flags(desc, lflags);
2996
Benoit Parrotf625d462015-02-02 11:44:44 -06002997 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
2998 if (IS_ERR(local_desc)) {
Laxman Dewanganc31a5712016-03-11 19:13:21 +05302999 status = PTR_ERR(local_desc);
3000 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
3001 name, chip->label, hwnum, status);
3002 return status;
Benoit Parrotf625d462015-02-02 11:44:44 -06003003 }
3004
Laurent Pinchart923b93e2015-10-13 00:20:20 +03003005 status = gpiod_configure_flags(desc, name, dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -06003006 if (status < 0) {
Laxman Dewanganc31a5712016-03-11 19:13:21 +05303007 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed, %d\n",
3008 name, chip->label, hwnum, status);
Benoit Parrotf625d462015-02-02 11:44:44 -06003009 gpiochip_free_own_desc(desc);
3010 return status;
3011 }
3012
3013 /* Mark GPIO as hogged so it can be identified and removed later */
3014 set_bit(FLAG_IS_HOGGED, &desc->flags);
3015
3016 pr_info("GPIO line %d (%s) hogged as %s%s\n",
3017 desc_to_gpio(desc), name,
3018 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
3019 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
3020 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
3021
3022 return 0;
3023}
3024
3025/**
3026 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
3027 * @chip: gpio chip to act on
3028 *
3029 * This is only used by of_gpiochip_remove to free hogged gpios
3030 */
3031static void gpiochip_free_hogs(struct gpio_chip *chip)
3032{
3033 int id;
3034
3035 for (id = 0; id < chip->ngpio; id++) {
Linus Walleij1c3cdb12016-02-09 13:51:59 +01003036 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
3037 gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
Benoit Parrotf625d462015-02-02 11:44:44 -06003038 }
3039}
3040
3041/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003042 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
3043 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3044 * @con_id: function within the GPIO consumer
3045 * @flags: optional GPIO initialization flags
3046 *
3047 * This function acquires all the GPIOs defined under a given function.
3048 *
3049 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
3050 * no GPIO has been assigned to the requested function, or another IS_ERR()
3051 * code if an error occurred while trying to acquire the GPIOs.
3052 */
3053struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
3054 const char *con_id,
3055 enum gpiod_flags flags)
3056{
3057 struct gpio_desc *desc;
3058 struct gpio_descs *descs;
3059 int count;
3060
3061 count = gpiod_count(dev, con_id);
3062 if (count < 0)
3063 return ERR_PTR(count);
3064
3065 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
3066 GFP_KERNEL);
3067 if (!descs)
3068 return ERR_PTR(-ENOMEM);
3069
3070 for (descs->ndescs = 0; descs->ndescs < count; ) {
3071 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
3072 if (IS_ERR(desc)) {
3073 gpiod_put_array(descs);
3074 return ERR_CAST(desc);
3075 }
3076 descs->desc[descs->ndescs] = desc;
3077 descs->ndescs++;
3078 }
3079 return descs;
3080}
3081EXPORT_SYMBOL_GPL(gpiod_get_array);
3082
3083/**
3084 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
3085 * function
3086 * @dev: GPIO consumer, can be NULL for system-global GPIOs
3087 * @con_id: function within the GPIO consumer
3088 * @flags: optional GPIO initialization flags
3089 *
3090 * This is equivalent to gpiod_get_array(), except that when no GPIO was
3091 * assigned to the requested function it will return NULL.
3092 */
3093struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
3094 const char *con_id,
3095 enum gpiod_flags flags)
3096{
3097 struct gpio_descs *descs;
3098
3099 descs = gpiod_get_array(dev, con_id, flags);
3100 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
3101 return NULL;
3102
3103 return descs;
3104}
3105EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
3106
3107/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07003108 * gpiod_put - dispose of a GPIO descriptor
3109 * @desc: GPIO descriptor to dispose of
3110 *
3111 * No descriptor can be used after gpiod_put() has been called on it.
3112 */
3113void gpiod_put(struct gpio_desc *desc)
3114{
3115 gpiod_free(desc);
3116}
3117EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08003118
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01003119/**
3120 * gpiod_put_array - dispose of multiple GPIO descriptors
3121 * @descs: struct gpio_descs containing an array of descriptors
3122 */
3123void gpiod_put_array(struct gpio_descs *descs)
3124{
3125 unsigned int i;
3126
3127 for (i = 0; i < descs->ndescs; i++)
3128 gpiod_put(descs->desc[i]);
3129
3130 kfree(descs);
3131}
3132EXPORT_SYMBOL_GPL(gpiod_put_array);
3133
Linus Walleij3c702e92015-10-21 15:29:53 +02003134static int __init gpiolib_dev_init(void)
3135{
3136 int ret;
3137
3138 /* Register GPIO sysfs bus */
3139 ret = bus_register(&gpio_bus_type);
3140 if (ret < 0) {
3141 pr_err("gpiolib: could not register GPIO bus type\n");
3142 return ret;
3143 }
3144
3145 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
3146 if (ret < 0) {
3147 pr_err("gpiolib: failed to allocate char dev region\n");
3148 bus_unregister(&gpio_bus_type);
Guenter Roeck159f3cd2016-03-31 08:11:30 -07003149 } else {
3150 gpiolib_initialized = true;
3151 gpiochip_setup_devs();
Linus Walleij3c702e92015-10-21 15:29:53 +02003152 }
3153 return ret;
3154}
3155core_initcall(gpiolib_dev_init);
3156
David Brownelld2876d02008-02-04 22:28:20 -08003157#ifdef CONFIG_DEBUG_FS
3158
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003159static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
David Brownelld2876d02008-02-04 22:28:20 -08003160{
3161 unsigned i;
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003162 struct gpio_chip *chip = gdev->chip;
3163 unsigned gpio = gdev->base;
3164 struct gpio_desc *gdesc = &gdev->descs[0];
David Brownelld2876d02008-02-04 22:28:20 -08003165 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02003166 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08003167
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003168 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02003169 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
3170 if (gdesc->name) {
3171 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
3172 gpio, gdesc->name);
3173 }
David Brownelld2876d02008-02-04 22:28:20 -08003174 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02003175 }
David Brownelld2876d02008-02-04 22:28:20 -08003176
Alexandre Courbot372e7222013-02-03 01:29:29 +09003177 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08003178 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02003179 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Markus Pargmannced433e2015-08-14 16:11:02 +02003180 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
3181 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08003182 is_out ? "out" : "in ",
3183 chip->get
3184 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02003185 : "? ",
3186 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08003187 seq_printf(s, "\n");
3188 }
3189}
3190
Thierry Redingf9c4a312012-04-12 13:26:01 +02003191static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08003192{
Grant Likely362432a2013-02-09 09:41:49 +00003193 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02003194 struct gpio_device *gdev = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003195 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003196
3197 s->private = "";
3198
Grant Likely362432a2013-02-09 09:41:49 +00003199 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003200 list_for_each_entry(gdev, &gpio_devices, list)
Grant Likely362432a2013-02-09 09:41:49 +00003201 if (index-- == 0) {
3202 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003203 return gdev;
Grant Likely362432a2013-02-09 09:41:49 +00003204 }
3205 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003206
3207 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003208}
3209
3210static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
3211{
Grant Likely362432a2013-02-09 09:41:49 +00003212 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02003213 struct gpio_device *gdev = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003214 void *ret = NULL;
3215
Grant Likely362432a2013-02-09 09:41:49 +00003216 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02003217 if (list_is_last(&gdev->list, &gpio_devices))
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09003218 ret = NULL;
3219 else
Linus Walleijff2b1352015-10-20 11:10:38 +02003220 ret = list_entry(gdev->list.next, struct gpio_device, list);
Grant Likely362432a2013-02-09 09:41:49 +00003221 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02003222
3223 s->private = "\n";
3224 ++*pos;
3225
3226 return ret;
3227}
3228
3229static void gpiolib_seq_stop(struct seq_file *s, void *v)
3230{
3231}
3232
3233static int gpiolib_seq_show(struct seq_file *s, void *v)
3234{
Linus Walleijff2b1352015-10-20 11:10:38 +02003235 struct gpio_device *gdev = v;
3236 struct gpio_chip *chip = gdev->chip;
3237 struct device *parent;
Thierry Redingf9c4a312012-04-12 13:26:01 +02003238
Linus Walleijff2b1352015-10-20 11:10:38 +02003239 if (!chip) {
3240 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
3241 dev_name(&gdev->dev));
3242 return 0;
3243 }
3244
3245 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
3246 dev_name(&gdev->dev),
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003247 gdev->base, gdev->base + gdev->ngpio - 1);
Linus Walleijff2b1352015-10-20 11:10:38 +02003248 parent = chip->parent;
3249 if (parent)
3250 seq_printf(s, ", parent: %s/%s",
3251 parent->bus ? parent->bus->name : "no-bus",
3252 dev_name(parent));
Thierry Redingf9c4a312012-04-12 13:26:01 +02003253 if (chip->label)
3254 seq_printf(s, ", %s", chip->label);
3255 if (chip->can_sleep)
3256 seq_printf(s, ", can sleep");
3257 seq_printf(s, ":\n");
3258
3259 if (chip->dbg_show)
3260 chip->dbg_show(s, chip);
3261 else
Linus Walleijfdeb8e12016-02-10 10:57:36 +01003262 gpiolib_dbg_show(s, gdev);
Thierry Redingf9c4a312012-04-12 13:26:01 +02003263
David Brownelld2876d02008-02-04 22:28:20 -08003264 return 0;
3265}
3266
Thierry Redingf9c4a312012-04-12 13:26:01 +02003267static const struct seq_operations gpiolib_seq_ops = {
3268 .start = gpiolib_seq_start,
3269 .next = gpiolib_seq_next,
3270 .stop = gpiolib_seq_stop,
3271 .show = gpiolib_seq_show,
3272};
3273
David Brownelld2876d02008-02-04 22:28:20 -08003274static int gpiolib_open(struct inode *inode, struct file *file)
3275{
Thierry Redingf9c4a312012-04-12 13:26:01 +02003276 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08003277}
3278
Alexey Dobriyan828c0952009-10-01 15:43:56 -07003279static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02003280 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08003281 .open = gpiolib_open,
3282 .read = seq_read,
3283 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02003284 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08003285};
3286
3287static int __init gpiolib_debugfs_init(void)
3288{
3289 /* /sys/kernel/debug/gpio */
3290 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
3291 NULL, NULL, &gpiolib_operations);
3292 return 0;
3293}
3294subsys_initcall(gpiolib_debugfs_init);
3295
3296#endif /* DEBUG_FS */