pwm-fsl-ftm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. /*
  2. * Freescale FlexTimer Module (FTM) PWM Driver
  3. *
  4. * Copyright 2012-2013 Freescale Semiconductor, Inc.
  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, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/err.h>
  13. #include <linux/io.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/mutex.h>
  17. #include <linux/of_address.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/pm.h>
  20. #include <linux/pwm.h>
  21. #include <linux/regmap.h>
  22. #include <linux/slab.h>
  23. #define FTM_SC 0x00
  24. #define FTM_SC_CLK_MASK_SHIFT 3
  25. #define FTM_SC_CLK_MASK (3 << FTM_SC_CLK_MASK_SHIFT)
  26. #define FTM_SC_CLK(c) (((c) + 1) << FTM_SC_CLK_MASK_SHIFT)
  27. #define FTM_SC_PS_MASK 0x7
  28. #define FTM_CNT 0x04
  29. #define FTM_MOD 0x08
  30. #define FTM_CSC_BASE 0x0C
  31. #define FTM_CSC_MSB BIT(5)
  32. #define FTM_CSC_MSA BIT(4)
  33. #define FTM_CSC_ELSB BIT(3)
  34. #define FTM_CSC_ELSA BIT(2)
  35. #define FTM_CSC(_channel) (FTM_CSC_BASE + ((_channel) * 8))
  36. #define FTM_CV_BASE 0x10
  37. #define FTM_CV(_channel) (FTM_CV_BASE + ((_channel) * 8))
  38. #define FTM_CNTIN 0x4C
  39. #define FTM_STATUS 0x50
  40. #define FTM_MODE 0x54
  41. #define FTM_MODE_FTMEN BIT(0)
  42. #define FTM_MODE_INIT BIT(2)
  43. #define FTM_MODE_PWMSYNC BIT(3)
  44. #define FTM_SYNC 0x58
  45. #define FTM_OUTINIT 0x5C
  46. #define FTM_OUTMASK 0x60
  47. #define FTM_COMBINE 0x64
  48. #define FTM_DEADTIME 0x68
  49. #define FTM_EXTTRIG 0x6C
  50. #define FTM_POL 0x70
  51. #define FTM_FMS 0x74
  52. #define FTM_FILTER 0x78
  53. #define FTM_FLTCTRL 0x7C
  54. #define FTM_QDCTRL 0x80
  55. #define FTM_CONF 0x84
  56. #define FTM_FLTPOL 0x88
  57. #define FTM_SYNCONF 0x8C
  58. #define FTM_INVCTRL 0x90
  59. #define FTM_SWOCTRL 0x94
  60. #define FTM_PWMLOAD 0x98
  61. enum fsl_pwm_clk {
  62. FSL_PWM_CLK_SYS,
  63. FSL_PWM_CLK_FIX,
  64. FSL_PWM_CLK_EXT,
  65. FSL_PWM_CLK_CNTEN,
  66. FSL_PWM_CLK_MAX
  67. };
  68. struct fsl_pwm_chip {
  69. struct pwm_chip chip;
  70. struct mutex lock;
  71. unsigned int cnt_select;
  72. unsigned int clk_ps;
  73. struct regmap *regmap;
  74. int period_ns;
  75. struct clk *clk[FSL_PWM_CLK_MAX];
  76. };
  77. static inline struct fsl_pwm_chip *to_fsl_chip(struct pwm_chip *chip)
  78. {
  79. return container_of(chip, struct fsl_pwm_chip, chip);
  80. }
  81. static int fsl_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
  82. {
  83. struct fsl_pwm_chip *fpc = to_fsl_chip(chip);
  84. return clk_prepare_enable(fpc->clk[FSL_PWM_CLK_SYS]);
  85. }
  86. static void fsl_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
  87. {
  88. struct fsl_pwm_chip *fpc = to_fsl_chip(chip);
  89. clk_disable_unprepare(fpc->clk[FSL_PWM_CLK_SYS]);
  90. }
  91. static int fsl_pwm_calculate_default_ps(struct fsl_pwm_chip *fpc,
  92. enum fsl_pwm_clk index)
  93. {
  94. unsigned long sys_rate, cnt_rate;
  95. unsigned long long ratio;
  96. sys_rate = clk_get_rate(fpc->clk[FSL_PWM_CLK_SYS]);
  97. if (!sys_rate)
  98. return -EINVAL;
  99. cnt_rate = clk_get_rate(fpc->clk[fpc->cnt_select]);
  100. if (!cnt_rate)
  101. return -EINVAL;
  102. switch (index) {
  103. case FSL_PWM_CLK_SYS:
  104. fpc->clk_ps = 1;
  105. break;
  106. case FSL_PWM_CLK_FIX:
  107. ratio = 2 * cnt_rate - 1;
  108. do_div(ratio, sys_rate);
  109. fpc->clk_ps = ratio;
  110. break;
  111. case FSL_PWM_CLK_EXT:
  112. ratio = 4 * cnt_rate - 1;
  113. do_div(ratio, sys_rate);
  114. fpc->clk_ps = ratio;
  115. break;
  116. default:
  117. return -EINVAL;
  118. }
  119. return 0;
  120. }
  121. static unsigned long fsl_pwm_calculate_cycles(struct fsl_pwm_chip *fpc,
  122. unsigned long period_ns)
  123. {
  124. unsigned long long c, c0;
  125. c = clk_get_rate(fpc->clk[fpc->cnt_select]);
  126. c = c * period_ns;
  127. do_div(c, 1000000000UL);
  128. do {
  129. c0 = c;
  130. do_div(c0, (1 << fpc->clk_ps));
  131. if (c0 <= 0xFFFF)
  132. return (unsigned long)c0;
  133. } while (++fpc->clk_ps < 8);
  134. return 0;
  135. }
  136. static unsigned long fsl_pwm_calculate_period_cycles(struct fsl_pwm_chip *fpc,
  137. unsigned long period_ns,
  138. enum fsl_pwm_clk index)
  139. {
  140. int ret;
  141. ret = fsl_pwm_calculate_default_ps(fpc, index);
  142. if (ret) {
  143. dev_err(fpc->chip.dev,
  144. "failed to calculate default prescaler: %d\n",
  145. ret);
  146. return 0;
  147. }
  148. return fsl_pwm_calculate_cycles(fpc, period_ns);
  149. }
  150. static unsigned long fsl_pwm_calculate_period(struct fsl_pwm_chip *fpc,
  151. unsigned long period_ns)
  152. {
  153. enum fsl_pwm_clk m0, m1;
  154. unsigned long fix_rate, ext_rate, cycles;
  155. cycles = fsl_pwm_calculate_period_cycles(fpc, period_ns,
  156. FSL_PWM_CLK_SYS);
  157. if (cycles) {
  158. fpc->cnt_select = FSL_PWM_CLK_SYS;
  159. return cycles;
  160. }
  161. fix_rate = clk_get_rate(fpc->clk[FSL_PWM_CLK_FIX]);
  162. ext_rate = clk_get_rate(fpc->clk[FSL_PWM_CLK_EXT]);
  163. if (fix_rate > ext_rate) {
  164. m0 = FSL_PWM_CLK_FIX;
  165. m1 = FSL_PWM_CLK_EXT;
  166. } else {
  167. m0 = FSL_PWM_CLK_EXT;
  168. m1 = FSL_PWM_CLK_FIX;
  169. }
  170. cycles = fsl_pwm_calculate_period_cycles(fpc, period_ns, m0);
  171. if (cycles) {
  172. fpc->cnt_select = m0;
  173. return cycles;
  174. }
  175. fpc->cnt_select = m1;
  176. return fsl_pwm_calculate_period_cycles(fpc, period_ns, m1);
  177. }
  178. static unsigned long fsl_pwm_calculate_duty(struct fsl_pwm_chip *fpc,
  179. unsigned long period_ns,
  180. unsigned long duty_ns)
  181. {
  182. unsigned long long duty;
  183. u32 val;
  184. regmap_read(fpc->regmap, FTM_MOD, &val);
  185. duty = (unsigned long long)duty_ns * (val + 1);
  186. do_div(duty, period_ns);
  187. return (unsigned long)duty;
  188. }
  189. static int fsl_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  190. int duty_ns, int period_ns)
  191. {
  192. struct fsl_pwm_chip *fpc = to_fsl_chip(chip);
  193. u32 period, duty;
  194. mutex_lock(&fpc->lock);
  195. /*
  196. * The Freescale FTM controller supports only a single period for
  197. * all PWM channels, therefore incompatible changes need to be
  198. * refused.
  199. */
  200. if (fpc->period_ns && fpc->period_ns != period_ns) {
  201. dev_err(fpc->chip.dev,
  202. "conflicting period requested for PWM %u\n",
  203. pwm->hwpwm);
  204. mutex_unlock(&fpc->lock);
  205. return -EBUSY;
  206. }
  207. if (!fpc->period_ns && duty_ns) {
  208. period = fsl_pwm_calculate_period(fpc, period_ns);
  209. if (!period) {
  210. dev_err(fpc->chip.dev, "failed to calculate period\n");
  211. mutex_unlock(&fpc->lock);
  212. return -EINVAL;
  213. }
  214. regmap_update_bits(fpc->regmap, FTM_SC, FTM_SC_PS_MASK,
  215. fpc->clk_ps);
  216. regmap_write(fpc->regmap, FTM_MOD, period - 1);
  217. fpc->period_ns = period_ns;
  218. }
  219. mutex_unlock(&fpc->lock);
  220. duty = fsl_pwm_calculate_duty(fpc, period_ns, duty_ns);
  221. regmap_write(fpc->regmap, FTM_CSC(pwm->hwpwm),
  222. FTM_CSC_MSB | FTM_CSC_ELSB);
  223. regmap_write(fpc->regmap, FTM_CV(pwm->hwpwm), duty);
  224. return 0;
  225. }
  226. static int fsl_pwm_set_polarity(struct pwm_chip *chip,
  227. struct pwm_device *pwm,
  228. enum pwm_polarity polarity)
  229. {
  230. struct fsl_pwm_chip *fpc = to_fsl_chip(chip);
  231. u32 val;
  232. regmap_read(fpc->regmap, FTM_POL, &val);
  233. if (polarity == PWM_POLARITY_INVERSED)
  234. val |= BIT(pwm->hwpwm);
  235. else
  236. val &= ~BIT(pwm->hwpwm);
  237. regmap_write(fpc->regmap, FTM_POL, val);
  238. return 0;
  239. }
  240. static int fsl_counter_clock_enable(struct fsl_pwm_chip *fpc)
  241. {
  242. int ret;
  243. /* select counter clock source */
  244. regmap_update_bits(fpc->regmap, FTM_SC, FTM_SC_CLK_MASK,
  245. FTM_SC_CLK(fpc->cnt_select));
  246. ret = clk_prepare_enable(fpc->clk[fpc->cnt_select]);
  247. if (ret)
  248. return ret;
  249. ret = clk_prepare_enable(fpc->clk[FSL_PWM_CLK_CNTEN]);
  250. if (ret) {
  251. clk_disable_unprepare(fpc->clk[fpc->cnt_select]);
  252. return ret;
  253. }
  254. return 0;
  255. }
  256. static int fsl_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  257. {
  258. struct fsl_pwm_chip *fpc = to_fsl_chip(chip);
  259. int ret;
  260. mutex_lock(&fpc->lock);
  261. regmap_update_bits(fpc->regmap, FTM_OUTMASK, BIT(pwm->hwpwm), 0);
  262. ret = fsl_counter_clock_enable(fpc);
  263. mutex_unlock(&fpc->lock);
  264. return ret;
  265. }
  266. static void fsl_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  267. {
  268. struct fsl_pwm_chip *fpc = to_fsl_chip(chip);
  269. u32 val;
  270. mutex_lock(&fpc->lock);
  271. regmap_update_bits(fpc->regmap, FTM_OUTMASK, BIT(pwm->hwpwm),
  272. BIT(pwm->hwpwm));
  273. clk_disable_unprepare(fpc->clk[FSL_PWM_CLK_CNTEN]);
  274. clk_disable_unprepare(fpc->clk[fpc->cnt_select]);
  275. regmap_read(fpc->regmap, FTM_OUTMASK, &val);
  276. if ((val & 0xFF) == 0xFF)
  277. fpc->period_ns = 0;
  278. mutex_unlock(&fpc->lock);
  279. }
  280. static const struct pwm_ops fsl_pwm_ops = {
  281. .request = fsl_pwm_request,
  282. .free = fsl_pwm_free,
  283. .config = fsl_pwm_config,
  284. .set_polarity = fsl_pwm_set_polarity,
  285. .enable = fsl_pwm_enable,
  286. .disable = fsl_pwm_disable,
  287. .owner = THIS_MODULE,
  288. };
  289. static int fsl_pwm_init(struct fsl_pwm_chip *fpc)
  290. {
  291. int ret;
  292. ret = clk_prepare_enable(fpc->clk[FSL_PWM_CLK_SYS]);
  293. if (ret)
  294. return ret;
  295. regmap_write(fpc->regmap, FTM_CNTIN, 0x00);
  296. regmap_write(fpc->regmap, FTM_OUTINIT, 0x00);
  297. regmap_write(fpc->regmap, FTM_OUTMASK, 0xFF);
  298. clk_disable_unprepare(fpc->clk[FSL_PWM_CLK_SYS]);
  299. return 0;
  300. }
  301. static bool fsl_pwm_volatile_reg(struct device *dev, unsigned int reg)
  302. {
  303. switch (reg) {
  304. case FTM_CNT:
  305. return true;
  306. }
  307. return false;
  308. }
  309. static const struct regmap_config fsl_pwm_regmap_config = {
  310. .reg_bits = 32,
  311. .reg_stride = 4,
  312. .val_bits = 32,
  313. .max_register = FTM_PWMLOAD,
  314. .volatile_reg = fsl_pwm_volatile_reg,
  315. .cache_type = REGCACHE_FLAT,
  316. };
  317. static int fsl_pwm_probe(struct platform_device *pdev)
  318. {
  319. struct fsl_pwm_chip *fpc;
  320. struct resource *res;
  321. void __iomem *base;
  322. int ret;
  323. fpc = devm_kzalloc(&pdev->dev, sizeof(*fpc), GFP_KERNEL);
  324. if (!fpc)
  325. return -ENOMEM;
  326. mutex_init(&fpc->lock);
  327. fpc->chip.dev = &pdev->dev;
  328. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  329. base = devm_ioremap_resource(&pdev->dev, res);
  330. if (IS_ERR(base))
  331. return PTR_ERR(base);
  332. fpc->regmap = devm_regmap_init_mmio_clk(&pdev->dev, "ftm_sys", base,
  333. &fsl_pwm_regmap_config);
  334. if (IS_ERR(fpc->regmap)) {
  335. dev_err(&pdev->dev, "regmap init failed\n");
  336. return PTR_ERR(fpc->regmap);
  337. }
  338. fpc->clk[FSL_PWM_CLK_SYS] = devm_clk_get(&pdev->dev, "ftm_sys");
  339. if (IS_ERR(fpc->clk[FSL_PWM_CLK_SYS])) {
  340. dev_err(&pdev->dev, "failed to get \"ftm_sys\" clock\n");
  341. return PTR_ERR(fpc->clk[FSL_PWM_CLK_SYS]);
  342. }
  343. fpc->clk[FSL_PWM_CLK_FIX] = devm_clk_get(fpc->chip.dev, "ftm_fix");
  344. if (IS_ERR(fpc->clk[FSL_PWM_CLK_FIX]))
  345. return PTR_ERR(fpc->clk[FSL_PWM_CLK_FIX]);
  346. fpc->clk[FSL_PWM_CLK_EXT] = devm_clk_get(fpc->chip.dev, "ftm_ext");
  347. if (IS_ERR(fpc->clk[FSL_PWM_CLK_EXT]))
  348. return PTR_ERR(fpc->clk[FSL_PWM_CLK_EXT]);
  349. fpc->clk[FSL_PWM_CLK_CNTEN] =
  350. devm_clk_get(fpc->chip.dev, "ftm_cnt_clk_en");
  351. if (IS_ERR(fpc->clk[FSL_PWM_CLK_CNTEN]))
  352. return PTR_ERR(fpc->clk[FSL_PWM_CLK_CNTEN]);
  353. fpc->chip.ops = &fsl_pwm_ops;
  354. fpc->chip.of_xlate = of_pwm_xlate_with_flags;
  355. fpc->chip.of_pwm_n_cells = 3;
  356. fpc->chip.base = -1;
  357. fpc->chip.npwm = 8;
  358. ret = pwmchip_add(&fpc->chip);
  359. if (ret < 0) {
  360. dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret);
  361. return ret;
  362. }
  363. platform_set_drvdata(pdev, fpc);
  364. return fsl_pwm_init(fpc);
  365. }
  366. static int fsl_pwm_remove(struct platform_device *pdev)
  367. {
  368. struct fsl_pwm_chip *fpc = platform_get_drvdata(pdev);
  369. return pwmchip_remove(&fpc->chip);
  370. }
  371. #ifdef CONFIG_PM_SLEEP
  372. static int fsl_pwm_suspend(struct device *dev)
  373. {
  374. struct fsl_pwm_chip *fpc = dev_get_drvdata(dev);
  375. int i;
  376. regcache_cache_only(fpc->regmap, true);
  377. regcache_mark_dirty(fpc->regmap);
  378. for (i = 0; i < fpc->chip.npwm; i++) {
  379. struct pwm_device *pwm = &fpc->chip.pwms[i];
  380. if (!test_bit(PWMF_REQUESTED, &pwm->flags))
  381. continue;
  382. clk_disable_unprepare(fpc->clk[FSL_PWM_CLK_SYS]);
  383. if (!pwm_is_enabled(pwm))
  384. continue;
  385. clk_disable_unprepare(fpc->clk[FSL_PWM_CLK_CNTEN]);
  386. clk_disable_unprepare(fpc->clk[fpc->cnt_select]);
  387. }
  388. return 0;
  389. }
  390. static int fsl_pwm_resume(struct device *dev)
  391. {
  392. struct fsl_pwm_chip *fpc = dev_get_drvdata(dev);
  393. int i;
  394. for (i = 0; i < fpc->chip.npwm; i++) {
  395. struct pwm_device *pwm = &fpc->chip.pwms[i];
  396. if (!test_bit(PWMF_REQUESTED, &pwm->flags))
  397. continue;
  398. clk_prepare_enable(fpc->clk[FSL_PWM_CLK_SYS]);
  399. if (!pwm_is_enabled(pwm))
  400. continue;
  401. clk_prepare_enable(fpc->clk[fpc->cnt_select]);
  402. clk_prepare_enable(fpc->clk[FSL_PWM_CLK_CNTEN]);
  403. }
  404. /* restore all registers from cache */
  405. regcache_cache_only(fpc->regmap, false);
  406. regcache_sync(fpc->regmap);
  407. return 0;
  408. }
  409. #endif
  410. static const struct dev_pm_ops fsl_pwm_pm_ops = {
  411. SET_SYSTEM_SLEEP_PM_OPS(fsl_pwm_suspend, fsl_pwm_resume)
  412. };
  413. static const struct of_device_id fsl_pwm_dt_ids[] = {
  414. { .compatible = "fsl,vf610-ftm-pwm", },
  415. { /* sentinel */ }
  416. };
  417. MODULE_DEVICE_TABLE(of, fsl_pwm_dt_ids);
  418. static struct platform_driver fsl_pwm_driver = {
  419. .driver = {
  420. .name = "fsl-ftm-pwm",
  421. .of_match_table = fsl_pwm_dt_ids,
  422. .pm = &fsl_pwm_pm_ops,
  423. },
  424. .probe = fsl_pwm_probe,
  425. .remove = fsl_pwm_remove,
  426. };
  427. module_platform_driver(fsl_pwm_driver);
  428. MODULE_DESCRIPTION("Freescale FlexTimer Module PWM Driver");
  429. MODULE_AUTHOR("Xiubo Li <Li.Xiubo@freescale.com>");
  430. MODULE_ALIAS("platform:fsl-ftm-pwm");
  431. MODULE_LICENSE("GPL");