Skip to contentSkip to conversation

pwm: mediatek: Add support for MT7986

PATCH In review Daniel Golle 3 participants 8 messages Oct 21, 2022 – Nov 24, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 pwm: mediatek: Add support for MT7986

Add support for PWM on MT7986 which has 2 PWM channels, one of them is typically used for a temperature controlled fan.

---
 drivers/pwm/pwm-mediatek.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/pwm/pwm-mediatek.c +7 -0
index 6901a44dc428de..2219cba033e348 100644
@@ -329,6 +329,12 @@ static const struct pwm_mediatek_of_data mt8365_pwm_data = {
 	.has_ck_26m_sel = true,
 };
 
+static const struct pwm_mediatek_of_data mt7986_pwm_data = {
+	.num_pwms = 2,
+	.pwm45_fixup = false,
+	.has_ck_26m_sel = true,
+};
+
 static const struct pwm_mediatek_of_data mt8516_pwm_data = {
 	.num_pwms = 5,
 	.pwm45_fixup = false,
@@ -342,6 +348,7 @@ static const struct of_device_id pwm_mediatek_of_match[] = {
 	{ .compatible = "mediatek,mt7623-pwm", .data = &mt7623_pwm_data },
 	{ .compatible = "mediatek,mt7628-pwm", .data = &mt7628_pwm_data },
 	{ .compatible = "mediatek,mt7629-pwm", .data = &mt7629_pwm_data },
+	{ .compatible = "mediatek,mt7986-pwm", .data = &mt7986_pwm_data },
 	{ .compatible = "mediatek,mt8183-pwm", .data = &mt8183_pwm_data },
 	{ .compatible = "mediatek,mt8365-pwm", .data = &mt8365_pwm_data },
 	{ .compatible = "mediatek,mt8516-pwm", .data = &mt8516_pwm_data },
Signature · 1 line
2.38.1
#1 lore raw

Hi Daniel:

Quoted 14 lines · Daniel Golle

Add support for PWM on MT7986 which has 2 PWM channels, one of them is
typically used for a temperature controlled fan.

---
 drivers/pwm/pwm-mediatek.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/pwm/pwm-mediatek.c +0 -0
index 6901a44dc428de..2219cba033e348 100644

drivers/pwm/pwm-mediatek.c static const struct pwm_mediatek_of_data · line 336

Quoted 8 lines
drivers/pwm/pwm-mediatek.c +4 -0
@@ -329,6 +329,12 @@ static const struct pwm_mediatek_of_data
 mt8365_pwm_data = {
 	.has_ck_26m_sel = true,
 };
 
+static const struct pwm_mediatek_of_data mt7986_pwm_data = {
+	.num_pwms = 2,
+	.pwm45_fixup = false,
+	.has_ck_26m_sel = true,

For MT7986 SoC, I think the value of "has_ck_26m_sel" should be 'false'

Quoted 14 lines
drivers/pwm/pwm-mediatek.c +2 -0
+};
+
 static const struct pwm_mediatek_of_data mt8516_pwm_data = {
 	.num_pwms = 5,
drivers/pwm/pwm-mediatek.c +0 -0
 	.pwm45_fixup = false,
@@ -342,6 +348,7 @@ static const struct of_device_id
 pwm_mediatek_of_match[] = {
 	{ .compatible = "mediatek,mt7623-pwm", .data = &mt7623_pwm_data
 },
 	{ .compatible = "mediatek,mt7628-pwm", .data = &mt7628_pwm_data
 },
 	{ .compatible = "mediatek,mt7629-pwm", .data = &mt7629_pwm_data

},

drivers/pwm/pwm-mediatek.c +1 -0
+	{ .compatible = "mediatek,mt7986-pwm", .data = &mt7986_pwm_data
Quoted 7 lines

},
{ .compatible = "mediatek,mt8183-pwm", .data = &mt8183_pwm_data
},
{ .compatible = "mediatek,mt8365-pwm", .data = &mt8365_pwm_data
},
{ .compatible = "mediatek,mt8516-pwm", .data = &mt8516_pwm_data
},

Regards,
Sam

#2 lore raw
Quoted 21 lines · Sam Shih

Hi Daniel:

Quoted 18 lines · Daniel Golle

Add support for PWM on MT7986 which has 2 PWM channels, one of them is
typically used for a temperature controlled fan.

---
 drivers/pwm/pwm-mediatek.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/pwm/pwm-mediatek.c +0 -0
index 6901a44dc428de..2219cba033e348 100644
@@ -329,6 +329,12 @@ static const struct pwm_mediatek_of_data
 mt8365_pwm_data = {
 	.has_ck_26m_sel = true,
 };
 
Quoted 6 lines

drivers/pwm/pwm-mediatek.c static const struct pwm_mediatek_of_data mt8365_pwm_data = { · line 335

Quoted 4 lines
drivers/pwm/pwm-mediatek.c +4 -0
+static const struct pwm_mediatek_of_data mt7986_pwm_data = {
+	.num_pwms = 2,
+	.pwm45_fixup = false,
+	.has_ck_26m_sel = true,

For MT7986 SoC, I think the value of "has_ck_26m_sel" should be 'false'

That's a bit surprising, please explain why.

Reading the commit adding .has_ck_26m_sel field:

Quoted 21 lines

commit 0c0ead76235db0bcfaab83f04db546995449d002

Author: Fabien Parent <[email protected]>
Date:   Mon Oct 19 16:07:02 2020 +0200

pwm: mediatek: Always use bus clock

The MediaTek PWM IP can sometimes use the 26 MHz source clock to generate the PWM signal, but the driver currently assumes that we always use the PWM bus clock to generate the PWM signal.

This commit modifies the PWM driver in order to force the PWM IP to always use the bus clock as source clock.

I do not have the datasheet of all the MediaTek SoC, so I don't know if the register to choose the source clock is present in all the SoCs or only in subset. As a consequence I made this change optional by using a platform data paremeter to says whether this register is supported or not. On all the SoCs I don't have the datasheet (MT2712, MT7622, MT7623, MT7628, MT7629) I kept the behavior to be the same as before this change.

Quoted 3 lines

From MT7986 datasheet:

Quoted 5 lines

0x10048210 PWM_CK_26M_SEL PWM BCLK Selection
Reset value 0x00000001
Description
0: Select bus CLK as BCLK
1: Select 26M fix CLK as BCLK

So after reset, the 26M clock is selected by default.

In pwm-mediatek.c I read:

Quoted 5 lines

#define PWM_CK_26M_SEL 0x210
...

        /* Make sure we use the bus clock and not the 26MHz clock */
        if (pc->soc->has_ck_26m_sel)
                writel(0, pc->regs + PWM_CK_26M_SEL);

So this PWM_CK_26M_SEL register does exist on MT7986 and has the same address as expected by the driver ($PWM_BASE + 0x210). The default value selected after reset (0x00000001) matches the problem and solution described in the commit description "pwm: mediatek: Always use bus clock".

Sidenode: I've tried with both, .has_ck_26m_sel = true as well as .has_ck_26m_sel = false. Both do work, but the behavior is slightly different, again matching the commit description above.

Quoted 26 lines
Quoted 15 lines
drivers/pwm/pwm-mediatek.c +2 -0
+};
+
 static const struct pwm_mediatek_of_data mt8516_pwm_data = {
 	.num_pwms = 5,
drivers/pwm/pwm-mediatek.c +0 -0
 	.pwm45_fixup = false,
@@ -342,6 +348,7 @@ static const struct of_device_id
 pwm_mediatek_of_match[] = {
 	{ .compatible = "mediatek,mt7623-pwm", .data = &mt7623_pwm_data
 },
 	{ .compatible = "mediatek,mt7628-pwm", .data = &mt7628_pwm_data
 },
 	{ .compatible = "mediatek,mt7629-pwm", .data = &mt7629_pwm_data

},

drivers/pwm/pwm-mediatek.c +1 -0
+	{ .compatible = "mediatek,mt7986-pwm", .data = &mt7986_pwm_data
Quoted 7 lines

},
{ .compatible = "mediatek,mt8183-pwm", .data = &mt8183_pwm_data
},
{ .compatible = "mediatek,mt8365-pwm", .data = &mt8365_pwm_data
},
{ .compatible = "mediatek,mt8516-pwm", .data = &mt8516_pwm_data
},

Regards,
Sam

#3 lore raw
Quoted 32 lines · Daniel Golle
Quoted 23 lines · Sam Shih

Hi Daniel:

Quoted 20 lines · Daniel Golle

Add support for PWM on MT7986 which has 2 PWM channels, one of them
is
typically used for a temperature controlled fan.

---
 drivers/pwm/pwm-mediatek.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/pwm/pwm- renamed from drivers/pwm/pwm-mediatek.c +0 -0

mediatek.c
index 6901a44dc428de..2219cba033e348 100644

drivers/pwm/pwm-mediatek.c +0 -0
@@ -329,6 +329,12 @@ static const struct pwm_mediatek_of_data
 mt8365_pwm_data = {
 	.has_ck_26m_sel = true,
 };
 
Quoted 7 lines

drivers/pwm/pwm-mediatek.c static const struct pwm_mediatek_of_data mt8365_pwm_data = { · line 335

Quoted 4 lines
drivers/pwm/pwm-mediatek.c +4 -0
+static const struct pwm_mediatek_of_data mt7986_pwm_data = {
+	.num_pwms = 2,
+	.pwm45_fixup = false,
+	.has_ck_26m_sel = true,

For MT7986 SoC, I think the value of "has_ck_26m_sel" should be 'false'

Quoted 2 lines

That's a bit surprising, please explain why.

The clock tree of MT7981/MT7986 PWM BCLK is as bellow:
PLL --> topckgen fix-factors --> TOP_PWM_SEL (topckgen clock mux) --> --

Quoted 1 line

CLK_INFRA_PWM_BSEL (infra clock mux) --> PWM BCLK (gate)

We do have the clock multiplexer to select the source clock for PWM_BCLK
https://github.com/torvalds/linux/blob/master/drivers/clk/mediatek/clk-mt7986-infracfg.c#L63

In my knowledge, the pwm hardware of MT7981/MT7986 SoC should ignore this register directtly, but we still keep the register for backword compatibility.

In fact, the MT7986 SoC is also working whether 'has_ck_26m_sel' is 'true' or 'false'.

Going back to the definition of 'has_ck_26m_sel', if it means "PWM_CK_26M_SEL" register exists or not, we should use 'true', but if it means clock from 26M clock or BUS clock, we should use 'false'

Quoted 54 lines

Reading the commit adding .has_ck_26m_sel field:

Quoted 27 lines

commit 0c0ead76235db0bcfaab83f04db546995449d002

Author: Fabien Parent <[email protected]>
Date:   Mon Oct 19 16:07:02 2020 +0200

pwm: mediatek: Always use bus clock

The MediaTek PWM IP can sometimes use the 26 MHz source clock to generate the PWM signal, but the driver currently assumes that we always
use the PWM bus clock to generate the PWM signal.

This commit modifies the PWM driver in order to force the PWM IP to always use the bus clock as source clock.

I do not have the datasheet of all the MediaTek SoC, so I don't know if
the register to choose the source clock is present in all the SoCs or
only in subset. As a consequence I made this change optional by using a
platform data paremeter to says whether this register is supported or
not. On all the SoCs I don't have the datasheet (MT2712, MT7622, MT7623,
MT7628, MT7629) I kept the behavior to be the same as before this change.

Quoted 3 lines

From MT7986 datasheet:

Quoted 5 lines

0x10048210 PWM_CK_26M_SEL PWM BCLK Selection
Reset value 0x00000001
Description
0: Select bus CLK as BCLK
1: Select 26M fix CLK as BCLK

So after reset, the 26M clock is selected by default.

In pwm-mediatek.c I read:

Quoted 6 lines

#define PWM_CK_26M_SEL 0x210
...

        /* Make sure we use the bus clock and not the 26MHz clock
*/
        if (pc->soc->has_ck_26m_sel)
                writel(0, pc->regs + PWM_CK_26M_SEL);

So this PWM_CK_26M_SEL register does exist on MT7986 and has the same address as expected by the driver ($PWM_BASE + 0x210). The default value selected after reset (0x00000001) matches the problem and solution described in the commit description "pwm: mediatek: Always use bus clock".

Quoted 3 lines

Sidenode: I've tried with both, .has_ck_26m_sel = true as well as .has_ck_26m_sel = false. Both do work, but the behavior is slightly different, again matching the commit description above.

What is the difference between the two?

I tried to config the pwm ch0 period=1000000 and duty=500000, Modify PWM_CK_26M_SEL, and measure the output waveform, the waveform keep the same.

Regards,
Sam

#4 lore raw
Quoted 48 lines · Sam Shih
Quoted 32 lines · Daniel Golle
Quoted 23 lines · Sam Shih

Hi Daniel:

Quoted 20 lines · Daniel Golle

Add support for PWM on MT7986 which has 2 PWM channels, one of them
is
typically used for a temperature controlled fan.

---
 drivers/pwm/pwm-mediatek.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/pwm/pwm- renamed from drivers/pwm/pwm-mediatek.c +0 -0

mediatek.c
index 6901a44dc428de..2219cba033e348 100644

drivers/pwm/pwm-mediatek.c +0 -0
@@ -329,6 +329,12 @@ static const struct pwm_mediatek_of_data
 mt8365_pwm_data = {
 	.has_ck_26m_sel = true,
 };
 
Quoted 7 lines

drivers/pwm/pwm-mediatek.c static const struct pwm_mediatek_of_data mt8365_pwm_data = { · line 335

Quoted 4 lines
drivers/pwm/pwm-mediatek.c +4 -0
+static const struct pwm_mediatek_of_data mt7986_pwm_data = {
+	.num_pwms = 2,
+	.pwm45_fixup = false,
+	.has_ck_26m_sel = true,

For MT7986 SoC, I think the value of "has_ck_26m_sel" should be 'false'

Quoted 2 lines

That's a bit surprising, please explain why.

The clock tree of MT7981/MT7986 PWM BCLK is as bellow:
PLL --> topckgen fix-factors --> TOP_PWM_SEL (topckgen clock mux) --> --

Quoted 1 line

CLK_INFRA_PWM_BSEL (infra clock mux) --> PWM BCLK (gate)

We do have the clock multiplexer to select the source clock for PWM_BCLK
https://github.com/torvalds/linux/blob/master/drivers/clk/mediatek/clk-mt7986-infracfg.c#L63

In my knowledge, the pwm hardware of MT7981/MT7986 SoC should ignore this register directtly, but we still keep the register for backword compatibility.

Quoted 6 lines

In fact, the MT7986 SoC is also working whether 'has_ck_26m_sel' is 'true' or 'false'.

Going back to the definition of 'has_ck_26m_sel', if it means "PWM_CK_26M_SEL" register exists or not, we should use 'true', but if it means clock from 26M clock or BUS clock, we should use 'false'

It means 'write 0 to PWM_CK_26M_SEL to make sure PWM BCLK is selected'. If the register isn't used at all on MT7986 (despite being mentioned in the datasheet) or the value written there never has any effect then there is no need to do this and has_ck_26m_sel can be false.

Quoted 59 lines
Quoted 55 lines

Reading the commit adding .has_ck_26m_sel field:

Quoted 27 lines

commit 0c0ead76235db0bcfaab83f04db546995449d002

Author: Fabien Parent <[email protected]>
Date:   Mon Oct 19 16:07:02 2020 +0200

pwm: mediatek: Always use bus clock

The MediaTek PWM IP can sometimes use the 26 MHz source clock to generate the PWM signal, but the driver currently assumes that we always
use the PWM bus clock to generate the PWM signal.

This commit modifies the PWM driver in order to force the PWM IP to always use the bus clock as source clock.

I do not have the datasheet of all the MediaTek SoC, so I don't know if
the register to choose the source clock is present in all the SoCs or
only in subset. As a consequence I made this change optional by using a
platform data paremeter to says whether this register is supported or
not. On all the SoCs I don't have the datasheet (MT2712, MT7622, MT7623,
MT7628, MT7629) I kept the behavior to be the same as before this change.

Quoted 3 lines

From MT7986 datasheet:

Quoted 5 lines

0x10048210 PWM_CK_26M_SEL PWM BCLK Selection
Reset value 0x00000001
Description
0: Select bus CLK as BCLK
1: Select 26M fix CLK as BCLK

So after reset, the 26M clock is selected by default.

In pwm-mediatek.c I read:

Quoted 6 lines

#define PWM_CK_26M_SEL 0x210
...

        /* Make sure we use the bus clock and not the 26MHz clock
*/
        if (pc->soc->has_ck_26m_sel)
                writel(0, pc->regs + PWM_CK_26M_SEL);

So this PWM_CK_26M_SEL register does exist on MT7986 and has the same address as expected by the driver ($PWM_BASE + 0x210). The default value selected after reset (0x00000001) matches the problem and solution described in the commit description "pwm: mediatek: Always use bus clock".

Quoted 3 lines

Sidenode: I've tried with both, .has_ck_26m_sel = true as well as .has_ck_26m_sel = false. Both do work, but the behavior is slightly different, again matching the commit description above.

Quoted 5 lines

What is the difference between the two?

I tried to config the pwm ch0 period=1000000 and duty=500000, Modify PWM_CK_26M_SEL, and measure the output waveform, the waveform keep the same.

Maybe something else already sets the PWM_CK_26M_SEL register to 0 before Linux starts (e.g. U-Boot)?

#5 lore raw
In reply to Sam Shih
Quoted 21 lines · Sam Shih

Hi Daniel:

Quoted 18 lines · Daniel Golle

Add support for PWM on MT7986 which has 2 PWM channels, one of them is
typically used for a temperature controlled fan.

---
 drivers/pwm/pwm-mediatek.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/pwm/pwm-mediatek.c +0 -0
index 6901a44dc428de..2219cba033e348 100644
@@ -329,6 +329,12 @@ static const struct pwm_mediatek_of_data
 mt8365_pwm_data = {
 	.has_ck_26m_sel = true,
 };
 
Quoted 6 lines

drivers/pwm/pwm-mediatek.c static const struct pwm_mediatek_of_data mt8365_pwm_data = { · line 335

Quoted 4 lines
drivers/pwm/pwm-mediatek.c +4 -0
+static const struct pwm_mediatek_of_data mt7986_pwm_data = {
+	.num_pwms = 2,
+	.pwm45_fixup = false,
+	.has_ck_26m_sel = true,

For MT7986 SoC, I think the value of "has_ck_26m_sel" should be 'false'

The status of the discussion isn't clear to me. You didn't visibly agree which value is right now. Will there be a v2 of this patch? Or is it expected to be picked up as is.

From my side (i.e. not having checked the hw details just judging with the PWM hat on) the patch is fine.

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |
#6 lore raw
Quoted 29 lines · Uwe Kleine-König
Quoted 21 lines · Sam Shih

Hi Daniel:

Quoted 18 lines · Daniel Golle

Add support for PWM on MT7986 which has 2 PWM channels, one of them is
typically used for a temperature controlled fan.

---
 drivers/pwm/pwm-mediatek.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/pwm/pwm-mediatek.c +0 -0
index 6901a44dc428de..2219cba033e348 100644
@@ -329,6 +329,12 @@ static const struct pwm_mediatek_of_data
 mt8365_pwm_data = {
 	.has_ck_26m_sel = true,
 };
 
Quoted 6 lines

drivers/pwm/pwm-mediatek.c static const struct pwm_mediatek_of_data mt8365_pwm_data = { · line 335

Quoted 4 lines
drivers/pwm/pwm-mediatek.c +4 -0
+static const struct pwm_mediatek_of_data mt7986_pwm_data = {
+	.num_pwms = 2,
+	.pwm45_fixup = false,
+	.has_ck_26m_sel = true,

For MT7986 SoC, I think the value of "has_ck_26m_sel" should be 'false'

Quoted 3 lines

The status of the discussion isn't clear to me. You didn't visibly agree which value is right now. Will there be a v2 of this patch? Or is it expected to be picked up as is.

The patch has been tested thoroughly and works well as-is. The CK_26M_SEL register does exist on MT7986 and, at least according to the datasheet[1], is set to the wrong value (selecting 26M clock) on reset (but probably then already set to 0 to select the bus clock, e.g. by the bootloader). So in the worst case, this is a no-op.

[1]: MT7986 Reference Manual, version 1.0 released 2022-05-29, page 428
available at https://wiki.banana-pi.org/Banana_Pi_BPI-R3#Documents

Quoted 10 lines

From my side (i.e. not having checked the hw details just judging with the PWM hat on) the patch is fine.

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |

/

Reviewed-by
#7 lore raw
In reply to Uwe Kleine-König

Hi Uwe,

Sorry for the late reply.

Since the CK_26M_SEL register does exist on MT7986, and for the other mediatek SoCs, we should update the value of the CK_26M_SEL register if it exists on the SoC, even the MT7986's PWM hardware just bypasses this setting.

From this point of view, I think it is fine to mark '.has_ck_26m_sel' attribute to 'true'.

For this patch:

Thanks,
Best Regards,
Sam Shih

Quoted 44 lines · Uwe Kleine-König
Quoted 23 lines · Sam Shih

Hi Daniel:

Quoted 20 lines · Daniel Golle

Add support for PWM on MT7986 which has 2 PWM channels, one of them
is
typically used for a temperature controlled fan.

---
 drivers/pwm/pwm-mediatek.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/pwm/pwm- renamed from drivers/pwm/pwm-mediatek.c +0 -0

mediatek.c
index 6901a44dc428de..2219cba033e348 100644

drivers/pwm/pwm-mediatek.c +0 -0
@@ -329,6 +329,12 @@ static const struct pwm_mediatek_of_data
 mt8365_pwm_data = {
 	.has_ck_26m_sel = true,
 };
 
Quoted 7 lines

drivers/pwm/pwm-mediatek.c static const struct pwm_mediatek_of_data mt8365_pwm_data = { · line 335

Quoted 4 lines
drivers/pwm/pwm-mediatek.c +4 -0
+static const struct pwm_mediatek_of_data mt7986_pwm_data = {
+	.num_pwms = 2,
+	.pwm45_fixup = false,
+	.has_ck_26m_sel = true,

For MT7986 SoC, I think the value of "has_ck_26m_sel" should be 'false'

The status of the discussion isn't clear to me. You didn't visibly agree
which value is right now. Will there be a v2 of this patch? Or is it expected to be picked up as is.

From my side (i.e. not having checked the hw details just judging with
the PWM hat on) the patch is fine.

Best regards
Uwe