0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef GPIOLIB_H
0010 #define GPIOLIB_H
0011
0012 #include <linux/gpio/driver.h>
0013 #include <linux/gpio/consumer.h> /* for enum gpiod_flags */
0014 #include <linux/err.h>
0015 #include <linux/device.h>
0016 #include <linux/module.h>
0017 #include <linux/cdev.h>
0018
0019 #define GPIOCHIP_NAME "gpiochip"
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049 struct gpio_device {
0050 int id;
0051 struct device dev;
0052 struct cdev chrdev;
0053 struct device *mockdev;
0054 struct module *owner;
0055 struct gpio_chip *chip;
0056 struct gpio_desc *descs;
0057 int base;
0058 u16 ngpio;
0059 const char *label;
0060 void *data;
0061 struct list_head list;
0062 struct blocking_notifier_head notifier;
0063
0064 #ifdef CONFIG_PINCTRL
0065
0066
0067
0068
0069
0070
0071 struct list_head pin_ranges;
0072 #endif
0073 };
0074
0075
0076 static __maybe_unused const char * const gpio_suffixes[] = { "gpios", "gpio" };
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092 struct gpio_array {
0093 struct gpio_desc **desc;
0094 unsigned int size;
0095 struct gpio_chip *chip;
0096 unsigned long *get_mask;
0097 unsigned long *set_mask;
0098 unsigned long invert_mask[];
0099 };
0100
0101 struct gpio_desc *gpiochip_get_desc(struct gpio_chip *gc, unsigned int hwnum);
0102
0103 #define for_each_gpio_desc(gc, desc) \
0104 for (unsigned int __i = 0; \
0105 __i < gc->ngpio && (desc = gpiochip_get_desc(gc, __i)); \
0106 __i++) \
0107
0108 #define for_each_gpio_desc_with_flag(gc, desc, flag) \
0109 for_each_gpio_desc(gc, desc) \
0110 if (!test_bit(flag, &desc->flags)) {} else
0111
0112 int gpiod_get_array_value_complex(bool raw, bool can_sleep,
0113 unsigned int array_size,
0114 struct gpio_desc **desc_array,
0115 struct gpio_array *array_info,
0116 unsigned long *value_bitmap);
0117 int gpiod_set_array_value_complex(bool raw, bool can_sleep,
0118 unsigned int array_size,
0119 struct gpio_desc **desc_array,
0120 struct gpio_array *array_info,
0121 unsigned long *value_bitmap);
0122
0123 extern spinlock_t gpio_lock;
0124 extern struct list_head gpio_devices;
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143 struct gpio_desc {
0144 struct gpio_device *gdev;
0145 unsigned long flags;
0146
0147 #define FLAG_REQUESTED 0
0148 #define FLAG_IS_OUT 1
0149 #define FLAG_EXPORT 2
0150 #define FLAG_SYSFS 3
0151 #define FLAG_ACTIVE_LOW 6
0152 #define FLAG_OPEN_DRAIN 7
0153 #define FLAG_OPEN_SOURCE 8
0154 #define FLAG_USED_AS_IRQ 9
0155 #define FLAG_IRQ_IS_ENABLED 10
0156 #define FLAG_IS_HOGGED 11
0157 #define FLAG_TRANSITORY 12
0158 #define FLAG_PULL_UP 13
0159 #define FLAG_PULL_DOWN 14
0160 #define FLAG_BIAS_DISABLE 15
0161 #define FLAG_EDGE_RISING 16
0162 #define FLAG_EDGE_FALLING 17
0163 #define FLAG_EVENT_CLOCK_REALTIME 18
0164 #define FLAG_EVENT_CLOCK_HTE 19
0165
0166
0167 const char *label;
0168
0169 const char *name;
0170 #ifdef CONFIG_OF_DYNAMIC
0171 struct device_node *hog;
0172 #endif
0173 #ifdef CONFIG_GPIO_CDEV
0174
0175 unsigned int debounce_period_us;
0176 #endif
0177 };
0178
0179 #define gpiod_not_found(desc) (IS_ERR(desc) && PTR_ERR(desc) == -ENOENT)
0180
0181 int gpiod_request(struct gpio_desc *desc, const char *label);
0182 void gpiod_free(struct gpio_desc *desc);
0183
0184 static inline int gpiod_request_user(struct gpio_desc *desc, const char *label)
0185 {
0186 int ret;
0187
0188 ret = gpiod_request(desc, label);
0189 if (ret == -EPROBE_DEFER)
0190 ret = -ENODEV;
0191
0192 return ret;
0193 }
0194
0195 int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
0196 unsigned long lflags, enum gpiod_flags dflags);
0197 int gpio_set_debounce_timeout(struct gpio_desc *desc, unsigned int debounce);
0198 int gpiod_hog(struct gpio_desc *desc, const char *name,
0199 unsigned long lflags, enum gpiod_flags dflags);
0200
0201
0202
0203
0204 static inline int gpio_chip_hwgpio(const struct gpio_desc *desc)
0205 {
0206 return desc - &desc->gdev->descs[0];
0207 }
0208
0209
0210
0211 #define gpiod_emerg(desc, fmt, ...) \
0212 pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
0213 ##__VA_ARGS__)
0214 #define gpiod_crit(desc, fmt, ...) \
0215 pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
0216 ##__VA_ARGS__)
0217 #define gpiod_err(desc, fmt, ...) \
0218 pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
0219 ##__VA_ARGS__)
0220 #define gpiod_warn(desc, fmt, ...) \
0221 pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
0222 ##__VA_ARGS__)
0223 #define gpiod_info(desc, fmt, ...) \
0224 pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
0225 ##__VA_ARGS__)
0226 #define gpiod_dbg(desc, fmt, ...) \
0227 pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
0228 ##__VA_ARGS__)
0229
0230
0231
0232 #define chip_emerg(gc, fmt, ...) \
0233 dev_emerg(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__)
0234 #define chip_crit(gc, fmt, ...) \
0235 dev_crit(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__)
0236 #define chip_err(gc, fmt, ...) \
0237 dev_err(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__)
0238 #define chip_warn(gc, fmt, ...) \
0239 dev_warn(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__)
0240 #define chip_info(gc, fmt, ...) \
0241 dev_info(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__)
0242 #define chip_dbg(gc, fmt, ...) \
0243 dev_dbg(&gc->gpiodev->dev, "(%s): " fmt, gc->label, ##__VA_ARGS__)
0244
0245 #endif