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