Re: [PATCH] i2c: imx: choose the better clock divider

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Sorry forgot to include Ben.

Ben, any ideas?

On Tue, Jun 14, 2011 at 3:17 PM, Eric Miao <eric.miao@xxxxxxxxxx> wrote:
> The original algorithm doesn't perform very well in some cases, e.g.
>
> ÂWhen the source clock of the I2C controller is 66MHz, and the
> Ârequested rate is 100KHz, it gives a divider of 768 instead of
> Âthe better 640.
>
> Choose a better clock divider so the final clock rate is closer to
> the requested one by comparing the rate distances calculated by
> two adjacent dividers.
>
> Cc: Richard Zhao <richard.zhao@xxxxxxxxxx>
> Signed-off-by: Eric Miao <eric.miao@xxxxxxxxxx>
> ---
> Âdrivers/i2c/busses/i2c-imx.c | Â 46 ++++++++++++++++++++++++++++-------------
> Â1 files changed, 31 insertions(+), 15 deletions(-)
>
> diff --git a/drivers/i2c/busses/i2c-imx.c b/drivers/i2c/busses/i2c-imx.c
> index 4c2a62b..cd640ff 100644
> --- a/drivers/i2c/busses/i2c-imx.c
> +++ b/drivers/i2c/busses/i2c-imx.c
> @@ -112,6 +112,8 @@ static u16 __initdata i2c_clk_div[50][2] = {
> Â Â Â Â{ 3072, 0x1E }, { 3840, 0x1F }
> Â};
>
> +#define I2C_CLK_DIV_NUM Â Â Â Â Â Â Â ÂARRAY_SIZE(i2c_clk_div)
> +
> Âstruct imx_i2c_struct {
>    Âstruct i2c_adapter   Âadapter;
>    Âstruct resource     *res;
> @@ -240,22 +242,37 @@ static void i2c_imx_stop(struct imx_i2c_struct *i2c_imx)
> Â Â Â Âclk_disable(i2c_imx->clk);
> Â}
>
> -static void __init i2c_imx_set_clk(struct imx_i2c_struct *i2c_imx,
> - Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â unsigned int rate)
> +/* find the index into i2c_clk_div[] array, compare with the two
> + * dividers found, return the one with smaller error
> + */
> +static int find_div(unsigned long i2c_clk_rate, unsigned long rate)
> Â{
> - Â Â Â unsigned int i2c_clk_rate;
> - Â Â Â unsigned int div;
> + Â Â Â unsigned long div, delta_l, delta_h;
> Â Â Â Âint i;
>
> - Â Â Â /* Divider value calculation */
> - Â Â Â i2c_clk_rate = clk_get_rate(i2c_imx->clk);
> Â Â Â Âdiv = (i2c_clk_rate + rate - 1) / rate;
> +
> Â Â Â Âif (div < i2c_clk_div[0][0])
> - Â Â Â Â Â Â Â i = 0;
> - Â Â Â else if (div > i2c_clk_div[ARRAY_SIZE(i2c_clk_div) - 1][0])
> - Â Â Â Â Â Â Â i = ARRAY_SIZE(i2c_clk_div) - 1;
> - Â Â Â else
> - Â Â Â Â Â Â Â for (i = 0; i2c_clk_div[i][0] < div; i++);
> + Â Â Â Â Â Â Â return 0;
> +
> + Â Â Â if (div > i2c_clk_div[I2C_CLK_DIV_NUM - 1][0])
> + Â Â Â Â Â Â Â return I2C_CLK_DIV_NUM;
> +
> + Â Â Â for (i = 0; i < I2C_CLK_DIV_NUM; i++)
> + Â Â Â Â Â Â Â if (i2c_clk_div[i][0] > div)
> + Â Â Â Â Â Â Â Â Â Â Â break;
> +
> + Â Â Â delta_h = (i2c_clk_rate / i2c_clk_div[i - 1][0]) - rate;
> + Â Â Â delta_l = rate - (i2c_clk_rate / i2c_clk_div[i][0]);
> +
> + Â Â Â return (delta_l < delta_h) ? i : i - 1;
> +}
> +
> +static void __init i2c_imx_set_clk(struct imx_i2c_struct *i2c_imx,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âunsigned int rate)
> +{
> + Â Â Â unsigned long i2c_clk_rate = clk_get_rate(i2c_imx->clk);
> + Â Â Â int i = find_div(i2c_clk_rate, rate);
>
> Â Â Â Â/* Store divider value */
> Â Â Â Âi2c_imx->ifdr = i2c_clk_div[i][1];
> @@ -271,10 +288,9 @@ static void __init i2c_imx_set_clk(struct imx_i2c_struct *i2c_imx,
>
> Â Â Â Â/* dev_dbg() can't be used, because adapter is not yet registered */
> Â#ifdef CONFIG_I2C_DEBUG_BUS
> - Â Â Â printk(KERN_DEBUG "I2C: <%s> I2C_CLK=%d, REQ DIV=%d\n",
> - Â Â Â Â Â Â Â __func__, i2c_clk_rate, div);
> - Â Â Â printk(KERN_DEBUG "I2C: <%s> IFDR[IC]=0x%x, REAL DIV=%d\n",
> - Â Â Â Â Â Â Â __func__, i2c_clk_div[i][1], i2c_clk_div[i][0]);
> + Â Â Â pr_debug("<%s> I2C_CLK=%ld, REQ RATE=%d, DIV=%d, IFDR[IC]=0x%x\n",
> + Â Â Â Â Â Â Â __func__, i2c_clk_rate, rate,
> + Â Â Â Â Â Â Â i2c_clk_div[i][0], i2c_clk_div[i][1]);
> Â#endif
> Â}
>
> --
> 1.7.4.1
>
>
--
To unsubscribe from this list: send the line "unsubscribe linux-i2c" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux GPIO]     [Linux SPI]     [Linux Hardward Monitoring]     [LM Sensors]     [Linux USB Devel]     [Linux Media]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux