exynos_pwm.c 2.8 KB

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