pwm-vt8500.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * drivers/pwm/pwm-vt8500.c
  3. *
  4. * Copyright (C) 2012 Tony Prisk <linux@prisktech.co.nz>
  5. * Copyright (C) 2010 Alexey Charkov <alchark@gmail.com>
  6. *
  7. * This software is licensed under the terms of the GNU General Public
  8. * License version 2, as published by the Free Software Foundation, and
  9. * may be copied, distributed, and modified under those terms.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/module.h>
  17. #include <linux/kernel.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/slab.h>
  20. #include <linux/err.h>
  21. #include <linux/io.h>
  22. #include <linux/pwm.h>
  23. #include <linux/delay.h>
  24. #include <linux/clk.h>
  25. #include <asm/div64.h>
  26. #include <linux/of.h>
  27. #include <linux/of_device.h>
  28. #include <linux/of_address.h>
  29. /*
  30. * SoC architecture allocates register space for 4 PWMs but only
  31. * 2 are currently implemented.
  32. */
  33. #define VT8500_NR_PWMS 2
  34. struct vt8500_chip {
  35. struct pwm_chip chip;
  36. void __iomem *base;
  37. struct clk *clk;
  38. };
  39. #define to_vt8500_chip(chip) container_of(chip, struct vt8500_chip, chip)
  40. #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
  41. static inline void pwm_busy_wait(void __iomem *reg, u8 bitmask)
  42. {
  43. int loops = msecs_to_loops(10);
  44. while ((readb(reg) & bitmask) && --loops)
  45. cpu_relax();
  46. if (unlikely(!loops))
  47. pr_warn("Waiting for status bits 0x%x to clear timed out\n",
  48. bitmask);
  49. }
  50. static int vt8500_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  51. int duty_ns, int period_ns)
  52. {
  53. struct vt8500_chip *vt8500 = to_vt8500_chip(chip);
  54. unsigned long long c;
  55. unsigned long period_cycles, prescale, pv, dc;
  56. int err;
  57. err = clk_enable(vt8500->clk);
  58. if (err < 0) {
  59. dev_err(chip->dev, "failed to enable clock\n");
  60. return err;
  61. }
  62. c = clk_get_rate(vt8500->clk);
  63. c = c * period_ns;
  64. do_div(c, 1000000000);
  65. period_cycles = c;
  66. if (period_cycles < 1)
  67. period_cycles = 1;
  68. prescale = (period_cycles - 1) / 4096;
  69. pv = period_cycles / (prescale + 1) - 1;
  70. if (pv > 4095)
  71. pv = 4095;
  72. if (prescale > 1023) {
  73. clk_disable(vt8500->clk);
  74. return -EINVAL;
  75. }
  76. c = (unsigned long long)pv * duty_ns;
  77. do_div(c, period_ns);
  78. dc = c;
  79. pwm_busy_wait(vt8500->base + 0x40 + pwm->hwpwm, (1 << 1));
  80. writel(prescale, vt8500->base + 0x4 + (pwm->hwpwm << 4));
  81. pwm_busy_wait(vt8500->base + 0x40 + pwm->hwpwm, (1 << 2));
  82. writel(pv, vt8500->base + 0x8 + (pwm->hwpwm << 4));
  83. pwm_busy_wait(vt8500->base + 0x40 + pwm->hwpwm, (1 << 3));
  84. writel(dc, vt8500->base + 0xc + (pwm->hwpwm << 4));
  85. clk_disable(vt8500->clk);
  86. return 0;
  87. }
  88. static int vt8500_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  89. {
  90. int err;
  91. struct vt8500_chip *vt8500 = to_vt8500_chip(chip);
  92. err = clk_enable(vt8500->clk);
  93. if (err < 0) {
  94. dev_err(chip->dev, "failed to enable clock\n");
  95. return err;
  96. }
  97. pwm_busy_wait(vt8500->base + 0x40 + pwm->hwpwm, (1 << 0));
  98. writel(5, vt8500->base + (pwm->hwpwm << 4));
  99. return 0;
  100. }
  101. static void vt8500_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  102. {
  103. struct vt8500_chip *vt8500 = to_vt8500_chip(chip);
  104. pwm_busy_wait(vt8500->base + 0x40 + pwm->hwpwm, (1 << 0));
  105. writel(0, vt8500->base + (pwm->hwpwm << 4));
  106. clk_disable(vt8500->clk);
  107. }
  108. static struct pwm_ops vt8500_pwm_ops = {
  109. .enable = vt8500_pwm_enable,
  110. .disable = vt8500_pwm_disable,
  111. .config = vt8500_pwm_config,
  112. .owner = THIS_MODULE,
  113. };
  114. static const struct of_device_id vt8500_pwm_dt_ids[] = {
  115. { .compatible = "via,vt8500-pwm", },
  116. { /* Sentinel */ }
  117. };
  118. MODULE_DEVICE_TABLE(of, vt8500_pwm_dt_ids);
  119. static int vt8500_pwm_probe(struct platform_device *pdev)
  120. {
  121. struct vt8500_chip *chip;
  122. struct resource *r;
  123. struct device_node *np = pdev->dev.of_node;
  124. int ret;
  125. if (!np) {
  126. dev_err(&pdev->dev, "invalid devicetree node\n");
  127. return -EINVAL;
  128. }
  129. chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
  130. if (chip == NULL) {
  131. dev_err(&pdev->dev, "failed to allocate memory\n");
  132. return -ENOMEM;
  133. }
  134. chip->chip.dev = &pdev->dev;
  135. chip->chip.ops = &vt8500_pwm_ops;
  136. chip->chip.base = -1;
  137. chip->chip.npwm = VT8500_NR_PWMS;
  138. chip->clk = devm_clk_get(&pdev->dev, NULL);
  139. if (IS_ERR(chip->clk)) {
  140. dev_err(&pdev->dev, "clock source not specified\n");
  141. return PTR_ERR(chip->clk);
  142. }
  143. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  144. if (r == NULL) {
  145. dev_err(&pdev->dev, "no memory resource defined\n");
  146. return -ENODEV;
  147. }
  148. chip->base = devm_request_and_ioremap(&pdev->dev, r);
  149. if (!chip->base)
  150. return -EADDRNOTAVAIL;
  151. ret = clk_prepare(chip->clk);
  152. if (ret < 0) {
  153. dev_err(&pdev->dev, "failed to prepare clock\n");
  154. return ret;
  155. }
  156. ret = pwmchip_add(&chip->chip);
  157. if (ret < 0) {
  158. dev_err(&pdev->dev, "failed to add PWM chip\n");
  159. return ret;
  160. }
  161. platform_set_drvdata(pdev, chip);
  162. return ret;
  163. }
  164. static int vt8500_pwm_remove(struct platform_device *pdev)
  165. {
  166. struct vt8500_chip *chip;
  167. chip = platform_get_drvdata(pdev);
  168. if (chip == NULL)
  169. return -ENODEV;
  170. clk_unprepare(chip->clk);
  171. return pwmchip_remove(&chip->chip);
  172. }
  173. static struct platform_driver vt8500_pwm_driver = {
  174. .probe = vt8500_pwm_probe,
  175. .remove = vt8500_pwm_remove,
  176. .driver = {
  177. .name = "vt8500-pwm",
  178. .owner = THIS_MODULE,
  179. .of_match_table = vt8500_pwm_dt_ids,
  180. },
  181. };
  182. module_platform_driver(vt8500_pwm_driver);
  183. MODULE_DESCRIPTION("VT8500 PWM Driver");
  184. MODULE_AUTHOR("Tony Prisk <linux@prisktech.co.nz>");
  185. MODULE_LICENSE("GPL v2");