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