pwm.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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 seq_file;
  7. struct pwm_chip;
  8. /**
  9. * enum pwm_polarity - polarity of a PWM signal
  10. * @PWM_POLARITY_NORMAL: a high signal for the duration of the duty-
  11. * cycle, followed by a low signal for the remainder of the pulse
  12. * period
  13. * @PWM_POLARITY_INVERSED: a low signal for the duration of the duty-
  14. * cycle, followed by a high signal for the remainder of the pulse
  15. * period
  16. */
  17. enum pwm_polarity {
  18. PWM_POLARITY_NORMAL,
  19. PWM_POLARITY_INVERSED,
  20. };
  21. /**
  22. * struct pwm_args - board-dependent PWM arguments
  23. * @period: reference period
  24. * @polarity: reference polarity
  25. *
  26. * This structure describes board-dependent arguments attached to a PWM
  27. * device. These arguments are usually retrieved from the PWM lookup table or
  28. * device tree.
  29. *
  30. * Do not confuse this with the PWM state: PWM arguments represent the initial
  31. * configuration that users want to use on this PWM device rather than the
  32. * current PWM hardware state.
  33. */
  34. struct pwm_args {
  35. unsigned int period;
  36. enum pwm_polarity polarity;
  37. };
  38. enum {
  39. PWMF_REQUESTED = 1 << 0,
  40. PWMF_EXPORTED = 1 << 1,
  41. };
  42. /*
  43. * struct pwm_state - state of a PWM channel
  44. * @period: PWM period (in nanoseconds)
  45. * @duty_cycle: PWM duty cycle (in nanoseconds)
  46. * @polarity: PWM polarity
  47. * @enabled: PWM enabled status
  48. */
  49. struct pwm_state {
  50. unsigned int period;
  51. unsigned int duty_cycle;
  52. enum pwm_polarity polarity;
  53. bool enabled;
  54. };
  55. /**
  56. * struct pwm_device - PWM channel object
  57. * @label: name of the PWM device
  58. * @flags: flags associated with the PWM device
  59. * @hwpwm: per-chip relative index of the PWM device
  60. * @pwm: global index of the PWM device
  61. * @chip: PWM chip providing this PWM device
  62. * @chip_data: chip-private data associated with the PWM device
  63. * @args: PWM arguments
  64. * @state: curent PWM channel state
  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. struct pwm_args args;
  74. struct pwm_state state;
  75. };
  76. /**
  77. * pwm_get_state() - retrieve the current PWM state
  78. * @pwm: PWM device
  79. * @state: state to fill with the current PWM state
  80. */
  81. static inline void pwm_get_state(const struct pwm_device *pwm,
  82. struct pwm_state *state)
  83. {
  84. *state = pwm->state;
  85. }
  86. static inline bool pwm_is_enabled(const struct pwm_device *pwm)
  87. {
  88. struct pwm_state state;
  89. pwm_get_state(pwm, &state);
  90. return state.enabled;
  91. }
  92. static inline void pwm_set_period(struct pwm_device *pwm, unsigned int period)
  93. {
  94. if (pwm)
  95. pwm->state.period = period;
  96. }
  97. static inline unsigned int pwm_get_period(const struct pwm_device *pwm)
  98. {
  99. struct pwm_state state;
  100. pwm_get_state(pwm, &state);
  101. return state.period;
  102. }
  103. static inline void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty)
  104. {
  105. if (pwm)
  106. pwm->state.duty_cycle = duty;
  107. }
  108. static inline unsigned int pwm_get_duty_cycle(const struct pwm_device *pwm)
  109. {
  110. struct pwm_state state;
  111. pwm_get_state(pwm, &state);
  112. return state.duty_cycle;
  113. }
  114. static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm)
  115. {
  116. struct pwm_state state;
  117. pwm_get_state(pwm, &state);
  118. return state.polarity;
  119. }
  120. static inline void pwm_get_args(const struct pwm_device *pwm,
  121. struct pwm_args *args)
  122. {
  123. *args = pwm->args;
  124. }
  125. /**
  126. * struct pwm_ops - PWM controller operations
  127. * @request: optional hook for requesting a PWM
  128. * @free: optional hook for freeing a PWM
  129. * @config: configure duty cycles and period length for this PWM
  130. * @set_polarity: configure the polarity of this PWM
  131. * @enable: enable PWM output toggling
  132. * @disable: disable PWM output toggling
  133. * @apply: atomically apply a new PWM config. The state argument
  134. * should be adjusted with the real hardware config (if the
  135. * approximate the period or duty_cycle value, state should
  136. * reflect it)
  137. * @get_state: get the current PWM state. This function is only
  138. * called once per PWM device when the PWM chip is
  139. * registered.
  140. * @dbg_show: optional routine to show contents in debugfs
  141. * @owner: helps prevent removal of modules exporting active PWMs
  142. */
  143. struct pwm_ops {
  144. int (*request)(struct pwm_chip *chip, struct pwm_device *pwm);
  145. void (*free)(struct pwm_chip *chip, struct pwm_device *pwm);
  146. int (*config)(struct pwm_chip *chip, struct pwm_device *pwm,
  147. int duty_ns, int period_ns);
  148. int (*set_polarity)(struct pwm_chip *chip, struct pwm_device *pwm,
  149. enum pwm_polarity polarity);
  150. int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm);
  151. void (*disable)(struct pwm_chip *chip, struct pwm_device *pwm);
  152. int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm,
  153. struct pwm_state *state);
  154. void (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,
  155. struct pwm_state *state);
  156. #ifdef CONFIG_DEBUG_FS
  157. void (*dbg_show)(struct pwm_chip *chip, struct seq_file *s);
  158. #endif
  159. struct module *owner;
  160. };
  161. /**
  162. * struct pwm_chip - abstract a PWM controller
  163. * @dev: device providing the PWMs
  164. * @list: list node for internal use
  165. * @ops: callbacks for this PWM controller
  166. * @base: number of first PWM controlled by this chip
  167. * @npwm: number of PWMs controlled by this chip
  168. * @pwms: array of PWM devices allocated by the framework
  169. * @of_xlate: request a PWM device given a device tree PWM specifier
  170. * @of_pwm_n_cells: number of cells expected in the device tree PWM specifier
  171. * @can_sleep: must be true if the .config(), .enable() or .disable()
  172. * operations may sleep
  173. */
  174. struct pwm_chip {
  175. struct device *dev;
  176. struct list_head list;
  177. const struct pwm_ops *ops;
  178. int base;
  179. unsigned int npwm;
  180. struct pwm_device *pwms;
  181. struct pwm_device * (*of_xlate)(struct pwm_chip *pc,
  182. const struct of_phandle_args *args);
  183. unsigned int of_pwm_n_cells;
  184. bool can_sleep;
  185. };
  186. #if IS_ENABLED(CONFIG_PWM)
  187. /* PWM user APIs */
  188. struct pwm_device *pwm_request(int pwm_id, const char *label);
  189. void pwm_free(struct pwm_device *pwm);
  190. int pwm_apply_state(struct pwm_device *pwm, struct pwm_state *state);
  191. int pwm_adjust_config(struct pwm_device *pwm);
  192. /**
  193. * pwm_config() - change a PWM device configuration
  194. * @pwm: PWM device
  195. * @duty_ns: "on" time (in nanoseconds)
  196. * @period_ns: duration (in nanoseconds) of one cycle
  197. *
  198. * Returns: 0 on success or a negative error code on failure.
  199. */
  200. static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
  201. int period_ns)
  202. {
  203. struct pwm_state state;
  204. if (!pwm)
  205. return -EINVAL;
  206. pwm_get_state(pwm, &state);
  207. if (state.duty_cycle == duty_ns && state.period == period_ns)
  208. return 0;
  209. state.duty_cycle = duty_ns;
  210. state.period = period_ns;
  211. return pwm_apply_state(pwm, &state);
  212. }
  213. /**
  214. * pwm_set_polarity() - configure the polarity of a PWM signal
  215. * @pwm: PWM device
  216. * @polarity: new polarity of the PWM signal
  217. *
  218. * Note that the polarity cannot be configured while the PWM device is
  219. * enabled.
  220. *
  221. * Returns: 0 on success or a negative error code on failure.
  222. */
  223. static inline int pwm_set_polarity(struct pwm_device *pwm,
  224. enum pwm_polarity polarity)
  225. {
  226. struct pwm_state state;
  227. if (!pwm)
  228. return -EINVAL;
  229. pwm_get_state(pwm, &state);
  230. if (state.polarity == polarity)
  231. return 0;
  232. /*
  233. * Changing the polarity of a running PWM without adjusting the
  234. * dutycycle/period value is a bit risky (can introduce glitches).
  235. * Return -EBUSY in this case.
  236. * Note that this is allowed when using pwm_apply_state() because
  237. * the user specifies all the parameters.
  238. */
  239. if (state.enabled)
  240. return -EBUSY;
  241. state.polarity = polarity;
  242. return pwm_apply_state(pwm, &state);
  243. }
  244. /**
  245. * pwm_enable() - start a PWM output toggling
  246. * @pwm: PWM device
  247. *
  248. * Returns: 0 on success or a negative error code on failure.
  249. */
  250. static inline int pwm_enable(struct pwm_device *pwm)
  251. {
  252. struct pwm_state state;
  253. if (!pwm)
  254. return -EINVAL;
  255. pwm_get_state(pwm, &state);
  256. if (state.enabled)
  257. return 0;
  258. state.enabled = true;
  259. return pwm_apply_state(pwm, &state);
  260. }
  261. /**
  262. * pwm_disable() - stop a PWM output toggling
  263. * @pwm: PWM device
  264. */
  265. static inline void pwm_disable(struct pwm_device *pwm)
  266. {
  267. struct pwm_state state;
  268. if (!pwm)
  269. return;
  270. pwm_get_state(pwm, &state);
  271. if (!state.enabled)
  272. return;
  273. state.enabled = false;
  274. pwm_apply_state(pwm, &state);
  275. }
  276. /* PWM provider APIs */
  277. int pwm_set_chip_data(struct pwm_device *pwm, void *data);
  278. void *pwm_get_chip_data(struct pwm_device *pwm);
  279. int pwmchip_add_with_polarity(struct pwm_chip *chip,
  280. enum pwm_polarity polarity);
  281. int pwmchip_add(struct pwm_chip *chip);
  282. int pwmchip_remove(struct pwm_chip *chip);
  283. struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
  284. unsigned int index,
  285. const char *label);
  286. struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *pc,
  287. const struct of_phandle_args *args);
  288. struct pwm_device *pwm_get(struct device *dev, const char *con_id);
  289. struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id);
  290. void pwm_put(struct pwm_device *pwm);
  291. struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
  292. struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
  293. const char *con_id);
  294. void devm_pwm_put(struct device *dev, struct pwm_device *pwm);
  295. bool pwm_can_sleep(struct pwm_device *pwm);
  296. #else
  297. static inline struct pwm_device *pwm_request(int pwm_id, const char *label)
  298. {
  299. return ERR_PTR(-ENODEV);
  300. }
  301. static inline void pwm_free(struct pwm_device *pwm)
  302. {
  303. }
  304. static inline int pwm_apply_state(struct pwm_device *pwm,
  305. const struct pwm_state *state)
  306. {
  307. return -ENOTSUPP;
  308. }
  309. static inline int pwm_adjust_config(struct pwm_device *pwm)
  310. {
  311. return -ENOTSUPP;
  312. }
  313. static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
  314. int period_ns)
  315. {
  316. return -EINVAL;
  317. }
  318. static inline int pwm_set_polarity(struct pwm_device *pwm,
  319. enum pwm_polarity polarity)
  320. {
  321. return -ENOTSUPP;
  322. }
  323. static inline int pwm_enable(struct pwm_device *pwm)
  324. {
  325. return -EINVAL;
  326. }
  327. static inline void pwm_disable(struct pwm_device *pwm)
  328. {
  329. }
  330. static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data)
  331. {
  332. return -EINVAL;
  333. }
  334. static inline void *pwm_get_chip_data(struct pwm_device *pwm)
  335. {
  336. return NULL;
  337. }
  338. static inline int pwmchip_add(struct pwm_chip *chip)
  339. {
  340. return -EINVAL;
  341. }
  342. static inline int pwmchip_add_inversed(struct pwm_chip *chip)
  343. {
  344. return -EINVAL;
  345. }
  346. static inline int pwmchip_remove(struct pwm_chip *chip)
  347. {
  348. return -EINVAL;
  349. }
  350. static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
  351. unsigned int index,
  352. const char *label)
  353. {
  354. return ERR_PTR(-ENODEV);
  355. }
  356. static inline struct pwm_device *pwm_get(struct device *dev,
  357. const char *consumer)
  358. {
  359. return ERR_PTR(-ENODEV);
  360. }
  361. static inline struct pwm_device *of_pwm_get(struct device_node *np,
  362. const char *con_id)
  363. {
  364. return ERR_PTR(-ENODEV);
  365. }
  366. static inline void pwm_put(struct pwm_device *pwm)
  367. {
  368. }
  369. static inline struct pwm_device *devm_pwm_get(struct device *dev,
  370. const char *consumer)
  371. {
  372. return ERR_PTR(-ENODEV);
  373. }
  374. static inline struct pwm_device *devm_of_pwm_get(struct device *dev,
  375. struct device_node *np,
  376. const char *con_id)
  377. {
  378. return ERR_PTR(-ENODEV);
  379. }
  380. static inline void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
  381. {
  382. }
  383. static inline bool pwm_can_sleep(struct pwm_device *pwm)
  384. {
  385. return false;
  386. }
  387. #endif
  388. static inline void pwm_apply_args(struct pwm_device *pwm)
  389. {
  390. /*
  391. * PWM users calling pwm_apply_args() expect to have a fresh config
  392. * where the polarity and period are set according to pwm_args info.
  393. * The problem is, polarity can only be changed when the PWM is
  394. * disabled.
  395. *
  396. * PWM drivers supporting hardware readout may declare the PWM device
  397. * as enabled, and prevent polarity setting, which changes from the
  398. * existing behavior, where all PWM devices are declared as disabled
  399. * at startup (even if they are actually enabled), thus authorizing
  400. * polarity setting.
  401. *
  402. * Instead of setting ->enabled to false, we call pwm_disable()
  403. * before pwm_set_polarity() to ensure that everything is configured
  404. * as expected, and the PWM is really disabled when the user request
  405. * it.
  406. *
  407. * Note that PWM users requiring a smooth handover between the
  408. * bootloader and the kernel (like critical regulators controlled by
  409. * PWM devices) will have to switch to the atomic API and avoid calling
  410. * pwm_apply_args().
  411. */
  412. pwm_disable(pwm);
  413. pwm_set_polarity(pwm, pwm->args.polarity);
  414. }
  415. struct pwm_lookup {
  416. struct list_head list;
  417. const char *provider;
  418. unsigned int index;
  419. const char *dev_id;
  420. const char *con_id;
  421. unsigned int period;
  422. enum pwm_polarity polarity;
  423. };
  424. #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
  425. { \
  426. .provider = _provider, \
  427. .index = _index, \
  428. .dev_id = _dev_id, \
  429. .con_id = _con_id, \
  430. .period = _period, \
  431. .polarity = _polarity \
  432. }
  433. #if IS_ENABLED(CONFIG_PWM)
  434. void pwm_add_table(struct pwm_lookup *table, size_t num);
  435. void pwm_remove_table(struct pwm_lookup *table, size_t num);
  436. #else
  437. static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
  438. {
  439. }
  440. static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
  441. {
  442. }
  443. #endif
  444. #ifdef CONFIG_PWM_SYSFS
  445. void pwmchip_sysfs_export(struct pwm_chip *chip);
  446. void pwmchip_sysfs_unexport(struct pwm_chip *chip);
  447. #else
  448. static inline void pwmchip_sysfs_export(struct pwm_chip *chip)
  449. {
  450. }
  451. static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip)
  452. {
  453. }
  454. #endif /* CONFIG_PWM_SYSFS */
  455. #endif /* __LINUX_PWM_H */