pwm-vt8500.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * drivers/pwm/pwm-vt8500.c
  3. *
  4. * Copyright (C) 2010 Alexey Charkov <alchark@gmail.com>
  5. *
  6. * This software is licensed under the terms of the GNU General Public
  7. * License version 2, as published by the Free Software Foundation, and
  8. * may be copied, distributed, and modified under those terms.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/kernel.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/slab.h>
  19. #include <linux/err.h>
  20. #include <linux/io.h>
  21. #include <linux/pwm.h>
  22. #include <linux/delay.h>
  23. #include <asm/div64.h>
  24. #define VT8500_NR_PWMS 4
  25. struct vt8500_chip {
  26. struct pwm_chip chip;
  27. void __iomem *base;
  28. };
  29. #define to_vt8500_chip(chip) container_of(chip, struct vt8500_chip, chip)
  30. #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
  31. static inline void pwm_busy_wait(void __iomem *reg, u8 bitmask)
  32. {
  33. int loops = msecs_to_loops(10);
  34. while ((readb(reg) & bitmask) && --loops)
  35. cpu_relax();
  36. if (unlikely(!loops))
  37. pr_warn("Waiting for status bits 0x%x to clear timed out\n",
  38. bitmask);
  39. }
  40. static int vt8500_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  41. int duty_ns, int period_ns)
  42. {
  43. struct vt8500_chip *vt8500 = to_vt8500_chip(chip);
  44. unsigned long long c;
  45. unsigned long period_cycles, prescale, pv, dc;
  46. c = 25000000/2; /* wild guess --- need to implement clocks */
  47. c = c * period_ns;
  48. do_div(c, 1000000000);
  49. period_cycles = c;
  50. if (period_cycles < 1)
  51. period_cycles = 1;
  52. prescale = (period_cycles - 1) / 4096;
  53. pv = period_cycles / (prescale + 1) - 1;
  54. if (pv > 4095)
  55. pv = 4095;
  56. if (prescale > 1023)
  57. return -EINVAL;
  58. c = (unsigned long long)pv * duty_ns;
  59. do_div(c, period_ns);
  60. dc = c;
  61. pwm_busy_wait(vt8500->base + 0x40 + pwm->hwpwm, (1 << 1));
  62. writel(prescale, vt8500->base + 0x4 + (pwm->hwpwm << 4));
  63. pwm_busy_wait(vt8500->base + 0x40 + pwm->hwpwm, (1 << 2));
  64. writel(pv, vt8500->base + 0x8 + (pwm->hwpwm << 4));
  65. pwm_busy_wait(vt8500->base + 0x40 + pwm->hwpwm, (1 << 3));
  66. writel(dc, vt8500->base + 0xc + (pwm->hwpwm << 4));
  67. return 0;
  68. }
  69. static int vt8500_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  70. {
  71. struct vt8500_chip *vt8500 = to_vt8500_chip(chip);
  72. pwm_busy_wait(vt8500->base + 0x40 + pwm->hwpwm, (1 << 0));
  73. writel(5, vt8500->base + (pwm->hwpwm << 4));
  74. return 0;
  75. }
  76. static void vt8500_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  77. {
  78. struct vt8500_chip *vt8500 = to_vt8500_chip(chip);
  79. pwm_busy_wait(vt8500->base + 0x40 + pwm->hwpwm, (1 << 0));
  80. writel(0, vt8500->base + (pwm->hwpwm << 4));
  81. }
  82. static struct pwm_ops vt8500_pwm_ops = {
  83. .enable = vt8500_pwm_enable,
  84. .disable = vt8500_pwm_disable,
  85. .config = vt8500_pwm_config,
  86. .owner = THIS_MODULE,
  87. };
  88. static int __devinit pwm_probe(struct platform_device *pdev)
  89. {
  90. struct vt8500_chip *chip;
  91. struct resource *r;
  92. int ret;
  93. chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
  94. if (chip == NULL) {
  95. dev_err(&pdev->dev, "failed to allocate memory\n");
  96. return -ENOMEM;
  97. }
  98. chip->chip.dev = &pdev->dev;
  99. chip->chip.ops = &vt8500_pwm_ops;
  100. chip->chip.base = -1;
  101. chip->chip.npwm = VT8500_NR_PWMS;
  102. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  103. if (r == NULL) {
  104. dev_err(&pdev->dev, "no memory resource defined\n");
  105. return -ENODEV;
  106. }
  107. chip->base = devm_request_and_ioremap(&pdev->dev, r);
  108. if (chip->base == NULL)
  109. return -EADDRNOTAVAIL;
  110. ret = pwmchip_add(&chip->chip);
  111. if (ret < 0)
  112. return ret;
  113. platform_set_drvdata(pdev, chip);
  114. return ret;
  115. }
  116. static int __devexit pwm_remove(struct platform_device *pdev)
  117. {
  118. struct vt8500_chip *chip;
  119. chip = platform_get_drvdata(pdev);
  120. if (chip == NULL)
  121. return -ENODEV;
  122. return pwmchip_remove(&chip->chip);
  123. }
  124. static struct platform_driver pwm_driver = {
  125. .driver = {
  126. .name = "vt8500-pwm",
  127. .owner = THIS_MODULE,
  128. },
  129. .probe = pwm_probe,
  130. .remove = __devexit_p(pwm_remove),
  131. };
  132. static int __init pwm_init(void)
  133. {
  134. return platform_driver_register(&pwm_driver);
  135. }
  136. arch_initcall(pwm_init);
  137. static void __exit pwm_exit(void)
  138. {
  139. platform_driver_unregister(&pwm_driver);
  140. }
  141. module_exit(pwm_exit);
  142. MODULE_LICENSE("GPL");