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