1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * Internal GPIO functions. 4 * 5 * Copyright (C) 2013, Intel Corporation 6 * Author: Mika Westerberg <mika.westerberg@linux.intel.com> 7 */ 8 9 #ifndef GPIOLIB_H 10 #define GPIOLIB_H 11 12 #include <linux/gpio/driver.h> 13 #include <linux/gpio/consumer.h> /* for enum gpiod_flags */ 14 #include <linux/err.h> 15 #include <linux/device.h> 16 #include <linux/module.h> 17 #include <linux/cdev.h> 18 19 enum of_gpio_flags; 20 struct acpi_device; 21 22 /** 23 * struct gpio_device - internal state container for GPIO devices 24 * @id: numerical ID number for the GPIO chip 25 * @dev: the GPIO device struct 26 * @chrdev: character device for the GPIO device 27 * @mockdev: class device used by the deprecated sysfs interface (may be 28 * NULL) 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 * @descs: array of ngpio descriptors. 33 * @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. 37 * @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. 39 * @data: per-instance data assigned by the driver 40 * @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 */ 47 struct gpio_device { 48 int id; 49 struct device dev; 50 struct cdev chrdev; 51 struct device *mockdev; 52 struct module *owner; 53 struct gpio_chip *chip; 54 struct gpio_desc *descs; 55 int base; 56 u16 ngpio; 57 const char *label; 58 void *data; 59 struct list_head list; 60 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 70 }; 71 72 /** 73 * struct acpi_gpio_info - ACPI GPIO specific information 74 * @adev: reference to ACPI device which consumes GPIO resource 75 * @flags: GPIO initialization flags 76 * @gpioint: if %true this GPIO is of type GpioInt otherwise type is GpioIo 77 * @pin_config: pin bias as provided by ACPI 78 * @polarity: interrupt polarity as provided by ACPI 79 * @triggering: triggering type as provided by ACPI 80 * @quirks: Linux specific quirks as provided by struct acpi_gpio_mapping 81 */ 82 struct acpi_gpio_info { 83 struct acpi_device *adev; 84 enum gpiod_flags flags; 85 bool gpioint; 86 int pin_config; 87 int polarity; 88 int triggering; 89 unsigned int quirks; 90 }; 91 92 /* gpio suffixes used for ACPI and device tree lookup */ 93 static __maybe_unused const char * const gpio_suffixes[] = { "gpios", "gpio" }; 94 95 #ifdef CONFIG_OF_GPIO 96 struct gpio_desc *of_find_gpio(struct device *dev, 97 const char *con_id, 98 unsigned int idx, 99 unsigned long *lookupflags); 100 struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np, 101 const char *list_name, int index, enum of_gpio_flags *flags); 102 int of_gpiochip_add(struct gpio_chip *gc); 103 void of_gpiochip_remove(struct gpio_chip *gc); 104 #else 105 static inline struct gpio_desc *of_find_gpio(struct device *dev, 106 const char *con_id, 107 unsigned int idx, 108 unsigned long *lookupflags) 109 { 110 return ERR_PTR(-ENOENT); 111 } 112 static 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 } 117 static inline int of_gpiochip_add(struct gpio_chip *gc) { return 0; } 118 static inline void of_gpiochip_remove(struct gpio_chip *gc) { } 119 #endif /* CONFIG_OF_GPIO */ 120 121 #ifdef CONFIG_ACPI 122 void acpi_gpiochip_add(struct gpio_chip *chip); 123 void acpi_gpiochip_remove(struct gpio_chip *chip); 124 125 void acpi_gpiochip_request_interrupts(struct gpio_chip *chip); 126 void acpi_gpiochip_free_interrupts(struct gpio_chip *chip); 127 128 int acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags, 129 struct acpi_gpio_info *info); 130 int acpi_gpio_update_gpiod_lookup_flags(unsigned long *lookupflags, 131 struct acpi_gpio_info *info); 132 133 struct gpio_desc *acpi_find_gpio(struct device *dev, 134 const char *con_id, 135 unsigned int idx, 136 enum gpiod_flags *dflags, 137 unsigned long *lookupflags); 138 struct gpio_desc *acpi_node_get_gpiod(struct fwnode_handle *fwnode, 139 const char *propname, int index, 140 struct acpi_gpio_info *info); 141 142 int acpi_gpio_count(struct device *dev, const char *con_id); 143 144 bool acpi_can_fallback_to_crs(struct acpi_device *adev, const char *con_id); 145 #else 146 static inline void acpi_gpiochip_add(struct gpio_chip *chip) { } 147 static inline void acpi_gpiochip_remove(struct gpio_chip *chip) { } 148 149 static inline void 150 acpi_gpiochip_request_interrupts(struct gpio_chip *chip) { } 151 152 static inline void 153 acpi_gpiochip_free_interrupts(struct gpio_chip *chip) { } 154 155 static inline int 156 acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags, struct acpi_gpio_info *info) 157 { 158 return 0; 159 } 160 static inline int 161 acpi_gpio_update_gpiod_lookup_flags(unsigned long *lookupflags, 162 struct acpi_gpio_info *info) 163 { 164 return 0; 165 } 166 167 static inline struct gpio_desc * 168 acpi_find_gpio(struct device *dev, const char *con_id, 169 unsigned int idx, enum gpiod_flags *dflags, 170 unsigned long *lookupflags) 171 { 172 return ERR_PTR(-ENOENT); 173 } 174 static inline struct gpio_desc * 175 acpi_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 } 180 static inline int acpi_gpio_count(struct device *dev, const char *con_id) 181 { 182 return -ENODEV; 183 } 184 185 static inline bool acpi_can_fallback_to_crs(struct acpi_device *adev, 186 const char *con_id) 187 { 188 return false; 189 } 190 #endif 191 192 struct 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 201 struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip, u16 hwnum); 202 int gpiod_get_array_value_complex(bool raw, bool can_sleep, 203 unsigned int array_size, 204 struct gpio_desc **desc_array, 205 struct gpio_array *array_info, 206 unsigned long *value_bitmap); 207 int gpiod_set_array_value_complex(bool raw, bool can_sleep, 208 unsigned int array_size, 209 struct gpio_desc **desc_array, 210 struct gpio_array *array_info, 211 unsigned long *value_bitmap); 212 213 extern struct spinlock gpio_lock; 214 extern struct list_head gpio_devices; 215 216 struct gpio_desc { 217 struct gpio_device *gdev; 218 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 */ 224 #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 */ 228 #define FLAG_IRQ_IS_ENABLED 10 /* GPIO is connected to an enabled IRQ */ 229 #define FLAG_IS_HOGGED 11 /* GPIO is hogged */ 230 #define FLAG_TRANSITORY 12 /* GPIO may lose value in sleep or reset */ 231 #define FLAG_PULL_UP 13 /* GPIO has pull up enabled */ 232 #define FLAG_PULL_DOWN 14 /* GPIO has pull down enabled */ 233 234 /* Connection label */ 235 const char *label; 236 /* Name of the GPIO */ 237 const char *name; 238 }; 239 240 int gpiod_request(struct gpio_desc *desc, const char *label); 241 void gpiod_free(struct gpio_desc *desc); 242 int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id, 243 unsigned long lflags, enum gpiod_flags dflags); 244 int gpiod_hog(struct gpio_desc *desc, const char *name, 245 unsigned long lflags, enum gpiod_flags dflags); 246 247 /* 248 * Return the GPIO number of the passed descriptor relative to its chip 249 */ 250 static inline int gpio_chip_hwgpio(const struct gpio_desc *desc) 251 { 252 return desc - &desc->gdev->descs[0]; 253 } 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, ...) \ 279 dev_emerg(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__) 280 #define chip_crit(chip, fmt, ...) \ 281 dev_crit(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__) 282 #define chip_err(chip, fmt, ...) \ 283 dev_err(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__) 284 #define chip_warn(chip, fmt, ...) \ 285 dev_warn(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__) 286 #define chip_info(chip, fmt, ...) \ 287 dev_info(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__) 288 #define chip_dbg(chip, fmt, ...) \ 289 dev_dbg(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__) 290 291 #ifdef CONFIG_GPIO_SYSFS 292 293 int gpiochip_sysfs_register(struct gpio_device *gdev); 294 void gpiochip_sysfs_unregister(struct gpio_device *gdev); 295 296 #else 297 298 static inline int gpiochip_sysfs_register(struct gpio_device *gdev) 299 { 300 return 0; 301 } 302 303 static inline void gpiochip_sysfs_unregister(struct gpio_device *gdev) 304 { 305 } 306 307 #endif /* CONFIG_GPIO_SYSFS */ 308 309 #endif /* GPIOLIB_H */ 310