pwm-lpc18xx-sct.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * NXP LPC18xx State Configurable Timer - Pulse Width Modulator driver
  4. *
  5. * Copyright (c) 2015 Ariel D'Alessandro <ariel@vanguardiasur.com>
  6. *
  7. * Notes
  8. * =====
  9. * NXP LPC18xx provides a State Configurable Timer (SCT) which can be configured
  10. * as a Pulse Width Modulator.
  11. *
  12. * SCT supports 16 outputs, 16 events and 16 registers. Each event will be
  13. * triggered when its related register matches the SCT counter value, and it
  14. * will set or clear a selected output.
  15. *
  16. * One of the events is preselected to generate the period, thus the maximum
  17. * number of simultaneous channels is limited to 15. Notice that period is
  18. * global to all the channels, thus PWM driver will refuse setting different
  19. * values to it, unless there's only one channel requested.
  20. */
  21. #include <linux/clk.h>
  22. #include <linux/err.h>
  23. #include <linux/io.h>
  24. #include <linux/mod_devicetable.h>
  25. #include <linux/module.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/pwm.h>
  28. /* LPC18xx SCT registers */
  29. #define LPC18XX_PWM_CONFIG 0x000
  30. #define LPC18XX_PWM_CONFIG_UNIFY BIT(0)
  31. #define LPC18XX_PWM_CONFIG_NORELOAD BIT(7)
  32. #define LPC18XX_PWM_CTRL 0x004
  33. #define LPC18XX_PWM_CTRL_HALT BIT(2)
  34. #define LPC18XX_PWM_BIDIR BIT(4)
  35. #define LPC18XX_PWM_PRE_SHIFT 5
  36. #define LPC18XX_PWM_PRE_MASK (0xff << LPC18XX_PWM_PRE_SHIFT)
  37. #define LPC18XX_PWM_PRE(x) (x << LPC18XX_PWM_PRE_SHIFT)
  38. #define LPC18XX_PWM_LIMIT 0x008
  39. #define LPC18XX_PWM_RES_BASE 0x058
  40. #define LPC18XX_PWM_RES_SHIFT(_ch) (_ch * 2)
  41. #define LPC18XX_PWM_RES(_ch, _action) (_action << LPC18XX_PWM_RES_SHIFT(_ch))
  42. #define LPC18XX_PWM_RES_MASK(_ch) (0x3 << LPC18XX_PWM_RES_SHIFT(_ch))
  43. #define LPC18XX_PWM_MATCH_BASE 0x100
  44. #define LPC18XX_PWM_MATCH(_ch) (LPC18XX_PWM_MATCH_BASE + _ch * 4)
  45. #define LPC18XX_PWM_MATCHREL_BASE 0x200
  46. #define LPC18XX_PWM_MATCHREL(_ch) (LPC18XX_PWM_MATCHREL_BASE + _ch * 4)
  47. #define LPC18XX_PWM_EVSTATEMSK_BASE 0x300
  48. #define LPC18XX_PWM_EVSTATEMSK(_ch) (LPC18XX_PWM_EVSTATEMSK_BASE + _ch * 8)
  49. #define LPC18XX_PWM_EVSTATEMSK_ALL 0xffffffff
  50. #define LPC18XX_PWM_EVCTRL_BASE 0x304
  51. #define LPC18XX_PWM_EVCTRL(_ev) (LPC18XX_PWM_EVCTRL_BASE + _ev * 8)
  52. #define LPC18XX_PWM_EVCTRL_MATCH(_ch) _ch
  53. #define LPC18XX_PWM_EVCTRL_COMB_SHIFT 12
  54. #define LPC18XX_PWM_EVCTRL_COMB_MATCH (0x1 << LPC18XX_PWM_EVCTRL_COMB_SHIFT)
  55. #define LPC18XX_PWM_OUTPUTSET_BASE 0x500
  56. #define LPC18XX_PWM_OUTPUTSET(_ch) (LPC18XX_PWM_OUTPUTSET_BASE + _ch * 8)
  57. #define LPC18XX_PWM_OUTPUTCL_BASE 0x504
  58. #define LPC18XX_PWM_OUTPUTCL(_ch) (LPC18XX_PWM_OUTPUTCL_BASE + _ch * 8)
  59. /* LPC18xx SCT unified counter */
  60. #define LPC18XX_PWM_TIMER_MAX 0xffffffff
  61. /* LPC18xx SCT events */
  62. #define LPC18XX_PWM_EVENT_PERIOD 0
  63. #define LPC18XX_PWM_EVENT_MAX 16
  64. #define LPC18XX_NUM_PWMS 16
  65. /* SCT conflict resolution */
  66. enum lpc18xx_pwm_res_action {
  67. LPC18XX_PWM_RES_NONE,
  68. LPC18XX_PWM_RES_SET,
  69. LPC18XX_PWM_RES_CLEAR,
  70. LPC18XX_PWM_RES_TOGGLE,
  71. };
  72. struct lpc18xx_pwm_data {
  73. unsigned int duty_event;
  74. };
  75. struct lpc18xx_pwm_chip {
  76. void __iomem *base;
  77. struct clk *pwm_clk;
  78. unsigned long clk_rate;
  79. unsigned int period_ns;
  80. unsigned int min_period_ns;
  81. u64 max_period_ns;
  82. unsigned int period_event;
  83. unsigned long event_map;
  84. struct mutex res_lock;
  85. struct mutex period_lock;
  86. struct lpc18xx_pwm_data channeldata[LPC18XX_NUM_PWMS];
  87. };
  88. static inline struct lpc18xx_pwm_chip *
  89. to_lpc18xx_pwm_chip(struct pwm_chip *chip)
  90. {
  91. return pwmchip_get_drvdata(chip);
  92. }
  93. static inline void lpc18xx_pwm_writel(struct lpc18xx_pwm_chip *lpc18xx_pwm,
  94. u32 reg, u32 val)
  95. {
  96. writel(val, lpc18xx_pwm->base + reg);
  97. }
  98. static inline u32 lpc18xx_pwm_readl(struct lpc18xx_pwm_chip *lpc18xx_pwm,
  99. u32 reg)
  100. {
  101. return readl(lpc18xx_pwm->base + reg);
  102. }
  103. static void lpc18xx_pwm_set_conflict_res(struct lpc18xx_pwm_chip *lpc18xx_pwm,
  104. struct pwm_device *pwm,
  105. enum lpc18xx_pwm_res_action action)
  106. {
  107. u32 val;
  108. mutex_lock(&lpc18xx_pwm->res_lock);
  109. /*
  110. * Simultaneous set and clear may happen on an output, that is the case
  111. * when duty_ns == period_ns. LPC18xx SCT allows to set a conflict
  112. * resolution action to be taken in such a case.
  113. */
  114. val = lpc18xx_pwm_readl(lpc18xx_pwm, LPC18XX_PWM_RES_BASE);
  115. val &= ~LPC18XX_PWM_RES_MASK(pwm->hwpwm);
  116. val |= LPC18XX_PWM_RES(pwm->hwpwm, action);
  117. lpc18xx_pwm_writel(lpc18xx_pwm, LPC18XX_PWM_RES_BASE, val);
  118. mutex_unlock(&lpc18xx_pwm->res_lock);
  119. }
  120. static void lpc18xx_pwm_config_period(struct pwm_chip *chip, u64 period_ns)
  121. {
  122. struct lpc18xx_pwm_chip *lpc18xx_pwm = to_lpc18xx_pwm_chip(chip);
  123. u32 val;
  124. /*
  125. * With clk_rate < NSEC_PER_SEC this cannot overflow.
  126. * With period_ns < max_period_ns this also fits into an u32.
  127. * As period_ns >= min_period_ns = DIV_ROUND_UP(NSEC_PER_SEC, lpc18xx_pwm->clk_rate);
  128. * we have val >= 1.
  129. */
  130. val = mul_u64_u64_div_u64(period_ns, lpc18xx_pwm->clk_rate, NSEC_PER_SEC);
  131. lpc18xx_pwm_writel(lpc18xx_pwm,
  132. LPC18XX_PWM_MATCH(lpc18xx_pwm->period_event),
  133. val - 1);
  134. lpc18xx_pwm_writel(lpc18xx_pwm,
  135. LPC18XX_PWM_MATCHREL(lpc18xx_pwm->period_event),
  136. val - 1);
  137. }
  138. static void lpc18xx_pwm_config_duty(struct pwm_chip *chip,
  139. struct pwm_device *pwm, u64 duty_ns)
  140. {
  141. struct lpc18xx_pwm_chip *lpc18xx_pwm = to_lpc18xx_pwm_chip(chip);
  142. struct lpc18xx_pwm_data *lpc18xx_data = &lpc18xx_pwm->channeldata[pwm->hwpwm];
  143. u32 val;
  144. /*
  145. * With clk_rate <= NSEC_PER_SEC this cannot overflow.
  146. * With duty_ns <= period_ns < max_period_ns this also fits into an u32.
  147. */
  148. val = mul_u64_u64_div_u64(duty_ns, lpc18xx_pwm->clk_rate, NSEC_PER_SEC);
  149. lpc18xx_pwm_writel(lpc18xx_pwm,
  150. LPC18XX_PWM_MATCH(lpc18xx_data->duty_event),
  151. val);
  152. lpc18xx_pwm_writel(lpc18xx_pwm,
  153. LPC18XX_PWM_MATCHREL(lpc18xx_data->duty_event),
  154. val);
  155. }
  156. static int lpc18xx_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  157. int duty_ns, int period_ns)
  158. {
  159. struct lpc18xx_pwm_chip *lpc18xx_pwm = to_lpc18xx_pwm_chip(chip);
  160. int requested_events;
  161. if (period_ns < lpc18xx_pwm->min_period_ns ||
  162. period_ns > lpc18xx_pwm->max_period_ns) {
  163. dev_err(pwmchip_parent(chip), "period %d not in range\n", period_ns);
  164. return -ERANGE;
  165. }
  166. mutex_lock(&lpc18xx_pwm->period_lock);
  167. requested_events = bitmap_weight(&lpc18xx_pwm->event_map,
  168. LPC18XX_PWM_EVENT_MAX);
  169. /*
  170. * The PWM supports only a single period for all PWM channels.
  171. * Once the period is set, it can only be changed if no more than one
  172. * channel is requested at that moment.
  173. */
  174. if (requested_events > 2 && lpc18xx_pwm->period_ns != period_ns &&
  175. lpc18xx_pwm->period_ns) {
  176. dev_err(pwmchip_parent(chip), "conflicting period requested for PWM %u\n",
  177. pwm->hwpwm);
  178. mutex_unlock(&lpc18xx_pwm->period_lock);
  179. return -EBUSY;
  180. }
  181. if ((requested_events <= 2 && lpc18xx_pwm->period_ns != period_ns) ||
  182. !lpc18xx_pwm->period_ns) {
  183. lpc18xx_pwm->period_ns = period_ns;
  184. lpc18xx_pwm_config_period(chip, period_ns);
  185. }
  186. mutex_unlock(&lpc18xx_pwm->period_lock);
  187. lpc18xx_pwm_config_duty(chip, pwm, duty_ns);
  188. return 0;
  189. }
  190. static int lpc18xx_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm, enum pwm_polarity polarity)
  191. {
  192. struct lpc18xx_pwm_chip *lpc18xx_pwm = to_lpc18xx_pwm_chip(chip);
  193. struct lpc18xx_pwm_data *lpc18xx_data = &lpc18xx_pwm->channeldata[pwm->hwpwm];
  194. enum lpc18xx_pwm_res_action res_action;
  195. unsigned int set_event, clear_event;
  196. lpc18xx_pwm_writel(lpc18xx_pwm,
  197. LPC18XX_PWM_EVCTRL(lpc18xx_data->duty_event),
  198. LPC18XX_PWM_EVCTRL_MATCH(lpc18xx_data->duty_event) |
  199. LPC18XX_PWM_EVCTRL_COMB_MATCH);
  200. lpc18xx_pwm_writel(lpc18xx_pwm,
  201. LPC18XX_PWM_EVSTATEMSK(lpc18xx_data->duty_event),
  202. LPC18XX_PWM_EVSTATEMSK_ALL);
  203. if (polarity == PWM_POLARITY_NORMAL) {
  204. set_event = lpc18xx_pwm->period_event;
  205. clear_event = lpc18xx_data->duty_event;
  206. res_action = LPC18XX_PWM_RES_SET;
  207. } else {
  208. set_event = lpc18xx_data->duty_event;
  209. clear_event = lpc18xx_pwm->period_event;
  210. res_action = LPC18XX_PWM_RES_CLEAR;
  211. }
  212. lpc18xx_pwm_writel(lpc18xx_pwm, LPC18XX_PWM_OUTPUTSET(pwm->hwpwm),
  213. BIT(set_event));
  214. lpc18xx_pwm_writel(lpc18xx_pwm, LPC18XX_PWM_OUTPUTCL(pwm->hwpwm),
  215. BIT(clear_event));
  216. lpc18xx_pwm_set_conflict_res(lpc18xx_pwm, pwm, res_action);
  217. return 0;
  218. }
  219. static void lpc18xx_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  220. {
  221. struct lpc18xx_pwm_chip *lpc18xx_pwm = to_lpc18xx_pwm_chip(chip);
  222. struct lpc18xx_pwm_data *lpc18xx_data = &lpc18xx_pwm->channeldata[pwm->hwpwm];
  223. lpc18xx_pwm_writel(lpc18xx_pwm,
  224. LPC18XX_PWM_EVCTRL(lpc18xx_data->duty_event), 0);
  225. lpc18xx_pwm_writel(lpc18xx_pwm, LPC18XX_PWM_OUTPUTSET(pwm->hwpwm), 0);
  226. lpc18xx_pwm_writel(lpc18xx_pwm, LPC18XX_PWM_OUTPUTCL(pwm->hwpwm), 0);
  227. }
  228. static int lpc18xx_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
  229. {
  230. struct lpc18xx_pwm_chip *lpc18xx_pwm = to_lpc18xx_pwm_chip(chip);
  231. struct lpc18xx_pwm_data *lpc18xx_data = &lpc18xx_pwm->channeldata[pwm->hwpwm];
  232. unsigned long event;
  233. event = find_first_zero_bit(&lpc18xx_pwm->event_map,
  234. LPC18XX_PWM_EVENT_MAX);
  235. if (event >= LPC18XX_PWM_EVENT_MAX) {
  236. dev_err(pwmchip_parent(chip),
  237. "maximum number of simultaneous channels reached\n");
  238. return -EBUSY;
  239. }
  240. set_bit(event, &lpc18xx_pwm->event_map);
  241. lpc18xx_data->duty_event = event;
  242. return 0;
  243. }
  244. static void lpc18xx_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
  245. {
  246. struct lpc18xx_pwm_chip *lpc18xx_pwm = to_lpc18xx_pwm_chip(chip);
  247. struct lpc18xx_pwm_data *lpc18xx_data = &lpc18xx_pwm->channeldata[pwm->hwpwm];
  248. clear_bit(lpc18xx_data->duty_event, &lpc18xx_pwm->event_map);
  249. }
  250. static int lpc18xx_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  251. const struct pwm_state *state)
  252. {
  253. int err;
  254. bool enabled = pwm->state.enabled;
  255. if (state->polarity != pwm->state.polarity && pwm->state.enabled) {
  256. lpc18xx_pwm_disable(chip, pwm);
  257. enabled = false;
  258. }
  259. if (!state->enabled) {
  260. if (enabled)
  261. lpc18xx_pwm_disable(chip, pwm);
  262. return 0;
  263. }
  264. err = lpc18xx_pwm_config(chip, pwm, state->duty_cycle, state->period);
  265. if (err)
  266. return err;
  267. if (!enabled)
  268. err = lpc18xx_pwm_enable(chip, pwm, state->polarity);
  269. return err;
  270. }
  271. static const struct pwm_ops lpc18xx_pwm_ops = {
  272. .apply = lpc18xx_pwm_apply,
  273. .request = lpc18xx_pwm_request,
  274. .free = lpc18xx_pwm_free,
  275. };
  276. static const struct of_device_id lpc18xx_pwm_of_match[] = {
  277. { .compatible = "nxp,lpc1850-sct-pwm" },
  278. {}
  279. };
  280. MODULE_DEVICE_TABLE(of, lpc18xx_pwm_of_match);
  281. static int lpc18xx_pwm_probe(struct platform_device *pdev)
  282. {
  283. struct pwm_chip *chip;
  284. struct lpc18xx_pwm_chip *lpc18xx_pwm;
  285. int ret;
  286. u64 val;
  287. chip = devm_pwmchip_alloc(&pdev->dev, LPC18XX_NUM_PWMS, sizeof(*lpc18xx_pwm));
  288. if (IS_ERR(chip))
  289. return PTR_ERR(chip);
  290. lpc18xx_pwm = to_lpc18xx_pwm_chip(chip);
  291. lpc18xx_pwm->base = devm_platform_ioremap_resource(pdev, 0);
  292. if (IS_ERR(lpc18xx_pwm->base))
  293. return PTR_ERR(lpc18xx_pwm->base);
  294. lpc18xx_pwm->pwm_clk = devm_clk_get_enabled(&pdev->dev, "pwm");
  295. if (IS_ERR(lpc18xx_pwm->pwm_clk))
  296. return dev_err_probe(&pdev->dev, PTR_ERR(lpc18xx_pwm->pwm_clk),
  297. "failed to get pwm clock\n");
  298. lpc18xx_pwm->clk_rate = clk_get_rate(lpc18xx_pwm->pwm_clk);
  299. if (!lpc18xx_pwm->clk_rate)
  300. return dev_err_probe(&pdev->dev,
  301. -EINVAL, "pwm clock has no frequency\n");
  302. /*
  303. * If clkrate is too fast, the calculations in .apply() might overflow.
  304. */
  305. if (lpc18xx_pwm->clk_rate > NSEC_PER_SEC)
  306. return dev_err_probe(&pdev->dev, -EINVAL, "pwm clock to fast\n");
  307. mutex_init(&lpc18xx_pwm->res_lock);
  308. mutex_init(&lpc18xx_pwm->period_lock);
  309. lpc18xx_pwm->max_period_ns =
  310. mul_u64_u64_div_u64(NSEC_PER_SEC, LPC18XX_PWM_TIMER_MAX, lpc18xx_pwm->clk_rate);
  311. lpc18xx_pwm->min_period_ns = DIV_ROUND_UP(NSEC_PER_SEC,
  312. lpc18xx_pwm->clk_rate);
  313. chip->ops = &lpc18xx_pwm_ops;
  314. /* SCT counter must be in unify (32 bit) mode */
  315. lpc18xx_pwm_writel(lpc18xx_pwm, LPC18XX_PWM_CONFIG,
  316. LPC18XX_PWM_CONFIG_UNIFY);
  317. /*
  318. * Everytime the timer counter reaches the period value, the related
  319. * event will be triggered and the counter reset to 0.
  320. */
  321. set_bit(LPC18XX_PWM_EVENT_PERIOD, &lpc18xx_pwm->event_map);
  322. lpc18xx_pwm->period_event = LPC18XX_PWM_EVENT_PERIOD;
  323. lpc18xx_pwm_writel(lpc18xx_pwm,
  324. LPC18XX_PWM_EVSTATEMSK(lpc18xx_pwm->period_event),
  325. LPC18XX_PWM_EVSTATEMSK_ALL);
  326. val = LPC18XX_PWM_EVCTRL_MATCH(lpc18xx_pwm->period_event) |
  327. LPC18XX_PWM_EVCTRL_COMB_MATCH;
  328. lpc18xx_pwm_writel(lpc18xx_pwm,
  329. LPC18XX_PWM_EVCTRL(lpc18xx_pwm->period_event), val);
  330. lpc18xx_pwm_writel(lpc18xx_pwm, LPC18XX_PWM_LIMIT,
  331. BIT(lpc18xx_pwm->period_event));
  332. val = lpc18xx_pwm_readl(lpc18xx_pwm, LPC18XX_PWM_CTRL);
  333. val &= ~LPC18XX_PWM_BIDIR;
  334. val &= ~LPC18XX_PWM_CTRL_HALT;
  335. val &= ~LPC18XX_PWM_PRE_MASK;
  336. val |= LPC18XX_PWM_PRE(0);
  337. lpc18xx_pwm_writel(lpc18xx_pwm, LPC18XX_PWM_CTRL, val);
  338. ret = pwmchip_add(chip);
  339. if (ret < 0)
  340. return dev_err_probe(&pdev->dev, ret, "pwmchip_add failed\n");
  341. platform_set_drvdata(pdev, chip);
  342. return 0;
  343. }
  344. static void lpc18xx_pwm_remove(struct platform_device *pdev)
  345. {
  346. struct pwm_chip *chip = platform_get_drvdata(pdev);
  347. struct lpc18xx_pwm_chip *lpc18xx_pwm = to_lpc18xx_pwm_chip(chip);
  348. u32 val;
  349. pwmchip_remove(chip);
  350. val = lpc18xx_pwm_readl(lpc18xx_pwm, LPC18XX_PWM_CTRL);
  351. lpc18xx_pwm_writel(lpc18xx_pwm, LPC18XX_PWM_CTRL,
  352. val | LPC18XX_PWM_CTRL_HALT);
  353. }
  354. static struct platform_driver lpc18xx_pwm_driver = {
  355. .driver = {
  356. .name = "lpc18xx-sct-pwm",
  357. .of_match_table = lpc18xx_pwm_of_match,
  358. },
  359. .probe = lpc18xx_pwm_probe,
  360. .remove = lpc18xx_pwm_remove,
  361. };
  362. module_platform_driver(lpc18xx_pwm_driver);
  363. MODULE_AUTHOR("Ariel D'Alessandro <ariel@vanguardiasur.com.ar>");
  364. MODULE_DESCRIPTION("NXP LPC18xx PWM driver");
  365. MODULE_LICENSE("GPL v2");