xref: /linux-6.15/include/linux/pwm.h (revision 17e40c25)
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 
1133c651a3SUwe Kleine-König 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 
52*17e40c25SUwe Kleine-König /**
53*17e40c25SUwe Kleine-König  * struct pwm_waveform - description of a PWM waveform
54*17e40c25SUwe Kleine-König  * @period_length_ns: PWM period
55*17e40c25SUwe Kleine-König  * @duty_length_ns: PWM duty cycle
56*17e40c25SUwe Kleine-König  * @duty_offset_ns: offset of the rising edge from the period's start
57*17e40c25SUwe Kleine-König  *
58*17e40c25SUwe Kleine-König  * This is a representation of a PWM waveform alternative to struct pwm_state
59*17e40c25SUwe Kleine-König  * below. It's more expressive than struct pwm_state as it contains a
60*17e40c25SUwe Kleine-König  * duty_offset_ns and so can represent offsets other than zero (with .polarity =
61*17e40c25SUwe Kleine-König  * PWM_POLARITY_NORMAL) and period - duty_cycle (.polarity =
62*17e40c25SUwe Kleine-König  * PWM_POLARITY_INVERSED).
63*17e40c25SUwe Kleine-König  *
64*17e40c25SUwe Kleine-König  * Note there is no explicit bool for enabled. A "disabled" PWM is represented
65*17e40c25SUwe Kleine-König  * by .period_length_ns = 0. Note further that the behaviour of a "disabled" PWM
66*17e40c25SUwe Kleine-König  * is undefined. Depending on the hardware's capabilities it might drive the
67*17e40c25SUwe Kleine-König  * active or inactive level, go high-z or even continue to toggle.
68*17e40c25SUwe Kleine-König  *
69*17e40c25SUwe Kleine-König  * The unit for all three members is nanoseconds.
70*17e40c25SUwe Kleine-König  */
71*17e40c25SUwe Kleine-König struct pwm_waveform {
72*17e40c25SUwe Kleine-König 	u64 period_length_ns;
73*17e40c25SUwe Kleine-König 	u64 duty_length_ns;
74*17e40c25SUwe Kleine-König 	u64 duty_offset_ns;
75*17e40c25SUwe Kleine-König };
76*17e40c25SUwe 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  */
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 
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 
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 
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 
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 
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  */
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
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
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
27927938fd8SRasmus Villemoes  * @apply: atomically apply a new PWM config
280a96d3659SUwe Kleine-König  * @get_state: get the current PWM state.
2810c2498f1SSascha Hauer  */
2820c2498f1SSascha Hauer struct pwm_ops {
2836bc7064aSThierry Reding 	int (*request)(struct pwm_chip *chip, struct pwm_device *pwm);
2846bc7064aSThierry Reding 	void (*free)(struct pwm_chip *chip, struct pwm_device *pwm);
2853a3d1a4eSLee Jones 	int (*capture)(struct pwm_chip *chip, struct pwm_device *pwm,
2863a3d1a4eSLee Jones 		       struct pwm_capture *result, unsigned long timeout);
287*17e40c25SUwe Kleine-König 
288*17e40c25SUwe Kleine-König 	size_t sizeof_wfhw;
289*17e40c25SUwe Kleine-König 	int (*round_waveform_tohw)(struct pwm_chip *chip, struct pwm_device *pwm,
290*17e40c25SUwe Kleine-König 				   const struct pwm_waveform *wf, void *wfhw);
291*17e40c25SUwe Kleine-König 	int (*round_waveform_fromhw)(struct pwm_chip *chip, struct pwm_device *pwm,
292*17e40c25SUwe Kleine-König 				     const void *wfhw, struct pwm_waveform *wf);
293*17e40c25SUwe Kleine-König 	int (*read_waveform)(struct pwm_chip *chip, struct pwm_device *pwm,
294*17e40c25SUwe Kleine-König 			    void *wfhw);
295*17e40c25SUwe Kleine-König 	int (*write_waveform)(struct pwm_chip *chip, struct pwm_device *pwm,
296*17e40c25SUwe Kleine-König 			      const void *wfhw);
297*17e40c25SUwe Kleine-König 
2985ec803edSBoris Brezillon 	int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm,
29971523d18SUwe Kleine-König 		     const struct pwm_state *state);
3006c452cffSUwe Kleine-König 	int (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,
30115fa8a43SBoris Brezillon 			 struct pwm_state *state);
3020c2498f1SSascha Hauer };
3030c2498f1SSascha Hauer 
3040c2498f1SSascha Hauer /**
305f051c466SThierry Reding  * struct pwm_chip - abstract a PWM controller
306f051c466SThierry Reding  * @dev: device providing the PWMs
307f051c466SThierry Reding  * @ops: callbacks for this PWM controller
308384461abSUwe Kleine-König  * @owner: module providing this chip
3092d91123aSUwe Kleine-König  * @id: unique number of this PWM chip
310f051c466SThierry Reding  * @npwm: number of PWMs controlled by this chip
31104883802SThierry Reding  * @of_xlate: request a PWM device given a device tree PWM specifier
3127170d3beSSean Young  * @atomic: can the driver's ->apply() be called in atomic context
31305947224SUwe Kleine-König  * @uses_pwmchip_alloc: signals if pwmchip_allow was used to allocate this chip
3141cc2e1faSUwe Kleine-König  * @operational: signals if the chip can be used (or is already deregistered)
3151cc2e1faSUwe Kleine-König  * @nonatomic_lock: mutex for nonatomic chips
3161cc2e1faSUwe Kleine-König  * @atomic_lock: mutex for atomic chips
3175d0a4c11SUwe Kleine-König  * @pwms: array of PWM devices allocated by the framework
3180c2498f1SSascha Hauer  */
3190c2498f1SSascha Hauer struct pwm_chip {
3204c56b143SUwe Kleine-König 	struct device dev;
321f051c466SThierry Reding 	const struct pwm_ops *ops;
322384461abSUwe Kleine-König 	struct module *owner;
32354c86dd2SUwe Kleine-König 	unsigned int id;
324f051c466SThierry Reding 	unsigned int npwm;
325f051c466SThierry Reding 
326b4f78ff7SUwe Kleine-König 	struct pwm_device * (*of_xlate)(struct pwm_chip *chip,
3277299ab70SThierry Reding 					const struct of_phandle_args *args);
3287170d3beSSean Young 	bool atomic;
3295d0a4c11SUwe Kleine-König 
3305d0a4c11SUwe Kleine-König 	/* only used internally by the PWM framework */
33105947224SUwe Kleine-König 	bool uses_pwmchip_alloc;
3321cc2e1faSUwe Kleine-König 	bool operational;
3331cc2e1faSUwe Kleine-König 	union {
3341cc2e1faSUwe Kleine-König 		/*
3351cc2e1faSUwe Kleine-König 		 * depending on the chip being atomic or not either the mutex or
3361cc2e1faSUwe Kleine-König 		 * the spinlock is used. It protects .operational and
3371cc2e1faSUwe Kleine-König 		 * synchronizes the callbacks in .ops
3381cc2e1faSUwe Kleine-König 		 */
3391cc2e1faSUwe Kleine-König 		struct mutex nonatomic_lock;
3401cc2e1faSUwe Kleine-König 		spinlock_t atomic_lock;
3411cc2e1faSUwe Kleine-König 	};
342ee37bf50SUwe Kleine-König 	struct pwm_device pwms[] __counted_by(npwm);
3430c2498f1SSascha Hauer };
3440c2498f1SSascha Hauer 
3454e59267cSUwe Kleine-König static inline struct device *pwmchip_parent(const struct pwm_chip *chip)
3464e59267cSUwe Kleine-König {
3474c56b143SUwe Kleine-König 	return chip->dev.parent;
3484e59267cSUwe Kleine-König }
3494e59267cSUwe Kleine-König 
35024003d50SUwe Kleine-König static inline void *pwmchip_get_drvdata(struct pwm_chip *chip)
35124003d50SUwe Kleine-König {
35238ae7142SUwe Kleine-König 	return dev_get_drvdata(&chip->dev);
35324003d50SUwe Kleine-König }
35424003d50SUwe Kleine-König 
35524003d50SUwe Kleine-König static inline void pwmchip_set_drvdata(struct pwm_chip *chip, void *data)
35624003d50SUwe Kleine-König {
35738ae7142SUwe Kleine-König 	dev_set_drvdata(&chip->dev, data);
35824003d50SUwe Kleine-König }
35924003d50SUwe Kleine-König 
3600bcf168bSTushar Behera #if IS_ENABLED(CONFIG_PWM)
3615ec803edSBoris Brezillon /* PWM user APIs */
362c748a6d7SSean Young int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state);
3637170d3beSSean Young int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state);
3645ec803edSBoris Brezillon int pwm_adjust_config(struct pwm_device *pwm);
3655ec803edSBoris Brezillon 
3665ec803edSBoris Brezillon /**
3675ec803edSBoris Brezillon  * pwm_config() - change a PWM device configuration
3685ec803edSBoris Brezillon  * @pwm: PWM device
3695ec803edSBoris Brezillon  * @duty_ns: "on" time (in nanoseconds)
3705ec803edSBoris Brezillon  * @period_ns: duration (in nanoseconds) of one cycle
3715ec803edSBoris Brezillon  *
3725ec803edSBoris Brezillon  * Returns: 0 on success or a negative error code on failure.
3735ec803edSBoris Brezillon  */
3745ec803edSBoris Brezillon static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
3755ec803edSBoris Brezillon 			     int period_ns)
3765ec803edSBoris Brezillon {
3775ec803edSBoris Brezillon 	struct pwm_state state;
3785ec803edSBoris Brezillon 
3795ec803edSBoris Brezillon 	if (!pwm)
3805ec803edSBoris Brezillon 		return -EINVAL;
3815ec803edSBoris Brezillon 
382ef2bf499SBrian Norris 	if (duty_ns < 0 || period_ns < 0)
383ef2bf499SBrian Norris 		return -EINVAL;
384ef2bf499SBrian Norris 
3855ec803edSBoris Brezillon 	pwm_get_state(pwm, &state);
3865ec803edSBoris Brezillon 	if (state.duty_cycle == duty_ns && state.period == period_ns)
3875ec803edSBoris Brezillon 		return 0;
3885ec803edSBoris Brezillon 
3895ec803edSBoris Brezillon 	state.duty_cycle = duty_ns;
3905ec803edSBoris Brezillon 	state.period = period_ns;
391c748a6d7SSean Young 	return pwm_apply_might_sleep(pwm, &state);
3925ec803edSBoris Brezillon }
3935ec803edSBoris Brezillon 
3945ec803edSBoris Brezillon /**
3955ec803edSBoris Brezillon  * pwm_enable() - start a PWM output toggling
3965ec803edSBoris Brezillon  * @pwm: PWM device
3975ec803edSBoris Brezillon  *
3985ec803edSBoris Brezillon  * Returns: 0 on success or a negative error code on failure.
3995ec803edSBoris Brezillon  */
4005ec803edSBoris Brezillon static inline int pwm_enable(struct pwm_device *pwm)
4015ec803edSBoris Brezillon {
4025ec803edSBoris Brezillon 	struct pwm_state state;
4035ec803edSBoris Brezillon 
4045ec803edSBoris Brezillon 	if (!pwm)
4055ec803edSBoris Brezillon 		return -EINVAL;
4065ec803edSBoris Brezillon 
4075ec803edSBoris Brezillon 	pwm_get_state(pwm, &state);
4085ec803edSBoris Brezillon 	if (state.enabled)
4095ec803edSBoris Brezillon 		return 0;
4105ec803edSBoris Brezillon 
4115ec803edSBoris Brezillon 	state.enabled = true;
412c748a6d7SSean Young 	return pwm_apply_might_sleep(pwm, &state);
4135ec803edSBoris Brezillon }
4145ec803edSBoris Brezillon 
4155ec803edSBoris Brezillon /**
4165ec803edSBoris Brezillon  * pwm_disable() - stop a PWM output toggling
4175ec803edSBoris Brezillon  * @pwm: PWM device
4185ec803edSBoris Brezillon  */
4195ec803edSBoris Brezillon static inline void pwm_disable(struct pwm_device *pwm)
4205ec803edSBoris Brezillon {
4215ec803edSBoris Brezillon 	struct pwm_state state;
4225ec803edSBoris Brezillon 
4235ec803edSBoris Brezillon 	if (!pwm)
4245ec803edSBoris Brezillon 		return;
4255ec803edSBoris Brezillon 
4265ec803edSBoris Brezillon 	pwm_get_state(pwm, &state);
4275ec803edSBoris Brezillon 	if (!state.enabled)
4285ec803edSBoris Brezillon 		return;
4295ec803edSBoris Brezillon 
4305ec803edSBoris Brezillon 	state.enabled = false;
431c748a6d7SSean Young 	pwm_apply_might_sleep(pwm, &state);
4325ec803edSBoris Brezillon }
4335ec803edSBoris Brezillon 
4347170d3beSSean Young /**
4357170d3beSSean Young  * pwm_might_sleep() - is pwm_apply_atomic() supported?
4367170d3beSSean Young  * @pwm: PWM device
4377170d3beSSean Young  *
4387170d3beSSean Young  * Returns: false if pwm_apply_atomic() can be called from atomic context.
4397170d3beSSean Young  */
4407170d3beSSean Young static inline bool pwm_might_sleep(struct pwm_device *pwm)
4417170d3beSSean Young {
4427170d3beSSean Young 	return !pwm->chip->atomic;
4437170d3beSSean Young }
4447170d3beSSean Young 
4455ec803edSBoris Brezillon /* PWM provider APIs */
446024913dbSUwe Kleine-König void pwmchip_put(struct pwm_chip *chip);
447024913dbSUwe Kleine-König struct pwm_chip *pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv);
448024913dbSUwe Kleine-König struct pwm_chip *devm_pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv);
449024913dbSUwe Kleine-König 
450384461abSUwe Kleine-König int __pwmchip_add(struct pwm_chip *chip, struct module *owner);
451384461abSUwe Kleine-König #define pwmchip_add(chip) __pwmchip_add(chip, THIS_MODULE)
4528083f58dSUwe Kleine-König void pwmchip_remove(struct pwm_chip *chip);
453bcda91bfSUwe Kleine-König 
454384461abSUwe Kleine-König int __devm_pwmchip_add(struct device *dev, struct pwm_chip *chip, struct module *owner);
455384461abSUwe Kleine-König #define devm_pwmchip_add(dev, chip) __devm_pwmchip_add(dev, chip, THIS_MODULE)
456bcda91bfSUwe Kleine-König 
457b4f78ff7SUwe Kleine-König struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *chip,
45883af2402SPhilip, Avinash 		const struct of_phandle_args *args);
459b4f78ff7SUwe Kleine-König struct pwm_device *of_pwm_single_xlate(struct pwm_chip *chip,
4603ab7b6acSBjorn Andersson 				       const struct of_phandle_args *args);
46183af2402SPhilip, Avinash 
462d4c0c470SPeter Ujfalusi struct pwm_device *pwm_get(struct device *dev, const char *con_id);
4638138d2ddSThierry Reding void pwm_put(struct pwm_device *pwm);
4648138d2ddSThierry Reding 
465d4c0c470SPeter Ujfalusi struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
4664a6ef8e3SNikolaus Voss struct pwm_device *devm_fwnode_pwm_get(struct device *dev,
4674a6ef8e3SNikolaus Voss 				       struct fwnode_handle *fwnode,
4684a6ef8e3SNikolaus Voss 				       const char *con_id);
4690bcf168bSTushar Behera #else
4707170d3beSSean Young static inline bool pwm_might_sleep(struct pwm_device *pwm)
4717170d3beSSean Young {
4727170d3beSSean Young 	return true;
4737170d3beSSean Young }
4747170d3beSSean Young 
475c748a6d7SSean Young static inline int pwm_apply_might_sleep(struct pwm_device *pwm,
4765ec803edSBoris Brezillon 					const struct pwm_state *state)
4775ec803edSBoris Brezillon {
4784ad91a22SUwe Kleine-König 	might_sleep();
479dc518b37SSean Young 	return -EOPNOTSUPP;
4805ec803edSBoris Brezillon }
4815ec803edSBoris Brezillon 
4827170d3beSSean Young static inline int pwm_apply_atomic(struct pwm_device *pwm,
4837170d3beSSean Young 				   const struct pwm_state *state)
4847170d3beSSean Young {
4857170d3beSSean Young 	return -EOPNOTSUPP;
4867170d3beSSean Young }
4877170d3beSSean Young 
4885ec803edSBoris Brezillon static inline int pwm_adjust_config(struct pwm_device *pwm)
4895ec803edSBoris Brezillon {
490dc518b37SSean Young 	return -EOPNOTSUPP;
4915ec803edSBoris Brezillon }
4925ec803edSBoris Brezillon 
4935ec803edSBoris Brezillon static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
4945ec803edSBoris Brezillon 			     int period_ns)
4955ec803edSBoris Brezillon {
4964ad91a22SUwe Kleine-König 	might_sleep();
4975ec803edSBoris Brezillon 	return -EINVAL;
4985ec803edSBoris Brezillon }
4995ec803edSBoris Brezillon 
5005ec803edSBoris Brezillon static inline int pwm_enable(struct pwm_device *pwm)
5015ec803edSBoris Brezillon {
5024ad91a22SUwe Kleine-König 	might_sleep();
5035ec803edSBoris Brezillon 	return -EINVAL;
5045ec803edSBoris Brezillon }
5055ec803edSBoris Brezillon 
5065ec803edSBoris Brezillon static inline void pwm_disable(struct pwm_device *pwm)
5075ec803edSBoris Brezillon {
5084ad91a22SUwe Kleine-König 	might_sleep();
5095ec803edSBoris Brezillon }
5105ec803edSBoris Brezillon 
511024913dbSUwe Kleine-König static inline void pwmchip_put(struct pwm_chip *chip)
512024913dbSUwe Kleine-König {
513024913dbSUwe Kleine-König }
514024913dbSUwe Kleine-König 
515024913dbSUwe Kleine-König static inline struct pwm_chip *pwmchip_alloc(struct device *parent,
516024913dbSUwe Kleine-König 					     unsigned int npwm,
517024913dbSUwe Kleine-König 					     size_t sizeof_priv)
518024913dbSUwe Kleine-König {
519024913dbSUwe Kleine-König 	return ERR_PTR(-EINVAL);
520024913dbSUwe Kleine-König }
521024913dbSUwe Kleine-König 
522024913dbSUwe Kleine-König static inline struct pwm_chip *devm_pwmchip_alloc(struct device *parent,
523024913dbSUwe Kleine-König 						  unsigned int npwm,
524024913dbSUwe Kleine-König 						  size_t sizeof_priv)
525024913dbSUwe Kleine-König {
526024913dbSUwe Kleine-König 	return pwmchip_alloc(parent, npwm, sizeof_priv);
527024913dbSUwe Kleine-König }
528024913dbSUwe Kleine-König 
5290bcf168bSTushar Behera static inline int pwmchip_add(struct pwm_chip *chip)
5300bcf168bSTushar Behera {
5310bcf168bSTushar Behera 	return -EINVAL;
5320bcf168bSTushar Behera }
5330bcf168bSTushar Behera 
5340bcf168bSTushar Behera static inline int pwmchip_remove(struct pwm_chip *chip)
5350bcf168bSTushar Behera {
5360bcf168bSTushar Behera 	return -EINVAL;
5370bcf168bSTushar Behera }
5380bcf168bSTushar Behera 
53988da4e81SAndy Shevchenko static inline int devm_pwmchip_add(struct device *dev, struct pwm_chip *chip)
54088da4e81SAndy Shevchenko {
54188da4e81SAndy Shevchenko 	return -EINVAL;
54288da4e81SAndy Shevchenko }
54388da4e81SAndy Shevchenko 
5440bcf168bSTushar Behera static inline struct pwm_device *pwm_get(struct device *dev,
5450bcf168bSTushar Behera 					 const char *consumer)
5460bcf168bSTushar Behera {
54727d9a4d6SUwe Kleine-König 	might_sleep();
5480bcf168bSTushar Behera 	return ERR_PTR(-ENODEV);
5490bcf168bSTushar Behera }
5500bcf168bSTushar Behera 
5510bcf168bSTushar Behera static inline void pwm_put(struct pwm_device *pwm)
5520bcf168bSTushar Behera {
55327d9a4d6SUwe Kleine-König 	might_sleep();
5540bcf168bSTushar Behera }
5550bcf168bSTushar Behera 
5560bcf168bSTushar Behera static inline struct pwm_device *devm_pwm_get(struct device *dev,
5570bcf168bSTushar Behera 					      const char *consumer)
5580bcf168bSTushar Behera {
55927d9a4d6SUwe Kleine-König 	might_sleep();
5600bcf168bSTushar Behera 	return ERR_PTR(-ENODEV);
5610bcf168bSTushar Behera }
5620bcf168bSTushar Behera 
5634a6ef8e3SNikolaus Voss static inline struct pwm_device *
5644a6ef8e3SNikolaus Voss devm_fwnode_pwm_get(struct device *dev, struct fwnode_handle *fwnode,
5654a6ef8e3SNikolaus Voss 		    const char *con_id)
5664a6ef8e3SNikolaus Voss {
56727d9a4d6SUwe Kleine-König 	might_sleep();
5684a6ef8e3SNikolaus Voss 	return ERR_PTR(-ENODEV);
5694a6ef8e3SNikolaus Voss }
5700bcf168bSTushar Behera #endif
5716354316dSAlexandre Courbot 
5725ec803edSBoris Brezillon static inline void pwm_apply_args(struct pwm_device *pwm)
5735ec803edSBoris Brezillon {
57433cdcee0SBoris Brezillon 	struct pwm_state state = { };
57533cdcee0SBoris Brezillon 
5765ec803edSBoris Brezillon 	/*
5775ec803edSBoris Brezillon 	 * PWM users calling pwm_apply_args() expect to have a fresh config
5785ec803edSBoris Brezillon 	 * where the polarity and period are set according to pwm_args info.
5795ec803edSBoris Brezillon 	 * The problem is, polarity can only be changed when the PWM is
5805ec803edSBoris Brezillon 	 * disabled.
5815ec803edSBoris Brezillon 	 *
5825ec803edSBoris Brezillon 	 * PWM drivers supporting hardware readout may declare the PWM device
5835ec803edSBoris Brezillon 	 * as enabled, and prevent polarity setting, which changes from the
5845ec803edSBoris Brezillon 	 * existing behavior, where all PWM devices are declared as disabled
5855ec803edSBoris Brezillon 	 * at startup (even if they are actually enabled), thus authorizing
5865ec803edSBoris Brezillon 	 * polarity setting.
5875ec803edSBoris Brezillon 	 *
58833cdcee0SBoris Brezillon 	 * To fulfill this requirement, we apply a new state which disables
58933cdcee0SBoris Brezillon 	 * the PWM device and set the reference period and polarity config.
5905ec803edSBoris Brezillon 	 *
5915ec803edSBoris Brezillon 	 * Note that PWM users requiring a smooth handover between the
5925ec803edSBoris Brezillon 	 * bootloader and the kernel (like critical regulators controlled by
5935ec803edSBoris Brezillon 	 * PWM devices) will have to switch to the atomic API and avoid calling
5945ec803edSBoris Brezillon 	 * pwm_apply_args().
5955ec803edSBoris Brezillon 	 */
59633cdcee0SBoris Brezillon 
59733cdcee0SBoris Brezillon 	state.enabled = false;
59833cdcee0SBoris Brezillon 	state.polarity = pwm->args.polarity;
59933cdcee0SBoris Brezillon 	state.period = pwm->args.period;
6009e40ee18SClemens Gruber 	state.usage_power = false;
60133cdcee0SBoris Brezillon 
602c748a6d7SSean Young 	pwm_apply_might_sleep(pwm, &state);
6035ec803edSBoris Brezillon }
6045ec803edSBoris Brezillon 
6058138d2ddSThierry Reding struct pwm_lookup {
6068138d2ddSThierry Reding 	struct list_head list;
6078138d2ddSThierry Reding 	const char *provider;
6088138d2ddSThierry Reding 	unsigned int index;
6098138d2ddSThierry Reding 	const char *dev_id;
6108138d2ddSThierry Reding 	const char *con_id;
6113796ce1dSAlexandre Belloni 	unsigned int period;
6123796ce1dSAlexandre Belloni 	enum pwm_polarity polarity;
613b526a314SHans de Goede 	const char *module; /* optional, may be NULL */
6148138d2ddSThierry Reding };
6158138d2ddSThierry Reding 
616b526a314SHans de Goede #define PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id,	\
617b526a314SHans de Goede 			       _period, _polarity, _module)		\
6188138d2ddSThierry Reding 	{								\
6198138d2ddSThierry Reding 		.provider = _provider,					\
6208138d2ddSThierry Reding 		.index = _index,					\
6218138d2ddSThierry Reding 		.dev_id = _dev_id,					\
6228138d2ddSThierry Reding 		.con_id = _con_id,					\
62342844029SAlexandre Belloni 		.period = _period,					\
624b526a314SHans de Goede 		.polarity = _polarity,					\
625b526a314SHans de Goede 		.module = _module,					\
6268138d2ddSThierry Reding 	}
6278138d2ddSThierry Reding 
628b526a314SHans de Goede #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
629b526a314SHans de Goede 	PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, _period, \
630b526a314SHans de Goede 			       _polarity, NULL)
631b526a314SHans de Goede 
6320bcf168bSTushar Behera #if IS_ENABLED(CONFIG_PWM)
6338138d2ddSThierry Reding void pwm_add_table(struct pwm_lookup *table, size_t num);
634efb0de55SShobhit Kumar void pwm_remove_table(struct pwm_lookup *table, size_t num);
6350bcf168bSTushar Behera #else
6360bcf168bSTushar Behera static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
6370bcf168bSTushar Behera {
6380bcf168bSTushar Behera }
639efb0de55SShobhit Kumar 
640efb0de55SShobhit Kumar static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
641efb0de55SShobhit Kumar {
642efb0de55SShobhit Kumar }
6430c2498f1SSascha Hauer #endif
6440c2498f1SSascha Hauer 
6455243ef8bSMark Vels #endif /* __LINUX_PWM_H */
646