xref: /linux-6.15/include/linux/pwm.h (revision 3a3d1a4e)
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 
8*3a3d1a4eSLee Jones struct pwm_capture;
962099abfSThierry Reding struct seq_file;
10*3a3d1a4eSLee Jones 
110c2498f1SSascha Hauer struct pwm_chip;
120c2498f1SSascha Hauer 
130aa0869cSPhilip, Avinash /**
140aa0869cSPhilip, Avinash  * enum pwm_polarity - polarity of a PWM signal
150aa0869cSPhilip, Avinash  * @PWM_POLARITY_NORMAL: a high signal for the duration of the duty-
160aa0869cSPhilip, Avinash  * cycle, followed by a low signal for the remainder of the pulse
170aa0869cSPhilip, Avinash  * period
180aa0869cSPhilip, Avinash  * @PWM_POLARITY_INVERSED: a low signal for the duration of the duty-
190aa0869cSPhilip, Avinash  * cycle, followed by a high signal for the remainder of the pulse
200aa0869cSPhilip, Avinash  * period
210aa0869cSPhilip, Avinash  */
220aa0869cSPhilip, Avinash enum pwm_polarity {
230aa0869cSPhilip, Avinash 	PWM_POLARITY_NORMAL,
240aa0869cSPhilip, Avinash 	PWM_POLARITY_INVERSED,
250aa0869cSPhilip, Avinash };
260aa0869cSPhilip, Avinash 
27e39c0df1SBoris Brezillon /**
28e39c0df1SBoris Brezillon  * struct pwm_args - board-dependent PWM arguments
29e39c0df1SBoris Brezillon  * @period: reference period
30e39c0df1SBoris Brezillon  * @polarity: reference polarity
31e39c0df1SBoris Brezillon  *
32e39c0df1SBoris Brezillon  * This structure describes board-dependent arguments attached to a PWM
33e39c0df1SBoris Brezillon  * device. These arguments are usually retrieved from the PWM lookup table or
34e39c0df1SBoris Brezillon  * device tree.
35e39c0df1SBoris Brezillon  *
36e39c0df1SBoris Brezillon  * Do not confuse this with the PWM state: PWM arguments represent the initial
37e39c0df1SBoris Brezillon  * configuration that users want to use on this PWM device rather than the
38e39c0df1SBoris Brezillon  * current PWM hardware state.
39e39c0df1SBoris Brezillon  */
40e39c0df1SBoris Brezillon struct pwm_args {
41e39c0df1SBoris Brezillon 	unsigned int period;
42e39c0df1SBoris Brezillon 	enum pwm_polarity polarity;
43e39c0df1SBoris Brezillon };
44e39c0df1SBoris Brezillon 
45f051c466SThierry Reding enum {
46f051c466SThierry Reding 	PWMF_REQUESTED = 1 << 0,
4709a7e4a3SBoris Brezillon 	PWMF_EXPORTED = 1 << 1,
48f051c466SThierry Reding };
49f051c466SThierry Reding 
5043a276b0SBoris Brezillon /*
5143a276b0SBoris Brezillon  * struct pwm_state - state of a PWM channel
5243a276b0SBoris Brezillon  * @period: PWM period (in nanoseconds)
5343a276b0SBoris Brezillon  * @duty_cycle: PWM duty cycle (in nanoseconds)
5443a276b0SBoris Brezillon  * @polarity: PWM polarity
5509a7e4a3SBoris Brezillon  * @enabled: PWM enabled status
5643a276b0SBoris Brezillon  */
5743a276b0SBoris Brezillon struct pwm_state {
5843a276b0SBoris Brezillon 	unsigned int period;
5943a276b0SBoris Brezillon 	unsigned int duty_cycle;
6043a276b0SBoris Brezillon 	enum pwm_polarity polarity;
6109a7e4a3SBoris Brezillon 	bool enabled;
6243a276b0SBoris Brezillon };
6343a276b0SBoris Brezillon 
6404883802SThierry Reding /**
6504883802SThierry Reding  * struct pwm_device - PWM channel object
6604883802SThierry Reding  * @label: name of the PWM device
6704883802SThierry Reding  * @flags: flags associated with the PWM device
6804883802SThierry Reding  * @hwpwm: per-chip relative index of the PWM device
6904883802SThierry Reding  * @pwm: global index of the PWM device
7004883802SThierry Reding  * @chip: PWM chip providing this PWM device
7104883802SThierry Reding  * @chip_data: chip-private data associated with the PWM device
72e39c0df1SBoris Brezillon  * @args: PWM arguments
7343a276b0SBoris Brezillon  * @state: curent PWM channel state
7404883802SThierry Reding  */
75f051c466SThierry Reding struct pwm_device {
76f051c466SThierry Reding 	const char *label;
77f051c466SThierry Reding 	unsigned long flags;
78f051c466SThierry Reding 	unsigned int hwpwm;
79f051c466SThierry Reding 	unsigned int pwm;
80f051c466SThierry Reding 	struct pwm_chip *chip;
81f051c466SThierry Reding 	void *chip_data;
82f051c466SThierry Reding 
83e39c0df1SBoris Brezillon 	struct pwm_args args;
8443a276b0SBoris Brezillon 	struct pwm_state state;
85f051c466SThierry Reding };
86f051c466SThierry Reding 
8743a276b0SBoris Brezillon /**
8843a276b0SBoris Brezillon  * pwm_get_state() - retrieve the current PWM state
8943a276b0SBoris Brezillon  * @pwm: PWM device
9043a276b0SBoris Brezillon  * @state: state to fill with the current PWM state
9143a276b0SBoris Brezillon  */
9243a276b0SBoris Brezillon static inline void pwm_get_state(const struct pwm_device *pwm,
9343a276b0SBoris Brezillon 				 struct pwm_state *state)
9443a276b0SBoris Brezillon {
9543a276b0SBoris Brezillon 	*state = pwm->state;
9643a276b0SBoris Brezillon }
9743a276b0SBoris Brezillon 
985c31252cSBoris Brezillon static inline bool pwm_is_enabled(const struct pwm_device *pwm)
995c31252cSBoris Brezillon {
10009a7e4a3SBoris Brezillon 	struct pwm_state state;
10109a7e4a3SBoris Brezillon 
10209a7e4a3SBoris Brezillon 	pwm_get_state(pwm, &state);
10309a7e4a3SBoris Brezillon 
10409a7e4a3SBoris Brezillon 	return state.enabled;
1055c31252cSBoris Brezillon }
1065c31252cSBoris Brezillon 
107f051c466SThierry Reding static inline void pwm_set_period(struct pwm_device *pwm, unsigned int period)
108f051c466SThierry Reding {
109f051c466SThierry Reding 	if (pwm)
11043a276b0SBoris Brezillon 		pwm->state.period = period;
111f051c466SThierry Reding }
112f051c466SThierry Reding 
113a1cf4217SBoris Brezillon static inline unsigned int pwm_get_period(const struct pwm_device *pwm)
114f051c466SThierry Reding {
11543a276b0SBoris Brezillon 	struct pwm_state state;
11643a276b0SBoris Brezillon 
11743a276b0SBoris Brezillon 	pwm_get_state(pwm, &state);
11843a276b0SBoris Brezillon 
11943a276b0SBoris Brezillon 	return state.period;
120f051c466SThierry Reding }
121f051c466SThierry Reding 
12276abbddeSH Hartley Sweeten static inline void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty)
12376abbddeSH Hartley Sweeten {
12476abbddeSH Hartley Sweeten 	if (pwm)
12543a276b0SBoris Brezillon 		pwm->state.duty_cycle = duty;
12676abbddeSH Hartley Sweeten }
12776abbddeSH Hartley Sweeten 
128a1cf4217SBoris Brezillon static inline unsigned int pwm_get_duty_cycle(const struct pwm_device *pwm)
12976abbddeSH Hartley Sweeten {
13043a276b0SBoris Brezillon 	struct pwm_state state;
13143a276b0SBoris Brezillon 
13243a276b0SBoris Brezillon 	pwm_get_state(pwm, &state);
13343a276b0SBoris Brezillon 
13443a276b0SBoris Brezillon 	return state.duty_cycle;
13576abbddeSH Hartley Sweeten }
13676abbddeSH Hartley Sweeten 
137011e7631SBoris Brezillon static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm)
138011e7631SBoris Brezillon {
13943a276b0SBoris Brezillon 	struct pwm_state state;
14043a276b0SBoris Brezillon 
14143a276b0SBoris Brezillon 	pwm_get_state(pwm, &state);
14243a276b0SBoris Brezillon 
14343a276b0SBoris Brezillon 	return state.polarity;
144011e7631SBoris Brezillon }
145011e7631SBoris Brezillon 
146e39c0df1SBoris Brezillon static inline void pwm_get_args(const struct pwm_device *pwm,
147e39c0df1SBoris Brezillon 				struct pwm_args *args)
148e39c0df1SBoris Brezillon {
149e39c0df1SBoris Brezillon 	*args = pwm->args;
150e39c0df1SBoris Brezillon }
151e39c0df1SBoris Brezillon 
1520c2498f1SSascha Hauer /**
1530c2498f1SSascha Hauer  * struct pwm_ops - PWM controller operations
1540c2498f1SSascha Hauer  * @request: optional hook for requesting a PWM
1550c2498f1SSascha Hauer  * @free: optional hook for freeing a PWM
1560c2498f1SSascha Hauer  * @config: configure duty cycles and period length for this PWM
1570aa0869cSPhilip, Avinash  * @set_polarity: configure the polarity of this PWM
158*3a3d1a4eSLee Jones  * @capture: capture and report PWM signal
1590c2498f1SSascha Hauer  * @enable: enable PWM output toggling
1600c2498f1SSascha Hauer  * @disable: disable PWM output toggling
1615ec803edSBoris Brezillon  * @apply: atomically apply a new PWM config. The state argument
1625ec803edSBoris Brezillon  *	   should be adjusted with the real hardware config (if the
1635ec803edSBoris Brezillon  *	   approximate the period or duty_cycle value, state should
1645ec803edSBoris Brezillon  *	   reflect it)
16515fa8a43SBoris Brezillon  * @get_state: get the current PWM state. This function is only
16615fa8a43SBoris Brezillon  *	       called once per PWM device when the PWM chip is
16715fa8a43SBoris Brezillon  *	       registered.
16862099abfSThierry Reding  * @dbg_show: optional routine to show contents in debugfs
1690c2498f1SSascha Hauer  * @owner: helps prevent removal of modules exporting active PWMs
1700c2498f1SSascha Hauer  */
1710c2498f1SSascha Hauer struct pwm_ops {
1726bc7064aSThierry Reding 	int (*request)(struct pwm_chip *chip, struct pwm_device *pwm);
1736bc7064aSThierry Reding 	void (*free)(struct pwm_chip *chip, struct pwm_device *pwm);
1746bc7064aSThierry Reding 	int (*config)(struct pwm_chip *chip, struct pwm_device *pwm,
175f051c466SThierry Reding 		      int duty_ns, int period_ns);
1766bc7064aSThierry Reding 	int (*set_polarity)(struct pwm_chip *chip, struct pwm_device *pwm,
1770aa0869cSPhilip, Avinash 			    enum pwm_polarity polarity);
178*3a3d1a4eSLee Jones 	int (*capture)(struct pwm_chip *chip, struct pwm_device *pwm,
179*3a3d1a4eSLee Jones 		       struct pwm_capture *result, unsigned long timeout);
1806bc7064aSThierry Reding 	int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm);
1816bc7064aSThierry Reding 	void (*disable)(struct pwm_chip *chip, struct pwm_device *pwm);
1825ec803edSBoris Brezillon 	int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm,
1835ec803edSBoris Brezillon 		     struct pwm_state *state);
18415fa8a43SBoris Brezillon 	void (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,
18515fa8a43SBoris Brezillon 			  struct pwm_state *state);
18662099abfSThierry Reding #ifdef CONFIG_DEBUG_FS
1876bc7064aSThierry Reding 	void (*dbg_show)(struct pwm_chip *chip, struct seq_file *s);
18862099abfSThierry Reding #endif
1890c2498f1SSascha Hauer 	struct module *owner;
1900c2498f1SSascha Hauer };
1910c2498f1SSascha Hauer 
1920c2498f1SSascha Hauer /**
193f051c466SThierry Reding  * struct pwm_chip - abstract a PWM controller
194f051c466SThierry Reding  * @dev: device providing the PWMs
195f051c466SThierry Reding  * @list: list node for internal use
196f051c466SThierry Reding  * @ops: callbacks for this PWM controller
197f051c466SThierry Reding  * @base: number of first PWM controlled by this chip
198f051c466SThierry Reding  * @npwm: number of PWMs controlled by this chip
199f051c466SThierry Reding  * @pwms: array of PWM devices allocated by the framework
20004883802SThierry Reding  * @of_xlate: request a PWM device given a device tree PWM specifier
20104883802SThierry Reding  * @of_pwm_n_cells: number of cells expected in the device tree PWM specifier
2026e69ab13SFlorian Vaussard  * @can_sleep: must be true if the .config(), .enable() or .disable()
2036e69ab13SFlorian Vaussard  *             operations may sleep
2040c2498f1SSascha Hauer  */
2050c2498f1SSascha Hauer struct pwm_chip {
206f051c466SThierry Reding 	struct device *dev;
207f051c466SThierry Reding 	struct list_head list;
208f051c466SThierry Reding 	const struct pwm_ops *ops;
209f051c466SThierry Reding 	int base;
210f051c466SThierry Reding 	unsigned int npwm;
211f051c466SThierry Reding 
212f051c466SThierry Reding 	struct pwm_device *pwms;
2137299ab70SThierry Reding 
2147299ab70SThierry Reding 	struct pwm_device * (*of_xlate)(struct pwm_chip *pc,
2157299ab70SThierry Reding 					const struct of_phandle_args *args);
2167299ab70SThierry Reding 	unsigned int of_pwm_n_cells;
2176e69ab13SFlorian Vaussard 	bool can_sleep;
2180c2498f1SSascha Hauer };
2190c2498f1SSascha Hauer 
220*3a3d1a4eSLee Jones /**
221*3a3d1a4eSLee Jones  * struct pwm_capture - PWM capture data
222*3a3d1a4eSLee Jones  * @period: period of the PWM signal (in nanoseconds)
223*3a3d1a4eSLee Jones  * @duty_cycle: duty cycle of the PWM signal (in nanoseconds)
224*3a3d1a4eSLee Jones  */
225*3a3d1a4eSLee Jones struct pwm_capture {
226*3a3d1a4eSLee Jones 	unsigned int period;
227*3a3d1a4eSLee Jones 	unsigned int duty_cycle;
228*3a3d1a4eSLee Jones };
229*3a3d1a4eSLee Jones 
2300bcf168bSTushar Behera #if IS_ENABLED(CONFIG_PWM)
2315ec803edSBoris Brezillon /* PWM user APIs */
2325ec803edSBoris Brezillon struct pwm_device *pwm_request(int pwm_id, const char *label);
2335ec803edSBoris Brezillon void pwm_free(struct pwm_device *pwm);
2345ec803edSBoris Brezillon int pwm_apply_state(struct pwm_device *pwm, struct pwm_state *state);
2355ec803edSBoris Brezillon int pwm_adjust_config(struct pwm_device *pwm);
2365ec803edSBoris Brezillon 
2375ec803edSBoris Brezillon /**
2385ec803edSBoris Brezillon  * pwm_config() - change a PWM device configuration
2395ec803edSBoris Brezillon  * @pwm: PWM device
2405ec803edSBoris Brezillon  * @duty_ns: "on" time (in nanoseconds)
2415ec803edSBoris Brezillon  * @period_ns: duration (in nanoseconds) of one cycle
2425ec803edSBoris Brezillon  *
2435ec803edSBoris Brezillon  * Returns: 0 on success or a negative error code on failure.
2445ec803edSBoris Brezillon  */
2455ec803edSBoris Brezillon static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
2465ec803edSBoris Brezillon 			     int period_ns)
2475ec803edSBoris Brezillon {
2485ec803edSBoris Brezillon 	struct pwm_state state;
2495ec803edSBoris Brezillon 
2505ec803edSBoris Brezillon 	if (!pwm)
2515ec803edSBoris Brezillon 		return -EINVAL;
2525ec803edSBoris Brezillon 
2535ec803edSBoris Brezillon 	pwm_get_state(pwm, &state);
2545ec803edSBoris Brezillon 	if (state.duty_cycle == duty_ns && state.period == period_ns)
2555ec803edSBoris Brezillon 		return 0;
2565ec803edSBoris Brezillon 
2575ec803edSBoris Brezillon 	state.duty_cycle = duty_ns;
2585ec803edSBoris Brezillon 	state.period = period_ns;
2595ec803edSBoris Brezillon 	return pwm_apply_state(pwm, &state);
2605ec803edSBoris Brezillon }
2615ec803edSBoris Brezillon 
2625ec803edSBoris Brezillon /**
2635ec803edSBoris Brezillon  * pwm_set_polarity() - configure the polarity of a PWM signal
2645ec803edSBoris Brezillon  * @pwm: PWM device
2655ec803edSBoris Brezillon  * @polarity: new polarity of the PWM signal
2665ec803edSBoris Brezillon  *
2675ec803edSBoris Brezillon  * Note that the polarity cannot be configured while the PWM device is
2685ec803edSBoris Brezillon  * enabled.
2695ec803edSBoris Brezillon  *
2705ec803edSBoris Brezillon  * Returns: 0 on success or a negative error code on failure.
2715ec803edSBoris Brezillon  */
2725ec803edSBoris Brezillon static inline int pwm_set_polarity(struct pwm_device *pwm,
2735ec803edSBoris Brezillon 				   enum pwm_polarity polarity)
2745ec803edSBoris Brezillon {
2755ec803edSBoris Brezillon 	struct pwm_state state;
2765ec803edSBoris Brezillon 
2775ec803edSBoris Brezillon 	if (!pwm)
2785ec803edSBoris Brezillon 		return -EINVAL;
2795ec803edSBoris Brezillon 
2805ec803edSBoris Brezillon 	pwm_get_state(pwm, &state);
2815ec803edSBoris Brezillon 	if (state.polarity == polarity)
2825ec803edSBoris Brezillon 		return 0;
2835ec803edSBoris Brezillon 
2845ec803edSBoris Brezillon 	/*
2855ec803edSBoris Brezillon 	 * Changing the polarity of a running PWM without adjusting the
2865ec803edSBoris Brezillon 	 * dutycycle/period value is a bit risky (can introduce glitches).
2875ec803edSBoris Brezillon 	 * Return -EBUSY in this case.
2885ec803edSBoris Brezillon 	 * Note that this is allowed when using pwm_apply_state() because
2895ec803edSBoris Brezillon 	 * the user specifies all the parameters.
2905ec803edSBoris Brezillon 	 */
2915ec803edSBoris Brezillon 	if (state.enabled)
2925ec803edSBoris Brezillon 		return -EBUSY;
2935ec803edSBoris Brezillon 
2945ec803edSBoris Brezillon 	state.polarity = polarity;
2955ec803edSBoris Brezillon 	return pwm_apply_state(pwm, &state);
2965ec803edSBoris Brezillon }
2975ec803edSBoris Brezillon 
2985ec803edSBoris Brezillon /**
2995ec803edSBoris Brezillon  * pwm_enable() - start a PWM output toggling
3005ec803edSBoris Brezillon  * @pwm: PWM device
3015ec803edSBoris Brezillon  *
3025ec803edSBoris Brezillon  * Returns: 0 on success or a negative error code on failure.
3035ec803edSBoris Brezillon  */
3045ec803edSBoris Brezillon static inline int pwm_enable(struct pwm_device *pwm)
3055ec803edSBoris Brezillon {
3065ec803edSBoris Brezillon 	struct pwm_state state;
3075ec803edSBoris Brezillon 
3085ec803edSBoris Brezillon 	if (!pwm)
3095ec803edSBoris Brezillon 		return -EINVAL;
3105ec803edSBoris Brezillon 
3115ec803edSBoris Brezillon 	pwm_get_state(pwm, &state);
3125ec803edSBoris Brezillon 	if (state.enabled)
3135ec803edSBoris Brezillon 		return 0;
3145ec803edSBoris Brezillon 
3155ec803edSBoris Brezillon 	state.enabled = true;
3165ec803edSBoris Brezillon 	return pwm_apply_state(pwm, &state);
3175ec803edSBoris Brezillon }
3185ec803edSBoris Brezillon 
3195ec803edSBoris Brezillon /**
3205ec803edSBoris Brezillon  * pwm_disable() - stop a PWM output toggling
3215ec803edSBoris Brezillon  * @pwm: PWM device
3225ec803edSBoris Brezillon  */
3235ec803edSBoris Brezillon static inline void pwm_disable(struct pwm_device *pwm)
3245ec803edSBoris Brezillon {
3255ec803edSBoris Brezillon 	struct pwm_state state;
3265ec803edSBoris Brezillon 
3275ec803edSBoris Brezillon 	if (!pwm)
3285ec803edSBoris Brezillon 		return;
3295ec803edSBoris Brezillon 
3305ec803edSBoris Brezillon 	pwm_get_state(pwm, &state);
3315ec803edSBoris Brezillon 	if (!state.enabled)
3325ec803edSBoris Brezillon 		return;
3335ec803edSBoris Brezillon 
3345ec803edSBoris Brezillon 	state.enabled = false;
3355ec803edSBoris Brezillon 	pwm_apply_state(pwm, &state);
3365ec803edSBoris Brezillon }
3375ec803edSBoris Brezillon 
3385ec803edSBoris Brezillon 
3395ec803edSBoris Brezillon /* PWM provider APIs */
340*3a3d1a4eSLee Jones int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
341*3a3d1a4eSLee Jones 		unsigned long timeout);
342f051c466SThierry Reding int pwm_set_chip_data(struct pwm_device *pwm, void *data);
343f051c466SThierry Reding void *pwm_get_chip_data(struct pwm_device *pwm);
344f051c466SThierry Reding 
345b6a00faeSTim Kryger int pwmchip_add_with_polarity(struct pwm_chip *chip,
346b6a00faeSTim Kryger 			      enum pwm_polarity polarity);
3470c2498f1SSascha Hauer int pwmchip_add(struct pwm_chip *chip);
3480c2498f1SSascha Hauer int pwmchip_remove(struct pwm_chip *chip);
349f051c466SThierry Reding struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
350f051c466SThierry Reding 					 unsigned int index,
351f051c466SThierry Reding 					 const char *label);
3528138d2ddSThierry Reding 
35383af2402SPhilip, Avinash struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *pc,
35483af2402SPhilip, Avinash 		const struct of_phandle_args *args);
35583af2402SPhilip, Avinash 
356d4c0c470SPeter Ujfalusi struct pwm_device *pwm_get(struct device *dev, const char *con_id);
3578eb96127SPeter Ujfalusi struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id);
3588138d2ddSThierry Reding void pwm_put(struct pwm_device *pwm);
3598138d2ddSThierry Reding 
360d4c0c470SPeter Ujfalusi struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
361261a5eddSPeter Ujfalusi struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
362261a5eddSPeter Ujfalusi 				   const char *con_id);
3636354316dSAlexandre Courbot void devm_pwm_put(struct device *dev, struct pwm_device *pwm);
3646e69ab13SFlorian Vaussard 
3656e69ab13SFlorian Vaussard bool pwm_can_sleep(struct pwm_device *pwm);
3660bcf168bSTushar Behera #else
3675ec803edSBoris Brezillon static inline struct pwm_device *pwm_request(int pwm_id, const char *label)
3685ec803edSBoris Brezillon {
3695ec803edSBoris Brezillon 	return ERR_PTR(-ENODEV);
3705ec803edSBoris Brezillon }
3715ec803edSBoris Brezillon 
3725ec803edSBoris Brezillon static inline void pwm_free(struct pwm_device *pwm)
3735ec803edSBoris Brezillon {
3745ec803edSBoris Brezillon }
3755ec803edSBoris Brezillon 
3765ec803edSBoris Brezillon static inline int pwm_apply_state(struct pwm_device *pwm,
3775ec803edSBoris Brezillon 				  const struct pwm_state *state)
3785ec803edSBoris Brezillon {
3795ec803edSBoris Brezillon 	return -ENOTSUPP;
3805ec803edSBoris Brezillon }
3815ec803edSBoris Brezillon 
3825ec803edSBoris Brezillon static inline int pwm_adjust_config(struct pwm_device *pwm)
3835ec803edSBoris Brezillon {
3845ec803edSBoris Brezillon 	return -ENOTSUPP;
3855ec803edSBoris Brezillon }
3865ec803edSBoris Brezillon 
3875ec803edSBoris Brezillon static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
3885ec803edSBoris Brezillon 			     int period_ns)
3895ec803edSBoris Brezillon {
3905ec803edSBoris Brezillon 	return -EINVAL;
3915ec803edSBoris Brezillon }
3925ec803edSBoris Brezillon 
393*3a3d1a4eSLee Jones static inline int pwm_capture(struct pwm_device *pwm,
394*3a3d1a4eSLee Jones 			      struct pwm_capture *result,
395*3a3d1a4eSLee Jones 			      unsigned long timeout)
396*3a3d1a4eSLee Jones {
397*3a3d1a4eSLee Jones 	return -EINVAL;
398*3a3d1a4eSLee Jones }
399*3a3d1a4eSLee Jones 
4005ec803edSBoris Brezillon static inline int pwm_set_polarity(struct pwm_device *pwm,
4015ec803edSBoris Brezillon 				   enum pwm_polarity polarity)
4025ec803edSBoris Brezillon {
4035ec803edSBoris Brezillon 	return -ENOTSUPP;
4045ec803edSBoris Brezillon }
4055ec803edSBoris Brezillon 
4065ec803edSBoris Brezillon static inline int pwm_enable(struct pwm_device *pwm)
4075ec803edSBoris Brezillon {
4085ec803edSBoris Brezillon 	return -EINVAL;
4095ec803edSBoris Brezillon }
4105ec803edSBoris Brezillon 
4115ec803edSBoris Brezillon static inline void pwm_disable(struct pwm_device *pwm)
4125ec803edSBoris Brezillon {
4135ec803edSBoris Brezillon }
4145ec803edSBoris Brezillon 
4150bcf168bSTushar Behera static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data)
4160bcf168bSTushar Behera {
4170bcf168bSTushar Behera 	return -EINVAL;
4180bcf168bSTushar Behera }
4190bcf168bSTushar Behera 
4200bcf168bSTushar Behera static inline void *pwm_get_chip_data(struct pwm_device *pwm)
4210bcf168bSTushar Behera {
4220bcf168bSTushar Behera 	return NULL;
4230bcf168bSTushar Behera }
4240bcf168bSTushar Behera 
4250bcf168bSTushar Behera static inline int pwmchip_add(struct pwm_chip *chip)
4260bcf168bSTushar Behera {
4270bcf168bSTushar Behera 	return -EINVAL;
4280bcf168bSTushar Behera }
4290bcf168bSTushar Behera 
430b6a00faeSTim Kryger static inline int pwmchip_add_inversed(struct pwm_chip *chip)
431b6a00faeSTim Kryger {
432b6a00faeSTim Kryger 	return -EINVAL;
433b6a00faeSTim Kryger }
434b6a00faeSTim Kryger 
4350bcf168bSTushar Behera static inline int pwmchip_remove(struct pwm_chip *chip)
4360bcf168bSTushar Behera {
4370bcf168bSTushar Behera 	return -EINVAL;
4380bcf168bSTushar Behera }
4390bcf168bSTushar Behera 
4400bcf168bSTushar Behera static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
4410bcf168bSTushar Behera 						       unsigned int index,
4420bcf168bSTushar Behera 						       const char *label)
4430bcf168bSTushar Behera {
4440bcf168bSTushar Behera 	return ERR_PTR(-ENODEV);
4450bcf168bSTushar Behera }
4460bcf168bSTushar Behera 
4470bcf168bSTushar Behera static inline struct pwm_device *pwm_get(struct device *dev,
4480bcf168bSTushar Behera 					 const char *consumer)
4490bcf168bSTushar Behera {
4500bcf168bSTushar Behera 	return ERR_PTR(-ENODEV);
4510bcf168bSTushar Behera }
4520bcf168bSTushar Behera 
4538eb96127SPeter Ujfalusi static inline struct pwm_device *of_pwm_get(struct device_node *np,
4548eb96127SPeter Ujfalusi 					    const char *con_id)
4558eb96127SPeter Ujfalusi {
4568eb96127SPeter Ujfalusi 	return ERR_PTR(-ENODEV);
4578eb96127SPeter Ujfalusi }
4588eb96127SPeter Ujfalusi 
4590bcf168bSTushar Behera static inline void pwm_put(struct pwm_device *pwm)
4600bcf168bSTushar Behera {
4610bcf168bSTushar Behera }
4620bcf168bSTushar Behera 
4630bcf168bSTushar Behera static inline struct pwm_device *devm_pwm_get(struct device *dev,
4640bcf168bSTushar Behera 					      const char *consumer)
4650bcf168bSTushar Behera {
4660bcf168bSTushar Behera 	return ERR_PTR(-ENODEV);
4670bcf168bSTushar Behera }
4680bcf168bSTushar Behera 
469261a5eddSPeter Ujfalusi static inline struct pwm_device *devm_of_pwm_get(struct device *dev,
470261a5eddSPeter Ujfalusi 						 struct device_node *np,
471261a5eddSPeter Ujfalusi 						 const char *con_id)
472261a5eddSPeter Ujfalusi {
473261a5eddSPeter Ujfalusi 	return ERR_PTR(-ENODEV);
474261a5eddSPeter Ujfalusi }
475261a5eddSPeter Ujfalusi 
4760bcf168bSTushar Behera static inline void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
4770bcf168bSTushar Behera {
4780bcf168bSTushar Behera }
4796e69ab13SFlorian Vaussard 
4806e69ab13SFlorian Vaussard static inline bool pwm_can_sleep(struct pwm_device *pwm)
4816e69ab13SFlorian Vaussard {
4826e69ab13SFlorian Vaussard 	return false;
4836e69ab13SFlorian Vaussard }
4840bcf168bSTushar Behera #endif
4856354316dSAlexandre Courbot 
4865ec803edSBoris Brezillon static inline void pwm_apply_args(struct pwm_device *pwm)
4875ec803edSBoris Brezillon {
4885ec803edSBoris Brezillon 	/*
4895ec803edSBoris Brezillon 	 * PWM users calling pwm_apply_args() expect to have a fresh config
4905ec803edSBoris Brezillon 	 * where the polarity and period are set according to pwm_args info.
4915ec803edSBoris Brezillon 	 * The problem is, polarity can only be changed when the PWM is
4925ec803edSBoris Brezillon 	 * disabled.
4935ec803edSBoris Brezillon 	 *
4945ec803edSBoris Brezillon 	 * PWM drivers supporting hardware readout may declare the PWM device
4955ec803edSBoris Brezillon 	 * as enabled, and prevent polarity setting, which changes from the
4965ec803edSBoris Brezillon 	 * existing behavior, where all PWM devices are declared as disabled
4975ec803edSBoris Brezillon 	 * at startup (even if they are actually enabled), thus authorizing
4985ec803edSBoris Brezillon 	 * polarity setting.
4995ec803edSBoris Brezillon 	 *
5005ec803edSBoris Brezillon 	 * Instead of setting ->enabled to false, we call pwm_disable()
5015ec803edSBoris Brezillon 	 * before pwm_set_polarity() to ensure that everything is configured
5025ec803edSBoris Brezillon 	 * as expected, and the PWM is really disabled when the user request
5035ec803edSBoris Brezillon 	 * it.
5045ec803edSBoris Brezillon 	 *
5055ec803edSBoris Brezillon 	 * Note that PWM users requiring a smooth handover between the
5065ec803edSBoris Brezillon 	 * bootloader and the kernel (like critical regulators controlled by
5075ec803edSBoris Brezillon 	 * PWM devices) will have to switch to the atomic API and avoid calling
5085ec803edSBoris Brezillon 	 * pwm_apply_args().
5095ec803edSBoris Brezillon 	 */
5105ec803edSBoris Brezillon 	pwm_disable(pwm);
5115ec803edSBoris Brezillon 	pwm_set_polarity(pwm, pwm->args.polarity);
5125ec803edSBoris Brezillon }
5135ec803edSBoris Brezillon 
5148138d2ddSThierry Reding struct pwm_lookup {
5158138d2ddSThierry Reding 	struct list_head list;
5168138d2ddSThierry Reding 	const char *provider;
5178138d2ddSThierry Reding 	unsigned int index;
5188138d2ddSThierry Reding 	const char *dev_id;
5198138d2ddSThierry Reding 	const char *con_id;
5203796ce1dSAlexandre Belloni 	unsigned int period;
5213796ce1dSAlexandre Belloni 	enum pwm_polarity polarity;
5228138d2ddSThierry Reding };
5238138d2ddSThierry Reding 
52442844029SAlexandre Belloni #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
5258138d2ddSThierry Reding 	{						\
5268138d2ddSThierry Reding 		.provider = _provider,			\
5278138d2ddSThierry Reding 		.index = _index,			\
5288138d2ddSThierry Reding 		.dev_id = _dev_id,			\
5298138d2ddSThierry Reding 		.con_id = _con_id,			\
53042844029SAlexandre Belloni 		.period = _period,			\
53142844029SAlexandre Belloni 		.polarity = _polarity			\
5328138d2ddSThierry Reding 	}
5338138d2ddSThierry Reding 
5340bcf168bSTushar Behera #if IS_ENABLED(CONFIG_PWM)
5358138d2ddSThierry Reding void pwm_add_table(struct pwm_lookup *table, size_t num);
536efb0de55SShobhit Kumar void pwm_remove_table(struct pwm_lookup *table, size_t num);
5370bcf168bSTushar Behera #else
5380bcf168bSTushar Behera static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
5390bcf168bSTushar Behera {
5400bcf168bSTushar Behera }
541efb0de55SShobhit Kumar 
542efb0de55SShobhit Kumar static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
543efb0de55SShobhit Kumar {
544efb0de55SShobhit Kumar }
5450c2498f1SSascha Hauer #endif
5460c2498f1SSascha Hauer 
54776abbddeSH Hartley Sweeten #ifdef CONFIG_PWM_SYSFS
54876abbddeSH Hartley Sweeten void pwmchip_sysfs_export(struct pwm_chip *chip);
54976abbddeSH Hartley Sweeten void pwmchip_sysfs_unexport(struct pwm_chip *chip);
55076abbddeSH Hartley Sweeten #else
55176abbddeSH Hartley Sweeten static inline void pwmchip_sysfs_export(struct pwm_chip *chip)
55276abbddeSH Hartley Sweeten {
55376abbddeSH Hartley Sweeten }
55476abbddeSH Hartley Sweeten 
55576abbddeSH Hartley Sweeten static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip)
55676abbddeSH Hartley Sweeten {
55776abbddeSH Hartley Sweeten }
55876abbddeSH Hartley Sweeten #endif /* CONFIG_PWM_SYSFS */
55976abbddeSH Hartley Sweeten 
5605243ef8bSMark Vels #endif /* __LINUX_PWM_H */
561