pwm-imx-tpm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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/platform_device.h>
  23. #include <linux/pwm.h>
  24. #include <linux/slab.h>
  25. #define PWM_IMX_TPM_PARAM 0x4
  26. #define PWM_IMX_TPM_GLOBAL 0x8
  27. #define PWM_IMX_TPM_SC 0x10
  28. #define PWM_IMX_TPM_CNT 0x14
  29. #define PWM_IMX_TPM_MOD 0x18
  30. #define PWM_IMX_TPM_CnSC(n) (0x20 + (n) * 0x8)
  31. #define PWM_IMX_TPM_CnV(n) (0x24 + (n) * 0x8)
  32. #define PWM_IMX_TPM_PARAM_CHAN GENMASK(7, 0)
  33. #define PWM_IMX_TPM_SC_PS GENMASK(2, 0)
  34. #define PWM_IMX_TPM_SC_CMOD GENMASK(4, 3)
  35. #define PWM_IMX_TPM_SC_CMOD_INC_EVERY_CLK FIELD_PREP(PWM_IMX_TPM_SC_CMOD, 1)
  36. #define PWM_IMX_TPM_SC_CPWMS BIT(5)
  37. #define PWM_IMX_TPM_CnSC_CHF BIT(7)
  38. #define PWM_IMX_TPM_CnSC_MSB BIT(5)
  39. #define PWM_IMX_TPM_CnSC_MSA BIT(4)
  40. /*
  41. * The reference manual describes this field as two separate bits. The
  42. * semantic of the two bits isn't orthogonal though, so they are treated
  43. * together as a 2-bit field here.
  44. */
  45. #define PWM_IMX_TPM_CnSC_ELS GENMASK(3, 2)
  46. #define PWM_IMX_TPM_CnSC_ELS_INVERSED FIELD_PREP(PWM_IMX_TPM_CnSC_ELS, 1)
  47. #define PWM_IMX_TPM_CnSC_ELS_NORMAL FIELD_PREP(PWM_IMX_TPM_CnSC_ELS, 2)
  48. #define PWM_IMX_TPM_MOD_WIDTH 16
  49. #define PWM_IMX_TPM_MOD_MOD GENMASK(PWM_IMX_TPM_MOD_WIDTH - 1, 0)
  50. struct imx_tpm_pwm_chip {
  51. struct pwm_chip 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 container_of(chip, struct imx_tpm_pwm_chip, 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. p->mod = period_count;
  95. /* calculate real period HW can support */
  96. tmp = (u64)period_count << prescale;
  97. tmp *= NSEC_PER_SEC;
  98. real_state->period = DIV_ROUND_CLOSEST_ULL(tmp, rate);
  99. /*
  100. * if eventually the PWM output is inactive, either
  101. * duty cycle is 0 or status is disabled, need to
  102. * make sure the output pin is inactive.
  103. */
  104. if (!state->enabled)
  105. real_state->duty_cycle = 0;
  106. else
  107. real_state->duty_cycle = state->duty_cycle;
  108. tmp = (u64)p->mod * real_state->duty_cycle;
  109. p->val = DIV64_U64_ROUND_CLOSEST(tmp, real_state->period);
  110. real_state->polarity = state->polarity;
  111. real_state->enabled = state->enabled;
  112. return 0;
  113. }
  114. static void pwm_imx_tpm_get_state(struct pwm_chip *chip,
  115. struct pwm_device *pwm,
  116. struct pwm_state *state)
  117. {
  118. struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
  119. u32 rate, val, prescale;
  120. u64 tmp;
  121. /* get period */
  122. state->period = tpm->real_period;
  123. /* get duty cycle */
  124. rate = clk_get_rate(tpm->clk);
  125. val = readl(tpm->base + PWM_IMX_TPM_SC);
  126. prescale = FIELD_GET(PWM_IMX_TPM_SC_PS, val);
  127. tmp = readl(tpm->base + PWM_IMX_TPM_CnV(pwm->hwpwm));
  128. tmp = (tmp << prescale) * NSEC_PER_SEC;
  129. state->duty_cycle = DIV_ROUND_CLOSEST_ULL(tmp, rate);
  130. /* get polarity */
  131. val = readl(tpm->base + PWM_IMX_TPM_CnSC(pwm->hwpwm));
  132. if ((val & PWM_IMX_TPM_CnSC_ELS) == PWM_IMX_TPM_CnSC_ELS_INVERSED)
  133. state->polarity = PWM_POLARITY_INVERSED;
  134. else
  135. /*
  136. * Assume reserved values (2b00 and 2b11) to yield
  137. * normal polarity.
  138. */
  139. state->polarity = PWM_POLARITY_NORMAL;
  140. /* get channel status */
  141. state->enabled = FIELD_GET(PWM_IMX_TPM_CnSC_ELS, val) ? true : false;
  142. }
  143. /* this function is supposed to be called with mutex hold */
  144. static int pwm_imx_tpm_apply_hw(struct pwm_chip *chip,
  145. struct imx_tpm_pwm_param *p,
  146. struct pwm_state *state,
  147. struct pwm_device *pwm)
  148. {
  149. struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
  150. bool period_update = false;
  151. bool duty_update = false;
  152. u32 val, cmod, cur_prescale;
  153. unsigned long timeout;
  154. struct pwm_state c;
  155. if (state->period != tpm->real_period) {
  156. /*
  157. * TPM counter is shared by multiple channels, so
  158. * prescale and period can NOT be modified when
  159. * there are multiple channels in use with different
  160. * period settings.
  161. */
  162. if (tpm->user_count > 1)
  163. return -EBUSY;
  164. val = readl(tpm->base + PWM_IMX_TPM_SC);
  165. cmod = FIELD_GET(PWM_IMX_TPM_SC_CMOD, val);
  166. cur_prescale = FIELD_GET(PWM_IMX_TPM_SC_PS, val);
  167. if (cmod && cur_prescale != p->prescale)
  168. return -EBUSY;
  169. /* set TPM counter prescale */
  170. val &= ~PWM_IMX_TPM_SC_PS;
  171. val |= FIELD_PREP(PWM_IMX_TPM_SC_PS, p->prescale);
  172. writel(val, tpm->base + PWM_IMX_TPM_SC);
  173. /*
  174. * set period count:
  175. * if the PWM is disabled (CMOD[1:0] = 2b00), then MOD register
  176. * is updated when MOD register is written.
  177. *
  178. * if the PWM is enabled (CMOD[1:0] ≠ 2b00), the period length
  179. * is latched into hardware when the next period starts.
  180. */
  181. writel(p->mod, tpm->base + PWM_IMX_TPM_MOD);
  182. tpm->real_period = state->period;
  183. period_update = true;
  184. }
  185. pwm_imx_tpm_get_state(chip, pwm, &c);
  186. /* polarity is NOT allowed to be changed if PWM is active */
  187. if (c.enabled && c.polarity != state->polarity)
  188. return -EBUSY;
  189. if (state->duty_cycle != c.duty_cycle) {
  190. /*
  191. * set channel value:
  192. * if the PWM is disabled (CMOD[1:0] = 2b00), then CnV register
  193. * is updated when CnV register is written.
  194. *
  195. * if the PWM is enabled (CMOD[1:0] ≠ 2b00), the duty length
  196. * is latched into hardware when the next period starts.
  197. */
  198. writel(p->val, tpm->base + PWM_IMX_TPM_CnV(pwm->hwpwm));
  199. duty_update = true;
  200. }
  201. /* make sure MOD & CnV registers are updated */
  202. if (period_update || duty_update) {
  203. timeout = jiffies + msecs_to_jiffies(tpm->real_period /
  204. NSEC_PER_MSEC + 1);
  205. while (readl(tpm->base + PWM_IMX_TPM_MOD) != p->mod
  206. || readl(tpm->base + PWM_IMX_TPM_CnV(pwm->hwpwm))
  207. != p->val) {
  208. if (time_after(jiffies, timeout))
  209. return -ETIME;
  210. cpu_relax();
  211. }
  212. }
  213. /*
  214. * polarity settings will enabled/disable output status
  215. * immediately, so if the channel is disabled, need to
  216. * make sure MSA/MSB/ELS are set to 0 which means channel
  217. * disabled.
  218. */
  219. val = readl(tpm->base + PWM_IMX_TPM_CnSC(pwm->hwpwm));
  220. val &= ~(PWM_IMX_TPM_CnSC_ELS | PWM_IMX_TPM_CnSC_MSA |
  221. PWM_IMX_TPM_CnSC_MSB);
  222. if (state->enabled) {
  223. /*
  224. * set polarity (for edge-aligned PWM modes)
  225. *
  226. * ELS[1:0] = 2b10 yields normal polarity behaviour,
  227. * ELS[1:0] = 2b01 yields inversed polarity.
  228. * The other values are reserved.
  229. */
  230. val |= PWM_IMX_TPM_CnSC_MSB;
  231. val |= (state->polarity == PWM_POLARITY_NORMAL) ?
  232. PWM_IMX_TPM_CnSC_ELS_NORMAL :
  233. PWM_IMX_TPM_CnSC_ELS_INVERSED;
  234. }
  235. writel(val, tpm->base + PWM_IMX_TPM_CnSC(pwm->hwpwm));
  236. /* control the counter status */
  237. if (state->enabled != c.enabled) {
  238. val = readl(tpm->base + PWM_IMX_TPM_SC);
  239. if (state->enabled) {
  240. if (++tpm->enable_count == 1)
  241. val |= PWM_IMX_TPM_SC_CMOD_INC_EVERY_CLK;
  242. } else {
  243. if (--tpm->enable_count == 0)
  244. val &= ~PWM_IMX_TPM_SC_CMOD;
  245. }
  246. writel(val, tpm->base + PWM_IMX_TPM_SC);
  247. }
  248. return 0;
  249. }
  250. static int pwm_imx_tpm_apply(struct pwm_chip *chip,
  251. struct pwm_device *pwm,
  252. const struct pwm_state *state)
  253. {
  254. struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
  255. struct imx_tpm_pwm_param param;
  256. struct pwm_state real_state;
  257. int ret;
  258. ret = pwm_imx_tpm_round_state(chip, &param, &real_state, state);
  259. if (ret)
  260. return ret;
  261. mutex_lock(&tpm->lock);
  262. ret = pwm_imx_tpm_apply_hw(chip, &param, &real_state, pwm);
  263. mutex_unlock(&tpm->lock);
  264. return ret;
  265. }
  266. static int pwm_imx_tpm_request(struct pwm_chip *chip, struct pwm_device *pwm)
  267. {
  268. struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
  269. mutex_lock(&tpm->lock);
  270. tpm->user_count++;
  271. mutex_unlock(&tpm->lock);
  272. return 0;
  273. }
  274. static void pwm_imx_tpm_free(struct pwm_chip *chip, struct pwm_device *pwm)
  275. {
  276. struct imx_tpm_pwm_chip *tpm = to_imx_tpm_pwm_chip(chip);
  277. mutex_lock(&tpm->lock);
  278. tpm->user_count--;
  279. mutex_unlock(&tpm->lock);
  280. }
  281. static const struct pwm_ops imx_tpm_pwm_ops = {
  282. .request = pwm_imx_tpm_request,
  283. .free = pwm_imx_tpm_free,
  284. .get_state = pwm_imx_tpm_get_state,
  285. .apply = pwm_imx_tpm_apply,
  286. .owner = THIS_MODULE,
  287. };
  288. static int pwm_imx_tpm_probe(struct platform_device *pdev)
  289. {
  290. struct imx_tpm_pwm_chip *tpm;
  291. int ret;
  292. u32 val;
  293. tpm = devm_kzalloc(&pdev->dev, sizeof(*tpm), GFP_KERNEL);
  294. if (!tpm)
  295. return -ENOMEM;
  296. platform_set_drvdata(pdev, tpm);
  297. tpm->base = devm_platform_ioremap_resource(pdev, 0);
  298. if (IS_ERR(tpm->base))
  299. return PTR_ERR(tpm->base);
  300. tpm->clk = devm_clk_get(&pdev->dev, NULL);
  301. if (IS_ERR(tpm->clk)) {
  302. ret = PTR_ERR(tpm->clk);
  303. if (ret != -EPROBE_DEFER)
  304. dev_err(&pdev->dev,
  305. "failed to get PWM clock: %d\n", ret);
  306. return ret;
  307. }
  308. ret = clk_prepare_enable(tpm->clk);
  309. if (ret) {
  310. dev_err(&pdev->dev,
  311. "failed to prepare or enable clock: %d\n", ret);
  312. return ret;
  313. }
  314. tpm->chip.dev = &pdev->dev;
  315. tpm->chip.ops = &imx_tpm_pwm_ops;
  316. tpm->chip.base = -1;
  317. tpm->chip.of_xlate = of_pwm_xlate_with_flags;
  318. tpm->chip.of_pwm_n_cells = 3;
  319. /* get number of channels */
  320. val = readl(tpm->base + PWM_IMX_TPM_PARAM);
  321. tpm->chip.npwm = FIELD_GET(PWM_IMX_TPM_PARAM_CHAN, val);
  322. mutex_init(&tpm->lock);
  323. ret = pwmchip_add(&tpm->chip);
  324. if (ret) {
  325. dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret);
  326. clk_disable_unprepare(tpm->clk);
  327. }
  328. return ret;
  329. }
  330. static int pwm_imx_tpm_remove(struct platform_device *pdev)
  331. {
  332. struct imx_tpm_pwm_chip *tpm = platform_get_drvdata(pdev);
  333. int ret = pwmchip_remove(&tpm->chip);
  334. clk_disable_unprepare(tpm->clk);
  335. return ret;
  336. }
  337. static int __maybe_unused pwm_imx_tpm_suspend(struct device *dev)
  338. {
  339. struct imx_tpm_pwm_chip *tpm = dev_get_drvdata(dev);
  340. if (tpm->enable_count > 0)
  341. return -EBUSY;
  342. clk_disable_unprepare(tpm->clk);
  343. return 0;
  344. }
  345. static int __maybe_unused pwm_imx_tpm_resume(struct device *dev)
  346. {
  347. struct imx_tpm_pwm_chip *tpm = dev_get_drvdata(dev);
  348. int ret = 0;
  349. ret = clk_prepare_enable(tpm->clk);
  350. if (ret)
  351. dev_err(dev,
  352. "failed to prepare or enable clock: %d\n",
  353. ret);
  354. return ret;
  355. }
  356. static SIMPLE_DEV_PM_OPS(imx_tpm_pwm_pm,
  357. pwm_imx_tpm_suspend, pwm_imx_tpm_resume);
  358. static const struct of_device_id imx_tpm_pwm_dt_ids[] = {
  359. { .compatible = "fsl,imx7ulp-pwm", },
  360. { /* sentinel */ }
  361. };
  362. MODULE_DEVICE_TABLE(of, imx_tpm_pwm_dt_ids);
  363. static struct platform_driver imx_tpm_pwm_driver = {
  364. .driver = {
  365. .name = "imx7ulp-tpm-pwm",
  366. .of_match_table = imx_tpm_pwm_dt_ids,
  367. .pm = &imx_tpm_pwm_pm,
  368. },
  369. .probe = pwm_imx_tpm_probe,
  370. .remove = pwm_imx_tpm_remove,
  371. };
  372. module_platform_driver(imx_tpm_pwm_driver);
  373. MODULE_AUTHOR("Anson Huang <Anson.Huang@nxp.com>");
  374. MODULE_DESCRIPTION("i.MX TPM PWM Driver");
  375. MODULE_LICENSE("GPL v2");