pwm-mxs.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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/platform_device.h>
  19. #include <linux/pwm.h>
  20. #include <linux/slab.h>
  21. #include <linux/stmp_device.h>
  22. #define SET 0x4
  23. #define CLR 0x8
  24. #define TOG 0xc
  25. #define PWM_CTRL 0x0
  26. #define PWM_ACTIVE0 0x10
  27. #define PWM_PERIOD0 0x20
  28. #define PERIOD_PERIOD(p) ((p) & 0xffff)
  29. #define PERIOD_PERIOD_MAX 0x10000
  30. #define PERIOD_ACTIVE_HIGH (3 << 16)
  31. #define PERIOD_INACTIVE_LOW (2 << 18)
  32. #define PERIOD_CDIV(div) (((div) & 0x7) << 20)
  33. #define PERIOD_CDIV_MAX 8
  34. static const unsigned int cdiv[PERIOD_CDIV_MAX] = {
  35. 1, 2, 4, 8, 16, 64, 256, 1024
  36. };
  37. struct mxs_pwm_chip {
  38. struct pwm_chip chip;
  39. struct clk *clk;
  40. void __iomem *base;
  41. };
  42. #define to_mxs_pwm_chip(_chip) container_of(_chip, struct mxs_pwm_chip, chip)
  43. static int mxs_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  44. int duty_ns, int period_ns)
  45. {
  46. struct mxs_pwm_chip *mxs = to_mxs_pwm_chip(chip);
  47. int ret, div = 0;
  48. unsigned int period_cycles, duty_cycles;
  49. unsigned long rate;
  50. unsigned long long c;
  51. rate = clk_get_rate(mxs->clk);
  52. while (1) {
  53. c = rate / cdiv[div];
  54. c = c * period_ns;
  55. do_div(c, 1000000000);
  56. if (c < PERIOD_PERIOD_MAX)
  57. break;
  58. div++;
  59. if (div >= PERIOD_CDIV_MAX)
  60. return -EINVAL;
  61. }
  62. period_cycles = c;
  63. c *= duty_ns;
  64. do_div(c, period_ns);
  65. duty_cycles = c;
  66. /*
  67. * If the PWM channel is disabled, make sure to turn on the clock
  68. * before writing the register. Otherwise, keep it enabled.
  69. */
  70. if (!pwm_is_enabled(pwm)) {
  71. ret = clk_prepare_enable(mxs->clk);
  72. if (ret)
  73. return ret;
  74. }
  75. writel(duty_cycles << 16,
  76. mxs->base + PWM_ACTIVE0 + pwm->hwpwm * 0x20);
  77. writel(PERIOD_PERIOD(period_cycles) | PERIOD_ACTIVE_HIGH |
  78. PERIOD_INACTIVE_LOW | PERIOD_CDIV(div),
  79. mxs->base + PWM_PERIOD0 + pwm->hwpwm * 0x20);
  80. /*
  81. * If the PWM is not enabled, turn the clock off again to save power.
  82. */
  83. if (!pwm_is_enabled(pwm))
  84. clk_disable_unprepare(mxs->clk);
  85. return 0;
  86. }
  87. static int mxs_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  88. {
  89. struct mxs_pwm_chip *mxs = to_mxs_pwm_chip(chip);
  90. int ret;
  91. ret = clk_prepare_enable(mxs->clk);
  92. if (ret)
  93. return ret;
  94. writel(1 << pwm->hwpwm, mxs->base + PWM_CTRL + SET);
  95. return 0;
  96. }
  97. static void mxs_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  98. {
  99. struct mxs_pwm_chip *mxs = to_mxs_pwm_chip(chip);
  100. writel(1 << pwm->hwpwm, mxs->base + PWM_CTRL + CLR);
  101. clk_disable_unprepare(mxs->clk);
  102. }
  103. static const struct pwm_ops mxs_pwm_ops = {
  104. .config = mxs_pwm_config,
  105. .enable = mxs_pwm_enable,
  106. .disable = mxs_pwm_disable,
  107. .owner = THIS_MODULE,
  108. };
  109. static int mxs_pwm_probe(struct platform_device *pdev)
  110. {
  111. struct device_node *np = pdev->dev.of_node;
  112. struct mxs_pwm_chip *mxs;
  113. struct resource *res;
  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. mxs->clk = devm_clk_get(&pdev->dev, NULL);
  123. if (IS_ERR(mxs->clk))
  124. return PTR_ERR(mxs->clk);
  125. mxs->chip.dev = &pdev->dev;
  126. mxs->chip.ops = &mxs_pwm_ops;
  127. mxs->chip.base = -1;
  128. mxs->chip.can_sleep = true;
  129. ret = of_property_read_u32(np, "fsl,pwm-number", &mxs->chip.npwm);
  130. if (ret < 0) {
  131. dev_err(&pdev->dev, "failed to get pwm number: %d\n", ret);
  132. return ret;
  133. }
  134. ret = pwmchip_add(&mxs->chip);
  135. if (ret < 0) {
  136. dev_err(&pdev->dev, "failed to add pwm chip %d\n", ret);
  137. return ret;
  138. }
  139. platform_set_drvdata(pdev, mxs);
  140. ret = stmp_reset_block(mxs->base);
  141. if (ret)
  142. goto pwm_remove;
  143. return 0;
  144. pwm_remove:
  145. pwmchip_remove(&mxs->chip);
  146. return ret;
  147. }
  148. static int mxs_pwm_remove(struct platform_device *pdev)
  149. {
  150. struct mxs_pwm_chip *mxs = platform_get_drvdata(pdev);
  151. return pwmchip_remove(&mxs->chip);
  152. }
  153. static const struct of_device_id mxs_pwm_dt_ids[] = {
  154. { .compatible = "fsl,imx23-pwm", },
  155. { /* sentinel */ }
  156. };
  157. MODULE_DEVICE_TABLE(of, mxs_pwm_dt_ids);
  158. static struct platform_driver mxs_pwm_driver = {
  159. .driver = {
  160. .name = "mxs-pwm",
  161. .of_match_table = mxs_pwm_dt_ids,
  162. },
  163. .probe = mxs_pwm_probe,
  164. .remove = mxs_pwm_remove,
  165. };
  166. module_platform_driver(mxs_pwm_driver);
  167. MODULE_ALIAS("platform:mxs-pwm");
  168. MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
  169. MODULE_DESCRIPTION("Freescale MXS PWM Driver");
  170. MODULE_LICENSE("GPL v2");