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