1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21a189b97SRussell King #ifndef __LINUX_PWM_H
31a189b97SRussell King #define __LINUX_PWM_H
41a189b97SRussell King
54c56b143SUwe Kleine-König #include <linux/device.h>
60bcf168bSTushar Behera #include <linux/err.h>
733c651a3SUwe Kleine-König #include <linux/module.h>
8d1cd2142SJonathan Richardson #include <linux/mutex.h>
97299ab70SThierry Reding #include <linux/of.h>
107299ab70SThierry Reding
11cdd30ebbSPeter Zijlstra MODULE_IMPORT_NS("PWM");
1233c651a3SUwe Kleine-König
130c2498f1SSascha Hauer struct pwm_chip;
140c2498f1SSascha Hauer
150aa0869cSPhilip, Avinash /**
160aa0869cSPhilip, Avinash * enum pwm_polarity - polarity of a PWM signal
170aa0869cSPhilip, Avinash * @PWM_POLARITY_NORMAL: a high signal for the duration of the duty-
180aa0869cSPhilip, Avinash * cycle, followed by a low signal for the remainder of the pulse
190aa0869cSPhilip, Avinash * period
200aa0869cSPhilip, Avinash * @PWM_POLARITY_INVERSED: a low signal for the duration of the duty-
210aa0869cSPhilip, Avinash * cycle, followed by a high signal for the remainder of the pulse
220aa0869cSPhilip, Avinash * period
230aa0869cSPhilip, Avinash */
240aa0869cSPhilip, Avinash enum pwm_polarity {
250aa0869cSPhilip, Avinash PWM_POLARITY_NORMAL,
260aa0869cSPhilip, Avinash PWM_POLARITY_INVERSED,
270aa0869cSPhilip, Avinash };
280aa0869cSPhilip, Avinash
29e39c0df1SBoris Brezillon /**
30e39c0df1SBoris Brezillon * struct pwm_args - board-dependent PWM arguments
31e39c0df1SBoris Brezillon * @period: reference period
32e39c0df1SBoris Brezillon * @polarity: reference polarity
33e39c0df1SBoris Brezillon *
34e39c0df1SBoris Brezillon * This structure describes board-dependent arguments attached to a PWM
35e39c0df1SBoris Brezillon * device. These arguments are usually retrieved from the PWM lookup table or
36e39c0df1SBoris Brezillon * device tree.
37e39c0df1SBoris Brezillon *
38e39c0df1SBoris Brezillon * Do not confuse this with the PWM state: PWM arguments represent the initial
39e39c0df1SBoris Brezillon * configuration that users want to use on this PWM device rather than the
40e39c0df1SBoris Brezillon * current PWM hardware state.
41e39c0df1SBoris Brezillon */
42e39c0df1SBoris Brezillon struct pwm_args {
43a9d887dcSGuru Das Srinagesh u64 period;
44e39c0df1SBoris Brezillon enum pwm_polarity polarity;
45e39c0df1SBoris Brezillon };
46e39c0df1SBoris Brezillon
47f051c466SThierry Reding enum {
48d27abbfdSDan Carpenter PWMF_REQUESTED = 0,
49d27abbfdSDan Carpenter PWMF_EXPORTED = 1,
50f051c466SThierry Reding };
51f051c466SThierry Reding
5217e40c25SUwe Kleine-König /**
5317e40c25SUwe Kleine-König * struct pwm_waveform - description of a PWM waveform
5417e40c25SUwe Kleine-König * @period_length_ns: PWM period
5517e40c25SUwe Kleine-König * @duty_length_ns: PWM duty cycle
5617e40c25SUwe Kleine-König * @duty_offset_ns: offset of the rising edge from the period's start
5717e40c25SUwe Kleine-König *
5817e40c25SUwe Kleine-König * This is a representation of a PWM waveform alternative to struct pwm_state
5917e40c25SUwe Kleine-König * below. It's more expressive than struct pwm_state as it contains a
6017e40c25SUwe Kleine-König * duty_offset_ns and so can represent offsets other than zero (with .polarity =
6117e40c25SUwe Kleine-König * PWM_POLARITY_NORMAL) and period - duty_cycle (.polarity =
6217e40c25SUwe Kleine-König * PWM_POLARITY_INVERSED).
6317e40c25SUwe Kleine-König *
6417e40c25SUwe Kleine-König * Note there is no explicit bool for enabled. A "disabled" PWM is represented
6517e40c25SUwe Kleine-König * by .period_length_ns = 0. Note further that the behaviour of a "disabled" PWM
6617e40c25SUwe Kleine-König * is undefined. Depending on the hardware's capabilities it might drive the
6717e40c25SUwe Kleine-König * active or inactive level, go high-z or even continue to toggle.
6817e40c25SUwe Kleine-König *
6917e40c25SUwe Kleine-König * The unit for all three members is nanoseconds.
7017e40c25SUwe Kleine-König */
7117e40c25SUwe Kleine-König struct pwm_waveform {
7217e40c25SUwe Kleine-König u64 period_length_ns;
7317e40c25SUwe Kleine-König u64 duty_length_ns;
7417e40c25SUwe Kleine-König u64 duty_offset_ns;
7517e40c25SUwe Kleine-König };
7617e40c25SUwe Kleine-König
7743a276b0SBoris Brezillon /*
7843a276b0SBoris Brezillon * struct pwm_state - state of a PWM channel
7943a276b0SBoris Brezillon * @period: PWM period (in nanoseconds)
8043a276b0SBoris Brezillon * @duty_cycle: PWM duty cycle (in nanoseconds)
8143a276b0SBoris Brezillon * @polarity: PWM polarity
8209a7e4a3SBoris Brezillon * @enabled: PWM enabled status
839e40ee18SClemens Gruber * @usage_power: If set, the PWM driver is only required to maintain the power
849e40ee18SClemens Gruber * output but has more freedom regarding signal form.
859e40ee18SClemens Gruber * If supported, the signal can be optimized, for example to
869e40ee18SClemens Gruber * improve EMI by phase shifting individual channels.
8743a276b0SBoris Brezillon */
8843a276b0SBoris Brezillon struct pwm_state {
89a9d887dcSGuru Das Srinagesh u64 period;
90a9d887dcSGuru Das Srinagesh u64 duty_cycle;
9143a276b0SBoris Brezillon enum pwm_polarity polarity;
9209a7e4a3SBoris Brezillon bool enabled;
939e40ee18SClemens Gruber bool usage_power;
9443a276b0SBoris Brezillon };
9543a276b0SBoris Brezillon
9604883802SThierry Reding /**
9704883802SThierry Reding * struct pwm_device - PWM channel object
9804883802SThierry Reding * @label: name of the PWM device
9904883802SThierry Reding * @flags: flags associated with the PWM device
10004883802SThierry Reding * @hwpwm: per-chip relative index of the PWM device
10104883802SThierry Reding * @chip: PWM chip providing this PWM device
102e39c0df1SBoris Brezillon * @args: PWM arguments
1033ad1f3a3SUwe Kleine-König * @state: last applied state
1043ad1f3a3SUwe Kleine-König * @last: last implemented state (for PWM_DEBUG)
10504883802SThierry Reding */
106f051c466SThierry Reding struct pwm_device {
107f051c466SThierry Reding const char *label;
108f051c466SThierry Reding unsigned long flags;
109f051c466SThierry Reding unsigned int hwpwm;
110f051c466SThierry Reding struct pwm_chip *chip;
111f051c466SThierry Reding
112e39c0df1SBoris Brezillon struct pwm_args args;
11343a276b0SBoris Brezillon struct pwm_state state;
1143ad1f3a3SUwe Kleine-König struct pwm_state last;
115f051c466SThierry Reding };
116f051c466SThierry Reding
11743a276b0SBoris Brezillon /**
11843a276b0SBoris Brezillon * pwm_get_state() - retrieve the current PWM state
11943a276b0SBoris Brezillon * @pwm: PWM device
12043a276b0SBoris Brezillon * @state: state to fill with the current PWM state
1211a7a6e80SUwe Kleine-König *
1221a7a6e80SUwe Kleine-König * The returned PWM state represents the state that was applied by a previous call to
123c748a6d7SSean Young * pwm_apply_might_sleep(). Drivers may have to slightly tweak that state before programming it to
124c748a6d7SSean Young * hardware. If pwm_apply_might_sleep() was never called, this returns either the current hardware
1251a7a6e80SUwe Kleine-König * state (if supported) or the default settings.
12643a276b0SBoris Brezillon */
pwm_get_state(const struct pwm_device * pwm,struct pwm_state * state)12743a276b0SBoris Brezillon static inline void pwm_get_state(const struct pwm_device *pwm,
12843a276b0SBoris Brezillon struct pwm_state *state)
12943a276b0SBoris Brezillon {
13043a276b0SBoris Brezillon *state = pwm->state;
13143a276b0SBoris Brezillon }
13243a276b0SBoris Brezillon
pwm_is_enabled(const struct pwm_device * pwm)1335c31252cSBoris Brezillon static inline bool pwm_is_enabled(const struct pwm_device *pwm)
1345c31252cSBoris Brezillon {
13509a7e4a3SBoris Brezillon struct pwm_state state;
13609a7e4a3SBoris Brezillon
13709a7e4a3SBoris Brezillon pwm_get_state(pwm, &state);
13809a7e4a3SBoris Brezillon
13909a7e4a3SBoris Brezillon return state.enabled;
1405c31252cSBoris Brezillon }
1415c31252cSBoris Brezillon
pwm_get_period(const struct pwm_device * pwm)142a9d887dcSGuru Das Srinagesh static inline u64 pwm_get_period(const struct pwm_device *pwm)
143f051c466SThierry Reding {
14443a276b0SBoris Brezillon struct pwm_state state;
14543a276b0SBoris Brezillon
14643a276b0SBoris Brezillon pwm_get_state(pwm, &state);
14743a276b0SBoris Brezillon
14843a276b0SBoris Brezillon return state.period;
149f051c466SThierry Reding }
150f051c466SThierry Reding
pwm_get_duty_cycle(const struct pwm_device * pwm)151a9d887dcSGuru Das Srinagesh static inline u64 pwm_get_duty_cycle(const struct pwm_device *pwm)
15276abbddeSH Hartley Sweeten {
15343a276b0SBoris Brezillon struct pwm_state state;
15443a276b0SBoris Brezillon
15543a276b0SBoris Brezillon pwm_get_state(pwm, &state);
15643a276b0SBoris Brezillon
15743a276b0SBoris Brezillon return state.duty_cycle;
15876abbddeSH Hartley Sweeten }
15976abbddeSH Hartley Sweeten
pwm_get_polarity(const struct pwm_device * pwm)160011e7631SBoris Brezillon static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm)
161011e7631SBoris Brezillon {
16243a276b0SBoris Brezillon struct pwm_state state;
16343a276b0SBoris Brezillon
16443a276b0SBoris Brezillon pwm_get_state(pwm, &state);
16543a276b0SBoris Brezillon
16643a276b0SBoris Brezillon return state.polarity;
167011e7631SBoris Brezillon }
168011e7631SBoris Brezillon
pwm_get_args(const struct pwm_device * pwm,struct pwm_args * args)169e39c0df1SBoris Brezillon static inline void pwm_get_args(const struct pwm_device *pwm,
170e39c0df1SBoris Brezillon struct pwm_args *args)
171e39c0df1SBoris Brezillon {
172e39c0df1SBoris Brezillon *args = pwm->args;
173e39c0df1SBoris Brezillon }
174e39c0df1SBoris Brezillon
1750c2498f1SSascha Hauer /**
176c748a6d7SSean Young * pwm_init_state() - prepare a new state to be applied with pwm_apply_might_sleep()
177a6a0dbbcSBoris Brezillon * @pwm: PWM device
178a6a0dbbcSBoris Brezillon * @state: state to fill with the prepared PWM state
179a6a0dbbcSBoris Brezillon *
180a6a0dbbcSBoris Brezillon * This functions prepares a state that can later be tweaked and applied
181c748a6d7SSean Young * to the PWM device with pwm_apply_might_sleep(). This is a convenient function
182a6a0dbbcSBoris Brezillon * that first retrieves the current PWM state and the replaces the period
183a6a0dbbcSBoris Brezillon * and polarity fields with the reference values defined in pwm->args.
184a6a0dbbcSBoris Brezillon * Once the function returns, you can adjust the ->enabled and ->duty_cycle
185c748a6d7SSean Young * fields according to your needs before calling pwm_apply_might_sleep().
186a6a0dbbcSBoris Brezillon *
187a6a0dbbcSBoris Brezillon * ->duty_cycle is initially set to zero to avoid cases where the current
188a6a0dbbcSBoris Brezillon * ->duty_cycle value exceed the pwm_args->period one, which would trigger
189c748a6d7SSean Young * an error if the user calls pwm_apply_might_sleep() without adjusting ->duty_cycle
190a6a0dbbcSBoris Brezillon * first.
191a6a0dbbcSBoris Brezillon */
pwm_init_state(const struct pwm_device * pwm,struct pwm_state * state)192a6a0dbbcSBoris Brezillon static inline void pwm_init_state(const struct pwm_device *pwm,
193a6a0dbbcSBoris Brezillon struct pwm_state *state)
194a6a0dbbcSBoris Brezillon {
195a6a0dbbcSBoris Brezillon struct pwm_args args;
196a6a0dbbcSBoris Brezillon
197a6a0dbbcSBoris Brezillon /* First get the current state. */
198a6a0dbbcSBoris Brezillon pwm_get_state(pwm, state);
199a6a0dbbcSBoris Brezillon
200a6a0dbbcSBoris Brezillon /* Then fill it with the reference config */
201a6a0dbbcSBoris Brezillon pwm_get_args(pwm, &args);
202a6a0dbbcSBoris Brezillon
203a6a0dbbcSBoris Brezillon state->period = args.period;
204a6a0dbbcSBoris Brezillon state->polarity = args.polarity;
205a6a0dbbcSBoris Brezillon state->duty_cycle = 0;
2069e40ee18SClemens Gruber state->usage_power = false;
207a6a0dbbcSBoris Brezillon }
208a6a0dbbcSBoris Brezillon
209a6a0dbbcSBoris Brezillon /**
210f6f3bddfSBoris Brezillon * pwm_get_relative_duty_cycle() - Get a relative duty cycle value
211f6f3bddfSBoris Brezillon * @state: PWM state to extract the duty cycle from
212f6f3bddfSBoris Brezillon * @scale: target scale of the relative duty cycle
213f6f3bddfSBoris Brezillon *
214f6f3bddfSBoris Brezillon * This functions converts the absolute duty cycle stored in @state (expressed
215f6f3bddfSBoris Brezillon * in nanosecond) into a value relative to the period.
216f6f3bddfSBoris Brezillon *
217f6f3bddfSBoris Brezillon * For example if you want to get the duty_cycle expressed in percent, call:
218f6f3bddfSBoris Brezillon *
219f6f3bddfSBoris Brezillon * pwm_get_state(pwm, &state);
220f6f3bddfSBoris Brezillon * duty = pwm_get_relative_duty_cycle(&state, 100);
221f6f3bddfSBoris Brezillon */
222f6f3bddfSBoris Brezillon static inline unsigned int
pwm_get_relative_duty_cycle(const struct pwm_state * state,unsigned int scale)223f6f3bddfSBoris Brezillon pwm_get_relative_duty_cycle(const struct pwm_state *state, unsigned int scale)
224f6f3bddfSBoris Brezillon {
225f6f3bddfSBoris Brezillon if (!state->period)
226f6f3bddfSBoris Brezillon return 0;
227f6f3bddfSBoris Brezillon
228f6f3bddfSBoris Brezillon return DIV_ROUND_CLOSEST_ULL((u64)state->duty_cycle * scale,
229f6f3bddfSBoris Brezillon state->period);
230f6f3bddfSBoris Brezillon }
231f6f3bddfSBoris Brezillon
232f6f3bddfSBoris Brezillon /**
233f6f3bddfSBoris Brezillon * pwm_set_relative_duty_cycle() - Set a relative duty cycle value
234f6f3bddfSBoris Brezillon * @state: PWM state to fill
235f6f3bddfSBoris Brezillon * @duty_cycle: relative duty cycle value
236f6f3bddfSBoris Brezillon * @scale: scale in which @duty_cycle is expressed
237f6f3bddfSBoris Brezillon *
238f6f3bddfSBoris Brezillon * This functions converts a relative into an absolute duty cycle (expressed
239f6f3bddfSBoris Brezillon * in nanoseconds), and puts the result in state->duty_cycle.
240f6f3bddfSBoris Brezillon *
241f6f3bddfSBoris Brezillon * For example if you want to configure a 50% duty cycle, call:
242f6f3bddfSBoris Brezillon *
243f6f3bddfSBoris Brezillon * pwm_init_state(pwm, &state);
244f6f3bddfSBoris Brezillon * pwm_set_relative_duty_cycle(&state, 50, 100);
245c748a6d7SSean Young * pwm_apply_might_sleep(pwm, &state);
246f6f3bddfSBoris Brezillon *
247f6f3bddfSBoris Brezillon * This functions returns -EINVAL if @duty_cycle and/or @scale are
248f6f3bddfSBoris Brezillon * inconsistent (@scale == 0 or @duty_cycle > @scale).
249f6f3bddfSBoris Brezillon */
250f6f3bddfSBoris Brezillon static inline int
pwm_set_relative_duty_cycle(struct pwm_state * state,unsigned int duty_cycle,unsigned int scale)251f6f3bddfSBoris Brezillon pwm_set_relative_duty_cycle(struct pwm_state *state, unsigned int duty_cycle,
252f6f3bddfSBoris Brezillon unsigned int scale)
253f6f3bddfSBoris Brezillon {
254f6f3bddfSBoris Brezillon if (!scale || duty_cycle > scale)
255f6f3bddfSBoris Brezillon return -EINVAL;
256f6f3bddfSBoris Brezillon
257f6f3bddfSBoris Brezillon state->duty_cycle = DIV_ROUND_CLOSEST_ULL((u64)duty_cycle *
258f6f3bddfSBoris Brezillon state->period,
259f6f3bddfSBoris Brezillon scale);
260f6f3bddfSBoris Brezillon
261f6f3bddfSBoris Brezillon return 0;
262f6f3bddfSBoris Brezillon }
263f6f3bddfSBoris Brezillon
264f6f3bddfSBoris Brezillon /**
265ef2e35d9SUwe Kleine-König * struct pwm_capture - PWM capture data
266ef2e35d9SUwe Kleine-König * @period: period of the PWM signal (in nanoseconds)
267ef2e35d9SUwe Kleine-König * @duty_cycle: duty cycle of the PWM signal (in nanoseconds)
268ef2e35d9SUwe Kleine-König */
269ef2e35d9SUwe Kleine-König struct pwm_capture {
270ef2e35d9SUwe Kleine-König unsigned int period;
271ef2e35d9SUwe Kleine-König unsigned int duty_cycle;
272ef2e35d9SUwe Kleine-König };
273ef2e35d9SUwe Kleine-König
274ef2e35d9SUwe Kleine-König /**
2750c2498f1SSascha Hauer * struct pwm_ops - PWM controller operations
2760c2498f1SSascha Hauer * @request: optional hook for requesting a PWM
2770c2498f1SSascha Hauer * @free: optional hook for freeing a PWM
2783a3d1a4eSLee Jones * @capture: capture and report PWM signal
279dab9cd4bSUwe Kleine-König * @sizeof_wfhw: size (in bytes) of driver specific waveform presentation
280dab9cd4bSUwe Kleine-König * @round_waveform_tohw: convert a struct pwm_waveform to driver specific presentation
281dab9cd4bSUwe Kleine-König * @round_waveform_fromhw: convert a driver specific waveform presentation to struct pwm_waveform
282dab9cd4bSUwe Kleine-König * @read_waveform: read driver specific waveform presentation from hardware
283dab9cd4bSUwe Kleine-König * @write_waveform: write driver specific waveform presentation to hardware
28427938fd8SRasmus Villemoes * @apply: atomically apply a new PWM config
285a96d3659SUwe Kleine-König * @get_state: get the current PWM state.
2860c2498f1SSascha Hauer */
2870c2498f1SSascha Hauer struct pwm_ops {
2886bc7064aSThierry Reding int (*request)(struct pwm_chip *chip, struct pwm_device *pwm);
2896bc7064aSThierry Reding void (*free)(struct pwm_chip *chip, struct pwm_device *pwm);
2903a3d1a4eSLee Jones int (*capture)(struct pwm_chip *chip, struct pwm_device *pwm,
2913a3d1a4eSLee Jones struct pwm_capture *result, unsigned long timeout);
29217e40c25SUwe Kleine-König
29317e40c25SUwe Kleine-König size_t sizeof_wfhw;
29417e40c25SUwe Kleine-König int (*round_waveform_tohw)(struct pwm_chip *chip, struct pwm_device *pwm,
29517e40c25SUwe Kleine-König const struct pwm_waveform *wf, void *wfhw);
29617e40c25SUwe Kleine-König int (*round_waveform_fromhw)(struct pwm_chip *chip, struct pwm_device *pwm,
29717e40c25SUwe Kleine-König const void *wfhw, struct pwm_waveform *wf);
29817e40c25SUwe Kleine-König int (*read_waveform)(struct pwm_chip *chip, struct pwm_device *pwm,
29917e40c25SUwe Kleine-König void *wfhw);
30017e40c25SUwe Kleine-König int (*write_waveform)(struct pwm_chip *chip, struct pwm_device *pwm,
30117e40c25SUwe Kleine-König const void *wfhw);
30217e40c25SUwe Kleine-König
3035ec803edSBoris Brezillon int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm,
30471523d18SUwe Kleine-König const struct pwm_state *state);
3056c452cffSUwe Kleine-König int (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,
30615fa8a43SBoris Brezillon struct pwm_state *state);
3070c2498f1SSascha Hauer };
3080c2498f1SSascha Hauer
3090c2498f1SSascha Hauer /**
310f051c466SThierry Reding * struct pwm_chip - abstract a PWM controller
311f051c466SThierry Reding * @dev: device providing the PWMs
312f051c466SThierry Reding * @ops: callbacks for this PWM controller
313384461abSUwe Kleine-König * @owner: module providing this chip
3142d91123aSUwe Kleine-König * @id: unique number of this PWM chip
315f051c466SThierry Reding * @npwm: number of PWMs controlled by this chip
31604883802SThierry Reding * @of_xlate: request a PWM device given a device tree PWM specifier
3177170d3beSSean Young * @atomic: can the driver's ->apply() be called in atomic context
31805947224SUwe Kleine-König * @uses_pwmchip_alloc: signals if pwmchip_allow was used to allocate this chip
3191cc2e1faSUwe Kleine-König * @operational: signals if the chip can be used (or is already deregistered)
3201cc2e1faSUwe Kleine-König * @nonatomic_lock: mutex for nonatomic chips
3211cc2e1faSUwe Kleine-König * @atomic_lock: mutex for atomic chips
3225d0a4c11SUwe Kleine-König * @pwms: array of PWM devices allocated by the framework
3230c2498f1SSascha Hauer */
3240c2498f1SSascha Hauer struct pwm_chip {
3254c56b143SUwe Kleine-König struct device dev;
326f051c466SThierry Reding const struct pwm_ops *ops;
327384461abSUwe Kleine-König struct module *owner;
32854c86dd2SUwe Kleine-König unsigned int id;
329f051c466SThierry Reding unsigned int npwm;
330f051c466SThierry Reding
331b4f78ff7SUwe Kleine-König struct pwm_device * (*of_xlate)(struct pwm_chip *chip,
3327299ab70SThierry Reding const struct of_phandle_args *args);
3337170d3beSSean Young bool atomic;
3345d0a4c11SUwe Kleine-König
3355d0a4c11SUwe Kleine-König /* only used internally by the PWM framework */
33605947224SUwe Kleine-König bool uses_pwmchip_alloc;
3371cc2e1faSUwe Kleine-König bool operational;
3381cc2e1faSUwe Kleine-König union {
3391cc2e1faSUwe Kleine-König /*
3401cc2e1faSUwe Kleine-König * depending on the chip being atomic or not either the mutex or
3411cc2e1faSUwe Kleine-König * the spinlock is used. It protects .operational and
3421cc2e1faSUwe Kleine-König * synchronizes the callbacks in .ops
3431cc2e1faSUwe Kleine-König */
3441cc2e1faSUwe Kleine-König struct mutex nonatomic_lock;
3451cc2e1faSUwe Kleine-König spinlock_t atomic_lock;
3461cc2e1faSUwe Kleine-König };
347ee37bf50SUwe Kleine-König struct pwm_device pwms[] __counted_by(npwm);
3480c2498f1SSascha Hauer };
3490c2498f1SSascha Hauer
350da6b3537SUwe Kleine-König /**
351da6b3537SUwe Kleine-König * pwmchip_supports_waveform() - checks if the given chip supports waveform callbacks
352da6b3537SUwe Kleine-König * @chip: The pwm_chip to test
353da6b3537SUwe Kleine-König *
354da6b3537SUwe Kleine-König * Returns true iff the pwm chip support the waveform functions like
355da6b3537SUwe Kleine-König * pwm_set_waveform_might_sleep() and pwm_round_waveform_might_sleep()
356da6b3537SUwe Kleine-König */
pwmchip_supports_waveform(struct pwm_chip * chip)357da6b3537SUwe Kleine-König static inline bool pwmchip_supports_waveform(struct pwm_chip *chip)
358da6b3537SUwe Kleine-König {
359da6b3537SUwe Kleine-König /*
360da6b3537SUwe Kleine-König * only check for .write_waveform(). If that is available,
361da6b3537SUwe Kleine-König * .round_waveform_tohw() and .round_waveform_fromhw() asserted to be
362da6b3537SUwe Kleine-König * available, too, in pwmchip_add().
363da6b3537SUwe Kleine-König */
364da6b3537SUwe Kleine-König return chip->ops->write_waveform != NULL;
365da6b3537SUwe Kleine-König }
366da6b3537SUwe Kleine-König
pwmchip_parent(const struct pwm_chip * chip)3674e59267cSUwe Kleine-König static inline struct device *pwmchip_parent(const struct pwm_chip *chip)
3684e59267cSUwe Kleine-König {
3694c56b143SUwe Kleine-König return chip->dev.parent;
3704e59267cSUwe Kleine-König }
3714e59267cSUwe Kleine-König
pwmchip_get_drvdata(struct pwm_chip * chip)37224003d50SUwe Kleine-König static inline void *pwmchip_get_drvdata(struct pwm_chip *chip)
37324003d50SUwe Kleine-König {
37438ae7142SUwe Kleine-König return dev_get_drvdata(&chip->dev);
37524003d50SUwe Kleine-König }
37624003d50SUwe Kleine-König
pwmchip_set_drvdata(struct pwm_chip * chip,void * data)37724003d50SUwe Kleine-König static inline void pwmchip_set_drvdata(struct pwm_chip *chip, void *data)
37824003d50SUwe Kleine-König {
37938ae7142SUwe Kleine-König dev_set_drvdata(&chip->dev, data);
38024003d50SUwe Kleine-König }
38124003d50SUwe Kleine-König
382*4b31eb55SUwe Kleine-König #if IS_REACHABLE(CONFIG_PWM)
3836c5126c6SUwe Kleine-König
3846c5126c6SUwe Kleine-König /* PWM consumer APIs */
3856c5126c6SUwe Kleine-König int pwm_round_waveform_might_sleep(struct pwm_device *pwm, struct pwm_waveform *wf);
3866c5126c6SUwe Kleine-König int pwm_get_waveform_might_sleep(struct pwm_device *pwm, struct pwm_waveform *wf);
3876c5126c6SUwe Kleine-König int pwm_set_waveform_might_sleep(struct pwm_device *pwm, const struct pwm_waveform *wf, bool exact);
388c748a6d7SSean Young int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state);
3897170d3beSSean Young int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state);
3902ea25aabSDavid Lechner int pwm_get_state_hw(struct pwm_device *pwm, struct pwm_state *state);
3915ec803edSBoris Brezillon int pwm_adjust_config(struct pwm_device *pwm);
3925ec803edSBoris Brezillon
3935ec803edSBoris Brezillon /**
3945ec803edSBoris Brezillon * pwm_config() - change a PWM device configuration
3955ec803edSBoris Brezillon * @pwm: PWM device
3965ec803edSBoris Brezillon * @duty_ns: "on" time (in nanoseconds)
3975ec803edSBoris Brezillon * @period_ns: duration (in nanoseconds) of one cycle
3985ec803edSBoris Brezillon *
3995ec803edSBoris Brezillon * Returns: 0 on success or a negative error code on failure.
4005ec803edSBoris Brezillon */
pwm_config(struct pwm_device * pwm,int duty_ns,int period_ns)4015ec803edSBoris Brezillon static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
4025ec803edSBoris Brezillon int period_ns)
4035ec803edSBoris Brezillon {
4045ec803edSBoris Brezillon struct pwm_state state;
4055ec803edSBoris Brezillon
4065ec803edSBoris Brezillon if (!pwm)
4075ec803edSBoris Brezillon return -EINVAL;
4085ec803edSBoris Brezillon
409ef2bf499SBrian Norris if (duty_ns < 0 || period_ns < 0)
410ef2bf499SBrian Norris return -EINVAL;
411ef2bf499SBrian Norris
4125ec803edSBoris Brezillon pwm_get_state(pwm, &state);
4135ec803edSBoris Brezillon if (state.duty_cycle == duty_ns && state.period == period_ns)
4145ec803edSBoris Brezillon return 0;
4155ec803edSBoris Brezillon
4165ec803edSBoris Brezillon state.duty_cycle = duty_ns;
4175ec803edSBoris Brezillon state.period = period_ns;
418c748a6d7SSean Young return pwm_apply_might_sleep(pwm, &state);
4195ec803edSBoris Brezillon }
4205ec803edSBoris Brezillon
4215ec803edSBoris Brezillon /**
4225ec803edSBoris Brezillon * pwm_enable() - start a PWM output toggling
4235ec803edSBoris Brezillon * @pwm: PWM device
4245ec803edSBoris Brezillon *
4255ec803edSBoris Brezillon * Returns: 0 on success or a negative error code on failure.
4265ec803edSBoris Brezillon */
pwm_enable(struct pwm_device * pwm)4275ec803edSBoris Brezillon static inline int pwm_enable(struct pwm_device *pwm)
4285ec803edSBoris Brezillon {
4295ec803edSBoris Brezillon struct pwm_state state;
4305ec803edSBoris Brezillon
4315ec803edSBoris Brezillon if (!pwm)
4325ec803edSBoris Brezillon return -EINVAL;
4335ec803edSBoris Brezillon
4345ec803edSBoris Brezillon pwm_get_state(pwm, &state);
4355ec803edSBoris Brezillon if (state.enabled)
4365ec803edSBoris Brezillon return 0;
4375ec803edSBoris Brezillon
4385ec803edSBoris Brezillon state.enabled = true;
439c748a6d7SSean Young return pwm_apply_might_sleep(pwm, &state);
4405ec803edSBoris Brezillon }
4415ec803edSBoris Brezillon
4425ec803edSBoris Brezillon /**
4435ec803edSBoris Brezillon * pwm_disable() - stop a PWM output toggling
4445ec803edSBoris Brezillon * @pwm: PWM device
4455ec803edSBoris Brezillon */
pwm_disable(struct pwm_device * pwm)4465ec803edSBoris Brezillon static inline void pwm_disable(struct pwm_device *pwm)
4475ec803edSBoris Brezillon {
4485ec803edSBoris Brezillon struct pwm_state state;
4495ec803edSBoris Brezillon
4505ec803edSBoris Brezillon if (!pwm)
4515ec803edSBoris Brezillon return;
4525ec803edSBoris Brezillon
4535ec803edSBoris Brezillon pwm_get_state(pwm, &state);
4545ec803edSBoris Brezillon if (!state.enabled)
4555ec803edSBoris Brezillon return;
4565ec803edSBoris Brezillon
4575ec803edSBoris Brezillon state.enabled = false;
458c748a6d7SSean Young pwm_apply_might_sleep(pwm, &state);
4595ec803edSBoris Brezillon }
4605ec803edSBoris Brezillon
4617170d3beSSean Young /**
4627170d3beSSean Young * pwm_might_sleep() - is pwm_apply_atomic() supported?
4637170d3beSSean Young * @pwm: PWM device
4647170d3beSSean Young *
4657170d3beSSean Young * Returns: false if pwm_apply_atomic() can be called from atomic context.
4667170d3beSSean Young */
pwm_might_sleep(struct pwm_device * pwm)4677170d3beSSean Young static inline bool pwm_might_sleep(struct pwm_device *pwm)
4687170d3beSSean Young {
4697170d3beSSean Young return !pwm->chip->atomic;
4707170d3beSSean Young }
4717170d3beSSean Young
4725ec803edSBoris Brezillon /* PWM provider APIs */
473024913dbSUwe Kleine-König void pwmchip_put(struct pwm_chip *chip);
474024913dbSUwe Kleine-König struct pwm_chip *pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv);
475024913dbSUwe Kleine-König struct pwm_chip *devm_pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv);
476024913dbSUwe Kleine-König
477384461abSUwe Kleine-König int __pwmchip_add(struct pwm_chip *chip, struct module *owner);
478384461abSUwe Kleine-König #define pwmchip_add(chip) __pwmchip_add(chip, THIS_MODULE)
4798083f58dSUwe Kleine-König void pwmchip_remove(struct pwm_chip *chip);
480bcda91bfSUwe Kleine-König
481384461abSUwe Kleine-König int __devm_pwmchip_add(struct device *dev, struct pwm_chip *chip, struct module *owner);
482384461abSUwe Kleine-König #define devm_pwmchip_add(dev, chip) __devm_pwmchip_add(dev, chip, THIS_MODULE)
483bcda91bfSUwe Kleine-König
484b4f78ff7SUwe Kleine-König struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *chip,
48583af2402SPhilip, Avinash const struct of_phandle_args *args);
486b4f78ff7SUwe Kleine-König struct pwm_device *of_pwm_single_xlate(struct pwm_chip *chip,
4873ab7b6acSBjorn Andersson const struct of_phandle_args *args);
48883af2402SPhilip, Avinash
489d4c0c470SPeter Ujfalusi struct pwm_device *pwm_get(struct device *dev, const char *con_id);
4908138d2ddSThierry Reding void pwm_put(struct pwm_device *pwm);
4918138d2ddSThierry Reding
492d4c0c470SPeter Ujfalusi struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
4934a6ef8e3SNikolaus Voss struct pwm_device *devm_fwnode_pwm_get(struct device *dev,
4944a6ef8e3SNikolaus Voss struct fwnode_handle *fwnode,
4954a6ef8e3SNikolaus Voss const char *con_id);
4960bcf168bSTushar Behera #else
pwm_might_sleep(struct pwm_device * pwm)4977170d3beSSean Young static inline bool pwm_might_sleep(struct pwm_device *pwm)
4987170d3beSSean Young {
4997170d3beSSean Young return true;
5007170d3beSSean Young }
5017170d3beSSean Young
pwm_apply_might_sleep(struct pwm_device * pwm,const struct pwm_state * state)502c748a6d7SSean Young static inline int pwm_apply_might_sleep(struct pwm_device *pwm,
5035ec803edSBoris Brezillon const struct pwm_state *state)
5045ec803edSBoris Brezillon {
5054ad91a22SUwe Kleine-König might_sleep();
506dc518b37SSean Young return -EOPNOTSUPP;
5075ec803edSBoris Brezillon }
5085ec803edSBoris Brezillon
pwm_apply_atomic(struct pwm_device * pwm,const struct pwm_state * state)5097170d3beSSean Young static inline int pwm_apply_atomic(struct pwm_device *pwm,
5107170d3beSSean Young const struct pwm_state *state)
5117170d3beSSean Young {
5127170d3beSSean Young return -EOPNOTSUPP;
5137170d3beSSean Young }
5147170d3beSSean Young
pwm_get_state_hw(struct pwm_device * pwm,struct pwm_state * state)5152ea25aabSDavid Lechner static inline int pwm_get_state_hw(struct pwm_device *pwm, struct pwm_state *state)
5162ea25aabSDavid Lechner {
5172ea25aabSDavid Lechner return -EOPNOTSUPP;
5182ea25aabSDavid Lechner }
5192ea25aabSDavid Lechner
pwm_adjust_config(struct pwm_device * pwm)5205ec803edSBoris Brezillon static inline int pwm_adjust_config(struct pwm_device *pwm)
5215ec803edSBoris Brezillon {
522dc518b37SSean Young return -EOPNOTSUPP;
5235ec803edSBoris Brezillon }
5245ec803edSBoris Brezillon
pwm_config(struct pwm_device * pwm,int duty_ns,int period_ns)5255ec803edSBoris Brezillon static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
5265ec803edSBoris Brezillon int period_ns)
5275ec803edSBoris Brezillon {
5284ad91a22SUwe Kleine-König might_sleep();
5295ec803edSBoris Brezillon return -EINVAL;
5305ec803edSBoris Brezillon }
5315ec803edSBoris Brezillon
pwm_enable(struct pwm_device * pwm)5325ec803edSBoris Brezillon static inline int pwm_enable(struct pwm_device *pwm)
5335ec803edSBoris Brezillon {
5344ad91a22SUwe Kleine-König might_sleep();
5355ec803edSBoris Brezillon return -EINVAL;
5365ec803edSBoris Brezillon }
5375ec803edSBoris Brezillon
pwm_disable(struct pwm_device * pwm)5385ec803edSBoris Brezillon static inline void pwm_disable(struct pwm_device *pwm)
5395ec803edSBoris Brezillon {
5404ad91a22SUwe Kleine-König might_sleep();
5415ec803edSBoris Brezillon }
5425ec803edSBoris Brezillon
pwmchip_put(struct pwm_chip * chip)543024913dbSUwe Kleine-König static inline void pwmchip_put(struct pwm_chip *chip)
544024913dbSUwe Kleine-König {
545024913dbSUwe Kleine-König }
546024913dbSUwe Kleine-König
pwmchip_alloc(struct device * parent,unsigned int npwm,size_t sizeof_priv)547024913dbSUwe Kleine-König static inline struct pwm_chip *pwmchip_alloc(struct device *parent,
548024913dbSUwe Kleine-König unsigned int npwm,
549024913dbSUwe Kleine-König size_t sizeof_priv)
550024913dbSUwe Kleine-König {
551024913dbSUwe Kleine-König return ERR_PTR(-EINVAL);
552024913dbSUwe Kleine-König }
553024913dbSUwe Kleine-König
devm_pwmchip_alloc(struct device * parent,unsigned int npwm,size_t sizeof_priv)554024913dbSUwe Kleine-König static inline struct pwm_chip *devm_pwmchip_alloc(struct device *parent,
555024913dbSUwe Kleine-König unsigned int npwm,
556024913dbSUwe Kleine-König size_t sizeof_priv)
557024913dbSUwe Kleine-König {
558024913dbSUwe Kleine-König return pwmchip_alloc(parent, npwm, sizeof_priv);
559024913dbSUwe Kleine-König }
560024913dbSUwe Kleine-König
pwmchip_add(struct pwm_chip * chip)5610bcf168bSTushar Behera static inline int pwmchip_add(struct pwm_chip *chip)
5620bcf168bSTushar Behera {
5630bcf168bSTushar Behera return -EINVAL;
5640bcf168bSTushar Behera }
5650bcf168bSTushar Behera
pwmchip_remove(struct pwm_chip * chip)5660bcf168bSTushar Behera static inline int pwmchip_remove(struct pwm_chip *chip)
5670bcf168bSTushar Behera {
5680bcf168bSTushar Behera return -EINVAL;
5690bcf168bSTushar Behera }
5700bcf168bSTushar Behera
devm_pwmchip_add(struct device * dev,struct pwm_chip * chip)57188da4e81SAndy Shevchenko static inline int devm_pwmchip_add(struct device *dev, struct pwm_chip *chip)
57288da4e81SAndy Shevchenko {
57388da4e81SAndy Shevchenko return -EINVAL;
57488da4e81SAndy Shevchenko }
57588da4e81SAndy Shevchenko
pwm_get(struct device * dev,const char * consumer)5760bcf168bSTushar Behera static inline struct pwm_device *pwm_get(struct device *dev,
5770bcf168bSTushar Behera const char *consumer)
5780bcf168bSTushar Behera {
57927d9a4d6SUwe Kleine-König might_sleep();
5800bcf168bSTushar Behera return ERR_PTR(-ENODEV);
5810bcf168bSTushar Behera }
5820bcf168bSTushar Behera
pwm_put(struct pwm_device * pwm)5830bcf168bSTushar Behera static inline void pwm_put(struct pwm_device *pwm)
5840bcf168bSTushar Behera {
58527d9a4d6SUwe Kleine-König might_sleep();
5860bcf168bSTushar Behera }
5870bcf168bSTushar Behera
devm_pwm_get(struct device * dev,const char * consumer)5880bcf168bSTushar Behera static inline struct pwm_device *devm_pwm_get(struct device *dev,
5890bcf168bSTushar Behera const char *consumer)
5900bcf168bSTushar Behera {
59127d9a4d6SUwe Kleine-König might_sleep();
5920bcf168bSTushar Behera return ERR_PTR(-ENODEV);
5930bcf168bSTushar Behera }
5940bcf168bSTushar Behera
5954a6ef8e3SNikolaus Voss static inline struct pwm_device *
devm_fwnode_pwm_get(struct device * dev,struct fwnode_handle * fwnode,const char * con_id)5964a6ef8e3SNikolaus Voss devm_fwnode_pwm_get(struct device *dev, struct fwnode_handle *fwnode,
5974a6ef8e3SNikolaus Voss const char *con_id)
5984a6ef8e3SNikolaus Voss {
59927d9a4d6SUwe Kleine-König might_sleep();
6004a6ef8e3SNikolaus Voss return ERR_PTR(-ENODEV);
6014a6ef8e3SNikolaus Voss }
6020bcf168bSTushar Behera #endif
6036354316dSAlexandre Courbot
pwm_apply_args(struct pwm_device * pwm)6045ec803edSBoris Brezillon static inline void pwm_apply_args(struct pwm_device *pwm)
6055ec803edSBoris Brezillon {
60633cdcee0SBoris Brezillon struct pwm_state state = { };
60733cdcee0SBoris Brezillon
6085ec803edSBoris Brezillon /*
6095ec803edSBoris Brezillon * PWM users calling pwm_apply_args() expect to have a fresh config
6105ec803edSBoris Brezillon * where the polarity and period are set according to pwm_args info.
6115ec803edSBoris Brezillon * The problem is, polarity can only be changed when the PWM is
6125ec803edSBoris Brezillon * disabled.
6135ec803edSBoris Brezillon *
6145ec803edSBoris Brezillon * PWM drivers supporting hardware readout may declare the PWM device
6155ec803edSBoris Brezillon * as enabled, and prevent polarity setting, which changes from the
6165ec803edSBoris Brezillon * existing behavior, where all PWM devices are declared as disabled
6175ec803edSBoris Brezillon * at startup (even if they are actually enabled), thus authorizing
6185ec803edSBoris Brezillon * polarity setting.
6195ec803edSBoris Brezillon *
62033cdcee0SBoris Brezillon * To fulfill this requirement, we apply a new state which disables
62133cdcee0SBoris Brezillon * the PWM device and set the reference period and polarity config.
6225ec803edSBoris Brezillon *
6235ec803edSBoris Brezillon * Note that PWM users requiring a smooth handover between the
6245ec803edSBoris Brezillon * bootloader and the kernel (like critical regulators controlled by
6255ec803edSBoris Brezillon * PWM devices) will have to switch to the atomic API and avoid calling
6265ec803edSBoris Brezillon * pwm_apply_args().
6275ec803edSBoris Brezillon */
62833cdcee0SBoris Brezillon
62933cdcee0SBoris Brezillon state.enabled = false;
63033cdcee0SBoris Brezillon state.polarity = pwm->args.polarity;
63133cdcee0SBoris Brezillon state.period = pwm->args.period;
6329e40ee18SClemens Gruber state.usage_power = false;
63333cdcee0SBoris Brezillon
634c748a6d7SSean Young pwm_apply_might_sleep(pwm, &state);
6355ec803edSBoris Brezillon }
6365ec803edSBoris Brezillon
6378138d2ddSThierry Reding struct pwm_lookup {
6388138d2ddSThierry Reding struct list_head list;
6398138d2ddSThierry Reding const char *provider;
6408138d2ddSThierry Reding unsigned int index;
6418138d2ddSThierry Reding const char *dev_id;
6428138d2ddSThierry Reding const char *con_id;
6433796ce1dSAlexandre Belloni unsigned int period;
6443796ce1dSAlexandre Belloni enum pwm_polarity polarity;
645b526a314SHans de Goede const char *module; /* optional, may be NULL */
6468138d2ddSThierry Reding };
6478138d2ddSThierry Reding
648b526a314SHans de Goede #define PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, \
649b526a314SHans de Goede _period, _polarity, _module) \
6508138d2ddSThierry Reding { \
6518138d2ddSThierry Reding .provider = _provider, \
6528138d2ddSThierry Reding .index = _index, \
6538138d2ddSThierry Reding .dev_id = _dev_id, \
6548138d2ddSThierry Reding .con_id = _con_id, \
65542844029SAlexandre Belloni .period = _period, \
656b526a314SHans de Goede .polarity = _polarity, \
657b526a314SHans de Goede .module = _module, \
6588138d2ddSThierry Reding }
6598138d2ddSThierry Reding
660b526a314SHans de Goede #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
661b526a314SHans de Goede PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, _period, \
662b526a314SHans de Goede _polarity, NULL)
663b526a314SHans de Goede
664*4b31eb55SUwe Kleine-König #if IS_REACHABLE(CONFIG_PWM)
6658138d2ddSThierry Reding void pwm_add_table(struct pwm_lookup *table, size_t num);
666efb0de55SShobhit Kumar void pwm_remove_table(struct pwm_lookup *table, size_t num);
6670bcf168bSTushar Behera #else
pwm_add_table(struct pwm_lookup * table,size_t num)6680bcf168bSTushar Behera static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
6690bcf168bSTushar Behera {
6700bcf168bSTushar Behera }
671efb0de55SShobhit Kumar
pwm_remove_table(struct pwm_lookup * table,size_t num)672efb0de55SShobhit Kumar static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
673efb0de55SShobhit Kumar {
674efb0de55SShobhit Kumar }
6750c2498f1SSascha Hauer #endif
6760c2498f1SSascha Hauer
6775243ef8bSMark Vels #endif /* __LINUX_PWM_H */
678