pwm-tegra.c 11 KB

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