5.17.5. Sony Sensor Bring-Up

  • This document uses imx586 as an example to describe the complete process of adapting Sony series Camera Sensors on the X5 platform

  • The process includes driver implementation, App configuration, and program verification. For general concepts and DTS configuration, please first read the Camera Debugging Guide.

Source Code Location

  • Driver interface implementation: hbre/camsys/libcam/src/sensor/imx586/imx586_setting.c

  • Gain table and setting configuration: hbre/camsys/libcam/src/sensor/imx586/inc/imx586_setting.h (gain_lut, init/stream_on/stream_off register arrays)


5.17.5.1. DTS Configuration


5.17.5.2. Driver

sensor_module_t and File Naming

  • File name: imx586_utility.c; structure/module name: imx586; library name: libimx586.so.1.0.0, deployed to /usr/hobot/lib/sensor/ on the board.

  • Required interfaces: init, deinit, start, stop, power_on, power_off, aexp_gain_control, aexp_line_control, userspace_control.

#ifdef CAMERA_FRAMEWORK_HBN
SENSOR_MODULE_F(imx586, CAM_MODULE_FLAG_A16D8);
sensor_module_t imx586 = {
        .module = SENSOR_MNAME(imx586),
#else
sensor_module_t imx586 = {
        .module = "imx586",
#endif
        .init = sensor_init,
        .start = sensor_start,
        .stop = sensor_stop,
        .deinit = sensor_deinit,
        .power_on = sensor_poweron,
        .power_off = sensor_poweroff,
        .aexp_gain_control = sensor_aexp_gain_control,
        .aexp_line_control = sensor_aexp_line_control,
        .userspace_control = sensor_userspace_control,
};

power_on

  1. power_on controls sensor power-on via X5 GPIO, following the spec power-up timing.

  2. sensor_poweron is typically called in sensor_init and only needs to control XCLR. Below is the power_on implementation for imx586:

../../_images/screenshot-20260210-154827.png

static int sensor_poweron(sensor_info_t *sensor_info)
{
        int gpio, ret = RET_OK;
        vin_dbg("%s gpio_num = %d \n", sensor_info->sensor_name, sensor_info->gpio_num);
        if(sensor_info->gpio_num > 0) {
                for(gpio = 0; gpio < sensor_info->gpio_num; gpio++) {
                        vin_dbg("%s gpio_pin[%d] = %d \n", sensor_info->sensor_name, gpio, sensor_info->gpio_pin[gpio]);
                        if(sensor_info->gpio_pin[gpio] != -1) {
                                ret = vin_power_ctrl(sensor_info->gpio_pin[gpio],
                                                sensor_info->gpio_level[gpio]);
                                usleep(100 * 1000);  // 100ms
                                ret |= vin_power_ctrl(sensor_info->gpio_pin[gpio],
                                                1 - sensor_info->gpio_level[gpio]);
                                if(ret < 0) {
                                        vin_err("vin_power_ctrl fail\n");
                                        return -HB_CAM_SENSOR_POWERON_FAIL;
                                }
                                usleep(100 * 1000);  // 100ms
                        }
                }
        }
        return ret;
}

power_off

  1. power_off controls sensor power-off via X5 GPIO, following the spec power-down timing.

  2. sensor_poweroff is typically called in sensor_deinit and only needs to control XCLR. Below is the power_off implementation for imx586:

../../_images/screenshot-20260210-154309.png

static int sensor_poweroff(sensor_info_t *sensor_info)
{
        int gpio, ret = RET_OK;
        if(sensor_info->gpio_num > 0) {
                for(gpio = 0; gpio < sensor_info->gpio_num; gpio++) {
                        if(sensor_info->gpio_pin[gpio] != -1) {
                                ret = vin_power_ctrl(sensor_info->gpio_pin[gpio],
                                        sensor_info->gpio_level[gpio]);
                                if(ret < 0) {
                                        vin_err("vin_power_ctrl fail\n");
                                        return -HB_CAM_SENSOR_POWEROFF_FAIL;
                                }
                        }
                }
        }
        return ret;
}

init

  1. Writes the sensor initialization register list to the sensor via the I2C write function; different resolutions use different setting arrays

  2. imx586_linear_data_init is used to populate turning_data information.

  3. sensor_init is called when hbn_camera_attach_to_vin is invoked

static int sensor_init(sensor_info_t *sensor_info)
{
        int ret = RET_OK;
        int setting_size = 0;

        ret = sensor_poweron(sensor_info);
        if (ret < 0) {
                vin_err("%d : sensor reset %s fail\n", __LINE__, sensor_info->sensor_name);
                return ret;
        }
        switch(sensor_info->sensor_mode) {
        case NORMAL_M:
                vin_info("imx586 in normal/linear mode\n");
                vin_info("bus_num = %d, sensor_addr = 0x%0x, fps = %d\n",
                        sensor_info->bus_num, sensor_info->sensor_addr, sensor_info->fps);
                setting_size = sizeof(imx586_init_3840x2160_4lane_linear_setting) / sizeof(uint32_t) / 2;
                ret = vin_write_array(sensor_info->bus_num, sensor_info->sensor_addr, REG_WIDTH,
                        setting_size, imx586_init_3840x2160_4lane_linear_setting);
                if (ret < 0) {
                        vin_err("%d : init %s fail\n", __LINE__, sensor_info->sensor_name);
                        return -HB_CAM_I2C_WRITE_FAIL;
                }
                ret = imx586_linear_data_init(sensor_info);
                if (ret < 0) {
                        vin_err("%d : linear data init %s fail\n", __LINE__, sensor_info->sensor_name);
                        return -HB_CAM_INIT_FAIL;
                }
                break;
        default:
                vin_err("%d not support mode %d\n", __LINE__, sensor_info->sensor_mode);
                ret = -HB_CAM_INIT_FAIL;
                break;
        }
        vin_info("imx586 config success under %d mode\n", sensor_info->sensor_mode);
        return ret;
}

deinit

  1. deinit is typically called for sensor de-initialization; simply call power_off to power down.

  2. sensor_deinit is called when hbn_camera_destroy or hbn_camera_detach_from_vin is invoked.

static int sensor_deinit(sensor_info_t *sensor_info)
{
        int ret = RET_OK;
        ret = sensor_poweroff(sensor_info);
        if (ret < 0) {
                vin_err("%d : deinit %s fail\n", __LINE__, sensor_info->sensor_name);
                return ret;
        }
        return ret;
}

start

  1. start implements sending the stream-on register 0x0100 as 0x01 to make the sensor start streaming.

  2. sensor_start is called when the upper layer calls hbn_vflow_start.

../../_images/screenshot-20260210-204256.png

static int sensor_start(sensor_info_t *sensor_info)
{
        int ret = RET_OK;
        int setting_size = 0;

        setting_size = sizeof(imx586_stream_on_setting)/sizeof(uint32_t)/2;
        vin_info("%s start normal/linear mode\n", sensor_info->sensor_name);
        ret = vin_write_array(sensor_info->bus_num, sensor_info->sensor_addr, REG_WIDTH,
                setting_size, imx586_stream_on_setting);
        if(ret < 0) {
                vin_err("start %s fail\n", sensor_info->sensor_name);
                return -HB_CAM_I2C_WRITE_FAIL;
        }
        return ret;
}

stop

  1. stop implements sending the stream-off register 0x0100 as 0x00 to make the sensor stop streaming.

  2. sensor_stop is called when the upper layer calls hbn_vflow_stop

static int sensor_stop(sensor_info_t *sensor_info)
{
        int ret = RET_OK;
        int setting_size = 0;

        setting_size = sizeof(imx586_stream_off_setting) / sizeof(uint32_t) / 2;
        vin_info("%s sensor stop\n", sensor_info->sensor_name);
        ret = vin_write_array(sensor_info->bus_num, sensor_info->sensor_addr, REG_WIDTH,
                setting_size, imx586_stream_off_setting);
        if (ret < 0) {
                vin_err("start %s fail\n", sensor_info->sensor_name);
                return -HB_CAM_I2C_WRITE_FAIL;
        }
        return ret;
}

ISP Configuration and linear_data_init

../../_images/screenshot-20260210-150959.png

../../_images/screenshot-20260210-202708.png

../../_images/screenshot-20260210-222914.png

In imx586_linear_data_init, populate the tuning parameters:

  • Read VTS from sensor (registers 0x0340/0x0341), calculate lines_per_second;

  • exposure_time_max/min, analog_gain_max, digital_gain_max;

  • Populate bayer (sensor_data_bayer_fill, sensor_data_bits_fill);

  • Set stream_ctrl; copy again_lut from imx586_setting.h (imx586_gain_lut) to turning_data.normal.again_lut, then submit via ioctl.

Key parameter configuration:

  • lines_per_second = vts * sensor_info->fps

  • exposure_time_max = vts - 48 Documentation specifies FRM_LENGTH_LINES - 48, i.e., VTS - 48

  • analog_gain_max = 160 consistent with gain_lut maximum index, 32x corresponds to 160, refer to Gain index and gain ratio lookup table

  • digital_gain_max = 0 dgain unused, configure 0; if used, refer to analog_gain_max

  • exposure_time_min = 1

  • sensor_data_bayer_fill(..., 10, BAYER_START_R, BAYER_PATTERN_RGGB); sensor_data_bits_fill(..., 12), BAYER_START at position 0,0 is BAYER_START_R, BAYER_PATTERN is RGGB, raw10 format. sensor_data_bits_fill is only used for PWL mode; configure 12 here

aexp_gain_control

For Sony sensors, gain control generally uses a linear formula. Taking imx586 as an example, you need to derive the register value calculation formula based on the gain ratio. The specific method is as follows:

Reference documentation and gain_table.xlsx:

../../_images/screenshot-20260116-110906.png

../../_images/screenshot-20260116-103726.png

Formula parsing:

"0x" & DEC2HEX(1024 - 1024/B4, 0)

The IMX586 analog gain formula is: Analog Gain = 1024 / (1024 - X)

Where: Analog Gain is the gain ratio (e.g., 2.0x, 4.0x, etc.) X is the value to write to the register (range 112-1008)

Derivation process:

Analog Gain = 1024 / (1024 - X)
=> 1024 - X = 1024 / Analog Gain
=> X = 1024 - 1024 / Analog Gain
static int sensor_aexp_gain_control(hal_control_info_t *info, uint32_t mode, uint32_t *again, uint32_t *dgain, uint32_t gain_num)
{
        const uint16_t AGAIN_H = 0x0204;
        const uint16_t AGAIN_L = 0x0205;
        char again_reg_value_h = 0, again_reg_value_l = 0;
        int gain_index = 0;

        if (mode == NORMAL_M) {
                if (again[0] >= sizeof(imx586_gain_lut)/sizeof(uint32_t))
                        gain_index = sizeof(imx586_gain_lut)/sizeof(uint32_t) - 1;
                else
                        gain_index = again[0];

                again_reg_value_h = (imx586_gain_lut[gain_index] >> 8) & 0xFF;
                again_reg_value_l = (imx586_gain_lut[gain_index]) & 0xFF;

                vin_i2c_write8(info->bus_num, 16, info->sensor_addr, AGAIN_H, again_reg_value_h);
                vin_i2c_write8(info->bus_num, 16, info->sensor_addr, AGAIN_L, again_reg_value_l);
        } else {
                vin_err(" unsupport mode %d\n", mode);
        }
        return 0;
}

aexp_line_control

The aexp_line_control function primarily implements exposure control, responsible for:

  1. Receiving the exposure line count calculated by the ISP 3A algorithm

  2. Converting the exposure line count to sensor register values

  3. Configuring and sending exposure registers to the sensor via I2C

  4. The exposure registers here are 0x0202, 0x0203.

../../_images/screenshot-20260210-202303.png

static int sensor_aexp_line_control(hal_control_info_t *info, uint32_t mode, uint32_t *line, uint32_t line_num)
{
        const uint16_t EXP_LINE0 = 0x0202;
        const uint16_t EXP_LINE1 = 0x0203;
        char temp0 = 0, temp1 = 0;

        if (mode == NORMAL_M) {
                uint32_t sline = line[0];
                if (sline > 3063) sline = 3063;
                if (sline < 8) sline = 8;

                temp0 = (sline >> 8) & 0xFF;
                vin_i2c_write8(info->bus_num, 16, info->sensor_addr, EXP_LINE0, temp0);
                temp1 = (sline) & 0xFF;
                vin_i2c_write8(info->bus_num, 16, info->sensor_addr, EXP_LINE1, temp1);
        } else {
                vin_err(" unsupport mode %d\n", mode);
        }
        return 0;
}

userspace_control

  • sensor_userspace_control is used for Gain Control, Exposure Control, and AF Control switches.

  • When debugging with hbplayer to troubleshoot flicker or image abnormalities, you can enable/disable HAL_GAIN_CONTROL / HAL_LINE_CONTROL to confirm the issue.

static int sensor_userspace_control(uint32_t port, uint32_t *enable)
{
        vin_info("enable userspace gain control and line control\n");
        *enable = HAL_GAIN_CONTROL | HAL_LINE_CONTROL | HAL_AF_CONTROL;
        return 0;
}

Driver Deployment

  1. Compile with ./bd.sh hbre camsys/libcam.

  2. Copy out/deploy/hbre/lib/sensor/libimx586.so.1.0.0 to /usr/hobot/lib/sensor/ on the board.


5.17.5.3. App Configuration

  • In the path app/samples/platform_samples/vp_sensors/imx586/, add configuration for imx586.

  • Configure mipi (lane, datatype, mipiclk, linelength, framelength, settle), camera (name, addr, sensor_mode, gpio, fps, width, height, format), vin_node_attr (mipi_rx, cim_isp_flyby, etc.), vin_ichn_attr, vin_ochn_attr (wstride, etc.), isp_attr, isp_ichn_attr, isp_ochn_attr.

  • HTS/VTS can be read from registers 0x342/0x343 and 0x340/0x341, or obtained from the sensor FAE, or measured with an oscilloscope. For measurement methods, refer to MIPI parameter determination

  • mipiclk can generally be obtained from the sensor FAE. For calculation methods, refer to MIPI CLK

  • Other configurations are generic; refer to the Data Structures documentation for configuration.

vp_sensor configuration file: linear_3840x2160_raw10_30fps_4lane.c

  • Configuration file path: imx586/linear_3840x2160_raw10_30fps_4lane.c.

  • Configuration structure name: linear_3840x2160_raw10_30fps_4lane (must be registered in vp_sensor_config_list).

  • The current example uses RAW10 output; datatype/format is configured as 0x2B. Main parameters are as follows:

Configuration Item Parameter Name / Location Value Description
Top-level config_file "linear_3840x2160_raw10_30fps_4lane.c"
sensor_name "imx586-30fps-4lane"
chip_id_reg / chip_id 0x0016 / 0x0586
sensor_i2c_addr_list {0x1A}
support_sensor_mode {NORMAL_M}
MIPI lane 4
datatype RAW10 (0x2B)
fps 30
mipiclk 4500 (Mbps)
width / height 3840 / 2160 (4K UHD)
linelength / framelength 8976 / 3064 (matching sensor HTS/VTS)
settle 30
channel_num 1
stop_check_instart 1
Camera name / addr "imx586" / 0x1A
sensor_mode 1 (NORMAL_M)
format RAW10
gpio_enable_bit / gpio_level_bit 0x01 / 0x00
calib_lname "disable"
VIN mipi_rx 0
cim_isp_flyby 0 (offline)
hdr_mode NOT_HDR
mclk_freq (vin_attr_ex) 24000000
VIN Output wstride SENSOR_WIDTH * 2 (RAW10 is x2)
ISP Input input_mode DDR_MODE
sensor_mode ISP_NORMAL_M
crop x=0, y=0, w=3840, h=2160
input_fmt FRM_FMT_RAW
input_bit_width 10
ISP Output output_fmt FRM_FMT_NV12
output_bit_width 8
ddr_en 1

5.17.5.4. Program Verification

  1. get_vin_data: Confirm raw image has no severe color cast and frame rate matches configuration.

  2. get_isp_data: Prepare imx586_tuning.json (can copy a JSON with the same resolution and change sensor_name), confirm YUV is normal.

  3. hbplayer: Connect to the board to preview, see hbplayer and tuning_tool Usage Guide.

  4. tuning_tool: After connecting to hbplayer using tuning_tool, enter command e, configure Manual AE, set aGain and integrationTime. Check driver printout to verify registers 0x0204/0x0205, 0x0202/0x0203 match the spec. ISP gain limits can be configured in imx586_tuning.json for self-testing.

For common issues, see Camera Sensor FAQ.