pwm.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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/kernel.h>
  16. #include <linux/clk.h>
  17. #include <linux/err.h>
  18. #include <linux/pwm.h>
  19. #include <linux/gpio.h>
  20. #include <asm/mach-jz4740/gpio.h>
  21. #include "timer.h"
  22. static struct clk *jz4740_pwm_clk;
  23. DEFINE_MUTEX(jz4740_pwm_mutex);
  24. struct pwm_device {
  25. unsigned int id;
  26. unsigned int gpio;
  27. bool used;
  28. };
  29. static struct pwm_device jz4740_pwm_list[] = {
  30. { 2, JZ_GPIO_PWM2, false },
  31. { 3, JZ_GPIO_PWM3, false },
  32. { 4, JZ_GPIO_PWM4, false },
  33. { 5, JZ_GPIO_PWM5, false },
  34. { 6, JZ_GPIO_PWM6, false },
  35. { 7, JZ_GPIO_PWM7, false },
  36. };
  37. struct pwm_device *pwm_request(int id, const char *label)
  38. {
  39. int ret = 0;
  40. struct pwm_device *pwm;
  41. if (id < 2 || id > 7 || !jz4740_pwm_clk)
  42. return ERR_PTR(-ENODEV);
  43. mutex_lock(&jz4740_pwm_mutex);
  44. pwm = &jz4740_pwm_list[id - 2];
  45. if (pwm->used)
  46. ret = -EBUSY;
  47. else
  48. pwm->used = true;
  49. mutex_unlock(&jz4740_pwm_mutex);
  50. if (ret)
  51. return ERR_PTR(ret);
  52. ret = gpio_request(pwm->gpio, label);
  53. if (ret) {
  54. printk(KERN_ERR "Failed to request pwm gpio: %d\n", ret);
  55. pwm->used = false;
  56. return ERR_PTR(ret);
  57. }
  58. jz_gpio_set_function(pwm->gpio, JZ_GPIO_FUNC_PWM);
  59. jz4740_timer_start(id);
  60. return pwm;
  61. }
  62. void pwm_free(struct pwm_device *pwm)
  63. {
  64. pwm_disable(pwm);
  65. jz4740_timer_set_ctrl(pwm->id, 0);
  66. jz_gpio_set_function(pwm->gpio, JZ_GPIO_FUNC_NONE);
  67. gpio_free(pwm->gpio);
  68. jz4740_timer_stop(pwm->id);
  69. pwm->used = false;
  70. }
  71. int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
  72. {
  73. unsigned long long tmp;
  74. unsigned long period, duty;
  75. unsigned int prescaler = 0;
  76. unsigned int id = pwm->id;
  77. uint16_t ctrl;
  78. bool is_enabled;
  79. if (duty_ns < 0 || duty_ns > period_ns)
  80. return -EINVAL;
  81. tmp = (unsigned long long)clk_get_rate(jz4740_pwm_clk) * period_ns;
  82. do_div(tmp, 1000000000);
  83. period = tmp;
  84. while (period > 0xffff && prescaler < 6) {
  85. period >>= 2;
  86. ++prescaler;
  87. }
  88. if (prescaler == 6)
  89. return -EINVAL;
  90. tmp = (unsigned long long)period * duty_ns;
  91. do_div(tmp, period_ns);
  92. duty = period - tmp;
  93. if (duty >= period)
  94. duty = period - 1;
  95. is_enabled = jz4740_timer_is_enabled(id);
  96. if (is_enabled)
  97. pwm_disable(pwm);
  98. jz4740_timer_set_count(id, 0);
  99. jz4740_timer_set_duty(id, duty);
  100. jz4740_timer_set_period(id, period);
  101. ctrl = JZ_TIMER_CTRL_PRESCALER(prescaler) | JZ_TIMER_CTRL_SRC_EXT |
  102. JZ_TIMER_CTRL_PWM_ABBRUPT_SHUTDOWN;
  103. jz4740_timer_set_ctrl(id, ctrl);
  104. if (is_enabled)
  105. pwm_enable(pwm);
  106. return 0;
  107. }
  108. int pwm_enable(struct pwm_device *pwm)
  109. {
  110. uint32_t ctrl = jz4740_timer_get_ctrl(pwm->id);
  111. ctrl |= JZ_TIMER_CTRL_PWM_ENABLE;
  112. jz4740_timer_set_ctrl(pwm->id, ctrl);
  113. jz4740_timer_enable(pwm->id);
  114. return 0;
  115. }
  116. void pwm_disable(struct pwm_device *pwm)
  117. {
  118. uint32_t ctrl = jz4740_timer_get_ctrl(pwm->id);
  119. ctrl &= ~JZ_TIMER_CTRL_PWM_ENABLE;
  120. jz4740_timer_disable(pwm->id);
  121. jz4740_timer_set_ctrl(pwm->id, ctrl);
  122. }
  123. static int __init jz4740_pwm_init(void)
  124. {
  125. int ret = 0;
  126. jz4740_pwm_clk = clk_get(NULL, "ext");
  127. if (IS_ERR(jz4740_pwm_clk)) {
  128. ret = PTR_ERR(jz4740_pwm_clk);
  129. jz4740_pwm_clk = NULL;
  130. }
  131. return ret;
  132. }
  133. subsys_initcall(jz4740_pwm_init);