pwm.h 7.7 KB

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