exynos_pwm.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2016 Google Inc.
  4. */
  5. #include <common.h>
  6. #include <dm.h>
  7. #include <log.h>
  8. #include <pwm.h>
  9. #include <asm/io.h>
  10. #include <asm/arch/clk.h>
  11. #include <asm/arch/clock.h>
  12. #include <asm/arch/pwm.h>
  13. struct exynos_pwm_priv {
  14. struct s5p_timer *regs;
  15. };
  16. static int exynos_pwm_set_config(struct udevice *dev, uint channel,
  17. uint period_ns, uint duty_ns)
  18. {
  19. struct exynos_pwm_priv *priv = dev_get_priv(dev);
  20. struct s5p_timer *regs = priv->regs;
  21. unsigned int offset, prescaler;
  22. uint div = 4, rate, rate_ns;
  23. u32 val;
  24. u32 tcnt, tcmp, tcon;
  25. if (channel >= 5)
  26. return -EINVAL;
  27. debug("%s: Configure '%s' channel %u, period_ns %u, duty_ns %u\n",
  28. __func__, dev->name, channel, period_ns, duty_ns);
  29. val = readl(&regs->tcfg0);
  30. prescaler = (channel < 2 ? val : (val >> 8)) & 0xff;
  31. div = (readl(&regs->tcfg1) >> MUX_DIV_SHIFT(channel)) & 0xf;
  32. rate = get_pwm_clk() / ((prescaler + 1) * (1 << div));
  33. debug("%s: pwm_clk %lu, rate %u\n", __func__, get_pwm_clk(), rate);
  34. if (channel < 4) {
  35. rate_ns = 1000000000 / rate;
  36. tcnt = period_ns / rate_ns;
  37. tcmp = duty_ns / rate_ns;
  38. debug("%s: tcnt %u, tcmp %u\n", __func__, tcnt, tcmp);
  39. /* Ensure that the comparitor will actually hit the target */
  40. if (tcmp == tcnt)
  41. tcmp = tcnt - 1;
  42. offset = channel * 3;
  43. writel(tcnt, &regs->tcntb0 + offset);
  44. writel(tcmp, &regs->tcmpb0 + offset);
  45. }
  46. tcon = readl(&regs->tcon);
  47. tcon |= TCON_UPDATE(channel);
  48. if (channel < 4)
  49. tcon |= TCON_AUTO_RELOAD(channel);
  50. else
  51. tcon |= TCON4_AUTO_RELOAD;
  52. writel(tcon, &regs->tcon);
  53. tcon &= ~TCON_UPDATE(channel);
  54. writel(tcon, &regs->tcon);
  55. return 0;
  56. }
  57. static int exynos_pwm_set_enable(struct udevice *dev, uint channel,
  58. bool enable)
  59. {
  60. struct exynos_pwm_priv *priv = dev_get_priv(dev);
  61. struct s5p_timer *regs = priv->regs;
  62. u32 mask;
  63. if (channel >= 4)
  64. return -EINVAL;
  65. debug("%s: Enable '%s' channel %u\n", __func__, dev->name, channel);
  66. mask = TCON_START(channel);
  67. clrsetbits_le32(&regs->tcon, mask, enable ? mask : 0);
  68. return 0;
  69. }
  70. static int exynos_pwm_probe(struct udevice *dev)
  71. {
  72. struct exynos_pwm_priv *priv = dev_get_priv(dev);
  73. struct s5p_timer *regs = priv->regs;
  74. writel(PRESCALER_0 | PRESCALER_1 << 8, &regs->tcfg0);
  75. return 0;
  76. }
  77. static int exynos_pwm_of_to_plat(struct udevice *dev)
  78. {
  79. struct exynos_pwm_priv *priv = dev_get_priv(dev);
  80. priv->regs = dev_read_addr_ptr(dev);
  81. return 0;
  82. }
  83. static const struct pwm_ops exynos_pwm_ops = {
  84. .set_config = exynos_pwm_set_config,
  85. .set_enable = exynos_pwm_set_enable,
  86. };
  87. static const struct udevice_id exynos_channels[] = {
  88. { .compatible = "samsung,exynos4210-pwm" },
  89. { }
  90. };
  91. U_BOOT_DRIVER(exynos_pwm) = {
  92. .name = "exynos_pwm",
  93. .id = UCLASS_PWM,
  94. .of_match = exynos_channels,
  95. .ops = &exynos_pwm_ops,
  96. .probe = exynos_pwm_probe,
  97. .of_to_plat = exynos_pwm_of_to_plat,
  98. .priv_auto = sizeof(struct exynos_pwm_priv),
  99. };