pwm-bcm-iproc.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. struct pwm_chip chip;
  32. void __iomem *base;
  33. struct clk *clk;
  34. };
  35. static inline struct iproc_pwmc *to_iproc_pwmc(struct pwm_chip *chip)
  36. {
  37. return container_of(chip, struct iproc_pwmc, chip);
  38. }
  39. static void iproc_pwmc_enable(struct iproc_pwmc *ip, unsigned int channel)
  40. {
  41. u32 value;
  42. value = readl(ip->base + IPROC_PWM_CTRL_OFFSET);
  43. value |= 1 << IPROC_PWM_CTRL_EN_SHIFT(channel);
  44. writel(value, ip->base + IPROC_PWM_CTRL_OFFSET);
  45. /* must be a 400 ns delay between clearing and setting enable bit */
  46. ndelay(400);
  47. }
  48. static void iproc_pwmc_disable(struct iproc_pwmc *ip, unsigned int channel)
  49. {
  50. u32 value;
  51. value = readl(ip->base + IPROC_PWM_CTRL_OFFSET);
  52. value &= ~(1 << IPROC_PWM_CTRL_EN_SHIFT(channel));
  53. writel(value, ip->base + IPROC_PWM_CTRL_OFFSET);
  54. /* must be a 400 ns delay between clearing and setting enable bit */
  55. ndelay(400);
  56. }
  57. static int iproc_pwmc_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
  58. struct pwm_state *state)
  59. {
  60. struct iproc_pwmc *ip = to_iproc_pwmc(chip);
  61. u64 tmp, multi, rate;
  62. u32 value, prescale;
  63. value = readl(ip->base + IPROC_PWM_CTRL_OFFSET);
  64. if (value & BIT(IPROC_PWM_CTRL_EN_SHIFT(pwm->hwpwm)))
  65. state->enabled = true;
  66. else
  67. state->enabled = false;
  68. if (value & BIT(IPROC_PWM_CTRL_POLARITY_SHIFT(pwm->hwpwm)))
  69. state->polarity = PWM_POLARITY_NORMAL;
  70. else
  71. state->polarity = PWM_POLARITY_INVERSED;
  72. rate = clk_get_rate(ip->clk);
  73. if (rate == 0) {
  74. state->period = 0;
  75. state->duty_cycle = 0;
  76. return 0;
  77. }
  78. value = readl(ip->base + IPROC_PWM_PRESCALE_OFFSET);
  79. prescale = value >> IPROC_PWM_PRESCALE_SHIFT(pwm->hwpwm);
  80. prescale &= IPROC_PWM_PRESCALE_MAX;
  81. multi = NSEC_PER_SEC * (prescale + 1);
  82. value = readl(ip->base + IPROC_PWM_PERIOD_OFFSET(pwm->hwpwm));
  83. tmp = (value & IPROC_PWM_PERIOD_MAX) * multi;
  84. state->period = div64_u64(tmp, rate);
  85. value = readl(ip->base + IPROC_PWM_DUTY_CYCLE_OFFSET(pwm->hwpwm));
  86. tmp = (value & IPROC_PWM_PERIOD_MAX) * multi;
  87. state->duty_cycle = div64_u64(tmp, rate);
  88. return 0;
  89. }
  90. static int iproc_pwmc_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  91. const struct pwm_state *state)
  92. {
  93. unsigned long prescale = IPROC_PWM_PRESCALE_MIN;
  94. struct iproc_pwmc *ip = to_iproc_pwmc(chip);
  95. u32 value, period, duty;
  96. u64 rate;
  97. rate = clk_get_rate(ip->clk);
  98. /*
  99. * Find period count, duty count and prescale to suit duty_cycle and
  100. * period. This is done according to formulas described below:
  101. *
  102. * period_ns = 10^9 * (PRESCALE + 1) * PC / PWM_CLK_RATE
  103. * duty_ns = 10^9 * (PRESCALE + 1) * DC / PWM_CLK_RATE
  104. *
  105. * PC = (PWM_CLK_RATE * period_ns) / (10^9 * (PRESCALE + 1))
  106. * DC = (PWM_CLK_RATE * duty_ns) / (10^9 * (PRESCALE + 1))
  107. */
  108. while (1) {
  109. u64 value, div;
  110. div = NSEC_PER_SEC * (prescale + 1);
  111. value = rate * state->period;
  112. period = div64_u64(value, div);
  113. value = rate * state->duty_cycle;
  114. duty = div64_u64(value, div);
  115. if (period < IPROC_PWM_PERIOD_MIN)
  116. return -EINVAL;
  117. if (period <= IPROC_PWM_PERIOD_MAX &&
  118. duty <= IPROC_PWM_DUTY_CYCLE_MAX)
  119. break;
  120. /* Otherwise, increase prescale and recalculate counts */
  121. if (++prescale > IPROC_PWM_PRESCALE_MAX)
  122. return -EINVAL;
  123. }
  124. iproc_pwmc_disable(ip, pwm->hwpwm);
  125. /* Set prescale */
  126. value = readl(ip->base + IPROC_PWM_PRESCALE_OFFSET);
  127. value &= ~IPROC_PWM_PRESCALE_MASK(pwm->hwpwm);
  128. value |= prescale << IPROC_PWM_PRESCALE_SHIFT(pwm->hwpwm);
  129. writel(value, ip->base + IPROC_PWM_PRESCALE_OFFSET);
  130. /* set period and duty cycle */
  131. writel(period, ip->base + IPROC_PWM_PERIOD_OFFSET(pwm->hwpwm));
  132. writel(duty, ip->base + IPROC_PWM_DUTY_CYCLE_OFFSET(pwm->hwpwm));
  133. /* set polarity */
  134. value = readl(ip->base + IPROC_PWM_CTRL_OFFSET);
  135. if (state->polarity == PWM_POLARITY_NORMAL)
  136. value |= 1 << IPROC_PWM_CTRL_POLARITY_SHIFT(pwm->hwpwm);
  137. else
  138. value &= ~(1 << IPROC_PWM_CTRL_POLARITY_SHIFT(pwm->hwpwm));
  139. writel(value, ip->base + IPROC_PWM_CTRL_OFFSET);
  140. if (state->enabled)
  141. iproc_pwmc_enable(ip, pwm->hwpwm);
  142. return 0;
  143. }
  144. static const struct pwm_ops iproc_pwm_ops = {
  145. .apply = iproc_pwmc_apply,
  146. .get_state = iproc_pwmc_get_state,
  147. .owner = THIS_MODULE,
  148. };
  149. static int iproc_pwmc_probe(struct platform_device *pdev)
  150. {
  151. struct iproc_pwmc *ip;
  152. unsigned int i;
  153. u32 value;
  154. int ret;
  155. ip = devm_kzalloc(&pdev->dev, sizeof(*ip), GFP_KERNEL);
  156. if (!ip)
  157. return -ENOMEM;
  158. platform_set_drvdata(pdev, ip);
  159. ip->chip.dev = &pdev->dev;
  160. ip->chip.ops = &iproc_pwm_ops;
  161. ip->chip.npwm = 4;
  162. ip->base = devm_platform_ioremap_resource(pdev, 0);
  163. if (IS_ERR(ip->base))
  164. return PTR_ERR(ip->base);
  165. ip->clk = devm_clk_get(&pdev->dev, NULL);
  166. if (IS_ERR(ip->clk)) {
  167. dev_err(&pdev->dev, "failed to get clock: %ld\n",
  168. PTR_ERR(ip->clk));
  169. return PTR_ERR(ip->clk);
  170. }
  171. ret = clk_prepare_enable(ip->clk);
  172. if (ret < 0) {
  173. dev_err(&pdev->dev, "failed to enable clock: %d\n", ret);
  174. return ret;
  175. }
  176. /* Set full drive and normal polarity for all channels */
  177. value = readl(ip->base + IPROC_PWM_CTRL_OFFSET);
  178. for (i = 0; i < ip->chip.npwm; i++) {
  179. value &= ~(1 << IPROC_PWM_CTRL_TYPE_SHIFT(i));
  180. value |= 1 << IPROC_PWM_CTRL_POLARITY_SHIFT(i);
  181. }
  182. writel(value, ip->base + IPROC_PWM_CTRL_OFFSET);
  183. ret = pwmchip_add(&ip->chip);
  184. if (ret < 0) {
  185. dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret);
  186. clk_disable_unprepare(ip->clk);
  187. }
  188. return ret;
  189. }
  190. static int iproc_pwmc_remove(struct platform_device *pdev)
  191. {
  192. struct iproc_pwmc *ip = platform_get_drvdata(pdev);
  193. pwmchip_remove(&ip->chip);
  194. clk_disable_unprepare(ip->clk);
  195. return 0;
  196. }
  197. static const struct of_device_id bcm_iproc_pwmc_dt[] = {
  198. { .compatible = "brcm,iproc-pwm" },
  199. { },
  200. };
  201. MODULE_DEVICE_TABLE(of, bcm_iproc_pwmc_dt);
  202. static struct platform_driver iproc_pwmc_driver = {
  203. .driver = {
  204. .name = "bcm-iproc-pwm",
  205. .of_match_table = bcm_iproc_pwmc_dt,
  206. },
  207. .probe = iproc_pwmc_probe,
  208. .remove = iproc_pwmc_remove,
  209. };
  210. module_platform_driver(iproc_pwmc_driver);
  211. MODULE_AUTHOR("Yendapally Reddy Dhananjaya Reddy <[email protected]>");
  212. MODULE_DESCRIPTION("Broadcom iProc PWM driver");
  213. MODULE_LICENSE("GPL v2");