pwm-pxa.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * drivers/pwm/pwm-pxa.c
  3. *
  4. * simple driver for PWM (Pulse Width Modulator) controller
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * 2008-02-13 initial version
  11. * eric miao <eric.miao@marvell.com>
  12. */
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/slab.h>
  17. #include <linux/err.h>
  18. #include <linux/clk.h>
  19. #include <linux/io.h>
  20. #include <linux/pwm.h>
  21. #include <asm/div64.h>
  22. #define HAS_SECONDARY_PWM 0x10
  23. #define PWM_ID_BASE(d) ((d) & 0xf)
  24. static const struct platform_device_id pwm_id_table[] = {
  25. /* PWM has_secondary_pwm? */
  26. { "pxa25x-pwm", 0 },
  27. { "pxa27x-pwm", 0 | HAS_SECONDARY_PWM },
  28. { "pxa168-pwm", 1 },
  29. { "pxa910-pwm", 1 },
  30. { },
  31. };
  32. MODULE_DEVICE_TABLE(platform, pwm_id_table);
  33. /* PWM registers and bits definitions */
  34. #define PWMCR (0x00)
  35. #define PWMDCR (0x04)
  36. #define PWMPCR (0x08)
  37. #define PWMCR_SD (1 << 6)
  38. #define PWMDCR_FD (1 << 10)
  39. struct pxa_pwm_chip {
  40. struct pwm_chip chip;
  41. struct device *dev;
  42. struct clk *clk;
  43. int clk_enabled;
  44. void __iomem *mmio_base;
  45. };
  46. static inline struct pxa_pwm_chip *to_pxa_pwm_chip(struct pwm_chip *chip)
  47. {
  48. return container_of(chip, struct pxa_pwm_chip, chip);
  49. }
  50. /*
  51. * period_ns = 10^9 * (PRESCALE + 1) * (PV + 1) / PWM_CLK_RATE
  52. * duty_ns = 10^9 * (PRESCALE + 1) * DC / PWM_CLK_RATE
  53. */
  54. static int pxa_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  55. int duty_ns, int period_ns)
  56. {
  57. struct pxa_pwm_chip *pc = to_pxa_pwm_chip(chip);
  58. unsigned long long c;
  59. unsigned long period_cycles, prescale, pv, dc;
  60. unsigned long offset;
  61. int rc;
  62. if (period_ns == 0 || duty_ns > period_ns)
  63. return -EINVAL;
  64. offset = pwm->hwpwm ? 0x10 : 0;
  65. c = clk_get_rate(pc->clk);
  66. c = c * period_ns;
  67. do_div(c, 1000000000);
  68. period_cycles = c;
  69. if (period_cycles < 1)
  70. period_cycles = 1;
  71. prescale = (period_cycles - 1) / 1024;
  72. pv = period_cycles / (prescale + 1) - 1;
  73. if (prescale > 63)
  74. return -EINVAL;
  75. if (duty_ns == period_ns)
  76. dc = PWMDCR_FD;
  77. else
  78. dc = (pv + 1) * duty_ns / period_ns;
  79. /* NOTE: the clock to PWM has to be enabled first
  80. * before writing to the registers
  81. */
  82. rc = clk_prepare_enable(pc->clk);
  83. if (rc < 0)
  84. return rc;
  85. writel(prescale, pc->mmio_base + offset + PWMCR);
  86. writel(dc, pc->mmio_base + offset + PWMDCR);
  87. writel(pv, pc->mmio_base + offset + PWMPCR);
  88. clk_disable_unprepare(pc->clk);
  89. return 0;
  90. }
  91. static int pxa_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  92. {
  93. struct pxa_pwm_chip *pc = to_pxa_pwm_chip(chip);
  94. int rc = 0;
  95. if (!pc->clk_enabled) {
  96. rc = clk_prepare_enable(pc->clk);
  97. if (!rc)
  98. pc->clk_enabled++;
  99. }
  100. return rc;
  101. }
  102. static void pxa_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  103. {
  104. struct pxa_pwm_chip *pc = to_pxa_pwm_chip(chip);
  105. if (pc->clk_enabled) {
  106. clk_disable_unprepare(pc->clk);
  107. pc->clk_enabled--;
  108. }
  109. }
  110. static struct pwm_ops pxa_pwm_ops = {
  111. .config = pxa_pwm_config,
  112. .enable = pxa_pwm_enable,
  113. .disable = pxa_pwm_disable,
  114. .owner = THIS_MODULE,
  115. };
  116. static int __devinit pwm_probe(struct platform_device *pdev)
  117. {
  118. const struct platform_device_id *id = platform_get_device_id(pdev);
  119. struct pxa_pwm_chip *pwm;
  120. struct resource *r;
  121. int ret = 0;
  122. pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL);
  123. if (pwm == NULL) {
  124. dev_err(&pdev->dev, "failed to allocate memory\n");
  125. return -ENOMEM;
  126. }
  127. pwm->clk = devm_clk_get(&pdev->dev, NULL);
  128. if (IS_ERR(pwm->clk))
  129. return PTR_ERR(pwm->clk);
  130. pwm->clk_enabled = 0;
  131. pwm->chip.dev = &pdev->dev;
  132. pwm->chip.ops = &pxa_pwm_ops;
  133. pwm->chip.base = -1;
  134. pwm->chip.npwm = (id->driver_data & HAS_SECONDARY_PWM) ? 2 : 1;
  135. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  136. if (r == NULL) {
  137. dev_err(&pdev->dev, "no memory resource defined\n");
  138. return -ENODEV;
  139. }
  140. pwm->mmio_base = devm_request_and_ioremap(&pdev->dev, r);
  141. if (pwm->mmio_base == NULL)
  142. return -EADDRNOTAVAIL;
  143. ret = pwmchip_add(&pwm->chip);
  144. if (ret < 0) {
  145. dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
  146. return ret;
  147. }
  148. platform_set_drvdata(pdev, pwm);
  149. return 0;
  150. }
  151. static int __devexit pwm_remove(struct platform_device *pdev)
  152. {
  153. struct pxa_pwm_chip *chip;
  154. chip = platform_get_drvdata(pdev);
  155. if (chip == NULL)
  156. return -ENODEV;
  157. return pwmchip_remove(&chip->chip);
  158. }
  159. static struct platform_driver pwm_driver = {
  160. .driver = {
  161. .name = "pxa25x-pwm",
  162. .owner = THIS_MODULE,
  163. },
  164. .probe = pwm_probe,
  165. .remove = __devexit_p(pwm_remove),
  166. .id_table = pwm_id_table,
  167. };
  168. static int __init pwm_init(void)
  169. {
  170. return platform_driver_register(&pwm_driver);
  171. }
  172. arch_initcall(pwm_init);
  173. static void __exit pwm_exit(void)
  174. {
  175. platform_driver_unregister(&pwm_driver);
  176. }
  177. module_exit(pwm_exit);
  178. MODULE_LICENSE("GPL v2");