21 #include <linux/module.h>
35 #define TBCTL_RUN_MASK (BIT(15) | BIT(14))
36 #define TBCTL_STOP_NEXT 0
37 #define TBCTL_STOP_ON_CYCLE BIT(14)
38 #define TBCTL_FREE_RUN (BIT(15) | BIT(14))
39 #define TBCTL_PRDLD_MASK BIT(3)
40 #define TBCTL_PRDLD_SHDW 0
41 #define TBCTL_PRDLD_IMDT BIT(3)
42 #define TBCTL_CLKDIV_MASK (BIT(12) | BIT(11) | BIT(10) | BIT(9) | \
44 #define TBCTL_CTRMODE_MASK (BIT(1) | BIT(0))
45 #define TBCTL_CTRMODE_UP 0
46 #define TBCTL_CTRMODE_DOWN BIT(0)
47 #define TBCTL_CTRMODE_UPDOWN BIT(1)
48 #define TBCTL_CTRMODE_FREEZE (BIT(1) | BIT(0))
50 #define TBCTL_HSPCLKDIV_SHIFT 7
51 #define TBCTL_CLKDIV_SHIFT 10
54 #define HSPCLKDIV_MAX 7
55 #define PERIOD_MAX 0xFFFF
67 #define AQCTL_CBU_MASK (BIT(9) | BIT(8))
68 #define AQCTL_CBU_FRCLOW BIT(8)
69 #define AQCTL_CBU_FRCHIGH BIT(9)
70 #define AQCTL_CBU_FRCTOGGLE (BIT(9) | BIT(8))
71 #define AQCTL_CAU_MASK (BIT(5) | BIT(4))
72 #define AQCTL_CAU_FRCLOW BIT(4)
73 #define AQCTL_CAU_FRCHIGH BIT(5)
74 #define AQCTL_CAU_FRCTOGGLE (BIT(5) | BIT(4))
75 #define AQCTL_PRD_MASK (BIT(3) | BIT(2))
76 #define AQCTL_PRD_FRCLOW BIT(2)
77 #define AQCTL_PRD_FRCHIGH BIT(3)
78 #define AQCTL_PRD_FRCTOGGLE (BIT(3) | BIT(2))
79 #define AQCTL_ZRO_MASK (BIT(1) | BIT(0))
80 #define AQCTL_ZRO_FRCLOW BIT(0)
81 #define AQCTL_ZRO_FRCHIGH BIT(1)
82 #define AQCTL_ZRO_FRCTOGGLE (BIT(1) | BIT(0))
84 #define AQCTL_CHANA_POLNORMAL (AQCTL_CAU_FRCLOW | AQCTL_PRD_FRCHIGH | \
86 #define AQCTL_CHANA_POLINVERSED (AQCTL_CAU_FRCHIGH | AQCTL_PRD_FRCLOW | \
88 #define AQCTL_CHANB_POLNORMAL (AQCTL_CBU_FRCLOW | AQCTL_PRD_FRCHIGH | \
90 #define AQCTL_CHANB_POLINVERSED (AQCTL_CBU_FRCHIGH | AQCTL_PRD_FRCLOW | \
93 #define AQSFRC_RLDCSF_MASK (BIT(7) | BIT(6))
94 #define AQSFRC_RLDCSF_ZRO 0
95 #define AQSFRC_RLDCSF_PRD BIT(6)
96 #define AQSFRC_RLDCSF_ZROPRD BIT(7)
97 #define AQSFRC_RLDCSF_IMDT (BIT(7) | BIT(6))
99 #define AQCSFRC_CSFB_MASK (BIT(3) | BIT(2))
100 #define AQCSFRC_CSFB_FRCDIS 0
101 #define AQCSFRC_CSFB_FRCLOW BIT(2)
102 #define AQCSFRC_CSFB_FRCHIGH BIT(3)
103 #define AQCSFRC_CSFB_DISSWFRC (BIT(3) | BIT(2))
104 #define AQCSFRC_CSFA_MASK (BIT(1) | BIT(0))
105 #define AQCSFRC_CSFA_FRCDIS 0
106 #define AQCSFRC_CSFA_FRCLOW BIT(0)
107 #define AQCSFRC_CSFA_FRCHIGH BIT(1)
108 #define AQCSFRC_CSFA_DISSWFRC (BIT(1) | BIT(0))
110 #define NUM_PWM_CHANNEL 2
125 static void ehrpwm_write(
void *base,
int offset,
unsigned int val)
127 writew(val & 0xFFFF, base + offset);
130 static void ehrpwm_modify(
void *base,
int offset,
131 unsigned short mask,
unsigned short val)
135 regval =
readw(base + offset);
137 regval |= val &
mask;
138 writew(regval, base + offset);
147 static int set_prescale_div(
unsigned long rqst_prescaler,
148 unsigned short *prescale_div,
unsigned short *tb_clk_div)
150 unsigned int clkdiv, hspclkdiv;
152 for (clkdiv = 0; clkdiv <=
CLKDIV_MAX; clkdiv++) {
153 for (hspclkdiv = 0; hspclkdiv <=
HSPCLKDIV_MAX; hspclkdiv++) {
166 *prescale_div = (1 << clkdiv) *
167 (hspclkdiv ? (hspclkdiv * 2) : 1);
168 if (*prescale_div > rqst_prescaler) {
181 unsigned short aqctl_val, aqctl_mask;
208 ehrpwm_modify(pc->
mmio_base, aqctl_reg, aqctl_mask, aqctl_val);
216 int duty_ns,
int period_ns)
219 unsigned long long c;
221 unsigned short ps_divval, tb_divval;
230 period_cycles = (
unsigned long)c;
232 if (period_cycles < 1) {
239 duty_cycles = (
unsigned long)c;
256 dev_err(chip->
dev,
"Period value conflicts with channel %d\n",
265 if (set_prescale_div(period_cycles/
PERIOD_MAX, &ps_divval,
271 pm_runtime_get_sync(chip->
dev);
277 period_cycles = period_cycles / ps_divval;
278 duty_cycles = duty_cycles / ps_divval;
296 ehrpwm_write(pc->
mmio_base, cmp_reg, duty_cycles);
298 pm_runtime_put_sync(chip->
dev);
302 static int ehrpwm_pwm_set_polarity(
struct pwm_chip *chip,
315 unsigned short aqcsfrc_val, aqcsfrc_mask;
318 pm_runtime_get_sync(chip->
dev);
336 configure_polarity(pc, pwm->
hwpwm);
346 unsigned short aqcsfrc_val, aqcsfrc_mask;
370 pm_runtime_put_sync(chip->
dev);
378 dev_warn(chip->
dev,
"Removing PWM device without disabling\n");
379 pm_runtime_put_sync(chip->
dev);
386 static const struct pwm_ops ehrpwm_pwm_ops = {
387 .free = ehrpwm_pwm_free,
388 .config = ehrpwm_pwm_config,
389 .set_polarity = ehrpwm_pwm_set_polarity,
390 .enable = ehrpwm_pwm_enable,
391 .disable = ehrpwm_pwm_disable,
404 dev_err(&pdev->
dev,
"failed to allocate memory\n");
416 dev_err(&pdev->
dev,
"failed to get clock rate\n");
421 pc->
chip.ops = &ehrpwm_pwm_ops;
427 dev_err(&pdev->
dev,
"no memory resource defined\n");
437 dev_err(&pdev->
dev,
"pwmchip_add() failed: %d\n", ret);
442 platform_set_drvdata(pdev, pc);
450 pm_runtime_put_sync(&pdev->
dev);
451 pm_runtime_disable(&pdev->
dev);
459 .probe = ehrpwm_pwm_probe,