blob: 2140a05b6796bbcf1c99b5a5024103aa1102a48c [file] [log] [blame]
Vasily Khoruzhick86a38e52018-05-14 08:16:20 -07001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (c) 2017-2018 Vasily Khoruzhick <anarsoul@gmail.com>
4 */
5
Vasily Khoruzhick86a38e52018-05-14 08:16:20 -07006#include <div64.h>
7#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
Vasily Khoruzhick86a38e52018-05-14 08:16:20 -07009#include <pwm.h>
10#include <regmap.h>
11#include <syscon.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060012#include <asm/global_data.h>
Vasily Khoruzhick86a38e52018-05-14 08:16:20 -070013#include <asm/io.h>
14#include <asm/arch/pwm.h>
Vasily Khoruzhick86a38e52018-05-14 08:16:20 -070015#include <power/regulator.h>
16
17DECLARE_GLOBAL_DATA_PTR;
18
19#define OSC_24MHZ 24000000
20
21struct sunxi_pwm_priv {
22 struct sunxi_pwm *regs;
23 bool invert;
24 u32 prescaler;
25};
26
27static const u32 prescaler_table[] = {
28 120, /* 0000 */
29 180, /* 0001 */
30 240, /* 0010 */
31 360, /* 0011 */
32 480, /* 0100 */
33 0, /* 0101 */
34 0, /* 0110 */
35 0, /* 0111 */
36 12000, /* 1000 */
37 24000, /* 1001 */
38 36000, /* 1010 */
39 48000, /* 1011 */
40 72000, /* 1100 */
41 0, /* 1101 */
42 0, /* 1110 */
43 1, /* 1111 */
44};
45
Vasily Khoruzhick86a38e52018-05-14 08:16:20 -070046static int sunxi_pwm_set_invert(struct udevice *dev, uint channel,
47 bool polarity)
48{
49 struct sunxi_pwm_priv *priv = dev_get_priv(dev);
50
51 debug("%s: polarity=%u\n", __func__, polarity);
52 priv->invert = polarity;
53
54 return 0;
55}
56
57static int sunxi_pwm_set_config(struct udevice *dev, uint channel,
58 uint period_ns, uint duty_ns)
59{
60 struct sunxi_pwm_priv *priv = dev_get_priv(dev);
61 struct sunxi_pwm *regs = priv->regs;
Vasily Khoruzhick50f3feb2018-10-16 21:56:35 -070062 int best_prescaler = 0;
63 u32 v, best_period = 0, duty;
64 u64 best_scaled_freq = 0;
Vasily Khoruzhick86a38e52018-05-14 08:16:20 -070065 const u32 nsecs_per_sec = 1000000000U;
66
67 debug("%s: period_ns=%u, duty_ns=%u\n", __func__, period_ns, duty_ns);
68
Vasily Khoruzhick50f3feb2018-10-16 21:56:35 -070069 for (int prescaler = 0; prescaler <= SUNXI_PWM_CTRL_PRESCALE0_MASK;
Vasily Khoruzhick86a38e52018-05-14 08:16:20 -070070 prescaler++) {
Vasily Khoruzhick50f3feb2018-10-16 21:56:35 -070071 u32 period = 0;
72 u64 scaled_freq = 0;
Vasily Khoruzhick86a38e52018-05-14 08:16:20 -070073 if (!prescaler_table[prescaler])
74 continue;
75 scaled_freq = lldiv(OSC_24MHZ, prescaler_table[prescaler]);
76 period = lldiv(scaled_freq * period_ns, nsecs_per_sec);
Vasily Khoruzhick50f3feb2018-10-16 21:56:35 -070077 if ((period - 1 <= SUNXI_PWM_CH0_PERIOD_MAX) &&
78 best_period < period) {
79 best_period = period;
80 best_scaled_freq = scaled_freq;
81 best_prescaler = prescaler;
82 }
Vasily Khoruzhick86a38e52018-05-14 08:16:20 -070083 }
84
Vasily Khoruzhick50f3feb2018-10-16 21:56:35 -070085 if (best_period - 1 > SUNXI_PWM_CH0_PERIOD_MAX) {
Vasily Khoruzhick86a38e52018-05-14 08:16:20 -070086 debug("%s: failed to find prescaler value\n", __func__);
87 return -EINVAL;
88 }
89
Vasily Khoruzhick50f3feb2018-10-16 21:56:35 -070090 duty = lldiv(best_scaled_freq * duty_ns, nsecs_per_sec);
Vasily Khoruzhick86a38e52018-05-14 08:16:20 -070091
Vasily Khoruzhick50f3feb2018-10-16 21:56:35 -070092 if (priv->prescaler != best_prescaler) {
Vasily Khoruzhick86a38e52018-05-14 08:16:20 -070093 /* Mask clock to update prescaler */
94 v = readl(&regs->ctrl);
95 v &= ~SUNXI_PWM_CTRL_CLK_GATE;
96 writel(v, &regs->ctrl);
97 v &= ~SUNXI_PWM_CTRL_PRESCALE0_MASK;
Vasily Khoruzhick50f3feb2018-10-16 21:56:35 -070098 v |= (best_prescaler & SUNXI_PWM_CTRL_PRESCALE0_MASK);
Vasily Khoruzhick86a38e52018-05-14 08:16:20 -070099 writel(v, &regs->ctrl);
100 v |= SUNXI_PWM_CTRL_CLK_GATE;
101 writel(v, &regs->ctrl);
Vasily Khoruzhick50f3feb2018-10-16 21:56:35 -0700102 priv->prescaler = best_prescaler;
Vasily Khoruzhick86a38e52018-05-14 08:16:20 -0700103 }
104
Vasily Khoruzhick50f3feb2018-10-16 21:56:35 -0700105 writel(SUNXI_PWM_CH0_PERIOD_PRD(best_period) |
Vasily Khoruzhick86a38e52018-05-14 08:16:20 -0700106 SUNXI_PWM_CH0_PERIOD_DUTY(duty), &regs->ch0_period);
107
108 debug("%s: prescaler: %d, period: %d, duty: %d\n",
109 __func__, priv->prescaler,
Vasily Khoruzhick50f3feb2018-10-16 21:56:35 -0700110 best_period, duty);
Vasily Khoruzhick86a38e52018-05-14 08:16:20 -0700111
112 return 0;
113}
114
115static int sunxi_pwm_set_enable(struct udevice *dev, uint channel, bool enable)
116{
117 struct sunxi_pwm_priv *priv = dev_get_priv(dev);
118 struct sunxi_pwm *regs = priv->regs;
119 u32 v;
120
121 debug("%s: Enable '%s'\n", __func__, dev->name);
122
123 v = readl(&regs->ctrl);
124 if (!enable) {
125 v &= ~SUNXI_PWM_CTRL_ENABLE0;
126 writel(v, &regs->ctrl);
127 return 0;
128 }
129
Vasily Khoruzhick86a38e52018-05-14 08:16:20 -0700130 if (priv->invert)
131 v &= ~SUNXI_PWM_CTRL_CH0_ACT_STA;
132 else
133 v |= SUNXI_PWM_CTRL_CH0_ACT_STA;
134 v |= SUNXI_PWM_CTRL_ENABLE0;
135 writel(v, &regs->ctrl);
136
137 return 0;
138}
139
Simon Glassaad29ae2020-12-03 16:55:21 -0700140static int sunxi_pwm_of_to_plat(struct udevice *dev)
Vasily Khoruzhick86a38e52018-05-14 08:16:20 -0700141{
142 struct sunxi_pwm_priv *priv = dev_get_priv(dev);
143
Masahiro Yamada1096ae12020-07-17 14:36:46 +0900144 priv->regs = dev_read_addr_ptr(dev);
Vasily Khoruzhick86a38e52018-05-14 08:16:20 -0700145
146 return 0;
147}
148
149static int sunxi_pwm_probe(struct udevice *dev)
150{
151 return 0;
152}
153
154static const struct pwm_ops sunxi_pwm_ops = {
155 .set_invert = sunxi_pwm_set_invert,
156 .set_config = sunxi_pwm_set_config,
157 .set_enable = sunxi_pwm_set_enable,
158};
159
160static const struct udevice_id sunxi_pwm_ids[] = {
161 { .compatible = "allwinner,sun5i-a13-pwm" },
162 { .compatible = "allwinner,sun50i-a64-pwm" },
163 { }
164};
165
166U_BOOT_DRIVER(sunxi_pwm) = {
167 .name = "sunxi_pwm",
168 .id = UCLASS_PWM,
169 .of_match = sunxi_pwm_ids,
170 .ops = &sunxi_pwm_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -0700171 .of_to_plat = sunxi_pwm_of_to_plat,
Vasily Khoruzhick86a38e52018-05-14 08:16:20 -0700172 .probe = sunxi_pwm_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700173 .priv_auto = sizeof(struct sunxi_pwm_priv),
Vasily Khoruzhick86a38e52018-05-14 08:16:20 -0700174};