pwm-tegra.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * drivers/pwm/pwm-tegra.c
  4. *
  5. * Tegra pulse-width-modulation controller driver
  6. *
  7. * Copyright (c) 2010-2020, NVIDIA Corporation.
  8. * Based on arch/arm/plat-mxc/pwm.c by Sascha Hauer <s.hauer@pengutronix.de>
  9. *
  10. * Overview of Tegra Pulse Width Modulator Register:
  11. * 1. 13-bit: Frequency division (SCALE)
  12. * 2. 8-bit : Pulse division (DUTY)
  13. * 3. 1-bit : Enable bit
  14. *
  15. * The PWM clock frequency is divided by 256 before subdividing it based
  16. * on the programmable frequency division value to generate the required
  17. * frequency for PWM output. The maximum output frequency that can be
  18. * achieved is (max rate of source clock) / 256.
  19. * e.g. if source clock rate is 408 MHz, maximum output frequency can be:
  20. * 408 MHz/256 = 1.6 MHz.
  21. * This 1.6 MHz frequency can further be divided using SCALE value in PWM.
  22. *
  23. * PWM pulse width: 8 bits are usable [23:16] for varying pulse width.
  24. * To achieve 100% duty cycle, program Bit [24] of this register to
  25. * 1’b1. In which case the other bits [23:16] are set to don't care.
  26. *
  27. * Limitations:
  28. * - When PWM is disabled, the output is driven to inactive.
  29. * - It does not allow the current PWM period to complete and
  30. * stops abruptly.
  31. *
  32. * - If the register is reconfigured while PWM is running,
  33. * it does not complete the currently running period.
  34. *
  35. * - If the user input duty is beyond acceptible limits,
  36. * -EINVAL is returned.
  37. */
  38. #include <linux/clk.h>
  39. #include <linux/err.h>
  40. #include <linux/io.h>
  41. #include <linux/module.h>
  42. #include <linux/of.h>
  43. #include <linux/pm_opp.h>
  44. #include <linux/pwm.h>
  45. #include <linux/platform_device.h>
  46. #include <linux/pinctrl/consumer.h>
  47. #include <linux/pm_runtime.h>
  48. #include <linux/slab.h>
  49. #include <linux/reset.h>
  50. #include <soc/tegra/common.h>
  51. #define PWM_ENABLE (1 << 31)
  52. #define PWM_DUTY_WIDTH 8
  53. #define PWM_DUTY_SHIFT 16
  54. #define PWM_SCALE_WIDTH 13
  55. #define PWM_SCALE_SHIFT 0
  56. struct tegra_pwm_soc {
  57. unsigned int num_channels;
  58. /* Maximum IP frequency for given SoCs */
  59. unsigned long max_frequency;
  60. };
  61. struct tegra_pwm_chip {
  62. struct clk *clk;
  63. struct reset_control*rst;
  64. unsigned long clk_rate;
  65. unsigned long min_period_ns;
  66. void __iomem *regs;
  67. const struct tegra_pwm_soc *soc;
  68. };
  69. static inline struct tegra_pwm_chip *to_tegra_pwm_chip(struct pwm_chip *chip)
  70. {
  71. return pwmchip_get_drvdata(chip);
  72. }
  73. static inline u32 pwm_readl(struct tegra_pwm_chip *pc, unsigned int offset)
  74. {
  75. return readl(pc->regs + (offset << 4));
  76. }
  77. static inline void pwm_writel(struct tegra_pwm_chip *pc, unsigned int offset, u32 value)
  78. {
  79. writel(value, pc->regs + (offset << 4));
  80. }
  81. static int tegra_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  82. int duty_ns, int period_ns)
  83. {
  84. struct tegra_pwm_chip *pc = to_tegra_pwm_chip(chip);
  85. unsigned long long c = duty_ns;
  86. unsigned long rate, required_clk_rate;
  87. u32 val = 0;
  88. int err;
  89. /*
  90. * Convert from duty_ns / period_ns to a fixed number of duty ticks
  91. * per (1 << PWM_DUTY_WIDTH) cycles and make sure to round to the
  92. * nearest integer during division.
  93. */
  94. c *= (1 << PWM_DUTY_WIDTH);
  95. c = DIV_ROUND_CLOSEST_ULL(c, period_ns);
  96. val = (u32)c << PWM_DUTY_SHIFT;
  97. /*
  98. * min period = max clock limit >> PWM_DUTY_WIDTH
  99. */
  100. if (period_ns < pc->min_period_ns)
  101. return -EINVAL;
  102. /*
  103. * Compute the prescaler value for which (1 << PWM_DUTY_WIDTH)
  104. * cycles at the PWM clock rate will take period_ns nanoseconds.
  105. *
  106. * num_channels: If single instance of PWM controller has multiple
  107. * channels (e.g. Tegra210 or older) then it is not possible to
  108. * configure separate clock rates to each of the channels, in such
  109. * case the value stored during probe will be referred.
  110. *
  111. * If every PWM controller instance has one channel respectively, i.e.
  112. * nums_channels == 1 then only the clock rate can be modified
  113. * dynamically (e.g. Tegra186 or Tegra194).
  114. */
  115. if (pc->soc->num_channels == 1) {
  116. /*
  117. * Rate is multiplied with 2^PWM_DUTY_WIDTH so that it matches
  118. * with the maximum possible rate that the controller can
  119. * provide. Any further lower value can be derived by setting
  120. * PFM bits[0:12].
  121. *
  122. * required_clk_rate is a reference rate for source clock and
  123. * it is derived based on user requested period. By setting the
  124. * source clock rate as required_clk_rate, PWM controller will
  125. * be able to configure the requested period.
  126. */
  127. required_clk_rate = DIV_ROUND_UP_ULL((u64)NSEC_PER_SEC << PWM_DUTY_WIDTH,
  128. period_ns);
  129. if (required_clk_rate > clk_round_rate(pc->clk, required_clk_rate))
  130. /*
  131. * required_clk_rate is a lower bound for the input
  132. * rate; for lower rates there is no value for PWM_SCALE
  133. * that yields a period less than or equal to the
  134. * requested period. Hence, for lower rates, double the
  135. * required_clk_rate to get a clock rate that can meet
  136. * the requested period.
  137. */
  138. required_clk_rate *= 2;
  139. err = dev_pm_opp_set_rate(pwmchip_parent(chip), required_clk_rate);
  140. if (err < 0)
  141. return -EINVAL;
  142. /* Store the new rate for further references */
  143. pc->clk_rate = clk_get_rate(pc->clk);
  144. }
  145. /* Consider precision in PWM_SCALE_WIDTH rate calculation */
  146. rate = mul_u64_u64_div_u64(pc->clk_rate, period_ns,
  147. (u64)NSEC_PER_SEC << PWM_DUTY_WIDTH);
  148. /*
  149. * Since the actual PWM divider is the register's frequency divider
  150. * field plus 1, we need to decrement to get the correct value to
  151. * write to the register.
  152. */
  153. if (rate > 0)
  154. rate--;
  155. else
  156. return -EINVAL;
  157. /*
  158. * Make sure that the rate will fit in the register's frequency
  159. * divider field.
  160. */
  161. if (rate >> PWM_SCALE_WIDTH)
  162. return -EINVAL;
  163. val |= rate << PWM_SCALE_SHIFT;
  164. /*
  165. * If the PWM channel is disabled, make sure to turn on the clock
  166. * before writing the register. Otherwise, keep it enabled.
  167. */
  168. if (!pwm_is_enabled(pwm)) {
  169. err = pm_runtime_resume_and_get(pwmchip_parent(chip));
  170. if (err)
  171. return err;
  172. } else
  173. val |= PWM_ENABLE;
  174. pwm_writel(pc, pwm->hwpwm, val);
  175. /*
  176. * If the PWM is not enabled, turn the clock off again to save power.
  177. */
  178. if (!pwm_is_enabled(pwm))
  179. pm_runtime_put(pwmchip_parent(chip));
  180. return 0;
  181. }
  182. static int tegra_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  183. {
  184. struct tegra_pwm_chip *pc = to_tegra_pwm_chip(chip);
  185. int rc = 0;
  186. u32 val;
  187. rc = pm_runtime_resume_and_get(pwmchip_parent(chip));
  188. if (rc)
  189. return rc;
  190. val = pwm_readl(pc, pwm->hwpwm);
  191. val |= PWM_ENABLE;
  192. pwm_writel(pc, pwm->hwpwm, val);
  193. return 0;
  194. }
  195. static void tegra_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  196. {
  197. struct tegra_pwm_chip *pc = to_tegra_pwm_chip(chip);
  198. u32 val;
  199. val = pwm_readl(pc, pwm->hwpwm);
  200. val &= ~PWM_ENABLE;
  201. pwm_writel(pc, pwm->hwpwm, val);
  202. pm_runtime_put_sync(pwmchip_parent(chip));
  203. }
  204. static int tegra_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  205. const struct pwm_state *state)
  206. {
  207. int err;
  208. bool enabled = pwm->state.enabled;
  209. if (state->polarity != PWM_POLARITY_NORMAL)
  210. return -EINVAL;
  211. if (!state->enabled) {
  212. if (enabled)
  213. tegra_pwm_disable(chip, pwm);
  214. return 0;
  215. }
  216. err = tegra_pwm_config(chip, pwm, state->duty_cycle, state->period);
  217. if (err)
  218. return err;
  219. if (!enabled)
  220. err = tegra_pwm_enable(chip, pwm);
  221. return err;
  222. }
  223. static const struct pwm_ops tegra_pwm_ops = {
  224. .apply = tegra_pwm_apply,
  225. };
  226. static int tegra_pwm_probe(struct platform_device *pdev)
  227. {
  228. struct pwm_chip *chip;
  229. struct tegra_pwm_chip *pc;
  230. const struct tegra_pwm_soc *soc;
  231. int ret;
  232. soc = of_device_get_match_data(&pdev->dev);
  233. chip = devm_pwmchip_alloc(&pdev->dev, soc->num_channels, sizeof(*pc));
  234. if (IS_ERR(chip))
  235. return PTR_ERR(chip);
  236. pc = to_tegra_pwm_chip(chip);
  237. pc->soc = soc;
  238. pc->regs = devm_platform_ioremap_resource(pdev, 0);
  239. if (IS_ERR(pc->regs))
  240. return PTR_ERR(pc->regs);
  241. platform_set_drvdata(pdev, chip);
  242. pc->clk = devm_clk_get(&pdev->dev, NULL);
  243. if (IS_ERR(pc->clk))
  244. return PTR_ERR(pc->clk);
  245. ret = devm_tegra_core_dev_init_opp_table_common(&pdev->dev);
  246. if (ret)
  247. return ret;
  248. pm_runtime_enable(&pdev->dev);
  249. ret = pm_runtime_resume_and_get(&pdev->dev);
  250. if (ret)
  251. return ret;
  252. /* Set maximum frequency of the IP */
  253. ret = dev_pm_opp_set_rate(&pdev->dev, pc->soc->max_frequency);
  254. if (ret < 0) {
  255. dev_err(&pdev->dev, "Failed to set max frequency: %d\n", ret);
  256. goto put_pm;
  257. }
  258. /*
  259. * The requested and configured frequency may differ due to
  260. * clock register resolutions. Get the configured frequency
  261. * so that PWM period can be calculated more accurately.
  262. */
  263. pc->clk_rate = clk_get_rate(pc->clk);
  264. /* Set minimum limit of PWM period for the IP */
  265. pc->min_period_ns =
  266. (NSEC_PER_SEC / (pc->soc->max_frequency >> PWM_DUTY_WIDTH)) + 1;
  267. pc->rst = devm_reset_control_get_exclusive(&pdev->dev, "pwm");
  268. if (IS_ERR(pc->rst)) {
  269. ret = PTR_ERR(pc->rst);
  270. dev_err(&pdev->dev, "Reset control is not found: %d\n", ret);
  271. goto put_pm;
  272. }
  273. reset_control_deassert(pc->rst);
  274. chip->ops = &tegra_pwm_ops;
  275. ret = pwmchip_add(chip);
  276. if (ret < 0) {
  277. dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
  278. reset_control_assert(pc->rst);
  279. goto put_pm;
  280. }
  281. pm_runtime_put(&pdev->dev);
  282. return 0;
  283. put_pm:
  284. pm_runtime_put_sync_suspend(&pdev->dev);
  285. pm_runtime_force_suspend(&pdev->dev);
  286. return ret;
  287. }
  288. static void tegra_pwm_remove(struct platform_device *pdev)
  289. {
  290. struct pwm_chip *chip = platform_get_drvdata(pdev);
  291. struct tegra_pwm_chip *pc = to_tegra_pwm_chip(chip);
  292. pwmchip_remove(chip);
  293. reset_control_assert(pc->rst);
  294. pm_runtime_force_suspend(&pdev->dev);
  295. }
  296. static int __maybe_unused tegra_pwm_runtime_suspend(struct device *dev)
  297. {
  298. struct pwm_chip *chip = dev_get_drvdata(dev);
  299. struct tegra_pwm_chip *pc = to_tegra_pwm_chip(chip);
  300. int err;
  301. clk_disable_unprepare(pc->clk);
  302. err = pinctrl_pm_select_sleep_state(dev);
  303. if (err) {
  304. clk_prepare_enable(pc->clk);
  305. return err;
  306. }
  307. return 0;
  308. }
  309. static int __maybe_unused tegra_pwm_runtime_resume(struct device *dev)
  310. {
  311. struct pwm_chip *chip = dev_get_drvdata(dev);
  312. struct tegra_pwm_chip *pc = to_tegra_pwm_chip(chip);
  313. int err;
  314. err = pinctrl_pm_select_default_state(dev);
  315. if (err)
  316. return err;
  317. err = clk_prepare_enable(pc->clk);
  318. if (err) {
  319. pinctrl_pm_select_sleep_state(dev);
  320. return err;
  321. }
  322. return 0;
  323. }
  324. static const struct tegra_pwm_soc tegra20_pwm_soc = {
  325. .num_channels = 4,
  326. .max_frequency = 48000000UL,
  327. };
  328. static const struct tegra_pwm_soc tegra186_pwm_soc = {
  329. .num_channels = 1,
  330. .max_frequency = 102000000UL,
  331. };
  332. static const struct tegra_pwm_soc tegra194_pwm_soc = {
  333. .num_channels = 1,
  334. .max_frequency = 408000000UL,
  335. };
  336. static const struct of_device_id tegra_pwm_of_match[] = {
  337. { .compatible = "nvidia,tegra20-pwm", .data = &tegra20_pwm_soc },
  338. { .compatible = "nvidia,tegra186-pwm", .data = &tegra186_pwm_soc },
  339. { .compatible = "nvidia,tegra194-pwm", .data = &tegra194_pwm_soc },
  340. { }
  341. };
  342. MODULE_DEVICE_TABLE(of, tegra_pwm_of_match);
  343. static const struct dev_pm_ops tegra_pwm_pm_ops = {
  344. SET_RUNTIME_PM_OPS(tegra_pwm_runtime_suspend, tegra_pwm_runtime_resume,
  345. NULL)
  346. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  347. pm_runtime_force_resume)
  348. };
  349. static struct platform_driver tegra_pwm_driver = {
  350. .driver = {
  351. .name = "tegra-pwm",
  352. .of_match_table = tegra_pwm_of_match,
  353. .pm = &tegra_pwm_pm_ops,
  354. },
  355. .probe = tegra_pwm_probe,
  356. .remove = tegra_pwm_remove,
  357. };
  358. module_platform_driver(tegra_pwm_driver);
  359. MODULE_LICENSE("GPL");
  360. MODULE_AUTHOR("Sandipan Patra <spatra@nvidia.com>");
  361. MODULE_DESCRIPTION("Tegra PWM controller driver");
  362. MODULE_ALIAS("platform:tegra-pwm");