blob: 4b94e31a50afd5cd20b69d6fd028c7ce654c98d0 [file] [log] [blame]
David Brownelld2876d02008-02-04 22:28:20 -08001#include <linux/kernel.h>
2#include <linux/module.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -07003#include <linux/interrupt.h>
David Brownelld2876d02008-02-04 22:28:20 -08004#include <linux/irq.h>
5#include <linux/spinlock.h>
Alexandre Courbot1a989d02013-02-03 01:29:24 +09006#include <linux/list.h>
David Brownelld8f388d2008-07-25 01:46:07 -07007#include <linux/device.h>
8#include <linux/err.h>
9#include <linux/debugfs.h>
10#include <linux/seq_file.h>
11#include <linux/gpio.h>
Anton Vorontsov391c9702010-06-08 07:48:17 -060012#include <linux/of_gpio.h>
Daniel Glöcknerff77c352009-09-22 16:46:38 -070013#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Rafael J. Wysocki7b199812013-11-11 22:41:56 +010015#include <linux/acpi.h>
Alexandre Courbot53e7cac2013-11-16 21:44:52 +090016#include <linux/gpio/driver.h>
Linus Walleij0a6d3152014-07-24 20:08:55 +020017#include <linux/gpio/machine.h>
Jonas Gorskic771c2f2015-10-11 17:34:15 +020018#include <linux/pinctrl/consumer.h>
Linus Walleijff2b1352015-10-20 11:10:38 +020019#include <linux/idr.h>
David Brownelld2876d02008-02-04 22:28:20 -080020
Mika Westerberg664e3e52014-01-08 12:40:54 +020021#include "gpiolib.h"
22
Uwe Kleine-König3f397c212011-05-20 00:40:19 -060023#define CREATE_TRACE_POINTS
24#include <trace/events/gpio.h>
David Brownelld2876d02008-02-04 22:28:20 -080025
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070026/* Implementation infrastructure for GPIO interfaces.
David Brownelld2876d02008-02-04 22:28:20 -080027 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070028 * The GPIO programming interface allows for inlining speed-critical
29 * get/set operations for common cases, so that access to SOC-integrated
30 * GPIOs can sometimes cost only an instruction or two per bit.
David Brownelld2876d02008-02-04 22:28:20 -080031 */
32
33
34/* When debugging, extend minimal trust to callers and platform code.
35 * Also emit diagnostic messages that may help initial bringup, when
36 * board setup or driver bugs are most common.
37 *
38 * Otherwise, minimize overhead in what may be bitbanging codepaths.
39 */
40#ifdef DEBUG
41#define extra_checks 1
42#else
43#define extra_checks 0
44#endif
45
Linus Walleijff2b1352015-10-20 11:10:38 +020046/* Device and char device-related information */
47static DEFINE_IDA(gpio_ida);
48
David Brownelld2876d02008-02-04 22:28:20 -080049/* gpio_lock prevents conflicts during gpio_desc[] table updates.
50 * While any GPIO is requested, its gpio_chip is not removable;
51 * each GPIO's "requested" flag serves as a lock and refcount.
52 */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +090053DEFINE_SPINLOCK(gpio_lock);
David Brownelld2876d02008-02-04 22:28:20 -080054
Alexandre Courbotbae48da2013-10-17 10:21:38 -070055static DEFINE_MUTEX(gpio_lookup_lock);
56static LIST_HEAD(gpio_lookup_list);
Linus Walleijff2b1352015-10-20 11:10:38 +020057LIST_HEAD(gpio_devices);
Johan Hovold6d867502015-05-04 17:23:25 +020058
59static void gpiochip_free_hogs(struct gpio_chip *chip);
60static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
61
62
David Brownelld2876d02008-02-04 22:28:20 -080063static inline void desc_set_label(struct gpio_desc *d, const char *label)
64{
David Brownelld2876d02008-02-04 22:28:20 -080065 d->label = label;
David Brownelld2876d02008-02-04 22:28:20 -080066}
67
Alexandre Courbot372e7222013-02-03 01:29:29 +090068/**
69 * Convert a GPIO number to its descriptor
70 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070071struct gpio_desc *gpio_to_desc(unsigned gpio)
Alexandre Courbot372e7222013-02-03 01:29:29 +090072{
Linus Walleijff2b1352015-10-20 11:10:38 +020073 struct gpio_device *gdev;
Alexandre Courbot14e85c02014-11-19 16:51:27 +090074 unsigned long flags;
75
76 spin_lock_irqsave(&gpio_lock, flags);
77
Linus Walleijff2b1352015-10-20 11:10:38 +020078 list_for_each_entry(gdev, &gpio_devices, list) {
79 if (gdev->chip->base <= gpio &&
80 gdev->chip->base + gdev->chip->ngpio > gpio) {
Alexandre Courbot14e85c02014-11-19 16:51:27 +090081 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +020082 return &gdev->chip->desc[gpio - gdev->chip->base];
Alexandre Courbot14e85c02014-11-19 16:51:27 +090083 }
84 }
85
86 spin_unlock_irqrestore(&gpio_lock, flags);
87
Alexandre Courbot0e9a5ed2014-12-02 23:15:05 +090088 if (!gpio_is_valid(gpio))
89 WARN(1, "invalid GPIO %d\n", gpio);
90
Alexandre Courbot14e85c02014-11-19 16:51:27 +090091 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +090092}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070093EXPORT_SYMBOL_GPL(gpio_to_desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +090094
95/**
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +090096 * Get the GPIO descriptor corresponding to the given hw number for this chip.
Linus Walleijd468bf92013-09-24 11:54:38 +020097 */
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +090098struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
99 u16 hwnum)
Linus Walleijd468bf92013-09-24 11:54:38 +0200100{
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900101 if (hwnum >= chip->ngpio)
Alexandre Courbotb7d0a282013-12-03 12:31:11 +0900102 return ERR_PTR(-EINVAL);
Linus Walleijd468bf92013-09-24 11:54:38 +0200103
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900104 return &chip->desc[hwnum];
Linus Walleijd468bf92013-09-24 11:54:38 +0200105}
Alexandre Courbot372e7222013-02-03 01:29:29 +0900106
107/**
108 * Convert a GPIO descriptor to the integer namespace.
109 * This should disappear in the future but is needed since we still
110 * use GPIO numbers for error messages and sysfs nodes
111 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700112int desc_to_gpio(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900113{
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900114 return desc->chip->base + (desc - &desc->chip->desc[0]);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900115}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700116EXPORT_SYMBOL_GPL(desc_to_gpio);
Alexandre Courbot372e7222013-02-03 01:29:29 +0900117
118
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700119/**
120 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
121 * @desc: descriptor to return the chip of
122 */
123struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +0900124{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +0900125 return desc ? desc->chip : NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900126}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700127EXPORT_SYMBOL_GPL(gpiod_to_chip);
David Brownelld2876d02008-02-04 22:28:20 -0800128
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700129/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
130static int gpiochip_find_base(int ngpio)
131{
Linus Walleijff2b1352015-10-20 11:10:38 +0200132 struct gpio_device *gdev;
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900133 int base = ARCH_NR_GPIOS - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700134
Linus Walleijff2b1352015-10-20 11:10:38 +0200135 list_for_each_entry_reverse(gdev, &gpio_devices, list) {
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900136 /* found a free space? */
Linus Walleijff2b1352015-10-20 11:10:38 +0200137 if (gdev->chip->base + gdev->chip->ngpio <= base)
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900138 break;
139 else
140 /* nope, check the space right before the chip */
Linus Walleijff2b1352015-10-20 11:10:38 +0200141 base = gdev->chip->base - ngpio;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700142 }
143
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900144 if (gpio_is_valid(base)) {
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700145 pr_debug("%s: found new base at %d\n", __func__, base);
Alexandre Courbot83cabe32013-02-03 01:29:28 +0900146 return base;
147 } else {
148 pr_err("%s: cannot find free range\n", __func__);
149 return -ENOSPC;
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700150 }
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700151}
152
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700153/**
154 * gpiod_get_direction - return the current direction of a GPIO
155 * @desc: GPIO to get the direction of
156 *
157 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
158 *
159 * This function may sleep if gpiod_cansleep() is true.
160 */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900161int gpiod_get_direction(struct gpio_desc *desc)
Mathias Nyman80b0a602012-10-24 17:25:27 +0300162{
163 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +0900164 unsigned offset;
Mathias Nyman80b0a602012-10-24 17:25:27 +0300165 int status = -EINVAL;
166
Alexandre Courbot372e7222013-02-03 01:29:29 +0900167 chip = gpiod_to_chip(desc);
168 offset = gpio_chip_hwgpio(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300169
170 if (!chip->get_direction)
171 return status;
172
Alexandre Courbot372e7222013-02-03 01:29:29 +0900173 status = chip->get_direction(chip, offset);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300174 if (status > 0) {
175 /* GPIOF_DIR_IN, or other positive */
176 status = 1;
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900177 clear_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300178 }
179 if (status == 0) {
180 /* GPIOF_DIR_OUT */
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +0900181 set_bit(FLAG_IS_OUT, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300182 }
183 return status;
184}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700185EXPORT_SYMBOL_GPL(gpiod_get_direction);
Mathias Nyman80b0a602012-10-24 17:25:27 +0300186
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900187/*
188 * Add a new chip to the global chips list, keeping the list of chips sorted
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800189 * by range(means [base, base + ngpio - 1]) order.
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900190 *
191 * Return -EBUSY if the new chip overlaps with some other chip's integer
192 * space.
193 */
Linus Walleijff2b1352015-10-20 11:10:38 +0200194static int gpiodev_add_to_list(struct gpio_device *gdev)
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900195{
Linus Walleijff2b1352015-10-20 11:10:38 +0200196 struct gpio_device *iterator;
197 struct gpio_device *previous = NULL;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900198
Linus Walleijff2b1352015-10-20 11:10:38 +0200199 if (!gdev->chip)
200 return -EINVAL;
201
202 if (list_empty(&gpio_devices)) {
203 list_add_tail(&gdev->list, &gpio_devices);
Sudip Mukherjeee28ecca2015-12-27 19:06:50 +0530204 return 0;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900205 }
206
Linus Walleijff2b1352015-10-20 11:10:38 +0200207 list_for_each_entry(iterator, &gpio_devices, list) {
208 /*
209 * The list may contain dangling GPIO devices with no
210 * live chip assigned.
211 */
212 if (!iterator->chip)
213 continue;
214 if (iterator->chip->base >=
215 gdev->chip->base + gdev->chip->ngpio) {
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800216 /*
217 * Iterator is the first GPIO chip so there is no
218 * previous one
219 */
Sudip Mukherjeee28ecca2015-12-27 19:06:50 +0530220 if (!previous) {
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800221 goto found;
222 } else {
223 /*
224 * We found a valid range(means
225 * [base, base + ngpio - 1]) between previous
226 * and iterator chip.
227 */
Linus Walleijff2b1352015-10-20 11:10:38 +0200228 if (previous->chip->base + previous->chip->ngpio
229 <= gdev->chip->base)
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800230 goto found;
231 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900232 }
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800233 previous = iterator;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900234 }
235
Sudip Mukherjeee28ecca2015-12-27 19:06:50 +0530236 /*
237 * We are beyond the last chip in the list and iterator now
238 * points to the head.
239 * Let iterator point to the last chip in the list.
240 */
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900241
Linus Walleijff2b1352015-10-20 11:10:38 +0200242 iterator = list_last_entry(&gpio_devices, struct gpio_device, list);
243 if (iterator->chip->base + iterator->chip->ngpio <= gdev->chip->base) {
244 list_add(&gdev->list, &iterator->list);
Julien Grossholtz96098df2016-01-07 16:46:45 -0500245 return 0;
246 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900247
Linus Walleijff2b1352015-10-20 11:10:38 +0200248 dev_err(&gdev->dev,
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800249 "GPIO integer space overlap, cannot add chip\n");
250 return -EBUSY;
251
252found:
Linus Walleijff2b1352015-10-20 11:10:38 +0200253 list_add_tail(&gdev->list, &iterator->list);
Bamvor Jian Zhangef7c7552015-11-16 13:02:46 +0800254 return 0;
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900255}
256
Linus Walleijf881bab2015-09-23 16:20:43 -0700257/**
258 * Convert a GPIO name to its descriptor
259 */
260static struct gpio_desc *gpio_name_to_desc(const char * const name)
261{
Linus Walleijff2b1352015-10-20 11:10:38 +0200262 struct gpio_device *gdev;
Linus Walleijf881bab2015-09-23 16:20:43 -0700263 unsigned long flags;
264
265 spin_lock_irqsave(&gpio_lock, flags);
266
Linus Walleijff2b1352015-10-20 11:10:38 +0200267 list_for_each_entry(gdev, &gpio_devices, list) {
Linus Walleijf881bab2015-09-23 16:20:43 -0700268 int i;
269
Linus Walleijff2b1352015-10-20 11:10:38 +0200270 for (i = 0; i != gdev->chip->ngpio; ++i) {
271 struct gpio_desc *gpio = &gdev->chip->desc[i];
Linus Walleijf881bab2015-09-23 16:20:43 -0700272
Vladimir Zapolskiyd06165b2015-11-11 14:36:53 +0200273 if (!gpio->name || !name)
Linus Walleijf881bab2015-09-23 16:20:43 -0700274 continue;
275
276 if (!strcmp(gpio->name, name)) {
277 spin_unlock_irqrestore(&gpio_lock, flags);
278 return gpio;
279 }
280 }
281 }
282
283 spin_unlock_irqrestore(&gpio_lock, flags);
284
285 return NULL;
286}
287
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200288/*
289 * Takes the names from gc->names and checks if they are all unique. If they
290 * are, they are assigned to their gpio descriptors.
291 *
Bamvor Jian Zhanged379152015-11-14 16:43:20 +0800292 * Warning if one of the names is already used for a different GPIO.
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200293 */
294static int gpiochip_set_desc_names(struct gpio_chip *gc)
295{
296 int i;
297
298 if (!gc->names)
299 return 0;
300
301 /* First check all names if they are unique */
302 for (i = 0; i != gc->ngpio; ++i) {
303 struct gpio_desc *gpio;
304
305 gpio = gpio_name_to_desc(gc->names[i]);
Linus Walleijf881bab2015-09-23 16:20:43 -0700306 if (gpio)
Linus Walleij34ffd852015-10-20 11:31:54 +0200307 dev_warn(&gc->gpiodev->dev,
308 "Detected name collision for GPIO name '%s'\n",
Linus Walleijf881bab2015-09-23 16:20:43 -0700309 gc->names[i]);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200310 }
311
312 /* Then add all names to the GPIO descriptors */
313 for (i = 0; i != gc->ngpio; ++i)
314 gc->desc[i].name = gc->names[i];
315
316 return 0;
317}
318
Linus Walleijff2b1352015-10-20 11:10:38 +0200319static void gpiodevice_release(struct device *dev)
320{
321 struct gpio_device *gdev = dev_get_drvdata(dev);
322
323 list_del(&gdev->list);
324 ida_simple_remove(&gpio_ida, gdev->id);
325}
326
Anton Vorontsov169b6a72008-04-28 02:14:47 -0700327/**
Linus Walleijb08ea352015-12-03 15:14:13 +0100328 * gpiochip_add_data() - register a gpio_chip
David Brownelld2876d02008-02-04 22:28:20 -0800329 * @chip: the chip to register, with chip->base initialized
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900330 * Context: potentially before irqs will work
David Brownelld2876d02008-02-04 22:28:20 -0800331 *
332 * Returns a negative errno if the chip can't be registered, such as
333 * because the chip->base is invalid or already associated with a
334 * different chip. Otherwise it returns zero as a success code.
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700335 *
Linus Walleijb08ea352015-12-03 15:14:13 +0100336 * When gpiochip_add_data() is called very early during boot, so that GPIOs
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +0800337 * can be freely used, the chip->parent device must be registered before
David Brownelld8f388d2008-07-25 01:46:07 -0700338 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
339 * for GPIOs will fail rudely.
340 *
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700341 * If chip->base is negative, this requests dynamic assignment of
342 * a range of valid GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -0800343 */
Linus Walleijb08ea352015-12-03 15:14:13 +0100344int gpiochip_add_data(struct gpio_chip *chip, void *data)
David Brownelld2876d02008-02-04 22:28:20 -0800345{
346 unsigned long flags;
347 int status = 0;
Linus Walleijff2b1352015-10-20 11:10:38 +0200348 unsigned i;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700349 int base = chip->base;
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900350 struct gpio_desc *descs;
Linus Walleijff2b1352015-10-20 11:10:38 +0200351 struct gpio_device *gdev;
David Brownelld2876d02008-02-04 22:28:20 -0800352
Linus Walleijff2b1352015-10-20 11:10:38 +0200353 /*
354 * First: allocate and populate the internal stat container, and
355 * set up the struct device.
356 */
357 gdev = kmalloc(sizeof(*gdev), GFP_KERNEL);
358 if (!gdev)
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900359 return -ENOMEM;
Linus Walleijff2b1352015-10-20 11:10:38 +0200360 gdev->chip = chip;
361 chip->gpiodev = gdev;
362 if (chip->parent) {
363 gdev->dev.parent = chip->parent;
364 gdev->dev.of_node = chip->parent->of_node;
365 } else {
366#ifdef CONFIG_OF_GPIO
367 /* If the gpiochip has an assigned OF node this takes precedence */
368 if (chip->of_node)
369 gdev->dev.of_node = chip->of_node;
370#endif
371 }
372 gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
373 if (gdev->id < 0) {
374 status = gdev->id;
375 goto err_free_gdev;
376 }
377 dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
378 device_initialize(&gdev->dev);
379 dev_set_drvdata(&gdev->dev, gdev);
380 if (chip->parent && chip->parent->driver)
381 gdev->owner = chip->parent->driver->owner;
382 else if (chip->owner)
383 /* TODO: remove chip->owner */
384 gdev->owner = chip->owner;
385 else
386 gdev->owner = THIS_MODULE;
David Brownelld2876d02008-02-04 22:28:20 -0800387
Linus Walleijff2b1352015-10-20 11:10:38 +0200388 /* FIXME: devm_kcalloc() these and move to gpio_device */
389 descs = kcalloc(chip->ngpio, sizeof(descs[0]), GFP_KERNEL);
390 if (!descs) {
391 status = -ENOMEM;
392 goto err_free_gdev;
393 }
394
395 /* FIXME: move driver data into gpio_device dev_set_drvdata() */
Linus Walleijb08ea352015-12-03 15:14:13 +0100396 chip->data = data;
397
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +0800398 if (chip->ngpio == 0) {
399 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
Linus Walleijff2b1352015-10-20 11:10:38 +0200400 status = -EINVAL;
401 goto err_free_descs;
Bamvor Jian Zhang5ed41cc2015-11-16 13:02:47 +0800402 }
403
David Brownelld2876d02008-02-04 22:28:20 -0800404 spin_lock_irqsave(&gpio_lock, flags);
405
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700406 if (base < 0) {
407 base = gpiochip_find_base(chip->ngpio);
408 if (base < 0) {
409 status = base;
Johan Hovold225fce82015-01-12 17:12:25 +0100410 spin_unlock_irqrestore(&gpio_lock, flags);
411 goto err_free_descs;
Anton Vorontsov8d0aab22008-04-28 02:14:46 -0700412 }
413 chip->base = base;
414 }
415
Linus Walleijff2b1352015-10-20 11:10:38 +0200416 status = gpiodev_add_to_list(gdev);
Johan Hovold05aa5202015-01-12 17:12:26 +0100417 if (status) {
418 spin_unlock_irqrestore(&gpio_lock, flags);
419 goto err_free_descs;
420 }
Alexandre Courbot1a989d02013-02-03 01:29:24 +0900421
Linus Walleijff2b1352015-10-20 11:10:38 +0200422 for (i = 0; i < chip->ngpio; i++) {
423 struct gpio_desc *desc = &descs[i];
David Brownelld8f388d2008-07-25 01:46:07 -0700424
Linus Walleijff2b1352015-10-20 11:10:38 +0200425 /* REVISIT: maybe a pointer to gpio_device is better */
Johan Hovold05aa5202015-01-12 17:12:26 +0100426 desc->chip = chip;
427
428 /* REVISIT: most hardware initializes GPIOs as inputs (often
429 * with pullups enabled) so power usage is minimized. Linux
430 * code should set the gpio direction first thing; but until
431 * it does, and in case chip->get_direction is not set, we may
432 * expose the wrong direction in sysfs.
433 */
434 desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0;
David Brownelld2876d02008-02-04 22:28:20 -0800435 }
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900436 chip->desc = descs;
437
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800438 spin_unlock_irqrestore(&gpio_lock, flags);
439
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530440#ifdef CONFIG_PINCTRL
Linus Walleijff2b1352015-10-20 11:10:38 +0200441 /* FIXME: move pin ranges to gpio_device */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530442 INIT_LIST_HEAD(&chip->pin_ranges);
443#endif
444
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200445 status = gpiochip_set_desc_names(chip);
446 if (status)
447 goto err_remove_from_list;
448
Tomeu Vizoso28355f82015-07-14 10:29:54 +0200449 status = of_gpiochip_add(chip);
450 if (status)
451 goto err_remove_chip;
452
Mika Westerberg664e3e52014-01-08 12:40:54 +0200453 acpi_gpiochip_add(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600454
Linus Walleijff2b1352015-10-20 11:10:38 +0200455 status = device_add(&gdev->dev);
Johan Hovold225fce82015-01-12 17:12:25 +0100456 if (status)
457 goto err_remove_chip;
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600458
Linus Walleijff2b1352015-10-20 11:10:38 +0200459 status = gpiochip_sysfs_register(chip);
460 if (status)
461 goto err_remove_device;
462
463 /* From this point, the .release() function cleans up gpio_device */
464 gdev->dev.release = gpiodevice_release;
465 get_device(&gdev->dev);
Andy Shevchenko7589e592013-12-05 11:26:23 +0200466 pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__,
Grant Likely64842aa2011-11-06 11:36:18 -0700467 chip->base, chip->base + chip->ngpio - 1,
468 chip->label ? : "generic");
469
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600470 return 0;
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800471
Linus Walleijff2b1352015-10-20 11:10:38 +0200472err_remove_device:
473 device_del(&gdev->dev);
Johan Hovold225fce82015-01-12 17:12:25 +0100474err_remove_chip:
475 acpi_gpiochip_remove(chip);
Johan Hovold6d867502015-05-04 17:23:25 +0200476 gpiochip_free_hogs(chip);
Johan Hovold225fce82015-01-12 17:12:25 +0100477 of_gpiochip_remove(chip);
Markus Pargmann5f3ca732015-08-14 16:11:00 +0200478err_remove_from_list:
Johan Hovold225fce82015-01-12 17:12:25 +0100479 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +0200480 list_del(&gdev->list);
Zhangfei Gao3bae4812013-06-09 11:08:32 +0800481 spin_unlock_irqrestore(&gpio_lock, flags);
Johan Hovold05aa5202015-01-12 17:12:26 +0100482 chip->desc = NULL;
Johan Hovold225fce82015-01-12 17:12:25 +0100483err_free_descs:
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900484 kfree(descs);
Linus Walleijff2b1352015-10-20 11:10:38 +0200485err_free_gdev:
486 ida_simple_remove(&gpio_ida, gdev->id);
487 kfree(gdev);
David Brownelld2876d02008-02-04 22:28:20 -0800488 /* failures here can mean systems won't boot... */
Andy Shevchenko7589e592013-12-05 11:26:23 +0200489 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
Anton Vorontsovcedb1882010-06-08 07:48:15 -0600490 chip->base, chip->base + chip->ngpio - 1,
491 chip->label ? : "generic");
David Brownelld2876d02008-02-04 22:28:20 -0800492 return status;
493}
Linus Walleijb08ea352015-12-03 15:14:13 +0100494EXPORT_SYMBOL_GPL(gpiochip_add_data);
David Brownelld2876d02008-02-04 22:28:20 -0800495
496/**
497 * gpiochip_remove() - unregister a gpio_chip
498 * @chip: the chip to unregister
499 *
500 * A gpio_chip with any GPIOs still requested may not be removed.
501 */
abdoulaye berthee1db1702014-07-05 18:28:50 +0200502void gpiochip_remove(struct gpio_chip *chip)
David Brownelld2876d02008-02-04 22:28:20 -0800503{
Linus Walleijff2b1352015-10-20 11:10:38 +0200504 struct gpio_device *gdev = chip->gpiodev;
Johan Hovoldfab28b82015-05-04 17:10:27 +0200505 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -0800506 unsigned long flags;
David Brownelld2876d02008-02-04 22:28:20 -0800507 unsigned id;
Johan Hovoldfab28b82015-05-04 17:10:27 +0200508 bool requested = false;
David Brownelld2876d02008-02-04 22:28:20 -0800509
Linus Walleijff2b1352015-10-20 11:10:38 +0200510 /* Numb the device, cancelling all outstanding operations */
511 gdev->chip = NULL;
512
513 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
Johan Hovold426577b2015-05-04 17:10:32 +0200514 gpiochip_sysfs_unregister(chip);
Johan Hovold00acc3d2015-01-12 17:12:27 +0100515 gpiochip_irqchip_remove(chip);
Mika Westerberg6072b9d2014-03-10 14:54:53 +0200516 acpi_gpiochip_remove(chip);
Linus Walleij9ef0d6f2012-11-06 15:15:44 +0100517 gpiochip_remove_pin_ranges(chip);
Benoit Parrotf625d462015-02-02 11:44:44 -0600518 gpiochip_free_hogs(chip);
Anton Vorontsov391c9702010-06-08 07:48:17 -0600519 of_gpiochip_remove(chip);
520
Johan Hovold6798aca2015-01-12 17:12:28 +0100521 spin_lock_irqsave(&gpio_lock, flags);
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +0900522 for (id = 0; id < chip->ngpio; id++) {
Johan Hovoldfab28b82015-05-04 17:10:27 +0200523 desc = &chip->desc[id];
524 desc->chip = NULL;
525 if (test_bit(FLAG_REQUESTED, &desc->flags))
526 requested = true;
David Brownelld2876d02008-02-04 22:28:20 -0800527 }
David Brownelld2876d02008-02-04 22:28:20 -0800528 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900529
Johan Hovoldfab28b82015-05-04 17:10:27 +0200530 if (requested)
Linus Walleij34ffd852015-10-20 11:31:54 +0200531 dev_crit(&chip->gpiodev->dev,
Linus Walleij58383c72015-11-04 09:56:26 +0100532 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
Johan Hovoldfab28b82015-05-04 17:10:27 +0200533
Linus Walleijff2b1352015-10-20 11:10:38 +0200534 /* FIXME: need to be moved to gpio_device and held there */
Alexandre Courbot14e85c02014-11-19 16:51:27 +0900535 kfree(chip->desc);
536 chip->desc = NULL;
Linus Walleijff2b1352015-10-20 11:10:38 +0200537
538 /*
539 * The gpiochip side puts its use of the device to rest here:
540 * if there are no userspace clients, the chardev and device will
541 * be removed, else it will be dangling until the last user is
542 * gone.
543 */
544 put_device(&gdev->dev);
David Brownelld2876d02008-02-04 22:28:20 -0800545}
546EXPORT_SYMBOL_GPL(gpiochip_remove);
547
Grant Likely594fa262010-06-08 07:48:16 -0600548/**
549 * gpiochip_find() - iterator for locating a specific gpio_chip
550 * @data: data to pass to match function
551 * @callback: Callback function to check gpio_chip
552 *
553 * Similar to bus_find_device. It returns a reference to a gpio_chip as
554 * determined by a user supplied @match callback. The callback should return
555 * 0 if the device doesn't match and non-zero if it does. If the callback is
556 * non-zero, this function will return to the caller and not iterate over any
557 * more gpio_chips.
558 */
Grant Likely07ce8ec2012-05-18 23:01:05 -0600559struct gpio_chip *gpiochip_find(void *data,
Grant Likely6e2cf652012-03-02 15:56:03 -0700560 int (*match)(struct gpio_chip *chip,
Grant Likely3d0f7cf2012-05-17 13:54:40 -0600561 void *data))
Grant Likely594fa262010-06-08 07:48:16 -0600562{
Linus Walleijff2b1352015-10-20 11:10:38 +0200563 struct gpio_device *gdev;
Alexandre Courbot125eef92013-02-03 01:29:26 +0900564 struct gpio_chip *chip;
Grant Likely594fa262010-06-08 07:48:16 -0600565 unsigned long flags;
Grant Likely594fa262010-06-08 07:48:16 -0600566
567 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +0200568 list_for_each_entry(gdev, &gpio_devices, list)
569 if (match(gdev->chip, data))
Grant Likely594fa262010-06-08 07:48:16 -0600570 break;
Alexandre Courbot125eef92013-02-03 01:29:26 +0900571
572 /* No match? */
Linus Walleijff2b1352015-10-20 11:10:38 +0200573 if (&gdev->list == &gpio_devices)
Alexandre Courbot125eef92013-02-03 01:29:26 +0900574 chip = NULL;
Linus Walleijff2b1352015-10-20 11:10:38 +0200575 else
576 chip = gdev->chip;
577
Grant Likely594fa262010-06-08 07:48:16 -0600578 spin_unlock_irqrestore(&gpio_lock, flags);
579
580 return chip;
581}
Jean Delvare8fa0c9b2011-05-20 00:40:18 -0600582EXPORT_SYMBOL_GPL(gpiochip_find);
David Brownelld2876d02008-02-04 22:28:20 -0800583
Alexandre Courbot79697ef2013-11-16 21:39:32 +0900584static int gpiochip_match_name(struct gpio_chip *chip, void *data)
585{
586 const char *name = data;
587
588 return !strcmp(chip->label, name);
589}
590
591static struct gpio_chip *find_chip_by_name(const char *name)
592{
593 return gpiochip_find((void *)name, gpiochip_match_name);
594}
595
Linus Walleij14250522014-03-25 10:40:18 +0100596#ifdef CONFIG_GPIOLIB_IRQCHIP
597
598/*
599 * The following is irqchip helper code for gpiochips.
600 */
601
602/**
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200603 * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip
604 * @gpiochip: the gpiochip to set the irqchip chain to
605 * @irqchip: the irqchip to chain to the gpiochip
Linus Walleij14250522014-03-25 10:40:18 +0100606 * @parent_irq: the irq number corresponding to the parent IRQ for this
607 * chained irqchip
608 * @parent_handler: the parent interrupt handler for the accumulated IRQ
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200609 * coming out of the gpiochip. If the interrupt is nested rather than
610 * cascaded, pass NULL in this handler argument
Linus Walleij14250522014-03-25 10:40:18 +0100611 */
612void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
613 struct irq_chip *irqchip,
614 int parent_irq,
615 irq_flow_handler_t parent_handler)
616{
Linus Walleij83141a72014-09-26 13:50:12 +0200617 unsigned int offset;
618
Linus Walleij83141a72014-09-26 13:50:12 +0200619 if (!gpiochip->irqdomain) {
620 chip_err(gpiochip, "called %s before setting up irqchip\n",
621 __func__);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200622 return;
623 }
624
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200625 if (parent_handler) {
626 if (gpiochip->can_sleep) {
627 chip_err(gpiochip,
628 "you cannot have chained interrupts on a "
629 "chip that may sleep\n");
630 return;
631 }
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200632 /*
633 * The parent irqchip is already using the chip_data for this
634 * irqchip, so our callbacks simply use the handler_data.
635 */
Thomas Gleixnerf7f87752015-06-21 21:10:48 +0200636 irq_set_chained_handler_and_data(parent_irq, parent_handler,
637 gpiochip);
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +0300638
639 gpiochip->irq_parent = parent_irq;
Linus Walleij3f97d5fc2014-09-26 14:19:52 +0200640 }
Linus Walleij83141a72014-09-26 13:50:12 +0200641
642 /* Set the parent IRQ for all affected IRQs */
643 for (offset = 0; offset < gpiochip->ngpio; offset++)
644 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset),
645 parent_irq);
Linus Walleij14250522014-03-25 10:40:18 +0100646}
647EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
648
649/**
650 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
651 * @d: the irqdomain used by this irqchip
652 * @irq: the global irq number used by this GPIO irqchip irq
653 * @hwirq: the local IRQ/GPIO line offset on this gpiochip
654 *
655 * This function will set up the mapping for a certain IRQ line on a
656 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
657 * stored inside the gpiochip.
658 */
659static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
660 irq_hw_number_t hwirq)
661{
662 struct gpio_chip *chip = d->host_data;
663
Linus Walleij14250522014-03-25 10:40:18 +0100664 irq_set_chip_data(irq, chip);
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300665 /*
666 * This lock class tells lockdep that GPIO irqs are in a different
667 * category than their parents, so it won't report false recursion.
668 */
669 irq_set_lockdep_class(irq, chip->lock_key);
Linus Walleij7633fb92014-04-09 13:20:38 +0200670 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler);
Linus Walleij1c8732b2014-04-09 13:34:39 +0200671 /* Chips that can sleep need nested thread handlers */
Octavian Purdila295494a2014-09-19 23:22:44 +0300672 if (chip->can_sleep && !chip->irq_not_threaded)
Linus Walleij1c8732b2014-04-09 13:34:39 +0200673 irq_set_nested_thread(irq, 1);
Linus Walleij14250522014-03-25 10:40:18 +0100674 irq_set_noprobe(irq);
Rob Herring23393d42015-07-27 15:55:16 -0500675
Linus Walleij1333b902014-04-23 16:45:12 +0200676 /*
677 * No set-up of the hardware will happen if IRQ_TYPE_NONE
678 * is passed as default type.
679 */
680 if (chip->irq_default_type != IRQ_TYPE_NONE)
681 irq_set_irq_type(irq, chip->irq_default_type);
Linus Walleij14250522014-03-25 10:40:18 +0100682
683 return 0;
684}
685
Linus Walleijc3626fd2014-03-28 20:42:01 +0100686static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
687{
Linus Walleij1c8732b2014-04-09 13:34:39 +0200688 struct gpio_chip *chip = d->host_data;
689
Linus Walleij1c8732b2014-04-09 13:34:39 +0200690 if (chip->can_sleep)
691 irq_set_nested_thread(irq, 0);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100692 irq_set_chip_and_handler(irq, NULL, NULL);
693 irq_set_chip_data(irq, NULL);
694}
695
Linus Walleij14250522014-03-25 10:40:18 +0100696static const struct irq_domain_ops gpiochip_domain_ops = {
697 .map = gpiochip_irq_map,
Linus Walleijc3626fd2014-03-28 20:42:01 +0100698 .unmap = gpiochip_irq_unmap,
Linus Walleij14250522014-03-25 10:40:18 +0100699 /* Virtually all GPIO irqchips are twocell:ed */
700 .xlate = irq_domain_xlate_twocell,
701};
702
703static int gpiochip_irq_reqres(struct irq_data *d)
704{
705 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
706
Linus Walleijff2b1352015-10-20 11:10:38 +0200707 if (!try_module_get(chip->gpiodev->owner))
Grygorii Strashko5b76e792015-06-25 20:30:50 +0300708 return -ENODEV;
709
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900710 if (gpiochip_lock_as_irq(chip, d->hwirq)) {
Linus Walleij14250522014-03-25 10:40:18 +0100711 chip_err(chip,
712 "unable to lock HW IRQ %lu for IRQ\n",
713 d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +0200714 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +0100715 return -EINVAL;
716 }
717 return 0;
718}
719
720static void gpiochip_irq_relres(struct irq_data *d)
721{
722 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
723
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900724 gpiochip_unlock_as_irq(chip, d->hwirq);
Linus Walleijff2b1352015-10-20 11:10:38 +0200725 module_put(chip->gpiodev->owner);
Linus Walleij14250522014-03-25 10:40:18 +0100726}
727
728static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
729{
730 return irq_find_mapping(chip->irqdomain, offset);
731}
732
733/**
734 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
735 * @gpiochip: the gpiochip to remove the irqchip from
736 *
737 * This is called only from gpiochip_remove()
738 */
739static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
740{
Linus Walleijc3626fd2014-03-28 20:42:01 +0100741 unsigned int offset;
742
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300743 acpi_gpiochip_free_interrupts(gpiochip);
744
Dmitry Eremin-Solenikov25e4fe92015-05-12 20:12:23 +0300745 if (gpiochip->irq_parent) {
746 irq_set_chained_handler(gpiochip->irq_parent, NULL);
747 irq_set_handler_data(gpiochip->irq_parent, NULL);
748 }
749
Linus Walleijc3626fd2014-03-28 20:42:01 +0100750 /* Remove all IRQ mappings and delete the domain */
751 if (gpiochip->irqdomain) {
752 for (offset = 0; offset < gpiochip->ngpio; offset++)
Grygorii Strashkoe3893382014-09-25 19:09:23 +0300753 irq_dispose_mapping(
754 irq_find_mapping(gpiochip->irqdomain, offset));
Linus Walleij14250522014-03-25 10:40:18 +0100755 irq_domain_remove(gpiochip->irqdomain);
Linus Walleijc3626fd2014-03-28 20:42:01 +0100756 }
Linus Walleij14250522014-03-25 10:40:18 +0100757
758 if (gpiochip->irqchip) {
759 gpiochip->irqchip->irq_request_resources = NULL;
760 gpiochip->irqchip->irq_release_resources = NULL;
761 gpiochip->irqchip = NULL;
762 }
763}
764
765/**
766 * gpiochip_irqchip_add() - adds an irqchip to a gpiochip
767 * @gpiochip: the gpiochip to add the irqchip to
768 * @irqchip: the irqchip to add to the gpiochip
769 * @first_irq: if not dynamically assigned, the base (first) IRQ to
770 * allocate gpiochip irqs from
771 * @handler: the irq handler to use (often a predefined irq core function)
Linus Walleij1333b902014-04-23 16:45:12 +0200772 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
773 * to have the core avoid setting up any default type in the hardware.
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300774 * @lock_key: lockdep class
Linus Walleij14250522014-03-25 10:40:18 +0100775 *
776 * This function closely associates a certain irqchip with a certain
777 * gpiochip, providing an irq domain to translate the local IRQs to
778 * global irqs in the gpiolib core, and making sure that the gpiochip
779 * is passed as chip data to all related functions. Driver callbacks
Linus Walleij09dd5f92015-12-07 15:31:58 +0100780 * need to use gpiochip_get_data() to get their local state containers back
Linus Walleij14250522014-03-25 10:40:18 +0100781 * from the gpiochip passed as chip data. An irqdomain will be stored
782 * in the gpiochip that shall be used by the driver to handle IRQ number
783 * translation. The gpiochip will need to be initialized and registered
784 * before calling this function.
785 *
Linus Walleijc3626fd2014-03-28 20:42:01 +0100786 * This function will handle two cell:ed simple IRQs and assumes all
787 * the pins on the gpiochip can generate a unique IRQ. Everything else
Linus Walleij14250522014-03-25 10:40:18 +0100788 * need to be open coded.
789 */
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300790int _gpiochip_irqchip_add(struct gpio_chip *gpiochip,
791 struct irq_chip *irqchip,
792 unsigned int first_irq,
793 irq_flow_handler_t handler,
794 unsigned int type,
795 struct lock_class_key *lock_key)
Linus Walleij14250522014-03-25 10:40:18 +0100796{
797 struct device_node *of_node;
798 unsigned int offset;
Linus Walleijc3626fd2014-03-28 20:42:01 +0100799 unsigned irq_base = 0;
Linus Walleij14250522014-03-25 10:40:18 +0100800
801 if (!gpiochip || !irqchip)
802 return -EINVAL;
803
Linus Walleij58383c72015-11-04 09:56:26 +0100804 if (!gpiochip->parent) {
Linus Walleij14250522014-03-25 10:40:18 +0100805 pr_err("missing gpiochip .dev parent pointer\n");
806 return -EINVAL;
807 }
Linus Walleij58383c72015-11-04 09:56:26 +0100808 of_node = gpiochip->parent->of_node;
Linus Walleij14250522014-03-25 10:40:18 +0100809#ifdef CONFIG_OF_GPIO
810 /*
Colin Cronin20a8a962015-05-18 11:41:43 -0700811 * If the gpiochip has an assigned OF node this takes precedence
Bamvor Jian Zhangc88402c2015-11-18 17:07:07 +0800812 * FIXME: get rid of this and use gpiochip->parent->of_node
813 * everywhere
Linus Walleij14250522014-03-25 10:40:18 +0100814 */
815 if (gpiochip->of_node)
816 of_node = gpiochip->of_node;
817#endif
818 gpiochip->irqchip = irqchip;
819 gpiochip->irq_handler = handler;
820 gpiochip->irq_default_type = type;
821 gpiochip->to_irq = gpiochip_to_irq;
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300822 gpiochip->lock_key = lock_key;
Linus Walleij14250522014-03-25 10:40:18 +0100823 gpiochip->irqdomain = irq_domain_add_simple(of_node,
824 gpiochip->ngpio, first_irq,
825 &gpiochip_domain_ops, gpiochip);
826 if (!gpiochip->irqdomain) {
827 gpiochip->irqchip = NULL;
828 return -EINVAL;
829 }
Rabin Vincent8b67a1f2015-07-31 14:48:56 +0200830
831 /*
832 * It is possible for a driver to override this, but only if the
833 * alternative functions are both implemented.
834 */
835 if (!irqchip->irq_request_resources &&
836 !irqchip->irq_release_resources) {
837 irqchip->irq_request_resources = gpiochip_irq_reqres;
838 irqchip->irq_release_resources = gpiochip_irq_relres;
839 }
Linus Walleij14250522014-03-25 10:40:18 +0100840
841 /*
842 * Prepare the mapping since the irqchip shall be orthogonal to
843 * any gpiochip calls. If the first_irq was zero, this is
844 * necessary to allocate descriptors for all IRQs.
845 */
Linus Walleijc3626fd2014-03-28 20:42:01 +0100846 for (offset = 0; offset < gpiochip->ngpio; offset++) {
847 irq_base = irq_create_mapping(gpiochip->irqdomain, offset);
848 if (offset == 0)
849 /*
850 * Store the base into the gpiochip to be used when
851 * unmapping the irqs.
852 */
853 gpiochip->irq_base = irq_base;
854 }
Linus Walleij14250522014-03-25 10:40:18 +0100855
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300856 acpi_gpiochip_request_interrupts(gpiochip);
857
Linus Walleij14250522014-03-25 10:40:18 +0100858 return 0;
859}
Grygorii Strashkoa0a8bcf2015-08-17 15:35:23 +0300860EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add);
Linus Walleij14250522014-03-25 10:40:18 +0100861
862#else /* CONFIG_GPIOLIB_IRQCHIP */
863
864static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
865
866#endif /* CONFIG_GPIOLIB_IRQCHIP */
867
Jonas Gorskic771c2f2015-10-11 17:34:15 +0200868/**
869 * gpiochip_generic_request() - request the gpio function for a pin
870 * @chip: the gpiochip owning the GPIO
871 * @offset: the offset of the GPIO to request for GPIO function
872 */
873int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
874{
875 return pinctrl_request_gpio(chip->base + offset);
876}
877EXPORT_SYMBOL_GPL(gpiochip_generic_request);
878
879/**
880 * gpiochip_generic_free() - free the gpio function from a pin
881 * @chip: the gpiochip to request the gpio function for
882 * @offset: the offset of the GPIO to free from GPIO function
883 */
884void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
885{
886 pinctrl_free_gpio(chip->base + offset);
887}
888EXPORT_SYMBOL_GPL(gpiochip_generic_free);
889
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530890#ifdef CONFIG_PINCTRL
Linus Walleij165adc92012-11-06 14:49:39 +0100891
Linus Walleij3f0f8672012-11-20 12:40:15 +0100892/**
Christian Ruppert586a87e2013-10-15 15:37:54 +0200893 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
894 * @chip: the gpiochip to add the range for
Tomeu Vizosod32651f2015-06-17 15:42:11 +0200895 * @pctldev: the pin controller to map to
Christian Ruppert586a87e2013-10-15 15:37:54 +0200896 * @gpio_offset: the start offset in the current gpio_chip number space
897 * @pin_group: name of the pin group inside the pin controller
898 */
899int gpiochip_add_pingroup_range(struct gpio_chip *chip,
900 struct pinctrl_dev *pctldev,
901 unsigned int gpio_offset, const char *pin_group)
902{
903 struct gpio_pin_range *pin_range;
904 int ret;
905
906 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
907 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200908 chip_err(chip, "failed to allocate pin ranges\n");
Christian Ruppert586a87e2013-10-15 15:37:54 +0200909 return -ENOMEM;
910 }
911
912 /* Use local offset as range ID */
913 pin_range->range.id = gpio_offset;
914 pin_range->range.gc = chip;
915 pin_range->range.name = chip->label;
916 pin_range->range.base = chip->base + gpio_offset;
917 pin_range->pctldev = pctldev;
918
919 ret = pinctrl_get_group_pins(pctldev, pin_group,
920 &pin_range->range.pins,
921 &pin_range->range.npins);
Michal Nazarewicz61c63752013-11-13 21:20:39 +0100922 if (ret < 0) {
923 kfree(pin_range);
Christian Ruppert586a87e2013-10-15 15:37:54 +0200924 return ret;
Michal Nazarewicz61c63752013-11-13 21:20:39 +0100925 }
Christian Ruppert586a87e2013-10-15 15:37:54 +0200926
927 pinctrl_add_gpio_range(pctldev, &pin_range->range);
928
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200929 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
930 gpio_offset, gpio_offset + pin_range->range.npins - 1,
Christian Ruppert586a87e2013-10-15 15:37:54 +0200931 pinctrl_dev_get_devname(pctldev), pin_group);
932
933 list_add_tail(&pin_range->node, &chip->pin_ranges);
934
935 return 0;
936}
937EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
938
939/**
Linus Walleij3f0f8672012-11-20 12:40:15 +0100940 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
941 * @chip: the gpiochip to add the range for
942 * @pinctrl_name: the dev_name() of the pin controller to map to
Linus Walleij316511c2012-11-21 08:48:09 +0100943 * @gpio_offset: the start offset in the current gpio_chip number space
944 * @pin_offset: the start offset in the pin controller number space
Linus Walleij3f0f8672012-11-20 12:40:15 +0100945 * @npins: the number of pins from the offset of each pin space (GPIO and
946 * pin controller) to accumulate in this range
947 */
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100948int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
Linus Walleij316511c2012-11-21 08:48:09 +0100949 unsigned int gpio_offset, unsigned int pin_offset,
Linus Walleij3f0f8672012-11-20 12:40:15 +0100950 unsigned int npins)
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530951{
952 struct gpio_pin_range *pin_range;
Axel Linb4d4b1f2012-11-21 14:33:56 +0800953 int ret;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530954
Linus Walleij3f0f8672012-11-20 12:40:15 +0100955 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530956 if (!pin_range) {
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200957 chip_err(chip, "failed to allocate pin ranges\n");
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100958 return -ENOMEM;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530959 }
960
Linus Walleij3f0f8672012-11-20 12:40:15 +0100961 /* Use local offset as range ID */
Linus Walleij316511c2012-11-21 08:48:09 +0100962 pin_range->range.id = gpio_offset;
Linus Walleij3f0f8672012-11-20 12:40:15 +0100963 pin_range->range.gc = chip;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530964 pin_range->range.name = chip->label;
Linus Walleij316511c2012-11-21 08:48:09 +0100965 pin_range->range.base = chip->base + gpio_offset;
966 pin_range->range.pin_base = pin_offset;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530967 pin_range->range.npins = npins;
Linus Walleij192c3692012-11-20 14:03:37 +0100968 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530969 &pin_range->range);
Linus Walleij8f23ca12012-11-20 14:56:25 +0100970 if (IS_ERR(pin_range->pctldev)) {
Axel Linb4d4b1f2012-11-21 14:33:56 +0800971 ret = PTR_ERR(pin_range->pctldev);
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200972 chip_err(chip, "could not create pin range\n");
Linus Walleij3f0f8672012-11-20 12:40:15 +0100973 kfree(pin_range);
Axel Linb4d4b1f2012-11-21 14:33:56 +0800974 return ret;
Linus Walleij3f0f8672012-11-20 12:40:15 +0100975 }
Andy Shevchenko1a2a99c2013-12-05 11:26:24 +0200976 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
977 gpio_offset, gpio_offset + npins - 1,
Linus Walleij316511c2012-11-21 08:48:09 +0100978 pinctl_name,
979 pin_offset, pin_offset + npins - 1);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530980
981 list_add_tail(&pin_range->node, &chip->pin_ranges);
Linus Walleij1e63d7b2012-11-06 16:03:35 +0100982
983 return 0;
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530984}
Linus Walleij165adc92012-11-06 14:49:39 +0100985EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530986
Linus Walleij3f0f8672012-11-20 12:40:15 +0100987/**
988 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
989 * @chip: the chip to remove all the mappings for
990 */
Shiraz Hashimf23f1512012-10-27 15:21:36 +0530991void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
992{
993 struct gpio_pin_range *pin_range, *tmp;
994
995 list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) {
996 list_del(&pin_range->node);
997 pinctrl_remove_gpio_range(pin_range->pctldev,
998 &pin_range->range);
Linus Walleij3f0f8672012-11-20 12:40:15 +0100999 kfree(pin_range);
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301000 }
1001}
Linus Walleij165adc92012-11-06 14:49:39 +01001002EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1003
1004#endif /* CONFIG_PINCTRL */
Shiraz Hashimf23f1512012-10-27 15:21:36 +05301005
David Brownelld2876d02008-02-04 22:28:20 -08001006/* These "optional" allocation calls help prevent drivers from stomping
1007 * on each other, and help provide better diagnostics in debugfs.
1008 * They're called even less than the "set direction" calls.
1009 */
Mika Westerberg77c2d792014-03-10 14:54:50 +02001010static int __gpiod_request(struct gpio_desc *desc, const char *label)
David Brownelld2876d02008-02-04 22:28:20 -08001011{
Mika Westerberg77c2d792014-03-10 14:54:50 +02001012 struct gpio_chip *chip = desc->chip;
1013 int status;
David Brownelld2876d02008-02-04 22:28:20 -08001014 unsigned long flags;
1015
1016 spin_lock_irqsave(&gpio_lock, flags);
1017
David Brownelld2876d02008-02-04 22:28:20 -08001018 /* NOTE: gpio_request() can be called in early boot,
David Brownell35e8bb52008-10-15 22:03:16 -07001019 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
David Brownelld2876d02008-02-04 22:28:20 -08001020 */
1021
1022 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
1023 desc_set_label(desc, label ? : "?");
1024 status = 0;
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001025 } else {
David Brownelld2876d02008-02-04 22:28:20 -08001026 status = -EBUSY;
Magnus Damm7460db52009-01-29 14:25:12 -08001027 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001028 }
1029
1030 if (chip->request) {
1031 /* chip->request may sleep */
1032 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001033 status = chip->request(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001034 spin_lock_irqsave(&gpio_lock, flags);
1035
1036 if (status < 0) {
1037 desc_set_label(desc, NULL);
David Brownell35e8bb52008-10-15 22:03:16 -07001038 clear_bit(FLAG_REQUESTED, &desc->flags);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001039 goto done;
David Brownell35e8bb52008-10-15 22:03:16 -07001040 }
Guennadi Liakhovetski438d8902008-04-28 02:14:44 -07001041 }
Mathias Nyman80b0a602012-10-24 17:25:27 +03001042 if (chip->get_direction) {
1043 /* chip->get_direction may sleep */
1044 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001045 gpiod_get_direction(desc);
Mathias Nyman80b0a602012-10-24 17:25:27 +03001046 spin_lock_irqsave(&gpio_lock, flags);
1047 }
David Brownelld2876d02008-02-04 22:28:20 -08001048done:
Laurent Pinchart923b93e2015-10-13 00:20:20 +03001049 if (status < 0) {
1050 /* Clear flags that might have been set by the caller before
1051 * requesting the GPIO.
1052 */
1053 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
1054 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
1055 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
1056 }
Mika Westerberg77c2d792014-03-10 14:54:50 +02001057 spin_unlock_irqrestore(&gpio_lock, flags);
1058 return status;
1059}
1060
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09001061int gpiod_request(struct gpio_desc *desc, const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001062{
1063 int status = -EPROBE_DEFER;
1064 struct gpio_chip *chip;
1065
1066 if (!desc) {
1067 pr_warn("%s: invalid GPIO\n", __func__);
1068 return -EINVAL;
1069 }
1070
1071 chip = desc->chip;
1072 if (!chip)
1073 goto done;
1074
Linus Walleijff2b1352015-10-20 11:10:38 +02001075 if (try_module_get(chip->gpiodev->owner)) {
Mika Westerberg77c2d792014-03-10 14:54:50 +02001076 status = __gpiod_request(desc, label);
1077 if (status < 0)
Linus Walleijff2b1352015-10-20 11:10:38 +02001078 module_put(chip->gpiodev->owner);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001079 }
1080
1081done:
David Brownelld2876d02008-02-04 22:28:20 -08001082 if (status)
Andy Shevchenko7589e592013-12-05 11:26:23 +02001083 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001084
David Brownelld2876d02008-02-04 22:28:20 -08001085 return status;
1086}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001087
Mika Westerberg77c2d792014-03-10 14:54:50 +02001088static bool __gpiod_free(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001089{
Mika Westerberg77c2d792014-03-10 14:54:50 +02001090 bool ret = false;
David Brownelld2876d02008-02-04 22:28:20 -08001091 unsigned long flags;
David Brownell35e8bb52008-10-15 22:03:16 -07001092 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001093
Uwe Kleine-König3d599d12008-10-15 22:03:12 -07001094 might_sleep();
1095
Alexandre Courbot372e7222013-02-03 01:29:29 +09001096 gpiod_unexport(desc);
David Brownelld8f388d2008-07-25 01:46:07 -07001097
David Brownelld2876d02008-02-04 22:28:20 -08001098 spin_lock_irqsave(&gpio_lock, flags);
1099
David Brownell35e8bb52008-10-15 22:03:16 -07001100 chip = desc->chip;
1101 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1102 if (chip->free) {
1103 spin_unlock_irqrestore(&gpio_lock, flags);
David Brownell9c4ba942010-08-10 18:02:24 -07001104 might_sleep_if(chip->can_sleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001105 chip->free(chip, gpio_chip_hwgpio(desc));
David Brownell35e8bb52008-10-15 22:03:16 -07001106 spin_lock_irqsave(&gpio_lock, flags);
1107 }
David Brownelld2876d02008-02-04 22:28:20 -08001108 desc_set_label(desc, NULL);
Jani Nikula07697462009-12-15 16:46:20 -08001109 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
David Brownell35e8bb52008-10-15 22:03:16 -07001110 clear_bit(FLAG_REQUESTED, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301111 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301112 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
Benoit Parrotf625d462015-02-02 11:44:44 -06001113 clear_bit(FLAG_IS_HOGGED, &desc->flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001114 ret = true;
1115 }
David Brownelld2876d02008-02-04 22:28:20 -08001116
1117 spin_unlock_irqrestore(&gpio_lock, flags);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001118 return ret;
1119}
1120
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +09001121void gpiod_free(struct gpio_desc *desc)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001122{
1123 if (desc && __gpiod_free(desc))
Linus Walleijff2b1352015-10-20 11:10:38 +02001124 module_put(desc->chip->gpiodev->owner);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001125 else
1126 WARN_ON(extra_checks);
David Brownelld2876d02008-02-04 22:28:20 -08001127}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001128
David Brownelld2876d02008-02-04 22:28:20 -08001129/**
1130 * gpiochip_is_requested - return string iff signal was requested
1131 * @chip: controller managing the signal
1132 * @offset: of signal within controller's 0..(ngpio - 1) range
1133 *
1134 * Returns NULL if the GPIO is not currently requested, else a string.
Alexandre Courbot9c8318f2014-07-01 14:45:14 +09001135 * The string returned is the label passed to gpio_request(); if none has been
1136 * passed it is a meaningless, non-NULL constant.
David Brownelld2876d02008-02-04 22:28:20 -08001137 *
1138 * This function is for use by GPIO controller drivers. The label can
1139 * help with diagnostics, and knowing that the signal is used as a GPIO
1140 * can help avoid accidentally multiplexing it to another controller.
1141 */
1142const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1143{
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001144 struct gpio_desc *desc;
David Brownelld2876d02008-02-04 22:28:20 -08001145
Dirk Behme48b59532015-08-18 18:02:32 +02001146 if (offset >= chip->ngpio)
David Brownelld2876d02008-02-04 22:28:20 -08001147 return NULL;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09001148
1149 desc = &chip->desc[offset];
1150
Alexandre Courbot372e7222013-02-03 01:29:29 +09001151 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
David Brownelld2876d02008-02-04 22:28:20 -08001152 return NULL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001153 return desc->label;
David Brownelld2876d02008-02-04 22:28:20 -08001154}
1155EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1156
Mika Westerberg77c2d792014-03-10 14:54:50 +02001157/**
1158 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
1159 * @desc: GPIO descriptor to request
1160 * @label: label for the GPIO
1161 *
1162 * Function allows GPIO chip drivers to request and use their own GPIO
1163 * descriptors via gpiolib API. Difference to gpiod_request() is that this
1164 * function will not increase reference count of the GPIO chip module. This
1165 * allows the GPIO chip module to be unloaded as needed (we assume that the
1166 * GPIO chip driver handles freeing the GPIOs it has requested).
1167 */
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001168struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
1169 const char *label)
Mika Westerberg77c2d792014-03-10 14:54:50 +02001170{
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001171 struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
1172 int err;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001173
Alexandre Courbotabdc08a2014-08-19 10:06:09 -07001174 if (IS_ERR(desc)) {
1175 chip_err(chip, "failed to get GPIO descriptor\n");
1176 return desc;
1177 }
1178
1179 err = __gpiod_request(desc, label);
1180 if (err < 0)
1181 return ERR_PTR(err);
1182
1183 return desc;
Mika Westerberg77c2d792014-03-10 14:54:50 +02001184}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07001185EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
Mika Westerberg77c2d792014-03-10 14:54:50 +02001186
1187/**
1188 * gpiochip_free_own_desc - Free GPIO requested by the chip driver
1189 * @desc: GPIO descriptor to free
1190 *
1191 * Function frees the given GPIO requested previously with
1192 * gpiochip_request_own_desc().
1193 */
1194void gpiochip_free_own_desc(struct gpio_desc *desc)
1195{
1196 if (desc)
1197 __gpiod_free(desc);
1198}
Guenter Roeckf7d4ad92014-07-22 08:01:01 -07001199EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
David Brownelld2876d02008-02-04 22:28:20 -08001200
1201/* Drivers MUST set GPIO direction before making get/set calls. In
1202 * some cases this is done in early boot, before IRQs are enabled.
1203 *
1204 * As a rule these aren't called more than once (except for drivers
1205 * using the open-drain emulation idiom) so these are natural places
1206 * to accumulate extra debugging checks. Note that we can't (yet)
1207 * rely on gpio_request() having been called beforehand.
1208 */
1209
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001210/**
1211 * gpiod_direction_input - set the GPIO direction to input
1212 * @desc: GPIO to set to input
1213 *
1214 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
1215 * be called safely on it.
1216 *
1217 * Return 0 in case of success, else an error code.
1218 */
1219int gpiod_direction_input(struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001220{
David Brownelld2876d02008-02-04 22:28:20 -08001221 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001222 int status = -EINVAL;
1223
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001224 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001225 pr_warn("%s: invalid GPIO\n", __func__);
1226 return -EINVAL;
1227 }
1228
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001229 chip = desc->chip;
1230 if (!chip->get || !chip->direction_input) {
Mark Brown6424de52013-09-09 10:33:49 +01001231 gpiod_warn(desc,
1232 "%s: missing get() or direction_input() operations\n",
Andy Shevchenko7589e592013-12-05 11:26:23 +02001233 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001234 return -EIO;
1235 }
1236
Alexandre Courbotd82da792014-07-22 16:17:43 +09001237 status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
David Brownelld2876d02008-02-04 22:28:20 -08001238 if (status == 0)
1239 clear_bit(FLAG_IS_OUT, &desc->flags);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001240
Alexandre Courbot372e7222013-02-03 01:29:29 +09001241 trace_gpio_direction(desc_to_gpio(desc), 1, status);
Alexandre Courbotd82da792014-07-22 16:17:43 +09001242
David Brownelld2876d02008-02-04 22:28:20 -08001243 return status;
1244}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001245EXPORT_SYMBOL_GPL(gpiod_direction_input);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001246
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001247static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
David Brownelld2876d02008-02-04 22:28:20 -08001248{
David Brownelld2876d02008-02-04 22:28:20 -08001249 struct gpio_chip *chip;
David Brownelld2876d02008-02-04 22:28:20 -08001250 int status = -EINVAL;
1251
Linus Walleijd468bf92013-09-24 11:54:38 +02001252 /* GPIOs used for IRQs shall not be set as output */
1253 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1254 gpiod_err(desc,
1255 "%s: tried to set a GPIO tied to an IRQ as output\n",
1256 __func__);
1257 return -EIO;
1258 }
1259
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301260 /* Open drain pin should not be driven to 1 */
1261 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001262 return gpiod_direction_input(desc);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301263
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301264 /* Open source pin should not be driven to 0 */
1265 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
Alexandre Courbot372e7222013-02-03 01:29:29 +09001266 return gpiod_direction_input(desc);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301267
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001268 chip = desc->chip;
1269 if (!chip->set || !chip->direction_output) {
Mark Brown6424de52013-09-09 10:33:49 +01001270 gpiod_warn(desc,
1271 "%s: missing set() or direction_output() operations\n",
1272 __func__);
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001273 return -EIO;
1274 }
1275
Alexandre Courbotd82da792014-07-22 16:17:43 +09001276 status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value);
David Brownelld2876d02008-02-04 22:28:20 -08001277 if (status == 0)
1278 set_bit(FLAG_IS_OUT, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001279 trace_gpio_value(desc_to_gpio(desc), 0, value);
1280 trace_gpio_direction(desc_to_gpio(desc), 0, status);
David Brownelld2876d02008-02-04 22:28:20 -08001281 return status;
1282}
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001283
1284/**
1285 * gpiod_direction_output_raw - set the GPIO direction to output
1286 * @desc: GPIO to set to output
1287 * @value: initial output value of the GPIO
1288 *
1289 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1290 * be called safely on it. The initial value of the output must be specified
1291 * as raw value on the physical line without regard for the ACTIVE_LOW status.
1292 *
1293 * Return 0 in case of success, else an error code.
1294 */
1295int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1296{
1297 if (!desc || !desc->chip) {
1298 pr_warn("%s: invalid GPIO\n", __func__);
1299 return -EINVAL;
1300 }
1301 return _gpiod_direction_output_raw(desc, value);
1302}
1303EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
1304
1305/**
Rahul Bedarkar90df4fe2014-02-08 11:55:25 +05301306 * gpiod_direction_output - set the GPIO direction to output
Philipp Zabelef70bbe2014-01-07 12:34:11 +01001307 * @desc: GPIO to set to output
1308 * @value: initial output value of the GPIO
1309 *
1310 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1311 * be called safely on it. The initial value of the output must be specified
1312 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1313 * account.
1314 *
1315 * Return 0 in case of success, else an error code.
1316 */
1317int gpiod_direction_output(struct gpio_desc *desc, int value)
1318{
1319 if (!desc || !desc->chip) {
1320 pr_warn("%s: invalid GPIO\n", __func__);
1321 return -EINVAL;
1322 }
1323 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1324 value = !value;
1325 return _gpiod_direction_output_raw(desc, value);
1326}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001327EXPORT_SYMBOL_GPL(gpiod_direction_output);
David Brownelld2876d02008-02-04 22:28:20 -08001328
Felipe Balbic4b5be92010-05-26 14:42:23 -07001329/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001330 * gpiod_set_debounce - sets @debounce time for a @gpio
Felipe Balbic4b5be92010-05-26 14:42:23 -07001331 * @gpio: the gpio to set debounce time
1332 * @debounce: debounce time is microseconds
Linus Walleij65d87652013-09-04 14:17:08 +02001333 *
1334 * returns -ENOTSUPP if the controller does not support setting
1335 * debounce.
Felipe Balbic4b5be92010-05-26 14:42:23 -07001336 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001337int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
Felipe Balbic4b5be92010-05-26 14:42:23 -07001338{
Felipe Balbic4b5be92010-05-26 14:42:23 -07001339 struct gpio_chip *chip;
Felipe Balbic4b5be92010-05-26 14:42:23 -07001340
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001341 if (!desc || !desc->chip) {
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001342 pr_warn("%s: invalid GPIO\n", __func__);
1343 return -EINVAL;
1344 }
1345
Felipe Balbic4b5be92010-05-26 14:42:23 -07001346 chip = desc->chip;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001347 if (!chip->set || !chip->set_debounce) {
Mark Brown6424de52013-09-09 10:33:49 +01001348 gpiod_dbg(desc,
1349 "%s: missing set() or set_debounce() operations\n",
1350 __func__);
Linus Walleij65d87652013-09-04 14:17:08 +02001351 return -ENOTSUPP;
Linus Walleijbe1a4b12013-08-30 09:41:45 +02001352 }
1353
Alexandre Courbotd82da792014-07-22 16:17:43 +09001354 return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce);
Felipe Balbic4b5be92010-05-26 14:42:23 -07001355}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001356EXPORT_SYMBOL_GPL(gpiod_set_debounce);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001357
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001358/**
1359 * gpiod_is_active_low - test whether a GPIO is active-low or not
1360 * @desc: the gpio descriptor to test
1361 *
1362 * Returns 1 if the GPIO is active-low, 0 otherwise.
1363 */
1364int gpiod_is_active_low(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001365{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001366 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001367}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001368EXPORT_SYMBOL_GPL(gpiod_is_active_low);
David Brownelld2876d02008-02-04 22:28:20 -08001369
1370/* I/O calls are only valid after configuration completed; the relevant
1371 * "is this a valid GPIO" error checks should already have been done.
1372 *
1373 * "Get" operations are often inlinable as reading a pin value register,
1374 * and masking the relevant bit in that register.
1375 *
1376 * When "set" operations are inlinable, they involve writing that mask to
1377 * one register to set a low value, or a different register to set it high.
1378 * Otherwise locking is needed, so there may be little value to inlining.
1379 *
1380 *------------------------------------------------------------------------
1381 *
1382 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers
1383 * have requested the GPIO. That can include implicit requesting by
1384 * a direction setting call. Marking a gpio as requested locks its chip
1385 * in memory, guaranteeing that these table lookups need no more locking
1386 * and that gpiochip_remove() will fail.
1387 *
1388 * REVISIT when debugging, consider adding some instrumentation to ensure
1389 * that the GPIO was actually requested.
1390 */
1391
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001392static int _gpiod_get_raw_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001393{
1394 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001395 int offset;
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001396 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001397
Alexandre Courbot372e7222013-02-03 01:29:29 +09001398 chip = desc->chip;
1399 offset = gpio_chip_hwgpio(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001400 value = chip->get ? chip->get(chip, offset) : -EIO;
Linus Walleij723a6302015-12-21 23:10:12 +01001401 value = value < 0 ? value : !!value;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001402 trace_gpio_value(desc_to_gpio(desc), 1, value);
Uwe Kleine-König3f397c212011-05-20 00:40:19 -06001403 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001404}
Alexandre Courbot372e7222013-02-03 01:29:29 +09001405
David Brownelld2876d02008-02-04 22:28:20 -08001406/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001407 * gpiod_get_raw_value() - return a gpio's raw value
1408 * @desc: gpio whose value will be returned
David Brownelld2876d02008-02-04 22:28:20 -08001409 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001410 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001411 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001412 *
1413 * This function should be called from contexts where we cannot sleep, and will
1414 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001415 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001416int gpiod_get_raw_value(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001417{
David Brownelld2876d02008-02-04 22:28:20 -08001418 if (!desc)
1419 return 0;
David Brownelld2876d02008-02-04 22:28:20 -08001420 /* Should be using gpio_get_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001421 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001422 return _gpiod_get_raw_value(desc);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001423}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001424EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001425
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001426/**
1427 * gpiod_get_value() - return a gpio's value
1428 * @desc: gpio whose value will be returned
1429 *
1430 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001431 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001432 *
1433 * This function should be called from contexts where we cannot sleep, and will
1434 * complain if the GPIO chip functions potentially sleep.
1435 */
1436int gpiod_get_value(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001437{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001438 int value;
1439 if (!desc)
1440 return 0;
1441 /* Should be using gpio_get_value_cansleep() */
1442 WARN_ON(desc->chip->can_sleep);
1443
1444 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001445 if (value < 0)
1446 return value;
1447
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001448 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1449 value = !value;
1450
1451 return value;
David Brownelld2876d02008-02-04 22:28:20 -08001452}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001453EXPORT_SYMBOL_GPL(gpiod_get_value);
David Brownelld2876d02008-02-04 22:28:20 -08001454
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301455/*
1456 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001457 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07001458 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301459 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001460static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301461{
1462 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001463 struct gpio_chip *chip = desc->chip;
1464 int offset = gpio_chip_hwgpio(desc);
1465
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301466 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001467 err = chip->direction_input(chip, offset);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301468 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001469 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301470 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001471 err = chip->direction_output(chip, offset, 0);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301472 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001473 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301474 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001475 trace_gpio_direction(desc_to_gpio(desc), value, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301476 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001477 gpiod_err(desc,
1478 "%s: Error in set_value for open drain err %d\n",
1479 __func__, err);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301480}
1481
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301482/*
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001483 * _gpio_set_open_source_value() - Set the open source gpio's value.
1484 * @desc: gpio descriptor whose state need to be set.
Colin Cronin20a8a962015-05-18 11:41:43 -07001485 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301486 */
Alexandre Courbot23600962014-03-11 15:52:09 +09001487static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301488{
1489 int err = 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001490 struct gpio_chip *chip = desc->chip;
1491 int offset = gpio_chip_hwgpio(desc);
1492
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301493 if (value) {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001494 err = chip->direction_output(chip, offset, 1);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301495 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001496 set_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301497 } else {
Alexandre Courbot372e7222013-02-03 01:29:29 +09001498 err = chip->direction_input(chip, offset);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301499 if (!err)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001500 clear_bit(FLAG_IS_OUT, &desc->flags);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301501 }
Alexandre Courbot372e7222013-02-03 01:29:29 +09001502 trace_gpio_direction(desc_to_gpio(desc), !value, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301503 if (err < 0)
Mark Brown6424de52013-09-09 10:33:49 +01001504 gpiod_err(desc,
1505 "%s: Error in set_value for open source err %d\n",
1506 __func__, err);
Laxman Dewangan25553ff2012-02-17 20:26:22 +05301507}
1508
Alexandre Courbot23600962014-03-11 15:52:09 +09001509static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value)
David Brownelld2876d02008-02-04 22:28:20 -08001510{
1511 struct gpio_chip *chip;
1512
Alexandre Courbot372e7222013-02-03 01:29:29 +09001513 chip = desc->chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001514 trace_gpio_value(desc_to_gpio(desc), 0, value);
1515 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1516 _gpio_set_open_drain_value(desc, value);
1517 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1518 _gpio_set_open_source_value(desc, value);
Laxman Dewanganaca5ce12012-02-17 20:26:21 +05301519 else
Alexandre Courbot372e7222013-02-03 01:29:29 +09001520 chip->set(chip, gpio_chip_hwgpio(desc), value);
1521}
1522
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001523/*
1524 * set multiple outputs on the same chip;
1525 * use the chip's set_multiple function if available;
1526 * otherwise set the outputs sequentially;
1527 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
1528 * defines which outputs are to be changed
1529 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
1530 * defines the values the outputs specified by mask are to be set to
1531 */
1532static void gpio_chip_set_multiple(struct gpio_chip *chip,
1533 unsigned long *mask, unsigned long *bits)
1534{
1535 if (chip->set_multiple) {
1536 chip->set_multiple(chip, mask, bits);
1537 } else {
1538 int i;
1539 for (i = 0; i < chip->ngpio; i++) {
1540 if (mask[BIT_WORD(i)] == 0) {
1541 /* no more set bits in this mask word;
1542 * skip ahead to the next word */
1543 i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
1544 continue;
1545 }
1546 /* set outputs if the corresponding mask bit is set */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001547 if (__test_and_clear_bit(i, mask))
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001548 chip->set(chip, i, test_bit(i, bits));
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001549 }
1550 }
1551}
1552
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001553static void gpiod_set_array_value_priv(bool raw, bool can_sleep,
1554 unsigned int array_size,
1555 struct gpio_desc **desc_array,
1556 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001557{
1558 int i = 0;
1559
1560 while (i < array_size) {
1561 struct gpio_chip *chip = desc_array[i]->chip;
1562 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
1563 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
1564 int count = 0;
1565
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001566 if (!can_sleep)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001567 WARN_ON(chip->can_sleep);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001568
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001569 memset(mask, 0, sizeof(mask));
1570 do {
1571 struct gpio_desc *desc = desc_array[i];
1572 int hwgpio = gpio_chip_hwgpio(desc);
1573 int value = value_array[i];
1574
1575 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1576 value = !value;
1577 trace_gpio_value(desc_to_gpio(desc), 0, value);
1578 /*
1579 * collect all normal outputs belonging to the same chip
1580 * open drain and open source outputs are set individually
1581 */
1582 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001583 _gpio_set_open_drain_value(desc, value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001584 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
1585 _gpio_set_open_source_value(desc, value);
1586 } else {
1587 __set_bit(hwgpio, mask);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001588 if (value)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001589 __set_bit(hwgpio, bits);
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001590 else
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001591 __clear_bit(hwgpio, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001592 count++;
1593 }
1594 i++;
1595 } while ((i < array_size) && (desc_array[i]->chip == chip));
1596 /* push collected bits to outputs */
Daniel Lockyer38e003f2015-06-10 14:26:27 +01001597 if (count != 0)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001598 gpio_chip_set_multiple(chip, mask, bits);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001599 }
1600}
1601
David Brownelld2876d02008-02-04 22:28:20 -08001602/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001603 * gpiod_set_raw_value() - assign a gpio's raw value
1604 * @desc: gpio whose value will be assigned
David Brownelld2876d02008-02-04 22:28:20 -08001605 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001606 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001607 * Set the raw value of the GPIO, i.e. the value of its physical line without
1608 * regard for its ACTIVE_LOW status.
1609 *
1610 * This function should be called from contexts where we cannot sleep, and will
1611 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001612 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001613void gpiod_set_raw_value(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001614{
David Brownelld2876d02008-02-04 22:28:20 -08001615 if (!desc)
1616 return;
David Brownelld2876d02008-02-04 22:28:20 -08001617 /* Should be using gpio_set_value_cansleep() */
Alexandre Courbotd8e0ac02013-09-04 20:29:25 +09001618 WARN_ON(desc->chip->can_sleep);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001619 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001620}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001621EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
David Brownelld2876d02008-02-04 22:28:20 -08001622
1623/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001624 * gpiod_set_value() - assign a gpio's value
1625 * @desc: gpio whose value will be assigned
1626 * @value: value to assign
David Brownelld2876d02008-02-04 22:28:20 -08001627 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001628 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1629 * account
1630 *
1631 * This function should be called from contexts where we cannot sleep, and will
1632 * complain if the GPIO chip functions potentially sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001633 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001634void gpiod_set_value(struct gpio_desc *desc, int value)
1635{
1636 if (!desc)
1637 return;
1638 /* Should be using gpio_set_value_cansleep() */
1639 WARN_ON(desc->chip->can_sleep);
1640 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1641 value = !value;
1642 _gpiod_set_raw_value(desc, value);
1643}
1644EXPORT_SYMBOL_GPL(gpiod_set_value);
1645
1646/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001647 * gpiod_set_raw_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001648 * @array_size: number of elements in the descriptor / value arrays
1649 * @desc_array: array of GPIO descriptors whose values will be assigned
1650 * @value_array: array of values to assign
1651 *
1652 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1653 * without regard for their ACTIVE_LOW status.
1654 *
1655 * This function should be called from contexts where we cannot sleep, and will
1656 * complain if the GPIO chip functions potentially sleep.
1657 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001658void gpiod_set_raw_array_value(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001659 struct gpio_desc **desc_array, int *value_array)
1660{
1661 if (!desc_array)
1662 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001663 gpiod_set_array_value_priv(true, false, array_size, desc_array,
1664 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001665}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001666EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001667
1668/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001669 * gpiod_set_array_value() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001670 * @array_size: number of elements in the descriptor / value arrays
1671 * @desc_array: array of GPIO descriptors whose values will be assigned
1672 * @value_array: array of values to assign
1673 *
1674 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1675 * into account.
1676 *
1677 * This function should be called from contexts where we cannot sleep, and will
1678 * complain if the GPIO chip functions potentially sleep.
1679 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001680void gpiod_set_array_value(unsigned int array_size,
1681 struct gpio_desc **desc_array, int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001682{
1683 if (!desc_array)
1684 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001685 gpiod_set_array_value_priv(false, false, array_size, desc_array,
1686 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001687}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001688EXPORT_SYMBOL_GPL(gpiod_set_array_value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001689
1690/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001691 * gpiod_cansleep() - report whether gpio value access may sleep
1692 * @desc: gpio to check
1693 *
1694 */
1695int gpiod_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001696{
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001697 if (!desc)
1698 return 0;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001699 return desc->chip->can_sleep;
1700}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001701EXPORT_SYMBOL_GPL(gpiod_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001702
David Brownell0f6d5042008-10-15 22:03:14 -07001703/**
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001704 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
1705 * @desc: gpio whose IRQ will be returned (already requested)
David Brownell0f6d5042008-10-15 22:03:14 -07001706 *
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001707 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
1708 * error.
David Brownell0f6d5042008-10-15 22:03:14 -07001709 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001710int gpiod_to_irq(const struct gpio_desc *desc)
David Brownell0f6d5042008-10-15 22:03:14 -07001711{
1712 struct gpio_chip *chip;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001713 int offset;
David Brownell0f6d5042008-10-15 22:03:14 -07001714
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001715 if (!desc)
1716 return -EINVAL;
Alexandre Courbot372e7222013-02-03 01:29:29 +09001717 chip = desc->chip;
1718 offset = gpio_chip_hwgpio(desc);
1719 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
1720}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001721EXPORT_SYMBOL_GPL(gpiod_to_irq);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001722
Linus Walleijd468bf92013-09-24 11:54:38 +02001723/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001724 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001725 * @chip: the chip the GPIO to lock belongs to
1726 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001727 *
1728 * This is used directly by GPIO drivers that want to lock down
Linus Walleijf438acd2014-03-07 10:12:49 +08001729 * a certain GPIO line to be used for IRQs.
David Brownelld2876d02008-02-04 22:28:20 -08001730 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001731int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
David Brownelld2876d02008-02-04 22:28:20 -08001732{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001733 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001734 return -EINVAL;
1735
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001736 if (test_bit(FLAG_IS_OUT, &chip->desc[offset].flags)) {
1737 chip_err(chip,
Linus Walleijd468bf92013-09-24 11:54:38 +02001738 "%s: tried to flag a GPIO set as output for IRQ\n",
1739 __func__);
1740 return -EIO;
1741 }
1742
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001743 set_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001744 return 0;
1745}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001746EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001747
Linus Walleijd468bf92013-09-24 11:54:38 +02001748/**
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001749 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001750 * @chip: the chip the GPIO to lock belongs to
1751 * @offset: the offset of the GPIO to lock as IRQ
Linus Walleijd468bf92013-09-24 11:54:38 +02001752 *
1753 * This is used directly by GPIO drivers that want to indicate
1754 * that a certain GPIO is no longer used exclusively for IRQ.
1755 */
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001756void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
Linus Walleijd468bf92013-09-24 11:54:38 +02001757{
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001758 if (offset >= chip->ngpio)
Linus Walleijd468bf92013-09-24 11:54:38 +02001759 return;
1760
Alexandre Courbotd74be6d2014-07-22 16:17:42 +09001761 clear_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02001762}
Alexandre Courbote3a2e872014-10-23 17:27:07 +09001763EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
Linus Walleijd468bf92013-09-24 11:54:38 +02001764
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001765/**
1766 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
1767 * @desc: gpio whose value will be returned
1768 *
1769 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001770 * its ACTIVE_LOW status, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001771 *
1772 * This function is to be called from contexts that can sleep.
David Brownelld2876d02008-02-04 22:28:20 -08001773 */
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001774int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
David Brownelld2876d02008-02-04 22:28:20 -08001775{
David Brownelld2876d02008-02-04 22:28:20 -08001776 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001777 if (!desc)
1778 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001779 return _gpiod_get_raw_value(desc);
David Brownelld2876d02008-02-04 22:28:20 -08001780}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001781EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001782
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001783/**
1784 * gpiod_get_value_cansleep() - return a gpio's value
1785 * @desc: gpio whose value will be returned
1786 *
1787 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001788 * account, or negative errno on failure.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001789 *
1790 * This function is to be called from contexts that can sleep.
1791 */
1792int gpiod_get_value_cansleep(const struct gpio_desc *desc)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001793{
David Brownelld2876d02008-02-04 22:28:20 -08001794 int value;
David Brownelld2876d02008-02-04 22:28:20 -08001795
1796 might_sleep_if(extra_checks);
1797 if (!desc)
1798 return 0;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001799
1800 value = _gpiod_get_raw_value(desc);
Bjorn Anderssone20538b2015-08-28 09:44:18 -07001801 if (value < 0)
1802 return value;
1803
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001804 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1805 value = !value;
1806
David Brownelld2876d02008-02-04 22:28:20 -08001807 return value;
1808}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001809EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001810
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001811/**
1812 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
1813 * @desc: gpio whose value will be assigned
1814 * @value: value to assign
1815 *
1816 * Set the raw value of the GPIO, i.e. the value of its physical line without
1817 * regard for its ACTIVE_LOW status.
1818 *
1819 * This function is to be called from contexts that can sleep.
1820 */
1821void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001822{
David Brownelld2876d02008-02-04 22:28:20 -08001823 might_sleep_if(extra_checks);
Alexandre Courbotbcabdef2013-02-15 14:46:14 +09001824 if (!desc)
1825 return;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001826 _gpiod_set_raw_value(desc, value);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001827}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001828EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
Alexandre Courbot372e7222013-02-03 01:29:29 +09001829
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001830/**
1831 * gpiod_set_value_cansleep() - assign a gpio's value
1832 * @desc: gpio whose value will be assigned
1833 * @value: value to assign
1834 *
1835 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1836 * account
1837 *
1838 * This function is to be called from contexts that can sleep.
1839 */
1840void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
Alexandre Courbot372e7222013-02-03 01:29:29 +09001841{
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001842 might_sleep_if(extra_checks);
1843 if (!desc)
1844 return;
1845
1846 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1847 value = !value;
1848 _gpiod_set_raw_value(desc, value);
David Brownelld2876d02008-02-04 22:28:20 -08001849}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001850EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
David Brownelld2876d02008-02-04 22:28:20 -08001851
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001852/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001853 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001854 * @array_size: number of elements in the descriptor / value arrays
1855 * @desc_array: array of GPIO descriptors whose values will be assigned
1856 * @value_array: array of values to assign
1857 *
1858 * Set the raw values of the GPIOs, i.e. the values of the physical lines
1859 * without regard for their ACTIVE_LOW status.
1860 *
1861 * This function is to be called from contexts that can sleep.
1862 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001863void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
1864 struct gpio_desc **desc_array,
1865 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001866{
1867 might_sleep_if(extra_checks);
1868 if (!desc_array)
1869 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001870 gpiod_set_array_value_priv(true, true, array_size, desc_array,
1871 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001872}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001873EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001874
1875/**
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001876 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001877 * @array_size: number of elements in the descriptor / value arrays
1878 * @desc_array: array of GPIO descriptors whose values will be assigned
1879 * @value_array: array of values to assign
1880 *
1881 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
1882 * into account.
1883 *
1884 * This function is to be called from contexts that can sleep.
1885 */
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001886void gpiod_set_array_value_cansleep(unsigned int array_size,
1887 struct gpio_desc **desc_array,
1888 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001889{
1890 might_sleep_if(extra_checks);
1891 if (!desc_array)
1892 return;
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001893 gpiod_set_array_value_priv(false, true, array_size, desc_array,
1894 value_array);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001895}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +02001896EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +01001897
1898/**
Alexandre Courbotad824782013-12-03 12:20:11 +09001899 * gpiod_add_lookup_table() - register GPIO device consumers
1900 * @table: table of consumers to register
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001901 */
Alexandre Courbotad824782013-12-03 12:20:11 +09001902void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001903{
1904 mutex_lock(&gpio_lookup_lock);
1905
Alexandre Courbotad824782013-12-03 12:20:11 +09001906 list_add_tail(&table->list, &gpio_lookup_list);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001907
1908 mutex_unlock(&gpio_lookup_lock);
David Brownelld2876d02008-02-04 22:28:20 -08001909}
1910
Shobhit Kumarbe9015a2015-06-26 14:32:04 +05301911/**
1912 * gpiod_remove_lookup_table() - unregister GPIO device consumers
1913 * @table: table of consumers to unregister
1914 */
1915void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
1916{
1917 mutex_lock(&gpio_lookup_lock);
1918
1919 list_del(&table->list);
1920
1921 mutex_unlock(&gpio_lookup_lock);
1922}
1923
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001924static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001925 unsigned int idx,
1926 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001927{
1928 char prop_name[32]; /* 32 is max size of property name */
1929 enum of_gpio_flags of_flags;
1930 struct gpio_desc *desc;
Thierry Redingdd34c372014-04-23 17:28:09 +02001931 unsigned int i;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001932
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001933 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Thierry Redingdd34c372014-04-23 17:28:09 +02001934 if (con_id)
Olliver Schinagl9e089242015-01-21 22:33:45 +01001935 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001936 gpio_suffixes[i]);
Thierry Redingdd34c372014-04-23 17:28:09 +02001937 else
Olliver Schinagl9e089242015-01-21 22:33:45 +01001938 snprintf(prop_name, sizeof(prop_name), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001939 gpio_suffixes[i]);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001940
Thierry Redingdd34c372014-04-23 17:28:09 +02001941 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
1942 &of_flags);
Tony Lindgren06fc3b72014-06-02 16:13:46 -07001943 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
Thierry Redingdd34c372014-04-23 17:28:09 +02001944 break;
1945 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001946
1947 if (IS_ERR(desc))
1948 return desc;
1949
1950 if (of_flags & OF_GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001951 *flags |= GPIO_ACTIVE_LOW;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001952
Laurent Pinchart90b665f2015-10-13 00:20:21 +03001953 if (of_flags & OF_GPIO_SINGLE_ENDED) {
1954 if (of_flags & OF_GPIO_ACTIVE_LOW)
1955 *flags |= GPIO_OPEN_DRAIN;
1956 else
1957 *flags |= GPIO_OPEN_SOURCE;
1958 }
1959
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001960 return desc;
1961}
Alexandre Courbotbae48da2013-10-17 10:21:38 -07001962
Mika Westerberg81f59e92013-10-10 11:01:09 +03001963static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001964 unsigned int idx,
1965 enum gpio_lookup_flags *flags)
Mika Westerberg81f59e92013-10-10 11:01:09 +03001966{
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001967 struct acpi_device *adev = ACPI_COMPANION(dev);
Mika Westerberge01f4402013-10-10 11:01:10 +03001968 struct acpi_gpio_info info;
1969 struct gpio_desc *desc;
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001970 char propname[32];
1971 int i;
Mika Westerberge01f4402013-10-10 11:01:10 +03001972
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001973 /* Try first from _DSD */
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001974 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001975 if (con_id && strcmp(con_id, "gpios")) {
1976 snprintf(propname, sizeof(propname), "%s-%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001977 con_id, gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001978 } else {
1979 snprintf(propname, sizeof(propname), "%s",
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +01001980 gpio_suffixes[i]);
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001981 }
Mika Westerberge01f4402013-10-10 11:01:10 +03001982
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001983 desc = acpi_get_gpiod_by_index(adev, propname, idx, &info);
1984 if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
1985 break;
1986 }
1987
1988 /* Then from plain _CRS GPIOs */
1989 if (IS_ERR(desc)) {
Dmitry Torokhov10cf4892015-11-11 11:45:30 -08001990 if (!acpi_can_fallback_to_crs(adev, con_id))
1991 return ERR_PTR(-ENOENT);
1992
Mika Westerberg0d9a6932014-10-29 15:41:01 +01001993 desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info);
1994 if (IS_ERR(desc))
1995 return desc;
1996 }
1997
Christophe RICARD52044722015-12-23 23:25:34 +01001998 if (info.polarity == GPIO_ACTIVE_LOW)
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09001999 *flags |= GPIO_ACTIVE_LOW;
Mika Westerberge01f4402013-10-10 11:01:10 +03002000
2001 return desc;
Mika Westerberg81f59e92013-10-10 11:01:09 +03002002}
2003
Alexandre Courbotad824782013-12-03 12:20:11 +09002004static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
2005{
2006 const char *dev_id = dev ? dev_name(dev) : NULL;
2007 struct gpiod_lookup_table *table;
2008
2009 mutex_lock(&gpio_lookup_lock);
2010
2011 list_for_each_entry(table, &gpio_lookup_list, list) {
2012 if (table->dev_id && dev_id) {
2013 /*
2014 * Valid strings on both ends, must be identical to have
2015 * a match
2016 */
2017 if (!strcmp(table->dev_id, dev_id))
2018 goto found;
2019 } else {
2020 /*
2021 * One of the pointers is NULL, so both must be to have
2022 * a match
2023 */
2024 if (dev_id == table->dev_id)
2025 goto found;
2026 }
2027 }
2028 table = NULL;
2029
2030found:
2031 mutex_unlock(&gpio_lookup_lock);
2032 return table;
2033}
2034
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002035static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
Alexandre Courbot53e7cac2013-11-16 21:44:52 +09002036 unsigned int idx,
2037 enum gpio_lookup_flags *flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002038{
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002039 struct gpio_desc *desc = ERR_PTR(-ENOENT);
Alexandre Courbotad824782013-12-03 12:20:11 +09002040 struct gpiod_lookup_table *table;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002041 struct gpiod_lookup *p;
2042
Alexandre Courbotad824782013-12-03 12:20:11 +09002043 table = gpiod_find_lookup_table(dev);
2044 if (!table)
2045 return desc;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002046
Alexandre Courbotad824782013-12-03 12:20:11 +09002047 for (p = &table->table[0]; p->chip_label; p++) {
2048 struct gpio_chip *chip;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002049
Alexandre Courbotad824782013-12-03 12:20:11 +09002050 /* idx must always match exactly */
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002051 if (p->idx != idx)
2052 continue;
2053
Alexandre Courbotad824782013-12-03 12:20:11 +09002054 /* If the lookup entry has a con_id, require exact match */
2055 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
2056 continue;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002057
Alexandre Courbotad824782013-12-03 12:20:11 +09002058 chip = find_chip_by_name(p->chip_label);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002059
Alexandre Courbotad824782013-12-03 12:20:11 +09002060 if (!chip) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002061 dev_err(dev, "cannot find GPIO chip %s\n",
2062 p->chip_label);
2063 return ERR_PTR(-ENODEV);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002064 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002065
Alexandre Courbotad824782013-12-03 12:20:11 +09002066 if (chip->ngpio <= p->chip_hwnum) {
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002067 dev_err(dev,
2068 "requested GPIO %d is out of range [0..%d] for chip %s\n",
2069 idx, chip->ngpio, chip->label);
2070 return ERR_PTR(-EINVAL);
Alexandre Courbotad824782013-12-03 12:20:11 +09002071 }
2072
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +09002073 desc = gpiochip_get_desc(chip, p->chip_hwnum);
Alexandre Courbotad824782013-12-03 12:20:11 +09002074 *flags = p->flags;
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002075
2076 return desc;
Alexandre Courbotad824782013-12-03 12:20:11 +09002077 }
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002078
2079 return desc;
2080}
2081
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002082static int dt_gpio_count(struct device *dev, const char *con_id)
2083{
2084 int ret;
2085 char propname[32];
2086 unsigned int i;
2087
2088 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
2089 if (con_id)
2090 snprintf(propname, sizeof(propname), "%s-%s",
2091 con_id, gpio_suffixes[i]);
2092 else
2093 snprintf(propname, sizeof(propname), "%s",
2094 gpio_suffixes[i]);
2095
2096 ret = of_gpio_named_count(dev->of_node, propname);
2097 if (ret >= 0)
2098 break;
2099 }
2100 return ret;
2101}
2102
2103static int platform_gpio_count(struct device *dev, const char *con_id)
2104{
2105 struct gpiod_lookup_table *table;
2106 struct gpiod_lookup *p;
2107 unsigned int count = 0;
2108
2109 table = gpiod_find_lookup_table(dev);
2110 if (!table)
2111 return -ENOENT;
2112
2113 for (p = &table->table[0]; p->chip_label; p++) {
2114 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
2115 (!con_id && !p->con_id))
2116 count++;
2117 }
2118 if (!count)
2119 return -ENOENT;
2120
2121 return count;
2122}
2123
2124/**
2125 * gpiod_count - return the number of GPIOs associated with a device / function
2126 * or -ENOENT if no GPIO has been assigned to the requested function
2127 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2128 * @con_id: function within the GPIO consumer
2129 */
2130int gpiod_count(struct device *dev, const char *con_id)
2131{
2132 int count = -ENOENT;
2133
2134 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
2135 count = dt_gpio_count(dev, con_id);
2136 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
2137 count = acpi_gpio_count(dev, con_id);
2138
2139 if (count < 0)
2140 count = platform_gpio_count(dev, con_id);
2141
2142 return count;
2143}
2144EXPORT_SYMBOL_GPL(gpiod_count);
2145
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002146/**
Thierry Reding08791622014-04-25 16:54:22 +02002147 * gpiod_get - obtain a GPIO for a given GPIO function
Alexandre Courbotad824782013-12-03 12:20:11 +09002148 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002149 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002150 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002151 *
2152 * Return the GPIO descriptor corresponding to the function con_id of device
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002153 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
Colin Cronin20a8a962015-05-18 11:41:43 -07002154 * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002155 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002156struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002157 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002158{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002159 return gpiod_get_index(dev, con_id, 0, flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002160}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002161EXPORT_SYMBOL_GPL(gpiod_get);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002162
2163/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02002164 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
2165 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2166 * @con_id: function within the GPIO consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002167 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02002168 *
2169 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
2170 * the requested function it will return NULL. This is convenient for drivers
2171 * that need to handle optional GPIOs.
2172 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002173struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002174 const char *con_id,
2175 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02002176{
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002177 return gpiod_get_index_optional(dev, con_id, 0, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002178}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002179EXPORT_SYMBOL_GPL(gpiod_get_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002180
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002181/**
2182 * gpiod_parse_flags - helper function to parse GPIO lookup flags
2183 * @desc: gpio to be setup
2184 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2185 * of_get_gpio_hog()
2186 *
2187 * Set the GPIO descriptor flags based on the given GPIO lookup flags.
2188 */
2189static void gpiod_parse_flags(struct gpio_desc *desc, unsigned long lflags)
2190{
2191 if (lflags & GPIO_ACTIVE_LOW)
2192 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2193 if (lflags & GPIO_OPEN_DRAIN)
2194 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2195 if (lflags & GPIO_OPEN_SOURCE)
2196 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2197}
Benoit Parrotf625d462015-02-02 11:44:44 -06002198
2199/**
2200 * gpiod_configure_flags - helper function to configure a given GPIO
2201 * @desc: gpio whose value will be assigned
2202 * @con_id: function within the GPIO consumer
Benoit Parrotf625d462015-02-02 11:44:44 -06002203 * @dflags: gpiod_flags - optional GPIO initialization flags
2204 *
2205 * Return 0 on success, -ENOENT if no GPIO has been assigned to the
2206 * requested function and/or index, or another IS_ERR() code if an error
2207 * occurred while trying to acquire the GPIO.
2208 */
2209static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002210 enum gpiod_flags dflags)
Benoit Parrotf625d462015-02-02 11:44:44 -06002211{
2212 int status;
2213
Benoit Parrotf625d462015-02-02 11:44:44 -06002214 /* No particular flag request, return here... */
2215 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
2216 pr_debug("no flags found for %s\n", con_id);
2217 return 0;
2218 }
2219
2220 /* Process flags */
2221 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
2222 status = gpiod_direction_output(desc,
2223 dflags & GPIOD_FLAGS_BIT_DIR_VAL);
2224 else
2225 status = gpiod_direction_input(desc);
2226
2227 return status;
2228}
2229
Thierry Reding29a1f2332014-04-25 17:10:06 +02002230/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002231 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
Andy Shevchenkofdd6a5f2013-12-05 11:26:26 +02002232 * @dev: GPIO consumer, can be NULL for system-global GPIOs
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002233 * @con_id: function within the GPIO consumer
2234 * @idx: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002235 * @flags: optional GPIO initialization flags
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002236 *
2237 * This variant of gpiod_get() allows to access GPIOs other than the first
2238 * defined one for functions that define several GPIOs.
2239 *
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002240 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
2241 * requested function and/or index, or another IS_ERR() code if an error
Colin Cronin20a8a962015-05-18 11:41:43 -07002242 * occurred while trying to acquire the GPIO.
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002243 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002244struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002245 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002246 unsigned int idx,
2247 enum gpiod_flags flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002248{
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09002249 struct gpio_desc *desc = NULL;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002250 int status;
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002251 enum gpio_lookup_flags lookupflags = 0;
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002252
2253 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
2254
Rafael J. Wysocki4d8440b2015-03-10 23:08:57 +01002255 if (dev) {
2256 /* Using device tree? */
2257 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
2258 dev_dbg(dev, "using device tree for GPIO lookup\n");
2259 desc = of_find_gpio(dev, con_id, idx, &lookupflags);
2260 } else if (ACPI_COMPANION(dev)) {
2261 dev_dbg(dev, "using ACPI for GPIO lookup\n");
2262 desc = acpi_find_gpio(dev, con_id, idx, &lookupflags);
2263 }
Alexandre Courbot35c5d7f2013-11-23 19:34:50 +09002264 }
2265
2266 /*
2267 * Either we are not using DT or ACPI, or their lookup did not return
2268 * a result. In that case, use platform lookup as a fallback.
2269 */
Alexandre Courbot2a3cf6a2013-12-11 11:32:28 +09002270 if (!desc || desc == ERR_PTR(-ENOENT)) {
Alexander Shiyan43a87852014-09-19 11:39:25 +04002271 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002272 desc = gpiod_find(dev, con_id, idx, &lookupflags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002273 }
2274
2275 if (IS_ERR(desc)) {
Heikki Krogerus351cfe02013-11-29 15:47:34 +02002276 dev_dbg(dev, "lookup for GPIO %s failed\n", con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002277 return desc;
2278 }
2279
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002280 gpiod_parse_flags(desc, lookupflags);
2281
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002282 status = gpiod_request(desc, con_id);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002283 if (status < 0)
2284 return ERR_PTR(status);
2285
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002286 status = gpiod_configure_flags(desc, con_id, flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002287 if (status < 0) {
2288 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
2289 gpiod_put(desc);
2290 return ERR_PTR(status);
2291 }
2292
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002293 return desc;
2294}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002295EXPORT_SYMBOL_GPL(gpiod_get_index);
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002296
2297/**
Mika Westerberg40b73182014-10-21 13:33:59 +02002298 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
2299 * @fwnode: handle of the firmware node
2300 * @propname: name of the firmware property representing the GPIO
2301 *
2302 * This function can be used for drivers that get their configuration
2303 * from firmware.
2304 *
2305 * Function properly finds the corresponding GPIO using whatever is the
2306 * underlying firmware interface and then makes sure that the GPIO
2307 * descriptor is requested before it is returned to the caller.
2308 *
2309 * In case of error an ERR_PTR() is returned.
2310 */
2311struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
2312 const char *propname)
2313{
2314 struct gpio_desc *desc = ERR_PTR(-ENODEV);
2315 bool active_low = false;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002316 bool single_ended = false;
Mika Westerberg40b73182014-10-21 13:33:59 +02002317 int ret;
2318
2319 if (!fwnode)
2320 return ERR_PTR(-EINVAL);
2321
2322 if (is_of_node(fwnode)) {
2323 enum of_gpio_flags flags;
2324
Alexander Sverdlinc181fb32015-06-22 22:38:53 +02002325 desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, 0,
Mika Westerberg40b73182014-10-21 13:33:59 +02002326 &flags);
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002327 if (!IS_ERR(desc)) {
Mika Westerberg40b73182014-10-21 13:33:59 +02002328 active_low = flags & OF_GPIO_ACTIVE_LOW;
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002329 single_ended = flags & OF_GPIO_SINGLE_ENDED;
2330 }
Mika Westerberg40b73182014-10-21 13:33:59 +02002331 } else if (is_acpi_node(fwnode)) {
2332 struct acpi_gpio_info info;
2333
Rafael J. Wysocki504a3372015-08-27 04:42:33 +02002334 desc = acpi_node_get_gpiod(fwnode, propname, 0, &info);
Mika Westerberg40b73182014-10-21 13:33:59 +02002335 if (!IS_ERR(desc))
Christophe RICARD52044722015-12-23 23:25:34 +01002336 active_low = info.polarity == GPIO_ACTIVE_LOW;
Mika Westerberg40b73182014-10-21 13:33:59 +02002337 }
2338
2339 if (IS_ERR(desc))
2340 return desc;
2341
Mika Westerberg40b73182014-10-21 13:33:59 +02002342 if (active_low)
2343 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2344
Laurent Pinchart90b665f2015-10-13 00:20:21 +03002345 if (single_ended) {
2346 if (active_low)
2347 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
2348 else
2349 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
2350 }
2351
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002352 ret = gpiod_request(desc, NULL);
2353 if (ret)
2354 return ERR_PTR(ret);
2355
Mika Westerberg40b73182014-10-21 13:33:59 +02002356 return desc;
2357}
2358EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
2359
2360/**
Thierry Reding29a1f2332014-04-25 17:10:06 +02002361 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
2362 * function
2363 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2364 * @con_id: function within the GPIO consumer
2365 * @index: index of the GPIO to obtain in the consumer
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002366 * @flags: optional GPIO initialization flags
Thierry Reding29a1f2332014-04-25 17:10:06 +02002367 *
2368 * This is equivalent to gpiod_get_index(), except that when no GPIO with the
2369 * specified index was assigned to the requested function it will return NULL.
2370 * This is convenient for drivers that need to handle optional GPIOs.
2371 */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002372struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +02002373 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002374 unsigned int index,
2375 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +02002376{
2377 struct gpio_desc *desc;
2378
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +09002379 desc = gpiod_get_index(dev, con_id, index, flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002380 if (IS_ERR(desc)) {
2381 if (PTR_ERR(desc) == -ENOENT)
2382 return NULL;
2383 }
2384
2385 return desc;
2386}
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +01002387EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
Thierry Reding29a1f2332014-04-25 17:10:06 +02002388
2389/**
Benoit Parrotf625d462015-02-02 11:44:44 -06002390 * gpiod_hog - Hog the specified GPIO desc given the provided flags
2391 * @desc: gpio whose value will be assigned
2392 * @name: gpio line name
2393 * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
2394 * of_get_gpio_hog()
2395 * @dflags: gpiod_flags - optional GPIO initialization flags
2396 */
2397int gpiod_hog(struct gpio_desc *desc, const char *name,
2398 unsigned long lflags, enum gpiod_flags dflags)
2399{
2400 struct gpio_chip *chip;
2401 struct gpio_desc *local_desc;
2402 int hwnum;
2403 int status;
2404
2405 chip = gpiod_to_chip(desc);
2406 hwnum = gpio_chip_hwgpio(desc);
2407
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002408 gpiod_parse_flags(desc, lflags);
2409
Benoit Parrotf625d462015-02-02 11:44:44 -06002410 local_desc = gpiochip_request_own_desc(chip, hwnum, name);
2411 if (IS_ERR(local_desc)) {
Linus Walleija7138902015-06-05 11:36:10 +02002412 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed\n",
2413 name, chip->label, hwnum);
Benoit Parrotf625d462015-02-02 11:44:44 -06002414 return PTR_ERR(local_desc);
2415 }
2416
Laurent Pinchart923b93e2015-10-13 00:20:20 +03002417 status = gpiod_configure_flags(desc, name, dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -06002418 if (status < 0) {
Linus Walleija7138902015-06-05 11:36:10 +02002419 pr_err("setup of hog GPIO %s (chip %s, offset %d) failed\n",
2420 name, chip->label, hwnum);
Benoit Parrotf625d462015-02-02 11:44:44 -06002421 gpiochip_free_own_desc(desc);
2422 return status;
2423 }
2424
2425 /* Mark GPIO as hogged so it can be identified and removed later */
2426 set_bit(FLAG_IS_HOGGED, &desc->flags);
2427
2428 pr_info("GPIO line %d (%s) hogged as %s%s\n",
2429 desc_to_gpio(desc), name,
2430 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
2431 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
2432 (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
2433
2434 return 0;
2435}
2436
2437/**
2438 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
2439 * @chip: gpio chip to act on
2440 *
2441 * This is only used by of_gpiochip_remove to free hogged gpios
2442 */
2443static void gpiochip_free_hogs(struct gpio_chip *chip)
2444{
2445 int id;
2446
2447 for (id = 0; id < chip->ngpio; id++) {
2448 if (test_bit(FLAG_IS_HOGGED, &chip->desc[id].flags))
2449 gpiochip_free_own_desc(&chip->desc[id]);
2450 }
2451}
2452
2453/**
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002454 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
2455 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2456 * @con_id: function within the GPIO consumer
2457 * @flags: optional GPIO initialization flags
2458 *
2459 * This function acquires all the GPIOs defined under a given function.
2460 *
2461 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
2462 * no GPIO has been assigned to the requested function, or another IS_ERR()
2463 * code if an error occurred while trying to acquire the GPIOs.
2464 */
2465struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
2466 const char *con_id,
2467 enum gpiod_flags flags)
2468{
2469 struct gpio_desc *desc;
2470 struct gpio_descs *descs;
2471 int count;
2472
2473 count = gpiod_count(dev, con_id);
2474 if (count < 0)
2475 return ERR_PTR(count);
2476
2477 descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count,
2478 GFP_KERNEL);
2479 if (!descs)
2480 return ERR_PTR(-ENOMEM);
2481
2482 for (descs->ndescs = 0; descs->ndescs < count; ) {
2483 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
2484 if (IS_ERR(desc)) {
2485 gpiod_put_array(descs);
2486 return ERR_CAST(desc);
2487 }
2488 descs->desc[descs->ndescs] = desc;
2489 descs->ndescs++;
2490 }
2491 return descs;
2492}
2493EXPORT_SYMBOL_GPL(gpiod_get_array);
2494
2495/**
2496 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
2497 * function
2498 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2499 * @con_id: function within the GPIO consumer
2500 * @flags: optional GPIO initialization flags
2501 *
2502 * This is equivalent to gpiod_get_array(), except that when no GPIO was
2503 * assigned to the requested function it will return NULL.
2504 */
2505struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
2506 const char *con_id,
2507 enum gpiod_flags flags)
2508{
2509 struct gpio_descs *descs;
2510
2511 descs = gpiod_get_array(dev, con_id, flags);
2512 if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
2513 return NULL;
2514
2515 return descs;
2516}
2517EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
2518
2519/**
Alexandre Courbotbae48da2013-10-17 10:21:38 -07002520 * gpiod_put - dispose of a GPIO descriptor
2521 * @desc: GPIO descriptor to dispose of
2522 *
2523 * No descriptor can be used after gpiod_put() has been called on it.
2524 */
2525void gpiod_put(struct gpio_desc *desc)
2526{
2527 gpiod_free(desc);
2528}
2529EXPORT_SYMBOL_GPL(gpiod_put);
David Brownelld2876d02008-02-04 22:28:20 -08002530
Rojhalat Ibrahim66858522015-02-11 17:27:58 +01002531/**
2532 * gpiod_put_array - dispose of multiple GPIO descriptors
2533 * @descs: struct gpio_descs containing an array of descriptors
2534 */
2535void gpiod_put_array(struct gpio_descs *descs)
2536{
2537 unsigned int i;
2538
2539 for (i = 0; i < descs->ndescs; i++)
2540 gpiod_put(descs->desc[i]);
2541
2542 kfree(descs);
2543}
2544EXPORT_SYMBOL_GPL(gpiod_put_array);
2545
David Brownelld2876d02008-02-04 22:28:20 -08002546#ifdef CONFIG_DEBUG_FS
2547
2548static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2549{
2550 unsigned i;
2551 unsigned gpio = chip->base;
Alexandre Courbot6c0b4e62013-02-03 01:29:30 +09002552 struct gpio_desc *gdesc = &chip->desc[0];
David Brownelld2876d02008-02-04 22:28:20 -08002553 int is_out;
Linus Walleijd468bf92013-09-24 11:54:38 +02002554 int is_irq;
David Brownelld2876d02008-02-04 22:28:20 -08002555
2556 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
Markus Pargmannced433e2015-08-14 16:11:02 +02002557 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
2558 if (gdesc->name) {
2559 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
2560 gpio, gdesc->name);
2561 }
David Brownelld2876d02008-02-04 22:28:20 -08002562 continue;
Markus Pargmannced433e2015-08-14 16:11:02 +02002563 }
David Brownelld2876d02008-02-04 22:28:20 -08002564
Alexandre Courbot372e7222013-02-03 01:29:29 +09002565 gpiod_get_direction(gdesc);
David Brownelld2876d02008-02-04 22:28:20 -08002566 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
Linus Walleijd468bf92013-09-24 11:54:38 +02002567 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
Markus Pargmannced433e2015-08-14 16:11:02 +02002568 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
2569 gpio, gdesc->name ? gdesc->name : "", gdesc->label,
David Brownelld2876d02008-02-04 22:28:20 -08002570 is_out ? "out" : "in ",
2571 chip->get
2572 ? (chip->get(chip, i) ? "hi" : "lo")
Linus Walleijd468bf92013-09-24 11:54:38 +02002573 : "? ",
2574 is_irq ? "IRQ" : " ");
David Brownelld2876d02008-02-04 22:28:20 -08002575 seq_printf(s, "\n");
2576 }
2577}
2578
Thierry Redingf9c4a312012-04-12 13:26:01 +02002579static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
David Brownelld2876d02008-02-04 22:28:20 -08002580{
Grant Likely362432a2013-02-09 09:41:49 +00002581 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02002582 struct gpio_device *gdev = NULL;
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002583 loff_t index = *pos;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002584
2585 s->private = "";
2586
Grant Likely362432a2013-02-09 09:41:49 +00002587 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02002588 list_for_each_entry(gdev, &gpio_devices, list)
Grant Likely362432a2013-02-09 09:41:49 +00002589 if (index-- == 0) {
2590 spin_unlock_irqrestore(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02002591 return gdev;
Grant Likely362432a2013-02-09 09:41:49 +00002592 }
2593 spin_unlock_irqrestore(&gpio_lock, flags);
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002594
2595 return NULL;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002596}
2597
2598static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
2599{
Grant Likely362432a2013-02-09 09:41:49 +00002600 unsigned long flags;
Linus Walleijff2b1352015-10-20 11:10:38 +02002601 struct gpio_device *gdev = v;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002602 void *ret = NULL;
2603
Grant Likely362432a2013-02-09 09:41:49 +00002604 spin_lock_irqsave(&gpio_lock, flags);
Linus Walleijff2b1352015-10-20 11:10:38 +02002605 if (list_is_last(&gdev->list, &gpio_devices))
Alexandre Courbotcb1650d2013-02-03 01:29:27 +09002606 ret = NULL;
2607 else
Linus Walleijff2b1352015-10-20 11:10:38 +02002608 ret = list_entry(gdev->list.next, struct gpio_device, list);
Grant Likely362432a2013-02-09 09:41:49 +00002609 spin_unlock_irqrestore(&gpio_lock, flags);
Thierry Redingf9c4a312012-04-12 13:26:01 +02002610
2611 s->private = "\n";
2612 ++*pos;
2613
2614 return ret;
2615}
2616
2617static void gpiolib_seq_stop(struct seq_file *s, void *v)
2618{
2619}
2620
2621static int gpiolib_seq_show(struct seq_file *s, void *v)
2622{
Linus Walleijff2b1352015-10-20 11:10:38 +02002623 struct gpio_device *gdev = v;
2624 struct gpio_chip *chip = gdev->chip;
2625 struct device *parent;
Thierry Redingf9c4a312012-04-12 13:26:01 +02002626
Linus Walleijff2b1352015-10-20 11:10:38 +02002627 if (!chip) {
2628 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
2629 dev_name(&gdev->dev));
2630 return 0;
2631 }
2632
2633 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
2634 dev_name(&gdev->dev),
2635 chip->base, chip->base + chip->ngpio - 1);
2636 parent = chip->parent;
2637 if (parent)
2638 seq_printf(s, ", parent: %s/%s",
2639 parent->bus ? parent->bus->name : "no-bus",
2640 dev_name(parent));
Thierry Redingf9c4a312012-04-12 13:26:01 +02002641 if (chip->label)
2642 seq_printf(s, ", %s", chip->label);
2643 if (chip->can_sleep)
2644 seq_printf(s, ", can sleep");
2645 seq_printf(s, ":\n");
2646
2647 if (chip->dbg_show)
2648 chip->dbg_show(s, chip);
2649 else
2650 gpiolib_dbg_show(s, chip);
2651
David Brownelld2876d02008-02-04 22:28:20 -08002652 return 0;
2653}
2654
Thierry Redingf9c4a312012-04-12 13:26:01 +02002655static const struct seq_operations gpiolib_seq_ops = {
2656 .start = gpiolib_seq_start,
2657 .next = gpiolib_seq_next,
2658 .stop = gpiolib_seq_stop,
2659 .show = gpiolib_seq_show,
2660};
2661
David Brownelld2876d02008-02-04 22:28:20 -08002662static int gpiolib_open(struct inode *inode, struct file *file)
2663{
Thierry Redingf9c4a312012-04-12 13:26:01 +02002664 return seq_open(file, &gpiolib_seq_ops);
David Brownelld2876d02008-02-04 22:28:20 -08002665}
2666
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002667static const struct file_operations gpiolib_operations = {
Thierry Redingf9c4a312012-04-12 13:26:01 +02002668 .owner = THIS_MODULE,
David Brownelld2876d02008-02-04 22:28:20 -08002669 .open = gpiolib_open,
2670 .read = seq_read,
2671 .llseek = seq_lseek,
Thierry Redingf9c4a312012-04-12 13:26:01 +02002672 .release = seq_release,
David Brownelld2876d02008-02-04 22:28:20 -08002673};
2674
2675static int __init gpiolib_debugfs_init(void)
2676{
2677 /* /sys/kernel/debug/gpio */
2678 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO,
2679 NULL, NULL, &gpiolib_operations);
2680 return 0;
2681}
2682subsys_initcall(gpiolib_debugfs_init);
2683
2684#endif /* DEBUG_FS */