Skip to content

Commit 78d7265

Browse files
emfendhverkuil
authored andcommitted
media: tc358746: improve calculation of the D-PHY timing registers
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]>
1 parent 44967f0 commit 78d7265

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
@@ -485,24 +485,20 @@ static int tc358746_apply_misc_config(struct tc358746 *tc358746)
485485
return err;
486486
}
487487

488-
/* Use MHz as base so the div needs no u64 */
489-
static u32 tc358746_cfg_to_cnt(unsigned int cfg_val,
490-
unsigned int clk_mhz,
491-
unsigned int time_base)
488+
static u32 tc358746_cfg_to_cnt(unsigned long cfg_val, unsigned long clk_hz,
489+
unsigned long long time_base)
492490
{
493-
return DIV_ROUND_UP(cfg_val * clk_mhz, time_base);
491+
return div64_u64((u64)cfg_val * clk_hz + time_base - 1, time_base);
494492
}
495493

496-
static u32 tc358746_ps_to_cnt(unsigned int cfg_val,
497-
unsigned int clk_mhz)
494+
static u32 tc358746_ps_to_cnt(unsigned long cfg_val, unsigned long clk_hz)
498495
{
499-
return tc358746_cfg_to_cnt(cfg_val, clk_mhz, USEC_PER_SEC);
496+
return tc358746_cfg_to_cnt(cfg_val, clk_hz, PSEC_PER_SEC);
500497
}
501498

502-
static u32 tc358746_us_to_cnt(unsigned int cfg_val,
503-
unsigned int clk_mhz)
499+
static u32 tc358746_us_to_cnt(unsigned long cfg_val, unsigned long clk_hz)
504500
{
505-
return tc358746_cfg_to_cnt(cfg_val, clk_mhz, 1);
501+
return tc358746_cfg_to_cnt(cfg_val, clk_hz, USEC_PER_SEC);
506502
}
507503

508504
static int tc358746_apply_dphy_config(struct tc358746 *tc358746)
@@ -517,7 +513,6 @@ static int tc358746_apply_dphy_config(struct tc358746 *tc358746)
517513

518514
/* The hs_byte_clk is also called SYSCLK in the excel sheet */
519515
hs_byte_clk = cfg->hs_clk_rate / 8;
520-
hs_byte_clk /= HZ_PER_MHZ;
521516
hf_clk = hs_byte_clk / 2;
522517

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

0 commit comments

Comments
 (0)