pwm.h 17 KB

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