pwm-bcm-iproc.c 7.6 KB

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