pwm-jz4740.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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/gpio.h>
  23. #include <asm/mach-jz4740/timer.h>
  24. #define NUM_PWM 8
  25. static const unsigned int jz4740_pwm_gpio_list[NUM_PWM] = {
  26. JZ_GPIO_PWM0,
  27. JZ_GPIO_PWM1,
  28. JZ_GPIO_PWM2,
  29. JZ_GPIO_PWM3,
  30. JZ_GPIO_PWM4,
  31. JZ_GPIO_PWM5,
  32. JZ_GPIO_PWM6,
  33. JZ_GPIO_PWM7,
  34. };
  35. struct jz4740_pwm_chip {
  36. struct pwm_chip chip;
  37. struct clk *clk;
  38. };
  39. static inline struct jz4740_pwm_chip *to_jz4740(struct pwm_chip *chip)
  40. {
  41. return container_of(chip, struct jz4740_pwm_chip, chip);
  42. }
  43. static int jz4740_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
  44. {
  45. unsigned int gpio = jz4740_pwm_gpio_list[pwm->hwpwm];
  46. int ret;
  47. /*
  48. * Timers 0 and 1 are used for system tasks, so they are unavailable
  49. * for use as PWMs.
  50. */
  51. if (pwm->hwpwm < 2)
  52. return -EBUSY;
  53. ret = gpio_request(gpio, pwm->label);
  54. if (ret) {
  55. dev_err(chip->dev, "Failed to request GPIO#%u for PWM: %d\n",
  56. gpio, ret);
  57. return ret;
  58. }
  59. jz_gpio_set_function(gpio, JZ_GPIO_FUNC_PWM);
  60. jz4740_timer_start(pwm->hwpwm);
  61. return 0;
  62. }
  63. static void jz4740_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
  64. {
  65. unsigned int gpio = jz4740_pwm_gpio_list[pwm->hwpwm];
  66. jz4740_timer_set_ctrl(pwm->hwpwm, 0);
  67. jz_gpio_set_function(gpio, JZ_GPIO_FUNC_NONE);
  68. gpio_free(gpio);
  69. jz4740_timer_stop(pwm->hwpwm);
  70. }
  71. static int jz4740_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  72. {
  73. uint32_t ctrl = jz4740_timer_get_ctrl(pwm->pwm);
  74. ctrl |= JZ_TIMER_CTRL_PWM_ENABLE;
  75. jz4740_timer_set_ctrl(pwm->hwpwm, ctrl);
  76. jz4740_timer_enable(pwm->hwpwm);
  77. return 0;
  78. }
  79. static void jz4740_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  80. {
  81. uint32_t ctrl = jz4740_timer_get_ctrl(pwm->hwpwm);
  82. ctrl &= ~JZ_TIMER_CTRL_PWM_ENABLE;
  83. jz4740_timer_disable(pwm->hwpwm);
  84. jz4740_timer_set_ctrl(pwm->hwpwm, ctrl);
  85. }
  86. static int jz4740_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  87. int duty_ns, int period_ns)
  88. {
  89. struct jz4740_pwm_chip *jz4740 = to_jz4740(pwm->chip);
  90. unsigned long long tmp;
  91. unsigned long period, duty;
  92. unsigned int prescaler = 0;
  93. uint16_t ctrl;
  94. bool is_enabled;
  95. tmp = (unsigned long long)clk_get_rate(jz4740->clk) * period_ns;
  96. do_div(tmp, 1000000000);
  97. period = tmp;
  98. while (period > 0xffff && prescaler < 6) {
  99. period >>= 2;
  100. ++prescaler;
  101. }
  102. if (prescaler == 6)
  103. return -EINVAL;
  104. tmp = (unsigned long long)period * duty_ns;
  105. do_div(tmp, period_ns);
  106. duty = period - tmp;
  107. if (duty >= period)
  108. duty = period - 1;
  109. is_enabled = jz4740_timer_is_enabled(pwm->hwpwm);
  110. if (is_enabled)
  111. jz4740_pwm_disable(chip, pwm);
  112. jz4740_timer_set_count(pwm->hwpwm, 0);
  113. jz4740_timer_set_duty(pwm->hwpwm, duty);
  114. jz4740_timer_set_period(pwm->hwpwm, period);
  115. ctrl = JZ_TIMER_CTRL_PRESCALER(prescaler) | JZ_TIMER_CTRL_SRC_EXT |
  116. JZ_TIMER_CTRL_PWM_ABBRUPT_SHUTDOWN;
  117. jz4740_timer_set_ctrl(pwm->hwpwm, ctrl);
  118. if (is_enabled)
  119. jz4740_pwm_enable(chip, pwm);
  120. return 0;
  121. }
  122. static const struct pwm_ops jz4740_pwm_ops = {
  123. .request = jz4740_pwm_request,
  124. .free = jz4740_pwm_free,
  125. .config = jz4740_pwm_config,
  126. .enable = jz4740_pwm_enable,
  127. .disable = jz4740_pwm_disable,
  128. .owner = THIS_MODULE,
  129. };
  130. static int jz4740_pwm_probe(struct platform_device *pdev)
  131. {
  132. struct jz4740_pwm_chip *jz4740;
  133. jz4740 = devm_kzalloc(&pdev->dev, sizeof(*jz4740), GFP_KERNEL);
  134. if (!jz4740)
  135. return -ENOMEM;
  136. jz4740->clk = devm_clk_get(&pdev->dev, "ext");
  137. if (IS_ERR(jz4740->clk))
  138. return PTR_ERR(jz4740->clk);
  139. jz4740->chip.dev = &pdev->dev;
  140. jz4740->chip.ops = &jz4740_pwm_ops;
  141. jz4740->chip.npwm = NUM_PWM;
  142. jz4740->chip.base = -1;
  143. platform_set_drvdata(pdev, jz4740);
  144. return pwmchip_add(&jz4740->chip);
  145. }
  146. static int jz4740_pwm_remove(struct platform_device *pdev)
  147. {
  148. struct jz4740_pwm_chip *jz4740 = platform_get_drvdata(pdev);
  149. return pwmchip_remove(&jz4740->chip);
  150. }
  151. static struct platform_driver jz4740_pwm_driver = {
  152. .driver = {
  153. .name = "jz4740-pwm",
  154. },
  155. .probe = jz4740_pwm_probe,
  156. .remove = jz4740_pwm_remove,
  157. };
  158. module_platform_driver(jz4740_pwm_driver);
  159. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  160. MODULE_DESCRIPTION("Ingenic JZ4740 PWM driver");
  161. MODULE_ALIAS("platform:jz4740-pwm");
  162. MODULE_LICENSE("GPL");