pwm-jz4740.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
  4. * JZ4740 platform PWM support
  5. *
  6. * Limitations:
  7. * - The .apply callback doesn't complete the currently running period before
  8. * reconfiguring the hardware.
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/err.h>
  12. #include <linux/gpio.h>
  13. #include <linux/kernel.h>
  14. #include <linux/mfd/ingenic-tcu.h>
  15. #include <linux/mfd/syscon.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/pwm.h>
  20. #include <linux/regmap.h>
  21. struct soc_info {
  22. unsigned int num_pwms;
  23. };
  24. struct jz4740_pwm_chip {
  25. struct regmap *map;
  26. struct clk *clk[];
  27. };
  28. static inline struct jz4740_pwm_chip *to_jz4740(struct pwm_chip *chip)
  29. {
  30. return pwmchip_get_drvdata(chip);
  31. }
  32. static bool jz4740_pwm_can_use_chn(struct pwm_chip *chip, unsigned int channel)
  33. {
  34. /* Enable all TCU channels for PWM use by default except channels 0/1 */
  35. u32 pwm_channels_mask = GENMASK(chip->npwm - 1, 2);
  36. device_property_read_u32(pwmchip_parent(chip)->parent,
  37. "ingenic,pwm-channels-mask",
  38. &pwm_channels_mask);
  39. return !!(pwm_channels_mask & BIT(channel));
  40. }
  41. static int jz4740_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
  42. {
  43. struct jz4740_pwm_chip *jz = to_jz4740(chip);
  44. struct clk *clk;
  45. char name[16];
  46. int err;
  47. if (!jz4740_pwm_can_use_chn(chip, pwm->hwpwm))
  48. return -EBUSY;
  49. snprintf(name, sizeof(name), "timer%u", pwm->hwpwm);
  50. clk = clk_get(pwmchip_parent(chip), name);
  51. if (IS_ERR(clk)) {
  52. dev_err(pwmchip_parent(chip),
  53. "error %pe: Failed to get clock\n", clk);
  54. return PTR_ERR(clk);
  55. }
  56. err = clk_prepare_enable(clk);
  57. if (err < 0) {
  58. clk_put(clk);
  59. return err;
  60. }
  61. jz->clk[pwm->hwpwm] = clk;
  62. return 0;
  63. }
  64. static void jz4740_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
  65. {
  66. struct jz4740_pwm_chip *jz = to_jz4740(chip);
  67. struct clk *clk = jz->clk[pwm->hwpwm];
  68. clk_disable_unprepare(clk);
  69. clk_put(clk);
  70. }
  71. static int jz4740_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  72. {
  73. struct jz4740_pwm_chip *jz = to_jz4740(chip);
  74. /* Enable PWM output */
  75. regmap_set_bits(jz->map, TCU_REG_TCSRc(pwm->hwpwm), TCU_TCSR_PWM_EN);
  76. /* Start counter */
  77. regmap_write(jz->map, TCU_REG_TESR, BIT(pwm->hwpwm));
  78. return 0;
  79. }
  80. static void jz4740_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  81. {
  82. struct jz4740_pwm_chip *jz = to_jz4740(chip);
  83. /*
  84. * Set duty > period. This trick allows the TCU channels in TCU2 mode to
  85. * properly return to their init level.
  86. */
  87. regmap_write(jz->map, TCU_REG_TDHRc(pwm->hwpwm), 0xffff);
  88. regmap_write(jz->map, TCU_REG_TDFRc(pwm->hwpwm), 0x0);
  89. /*
  90. * Disable PWM output.
  91. * In TCU2 mode (channel 1/2 on JZ4750+), this must be done before the
  92. * counter is stopped, while in TCU1 mode the order does not matter.
  93. */
  94. regmap_clear_bits(jz->map, TCU_REG_TCSRc(pwm->hwpwm), TCU_TCSR_PWM_EN);
  95. /* Stop counter */
  96. regmap_write(jz->map, TCU_REG_TECR, BIT(pwm->hwpwm));
  97. }
  98. static int jz4740_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  99. const struct pwm_state *state)
  100. {
  101. struct jz4740_pwm_chip *jz = to_jz4740(chip);
  102. unsigned long long tmp = 0xffffull * NSEC_PER_SEC;
  103. struct clk *clk = jz->clk[pwm->hwpwm];
  104. unsigned long period, duty;
  105. long rate;
  106. int err;
  107. /*
  108. * Limit the clock to a maximum rate that still gives us a period value
  109. * which fits in 16 bits.
  110. */
  111. do_div(tmp, state->period);
  112. /*
  113. * /!\ IMPORTANT NOTE:
  114. * -------------------
  115. * This code relies on the fact that clk_round_rate() will always round
  116. * down, which is not a valid assumption given by the clk API, but only
  117. * happens to be true with the clk drivers used for Ingenic SoCs.
  118. *
  119. * Right now, there is no alternative as the clk API does not have a
  120. * round-down function (and won't have one for a while), but if it ever
  121. * comes to light, a round-down function should be used instead.
  122. */
  123. rate = clk_round_rate(clk, tmp);
  124. if (rate < 0) {
  125. dev_err(pwmchip_parent(chip), "Unable to round rate: %ld\n", rate);
  126. return rate;
  127. }
  128. /* Calculate period value */
  129. tmp = (unsigned long long)rate * state->period;
  130. do_div(tmp, NSEC_PER_SEC);
  131. period = tmp;
  132. /* Calculate duty value */
  133. tmp = (unsigned long long)rate * state->duty_cycle;
  134. do_div(tmp, NSEC_PER_SEC);
  135. duty = tmp;
  136. if (duty >= period)
  137. duty = period - 1;
  138. jz4740_pwm_disable(chip, pwm);
  139. err = clk_set_rate(clk, rate);
  140. if (err) {
  141. dev_err(pwmchip_parent(chip), "Unable to set rate: %d\n", err);
  142. return err;
  143. }
  144. /* Reset counter to 0 */
  145. regmap_write(jz->map, TCU_REG_TCNTc(pwm->hwpwm), 0);
  146. /* Set duty */
  147. regmap_write(jz->map, TCU_REG_TDHRc(pwm->hwpwm), duty);
  148. /* Set period */
  149. regmap_write(jz->map, TCU_REG_TDFRc(pwm->hwpwm), period);
  150. /* Set abrupt shutdown */
  151. regmap_set_bits(jz->map, TCU_REG_TCSRc(pwm->hwpwm),
  152. TCU_TCSR_PWM_SD);
  153. /*
  154. * Set polarity.
  155. *
  156. * The PWM starts in inactive state until the internal timer reaches the
  157. * duty value, then becomes active until the timer reaches the period
  158. * value. In theory, we should then use (period - duty) as the real duty
  159. * value, as a high duty value would otherwise result in the PWM pin
  160. * being inactive most of the time.
  161. *
  162. * Here, we don't do that, and instead invert the polarity of the PWM
  163. * when it is active. This trick makes the PWM start with its active
  164. * state instead of its inactive state.
  165. */
  166. if ((state->polarity == PWM_POLARITY_NORMAL) ^ state->enabled)
  167. regmap_clear_bits(jz->map, TCU_REG_TCSRc(pwm->hwpwm),
  168. TCU_TCSR_PWM_INITL_HIGH);
  169. else
  170. regmap_set_bits(jz->map, TCU_REG_TCSRc(pwm->hwpwm),
  171. TCU_TCSR_PWM_INITL_HIGH);
  172. if (state->enabled)
  173. jz4740_pwm_enable(chip, pwm);
  174. return 0;
  175. }
  176. static const struct pwm_ops jz4740_pwm_ops = {
  177. .request = jz4740_pwm_request,
  178. .free = jz4740_pwm_free,
  179. .apply = jz4740_pwm_apply,
  180. };
  181. static int jz4740_pwm_probe(struct platform_device *pdev)
  182. {
  183. struct device *dev = &pdev->dev;
  184. struct pwm_chip *chip;
  185. struct jz4740_pwm_chip *jz;
  186. const struct soc_info *info;
  187. info = device_get_match_data(dev);
  188. if (!info)
  189. return -EINVAL;
  190. chip = devm_pwmchip_alloc(dev, info->num_pwms, struct_size(jz, clk, info->num_pwms));
  191. if (IS_ERR(chip))
  192. return PTR_ERR(chip);
  193. jz = to_jz4740(chip);
  194. jz->map = device_node_to_regmap(dev->parent->of_node);
  195. if (IS_ERR(jz->map)) {
  196. dev_err(dev, "regmap not found: %ld\n", PTR_ERR(jz->map));
  197. return PTR_ERR(jz->map);
  198. }
  199. chip->ops = &jz4740_pwm_ops;
  200. return devm_pwmchip_add(dev, chip);
  201. }
  202. static const struct soc_info jz4740_soc_info = {
  203. .num_pwms = 8,
  204. };
  205. static const struct soc_info jz4725b_soc_info = {
  206. .num_pwms = 6,
  207. };
  208. static const struct soc_info x1000_soc_info = {
  209. .num_pwms = 5,
  210. };
  211. static const struct of_device_id jz4740_pwm_dt_ids[] = {
  212. { .compatible = "ingenic,jz4740-pwm", .data = &jz4740_soc_info },
  213. { .compatible = "ingenic,jz4725b-pwm", .data = &jz4725b_soc_info },
  214. { .compatible = "ingenic,x1000-pwm", .data = &x1000_soc_info },
  215. {},
  216. };
  217. MODULE_DEVICE_TABLE(of, jz4740_pwm_dt_ids);
  218. static struct platform_driver jz4740_pwm_driver = {
  219. .driver = {
  220. .name = "jz4740-pwm",
  221. .of_match_table = jz4740_pwm_dt_ids,
  222. },
  223. .probe = jz4740_pwm_probe,
  224. };
  225. module_platform_driver(jz4740_pwm_driver);
  226. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  227. MODULE_DESCRIPTION("Ingenic JZ4740 PWM driver");
  228. MODULE_ALIAS("platform:jz4740-pwm");
  229. MODULE_LICENSE("GPL");