pwm.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. #ifndef __LINUX_PWM_H
  2. #define __LINUX_PWM_H
  3. #include <linux/err.h>
  4. #include <linux/mutex.h>
  5. #include <linux/of.h>
  6. struct pwm_device;
  7. struct seq_file;
  8. #if IS_ENABLED(CONFIG_PWM)
  9. /*
  10. * pwm_request - request a PWM device
  11. */
  12. struct pwm_device *pwm_request(int pwm_id, const char *label);
  13. /*
  14. * pwm_free - free a PWM device
  15. */
  16. void pwm_free(struct pwm_device *pwm);
  17. /*
  18. * pwm_config - change a PWM device configuration
  19. */
  20. int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns);
  21. /*
  22. * pwm_enable - start a PWM output toggling
  23. */
  24. int pwm_enable(struct pwm_device *pwm);
  25. /*
  26. * pwm_disable - stop a PWM output toggling
  27. */
  28. void pwm_disable(struct pwm_device *pwm);
  29. #else
  30. static inline struct pwm_device *pwm_request(int pwm_id, const char *label)
  31. {
  32. return ERR_PTR(-ENODEV);
  33. }
  34. static inline void pwm_free(struct pwm_device *pwm)
  35. {
  36. }
  37. static inline int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
  38. {
  39. return -EINVAL;
  40. }
  41. static inline int pwm_enable(struct pwm_device *pwm)
  42. {
  43. return -EINVAL;
  44. }
  45. static inline void pwm_disable(struct pwm_device *pwm)
  46. {
  47. }
  48. #endif
  49. struct pwm_chip;
  50. /**
  51. * enum pwm_polarity - polarity of a PWM signal
  52. * @PWM_POLARITY_NORMAL: a high signal for the duration of the duty-
  53. * cycle, followed by a low signal for the remainder of the pulse
  54. * period
  55. * @PWM_POLARITY_INVERSED: a low signal for the duration of the duty-
  56. * cycle, followed by a high signal for the remainder of the pulse
  57. * period
  58. */
  59. enum pwm_polarity {
  60. PWM_POLARITY_NORMAL,
  61. PWM_POLARITY_INVERSED,
  62. };
  63. /**
  64. * struct pwm_args - board-dependent PWM arguments
  65. * @period: reference period
  66. * @polarity: reference polarity
  67. *
  68. * This structure describes board-dependent arguments attached to a PWM
  69. * device. These arguments are usually retrieved from the PWM lookup table or
  70. * device tree.
  71. *
  72. * Do not confuse this with the PWM state: PWM arguments represent the initial
  73. * configuration that users want to use on this PWM device rather than the
  74. * current PWM hardware state.
  75. */
  76. struct pwm_args {
  77. unsigned int period;
  78. enum pwm_polarity polarity;
  79. };
  80. enum {
  81. PWMF_REQUESTED = 1 << 0,
  82. PWMF_EXPORTED = 1 << 1,
  83. };
  84. /*
  85. * struct pwm_state - state of a PWM channel
  86. * @period: PWM period (in nanoseconds)
  87. * @duty_cycle: PWM duty cycle (in nanoseconds)
  88. * @polarity: PWM polarity
  89. * @enabled: PWM enabled status
  90. */
  91. struct pwm_state {
  92. unsigned int period;
  93. unsigned int duty_cycle;
  94. enum pwm_polarity polarity;
  95. bool enabled;
  96. };
  97. /**
  98. * struct pwm_device - PWM channel object
  99. * @label: name of the PWM device
  100. * @flags: flags associated with the PWM device
  101. * @hwpwm: per-chip relative index of the PWM device
  102. * @pwm: global index of the PWM device
  103. * @chip: PWM chip providing this PWM device
  104. * @chip_data: chip-private data associated with the PWM device
  105. * @args: PWM arguments
  106. * @state: curent PWM channel state
  107. */
  108. struct pwm_device {
  109. const char *label;
  110. unsigned long flags;
  111. unsigned int hwpwm;
  112. unsigned int pwm;
  113. struct pwm_chip *chip;
  114. void *chip_data;
  115. struct pwm_args args;
  116. struct pwm_state state;
  117. };
  118. /**
  119. * pwm_get_state() - retrieve the current PWM state
  120. * @pwm: PWM device
  121. * @state: state to fill with the current PWM state
  122. */
  123. static inline void pwm_get_state(const struct pwm_device *pwm,
  124. struct pwm_state *state)
  125. {
  126. *state = pwm->state;
  127. }
  128. static inline bool pwm_is_enabled(const struct pwm_device *pwm)
  129. {
  130. struct pwm_state state;
  131. pwm_get_state(pwm, &state);
  132. return state.enabled;
  133. }
  134. static inline void pwm_set_period(struct pwm_device *pwm, unsigned int period)
  135. {
  136. if (pwm)
  137. pwm->state.period = period;
  138. }
  139. static inline unsigned int pwm_get_period(const struct pwm_device *pwm)
  140. {
  141. struct pwm_state state;
  142. pwm_get_state(pwm, &state);
  143. return state.period;
  144. }
  145. static inline void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty)
  146. {
  147. if (pwm)
  148. pwm->state.duty_cycle = duty;
  149. }
  150. static inline unsigned int pwm_get_duty_cycle(const struct pwm_device *pwm)
  151. {
  152. struct pwm_state state;
  153. pwm_get_state(pwm, &state);
  154. return state.duty_cycle;
  155. }
  156. /*
  157. * pwm_set_polarity - configure the polarity of a PWM signal
  158. */
  159. int pwm_set_polarity(struct pwm_device *pwm, enum pwm_polarity polarity);
  160. static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm)
  161. {
  162. struct pwm_state state;
  163. pwm_get_state(pwm, &state);
  164. return state.polarity;
  165. }
  166. static inline void pwm_get_args(const struct pwm_device *pwm,
  167. struct pwm_args *args)
  168. {
  169. *args = pwm->args;
  170. }
  171. static inline void pwm_apply_args(struct pwm_device *pwm)
  172. {
  173. pwm_set_polarity(pwm, pwm->args.polarity);
  174. }
  175. /**
  176. * struct pwm_ops - PWM controller operations
  177. * @request: optional hook for requesting a PWM
  178. * @free: optional hook for freeing a PWM
  179. * @config: configure duty cycles and period length for this PWM
  180. * @set_polarity: configure the polarity of this PWM
  181. * @enable: enable PWM output toggling
  182. * @disable: disable PWM output toggling
  183. * @dbg_show: optional routine to show contents in debugfs
  184. * @owner: helps prevent removal of modules exporting active PWMs
  185. */
  186. struct pwm_ops {
  187. int (*request)(struct pwm_chip *chip, struct pwm_device *pwm);
  188. void (*free)(struct pwm_chip *chip, struct pwm_device *pwm);
  189. int (*config)(struct pwm_chip *chip, struct pwm_device *pwm,
  190. int duty_ns, int period_ns);
  191. int (*set_polarity)(struct pwm_chip *chip, struct pwm_device *pwm,
  192. enum pwm_polarity polarity);
  193. int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm);
  194. void (*disable)(struct pwm_chip *chip, struct pwm_device *pwm);
  195. #ifdef CONFIG_DEBUG_FS
  196. void (*dbg_show)(struct pwm_chip *chip, struct seq_file *s);
  197. #endif
  198. struct module *owner;
  199. };
  200. /**
  201. * struct pwm_chip - abstract a PWM controller
  202. * @dev: device providing the PWMs
  203. * @list: list node for internal use
  204. * @ops: callbacks for this PWM controller
  205. * @base: number of first PWM controlled by this chip
  206. * @npwm: number of PWMs controlled by this chip
  207. * @pwms: array of PWM devices allocated by the framework
  208. * @of_xlate: request a PWM device given a device tree PWM specifier
  209. * @of_pwm_n_cells: number of cells expected in the device tree PWM specifier
  210. * @can_sleep: must be true if the .config(), .enable() or .disable()
  211. * operations may sleep
  212. */
  213. struct pwm_chip {
  214. struct device *dev;
  215. struct list_head list;
  216. const struct pwm_ops *ops;
  217. int base;
  218. unsigned int npwm;
  219. struct pwm_device *pwms;
  220. struct pwm_device * (*of_xlate)(struct pwm_chip *pc,
  221. const struct of_phandle_args *args);
  222. unsigned int of_pwm_n_cells;
  223. bool can_sleep;
  224. };
  225. #if IS_ENABLED(CONFIG_PWM)
  226. int pwm_set_chip_data(struct pwm_device *pwm, void *data);
  227. void *pwm_get_chip_data(struct pwm_device *pwm);
  228. int pwmchip_add_with_polarity(struct pwm_chip *chip,
  229. enum pwm_polarity polarity);
  230. int pwmchip_add(struct pwm_chip *chip);
  231. int pwmchip_remove(struct pwm_chip *chip);
  232. struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
  233. unsigned int index,
  234. const char *label);
  235. struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *pc,
  236. const struct of_phandle_args *args);
  237. struct pwm_device *pwm_get(struct device *dev, const char *con_id);
  238. struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id);
  239. void pwm_put(struct pwm_device *pwm);
  240. struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
  241. struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
  242. const char *con_id);
  243. void devm_pwm_put(struct device *dev, struct pwm_device *pwm);
  244. bool pwm_can_sleep(struct pwm_device *pwm);
  245. #else
  246. static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data)
  247. {
  248. return -EINVAL;
  249. }
  250. static inline void *pwm_get_chip_data(struct pwm_device *pwm)
  251. {
  252. return NULL;
  253. }
  254. static inline int pwmchip_add(struct pwm_chip *chip)
  255. {
  256. return -EINVAL;
  257. }
  258. static inline int pwmchip_add_inversed(struct pwm_chip *chip)
  259. {
  260. return -EINVAL;
  261. }
  262. static inline int pwmchip_remove(struct pwm_chip *chip)
  263. {
  264. return -EINVAL;
  265. }
  266. static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
  267. unsigned int index,
  268. const char *label)
  269. {
  270. return ERR_PTR(-ENODEV);
  271. }
  272. static inline struct pwm_device *pwm_get(struct device *dev,
  273. const char *consumer)
  274. {
  275. return ERR_PTR(-ENODEV);
  276. }
  277. static inline struct pwm_device *of_pwm_get(struct device_node *np,
  278. const char *con_id)
  279. {
  280. return ERR_PTR(-ENODEV);
  281. }
  282. static inline void pwm_put(struct pwm_device *pwm)
  283. {
  284. }
  285. static inline struct pwm_device *devm_pwm_get(struct device *dev,
  286. const char *consumer)
  287. {
  288. return ERR_PTR(-ENODEV);
  289. }
  290. static inline struct pwm_device *devm_of_pwm_get(struct device *dev,
  291. struct device_node *np,
  292. const char *con_id)
  293. {
  294. return ERR_PTR(-ENODEV);
  295. }
  296. static inline void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
  297. {
  298. }
  299. static inline bool pwm_can_sleep(struct pwm_device *pwm)
  300. {
  301. return false;
  302. }
  303. #endif
  304. struct pwm_lookup {
  305. struct list_head list;
  306. const char *provider;
  307. unsigned int index;
  308. const char *dev_id;
  309. const char *con_id;
  310. unsigned int period;
  311. enum pwm_polarity polarity;
  312. };
  313. #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
  314. { \
  315. .provider = _provider, \
  316. .index = _index, \
  317. .dev_id = _dev_id, \
  318. .con_id = _con_id, \
  319. .period = _period, \
  320. .polarity = _polarity \
  321. }
  322. #if IS_ENABLED(CONFIG_PWM)
  323. void pwm_add_table(struct pwm_lookup *table, size_t num);
  324. void pwm_remove_table(struct pwm_lookup *table, size_t num);
  325. #else
  326. static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
  327. {
  328. }
  329. static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
  330. {
  331. }
  332. #endif
  333. #ifdef CONFIG_PWM_SYSFS
  334. void pwmchip_sysfs_export(struct pwm_chip *chip);
  335. void pwmchip_sysfs_unexport(struct pwm_chip *chip);
  336. #else
  337. static inline void pwmchip_sysfs_export(struct pwm_chip *chip)
  338. {
  339. }
  340. static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip)
  341. {
  342. }
  343. #endif /* CONFIG_PWM_SYSFS */
  344. #endif /* __LINUX_PWM_H */