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