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