pwm-pxa.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. static const struct platform_device_id pwm_id_table[] = {
  24. /* PWM has_secondary_pwm? */
  25. { "pxa25x-pwm", 0 },
  26. { "pxa27x-pwm", HAS_SECONDARY_PWM },
  27. { "pxa168-pwm", 0 },
  28. { "pxa910-pwm", 0 },
  29. { },
  30. };
  31. MODULE_DEVICE_TABLE(platform, pwm_id_table);
  32. /* PWM registers and bits definitions */
  33. #define PWMCR (0x00)
  34. #define PWMDCR (0x04)
  35. #define PWMPCR (0x08)
  36. #define PWMCR_SD (1 << 6)
  37. #define PWMDCR_FD (1 << 10)
  38. struct pxa_pwm_chip {
  39. struct pwm_chip chip;
  40. struct device *dev;
  41. struct clk *clk;
  42. void __iomem *mmio_base;
  43. };
  44. static inline struct pxa_pwm_chip *to_pxa_pwm_chip(struct pwm_chip *chip)
  45. {
  46. return container_of(chip, struct pxa_pwm_chip, chip);
  47. }
  48. /*
  49. * period_ns = 10^9 * (PRESCALE + 1) * (PV + 1) / PWM_CLK_RATE
  50. * duty_ns = 10^9 * (PRESCALE + 1) * DC / PWM_CLK_RATE
  51. */
  52. static int pxa_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  53. int duty_ns, int period_ns)
  54. {
  55. struct pxa_pwm_chip *pc = to_pxa_pwm_chip(chip);
  56. unsigned long long c;
  57. unsigned long period_cycles, prescale, pv, dc;
  58. unsigned long offset;
  59. int rc;
  60. offset = pwm->hwpwm ? 0x10 : 0;
  61. c = clk_get_rate(pc->clk);
  62. c = c * period_ns;
  63. do_div(c, 1000000000);
  64. period_cycles = c;
  65. if (period_cycles < 1)
  66. period_cycles = 1;
  67. prescale = (period_cycles - 1) / 1024;
  68. pv = period_cycles / (prescale + 1) - 1;
  69. if (prescale > 63)
  70. return -EINVAL;
  71. if (duty_ns == period_ns)
  72. dc = PWMDCR_FD;
  73. else
  74. dc = (pv + 1) * duty_ns / period_ns;
  75. /* NOTE: the clock to PWM has to be enabled first
  76. * before writing to the registers
  77. */
  78. rc = clk_prepare_enable(pc->clk);
  79. if (rc < 0)
  80. return rc;
  81. writel(prescale, pc->mmio_base + offset + PWMCR);
  82. writel(dc, pc->mmio_base + offset + PWMDCR);
  83. writel(pv, pc->mmio_base + offset + PWMPCR);
  84. clk_disable_unprepare(pc->clk);
  85. return 0;
  86. }
  87. static int pxa_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  88. {
  89. struct pxa_pwm_chip *pc = to_pxa_pwm_chip(chip);
  90. return clk_prepare_enable(pc->clk);
  91. }
  92. static void pxa_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  93. {
  94. struct pxa_pwm_chip *pc = to_pxa_pwm_chip(chip);
  95. clk_disable_unprepare(pc->clk);
  96. }
  97. static struct pwm_ops pxa_pwm_ops = {
  98. .config = pxa_pwm_config,
  99. .enable = pxa_pwm_enable,
  100. .disable = pxa_pwm_disable,
  101. .owner = THIS_MODULE,
  102. };
  103. static int pwm_probe(struct platform_device *pdev)
  104. {
  105. const struct platform_device_id *id = platform_get_device_id(pdev);
  106. struct pxa_pwm_chip *pwm;
  107. struct resource *r;
  108. int ret = 0;
  109. pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL);
  110. if (pwm == NULL) {
  111. dev_err(&pdev->dev, "failed to allocate memory\n");
  112. return -ENOMEM;
  113. }
  114. pwm->clk = devm_clk_get(&pdev->dev, NULL);
  115. if (IS_ERR(pwm->clk))
  116. return PTR_ERR(pwm->clk);
  117. pwm->chip.dev = &pdev->dev;
  118. pwm->chip.ops = &pxa_pwm_ops;
  119. pwm->chip.base = -1;
  120. pwm->chip.npwm = (id->driver_data & HAS_SECONDARY_PWM) ? 2 : 1;
  121. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  122. pwm->mmio_base = devm_ioremap_resource(&pdev->dev, r);
  123. if (IS_ERR(pwm->mmio_base))
  124. return PTR_ERR(pwm->mmio_base);
  125. ret = pwmchip_add(&pwm->chip);
  126. if (ret < 0) {
  127. dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
  128. return ret;
  129. }
  130. platform_set_drvdata(pdev, pwm);
  131. return 0;
  132. }
  133. static int pwm_remove(struct platform_device *pdev)
  134. {
  135. struct pxa_pwm_chip *chip;
  136. chip = platform_get_drvdata(pdev);
  137. if (chip == NULL)
  138. return -ENODEV;
  139. return pwmchip_remove(&chip->chip);
  140. }
  141. static struct platform_driver pwm_driver = {
  142. .driver = {
  143. .name = "pxa25x-pwm",
  144. .owner = THIS_MODULE,
  145. },
  146. .probe = pwm_probe,
  147. .remove = pwm_remove,
  148. .id_table = pwm_id_table,
  149. };
  150. module_platform_driver(pwm_driver);
  151. MODULE_LICENSE("GPL v2");