1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright (C) 2012 Texas Instruments, Inc. - https://www.ti.com/
8 #include <linux/module.h>
9 #include <linux/platform_device.h>
10 #include <linux/pwm.h>
12 #include <linux/err.h>
13 #include <linux/clk.h>
14 #include <linux/pm_runtime.h>
17 /* EHRPWM registers and bits definitions */
19 /* Time base module registers */
23 #define TBCTL_PRDLD_MASK BIT(3)
24 #define TBCTL_PRDLD_SHDW 0
25 #define TBCTL_PRDLD_IMDT BIT(3)
26 #define TBCTL_CLKDIV_MASK (BIT(12) | BIT(11) | BIT(10) | BIT(9) | \
28 #define TBCTL_CTRMODE_MASK (BIT(1) | BIT(0))
29 #define TBCTL_CTRMODE_UP 0
30 #define TBCTL_CTRMODE_DOWN BIT(0)
31 #define TBCTL_CTRMODE_UPDOWN BIT(1)
32 #define TBCTL_CTRMODE_FREEZE (BIT(1) | BIT(0))
34 #define TBCTL_HSPCLKDIV_SHIFT 7
35 #define TBCTL_CLKDIV_SHIFT 10
38 #define HSPCLKDIV_MAX 7
39 #define PERIOD_MAX 0xFFFF
41 /* compare module registers */
45 /* Action qualifier module registers */
51 #define AQCTL_CBU_MASK (BIT(9) | BIT(8))
52 #define AQCTL_CBU_FRCLOW BIT(8)
53 #define AQCTL_CBU_FRCHIGH BIT(9)
54 #define AQCTL_CBU_FRCTOGGLE (BIT(9) | BIT(8))
55 #define AQCTL_CAU_MASK (BIT(5) | BIT(4))
56 #define AQCTL_CAU_FRCLOW BIT(4)
57 #define AQCTL_CAU_FRCHIGH BIT(5)
58 #define AQCTL_CAU_FRCTOGGLE (BIT(5) | BIT(4))
59 #define AQCTL_PRD_MASK (BIT(3) | BIT(2))
60 #define AQCTL_PRD_FRCLOW BIT(2)
61 #define AQCTL_PRD_FRCHIGH BIT(3)
62 #define AQCTL_PRD_FRCTOGGLE (BIT(3) | BIT(2))
63 #define AQCTL_ZRO_MASK (BIT(1) | BIT(0))
64 #define AQCTL_ZRO_FRCLOW BIT(0)
65 #define AQCTL_ZRO_FRCHIGH BIT(1)
66 #define AQCTL_ZRO_FRCTOGGLE (BIT(1) | BIT(0))
68 #define AQCTL_CHANA_POLNORMAL (AQCTL_CAU_FRCLOW | AQCTL_PRD_FRCHIGH | \
70 #define AQCTL_CHANA_POLINVERSED (AQCTL_CAU_FRCHIGH | AQCTL_PRD_FRCLOW | \
72 #define AQCTL_CHANB_POLNORMAL (AQCTL_CBU_FRCLOW | AQCTL_PRD_FRCHIGH | \
74 #define AQCTL_CHANB_POLINVERSED (AQCTL_CBU_FRCHIGH | AQCTL_PRD_FRCLOW | \
77 #define AQSFRC_RLDCSF_MASK (BIT(7) | BIT(6))
78 #define AQSFRC_RLDCSF_ZRO 0
79 #define AQSFRC_RLDCSF_PRD BIT(6)
80 #define AQSFRC_RLDCSF_ZROPRD BIT(7)
81 #define AQSFRC_RLDCSF_IMDT (BIT(7) | BIT(6))
83 #define AQCSFRC_CSFB_MASK (BIT(3) | BIT(2))
84 #define AQCSFRC_CSFB_FRCDIS 0
85 #define AQCSFRC_CSFB_FRCLOW BIT(2)
86 #define AQCSFRC_CSFB_FRCHIGH BIT(3)
87 #define AQCSFRC_CSFB_DISSWFRC (BIT(3) | BIT(2))
88 #define AQCSFRC_CSFA_MASK (BIT(1) | BIT(0))
89 #define AQCSFRC_CSFA_FRCDIS 0
90 #define AQCSFRC_CSFA_FRCLOW BIT(0)
91 #define AQCSFRC_CSFA_FRCHIGH BIT(1)
92 #define AQCSFRC_CSFA_DISSWFRC (BIT(1) | BIT(0))
94 #define NUM_PWM_CHANNEL 2 /* EHRPWM channels */
96 struct ehrpwm_context
{
107 struct ehrpwm_pwm_chip
{
108 unsigned long clk_rate
;
109 void __iomem
*mmio_base
;
110 unsigned long period_cycles
[NUM_PWM_CHANNEL
];
111 enum pwm_polarity polarity
[NUM_PWM_CHANNEL
];
113 struct ehrpwm_context ctx
;
116 static inline struct ehrpwm_pwm_chip
*to_ehrpwm_pwm_chip(struct pwm_chip
*chip
)
118 return pwmchip_get_drvdata(chip
);
121 static inline u16
ehrpwm_read(void __iomem
*base
, unsigned int offset
)
123 return readw(base
+ offset
);
126 static inline void ehrpwm_write(void __iomem
*base
, unsigned int offset
,
129 writew(value
, base
+ offset
);
132 static void ehrpwm_modify(void __iomem
*base
, unsigned int offset
, u16 mask
,
137 val
= readw(base
+ offset
);
140 writew(val
, base
+ offset
);
144 * set_prescale_div - Set up the prescaler divider function
145 * @rqst_prescaler: prescaler value min
146 * @prescale_div: prescaler value set
147 * @tb_clk_div: Time Base Control prescaler bits
149 static int set_prescale_div(unsigned long rqst_prescaler
, u16
*prescale_div
,
152 unsigned int clkdiv
, hspclkdiv
;
154 for (clkdiv
= 0; clkdiv
<= CLKDIV_MAX
; clkdiv
++) {
155 for (hspclkdiv
= 0; hspclkdiv
<= HSPCLKDIV_MAX
; hspclkdiv
++) {
157 * calculations for prescaler value :
158 * prescale_div = HSPCLKDIVIDER * CLKDIVIDER.
159 * HSPCLKDIVIDER = 2 ** hspclkdiv
160 * CLKDIVIDER = (1), if clkdiv == 0 *OR*
161 * (2 * clkdiv), if clkdiv != 0
163 * Configure prescale_div value such that period
164 * register value is less than 65535.
167 *prescale_div
= (1 << clkdiv
) *
168 (hspclkdiv
? (hspclkdiv
* 2) : 1);
169 if (*prescale_div
> rqst_prescaler
) {
170 *tb_clk_div
= (clkdiv
<< TBCTL_CLKDIV_SHIFT
) |
171 (hspclkdiv
<< TBCTL_HSPCLKDIV_SHIFT
);
180 static void configure_polarity(struct ehrpwm_pwm_chip
*pc
, int chan
)
182 u16 aqctl_val
, aqctl_mask
;
183 unsigned int aqctl_reg
;
186 * Configure PWM output to HIGH/LOW level on counter
187 * reaches compare register value and LOW/HIGH level
188 * on counter value reaches period register value and
189 * zero value on counter
193 aqctl_mask
= AQCTL_CBU_MASK
;
195 if (pc
->polarity
[chan
] == PWM_POLARITY_INVERSED
)
196 aqctl_val
= AQCTL_CHANB_POLINVERSED
;
198 aqctl_val
= AQCTL_CHANB_POLNORMAL
;
201 aqctl_mask
= AQCTL_CAU_MASK
;
203 if (pc
->polarity
[chan
] == PWM_POLARITY_INVERSED
)
204 aqctl_val
= AQCTL_CHANA_POLINVERSED
;
206 aqctl_val
= AQCTL_CHANA_POLNORMAL
;
209 aqctl_mask
|= AQCTL_PRD_MASK
| AQCTL_ZRO_MASK
;
210 ehrpwm_modify(pc
->mmio_base
, aqctl_reg
, aqctl_mask
, aqctl_val
);
214 * period_ns = 10^9 * (ps_divval * period_cycles) / PWM_CLK_RATE
215 * duty_ns = 10^9 * (ps_divval * duty_cycles) / PWM_CLK_RATE
217 static int ehrpwm_pwm_config(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
218 u64 duty_ns
, u64 period_ns
)
220 struct ehrpwm_pwm_chip
*pc
= to_ehrpwm_pwm_chip(chip
);
221 u32 period_cycles
, duty_cycles
;
222 u16 ps_divval
, tb_divval
;
223 unsigned int i
, cmp_reg
;
224 unsigned long long c
;
226 if (period_ns
> NSEC_PER_SEC
)
231 do_div(c
, NSEC_PER_SEC
);
232 period_cycles
= (unsigned long)c
;
234 if (period_cycles
< 1) {
240 do_div(c
, NSEC_PER_SEC
);
241 duty_cycles
= (unsigned long)c
;
245 * Period values should be same for multiple PWM channels as IP uses
246 * same period register for multiple channels.
248 for (i
= 0; i
< NUM_PWM_CHANNEL
; i
++) {
249 if (pc
->period_cycles
[i
] &&
250 (pc
->period_cycles
[i
] != period_cycles
)) {
252 * Allow channel to reconfigure period if no other
253 * channels being configured.
258 dev_err(pwmchip_parent(chip
),
259 "period value conflicts with channel %u\n",
265 pc
->period_cycles
[pwm
->hwpwm
] = period_cycles
;
267 /* Configure clock prescaler to support Low frequency PWM wave */
268 if (set_prescale_div(period_cycles
/PERIOD_MAX
, &ps_divval
,
270 dev_err(pwmchip_parent(chip
), "Unsupported values\n");
274 pm_runtime_get_sync(pwmchip_parent(chip
));
276 /* Update clock prescaler values */
277 ehrpwm_modify(pc
->mmio_base
, TBCTL
, TBCTL_CLKDIV_MASK
, tb_divval
);
279 /* Update period & duty cycle with presacler division */
280 period_cycles
= period_cycles
/ ps_divval
;
281 duty_cycles
= duty_cycles
/ ps_divval
;
283 /* Configure shadow loading on Period register */
284 ehrpwm_modify(pc
->mmio_base
, TBCTL
, TBCTL_PRDLD_MASK
, TBCTL_PRDLD_SHDW
);
286 ehrpwm_write(pc
->mmio_base
, TBPRD
, period_cycles
);
288 /* Configure ehrpwm counter for up-count mode */
289 ehrpwm_modify(pc
->mmio_base
, TBCTL
, TBCTL_CTRMODE_MASK
,
293 /* Channel 1 configured with compare B register */
296 /* Channel 0 configured with compare A register */
299 ehrpwm_write(pc
->mmio_base
, cmp_reg
, duty_cycles
);
301 pm_runtime_put_sync(pwmchip_parent(chip
));
306 static int ehrpwm_pwm_set_polarity(struct pwm_chip
*chip
,
307 struct pwm_device
*pwm
,
308 enum pwm_polarity polarity
)
310 struct ehrpwm_pwm_chip
*pc
= to_ehrpwm_pwm_chip(chip
);
312 /* Configuration of polarity in hardware delayed, do at enable */
313 pc
->polarity
[pwm
->hwpwm
] = polarity
;
318 static int ehrpwm_pwm_enable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
320 struct ehrpwm_pwm_chip
*pc
= to_ehrpwm_pwm_chip(chip
);
321 u16 aqcsfrc_val
, aqcsfrc_mask
;
324 /* Leave clock enabled on enabling PWM */
325 pm_runtime_get_sync(pwmchip_parent(chip
));
327 /* Disabling Action Qualifier on PWM output */
329 aqcsfrc_val
= AQCSFRC_CSFB_FRCDIS
;
330 aqcsfrc_mask
= AQCSFRC_CSFB_MASK
;
332 aqcsfrc_val
= AQCSFRC_CSFA_FRCDIS
;
333 aqcsfrc_mask
= AQCSFRC_CSFA_MASK
;
336 /* Changes to shadow mode */
337 ehrpwm_modify(pc
->mmio_base
, AQSFRC
, AQSFRC_RLDCSF_MASK
,
340 ehrpwm_modify(pc
->mmio_base
, AQCSFRC
, aqcsfrc_mask
, aqcsfrc_val
);
342 /* Channels polarity can be configured from action qualifier module */
343 configure_polarity(pc
, pwm
->hwpwm
);
346 ret
= clk_enable(pc
->tbclk
);
348 dev_err(pwmchip_parent(chip
), "Failed to enable TBCLK for %s: %d\n",
349 dev_name(pwmchip_parent(chip
)), ret
);
356 static void ehrpwm_pwm_disable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
358 struct ehrpwm_pwm_chip
*pc
= to_ehrpwm_pwm_chip(chip
);
359 u16 aqcsfrc_val
, aqcsfrc_mask
;
361 /* Action Qualifier puts PWM output low forcefully */
363 aqcsfrc_val
= AQCSFRC_CSFB_FRCLOW
;
364 aqcsfrc_mask
= AQCSFRC_CSFB_MASK
;
366 aqcsfrc_val
= AQCSFRC_CSFA_FRCLOW
;
367 aqcsfrc_mask
= AQCSFRC_CSFA_MASK
;
370 /* Update shadow register first before modifying active register */
371 ehrpwm_modify(pc
->mmio_base
, AQSFRC
, AQSFRC_RLDCSF_MASK
,
373 ehrpwm_modify(pc
->mmio_base
, AQCSFRC
, aqcsfrc_mask
, aqcsfrc_val
);
375 * Changes to immediate action on Action Qualifier. This puts
376 * Action Qualifier control on PWM output from next TBCLK
378 ehrpwm_modify(pc
->mmio_base
, AQSFRC
, AQSFRC_RLDCSF_MASK
,
381 ehrpwm_modify(pc
->mmio_base
, AQCSFRC
, aqcsfrc_mask
, aqcsfrc_val
);
383 /* Disabling TBCLK on PWM disable */
384 clk_disable(pc
->tbclk
);
386 /* Disable clock on PWM disable */
387 pm_runtime_put_sync(pwmchip_parent(chip
));
390 static void ehrpwm_pwm_free(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
392 struct ehrpwm_pwm_chip
*pc
= to_ehrpwm_pwm_chip(chip
);
394 if (pwm_is_enabled(pwm
)) {
395 dev_warn(pwmchip_parent(chip
), "Removing PWM device without disabling\n");
396 pm_runtime_put_sync(pwmchip_parent(chip
));
399 /* set period value to zero on free */
400 pc
->period_cycles
[pwm
->hwpwm
] = 0;
403 static int ehrpwm_pwm_apply(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
404 const struct pwm_state
*state
)
407 bool enabled
= pwm
->state
.enabled
;
409 if (state
->polarity
!= pwm
->state
.polarity
) {
411 ehrpwm_pwm_disable(chip
, pwm
);
415 err
= ehrpwm_pwm_set_polarity(chip
, pwm
, state
->polarity
);
420 if (!state
->enabled
) {
422 ehrpwm_pwm_disable(chip
, pwm
);
426 err
= ehrpwm_pwm_config(chip
, pwm
, state
->duty_cycle
, state
->period
);
431 err
= ehrpwm_pwm_enable(chip
, pwm
);
436 static const struct pwm_ops ehrpwm_pwm_ops
= {
437 .free
= ehrpwm_pwm_free
,
438 .apply
= ehrpwm_pwm_apply
,
441 static const struct of_device_id ehrpwm_of_match
[] = {
442 { .compatible
= "ti,am3352-ehrpwm" },
443 { .compatible
= "ti,am33xx-ehrpwm" },
446 MODULE_DEVICE_TABLE(of
, ehrpwm_of_match
);
448 static int ehrpwm_pwm_probe(struct platform_device
*pdev
)
450 struct device_node
*np
= pdev
->dev
.of_node
;
451 struct ehrpwm_pwm_chip
*pc
;
452 struct pwm_chip
*chip
;
456 chip
= devm_pwmchip_alloc(&pdev
->dev
, NUM_PWM_CHANNEL
, sizeof(*pc
));
458 return PTR_ERR(chip
);
459 pc
= to_ehrpwm_pwm_chip(chip
);
461 clk
= devm_clk_get(&pdev
->dev
, "fck");
463 if (of_device_is_compatible(np
, "ti,am33xx-ecap")) {
464 dev_warn(&pdev
->dev
, "Binding is obsolete.\n");
465 clk
= devm_clk_get(pdev
->dev
.parent
, "fck");
470 return dev_err_probe(&pdev
->dev
, PTR_ERR(clk
), "Failed to get fck\n");
472 pc
->clk_rate
= clk_get_rate(clk
);
474 dev_err(&pdev
->dev
, "failed to get clock rate\n");
478 chip
->ops
= &ehrpwm_pwm_ops
;
480 pc
->mmio_base
= devm_platform_ioremap_resource(pdev
, 0);
481 if (IS_ERR(pc
->mmio_base
))
482 return PTR_ERR(pc
->mmio_base
);
484 /* Acquire tbclk for Time Base EHRPWM submodule */
485 pc
->tbclk
= devm_clk_get(&pdev
->dev
, "tbclk");
486 if (IS_ERR(pc
->tbclk
))
487 return dev_err_probe(&pdev
->dev
, PTR_ERR(pc
->tbclk
), "Failed to get tbclk\n");
489 ret
= clk_prepare(pc
->tbclk
);
491 dev_err(&pdev
->dev
, "clk_prepare() failed: %d\n", ret
);
495 ret
= pwmchip_add(chip
);
497 dev_err(&pdev
->dev
, "pwmchip_add() failed: %d\n", ret
);
498 goto err_clk_unprepare
;
501 platform_set_drvdata(pdev
, chip
);
502 pm_runtime_enable(&pdev
->dev
);
507 clk_unprepare(pc
->tbclk
);
512 static void ehrpwm_pwm_remove(struct platform_device
*pdev
)
514 struct pwm_chip
*chip
= platform_get_drvdata(pdev
);
515 struct ehrpwm_pwm_chip
*pc
= to_ehrpwm_pwm_chip(chip
);
517 pwmchip_remove(chip
);
519 clk_unprepare(pc
->tbclk
);
521 pm_runtime_disable(&pdev
->dev
);
524 static void ehrpwm_pwm_save_context(struct pwm_chip
*chip
)
526 struct ehrpwm_pwm_chip
*pc
= to_ehrpwm_pwm_chip(chip
);
528 pm_runtime_get_sync(pwmchip_parent(chip
));
530 pc
->ctx
.tbctl
= ehrpwm_read(pc
->mmio_base
, TBCTL
);
531 pc
->ctx
.tbprd
= ehrpwm_read(pc
->mmio_base
, TBPRD
);
532 pc
->ctx
.cmpa
= ehrpwm_read(pc
->mmio_base
, CMPA
);
533 pc
->ctx
.cmpb
= ehrpwm_read(pc
->mmio_base
, CMPB
);
534 pc
->ctx
.aqctla
= ehrpwm_read(pc
->mmio_base
, AQCTLA
);
535 pc
->ctx
.aqctlb
= ehrpwm_read(pc
->mmio_base
, AQCTLB
);
536 pc
->ctx
.aqsfrc
= ehrpwm_read(pc
->mmio_base
, AQSFRC
);
537 pc
->ctx
.aqcsfrc
= ehrpwm_read(pc
->mmio_base
, AQCSFRC
);
539 pm_runtime_put_sync(pwmchip_parent(chip
));
542 static void ehrpwm_pwm_restore_context(struct pwm_chip
*chip
)
544 struct ehrpwm_pwm_chip
*pc
= to_ehrpwm_pwm_chip(chip
);
546 ehrpwm_write(pc
->mmio_base
, TBPRD
, pc
->ctx
.tbprd
);
547 ehrpwm_write(pc
->mmio_base
, CMPA
, pc
->ctx
.cmpa
);
548 ehrpwm_write(pc
->mmio_base
, CMPB
, pc
->ctx
.cmpb
);
549 ehrpwm_write(pc
->mmio_base
, AQCTLA
, pc
->ctx
.aqctla
);
550 ehrpwm_write(pc
->mmio_base
, AQCTLB
, pc
->ctx
.aqctlb
);
551 ehrpwm_write(pc
->mmio_base
, AQSFRC
, pc
->ctx
.aqsfrc
);
552 ehrpwm_write(pc
->mmio_base
, AQCSFRC
, pc
->ctx
.aqcsfrc
);
553 ehrpwm_write(pc
->mmio_base
, TBCTL
, pc
->ctx
.tbctl
);
556 static int ehrpwm_pwm_suspend(struct device
*dev
)
558 struct pwm_chip
*chip
= dev_get_drvdata(dev
);
561 ehrpwm_pwm_save_context(chip
);
563 for (i
= 0; i
< chip
->npwm
; i
++) {
564 struct pwm_device
*pwm
= &chip
->pwms
[i
];
566 if (!pwm_is_enabled(pwm
))
569 /* Disable explicitly if PWM is running */
570 pm_runtime_put_sync(dev
);
576 static int ehrpwm_pwm_resume(struct device
*dev
)
578 struct pwm_chip
*chip
= dev_get_drvdata(dev
);
581 for (i
= 0; i
< chip
->npwm
; i
++) {
582 struct pwm_device
*pwm
= &chip
->pwms
[i
];
584 if (!pwm_is_enabled(pwm
))
587 /* Enable explicitly if PWM was running */
588 pm_runtime_get_sync(dev
);
591 ehrpwm_pwm_restore_context(chip
);
596 static DEFINE_SIMPLE_DEV_PM_OPS(ehrpwm_pwm_pm_ops
, ehrpwm_pwm_suspend
,
599 static struct platform_driver ehrpwm_pwm_driver
= {
602 .of_match_table
= ehrpwm_of_match
,
603 .pm
= pm_ptr(&ehrpwm_pwm_pm_ops
),
605 .probe
= ehrpwm_pwm_probe
,
606 .remove
= ehrpwm_pwm_remove
,
608 module_platform_driver(ehrpwm_pwm_driver
);
610 MODULE_DESCRIPTION("EHRPWM PWM driver");
611 MODULE_AUTHOR("Texas Instruments");
612 MODULE_LICENSE("GPL");