pwm-berlin.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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/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 *chip,
  54. unsigned int channel, unsigned long offset)
  55. {
  56. return readl_relaxed(chip->base + channel * 0x10 + offset);
  57. }
  58. static inline void berlin_pwm_writel(struct berlin_pwm_chip *chip,
  59. unsigned int channel, u32 value,
  60. unsigned long offset)
  61. {
  62. writel_relaxed(value, chip->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. pwm_set_chip_data(pwm, NULL);
  76. kfree(channel);
  77. }
  78. static int berlin_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm_dev,
  79. int duty_ns, int period_ns)
  80. {
  81. struct berlin_pwm_chip *pwm = to_berlin_pwm_chip(chip);
  82. bool prescale_4096 = false;
  83. u32 value, duty, period;
  84. u64 cycles;
  85. cycles = clk_get_rate(pwm->clk);
  86. cycles *= period_ns;
  87. do_div(cycles, NSEC_PER_SEC);
  88. if (cycles > BERLIN_PWM_MAX_TCNT) {
  89. prescale_4096 = true;
  90. cycles >>= 12; // Prescaled by 4096
  91. if (cycles > BERLIN_PWM_MAX_TCNT)
  92. return -ERANGE;
  93. }
  94. period = cycles;
  95. cycles *= duty_ns;
  96. do_div(cycles, period_ns);
  97. duty = cycles;
  98. value = berlin_pwm_readl(pwm, pwm_dev->hwpwm, BERLIN_PWM_CONTROL);
  99. if (prescale_4096)
  100. value |= BERLIN_PWM_PRESCALE_4096;
  101. else
  102. value &= ~BERLIN_PWM_PRESCALE_4096;
  103. berlin_pwm_writel(pwm, pwm_dev->hwpwm, value, BERLIN_PWM_CONTROL);
  104. berlin_pwm_writel(pwm, pwm_dev->hwpwm, duty, BERLIN_PWM_DUTY);
  105. berlin_pwm_writel(pwm, pwm_dev->hwpwm, period, BERLIN_PWM_TCNT);
  106. return 0;
  107. }
  108. static int berlin_pwm_set_polarity(struct pwm_chip *chip,
  109. struct pwm_device *pwm_dev,
  110. enum pwm_polarity polarity)
  111. {
  112. struct berlin_pwm_chip *pwm = to_berlin_pwm_chip(chip);
  113. u32 value;
  114. value = berlin_pwm_readl(pwm, pwm_dev->hwpwm, BERLIN_PWM_CONTROL);
  115. if (polarity == PWM_POLARITY_NORMAL)
  116. value &= ~BERLIN_PWM_INVERT_POLARITY;
  117. else
  118. value |= BERLIN_PWM_INVERT_POLARITY;
  119. berlin_pwm_writel(pwm, pwm_dev->hwpwm, value, BERLIN_PWM_CONTROL);
  120. return 0;
  121. }
  122. static int berlin_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm_dev)
  123. {
  124. struct berlin_pwm_chip *pwm = to_berlin_pwm_chip(chip);
  125. u32 value;
  126. value = berlin_pwm_readl(pwm, pwm_dev->hwpwm, BERLIN_PWM_EN);
  127. value |= BERLIN_PWM_ENABLE;
  128. berlin_pwm_writel(pwm, pwm_dev->hwpwm, value, BERLIN_PWM_EN);
  129. return 0;
  130. }
  131. static void berlin_pwm_disable(struct pwm_chip *chip,
  132. struct pwm_device *pwm_dev)
  133. {
  134. struct berlin_pwm_chip *pwm = to_berlin_pwm_chip(chip);
  135. u32 value;
  136. value = berlin_pwm_readl(pwm, pwm_dev->hwpwm, BERLIN_PWM_EN);
  137. value &= ~BERLIN_PWM_ENABLE;
  138. berlin_pwm_writel(pwm, pwm_dev->hwpwm, value, BERLIN_PWM_EN);
  139. }
  140. static const struct pwm_ops berlin_pwm_ops = {
  141. .request = berlin_pwm_request,
  142. .free = berlin_pwm_free,
  143. .config = berlin_pwm_config,
  144. .set_polarity = berlin_pwm_set_polarity,
  145. .enable = berlin_pwm_enable,
  146. .disable = berlin_pwm_disable,
  147. .owner = THIS_MODULE,
  148. };
  149. static const struct of_device_id berlin_pwm_match[] = {
  150. { .compatible = "marvell,berlin-pwm" },
  151. { },
  152. };
  153. MODULE_DEVICE_TABLE(of, berlin_pwm_match);
  154. static int berlin_pwm_probe(struct platform_device *pdev)
  155. {
  156. struct berlin_pwm_chip *pwm;
  157. struct resource *res;
  158. int ret;
  159. pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL);
  160. if (!pwm)
  161. return -ENOMEM;
  162. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  163. pwm->base = devm_ioremap_resource(&pdev->dev, res);
  164. if (IS_ERR(pwm->base))
  165. return PTR_ERR(pwm->base);
  166. pwm->clk = devm_clk_get(&pdev->dev, NULL);
  167. if (IS_ERR(pwm->clk))
  168. return PTR_ERR(pwm->clk);
  169. ret = clk_prepare_enable(pwm->clk);
  170. if (ret)
  171. return ret;
  172. pwm->chip.dev = &pdev->dev;
  173. pwm->chip.ops = &berlin_pwm_ops;
  174. pwm->chip.base = -1;
  175. pwm->chip.npwm = 4;
  176. pwm->chip.of_xlate = of_pwm_xlate_with_flags;
  177. pwm->chip.of_pwm_n_cells = 3;
  178. ret = pwmchip_add(&pwm->chip);
  179. if (ret < 0) {
  180. dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret);
  181. clk_disable_unprepare(pwm->clk);
  182. return ret;
  183. }
  184. platform_set_drvdata(pdev, pwm);
  185. return 0;
  186. }
  187. static int berlin_pwm_remove(struct platform_device *pdev)
  188. {
  189. struct berlin_pwm_chip *pwm = platform_get_drvdata(pdev);
  190. int ret;
  191. ret = pwmchip_remove(&pwm->chip);
  192. clk_disable_unprepare(pwm->clk);
  193. return ret;
  194. }
  195. #ifdef CONFIG_PM_SLEEP
  196. static int berlin_pwm_suspend(struct device *dev)
  197. {
  198. struct berlin_pwm_chip *pwm = dev_get_drvdata(dev);
  199. unsigned int i;
  200. for (i = 0; i < pwm->chip.npwm; i++) {
  201. struct berlin_pwm_channel *channel;
  202. channel = pwm_get_chip_data(&pwm->chip.pwms[i]);
  203. if (!channel)
  204. continue;
  205. channel->enable = berlin_pwm_readl(pwm, i, BERLIN_PWM_ENABLE);
  206. channel->ctrl = berlin_pwm_readl(pwm, i, BERLIN_PWM_CONTROL);
  207. channel->duty = berlin_pwm_readl(pwm, i, BERLIN_PWM_DUTY);
  208. channel->tcnt = berlin_pwm_readl(pwm, i, BERLIN_PWM_TCNT);
  209. }
  210. clk_disable_unprepare(pwm->clk);
  211. return 0;
  212. }
  213. static int berlin_pwm_resume(struct device *dev)
  214. {
  215. struct berlin_pwm_chip *pwm = dev_get_drvdata(dev);
  216. unsigned int i;
  217. int ret;
  218. ret = clk_prepare_enable(pwm->clk);
  219. if (ret)
  220. return ret;
  221. for (i = 0; i < pwm->chip.npwm; i++) {
  222. struct berlin_pwm_channel *channel;
  223. channel = pwm_get_chip_data(&pwm->chip.pwms[i]);
  224. if (!channel)
  225. continue;
  226. berlin_pwm_writel(pwm, i, channel->ctrl, BERLIN_PWM_CONTROL);
  227. berlin_pwm_writel(pwm, i, channel->duty, BERLIN_PWM_DUTY);
  228. berlin_pwm_writel(pwm, i, channel->tcnt, BERLIN_PWM_TCNT);
  229. berlin_pwm_writel(pwm, i, channel->enable, BERLIN_PWM_ENABLE);
  230. }
  231. return 0;
  232. }
  233. #endif
  234. static SIMPLE_DEV_PM_OPS(berlin_pwm_pm_ops, berlin_pwm_suspend,
  235. berlin_pwm_resume);
  236. static struct platform_driver berlin_pwm_driver = {
  237. .probe = berlin_pwm_probe,
  238. .remove = berlin_pwm_remove,
  239. .driver = {
  240. .name = "berlin-pwm",
  241. .of_match_table = berlin_pwm_match,
  242. .pm = &berlin_pwm_pm_ops,
  243. },
  244. };
  245. module_platform_driver(berlin_pwm_driver);
  246. MODULE_AUTHOR("Antoine Tenart <antoine.tenart@free-electrons.com>");
  247. MODULE_DESCRIPTION("Marvell Berlin PWM driver");
  248. MODULE_LICENSE("GPL v2");