Rev 6936 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
6082 | serge | 1 | #ifndef __LINUX_PWM_H |
2 | #define __LINUX_PWM_H |
||
3 | |||
4 | #include |
||
5 | #include |
||
6 | //#include |
||
7 | |||
8 | struct device; |
||
7143 | serge | 9 | struct device_node; |
6082 | serge | 10 | struct pwm_device; |
11 | struct seq_file; |
||
12 | |||
13 | #if IS_ENABLED(CONFIG_PWM) |
||
14 | /* |
||
15 | * pwm_request - request a PWM device |
||
16 | */ |
||
17 | struct pwm_device *pwm_request(int pwm_id, const char *label); |
||
18 | |||
19 | /* |
||
20 | * pwm_free - free a PWM device |
||
21 | */ |
||
22 | void pwm_free(struct pwm_device *pwm); |
||
23 | |||
24 | /* |
||
25 | * pwm_config - change a PWM device configuration |
||
26 | */ |
||
27 | int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns); |
||
28 | |||
29 | /* |
||
30 | * pwm_enable - start a PWM output toggling |
||
31 | */ |
||
32 | int pwm_enable(struct pwm_device *pwm); |
||
33 | |||
34 | /* |
||
35 | * pwm_disable - stop a PWM output toggling |
||
36 | */ |
||
37 | void pwm_disable(struct pwm_device *pwm); |
||
38 | #else |
||
39 | static inline struct pwm_device *pwm_request(int pwm_id, const char *label) |
||
40 | { |
||
41 | return ERR_PTR(-ENODEV); |
||
42 | } |
||
43 | |||
44 | static inline void pwm_free(struct pwm_device *pwm) |
||
45 | { |
||
46 | } |
||
47 | |||
48 | static inline int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns) |
||
49 | { |
||
50 | return -EINVAL; |
||
51 | } |
||
52 | |||
53 | static inline int pwm_enable(struct pwm_device *pwm) |
||
54 | { |
||
55 | return -EINVAL; |
||
56 | } |
||
57 | |||
58 | static inline void pwm_disable(struct pwm_device *pwm) |
||
59 | { |
||
60 | } |
||
61 | #endif |
||
62 | |||
63 | struct pwm_chip; |
||
64 | |||
65 | /** |
||
66 | * enum pwm_polarity - polarity of a PWM signal |
||
67 | * @PWM_POLARITY_NORMAL: a high signal for the duration of the duty- |
||
68 | * cycle, followed by a low signal for the remainder of the pulse |
||
69 | * period |
||
70 | * @PWM_POLARITY_INVERSED: a low signal for the duration of the duty- |
||
71 | * cycle, followed by a high signal for the remainder of the pulse |
||
72 | * period |
||
73 | */ |
||
74 | enum pwm_polarity { |
||
75 | PWM_POLARITY_NORMAL, |
||
76 | PWM_POLARITY_INVERSED, |
||
77 | }; |
||
78 | |||
79 | enum { |
||
80 | PWMF_REQUESTED = 1 << 0, |
||
81 | PWMF_ENABLED = 1 << 1, |
||
82 | PWMF_EXPORTED = 1 << 2, |
||
83 | }; |
||
84 | |||
85 | /** |
||
86 | * struct pwm_device - PWM channel object |
||
87 | * @label: name of the PWM device |
||
88 | * @flags: flags associated with the PWM device |
||
89 | * @hwpwm: per-chip relative index of the PWM device |
||
90 | * @pwm: global index of the PWM device |
||
91 | * @chip: PWM chip providing this PWM device |
||
92 | * @chip_data: chip-private data associated with the PWM device |
||
93 | * @lock: used to serialize accesses to the PWM device where necessary |
||
94 | * @period: period of the PWM signal (in nanoseconds) |
||
95 | * @duty_cycle: duty cycle of the PWM signal (in nanoseconds) |
||
96 | * @polarity: polarity of the PWM signal |
||
97 | */ |
||
98 | struct pwm_device { |
||
99 | const char *label; |
||
100 | unsigned long flags; |
||
101 | unsigned int hwpwm; |
||
102 | unsigned int pwm; |
||
103 | struct pwm_chip *chip; |
||
104 | void *chip_data; |
||
105 | struct mutex lock; |
||
106 | |||
107 | unsigned int period; |
||
108 | unsigned int duty_cycle; |
||
109 | enum pwm_polarity polarity; |
||
110 | }; |
||
111 | |||
112 | static inline bool pwm_is_enabled(const struct pwm_device *pwm) |
||
113 | { |
||
114 | return test_bit(PWMF_ENABLED, &pwm->flags); |
||
115 | } |
||
116 | |||
117 | static inline void pwm_set_period(struct pwm_device *pwm, unsigned int period) |
||
118 | { |
||
119 | if (pwm) |
||
120 | pwm->period = period; |
||
121 | } |
||
122 | |||
123 | static inline unsigned int pwm_get_period(const struct pwm_device *pwm) |
||
124 | { |
||
125 | return pwm ? pwm->period : 0; |
||
126 | } |
||
127 | |||
128 | static inline void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty) |
||
129 | { |
||
130 | if (pwm) |
||
131 | pwm->duty_cycle = duty; |
||
132 | } |
||
133 | |||
134 | static inline unsigned int pwm_get_duty_cycle(const struct pwm_device *pwm) |
||
135 | { |
||
136 | return pwm ? pwm->duty_cycle : 0; |
||
137 | } |
||
138 | |||
139 | /* |
||
140 | * pwm_set_polarity - configure the polarity of a PWM signal |
||
141 | */ |
||
142 | int pwm_set_polarity(struct pwm_device *pwm, enum pwm_polarity polarity); |
||
143 | |||
144 | static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm) |
||
145 | { |
||
146 | return pwm ? pwm->polarity : PWM_POLARITY_NORMAL; |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * struct pwm_ops - PWM controller operations |
||
151 | * @request: optional hook for requesting a PWM |
||
152 | * @free: optional hook for freeing a PWM |
||
153 | * @config: configure duty cycles and period length for this PWM |
||
154 | * @set_polarity: configure the polarity of this PWM |
||
155 | * @enable: enable PWM output toggling |
||
156 | * @disable: disable PWM output toggling |
||
157 | * @dbg_show: optional routine to show contents in debugfs |
||
158 | * @owner: helps prevent removal of modules exporting active PWMs |
||
159 | */ |
||
160 | struct pwm_ops { |
||
161 | int (*request)(struct pwm_chip *chip, struct pwm_device *pwm); |
||
162 | void (*free)(struct pwm_chip *chip, struct pwm_device *pwm); |
||
163 | int (*config)(struct pwm_chip *chip, struct pwm_device *pwm, |
||
164 | int duty_ns, int period_ns); |
||
165 | int (*set_polarity)(struct pwm_chip *chip, struct pwm_device *pwm, |
||
166 | enum pwm_polarity polarity); |
||
167 | int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm); |
||
168 | void (*disable)(struct pwm_chip *chip, struct pwm_device *pwm); |
||
169 | #ifdef CONFIG_DEBUG_FS |
||
170 | void (*dbg_show)(struct pwm_chip *chip, struct seq_file *s); |
||
171 | #endif |
||
172 | struct module *owner; |
||
173 | }; |
||
174 | |||
175 | #if IS_ENABLED(CONFIG_PWM) |
||
176 | int pwm_set_chip_data(struct pwm_device *pwm, void *data); |
||
177 | void *pwm_get_chip_data(struct pwm_device *pwm); |
||
178 | struct pwm_device *pwm_get(struct device *dev, const char *con_id); |
||
179 | struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id); |
||
180 | void pwm_put(struct pwm_device *pwm); |
||
181 | |||
182 | struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id); |
||
6936 | serge | 183 | struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np, |
184 | const char *con_id); |
||
6082 | serge | 185 | void devm_pwm_put(struct device *dev, struct pwm_device *pwm); |
186 | |||
187 | bool pwm_can_sleep(struct pwm_device *pwm); |
||
188 | #else |
||
189 | static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data) |
||
190 | { |
||
191 | return -EINVAL; |
||
192 | } |
||
193 | |||
194 | static inline void *pwm_get_chip_data(struct pwm_device *pwm) |
||
195 | { |
||
196 | return NULL; |
||
197 | } |
||
6936 | serge | 198 | |
199 | static inline int pwmchip_add(struct pwm_chip *chip) |
||
200 | { |
||
201 | return -EINVAL; |
||
202 | } |
||
203 | |||
204 | static inline int pwmchip_add_inversed(struct pwm_chip *chip) |
||
205 | { |
||
206 | return -EINVAL; |
||
207 | } |
||
208 | |||
209 | static inline int pwmchip_remove(struct pwm_chip *chip) |
||
210 | { |
||
211 | return -EINVAL; |
||
212 | } |
||
213 | |||
214 | static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip, |
||
215 | unsigned int index, |
||
216 | const char *label) |
||
217 | { |
||
218 | return ERR_PTR(-ENODEV); |
||
219 | } |
||
220 | |||
6082 | serge | 221 | static inline struct pwm_device *pwm_get(struct device *dev, |
222 | const char *consumer) |
||
223 | { |
||
224 | return ERR_PTR(-ENODEV); |
||
225 | } |
||
6936 | serge | 226 | |
7143 | serge | 227 | static inline struct pwm_device *of_pwm_get(struct device_node *np, |
228 | const char *con_id) |
||
229 | { |
||
230 | return ERR_PTR(-ENODEV); |
||
231 | } |
||
6936 | serge | 232 | |
6082 | serge | 233 | static inline void pwm_put(struct pwm_device *pwm) |
234 | { |
||
235 | } |
||
236 | |||
237 | static inline struct pwm_device *devm_pwm_get(struct device *dev, |
||
238 | const char *consumer) |
||
239 | { |
||
240 | return ERR_PTR(-ENODEV); |
||
241 | } |
||
6936 | serge | 242 | |
7143 | serge | 243 | static inline struct pwm_device *devm_of_pwm_get(struct device *dev, |
244 | struct device_node *np, |
||
245 | const char *con_id) |
||
246 | { |
||
247 | return ERR_PTR(-ENODEV); |
||
248 | } |
||
6936 | serge | 249 | |
6082 | serge | 250 | static inline void devm_pwm_put(struct device *dev, struct pwm_device *pwm) |
251 | { |
||
252 | } |
||
253 | |||
254 | static inline bool pwm_can_sleep(struct pwm_device *pwm) |
||
255 | { |
||
256 | return false; |
||
257 | } |
||
258 | #endif |
||
259 | |||
260 | #endif /* __LINUX_PWM_H */><>><>><> |