Linux 6.10-rc4
[linux-block.git] / include / linux / pwm.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LINUX_PWM_H
3 #define __LINUX_PWM_H
4
5 #include <linux/device.h>
6 #include <linux/err.h>
7 #include <linux/mutex.h>
8 #include <linux/of.h>
9
10 struct pwm_chip;
11
12 /**
13  * enum pwm_polarity - polarity of a PWM signal
14  * @PWM_POLARITY_NORMAL: a high signal for the duration of the duty-
15  * cycle, followed by a low signal for the remainder of the pulse
16  * period
17  * @PWM_POLARITY_INVERSED: a low signal for the duration of the duty-
18  * cycle, followed by a high signal for the remainder of the pulse
19  * period
20  */
21 enum pwm_polarity {
22         PWM_POLARITY_NORMAL,
23         PWM_POLARITY_INVERSED,
24 };
25
26 /**
27  * struct pwm_args - board-dependent PWM arguments
28  * @period: reference period
29  * @polarity: reference polarity
30  *
31  * This structure describes board-dependent arguments attached to a PWM
32  * device. These arguments are usually retrieved from the PWM lookup table or
33  * device tree.
34  *
35  * Do not confuse this with the PWM state: PWM arguments represent the initial
36  * configuration that users want to use on this PWM device rather than the
37  * current PWM hardware state.
38  */
39 struct pwm_args {
40         u64 period;
41         enum pwm_polarity polarity;
42 };
43
44 enum {
45         PWMF_REQUESTED = 0,
46         PWMF_EXPORTED = 1,
47 };
48
49 /*
50  * struct pwm_state - state of a PWM channel
51  * @period: PWM period (in nanoseconds)
52  * @duty_cycle: PWM duty cycle (in nanoseconds)
53  * @polarity: PWM polarity
54  * @enabled: PWM enabled status
55  * @usage_power: If set, the PWM driver is only required to maintain the power
56  *               output but has more freedom regarding signal form.
57  *               If supported, the signal can be optimized, for example to
58  *               improve EMI by phase shifting individual channels.
59  */
60 struct pwm_state {
61         u64 period;
62         u64 duty_cycle;
63         enum pwm_polarity polarity;
64         bool enabled;
65         bool usage_power;
66 };
67
68 /**
69  * struct pwm_device - PWM channel object
70  * @label: name of the PWM device
71  * @flags: flags associated with the PWM device
72  * @hwpwm: per-chip relative index of the PWM device
73  * @chip: PWM chip providing this PWM device
74  * @args: PWM arguments
75  * @state: last applied state
76  * @last: last implemented state (for PWM_DEBUG)
77  */
78 struct pwm_device {
79         const char *label;
80         unsigned long flags;
81         unsigned int hwpwm;
82         struct pwm_chip *chip;
83
84         struct pwm_args args;
85         struct pwm_state state;
86         struct pwm_state last;
87 };
88
89 /**
90  * pwm_get_state() - retrieve the current PWM state
91  * @pwm: PWM device
92  * @state: state to fill with the current PWM state
93  *
94  * The returned PWM state represents the state that was applied by a previous call to
95  * pwm_apply_might_sleep(). Drivers may have to slightly tweak that state before programming it to
96  * hardware. If pwm_apply_might_sleep() was never called, this returns either the current hardware
97  * state (if supported) or the default settings.
98  */
99 static inline void pwm_get_state(const struct pwm_device *pwm,
100                                  struct pwm_state *state)
101 {
102         *state = pwm->state;
103 }
104
105 static inline bool pwm_is_enabled(const struct pwm_device *pwm)
106 {
107         struct pwm_state state;
108
109         pwm_get_state(pwm, &state);
110
111         return state.enabled;
112 }
113
114 static inline u64 pwm_get_period(const struct pwm_device *pwm)
115 {
116         struct pwm_state state;
117
118         pwm_get_state(pwm, &state);
119
120         return state.period;
121 }
122
123 static inline u64 pwm_get_duty_cycle(const struct pwm_device *pwm)
124 {
125         struct pwm_state state;
126
127         pwm_get_state(pwm, &state);
128
129         return state.duty_cycle;
130 }
131
132 static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm)
133 {
134         struct pwm_state state;
135
136         pwm_get_state(pwm, &state);
137
138         return state.polarity;
139 }
140
141 static inline void pwm_get_args(const struct pwm_device *pwm,
142                                 struct pwm_args *args)
143 {
144         *args = pwm->args;
145 }
146
147 /**
148  * pwm_init_state() - prepare a new state to be applied with pwm_apply_might_sleep()
149  * @pwm: PWM device
150  * @state: state to fill with the prepared PWM state
151  *
152  * This functions prepares a state that can later be tweaked and applied
153  * to the PWM device with pwm_apply_might_sleep(). This is a convenient function
154  * that first retrieves the current PWM state and the replaces the period
155  * and polarity fields with the reference values defined in pwm->args.
156  * Once the function returns, you can adjust the ->enabled and ->duty_cycle
157  * fields according to your needs before calling pwm_apply_might_sleep().
158  *
159  * ->duty_cycle is initially set to zero to avoid cases where the current
160  * ->duty_cycle value exceed the pwm_args->period one, which would trigger
161  * an error if the user calls pwm_apply_might_sleep() without adjusting ->duty_cycle
162  * first.
163  */
164 static inline void pwm_init_state(const struct pwm_device *pwm,
165                                   struct pwm_state *state)
166 {
167         struct pwm_args args;
168
169         /* First get the current state. */
170         pwm_get_state(pwm, state);
171
172         /* Then fill it with the reference config */
173         pwm_get_args(pwm, &args);
174
175         state->period = args.period;
176         state->polarity = args.polarity;
177         state->duty_cycle = 0;
178         state->usage_power = false;
179 }
180
181 /**
182  * pwm_get_relative_duty_cycle() - Get a relative duty cycle value
183  * @state: PWM state to extract the duty cycle from
184  * @scale: target scale of the relative duty cycle
185  *
186  * This functions converts the absolute duty cycle stored in @state (expressed
187  * in nanosecond) into a value relative to the period.
188  *
189  * For example if you want to get the duty_cycle expressed in percent, call:
190  *
191  * pwm_get_state(pwm, &state);
192  * duty = pwm_get_relative_duty_cycle(&state, 100);
193  */
194 static inline unsigned int
195 pwm_get_relative_duty_cycle(const struct pwm_state *state, unsigned int scale)
196 {
197         if (!state->period)
198                 return 0;
199
200         return DIV_ROUND_CLOSEST_ULL((u64)state->duty_cycle * scale,
201                                      state->period);
202 }
203
204 /**
205  * pwm_set_relative_duty_cycle() - Set a relative duty cycle value
206  * @state: PWM state to fill
207  * @duty_cycle: relative duty cycle value
208  * @scale: scale in which @duty_cycle is expressed
209  *
210  * This functions converts a relative into an absolute duty cycle (expressed
211  * in nanoseconds), and puts the result in state->duty_cycle.
212  *
213  * For example if you want to configure a 50% duty cycle, call:
214  *
215  * pwm_init_state(pwm, &state);
216  * pwm_set_relative_duty_cycle(&state, 50, 100);
217  * pwm_apply_might_sleep(pwm, &state);
218  *
219  * This functions returns -EINVAL if @duty_cycle and/or @scale are
220  * inconsistent (@scale == 0 or @duty_cycle > @scale).
221  */
222 static inline int
223 pwm_set_relative_duty_cycle(struct pwm_state *state, unsigned int duty_cycle,
224                             unsigned int scale)
225 {
226         if (!scale || duty_cycle > scale)
227                 return -EINVAL;
228
229         state->duty_cycle = DIV_ROUND_CLOSEST_ULL((u64)duty_cycle *
230                                                   state->period,
231                                                   scale);
232
233         return 0;
234 }
235
236 /**
237  * struct pwm_capture - PWM capture data
238  * @period: period of the PWM signal (in nanoseconds)
239  * @duty_cycle: duty cycle of the PWM signal (in nanoseconds)
240  */
241 struct pwm_capture {
242         unsigned int period;
243         unsigned int duty_cycle;
244 };
245
246 /**
247  * struct pwm_ops - PWM controller operations
248  * @request: optional hook for requesting a PWM
249  * @free: optional hook for freeing a PWM
250  * @capture: capture and report PWM signal
251  * @apply: atomically apply a new PWM config
252  * @get_state: get the current PWM state. This function is only
253  *             called once per PWM device when the PWM chip is
254  *             registered.
255  */
256 struct pwm_ops {
257         int (*request)(struct pwm_chip *chip, struct pwm_device *pwm);
258         void (*free)(struct pwm_chip *chip, struct pwm_device *pwm);
259         int (*capture)(struct pwm_chip *chip, struct pwm_device *pwm,
260                        struct pwm_capture *result, unsigned long timeout);
261         int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm,
262                      const struct pwm_state *state);
263         int (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,
264                          struct pwm_state *state);
265 };
266
267 /**
268  * struct pwm_chip - abstract a PWM controller
269  * @dev: device providing the PWMs
270  * @ops: callbacks for this PWM controller
271  * @owner: module providing this chip
272  * @id: unique number of this PWM chip
273  * @npwm: number of PWMs controlled by this chip
274  * @of_xlate: request a PWM device given a device tree PWM specifier
275  * @atomic: can the driver's ->apply() be called in atomic context
276  * @uses_pwmchip_alloc: signals if pwmchip_allow was used to allocate this chip
277  * @pwms: array of PWM devices allocated by the framework
278  */
279 struct pwm_chip {
280         struct device dev;
281         const struct pwm_ops *ops;
282         struct module *owner;
283         unsigned int id;
284         unsigned int npwm;
285
286         struct pwm_device * (*of_xlate)(struct pwm_chip *chip,
287                                         const struct of_phandle_args *args);
288         bool atomic;
289
290         /* only used internally by the PWM framework */
291         bool uses_pwmchip_alloc;
292         struct pwm_device pwms[] __counted_by(npwm);
293 };
294
295 static inline struct device *pwmchip_parent(const struct pwm_chip *chip)
296 {
297         return chip->dev.parent;
298 }
299
300 static inline void *pwmchip_get_drvdata(struct pwm_chip *chip)
301 {
302         return dev_get_drvdata(&chip->dev);
303 }
304
305 static inline void pwmchip_set_drvdata(struct pwm_chip *chip, void *data)
306 {
307         dev_set_drvdata(&chip->dev, data);
308 }
309
310 #if IS_ENABLED(CONFIG_PWM)
311 /* PWM user APIs */
312 int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state);
313 int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state);
314 int pwm_adjust_config(struct pwm_device *pwm);
315
316 /**
317  * pwm_config() - change a PWM device configuration
318  * @pwm: PWM device
319  * @duty_ns: "on" time (in nanoseconds)
320  * @period_ns: duration (in nanoseconds) of one cycle
321  *
322  * Returns: 0 on success or a negative error code on failure.
323  */
324 static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
325                              int period_ns)
326 {
327         struct pwm_state state;
328
329         if (!pwm)
330                 return -EINVAL;
331
332         if (duty_ns < 0 || period_ns < 0)
333                 return -EINVAL;
334
335         pwm_get_state(pwm, &state);
336         if (state.duty_cycle == duty_ns && state.period == period_ns)
337                 return 0;
338
339         state.duty_cycle = duty_ns;
340         state.period = period_ns;
341         return pwm_apply_might_sleep(pwm, &state);
342 }
343
344 /**
345  * pwm_enable() - start a PWM output toggling
346  * @pwm: PWM device
347  *
348  * Returns: 0 on success or a negative error code on failure.
349  */
350 static inline int pwm_enable(struct pwm_device *pwm)
351 {
352         struct pwm_state state;
353
354         if (!pwm)
355                 return -EINVAL;
356
357         pwm_get_state(pwm, &state);
358         if (state.enabled)
359                 return 0;
360
361         state.enabled = true;
362         return pwm_apply_might_sleep(pwm, &state);
363 }
364
365 /**
366  * pwm_disable() - stop a PWM output toggling
367  * @pwm: PWM device
368  */
369 static inline void pwm_disable(struct pwm_device *pwm)
370 {
371         struct pwm_state state;
372
373         if (!pwm)
374                 return;
375
376         pwm_get_state(pwm, &state);
377         if (!state.enabled)
378                 return;
379
380         state.enabled = false;
381         pwm_apply_might_sleep(pwm, &state);
382 }
383
384 /**
385  * pwm_might_sleep() - is pwm_apply_atomic() supported?
386  * @pwm: PWM device
387  *
388  * Returns: false if pwm_apply_atomic() can be called from atomic context.
389  */
390 static inline bool pwm_might_sleep(struct pwm_device *pwm)
391 {
392         return !pwm->chip->atomic;
393 }
394
395 /* PWM provider APIs */
396 int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
397                 unsigned long timeout);
398
399 void pwmchip_put(struct pwm_chip *chip);
400 struct pwm_chip *pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv);
401 struct pwm_chip *devm_pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv);
402
403 int __pwmchip_add(struct pwm_chip *chip, struct module *owner);
404 #define pwmchip_add(chip) __pwmchip_add(chip, THIS_MODULE)
405 void pwmchip_remove(struct pwm_chip *chip);
406
407 int __devm_pwmchip_add(struct device *dev, struct pwm_chip *chip, struct module *owner);
408 #define devm_pwmchip_add(dev, chip) __devm_pwmchip_add(dev, chip, THIS_MODULE)
409
410 struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
411                                          unsigned int index,
412                                          const char *label);
413
414 struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *chip,
415                 const struct of_phandle_args *args);
416 struct pwm_device *of_pwm_single_xlate(struct pwm_chip *chip,
417                                        const struct of_phandle_args *args);
418
419 struct pwm_device *pwm_get(struct device *dev, const char *con_id);
420 void pwm_put(struct pwm_device *pwm);
421
422 struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
423 struct pwm_device *devm_fwnode_pwm_get(struct device *dev,
424                                        struct fwnode_handle *fwnode,
425                                        const char *con_id);
426 #else
427 static inline bool pwm_might_sleep(struct pwm_device *pwm)
428 {
429         return true;
430 }
431
432 static inline int pwm_apply_might_sleep(struct pwm_device *pwm,
433                                         const struct pwm_state *state)
434 {
435         might_sleep();
436         return -EOPNOTSUPP;
437 }
438
439 static inline int pwm_apply_atomic(struct pwm_device *pwm,
440                                    const struct pwm_state *state)
441 {
442         return -EOPNOTSUPP;
443 }
444
445 static inline int pwm_adjust_config(struct pwm_device *pwm)
446 {
447         return -EOPNOTSUPP;
448 }
449
450 static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
451                              int period_ns)
452 {
453         might_sleep();
454         return -EINVAL;
455 }
456
457 static inline int pwm_enable(struct pwm_device *pwm)
458 {
459         might_sleep();
460         return -EINVAL;
461 }
462
463 static inline void pwm_disable(struct pwm_device *pwm)
464 {
465         might_sleep();
466 }
467
468 static inline int pwm_capture(struct pwm_device *pwm,
469                               struct pwm_capture *result,
470                               unsigned long timeout)
471 {
472         return -EINVAL;
473 }
474
475 static inline void pwmchip_put(struct pwm_chip *chip)
476 {
477 }
478
479 static inline struct pwm_chip *pwmchip_alloc(struct device *parent,
480                                              unsigned int npwm,
481                                              size_t sizeof_priv)
482 {
483         return ERR_PTR(-EINVAL);
484 }
485
486 static inline struct pwm_chip *devm_pwmchip_alloc(struct device *parent,
487                                                   unsigned int npwm,
488                                                   size_t sizeof_priv)
489 {
490         return pwmchip_alloc(parent, npwm, sizeof_priv);
491 }
492
493 static inline int pwmchip_add(struct pwm_chip *chip)
494 {
495         return -EINVAL;
496 }
497
498 static inline int pwmchip_remove(struct pwm_chip *chip)
499 {
500         return -EINVAL;
501 }
502
503 static inline int devm_pwmchip_add(struct device *dev, struct pwm_chip *chip)
504 {
505         return -EINVAL;
506 }
507
508 static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
509                                                        unsigned int index,
510                                                        const char *label)
511 {
512         might_sleep();
513         return ERR_PTR(-ENODEV);
514 }
515
516 static inline struct pwm_device *pwm_get(struct device *dev,
517                                          const char *consumer)
518 {
519         might_sleep();
520         return ERR_PTR(-ENODEV);
521 }
522
523 static inline void pwm_put(struct pwm_device *pwm)
524 {
525         might_sleep();
526 }
527
528 static inline struct pwm_device *devm_pwm_get(struct device *dev,
529                                               const char *consumer)
530 {
531         might_sleep();
532         return ERR_PTR(-ENODEV);
533 }
534
535 static inline struct pwm_device *
536 devm_fwnode_pwm_get(struct device *dev, struct fwnode_handle *fwnode,
537                     const char *con_id)
538 {
539         might_sleep();
540         return ERR_PTR(-ENODEV);
541 }
542 #endif
543
544 static inline void pwm_apply_args(struct pwm_device *pwm)
545 {
546         struct pwm_state state = { };
547
548         /*
549          * PWM users calling pwm_apply_args() expect to have a fresh config
550          * where the polarity and period are set according to pwm_args info.
551          * The problem is, polarity can only be changed when the PWM is
552          * disabled.
553          *
554          * PWM drivers supporting hardware readout may declare the PWM device
555          * as enabled, and prevent polarity setting, which changes from the
556          * existing behavior, where all PWM devices are declared as disabled
557          * at startup (even if they are actually enabled), thus authorizing
558          * polarity setting.
559          *
560          * To fulfill this requirement, we apply a new state which disables
561          * the PWM device and set the reference period and polarity config.
562          *
563          * Note that PWM users requiring a smooth handover between the
564          * bootloader and the kernel (like critical regulators controlled by
565          * PWM devices) will have to switch to the atomic API and avoid calling
566          * pwm_apply_args().
567          */
568
569         state.enabled = false;
570         state.polarity = pwm->args.polarity;
571         state.period = pwm->args.period;
572         state.usage_power = false;
573
574         pwm_apply_might_sleep(pwm, &state);
575 }
576
577 /* only for backwards-compatibility, new code should not use this */
578 static inline int pwm_apply_state(struct pwm_device *pwm,
579                                   const struct pwm_state *state)
580 {
581         return pwm_apply_might_sleep(pwm, state);
582 }
583
584 struct pwm_lookup {
585         struct list_head list;
586         const char *provider;
587         unsigned int index;
588         const char *dev_id;
589         const char *con_id;
590         unsigned int period;
591         enum pwm_polarity polarity;
592         const char *module; /* optional, may be NULL */
593 };
594
595 #define PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id,     \
596                                _period, _polarity, _module)             \
597         {                                                               \
598                 .provider = _provider,                                  \
599                 .index = _index,                                        \
600                 .dev_id = _dev_id,                                      \
601                 .con_id = _con_id,                                      \
602                 .period = _period,                                      \
603                 .polarity = _polarity,                                  \
604                 .module = _module,                                      \
605         }
606
607 #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
608         PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, _period, \
609                                _polarity, NULL)
610
611 #if IS_ENABLED(CONFIG_PWM)
612 void pwm_add_table(struct pwm_lookup *table, size_t num);
613 void pwm_remove_table(struct pwm_lookup *table, size_t num);
614 #else
615 static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
616 {
617 }
618
619 static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
620 {
621 }
622 #endif
623
624 #endif /* __LINUX_PWM_H */