xref: /linux-6.15/include/linux/pwm.h (revision ee37bf50)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21a189b97SRussell King #ifndef __LINUX_PWM_H
31a189b97SRussell King #define __LINUX_PWM_H
41a189b97SRussell King 
50bcf168bSTushar Behera #include <linux/err.h>
6d1cd2142SJonathan Richardson #include <linux/mutex.h>
77299ab70SThierry Reding #include <linux/of.h>
87299ab70SThierry Reding 
90c2498f1SSascha Hauer struct pwm_chip;
100c2498f1SSascha Hauer 
110aa0869cSPhilip, Avinash /**
120aa0869cSPhilip, Avinash  * enum pwm_polarity - polarity of a PWM signal
130aa0869cSPhilip, Avinash  * @PWM_POLARITY_NORMAL: a high signal for the duration of the duty-
140aa0869cSPhilip, Avinash  * cycle, followed by a low signal for the remainder of the pulse
150aa0869cSPhilip, Avinash  * period
160aa0869cSPhilip, Avinash  * @PWM_POLARITY_INVERSED: a low signal for the duration of the duty-
170aa0869cSPhilip, Avinash  * cycle, followed by a high signal for the remainder of the pulse
180aa0869cSPhilip, Avinash  * period
190aa0869cSPhilip, Avinash  */
200aa0869cSPhilip, Avinash enum pwm_polarity {
210aa0869cSPhilip, Avinash 	PWM_POLARITY_NORMAL,
220aa0869cSPhilip, Avinash 	PWM_POLARITY_INVERSED,
230aa0869cSPhilip, Avinash };
240aa0869cSPhilip, Avinash 
25e39c0df1SBoris Brezillon /**
26e39c0df1SBoris Brezillon  * struct pwm_args - board-dependent PWM arguments
27e39c0df1SBoris Brezillon  * @period: reference period
28e39c0df1SBoris Brezillon  * @polarity: reference polarity
29e39c0df1SBoris Brezillon  *
30e39c0df1SBoris Brezillon  * This structure describes board-dependent arguments attached to a PWM
31e39c0df1SBoris Brezillon  * device. These arguments are usually retrieved from the PWM lookup table or
32e39c0df1SBoris Brezillon  * device tree.
33e39c0df1SBoris Brezillon  *
34e39c0df1SBoris Brezillon  * Do not confuse this with the PWM state: PWM arguments represent the initial
35e39c0df1SBoris Brezillon  * configuration that users want to use on this PWM device rather than the
36e39c0df1SBoris Brezillon  * current PWM hardware state.
37e39c0df1SBoris Brezillon  */
38e39c0df1SBoris Brezillon struct pwm_args {
39a9d887dcSGuru Das Srinagesh 	u64 period;
40e39c0df1SBoris Brezillon 	enum pwm_polarity polarity;
41e39c0df1SBoris Brezillon };
42e39c0df1SBoris Brezillon 
43f051c466SThierry Reding enum {
44d27abbfdSDan Carpenter 	PWMF_REQUESTED = 0,
45d27abbfdSDan Carpenter 	PWMF_EXPORTED = 1,
46f051c466SThierry Reding };
47f051c466SThierry Reding 
4843a276b0SBoris Brezillon /*
4943a276b0SBoris Brezillon  * struct pwm_state - state of a PWM channel
5043a276b0SBoris Brezillon  * @period: PWM period (in nanoseconds)
5143a276b0SBoris Brezillon  * @duty_cycle: PWM duty cycle (in nanoseconds)
5243a276b0SBoris Brezillon  * @polarity: PWM polarity
5309a7e4a3SBoris Brezillon  * @enabled: PWM enabled status
549e40ee18SClemens Gruber  * @usage_power: If set, the PWM driver is only required to maintain the power
559e40ee18SClemens Gruber  *               output but has more freedom regarding signal form.
569e40ee18SClemens Gruber  *               If supported, the signal can be optimized, for example to
579e40ee18SClemens Gruber  *               improve EMI by phase shifting individual channels.
5843a276b0SBoris Brezillon  */
5943a276b0SBoris Brezillon struct pwm_state {
60a9d887dcSGuru Das Srinagesh 	u64 period;
61a9d887dcSGuru Das Srinagesh 	u64 duty_cycle;
6243a276b0SBoris Brezillon 	enum pwm_polarity polarity;
6309a7e4a3SBoris Brezillon 	bool enabled;
649e40ee18SClemens Gruber 	bool usage_power;
6543a276b0SBoris Brezillon };
6643a276b0SBoris Brezillon 
6704883802SThierry Reding /**
6804883802SThierry Reding  * struct pwm_device - PWM channel object
6904883802SThierry Reding  * @label: name of the PWM device
7004883802SThierry Reding  * @flags: flags associated with the PWM device
7104883802SThierry Reding  * @hwpwm: per-chip relative index of the PWM device
7204883802SThierry Reding  * @chip: PWM chip providing this PWM device
73e39c0df1SBoris Brezillon  * @args: PWM arguments
743ad1f3a3SUwe Kleine-König  * @state: last applied state
753ad1f3a3SUwe Kleine-König  * @last: last implemented state (for PWM_DEBUG)
7604883802SThierry Reding  */
77f051c466SThierry Reding struct pwm_device {
78f051c466SThierry Reding 	const char *label;
79f051c466SThierry Reding 	unsigned long flags;
80f051c466SThierry Reding 	unsigned int hwpwm;
81f051c466SThierry Reding 	struct pwm_chip *chip;
82f051c466SThierry Reding 
83e39c0df1SBoris Brezillon 	struct pwm_args args;
8443a276b0SBoris Brezillon 	struct pwm_state state;
853ad1f3a3SUwe Kleine-König 	struct pwm_state last;
86f051c466SThierry Reding };
87f051c466SThierry Reding 
8843a276b0SBoris Brezillon /**
8943a276b0SBoris Brezillon  * pwm_get_state() - retrieve the current PWM state
9043a276b0SBoris Brezillon  * @pwm: PWM device
9143a276b0SBoris Brezillon  * @state: state to fill with the current PWM state
921a7a6e80SUwe Kleine-König  *
931a7a6e80SUwe Kleine-König  * The returned PWM state represents the state that was applied by a previous call to
94c748a6d7SSean Young  * pwm_apply_might_sleep(). Drivers may have to slightly tweak that state before programming it to
95c748a6d7SSean Young  * hardware. If pwm_apply_might_sleep() was never called, this returns either the current hardware
961a7a6e80SUwe Kleine-König  * state (if supported) or the default settings.
9743a276b0SBoris Brezillon  */
9843a276b0SBoris Brezillon static inline void pwm_get_state(const struct pwm_device *pwm,
9943a276b0SBoris Brezillon 				 struct pwm_state *state)
10043a276b0SBoris Brezillon {
10143a276b0SBoris Brezillon 	*state = pwm->state;
10243a276b0SBoris Brezillon }
10343a276b0SBoris Brezillon 
1045c31252cSBoris Brezillon static inline bool pwm_is_enabled(const struct pwm_device *pwm)
1055c31252cSBoris Brezillon {
10609a7e4a3SBoris Brezillon 	struct pwm_state state;
10709a7e4a3SBoris Brezillon 
10809a7e4a3SBoris Brezillon 	pwm_get_state(pwm, &state);
10909a7e4a3SBoris Brezillon 
11009a7e4a3SBoris Brezillon 	return state.enabled;
1115c31252cSBoris Brezillon }
1125c31252cSBoris Brezillon 
113a9d887dcSGuru Das Srinagesh static inline u64 pwm_get_period(const struct pwm_device *pwm)
114f051c466SThierry Reding {
11543a276b0SBoris Brezillon 	struct pwm_state state;
11643a276b0SBoris Brezillon 
11743a276b0SBoris Brezillon 	pwm_get_state(pwm, &state);
11843a276b0SBoris Brezillon 
11943a276b0SBoris Brezillon 	return state.period;
120f051c466SThierry Reding }
121f051c466SThierry Reding 
122a9d887dcSGuru Das Srinagesh static inline u64 pwm_get_duty_cycle(const struct pwm_device *pwm)
12376abbddeSH Hartley Sweeten {
12443a276b0SBoris Brezillon 	struct pwm_state state;
12543a276b0SBoris Brezillon 
12643a276b0SBoris Brezillon 	pwm_get_state(pwm, &state);
12743a276b0SBoris Brezillon 
12843a276b0SBoris Brezillon 	return state.duty_cycle;
12976abbddeSH Hartley Sweeten }
13076abbddeSH Hartley Sweeten 
131011e7631SBoris Brezillon static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm)
132011e7631SBoris Brezillon {
13343a276b0SBoris Brezillon 	struct pwm_state state;
13443a276b0SBoris Brezillon 
13543a276b0SBoris Brezillon 	pwm_get_state(pwm, &state);
13643a276b0SBoris Brezillon 
13743a276b0SBoris Brezillon 	return state.polarity;
138011e7631SBoris Brezillon }
139011e7631SBoris Brezillon 
140e39c0df1SBoris Brezillon static inline void pwm_get_args(const struct pwm_device *pwm,
141e39c0df1SBoris Brezillon 				struct pwm_args *args)
142e39c0df1SBoris Brezillon {
143e39c0df1SBoris Brezillon 	*args = pwm->args;
144e39c0df1SBoris Brezillon }
145e39c0df1SBoris Brezillon 
1460c2498f1SSascha Hauer /**
147c748a6d7SSean Young  * pwm_init_state() - prepare a new state to be applied with pwm_apply_might_sleep()
148a6a0dbbcSBoris Brezillon  * @pwm: PWM device
149a6a0dbbcSBoris Brezillon  * @state: state to fill with the prepared PWM state
150a6a0dbbcSBoris Brezillon  *
151a6a0dbbcSBoris Brezillon  * This functions prepares a state that can later be tweaked and applied
152c748a6d7SSean Young  * to the PWM device with pwm_apply_might_sleep(). This is a convenient function
153a6a0dbbcSBoris Brezillon  * that first retrieves the current PWM state and the replaces the period
154a6a0dbbcSBoris Brezillon  * and polarity fields with the reference values defined in pwm->args.
155a6a0dbbcSBoris Brezillon  * Once the function returns, you can adjust the ->enabled and ->duty_cycle
156c748a6d7SSean Young  * fields according to your needs before calling pwm_apply_might_sleep().
157a6a0dbbcSBoris Brezillon  *
158a6a0dbbcSBoris Brezillon  * ->duty_cycle is initially set to zero to avoid cases where the current
159a6a0dbbcSBoris Brezillon  * ->duty_cycle value exceed the pwm_args->period one, which would trigger
160c748a6d7SSean Young  * an error if the user calls pwm_apply_might_sleep() without adjusting ->duty_cycle
161a6a0dbbcSBoris Brezillon  * first.
162a6a0dbbcSBoris Brezillon  */
163a6a0dbbcSBoris Brezillon static inline void pwm_init_state(const struct pwm_device *pwm,
164a6a0dbbcSBoris Brezillon 				  struct pwm_state *state)
165a6a0dbbcSBoris Brezillon {
166a6a0dbbcSBoris Brezillon 	struct pwm_args args;
167a6a0dbbcSBoris Brezillon 
168a6a0dbbcSBoris Brezillon 	/* First get the current state. */
169a6a0dbbcSBoris Brezillon 	pwm_get_state(pwm, state);
170a6a0dbbcSBoris Brezillon 
171a6a0dbbcSBoris Brezillon 	/* Then fill it with the reference config */
172a6a0dbbcSBoris Brezillon 	pwm_get_args(pwm, &args);
173a6a0dbbcSBoris Brezillon 
174a6a0dbbcSBoris Brezillon 	state->period = args.period;
175a6a0dbbcSBoris Brezillon 	state->polarity = args.polarity;
176a6a0dbbcSBoris Brezillon 	state->duty_cycle = 0;
1779e40ee18SClemens Gruber 	state->usage_power = false;
178a6a0dbbcSBoris Brezillon }
179a6a0dbbcSBoris Brezillon 
180a6a0dbbcSBoris Brezillon /**
181f6f3bddfSBoris Brezillon  * pwm_get_relative_duty_cycle() - Get a relative duty cycle value
182f6f3bddfSBoris Brezillon  * @state: PWM state to extract the duty cycle from
183f6f3bddfSBoris Brezillon  * @scale: target scale of the relative duty cycle
184f6f3bddfSBoris Brezillon  *
185f6f3bddfSBoris Brezillon  * This functions converts the absolute duty cycle stored in @state (expressed
186f6f3bddfSBoris Brezillon  * in nanosecond) into a value relative to the period.
187f6f3bddfSBoris Brezillon  *
188f6f3bddfSBoris Brezillon  * For example if you want to get the duty_cycle expressed in percent, call:
189f6f3bddfSBoris Brezillon  *
190f6f3bddfSBoris Brezillon  * pwm_get_state(pwm, &state);
191f6f3bddfSBoris Brezillon  * duty = pwm_get_relative_duty_cycle(&state, 100);
192f6f3bddfSBoris Brezillon  */
193f6f3bddfSBoris Brezillon static inline unsigned int
194f6f3bddfSBoris Brezillon pwm_get_relative_duty_cycle(const struct pwm_state *state, unsigned int scale)
195f6f3bddfSBoris Brezillon {
196f6f3bddfSBoris Brezillon 	if (!state->period)
197f6f3bddfSBoris Brezillon 		return 0;
198f6f3bddfSBoris Brezillon 
199f6f3bddfSBoris Brezillon 	return DIV_ROUND_CLOSEST_ULL((u64)state->duty_cycle * scale,
200f6f3bddfSBoris Brezillon 				     state->period);
201f6f3bddfSBoris Brezillon }
202f6f3bddfSBoris Brezillon 
203f6f3bddfSBoris Brezillon /**
204f6f3bddfSBoris Brezillon  * pwm_set_relative_duty_cycle() - Set a relative duty cycle value
205f6f3bddfSBoris Brezillon  * @state: PWM state to fill
206f6f3bddfSBoris Brezillon  * @duty_cycle: relative duty cycle value
207f6f3bddfSBoris Brezillon  * @scale: scale in which @duty_cycle is expressed
208f6f3bddfSBoris Brezillon  *
209f6f3bddfSBoris Brezillon  * This functions converts a relative into an absolute duty cycle (expressed
210f6f3bddfSBoris Brezillon  * in nanoseconds), and puts the result in state->duty_cycle.
211f6f3bddfSBoris Brezillon  *
212f6f3bddfSBoris Brezillon  * For example if you want to configure a 50% duty cycle, call:
213f6f3bddfSBoris Brezillon  *
214f6f3bddfSBoris Brezillon  * pwm_init_state(pwm, &state);
215f6f3bddfSBoris Brezillon  * pwm_set_relative_duty_cycle(&state, 50, 100);
216c748a6d7SSean Young  * pwm_apply_might_sleep(pwm, &state);
217f6f3bddfSBoris Brezillon  *
218f6f3bddfSBoris Brezillon  * This functions returns -EINVAL if @duty_cycle and/or @scale are
219f6f3bddfSBoris Brezillon  * inconsistent (@scale == 0 or @duty_cycle > @scale).
220f6f3bddfSBoris Brezillon  */
221f6f3bddfSBoris Brezillon static inline int
222f6f3bddfSBoris Brezillon pwm_set_relative_duty_cycle(struct pwm_state *state, unsigned int duty_cycle,
223f6f3bddfSBoris Brezillon 			    unsigned int scale)
224f6f3bddfSBoris Brezillon {
225f6f3bddfSBoris Brezillon 	if (!scale || duty_cycle > scale)
226f6f3bddfSBoris Brezillon 		return -EINVAL;
227f6f3bddfSBoris Brezillon 
228f6f3bddfSBoris Brezillon 	state->duty_cycle = DIV_ROUND_CLOSEST_ULL((u64)duty_cycle *
229f6f3bddfSBoris Brezillon 						  state->period,
230f6f3bddfSBoris Brezillon 						  scale);
231f6f3bddfSBoris Brezillon 
232f6f3bddfSBoris Brezillon 	return 0;
233f6f3bddfSBoris Brezillon }
234f6f3bddfSBoris Brezillon 
235f6f3bddfSBoris Brezillon /**
236ef2e35d9SUwe Kleine-König  * struct pwm_capture - PWM capture data
237ef2e35d9SUwe Kleine-König  * @period: period of the PWM signal (in nanoseconds)
238ef2e35d9SUwe Kleine-König  * @duty_cycle: duty cycle of the PWM signal (in nanoseconds)
239ef2e35d9SUwe Kleine-König  */
240ef2e35d9SUwe Kleine-König struct pwm_capture {
241ef2e35d9SUwe Kleine-König 	unsigned int period;
242ef2e35d9SUwe Kleine-König 	unsigned int duty_cycle;
243ef2e35d9SUwe Kleine-König };
244ef2e35d9SUwe Kleine-König 
245ef2e35d9SUwe Kleine-König /**
2460c2498f1SSascha Hauer  * struct pwm_ops - PWM controller operations
2470c2498f1SSascha Hauer  * @request: optional hook for requesting a PWM
2480c2498f1SSascha Hauer  * @free: optional hook for freeing a PWM
2493a3d1a4eSLee Jones  * @capture: capture and report PWM signal
25027938fd8SRasmus Villemoes  * @apply: atomically apply a new PWM config
25115fa8a43SBoris Brezillon  * @get_state: get the current PWM state. This function is only
25215fa8a43SBoris Brezillon  *	       called once per PWM device when the PWM chip is
25315fa8a43SBoris Brezillon  *	       registered.
2540c2498f1SSascha Hauer  */
2550c2498f1SSascha Hauer struct pwm_ops {
2566bc7064aSThierry Reding 	int (*request)(struct pwm_chip *chip, struct pwm_device *pwm);
2576bc7064aSThierry Reding 	void (*free)(struct pwm_chip *chip, struct pwm_device *pwm);
2583a3d1a4eSLee Jones 	int (*capture)(struct pwm_chip *chip, struct pwm_device *pwm,
2593a3d1a4eSLee Jones 		       struct pwm_capture *result, unsigned long timeout);
2605ec803edSBoris Brezillon 	int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm,
26171523d18SUwe Kleine-König 		     const struct pwm_state *state);
2626c452cffSUwe Kleine-König 	int (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,
26315fa8a43SBoris Brezillon 			 struct pwm_state *state);
2640c2498f1SSascha Hauer };
2650c2498f1SSascha Hauer 
2660c2498f1SSascha Hauer /**
267f051c466SThierry Reding  * struct pwm_chip - abstract a PWM controller
268f051c466SThierry Reding  * @dev: device providing the PWMs
269f051c466SThierry Reding  * @ops: callbacks for this PWM controller
270384461abSUwe Kleine-König  * @owner: module providing this chip
2712d91123aSUwe Kleine-König  * @id: unique number of this PWM chip
272f051c466SThierry Reding  * @npwm: number of PWMs controlled by this chip
27304883802SThierry Reding  * @of_xlate: request a PWM device given a device tree PWM specifier
2747170d3beSSean Young  * @atomic: can the driver's ->apply() be called in atomic context
27505947224SUwe Kleine-König  * @uses_pwmchip_alloc: signals if pwmchip_allow was used to allocate this chip
27624003d50SUwe Kleine-König  * @driver_data: Private pointer for driver specific info
2775d0a4c11SUwe Kleine-König  * @pwms: array of PWM devices allocated by the framework
2780c2498f1SSascha Hauer  */
2790c2498f1SSascha Hauer struct pwm_chip {
280f051c466SThierry Reding 	struct device *dev;
281f051c466SThierry Reding 	const struct pwm_ops *ops;
282384461abSUwe Kleine-König 	struct module *owner;
28354c86dd2SUwe Kleine-König 	unsigned int id;
284f051c466SThierry Reding 	unsigned int npwm;
285f051c466SThierry Reding 
286b4f78ff7SUwe Kleine-König 	struct pwm_device * (*of_xlate)(struct pwm_chip *chip,
2877299ab70SThierry Reding 					const struct of_phandle_args *args);
2887170d3beSSean Young 	bool atomic;
2895d0a4c11SUwe Kleine-König 
2905d0a4c11SUwe Kleine-König 	/* only used internally by the PWM framework */
29105947224SUwe Kleine-König 	bool uses_pwmchip_alloc;
29224003d50SUwe Kleine-König 	void *driver_data;
293*ee37bf50SUwe Kleine-König 	struct pwm_device pwms[] __counted_by(npwm);
2940c2498f1SSascha Hauer };
2950c2498f1SSascha Hauer 
2964e59267cSUwe Kleine-König static inline struct device *pwmchip_parent(const struct pwm_chip *chip)
2974e59267cSUwe Kleine-König {
2984e59267cSUwe Kleine-König 	return chip->dev;
2994e59267cSUwe Kleine-König }
3004e59267cSUwe Kleine-König 
30124003d50SUwe Kleine-König static inline void *pwmchip_get_drvdata(struct pwm_chip *chip)
30224003d50SUwe Kleine-König {
30324003d50SUwe Kleine-König 	/*
30424003d50SUwe Kleine-König 	 * After pwm_chip got a dedicated struct device, this can be replaced by
30524003d50SUwe Kleine-König 	 * dev_get_drvdata(&chip->dev);
30624003d50SUwe Kleine-König 	 */
30724003d50SUwe Kleine-König 	return chip->driver_data;
30824003d50SUwe Kleine-König }
30924003d50SUwe Kleine-König 
31024003d50SUwe Kleine-König static inline void pwmchip_set_drvdata(struct pwm_chip *chip, void *data)
31124003d50SUwe Kleine-König {
31224003d50SUwe Kleine-König 	/*
31324003d50SUwe Kleine-König 	 * After pwm_chip got a dedicated struct device, this can be replaced by
31424003d50SUwe Kleine-König 	 * dev_set_drvdata(&chip->dev, data);
31524003d50SUwe Kleine-König 	 */
31624003d50SUwe Kleine-König 	chip->driver_data = data;
31724003d50SUwe Kleine-König }
31824003d50SUwe Kleine-König 
3190bcf168bSTushar Behera #if IS_ENABLED(CONFIG_PWM)
3205ec803edSBoris Brezillon /* PWM user APIs */
321c748a6d7SSean Young int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state);
3227170d3beSSean Young int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state);
3235ec803edSBoris Brezillon int pwm_adjust_config(struct pwm_device *pwm);
3245ec803edSBoris Brezillon 
3255ec803edSBoris Brezillon /**
3265ec803edSBoris Brezillon  * pwm_config() - change a PWM device configuration
3275ec803edSBoris Brezillon  * @pwm: PWM device
3285ec803edSBoris Brezillon  * @duty_ns: "on" time (in nanoseconds)
3295ec803edSBoris Brezillon  * @period_ns: duration (in nanoseconds) of one cycle
3305ec803edSBoris Brezillon  *
3315ec803edSBoris Brezillon  * Returns: 0 on success or a negative error code on failure.
3325ec803edSBoris Brezillon  */
3335ec803edSBoris Brezillon static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
3345ec803edSBoris Brezillon 			     int period_ns)
3355ec803edSBoris Brezillon {
3365ec803edSBoris Brezillon 	struct pwm_state state;
3375ec803edSBoris Brezillon 
3385ec803edSBoris Brezillon 	if (!pwm)
3395ec803edSBoris Brezillon 		return -EINVAL;
3405ec803edSBoris Brezillon 
341ef2bf499SBrian Norris 	if (duty_ns < 0 || period_ns < 0)
342ef2bf499SBrian Norris 		return -EINVAL;
343ef2bf499SBrian Norris 
3445ec803edSBoris Brezillon 	pwm_get_state(pwm, &state);
3455ec803edSBoris Brezillon 	if (state.duty_cycle == duty_ns && state.period == period_ns)
3465ec803edSBoris Brezillon 		return 0;
3475ec803edSBoris Brezillon 
3485ec803edSBoris Brezillon 	state.duty_cycle = duty_ns;
3495ec803edSBoris Brezillon 	state.period = period_ns;
350c748a6d7SSean Young 	return pwm_apply_might_sleep(pwm, &state);
3515ec803edSBoris Brezillon }
3525ec803edSBoris Brezillon 
3535ec803edSBoris Brezillon /**
3545ec803edSBoris Brezillon  * pwm_enable() - start a PWM output toggling
3555ec803edSBoris Brezillon  * @pwm: PWM device
3565ec803edSBoris Brezillon  *
3575ec803edSBoris Brezillon  * Returns: 0 on success or a negative error code on failure.
3585ec803edSBoris Brezillon  */
3595ec803edSBoris Brezillon static inline int pwm_enable(struct pwm_device *pwm)
3605ec803edSBoris Brezillon {
3615ec803edSBoris Brezillon 	struct pwm_state state;
3625ec803edSBoris Brezillon 
3635ec803edSBoris Brezillon 	if (!pwm)
3645ec803edSBoris Brezillon 		return -EINVAL;
3655ec803edSBoris Brezillon 
3665ec803edSBoris Brezillon 	pwm_get_state(pwm, &state);
3675ec803edSBoris Brezillon 	if (state.enabled)
3685ec803edSBoris Brezillon 		return 0;
3695ec803edSBoris Brezillon 
3705ec803edSBoris Brezillon 	state.enabled = true;
371c748a6d7SSean Young 	return pwm_apply_might_sleep(pwm, &state);
3725ec803edSBoris Brezillon }
3735ec803edSBoris Brezillon 
3745ec803edSBoris Brezillon /**
3755ec803edSBoris Brezillon  * pwm_disable() - stop a PWM output toggling
3765ec803edSBoris Brezillon  * @pwm: PWM device
3775ec803edSBoris Brezillon  */
3785ec803edSBoris Brezillon static inline void pwm_disable(struct pwm_device *pwm)
3795ec803edSBoris Brezillon {
3805ec803edSBoris Brezillon 	struct pwm_state state;
3815ec803edSBoris Brezillon 
3825ec803edSBoris Brezillon 	if (!pwm)
3835ec803edSBoris Brezillon 		return;
3845ec803edSBoris Brezillon 
3855ec803edSBoris Brezillon 	pwm_get_state(pwm, &state);
3865ec803edSBoris Brezillon 	if (!state.enabled)
3875ec803edSBoris Brezillon 		return;
3885ec803edSBoris Brezillon 
3895ec803edSBoris Brezillon 	state.enabled = false;
390c748a6d7SSean Young 	pwm_apply_might_sleep(pwm, &state);
3915ec803edSBoris Brezillon }
3925ec803edSBoris Brezillon 
3937170d3beSSean Young /**
3947170d3beSSean Young  * pwm_might_sleep() - is pwm_apply_atomic() supported?
3957170d3beSSean Young  * @pwm: PWM device
3967170d3beSSean Young  *
3977170d3beSSean Young  * Returns: false if pwm_apply_atomic() can be called from atomic context.
3987170d3beSSean Young  */
3997170d3beSSean Young static inline bool pwm_might_sleep(struct pwm_device *pwm)
4007170d3beSSean Young {
4017170d3beSSean Young 	return !pwm->chip->atomic;
4027170d3beSSean Young }
4037170d3beSSean Young 
4045ec803edSBoris Brezillon /* PWM provider APIs */
4053a3d1a4eSLee Jones int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
4063a3d1a4eSLee Jones 		unsigned long timeout);
407f051c466SThierry Reding 
408024913dbSUwe Kleine-König void pwmchip_put(struct pwm_chip *chip);
409024913dbSUwe Kleine-König struct pwm_chip *pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv);
410024913dbSUwe Kleine-König struct pwm_chip *devm_pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv);
411024913dbSUwe Kleine-König 
412384461abSUwe Kleine-König int __pwmchip_add(struct pwm_chip *chip, struct module *owner);
413384461abSUwe Kleine-König #define pwmchip_add(chip) __pwmchip_add(chip, THIS_MODULE)
4148083f58dSUwe Kleine-König void pwmchip_remove(struct pwm_chip *chip);
415bcda91bfSUwe Kleine-König 
416384461abSUwe Kleine-König int __devm_pwmchip_add(struct device *dev, struct pwm_chip *chip, struct module *owner);
417384461abSUwe Kleine-König #define devm_pwmchip_add(dev, chip) __devm_pwmchip_add(dev, chip, THIS_MODULE)
418bcda91bfSUwe Kleine-König 
419f051c466SThierry Reding struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
420f051c466SThierry Reding 					 unsigned int index,
421f051c466SThierry Reding 					 const char *label);
4228138d2ddSThierry Reding 
423b4f78ff7SUwe Kleine-König struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *chip,
42483af2402SPhilip, Avinash 		const struct of_phandle_args *args);
425b4f78ff7SUwe Kleine-König struct pwm_device *of_pwm_single_xlate(struct pwm_chip *chip,
4263ab7b6acSBjorn Andersson 				       const struct of_phandle_args *args);
42783af2402SPhilip, Avinash 
428d4c0c470SPeter Ujfalusi struct pwm_device *pwm_get(struct device *dev, const char *con_id);
4298138d2ddSThierry Reding void pwm_put(struct pwm_device *pwm);
4308138d2ddSThierry Reding 
431d4c0c470SPeter Ujfalusi struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
4324a6ef8e3SNikolaus Voss struct pwm_device *devm_fwnode_pwm_get(struct device *dev,
4334a6ef8e3SNikolaus Voss 				       struct fwnode_handle *fwnode,
4344a6ef8e3SNikolaus Voss 				       const char *con_id);
4350bcf168bSTushar Behera #else
4367170d3beSSean Young static inline bool pwm_might_sleep(struct pwm_device *pwm)
4377170d3beSSean Young {
4387170d3beSSean Young 	return true;
4397170d3beSSean Young }
4407170d3beSSean Young 
441c748a6d7SSean Young static inline int pwm_apply_might_sleep(struct pwm_device *pwm,
4425ec803edSBoris Brezillon 					const struct pwm_state *state)
4435ec803edSBoris Brezillon {
4444ad91a22SUwe Kleine-König 	might_sleep();
445dc518b37SSean Young 	return -EOPNOTSUPP;
4465ec803edSBoris Brezillon }
4475ec803edSBoris Brezillon 
4487170d3beSSean Young static inline int pwm_apply_atomic(struct pwm_device *pwm,
4497170d3beSSean Young 				   const struct pwm_state *state)
4507170d3beSSean Young {
4517170d3beSSean Young 	return -EOPNOTSUPP;
4527170d3beSSean Young }
4537170d3beSSean Young 
4545ec803edSBoris Brezillon static inline int pwm_adjust_config(struct pwm_device *pwm)
4555ec803edSBoris Brezillon {
456dc518b37SSean Young 	return -EOPNOTSUPP;
4575ec803edSBoris Brezillon }
4585ec803edSBoris Brezillon 
4595ec803edSBoris Brezillon static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
4605ec803edSBoris Brezillon 			     int period_ns)
4615ec803edSBoris Brezillon {
4624ad91a22SUwe Kleine-König 	might_sleep();
4635ec803edSBoris Brezillon 	return -EINVAL;
4645ec803edSBoris Brezillon }
4655ec803edSBoris Brezillon 
4665ec803edSBoris Brezillon static inline int pwm_enable(struct pwm_device *pwm)
4675ec803edSBoris Brezillon {
4684ad91a22SUwe Kleine-König 	might_sleep();
4695ec803edSBoris Brezillon 	return -EINVAL;
4705ec803edSBoris Brezillon }
4715ec803edSBoris Brezillon 
4725ec803edSBoris Brezillon static inline void pwm_disable(struct pwm_device *pwm)
4735ec803edSBoris Brezillon {
4744ad91a22SUwe Kleine-König 	might_sleep();
4755ec803edSBoris Brezillon }
4765ec803edSBoris Brezillon 
477b3c650adSGeert Uytterhoeven static inline int pwm_capture(struct pwm_device *pwm,
478b3c650adSGeert Uytterhoeven 			      struct pwm_capture *result,
479b3c650adSGeert Uytterhoeven 			      unsigned long timeout)
480b3c650adSGeert Uytterhoeven {
481b3c650adSGeert Uytterhoeven 	return -EINVAL;
482b3c650adSGeert Uytterhoeven }
483b3c650adSGeert Uytterhoeven 
484024913dbSUwe Kleine-König static inline void pwmchip_put(struct pwm_chip *chip)
485024913dbSUwe Kleine-König {
486024913dbSUwe Kleine-König }
487024913dbSUwe Kleine-König 
488024913dbSUwe Kleine-König static inline struct pwm_chip *pwmchip_alloc(struct device *parent,
489024913dbSUwe Kleine-König 					     unsigned int npwm,
490024913dbSUwe Kleine-König 					     size_t sizeof_priv)
491024913dbSUwe Kleine-König {
492024913dbSUwe Kleine-König 	return ERR_PTR(-EINVAL);
493024913dbSUwe Kleine-König }
494024913dbSUwe Kleine-König 
495024913dbSUwe Kleine-König static inline struct pwm_chip *devm_pwmchip_alloc(struct device *parent,
496024913dbSUwe Kleine-König 						  unsigned int npwm,
497024913dbSUwe Kleine-König 						  size_t sizeof_priv)
498024913dbSUwe Kleine-König {
499024913dbSUwe Kleine-König 	return pwmchip_alloc(parent, npwm, sizeof_priv);
500024913dbSUwe Kleine-König }
501024913dbSUwe Kleine-König 
5020bcf168bSTushar Behera static inline int pwmchip_add(struct pwm_chip *chip)
5030bcf168bSTushar Behera {
5040bcf168bSTushar Behera 	return -EINVAL;
5050bcf168bSTushar Behera }
5060bcf168bSTushar Behera 
5070bcf168bSTushar Behera static inline int pwmchip_remove(struct pwm_chip *chip)
5080bcf168bSTushar Behera {
5090bcf168bSTushar Behera 	return -EINVAL;
5100bcf168bSTushar Behera }
5110bcf168bSTushar Behera 
51288da4e81SAndy Shevchenko static inline int devm_pwmchip_add(struct device *dev, struct pwm_chip *chip)
51388da4e81SAndy Shevchenko {
51488da4e81SAndy Shevchenko 	return -EINVAL;
51588da4e81SAndy Shevchenko }
51688da4e81SAndy Shevchenko 
5170bcf168bSTushar Behera static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
5180bcf168bSTushar Behera 						       unsigned int index,
5190bcf168bSTushar Behera 						       const char *label)
5200bcf168bSTushar Behera {
52127d9a4d6SUwe Kleine-König 	might_sleep();
5220bcf168bSTushar Behera 	return ERR_PTR(-ENODEV);
5230bcf168bSTushar Behera }
5240bcf168bSTushar Behera 
5250bcf168bSTushar Behera static inline struct pwm_device *pwm_get(struct device *dev,
5260bcf168bSTushar Behera 					 const char *consumer)
5270bcf168bSTushar Behera {
52827d9a4d6SUwe Kleine-König 	might_sleep();
5290bcf168bSTushar Behera 	return ERR_PTR(-ENODEV);
5300bcf168bSTushar Behera }
5310bcf168bSTushar Behera 
5320bcf168bSTushar Behera static inline void pwm_put(struct pwm_device *pwm)
5330bcf168bSTushar Behera {
53427d9a4d6SUwe Kleine-König 	might_sleep();
5350bcf168bSTushar Behera }
5360bcf168bSTushar Behera 
5370bcf168bSTushar Behera static inline struct pwm_device *devm_pwm_get(struct device *dev,
5380bcf168bSTushar Behera 					      const char *consumer)
5390bcf168bSTushar Behera {
54027d9a4d6SUwe Kleine-König 	might_sleep();
5410bcf168bSTushar Behera 	return ERR_PTR(-ENODEV);
5420bcf168bSTushar Behera }
5430bcf168bSTushar Behera 
5444a6ef8e3SNikolaus Voss static inline struct pwm_device *
5454a6ef8e3SNikolaus Voss devm_fwnode_pwm_get(struct device *dev, struct fwnode_handle *fwnode,
5464a6ef8e3SNikolaus Voss 		    const char *con_id)
5474a6ef8e3SNikolaus Voss {
54827d9a4d6SUwe Kleine-König 	might_sleep();
5494a6ef8e3SNikolaus Voss 	return ERR_PTR(-ENODEV);
5504a6ef8e3SNikolaus Voss }
5510bcf168bSTushar Behera #endif
5526354316dSAlexandre Courbot 
5535ec803edSBoris Brezillon static inline void pwm_apply_args(struct pwm_device *pwm)
5545ec803edSBoris Brezillon {
55533cdcee0SBoris Brezillon 	struct pwm_state state = { };
55633cdcee0SBoris Brezillon 
5575ec803edSBoris Brezillon 	/*
5585ec803edSBoris Brezillon 	 * PWM users calling pwm_apply_args() expect to have a fresh config
5595ec803edSBoris Brezillon 	 * where the polarity and period are set according to pwm_args info.
5605ec803edSBoris Brezillon 	 * The problem is, polarity can only be changed when the PWM is
5615ec803edSBoris Brezillon 	 * disabled.
5625ec803edSBoris Brezillon 	 *
5635ec803edSBoris Brezillon 	 * PWM drivers supporting hardware readout may declare the PWM device
5645ec803edSBoris Brezillon 	 * as enabled, and prevent polarity setting, which changes from the
5655ec803edSBoris Brezillon 	 * existing behavior, where all PWM devices are declared as disabled
5665ec803edSBoris Brezillon 	 * at startup (even if they are actually enabled), thus authorizing
5675ec803edSBoris Brezillon 	 * polarity setting.
5685ec803edSBoris Brezillon 	 *
56933cdcee0SBoris Brezillon 	 * To fulfill this requirement, we apply a new state which disables
57033cdcee0SBoris Brezillon 	 * the PWM device and set the reference period and polarity config.
5715ec803edSBoris Brezillon 	 *
5725ec803edSBoris Brezillon 	 * Note that PWM users requiring a smooth handover between the
5735ec803edSBoris Brezillon 	 * bootloader and the kernel (like critical regulators controlled by
5745ec803edSBoris Brezillon 	 * PWM devices) will have to switch to the atomic API and avoid calling
5755ec803edSBoris Brezillon 	 * pwm_apply_args().
5765ec803edSBoris Brezillon 	 */
57733cdcee0SBoris Brezillon 
57833cdcee0SBoris Brezillon 	state.enabled = false;
57933cdcee0SBoris Brezillon 	state.polarity = pwm->args.polarity;
58033cdcee0SBoris Brezillon 	state.period = pwm->args.period;
5819e40ee18SClemens Gruber 	state.usage_power = false;
58233cdcee0SBoris Brezillon 
583c748a6d7SSean Young 	pwm_apply_might_sleep(pwm, &state);
5845ec803edSBoris Brezillon }
5855ec803edSBoris Brezillon 
586c2e64baaSThierry Reding /* only for backwards-compatibility, new code should not use this */
587c2e64baaSThierry Reding static inline int pwm_apply_state(struct pwm_device *pwm,
588c2e64baaSThierry Reding 				  const struct pwm_state *state)
589c2e64baaSThierry Reding {
590c2e64baaSThierry Reding 	return pwm_apply_might_sleep(pwm, state);
591c2e64baaSThierry Reding }
592c2e64baaSThierry Reding 
5938138d2ddSThierry Reding struct pwm_lookup {
5948138d2ddSThierry Reding 	struct list_head list;
5958138d2ddSThierry Reding 	const char *provider;
5968138d2ddSThierry Reding 	unsigned int index;
5978138d2ddSThierry Reding 	const char *dev_id;
5988138d2ddSThierry Reding 	const char *con_id;
5993796ce1dSAlexandre Belloni 	unsigned int period;
6003796ce1dSAlexandre Belloni 	enum pwm_polarity polarity;
601b526a314SHans de Goede 	const char *module; /* optional, may be NULL */
6028138d2ddSThierry Reding };
6038138d2ddSThierry Reding 
604b526a314SHans de Goede #define PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id,	\
605b526a314SHans de Goede 			       _period, _polarity, _module)		\
6068138d2ddSThierry Reding 	{								\
6078138d2ddSThierry Reding 		.provider = _provider,					\
6088138d2ddSThierry Reding 		.index = _index,					\
6098138d2ddSThierry Reding 		.dev_id = _dev_id,					\
6108138d2ddSThierry Reding 		.con_id = _con_id,					\
61142844029SAlexandre Belloni 		.period = _period,					\
612b526a314SHans de Goede 		.polarity = _polarity,					\
613b526a314SHans de Goede 		.module = _module,					\
6148138d2ddSThierry Reding 	}
6158138d2ddSThierry Reding 
616b526a314SHans de Goede #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
617b526a314SHans de Goede 	PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, _period, \
618b526a314SHans de Goede 			       _polarity, NULL)
619b526a314SHans de Goede 
6200bcf168bSTushar Behera #if IS_ENABLED(CONFIG_PWM)
6218138d2ddSThierry Reding void pwm_add_table(struct pwm_lookup *table, size_t num);
622efb0de55SShobhit Kumar void pwm_remove_table(struct pwm_lookup *table, size_t num);
6230bcf168bSTushar Behera #else
6240bcf168bSTushar Behera static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
6250bcf168bSTushar Behera {
6260bcf168bSTushar Behera }
627efb0de55SShobhit Kumar 
628efb0de55SShobhit Kumar static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
629efb0de55SShobhit Kumar {
630efb0de55SShobhit Kumar }
6310c2498f1SSascha Hauer #endif
6320c2498f1SSascha Hauer 
6335243ef8bSMark Vels #endif /* __LINUX_PWM_H */
634