Skip to content

Commit 412cf1c

Browse files
emfendjfvogel
authored andcommitted
media: tc358746: improve calculation of the D-PHY timing registers
[ Upstream commit 78d7265e2e1ce349e7f3c6a085f2b66d7b73f4ca ] When calculating D-PHY registers, using data rates that are not multiples of 16 can lead to precision loss in division operations. This can result in register values that produce timing violations against the MIPI standard. An example: cfg->hs_clk_rate = 294MHz hf_clk = 18 If the desired value in cfg->init is 100us, which is the minimum allowed value, then the LINEINITCNT register is calculated as 1799. But since the actual clock is 18.375MHz instead of 18MHz, this setting results in a time that is shorter than 100us and thus violates the standard. The correct value for LINEINITCNT would be 1837. Improve the precision of calculations by using Hz instead of MHz as unit. Signed-off-by: Matthias Fend <[email protected]> Reviewed-by: Marco Felsch <[email protected]> Signed-off-by: Hans Verkuil <[email protected]> Signed-off-by: Sasha Levin <[email protected]> (cherry picked from commit c2aa6567a6a486a61325eb222afbebf28768989f) Signed-off-by: Jack Vogel <[email protected]>
1 parent 2b62f1f commit 412cf1c

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

drivers/media/i2c/tc358746.c

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -460,24 +460,20 @@ static int tc358746_apply_misc_config(struct tc358746 *tc358746)
460460
return err;
461461
}
462462

463-
/* Use MHz as base so the div needs no u64 */
464-
static u32 tc358746_cfg_to_cnt(unsigned int cfg_val,
465-
unsigned int clk_mhz,
466-
unsigned int time_base)
463+
static u32 tc358746_cfg_to_cnt(unsigned long cfg_val, unsigned long clk_hz,
464+
unsigned long long time_base)
467465
{
468-
return DIV_ROUND_UP(cfg_val * clk_mhz, time_base);
466+
return div64_u64((u64)cfg_val * clk_hz + time_base - 1, time_base);
469467
}
470468

471-
static u32 tc358746_ps_to_cnt(unsigned int cfg_val,
472-
unsigned int clk_mhz)
469+
static u32 tc358746_ps_to_cnt(unsigned long cfg_val, unsigned long clk_hz)
473470
{
474-
return tc358746_cfg_to_cnt(cfg_val, clk_mhz, USEC_PER_SEC);
471+
return tc358746_cfg_to_cnt(cfg_val, clk_hz, PSEC_PER_SEC);
475472
}
476473

477-
static u32 tc358746_us_to_cnt(unsigned int cfg_val,
478-
unsigned int clk_mhz)
474+
static u32 tc358746_us_to_cnt(unsigned long cfg_val, unsigned long clk_hz)
479475
{
480-
return tc358746_cfg_to_cnt(cfg_val, clk_mhz, 1);
476+
return tc358746_cfg_to_cnt(cfg_val, clk_hz, USEC_PER_SEC);
481477
}
482478

483479
static int tc358746_apply_dphy_config(struct tc358746 *tc358746)
@@ -492,7 +488,6 @@ static int tc358746_apply_dphy_config(struct tc358746 *tc358746)
492488

493489
/* The hs_byte_clk is also called SYSCLK in the excel sheet */
494490
hs_byte_clk = cfg->hs_clk_rate / 8;
495-
hs_byte_clk /= HZ_PER_MHZ;
496491
hf_clk = hs_byte_clk / 2;
497492

498493
val = tc358746_us_to_cnt(cfg->init, hf_clk) - 1;

0 commit comments

Comments
 (0)