pwm-lpc18xx-sct.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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 lpc18xx_pwm_data channeldata[LPC18XX_NUM_PWMS];
  85. };
  86. static inline struct lpc18xx_pwm_chip *
  87. to_lpc18xx_pwm_chip(struct pwm_chip *chip)
  88. {
  89. return pwmchip_get_drvdata(chip);
  90. }
  91. static inline void lpc18xx_pwm_writel(struct lpc18xx_pwm_chip *lpc18xx_pwm,
  92. u32 reg, u32 val)
  93. {
  94. writel(val, lpc18xx_pwm->base + reg);
  95. }
  96. static inline u32 lpc18xx_pwm_readl(struct lpc18xx_pwm_chip *lpc18xx_pwm,
  97. u32 reg)
  98. {
  99. return readl(lpc18xx_pwm->base + reg);
  100. }
  101. static void lpc18xx_pwm_set_conflict_res(struct lpc18xx_pwm_chip *lpc18xx_pwm,
  102. struct pwm_device *pwm,
  103. enum lpc18xx_pwm_res_action action)
  104. {
  105. u32 val;
  106. /*
  107. * Simultaneous set and clear may happen on an output, that is the case
  108. * when duty_ns == period_ns. LPC18xx SCT allows to set a conflict
  109. * resolution action to be taken in such a case.
  110. */
  111. val = lpc18xx_pwm_readl(lpc18xx_pwm, LPC18XX_PWM_RES_BASE);
  112. val &= ~LPC18XX_PWM_RES_MASK(pwm->hwpwm);
  113. val |= LPC18XX_PWM_RES(pwm->hwpwm, action);
  114. lpc18xx_pwm_writel(lpc18xx_pwm, LPC18XX_PWM_RES_BASE, val);
  115. }
  116. static void lpc18xx_pwm_config_period(struct pwm_chip *chip, u64 period_ns)
  117. {
  118. struct lpc18xx_pwm_chip *lpc18xx_pwm = to_lpc18xx_pwm_chip(chip);
  119. u32 val;
  120. /*
  121. * With clk_rate < NSEC_PER_SEC this cannot overflow.
  122. * With period_ns < max_period_ns this also fits into an u32.
  123. * As period_ns >= min_period_ns = DIV_ROUND_UP(NSEC_PER_SEC, lpc18xx_pwm->clk_rate);
  124. * we have val >= 1.
  125. */
  126. val = mul_u64_u64_div_u64(period_ns, lpc18xx_pwm->clk_rate, NSEC_PER_SEC);
  127. lpc18xx_pwm_writel(lpc18xx_pwm,
  128. LPC18XX_PWM_MATCH(lpc18xx_pwm->period_event),
  129. val - 1);
  130. lpc18xx_pwm_writel(lpc18xx_pwm,
  131. LPC18XX_PWM_MATCHREL(lpc18xx_pwm->period_event),
  132. val - 1);
  133. }
  134. static void lpc18xx_pwm_config_duty(struct pwm_chip *chip,
  135. struct pwm_device *pwm, u64 duty_ns)
  136. {
  137. struct lpc18xx_pwm_chip *lpc18xx_pwm = to_lpc18xx_pwm_chip(chip);
  138. struct lpc18xx_pwm_data *lpc18xx_data = &lpc18xx_pwm->channeldata[pwm->hwpwm];
  139. u32 val;
  140. /*
  141. * With clk_rate <= NSEC_PER_SEC this cannot overflow.
  142. * With duty_ns <= period_ns < max_period_ns this also fits into an u32.
  143. */
  144. val = mul_u64_u64_div_u64(duty_ns, lpc18xx_pwm->clk_rate, NSEC_PER_SEC);
  145. lpc18xx_pwm_writel(lpc18xx_pwm,
  146. LPC18XX_PWM_MATCH(lpc18xx_data->duty_event),
  147. val);
  148. lpc18xx_pwm_writel(lpc18xx_pwm,
  149. LPC18XX_PWM_MATCHREL(lpc18xx_data->duty_event),
  150. val);
  151. }
  152. static int lpc18xx_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  153. int duty_ns, int period_ns)
  154. {
  155. struct lpc18xx_pwm_chip *lpc18xx_pwm = to_lpc18xx_pwm_chip(chip);
  156. int requested_events;
  157. if (period_ns < lpc18xx_pwm->min_period_ns ||
  158. period_ns > lpc18xx_pwm->max_period_ns) {
  159. dev_err(pwmchip_parent(chip), "period %d not in range\n", period_ns);
  160. return -ERANGE;
  161. }
  162. requested_events = bitmap_weight(&lpc18xx_pwm->event_map,
  163. LPC18XX_PWM_EVENT_MAX);
  164. /*
  165. * The PWM supports only a single period for all PWM channels.
  166. * Once the period is set, it can only be changed if no more than one
  167. * channel is requested at that moment.
  168. */
  169. if (requested_events > 2 && lpc18xx_pwm->period_ns != period_ns &&
  170. lpc18xx_pwm->period_ns) {
  171. dev_err(pwmchip_parent(chip), "conflicting period requested for PWM %u\n",
  172. pwm->hwpwm);
  173. return -EBUSY;
  174. }
  175. if ((requested_events <= 2 && lpc18xx_pwm->period_ns != period_ns) ||
  176. !lpc18xx_pwm->period_ns) {
  177. lpc18xx_pwm->period_ns = period_ns;
  178. lpc18xx_pwm_config_period(chip, period_ns);
  179. }
  180. lpc18xx_pwm_config_duty(chip, pwm, duty_ns);
  181. return 0;
  182. }
  183. static int lpc18xx_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm, enum pwm_polarity polarity)
  184. {
  185. struct lpc18xx_pwm_chip *lpc18xx_pwm = to_lpc18xx_pwm_chip(chip);
  186. struct lpc18xx_pwm_data *lpc18xx_data = &lpc18xx_pwm->channeldata[pwm->hwpwm];
  187. enum lpc18xx_pwm_res_action res_action;
  188. unsigned int set_event, clear_event;
  189. lpc18xx_pwm_writel(lpc18xx_pwm,
  190. LPC18XX_PWM_EVCTRL(lpc18xx_data->duty_event),
  191. LPC18XX_PWM_EVCTRL_MATCH(lpc18xx_data->duty_event) |
  192. LPC18XX_PWM_EVCTRL_COMB_MATCH);
  193. lpc18xx_pwm_writel(lpc18xx_pwm,
  194. LPC18XX_PWM_EVSTATEMSK(lpc18xx_data->duty_event),
  195. LPC18XX_PWM_EVSTATEMSK_ALL);
  196. if (polarity == PWM_POLARITY_NORMAL) {
  197. set_event = lpc18xx_pwm->period_event;
  198. clear_event = lpc18xx_data->duty_event;
  199. res_action = LPC18XX_PWM_RES_SET;
  200. } else {
  201. set_event = lpc18xx_data->duty_event;
  202. clear_event = lpc18xx_pwm->period_event;
  203. res_action = LPC18XX_PWM_RES_CLEAR;
  204. }
  205. lpc18xx_pwm_writel(lpc18xx_pwm, LPC18XX_PWM_OUTPUTSET(pwm->hwpwm),
  206. BIT(set_event));
  207. lpc18xx_pwm_writel(lpc18xx_pwm, LPC18XX_PWM_OUTPUTCL(pwm->hwpwm),
  208. BIT(clear_event));
  209. lpc18xx_pwm_set_conflict_res(lpc18xx_pwm, pwm, res_action);
  210. return 0;
  211. }
  212. static void lpc18xx_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  213. {
  214. struct lpc18xx_pwm_chip *lpc18xx_pwm = to_lpc18xx_pwm_chip(chip);
  215. struct lpc18xx_pwm_data *lpc18xx_data = &lpc18xx_pwm->channeldata[pwm->hwpwm];
  216. lpc18xx_pwm_writel(lpc18xx_pwm,
  217. LPC18XX_PWM_EVCTRL(lpc18xx_data->duty_event), 0);
  218. lpc18xx_pwm_writel(lpc18xx_pwm, LPC18XX_PWM_OUTPUTSET(pwm->hwpwm), 0);
  219. lpc18xx_pwm_writel(lpc18xx_pwm, LPC18XX_PWM_OUTPUTCL(pwm->hwpwm), 0);
  220. }
  221. static int lpc18xx_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
  222. {
  223. struct lpc18xx_pwm_chip *lpc18xx_pwm = to_lpc18xx_pwm_chip(chip);
  224. struct lpc18xx_pwm_data *lpc18xx_data = &lpc18xx_pwm->channeldata[pwm->hwpwm];
  225. unsigned long event;
  226. event = find_first_zero_bit(&lpc18xx_pwm->event_map,
  227. LPC18XX_PWM_EVENT_MAX);
  228. if (event >= LPC18XX_PWM_EVENT_MAX) {
  229. dev_err(pwmchip_parent(chip),
  230. "maximum number of simultaneous channels reached\n");
  231. return -EBUSY;
  232. }
  233. set_bit(event, &lpc18xx_pwm->event_map);
  234. lpc18xx_data->duty_event = event;
  235. return 0;
  236. }
  237. static void lpc18xx_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
  238. {
  239. struct lpc18xx_pwm_chip *lpc18xx_pwm = to_lpc18xx_pwm_chip(chip);
  240. struct lpc18xx_pwm_data *lpc18xx_data = &lpc18xx_pwm->channeldata[pwm->hwpwm];
  241. clear_bit(lpc18xx_data->duty_event, &lpc18xx_pwm->event_map);
  242. }
  243. static int lpc18xx_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  244. const struct pwm_state *state)
  245. {
  246. int err;
  247. bool enabled = pwm->state.enabled;
  248. if (state->polarity != pwm->state.polarity && pwm->state.enabled) {
  249. lpc18xx_pwm_disable(chip, pwm);
  250. enabled = false;
  251. }
  252. if (!state->enabled) {
  253. if (enabled)
  254. lpc18xx_pwm_disable(chip, pwm);
  255. return 0;
  256. }
  257. err = lpc18xx_pwm_config(chip, pwm, state->duty_cycle, state->period);
  258. if (err)
  259. return err;
  260. if (!enabled)
  261. err = lpc18xx_pwm_enable(chip, pwm, state->polarity);
  262. return err;
  263. }
  264. static const struct pwm_ops lpc18xx_pwm_ops = {
  265. .apply = lpc18xx_pwm_apply,
  266. .request = lpc18xx_pwm_request,
  267. .free = lpc18xx_pwm_free,
  268. };
  269. static const struct of_device_id lpc18xx_pwm_of_match[] = {
  270. { .compatible = "nxp,lpc1850-sct-pwm" },
  271. {}
  272. };
  273. MODULE_DEVICE_TABLE(of, lpc18xx_pwm_of_match);
  274. static int lpc18xx_pwm_probe(struct platform_device *pdev)
  275. {
  276. struct pwm_chip *chip;
  277. struct lpc18xx_pwm_chip *lpc18xx_pwm;
  278. int ret;
  279. u64 val;
  280. chip = devm_pwmchip_alloc(&pdev->dev, LPC18XX_NUM_PWMS, sizeof(*lpc18xx_pwm));
  281. if (IS_ERR(chip))
  282. return PTR_ERR(chip);
  283. lpc18xx_pwm = to_lpc18xx_pwm_chip(chip);
  284. lpc18xx_pwm->base = devm_platform_ioremap_resource(pdev, 0);
  285. if (IS_ERR(lpc18xx_pwm->base))
  286. return PTR_ERR(lpc18xx_pwm->base);
  287. lpc18xx_pwm->pwm_clk = devm_clk_get_enabled(&pdev->dev, "pwm");
  288. if (IS_ERR(lpc18xx_pwm->pwm_clk))
  289. return dev_err_probe(&pdev->dev, PTR_ERR(lpc18xx_pwm->pwm_clk),
  290. "failed to get pwm clock\n");
  291. lpc18xx_pwm->clk_rate = clk_get_rate(lpc18xx_pwm->pwm_clk);
  292. if (!lpc18xx_pwm->clk_rate)
  293. return dev_err_probe(&pdev->dev,
  294. -EINVAL, "pwm clock has no frequency\n");
  295. /*
  296. * If clkrate is too fast, the calculations in .apply() might overflow.
  297. */
  298. if (lpc18xx_pwm->clk_rate > NSEC_PER_SEC)
  299. return dev_err_probe(&pdev->dev, -EINVAL, "pwm clock to fast\n");
  300. lpc18xx_pwm->max_period_ns =
  301. mul_u64_u64_div_u64(NSEC_PER_SEC, LPC18XX_PWM_TIMER_MAX, lpc18xx_pwm->clk_rate);
  302. lpc18xx_pwm->min_period_ns = DIV_ROUND_UP(NSEC_PER_SEC,
  303. lpc18xx_pwm->clk_rate);
  304. chip->ops = &lpc18xx_pwm_ops;
  305. /* SCT counter must be in unify (32 bit) mode */
  306. lpc18xx_pwm_writel(lpc18xx_pwm, LPC18XX_PWM_CONFIG,
  307. LPC18XX_PWM_CONFIG_UNIFY);
  308. /*
  309. * Everytime the timer counter reaches the period value, the related
  310. * event will be triggered and the counter reset to 0.
  311. */
  312. set_bit(LPC18XX_PWM_EVENT_PERIOD, &lpc18xx_pwm->event_map);
  313. lpc18xx_pwm->period_event = LPC18XX_PWM_EVENT_PERIOD;
  314. lpc18xx_pwm_writel(lpc18xx_pwm,
  315. LPC18XX_PWM_EVSTATEMSK(lpc18xx_pwm->period_event),
  316. LPC18XX_PWM_EVSTATEMSK_ALL);
  317. val = LPC18XX_PWM_EVCTRL_MATCH(lpc18xx_pwm->period_event) |
  318. LPC18XX_PWM_EVCTRL_COMB_MATCH;
  319. lpc18xx_pwm_writel(lpc18xx_pwm,
  320. LPC18XX_PWM_EVCTRL(lpc18xx_pwm->period_event), val);
  321. lpc18xx_pwm_writel(lpc18xx_pwm, LPC18XX_PWM_LIMIT,
  322. BIT(lpc18xx_pwm->period_event));
  323. val = lpc18xx_pwm_readl(lpc18xx_pwm, LPC18XX_PWM_CTRL);
  324. val &= ~LPC18XX_PWM_BIDIR;
  325. val &= ~LPC18XX_PWM_CTRL_HALT;
  326. val &= ~LPC18XX_PWM_PRE_MASK;
  327. val |= LPC18XX_PWM_PRE(0);
  328. lpc18xx_pwm_writel(lpc18xx_pwm, LPC18XX_PWM_CTRL, val);
  329. ret = pwmchip_add(chip);
  330. if (ret < 0)
  331. return dev_err_probe(&pdev->dev, ret, "pwmchip_add failed\n");
  332. platform_set_drvdata(pdev, chip);
  333. return 0;
  334. }
  335. static void lpc18xx_pwm_remove(struct platform_device *pdev)
  336. {
  337. struct pwm_chip *chip = platform_get_drvdata(pdev);
  338. struct lpc18xx_pwm_chip *lpc18xx_pwm = to_lpc18xx_pwm_chip(chip);
  339. u32 val;
  340. pwmchip_remove(chip);
  341. val = lpc18xx_pwm_readl(lpc18xx_pwm, LPC18XX_PWM_CTRL);
  342. lpc18xx_pwm_writel(lpc18xx_pwm, LPC18XX_PWM_CTRL,
  343. val | LPC18XX_PWM_CTRL_HALT);
  344. }
  345. static struct platform_driver lpc18xx_pwm_driver = {
  346. .driver = {
  347. .name = "lpc18xx-sct-pwm",
  348. .of_match_table = lpc18xx_pwm_of_match,
  349. },
  350. .probe = lpc18xx_pwm_probe,
  351. .remove = lpc18xx_pwm_remove,
  352. };
  353. module_platform_driver(lpc18xx_pwm_driver);
  354. MODULE_AUTHOR("Ariel D'Alessandro <ariel@vanguardiasur.com.ar>");
  355. MODULE_DESCRIPTION("NXP LPC18xx PWM driver");
  356. MODULE_LICENSE("GPL v2");