pwm-lpc18xx-sct.c 14 KB

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