OV12870.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890
  1. #include <ebase/types.h>
  2. #include <ebase/trace.h>
  3. #include <ebase/builtins.h>
  4. #include <common/return_codes.h>
  5. #include <common/misc.h>
  6. #include <sys/ioctl.h>
  7. #include <fcntl.h>
  8. #include <math.h>
  9. #include <sys/types.h>
  10. #include <isi/isi.h>
  11. #include <isi/isi_iss.h>
  12. #include <isi/isi_priv.h>
  13. #include "vvsensor.h"
  14. #include "OV12870_priv.h"
  15. #include "ov12870.h"
  16. CREATE_TRACER( OV12870_INFO , "OV12870: ", INFO, 0);
  17. CREATE_TRACER( OV12870_WARN , "OV12870: ", WARNING, 1);
  18. CREATE_TRACER( OV12870_ERROR, "OV12870: ", ERROR, 1);
  19. CREATE_TRACER( OV12870_DEBUG, "OV12870: ", INFO, 1);
  20. CREATE_TRACER( OV12870_REG_INFO , "OV12870: ", INFO, 1);
  21. CREATE_TRACER( OV12870_REG_DEBUG, "OV12870: ", INFO, 1);
  22. #ifdef SUBDEV_V4L2
  23. #include <sys/ioctl.h>
  24. #include <sys/mman.h>
  25. #include <fcntl.h>
  26. #include <linux/videodev2.h>
  27. #include <linux/v4l2-subdev.h>
  28. #undef TRACE
  29. #define TRACE(x, ...)
  30. #endif
  31. #define OV12870_MIN_GAIN_STEP ( 1.0f/16.0f ) /**< min gain step size used by GUI (hardware min = 1/16; 1/16..32/16 depending on actual gain ) */
  32. #define OV12870_MAX_GAIN_AEC ( 32.0f ) /**< max. gain used by the AEC (arbitrarily chosen, hardware limit = 62.0, driver limit = 32.0 ) */
  33. #define OV12870_VS_MAX_INTEGRATION_TIME (0.0018)
  34. /*****************************************************************************
  35. *Sensor Info
  36. *****************************************************************************/
  37. static const char SensorName[16] = "OV12870";
  38. static struct vvcam_mode_info pov12870_mode_info[] = {
  39. {
  40. .index = 0,
  41. .width = 640,
  42. .height = 480,
  43. .fps = 30,
  44. .hdr_mode = SENSOR_MODE_LINEAR,
  45. .bit_width = 12,
  46. .bayer_pattern = BAYER_BGGR,
  47. .mipi_phy_freq = 400, //mbps
  48. .mipi_line_num = 4,
  49. .config_file_3a = "OV12870_640x480_raw12", //3aconfig_OV12870_640x480_raw12.json
  50. .preg_data = (void *)"ov12870 sensor liner mode, raw12, 400mbps(sensor clk 24m), img resolution is 640*480",
  51. //pclk 90mhz
  52. },
  53. {
  54. .index = 1,
  55. .width = 1920,
  56. .height = 1080,
  57. .fps = 30,
  58. .hdr_mode = SENSOR_MODE_LINEAR,
  59. .bit_width = 12,
  60. .bayer_pattern = BAYER_BGGR,
  61. //.mipi_phy_freq = 1200, //mbps
  62. .mipi_phy_freq = 400, //mbps
  63. .mipi_line_num = 4,
  64. .config_file_3a = "OV12870_1920x1080_raw12", //3aconfig_OV12870_1920x1080_raw12.json
  65. .preg_data = (void *)"ov12870 sensor liner mode, raw12, 1200mbps(sensor clk 24m), img resolution is 1280*1080",
  66. //pclk 180mhz
  67. },
  68. {
  69. .index = 2,
  70. .width = 4096,
  71. .height = 3072,
  72. .fps = 30,
  73. .hdr_mode = SENSOR_MODE_LINEAR,
  74. .bit_width = 10,
  75. .bayer_pattern = BAYER_BGGR,
  76. .mipi_phy_freq = 1200, //mbps
  77. .mipi_line_num = 4,
  78. .config_file_3a = "OV12870_4096x3072_raw12", //3aconfig_OV12870_4096x3072_raw12.json
  79. .preg_data = (void *)"ov12870 sensor liner mode, raw12, 1200mbps(sensor clk 24m), img resolution is 4096*3072",
  80. //pclk 180mhz
  81. },
  82. };
  83. static RESULT OV12870_IsiRegisterWriteIss(IsiSensorHandle_t handle, const uint32_t address, const uint32_t value);
  84. long __ov12870_set_exposure(IsiSensorHandle_t handle, int coarse_itg,
  85. int gain, int digitgain, OV12870_EXPOSURE_SETTING_t type)
  86. {
  87. return 0;
  88. }
  89. static RESULT OV12870_IsiSensorSetPowerIss(IsiSensorHandle_t handle, bool_t on) {
  90. RESULT result = RET_SUCCESS;
  91. int ret = 0;
  92. OV12870_Context_t *pOV12870Ctx = (OV12870_Context_t *) handle;
  93. if (pOV12870Ctx == NULL || pOV12870Ctx->IsiCtx.HalHandle == NULL) {
  94. return RET_NULL_POINTER;
  95. }
  96. HalContext_t *pHalCtx = (HalContext_t *) pOV12870Ctx->IsiCtx.HalHandle;
  97. TRACE(OV12870_INFO, "%s (enter)\n", __func__);
  98. int32_t enable = on;
  99. ret = ioctl(pHalCtx->sensor_fd, VVSENSORIOC_S_POWER, &enable);
  100. if (ret != 0) {
  101. // to do
  102. //TRACE(OV12870_ERROR, "%s: sensor set power error!\n", __func__);
  103. //return (RET_FAILURE);
  104. }
  105. TRACE(OV12870_INFO, "%s (exit)\n", __func__);
  106. return (result);
  107. }
  108. static RESULT OV12870_IsiResetSensorIss(IsiSensorHandle_t handle) {
  109. RESULT result = RET_SUCCESS;
  110. int ret = 0;
  111. TRACE(OV12870_INFO, "%s (enter)\n", __func__);
  112. OV12870_Context_t *pOV12870Ctx = (OV12870_Context_t *) handle;
  113. if (pOV12870Ctx == NULL || pOV12870Ctx->IsiCtx.HalHandle == NULL) {
  114. return RET_NULL_POINTER;
  115. }
  116. HalContext_t *pHalCtx = (HalContext_t *) pOV12870Ctx->IsiCtx.HalHandle;
  117. ret = ioctl(pHalCtx->sensor_fd, VVSENSORIOC_RESET, NULL);
  118. if (ret != 0) {
  119. TRACE(OV12870_ERROR, "%s: sensor reset error!\n", __func__);
  120. return (RET_FAILURE);
  121. }
  122. sleep(0.2);
  123. ret = OV12870_IsiRegisterWriteIss(handle, 0x301e, 0x0);
  124. if (ret != 0) {
  125. TRACE(OV12870_ERROR, "%s: sensor reset error!\n", __func__);
  126. return (RET_FAILURE);
  127. }
  128. ret = OV12870_IsiRegisterWriteIss(handle, 0x103, 1);
  129. if (ret != 0) {
  130. TRACE(OV12870_ERROR, "%s: sensor reset error!\n", __func__);
  131. return (RET_FAILURE);
  132. }
  133. sleep(0.2);
  134. TRACE(OV12870_INFO, "%s (exit)\n", __func__);
  135. return (result);
  136. }
  137. #ifdef SUBDEV_CHAR
  138. static RESULT OV12870_IsiSensorSetClkIss(IsiSensorHandle_t handle, uint32_t clk) {
  139. RESULT result = RET_SUCCESS;
  140. int32_t ret = 0;
  141. OV12870_Context_t *pOV12870Ctx = (OV12870_Context_t *) handle;
  142. if (pOV12870Ctx == NULL || pOV12870Ctx->IsiCtx.HalHandle == NULL) {
  143. return RET_NULL_POINTER;
  144. }
  145. HalContext_t *pHalCtx = (HalContext_t *) pOV12870Ctx->IsiCtx.HalHandle;
  146. TRACE(OV12870_INFO, "%s (enter)\n", __func__);
  147. ret = ioctl(pHalCtx->sensor_fd, VVSENSORIOC_S_CLK, &clk);
  148. if (ret != 0) {
  149. // to do
  150. //TRACE(OV12870_ERROR, "%s: sensor set clk error!\n", __func__);
  151. //return (RET_FAILURE);
  152. }
  153. TRACE(OV12870_INFO, "%s (exit)\n", __func__);
  154. return (result);
  155. }
  156. static RESULT OV12870_IsiSensorGetClkIss
  157. (IsiSensorHandle_t handle, uint32_t * pclk) {
  158. RESULT result = RET_SUCCESS;
  159. int ret = 0;
  160. OV12870_Context_t *pOV12870Ctx = (OV12870_Context_t *) handle;
  161. if (pOV12870Ctx == NULL || pOV12870Ctx->IsiCtx.HalHandle == NULL) {
  162. return RET_NULL_POINTER;
  163. }
  164. HalContext_t *pHalCtx = (HalContext_t *) pOV12870Ctx->IsiCtx.HalHandle;
  165. TRACE(OV12870_INFO, "%s (enter)\n", __func__);
  166. ret = ioctl(pHalCtx->sensor_fd, VVSENSORIOC_G_CLK, pclk);
  167. if (ret != 0) {
  168. // to do
  169. //TRACE(OV12870_ERROR, "%s: sensor get clk error!\n", __func__);
  170. //return (RET_FAILURE);
  171. }
  172. TRACE(OV12870_INFO, "%s (exit)\n", __func__);
  173. return (result);
  174. }
  175. static RESULT OV12870_IsiConfigSensorSCCBIss(IsiSensorHandle_t handle)
  176. {
  177. int ret = 0;
  178. TRACE(OV12870_INFO, "%s (enter)\n", __func__);
  179. return RET_SUCCESS;
  180. OV12870_Context_t *pOV12870Ctx = (OV12870_Context_t *) handle;
  181. if (pOV12870Ctx == NULL || pOV12870Ctx->IsiCtx.HalHandle == NULL) {
  182. return RET_NULL_POINTER;
  183. }
  184. HalContext_t *pHalCtx = (HalContext_t *) pOV12870Ctx->IsiCtx.HalHandle;
  185. static const IsiSccbInfo_t SensorSccbInfo = {
  186. .slave_addr = (0x10), //0x30 or 0x32
  187. .addr_byte = 2,
  188. .data_byte = 1,
  189. };
  190. struct vvcam_sccb_cfg_s sensor_sccb_config;
  191. sensor_sccb_config.slave_addr = SensorSccbInfo.slave_addr;
  192. sensor_sccb_config.addr_byte = SensorSccbInfo.addr_byte;
  193. sensor_sccb_config.data_byte = SensorSccbInfo.data_byte;
  194. ret = ioctl(pHalCtx->sensor_fd, VVSENSORIOC_SENSOR_SCCB_CFG,
  195. &sensor_sccb_config);
  196. if (ret != 0) {
  197. TRACE(OV12870_ERROR, "%s: sensor config sccb info error!\n",
  198. __func__);
  199. return (RET_FAILURE);
  200. }
  201. TRACE(OV12870_INFO, "%s (exit)\n", __func__);
  202. return RET_SUCCESS;
  203. }
  204. #endif
  205. static RESULT OV12870_IsiRegisterReadIss
  206. (IsiSensorHandle_t handle, const uint32_t address, uint32_t * p_value) {
  207. RESULT result = RET_SUCCESS;
  208. int32_t ret = 0;
  209. TRACE(OV12870_INFO, "%s (enter)\n", __func__);
  210. OV12870_Context_t *pOV12870Ctx = (OV12870_Context_t *) handle;
  211. if (pOV12870Ctx == NULL || pOV12870Ctx->IsiCtx.HalHandle == NULL) {
  212. return RET_NULL_POINTER;
  213. }
  214. HalContext_t *pHalCtx = (HalContext_t *) pOV12870Ctx->IsiCtx.HalHandle;
  215. struct vvcam_sccb_data sccb_data;
  216. sccb_data.addr = address;
  217. sccb_data.data = 0;
  218. ret = ioctl(pHalCtx->sensor_fd, VVSENSORIOC_READ_REG, &sccb_data);
  219. if (ret != 0) {
  220. TRACE(OV12870_ERROR, "%s: read sensor register error!\n",
  221. __func__);
  222. return (RET_FAILURE);
  223. }
  224. *p_value = sccb_data.data;
  225. TRACE(OV12870_INFO, "%s (exit) result = %d\n", __func__, result);
  226. return (result);
  227. }
  228. static RESULT OV12870_IsiRegisterWriteIss
  229. (IsiSensorHandle_t handle, const uint32_t address, const uint32_t value) {
  230. RESULT result = RET_SUCCESS;
  231. int ret = 0;
  232. TRACE(OV12870_INFO, "%s (enter)\n", __func__);
  233. OV12870_Context_t *pOV12870Ctx = (OV12870_Context_t *) handle;
  234. if (pOV12870Ctx == NULL || pOV12870Ctx->IsiCtx.HalHandle == NULL) {
  235. return RET_NULL_POINTER;
  236. }
  237. HalContext_t *pHalCtx = (HalContext_t *) pOV12870Ctx->IsiCtx.HalHandle;
  238. struct vvcam_sccb_data sccb_data;
  239. sccb_data.addr = address;
  240. sccb_data.data = value;
  241. ret = ioctl(pHalCtx->sensor_fd, VVSENSORIOC_WRITE_REG, &sccb_data);
  242. if (ret != 0) {
  243. TRACE(OV12870_ERROR, "%s: write sensor register error!\n",
  244. __func__);
  245. return (RET_FAILURE);
  246. }
  247. TRACE(OV12870_INFO, "%s (exit) result = %d\n", __func__, result);
  248. return (result);
  249. }
  250. static RESULT OV12870_IsiQuerySensorSupportIss(HalHandle_t HalHandle, vvcam_mode_info_array_t *pSensorSupportInfo)
  251. {
  252. //int ret = 0;
  253. struct vvcam_mode_info_array *psensor_mode_info_arry;
  254. HalContext_t *pHalCtx = HalHandle;
  255. if ( pHalCtx == NULL ) {
  256. return RET_NULL_POINTER;
  257. }
  258. psensor_mode_info_arry = pSensorSupportInfo;
  259. psensor_mode_info_arry->count = sizeof(pov12870_mode_info) / sizeof(struct vvcam_mode_info);
  260. memcpy(psensor_mode_info_arry->modes, pov12870_mode_info, sizeof(pov12870_mode_info));
  261. return RET_SUCCESS;
  262. }
  263. static RESULT OV12870_IsiQuerySensorIss(IsiSensorHandle_t handle, vvcam_mode_info_array_t *pSensorInfo)
  264. {
  265. RESULT result = RET_SUCCESS;
  266. OV12870_Context_t *pOV12870Ctx = (OV12870_Context_t *) handle;
  267. if (pOV12870Ctx == NULL || pOV12870Ctx->IsiCtx.HalHandle == NULL) {
  268. return RET_NULL_POINTER;
  269. }
  270. HalContext_t *pHalCtx = (HalContext_t *) pOV12870Ctx->IsiCtx.HalHandle;
  271. OV12870_IsiQuerySensorSupportIss(pHalCtx,pSensorInfo);
  272. return result;
  273. }
  274. static RESULT OV12870_IsiGetSensorModeIss(IsiSensorHandle_t handle,void *mode)
  275. {
  276. OV12870_Context_t *pOV12870Ctx = (OV12870_Context_t *) handle;
  277. if (pOV12870Ctx == NULL) {
  278. return (RET_WRONG_HANDLE);
  279. }
  280. memcpy(mode,&(pOV12870Ctx->SensorMode), sizeof(pOV12870Ctx->SensorMode));
  281. return ( RET_SUCCESS );
  282. }
  283. static RESULT OV12870_IsiCreateSensorIss(IsiSensorInstanceConfig_t * pConfig) {
  284. RESULT result = RET_SUCCESS;
  285. OV12870_Context_t *pOV12870Ctx;
  286. TRACE(OV12870_INFO, "%s (enter)\n", __func__);
  287. if (!pConfig || !pConfig->pSensor)
  288. return (RET_NULL_POINTER);
  289. pOV12870Ctx = (OV12870_Context_t *) malloc(sizeof(OV12870_Context_t));
  290. if (!pOV12870Ctx) {
  291. TRACE(OV12870_ERROR, "%s: Can't allocate ov12870 context\n",
  292. __func__);
  293. return (RET_OUTOFMEM);
  294. }
  295. MEMSET(pOV12870Ctx, 0, sizeof(OV12870_Context_t));
  296. result = HalAddRef(pConfig->HalHandle);
  297. if (result != RET_SUCCESS) {
  298. free(pOV12870Ctx);
  299. return (result);
  300. }
  301. pOV12870Ctx->IsiCtx.HalHandle = pConfig->HalHandle;
  302. pOV12870Ctx->IsiCtx.pSensor = pConfig->pSensor;
  303. pOV12870Ctx->GroupHold = BOOL_FALSE;
  304. pOV12870Ctx->OldGain = 0;
  305. pOV12870Ctx->OldIntegrationTime = 0;
  306. pOV12870Ctx->Configured = BOOL_FALSE;
  307. pOV12870Ctx->Streaming = BOOL_FALSE;
  308. pOV12870Ctx->TestPattern = BOOL_FALSE;
  309. pOV12870Ctx->isAfpsRun = BOOL_FALSE;
  310. pOV12870Ctx->SensorMode.index = pConfig->SensorModeIndex;
  311. pConfig->hSensor = (IsiSensorHandle_t) pOV12870Ctx;
  312. #ifdef SUBDEV_CHAR
  313. struct vvcam_mode_info *SensorDefaultMode = NULL;
  314. for (int i=0; i < sizeof(pov12870_mode_info)/ sizeof(struct vvcam_mode_info); i++)
  315. {
  316. if (pov12870_mode_info[i].index == pOV12870Ctx->SensorMode.index)
  317. {
  318. SensorDefaultMode = &(pov12870_mode_info[i]);
  319. break;
  320. }
  321. }
  322. if (SensorDefaultMode != NULL)
  323. {
  324. strcpy(pOV12870Ctx->SensorRegCfgFile, get_vi_config_path());
  325. switch(SensorDefaultMode->index)
  326. {
  327. case 0:
  328. strcat(pOV12870Ctx->SensorRegCfgFile,
  329. "OV12870_mipi4lane_640x480_init.txt");
  330. break;
  331. case 1:
  332. strcat(pOV12870Ctx->SensorRegCfgFile,
  333. "OV12870_mipi4lane_1920x1080_1200_30f.txt");
  334. break;
  335. case 2:
  336. strcat(pOV12870Ctx->SensorRegCfgFile,
  337. "OV12870_mipi4lane_4096X3072_1200_30f_init.txt");
  338. break;
  339. default:
  340. return -1;
  341. }
  342. if (access(pOV12870Ctx->SensorRegCfgFile, F_OK) == 0) {
  343. pOV12870Ctx->KernelDriverFlag = 0;
  344. memcpy(&(pOV12870Ctx->SensorMode),SensorDefaultMode,sizeof(struct vvcam_mode_info));
  345. } else {
  346. pOV12870Ctx->KernelDriverFlag = 1;
  347. }
  348. }else
  349. {
  350. pOV12870Ctx->KernelDriverFlag = 1;
  351. }
  352. result = OV12870_IsiSensorSetPowerIss(pOV12870Ctx, BOOL_TRUE);
  353. system("echo 456 > /sys/class/gpio/export");
  354. system("echo out > /sys/class/gpio/gpio456/direction");
  355. system("echo 0 > /sys/class/gpio/gpio456/value");
  356. sleep(0.2);
  357. system("echo 1 > /sys/class/gpio/gpio456/value");
  358. sleep(0.2);
  359. system("echo 456 > /sys/class/gpio/unexport");
  360. RETURN_RESULT_IF_DIFFERENT(RET_SUCCESS, result);
  361. uint32_t SensorClkIn = 0;
  362. if (pOV12870Ctx->KernelDriverFlag) {
  363. result = OV12870_IsiSensorGetClkIss(pOV12870Ctx, &SensorClkIn);
  364. RETURN_RESULT_IF_DIFFERENT(RET_SUCCESS, result);
  365. }
  366. result = OV12870_IsiSensorSetClkIss(pOV12870Ctx, SensorClkIn);
  367. RETURN_RESULT_IF_DIFFERENT(RET_SUCCESS, result);
  368. result = OV12870_IsiResetSensorIss(pOV12870Ctx);
  369. RETURN_RESULT_IF_DIFFERENT(RET_SUCCESS, result);
  370. pOV12870Ctx->pattern = ISI_BPAT_BGBGGRGR;
  371. if (!pOV12870Ctx->KernelDriverFlag) {
  372. result = OV12870_IsiConfigSensorSCCBIss(pOV12870Ctx);
  373. RETURN_RESULT_IF_DIFFERENT(RET_SUCCESS, result);
  374. }
  375. #endif
  376. TRACE(OV12870_INFO, "%s (exit)\n", __func__);
  377. return (result);
  378. }
  379. static RESULT OV12870_IsiGetRegCfgIss(const char *registerFileName,
  380. struct vvcam_sccb_array *arry)
  381. {
  382. if (NULL == registerFileName) {
  383. TRACE(OV12870_ERROR, "%s:registerFileName is NULL\n", __func__);
  384. return (RET_NULL_POINTER);
  385. }
  386. #ifdef SUBDEV_CHAR
  387. FILE *fp = NULL;
  388. fp = fopen(registerFileName, "rb");
  389. if (!fp) {
  390. TRACE(OV12870_ERROR, "%s:load register file %s error!\n",
  391. __func__, registerFileName);
  392. return (RET_FAILURE);
  393. }
  394. char LineBuf[512];
  395. uint32_t FileTotalLine = 0;
  396. while (!feof(fp)) {
  397. fgets(LineBuf, 512, fp);
  398. FileTotalLine++;
  399. }
  400. arry->sccb_data =
  401. malloc(FileTotalLine * sizeof(struct vvcam_sccb_data));
  402. if (arry->sccb_data == NULL) {
  403. TRACE(OV12870_ERROR, "%s:malloc failed NULL Point!\n", __func__,
  404. registerFileName);
  405. return (RET_FAILURE);
  406. }
  407. rewind(fp);
  408. arry->count = 0;
  409. while (!feof(fp)) {
  410. memset(LineBuf, 0, sizeof(LineBuf));
  411. fgets(LineBuf, 512, fp);
  412. int result =
  413. sscanf(LineBuf, "0x%x 0x%x",
  414. &(arry->sccb_data[arry->count].addr),
  415. &(arry->sccb_data[arry->count].data));
  416. if (result != 2)
  417. continue;
  418. arry->count++;
  419. }
  420. #endif
  421. return 0;
  422. }
  423. static RESULT OV12870_IsiInitSensorIss(IsiSensorHandle_t handle) {
  424. RESULT result = RET_SUCCESS;
  425. int ret = 0;
  426. OV12870_Context_t *pOV12870Ctx = (OV12870_Context_t *) handle;
  427. HalContext_t *pHalCtx = (HalContext_t *) pOV12870Ctx->IsiCtx.HalHandle;
  428. TRACE(OV12870_INFO, "%s (enter)\n", __func__);
  429. if (pOV12870Ctx == NULL) {
  430. return (RET_WRONG_HANDLE);
  431. }
  432. if (pOV12870Ctx->KernelDriverFlag) {
  433. ;
  434. } else {
  435. struct vvcam_sccb_array arry;
  436. result = OV12870_IsiGetRegCfgIss(pOV12870Ctx->SensorRegCfgFile, &arry);
  437. if (result != 0) {
  438. TRACE(OV12870_ERROR,
  439. "%s:OV12870_IsiGetRegCfgIss error!\n", __func__);
  440. return (RET_FAILURE);
  441. }
  442. ret = ioctl(pHalCtx->sensor_fd, VVSENSORIOC_WRITE_ARRAY, &arry);
  443. if (ret != 0) {
  444. TRACE(OV12870_ERROR, "%s:Sensor Write Reg arry error!\n",
  445. __func__);
  446. return (RET_FAILURE);
  447. }
  448. switch(pOV12870Ctx->SensorMode.index)
  449. {
  450. case 0:
  451. pOV12870Ctx->one_line_exp_time = 0.000001;
  452. pOV12870Ctx->FrameLengthLines = 480-36;
  453. pOV12870Ctx->CurFrameLengthLines = pOV12870Ctx->FrameLengthLines;
  454. pOV12870Ctx->MaxIntegrationLine = pOV12870Ctx->CurFrameLengthLines;
  455. pOV12870Ctx->MinIntegrationLine = 8;
  456. pOV12870Ctx->AecMaxGain = 0xffff;
  457. pOV12870Ctx->AecMinGain = 1;
  458. break;
  459. case 1:
  460. pOV12870Ctx->one_line_exp_time = 0.000001;
  461. pOV12870Ctx->FrameLengthLines = 2142;
  462. pOV12870Ctx->CurFrameLengthLines = pOV12870Ctx->FrameLengthLines;
  463. pOV12870Ctx->MaxIntegrationLine = pOV12870Ctx->CurFrameLengthLines;
  464. pOV12870Ctx->MinIntegrationLine = 8;
  465. pOV12870Ctx->AecMaxGain = 0xffff;
  466. pOV12870Ctx->AecMinGain = 1;
  467. break;
  468. case 2:
  469. break;
  470. default:
  471. return ( RET_NOTAVAILABLE );
  472. break;
  473. }
  474. pOV12870Ctx->AecIntegrationTimeIncrement = pOV12870Ctx->one_line_exp_time;
  475. pOV12870Ctx->AecMinIntegrationTime =
  476. pOV12870Ctx->one_line_exp_time * pOV12870Ctx->MinIntegrationLine;
  477. pOV12870Ctx->AecMaxIntegrationTime =
  478. pOV12870Ctx->one_line_exp_time * pOV12870Ctx->MaxIntegrationLine;
  479. pOV12870Ctx->MaxFps = pOV12870Ctx->SensorMode.fps;
  480. pOV12870Ctx->MinFps = 1;
  481. pOV12870Ctx->CurrFps = pOV12870Ctx->MaxFps;
  482. }
  483. TRACE(OV12870_INFO, "%s (exit)\n", __func__);
  484. return (result);
  485. }
  486. static RESULT OV12870_IsiReleaseSensorIss(IsiSensorHandle_t handle) {
  487. OV12870_Context_t *pOV12870Ctx = (OV12870_Context_t *) handle;
  488. RESULT result = RET_SUCCESS;
  489. TRACE(OV12870_INFO, "%s (enter)\n", __func__);
  490. if (pOV12870Ctx == NULL)
  491. return (RET_WRONG_HANDLE);
  492. (void)OV12870_IsiSensorSetStreamingIss(pOV12870Ctx, BOOL_FALSE);
  493. (void)OV12870_IsiSensorSetPowerIss(pOV12870Ctx, BOOL_FALSE);
  494. (void)HalDelRef(pOV12870Ctx->IsiCtx.HalHandle);
  495. MEMSET(pOV12870Ctx, 0, sizeof(OV12870_Context_t));
  496. free(pOV12870Ctx);
  497. TRACE(OV12870_INFO, "%s (exit)\n", __func__);
  498. return (result);
  499. }
  500. struct ov12870_fmt {
  501. int width;
  502. int height;
  503. int fps;
  504. };
  505. static RESULT OV12870_IsiSetupSensorIss
  506. (IsiSensorHandle_t handle, const IsiSensorConfig_t * pConfig) {
  507. OV12870_Context_t *pOV12870Ctx = (OV12870_Context_t *) handle;
  508. HalContext_t *pHalCtx = (HalContext_t *) pOV12870Ctx->IsiCtx.HalHandle;
  509. RESULT result = RET_SUCCESS;
  510. TRACE(OV12870_INFO, "%s: (enter)\n", __func__);
  511. if (!pOV12870Ctx) {
  512. TRACE(OV12870_ERROR,
  513. "%s: Invalid sensor handle (NULL pointer detected)\n",
  514. __func__);
  515. return (RET_WRONG_HANDLE);
  516. }
  517. if (!pConfig) {
  518. TRACE(OV12870_ERROR,
  519. "%s: Invalid configuration (NULL pointer detected)\n",
  520. __func__);
  521. return (RET_NULL_POINTER);
  522. }
  523. if (pOV12870Ctx->Streaming != BOOL_FALSE) {
  524. return RET_WRONG_STATE;
  525. }
  526. memcpy(&pOV12870Ctx->Config, pConfig, sizeof(IsiSensorConfig_t));
  527. /* 1.) SW reset of image sensor (via I2C register interface) be careful, bits 6..0 are reserved, reset bit is not sticky */
  528. TRACE(OV12870_DEBUG, "%s: OV12870 System-Reset executed\n", __func__);
  529. osSleep(100);
  530. //OV12870_AecSetModeParameters not defined yet as of 2021/8/9.
  531. //result = OV12870_AecSetModeParameters(pOV12870Ctx, pConfig);
  532. //if (result != RET_SUCCESS) {
  533. // TRACE(OV12870_ERROR, "%s: SetupOutputWindow failed.\n",
  534. // __func__);
  535. // return (result);
  536. //}
  537. #if 1
  538. struct ov12870_fmt fmt;
  539. fmt.width = pConfig->Resolution.width;
  540. fmt.height = pConfig->Resolution.height;
  541. ioctl(pHalCtx->sensor_fd, VVSENSORIOC_S_FPS, &fmt);//result = ioctl(pHalCtx->sensor_fd, VVSENSORIOC_S_FPS, &fmt);
  542. #endif
  543. pOV12870Ctx->Configured = BOOL_TRUE;
  544. TRACE(OV12870_INFO, "%s: (exit) ret=0x%x \n", __func__, result);
  545. return result;
  546. }
  547. static RESULT OV12870_IsiChangeSensorResolutionIss(IsiSensorHandle_t handle, uint16_t width, uint16_t height) {
  548. RESULT result = RET_SUCCESS;
  549. #if 0
  550. struct ov12870_fmt fmt;
  551. fmt.width = width;
  552. fmt.height = height;
  553. int ret = ioctl(pHalCtx->sensor_fd, VVSENSORIOC_S_FPS, &fmt);
  554. #endif
  555. TRACE(OV12870_INFO, "%s (exit)\n", __func__);
  556. return (result);
  557. }
  558. static RESULT OV12870_IsiSensorSetStreamingIss
  559. (IsiSensorHandle_t handle, bool_t on) {
  560. RESULT result = RET_SUCCESS;
  561. int ret = 0;
  562. TRACE(OV12870_INFO, "%s (enter)\n", __func__);
  563. OV12870_Context_t *pOV12870Ctx = (OV12870_Context_t *) handle;
  564. if (pOV12870Ctx == NULL || pOV12870Ctx->IsiCtx.HalHandle == NULL) {
  565. return RET_NULL_POINTER;
  566. }
  567. HalContext_t *pHalCtx = (HalContext_t *) pOV12870Ctx->IsiCtx.HalHandle;
  568. if (pOV12870Ctx->Configured != BOOL_TRUE)
  569. return RET_WRONG_STATE;
  570. int32_t enable = (uint32_t) on;
  571. ret = OV12870_IsiRegisterWriteIss(handle, 0x100, on);
  572. if (ret != 0) {
  573. return (RET_FAILURE);
  574. }
  575. pOV12870Ctx->Streaming = on;
  576. TRACE(OV12870_INFO, "%s (exit)\n", __func__);
  577. return (result);
  578. }
  579. static uint32_t sensor_get_chip_id(IsiSensorHandle_t handle, uint32_t *chip_id)
  580. {
  581. RESULT result = RET_SUCCESS;
  582. int32_t ret = 0;
  583. uint32_t id_val = 0;
  584. ret = OV12870_IsiRegisterReadIss(handle, 0x6000, &id_val);
  585. if (ret != 0) {
  586. TRACE(OV12870_ERROR,
  587. "%s: Read Sensor correct ID Error! \n", __func__);
  588. return (RET_FAILURE);
  589. }
  590. *chip_id |= id_val << 16;
  591. ret = OV12870_IsiRegisterReadIss(handle, 0x6001, &id_val);
  592. if (ret != 0) {
  593. TRACE(OV12870_ERROR,
  594. "%s: Read Sensor correct ID Error! \n", __func__);
  595. return (RET_FAILURE);
  596. }
  597. *chip_id |= id_val << 8;
  598. ret = OV12870_IsiRegisterReadIss(handle, 0x6002, &id_val);
  599. if (ret != 0) {
  600. TRACE(OV12870_ERROR,
  601. "%s: Read Sensor correct ID Error! \n", __func__);
  602. return (RET_FAILURE);
  603. }
  604. *chip_id |= id_val;
  605. return 0;
  606. }
  607. static RESULT OV12870_IsiCheckSensorConnectionIss(IsiSensorHandle_t handle) {
  608. RESULT result = RET_SUCCESS;
  609. int ret = 0;
  610. //uint32_t correct_id = 0x12870;
  611. uint32_t correct_id = 0x0;
  612. uint32_t sensor_id = 0;
  613. TRACE(OV12870_INFO, "%s (enter)\n", __func__);
  614. OV12870_Context_t *pOV12870Ctx = (OV12870_Context_t *) handle;
  615. if (pOV12870Ctx == NULL || pOV12870Ctx->IsiCtx.HalHandle == NULL) {
  616. return RET_NULL_POINTER;
  617. }
  618. HalContext_t *pHalCtx = (HalContext_t *) pOV12870Ctx->IsiCtx.HalHandle;
  619. ret = sensor_get_chip_id(handle, &sensor_id);
  620. if (ret != 0) {
  621. TRACE(OV12870_ERROR,
  622. "%s: Read Sensor chip ID Error! \n", __func__);
  623. return (RET_FAILURE);
  624. }
  625. if (correct_id != sensor_id) {
  626. TRACE(OV12870_ERROR, "%s:ChipID =0x%x sensor_id=%x error! \n",
  627. __func__, correct_id, sensor_id);
  628. return (RET_FAILURE);
  629. }
  630. TRACE(OV12870_INFO,
  631. "%s ChipID = 0x%08x, sensor_id = 0x%08x, success! \n", __func__,
  632. correct_id, sensor_id);
  633. TRACE(OV12870_INFO, "%s (exit)\n", __func__);
  634. return (result);
  635. }
  636. static RESULT OV12870_IsiGetSensorRevisionIss
  637. (IsiSensorHandle_t handle, uint32_t * p_value) {
  638. RESULT result = RET_SUCCESS;
  639. TRACE(OV12870_INFO, "%s (enter)\n", __func__);
  640. *p_value = 0x12870;
  641. TRACE(OV12870_INFO, "%s (exit)\n", __func__);
  642. return (result);
  643. }
  644. static RESULT OV12870_IsiGetGainLimitsIss
  645. (IsiSensorHandle_t handle, float *pMinGain, float *pMaxGain) {
  646. OV12870_Context_t *pOV12870Ctx = (OV12870_Context_t *) handle;
  647. RESULT result = RET_SUCCESS;
  648. /*TODO*/
  649. TRACE(OV12870_INFO, "%s: (enter)\n", __func__);
  650. if (pOV12870Ctx == NULL) {
  651. TRACE(OV12870_ERROR,
  652. "%s: Invalid sensor handle (NULL pointer detected)\n",
  653. __func__);
  654. return (RET_WRONG_HANDLE);
  655. }
  656. if ((pMinGain == NULL) || (pMaxGain == NULL)) {
  657. TRACE(OV12870_ERROR, "%s: NULL pointer received!!\n");
  658. return (RET_NULL_POINTER);
  659. }
  660. *pMinGain = pOV12870Ctx->AecMinGain;
  661. *pMaxGain = pOV12870Ctx->AecMaxGain;
  662. TRACE(OV12870_INFO, "%s: (enter)\n", __func__);
  663. return (result);
  664. }
  665. static RESULT OV12870_IsiGetIntegrationTimeLimitsIss
  666. (IsiSensorHandle_t handle,
  667. float *pMinIntegrationTime, float *pMaxIntegrationTime) {
  668. OV12870_Context_t *pOV12870Ctx = (OV12870_Context_t *) handle;
  669. RESULT result = RET_SUCCESS;
  670. /*TODO*/
  671. TRACE(OV12870_INFO, "%s: (enter)\n", __func__);
  672. if (pOV12870Ctx == NULL) {
  673. TRACE(OV12870_ERROR,
  674. "%s: Invalid sensor handle (NULL pointer detected)\n",
  675. __func__);
  676. return (RET_WRONG_HANDLE);
  677. }
  678. if ((pMinIntegrationTime == NULL) || (pMaxIntegrationTime == NULL)) {
  679. TRACE(OV12870_ERROR, "%s: NULL pointer received!!\n");
  680. return (RET_NULL_POINTER);
  681. }
  682. *pMinIntegrationTime = pOV12870Ctx->AecMinIntegrationTime;
  683. *pMaxIntegrationTime = pOV12870Ctx->AecMaxIntegrationTime;
  684. TRACE(OV12870_INFO, "%s: (enter)\n", __func__);
  685. return (result);
  686. }
  687. RESULT OV12870_IsiGetGainIss(IsiSensorHandle_t handle, float *pSetGain) {
  688. OV12870_Context_t *pOV12870Ctx = (OV12870_Context_t *) handle;
  689. RESULT result = RET_SUCCESS;
  690. TRACE(OV12870_INFO, "%s: (enter)\n", __func__);
  691. if (pOV12870Ctx == NULL) {
  692. TRACE(OV12870_ERROR,
  693. "%s: Invalid sensor handle (NULL pointer detected)\n",
  694. __func__);
  695. return (RET_WRONG_HANDLE);
  696. }
  697. if (pSetGain == NULL) {
  698. return (RET_NULL_POINTER);
  699. }
  700. *pSetGain = pOV12870Ctx->AecCurGain;
  701. TRACE(OV12870_INFO, "%s: (exit)\n", __func__);
  702. return (result);
  703. }
  704. RESULT OV12870_IsiGetLongGainIss(IsiSensorHandle_t handle, float *gain)
  705. {
  706. OV12870_Context_t *pOV12870Ctx = (OV12870_Context_t *) handle;
  707. TRACE(OV12870_INFO, "%s: (enter)\n", __func__);
  708. if (pOV12870Ctx == NULL) {
  709. TRACE(OV12870_ERROR,"%s: Invalid sensor handle (NULL pointer detected)\n",__func__);
  710. return (RET_WRONG_HANDLE);
  711. }
  712. if (gain == NULL) {
  713. return (RET_NULL_POINTER);
  714. }
  715. *gain = pOV12870Ctx->AecCurLongGain;
  716. TRACE(OV12870_INFO, "%s: (exit)\n", __func__);
  717. return (RET_SUCCESS);
  718. }
  719. RESULT OV12870_IsiGetVSGainIss(IsiSensorHandle_t handle, float *pSetGain) {
  720. OV12870_Context_t *pOV12870Ctx = (OV12870_Context_t *) handle;
  721. RESULT result = RET_SUCCESS;
  722. TRACE(OV12870_INFO, "%s: (enter)\n", __func__);
  723. if (pOV12870Ctx == NULL) {
  724. TRACE(OV12870_ERROR,
  725. "%s: Invalid sensor handle (NULL pointer detected)\n",
  726. __func__);
  727. return (RET_WRONG_HANDLE);
  728. }
  729. if (pSetGain == NULL) {
  730. return (RET_NULL_POINTER);
  731. }
  732. *pSetGain = pOV12870Ctx->AecCurVSGain;
  733. TRACE(OV12870_INFO, "%s: (exit)\n", __func__);
  734. return (result);
  735. }
  736. RESULT OV12870_IsiGetGainIncrementIss(IsiSensorHandle_t handle, float *pIncr) {
  737. OV12870_Context_t *pOV12870Ctx = (OV12870_Context_t *) handle;
  738. RESULT result = RET_SUCCESS;
  739. TRACE(OV12870_INFO, "%s: (enter)\n", __func__);
  740. if (pOV12870Ctx == NULL) {
  741. TRACE(OV12870_ERROR,
  742. "%s: Invalid sensor handle (NULL pointer detected)\n",
  743. __func__);
  744. return (RET_WRONG_HANDLE);
  745. }
  746. if (pIncr == NULL)
  747. return (RET_NULL_POINTER);
  748. *pIncr = pOV12870Ctx->AecGainIncrement;
  749. TRACE(OV12870_INFO, "%s: (exit)\n", __func__);
  750. return (result);
  751. }
  752. RESULT OV12870_IsiSetGainIss
  753. (IsiSensorHandle_t handle,
  754. float NewGain, float *pSetGain, float *hdr_ratio) {
  755. RESULT result = RET_SUCCESS;
  756. int32_t ret = 0;
  757. int TmpGain;
  758. OV12870_Context_t *pOV12870Ctx = (OV12870_Context_t *) handle;
  759. if (pOV12870Ctx == NULL || pOV12870Ctx->IsiCtx.HalHandle == NULL) {
  760. return RET_NULL_POINTER;
  761. }
  762. if (NewGain >= 25) { // More than 25 will not take effect
  763. NewGain = 25;
  764. }
  765. HalContext_t *pHalCtx = (HalContext_t *) pOV12870Ctx->IsiCtx.HalHandle;
  766. uint32_t SensorGain = 0;
  767. SensorGain = NewGain * pOV12870Ctx->gain_accuracy;
  768. /*TODO*/
  769. #if 0
  770. ret |= ioctl(pHalCtx->sensor_fd, VVSENSORIOC_S_GAIN, &SensorGain);
  771. if (ret != 0) {
  772. TRACE(OV12870_ERROR, "%s: set sensor gain error\n",
  773. __func__);
  774. return RET_FAILURE;
  775. }
  776. #endif
  777. pOV12870Ctx->AecCurGain = ((float)(NewGain));
  778. TmpGain = (int)NewGain;
  779. __ov12870_set_exposure(handle, 0,
  780. ((int)NewGain << 4) + round((NewGain - TmpGain) / 0.0625f), 0, OV12870_ANALOG_GAIN);
  781. *pSetGain = pOV12870Ctx->AecCurGain;
  782. TRACE(OV12870_DEBUG, "%s: g=%f\n", __func__, *pSetGain);
  783. return (result);
  784. }
  785. RESULT OV12870_IsiSetLongGainIss(IsiSensorHandle_t handle, float gain)
  786. {
  787. int ret = 0;
  788. TRACE(OV12870_INFO, "%s: (enter)\n", __func__);
  789. OV12870_Context_t *pOV12870Ctx = (OV12870_Context_t *) handle;
  790. if (!pOV12870Ctx || !pOV12870Ctx->IsiCtx.HalHandle)
  791. {
  792. TRACE(OV12870_ERROR,"%s: Invalid sensor handle (NULL pointer detected)\n",__func__);
  793. return (RET_WRONG_HANDLE);
  794. }
  795. HalContext_t *pHalCtx = (HalContext_t *) pOV12870Ctx->IsiCtx.HalHandle;
  796. uint32_t SensorGain = 0;
  797. SensorGain = gain * pOV12870Ctx->gain_accuracy;
  798. if (pOV12870Ctx->LastLongGain != SensorGain)
  799. {
  800. /*TODO*/
  801. #if 0
  802. ret = ioctl(pHalCtx->sensor_fd, VVSENSORIOC_S_LONG_GAIN, &SensorGain);
  803. if (ret != 0)
  804. {
  805. return (RET_FAILURE);
  806. TRACE(OV12870_ERROR,"%s: set long gain failed\n");
  807. }
  808. #endif
  809. pOV12870Ctx->LastLongGain = SensorGain;
  810. pOV12870Ctx->AecCurLongGain = gain;
  811. }
  812. TRACE(OV12870_INFO, "%s: (exit)\n", __func__);
  813. return (RET_SUCCESS);
  814. }
  815. RESULT OV12870_IsiSetVSGainIss
  816. (IsiSensorHandle_t handle,
  817. float NewIntegrationTime,
  818. float NewGain, float *pSetGain, float *hdr_ratio) {
  819. OV12870_Context_t *pOV12870Ctx = (OV12870_Context_t *) handle;
  820. HalContext_t *pHalCtx = (HalContext_t *) pOV12870Ctx->IsiCtx.HalHandle;
  821. RESULT result = RET_SUCCESS;
  822. #if 0
  823. float Gain = 0.0f;
  824. uint32_t ucGain = 0U;
  825. uint32_t again = 0U;
  826. #endif
  827. TRACE(OV12870_INFO, "%s: (enter)\n", __func__);
  828. if (!pOV12870Ctx) {
  829. TRACE(OV12870_ERROR,
  830. "%s: Invalid sensor handle (NULL pointer detected)\n",
  831. __func__);
  832. return (RET_WRONG_HANDLE);
  833. }
  834. if (!pSetGain || !hdr_ratio)
  835. return (RET_NULL_POINTER);
  836. uint32_t SensorGain = 0;
  837. SensorGain = NewGain * pOV12870Ctx->gain_accuracy;
  838. /*TODO*/
  839. //ioctl(pHalCtx->sensor_fd, VVSENSORIOC_S_VSGAIN, &SensorGain);
  840. pOV12870Ctx->AecCurVSGain = NewGain;
  841. *pSetGain = pOV12870Ctx->AecCurGain;
  842. TRACE(OV12870_DEBUG, "%s: g=%f\n", __func__, *pSetGain);
  843. TRACE(OV12870_INFO, "%s: (exit)\n", __func__);
  844. return (result);
  845. }
  846. RESULT OV12870_IsiSetBayerPattern(IsiSensorHandle_t handle, uint8_t pattern)
  847. {
  848. RESULT result = RET_SUCCESS;
  849. #if 0
  850. uint8_t h_shift = 0, v_shift = 0;
  851. uint32_t val_h = 0, val_l = 0;
  852. uint16_t val = 0;
  853. uint8_t Start_p = 0;
  854. bool_t streaming_status;
  855. TRACE(OV12870_INFO, "%s: (enter)\n", __func__);
  856. OV12870_Context_t *pOV12870Ctx = (OV12870_Context_t *) handle;
  857. if (pOV12870Ctx == NULL || pOV12870Ctx->IsiCtx.HalHandle == NULL) {
  858. return RET_NULL_POINTER;
  859. }
  860. // pattern 0:B 1:GB 2:GR 3:R
  861. streaming_status = pOV12870Ctx->Streaming;
  862. result = OV12870_IsiSensorSetStreamingIss(handle, 0);
  863. switch (pattern) {
  864. case BAYER_BGGR:
  865. Start_p = 0;
  866. break;
  867. case BAYER_GBRG:
  868. Start_p = 1;
  869. break;
  870. case BAYER_GRBG:
  871. Start_p = 2;
  872. break;
  873. case BAYER_RGGB:
  874. Start_p = 3;
  875. break;
  876. }
  877. h_shift = Start_p % 2;
  878. v_shift = Start_p / 2;
  879. OV12870_IsiRegisterReadIss(handle, 0x30a0, &val_h);
  880. OV12870_IsiRegisterReadIss(handle, 0x30a1, &val_l);
  881. val = (((val_h << 8) & 0xff00) | (val_l & 0x00ff)) + h_shift;
  882. val_h = (val >> 8) & 0xff;
  883. val_l = val & 0xff;
  884. OV12870_IsiRegisterWriteIss(handle, 0x30a0, (uint8_t)val_h);
  885. OV12870_IsiRegisterWriteIss(handle, 0x30a1, (uint8_t)val_l);
  886. OV12870_IsiRegisterReadIss(handle, 0x30a2, &val_h);
  887. OV12870_IsiRegisterReadIss(handle, 0x30a3, &val_l);
  888. val = (((val_h << 8) & 0xff00) | (val_l & 0x00ff)) + v_shift;
  889. val_h = (val >> 8) & 0xff;
  890. val_l = val & 0xff;
  891. OV12870_IsiRegisterWriteIss(handle, 0x30a2, (uint8_t)val_h);
  892. OV12870_IsiRegisterWriteIss(handle, 0x30a3, (uint8_t)val_l);
  893. OV12870_IsiRegisterReadIss(handle, 0x30a4, &val_h);
  894. OV12870_IsiRegisterReadIss(handle, 0x30a5, &val_l);
  895. val = (((val_h << 8) & 0xff00) | (val_l & 0x00ff)) + h_shift;
  896. val_h = (val >> 8) & 0xff;
  897. val_l = val & 0xff;
  898. OV12870_IsiRegisterWriteIss(handle, 0x30a4, (uint8_t)val_h);
  899. OV12870_IsiRegisterWriteIss(handle, 0x30a5, (uint8_t)val_l);
  900. OV12870_IsiRegisterReadIss(handle, 0x30a6, &val_h);
  901. OV12870_IsiRegisterReadIss(handle, 0x30a7, &val_l);
  902. val = (((val_h << 8) & 0xff00) | (val_l & 0x00ff)) + v_shift;
  903. val_h = (val >> 8) & 0xff;
  904. val_l = val & 0xff;
  905. OV12870_IsiRegisterWriteIss(handle, 0x30a6, (uint8_t)val_h);
  906. OV12870_IsiRegisterWriteIss(handle, 0x30a7, (uint8_t)val_l);
  907. pOV12870Ctx->pattern = pattern;
  908. result = OV12870_IsiSensorSetStreamingIss(handle, streaming_status);
  909. #endif
  910. return (result);
  911. }
  912. RESULT OV12870_IsiGetIntegrationTimeIss
  913. (IsiSensorHandle_t handle, float *pSetIntegrationTime)
  914. {
  915. OV12870_Context_t *pOV12870Ctx = (OV12870_Context_t *) handle;
  916. RESULT result = RET_SUCCESS;
  917. TRACE(OV12870_INFO, "%s: (enter)\n", __func__);
  918. if (!pOV12870Ctx) {
  919. TRACE(OV12870_ERROR,
  920. "%s: Invalid sensor handle (NULL pointer detected)\n",
  921. __func__);
  922. return (RET_WRONG_HANDLE);
  923. }
  924. if (!pSetIntegrationTime)
  925. return (RET_NULL_POINTER);
  926. *pSetIntegrationTime = pOV12870Ctx->AecCurIntegrationTime;
  927. TRACE(OV12870_INFO, "%s: (exit)\n", __func__);
  928. return (result);
  929. }
  930. RESULT OV12870_IsiGetLongIntegrationTimeIss(IsiSensorHandle_t handle, float *pIntegrationTime)
  931. {
  932. OV12870_Context_t *pOV12870Ctx = (OV12870_Context_t *) handle;
  933. TRACE(OV12870_INFO, "%s: (enter)\n", __func__);
  934. if (!pOV12870Ctx) {
  935. TRACE(OV12870_ERROR,"%s: Invalid sensor handle (NULL pointer detected)\n",__func__);
  936. return (RET_WRONG_HANDLE);
  937. }
  938. if (!pIntegrationTime)
  939. return (RET_NULL_POINTER);
  940. pOV12870Ctx->AecCurLongIntegrationTime = pOV12870Ctx->AecCurIntegrationTime;
  941. *pIntegrationTime = pOV12870Ctx->AecCurLongIntegrationTime;
  942. TRACE(OV12870_INFO, "%s: (exit)\n", __func__);
  943. return (RET_SUCCESS);
  944. }
  945. RESULT OV12870_IsiGetVSIntegrationTimeIss
  946. (IsiSensorHandle_t handle, float *pSetIntegrationTime)
  947. {
  948. OV12870_Context_t *pOV12870Ctx = (OV12870_Context_t *) handle;
  949. RESULT result = RET_SUCCESS;
  950. TRACE(OV12870_INFO, "%s: (enter)\n", __func__);
  951. if (!pOV12870Ctx) {
  952. TRACE(OV12870_ERROR,
  953. "%s: Invalid sensor handle (NULL pointer detected)\n",
  954. __func__);
  955. return (RET_WRONG_HANDLE);
  956. }
  957. if (!pSetIntegrationTime)
  958. return (RET_NULL_POINTER);
  959. *pSetIntegrationTime = pOV12870Ctx->AecCurVSIntegrationTime;
  960. TRACE(OV12870_INFO, "%s: (exit)\n", __func__);
  961. return (result);
  962. }
  963. RESULT OV12870_IsiGetIntegrationTimeIncrementIss
  964. (IsiSensorHandle_t handle, float *pIncr)
  965. {
  966. OV12870_Context_t *pOV12870Ctx = (OV12870_Context_t *) handle;
  967. RESULT result = RET_SUCCESS;
  968. TRACE(OV12870_INFO, "%s: (enter)\n", __func__);
  969. if (!pOV12870Ctx) {
  970. TRACE(OV12870_ERROR,
  971. "%s: Invalid sensor handle (NULL pointer detected)\n",
  972. __func__);
  973. return (RET_WRONG_HANDLE);
  974. }
  975. if (!pIncr)
  976. return (RET_NULL_POINTER);
  977. //_smallest_ increment the sensor/driver can handle (e.g. used for sliders in the application)
  978. *pIncr = pOV12870Ctx->AecIntegrationTimeIncrement;
  979. TRACE(OV12870_INFO, "%s: (exit)\n", __func__);
  980. return (result);
  981. }
  982. RESULT OV12870_IsiSetIntegrationTimeIss
  983. (IsiSensorHandle_t handle,
  984. float NewIntegrationTime,
  985. float *pSetIntegrationTime,
  986. uint8_t * pNumberOfFramesToSkip, float *hdr_ratio)
  987. {
  988. RESULT result = RET_SUCCESS;
  989. OV12870_Context_t *pOV12870Ctx = (OV12870_Context_t *) handle;
  990. HalContext_t *pHalCtx = (HalContext_t *) pOV12870Ctx->IsiCtx.HalHandle;
  991. uint32_t exp_line = 0;
  992. uint32_t exp_line_old = 0;
  993. TRACE(OV12870_INFO, "%s: (enter)\n", __func__);
  994. if (!pOV12870Ctx) {
  995. TRACE(OV12870_ERROR,
  996. "%s: Invalid sensor handle (NULL pointer detected)\n",
  997. __func__);
  998. return (RET_WRONG_HANDLE);
  999. }
  1000. if (!pSetIntegrationTime || !pNumberOfFramesToSkip) {
  1001. TRACE(OV12870_ERROR,
  1002. "%s: Invalid parameter (NULL pointer detected)\n",
  1003. __func__);
  1004. return (RET_NULL_POINTER);
  1005. }
  1006. exp_line = NewIntegrationTime / pOV12870Ctx->one_line_exp_time;
  1007. exp_line_old = exp_line;
  1008. exp_line =
  1009. MIN(pOV12870Ctx->MaxIntegrationLine,
  1010. MAX(pOV12870Ctx->MinIntegrationLine, exp_line));
  1011. TRACE(OV12870_DEBUG, "%s: set AEC_PK_EXPO=0x%05x\n", __func__, exp_line);
  1012. if (exp_line != pOV12870Ctx->OldIntegrationTime) {
  1013. /*TODO*/
  1014. //ioctl(pHalCtx->sensor_fd, VVSENSORIOC_S_EXP, &exp_line);
  1015. pOV12870Ctx->OldIntegrationTime = exp_line; // remember current integration time
  1016. pOV12870Ctx->AecCurIntegrationTime =
  1017. exp_line * pOV12870Ctx->one_line_exp_time;
  1018. *pNumberOfFramesToSkip = 1U; //skip 1 frame
  1019. } else {
  1020. *pNumberOfFramesToSkip = 0U; //no frame skip
  1021. }
  1022. uint32_t reg_h = (exp_line & 0xff00) >> 8;
  1023. uint32_t reg_l = (exp_line & 0xff);
  1024. int ret = OV12870_IsiRegisterWriteIss(handle, 0x3501, reg_h);
  1025. if (ret != 0) {
  1026. TRACE(OV12870_ERROR, "%s: sensor reset error!\n", __func__);
  1027. return (RET_FAILURE);
  1028. }
  1029. ret = OV12870_IsiRegisterWriteIss(handle, 0x3502, reg_l);
  1030. if (ret != 0) {
  1031. TRACE(OV12870_ERROR, "%s: sensor reset error!\n", __func__);
  1032. return (RET_FAILURE);
  1033. }
  1034. if (exp_line_old != exp_line) {
  1035. *pSetIntegrationTime = pOV12870Ctx->AecCurIntegrationTime;
  1036. } else {
  1037. *pSetIntegrationTime = NewIntegrationTime;
  1038. }
  1039. TRACE(OV12870_DEBUG, "%s: Ti=%f\n", __func__, *pSetIntegrationTime);
  1040. TRACE(OV12870_INFO, "%s: (exit)\n", __func__);
  1041. return (result);
  1042. }
  1043. RESULT OV12870_IsiSetLongIntegrationTimeIss(IsiSensorHandle_t handle,float IntegrationTime)
  1044. {
  1045. int ret;
  1046. TRACE(OV12870_INFO, "%s: (enter)\n", __func__);
  1047. OV12870_Context_t *pOV12870Ctx = (OV12870_Context_t *) handle;
  1048. if (!handle || !pOV12870Ctx->IsiCtx.HalHandle)
  1049. {
  1050. TRACE(OV12870_ERROR,"%s: Invalid sensor handle (NULL pointer detected)\n",__func__);
  1051. return (RET_WRONG_HANDLE);
  1052. }
  1053. HalContext_t *pHalCtx = (HalContext_t *) pOV12870Ctx->IsiCtx.HalHandle;
  1054. uint32_t exp_line = 0;
  1055. exp_line = IntegrationTime / pOV12870Ctx->one_line_exp_time;
  1056. exp_line = MIN(pOV12870Ctx->MaxIntegrationLine, MAX(pOV12870Ctx->MinIntegrationLine, exp_line));
  1057. if (exp_line != pOV12870Ctx->LastLongExpLine)
  1058. {
  1059. if (pOV12870Ctx->KernelDriverFlag)
  1060. {
  1061. ret = ioctl(pHalCtx->sensor_fd, VVSENSORIOC_S_LONG_EXP, &exp_line);
  1062. if (ret != 0)
  1063. {
  1064. TRACE(OV12870_ERROR,"%s: set long gain failed\n");
  1065. return RET_FAILURE;
  1066. }
  1067. }
  1068. pOV12870Ctx->LastLongExpLine = exp_line;
  1069. pOV12870Ctx->AecCurLongIntegrationTime = pOV12870Ctx->LastLongExpLine*pOV12870Ctx->one_line_exp_time;
  1070. }
  1071. TRACE(OV12870_INFO, "%s: (exit)\n", __func__);
  1072. return (RET_SUCCESS);
  1073. }
  1074. RESULT OV12870_IsiSetVSIntegrationTimeIss
  1075. (IsiSensorHandle_t handle,
  1076. float NewIntegrationTime,
  1077. float *pSetVSIntegrationTime,
  1078. uint8_t * pNumberOfFramesToSkip, float *hdr_ratio)
  1079. {
  1080. OV12870_Context_t *pOV12870Ctx = (OV12870_Context_t *) handle;
  1081. RESULT result = RET_SUCCESS;
  1082. uint32_t exp_line = 0;
  1083. TRACE(OV12870_INFO, "%s: (enter)\n", __func__);
  1084. if (!pOV12870Ctx) {
  1085. TRACE(OV12870_ERROR,
  1086. "%s: Invalid sensor handle (NULL pointer detected)\n",
  1087. __func__);
  1088. return (RET_WRONG_HANDLE);
  1089. }
  1090. if (!pSetVSIntegrationTime || !pNumberOfFramesToSkip) {
  1091. TRACE(OV12870_ERROR,
  1092. "%s: Invalid parameter (NULL pointer detected)\n",
  1093. __func__);
  1094. return (RET_NULL_POINTER);
  1095. }
  1096. TRACE(OV12870_INFO,
  1097. "%s: maxIntegrationTime-=%f minIntegrationTime = %f\n", __func__,
  1098. pOV12870Ctx->AecMaxIntegrationTime,
  1099. pOV12870Ctx->AecMinIntegrationTime);
  1100. exp_line = NewIntegrationTime / pOV12870Ctx->one_line_exp_time;
  1101. exp_line =
  1102. MIN(pOV12870Ctx->MaxIntegrationLine,
  1103. MAX(pOV12870Ctx->MinIntegrationLine, exp_line));
  1104. if (exp_line != pOV12870Ctx->OldVsIntegrationTime) {
  1105. /*TODO*/
  1106. // ioctl(pHalCtx->sensor_fd, VVSENSORIOC_S_VSEXP, &exp_line);
  1107. } else if (1){
  1108. pOV12870Ctx->OldVsIntegrationTime = exp_line;
  1109. pOV12870Ctx->AecCurVSIntegrationTime = exp_line * pOV12870Ctx->one_line_exp_time; //remember current integration time
  1110. *pNumberOfFramesToSkip = 1U; //skip 1 frame
  1111. } else {
  1112. *pNumberOfFramesToSkip = 0U; //no frame skip
  1113. }
  1114. *pSetVSIntegrationTime = pOV12870Ctx->AecCurVSIntegrationTime;
  1115. TRACE(OV12870_DEBUG, "%s: NewIntegrationTime=%f\n", __func__,
  1116. NewIntegrationTime);
  1117. TRACE(OV12870_INFO, "%s: (exit)\n", __func__);
  1118. return (result);
  1119. }
  1120. RESULT OV12870_IsiExposureControlIss
  1121. (IsiSensorHandle_t handle,
  1122. float NewGain,
  1123. float NewIntegrationTime,
  1124. uint8_t * pNumberOfFramesToSkip,
  1125. float *pSetGain, float *pSetIntegrationTime, float *hdr_ratio)
  1126. {
  1127. OV12870_Context_t *pOV12870Ctx = (OV12870_Context_t *) handle;
  1128. RESULT result = RET_SUCCESS;
  1129. int TmpGain;
  1130. /*TODO*/
  1131. TRACE(OV12870_INFO, "%s: (enter)\n", __func__);
  1132. if (pOV12870Ctx == NULL) {
  1133. TRACE(OV12870_ERROR,
  1134. "%s: Invalid sensor handle (NULL pointer detected)\n",
  1135. __func__);
  1136. return (RET_WRONG_HANDLE);
  1137. }
  1138. if ((pNumberOfFramesToSkip == NULL) || (pSetGain == NULL)
  1139. || (pSetIntegrationTime == NULL)) {
  1140. TRACE(OV12870_ERROR,
  1141. "%s: Invalid parameter (NULL pointer detected)\n",
  1142. __func__);
  1143. return (RET_NULL_POINTER);
  1144. }
  1145. if (NewGain >= 25) { // More than 25 will not take effect
  1146. NewGain = 25;
  1147. }
  1148. TRACE(OV12870_DEBUG, "%s: g=%f, Ti=%f\n", __func__, NewGain,
  1149. NewIntegrationTime);
  1150. if (NewIntegrationTime > pOV12870Ctx->FrameLengthLines * pOV12870Ctx->one_line_exp_time)
  1151. NewIntegrationTime = pOV12870Ctx->FrameLengthLines * pOV12870Ctx->one_line_exp_time;
  1152. float exp_t = NewIntegrationTime * 16.0f / pOV12870Ctx->one_line_exp_time;
  1153. TmpGain = (int)NewGain;
  1154. __ov12870_set_exposure(handle, (int)exp_t,
  1155. ((int)NewGain << 4) + round((NewGain - TmpGain) / 0.0625f), 1024, OV12870_ANALOG_GAIN | OV12870_INTEGRATION_TIME | OV12870_DIGITAL_GAIN);
  1156. *pSetGain = NewGain;
  1157. *pSetIntegrationTime = (int)exp_t / 16.0f * pOV12870Ctx->one_line_exp_time;
  1158. pOV12870Ctx->AecCurGain = NewGain;
  1159. pOV12870Ctx->AecCurIntegrationTime = *pSetIntegrationTime;
  1160. #if 0
  1161. float long_gain=0;
  1162. float long_exp=0;
  1163. float short_gain=0;
  1164. float short_exp=0;
  1165. if (pOV12870Ctx->SensorMode.hdr_mode != SENSOR_MODE_LINEAR)
  1166. {
  1167. long_exp = NewIntegrationTime;
  1168. long_gain = NewGain;
  1169. float short_exposure_measure = NewIntegrationTime*NewGain / *hdr_ratio;
  1170. if (short_exposure_measure < 48 * pOV12870Ctx->one_line_exp_time * pOV12870Ctx->AecMinGain)
  1171. {
  1172. short_exp = short_exposure_measure / pOV12870Ctx->AecMinGain;
  1173. short_gain = pOV12870Ctx->AecMinGain;
  1174. }else
  1175. {
  1176. short_exp = 48 * pOV12870Ctx->one_line_exp_time;
  1177. short_gain = short_exposure_measure / short_exp;
  1178. }
  1179. }else
  1180. {
  1181. long_exp = NewIntegrationTime;
  1182. long_gain = NewGain;
  1183. }
  1184. if (pOV12870Ctx->SensorMode.hdr_mode != SENSOR_MODE_LINEAR)
  1185. {
  1186. result = OV12870_IsiSetVSIntegrationTimeIss(handle,
  1187. short_exp,
  1188. pSetIntegrationTime,
  1189. pNumberOfFramesToSkip,
  1190. hdr_ratio);
  1191. result =
  1192. OV12870_IsiSetVSGainIss(handle, short_exp, short_gain,
  1193. pSetGain, hdr_ratio);
  1194. result = OV12870_IsiSetLongGainIss(handle, long_gain * (*hdr_ratio));
  1195. }
  1196. TRACE(OV12870_DEBUG, "%s: set: NewGain=%f, hcgTi=%f, hcgskip=%d\n",
  1197. __func__, NewGain, NewIntegrationTime, *pNumberOfFramesToSkip);
  1198. result = OV12870_IsiSetGainIss(handle, long_gain, pSetGain, hdr_ratio);
  1199. TRACE(OV12870_DEBUG, "%s: set: NewGain=%f, hcgTi=%f, hcgskip=%d\n",
  1200. __func__, NewGain, NewIntegrationTime, *pNumberOfFramesToSkip);
  1201. pOV12870Ctx->CurHdrRatio = *hdr_ratio;
  1202. #endif
  1203. result = OV12870_IsiSetIntegrationTimeIss(handle, NewIntegrationTime, pSetIntegrationTime, pNumberOfFramesToSkip, hdr_ratio);
  1204. TRACE(OV12870_DEBUG, "%s: set: vsg=%f, vsTi=%f, vsskip=%d\n", __func__,
  1205. NewGain, NewIntegrationTime, *pNumberOfFramesToSkip);
  1206. TRACE(OV12870_INFO, "%s: (exit)\n", __func__);
  1207. return result;
  1208. }
  1209. RESULT OV12870_IsiGetCurrentExposureIss
  1210. (IsiSensorHandle_t handle, float *pSetGain, float *pSetIntegrationTime, float *hdr_ratio) {
  1211. OV12870_Context_t *pOV12870Ctx = (OV12870_Context_t *) handle;
  1212. RESULT result = RET_SUCCESS;
  1213. TRACE(OV12870_INFO, "%s: (enter)\n", __func__);
  1214. if (pOV12870Ctx == NULL) {
  1215. TRACE(OV12870_ERROR,
  1216. "%s: Invalid sensor handle (NULL pointer detected)\n",
  1217. __func__);
  1218. return (RET_WRONG_HANDLE);
  1219. }
  1220. if ((pSetGain == NULL) || (pSetIntegrationTime == NULL))
  1221. return (RET_NULL_POINTER);
  1222. *pSetGain = pOV12870Ctx->AecCurGain;
  1223. *pSetIntegrationTime = pOV12870Ctx->AecCurIntegrationTime;
  1224. *hdr_ratio = pOV12870Ctx->CurHdrRatio;
  1225. TRACE(OV12870_INFO, "%s: (exit)\n", __func__);
  1226. return (result);
  1227. }
  1228. RESULT OV12870_IsiGetResolutionIss(IsiSensorHandle_t handle, uint16_t *pwidth, uint16_t *pheight) {
  1229. OV12870_Context_t *pOV12870Ctx = (OV12870_Context_t *) handle;
  1230. RESULT result = RET_SUCCESS;
  1231. TRACE(OV12870_INFO, "%s: (enter)\n", __func__);
  1232. if (pOV12870Ctx == NULL) {
  1233. TRACE(OV12870_ERROR,
  1234. "%s: Invalid sensor handle (NULL pointer detected)\n",
  1235. __func__);
  1236. return (RET_WRONG_HANDLE);
  1237. }
  1238. *pwidth = pOV12870Ctx->SensorMode.width;
  1239. *pheight = pOV12870Ctx->SensorMode.height;
  1240. TRACE(OV12870_INFO, "%s: (exit)\n", __func__);
  1241. return (result);
  1242. }
  1243. RESULT OV12870_IsiGetSensorFpsIss(IsiSensorHandle_t handle, uint32_t * pfps)
  1244. {
  1245. OV12870_Context_t *pOV12870Ctx = (OV12870_Context_t *) handle;
  1246. RESULT result = RET_SUCCESS;
  1247. TRACE(OV12870_INFO, "%s: (enter)\n", __func__);
  1248. if (pOV12870Ctx == NULL) {
  1249. TRACE(OV12870_ERROR,
  1250. "%s: Invalid sensor handle (NULL pointer detected)\n",
  1251. __func__);
  1252. return (RET_WRONG_HANDLE);
  1253. }
  1254. HalContext_t *pHalCtx = (HalContext_t *) pOV12870Ctx->IsiCtx.HalHandle;
  1255. if (pOV12870Ctx->KernelDriverFlag) {
  1256. /*TODO*/
  1257. ioctl(pHalCtx->sensor_fd, VVSENSORIOC_G_FPS, pfps);
  1258. pOV12870Ctx->CurrFps = *pfps;
  1259. }
  1260. *pfps = pOV12870Ctx->CurrFps;
  1261. TRACE(OV12870_INFO, "%s: (exit)\n", __func__);
  1262. return (result);
  1263. }
  1264. RESULT OV12870_IsiSetSensorFpsIss(IsiSensorHandle_t handle, uint32_t fps)
  1265. {
  1266. OV12870_Context_t *pOV12870Ctx = (OV12870_Context_t *) handle;
  1267. RESULT result = RET_SUCCESS;
  1268. int32_t ret = 0;
  1269. TRACE(OV12870_INFO, "%s: (enter)\n", __func__);
  1270. if (pOV12870Ctx == NULL) {
  1271. TRACE(OV12870_ERROR,
  1272. "%s: Invalid sensor handle (NULL pointer detected)\n",
  1273. __func__);
  1274. return (RET_WRONG_HANDLE);
  1275. }
  1276. HalContext_t *pHalCtx = (HalContext_t *) pOV12870Ctx->IsiCtx.HalHandle;
  1277. if (fps > pOV12870Ctx->MaxFps) {
  1278. TRACE(OV12870_ERROR,
  1279. "%s: set fps(%d) out of range, correct to %d (%d, %d)\n",
  1280. __func__, fps, pOV12870Ctx->MaxFps, pOV12870Ctx->MinFps,
  1281. pOV12870Ctx->MaxFps);
  1282. fps = pOV12870Ctx->MaxFps;
  1283. }
  1284. if (fps < pOV12870Ctx->MinFps) {
  1285. TRACE(OV12870_ERROR,
  1286. "%s: set fps(%d) out of range, correct to %d (%d, %d)\n",
  1287. __func__, fps, pOV12870Ctx->MinFps, pOV12870Ctx->MinFps,
  1288. pOV12870Ctx->MaxFps);
  1289. fps = pOV12870Ctx->MinFps;
  1290. }
  1291. if (pOV12870Ctx->KernelDriverFlag) {
  1292. ret = ioctl(pHalCtx->sensor_fd, VVSENSORIOC_S_FPS, &fps);
  1293. if (ret != 0) {
  1294. TRACE(OV12870_ERROR, "%s: set sensor fps=%d error\n",
  1295. __func__);
  1296. return (RET_FAILURE);
  1297. }
  1298. ret = ioctl(pHalCtx->sensor_fd, VVSENSORIOC_G_SENSOR_MODE, &(pOV12870Ctx->SensorMode));
  1299. {
  1300. pOV12870Ctx->MaxIntegrationLine = pOV12870Ctx->SensorMode.ae_info.max_integration_time;
  1301. pOV12870Ctx->AecMaxIntegrationTime = pOV12870Ctx->MaxIntegrationLine * pOV12870Ctx->one_line_exp_time;
  1302. }
  1303. #ifdef SUBDEV_CHAR
  1304. struct vvcam_ae_info_s ae_info;
  1305. ret =
  1306. ioctl(pHalCtx->sensor_fd, VVSENSORIOC_G_AE_INFO, &ae_info);
  1307. if (ret != 0) {
  1308. TRACE(OV12870_ERROR, "%s:sensor get ae info error!\n",
  1309. __func__);
  1310. return (RET_FAILURE);
  1311. }
  1312. pOV12870Ctx->one_line_exp_time =
  1313. (float)ae_info.one_line_exp_time_ns / 1000000000;
  1314. pOV12870Ctx->MaxIntegrationLine = ae_info.max_integration_time;
  1315. pOV12870Ctx->AecMaxIntegrationTime =
  1316. pOV12870Ctx->MaxIntegrationLine *
  1317. pOV12870Ctx->one_line_exp_time;
  1318. #endif
  1319. }
  1320. TRACE(OV12870_INFO, "%s: set sensor fps = %d\n", __func__,
  1321. pOV12870Ctx->CurrFps);
  1322. TRACE(OV12870_INFO, "%s: (exit)\n", __func__);
  1323. return (result);
  1324. }
  1325. static RESULT OV12870_IsiActivateTestPattern(IsiSensorHandle_t handle,
  1326. const bool_t enable)
  1327. {
  1328. RESULT result = RET_SUCCESS;
  1329. TRACE(OV12870_INFO, "%s: (enter)\n", __func__);
  1330. OV12870_Context_t *pOV12870Ctx = (OV12870_Context_t *) handle;
  1331. if (pOV12870Ctx == NULL || pOV12870Ctx->IsiCtx.HalHandle == NULL) {
  1332. return RET_NULL_POINTER;
  1333. }
  1334. if (pOV12870Ctx->Configured != BOOL_TRUE)
  1335. return RET_WRONG_STATE;
  1336. if (BOOL_TRUE == enable) {
  1337. //result = OV12870_IsiRegisterWriteIss(handle, 0x3253, 0x80);
  1338. } else {
  1339. //result = OV12870_IsiRegisterWriteIss(handle, 0x3253, 0x00);
  1340. }
  1341. pOV12870Ctx->TestPattern = enable;
  1342. TRACE(OV12870_INFO, "%s: (exit)\n", __func__);
  1343. return (result);
  1344. }
  1345. static RESULT OV12870_IsiSensorSetBlcIss(IsiSensorHandle_t handle, sensor_blc_t * pblc)
  1346. {
  1347. int32_t ret = 0;
  1348. OV12870_Context_t *pOV12870Ctx = (OV12870_Context_t *) handle;
  1349. if (pOV12870Ctx == NULL || pOV12870Ctx->IsiCtx.HalHandle == NULL) {
  1350. return RET_WRONG_HANDLE;
  1351. }
  1352. if (pblc == NULL)
  1353. return RET_NULL_POINTER;
  1354. HalContext_t *pHalCtx = (HalContext_t *) pOV12870Ctx->IsiCtx.HalHandle;
  1355. ret = ioctl(pHalCtx->sensor_fd, VVSENSORIOC_S_BLC, pblc);
  1356. if (ret != 0)
  1357. {
  1358. TRACE(OV12870_ERROR, "%s: set wb error\n", __func__);
  1359. }
  1360. return RET_SUCCESS;
  1361. }
  1362. static RESULT OV12870_IsiSensorSetWBIss(IsiSensorHandle_t handle, sensor_white_balance_t * pwb)
  1363. {
  1364. int32_t ret = 0;
  1365. OV12870_Context_t *pOV12870Ctx = (OV12870_Context_t *) handle;
  1366. if (pOV12870Ctx == NULL || pOV12870Ctx->IsiCtx.HalHandle == NULL) {
  1367. return RET_WRONG_HANDLE;
  1368. }
  1369. HalContext_t *pHalCtx = (HalContext_t *) pOV12870Ctx->IsiCtx.HalHandle;
  1370. if (pwb == NULL)
  1371. return RET_NULL_POINTER;
  1372. ret = ioctl(pHalCtx->sensor_fd, VVSENSORIOC_S_WB, pwb);
  1373. if (ret != 0)
  1374. {
  1375. TRACE(OV12870_ERROR, "%s: set wb error\n", __func__);
  1376. }
  1377. return RET_SUCCESS;
  1378. }
  1379. static RESULT OV12870_IsiGetSensorAWBModeIss(IsiSensorHandle_t handle, IsiSensorAwbMode_t *pawbmode)
  1380. {
  1381. OV12870_Context_t *pOV12870Ctx = (OV12870_Context_t *) handle;
  1382. if (pOV12870Ctx == NULL || pOV12870Ctx->IsiCtx.HalHandle == NULL) {
  1383. return RET_NULL_POINTER;
  1384. }
  1385. if (pOV12870Ctx->SensorMode.hdr_mode == SENSOR_MODE_HDR_NATIVE){
  1386. *pawbmode = ISI_SENSOR_AWB_MODE_SENSOR;
  1387. }else{
  1388. *pawbmode = ISI_SENSOR_AWB_MODE_NORMAL;
  1389. }
  1390. return RET_SUCCESS;
  1391. }
  1392. static RESULT OV12870_IsiSensorGetExpandCurveIss(IsiSensorHandle_t handle, sensor_expand_curve_t * pexpand_curve)
  1393. {
  1394. int32_t ret = 0;
  1395. OV12870_Context_t *pOV12870Ctx = (OV12870_Context_t *) handle;
  1396. if (pOV12870Ctx == NULL || pOV12870Ctx->IsiCtx.HalHandle == NULL) {
  1397. return RET_NULL_POINTER;
  1398. }
  1399. HalContext_t *pHalCtx = (HalContext_t *) pOV12870Ctx->IsiCtx.HalHandle;
  1400. ret = ioctl(pHalCtx->sensor_fd, VVSENSORIOC_G_EXPAND_CURVE, pexpand_curve);
  1401. if (ret != 0)
  1402. {
  1403. TRACE(OV12870_ERROR, "%s: get expand cure error\n", __func__);
  1404. return RET_FAILURE;
  1405. }
  1406. return RET_SUCCESS;
  1407. }
  1408. static RESULT OV12870_IsiGetCapsIss(IsiSensorHandle_t handle,
  1409. IsiSensorCaps_t * pIsiSensorCaps)
  1410. {
  1411. OV12870_Context_t *pOV12870Ctx = (OV12870_Context_t *) handle;
  1412. RESULT result = RET_SUCCESS;
  1413. TRACE(OV12870_INFO, "%s (enter)\n", __func__);
  1414. if (pOV12870Ctx == NULL) {
  1415. return (RET_WRONG_HANDLE);
  1416. }
  1417. if (pIsiSensorCaps == NULL) {
  1418. return (RET_NULL_POINTER);
  1419. }
  1420. pIsiSensorCaps->BusWidth = pOV12870Ctx->SensorMode.bit_width;
  1421. pIsiSensorCaps->Mode = ISI_MODE_BAYER;
  1422. pIsiSensorCaps->FieldSelection = ISI_FIELDSEL_BOTH;
  1423. pIsiSensorCaps->YCSequence = ISI_YCSEQ_YCBYCR;
  1424. pIsiSensorCaps->Conv422 = ISI_CONV422_NOCOSITED;
  1425. pIsiSensorCaps->BPat = pOV12870Ctx->SensorMode.bayer_pattern;
  1426. pIsiSensorCaps->HPol = ISI_HPOL_REFPOS;
  1427. pIsiSensorCaps->VPol = ISI_VPOL_NEG;
  1428. pIsiSensorCaps->Edge = ISI_EDGE_RISING;
  1429. pIsiSensorCaps->Resolution.width = pOV12870Ctx->SensorMode.width;
  1430. pIsiSensorCaps->Resolution.height = pOV12870Ctx->SensorMode.height;
  1431. pIsiSensorCaps->SmiaMode = ISI_SMIA_OFF;
  1432. pIsiSensorCaps->MipiLanes = ISI_MIPI_2LANES;
  1433. if (pIsiSensorCaps->BusWidth == 10) {
  1434. pIsiSensorCaps->MipiMode = ISI_MIPI_MODE_RAW_10;
  1435. }else if (pIsiSensorCaps->BusWidth == 12){
  1436. pIsiSensorCaps->MipiMode = ISI_MIPI_MODE_RAW_12;
  1437. }else{
  1438. pIsiSensorCaps->MipiMode = ISI_MIPI_OFF;
  1439. }
  1440. TRACE(OV12870_INFO, "%s (exit)\n", __func__);
  1441. return result;
  1442. }
  1443. RESULT OV12870_IsiGetSensorIss(IsiSensor_t *pIsiSensor)
  1444. {
  1445. RESULT result = RET_SUCCESS;
  1446. TRACE( OV12870_INFO, "%s (enter)\n", __func__);
  1447. if ( pIsiSensor != NULL ) {
  1448. pIsiSensor->pszName = SensorName;
  1449. pIsiSensor->pIsiCreateSensorIss = OV12870_IsiCreateSensorIss;
  1450. pIsiSensor->pIsiInitSensorIss = OV12870_IsiInitSensorIss;
  1451. pIsiSensor->pIsiGetSensorModeIss = OV12870_IsiGetSensorModeIss;
  1452. pIsiSensor->pIsiResetSensorIss = OV12870_IsiResetSensorIss;
  1453. pIsiSensor->pIsiReleaseSensorIss = OV12870_IsiReleaseSensorIss;
  1454. pIsiSensor->pIsiGetCapsIss = OV12870_IsiGetCapsIss;
  1455. pIsiSensor->pIsiSetupSensorIss = OV12870_IsiSetupSensorIss;
  1456. pIsiSensor->pIsiChangeSensorResolutionIss = OV12870_IsiChangeSensorResolutionIss;
  1457. pIsiSensor->pIsiSensorSetStreamingIss = OV12870_IsiSensorSetStreamingIss;
  1458. pIsiSensor->pIsiSensorSetPowerIss = OV12870_IsiSensorSetPowerIss;
  1459. pIsiSensor->pIsiCheckSensorConnectionIss = OV12870_IsiCheckSensorConnectionIss;
  1460. pIsiSensor->pIsiGetSensorRevisionIss = OV12870_IsiGetSensorRevisionIss;
  1461. pIsiSensor->pIsiRegisterReadIss = OV12870_IsiRegisterReadIss;
  1462. pIsiSensor->pIsiRegisterWriteIss = OV12870_IsiRegisterWriteIss;
  1463. /* AEC functions */
  1464. pIsiSensor->pIsiExposureControlIss = OV12870_IsiExposureControlIss;
  1465. pIsiSensor->pIsiGetGainLimitsIss = OV12870_IsiGetGainLimitsIss;
  1466. pIsiSensor->pIsiGetIntegrationTimeLimitsIss = OV12870_IsiGetIntegrationTimeLimitsIss;
  1467. pIsiSensor->pIsiGetCurrentExposureIss = OV12870_IsiGetCurrentExposureIss;
  1468. pIsiSensor->pIsiGetVSGainIss = OV12870_IsiGetVSGainIss;
  1469. pIsiSensor->pIsiGetGainIss = OV12870_IsiGetGainIss;
  1470. pIsiSensor->pIsiGetLongGainIss = OV12870_IsiGetLongGainIss;
  1471. pIsiSensor->pIsiGetGainIncrementIss = OV12870_IsiGetGainIncrementIss;
  1472. pIsiSensor->pIsiSetGainIss = OV12870_IsiSetGainIss;
  1473. pIsiSensor->pIsiGetIntegrationTimeIss = OV12870_IsiGetIntegrationTimeIss;
  1474. pIsiSensor->pIsiGetVSIntegrationTimeIss = OV12870_IsiGetVSIntegrationTimeIss;
  1475. pIsiSensor->pIsiGetLongIntegrationTimeIss = OV12870_IsiGetLongIntegrationTimeIss;
  1476. pIsiSensor->pIsiGetIntegrationTimeIncrementIss = OV12870_IsiGetIntegrationTimeIncrementIss;
  1477. pIsiSensor->pIsiSetIntegrationTimeIss = OV12870_IsiSetIntegrationTimeIss;
  1478. pIsiSensor->pIsiQuerySensorIss = OV12870_IsiQuerySensorIss;
  1479. pIsiSensor->pIsiGetResolutionIss = OV12870_IsiGetResolutionIss;
  1480. pIsiSensor->pIsiGetSensorFpsIss = OV12870_IsiGetSensorFpsIss;
  1481. pIsiSensor->pIsiSetSensorFpsIss = OV12870_IsiSetSensorFpsIss;
  1482. pIsiSensor->pIsiSensorGetExpandCurveIss = OV12870_IsiSensorGetExpandCurveIss;
  1483. /* AWB specific functions */
  1484. /* Testpattern */
  1485. pIsiSensor->pIsiActivateTestPattern = OV12870_IsiActivateTestPattern;
  1486. pIsiSensor->pIsiSetBayerPattern = OV12870_IsiSetBayerPattern;
  1487. pIsiSensor->pIsiSensorSetBlcIss = OV12870_IsiSensorSetBlcIss;
  1488. pIsiSensor->pIsiSensorSetWBIss = OV12870_IsiSensorSetWBIss;
  1489. pIsiSensor->pIsiGetSensorAWBModeIss = OV12870_IsiGetSensorAWBModeIss;
  1490. } else {
  1491. result = RET_NULL_POINTER;
  1492. }
  1493. TRACE( OV12870_INFO, "%s (exit)\n", __func__);
  1494. return ( result );
  1495. }
  1496. /*****************************************************************************
  1497. * each sensor driver need declare this struct for isi load
  1498. *****************************************************************************/
  1499. IsiCamDrvConfig_t OV12870_IsiCamDrvConfig = {
  1500. 0,
  1501. OV12870_IsiQuerySensorSupportIss,
  1502. OV12870_IsiGetSensorIss,
  1503. {
  1504. SensorName, /**< IsiSensor_t.pszName */
  1505. 0, /**< IsiSensor_t.pIsiInitIss>*/
  1506. 0, /**< IsiSensor_t.pIsiResetSensorIss>*/
  1507. 0, /**< IsiSensor_t.pRegisterTable */
  1508. 0, /**< IsiSensor_t.pIsiSensorCaps */
  1509. 0, /**< IsiSensor_t.pIsiCreateSensorIss */
  1510. 0, /**< IsiSensor_t.pIsiReleaseSensorIss */
  1511. 0, /**< IsiSensor_t.pIsiGetCapsIss */
  1512. 0, /**< IsiSensor_t.pIsiSetupSensorIss */
  1513. 0, /**< IsiSensor_t.pIsiChangeSensorResolutionIss */
  1514. 0, /**< IsiSensor_t.pIsiSensorSetStreamingIss */
  1515. 0, /**< IsiSensor_t.pIsiSensorSetPowerIss */
  1516. 0, /**< IsiSensor_t.pIsiCheckSensorConnectionIss */
  1517. 0, /**< IsiSensor_t.pIsiGetSensorRevisionIss */
  1518. 0, /**< IsiSensor_t.pIsiRegisterReadIss */
  1519. 0, /**< IsiSensor_t.pIsiRegisterWriteIss */
  1520. 0, /**< IsiSensor_t.pIsiExposureControlIss */
  1521. 0, /**< IsiSensor_t.pIsiGetGainLimitsIss */
  1522. 0, /**< IsiSensor_t.pIsiGetIntegrationTimeLimitsIss */
  1523. 0, /**< IsiSensor_t.pIsiGetCurrentExposureIss */
  1524. 0, /**< IsiSensor_t.pIsiGetGainIss */
  1525. 0, /**< IsiSensor_t.pIsiGetVSGainIss */
  1526. 0, /**< IsiSensor_t.pIsiGetGainIncrementIss */
  1527. 0, /**< IsiSensor_t.pIsiGetGainIncrementIss */
  1528. 0, /**< IsiSensor_t.pIsiSetGainIss */
  1529. 0, /**< IsiSensor_t.pIsiGetIntegrationTimeIss */
  1530. 0, /**< IsiSensor_t.pIsiGetIntegrationTimeIncrementIss */
  1531. 0, /**< IsiSensor_t.pIsiSetIntegrationTimeIss */
  1532. 0, /**< IsiSensor_t.pIsiGetResolutionIss */
  1533. 0, /**< IsiSensor_t.pIsiGetAfpsInfoIss */
  1534. 0, /**< IsiSensor_t.pIsiMdiInitMotoDriveMds */
  1535. 0, /**< IsiSensor_t.pIsiMdiSetupMotoDrive */
  1536. 0, /**< IsiSensor_t.pIsiMdiFocusSet */
  1537. 0, /**< IsiSensor_t.pIsiMdiFocusGet */
  1538. 0, /**< IsiSensor_t.pIsiMdiFocusCalibrate */
  1539. 0, /**< IsiSensor_t.pIsiGetSensorMipiInfoIss */
  1540. 0, /**< IsiSensor_t.pIsiActivateTestPattern */
  1541. 0, /**< IsiSensor_t.pIsiSetBayerPattern */
  1542. }
  1543. };