661ece188a5161d1ef2fd8a87b9c585adfd68a11
[firefly-linux-kernel-4.4.55.git] / include / linux / pwm.h
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
8 struct pwm_device;
9 struct seq_file;
10
11 #if IS_ENABLED(CONFIG_PWM)
12 /*
13  * pwm_request - request a PWM device
14  */
15 struct pwm_device *pwm_request(int pwm_id, const char *label);
16
17 /*
18  * pwm_free - free a PWM device
19  */
20 void pwm_free(struct pwm_device *pwm);
21
22 /*
23  * pwm_config - change a PWM device configuration
24  */
25 int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns);
26
27 /*
28  * pwm_enable - start a PWM output toggling
29  */
30 int pwm_enable(struct pwm_device *pwm);
31
32 /*
33  * pwm_disable - stop a PWM output toggling
34  */
35 void pwm_disable(struct pwm_device *pwm);
36 #else
37 static inline struct pwm_device *pwm_request(int pwm_id, const char *label)
38 {
39         return ERR_PTR(-ENODEV);
40 }
41
42 static inline void pwm_free(struct pwm_device *pwm)
43 {
44 }
45
46 static inline int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
47 {
48         return -EINVAL;
49 }
50
51 static inline int pwm_enable(struct pwm_device *pwm)
52 {
53         return -EINVAL;
54 }
55
56 static inline void pwm_disable(struct pwm_device *pwm)
57 {
58 }
59 #endif
60
61 struct pwm_chip;
62
63 /**
64  * enum pwm_polarity - polarity of a PWM signal
65  * @PWM_POLARITY_NORMAL: a high signal for the duration of the duty-
66  * cycle, followed by a low signal for the remainder of the pulse
67  * period
68  * @PWM_POLARITY_INVERSED: a low signal for the duration of the duty-
69  * cycle, followed by a high signal for the remainder of the pulse
70  * period
71  */
72 enum pwm_polarity {
73         PWM_POLARITY_NORMAL,
74         PWM_POLARITY_INVERSED,
75 };
76
77 /**
78  * struct pwm_args - board-dependent PWM arguments
79  * @period: reference period
80  * @polarity: reference polarity
81  *
82  * This structure describes board-dependent arguments attached to a PWM
83  * device. These arguments are usually retrieved from the PWM lookup table or
84  * device tree.
85  *
86  * Do not confuse this with the PWM state: PWM arguments represent the initial
87  * configuration that users want to use on this PWM device rather than the
88  * current PWM hardware state.
89  */
90 struct pwm_args {
91         unsigned int period;
92         enum pwm_polarity polarity;
93 };
94
95 enum {
96         PWMF_REQUESTED = 1 << 0,
97         PWMF_ENABLED = 1 << 1,
98         PWMF_EXPORTED = 1 << 2,
99 };
100
101 /**
102  * struct pwm_device - PWM channel object
103  * @label: name of the PWM device
104  * @flags: flags associated with the PWM device
105  * @hwpwm: per-chip relative index of the PWM device
106  * @pwm: global index of the PWM device
107  * @chip: PWM chip providing this PWM device
108  * @chip_data: chip-private data associated with the PWM device
109  * @period: period of the PWM signal (in nanoseconds)
110  * @duty_cycle: duty cycle of the PWM signal (in nanoseconds)
111  * @polarity: polarity of the PWM signal
112  * @args: PWM arguments
113  */
114 struct pwm_device {
115         const char *label;
116         unsigned long flags;
117         unsigned int hwpwm;
118         unsigned int pwm;
119         struct pwm_chip *chip;
120         void *chip_data;
121
122         unsigned int period;
123         unsigned int duty_cycle;
124         enum pwm_polarity polarity;
125
126         struct pwm_args args;
127 };
128
129 static inline bool pwm_is_enabled(const struct pwm_device *pwm)
130 {
131         return test_bit(PWMF_ENABLED, &pwm->flags);
132 }
133
134 static inline void pwm_set_period(struct pwm_device *pwm, unsigned int period)
135 {
136         if (pwm)
137                 pwm->period = period;
138 }
139
140 static inline unsigned int pwm_get_period(const struct pwm_device *pwm)
141 {
142         return pwm ? pwm->period : 0;
143 }
144
145 static inline void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty)
146 {
147         if (pwm)
148                 pwm->duty_cycle = duty;
149 }
150
151 static inline unsigned int pwm_get_duty_cycle(const struct pwm_device *pwm)
152 {
153         return pwm ? pwm->duty_cycle : 0;
154 }
155
156 /*
157  * pwm_set_polarity - configure the polarity of a PWM signal
158  */
159 int pwm_set_polarity(struct pwm_device *pwm, enum pwm_polarity polarity);
160
161 static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm)
162 {
163         return pwm ? pwm->polarity : PWM_POLARITY_NORMAL;
164 }
165
166 static inline void pwm_get_args(const struct pwm_device *pwm,
167                                 struct pwm_args *args)
168 {
169         *args = pwm->args;
170 }
171
172 static inline void pwm_apply_args(struct pwm_device *pwm)
173 {
174         pwm_set_period(pwm, pwm->args.period);
175         pwm_set_polarity(pwm, pwm->args.polarity);
176 }
177
178 /**
179  * struct pwm_ops - PWM controller operations
180  * @request: optional hook for requesting a PWM
181  * @free: optional hook for freeing a PWM
182  * @config: configure duty cycles and period length for this PWM
183  * @set_polarity: configure the polarity of this PWM
184  * @enable: enable PWM output toggling
185  * @disable: disable PWM output toggling
186  * @dbg_show: optional routine to show contents in debugfs
187  * @owner: helps prevent removal of modules exporting active PWMs
188  */
189 struct pwm_ops {
190         int (*request)(struct pwm_chip *chip, struct pwm_device *pwm);
191         void (*free)(struct pwm_chip *chip, struct pwm_device *pwm);
192         int (*config)(struct pwm_chip *chip, struct pwm_device *pwm,
193                       int duty_ns, int period_ns);
194         int (*set_polarity)(struct pwm_chip *chip, struct pwm_device *pwm,
195                             enum pwm_polarity polarity);
196         int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm);
197         void (*disable)(struct pwm_chip *chip, struct pwm_device *pwm);
198 #ifdef CONFIG_DEBUG_FS
199         void (*dbg_show)(struct pwm_chip *chip, struct seq_file *s);
200 #endif
201         struct module *owner;
202 };
203
204 /**
205  * struct pwm_chip - abstract a PWM controller
206  * @dev: device providing the PWMs
207  * @list: list node for internal use
208  * @ops: callbacks for this PWM controller
209  * @base: number of first PWM controlled by this chip
210  * @npwm: number of PWMs controlled by this chip
211  * @pwms: array of PWM devices allocated by the framework
212  * @of_xlate: request a PWM device given a device tree PWM specifier
213  * @of_pwm_n_cells: number of cells expected in the device tree PWM specifier
214  * @can_sleep: must be true if the .config(), .enable() or .disable()
215  *             operations may sleep
216  */
217 struct pwm_chip {
218         struct device *dev;
219         struct list_head list;
220         const struct pwm_ops *ops;
221         int base;
222         unsigned int npwm;
223
224         struct pwm_device *pwms;
225
226         struct pwm_device * (*of_xlate)(struct pwm_chip *pc,
227                                         const struct of_phandle_args *args);
228         unsigned int of_pwm_n_cells;
229         bool can_sleep;
230 };
231
232 #if IS_ENABLED(CONFIG_PWM)
233 int pwm_set_chip_data(struct pwm_device *pwm, void *data);
234 void *pwm_get_chip_data(struct pwm_device *pwm);
235
236 int pwmchip_add_with_polarity(struct pwm_chip *chip,
237                               enum pwm_polarity polarity);
238 int pwmchip_add(struct pwm_chip *chip);
239 int pwmchip_remove(struct pwm_chip *chip);
240 struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
241                                          unsigned int index,
242                                          const char *label);
243
244 struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *pc,
245                 const struct of_phandle_args *args);
246
247 struct pwm_device *pwm_get(struct device *dev, const char *con_id);
248 struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id);
249 void pwm_put(struct pwm_device *pwm);
250
251 struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
252 struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
253                                    const char *con_id);
254 void devm_pwm_put(struct device *dev, struct pwm_device *pwm);
255
256 bool pwm_can_sleep(struct pwm_device *pwm);
257 #else
258 static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data)
259 {
260         return -EINVAL;
261 }
262
263 static inline void *pwm_get_chip_data(struct pwm_device *pwm)
264 {
265         return NULL;
266 }
267
268 static inline int pwmchip_add(struct pwm_chip *chip)
269 {
270         return -EINVAL;
271 }
272
273 static inline int pwmchip_add_inversed(struct pwm_chip *chip)
274 {
275         return -EINVAL;
276 }
277
278 static inline int pwmchip_remove(struct pwm_chip *chip)
279 {
280         return -EINVAL;
281 }
282
283 static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
284                                                        unsigned int index,
285                                                        const char *label)
286 {
287         return ERR_PTR(-ENODEV);
288 }
289
290 static inline struct pwm_device *pwm_get(struct device *dev,
291                                          const char *consumer)
292 {
293         return ERR_PTR(-ENODEV);
294 }
295
296 static inline struct pwm_device *of_pwm_get(struct device_node *np,
297                                             const char *con_id)
298 {
299         return ERR_PTR(-ENODEV);
300 }
301
302 static inline void pwm_put(struct pwm_device *pwm)
303 {
304 }
305
306 static inline struct pwm_device *devm_pwm_get(struct device *dev,
307                                               const char *consumer)
308 {
309         return ERR_PTR(-ENODEV);
310 }
311
312 static inline struct pwm_device *devm_of_pwm_get(struct device *dev,
313                                                  struct device_node *np,
314                                                  const char *con_id)
315 {
316         return ERR_PTR(-ENODEV);
317 }
318
319 static inline void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
320 {
321 }
322
323 static inline bool pwm_can_sleep(struct pwm_device *pwm)
324 {
325         return false;
326 }
327 #endif
328
329 struct pwm_lookup {
330         struct list_head list;
331         const char *provider;
332         unsigned int index;
333         const char *dev_id;
334         const char *con_id;
335         unsigned int period;
336         enum pwm_polarity polarity;
337 };
338
339 #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
340         {                                               \
341                 .provider = _provider,                  \
342                 .index = _index,                        \
343                 .dev_id = _dev_id,                      \
344                 .con_id = _con_id,                      \
345                 .period = _period,                      \
346                 .polarity = _polarity                   \
347         }
348
349 #if IS_ENABLED(CONFIG_PWM)
350 void pwm_add_table(struct pwm_lookup *table, size_t num);
351 void pwm_remove_table(struct pwm_lookup *table, size_t num);
352 #else
353 static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
354 {
355 }
356
357 static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
358 {
359 }
360 #endif
361
362 #ifdef CONFIG_PWM_SYSFS
363 void pwmchip_sysfs_export(struct pwm_chip *chip);
364 void pwmchip_sysfs_unexport(struct pwm_chip *chip);
365 void pwmchip_sysfs_unexport_children(struct pwm_chip *chip);
366 #else
367 static inline void pwmchip_sysfs_export(struct pwm_chip *chip)
368 {
369 }
370
371 static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip)
372 {
373 }
374
375 static inline void pwmchip_sysfs_unexport_children(struct pwm_chip *chip)
376 {
377 }
378 #endif /* CONFIG_PWM_SYSFS */
379
380 #endif /* __LINUX_PWM_H */