pwm.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. PWMF_EXPORTED = 1 << 2,
  66. };
  67. struct pwm_device {
  68. const char *label;
  69. unsigned long flags;
  70. unsigned int hwpwm;
  71. unsigned int pwm;
  72. struct pwm_chip *chip;
  73. void *chip_data;
  74. unsigned int period; /* in nanoseconds */
  75. unsigned int duty_cycle; /* in nanoseconds */
  76. enum pwm_polarity polarity;
  77. };
  78. static inline void pwm_set_period(struct pwm_device *pwm, unsigned int period)
  79. {
  80. if (pwm)
  81. pwm->period = period;
  82. }
  83. static inline unsigned int pwm_get_period(struct pwm_device *pwm)
  84. {
  85. return pwm ? pwm->period : 0;
  86. }
  87. static inline void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty)
  88. {
  89. if (pwm)
  90. pwm->duty_cycle = duty;
  91. }
  92. static inline unsigned int pwm_get_duty_cycle(struct pwm_device *pwm)
  93. {
  94. return pwm ? pwm->duty_cycle : 0;
  95. }
  96. /*
  97. * pwm_set_polarity - configure the polarity of a PWM signal
  98. */
  99. int pwm_set_polarity(struct pwm_device *pwm, enum pwm_polarity polarity);
  100. /**
  101. * struct pwm_ops - PWM controller operations
  102. * @request: optional hook for requesting a PWM
  103. * @free: optional hook for freeing a PWM
  104. * @config: configure duty cycles and period length for this PWM
  105. * @set_polarity: configure the polarity of this PWM
  106. * @enable: enable PWM output toggling
  107. * @disable: disable PWM output toggling
  108. * @dbg_show: optional routine to show contents in debugfs
  109. * @owner: helps prevent removal of modules exporting active PWMs
  110. */
  111. struct pwm_ops {
  112. int (*request)(struct pwm_chip *chip,
  113. struct pwm_device *pwm);
  114. void (*free)(struct pwm_chip *chip,
  115. struct pwm_device *pwm);
  116. int (*config)(struct pwm_chip *chip,
  117. struct pwm_device *pwm,
  118. int duty_ns, int period_ns);
  119. int (*set_polarity)(struct pwm_chip *chip,
  120. struct pwm_device *pwm,
  121. enum pwm_polarity polarity);
  122. int (*enable)(struct pwm_chip *chip,
  123. struct pwm_device *pwm);
  124. void (*disable)(struct pwm_chip *chip,
  125. struct pwm_device *pwm);
  126. #ifdef CONFIG_DEBUG_FS
  127. void (*dbg_show)(struct pwm_chip *chip,
  128. struct seq_file *s);
  129. #endif
  130. struct module *owner;
  131. };
  132. /**
  133. * struct pwm_chip - abstract a PWM controller
  134. * @dev: device providing the PWMs
  135. * @list: list node for internal use
  136. * @ops: callbacks for this PWM controller
  137. * @base: number of first PWM controlled by this chip
  138. * @npwm: number of PWMs controlled by this chip
  139. * @pwms: array of PWM devices allocated by the framework
  140. * @can_sleep: must be true if the .config(), .enable() or .disable()
  141. * operations may sleep
  142. */
  143. struct pwm_chip {
  144. struct device *dev;
  145. struct list_head list;
  146. const struct pwm_ops *ops;
  147. int base;
  148. unsigned int npwm;
  149. struct pwm_device *pwms;
  150. struct pwm_device * (*of_xlate)(struct pwm_chip *pc,
  151. const struct of_phandle_args *args);
  152. unsigned int of_pwm_n_cells;
  153. bool can_sleep;
  154. };
  155. #if IS_ENABLED(CONFIG_PWM)
  156. int pwm_set_chip_data(struct pwm_device *pwm, void *data);
  157. void *pwm_get_chip_data(struct pwm_device *pwm);
  158. int pwmchip_add(struct pwm_chip *chip);
  159. int pwmchip_remove(struct pwm_chip *chip);
  160. struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
  161. unsigned int index,
  162. const char *label);
  163. struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *pc,
  164. const struct of_phandle_args *args);
  165. struct pwm_device *pwm_get(struct device *dev, const char *con_id);
  166. struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id);
  167. void pwm_put(struct pwm_device *pwm);
  168. struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
  169. struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
  170. const char *con_id);
  171. void devm_pwm_put(struct device *dev, struct pwm_device *pwm);
  172. bool pwm_can_sleep(struct pwm_device *pwm);
  173. #else
  174. static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data)
  175. {
  176. return -EINVAL;
  177. }
  178. static inline void *pwm_get_chip_data(struct pwm_device *pwm)
  179. {
  180. return NULL;
  181. }
  182. static inline int pwmchip_add(struct pwm_chip *chip)
  183. {
  184. return -EINVAL;
  185. }
  186. static inline int pwmchip_remove(struct pwm_chip *chip)
  187. {
  188. return -EINVAL;
  189. }
  190. static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
  191. unsigned int index,
  192. const char *label)
  193. {
  194. return ERR_PTR(-ENODEV);
  195. }
  196. static inline struct pwm_device *pwm_get(struct device *dev,
  197. const char *consumer)
  198. {
  199. return ERR_PTR(-ENODEV);
  200. }
  201. static inline struct pwm_device *of_pwm_get(struct device_node *np,
  202. const char *con_id)
  203. {
  204. return ERR_PTR(-ENODEV);
  205. }
  206. static inline void pwm_put(struct pwm_device *pwm)
  207. {
  208. }
  209. static inline struct pwm_device *devm_pwm_get(struct device *dev,
  210. const char *consumer)
  211. {
  212. return ERR_PTR(-ENODEV);
  213. }
  214. static inline struct pwm_device *devm_of_pwm_get(struct device *dev,
  215. struct device_node *np,
  216. const char *con_id)
  217. {
  218. return ERR_PTR(-ENODEV);
  219. }
  220. static inline void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
  221. {
  222. }
  223. static inline bool pwm_can_sleep(struct pwm_device *pwm)
  224. {
  225. return false;
  226. }
  227. #endif
  228. struct pwm_lookup {
  229. struct list_head list;
  230. const char *provider;
  231. unsigned int index;
  232. const char *dev_id;
  233. const char *con_id;
  234. };
  235. #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id) \
  236. { \
  237. .provider = _provider, \
  238. .index = _index, \
  239. .dev_id = _dev_id, \
  240. .con_id = _con_id, \
  241. }
  242. #if IS_ENABLED(CONFIG_PWM)
  243. void pwm_add_table(struct pwm_lookup *table, size_t num);
  244. #else
  245. static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
  246. {
  247. }
  248. #endif
  249. #ifdef CONFIG_PWM_SYSFS
  250. void pwmchip_sysfs_export(struct pwm_chip *chip);
  251. void pwmchip_sysfs_unexport(struct pwm_chip *chip);
  252. #else
  253. static inline void pwmchip_sysfs_export(struct pwm_chip *chip)
  254. {
  255. }
  256. static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip)
  257. {
  258. }
  259. #endif /* CONFIG_PWM_SYSFS */
  260. #endif /* __LINUX_PWM_H */