xref: /linux-6.15/include/linux/pwm.h (revision ef2bf499)
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
1585ec803edSBoris Brezillon  * @apply: atomically apply a new PWM config. The state argument
1595ec803edSBoris Brezillon  *	   should be adjusted with the real hardware config (if the
1605ec803edSBoris Brezillon  *	   approximate the period or duty_cycle value, state should
1615ec803edSBoris 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);
1775ec803edSBoris Brezillon 	int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm,
1785ec803edSBoris 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)
2165ec803edSBoris Brezillon /* PWM user APIs */
2175ec803edSBoris Brezillon struct pwm_device *pwm_request(int pwm_id, const char *label);
2185ec803edSBoris Brezillon void pwm_free(struct pwm_device *pwm);
2195ec803edSBoris Brezillon int pwm_apply_state(struct pwm_device *pwm, struct pwm_state *state);
2205ec803edSBoris Brezillon int pwm_adjust_config(struct pwm_device *pwm);
2215ec803edSBoris Brezillon 
2225ec803edSBoris Brezillon /**
2235ec803edSBoris Brezillon  * pwm_config() - change a PWM device configuration
2245ec803edSBoris Brezillon  * @pwm: PWM device
2255ec803edSBoris Brezillon  * @duty_ns: "on" time (in nanoseconds)
2265ec803edSBoris Brezillon  * @period_ns: duration (in nanoseconds) of one cycle
2275ec803edSBoris Brezillon  *
2285ec803edSBoris Brezillon  * Returns: 0 on success or a negative error code on failure.
2295ec803edSBoris Brezillon  */
2305ec803edSBoris Brezillon static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
2315ec803edSBoris Brezillon 			     int period_ns)
2325ec803edSBoris Brezillon {
2335ec803edSBoris Brezillon 	struct pwm_state state;
2345ec803edSBoris Brezillon 
2355ec803edSBoris Brezillon 	if (!pwm)
2365ec803edSBoris Brezillon 		return -EINVAL;
2375ec803edSBoris Brezillon 
238*ef2bf499SBrian Norris 	if (duty_ns < 0 || period_ns < 0)
239*ef2bf499SBrian Norris 		return -EINVAL;
240*ef2bf499SBrian Norris 
2415ec803edSBoris Brezillon 	pwm_get_state(pwm, &state);
2425ec803edSBoris Brezillon 	if (state.duty_cycle == duty_ns && state.period == period_ns)
2435ec803edSBoris Brezillon 		return 0;
2445ec803edSBoris Brezillon 
2455ec803edSBoris Brezillon 	state.duty_cycle = duty_ns;
2465ec803edSBoris Brezillon 	state.period = period_ns;
2475ec803edSBoris Brezillon 	return pwm_apply_state(pwm, &state);
2485ec803edSBoris Brezillon }
2495ec803edSBoris Brezillon 
2505ec803edSBoris Brezillon /**
2515ec803edSBoris Brezillon  * pwm_set_polarity() - configure the polarity of a PWM signal
2525ec803edSBoris Brezillon  * @pwm: PWM device
2535ec803edSBoris Brezillon  * @polarity: new polarity of the PWM signal
2545ec803edSBoris Brezillon  *
2555ec803edSBoris Brezillon  * Note that the polarity cannot be configured while the PWM device is
2565ec803edSBoris Brezillon  * enabled.
2575ec803edSBoris Brezillon  *
2585ec803edSBoris Brezillon  * Returns: 0 on success or a negative error code on failure.
2595ec803edSBoris Brezillon  */
2605ec803edSBoris Brezillon static inline int pwm_set_polarity(struct pwm_device *pwm,
2615ec803edSBoris Brezillon 				   enum pwm_polarity polarity)
2625ec803edSBoris Brezillon {
2635ec803edSBoris Brezillon 	struct pwm_state state;
2645ec803edSBoris Brezillon 
2655ec803edSBoris Brezillon 	if (!pwm)
2665ec803edSBoris Brezillon 		return -EINVAL;
2675ec803edSBoris Brezillon 
2685ec803edSBoris Brezillon 	pwm_get_state(pwm, &state);
2695ec803edSBoris Brezillon 	if (state.polarity == polarity)
2705ec803edSBoris Brezillon 		return 0;
2715ec803edSBoris Brezillon 
2725ec803edSBoris Brezillon 	/*
2735ec803edSBoris Brezillon 	 * Changing the polarity of a running PWM without adjusting the
2745ec803edSBoris Brezillon 	 * dutycycle/period value is a bit risky (can introduce glitches).
2755ec803edSBoris Brezillon 	 * Return -EBUSY in this case.
2765ec803edSBoris Brezillon 	 * Note that this is allowed when using pwm_apply_state() because
2775ec803edSBoris Brezillon 	 * the user specifies all the parameters.
2785ec803edSBoris Brezillon 	 */
2795ec803edSBoris Brezillon 	if (state.enabled)
2805ec803edSBoris Brezillon 		return -EBUSY;
2815ec803edSBoris Brezillon 
2825ec803edSBoris Brezillon 	state.polarity = polarity;
2835ec803edSBoris Brezillon 	return pwm_apply_state(pwm, &state);
2845ec803edSBoris Brezillon }
2855ec803edSBoris Brezillon 
2865ec803edSBoris Brezillon /**
2875ec803edSBoris Brezillon  * pwm_enable() - start a PWM output toggling
2885ec803edSBoris Brezillon  * @pwm: PWM device
2895ec803edSBoris Brezillon  *
2905ec803edSBoris Brezillon  * Returns: 0 on success or a negative error code on failure.
2915ec803edSBoris Brezillon  */
2925ec803edSBoris Brezillon static inline int pwm_enable(struct pwm_device *pwm)
2935ec803edSBoris Brezillon {
2945ec803edSBoris Brezillon 	struct pwm_state state;
2955ec803edSBoris Brezillon 
2965ec803edSBoris Brezillon 	if (!pwm)
2975ec803edSBoris Brezillon 		return -EINVAL;
2985ec803edSBoris Brezillon 
2995ec803edSBoris Brezillon 	pwm_get_state(pwm, &state);
3005ec803edSBoris Brezillon 	if (state.enabled)
3015ec803edSBoris Brezillon 		return 0;
3025ec803edSBoris Brezillon 
3035ec803edSBoris Brezillon 	state.enabled = true;
3045ec803edSBoris Brezillon 	return pwm_apply_state(pwm, &state);
3055ec803edSBoris Brezillon }
3065ec803edSBoris Brezillon 
3075ec803edSBoris Brezillon /**
3085ec803edSBoris Brezillon  * pwm_disable() - stop a PWM output toggling
3095ec803edSBoris Brezillon  * @pwm: PWM device
3105ec803edSBoris Brezillon  */
3115ec803edSBoris Brezillon static inline void pwm_disable(struct pwm_device *pwm)
3125ec803edSBoris Brezillon {
3135ec803edSBoris Brezillon 	struct pwm_state state;
3145ec803edSBoris Brezillon 
3155ec803edSBoris Brezillon 	if (!pwm)
3165ec803edSBoris Brezillon 		return;
3175ec803edSBoris Brezillon 
3185ec803edSBoris Brezillon 	pwm_get_state(pwm, &state);
3195ec803edSBoris Brezillon 	if (!state.enabled)
3205ec803edSBoris Brezillon 		return;
3215ec803edSBoris Brezillon 
3225ec803edSBoris Brezillon 	state.enabled = false;
3235ec803edSBoris Brezillon 	pwm_apply_state(pwm, &state);
3245ec803edSBoris Brezillon }
3255ec803edSBoris Brezillon 
3265ec803edSBoris Brezillon 
3275ec803edSBoris Brezillon /* PWM provider APIs */
328f051c466SThierry Reding int pwm_set_chip_data(struct pwm_device *pwm, void *data);
329f051c466SThierry Reding void *pwm_get_chip_data(struct pwm_device *pwm);
330f051c466SThierry Reding 
331b6a00faeSTim Kryger int pwmchip_add_with_polarity(struct pwm_chip *chip,
332b6a00faeSTim Kryger 			      enum pwm_polarity polarity);
3330c2498f1SSascha Hauer int pwmchip_add(struct pwm_chip *chip);
3340c2498f1SSascha Hauer int pwmchip_remove(struct pwm_chip *chip);
335f051c466SThierry Reding struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
336f051c466SThierry Reding 					 unsigned int index,
337f051c466SThierry Reding 					 const char *label);
3388138d2ddSThierry Reding 
33983af2402SPhilip, Avinash struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *pc,
34083af2402SPhilip, Avinash 		const struct of_phandle_args *args);
34183af2402SPhilip, Avinash 
342d4c0c470SPeter Ujfalusi struct pwm_device *pwm_get(struct device *dev, const char *con_id);
3438eb96127SPeter Ujfalusi struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id);
3448138d2ddSThierry Reding void pwm_put(struct pwm_device *pwm);
3458138d2ddSThierry Reding 
346d4c0c470SPeter Ujfalusi struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
347261a5eddSPeter Ujfalusi struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
348261a5eddSPeter Ujfalusi 				   const char *con_id);
3496354316dSAlexandre Courbot void devm_pwm_put(struct device *dev, struct pwm_device *pwm);
3506e69ab13SFlorian Vaussard 
3516e69ab13SFlorian Vaussard bool pwm_can_sleep(struct pwm_device *pwm);
3520bcf168bSTushar Behera #else
3535ec803edSBoris Brezillon static inline struct pwm_device *pwm_request(int pwm_id, const char *label)
3545ec803edSBoris Brezillon {
3555ec803edSBoris Brezillon 	return ERR_PTR(-ENODEV);
3565ec803edSBoris Brezillon }
3575ec803edSBoris Brezillon 
3585ec803edSBoris Brezillon static inline void pwm_free(struct pwm_device *pwm)
3595ec803edSBoris Brezillon {
3605ec803edSBoris Brezillon }
3615ec803edSBoris Brezillon 
3625ec803edSBoris Brezillon static inline int pwm_apply_state(struct pwm_device *pwm,
3635ec803edSBoris Brezillon 				  const struct pwm_state *state)
3645ec803edSBoris Brezillon {
3655ec803edSBoris Brezillon 	return -ENOTSUPP;
3665ec803edSBoris Brezillon }
3675ec803edSBoris Brezillon 
3685ec803edSBoris Brezillon static inline int pwm_adjust_config(struct pwm_device *pwm)
3695ec803edSBoris Brezillon {
3705ec803edSBoris Brezillon 	return -ENOTSUPP;
3715ec803edSBoris Brezillon }
3725ec803edSBoris Brezillon 
3735ec803edSBoris Brezillon static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
3745ec803edSBoris Brezillon 			     int period_ns)
3755ec803edSBoris Brezillon {
3765ec803edSBoris Brezillon 	return -EINVAL;
3775ec803edSBoris Brezillon }
3785ec803edSBoris Brezillon 
3795ec803edSBoris Brezillon static inline int pwm_set_polarity(struct pwm_device *pwm,
3805ec803edSBoris Brezillon 				   enum pwm_polarity polarity)
3815ec803edSBoris Brezillon {
3825ec803edSBoris Brezillon 	return -ENOTSUPP;
3835ec803edSBoris Brezillon }
3845ec803edSBoris Brezillon 
3855ec803edSBoris Brezillon static inline int pwm_enable(struct pwm_device *pwm)
3865ec803edSBoris Brezillon {
3875ec803edSBoris Brezillon 	return -EINVAL;
3885ec803edSBoris Brezillon }
3895ec803edSBoris Brezillon 
3905ec803edSBoris Brezillon static inline void pwm_disable(struct pwm_device *pwm)
3915ec803edSBoris Brezillon {
3925ec803edSBoris Brezillon }
3935ec803edSBoris Brezillon 
3940bcf168bSTushar Behera static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data)
3950bcf168bSTushar Behera {
3960bcf168bSTushar Behera 	return -EINVAL;
3970bcf168bSTushar Behera }
3980bcf168bSTushar Behera 
3990bcf168bSTushar Behera static inline void *pwm_get_chip_data(struct pwm_device *pwm)
4000bcf168bSTushar Behera {
4010bcf168bSTushar Behera 	return NULL;
4020bcf168bSTushar Behera }
4030bcf168bSTushar Behera 
4040bcf168bSTushar Behera static inline int pwmchip_add(struct pwm_chip *chip)
4050bcf168bSTushar Behera {
4060bcf168bSTushar Behera 	return -EINVAL;
4070bcf168bSTushar Behera }
4080bcf168bSTushar Behera 
409b6a00faeSTim Kryger static inline int pwmchip_add_inversed(struct pwm_chip *chip)
410b6a00faeSTim Kryger {
411b6a00faeSTim Kryger 	return -EINVAL;
412b6a00faeSTim Kryger }
413b6a00faeSTim Kryger 
4140bcf168bSTushar Behera static inline int pwmchip_remove(struct pwm_chip *chip)
4150bcf168bSTushar Behera {
4160bcf168bSTushar Behera 	return -EINVAL;
4170bcf168bSTushar Behera }
4180bcf168bSTushar Behera 
4190bcf168bSTushar Behera static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
4200bcf168bSTushar Behera 						       unsigned int index,
4210bcf168bSTushar Behera 						       const char *label)
4220bcf168bSTushar Behera {
4230bcf168bSTushar Behera 	return ERR_PTR(-ENODEV);
4240bcf168bSTushar Behera }
4250bcf168bSTushar Behera 
4260bcf168bSTushar Behera static inline struct pwm_device *pwm_get(struct device *dev,
4270bcf168bSTushar Behera 					 const char *consumer)
4280bcf168bSTushar Behera {
4290bcf168bSTushar Behera 	return ERR_PTR(-ENODEV);
4300bcf168bSTushar Behera }
4310bcf168bSTushar Behera 
4328eb96127SPeter Ujfalusi static inline struct pwm_device *of_pwm_get(struct device_node *np,
4338eb96127SPeter Ujfalusi 					    const char *con_id)
4348eb96127SPeter Ujfalusi {
4358eb96127SPeter Ujfalusi 	return ERR_PTR(-ENODEV);
4368eb96127SPeter Ujfalusi }
4378eb96127SPeter Ujfalusi 
4380bcf168bSTushar Behera static inline void pwm_put(struct pwm_device *pwm)
4390bcf168bSTushar Behera {
4400bcf168bSTushar Behera }
4410bcf168bSTushar Behera 
4420bcf168bSTushar Behera static inline struct pwm_device *devm_pwm_get(struct device *dev,
4430bcf168bSTushar Behera 					      const char *consumer)
4440bcf168bSTushar Behera {
4450bcf168bSTushar Behera 	return ERR_PTR(-ENODEV);
4460bcf168bSTushar Behera }
4470bcf168bSTushar Behera 
448261a5eddSPeter Ujfalusi static inline struct pwm_device *devm_of_pwm_get(struct device *dev,
449261a5eddSPeter Ujfalusi 						 struct device_node *np,
450261a5eddSPeter Ujfalusi 						 const char *con_id)
451261a5eddSPeter Ujfalusi {
452261a5eddSPeter Ujfalusi 	return ERR_PTR(-ENODEV);
453261a5eddSPeter Ujfalusi }
454261a5eddSPeter Ujfalusi 
4550bcf168bSTushar Behera static inline void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
4560bcf168bSTushar Behera {
4570bcf168bSTushar Behera }
4586e69ab13SFlorian Vaussard 
4596e69ab13SFlorian Vaussard static inline bool pwm_can_sleep(struct pwm_device *pwm)
4606e69ab13SFlorian Vaussard {
4616e69ab13SFlorian Vaussard 	return false;
4626e69ab13SFlorian Vaussard }
4630bcf168bSTushar Behera #endif
4646354316dSAlexandre Courbot 
4655ec803edSBoris Brezillon static inline void pwm_apply_args(struct pwm_device *pwm)
4665ec803edSBoris Brezillon {
4675ec803edSBoris Brezillon 	/*
4685ec803edSBoris Brezillon 	 * PWM users calling pwm_apply_args() expect to have a fresh config
4695ec803edSBoris Brezillon 	 * where the polarity and period are set according to pwm_args info.
4705ec803edSBoris Brezillon 	 * The problem is, polarity can only be changed when the PWM is
4715ec803edSBoris Brezillon 	 * disabled.
4725ec803edSBoris Brezillon 	 *
4735ec803edSBoris Brezillon 	 * PWM drivers supporting hardware readout may declare the PWM device
4745ec803edSBoris Brezillon 	 * as enabled, and prevent polarity setting, which changes from the
4755ec803edSBoris Brezillon 	 * existing behavior, where all PWM devices are declared as disabled
4765ec803edSBoris Brezillon 	 * at startup (even if they are actually enabled), thus authorizing
4775ec803edSBoris Brezillon 	 * polarity setting.
4785ec803edSBoris Brezillon 	 *
4795ec803edSBoris Brezillon 	 * Instead of setting ->enabled to false, we call pwm_disable()
4805ec803edSBoris Brezillon 	 * before pwm_set_polarity() to ensure that everything is configured
4815ec803edSBoris Brezillon 	 * as expected, and the PWM is really disabled when the user request
4825ec803edSBoris Brezillon 	 * it.
4835ec803edSBoris Brezillon 	 *
4845ec803edSBoris Brezillon 	 * Note that PWM users requiring a smooth handover between the
4855ec803edSBoris Brezillon 	 * bootloader and the kernel (like critical regulators controlled by
4865ec803edSBoris Brezillon 	 * PWM devices) will have to switch to the atomic API and avoid calling
4875ec803edSBoris Brezillon 	 * pwm_apply_args().
4885ec803edSBoris Brezillon 	 */
4895ec803edSBoris Brezillon 	pwm_disable(pwm);
4905ec803edSBoris Brezillon 	pwm_set_polarity(pwm, pwm->args.polarity);
4915ec803edSBoris Brezillon }
4925ec803edSBoris Brezillon 
4938138d2ddSThierry Reding struct pwm_lookup {
4948138d2ddSThierry Reding 	struct list_head list;
4958138d2ddSThierry Reding 	const char *provider;
4968138d2ddSThierry Reding 	unsigned int index;
4978138d2ddSThierry Reding 	const char *dev_id;
4988138d2ddSThierry Reding 	const char *con_id;
4993796ce1dSAlexandre Belloni 	unsigned int period;
5003796ce1dSAlexandre Belloni 	enum pwm_polarity polarity;
5018138d2ddSThierry Reding };
5028138d2ddSThierry Reding 
50342844029SAlexandre Belloni #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
5048138d2ddSThierry Reding 	{						\
5058138d2ddSThierry Reding 		.provider = _provider,			\
5068138d2ddSThierry Reding 		.index = _index,			\
5078138d2ddSThierry Reding 		.dev_id = _dev_id,			\
5088138d2ddSThierry Reding 		.con_id = _con_id,			\
50942844029SAlexandre Belloni 		.period = _period,			\
51042844029SAlexandre Belloni 		.polarity = _polarity			\
5118138d2ddSThierry Reding 	}
5128138d2ddSThierry Reding 
5130bcf168bSTushar Behera #if IS_ENABLED(CONFIG_PWM)
5148138d2ddSThierry Reding void pwm_add_table(struct pwm_lookup *table, size_t num);
515efb0de55SShobhit Kumar void pwm_remove_table(struct pwm_lookup *table, size_t num);
5160bcf168bSTushar Behera #else
5170bcf168bSTushar Behera static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
5180bcf168bSTushar Behera {
5190bcf168bSTushar Behera }
520efb0de55SShobhit Kumar 
521efb0de55SShobhit Kumar static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
522efb0de55SShobhit Kumar {
523efb0de55SShobhit Kumar }
5240c2498f1SSascha Hauer #endif
5250c2498f1SSascha Hauer 
52676abbddeSH Hartley Sweeten #ifdef CONFIG_PWM_SYSFS
52776abbddeSH Hartley Sweeten void pwmchip_sysfs_export(struct pwm_chip *chip);
52876abbddeSH Hartley Sweeten void pwmchip_sysfs_unexport(struct pwm_chip *chip);
52976abbddeSH Hartley Sweeten #else
53076abbddeSH Hartley Sweeten static inline void pwmchip_sysfs_export(struct pwm_chip *chip)
53176abbddeSH Hartley Sweeten {
53276abbddeSH Hartley Sweeten }
53376abbddeSH Hartley Sweeten 
53476abbddeSH Hartley Sweeten static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip)
53576abbddeSH Hartley Sweeten {
53676abbddeSH Hartley Sweeten }
53776abbddeSH Hartley Sweeten #endif /* CONFIG_PWM_SYSFS */
53876abbddeSH Hartley Sweeten 
5395243ef8bSMark Vels #endif /* __LINUX_PWM_H */
540