xref: /linux-6.15/include/linux/pwm.h (revision 5ec803ed)
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 /**
1510c2498f1SSascha Hauer  * struct pwm_ops - PWM controller operations
1520c2498f1SSascha Hauer  * @request: optional hook for requesting a PWM
1530c2498f1SSascha Hauer  * @free: optional hook for freeing a PWM
1540c2498f1SSascha Hauer  * @config: configure duty cycles and period length for this PWM
1550aa0869cSPhilip, Avinash  * @set_polarity: configure the polarity of this PWM
1560c2498f1SSascha Hauer  * @enable: enable PWM output toggling
1570c2498f1SSascha Hauer  * @disable: disable PWM output toggling
158*5ec803edSBoris Brezillon  * @apply: atomically apply a new PWM config. The state argument
159*5ec803edSBoris Brezillon  *	   should be adjusted with the real hardware config (if the
160*5ec803edSBoris Brezillon  *	   approximate the period or duty_cycle value, state should
161*5ec803edSBoris Brezillon  *	   reflect it)
16215fa8a43SBoris Brezillon  * @get_state: get the current PWM state. This function is only
16315fa8a43SBoris Brezillon  *	       called once per PWM device when the PWM chip is
16415fa8a43SBoris Brezillon  *	       registered.
16562099abfSThierry Reding  * @dbg_show: optional routine to show contents in debugfs
1660c2498f1SSascha Hauer  * @owner: helps prevent removal of modules exporting active PWMs
1670c2498f1SSascha Hauer  */
1680c2498f1SSascha Hauer struct pwm_ops {
1696bc7064aSThierry Reding 	int (*request)(struct pwm_chip *chip, struct pwm_device *pwm);
1706bc7064aSThierry Reding 	void (*free)(struct pwm_chip *chip, struct pwm_device *pwm);
1716bc7064aSThierry Reding 	int (*config)(struct pwm_chip *chip, struct pwm_device *pwm,
172f051c466SThierry Reding 		      int duty_ns, int period_ns);
1736bc7064aSThierry Reding 	int (*set_polarity)(struct pwm_chip *chip, struct pwm_device *pwm,
1740aa0869cSPhilip, Avinash 			    enum pwm_polarity polarity);
1756bc7064aSThierry Reding 	int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm);
1766bc7064aSThierry Reding 	void (*disable)(struct pwm_chip *chip, struct pwm_device *pwm);
177*5ec803edSBoris Brezillon 	int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm,
178*5ec803edSBoris Brezillon 		     struct pwm_state *state);
17915fa8a43SBoris Brezillon 	void (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,
18015fa8a43SBoris Brezillon 			  struct pwm_state *state);
18162099abfSThierry Reding #ifdef CONFIG_DEBUG_FS
1826bc7064aSThierry Reding 	void (*dbg_show)(struct pwm_chip *chip, struct seq_file *s);
18362099abfSThierry Reding #endif
1840c2498f1SSascha Hauer 	struct module *owner;
1850c2498f1SSascha Hauer };
1860c2498f1SSascha Hauer 
1870c2498f1SSascha Hauer /**
188f051c466SThierry Reding  * struct pwm_chip - abstract a PWM controller
189f051c466SThierry Reding  * @dev: device providing the PWMs
190f051c466SThierry Reding  * @list: list node for internal use
191f051c466SThierry Reding  * @ops: callbacks for this PWM controller
192f051c466SThierry Reding  * @base: number of first PWM controlled by this chip
193f051c466SThierry Reding  * @npwm: number of PWMs controlled by this chip
194f051c466SThierry Reding  * @pwms: array of PWM devices allocated by the framework
19504883802SThierry Reding  * @of_xlate: request a PWM device given a device tree PWM specifier
19604883802SThierry Reding  * @of_pwm_n_cells: number of cells expected in the device tree PWM specifier
1976e69ab13SFlorian Vaussard  * @can_sleep: must be true if the .config(), .enable() or .disable()
1986e69ab13SFlorian Vaussard  *             operations may sleep
1990c2498f1SSascha Hauer  */
2000c2498f1SSascha Hauer struct pwm_chip {
201f051c466SThierry Reding 	struct device *dev;
202f051c466SThierry Reding 	struct list_head list;
203f051c466SThierry Reding 	const struct pwm_ops *ops;
204f051c466SThierry Reding 	int base;
205f051c466SThierry Reding 	unsigned int npwm;
206f051c466SThierry Reding 
207f051c466SThierry Reding 	struct pwm_device *pwms;
2087299ab70SThierry Reding 
2097299ab70SThierry Reding 	struct pwm_device * (*of_xlate)(struct pwm_chip *pc,
2107299ab70SThierry Reding 					const struct of_phandle_args *args);
2117299ab70SThierry Reding 	unsigned int of_pwm_n_cells;
2126e69ab13SFlorian Vaussard 	bool can_sleep;
2130c2498f1SSascha Hauer };
2140c2498f1SSascha Hauer 
2150bcf168bSTushar Behera #if IS_ENABLED(CONFIG_PWM)
216*5ec803edSBoris Brezillon /* PWM user APIs */
217*5ec803edSBoris Brezillon struct pwm_device *pwm_request(int pwm_id, const char *label);
218*5ec803edSBoris Brezillon void pwm_free(struct pwm_device *pwm);
219*5ec803edSBoris Brezillon int pwm_apply_state(struct pwm_device *pwm, struct pwm_state *state);
220*5ec803edSBoris Brezillon int pwm_adjust_config(struct pwm_device *pwm);
221*5ec803edSBoris Brezillon 
222*5ec803edSBoris Brezillon /**
223*5ec803edSBoris Brezillon  * pwm_config() - change a PWM device configuration
224*5ec803edSBoris Brezillon  * @pwm: PWM device
225*5ec803edSBoris Brezillon  * @duty_ns: "on" time (in nanoseconds)
226*5ec803edSBoris Brezillon  * @period_ns: duration (in nanoseconds) of one cycle
227*5ec803edSBoris Brezillon  *
228*5ec803edSBoris Brezillon  * Returns: 0 on success or a negative error code on failure.
229*5ec803edSBoris Brezillon  */
230*5ec803edSBoris Brezillon static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
231*5ec803edSBoris Brezillon 			     int period_ns)
232*5ec803edSBoris Brezillon {
233*5ec803edSBoris Brezillon 	struct pwm_state state;
234*5ec803edSBoris Brezillon 
235*5ec803edSBoris Brezillon 	if (!pwm)
236*5ec803edSBoris Brezillon 		return -EINVAL;
237*5ec803edSBoris Brezillon 
238*5ec803edSBoris Brezillon 	pwm_get_state(pwm, &state);
239*5ec803edSBoris Brezillon 	if (state.duty_cycle == duty_ns && state.period == period_ns)
240*5ec803edSBoris Brezillon 		return 0;
241*5ec803edSBoris Brezillon 
242*5ec803edSBoris Brezillon 	state.duty_cycle = duty_ns;
243*5ec803edSBoris Brezillon 	state.period = period_ns;
244*5ec803edSBoris Brezillon 	return pwm_apply_state(pwm, &state);
245*5ec803edSBoris Brezillon }
246*5ec803edSBoris Brezillon 
247*5ec803edSBoris Brezillon /**
248*5ec803edSBoris Brezillon  * pwm_set_polarity() - configure the polarity of a PWM signal
249*5ec803edSBoris Brezillon  * @pwm: PWM device
250*5ec803edSBoris Brezillon  * @polarity: new polarity of the PWM signal
251*5ec803edSBoris Brezillon  *
252*5ec803edSBoris Brezillon  * Note that the polarity cannot be configured while the PWM device is
253*5ec803edSBoris Brezillon  * enabled.
254*5ec803edSBoris Brezillon  *
255*5ec803edSBoris Brezillon  * Returns: 0 on success or a negative error code on failure.
256*5ec803edSBoris Brezillon  */
257*5ec803edSBoris Brezillon static inline int pwm_set_polarity(struct pwm_device *pwm,
258*5ec803edSBoris Brezillon 				   enum pwm_polarity polarity)
259*5ec803edSBoris Brezillon {
260*5ec803edSBoris Brezillon 	struct pwm_state state;
261*5ec803edSBoris Brezillon 
262*5ec803edSBoris Brezillon 	if (!pwm)
263*5ec803edSBoris Brezillon 		return -EINVAL;
264*5ec803edSBoris Brezillon 
265*5ec803edSBoris Brezillon 	pwm_get_state(pwm, &state);
266*5ec803edSBoris Brezillon 	if (state.polarity == polarity)
267*5ec803edSBoris Brezillon 		return 0;
268*5ec803edSBoris Brezillon 
269*5ec803edSBoris Brezillon 	/*
270*5ec803edSBoris Brezillon 	 * Changing the polarity of a running PWM without adjusting the
271*5ec803edSBoris Brezillon 	 * dutycycle/period value is a bit risky (can introduce glitches).
272*5ec803edSBoris Brezillon 	 * Return -EBUSY in this case.
273*5ec803edSBoris Brezillon 	 * Note that this is allowed when using pwm_apply_state() because
274*5ec803edSBoris Brezillon 	 * the user specifies all the parameters.
275*5ec803edSBoris Brezillon 	 */
276*5ec803edSBoris Brezillon 	if (state.enabled)
277*5ec803edSBoris Brezillon 		return -EBUSY;
278*5ec803edSBoris Brezillon 
279*5ec803edSBoris Brezillon 	state.polarity = polarity;
280*5ec803edSBoris Brezillon 	return pwm_apply_state(pwm, &state);
281*5ec803edSBoris Brezillon }
282*5ec803edSBoris Brezillon 
283*5ec803edSBoris Brezillon /**
284*5ec803edSBoris Brezillon  * pwm_enable() - start a PWM output toggling
285*5ec803edSBoris Brezillon  * @pwm: PWM device
286*5ec803edSBoris Brezillon  *
287*5ec803edSBoris Brezillon  * Returns: 0 on success or a negative error code on failure.
288*5ec803edSBoris Brezillon  */
289*5ec803edSBoris Brezillon static inline int pwm_enable(struct pwm_device *pwm)
290*5ec803edSBoris Brezillon {
291*5ec803edSBoris Brezillon 	struct pwm_state state;
292*5ec803edSBoris Brezillon 
293*5ec803edSBoris Brezillon 	if (!pwm)
294*5ec803edSBoris Brezillon 		return -EINVAL;
295*5ec803edSBoris Brezillon 
296*5ec803edSBoris Brezillon 	pwm_get_state(pwm, &state);
297*5ec803edSBoris Brezillon 	if (state.enabled)
298*5ec803edSBoris Brezillon 		return 0;
299*5ec803edSBoris Brezillon 
300*5ec803edSBoris Brezillon 	state.enabled = true;
301*5ec803edSBoris Brezillon 	return pwm_apply_state(pwm, &state);
302*5ec803edSBoris Brezillon }
303*5ec803edSBoris Brezillon 
304*5ec803edSBoris Brezillon /**
305*5ec803edSBoris Brezillon  * pwm_disable() - stop a PWM output toggling
306*5ec803edSBoris Brezillon  * @pwm: PWM device
307*5ec803edSBoris Brezillon  */
308*5ec803edSBoris Brezillon static inline void pwm_disable(struct pwm_device *pwm)
309*5ec803edSBoris Brezillon {
310*5ec803edSBoris Brezillon 	struct pwm_state state;
311*5ec803edSBoris Brezillon 
312*5ec803edSBoris Brezillon 	if (!pwm)
313*5ec803edSBoris Brezillon 		return;
314*5ec803edSBoris Brezillon 
315*5ec803edSBoris Brezillon 	pwm_get_state(pwm, &state);
316*5ec803edSBoris Brezillon 	if (!state.enabled)
317*5ec803edSBoris Brezillon 		return;
318*5ec803edSBoris Brezillon 
319*5ec803edSBoris Brezillon 	state.enabled = false;
320*5ec803edSBoris Brezillon 	pwm_apply_state(pwm, &state);
321*5ec803edSBoris Brezillon }
322*5ec803edSBoris Brezillon 
323*5ec803edSBoris Brezillon 
324*5ec803edSBoris Brezillon /* PWM provider APIs */
325f051c466SThierry Reding int pwm_set_chip_data(struct pwm_device *pwm, void *data);
326f051c466SThierry Reding void *pwm_get_chip_data(struct pwm_device *pwm);
327f051c466SThierry Reding 
328b6a00faeSTim Kryger int pwmchip_add_with_polarity(struct pwm_chip *chip,
329b6a00faeSTim Kryger 			      enum pwm_polarity polarity);
3300c2498f1SSascha Hauer int pwmchip_add(struct pwm_chip *chip);
3310c2498f1SSascha Hauer int pwmchip_remove(struct pwm_chip *chip);
332f051c466SThierry Reding struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
333f051c466SThierry Reding 					 unsigned int index,
334f051c466SThierry Reding 					 const char *label);
3358138d2ddSThierry Reding 
33683af2402SPhilip, Avinash struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *pc,
33783af2402SPhilip, Avinash 		const struct of_phandle_args *args);
33883af2402SPhilip, Avinash 
339d4c0c470SPeter Ujfalusi struct pwm_device *pwm_get(struct device *dev, const char *con_id);
3408eb96127SPeter Ujfalusi struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id);
3418138d2ddSThierry Reding void pwm_put(struct pwm_device *pwm);
3428138d2ddSThierry Reding 
343d4c0c470SPeter Ujfalusi struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
344261a5eddSPeter Ujfalusi struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
345261a5eddSPeter Ujfalusi 				   const char *con_id);
3466354316dSAlexandre Courbot void devm_pwm_put(struct device *dev, struct pwm_device *pwm);
3476e69ab13SFlorian Vaussard 
3486e69ab13SFlorian Vaussard bool pwm_can_sleep(struct pwm_device *pwm);
3490bcf168bSTushar Behera #else
350*5ec803edSBoris Brezillon static inline struct pwm_device *pwm_request(int pwm_id, const char *label)
351*5ec803edSBoris Brezillon {
352*5ec803edSBoris Brezillon 	return ERR_PTR(-ENODEV);
353*5ec803edSBoris Brezillon }
354*5ec803edSBoris Brezillon 
355*5ec803edSBoris Brezillon static inline void pwm_free(struct pwm_device *pwm)
356*5ec803edSBoris Brezillon {
357*5ec803edSBoris Brezillon }
358*5ec803edSBoris Brezillon 
359*5ec803edSBoris Brezillon static inline int pwm_apply_state(struct pwm_device *pwm,
360*5ec803edSBoris Brezillon 				  const struct pwm_state *state)
361*5ec803edSBoris Brezillon {
362*5ec803edSBoris Brezillon 	return -ENOTSUPP;
363*5ec803edSBoris Brezillon }
364*5ec803edSBoris Brezillon 
365*5ec803edSBoris Brezillon static inline int pwm_adjust_config(struct pwm_device *pwm)
366*5ec803edSBoris Brezillon {
367*5ec803edSBoris Brezillon 	return -ENOTSUPP;
368*5ec803edSBoris Brezillon }
369*5ec803edSBoris Brezillon 
370*5ec803edSBoris Brezillon static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
371*5ec803edSBoris Brezillon 			     int period_ns)
372*5ec803edSBoris Brezillon {
373*5ec803edSBoris Brezillon 	return -EINVAL;
374*5ec803edSBoris Brezillon }
375*5ec803edSBoris Brezillon 
376*5ec803edSBoris Brezillon static inline int pwm_set_polarity(struct pwm_device *pwm,
377*5ec803edSBoris Brezillon 				   enum pwm_polarity polarity)
378*5ec803edSBoris Brezillon {
379*5ec803edSBoris Brezillon 	return -ENOTSUPP;
380*5ec803edSBoris Brezillon }
381*5ec803edSBoris Brezillon 
382*5ec803edSBoris Brezillon static inline int pwm_enable(struct pwm_device *pwm)
383*5ec803edSBoris Brezillon {
384*5ec803edSBoris Brezillon 	return -EINVAL;
385*5ec803edSBoris Brezillon }
386*5ec803edSBoris Brezillon 
387*5ec803edSBoris Brezillon static inline void pwm_disable(struct pwm_device *pwm)
388*5ec803edSBoris Brezillon {
389*5ec803edSBoris Brezillon }
390*5ec803edSBoris Brezillon 
3910bcf168bSTushar Behera static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data)
3920bcf168bSTushar Behera {
3930bcf168bSTushar Behera 	return -EINVAL;
3940bcf168bSTushar Behera }
3950bcf168bSTushar Behera 
3960bcf168bSTushar Behera static inline void *pwm_get_chip_data(struct pwm_device *pwm)
3970bcf168bSTushar Behera {
3980bcf168bSTushar Behera 	return NULL;
3990bcf168bSTushar Behera }
4000bcf168bSTushar Behera 
4010bcf168bSTushar Behera static inline int pwmchip_add(struct pwm_chip *chip)
4020bcf168bSTushar Behera {
4030bcf168bSTushar Behera 	return -EINVAL;
4040bcf168bSTushar Behera }
4050bcf168bSTushar Behera 
406b6a00faeSTim Kryger static inline int pwmchip_add_inversed(struct pwm_chip *chip)
407b6a00faeSTim Kryger {
408b6a00faeSTim Kryger 	return -EINVAL;
409b6a00faeSTim Kryger }
410b6a00faeSTim Kryger 
4110bcf168bSTushar Behera static inline int pwmchip_remove(struct pwm_chip *chip)
4120bcf168bSTushar Behera {
4130bcf168bSTushar Behera 	return -EINVAL;
4140bcf168bSTushar Behera }
4150bcf168bSTushar Behera 
4160bcf168bSTushar Behera static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
4170bcf168bSTushar Behera 						       unsigned int index,
4180bcf168bSTushar Behera 						       const char *label)
4190bcf168bSTushar Behera {
4200bcf168bSTushar Behera 	return ERR_PTR(-ENODEV);
4210bcf168bSTushar Behera }
4220bcf168bSTushar Behera 
4230bcf168bSTushar Behera static inline struct pwm_device *pwm_get(struct device *dev,
4240bcf168bSTushar Behera 					 const char *consumer)
4250bcf168bSTushar Behera {
4260bcf168bSTushar Behera 	return ERR_PTR(-ENODEV);
4270bcf168bSTushar Behera }
4280bcf168bSTushar Behera 
4298eb96127SPeter Ujfalusi static inline struct pwm_device *of_pwm_get(struct device_node *np,
4308eb96127SPeter Ujfalusi 					    const char *con_id)
4318eb96127SPeter Ujfalusi {
4328eb96127SPeter Ujfalusi 	return ERR_PTR(-ENODEV);
4338eb96127SPeter Ujfalusi }
4348eb96127SPeter Ujfalusi 
4350bcf168bSTushar Behera static inline void pwm_put(struct pwm_device *pwm)
4360bcf168bSTushar Behera {
4370bcf168bSTushar Behera }
4380bcf168bSTushar Behera 
4390bcf168bSTushar Behera static inline struct pwm_device *devm_pwm_get(struct device *dev,
4400bcf168bSTushar Behera 					      const char *consumer)
4410bcf168bSTushar Behera {
4420bcf168bSTushar Behera 	return ERR_PTR(-ENODEV);
4430bcf168bSTushar Behera }
4440bcf168bSTushar Behera 
445261a5eddSPeter Ujfalusi static inline struct pwm_device *devm_of_pwm_get(struct device *dev,
446261a5eddSPeter Ujfalusi 						 struct device_node *np,
447261a5eddSPeter Ujfalusi 						 const char *con_id)
448261a5eddSPeter Ujfalusi {
449261a5eddSPeter Ujfalusi 	return ERR_PTR(-ENODEV);
450261a5eddSPeter Ujfalusi }
451261a5eddSPeter Ujfalusi 
4520bcf168bSTushar Behera static inline void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
4530bcf168bSTushar Behera {
4540bcf168bSTushar Behera }
4556e69ab13SFlorian Vaussard 
4566e69ab13SFlorian Vaussard static inline bool pwm_can_sleep(struct pwm_device *pwm)
4576e69ab13SFlorian Vaussard {
4586e69ab13SFlorian Vaussard 	return false;
4596e69ab13SFlorian Vaussard }
4600bcf168bSTushar Behera #endif
4616354316dSAlexandre Courbot 
462*5ec803edSBoris Brezillon static inline void pwm_apply_args(struct pwm_device *pwm)
463*5ec803edSBoris Brezillon {
464*5ec803edSBoris Brezillon 	/*
465*5ec803edSBoris Brezillon 	 * PWM users calling pwm_apply_args() expect to have a fresh config
466*5ec803edSBoris Brezillon 	 * where the polarity and period are set according to pwm_args info.
467*5ec803edSBoris Brezillon 	 * The problem is, polarity can only be changed when the PWM is
468*5ec803edSBoris Brezillon 	 * disabled.
469*5ec803edSBoris Brezillon 	 *
470*5ec803edSBoris Brezillon 	 * PWM drivers supporting hardware readout may declare the PWM device
471*5ec803edSBoris Brezillon 	 * as enabled, and prevent polarity setting, which changes from the
472*5ec803edSBoris Brezillon 	 * existing behavior, where all PWM devices are declared as disabled
473*5ec803edSBoris Brezillon 	 * at startup (even if they are actually enabled), thus authorizing
474*5ec803edSBoris Brezillon 	 * polarity setting.
475*5ec803edSBoris Brezillon 	 *
476*5ec803edSBoris Brezillon 	 * Instead of setting ->enabled to false, we call pwm_disable()
477*5ec803edSBoris Brezillon 	 * before pwm_set_polarity() to ensure that everything is configured
478*5ec803edSBoris Brezillon 	 * as expected, and the PWM is really disabled when the user request
479*5ec803edSBoris Brezillon 	 * it.
480*5ec803edSBoris Brezillon 	 *
481*5ec803edSBoris Brezillon 	 * Note that PWM users requiring a smooth handover between the
482*5ec803edSBoris Brezillon 	 * bootloader and the kernel (like critical regulators controlled by
483*5ec803edSBoris Brezillon 	 * PWM devices) will have to switch to the atomic API and avoid calling
484*5ec803edSBoris Brezillon 	 * pwm_apply_args().
485*5ec803edSBoris Brezillon 	 */
486*5ec803edSBoris Brezillon 	pwm_disable(pwm);
487*5ec803edSBoris Brezillon 	pwm_set_polarity(pwm, pwm->args.polarity);
488*5ec803edSBoris Brezillon }
489*5ec803edSBoris Brezillon 
4908138d2ddSThierry Reding struct pwm_lookup {
4918138d2ddSThierry Reding 	struct list_head list;
4928138d2ddSThierry Reding 	const char *provider;
4938138d2ddSThierry Reding 	unsigned int index;
4948138d2ddSThierry Reding 	const char *dev_id;
4958138d2ddSThierry Reding 	const char *con_id;
4963796ce1dSAlexandre Belloni 	unsigned int period;
4973796ce1dSAlexandre Belloni 	enum pwm_polarity polarity;
4988138d2ddSThierry Reding };
4998138d2ddSThierry Reding 
50042844029SAlexandre Belloni #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
5018138d2ddSThierry Reding 	{						\
5028138d2ddSThierry Reding 		.provider = _provider,			\
5038138d2ddSThierry Reding 		.index = _index,			\
5048138d2ddSThierry Reding 		.dev_id = _dev_id,			\
5058138d2ddSThierry Reding 		.con_id = _con_id,			\
50642844029SAlexandre Belloni 		.period = _period,			\
50742844029SAlexandre Belloni 		.polarity = _polarity			\
5088138d2ddSThierry Reding 	}
5098138d2ddSThierry Reding 
5100bcf168bSTushar Behera #if IS_ENABLED(CONFIG_PWM)
5118138d2ddSThierry Reding void pwm_add_table(struct pwm_lookup *table, size_t num);
512efb0de55SShobhit Kumar void pwm_remove_table(struct pwm_lookup *table, size_t num);
5130bcf168bSTushar Behera #else
5140bcf168bSTushar Behera static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
5150bcf168bSTushar Behera {
5160bcf168bSTushar Behera }
517efb0de55SShobhit Kumar 
518efb0de55SShobhit Kumar static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
519efb0de55SShobhit Kumar {
520efb0de55SShobhit Kumar }
5210c2498f1SSascha Hauer #endif
5220c2498f1SSascha Hauer 
52376abbddeSH Hartley Sweeten #ifdef CONFIG_PWM_SYSFS
52476abbddeSH Hartley Sweeten void pwmchip_sysfs_export(struct pwm_chip *chip);
52576abbddeSH Hartley Sweeten void pwmchip_sysfs_unexport(struct pwm_chip *chip);
52676abbddeSH Hartley Sweeten #else
52776abbddeSH Hartley Sweeten static inline void pwmchip_sysfs_export(struct pwm_chip *chip)
52876abbddeSH Hartley Sweeten {
52976abbddeSH Hartley Sweeten }
53076abbddeSH Hartley Sweeten 
53176abbddeSH Hartley Sweeten static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip)
53276abbddeSH Hartley Sweeten {
53376abbddeSH Hartley Sweeten }
53476abbddeSH Hartley Sweeten #endif /* CONFIG_PWM_SYSFS */
53576abbddeSH Hartley Sweeten 
5365243ef8bSMark Vels #endif /* __LINUX_PWM_H */
537