1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2017-2018 Vasily Khoruzhick <anarsoul@gmail.com>
13 #include <asm/arch/pwm.h>
14 #include <asm/arch/gpio.h>
15 #include <power/regulator.h>
17 DECLARE_GLOBAL_DATA_PTR;
19 #define OSC_24MHZ 24000000
21 struct sunxi_pwm_priv {
22 struct sunxi_pwm *regs;
27 static const u32 prescaler_table[] = {
46 static int sunxi_pwm_config_pinmux(void)
48 #ifdef CONFIG_MACH_SUN50I
49 sunxi_gpio_set_cfgpin(SUNXI_GPD(22), SUNXI_GPD_PWM);
54 static int sunxi_pwm_set_invert(struct udevice *dev, uint channel,
57 struct sunxi_pwm_priv *priv = dev_get_priv(dev);
59 debug("%s: polarity=%u\n", __func__, polarity);
60 priv->invert = polarity;
65 static int sunxi_pwm_set_config(struct udevice *dev, uint channel,
66 uint period_ns, uint duty_ns)
68 struct sunxi_pwm_priv *priv = dev_get_priv(dev);
69 struct sunxi_pwm *regs = priv->regs;
70 int best_prescaler = 0;
71 u32 v, best_period = 0, duty;
72 u64 best_scaled_freq = 0;
73 const u32 nsecs_per_sec = 1000000000U;
75 debug("%s: period_ns=%u, duty_ns=%u\n", __func__, period_ns, duty_ns);
77 for (int prescaler = 0; prescaler <= SUNXI_PWM_CTRL_PRESCALE0_MASK;
81 if (!prescaler_table[prescaler])
83 scaled_freq = lldiv(OSC_24MHZ, prescaler_table[prescaler]);
84 period = lldiv(scaled_freq * period_ns, nsecs_per_sec);
85 if ((period - 1 <= SUNXI_PWM_CH0_PERIOD_MAX) &&
86 best_period < period) {
88 best_scaled_freq = scaled_freq;
89 best_prescaler = prescaler;
93 if (best_period - 1 > SUNXI_PWM_CH0_PERIOD_MAX) {
94 debug("%s: failed to find prescaler value\n", __func__);
98 duty = lldiv(best_scaled_freq * duty_ns, nsecs_per_sec);
100 if (priv->prescaler != best_prescaler) {
101 /* Mask clock to update prescaler */
102 v = readl(®s->ctrl);
103 v &= ~SUNXI_PWM_CTRL_CLK_GATE;
104 writel(v, ®s->ctrl);
105 v &= ~SUNXI_PWM_CTRL_PRESCALE0_MASK;
106 v |= (best_prescaler & SUNXI_PWM_CTRL_PRESCALE0_MASK);
107 writel(v, ®s->ctrl);
108 v |= SUNXI_PWM_CTRL_CLK_GATE;
109 writel(v, ®s->ctrl);
110 priv->prescaler = best_prescaler;
113 writel(SUNXI_PWM_CH0_PERIOD_PRD(best_period) |
114 SUNXI_PWM_CH0_PERIOD_DUTY(duty), ®s->ch0_period);
116 debug("%s: prescaler: %d, period: %d, duty: %d\n",
117 __func__, priv->prescaler,
123 static int sunxi_pwm_set_enable(struct udevice *dev, uint channel, bool enable)
125 struct sunxi_pwm_priv *priv = dev_get_priv(dev);
126 struct sunxi_pwm *regs = priv->regs;
129 debug("%s: Enable '%s'\n", __func__, dev->name);
131 v = readl(®s->ctrl);
133 v &= ~SUNXI_PWM_CTRL_ENABLE0;
134 writel(v, ®s->ctrl);
138 sunxi_pwm_config_pinmux();
141 v &= ~SUNXI_PWM_CTRL_CH0_ACT_STA;
143 v |= SUNXI_PWM_CTRL_CH0_ACT_STA;
144 v |= SUNXI_PWM_CTRL_ENABLE0;
145 writel(v, ®s->ctrl);
150 static int sunxi_pwm_ofdata_to_platdata(struct udevice *dev)
152 struct sunxi_pwm_priv *priv = dev_get_priv(dev);
154 priv->regs = (struct sunxi_pwm *)devfdt_get_addr(dev);
159 static int sunxi_pwm_probe(struct udevice *dev)
164 static const struct pwm_ops sunxi_pwm_ops = {
165 .set_invert = sunxi_pwm_set_invert,
166 .set_config = sunxi_pwm_set_config,
167 .set_enable = sunxi_pwm_set_enable,
170 static const struct udevice_id sunxi_pwm_ids[] = {
171 { .compatible = "allwinner,sun5i-a13-pwm" },
172 { .compatible = "allwinner,sun50i-a64-pwm" },
176 U_BOOT_DRIVER(sunxi_pwm) = {
179 .of_match = sunxi_pwm_ids,
180 .ops = &sunxi_pwm_ops,
181 .ofdata_to_platdata = sunxi_pwm_ofdata_to_platdata,
182 .probe = sunxi_pwm_probe,
183 .priv_auto_alloc_size = sizeof(struct sunxi_pwm_priv),