blob: 087d865286a0c8ffe620c5abf33a14778a3f5e36 [file] [log] [blame]
Linus Walleijdae5f0a2018-09-25 09:08:48 +02001/* SPDX-License-Identifier: GPL-2.0 */
Mika Westerberg664e3e52014-01-08 12:40:54 +02002/*
3 * Internal GPIO functions.
4 *
5 * Copyright (C) 2013, Intel Corporation
6 * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
Mika Westerberg664e3e52014-01-08 12:40:54 +02007 */
8
9#ifndef GPIOLIB_H
10#define GPIOLIB_H
11
Linus Walleijff2b1352015-10-20 11:10:38 +020012#include <linux/gpio/driver.h>
Linus Walleij63f2dc02018-02-08 17:57:43 +010013#include <linux/gpio/consumer.h> /* for enum gpiod_flags */
Mika Westerberg5ccff852014-01-08 12:40:56 +020014#include <linux/err.h>
15#include <linux/device.h>
Linus Walleijff2b1352015-10-20 11:10:38 +020016#include <linux/module.h>
17#include <linux/cdev.h>
Mika Westerberg5ccff852014-01-08 12:40:56 +020018
Alexandre Courbotf01d9072014-05-17 14:54:50 +090019enum of_gpio_flags;
Linus Walleijea713bc2016-10-03 10:09:40 +020020enum gpio_lookup_flags;
Rafael J. Wysockice793482015-03-16 23:49:03 +010021struct acpi_device;
22
Mika Westerberg5ccff852014-01-08 12:40:56 +020023/**
Linus Walleijff2b1352015-10-20 11:10:38 +020024 * struct gpio_device - internal state container for GPIO devices
25 * @id: numerical ID number for the GPIO chip
26 * @dev: the GPIO device struct
Linus Walleij3c702e92015-10-21 15:29:53 +020027 * @chrdev: character device for the GPIO device
Linus Walleijafbc4f32016-02-09 13:21:06 +010028 * @mockdev: class device used by the deprecated sysfs interface (may be
29 * NULL)
Linus Walleijff2b1352015-10-20 11:10:38 +020030 * @owner: helps prevent removal of modules exporting active GPIOs
31 * @chip: pointer to the corresponding gpiochip, holding static
32 * data for this device
Linus Walleij1c3cdb12016-02-09 13:51:59 +010033 * @descs: array of ngpio descriptors.
Linus Walleijfdeb8e12016-02-10 10:57:36 +010034 * @ngpio: the number of GPIO lines on this GPIO device, equal to the size
35 * of the @descs array.
36 * @base: GPIO base in the DEPRECATED global Linux GPIO numberspace, assigned
37 * at device creation time.
Linus Walleijdf4878e2016-02-12 14:48:23 +010038 * @label: a descriptive name for the GPIO device, such as the part number
39 * or name of the IP component in a System on Chip.
Linus Walleij43c54ec2016-02-11 11:37:48 +010040 * @data: per-instance data assigned by the driver
Linus Walleijff2b1352015-10-20 11:10:38 +020041 * @list: links gpio_device:s together for traversal
42 *
43 * This state container holds most of the runtime variable data
44 * for a GPIO device and can hold references and live on after the
45 * GPIO chip has been removed, if it is still being used from
46 * userspace.
47 */
48struct gpio_device {
49 int id;
50 struct device dev;
Linus Walleij3c702e92015-10-21 15:29:53 +020051 struct cdev chrdev;
Linus Walleijafbc4f32016-02-09 13:21:06 +010052 struct device *mockdev;
Linus Walleijff2b1352015-10-20 11:10:38 +020053 struct module *owner;
54 struct gpio_chip *chip;
Linus Walleij1c3cdb12016-02-09 13:51:59 +010055 struct gpio_desc *descs;
Linus Walleijfdeb8e12016-02-10 10:57:36 +010056 int base;
57 u16 ngpio;
Bartosz Golaszewski3b469b02017-12-13 12:25:19 +010058 const char *label;
Linus Walleij43c54ec2016-02-11 11:37:48 +010059 void *data;
Linus Walleijff2b1352015-10-20 11:10:38 +020060 struct list_head list;
Linus Walleij20ec3e32016-02-11 11:03:06 +010061
62#ifdef CONFIG_PINCTRL
63 /*
64 * If CONFIG_PINCTRL is enabled, then gpio controllers can optionally
65 * describe the actual pin range which they serve in an SoC. This
66 * information would be used by pinctrl subsystem to configure
67 * corresponding pins for gpio usage.
68 */
69 struct list_head pin_ranges;
70#endif
Linus Walleijff2b1352015-10-20 11:10:38 +020071};
72
73/**
Mika Westerberg5ccff852014-01-08 12:40:56 +020074 * struct acpi_gpio_info - ACPI GPIO specific information
Andy Shevchenko5870cff472017-11-10 15:40:30 +020075 * @adev: reference to ACPI device which consumes GPIO resource
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +030076 * @flags: GPIO initialization flags
Mika Westerberg5ccff852014-01-08 12:40:56 +020077 * @gpioint: if %true this GPIO is of type GpioInt otherwise type is GpioIo
Andy Shevchenkoe567c352017-01-03 19:01:18 +020078 * @polarity: interrupt polarity as provided by ACPI
79 * @triggering: triggering type as provided by ACPI
Andy Shevchenkoce0929d2017-11-10 15:40:32 +020080 * @quirks: Linux specific quirks as provided by struct acpi_gpio_mapping
Mika Westerberg5ccff852014-01-08 12:40:56 +020081 */
82struct acpi_gpio_info {
Andy Shevchenko5870cff472017-11-10 15:40:30 +020083 struct acpi_device *adev;
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +030084 enum gpiod_flags flags;
Mika Westerberg5ccff852014-01-08 12:40:56 +020085 bool gpioint;
Christophe RICARD52044722015-12-23 23:25:34 +010086 int polarity;
87 int triggering;
Andy Shevchenkoce0929d2017-11-10 15:40:32 +020088 unsigned int quirks;
Mika Westerberg5ccff852014-01-08 12:40:56 +020089};
90
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +010091/* gpio suffixes used for ACPI and device tree lookup */
Andy Shevchenkob23ec592018-07-09 21:47:27 +030092static __maybe_unused const char * const gpio_suffixes[] = { "gpios", "gpio" };
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +010093
Linus Walleijea713bc2016-10-03 10:09:40 +020094#ifdef CONFIG_OF_GPIO
95struct gpio_desc *of_find_gpio(struct device *dev,
96 const char *con_id,
97 unsigned int idx,
98 enum gpio_lookup_flags *flags);
Linus Walleije0852942016-10-03 11:37:24 +020099struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
100 const char *list_name, int index, enum of_gpio_flags *flags);
Linus Walleijf4c11812016-10-03 10:59:32 +0200101int of_gpiochip_add(struct gpio_chip *gc);
102void of_gpiochip_remove(struct gpio_chip *gc);
Linus Walleijea713bc2016-10-03 10:09:40 +0200103#else
104static inline struct gpio_desc *of_find_gpio(struct device *dev,
105 const char *con_id,
106 unsigned int idx,
107 enum gpio_lookup_flags *flags)
108{
109 return ERR_PTR(-ENOENT);
110}
Linus Walleije0852942016-10-03 11:37:24 +0200111static inline struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
112 const char *list_name, int index, enum of_gpio_flags *flags)
113{
114 return ERR_PTR(-ENOENT);
115}
Linus Walleijf4c11812016-10-03 10:59:32 +0200116static inline int of_gpiochip_add(struct gpio_chip *gc) { return 0; }
117static inline void of_gpiochip_remove(struct gpio_chip *gc) { }
Linus Walleijea713bc2016-10-03 10:09:40 +0200118#endif /* CONFIG_OF_GPIO */
119
Mika Westerberg664e3e52014-01-08 12:40:54 +0200120#ifdef CONFIG_ACPI
121void acpi_gpiochip_add(struct gpio_chip *chip);
122void acpi_gpiochip_remove(struct gpio_chip *chip);
Mika Westerberg5ccff852014-01-08 12:40:56 +0200123
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300124void acpi_gpiochip_request_interrupts(struct gpio_chip *chip);
125void acpi_gpiochip_free_interrupts(struct gpio_chip *chip);
126
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300127int acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags,
Andy Shevchenko5c34b6c2017-11-10 15:40:31 +0200128 struct acpi_gpio_info *info);
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300129
Linus Walleij031ba282016-10-03 10:40:03 +0200130struct gpio_desc *acpi_find_gpio(struct device *dev,
131 const char *con_id,
132 unsigned int idx,
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300133 enum gpiod_flags *dflags,
Linus Walleij031ba282016-10-03 10:40:03 +0200134 enum gpio_lookup_flags *lookupflags);
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200135struct gpio_desc *acpi_node_get_gpiod(struct fwnode_handle *fwnode,
136 const char *propname, int index,
137 struct acpi_gpio_info *info);
Rojhalat Ibrahim66858522015-02-11 17:27:58 +0100138
139int acpi_gpio_count(struct device *dev, const char *con_id);
Dmitry Torokhov10cf4892015-11-11 11:45:30 -0800140
141bool acpi_can_fallback_to_crs(struct acpi_device *adev, const char *con_id);
Mika Westerberg664e3e52014-01-08 12:40:54 +0200142#else
143static inline void acpi_gpiochip_add(struct gpio_chip *chip) { }
144static inline void acpi_gpiochip_remove(struct gpio_chip *chip) { }
Mika Westerberg5ccff852014-01-08 12:40:56 +0200145
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300146static inline void
147acpi_gpiochip_request_interrupts(struct gpio_chip *chip) { }
148
149static inline void
150acpi_gpiochip_free_interrupts(struct gpio_chip *chip) { }
151
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300152static inline int
Andy Shevchenko5c34b6c2017-11-10 15:40:31 +0200153acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags, struct acpi_gpio_info *info)
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300154{
155 return 0;
156}
157
Mika Westerberg5ccff852014-01-08 12:40:56 +0200158static inline struct gpio_desc *
Linus Walleij031ba282016-10-03 10:40:03 +0200159acpi_find_gpio(struct device *dev, const char *con_id,
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300160 unsigned int idx, enum gpiod_flags *dflags,
Linus Walleij031ba282016-10-03 10:40:03 +0200161 enum gpio_lookup_flags *lookupflags)
Mika Westerberg5ccff852014-01-08 12:40:56 +0200162{
Linus Walleij031ba282016-10-03 10:40:03 +0200163 return ERR_PTR(-ENOENT);
Mika Westerberg5ccff852014-01-08 12:40:56 +0200164}
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200165static inline struct gpio_desc *
166acpi_node_get_gpiod(struct fwnode_handle *fwnode, const char *propname,
167 int index, struct acpi_gpio_info *info)
168{
169 return ERR_PTR(-ENXIO);
170}
Rojhalat Ibrahim66858522015-02-11 17:27:58 +0100171static inline int acpi_gpio_count(struct device *dev, const char *con_id)
172{
173 return -ENODEV;
174}
Dmitry Torokhov10cf4892015-11-11 11:45:30 -0800175
176static inline bool acpi_can_fallback_to_crs(struct acpi_device *adev,
177 const char *con_id)
178{
179 return false;
180}
Mika Westerberg664e3e52014-01-08 12:40:54 +0200181#endif
182
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +0200183struct gpio_array {
184 struct gpio_desc **desc;
185 unsigned int size;
186 struct gpio_chip *chip;
187 unsigned long *get_mask;
188 unsigned long *set_mask;
189 unsigned long invert_mask[];
190};
191
Alexandre Courbot1bd6b602014-07-22 16:17:41 +0900192struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip, u16 hwnum);
Lukas Wunnereec1d562017-10-12 12:40:10 +0200193int gpiod_get_array_value_complex(bool raw, bool can_sleep,
194 unsigned int array_size,
195 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +0200196 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +0200197 unsigned long *value_bitmap);
Laura Abbott30277432018-05-21 10:57:07 -0700198int gpiod_set_array_value_complex(bool raw, bool can_sleep,
Geert Uytterhoeven3c940662018-09-27 13:38:10 +0200199 unsigned int array_size,
200 struct gpio_desc **desc_array,
201 struct gpio_array *array_info,
202 unsigned long *value_bitmap);
Alexandre Courbot1bd6b602014-07-22 16:17:41 +0900203
Linus Walleij92542ed2017-12-29 22:52:02 +0100204/* This is just passed between gpiolib and devres */
205struct gpio_desc *gpiod_get_from_of_node(struct device_node *node,
206 const char *propname, int index,
207 enum gpiod_flags dflags,
208 const char *label);
209
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900210extern struct spinlock gpio_lock;
Linus Walleijff2b1352015-10-20 11:10:38 +0200211extern struct list_head gpio_devices;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900212
213struct gpio_desc {
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100214 struct gpio_device *gdev;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900215 unsigned long flags;
216/* flag symbols are bit numbers */
217#define FLAG_REQUESTED 0
218#define FLAG_IS_OUT 1
219#define FLAG_EXPORT 2 /* protected by sysfs_lock */
220#define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900221#define FLAG_ACTIVE_LOW 6 /* value has active low */
222#define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
223#define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
224#define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */
Hans Verkuil4e9439d2018-09-08 11:23:16 +0200225#define FLAG_IRQ_IS_ENABLED 10 /* GPIO is connected to an enabled IRQ */
Benoit Parrotf625d462015-02-02 11:44:44 -0600226#define FLAG_IS_HOGGED 11 /* GPIO is hogged */
Andrew Jefferye10f72b2017-11-30 14:25:24 +1030227#define FLAG_TRANSITORY 12 /* GPIO may lose value in sleep or reset */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900228
Markus Pargmannc0017ed2015-08-14 16:10:59 +0200229 /* Connection label */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900230 const char *label;
Markus Pargmannc0017ed2015-08-14 16:10:59 +0200231 /* Name of the GPIO */
232 const char *name;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900233};
234
235int gpiod_request(struct gpio_desc *desc, const char *label);
236void gpiod_free(struct gpio_desc *desc);
Andy Shevchenkoc29fd9e2017-05-23 20:03:16 +0300237int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
238 unsigned long lflags, enum gpiod_flags dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -0600239int gpiod_hog(struct gpio_desc *desc, const char *name,
240 unsigned long lflags, enum gpiod_flags dflags);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900241
242/*
243 * Return the GPIO number of the passed descriptor relative to its chip
244 */
Masahiro Yamada4a5c8862017-07-11 23:41:43 +0900245static inline int gpio_chip_hwgpio(const struct gpio_desc *desc)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900246{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100247 return desc - &desc->gdev->descs[0];
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900248}
249
Christophe Leroy82270332017-12-15 15:02:33 +0100250void devprop_gpiochip_set_names(struct gpio_chip *chip,
251 const struct fwnode_handle *fwnode);
Mika Westerberg9427ecb2016-10-21 17:21:31 +0300252
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900253/* With descriptor prefix */
254
255#define gpiod_emerg(desc, fmt, ...) \
256 pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
257 ##__VA_ARGS__)
258#define gpiod_crit(desc, fmt, ...) \
259 pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
260 ##__VA_ARGS__)
261#define gpiod_err(desc, fmt, ...) \
262 pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
263 ##__VA_ARGS__)
264#define gpiod_warn(desc, fmt, ...) \
265 pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
266 ##__VA_ARGS__)
267#define gpiod_info(desc, fmt, ...) \
268 pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
269 ##__VA_ARGS__)
270#define gpiod_dbg(desc, fmt, ...) \
271 pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
272 ##__VA_ARGS__)
273
274/* With chip prefix */
275
276#define chip_emerg(chip, fmt, ...) \
Linus Walleij34ffd852015-10-20 11:31:54 +0200277 dev_emerg(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900278#define chip_crit(chip, fmt, ...) \
Linus Walleij34ffd852015-10-20 11:31:54 +0200279 dev_crit(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900280#define chip_err(chip, fmt, ...) \
Linus Walleij34ffd852015-10-20 11:31:54 +0200281 dev_err(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900282#define chip_warn(chip, fmt, ...) \
Linus Walleij34ffd852015-10-20 11:31:54 +0200283 dev_warn(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900284#define chip_info(chip, fmt, ...) \
Linus Walleij34ffd852015-10-20 11:31:54 +0200285 dev_info(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900286#define chip_dbg(chip, fmt, ...) \
Linus Walleij34ffd852015-10-20 11:31:54 +0200287 dev_dbg(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900288
289#ifdef CONFIG_GPIO_SYSFS
290
Linus Walleijafbc4f32016-02-09 13:21:06 +0100291int gpiochip_sysfs_register(struct gpio_device *gdev);
292void gpiochip_sysfs_unregister(struct gpio_device *gdev);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900293
294#else
295
Linus Walleijafbc4f32016-02-09 13:21:06 +0100296static inline int gpiochip_sysfs_register(struct gpio_device *gdev)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900297{
298 return 0;
299}
300
Linus Walleijafbc4f32016-02-09 13:21:06 +0100301static inline void gpiochip_sysfs_unregister(struct gpio_device *gdev)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900302{
303}
304
305#endif /* CONFIG_GPIO_SYSFS */
306
Mika Westerberg664e3e52014-01-08 12:40:54 +0200307#endif /* GPIOLIB_H */