pwm-jz4740.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
  3. * JZ4740 platform PWM support
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version.
  9. *
  10. * You should have received a copy of the GNU General Public License along
  11. * with this program; if not, write to the Free Software Foundation, Inc.,
  12. * 675 Mass Ave, Cambridge, MA 02139, USA.
  13. *
  14. */
  15. #include <linux/clk.h>
  16. #include <linux/err.h>
  17. #include <linux/gpio.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/pwm.h>
  22. #include <asm/mach-jz4740/timer.h>
  23. #define NUM_PWM 8
  24. struct jz4740_pwm_chip {
  25. struct pwm_chip chip;
  26. struct clk *clk;
  27. };
  28. static inline struct jz4740_pwm_chip *to_jz4740(struct pwm_chip *chip)
  29. {
  30. return container_of(chip, struct jz4740_pwm_chip, chip);
  31. }
  32. static int jz4740_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
  33. {
  34. /*
  35. * Timers 0 and 1 are used for system tasks, so they are unavailable
  36. * for use as PWMs.
  37. */
  38. if (pwm->hwpwm < 2)
  39. return -EBUSY;
  40. jz4740_timer_start(pwm->hwpwm);
  41. return 0;
  42. }
  43. static void jz4740_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
  44. {
  45. jz4740_timer_set_ctrl(pwm->hwpwm, 0);
  46. jz4740_timer_stop(pwm->hwpwm);
  47. }
  48. static int jz4740_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  49. {
  50. uint32_t ctrl = jz4740_timer_get_ctrl(pwm->pwm);
  51. ctrl |= JZ_TIMER_CTRL_PWM_ENABLE;
  52. jz4740_timer_set_ctrl(pwm->hwpwm, ctrl);
  53. jz4740_timer_enable(pwm->hwpwm);
  54. return 0;
  55. }
  56. static void jz4740_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  57. {
  58. uint32_t ctrl = jz4740_timer_get_ctrl(pwm->hwpwm);
  59. ctrl &= ~JZ_TIMER_CTRL_PWM_ENABLE;
  60. jz4740_timer_disable(pwm->hwpwm);
  61. jz4740_timer_set_ctrl(pwm->hwpwm, ctrl);
  62. }
  63. static int jz4740_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  64. int duty_ns, int period_ns)
  65. {
  66. struct jz4740_pwm_chip *jz4740 = to_jz4740(pwm->chip);
  67. unsigned long long tmp;
  68. unsigned long period, duty;
  69. unsigned int prescaler = 0;
  70. uint16_t ctrl;
  71. bool is_enabled;
  72. tmp = (unsigned long long)clk_get_rate(jz4740->clk) * period_ns;
  73. do_div(tmp, 1000000000);
  74. period = tmp;
  75. while (period > 0xffff && prescaler < 6) {
  76. period >>= 2;
  77. ++prescaler;
  78. }
  79. if (prescaler == 6)
  80. return -EINVAL;
  81. tmp = (unsigned long long)period * duty_ns;
  82. do_div(tmp, period_ns);
  83. duty = period - tmp;
  84. if (duty >= period)
  85. duty = period - 1;
  86. is_enabled = jz4740_timer_is_enabled(pwm->hwpwm);
  87. if (is_enabled)
  88. jz4740_pwm_disable(chip, pwm);
  89. jz4740_timer_set_count(pwm->hwpwm, 0);
  90. jz4740_timer_set_duty(pwm->hwpwm, duty);
  91. jz4740_timer_set_period(pwm->hwpwm, period);
  92. ctrl = JZ_TIMER_CTRL_PRESCALER(prescaler) | JZ_TIMER_CTRL_SRC_EXT |
  93. JZ_TIMER_CTRL_PWM_ABBRUPT_SHUTDOWN;
  94. jz4740_timer_set_ctrl(pwm->hwpwm, ctrl);
  95. if (is_enabled)
  96. jz4740_pwm_enable(chip, pwm);
  97. return 0;
  98. }
  99. static const struct pwm_ops jz4740_pwm_ops = {
  100. .request = jz4740_pwm_request,
  101. .free = jz4740_pwm_free,
  102. .config = jz4740_pwm_config,
  103. .enable = jz4740_pwm_enable,
  104. .disable = jz4740_pwm_disable,
  105. .owner = THIS_MODULE,
  106. };
  107. static int jz4740_pwm_probe(struct platform_device *pdev)
  108. {
  109. struct jz4740_pwm_chip *jz4740;
  110. jz4740 = devm_kzalloc(&pdev->dev, sizeof(*jz4740), GFP_KERNEL);
  111. if (!jz4740)
  112. return -ENOMEM;
  113. jz4740->clk = devm_clk_get(&pdev->dev, "ext");
  114. if (IS_ERR(jz4740->clk))
  115. return PTR_ERR(jz4740->clk);
  116. jz4740->chip.dev = &pdev->dev;
  117. jz4740->chip.ops = &jz4740_pwm_ops;
  118. jz4740->chip.npwm = NUM_PWM;
  119. jz4740->chip.base = -1;
  120. platform_set_drvdata(pdev, jz4740);
  121. return pwmchip_add(&jz4740->chip);
  122. }
  123. static int jz4740_pwm_remove(struct platform_device *pdev)
  124. {
  125. struct jz4740_pwm_chip *jz4740 = platform_get_drvdata(pdev);
  126. return pwmchip_remove(&jz4740->chip);
  127. }
  128. static struct platform_driver jz4740_pwm_driver = {
  129. .driver = {
  130. .name = "jz4740-pwm",
  131. },
  132. .probe = jz4740_pwm_probe,
  133. .remove = jz4740_pwm_remove,
  134. };
  135. module_platform_driver(jz4740_pwm_driver);
  136. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  137. MODULE_DESCRIPTION("Ingenic JZ4740 PWM driver");
  138. MODULE_ALIAS("platform:jz4740-pwm");
  139. MODULE_LICENSE("GPL");