pwm-vt8500.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. c = clk_get_rate(vt8500->clk);
  57. c = c * period_ns;
  58. do_div(c, 1000000000);
  59. period_cycles = c;
  60. if (period_cycles < 1)
  61. period_cycles = 1;
  62. prescale = (period_cycles - 1) / 4096;
  63. pv = period_cycles / (prescale + 1) - 1;
  64. if (pv > 4095)
  65. pv = 4095;
  66. if (prescale > 1023)
  67. return -EINVAL;
  68. c = (unsigned long long)pv * duty_ns;
  69. do_div(c, period_ns);
  70. dc = c;
  71. pwm_busy_wait(vt8500->base + 0x40 + pwm->hwpwm, (1 << 1));
  72. writel(prescale, vt8500->base + 0x4 + (pwm->hwpwm << 4));
  73. pwm_busy_wait(vt8500->base + 0x40 + pwm->hwpwm, (1 << 2));
  74. writel(pv, vt8500->base + 0x8 + (pwm->hwpwm << 4));
  75. pwm_busy_wait(vt8500->base + 0x40 + pwm->hwpwm, (1 << 3));
  76. writel(dc, vt8500->base + 0xc + (pwm->hwpwm << 4));
  77. return 0;
  78. }
  79. static int vt8500_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  80. {
  81. int err;
  82. struct vt8500_chip *vt8500 = to_vt8500_chip(chip);
  83. err = clk_enable(vt8500->clk);
  84. if (err < 0)
  85. dev_err(chip->dev, "failed to enable clock\n");
  86. return err;
  87. };
  88. pwm_busy_wait(vt8500->base + 0x40 + pwm->hwpwm, (1 << 0));
  89. writel(5, vt8500->base + (pwm->hwpwm << 4));
  90. return 0;
  91. }
  92. static void vt8500_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  93. {
  94. struct vt8500_chip *vt8500 = to_vt8500_chip(chip);
  95. pwm_busy_wait(vt8500->base + 0x40 + pwm->hwpwm, (1 << 0));
  96. writel(0, vt8500->base + (pwm->hwpwm << 4));
  97. clk_disable(vt8500->clk);
  98. }
  99. static struct pwm_ops vt8500_pwm_ops = {
  100. .enable = vt8500_pwm_enable,
  101. .disable = vt8500_pwm_disable,
  102. .config = vt8500_pwm_config,
  103. .owner = THIS_MODULE,
  104. };
  105. static const struct of_device_id vt8500_pwm_dt_ids[] = {
  106. { .compatible = "via,vt8500-pwm", },
  107. { /* Sentinel */ }
  108. };
  109. MODULE_DEVICE_TABLE(of, vt8500_pwm_dt_ids);
  110. static int vt8500_pwm_probe(struct platform_device *pdev)
  111. {
  112. struct vt8500_chip *chip;
  113. struct resource *r;
  114. struct device_node *np = pdev->dev.of_node;
  115. int ret;
  116. if (!np) {
  117. dev_err(&pdev->dev, "invalid devicetree node\n");
  118. return -EINVAL;
  119. }
  120. chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
  121. if (chip == NULL) {
  122. dev_err(&pdev->dev, "failed to allocate memory\n");
  123. return -ENOMEM;
  124. }
  125. chip->chip.dev = &pdev->dev;
  126. chip->chip.ops = &vt8500_pwm_ops;
  127. chip->chip.base = -1;
  128. chip->chip.npwm = VT8500_NR_PWMS;
  129. chip->clk = devm_clk_get(&pdev->dev, NULL);
  130. if (IS_ERR(chip->clk)) {
  131. dev_err(&pdev->dev, "clock source not specified\n");
  132. return PTR_ERR(chip->clk);
  133. }
  134. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  135. if (r == NULL) {
  136. dev_err(&pdev->dev, "no memory resource defined\n");
  137. return -ENODEV;
  138. }
  139. chip->base = devm_request_and_ioremap(&pdev->dev, r);
  140. if (!chip->base)
  141. return -EADDRNOTAVAIL;
  142. ret = clk_prepare(chip->clk);
  143. if (ret < 0) {
  144. dev_err(&pdev->dev, "failed to prepare clock\n");
  145. return ret;
  146. }
  147. ret = pwmchip_add(&chip->chip);
  148. if (ret < 0) {
  149. dev_err(&pdev->dev, "failed to add PWM chip\n");
  150. return ret;
  151. }
  152. platform_set_drvdata(pdev, chip);
  153. return ret;
  154. }
  155. static int vt8500_pwm_remove(struct platform_device *pdev)
  156. {
  157. struct vt8500_chip *chip;
  158. chip = platform_get_drvdata(pdev);
  159. if (chip == NULL)
  160. return -ENODEV;
  161. clk_unprepare(chip->clk);
  162. return pwmchip_remove(&chip->chip);
  163. }
  164. static struct platform_driver vt8500_pwm_driver = {
  165. .probe = vt8500_pwm_probe,
  166. .remove = vt8500_pwm_remove,
  167. .driver = {
  168. .name = "vt8500-pwm",
  169. .owner = THIS_MODULE,
  170. .of_match_table = vt8500_pwm_dt_ids,
  171. },
  172. };
  173. module_platform_driver(vt8500_pwm_driver);
  174. MODULE_DESCRIPTION("VT8500 PWM Driver");
  175. MODULE_AUTHOR("Tony Prisk <linux@prisktech.co.nz>");
  176. MODULE_LICENSE("GPL v2");