fix and improve for Hi846
Subsystems (5)
v2 cover fix and improve for Hi846
This series fixes a error blocking Hi846 driver function, fixes link frequency and supports 6MP and 8MP modes on Hi846.
---
Changes in v2:
- remove uncessary else (Sakari)
- fix link frequency (Sakari)
- correct link frequency for DT
- Link to v1: https://lore.kernel.org/linux-media/[email protected]
Pengyu Luo (4):
media: hi846: fix hi846_write_reg_16 handling
media: hi846: fix link frequency handling
media: hi846: Add 6MP and 8MP modes support
arm64: dts: imx8mq-librem5: Correct link frequency list
.../boot/dts/freescale/imx8mq-librem5.dtsi | 2 +- drivers/media/i2c/hi846.c | 226 +++++++++++++++--- 2 files changed, 190 insertions(+), 38 deletions(-)
Signature · 1 line
2.54.0
PATCH v2 1/4 media: hi846: fix hi846_write_reg_16 handling
hi846_write_reg_16() does not clear a positive *err value on success. pm_runtime_get_if_in_use() returns a positive value when the device is already in use. When hi846_set_ctrl() passes &ret holding this positive value) to hi846_write_reg_16(), the function returns with ret as is, the positive value propagates back as a return code, which callers interpret as an error.
Fix this by resetting *err to 0 only when it is positive.
---
remove else in v2
--- drivers/media/i2c/hi846.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/media/i2c/hi846.c +2 -0
@@ -1271,6 +1271,8 @@ static void hi846_write_reg_16(struct hi846 *hi846, u16 reg, u16 val, int *err)if (*err <0 )return ; + *err =0 ; +put_unaligned_be16 (reg, buf);put_unaligned_be16 (val, buf +2 ); ret =i2c_master_send (client, buf,sizeof (buf));
Signature · 1 line
2.54.0
PATCH v2 2/4 media: hi846: fix link frequency handling
In reply to Pengyu Luolink frequency is tied to PLL configuration, lane count, and external clock rate, so use runtime here instead of hardcoding for specific configuration. To implement this, this commit did
1. dropped exposed link freqs as a v4l2_ctrl, since we are runtime now, it is inconvenient and unnecessary to expose it, and hi846_set_ctrl has nothing to do with it.
2. attached pll_cfg_2lane to current mode, and use it with clock, lane count to calculate link frequency, we use 2-lane config as default, but we can halve it easily for 4-lane case.
3. dropped mclk clock rate check.
--- drivers/media/i2c/hi846.c | 72 +++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 36 deletions(-)
drivers/media/i2c/hi846.c +36 -36
@@ -219,8 +219,8 @@ struct hi846_mode {/* Horizontal timing size */ u32 llp; -/* Link frequency needed for this resolution */ -u8 link_freq_index; +/* PLL configuration for 4-lane link at this resolution */ +u16 pll_cfg_4lane;u16 fps;
@@ -1040,13 +1040,6 @@ static const char * const hi846_test_pattern_menu[] = { "Resolution Pattern", }; -#define FREQ_INDEX_640 0 -#define FREQ_INDEX_1280 1 -static const s64 hi846_link_freqs[] = { - [FREQ_INDEX_640 ] =80000000 , - [FREQ_INDEX_1280 ] =200000000 , -}; -static const struct hi846_reg_list hi846_init_regs_list_2lane = { .num_of_regs =ARRAY_SIZE (hi846_init_2lane), .regs = hi846_init_2lane,
@@ -1061,7 +1054,7 @@ static const struct hi846_mode supported_modes[] = { { .width = 640, .height = 480, - .link_freq_index = FREQ_INDEX_640, + .pll_cfg_4lane = 0x4924, /* HI846_REG_PLL_CFG_MIPI2_H */ .fps = 120, .frame_len = 631, .llp = HI846_LINE_LENGTH,
@@ -1086,7 +1079,7 @@ static const struct hi846_mode supported_modes[] = { { .width = 1280, .height = 720, - .link_freq_index = FREQ_INDEX_1280, + .pll_cfg_4lane = 0x4924, /* HI846_REG_PLL_CFG_MIPI2_H */ .fps = 90, .frame_len = 842, .llp = HI846_LINE_LENGTH,
@@ -1112,7 +1105,7 @@ static const struct hi846_mode supported_modes[] = { { .width = 1632, .height = 1224, - .link_freq_index = FREQ_INDEX_1280, + .pll_cfg_4lane = 0x4924, /* HI846_REG_PLL_CFG_MIPI2_H */ .fps = 30, .frame_len = 2526, .llp = HI846_LINE_LENGTH,
@@ -1161,7 +1154,6 @@ struct hi846 {struct v4l2_ctrl_handler ctrl_handler;u8 nr_lanes; -struct v4l2_ctrl *link_freq;struct v4l2_ctrl *pixel_rate;struct v4l2_ctrl *vblank;struct v4l2_ctrl *hblank;
@@ -1192,21 +1184,37 @@ static const struct hi846_datafmt *hi846_find_datafmt(u32 code)return NULL ; } -static inline u8 hi846_get_link_freq_index (struct hi846 *hi846) +static u64 +hi846_get_link_freq (struct hi846 *hi846,const struct hi846_mode *cur_mode) { -return hi846->cur_mode->link_freq_index; -} +u16 cfg = cur_mode->pll_cfg_4lane; +u64 mclk =clk_get_rate (hi846->clock); -static u64 hi846_get_link_freq (struct hi846 *hi846) -{ -u8 index =hi846_get_link_freq_index (hi846); +/* NOTE: 6, 7 actually map to 8, 10, but the used won't be that big */ +u8 post_div1 =1 +FIELD_GET (GENMASK (10 ,8 ), cfg); +u8 post_div2 =1 <<FIELD_GET (GENMASK (12 ,11 ), cfg); + +/* +* HI846_REG_PLL_CFG_MIPI1_H = 0x025a, it is fixed in listed modes +* [11:8]: 0x02 => pre_div = 3 +* [7:0]: 0x5a => multiplier = 90 +*/ +u64 link_freq = mclk /3 *90 / post_div1 / post_div2; +/* +* for shared modes, since lane count is halved for 2-lane, then getting +* link_freq doubled to match the same data rate. Since 720x480 is +* 2-lane only(i.e. reg_list_4lane.num_of_regs == 0), no pll cfg for +* 4-lane, the pll cfg is actually for 2-lane, so use the cfg as is. +*/ +if (hi846->nr_lanes ==2 && cur_mode->reg_list_4lane.num_of_regs) + link_freq *=2 ; -return hi846_link_freqs[index]; +return link_freq; }static u64 hi846_calc_pixel_rate (struct hi846 *hi846) { -u64 link_freq =hi846_get_link_freq (hi846); +u64 link_freq =hi846_get_link_freq (hi846, hi846->cur_mode);u64 pixel_rate = link_freq *2 * hi846->nr_lanes;do_div (pixel_rate,HI846_RGB_DEPTH );
@@ -1426,14 +1434,6 @@ static int hi846_init_controls(struct hi846 *hi846) ctrl_hdlr->lock = &hi846->mutex; - hi846->link_freq = -v4l2_ctrl_new_int_menu (ctrl_hdlr, &hi846_ctrl_ops, -V4L2_CID_LINK_FREQ , -ARRAY_SIZE (hi846_link_freqs) -1 , -0 , hi846_link_freqs); -if (hi846->link_freq) - hi846->link_freq->flags |=V4L2_CTRL_FLAG_READ_ONLY ; - hi846->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &hi846_ctrl_ops, V4L2_CID_PIXEL_RATE, 0,
@@ -1503,10 +1503,10 @@ static int hi846_set_video_mode(struct hi846 *hi846, int fps)u64 frame_length;int ret =0 ;int dummy_lines; -u64 link_freq =hi846_get_link_freq (hi846); +u64 link_freq =hi846_get_link_freq (hi846, hi846->cur_mode);dev_dbg (&client->dev,"%s: link freq: %llu\n" , __func__, -hi846_get_link_freq (hi846)); + link_freq);do_div (link_freq, fps); frame_length = link_freq;
@@ -1749,7 +1749,6 @@ static int hi846_set_format(struct v4l2_subdev *sd, mf->code =HI846_MEDIA_BUS_FORMAT ; mf->field =V4L2_FIELD_NONE ; -__v4l2_ctrl_s_ctrl (hi846->link_freq,hi846_get_link_freq_index (hi846));__v4l2_ctrl_s_ctrl_int64 (hi846->pixel_rate,hi846_calc_pixel_rate (hi846));
@@ -1950,16 +1949,17 @@ static int hi846_identify_module(struct hi846 *hi846)static s64 hi846_check_link_freqs (struct hi846 *hi846,struct v4l2_fwnode_endpoint *ep) { -const s64 *freqs = hi846_link_freqs; -int freqs_count =ARRAY_SIZE (hi846_link_freqs); +int freqs_count =ARRAY_SIZE (supported_modes); +u64 link_freq;int i, j;for (i =0 ; i < freqs_count; i++) { + link_freq =hi846_get_link_freq (hi846, &supported_modes[i]);for (j =0 ; j < ep->nr_of_link_frequencies; j++) -if (freqs[i] == ep->link_frequencies[j]) +if (link_freq == ep->link_frequencies[j])break ;if (j == ep->nr_of_link_frequencies) -return freqs[i]; +return link_freq; }return 0 ;
Signature · 1 line
2.54.0
PATCH v2 3/4 media: hi846: Add 6MP and 8MP modes support
In reply to Pengyu LuoHi846 is an 8MP sensor, but the upstream driver has only supported 2MP mode for years. This patch adds 6MP and 8MP modes to maximize sensor utilization.
Note that these modes require 4-lane MIPI CSI-2, as the downstream driver only exposes 2MP, 6MP, and 8MP configurations in 4-lane operation on the target device. The register sequences are extracted from the downstream Windows driver.
---
No changes in v2
--- drivers/media/i2c/hi846.c | 152 +++++++++++++++++++++++++++++++++++++- 1 file changed, 151 insertions(+), 1 deletion(-)
drivers/media/i2c/hi846.c +151 -1
@@ -1027,6 +1027,106 @@ static const struct hi846_reg mode_1632x1224_mipi_4lane[] = { {HI846_REG_TG_ENABLE, 0x0100}, }; +static const struct hi846_reg mode_3264x1836_config[] = { + {HI846_REG_MODE_SELECT ,0x0000 }, + {HI846_REG_Y_ODD_INC_FOBP ,0x1111 }, + {HI846_REG_Y_ODD_INC_VACT ,0x1111 }, + {HI846_REG_Y_ADDR_START_VACT_H ,0x0172 }, + {HI846_REG_Y_ADDR_END_VACT_H ,0x089d }, + {HI846_REG_UNKNOWN_005C ,0x2101 }, + {HI846_REG_FLL ,0x09de }, + {HI846_REG_LLP ,0x0ed8 }, + {HI846_REG_BINNING_MODE ,0x0022 }, + {HI846_REG_HBIN_MODE ,0x0000 }, + {HI846_REG_UNKNOWN_0A24 ,0x0000 }, + {HI846_REG_X_START_H ,0x0000 }, + {HI846_REG_X_OUTPUT_SIZE_H ,0x0cc0 }, + {HI846_REG_Y_OUTPUT_SIZE_H ,0x072c }, + {HI846_REG_EXPOSURE ,0x09d8 }, + +/* For OTP */ + {HI846_REG_UNKNOWN_021C ,0x0001 }, + {HI846_REG_UNKNOWN_021E ,0x0235 }, + + {HI846_REG_ISP_EN_H ,0x014a }, + {HI846_REG_UNKNOWN_0418 ,0x023e }, + {HI846_REG_UNKNOWN_0B02 ,0xe04d }, + {HI846_REG_UNKNOWN_0B10 ,0x6821 }, + {HI846_REG_UNKNOWN_0B12 ,0x0120 }, + {HI846_REG_UNKNOWN_0B14 ,0x0001 }, + {HI846_REG_UNKNOWN_2008 ,0x38fd }, + {HI846_REG_UNKNOWN_326E ,0x0000 }, +}; + +static const struct hi846_reg mode_3264x1836_mipi_4lane[] = { + {HI846_REG_UNKNOWN_0900 ,0x0300 }, + {HI846_REG_MIPI_TX_OP_MODE ,0xc319 }, + {HI846_REG_UNKNOWN_0914 ,0xc109 }, + {HI846_REG_TCLK_PREPARE ,0x061a }, + {HI846_REG_UNKNOWN_0918 ,0x0407 }, + {HI846_REG_THS_ZERO ,0x0a0b }, + {HI846_REG_TCLK_POST ,0x0e08 }, + {HI846_REG_UNKNOWN_091E ,0x0a00 }, + {HI846_REG_UNKNOWN_090C ,0x0427 }, + {HI846_REG_UNKNOWN_090E ,0x0059 }, + {HI846_REG_UNKNOWN_0954 ,0x0089 }, + {HI846_REG_UNKNOWN_0956 ,0x0000 }, + {HI846_REG_UNKNOWN_0958 ,0xca80 }, + {HI846_REG_UNKNOWN_095A ,0x9240 }, + {HI846_REG_PLL_CFG_MIPI2_H ,0x4124 }, + {HI846_REG_TG_ENABLE ,0x0100 }, +}; + +static const struct hi846_reg mode_3264x2448_config[] = { + {HI846_REG_MODE_SELECT ,0x0000 }, + {HI846_REG_Y_ODD_INC_FOBP ,0x1111 }, + {HI846_REG_Y_ODD_INC_VACT ,0x1111 }, + {HI846_REG_Y_ADDR_START_VACT_H ,0x0040 }, + {HI846_REG_Y_ADDR_END_VACT_H ,0x09cf }, + {HI846_REG_UNKNOWN_005C ,0x2101 }, + {HI846_REG_FLL ,0x09de }, + {HI846_REG_LLP ,0x0ed8 }, + {HI846_REG_BINNING_MODE ,0x0022 }, + {HI846_REG_HBIN_MODE ,0x0000 }, + {HI846_REG_UNKNOWN_0A24 ,0x0000 }, + {HI846_REG_X_START_H ,0x0000 }, + {HI846_REG_X_OUTPUT_SIZE_H ,0x0cc0 }, + {HI846_REG_Y_OUTPUT_SIZE_H ,0x0990 }, + {HI846_REG_EXPOSURE ,0x09d8 }, + +/* For OTP */ + {HI846_REG_UNKNOWN_021C ,0x0001 }, + {HI846_REG_UNKNOWN_021E ,0x0235 }, + + {HI846_REG_ISP_EN_H ,0x014a }, + {HI846_REG_UNKNOWN_0418 ,0x0000 }, + {HI846_REG_UNKNOWN_0B02 ,0xe04d }, + {HI846_REG_UNKNOWN_0B10 ,0x6821 }, + {HI846_REG_UNKNOWN_0B12 ,0x0120 }, + {HI846_REG_UNKNOWN_0B14 ,0x0001 }, + {HI846_REG_UNKNOWN_2008 ,0x38fd }, + {HI846_REG_UNKNOWN_326E ,0x0000 }, +}; + +static const struct hi846_reg mode_3264x2448_mipi_4lane[] = { + {HI846_REG_UNKNOWN_0900 ,0x0300 }, + {HI846_REG_MIPI_TX_OP_MODE ,0xc319 }, + {HI846_REG_UNKNOWN_0914 ,0xc109 }, + {HI846_REG_TCLK_PREPARE ,0x061a }, + {HI846_REG_UNKNOWN_0918 ,0x0407 }, + {HI846_REG_THS_ZERO ,0x0a0b }, + {HI846_REG_TCLK_POST ,0x0e08 }, + {HI846_REG_UNKNOWN_091E ,0x0a00 }, + {HI846_REG_UNKNOWN_090C ,0x0427 }, + {HI846_REG_UNKNOWN_090E ,0x0059 }, + {HI846_REG_UNKNOWN_0954 ,0x0089 }, + {HI846_REG_UNKNOWN_0956 ,0x0000 }, + {HI846_REG_UNKNOWN_0958 ,0xca80 }, + {HI846_REG_UNKNOWN_095A ,0x9240 }, + {HI846_REG_PLL_CFG_MIPI2_H ,0x4124 }, + {HI846_REG_TG_ENABLE ,0x0100 }, +}; +static const char *const hi846_test_pattern_menu[] = {"Disabled" , "Solid Colour",
@@ -1127,7 +1227,57 @@ static const struct hi846_mode supported_modes[] = { .width = 1632 * 2, .height = 1224 * 2, }, - } + }, + { + .width = 3264, + .height = 1836, + .pll_cfg_4lane = 0x4124, /* HI846_REG_PLL_CFG_MIPI2_H */ + .fps = 30, + .frame_len = 2526, + .llp = HI846_LINE_LENGTH, + .reg_list_config = { + .num_of_regs = ARRAY_SIZE(mode_3264x1836_config), + .regs = mode_3264x1836_config, + }, + .reg_list_2lane = { + .num_of_regs = 0, + }, + .reg_list_4lane = { + .num_of_regs = ARRAY_SIZE(mode_3264x1836_mipi_4lane), + .regs = mode_3264x1836_mipi_4lane, + }, + .crop = { + .left = 0x46, + .top = 0x172, + .width = 3264, + .height = 1836, + }, + }, + { + .width = 3264, + .height = 2448, + .pll_cfg_4lane = 0x4124, /* HI846_REG_PLL_CFG_MIPI2_H */ + .fps = 30, + .frame_len = 2526, + .llp = HI846_LINE_LENGTH, + .reg_list_config = { + .num_of_regs = ARRAY_SIZE(mode_3264x2448_config), + .regs = mode_3264x2448_config, + }, + .reg_list_2lane = { + .num_of_regs = 0, + }, + .reg_list_4lane = { + .num_of_regs = ARRAY_SIZE(mode_3264x2448_mipi_4lane), + .regs = mode_3264x2448_mipi_4lane, + }, + .crop = { + .left = 0x46, + .top = 0x40, + .width = 3264, + .height = 2448, + }, + }, }; struct hi846_datafmt {
Signature · 1 line
2.54.0
Re: [PATCH v2 3/4] media: hi846: Add 6MP and 8MP modes support
Quoted 8 lines · On piątek, 1 maja 2026 11:54:32 czas środkowoeuropejski letni Pengyu Luo wrote:
Hi846 is an 8MP sensor, but the upstream driver has only supported 2MP mode for years. This patch adds 6MP and 8MP modes to maximize sensor utilization.
Note that these modes require 4-lane MIPI CSI-2, as the downstream driver only exposes 2MP, 6MP, and 8MP configurations in 4-lane operation on the target device. The register sequences are extracted from the downstream Windows driver.
Has this been tested in this form at all? I tried, failed and looking at the driver I don't see how could it ever end up using these modes in its current state. It can only cause troubles when used with 2 lanes, while 4 lanes are completely broken.
The driver defaults to its first supported mode, which happens to be a 640x480 mode that only defines its register list for 2 lanes. hi846_set_format was supposedly meant to check whether the mode returned by v4l2_find_nearest_size is compatible with the used lane count, but it does so too early - it actually checks it against the already set mode rather than the one it's about to set. This means that you're never going to be able to set any valid mode when using 4 lanes without fixing this first.
And even if this was fixed, with 2 lanes this makes some calls from userspace that previously succeeded now fail when v4l2_find_nearest_size happens to match a mode that's only supported with 4 lanes. v4l2_find_nearest_size would have to be fed with already filtered list of modes to fix that (which would actually make the check mentioned above unnecessary).
Of course these are preexisting issues in the driver, but they make it impossible to actually use what's being added in this patch and to add these modes without causing regressions. So far these didn't matter as this driver was only ever used with 2 lanes, but they have to be fixed first before adding any actual 4 lane usage or modes that require 4 lanes.
S.
Re: [PATCH v2 3/4] media: hi846: Add 6MP and 8MP modes support
Quoted 12 lines · Sebastian Krzyszkowiak
Quoted 8 lines · On piątek, 1 maja 2026 11:54:32 czas środkowoeuropejski letni Pengyu Luo wrote:
Hi846 is an 8MP sensor, but the upstream driver has only supported 2MP mode for years. This patch adds 6MP and 8MP modes to maximize sensor utilization.
Note that these modes require 4-lane MIPI CSI-2, as the downstream driver only exposes 2MP, 6MP, and 8MP configurations in 4-lane operation on the target device. The register sequences are extracted from the downstream Windows driver.
Quoted 5 lines
Has this been tested in this form at all? I tried, failed and looking at the driver I don't see how could it ever end up using these modes in its current state. It can only cause troubles when used with 2 lanes, while 4 lanes are completely broken.
I may have messed up. I tested it with two more patches. The first patch removes 480P and 720P, the second uses a new 4-lane init list.(I just sincerely followed the downstream) Since I didn't think much, I thought removing modes would not break things, I just tested without the second patch, so it works for me.
Quoted 14 lines
The driver defaults to its first supported mode, which happens to be a 640x480 mode that only defines its register list for 2 lanes.
hi846_set_formatwas supposedly meant to check whether the mode returned byv4l2_find_nearest_sizeis compatible with the used lane count, but it does so too early - it actually checks it against the already set mode rather than the one it's about to set. This means that you're never going to be able to set any valid mode when using 4 lanes without fixing this first.And even if this was fixed, with 2 lanes this makes some calls from userspace that previously succeeded now fail when
v4l2_find_nearest_sizehappens to match a mode that's only supported with 4 lanes.v4l2_find_nearest_sizewould have to be fed with already filtered list of modes to fix that (which would actually make the check mentioned above unnecessary).
Quoted 6 lines
Of course these are preexisting issues in the driver, but they make it impossible to actually use what's being added in this patch and to add these modes without causing regressions. So far these didn't matter as this driver was only ever used with 2 lanes, but they have to be fixed first before adding any actual 4 lane usage or modes that require 4 lanes.
Thanks for your feedback. I think we can use specific modes for different lane modes. The snippet like
hi846->supported_modes = supported_modes;
hi846->num_modes = ARRAY_SIZE(supported_modes) - 2;
if (hi846->nr_lanes == 4) {
hi846->supported_modes = supported_modes + 1;
hi846->num_modes = ARRAY_SIZE(supported_modes) - 1;
}Best wishes,
Pengyu
Quoted 3 lines
S.
PATCH v2 4/4 arm64: dts: imx8mq-librem5: Correct link frequency list
In reply to Pengyu LuoAccording to the hi846 datasheet and the register lists, they are
mclk / prediv * multiplier / post_div1 / post_div2 = 25Mhz / 3 * 90 / 1 / {1,2} = 187.5Mhz, 375Mhz
for the detail, please check PATCH 2/4.
--- arch/arm64/boot/dts/freescale/imx8mq-librem5.dtsi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/boot/dts/freescale/imx8mq-librem5.dtsi +1 -1
@@ -1122,7 +1122,7 @@ port { camera1_ep: endpoint { data-lanes = <1 2>; link-frequencies = /bits/ 64 - <80000000 200000000 300000000>; + <187500000 375000000>; remote-endpoint = <&mipi1_sensor_ep>; }; };
Signature · 1 line
2.54.0