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