pwm-fsl-ftm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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 use_count;
  72. unsigned int cnt_select;
  73. unsigned int clk_ps;
  74. struct regmap *regmap;
  75. int period_ns;
  76. struct clk *clk[FSL_PWM_CLK_MAX];
  77. };
  78. static inline struct fsl_pwm_chip *to_fsl_chip(struct pwm_chip *chip)
  79. {
  80. return container_of(chip, struct fsl_pwm_chip, chip);
  81. }
  82. static int fsl_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
  83. {
  84. struct fsl_pwm_chip *fpc = to_fsl_chip(chip);
  85. return clk_prepare_enable(fpc->clk[FSL_PWM_CLK_SYS]);
  86. }
  87. static void fsl_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
  88. {
  89. struct fsl_pwm_chip *fpc = to_fsl_chip(chip);
  90. clk_disable_unprepare(fpc->clk[FSL_PWM_CLK_SYS]);
  91. }
  92. static int fsl_pwm_calculate_default_ps(struct fsl_pwm_chip *fpc,
  93. enum fsl_pwm_clk index)
  94. {
  95. unsigned long sys_rate, cnt_rate;
  96. unsigned long long ratio;
  97. sys_rate = clk_get_rate(fpc->clk[FSL_PWM_CLK_SYS]);
  98. if (!sys_rate)
  99. return -EINVAL;
  100. cnt_rate = clk_get_rate(fpc->clk[fpc->cnt_select]);
  101. if (!cnt_rate)
  102. return -EINVAL;
  103. switch (index) {
  104. case FSL_PWM_CLK_SYS:
  105. fpc->clk_ps = 1;
  106. break;
  107. case FSL_PWM_CLK_FIX:
  108. ratio = 2 * cnt_rate - 1;
  109. do_div(ratio, sys_rate);
  110. fpc->clk_ps = ratio;
  111. break;
  112. case FSL_PWM_CLK_EXT:
  113. ratio = 4 * cnt_rate - 1;
  114. do_div(ratio, sys_rate);
  115. fpc->clk_ps = ratio;
  116. break;
  117. default:
  118. return -EINVAL;
  119. }
  120. return 0;
  121. }
  122. static unsigned long fsl_pwm_calculate_cycles(struct fsl_pwm_chip *fpc,
  123. unsigned long period_ns)
  124. {
  125. unsigned long long c, c0;
  126. c = clk_get_rate(fpc->clk[fpc->cnt_select]);
  127. c = c * period_ns;
  128. do_div(c, 1000000000UL);
  129. do {
  130. c0 = c;
  131. do_div(c0, (1 << fpc->clk_ps));
  132. if (c0 <= 0xFFFF)
  133. return (unsigned long)c0;
  134. } while (++fpc->clk_ps < 8);
  135. return 0;
  136. }
  137. static unsigned long fsl_pwm_calculate_period_cycles(struct fsl_pwm_chip *fpc,
  138. unsigned long period_ns,
  139. enum fsl_pwm_clk index)
  140. {
  141. int ret;
  142. ret = fsl_pwm_calculate_default_ps(fpc, index);
  143. if (ret) {
  144. dev_err(fpc->chip.dev,
  145. "failed to calculate default prescaler: %d\n",
  146. ret);
  147. return 0;
  148. }
  149. return fsl_pwm_calculate_cycles(fpc, period_ns);
  150. }
  151. static unsigned long fsl_pwm_calculate_period(struct fsl_pwm_chip *fpc,
  152. unsigned long period_ns)
  153. {
  154. enum fsl_pwm_clk m0, m1;
  155. unsigned long fix_rate, ext_rate, cycles;
  156. cycles = fsl_pwm_calculate_period_cycles(fpc, period_ns,
  157. FSL_PWM_CLK_SYS);
  158. if (cycles) {
  159. fpc->cnt_select = FSL_PWM_CLK_SYS;
  160. return cycles;
  161. }
  162. fix_rate = clk_get_rate(fpc->clk[FSL_PWM_CLK_FIX]);
  163. ext_rate = clk_get_rate(fpc->clk[FSL_PWM_CLK_EXT]);
  164. if (fix_rate > ext_rate) {
  165. m0 = FSL_PWM_CLK_FIX;
  166. m1 = FSL_PWM_CLK_EXT;
  167. } else {
  168. m0 = FSL_PWM_CLK_EXT;
  169. m1 = FSL_PWM_CLK_FIX;
  170. }
  171. cycles = fsl_pwm_calculate_period_cycles(fpc, period_ns, m0);
  172. if (cycles) {
  173. fpc->cnt_select = m0;
  174. return cycles;
  175. }
  176. fpc->cnt_select = m1;
  177. return fsl_pwm_calculate_period_cycles(fpc, period_ns, m1);
  178. }
  179. static unsigned long fsl_pwm_calculate_duty(struct fsl_pwm_chip *fpc,
  180. unsigned long period_ns,
  181. unsigned long duty_ns)
  182. {
  183. unsigned long long duty;
  184. u32 val;
  185. regmap_read(fpc->regmap, FTM_MOD, &val);
  186. duty = (unsigned long long)duty_ns * (val + 1);
  187. do_div(duty, period_ns);
  188. return (unsigned long)duty;
  189. }
  190. static int fsl_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  191. int duty_ns, int period_ns)
  192. {
  193. struct fsl_pwm_chip *fpc = to_fsl_chip(chip);
  194. u32 period, duty;
  195. mutex_lock(&fpc->lock);
  196. /*
  197. * The Freescale FTM controller supports only a single period for
  198. * all PWM channels, therefore incompatible changes need to be
  199. * refused.
  200. */
  201. if (fpc->period_ns && fpc->period_ns != period_ns) {
  202. dev_err(fpc->chip.dev,
  203. "conflicting period requested for PWM %u\n",
  204. pwm->hwpwm);
  205. mutex_unlock(&fpc->lock);
  206. return -EBUSY;
  207. }
  208. if (!fpc->period_ns && duty_ns) {
  209. period = fsl_pwm_calculate_period(fpc, period_ns);
  210. if (!period) {
  211. dev_err(fpc->chip.dev, "failed to calculate period\n");
  212. mutex_unlock(&fpc->lock);
  213. return -EINVAL;
  214. }
  215. regmap_update_bits(fpc->regmap, FTM_SC, FTM_SC_PS_MASK,
  216. fpc->clk_ps);
  217. regmap_write(fpc->regmap, FTM_MOD, period - 1);
  218. fpc->period_ns = period_ns;
  219. }
  220. mutex_unlock(&fpc->lock);
  221. duty = fsl_pwm_calculate_duty(fpc, period_ns, duty_ns);
  222. regmap_write(fpc->regmap, FTM_CSC(pwm->hwpwm),
  223. FTM_CSC_MSB | FTM_CSC_ELSB);
  224. regmap_write(fpc->regmap, FTM_CV(pwm->hwpwm), duty);
  225. return 0;
  226. }
  227. static int fsl_pwm_set_polarity(struct pwm_chip *chip,
  228. struct pwm_device *pwm,
  229. enum pwm_polarity polarity)
  230. {
  231. struct fsl_pwm_chip *fpc = to_fsl_chip(chip);
  232. u32 val;
  233. regmap_read(fpc->regmap, FTM_POL, &val);
  234. if (polarity == PWM_POLARITY_INVERSED)
  235. val |= BIT(pwm->hwpwm);
  236. else
  237. val &= ~BIT(pwm->hwpwm);
  238. regmap_write(fpc->regmap, FTM_POL, val);
  239. return 0;
  240. }
  241. static int fsl_counter_clock_enable(struct fsl_pwm_chip *fpc)
  242. {
  243. int ret;
  244. if (fpc->use_count++ != 0)
  245. return 0;
  246. /* select counter clock source */
  247. regmap_update_bits(fpc->regmap, FTM_SC, FTM_SC_CLK_MASK,
  248. FTM_SC_CLK(fpc->cnt_select));
  249. ret = clk_prepare_enable(fpc->clk[fpc->cnt_select]);
  250. if (ret)
  251. return ret;
  252. ret = clk_prepare_enable(fpc->clk[FSL_PWM_CLK_CNTEN]);
  253. if (ret) {
  254. clk_disable_unprepare(fpc->clk[fpc->cnt_select]);
  255. return ret;
  256. }
  257. return 0;
  258. }
  259. static int fsl_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  260. {
  261. struct fsl_pwm_chip *fpc = to_fsl_chip(chip);
  262. int ret;
  263. mutex_lock(&fpc->lock);
  264. regmap_update_bits(fpc->regmap, FTM_OUTMASK, BIT(pwm->hwpwm), 0);
  265. ret = fsl_counter_clock_enable(fpc);
  266. mutex_unlock(&fpc->lock);
  267. return ret;
  268. }
  269. static void fsl_counter_clock_disable(struct fsl_pwm_chip *fpc)
  270. {
  271. /*
  272. * already disabled, do nothing
  273. */
  274. if (fpc->use_count == 0)
  275. return;
  276. /* there are still users, so can't disable yet */
  277. if (--fpc->use_count > 0)
  278. return;
  279. /* no users left, disable PWM counter clock */
  280. regmap_update_bits(fpc->regmap, FTM_SC, FTM_SC_CLK_MASK, 0);
  281. clk_disable_unprepare(fpc->clk[FSL_PWM_CLK_CNTEN]);
  282. clk_disable_unprepare(fpc->clk[fpc->cnt_select]);
  283. }
  284. static void fsl_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  285. {
  286. struct fsl_pwm_chip *fpc = to_fsl_chip(chip);
  287. u32 val;
  288. mutex_lock(&fpc->lock);
  289. regmap_update_bits(fpc->regmap, FTM_OUTMASK, BIT(pwm->hwpwm),
  290. BIT(pwm->hwpwm));
  291. fsl_counter_clock_disable(fpc);
  292. regmap_read(fpc->regmap, FTM_OUTMASK, &val);
  293. if ((val & 0xFF) == 0xFF)
  294. fpc->period_ns = 0;
  295. mutex_unlock(&fpc->lock);
  296. }
  297. static const struct pwm_ops fsl_pwm_ops = {
  298. .request = fsl_pwm_request,
  299. .free = fsl_pwm_free,
  300. .config = fsl_pwm_config,
  301. .set_polarity = fsl_pwm_set_polarity,
  302. .enable = fsl_pwm_enable,
  303. .disable = fsl_pwm_disable,
  304. .owner = THIS_MODULE,
  305. };
  306. static int fsl_pwm_init(struct fsl_pwm_chip *fpc)
  307. {
  308. int ret;
  309. ret = clk_prepare_enable(fpc->clk[FSL_PWM_CLK_SYS]);
  310. if (ret)
  311. return ret;
  312. regmap_write(fpc->regmap, FTM_CNTIN, 0x00);
  313. regmap_write(fpc->regmap, FTM_OUTINIT, 0x00);
  314. regmap_write(fpc->regmap, FTM_OUTMASK, 0xFF);
  315. clk_disable_unprepare(fpc->clk[FSL_PWM_CLK_SYS]);
  316. return 0;
  317. }
  318. static bool fsl_pwm_volatile_reg(struct device *dev, unsigned int reg)
  319. {
  320. switch (reg) {
  321. case FTM_CNT:
  322. return true;
  323. }
  324. return false;
  325. }
  326. static const struct regmap_config fsl_pwm_regmap_config = {
  327. .reg_bits = 32,
  328. .reg_stride = 4,
  329. .val_bits = 32,
  330. .max_register = FTM_PWMLOAD,
  331. .volatile_reg = fsl_pwm_volatile_reg,
  332. .cache_type = REGCACHE_RBTREE,
  333. };
  334. static int fsl_pwm_probe(struct platform_device *pdev)
  335. {
  336. struct fsl_pwm_chip *fpc;
  337. struct resource *res;
  338. void __iomem *base;
  339. int ret;
  340. fpc = devm_kzalloc(&pdev->dev, sizeof(*fpc), GFP_KERNEL);
  341. if (!fpc)
  342. return -ENOMEM;
  343. mutex_init(&fpc->lock);
  344. fpc->chip.dev = &pdev->dev;
  345. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  346. base = devm_ioremap_resource(&pdev->dev, res);
  347. if (IS_ERR(base))
  348. return PTR_ERR(base);
  349. fpc->regmap = devm_regmap_init_mmio_clk(&pdev->dev, "ftm_sys", base,
  350. &fsl_pwm_regmap_config);
  351. if (IS_ERR(fpc->regmap)) {
  352. dev_err(&pdev->dev, "regmap init failed\n");
  353. return PTR_ERR(fpc->regmap);
  354. }
  355. fpc->clk[FSL_PWM_CLK_SYS] = devm_clk_get(&pdev->dev, "ftm_sys");
  356. if (IS_ERR(fpc->clk[FSL_PWM_CLK_SYS])) {
  357. dev_err(&pdev->dev, "failed to get \"ftm_sys\" clock\n");
  358. return PTR_ERR(fpc->clk[FSL_PWM_CLK_SYS]);
  359. }
  360. fpc->clk[FSL_PWM_CLK_FIX] = devm_clk_get(fpc->chip.dev, "ftm_fix");
  361. if (IS_ERR(fpc->clk[FSL_PWM_CLK_FIX]))
  362. return PTR_ERR(fpc->clk[FSL_PWM_CLK_FIX]);
  363. fpc->clk[FSL_PWM_CLK_EXT] = devm_clk_get(fpc->chip.dev, "ftm_ext");
  364. if (IS_ERR(fpc->clk[FSL_PWM_CLK_EXT]))
  365. return PTR_ERR(fpc->clk[FSL_PWM_CLK_EXT]);
  366. fpc->clk[FSL_PWM_CLK_CNTEN] =
  367. devm_clk_get(fpc->chip.dev, "ftm_cnt_clk_en");
  368. if (IS_ERR(fpc->clk[FSL_PWM_CLK_CNTEN]))
  369. return PTR_ERR(fpc->clk[FSL_PWM_CLK_CNTEN]);
  370. fpc->chip.ops = &fsl_pwm_ops;
  371. fpc->chip.of_xlate = of_pwm_xlate_with_flags;
  372. fpc->chip.of_pwm_n_cells = 3;
  373. fpc->chip.base = -1;
  374. fpc->chip.npwm = 8;
  375. fpc->chip.can_sleep = true;
  376. ret = pwmchip_add(&fpc->chip);
  377. if (ret < 0) {
  378. dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret);
  379. return ret;
  380. }
  381. platform_set_drvdata(pdev, fpc);
  382. return fsl_pwm_init(fpc);
  383. }
  384. static int fsl_pwm_remove(struct platform_device *pdev)
  385. {
  386. struct fsl_pwm_chip *fpc = platform_get_drvdata(pdev);
  387. return pwmchip_remove(&fpc->chip);
  388. }
  389. #ifdef CONFIG_PM_SLEEP
  390. static int fsl_pwm_suspend(struct device *dev)
  391. {
  392. struct fsl_pwm_chip *fpc = dev_get_drvdata(dev);
  393. u32 val;
  394. regcache_cache_only(fpc->regmap, true);
  395. regcache_mark_dirty(fpc->regmap);
  396. /* read from cache */
  397. regmap_read(fpc->regmap, FTM_OUTMASK, &val);
  398. if ((val & 0xFF) != 0xFF) {
  399. clk_disable_unprepare(fpc->clk[FSL_PWM_CLK_CNTEN]);
  400. clk_disable_unprepare(fpc->clk[fpc->cnt_select]);
  401. clk_disable_unprepare(fpc->clk[FSL_PWM_CLK_SYS]);
  402. }
  403. return 0;
  404. }
  405. static int fsl_pwm_resume(struct device *dev)
  406. {
  407. struct fsl_pwm_chip *fpc = dev_get_drvdata(dev);
  408. u32 val;
  409. /* read from cache */
  410. regmap_read(fpc->regmap, FTM_OUTMASK, &val);
  411. if ((val & 0xFF) != 0xFF) {
  412. clk_prepare_enable(fpc->clk[FSL_PWM_CLK_SYS]);
  413. clk_prepare_enable(fpc->clk[fpc->cnt_select]);
  414. clk_prepare_enable(fpc->clk[FSL_PWM_CLK_CNTEN]);
  415. }
  416. /* restore all registers from cache */
  417. regcache_cache_only(fpc->regmap, false);
  418. regcache_sync(fpc->regmap);
  419. return 0;
  420. }
  421. #endif
  422. static const struct dev_pm_ops fsl_pwm_pm_ops = {
  423. SET_SYSTEM_SLEEP_PM_OPS(fsl_pwm_suspend, fsl_pwm_resume)
  424. };
  425. static const struct of_device_id fsl_pwm_dt_ids[] = {
  426. { .compatible = "fsl,vf610-ftm-pwm", },
  427. { /* sentinel */ }
  428. };
  429. MODULE_DEVICE_TABLE(of, fsl_pwm_dt_ids);
  430. static struct platform_driver fsl_pwm_driver = {
  431. .driver = {
  432. .name = "fsl-ftm-pwm",
  433. .of_match_table = fsl_pwm_dt_ids,
  434. .pm = &fsl_pwm_pm_ops,
  435. },
  436. .probe = fsl_pwm_probe,
  437. .remove = fsl_pwm_remove,
  438. };
  439. module_platform_driver(fsl_pwm_driver);
  440. MODULE_DESCRIPTION("Freescale FlexTimer Module PWM Driver");
  441. MODULE_AUTHOR("Xiubo Li <Li.Xiubo@freescale.com>");
  442. MODULE_ALIAS("platform:fsl-ftm-pwm");
  443. MODULE_LICENSE("GPL");