pwm-pxa.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 <[email protected]>
  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. u64 duty_ns, u64 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 = mul_u64_u64_div_u64(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 int pxa_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  96. const struct pwm_state *state)
  97. {
  98. int err;
  99. if (state->polarity != PWM_POLARITY_NORMAL)
  100. return -EINVAL;
  101. if (!state->enabled) {
  102. if (pwm->state.enabled)
  103. pxa_pwm_disable(chip, pwm);
  104. return 0;
  105. }
  106. err = pxa_pwm_config(chip, pwm, state->duty_cycle, state->period);
  107. if (err)
  108. return err;
  109. if (!pwm->state.enabled)
  110. return pxa_pwm_enable(chip, pwm);
  111. return 0;
  112. }
  113. static const struct pwm_ops pxa_pwm_ops = {
  114. .apply = pxa_pwm_apply,
  115. .owner = THIS_MODULE,
  116. };
  117. #ifdef CONFIG_OF
  118. /*
  119. * Device tree users must create one device instance for each PWM channel.
  120. * Hence we dispense with the HAS_SECONDARY_PWM and "tell" the original driver
  121. * code that this is a single channel pxa25x-pwm. Currently all devices are
  122. * supported identically.
  123. */
  124. static const struct of_device_id pwm_of_match[] = {
  125. { .compatible = "marvell,pxa250-pwm", .data = &pwm_id_table[0]},
  126. { .compatible = "marvell,pxa270-pwm", .data = &pwm_id_table[0]},
  127. { .compatible = "marvell,pxa168-pwm", .data = &pwm_id_table[0]},
  128. { .compatible = "marvell,pxa910-pwm", .data = &pwm_id_table[0]},
  129. { }
  130. };
  131. MODULE_DEVICE_TABLE(of, pwm_of_match);
  132. #else
  133. #define pwm_of_match NULL
  134. #endif
  135. static const struct platform_device_id *pxa_pwm_get_id_dt(struct device *dev)
  136. {
  137. const struct of_device_id *id = of_match_device(pwm_of_match, dev);
  138. return id ? id->data : NULL;
  139. }
  140. static int pwm_probe(struct platform_device *pdev)
  141. {
  142. const struct platform_device_id *id = platform_get_device_id(pdev);
  143. struct pxa_pwm_chip *pc;
  144. int ret = 0;
  145. if (IS_ENABLED(CONFIG_OF) && id == NULL)
  146. id = pxa_pwm_get_id_dt(&pdev->dev);
  147. if (id == NULL)
  148. return -EINVAL;
  149. pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
  150. if (pc == NULL)
  151. return -ENOMEM;
  152. pc->clk = devm_clk_get(&pdev->dev, NULL);
  153. if (IS_ERR(pc->clk))
  154. return PTR_ERR(pc->clk);
  155. pc->chip.dev = &pdev->dev;
  156. pc->chip.ops = &pxa_pwm_ops;
  157. pc->chip.npwm = (id->driver_data & HAS_SECONDARY_PWM) ? 2 : 1;
  158. if (IS_ENABLED(CONFIG_OF)) {
  159. pc->chip.of_xlate = of_pwm_single_xlate;
  160. pc->chip.of_pwm_n_cells = 1;
  161. }
  162. pc->mmio_base = devm_platform_ioremap_resource(pdev, 0);
  163. if (IS_ERR(pc->mmio_base))
  164. return PTR_ERR(pc->mmio_base);
  165. ret = devm_pwmchip_add(&pdev->dev, &pc->chip);
  166. if (ret < 0) {
  167. dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
  168. return ret;
  169. }
  170. return 0;
  171. }
  172. static struct platform_driver pwm_driver = {
  173. .driver = {
  174. .name = "pxa25x-pwm",
  175. .of_match_table = pwm_of_match,
  176. },
  177. .probe = pwm_probe,
  178. .id_table = pwm_id_table,
  179. };
  180. module_platform_driver(pwm_driver);
  181. MODULE_LICENSE("GPL v2");