Commit cd378881 authored by David Rivshin's avatar David Rivshin Committed by Thierry Reding

pwm: omap-dmtimer: Add sanity checking for load and match values

Add sanity checking to ensure that we do not program load or match values
that are out of range if a user requests period or duty_cycle values which
are not achievable. The match value cannot be less than the load value (but
can be equal), and neither can be 0xffffffff. This means that there must be
at least one fclk cycle between load and match, and another between match
and overflow.

Fixes: 6604c655 ("pwm: Add PWM driver for OMAP using dual-mode timers")
Signed-off-by: default avatarDavid Rivshin <drivshin@allworx.com>
Acked-by: default avatarNeil Armstrong <narmstrong@baylibre.com>
[thierry.reding@gmail.com: minor coding style cleanups]
Signed-off-by: default avatarThierry Reding <thierry.reding@gmail.com>
parent f8caa792
...@@ -119,15 +119,13 @@ static int pwm_omap_dmtimer_config(struct pwm_chip *chip, ...@@ -119,15 +119,13 @@ static int pwm_omap_dmtimer_config(struct pwm_chip *chip,
fclk = omap->pdata->get_fclk(omap->dm_timer); fclk = omap->pdata->get_fclk(omap->dm_timer);
if (!fclk) { if (!fclk) {
dev_err(chip->dev, "invalid pmtimer fclk\n"); dev_err(chip->dev, "invalid pmtimer fclk\n");
mutex_unlock(&omap->mutex); goto err_einval;
return -EINVAL;
} }
clk_rate = clk_get_rate(fclk); clk_rate = clk_get_rate(fclk);
if (!clk_rate) { if (!clk_rate) {
dev_err(chip->dev, "invalid pmtimer fclk rate\n"); dev_err(chip->dev, "invalid pmtimer fclk rate\n");
mutex_unlock(&omap->mutex); goto err_einval;
return -EINVAL;
} }
dev_dbg(chip->dev, "clk rate: %luHz\n", clk_rate); dev_dbg(chip->dev, "clk rate: %luHz\n", clk_rate);
...@@ -142,6 +140,8 @@ static int pwm_omap_dmtimer_config(struct pwm_chip *chip, ...@@ -142,6 +140,8 @@ static int pwm_omap_dmtimer_config(struct pwm_chip *chip,
* The non-active time is the remainder: (DM_TIMER_MAX-match_value) * The non-active time is the remainder: (DM_TIMER_MAX-match_value)
* clock cycles. * clock cycles.
* *
* NOTE: It is required that: load_value <= match_value < DM_TIMER_MAX
*
* References: * References:
* OMAP4430/60/70 TRM sections 22.2.4.10 and 22.2.4.11 * OMAP4430/60/70 TRM sections 22.2.4.10 and 22.2.4.11
* AM335x Sitara TRM sections 20.1.3.5 and 20.1.3.6 * AM335x Sitara TRM sections 20.1.3.5 and 20.1.3.6
...@@ -149,6 +149,27 @@ static int pwm_omap_dmtimer_config(struct pwm_chip *chip, ...@@ -149,6 +149,27 @@ static int pwm_omap_dmtimer_config(struct pwm_chip *chip,
period_cycles = pwm_omap_dmtimer_get_clock_cycles(clk_rate, period_ns); period_cycles = pwm_omap_dmtimer_get_clock_cycles(clk_rate, period_ns);
duty_cycles = pwm_omap_dmtimer_get_clock_cycles(clk_rate, duty_ns); duty_cycles = pwm_omap_dmtimer_get_clock_cycles(clk_rate, duty_ns);
if (period_cycles < 2) {
dev_info(chip->dev,
"period %d ns too short for clock rate %lu Hz\n",
period_ns, clk_rate);
goto err_einval;
}
if (duty_cycles < 1) {
dev_dbg(chip->dev,
"duty cycle %d ns is too short for clock rate %lu Hz\n",
duty_ns, clk_rate);
dev_dbg(chip->dev, "using minimum of 1 clock cycle\n");
duty_cycles = 1;
} else if (duty_cycles >= period_cycles) {
dev_dbg(chip->dev,
"duty cycle %d ns is too long for period %d ns at clock rate %lu Hz\n",
duty_ns, period_ns, clk_rate);
dev_dbg(chip->dev, "using maximum of 1 clock cycle less than period\n");
duty_cycles = period_cycles - 1;
}
load_value = (DM_TIMER_MAX - period_cycles) + 1; load_value = (DM_TIMER_MAX - period_cycles) + 1;
match_value = load_value + duty_cycles - 1; match_value = load_value + duty_cycles - 1;
...@@ -179,6 +200,11 @@ static int pwm_omap_dmtimer_config(struct pwm_chip *chip, ...@@ -179,6 +200,11 @@ static int pwm_omap_dmtimer_config(struct pwm_chip *chip,
mutex_unlock(&omap->mutex); mutex_unlock(&omap->mutex);
return 0; return 0;
err_einval:
mutex_unlock(&omap->mutex);
return -EINVAL;
} }
static int pwm_omap_dmtimer_set_polarity(struct pwm_chip *chip, static int pwm_omap_dmtimer_set_polarity(struct pwm_chip *chip,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment