]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - include/linux/pwm.h
pwm: Constify PWM device where possible
[mirror_ubuntu-artful-kernel.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>
7299ab70
TR
5#include <linux/of.h>
6
1a189b97 7struct pwm_device;
62099abf 8struct seq_file;
1a189b97 9
557fe99d 10#if IS_ENABLED(CONFIG_PWM)
1a189b97
RK
11/*
12 * pwm_request - request a PWM device
13 */
14struct pwm_device *pwm_request(int pwm_id, const char *label);
15
16/*
17 * pwm_free - free a PWM device
18 */
19void pwm_free(struct pwm_device *pwm);
20
21/*
22 * pwm_config - change a PWM device configuration
23 */
24int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns);
25
26/*
27 * pwm_enable - start a PWM output toggling
28 */
29int pwm_enable(struct pwm_device *pwm);
30
31/*
32 * pwm_disable - stop a PWM output toggling
33 */
34void pwm_disable(struct pwm_device *pwm);
0bcf168b
TB
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
1a189b97 59
0c2498f1
SH
60struct pwm_chip;
61
0aa0869c
PA
62/**
63 * enum pwm_polarity - polarity of a PWM signal
64 * @PWM_POLARITY_NORMAL: a high signal for the duration of the duty-
65 * cycle, followed by a low signal for the remainder of the pulse
66 * period
67 * @PWM_POLARITY_INVERSED: a low signal for the duration of the duty-
68 * cycle, followed by a high signal for the remainder of the pulse
69 * period
70 */
71enum pwm_polarity {
72 PWM_POLARITY_NORMAL,
73 PWM_POLARITY_INVERSED,
74};
75
f051c466
TR
76enum {
77 PWMF_REQUESTED = 1 << 0,
78 PWMF_ENABLED = 1 << 1,
76abbdde 79 PWMF_EXPORTED = 1 << 2,
f051c466
TR
80};
81
82struct pwm_device {
83 const char *label;
84 unsigned long flags;
85 unsigned int hwpwm;
86 unsigned int pwm;
87 struct pwm_chip *chip;
88 void *chip_data;
89
76abbdde
HS
90 unsigned int period; /* in nanoseconds */
91 unsigned int duty_cycle; /* in nanoseconds */
92 enum pwm_polarity polarity;
f051c466
TR
93};
94
5c31252c
BB
95static inline bool pwm_is_enabled(const struct pwm_device *pwm)
96{
97 return test_bit(PWMF_ENABLED, &pwm->flags);
98}
99
f051c466
TR
100static inline void pwm_set_period(struct pwm_device *pwm, unsigned int period)
101{
102 if (pwm)
103 pwm->period = period;
104}
105
a1cf4217 106static inline unsigned int pwm_get_period(const struct pwm_device *pwm)
f051c466
TR
107{
108 return pwm ? pwm->period : 0;
109}
110
76abbdde
HS
111static inline void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty)
112{
113 if (pwm)
114 pwm->duty_cycle = duty;
115}
116
a1cf4217 117static inline unsigned int pwm_get_duty_cycle(const struct pwm_device *pwm)
76abbdde
HS
118{
119 return pwm ? pwm->duty_cycle : 0;
120}
121
0aa0869c
PA
122/*
123 * pwm_set_polarity - configure the polarity of a PWM signal
124 */
125int pwm_set_polarity(struct pwm_device *pwm, enum pwm_polarity polarity);
126
0c2498f1
SH
127/**
128 * struct pwm_ops - PWM controller operations
129 * @request: optional hook for requesting a PWM
130 * @free: optional hook for freeing a PWM
131 * @config: configure duty cycles and period length for this PWM
0aa0869c 132 * @set_polarity: configure the polarity of this PWM
0c2498f1
SH
133 * @enable: enable PWM output toggling
134 * @disable: disable PWM output toggling
62099abf 135 * @dbg_show: optional routine to show contents in debugfs
0c2498f1
SH
136 * @owner: helps prevent removal of modules exporting active PWMs
137 */
138struct pwm_ops {
f051c466
TR
139 int (*request)(struct pwm_chip *chip,
140 struct pwm_device *pwm);
141 void (*free)(struct pwm_chip *chip,
142 struct pwm_device *pwm);
143 int (*config)(struct pwm_chip *chip,
144 struct pwm_device *pwm,
145 int duty_ns, int period_ns);
0aa0869c
PA
146 int (*set_polarity)(struct pwm_chip *chip,
147 struct pwm_device *pwm,
148 enum pwm_polarity polarity);
f051c466
TR
149 int (*enable)(struct pwm_chip *chip,
150 struct pwm_device *pwm);
151 void (*disable)(struct pwm_chip *chip,
152 struct pwm_device *pwm);
62099abf
TR
153#ifdef CONFIG_DEBUG_FS
154 void (*dbg_show)(struct pwm_chip *chip,
155 struct seq_file *s);
156#endif
0c2498f1
SH
157 struct module *owner;
158};
159
160/**
f051c466
TR
161 * struct pwm_chip - abstract a PWM controller
162 * @dev: device providing the PWMs
163 * @list: list node for internal use
164 * @ops: callbacks for this PWM controller
165 * @base: number of first PWM controlled by this chip
166 * @npwm: number of PWMs controlled by this chip
167 * @pwms: array of PWM devices allocated by the framework
6e69ab13
FV
168 * @can_sleep: must be true if the .config(), .enable() or .disable()
169 * operations may sleep
0c2498f1
SH
170 */
171struct pwm_chip {
f051c466
TR
172 struct device *dev;
173 struct list_head list;
174 const struct pwm_ops *ops;
175 int base;
176 unsigned int npwm;
177
178 struct pwm_device *pwms;
7299ab70
TR
179
180 struct pwm_device * (*of_xlate)(struct pwm_chip *pc,
181 const struct of_phandle_args *args);
182 unsigned int of_pwm_n_cells;
6e69ab13 183 bool can_sleep;
0c2498f1
SH
184};
185
0bcf168b 186#if IS_ENABLED(CONFIG_PWM)
f051c466
TR
187int pwm_set_chip_data(struct pwm_device *pwm, void *data);
188void *pwm_get_chip_data(struct pwm_device *pwm);
189
b6a00fae
TK
190int pwmchip_add_with_polarity(struct pwm_chip *chip,
191 enum pwm_polarity polarity);
0c2498f1
SH
192int pwmchip_add(struct pwm_chip *chip);
193int pwmchip_remove(struct pwm_chip *chip);
f051c466
TR
194struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
195 unsigned int index,
196 const char *label);
8138d2dd 197
83af2402
PA
198struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *pc,
199 const struct of_phandle_args *args);
200
d4c0c470 201struct pwm_device *pwm_get(struct device *dev, const char *con_id);
8eb96127 202struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id);
8138d2dd
TR
203void pwm_put(struct pwm_device *pwm);
204
d4c0c470 205struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
261a5edd
PU
206struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
207 const char *con_id);
6354316d 208void devm_pwm_put(struct device *dev, struct pwm_device *pwm);
6e69ab13
FV
209
210bool pwm_can_sleep(struct pwm_device *pwm);
0bcf168b
TB
211#else
212static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data)
213{
214 return -EINVAL;
215}
216
217static inline void *pwm_get_chip_data(struct pwm_device *pwm)
218{
219 return NULL;
220}
221
222static inline int pwmchip_add(struct pwm_chip *chip)
223{
224 return -EINVAL;
225}
226
b6a00fae
TK
227static inline int pwmchip_add_inversed(struct pwm_chip *chip)
228{
229 return -EINVAL;
230}
231
0bcf168b
TB
232static inline int pwmchip_remove(struct pwm_chip *chip)
233{
234 return -EINVAL;
235}
236
237static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
238 unsigned int index,
239 const char *label)
240{
241 return ERR_PTR(-ENODEV);
242}
243
244static inline struct pwm_device *pwm_get(struct device *dev,
245 const char *consumer)
246{
247 return ERR_PTR(-ENODEV);
248}
249
8eb96127
PU
250static inline struct pwm_device *of_pwm_get(struct device_node *np,
251 const char *con_id)
252{
253 return ERR_PTR(-ENODEV);
254}
255
0bcf168b
TB
256static inline void pwm_put(struct pwm_device *pwm)
257{
258}
259
260static inline struct pwm_device *devm_pwm_get(struct device *dev,
261 const char *consumer)
262{
263 return ERR_PTR(-ENODEV);
264}
265
261a5edd
PU
266static inline struct pwm_device *devm_of_pwm_get(struct device *dev,
267 struct device_node *np,
268 const char *con_id)
269{
270 return ERR_PTR(-ENODEV);
271}
272
0bcf168b
TB
273static inline void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
274{
275}
6e69ab13
FV
276
277static inline bool pwm_can_sleep(struct pwm_device *pwm)
278{
279 return false;
280}
0bcf168b 281#endif
6354316d 282
8138d2dd
TR
283struct pwm_lookup {
284 struct list_head list;
285 const char *provider;
286 unsigned int index;
287 const char *dev_id;
288 const char *con_id;
3796ce1d
AB
289 unsigned int period;
290 enum pwm_polarity polarity;
8138d2dd
TR
291};
292
42844029 293#define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
8138d2dd
TR
294 { \
295 .provider = _provider, \
296 .index = _index, \
297 .dev_id = _dev_id, \
298 .con_id = _con_id, \
42844029
AB
299 .period = _period, \
300 .polarity = _polarity \
8138d2dd
TR
301 }
302
0bcf168b 303#if IS_ENABLED(CONFIG_PWM)
8138d2dd 304void pwm_add_table(struct pwm_lookup *table, size_t num);
efb0de55 305void pwm_remove_table(struct pwm_lookup *table, size_t num);
0bcf168b
TB
306#else
307static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
308{
309}
efb0de55
SK
310
311static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
312{
313}
0c2498f1
SH
314#endif
315
76abbdde
HS
316#ifdef CONFIG_PWM_SYSFS
317void pwmchip_sysfs_export(struct pwm_chip *chip);
318void pwmchip_sysfs_unexport(struct pwm_chip *chip);
319#else
320static inline void pwmchip_sysfs_export(struct pwm_chip *chip)
321{
322}
323
324static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip)
325{
326}
327#endif /* CONFIG_PWM_SYSFS */
328
5243ef8b 329#endif /* __LINUX_PWM_H */