pwm-mxs.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Copyright 2012 Freescale Semiconductor, Inc.
  3. *
  4. * The code contained herein is licensed under the GNU General Public
  5. * License. You may obtain a copy of the GNU General Public License
  6. * Version 2 or later at the following locations:
  7. *
  8. * http://www.opensource.org/licenses/gpl-license.html
  9. * http://www.gnu.org/copyleft/gpl.html
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/err.h>
  13. #include <linux/io.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/of_address.h>
  18. #include <linux/pinctrl/consumer.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/pwm.h>
  21. #include <linux/slab.h>
  22. #include <linux/stmp_device.h>
  23. #define SET 0x4
  24. #define CLR 0x8
  25. #define TOG 0xc
  26. #define PWM_CTRL 0x0
  27. #define PWM_ACTIVE0 0x10
  28. #define PWM_PERIOD0 0x20
  29. #define PERIOD_PERIOD(p) ((p) & 0xffff)
  30. #define PERIOD_PERIOD_MAX 0x10000
  31. #define PERIOD_ACTIVE_HIGH (3 << 16)
  32. #define PERIOD_INACTIVE_LOW (2 << 18)
  33. #define PERIOD_CDIV(div) (((div) & 0x7) << 20)
  34. #define PERIOD_CDIV_MAX 8
  35. struct mxs_pwm_chip {
  36. struct pwm_chip chip;
  37. struct device *dev;
  38. struct clk *clk;
  39. void __iomem *base;
  40. };
  41. #define to_mxs_pwm_chip(_chip) container_of(_chip, struct mxs_pwm_chip, chip)
  42. static int mxs_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  43. int duty_ns, int period_ns)
  44. {
  45. struct mxs_pwm_chip *mxs = to_mxs_pwm_chip(chip);
  46. int ret, div = 0;
  47. unsigned int period_cycles, duty_cycles;
  48. unsigned long rate;
  49. unsigned long long c;
  50. rate = clk_get_rate(mxs->clk);
  51. while (1) {
  52. c = rate / (1 << div);
  53. c = c * period_ns;
  54. do_div(c, 1000000000);
  55. if (c < PERIOD_PERIOD_MAX)
  56. break;
  57. div++;
  58. if (div > PERIOD_CDIV_MAX)
  59. return -EINVAL;
  60. }
  61. period_cycles = c;
  62. c *= duty_ns;
  63. do_div(c, period_ns);
  64. duty_cycles = c;
  65. /*
  66. * If the PWM channel is disabled, make sure to turn on the clock
  67. * before writing the register. Otherwise, keep it enabled.
  68. */
  69. if (!test_bit(PWMF_ENABLED, &pwm->flags)) {
  70. ret = clk_prepare_enable(mxs->clk);
  71. if (ret)
  72. return ret;
  73. }
  74. writel(duty_cycles << 16,
  75. mxs->base + PWM_ACTIVE0 + pwm->hwpwm * 0x20);
  76. writel(PERIOD_PERIOD(period_cycles) | PERIOD_ACTIVE_HIGH |
  77. PERIOD_INACTIVE_LOW | PERIOD_CDIV(div),
  78. mxs->base + PWM_PERIOD0 + pwm->hwpwm * 0x20);
  79. /*
  80. * If the PWM is not enabled, turn the clock off again to save power.
  81. */
  82. if (!test_bit(PWMF_ENABLED, &pwm->flags))
  83. clk_disable_unprepare(mxs->clk);
  84. return 0;
  85. }
  86. static int mxs_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  87. {
  88. struct mxs_pwm_chip *mxs = to_mxs_pwm_chip(chip);
  89. int ret;
  90. ret = clk_prepare_enable(mxs->clk);
  91. if (ret)
  92. return ret;
  93. writel(1 << pwm->hwpwm, mxs->base + PWM_CTRL + SET);
  94. return 0;
  95. }
  96. static void mxs_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  97. {
  98. struct mxs_pwm_chip *mxs = to_mxs_pwm_chip(chip);
  99. writel(1 << pwm->hwpwm, mxs->base + PWM_CTRL + CLR);
  100. clk_disable_unprepare(mxs->clk);
  101. }
  102. static const struct pwm_ops mxs_pwm_ops = {
  103. .config = mxs_pwm_config,
  104. .enable = mxs_pwm_enable,
  105. .disable = mxs_pwm_disable,
  106. .owner = THIS_MODULE,
  107. };
  108. static int mxs_pwm_probe(struct platform_device *pdev)
  109. {
  110. struct device_node *np = pdev->dev.of_node;
  111. struct mxs_pwm_chip *mxs;
  112. struct resource *res;
  113. struct pinctrl *pinctrl;
  114. int ret;
  115. mxs = devm_kzalloc(&pdev->dev, sizeof(*mxs), GFP_KERNEL);
  116. if (!mxs)
  117. return -ENOMEM;
  118. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  119. mxs->base = devm_ioremap_resource(&pdev->dev, res);
  120. if (IS_ERR(mxs->base))
  121. return PTR_ERR(mxs->base);
  122. pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
  123. if (IS_ERR(pinctrl))
  124. return PTR_ERR(pinctrl);
  125. mxs->clk = devm_clk_get(&pdev->dev, NULL);
  126. if (IS_ERR(mxs->clk))
  127. return PTR_ERR(mxs->clk);
  128. mxs->chip.dev = &pdev->dev;
  129. mxs->chip.ops = &mxs_pwm_ops;
  130. mxs->chip.base = -1;
  131. ret = of_property_read_u32(np, "fsl,pwm-number", &mxs->chip.npwm);
  132. if (ret < 0) {
  133. dev_err(&pdev->dev, "failed to get pwm number: %d\n", ret);
  134. return ret;
  135. }
  136. ret = pwmchip_add(&mxs->chip);
  137. if (ret < 0) {
  138. dev_err(&pdev->dev, "failed to add pwm chip %d\n", ret);
  139. return ret;
  140. }
  141. mxs->dev = &pdev->dev;
  142. platform_set_drvdata(pdev, mxs);
  143. stmp_reset_block(mxs->base);
  144. return 0;
  145. }
  146. static int mxs_pwm_remove(struct platform_device *pdev)
  147. {
  148. struct mxs_pwm_chip *mxs = platform_get_drvdata(pdev);
  149. return pwmchip_remove(&mxs->chip);
  150. }
  151. static struct of_device_id mxs_pwm_dt_ids[] = {
  152. { .compatible = "fsl,imx23-pwm", },
  153. { /* sentinel */ }
  154. };
  155. MODULE_DEVICE_TABLE(of, mxs_pwm_dt_ids);
  156. static struct platform_driver mxs_pwm_driver = {
  157. .driver = {
  158. .name = "mxs-pwm",
  159. .of_match_table = of_match_ptr(mxs_pwm_dt_ids),
  160. },
  161. .probe = mxs_pwm_probe,
  162. .remove = mxs_pwm_remove,
  163. };
  164. module_platform_driver(mxs_pwm_driver);
  165. MODULE_ALIAS("platform:mxs-pwm");
  166. MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
  167. MODULE_DESCRIPTION("Freescale MXS PWM Driver");
  168. MODULE_LICENSE("GPL v2");