1#ifndef __LINUX_PWM_H
2#define __LINUX_PWM_H
3
4#include <linux/err.h>
5#include <linux/mutex.h>
6#include <linux/of.h>
7
8struct pwm_device;
9struct seq_file;
10
11#if IS_ENABLED(CONFIG_PWM)
12
13
14
15struct pwm_device *pwm_request(int pwm_id, const char *label);
16
17
18
19
20void pwm_free(struct pwm_device *pwm);
21
22
23
24
25int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns);
26
27
28
29
30int pwm_enable(struct pwm_device *pwm);
31
32
33
34
35void pwm_disable(struct pwm_device *pwm);
36#else
37static inline struct pwm_device *pwm_request(int pwm_id, const char *label)
38{
39 return ERR_PTR(-ENODEV);
40}
41
42static inline void pwm_free(struct pwm_device *pwm)
43{
44}
45
46static inline int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
47{
48 return -EINVAL;
49}
50
51static inline int pwm_enable(struct pwm_device *pwm)
52{
53 return -EINVAL;
54}
55
56static inline void pwm_disable(struct pwm_device *pwm)
57{
58}
59#endif
60
61struct pwm_chip;
62
63
64
65
66
67
68
69
70
71
72enum pwm_polarity {
73 PWM_POLARITY_NORMAL,
74 PWM_POLARITY_INVERSED,
75};
76
77enum {
78 PWMF_REQUESTED = 1 << 0,
79 PWMF_ENABLED = 1 << 1,
80 PWMF_EXPORTED = 1 << 2,
81};
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96struct pwm_device {
97 const char *label;
98 unsigned long flags;
99 unsigned int hwpwm;
100 unsigned int pwm;
101 struct pwm_chip *chip;
102 void *chip_data;
103 struct mutex lock;
104
105 unsigned int period;
106 unsigned int duty_cycle;
107 enum pwm_polarity polarity;
108};
109
110static inline bool pwm_is_enabled(const struct pwm_device *pwm)
111{
112 return test_bit(PWMF_ENABLED, &pwm->flags);
113}
114
115static inline void pwm_set_period(struct pwm_device *pwm, unsigned int period)
116{
117 if (pwm)
118 pwm->period = period;
119}
120
121static inline unsigned int pwm_get_period(const struct pwm_device *pwm)
122{
123 return pwm ? pwm->period : 0;
124}
125
126static inline void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty)
127{
128 if (pwm)
129 pwm->duty_cycle = duty;
130}
131
132static inline unsigned int pwm_get_duty_cycle(const struct pwm_device *pwm)
133{
134 return pwm ? pwm->duty_cycle : 0;
135}
136
137
138
139
140int pwm_set_polarity(struct pwm_device *pwm, enum pwm_polarity polarity);
141
142static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm)
143{
144 return pwm ? pwm->polarity : PWM_POLARITY_NORMAL;
145}
146
147
148
149
150
151
152
153
154
155
156
157
158struct pwm_ops {
159 int (*request)(struct pwm_chip *chip, struct pwm_device *pwm);
160 void (*free)(struct pwm_chip *chip, struct pwm_device *pwm);
161 int (*config)(struct pwm_chip *chip, struct pwm_device *pwm,
162 int duty_ns, int period_ns);
163 int (*set_polarity)(struct pwm_chip *chip, struct pwm_device *pwm,
164 enum pwm_polarity polarity);
165 int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm);
166 void (*disable)(struct pwm_chip *chip, struct pwm_device *pwm);
167#ifdef CONFIG_DEBUG_FS
168 void (*dbg_show)(struct pwm_chip *chip, struct seq_file *s);
169#endif
170 struct module *owner;
171};
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186struct pwm_chip {
187 struct device *dev;
188 struct list_head list;
189 const struct pwm_ops *ops;
190 int base;
191 unsigned int npwm;
192
193 struct pwm_device *pwms;
194
195 struct pwm_device * (*of_xlate)(struct pwm_chip *pc,
196 const struct of_phandle_args *args);
197 unsigned int of_pwm_n_cells;
198 bool can_sleep;
199};
200
201#if IS_ENABLED(CONFIG_PWM)
202int pwm_set_chip_data(struct pwm_device *pwm, void *data);
203void *pwm_get_chip_data(struct pwm_device *pwm);
204
205int pwmchip_add_with_polarity(struct pwm_chip *chip,
206 enum pwm_polarity polarity);
207int pwmchip_add(struct pwm_chip *chip);
208int pwmchip_remove(struct pwm_chip *chip);
209struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
210 unsigned int index,
211 const char *label);
212
213struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *pc,
214 const struct of_phandle_args *args);
215
216struct pwm_device *pwm_get(struct device *dev, const char *con_id);
217struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id);
218void pwm_put(struct pwm_device *pwm);
219
220struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
221struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
222 const char *con_id);
223void devm_pwm_put(struct device *dev, struct pwm_device *pwm);
224
225bool pwm_can_sleep(struct pwm_device *pwm);
226#else
227static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data)
228{
229 return -EINVAL;
230}
231
232static inline void *pwm_get_chip_data(struct pwm_device *pwm)
233{
234 return NULL;
235}
236
237static inline int pwmchip_add(struct pwm_chip *chip)
238{
239 return -EINVAL;
240}
241
242static inline int pwmchip_add_inversed(struct pwm_chip *chip)
243{
244 return -EINVAL;
245}
246
247static inline int pwmchip_remove(struct pwm_chip *chip)
248{
249 return -EINVAL;
250}
251
252static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
253 unsigned int index,
254 const char *label)
255{
256 return ERR_PTR(-ENODEV);
257}
258
259static inline struct pwm_device *pwm_get(struct device *dev,
260 const char *consumer)
261{
262 return ERR_PTR(-ENODEV);
263}
264
265static inline struct pwm_device *of_pwm_get(struct device_node *np,
266 const char *con_id)
267{
268 return ERR_PTR(-ENODEV);
269}
270
271static inline void pwm_put(struct pwm_device *pwm)
272{
273}
274
275static inline struct pwm_device *devm_pwm_get(struct device *dev,
276 const char *consumer)
277{
278 return ERR_PTR(-ENODEV);
279}
280
281static inline struct pwm_device *devm_of_pwm_get(struct device *dev,
282 struct device_node *np,
283 const char *con_id)
284{
285 return ERR_PTR(-ENODEV);
286}
287
288static inline void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
289{
290}
291
292static inline bool pwm_can_sleep(struct pwm_device *pwm)
293{
294 return false;
295}
296#endif
297
298struct pwm_lookup {
299 struct list_head list;
300 const char *provider;
301 unsigned int index;
302 const char *dev_id;
303 const char *con_id;
304 unsigned int period;
305 enum pwm_polarity polarity;
306};
307
308#define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
309 { \
310 .provider = _provider, \
311 .index = _index, \
312 .dev_id = _dev_id, \
313 .con_id = _con_id, \
314 .period = _period, \
315 .polarity = _polarity \
316 }
317
318#if IS_ENABLED(CONFIG_PWM)
319void pwm_add_table(struct pwm_lookup *table, size_t num);
320void pwm_remove_table(struct pwm_lookup *table, size_t num);
321#else
322static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
323{
324}
325
326static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
327{
328}
329#endif
330
331#ifdef CONFIG_PWM_SYSFS
332void pwmchip_sysfs_export(struct pwm_chip *chip);
333void pwmchip_sysfs_unexport(struct pwm_chip *chip);
334#else
335static inline void pwmchip_sysfs_export(struct pwm_chip *chip)
336{
337}
338
339static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip)
340{
341}
342#endif
343
344#endif
345