pwm-bcm-kona.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. struct pwm_chip chip;
  53. void __iomem *base;
  54. struct clk *clk;
  55. };
  56. static inline struct kona_pwmc *to_kona_pwmc(struct pwm_chip *_chip)
  57. {
  58. return container_of(_chip, struct kona_pwmc, chip);
  59. }
  60. /*
  61. * Clear trigger bit but set smooth bit to maintain old output.
  62. */
  63. static void kona_pwmc_prepare_for_settings(struct kona_pwmc *kp,
  64. unsigned int chan)
  65. {
  66. unsigned int value = readl(kp->base + PWM_CONTROL_OFFSET);
  67. value |= 1 << PWM_CONTROL_SMOOTH_SHIFT(chan);
  68. value &= ~(1 << PWM_CONTROL_TRIGGER_SHIFT(chan));
  69. writel(value, kp->base + PWM_CONTROL_OFFSET);
  70. /*
  71. * There must be a min 400ns delay between clearing trigger and setting
  72. * it. Failing to do this may result in no PWM signal.
  73. */
  74. ndelay(400);
  75. }
  76. static void kona_pwmc_apply_settings(struct kona_pwmc *kp, unsigned int chan)
  77. {
  78. unsigned int value = readl(kp->base + PWM_CONTROL_OFFSET);
  79. /* Set trigger bit and clear smooth bit to apply new settings */
  80. value &= ~(1 << PWM_CONTROL_SMOOTH_SHIFT(chan));
  81. value |= 1 << PWM_CONTROL_TRIGGER_SHIFT(chan);
  82. writel(value, kp->base + PWM_CONTROL_OFFSET);
  83. /* Trigger bit must be held high for at least 400 ns. */
  84. ndelay(400);
  85. }
  86. static int kona_pwmc_config(struct pwm_chip *chip, struct pwm_device *pwm,
  87. u64 duty_ns, u64 period_ns)
  88. {
  89. struct kona_pwmc *kp = to_kona_pwmc(chip);
  90. u64 div, rate;
  91. unsigned long prescale = PRESCALE_MIN, pc, dc;
  92. unsigned int value, chan = pwm->hwpwm;
  93. /*
  94. * Find period count, duty count and prescale to suit duty_ns and
  95. * period_ns. This is done according to formulas described below:
  96. *
  97. * period_ns = 10^9 * (PRESCALE + 1) * PC / PWM_CLK_RATE
  98. * duty_ns = 10^9 * (PRESCALE + 1) * DC / PWM_CLK_RATE
  99. *
  100. * PC = (PWM_CLK_RATE * period_ns) / (10^9 * (PRESCALE + 1))
  101. * DC = (PWM_CLK_RATE * duty_ns) / (10^9 * (PRESCALE + 1))
  102. */
  103. rate = clk_get_rate(kp->clk);
  104. while (1) {
  105. div = 1000000000;
  106. div *= 1 + prescale;
  107. pc = mul_u64_u64_div_u64(rate, period_ns, div);
  108. dc = mul_u64_u64_div_u64(rate, duty_ns, div);
  109. /* If duty_ns or period_ns are not achievable then return */
  110. if (pc < PERIOD_COUNT_MIN)
  111. return -EINVAL;
  112. /* If pc and dc are in bounds, the calculation is done */
  113. if (pc <= PERIOD_COUNT_MAX && dc <= DUTY_CYCLE_HIGH_MAX)
  114. break;
  115. /* Otherwise, increase prescale and recalculate pc and dc */
  116. if (++prescale > PRESCALE_MAX)
  117. return -EINVAL;
  118. }
  119. kona_pwmc_prepare_for_settings(kp, chan);
  120. value = readl(kp->base + PRESCALE_OFFSET);
  121. value &= ~PRESCALE_MASK(chan);
  122. value |= prescale << PRESCALE_SHIFT(chan);
  123. writel(value, kp->base + PRESCALE_OFFSET);
  124. writel(pc, kp->base + PERIOD_COUNT_OFFSET(chan));
  125. writel(dc, kp->base + DUTY_CYCLE_HIGH_OFFSET(chan));
  126. kona_pwmc_apply_settings(kp, chan);
  127. return 0;
  128. }
  129. static int kona_pwmc_set_polarity(struct pwm_chip *chip, struct pwm_device *pwm,
  130. enum pwm_polarity polarity)
  131. {
  132. struct kona_pwmc *kp = to_kona_pwmc(chip);
  133. unsigned int chan = pwm->hwpwm;
  134. unsigned int value;
  135. int ret;
  136. ret = clk_prepare_enable(kp->clk);
  137. if (ret < 0) {
  138. dev_err(chip->dev, "failed to enable clock: %d\n", ret);
  139. return ret;
  140. }
  141. kona_pwmc_prepare_for_settings(kp, chan);
  142. value = readl(kp->base + PWM_CONTROL_OFFSET);
  143. if (polarity == PWM_POLARITY_NORMAL)
  144. value |= 1 << PWM_CONTROL_POLARITY_SHIFT(chan);
  145. else
  146. value &= ~(1 << PWM_CONTROL_POLARITY_SHIFT(chan));
  147. writel(value, kp->base + PWM_CONTROL_OFFSET);
  148. kona_pwmc_apply_settings(kp, chan);
  149. clk_disable_unprepare(kp->clk);
  150. return 0;
  151. }
  152. static int kona_pwmc_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  153. {
  154. struct kona_pwmc *kp = to_kona_pwmc(chip);
  155. int ret;
  156. ret = clk_prepare_enable(kp->clk);
  157. if (ret < 0) {
  158. dev_err(chip->dev, "failed to enable clock: %d\n", ret);
  159. return ret;
  160. }
  161. return 0;
  162. }
  163. static void kona_pwmc_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  164. {
  165. struct kona_pwmc *kp = to_kona_pwmc(chip);
  166. unsigned int chan = pwm->hwpwm;
  167. unsigned int value;
  168. kona_pwmc_prepare_for_settings(kp, chan);
  169. /* Simulate a disable by configuring for zero duty */
  170. writel(0, kp->base + DUTY_CYCLE_HIGH_OFFSET(chan));
  171. writel(0, kp->base + PERIOD_COUNT_OFFSET(chan));
  172. /* Set prescale to 0 for this channel */
  173. value = readl(kp->base + PRESCALE_OFFSET);
  174. value &= ~PRESCALE_MASK(chan);
  175. writel(value, kp->base + PRESCALE_OFFSET);
  176. kona_pwmc_apply_settings(kp, chan);
  177. clk_disable_unprepare(kp->clk);
  178. }
  179. static int kona_pwmc_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  180. const struct pwm_state *state)
  181. {
  182. int err;
  183. struct kona_pwmc *kp = to_kona_pwmc(chip);
  184. bool enabled = pwm->state.enabled;
  185. if (state->polarity != pwm->state.polarity) {
  186. if (enabled) {
  187. kona_pwmc_disable(chip, pwm);
  188. enabled = false;
  189. }
  190. err = kona_pwmc_set_polarity(chip, pwm, state->polarity);
  191. if (err)
  192. return err;
  193. pwm->state.polarity = state->polarity;
  194. }
  195. if (!state->enabled) {
  196. if (enabled)
  197. kona_pwmc_disable(chip, pwm);
  198. return 0;
  199. } else if (!enabled) {
  200. /*
  201. * This is a bit special here, usually the PWM should only be
  202. * enabled when duty and period are setup. But before this
  203. * driver was converted to .apply it was done the other way
  204. * around and so this behaviour was kept even though this might
  205. * result in a glitch. This might be improvable by someone with
  206. * hardware and/or documentation.
  207. */
  208. err = kona_pwmc_enable(chip, pwm);
  209. if (err)
  210. return err;
  211. }
  212. err = kona_pwmc_config(pwm->chip, pwm, state->duty_cycle, state->period);
  213. if (err && !pwm->state.enabled)
  214. clk_disable_unprepare(kp->clk);
  215. return err;
  216. }
  217. static const struct pwm_ops kona_pwm_ops = {
  218. .apply = kona_pwmc_apply,
  219. .owner = THIS_MODULE,
  220. };
  221. static int kona_pwmc_probe(struct platform_device *pdev)
  222. {
  223. struct kona_pwmc *kp;
  224. unsigned int chan;
  225. unsigned int value = 0;
  226. int ret = 0;
  227. kp = devm_kzalloc(&pdev->dev, sizeof(*kp), GFP_KERNEL);
  228. if (kp == NULL)
  229. return -ENOMEM;
  230. kp->chip.dev = &pdev->dev;
  231. kp->chip.ops = &kona_pwm_ops;
  232. kp->chip.npwm = 6;
  233. kp->base = devm_platform_ioremap_resource(pdev, 0);
  234. if (IS_ERR(kp->base))
  235. return PTR_ERR(kp->base);
  236. kp->clk = devm_clk_get(&pdev->dev, NULL);
  237. if (IS_ERR(kp->clk)) {
  238. dev_err(&pdev->dev, "failed to get clock: %ld\n",
  239. PTR_ERR(kp->clk));
  240. return PTR_ERR(kp->clk);
  241. }
  242. ret = clk_prepare_enable(kp->clk);
  243. if (ret < 0) {
  244. dev_err(&pdev->dev, "failed to enable clock: %d\n", ret);
  245. return ret;
  246. }
  247. /* Set push/pull for all channels */
  248. for (chan = 0; chan < kp->chip.npwm; chan++)
  249. value |= (1 << PWM_CONTROL_TYPE_SHIFT(chan));
  250. writel(value, kp->base + PWM_CONTROL_OFFSET);
  251. clk_disable_unprepare(kp->clk);
  252. ret = devm_pwmchip_add(&pdev->dev, &kp->chip);
  253. if (ret < 0)
  254. dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret);
  255. return ret;
  256. }
  257. static const struct of_device_id bcm_kona_pwmc_dt[] = {
  258. { .compatible = "brcm,kona-pwm" },
  259. { },
  260. };
  261. MODULE_DEVICE_TABLE(of, bcm_kona_pwmc_dt);
  262. static struct platform_driver kona_pwmc_driver = {
  263. .driver = {
  264. .name = "bcm-kona-pwm",
  265. .of_match_table = bcm_kona_pwmc_dt,
  266. },
  267. .probe = kona_pwmc_probe,
  268. };
  269. module_platform_driver(kona_pwmc_driver);
  270. MODULE_AUTHOR("Broadcom Corporation <[email protected]>");
  271. MODULE_AUTHOR("Tim Kryger <[email protected]>");
  272. MODULE_DESCRIPTION("Broadcom Kona PWM driver");
  273. MODULE_LICENSE("GPL v2");