pwm-berlin.c 7.3 KB

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