pwm-jz4740.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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/of_device.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/pwm.h>
  23. #include <asm/mach-jz4740/timer.h>
  24. #define NUM_PWM 8
  25. struct jz4740_pwm_chip {
  26. struct pwm_chip chip;
  27. struct clk *clk;
  28. };
  29. static inline struct jz4740_pwm_chip *to_jz4740(struct pwm_chip *chip)
  30. {
  31. return container_of(chip, struct jz4740_pwm_chip, chip);
  32. }
  33. static int jz4740_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
  34. {
  35. /*
  36. * Timers 0 and 1 are used for system tasks, so they are unavailable
  37. * for use as PWMs.
  38. */
  39. if (pwm->hwpwm < 2)
  40. return -EBUSY;
  41. jz4740_timer_start(pwm->hwpwm);
  42. return 0;
  43. }
  44. static void jz4740_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
  45. {
  46. jz4740_timer_set_ctrl(pwm->hwpwm, 0);
  47. jz4740_timer_stop(pwm->hwpwm);
  48. }
  49. static int jz4740_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  50. {
  51. uint32_t ctrl = jz4740_timer_get_ctrl(pwm->pwm);
  52. ctrl |= JZ_TIMER_CTRL_PWM_ENABLE;
  53. jz4740_timer_set_ctrl(pwm->hwpwm, ctrl);
  54. jz4740_timer_enable(pwm->hwpwm);
  55. return 0;
  56. }
  57. static void jz4740_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  58. {
  59. uint32_t ctrl = jz4740_timer_get_ctrl(pwm->hwpwm);
  60. /* Disable PWM output.
  61. * In TCU2 mode (channel 1/2 on JZ4750+), this must be done before the
  62. * counter is stopped, while in TCU1 mode the order does not matter.
  63. */
  64. ctrl &= ~JZ_TIMER_CTRL_PWM_ENABLE;
  65. jz4740_timer_set_ctrl(pwm->hwpwm, ctrl);
  66. /* Stop counter */
  67. jz4740_timer_disable(pwm->hwpwm);
  68. }
  69. static int jz4740_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  70. int duty_ns, int period_ns)
  71. {
  72. struct jz4740_pwm_chip *jz4740 = to_jz4740(pwm->chip);
  73. unsigned long long tmp;
  74. unsigned long period, duty;
  75. unsigned int prescaler = 0;
  76. uint16_t ctrl;
  77. bool is_enabled;
  78. tmp = (unsigned long long)clk_get_rate(jz4740->clk) * period_ns;
  79. do_div(tmp, 1000000000);
  80. period = tmp;
  81. while (period > 0xffff && prescaler < 6) {
  82. period >>= 2;
  83. ++prescaler;
  84. }
  85. if (prescaler == 6)
  86. return -EINVAL;
  87. tmp = (unsigned long long)period * duty_ns;
  88. do_div(tmp, period_ns);
  89. duty = period - tmp;
  90. if (duty >= period)
  91. duty = period - 1;
  92. is_enabled = jz4740_timer_is_enabled(pwm->hwpwm);
  93. if (is_enabled)
  94. jz4740_pwm_disable(chip, pwm);
  95. jz4740_timer_set_count(pwm->hwpwm, 0);
  96. jz4740_timer_set_duty(pwm->hwpwm, duty);
  97. jz4740_timer_set_period(pwm->hwpwm, period);
  98. ctrl = JZ_TIMER_CTRL_PRESCALER(prescaler) | JZ_TIMER_CTRL_SRC_EXT |
  99. JZ_TIMER_CTRL_PWM_ABBRUPT_SHUTDOWN;
  100. jz4740_timer_set_ctrl(pwm->hwpwm, ctrl);
  101. if (is_enabled)
  102. jz4740_pwm_enable(chip, pwm);
  103. return 0;
  104. }
  105. static int jz4740_pwm_set_polarity(struct pwm_chip *chip,
  106. struct pwm_device *pwm, enum pwm_polarity polarity)
  107. {
  108. uint32_t ctrl = jz4740_timer_get_ctrl(pwm->pwm);
  109. switch (polarity) {
  110. case PWM_POLARITY_NORMAL:
  111. ctrl &= ~JZ_TIMER_CTRL_PWM_ACTIVE_LOW;
  112. break;
  113. case PWM_POLARITY_INVERSED:
  114. ctrl |= JZ_TIMER_CTRL_PWM_ACTIVE_LOW;
  115. break;
  116. }
  117. jz4740_timer_set_ctrl(pwm->hwpwm, ctrl);
  118. return 0;
  119. }
  120. static const struct pwm_ops jz4740_pwm_ops = {
  121. .request = jz4740_pwm_request,
  122. .free = jz4740_pwm_free,
  123. .config = jz4740_pwm_config,
  124. .set_polarity = jz4740_pwm_set_polarity,
  125. .enable = jz4740_pwm_enable,
  126. .disable = jz4740_pwm_disable,
  127. .owner = THIS_MODULE,
  128. };
  129. static int jz4740_pwm_probe(struct platform_device *pdev)
  130. {
  131. struct jz4740_pwm_chip *jz4740;
  132. jz4740 = devm_kzalloc(&pdev->dev, sizeof(*jz4740), GFP_KERNEL);
  133. if (!jz4740)
  134. return -ENOMEM;
  135. jz4740->clk = devm_clk_get(&pdev->dev, "ext");
  136. if (IS_ERR(jz4740->clk))
  137. return PTR_ERR(jz4740->clk);
  138. jz4740->chip.dev = &pdev->dev;
  139. jz4740->chip.ops = &jz4740_pwm_ops;
  140. jz4740->chip.npwm = NUM_PWM;
  141. jz4740->chip.base = -1;
  142. jz4740->chip.of_xlate = of_pwm_xlate_with_flags;
  143. jz4740->chip.of_pwm_n_cells = 3;
  144. platform_set_drvdata(pdev, jz4740);
  145. return pwmchip_add(&jz4740->chip);
  146. }
  147. static int jz4740_pwm_remove(struct platform_device *pdev)
  148. {
  149. struct jz4740_pwm_chip *jz4740 = platform_get_drvdata(pdev);
  150. return pwmchip_remove(&jz4740->chip);
  151. }
  152. #ifdef CONFIG_OF
  153. static const struct of_device_id jz4740_pwm_dt_ids[] = {
  154. { .compatible = "ingenic,jz4740-pwm", },
  155. { .compatible = "ingenic,jz4770-pwm", },
  156. { .compatible = "ingenic,jz4780-pwm", },
  157. {},
  158. };
  159. MODULE_DEVICE_TABLE(of, jz4740_pwm_dt_ids);
  160. #endif
  161. static struct platform_driver jz4740_pwm_driver = {
  162. .driver = {
  163. .name = "jz4740-pwm",
  164. .of_match_table = of_match_ptr(jz4740_pwm_dt_ids),
  165. },
  166. .probe = jz4740_pwm_probe,
  167. .remove = jz4740_pwm_remove,
  168. };
  169. module_platform_driver(jz4740_pwm_driver);
  170. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  171. MODULE_DESCRIPTION("Ingenic JZ4740 PWM driver");
  172. MODULE_ALIAS("platform:jz4740-pwm");
  173. MODULE_LICENSE("GPL");