pwm-bcm-kona.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. // Copyright (C) 2014 Broadcom Corporation
  3. #include <linux/clk.h>
  4. #include <linux/delay.h>
  5. #include <linux/err.h>
  6. #include <linux/io.h>
  7. #include <linux/ioport.h>
  8. #include <linux/math64.h>
  9. #include <linux/module.h>
  10. #include <linux/of.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/pwm.h>
  13. #include <linux/slab.h>
  14. #include <linux/types.h>
  15. /*
  16. * The Kona PWM has some unusual characteristics. Here are the main points.
  17. *
  18. * 1) There is no disable bit and the hardware docs advise programming a zero
  19. * duty to achieve output equivalent to that of a normal disable operation.
  20. *
  21. * 2) Changes to prescale, duty, period, and polarity do not take effect until
  22. * a subsequent rising edge of the trigger bit.
  23. *
  24. * 3) If the smooth bit and trigger bit are both low, the output is a constant
  25. * high signal. Otherwise, the earlier waveform continues to be output.
  26. *
  27. * 4) If the smooth bit is set on the rising edge of the trigger bit, output
  28. * will transition to the new settings on a period boundary (which could be
  29. * seconds away). If the smooth bit is clear, new settings will be applied
  30. * as soon as possible (the hardware always has a 400ns delay).
  31. *
  32. * 5) When the external clock that feeds the PWM is disabled, output is pegged
  33. * high or low depending on its state at that exact instant.
  34. */
  35. #define PWM_CONTROL_OFFSET 0x00000000
  36. #define PWM_CONTROL_SMOOTH_SHIFT(chan) (24 + (chan))
  37. #define PWM_CONTROL_TYPE_SHIFT(chan) (16 + (chan))
  38. #define PWM_CONTROL_POLARITY_SHIFT(chan) (8 + (chan))
  39. #define PWM_CONTROL_TRIGGER_SHIFT(chan) (chan)
  40. #define PRESCALE_OFFSET 0x00000004
  41. #define PRESCALE_SHIFT(chan) ((chan) << 2)
  42. #define PRESCALE_MASK(chan) (0x7 << PRESCALE_SHIFT(chan))
  43. #define PRESCALE_MIN 0x00000000
  44. #define PRESCALE_MAX 0x00000007
  45. #define PERIOD_COUNT_OFFSET(chan) (0x00000008 + ((chan) << 3))
  46. #define PERIOD_COUNT_MIN 0x00000002
  47. #define PERIOD_COUNT_MAX 0x00ffffff
  48. #define DUTY_CYCLE_HIGH_OFFSET(chan) (0x0000000c + ((chan) << 3))
  49. #define DUTY_CYCLE_HIGH_MIN 0x00000000
  50. #define DUTY_CYCLE_HIGH_MAX 0x00ffffff
  51. struct kona_pwmc {
  52. void __iomem *base;
  53. struct clk *clk;
  54. };
  55. static inline struct kona_pwmc *to_kona_pwmc(struct pwm_chip *chip)
  56. {
  57. return pwmchip_get_drvdata(chip);
  58. }
  59. /*
  60. * Clear trigger bit but set smooth bit to maintain old output.
  61. */
  62. static void kona_pwmc_prepare_for_settings(struct kona_pwmc *kp,
  63. unsigned int chan)
  64. {
  65. unsigned int value = readl(kp->base + PWM_CONTROL_OFFSET);
  66. value |= 1 << PWM_CONTROL_SMOOTH_SHIFT(chan);
  67. value &= ~(1 << PWM_CONTROL_TRIGGER_SHIFT(chan));
  68. writel(value, kp->base + PWM_CONTROL_OFFSET);
  69. /*
  70. * There must be a min 400ns delay between clearing trigger and setting
  71. * it. Failing to do this may result in no PWM signal.
  72. */
  73. ndelay(400);
  74. }
  75. static void kona_pwmc_apply_settings(struct kona_pwmc *kp, unsigned int chan)
  76. {
  77. unsigned int value = readl(kp->base + PWM_CONTROL_OFFSET);
  78. /* Set trigger bit and clear smooth bit to apply new settings */
  79. value &= ~(1 << PWM_CONTROL_SMOOTH_SHIFT(chan));
  80. value |= 1 << PWM_CONTROL_TRIGGER_SHIFT(chan);
  81. writel(value, kp->base + PWM_CONTROL_OFFSET);
  82. /* Trigger bit must be held high for at least 400 ns. */
  83. ndelay(400);
  84. }
  85. static int kona_pwmc_config(struct pwm_chip *chip, struct pwm_device *pwm,
  86. u64 duty_ns, u64 period_ns)
  87. {
  88. struct kona_pwmc *kp = to_kona_pwmc(chip);
  89. u64 div, rate;
  90. unsigned long prescale = PRESCALE_MIN, pc, dc;
  91. unsigned int value, chan = pwm->hwpwm;
  92. /*
  93. * Find period count, duty count and prescale to suit duty_ns and
  94. * period_ns. This is done according to formulas described below:
  95. *
  96. * period_ns = 10^9 * (PRESCALE + 1) * PC / PWM_CLK_RATE
  97. * duty_ns = 10^9 * (PRESCALE + 1) * DC / PWM_CLK_RATE
  98. *
  99. * PC = (PWM_CLK_RATE * period_ns) / (10^9 * (PRESCALE + 1))
  100. * DC = (PWM_CLK_RATE * duty_ns) / (10^9 * (PRESCALE + 1))
  101. */
  102. rate = clk_get_rate(kp->clk);
  103. while (1) {
  104. div = 1000000000;
  105. div *= 1 + prescale;
  106. pc = mul_u64_u64_div_u64(rate, period_ns, div);
  107. dc = mul_u64_u64_div_u64(rate, duty_ns, div);
  108. /* If duty_ns or period_ns are not achievable then return */
  109. if (pc < PERIOD_COUNT_MIN)
  110. return -EINVAL;
  111. /* If pc and dc are in bounds, the calculation is done */
  112. if (pc <= PERIOD_COUNT_MAX && dc <= DUTY_CYCLE_HIGH_MAX)
  113. break;
  114. /* Otherwise, increase prescale and recalculate pc and dc */
  115. if (++prescale > PRESCALE_MAX)
  116. return -EINVAL;
  117. }
  118. kona_pwmc_prepare_for_settings(kp, chan);
  119. value = readl(kp->base + PRESCALE_OFFSET);
  120. value &= ~PRESCALE_MASK(chan);
  121. value |= prescale << PRESCALE_SHIFT(chan);
  122. writel(value, kp->base + PRESCALE_OFFSET);
  123. writel(pc, kp->base + PERIOD_COUNT_OFFSET(chan));
  124. writel(dc, kp->base + DUTY_CYCLE_HIGH_OFFSET(chan));
  125. kona_pwmc_apply_settings(kp, chan);
  126. return 0;
  127. }
  128. static int kona_pwmc_set_polarity(struct pwm_chip *chip, struct pwm_device *pwm,
  129. enum pwm_polarity polarity)
  130. {
  131. struct kona_pwmc *kp = to_kona_pwmc(chip);
  132. unsigned int chan = pwm->hwpwm;
  133. unsigned int value;
  134. int ret;
  135. ret = clk_prepare_enable(kp->clk);
  136. if (ret < 0) {
  137. dev_err(pwmchip_parent(chip), "failed to enable clock: %d\n", ret);
  138. return ret;
  139. }
  140. kona_pwmc_prepare_for_settings(kp, chan);
  141. value = readl(kp->base + PWM_CONTROL_OFFSET);
  142. if (polarity == PWM_POLARITY_NORMAL)
  143. value |= 1 << PWM_CONTROL_POLARITY_SHIFT(chan);
  144. else
  145. value &= ~(1 << PWM_CONTROL_POLARITY_SHIFT(chan));
  146. writel(value, kp->base + PWM_CONTROL_OFFSET);
  147. kona_pwmc_apply_settings(kp, chan);
  148. clk_disable_unprepare(kp->clk);
  149. return 0;
  150. }
  151. static int kona_pwmc_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  152. {
  153. struct kona_pwmc *kp = to_kona_pwmc(chip);
  154. int ret;
  155. ret = clk_prepare_enable(kp->clk);
  156. if (ret < 0) {
  157. dev_err(pwmchip_parent(chip), "failed to enable clock: %d\n", ret);
  158. return ret;
  159. }
  160. return 0;
  161. }
  162. static void kona_pwmc_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  163. {
  164. struct kona_pwmc *kp = to_kona_pwmc(chip);
  165. unsigned int chan = pwm->hwpwm;
  166. unsigned int value;
  167. kona_pwmc_prepare_for_settings(kp, chan);
  168. /* Simulate a disable by configuring for zero duty */
  169. writel(0, kp->base + DUTY_CYCLE_HIGH_OFFSET(chan));
  170. writel(0, kp->base + PERIOD_COUNT_OFFSET(chan));
  171. /* Set prescale to 0 for this channel */
  172. value = readl(kp->base + PRESCALE_OFFSET);
  173. value &= ~PRESCALE_MASK(chan);
  174. writel(value, kp->base + PRESCALE_OFFSET);
  175. kona_pwmc_apply_settings(kp, chan);
  176. clk_disable_unprepare(kp->clk);
  177. }
  178. static int kona_pwmc_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  179. const struct pwm_state *state)
  180. {
  181. int err;
  182. struct kona_pwmc *kp = to_kona_pwmc(chip);
  183. bool enabled = pwm->state.enabled;
  184. if (state->polarity != pwm->state.polarity) {
  185. if (enabled) {
  186. kona_pwmc_disable(chip, pwm);
  187. enabled = false;
  188. }
  189. err = kona_pwmc_set_polarity(chip, pwm, state->polarity);
  190. if (err)
  191. return err;
  192. pwm->state.polarity = state->polarity;
  193. }
  194. if (!state->enabled) {
  195. if (enabled)
  196. kona_pwmc_disable(chip, pwm);
  197. return 0;
  198. } else if (!enabled) {
  199. /*
  200. * This is a bit special here, usually the PWM should only be
  201. * enabled when duty and period are setup. But before this
  202. * driver was converted to .apply it was done the other way
  203. * around and so this behaviour was kept even though this might
  204. * result in a glitch. This might be improvable by someone with
  205. * hardware and/or documentation.
  206. */
  207. err = kona_pwmc_enable(chip, pwm);
  208. if (err)
  209. return err;
  210. }
  211. err = kona_pwmc_config(chip, pwm, state->duty_cycle, state->period);
  212. if (err && !pwm->state.enabled)
  213. clk_disable_unprepare(kp->clk);
  214. return err;
  215. }
  216. static const struct pwm_ops kona_pwm_ops = {
  217. .apply = kona_pwmc_apply,
  218. };
  219. static int kona_pwmc_probe(struct platform_device *pdev)
  220. {
  221. struct pwm_chip *chip;
  222. struct kona_pwmc *kp;
  223. unsigned int chan;
  224. unsigned int value = 0;
  225. int ret = 0;
  226. chip = devm_pwmchip_alloc(&pdev->dev, 6, sizeof(*kp));
  227. if (IS_ERR(chip))
  228. return PTR_ERR(chip);
  229. kp = to_kona_pwmc(chip);
  230. chip->ops = &kona_pwm_ops;
  231. kp->base = devm_platform_ioremap_resource(pdev, 0);
  232. if (IS_ERR(kp->base))
  233. return PTR_ERR(kp->base);
  234. kp->clk = devm_clk_get(&pdev->dev, NULL);
  235. if (IS_ERR(kp->clk)) {
  236. dev_err(&pdev->dev, "failed to get clock: %ld\n",
  237. PTR_ERR(kp->clk));
  238. return PTR_ERR(kp->clk);
  239. }
  240. ret = clk_prepare_enable(kp->clk);
  241. if (ret < 0) {
  242. dev_err(&pdev->dev, "failed to enable clock: %d\n", ret);
  243. return ret;
  244. }
  245. /* Set push/pull for all channels */
  246. for (chan = 0; chan < chip->npwm; chan++)
  247. value |= (1 << PWM_CONTROL_TYPE_SHIFT(chan));
  248. writel(value, kp->base + PWM_CONTROL_OFFSET);
  249. clk_disable_unprepare(kp->clk);
  250. ret = devm_pwmchip_add(&pdev->dev, chip);
  251. if (ret < 0)
  252. dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret);
  253. return ret;
  254. }
  255. static const struct of_device_id bcm_kona_pwmc_dt[] = {
  256. { .compatible = "brcm,kona-pwm" },
  257. { },
  258. };
  259. MODULE_DEVICE_TABLE(of, bcm_kona_pwmc_dt);
  260. static struct platform_driver kona_pwmc_driver = {
  261. .driver = {
  262. .name = "bcm-kona-pwm",
  263. .of_match_table = bcm_kona_pwmc_dt,
  264. },
  265. .probe = kona_pwmc_probe,
  266. };
  267. module_platform_driver(kona_pwmc_driver);
  268. MODULE_AUTHOR("Broadcom Corporation <bcm-kernel-feedback-list@broadcom.com>");
  269. MODULE_AUTHOR("Tim Kryger <tkryger@broadcom.com>");
  270. MODULE_DESCRIPTION("Broadcom Kona PWM driver");
  271. MODULE_LICENSE("GPL v2");