/
sunny_q8v18a_eeprom.c
433 lines (376 loc) · 12 KB
/
sunny_q8v18a_eeprom.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*============================================================================
Copyright (c) 2013 Qualcomm Technologies, Inc. All Rights Reserved.
Qualcomm Technologies Proprietary and Confidential.
============================================================================*/
#include <stdio.h>
#include <dlfcn.h>
#include <math.h>
#include <stdlib.h>
#include "chromatix.h"
#include "eeprom.h"
#include "sensor_common.h"
#undef DEBUG_INFO
#ifdef SUNNY_Q8V18A_DEBUG
#define DEBUG_INFO(fmt, args...) SERR(fmt, ##args)
#else
#define DEBUG_INFO(fmt, args...) do { } while (0)
#endif
#define BASE_ADDR 0x7010
#define WB_FLAG_ADDR 0x7020
#define LENS_FLAG_ADDR 0x703A
#define VCM_FLAG_ADDR 0x7030
#define WB_OFFSET (WB_FLAG_ADDR-BASE_ADDR)
#define WB_GROUP_SIZE 5
#define LENS_OFFSET (LENS_FLAG_ADDR-BASE_ADDR)
#define VCM_OFFSET (VCM_FLAG_ADDR-BASE_ADDR)
#define AWB_REG_SIZE 6
#define LSC_REG_SIZE 62
#define RG_RATIO_TYPICAL_VALUE 0x124
#define BG_RATIO_TYPICAL_VALUE 0x11B
#define ABS(x) (((x) < 0) ? -(x) : (x))
struct otp_struct {
uint16_t module_integrator_id;
uint16_t lens_id;
uint16_t production_year;
uint16_t production_month;
uint16_t production_day;
uint16_t rg_ratio;
uint16_t bg_ratio;
uint16_t light_rg;
uint16_t light_bg;
uint16_t user_data[5];
uint16_t lenc[62];
uint16_t VCM_start ;
uint16_t VCM_end ;
uint16_t VCM_dir ;
} otp_data;
struct msm_camera_i2c_reg_array g_reg_array[AWB_REG_SIZE+LSC_REG_SIZE];
struct msm_camera_i2c_reg_setting g_reg_setting;
/** sunny_q8v18a_get_calibration_items:
* @e_ctrl: point to sensor_eeprom_data_t of the eeprom device
*
* Get calibration capabilities and mode items.
*
* This function executes in eeprom module context
*
* Return: void.
**/
void sunny_q8v18a_get_calibration_items(void *e_ctrl)
{
sensor_eeprom_data_t *ectrl = (sensor_eeprom_data_t *)e_ctrl;
eeprom_calib_items_t *e_items = &(ectrl->eeprom_data.items);
e_items->is_insensor = TRUE;
e_items->is_afc = FALSE;
e_items->is_wbc = TRUE;
e_items->is_lsc = TRUE;
e_items->is_dpc = FALSE;
}
/** sunny_q8v18a_update_awb:
*
* Calculate and apply white balance calibration data
*
* This function executes in eeprom module context
*
* Return: void.
**/
static void sunny_q8v18a_update_awb()
{
uint16_t R_gain, G_gain, B_gain;
uint16_t G_gain_R, G_gain_B ;
uint16_t nR_G_gain, nB_G_gain, nG_G_gain;
uint16_t nBase_gain;
if(otp_data.light_rg) {
otp_data.rg_ratio = otp_data.rg_ratio * (otp_data.light_rg +512) / 1024;
}
if(otp_data.light_bg){
otp_data.bg_ratio = otp_data.bg_ratio * (otp_data.light_bg +512) / 1024;
}
DEBUG_INFO("rg_ratio=0x%x,bg_ratio=0x%x,light_rg=0x%x,light_bg=0x%x",
otp_data.rg_ratio,otp_data.bg_ratio,otp_data.light_rg,otp_data.light_bg) ;
nR_G_gain = (RG_RATIO_TYPICAL_VALUE*1000) / otp_data.rg_ratio;
nB_G_gain = (BG_RATIO_TYPICAL_VALUE*1000) / otp_data.bg_ratio;
nG_G_gain = 1000;
if (nR_G_gain < 1000 || nB_G_gain < 1000)
{
if (nR_G_gain < nB_G_gain)
nBase_gain = nR_G_gain;
else
nBase_gain = nB_G_gain;
}
else
{
nBase_gain = nG_G_gain;
}
R_gain = 0x1000 * nR_G_gain / (nBase_gain);
B_gain = 0x1000 * nB_G_gain / (nBase_gain);
G_gain = 0x1000 * nG_G_gain / (nBase_gain);
DEBUG_INFO("R_gain=0x%x,G_gain=0x%x,B_gain=0x%x",R_gain,G_gain,B_gain) ;
if (R_gain > 0x1000) {
g_reg_array[g_reg_setting.size].reg_addr = 0x5018;
g_reg_array[g_reg_setting.size].reg_data = R_gain >> 8;
g_reg_setting.size++;
g_reg_array[g_reg_setting.size].reg_addr = 0x5019;
g_reg_array[g_reg_setting.size].reg_data = R_gain & 0x00ff;
g_reg_setting.size++;
}
if (G_gain > 0x1000) {
g_reg_array[g_reg_setting.size].reg_addr = 0x501A;
g_reg_array[g_reg_setting.size].reg_data = G_gain >> 8;
g_reg_setting.size++;
g_reg_array[g_reg_setting.size].reg_addr = 0x501B;
g_reg_array[g_reg_setting.size].reg_data = G_gain & 0x00ff;
g_reg_setting.size++;
}
if (B_gain > 0x1000) {
g_reg_array[g_reg_setting.size].reg_addr = 0x501C;
g_reg_array[g_reg_setting.size].reg_data = B_gain >> 8;
g_reg_setting.size++;
g_reg_array[g_reg_setting.size].reg_addr = 0x501D;
g_reg_array[g_reg_setting.size].reg_data = B_gain & 0x00ff;
g_reg_setting.size++;
}
}
/** sunny_q8v18a_get_group_index:
* @mid: group index register value
*
* Get which group is used
*
* This function executes in eeprom module context
*
* Return: int to indicate group index.
**/
static int sunny_q8v18a_get_group_index(uint8_t mid)
{
int8_t group_index = -1 ;
mid = mid & 0xFC ;
if((mid&0xC0) == 0x40){
group_index = 0 ;
}else if((mid&0x30) == 0x10){
group_index = 1 ;
}else if((mid&0x0C) == 0x04){
group_index = 2 ;
}else{
group_index = -1 ;
}
return group_index ;
}
/** sunny_q8v18a_read_info:
* @e_ctrl: point to sensor_eeprom_data_t of the eeprom device
*
* Read the data structure of product information like product date
*
* This function executes in eeprom module context
*
* Return: int to indicate read information success or not.
**/
static int sunny_q8v18a_read_info(sensor_eeprom_data_t *e_ctrl)
{
uint8_t mid,temp,reg_val;
int i = 0, group_index,addr_offset = 0,group_offset=5;
SLOW("Enter");
mid = (uint8_t)(e_ctrl->eeprom_params.buffer[0]);
if((group_index=sunny_q8v18a_get_group_index(mid))==-1){
SERR("%s:invalid or empty opt data",__func__) ;
return -1 ;
}
addr_offset = group_offset*group_index ;
otp_data.module_integrator_id =
(uint8_t)(e_ctrl->eeprom_params.buffer[addr_offset + 1]);
otp_data.lens_id =
(uint8_t)(e_ctrl->eeprom_params.buffer[addr_offset + 2]);
otp_data.production_year =
(uint8_t)(e_ctrl->eeprom_params.buffer[addr_offset + 3]);
otp_data.production_month =
(uint8_t)(e_ctrl->eeprom_params.buffer[addr_offset + 4]);
otp_data.production_day =
(uint8_t)(e_ctrl->eeprom_params.buffer[addr_offset + 5]);
return 0;
}
/** sunny_q8v18a_read_wbdata:
* @e_ctrl: point to sensor_eeprom_data_t of the eeprom device
*
* Read the data structure of white balance calibration
*
* This function executes in eeprom module context
*
* Return: int to indicate read white balance success or not.
**/
static int sunny_q8v18a_read_wbdata(sensor_eeprom_data_t *e_ctrl)
{
uint8_t mid ;
uint8_t temp ;
uint8_t reg_val ;
int addr_offset = -1 ;
int group_index = -1 ;
int group_offset = 5 ;
SLOW("Enter");
mid = (uint8_t)(e_ctrl->eeprom_params.buffer[WB_OFFSET]);
if((group_index=sunny_q8v18a_get_group_index(mid))==-1){
SERR("%s:invalid or empty awb data",__func__) ;
return -1 ;
}
addr_offset =WB_OFFSET+group_index*group_offset ;
temp = (uint8_t)(e_ctrl->eeprom_params.buffer[addr_offset + 5]);
DEBUG_INFO("temp=0x%x",temp) ;
reg_val = (uint8_t)(e_ctrl->eeprom_params.buffer[addr_offset + 1]);
DEBUG_INFO("reg_val=0x%x",reg_val) ;
otp_data.rg_ratio = (reg_val<<2) + ((temp>>6)&0x03) ;
reg_val = (uint8_t)(e_ctrl->eeprom_params.buffer[addr_offset + 2]);
DEBUG_INFO("reg_val=0x%x",reg_val) ;
otp_data.bg_ratio = (reg_val<<2) + ((temp>>4)&0x03) ;
reg_val = (uint8_t)(e_ctrl->eeprom_params.buffer[addr_offset + 3]);
DEBUG_INFO("reg_val=0x%x",reg_val) ;
otp_data.light_rg = (reg_val<<2) + ((temp>>2)&0x03) ;
reg_val = (uint8_t)(e_ctrl->eeprom_params.buffer[addr_offset + 4]);
DEBUG_INFO("reg_val=0x%x",reg_val) ;
otp_data.light_bg = (reg_val<<2) + (temp&0x03) ;
return 0;
}
/** sunny_q8v18a_read_VCMdata:
* @e_ctrl: point to sensor_eeprom_data_t of the eeprom device
*
* Read the data structure of white balance calibration
*
* This function executes in eeprom module context
*
* Return: int to indicate read VCM data success or not.
**/
static int sunny_q8v18a_read_VCMdata(sensor_eeprom_data_t *e_ctrl)
{
uint8_t mid ;
uint8_t temp ;
uint8_t reg_val ;
int addr_offset = -1 ;
int group_index = -1 ;
int group_offset = 3 ;
SLOW("Enter");
mid = (uint8_t)(e_ctrl->eeprom_params.buffer[VCM_OFFSET]);
if((group_index=sunny_q8v18a_get_group_index(mid))==-1){
SERR("%s:invalid or empty VCM data",__func__) ;
return -1 ;
}
addr_offset =VCM_OFFSET+group_index*group_offset ;
temp = (uint8_t)(e_ctrl->eeprom_params.buffer[addr_offset + 3]);
DEBUG_INFO("temp=0x%x",temp) ;
reg_val = (uint8_t)(e_ctrl->eeprom_params.buffer[addr_offset + 1]);
DEBUG_INFO("reg_val=0x%x",reg_val) ;
otp_data.VCM_start = (reg_val<<2) + ((temp>>6)&0x03) ;
reg_val = (uint8_t)(e_ctrl->eeprom_params.buffer[addr_offset + 2]);
DEBUG_INFO("reg_val=0x%x",reg_val) ;
otp_data.VCM_end = (reg_val<<2) + ((temp>>4)&0x03) ;
otp_data.VCM_dir = ((temp>>2)&0x03) ;
return 0;
}
/** sunny_q8v18a_format_wbdata:
* @e_ctrl: point to sensor_eeprom_data_t of the eeprom device
*
* Format the data structure of white balance calibration
*
* This function executes in eeprom module context
*
* Return: void.
**/
static void sunny_q8v18a_format_wbdata(sensor_eeprom_data_t *e_ctrl)
{
SLOW("Enter");
int rc = 0;
rc = sunny_q8v18a_read_wbdata(e_ctrl);
if(rc < 0)
SERR("read wbdata failed");
sunny_q8v18a_update_awb();
SLOW("Exit");
}
/** sunny_q8v18a_format_lensshading:
* @e_ctrl: point to sensor_eeprom_data_t of the eeprom device
*
* Format the data structure of lens shading correction calibration
*
* This function executes in eeprom module context
*
* Return: void.
**/
void sunny_q8v18a_format_lensshading(sensor_eeprom_data_t *e_ctrl)
{
int addr_offset = -1 ;
int group_index = -1 ;
int group_offset = LSC_REG_SIZE ;
int j = 0 ;
uint8_t mid;
mid = (uint8_t)(e_ctrl->eeprom_params.buffer[LENS_OFFSET]);
if((group_index=sunny_q8v18a_get_group_index(mid))==-1){
SERR("%s:invalid or empty lensshading data",__func__) ;
return;
}
addr_offset = LENS_OFFSET + group_index*group_offset ;
for (j = 0; j < LSC_REG_SIZE; j++) {
g_reg_array[g_reg_setting.size].reg_addr = 0x5800 + j;
g_reg_array[g_reg_setting.size].reg_data =
(uint8_t)(e_ctrl->eeprom_params.buffer[addr_offset + j + 1]);
g_reg_setting.size++;
DEBUG_INFO("reg_val=0x%x",
(uint8_t)(e_ctrl->eeprom_params.buffer[addr_offset + j + 1])) ;
}
SLOW("Exit");
}
/** sunny_q8v18a_format_calibration_data:
* @e_ctrl: point to sensor_eeprom_data_t of the eeprom device
*
* Format all the data structure of calibration
*
* This function executes in eeprom module context and generate
* all the calibration registers setting of the sensor.
*
* Return: void.
**/
void sunny_q8v18a_format_calibration_data(void *e_ctrl) {
SLOW("Enter");
sensor_eeprom_data_t *ectrl = (sensor_eeprom_data_t *)e_ctrl;
uint8_t *data = ectrl->eeprom_params.buffer;
g_reg_setting.addr_type = MSM_CAMERA_I2C_WORD_ADDR;
g_reg_setting.data_type = MSM_CAMERA_I2C_BYTE_DATA;
g_reg_setting.reg_setting = &g_reg_array[0];
g_reg_setting.size = 0;
g_reg_setting.delay = 0;
sunny_q8v18a_read_info(e_ctrl) ;
sunny_q8v18a_format_wbdata(ectrl);
sunny_q8v18a_format_lensshading(ectrl);
SLOW("Exit");
}
/** sunny_q8v18a_get_raw_data:
* @e_ctrl: point to sensor_eeprom_data_t of the eeprom device
* @data: point to the destination msm_camera_i2c_reg_setting
*
* Get the all the calibration registers setting of the sensor
*
* This function executes in eeprom module context.
*
* Return: void.
**/
void sunny_q8v18a_get_raw_data(void *e_ctrl, void *data) {
if (e_ctrl && data)
memcpy(data, &g_reg_setting, sizeof(g_reg_setting));
else
SERR("failed Null pointer");
return;
}
static eeprom_lib_func_t sunny_q8v18a_lib_func_ptr = {
.get_calibration_items = sunny_q8v18a_get_calibration_items,
.format_calibration_data = sunny_q8v18a_format_calibration_data,
.do_af_calibration = NULL,
.do_wbc_calibration = NULL,
.do_lsc_calibration = NULL,
.do_dpc_calibration = NULL,
.get_dpc_calibration_info = NULL,
.get_raw_data = sunny_q8v18a_get_raw_data,
};
/** sunny_q8v18a_eeprom_open_lib:
*
* Get the funtion pointer of this lib.
*
* This function executes in eeprom module context.
*
* Return: eeprom_lib_func_t point to the function pointer.
**/
void* sunny_q8v18a_eeprom_open_lib(void) {
return &sunny_q8v18a_lib_func_ptr;
}