pwm-tegra.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * drivers/pwm/pwm-tegra.c
  3. *
  4. * Tegra pulse-width-modulation controller driver
  5. *
  6. * Copyright (c) 2010, NVIDIA Corporation.
  7. * Based on arch/arm/plat-mxc/pwm.c by Sascha Hauer <s.hauer@pengutronix.de>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  17. * more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. */
  23. #include <linux/clk.h>
  24. #include <linux/err.h>
  25. #include <linux/io.h>
  26. #include <linux/module.h>
  27. #include <linux/of.h>
  28. #include <linux/of_device.h>
  29. #include <linux/pwm.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/pinctrl/consumer.h>
  32. #include <linux/slab.h>
  33. #include <linux/reset.h>
  34. #define PWM_ENABLE (1 << 31)
  35. #define PWM_DUTY_WIDTH 8
  36. #define PWM_DUTY_SHIFT 16
  37. #define PWM_SCALE_WIDTH 13
  38. #define PWM_SCALE_SHIFT 0
  39. struct tegra_pwm_soc {
  40. unsigned int num_channels;
  41. /* Maximum IP frequency for given SoCs */
  42. unsigned long max_frequency;
  43. };
  44. struct tegra_pwm_chip {
  45. struct pwm_chip chip;
  46. struct device *dev;
  47. struct clk *clk;
  48. struct reset_control*rst;
  49. unsigned long clk_rate;
  50. void __iomem *regs;
  51. const struct tegra_pwm_soc *soc;
  52. };
  53. static inline struct tegra_pwm_chip *to_tegra_pwm_chip(struct pwm_chip *chip)
  54. {
  55. return container_of(chip, struct tegra_pwm_chip, chip);
  56. }
  57. static inline u32 pwm_readl(struct tegra_pwm_chip *chip, unsigned int num)
  58. {
  59. return readl(chip->regs + (num << 4));
  60. }
  61. static inline void pwm_writel(struct tegra_pwm_chip *chip, unsigned int num,
  62. unsigned long val)
  63. {
  64. writel(val, chip->regs + (num << 4));
  65. }
  66. static int tegra_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  67. int duty_ns, int period_ns)
  68. {
  69. struct tegra_pwm_chip *pc = to_tegra_pwm_chip(chip);
  70. unsigned long long c = duty_ns, hz;
  71. unsigned long rate;
  72. u32 val = 0;
  73. int err;
  74. /*
  75. * Convert from duty_ns / period_ns to a fixed number of duty ticks
  76. * per (1 << PWM_DUTY_WIDTH) cycles and make sure to round to the
  77. * nearest integer during division.
  78. */
  79. c *= (1 << PWM_DUTY_WIDTH);
  80. c = DIV_ROUND_CLOSEST_ULL(c, period_ns);
  81. val = (u32)c << PWM_DUTY_SHIFT;
  82. /*
  83. * Compute the prescaler value for which (1 << PWM_DUTY_WIDTH)
  84. * cycles at the PWM clock rate will take period_ns nanoseconds.
  85. */
  86. rate = pc->clk_rate >> PWM_DUTY_WIDTH;
  87. /* Consider precision in PWM_SCALE_WIDTH rate calculation */
  88. hz = DIV_ROUND_CLOSEST_ULL(100ULL * NSEC_PER_SEC, period_ns);
  89. rate = DIV_ROUND_CLOSEST_ULL(100ULL * rate, hz);
  90. /*
  91. * Since the actual PWM divider is the register's frequency divider
  92. * field minus 1, we need to decrement to get the correct value to
  93. * write to the register.
  94. */
  95. if (rate > 0)
  96. rate--;
  97. /*
  98. * Make sure that the rate will fit in the register's frequency
  99. * divider field.
  100. */
  101. if (rate >> PWM_SCALE_WIDTH)
  102. return -EINVAL;
  103. val |= rate << PWM_SCALE_SHIFT;
  104. /*
  105. * If the PWM channel is disabled, make sure to turn on the clock
  106. * before writing the register. Otherwise, keep it enabled.
  107. */
  108. if (!pwm_is_enabled(pwm)) {
  109. err = clk_prepare_enable(pc->clk);
  110. if (err < 0)
  111. return err;
  112. } else
  113. val |= PWM_ENABLE;
  114. pwm_writel(pc, pwm->hwpwm, val);
  115. /*
  116. * If the PWM is not enabled, turn the clock off again to save power.
  117. */
  118. if (!pwm_is_enabled(pwm))
  119. clk_disable_unprepare(pc->clk);
  120. return 0;
  121. }
  122. static int tegra_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  123. {
  124. struct tegra_pwm_chip *pc = to_tegra_pwm_chip(chip);
  125. int rc = 0;
  126. u32 val;
  127. rc = clk_prepare_enable(pc->clk);
  128. if (rc < 0)
  129. return rc;
  130. val = pwm_readl(pc, pwm->hwpwm);
  131. val |= PWM_ENABLE;
  132. pwm_writel(pc, pwm->hwpwm, val);
  133. return 0;
  134. }
  135. static void tegra_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  136. {
  137. struct tegra_pwm_chip *pc = to_tegra_pwm_chip(chip);
  138. u32 val;
  139. val = pwm_readl(pc, pwm->hwpwm);
  140. val &= ~PWM_ENABLE;
  141. pwm_writel(pc, pwm->hwpwm, val);
  142. clk_disable_unprepare(pc->clk);
  143. }
  144. static const struct pwm_ops tegra_pwm_ops = {
  145. .config = tegra_pwm_config,
  146. .enable = tegra_pwm_enable,
  147. .disable = tegra_pwm_disable,
  148. .owner = THIS_MODULE,
  149. };
  150. static int tegra_pwm_probe(struct platform_device *pdev)
  151. {
  152. struct tegra_pwm_chip *pwm;
  153. struct resource *r;
  154. int ret;
  155. pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL);
  156. if (!pwm)
  157. return -ENOMEM;
  158. pwm->soc = of_device_get_match_data(&pdev->dev);
  159. pwm->dev = &pdev->dev;
  160. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  161. pwm->regs = devm_ioremap_resource(&pdev->dev, r);
  162. if (IS_ERR(pwm->regs))
  163. return PTR_ERR(pwm->regs);
  164. platform_set_drvdata(pdev, pwm);
  165. pwm->clk = devm_clk_get(&pdev->dev, NULL);
  166. if (IS_ERR(pwm->clk))
  167. return PTR_ERR(pwm->clk);
  168. /* Set maximum frequency of the IP */
  169. ret = clk_set_rate(pwm->clk, pwm->soc->max_frequency);
  170. if (ret < 0) {
  171. dev_err(&pdev->dev, "Failed to set max frequency: %d\n", ret);
  172. return ret;
  173. }
  174. /*
  175. * The requested and configured frequency may differ due to
  176. * clock register resolutions. Get the configured frequency
  177. * so that PWM period can be calculated more accurately.
  178. */
  179. pwm->clk_rate = clk_get_rate(pwm->clk);
  180. pwm->rst = devm_reset_control_get_exclusive(&pdev->dev, "pwm");
  181. if (IS_ERR(pwm->rst)) {
  182. ret = PTR_ERR(pwm->rst);
  183. dev_err(&pdev->dev, "Reset control is not found: %d\n", ret);
  184. return ret;
  185. }
  186. reset_control_deassert(pwm->rst);
  187. pwm->chip.dev = &pdev->dev;
  188. pwm->chip.ops = &tegra_pwm_ops;
  189. pwm->chip.base = -1;
  190. pwm->chip.npwm = pwm->soc->num_channels;
  191. ret = pwmchip_add(&pwm->chip);
  192. if (ret < 0) {
  193. dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
  194. reset_control_assert(pwm->rst);
  195. return ret;
  196. }
  197. return 0;
  198. }
  199. static int tegra_pwm_remove(struct platform_device *pdev)
  200. {
  201. struct tegra_pwm_chip *pc = platform_get_drvdata(pdev);
  202. int err;
  203. if (WARN_ON(!pc))
  204. return -ENODEV;
  205. err = clk_prepare_enable(pc->clk);
  206. if (err < 0)
  207. return err;
  208. reset_control_assert(pc->rst);
  209. clk_disable_unprepare(pc->clk);
  210. return pwmchip_remove(&pc->chip);
  211. }
  212. #ifdef CONFIG_PM_SLEEP
  213. static int tegra_pwm_suspend(struct device *dev)
  214. {
  215. return pinctrl_pm_select_sleep_state(dev);
  216. }
  217. static int tegra_pwm_resume(struct device *dev)
  218. {
  219. return pinctrl_pm_select_default_state(dev);
  220. }
  221. #endif
  222. static const struct tegra_pwm_soc tegra20_pwm_soc = {
  223. .num_channels = 4,
  224. .max_frequency = 48000000UL,
  225. };
  226. static const struct tegra_pwm_soc tegra186_pwm_soc = {
  227. .num_channels = 1,
  228. .max_frequency = 102000000UL,
  229. };
  230. static const struct of_device_id tegra_pwm_of_match[] = {
  231. { .compatible = "nvidia,tegra20-pwm", .data = &tegra20_pwm_soc },
  232. { .compatible = "nvidia,tegra186-pwm", .data = &tegra186_pwm_soc },
  233. { }
  234. };
  235. MODULE_DEVICE_TABLE(of, tegra_pwm_of_match);
  236. static const struct dev_pm_ops tegra_pwm_pm_ops = {
  237. SET_SYSTEM_SLEEP_PM_OPS(tegra_pwm_suspend, tegra_pwm_resume)
  238. };
  239. static struct platform_driver tegra_pwm_driver = {
  240. .driver = {
  241. .name = "tegra-pwm",
  242. .of_match_table = tegra_pwm_of_match,
  243. .pm = &tegra_pwm_pm_ops,
  244. },
  245. .probe = tegra_pwm_probe,
  246. .remove = tegra_pwm_remove,
  247. };
  248. module_platform_driver(tegra_pwm_driver);
  249. MODULE_LICENSE("GPL");
  250. MODULE_AUTHOR("NVIDIA Corporation");
  251. MODULE_ALIAS("platform:tegra-pwm");