pwm-tegra.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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/pwm.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/slab.h>
  31. #include <linux/reset.h>
  32. #define PWM_ENABLE (1 << 31)
  33. #define PWM_DUTY_WIDTH 8
  34. #define PWM_DUTY_SHIFT 16
  35. #define PWM_SCALE_WIDTH 13
  36. #define PWM_SCALE_SHIFT 0
  37. struct tegra_pwm_chip {
  38. struct pwm_chip chip;
  39. struct device *dev;
  40. struct clk *clk;
  41. struct reset_control*rst;
  42. void __iomem *regs;
  43. };
  44. static inline struct tegra_pwm_chip *to_tegra_pwm_chip(struct pwm_chip *chip)
  45. {
  46. return container_of(chip, struct tegra_pwm_chip, chip);
  47. }
  48. static inline u32 pwm_readl(struct tegra_pwm_chip *chip, unsigned int num)
  49. {
  50. return readl(chip->regs + (num << 4));
  51. }
  52. static inline void pwm_writel(struct tegra_pwm_chip *chip, unsigned int num,
  53. unsigned long val)
  54. {
  55. writel(val, chip->regs + (num << 4));
  56. }
  57. static int tegra_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  58. int duty_ns, int period_ns)
  59. {
  60. struct tegra_pwm_chip *pc = to_tegra_pwm_chip(chip);
  61. unsigned long long c;
  62. unsigned long rate, hz;
  63. u32 val = 0;
  64. int err;
  65. /*
  66. * Convert from duty_ns / period_ns to a fixed number of duty ticks
  67. * per (1 << PWM_DUTY_WIDTH) cycles and make sure to round to the
  68. * nearest integer during division.
  69. */
  70. c = duty_ns * (1 << PWM_DUTY_WIDTH) + period_ns / 2;
  71. do_div(c, period_ns);
  72. val = (u32)c << PWM_DUTY_SHIFT;
  73. /*
  74. * Compute the prescaler value for which (1 << PWM_DUTY_WIDTH)
  75. * cycles at the PWM clock rate will take period_ns nanoseconds.
  76. */
  77. rate = clk_get_rate(pc->clk) >> PWM_DUTY_WIDTH;
  78. hz = NSEC_PER_SEC / period_ns;
  79. rate = (rate + (hz / 2)) / hz;
  80. /*
  81. * Since the actual PWM divider is the register's frequency divider
  82. * field minus 1, we need to decrement to get the correct value to
  83. * write to the register.
  84. */
  85. if (rate > 0)
  86. rate--;
  87. /*
  88. * Make sure that the rate will fit in the register's frequency
  89. * divider field.
  90. */
  91. if (rate >> PWM_SCALE_WIDTH)
  92. return -EINVAL;
  93. val |= rate << PWM_SCALE_SHIFT;
  94. /*
  95. * If the PWM channel is disabled, make sure to turn on the clock
  96. * before writing the register. Otherwise, keep it enabled.
  97. */
  98. if (!pwm_is_enabled(pwm)) {
  99. err = clk_prepare_enable(pc->clk);
  100. if (err < 0)
  101. return err;
  102. } else
  103. val |= PWM_ENABLE;
  104. pwm_writel(pc, pwm->hwpwm, val);
  105. /*
  106. * If the PWM is not enabled, turn the clock off again to save power.
  107. */
  108. if (!pwm_is_enabled(pwm))
  109. clk_disable_unprepare(pc->clk);
  110. return 0;
  111. }
  112. static int tegra_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  113. {
  114. struct tegra_pwm_chip *pc = to_tegra_pwm_chip(chip);
  115. int rc = 0;
  116. u32 val;
  117. rc = clk_prepare_enable(pc->clk);
  118. if (rc < 0)
  119. return rc;
  120. val = pwm_readl(pc, pwm->hwpwm);
  121. val |= PWM_ENABLE;
  122. pwm_writel(pc, pwm->hwpwm, val);
  123. return 0;
  124. }
  125. static void tegra_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  126. {
  127. struct tegra_pwm_chip *pc = to_tegra_pwm_chip(chip);
  128. u32 val;
  129. val = pwm_readl(pc, pwm->hwpwm);
  130. val &= ~PWM_ENABLE;
  131. pwm_writel(pc, pwm->hwpwm, val);
  132. clk_disable_unprepare(pc->clk);
  133. }
  134. static const struct pwm_ops tegra_pwm_ops = {
  135. .config = tegra_pwm_config,
  136. .enable = tegra_pwm_enable,
  137. .disable = tegra_pwm_disable,
  138. .owner = THIS_MODULE,
  139. };
  140. static int tegra_pwm_probe(struct platform_device *pdev)
  141. {
  142. struct tegra_pwm_chip *pwm;
  143. struct resource *r;
  144. int ret;
  145. pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL);
  146. if (!pwm)
  147. return -ENOMEM;
  148. pwm->dev = &pdev->dev;
  149. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  150. pwm->regs = devm_ioremap_resource(&pdev->dev, r);
  151. if (IS_ERR(pwm->regs))
  152. return PTR_ERR(pwm->regs);
  153. platform_set_drvdata(pdev, pwm);
  154. pwm->clk = devm_clk_get(&pdev->dev, NULL);
  155. if (IS_ERR(pwm->clk))
  156. return PTR_ERR(pwm->clk);
  157. pwm->rst = devm_reset_control_get(&pdev->dev, "pwm");
  158. if (IS_ERR(pwm->rst)) {
  159. ret = PTR_ERR(pwm->rst);
  160. dev_err(&pdev->dev, "Reset control is not found: %d\n", ret);
  161. return ret;
  162. }
  163. reset_control_deassert(pwm->rst);
  164. pwm->chip.dev = &pdev->dev;
  165. pwm->chip.ops = &tegra_pwm_ops;
  166. pwm->chip.base = -1;
  167. pwm->chip.npwm = 4;
  168. ret = pwmchip_add(&pwm->chip);
  169. if (ret < 0) {
  170. dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
  171. reset_control_assert(pwm->rst);
  172. return ret;
  173. }
  174. return 0;
  175. }
  176. static int tegra_pwm_remove(struct platform_device *pdev)
  177. {
  178. struct tegra_pwm_chip *pc = platform_get_drvdata(pdev);
  179. unsigned int i;
  180. int err;
  181. if (WARN_ON(!pc))
  182. return -ENODEV;
  183. err = clk_prepare_enable(pc->clk);
  184. if (err < 0)
  185. return err;
  186. for (i = 0; i < pc->chip.npwm; i++) {
  187. struct pwm_device *pwm = &pc->chip.pwms[i];
  188. if (!pwm_is_enabled(pwm))
  189. if (clk_prepare_enable(pc->clk) < 0)
  190. continue;
  191. pwm_writel(pc, i, 0);
  192. clk_disable_unprepare(pc->clk);
  193. }
  194. reset_control_assert(pc->rst);
  195. clk_disable_unprepare(pc->clk);
  196. return pwmchip_remove(&pc->chip);
  197. }
  198. static const struct of_device_id tegra_pwm_of_match[] = {
  199. { .compatible = "nvidia,tegra20-pwm" },
  200. { .compatible = "nvidia,tegra30-pwm" },
  201. { }
  202. };
  203. MODULE_DEVICE_TABLE(of, tegra_pwm_of_match);
  204. static struct platform_driver tegra_pwm_driver = {
  205. .driver = {
  206. .name = "tegra-pwm",
  207. .of_match_table = tegra_pwm_of_match,
  208. },
  209. .probe = tegra_pwm_probe,
  210. .remove = tegra_pwm_remove,
  211. };
  212. module_platform_driver(tegra_pwm_driver);
  213. MODULE_LICENSE("GPL");
  214. MODULE_AUTHOR("NVIDIA Corporation");
  215. MODULE_ALIAS("platform:tegra-pwm");