pwm-berlin.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. #define BERLIN_PWM_EN 0x0
  19. #define BERLIN_PWM_ENABLE BIT(0)
  20. #define BERLIN_PWM_CONTROL 0x4
  21. #define BERLIN_PWM_PRESCALE_MASK 0x7
  22. #define BERLIN_PWM_PRESCALE_MAX 4096
  23. #define BERLIN_PWM_INVERT_POLARITY BIT(3)
  24. #define BERLIN_PWM_DUTY 0x8
  25. #define BERLIN_PWM_TCNT 0xc
  26. #define BERLIN_PWM_MAX_TCNT 65535
  27. struct berlin_pwm_chip {
  28. struct pwm_chip chip;
  29. struct clk *clk;
  30. void __iomem *base;
  31. };
  32. static inline struct berlin_pwm_chip *to_berlin_pwm_chip(struct pwm_chip *chip)
  33. {
  34. return container_of(chip, struct berlin_pwm_chip, chip);
  35. }
  36. static const u32 prescaler_table[] = {
  37. 1, 4, 8, 16, 64, 256, 1024, 4096
  38. };
  39. static inline u32 berlin_pwm_readl(struct berlin_pwm_chip *chip,
  40. unsigned int channel, unsigned long offset)
  41. {
  42. return readl_relaxed(chip->base + channel * 0x10 + offset);
  43. }
  44. static inline void berlin_pwm_writel(struct berlin_pwm_chip *chip,
  45. unsigned int channel, u32 value,
  46. unsigned long offset)
  47. {
  48. writel_relaxed(value, chip->base + channel * 0x10 + offset);
  49. }
  50. static int berlin_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm_dev,
  51. int duty_ns, int period_ns)
  52. {
  53. struct berlin_pwm_chip *pwm = to_berlin_pwm_chip(chip);
  54. unsigned int prescale;
  55. u32 value, duty, period;
  56. u64 cycles, tmp;
  57. cycles = clk_get_rate(pwm->clk);
  58. cycles *= period_ns;
  59. do_div(cycles, NSEC_PER_SEC);
  60. for (prescale = 0; prescale < ARRAY_SIZE(prescaler_table); prescale++) {
  61. tmp = cycles;
  62. do_div(tmp, prescaler_table[prescale]);
  63. if (tmp <= BERLIN_PWM_MAX_TCNT)
  64. break;
  65. }
  66. if (tmp > BERLIN_PWM_MAX_TCNT)
  67. return -ERANGE;
  68. period = tmp;
  69. cycles = tmp * duty_ns;
  70. do_div(cycles, period_ns);
  71. duty = cycles;
  72. value = berlin_pwm_readl(pwm, pwm_dev->hwpwm, BERLIN_PWM_CONTROL);
  73. value &= ~BERLIN_PWM_PRESCALE_MASK;
  74. value |= prescale;
  75. berlin_pwm_writel(pwm, pwm_dev->hwpwm, value, BERLIN_PWM_CONTROL);
  76. berlin_pwm_writel(pwm, pwm_dev->hwpwm, duty, BERLIN_PWM_DUTY);
  77. berlin_pwm_writel(pwm, pwm_dev->hwpwm, period, BERLIN_PWM_TCNT);
  78. return 0;
  79. }
  80. static int berlin_pwm_set_polarity(struct pwm_chip *chip,
  81. struct pwm_device *pwm_dev,
  82. enum pwm_polarity polarity)
  83. {
  84. struct berlin_pwm_chip *pwm = to_berlin_pwm_chip(chip);
  85. u32 value;
  86. value = berlin_pwm_readl(pwm, pwm_dev->hwpwm, BERLIN_PWM_CONTROL);
  87. if (polarity == PWM_POLARITY_NORMAL)
  88. value &= ~BERLIN_PWM_INVERT_POLARITY;
  89. else
  90. value |= BERLIN_PWM_INVERT_POLARITY;
  91. berlin_pwm_writel(pwm, pwm_dev->hwpwm, value, BERLIN_PWM_CONTROL);
  92. return 0;
  93. }
  94. static int berlin_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm_dev)
  95. {
  96. struct berlin_pwm_chip *pwm = to_berlin_pwm_chip(chip);
  97. u32 value;
  98. value = berlin_pwm_readl(pwm, pwm_dev->hwpwm, BERLIN_PWM_EN);
  99. value |= BERLIN_PWM_ENABLE;
  100. berlin_pwm_writel(pwm, pwm_dev->hwpwm, value, BERLIN_PWM_EN);
  101. return 0;
  102. }
  103. static void berlin_pwm_disable(struct pwm_chip *chip,
  104. struct pwm_device *pwm_dev)
  105. {
  106. struct berlin_pwm_chip *pwm = to_berlin_pwm_chip(chip);
  107. u32 value;
  108. value = berlin_pwm_readl(pwm, pwm_dev->hwpwm, BERLIN_PWM_EN);
  109. value &= ~BERLIN_PWM_ENABLE;
  110. berlin_pwm_writel(pwm, pwm_dev->hwpwm, value, BERLIN_PWM_EN);
  111. }
  112. static const struct pwm_ops berlin_pwm_ops = {
  113. .config = berlin_pwm_config,
  114. .set_polarity = berlin_pwm_set_polarity,
  115. .enable = berlin_pwm_enable,
  116. .disable = berlin_pwm_disable,
  117. .owner = THIS_MODULE,
  118. };
  119. static const struct of_device_id berlin_pwm_match[] = {
  120. { .compatible = "marvell,berlin-pwm" },
  121. { },
  122. };
  123. MODULE_DEVICE_TABLE(of, berlin_pwm_match);
  124. static int berlin_pwm_probe(struct platform_device *pdev)
  125. {
  126. struct berlin_pwm_chip *pwm;
  127. struct resource *res;
  128. int ret;
  129. pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL);
  130. if (!pwm)
  131. return -ENOMEM;
  132. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  133. pwm->base = devm_ioremap_resource(&pdev->dev, res);
  134. if (IS_ERR(pwm->base))
  135. return PTR_ERR(pwm->base);
  136. pwm->clk = devm_clk_get(&pdev->dev, NULL);
  137. if (IS_ERR(pwm->clk))
  138. return PTR_ERR(pwm->clk);
  139. ret = clk_prepare_enable(pwm->clk);
  140. if (ret)
  141. return ret;
  142. pwm->chip.dev = &pdev->dev;
  143. pwm->chip.ops = &berlin_pwm_ops;
  144. pwm->chip.base = -1;
  145. pwm->chip.npwm = 4;
  146. pwm->chip.can_sleep = true;
  147. pwm->chip.of_xlate = of_pwm_xlate_with_flags;
  148. pwm->chip.of_pwm_n_cells = 3;
  149. ret = pwmchip_add(&pwm->chip);
  150. if (ret < 0) {
  151. dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret);
  152. clk_disable_unprepare(pwm->clk);
  153. return ret;
  154. }
  155. platform_set_drvdata(pdev, pwm);
  156. return 0;
  157. }
  158. static int berlin_pwm_remove(struct platform_device *pdev)
  159. {
  160. struct berlin_pwm_chip *pwm = platform_get_drvdata(pdev);
  161. int ret;
  162. ret = pwmchip_remove(&pwm->chip);
  163. clk_disable_unprepare(pwm->clk);
  164. return ret;
  165. }
  166. static struct platform_driver berlin_pwm_driver = {
  167. .probe = berlin_pwm_probe,
  168. .remove = berlin_pwm_remove,
  169. .driver = {
  170. .name = "berlin-pwm",
  171. .of_match_table = berlin_pwm_match,
  172. },
  173. };
  174. module_platform_driver(berlin_pwm_driver);
  175. MODULE_AUTHOR("Antoine Tenart <antoine.tenart@free-electrons.com>");
  176. MODULE_DESCRIPTION("Marvell Berlin PWM driver");
  177. MODULE_LICENSE("GPL v2");