blob: 767628833bc0660407337b2434ec1775f527ee51 [file] [log] [blame]
Michal Simek8ab1b032021-10-15 15:17:29 +02001// SPDX-License-Identifier: GPL-2.0
2/*
3 * (C) Copyright 2021 Xilinx, Inc. Michal Simek
4 */
5
6#define LOG_CATEGORY UCLASS_PWM
7
8#include <clk.h>
Michal Simek8ab1b032021-10-15 15:17:29 +02009#include <div64.h>
10#include <dm.h>
11#include <log.h>
12#include <pwm.h>
13#include <asm/io.h>
14#include <log.h>
15#include <div64.h>
16#include <linux/bitfield.h>
17#include <linux/math64.h>
18#include <linux/log2.h>
Igor Prusovc3421ea2023-11-09 20:10:04 +030019#include <linux/time.h>
Michal Simek8ab1b032021-10-15 15:17:29 +020020#include <dm/device_compat.h>
21
22#define CLOCK_CONTROL 0
23#define COUNTER_CONTROL 0xc
24#define INTERVAL_COUNTER 0x24
25#define MATCH_1_COUNTER 0x30
26
27#define CLK_FALLING_EDGE BIT(6)
28#define CLK_SRC_EXTERNAL BIT(5)
29#define CLK_PRESCALE_MASK GENMASK(4, 1)
30#define CLK_PRESCALE_ENABLE BIT(0)
31
32#define COUNTER_WAVE_POL BIT(6)
33#define COUNTER_WAVE_DISABLE BIT(5)
34#define COUNTER_RESET BIT(4)
35#define COUNTER_MATCH_ENABLE BIT(3)
36#define COUNTER_DECREMENT_ENABLE BIT(2)
37#define COUNTER_INTERVAL_ENABLE BIT(1)
38#define COUNTER_COUNTING_DISABLE BIT(0)
39
Michal Simek8ab1b032021-10-15 15:17:29 +020040#define TTC_REG(reg, channel) ((reg) + (channel) * sizeof(u32))
41#define TTC_CLOCK_CONTROL(reg, channel) \
42 TTC_REG((reg) + CLOCK_CONTROL, (channel))
43#define TTC_COUNTER_CONTROL(reg, channel) \
44 TTC_REG((reg) + COUNTER_CONTROL, (channel))
45#define TTC_INTERVAL_COUNTER(reg, channel) \
46 TTC_REG((reg) + INTERVAL_COUNTER, (channel))
47#define TTC_MATCH_1_COUNTER(reg, channel) \
48 TTC_REG((reg) + MATCH_1_COUNTER, (channel))
49
50struct cadence_ttc_pwm_plat {
51 u8 *regs;
52 u32 timer_width;
53};
54
55struct cadence_ttc_pwm_priv {
56 u8 *regs;
57 u32 timer_width;
58 u32 timer_mask;
59 unsigned long frequency;
60 bool invert[2];
61};
62
63static int cadence_ttc_pwm_set_invert(struct udevice *dev, uint channel,
64 bool polarity)
65{
66 struct cadence_ttc_pwm_priv *priv = dev_get_priv(dev);
67
68 if (channel > 2) {
69 dev_err(dev, "Unsupported channel number %d(max 2)\n", channel);
70 return -EINVAL;
71 }
72
73 priv->invert[channel] = polarity;
74
75 dev_dbg(dev, "polarity=%u. Please config PWM again\n", polarity);
76
77 return 0;
78}
79
80static int cadence_ttc_pwm_set_config(struct udevice *dev, uint channel,
81 uint period_ns, uint duty_ns)
82{
83 struct cadence_ttc_pwm_priv *priv = dev_get_priv(dev);
84 u32 counter_ctrl, clock_ctrl;
85 int period_clocks, duty_clocks, prescaler;
86
87 dev_dbg(dev, "channel %d, duty %d/period %d ns\n", channel,
88 duty_ns, period_ns);
89
90 if (channel > 2) {
91 dev_err(dev, "Unsupported channel number %d(max 2)\n", channel);
92 return -EINVAL;
93 }
94
95 /* Make sure counter is stopped */
96 counter_ctrl = readl(TTC_COUNTER_CONTROL(priv->regs, channel));
97 setbits_le32(TTC_COUNTER_CONTROL(priv->regs, channel),
98 COUNTER_COUNTING_DISABLE | COUNTER_WAVE_DISABLE);
99
100 /* Calculate period, prescaler and set clock control register */
101 period_clocks = div64_u64(((int64_t)period_ns * priv->frequency),
102 NSEC_PER_SEC);
103
104 prescaler = ilog2(period_clocks) + 1 - priv->timer_width;
105 if (prescaler < 0)
106 prescaler = 0;
107
108 clock_ctrl = readl(TTC_CLOCK_CONTROL(priv->regs, channel));
109
110 if (!prescaler) {
111 clock_ctrl &= ~(CLK_PRESCALE_ENABLE | CLK_PRESCALE_MASK);
112 } else {
113 clock_ctrl &= ~CLK_PRESCALE_MASK;
114 clock_ctrl |= CLK_PRESCALE_ENABLE;
115 clock_ctrl |= FIELD_PREP(CLK_PRESCALE_MASK, prescaler - 1);
116 };
117
118 /* External source is not handled by this driver now */
119 clock_ctrl &= ~CLK_SRC_EXTERNAL;
120
121 writel(clock_ctrl, TTC_CLOCK_CONTROL(priv->regs, channel));
122
123 /* Calculate interval and set counter control value */
124 duty_clocks = div64_u64(((int64_t)duty_ns * priv->frequency),
125 NSEC_PER_SEC);
126
127 writel((period_clocks >> prescaler) & priv->timer_mask,
128 TTC_INTERVAL_COUNTER(priv->regs, channel));
129 writel((duty_clocks >> prescaler) & priv->timer_mask,
130 TTC_MATCH_1_COUNTER(priv->regs, channel));
131
132 /* Restore/reset counter */
133 counter_ctrl &= ~COUNTER_DECREMENT_ENABLE;
134 counter_ctrl |= COUNTER_INTERVAL_ENABLE |
135 COUNTER_RESET |
136 COUNTER_MATCH_ENABLE;
137
138 if (priv->invert[channel])
139 counter_ctrl |= COUNTER_WAVE_POL;
140 else
141 counter_ctrl &= ~COUNTER_WAVE_POL;
142
143 writel(counter_ctrl, TTC_COUNTER_CONTROL(priv->regs, channel));
144
145 dev_dbg(dev, "%d/%d clocks, prescaler 2^%d\n", duty_clocks,
146 period_clocks, prescaler);
147
148 return 0;
149};
150
151static int cadence_ttc_pwm_set_enable(struct udevice *dev, uint channel,
152 bool enable)
153{
154 struct cadence_ttc_pwm_priv *priv = dev_get_priv(dev);
155
156 if (channel > 2) {
157 dev_err(dev, "Unsupported channel number %d(max 2)\n", channel);
158 return -EINVAL;
159 }
160
161 dev_dbg(dev, "Enable: %d, channel %d\n", enable, channel);
162
163 if (enable) {
164 clrbits_le32(TTC_COUNTER_CONTROL(priv->regs, channel),
165 COUNTER_COUNTING_DISABLE |
166 COUNTER_WAVE_DISABLE);
167 setbits_le32(TTC_COUNTER_CONTROL(priv->regs, channel),
168 COUNTER_RESET);
169 } else {
170 setbits_le32(TTC_COUNTER_CONTROL(priv->regs, channel),
171 COUNTER_COUNTING_DISABLE |
172 COUNTER_WAVE_DISABLE);
173 }
174
175 return 0;
176};
177
178static int cadence_ttc_pwm_probe(struct udevice *dev)
179{
180 struct cadence_ttc_pwm_priv *priv = dev_get_priv(dev);
181 struct cadence_ttc_pwm_plat *plat = dev_get_plat(dev);
182 struct clk clk;
183 int ret;
184
185 priv->regs = plat->regs;
186 priv->timer_width = plat->timer_width;
187 priv->timer_mask = GENMASK(priv->timer_width - 1, 0);
188
189 ret = clk_get_by_index(dev, 0, &clk);
190 if (ret < 0) {
191 dev_err(dev, "failed to get clock\n");
192 return ret;
193 }
194
195 priv->frequency = clk_get_rate(&clk);
196 if (IS_ERR_VALUE(priv->frequency)) {
197 dev_err(dev, "failed to get rate\n");
198 return priv->frequency;
199 }
200 dev_dbg(dev, "Clk frequency: %ld\n", priv->frequency);
201
202 ret = clk_enable(&clk);
203 if (ret) {
204 dev_err(dev, "failed to enable clock\n");
205 return ret;
206 }
207
208 return 0;
209}
210
211static int cadence_ttc_pwm_of_to_plat(struct udevice *dev)
212{
213 struct cadence_ttc_pwm_plat *plat = dev_get_plat(dev);
214 const char *cells;
215
216 cells = dev_read_prop(dev, "#pwm-cells", NULL);
217 if (!cells)
218 return -EINVAL;
219
220 plat->regs = dev_read_addr_ptr(dev);
221
222 plat->timer_width = dev_read_u32_default(dev, "timer-width", 16);
223
224 return 0;
225}
226
227static int cadence_ttc_pwm_bind(struct udevice *dev)
228{
229 const char *cells;
230
231 cells = dev_read_prop(dev, "#pwm-cells", NULL);
232 if (!cells)
233 return -ENODEV;
234
235 return 0;
236}
237
238static const struct pwm_ops cadence_ttc_pwm_ops = {
239 .set_invert = cadence_ttc_pwm_set_invert,
240 .set_config = cadence_ttc_pwm_set_config,
241 .set_enable = cadence_ttc_pwm_set_enable,
242};
243
244static const struct udevice_id cadence_ttc_pwm_ids[] = {
245 { .compatible = "cdns,ttc" },
246 { }
247};
248
249U_BOOT_DRIVER(cadence_ttc_pwm) = {
250 .name = "cadence_ttc_pwm",
251 .id = UCLASS_PWM,
252 .of_match = cadence_ttc_pwm_ids,
253 .ops = &cadence_ttc_pwm_ops,
254 .bind = cadence_ttc_pwm_bind,
255 .of_to_plat = cadence_ttc_pwm_of_to_plat,
256 .probe = cadence_ttc_pwm_probe,
257 .priv_auto = sizeof(struct cadence_ttc_pwm_priv),
258 .plat_auto = sizeof(struct cadence_ttc_pwm_plat),
259};