1#ifndef __LINUX_PWM_H
2#define __LINUX_PWM_H
3
4#include <linux/err.h>
5#include <linux/of.h>
6
7struct pwm_device;
8struct seq_file;
9
10#if IS_ENABLED(CONFIG_PWM) || IS_ENABLED(CONFIG_HAVE_PWM)
11
12
13
14struct pwm_device *pwm_request(int pwm_id, const char *label);
15
16
17
18
19void pwm_free(struct pwm_device *pwm);
20
21
22
23
24int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns);
25
26
27
28
29int pwm_enable(struct pwm_device *pwm);
30
31
32
33
34void pwm_disable(struct pwm_device *pwm);
35#else
36static inline struct pwm_device *pwm_request(int pwm_id, const char *label)
37{
38 return ERR_PTR(-ENODEV);
39}
40
41static inline void pwm_free(struct pwm_device *pwm)
42{
43}
44
45static inline int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
46{
47 return -EINVAL;
48}
49
50static inline int pwm_enable(struct pwm_device *pwm)
51{
52 return -EINVAL;
53}
54
55static inline void pwm_disable(struct pwm_device *pwm)
56{
57}
58#endif
59
60struct pwm_chip;
61
62
63
64
65
66
67
68
69
70
71enum pwm_polarity {
72 PWM_POLARITY_NORMAL,
73 PWM_POLARITY_INVERSED,
74};
75
76enum {
77 PWMF_REQUESTED = 1 << 0,
78 PWMF_ENABLED = 1 << 1,
79};
80
81struct pwm_device {
82 const char *label;
83 unsigned long flags;
84 unsigned int hwpwm;
85 unsigned int pwm;
86 struct pwm_chip *chip;
87 void *chip_data;
88
89 unsigned int period;
90};
91
92static inline void pwm_set_period(struct pwm_device *pwm, unsigned int period)
93{
94 if (pwm)
95 pwm->period = period;
96}
97
98static inline unsigned int pwm_get_period(struct pwm_device *pwm)
99{
100 return pwm ? pwm->period : 0;
101}
102
103
104
105
106int pwm_set_polarity(struct pwm_device *pwm, enum pwm_polarity polarity);
107
108
109
110
111
112
113
114
115
116
117
118
119struct pwm_ops {
120 int (*request)(struct pwm_chip *chip,
121 struct pwm_device *pwm);
122 void (*free)(struct pwm_chip *chip,
123 struct pwm_device *pwm);
124 int (*config)(struct pwm_chip *chip,
125 struct pwm_device *pwm,
126 int duty_ns, int period_ns);
127 int (*set_polarity)(struct pwm_chip *chip,
128 struct pwm_device *pwm,
129 enum pwm_polarity polarity);
130 int (*enable)(struct pwm_chip *chip,
131 struct pwm_device *pwm);
132 void (*disable)(struct pwm_chip *chip,
133 struct pwm_device *pwm);
134#ifdef CONFIG_DEBUG_FS
135 void (*dbg_show)(struct pwm_chip *chip,
136 struct seq_file *s);
137#endif
138 struct module *owner;
139};
140
141
142
143
144
145
146
147
148
149
150struct pwm_chip {
151 struct device *dev;
152 struct list_head list;
153 const struct pwm_ops *ops;
154 int base;
155 unsigned int npwm;
156
157 struct pwm_device *pwms;
158
159 struct pwm_device * (*of_xlate)(struct pwm_chip *pc,
160 const struct of_phandle_args *args);
161 unsigned int of_pwm_n_cells;
162};
163
164#if IS_ENABLED(CONFIG_PWM)
165int pwm_set_chip_data(struct pwm_device *pwm, void *data);
166void *pwm_get_chip_data(struct pwm_device *pwm);
167
168int pwmchip_add(struct pwm_chip *chip);
169int pwmchip_remove(struct pwm_chip *chip);
170struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
171 unsigned int index,
172 const char *label);
173
174struct pwm_device *pwm_get(struct device *dev, const char *consumer);
175void pwm_put(struct pwm_device *pwm);
176
177struct pwm_device *devm_pwm_get(struct device *dev, const char *consumer);
178void devm_pwm_put(struct device *dev, struct pwm_device *pwm);
179#else
180static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data)
181{
182 return -EINVAL;
183}
184
185static inline void *pwm_get_chip_data(struct pwm_device *pwm)
186{
187 return NULL;
188}
189
190static inline int pwmchip_add(struct pwm_chip *chip)
191{
192 return -EINVAL;
193}
194
195static inline int pwmchip_remove(struct pwm_chip *chip)
196{
197 return -EINVAL;
198}
199
200static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
201 unsigned int index,
202 const char *label)
203{
204 return ERR_PTR(-ENODEV);
205}
206
207static inline struct pwm_device *pwm_get(struct device *dev,
208 const char *consumer)
209{
210 return ERR_PTR(-ENODEV);
211}
212
213static inline void pwm_put(struct pwm_device *pwm)
214{
215}
216
217static inline struct pwm_device *devm_pwm_get(struct device *dev,
218 const char *consumer)
219{
220 return ERR_PTR(-ENODEV);
221}
222
223static inline void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
224{
225}
226#endif
227
228struct pwm_lookup {
229 struct list_head list;
230 const char *provider;
231 unsigned int index;
232 const char *dev_id;
233 const char *con_id;
234};
235
236#define PWM_LOOKUP(_provider, _index, _dev_id, _con_id) \
237 { \
238 .provider = _provider, \
239 .index = _index, \
240 .dev_id = _dev_id, \
241 .con_id = _con_id, \
242 }
243
244#if IS_ENABLED(CONFIG_PWM)
245void pwm_add_table(struct pwm_lookup *table, size_t num);
246#else
247static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
248{
249}
250#endif
251
252#endif
253