Skip to contentSkip to conversation

pwm: imx: support output polarity inversion

PATCH Superseded Lothar Waßmann 4 participants 19 messages Mar 19, 2014 – Apr 10, 2014

Subsystems (2)

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

This patch series adds support for polarity inversion to the pwm-imx driver.

Changes wrt. v2:
- make the use of '#pwm-cells = <3>' optional, so that the change does not break existing DT blobs as suggested by Arnd Bergmann and Sascha Hauer.

#1 lore raw

PATCH v3 1/3 pwm: make the PWM_POLARITY flag in DTB optional

Change the pwm chip driver registration, so that a chip driver that supports polarity inversion can still be used with DTBs that don't provide the 'PWM_POLARITY' flag.

This is done to provide polarity inversion support for the pwm-imx driver without having to modify all existing DTS files.

---
 drivers/pwm/core.c |   46 ++++++++++++++++------------------------------
 1 file changed, 16 insertions(+), 30 deletions(-)
drivers/pwm/core.c +16 -30
index a804713..dc15543 100644
@@ -131,12 +131,12 @@ static int pwm_device_request(struct pwm_device *pwm, const char *label)
 	return 0;
 }
 
-struct pwm_device *
-of_pwm_xlate_with_flags(struct pwm_chip *pc, const struct of_phandle_args *args)
+struct pwm_device *of_pwm_xlate(struct pwm_chip *pc,
+				const struct of_phandle_args *args)
 {
 	struct pwm_device *pwm;
 
-	if (pc->of_pwm_n_cells < 3)
+	if (pc->of_pwm_n_cells < 2)
 		return ERR_PTR(-EINVAL);
 
 	if (args->args[0] >= pc->npwm)
@@ -148,6 +148,9 @@ of_pwm_xlate_with_flags(struct pwm_chip *pc, const struct of_phandle_args *args)
 
 	pwm_set_period(pwm, args->args[1]);
 
+	if (pc->of_pwm_n_cells < 3)
+		return pwm;
+
 	if (args->args[2] & PWM_POLARITY_INVERTED)
 		pwm_set_polarity(pwm, PWM_POLARITY_INVERSED);
 	else
@@ -155,27 +158,14 @@ of_pwm_xlate_with_flags(struct pwm_chip *pc, const struct of_phandle_args *args)
 
 	return pwm;
 }
-EXPORT_SYMBOL_GPL(of_pwm_xlate_with_flags);
 
-static struct pwm_device *
-of_pwm_simple_xlate(struct pwm_chip *pc, const struct of_phandle_args *args)
+struct pwm_device *
+of_pwm_xlate_with_flags(struct pwm_chip *pc,
+			const struct of_phandle_args *args)
 {
-	struct pwm_device *pwm;
-
-	if (pc->of_pwm_n_cells < 2)
-		return ERR_PTR(-EINVAL);
-
-	if (args->args[0] >= pc->npwm)
-		return ERR_PTR(-EINVAL);
-
-	pwm = pwm_request_from_chip(pc, args->args[0], NULL);
-	if (IS_ERR(pwm))
-		return pwm;
-
-	pwm_set_period(pwm, args->args[1]);
-
-	return pwm;
+	return of_pwm_xlate(pc, args);
 }
+EXPORT_SYMBOL_GPL(of_pwm_xlate_with_flags);
 
 static void of_pwmchip_add(struct pwm_chip *chip)
 {
@@ -183,8 +173,11 @@ static void of_pwmchip_add(struct pwm_chip *chip)
 		return;
 
 	if (!chip->of_xlate) {
-		chip->of_xlate = of_pwm_simple_xlate;
-		chip->of_pwm_n_cells = 2;
+		chip->of_xlate = of_pwm_xlate;
+		if (chip->ops->set_polarity)
+			chip->of_pwm_n_cells = 3;
+		else
+			chip->of_pwm_n_cells = 2;
 	}
 
 	of_node_get(chip->dev->of_node);
@@ -536,13 +529,6 @@ struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id)
 		goto put;
 	}
 
-	if (args.args_count != pc->of_pwm_n_cells) {
-		pr_debug("%s: wrong #pwm-cells for %s\n", np->full_name,
-			 args.np->full_name);
-		pwm = ERR_PTR(-EINVAL);
-		goto put;
-	}
-
 	pwm = pc->of_xlate(pc, &args);
 	if (IS_ERR(pwm))
 		goto put;
Signature · 1 line
1.7.10.4
#2 lore raw

PATCH v3 2/3 pwm: imx: indentation cleanup

In reply to Lothar Waßmann
---
 drivers/pwm/pwm-imx.c |   21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)
drivers/pwm/pwm-imx.c +11 -10
index cc47733..52aac5e 100644
@@ -25,20 +25,21 @@
 #define MX1_PWMS    0x04   /* PWM Sample Register */
 #define MX1_PWMP    0x08   /* PWM Period Register */
 
-#define MX1_PWMC_EN		(1 << 4)
+#define MX1_PWMC_EN			(1 << 4)
 
 /* i.MX27, i.MX31, i.MX35 share the same PWM function block: */
 
-#define MX3_PWMCR                 0x00    /* PWM Control Register */
-#define MX3_PWMSAR                0x0C    /* PWM Sample Register */
-#define MX3_PWMPR                 0x10    /* PWM Period Register */
-#define MX3_PWMCR_PRESCALER(x)    (((x - 1) & 0xFFF) << 4)
-#define MX3_PWMCR_DOZEEN                (1 << 24)
-#define MX3_PWMCR_WAITEN                (1 << 23)
+#define MX3_PWMCR			0x00	  /* PWM Control Register */
+#define MX3_PWMSAR			0x0C	  /* PWM Sample Register */
+#define MX3_PWMPR			0x10	  /* PWM Period Register */
+#define MX3_PWMCR_PRESCALER(x)		((((x) - 1) & 0xFFF) << 4)
+#define MX3_PWMCR_DOZEEN		(1 << 24)
+#define MX3_PWMCR_WAITEN		(1 << 23)
 #define MX3_PWMCR_DBGEN			(1 << 22)
-#define MX3_PWMCR_CLKSRC_IPG_HIGH (2 << 16)
-#define MX3_PWMCR_CLKSRC_IPG      (1 << 16)
-#define MX3_PWMCR_EN              (1 << 0)
+#define MX3_PWMCR_POUTC			(1 << 18)
+#define MX3_PWMCR_CLKSRC_IPG_HIGH	(2 << 16)
+#define MX3_PWMCR_CLKSRC_IPG		(1 << 16)
+#define MX3_PWMCR_EN			(1 << 0)
 
 struct imx_chip {
 	struct clk	*clk_per;
Signature · 1 line
1.7.10.4
#3 lore raw

PATCH v3 3/3 pwm: imx: support output polarity inversion

In reply to Lothar Waßmann

The i.MX pwm unit on i.MX27 and newer SoCs provides a configurable output polarity. This patch adds support to utilize this feature where available.

---
 drivers/pwm/pwm-imx.c |   25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
drivers/pwm/pwm-imx.c +25 -0
index 52aac5e..a43410a 100644
@@ -138,6 +138,8 @@ static int imx_pwm_config_v2(struct pwm_chip *chip,
 
 	if (test_bit(PWMF_ENABLED, &pwm->flags))
 		cr |= MX3_PWMCR_EN;
+	if (pwm->polarity == PWM_POLARITY_INVERSED)
+		cr |= MX3_PWMCR_POUTC;
 
 	writel(cr, imx->mmio_base + MX3_PWMCR);
 
@@ -156,6 +158,11 @@ static void imx_pwm_set_enable_v2(struct pwm_chip *chip, bool enable)
 	else
 		val &= ~MX3_PWMCR_EN;
 
+	if (chip->pwms[0].polarity == PWM_POLARITY_INVERSED)
+		val |= MX3_PWMCR_POUTC;
+	else
+		val &= ~MX3_PWMCR_POUTC;
+
 	writel(val, imx->mmio_base + MX3_PWMCR);
 }
 
@@ -199,6 +206,17 @@ static void imx_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
 	clk_disable_unprepare(imx->clk_per);
 }
 
+static int imx_pwm_set_polarity(struct pwm_chip *chip, struct pwm_device *pwm,
+				enum pwm_polarity polarity)
+{
+	struct imx_chip *imx = to_imx_chip(chip);
+
+	dev_dbg(imx->chip.dev, "%s: polarity set to %s\n", __func__,
+		polarity == PWM_POLARITY_INVERSED ? "inverted" : "normal");
+
+	return 0;
+}
+
 static struct pwm_ops imx_pwm_ops = {
 	.enable = imx_pwm_enable,
 	.disable = imx_pwm_disable,
@@ -210,6 +228,7 @@ struct imx_pwm_data {
 	int (*config)(struct pwm_chip *chip,
 		struct pwm_device *pwm, int duty_ns, int period_ns);
 	void (*set_enable)(struct pwm_chip *chip, bool enable);
+	unsigned output_polarity:1;
 };
 
 static struct imx_pwm_data imx_pwm_data_v1 = {
@@ -220,6 +239,7 @@ static struct imx_pwm_data imx_pwm_data_v1 = {
 static struct imx_pwm_data imx_pwm_data_v2 = {
 	.config = imx_pwm_config_v2,
 	.set_enable = imx_pwm_set_enable_v2,
+	.output_polarity = 1,
 };
 
 static const struct of_device_id imx_pwm_dt_ids[] = {
@@ -272,6 +292,11 @@ static int imx_pwm_probe(struct platform_device *pdev)
 		return PTR_ERR(imx->mmio_base);
 
 	data = of_id->data;
+	if (data->output_polarity) {
+		dev_dbg(&pdev->dev, "PWM supports output inversion\n");
+		imx_pwm_ops.set_polarity = imx_pwm_set_polarity;
+	}
+
 	imx->config = data->config;
 	imx->set_enable = data->set_enable;
 
Signature · 1 line
1.7.10.4
#4 lore raw

v3 cover pwm: imx: support output polarity inversion

In reply to Lothar Waßmann

This patch series adds support for polarity inversion to the pwm-imx driver.

Changes wrt. v2:
- make the use of '#pwm-cells = <3>' optional, so that the change does not break existing DT blobs as suggested by Arnd Bergmann and Sascha Hauer.

#5 lore raw

PATCH v3 1/3 pwm: make the PWM_POLARITY flag in DTB optional

Change the pwm chip driver registration, so that a chip driver that supports polarity inversion can still be used with DTBs that don't provide the 'PWM_POLARITY' flag.

This is done to provide polarity inversion support for the pwm-imx driver without having to modify all existing DTS files.

---
 drivers/pwm/core.c |   46 ++++++++++++++++------------------------------
 1 file changed, 16 insertions(+), 30 deletions(-)
drivers/pwm/core.c +16 -30
index a804713..dc15543 100644
@@ -131,12 +131,12 @@ static int pwm_device_request(struct pwm_device *pwm, const char *label)
 	return 0;
 }
 
-struct pwm_device *
-of_pwm_xlate_with_flags(struct pwm_chip *pc, const struct of_phandle_args *args)
+struct pwm_device *of_pwm_xlate(struct pwm_chip *pc,
+				const struct of_phandle_args *args)
 {
 	struct pwm_device *pwm;
 
-	if (pc->of_pwm_n_cells < 3)
+	if (pc->of_pwm_n_cells < 2)
 		return ERR_PTR(-EINVAL);
 
 	if (args->args[0] >= pc->npwm)
@@ -148,6 +148,9 @@ of_pwm_xlate_with_flags(struct pwm_chip *pc, const struct of_phandle_args *args)
 
 	pwm_set_period(pwm, args->args[1]);
 
+	if (pc->of_pwm_n_cells < 3)
+		return pwm;
+
 	if (args->args[2] & PWM_POLARITY_INVERTED)
 		pwm_set_polarity(pwm, PWM_POLARITY_INVERSED);
 	else
@@ -155,27 +158,14 @@ of_pwm_xlate_with_flags(struct pwm_chip *pc, const struct of_phandle_args *args)
 
 	return pwm;
 }
-EXPORT_SYMBOL_GPL(of_pwm_xlate_with_flags);
 
-static struct pwm_device *
-of_pwm_simple_xlate(struct pwm_chip *pc, const struct of_phandle_args *args)
+struct pwm_device *
+of_pwm_xlate_with_flags(struct pwm_chip *pc,
+			const struct of_phandle_args *args)
 {
-	struct pwm_device *pwm;
-
-	if (pc->of_pwm_n_cells < 2)
-		return ERR_PTR(-EINVAL);
-
-	if (args->args[0] >= pc->npwm)
-		return ERR_PTR(-EINVAL);
-
-	pwm = pwm_request_from_chip(pc, args->args[0], NULL);
-	if (IS_ERR(pwm))
-		return pwm;
-
-	pwm_set_period(pwm, args->args[1]);
-
-	return pwm;
+	return of_pwm_xlate(pc, args);
 }
+EXPORT_SYMBOL_GPL(of_pwm_xlate_with_flags);
 
 static void of_pwmchip_add(struct pwm_chip *chip)
 {
@@ -183,8 +173,11 @@ static void of_pwmchip_add(struct pwm_chip *chip)
 		return;
 
 	if (!chip->of_xlate) {
-		chip->of_xlate = of_pwm_simple_xlate;
-		chip->of_pwm_n_cells = 2;
+		chip->of_xlate = of_pwm_xlate;
+		if (chip->ops->set_polarity)
+			chip->of_pwm_n_cells = 3;
+		else
+			chip->of_pwm_n_cells = 2;
 	}
 
 	of_node_get(chip->dev->of_node);
@@ -536,13 +529,6 @@ struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id)
 		goto put;
 	}
 
-	if (args.args_count != pc->of_pwm_n_cells) {
-		pr_debug("%s: wrong #pwm-cells for %s\n", np->full_name,
-			 args.np->full_name);
-		pwm = ERR_PTR(-EINVAL);
-		goto put;
-	}
-
 	pwm = pc->of_xlate(pc, &args);
 	if (IS_ERR(pwm))
 		goto put;
Signature · 1 line
1.7.10.4
maintainer
#6 lore raw

Re: [PATCHv3 1/3] pwm: make the PWM_POLARITY flag in DTB optional

Quoted 75 lines · Lothar Waßmann

Change the pwm chip driver registration, so that a chip driver that supports polarity inversion can still be used with DTBs that don't provide the 'PWM_POLARITY' flag.

This is done to provide polarity inversion support for the pwm-imx driver without having to modify all existing DTS files.

---
 drivers/pwm/core.c |   46 ++++++++++++++++------------------------------
 1 file changed, 16 insertions(+), 30 deletions(-)
drivers/pwm/core.c +11 -21
index a804713..dc15543 100644
@@ -131,12 +131,12 @@ static int pwm_device_request(struct pwm_device *pwm, const char *label)
 	return 0;
 }
 
-struct pwm_device *
-of_pwm_xlate_with_flags(struct pwm_chip *pc, const struct of_phandle_args *args)
+struct pwm_device *of_pwm_xlate(struct pwm_chip *pc,
+				const struct of_phandle_args *args)
 {
 	struct pwm_device *pwm;
 
-	if (pc->of_pwm_n_cells < 3)
+	if (pc->of_pwm_n_cells < 2)
 		return ERR_PTR(-EINVAL);
 
 	if (args->args[0] >= pc->npwm)
@@ -148,6 +148,9 @@ of_pwm_xlate_with_flags(struct pwm_chip *pc, const struct of_phandle_args *args)
 
 	pwm_set_period(pwm, args->args[1]);
 
+	if (pc->of_pwm_n_cells < 3)
+		return pwm;
+
 	if (args->args[2] & PWM_POLARITY_INVERTED)
 		pwm_set_polarity(pwm, PWM_POLARITY_INVERSED);
 	else
@@ -155,27 +158,14 @@ of_pwm_xlate_with_flags(struct pwm_chip *pc, const struct of_phandle_args *args)
 
 	return pwm;
 }
-EXPORT_SYMBOL_GPL(of_pwm_xlate_with_flags);
 
-static struct pwm_device *
-of_pwm_simple_xlate(struct pwm_chip *pc, const struct of_phandle_args *args)
+struct pwm_device *
+of_pwm_xlate_with_flags(struct pwm_chip *pc,
+			const struct of_phandle_args *args)
 {
-	struct pwm_device *pwm;
-
-	if (pc->of_pwm_n_cells < 2)
-		return ERR_PTR(-EINVAL);
-
-	if (args->args[0] >= pc->npwm)
-		return ERR_PTR(-EINVAL);
-
-	pwm = pwm_request_from_chip(pc, args->args[0], NULL);
-	if (IS_ERR(pwm))
-		return pwm;
-
-	pwm_set_period(pwm, args->args[1]);
-
-	return pwm;
+	return of_pwm_xlate(pc, args);
 }
+EXPORT_SYMBOL_GPL(of_pwm_xlate_with_flags);
 
 static void of_pwmchip_add(struct pwm_chip *chip)
 {

drivers/pwm/core.c static void of_pwmchip_add(struct pwm_chip *chip) · line 180

Quoted 10 lines
drivers/pwm/core.c +5 -2
@@ -183,8 +173,11 @@ static void of_pwmchip_add(struct pwm_chip *chip)
 		return;
 
 	if (!chip->of_xlate) {
-		chip->of_xlate = of_pwm_simple_xlate;
-		chip->of_pwm_n_cells = 2;
+		chip->of_xlate = of_pwm_xlate;
+		if (chip->ops->set_polarity)
+			chip->of_pwm_n_cells = 3;
+		else
+			chip->of_pwm_n_cells = 2;

I think the presence of the set_polarity callback shouldn't influence the number of cells the parser expects. As commented on 2/2 this doesn't actually mean the device actually support polarity inversion. Also, polarity inversion could still be done in software for hardware that doesn't support it.

Sascha

Signature · 4 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#7 lore raw

Re: [PATCHv3 1/3] pwm: make the PWM_POLARITY flag in DTB optional

Quoted 1 line · Sascha Hauer

On Fri, Mar 28, 2014 at 09:48:58AM +0100, Lothar Waßmann wrote:

[...]

Quoted 15 lines

drivers/pwm/core.c static void of_pwmchip_add(struct pwm_chip *chip) · line 180

Quoted 11 lines
drivers/pwm/core.c +5 -2
@@ -183,8 +173,11 @@ static void of_pwmchip_add(struct pwm_chip *chip)
 		return;
 
 	if (!chip->of_xlate) {
-		chip->of_xlate = of_pwm_simple_xlate;
-		chip->of_pwm_n_cells = 2;
+		chip->of_xlate = of_pwm_xlate;
+		if (chip->ops->set_polarity)
+			chip->of_pwm_n_cells = 3;
+		else
+			chip->of_pwm_n_cells = 2;

I think the presence of the set_polarity callback shouldn't influence the number of cells the parser expects. As commented on 2/2 this doesn't actually mean the device actually support polarity inversion.

How so? A driver should only implement .set_polarity() if it supports changing the polarity.

That said, I agree that the presence of .set_polarity() shouldn't determine the number of cells. You could have any number of other flags set via the third cell.

Quoted 2 lines

Also, polarity inversion could still be done in software for hardware that doesn't support it.

No. You cannot emulate polarity inversion in software.

Thierry

#8 lore raw

Re: [PATCHv3 1/3] pwm: make the PWM_POLARITY flag in DTB optional

Hi,

Quoted 22 lines · Thierry Reding
Quoted 1 line · Sascha Hauer

On Fri, Mar 28, 2014 at 09:48:58AM +0100, Lothar Waßmann wrote:

[...]

Quoted 15 lines

drivers/pwm/core.c static void of_pwmchip_add(struct pwm_chip *chip) · line 180

Quoted 11 lines
drivers/pwm/core.c +5 -2
@@ -183,8 +173,11 @@ static void of_pwmchip_add(struct pwm_chip *chip)
 		return;
 
 	if (!chip->of_xlate) {
-		chip->of_xlate = of_pwm_simple_xlate;
-		chip->of_pwm_n_cells = 2;
+		chip->of_xlate = of_pwm_xlate;
+		if (chip->ops->set_polarity)
+			chip->of_pwm_n_cells = 3;
+		else
+			chip->of_pwm_n_cells = 2;

I think the presence of the set_polarity callback shouldn't influence the number of cells the parser expects. As commented on 2/2 this doesn't actually mean the device actually support polarity inversion.

How so? A driver should only implement .set_polarity() if it supports changing the polarity.

Quoted 9 lines

That said, I agree that the presence of .set_polarity() shouldn't determine the number of cells. You could have any number of other flags set via the third cell.

Quoted 2 lines

Also, polarity inversion could still be done in software for hardware that doesn't support it.

No. You cannot emulate polarity inversion in software.

Why not?

duty_ns = period_ns - duty_ns;

Lothar Waßmann

Signature · 9 lines
___________________________________________________________

Ka-Ro electronics GmbH | Pascalstraße 22 | D - 52076 Aachen
Phone: +49 2408 1402-0 | Fax: +49 2408 1402-10
Geschäftsführer: Matthias Kaussen
Handelsregistereintrag: Amtsgericht Aachen, HRB 4996

www.karo-electronics.de | [email protected]
___________________________________________________________
#9 lore raw

Re: [PATCHv3 1/3] pwm: make the PWM_POLARITY flag in DTB optional

Quoted 10 lines · Lothar Waßmann
Quoted 2 lines · Thierry Reding

No. You cannot emulate polarity inversion in software.

Why not?

duty_ns = period_ns - duty_ns;

Since I made the same mistake, I will pass along the pointer Thierry gave me.

In include/linux/pwm.h the second difference for an inverted signal is described.

/**
* enum pwm_polarity - polarity of a PWM signal
* @PWM_POLARITY_NORMAL: a high signal for the duration of the duty-
* cycle, followed by a low signal for the remainder of the pulse
* period
* @PWM_POLARITY_INVERSED: a low signal for the duration of the duty-
* cycle, followed by a high signal for the remainder of the pulse
* period
*/
enum pwm_polarity {
PWM_POLARITY_NORMAL,
PWM_POLARITY_INVERSED,
};

Of course, I suspect not all PWM hardware respects this definition of inverted output.

Either way, hacking the duty in software certainly would get the high/low order wrong.

-Tim

#10 lore raw

Re: [PATCHv3 1/3] pwm: make the PWM_POLARITY flag in DTB optional

Hi,

Quoted 32 lines · Tim Kryger
Quoted 10 lines · Lothar Waßmann
Quoted 2 lines · Thierry Reding

No. You cannot emulate polarity inversion in software.

Why not?

duty_ns = period_ns - duty_ns;

Since I made the same mistake, I will pass along the pointer Thierry gave me.

In include/linux/pwm.h the second difference for an inverted signal is described.

/**
* enum pwm_polarity - polarity of a PWM signal
* @PWM_POLARITY_NORMAL: a high signal for the duration of the duty-
* cycle, followed by a low signal for the remainder of the pulse
* period
* @PWM_POLARITY_INVERSED: a low signal for the duration of the duty-
* cycle, followed by a high signal for the remainder of the pulse
* period
*/
enum pwm_polarity {
PWM_POLARITY_NORMAL,
PWM_POLARITY_INVERSED,
};

Quoted 6 lines

Of course, I suspect not all PWM hardware respects this definition of inverted output.

Either way, hacking the duty in software certainly would get the high/low order wrong.

OK. But for a periodic signal this doesn't make any difference. It's just a matter of where you set your reference point.
Only if you program the PWM to create a single cycle you would see the difference. I wonder if this is a real life usecase though.

Lothar Waßmann

Signature · 9 lines
___________________________________________________________

Ka-Ro electronics GmbH | Pascalstraße 22 | D - 52076 Aachen
Phone: +49 2408 1402-0 | Fax: +49 2408 1402-10
Geschäftsführer: Matthias Kaussen
Handelsregistereintrag: Amtsgericht Aachen, HRB 4996

www.karo-electronics.de | [email protected]
___________________________________________________________
#11 lore raw

Re: [PATCHv3 1/3] pwm: make the PWM_POLARITY flag in DTB optional

Quoted 38 lines · Lothar Waßmann

Hi,

Quoted 35 lines · Tim Kryger
Quoted 10 lines · Lothar Waßmann
Quoted 2 lines · Thierry Reding

No. You cannot emulate polarity inversion in software.

Why not?

duty_ns = period_ns - duty_ns;

Since I made the same mistake, I will pass along the pointer Thierry gave me.

In include/linux/pwm.h the second difference for an inverted signal is described.

/**
* enum pwm_polarity - polarity of a PWM signal
* @PWM_POLARITY_NORMAL: a high signal for the duration of the duty-
* cycle, followed by a low signal for the remainder of the pulse
* period
* @PWM_POLARITY_INVERSED: a low signal for the duration of the duty-
* cycle, followed by a high signal for the remainder of the pulse
* period
*/
enum pwm_polarity {
PWM_POLARITY_NORMAL,
PWM_POLARITY_INVERSED,
};

Of course, I suspect not all PWM hardware respects this definition of inverted output.

Quoted 7 lines
Quoted 3 lines

Either way, hacking the duty in software certainly would get the high/low order wrong.

OK. But for a periodic signal this doesn't make any difference. It's just a matter of where you set your reference point.
Only if you program the PWM to create a single cycle you would see the difference. I wonder if this is a real life usecase though.

It doesn't make a difference if all you're concerned about is the signal power (which happens to be the case for LED and backlight use-cases).

Currently any in-kernel users only care about the signal power, but there's no guarantee that it will stay that way. Furthermore, PWM channels are exposed to userspace via sysfs, so code that we don't actually see may rely on this behaviour.

Thierry

#12 lore raw

Re: [PATCHv3 1/3] pwm: make the PWM_POLARITY flag in DTB optional

In reply to Lothar Waßmann
Quoted 7 lines · Lothar Waßmann
Quoted 2 lines · Thierry Reding

No. You cannot emulate polarity inversion in software.

Why not?

duty_ns = period_ns - duty_ns;

Since I made the same mistake, I will pass along the pointer Thierry gave me.

In include/linux/pwm.h the second difference for an inverted signal is described.

/**
* enum pwm_polarity - polarity of a PWM signal
* @PWM_POLARITY_NORMAL: a high signal for the duration of the duty-
* cycle, followed by a low signal for the remainder of the pulse
* period
* @PWM_POLARITY_INVERSED: a low signal for the duration of the duty-
* cycle, followed by a high signal for the remainder of the pulse
* period
*/
enum pwm_polarity {
PWM_POLARITY_NORMAL,
PWM_POLARITY_INVERSED,
};

Of course, I suspect not all PWM hardware respects this definition of inverted output.

Either way, hacking the duty in software certainly would get the high/low order wrong.

-Tim

maintainer
#13 lore raw

Re: [PATCHv3 1/3] pwm: make the PWM_POLARITY flag in DTB optional

Quoted 28 lines · Tim Kryger
Quoted 7 lines · Lothar Waßmann
Quoted 2 lines · Thierry Reding

No. You cannot emulate polarity inversion in software.

Why not?

duty_ns = period_ns - duty_ns;

Since I made the same mistake, I will pass along the pointer Thierry gave me.

In include/linux/pwm.h the second difference for an inverted signal is described.

/**
* enum pwm_polarity - polarity of a PWM signal
* @PWM_POLARITY_NORMAL: a high signal for the duration of the duty-
* cycle, followed by a low signal for the remainder of the pulse
* period
* @PWM_POLARITY_INVERSED: a low signal for the duration of the duty-
* cycle, followed by a high signal for the remainder of the pulse
* period
*/
enum pwm_polarity {
PWM_POLARITY_NORMAL,
PWM_POLARITY_INVERSED,
};

Quoted 5 lines

Of course, I suspect not all PWM hardware respects this definition of inverted output.

Either way, hacking the duty in software certainly would get the high/low order wrong.

This only relevant if you have some reference signal the PWM must be relative to, for example if you combine multiple PWMs for motor control. For PWMs used for backlight or beepers a signal inversion in software is perfectly fine. And I also think that it makes sense to put it once into the framework instead of bothering all consumer drivers with the inversion.

Sascha

Signature · 4 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#14 lore raw

Re: [PATCHv3 1/3] pwm: make the PWM_POLARITY flag in DTB optional

Quoted 35 lines · Sascha Hauer
Quoted 28 lines · Tim Kryger
Quoted 7 lines · Lothar Waßmann
Quoted 2 lines · Thierry Reding

No. You cannot emulate polarity inversion in software.

Why not?

duty_ns = period_ns - duty_ns;

Since I made the same mistake, I will pass along the pointer Thierry gave me.

In include/linux/pwm.h the second difference for an inverted signal is described.

/**
* enum pwm_polarity - polarity of a PWM signal
* @PWM_POLARITY_NORMAL: a high signal for the duration of the duty-
* cycle, followed by a low signal for the remainder of the pulse
* period
* @PWM_POLARITY_INVERSED: a low signal for the duration of the duty-
* cycle, followed by a high signal for the remainder of the pulse
* period
*/
enum pwm_polarity {
PWM_POLARITY_NORMAL,
PWM_POLARITY_INVERSED,
};

Quoted 5 lines

Of course, I suspect not all PWM hardware respects this definition of inverted output.

Either way, hacking the duty in software certainly would get the high/low order wrong.

Quoted 6 lines

This only relevant if you have some reference signal the PWM must be relative to, for example if you combine multiple PWMs for motor control. For PWMs used for backlight or beepers a signal inversion in software is perfectly fine. And I also think that it makes sense to put it once into the framework instead of bothering all consumer drivers with the inversion.

The PWM framework itself doesn't have enough knowledge about what a PWM is being used for. Therefore it cannot determine whether emulating polarity inversion by reversing the duty cycle will be appropriate.

Putting such functionality into the core will prevent PWM channels from being used for cases where the signal polarity does matter.

Thierry

maintainer
#15 lore raw

Re: [PATCHv3 1/3] pwm: make the PWM_POLARITY flag in DTB optional

Quoted 43 lines · Thierry Reding
Quoted 35 lines · Sascha Hauer
Quoted 28 lines · Tim Kryger
Quoted 7 lines · Lothar Waßmann
Quoted 2 lines · Thierry Reding

No. You cannot emulate polarity inversion in software.

Why not?

duty_ns = period_ns - duty_ns;

Since I made the same mistake, I will pass along the pointer Thierry gave me.

In include/linux/pwm.h the second difference for an inverted signal is described.

/**
* enum pwm_polarity - polarity of a PWM signal
* @PWM_POLARITY_NORMAL: a high signal for the duration of the duty-
* cycle, followed by a low signal for the remainder of the pulse
* period
* @PWM_POLARITY_INVERSED: a low signal for the duration of the duty-
* cycle, followed by a high signal for the remainder of the pulse
* period
*/
enum pwm_polarity {
PWM_POLARITY_NORMAL,
PWM_POLARITY_INVERSED,
};

Quoted 5 lines

Of course, I suspect not all PWM hardware respects this definition of inverted output.

Either way, hacking the duty in software certainly would get the high/low order wrong.

Quoted 6 lines

This only relevant if you have some reference signal the PWM must be relative to, for example if you combine multiple PWMs for motor control. For PWMs used for backlight or beepers a signal inversion in software is perfectly fine. And I also think that it makes sense to put it once into the framework instead of bothering all consumer drivers with the inversion.

Quoted 6 lines

The PWM framework itself doesn't have enough knowledge about what a PWM is being used for. Therefore it cannot determine whether emulating polarity inversion by reversing the duty cycle will be appropriate.

Putting such functionality into the core will prevent PWM channels from being used for cases where the signal polarity does matter

The PWM core is in no way prepared for handling such situations. Should we want to add support it a PWM_POLARITY_INVERSED flag would be the least of our problems. It could be renamed to PWM_POLARITY_INVERSED_ASYNC for the beeper/led drivers which do not need synchronization.

Sascha

Signature · 4 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#16 lore raw

PATCH v3 2/3 pwm: imx: indentation cleanup

In reply to Lothar Waßmann
---
 drivers/pwm/pwm-imx.c |   21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)
drivers/pwm/pwm-imx.c +11 -10
index cc47733..52aac5e 100644
@@ -25,20 +25,21 @@
 #define MX1_PWMS    0x04   /* PWM Sample Register */
 #define MX1_PWMP    0x08   /* PWM Period Register */
 
-#define MX1_PWMC_EN		(1 << 4)
+#define MX1_PWMC_EN			(1 << 4)
 
 /* i.MX27, i.MX31, i.MX35 share the same PWM function block: */
 
-#define MX3_PWMCR                 0x00    /* PWM Control Register */
-#define MX3_PWMSAR                0x0C    /* PWM Sample Register */
-#define MX3_PWMPR                 0x10    /* PWM Period Register */
-#define MX3_PWMCR_PRESCALER(x)    (((x - 1) & 0xFFF) << 4)
-#define MX3_PWMCR_DOZEEN                (1 << 24)
-#define MX3_PWMCR_WAITEN                (1 << 23)
+#define MX3_PWMCR			0x00	  /* PWM Control Register */
+#define MX3_PWMSAR			0x0C	  /* PWM Sample Register */
+#define MX3_PWMPR			0x10	  /* PWM Period Register */
+#define MX3_PWMCR_PRESCALER(x)		((((x) - 1) & 0xFFF) << 4)
+#define MX3_PWMCR_DOZEEN		(1 << 24)
+#define MX3_PWMCR_WAITEN		(1 << 23)
 #define MX3_PWMCR_DBGEN			(1 << 22)
-#define MX3_PWMCR_CLKSRC_IPG_HIGH (2 << 16)
-#define MX3_PWMCR_CLKSRC_IPG      (1 << 16)
-#define MX3_PWMCR_EN              (1 << 0)
+#define MX3_PWMCR_POUTC			(1 << 18)
+#define MX3_PWMCR_CLKSRC_IPG_HIGH	(2 << 16)
+#define MX3_PWMCR_CLKSRC_IPG		(1 << 16)
+#define MX3_PWMCR_EN			(1 << 0)
 
 struct imx_chip {
 	struct clk	*clk_per;
Signature · 1 line
1.7.10.4
#17 lore raw

PATCH v3 3/3 pwm: imx: support output polarity inversion

In reply to Lothar Waßmann

The i.MX pwm unit on i.MX27 and newer SoCs provides a configurable output polarity. This patch adds support to utilize this feature where available.

---
 drivers/pwm/pwm-imx.c |   25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
drivers/pwm/pwm-imx.c +25 -0
index 52aac5e..a43410a 100644
@@ -138,6 +138,8 @@ static int imx_pwm_config_v2(struct pwm_chip *chip,
 
 	if (test_bit(PWMF_ENABLED, &pwm->flags))
 		cr |= MX3_PWMCR_EN;
+	if (pwm->polarity == PWM_POLARITY_INVERSED)
+		cr |= MX3_PWMCR_POUTC;
 
 	writel(cr, imx->mmio_base + MX3_PWMCR);
 
@@ -156,6 +158,11 @@ static void imx_pwm_set_enable_v2(struct pwm_chip *chip, bool enable)
 	else
 		val &= ~MX3_PWMCR_EN;
 
+	if (chip->pwms[0].polarity == PWM_POLARITY_INVERSED)
+		val |= MX3_PWMCR_POUTC;
+	else
+		val &= ~MX3_PWMCR_POUTC;
+
 	writel(val, imx->mmio_base + MX3_PWMCR);
 }
 
@@ -199,6 +206,17 @@ static void imx_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
 	clk_disable_unprepare(imx->clk_per);
 }
 
+static int imx_pwm_set_polarity(struct pwm_chip *chip, struct pwm_device *pwm,
+				enum pwm_polarity polarity)
+{
+	struct imx_chip *imx = to_imx_chip(chip);
+
+	dev_dbg(imx->chip.dev, "%s: polarity set to %s\n", __func__,
+		polarity == PWM_POLARITY_INVERSED ? "inverted" : "normal");
+
+	return 0;
+}
+
 static struct pwm_ops imx_pwm_ops = {
 	.enable = imx_pwm_enable,
 	.disable = imx_pwm_disable,
@@ -210,6 +228,7 @@ struct imx_pwm_data {
 	int (*config)(struct pwm_chip *chip,
 		struct pwm_device *pwm, int duty_ns, int period_ns);
 	void (*set_enable)(struct pwm_chip *chip, bool enable);
+	unsigned output_polarity:1;
 };
 
 static struct imx_pwm_data imx_pwm_data_v1 = {
@@ -220,6 +239,7 @@ static struct imx_pwm_data imx_pwm_data_v1 = {
 static struct imx_pwm_data imx_pwm_data_v2 = {
 	.config = imx_pwm_config_v2,
 	.set_enable = imx_pwm_set_enable_v2,
+	.output_polarity = 1,
 };
 
 static const struct of_device_id imx_pwm_dt_ids[] = {
@@ -272,6 +292,11 @@ static int imx_pwm_probe(struct platform_device *pdev)
 		return PTR_ERR(imx->mmio_base);
 
 	data = of_id->data;
+	if (data->output_polarity) {
+		dev_dbg(&pdev->dev, "PWM supports output inversion\n");
+		imx_pwm_ops.set_polarity = imx_pwm_set_polarity;
+	}
+
 	imx->config = data->config;
 	imx->set_enable = data->set_enable;
 
Signature · 1 line
1.7.10.4
maintainer
#18 lore raw

Hi Lothar,

Quoted 20 lines · Lothar Waßmann

static struct pwm_ops imx_pwm_ops = {

drivers/pwm/pwm-imx.c +2 -0
 	.enable = imx_pwm_enable,
 	.disable = imx_pwm_disable,
@@ -210,6 +228,7 @@ struct imx_pwm_data {
 	int (*config)(struct pwm_chip *chip,
 		struct pwm_device *pwm, int duty_ns, int period_ns);
 	void (*set_enable)(struct pwm_chip *chip, bool enable);
+	unsigned output_polarity:1;
 };
 
 static struct imx_pwm_data imx_pwm_data_v1 = {
@@ -220,6 +239,7 @@ static struct imx_pwm_data imx_pwm_data_v1 = {
 static struct imx_pwm_data imx_pwm_data_v2 = {
 	.config = imx_pwm_config_v2,
 	.set_enable = imx_pwm_set_enable_v2,
+	.output_polarity = 1,
 };
 
 static const struct of_device_id imx_pwm_dt_ids[] = {

drivers/pwm/pwm-imx.c static int imx_pwm_probe(struct platform_device *pdev) · line 298

Quoted 7 lines
drivers/pwm/pwm-imx.c +4 -0
@@ -272,6 +292,11 @@ static int imx_pwm_probe(struct platform_device *pdev)
 		return PTR_ERR(imx->mmio_base);
 
 	data = of_id->data;
+	if (data->output_polarity) {
+		dev_dbg(&pdev->dev, "PWM supports output inversion\n");
+		imx_pwm_ops.set_polarity = imx_pwm_set_polarity;
+	}

You shouldn't modify imx_pwm_ops in the probe function, it's not good style and prevents us from making the ops const.

Sascha

Signature · 4 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |