pwm-berlin.c 7.8 KB

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