pwm-jz4740.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2010, Lars-Peter Clausen <[email protected]>
  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_device.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 pwm_chip chip;
  26. struct regmap *map;
  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 bool jz4740_pwm_can_use_chn(struct jz4740_pwm_chip *jz,
  33. unsigned int channel)
  34. {
  35. /* Enable all TCU channels for PWM use by default except channels 0/1 */
  36. u32 pwm_channels_mask = GENMASK(jz->chip.npwm - 1, 2);
  37. device_property_read_u32(jz->chip.dev->parent,
  38. "ingenic,pwm-channels-mask",
  39. &pwm_channels_mask);
  40. return !!(pwm_channels_mask & BIT(channel));
  41. }
  42. static int jz4740_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
  43. {
  44. struct jz4740_pwm_chip *jz = to_jz4740(chip);
  45. struct clk *clk;
  46. char name[16];
  47. int err;
  48. if (!jz4740_pwm_can_use_chn(jz, pwm->hwpwm))
  49. return -EBUSY;
  50. snprintf(name, sizeof(name), "timer%u", pwm->hwpwm);
  51. clk = clk_get(chip->dev, name);
  52. if (IS_ERR(clk))
  53. return dev_err_probe(chip->dev, PTR_ERR(clk),
  54. "Failed to get clock\n");
  55. err = clk_prepare_enable(clk);
  56. if (err < 0) {
  57. clk_put(clk);
  58. return err;
  59. }
  60. pwm_set_chip_data(pwm, clk);
  61. return 0;
  62. }
  63. static void jz4740_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
  64. {
  65. struct clk *clk = pwm_get_chip_data(pwm);
  66. clk_disable_unprepare(clk);
  67. clk_put(clk);
  68. }
  69. static int jz4740_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  70. {
  71. struct jz4740_pwm_chip *jz = to_jz4740(chip);
  72. /* Enable PWM output */
  73. regmap_update_bits(jz->map, TCU_REG_TCSRc(pwm->hwpwm),
  74. TCU_TCSR_PWM_EN, TCU_TCSR_PWM_EN);
  75. /* Start counter */
  76. regmap_write(jz->map, TCU_REG_TESR, BIT(pwm->hwpwm));
  77. return 0;
  78. }
  79. static void jz4740_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  80. {
  81. struct jz4740_pwm_chip *jz = to_jz4740(chip);
  82. /*
  83. * Set duty > period. This trick allows the TCU channels in TCU2 mode to
  84. * properly return to their init level.
  85. */
  86. regmap_write(jz->map, TCU_REG_TDHRc(pwm->hwpwm), 0xffff);
  87. regmap_write(jz->map, TCU_REG_TDFRc(pwm->hwpwm), 0x0);
  88. /*
  89. * Disable PWM output.
  90. * In TCU2 mode (channel 1/2 on JZ4750+), this must be done before the
  91. * counter is stopped, while in TCU1 mode the order does not matter.
  92. */
  93. regmap_update_bits(jz->map, TCU_REG_TCSRc(pwm->hwpwm),
  94. TCU_TCSR_PWM_EN, 0);
  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 *jz4740 = to_jz4740(pwm->chip);
  102. unsigned long long tmp = 0xffffull * NSEC_PER_SEC;
  103. struct clk *clk = pwm_get_chip_data(pwm);
  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(chip->dev, "Unable to round rate: %ld", 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(chip->dev, "Unable to set rate: %d", err);
  142. return err;
  143. }
  144. /* Reset counter to 0 */
  145. regmap_write(jz4740->map, TCU_REG_TCNTc(pwm->hwpwm), 0);
  146. /* Set duty */
  147. regmap_write(jz4740->map, TCU_REG_TDHRc(pwm->hwpwm), duty);
  148. /* Set period */
  149. regmap_write(jz4740->map, TCU_REG_TDFRc(pwm->hwpwm), period);
  150. /* Set abrupt shutdown */
  151. regmap_update_bits(jz4740->map, TCU_REG_TCSRc(pwm->hwpwm),
  152. TCU_TCSR_PWM_SD, 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_update_bits(jz4740->map, TCU_REG_TCSRc(pwm->hwpwm),
  168. TCU_TCSR_PWM_INITL_HIGH, 0);
  169. else
  170. regmap_update_bits(jz4740->map, TCU_REG_TCSRc(pwm->hwpwm),
  171. TCU_TCSR_PWM_INITL_HIGH,
  172. TCU_TCSR_PWM_INITL_HIGH);
  173. if (state->enabled)
  174. jz4740_pwm_enable(chip, pwm);
  175. return 0;
  176. }
  177. static const struct pwm_ops jz4740_pwm_ops = {
  178. .request = jz4740_pwm_request,
  179. .free = jz4740_pwm_free,
  180. .apply = jz4740_pwm_apply,
  181. .owner = THIS_MODULE,
  182. };
  183. static int jz4740_pwm_probe(struct platform_device *pdev)
  184. {
  185. struct device *dev = &pdev->dev;
  186. struct jz4740_pwm_chip *jz4740;
  187. const struct soc_info *info;
  188. info = device_get_match_data(dev);
  189. if (!info)
  190. return -EINVAL;
  191. jz4740 = devm_kzalloc(dev, sizeof(*jz4740), GFP_KERNEL);
  192. if (!jz4740)
  193. return -ENOMEM;
  194. jz4740->map = device_node_to_regmap(dev->parent->of_node);
  195. if (IS_ERR(jz4740->map)) {
  196. dev_err(dev, "regmap not found: %ld\n", PTR_ERR(jz4740->map));
  197. return PTR_ERR(jz4740->map);
  198. }
  199. jz4740->chip.dev = dev;
  200. jz4740->chip.ops = &jz4740_pwm_ops;
  201. jz4740->chip.npwm = info->num_pwms;
  202. return devm_pwmchip_add(dev, &jz4740->chip);
  203. }
  204. static const struct soc_info __maybe_unused jz4740_soc_info = {
  205. .num_pwms = 8,
  206. };
  207. static const struct soc_info __maybe_unused jz4725b_soc_info = {
  208. .num_pwms = 6,
  209. };
  210. static const struct soc_info __maybe_unused x1000_soc_info = {
  211. .num_pwms = 5,
  212. };
  213. #ifdef CONFIG_OF
  214. static const struct of_device_id jz4740_pwm_dt_ids[] = {
  215. { .compatible = "ingenic,jz4740-pwm", .data = &jz4740_soc_info },
  216. { .compatible = "ingenic,jz4725b-pwm", .data = &jz4725b_soc_info },
  217. { .compatible = "ingenic,x1000-pwm", .data = &x1000_soc_info },
  218. {},
  219. };
  220. MODULE_DEVICE_TABLE(of, jz4740_pwm_dt_ids);
  221. #endif
  222. static struct platform_driver jz4740_pwm_driver = {
  223. .driver = {
  224. .name = "jz4740-pwm",
  225. .of_match_table = of_match_ptr(jz4740_pwm_dt_ids),
  226. },
  227. .probe = jz4740_pwm_probe,
  228. };
  229. module_platform_driver(jz4740_pwm_driver);
  230. MODULE_AUTHOR("Lars-Peter Clausen <[email protected]>");
  231. MODULE_DESCRIPTION("Ingenic JZ4740 PWM driver");
  232. MODULE_ALIAS("platform:jz4740-pwm");
  233. MODULE_LICENSE("GPL");