pwm.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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. if (duty_ns < 0 || period_ns < 0)
  207. return -EINVAL;
  208. pwm_get_state(pwm, &state);
  209. if (state.duty_cycle == duty_ns && state.period == period_ns)
  210. return 0;
  211. state.duty_cycle = duty_ns;
  212. state.period = period_ns;
  213. return pwm_apply_state(pwm, &state);
  214. }
  215. /**
  216. * pwm_set_polarity() - configure the polarity of a PWM signal
  217. * @pwm: PWM device
  218. * @polarity: new polarity of the PWM signal
  219. *
  220. * Note that the polarity cannot be configured while the PWM device is
  221. * enabled.
  222. *
  223. * Returns: 0 on success or a negative error code on failure.
  224. */
  225. static inline int pwm_set_polarity(struct pwm_device *pwm,
  226. enum pwm_polarity polarity)
  227. {
  228. struct pwm_state state;
  229. if (!pwm)
  230. return -EINVAL;
  231. pwm_get_state(pwm, &state);
  232. if (state.polarity == polarity)
  233. return 0;
  234. /*
  235. * Changing the polarity of a running PWM without adjusting the
  236. * dutycycle/period value is a bit risky (can introduce glitches).
  237. * Return -EBUSY in this case.
  238. * Note that this is allowed when using pwm_apply_state() because
  239. * the user specifies all the parameters.
  240. */
  241. if (state.enabled)
  242. return -EBUSY;
  243. state.polarity = polarity;
  244. return pwm_apply_state(pwm, &state);
  245. }
  246. /**
  247. * pwm_enable() - start a PWM output toggling
  248. * @pwm: PWM device
  249. *
  250. * Returns: 0 on success or a negative error code on failure.
  251. */
  252. static inline int pwm_enable(struct pwm_device *pwm)
  253. {
  254. struct pwm_state state;
  255. if (!pwm)
  256. return -EINVAL;
  257. pwm_get_state(pwm, &state);
  258. if (state.enabled)
  259. return 0;
  260. state.enabled = true;
  261. return pwm_apply_state(pwm, &state);
  262. }
  263. /**
  264. * pwm_disable() - stop a PWM output toggling
  265. * @pwm: PWM device
  266. */
  267. static inline void pwm_disable(struct pwm_device *pwm)
  268. {
  269. struct pwm_state state;
  270. if (!pwm)
  271. return;
  272. pwm_get_state(pwm, &state);
  273. if (!state.enabled)
  274. return;
  275. state.enabled = false;
  276. pwm_apply_state(pwm, &state);
  277. }
  278. /* PWM provider APIs */
  279. int pwm_set_chip_data(struct pwm_device *pwm, void *data);
  280. void *pwm_get_chip_data(struct pwm_device *pwm);
  281. int pwmchip_add_with_polarity(struct pwm_chip *chip,
  282. enum pwm_polarity polarity);
  283. int pwmchip_add(struct pwm_chip *chip);
  284. int pwmchip_remove(struct pwm_chip *chip);
  285. struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
  286. unsigned int index,
  287. const char *label);
  288. struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *pc,
  289. const struct of_phandle_args *args);
  290. struct pwm_device *pwm_get(struct device *dev, const char *con_id);
  291. struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id);
  292. void pwm_put(struct pwm_device *pwm);
  293. struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
  294. struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
  295. const char *con_id);
  296. void devm_pwm_put(struct device *dev, struct pwm_device *pwm);
  297. bool pwm_can_sleep(struct pwm_device *pwm);
  298. #else
  299. static inline struct pwm_device *pwm_request(int pwm_id, const char *label)
  300. {
  301. return ERR_PTR(-ENODEV);
  302. }
  303. static inline void pwm_free(struct pwm_device *pwm)
  304. {
  305. }
  306. static inline int pwm_apply_state(struct pwm_device *pwm,
  307. const struct pwm_state *state)
  308. {
  309. return -ENOTSUPP;
  310. }
  311. static inline int pwm_adjust_config(struct pwm_device *pwm)
  312. {
  313. return -ENOTSUPP;
  314. }
  315. static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
  316. int period_ns)
  317. {
  318. return -EINVAL;
  319. }
  320. static inline int pwm_set_polarity(struct pwm_device *pwm,
  321. enum pwm_polarity polarity)
  322. {
  323. return -ENOTSUPP;
  324. }
  325. static inline int pwm_enable(struct pwm_device *pwm)
  326. {
  327. return -EINVAL;
  328. }
  329. static inline void pwm_disable(struct pwm_device *pwm)
  330. {
  331. }
  332. static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data)
  333. {
  334. return -EINVAL;
  335. }
  336. static inline void *pwm_get_chip_data(struct pwm_device *pwm)
  337. {
  338. return NULL;
  339. }
  340. static inline int pwmchip_add(struct pwm_chip *chip)
  341. {
  342. return -EINVAL;
  343. }
  344. static inline int pwmchip_add_inversed(struct pwm_chip *chip)
  345. {
  346. return -EINVAL;
  347. }
  348. static inline int pwmchip_remove(struct pwm_chip *chip)
  349. {
  350. return -EINVAL;
  351. }
  352. static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
  353. unsigned int index,
  354. const char *label)
  355. {
  356. return ERR_PTR(-ENODEV);
  357. }
  358. static inline struct pwm_device *pwm_get(struct device *dev,
  359. const char *consumer)
  360. {
  361. return ERR_PTR(-ENODEV);
  362. }
  363. static inline struct pwm_device *of_pwm_get(struct device_node *np,
  364. const char *con_id)
  365. {
  366. return ERR_PTR(-ENODEV);
  367. }
  368. static inline void pwm_put(struct pwm_device *pwm)
  369. {
  370. }
  371. static inline struct pwm_device *devm_pwm_get(struct device *dev,
  372. const char *consumer)
  373. {
  374. return ERR_PTR(-ENODEV);
  375. }
  376. static inline struct pwm_device *devm_of_pwm_get(struct device *dev,
  377. struct device_node *np,
  378. const char *con_id)
  379. {
  380. return ERR_PTR(-ENODEV);
  381. }
  382. static inline void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
  383. {
  384. }
  385. static inline bool pwm_can_sleep(struct pwm_device *pwm)
  386. {
  387. return false;
  388. }
  389. #endif
  390. static inline void pwm_apply_args(struct pwm_device *pwm)
  391. {
  392. struct pwm_state state = { };
  393. /*
  394. * PWM users calling pwm_apply_args() expect to have a fresh config
  395. * where the polarity and period are set according to pwm_args info.
  396. * The problem is, polarity can only be changed when the PWM is
  397. * disabled.
  398. *
  399. * PWM drivers supporting hardware readout may declare the PWM device
  400. * as enabled, and prevent polarity setting, which changes from the
  401. * existing behavior, where all PWM devices are declared as disabled
  402. * at startup (even if they are actually enabled), thus authorizing
  403. * polarity setting.
  404. *
  405. * To fulfill this requirement, we apply a new state which disables
  406. * the PWM device and set the reference period and polarity config.
  407. *
  408. * Note that PWM users requiring a smooth handover between the
  409. * bootloader and the kernel (like critical regulators controlled by
  410. * PWM devices) will have to switch to the atomic API and avoid calling
  411. * pwm_apply_args().
  412. */
  413. state.enabled = false;
  414. state.polarity = pwm->args.polarity;
  415. state.period = pwm->args.period;
  416. pwm_apply_state(pwm, &state);
  417. }
  418. struct pwm_lookup {
  419. struct list_head list;
  420. const char *provider;
  421. unsigned int index;
  422. const char *dev_id;
  423. const char *con_id;
  424. unsigned int period;
  425. enum pwm_polarity polarity;
  426. };
  427. #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
  428. { \
  429. .provider = _provider, \
  430. .index = _index, \
  431. .dev_id = _dev_id, \
  432. .con_id = _con_id, \
  433. .period = _period, \
  434. .polarity = _polarity \
  435. }
  436. #if IS_ENABLED(CONFIG_PWM)
  437. void pwm_add_table(struct pwm_lookup *table, size_t num);
  438. void pwm_remove_table(struct pwm_lookup *table, size_t num);
  439. #else
  440. static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
  441. {
  442. }
  443. static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
  444. {
  445. }
  446. #endif
  447. #ifdef CONFIG_PWM_SYSFS
  448. void pwmchip_sysfs_export(struct pwm_chip *chip);
  449. void pwmchip_sysfs_unexport(struct pwm_chip *chip);
  450. #else
  451. static inline void pwmchip_sysfs_export(struct pwm_chip *chip)
  452. {
  453. }
  454. static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip)
  455. {
  456. }
  457. #endif /* CONFIG_PWM_SYSFS */
  458. #endif /* __LINUX_PWM_H */