xref: /linux-6.15/include/linux/pwm.h (revision 3ad1f3a3)
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 
93a3d1a4eSLee Jones struct pwm_capture;
1062099abfSThierry Reding struct seq_file;
113a3d1a4eSLee Jones 
120c2498f1SSascha Hauer struct pwm_chip;
130c2498f1SSascha Hauer 
140aa0869cSPhilip, Avinash /**
150aa0869cSPhilip, Avinash  * enum pwm_polarity - polarity of a PWM signal
160aa0869cSPhilip, Avinash  * @PWM_POLARITY_NORMAL: a high signal for the duration of the duty-
170aa0869cSPhilip, Avinash  * cycle, followed by a low signal for the remainder of the pulse
180aa0869cSPhilip, Avinash  * period
190aa0869cSPhilip, Avinash  * @PWM_POLARITY_INVERSED: a low signal for the duration of the duty-
200aa0869cSPhilip, Avinash  * cycle, followed by a high signal for the remainder of the pulse
210aa0869cSPhilip, Avinash  * period
220aa0869cSPhilip, Avinash  */
230aa0869cSPhilip, Avinash enum pwm_polarity {
240aa0869cSPhilip, Avinash 	PWM_POLARITY_NORMAL,
250aa0869cSPhilip, Avinash 	PWM_POLARITY_INVERSED,
260aa0869cSPhilip, Avinash };
270aa0869cSPhilip, Avinash 
28e39c0df1SBoris Brezillon /**
29e39c0df1SBoris Brezillon  * struct pwm_args - board-dependent PWM arguments
30e39c0df1SBoris Brezillon  * @period: reference period
31e39c0df1SBoris Brezillon  * @polarity: reference polarity
32e39c0df1SBoris Brezillon  *
33e39c0df1SBoris Brezillon  * This structure describes board-dependent arguments attached to a PWM
34e39c0df1SBoris Brezillon  * device. These arguments are usually retrieved from the PWM lookup table or
35e39c0df1SBoris Brezillon  * device tree.
36e39c0df1SBoris Brezillon  *
37e39c0df1SBoris Brezillon  * Do not confuse this with the PWM state: PWM arguments represent the initial
38e39c0df1SBoris Brezillon  * configuration that users want to use on this PWM device rather than the
39e39c0df1SBoris Brezillon  * current PWM hardware state.
40e39c0df1SBoris Brezillon  */
41e39c0df1SBoris Brezillon struct pwm_args {
42e39c0df1SBoris Brezillon 	unsigned int period;
43e39c0df1SBoris Brezillon 	enum pwm_polarity polarity;
44e39c0df1SBoris Brezillon };
45e39c0df1SBoris Brezillon 
46f051c466SThierry Reding enum {
47f051c466SThierry Reding 	PWMF_REQUESTED = 1 << 0,
4809a7e4a3SBoris Brezillon 	PWMF_EXPORTED = 1 << 1,
49f051c466SThierry Reding };
50f051c466SThierry Reding 
5143a276b0SBoris Brezillon /*
5243a276b0SBoris Brezillon  * struct pwm_state - state of a PWM channel
5343a276b0SBoris Brezillon  * @period: PWM period (in nanoseconds)
5443a276b0SBoris Brezillon  * @duty_cycle: PWM duty cycle (in nanoseconds)
5543a276b0SBoris Brezillon  * @polarity: PWM polarity
5609a7e4a3SBoris Brezillon  * @enabled: PWM enabled status
5743a276b0SBoris Brezillon  */
5843a276b0SBoris Brezillon struct pwm_state {
5943a276b0SBoris Brezillon 	unsigned int period;
6043a276b0SBoris Brezillon 	unsigned int duty_cycle;
6143a276b0SBoris Brezillon 	enum pwm_polarity polarity;
6209a7e4a3SBoris Brezillon 	bool enabled;
6343a276b0SBoris Brezillon };
6443a276b0SBoris Brezillon 
6504883802SThierry Reding /**
6604883802SThierry Reding  * struct pwm_device - PWM channel object
6704883802SThierry Reding  * @label: name of the PWM device
6804883802SThierry Reding  * @flags: flags associated with the PWM device
6904883802SThierry Reding  * @hwpwm: per-chip relative index of the PWM device
7004883802SThierry Reding  * @pwm: global index of the PWM device
7104883802SThierry Reding  * @chip: PWM chip providing this PWM device
7204883802SThierry Reding  * @chip_data: chip-private data associated with the PWM device
73e39c0df1SBoris Brezillon  * @args: PWM arguments
74*3ad1f3a3SUwe Kleine-König  * @state: last applied state
75*3ad1f3a3SUwe 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 	unsigned int pwm;
82f051c466SThierry Reding 	struct pwm_chip *chip;
83f051c466SThierry Reding 	void *chip_data;
84f051c466SThierry Reding 
85e39c0df1SBoris Brezillon 	struct pwm_args args;
8643a276b0SBoris Brezillon 	struct pwm_state state;
87*3ad1f3a3SUwe Kleine-König 	struct pwm_state last;
88f051c466SThierry Reding };
89f051c466SThierry Reding 
9043a276b0SBoris Brezillon /**
9143a276b0SBoris Brezillon  * pwm_get_state() - retrieve the current PWM state
9243a276b0SBoris Brezillon  * @pwm: PWM device
9343a276b0SBoris Brezillon  * @state: state to fill with the current PWM state
9443a276b0SBoris Brezillon  */
9543a276b0SBoris Brezillon static inline void pwm_get_state(const struct pwm_device *pwm,
9643a276b0SBoris Brezillon 				 struct pwm_state *state)
9743a276b0SBoris Brezillon {
9843a276b0SBoris Brezillon 	*state = pwm->state;
9943a276b0SBoris Brezillon }
10043a276b0SBoris Brezillon 
1015c31252cSBoris Brezillon static inline bool pwm_is_enabled(const struct pwm_device *pwm)
1025c31252cSBoris Brezillon {
10309a7e4a3SBoris Brezillon 	struct pwm_state state;
10409a7e4a3SBoris Brezillon 
10509a7e4a3SBoris Brezillon 	pwm_get_state(pwm, &state);
10609a7e4a3SBoris Brezillon 
10709a7e4a3SBoris Brezillon 	return state.enabled;
1085c31252cSBoris Brezillon }
1095c31252cSBoris Brezillon 
110f051c466SThierry Reding static inline void pwm_set_period(struct pwm_device *pwm, unsigned int period)
111f051c466SThierry Reding {
112f051c466SThierry Reding 	if (pwm)
11343a276b0SBoris Brezillon 		pwm->state.period = period;
114f051c466SThierry Reding }
115f051c466SThierry Reding 
116a1cf4217SBoris Brezillon static inline unsigned int pwm_get_period(const struct pwm_device *pwm)
117f051c466SThierry Reding {
11843a276b0SBoris Brezillon 	struct pwm_state state;
11943a276b0SBoris Brezillon 
12043a276b0SBoris Brezillon 	pwm_get_state(pwm, &state);
12143a276b0SBoris Brezillon 
12243a276b0SBoris Brezillon 	return state.period;
123f051c466SThierry Reding }
124f051c466SThierry Reding 
12576abbddeSH Hartley Sweeten static inline void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty)
12676abbddeSH Hartley Sweeten {
12776abbddeSH Hartley Sweeten 	if (pwm)
12843a276b0SBoris Brezillon 		pwm->state.duty_cycle = duty;
12976abbddeSH Hartley Sweeten }
13076abbddeSH Hartley Sweeten 
131a1cf4217SBoris Brezillon static inline unsigned int pwm_get_duty_cycle(const struct pwm_device *pwm)
13276abbddeSH Hartley Sweeten {
13343a276b0SBoris Brezillon 	struct pwm_state state;
13443a276b0SBoris Brezillon 
13543a276b0SBoris Brezillon 	pwm_get_state(pwm, &state);
13643a276b0SBoris Brezillon 
13743a276b0SBoris Brezillon 	return state.duty_cycle;
13876abbddeSH Hartley Sweeten }
13976abbddeSH Hartley Sweeten 
140011e7631SBoris Brezillon static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm)
141011e7631SBoris Brezillon {
14243a276b0SBoris Brezillon 	struct pwm_state state;
14343a276b0SBoris Brezillon 
14443a276b0SBoris Brezillon 	pwm_get_state(pwm, &state);
14543a276b0SBoris Brezillon 
14643a276b0SBoris Brezillon 	return state.polarity;
147011e7631SBoris Brezillon }
148011e7631SBoris Brezillon 
149e39c0df1SBoris Brezillon static inline void pwm_get_args(const struct pwm_device *pwm,
150e39c0df1SBoris Brezillon 				struct pwm_args *args)
151e39c0df1SBoris Brezillon {
152e39c0df1SBoris Brezillon 	*args = pwm->args;
153e39c0df1SBoris Brezillon }
154e39c0df1SBoris Brezillon 
1550c2498f1SSascha Hauer /**
156a6a0dbbcSBoris Brezillon  * pwm_init_state() - prepare a new state to be applied with pwm_apply_state()
157a6a0dbbcSBoris Brezillon  * @pwm: PWM device
158a6a0dbbcSBoris Brezillon  * @state: state to fill with the prepared PWM state
159a6a0dbbcSBoris Brezillon  *
160a6a0dbbcSBoris Brezillon  * This functions prepares a state that can later be tweaked and applied
161a6a0dbbcSBoris Brezillon  * to the PWM device with pwm_apply_state(). This is a convenient function
162a6a0dbbcSBoris Brezillon  * that first retrieves the current PWM state and the replaces the period
163a6a0dbbcSBoris Brezillon  * and polarity fields with the reference values defined in pwm->args.
164a6a0dbbcSBoris Brezillon  * Once the function returns, you can adjust the ->enabled and ->duty_cycle
165a6a0dbbcSBoris Brezillon  * fields according to your needs before calling pwm_apply_state().
166a6a0dbbcSBoris Brezillon  *
167a6a0dbbcSBoris Brezillon  * ->duty_cycle is initially set to zero to avoid cases where the current
168a6a0dbbcSBoris Brezillon  * ->duty_cycle value exceed the pwm_args->period one, which would trigger
169a6a0dbbcSBoris Brezillon  * an error if the user calls pwm_apply_state() without adjusting ->duty_cycle
170a6a0dbbcSBoris Brezillon  * first.
171a6a0dbbcSBoris Brezillon  */
172a6a0dbbcSBoris Brezillon static inline void pwm_init_state(const struct pwm_device *pwm,
173a6a0dbbcSBoris Brezillon 				  struct pwm_state *state)
174a6a0dbbcSBoris Brezillon {
175a6a0dbbcSBoris Brezillon 	struct pwm_args args;
176a6a0dbbcSBoris Brezillon 
177a6a0dbbcSBoris Brezillon 	/* First get the current state. */
178a6a0dbbcSBoris Brezillon 	pwm_get_state(pwm, state);
179a6a0dbbcSBoris Brezillon 
180a6a0dbbcSBoris Brezillon 	/* Then fill it with the reference config */
181a6a0dbbcSBoris Brezillon 	pwm_get_args(pwm, &args);
182a6a0dbbcSBoris Brezillon 
183a6a0dbbcSBoris Brezillon 	state->period = args.period;
184a6a0dbbcSBoris Brezillon 	state->polarity = args.polarity;
185a6a0dbbcSBoris Brezillon 	state->duty_cycle = 0;
186a6a0dbbcSBoris Brezillon }
187a6a0dbbcSBoris Brezillon 
188a6a0dbbcSBoris Brezillon /**
189f6f3bddfSBoris Brezillon  * pwm_get_relative_duty_cycle() - Get a relative duty cycle value
190f6f3bddfSBoris Brezillon  * @state: PWM state to extract the duty cycle from
191f6f3bddfSBoris Brezillon  * @scale: target scale of the relative duty cycle
192f6f3bddfSBoris Brezillon  *
193f6f3bddfSBoris Brezillon  * This functions converts the absolute duty cycle stored in @state (expressed
194f6f3bddfSBoris Brezillon  * in nanosecond) into a value relative to the period.
195f6f3bddfSBoris Brezillon  *
196f6f3bddfSBoris Brezillon  * For example if you want to get the duty_cycle expressed in percent, call:
197f6f3bddfSBoris Brezillon  *
198f6f3bddfSBoris Brezillon  * pwm_get_state(pwm, &state);
199f6f3bddfSBoris Brezillon  * duty = pwm_get_relative_duty_cycle(&state, 100);
200f6f3bddfSBoris Brezillon  */
201f6f3bddfSBoris Brezillon static inline unsigned int
202f6f3bddfSBoris Brezillon pwm_get_relative_duty_cycle(const struct pwm_state *state, unsigned int scale)
203f6f3bddfSBoris Brezillon {
204f6f3bddfSBoris Brezillon 	if (!state->period)
205f6f3bddfSBoris Brezillon 		return 0;
206f6f3bddfSBoris Brezillon 
207f6f3bddfSBoris Brezillon 	return DIV_ROUND_CLOSEST_ULL((u64)state->duty_cycle * scale,
208f6f3bddfSBoris Brezillon 				     state->period);
209f6f3bddfSBoris Brezillon }
210f6f3bddfSBoris Brezillon 
211f6f3bddfSBoris Brezillon /**
212f6f3bddfSBoris Brezillon  * pwm_set_relative_duty_cycle() - Set a relative duty cycle value
213f6f3bddfSBoris Brezillon  * @state: PWM state to fill
214f6f3bddfSBoris Brezillon  * @duty_cycle: relative duty cycle value
215f6f3bddfSBoris Brezillon  * @scale: scale in which @duty_cycle is expressed
216f6f3bddfSBoris Brezillon  *
217f6f3bddfSBoris Brezillon  * This functions converts a relative into an absolute duty cycle (expressed
218f6f3bddfSBoris Brezillon  * in nanoseconds), and puts the result in state->duty_cycle.
219f6f3bddfSBoris Brezillon  *
220f6f3bddfSBoris Brezillon  * For example if you want to configure a 50% duty cycle, call:
221f6f3bddfSBoris Brezillon  *
222f6f3bddfSBoris Brezillon  * pwm_init_state(pwm, &state);
223f6f3bddfSBoris Brezillon  * pwm_set_relative_duty_cycle(&state, 50, 100);
224f6f3bddfSBoris Brezillon  * pwm_apply_state(pwm, &state);
225f6f3bddfSBoris Brezillon  *
226f6f3bddfSBoris Brezillon  * This functions returns -EINVAL if @duty_cycle and/or @scale are
227f6f3bddfSBoris Brezillon  * inconsistent (@scale == 0 or @duty_cycle > @scale).
228f6f3bddfSBoris Brezillon  */
229f6f3bddfSBoris Brezillon static inline int
230f6f3bddfSBoris Brezillon pwm_set_relative_duty_cycle(struct pwm_state *state, unsigned int duty_cycle,
231f6f3bddfSBoris Brezillon 			    unsigned int scale)
232f6f3bddfSBoris Brezillon {
233f6f3bddfSBoris Brezillon 	if (!scale || duty_cycle > scale)
234f6f3bddfSBoris Brezillon 		return -EINVAL;
235f6f3bddfSBoris Brezillon 
236f6f3bddfSBoris Brezillon 	state->duty_cycle = DIV_ROUND_CLOSEST_ULL((u64)duty_cycle *
237f6f3bddfSBoris Brezillon 						  state->period,
238f6f3bddfSBoris Brezillon 						  scale);
239f6f3bddfSBoris Brezillon 
240f6f3bddfSBoris Brezillon 	return 0;
241f6f3bddfSBoris Brezillon }
242f6f3bddfSBoris Brezillon 
243f6f3bddfSBoris Brezillon /**
2440c2498f1SSascha Hauer  * struct pwm_ops - PWM controller operations
2450c2498f1SSascha Hauer  * @request: optional hook for requesting a PWM
2460c2498f1SSascha Hauer  * @free: optional hook for freeing a PWM
2473a3d1a4eSLee Jones  * @capture: capture and report PWM signal
24827938fd8SRasmus Villemoes  * @apply: atomically apply a new PWM config
24915fa8a43SBoris Brezillon  * @get_state: get the current PWM state. This function is only
25015fa8a43SBoris Brezillon  *	       called once per PWM device when the PWM chip is
25115fa8a43SBoris Brezillon  *	       registered.
2520c2498f1SSascha Hauer  * @owner: helps prevent removal of modules exporting active PWMs
2535d0a4c11SUwe Kleine-König  * @config: configure duty cycles and period length for this PWM
2545d0a4c11SUwe Kleine-König  * @set_polarity: configure the polarity of this PWM
2555d0a4c11SUwe Kleine-König  * @enable: enable PWM output toggling
2565d0a4c11SUwe Kleine-König  * @disable: disable PWM output toggling
2570c2498f1SSascha Hauer  */
2580c2498f1SSascha Hauer struct pwm_ops {
2596bc7064aSThierry Reding 	int (*request)(struct pwm_chip *chip, struct pwm_device *pwm);
2606bc7064aSThierry Reding 	void (*free)(struct pwm_chip *chip, struct pwm_device *pwm);
2613a3d1a4eSLee Jones 	int (*capture)(struct pwm_chip *chip, struct pwm_device *pwm,
2623a3d1a4eSLee Jones 		       struct pwm_capture *result, unsigned long timeout);
2635ec803edSBoris Brezillon 	int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm,
26471523d18SUwe Kleine-König 		     const struct pwm_state *state);
26515fa8a43SBoris Brezillon 	void (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,
26615fa8a43SBoris Brezillon 			  struct pwm_state *state);
2670c2498f1SSascha Hauer 	struct module *owner;
2685d0a4c11SUwe Kleine-König 
2695d0a4c11SUwe Kleine-König 	/* Only used by legacy drivers */
2705d0a4c11SUwe Kleine-König 	int (*config)(struct pwm_chip *chip, struct pwm_device *pwm,
2715d0a4c11SUwe Kleine-König 		      int duty_ns, int period_ns);
2725d0a4c11SUwe Kleine-König 	int (*set_polarity)(struct pwm_chip *chip, struct pwm_device *pwm,
2735d0a4c11SUwe Kleine-König 			    enum pwm_polarity polarity);
2745d0a4c11SUwe Kleine-König 	int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm);
2755d0a4c11SUwe Kleine-König 	void (*disable)(struct pwm_chip *chip, struct pwm_device *pwm);
2760c2498f1SSascha Hauer };
2770c2498f1SSascha Hauer 
2780c2498f1SSascha Hauer /**
279f051c466SThierry Reding  * struct pwm_chip - abstract a PWM controller
280f051c466SThierry Reding  * @dev: device providing the PWMs
281f051c466SThierry Reding  * @ops: callbacks for this PWM controller
282f051c466SThierry Reding  * @base: number of first PWM controlled by this chip
283f051c466SThierry Reding  * @npwm: number of PWMs controlled by this chip
28404883802SThierry Reding  * @of_xlate: request a PWM device given a device tree PWM specifier
28504883802SThierry Reding  * @of_pwm_n_cells: number of cells expected in the device tree PWM specifier
2865d0a4c11SUwe Kleine-König  * @list: list node for internal use
2875d0a4c11SUwe Kleine-König  * @pwms: array of PWM devices allocated by the framework
2880c2498f1SSascha Hauer  */
2890c2498f1SSascha Hauer struct pwm_chip {
290f051c466SThierry Reding 	struct device *dev;
291f051c466SThierry Reding 	const struct pwm_ops *ops;
292f051c466SThierry Reding 	int base;
293f051c466SThierry Reding 	unsigned int npwm;
294f051c466SThierry Reding 
2957299ab70SThierry Reding 	struct pwm_device * (*of_xlate)(struct pwm_chip *pc,
2967299ab70SThierry Reding 					const struct of_phandle_args *args);
2977299ab70SThierry Reding 	unsigned int of_pwm_n_cells;
2985d0a4c11SUwe Kleine-König 
2995d0a4c11SUwe Kleine-König 	/* only used internally by the PWM framework */
3005d0a4c11SUwe Kleine-König 	struct list_head list;
3015d0a4c11SUwe Kleine-König 	struct pwm_device *pwms;
3020c2498f1SSascha Hauer };
3030c2498f1SSascha Hauer 
3043a3d1a4eSLee Jones /**
3053a3d1a4eSLee Jones  * struct pwm_capture - PWM capture data
3063a3d1a4eSLee Jones  * @period: period of the PWM signal (in nanoseconds)
3073a3d1a4eSLee Jones  * @duty_cycle: duty cycle of the PWM signal (in nanoseconds)
3083a3d1a4eSLee Jones  */
3093a3d1a4eSLee Jones struct pwm_capture {
3103a3d1a4eSLee Jones 	unsigned int period;
3113a3d1a4eSLee Jones 	unsigned int duty_cycle;
3123a3d1a4eSLee Jones };
3133a3d1a4eSLee Jones 
3140bcf168bSTushar Behera #if IS_ENABLED(CONFIG_PWM)
3155ec803edSBoris Brezillon /* PWM user APIs */
3165ec803edSBoris Brezillon struct pwm_device *pwm_request(int pwm_id, const char *label);
3175ec803edSBoris Brezillon void pwm_free(struct pwm_device *pwm);
31871523d18SUwe Kleine-König int pwm_apply_state(struct pwm_device *pwm, const struct pwm_state *state);
3195ec803edSBoris Brezillon int pwm_adjust_config(struct pwm_device *pwm);
3205ec803edSBoris Brezillon 
3215ec803edSBoris Brezillon /**
3225ec803edSBoris Brezillon  * pwm_config() - change a PWM device configuration
3235ec803edSBoris Brezillon  * @pwm: PWM device
3245ec803edSBoris Brezillon  * @duty_ns: "on" time (in nanoseconds)
3255ec803edSBoris Brezillon  * @period_ns: duration (in nanoseconds) of one cycle
3265ec803edSBoris Brezillon  *
3275ec803edSBoris Brezillon  * Returns: 0 on success or a negative error code on failure.
3285ec803edSBoris Brezillon  */
3295ec803edSBoris Brezillon static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
3305ec803edSBoris Brezillon 			     int period_ns)
3315ec803edSBoris Brezillon {
3325ec803edSBoris Brezillon 	struct pwm_state state;
3335ec803edSBoris Brezillon 
3345ec803edSBoris Brezillon 	if (!pwm)
3355ec803edSBoris Brezillon 		return -EINVAL;
3365ec803edSBoris Brezillon 
337ef2bf499SBrian Norris 	if (duty_ns < 0 || period_ns < 0)
338ef2bf499SBrian Norris 		return -EINVAL;
339ef2bf499SBrian Norris 
3405ec803edSBoris Brezillon 	pwm_get_state(pwm, &state);
3415ec803edSBoris Brezillon 	if (state.duty_cycle == duty_ns && state.period == period_ns)
3425ec803edSBoris Brezillon 		return 0;
3435ec803edSBoris Brezillon 
3445ec803edSBoris Brezillon 	state.duty_cycle = duty_ns;
3455ec803edSBoris Brezillon 	state.period = period_ns;
3465ec803edSBoris Brezillon 	return pwm_apply_state(pwm, &state);
3475ec803edSBoris Brezillon }
3485ec803edSBoris Brezillon 
3495ec803edSBoris Brezillon /**
3505ec803edSBoris Brezillon  * pwm_enable() - start a PWM output toggling
3515ec803edSBoris Brezillon  * @pwm: PWM device
3525ec803edSBoris Brezillon  *
3535ec803edSBoris Brezillon  * Returns: 0 on success or a negative error code on failure.
3545ec803edSBoris Brezillon  */
3555ec803edSBoris Brezillon static inline int pwm_enable(struct pwm_device *pwm)
3565ec803edSBoris Brezillon {
3575ec803edSBoris Brezillon 	struct pwm_state state;
3585ec803edSBoris Brezillon 
3595ec803edSBoris Brezillon 	if (!pwm)
3605ec803edSBoris Brezillon 		return -EINVAL;
3615ec803edSBoris Brezillon 
3625ec803edSBoris Brezillon 	pwm_get_state(pwm, &state);
3635ec803edSBoris Brezillon 	if (state.enabled)
3645ec803edSBoris Brezillon 		return 0;
3655ec803edSBoris Brezillon 
3665ec803edSBoris Brezillon 	state.enabled = true;
3675ec803edSBoris Brezillon 	return pwm_apply_state(pwm, &state);
3685ec803edSBoris Brezillon }
3695ec803edSBoris Brezillon 
3705ec803edSBoris Brezillon /**
3715ec803edSBoris Brezillon  * pwm_disable() - stop a PWM output toggling
3725ec803edSBoris Brezillon  * @pwm: PWM device
3735ec803edSBoris Brezillon  */
3745ec803edSBoris Brezillon static inline void pwm_disable(struct pwm_device *pwm)
3755ec803edSBoris Brezillon {
3765ec803edSBoris Brezillon 	struct pwm_state state;
3775ec803edSBoris Brezillon 
3785ec803edSBoris Brezillon 	if (!pwm)
3795ec803edSBoris Brezillon 		return;
3805ec803edSBoris Brezillon 
3815ec803edSBoris Brezillon 	pwm_get_state(pwm, &state);
3825ec803edSBoris Brezillon 	if (!state.enabled)
3835ec803edSBoris Brezillon 		return;
3845ec803edSBoris Brezillon 
3855ec803edSBoris Brezillon 	state.enabled = false;
3865ec803edSBoris Brezillon 	pwm_apply_state(pwm, &state);
3875ec803edSBoris Brezillon }
3885ec803edSBoris Brezillon 
3895ec803edSBoris Brezillon /* PWM provider APIs */
3903a3d1a4eSLee Jones int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
3913a3d1a4eSLee Jones 		unsigned long timeout);
392f051c466SThierry Reding int pwm_set_chip_data(struct pwm_device *pwm, void *data);
393f051c466SThierry Reding void *pwm_get_chip_data(struct pwm_device *pwm);
394f051c466SThierry Reding 
395b6a00faeSTim Kryger int pwmchip_add_with_polarity(struct pwm_chip *chip,
396b6a00faeSTim Kryger 			      enum pwm_polarity polarity);
3970c2498f1SSascha Hauer int pwmchip_add(struct pwm_chip *chip);
3980c2498f1SSascha Hauer int pwmchip_remove(struct pwm_chip *chip);
399f051c466SThierry Reding struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
400f051c466SThierry Reding 					 unsigned int index,
401f051c466SThierry Reding 					 const char *label);
4028138d2ddSThierry Reding 
40383af2402SPhilip, Avinash struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *pc,
40483af2402SPhilip, Avinash 		const struct of_phandle_args *args);
40583af2402SPhilip, Avinash 
406d4c0c470SPeter Ujfalusi struct pwm_device *pwm_get(struct device *dev, const char *con_id);
407b2c200e3SFabrice Gasnier struct pwm_device *of_pwm_get(struct device *dev, struct device_node *np,
408b2c200e3SFabrice Gasnier 			      const char *con_id);
4098138d2ddSThierry Reding void pwm_put(struct pwm_device *pwm);
4108138d2ddSThierry Reding 
411d4c0c470SPeter Ujfalusi struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
412261a5eddSPeter Ujfalusi struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
413261a5eddSPeter Ujfalusi 				   const char *con_id);
4144a6ef8e3SNikolaus Voss struct pwm_device *devm_fwnode_pwm_get(struct device *dev,
4154a6ef8e3SNikolaus Voss 				       struct fwnode_handle *fwnode,
4164a6ef8e3SNikolaus Voss 				       const char *con_id);
4176354316dSAlexandre Courbot void devm_pwm_put(struct device *dev, struct pwm_device *pwm);
4180bcf168bSTushar Behera #else
4195ec803edSBoris Brezillon static inline struct pwm_device *pwm_request(int pwm_id, const char *label)
4205ec803edSBoris Brezillon {
4215ec803edSBoris Brezillon 	return ERR_PTR(-ENODEV);
4225ec803edSBoris Brezillon }
4235ec803edSBoris Brezillon 
4245ec803edSBoris Brezillon static inline void pwm_free(struct pwm_device *pwm)
4255ec803edSBoris Brezillon {
4265ec803edSBoris Brezillon }
4275ec803edSBoris Brezillon 
4285ec803edSBoris Brezillon static inline int pwm_apply_state(struct pwm_device *pwm,
4295ec803edSBoris Brezillon 				  const struct pwm_state *state)
4305ec803edSBoris Brezillon {
4315ec803edSBoris Brezillon 	return -ENOTSUPP;
4325ec803edSBoris Brezillon }
4335ec803edSBoris Brezillon 
4345ec803edSBoris Brezillon static inline int pwm_adjust_config(struct pwm_device *pwm)
4355ec803edSBoris Brezillon {
4365ec803edSBoris Brezillon 	return -ENOTSUPP;
4375ec803edSBoris Brezillon }
4385ec803edSBoris Brezillon 
4395ec803edSBoris Brezillon static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
4405ec803edSBoris Brezillon 			     int period_ns)
4415ec803edSBoris Brezillon {
4425ec803edSBoris Brezillon 	return -EINVAL;
4435ec803edSBoris Brezillon }
4445ec803edSBoris Brezillon 
4453a3d1a4eSLee Jones static inline int pwm_capture(struct pwm_device *pwm,
4463a3d1a4eSLee Jones 			      struct pwm_capture *result,
4473a3d1a4eSLee Jones 			      unsigned long timeout)
4483a3d1a4eSLee Jones {
4493a3d1a4eSLee Jones 	return -EINVAL;
4503a3d1a4eSLee Jones }
4513a3d1a4eSLee Jones 
4525ec803edSBoris Brezillon static inline int pwm_enable(struct pwm_device *pwm)
4535ec803edSBoris Brezillon {
4545ec803edSBoris Brezillon 	return -EINVAL;
4555ec803edSBoris Brezillon }
4565ec803edSBoris Brezillon 
4575ec803edSBoris Brezillon static inline void pwm_disable(struct pwm_device *pwm)
4585ec803edSBoris Brezillon {
4595ec803edSBoris Brezillon }
4605ec803edSBoris Brezillon 
4610bcf168bSTushar Behera static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data)
4620bcf168bSTushar Behera {
4630bcf168bSTushar Behera 	return -EINVAL;
4640bcf168bSTushar Behera }
4650bcf168bSTushar Behera 
4660bcf168bSTushar Behera static inline void *pwm_get_chip_data(struct pwm_device *pwm)
4670bcf168bSTushar Behera {
4680bcf168bSTushar Behera 	return NULL;
4690bcf168bSTushar Behera }
4700bcf168bSTushar Behera 
4710bcf168bSTushar Behera static inline int pwmchip_add(struct pwm_chip *chip)
4720bcf168bSTushar Behera {
4730bcf168bSTushar Behera 	return -EINVAL;
4740bcf168bSTushar Behera }
4750bcf168bSTushar Behera 
476b6a00faeSTim Kryger static inline int pwmchip_add_inversed(struct pwm_chip *chip)
477b6a00faeSTim Kryger {
478b6a00faeSTim Kryger 	return -EINVAL;
479b6a00faeSTim Kryger }
480b6a00faeSTim Kryger 
4810bcf168bSTushar Behera static inline int pwmchip_remove(struct pwm_chip *chip)
4820bcf168bSTushar Behera {
4830bcf168bSTushar Behera 	return -EINVAL;
4840bcf168bSTushar Behera }
4850bcf168bSTushar Behera 
4860bcf168bSTushar Behera static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
4870bcf168bSTushar Behera 						       unsigned int index,
4880bcf168bSTushar Behera 						       const char *label)
4890bcf168bSTushar Behera {
4900bcf168bSTushar Behera 	return ERR_PTR(-ENODEV);
4910bcf168bSTushar Behera }
4920bcf168bSTushar Behera 
4930bcf168bSTushar Behera static inline struct pwm_device *pwm_get(struct device *dev,
4940bcf168bSTushar Behera 					 const char *consumer)
4950bcf168bSTushar Behera {
4960bcf168bSTushar Behera 	return ERR_PTR(-ENODEV);
4970bcf168bSTushar Behera }
4980bcf168bSTushar Behera 
499b2c200e3SFabrice Gasnier static inline struct pwm_device *of_pwm_get(struct device *dev,
500b2c200e3SFabrice Gasnier 					    struct device_node *np,
5018eb96127SPeter Ujfalusi 					    const char *con_id)
5028eb96127SPeter Ujfalusi {
5038eb96127SPeter Ujfalusi 	return ERR_PTR(-ENODEV);
5048eb96127SPeter Ujfalusi }
5058eb96127SPeter Ujfalusi 
5060bcf168bSTushar Behera static inline void pwm_put(struct pwm_device *pwm)
5070bcf168bSTushar Behera {
5080bcf168bSTushar Behera }
5090bcf168bSTushar Behera 
5100bcf168bSTushar Behera static inline struct pwm_device *devm_pwm_get(struct device *dev,
5110bcf168bSTushar Behera 					      const char *consumer)
5120bcf168bSTushar Behera {
5130bcf168bSTushar Behera 	return ERR_PTR(-ENODEV);
5140bcf168bSTushar Behera }
5150bcf168bSTushar Behera 
516261a5eddSPeter Ujfalusi static inline struct pwm_device *devm_of_pwm_get(struct device *dev,
517261a5eddSPeter Ujfalusi 						 struct device_node *np,
518261a5eddSPeter Ujfalusi 						 const char *con_id)
519261a5eddSPeter Ujfalusi {
520261a5eddSPeter Ujfalusi 	return ERR_PTR(-ENODEV);
521261a5eddSPeter Ujfalusi }
522261a5eddSPeter Ujfalusi 
5234a6ef8e3SNikolaus Voss static inline struct pwm_device *
5244a6ef8e3SNikolaus Voss devm_fwnode_pwm_get(struct device *dev, struct fwnode_handle *fwnode,
5254a6ef8e3SNikolaus Voss 		    const char *con_id)
5264a6ef8e3SNikolaus Voss {
5274a6ef8e3SNikolaus Voss 	return ERR_PTR(-ENODEV);
5284a6ef8e3SNikolaus Voss }
5294a6ef8e3SNikolaus Voss 
5300bcf168bSTushar Behera static inline void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
5310bcf168bSTushar Behera {
5320bcf168bSTushar Behera }
5330bcf168bSTushar Behera #endif
5346354316dSAlexandre Courbot 
5355ec803edSBoris Brezillon static inline void pwm_apply_args(struct pwm_device *pwm)
5365ec803edSBoris Brezillon {
53733cdcee0SBoris Brezillon 	struct pwm_state state = { };
53833cdcee0SBoris Brezillon 
5395ec803edSBoris Brezillon 	/*
5405ec803edSBoris Brezillon 	 * PWM users calling pwm_apply_args() expect to have a fresh config
5415ec803edSBoris Brezillon 	 * where the polarity and period are set according to pwm_args info.
5425ec803edSBoris Brezillon 	 * The problem is, polarity can only be changed when the PWM is
5435ec803edSBoris Brezillon 	 * disabled.
5445ec803edSBoris Brezillon 	 *
5455ec803edSBoris Brezillon 	 * PWM drivers supporting hardware readout may declare the PWM device
5465ec803edSBoris Brezillon 	 * as enabled, and prevent polarity setting, which changes from the
5475ec803edSBoris Brezillon 	 * existing behavior, where all PWM devices are declared as disabled
5485ec803edSBoris Brezillon 	 * at startup (even if they are actually enabled), thus authorizing
5495ec803edSBoris Brezillon 	 * polarity setting.
5505ec803edSBoris Brezillon 	 *
55133cdcee0SBoris Brezillon 	 * To fulfill this requirement, we apply a new state which disables
55233cdcee0SBoris Brezillon 	 * the PWM device and set the reference period and polarity config.
5535ec803edSBoris Brezillon 	 *
5545ec803edSBoris Brezillon 	 * Note that PWM users requiring a smooth handover between the
5555ec803edSBoris Brezillon 	 * bootloader and the kernel (like critical regulators controlled by
5565ec803edSBoris Brezillon 	 * PWM devices) will have to switch to the atomic API and avoid calling
5575ec803edSBoris Brezillon 	 * pwm_apply_args().
5585ec803edSBoris Brezillon 	 */
55933cdcee0SBoris Brezillon 
56033cdcee0SBoris Brezillon 	state.enabled = false;
56133cdcee0SBoris Brezillon 	state.polarity = pwm->args.polarity;
56233cdcee0SBoris Brezillon 	state.period = pwm->args.period;
56333cdcee0SBoris Brezillon 
56433cdcee0SBoris Brezillon 	pwm_apply_state(pwm, &state);
5655ec803edSBoris Brezillon }
5665ec803edSBoris Brezillon 
5678138d2ddSThierry Reding struct pwm_lookup {
5688138d2ddSThierry Reding 	struct list_head list;
5698138d2ddSThierry Reding 	const char *provider;
5708138d2ddSThierry Reding 	unsigned int index;
5718138d2ddSThierry Reding 	const char *dev_id;
5728138d2ddSThierry Reding 	const char *con_id;
5733796ce1dSAlexandre Belloni 	unsigned int period;
5743796ce1dSAlexandre Belloni 	enum pwm_polarity polarity;
575b526a314SHans de Goede 	const char *module; /* optional, may be NULL */
5768138d2ddSThierry Reding };
5778138d2ddSThierry Reding 
578b526a314SHans de Goede #define PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id,	\
579b526a314SHans de Goede 			       _period, _polarity, _module)		\
5808138d2ddSThierry Reding 	{								\
5818138d2ddSThierry Reding 		.provider = _provider,					\
5828138d2ddSThierry Reding 		.index = _index,					\
5838138d2ddSThierry Reding 		.dev_id = _dev_id,					\
5848138d2ddSThierry Reding 		.con_id = _con_id,					\
58542844029SAlexandre Belloni 		.period = _period,					\
586b526a314SHans de Goede 		.polarity = _polarity,					\
587b526a314SHans de Goede 		.module = _module,					\
5888138d2ddSThierry Reding 	}
5898138d2ddSThierry Reding 
590b526a314SHans de Goede #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
591b526a314SHans de Goede 	PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, _period, \
592b526a314SHans de Goede 			       _polarity, NULL)
593b526a314SHans de Goede 
5940bcf168bSTushar Behera #if IS_ENABLED(CONFIG_PWM)
5958138d2ddSThierry Reding void pwm_add_table(struct pwm_lookup *table, size_t num);
596efb0de55SShobhit Kumar void pwm_remove_table(struct pwm_lookup *table, size_t num);
5970bcf168bSTushar Behera #else
5980bcf168bSTushar Behera static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
5990bcf168bSTushar Behera {
6000bcf168bSTushar Behera }
601efb0de55SShobhit Kumar 
602efb0de55SShobhit Kumar static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
603efb0de55SShobhit Kumar {
604efb0de55SShobhit Kumar }
6050c2498f1SSascha Hauer #endif
6060c2498f1SSascha Hauer 
60776abbddeSH Hartley Sweeten #ifdef CONFIG_PWM_SYSFS
60876abbddeSH Hartley Sweeten void pwmchip_sysfs_export(struct pwm_chip *chip);
60976abbddeSH Hartley Sweeten void pwmchip_sysfs_unexport(struct pwm_chip *chip);
61076abbddeSH Hartley Sweeten #else
61176abbddeSH Hartley Sweeten static inline void pwmchip_sysfs_export(struct pwm_chip *chip)
61276abbddeSH Hartley Sweeten {
61376abbddeSH Hartley Sweeten }
61476abbddeSH Hartley Sweeten 
61576abbddeSH Hartley Sweeten static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip)
61676abbddeSH Hartley Sweeten {
61776abbddeSH Hartley Sweeten }
61876abbddeSH Hartley Sweeten #endif /* CONFIG_PWM_SYSFS */
61976abbddeSH Hartley Sweeten 
6205243ef8bSMark Vels #endif /* __LINUX_PWM_H */
621