pwm.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #ifndef __LINUX_PWM_H
  2. #define __LINUX_PWM_H
  3. #include <linux/err.h>
  4. #include <linux/of.h>
  5. struct pwm_device;
  6. struct seq_file;
  7. #if IS_ENABLED(CONFIG_PWM) || IS_ENABLED(CONFIG_HAVE_PWM)
  8. /*
  9. * pwm_request - request a PWM device
  10. */
  11. struct pwm_device *pwm_request(int pwm_id, const char *label);
  12. /*
  13. * pwm_free - free a PWM device
  14. */
  15. void pwm_free(struct pwm_device *pwm);
  16. /*
  17. * pwm_config - change a PWM device configuration
  18. */
  19. int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns);
  20. /*
  21. * pwm_enable - start a PWM output toggling
  22. */
  23. int pwm_enable(struct pwm_device *pwm);
  24. /*
  25. * pwm_disable - stop a PWM output toggling
  26. */
  27. void pwm_disable(struct pwm_device *pwm);
  28. #else
  29. static inline struct pwm_device *pwm_request(int pwm_id, const char *label)
  30. {
  31. return ERR_PTR(-ENODEV);
  32. }
  33. static inline void pwm_free(struct pwm_device *pwm)
  34. {
  35. }
  36. static inline int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
  37. {
  38. return -EINVAL;
  39. }
  40. static inline int pwm_enable(struct pwm_device *pwm)
  41. {
  42. return -EINVAL;
  43. }
  44. static inline void pwm_disable(struct pwm_device *pwm)
  45. {
  46. }
  47. #endif
  48. struct pwm_chip;
  49. /**
  50. * enum pwm_polarity - polarity of a PWM signal
  51. * @PWM_POLARITY_NORMAL: a high signal for the duration of the duty-
  52. * cycle, followed by a low signal for the remainder of the pulse
  53. * period
  54. * @PWM_POLARITY_INVERSED: a low signal for the duration of the duty-
  55. * cycle, followed by a high signal for the remainder of the pulse
  56. * period
  57. */
  58. enum pwm_polarity {
  59. PWM_POLARITY_NORMAL,
  60. PWM_POLARITY_INVERSED,
  61. };
  62. enum {
  63. PWMF_REQUESTED = 1 << 0,
  64. PWMF_ENABLED = 1 << 1,
  65. };
  66. struct pwm_device {
  67. const char *label;
  68. unsigned long flags;
  69. unsigned int hwpwm;
  70. unsigned int pwm;
  71. struct pwm_chip *chip;
  72. void *chip_data;
  73. unsigned int period; /* in nanoseconds */
  74. };
  75. static inline void pwm_set_period(struct pwm_device *pwm, unsigned int period)
  76. {
  77. if (pwm)
  78. pwm->period = period;
  79. }
  80. static inline unsigned int pwm_get_period(struct pwm_device *pwm)
  81. {
  82. return pwm ? pwm->period : 0;
  83. }
  84. /*
  85. * pwm_set_polarity - configure the polarity of a PWM signal
  86. */
  87. int pwm_set_polarity(struct pwm_device *pwm, enum pwm_polarity polarity);
  88. /**
  89. * struct pwm_ops - PWM controller operations
  90. * @request: optional hook for requesting a PWM
  91. * @free: optional hook for freeing a PWM
  92. * @config: configure duty cycles and period length for this PWM
  93. * @set_polarity: configure the polarity of this PWM
  94. * @enable: enable PWM output toggling
  95. * @disable: disable PWM output toggling
  96. * @dbg_show: optional routine to show contents in debugfs
  97. * @owner: helps prevent removal of modules exporting active PWMs
  98. */
  99. struct pwm_ops {
  100. int (*request)(struct pwm_chip *chip,
  101. struct pwm_device *pwm);
  102. void (*free)(struct pwm_chip *chip,
  103. struct pwm_device *pwm);
  104. int (*config)(struct pwm_chip *chip,
  105. struct pwm_device *pwm,
  106. int duty_ns, int period_ns);
  107. int (*set_polarity)(struct pwm_chip *chip,
  108. struct pwm_device *pwm,
  109. enum pwm_polarity polarity);
  110. int (*enable)(struct pwm_chip *chip,
  111. struct pwm_device *pwm);
  112. void (*disable)(struct pwm_chip *chip,
  113. struct pwm_device *pwm);
  114. #ifdef CONFIG_DEBUG_FS
  115. void (*dbg_show)(struct pwm_chip *chip,
  116. struct seq_file *s);
  117. #endif
  118. struct module *owner;
  119. };
  120. /**
  121. * struct pwm_chip - abstract a PWM controller
  122. * @dev: device providing the PWMs
  123. * @list: list node for internal use
  124. * @ops: callbacks for this PWM controller
  125. * @base: number of first PWM controlled by this chip
  126. * @npwm: number of PWMs controlled by this chip
  127. * @pwms: array of PWM devices allocated by the framework
  128. */
  129. struct pwm_chip {
  130. struct device *dev;
  131. struct list_head list;
  132. const struct pwm_ops *ops;
  133. int base;
  134. unsigned int npwm;
  135. struct pwm_device *pwms;
  136. struct pwm_device * (*of_xlate)(struct pwm_chip *pc,
  137. const struct of_phandle_args *args);
  138. unsigned int of_pwm_n_cells;
  139. };
  140. #if IS_ENABLED(CONFIG_PWM)
  141. int pwm_set_chip_data(struct pwm_device *pwm, void *data);
  142. void *pwm_get_chip_data(struct pwm_device *pwm);
  143. int pwmchip_add(struct pwm_chip *chip);
  144. int pwmchip_remove(struct pwm_chip *chip);
  145. struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
  146. unsigned int index,
  147. const char *label);
  148. struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *pc,
  149. const struct of_phandle_args *args);
  150. struct pwm_device *pwm_get(struct device *dev, const char *con_id);
  151. struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id);
  152. void pwm_put(struct pwm_device *pwm);
  153. struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
  154. struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
  155. const char *con_id);
  156. void devm_pwm_put(struct device *dev, struct pwm_device *pwm);
  157. #else
  158. static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data)
  159. {
  160. return -EINVAL;
  161. }
  162. static inline void *pwm_get_chip_data(struct pwm_device *pwm)
  163. {
  164. return NULL;
  165. }
  166. static inline int pwmchip_add(struct pwm_chip *chip)
  167. {
  168. return -EINVAL;
  169. }
  170. static inline int pwmchip_remove(struct pwm_chip *chip)
  171. {
  172. return -EINVAL;
  173. }
  174. static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
  175. unsigned int index,
  176. const char *label)
  177. {
  178. return ERR_PTR(-ENODEV);
  179. }
  180. static inline struct pwm_device *pwm_get(struct device *dev,
  181. const char *consumer)
  182. {
  183. return ERR_PTR(-ENODEV);
  184. }
  185. static inline struct pwm_device *of_pwm_get(struct device_node *np,
  186. const char *con_id)
  187. {
  188. return ERR_PTR(-ENODEV);
  189. }
  190. static inline void pwm_put(struct pwm_device *pwm)
  191. {
  192. }
  193. static inline struct pwm_device *devm_pwm_get(struct device *dev,
  194. const char *consumer)
  195. {
  196. return ERR_PTR(-ENODEV);
  197. }
  198. static inline struct pwm_device *devm_of_pwm_get(struct device *dev,
  199. struct device_node *np,
  200. const char *con_id)
  201. {
  202. return ERR_PTR(-ENODEV);
  203. }
  204. static inline void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
  205. {
  206. }
  207. #endif
  208. struct pwm_lookup {
  209. struct list_head list;
  210. const char *provider;
  211. unsigned int index;
  212. const char *dev_id;
  213. const char *con_id;
  214. };
  215. #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id) \
  216. { \
  217. .provider = _provider, \
  218. .index = _index, \
  219. .dev_id = _dev_id, \
  220. .con_id = _con_id, \
  221. }
  222. #if IS_ENABLED(CONFIG_PWM)
  223. void pwm_add_table(struct pwm_lookup *table, size_t num);
  224. #else
  225. static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
  226. {
  227. }
  228. #endif
  229. #endif /* __LINUX_PWM_H */