123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649 |
- /*
- * sc2210_mipi.c
- *
- */
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <assert.h>
- #include <string.h>
- #include "isp_func_def.h"
- #include "isp_sensor_if.h"
- #include "sensor_ops.h"
- #include "clock_ops.h"
- #include "isp_enum.h"
- #include "mipi_api.h"
- #include "sc2210_mipi.h"
- #include "sensor.h"
- #ifndef OS_LINUX
- #include <rtthread.h>
- #endif
- static struct sns_cfg_s sns_cfg = {
- .inited = 0,
- .curr_exposure_ratio = 256, // 默认初始化为16倍
- .MinRstLine = 5,
- .minAGain = 0x40,
- };
- static int SetIntt(FH_UINT32 intt, FH_UINT8 fNo);
- static int GetIntt();
- static int SetGain(FH_UINT32 again, FH_UINT8 fNo);
- static int GetGain();
- static int SetExposureRatio(FH_UINT32 exposure_ratio);
- static int GetExposureRatio(FH_UINT32 *exposure_ratio);
- static int GetSensorViAttr(ISP_VI_ATTR_S *vi_attr)
- {
- if(vi_attr == NULL)
- return FH_ERR_NULL_POINTER;
- switch (sns_cfg.sensor_fmt)
- {
- case FORMAT_1080P25:
- vi_attr->u16WndWidth = FRAM_W_1080P25;
- vi_attr->u16WndHeight = FRAM_H_1080P25;
- vi_attr->u16InputWidth = ACTIVE_W_1080P25;
- vi_attr->u16InputHeight = ACTIVE_H_1080P25;
- vi_attr->u16PicWidth = PIC_IN_W_1080P25;
- vi_attr->u16PicHeight = PIC_IN_H_1080P25;
- vi_attr->u16OffsetX = OFFSET_X_1080P25;
- vi_attr->u16OffsetY = OFFSET_Y_1080P25;
- vi_attr->enBayerType = BAYER_BGGR;
- sns_cfg.curr_hsf = 25 * vi_attr->u16WndHeight;
- break;
- case FORMAT_1080P30:
- vi_attr->u16WndWidth = FRAM_W_1080P30;
- vi_attr->u16WndHeight = FRAM_H_1080P30;
- vi_attr->u16InputWidth = ACTIVE_W_1080P30;
- vi_attr->u16InputHeight = ACTIVE_H_1080P30;
- vi_attr->u16PicWidth = PIC_IN_W_1080P30;
- vi_attr->u16PicHeight = PIC_IN_H_1080P30;
- vi_attr->u16OffsetX = OFFSET_X_1080P30;
- vi_attr->u16OffsetY = OFFSET_Y_1080P30;
- vi_attr->enBayerType = BAYER_BGGR;
-
- sns_cfg.curr_hsf = 30 * vi_attr->u16WndHeight;
- break;
- case FORMAT_WDR_1080P25:
- vi_attr->u16WndWidth = FRAM_W_1080P25_WDR;
- vi_attr->u16WndHeight = FRAM_H_1080P25_WDR;
- vi_attr->u16InputWidth = ACTIVE_W_1080P25_WDR;
- vi_attr->u16InputHeight = ACTIVE_H_1080P25_WDR;
- vi_attr->u16PicWidth = PIC_IN_W_1080P25_WDR;
- vi_attr->u16PicHeight = PIC_IN_H_1080P25_WDR;
- vi_attr->u16OffsetX = OFFSET_X_1080P25_WDR;
- vi_attr->u16OffsetY = OFFSET_Y_1080P25_WDR;
- vi_attr->enBayerType = BAYER_BGGR;
-
- sns_cfg.curr_hsf = 25 * vi_attr->u16WndHeight;
- break;
- case FORMAT_WDR_1080P30:
- vi_attr->u16WndWidth = FRAM_W_1080P30_WDR;
- vi_attr->u16WndHeight = FRAM_H_1080P30_WDR;
- vi_attr->u16InputWidth = ACTIVE_W_1080P30_WDR;
- vi_attr->u16InputHeight = ACTIVE_H_1080P30_WDR;
- vi_attr->u16PicWidth = PIC_IN_W_1080P30_WDR;
- vi_attr->u16PicHeight = PIC_IN_H_1080P30_WDR;
- vi_attr->u16OffsetX = OFFSET_X_1080P30_WDR;
- vi_attr->u16OffsetY = OFFSET_Y_1080P30_WDR;
- vi_attr->enBayerType = BAYER_BGGR;
-
- sns_cfg.curr_hsf = 30 * vi_attr->u16WndHeight;
- break;
- default:
- break;
- }
- return 0;
- }
- static void SensorReset(void)
- {
- return;
- }
- static unsigned short sc2210_i2c_addr = 0x60;
- static int Sensor_Isconnect(void)
- {
- unsigned short pid1, pid2;
- unsigned short i2c_addr[4] = {0x60,0x62, 0x64,0x66};
- int i;
- for(i = 0; i < 4; i ++)
- {
- SensorDevice_Init((i2c_addr[i] >> 1), 2);
- pid1 = Sensor_Read(0x3107);
- if(pid1!=0x22)
- {
- SensorDevice_Close();
- continue;
- }
- pid2 = Sensor_Read(0x3108);
- SensorDevice_Close();
- if (pid1 == 0x22 && pid2 == 0x10)
- {
- sc2210_i2c_addr = i2c_addr[i];
- return 1;
- }
- else
- {
- continue;
- }
- }
- return 0;
- }
- static int Sensor_Init(void)
- {
- Sensor_Isconnect();
- SensorDevice_Init((sc2210_i2c_addr>>1), 2);
- SensorReset();
- sns_cfg.inited = 1;
- return 0;
- }
- static int Sensor_DeInit(void)
- {
- SensorReset();
- return SensorDevice_Close();
- }
- // GetGain仅在ae refresh时会重新获取,所以尽量直接从sensor端获取,保证intt的正确性
- static int GetGain()
- {
- return sns_cfg.curr_sensor_gain;
- }
- // GetIntt仅在ae refresh时会重新获取,所以尽量直接从sensor端获取,保证intt的正确性
- static int GetIntt(void)
- {
- return sns_cfg.curr_sensor_intt;
- }
- static int SetLaneNumMax(FH_UINT32 lane_num)
- {
- sns_cfg.maxLaneNum = lane_num;
- return 0;
- }
- static void SetSensorMipiCfg(void)
- {
- struct mipi_conf config;
- config.frq_range = R_750_799;
- config.sensor_mode = NOT_SONY_WDR_USE_WDR;
- config.raw_type = RAW10;
- // config.lf_vc_id = 0;
- // config.sf_vc_id = 1;
- // config.lane_num =2;
- if(!sns_cfg.wdr_flag)
- {
- config.lf_vc_id = 0xff;//0xff
- config.sf_vc_id = 0xff;//0xff
- config.lane_num =2;
- }
- else
- {
- config.lf_vc_id = 0;
- config.sf_vc_id = 2;
- config.lane_num =2;
- }
-
- mipi_init(&config);
- }
- static int SetSensorFmt(int format)
- {
- sns_cfg.sensor_fmt = format;
- sns_cfg.wdr_flag = (format >> 16)&1;
- SetSensorMipiCfg();
- if(sns_cfg.inited)
- {
- int i;
-
- printf("sc2210 ver20210701\n");
- switch(format)
- {
- case FORMAT_1080P25:
- printf("1080p25\n");
- for(i=0; i<sizeof(Sensor_Cfg_SC2210_MIPI_1080P25)/4; i++)
- {
- Sensor_Write(Sensor_Cfg_SC2210_MIPI_1080P25[2*i], Sensor_Cfg_SC2210_MIPI_1080P25[2*i+1]);
- }
- break;
- case FORMAT_1080P30:
- printf("1080p30\n");
- for(i=0; i<sizeof(Sensor_Cfg_SC2210_MIPI_1080P30)/4; i++)
- {
- Sensor_Write(Sensor_Cfg_SC2210_MIPI_1080P30[2*i], Sensor_Cfg_SC2210_MIPI_1080P30[2*i+1]);
- }
- break;
- case FORMAT_WDR_1080P25:
- printf("1080p25wdr\n");
- for(i=0; i<sizeof(Sensor_Cfg_SC2210_MIPI_1080P25_WDR)/4; i++)
- {
- Sensor_Write(Sensor_Cfg_SC2210_MIPI_1080P25_WDR[2*i], Sensor_Cfg_SC2210_MIPI_1080P25_WDR[2*i+1]);
- }
- break;
- case FORMAT_WDR_1080P30:
- printf("1080p30wdr\n");
- for(i=0; i<sizeof(Sensor_Cfg_SC2210_MIPI_1080P30_WDR)/4; i++)
- {
- Sensor_Write(Sensor_Cfg_SC2210_MIPI_1080P30_WDR[2*i], Sensor_Cfg_SC2210_MIPI_1080P30_WDR[2*i+1]);
- }
- break;
- default:
- printf("Format error!");
- return -1;
- break;
- }
- usleep(20000);
- }
- else
- {
- return -1;
- }
- sns_cfg.v_cycle = (Sensor_Read(0x320e) << 8) | (Sensor_Read(0x320f));
- sns_cfg.fullLineStd = sns_cfg.v_cycle;
- if (sns_cfg.wdr_flag == 0)
- {
- sns_cfg.MinRstLine = 5;
- }
- else
- {
- sns_cfg.MinRstLine = sns_cfg.v_cycle / 7 + 5;
- }
- sns_cfg.max_intt = sns_cfg.v_cycle - sns_cfg.MinRstLine;
- SetIntt(sns_cfg.max_intt, 0);
- SetExposureRatio(sns_cfg.curr_exposure_ratio);
- SetGain(sns_cfg.minAGain, 0);
- sns_cfg.curr_sensor_gain = sns_cfg.minAGain;
- return 0;
- }
- static int GetAEInfo(Sensor_AE_INFO_S *sensAEInfo)
- {
- sensAEInfo->currIntt = GetIntt();
- sensAEInfo->currAGain = GetGain();
- sensAEInfo->currHsf = sns_cfg.curr_hsf;
- sensAEInfo->currFrameH = sns_cfg.v_cycle;
- return 0;
- }
- static int GetAEDefault(Sensor_AE_Default_S *sensAEDefault)
- {
- sensAEDefault->minIntt = 1;
- sensAEDefault->maxIntt = sns_cfg.max_intt;
- sensAEDefault->minAGain = sns_cfg.minAGain;
- sensAEDefault->maxAGain = 0x7fff;
- sensAEDefault->fullLineStd = sns_cfg.fullLineStd;
- sensAEDefault->MinRstLine = sns_cfg.MinRstLine;
- return 0;
- }
- static int CalcSnsValidGain(FH_UINT32 *u32Gain)
- {
- return 0;
- }
- static int SetGain(FH_UINT32 again, FH_UINT8 fNo)
- {
- sns_cfg.curr_sensor_gain = again;
- unsigned char gain_0x3e06 = 0x00;
- unsigned char gain_0x3e07 = 0x80;
- unsigned char gain_0x3e08 = 0x03;
- unsigned char gain_0x3e09 = 0x40;
- //UINT32 gainShift = 0;
- //printf("again: 0x%x, ", again);
-
-
- if (again < 64 * 2) //again 1x-2x
- {
- gain_0x3e08 = 0x03;
- gain_0x3e09 = again;
- gain_0x3e06 = 0x0;
- gain_0x3e07 = 0x80;
- }
- else if (again <= 64 * 3.375) //again 2x-3.375x
- {
- gain_0x3e08 = 0x07;
- gain_0x3e09 = again / 2;
- gain_0x3e06 = 0x0;
- gain_0x3e07 = 0x80;
- }
- else if (again <= 64 * 3.375 * 2) //again 3.375x-6.812x
- {
- gain_0x3e08 = 0x23;
- gain_0x3e09 = again / 3.375;
- gain_0x3e06 = 0x0;
- gain_0x3e07 = 0x80;
- }
- else if (again <= 64 * 3.375 * 4) //again 6.812x-13.624x
- {
- gain_0x3e08 = 0x27;
- gain_0x3e09 = again / 3.375 / 2;
- gain_0x3e06 = 0x0;
- gain_0x3e07 = 0x80;
- }
- else if (again <= 64 * 3.375 * 8) //again 13.624x-27.248x
- {
- gain_0x3e08 = 0x2f;
- gain_0x3e09 = again / 3.375 / 4;
- gain_0x3e06 = 0x0;
- gain_0x3e07 = 0x80;
- }
- else if (again <= 64 * 3.375 * 16) //again 27.248x-54.07x
- {
- gain_0x3e08 = 0x3f;
- gain_0x3e09 = again / 3.375 / 8;
- gain_0x3e06 = 0x0;
- gain_0x3e07 = 0x80;
- }
- #if 1
- else if (again <= 64 * 3.375 * 16 * 2) //again 54.07x dgain 2x
- {
- gain_0x3e08 = 0x3f;
- gain_0x3e09 = 0x7f;
- gain_0x3e06 = 0x0;
- gain_0x3e07 = again / 3.375 / 8;
- }
- else if (again <= 64 * 3.375 * 16 * 4) //again 54.07x dgain 4x
- {
- gain_0x3e08 = 0x3f;
- gain_0x3e09 = 0x7f;
- gain_0x3e06 = 0x1;
- gain_0x3e07 = again / 3.375 / 16;
- }
- else if (again <= 64 * 3.375 * 16 * 8) //again 54.07x dgain 8x
- {
- gain_0x3e08 = 0x3f;
- gain_0x3e09 = 0x7f;
- gain_0x3e06 = 0x3;
- gain_0x3e07 = again / 3.375 / 32;
- }
- else if (again <= 64 * 3.375 * 16 * 16) //again 54.07x dgain 16x
- {
- gain_0x3e08 = 0x3f;
- gain_0x3e09 = 0x7f;
- gain_0x3e06 = 0x7;
- gain_0x3e07 = again / 3.375 / 64;
- }
- else if (again <= 64 * 3.375 * 16 * 32) //again 54.07x dgain 32x
- {
- gain_0x3e08 = 0x3f;
- gain_0x3e09 = 0x7f;
- gain_0x3e06 = 0xf;
- gain_0x3e07 = again / 3.375 / 128;
- }
- #endif
- // high frm GAIN
- Sensor_Write(0x3e06, gain_0x3e06);
- Sensor_Write(0x3e07, gain_0x3e07);
- Sensor_Write(0x3e08, gain_0x3e08);
- Sensor_Write(0x3e09, gain_0x3e09);
- // low frm GAIN
- Sensor_Write(0x3e10, gain_0x3e06);
- Sensor_Write(0x3e11, gain_0x3e07);
- Sensor_Write(0x3e12, gain_0x3e08);
- Sensor_Write(0x3e13, gain_0x3e09);
- return 0;
- }
- static int CalcSnsValidIntt(FH_UINT32 *u32Intt)
- {
- return 0;
- }
- static int SetIntt(FH_UINT32 intt, FH_UINT8 fNo)
- {
- //printf("intt=%d----",intt);
- if(intt<2)
- intt=2;
- sns_cfg.curr_sensor_intt = intt;
- //printf("curr_sensor_intt=%d\n",curr_sensor_intt);
- /*
- FH_SINT32 height1L,height1H,height1,height2L,height2H,height2;
- height1H = Sensor_Read(0x320e);
- height1L = Sensor_Read(0x320f);
- height1 = (height1H << 8) | height1L;
- height2H = Sensor_Read(0x3e23);
- height2L = Sensor_Read(0x3e24);
- height2 = (height2H << 8) | height2L;
- */
- FH_UINT8 exposure_3e00 = 0x00;
- FH_UINT8 exposure_3e01 = 0x00;
- FH_UINT8 exposure_3e02 = 0x00;
- /*
- if(wdr_flag==0)
- {
- if(intt > height1-4 )
- intt = height1-4;
- }
- else
- {
- if(intt > height1-height2-8 )
- intt = height1-height2-8 ;
- }
- */
- exposure_3e00 = (intt>>12) & 0x0f;
- exposure_3e01 = (intt &0xfff)>>4;
- exposure_3e02 = (intt&0xf)<<4;
- Sensor_Write(0x3e00, exposure_3e00);
- Sensor_Write(0x3e01, exposure_3e01);
- Sensor_Write(0x3e02, exposure_3e02);
- sns_cfg.curr_sensor_intt = intt;
- return 0;
- }
- static int SetSnsFrameH(FH_UINT32 frameH)
- {
- //if(sns_cfg.wdr_flag)
- // frame_height = (vi_attr.u16WndHeight )* multiple;
- // else
- // frame_height = (vi_attr.u16WndHeight/2 )* multiple;
- Sensor_Write(0x320e, (frameH>>8)&0xff);
- Sensor_Write(0x320f, frameH&0xff);
- sns_cfg.v_cycle = frameH;
- return 0;
- }
- static int SetExposureRatio(FH_UINT32 exposure_ratio)
- {
- FH_UINT32 s_intt;
- //FH_SINT32 s_3e23,s_3e24,sf_exp_limit;
- if(exposure_ratio == 0)
- return 0;
- //s_3e23=Sensor_Read(0x3e23);
- //s_3e24=Sensor_Read(0x3e24);
- //sf_exp_limit=(s_3e23<<8)|s_3e24;
-
- s_intt = MAX((((sns_cfg.curr_sensor_intt<<6)+(exposure_ratio>>1)) / exposure_ratio), 1);
-
- //printf("the s_intt =%d, %d\n",s_intt,s_intt%4);
- if(s_intt < 1)
- s_intt = 1;
- /*
- if(s_intt > (2 * sf_exp_limit -8))
- s_intt = 2 * sf_exp_limit -8;
- */
- Sensor_Write(0x3e04, (s_intt&0xfff)>>4);
- Sensor_Write(0x3e05, (s_intt&0xf)<<4);
- sns_cfg.curr_exposure_ratio = (sns_cfg.curr_sensor_intt<<4)/s_intt;
- return 0;
- }
- static int GetExposureRatio(FH_UINT32 *exposure_ratio)
- {
- *exposure_ratio = sns_cfg.curr_exposure_ratio;
- return 0;
- }
- /*
- static int Sensor_Kick()
- {
- if (sns_cfg.inited)
- {
- SensorDevice_Init((sc2210_i2c_addr>>1), 2); //0x64
- SensorReset();
- sns_cfg.inited = 1;
- }
- return 0;
- }
- */
- static int SetSensorReg(unsigned short addr,unsigned short data)
- {
- Sensor_Write(addr,data);
- return 0;
- }
- static int GetSensorReg(unsigned short addr, unsigned short *data)
- {
- *data = Sensor_Read(addr);
- return 0;
- }
- static int SetSensorFlipMirror(FH_UINT32 sensor_en_stat)
- {
- FH_UINT32 mirror,flip;
- mirror = (sensor_en_stat >>1)&0x1;
- flip = sensor_en_stat & 0x1;
- Sensor_Write(0x3221, ((flip?0x3:0x00)<<5) | ((mirror?0x3:0x00) <<1) );
- return 0;
- }
- static int GetSensorFlipMirror(FH_UINT32 *sensor_en_stat)
- {
- FH_UINT32 mirror,flip,tmp;
- tmp = Sensor_Read(0x3221);
- mirror = (tmp>>1) & 0x1;
- flip = (tmp>>5) &0x01 ;
- *sensor_en_stat = (mirror<<1)|flip;
- return 0;
- }
- static int GetSensorAttribute(char *name, FH_UINT32 *value)
- {
- #ifdef OS_LINUX
- if (strcmp(name, "WDR") == 0) {
- #else
- if (rt_strcmp(name, "WDR") == 0) {
- #endif
- *value = sns_cfg.wdr_flag;
- return 0;
- }
- return -1;
- }
- static int SensorCommonIf(FH_UINT32 cmd, ISP_SENSOR_COMMON_CMD_DATA0* data0, ISP_SENSOR_COMMON_CMD_DATA1* data1)
- {
- int ret = -1;
- return ret;
- }
- static FH_UINT32* GetMirrorFlipBayerFormat(void) {
- return NAME(MirrorFlipBayerFormat);
- }
- static FH_UINT32* GetUserSensorAwbGain(FH_UINT32 idx) {return 0;}
- static FH_UINT32* GetSensorLtmCurve(FH_UINT32 idx) {
- return 0;
- }
- /***********************adv api related end*************************************/
- /***********************not used for now start**********************************/
- static int SetSensorAwbGain(FH_UINT32 *awb_gain){return 0;}
- static int GetSensorAwbGain(FH_UINT32 *awb_gain){return 0;}
- static int SetSensorIris(FH_UINT32 iris){return 0;}
- #define SENSOR_SC2210_MIPI "sc2210_mipi"
- struct isp_sensor_if sc2210_mipi_sensor_if;
- /***********************not used for now end************************************/
- #ifdef OS_RTT
- struct isp_sensor_if sc2210_mipi_sensor_if __attribute__((section(".sensor_drv"))) = {
- #else
- struct isp_sensor_if sc2210_mipi_sensor_if = {
- #endif
- .name = SENSOR_SC2210_MIPI,
- .get_vi_attr = GetSensorViAttr,
- .set_flipmirror = SetSensorFlipMirror,
- .get_flipmirror = GetSensorFlipMirror,
- .set_iris = SetSensorIris,
- .init = Sensor_Init,
- .reset = SensorReset,
- .deinit = Sensor_DeInit,
- .set_sns_fmt = SetSensorFmt,
- //.kick = Sensor_Kick,
- .set_sns_reg = SetSensorReg,
- .set_exposure_ratio = SetExposureRatio,
- .get_exposure_ratio = GetExposureRatio,
- .get_sensor_attribute = GetSensorAttribute,
- .set_lane_num_max = SetLaneNumMax,
- .get_sns_reg = GetSensorReg,
- .get_awb_gain = GetSensorAwbGain,
- .set_awb_gain = SetSensorAwbGain,
- .data = 0,
- .common_if = SensorCommonIf,
- .get_sns_ae_default = GetAEDefault,
- .get_sns_ae_info = GetAEInfo,
- .set_sns_intt = SetIntt,
- .calc_sns_valid_intt = CalcSnsValidIntt,
- .set_sns_gain = SetGain,
- .calc_sns_valid_gain = CalcSnsValidGain,
- .set_sns_frame_height = SetSnsFrameH,
- .get_sensor_mirror_flip_bayer_format = GetMirrorFlipBayerFormat,
- .get_user_awb_gain = GetUserSensorAwbGain,
- .get_user_ltm_curve = GetSensorLtmCurve,
- .is_sensor_connect = Sensor_Isconnect,
- };
- #ifdef OS_RTT
- #else
- struct isp_sensor_if* NAME(Sensor_Create)() {
- return &sc2210_mipi_sensor_if;
- }
- void NAME(Sensor_Destroy)(struct isp_sensor_if* s_if){
- memset(&sc2210_mipi_sensor_if, 0, sizeof(struct isp_sensor_if));
- }
- #endif
|