blob: fefa3c65ec450ae8795d834ce12c35f5da271c50 [file] [log] [blame]
Dario Binacchiade7f0d2020-12-30 00:16:24 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * EHRPWM PWM driver
4 *
5 * Copyright (C) 2020 Dario Binacchi <dariobin@libero.it>
6 *
7 * Based on Linux kernel drivers/pwm/pwm-tiehrpwm.c
8 */
9
10#include <common.h>
11#include <clk.h>
12#include <div64.h>
13#include <dm.h>
14#include <dm/device_compat.h>
15#include <pwm.h>
16#include <asm/io.h>
Igor Prusov13248d62023-11-09 20:10:04 +030017#include <linux/time.h>
Dario Binacchiade7f0d2020-12-30 00:16:24 +010018
19/* Time base module registers */
20#define TI_EHRPWM_TBCTL 0x00
21#define TI_EHRPWM_TBPRD 0x0A
22
23#define TI_EHRPWM_TBCTL_PRDLD_MASK BIT(3)
24#define TI_EHRPWM_TBCTL_PRDLD_SHDW 0
25#define TI_EHRPWM_TBCTL_PRDLD_IMDT BIT(3)
26#define TI_EHRPWM_TBCTL_CLKDIV_MASK GENMASK(12, 7)
27#define TI_EHRPWM_TBCTL_CTRMODE_MASK GENMASK(1, 0)
28#define TI_EHRPWM_TBCTL_CTRMODE_UP 0
29#define TI_EHRPWM_TBCTL_CTRMODE_DOWN BIT(0)
30#define TI_EHRPWM_TBCTL_CTRMODE_UPDOWN BIT(1)
31#define TI_EHRPWM_TBCTL_CTRMODE_FREEZE GENMASK(1, 0)
32
33#define TI_EHRPWM_TBCTL_HSPCLKDIV_SHIFT 7
34#define TI_EHRPWM_TBCTL_CLKDIV_SHIFT 10
35
36#define TI_EHRPWM_CLKDIV_MAX 7
37#define TI_EHRPWM_HSPCLKDIV_MAX 7
38#define TI_EHRPWM_PERIOD_MAX 0xFFFF
39
40/* Counter compare module registers */
41#define TI_EHRPWM_CMPA 0x12
42#define TI_EHRPWM_CMPB 0x14
43
44/* Action qualifier module registers */
45#define TI_EHRPWM_AQCTLA 0x16
46#define TI_EHRPWM_AQCTLB 0x18
47#define TI_EHRPWM_AQSFRC 0x1A
48#define TI_EHRPWM_AQCSFRC 0x1C
49
50#define TI_EHRPWM_AQCTL_CBU_MASK GENMASK(9, 8)
51#define TI_EHRPWM_AQCTL_CBU_FRCLOW BIT(8)
52#define TI_EHRPWM_AQCTL_CBU_FRCHIGH BIT(9)
53#define TI_EHRPWM_AQCTL_CBU_FRCTOGGLE GENMASK(9, 8)
54#define TI_EHRPWM_AQCTL_CAU_MASK GENMASK(5, 4)
55#define TI_EHRPWM_AQCTL_CAU_FRCLOW BIT(4)
56#define TI_EHRPWM_AQCTL_CAU_FRCHIGH BIT(5)
57#define TI_EHRPWM_AQCTL_CAU_FRCTOGGLE GENMASK(5, 4)
58#define TI_EHRPWM_AQCTL_PRD_MASK GENMASK(3, 2)
59#define TI_EHRPWM_AQCTL_PRD_FRCLOW BIT(2)
60#define TI_EHRPWM_AQCTL_PRD_FRCHIGH BIT(3)
61#define TI_EHRPWM_AQCTL_PRD_FRCTOGGLE GENMASK(3, 2)
62#define TI_EHRPWM_AQCTL_ZRO_MASK GENMASK(1, 0)
63#define TI_EHRPWM_AQCTL_ZRO_FRCLOW BIT(0)
64#define TI_EHRPWM_AQCTL_ZRO_FRCHIGH BIT(1)
65#define TI_EHRPWM_AQCTL_ZRO_FRCTOGGLE GENMASK(1, 0)
66
67#define TI_EHRPWM_AQCTL_CHANA_POLNORMAL (TI_EHRPWM_AQCTL_CAU_FRCLOW | \
68 TI_EHRPWM_AQCTL_PRD_FRCHIGH | \
69 TI_EHRPWM_AQCTL_ZRO_FRCHIGH)
70#define TI_EHRPWM_AQCTL_CHANA_POLINVERSED (TI_EHRPWM_AQCTL_CAU_FRCHIGH | \
71 TI_EHRPWM_AQCTL_PRD_FRCLOW | \
72 TI_EHRPWM_AQCTL_ZRO_FRCLOW)
73#define TI_EHRPWM_AQCTL_CHANB_POLNORMAL (TI_EHRPWM_AQCTL_CBU_FRCLOW | \
74 TI_EHRPWM_AQCTL_PRD_FRCHIGH | \
75 TI_EHRPWM_AQCTL_ZRO_FRCHIGH)
76#define TI_EHRPWM_AQCTL_CHANB_POLINVERSED (TI_EHRPWM_AQCTL_CBU_FRCHIGH | \
77 TI_EHRPWM_AQCTL_PRD_FRCLOW | \
78 TI_EHRPWM_AQCTL_ZRO_FRCLOW)
79
80#define TI_EHRPWM_AQSFRC_RLDCSF_MASK GENMASK(7, 6)
81#define TI_EHRPWM_AQSFRC_RLDCSF_ZRO 0
82#define TI_EHRPWM_AQSFRC_RLDCSF_PRD BIT(6)
83#define TI_EHRPWM_AQSFRC_RLDCSF_ZROPRD BIT(7)
84#define TI_EHRPWM_AQSFRC_RLDCSF_IMDT GENMASK(7, 6)
85
86#define TI_EHRPWM_AQCSFRC_CSFB_MASK GENMASK(3, 2)
87#define TI_EHRPWM_AQCSFRC_CSFB_FRCDIS 0
88#define TI_EHRPWM_AQCSFRC_CSFB_FRCLOW BIT(2)
89#define TI_EHRPWM_AQCSFRC_CSFB_FRCHIGH BIT(3)
90#define TI_EHRPWM_AQCSFRC_CSFB_DISSWFRC GENMASK(3, 2)
91#define TI_EHRPWM_AQCSFRC_CSFA_MASK GENMASK(1, 0)
92#define TI_EHRPWM_AQCSFRC_CSFA_FRCDIS 0
93#define TI_EHRPWM_AQCSFRC_CSFA_FRCLOW BIT(0)
94#define TI_EHRPWM_AQCSFRC_CSFA_FRCHIGH BIT(1)
95#define TI_EHRPWM_AQCSFRC_CSFA_DISSWFRC GENMASK(1, 0)
96
97#define TI_EHRPWM_NUM_CHANNELS 2
98
99struct ti_ehrpwm_priv {
100 fdt_addr_t regs;
101 u32 clk_rate;
102 struct clk tbclk;
103 unsigned long period_cycles[TI_EHRPWM_NUM_CHANNELS];
104 bool polarity_reversed[TI_EHRPWM_NUM_CHANNELS];
105};
106
107static void ti_ehrpwm_modify(u16 val, u16 mask, fdt_addr_t reg)
108{
109 unsigned short v;
110
111 v = readw(reg);
112 v &= ~mask;
113 v |= val & mask;
114 writew(v, reg);
115}
116
117static int ti_ehrpwm_set_invert(struct udevice *dev, uint channel,
118 bool polarity)
119{
120 struct ti_ehrpwm_priv *priv = dev_get_priv(dev);
121
122 if (channel >= TI_EHRPWM_NUM_CHANNELS)
123 return -ENOSPC;
124
125 /* Configuration of polarity in hardware delayed, do at enable */
126 priv->polarity_reversed[channel] = polarity;
127 return 0;
128}
129
130/**
131 * set_prescale_div - Set up the prescaler divider function
132 * @rqst_prescaler: prescaler value min
133 * @prescale_div: prescaler value set
134 * @tb_clk_div: Time Base Control prescaler bits
135 */
136static int set_prescale_div(unsigned long rqst_prescaler, u16 *prescale_div,
137 u16 *tb_clk_div)
138{
139 unsigned int clkdiv, hspclkdiv;
140
141 for (clkdiv = 0; clkdiv <= TI_EHRPWM_CLKDIV_MAX; clkdiv++) {
142 for (hspclkdiv = 0; hspclkdiv <= TI_EHRPWM_HSPCLKDIV_MAX;
143 hspclkdiv++) {
144 /*
145 * calculations for prescaler value :
146 * prescale_div = HSPCLKDIVIDER * CLKDIVIDER.
147 * HSPCLKDIVIDER = 2 ** hspclkdiv
148 * CLKDIVIDER = (1), if clkdiv == 0 *OR*
149 * (2 * clkdiv), if clkdiv != 0
150 *
151 * Configure prescale_div value such that period
152 * register value is less than 65535.
153 */
154
155 *prescale_div = (1 << clkdiv) *
156 (hspclkdiv ? (hspclkdiv * 2) : 1);
157 if (*prescale_div > rqst_prescaler) {
158 *tb_clk_div =
159 (clkdiv << TI_EHRPWM_TBCTL_CLKDIV_SHIFT) |
160 (hspclkdiv <<
161 TI_EHRPWM_TBCTL_HSPCLKDIV_SHIFT);
162 return 0;
163 }
164 }
165 }
166
167 return 1;
168}
169
170static void ti_ehrpwm_configure_polarity(struct udevice *dev, uint channel)
171{
172 struct ti_ehrpwm_priv *priv = dev_get_priv(dev);
173 u16 aqctl_val, aqctl_mask;
174 unsigned int aqctl_reg;
175
176 /*
177 * Configure PWM output to HIGH/LOW level on counter
178 * reaches compare register value and LOW/HIGH level
179 * on counter value reaches period register value and
180 * zero value on counter
181 */
182 if (channel == 1) {
183 aqctl_reg = TI_EHRPWM_AQCTLB;
184 aqctl_mask = TI_EHRPWM_AQCTL_CBU_MASK;
185
186 if (priv->polarity_reversed[channel])
187 aqctl_val = TI_EHRPWM_AQCTL_CHANB_POLINVERSED;
188 else
189 aqctl_val = TI_EHRPWM_AQCTL_CHANB_POLNORMAL;
190 } else {
191 aqctl_reg = TI_EHRPWM_AQCTLA;
192 aqctl_mask = TI_EHRPWM_AQCTL_CAU_MASK;
193
194 if (priv->polarity_reversed[channel])
195 aqctl_val = TI_EHRPWM_AQCTL_CHANA_POLINVERSED;
196 else
197 aqctl_val = TI_EHRPWM_AQCTL_CHANA_POLNORMAL;
198 }
199
200 aqctl_mask |= TI_EHRPWM_AQCTL_PRD_MASK | TI_EHRPWM_AQCTL_ZRO_MASK;
201 ti_ehrpwm_modify(aqctl_val, aqctl_mask, priv->regs + aqctl_reg);
202}
203
204/*
205 * period_ns = 10^9 * (ps_divval * period_cycles) / PWM_CLK_RATE
206 * duty_ns = 10^9 * (ps_divval * duty_cycles) / PWM_CLK_RATE
207 */
208static int ti_ehrpwm_set_config(struct udevice *dev, uint channel,
209 uint period_ns, uint duty_ns)
210{
211 struct ti_ehrpwm_priv *priv = dev_get_priv(dev);
212 u32 period_cycles, duty_cycles;
213 u16 ps_divval, tb_divval;
214 unsigned int i, cmp_reg;
215 unsigned long long c;
216
217 if (channel >= TI_EHRPWM_NUM_CHANNELS)
218 return -ENOSPC;
219
220 if (period_ns > NSEC_PER_SEC)
221 return -ERANGE;
222
223 c = priv->clk_rate;
224 c = c * period_ns;
225 do_div(c, NSEC_PER_SEC);
226 period_cycles = (unsigned long)c;
227
228 if (period_cycles < 1) {
229 period_cycles = 1;
230 duty_cycles = 1;
231 } else {
232 c = priv->clk_rate;
233 c = c * duty_ns;
234 do_div(c, NSEC_PER_SEC);
235 duty_cycles = (unsigned long)c;
236 }
237
238 dev_dbg(dev, "channel=%d, period_ns=%d, duty_ns=%d\n",
239 channel, period_ns, duty_ns);
240
241 /*
242 * Period values should be same for multiple PWM channels as IP uses
243 * same period register for multiple channels.
244 */
245 for (i = 0; i < TI_EHRPWM_NUM_CHANNELS; i++) {
246 if (priv->period_cycles[i] &&
247 priv->period_cycles[i] != period_cycles) {
248 /*
249 * Allow channel to reconfigure period if no other
250 * channels being configured.
251 */
252 if (i == channel)
253 continue;
254
255 dev_err(dev, "period value conflicts with channel %u\n",
256 i);
257 return -EINVAL;
258 }
259 }
260
261 priv->period_cycles[channel] = period_cycles;
262
263 /* Configure clock prescaler to support Low frequency PWM wave */
264 if (set_prescale_div(period_cycles / TI_EHRPWM_PERIOD_MAX, &ps_divval,
265 &tb_divval)) {
266 dev_err(dev, "unsupported values\n");
267 return -EINVAL;
268 }
269
270 /* Update clock prescaler values */
271 ti_ehrpwm_modify(tb_divval, TI_EHRPWM_TBCTL_CLKDIV_MASK,
272 priv->regs + TI_EHRPWM_TBCTL);
273
274 /* Update period & duty cycle with presacler division */
275 period_cycles = period_cycles / ps_divval;
276 duty_cycles = duty_cycles / ps_divval;
277
278 /* Configure shadow loading on Period register */
279 ti_ehrpwm_modify(TI_EHRPWM_TBCTL_PRDLD_SHDW, TI_EHRPWM_TBCTL_PRDLD_MASK,
280 priv->regs + TI_EHRPWM_TBCTL);
281
282 writew(period_cycles, priv->regs + TI_EHRPWM_TBPRD);
283
284 /* Configure ehrpwm counter for up-count mode */
285 ti_ehrpwm_modify(TI_EHRPWM_TBCTL_CTRMODE_UP,
286 TI_EHRPWM_TBCTL_CTRMODE_MASK,
287 priv->regs + TI_EHRPWM_TBCTL);
288
289 if (channel == 1)
290 /* Channel 1 configured with compare B register */
291 cmp_reg = TI_EHRPWM_CMPB;
292 else
293 /* Channel 0 configured with compare A register */
294 cmp_reg = TI_EHRPWM_CMPA;
295
296 writew(duty_cycles, priv->regs + cmp_reg);
297 return 0;
298}
299
300static int ti_ehrpwm_disable(struct udevice *dev, uint channel)
301{
302 struct ti_ehrpwm_priv *priv = dev_get_priv(dev);
303 u16 aqcsfrc_val, aqcsfrc_mask;
304 int err;
305
306 if (channel >= TI_EHRPWM_NUM_CHANNELS)
307 return -ENOSPC;
308
309 /* Action Qualifier puts PWM output low forcefully */
310 if (channel) {
311 aqcsfrc_val = TI_EHRPWM_AQCSFRC_CSFB_FRCLOW;
312 aqcsfrc_mask = TI_EHRPWM_AQCSFRC_CSFB_MASK;
313 } else {
314 aqcsfrc_val = TI_EHRPWM_AQCSFRC_CSFA_FRCLOW;
315 aqcsfrc_mask = TI_EHRPWM_AQCSFRC_CSFA_MASK;
316 }
317
318 /* Update shadow register first before modifying active register */
319 ti_ehrpwm_modify(TI_EHRPWM_AQSFRC_RLDCSF_ZRO,
320 TI_EHRPWM_AQSFRC_RLDCSF_MASK,
321 priv->regs + TI_EHRPWM_AQSFRC);
322
323 ti_ehrpwm_modify(aqcsfrc_val, aqcsfrc_mask,
324 priv->regs + TI_EHRPWM_AQCSFRC);
325
326 /*
327 * Changes to immediate action on Action Qualifier. This puts
328 * Action Qualifier control on PWM output from next TBCLK
329 */
330 ti_ehrpwm_modify(TI_EHRPWM_AQSFRC_RLDCSF_IMDT,
331 TI_EHRPWM_AQSFRC_RLDCSF_MASK,
332 priv->regs + TI_EHRPWM_AQSFRC);
333
334 ti_ehrpwm_modify(aqcsfrc_val, aqcsfrc_mask,
335 priv->regs + TI_EHRPWM_AQCSFRC);
336
337 /* Disabling TBCLK on PWM disable */
338 err = clk_disable(&priv->tbclk);
339 if (err) {
340 dev_err(dev, "failed to disable tbclk\n");
341 return err;
342 }
343
344 return 0;
345}
346
347static int ti_ehrpwm_enable(struct udevice *dev, uint channel)
348{
349 struct ti_ehrpwm_priv *priv = dev_get_priv(dev);
350 u16 aqcsfrc_val, aqcsfrc_mask;
351 int err;
352
353 if (channel >= TI_EHRPWM_NUM_CHANNELS)
354 return -ENOSPC;
355
356 /* Disabling Action Qualifier on PWM output */
357 if (channel) {
358 aqcsfrc_val = TI_EHRPWM_AQCSFRC_CSFB_FRCDIS;
359 aqcsfrc_mask = TI_EHRPWM_AQCSFRC_CSFB_MASK;
360 } else {
361 aqcsfrc_val = TI_EHRPWM_AQCSFRC_CSFA_FRCDIS;
362 aqcsfrc_mask = TI_EHRPWM_AQCSFRC_CSFA_MASK;
363 }
364
365 /* Changes to shadow mode */
366 ti_ehrpwm_modify(TI_EHRPWM_AQSFRC_RLDCSF_ZRO,
367 TI_EHRPWM_AQSFRC_RLDCSF_MASK,
368 priv->regs + TI_EHRPWM_AQSFRC);
369
370 ti_ehrpwm_modify(aqcsfrc_val, aqcsfrc_mask,
371 priv->regs + TI_EHRPWM_AQCSFRC);
372
373 /* Channels polarity can be configured from action qualifier module */
374 ti_ehrpwm_configure_polarity(dev, channel);
375
376 err = clk_enable(&priv->tbclk);
377 if (err) {
378 dev_err(dev, "failed to enable tbclk\n");
379 return err;
380 }
381
382 return 0;
383}
384
385static int ti_ehrpwm_set_enable(struct udevice *dev, uint channel, bool enable)
386{
387 if (enable)
388 return ti_ehrpwm_enable(dev, channel);
389
390 return ti_ehrpwm_disable(dev, channel);
391}
392
393static int ti_ehrpwm_of_to_plat(struct udevice *dev)
394{
395 struct ti_ehrpwm_priv *priv = dev_get_priv(dev);
396
397 priv->regs = dev_read_addr(dev);
398 if (priv->regs == FDT_ADDR_T_NONE) {
399 dev_err(dev, "invalid address\n");
400 return -EINVAL;
401 }
402
403 dev_dbg(dev, "regs=0x%08lx\n", priv->regs);
404 return 0;
405}
406
407static int ti_ehrpwm_remove(struct udevice *dev)
408{
409 struct ti_ehrpwm_priv *priv = dev_get_priv(dev);
410
411 clk_release_all(&priv->tbclk, 1);
412 return 0;
413}
414
415static int ti_ehrpwm_probe(struct udevice *dev)
416{
417 struct ti_ehrpwm_priv *priv = dev_get_priv(dev);
418 struct clk clk;
419 int err;
420
421 err = clk_get_by_name(dev, "fck", &clk);
422 if (err) {
423 dev_err(dev, "failed to get clock\n");
424 return err;
425 }
426
427 priv->clk_rate = clk_get_rate(&clk);
428 if (IS_ERR_VALUE(priv->clk_rate) || !priv->clk_rate) {
429 dev_err(dev, "failed to get clock rate\n");
430 if (IS_ERR_VALUE(priv->clk_rate))
431 return priv->clk_rate;
432
433 return -EINVAL;
434 }
435
436 /* Acquire tbclk for Time Base EHRPWM submodule */
437 err = clk_get_by_name(dev, "tbclk", &priv->tbclk);
438 if (err) {
439 dev_err(dev, "failed to get tbclk clock\n");
440 return err;
441 }
442
443 return 0;
444}
445
446static const struct pwm_ops ti_ehrpwm_ops = {
447 .set_config = ti_ehrpwm_set_config,
448 .set_enable = ti_ehrpwm_set_enable,
449 .set_invert = ti_ehrpwm_set_invert,
450};
451
452static const struct udevice_id ti_ehrpwm_ids[] = {
453 {.compatible = "ti,am3352-ehrpwm"},
454 {.compatible = "ti,am33xx-ehrpwm"},
455 {}
456};
457
458U_BOOT_DRIVER(ti_ehrpwm) = {
459 .name = "ti_ehrpwm",
460 .id = UCLASS_PWM,
461 .of_match = ti_ehrpwm_ids,
462 .ops = &ti_ehrpwm_ops,
Dario Binacchib0db69b2021-01-15 09:10:26 +0100463 .of_to_plat = ti_ehrpwm_of_to_plat,
Dario Binacchiade7f0d2020-12-30 00:16:24 +0100464 .probe = ti_ehrpwm_probe,
465 .remove = ti_ehrpwm_remove,
466 .priv_auto = sizeof(struct ti_ehrpwm_priv),
467};