pwm-puv3.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * linux/arch/unicore32/kernel/pwm.c
  3. *
  4. * Code specific to PKUnity SoC and UniCore ISA
  5. *
  6. * Maintained by GUAN Xue-tao <gxt@mprc.pku.edu.cn>
  7. * Copyright (C) 2001-2010 Guan Xuetao
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  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. #include <mach/hardware.h>
  23. struct puv3_pwm_chip {
  24. struct pwm_chip chip;
  25. void __iomem *base;
  26. struct clk *clk;
  27. bool enabled;
  28. };
  29. static inline struct puv3_pwm_chip *to_puv3(struct pwm_chip *chip)
  30. {
  31. return container_of(chip, struct puv3_pwm_chip, chip);
  32. }
  33. /*
  34. * period_ns = 10^9 * (PRESCALE + 1) * (PV + 1) / PWM_CLK_RATE
  35. * duty_ns = 10^9 * (PRESCALE + 1) * DC / PWM_CLK_RATE
  36. */
  37. static int puv3_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  38. int duty_ns, int period_ns)
  39. {
  40. unsigned long period_cycles, prescale, pv, dc;
  41. struct puv3_pwm_chip *puv3 = to_puv3(chip);
  42. unsigned long long c;
  43. c = clk_get_rate(puv3->clk);
  44. c = c * period_ns;
  45. do_div(c, 1000000000);
  46. period_cycles = c;
  47. if (period_cycles < 1)
  48. period_cycles = 1;
  49. prescale = (period_cycles - 1) / 1024;
  50. pv = period_cycles / (prescale + 1) - 1;
  51. if (prescale > 63)
  52. return -EINVAL;
  53. if (duty_ns == period_ns)
  54. dc = OST_PWMDCCR_FDCYCLE;
  55. else
  56. dc = (pv + 1) * duty_ns / period_ns;
  57. /*
  58. * NOTE: the clock to PWM has to be enabled first
  59. * before writing to the registers
  60. */
  61. clk_prepare_enable(puv3->clk);
  62. writel(prescale, puv3->base + OST_PWM_PWCR);
  63. writel(pv - dc, puv3->base + OST_PWM_DCCR);
  64. writel(pv, puv3->base + OST_PWM_PCR);
  65. clk_disable_unprepare(puv3->clk);
  66. return 0;
  67. }
  68. static int puv3_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  69. {
  70. struct puv3_pwm_chip *puv3 = to_puv3(chip);
  71. return clk_prepare_enable(puv3->clk);
  72. }
  73. static void puv3_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  74. {
  75. struct puv3_pwm_chip *puv3 = to_puv3(chip);
  76. clk_disable_unprepare(puv3->clk);
  77. }
  78. static const struct pwm_ops puv3_pwm_ops = {
  79. .config = puv3_pwm_config,
  80. .enable = puv3_pwm_enable,
  81. .disable = puv3_pwm_disable,
  82. .owner = THIS_MODULE,
  83. };
  84. static int pwm_probe(struct platform_device *pdev)
  85. {
  86. struct puv3_pwm_chip *puv3;
  87. struct resource *r;
  88. int ret;
  89. puv3 = devm_kzalloc(&pdev->dev, sizeof(*puv3), GFP_KERNEL);
  90. if (puv3 == NULL) {
  91. dev_err(&pdev->dev, "failed to allocate memory\n");
  92. return -ENOMEM;
  93. }
  94. puv3->clk = devm_clk_get(&pdev->dev, "OST_CLK");
  95. if (IS_ERR(puv3->clk))
  96. return PTR_ERR(puv3->clk);
  97. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  98. if (r == NULL) {
  99. dev_err(&pdev->dev, "no memory resource defined\n");
  100. return -ENODEV;
  101. }
  102. puv3->base = devm_ioremap_resource(&pdev->dev, r);
  103. if (IS_ERR(puv3->base))
  104. return PTR_ERR(puv3->base);
  105. puv3->chip.dev = &pdev->dev;
  106. puv3->chip.ops = &puv3_pwm_ops;
  107. puv3->chip.base = -1;
  108. puv3->chip.npwm = 1;
  109. ret = pwmchip_add(&puv3->chip);
  110. if (ret < 0) {
  111. dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
  112. return ret;
  113. }
  114. platform_set_drvdata(pdev, puv3);
  115. return 0;
  116. }
  117. static int pwm_remove(struct platform_device *pdev)
  118. {
  119. struct puv3_pwm_chip *puv3 = platform_get_drvdata(pdev);
  120. return pwmchip_remove(&puv3->chip);
  121. }
  122. static struct platform_driver puv3_pwm_driver = {
  123. .driver = {
  124. .name = "PKUnity-v3-PWM",
  125. },
  126. .probe = pwm_probe,
  127. .remove = pwm_remove,
  128. };
  129. module_platform_driver(puv3_pwm_driver);
  130. MODULE_LICENSE("GPL v2");