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