Merge tag 'renesas-fixes-for-v5.1' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-block.git] / drivers / gpio / gpiolib.h
CommitLineData
dae5f0af 1/* SPDX-License-Identifier: GPL-2.0 */
664e3e5a
MW
2/*
3 * Internal GPIO functions.
4 *
5 * Copyright (C) 2013, Intel Corporation
6 * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
664e3e5a
MW
7 */
8
9#ifndef GPIOLIB_H
10#define GPIOLIB_H
11
ff2b1359 12#include <linux/gpio/driver.h>
63f2dc0a 13#include <linux/gpio/consumer.h> /* for enum gpiod_flags */
5ccff852
MW
14#include <linux/err.h>
15#include <linux/device.h>
ff2b1359
LW
16#include <linux/module.h>
17#include <linux/cdev.h>
5ccff852 18
f01d9075 19enum of_gpio_flags;
ea713bc4 20enum gpio_lookup_flags;
ce793486
RW
21struct acpi_device;
22
ff2b1359
LW
23/**
24 * struct gpio_device - internal state container for GPIO devices
25 * @id: numerical ID number for the GPIO chip
26 * @dev: the GPIO device struct
3c702e99 27 * @chrdev: character device for the GPIO device
afbc4f31
LW
28 * @mockdev: class device used by the deprecated sysfs interface (may be
29 * NULL)
ff2b1359
LW
30 * @owner: helps prevent removal of modules exporting active GPIOs
31 * @chip: pointer to the corresponding gpiochip, holding static
32 * data for this device
1c3cdb18 33 * @descs: array of ngpio descriptors.
fdeb8e15
LW
34 * @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.
df4878e9
LW
38 * @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.
43c54eca 40 * @data: per-instance data assigned by the driver
ff2b1359
LW
41 * @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;
3c702e99 51 struct cdev chrdev;
afbc4f31 52 struct device *mockdev;
ff2b1359
LW
53 struct module *owner;
54 struct gpio_chip *chip;
1c3cdb18 55 struct gpio_desc *descs;
fdeb8e15
LW
56 int base;
57 u16 ngpio;
3b469b0a 58 const char *label;
43c54eca 59 void *data;
ff2b1359 60 struct list_head list;
20ec3e39
LW
61
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
ff2b1359
LW
71};
72
5ccff852
MW
73/**
74 * struct acpi_gpio_info - ACPI GPIO specific information
5870cff4 75 * @adev: reference to ACPI device which consumes GPIO resource
a31f5c3a 76 * @flags: GPIO initialization flags
5ccff852 77 * @gpioint: if %true this GPIO is of type GpioInt otherwise type is GpioIo
e567c35f
AS
78 * @polarity: interrupt polarity as provided by ACPI
79 * @triggering: triggering type as provided by ACPI
ce0929d2 80 * @quirks: Linux specific quirks as provided by struct acpi_gpio_mapping
5ccff852
MW
81 */
82struct acpi_gpio_info {
5870cff4 83 struct acpi_device *adev;
a31f5c3a 84 enum gpiod_flags flags;
5ccff852 85 bool gpioint;
52044723
CR
86 int polarity;
87 int triggering;
ce0929d2 88 unsigned int quirks;
5ccff852
MW
89};
90
7f2e553a 91/* gpio suffixes used for ACPI and device tree lookup */
b23ec599 92static __maybe_unused const char * const gpio_suffixes[] = { "gpios", "gpio" };
7f2e553a 93
ea713bc4
LW
94#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);
e0852940
LW
99struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
100 const char *list_name, int index, enum of_gpio_flags *flags);
f4c1181f
LW
101int of_gpiochip_add(struct gpio_chip *gc);
102void of_gpiochip_remove(struct gpio_chip *gc);
ea713bc4
LW
103#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}
e0852940
LW
111static 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}
f4c1181f
LW
116static inline int of_gpiochip_add(struct gpio_chip *gc) { return 0; }
117static inline void of_gpiochip_remove(struct gpio_chip *gc) { }
ea713bc4
LW
118#endif /* CONFIG_OF_GPIO */
119
664e3e5a
MW
120#ifdef CONFIG_ACPI
121void acpi_gpiochip_add(struct gpio_chip *chip);
122void acpi_gpiochip_remove(struct gpio_chip *chip);
5ccff852 123
afa82fab
MW
124void acpi_gpiochip_request_interrupts(struct gpio_chip *chip);
125void acpi_gpiochip_free_interrupts(struct gpio_chip *chip);
126
a31f5c3a 127int acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags,
5c34b6c1 128 struct acpi_gpio_info *info);
a31f5c3a 129
031ba28a
LW
130struct gpio_desc *acpi_find_gpio(struct device *dev,
131 const char *con_id,
132 unsigned int idx,
a31f5c3a 133 enum gpiod_flags *dflags,
031ba28a 134 enum gpio_lookup_flags *lookupflags);
504a3374
RW
135struct gpio_desc *acpi_node_get_gpiod(struct fwnode_handle *fwnode,
136 const char *propname, int index,
137 struct acpi_gpio_info *info);
66858527
RI
138
139int acpi_gpio_count(struct device *dev, const char *con_id);
10cf4899
DT
140
141bool acpi_can_fallback_to_crs(struct acpi_device *adev, const char *con_id);
664e3e5a
MW
142#else
143static inline void acpi_gpiochip_add(struct gpio_chip *chip) { }
144static inline void acpi_gpiochip_remove(struct gpio_chip *chip) { }
5ccff852 145
afa82fab
MW
146static inline void
147acpi_gpiochip_request_interrupts(struct gpio_chip *chip) { }
148
149static inline void
150acpi_gpiochip_free_interrupts(struct gpio_chip *chip) { }
151
a31f5c3a 152static inline int
5c34b6c1 153acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags, struct acpi_gpio_info *info)
a31f5c3a
AS
154{
155 return 0;
156}
157
5ccff852 158static inline struct gpio_desc *
031ba28a 159acpi_find_gpio(struct device *dev, const char *con_id,
a31f5c3a 160 unsigned int idx, enum gpiod_flags *dflags,
031ba28a 161 enum gpio_lookup_flags *lookupflags)
5ccff852 162{
031ba28a 163 return ERR_PTR(-ENOENT);
5ccff852 164}
504a3374
RW
165static 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}
66858527
RI
171static inline int acpi_gpio_count(struct device *dev, const char *con_id)
172{
173 return -ENODEV;
174}
10cf4899
DT
175
176static inline bool acpi_can_fallback_to_crs(struct acpi_device *adev,
177 const char *con_id)
178{
179 return false;
180}
664e3e5a
MW
181#endif
182
bf9346f5
JK
183struct 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
1bd6b601 192struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip, u16 hwnum);
eec1d566
LW
193int gpiod_get_array_value_complex(bool raw, bool can_sleep,
194 unsigned int array_size,
195 struct gpio_desc **desc_array,
77588c14 196 struct gpio_array *array_info,
b9762beb 197 unsigned long *value_bitmap);
3027743f 198int gpiod_set_array_value_complex(bool raw, bool can_sleep,
3c940660
GU
199 unsigned int array_size,
200 struct gpio_desc **desc_array,
201 struct gpio_array *array_info,
202 unsigned long *value_bitmap);
1bd6b601 203
0eb4c6c2 204extern struct spinlock gpio_lock;
ff2b1359 205extern struct list_head gpio_devices;
0eb4c6c2
AC
206
207struct gpio_desc {
fdeb8e15 208 struct gpio_device *gdev;
0eb4c6c2
AC
209 unsigned long flags;
210/* flag symbols are bit numbers */
211#define FLAG_REQUESTED 0
212#define FLAG_IS_OUT 1
213#define FLAG_EXPORT 2 /* protected by sysfs_lock */
214#define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
0eb4c6c2
AC
215#define FLAG_ACTIVE_LOW 6 /* value has active low */
216#define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
217#define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
218#define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */
4e9439dd 219#define FLAG_IRQ_IS_ENABLED 10 /* GPIO is connected to an enabled IRQ */
f625d460 220#define FLAG_IS_HOGGED 11 /* GPIO is hogged */
e10f72bf 221#define FLAG_TRANSITORY 12 /* GPIO may lose value in sleep or reset */
d449991c
TP
222#define FLAG_PULL_UP 13 /* GPIO has pull up enabled */
223#define FLAG_PULL_DOWN 14 /* GPIO has pull down enabled */
0eb4c6c2 224
c0017ed7 225 /* Connection label */
0eb4c6c2 226 const char *label;
c0017ed7
MP
227 /* Name of the GPIO */
228 const char *name;
0eb4c6c2
AC
229};
230
231int gpiod_request(struct gpio_desc *desc, const char *label);
232void gpiod_free(struct gpio_desc *desc);
c29fd9eb
AS
233int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
234 unsigned long lflags, enum gpiod_flags dflags);
f625d460
BP
235int gpiod_hog(struct gpio_desc *desc, const char *name,
236 unsigned long lflags, enum gpiod_flags dflags);
0eb4c6c2
AC
237
238/*
239 * Return the GPIO number of the passed descriptor relative to its chip
240 */
4a5c886e 241static inline int gpio_chip_hwgpio(const struct gpio_desc *desc)
0eb4c6c2 242{
fdeb8e15 243 return desc - &desc->gdev->descs[0];
0eb4c6c2
AC
244}
245
82270335
CL
246void devprop_gpiochip_set_names(struct gpio_chip *chip,
247 const struct fwnode_handle *fwnode);
9427ecbe 248
0eb4c6c2
AC
249/* With descriptor prefix */
250
251#define gpiod_emerg(desc, fmt, ...) \
252 pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
253 ##__VA_ARGS__)
254#define gpiod_crit(desc, fmt, ...) \
255 pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
256 ##__VA_ARGS__)
257#define gpiod_err(desc, fmt, ...) \
258 pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
259 ##__VA_ARGS__)
260#define gpiod_warn(desc, fmt, ...) \
261 pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
262 ##__VA_ARGS__)
263#define gpiod_info(desc, fmt, ...) \
264 pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
265 ##__VA_ARGS__)
266#define gpiod_dbg(desc, fmt, ...) \
267 pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
268 ##__VA_ARGS__)
269
270/* With chip prefix */
271
272#define chip_emerg(chip, fmt, ...) \
34ffd85d 273 dev_emerg(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
0eb4c6c2 274#define chip_crit(chip, fmt, ...) \
34ffd85d 275 dev_crit(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
0eb4c6c2 276#define chip_err(chip, fmt, ...) \
34ffd85d 277 dev_err(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
0eb4c6c2 278#define chip_warn(chip, fmt, ...) \
34ffd85d 279 dev_warn(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
0eb4c6c2 280#define chip_info(chip, fmt, ...) \
34ffd85d 281 dev_info(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
0eb4c6c2 282#define chip_dbg(chip, fmt, ...) \
34ffd85d 283 dev_dbg(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
0eb4c6c2
AC
284
285#ifdef CONFIG_GPIO_SYSFS
286
afbc4f31
LW
287int gpiochip_sysfs_register(struct gpio_device *gdev);
288void gpiochip_sysfs_unregister(struct gpio_device *gdev);
0eb4c6c2
AC
289
290#else
291
afbc4f31 292static inline int gpiochip_sysfs_register(struct gpio_device *gdev)
0eb4c6c2
AC
293{
294 return 0;
295}
296
afbc4f31 297static inline void gpiochip_sysfs_unregister(struct gpio_device *gdev)
0eb4c6c2
AC
298{
299}
300
301#endif /* CONFIG_GPIO_SYSFS */
302
664e3e5a 303#endif /* GPIOLIB_H */