pwm.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. * @can_sleep: must be true if the .config(), .enable() or .disable()
  129. * operations may sleep
  130. */
  131. struct pwm_chip {
  132. struct device *dev;
  133. struct list_head list;
  134. const struct pwm_ops *ops;
  135. int base;
  136. unsigned int npwm;
  137. struct pwm_device *pwms;
  138. struct pwm_device * (*of_xlate)(struct pwm_chip *pc,
  139. const struct of_phandle_args *args);
  140. unsigned int of_pwm_n_cells;
  141. bool can_sleep;
  142. };
  143. #if IS_ENABLED(CONFIG_PWM)
  144. int pwm_set_chip_data(struct pwm_device *pwm, void *data);
  145. void *pwm_get_chip_data(struct pwm_device *pwm);
  146. int pwmchip_add(struct pwm_chip *chip);
  147. int pwmchip_remove(struct pwm_chip *chip);
  148. struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
  149. unsigned int index,
  150. const char *label);
  151. struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *pc,
  152. const struct of_phandle_args *args);
  153. struct pwm_device *pwm_get(struct device *dev, const char *con_id);
  154. struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id);
  155. void pwm_put(struct pwm_device *pwm);
  156. struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
  157. struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
  158. const char *con_id);
  159. void devm_pwm_put(struct device *dev, struct pwm_device *pwm);
  160. bool pwm_can_sleep(struct pwm_device *pwm);
  161. #else
  162. static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data)
  163. {
  164. return -EINVAL;
  165. }
  166. static inline void *pwm_get_chip_data(struct pwm_device *pwm)
  167. {
  168. return NULL;
  169. }
  170. static inline int pwmchip_add(struct pwm_chip *chip)
  171. {
  172. return -EINVAL;
  173. }
  174. static inline int pwmchip_remove(struct pwm_chip *chip)
  175. {
  176. return -EINVAL;
  177. }
  178. static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
  179. unsigned int index,
  180. const char *label)
  181. {
  182. return ERR_PTR(-ENODEV);
  183. }
  184. static inline struct pwm_device *pwm_get(struct device *dev,
  185. const char *consumer)
  186. {
  187. return ERR_PTR(-ENODEV);
  188. }
  189. static inline struct pwm_device *of_pwm_get(struct device_node *np,
  190. const char *con_id)
  191. {
  192. return ERR_PTR(-ENODEV);
  193. }
  194. static inline void pwm_put(struct pwm_device *pwm)
  195. {
  196. }
  197. static inline struct pwm_device *devm_pwm_get(struct device *dev,
  198. const char *consumer)
  199. {
  200. return ERR_PTR(-ENODEV);
  201. }
  202. static inline struct pwm_device *devm_of_pwm_get(struct device *dev,
  203. struct device_node *np,
  204. const char *con_id)
  205. {
  206. return ERR_PTR(-ENODEV);
  207. }
  208. static inline void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
  209. {
  210. }
  211. static inline bool pwm_can_sleep(struct pwm_device *pwm)
  212. {
  213. return false;
  214. }
  215. #endif
  216. struct pwm_lookup {
  217. struct list_head list;
  218. const char *provider;
  219. unsigned int index;
  220. const char *dev_id;
  221. const char *con_id;
  222. };
  223. #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id) \
  224. { \
  225. .provider = _provider, \
  226. .index = _index, \
  227. .dev_id = _dev_id, \
  228. .con_id = _con_id, \
  229. }
  230. #if IS_ENABLED(CONFIG_PWM)
  231. void pwm_add_table(struct pwm_lookup *table, size_t num);
  232. #else
  233. static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
  234. {
  235. }
  236. #endif
  237. #endif /* __LINUX_PWM_H */