pwm-pxa.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * drivers/pwm/pwm-pxa.c
  3. *
  4. * simple driver for PWM (Pulse Width Modulator) controller
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * 2008-02-13 initial version
  11. * eric miao <eric.miao@marvell.com>
  12. */
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/slab.h>
  17. #include <linux/err.h>
  18. #include <linux/clk.h>
  19. #include <linux/io.h>
  20. #include <linux/pwm.h>
  21. #include <linux/of_device.h>
  22. #include <asm/div64.h>
  23. #define HAS_SECONDARY_PWM 0x10
  24. static const struct platform_device_id pwm_id_table[] = {
  25. /* PWM has_secondary_pwm? */
  26. { "pxa25x-pwm", 0 },
  27. { "pxa27x-pwm", HAS_SECONDARY_PWM },
  28. { "pxa168-pwm", 0 },
  29. { "pxa910-pwm", 0 },
  30. { },
  31. };
  32. MODULE_DEVICE_TABLE(platform, pwm_id_table);
  33. /* PWM registers and bits definitions */
  34. #define PWMCR (0x00)
  35. #define PWMDCR (0x04)
  36. #define PWMPCR (0x08)
  37. #define PWMCR_SD (1 << 6)
  38. #define PWMDCR_FD (1 << 10)
  39. struct pxa_pwm_chip {
  40. struct pwm_chip chip;
  41. struct device *dev;
  42. struct clk *clk;
  43. void __iomem *mmio_base;
  44. };
  45. static inline struct pxa_pwm_chip *to_pxa_pwm_chip(struct pwm_chip *chip)
  46. {
  47. return container_of(chip, struct pxa_pwm_chip, chip);
  48. }
  49. /*
  50. * period_ns = 10^9 * (PRESCALE + 1) * (PV + 1) / PWM_CLK_RATE
  51. * duty_ns = 10^9 * (PRESCALE + 1) * DC / PWM_CLK_RATE
  52. */
  53. static int pxa_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  54. int duty_ns, int period_ns)
  55. {
  56. struct pxa_pwm_chip *pc = to_pxa_pwm_chip(chip);
  57. unsigned long long c;
  58. unsigned long period_cycles, prescale, pv, dc;
  59. unsigned long offset;
  60. int rc;
  61. offset = pwm->hwpwm ? 0x10 : 0;
  62. c = clk_get_rate(pc->clk);
  63. c = c * period_ns;
  64. do_div(c, 1000000000);
  65. period_cycles = c;
  66. if (period_cycles < 1)
  67. period_cycles = 1;
  68. prescale = (period_cycles - 1) / 1024;
  69. pv = period_cycles / (prescale + 1) - 1;
  70. if (prescale > 63)
  71. return -EINVAL;
  72. if (duty_ns == period_ns)
  73. dc = PWMDCR_FD;
  74. else
  75. dc = (pv + 1) * duty_ns / period_ns;
  76. /* NOTE: the clock to PWM has to be enabled first
  77. * before writing to the registers
  78. */
  79. rc = clk_prepare_enable(pc->clk);
  80. if (rc < 0)
  81. return rc;
  82. writel(prescale, pc->mmio_base + offset + PWMCR);
  83. writel(dc, pc->mmio_base + offset + PWMDCR);
  84. writel(pv, pc->mmio_base + offset + PWMPCR);
  85. clk_disable_unprepare(pc->clk);
  86. return 0;
  87. }
  88. static int pxa_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  89. {
  90. struct pxa_pwm_chip *pc = to_pxa_pwm_chip(chip);
  91. return clk_prepare_enable(pc->clk);
  92. }
  93. static void pxa_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  94. {
  95. struct pxa_pwm_chip *pc = to_pxa_pwm_chip(chip);
  96. clk_disable_unprepare(pc->clk);
  97. }
  98. static struct pwm_ops pxa_pwm_ops = {
  99. .config = pxa_pwm_config,
  100. .enable = pxa_pwm_enable,
  101. .disable = pxa_pwm_disable,
  102. .owner = THIS_MODULE,
  103. };
  104. #ifdef CONFIG_OF
  105. /*
  106. * Device tree users must create one device instance for each PWM channel.
  107. * Hence we dispense with the HAS_SECONDARY_PWM and "tell" the original driver
  108. * code that this is a single channel pxa25x-pwm. Currently all devices are
  109. * supported identically.
  110. */
  111. static const struct of_device_id pwm_of_match[] = {
  112. { .compatible = "marvell,pxa250-pwm", .data = &pwm_id_table[0]},
  113. { .compatible = "marvell,pxa270-pwm", .data = &pwm_id_table[0]},
  114. { .compatible = "marvell,pxa168-pwm", .data = &pwm_id_table[0]},
  115. { .compatible = "marvell,pxa910-pwm", .data = &pwm_id_table[0]},
  116. { }
  117. };
  118. MODULE_DEVICE_TABLE(of, pwm_of_match);
  119. #else
  120. #define pwm_of_match NULL
  121. #endif
  122. static const struct platform_device_id *pxa_pwm_get_id_dt(struct device *dev)
  123. {
  124. const struct of_device_id *id = of_match_device(pwm_of_match, dev);
  125. return id ? id->data : NULL;
  126. }
  127. static struct pwm_device *
  128. pxa_pwm_of_xlate(struct pwm_chip *pc, const struct of_phandle_args *args)
  129. {
  130. struct pwm_device *pwm;
  131. pwm = pwm_request_from_chip(pc, 0, NULL);
  132. if (IS_ERR(pwm))
  133. return pwm;
  134. pwm->args.period = args->args[0];
  135. return pwm;
  136. }
  137. static int pwm_probe(struct platform_device *pdev)
  138. {
  139. const struct platform_device_id *id = platform_get_device_id(pdev);
  140. struct pxa_pwm_chip *pwm;
  141. struct resource *r;
  142. int ret = 0;
  143. if (IS_ENABLED(CONFIG_OF) && id == NULL)
  144. id = pxa_pwm_get_id_dt(&pdev->dev);
  145. if (id == NULL)
  146. return -EINVAL;
  147. pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL);
  148. if (pwm == NULL)
  149. return -ENOMEM;
  150. pwm->clk = devm_clk_get(&pdev->dev, NULL);
  151. if (IS_ERR(pwm->clk))
  152. return PTR_ERR(pwm->clk);
  153. pwm->chip.dev = &pdev->dev;
  154. pwm->chip.ops = &pxa_pwm_ops;
  155. pwm->chip.base = -1;
  156. pwm->chip.npwm = (id->driver_data & HAS_SECONDARY_PWM) ? 2 : 1;
  157. if (IS_ENABLED(CONFIG_OF)) {
  158. pwm->chip.of_xlate = pxa_pwm_of_xlate;
  159. pwm->chip.of_pwm_n_cells = 1;
  160. }
  161. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  162. pwm->mmio_base = devm_ioremap_resource(&pdev->dev, r);
  163. if (IS_ERR(pwm->mmio_base))
  164. return PTR_ERR(pwm->mmio_base);
  165. ret = pwmchip_add(&pwm->chip);
  166. if (ret < 0) {
  167. dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
  168. return ret;
  169. }
  170. platform_set_drvdata(pdev, pwm);
  171. return 0;
  172. }
  173. static int pwm_remove(struct platform_device *pdev)
  174. {
  175. struct pxa_pwm_chip *chip;
  176. chip = platform_get_drvdata(pdev);
  177. if (chip == NULL)
  178. return -ENODEV;
  179. return pwmchip_remove(&chip->chip);
  180. }
  181. static struct platform_driver pwm_driver = {
  182. .driver = {
  183. .name = "pxa25x-pwm",
  184. .of_match_table = pwm_of_match,
  185. },
  186. .probe = pwm_probe,
  187. .remove = pwm_remove,
  188. .id_table = pwm_id_table,
  189. };
  190. module_platform_driver(pwm_driver);
  191. MODULE_LICENSE("GPL v2");