pwm-imx-tpm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. * set period count:
  178. * if the PWM is disabled (CMOD[1:0] = 2b00), then MOD register
  179. * is updated when MOD register is written.
  180. *
  181. * if the PWM is enabled (CMOD[1:0] ≠ 2b00), the period length
  182. * is latched into hardware when the next period starts.
  183. */
  184. writel(p->mod, tpm->base + PWM_IMX_TPM_MOD);
  185. tpm->real_period = state->period;
  186. period_update = true;
  187. }
  188. pwm_imx_tpm_get_state(chip, pwm, &c);
  189. /* polarity is NOT allowed to be changed if PWM is active */
  190. if (c.enabled && c.polarity != state->polarity)
  191. return -EBUSY;
  192. if (state->duty_cycle != c.duty_cycle) {
  193. /*
  194. * set channel value:
  195. * if the PWM is disabled (CMOD[1:0] = 2b00), then CnV register
  196. * is updated when CnV register is written.
  197. *
  198. * if the PWM is enabled (CMOD[1:0] ≠ 2b00), the duty length
  199. * is latched into hardware when the next period starts.
  200. */
  201. writel(p->val, tpm->base + PWM_IMX_TPM_CnV(pwm->hwpwm));
  202. duty_update = true;
  203. }
  204. /* make sure MOD & CnV registers are updated */
  205. if (period_update || duty_update) {
  206. timeout = jiffies + msecs_to_jiffies(tpm->real_period /
  207. NSEC_PER_MSEC + 1);
  208. while (readl(tpm->base + PWM_IMX_TPM_MOD) != p->mod
  209. || readl(tpm->base + PWM_IMX_TPM_CnV(pwm->hwpwm))
  210. != p->val) {
  211. if (time_after(jiffies, timeout))
  212. return -ETIME;
  213. cpu_relax();
  214. }
  215. }
  216. /*
  217. * polarity settings will enabled/disable output status
  218. * immediately, so if the channel is disabled, need to
  219. * make sure MSA/MSB/ELS are set to 0 which means channel
  220. * disabled.
  221. */
  222. val = readl(tpm->base + PWM_IMX_TPM_CnSC(pwm->hwpwm));
  223. val &= ~(PWM_IMX_TPM_CnSC_ELS | PWM_IMX_TPM_CnSC_MSA |
  224. PWM_IMX_TPM_CnSC_MSB);
  225. if (state->enabled) {
  226. /*
  227. * set polarity (for edge-aligned PWM modes)
  228. *
  229. * ELS[1:0] = 2b10 yields normal polarity behaviour,
  230. * ELS[1:0] = 2b01 yields inversed polarity.
  231. * The other values are reserved.
  232. */
  233. val |= PWM_IMX_TPM_CnSC_MSB;
  234. val |= (state->polarity == PWM_POLARITY_NORMAL) ?
  235. PWM_IMX_TPM_CnSC_ELS_NORMAL :
  236. PWM_IMX_TPM_CnSC_ELS_INVERSED;
  237. }
  238. writel(val, tpm->base + PWM_IMX_TPM_CnSC(pwm->hwpwm));
  239. /* control the counter status */
  240. if (state->enabled != c.enabled) {
  241. val = readl(tpm->base + PWM_IMX_TPM_SC);
  242. if (state->enabled) {
  243. if (++tpm->enable_count == 1)
  244. val |= PWM_IMX_TPM_SC_CMOD_INC_EVERY_CLK;
  245. } else {
  246. if (--tpm->enable_count == 0)
  247. val &= ~PWM_IMX_TPM_SC_CMOD;
  248. }
  249. writel(val, tpm->base + PWM_IMX_TPM_SC);
  250. }
  251. return 0;
  252. }
  253. static int pwm_imx_tpm_apply(struct pwm_chip *chip,
  254. struct pwm_device *pwm,
  255. const struct pwm_state *state)
  256. {
  257. struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
  258. struct imx_tpm_pwm_param param;
  259. struct pwm_state real_state;
  260. int ret;
  261. ret = pwm_imx_tpm_round_state(chip, &param, &real_state, state);
  262. if (ret)
  263. return ret;
  264. mutex_lock(&tpm->lock);
  265. ret = pwm_imx_tpm_apply_hw(chip, &param, &real_state, pwm);
  266. mutex_unlock(&tpm->lock);
  267. return ret;
  268. }
  269. static int pwm_imx_tpm_request(struct pwm_chip *chip, struct pwm_device *pwm)
  270. {
  271. struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
  272. mutex_lock(&tpm->lock);
  273. tpm->user_count++;
  274. mutex_unlock(&tpm->lock);
  275. return 0;
  276. }
  277. static void pwm_imx_tpm_free(struct pwm_chip *chip, struct pwm_device *pwm)
  278. {
  279. struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
  280. mutex_lock(&tpm->lock);
  281. tpm->user_count--;
  282. mutex_unlock(&tpm->lock);
  283. }
  284. static const struct pwm_ops imx_tpm_pwm_ops = {
  285. .request = pwm_imx_tpm_request,
  286. .free = pwm_imx_tpm_free,
  287. .get_state = pwm_imx_tpm_get_state,
  288. .apply = pwm_imx_tpm_apply,
  289. };
  290. static int pwm_imx_tpm_probe(struct platform_device *pdev)
  291. {
  292. struct pwm_chip *chip;
  293. struct imx_tpm_pwm_chip *tpm;
  294. struct clk *clk;
  295. void __iomem *base;
  296. int ret;
  297. unsigned int npwm;
  298. u32 val;
  299. base = devm_platform_ioremap_resource(pdev, 0);
  300. if (IS_ERR(base))
  301. return PTR_ERR(base);
  302. clk = devm_clk_get_enabled(&pdev->dev, NULL);
  303. if (IS_ERR(clk))
  304. return dev_err_probe(&pdev->dev, PTR_ERR(clk),
  305. "failed to get PWM clock\n");
  306. /* get number of channels */
  307. val = readl(base + PWM_IMX_TPM_PARAM);
  308. npwm = FIELD_GET(PWM_IMX_TPM_PARAM_CHAN, val);
  309. chip = devm_pwmchip_alloc(&pdev->dev, npwm, sizeof(*tpm));
  310. if (IS_ERR(chip))
  311. return PTR_ERR(chip);
  312. tpm = to_imx_tpm_pwm_chip(chip);
  313. platform_set_drvdata(pdev, tpm);
  314. tpm->base = base;
  315. tpm->clk = clk;
  316. chip->ops = &imx_tpm_pwm_ops;
  317. mutex_init(&tpm->lock);
  318. ret = devm_pwmchip_add(&pdev->dev, chip);
  319. if (ret)
  320. return dev_err_probe(&pdev->dev, ret, "failed to add PWM chip\n");
  321. return 0;
  322. }
  323. static int pwm_imx_tpm_suspend(struct device *dev)
  324. {
  325. struct imx_tpm_pwm_chip *tpm = dev_get_drvdata(dev);
  326. int ret;
  327. if (tpm->enable_count > 0)
  328. return -EBUSY;
  329. /*
  330. * Force 'real_period' to be zero to force period update code
  331. * can be executed after system resume back, since suspend causes
  332. * the period related registers to become their reset values.
  333. */
  334. tpm->real_period = 0;
  335. clk_disable_unprepare(tpm->clk);
  336. ret = pinctrl_pm_select_sleep_state(dev);
  337. if (ret)
  338. clk_prepare_enable(tpm->clk);
  339. return ret;
  340. }
  341. static int pwm_imx_tpm_resume(struct device *dev)
  342. {
  343. struct imx_tpm_pwm_chip *tpm = dev_get_drvdata(dev);
  344. int ret = 0;
  345. ret = pinctrl_pm_select_default_state(dev);
  346. if (ret)
  347. return ret;
  348. ret = clk_prepare_enable(tpm->clk);
  349. if (ret) {
  350. dev_err(dev, "failed to prepare or enable clock: %d\n", ret);
  351. pinctrl_pm_select_sleep_state(dev);
  352. }
  353. return ret;
  354. }
  355. static DEFINE_SIMPLE_DEV_PM_OPS(imx_tpm_pwm_pm,
  356. pwm_imx_tpm_suspend, pwm_imx_tpm_resume);
  357. static const struct of_device_id imx_tpm_pwm_dt_ids[] = {
  358. { .compatible = "fsl,imx7ulp-pwm", },
  359. { /* sentinel */ }
  360. };
  361. MODULE_DEVICE_TABLE(of, imx_tpm_pwm_dt_ids);
  362. static struct platform_driver imx_tpm_pwm_driver = {
  363. .driver = {
  364. .name = "imx7ulp-tpm-pwm",
  365. .of_match_table = imx_tpm_pwm_dt_ids,
  366. .pm = pm_ptr(&imx_tpm_pwm_pm),
  367. },
  368. .probe = pwm_imx_tpm_probe,
  369. };
  370. module_platform_driver(imx_tpm_pwm_driver);
  371. MODULE_AUTHOR("Anson Huang <Anson.Huang@nxp.com>");
  372. MODULE_DESCRIPTION("i.MX TPM PWM Driver");
  373. MODULE_LICENSE("GPL v2");