Russell King | 1a189b9 | 2008-04-13 21:41:55 +0100 | [diff] [blame] | 1 | #ifndef __LINUX_PWM_H |
| 2 | #define __LINUX_PWM_H |
| 3 | |
Tushar Behera | 0bcf168 | 2012-09-12 15:31:46 +0530 | [diff] [blame] | 4 | #include <linux/err.h> |
Thierry Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 5 | #include <linux/of.h> |
| 6 | |
Russell King | 1a189b9 | 2008-04-13 21:41:55 +0100 | [diff] [blame] | 7 | struct pwm_device; |
Thierry Reding | 62099ab | 2012-03-26 09:31:48 +0200 | [diff] [blame] | 8 | struct seq_file; |
Russell King | 1a189b9 | 2008-04-13 21:41:55 +0100 | [diff] [blame] | 9 | |
Tushar Behera | 0bcf168 | 2012-09-12 15:31:46 +0530 | [diff] [blame] | 10 | #if IS_ENABLED(CONFIG_PWM) || IS_ENABLED(CONFIG_HAVE_PWM) |
Russell King | 1a189b9 | 2008-04-13 21:41:55 +0100 | [diff] [blame] | 11 | /* |
| 12 | * pwm_request - request a PWM device |
| 13 | */ |
| 14 | struct pwm_device *pwm_request(int pwm_id, const char *label); |
| 15 | |
| 16 | /* |
| 17 | * pwm_free - free a PWM device |
| 18 | */ |
| 19 | void pwm_free(struct pwm_device *pwm); |
| 20 | |
| 21 | /* |
| 22 | * pwm_config - change a PWM device configuration |
| 23 | */ |
| 24 | int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns); |
| 25 | |
| 26 | /* |
| 27 | * pwm_enable - start a PWM output toggling |
| 28 | */ |
| 29 | int pwm_enable(struct pwm_device *pwm); |
| 30 | |
| 31 | /* |
| 32 | * pwm_disable - stop a PWM output toggling |
| 33 | */ |
| 34 | void pwm_disable(struct pwm_device *pwm); |
Tushar Behera | 0bcf168 | 2012-09-12 15:31:46 +0530 | [diff] [blame] | 35 | #else |
| 36 | static inline struct pwm_device *pwm_request(int pwm_id, const char *label) |
| 37 | { |
| 38 | return ERR_PTR(-ENODEV); |
| 39 | } |
Russell King | 1a189b9 | 2008-04-13 21:41:55 +0100 | [diff] [blame] | 40 | |
Tushar Behera | 0bcf168 | 2012-09-12 15:31:46 +0530 | [diff] [blame] | 41 | static inline void pwm_free(struct pwm_device *pwm) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | static inline int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns) |
| 46 | { |
| 47 | return -EINVAL; |
| 48 | } |
| 49 | |
| 50 | static inline int pwm_enable(struct pwm_device *pwm) |
| 51 | { |
| 52 | return -EINVAL; |
| 53 | } |
| 54 | |
| 55 | static inline void pwm_disable(struct pwm_device *pwm) |
| 56 | { |
| 57 | } |
| 58 | #endif |
| 59 | |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 60 | struct pwm_chip; |
| 61 | |
Philip, Avinash | 0aa0869 | 2012-07-24 19:35:32 +0530 | [diff] [blame] | 62 | /** |
| 63 | * enum pwm_polarity - polarity of a PWM signal |
| 64 | * @PWM_POLARITY_NORMAL: a high signal for the duration of the duty- |
| 65 | * cycle, followed by a low signal for the remainder of the pulse |
| 66 | * period |
| 67 | * @PWM_POLARITY_INVERSED: a low signal for the duration of the duty- |
| 68 | * cycle, followed by a high signal for the remainder of the pulse |
| 69 | * period |
| 70 | */ |
| 71 | enum pwm_polarity { |
| 72 | PWM_POLARITY_NORMAL, |
| 73 | PWM_POLARITY_INVERSED, |
| 74 | }; |
| 75 | |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 76 | enum { |
| 77 | PWMF_REQUESTED = 1 << 0, |
| 78 | PWMF_ENABLED = 1 << 1, |
| 79 | }; |
| 80 | |
| 81 | struct pwm_device { |
| 82 | const char *label; |
| 83 | unsigned long flags; |
| 84 | unsigned int hwpwm; |
| 85 | unsigned int pwm; |
| 86 | struct pwm_chip *chip; |
| 87 | void *chip_data; |
| 88 | |
| 89 | unsigned int period; /* in nanoseconds */ |
| 90 | }; |
| 91 | |
| 92 | static inline void pwm_set_period(struct pwm_device *pwm, unsigned int period) |
| 93 | { |
| 94 | if (pwm) |
| 95 | pwm->period = period; |
| 96 | } |
| 97 | |
| 98 | static inline unsigned int pwm_get_period(struct pwm_device *pwm) |
| 99 | { |
| 100 | return pwm ? pwm->period : 0; |
| 101 | } |
| 102 | |
Philip, Avinash | 0aa0869 | 2012-07-24 19:35:32 +0530 | [diff] [blame] | 103 | /* |
| 104 | * pwm_set_polarity - configure the polarity of a PWM signal |
| 105 | */ |
| 106 | int pwm_set_polarity(struct pwm_device *pwm, enum pwm_polarity polarity); |
| 107 | |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 108 | /** |
| 109 | * struct pwm_ops - PWM controller operations |
| 110 | * @request: optional hook for requesting a PWM |
| 111 | * @free: optional hook for freeing a PWM |
| 112 | * @config: configure duty cycles and period length for this PWM |
Philip, Avinash | 0aa0869 | 2012-07-24 19:35:32 +0530 | [diff] [blame] | 113 | * @set_polarity: configure the polarity of this PWM |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 114 | * @enable: enable PWM output toggling |
| 115 | * @disable: disable PWM output toggling |
Thierry Reding | 62099ab | 2012-03-26 09:31:48 +0200 | [diff] [blame] | 116 | * @dbg_show: optional routine to show contents in debugfs |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 117 | * @owner: helps prevent removal of modules exporting active PWMs |
| 118 | */ |
| 119 | struct pwm_ops { |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 120 | int (*request)(struct pwm_chip *chip, |
| 121 | struct pwm_device *pwm); |
| 122 | void (*free)(struct pwm_chip *chip, |
| 123 | struct pwm_device *pwm); |
| 124 | int (*config)(struct pwm_chip *chip, |
| 125 | struct pwm_device *pwm, |
| 126 | int duty_ns, int period_ns); |
Philip, Avinash | 0aa0869 | 2012-07-24 19:35:32 +0530 | [diff] [blame] | 127 | int (*set_polarity)(struct pwm_chip *chip, |
| 128 | struct pwm_device *pwm, |
| 129 | enum pwm_polarity polarity); |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 130 | int (*enable)(struct pwm_chip *chip, |
| 131 | struct pwm_device *pwm); |
| 132 | void (*disable)(struct pwm_chip *chip, |
| 133 | struct pwm_device *pwm); |
Thierry Reding | 62099ab | 2012-03-26 09:31:48 +0200 | [diff] [blame] | 134 | #ifdef CONFIG_DEBUG_FS |
| 135 | void (*dbg_show)(struct pwm_chip *chip, |
| 136 | struct seq_file *s); |
| 137 | #endif |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 138 | struct module *owner; |
| 139 | }; |
| 140 | |
| 141 | /** |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 142 | * struct pwm_chip - abstract a PWM controller |
| 143 | * @dev: device providing the PWMs |
| 144 | * @list: list node for internal use |
| 145 | * @ops: callbacks for this PWM controller |
| 146 | * @base: number of first PWM controlled by this chip |
| 147 | * @npwm: number of PWMs controlled by this chip |
| 148 | * @pwms: array of PWM devices allocated by the framework |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 149 | */ |
| 150 | struct pwm_chip { |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 151 | struct device *dev; |
| 152 | struct list_head list; |
| 153 | const struct pwm_ops *ops; |
| 154 | int base; |
| 155 | unsigned int npwm; |
| 156 | |
| 157 | struct pwm_device *pwms; |
Thierry Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 158 | |
| 159 | struct pwm_device * (*of_xlate)(struct pwm_chip *pc, |
| 160 | const struct of_phandle_args *args); |
| 161 | unsigned int of_pwm_n_cells; |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 162 | }; |
| 163 | |
Tushar Behera | 0bcf168 | 2012-09-12 15:31:46 +0530 | [diff] [blame] | 164 | #if IS_ENABLED(CONFIG_PWM) |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 165 | int pwm_set_chip_data(struct pwm_device *pwm, void *data); |
| 166 | void *pwm_get_chip_data(struct pwm_device *pwm); |
| 167 | |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 168 | int pwmchip_add(struct pwm_chip *chip); |
| 169 | int pwmchip_remove(struct pwm_chip *chip); |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 170 | struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip, |
| 171 | unsigned int index, |
| 172 | const char *label); |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 173 | |
Philip, Avinash | 83af240 | 2012-11-21 13:10:44 +0530 | [diff] [blame] | 174 | struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *pc, |
| 175 | const struct of_phandle_args *args); |
| 176 | |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 177 | struct pwm_device *pwm_get(struct device *dev, const char *consumer); |
| 178 | void pwm_put(struct pwm_device *pwm); |
| 179 | |
Alexandre Courbot | 6354316 | 2012-08-01 19:20:58 +0900 | [diff] [blame] | 180 | struct pwm_device *devm_pwm_get(struct device *dev, const char *consumer); |
| 181 | void devm_pwm_put(struct device *dev, struct pwm_device *pwm); |
Tushar Behera | 0bcf168 | 2012-09-12 15:31:46 +0530 | [diff] [blame] | 182 | #else |
| 183 | static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data) |
| 184 | { |
| 185 | return -EINVAL; |
| 186 | } |
| 187 | |
| 188 | static inline void *pwm_get_chip_data(struct pwm_device *pwm) |
| 189 | { |
| 190 | return NULL; |
| 191 | } |
| 192 | |
| 193 | static inline int pwmchip_add(struct pwm_chip *chip) |
| 194 | { |
| 195 | return -EINVAL; |
| 196 | } |
| 197 | |
| 198 | static inline int pwmchip_remove(struct pwm_chip *chip) |
| 199 | { |
| 200 | return -EINVAL; |
| 201 | } |
| 202 | |
| 203 | static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip, |
| 204 | unsigned int index, |
| 205 | const char *label) |
| 206 | { |
| 207 | return ERR_PTR(-ENODEV); |
| 208 | } |
| 209 | |
| 210 | static inline struct pwm_device *pwm_get(struct device *dev, |
| 211 | const char *consumer) |
| 212 | { |
| 213 | return ERR_PTR(-ENODEV); |
| 214 | } |
| 215 | |
| 216 | static inline void pwm_put(struct pwm_device *pwm) |
| 217 | { |
| 218 | } |
| 219 | |
| 220 | static inline struct pwm_device *devm_pwm_get(struct device *dev, |
| 221 | const char *consumer) |
| 222 | { |
| 223 | return ERR_PTR(-ENODEV); |
| 224 | } |
| 225 | |
| 226 | static inline void devm_pwm_put(struct device *dev, struct pwm_device *pwm) |
| 227 | { |
| 228 | } |
| 229 | #endif |
Alexandre Courbot | 6354316 | 2012-08-01 19:20:58 +0900 | [diff] [blame] | 230 | |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 231 | struct pwm_lookup { |
| 232 | struct list_head list; |
| 233 | const char *provider; |
| 234 | unsigned int index; |
| 235 | const char *dev_id; |
| 236 | const char *con_id; |
| 237 | }; |
| 238 | |
| 239 | #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id) \ |
| 240 | { \ |
| 241 | .provider = _provider, \ |
| 242 | .index = _index, \ |
| 243 | .dev_id = _dev_id, \ |
| 244 | .con_id = _con_id, \ |
| 245 | } |
| 246 | |
Tushar Behera | 0bcf168 | 2012-09-12 15:31:46 +0530 | [diff] [blame] | 247 | #if IS_ENABLED(CONFIG_PWM) |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 248 | void pwm_add_table(struct pwm_lookup *table, size_t num); |
Tushar Behera | 0bcf168 | 2012-09-12 15:31:46 +0530 | [diff] [blame] | 249 | #else |
| 250 | static inline void pwm_add_table(struct pwm_lookup *table, size_t num) |
| 251 | { |
| 252 | } |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 253 | #endif |
| 254 | |
Mark Vels | 5243ef8 | 2009-01-18 18:42:45 +0100 | [diff] [blame] | 255 | #endif /* __LINUX_PWM_H */ |