blob: 7a65dad43932c62e3741b4576188630c32374f9b [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;
Rafael J. Wysockice793482015-03-16 23:49:03 +010020struct acpi_device;
21
Mika Westerberg5ccff852014-01-08 12:40:56 +020022/**
Linus Walleijff2b1352015-10-20 11:10:38 +020023 * struct gpio_device - internal state container for GPIO devices
24 * @id: numerical ID number for the GPIO chip
25 * @dev: the GPIO device struct
Linus Walleij3c702e92015-10-21 15:29:53 +020026 * @chrdev: character device for the GPIO device
Linus Walleijafbc4f32016-02-09 13:21:06 +010027 * @mockdev: class device used by the deprecated sysfs interface (may be
28 * NULL)
Linus Walleijff2b1352015-10-20 11:10:38 +020029 * @owner: helps prevent removal of modules exporting active GPIOs
30 * @chip: pointer to the corresponding gpiochip, holding static
31 * data for this device
Linus Walleij1c3cdb12016-02-09 13:51:59 +010032 * @descs: array of ngpio descriptors.
Linus Walleijfdeb8e12016-02-10 10:57:36 +010033 * @ngpio: the number of GPIO lines on this GPIO device, equal to the size
34 * of the @descs array.
35 * @base: GPIO base in the DEPRECATED global Linux GPIO numberspace, assigned
36 * at device creation time.
Linus Walleijdf4878e2016-02-12 14:48:23 +010037 * @label: a descriptive name for the GPIO device, such as the part number
38 * or name of the IP component in a System on Chip.
Linus Walleij43c54ec2016-02-11 11:37:48 +010039 * @data: per-instance data assigned by the driver
Linus Walleijff2b1352015-10-20 11:10:38 +020040 * @list: links gpio_device:s together for traversal
41 *
42 * This state container holds most of the runtime variable data
43 * for a GPIO device and can hold references and live on after the
44 * GPIO chip has been removed, if it is still being used from
45 * userspace.
46 */
47struct gpio_device {
48 int id;
49 struct device dev;
Linus Walleij3c702e92015-10-21 15:29:53 +020050 struct cdev chrdev;
Linus Walleijafbc4f32016-02-09 13:21:06 +010051 struct device *mockdev;
Linus Walleijff2b1352015-10-20 11:10:38 +020052 struct module *owner;
53 struct gpio_chip *chip;
Linus Walleij1c3cdb12016-02-09 13:51:59 +010054 struct gpio_desc *descs;
Linus Walleijfdeb8e12016-02-10 10:57:36 +010055 int base;
56 u16 ngpio;
Bartosz Golaszewski3b469b02017-12-13 12:25:19 +010057 const char *label;
Linus Walleij43c54ec2016-02-11 11:37:48 +010058 void *data;
Linus Walleijff2b1352015-10-20 11:10:38 +020059 struct list_head list;
Linus Walleij20ec3e32016-02-11 11:03:06 +010060
61#ifdef CONFIG_PINCTRL
62 /*
63 * If CONFIG_PINCTRL is enabled, then gpio controllers can optionally
64 * describe the actual pin range which they serve in an SoC. This
65 * information would be used by pinctrl subsystem to configure
66 * corresponding pins for gpio usage.
67 */
68 struct list_head pin_ranges;
69#endif
Linus Walleijff2b1352015-10-20 11:10:38 +020070};
71
72/**
Mika Westerberg5ccff852014-01-08 12:40:56 +020073 * struct acpi_gpio_info - ACPI GPIO specific information
Andy Shevchenko5870cff472017-11-10 15:40:30 +020074 * @adev: reference to ACPI device which consumes GPIO resource
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +030075 * @flags: GPIO initialization flags
Mika Westerberg5ccff852014-01-08 12:40:56 +020076 * @gpioint: if %true this GPIO is of type GpioInt otherwise type is GpioIo
Andy Shevchenko2d3b6db2019-04-10 18:39:21 +030077 * @pin_config: pin bias as provided by ACPI
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;
Andy Shevchenko2d3b6db2019-04-10 18:39:21 +030086 int pin_config;
Christophe RICARD52044722015-12-23 23:25:34 +010087 int polarity;
88 int triggering;
Andy Shevchenkoce0929d2017-11-10 15:40:32 +020089 unsigned int quirks;
Mika Westerberg5ccff852014-01-08 12:40:56 +020090};
91
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +010092/* gpio suffixes used for ACPI and device tree lookup */
Andy Shevchenkob23ec592018-07-09 21:47:27 +030093static __maybe_unused const char * const gpio_suffixes[] = { "gpios", "gpio" };
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +010094
Linus Walleijea713bc2016-10-03 10:09:40 +020095#ifdef CONFIG_OF_GPIO
96struct gpio_desc *of_find_gpio(struct device *dev,
97 const char *con_id,
98 unsigned int idx,
Andy Shevchenkofed70262019-04-10 18:39:16 +030099 unsigned long *lookupflags);
Linus Walleije0852942016-10-03 11:37:24 +0200100struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
101 const char *list_name, int index, enum of_gpio_flags *flags);
Linus Walleijf4c11812016-10-03 10:59:32 +0200102int of_gpiochip_add(struct gpio_chip *gc);
103void of_gpiochip_remove(struct gpio_chip *gc);
Linus Walleijea713bc2016-10-03 10:09:40 +0200104#else
105static inline struct gpio_desc *of_find_gpio(struct device *dev,
106 const char *con_id,
107 unsigned int idx,
Andy Shevchenkofed70262019-04-10 18:39:16 +0300108 unsigned long *lookupflags)
Linus Walleijea713bc2016-10-03 10:09:40 +0200109{
110 return ERR_PTR(-ENOENT);
111}
Linus Walleije0852942016-10-03 11:37:24 +0200112static inline struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
113 const char *list_name, int index, enum of_gpio_flags *flags)
114{
115 return ERR_PTR(-ENOENT);
116}
Linus Walleijf4c11812016-10-03 10:59:32 +0200117static inline int of_gpiochip_add(struct gpio_chip *gc) { return 0; }
118static inline void of_gpiochip_remove(struct gpio_chip *gc) { }
Linus Walleijea713bc2016-10-03 10:09:40 +0200119#endif /* CONFIG_OF_GPIO */
120
Mika Westerberg664e3e52014-01-08 12:40:54 +0200121#ifdef CONFIG_ACPI
122void acpi_gpiochip_add(struct gpio_chip *chip);
123void acpi_gpiochip_remove(struct gpio_chip *chip);
Mika Westerberg5ccff852014-01-08 12:40:56 +0200124
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300125void acpi_gpiochip_request_interrupts(struct gpio_chip *chip);
126void acpi_gpiochip_free_interrupts(struct gpio_chip *chip);
127
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300128int acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags,
Andy Shevchenko5c34b6c2017-11-10 15:40:31 +0200129 struct acpi_gpio_info *info);
Andy Shevchenko606be342019-04-10 18:39:20 +0300130int acpi_gpio_update_gpiod_lookup_flags(unsigned long *lookupflags,
131 struct acpi_gpio_info *info);
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300132
Linus Walleij031ba282016-10-03 10:40:03 +0200133struct gpio_desc *acpi_find_gpio(struct device *dev,
134 const char *con_id,
135 unsigned int idx,
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300136 enum gpiod_flags *dflags,
Andy Shevchenkofed70262019-04-10 18:39:16 +0300137 unsigned long *lookupflags);
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200138struct gpio_desc *acpi_node_get_gpiod(struct fwnode_handle *fwnode,
139 const char *propname, int index,
140 struct acpi_gpio_info *info);
Rojhalat Ibrahim66858522015-02-11 17:27:58 +0100141
142int acpi_gpio_count(struct device *dev, const char *con_id);
Dmitry Torokhov10cf4892015-11-11 11:45:30 -0800143
144bool acpi_can_fallback_to_crs(struct acpi_device *adev, const char *con_id);
Mika Westerberg664e3e52014-01-08 12:40:54 +0200145#else
146static inline void acpi_gpiochip_add(struct gpio_chip *chip) { }
147static inline void acpi_gpiochip_remove(struct gpio_chip *chip) { }
Mika Westerberg5ccff852014-01-08 12:40:56 +0200148
Mika Westerbergafa82fa2014-07-25 09:54:48 +0300149static inline void
150acpi_gpiochip_request_interrupts(struct gpio_chip *chip) { }
151
152static inline void
153acpi_gpiochip_free_interrupts(struct gpio_chip *chip) { }
154
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300155static inline int
Andy Shevchenko5c34b6c2017-11-10 15:40:31 +0200156acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags, struct acpi_gpio_info *info)
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300157{
158 return 0;
159}
Andy Shevchenko606be342019-04-10 18:39:20 +0300160static inline int
161acpi_gpio_update_gpiod_lookup_flags(unsigned long *lookupflags,
162 struct acpi_gpio_info *info)
163{
164 return 0;
165}
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300166
Mika Westerberg5ccff852014-01-08 12:40:56 +0200167static inline struct gpio_desc *
Linus Walleij031ba282016-10-03 10:40:03 +0200168acpi_find_gpio(struct device *dev, const char *con_id,
Andy Shevchenkoa31f5c32017-05-23 20:03:23 +0300169 unsigned int idx, enum gpiod_flags *dflags,
Andy Shevchenkofed70262019-04-10 18:39:16 +0300170 unsigned long *lookupflags)
Mika Westerberg5ccff852014-01-08 12:40:56 +0200171{
Linus Walleij031ba282016-10-03 10:40:03 +0200172 return ERR_PTR(-ENOENT);
Mika Westerberg5ccff852014-01-08 12:40:56 +0200173}
Rafael J. Wysocki504a3372015-08-27 04:42:33 +0200174static inline struct gpio_desc *
175acpi_node_get_gpiod(struct fwnode_handle *fwnode, const char *propname,
176 int index, struct acpi_gpio_info *info)
177{
178 return ERR_PTR(-ENXIO);
179}
Rojhalat Ibrahim66858522015-02-11 17:27:58 +0100180static inline int acpi_gpio_count(struct device *dev, const char *con_id)
181{
182 return -ENODEV;
183}
Dmitry Torokhov10cf4892015-11-11 11:45:30 -0800184
185static inline bool acpi_can_fallback_to_crs(struct acpi_device *adev,
186 const char *con_id)
187{
188 return false;
189}
Mika Westerberg664e3e52014-01-08 12:40:54 +0200190#endif
191
Janusz Krzysztofikbf9346f2018-09-05 23:50:06 +0200192struct gpio_array {
193 struct gpio_desc **desc;
194 unsigned int size;
195 struct gpio_chip *chip;
196 unsigned long *get_mask;
197 unsigned long *set_mask;
198 unsigned long invert_mask[];
199};
200
Alexandre Courbot1bd6b602014-07-22 16:17:41 +0900201struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip, u16 hwnum);
Lukas Wunnereec1d562017-10-12 12:40:10 +0200202int gpiod_get_array_value_complex(bool raw, bool can_sleep,
203 unsigned int array_size,
204 struct gpio_desc **desc_array,
Janusz Krzysztofik77588c12018-09-05 23:50:07 +0200205 struct gpio_array *array_info,
Janusz Krzysztofikb9762be2018-09-05 23:50:05 +0200206 unsigned long *value_bitmap);
Laura Abbott30277432018-05-21 10:57:07 -0700207int gpiod_set_array_value_complex(bool raw, bool can_sleep,
Geert Uytterhoeven3c940662018-09-27 13:38:10 +0200208 unsigned int array_size,
209 struct gpio_desc **desc_array,
210 struct gpio_array *array_info,
211 unsigned long *value_bitmap);
Alexandre Courbot1bd6b602014-07-22 16:17:41 +0900212
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900213extern struct spinlock gpio_lock;
Linus Walleijff2b1352015-10-20 11:10:38 +0200214extern struct list_head gpio_devices;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900215
216struct gpio_desc {
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100217 struct gpio_device *gdev;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900218 unsigned long flags;
219/* flag symbols are bit numbers */
220#define FLAG_REQUESTED 0
221#define FLAG_IS_OUT 1
222#define FLAG_EXPORT 2 /* protected by sysfs_lock */
223#define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900224#define FLAG_ACTIVE_LOW 6 /* value has active low */
225#define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
226#define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
227#define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */
Hans Verkuil4e9439d2018-09-08 11:23:16 +0200228#define FLAG_IRQ_IS_ENABLED 10 /* GPIO is connected to an enabled IRQ */
Benoit Parrotf625d462015-02-02 11:44:44 -0600229#define FLAG_IS_HOGGED 11 /* GPIO is hogged */
Andrew Jefferye10f72b2017-11-30 14:25:24 +1030230#define FLAG_TRANSITORY 12 /* GPIO may lose value in sleep or reset */
Thomas Petazzonid4499912019-02-07 17:28:58 +0100231#define FLAG_PULL_UP 13 /* GPIO has pull up enabled */
232#define FLAG_PULL_DOWN 14 /* GPIO has pull down enabled */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900233
Markus Pargmannc0017ed2015-08-14 16:10:59 +0200234 /* Connection label */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900235 const char *label;
Markus Pargmannc0017ed2015-08-14 16:10:59 +0200236 /* Name of the GPIO */
237 const char *name;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900238};
239
240int gpiod_request(struct gpio_desc *desc, const char *label);
241void gpiod_free(struct gpio_desc *desc);
Andy Shevchenkoc29fd9e2017-05-23 20:03:16 +0300242int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
243 unsigned long lflags, enum gpiod_flags dflags);
Benoit Parrotf625d462015-02-02 11:44:44 -0600244int gpiod_hog(struct gpio_desc *desc, const char *name,
245 unsigned long lflags, enum gpiod_flags dflags);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900246
247/*
248 * Return the GPIO number of the passed descriptor relative to its chip
249 */
Masahiro Yamada4a5c8862017-07-11 23:41:43 +0900250static inline int gpio_chip_hwgpio(const struct gpio_desc *desc)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900251{
Linus Walleijfdeb8e12016-02-10 10:57:36 +0100252 return desc - &desc->gdev->descs[0];
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900253}
254
255/* With descriptor prefix */
256
257#define gpiod_emerg(desc, fmt, ...) \
258 pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
259 ##__VA_ARGS__)
260#define gpiod_crit(desc, fmt, ...) \
261 pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
262 ##__VA_ARGS__)
263#define gpiod_err(desc, fmt, ...) \
264 pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
265 ##__VA_ARGS__)
266#define gpiod_warn(desc, fmt, ...) \
267 pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
268 ##__VA_ARGS__)
269#define gpiod_info(desc, fmt, ...) \
270 pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
271 ##__VA_ARGS__)
272#define gpiod_dbg(desc, fmt, ...) \
273 pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
274 ##__VA_ARGS__)
275
276/* With chip prefix */
277
278#define chip_emerg(chip, fmt, ...) \
Linus Walleij34ffd852015-10-20 11:31:54 +0200279 dev_emerg(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900280#define chip_crit(chip, fmt, ...) \
Linus Walleij34ffd852015-10-20 11:31:54 +0200281 dev_crit(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900282#define chip_err(chip, fmt, ...) \
Linus Walleij34ffd852015-10-20 11:31:54 +0200283 dev_err(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900284#define chip_warn(chip, fmt, ...) \
Linus Walleij34ffd852015-10-20 11:31:54 +0200285 dev_warn(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900286#define chip_info(chip, fmt, ...) \
Linus Walleij34ffd852015-10-20 11:31:54 +0200287 dev_info(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900288#define chip_dbg(chip, fmt, ...) \
Linus Walleij34ffd852015-10-20 11:31:54 +0200289 dev_dbg(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900290
291#ifdef CONFIG_GPIO_SYSFS
292
Linus Walleijafbc4f32016-02-09 13:21:06 +0100293int gpiochip_sysfs_register(struct gpio_device *gdev);
294void gpiochip_sysfs_unregister(struct gpio_device *gdev);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900295
296#else
297
Linus Walleijafbc4f32016-02-09 13:21:06 +0100298static inline int gpiochip_sysfs_register(struct gpio_device *gdev)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900299{
300 return 0;
301}
302
Linus Walleijafbc4f32016-02-09 13:21:06 +0100303static inline void gpiochip_sysfs_unregister(struct gpio_device *gdev)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900304{
305}
306
307#endif /* CONFIG_GPIO_SYSFS */
308
Mika Westerberg664e3e52014-01-08 12:40:54 +0200309#endif /* GPIOLIB_H */