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> |
Jonathan Richardson | d1cd214 | 2015-10-16 17:40:58 -0700 | [diff] [blame] | 5 | #include <linux/mutex.h> |
Thierry Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 6 | #include <linux/of.h> |
| 7 | |
Thierry Reding | 62099ab | 2012-03-26 09:31:48 +0200 | [diff] [blame] | 8 | struct seq_file; |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 9 | struct pwm_chip; |
| 10 | |
Philip, Avinash | 0aa0869c | 2012-07-24 19:35:32 +0530 | [diff] [blame] | 11 | /** |
| 12 | * enum pwm_polarity - polarity of a PWM signal |
| 13 | * @PWM_POLARITY_NORMAL: a high signal for the duration of the duty- |
| 14 | * cycle, followed by a low signal for the remainder of the pulse |
| 15 | * period |
| 16 | * @PWM_POLARITY_INVERSED: a low signal for the duration of the duty- |
| 17 | * cycle, followed by a high signal for the remainder of the pulse |
| 18 | * period |
| 19 | */ |
| 20 | enum pwm_polarity { |
| 21 | PWM_POLARITY_NORMAL, |
| 22 | PWM_POLARITY_INVERSED, |
| 23 | }; |
| 24 | |
Boris Brezillon | e39c0df | 2016-04-14 21:17:21 +0200 | [diff] [blame] | 25 | /** |
| 26 | * struct pwm_args - board-dependent PWM arguments |
| 27 | * @period: reference period |
| 28 | * @polarity: reference polarity |
| 29 | * |
| 30 | * This structure describes board-dependent arguments attached to a PWM |
| 31 | * device. These arguments are usually retrieved from the PWM lookup table or |
| 32 | * device tree. |
| 33 | * |
| 34 | * Do not confuse this with the PWM state: PWM arguments represent the initial |
| 35 | * configuration that users want to use on this PWM device rather than the |
| 36 | * current PWM hardware state. |
| 37 | */ |
| 38 | struct pwm_args { |
| 39 | unsigned int period; |
| 40 | enum pwm_polarity polarity; |
| 41 | }; |
| 42 | |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 43 | enum { |
| 44 | PWMF_REQUESTED = 1 << 0, |
Boris Brezillon | 09a7e4a | 2016-04-14 21:17:39 +0200 | [diff] [blame] | 45 | PWMF_EXPORTED = 1 << 1, |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 46 | }; |
| 47 | |
Boris Brezillon | 43a276b | 2016-04-14 21:17:38 +0200 | [diff] [blame] | 48 | /* |
| 49 | * struct pwm_state - state of a PWM channel |
| 50 | * @period: PWM period (in nanoseconds) |
| 51 | * @duty_cycle: PWM duty cycle (in nanoseconds) |
| 52 | * @polarity: PWM polarity |
Boris Brezillon | 09a7e4a | 2016-04-14 21:17:39 +0200 | [diff] [blame] | 53 | * @enabled: PWM enabled status |
Boris Brezillon | 43a276b | 2016-04-14 21:17:38 +0200 | [diff] [blame] | 54 | */ |
| 55 | struct pwm_state { |
| 56 | unsigned int period; |
| 57 | unsigned int duty_cycle; |
| 58 | enum pwm_polarity polarity; |
Boris Brezillon | 09a7e4a | 2016-04-14 21:17:39 +0200 | [diff] [blame] | 59 | bool enabled; |
Boris Brezillon | 43a276b | 2016-04-14 21:17:38 +0200 | [diff] [blame] | 60 | }; |
| 61 | |
Thierry Reding | 0488380 | 2015-07-27 11:58:32 +0200 | [diff] [blame] | 62 | /** |
| 63 | * struct pwm_device - PWM channel object |
| 64 | * @label: name of the PWM device |
| 65 | * @flags: flags associated with the PWM device |
| 66 | * @hwpwm: per-chip relative index of the PWM device |
| 67 | * @pwm: global index of the PWM device |
| 68 | * @chip: PWM chip providing this PWM device |
| 69 | * @chip_data: chip-private data associated with the PWM device |
Boris Brezillon | e39c0df | 2016-04-14 21:17:21 +0200 | [diff] [blame] | 70 | * @args: PWM arguments |
Boris Brezillon | 43a276b | 2016-04-14 21:17:38 +0200 | [diff] [blame] | 71 | * @state: curent PWM channel state |
Thierry Reding | 0488380 | 2015-07-27 11:58:32 +0200 | [diff] [blame] | 72 | */ |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 73 | struct pwm_device { |
Thierry Reding | 6bc7064 | 2015-07-27 11:57:28 +0200 | [diff] [blame] | 74 | const char *label; |
| 75 | unsigned long flags; |
| 76 | unsigned int hwpwm; |
| 77 | unsigned int pwm; |
| 78 | struct pwm_chip *chip; |
| 79 | void *chip_data; |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 80 | |
Boris Brezillon | e39c0df | 2016-04-14 21:17:21 +0200 | [diff] [blame] | 81 | struct pwm_args args; |
Boris Brezillon | 43a276b | 2016-04-14 21:17:38 +0200 | [diff] [blame] | 82 | struct pwm_state state; |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 83 | }; |
| 84 | |
Boris Brezillon | 43a276b | 2016-04-14 21:17:38 +0200 | [diff] [blame] | 85 | /** |
| 86 | * pwm_get_state() - retrieve the current PWM state |
| 87 | * @pwm: PWM device |
| 88 | * @state: state to fill with the current PWM state |
| 89 | */ |
| 90 | static inline void pwm_get_state(const struct pwm_device *pwm, |
| 91 | struct pwm_state *state) |
| 92 | { |
| 93 | *state = pwm->state; |
| 94 | } |
| 95 | |
Boris Brezillon | 5c31252 | 2015-07-01 10:21:47 +0200 | [diff] [blame] | 96 | static inline bool pwm_is_enabled(const struct pwm_device *pwm) |
| 97 | { |
Boris Brezillon | 09a7e4a | 2016-04-14 21:17:39 +0200 | [diff] [blame] | 98 | struct pwm_state state; |
| 99 | |
| 100 | pwm_get_state(pwm, &state); |
| 101 | |
| 102 | return state.enabled; |
Boris Brezillon | 5c31252 | 2015-07-01 10:21:47 +0200 | [diff] [blame] | 103 | } |
| 104 | |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 105 | static inline void pwm_set_period(struct pwm_device *pwm, unsigned int period) |
| 106 | { |
| 107 | if (pwm) |
Boris Brezillon | 43a276b | 2016-04-14 21:17:38 +0200 | [diff] [blame] | 108 | pwm->state.period = period; |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 109 | } |
| 110 | |
Boris Brezillon | a1cf421 | 2015-07-01 10:21:48 +0200 | [diff] [blame] | 111 | static inline unsigned int pwm_get_period(const struct pwm_device *pwm) |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 112 | { |
Boris Brezillon | 43a276b | 2016-04-14 21:17:38 +0200 | [diff] [blame] | 113 | struct pwm_state state; |
| 114 | |
| 115 | pwm_get_state(pwm, &state); |
| 116 | |
| 117 | return state.period; |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 118 | } |
| 119 | |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 120 | static inline void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty) |
| 121 | { |
| 122 | if (pwm) |
Boris Brezillon | 43a276b | 2016-04-14 21:17:38 +0200 | [diff] [blame] | 123 | pwm->state.duty_cycle = duty; |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Boris Brezillon | a1cf421 | 2015-07-01 10:21:48 +0200 | [diff] [blame] | 126 | 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] | 127 | { |
Boris Brezillon | 43a276b | 2016-04-14 21:17:38 +0200 | [diff] [blame] | 128 | struct pwm_state state; |
| 129 | |
| 130 | pwm_get_state(pwm, &state); |
| 131 | |
| 132 | return state.duty_cycle; |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Boris Brezillon | 011e763 | 2015-07-01 10:21:49 +0200 | [diff] [blame] | 135 | static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm) |
| 136 | { |
Boris Brezillon | 43a276b | 2016-04-14 21:17:38 +0200 | [diff] [blame] | 137 | struct pwm_state state; |
| 138 | |
| 139 | pwm_get_state(pwm, &state); |
| 140 | |
| 141 | return state.polarity; |
Boris Brezillon | 011e763 | 2015-07-01 10:21:49 +0200 | [diff] [blame] | 142 | } |
| 143 | |
Boris Brezillon | e39c0df | 2016-04-14 21:17:21 +0200 | [diff] [blame] | 144 | static inline void pwm_get_args(const struct pwm_device *pwm, |
| 145 | struct pwm_args *args) |
| 146 | { |
| 147 | *args = pwm->args; |
| 148 | } |
| 149 | |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 150 | /** |
Boris Brezillon | a6a0dbb | 2016-06-14 11:13:09 +0200 | [diff] [blame^] | 151 | * pwm_init_state() - prepare a new state to be applied with pwm_apply_state() |
| 152 | * @pwm: PWM device |
| 153 | * @state: state to fill with the prepared PWM state |
| 154 | * |
| 155 | * This functions prepares a state that can later be tweaked and applied |
| 156 | * to the PWM device with pwm_apply_state(). This is a convenient function |
| 157 | * that first retrieves the current PWM state and the replaces the period |
| 158 | * and polarity fields with the reference values defined in pwm->args. |
| 159 | * Once the function returns, you can adjust the ->enabled and ->duty_cycle |
| 160 | * fields according to your needs before calling pwm_apply_state(). |
| 161 | * |
| 162 | * ->duty_cycle is initially set to zero to avoid cases where the current |
| 163 | * ->duty_cycle value exceed the pwm_args->period one, which would trigger |
| 164 | * an error if the user calls pwm_apply_state() without adjusting ->duty_cycle |
| 165 | * first. |
| 166 | */ |
| 167 | static inline void pwm_init_state(const struct pwm_device *pwm, |
| 168 | struct pwm_state *state) |
| 169 | { |
| 170 | struct pwm_args args; |
| 171 | |
| 172 | /* First get the current state. */ |
| 173 | pwm_get_state(pwm, state); |
| 174 | |
| 175 | /* Then fill it with the reference config */ |
| 176 | pwm_get_args(pwm, &args); |
| 177 | |
| 178 | state->period = args.period; |
| 179 | state->polarity = args.polarity; |
| 180 | state->duty_cycle = 0; |
| 181 | } |
| 182 | |
| 183 | /** |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 184 | * struct pwm_ops - PWM controller operations |
| 185 | * @request: optional hook for requesting a PWM |
| 186 | * @free: optional hook for freeing a PWM |
| 187 | * @config: configure duty cycles and period length for this PWM |
Philip, Avinash | 0aa0869c | 2012-07-24 19:35:32 +0530 | [diff] [blame] | 188 | * @set_polarity: configure the polarity of this PWM |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 189 | * @enable: enable PWM output toggling |
| 190 | * @disable: disable PWM output toggling |
Boris Brezillon | 5ec803e | 2016-04-14 21:17:41 +0200 | [diff] [blame] | 191 | * @apply: atomically apply a new PWM config. The state argument |
| 192 | * should be adjusted with the real hardware config (if the |
| 193 | * approximate the period or duty_cycle value, state should |
| 194 | * reflect it) |
Boris Brezillon | 15fa8a43 | 2016-04-14 21:17:40 +0200 | [diff] [blame] | 195 | * @get_state: get the current PWM state. This function is only |
| 196 | * called once per PWM device when the PWM chip is |
| 197 | * registered. |
Thierry Reding | 62099ab | 2012-03-26 09:31:48 +0200 | [diff] [blame] | 198 | * @dbg_show: optional routine to show contents in debugfs |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 199 | * @owner: helps prevent removal of modules exporting active PWMs |
| 200 | */ |
| 201 | struct pwm_ops { |
Thierry Reding | 6bc7064 | 2015-07-27 11:57:28 +0200 | [diff] [blame] | 202 | int (*request)(struct pwm_chip *chip, struct pwm_device *pwm); |
| 203 | void (*free)(struct pwm_chip *chip, struct pwm_device *pwm); |
| 204 | int (*config)(struct pwm_chip *chip, struct pwm_device *pwm, |
| 205 | int duty_ns, int period_ns); |
| 206 | int (*set_polarity)(struct pwm_chip *chip, struct pwm_device *pwm, |
| 207 | enum pwm_polarity polarity); |
| 208 | int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm); |
| 209 | void (*disable)(struct pwm_chip *chip, struct pwm_device *pwm); |
Boris Brezillon | 5ec803e | 2016-04-14 21:17:41 +0200 | [diff] [blame] | 210 | int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm, |
| 211 | struct pwm_state *state); |
Boris Brezillon | 15fa8a43 | 2016-04-14 21:17:40 +0200 | [diff] [blame] | 212 | void (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm, |
| 213 | struct pwm_state *state); |
Thierry Reding | 62099ab | 2012-03-26 09:31:48 +0200 | [diff] [blame] | 214 | #ifdef CONFIG_DEBUG_FS |
Thierry Reding | 6bc7064 | 2015-07-27 11:57:28 +0200 | [diff] [blame] | 215 | void (*dbg_show)(struct pwm_chip *chip, struct seq_file *s); |
Thierry Reding | 62099ab | 2012-03-26 09:31:48 +0200 | [diff] [blame] | 216 | #endif |
Thierry Reding | 6bc7064 | 2015-07-27 11:57:28 +0200 | [diff] [blame] | 217 | struct module *owner; |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 218 | }; |
| 219 | |
| 220 | /** |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 221 | * struct pwm_chip - abstract a PWM controller |
| 222 | * @dev: device providing the PWMs |
| 223 | * @list: list node for internal use |
| 224 | * @ops: callbacks for this PWM controller |
| 225 | * @base: number of first PWM controlled by this chip |
| 226 | * @npwm: number of PWMs controlled by this chip |
| 227 | * @pwms: array of PWM devices allocated by the framework |
Thierry Reding | 0488380 | 2015-07-27 11:58:32 +0200 | [diff] [blame] | 228 | * @of_xlate: request a PWM device given a device tree PWM specifier |
| 229 | * @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] | 230 | * @can_sleep: must be true if the .config(), .enable() or .disable() |
| 231 | * operations may sleep |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 232 | */ |
| 233 | struct pwm_chip { |
Thierry Reding | 6bc7064 | 2015-07-27 11:57:28 +0200 | [diff] [blame] | 234 | struct device *dev; |
| 235 | struct list_head list; |
| 236 | const struct pwm_ops *ops; |
| 237 | int base; |
| 238 | unsigned int npwm; |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 239 | |
Thierry Reding | 6bc7064 | 2015-07-27 11:57:28 +0200 | [diff] [blame] | 240 | struct pwm_device *pwms; |
Thierry Reding | 7299ab7 | 2011-12-14 11:10:32 +0100 | [diff] [blame] | 241 | |
Thierry Reding | 6bc7064 | 2015-07-27 11:57:28 +0200 | [diff] [blame] | 242 | struct pwm_device * (*of_xlate)(struct pwm_chip *pc, |
| 243 | const struct of_phandle_args *args); |
| 244 | unsigned int of_pwm_n_cells; |
| 245 | bool can_sleep; |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 246 | }; |
| 247 | |
Tushar Behera | 0bcf168 | 2012-09-12 15:31:46 +0530 | [diff] [blame] | 248 | #if IS_ENABLED(CONFIG_PWM) |
Boris Brezillon | 5ec803e | 2016-04-14 21:17:41 +0200 | [diff] [blame] | 249 | /* PWM user APIs */ |
| 250 | struct pwm_device *pwm_request(int pwm_id, const char *label); |
| 251 | void pwm_free(struct pwm_device *pwm); |
| 252 | int pwm_apply_state(struct pwm_device *pwm, struct pwm_state *state); |
| 253 | int pwm_adjust_config(struct pwm_device *pwm); |
| 254 | |
| 255 | /** |
| 256 | * pwm_config() - change a PWM device configuration |
| 257 | * @pwm: PWM device |
| 258 | * @duty_ns: "on" time (in nanoseconds) |
| 259 | * @period_ns: duration (in nanoseconds) of one cycle |
| 260 | * |
| 261 | * Returns: 0 on success or a negative error code on failure. |
| 262 | */ |
| 263 | static inline int pwm_config(struct pwm_device *pwm, int duty_ns, |
| 264 | int period_ns) |
| 265 | { |
| 266 | struct pwm_state state; |
| 267 | |
| 268 | if (!pwm) |
| 269 | return -EINVAL; |
| 270 | |
| 271 | pwm_get_state(pwm, &state); |
| 272 | if (state.duty_cycle == duty_ns && state.period == period_ns) |
| 273 | return 0; |
| 274 | |
| 275 | state.duty_cycle = duty_ns; |
| 276 | state.period = period_ns; |
| 277 | return pwm_apply_state(pwm, &state); |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * pwm_set_polarity() - configure the polarity of a PWM signal |
| 282 | * @pwm: PWM device |
| 283 | * @polarity: new polarity of the PWM signal |
| 284 | * |
| 285 | * Note that the polarity cannot be configured while the PWM device is |
| 286 | * enabled. |
| 287 | * |
| 288 | * Returns: 0 on success or a negative error code on failure. |
| 289 | */ |
| 290 | static inline int pwm_set_polarity(struct pwm_device *pwm, |
| 291 | enum pwm_polarity polarity) |
| 292 | { |
| 293 | struct pwm_state state; |
| 294 | |
| 295 | if (!pwm) |
| 296 | return -EINVAL; |
| 297 | |
| 298 | pwm_get_state(pwm, &state); |
| 299 | if (state.polarity == polarity) |
| 300 | return 0; |
| 301 | |
| 302 | /* |
| 303 | * Changing the polarity of a running PWM without adjusting the |
| 304 | * dutycycle/period value is a bit risky (can introduce glitches). |
| 305 | * Return -EBUSY in this case. |
| 306 | * Note that this is allowed when using pwm_apply_state() because |
| 307 | * the user specifies all the parameters. |
| 308 | */ |
| 309 | if (state.enabled) |
| 310 | return -EBUSY; |
| 311 | |
| 312 | state.polarity = polarity; |
| 313 | return pwm_apply_state(pwm, &state); |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * pwm_enable() - start a PWM output toggling |
| 318 | * @pwm: PWM device |
| 319 | * |
| 320 | * Returns: 0 on success or a negative error code on failure. |
| 321 | */ |
| 322 | static inline int pwm_enable(struct pwm_device *pwm) |
| 323 | { |
| 324 | struct pwm_state state; |
| 325 | |
| 326 | if (!pwm) |
| 327 | return -EINVAL; |
| 328 | |
| 329 | pwm_get_state(pwm, &state); |
| 330 | if (state.enabled) |
| 331 | return 0; |
| 332 | |
| 333 | state.enabled = true; |
| 334 | return pwm_apply_state(pwm, &state); |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * pwm_disable() - stop a PWM output toggling |
| 339 | * @pwm: PWM device |
| 340 | */ |
| 341 | static inline void pwm_disable(struct pwm_device *pwm) |
| 342 | { |
| 343 | struct pwm_state state; |
| 344 | |
| 345 | if (!pwm) |
| 346 | return; |
| 347 | |
| 348 | pwm_get_state(pwm, &state); |
| 349 | if (!state.enabled) |
| 350 | return; |
| 351 | |
| 352 | state.enabled = false; |
| 353 | pwm_apply_state(pwm, &state); |
| 354 | } |
| 355 | |
| 356 | |
| 357 | /* PWM provider APIs */ |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 358 | int pwm_set_chip_data(struct pwm_device *pwm, void *data); |
| 359 | void *pwm_get_chip_data(struct pwm_device *pwm); |
| 360 | |
Tim Kryger | b6a00fa | 2015-05-26 13:08:16 -0700 | [diff] [blame] | 361 | int pwmchip_add_with_polarity(struct pwm_chip *chip, |
| 362 | enum pwm_polarity polarity); |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 363 | int pwmchip_add(struct pwm_chip *chip); |
| 364 | int pwmchip_remove(struct pwm_chip *chip); |
Thierry Reding | f051c46 | 2011-12-14 11:12:23 +0100 | [diff] [blame] | 365 | struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip, |
| 366 | unsigned int index, |
| 367 | const char *label); |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 368 | |
Philip, Avinash | 83af240 | 2012-11-21 13:10:44 +0530 | [diff] [blame] | 369 | struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *pc, |
| 370 | const struct of_phandle_args *args); |
| 371 | |
Peter Ujfalusi | d4c0c47 | 2012-12-21 01:43:57 -0800 | [diff] [blame] | 372 | struct pwm_device *pwm_get(struct device *dev, const char *con_id); |
Peter Ujfalusi | 8eb9612 | 2012-12-21 01:43:58 -0800 | [diff] [blame] | 373 | 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] | 374 | void pwm_put(struct pwm_device *pwm); |
| 375 | |
Peter Ujfalusi | d4c0c47 | 2012-12-21 01:43:57 -0800 | [diff] [blame] | 376 | 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] | 377 | struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np, |
| 378 | const char *con_id); |
Alexandre Courbot | 6354316 | 2012-08-01 19:20:58 +0900 | [diff] [blame] | 379 | void devm_pwm_put(struct device *dev, struct pwm_device *pwm); |
Florian Vaussard | 6e69ab1 | 2013-01-28 15:00:57 +0100 | [diff] [blame] | 380 | |
| 381 | bool pwm_can_sleep(struct pwm_device *pwm); |
Tushar Behera | 0bcf168 | 2012-09-12 15:31:46 +0530 | [diff] [blame] | 382 | #else |
Boris Brezillon | 5ec803e | 2016-04-14 21:17:41 +0200 | [diff] [blame] | 383 | static inline struct pwm_device *pwm_request(int pwm_id, const char *label) |
| 384 | { |
| 385 | return ERR_PTR(-ENODEV); |
| 386 | } |
| 387 | |
| 388 | static inline void pwm_free(struct pwm_device *pwm) |
| 389 | { |
| 390 | } |
| 391 | |
| 392 | static inline int pwm_apply_state(struct pwm_device *pwm, |
| 393 | const struct pwm_state *state) |
| 394 | { |
| 395 | return -ENOTSUPP; |
| 396 | } |
| 397 | |
| 398 | static inline int pwm_adjust_config(struct pwm_device *pwm) |
| 399 | { |
| 400 | return -ENOTSUPP; |
| 401 | } |
| 402 | |
| 403 | static inline int pwm_config(struct pwm_device *pwm, int duty_ns, |
| 404 | int period_ns) |
| 405 | { |
| 406 | return -EINVAL; |
| 407 | } |
| 408 | |
| 409 | static inline int pwm_set_polarity(struct pwm_device *pwm, |
| 410 | enum pwm_polarity polarity) |
| 411 | { |
| 412 | return -ENOTSUPP; |
| 413 | } |
| 414 | |
| 415 | static inline int pwm_enable(struct pwm_device *pwm) |
| 416 | { |
| 417 | return -EINVAL; |
| 418 | } |
| 419 | |
| 420 | static inline void pwm_disable(struct pwm_device *pwm) |
| 421 | { |
| 422 | } |
| 423 | |
Tushar Behera | 0bcf168 | 2012-09-12 15:31:46 +0530 | [diff] [blame] | 424 | static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data) |
| 425 | { |
| 426 | return -EINVAL; |
| 427 | } |
| 428 | |
| 429 | static inline void *pwm_get_chip_data(struct pwm_device *pwm) |
| 430 | { |
| 431 | return NULL; |
| 432 | } |
| 433 | |
| 434 | static inline int pwmchip_add(struct pwm_chip *chip) |
| 435 | { |
| 436 | return -EINVAL; |
| 437 | } |
| 438 | |
Tim Kryger | b6a00fa | 2015-05-26 13:08:16 -0700 | [diff] [blame] | 439 | static inline int pwmchip_add_inversed(struct pwm_chip *chip) |
| 440 | { |
| 441 | return -EINVAL; |
| 442 | } |
| 443 | |
Tushar Behera | 0bcf168 | 2012-09-12 15:31:46 +0530 | [diff] [blame] | 444 | static inline int pwmchip_remove(struct pwm_chip *chip) |
| 445 | { |
| 446 | return -EINVAL; |
| 447 | } |
| 448 | |
| 449 | static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip, |
| 450 | unsigned int index, |
| 451 | const char *label) |
| 452 | { |
| 453 | return ERR_PTR(-ENODEV); |
| 454 | } |
| 455 | |
| 456 | static inline struct pwm_device *pwm_get(struct device *dev, |
| 457 | const char *consumer) |
| 458 | { |
| 459 | return ERR_PTR(-ENODEV); |
| 460 | } |
| 461 | |
Peter Ujfalusi | 8eb9612 | 2012-12-21 01:43:58 -0800 | [diff] [blame] | 462 | static inline struct pwm_device *of_pwm_get(struct device_node *np, |
| 463 | const char *con_id) |
| 464 | { |
| 465 | return ERR_PTR(-ENODEV); |
| 466 | } |
| 467 | |
Tushar Behera | 0bcf168 | 2012-09-12 15:31:46 +0530 | [diff] [blame] | 468 | static inline void pwm_put(struct pwm_device *pwm) |
| 469 | { |
| 470 | } |
| 471 | |
| 472 | static inline struct pwm_device *devm_pwm_get(struct device *dev, |
| 473 | const char *consumer) |
| 474 | { |
| 475 | return ERR_PTR(-ENODEV); |
| 476 | } |
| 477 | |
Peter Ujfalusi | 261a5ed | 2012-12-21 01:43:59 -0800 | [diff] [blame] | 478 | static inline struct pwm_device *devm_of_pwm_get(struct device *dev, |
| 479 | struct device_node *np, |
| 480 | const char *con_id) |
| 481 | { |
| 482 | return ERR_PTR(-ENODEV); |
| 483 | } |
| 484 | |
Tushar Behera | 0bcf168 | 2012-09-12 15:31:46 +0530 | [diff] [blame] | 485 | static inline void devm_pwm_put(struct device *dev, struct pwm_device *pwm) |
| 486 | { |
| 487 | } |
Florian Vaussard | 6e69ab1 | 2013-01-28 15:00:57 +0100 | [diff] [blame] | 488 | |
| 489 | static inline bool pwm_can_sleep(struct pwm_device *pwm) |
| 490 | { |
| 491 | return false; |
| 492 | } |
Tushar Behera | 0bcf168 | 2012-09-12 15:31:46 +0530 | [diff] [blame] | 493 | #endif |
Alexandre Courbot | 6354316 | 2012-08-01 19:20:58 +0900 | [diff] [blame] | 494 | |
Boris Brezillon | 5ec803e | 2016-04-14 21:17:41 +0200 | [diff] [blame] | 495 | static inline void pwm_apply_args(struct pwm_device *pwm) |
| 496 | { |
| 497 | /* |
| 498 | * PWM users calling pwm_apply_args() expect to have a fresh config |
| 499 | * where the polarity and period are set according to pwm_args info. |
| 500 | * The problem is, polarity can only be changed when the PWM is |
| 501 | * disabled. |
| 502 | * |
| 503 | * PWM drivers supporting hardware readout may declare the PWM device |
| 504 | * as enabled, and prevent polarity setting, which changes from the |
| 505 | * existing behavior, where all PWM devices are declared as disabled |
| 506 | * at startup (even if they are actually enabled), thus authorizing |
| 507 | * polarity setting. |
| 508 | * |
| 509 | * Instead of setting ->enabled to false, we call pwm_disable() |
| 510 | * before pwm_set_polarity() to ensure that everything is configured |
| 511 | * as expected, and the PWM is really disabled when the user request |
| 512 | * it. |
| 513 | * |
| 514 | * Note that PWM users requiring a smooth handover between the |
| 515 | * bootloader and the kernel (like critical regulators controlled by |
| 516 | * PWM devices) will have to switch to the atomic API and avoid calling |
| 517 | * pwm_apply_args(). |
| 518 | */ |
| 519 | pwm_disable(pwm); |
| 520 | pwm_set_polarity(pwm, pwm->args.polarity); |
| 521 | } |
| 522 | |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 523 | struct pwm_lookup { |
| 524 | struct list_head list; |
| 525 | const char *provider; |
| 526 | unsigned int index; |
| 527 | const char *dev_id; |
| 528 | const char *con_id; |
Alexandre Belloni | 3796ce1 | 2014-05-19 22:42:32 +0200 | [diff] [blame] | 529 | unsigned int period; |
| 530 | enum pwm_polarity polarity; |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 531 | }; |
| 532 | |
Alexandre Belloni | 4284402 | 2014-05-19 22:42:37 +0200 | [diff] [blame] | 533 | #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \ |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 534 | { \ |
| 535 | .provider = _provider, \ |
| 536 | .index = _index, \ |
| 537 | .dev_id = _dev_id, \ |
| 538 | .con_id = _con_id, \ |
Alexandre Belloni | 4284402 | 2014-05-19 22:42:37 +0200 | [diff] [blame] | 539 | .period = _period, \ |
| 540 | .polarity = _polarity \ |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 541 | } |
| 542 | |
Tushar Behera | 0bcf168 | 2012-09-12 15:31:46 +0530 | [diff] [blame] | 543 | #if IS_ENABLED(CONFIG_PWM) |
Thierry Reding | 8138d2d | 2012-03-26 08:42:48 +0200 | [diff] [blame] | 544 | void pwm_add_table(struct pwm_lookup *table, size_t num); |
Shobhit Kumar | efb0de5 | 2015-05-05 15:04:18 +0530 | [diff] [blame] | 545 | void pwm_remove_table(struct pwm_lookup *table, size_t num); |
Tushar Behera | 0bcf168 | 2012-09-12 15:31:46 +0530 | [diff] [blame] | 546 | #else |
| 547 | static inline void pwm_add_table(struct pwm_lookup *table, size_t num) |
| 548 | { |
| 549 | } |
Shobhit Kumar | efb0de5 | 2015-05-05 15:04:18 +0530 | [diff] [blame] | 550 | |
| 551 | static inline void pwm_remove_table(struct pwm_lookup *table, size_t num) |
| 552 | { |
| 553 | } |
Sascha Hauer | 0c2498f | 2011-01-28 09:40:40 +0100 | [diff] [blame] | 554 | #endif |
| 555 | |
H Hartley Sweeten | 76abbdde | 2013-06-11 10:38:59 -0700 | [diff] [blame] | 556 | #ifdef CONFIG_PWM_SYSFS |
| 557 | void pwmchip_sysfs_export(struct pwm_chip *chip); |
| 558 | void pwmchip_sysfs_unexport(struct pwm_chip *chip); |
| 559 | #else |
| 560 | static inline void pwmchip_sysfs_export(struct pwm_chip *chip) |
| 561 | { |
| 562 | } |
| 563 | |
| 564 | static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip) |
| 565 | { |
| 566 | } |
| 567 | #endif /* CONFIG_PWM_SYSFS */ |
| 568 | |
Mark Vels | 5243ef8 | 2009-01-18 18:42:45 +0100 | [diff] [blame] | 569 | #endif /* __LINUX_PWM_H */ |