]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/gpio/gpiolib.h
gpio: make the gpiochip a real device
[mirror_ubuntu-bionic-kernel.git] / drivers / gpio / gpiolib.h
1 /*
2 * Internal GPIO functions.
3 *
4 * Copyright (C) 2013, Intel Corporation
5 * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12 #ifndef GPIOLIB_H
13 #define GPIOLIB_H
14
15 #include <linux/gpio/driver.h>
16 #include <linux/err.h>
17 #include <linux/device.h>
18 #include <linux/module.h>
19 #include <linux/cdev.h>
20
21 enum of_gpio_flags;
22 enum gpiod_flags;
23 struct acpi_device;
24
25 /**
26 * struct gpio_device - internal state container for GPIO devices
27 * @id: numerical ID number for the GPIO chip
28 * @dev: the GPIO device struct
29 * @owner: helps prevent removal of modules exporting active GPIOs
30 * @chip: pointer to the corresponding gpiochip, holding static
31 * data for this device
32 * @list: links gpio_device:s together for traversal
33 *
34 * This state container holds most of the runtime variable data
35 * for a GPIO device and can hold references and live on after the
36 * GPIO chip has been removed, if it is still being used from
37 * userspace.
38 */
39 struct gpio_device {
40 int id;
41 struct device dev;
42 struct module *owner;
43 struct gpio_chip *chip;
44 struct list_head list;
45 };
46
47 /**
48 * struct acpi_gpio_info - ACPI GPIO specific information
49 * @gpioint: if %true this GPIO is of type GpioInt otherwise type is GpioIo
50 * @active_low: in case of @gpioint, the pin is active low
51 */
52 struct acpi_gpio_info {
53 bool gpioint;
54 int polarity;
55 int triggering;
56 };
57
58 /* gpio suffixes used for ACPI and device tree lookup */
59 static const char * const gpio_suffixes[] = { "gpios", "gpio" };
60
61 #ifdef CONFIG_ACPI
62 void acpi_gpiochip_add(struct gpio_chip *chip);
63 void acpi_gpiochip_remove(struct gpio_chip *chip);
64
65 void acpi_gpiochip_request_interrupts(struct gpio_chip *chip);
66 void acpi_gpiochip_free_interrupts(struct gpio_chip *chip);
67
68 struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev,
69 const char *propname, int index,
70 struct acpi_gpio_info *info);
71 struct gpio_desc *acpi_node_get_gpiod(struct fwnode_handle *fwnode,
72 const char *propname, int index,
73 struct acpi_gpio_info *info);
74
75 int acpi_gpio_count(struct device *dev, const char *con_id);
76
77 bool acpi_can_fallback_to_crs(struct acpi_device *adev, const char *con_id);
78 #else
79 static inline void acpi_gpiochip_add(struct gpio_chip *chip) { }
80 static inline void acpi_gpiochip_remove(struct gpio_chip *chip) { }
81
82 static inline void
83 acpi_gpiochip_request_interrupts(struct gpio_chip *chip) { }
84
85 static inline void
86 acpi_gpiochip_free_interrupts(struct gpio_chip *chip) { }
87
88 static inline struct gpio_desc *
89 acpi_get_gpiod_by_index(struct acpi_device *adev, const char *propname,
90 int index, struct acpi_gpio_info *info)
91 {
92 return ERR_PTR(-ENOSYS);
93 }
94 static inline struct gpio_desc *
95 acpi_node_get_gpiod(struct fwnode_handle *fwnode, const char *propname,
96 int index, struct acpi_gpio_info *info)
97 {
98 return ERR_PTR(-ENXIO);
99 }
100 static inline int acpi_gpio_count(struct device *dev, const char *con_id)
101 {
102 return -ENODEV;
103 }
104
105 static inline bool acpi_can_fallback_to_crs(struct acpi_device *adev,
106 const char *con_id)
107 {
108 return false;
109 }
110 #endif
111
112 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 struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip, u16 hwnum);
116
117 extern struct spinlock gpio_lock;
118 extern struct list_head gpio_devices;
119
120 struct gpio_desc {
121 struct gpio_chip *chip;
122 unsigned long flags;
123 /* flag symbols are bit numbers */
124 #define FLAG_REQUESTED 0
125 #define FLAG_IS_OUT 1
126 #define FLAG_EXPORT 2 /* protected by sysfs_lock */
127 #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
128 #define FLAG_ACTIVE_LOW 6 /* value has active low */
129 #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
130 #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
131 #define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */
132 #define FLAG_IS_HOGGED 11 /* GPIO is hogged */
133
134 /* Connection label */
135 const char *label;
136 /* Name of the GPIO */
137 const char *name;
138 };
139
140 int gpiod_request(struct gpio_desc *desc, const char *label);
141 void gpiod_free(struct gpio_desc *desc);
142 int gpiod_hog(struct gpio_desc *desc, const char *name,
143 unsigned long lflags, enum gpiod_flags dflags);
144
145 /*
146 * Return the GPIO number of the passed descriptor relative to its chip
147 */
148 static int __maybe_unused gpio_chip_hwgpio(const struct gpio_desc *desc)
149 {
150 return desc - &desc->chip->desc[0];
151 }
152
153 /* With descriptor prefix */
154
155 #define gpiod_emerg(desc, fmt, ...) \
156 pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
157 ##__VA_ARGS__)
158 #define gpiod_crit(desc, fmt, ...) \
159 pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
160 ##__VA_ARGS__)
161 #define gpiod_err(desc, fmt, ...) \
162 pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
163 ##__VA_ARGS__)
164 #define gpiod_warn(desc, fmt, ...) \
165 pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
166 ##__VA_ARGS__)
167 #define gpiod_info(desc, fmt, ...) \
168 pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
169 ##__VA_ARGS__)
170 #define gpiod_dbg(desc, fmt, ...) \
171 pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
172 ##__VA_ARGS__)
173
174 /* With chip prefix */
175
176 #define chip_emerg(chip, fmt, ...) \
177 pr_emerg("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
178 #define chip_crit(chip, fmt, ...) \
179 pr_crit("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
180 #define chip_err(chip, fmt, ...) \
181 pr_err("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
182 #define chip_warn(chip, fmt, ...) \
183 pr_warn("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
184 #define chip_info(chip, fmt, ...) \
185 pr_info("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
186 #define chip_dbg(chip, fmt, ...) \
187 pr_debug("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
188
189 #ifdef CONFIG_GPIO_SYSFS
190
191 int gpiochip_sysfs_register(struct gpio_chip *chip);
192 void gpiochip_sysfs_unregister(struct gpio_chip *chip);
193
194 #else
195
196 static inline int gpiochip_sysfs_register(struct gpio_chip *chip)
197 {
198 return 0;
199 }
200
201 static inline void gpiochip_sysfs_unregister(struct gpio_chip *chip)
202 {
203 }
204
205 #endif /* CONFIG_GPIO_SYSFS */
206
207 #endif /* GPIOLIB_H */