]> Git Repo - J-linux.git/blob - include/linux/pwm.h
Merge tag 'vfs-6.13-rc7.fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs
[J-linux.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/module.h>
8 #include <linux/mutex.h>
9 #include <linux/of.h>
10
11 MODULE_IMPORT_NS("PWM");
12
13 struct pwm_chip;
14
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
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 {
43         u64 period;
44         enum pwm_polarity polarity;
45 };
46
47 enum {
48         PWMF_REQUESTED = 0,
49         PWMF_EXPORTED = 1,
50 };
51
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
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
82  * @enabled: PWM enabled status
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.
87  */
88 struct pwm_state {
89         u64 period;
90         u64 duty_cycle;
91         enum pwm_polarity polarity;
92         bool enabled;
93         bool usage_power;
94 };
95
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
101  * @chip: PWM chip providing this PWM device
102  * @args: PWM arguments
103  * @state: last applied state
104  * @last: last implemented state (for PWM_DEBUG)
105  */
106 struct pwm_device {
107         const char *label;
108         unsigned long flags;
109         unsigned int hwpwm;
110         struct pwm_chip *chip;
111
112         struct pwm_args args;
113         struct pwm_state state;
114         struct pwm_state last;
115 };
116
117 /**
118  * pwm_get_state() - retrieve the current PWM state
119  * @pwm: PWM device
120  * @state: state to fill with the current PWM state
121  *
122  * The returned PWM state represents the state that was applied by a previous call to
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
125  * state (if supported) or the default settings.
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
133 static inline bool pwm_is_enabled(const struct pwm_device *pwm)
134 {
135         struct pwm_state state;
136
137         pwm_get_state(pwm, &state);
138
139         return state.enabled;
140 }
141
142 static inline u64 pwm_get_period(const struct pwm_device *pwm)
143 {
144         struct pwm_state state;
145
146         pwm_get_state(pwm, &state);
147
148         return state.period;
149 }
150
151 static inline u64 pwm_get_duty_cycle(const struct pwm_device *pwm)
152 {
153         struct pwm_state state;
154
155         pwm_get_state(pwm, &state);
156
157         return state.duty_cycle;
158 }
159
160 static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm)
161 {
162         struct pwm_state state;
163
164         pwm_get_state(pwm, &state);
165
166         return state.polarity;
167 }
168
169 static inline void pwm_get_args(const struct pwm_device *pwm,
170                                 struct pwm_args *args)
171 {
172         *args = pwm->args;
173 }
174
175 /**
176  * pwm_init_state() - prepare a new state to be applied with pwm_apply_might_sleep()
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
181  * to the PWM device with pwm_apply_might_sleep(). This is a convenient function
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
185  * fields according to your needs before calling pwm_apply_might_sleep().
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
189  * an error if the user calls pwm_apply_might_sleep() without adjusting ->duty_cycle
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;
206         state->usage_power = false;
207 }
208
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);
221  */
222 static inline unsigned int
223 pwm_get_relative_duty_cycle(const struct pwm_state *state, unsigned int scale)
224 {
225         if (!state->period)
226                 return 0;
227
228         return DIV_ROUND_CLOSEST_ULL((u64)state->duty_cycle * scale,
229                                      state->period);
230 }
231
232 /**
233  * pwm_set_relative_duty_cycle() - Set a relative duty cycle value
234  * @state: PWM state to fill
235  * @duty_cycle: relative duty cycle value
236  * @scale: scale in which @duty_cycle is expressed
237  *
238  * This functions converts a relative into an absolute duty cycle (expressed
239  * in nanoseconds), and puts the result in state->duty_cycle.
240  *
241  * For example if you want to configure a 50% duty cycle, call:
242  *
243  * pwm_init_state(pwm, &state);
244  * pwm_set_relative_duty_cycle(&state, 50, 100);
245  * pwm_apply_might_sleep(pwm, &state);
246  *
247  * This functions returns -EINVAL if @duty_cycle and/or @scale are
248  * inconsistent (@scale == 0 or @duty_cycle > @scale).
249  */
250 static inline int
251 pwm_set_relative_duty_cycle(struct pwm_state *state, unsigned int duty_cycle,
252                             unsigned int scale)
253 {
254         if (!scale || duty_cycle > scale)
255                 return -EINVAL;
256
257         state->duty_cycle = DIV_ROUND_CLOSEST_ULL((u64)duty_cycle *
258                                                   state->period,
259                                                   scale);
260
261         return 0;
262 }
263
264 /**
265  * struct pwm_capture - PWM capture data
266  * @period: period of the PWM signal (in nanoseconds)
267  * @duty_cycle: duty cycle of the PWM signal (in nanoseconds)
268  */
269 struct pwm_capture {
270         unsigned int period;
271         unsigned int duty_cycle;
272 };
273
274 /**
275  * struct pwm_ops - PWM controller operations
276  * @request: optional hook for requesting a PWM
277  * @free: optional hook for freeing a PWM
278  * @capture: capture and report PWM signal
279  * @sizeof_wfhw: size (in bytes) of driver specific waveform presentation
280  * @round_waveform_tohw: convert a struct pwm_waveform to driver specific presentation
281  * @round_waveform_fromhw: convert a driver specific waveform presentation to struct pwm_waveform
282  * @read_waveform: read driver specific waveform presentation from hardware
283  * @write_waveform: write driver specific waveform presentation to hardware
284  * @apply: atomically apply a new PWM config
285  * @get_state: get the current PWM state.
286  */
287 struct pwm_ops {
288         int (*request)(struct pwm_chip *chip, struct pwm_device *pwm);
289         void (*free)(struct pwm_chip *chip, struct pwm_device *pwm);
290         int (*capture)(struct pwm_chip *chip, struct pwm_device *pwm,
291                        struct pwm_capture *result, unsigned long timeout);
292
293         size_t sizeof_wfhw;
294         int (*round_waveform_tohw)(struct pwm_chip *chip, struct pwm_device *pwm,
295                                    const struct pwm_waveform *wf, void *wfhw);
296         int (*round_waveform_fromhw)(struct pwm_chip *chip, struct pwm_device *pwm,
297                                      const void *wfhw, struct pwm_waveform *wf);
298         int (*read_waveform)(struct pwm_chip *chip, struct pwm_device *pwm,
299                             void *wfhw);
300         int (*write_waveform)(struct pwm_chip *chip, struct pwm_device *pwm,
301                               const void *wfhw);
302
303         int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm,
304                      const struct pwm_state *state);
305         int (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,
306                          struct pwm_state *state);
307 };
308
309 /**
310  * struct pwm_chip - abstract a PWM controller
311  * @dev: device providing the PWMs
312  * @ops: callbacks for this PWM controller
313  * @owner: module providing this chip
314  * @id: unique number of this PWM chip
315  * @npwm: number of PWMs controlled by this chip
316  * @of_xlate: request a PWM device given a device tree PWM specifier
317  * @atomic: can the driver's ->apply() be called in atomic context
318  * @uses_pwmchip_alloc: signals if pwmchip_allow was used to allocate this chip
319  * @operational: signals if the chip can be used (or is already deregistered)
320  * @nonatomic_lock: mutex for nonatomic chips
321  * @atomic_lock: mutex for atomic chips
322  * @pwms: array of PWM devices allocated by the framework
323  */
324 struct pwm_chip {
325         struct device dev;
326         const struct pwm_ops *ops;
327         struct module *owner;
328         unsigned int id;
329         unsigned int npwm;
330
331         struct pwm_device * (*of_xlate)(struct pwm_chip *chip,
332                                         const struct of_phandle_args *args);
333         bool atomic;
334
335         /* only used internally by the PWM framework */
336         bool uses_pwmchip_alloc;
337         bool operational;
338         union {
339                 /*
340                  * depending on the chip being atomic or not either the mutex or
341                  * the spinlock is used. It protects .operational and
342                  * synchronizes the callbacks in .ops
343                  */
344                 struct mutex nonatomic_lock;
345                 spinlock_t atomic_lock;
346         };
347         struct pwm_device pwms[] __counted_by(npwm);
348 };
349
350 static inline struct device *pwmchip_parent(const struct pwm_chip *chip)
351 {
352         return chip->dev.parent;
353 }
354
355 static inline void *pwmchip_get_drvdata(struct pwm_chip *chip)
356 {
357         return dev_get_drvdata(&chip->dev);
358 }
359
360 static inline void pwmchip_set_drvdata(struct pwm_chip *chip, void *data)
361 {
362         dev_set_drvdata(&chip->dev, data);
363 }
364
365 #if IS_ENABLED(CONFIG_PWM)
366
367 /* PWM consumer APIs */
368 int pwm_round_waveform_might_sleep(struct pwm_device *pwm, struct pwm_waveform *wf);
369 int pwm_get_waveform_might_sleep(struct pwm_device *pwm, struct pwm_waveform *wf);
370 int pwm_set_waveform_might_sleep(struct pwm_device *pwm, const struct pwm_waveform *wf, bool exact);
371 int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state);
372 int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state);
373 int pwm_get_state_hw(struct pwm_device *pwm, struct pwm_state *state);
374 int pwm_adjust_config(struct pwm_device *pwm);
375
376 /**
377  * pwm_config() - change a PWM device configuration
378  * @pwm: PWM device
379  * @duty_ns: "on" time (in nanoseconds)
380  * @period_ns: duration (in nanoseconds) of one cycle
381  *
382  * Returns: 0 on success or a negative error code on failure.
383  */
384 static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
385                              int period_ns)
386 {
387         struct pwm_state state;
388
389         if (!pwm)
390                 return -EINVAL;
391
392         if (duty_ns < 0 || period_ns < 0)
393                 return -EINVAL;
394
395         pwm_get_state(pwm, &state);
396         if (state.duty_cycle == duty_ns && state.period == period_ns)
397                 return 0;
398
399         state.duty_cycle = duty_ns;
400         state.period = period_ns;
401         return pwm_apply_might_sleep(pwm, &state);
402 }
403
404 /**
405  * pwm_enable() - start a PWM output toggling
406  * @pwm: PWM device
407  *
408  * Returns: 0 on success or a negative error code on failure.
409  */
410 static inline int pwm_enable(struct pwm_device *pwm)
411 {
412         struct pwm_state state;
413
414         if (!pwm)
415                 return -EINVAL;
416
417         pwm_get_state(pwm, &state);
418         if (state.enabled)
419                 return 0;
420
421         state.enabled = true;
422         return pwm_apply_might_sleep(pwm, &state);
423 }
424
425 /**
426  * pwm_disable() - stop a PWM output toggling
427  * @pwm: PWM device
428  */
429 static inline void pwm_disable(struct pwm_device *pwm)
430 {
431         struct pwm_state state;
432
433         if (!pwm)
434                 return;
435
436         pwm_get_state(pwm, &state);
437         if (!state.enabled)
438                 return;
439
440         state.enabled = false;
441         pwm_apply_might_sleep(pwm, &state);
442 }
443
444 /**
445  * pwm_might_sleep() - is pwm_apply_atomic() supported?
446  * @pwm: PWM device
447  *
448  * Returns: false if pwm_apply_atomic() can be called from atomic context.
449  */
450 static inline bool pwm_might_sleep(struct pwm_device *pwm)
451 {
452         return !pwm->chip->atomic;
453 }
454
455 /* PWM provider APIs */
456 void pwmchip_put(struct pwm_chip *chip);
457 struct pwm_chip *pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv);
458 struct pwm_chip *devm_pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv);
459
460 int __pwmchip_add(struct pwm_chip *chip, struct module *owner);
461 #define pwmchip_add(chip) __pwmchip_add(chip, THIS_MODULE)
462 void pwmchip_remove(struct pwm_chip *chip);
463
464 int __devm_pwmchip_add(struct device *dev, struct pwm_chip *chip, struct module *owner);
465 #define devm_pwmchip_add(dev, chip) __devm_pwmchip_add(dev, chip, THIS_MODULE)
466
467 struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *chip,
468                 const struct of_phandle_args *args);
469 struct pwm_device *of_pwm_single_xlate(struct pwm_chip *chip,
470                                        const struct of_phandle_args *args);
471
472 struct pwm_device *pwm_get(struct device *dev, const char *con_id);
473 void pwm_put(struct pwm_device *pwm);
474
475 struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
476 struct pwm_device *devm_fwnode_pwm_get(struct device *dev,
477                                        struct fwnode_handle *fwnode,
478                                        const char *con_id);
479 #else
480 static inline bool pwm_might_sleep(struct pwm_device *pwm)
481 {
482         return true;
483 }
484
485 static inline int pwm_apply_might_sleep(struct pwm_device *pwm,
486                                         const struct pwm_state *state)
487 {
488         might_sleep();
489         return -EOPNOTSUPP;
490 }
491
492 static inline int pwm_apply_atomic(struct pwm_device *pwm,
493                                    const struct pwm_state *state)
494 {
495         return -EOPNOTSUPP;
496 }
497
498 static inline int pwm_get_state_hw(struct pwm_device *pwm, struct pwm_state *state)
499 {
500         return -EOPNOTSUPP;
501 }
502
503 static inline int pwm_adjust_config(struct pwm_device *pwm)
504 {
505         return -EOPNOTSUPP;
506 }
507
508 static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
509                              int period_ns)
510 {
511         might_sleep();
512         return -EINVAL;
513 }
514
515 static inline int pwm_enable(struct pwm_device *pwm)
516 {
517         might_sleep();
518         return -EINVAL;
519 }
520
521 static inline void pwm_disable(struct pwm_device *pwm)
522 {
523         might_sleep();
524 }
525
526 static inline void pwmchip_put(struct pwm_chip *chip)
527 {
528 }
529
530 static inline struct pwm_chip *pwmchip_alloc(struct device *parent,
531                                              unsigned int npwm,
532                                              size_t sizeof_priv)
533 {
534         return ERR_PTR(-EINVAL);
535 }
536
537 static inline struct pwm_chip *devm_pwmchip_alloc(struct device *parent,
538                                                   unsigned int npwm,
539                                                   size_t sizeof_priv)
540 {
541         return pwmchip_alloc(parent, npwm, sizeof_priv);
542 }
543
544 static inline int pwmchip_add(struct pwm_chip *chip)
545 {
546         return -EINVAL;
547 }
548
549 static inline int pwmchip_remove(struct pwm_chip *chip)
550 {
551         return -EINVAL;
552 }
553
554 static inline int devm_pwmchip_add(struct device *dev, struct pwm_chip *chip)
555 {
556         return -EINVAL;
557 }
558
559 static inline struct pwm_device *pwm_get(struct device *dev,
560                                          const char *consumer)
561 {
562         might_sleep();
563         return ERR_PTR(-ENODEV);
564 }
565
566 static inline void pwm_put(struct pwm_device *pwm)
567 {
568         might_sleep();
569 }
570
571 static inline struct pwm_device *devm_pwm_get(struct device *dev,
572                                               const char *consumer)
573 {
574         might_sleep();
575         return ERR_PTR(-ENODEV);
576 }
577
578 static inline struct pwm_device *
579 devm_fwnode_pwm_get(struct device *dev, struct fwnode_handle *fwnode,
580                     const char *con_id)
581 {
582         might_sleep();
583         return ERR_PTR(-ENODEV);
584 }
585 #endif
586
587 static inline void pwm_apply_args(struct pwm_device *pwm)
588 {
589         struct pwm_state state = { };
590
591         /*
592          * PWM users calling pwm_apply_args() expect to have a fresh config
593          * where the polarity and period are set according to pwm_args info.
594          * The problem is, polarity can only be changed when the PWM is
595          * disabled.
596          *
597          * PWM drivers supporting hardware readout may declare the PWM device
598          * as enabled, and prevent polarity setting, which changes from the
599          * existing behavior, where all PWM devices are declared as disabled
600          * at startup (even if they are actually enabled), thus authorizing
601          * polarity setting.
602          *
603          * To fulfill this requirement, we apply a new state which disables
604          * the PWM device and set the reference period and polarity config.
605          *
606          * Note that PWM users requiring a smooth handover between the
607          * bootloader and the kernel (like critical regulators controlled by
608          * PWM devices) will have to switch to the atomic API and avoid calling
609          * pwm_apply_args().
610          */
611
612         state.enabled = false;
613         state.polarity = pwm->args.polarity;
614         state.period = pwm->args.period;
615         state.usage_power = false;
616
617         pwm_apply_might_sleep(pwm, &state);
618 }
619
620 struct pwm_lookup {
621         struct list_head list;
622         const char *provider;
623         unsigned int index;
624         const char *dev_id;
625         const char *con_id;
626         unsigned int period;
627         enum pwm_polarity polarity;
628         const char *module; /* optional, may be NULL */
629 };
630
631 #define PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id,     \
632                                _period, _polarity, _module)             \
633         {                                                               \
634                 .provider = _provider,                                  \
635                 .index = _index,                                        \
636                 .dev_id = _dev_id,                                      \
637                 .con_id = _con_id,                                      \
638                 .period = _period,                                      \
639                 .polarity = _polarity,                                  \
640                 .module = _module,                                      \
641         }
642
643 #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
644         PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, _period, \
645                                _polarity, NULL)
646
647 #if IS_ENABLED(CONFIG_PWM)
648 void pwm_add_table(struct pwm_lookup *table, size_t num);
649 void pwm_remove_table(struct pwm_lookup *table, size_t num);
650 #else
651 static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
652 {
653 }
654
655 static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
656 {
657 }
658 #endif
659
660 #endif /* __LINUX_PWM_H */
This page took 0.065291 seconds and 4 git commands to generate.