xref: /linux-6.15/include/linux/pwm.h (revision 7c94fe4a)
1 #ifndef __LINUX_PWM_H
2 #define __LINUX_PWM_H
3 
4 #include <linux/of.h>
5 
6 struct pwm_device;
7 struct seq_file;
8 
9 /*
10  * pwm_request - request a PWM device
11  */
12 struct pwm_device *pwm_request(int pwm_id, const char *label);
13 
14 /*
15  * pwm_free - free a PWM device
16  */
17 void pwm_free(struct pwm_device *pwm);
18 
19 /*
20  * pwm_config - change a PWM device configuration
21  */
22 int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns);
23 
24 /*
25  * pwm_enable - start a PWM output toggling
26  */
27 int pwm_enable(struct pwm_device *pwm);
28 
29 /*
30  * pwm_disable - stop a PWM output toggling
31  */
32 void pwm_disable(struct pwm_device *pwm);
33 
34 #ifdef CONFIG_PWM
35 struct pwm_chip;
36 
37 enum {
38 	PWMF_REQUESTED = 1 << 0,
39 	PWMF_ENABLED = 1 << 1,
40 };
41 
42 struct pwm_device {
43 	const char		*label;
44 	unsigned long		flags;
45 	unsigned int		hwpwm;
46 	unsigned int		pwm;
47 	struct pwm_chip		*chip;
48 	void			*chip_data;
49 
50 	unsigned int		period; /* in nanoseconds */
51 };
52 
53 static inline void pwm_set_period(struct pwm_device *pwm, unsigned int period)
54 {
55 	if (pwm)
56 		pwm->period = period;
57 }
58 
59 static inline unsigned int pwm_get_period(struct pwm_device *pwm)
60 {
61 	return pwm ? pwm->period : 0;
62 }
63 
64 /**
65  * struct pwm_ops - PWM controller operations
66  * @request: optional hook for requesting a PWM
67  * @free: optional hook for freeing a PWM
68  * @config: configure duty cycles and period length for this PWM
69  * @enable: enable PWM output toggling
70  * @disable: disable PWM output toggling
71  * @dbg_show: optional routine to show contents in debugfs
72  * @owner: helps prevent removal of modules exporting active PWMs
73  */
74 struct pwm_ops {
75 	int			(*request)(struct pwm_chip *chip,
76 					   struct pwm_device *pwm);
77 	void			(*free)(struct pwm_chip *chip,
78 					struct pwm_device *pwm);
79 	int			(*config)(struct pwm_chip *chip,
80 					  struct pwm_device *pwm,
81 					  int duty_ns, int period_ns);
82 	int			(*enable)(struct pwm_chip *chip,
83 					  struct pwm_device *pwm);
84 	void			(*disable)(struct pwm_chip *chip,
85 					   struct pwm_device *pwm);
86 #ifdef CONFIG_DEBUG_FS
87 	void			(*dbg_show)(struct pwm_chip *chip,
88 					    struct seq_file *s);
89 #endif
90 	struct module		*owner;
91 };
92 
93 /**
94  * struct pwm_chip - abstract a PWM controller
95  * @dev: device providing the PWMs
96  * @list: list node for internal use
97  * @ops: callbacks for this PWM controller
98  * @base: number of first PWM controlled by this chip
99  * @npwm: number of PWMs controlled by this chip
100  * @pwms: array of PWM devices allocated by the framework
101  */
102 struct pwm_chip {
103 	struct device		*dev;
104 	struct list_head	list;
105 	const struct pwm_ops	*ops;
106 	int			base;
107 	unsigned int		npwm;
108 
109 	struct pwm_device	*pwms;
110 
111 	struct pwm_device *	(*of_xlate)(struct pwm_chip *pc,
112 					    const struct of_phandle_args *args);
113 	unsigned int		of_pwm_n_cells;
114 };
115 
116 int pwm_set_chip_data(struct pwm_device *pwm, void *data);
117 void *pwm_get_chip_data(struct pwm_device *pwm);
118 
119 int pwmchip_add(struct pwm_chip *chip);
120 int pwmchip_remove(struct pwm_chip *chip);
121 struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
122 					 unsigned int index,
123 					 const char *label);
124 
125 struct pwm_device *pwm_get(struct device *dev, const char *consumer);
126 void pwm_put(struct pwm_device *pwm);
127 
128 struct pwm_lookup {
129 	struct list_head list;
130 	const char *provider;
131 	unsigned int index;
132 	const char *dev_id;
133 	const char *con_id;
134 };
135 
136 #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id)	\
137 	{						\
138 		.provider = _provider,			\
139 		.index = _index,			\
140 		.dev_id = _dev_id,			\
141 		.con_id = _con_id,			\
142 	}
143 
144 void pwm_add_table(struct pwm_lookup *table, size_t num);
145 
146 #endif
147 
148 #endif /* __LINUX_PWM_H */
149