1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2016 Google Inc.
10 #include <asm/arch/clk.h>
11 #include <asm/arch/clock.h>
12 #include <asm/arch/pwm.h>
14 struct exynos_pwm_priv {
15 struct s5p_timer *regs;
18 static int exynos_pwm_set_config(struct udevice *dev, uint channel,
19 uint period_ns, uint duty_ns)
21 struct exynos_pwm_priv *priv = dev_get_priv(dev);
22 struct s5p_timer *regs = priv->regs;
23 unsigned int offset, prescaler;
24 uint div = 4, rate, rate_ns;
30 debug("%s: Configure '%s' channel %u, period_ns %u, duty_ns %u\n",
31 __func__, dev->name, channel, period_ns, duty_ns);
33 val = readl(®s->tcfg0);
34 prescaler = (channel < 2 ? val : (val >> 8)) & 0xff;
35 div = (readl(®s->tcfg1) >> MUX_DIV_SHIFT(channel)) & 0xf;
37 rate = get_pwm_clk() / ((prescaler + 1) * (1 << div));
38 debug("%s: pwm_clk %lu, rate %u\n", __func__, get_pwm_clk(), rate);
41 rate_ns = 1000000000 / rate;
42 tcnt = period_ns / rate_ns;
43 tcmp = duty_ns / rate_ns;
44 debug("%s: tcnt %u, tcmp %u\n", __func__, tcnt, tcmp);
46 writel(tcnt, ®s->tcntb0 + offset);
47 writel(tcmp, ®s->tcmpb0 + offset);
50 tcon = readl(®s->tcon);
51 tcon |= TCON_UPDATE(channel);
53 tcon |= TCON_AUTO_RELOAD(channel);
55 tcon |= TCON4_AUTO_RELOAD;
56 writel(tcon, ®s->tcon);
58 tcon &= ~TCON_UPDATE(channel);
59 writel(tcon, ®s->tcon);
64 static int exynos_pwm_set_enable(struct udevice *dev, uint channel,
67 struct exynos_pwm_priv *priv = dev_get_priv(dev);
68 struct s5p_timer *regs = priv->regs;
73 debug("%s: Enable '%s' channel %u\n", __func__, dev->name, channel);
74 mask = TCON_START(channel);
75 clrsetbits_le32(®s->tcon, mask, enable ? mask : 0);
80 static int exynos_pwm_probe(struct udevice *dev)
82 struct exynos_pwm_priv *priv = dev_get_priv(dev);
83 struct s5p_timer *regs = priv->regs;
85 writel(PRESCALER_0 | PRESCALER_1 << 8, ®s->tcfg0);
90 static int exynos_pwm_ofdata_to_platdata(struct udevice *dev)
92 struct exynos_pwm_priv *priv = dev_get_priv(dev);
94 priv->regs = (struct s5p_timer *)devfdt_get_addr(dev);
99 static const struct pwm_ops exynos_pwm_ops = {
100 .set_config = exynos_pwm_set_config,
101 .set_enable = exynos_pwm_set_enable,
104 static const struct udevice_id exynos_channels[] = {
105 { .compatible = "samsung,exynos4210-pwm" },
109 U_BOOT_DRIVER(exynos_pwm) = {
110 .name = "exynos_pwm",
112 .of_match = exynos_channels,
113 .ops = &exynos_pwm_ops,
114 .probe = exynos_pwm_probe,
115 .ofdata_to_platdata = exynos_pwm_ofdata_to_platdata,
116 .priv_auto_alloc_size = sizeof(struct exynos_pwm_priv),