pwm-brcmstb.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Broadcom BCM7038 PWM driver
  4. * Author: Florian Fainelli
  5. *
  6. * Copyright (C) 2015 Broadcom Corporation
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/clk.h>
  10. #include <linux/export.h>
  11. #include <linux/init.h>
  12. #include <linux/io.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/pwm.h>
  18. #include <linux/spinlock.h>
  19. #define PWM_CTRL 0x00
  20. #define CTRL_START BIT(0)
  21. #define CTRL_OEB BIT(1)
  22. #define CTRL_FORCE_HIGH BIT(2)
  23. #define CTRL_OPENDRAIN BIT(3)
  24. #define CTRL_CHAN_OFFS 4
  25. #define PWM_CTRL2 0x04
  26. #define CTRL2_OUT_SELECT BIT(0)
  27. #define PWM_CH_SIZE 0x8
  28. #define PWM_CWORD_MSB(ch) (0x08 + ((ch) * PWM_CH_SIZE))
  29. #define PWM_CWORD_LSB(ch) (0x0c + ((ch) * PWM_CH_SIZE))
  30. /* Number of bits for the CWORD value */
  31. #define CWORD_BIT_SIZE 16
  32. /*
  33. * Maximum control word value allowed when variable-frequency PWM is used as a
  34. * clock for the constant-frequency PMW.
  35. */
  36. #define CONST_VAR_F_MAX 32768
  37. #define CONST_VAR_F_MIN 1
  38. #define PWM_ON(ch) (0x18 + ((ch) * PWM_CH_SIZE))
  39. #define PWM_ON_MIN 1
  40. #define PWM_PERIOD(ch) (0x1c + ((ch) * PWM_CH_SIZE))
  41. #define PWM_PERIOD_MIN 0
  42. #define PWM_ON_PERIOD_MAX 0xff
  43. struct brcmstb_pwm {
  44. void __iomem *base;
  45. struct clk *clk;
  46. };
  47. static inline u32 brcmstb_pwm_readl(struct brcmstb_pwm *p,
  48. unsigned int offset)
  49. {
  50. if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
  51. return __raw_readl(p->base + offset);
  52. else
  53. return readl_relaxed(p->base + offset);
  54. }
  55. static inline void brcmstb_pwm_writel(struct brcmstb_pwm *p, u32 value,
  56. unsigned int offset)
  57. {
  58. if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
  59. __raw_writel(value, p->base + offset);
  60. else
  61. writel_relaxed(value, p->base + offset);
  62. }
  63. static inline struct brcmstb_pwm *to_brcmstb_pwm(struct pwm_chip *chip)
  64. {
  65. return pwmchip_get_drvdata(chip);
  66. }
  67. /*
  68. * Fv is derived from the variable frequency output. The variable frequency
  69. * output is configured using this formula:
  70. *
  71. * W = cword, if cword < 2 ^ 15 else 16-bit 2's complement of cword
  72. *
  73. * Fv = W x 2 ^ -16 x 27Mhz (reference clock)
  74. *
  75. * The period is: (period + 1) / Fv and "on" time is on / (period + 1)
  76. *
  77. * The PWM core framework specifies that the "duty_ns" parameter is in fact the
  78. * "on" time, so this translates directly into our HW programming here.
  79. */
  80. static int brcmstb_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  81. u64 duty_ns, u64 period_ns)
  82. {
  83. struct brcmstb_pwm *p = to_brcmstb_pwm(chip);
  84. unsigned long pc, dc, cword = CONST_VAR_F_MAX;
  85. unsigned int channel = pwm->hwpwm;
  86. u32 value;
  87. /*
  88. * If asking for a duty_ns equal to period_ns, we need to substract
  89. * the period value by 1 to make it shorter than the "on" time and
  90. * produce a flat 100% duty cycle signal, and max out the "on" time
  91. */
  92. if (duty_ns == period_ns) {
  93. dc = PWM_ON_PERIOD_MAX;
  94. pc = PWM_ON_PERIOD_MAX - 1;
  95. goto done;
  96. }
  97. while (1) {
  98. u64 rate;
  99. /*
  100. * Calculate the base rate from base frequency and current
  101. * cword
  102. */
  103. rate = (u64)clk_get_rate(p->clk) * (u64)cword;
  104. rate >>= CWORD_BIT_SIZE;
  105. pc = mul_u64_u64_div_u64(period_ns, rate, NSEC_PER_SEC);
  106. dc = mul_u64_u64_div_u64(duty_ns + 1, rate, NSEC_PER_SEC);
  107. /*
  108. * We can be called with separate duty and period updates,
  109. * so do not reject dc == 0 right away
  110. */
  111. if (pc == PWM_PERIOD_MIN || (dc < PWM_ON_MIN && duty_ns))
  112. return -EINVAL;
  113. /* We converged on a calculation */
  114. if (pc <= PWM_ON_PERIOD_MAX && dc <= PWM_ON_PERIOD_MAX)
  115. break;
  116. /*
  117. * The cword needs to be a power of 2 for the variable
  118. * frequency generator to output a 50% duty cycle variable
  119. * frequency which is used as input clock to the fixed
  120. * frequency generator.
  121. */
  122. cword >>= 1;
  123. /*
  124. * Desired periods are too large, we do not have a divider
  125. * for them
  126. */
  127. if (cword < CONST_VAR_F_MIN)
  128. return -EINVAL;
  129. }
  130. done:
  131. /*
  132. * Configure the defined "cword" value to have the variable frequency
  133. * generator output a base frequency for the constant frequency
  134. * generator to derive from.
  135. */
  136. brcmstb_pwm_writel(p, cword >> 8, PWM_CWORD_MSB(channel));
  137. brcmstb_pwm_writel(p, cword & 0xff, PWM_CWORD_LSB(channel));
  138. /* Select constant frequency signal output */
  139. value = brcmstb_pwm_readl(p, PWM_CTRL2);
  140. value |= CTRL2_OUT_SELECT << (channel * CTRL_CHAN_OFFS);
  141. brcmstb_pwm_writel(p, value, PWM_CTRL2);
  142. /* Configure on and period value */
  143. brcmstb_pwm_writel(p, pc, PWM_PERIOD(channel));
  144. brcmstb_pwm_writel(p, dc, PWM_ON(channel));
  145. return 0;
  146. }
  147. static inline void brcmstb_pwm_enable_set(struct brcmstb_pwm *p,
  148. unsigned int channel, bool enable)
  149. {
  150. unsigned int shift = channel * CTRL_CHAN_OFFS;
  151. u32 value;
  152. value = brcmstb_pwm_readl(p, PWM_CTRL);
  153. if (enable) {
  154. value &= ~(CTRL_OEB << shift);
  155. value |= (CTRL_START | CTRL_OPENDRAIN) << shift;
  156. } else {
  157. value &= ~((CTRL_START | CTRL_OPENDRAIN) << shift);
  158. value |= CTRL_OEB << shift;
  159. }
  160. brcmstb_pwm_writel(p, value, PWM_CTRL);
  161. }
  162. static int brcmstb_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  163. const struct pwm_state *state)
  164. {
  165. struct brcmstb_pwm *p = to_brcmstb_pwm(chip);
  166. int err;
  167. if (state->polarity != PWM_POLARITY_NORMAL)
  168. return -EINVAL;
  169. if (!state->enabled) {
  170. if (pwm->state.enabled)
  171. brcmstb_pwm_enable_set(p, pwm->hwpwm, false);
  172. return 0;
  173. }
  174. err = brcmstb_pwm_config(chip, pwm, state->duty_cycle, state->period);
  175. if (err)
  176. return err;
  177. if (!pwm->state.enabled)
  178. brcmstb_pwm_enable_set(p, pwm->hwpwm, true);
  179. return 0;
  180. }
  181. static const struct pwm_ops brcmstb_pwm_ops = {
  182. .apply = brcmstb_pwm_apply,
  183. };
  184. static const struct of_device_id brcmstb_pwm_of_match[] = {
  185. { .compatible = "brcm,bcm7038-pwm", },
  186. { /* sentinel */ }
  187. };
  188. MODULE_DEVICE_TABLE(of, brcmstb_pwm_of_match);
  189. static int brcmstb_pwm_probe(struct platform_device *pdev)
  190. {
  191. struct pwm_chip *chip;
  192. struct brcmstb_pwm *p;
  193. int ret;
  194. chip = devm_pwmchip_alloc(&pdev->dev, 2, sizeof(*p));
  195. if (IS_ERR(chip))
  196. return PTR_ERR(chip);
  197. p = to_brcmstb_pwm(chip);
  198. p->clk = devm_clk_get_enabled(&pdev->dev, NULL);
  199. if (IS_ERR(p->clk))
  200. return dev_err_probe(&pdev->dev, PTR_ERR(p->clk),
  201. "failed to obtain clock\n");
  202. platform_set_drvdata(pdev, p);
  203. chip->ops = &brcmstb_pwm_ops;
  204. p->base = devm_platform_ioremap_resource(pdev, 0);
  205. if (IS_ERR(p->base))
  206. return PTR_ERR(p->base);
  207. ret = devm_pwmchip_add(&pdev->dev, chip);
  208. if (ret)
  209. return dev_err_probe(&pdev->dev, ret, "failed to add PWM chip\n");
  210. return 0;
  211. }
  212. static int brcmstb_pwm_suspend(struct device *dev)
  213. {
  214. struct brcmstb_pwm *p = dev_get_drvdata(dev);
  215. clk_disable_unprepare(p->clk);
  216. return 0;
  217. }
  218. static int brcmstb_pwm_resume(struct device *dev)
  219. {
  220. struct brcmstb_pwm *p = dev_get_drvdata(dev);
  221. return clk_prepare_enable(p->clk);
  222. }
  223. static DEFINE_SIMPLE_DEV_PM_OPS(brcmstb_pwm_pm_ops, brcmstb_pwm_suspend,
  224. brcmstb_pwm_resume);
  225. static struct platform_driver brcmstb_pwm_driver = {
  226. .probe = brcmstb_pwm_probe,
  227. .driver = {
  228. .name = "pwm-brcmstb",
  229. .of_match_table = brcmstb_pwm_of_match,
  230. .pm = pm_ptr(&brcmstb_pwm_pm_ops),
  231. },
  232. };
  233. module_platform_driver(brcmstb_pwm_driver);
  234. MODULE_AUTHOR("Florian Fainelli <f.fainelli@gmail.com>");
  235. MODULE_DESCRIPTION("Broadcom STB PWM driver");
  236. MODULE_ALIAS("platform:pwm-brcmstb");
  237. MODULE_LICENSE("GPL");