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 enum gpio_lookup_flags; 21 struct acpi_device; 22 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 27 * @chrdev: character device for the GPIO device 28 * @mockdev: class device used by the deprecated sysfs interface (may be 29 * NULL) 30 * @owner: helps prevent removal of modules exporting active GPIOs 31 * @chip: pointer to the corresponding gpiochip, holding static 32 * data for this device 33 * @descs: array of ngpio descriptors. 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. 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. 40 * @data: per-instance data assigned by the driver 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 */ 48 struct gpio_device { 49 int id; 50 struct device dev; 51 struct cdev chrdev; 52 struct device *mockdev; 53 struct module *owner; 54 struct gpio_chip *chip; 55 struct gpio_desc *descs; 56 int base; 57 u16 ngpio; 58 const char *label; 59 void *data; 60 struct list_head list; 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 71 }; 72 73 /** 74 * struct acpi_gpio_info - ACPI GPIO specific information 75 * @adev: reference to ACPI device which consumes GPIO resource 76 * @flags: GPIO initialization flags 77 * @gpioint: if %true this GPIO is of type GpioInt otherwise type is GpioIo 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 polarity; 87 int triggering; 88 unsigned int quirks; 89 }; 90 91 /* gpio suffixes used for ACPI and device tree lookup */ 92 static __maybe_unused const char * const gpio_suffixes[] = { "gpios", "gpio" }; 93 94 #ifdef CONFIG_OF_GPIO 95 struct gpio_desc *of_find_gpio(struct device *dev, 96 const char *con_id, 97 unsigned int idx, 98 enum gpio_lookup_flags *flags); 99 struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np, 100 const char *list_name, int index, enum of_gpio_flags *flags); 101 int of_gpiochip_add(struct gpio_chip *gc); 102 void of_gpiochip_remove(struct gpio_chip *gc); 103 #else 104 static 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 } 111 static 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 } 116 static inline int of_gpiochip_add(struct gpio_chip *gc) { return 0; } 117 static inline void of_gpiochip_remove(struct gpio_chip *gc) { } 118 #endif /* CONFIG_OF_GPIO */ 119 120 #ifdef CONFIG_ACPI 121 void acpi_gpiochip_add(struct gpio_chip *chip); 122 void acpi_gpiochip_remove(struct gpio_chip *chip); 123 124 void acpi_gpiochip_request_interrupts(struct gpio_chip *chip); 125 void acpi_gpiochip_free_interrupts(struct gpio_chip *chip); 126 127 int acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags, 128 struct acpi_gpio_info *info); 129 130 struct gpio_desc *acpi_find_gpio(struct device *dev, 131 const char *con_id, 132 unsigned int idx, 133 enum gpiod_flags *dflags, 134 enum gpio_lookup_flags *lookupflags); 135 struct gpio_desc *acpi_node_get_gpiod(struct fwnode_handle *fwnode, 136 const char *propname, int index, 137 struct acpi_gpio_info *info); 138 139 int acpi_gpio_count(struct device *dev, const char *con_id); 140 141 bool acpi_can_fallback_to_crs(struct acpi_device *adev, const char *con_id); 142 #else 143 static inline void acpi_gpiochip_add(struct gpio_chip *chip) { } 144 static inline void acpi_gpiochip_remove(struct gpio_chip *chip) { } 145 146 static inline void 147 acpi_gpiochip_request_interrupts(struct gpio_chip *chip) { } 148 149 static inline void 150 acpi_gpiochip_free_interrupts(struct gpio_chip *chip) { } 151 152 static inline int 153 acpi_gpio_update_gpiod_flags(enum gpiod_flags *flags, struct acpi_gpio_info *info) 154 { 155 return 0; 156 } 157 158 static inline struct gpio_desc * 159 acpi_find_gpio(struct device *dev, const char *con_id, 160 unsigned int idx, enum gpiod_flags *dflags, 161 enum gpio_lookup_flags *lookupflags) 162 { 163 return ERR_PTR(-ENOENT); 164 } 165 static inline struct gpio_desc * 166 acpi_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 } 171 static inline int acpi_gpio_count(struct device *dev, const char *con_id) 172 { 173 return -ENODEV; 174 } 175 176 static inline bool acpi_can_fallback_to_crs(struct acpi_device *adev, 177 const char *con_id) 178 { 179 return false; 180 } 181 #endif 182 183 struct 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 192 struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip, u16 hwnum); 193 int gpiod_get_array_value_complex(bool raw, bool can_sleep, 194 unsigned int array_size, 195 struct gpio_desc **desc_array, 196 struct gpio_array *array_info, 197 unsigned long *value_bitmap); 198 int gpiod_set_array_value_complex(bool raw, bool can_sleep, 199 unsigned int array_size, 200 struct gpio_desc **desc_array, 201 struct gpio_array *array_info, 202 unsigned long *value_bitmap); 203 204 extern struct spinlock gpio_lock; 205 extern struct list_head gpio_devices; 206 207 struct gpio_desc { 208 struct gpio_device *gdev; 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 */ 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 */ 219 #define FLAG_IRQ_IS_ENABLED 10 /* GPIO is connected to an enabled IRQ */ 220 #define FLAG_IS_HOGGED 11 /* GPIO is hogged */ 221 #define FLAG_TRANSITORY 12 /* GPIO may lose value in sleep or reset */ 222 #define FLAG_PULL_UP 13 /* GPIO has pull up enabled */ 223 #define FLAG_PULL_DOWN 14 /* GPIO has pull down enabled */ 224 225 /* Connection label */ 226 const char *label; 227 /* Name of the GPIO */ 228 const char *name; 229 }; 230 231 int gpiod_request(struct gpio_desc *desc, const char *label); 232 void gpiod_free(struct gpio_desc *desc); 233 int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id, 234 unsigned long lflags, enum gpiod_flags dflags); 235 int gpiod_hog(struct gpio_desc *desc, const char *name, 236 unsigned long lflags, enum gpiod_flags dflags); 237 238 /* 239 * Return the GPIO number of the passed descriptor relative to its chip 240 */ 241 static inline int gpio_chip_hwgpio(const struct gpio_desc *desc) 242 { 243 return desc - &desc->gdev->descs[0]; 244 } 245 246 void devprop_gpiochip_set_names(struct gpio_chip *chip, 247 const struct fwnode_handle *fwnode); 248 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, ...) \ 273 dev_emerg(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__) 274 #define chip_crit(chip, fmt, ...) \ 275 dev_crit(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__) 276 #define chip_err(chip, fmt, ...) \ 277 dev_err(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__) 278 #define chip_warn(chip, fmt, ...) \ 279 dev_warn(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__) 280 #define chip_info(chip, fmt, ...) \ 281 dev_info(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__) 282 #define chip_dbg(chip, fmt, ...) \ 283 dev_dbg(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__) 284 285 #ifdef CONFIG_GPIO_SYSFS 286 287 int gpiochip_sysfs_register(struct gpio_device *gdev); 288 void gpiochip_sysfs_unregister(struct gpio_device *gdev); 289 290 #else 291 292 static inline int gpiochip_sysfs_register(struct gpio_device *gdev) 293 { 294 return 0; 295 } 296 297 static inline void gpiochip_sysfs_unregister(struct gpio_device *gdev) 298 { 299 } 300 301 #endif /* CONFIG_GPIO_SYSFS */ 302 303 #endif /* GPIOLIB_H */ 304