xref: /linux-6.15/include/linux/pwm.h (revision 7170d3be)
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  * @pwm: global index of the PWM device
7304883802SThierry Reding  * @chip: PWM chip providing this PWM device
74e39c0df1SBoris Brezillon  * @args: PWM arguments
753ad1f3a3SUwe Kleine-König  * @state: last applied state
763ad1f3a3SUwe Kleine-König  * @last: last implemented state (for PWM_DEBUG)
7704883802SThierry Reding  */
78f051c466SThierry Reding struct pwm_device {
79f051c466SThierry Reding 	const char *label;
80f051c466SThierry Reding 	unsigned long flags;
81f051c466SThierry Reding 	unsigned int hwpwm;
82f051c466SThierry Reding 	struct pwm_chip *chip;
83f051c466SThierry Reding 
84e39c0df1SBoris Brezillon 	struct pwm_args args;
8543a276b0SBoris Brezillon 	struct pwm_state state;
863ad1f3a3SUwe Kleine-König 	struct pwm_state last;
87f051c466SThierry Reding };
88f051c466SThierry Reding 
8943a276b0SBoris Brezillon /**
9043a276b0SBoris Brezillon  * pwm_get_state() - retrieve the current PWM state
9143a276b0SBoris Brezillon  * @pwm: PWM device
9243a276b0SBoris Brezillon  * @state: state to fill with the current PWM state
931a7a6e80SUwe Kleine-König  *
941a7a6e80SUwe Kleine-König  * The returned PWM state represents the state that was applied by a previous call to
95c748a6d7SSean Young  * pwm_apply_might_sleep(). Drivers may have to slightly tweak that state before programming it to
96c748a6d7SSean Young  * hardware. If pwm_apply_might_sleep() was never called, this returns either the current hardware
971a7a6e80SUwe Kleine-König  * state (if supported) or the default settings.
9843a276b0SBoris Brezillon  */
9943a276b0SBoris Brezillon static inline void pwm_get_state(const struct pwm_device *pwm,
10043a276b0SBoris Brezillon 				 struct pwm_state *state)
10143a276b0SBoris Brezillon {
10243a276b0SBoris Brezillon 	*state = pwm->state;
10343a276b0SBoris Brezillon }
10443a276b0SBoris Brezillon 
1055c31252cSBoris Brezillon static inline bool pwm_is_enabled(const struct pwm_device *pwm)
1065c31252cSBoris Brezillon {
10709a7e4a3SBoris Brezillon 	struct pwm_state state;
10809a7e4a3SBoris Brezillon 
10909a7e4a3SBoris Brezillon 	pwm_get_state(pwm, &state);
11009a7e4a3SBoris Brezillon 
11109a7e4a3SBoris Brezillon 	return state.enabled;
1125c31252cSBoris Brezillon }
1135c31252cSBoris Brezillon 
114a9d887dcSGuru Das Srinagesh static inline void pwm_set_period(struct pwm_device *pwm, u64 period)
115f051c466SThierry Reding {
116f051c466SThierry Reding 	if (pwm)
11743a276b0SBoris Brezillon 		pwm->state.period = period;
118f051c466SThierry Reding }
119f051c466SThierry Reding 
120a9d887dcSGuru Das Srinagesh static inline u64 pwm_get_period(const struct pwm_device *pwm)
121f051c466SThierry Reding {
12243a276b0SBoris Brezillon 	struct pwm_state state;
12343a276b0SBoris Brezillon 
12443a276b0SBoris Brezillon 	pwm_get_state(pwm, &state);
12543a276b0SBoris Brezillon 
12643a276b0SBoris Brezillon 	return state.period;
127f051c466SThierry Reding }
128f051c466SThierry Reding 
12976abbddeSH Hartley Sweeten static inline void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty)
13076abbddeSH Hartley Sweeten {
13176abbddeSH Hartley Sweeten 	if (pwm)
13243a276b0SBoris Brezillon 		pwm->state.duty_cycle = duty;
13376abbddeSH Hartley Sweeten }
13476abbddeSH Hartley Sweeten 
135a9d887dcSGuru Das Srinagesh static inline u64 pwm_get_duty_cycle(const struct pwm_device *pwm)
13676abbddeSH Hartley Sweeten {
13743a276b0SBoris Brezillon 	struct pwm_state state;
13843a276b0SBoris Brezillon 
13943a276b0SBoris Brezillon 	pwm_get_state(pwm, &state);
14043a276b0SBoris Brezillon 
14143a276b0SBoris Brezillon 	return state.duty_cycle;
14276abbddeSH Hartley Sweeten }
14376abbddeSH Hartley Sweeten 
144011e7631SBoris Brezillon static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm)
145011e7631SBoris Brezillon {
14643a276b0SBoris Brezillon 	struct pwm_state state;
14743a276b0SBoris Brezillon 
14843a276b0SBoris Brezillon 	pwm_get_state(pwm, &state);
14943a276b0SBoris Brezillon 
15043a276b0SBoris Brezillon 	return state.polarity;
151011e7631SBoris Brezillon }
152011e7631SBoris Brezillon 
153e39c0df1SBoris Brezillon static inline void pwm_get_args(const struct pwm_device *pwm,
154e39c0df1SBoris Brezillon 				struct pwm_args *args)
155e39c0df1SBoris Brezillon {
156e39c0df1SBoris Brezillon 	*args = pwm->args;
157e39c0df1SBoris Brezillon }
158e39c0df1SBoris Brezillon 
1590c2498f1SSascha Hauer /**
160c748a6d7SSean Young  * pwm_init_state() - prepare a new state to be applied with pwm_apply_might_sleep()
161a6a0dbbcSBoris Brezillon  * @pwm: PWM device
162a6a0dbbcSBoris Brezillon  * @state: state to fill with the prepared PWM state
163a6a0dbbcSBoris Brezillon  *
164a6a0dbbcSBoris Brezillon  * This functions prepares a state that can later be tweaked and applied
165c748a6d7SSean Young  * to the PWM device with pwm_apply_might_sleep(). This is a convenient function
166a6a0dbbcSBoris Brezillon  * that first retrieves the current PWM state and the replaces the period
167a6a0dbbcSBoris Brezillon  * and polarity fields with the reference values defined in pwm->args.
168a6a0dbbcSBoris Brezillon  * Once the function returns, you can adjust the ->enabled and ->duty_cycle
169c748a6d7SSean Young  * fields according to your needs before calling pwm_apply_might_sleep().
170a6a0dbbcSBoris Brezillon  *
171a6a0dbbcSBoris Brezillon  * ->duty_cycle is initially set to zero to avoid cases where the current
172a6a0dbbcSBoris Brezillon  * ->duty_cycle value exceed the pwm_args->period one, which would trigger
173c748a6d7SSean Young  * an error if the user calls pwm_apply_might_sleep() without adjusting ->duty_cycle
174a6a0dbbcSBoris Brezillon  * first.
175a6a0dbbcSBoris Brezillon  */
176a6a0dbbcSBoris Brezillon static inline void pwm_init_state(const struct pwm_device *pwm,
177a6a0dbbcSBoris Brezillon 				  struct pwm_state *state)
178a6a0dbbcSBoris Brezillon {
179a6a0dbbcSBoris Brezillon 	struct pwm_args args;
180a6a0dbbcSBoris Brezillon 
181a6a0dbbcSBoris Brezillon 	/* First get the current state. */
182a6a0dbbcSBoris Brezillon 	pwm_get_state(pwm, state);
183a6a0dbbcSBoris Brezillon 
184a6a0dbbcSBoris Brezillon 	/* Then fill it with the reference config */
185a6a0dbbcSBoris Brezillon 	pwm_get_args(pwm, &args);
186a6a0dbbcSBoris Brezillon 
187a6a0dbbcSBoris Brezillon 	state->period = args.period;
188a6a0dbbcSBoris Brezillon 	state->polarity = args.polarity;
189a6a0dbbcSBoris Brezillon 	state->duty_cycle = 0;
1909e40ee18SClemens Gruber 	state->usage_power = false;
191a6a0dbbcSBoris Brezillon }
192a6a0dbbcSBoris Brezillon 
193a6a0dbbcSBoris Brezillon /**
194f6f3bddfSBoris Brezillon  * pwm_get_relative_duty_cycle() - Get a relative duty cycle value
195f6f3bddfSBoris Brezillon  * @state: PWM state to extract the duty cycle from
196f6f3bddfSBoris Brezillon  * @scale: target scale of the relative duty cycle
197f6f3bddfSBoris Brezillon  *
198f6f3bddfSBoris Brezillon  * This functions converts the absolute duty cycle stored in @state (expressed
199f6f3bddfSBoris Brezillon  * in nanosecond) into a value relative to the period.
200f6f3bddfSBoris Brezillon  *
201f6f3bddfSBoris Brezillon  * For example if you want to get the duty_cycle expressed in percent, call:
202f6f3bddfSBoris Brezillon  *
203f6f3bddfSBoris Brezillon  * pwm_get_state(pwm, &state);
204f6f3bddfSBoris Brezillon  * duty = pwm_get_relative_duty_cycle(&state, 100);
205f6f3bddfSBoris Brezillon  */
206f6f3bddfSBoris Brezillon static inline unsigned int
207f6f3bddfSBoris Brezillon pwm_get_relative_duty_cycle(const struct pwm_state *state, unsigned int scale)
208f6f3bddfSBoris Brezillon {
209f6f3bddfSBoris Brezillon 	if (!state->period)
210f6f3bddfSBoris Brezillon 		return 0;
211f6f3bddfSBoris Brezillon 
212f6f3bddfSBoris Brezillon 	return DIV_ROUND_CLOSEST_ULL((u64)state->duty_cycle * scale,
213f6f3bddfSBoris Brezillon 				     state->period);
214f6f3bddfSBoris Brezillon }
215f6f3bddfSBoris Brezillon 
216f6f3bddfSBoris Brezillon /**
217f6f3bddfSBoris Brezillon  * pwm_set_relative_duty_cycle() - Set a relative duty cycle value
218f6f3bddfSBoris Brezillon  * @state: PWM state to fill
219f6f3bddfSBoris Brezillon  * @duty_cycle: relative duty cycle value
220f6f3bddfSBoris Brezillon  * @scale: scale in which @duty_cycle is expressed
221f6f3bddfSBoris Brezillon  *
222f6f3bddfSBoris Brezillon  * This functions converts a relative into an absolute duty cycle (expressed
223f6f3bddfSBoris Brezillon  * in nanoseconds), and puts the result in state->duty_cycle.
224f6f3bddfSBoris Brezillon  *
225f6f3bddfSBoris Brezillon  * For example if you want to configure a 50% duty cycle, call:
226f6f3bddfSBoris Brezillon  *
227f6f3bddfSBoris Brezillon  * pwm_init_state(pwm, &state);
228f6f3bddfSBoris Brezillon  * pwm_set_relative_duty_cycle(&state, 50, 100);
229c748a6d7SSean Young  * pwm_apply_might_sleep(pwm, &state);
230f6f3bddfSBoris Brezillon  *
231f6f3bddfSBoris Brezillon  * This functions returns -EINVAL if @duty_cycle and/or @scale are
232f6f3bddfSBoris Brezillon  * inconsistent (@scale == 0 or @duty_cycle > @scale).
233f6f3bddfSBoris Brezillon  */
234f6f3bddfSBoris Brezillon static inline int
235f6f3bddfSBoris Brezillon pwm_set_relative_duty_cycle(struct pwm_state *state, unsigned int duty_cycle,
236f6f3bddfSBoris Brezillon 			    unsigned int scale)
237f6f3bddfSBoris Brezillon {
238f6f3bddfSBoris Brezillon 	if (!scale || duty_cycle > scale)
239f6f3bddfSBoris Brezillon 		return -EINVAL;
240f6f3bddfSBoris Brezillon 
241f6f3bddfSBoris Brezillon 	state->duty_cycle = DIV_ROUND_CLOSEST_ULL((u64)duty_cycle *
242f6f3bddfSBoris Brezillon 						  state->period,
243f6f3bddfSBoris Brezillon 						  scale);
244f6f3bddfSBoris Brezillon 
245f6f3bddfSBoris Brezillon 	return 0;
246f6f3bddfSBoris Brezillon }
247f6f3bddfSBoris Brezillon 
248f6f3bddfSBoris Brezillon /**
249ef2e35d9SUwe Kleine-König  * struct pwm_capture - PWM capture data
250ef2e35d9SUwe Kleine-König  * @period: period of the PWM signal (in nanoseconds)
251ef2e35d9SUwe Kleine-König  * @duty_cycle: duty cycle of the PWM signal (in nanoseconds)
252ef2e35d9SUwe Kleine-König  */
253ef2e35d9SUwe Kleine-König struct pwm_capture {
254ef2e35d9SUwe Kleine-König 	unsigned int period;
255ef2e35d9SUwe Kleine-König 	unsigned int duty_cycle;
256ef2e35d9SUwe Kleine-König };
257ef2e35d9SUwe Kleine-König 
258ef2e35d9SUwe Kleine-König /**
2590c2498f1SSascha Hauer  * struct pwm_ops - PWM controller operations
2600c2498f1SSascha Hauer  * @request: optional hook for requesting a PWM
2610c2498f1SSascha Hauer  * @free: optional hook for freeing a PWM
2623a3d1a4eSLee Jones  * @capture: capture and report PWM signal
26327938fd8SRasmus Villemoes  * @apply: atomically apply a new PWM config
26415fa8a43SBoris Brezillon  * @get_state: get the current PWM state. This function is only
26515fa8a43SBoris Brezillon  *	       called once per PWM device when the PWM chip is
26615fa8a43SBoris Brezillon  *	       registered.
2670c2498f1SSascha Hauer  */
2680c2498f1SSascha Hauer struct pwm_ops {
2696bc7064aSThierry Reding 	int (*request)(struct pwm_chip *chip, struct pwm_device *pwm);
2706bc7064aSThierry Reding 	void (*free)(struct pwm_chip *chip, struct pwm_device *pwm);
2713a3d1a4eSLee Jones 	int (*capture)(struct pwm_chip *chip, struct pwm_device *pwm,
2723a3d1a4eSLee Jones 		       struct pwm_capture *result, unsigned long timeout);
2735ec803edSBoris Brezillon 	int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm,
27471523d18SUwe Kleine-König 		     const struct pwm_state *state);
2756c452cffSUwe Kleine-König 	int (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,
27615fa8a43SBoris Brezillon 			 struct pwm_state *state);
2770c2498f1SSascha Hauer };
2780c2498f1SSascha Hauer 
2790c2498f1SSascha Hauer /**
280f051c466SThierry Reding  * struct pwm_chip - abstract a PWM controller
281f051c466SThierry Reding  * @dev: device providing the PWMs
282f051c466SThierry Reding  * @ops: callbacks for this PWM controller
283384461abSUwe Kleine-König  * @owner: module providing this chip
2842d91123aSUwe Kleine-König  * @id: unique number of this PWM chip
285f051c466SThierry Reding  * @npwm: number of PWMs controlled by this chip
28604883802SThierry Reding  * @of_xlate: request a PWM device given a device tree PWM specifier
28704883802SThierry Reding  * @of_pwm_n_cells: number of cells expected in the device tree PWM specifier
288*7170d3beSSean Young  * @atomic: can the driver's ->apply() be called in atomic context
2895d0a4c11SUwe Kleine-König  * @pwms: array of PWM devices allocated by the framework
2900c2498f1SSascha Hauer  */
2910c2498f1SSascha Hauer struct pwm_chip {
292f051c466SThierry Reding 	struct device *dev;
293f051c466SThierry Reding 	const struct pwm_ops *ops;
294384461abSUwe Kleine-König 	struct module *owner;
29554c86dd2SUwe Kleine-König 	unsigned int id;
296f051c466SThierry Reding 	unsigned int npwm;
297f051c466SThierry Reding 
298b4f78ff7SUwe Kleine-König 	struct pwm_device * (*of_xlate)(struct pwm_chip *chip,
2997299ab70SThierry Reding 					const struct of_phandle_args *args);
3007299ab70SThierry Reding 	unsigned int of_pwm_n_cells;
301*7170d3beSSean Young 	bool atomic;
3025d0a4c11SUwe Kleine-König 
3035d0a4c11SUwe Kleine-König 	/* only used internally by the PWM framework */
3045d0a4c11SUwe Kleine-König 	struct pwm_device *pwms;
3050c2498f1SSascha Hauer };
3060c2498f1SSascha Hauer 
3070bcf168bSTushar Behera #if IS_ENABLED(CONFIG_PWM)
3085ec803edSBoris Brezillon /* PWM user APIs */
309c748a6d7SSean Young int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state);
310*7170d3beSSean Young int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state);
3115ec803edSBoris Brezillon int pwm_adjust_config(struct pwm_device *pwm);
3125ec803edSBoris Brezillon 
3135ec803edSBoris Brezillon /**
3145ec803edSBoris Brezillon  * pwm_config() - change a PWM device configuration
3155ec803edSBoris Brezillon  * @pwm: PWM device
3165ec803edSBoris Brezillon  * @duty_ns: "on" time (in nanoseconds)
3175ec803edSBoris Brezillon  * @period_ns: duration (in nanoseconds) of one cycle
3185ec803edSBoris Brezillon  *
3195ec803edSBoris Brezillon  * Returns: 0 on success or a negative error code on failure.
3205ec803edSBoris Brezillon  */
3215ec803edSBoris Brezillon static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
3225ec803edSBoris Brezillon 			     int period_ns)
3235ec803edSBoris Brezillon {
3245ec803edSBoris Brezillon 	struct pwm_state state;
3255ec803edSBoris Brezillon 
3265ec803edSBoris Brezillon 	if (!pwm)
3275ec803edSBoris Brezillon 		return -EINVAL;
3285ec803edSBoris Brezillon 
329ef2bf499SBrian Norris 	if (duty_ns < 0 || period_ns < 0)
330ef2bf499SBrian Norris 		return -EINVAL;
331ef2bf499SBrian Norris 
3325ec803edSBoris Brezillon 	pwm_get_state(pwm, &state);
3335ec803edSBoris Brezillon 	if (state.duty_cycle == duty_ns && state.period == period_ns)
3345ec803edSBoris Brezillon 		return 0;
3355ec803edSBoris Brezillon 
3365ec803edSBoris Brezillon 	state.duty_cycle = duty_ns;
3375ec803edSBoris Brezillon 	state.period = period_ns;
338c748a6d7SSean Young 	return pwm_apply_might_sleep(pwm, &state);
3395ec803edSBoris Brezillon }
3405ec803edSBoris Brezillon 
3415ec803edSBoris Brezillon /**
3425ec803edSBoris Brezillon  * pwm_enable() - start a PWM output toggling
3435ec803edSBoris Brezillon  * @pwm: PWM device
3445ec803edSBoris Brezillon  *
3455ec803edSBoris Brezillon  * Returns: 0 on success or a negative error code on failure.
3465ec803edSBoris Brezillon  */
3475ec803edSBoris Brezillon static inline int pwm_enable(struct pwm_device *pwm)
3485ec803edSBoris Brezillon {
3495ec803edSBoris Brezillon 	struct pwm_state state;
3505ec803edSBoris Brezillon 
3515ec803edSBoris Brezillon 	if (!pwm)
3525ec803edSBoris Brezillon 		return -EINVAL;
3535ec803edSBoris Brezillon 
3545ec803edSBoris Brezillon 	pwm_get_state(pwm, &state);
3555ec803edSBoris Brezillon 	if (state.enabled)
3565ec803edSBoris Brezillon 		return 0;
3575ec803edSBoris Brezillon 
3585ec803edSBoris Brezillon 	state.enabled = true;
359c748a6d7SSean Young 	return pwm_apply_might_sleep(pwm, &state);
3605ec803edSBoris Brezillon }
3615ec803edSBoris Brezillon 
3625ec803edSBoris Brezillon /**
3635ec803edSBoris Brezillon  * pwm_disable() - stop a PWM output toggling
3645ec803edSBoris Brezillon  * @pwm: PWM device
3655ec803edSBoris Brezillon  */
3665ec803edSBoris Brezillon static inline void pwm_disable(struct pwm_device *pwm)
3675ec803edSBoris Brezillon {
3685ec803edSBoris Brezillon 	struct pwm_state state;
3695ec803edSBoris Brezillon 
3705ec803edSBoris Brezillon 	if (!pwm)
3715ec803edSBoris Brezillon 		return;
3725ec803edSBoris Brezillon 
3735ec803edSBoris Brezillon 	pwm_get_state(pwm, &state);
3745ec803edSBoris Brezillon 	if (!state.enabled)
3755ec803edSBoris Brezillon 		return;
3765ec803edSBoris Brezillon 
3775ec803edSBoris Brezillon 	state.enabled = false;
378c748a6d7SSean Young 	pwm_apply_might_sleep(pwm, &state);
3795ec803edSBoris Brezillon }
3805ec803edSBoris Brezillon 
381*7170d3beSSean Young /**
382*7170d3beSSean Young  * pwm_might_sleep() - is pwm_apply_atomic() supported?
383*7170d3beSSean Young  * @pwm: PWM device
384*7170d3beSSean Young  *
385*7170d3beSSean Young  * Returns: false if pwm_apply_atomic() can be called from atomic context.
386*7170d3beSSean Young  */
387*7170d3beSSean Young static inline bool pwm_might_sleep(struct pwm_device *pwm)
388*7170d3beSSean Young {
389*7170d3beSSean Young 	return !pwm->chip->atomic;
390*7170d3beSSean Young }
391*7170d3beSSean Young 
3925ec803edSBoris Brezillon /* PWM provider APIs */
3933a3d1a4eSLee Jones int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
3943a3d1a4eSLee Jones 		unsigned long timeout);
395f051c466SThierry Reding 
396384461abSUwe Kleine-König int __pwmchip_add(struct pwm_chip *chip, struct module *owner);
397384461abSUwe Kleine-König #define pwmchip_add(chip) __pwmchip_add(chip, THIS_MODULE)
3988083f58dSUwe Kleine-König void pwmchip_remove(struct pwm_chip *chip);
399bcda91bfSUwe Kleine-König 
400384461abSUwe Kleine-König int __devm_pwmchip_add(struct device *dev, struct pwm_chip *chip, struct module *owner);
401384461abSUwe Kleine-König #define devm_pwmchip_add(dev, chip) __devm_pwmchip_add(dev, chip, THIS_MODULE)
402bcda91bfSUwe Kleine-König 
403f051c466SThierry Reding struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
404f051c466SThierry Reding 					 unsigned int index,
405f051c466SThierry Reding 					 const char *label);
4068138d2ddSThierry Reding 
407b4f78ff7SUwe Kleine-König struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *chip,
40883af2402SPhilip, Avinash 		const struct of_phandle_args *args);
409b4f78ff7SUwe Kleine-König struct pwm_device *of_pwm_single_xlate(struct pwm_chip *chip,
4103ab7b6acSBjorn Andersson 				       const struct of_phandle_args *args);
41183af2402SPhilip, Avinash 
412d4c0c470SPeter Ujfalusi struct pwm_device *pwm_get(struct device *dev, const char *con_id);
4138138d2ddSThierry Reding void pwm_put(struct pwm_device *pwm);
4148138d2ddSThierry Reding 
415d4c0c470SPeter Ujfalusi struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
4164a6ef8e3SNikolaus Voss struct pwm_device *devm_fwnode_pwm_get(struct device *dev,
4174a6ef8e3SNikolaus Voss 				       struct fwnode_handle *fwnode,
4184a6ef8e3SNikolaus Voss 				       const char *con_id);
4190bcf168bSTushar Behera #else
420*7170d3beSSean Young static inline bool pwm_might_sleep(struct pwm_device *pwm)
421*7170d3beSSean Young {
422*7170d3beSSean Young 	return true;
423*7170d3beSSean Young }
424*7170d3beSSean Young 
425c748a6d7SSean Young static inline int pwm_apply_might_sleep(struct pwm_device *pwm,
4265ec803edSBoris Brezillon 					const struct pwm_state *state)
4275ec803edSBoris Brezillon {
4284ad91a22SUwe Kleine-König 	might_sleep();
429dc518b37SSean Young 	return -EOPNOTSUPP;
4305ec803edSBoris Brezillon }
4315ec803edSBoris Brezillon 
432*7170d3beSSean Young static inline int pwm_apply_atomic(struct pwm_device *pwm,
433*7170d3beSSean Young 				   const struct pwm_state *state)
434*7170d3beSSean Young {
435*7170d3beSSean Young 	return -EOPNOTSUPP;
436*7170d3beSSean Young }
437*7170d3beSSean Young 
4385ec803edSBoris Brezillon static inline int pwm_adjust_config(struct pwm_device *pwm)
4395ec803edSBoris Brezillon {
440dc518b37SSean Young 	return -EOPNOTSUPP;
4415ec803edSBoris Brezillon }
4425ec803edSBoris Brezillon 
4435ec803edSBoris Brezillon static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
4445ec803edSBoris Brezillon 			     int period_ns)
4455ec803edSBoris Brezillon {
4464ad91a22SUwe Kleine-König 	might_sleep();
4475ec803edSBoris Brezillon 	return -EINVAL;
4485ec803edSBoris Brezillon }
4495ec803edSBoris Brezillon 
4505ec803edSBoris Brezillon static inline int pwm_enable(struct pwm_device *pwm)
4515ec803edSBoris Brezillon {
4524ad91a22SUwe Kleine-König 	might_sleep();
4535ec803edSBoris Brezillon 	return -EINVAL;
4545ec803edSBoris Brezillon }
4555ec803edSBoris Brezillon 
4565ec803edSBoris Brezillon static inline void pwm_disable(struct pwm_device *pwm)
4575ec803edSBoris Brezillon {
4584ad91a22SUwe Kleine-König 	might_sleep();
4595ec803edSBoris Brezillon }
4605ec803edSBoris Brezillon 
461b3c650adSGeert Uytterhoeven static inline int pwm_capture(struct pwm_device *pwm,
462b3c650adSGeert Uytterhoeven 			      struct pwm_capture *result,
463b3c650adSGeert Uytterhoeven 			      unsigned long timeout)
464b3c650adSGeert Uytterhoeven {
465b3c650adSGeert Uytterhoeven 	return -EINVAL;
466b3c650adSGeert Uytterhoeven }
467b3c650adSGeert Uytterhoeven 
4680bcf168bSTushar Behera static inline int pwmchip_add(struct pwm_chip *chip)
4690bcf168bSTushar Behera {
4700bcf168bSTushar Behera 	return -EINVAL;
4710bcf168bSTushar Behera }
4720bcf168bSTushar Behera 
4730bcf168bSTushar Behera static inline int pwmchip_remove(struct pwm_chip *chip)
4740bcf168bSTushar Behera {
4750bcf168bSTushar Behera 	return -EINVAL;
4760bcf168bSTushar Behera }
4770bcf168bSTushar Behera 
47888da4e81SAndy Shevchenko static inline int devm_pwmchip_add(struct device *dev, struct pwm_chip *chip)
47988da4e81SAndy Shevchenko {
48088da4e81SAndy Shevchenko 	return -EINVAL;
48188da4e81SAndy Shevchenko }
48288da4e81SAndy Shevchenko 
4830bcf168bSTushar Behera static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
4840bcf168bSTushar Behera 						       unsigned int index,
4850bcf168bSTushar Behera 						       const char *label)
4860bcf168bSTushar Behera {
48727d9a4d6SUwe Kleine-König 	might_sleep();
4880bcf168bSTushar Behera 	return ERR_PTR(-ENODEV);
4890bcf168bSTushar Behera }
4900bcf168bSTushar Behera 
4910bcf168bSTushar Behera static inline struct pwm_device *pwm_get(struct device *dev,
4920bcf168bSTushar Behera 					 const char *consumer)
4930bcf168bSTushar Behera {
49427d9a4d6SUwe Kleine-König 	might_sleep();
4950bcf168bSTushar Behera 	return ERR_PTR(-ENODEV);
4960bcf168bSTushar Behera }
4970bcf168bSTushar Behera 
4980bcf168bSTushar Behera static inline void pwm_put(struct pwm_device *pwm)
4990bcf168bSTushar Behera {
50027d9a4d6SUwe Kleine-König 	might_sleep();
5010bcf168bSTushar Behera }
5020bcf168bSTushar Behera 
5030bcf168bSTushar Behera static inline struct pwm_device *devm_pwm_get(struct device *dev,
5040bcf168bSTushar Behera 					      const char *consumer)
5050bcf168bSTushar Behera {
50627d9a4d6SUwe Kleine-König 	might_sleep();
5070bcf168bSTushar Behera 	return ERR_PTR(-ENODEV);
5080bcf168bSTushar Behera }
5090bcf168bSTushar Behera 
5104a6ef8e3SNikolaus Voss static inline struct pwm_device *
5114a6ef8e3SNikolaus Voss devm_fwnode_pwm_get(struct device *dev, struct fwnode_handle *fwnode,
5124a6ef8e3SNikolaus Voss 		    const char *con_id)
5134a6ef8e3SNikolaus Voss {
51427d9a4d6SUwe Kleine-König 	might_sleep();
5154a6ef8e3SNikolaus Voss 	return ERR_PTR(-ENODEV);
5164a6ef8e3SNikolaus Voss }
5170bcf168bSTushar Behera #endif
5186354316dSAlexandre Courbot 
5195ec803edSBoris Brezillon static inline void pwm_apply_args(struct pwm_device *pwm)
5205ec803edSBoris Brezillon {
52133cdcee0SBoris Brezillon 	struct pwm_state state = { };
52233cdcee0SBoris Brezillon 
5235ec803edSBoris Brezillon 	/*
5245ec803edSBoris Brezillon 	 * PWM users calling pwm_apply_args() expect to have a fresh config
5255ec803edSBoris Brezillon 	 * where the polarity and period are set according to pwm_args info.
5265ec803edSBoris Brezillon 	 * The problem is, polarity can only be changed when the PWM is
5275ec803edSBoris Brezillon 	 * disabled.
5285ec803edSBoris Brezillon 	 *
5295ec803edSBoris Brezillon 	 * PWM drivers supporting hardware readout may declare the PWM device
5305ec803edSBoris Brezillon 	 * as enabled, and prevent polarity setting, which changes from the
5315ec803edSBoris Brezillon 	 * existing behavior, where all PWM devices are declared as disabled
5325ec803edSBoris Brezillon 	 * at startup (even if they are actually enabled), thus authorizing
5335ec803edSBoris Brezillon 	 * polarity setting.
5345ec803edSBoris Brezillon 	 *
53533cdcee0SBoris Brezillon 	 * To fulfill this requirement, we apply a new state which disables
53633cdcee0SBoris Brezillon 	 * the PWM device and set the reference period and polarity config.
5375ec803edSBoris Brezillon 	 *
5385ec803edSBoris Brezillon 	 * Note that PWM users requiring a smooth handover between the
5395ec803edSBoris Brezillon 	 * bootloader and the kernel (like critical regulators controlled by
5405ec803edSBoris Brezillon 	 * PWM devices) will have to switch to the atomic API and avoid calling
5415ec803edSBoris Brezillon 	 * pwm_apply_args().
5425ec803edSBoris Brezillon 	 */
54333cdcee0SBoris Brezillon 
54433cdcee0SBoris Brezillon 	state.enabled = false;
54533cdcee0SBoris Brezillon 	state.polarity = pwm->args.polarity;
54633cdcee0SBoris Brezillon 	state.period = pwm->args.period;
5479e40ee18SClemens Gruber 	state.usage_power = false;
54833cdcee0SBoris Brezillon 
549c748a6d7SSean Young 	pwm_apply_might_sleep(pwm, &state);
5505ec803edSBoris Brezillon }
5515ec803edSBoris Brezillon 
5528138d2ddSThierry Reding struct pwm_lookup {
5538138d2ddSThierry Reding 	struct list_head list;
5548138d2ddSThierry Reding 	const char *provider;
5558138d2ddSThierry Reding 	unsigned int index;
5568138d2ddSThierry Reding 	const char *dev_id;
5578138d2ddSThierry Reding 	const char *con_id;
5583796ce1dSAlexandre Belloni 	unsigned int period;
5593796ce1dSAlexandre Belloni 	enum pwm_polarity polarity;
560b526a314SHans de Goede 	const char *module; /* optional, may be NULL */
5618138d2ddSThierry Reding };
5628138d2ddSThierry Reding 
563b526a314SHans de Goede #define PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id,	\
564b526a314SHans de Goede 			       _period, _polarity, _module)		\
5658138d2ddSThierry Reding 	{								\
5668138d2ddSThierry Reding 		.provider = _provider,					\
5678138d2ddSThierry Reding 		.index = _index,					\
5688138d2ddSThierry Reding 		.dev_id = _dev_id,					\
5698138d2ddSThierry Reding 		.con_id = _con_id,					\
57042844029SAlexandre Belloni 		.period = _period,					\
571b526a314SHans de Goede 		.polarity = _polarity,					\
572b526a314SHans de Goede 		.module = _module,					\
5738138d2ddSThierry Reding 	}
5748138d2ddSThierry Reding 
575b526a314SHans de Goede #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
576b526a314SHans de Goede 	PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, _period, \
577b526a314SHans de Goede 			       _polarity, NULL)
578b526a314SHans de Goede 
5790bcf168bSTushar Behera #if IS_ENABLED(CONFIG_PWM)
5808138d2ddSThierry Reding void pwm_add_table(struct pwm_lookup *table, size_t num);
581efb0de55SShobhit Kumar void pwm_remove_table(struct pwm_lookup *table, size_t num);
5820bcf168bSTushar Behera #else
5830bcf168bSTushar Behera static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
5840bcf168bSTushar Behera {
5850bcf168bSTushar Behera }
586efb0de55SShobhit Kumar 
587efb0de55SShobhit Kumar static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
588efb0de55SShobhit Kumar {
589efb0de55SShobhit Kumar }
5900c2498f1SSascha Hauer #endif
5910c2498f1SSascha Hauer 
59276abbddeSH Hartley Sweeten #ifdef CONFIG_PWM_SYSFS
59376abbddeSH Hartley Sweeten void pwmchip_sysfs_export(struct pwm_chip *chip);
59476abbddeSH Hartley Sweeten void pwmchip_sysfs_unexport(struct pwm_chip *chip);
59576abbddeSH Hartley Sweeten #else
59676abbddeSH Hartley Sweeten static inline void pwmchip_sysfs_export(struct pwm_chip *chip)
59776abbddeSH Hartley Sweeten {
59876abbddeSH Hartley Sweeten }
59976abbddeSH Hartley Sweeten 
60076abbddeSH Hartley Sweeten static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip)
60176abbddeSH Hartley Sweeten {
60276abbddeSH Hartley Sweeten }
60376abbddeSH Hartley Sweeten #endif /* CONFIG_PWM_SYSFS */
60476abbddeSH Hartley Sweeten 
6055243ef8bSMark Vels #endif /* __LINUX_PWM_H */
606