pwm-bcm-iproc.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. rate = clk_get_rate(ip->clk);
  74. value = readl(ip->base + IPROC_PWM_CTRL_OFFSET);
  75. if (value & BIT(IPROC_PWM_CTRL_EN_SHIFT(pwm->hwpwm)))
  76. state->enabled = true;
  77. else
  78. state->enabled = false;
  79. if (value & BIT(IPROC_PWM_CTRL_POLARITY_SHIFT(pwm->hwpwm)))
  80. state->polarity = PWM_POLARITY_NORMAL;
  81. else
  82. state->polarity = PWM_POLARITY_INVERSED;
  83. value = readl(ip->base + IPROC_PWM_PRESCALE_OFFSET);
  84. prescale = value >> IPROC_PWM_PRESCALE_SHIFT(pwm->hwpwm);
  85. prescale &= IPROC_PWM_PRESCALE_MAX;
  86. multi = NSEC_PER_SEC * (prescale + 1);
  87. value = readl(ip->base + IPROC_PWM_PERIOD_OFFSET(pwm->hwpwm));
  88. tmp = (value & IPROC_PWM_PERIOD_MAX) * multi;
  89. state->period = div64_u64(tmp, rate);
  90. value = readl(ip->base + IPROC_PWM_DUTY_CYCLE_OFFSET(pwm->hwpwm));
  91. tmp = (value & IPROC_PWM_PERIOD_MAX) * multi;
  92. state->duty_cycle = div64_u64(tmp, rate);
  93. }
  94. static int iproc_pwmc_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  95. struct pwm_state *state)
  96. {
  97. unsigned long prescale = IPROC_PWM_PRESCALE_MIN;
  98. struct iproc_pwmc *ip = to_iproc_pwmc(chip);
  99. u32 value, period, duty;
  100. u64 rate;
  101. rate = clk_get_rate(ip->clk);
  102. /*
  103. * Find period count, duty count and prescale to suit duty_cycle and
  104. * period. This is done according to formulas described below:
  105. *
  106. * period_ns = 10^9 * (PRESCALE + 1) * PC / PWM_CLK_RATE
  107. * duty_ns = 10^9 * (PRESCALE + 1) * DC / PWM_CLK_RATE
  108. *
  109. * PC = (PWM_CLK_RATE * period_ns) / (10^9 * (PRESCALE + 1))
  110. * DC = (PWM_CLK_RATE * duty_ns) / (10^9 * (PRESCALE + 1))
  111. */
  112. while (1) {
  113. u64 value, div;
  114. div = NSEC_PER_SEC * (prescale + 1);
  115. value = rate * state->period;
  116. period = div64_u64(value, div);
  117. value = rate * state->duty_cycle;
  118. duty = div64_u64(value, div);
  119. if (period < IPROC_PWM_PERIOD_MIN ||
  120. duty < IPROC_PWM_DUTY_CYCLE_MIN)
  121. return -EINVAL;
  122. if (period <= IPROC_PWM_PERIOD_MAX &&
  123. duty <= IPROC_PWM_DUTY_CYCLE_MAX)
  124. break;
  125. /* Otherwise, increase prescale and recalculate counts */
  126. if (++prescale > IPROC_PWM_PRESCALE_MAX)
  127. return -EINVAL;
  128. }
  129. iproc_pwmc_disable(ip, pwm->hwpwm);
  130. /* Set prescale */
  131. value = readl(ip->base + IPROC_PWM_PRESCALE_OFFSET);
  132. value &= ~IPROC_PWM_PRESCALE_MASK(pwm->hwpwm);
  133. value |= prescale << IPROC_PWM_PRESCALE_SHIFT(pwm->hwpwm);
  134. writel(value, ip->base + IPROC_PWM_PRESCALE_OFFSET);
  135. /* set period and duty cycle */
  136. writel(period, ip->base + IPROC_PWM_PERIOD_OFFSET(pwm->hwpwm));
  137. writel(duty, ip->base + IPROC_PWM_DUTY_CYCLE_OFFSET(pwm->hwpwm));
  138. /* set polarity */
  139. value = readl(ip->base + IPROC_PWM_CTRL_OFFSET);
  140. if (state->polarity == PWM_POLARITY_NORMAL)
  141. value |= 1 << IPROC_PWM_CTRL_POLARITY_SHIFT(pwm->hwpwm);
  142. else
  143. value &= ~(1 << IPROC_PWM_CTRL_POLARITY_SHIFT(pwm->hwpwm));
  144. writel(value, ip->base + IPROC_PWM_CTRL_OFFSET);
  145. if (state->enabled)
  146. iproc_pwmc_enable(ip, pwm->hwpwm);
  147. return 0;
  148. }
  149. static const struct pwm_ops iproc_pwm_ops = {
  150. .apply = iproc_pwmc_apply,
  151. .get_state = iproc_pwmc_get_state,
  152. };
  153. static int iproc_pwmc_probe(struct platform_device *pdev)
  154. {
  155. struct iproc_pwmc *ip;
  156. struct resource *res;
  157. unsigned int i;
  158. u32 value;
  159. int ret;
  160. ip = devm_kzalloc(&pdev->dev, sizeof(*ip), GFP_KERNEL);
  161. if (!ip)
  162. return -ENOMEM;
  163. platform_set_drvdata(pdev, ip);
  164. ip->chip.dev = &pdev->dev;
  165. ip->chip.ops = &iproc_pwm_ops;
  166. ip->chip.base = -1;
  167. ip->chip.npwm = 4;
  168. ip->chip.of_xlate = of_pwm_xlate_with_flags;
  169. ip->chip.of_pwm_n_cells = 3;
  170. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  171. ip->base = devm_ioremap_resource(&pdev->dev, res);
  172. if (IS_ERR(ip->base))
  173. return PTR_ERR(ip->base);
  174. ip->clk = devm_clk_get(&pdev->dev, NULL);
  175. if (IS_ERR(ip->clk)) {
  176. dev_err(&pdev->dev, "failed to get clock: %ld\n",
  177. PTR_ERR(ip->clk));
  178. return PTR_ERR(ip->clk);
  179. }
  180. ret = clk_prepare_enable(ip->clk);
  181. if (ret < 0) {
  182. dev_err(&pdev->dev, "failed to enable clock: %d\n", ret);
  183. return ret;
  184. }
  185. /* Set full drive and normal polarity for all channels */
  186. value = readl(ip->base + IPROC_PWM_CTRL_OFFSET);
  187. for (i = 0; i < ip->chip.npwm; i++) {
  188. value &= ~(1 << IPROC_PWM_CTRL_TYPE_SHIFT(i));
  189. value |= 1 << IPROC_PWM_CTRL_POLARITY_SHIFT(i);
  190. }
  191. writel(value, ip->base + IPROC_PWM_CTRL_OFFSET);
  192. ret = pwmchip_add(&ip->chip);
  193. if (ret < 0) {
  194. dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret);
  195. clk_disable_unprepare(ip->clk);
  196. }
  197. return ret;
  198. }
  199. static int iproc_pwmc_remove(struct platform_device *pdev)
  200. {
  201. struct iproc_pwmc *ip = platform_get_drvdata(pdev);
  202. clk_disable_unprepare(ip->clk);
  203. return pwmchip_remove(&ip->chip);
  204. }
  205. static const struct of_device_id bcm_iproc_pwmc_dt[] = {
  206. { .compatible = "brcm,iproc-pwm" },
  207. { },
  208. };
  209. MODULE_DEVICE_TABLE(of, bcm_iproc_pwmc_dt);
  210. static struct platform_driver iproc_pwmc_driver = {
  211. .driver = {
  212. .name = "bcm-iproc-pwm",
  213. .of_match_table = bcm_iproc_pwmc_dt,
  214. },
  215. .probe = iproc_pwmc_probe,
  216. .remove = iproc_pwmc_remove,
  217. };
  218. module_platform_driver(iproc_pwmc_driver);
  219. MODULE_AUTHOR("Yendapally Reddy Dhananjaya Reddy <yendapally.reddy@broadcom.com>");
  220. MODULE_DESCRIPTION("Broadcom iProc PWM driver");
  221. MODULE_LICENSE("GPL v2");