Skip to contentSkip to conversation

pwm: jz4740: Drop dependency on MACH_INGENIC

PATCH Superseded Paul Cercueil 2 participants 9 messages Apr 13, 2020 – May 24, 2020

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Paul Cercueil

author maintainer
#0 lore raw

PATCH 1/3 pwm: jz4740: Drop dependency on MACH_INGENIC

Depending on MACH_INGENIC prevent us from creating a generic kernel that works on more than one MIPS board. Instead, we just depend on MIPS being set.

---
 drivers/pwm/Kconfig | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pwm/Kconfig +1 -1
index eebbc917ac97..7814e5b2cad7 100644
@@ -234,7 +234,7 @@ config PWM_IMX_TPM
 
 config PWM_JZ4740
 	tristate "Ingenic JZ47xx PWM support"
-	depends on MACH_INGENIC
+	depends on MIPS
 	depends on COMMON_CLK
 	select MFD_SYSCON
 	help
Signature · 1 line
2.25.1
author maintainer
#1 lore raw

PATCH 2/3 pwm: jz4740: Make PWM start with the active part

The PWM would previously always start with the inactive part. To counter that, the common trick is to use an inverted duty as the real duty (as in, 'period - duty'), and invert the polarity when the PWM is enabled.

However, for some reason the driver was already configuring the hardware for an inverted duty, so inverting it again means we do configure the hardware with the actual duty value.

---
 drivers/pwm/pwm-jz4740.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)
drivers/pwm/pwm-jz4740.c +3 -8
index 3cd5c054ad9a..f566f9d248d6 100644
@@ -6,7 +6,6 @@
  * Limitations:
  * - The .apply callback doesn't complete the currently running period before
  *   reconfiguring the hardware.
- * - Each period starts with the inactive part.
  */
 
 #include <linux/clk.h>
@@ -163,7 +162,7 @@ static int jz4740_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
 	/* Calculate duty value */
 	tmp = (unsigned long long)period * state->duty_cycle;
 	do_div(tmp, state->period);
-	duty = period - tmp;
+	duty = (unsigned long)tmp;
 
 	if (duty >= period)
 		duty = period - 1;
@@ -190,17 +189,13 @@ static int jz4740_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
 			   TCU_TCSR_PWM_SD, TCU_TCSR_PWM_SD);
 
 	/* Set polarity */
-	switch (state->polarity) {
-	case PWM_POLARITY_NORMAL:
+	if ((state->polarity != PWM_POLARITY_INVERSED) ^ state->enabled)
 		regmap_update_bits(jz4740->map, TCU_REG_TCSRc(pwm->hwpwm),
 				   TCU_TCSR_PWM_INITL_HIGH, 0);
-		break;
-	case PWM_POLARITY_INVERSED:
+	else
 		regmap_update_bits(jz4740->map, TCU_REG_TCSRc(pwm->hwpwm),
 				   TCU_TCSR_PWM_INITL_HIGH,
 				   TCU_TCSR_PWM_INITL_HIGH);
-		break;
-	}
 
 	if (state->enabled)
 		jz4740_pwm_enable(chip, pwm);
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH 2/3] pwm: jz4740: Make PWM start with the active part

drivers/pwm/pwm-jz4740.c static int jz4740_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,

Quoted 27 lines · Paul Cercueil

The PWM would previously always start with the inactive part. To counter that, the common trick is to use an inverted duty as the real duty (as in, 'period - duty'), and invert the polarity when the PWM is enabled.

However, for some reason the driver was already configuring the hardware for an inverted duty, so inverting it again means we do configure the hardware with the actual duty value.

---
 drivers/pwm/pwm-jz4740.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)
drivers/pwm/pwm-jz4740.c +0 -1
index 3cd5c054ad9a..f566f9d248d6 100644
@@ -6,7 +6,6 @@
  * Limitations:
  * - The .apply callback doesn't complete the currently running period before
  *   reconfiguring the hardware.
- * - Each period starts with the inactive part.
  */
 
 #include <linux/clk.h>
@@ -163,7 +162,7 @@ static int jz4740_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,

Side note: in the calculation above the driver is loosing precision as the calculation for the duty counter uses the (already rounded) stuff for the period counter.

The currently used calculation is (somewhat simplified syntax):

	period = (rate * requested_period) // NSEC_PER_SEC;
	duty = (period * requested_duty) // requested_period

The output then is an actual period of

period * NSEC_PER_SEC / rate

and the duty cycle is

duty * NSEC_PER_SEC / rate

.

Consider the following:

	rate = 23856100
	requested_period = 20959
	requested_duty = 16000

With the currently implemented algorithm we get:

	period = 499
	duty = 380

yielding a real period of 20917.08 ns (ok) and a duty cycle of 15928.84 ns. With duty = 381 we'd get 15970.758 ns which is better.

To reach that we'd need:

	/* Calculate duty value */
	tmp = (unsigned long long)rate * state->duty_cycle;
	do_div(tmp, NSEC_PER_SEC);
	duty = tmp

drivers/pwm/pwm-jz4740.c static int jz4740_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, · line 165

Quoted 5 lines
drivers/pwm/pwm-jz4740.c +1 -1
 	/* Calculate duty value */
 	tmp = (unsigned long long)period * state->duty_cycle;
 	do_div(tmp, state->period);
-	duty = period - tmp;
+	duty = (unsigned long)tmp;

That cast is unnecessary.

drivers/pwm/pwm-jz4740.c static int jz4740_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, · line 192

Quoted 10 lines
drivers/pwm/pwm-jz4740.c +1 -2
 	if (duty >= period)
 		duty = period - 1;
@@ -190,17 +189,13 @@ static int jz4740_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
 			   TCU_TCSR_PWM_SD, TCU_TCSR_PWM_SD);
 
 	/* Set polarity */
-	switch (state->polarity) {
-	case PWM_POLARITY_NORMAL:
+	if ((state->polarity != PWM_POLARITY_INVERSED) ^ state->enabled)

This needs to be more complicated than before as a disabled PWM needs the other setting to yield its inactive level?

Maybe worth a comment?

drivers/pwm/pwm-jz4740.c static int jz4740_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, · line 203

Quoted 10 lines
drivers/pwm/pwm-jz4740.c +1 -4
 		regmap_update_bits(jz4740->map, TCU_REG_TCSRc(pwm->hwpwm),
 				   TCU_TCSR_PWM_INITL_HIGH, 0);
-		break;
-	case PWM_POLARITY_INVERSED:
+	else
 		regmap_update_bits(jz4740->map, TCU_REG_TCSRc(pwm->hwpwm),
 				   TCU_TCSR_PWM_INITL_HIGH,
 				   TCU_TCSR_PWM_INITL_HIGH);
-		break;
-	}

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |
author maintainer
#3 lore raw

PATCH 3/3 pwm: jz4740: Add support for the JZ4725B

In reply to Paul Cercueil

The PWM hardware in the JZ4725B works the same as in the JZ4740, but has only six channels available.

---

Notes:
I did not add documentation for the new jz4725b-pwm compatible string on purpose. The reason is that the documentation file for the Timer/Counter Unit (TCU) of Ingenic SoCs will be completely rewritten from .txt to YAML in a separate patchset.

 drivers/pwm/pwm-jz4740.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
drivers/pwm/pwm-jz4740.c +17 -3
index f566f9d248d6..bb27934fb6c2 100644
@@ -22,6 +22,10 @@
 
 #define NUM_PWM 8
 
+struct soc_info {
+	unsigned int num_pwms;
+};
+
 struct jz4740_pwm_chip {
 	struct pwm_chip chip;
 	struct regmap *map;
@@ -36,7 +40,7 @@ static bool jz4740_pwm_can_use_chn(struct jz4740_pwm_chip *jz,
 				   unsigned int channel)
 {
 	/* Enable all TCU channels for PWM use by default except channels 0/1 */
-	u32 pwm_channels_mask = GENMASK(NUM_PWM - 1, 2);
+	u32 pwm_channels_mask = GENMASK(jz->chip.npwm - 1, 2);
 
 	device_property_read_u32(jz->chip.dev->parent,
 				 "ingenic,pwm-channels-mask",
@@ -214,6 +218,7 @@ static int jz4740_pwm_probe(struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
 	struct jz4740_pwm_chip *jz4740;
+	const struct soc_info *info = device_get_match_data(dev);
 
 	jz4740 = devm_kzalloc(dev, sizeof(*jz4740), GFP_KERNEL);
 	if (!jz4740)
@@ -227,8 +232,8 @@ static int jz4740_pwm_probe(struct platform_device *pdev)
 
 	jz4740->chip.dev = dev;
 	jz4740->chip.ops = &jz4740_pwm_ops;
-	jz4740->chip.npwm = NUM_PWM;
 	jz4740->chip.base = -1;
+	jz4740->chip.npwm = info ? info->num_pwms : NUM_PWM;
 	jz4740->chip.of_xlate = of_pwm_xlate_with_flags;
 	jz4740->chip.of_pwm_n_cells = 3;
 
@@ -244,9 +249,18 @@ static int jz4740_pwm_remove(struct platform_device *pdev)
 	return pwmchip_remove(&jz4740->chip);
 }
 
+static const struct soc_info __maybe_unused jz4740_soc_info = {
+	.num_pwms = 8,
+};
+
+static const struct soc_info __maybe_unused jz4725b_soc_info = {
+	.num_pwms = 6,
+};
+
 #ifdef CONFIG_OF
 static const struct of_device_id jz4740_pwm_dt_ids[] = {
-	{ .compatible = "ingenic,jz4740-pwm", },
+	{ .compatible = "ingenic,jz4740-pwm", .data = &jz4740_soc_info },
+	{ .compatible = "ingenic,jz4725b-pwm", .data = &jz4725b_soc_info },
 	{},
 };
 MODULE_DEVICE_TABLE(of, jz4740_pwm_dt_ids);
Signature · 1 line
2.25.1
#4 lore raw

Re: [PATCH 3/3] pwm: jz4740: Add support for the JZ4725B

Quoted 48 lines · Paul Cercueil

The PWM hardware in the JZ4725B works the same as in the JZ4740, but has only six channels available.

---

Notes:
I did not add documentation for the new jz4725b-pwm compatible string on purpose. The reason is that the documentation file for the Timer/Counter Unit (TCU) of Ingenic SoCs will be completely rewritten from .txt to YAML in a separate patchset.

 drivers/pwm/pwm-jz4740.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
drivers/pwm/pwm-jz4740.c +6 -1
index f566f9d248d6..bb27934fb6c2 100644
@@ -22,6 +22,10 @@
 
 #define NUM_PWM 8
 
+struct soc_info {
+	unsigned int num_pwms;
+};
+
 struct jz4740_pwm_chip {
 	struct pwm_chip chip;
 	struct regmap *map;
@@ -36,7 +40,7 @@ static bool jz4740_pwm_can_use_chn(struct jz4740_pwm_chip *jz,
 				   unsigned int channel)
 {
 	/* Enable all TCU channels for PWM use by default except channels 0/1 */
-	u32 pwm_channels_mask = GENMASK(NUM_PWM - 1, 2);
+	u32 pwm_channels_mask = GENMASK(jz->chip.npwm - 1, 2);
 
 	device_property_read_u32(jz->chip.dev->parent,
 				 "ingenic,pwm-channels-mask",
@@ -214,6 +218,7 @@ static int jz4740_pwm_probe(struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
 	struct jz4740_pwm_chip *jz4740;
+	const struct soc_info *info = device_get_match_data(dev);
 
 	jz4740 = devm_kzalloc(dev, sizeof(*jz4740), GFP_KERNEL);
 	if (!jz4740)

drivers/pwm/pwm-jz4740.c static int jz4740_pwm_probe(struct platform_device *pdev) · line 236

Quoted 6 lines
drivers/pwm/pwm-jz4740.c +1 -1
@@ -227,8 +232,8 @@ static int jz4740_pwm_probe(struct platform_device *pdev)
 
 	jz4740->chip.dev = dev;
 	jz4740->chip.ops = &jz4740_pwm_ops;
-	jz4740->chip.npwm = NUM_PWM;
 	jz4740->chip.base = -1;
+	jz4740->chip.npwm = info ? info->num_pwms : NUM_PWM;

Can info be actually NULL? I don't think so, so you can just use info->num_pwms here and drop the definition of NUM_PWM.

drivers/pwm/pwm-jz4740.c static int jz4740_pwm_probe(struct platform_device *pdev) · line 239

Quoted 3 lines
drivers/pwm/pwm-jz4740.c +0 -0
 	jz4740->chip.of_xlate = of_pwm_xlate_with_flags;
 	jz4740->chip.of_pwm_n_cells = 3;
 

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |
author maintainer
#5 lore raw

Re: [PATCH 3/3] pwm: jz4740: Add support for the JZ4725B

Hi Uwe,

Quoted 55 lines · Uwe Kleine-König
Quoted 54 lines · Paul Cercueil

The PWM hardware in the JZ4725B works the same as in the JZ4740,
but has
only six channels available.

---

Notes:
I did not add documentation for the new jz4725b-pwm compatible string on purpose. The reason is that the documentation file for the Timer/Counter Unit (TCU) of Ingenic SoCs will be completely rewritten from .txt to YAML in a separate patchset.

  drivers/pwm/pwm-jz4740.c | 20 +++++++++++++++++---
  1 file changed, 17 insertions(+), 3 deletions(-)

diff --git a/drivers/pwm/pwm-jz4740.c b/drivers/pwm/pwm-jz4740.c index f566f9d248d6..bb27934fb6c2 100644
--- a/drivers/pwm/pwm-jz4740.c
+++ b/drivers/pwm/pwm-jz4740.c
@@ -22,6 +22,10 @@

  #define NUM_PWM 8

+struct soc_info {
+ unsigned int num_pwms;
+};
+
struct jz4740_pwm_chip {
struct pwm_chip chip;
struct regmap *map;
@@ -36,7 +40,7 @@ static bool jz4740_pwm_can_use_chn(struct
jz4740_pwm_chip *jz,
unsigned int channel)
{
/* Enable all TCU channels for PWM use by default except channels
0/1 */
- u32 pwm_channels_mask = GENMASK(NUM_PWM - 1, 2);
+ u32 pwm_channels_mask = GENMASK(jz->chip.npwm - 1, 2);

device_property_read_u32(jz->chip.dev->parent,
"ingenic,pwm-channels-mask",
@@ -214,6 +218,7 @@ static int jz4740_pwm_probe(struct
platform_device *pdev)
{
struct device *dev = &pdev->dev;
struct jz4740_pwm_chip *jz4740;
+ const struct soc_info *info = device_get_match_data(dev);

jz4740 = devm_kzalloc(dev, sizeof(*jz4740), GFP_KERNEL); if (!jz4740)
@@ -227,8 +232,8 @@ static int jz4740_pwm_probe(struct
platform_device *pdev)

Quoted 8 lines
Quoted 5 lines

jz4740->chip.dev = dev;
jz4740->chip.ops = &jz4740_pwm_ops;
- jz4740->chip.npwm = NUM_PWM;
jz4740->chip.base = -1;
+ jz4740->chip.npwm = info ? info->num_pwms : NUM_PWM;

Can info be actually NULL? I don't think so, so you can just use info->num_pwms here and drop the definition of NUM_PWM.

In *theory* it can be NULL if the kernel is configured without CONFIG_OF, which will never happen on any board supported by this driver. I can add a dependency on CONFIG_OF in V2, then use info->num_pwms there.

cheers,
-Paul

Quoted 11 lines
Quoted 3 lines
  	jz4740->chip.of_xlate = of_pwm_xlate_with_flags;
  	jz4740->chip.of_pwm_n_cells = 3;

Best regards
Uwe

--
Pengutronix e.K. | Uwe Kleine-König

    |

Industrial Linux Solutions | https://www.pengutronix.de/ |

#6 lore raw

Re: [PATCH 3/3] pwm: jz4740: Add support for the JZ4725B

Hello Paul,

Quoted 21 lines · Paul Cercueil
Quoted 19 lines · Uwe Kleine-König
Quoted 17 lines · Paul Cercueil

@@ -214,6 +218,7 @@ static int jz4740_pwm_probe(struct
platform_device *pdev)
{
struct device *dev = &pdev->dev; struct jz4740_pwm_chip *jz4740;
+ const struct soc_info *info = device_get_match_data(dev);

jz4740 = devm_kzalloc(dev, sizeof(*jz4740), GFP_KERNEL); if (!jz4740)
@@ -227,8 +232,8 @@ static int jz4740_pwm_probe(struct
platform_device *pdev)

jz4740->chip.dev = dev;
jz4740->chip.ops = &jz4740_pwm_ops;
- jz4740->chip.npwm = NUM_PWM;
jz4740->chip.base = -1;
+ jz4740->chip.npwm = info ? info->num_pwms : NUM_PWM;

Quoted 6 lines
Quoted 2 lines

Can info be actually NULL? I don't think so, so you can just use info->num_pwms here and drop the definition of NUM_PWM.

In *theory* it can be NULL if the kernel is configured without CONFIG_OF, which will never happen on any board supported by this driver. I can add a dependency on CONFIG_OF in V2, then use info->num_pwms there.

I'd prefer just something like:

	if (!info)
		return -EINVAL;

instead of a dependency on CONFIG_OF.

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |
author maintainer
#7 lore raw
In reply to Paul Cercueil

Hi,

Any feedback on this patchset?

Cheers,
-Paul

Quoted 25 lines · Paul Cercueil

Depending on MACH_INGENIC prevent us from creating a generic kernel that works on more than one MIPS board. Instead, we just depend on MIPS being set.

---
 drivers/pwm/Kconfig | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pwm/Kconfig +1 -2
index eebbc917ac97..7814e5b2cad7 100644
@@ -234,7 +234,7 @@ config PWM_IMX_TPM
 
 config PWM_JZ4740
 	tristate "Ingenic JZ47xx PWM support"
-	depends on MACH_INGENIC
+	depends on MIPS
 	depends on COMMON_CLK
 	select MFD_SYSCON
 	help
--

2.25.1

Acked-by on v1 1/3
#8 lore raw
In reply to Paul Cercueil
Quoted 14 lines · Paul Cercueil

Depending on MACH_INGENIC prevent us from creating a generic kernel that works on more than one MIPS board. Instead, we just depend on MIPS being set.

---
 drivers/pwm/Kconfig | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pwm/Kconfig +0 -0
index eebbc917ac97..7814e5b2cad7 100644

drivers/pwm/Kconfig config PWM_IMX_TPM · line 237

Quoted 5 lines
drivers/pwm/Kconfig +1 -1
@@ -234,7 +234,7 @@ config PWM_IMX_TPM
 
 config PWM_JZ4740
 	tristate "Ingenic JZ47xx PWM support"
-	depends on MACH_INGENIC
+	depends on MIPS

Looks good to me.

Best regards
Uwe

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