| 1 |
/* SPDX-License-Identifier: GPL-2.0 */ |
| 2 |
/* |
| 3 |
* Internal GPIO functions. |
| 4 |
* |
| 5 |
* Copyright (C) 2013, Intel Corporation |
| 6 |
* Author: Mika Westerberg <[email protected]> |
| 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 |
* @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 |
* @valid_mask: If not %NULL, holds bitmask of GPIOs which are valid to be |
| 35 |
* used from the chip. |
| 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 module *owner; |
| 67 |
struct gpio_chip __rcu *chip; |
| 68 |
struct gpio_desc *descs; |
| 69 |
unsigned long *valid_mask; |
| 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 GPIOD_FLAG_REQUESTED 0 /* GPIO is in use */ |
| 190 |
#define GPIOD_FLAG_IS_OUT 1 /* GPIO is in output mode */ |
| 191 |
#define GPIOD_FLAG_EXPORT 2 /* GPIO is exported to user-space */ |
| 192 |
#define GPIOD_FLAG_SYSFS 3 /* GPIO is exported via /sys/class/gpio */ |
| 193 |
#define GPIOD_FLAG_ACTIVE_LOW 6 /* GPIO is active-low */ |
| 194 |
#define GPIOD_FLAG_OPEN_DRAIN 7 /* GPIO is open drain type */ |
| 195 |
#define GPIOD_FLAG_OPEN_SOURCE 8 /* GPIO is open source type */ |
| 196 |
#define GPIOD_FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */ |
| 197 |
#define GPIOD_FLAG_IRQ_IS_ENABLED 10 /* GPIO is connected to an enabled IRQ */ |
| 198 |
#define GPIOD_FLAG_IS_HOGGED 11 /* GPIO is hogged */ |
| 199 |
#define GPIOD_FLAG_TRANSITORY 12 /* GPIO may lose value in sleep or reset */ |
| 200 |
#define GPIOD_FLAG_PULL_UP 13 /* GPIO has pull up enabled */ |
| 201 |
#define GPIOD_FLAG_PULL_DOWN 14 /* GPIO has pull down enabled */ |
| 202 |
#define GPIOD_FLAG_BIAS_DISABLE 15 /* GPIO has pull disabled */ |
| 203 |
#define GPIOD_FLAG_EDGE_RISING 16 /* GPIO CDEV detects rising edge events */ |
| 204 |
#define GPIOD_FLAG_EDGE_FALLING 17 /* GPIO CDEV detects falling edge events */ |
| 205 |
#define GPIOD_FLAG_EVENT_CLOCK_REALTIME 18 /* GPIO CDEV reports REALTIME timestamps in events */ |
| 206 |
#define GPIOD_FLAG_EVENT_CLOCK_HTE 19 /* GPIO CDEV reports hardware timestamps in events */ |
| 207 |
#define GPIOD_FLAG_SHARED 20 /* GPIO is shared by multiple consumers */ |
| 208 |
#define GPIOD_FLAG_SHARED_PROXY 21 /* GPIO is a virtual proxy to a physically shared pin. */ |
| 209 |
|
| 210 |
/* Connection label */ |
| 211 |
struct gpio_desc_label __rcu *label; |
| 212 |
/* Name of the GPIO */ |
| 213 |
const char *name; |
| 214 |
#ifdef CONFIG_OF_DYNAMIC |
| 215 |
struct device_node *hog; |
| 216 |
#endif |
| 217 |
#ifdef CONFIG_GPIO_CDEV |
| 218 |
/* debounce period in microseconds */ |
| 219 |
unsigned int debounce_period_us; |
| 220 |
#endif |
| 221 |
}; |
| 222 |
|
| 223 |
#define gpiod_not_found(desc) (IS_ERR(desc) && PTR_ERR(desc) == -ENOENT) |
| 224 |
|
| 225 |
struct gpio_chip_guard { |
| 226 |
struct gpio_device *gdev; |
| 227 |
struct gpio_chip *gc; |
| 228 |
int idx; |
| 229 |
}; |
| 230 |
|
| 231 |
DEFINE_CLASS(gpio_chip_guard, |
| 232 |
struct gpio_chip_guard, |
| 233 |
srcu_read_unlock(&_T.gdev->srcu, _T.idx), |
| 234 |
({ |
| 235 |
struct gpio_chip_guard _guard; |
| 236 |
|
| 237 |
_guard.gdev = desc->gdev; |
| 238 |
_guard.idx = srcu_read_lock(&_guard.gdev->srcu); |
| 239 |
_guard.gc = srcu_dereference(_guard.gdev->chip, |
| 240 |
&_guard.gdev->srcu); |
| 241 |
|
| 242 |
_guard; |
| 243 |
}), |
| 244 |
struct gpio_desc *desc) |
| 245 |
|
| 246 |
int gpiod_request(struct gpio_desc *desc, const char *label); |
| 247 |
int gpiod_request_commit(struct gpio_desc *desc, const char *label); |
| 248 |
void gpiod_free(struct gpio_desc *desc); |
| 249 |
void gpiod_free_commit(struct gpio_desc *desc); |
| 250 |
|
| 251 |
static inline int gpiod_request_user(struct gpio_desc *desc, const char *label) |
| 252 |
{ |
| 253 |
int ret; |
| 254 |
|
| 255 |
ret = gpiod_request(desc, label); |
| 256 |
if (ret == -EPROBE_DEFER) |
| 257 |
ret = -ENODEV; |
| 258 |
|
| 259 |
return ret; |
| 260 |
} |
| 261 |
|
| 262 |
struct gpio_desc *gpiod_find_and_request(struct device *consumer, |
| 263 |
struct fwnode_handle *fwnode, |
| 264 |
const char *con_id, |
| 265 |
unsigned int idx, |
| 266 |
enum gpiod_flags flags, |
| 267 |
const char *label, |
| 268 |
bool platform_lookup_allowed); |
| 269 |
|
| 270 |
int gpio_do_set_config(struct gpio_desc *desc, unsigned long config); |
| 271 |
int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id, |
| 272 |
unsigned long lflags, enum gpiod_flags dflags); |
| 273 |
int gpio_set_debounce_timeout(struct gpio_desc *desc, unsigned int debounce); |
| 274 |
int gpiod_hog(struct gpio_desc *desc, const char *name, |
| 275 |
unsigned long lflags, enum gpiod_flags dflags); |
| 276 |
int gpiochip_get_ngpios(struct gpio_chip *gc, struct device *dev); |
| 277 |
struct gpio_desc *gpiochip_get_desc(struct gpio_chip *gc, unsigned int hwnum); |
| 278 |
const char *gpiod_get_label(struct gpio_desc *desc); |
| 279 |
|
| 280 |
/* With descriptor prefix */ |
| 281 |
|
| 282 |
#define __gpiod_pr(level, desc, fmt, ...) \ |
| 283 |
do { \ |
| 284 |
scoped_guard(srcu, &desc->gdev->desc_srcu) { \ |
| 285 |
pr_##level("gpio-%d (%s): " fmt, desc_to_gpio(desc), \ |
| 286 |
gpiod_get_label(desc) ?: "?", ##__VA_ARGS__); \ |
| 287 |
} \ |
| 288 |
} while (0) |
| 289 |
|
| 290 |
#define gpiod_err(desc, fmt, ...) __gpiod_pr(err, desc, fmt, ##__VA_ARGS__) |
| 291 |
#define gpiod_warn(desc, fmt, ...) __gpiod_pr(warn, desc, fmt, ##__VA_ARGS__) |
| 292 |
#define gpiod_dbg(desc, fmt, ...) __gpiod_pr(debug, desc, fmt, ##__VA_ARGS__) |
| 293 |
|
| 294 |
/* With chip prefix */ |
| 295 |
|
| 296 |
#define __gpiochip_pr(level, gc, fmt, ...) \ |
| 297 |
do { \ |
| 298 |
dev_##level(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__); \ |
| 299 |
} while (0) |
| 300 |
|
| 301 |
#define gpiochip_err(gc, fmt, ...) __gpiochip_pr(err, gc, fmt, ##__VA_ARGS__) |
| 302 |
#define gpiochip_warn(gc, fmt, ...) __gpiochip_pr(warn, gc, fmt, ##__VA_ARGS__) |
| 303 |
#define gpiochip_info(gc, fmt, ...) __gpiochip_pr(info, gc, fmt, ##__VA_ARGS__) |
| 304 |
#define gpiochip_dbg(gc, fmt, ...) __gpiochip_pr(dbg, gc, fmt, ##__VA_ARGS__) |
| 305 |
|
| 306 |
#endif /* GPIOLIB_H */ |