Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
1a189b97 RK |
2 | #ifndef __LINUX_PWM_H |
3 | #define __LINUX_PWM_H | |
4 | ||
0bcf168b | 5 | #include <linux/err.h> |
d1cd2142 | 6 | #include <linux/mutex.h> |
7299ab70 TR |
7 | #include <linux/of.h> |
8 | ||
3a3d1a4e | 9 | struct pwm_capture; |
62099abf | 10 | struct seq_file; |
3a3d1a4e | 11 | |
0c2498f1 SH |
12 | struct pwm_chip; |
13 | ||
0aa0869c PA |
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 | ||
e39c0df1 BB |
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 | ||
f051c466 TR |
46 | enum { |
47 | PWMF_REQUESTED = 1 << 0, | |
09a7e4a3 | 48 | PWMF_EXPORTED = 1 << 1, |
f051c466 TR |
49 | }; |
50 | ||
43a276b0 BB |
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 | |
09a7e4a3 | 56 | * @enabled: PWM enabled status |
43a276b0 BB |
57 | */ |
58 | struct pwm_state { | |
59 | unsigned int period; | |
60 | unsigned int duty_cycle; | |
61 | enum pwm_polarity polarity; | |
09a7e4a3 | 62 | bool enabled; |
43a276b0 BB |
63 | }; |
64 | ||
04883802 TR |
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 | |
e39c0df1 | 73 | * @args: PWM arguments |
43a276b0 | 74 | * @state: curent PWM channel state |
04883802 | 75 | */ |
f051c466 | 76 | struct pwm_device { |
6bc7064a TR |
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; | |
83 | ||
e39c0df1 | 84 | struct pwm_args args; |
43a276b0 | 85 | struct pwm_state state; |
f051c466 TR |
86 | }; |
87 | ||
43a276b0 BB |
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 | ||
5c31252c BB |
99 | static inline bool pwm_is_enabled(const struct pwm_device *pwm) |
100 | { | |
09a7e4a3 BB |
101 | struct pwm_state state; |
102 | ||
103 | pwm_get_state(pwm, &state); | |
104 | ||
105 | return state.enabled; | |
5c31252c BB |
106 | } |
107 | ||
f051c466 TR |
108 | static inline void pwm_set_period(struct pwm_device *pwm, unsigned int period) |
109 | { | |
110 | if (pwm) | |
43a276b0 | 111 | pwm->state.period = period; |
f051c466 TR |
112 | } |
113 | ||
a1cf4217 | 114 | static inline unsigned int pwm_get_period(const struct pwm_device *pwm) |
f051c466 | 115 | { |
43a276b0 BB |
116 | struct pwm_state state; |
117 | ||
118 | pwm_get_state(pwm, &state); | |
119 | ||
120 | return state.period; | |
f051c466 TR |
121 | } |
122 | ||
76abbdde HS |
123 | static inline void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty) |
124 | { | |
125 | if (pwm) | |
43a276b0 | 126 | pwm->state.duty_cycle = duty; |
76abbdde HS |
127 | } |
128 | ||
a1cf4217 | 129 | static inline unsigned int pwm_get_duty_cycle(const struct pwm_device *pwm) |
76abbdde | 130 | { |
43a276b0 BB |
131 | struct pwm_state state; |
132 | ||
133 | pwm_get_state(pwm, &state); | |
134 | ||
135 | return state.duty_cycle; | |
76abbdde HS |
136 | } |
137 | ||
011e7631 BB |
138 | static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm) |
139 | { | |
43a276b0 BB |
140 | struct pwm_state state; |
141 | ||
142 | pwm_get_state(pwm, &state); | |
143 | ||
144 | return state.polarity; | |
011e7631 BB |
145 | } |
146 | ||
e39c0df1 BB |
147 | static inline void pwm_get_args(const struct pwm_device *pwm, |
148 | struct pwm_args *args) | |
149 | { | |
150 | *args = pwm->args; | |
151 | } | |
152 | ||
a6a0dbbc BB |
153 | /** |
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 | ||
f6f3bddf BB |
186 | /** |
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 | ||
0c2498f1 SH |
241 | /** |
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 | |
0aa0869c | 246 | * @set_polarity: configure the polarity of this PWM |
3a3d1a4e | 247 | * @capture: capture and report PWM signal |
0c2498f1 SH |
248 | * @enable: enable PWM output toggling |
249 | * @disable: disable PWM output toggling | |
5ec803ed BB |
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) | |
15fa8a43 BB |
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. | |
62099abf | 257 | * @dbg_show: optional routine to show contents in debugfs |
0c2498f1 SH |
258 | * @owner: helps prevent removal of modules exporting active PWMs |
259 | */ | |
260 | struct pwm_ops { | |
6bc7064a TR |
261 | int (*request)(struct pwm_chip *chip, struct pwm_device *pwm); |
262 | void (*free)(struct pwm_chip *chip, struct pwm_device *pwm); | |
263 | int (*config)(struct pwm_chip *chip, struct pwm_device *pwm, | |
264 | int duty_ns, int period_ns); | |
265 | int (*set_polarity)(struct pwm_chip *chip, struct pwm_device *pwm, | |
266 | enum pwm_polarity polarity); | |
3a3d1a4e LJ |
267 | int (*capture)(struct pwm_chip *chip, struct pwm_device *pwm, |
268 | struct pwm_capture *result, unsigned long timeout); | |
6bc7064a TR |
269 | int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm); |
270 | void (*disable)(struct pwm_chip *chip, struct pwm_device *pwm); | |
5ec803ed BB |
271 | int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm, |
272 | struct pwm_state *state); | |
15fa8a43 BB |
273 | void (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm, |
274 | struct pwm_state *state); | |
62099abf | 275 | #ifdef CONFIG_DEBUG_FS |
6bc7064a | 276 | void (*dbg_show)(struct pwm_chip *chip, struct seq_file *s); |
62099abf | 277 | #endif |
6bc7064a | 278 | struct module *owner; |
0c2498f1 SH |
279 | }; |
280 | ||
281 | /** | |
f051c466 TR |
282 | * struct pwm_chip - abstract a PWM controller |
283 | * @dev: device providing the PWMs | |
284 | * @list: list node for internal use | |
285 | * @ops: callbacks for this PWM controller | |
286 | * @base: number of first PWM controlled by this chip | |
287 | * @npwm: number of PWMs controlled by this chip | |
288 | * @pwms: array of PWM devices allocated by the framework | |
04883802 TR |
289 | * @of_xlate: request a PWM device given a device tree PWM specifier |
290 | * @of_pwm_n_cells: number of cells expected in the device tree PWM specifier | |
0c2498f1 SH |
291 | */ |
292 | struct pwm_chip { | |
6bc7064a TR |
293 | struct device *dev; |
294 | struct list_head list; | |
295 | const struct pwm_ops *ops; | |
296 | int base; | |
297 | unsigned int npwm; | |
298 | ||
299 | struct pwm_device *pwms; | |
300 | ||
301 | struct pwm_device * (*of_xlate)(struct pwm_chip *pc, | |
302 | const struct of_phandle_args *args); | |
303 | unsigned int of_pwm_n_cells; | |
0c2498f1 SH |
304 | }; |
305 | ||
3a3d1a4e LJ |
306 | /** |
307 | * struct pwm_capture - PWM capture data | |
308 | * @period: period of the PWM signal (in nanoseconds) | |
309 | * @duty_cycle: duty cycle of the PWM signal (in nanoseconds) | |
310 | */ | |
311 | struct pwm_capture { | |
312 | unsigned int period; | |
313 | unsigned int duty_cycle; | |
314 | }; | |
315 | ||
0bcf168b | 316 | #if IS_ENABLED(CONFIG_PWM) |
5ec803ed BB |
317 | /* PWM user APIs */ |
318 | struct pwm_device *pwm_request(int pwm_id, const char *label); | |
319 | void pwm_free(struct pwm_device *pwm); | |
320 | int pwm_apply_state(struct pwm_device *pwm, struct pwm_state *state); | |
321 | int pwm_adjust_config(struct pwm_device *pwm); | |
322 | ||
323 | /** | |
324 | * pwm_config() - change a PWM device configuration | |
325 | * @pwm: PWM device | |
326 | * @duty_ns: "on" time (in nanoseconds) | |
327 | * @period_ns: duration (in nanoseconds) of one cycle | |
328 | * | |
329 | * Returns: 0 on success or a negative error code on failure. | |
330 | */ | |
331 | static inline int pwm_config(struct pwm_device *pwm, int duty_ns, | |
332 | int period_ns) | |
333 | { | |
334 | struct pwm_state state; | |
335 | ||
336 | if (!pwm) | |
337 | return -EINVAL; | |
338 | ||
ef2bf499 BN |
339 | if (duty_ns < 0 || period_ns < 0) |
340 | return -EINVAL; | |
341 | ||
5ec803ed BB |
342 | pwm_get_state(pwm, &state); |
343 | if (state.duty_cycle == duty_ns && state.period == period_ns) | |
344 | return 0; | |
345 | ||
346 | state.duty_cycle = duty_ns; | |
347 | state.period = period_ns; | |
348 | return pwm_apply_state(pwm, &state); | |
349 | } | |
350 | ||
351 | /** | |
352 | * pwm_set_polarity() - configure the polarity of a PWM signal | |
353 | * @pwm: PWM device | |
354 | * @polarity: new polarity of the PWM signal | |
355 | * | |
356 | * Note that the polarity cannot be configured while the PWM device is | |
357 | * enabled. | |
358 | * | |
359 | * Returns: 0 on success or a negative error code on failure. | |
360 | */ | |
361 | static inline int pwm_set_polarity(struct pwm_device *pwm, | |
362 | enum pwm_polarity polarity) | |
363 | { | |
364 | struct pwm_state state; | |
365 | ||
366 | if (!pwm) | |
367 | return -EINVAL; | |
368 | ||
369 | pwm_get_state(pwm, &state); | |
370 | if (state.polarity == polarity) | |
371 | return 0; | |
372 | ||
373 | /* | |
374 | * Changing the polarity of a running PWM without adjusting the | |
375 | * dutycycle/period value is a bit risky (can introduce glitches). | |
376 | * Return -EBUSY in this case. | |
377 | * Note that this is allowed when using pwm_apply_state() because | |
378 | * the user specifies all the parameters. | |
379 | */ | |
380 | if (state.enabled) | |
381 | return -EBUSY; | |
382 | ||
383 | state.polarity = polarity; | |
384 | return pwm_apply_state(pwm, &state); | |
385 | } | |
386 | ||
387 | /** | |
388 | * pwm_enable() - start a PWM output toggling | |
389 | * @pwm: PWM device | |
390 | * | |
391 | * Returns: 0 on success or a negative error code on failure. | |
392 | */ | |
393 | static inline int pwm_enable(struct pwm_device *pwm) | |
394 | { | |
395 | struct pwm_state state; | |
396 | ||
397 | if (!pwm) | |
398 | return -EINVAL; | |
399 | ||
400 | pwm_get_state(pwm, &state); | |
401 | if (state.enabled) | |
402 | return 0; | |
403 | ||
404 | state.enabled = true; | |
405 | return pwm_apply_state(pwm, &state); | |
406 | } | |
407 | ||
408 | /** | |
409 | * pwm_disable() - stop a PWM output toggling | |
410 | * @pwm: PWM device | |
411 | */ | |
412 | static inline void pwm_disable(struct pwm_device *pwm) | |
413 | { | |
414 | struct pwm_state state; | |
415 | ||
416 | if (!pwm) | |
417 | return; | |
418 | ||
419 | pwm_get_state(pwm, &state); | |
420 | if (!state.enabled) | |
421 | return; | |
422 | ||
423 | state.enabled = false; | |
424 | pwm_apply_state(pwm, &state); | |
425 | } | |
426 | ||
5ec803ed | 427 | /* PWM provider APIs */ |
3a3d1a4e LJ |
428 | int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result, |
429 | unsigned long timeout); | |
f051c466 TR |
430 | int pwm_set_chip_data(struct pwm_device *pwm, void *data); |
431 | void *pwm_get_chip_data(struct pwm_device *pwm); | |
432 | ||
b6a00fae TK |
433 | int pwmchip_add_with_polarity(struct pwm_chip *chip, |
434 | enum pwm_polarity polarity); | |
0c2498f1 SH |
435 | int pwmchip_add(struct pwm_chip *chip); |
436 | int pwmchip_remove(struct pwm_chip *chip); | |
f051c466 TR |
437 | struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip, |
438 | unsigned int index, | |
439 | const char *label); | |
8138d2dd | 440 | |
83af2402 PA |
441 | struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *pc, |
442 | const struct of_phandle_args *args); | |
443 | ||
d4c0c470 | 444 | struct pwm_device *pwm_get(struct device *dev, const char *con_id); |
8eb96127 | 445 | struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id); |
8138d2dd TR |
446 | void pwm_put(struct pwm_device *pwm); |
447 | ||
d4c0c470 | 448 | struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id); |
261a5edd PU |
449 | struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np, |
450 | const char *con_id); | |
6354316d | 451 | void devm_pwm_put(struct device *dev, struct pwm_device *pwm); |
0bcf168b | 452 | #else |
5ec803ed BB |
453 | static inline struct pwm_device *pwm_request(int pwm_id, const char *label) |
454 | { | |
455 | return ERR_PTR(-ENODEV); | |
456 | } | |
457 | ||
458 | static inline void pwm_free(struct pwm_device *pwm) | |
459 | { | |
460 | } | |
461 | ||
462 | static inline int pwm_apply_state(struct pwm_device *pwm, | |
463 | const struct pwm_state *state) | |
464 | { | |
465 | return -ENOTSUPP; | |
466 | } | |
467 | ||
468 | static inline int pwm_adjust_config(struct pwm_device *pwm) | |
469 | { | |
470 | return -ENOTSUPP; | |
471 | } | |
472 | ||
473 | static inline int pwm_config(struct pwm_device *pwm, int duty_ns, | |
474 | int period_ns) | |
475 | { | |
476 | return -EINVAL; | |
477 | } | |
478 | ||
3a3d1a4e LJ |
479 | static inline int pwm_capture(struct pwm_device *pwm, |
480 | struct pwm_capture *result, | |
481 | unsigned long timeout) | |
482 | { | |
483 | return -EINVAL; | |
484 | } | |
485 | ||
5ec803ed BB |
486 | static inline int pwm_set_polarity(struct pwm_device *pwm, |
487 | enum pwm_polarity polarity) | |
488 | { | |
489 | return -ENOTSUPP; | |
490 | } | |
491 | ||
492 | static inline int pwm_enable(struct pwm_device *pwm) | |
493 | { | |
494 | return -EINVAL; | |
495 | } | |
496 | ||
497 | static inline void pwm_disable(struct pwm_device *pwm) | |
498 | { | |
499 | } | |
500 | ||
0bcf168b TB |
501 | static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data) |
502 | { | |
503 | return -EINVAL; | |
504 | } | |
505 | ||
506 | static inline void *pwm_get_chip_data(struct pwm_device *pwm) | |
507 | { | |
508 | return NULL; | |
509 | } | |
510 | ||
511 | static inline int pwmchip_add(struct pwm_chip *chip) | |
512 | { | |
513 | return -EINVAL; | |
514 | } | |
515 | ||
b6a00fae TK |
516 | static inline int pwmchip_add_inversed(struct pwm_chip *chip) |
517 | { | |
518 | return -EINVAL; | |
519 | } | |
520 | ||
0bcf168b TB |
521 | static inline int pwmchip_remove(struct pwm_chip *chip) |
522 | { | |
523 | return -EINVAL; | |
524 | } | |
525 | ||
526 | static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip, | |
527 | unsigned int index, | |
528 | const char *label) | |
529 | { | |
530 | return ERR_PTR(-ENODEV); | |
531 | } | |
532 | ||
533 | static inline struct pwm_device *pwm_get(struct device *dev, | |
534 | const char *consumer) | |
535 | { | |
536 | return ERR_PTR(-ENODEV); | |
537 | } | |
538 | ||
8eb96127 PU |
539 | static inline struct pwm_device *of_pwm_get(struct device_node *np, |
540 | const char *con_id) | |
541 | { | |
542 | return ERR_PTR(-ENODEV); | |
543 | } | |
544 | ||
0bcf168b TB |
545 | static inline void pwm_put(struct pwm_device *pwm) |
546 | { | |
547 | } | |
548 | ||
549 | static inline struct pwm_device *devm_pwm_get(struct device *dev, | |
550 | const char *consumer) | |
551 | { | |
552 | return ERR_PTR(-ENODEV); | |
553 | } | |
554 | ||
261a5edd PU |
555 | static inline struct pwm_device *devm_of_pwm_get(struct device *dev, |
556 | struct device_node *np, | |
557 | const char *con_id) | |
558 | { | |
559 | return ERR_PTR(-ENODEV); | |
560 | } | |
561 | ||
0bcf168b TB |
562 | static inline void devm_pwm_put(struct device *dev, struct pwm_device *pwm) |
563 | { | |
564 | } | |
565 | #endif | |
6354316d | 566 | |
5ec803ed BB |
567 | static inline void pwm_apply_args(struct pwm_device *pwm) |
568 | { | |
33cdcee0 BB |
569 | struct pwm_state state = { }; |
570 | ||
5ec803ed BB |
571 | /* |
572 | * PWM users calling pwm_apply_args() expect to have a fresh config | |
573 | * where the polarity and period are set according to pwm_args info. | |
574 | * The problem is, polarity can only be changed when the PWM is | |
575 | * disabled. | |
576 | * | |
577 | * PWM drivers supporting hardware readout may declare the PWM device | |
578 | * as enabled, and prevent polarity setting, which changes from the | |
579 | * existing behavior, where all PWM devices are declared as disabled | |
580 | * at startup (even if they are actually enabled), thus authorizing | |
581 | * polarity setting. | |
582 | * | |
33cdcee0 BB |
583 | * To fulfill this requirement, we apply a new state which disables |
584 | * the PWM device and set the reference period and polarity config. | |
5ec803ed BB |
585 | * |
586 | * Note that PWM users requiring a smooth handover between the | |
587 | * bootloader and the kernel (like critical regulators controlled by | |
588 | * PWM devices) will have to switch to the atomic API and avoid calling | |
589 | * pwm_apply_args(). | |
590 | */ | |
33cdcee0 BB |
591 | |
592 | state.enabled = false; | |
593 | state.polarity = pwm->args.polarity; | |
594 | state.period = pwm->args.period; | |
595 | ||
596 | pwm_apply_state(pwm, &state); | |
5ec803ed BB |
597 | } |
598 | ||
8138d2dd TR |
599 | struct pwm_lookup { |
600 | struct list_head list; | |
601 | const char *provider; | |
602 | unsigned int index; | |
603 | const char *dev_id; | |
604 | const char *con_id; | |
3796ce1d AB |
605 | unsigned int period; |
606 | enum pwm_polarity polarity; | |
b526a314 | 607 | const char *module; /* optional, may be NULL */ |
8138d2dd TR |
608 | }; |
609 | ||
b526a314 HG |
610 | #define PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, \ |
611 | _period, _polarity, _module) \ | |
612 | { \ | |
613 | .provider = _provider, \ | |
614 | .index = _index, \ | |
615 | .dev_id = _dev_id, \ | |
616 | .con_id = _con_id, \ | |
617 | .period = _period, \ | |
618 | .polarity = _polarity, \ | |
619 | .module = _module, \ | |
8138d2dd TR |
620 | } |
621 | ||
b526a314 HG |
622 | #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \ |
623 | PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, _period, \ | |
624 | _polarity, NULL) | |
625 | ||
0bcf168b | 626 | #if IS_ENABLED(CONFIG_PWM) |
8138d2dd | 627 | void pwm_add_table(struct pwm_lookup *table, size_t num); |
efb0de55 | 628 | void pwm_remove_table(struct pwm_lookup *table, size_t num); |
0bcf168b TB |
629 | #else |
630 | static inline void pwm_add_table(struct pwm_lookup *table, size_t num) | |
631 | { | |
632 | } | |
efb0de55 SK |
633 | |
634 | static inline void pwm_remove_table(struct pwm_lookup *table, size_t num) | |
635 | { | |
636 | } | |
0c2498f1 SH |
637 | #endif |
638 | ||
76abbdde HS |
639 | #ifdef CONFIG_PWM_SYSFS |
640 | void pwmchip_sysfs_export(struct pwm_chip *chip); | |
641 | void pwmchip_sysfs_unexport(struct pwm_chip *chip); | |
0733424c | 642 | void pwmchip_sysfs_unexport_children(struct pwm_chip *chip); |
76abbdde HS |
643 | #else |
644 | static inline void pwmchip_sysfs_export(struct pwm_chip *chip) | |
645 | { | |
646 | } | |
647 | ||
648 | static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip) | |
649 | { | |
650 | } | |
0733424c DH |
651 | |
652 | static inline void pwmchip_sysfs_unexport_children(struct pwm_chip *chip) | |
653 | { | |
654 | } | |
76abbdde HS |
655 | #endif /* CONFIG_PWM_SYSFS */ |
656 | ||
5243ef8b | 657 | #endif /* __LINUX_PWM_H */ |