Merge tag 'sound-4.7-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai...
[deliverable/linux.git] / include / linux / pwm.h
CommitLineData
1a189b97
RK
1#ifndef __LINUX_PWM_H
2#define __LINUX_PWM_H
3
0bcf168b 4#include <linux/err.h>
d1cd2142 5#include <linux/mutex.h>
7299ab70
TR
6#include <linux/of.h>
7
1a189b97 8struct pwm_device;
62099abf 9struct seq_file;
1a189b97 10
557fe99d 11#if IS_ENABLED(CONFIG_PWM)
1a189b97
RK
12/*
13 * pwm_request - request a PWM device
14 */
15struct pwm_device *pwm_request(int pwm_id, const char *label);
16
17/*
18 * pwm_free - free a PWM device
19 */
20void pwm_free(struct pwm_device *pwm);
21
22/*
23 * pwm_config - change a PWM device configuration
24 */
25int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns);
26
27/*
28 * pwm_enable - start a PWM output toggling
29 */
30int pwm_enable(struct pwm_device *pwm);
31
32/*
33 * pwm_disable - stop a PWM output toggling
34 */
35void pwm_disable(struct pwm_device *pwm);
0bcf168b
TB
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
1a189b97 60
0c2498f1
SH
61struct pwm_chip;
62
0aa0869c
PA
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 */
72enum pwm_polarity {
73 PWM_POLARITY_NORMAL,
74 PWM_POLARITY_INVERSED,
75};
76
e39c0df1
BB
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 */
90struct pwm_args {
91 unsigned int period;
92 enum pwm_polarity polarity;
93};
94
f051c466
TR
95enum {
96 PWMF_REQUESTED = 1 << 0,
97 PWMF_ENABLED = 1 << 1,
76abbdde 98 PWMF_EXPORTED = 1 << 2,
f051c466
TR
99};
100
04883802
TR
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
d1cd2142 109 * @lock: used to serialize accesses to the PWM device where necessary
04883802
TR
110 * @period: period of the PWM signal (in nanoseconds)
111 * @duty_cycle: duty cycle of the PWM signal (in nanoseconds)
112 * @polarity: polarity of the PWM signal
e39c0df1 113 * @args: PWM arguments
04883802 114 */
f051c466 115struct pwm_device {
6bc7064a
TR
116 const char *label;
117 unsigned long flags;
118 unsigned int hwpwm;
119 unsigned int pwm;
120 struct pwm_chip *chip;
121 void *chip_data;
d1cd2142 122 struct mutex lock;
6bc7064a
TR
123
124 unsigned int period;
125 unsigned int duty_cycle;
126 enum pwm_polarity polarity;
e39c0df1
BB
127
128 struct pwm_args args;
f051c466
TR
129};
130
5c31252c
BB
131static inline bool pwm_is_enabled(const struct pwm_device *pwm)
132{
133 return test_bit(PWMF_ENABLED, &pwm->flags);
134}
135
f051c466
TR
136static inline void pwm_set_period(struct pwm_device *pwm, unsigned int period)
137{
138 if (pwm)
139 pwm->period = period;
140}
141
a1cf4217 142static inline unsigned int pwm_get_period(const struct pwm_device *pwm)
f051c466
TR
143{
144 return pwm ? pwm->period : 0;
145}
146
76abbdde
HS
147static inline void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty)
148{
149 if (pwm)
150 pwm->duty_cycle = duty;
151}
152
a1cf4217 153static inline unsigned int pwm_get_duty_cycle(const struct pwm_device *pwm)
76abbdde
HS
154{
155 return pwm ? pwm->duty_cycle : 0;
156}
157
0aa0869c
PA
158/*
159 * pwm_set_polarity - configure the polarity of a PWM signal
160 */
161int pwm_set_polarity(struct pwm_device *pwm, enum pwm_polarity polarity);
162
011e7631
BB
163static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm)
164{
165 return pwm ? pwm->polarity : PWM_POLARITY_NORMAL;
166}
167
e39c0df1
BB
168static inline void pwm_get_args(const struct pwm_device *pwm,
169 struct pwm_args *args)
170{
171 *args = pwm->args;
172}
173
174static inline void pwm_apply_args(struct pwm_device *pwm)
175{
176 pwm_set_period(pwm, pwm->args.period);
177 pwm_set_polarity(pwm, pwm->args.polarity);
178}
179
0c2498f1
SH
180/**
181 * struct pwm_ops - PWM controller operations
182 * @request: optional hook for requesting a PWM
183 * @free: optional hook for freeing a PWM
184 * @config: configure duty cycles and period length for this PWM
0aa0869c 185 * @set_polarity: configure the polarity of this PWM
0c2498f1
SH
186 * @enable: enable PWM output toggling
187 * @disable: disable PWM output toggling
62099abf 188 * @dbg_show: optional routine to show contents in debugfs
0c2498f1
SH
189 * @owner: helps prevent removal of modules exporting active PWMs
190 */
191struct pwm_ops {
6bc7064a
TR
192 int (*request)(struct pwm_chip *chip, struct pwm_device *pwm);
193 void (*free)(struct pwm_chip *chip, struct pwm_device *pwm);
194 int (*config)(struct pwm_chip *chip, struct pwm_device *pwm,
195 int duty_ns, int period_ns);
196 int (*set_polarity)(struct pwm_chip *chip, struct pwm_device *pwm,
197 enum pwm_polarity polarity);
198 int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm);
199 void (*disable)(struct pwm_chip *chip, struct pwm_device *pwm);
62099abf 200#ifdef CONFIG_DEBUG_FS
6bc7064a 201 void (*dbg_show)(struct pwm_chip *chip, struct seq_file *s);
62099abf 202#endif
6bc7064a 203 struct module *owner;
0c2498f1
SH
204};
205
206/**
f051c466
TR
207 * struct pwm_chip - abstract a PWM controller
208 * @dev: device providing the PWMs
209 * @list: list node for internal use
210 * @ops: callbacks for this PWM controller
211 * @base: number of first PWM controlled by this chip
212 * @npwm: number of PWMs controlled by this chip
213 * @pwms: array of PWM devices allocated by the framework
04883802
TR
214 * @of_xlate: request a PWM device given a device tree PWM specifier
215 * @of_pwm_n_cells: number of cells expected in the device tree PWM specifier
6e69ab13
FV
216 * @can_sleep: must be true if the .config(), .enable() or .disable()
217 * operations may sleep
0c2498f1
SH
218 */
219struct pwm_chip {
6bc7064a
TR
220 struct device *dev;
221 struct list_head list;
222 const struct pwm_ops *ops;
223 int base;
224 unsigned int npwm;
225
226 struct pwm_device *pwms;
227
228 struct pwm_device * (*of_xlate)(struct pwm_chip *pc,
229 const struct of_phandle_args *args);
230 unsigned int of_pwm_n_cells;
231 bool can_sleep;
0c2498f1
SH
232};
233
0bcf168b 234#if IS_ENABLED(CONFIG_PWM)
f051c466
TR
235int pwm_set_chip_data(struct pwm_device *pwm, void *data);
236void *pwm_get_chip_data(struct pwm_device *pwm);
237
b6a00fae
TK
238int pwmchip_add_with_polarity(struct pwm_chip *chip,
239 enum pwm_polarity polarity);
0c2498f1
SH
240int pwmchip_add(struct pwm_chip *chip);
241int pwmchip_remove(struct pwm_chip *chip);
f051c466
TR
242struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
243 unsigned int index,
244 const char *label);
8138d2dd 245
83af2402
PA
246struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *pc,
247 const struct of_phandle_args *args);
248
d4c0c470 249struct pwm_device *pwm_get(struct device *dev, const char *con_id);
8eb96127 250struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id);
8138d2dd
TR
251void pwm_put(struct pwm_device *pwm);
252
d4c0c470 253struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
261a5edd
PU
254struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
255 const char *con_id);
6354316d 256void devm_pwm_put(struct device *dev, struct pwm_device *pwm);
6e69ab13
FV
257
258bool pwm_can_sleep(struct pwm_device *pwm);
0bcf168b
TB
259#else
260static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data)
261{
262 return -EINVAL;
263}
264
265static inline void *pwm_get_chip_data(struct pwm_device *pwm)
266{
267 return NULL;
268}
269
270static inline int pwmchip_add(struct pwm_chip *chip)
271{
272 return -EINVAL;
273}
274
b6a00fae
TK
275static inline int pwmchip_add_inversed(struct pwm_chip *chip)
276{
277 return -EINVAL;
278}
279
0bcf168b
TB
280static inline int pwmchip_remove(struct pwm_chip *chip)
281{
282 return -EINVAL;
283}
284
285static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
286 unsigned int index,
287 const char *label)
288{
289 return ERR_PTR(-ENODEV);
290}
291
292static inline struct pwm_device *pwm_get(struct device *dev,
293 const char *consumer)
294{
295 return ERR_PTR(-ENODEV);
296}
297
8eb96127
PU
298static inline struct pwm_device *of_pwm_get(struct device_node *np,
299 const char *con_id)
300{
301 return ERR_PTR(-ENODEV);
302}
303
0bcf168b
TB
304static inline void pwm_put(struct pwm_device *pwm)
305{
306}
307
308static inline struct pwm_device *devm_pwm_get(struct device *dev,
309 const char *consumer)
310{
311 return ERR_PTR(-ENODEV);
312}
313
261a5edd
PU
314static inline struct pwm_device *devm_of_pwm_get(struct device *dev,
315 struct device_node *np,
316 const char *con_id)
317{
318 return ERR_PTR(-ENODEV);
319}
320
0bcf168b
TB
321static inline void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
322{
323}
6e69ab13
FV
324
325static inline bool pwm_can_sleep(struct pwm_device *pwm)
326{
327 return false;
328}
0bcf168b 329#endif
6354316d 330
8138d2dd
TR
331struct pwm_lookup {
332 struct list_head list;
333 const char *provider;
334 unsigned int index;
335 const char *dev_id;
336 const char *con_id;
3796ce1d
AB
337 unsigned int period;
338 enum pwm_polarity polarity;
8138d2dd
TR
339};
340
42844029 341#define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
8138d2dd
TR
342 { \
343 .provider = _provider, \
344 .index = _index, \
345 .dev_id = _dev_id, \
346 .con_id = _con_id, \
42844029
AB
347 .period = _period, \
348 .polarity = _polarity \
8138d2dd
TR
349 }
350
0bcf168b 351#if IS_ENABLED(CONFIG_PWM)
8138d2dd 352void pwm_add_table(struct pwm_lookup *table, size_t num);
efb0de55 353void pwm_remove_table(struct pwm_lookup *table, size_t num);
0bcf168b
TB
354#else
355static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
356{
357}
efb0de55
SK
358
359static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
360{
361}
0c2498f1
SH
362#endif
363
76abbdde
HS
364#ifdef CONFIG_PWM_SYSFS
365void pwmchip_sysfs_export(struct pwm_chip *chip);
366void pwmchip_sysfs_unexport(struct pwm_chip *chip);
367#else
368static inline void pwmchip_sysfs_export(struct pwm_chip *chip)
369{
370}
371
372static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip)
373{
374}
375#endif /* CONFIG_PWM_SYSFS */
376
5243ef8b 377#endif /* __LINUX_PWM_H */
This page took 2.299059 seconds and 5 git commands to generate.