UPSTREAM: pwm: Add PWM capture support
[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/mutex.h>
6 #include <linux/of.h>
7
8 struct pwm_capture;
9 struct seq_file;
10
11 struct pwm_chip;
12
13 /**
14  * enum pwm_polarity - polarity of a PWM signal
15  * @PWM_POLARITY_NORMAL: a high signal for the duration of the duty-
16  * cycle, followed by a low signal for the remainder of the pulse
17  * period
18  * @PWM_POLARITY_INVERSED: a low signal for the duration of the duty-
19  * cycle, followed by a high signal for the remainder of the pulse
20  * period
21  */
22 enum pwm_polarity {
23         PWM_POLARITY_NORMAL,
24         PWM_POLARITY_INVERSED,
25 };
26
27 /**
28  * struct pwm_args - board-dependent PWM arguments
29  * @period: reference period
30  * @polarity: reference polarity
31  *
32  * This structure describes board-dependent arguments attached to a PWM
33  * device. These arguments are usually retrieved from the PWM lookup table or
34  * device tree.
35  *
36  * Do not confuse this with the PWM state: PWM arguments represent the initial
37  * configuration that users want to use on this PWM device rather than the
38  * current PWM hardware state.
39  */
40 struct pwm_args {
41         unsigned int period;
42         enum pwm_polarity polarity;
43 };
44
45 enum {
46         PWMF_REQUESTED = 1 << 0,
47         PWMF_EXPORTED = 1 << 1,
48 };
49
50 /*
51  * struct pwm_state - state of a PWM channel
52  * @period: PWM period (in nanoseconds)
53  * @duty_cycle: PWM duty cycle (in nanoseconds)
54  * @polarity: PWM polarity
55  * @enabled: PWM enabled status
56  */
57 struct pwm_state {
58         unsigned int period;
59         unsigned int duty_cycle;
60         enum pwm_polarity polarity;
61         bool enabled;
62 };
63
64 /**
65  * struct pwm_device - PWM channel object
66  * @label: name of the PWM device
67  * @flags: flags associated with the PWM device
68  * @hwpwm: per-chip relative index of the PWM device
69  * @pwm: global index of the PWM device
70  * @chip: PWM chip providing this PWM device
71  * @chip_data: chip-private data associated with the PWM device
72  * @args: PWM arguments
73  * @state: curent PWM channel state
74  */
75 struct pwm_device {
76         const char *label;
77         unsigned long flags;
78         unsigned int hwpwm;
79         unsigned int pwm;
80         struct pwm_chip *chip;
81         void *chip_data;
82
83         struct pwm_args args;
84         struct pwm_state state;
85 };
86
87 /**
88  * pwm_get_state() - retrieve the current PWM state
89  * @pwm: PWM device
90  * @state: state to fill with the current PWM state
91  */
92 static inline void pwm_get_state(const struct pwm_device *pwm,
93                                  struct pwm_state *state)
94 {
95         *state = pwm->state;
96 }
97
98 static inline bool pwm_is_enabled(const struct pwm_device *pwm)
99 {
100         struct pwm_state state;
101
102         pwm_get_state(pwm, &state);
103
104         return state.enabled;
105 }
106
107 static inline void pwm_set_period(struct pwm_device *pwm, unsigned int period)
108 {
109         if (pwm)
110                 pwm->state.period = period;
111 }
112
113 static inline unsigned int pwm_get_period(const struct pwm_device *pwm)
114 {
115         struct pwm_state state;
116
117         pwm_get_state(pwm, &state);
118
119         return state.period;
120 }
121
122 static inline void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty)
123 {
124         if (pwm)
125                 pwm->state.duty_cycle = duty;
126 }
127
128 static inline unsigned int pwm_get_duty_cycle(const struct pwm_device *pwm)
129 {
130         struct pwm_state state;
131
132         pwm_get_state(pwm, &state);
133
134         return state.duty_cycle;
135 }
136
137 static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm)
138 {
139         struct pwm_state state;
140
141         pwm_get_state(pwm, &state);
142
143         return state.polarity;
144 }
145
146 static inline void pwm_get_args(const struct pwm_device *pwm,
147                                 struct pwm_args *args)
148 {
149         *args = pwm->args;
150 }
151
152 /**
153  * struct pwm_ops - PWM controller operations
154  * @request: optional hook for requesting a PWM
155  * @free: optional hook for freeing a PWM
156  * @config: configure duty cycles and period length for this PWM
157  * @set_polarity: configure the polarity of this PWM
158  * @capture: capture and report PWM signal
159  * @enable: enable PWM output toggling
160  * @disable: disable PWM output toggling
161  * @apply: atomically apply a new PWM config. The state argument
162  *         should be adjusted with the real hardware config (if the
163  *         approximate the period or duty_cycle value, state should
164  *         reflect it)
165  * @get_state: get the current PWM state. This function is only
166  *             called once per PWM device when the PWM chip is
167  *             registered.
168  * @dbg_show: optional routine to show contents in debugfs
169  * @owner: helps prevent removal of modules exporting active PWMs
170  */
171 struct pwm_ops {
172         int (*request)(struct pwm_chip *chip, struct pwm_device *pwm);
173         void (*free)(struct pwm_chip *chip, struct pwm_device *pwm);
174         int (*config)(struct pwm_chip *chip, struct pwm_device *pwm,
175                       int duty_ns, int period_ns);
176         int (*set_polarity)(struct pwm_chip *chip, struct pwm_device *pwm,
177                             enum pwm_polarity polarity);
178         int (*capture)(struct pwm_chip *chip, struct pwm_device *pwm,
179                        struct pwm_capture *result, unsigned long timeout);
180         int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm);
181         void (*disable)(struct pwm_chip *chip, struct pwm_device *pwm);
182         int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm,
183                      struct pwm_state *state);
184         void (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,
185                           struct pwm_state *state);
186 #ifdef CONFIG_DEBUG_FS
187         void (*dbg_show)(struct pwm_chip *chip, struct seq_file *s);
188 #endif
189         struct module *owner;
190 };
191
192 /**
193  * struct pwm_chip - abstract a PWM controller
194  * @dev: device providing the PWMs
195  * @list: list node for internal use
196  * @ops: callbacks for this PWM controller
197  * @base: number of first PWM controlled by this chip
198  * @npwm: number of PWMs controlled by this chip
199  * @pwms: array of PWM devices allocated by the framework
200  * @of_xlate: request a PWM device given a device tree PWM specifier
201  * @of_pwm_n_cells: number of cells expected in the device tree PWM specifier
202  * @can_sleep: must be true if the .config(), .enable() or .disable()
203  *             operations may sleep
204  */
205 struct pwm_chip {
206         struct device *dev;
207         struct list_head list;
208         const struct pwm_ops *ops;
209         int base;
210         unsigned int npwm;
211
212         struct pwm_device *pwms;
213
214         struct pwm_device * (*of_xlate)(struct pwm_chip *pc,
215                                         const struct of_phandle_args *args);
216         unsigned int of_pwm_n_cells;
217         bool can_sleep;
218 };
219
220 /**
221  * struct pwm_capture - PWM capture data
222  * @period: period of the PWM signal (in nanoseconds)
223  * @duty_cycle: duty cycle of the PWM signal (in nanoseconds)
224  */
225 struct pwm_capture {
226         unsigned int period;
227         unsigned int duty_cycle;
228 };
229
230 #if IS_ENABLED(CONFIG_PWM)
231 /* PWM user APIs */
232 struct pwm_device *pwm_request(int pwm_id, const char *label);
233 void pwm_free(struct pwm_device *pwm);
234 int pwm_apply_state(struct pwm_device *pwm, struct pwm_state *state);
235 int pwm_adjust_config(struct pwm_device *pwm);
236
237 /**
238  * pwm_config() - change a PWM device configuration
239  * @pwm: PWM device
240  * @duty_ns: "on" time (in nanoseconds)
241  * @period_ns: duration (in nanoseconds) of one cycle
242  *
243  * Returns: 0 on success or a negative error code on failure.
244  */
245 static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
246                              int period_ns)
247 {
248         struct pwm_state state;
249
250         if (!pwm)
251                 return -EINVAL;
252
253         if (duty_ns < 0 || period_ns < 0)
254                 return -EINVAL;
255
256         pwm_get_state(pwm, &state);
257         if (state.duty_cycle == duty_ns && state.period == period_ns)
258                 return 0;
259
260         state.duty_cycle = duty_ns;
261         state.period = period_ns;
262         return pwm_apply_state(pwm, &state);
263 }
264
265 /**
266  * pwm_set_polarity() - configure the polarity of a PWM signal
267  * @pwm: PWM device
268  * @polarity: new polarity of the PWM signal
269  *
270  * Note that the polarity cannot be configured while the PWM device is
271  * enabled.
272  *
273  * Returns: 0 on success or a negative error code on failure.
274  */
275 static inline int pwm_set_polarity(struct pwm_device *pwm,
276                                    enum pwm_polarity polarity)
277 {
278         struct pwm_state state;
279
280         if (!pwm)
281                 return -EINVAL;
282
283         pwm_get_state(pwm, &state);
284         if (state.polarity == polarity)
285                 return 0;
286
287         /*
288          * Changing the polarity of a running PWM without adjusting the
289          * dutycycle/period value is a bit risky (can introduce glitches).
290          * Return -EBUSY in this case.
291          * Note that this is allowed when using pwm_apply_state() because
292          * the user specifies all the parameters.
293          */
294         if (state.enabled)
295                 return -EBUSY;
296
297         state.polarity = polarity;
298         return pwm_apply_state(pwm, &state);
299 }
300
301 /**
302  * pwm_enable() - start a PWM output toggling
303  * @pwm: PWM device
304  *
305  * Returns: 0 on success or a negative error code on failure.
306  */
307 static inline int pwm_enable(struct pwm_device *pwm)
308 {
309         struct pwm_state state;
310
311         if (!pwm)
312                 return -EINVAL;
313
314         pwm_get_state(pwm, &state);
315         if (state.enabled)
316                 return 0;
317
318         state.enabled = true;
319         return pwm_apply_state(pwm, &state);
320 }
321
322 /**
323  * pwm_disable() - stop a PWM output toggling
324  * @pwm: PWM device
325  */
326 static inline void pwm_disable(struct pwm_device *pwm)
327 {
328         struct pwm_state state;
329
330         if (!pwm)
331                 return;
332
333         pwm_get_state(pwm, &state);
334         if (!state.enabled)
335                 return;
336
337         state.enabled = false;
338         pwm_apply_state(pwm, &state);
339 }
340
341
342 /* PWM provider APIs */
343 int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
344                 unsigned long timeout);
345 int pwm_set_chip_data(struct pwm_device *pwm, void *data);
346 void *pwm_get_chip_data(struct pwm_device *pwm);
347
348 int pwmchip_add_with_polarity(struct pwm_chip *chip,
349                               enum pwm_polarity polarity);
350 int pwmchip_add(struct pwm_chip *chip);
351 int pwmchip_remove(struct pwm_chip *chip);
352 struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
353                                          unsigned int index,
354                                          const char *label);
355
356 struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *pc,
357                 const struct of_phandle_args *args);
358
359 struct pwm_device *pwm_get(struct device *dev, const char *con_id);
360 struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id);
361 void pwm_put(struct pwm_device *pwm);
362
363 struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
364 struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
365                                    const char *con_id);
366 void devm_pwm_put(struct device *dev, struct pwm_device *pwm);
367
368 bool pwm_can_sleep(struct pwm_device *pwm);
369 #else
370 static inline struct pwm_device *pwm_request(int pwm_id, const char *label)
371 {
372         return ERR_PTR(-ENODEV);
373 }
374
375 static inline void pwm_free(struct pwm_device *pwm)
376 {
377 }
378
379 static inline int pwm_apply_state(struct pwm_device *pwm,
380                                   const struct pwm_state *state)
381 {
382         return -ENOTSUPP;
383 }
384
385 static inline int pwm_adjust_config(struct pwm_device *pwm)
386 {
387         return -ENOTSUPP;
388 }
389
390 static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
391                              int period_ns)
392 {
393         return -EINVAL;
394 }
395
396 static inline int pwm_capture(struct pwm_device *pwm,
397                               struct pwm_capture *result,
398                               unsigned long timeout)
399 {
400         return -EINVAL;
401 }
402
403 static inline int pwm_set_polarity(struct pwm_device *pwm,
404                                    enum pwm_polarity polarity)
405 {
406         return -ENOTSUPP;
407 }
408
409 static inline int pwm_enable(struct pwm_device *pwm)
410 {
411         return -EINVAL;
412 }
413
414 static inline void pwm_disable(struct pwm_device *pwm)
415 {
416 }
417
418 static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data)
419 {
420         return -EINVAL;
421 }
422
423 static inline void *pwm_get_chip_data(struct pwm_device *pwm)
424 {
425         return NULL;
426 }
427
428 static inline int pwmchip_add(struct pwm_chip *chip)
429 {
430         return -EINVAL;
431 }
432
433 static inline int pwmchip_add_inversed(struct pwm_chip *chip)
434 {
435         return -EINVAL;
436 }
437
438 static inline int pwmchip_remove(struct pwm_chip *chip)
439 {
440         return -EINVAL;
441 }
442
443 static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
444                                                        unsigned int index,
445                                                        const char *label)
446 {
447         return ERR_PTR(-ENODEV);
448 }
449
450 static inline struct pwm_device *pwm_get(struct device *dev,
451                                          const char *consumer)
452 {
453         return ERR_PTR(-ENODEV);
454 }
455
456 static inline struct pwm_device *of_pwm_get(struct device_node *np,
457                                             const char *con_id)
458 {
459         return ERR_PTR(-ENODEV);
460 }
461
462 static inline void pwm_put(struct pwm_device *pwm)
463 {
464 }
465
466 static inline struct pwm_device *devm_pwm_get(struct device *dev,
467                                               const char *consumer)
468 {
469         return ERR_PTR(-ENODEV);
470 }
471
472 static inline struct pwm_device *devm_of_pwm_get(struct device *dev,
473                                                  struct device_node *np,
474                                                  const char *con_id)
475 {
476         return ERR_PTR(-ENODEV);
477 }
478
479 static inline void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
480 {
481 }
482
483 static inline bool pwm_can_sleep(struct pwm_device *pwm)
484 {
485         return false;
486 }
487 #endif
488
489 static inline void pwm_apply_args(struct pwm_device *pwm)
490 {
491         /*
492          * PWM users calling pwm_apply_args() expect to have a fresh config
493          * where the polarity and period are set according to pwm_args info.
494          * The problem is, polarity can only be changed when the PWM is
495          * disabled.
496          *
497          * PWM drivers supporting hardware readout may declare the PWM device
498          * as enabled, and prevent polarity setting, which changes from the
499          * existing behavior, where all PWM devices are declared as disabled
500          * at startup (even if they are actually enabled), thus authorizing
501          * polarity setting.
502          *
503          * Instead of setting ->enabled to false, we call pwm_disable()
504          * before pwm_set_polarity() to ensure that everything is configured
505          * as expected, and the PWM is really disabled when the user request
506          * it.
507          *
508          * Note that PWM users requiring a smooth handover between the
509          * bootloader and the kernel (like critical regulators controlled by
510          * PWM devices) will have to switch to the atomic API and avoid calling
511          * pwm_apply_args().
512          */
513         pwm_disable(pwm);
514         pwm_set_polarity(pwm, pwm->args.polarity);
515 }
516
517 struct pwm_lookup {
518         struct list_head list;
519         const char *provider;
520         unsigned int index;
521         const char *dev_id;
522         const char *con_id;
523         unsigned int period;
524         enum pwm_polarity polarity;
525 };
526
527 #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
528         {                                               \
529                 .provider = _provider,                  \
530                 .index = _index,                        \
531                 .dev_id = _dev_id,                      \
532                 .con_id = _con_id,                      \
533                 .period = _period,                      \
534                 .polarity = _polarity                   \
535         }
536
537 #if IS_ENABLED(CONFIG_PWM)
538 void pwm_add_table(struct pwm_lookup *table, size_t num);
539 void pwm_remove_table(struct pwm_lookup *table, size_t num);
540 #else
541 static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
542 {
543 }
544
545 static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
546 {
547 }
548 #endif
549
550 #ifdef CONFIG_PWM_SYSFS
551 void pwmchip_sysfs_export(struct pwm_chip *chip);
552 void pwmchip_sysfs_unexport(struct pwm_chip *chip);
553 void pwmchip_sysfs_unexport_children(struct pwm_chip *chip);
554 #else
555 static inline void pwmchip_sysfs_export(struct pwm_chip *chip)
556 {
557 }
558
559 static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip)
560 {
561 }
562
563 static inline void pwmchip_sysfs_unexport_children(struct pwm_chip *chip)
564 {
565 }
566 #endif /* CONFIG_PWM_SYSFS */
567
568 #endif /* __LINUX_PWM_H */