pwm.h 9.3 KB

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