pwm-bcm-kona.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * Copyright (C) 2014 Broadcom Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation version 2.
  7. *
  8. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  9. * kind, whether express or implied; without even the implied warranty
  10. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/delay.h>
  15. #include <linux/err.h>
  16. #include <linux/io.h>
  17. #include <linux/ioport.h>
  18. #include <linux/math64.h>
  19. #include <linux/module.h>
  20. #include <linux/of.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/pwm.h>
  23. #include <linux/slab.h>
  24. #include <linux/types.h>
  25. /*
  26. * The Kona PWM has some unusual characteristics. Here are the main points.
  27. *
  28. * 1) There is no disable bit and the hardware docs advise programming a zero
  29. * duty to achieve output equivalent to that of a normal disable operation.
  30. *
  31. * 2) Changes to prescale, duty, period, and polarity do not take effect until
  32. * a subsequent rising edge of the trigger bit.
  33. *
  34. * 3) If the smooth bit and trigger bit are both low, the output is a constant
  35. * high signal. Otherwise, the earlier waveform continues to be output.
  36. *
  37. * 4) If the smooth bit is set on the rising edge of the trigger bit, output
  38. * will transition to the new settings on a period boundary (which could be
  39. * seconds away). If the smooth bit is clear, new settings will be applied
  40. * as soon as possible (the hardware always has a 400ns delay).
  41. *
  42. * 5) When the external clock that feeds the PWM is disabled, output is pegged
  43. * high or low depending on its state at that exact instant.
  44. */
  45. #define PWM_CONTROL_OFFSET (0x00000000)
  46. #define PWM_CONTROL_SMOOTH_SHIFT(chan) (24 + (chan))
  47. #define PWM_CONTROL_TYPE_SHIFT(chan) (16 + (chan))
  48. #define PWM_CONTROL_POLARITY_SHIFT(chan) (8 + (chan))
  49. #define PWM_CONTROL_TRIGGER_SHIFT(chan) (chan)
  50. #define PRESCALE_OFFSET (0x00000004)
  51. #define PRESCALE_SHIFT(chan) ((chan) << 2)
  52. #define PRESCALE_MASK(chan) (0x7 << PRESCALE_SHIFT(chan))
  53. #define PRESCALE_MIN (0x00000000)
  54. #define PRESCALE_MAX (0x00000007)
  55. #define PERIOD_COUNT_OFFSET(chan) (0x00000008 + ((chan) << 3))
  56. #define PERIOD_COUNT_MIN (0x00000002)
  57. #define PERIOD_COUNT_MAX (0x00ffffff)
  58. #define DUTY_CYCLE_HIGH_OFFSET(chan) (0x0000000c + ((chan) << 3))
  59. #define DUTY_CYCLE_HIGH_MIN (0x00000000)
  60. #define DUTY_CYCLE_HIGH_MAX (0x00ffffff)
  61. struct kona_pwmc {
  62. struct pwm_chip chip;
  63. void __iomem *base;
  64. struct clk *clk;
  65. };
  66. static inline struct kona_pwmc *to_kona_pwmc(struct pwm_chip *_chip)
  67. {
  68. return container_of(_chip, struct kona_pwmc, chip);
  69. }
  70. static void kona_pwmc_apply_settings(struct kona_pwmc *kp, unsigned int chan)
  71. {
  72. unsigned int value = readl(kp->base + PWM_CONTROL_OFFSET);
  73. /* Clear trigger bit but set smooth bit to maintain old output */
  74. value |= 1 << PWM_CONTROL_SMOOTH_SHIFT(chan);
  75. value &= ~(1 << PWM_CONTROL_TRIGGER_SHIFT(chan));
  76. writel(value, kp->base + PWM_CONTROL_OFFSET);
  77. /* Set trigger bit and clear smooth bit to apply new settings */
  78. value &= ~(1 << PWM_CONTROL_SMOOTH_SHIFT(chan));
  79. value |= 1 << PWM_CONTROL_TRIGGER_SHIFT(chan);
  80. writel(value, kp->base + PWM_CONTROL_OFFSET);
  81. }
  82. static int kona_pwmc_config(struct pwm_chip *chip, struct pwm_device *pwm,
  83. int duty_ns, int period_ns)
  84. {
  85. struct kona_pwmc *kp = to_kona_pwmc(chip);
  86. u64 val, div, rate;
  87. unsigned long prescale = PRESCALE_MIN, pc, dc;
  88. unsigned int value, chan = pwm->hwpwm;
  89. /*
  90. * Find period count, duty count and prescale to suit duty_ns and
  91. * period_ns. This is done according to formulas described below:
  92. *
  93. * period_ns = 10^9 * (PRESCALE + 1) * PC / PWM_CLK_RATE
  94. * duty_ns = 10^9 * (PRESCALE + 1) * DC / PWM_CLK_RATE
  95. *
  96. * PC = (PWM_CLK_RATE * period_ns) / (10^9 * (PRESCALE + 1))
  97. * DC = (PWM_CLK_RATE * duty_ns) / (10^9 * (PRESCALE + 1))
  98. */
  99. rate = clk_get_rate(kp->clk);
  100. while (1) {
  101. div = 1000000000;
  102. div *= 1 + prescale;
  103. val = rate * period_ns;
  104. pc = div64_u64(val, div);
  105. val = rate * duty_ns;
  106. dc = div64_u64(val, div);
  107. /* If duty_ns or period_ns are not achievable then return */
  108. if (pc < PERIOD_COUNT_MIN || dc < DUTY_CYCLE_HIGH_MIN)
  109. return -EINVAL;
  110. /* If pc and dc are in bounds, the calculation is done */
  111. if (pc <= PERIOD_COUNT_MAX && dc <= DUTY_CYCLE_HIGH_MAX)
  112. break;
  113. /* Otherwise, increase prescale and recalculate pc and dc */
  114. if (++prescale > PRESCALE_MAX)
  115. return -EINVAL;
  116. }
  117. /* If the PWM channel is enabled, write the settings to the HW */
  118. if (test_bit(PWMF_ENABLED, &pwm->flags)) {
  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. }
  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. 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. /* Wait for waveform to settle before gating off the clock */
  149. ndelay(400);
  150. clk_disable_unprepare(kp->clk);
  151. return 0;
  152. }
  153. static int kona_pwmc_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  154. {
  155. struct kona_pwmc *kp = to_kona_pwmc(chip);
  156. int ret;
  157. ret = clk_prepare_enable(kp->clk);
  158. if (ret < 0) {
  159. dev_err(chip->dev, "failed to enable clock: %d\n", ret);
  160. return ret;
  161. }
  162. ret = kona_pwmc_config(chip, pwm, pwm->duty_cycle, pwm->period);
  163. if (ret < 0) {
  164. clk_disable_unprepare(kp->clk);
  165. return ret;
  166. }
  167. return 0;
  168. }
  169. static void kona_pwmc_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  170. {
  171. struct kona_pwmc *kp = to_kona_pwmc(chip);
  172. unsigned int chan = pwm->hwpwm;
  173. /* Simulate a disable by configuring for zero duty */
  174. writel(0, kp->base + DUTY_CYCLE_HIGH_OFFSET(chan));
  175. kona_pwmc_apply_settings(kp, chan);
  176. /* Wait for waveform to settle before gating off the clock */
  177. ndelay(400);
  178. clk_disable_unprepare(kp->clk);
  179. }
  180. static const struct pwm_ops kona_pwm_ops = {
  181. .config = kona_pwmc_config,
  182. .set_polarity = kona_pwmc_set_polarity,
  183. .enable = kona_pwmc_enable,
  184. .disable = kona_pwmc_disable,
  185. .owner = THIS_MODULE,
  186. };
  187. static int kona_pwmc_probe(struct platform_device *pdev)
  188. {
  189. struct kona_pwmc *kp;
  190. struct resource *res;
  191. unsigned int chan;
  192. unsigned int value = 0;
  193. int ret = 0;
  194. kp = devm_kzalloc(&pdev->dev, sizeof(*kp), GFP_KERNEL);
  195. if (kp == NULL)
  196. return -ENOMEM;
  197. platform_set_drvdata(pdev, kp);
  198. kp->chip.dev = &pdev->dev;
  199. kp->chip.ops = &kona_pwm_ops;
  200. kp->chip.base = -1;
  201. kp->chip.npwm = 6;
  202. kp->chip.of_xlate = of_pwm_xlate_with_flags;
  203. kp->chip.of_pwm_n_cells = 3;
  204. kp->chip.can_sleep = true;
  205. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  206. kp->base = devm_ioremap_resource(&pdev->dev, res);
  207. if (IS_ERR(kp->base))
  208. return PTR_ERR(kp->base);
  209. kp->clk = devm_clk_get(&pdev->dev, NULL);
  210. if (IS_ERR(kp->clk)) {
  211. dev_err(&pdev->dev, "failed to get clock: %ld\n",
  212. PTR_ERR(kp->clk));
  213. return PTR_ERR(kp->clk);
  214. }
  215. ret = clk_prepare_enable(kp->clk);
  216. if (ret < 0) {
  217. dev_err(&pdev->dev, "failed to enable clock: %d\n", ret);
  218. return ret;
  219. }
  220. /* Set push/pull for all channels */
  221. for (chan = 0; chan < kp->chip.npwm; chan++)
  222. value |= (1 << PWM_CONTROL_TYPE_SHIFT(chan));
  223. writel(value, kp->base + PWM_CONTROL_OFFSET);
  224. clk_disable_unprepare(kp->clk);
  225. ret = pwmchip_add_with_polarity(&kp->chip, PWM_POLARITY_INVERSED);
  226. if (ret < 0)
  227. dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret);
  228. return ret;
  229. }
  230. static int kona_pwmc_remove(struct platform_device *pdev)
  231. {
  232. struct kona_pwmc *kp = platform_get_drvdata(pdev);
  233. unsigned int chan;
  234. for (chan = 0; chan < kp->chip.npwm; chan++)
  235. if (test_bit(PWMF_ENABLED, &kp->chip.pwms[chan].flags))
  236. clk_disable_unprepare(kp->clk);
  237. return pwmchip_remove(&kp->chip);
  238. }
  239. static const struct of_device_id bcm_kona_pwmc_dt[] = {
  240. { .compatible = "brcm,kona-pwm" },
  241. { },
  242. };
  243. MODULE_DEVICE_TABLE(of, bcm_kona_pwmc_dt);
  244. static struct platform_driver kona_pwmc_driver = {
  245. .driver = {
  246. .name = "bcm-kona-pwm",
  247. .of_match_table = bcm_kona_pwmc_dt,
  248. },
  249. .probe = kona_pwmc_probe,
  250. .remove = kona_pwmc_remove,
  251. };
  252. module_platform_driver(kona_pwmc_driver);
  253. MODULE_AUTHOR("Broadcom Corporation <bcm-kernel-feedback-list@broadcom.com>");
  254. MODULE_AUTHOR("Tim Kryger <tkryger@broadcom.com>");
  255. MODULE_DESCRIPTION("Broadcom Kona PWM driver");
  256. MODULE_LICENSE("GPL v2");