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
9struct pwm_capture;
10struct seq_file;
11
12struct pwm_chip;
13
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 */
23enum pwm_polarity {
24    PWM_POLARITY_NORMAL,
25    PWM_POLARITY_INVERSED,
26};
27
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 */
41struct pwm_args {
42    u64 period;
43    enum pwm_polarity polarity;
44};
45
46enum {
47    PWMF_REQUESTED = 1 << 0,
48    PWMF_EXPORTED = 1 << 1,
49};
50
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
56 * @enabled: PWM enabled status
57 */
58struct pwm_state {
59    u64 period;
60    u64 duty_cycle;
61    enum pwm_polarity polarity;
62#ifdef CONFIG_PWM_ROCKCHIP_ONESHOT
63    u64 oneshot_count;
64#endif /* CONFIG_PWM_ROCKCHIP_ONESHOT */
65    bool enabled;
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 * @pwm: global index of the PWM device
74 * @chip: PWM chip providing this PWM device
75 * @chip_data: chip-private data associated with the PWM device
76 * @args: PWM arguments
77 * @state: last applied state
78 * @last: last implemented state (for PWM_DEBUG)
79 */
80struct pwm_device {
81    const char *label;
82    unsigned long flags;
83    unsigned int hwpwm;
84    unsigned int pwm;
85    struct pwm_chip *chip;
86    void *chip_data;
87
88    struct pwm_args args;
89    struct pwm_state state;
90    struct pwm_state last;
91};
92
93/**
94 * pwm_get_state() - retrieve the current PWM state
95 * @pwm: PWM device
96 * @state: state to fill with the current PWM state
97 */
98static inline void pwm_get_state(const struct pwm_device *pwm, struct pwm_state *state)
99{
100    *state = pwm->state;
101}
102
103static inline bool pwm_is_enabled(const struct pwm_device *pwm)
104{
105    struct pwm_state state;
106
107    pwm_get_state(pwm, &state);
108
109    return state.enabled;
110}
111
112static inline void pwm_set_period(struct pwm_device *pwm, u64 period)
113{
114    if (pwm) {
115        pwm->state.period = period;
116    }
117}
118
119static inline u64 pwm_get_period(const struct pwm_device *pwm)
120{
121    struct pwm_state state;
122
123    pwm_get_state(pwm, &state);
124
125    return state.period;
126}
127
128static inline void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty)
129{
130    if (pwm) {
131        pwm->state.duty_cycle = duty;
132    }
133}
134
135static inline u64 pwm_get_duty_cycle(const struct pwm_device *pwm)
136{
137    struct pwm_state state;
138
139    pwm_get_state(pwm, &state);
140
141    return state.duty_cycle;
142}
143
144static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm)
145{
146    struct pwm_state state;
147
148    pwm_get_state(pwm, &state);
149
150    return state.polarity;
151}
152
153static inline void pwm_get_args(const struct pwm_device *pwm, struct pwm_args *args)
154{
155    *args = pwm->args;
156}
157
158/**
159 * pwm_init_state() - prepare a new state to be applied with pwm_apply_state()
160 * @pwm: PWM device
161 * @state: state to fill with the prepared PWM state
162 *
163 * This functions prepares a state that can later be tweaked and applied
164 * to the PWM device with pwm_apply_state(). This is a convenient function
165 * that first retrieves the current PWM state and the replaces the period
166 * and polarity fields with the reference values defined in pwm->args.
167 * Once the function returns, you can adjust the ->enabled and ->duty_cycle
168 * fields according to your needs before calling pwm_apply_state().
169 *
170 * ->duty_cycle is initially set to zero to avoid cases where the current
171 * ->duty_cycle value exceed the pwm_args->period one, which would trigger
172 * an error if the user calls pwm_apply_state() without adjusting ->duty_cycle
173 * first.
174 */
175static inline void pwm_init_state(const struct pwm_device *pwm, struct pwm_state *state)
176{
177    struct pwm_args args;
178
179    /* First get the current state. */
180    pwm_get_state(pwm, state);
181
182    /* Then fill it with the reference config */
183    pwm_get_args(pwm, &args);
184
185    state->period = args.period;
186    state->polarity = args.polarity;
187    state->duty_cycle = 0;
188}
189
190/**
191 * pwm_get_relative_duty_cycle() - Get a relative duty cycle value
192 * @state: PWM state to extract the duty cycle from
193 * @scale: target scale of the relative duty cycle
194 *
195 * This functions converts the absolute duty cycle stored in @state (expressed
196 * in nanosecond) into a value relative to the period.
197 *
198 * For example if you want to get the duty_cycle expressed in percent, call:
199 *
200 * pwm_get_state(pwm, &state);
201 * duty = pwm_get_relative_duty_cycle(&state, 100);
202 */
203static inline unsigned int pwm_get_relative_duty_cycle(const struct pwm_state *state, unsigned int scale)
204{
205    if (!state->period) {
206        return 0;
207    }
208
209    return DIV_ROUND_CLOSEST_ULL((u64)state->duty_cycle * scale, state->period);
210}
211
212/**
213 * pwm_set_relative_duty_cycle() - Set a relative duty cycle value
214 * @state: PWM state to fill
215 * @duty_cycle: relative duty cycle value
216 * @scale: scale in which @duty_cycle is expressed
217 *
218 * This functions converts a relative into an absolute duty cycle (expressed
219 * in nanoseconds), and puts the result in state->duty_cycle.
220 *
221 * For example if you want to configure a 50% duty cycle, call:
222 *
223 * pwm_init_state(pwm, &state);
224 * pwm_set_relative_duty_cycle(&state, 50, 100);
225 * pwm_apply_state(pwm, &state);
226 *
227 * This functions returns -EINVAL if @duty_cycle and/or @scale are
228 * inconsistent (@scale == 0 or @duty_cycle > @scale).
229 */
230static inline int pwm_set_relative_duty_cycle(struct pwm_state *state, unsigned int duty_cycle, unsigned int scale)
231{
232    if (!scale || duty_cycle > scale) {
233        return -EINVAL;
234    }
235
236    state->duty_cycle = DIV_ROUND_CLOSEST_ULL((u64)duty_cycle * state->period, scale);
237
238    return 0;
239}
240
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 * @capture: capture and report PWM signal
246 * @apply: atomically apply a new PWM config
247 * @get_state: get the current PWM state. This function is only
248 *           called once per PWM device when the PWM chip is
249 *           registered.
250 * @owner: helps prevent removal of modules exporting active PWMs
251 * @config: configure duty cycles and period length for this PWM
252 * @set_polarity: configure the polarity of this PWM
253 * @enable: enable PWM output toggling
254 * @disable: disable PWM output toggling
255 */
256struct 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, struct pwm_capture *result, unsigned long timeout);
260    int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm, const struct pwm_state *state);
261    void (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm, struct pwm_state *state);
262    struct module *owner;
263
264    /* Only used by legacy drivers */
265    int (*config)(struct pwm_chip *chip, struct pwm_device *pwm, int duty_ns, int period_ns);
266    int (*set_polarity)(struct pwm_chip *chip, struct pwm_device *pwm, enum pwm_polarity polarity);
267    int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm);
268    void (*disable)(struct pwm_chip *chip, struct pwm_device *pwm);
269};
270
271/**
272 * struct pwm_chip - abstract a PWM controller
273 * @dev: device providing the PWMs
274 * @ops: callbacks for this PWM controller
275 * @base: number of first PWM controlled by this chip
276 * @npwm: number of PWMs controlled by this chip
277 * @of_xlate: request a PWM device given a device tree PWM specifier
278 * @of_pwm_n_cells: number of cells expected in the device tree PWM specifier
279 * @list: list node for internal use
280 * @pwms: array of PWM devices allocated by the framework
281 */
282struct pwm_chip {
283    struct device *dev;
284    const struct pwm_ops *ops;
285    int base;
286    unsigned int npwm;
287
288    struct pwm_device *(*of_xlate)(struct pwm_chip *pc, const struct of_phandle_args *args);
289    unsigned int of_pwm_n_cells;
290
291    /* only used internally by the PWM framework */
292    struct list_head list;
293    struct pwm_device *pwms;
294};
295
296/**
297 * struct pwm_capture - PWM capture data
298 * @period: period of the PWM signal (in nanoseconds)
299 * @duty_cycle: duty cycle of the PWM signal (in nanoseconds)
300 */
301struct pwm_capture {
302    unsigned int period;
303    unsigned int duty_cycle;
304};
305
306#if IS_ENABLED(CONFIG_PWM)
307/* PWM user APIs */
308struct pwm_device *pwm_request(int pwm_id, const char *label);
309void pwm_free(struct pwm_device *pwm);
310int pwm_apply_state(struct pwm_device *pwm, const struct pwm_state *state);
311int pwm_adjust_config(struct pwm_device *pwm);
312
313/**
314 * pwm_config() - change a PWM device configuration
315 * @pwm: PWM device
316 * @duty_ns: "on" time (in nanoseconds)
317 * @period_ns: duration (in nanoseconds) of one cycle
318 *
319 * Returns: 0 on success or a negative error code on failure.
320 */
321static inline int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
322{
323    struct pwm_state state;
324
325    if (!pwm) {
326        return -EINVAL;
327    }
328
329    if (duty_ns < 0 || period_ns < 0) {
330        return -EINVAL;
331    }
332
333    pwm_get_state(pwm, &state);
334    if (state.duty_cycle == duty_ns && state.period == period_ns) {
335        return 0;
336    }
337
338    state.duty_cycle = duty_ns;
339    state.period = period_ns;
340    return pwm_apply_state(pwm, &state);
341}
342
343/**
344 * pwm_enable() - start a PWM output toggling
345 * @pwm: PWM device
346 *
347 * Returns: 0 on success or a negative error code on failure.
348 */
349static inline int pwm_enable(struct pwm_device *pwm)
350{
351    struct pwm_state state;
352
353    if (!pwm) {
354        return -EINVAL;
355    }
356
357    pwm_get_state(pwm, &state);
358    if (state.enabled) {
359        return 0;
360    }
361
362    state.enabled = true;
363    return pwm_apply_state(pwm, &state);
364}
365
366/**
367 * pwm_disable() - stop a PWM output toggling
368 * @pwm: PWM device
369 */
370static inline void pwm_disable(struct pwm_device *pwm)
371{
372    struct pwm_state state;
373
374    if (!pwm) {
375        return;
376    }
377
378    pwm_get_state(pwm, &state);
379    if (!state.enabled) {
380        return;
381    }
382
383    state.enabled = false;
384    pwm_apply_state(pwm, &state);
385}
386
387/* PWM provider APIs */
388int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result, unsigned long timeout);
389int pwm_set_chip_data(struct pwm_device *pwm, void *data);
390void *pwm_get_chip_data(struct pwm_device *pwm);
391
392int pwmchip_add_with_polarity(struct pwm_chip *chip, enum pwm_polarity polarity);
393int pwmchip_add(struct pwm_chip *chip);
394int pwmchip_remove(struct pwm_chip *chip);
395struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip, unsigned int index, const char *label);
396
397struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *pc, const struct of_phandle_args *args);
398
399struct pwm_device *pwm_get(struct device *dev, const char *con_id);
400struct pwm_device *of_pwm_get(struct device *dev, struct device_node *np, const char *con_id);
401void pwm_put(struct pwm_device *pwm);
402
403struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
404struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np, const char *con_id);
405struct pwm_device *devm_fwnode_pwm_get(struct device *dev, struct fwnode_handle *fwnode, const char *con_id);
406void devm_pwm_put(struct device *dev, struct pwm_device *pwm);
407#else
408static inline struct pwm_device *pwm_request(int pwm_id, const char *label)
409{
410    return ERR_PTR(-ENODEV);
411}
412
413static inline void pwm_free(struct pwm_device *pwm)
414{
415}
416
417static inline int pwm_apply_state(struct pwm_device *pwm, const struct pwm_state *state)
418{
419    return -ENOTSUPP;
420}
421
422static inline int pwm_adjust_config(struct pwm_device *pwm)
423{
424    return -ENOTSUPP;
425}
426
427static inline int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
428{
429    return -EINVAL;
430}
431
432static inline int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result, unsigned long timeout)
433{
434    return -EINVAL;
435}
436
437static inline int pwm_enable(struct pwm_device *pwm)
438{
439    return -EINVAL;
440}
441
442static inline void pwm_disable(struct pwm_device *pwm)
443{
444}
445
446static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data)
447{
448    return -EINVAL;
449}
450
451static inline void *pwm_get_chip_data(struct pwm_device *pwm)
452{
453    return NULL;
454}
455
456static inline int pwmchip_add(struct pwm_chip *chip)
457{
458    return -EINVAL;
459}
460
461static inline int pwmchip_add_inversed(struct pwm_chip *chip)
462{
463    return -EINVAL;
464}
465
466static inline int pwmchip_remove(struct pwm_chip *chip)
467{
468    return -EINVAL;
469}
470
471static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip, unsigned int index, const char *label)
472{
473    return ERR_PTR(-ENODEV);
474}
475
476static inline struct pwm_device *pwm_get(struct device *dev, const char *consumer)
477{
478    return ERR_PTR(-ENODEV);
479}
480
481static inline struct pwm_device *of_pwm_get(struct device *dev, struct device_node *np, const char *con_id)
482{
483    return ERR_PTR(-ENODEV);
484}
485
486static inline void pwm_put(struct pwm_device *pwm)
487{
488}
489
490static inline struct pwm_device *devm_pwm_get(struct device *dev, const char *consumer)
491{
492    return ERR_PTR(-ENODEV);
493}
494
495static inline struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np, const char *con_id)
496{
497    return ERR_PTR(-ENODEV);
498}
499
500static inline struct pwm_device *devm_fwnode_pwm_get(struct device *dev, struct fwnode_handle *fwnode,
501                                                     const char *con_id)
502{
503    return ERR_PTR(-ENODEV);
504}
505
506static inline void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
507{
508}
509#endif
510
511static inline void pwm_apply_args(struct pwm_device *pwm)
512{
513    struct pwm_state state = {};
514
515    /*
516     * PWM users calling pwm_apply_args() expect to have a fresh config
517     * where the polarity and period are set according to pwm_args info.
518     * The problem is, polarity can only be changed when the PWM is
519     * disabled.
520     *
521     * PWM drivers supporting hardware readout may declare the PWM device
522     * as enabled, and prevent polarity setting, which changes from the
523     * existing behavior, where all PWM devices are declared as disabled
524     * at startup (even if they are actually enabled), thus authorizing
525     * polarity setting.
526     *
527     * To fulfill this requirement, we apply a new state which disables
528     * the PWM device and set the reference period and polarity config.
529     *
530     * Note that PWM users requiring a smooth handover between the
531     * bootloader and the kernel (like critical regulators controlled by
532     * PWM devices) will have to switch to the atomic API and avoid calling
533     * pwm_apply_args().
534     */
535
536    state.enabled = false;
537    state.polarity = pwm->args.polarity;
538    state.period = pwm->args.period;
539
540    pwm_apply_state(pwm, &state);
541}
542
543struct pwm_lookup {
544    struct list_head list;
545    const char *provider;
546    unsigned int index;
547    const char *dev_id;
548    const char *con_id;
549    unsigned int period;
550    enum pwm_polarity polarity;
551    const char *module; /* optional, may be NULL */
552};
553
554#define PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, _period, _polarity, _module)                       \
555    {                                                                                                                  \
556        .provider = (_provider), .index = (_index), .dev_id = (_dev_id), .con_id = (_con_id), .period = (_period),     \
557        .polarity = (_polarity), .module = (_module),                                                                  \
558    }
559
560#define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity)                                            \
561    PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, _period, _polarity, NULL)
562
563#if IS_ENABLED(CONFIG_PWM)
564void pwm_add_table(struct pwm_lookup *table, size_t num);
565void pwm_remove_table(struct pwm_lookup *table, size_t num);
566#else
567static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
568{
569}
570
571static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
572{
573}
574#endif
575
576#ifdef CONFIG_PWM_SYSFS
577void pwmchip_sysfs_export(struct pwm_chip *chip);
578void pwmchip_sysfs_unexport(struct pwm_chip *chip);
579#else
580static inline void pwmchip_sysfs_export(struct pwm_chip *chip)
581{
582}
583
584static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip)
585{
586}
587#endif /* CONFIG_PWM_SYSFS */
588
589#endif /* __LINUX_PWM_H */
590