exynos_pwm.c 2.8 KB

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