Skip to contentSkip to conversation

pwm: tegra: improve PWM module handling

PATCH New Svyatoslav Ryhel 1 participant 5 messages Feb 9

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

cover pwm: tegra: improve PWM module handling

Align driver closer to the Linux kernel to better handle various PWM use cases.

Ion Agorria (1):
pwm: tegra: properly calculate pulse and frequency divider fields

Jonas Schwöbel (3):
pwm: tegra: add probe function
pwm: tegra: add set_invert PWM operation
pwm: tegra: fix pulse_width calculation

 drivers/pwm/tegra_pwm.c | 85 +++++++++++++++++++++++++++++++++++++----
 1 file changed, 78 insertions(+), 7 deletions(-)
Signature · 1 line
2.51.0
#1 lore raw

PATCH 1/4 pwm: tegra: add probe function

From: Jonas Schwöbel <[email protected]>

When PWM config was updated the clock was restarted which caused loss of previous configuration of other channels. Further this fixes a bug/hang that can happen when set_enable was called before set_config.

---
 drivers/pwm/tegra_pwm.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/pwm/tegra_pwm.c +10 -3
index e3f1417f2ad..7105d6db90d 100644
@@ -19,7 +19,6 @@ static int tegra_pwm_set_config(struct udevice *dev, uint channel,
 {
 	struct tegra_pwm_priv *priv = dev_get_priv(dev);
 	struct pwm_ctlr *regs = priv->regs;
-	const u32 pwm_max_freq = dev_get_driver_data(dev);
 	uint pulse_width;
 	u32 reg;
 
@@ -27,8 +26,6 @@ static int tegra_pwm_set_config(struct udevice *dev, uint channel,
 		return -EINVAL;
 	debug("%s: Configure '%s' channel %u\n", __func__, dev->name, channel);
 
-	clock_start_periph_pll(PERIPH_ID_PWM, CLOCK_ID_PERIPH, pwm_max_freq);
-
 	pulse_width = duty_ns * 255 / period_ns;
 
 	reg = pulse_width << PWM_WIDTH_SHIFT;
@@ -63,6 +60,15 @@ static int tegra_pwm_of_to_plat(struct udevice *dev)
 	return 0;
 }
 
+static int tegra_pwm_probe(struct udevice *dev)
+{
+	const u32 pwm_max_freq = dev_get_driver_data(dev);
+
+	clock_start_periph_pll(PERIPH_ID_PWM, CLOCK_ID_PERIPH, pwm_max_freq);
+
+	return 0;
+}
+
 static const struct pwm_ops tegra_pwm_ops = {
 	.set_config	= tegra_pwm_set_config,
 	.set_enable	= tegra_pwm_set_enable,
@@ -80,5 +86,6 @@ U_BOOT_DRIVER(tegra_pwm) = {
 	.of_match = tegra_pwm_ids,
 	.ops	= &tegra_pwm_ops,
 	.of_to_plat	= tegra_pwm_of_to_plat,
+	.probe		= tegra_pwm_probe,
 	.priv_auto	= sizeof(struct tegra_pwm_priv),
 };
Signature · 1 line
2.51.0
#2 lore raw

PATCH 2/4 pwm: tegra: add set_invert PWM operation

In reply to Svyatoslav Ryhel

From: Jonas Schwöbel <[email protected]>

Add active-low support to the PWM controller, useful for active-low pwm-leds.

---
 drivers/pwm/tegra_pwm.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
drivers/pwm/tegra_pwm.c +17 -0
index 7105d6db90d..3d398490408 100644
@@ -12,8 +12,21 @@
 
 struct tegra_pwm_priv {
 	struct pwm_ctlr *regs;
+	u8 polarity;
 };
 
+static int tegra_pwm_set_invert(struct udevice *dev, uint channel, bool polarity)
+{
+	struct tegra_pwm_priv *priv = dev_get_priv(dev);
+
+	if (channel >= 4)
+		return -EINVAL;
+
+	clrsetbits_8(&priv->polarity, BIT(channel), (polarity << channel));
+
+	return 0;
+}
+
 static int tegra_pwm_set_config(struct udevice *dev, uint channel,
 				uint period_ns, uint duty_ns)
 {
@@ -28,6 +41,9 @@ static int tegra_pwm_set_config(struct udevice *dev, uint channel,
 
 	pulse_width = duty_ns * 255 / period_ns;
 
+	if (priv->polarity & BIT(channel))
+		pulse_width = 256 - pulse_width;
+
 	reg = pulse_width << PWM_WIDTH_SHIFT;
 	reg |= 1 << PWM_DIVIDER_SHIFT;
 	reg |= PWM_ENABLE_MASK;
@@ -72,6 +88,7 @@ static int tegra_pwm_probe(struct udevice *dev)
 static const struct pwm_ops tegra_pwm_ops = {
 	.set_config	= tegra_pwm_set_config,
 	.set_enable	= tegra_pwm_set_enable,
+	.set_invert	= tegra_pwm_set_invert,
 };
 
 static const struct udevice_id tegra_pwm_ids[] = {
Signature · 1 line
2.51.0
#3 lore raw

PATCH 3/4 pwm: tegra: fix pulse_width calculation

In reply to Svyatoslav Ryhel

From: Jonas Schwöbel <[email protected]>

The pulse_width is expressed as N/256. A 100% duty cycle is only possible when multiplied by 256 instead of 255.

---
 drivers/pwm/tegra_pwm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pwm/tegra_pwm.c +1 -1
index 3d398490408..6ddaf626dc4 100644
@@ -39,7 +39,7 @@ static int tegra_pwm_set_config(struct udevice *dev, uint channel,
 		return -EINVAL;
 	debug("%s: Configure '%s' channel %u\n", __func__, dev->name, channel);
 
-	pulse_width = duty_ns * 255 / period_ns;
+	pulse_width = duty_ns * 256 / period_ns;
 
 	if (priv->polarity & BIT(channel))
 		pulse_width = 256 - pulse_width;
Signature · 1 line
2.51.0
#4 lore raw

PATCH 4/4 pwm: tegra: properly calculate pulse and frequency divider fields

In reply to Svyatoslav Ryhel

From: Ion Agorria <[email protected]>

The pulse width field requires better precision during calculation. Add a proper frequency divider calculation based on the PWM clock instead of hardcoding it to 1.

---
 drivers/pwm/tegra_pwm.c | 61 ++++++++++++++++++++++++++++++++++++-----
 1 file changed, 54 insertions(+), 7 deletions(-)
drivers/pwm/tegra_pwm.c +54 -7
index 6ddaf626dc4..2601ed6c5a5 100644
@@ -4,14 +4,22 @@
  */
 
 #include <dm.h>
+#include <div64.h>
 #include <log.h>
 #include <pwm.h>
 #include <asm/io.h>
 #include <asm/arch/clock.h>
 #include <asm/arch/pwm.h>
+#include <linux/time.h>
+
+#define PWM_PDIV_WIDTH		8
+#define PWM_PDIV_MAX		BIT(PWM_PDIV_WIDTH)
+#define PWM_FDIV_WIDTH		13
 
 struct tegra_pwm_priv {
 	struct pwm_ctlr *regs;
+	u64 clk_rate;
+	u32 min_period_ns;
 	u8 polarity;
 };
 
@@ -32,23 +40,56 @@ static int tegra_pwm_set_config(struct udevice *dev, uint channel,
 {
 	struct tegra_pwm_priv *priv = dev_get_priv(dev);
 	struct pwm_ctlr *regs = priv->regs;
-	uint pulse_width;
-	u32 reg;
+	u64 pulse_width;
+	u32 rate, reg;
 
 	if (channel >= 4)
 		return -EINVAL;
 	debug("%s: Configure '%s' channel %u\n", __func__, dev->name, channel);
 
-	pulse_width = duty_ns * 256 / period_ns;
+	if (period_ns < priv->min_period_ns) {
+		debug("%s: Channel %u period too low, period_ns %u minimum %u\n",
+		      __func__, channel, period_ns, priv->min_period_ns);
+		return -EINVAL;
+	}
+
+	/*
+	 * Convert from duty_ns / period_ns to a fixed number of duty ticks
+	 * per (1 << PWM_PDIV_WIDTH) cycles and make sure to round to the
+	 * nearest integer during division.
+	 */
+	pulse_width = duty_ns * PWM_PDIV_MAX;
+	pulse_width = DIV_ROUND_CLOSEST_ULL(pulse_width, period_ns);
 
 	if (priv->polarity & BIT(channel))
-		pulse_width = 256 - pulse_width;
+		pulse_width = PWM_PDIV_MAX - pulse_width;
+
+	if (pulse_width > PWM_PDIV_MAX) {
+		debug("%s: Channel %u pulse_width too high %llu\n",
+		      __func__, channel, pulse_width);
+		return -EINVAL;
+	}
+
+	/*
+	 * Since the actual PWM divider is the register's frequency divider
+	 * field plus 1, we need to decrement to get the correct value to
+	 * write to the register.
+	 */
+	rate = (priv->clk_rate * period_ns) / ((u64)NSEC_PER_SEC << PWM_PDIV_WIDTH) - 1;
+	if (rate < 0) {
+		debug("%s: Channel %u rate is not positive\n", __func__, channel);
+		return -EINVAL;
+	}
+
+	if (rate >> PWM_FDIV_WIDTH) {
+		debug("%s: Channel %u rate too high %u\n", __func__, channel, rate);
+		return -EINVAL;
+	}
 
 	reg = pulse_width << PWM_WIDTH_SHIFT;
-	reg |= 1 << PWM_DIVIDER_SHIFT;
+	reg |= rate << PWM_DIVIDER_SHIFT;
 	reg |= PWM_ENABLE_MASK;
 	writel(reg, &regs[channel].control);
-	debug("%s: pulse_width=%u\n", __func__, pulse_width);
 
 	return 0;
 }
@@ -78,9 +119,15 @@ static int tegra_pwm_of_to_plat(struct udevice *dev)
 
 static int tegra_pwm_probe(struct udevice *dev)
 {
+	struct tegra_pwm_priv *priv = dev_get_priv(dev);
 	const u32 pwm_max_freq = dev_get_driver_data(dev);
 
-	clock_start_periph_pll(PERIPH_ID_PWM, CLOCK_ID_PERIPH, pwm_max_freq);
+	priv->clk_rate = clock_start_periph_pll(PERIPH_ID_PWM, CLOCK_ID_PERIPH,
+						pwm_max_freq);
+	priv->min_period_ns = (NSEC_PER_SEC / (pwm_max_freq >> PWM_PDIV_WIDTH)) + 1;
+
+	debug("%s: clk_rate = %llu min_period_ns = %u\n", __func__,
+	      priv->clk_rate, priv->min_period_ns);
 
 	return 0;
 }
Signature · 1 line
2.51.0