pwm-imx-tpm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2018-2019 NXP.
  4. *
  5. * Limitations:
  6. * - The TPM counter and period counter are shared between
  7. * multiple channels, so all channels should use same period
  8. * settings.
  9. * - Changes to polarity cannot be latched at the time of the
  10. * next period start.
  11. * - Changing period and duty cycle together isn't atomic,
  12. * with the wrong timing it might happen that a period is
  13. * produced with old duty cycle but new period settings.
  14. */
  15. #include <linux/bitfield.h>
  16. #include <linux/bitops.h>
  17. #include <linux/clk.h>
  18. #include <linux/err.h>
  19. #include <linux/io.h>
  20. #include <linux/module.h>
  21. #include <linux/of.h>
  22. #include <linux/pinctrl/consumer.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/pwm.h>
  25. #include <linux/slab.h>
  26. #define PWM_IMX_TPM_PARAM 0x4
  27. #define PWM_IMX_TPM_GLOBAL 0x8
  28. #define PWM_IMX_TPM_SC 0x10
  29. #define PWM_IMX_TPM_CNT 0x14
  30. #define PWM_IMX_TPM_MOD 0x18
  31. #define PWM_IMX_TPM_CnSC(n) (0x20 + (n) * 0x8)
  32. #define PWM_IMX_TPM_CnV(n) (0x24 + (n) * 0x8)
  33. #define PWM_IMX_TPM_PARAM_CHAN GENMASK(7, 0)
  34. #define PWM_IMX_TPM_SC_PS GENMASK(2, 0)
  35. #define PWM_IMX_TPM_SC_CMOD GENMASK(4, 3)
  36. #define PWM_IMX_TPM_SC_CMOD_INC_EVERY_CLK FIELD_PREP(PWM_IMX_TPM_SC_CMOD, 1)
  37. #define PWM_IMX_TPM_SC_CPWMS BIT(5)
  38. #define PWM_IMX_TPM_CnSC_CHF BIT(7)
  39. #define PWM_IMX_TPM_CnSC_MSB BIT(5)
  40. #define PWM_IMX_TPM_CnSC_MSA BIT(4)
  41. /*
  42. * The reference manual describes this field as two separate bits. The
  43. * semantic of the two bits isn't orthogonal though, so they are treated
  44. * together as a 2-bit field here.
  45. */
  46. #define PWM_IMX_TPM_CnSC_ELS GENMASK(3, 2)
  47. #define PWM_IMX_TPM_CnSC_ELS_INVERSED FIELD_PREP(PWM_IMX_TPM_CnSC_ELS, 1)
  48. #define PWM_IMX_TPM_CnSC_ELS_NORMAL FIELD_PREP(PWM_IMX_TPM_CnSC_ELS, 2)
  49. #define PWM_IMX_TPM_MOD_WIDTH 16
  50. #define PWM_IMX_TPM_MOD_MOD GENMASK(PWM_IMX_TPM_MOD_WIDTH - 1, 0)
  51. struct imx_tpm_pwm_chip {
  52. struct clk *clk;
  53. void __iomem *base;
  54. struct mutex lock;
  55. u32 user_count;
  56. u32 enable_count;
  57. u32 real_period;
  58. };
  59. struct imx_tpm_pwm_param {
  60. u8 prescale;
  61. u32 mod;
  62. u32 val;
  63. };
  64. static inline struct imx_tpm_pwm_chip *
  65. to_imx_tpm_pwm_chip(struct pwm_chip *chip)
  66. {
  67. return pwmchip_get_drvdata(chip);
  68. }
  69. /*
  70. * This function determines for a given pwm_state *state that a consumer
  71. * might request the pwm_state *real_state that eventually is implemented
  72. * by the hardware and the necessary register values (in *p) to achieve
  73. * this.
  74. */
  75. static int pwm_imx_tpm_round_state(struct pwm_chip *chip,
  76. struct imx_tpm_pwm_param *p,
  77. struct pwm_state *real_state,
  78. const struct pwm_state *state)
  79. {
  80. struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
  81. u32 rate, prescale, period_count, clock_unit;
  82. u64 tmp;
  83. rate = clk_get_rate(tpm->clk);
  84. tmp = (u64)state->period * rate;
  85. clock_unit = DIV_ROUND_CLOSEST_ULL(tmp, NSEC_PER_SEC);
  86. if (clock_unit <= PWM_IMX_TPM_MOD_MOD)
  87. prescale = 0;
  88. else
  89. prescale = ilog2(clock_unit) + 1 - PWM_IMX_TPM_MOD_WIDTH;
  90. if ((!FIELD_FIT(PWM_IMX_TPM_SC_PS, prescale)))
  91. return -ERANGE;
  92. p->prescale = prescale;
  93. period_count = (clock_unit + ((1 << prescale) >> 1)) >> prescale;
  94. if (period_count == 0)
  95. return -EINVAL;
  96. p->mod = period_count - 1;
  97. /* calculate real period HW can support */
  98. tmp = (u64)period_count << prescale;
  99. tmp *= NSEC_PER_SEC;
  100. real_state->period = DIV_ROUND_CLOSEST_ULL(tmp, rate);
  101. /*
  102. * if eventually the PWM output is inactive, either
  103. * duty cycle is 0 or status is disabled, need to
  104. * make sure the output pin is inactive.
  105. */
  106. if (!state->enabled)
  107. real_state->duty_cycle = 0;
  108. else
  109. real_state->duty_cycle = state->duty_cycle;
  110. tmp = (u64)p->mod * real_state->duty_cycle;
  111. p->val = DIV64_U64_ROUND_CLOSEST(tmp, real_state->period);
  112. real_state->polarity = state->polarity;
  113. real_state->enabled = state->enabled;
  114. return 0;
  115. }
  116. static int pwm_imx_tpm_get_state(struct pwm_chip *chip,
  117. struct pwm_device *pwm,
  118. struct pwm_state *state)
  119. {
  120. struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
  121. u32 rate, val, prescale;
  122. u64 tmp;
  123. /* get period */
  124. state->period = tpm->real_period;
  125. /* get duty cycle */
  126. rate = clk_get_rate(tpm->clk);
  127. val = readl(tpm->base + PWM_IMX_TPM_SC);
  128. prescale = FIELD_GET(PWM_IMX_TPM_SC_PS, val);
  129. tmp = readl(tpm->base + PWM_IMX_TPM_CnV(pwm->hwpwm));
  130. tmp = (tmp << prescale) * NSEC_PER_SEC;
  131. state->duty_cycle = DIV_ROUND_CLOSEST_ULL(tmp, rate);
  132. /* get polarity */
  133. val = readl(tpm->base + PWM_IMX_TPM_CnSC(pwm->hwpwm));
  134. if ((val & PWM_IMX_TPM_CnSC_ELS) == PWM_IMX_TPM_CnSC_ELS_INVERSED)
  135. state->polarity = PWM_POLARITY_INVERSED;
  136. else
  137. /*
  138. * Assume reserved values (2b00 and 2b11) to yield
  139. * normal polarity.
  140. */
  141. state->polarity = PWM_POLARITY_NORMAL;
  142. /* get channel status */
  143. state->enabled = FIELD_GET(PWM_IMX_TPM_CnSC_ELS, val) ? true : false;
  144. return 0;
  145. }
  146. /* this function is supposed to be called with mutex hold */
  147. static int pwm_imx_tpm_apply_hw(struct pwm_chip *chip,
  148. struct imx_tpm_pwm_param *p,
  149. struct pwm_state *state,
  150. struct pwm_device *pwm)
  151. {
  152. struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
  153. bool period_update = false;
  154. bool duty_update = false;
  155. u32 val, cmod, cur_prescale;
  156. unsigned long timeout;
  157. struct pwm_state c;
  158. if (state->period != tpm->real_period) {
  159. /*
  160. * TPM counter is shared by multiple channels, so
  161. * prescale and period can NOT be modified when
  162. * there are multiple channels in use with different
  163. * period settings.
  164. */
  165. if (tpm->user_count > 1)
  166. return -EBUSY;
  167. val = readl(tpm->base + PWM_IMX_TPM_SC);
  168. cmod = FIELD_GET(PWM_IMX_TPM_SC_CMOD, val);
  169. cur_prescale = FIELD_GET(PWM_IMX_TPM_SC_PS, val);
  170. if (cmod && cur_prescale != p->prescale)
  171. return -EBUSY;
  172. /* set TPM counter prescale */
  173. val &= ~PWM_IMX_TPM_SC_PS;
  174. val |= FIELD_PREP(PWM_IMX_TPM_SC_PS, p->prescale);
  175. writel(val, tpm->base + PWM_IMX_TPM_SC);
  176. /*
  177. * if the counter is disabled (CMOD == 0), programming the new
  178. * period length (MOD) will not reset the counter (CNT). If
  179. * CNT.COUNT happens to be bigger than the new MOD value then
  180. * the counter will end up being reset way too late. Therefore,
  181. * manually reset it to 0.
  182. */
  183. if (!cmod)
  184. writel(0x0, tpm->base + PWM_IMX_TPM_CNT);
  185. /*
  186. * set period count:
  187. * if the PWM is disabled (CMOD[1:0] = 2b00), then MOD register
  188. * is updated when MOD register is written.
  189. *
  190. * if the PWM is enabled (CMOD[1:0] ≠ 2b00), the period length
  191. * is latched into hardware when the next period starts.
  192. */
  193. writel(p->mod, tpm->base + PWM_IMX_TPM_MOD);
  194. tpm->real_period = state->period;
  195. period_update = true;
  196. }
  197. pwm_imx_tpm_get_state(chip, pwm, &c);
  198. /* polarity is NOT allowed to be changed if PWM is active */
  199. if (c.enabled && c.polarity != state->polarity)
  200. return -EBUSY;
  201. if (state->duty_cycle != c.duty_cycle) {
  202. /*
  203. * set channel value:
  204. * if the PWM is disabled (CMOD[1:0] = 2b00), then CnV register
  205. * is updated when CnV register is written.
  206. *
  207. * if the PWM is enabled (CMOD[1:0] ≠ 2b00), the duty length
  208. * is latched into hardware when the next period starts.
  209. */
  210. writel(p->val, tpm->base + PWM_IMX_TPM_CnV(pwm->hwpwm));
  211. duty_update = true;
  212. }
  213. /* make sure MOD & CnV registers are updated */
  214. if (period_update || duty_update) {
  215. timeout = jiffies + msecs_to_jiffies(tpm->real_period /
  216. NSEC_PER_MSEC + 1);
  217. while (readl(tpm->base + PWM_IMX_TPM_MOD) != p->mod
  218. || readl(tpm->base + PWM_IMX_TPM_CnV(pwm->hwpwm))
  219. != p->val) {
  220. if (time_after(jiffies, timeout))
  221. return -ETIME;
  222. cpu_relax();
  223. }
  224. }
  225. /*
  226. * polarity settings will enabled/disable output status
  227. * immediately, so if the channel is disabled, need to
  228. * make sure MSA/MSB/ELS are set to 0 which means channel
  229. * disabled.
  230. */
  231. val = readl(tpm->base + PWM_IMX_TPM_CnSC(pwm->hwpwm));
  232. val &= ~(PWM_IMX_TPM_CnSC_ELS | PWM_IMX_TPM_CnSC_MSA |
  233. PWM_IMX_TPM_CnSC_MSB);
  234. if (state->enabled) {
  235. /*
  236. * set polarity (for edge-aligned PWM modes)
  237. *
  238. * ELS[1:0] = 2b10 yields normal polarity behaviour,
  239. * ELS[1:0] = 2b01 yields inversed polarity.
  240. * The other values are reserved.
  241. */
  242. val |= PWM_IMX_TPM_CnSC_MSB;
  243. val |= (state->polarity == PWM_POLARITY_NORMAL) ?
  244. PWM_IMX_TPM_CnSC_ELS_NORMAL :
  245. PWM_IMX_TPM_CnSC_ELS_INVERSED;
  246. }
  247. writel(val, tpm->base + PWM_IMX_TPM_CnSC(pwm->hwpwm));
  248. /* control the counter status */
  249. if (state->enabled != c.enabled) {
  250. val = readl(tpm->base + PWM_IMX_TPM_SC);
  251. if (state->enabled) {
  252. if (++tpm->enable_count == 1)
  253. val |= PWM_IMX_TPM_SC_CMOD_INC_EVERY_CLK;
  254. } else {
  255. if (--tpm->enable_count == 0)
  256. val &= ~PWM_IMX_TPM_SC_CMOD;
  257. }
  258. writel(val, tpm->base + PWM_IMX_TPM_SC);
  259. }
  260. return 0;
  261. }
  262. static int pwm_imx_tpm_apply(struct pwm_chip *chip,
  263. struct pwm_device *pwm,
  264. const struct pwm_state *state)
  265. {
  266. struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
  267. struct imx_tpm_pwm_param param;
  268. struct pwm_state real_state;
  269. int ret;
  270. ret = pwm_imx_tpm_round_state(chip, &param, &real_state, state);
  271. if (ret)
  272. return ret;
  273. mutex_lock(&tpm->lock);
  274. ret = pwm_imx_tpm_apply_hw(chip, &param, &real_state, pwm);
  275. mutex_unlock(&tpm->lock);
  276. return ret;
  277. }
  278. static int pwm_imx_tpm_request(struct pwm_chip *chip, struct pwm_device *pwm)
  279. {
  280. struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
  281. mutex_lock(&tpm->lock);
  282. tpm->user_count++;
  283. mutex_unlock(&tpm->lock);
  284. return 0;
  285. }
  286. static void pwm_imx_tpm_free(struct pwm_chip *chip, struct pwm_device *pwm)
  287. {
  288. struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
  289. mutex_lock(&tpm->lock);
  290. tpm->user_count--;
  291. mutex_unlock(&tpm->lock);
  292. }
  293. static const struct pwm_ops imx_tpm_pwm_ops = {
  294. .request = pwm_imx_tpm_request,
  295. .free = pwm_imx_tpm_free,
  296. .get_state = pwm_imx_tpm_get_state,
  297. .apply = pwm_imx_tpm_apply,
  298. };
  299. static int pwm_imx_tpm_probe(struct platform_device *pdev)
  300. {
  301. struct pwm_chip *chip;
  302. struct imx_tpm_pwm_chip *tpm;
  303. struct clk *clk;
  304. void __iomem *base;
  305. int ret;
  306. unsigned int npwm;
  307. u32 val;
  308. base = devm_platform_ioremap_resource(pdev, 0);
  309. if (IS_ERR(base))
  310. return PTR_ERR(base);
  311. clk = devm_clk_get_enabled(&pdev->dev, NULL);
  312. if (IS_ERR(clk))
  313. return dev_err_probe(&pdev->dev, PTR_ERR(clk),
  314. "failed to get PWM clock\n");
  315. /* get number of channels */
  316. val = readl(base + PWM_IMX_TPM_PARAM);
  317. npwm = FIELD_GET(PWM_IMX_TPM_PARAM_CHAN, val);
  318. chip = devm_pwmchip_alloc(&pdev->dev, npwm, sizeof(*tpm));
  319. if (IS_ERR(chip))
  320. return PTR_ERR(chip);
  321. tpm = to_imx_tpm_pwm_chip(chip);
  322. platform_set_drvdata(pdev, tpm);
  323. tpm->base = base;
  324. tpm->clk = clk;
  325. chip->ops = &imx_tpm_pwm_ops;
  326. mutex_init(&tpm->lock);
  327. ret = devm_pwmchip_add(&pdev->dev, chip);
  328. if (ret)
  329. return dev_err_probe(&pdev->dev, ret, "failed to add PWM chip\n");
  330. return 0;
  331. }
  332. static int pwm_imx_tpm_suspend(struct device *dev)
  333. {
  334. struct imx_tpm_pwm_chip *tpm = dev_get_drvdata(dev);
  335. int ret;
  336. if (tpm->enable_count > 0)
  337. return -EBUSY;
  338. /*
  339. * Force 'real_period' to be zero to force period update code
  340. * can be executed after system resume back, since suspend causes
  341. * the period related registers to become their reset values.
  342. */
  343. tpm->real_period = 0;
  344. clk_disable_unprepare(tpm->clk);
  345. ret = pinctrl_pm_select_sleep_state(dev);
  346. if (ret)
  347. clk_prepare_enable(tpm->clk);
  348. return ret;
  349. }
  350. static int pwm_imx_tpm_resume(struct device *dev)
  351. {
  352. struct imx_tpm_pwm_chip *tpm = dev_get_drvdata(dev);
  353. int ret = 0;
  354. ret = pinctrl_pm_select_default_state(dev);
  355. if (ret)
  356. return ret;
  357. ret = clk_prepare_enable(tpm->clk);
  358. if (ret) {
  359. dev_err(dev, "failed to prepare or enable clock: %d\n", ret);
  360. pinctrl_pm_select_sleep_state(dev);
  361. }
  362. return ret;
  363. }
  364. static DEFINE_SIMPLE_DEV_PM_OPS(imx_tpm_pwm_pm,
  365. pwm_imx_tpm_suspend, pwm_imx_tpm_resume);
  366. static const struct of_device_id imx_tpm_pwm_dt_ids[] = {
  367. { .compatible = "fsl,imx7ulp-pwm", },
  368. { /* sentinel */ }
  369. };
  370. MODULE_DEVICE_TABLE(of, imx_tpm_pwm_dt_ids);
  371. static struct platform_driver imx_tpm_pwm_driver = {
  372. .driver = {
  373. .name = "imx7ulp-tpm-pwm",
  374. .of_match_table = imx_tpm_pwm_dt_ids,
  375. .pm = pm_ptr(&imx_tpm_pwm_pm),
  376. },
  377. .probe = pwm_imx_tpm_probe,
  378. };
  379. module_platform_driver(imx_tpm_pwm_driver);
  380. MODULE_AUTHOR("Anson Huang <Anson.Huang@nxp.com>");
  381. MODULE_DESCRIPTION("i.MX TPM PWM Driver");
  382. MODULE_LICENSE("GPL v2");