1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2016 Google Inc.
11 #include <asm/arch/clk.h>
12 #include <asm/arch/clock.h>
13 #include <asm/arch/pwm.h>
15 struct exynos_pwm_priv {
16 struct s5p_timer *regs;
19 static int exynos_pwm_set_config(struct udevice *dev, uint channel,
20 uint period_ns, uint duty_ns)
22 struct exynos_pwm_priv *priv = dev_get_priv(dev);
23 struct s5p_timer *regs = priv->regs;
24 unsigned int offset, prescaler;
25 uint div = 4, rate, rate_ns;
31 debug("%s: Configure '%s' channel %u, period_ns %u, duty_ns %u\n",
32 __func__, dev->name, channel, period_ns, duty_ns);
34 val = readl(®s->tcfg0);
35 prescaler = (channel < 2 ? val : (val >> 8)) & 0xff;
36 div = (readl(®s->tcfg1) >> MUX_DIV_SHIFT(channel)) & 0xf;
38 rate = get_pwm_clk() / ((prescaler + 1) * (1 << div));
39 debug("%s: pwm_clk %lu, rate %u\n", __func__, get_pwm_clk(), rate);
42 rate_ns = 1000000000 / rate;
43 tcnt = period_ns / rate_ns;
44 tcmp = duty_ns / rate_ns;
45 debug("%s: tcnt %u, tcmp %u\n", __func__, tcnt, tcmp);
47 writel(tcnt, ®s->tcntb0 + offset);
48 writel(tcmp, ®s->tcmpb0 + offset);
51 tcon = readl(®s->tcon);
52 tcon |= TCON_UPDATE(channel);
54 tcon |= TCON_AUTO_RELOAD(channel);
56 tcon |= TCON4_AUTO_RELOAD;
57 writel(tcon, ®s->tcon);
59 tcon &= ~TCON_UPDATE(channel);
60 writel(tcon, ®s->tcon);
65 static int exynos_pwm_set_enable(struct udevice *dev, uint channel,
68 struct exynos_pwm_priv *priv = dev_get_priv(dev);
69 struct s5p_timer *regs = priv->regs;
74 debug("%s: Enable '%s' channel %u\n", __func__, dev->name, channel);
75 mask = TCON_START(channel);
76 clrsetbits_le32(®s->tcon, mask, enable ? mask : 0);
81 static int exynos_pwm_probe(struct udevice *dev)
83 struct exynos_pwm_priv *priv = dev_get_priv(dev);
84 struct s5p_timer *regs = priv->regs;
86 writel(PRESCALER_0 | PRESCALER_1 << 8, ®s->tcfg0);
91 static int exynos_pwm_ofdata_to_platdata(struct udevice *dev)
93 struct exynos_pwm_priv *priv = dev_get_priv(dev);
95 priv->regs = dev_read_addr_ptr(dev);
100 static const struct pwm_ops exynos_pwm_ops = {
101 .set_config = exynos_pwm_set_config,
102 .set_enable = exynos_pwm_set_enable,
105 static const struct udevice_id exynos_channels[] = {
106 { .compatible = "samsung,exynos4210-pwm" },
110 U_BOOT_DRIVER(exynos_pwm) = {
111 .name = "exynos_pwm",
113 .of_match = exynos_channels,
114 .ops = &exynos_pwm_ops,
115 .probe = exynos_pwm_probe,
116 .ofdata_to_platdata = exynos_pwm_ofdata_to_platdata,
117 .priv_auto_alloc_size = sizeof(struct exynos_pwm_priv),