pwm-lpc32xx.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright 2012 Alexandre Pereira da Silva <aletes.xgr@gmail.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; version 2.
  7. *
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/err.h>
  11. #include <linux/io.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/of_address.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/pwm.h>
  18. #include <linux/slab.h>
  19. struct lpc32xx_pwm_chip {
  20. struct pwm_chip chip;
  21. struct clk *clk;
  22. void __iomem *base;
  23. };
  24. #define PWM_ENABLE (1 << 31)
  25. #define PWM_RELOADV(x) (((x) & 0xFF) << 8)
  26. #define PWM_DUTY(x) ((x) & 0xFF)
  27. #define to_lpc32xx_pwm_chip(_chip) \
  28. container_of(_chip, struct lpc32xx_pwm_chip, chip)
  29. static int lpc32xx_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  30. int duty_ns, int period_ns)
  31. {
  32. struct lpc32xx_pwm_chip *lpc32xx = to_lpc32xx_pwm_chip(chip);
  33. unsigned long long c;
  34. int period_cycles, duty_cycles;
  35. u32 val;
  36. c = clk_get_rate(lpc32xx->clk) / 256;
  37. c = c * period_ns;
  38. do_div(c, NSEC_PER_SEC);
  39. /* Handle high and low extremes */
  40. if (c == 0)
  41. c = 1;
  42. if (c > 255)
  43. c = 0; /* 0 set division by 256 */
  44. period_cycles = c;
  45. /* The duty-cycle value is as follows:
  46. *
  47. * DUTY-CYCLE HIGH LEVEL
  48. * 1 99.9%
  49. * 25 90.0%
  50. * 128 50.0%
  51. * 220 10.0%
  52. * 255 0.1%
  53. * 0 0.0%
  54. *
  55. * In other words, the register value is duty-cycle % 256 with
  56. * duty-cycle in the range 1-256.
  57. */
  58. c = 256 * duty_ns;
  59. do_div(c, period_ns);
  60. if (c > 255)
  61. c = 255;
  62. duty_cycles = 256 - c;
  63. val = readl(lpc32xx->base + (pwm->hwpwm << 2));
  64. val &= ~0xFFFF;
  65. val |= PWM_RELOADV(period_cycles) | PWM_DUTY(duty_cycles);
  66. writel(val, lpc32xx->base + (pwm->hwpwm << 2));
  67. return 0;
  68. }
  69. static int lpc32xx_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  70. {
  71. struct lpc32xx_pwm_chip *lpc32xx = to_lpc32xx_pwm_chip(chip);
  72. u32 val;
  73. int ret;
  74. ret = clk_enable(lpc32xx->clk);
  75. if (ret)
  76. return ret;
  77. val = readl(lpc32xx->base + (pwm->hwpwm << 2));
  78. val |= PWM_ENABLE;
  79. writel(val, lpc32xx->base + (pwm->hwpwm << 2));
  80. return 0;
  81. }
  82. static void lpc32xx_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  83. {
  84. struct lpc32xx_pwm_chip *lpc32xx = to_lpc32xx_pwm_chip(chip);
  85. u32 val;
  86. val = readl(lpc32xx->base + (pwm->hwpwm << 2));
  87. val &= ~PWM_ENABLE;
  88. writel(val, lpc32xx->base + (pwm->hwpwm << 2));
  89. clk_disable(lpc32xx->clk);
  90. }
  91. static const struct pwm_ops lpc32xx_pwm_ops = {
  92. .config = lpc32xx_pwm_config,
  93. .enable = lpc32xx_pwm_enable,
  94. .disable = lpc32xx_pwm_disable,
  95. .owner = THIS_MODULE,
  96. };
  97. static int lpc32xx_pwm_probe(struct platform_device *pdev)
  98. {
  99. struct lpc32xx_pwm_chip *lpc32xx;
  100. struct resource *res;
  101. int ret;
  102. lpc32xx = devm_kzalloc(&pdev->dev, sizeof(*lpc32xx), GFP_KERNEL);
  103. if (!lpc32xx)
  104. return -ENOMEM;
  105. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  106. lpc32xx->base = devm_ioremap_resource(&pdev->dev, res);
  107. if (IS_ERR(lpc32xx->base))
  108. return PTR_ERR(lpc32xx->base);
  109. lpc32xx->clk = devm_clk_get(&pdev->dev, NULL);
  110. if (IS_ERR(lpc32xx->clk))
  111. return PTR_ERR(lpc32xx->clk);
  112. lpc32xx->chip.dev = &pdev->dev;
  113. lpc32xx->chip.ops = &lpc32xx_pwm_ops;
  114. lpc32xx->chip.npwm = 2;
  115. lpc32xx->chip.base = -1;
  116. ret = pwmchip_add(&lpc32xx->chip);
  117. if (ret < 0) {
  118. dev_err(&pdev->dev, "failed to add PWM chip, error %d\n", ret);
  119. return ret;
  120. }
  121. platform_set_drvdata(pdev, lpc32xx);
  122. return 0;
  123. }
  124. static int lpc32xx_pwm_remove(struct platform_device *pdev)
  125. {
  126. struct lpc32xx_pwm_chip *lpc32xx = platform_get_drvdata(pdev);
  127. unsigned int i;
  128. for (i = 0; i < lpc32xx->chip.npwm; i++)
  129. pwm_disable(&lpc32xx->chip.pwms[i]);
  130. return pwmchip_remove(&lpc32xx->chip);
  131. }
  132. static const struct of_device_id lpc32xx_pwm_dt_ids[] = {
  133. { .compatible = "nxp,lpc3220-pwm", },
  134. { /* sentinel */ }
  135. };
  136. MODULE_DEVICE_TABLE(of, lpc32xx_pwm_dt_ids);
  137. static struct platform_driver lpc32xx_pwm_driver = {
  138. .driver = {
  139. .name = "lpc32xx-pwm",
  140. .of_match_table = lpc32xx_pwm_dt_ids,
  141. },
  142. .probe = lpc32xx_pwm_probe,
  143. .remove = lpc32xx_pwm_remove,
  144. };
  145. module_platform_driver(lpc32xx_pwm_driver);
  146. MODULE_ALIAS("platform:lpc32xx-pwm");
  147. MODULE_AUTHOR("Alexandre Pereira da Silva <aletes.xgr@gmail.com>");
  148. MODULE_DESCRIPTION("LPC32XX PWM Driver");
  149. MODULE_LICENSE("GPL v2");