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