xref: /linux-6.15/include/linux/pwm.h (revision f6f3bddf)
11a189b97SRussell King #ifndef __LINUX_PWM_H
21a189b97SRussell King #define __LINUX_PWM_H
31a189b97SRussell King 
40bcf168bSTushar Behera #include <linux/err.h>
5d1cd2142SJonathan Richardson #include <linux/mutex.h>
67299ab70SThierry Reding #include <linux/of.h>
77299ab70SThierry Reding 
862099abfSThierry Reding struct seq_file;
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 {
39e39c0df1SBoris Brezillon 	unsigned int period;
40e39c0df1SBoris Brezillon 	enum pwm_polarity polarity;
41e39c0df1SBoris Brezillon };
42e39c0df1SBoris Brezillon 
43f051c466SThierry Reding enum {
44f051c466SThierry Reding 	PWMF_REQUESTED = 1 << 0,
4509a7e4a3SBoris Brezillon 	PWMF_EXPORTED = 1 << 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
5443a276b0SBoris Brezillon  */
5543a276b0SBoris Brezillon struct pwm_state {
5643a276b0SBoris Brezillon 	unsigned int period;
5743a276b0SBoris Brezillon 	unsigned int duty_cycle;
5843a276b0SBoris Brezillon 	enum pwm_polarity polarity;
5909a7e4a3SBoris Brezillon 	bool enabled;
6043a276b0SBoris Brezillon };
6143a276b0SBoris Brezillon 
6204883802SThierry Reding /**
6304883802SThierry Reding  * struct pwm_device - PWM channel object
6404883802SThierry Reding  * @label: name of the PWM device
6504883802SThierry Reding  * @flags: flags associated with the PWM device
6604883802SThierry Reding  * @hwpwm: per-chip relative index of the PWM device
6704883802SThierry Reding  * @pwm: global index of the PWM device
6804883802SThierry Reding  * @chip: PWM chip providing this PWM device
6904883802SThierry Reding  * @chip_data: chip-private data associated with the PWM device
70e39c0df1SBoris Brezillon  * @args: PWM arguments
7143a276b0SBoris Brezillon  * @state: curent PWM channel state
7204883802SThierry Reding  */
73f051c466SThierry Reding struct pwm_device {
74f051c466SThierry Reding 	const char *label;
75f051c466SThierry Reding 	unsigned long flags;
76f051c466SThierry Reding 	unsigned int hwpwm;
77f051c466SThierry Reding 	unsigned int pwm;
78f051c466SThierry Reding 	struct pwm_chip *chip;
79f051c466SThierry Reding 	void *chip_data;
80f051c466SThierry Reding 
81e39c0df1SBoris Brezillon 	struct pwm_args args;
8243a276b0SBoris Brezillon 	struct pwm_state state;
83f051c466SThierry Reding };
84f051c466SThierry Reding 
8543a276b0SBoris Brezillon /**
8643a276b0SBoris Brezillon  * pwm_get_state() - retrieve the current PWM state
8743a276b0SBoris Brezillon  * @pwm: PWM device
8843a276b0SBoris Brezillon  * @state: state to fill with the current PWM state
8943a276b0SBoris Brezillon  */
9043a276b0SBoris Brezillon static inline void pwm_get_state(const struct pwm_device *pwm,
9143a276b0SBoris Brezillon 				 struct pwm_state *state)
9243a276b0SBoris Brezillon {
9343a276b0SBoris Brezillon 	*state = pwm->state;
9443a276b0SBoris Brezillon }
9543a276b0SBoris Brezillon 
965c31252cSBoris Brezillon static inline bool pwm_is_enabled(const struct pwm_device *pwm)
975c31252cSBoris Brezillon {
9809a7e4a3SBoris Brezillon 	struct pwm_state state;
9909a7e4a3SBoris Brezillon 
10009a7e4a3SBoris Brezillon 	pwm_get_state(pwm, &state);
10109a7e4a3SBoris Brezillon 
10209a7e4a3SBoris Brezillon 	return state.enabled;
1035c31252cSBoris Brezillon }
1045c31252cSBoris Brezillon 
105f051c466SThierry Reding static inline void pwm_set_period(struct pwm_device *pwm, unsigned int period)
106f051c466SThierry Reding {
107f051c466SThierry Reding 	if (pwm)
10843a276b0SBoris Brezillon 		pwm->state.period = period;
109f051c466SThierry Reding }
110f051c466SThierry Reding 
111a1cf4217SBoris Brezillon static inline unsigned int pwm_get_period(const struct pwm_device *pwm)
112f051c466SThierry Reding {
11343a276b0SBoris Brezillon 	struct pwm_state state;
11443a276b0SBoris Brezillon 
11543a276b0SBoris Brezillon 	pwm_get_state(pwm, &state);
11643a276b0SBoris Brezillon 
11743a276b0SBoris Brezillon 	return state.period;
118f051c466SThierry Reding }
119f051c466SThierry Reding 
12076abbddeSH Hartley Sweeten static inline void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty)
12176abbddeSH Hartley Sweeten {
12276abbddeSH Hartley Sweeten 	if (pwm)
12343a276b0SBoris Brezillon 		pwm->state.duty_cycle = duty;
12476abbddeSH Hartley Sweeten }
12576abbddeSH Hartley Sweeten 
126a1cf4217SBoris Brezillon static inline unsigned int pwm_get_duty_cycle(const struct pwm_device *pwm)
12776abbddeSH Hartley Sweeten {
12843a276b0SBoris Brezillon 	struct pwm_state state;
12943a276b0SBoris Brezillon 
13043a276b0SBoris Brezillon 	pwm_get_state(pwm, &state);
13143a276b0SBoris Brezillon 
13243a276b0SBoris Brezillon 	return state.duty_cycle;
13376abbddeSH Hartley Sweeten }
13476abbddeSH Hartley Sweeten 
135011e7631SBoris Brezillon static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm)
136011e7631SBoris Brezillon {
13743a276b0SBoris Brezillon 	struct pwm_state state;
13843a276b0SBoris Brezillon 
13943a276b0SBoris Brezillon 	pwm_get_state(pwm, &state);
14043a276b0SBoris Brezillon 
14143a276b0SBoris Brezillon 	return state.polarity;
142011e7631SBoris Brezillon }
143011e7631SBoris Brezillon 
144e39c0df1SBoris Brezillon static inline void pwm_get_args(const struct pwm_device *pwm,
145e39c0df1SBoris Brezillon 				struct pwm_args *args)
146e39c0df1SBoris Brezillon {
147e39c0df1SBoris Brezillon 	*args = pwm->args;
148e39c0df1SBoris Brezillon }
149e39c0df1SBoris Brezillon 
1500c2498f1SSascha Hauer /**
151a6a0dbbcSBoris Brezillon  * pwm_init_state() - prepare a new state to be applied with pwm_apply_state()
152a6a0dbbcSBoris Brezillon  * @pwm: PWM device
153a6a0dbbcSBoris Brezillon  * @state: state to fill with the prepared PWM state
154a6a0dbbcSBoris Brezillon  *
155a6a0dbbcSBoris Brezillon  * This functions prepares a state that can later be tweaked and applied
156a6a0dbbcSBoris Brezillon  * to the PWM device with pwm_apply_state(). This is a convenient function
157a6a0dbbcSBoris Brezillon  * that first retrieves the current PWM state and the replaces the period
158a6a0dbbcSBoris Brezillon  * and polarity fields with the reference values defined in pwm->args.
159a6a0dbbcSBoris Brezillon  * Once the function returns, you can adjust the ->enabled and ->duty_cycle
160a6a0dbbcSBoris Brezillon  * fields according to your needs before calling pwm_apply_state().
161a6a0dbbcSBoris Brezillon  *
162a6a0dbbcSBoris Brezillon  * ->duty_cycle is initially set to zero to avoid cases where the current
163a6a0dbbcSBoris Brezillon  * ->duty_cycle value exceed the pwm_args->period one, which would trigger
164a6a0dbbcSBoris Brezillon  * an error if the user calls pwm_apply_state() without adjusting ->duty_cycle
165a6a0dbbcSBoris Brezillon  * first.
166a6a0dbbcSBoris Brezillon  */
167a6a0dbbcSBoris Brezillon static inline void pwm_init_state(const struct pwm_device *pwm,
168a6a0dbbcSBoris Brezillon 				  struct pwm_state *state)
169a6a0dbbcSBoris Brezillon {
170a6a0dbbcSBoris Brezillon 	struct pwm_args args;
171a6a0dbbcSBoris Brezillon 
172a6a0dbbcSBoris Brezillon 	/* First get the current state. */
173a6a0dbbcSBoris Brezillon 	pwm_get_state(pwm, state);
174a6a0dbbcSBoris Brezillon 
175a6a0dbbcSBoris Brezillon 	/* Then fill it with the reference config */
176a6a0dbbcSBoris Brezillon 	pwm_get_args(pwm, &args);
177a6a0dbbcSBoris Brezillon 
178a6a0dbbcSBoris Brezillon 	state->period = args.period;
179a6a0dbbcSBoris Brezillon 	state->polarity = args.polarity;
180a6a0dbbcSBoris Brezillon 	state->duty_cycle = 0;
181a6a0dbbcSBoris Brezillon }
182a6a0dbbcSBoris Brezillon 
183a6a0dbbcSBoris Brezillon /**
184*f6f3bddfSBoris Brezillon  * pwm_get_relative_duty_cycle() - Get a relative duty cycle value
185*f6f3bddfSBoris Brezillon  * @state: PWM state to extract the duty cycle from
186*f6f3bddfSBoris Brezillon  * @scale: target scale of the relative duty cycle
187*f6f3bddfSBoris Brezillon  *
188*f6f3bddfSBoris Brezillon  * This functions converts the absolute duty cycle stored in @state (expressed
189*f6f3bddfSBoris Brezillon  * in nanosecond) into a value relative to the period.
190*f6f3bddfSBoris Brezillon  *
191*f6f3bddfSBoris Brezillon  * For example if you want to get the duty_cycle expressed in percent, call:
192*f6f3bddfSBoris Brezillon  *
193*f6f3bddfSBoris Brezillon  * pwm_get_state(pwm, &state);
194*f6f3bddfSBoris Brezillon  * duty = pwm_get_relative_duty_cycle(&state, 100);
195*f6f3bddfSBoris Brezillon  */
196*f6f3bddfSBoris Brezillon static inline unsigned int
197*f6f3bddfSBoris Brezillon pwm_get_relative_duty_cycle(const struct pwm_state *state, unsigned int scale)
198*f6f3bddfSBoris Brezillon {
199*f6f3bddfSBoris Brezillon 	if (!state->period)
200*f6f3bddfSBoris Brezillon 		return 0;
201*f6f3bddfSBoris Brezillon 
202*f6f3bddfSBoris Brezillon 	return DIV_ROUND_CLOSEST_ULL((u64)state->duty_cycle * scale,
203*f6f3bddfSBoris Brezillon 				     state->period);
204*f6f3bddfSBoris Brezillon }
205*f6f3bddfSBoris Brezillon 
206*f6f3bddfSBoris Brezillon /**
207*f6f3bddfSBoris Brezillon  * pwm_set_relative_duty_cycle() - Set a relative duty cycle value
208*f6f3bddfSBoris Brezillon  * @state: PWM state to fill
209*f6f3bddfSBoris Brezillon  * @duty_cycle: relative duty cycle value
210*f6f3bddfSBoris Brezillon  * @scale: scale in which @duty_cycle is expressed
211*f6f3bddfSBoris Brezillon  *
212*f6f3bddfSBoris Brezillon  * This functions converts a relative into an absolute duty cycle (expressed
213*f6f3bddfSBoris Brezillon  * in nanoseconds), and puts the result in state->duty_cycle.
214*f6f3bddfSBoris Brezillon  *
215*f6f3bddfSBoris Brezillon  * For example if you want to configure a 50% duty cycle, call:
216*f6f3bddfSBoris Brezillon  *
217*f6f3bddfSBoris Brezillon  * pwm_init_state(pwm, &state);
218*f6f3bddfSBoris Brezillon  * pwm_set_relative_duty_cycle(&state, 50, 100);
219*f6f3bddfSBoris Brezillon  * pwm_apply_state(pwm, &state);
220*f6f3bddfSBoris Brezillon  *
221*f6f3bddfSBoris Brezillon  * This functions returns -EINVAL if @duty_cycle and/or @scale are
222*f6f3bddfSBoris Brezillon  * inconsistent (@scale == 0 or @duty_cycle > @scale).
223*f6f3bddfSBoris Brezillon  */
224*f6f3bddfSBoris Brezillon static inline int
225*f6f3bddfSBoris Brezillon pwm_set_relative_duty_cycle(struct pwm_state *state, unsigned int duty_cycle,
226*f6f3bddfSBoris Brezillon 			    unsigned int scale)
227*f6f3bddfSBoris Brezillon {
228*f6f3bddfSBoris Brezillon 	if (!scale || duty_cycle > scale)
229*f6f3bddfSBoris Brezillon 		return -EINVAL;
230*f6f3bddfSBoris Brezillon 
231*f6f3bddfSBoris Brezillon 	state->duty_cycle = DIV_ROUND_CLOSEST_ULL((u64)duty_cycle *
232*f6f3bddfSBoris Brezillon 						  state->period,
233*f6f3bddfSBoris Brezillon 						  scale);
234*f6f3bddfSBoris Brezillon 
235*f6f3bddfSBoris Brezillon 	return 0;
236*f6f3bddfSBoris Brezillon }
237*f6f3bddfSBoris Brezillon 
238*f6f3bddfSBoris Brezillon /**
2390c2498f1SSascha Hauer  * struct pwm_ops - PWM controller operations
2400c2498f1SSascha Hauer  * @request: optional hook for requesting a PWM
2410c2498f1SSascha Hauer  * @free: optional hook for freeing a PWM
2420c2498f1SSascha Hauer  * @config: configure duty cycles and period length for this PWM
2430aa0869cSPhilip, Avinash  * @set_polarity: configure the polarity of this PWM
2440c2498f1SSascha Hauer  * @enable: enable PWM output toggling
2450c2498f1SSascha Hauer  * @disable: disable PWM output toggling
2465ec803edSBoris Brezillon  * @apply: atomically apply a new PWM config. The state argument
2475ec803edSBoris Brezillon  *	   should be adjusted with the real hardware config (if the
2485ec803edSBoris Brezillon  *	   approximate the period or duty_cycle value, state should
2495ec803edSBoris Brezillon  *	   reflect it)
25015fa8a43SBoris Brezillon  * @get_state: get the current PWM state. This function is only
25115fa8a43SBoris Brezillon  *	       called once per PWM device when the PWM chip is
25215fa8a43SBoris Brezillon  *	       registered.
25362099abfSThierry Reding  * @dbg_show: optional routine to show contents in debugfs
2540c2498f1SSascha Hauer  * @owner: helps prevent removal of modules exporting active PWMs
2550c2498f1SSascha Hauer  */
2560c2498f1SSascha Hauer struct pwm_ops {
2576bc7064aSThierry Reding 	int (*request)(struct pwm_chip *chip, struct pwm_device *pwm);
2586bc7064aSThierry Reding 	void (*free)(struct pwm_chip *chip, struct pwm_device *pwm);
2596bc7064aSThierry Reding 	int (*config)(struct pwm_chip *chip, struct pwm_device *pwm,
260f051c466SThierry Reding 		      int duty_ns, int period_ns);
2616bc7064aSThierry Reding 	int (*set_polarity)(struct pwm_chip *chip, struct pwm_device *pwm,
2620aa0869cSPhilip, Avinash 			    enum pwm_polarity polarity);
2636bc7064aSThierry Reding 	int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm);
2646bc7064aSThierry Reding 	void (*disable)(struct pwm_chip *chip, struct pwm_device *pwm);
2655ec803edSBoris Brezillon 	int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm,
2665ec803edSBoris Brezillon 		     struct pwm_state *state);
26715fa8a43SBoris Brezillon 	void (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,
26815fa8a43SBoris Brezillon 			  struct pwm_state *state);
26962099abfSThierry Reding #ifdef CONFIG_DEBUG_FS
2706bc7064aSThierry Reding 	void (*dbg_show)(struct pwm_chip *chip, struct seq_file *s);
27162099abfSThierry Reding #endif
2720c2498f1SSascha Hauer 	struct module *owner;
2730c2498f1SSascha Hauer };
2740c2498f1SSascha Hauer 
2750c2498f1SSascha Hauer /**
276f051c466SThierry Reding  * struct pwm_chip - abstract a PWM controller
277f051c466SThierry Reding  * @dev: device providing the PWMs
278f051c466SThierry Reding  * @list: list node for internal use
279f051c466SThierry Reding  * @ops: callbacks for this PWM controller
280f051c466SThierry Reding  * @base: number of first PWM controlled by this chip
281f051c466SThierry Reding  * @npwm: number of PWMs controlled by this chip
282f051c466SThierry Reding  * @pwms: array of PWM devices allocated by the framework
28304883802SThierry Reding  * @of_xlate: request a PWM device given a device tree PWM specifier
28404883802SThierry Reding  * @of_pwm_n_cells: number of cells expected in the device tree PWM specifier
2856e69ab13SFlorian Vaussard  * @can_sleep: must be true if the .config(), .enable() or .disable()
2866e69ab13SFlorian Vaussard  *             operations may sleep
2870c2498f1SSascha Hauer  */
2880c2498f1SSascha Hauer struct pwm_chip {
289f051c466SThierry Reding 	struct device *dev;
290f051c466SThierry Reding 	struct list_head list;
291f051c466SThierry Reding 	const struct pwm_ops *ops;
292f051c466SThierry Reding 	int base;
293f051c466SThierry Reding 	unsigned int npwm;
294f051c466SThierry Reding 
295f051c466SThierry Reding 	struct pwm_device *pwms;
2967299ab70SThierry Reding 
2977299ab70SThierry Reding 	struct pwm_device * (*of_xlate)(struct pwm_chip *pc,
2987299ab70SThierry Reding 					const struct of_phandle_args *args);
2997299ab70SThierry Reding 	unsigned int of_pwm_n_cells;
3006e69ab13SFlorian Vaussard 	bool can_sleep;
3010c2498f1SSascha Hauer };
3020c2498f1SSascha Hauer 
3030bcf168bSTushar Behera #if IS_ENABLED(CONFIG_PWM)
3045ec803edSBoris Brezillon /* PWM user APIs */
3055ec803edSBoris Brezillon struct pwm_device *pwm_request(int pwm_id, const char *label);
3065ec803edSBoris Brezillon void pwm_free(struct pwm_device *pwm);
3075ec803edSBoris Brezillon int pwm_apply_state(struct pwm_device *pwm, struct pwm_state *state);
3085ec803edSBoris Brezillon int pwm_adjust_config(struct pwm_device *pwm);
3095ec803edSBoris Brezillon 
3105ec803edSBoris Brezillon /**
3115ec803edSBoris Brezillon  * pwm_config() - change a PWM device configuration
3125ec803edSBoris Brezillon  * @pwm: PWM device
3135ec803edSBoris Brezillon  * @duty_ns: "on" time (in nanoseconds)
3145ec803edSBoris Brezillon  * @period_ns: duration (in nanoseconds) of one cycle
3155ec803edSBoris Brezillon  *
3165ec803edSBoris Brezillon  * Returns: 0 on success or a negative error code on failure.
3175ec803edSBoris Brezillon  */
3185ec803edSBoris Brezillon static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
3195ec803edSBoris Brezillon 			     int period_ns)
3205ec803edSBoris Brezillon {
3215ec803edSBoris Brezillon 	struct pwm_state state;
3225ec803edSBoris Brezillon 
3235ec803edSBoris Brezillon 	if (!pwm)
3245ec803edSBoris Brezillon 		return -EINVAL;
3255ec803edSBoris Brezillon 
3265ec803edSBoris Brezillon 	pwm_get_state(pwm, &state);
3275ec803edSBoris Brezillon 	if (state.duty_cycle == duty_ns && state.period == period_ns)
3285ec803edSBoris Brezillon 		return 0;
3295ec803edSBoris Brezillon 
3305ec803edSBoris Brezillon 	state.duty_cycle = duty_ns;
3315ec803edSBoris Brezillon 	state.period = period_ns;
3325ec803edSBoris Brezillon 	return pwm_apply_state(pwm, &state);
3335ec803edSBoris Brezillon }
3345ec803edSBoris Brezillon 
3355ec803edSBoris Brezillon /**
3365ec803edSBoris Brezillon  * pwm_set_polarity() - configure the polarity of a PWM signal
3375ec803edSBoris Brezillon  * @pwm: PWM device
3385ec803edSBoris Brezillon  * @polarity: new polarity of the PWM signal
3395ec803edSBoris Brezillon  *
3405ec803edSBoris Brezillon  * Note that the polarity cannot be configured while the PWM device is
3415ec803edSBoris Brezillon  * enabled.
3425ec803edSBoris Brezillon  *
3435ec803edSBoris Brezillon  * Returns: 0 on success or a negative error code on failure.
3445ec803edSBoris Brezillon  */
3455ec803edSBoris Brezillon static inline int pwm_set_polarity(struct pwm_device *pwm,
3465ec803edSBoris Brezillon 				   enum pwm_polarity polarity)
3475ec803edSBoris Brezillon {
3485ec803edSBoris Brezillon 	struct pwm_state state;
3495ec803edSBoris Brezillon 
3505ec803edSBoris Brezillon 	if (!pwm)
3515ec803edSBoris Brezillon 		return -EINVAL;
3525ec803edSBoris Brezillon 
3535ec803edSBoris Brezillon 	pwm_get_state(pwm, &state);
3545ec803edSBoris Brezillon 	if (state.polarity == polarity)
3555ec803edSBoris Brezillon 		return 0;
3565ec803edSBoris Brezillon 
3575ec803edSBoris Brezillon 	/*
3585ec803edSBoris Brezillon 	 * Changing the polarity of a running PWM without adjusting the
3595ec803edSBoris Brezillon 	 * dutycycle/period value is a bit risky (can introduce glitches).
3605ec803edSBoris Brezillon 	 * Return -EBUSY in this case.
3615ec803edSBoris Brezillon 	 * Note that this is allowed when using pwm_apply_state() because
3625ec803edSBoris Brezillon 	 * the user specifies all the parameters.
3635ec803edSBoris Brezillon 	 */
3645ec803edSBoris Brezillon 	if (state.enabled)
3655ec803edSBoris Brezillon 		return -EBUSY;
3665ec803edSBoris Brezillon 
3675ec803edSBoris Brezillon 	state.polarity = polarity;
3685ec803edSBoris Brezillon 	return pwm_apply_state(pwm, &state);
3695ec803edSBoris Brezillon }
3705ec803edSBoris Brezillon 
3715ec803edSBoris Brezillon /**
3725ec803edSBoris Brezillon  * pwm_enable() - start a PWM output toggling
3735ec803edSBoris Brezillon  * @pwm: PWM device
3745ec803edSBoris Brezillon  *
3755ec803edSBoris Brezillon  * Returns: 0 on success or a negative error code on failure.
3765ec803edSBoris Brezillon  */
3775ec803edSBoris Brezillon static inline int pwm_enable(struct pwm_device *pwm)
3785ec803edSBoris Brezillon {
3795ec803edSBoris Brezillon 	struct pwm_state state;
3805ec803edSBoris Brezillon 
3815ec803edSBoris Brezillon 	if (!pwm)
3825ec803edSBoris Brezillon 		return -EINVAL;
3835ec803edSBoris Brezillon 
3845ec803edSBoris Brezillon 	pwm_get_state(pwm, &state);
3855ec803edSBoris Brezillon 	if (state.enabled)
3865ec803edSBoris Brezillon 		return 0;
3875ec803edSBoris Brezillon 
3885ec803edSBoris Brezillon 	state.enabled = true;
3895ec803edSBoris Brezillon 	return pwm_apply_state(pwm, &state);
3905ec803edSBoris Brezillon }
3915ec803edSBoris Brezillon 
3925ec803edSBoris Brezillon /**
3935ec803edSBoris Brezillon  * pwm_disable() - stop a PWM output toggling
3945ec803edSBoris Brezillon  * @pwm: PWM device
3955ec803edSBoris Brezillon  */
3965ec803edSBoris Brezillon static inline void pwm_disable(struct pwm_device *pwm)
3975ec803edSBoris Brezillon {
3985ec803edSBoris Brezillon 	struct pwm_state state;
3995ec803edSBoris Brezillon 
4005ec803edSBoris Brezillon 	if (!pwm)
4015ec803edSBoris Brezillon 		return;
4025ec803edSBoris Brezillon 
4035ec803edSBoris Brezillon 	pwm_get_state(pwm, &state);
4045ec803edSBoris Brezillon 	if (!state.enabled)
4055ec803edSBoris Brezillon 		return;
4065ec803edSBoris Brezillon 
4075ec803edSBoris Brezillon 	state.enabled = false;
4085ec803edSBoris Brezillon 	pwm_apply_state(pwm, &state);
4095ec803edSBoris Brezillon }
4105ec803edSBoris Brezillon 
4115ec803edSBoris Brezillon 
4125ec803edSBoris Brezillon /* PWM provider APIs */
413f051c466SThierry Reding int pwm_set_chip_data(struct pwm_device *pwm, void *data);
414f051c466SThierry Reding void *pwm_get_chip_data(struct pwm_device *pwm);
415f051c466SThierry Reding 
416b6a00faeSTim Kryger int pwmchip_add_with_polarity(struct pwm_chip *chip,
417b6a00faeSTim Kryger 			      enum pwm_polarity polarity);
4180c2498f1SSascha Hauer int pwmchip_add(struct pwm_chip *chip);
4190c2498f1SSascha Hauer int pwmchip_remove(struct pwm_chip *chip);
420f051c466SThierry Reding struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
421f051c466SThierry Reding 					 unsigned int index,
422f051c466SThierry Reding 					 const char *label);
4238138d2ddSThierry Reding 
42483af2402SPhilip, Avinash struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *pc,
42583af2402SPhilip, Avinash 		const struct of_phandle_args *args);
42683af2402SPhilip, Avinash 
427d4c0c470SPeter Ujfalusi struct pwm_device *pwm_get(struct device *dev, const char *con_id);
4288eb96127SPeter Ujfalusi struct pwm_device *of_pwm_get(struct device_node *np, 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);
432261a5eddSPeter Ujfalusi struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
433261a5eddSPeter Ujfalusi 				   const char *con_id);
4346354316dSAlexandre Courbot void devm_pwm_put(struct device *dev, struct pwm_device *pwm);
4356e69ab13SFlorian Vaussard 
4366e69ab13SFlorian Vaussard bool pwm_can_sleep(struct pwm_device *pwm);
4370bcf168bSTushar Behera #else
4385ec803edSBoris Brezillon static inline struct pwm_device *pwm_request(int pwm_id, const char *label)
4395ec803edSBoris Brezillon {
4405ec803edSBoris Brezillon 	return ERR_PTR(-ENODEV);
4415ec803edSBoris Brezillon }
4425ec803edSBoris Brezillon 
4435ec803edSBoris Brezillon static inline void pwm_free(struct pwm_device *pwm)
4445ec803edSBoris Brezillon {
4455ec803edSBoris Brezillon }
4465ec803edSBoris Brezillon 
4475ec803edSBoris Brezillon static inline int pwm_apply_state(struct pwm_device *pwm,
4485ec803edSBoris Brezillon 				  const struct pwm_state *state)
4495ec803edSBoris Brezillon {
4505ec803edSBoris Brezillon 	return -ENOTSUPP;
4515ec803edSBoris Brezillon }
4525ec803edSBoris Brezillon 
4535ec803edSBoris Brezillon static inline int pwm_adjust_config(struct pwm_device *pwm)
4545ec803edSBoris Brezillon {
4555ec803edSBoris Brezillon 	return -ENOTSUPP;
4565ec803edSBoris Brezillon }
4575ec803edSBoris Brezillon 
4585ec803edSBoris Brezillon static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
4595ec803edSBoris Brezillon 			     int period_ns)
4605ec803edSBoris Brezillon {
4615ec803edSBoris Brezillon 	return -EINVAL;
4625ec803edSBoris Brezillon }
4635ec803edSBoris Brezillon 
4645ec803edSBoris Brezillon static inline int pwm_set_polarity(struct pwm_device *pwm,
4655ec803edSBoris Brezillon 				   enum pwm_polarity polarity)
4665ec803edSBoris Brezillon {
4675ec803edSBoris Brezillon 	return -ENOTSUPP;
4685ec803edSBoris Brezillon }
4695ec803edSBoris Brezillon 
4705ec803edSBoris Brezillon static inline int pwm_enable(struct pwm_device *pwm)
4715ec803edSBoris Brezillon {
4725ec803edSBoris Brezillon 	return -EINVAL;
4735ec803edSBoris Brezillon }
4745ec803edSBoris Brezillon 
4755ec803edSBoris Brezillon static inline void pwm_disable(struct pwm_device *pwm)
4765ec803edSBoris Brezillon {
4775ec803edSBoris Brezillon }
4785ec803edSBoris Brezillon 
4790bcf168bSTushar Behera static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data)
4800bcf168bSTushar Behera {
4810bcf168bSTushar Behera 	return -EINVAL;
4820bcf168bSTushar Behera }
4830bcf168bSTushar Behera 
4840bcf168bSTushar Behera static inline void *pwm_get_chip_data(struct pwm_device *pwm)
4850bcf168bSTushar Behera {
4860bcf168bSTushar Behera 	return NULL;
4870bcf168bSTushar Behera }
4880bcf168bSTushar Behera 
4890bcf168bSTushar Behera static inline int pwmchip_add(struct pwm_chip *chip)
4900bcf168bSTushar Behera {
4910bcf168bSTushar Behera 	return -EINVAL;
4920bcf168bSTushar Behera }
4930bcf168bSTushar Behera 
494b6a00faeSTim Kryger static inline int pwmchip_add_inversed(struct pwm_chip *chip)
495b6a00faeSTim Kryger {
496b6a00faeSTim Kryger 	return -EINVAL;
497b6a00faeSTim Kryger }
498b6a00faeSTim Kryger 
4990bcf168bSTushar Behera static inline int pwmchip_remove(struct pwm_chip *chip)
5000bcf168bSTushar Behera {
5010bcf168bSTushar Behera 	return -EINVAL;
5020bcf168bSTushar Behera }
5030bcf168bSTushar Behera 
5040bcf168bSTushar Behera static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
5050bcf168bSTushar Behera 						       unsigned int index,
5060bcf168bSTushar Behera 						       const char *label)
5070bcf168bSTushar Behera {
5080bcf168bSTushar Behera 	return ERR_PTR(-ENODEV);
5090bcf168bSTushar Behera }
5100bcf168bSTushar Behera 
5110bcf168bSTushar Behera static inline struct pwm_device *pwm_get(struct device *dev,
5120bcf168bSTushar Behera 					 const char *consumer)
5130bcf168bSTushar Behera {
5140bcf168bSTushar Behera 	return ERR_PTR(-ENODEV);
5150bcf168bSTushar Behera }
5160bcf168bSTushar Behera 
5178eb96127SPeter Ujfalusi static inline struct pwm_device *of_pwm_get(struct device_node *np,
5188eb96127SPeter Ujfalusi 					    const char *con_id)
5198eb96127SPeter Ujfalusi {
5208eb96127SPeter Ujfalusi 	return ERR_PTR(-ENODEV);
5218eb96127SPeter Ujfalusi }
5228eb96127SPeter Ujfalusi 
5230bcf168bSTushar Behera static inline void pwm_put(struct pwm_device *pwm)
5240bcf168bSTushar Behera {
5250bcf168bSTushar Behera }
5260bcf168bSTushar Behera 
5270bcf168bSTushar Behera static inline struct pwm_device *devm_pwm_get(struct device *dev,
5280bcf168bSTushar Behera 					      const char *consumer)
5290bcf168bSTushar Behera {
5300bcf168bSTushar Behera 	return ERR_PTR(-ENODEV);
5310bcf168bSTushar Behera }
5320bcf168bSTushar Behera 
533261a5eddSPeter Ujfalusi static inline struct pwm_device *devm_of_pwm_get(struct device *dev,
534261a5eddSPeter Ujfalusi 						 struct device_node *np,
535261a5eddSPeter Ujfalusi 						 const char *con_id)
536261a5eddSPeter Ujfalusi {
537261a5eddSPeter Ujfalusi 	return ERR_PTR(-ENODEV);
538261a5eddSPeter Ujfalusi }
539261a5eddSPeter Ujfalusi 
5400bcf168bSTushar Behera static inline void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
5410bcf168bSTushar Behera {
5420bcf168bSTushar Behera }
5436e69ab13SFlorian Vaussard 
5446e69ab13SFlorian Vaussard static inline bool pwm_can_sleep(struct pwm_device *pwm)
5456e69ab13SFlorian Vaussard {
5466e69ab13SFlorian Vaussard 	return false;
5476e69ab13SFlorian Vaussard }
5480bcf168bSTushar Behera #endif
5496354316dSAlexandre Courbot 
5505ec803edSBoris Brezillon static inline void pwm_apply_args(struct pwm_device *pwm)
5515ec803edSBoris Brezillon {
5525ec803edSBoris Brezillon 	/*
5535ec803edSBoris Brezillon 	 * PWM users calling pwm_apply_args() expect to have a fresh config
5545ec803edSBoris Brezillon 	 * where the polarity and period are set according to pwm_args info.
5555ec803edSBoris Brezillon 	 * The problem is, polarity can only be changed when the PWM is
5565ec803edSBoris Brezillon 	 * disabled.
5575ec803edSBoris Brezillon 	 *
5585ec803edSBoris Brezillon 	 * PWM drivers supporting hardware readout may declare the PWM device
5595ec803edSBoris Brezillon 	 * as enabled, and prevent polarity setting, which changes from the
5605ec803edSBoris Brezillon 	 * existing behavior, where all PWM devices are declared as disabled
5615ec803edSBoris Brezillon 	 * at startup (even if they are actually enabled), thus authorizing
5625ec803edSBoris Brezillon 	 * polarity setting.
5635ec803edSBoris Brezillon 	 *
5645ec803edSBoris Brezillon 	 * Instead of setting ->enabled to false, we call pwm_disable()
5655ec803edSBoris Brezillon 	 * before pwm_set_polarity() to ensure that everything is configured
5665ec803edSBoris Brezillon 	 * as expected, and the PWM is really disabled when the user request
5675ec803edSBoris Brezillon 	 * it.
5685ec803edSBoris Brezillon 	 *
5695ec803edSBoris Brezillon 	 * Note that PWM users requiring a smooth handover between the
5705ec803edSBoris Brezillon 	 * bootloader and the kernel (like critical regulators controlled by
5715ec803edSBoris Brezillon 	 * PWM devices) will have to switch to the atomic API and avoid calling
5725ec803edSBoris Brezillon 	 * pwm_apply_args().
5735ec803edSBoris Brezillon 	 */
5745ec803edSBoris Brezillon 	pwm_disable(pwm);
5755ec803edSBoris Brezillon 	pwm_set_polarity(pwm, pwm->args.polarity);
5765ec803edSBoris Brezillon }
5775ec803edSBoris Brezillon 
5788138d2ddSThierry Reding struct pwm_lookup {
5798138d2ddSThierry Reding 	struct list_head list;
5808138d2ddSThierry Reding 	const char *provider;
5818138d2ddSThierry Reding 	unsigned int index;
5828138d2ddSThierry Reding 	const char *dev_id;
5838138d2ddSThierry Reding 	const char *con_id;
5843796ce1dSAlexandre Belloni 	unsigned int period;
5853796ce1dSAlexandre Belloni 	enum pwm_polarity polarity;
5868138d2ddSThierry Reding };
5878138d2ddSThierry Reding 
58842844029SAlexandre Belloni #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
5898138d2ddSThierry Reding 	{						\
5908138d2ddSThierry Reding 		.provider = _provider,			\
5918138d2ddSThierry Reding 		.index = _index,			\
5928138d2ddSThierry Reding 		.dev_id = _dev_id,			\
5938138d2ddSThierry Reding 		.con_id = _con_id,			\
59442844029SAlexandre Belloni 		.period = _period,			\
59542844029SAlexandre Belloni 		.polarity = _polarity			\
5968138d2ddSThierry Reding 	}
5978138d2ddSThierry Reding 
5980bcf168bSTushar Behera #if IS_ENABLED(CONFIG_PWM)
5998138d2ddSThierry Reding void pwm_add_table(struct pwm_lookup *table, size_t num);
600efb0de55SShobhit Kumar void pwm_remove_table(struct pwm_lookup *table, size_t num);
6010bcf168bSTushar Behera #else
6020bcf168bSTushar Behera static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
6030bcf168bSTushar Behera {
6040bcf168bSTushar Behera }
605efb0de55SShobhit Kumar 
606efb0de55SShobhit Kumar static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
607efb0de55SShobhit Kumar {
608efb0de55SShobhit Kumar }
6090c2498f1SSascha Hauer #endif
6100c2498f1SSascha Hauer 
61176abbddeSH Hartley Sweeten #ifdef CONFIG_PWM_SYSFS
61276abbddeSH Hartley Sweeten void pwmchip_sysfs_export(struct pwm_chip *chip);
61376abbddeSH Hartley Sweeten void pwmchip_sysfs_unexport(struct pwm_chip *chip);
61476abbddeSH Hartley Sweeten #else
61576abbddeSH Hartley Sweeten static inline void pwmchip_sysfs_export(struct pwm_chip *chip)
61676abbddeSH Hartley Sweeten {
61776abbddeSH Hartley Sweeten }
61876abbddeSH Hartley Sweeten 
61976abbddeSH Hartley Sweeten static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip)
62076abbddeSH Hartley Sweeten {
62176abbddeSH Hartley Sweeten }
62276abbddeSH Hartley Sweeten #endif /* CONFIG_PWM_SYSFS */
62376abbddeSH Hartley Sweeten 
6245243ef8bSMark Vels #endif /* __LINUX_PWM_H */
625