Commit 4d798332 authored by Axel Lin's avatar Axel Lin Committed by Bryan Wu

leds: renesas-tpu: Improve the readability to pick the lowest acceptable rate

I spent a few minutes to understand why the code catching the mismatch case
by checking if k is 0 or not. And the code using "k - 1" as array index is
unusual.

This patch checks acceptable rate from the lowest rate, and then we don't need
to subtract k by 1. This change improves the readability.
Signed-off-by: default avatarAxel Lin <axel.lin@ingics.com>
Signed-off-by: default avatarBryan Wu <cooloney@gmail.com>
parent 5e3b7c6b
...@@ -133,24 +133,24 @@ static int r_tpu_enable(struct r_tpu_priv *p, enum led_brightness brightness) ...@@ -133,24 +133,24 @@ static int r_tpu_enable(struct r_tpu_priv *p, enum led_brightness brightness)
rate = clk_get_rate(p->clk); rate = clk_get_rate(p->clk);
/* pick the lowest acceptable rate */ /* pick the lowest acceptable rate */
for (k = 0; k < ARRAY_SIZE(prescaler); k++) for (k = ARRAY_SIZE(prescaler) - 1; k >= 0; k--)
if ((rate / prescaler[k]) < p->min_rate) if ((rate / prescaler[k]) >= p->min_rate)
break; break;
if (!k) { if (k < 0) {
dev_err(&p->pdev->dev, "clock rate mismatch\n"); dev_err(&p->pdev->dev, "clock rate mismatch\n");
goto err0; goto err0;
} }
dev_dbg(&p->pdev->dev, "rate = %lu, prescaler %u\n", dev_dbg(&p->pdev->dev, "rate = %lu, prescaler %u\n",
rate, prescaler[k - 1]); rate, prescaler[k]);
/* clear TCNT on TGRB match, count on rising edge, set prescaler */ /* clear TCNT on TGRB match, count on rising edge, set prescaler */
r_tpu_write(p, TCR, 0x0040 | (k - 1)); r_tpu_write(p, TCR, 0x0040 | k);
/* output 0 until TGRA, output 1 until TGRB */ /* output 0 until TGRA, output 1 until TGRB */
r_tpu_write(p, TIOR, 0x0002); r_tpu_write(p, TIOR, 0x0002);
rate /= prescaler[k - 1] * p->refresh_rate; rate /= prescaler[k] * p->refresh_rate;
r_tpu_write(p, TGRB, rate); r_tpu_write(p, TGRB, rate);
dev_dbg(&p->pdev->dev, "TRGB = 0x%04lx\n", rate); dev_dbg(&p->pdev->dev, "TRGB = 0x%04lx\n", rate);
......
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