pwm-lpc32xx.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. c = clk_get_rate(lpc32xx->clk) / 256;
  36. c = c * period_ns;
  37. do_div(c, NSEC_PER_SEC);
  38. /* Handle high and low extremes */
  39. if (c == 0)
  40. c = 1;
  41. if (c > 255)
  42. c = 0; /* 0 set division by 256 */
  43. period_cycles = c;
  44. /* The duty-cycle value is as follows:
  45. *
  46. * DUTY-CYCLE HIGH LEVEL
  47. * 1 99.9%
  48. * 25 90.0%
  49. * 128 50.0%
  50. * 220 10.0%
  51. * 255 0.1%
  52. * 0 0.0%
  53. *
  54. * In other words, the register value is duty-cycle % 256 with
  55. * duty-cycle in the range 1-256.
  56. */
  57. c = 256 * duty_ns;
  58. do_div(c, period_ns);
  59. if (c > 255)
  60. c = 255;
  61. duty_cycles = 256 - c;
  62. writel(PWM_ENABLE | PWM_RELOADV(period_cycles) | PWM_DUTY(duty_cycles),
  63. lpc32xx->base + (pwm->hwpwm << 2));
  64. return 0;
  65. }
  66. static int lpc32xx_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  67. {
  68. struct lpc32xx_pwm_chip *lpc32xx = to_lpc32xx_pwm_chip(chip);
  69. return clk_enable(lpc32xx->clk);
  70. }
  71. static void lpc32xx_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  72. {
  73. struct lpc32xx_pwm_chip *lpc32xx = to_lpc32xx_pwm_chip(chip);
  74. writel(0, lpc32xx->base + (pwm->hwpwm << 2));
  75. clk_disable(lpc32xx->clk);
  76. }
  77. static const struct pwm_ops lpc32xx_pwm_ops = {
  78. .config = lpc32xx_pwm_config,
  79. .enable = lpc32xx_pwm_enable,
  80. .disable = lpc32xx_pwm_disable,
  81. .owner = THIS_MODULE,
  82. };
  83. static int lpc32xx_pwm_probe(struct platform_device *pdev)
  84. {
  85. struct lpc32xx_pwm_chip *lpc32xx;
  86. struct resource *res;
  87. int ret;
  88. lpc32xx = devm_kzalloc(&pdev->dev, sizeof(*lpc32xx), GFP_KERNEL);
  89. if (!lpc32xx)
  90. return -ENOMEM;
  91. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  92. if (!res)
  93. return -EINVAL;
  94. lpc32xx->base = devm_ioremap_resource(&pdev->dev, res);
  95. if (IS_ERR(lpc32xx->base))
  96. return PTR_ERR(lpc32xx->base);
  97. lpc32xx->clk = devm_clk_get(&pdev->dev, NULL);
  98. if (IS_ERR(lpc32xx->clk))
  99. return PTR_ERR(lpc32xx->clk);
  100. lpc32xx->chip.dev = &pdev->dev;
  101. lpc32xx->chip.ops = &lpc32xx_pwm_ops;
  102. lpc32xx->chip.npwm = 2;
  103. lpc32xx->chip.base = -1;
  104. ret = pwmchip_add(&lpc32xx->chip);
  105. if (ret < 0) {
  106. dev_err(&pdev->dev, "failed to add PWM chip, error %d\n", ret);
  107. return ret;
  108. }
  109. platform_set_drvdata(pdev, lpc32xx);
  110. return 0;
  111. }
  112. static int lpc32xx_pwm_remove(struct platform_device *pdev)
  113. {
  114. struct lpc32xx_pwm_chip *lpc32xx = platform_get_drvdata(pdev);
  115. unsigned int i;
  116. for (i = 0; i < lpc32xx->chip.npwm; i++)
  117. pwm_disable(&lpc32xx->chip.pwms[i]);
  118. return pwmchip_remove(&lpc32xx->chip);
  119. }
  120. static struct of_device_id lpc32xx_pwm_dt_ids[] = {
  121. { .compatible = "nxp,lpc3220-pwm", },
  122. { /* sentinel */ }
  123. };
  124. MODULE_DEVICE_TABLE(of, lpc32xx_pwm_dt_ids);
  125. static struct platform_driver lpc32xx_pwm_driver = {
  126. .driver = {
  127. .name = "lpc32xx-pwm",
  128. .of_match_table = of_match_ptr(lpc32xx_pwm_dt_ids),
  129. },
  130. .probe = lpc32xx_pwm_probe,
  131. .remove = lpc32xx_pwm_remove,
  132. };
  133. module_platform_driver(lpc32xx_pwm_driver);
  134. MODULE_ALIAS("platform:lpc32xx-pwm");
  135. MODULE_AUTHOR("Alexandre Pereira da Silva <aletes.xgr@gmail.com>");
  136. MODULE_DESCRIPTION("LPC32XX PWM Driver");
  137. MODULE_LICENSE("GPL v2");