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