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 | |
Sascha Hauer | 557fe99 | 2014-01-24 08:54:16 +0100 | [diff] [blame] | 10 | #if IS_ENABLED(CONFIG_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, |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 79 | PWMF_EXPORTED = 1 << 2, |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 80 | }; |
| 81 | |
Thierry Reding | 0488380 | 2015-07-27 11:58:32 +0200 | [diff] [blame] | 82 | /** |
| 83 | * struct pwm_device - PWM channel object |
| 84 | * @label: name of the PWM device |
| 85 | * @flags: flags associated with the PWM device |
| 86 | * @hwpwm: per-chip relative index of the PWM device |
| 87 | * @pwm: global index of the PWM device |
| 88 | * @chip: PWM chip providing this PWM device |
| 89 | * @chip_data: chip-private data associated with the PWM device |
| 90 | * @period: period of the PWM signal (in nanoseconds) |
| 91 | * @duty_cycle: duty cycle of the PWM signal (in nanoseconds) |
| 92 | * @polarity: polarity of the PWM signal |
| 93 | */ |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 94 | struct pwm_device { |
Thierry Reding | 6bc7064 | 2015-07-27 11:57:28 +0200 | [diff] [blame] | 95 | const char *label; |
| 96 | unsigned long flags; |
| 97 | unsigned int hwpwm; |
| 98 | unsigned int pwm; |
| 99 | struct pwm_chip *chip; |
| 100 | void *chip_data; |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 101 | |
Thierry Reding | 6bc7064 | 2015-07-27 11:57:28 +0200 | [diff] [blame] | 102 | unsigned int period; |
| 103 | unsigned int duty_cycle; |
| 104 | enum pwm_polarity polarity; |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 105 | }; |
| 106 | |
Boris Brezillon | 5c31252 | 2015-07-01 10:21:47 +0200 | [diff] [blame] | 107 | static inline bool pwm_is_enabled(const struct pwm_device *pwm) |
| 108 | { |
| 109 | return test_bit(PWMF_ENABLED, &pwm->flags); |
| 110 | } |
| 111 | |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 112 | static inline void pwm_set_period(struct pwm_device *pwm, unsigned int period) |
| 113 | { |
| 114 | if (pwm) |
| 115 | pwm->period = period; |
| 116 | } |
| 117 | |
Boris Brezillon | a1cf421 | 2015-07-01 10:21:48 +0200 | [diff] [blame] | 118 | static inline unsigned int pwm_get_period(const struct pwm_device *pwm) |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 119 | { |
| 120 | return pwm ? pwm->period : 0; |
| 121 | } |
| 122 | |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 123 | static inline void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty) |
| 124 | { |
| 125 | if (pwm) |
| 126 | pwm->duty_cycle = duty; |
| 127 | } |
| 128 | |
Boris Brezillon | a1cf421 | 2015-07-01 10:21:48 +0200 | [diff] [blame] | 129 | static inline unsigned int pwm_get_duty_cycle(const struct pwm_device *pwm) |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 130 | { |
| 131 | return pwm ? pwm->duty_cycle : 0; |
| 132 | } |
| 133 | |
Philip, Avinash | 0aa0869 | 2012-07-24 19:35:32 +0530 | [diff] [blame] | 134 | /* |
| 135 | * pwm_set_polarity - configure the polarity of a PWM signal |
| 136 | */ |
| 137 | int pwm_set_polarity(struct pwm_device *pwm, enum pwm_polarity polarity); |
| 138 | |
Boris Brezillon | 011e763 | 2015-07-01 10:21:49 +0200 | [diff] [blame] | 139 | static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm) |
| 140 | { |
| 141 | return pwm ? pwm->polarity : PWM_POLARITY_NORMAL; |
| 142 | } |
| 143 | |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 144 | /** |
| 145 | * struct pwm_ops - PWM controller operations |
| 146 | * @request: optional hook for requesting a PWM |
| 147 | * @free: optional hook for freeing a PWM |
| 148 | * @config: configure duty cycles and period length for this PWM |
Philip, Avinash | 0aa0869 | 2012-07-24 19:35:32 +0530 | [diff] [blame] | 149 | * @set_polarity: configure the polarity of this PWM |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 150 | * @enable: enable PWM output toggling |
| 151 | * @disable: disable PWM output toggling |
Thierry Reding | 62099ab | 2012-03-26 09:31:48 +0200 | [diff] [blame] | 152 | * @dbg_show: optional routine to show contents in debugfs |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 153 | * @owner: helps prevent removal of modules exporting active PWMs |
| 154 | */ |
| 155 | struct pwm_ops { |
Thierry Reding | 6bc7064 | 2015-07-27 11:57:28 +0200 | [diff] [blame] | 156 | int (*request)(struct pwm_chip *chip, struct pwm_device *pwm); |
| 157 | void (*free)(struct pwm_chip *chip, struct pwm_device *pwm); |
| 158 | int (*config)(struct pwm_chip *chip, struct pwm_device *pwm, |
| 159 | int duty_ns, int period_ns); |
| 160 | int (*set_polarity)(struct pwm_chip *chip, struct pwm_device *pwm, |
| 161 | enum pwm_polarity polarity); |
| 162 | int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm); |
| 163 | void (*disable)(struct pwm_chip *chip, struct pwm_device *pwm); |
Thierry Reding | 62099ab | 2012-03-26 09:31:48 +0200 | [diff] [blame] | 164 | #ifdef CONFIG_DEBUG_FS |
Thierry Reding | 6bc7064 | 2015-07-27 11:57:28 +0200 | [diff] [blame] | 165 | void (*dbg_show)(struct pwm_chip *chip, struct seq_file *s); |
Thierry Reding | 62099ab | 2012-03-26 09:31:48 +0200 | [diff] [blame] | 166 | #endif |
Thierry Reding | 6bc7064 | 2015-07-27 11:57:28 +0200 | [diff] [blame] | 167 | struct module *owner; |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 168 | }; |
| 169 | |
| 170 | /** |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 171 | * struct pwm_chip - abstract a PWM controller |
| 172 | * @dev: device providing the PWMs |
| 173 | * @list: list node for internal use |
| 174 | * @ops: callbacks for this PWM controller |
| 175 | * @base: number of first PWM controlled by this chip |
| 176 | * @npwm: number of PWMs controlled by this chip |
| 177 | * @pwms: array of PWM devices allocated by the framework |
Thierry Reding | 0488380 | 2015-07-27 11:58:32 +0200 | [diff] [blame] | 178 | * @of_xlate: request a PWM device given a device tree PWM specifier |
| 179 | * @of_pwm_n_cells: number of cells expected in the device tree PWM specifier |
Florian Vaussard | 6e69ab1 | 2013-01-28 15:00:57 +0100 | [diff] [blame] | 180 | * @can_sleep: must be true if the .config(), .enable() or .disable() |
| 181 | * operations may sleep |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 182 | */ |
| 183 | struct pwm_chip { |
Thierry Reding | 6bc7064 | 2015-07-27 11:57:28 +0200 | [diff] [blame] | 184 | struct device *dev; |
| 185 | struct list_head list; |
| 186 | const struct pwm_ops *ops; |
| 187 | int base; |
| 188 | unsigned int npwm; |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 189 | |
Thierry Reding | 6bc7064 | 2015-07-27 11:57:28 +0200 | [diff] [blame] | 190 | struct pwm_device *pwms; |
Thierry Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 191 | |
Thierry Reding | 6bc7064 | 2015-07-27 11:57:28 +0200 | [diff] [blame] | 192 | struct pwm_device * (*of_xlate)(struct pwm_chip *pc, |
| 193 | const struct of_phandle_args *args); |
| 194 | unsigned int of_pwm_n_cells; |
| 195 | bool can_sleep; |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 196 | }; |
| 197 | |
Tushar Behera | 0bcf168 | 2012-09-12 15:31:46 +0530 | [diff] [blame] | 198 | #if IS_ENABLED(CONFIG_PWM) |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 199 | int pwm_set_chip_data(struct pwm_device *pwm, void *data); |
| 200 | void *pwm_get_chip_data(struct pwm_device *pwm); |
| 201 | |
Tim Kryger | b6a00fa | 2015-05-26 13:08:16 -0700 | [diff] [blame] | 202 | int pwmchip_add_with_polarity(struct pwm_chip *chip, |
| 203 | enum pwm_polarity polarity); |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 204 | int pwmchip_add(struct pwm_chip *chip); |
| 205 | int pwmchip_remove(struct pwm_chip *chip); |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 206 | struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip, |
| 207 | unsigned int index, |
| 208 | const char *label); |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 209 | |
Philip, Avinash | 83af240 | 2012-11-21 13:10:44 +0530 | [diff] [blame] | 210 | struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *pc, |
| 211 | const struct of_phandle_args *args); |
| 212 | |
Peter Ujfalusi | d4c0c47 | 2012-12-21 01:43:57 -0800 | [diff] [blame] | 213 | struct pwm_device *pwm_get(struct device *dev, const char *con_id); |
Peter Ujfalusi | 8eb9612 | 2012-12-21 01:43:58 -0800 | [diff] [blame] | 214 | struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id); |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 215 | void pwm_put(struct pwm_device *pwm); |
| 216 | |
Peter Ujfalusi | d4c0c47 | 2012-12-21 01:43:57 -0800 | [diff] [blame] | 217 | struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id); |
Peter Ujfalusi | 261a5ed | 2012-12-21 01:43:59 -0800 | [diff] [blame] | 218 | struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np, |
| 219 | const char *con_id); |
Alexandre Courbot | 6354316 | 2012-08-01 19:20:58 +0900 | [diff] [blame] | 220 | void devm_pwm_put(struct device *dev, struct pwm_device *pwm); |
Florian Vaussard | 6e69ab1 | 2013-01-28 15:00:57 +0100 | [diff] [blame] | 221 | |
| 222 | bool pwm_can_sleep(struct pwm_device *pwm); |
Tushar Behera | 0bcf168 | 2012-09-12 15:31:46 +0530 | [diff] [blame] | 223 | #else |
| 224 | static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data) |
| 225 | { |
| 226 | return -EINVAL; |
| 227 | } |
| 228 | |
| 229 | static inline void *pwm_get_chip_data(struct pwm_device *pwm) |
| 230 | { |
| 231 | return NULL; |
| 232 | } |
| 233 | |
| 234 | static inline int pwmchip_add(struct pwm_chip *chip) |
| 235 | { |
| 236 | return -EINVAL; |
| 237 | } |
| 238 | |
Tim Kryger | b6a00fa | 2015-05-26 13:08:16 -0700 | [diff] [blame] | 239 | static inline int pwmchip_add_inversed(struct pwm_chip *chip) |
| 240 | { |
| 241 | return -EINVAL; |
| 242 | } |
| 243 | |
Tushar Behera | 0bcf168 | 2012-09-12 15:31:46 +0530 | [diff] [blame] | 244 | static inline int pwmchip_remove(struct pwm_chip *chip) |
| 245 | { |
| 246 | return -EINVAL; |
| 247 | } |
| 248 | |
| 249 | static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip, |
| 250 | unsigned int index, |
| 251 | const char *label) |
| 252 | { |
| 253 | return ERR_PTR(-ENODEV); |
| 254 | } |
| 255 | |
| 256 | static inline struct pwm_device *pwm_get(struct device *dev, |
| 257 | const char *consumer) |
| 258 | { |
| 259 | return ERR_PTR(-ENODEV); |
| 260 | } |
| 261 | |
Peter Ujfalusi | 8eb9612 | 2012-12-21 01:43:58 -0800 | [diff] [blame] | 262 | static inline struct pwm_device *of_pwm_get(struct device_node *np, |
| 263 | const char *con_id) |
| 264 | { |
| 265 | return ERR_PTR(-ENODEV); |
| 266 | } |
| 267 | |
Tushar Behera | 0bcf168 | 2012-09-12 15:31:46 +0530 | [diff] [blame] | 268 | static inline void pwm_put(struct pwm_device *pwm) |
| 269 | { |
| 270 | } |
| 271 | |
| 272 | static inline struct pwm_device *devm_pwm_get(struct device *dev, |
| 273 | const char *consumer) |
| 274 | { |
| 275 | return ERR_PTR(-ENODEV); |
| 276 | } |
| 277 | |
Peter Ujfalusi | 261a5ed | 2012-12-21 01:43:59 -0800 | [diff] [blame] | 278 | static inline struct pwm_device *devm_of_pwm_get(struct device *dev, |
| 279 | struct device_node *np, |
| 280 | const char *con_id) |
| 281 | { |
| 282 | return ERR_PTR(-ENODEV); |
| 283 | } |
| 284 | |
Tushar Behera | 0bcf168 | 2012-09-12 15:31:46 +0530 | [diff] [blame] | 285 | static inline void devm_pwm_put(struct device *dev, struct pwm_device *pwm) |
| 286 | { |
| 287 | } |
Florian Vaussard | 6e69ab1 | 2013-01-28 15:00:57 +0100 | [diff] [blame] | 288 | |
| 289 | static inline bool pwm_can_sleep(struct pwm_device *pwm) |
| 290 | { |
| 291 | return false; |
| 292 | } |
Tushar Behera | 0bcf168 | 2012-09-12 15:31:46 +0530 | [diff] [blame] | 293 | #endif |
Alexandre Courbot | 6354316 | 2012-08-01 19:20:58 +0900 | [diff] [blame] | 294 | |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 295 | struct pwm_lookup { |
| 296 | struct list_head list; |
| 297 | const char *provider; |
| 298 | unsigned int index; |
| 299 | const char *dev_id; |
| 300 | const char *con_id; |
Alexandre Belloni | 3796ce1 | 2014-05-19 22:42:32 +0200 | [diff] [blame] | 301 | unsigned int period; |
| 302 | enum pwm_polarity polarity; |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 303 | }; |
| 304 | |
Alexandre Belloni | 4284402 | 2014-05-19 22:42:37 +0200 | [diff] [blame] | 305 | #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \ |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 306 | { \ |
| 307 | .provider = _provider, \ |
| 308 | .index = _index, \ |
| 309 | .dev_id = _dev_id, \ |
| 310 | .con_id = _con_id, \ |
Alexandre Belloni | 4284402 | 2014-05-19 22:42:37 +0200 | [diff] [blame] | 311 | .period = _period, \ |
| 312 | .polarity = _polarity \ |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 313 | } |
| 314 | |
Tushar Behera | 0bcf168 | 2012-09-12 15:31:46 +0530 | [diff] [blame] | 315 | #if IS_ENABLED(CONFIG_PWM) |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 316 | void pwm_add_table(struct pwm_lookup *table, size_t num); |
Shobhit Kumar | efb0de5 | 2015-05-05 15:04:18 +0530 | [diff] [blame] | 317 | void pwm_remove_table(struct pwm_lookup *table, size_t num); |
Tushar Behera | 0bcf168 | 2012-09-12 15:31:46 +0530 | [diff] [blame] | 318 | #else |
| 319 | static inline void pwm_add_table(struct pwm_lookup *table, size_t num) |
| 320 | { |
| 321 | } |
Shobhit Kumar | efb0de5 | 2015-05-05 15:04:18 +0530 | [diff] [blame] | 322 | |
| 323 | static inline void pwm_remove_table(struct pwm_lookup *table, size_t num) |
| 324 | { |
| 325 | } |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 326 | #endif |
| 327 | |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 328 | #ifdef CONFIG_PWM_SYSFS |
| 329 | void pwmchip_sysfs_export(struct pwm_chip *chip); |
| 330 | void pwmchip_sysfs_unexport(struct pwm_chip *chip); |
| 331 | #else |
| 332 | static inline void pwmchip_sysfs_export(struct pwm_chip *chip) |
| 333 | { |
| 334 | } |
| 335 | |
| 336 | static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip) |
| 337 | { |
| 338 | } |
| 339 | #endif /* CONFIG_PWM_SYSFS */ |
| 340 | |
Mark Vels | 5243ef8 | 2009-01-18 18:42:45 +0100 | [diff] [blame] | 341 | #endif /* __LINUX_PWM_H */ |