pwm-tegra.c 5.8 KB

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