pwm: Move the enabled/disabled info into pwm_state
[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,
09a7e4a3 97 PWMF_EXPORTED = 1 << 1,
f051c466
TR
98};
99
43a276b0
BB
100/*
101 * struct pwm_state - state of a PWM channel
102 * @period: PWM period (in nanoseconds)
103 * @duty_cycle: PWM duty cycle (in nanoseconds)
104 * @polarity: PWM polarity
09a7e4a3 105 * @enabled: PWM enabled status
43a276b0
BB
106 */
107struct pwm_state {
108 unsigned int period;
109 unsigned int duty_cycle;
110 enum pwm_polarity polarity;
09a7e4a3 111 bool enabled;
43a276b0
BB
112};
113
04883802
TR
114/**
115 * struct pwm_device - PWM channel object
116 * @label: name of the PWM device
117 * @flags: flags associated with the PWM device
118 * @hwpwm: per-chip relative index of the PWM device
119 * @pwm: global index of the PWM device
120 * @chip: PWM chip providing this PWM device
121 * @chip_data: chip-private data associated with the PWM device
e39c0df1 122 * @args: PWM arguments
43a276b0 123 * @state: curent PWM channel state
04883802 124 */
f051c466 125struct pwm_device {
6bc7064a
TR
126 const char *label;
127 unsigned long flags;
128 unsigned int hwpwm;
129 unsigned int pwm;
130 struct pwm_chip *chip;
131 void *chip_data;
132
e39c0df1 133 struct pwm_args args;
43a276b0 134 struct pwm_state state;
f051c466
TR
135};
136
43a276b0
BB
137/**
138 * pwm_get_state() - retrieve the current PWM state
139 * @pwm: PWM device
140 * @state: state to fill with the current PWM state
141 */
142static inline void pwm_get_state(const struct pwm_device *pwm,
143 struct pwm_state *state)
144{
145 *state = pwm->state;
146}
147
5c31252c
BB
148static inline bool pwm_is_enabled(const struct pwm_device *pwm)
149{
09a7e4a3
BB
150 struct pwm_state state;
151
152 pwm_get_state(pwm, &state);
153
154 return state.enabled;
5c31252c
BB
155}
156
f051c466
TR
157static inline void pwm_set_period(struct pwm_device *pwm, unsigned int period)
158{
159 if (pwm)
43a276b0 160 pwm->state.period = period;
f051c466
TR
161}
162
a1cf4217 163static inline unsigned int pwm_get_period(const struct pwm_device *pwm)
f051c466 164{
43a276b0
BB
165 struct pwm_state state;
166
167 pwm_get_state(pwm, &state);
168
169 return state.period;
f051c466
TR
170}
171
76abbdde
HS
172static inline void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty)
173{
174 if (pwm)
43a276b0 175 pwm->state.duty_cycle = duty;
76abbdde
HS
176}
177
a1cf4217 178static inline unsigned int pwm_get_duty_cycle(const struct pwm_device *pwm)
76abbdde 179{
43a276b0
BB
180 struct pwm_state state;
181
182 pwm_get_state(pwm, &state);
183
184 return state.duty_cycle;
76abbdde
HS
185}
186
0aa0869c
PA
187/*
188 * pwm_set_polarity - configure the polarity of a PWM signal
189 */
190int pwm_set_polarity(struct pwm_device *pwm, enum pwm_polarity polarity);
191
011e7631
BB
192static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm)
193{
43a276b0
BB
194 struct pwm_state state;
195
196 pwm_get_state(pwm, &state);
197
198 return state.polarity;
011e7631
BB
199}
200
e39c0df1
BB
201static inline void pwm_get_args(const struct pwm_device *pwm,
202 struct pwm_args *args)
203{
204 *args = pwm->args;
205}
206
207static inline void pwm_apply_args(struct pwm_device *pwm)
208{
e39c0df1
BB
209 pwm_set_polarity(pwm, pwm->args.polarity);
210}
211
0c2498f1
SH
212/**
213 * struct pwm_ops - PWM controller operations
214 * @request: optional hook for requesting a PWM
215 * @free: optional hook for freeing a PWM
216 * @config: configure duty cycles and period length for this PWM
0aa0869c 217 * @set_polarity: configure the polarity of this PWM
0c2498f1
SH
218 * @enable: enable PWM output toggling
219 * @disable: disable PWM output toggling
62099abf 220 * @dbg_show: optional routine to show contents in debugfs
0c2498f1
SH
221 * @owner: helps prevent removal of modules exporting active PWMs
222 */
223struct pwm_ops {
6bc7064a
TR
224 int (*request)(struct pwm_chip *chip, struct pwm_device *pwm);
225 void (*free)(struct pwm_chip *chip, struct pwm_device *pwm);
226 int (*config)(struct pwm_chip *chip, struct pwm_device *pwm,
227 int duty_ns, int period_ns);
228 int (*set_polarity)(struct pwm_chip *chip, struct pwm_device *pwm,
229 enum pwm_polarity polarity);
230 int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm);
231 void (*disable)(struct pwm_chip *chip, struct pwm_device *pwm);
62099abf 232#ifdef CONFIG_DEBUG_FS
6bc7064a 233 void (*dbg_show)(struct pwm_chip *chip, struct seq_file *s);
62099abf 234#endif
6bc7064a 235 struct module *owner;
0c2498f1
SH
236};
237
238/**
f051c466
TR
239 * struct pwm_chip - abstract a PWM controller
240 * @dev: device providing the PWMs
241 * @list: list node for internal use
242 * @ops: callbacks for this PWM controller
243 * @base: number of first PWM controlled by this chip
244 * @npwm: number of PWMs controlled by this chip
245 * @pwms: array of PWM devices allocated by the framework
04883802
TR
246 * @of_xlate: request a PWM device given a device tree PWM specifier
247 * @of_pwm_n_cells: number of cells expected in the device tree PWM specifier
6e69ab13
FV
248 * @can_sleep: must be true if the .config(), .enable() or .disable()
249 * operations may sleep
0c2498f1
SH
250 */
251struct pwm_chip {
6bc7064a
TR
252 struct device *dev;
253 struct list_head list;
254 const struct pwm_ops *ops;
255 int base;
256 unsigned int npwm;
257
258 struct pwm_device *pwms;
259
260 struct pwm_device * (*of_xlate)(struct pwm_chip *pc,
261 const struct of_phandle_args *args);
262 unsigned int of_pwm_n_cells;
263 bool can_sleep;
0c2498f1
SH
264};
265
0bcf168b 266#if IS_ENABLED(CONFIG_PWM)
f051c466
TR
267int pwm_set_chip_data(struct pwm_device *pwm, void *data);
268void *pwm_get_chip_data(struct pwm_device *pwm);
269
b6a00fae
TK
270int pwmchip_add_with_polarity(struct pwm_chip *chip,
271 enum pwm_polarity polarity);
0c2498f1
SH
272int pwmchip_add(struct pwm_chip *chip);
273int pwmchip_remove(struct pwm_chip *chip);
f051c466
TR
274struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
275 unsigned int index,
276 const char *label);
8138d2dd 277
83af2402
PA
278struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *pc,
279 const struct of_phandle_args *args);
280
d4c0c470 281struct pwm_device *pwm_get(struct device *dev, const char *con_id);
8eb96127 282struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id);
8138d2dd
TR
283void pwm_put(struct pwm_device *pwm);
284
d4c0c470 285struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
261a5edd
PU
286struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
287 const char *con_id);
6354316d 288void devm_pwm_put(struct device *dev, struct pwm_device *pwm);
6e69ab13
FV
289
290bool pwm_can_sleep(struct pwm_device *pwm);
0bcf168b
TB
291#else
292static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data)
293{
294 return -EINVAL;
295}
296
297static inline void *pwm_get_chip_data(struct pwm_device *pwm)
298{
299 return NULL;
300}
301
302static inline int pwmchip_add(struct pwm_chip *chip)
303{
304 return -EINVAL;
305}
306
b6a00fae
TK
307static inline int pwmchip_add_inversed(struct pwm_chip *chip)
308{
309 return -EINVAL;
310}
311
0bcf168b
TB
312static inline int pwmchip_remove(struct pwm_chip *chip)
313{
314 return -EINVAL;
315}
316
317static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
318 unsigned int index,
319 const char *label)
320{
321 return ERR_PTR(-ENODEV);
322}
323
324static inline struct pwm_device *pwm_get(struct device *dev,
325 const char *consumer)
326{
327 return ERR_PTR(-ENODEV);
328}
329
8eb96127
PU
330static inline struct pwm_device *of_pwm_get(struct device_node *np,
331 const char *con_id)
332{
333 return ERR_PTR(-ENODEV);
334}
335
0bcf168b
TB
336static inline void pwm_put(struct pwm_device *pwm)
337{
338}
339
340static inline struct pwm_device *devm_pwm_get(struct device *dev,
341 const char *consumer)
342{
343 return ERR_PTR(-ENODEV);
344}
345
261a5edd
PU
346static inline struct pwm_device *devm_of_pwm_get(struct device *dev,
347 struct device_node *np,
348 const char *con_id)
349{
350 return ERR_PTR(-ENODEV);
351}
352
0bcf168b
TB
353static inline void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
354{
355}
6e69ab13
FV
356
357static inline bool pwm_can_sleep(struct pwm_device *pwm)
358{
359 return false;
360}
0bcf168b 361#endif
6354316d 362
8138d2dd
TR
363struct pwm_lookup {
364 struct list_head list;
365 const char *provider;
366 unsigned int index;
367 const char *dev_id;
368 const char *con_id;
3796ce1d
AB
369 unsigned int period;
370 enum pwm_polarity polarity;
8138d2dd
TR
371};
372
42844029 373#define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
8138d2dd
TR
374 { \
375 .provider = _provider, \
376 .index = _index, \
377 .dev_id = _dev_id, \
378 .con_id = _con_id, \
42844029
AB
379 .period = _period, \
380 .polarity = _polarity \
8138d2dd
TR
381 }
382
0bcf168b 383#if IS_ENABLED(CONFIG_PWM)
8138d2dd 384void pwm_add_table(struct pwm_lookup *table, size_t num);
efb0de55 385void pwm_remove_table(struct pwm_lookup *table, size_t num);
0bcf168b
TB
386#else
387static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
388{
389}
efb0de55
SK
390
391static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
392{
393}
0c2498f1
SH
394#endif
395
76abbdde
HS
396#ifdef CONFIG_PWM_SYSFS
397void pwmchip_sysfs_export(struct pwm_chip *chip);
398void pwmchip_sysfs_unexport(struct pwm_chip *chip);
399#else
400static inline void pwmchip_sysfs_export(struct pwm_chip *chip)
401{
402}
403
404static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip)
405{
406}
407#endif /* CONFIG_PWM_SYSFS */
408
5243ef8b 409#endif /* __LINUX_PWM_H */
This page took 1.850992 seconds and 5 git commands to generate.