Skip to contentSkip to conversation

phy: dphy: Fix Unit Interval(UI) calculation formula

RFC PATCH New Changhuang Liang 2 participants 3 messages Oct 31, 2024 – Nov 3, 2024 Generic PHY framework

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 phy: dphy: Fix Unit Interval(UI) calculation formula

ALIGN() expects its second argument to be a power of 2, but in the context, the hs_clk_rate will not satisfy this condition in most cases. This may lead to unknown results in the UI calculation.

So, let fix the UI calculation formula.

Fixes dddc97e82303 ("phy: dphy: Add configuration helpers")

---

Hi, Maxime

This patch may require your careful review. I have no environmental test so there is no guarantee of any effect.

This UI calculation is a reference to your patch:
https://lore.kernel.org/all/aa491b814100a670ad16b646765005efbdae05d9.1536138624.git-series.maxime.ripard@bootlin.com/

Best Regards,
Changhuang

 drivers/phy/phy-core-mipi-dphy.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/phy/phy-core-mipi-dphy.c +2 -4
index f4956a417a47..c7712e594dbf 100644
@@ -33,8 +33,7 @@ static int phy_mipi_dphy_calc_config(unsigned long pixel_clock,
 		do_div(hs_clk_rate, lanes);
 	}
 
-	ui = ALIGN(PSEC_PER_SEC, hs_clk_rate);
-	do_div(ui, hs_clk_rate);
+	ui = DIV_ROUND_UP(PSEC_PER_SEC, hs_clk_rate);
 
 	cfg->clk_miss = 0;
 	cfg->clk_post = 60000 + 52 * ui;
@@ -111,8 +110,7 @@ int phy_mipi_dphy_config_validate(struct phy_configure_opts_mipi_dphy *cfg)
 	if (!cfg)
 		return -EINVAL;
 
-	ui = ALIGN(PSEC_PER_SEC, cfg->hs_clk_rate);
-	do_div(ui, cfg->hs_clk_rate);
+	ui = DIV_ROUND_UP(PSEC_PER_SEC, cfg->hs_clk_rate);
 
 	if (cfg->clk_miss > 60000)
 		return -EINVAL;

--
2.25.1

#1 lore raw
kernel test robot noticed the following build errors:

Hi Changhuang,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:

[auto build test ERROR on linus/master]
[also build test ERROR on v6.12-rc5 next-20241101]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Changhuang-Liang/phy-dphy-Fix-Unit-Interval-UI-calculation-formula/20241031-203920
base:   linus/master
patch link:    https://lore.kernel.org/r/20241031123643.272111-1-changhuang.liang%40starfivetech.com
patch subject: [RFC PATCH] phy: dphy: Fix Unit Interval(UI) calculation formula
config: i386-buildonly-randconfig-001-20241103 (https://download.01.org/0day-ci/archive/20241103/[email protected]/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241103/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

ld: drivers/phy/phy-core-mipi-dphy.o: in function `phy_mipi_dphy_calc_config':

>> drivers/phy/phy-core-mipi-dphy.c:36: undefined reference to `__udivdi3'
   ld: drivers/phy/phy-core-mipi-dphy.o: in function `phy_mipi_dphy_config_validate':
>> drivers/phy/phy-core-mipi-dphy.c:113: undefined reference to `__divdi3'

vim +36 drivers/phy/phy-core-mipi-dphy.c

    14	
    15	/*
    16	 * Minimum D-PHY timings based on MIPI D-PHY specification. Derived
    17	 * from the valid ranges specified in Section 6.9, Table 14, Page 41
    18	 * of the D-PHY specification (v1.2).
    19	 */
    20	static int phy_mipi_dphy_calc_config(unsigned long pixel_clock,
    21					     unsigned int bpp,
    22					     unsigned int lanes,
    23					     unsigned long long hs_clk_rate,
    24					     struct phy_configure_opts_mipi_dphy *cfg)
    25	{
    26		unsigned long long ui;
    27	
    28		if (!cfg)
    29			return -EINVAL;
    30	
    31		if (!hs_clk_rate) {
    32			hs_clk_rate = pixel_clock * bpp;
    33			do_div(hs_clk_rate, lanes);
    34		}
    35	
  > 36		ui = DIV_ROUND_UP(PSEC_PER_SEC, hs_clk_rate);
    37	
    38		cfg->clk_miss = 0;
    39		cfg->clk_post = 60000 + 52 * ui;
    40		cfg->clk_pre = 8;
    41		cfg->clk_prepare = 38000;
    42		cfg->clk_settle = 95000;
    43		cfg->clk_term_en = 0;
    44		cfg->clk_trail = 60000;
    45		cfg->clk_zero = 262000;
    46		cfg->d_term_en = 0;
    47		cfg->eot = 0;
    48		cfg->hs_exit = 100000;
    49		cfg->hs_prepare = 40000 + 4 * ui;
    50		cfg->hs_zero = 105000 + 6 * ui;
    51		cfg->hs_settle = 85000 + 6 * ui;
    52		cfg->hs_skip = 40000;
    53	
    54		/*
    55		 * The MIPI D-PHY specification (Section 6.9, v1.2, Table 14, Page 40)
    56		 * contains this formula as:
    57		 *
    58		 *     T_HS-TRAIL = max(n * 8 * ui, 60 + n * 4 * ui)
    59		 *
    60		 * where n = 1 for forward-direction HS mode and n = 4 for reverse-
    61		 * direction HS mode. There's only one setting and this function does
    62		 * not parameterize on anything other that ui, so this code will
    63		 * assumes that reverse-direction HS mode is supported and uses n = 4.
    64		 */
    65		cfg->hs_trail = max(4 * 8 * ui, 60000 + 4 * 4 * ui);
    66	
    67		cfg->init = 100;
    68		cfg->lpx = 50000;
    69		cfg->ta_get = 5 * cfg->lpx;
    70		cfg->ta_go = 4 * cfg->lpx;
    71		cfg->ta_sure = cfg->lpx;
    72		cfg->wakeup = 1000;
    73	
    74		cfg->hs_clk_rate = hs_clk_rate;
    75		cfg->lanes = lanes;
    76	
    77		return 0;
    78	}
    79	
    80	int phy_mipi_dphy_get_default_config(unsigned long pixel_clock,
    81					     unsigned int bpp,
    82					     unsigned int lanes,
    83					     struct phy_configure_opts_mipi_dphy *cfg)
    84	{
    85		return phy_mipi_dphy_calc_config(pixel_clock, bpp, lanes, 0, cfg);
    86	
    87	}
    88	EXPORT_SYMBOL(phy_mipi_dphy_get_default_config);
    89	
    90	int phy_mipi_dphy_get_default_config_for_hsclk(unsigned long long hs_clk_rate,
    91						       unsigned int lanes,
    92						       struct phy_configure_opts_mipi_dphy *cfg)
    93	{
    94		if (!hs_clk_rate)
    95			return -EINVAL;
    96	
    97		return phy_mipi_dphy_calc_config(0, 0, lanes, hs_clk_rate, cfg);
    98	
    99	}
   100	EXPORT_SYMBOL(phy_mipi_dphy_get_default_config_for_hsclk);
   101	
   102	/*
   103	 * Validate D-PHY configuration according to MIPI D-PHY specification
   104	 * (v1.2, Section Section 6.9 "Global Operation Timing Parameters").
   105	 */
   106	int phy_mipi_dphy_config_validate(struct phy_configure_opts_mipi_dphy *cfg)
   107	{
   108		unsigned long long ui;
   109	
   110		if (!cfg)
   111			return -EINVAL;
   112	
 > 113		ui = DIV_ROUND_UP(PSEC_PER_SEC, cfg->hs_clk_rate);
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#2 lore raw
In reply to Changhuang Liang
kernel test robot noticed the following build errors:

Hi Changhuang,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:

[auto build test ERROR on linus/master]
[also build test ERROR on v6.12-rc5 next-20241101]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Changhuang-Liang/phy-dphy-Fix-Unit-Interval-UI-calculation-formula/20241031-203920
base:   linus/master
patch link:    https://lore.kernel.org/r/20241031123643.272111-1-changhuang.liang%40starfivetech.com
patch subject: [RFC PATCH] phy: dphy: Fix Unit Interval(UI) calculation formula
config: i386-allmodconfig (https://download.01.org/0day-ci/archive/20241103/[email protected]/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241103/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

ld: drivers/phy/phy-core-mipi-dphy.o: in function `phy_mipi_dphy_calc_config':

>> phy-core-mipi-dphy.c:(.text+0x4c): undefined reference to `__udivdi3'
   ld: drivers/phy/phy-core-mipi-dphy.o: in function `phy_mipi_dphy_config_validate':
>> phy-core-mipi-dphy.c:(.text+0x268): undefined reference to `__divdi3'
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki