Subversion Repositories Kolibri OS

Rev

Rev 6936 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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