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/cdev.h> 13 #include <linux/device.h> 14 #include <linux/err.h> 15 #include <linux/gpio/consumer.h> /* for enum gpiod_flags */ 16 #include <linux/gpio/driver.h> 17 #include <linux/module.h> 18 #include <linux/notifier.h> 19 #include <linux/srcu.h> 20 21 #define GPIOCHIP_NAME "gpiochip" 22 23 /** 24 * struct gpio_device - internal state container for GPIO devices 25 * @dev: the GPIO device struct 26 * @chrdev: character device for the GPIO device 27 * @id: numerical ID number for the GPIO chip 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 * @desc_srcu: ensures consistent state of GPIO descriptors exposed to users 35 * @ngpio: the number of GPIO lines on this GPIO device, equal to the size 36 * of the @descs array. 37 * @can_sleep: indicate whether the GPIO chip driver's callbacks can sleep 38 * implying that they cannot be used from atomic context 39 * @base: GPIO base in the DEPRECATED global Linux GPIO numberspace, assigned 40 * at device creation time. 41 * @label: a descriptive name for the GPIO device, such as the part number 42 * or name of the IP component in a System on Chip. 43 * @data: per-instance data assigned by the driver 44 * @list: links gpio_device:s together for traversal 45 * @line_state_notifier: used to notify subscribers about lines being 46 * requested, released or reconfigured 47 * @device_notifier: used to notify character device wait queues about the GPIO 48 * device being unregistered 49 * @srcu: protects the pointer to the underlying GPIO chip 50 * @pin_ranges: range of pins served by the GPIO driver 51 * 52 * This state container holds most of the runtime variable data 53 * for a GPIO device and can hold references and live on after the 54 * GPIO chip has been removed, if it is still being used from 55 * userspace. 56 */ 57 struct gpio_device { 58 struct device dev; 59 struct cdev chrdev; 60 int id; 61 struct device *mockdev; 62 struct module *owner; 63 struct gpio_chip __rcu *chip; 64 struct gpio_desc *descs; 65 struct srcu_struct desc_srcu; 66 unsigned int base; 67 u16 ngpio; 68 bool can_sleep; 69 const char *label; 70 void *data; 71 struct list_head list; 72 struct blocking_notifier_head line_state_notifier; 73 struct blocking_notifier_head device_notifier; 74 struct srcu_struct srcu; 75 76 #ifdef CONFIG_PINCTRL 77 /* 78 * If CONFIG_PINCTRL is enabled, then gpio controllers can optionally 79 * describe the actual pin range which they serve in an SoC. This 80 * information would be used by pinctrl subsystem to configure 81 * corresponding pins for gpio usage. 82 */ 83 struct list_head pin_ranges; 84 #endif 85 }; 86 87 static inline struct gpio_device *to_gpio_device(struct device *dev) 88 { 89 return container_of(dev, struct gpio_device, dev); 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 /** 96 * struct gpio_array - Opaque descriptor for a structure of GPIO array attributes 97 * 98 * @desc: Array of pointers to the GPIO descriptors 99 * @size: Number of elements in desc 100 * @chip: Parent GPIO chip 101 * @get_mask: Get mask used in fastpath 102 * @set_mask: Set mask used in fastpath 103 * @invert_mask: Invert mask used in fastpath 104 * 105 * This structure is attached to struct gpiod_descs obtained from 106 * gpiod_get_array() and can be passed back to get/set array functions in order 107 * to activate fast processing path if applicable. 108 */ 109 struct gpio_array { 110 struct gpio_desc **desc; 111 unsigned int size; 112 struct gpio_chip *chip; 113 unsigned long *get_mask; 114 unsigned long *set_mask; 115 unsigned long invert_mask[]; 116 }; 117 118 #define for_each_gpio_desc(gc, desc) \ 119 for (unsigned int __i = 0; \ 120 __i < gc->ngpio && (desc = gpiochip_get_desc(gc, __i)); \ 121 __i++) \ 122 123 #define for_each_gpio_desc_with_flag(gc, desc, flag) \ 124 for_each_gpio_desc(gc, desc) \ 125 if (!test_bit(flag, &desc->flags)) {} else 126 127 int gpiod_get_array_value_complex(bool raw, bool can_sleep, 128 unsigned int array_size, 129 struct gpio_desc **desc_array, 130 struct gpio_array *array_info, 131 unsigned long *value_bitmap); 132 int gpiod_set_array_value_complex(bool raw, bool can_sleep, 133 unsigned int array_size, 134 struct gpio_desc **desc_array, 135 struct gpio_array *array_info, 136 unsigned long *value_bitmap); 137 138 int gpiod_set_transitory(struct gpio_desc *desc, bool transitory); 139 140 void gpiod_line_state_notify(struct gpio_desc *desc, unsigned long action); 141 142 struct gpio_desc_label { 143 struct rcu_head rh; 144 char str[]; 145 }; 146 147 /** 148 * struct gpio_desc - Opaque descriptor for a GPIO 149 * 150 * @gdev: Pointer to the parent GPIO device 151 * @flags: Binary descriptor flags 152 * @label: Name of the consumer 153 * @name: Line name 154 * @hog: Pointer to the device node that hogs this line (if any) 155 * 156 * These are obtained using gpiod_get() and are preferable to the old 157 * integer-based handles. 158 * 159 * Contrary to integers, a pointer to a &struct gpio_desc is guaranteed to be 160 * valid until the GPIO is released. 161 */ 162 struct gpio_desc { 163 struct gpio_device *gdev; 164 unsigned long flags; 165 /* flag symbols are bit numbers */ 166 #define FLAG_REQUESTED 0 167 #define FLAG_IS_OUT 1 168 #define FLAG_EXPORT 2 /* protected by sysfs_lock */ 169 #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */ 170 #define FLAG_ACTIVE_LOW 6 /* value has active low */ 171 #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */ 172 #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */ 173 #define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */ 174 #define FLAG_IRQ_IS_ENABLED 10 /* GPIO is connected to an enabled IRQ */ 175 #define FLAG_IS_HOGGED 11 /* GPIO is hogged */ 176 #define FLAG_TRANSITORY 12 /* GPIO may lose value in sleep or reset */ 177 #define FLAG_PULL_UP 13 /* GPIO has pull up enabled */ 178 #define FLAG_PULL_DOWN 14 /* GPIO has pull down enabled */ 179 #define FLAG_BIAS_DISABLE 15 /* GPIO has pull disabled */ 180 #define FLAG_EDGE_RISING 16 /* GPIO CDEV detects rising edge events */ 181 #define FLAG_EDGE_FALLING 17 /* GPIO CDEV detects falling edge events */ 182 #define FLAG_EVENT_CLOCK_REALTIME 18 /* GPIO CDEV reports REALTIME timestamps in events */ 183 #define FLAG_EVENT_CLOCK_HTE 19 /* GPIO CDEV reports hardware timestamps in events */ 184 185 /* Connection label */ 186 struct gpio_desc_label __rcu *label; 187 /* Name of the GPIO */ 188 const char *name; 189 #ifdef CONFIG_OF_DYNAMIC 190 struct device_node *hog; 191 #endif 192 }; 193 194 #define gpiod_not_found(desc) (IS_ERR(desc) && PTR_ERR(desc) == -ENOENT) 195 196 struct gpio_chip_guard { 197 struct gpio_device *gdev; 198 struct gpio_chip *gc; 199 int idx; 200 }; 201 202 DEFINE_CLASS(gpio_chip_guard, 203 struct gpio_chip_guard, 204 srcu_read_unlock(&_T.gdev->srcu, _T.idx), 205 ({ 206 struct gpio_chip_guard _guard; 207 208 _guard.gdev = desc->gdev; 209 _guard.idx = srcu_read_lock(&_guard.gdev->srcu); 210 _guard.gc = srcu_dereference(_guard.gdev->chip, 211 &_guard.gdev->srcu); 212 213 _guard; 214 }), 215 struct gpio_desc *desc) 216 217 int gpiod_request(struct gpio_desc *desc, const char *label); 218 void gpiod_free(struct gpio_desc *desc); 219 220 static inline int gpiod_request_user(struct gpio_desc *desc, const char *label) 221 { 222 int ret; 223 224 ret = gpiod_request(desc, label); 225 if (ret == -EPROBE_DEFER) 226 ret = -ENODEV; 227 228 return ret; 229 } 230 231 struct gpio_desc *gpiod_find_and_request(struct device *consumer, 232 struct fwnode_handle *fwnode, 233 const char *con_id, 234 unsigned int idx, 235 enum gpiod_flags flags, 236 const char *label, 237 bool platform_lookup_allowed); 238 239 int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id, 240 unsigned long lflags, enum gpiod_flags dflags); 241 int gpio_set_debounce_timeout(struct gpio_desc *desc, unsigned int debounce); 242 int gpiod_hog(struct gpio_desc *desc, const char *name, 243 unsigned long lflags, enum gpiod_flags dflags); 244 int gpiochip_get_ngpios(struct gpio_chip *gc, struct device *dev); 245 const char *gpiod_get_label(struct gpio_desc *desc); 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_err(desc, fmt, ...) \ 258 do { \ 259 scoped_guard(srcu, &desc->gdev->desc_srcu) { \ 260 pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), \ 261 gpiod_get_label(desc) ? : "?", ##__VA_ARGS__); \ 262 } \ 263 } while (0) 264 265 #define gpiod_warn(desc, fmt, ...) \ 266 do { \ 267 scoped_guard(srcu, &desc->gdev->desc_srcu) { \ 268 pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), \ 269 gpiod_get_label(desc) ? : "?", ##__VA_ARGS__); \ 270 } \ 271 } while (0) 272 273 #define gpiod_dbg(desc, fmt, ...) \ 274 do { \ 275 scoped_guard(srcu, &desc->gdev->desc_srcu) { \ 276 pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), \ 277 gpiod_get_label(desc) ? : "?", ##__VA_ARGS__); \ 278 } \ 279 } while (0) 280 281 /* With chip prefix */ 282 283 #define chip_err(gc, fmt, ...) \ 284 dev_err(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__) 285 #define chip_warn(gc, fmt, ...) \ 286 dev_warn(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__) 287 #define chip_info(gc, fmt, ...) \ 288 dev_info(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__) 289 #define chip_dbg(gc, fmt, ...) \ 290 dev_dbg(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__) 291 292 #endif /* GPIOLIB_H */ 293