pwm.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __LINUX_PWM_H
  3. #define __LINUX_PWM_H
  4. #include <linux/err.h>
  5. #include <linux/mutex.h>
  6. #include <linux/of.h>
  7. #include <linux/android_kabi.h>
  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. u64 period;
  37. enum pwm_polarity polarity;
  38. };
  39. enum {
  40. PWMF_REQUESTED = 0,
  41. PWMF_EXPORTED = 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. * @usage_power: If set, the PWM driver is only required to maintain the power
  50. * output but has more freedom regarding signal form.
  51. * If supported, the signal can be optimized, for example to
  52. * improve EMI by phase shifting individual channels.
  53. */
  54. struct pwm_state {
  55. u64 period;
  56. u64 duty_cycle;
  57. enum pwm_polarity polarity;
  58. bool enabled;
  59. bool usage_power;
  60. };
  61. /**
  62. * struct pwm_device - PWM channel object
  63. * @label: name of the PWM device
  64. * @flags: flags associated with the PWM device
  65. * @hwpwm: per-chip relative index of the PWM device
  66. * @pwm: global index of the PWM device
  67. * @chip: PWM chip providing this PWM device
  68. * @chip_data: chip-private data associated with the PWM device
  69. * @args: PWM arguments
  70. * @state: last applied state
  71. * @last: last implemented state (for PWM_DEBUG)
  72. */
  73. struct pwm_device {
  74. const char *label;
  75. unsigned long flags;
  76. unsigned int hwpwm;
  77. unsigned int pwm;
  78. struct pwm_chip *chip;
  79. void *chip_data;
  80. struct pwm_args args;
  81. struct pwm_state state;
  82. struct pwm_state last;
  83. ANDROID_KABI_RESERVE(1);
  84. };
  85. /**
  86. * pwm_get_state() - retrieve the current PWM state
  87. * @pwm: PWM device
  88. * @state: state to fill with the current PWM state
  89. *
  90. * The returned PWM state represents the state that was applied by a previous call to
  91. * pwm_apply_state(). Drivers may have to slightly tweak that state before programming it to
  92. * hardware. If pwm_apply_state() was never called, this returns either the current hardware
  93. * state (if supported) or the default settings.
  94. */
  95. static inline void pwm_get_state(const struct pwm_device *pwm,
  96. struct pwm_state *state)
  97. {
  98. *state = pwm->state;
  99. }
  100. static inline bool pwm_is_enabled(const struct pwm_device *pwm)
  101. {
  102. struct pwm_state state;
  103. pwm_get_state(pwm, &state);
  104. return state.enabled;
  105. }
  106. static inline void pwm_set_period(struct pwm_device *pwm, u64 period)
  107. {
  108. if (pwm)
  109. pwm->state.period = period;
  110. }
  111. static inline u64 pwm_get_period(const struct pwm_device *pwm)
  112. {
  113. struct pwm_state state;
  114. pwm_get_state(pwm, &state);
  115. return state.period;
  116. }
  117. static inline void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty)
  118. {
  119. if (pwm)
  120. pwm->state.duty_cycle = duty;
  121. }
  122. static inline u64 pwm_get_duty_cycle(const struct pwm_device *pwm)
  123. {
  124. struct pwm_state state;
  125. pwm_get_state(pwm, &state);
  126. return state.duty_cycle;
  127. }
  128. static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm)
  129. {
  130. struct pwm_state state;
  131. pwm_get_state(pwm, &state);
  132. return state.polarity;
  133. }
  134. static inline void pwm_get_args(const struct pwm_device *pwm,
  135. struct pwm_args *args)
  136. {
  137. *args = pwm->args;
  138. }
  139. /**
  140. * pwm_init_state() - prepare a new state to be applied with pwm_apply_state()
  141. * @pwm: PWM device
  142. * @state: state to fill with the prepared PWM state
  143. *
  144. * This functions prepares a state that can later be tweaked and applied
  145. * to the PWM device with pwm_apply_state(). This is a convenient function
  146. * that first retrieves the current PWM state and the replaces the period
  147. * and polarity fields with the reference values defined in pwm->args.
  148. * Once the function returns, you can adjust the ->enabled and ->duty_cycle
  149. * fields according to your needs before calling pwm_apply_state().
  150. *
  151. * ->duty_cycle is initially set to zero to avoid cases where the current
  152. * ->duty_cycle value exceed the pwm_args->period one, which would trigger
  153. * an error if the user calls pwm_apply_state() without adjusting ->duty_cycle
  154. * first.
  155. */
  156. static inline void pwm_init_state(const struct pwm_device *pwm,
  157. struct pwm_state *state)
  158. {
  159. struct pwm_args args;
  160. /* First get the current state. */
  161. pwm_get_state(pwm, state);
  162. /* Then fill it with the reference config */
  163. pwm_get_args(pwm, &args);
  164. state->period = args.period;
  165. state->polarity = args.polarity;
  166. state->duty_cycle = 0;
  167. state->usage_power = false;
  168. }
  169. /**
  170. * pwm_get_relative_duty_cycle() - Get a relative duty cycle value
  171. * @state: PWM state to extract the duty cycle from
  172. * @scale: target scale of the relative duty cycle
  173. *
  174. * This functions converts the absolute duty cycle stored in @state (expressed
  175. * in nanosecond) into a value relative to the period.
  176. *
  177. * For example if you want to get the duty_cycle expressed in percent, call:
  178. *
  179. * pwm_get_state(pwm, &state);
  180. * duty = pwm_get_relative_duty_cycle(&state, 100);
  181. */
  182. static inline unsigned int
  183. pwm_get_relative_duty_cycle(const struct pwm_state *state, unsigned int scale)
  184. {
  185. if (!state->period)
  186. return 0;
  187. return DIV_ROUND_CLOSEST_ULL((u64)state->duty_cycle * scale,
  188. state->period);
  189. }
  190. /**
  191. * pwm_set_relative_duty_cycle() - Set a relative duty cycle value
  192. * @state: PWM state to fill
  193. * @duty_cycle: relative duty cycle value
  194. * @scale: scale in which @duty_cycle is expressed
  195. *
  196. * This functions converts a relative into an absolute duty cycle (expressed
  197. * in nanoseconds), and puts the result in state->duty_cycle.
  198. *
  199. * For example if you want to configure a 50% duty cycle, call:
  200. *
  201. * pwm_init_state(pwm, &state);
  202. * pwm_set_relative_duty_cycle(&state, 50, 100);
  203. * pwm_apply_state(pwm, &state);
  204. *
  205. * This functions returns -EINVAL if @duty_cycle and/or @scale are
  206. * inconsistent (@scale == 0 or @duty_cycle > @scale).
  207. */
  208. static inline int
  209. pwm_set_relative_duty_cycle(struct pwm_state *state, unsigned int duty_cycle,
  210. unsigned int scale)
  211. {
  212. if (!scale || duty_cycle > scale)
  213. return -EINVAL;
  214. state->duty_cycle = DIV_ROUND_CLOSEST_ULL((u64)duty_cycle *
  215. state->period,
  216. scale);
  217. return 0;
  218. }
  219. /**
  220. * struct pwm_capture - PWM capture data
  221. * @period: period of the PWM signal (in nanoseconds)
  222. * @duty_cycle: duty cycle of the PWM signal (in nanoseconds)
  223. */
  224. struct pwm_capture {
  225. unsigned int period;
  226. unsigned int duty_cycle;
  227. };
  228. /**
  229. * struct pwm_ops - PWM controller operations
  230. * @request: optional hook for requesting a PWM
  231. * @free: optional hook for freeing a PWM
  232. * @capture: capture and report PWM signal
  233. * @apply: atomically apply a new PWM config
  234. * @get_state: get the current PWM state. This function is only
  235. * called once per PWM device when the PWM chip is
  236. * registered.
  237. * @owner: helps prevent removal of modules exporting active PWMs
  238. */
  239. struct pwm_ops {
  240. int (*request)(struct pwm_chip *chip, struct pwm_device *pwm);
  241. void (*free)(struct pwm_chip *chip, struct pwm_device *pwm);
  242. int (*capture)(struct pwm_chip *chip, struct pwm_device *pwm,
  243. struct pwm_capture *result, unsigned long timeout);
  244. int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm,
  245. const struct pwm_state *state);
  246. int (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,
  247. struct pwm_state *state);
  248. struct module *owner;
  249. ANDROID_KABI_RESERVE(1);
  250. };
  251. /**
  252. * struct pwm_chip - abstract a PWM controller
  253. * @dev: device providing the PWMs
  254. * @ops: callbacks for this PWM controller
  255. * @base: number of first PWM controlled by this chip
  256. * @npwm: number of PWMs controlled by this chip
  257. * @of_xlate: request a PWM device given a device tree PWM specifier
  258. * @of_pwm_n_cells: number of cells expected in the device tree PWM specifier
  259. * @list: list node for internal use
  260. * @pwms: array of PWM devices allocated by the framework
  261. */
  262. struct pwm_chip {
  263. struct device *dev;
  264. const struct pwm_ops *ops;
  265. int base;
  266. unsigned int npwm;
  267. struct pwm_device * (*of_xlate)(struct pwm_chip *pc,
  268. const struct of_phandle_args *args);
  269. unsigned int of_pwm_n_cells;
  270. /* only used internally by the PWM framework */
  271. struct list_head list;
  272. struct pwm_device *pwms;
  273. ANDROID_KABI_RESERVE(1);
  274. };
  275. #if IS_ENABLED(CONFIG_PWM)
  276. /* PWM user APIs */
  277. struct pwm_device *pwm_request(int pwm_id, const char *label);
  278. void pwm_free(struct pwm_device *pwm);
  279. int pwm_apply_state(struct pwm_device *pwm, const struct pwm_state *state);
  280. int pwm_adjust_config(struct pwm_device *pwm);
  281. /**
  282. * pwm_config() - change a PWM device configuration
  283. * @pwm: PWM device
  284. * @duty_ns: "on" time (in nanoseconds)
  285. * @period_ns: duration (in nanoseconds) of one cycle
  286. *
  287. * Returns: 0 on success or a negative error code on failure.
  288. */
  289. static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
  290. int period_ns)
  291. {
  292. struct pwm_state state;
  293. if (!pwm)
  294. return -EINVAL;
  295. if (duty_ns < 0 || period_ns < 0)
  296. return -EINVAL;
  297. pwm_get_state(pwm, &state);
  298. if (state.duty_cycle == duty_ns && state.period == period_ns)
  299. return 0;
  300. state.duty_cycle = duty_ns;
  301. state.period = period_ns;
  302. return pwm_apply_state(pwm, &state);
  303. }
  304. /**
  305. * pwm_enable() - start a PWM output toggling
  306. * @pwm: PWM device
  307. *
  308. * Returns: 0 on success or a negative error code on failure.
  309. */
  310. static inline int pwm_enable(struct pwm_device *pwm)
  311. {
  312. struct pwm_state state;
  313. if (!pwm)
  314. return -EINVAL;
  315. pwm_get_state(pwm, &state);
  316. if (state.enabled)
  317. return 0;
  318. state.enabled = true;
  319. return pwm_apply_state(pwm, &state);
  320. }
  321. /**
  322. * pwm_disable() - stop a PWM output toggling
  323. * @pwm: PWM device
  324. */
  325. static inline void pwm_disable(struct pwm_device *pwm)
  326. {
  327. struct pwm_state state;
  328. if (!pwm)
  329. return;
  330. pwm_get_state(pwm, &state);
  331. if (!state.enabled)
  332. return;
  333. state.enabled = false;
  334. pwm_apply_state(pwm, &state);
  335. }
  336. /* PWM provider APIs */
  337. int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
  338. unsigned long timeout);
  339. int pwm_set_chip_data(struct pwm_device *pwm, void *data);
  340. void *pwm_get_chip_data(struct pwm_device *pwm);
  341. int pwmchip_add(struct pwm_chip *chip);
  342. void pwmchip_remove(struct pwm_chip *chip);
  343. int devm_pwmchip_add(struct device *dev, struct pwm_chip *chip);
  344. struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
  345. unsigned int index,
  346. const char *label);
  347. struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *pc,
  348. const struct of_phandle_args *args);
  349. struct pwm_device *of_pwm_single_xlate(struct pwm_chip *pc,
  350. const struct of_phandle_args *args);
  351. struct pwm_device *pwm_get(struct device *dev, const char *con_id);
  352. void pwm_put(struct pwm_device *pwm);
  353. struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
  354. struct pwm_device *devm_fwnode_pwm_get(struct device *dev,
  355. struct fwnode_handle *fwnode,
  356. const char *con_id);
  357. #else
  358. static inline struct pwm_device *pwm_request(int pwm_id, const char *label)
  359. {
  360. might_sleep();
  361. return ERR_PTR(-ENODEV);
  362. }
  363. static inline void pwm_free(struct pwm_device *pwm)
  364. {
  365. might_sleep();
  366. }
  367. static inline int pwm_apply_state(struct pwm_device *pwm,
  368. const struct pwm_state *state)
  369. {
  370. might_sleep();
  371. return -ENOTSUPP;
  372. }
  373. static inline int pwm_adjust_config(struct pwm_device *pwm)
  374. {
  375. return -ENOTSUPP;
  376. }
  377. static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
  378. int period_ns)
  379. {
  380. might_sleep();
  381. return -EINVAL;
  382. }
  383. static inline int pwm_capture(struct pwm_device *pwm,
  384. struct pwm_capture *result,
  385. unsigned long timeout)
  386. {
  387. return -EINVAL;
  388. }
  389. static inline int pwm_enable(struct pwm_device *pwm)
  390. {
  391. might_sleep();
  392. return -EINVAL;
  393. }
  394. static inline void pwm_disable(struct pwm_device *pwm)
  395. {
  396. might_sleep();
  397. }
  398. static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data)
  399. {
  400. return -EINVAL;
  401. }
  402. static inline void *pwm_get_chip_data(struct pwm_device *pwm)
  403. {
  404. return NULL;
  405. }
  406. static inline int pwmchip_add(struct pwm_chip *chip)
  407. {
  408. return -EINVAL;
  409. }
  410. static inline int pwmchip_remove(struct pwm_chip *chip)
  411. {
  412. return -EINVAL;
  413. }
  414. static inline int devm_pwmchip_add(struct device *dev, struct pwm_chip *chip)
  415. {
  416. return -EINVAL;
  417. }
  418. static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
  419. unsigned int index,
  420. const char *label)
  421. {
  422. might_sleep();
  423. return ERR_PTR(-ENODEV);
  424. }
  425. static inline struct pwm_device *pwm_get(struct device *dev,
  426. const char *consumer)
  427. {
  428. might_sleep();
  429. return ERR_PTR(-ENODEV);
  430. }
  431. static inline void pwm_put(struct pwm_device *pwm)
  432. {
  433. might_sleep();
  434. }
  435. static inline struct pwm_device *devm_pwm_get(struct device *dev,
  436. const char *consumer)
  437. {
  438. might_sleep();
  439. return ERR_PTR(-ENODEV);
  440. }
  441. static inline struct pwm_device *
  442. devm_fwnode_pwm_get(struct device *dev, struct fwnode_handle *fwnode,
  443. const char *con_id)
  444. {
  445. might_sleep();
  446. return ERR_PTR(-ENODEV);
  447. }
  448. #endif
  449. static inline void pwm_apply_args(struct pwm_device *pwm)
  450. {
  451. struct pwm_state state = { };
  452. /*
  453. * PWM users calling pwm_apply_args() expect to have a fresh config
  454. * where the polarity and period are set according to pwm_args info.
  455. * The problem is, polarity can only be changed when the PWM is
  456. * disabled.
  457. *
  458. * PWM drivers supporting hardware readout may declare the PWM device
  459. * as enabled, and prevent polarity setting, which changes from the
  460. * existing behavior, where all PWM devices are declared as disabled
  461. * at startup (even if they are actually enabled), thus authorizing
  462. * polarity setting.
  463. *
  464. * To fulfill this requirement, we apply a new state which disables
  465. * the PWM device and set the reference period and polarity config.
  466. *
  467. * Note that PWM users requiring a smooth handover between the
  468. * bootloader and the kernel (like critical regulators controlled by
  469. * PWM devices) will have to switch to the atomic API and avoid calling
  470. * pwm_apply_args().
  471. */
  472. state.enabled = false;
  473. state.polarity = pwm->args.polarity;
  474. state.period = pwm->args.period;
  475. state.usage_power = false;
  476. pwm_apply_state(pwm, &state);
  477. }
  478. struct pwm_lookup {
  479. struct list_head list;
  480. const char *provider;
  481. unsigned int index;
  482. const char *dev_id;
  483. const char *con_id;
  484. unsigned int period;
  485. enum pwm_polarity polarity;
  486. const char *module; /* optional, may be NULL */
  487. };
  488. #define PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, \
  489. _period, _polarity, _module) \
  490. { \
  491. .provider = _provider, \
  492. .index = _index, \
  493. .dev_id = _dev_id, \
  494. .con_id = _con_id, \
  495. .period = _period, \
  496. .polarity = _polarity, \
  497. .module = _module, \
  498. }
  499. #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
  500. PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, _period, \
  501. _polarity, NULL)
  502. #if IS_ENABLED(CONFIG_PWM)
  503. void pwm_add_table(struct pwm_lookup *table, size_t num);
  504. void pwm_remove_table(struct pwm_lookup *table, size_t num);
  505. #else
  506. static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
  507. {
  508. }
  509. static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
  510. {
  511. }
  512. #endif
  513. #ifdef CONFIG_PWM_SYSFS
  514. void pwmchip_sysfs_export(struct pwm_chip *chip);
  515. void pwmchip_sysfs_unexport(struct pwm_chip *chip);
  516. #else
  517. static inline void pwmchip_sysfs_export(struct pwm_chip *chip)
  518. {
  519. }
  520. static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip)
  521. {
  522. }
  523. #endif /* CONFIG_PWM_SYSFS */
  524. #endif /* __LINUX_PWM_H */