pwm-berlin.c 8.0 KB

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