pwm-berlin.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * Marvell Berlin PWM driver
  3. *
  4. * Copyright (C) 2015 Marvell Technology Group Ltd.
  5. *
  6. * Author: Antoine Tenart <antoine.tenart@free-electrons.com>
  7. *
  8. * This file is licensed under the terms of the GNU General Public
  9. * License version 2. This program is licensed "as is" without any
  10. * warranty of any kind, whether express or implied.
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/io.h>
  14. #include <linux/kernel.h>
  15. #include <linux/mod_devicetable.h>
  16. #include <linux/module.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/pwm.h>
  19. #include <linux/slab.h>
  20. #define BERLIN_PWM_EN 0x0
  21. #define BERLIN_PWM_ENABLE BIT(0)
  22. #define BERLIN_PWM_CONTROL 0x4
  23. /*
  24. * The prescaler claims to support 8 different moduli, configured using the
  25. * low three bits of PWM_CONTROL. (Sequentially, they are 1, 4, 8, 16, 64,
  26. * 256, 1024, and 4096.) However, the moduli from 4 to 1024 appear to be
  27. * implemented by internally shifting TCNT left without adding additional
  28. * bits. So, the max TCNT that actually works for a modulus of 4 is 0x3fff;
  29. * for 8, 0x1fff; and so on. This means that those moduli are entirely
  30. * useless, as we could just do the shift ourselves. The 4096 modulus is
  31. * implemented with a real prescaler, so we do use that, but we treat it
  32. * as a flag instead of pretending the modulus is actually configurable.
  33. */
  34. #define BERLIN_PWM_PRESCALE_4096 0x7
  35. #define BERLIN_PWM_INVERT_POLARITY BIT(3)
  36. #define BERLIN_PWM_DUTY 0x8
  37. #define BERLIN_PWM_TCNT 0xc
  38. #define BERLIN_PWM_MAX_TCNT 65535
  39. #define BERLIN_PWM_NUMPWMS 4
  40. struct berlin_pwm_channel {
  41. u32 enable;
  42. u32 ctrl;
  43. u32 duty;
  44. u32 tcnt;
  45. };
  46. struct berlin_pwm_chip {
  47. struct clk *clk;
  48. void __iomem *base;
  49. struct berlin_pwm_channel channel[BERLIN_PWM_NUMPWMS];
  50. };
  51. static inline struct berlin_pwm_chip *to_berlin_pwm_chip(struct pwm_chip *chip)
  52. {
  53. return pwmchip_get_drvdata(chip);
  54. }
  55. static inline u32 berlin_pwm_readl(struct berlin_pwm_chip *bpc,
  56. unsigned int channel, unsigned long offset)
  57. {
  58. return readl_relaxed(bpc->base + channel * 0x10 + offset);
  59. }
  60. static inline void berlin_pwm_writel(struct berlin_pwm_chip *bpc,
  61. unsigned int channel, u32 value,
  62. unsigned long offset)
  63. {
  64. writel_relaxed(value, bpc->base + channel * 0x10 + offset);
  65. }
  66. static int berlin_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  67. u64 duty_ns, u64 period_ns)
  68. {
  69. struct berlin_pwm_chip *bpc = to_berlin_pwm_chip(chip);
  70. bool prescale_4096 = false;
  71. u32 value, duty, period;
  72. u64 cycles;
  73. cycles = clk_get_rate(bpc->clk);
  74. cycles *= period_ns;
  75. do_div(cycles, NSEC_PER_SEC);
  76. if (cycles > BERLIN_PWM_MAX_TCNT) {
  77. prescale_4096 = true;
  78. cycles >>= 12; // Prescaled by 4096
  79. if (cycles > BERLIN_PWM_MAX_TCNT)
  80. return -ERANGE;
  81. }
  82. period = cycles;
  83. cycles *= duty_ns;
  84. do_div(cycles, period_ns);
  85. duty = cycles;
  86. value = berlin_pwm_readl(bpc, pwm->hwpwm, BERLIN_PWM_CONTROL);
  87. if (prescale_4096)
  88. value |= BERLIN_PWM_PRESCALE_4096;
  89. else
  90. value &= ~BERLIN_PWM_PRESCALE_4096;
  91. berlin_pwm_writel(bpc, pwm->hwpwm, value, BERLIN_PWM_CONTROL);
  92. berlin_pwm_writel(bpc, pwm->hwpwm, duty, BERLIN_PWM_DUTY);
  93. berlin_pwm_writel(bpc, pwm->hwpwm, period, BERLIN_PWM_TCNT);
  94. return 0;
  95. }
  96. static int berlin_pwm_set_polarity(struct pwm_chip *chip,
  97. struct pwm_device *pwm,
  98. enum pwm_polarity polarity)
  99. {
  100. struct berlin_pwm_chip *bpc = to_berlin_pwm_chip(chip);
  101. u32 value;
  102. value = berlin_pwm_readl(bpc, pwm->hwpwm, BERLIN_PWM_CONTROL);
  103. if (polarity == PWM_POLARITY_NORMAL)
  104. value &= ~BERLIN_PWM_INVERT_POLARITY;
  105. else
  106. value |= BERLIN_PWM_INVERT_POLARITY;
  107. berlin_pwm_writel(bpc, pwm->hwpwm, value, BERLIN_PWM_CONTROL);
  108. return 0;
  109. }
  110. static int berlin_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  111. {
  112. struct berlin_pwm_chip *bpc = to_berlin_pwm_chip(chip);
  113. u32 value;
  114. value = berlin_pwm_readl(bpc, pwm->hwpwm, BERLIN_PWM_EN);
  115. value |= BERLIN_PWM_ENABLE;
  116. berlin_pwm_writel(bpc, pwm->hwpwm, value, BERLIN_PWM_EN);
  117. return 0;
  118. }
  119. static void berlin_pwm_disable(struct pwm_chip *chip,
  120. struct pwm_device *pwm)
  121. {
  122. struct berlin_pwm_chip *bpc = to_berlin_pwm_chip(chip);
  123. u32 value;
  124. value = berlin_pwm_readl(bpc, pwm->hwpwm, BERLIN_PWM_EN);
  125. value &= ~BERLIN_PWM_ENABLE;
  126. berlin_pwm_writel(bpc, pwm->hwpwm, value, BERLIN_PWM_EN);
  127. }
  128. static int berlin_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  129. const struct pwm_state *state)
  130. {
  131. int err;
  132. bool enabled = pwm->state.enabled;
  133. if (state->polarity != pwm->state.polarity) {
  134. if (enabled) {
  135. berlin_pwm_disable(chip, pwm);
  136. enabled = false;
  137. }
  138. err = berlin_pwm_set_polarity(chip, pwm, state->polarity);
  139. if (err)
  140. return err;
  141. }
  142. if (!state->enabled) {
  143. if (enabled)
  144. berlin_pwm_disable(chip, pwm);
  145. return 0;
  146. }
  147. err = berlin_pwm_config(chip, pwm, state->duty_cycle, state->period);
  148. if (err)
  149. return err;
  150. if (!enabled)
  151. return berlin_pwm_enable(chip, pwm);
  152. return 0;
  153. }
  154. static const struct pwm_ops berlin_pwm_ops = {
  155. .apply = berlin_pwm_apply,
  156. };
  157. static const struct of_device_id berlin_pwm_match[] = {
  158. { .compatible = "marvell,berlin-pwm" },
  159. { },
  160. };
  161. MODULE_DEVICE_TABLE(of, berlin_pwm_match);
  162. static int berlin_pwm_probe(struct platform_device *pdev)
  163. {
  164. struct pwm_chip *chip;
  165. struct berlin_pwm_chip *bpc;
  166. int ret;
  167. chip = devm_pwmchip_alloc(&pdev->dev, BERLIN_PWM_NUMPWMS, sizeof(*bpc));
  168. if (IS_ERR(chip))
  169. return PTR_ERR(chip);
  170. bpc = to_berlin_pwm_chip(chip);
  171. bpc->base = devm_platform_ioremap_resource(pdev, 0);
  172. if (IS_ERR(bpc->base))
  173. return PTR_ERR(bpc->base);
  174. bpc->clk = devm_clk_get_enabled(&pdev->dev, NULL);
  175. if (IS_ERR(bpc->clk))
  176. return PTR_ERR(bpc->clk);
  177. chip->ops = &berlin_pwm_ops;
  178. ret = devm_pwmchip_add(&pdev->dev, chip);
  179. if (ret < 0)
  180. return dev_err_probe(&pdev->dev, ret, "failed to add PWM chip\n");
  181. platform_set_drvdata(pdev, chip);
  182. return 0;
  183. }
  184. static int berlin_pwm_suspend(struct device *dev)
  185. {
  186. struct pwm_chip *chip = dev_get_drvdata(dev);
  187. struct berlin_pwm_chip *bpc = to_berlin_pwm_chip(chip);
  188. unsigned int i;
  189. for (i = 0; i < chip->npwm; i++) {
  190. struct berlin_pwm_channel *channel = &bpc->channel[i];
  191. channel->enable = berlin_pwm_readl(bpc, i, BERLIN_PWM_EN);
  192. channel->ctrl = berlin_pwm_readl(bpc, i, BERLIN_PWM_CONTROL);
  193. channel->duty = berlin_pwm_readl(bpc, i, BERLIN_PWM_DUTY);
  194. channel->tcnt = berlin_pwm_readl(bpc, i, BERLIN_PWM_TCNT);
  195. }
  196. clk_disable_unprepare(bpc->clk);
  197. return 0;
  198. }
  199. static int berlin_pwm_resume(struct device *dev)
  200. {
  201. struct pwm_chip *chip = dev_get_drvdata(dev);
  202. struct berlin_pwm_chip *bpc = to_berlin_pwm_chip(chip);
  203. unsigned int i;
  204. int ret;
  205. ret = clk_prepare_enable(bpc->clk);
  206. if (ret)
  207. return ret;
  208. for (i = 0; i < chip->npwm; i++) {
  209. struct berlin_pwm_channel *channel = &bpc->channel[i];
  210. berlin_pwm_writel(bpc, i, channel->ctrl, BERLIN_PWM_CONTROL);
  211. berlin_pwm_writel(bpc, i, channel->duty, BERLIN_PWM_DUTY);
  212. berlin_pwm_writel(bpc, i, channel->tcnt, BERLIN_PWM_TCNT);
  213. berlin_pwm_writel(bpc, i, channel->enable, BERLIN_PWM_EN);
  214. }
  215. return 0;
  216. }
  217. static DEFINE_SIMPLE_DEV_PM_OPS(berlin_pwm_pm_ops, berlin_pwm_suspend,
  218. berlin_pwm_resume);
  219. static struct platform_driver berlin_pwm_driver = {
  220. .probe = berlin_pwm_probe,
  221. .driver = {
  222. .name = "berlin-pwm",
  223. .of_match_table = berlin_pwm_match,
  224. .pm = pm_ptr(&berlin_pwm_pm_ops),
  225. },
  226. };
  227. module_platform_driver(berlin_pwm_driver);
  228. MODULE_AUTHOR("Antoine Tenart <antoine.tenart@free-electrons.com>");
  229. MODULE_DESCRIPTION("Marvell Berlin PWM driver");
  230. MODULE_LICENSE("GPL v2");