pwm-bcm-iproc.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. // Copyright (C) 2016 Broadcom
  3. #include <linux/clk.h>
  4. #include <linux/delay.h>
  5. #include <linux/err.h>
  6. #include <linux/io.h>
  7. #include <linux/math64.h>
  8. #include <linux/module.h>
  9. #include <linux/of.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/pwm.h>
  12. #define IPROC_PWM_CTRL_OFFSET 0x00
  13. #define IPROC_PWM_CTRL_TYPE_SHIFT(ch) (15 + (ch))
  14. #define IPROC_PWM_CTRL_POLARITY_SHIFT(ch) (8 + (ch))
  15. #define IPROC_PWM_CTRL_EN_SHIFT(ch) (ch)
  16. #define IPROC_PWM_PERIOD_OFFSET(ch) (0x04 + ((ch) << 3))
  17. #define IPROC_PWM_PERIOD_MIN 0x02
  18. #define IPROC_PWM_PERIOD_MAX 0xffff
  19. #define IPROC_PWM_DUTY_CYCLE_OFFSET(ch) (0x08 + ((ch) << 3))
  20. #define IPROC_PWM_DUTY_CYCLE_MIN 0x00
  21. #define IPROC_PWM_DUTY_CYCLE_MAX 0xffff
  22. #define IPROC_PWM_PRESCALE_OFFSET 0x24
  23. #define IPROC_PWM_PRESCALE_BITS 0x06
  24. #define IPROC_PWM_PRESCALE_SHIFT(ch) ((3 - (ch)) * \
  25. IPROC_PWM_PRESCALE_BITS)
  26. #define IPROC_PWM_PRESCALE_MASK(ch) (IPROC_PWM_PRESCALE_MAX << \
  27. IPROC_PWM_PRESCALE_SHIFT(ch))
  28. #define IPROC_PWM_PRESCALE_MIN 0x00
  29. #define IPROC_PWM_PRESCALE_MAX 0x3f
  30. struct iproc_pwmc {
  31. void __iomem *base;
  32. struct clk *clk;
  33. };
  34. static inline struct iproc_pwmc *to_iproc_pwmc(struct pwm_chip *chip)
  35. {
  36. return pwmchip_get_drvdata(chip);
  37. }
  38. static void iproc_pwmc_enable(struct iproc_pwmc *ip, unsigned int channel)
  39. {
  40. u32 value;
  41. value = readl(ip->base + IPROC_PWM_CTRL_OFFSET);
  42. value |= 1 << IPROC_PWM_CTRL_EN_SHIFT(channel);
  43. writel(value, ip->base + IPROC_PWM_CTRL_OFFSET);
  44. /* must be a 400 ns delay between clearing and setting enable bit */
  45. ndelay(400);
  46. }
  47. static void iproc_pwmc_disable(struct iproc_pwmc *ip, unsigned int channel)
  48. {
  49. u32 value;
  50. value = readl(ip->base + IPROC_PWM_CTRL_OFFSET);
  51. value &= ~(1 << IPROC_PWM_CTRL_EN_SHIFT(channel));
  52. writel(value, ip->base + IPROC_PWM_CTRL_OFFSET);
  53. /* must be a 400 ns delay between clearing and setting enable bit */
  54. ndelay(400);
  55. }
  56. static int iproc_pwmc_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
  57. struct pwm_state *state)
  58. {
  59. struct iproc_pwmc *ip = to_iproc_pwmc(chip);
  60. u64 tmp, multi, rate;
  61. u32 value, prescale;
  62. value = readl(ip->base + IPROC_PWM_CTRL_OFFSET);
  63. if (value & BIT(IPROC_PWM_CTRL_EN_SHIFT(pwm->hwpwm)))
  64. state->enabled = true;
  65. else
  66. state->enabled = false;
  67. if (value & BIT(IPROC_PWM_CTRL_POLARITY_SHIFT(pwm->hwpwm)))
  68. state->polarity = PWM_POLARITY_NORMAL;
  69. else
  70. state->polarity = PWM_POLARITY_INVERSED;
  71. rate = clk_get_rate(ip->clk);
  72. if (rate == 0) {
  73. state->period = 0;
  74. state->duty_cycle = 0;
  75. return 0;
  76. }
  77. value = readl(ip->base + IPROC_PWM_PRESCALE_OFFSET);
  78. prescale = value >> IPROC_PWM_PRESCALE_SHIFT(pwm->hwpwm);
  79. prescale &= IPROC_PWM_PRESCALE_MAX;
  80. multi = NSEC_PER_SEC * (prescale + 1);
  81. value = readl(ip->base + IPROC_PWM_PERIOD_OFFSET(pwm->hwpwm));
  82. tmp = (value & IPROC_PWM_PERIOD_MAX) * multi;
  83. state->period = div64_u64(tmp, rate);
  84. value = readl(ip->base + IPROC_PWM_DUTY_CYCLE_OFFSET(pwm->hwpwm));
  85. tmp = (value & IPROC_PWM_PERIOD_MAX) * multi;
  86. state->duty_cycle = div64_u64(tmp, rate);
  87. return 0;
  88. }
  89. static int iproc_pwmc_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  90. const struct pwm_state *state)
  91. {
  92. unsigned long prescale = IPROC_PWM_PRESCALE_MIN;
  93. struct iproc_pwmc *ip = to_iproc_pwmc(chip);
  94. u32 value, period, duty;
  95. u64 rate;
  96. rate = clk_get_rate(ip->clk);
  97. /*
  98. * Find period count, duty count and prescale to suit duty_cycle and
  99. * period. This is done according to formulas described below:
  100. *
  101. * period_ns = 10^9 * (PRESCALE + 1) * PC / PWM_CLK_RATE
  102. * duty_ns = 10^9 * (PRESCALE + 1) * DC / PWM_CLK_RATE
  103. *
  104. * PC = (PWM_CLK_RATE * period_ns) / (10^9 * (PRESCALE + 1))
  105. * DC = (PWM_CLK_RATE * duty_ns) / (10^9 * (PRESCALE + 1))
  106. */
  107. while (1) {
  108. u64 value, div;
  109. div = NSEC_PER_SEC * (prescale + 1);
  110. value = rate * state->period;
  111. period = div64_u64(value, div);
  112. value = rate * state->duty_cycle;
  113. duty = div64_u64(value, div);
  114. if (period < IPROC_PWM_PERIOD_MIN)
  115. return -EINVAL;
  116. if (period <= IPROC_PWM_PERIOD_MAX &&
  117. duty <= IPROC_PWM_DUTY_CYCLE_MAX)
  118. break;
  119. /* Otherwise, increase prescale and recalculate counts */
  120. if (++prescale > IPROC_PWM_PRESCALE_MAX)
  121. return -EINVAL;
  122. }
  123. iproc_pwmc_disable(ip, pwm->hwpwm);
  124. /* Set prescale */
  125. value = readl(ip->base + IPROC_PWM_PRESCALE_OFFSET);
  126. value &= ~IPROC_PWM_PRESCALE_MASK(pwm->hwpwm);
  127. value |= prescale << IPROC_PWM_PRESCALE_SHIFT(pwm->hwpwm);
  128. writel(value, ip->base + IPROC_PWM_PRESCALE_OFFSET);
  129. /* set period and duty cycle */
  130. writel(period, ip->base + IPROC_PWM_PERIOD_OFFSET(pwm->hwpwm));
  131. writel(duty, ip->base + IPROC_PWM_DUTY_CYCLE_OFFSET(pwm->hwpwm));
  132. /* set polarity */
  133. value = readl(ip->base + IPROC_PWM_CTRL_OFFSET);
  134. if (state->polarity == PWM_POLARITY_NORMAL)
  135. value |= 1 << IPROC_PWM_CTRL_POLARITY_SHIFT(pwm->hwpwm);
  136. else
  137. value &= ~(1 << IPROC_PWM_CTRL_POLARITY_SHIFT(pwm->hwpwm));
  138. writel(value, ip->base + IPROC_PWM_CTRL_OFFSET);
  139. if (state->enabled)
  140. iproc_pwmc_enable(ip, pwm->hwpwm);
  141. return 0;
  142. }
  143. static const struct pwm_ops iproc_pwm_ops = {
  144. .apply = iproc_pwmc_apply,
  145. .get_state = iproc_pwmc_get_state,
  146. };
  147. static int iproc_pwmc_probe(struct platform_device *pdev)
  148. {
  149. struct pwm_chip *chip;
  150. struct iproc_pwmc *ip;
  151. unsigned int i;
  152. u32 value;
  153. int ret;
  154. chip = devm_pwmchip_alloc(&pdev->dev, 4, sizeof(*ip));
  155. if (IS_ERR(chip))
  156. return PTR_ERR(chip);
  157. ip = to_iproc_pwmc(chip);
  158. platform_set_drvdata(pdev, ip);
  159. chip->ops = &iproc_pwm_ops;
  160. ip->base = devm_platform_ioremap_resource(pdev, 0);
  161. if (IS_ERR(ip->base))
  162. return PTR_ERR(ip->base);
  163. ip->clk = devm_clk_get_enabled(&pdev->dev, NULL);
  164. if (IS_ERR(ip->clk))
  165. return dev_err_probe(&pdev->dev, PTR_ERR(ip->clk),
  166. "failed to get clock\n");
  167. /* Set full drive and normal polarity for all channels */
  168. value = readl(ip->base + IPROC_PWM_CTRL_OFFSET);
  169. for (i = 0; i < chip->npwm; i++) {
  170. value &= ~(1 << IPROC_PWM_CTRL_TYPE_SHIFT(i));
  171. value |= 1 << IPROC_PWM_CTRL_POLARITY_SHIFT(i);
  172. }
  173. writel(value, ip->base + IPROC_PWM_CTRL_OFFSET);
  174. ret = devm_pwmchip_add(&pdev->dev, chip);
  175. if (ret < 0)
  176. return dev_err_probe(&pdev->dev, ret,
  177. "failed to add PWM chip\n");
  178. return 0;
  179. }
  180. static const struct of_device_id bcm_iproc_pwmc_dt[] = {
  181. { .compatible = "brcm,iproc-pwm" },
  182. { },
  183. };
  184. MODULE_DEVICE_TABLE(of, bcm_iproc_pwmc_dt);
  185. static struct platform_driver iproc_pwmc_driver = {
  186. .driver = {
  187. .name = "bcm-iproc-pwm",
  188. .of_match_table = bcm_iproc_pwmc_dt,
  189. },
  190. .probe = iproc_pwmc_probe,
  191. };
  192. module_platform_driver(iproc_pwmc_driver);
  193. MODULE_AUTHOR("Yendapally Reddy Dhananjaya Reddy <yendapally.reddy@broadcom.com>");
  194. MODULE_DESCRIPTION("Broadcom iProc PWM driver");
  195. MODULE_LICENSE("GPL v2");