pwm-pxa.c 5.3 KB

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