Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
1a189b97 RK |
2 | #ifndef __LINUX_PWM_H |
3 | #define __LINUX_PWM_H | |
4 | ||
4c56b143 | 5 | #include <linux/device.h> |
0bcf168b | 6 | #include <linux/err.h> |
33c651a3 | 7 | #include <linux/module.h> |
d1cd2142 | 8 | #include <linux/mutex.h> |
7299ab70 TR |
9 | #include <linux/of.h> |
10 | ||
cdd30ebb | 11 | MODULE_IMPORT_NS("PWM"); |
33c651a3 | 12 | |
0c2498f1 SH |
13 | struct pwm_chip; |
14 | ||
0aa0869c PA |
15 | /** |
16 | * enum pwm_polarity - polarity of a PWM signal | |
17 | * @PWM_POLARITY_NORMAL: a high signal for the duration of the duty- | |
18 | * cycle, followed by a low signal for the remainder of the pulse | |
19 | * period | |
20 | * @PWM_POLARITY_INVERSED: a low signal for the duration of the duty- | |
21 | * cycle, followed by a high signal for the remainder of the pulse | |
22 | * period | |
23 | */ | |
24 | enum pwm_polarity { | |
25 | PWM_POLARITY_NORMAL, | |
26 | PWM_POLARITY_INVERSED, | |
27 | }; | |
28 | ||
e39c0df1 BB |
29 | /** |
30 | * struct pwm_args - board-dependent PWM arguments | |
31 | * @period: reference period | |
32 | * @polarity: reference polarity | |
33 | * | |
34 | * This structure describes board-dependent arguments attached to a PWM | |
35 | * device. These arguments are usually retrieved from the PWM lookup table or | |
36 | * device tree. | |
37 | * | |
38 | * Do not confuse this with the PWM state: PWM arguments represent the initial | |
39 | * configuration that users want to use on this PWM device rather than the | |
40 | * current PWM hardware state. | |
41 | */ | |
42 | struct pwm_args { | |
a9d887dc | 43 | u64 period; |
e39c0df1 BB |
44 | enum pwm_polarity polarity; |
45 | }; | |
46 | ||
f051c466 | 47 | enum { |
d27abbfd DC |
48 | PWMF_REQUESTED = 0, |
49 | PWMF_EXPORTED = 1, | |
f051c466 TR |
50 | }; |
51 | ||
17e40c25 UKK |
52 | /** |
53 | * struct pwm_waveform - description of a PWM waveform | |
54 | * @period_length_ns: PWM period | |
55 | * @duty_length_ns: PWM duty cycle | |
56 | * @duty_offset_ns: offset of the rising edge from the period's start | |
57 | * | |
58 | * This is a representation of a PWM waveform alternative to struct pwm_state | |
59 | * below. It's more expressive than struct pwm_state as it contains a | |
60 | * duty_offset_ns and so can represent offsets other than zero (with .polarity = | |
61 | * PWM_POLARITY_NORMAL) and period - duty_cycle (.polarity = | |
62 | * PWM_POLARITY_INVERSED). | |
63 | * | |
64 | * Note there is no explicit bool for enabled. A "disabled" PWM is represented | |
65 | * by .period_length_ns = 0. Note further that the behaviour of a "disabled" PWM | |
66 | * is undefined. Depending on the hardware's capabilities it might drive the | |
67 | * active or inactive level, go high-z or even continue to toggle. | |
68 | * | |
69 | * The unit for all three members is nanoseconds. | |
70 | */ | |
71 | struct pwm_waveform { | |
72 | u64 period_length_ns; | |
73 | u64 duty_length_ns; | |
74 | u64 duty_offset_ns; | |
75 | }; | |
76 | ||
43a276b0 BB |
77 | /* |
78 | * struct pwm_state - state of a PWM channel | |
79 | * @period: PWM period (in nanoseconds) | |
80 | * @duty_cycle: PWM duty cycle (in nanoseconds) | |
81 | * @polarity: PWM polarity | |
09a7e4a3 | 82 | * @enabled: PWM enabled status |
9e40ee18 CG |
83 | * @usage_power: If set, the PWM driver is only required to maintain the power |
84 | * output but has more freedom regarding signal form. | |
85 | * If supported, the signal can be optimized, for example to | |
86 | * improve EMI by phase shifting individual channels. | |
43a276b0 BB |
87 | */ |
88 | struct pwm_state { | |
a9d887dc GDS |
89 | u64 period; |
90 | u64 duty_cycle; | |
43a276b0 | 91 | enum pwm_polarity polarity; |
09a7e4a3 | 92 | bool enabled; |
9e40ee18 | 93 | bool usage_power; |
43a276b0 BB |
94 | }; |
95 | ||
04883802 TR |
96 | /** |
97 | * struct pwm_device - PWM channel object | |
98 | * @label: name of the PWM device | |
99 | * @flags: flags associated with the PWM device | |
100 | * @hwpwm: per-chip relative index of the PWM device | |
04883802 | 101 | * @chip: PWM chip providing this PWM device |
e39c0df1 | 102 | * @args: PWM arguments |
3ad1f3a3 UKK |
103 | * @state: last applied state |
104 | * @last: last implemented state (for PWM_DEBUG) | |
04883802 | 105 | */ |
f051c466 | 106 | struct pwm_device { |
6bc7064a TR |
107 | const char *label; |
108 | unsigned long flags; | |
109 | unsigned int hwpwm; | |
6bc7064a | 110 | struct pwm_chip *chip; |
6bc7064a | 111 | |
e39c0df1 | 112 | struct pwm_args args; |
43a276b0 | 113 | struct pwm_state state; |
3ad1f3a3 | 114 | struct pwm_state last; |
f051c466 TR |
115 | }; |
116 | ||
43a276b0 BB |
117 | /** |
118 | * pwm_get_state() - retrieve the current PWM state | |
119 | * @pwm: PWM device | |
120 | * @state: state to fill with the current PWM state | |
1a7a6e80 UKK |
121 | * |
122 | * The returned PWM state represents the state that was applied by a previous call to | |
c748a6d7 SY |
123 | * pwm_apply_might_sleep(). Drivers may have to slightly tweak that state before programming it to |
124 | * hardware. If pwm_apply_might_sleep() was never called, this returns either the current hardware | |
1a7a6e80 | 125 | * state (if supported) or the default settings. |
43a276b0 BB |
126 | */ |
127 | static inline void pwm_get_state(const struct pwm_device *pwm, | |
128 | struct pwm_state *state) | |
129 | { | |
130 | *state = pwm->state; | |
131 | } | |
132 | ||
5c31252c BB |
133 | static inline bool pwm_is_enabled(const struct pwm_device *pwm) |
134 | { | |
09a7e4a3 BB |
135 | struct pwm_state state; |
136 | ||
137 | pwm_get_state(pwm, &state); | |
138 | ||
139 | return state.enabled; | |
5c31252c BB |
140 | } |
141 | ||
a9d887dc | 142 | static inline u64 pwm_get_period(const struct pwm_device *pwm) |
f051c466 | 143 | { |
43a276b0 BB |
144 | struct pwm_state state; |
145 | ||
146 | pwm_get_state(pwm, &state); | |
147 | ||
148 | return state.period; | |
f051c466 TR |
149 | } |
150 | ||
a9d887dc | 151 | static inline u64 pwm_get_duty_cycle(const struct pwm_device *pwm) |
76abbdde | 152 | { |
43a276b0 BB |
153 | struct pwm_state state; |
154 | ||
155 | pwm_get_state(pwm, &state); | |
156 | ||
157 | return state.duty_cycle; | |
76abbdde HS |
158 | } |
159 | ||
011e7631 BB |
160 | static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm) |
161 | { | |
43a276b0 BB |
162 | struct pwm_state state; |
163 | ||
164 | pwm_get_state(pwm, &state); | |
165 | ||
166 | return state.polarity; | |
011e7631 BB |
167 | } |
168 | ||
e39c0df1 BB |
169 | static inline void pwm_get_args(const struct pwm_device *pwm, |
170 | struct pwm_args *args) | |
171 | { | |
172 | *args = pwm->args; | |
173 | } | |
174 | ||
a6a0dbbc | 175 | /** |
c748a6d7 | 176 | * pwm_init_state() - prepare a new state to be applied with pwm_apply_might_sleep() |
a6a0dbbc BB |
177 | * @pwm: PWM device |
178 | * @state: state to fill with the prepared PWM state | |
179 | * | |
180 | * This functions prepares a state that can later be tweaked and applied | |
c748a6d7 | 181 | * to the PWM device with pwm_apply_might_sleep(). This is a convenient function |
a6a0dbbc BB |
182 | * that first retrieves the current PWM state and the replaces the period |
183 | * and polarity fields with the reference values defined in pwm->args. | |
184 | * Once the function returns, you can adjust the ->enabled and ->duty_cycle | |
c748a6d7 | 185 | * fields according to your needs before calling pwm_apply_might_sleep(). |
a6a0dbbc BB |
186 | * |
187 | * ->duty_cycle is initially set to zero to avoid cases where the current | |
188 | * ->duty_cycle value exceed the pwm_args->period one, which would trigger | |
c748a6d7 | 189 | * an error if the user calls pwm_apply_might_sleep() without adjusting ->duty_cycle |
a6a0dbbc BB |
190 | * first. |
191 | */ | |
192 | static inline void pwm_init_state(const struct pwm_device *pwm, | |
193 | struct pwm_state *state) | |
194 | { | |
195 | struct pwm_args args; | |
196 | ||
197 | /* First get the current state. */ | |
198 | pwm_get_state(pwm, state); | |
199 | ||
200 | /* Then fill it with the reference config */ | |
201 | pwm_get_args(pwm, &args); | |
202 | ||
203 | state->period = args.period; | |
204 | state->polarity = args.polarity; | |
205 | state->duty_cycle = 0; | |
9e40ee18 | 206 | state->usage_power = false; |
a6a0dbbc BB |
207 | } |
208 | ||
f6f3bddf BB |
209 | /** |
210 | * pwm_get_relative_duty_cycle() - Get a relative duty cycle value | |
211 | * @state: PWM state to extract the duty cycle from | |
212 | * @scale: target scale of the relative duty cycle | |
213 | * | |
214 | * This functions converts the absolute duty cycle stored in @state (expressed | |
215 | * in nanosecond) into a value relative to the period. | |
216 | * | |
217 | * For example if you want to get the duty_cycle expressed in percent, call: | |
218 | * | |
219 | * pwm_get_state(pwm, &state); | |
220 | * duty = pwm_get_relative_duty_cycle(&state, 100); | |
7f8ce4d8 UKK |
221 | * |
222 | * Returns: rounded relative duty cycle multiplied by @scale | |
f6f3bddf BB |
223 | */ |
224 | static inline unsigned int | |
225 | pwm_get_relative_duty_cycle(const struct pwm_state *state, unsigned int scale) | |
226 | { | |
227 | if (!state->period) | |
228 | return 0; | |
229 | ||
230 | return DIV_ROUND_CLOSEST_ULL((u64)state->duty_cycle * scale, | |
231 | state->period); | |
232 | } | |
233 | ||
234 | /** | |
235 | * pwm_set_relative_duty_cycle() - Set a relative duty cycle value | |
236 | * @state: PWM state to fill | |
237 | * @duty_cycle: relative duty cycle value | |
238 | * @scale: scale in which @duty_cycle is expressed | |
239 | * | |
240 | * This functions converts a relative into an absolute duty cycle (expressed | |
241 | * in nanoseconds), and puts the result in state->duty_cycle. | |
242 | * | |
243 | * For example if you want to configure a 50% duty cycle, call: | |
244 | * | |
245 | * pwm_init_state(pwm, &state); | |
246 | * pwm_set_relative_duty_cycle(&state, 50, 100); | |
c748a6d7 | 247 | * pwm_apply_might_sleep(pwm, &state); |
f6f3bddf | 248 | * |
7f8ce4d8 UKK |
249 | * Returns: 0 on success or ``-EINVAL`` if @duty_cycle and/or @scale are |
250 | * inconsistent (@scale == 0 or @duty_cycle > @scale) | |
f6f3bddf BB |
251 | */ |
252 | static inline int | |
253 | pwm_set_relative_duty_cycle(struct pwm_state *state, unsigned int duty_cycle, | |
254 | unsigned int scale) | |
255 | { | |
256 | if (!scale || duty_cycle > scale) | |
257 | return -EINVAL; | |
258 | ||
259 | state->duty_cycle = DIV_ROUND_CLOSEST_ULL((u64)duty_cycle * | |
260 | state->period, | |
261 | scale); | |
262 | ||
263 | return 0; | |
264 | } | |
265 | ||
ef2e35d9 UKK |
266 | /** |
267 | * struct pwm_capture - PWM capture data | |
268 | * @period: period of the PWM signal (in nanoseconds) | |
269 | * @duty_cycle: duty cycle of the PWM signal (in nanoseconds) | |
270 | */ | |
271 | struct pwm_capture { | |
272 | unsigned int period; | |
273 | unsigned int duty_cycle; | |
274 | }; | |
275 | ||
0c2498f1 SH |
276 | /** |
277 | * struct pwm_ops - PWM controller operations | |
278 | * @request: optional hook for requesting a PWM | |
279 | * @free: optional hook for freeing a PWM | |
3a3d1a4e | 280 | * @capture: capture and report PWM signal |
dab9cd4b UKK |
281 | * @sizeof_wfhw: size (in bytes) of driver specific waveform presentation |
282 | * @round_waveform_tohw: convert a struct pwm_waveform to driver specific presentation | |
283 | * @round_waveform_fromhw: convert a driver specific waveform presentation to struct pwm_waveform | |
284 | * @read_waveform: read driver specific waveform presentation from hardware | |
285 | * @write_waveform: write driver specific waveform presentation to hardware | |
27938fd8 | 286 | * @apply: atomically apply a new PWM config |
a96d3659 | 287 | * @get_state: get the current PWM state. |
0c2498f1 SH |
288 | */ |
289 | struct pwm_ops { | |
6bc7064a TR |
290 | int (*request)(struct pwm_chip *chip, struct pwm_device *pwm); |
291 | void (*free)(struct pwm_chip *chip, struct pwm_device *pwm); | |
3a3d1a4e LJ |
292 | int (*capture)(struct pwm_chip *chip, struct pwm_device *pwm, |
293 | struct pwm_capture *result, unsigned long timeout); | |
17e40c25 UKK |
294 | |
295 | size_t sizeof_wfhw; | |
296 | int (*round_waveform_tohw)(struct pwm_chip *chip, struct pwm_device *pwm, | |
297 | const struct pwm_waveform *wf, void *wfhw); | |
298 | int (*round_waveform_fromhw)(struct pwm_chip *chip, struct pwm_device *pwm, | |
299 | const void *wfhw, struct pwm_waveform *wf); | |
300 | int (*read_waveform)(struct pwm_chip *chip, struct pwm_device *pwm, | |
301 | void *wfhw); | |
302 | int (*write_waveform)(struct pwm_chip *chip, struct pwm_device *pwm, | |
303 | const void *wfhw); | |
304 | ||
5ec803ed | 305 | int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm, |
71523d18 | 306 | const struct pwm_state *state); |
6c452cff UKK |
307 | int (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm, |
308 | struct pwm_state *state); | |
0c2498f1 SH |
309 | }; |
310 | ||
311 | /** | |
f051c466 TR |
312 | * struct pwm_chip - abstract a PWM controller |
313 | * @dev: device providing the PWMs | |
f051c466 | 314 | * @ops: callbacks for this PWM controller |
384461ab | 315 | * @owner: module providing this chip |
2d91123a | 316 | * @id: unique number of this PWM chip |
f051c466 | 317 | * @npwm: number of PWMs controlled by this chip |
04883802 | 318 | * @of_xlate: request a PWM device given a device tree PWM specifier |
7170d3be | 319 | * @atomic: can the driver's ->apply() be called in atomic context |
05947224 | 320 | * @uses_pwmchip_alloc: signals if pwmchip_allow was used to allocate this chip |
1cc2e1fa UKK |
321 | * @operational: signals if the chip can be used (or is already deregistered) |
322 | * @nonatomic_lock: mutex for nonatomic chips | |
323 | * @atomic_lock: mutex for atomic chips | |
5d0a4c11 | 324 | * @pwms: array of PWM devices allocated by the framework |
0c2498f1 SH |
325 | */ |
326 | struct pwm_chip { | |
4c56b143 | 327 | struct device dev; |
6bc7064a | 328 | const struct pwm_ops *ops; |
384461ab | 329 | struct module *owner; |
54c86dd2 | 330 | unsigned int id; |
6bc7064a TR |
331 | unsigned int npwm; |
332 | ||
b4f78ff7 | 333 | struct pwm_device * (*of_xlate)(struct pwm_chip *chip, |
6bc7064a | 334 | const struct of_phandle_args *args); |
7170d3be | 335 | bool atomic; |
5d0a4c11 UKK |
336 | |
337 | /* only used internally by the PWM framework */ | |
05947224 | 338 | bool uses_pwmchip_alloc; |
1cc2e1fa UKK |
339 | bool operational; |
340 | union { | |
341 | /* | |
342 | * depending on the chip being atomic or not either the mutex or | |
343 | * the spinlock is used. It protects .operational and | |
344 | * synchronizes the callbacks in .ops | |
345 | */ | |
346 | struct mutex nonatomic_lock; | |
347 | spinlock_t atomic_lock; | |
348 | }; | |
ee37bf50 | 349 | struct pwm_device pwms[] __counted_by(npwm); |
0c2498f1 SH |
350 | }; |
351 | ||
da6b3537 UKK |
352 | /** |
353 | * pwmchip_supports_waveform() - checks if the given chip supports waveform callbacks | |
354 | * @chip: The pwm_chip to test | |
355 | * | |
7f8ce4d8 | 356 | * Returns: true iff the pwm chip support the waveform functions like |
da6b3537 UKK |
357 | * pwm_set_waveform_might_sleep() and pwm_round_waveform_might_sleep() |
358 | */ | |
359 | static inline bool pwmchip_supports_waveform(struct pwm_chip *chip) | |
360 | { | |
361 | /* | |
362 | * only check for .write_waveform(). If that is available, | |
363 | * .round_waveform_tohw() and .round_waveform_fromhw() asserted to be | |
364 | * available, too, in pwmchip_add(). | |
365 | */ | |
366 | return chip->ops->write_waveform != NULL; | |
367 | } | |
368 | ||
4e59267c UKK |
369 | static inline struct device *pwmchip_parent(const struct pwm_chip *chip) |
370 | { | |
4c56b143 | 371 | return chip->dev.parent; |
4e59267c UKK |
372 | } |
373 | ||
7cfe1e20 | 374 | static inline void *pwmchip_get_drvdata(const struct pwm_chip *chip) |
24003d50 | 375 | { |
38ae7142 | 376 | return dev_get_drvdata(&chip->dev); |
24003d50 UKK |
377 | } |
378 | ||
379 | static inline void pwmchip_set_drvdata(struct pwm_chip *chip, void *data) | |
380 | { | |
38ae7142 | 381 | dev_set_drvdata(&chip->dev, data); |
24003d50 UKK |
382 | } |
383 | ||
4b31eb55 | 384 | #if IS_REACHABLE(CONFIG_PWM) |
6c5126c6 UKK |
385 | |
386 | /* PWM consumer APIs */ | |
387 | int pwm_round_waveform_might_sleep(struct pwm_device *pwm, struct pwm_waveform *wf); | |
388 | int pwm_get_waveform_might_sleep(struct pwm_device *pwm, struct pwm_waveform *wf); | |
389 | int pwm_set_waveform_might_sleep(struct pwm_device *pwm, const struct pwm_waveform *wf, bool exact); | |
c748a6d7 | 390 | int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state); |
7170d3be | 391 | int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state); |
2ea25aab | 392 | int pwm_get_state_hw(struct pwm_device *pwm, struct pwm_state *state); |
5ec803ed BB |
393 | int pwm_adjust_config(struct pwm_device *pwm); |
394 | ||
395 | /** | |
396 | * pwm_config() - change a PWM device configuration | |
397 | * @pwm: PWM device | |
398 | * @duty_ns: "on" time (in nanoseconds) | |
399 | * @period_ns: duration (in nanoseconds) of one cycle | |
400 | * | |
401 | * Returns: 0 on success or a negative error code on failure. | |
402 | */ | |
403 | static inline int pwm_config(struct pwm_device *pwm, int duty_ns, | |
404 | int period_ns) | |
405 | { | |
406 | struct pwm_state state; | |
407 | ||
408 | if (!pwm) | |
409 | return -EINVAL; | |
410 | ||
ef2bf499 BN |
411 | if (duty_ns < 0 || period_ns < 0) |
412 | return -EINVAL; | |
413 | ||
5ec803ed BB |
414 | pwm_get_state(pwm, &state); |
415 | if (state.duty_cycle == duty_ns && state.period == period_ns) | |
416 | return 0; | |
417 | ||
418 | state.duty_cycle = duty_ns; | |
419 | state.period = period_ns; | |
c748a6d7 | 420 | return pwm_apply_might_sleep(pwm, &state); |
5ec803ed BB |
421 | } |
422 | ||
5ec803ed BB |
423 | /** |
424 | * pwm_enable() - start a PWM output toggling | |
425 | * @pwm: PWM device | |
426 | * | |
427 | * Returns: 0 on success or a negative error code on failure. | |
428 | */ | |
429 | static inline int pwm_enable(struct pwm_device *pwm) | |
430 | { | |
431 | struct pwm_state state; | |
432 | ||
433 | if (!pwm) | |
434 | return -EINVAL; | |
435 | ||
436 | pwm_get_state(pwm, &state); | |
437 | if (state.enabled) | |
438 | return 0; | |
439 | ||
440 | state.enabled = true; | |
c748a6d7 | 441 | return pwm_apply_might_sleep(pwm, &state); |
5ec803ed BB |
442 | } |
443 | ||
444 | /** | |
445 | * pwm_disable() - stop a PWM output toggling | |
446 | * @pwm: PWM device | |
447 | */ | |
448 | static inline void pwm_disable(struct pwm_device *pwm) | |
449 | { | |
450 | struct pwm_state state; | |
451 | ||
452 | if (!pwm) | |
453 | return; | |
454 | ||
455 | pwm_get_state(pwm, &state); | |
456 | if (!state.enabled) | |
457 | return; | |
458 | ||
459 | state.enabled = false; | |
c748a6d7 | 460 | pwm_apply_might_sleep(pwm, &state); |
5ec803ed BB |
461 | } |
462 | ||
7170d3be SY |
463 | /** |
464 | * pwm_might_sleep() - is pwm_apply_atomic() supported? | |
465 | * @pwm: PWM device | |
466 | * | |
467 | * Returns: false if pwm_apply_atomic() can be called from atomic context. | |
468 | */ | |
469 | static inline bool pwm_might_sleep(struct pwm_device *pwm) | |
470 | { | |
471 | return !pwm->chip->atomic; | |
472 | } | |
473 | ||
5ec803ed | 474 | /* PWM provider APIs */ |
024913db UKK |
475 | void pwmchip_put(struct pwm_chip *chip); |
476 | struct pwm_chip *pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv); | |
477 | struct pwm_chip *devm_pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv); | |
478 | ||
384461ab UKK |
479 | int __pwmchip_add(struct pwm_chip *chip, struct module *owner); |
480 | #define pwmchip_add(chip) __pwmchip_add(chip, THIS_MODULE) | |
8083f58d | 481 | void pwmchip_remove(struct pwm_chip *chip); |
bcda91bf | 482 | |
384461ab UKK |
483 | int __devm_pwmchip_add(struct device *dev, struct pwm_chip *chip, struct module *owner); |
484 | #define devm_pwmchip_add(dev, chip) __devm_pwmchip_add(dev, chip, THIS_MODULE) | |
bcda91bf | 485 | |
b4f78ff7 | 486 | struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *chip, |
83af2402 | 487 | const struct of_phandle_args *args); |
b4f78ff7 | 488 | struct pwm_device *of_pwm_single_xlate(struct pwm_chip *chip, |
3ab7b6ac | 489 | const struct of_phandle_args *args); |
83af2402 | 490 | |
d4c0c470 | 491 | struct pwm_device *pwm_get(struct device *dev, const char *con_id); |
8138d2dd TR |
492 | void pwm_put(struct pwm_device *pwm); |
493 | ||
d4c0c470 | 494 | struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id); |
4a6ef8e3 NV |
495 | struct pwm_device *devm_fwnode_pwm_get(struct device *dev, |
496 | struct fwnode_handle *fwnode, | |
497 | const char *con_id); | |
0bcf168b | 498 | #else |
7170d3be SY |
499 | static inline bool pwm_might_sleep(struct pwm_device *pwm) |
500 | { | |
501 | return true; | |
502 | } | |
503 | ||
c748a6d7 SY |
504 | static inline int pwm_apply_might_sleep(struct pwm_device *pwm, |
505 | const struct pwm_state *state) | |
5ec803ed | 506 | { |
4ad91a22 | 507 | might_sleep(); |
dc518b37 | 508 | return -EOPNOTSUPP; |
5ec803ed BB |
509 | } |
510 | ||
7170d3be SY |
511 | static inline int pwm_apply_atomic(struct pwm_device *pwm, |
512 | const struct pwm_state *state) | |
513 | { | |
514 | return -EOPNOTSUPP; | |
515 | } | |
516 | ||
2ea25aab DL |
517 | static inline int pwm_get_state_hw(struct pwm_device *pwm, struct pwm_state *state) |
518 | { | |
519 | return -EOPNOTSUPP; | |
520 | } | |
521 | ||
5ec803ed BB |
522 | static inline int pwm_adjust_config(struct pwm_device *pwm) |
523 | { | |
dc518b37 | 524 | return -EOPNOTSUPP; |
5ec803ed BB |
525 | } |
526 | ||
527 | static inline int pwm_config(struct pwm_device *pwm, int duty_ns, | |
528 | int period_ns) | |
529 | { | |
4ad91a22 | 530 | might_sleep(); |
5ec803ed BB |
531 | return -EINVAL; |
532 | } | |
533 | ||
5ec803ed BB |
534 | static inline int pwm_enable(struct pwm_device *pwm) |
535 | { | |
4ad91a22 | 536 | might_sleep(); |
5ec803ed BB |
537 | return -EINVAL; |
538 | } | |
539 | ||
540 | static inline void pwm_disable(struct pwm_device *pwm) | |
541 | { | |
4ad91a22 | 542 | might_sleep(); |
5ec803ed BB |
543 | } |
544 | ||
024913db UKK |
545 | static inline void pwmchip_put(struct pwm_chip *chip) |
546 | { | |
547 | } | |
548 | ||
549 | static inline struct pwm_chip *pwmchip_alloc(struct device *parent, | |
550 | unsigned int npwm, | |
551 | size_t sizeof_priv) | |
552 | { | |
553 | return ERR_PTR(-EINVAL); | |
554 | } | |
555 | ||
556 | static inline struct pwm_chip *devm_pwmchip_alloc(struct device *parent, | |
557 | unsigned int npwm, | |
558 | size_t sizeof_priv) | |
559 | { | |
560 | return pwmchip_alloc(parent, npwm, sizeof_priv); | |
561 | } | |
562 | ||
0bcf168b TB |
563 | static inline int pwmchip_add(struct pwm_chip *chip) |
564 | { | |
565 | return -EINVAL; | |
566 | } | |
567 | ||
568 | static inline int pwmchip_remove(struct pwm_chip *chip) | |
569 | { | |
570 | return -EINVAL; | |
571 | } | |
572 | ||
88da4e81 AS |
573 | static inline int devm_pwmchip_add(struct device *dev, struct pwm_chip *chip) |
574 | { | |
575 | return -EINVAL; | |
576 | } | |
577 | ||
0bcf168b TB |
578 | static inline struct pwm_device *pwm_get(struct device *dev, |
579 | const char *consumer) | |
580 | { | |
27d9a4d6 | 581 | might_sleep(); |
0bcf168b TB |
582 | return ERR_PTR(-ENODEV); |
583 | } | |
584 | ||
585 | static inline void pwm_put(struct pwm_device *pwm) | |
586 | { | |
27d9a4d6 | 587 | might_sleep(); |
0bcf168b TB |
588 | } |
589 | ||
590 | static inline struct pwm_device *devm_pwm_get(struct device *dev, | |
591 | const char *consumer) | |
592 | { | |
27d9a4d6 | 593 | might_sleep(); |
0bcf168b TB |
594 | return ERR_PTR(-ENODEV); |
595 | } | |
596 | ||
4a6ef8e3 NV |
597 | static inline struct pwm_device * |
598 | devm_fwnode_pwm_get(struct device *dev, struct fwnode_handle *fwnode, | |
599 | const char *con_id) | |
600 | { | |
27d9a4d6 | 601 | might_sleep(); |
4a6ef8e3 NV |
602 | return ERR_PTR(-ENODEV); |
603 | } | |
0bcf168b | 604 | #endif |
6354316d | 605 | |
5ec803ed BB |
606 | static inline void pwm_apply_args(struct pwm_device *pwm) |
607 | { | |
33cdcee0 BB |
608 | struct pwm_state state = { }; |
609 | ||
5ec803ed BB |
610 | /* |
611 | * PWM users calling pwm_apply_args() expect to have a fresh config | |
612 | * where the polarity and period are set according to pwm_args info. | |
613 | * The problem is, polarity can only be changed when the PWM is | |
614 | * disabled. | |
615 | * | |
616 | * PWM drivers supporting hardware readout may declare the PWM device | |
617 | * as enabled, and prevent polarity setting, which changes from the | |
618 | * existing behavior, where all PWM devices are declared as disabled | |
619 | * at startup (even if they are actually enabled), thus authorizing | |
620 | * polarity setting. | |
621 | * | |
33cdcee0 BB |
622 | * To fulfill this requirement, we apply a new state which disables |
623 | * the PWM device and set the reference period and polarity config. | |
5ec803ed BB |
624 | * |
625 | * Note that PWM users requiring a smooth handover between the | |
626 | * bootloader and the kernel (like critical regulators controlled by | |
627 | * PWM devices) will have to switch to the atomic API and avoid calling | |
628 | * pwm_apply_args(). | |
629 | */ | |
33cdcee0 BB |
630 | |
631 | state.enabled = false; | |
632 | state.polarity = pwm->args.polarity; | |
633 | state.period = pwm->args.period; | |
9e40ee18 | 634 | state.usage_power = false; |
33cdcee0 | 635 | |
c748a6d7 | 636 | pwm_apply_might_sleep(pwm, &state); |
5ec803ed BB |
637 | } |
638 | ||
8138d2dd TR |
639 | struct pwm_lookup { |
640 | struct list_head list; | |
641 | const char *provider; | |
642 | unsigned int index; | |
643 | const char *dev_id; | |
644 | const char *con_id; | |
3796ce1d AB |
645 | unsigned int period; |
646 | enum pwm_polarity polarity; | |
b526a314 | 647 | const char *module; /* optional, may be NULL */ |
8138d2dd TR |
648 | }; |
649 | ||
b526a314 HG |
650 | #define PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, \ |
651 | _period, _polarity, _module) \ | |
652 | { \ | |
653 | .provider = _provider, \ | |
654 | .index = _index, \ | |
655 | .dev_id = _dev_id, \ | |
656 | .con_id = _con_id, \ | |
657 | .period = _period, \ | |
658 | .polarity = _polarity, \ | |
659 | .module = _module, \ | |
8138d2dd TR |
660 | } |
661 | ||
b526a314 HG |
662 | #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \ |
663 | PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, _period, \ | |
664 | _polarity, NULL) | |
665 | ||
4b31eb55 | 666 | #if IS_REACHABLE(CONFIG_PWM) |
8138d2dd | 667 | void pwm_add_table(struct pwm_lookup *table, size_t num); |
efb0de55 | 668 | void pwm_remove_table(struct pwm_lookup *table, size_t num); |
0bcf168b TB |
669 | #else |
670 | static inline void pwm_add_table(struct pwm_lookup *table, size_t num) | |
671 | { | |
672 | } | |
efb0de55 SK |
673 | |
674 | static inline void pwm_remove_table(struct pwm_lookup *table, size_t num) | |
675 | { | |
676 | } | |
0c2498f1 SH |
677 | #endif |
678 | ||
5243ef8b | 679 | #endif /* __LINUX_PWM_H */ |