pwm-sifive.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2017-2018 SiFive
  4. * For SiFive's PWM IP block documentation please refer Chapter 14 of
  5. * Reference Manual : https://static.dev.sifive.com/FU540-C000-v1.0.pdf
  6. *
  7. * Limitations:
  8. * - When changing both duty cycle and period, we cannot prevent in
  9. * software that the output might produce a period with mixed
  10. * settings (new period length and old duty cycle).
  11. * - The hardware cannot generate a 100% duty cycle.
  12. * - The hardware generates only inverted output.
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/io.h>
  16. #include <linux/module.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/pwm.h>
  19. #include <linux/slab.h>
  20. #include <linux/bitfield.h>
  21. /* Register offsets */
  22. #define PWM_SIFIVE_PWMCFG 0x0
  23. #define PWM_SIFIVE_PWMCOUNT 0x8
  24. #define PWM_SIFIVE_PWMS 0x10
  25. #define PWM_SIFIVE_PWMCMP(i) (0x20 + 4 * (i))
  26. /* PWMCFG fields */
  27. #define PWM_SIFIVE_PWMCFG_SCALE GENMASK(3, 0)
  28. #define PWM_SIFIVE_PWMCFG_STICKY BIT(8)
  29. #define PWM_SIFIVE_PWMCFG_ZERO_CMP BIT(9)
  30. #define PWM_SIFIVE_PWMCFG_DEGLITCH BIT(10)
  31. #define PWM_SIFIVE_PWMCFG_EN_ALWAYS BIT(12)
  32. #define PWM_SIFIVE_PWMCFG_EN_ONCE BIT(13)
  33. #define PWM_SIFIVE_PWMCFG_CENTER BIT(16)
  34. #define PWM_SIFIVE_PWMCFG_GANG BIT(24)
  35. #define PWM_SIFIVE_PWMCFG_IP BIT(28)
  36. #define PWM_SIFIVE_CMPWIDTH 16
  37. #define PWM_SIFIVE_DEFAULT_PERIOD 10000000
  38. struct pwm_sifive_ddata {
  39. struct pwm_chip chip;
  40. struct mutex lock; /* lock to protect user_count and approx_period */
  41. struct notifier_block notifier;
  42. struct clk *clk;
  43. void __iomem *regs;
  44. unsigned int real_period;
  45. unsigned int approx_period;
  46. int user_count;
  47. };
  48. static inline
  49. struct pwm_sifive_ddata *pwm_sifive_chip_to_ddata(struct pwm_chip *c)
  50. {
  51. return container_of(c, struct pwm_sifive_ddata, chip);
  52. }
  53. static int pwm_sifive_request(struct pwm_chip *chip, struct pwm_device *pwm)
  54. {
  55. struct pwm_sifive_ddata *ddata = pwm_sifive_chip_to_ddata(chip);
  56. mutex_lock(&ddata->lock);
  57. ddata->user_count++;
  58. mutex_unlock(&ddata->lock);
  59. return 0;
  60. }
  61. static void pwm_sifive_free(struct pwm_chip *chip, struct pwm_device *pwm)
  62. {
  63. struct pwm_sifive_ddata *ddata = pwm_sifive_chip_to_ddata(chip);
  64. mutex_lock(&ddata->lock);
  65. ddata->user_count--;
  66. mutex_unlock(&ddata->lock);
  67. }
  68. /* Called holding ddata->lock */
  69. static void pwm_sifive_update_clock(struct pwm_sifive_ddata *ddata,
  70. unsigned long rate)
  71. {
  72. unsigned long long num;
  73. unsigned long scale_pow;
  74. int scale;
  75. u32 val;
  76. /*
  77. * The PWM unit is used with pwmzerocmp=0, so the only way to modify the
  78. * period length is using pwmscale which provides the number of bits the
  79. * counter is shifted before being feed to the comparators. A period
  80. * lasts (1 << (PWM_SIFIVE_CMPWIDTH + pwmscale)) clock ticks.
  81. * (1 << (PWM_SIFIVE_CMPWIDTH + scale)) * 10^9/rate = period
  82. */
  83. scale_pow = div64_ul(ddata->approx_period * (u64)rate, NSEC_PER_SEC);
  84. scale = clamp(ilog2(scale_pow) - PWM_SIFIVE_CMPWIDTH, 0, 0xf);
  85. val = PWM_SIFIVE_PWMCFG_EN_ALWAYS |
  86. FIELD_PREP(PWM_SIFIVE_PWMCFG_SCALE, scale);
  87. writel(val, ddata->regs + PWM_SIFIVE_PWMCFG);
  88. /* As scale <= 15 the shift operation cannot overflow. */
  89. num = (unsigned long long)NSEC_PER_SEC << (PWM_SIFIVE_CMPWIDTH + scale);
  90. ddata->real_period = div64_ul(num, rate);
  91. dev_dbg(ddata->chip.dev,
  92. "New real_period = %u ns\n", ddata->real_period);
  93. }
  94. static int pwm_sifive_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
  95. struct pwm_state *state)
  96. {
  97. struct pwm_sifive_ddata *ddata = pwm_sifive_chip_to_ddata(chip);
  98. u32 duty, val;
  99. duty = readl(ddata->regs + PWM_SIFIVE_PWMCMP(pwm->hwpwm));
  100. state->enabled = duty > 0;
  101. val = readl(ddata->regs + PWM_SIFIVE_PWMCFG);
  102. if (!(val & PWM_SIFIVE_PWMCFG_EN_ALWAYS))
  103. state->enabled = false;
  104. state->period = ddata->real_period;
  105. state->duty_cycle =
  106. (u64)duty * ddata->real_period >> PWM_SIFIVE_CMPWIDTH;
  107. state->polarity = PWM_POLARITY_INVERSED;
  108. return 0;
  109. }
  110. static int pwm_sifive_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  111. const struct pwm_state *state)
  112. {
  113. struct pwm_sifive_ddata *ddata = pwm_sifive_chip_to_ddata(chip);
  114. struct pwm_state cur_state;
  115. unsigned int duty_cycle;
  116. unsigned long long num;
  117. bool enabled;
  118. int ret = 0;
  119. u32 frac;
  120. if (state->polarity != PWM_POLARITY_INVERSED)
  121. return -EINVAL;
  122. cur_state = pwm->state;
  123. enabled = cur_state.enabled;
  124. duty_cycle = state->duty_cycle;
  125. if (!state->enabled)
  126. duty_cycle = 0;
  127. /*
  128. * The problem of output producing mixed setting as mentioned at top,
  129. * occurs here. To minimize the window for this problem, we are
  130. * calculating the register values first and then writing them
  131. * consecutively
  132. */
  133. num = (u64)duty_cycle * (1U << PWM_SIFIVE_CMPWIDTH);
  134. frac = DIV64_U64_ROUND_CLOSEST(num, state->period);
  135. /* The hardware cannot generate a 100% duty cycle */
  136. frac = min(frac, (1U << PWM_SIFIVE_CMPWIDTH) - 1);
  137. mutex_lock(&ddata->lock);
  138. if (state->period != ddata->approx_period) {
  139. /*
  140. * Don't let a 2nd user change the period underneath the 1st user.
  141. * However if ddate->approx_period == 0 this is the first time we set
  142. * any period, so let whoever gets here first set the period so other
  143. * users who agree on the period won't fail.
  144. */
  145. if (ddata->user_count != 1 && ddata->approx_period) {
  146. mutex_unlock(&ddata->lock);
  147. return -EBUSY;
  148. }
  149. ddata->approx_period = state->period;
  150. pwm_sifive_update_clock(ddata, clk_get_rate(ddata->clk));
  151. }
  152. mutex_unlock(&ddata->lock);
  153. /*
  154. * If the PWM is enabled the clk is already on. So only enable it
  155. * conditionally to have it on exactly once afterwards independent of
  156. * the PWM state.
  157. */
  158. if (!enabled) {
  159. ret = clk_enable(ddata->clk);
  160. if (ret) {
  161. dev_err(ddata->chip.dev, "Enable clk failed\n");
  162. return ret;
  163. }
  164. }
  165. writel(frac, ddata->regs + PWM_SIFIVE_PWMCMP(pwm->hwpwm));
  166. if (!state->enabled)
  167. clk_disable(ddata->clk);
  168. return 0;
  169. }
  170. static const struct pwm_ops pwm_sifive_ops = {
  171. .request = pwm_sifive_request,
  172. .free = pwm_sifive_free,
  173. .get_state = pwm_sifive_get_state,
  174. .apply = pwm_sifive_apply,
  175. .owner = THIS_MODULE,
  176. };
  177. static int pwm_sifive_clock_notifier(struct notifier_block *nb,
  178. unsigned long event, void *data)
  179. {
  180. struct clk_notifier_data *ndata = data;
  181. struct pwm_sifive_ddata *ddata =
  182. container_of(nb, struct pwm_sifive_ddata, notifier);
  183. if (event == POST_RATE_CHANGE) {
  184. mutex_lock(&ddata->lock);
  185. pwm_sifive_update_clock(ddata, ndata->new_rate);
  186. mutex_unlock(&ddata->lock);
  187. }
  188. return NOTIFY_OK;
  189. }
  190. static int pwm_sifive_probe(struct platform_device *pdev)
  191. {
  192. struct device *dev = &pdev->dev;
  193. struct pwm_sifive_ddata *ddata;
  194. struct pwm_chip *chip;
  195. int ret;
  196. u32 val;
  197. unsigned int enabled_pwms = 0, enabled_clks = 1;
  198. ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);
  199. if (!ddata)
  200. return -ENOMEM;
  201. mutex_init(&ddata->lock);
  202. chip = &ddata->chip;
  203. chip->dev = dev;
  204. chip->ops = &pwm_sifive_ops;
  205. chip->npwm = 4;
  206. ddata->regs = devm_platform_ioremap_resource(pdev, 0);
  207. if (IS_ERR(ddata->regs))
  208. return PTR_ERR(ddata->regs);
  209. ddata->clk = devm_clk_get(dev, NULL);
  210. if (IS_ERR(ddata->clk))
  211. return dev_err_probe(dev, PTR_ERR(ddata->clk),
  212. "Unable to find controller clock\n");
  213. ret = clk_prepare_enable(ddata->clk);
  214. if (ret) {
  215. dev_err(dev, "failed to enable clock for pwm: %d\n", ret);
  216. return ret;
  217. }
  218. val = readl(ddata->regs + PWM_SIFIVE_PWMCFG);
  219. if (val & PWM_SIFIVE_PWMCFG_EN_ALWAYS) {
  220. unsigned int i;
  221. for (i = 0; i < chip->npwm; ++i) {
  222. val = readl(ddata->regs + PWM_SIFIVE_PWMCMP(i));
  223. if (val > 0)
  224. ++enabled_pwms;
  225. }
  226. }
  227. /* The clk should be on once for each running PWM. */
  228. if (enabled_pwms) {
  229. while (enabled_clks < enabled_pwms) {
  230. /* This is not expected to fail as the clk is already on */
  231. ret = clk_enable(ddata->clk);
  232. if (unlikely(ret)) {
  233. dev_err_probe(dev, ret, "Failed to enable clk\n");
  234. goto disable_clk;
  235. }
  236. ++enabled_clks;
  237. }
  238. } else {
  239. clk_disable(ddata->clk);
  240. enabled_clks = 0;
  241. }
  242. /* Watch for changes to underlying clock frequency */
  243. ddata->notifier.notifier_call = pwm_sifive_clock_notifier;
  244. ret = clk_notifier_register(ddata->clk, &ddata->notifier);
  245. if (ret) {
  246. dev_err(dev, "failed to register clock notifier: %d\n", ret);
  247. goto disable_clk;
  248. }
  249. ret = pwmchip_add(chip);
  250. if (ret < 0) {
  251. dev_err(dev, "cannot register PWM: %d\n", ret);
  252. goto unregister_clk;
  253. }
  254. platform_set_drvdata(pdev, ddata);
  255. dev_dbg(dev, "SiFive PWM chip registered %d PWMs\n", chip->npwm);
  256. return 0;
  257. unregister_clk:
  258. clk_notifier_unregister(ddata->clk, &ddata->notifier);
  259. disable_clk:
  260. while (enabled_clks) {
  261. clk_disable(ddata->clk);
  262. --enabled_clks;
  263. }
  264. clk_unprepare(ddata->clk);
  265. return ret;
  266. }
  267. static int pwm_sifive_remove(struct platform_device *dev)
  268. {
  269. struct pwm_sifive_ddata *ddata = platform_get_drvdata(dev);
  270. struct pwm_device *pwm;
  271. int ch;
  272. pwmchip_remove(&ddata->chip);
  273. clk_notifier_unregister(ddata->clk, &ddata->notifier);
  274. for (ch = 0; ch < ddata->chip.npwm; ch++) {
  275. pwm = &ddata->chip.pwms[ch];
  276. if (pwm->state.enabled)
  277. clk_disable(ddata->clk);
  278. }
  279. clk_unprepare(ddata->clk);
  280. return 0;
  281. }
  282. static const struct of_device_id pwm_sifive_of_match[] = {
  283. { .compatible = "sifive,pwm0" },
  284. {},
  285. };
  286. MODULE_DEVICE_TABLE(of, pwm_sifive_of_match);
  287. static struct platform_driver pwm_sifive_driver = {
  288. .probe = pwm_sifive_probe,
  289. .remove = pwm_sifive_remove,
  290. .driver = {
  291. .name = "pwm-sifive",
  292. .of_match_table = pwm_sifive_of_match,
  293. },
  294. };
  295. module_platform_driver(pwm_sifive_driver);
  296. MODULE_DESCRIPTION("SiFive PWM driver");
  297. MODULE_LICENSE("GPL v2");