pwm-tegra.c 6.7 KB

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