pwm-puv3.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. };
  28. static inline struct puv3_pwm_chip *to_puv3(struct pwm_chip *chip)
  29. {
  30. return container_of(chip, struct puv3_pwm_chip, chip);
  31. }
  32. /*
  33. * period_ns = 10^9 * (PRESCALE + 1) * (PV + 1) / PWM_CLK_RATE
  34. * duty_ns = 10^9 * (PRESCALE + 1) * DC / PWM_CLK_RATE
  35. */
  36. static int puv3_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  37. int duty_ns, int period_ns)
  38. {
  39. unsigned long period_cycles, prescale, pv, dc;
  40. struct puv3_pwm_chip *puv3 = to_puv3(chip);
  41. unsigned long long c;
  42. c = clk_get_rate(puv3->clk);
  43. c = c * period_ns;
  44. do_div(c, 1000000000);
  45. period_cycles = c;
  46. if (period_cycles < 1)
  47. period_cycles = 1;
  48. prescale = (period_cycles - 1) / 1024;
  49. pv = period_cycles / (prescale + 1) - 1;
  50. if (prescale > 63)
  51. return -EINVAL;
  52. if (duty_ns == period_ns)
  53. dc = OST_PWMDCCR_FDCYCLE;
  54. else
  55. dc = (pv + 1) * duty_ns / period_ns;
  56. /*
  57. * NOTE: the clock to PWM has to be enabled first
  58. * before writing to the registers
  59. */
  60. clk_prepare_enable(puv3->clk);
  61. writel(prescale, puv3->base + OST_PWM_PWCR);
  62. writel(pv - dc, puv3->base + OST_PWM_DCCR);
  63. writel(pv, puv3->base + OST_PWM_PCR);
  64. clk_disable_unprepare(puv3->clk);
  65. return 0;
  66. }
  67. static int puv3_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  68. {
  69. struct puv3_pwm_chip *puv3 = to_puv3(chip);
  70. return clk_prepare_enable(puv3->clk);
  71. }
  72. static void puv3_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  73. {
  74. struct puv3_pwm_chip *puv3 = to_puv3(chip);
  75. clk_disable_unprepare(puv3->clk);
  76. }
  77. static const struct pwm_ops puv3_pwm_ops = {
  78. .config = puv3_pwm_config,
  79. .enable = puv3_pwm_enable,
  80. .disable = puv3_pwm_disable,
  81. .owner = THIS_MODULE,
  82. };
  83. static int pwm_probe(struct platform_device *pdev)
  84. {
  85. struct puv3_pwm_chip *puv3;
  86. struct resource *r;
  87. int ret;
  88. puv3 = devm_kzalloc(&pdev->dev, sizeof(*puv3), GFP_KERNEL);
  89. if (puv3 == NULL) {
  90. dev_err(&pdev->dev, "failed to allocate memory\n");
  91. return -ENOMEM;
  92. }
  93. puv3->clk = devm_clk_get(&pdev->dev, "OST_CLK");
  94. if (IS_ERR(puv3->clk))
  95. return PTR_ERR(puv3->clk);
  96. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  97. puv3->base = devm_ioremap_resource(&pdev->dev, r);
  98. if (IS_ERR(puv3->base))
  99. return PTR_ERR(puv3->base);
  100. puv3->chip.dev = &pdev->dev;
  101. puv3->chip.ops = &puv3_pwm_ops;
  102. puv3->chip.base = -1;
  103. puv3->chip.npwm = 1;
  104. ret = pwmchip_add(&puv3->chip);
  105. if (ret < 0) {
  106. dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
  107. return ret;
  108. }
  109. platform_set_drvdata(pdev, puv3);
  110. return 0;
  111. }
  112. static int pwm_remove(struct platform_device *pdev)
  113. {
  114. struct puv3_pwm_chip *puv3 = platform_get_drvdata(pdev);
  115. return pwmchip_remove(&puv3->chip);
  116. }
  117. static struct platform_driver puv3_pwm_driver = {
  118. .driver = {
  119. .name = "PKUnity-v3-PWM",
  120. },
  121. .probe = pwm_probe,
  122. .remove = pwm_remove,
  123. };
  124. module_platform_driver(puv3_pwm_driver);
  125. MODULE_LICENSE("GPL v2");