pwm-lpc32xx.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. c = 256 * duty_ns;
  45. do_div(c, period_ns);
  46. duty_cycles = c;
  47. writel(PWM_ENABLE | PWM_RELOADV(period_cycles) | PWM_DUTY(duty_cycles),
  48. lpc32xx->base + (pwm->hwpwm << 2));
  49. return 0;
  50. }
  51. static int lpc32xx_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  52. {
  53. struct lpc32xx_pwm_chip *lpc32xx = to_lpc32xx_pwm_chip(chip);
  54. return clk_enable(lpc32xx->clk);
  55. }
  56. static void lpc32xx_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  57. {
  58. struct lpc32xx_pwm_chip *lpc32xx = to_lpc32xx_pwm_chip(chip);
  59. writel(0, lpc32xx->base + (pwm->hwpwm << 2));
  60. clk_disable(lpc32xx->clk);
  61. }
  62. static const struct pwm_ops lpc32xx_pwm_ops = {
  63. .config = lpc32xx_pwm_config,
  64. .enable = lpc32xx_pwm_enable,
  65. .disable = lpc32xx_pwm_disable,
  66. .owner = THIS_MODULE,
  67. };
  68. static int lpc32xx_pwm_probe(struct platform_device *pdev)
  69. {
  70. struct lpc32xx_pwm_chip *lpc32xx;
  71. struct resource *res;
  72. int ret;
  73. lpc32xx = devm_kzalloc(&pdev->dev, sizeof(*lpc32xx), GFP_KERNEL);
  74. if (!lpc32xx)
  75. return -ENOMEM;
  76. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  77. if (!res)
  78. return -EINVAL;
  79. lpc32xx->base = devm_request_and_ioremap(&pdev->dev, res);
  80. if (!lpc32xx->base)
  81. return -EADDRNOTAVAIL;
  82. lpc32xx->clk = devm_clk_get(&pdev->dev, NULL);
  83. if (IS_ERR(lpc32xx->clk))
  84. return PTR_ERR(lpc32xx->clk);
  85. lpc32xx->chip.dev = &pdev->dev;
  86. lpc32xx->chip.ops = &lpc32xx_pwm_ops;
  87. lpc32xx->chip.npwm = 2;
  88. ret = pwmchip_add(&lpc32xx->chip);
  89. if (ret < 0) {
  90. dev_err(&pdev->dev, "failed to add PWM chip, error %d\n", ret);
  91. return ret;
  92. }
  93. platform_set_drvdata(pdev, lpc32xx);
  94. return 0;
  95. }
  96. static int __devexit lpc32xx_pwm_remove(struct platform_device *pdev)
  97. {
  98. struct lpc32xx_pwm_chip *lpc32xx = platform_get_drvdata(pdev);
  99. clk_disable(lpc32xx->clk);
  100. return pwmchip_remove(&lpc32xx->chip);
  101. }
  102. static struct of_device_id lpc32xx_pwm_dt_ids[] = {
  103. { .compatible = "nxp,lpc3220-pwm", },
  104. { /* sentinel */ }
  105. };
  106. MODULE_DEVICE_TABLE(of, lpc32xx_pwm_dt_ids);
  107. static struct platform_driver lpc32xx_pwm_driver = {
  108. .driver = {
  109. .name = "lpc32xx-pwm",
  110. .of_match_table = of_match_ptr(lpc32xx_pwm_dt_ids),
  111. },
  112. .probe = lpc32xx_pwm_probe,
  113. .remove = __devexit_p(lpc32xx_pwm_remove),
  114. };
  115. module_platform_driver(lpc32xx_pwm_driver);
  116. MODULE_ALIAS("platform:lpc32xx-pwm");
  117. MODULE_AUTHOR("Alexandre Pereira da Silva <aletes.xgr@gmail.com>");
  118. MODULE_DESCRIPTION("LPC32XX PWM Driver");
  119. MODULE_LICENSE("GPL v2");