1e7621fd0a07c0bfb06453787e5c0791d6ea0498
[GitHub/LineageOS/android_kernel_motorola_exynos9610.git] /
1 /*
2 * Samsung Exynos5 SoC series Sensor driver
3 *
4 *
5 * Copyright (c) 2018 Samsung Electronics Co., Ltd
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12 #include <linux/i2c.h>
13 #include <linux/time.h>
14 #include <linux/slab.h>
15 #include <linux/module.h>
16 #include <linux/videodev2.h>
17 #include <linux/videodev2_exynos_camera.h>
18 #include <media/v4l2-ctrls.h>
19 #include <media/v4l2-device.h>
20 #include <media/v4l2-subdev.h>
21
22 #include <exynos-fimc-is-sensor.h>
23 #include "fimc-is-sensor-eeprom-16885c.h"
24 #include "fimc-is-sensor-eeprom.h"
25 #include "fimc-is-device-sensor.h"
26 #include "fimc-is-device-sensor-peri.h"
27 #include "fimc-is-core.h"
28
29 #define SENSOR_EEPROM_NAME "16885C"
30
31 int fimc_is_eeprom_16885c_check_all_crc(struct v4l2_subdev *subdev)
32 {
33 int ret = 0;
34 struct fimc_is_module_enum *module;
35 struct fimc_is_eeprom *eeprom = NULL;
36 struct fimc_is_device_sensor *sensor = NULL;
37 int ret_sum = 0;
38
39 FIMC_BUG(!subdev);
40
41 module = (struct fimc_is_module_enum *)v4l2_get_subdev_hostdata(subdev);
42
43 eeprom = (struct fimc_is_eeprom *)v4l2_get_subdevdata(subdev);
44
45 FIMC_BUG(!eeprom);
46 FIMC_BUG(!module);
47
48 sensor = (struct fimc_is_device_sensor *)v4l2_get_subdev_hostdata(module->subdev);
49 if (!sensor) {
50 err("device sensor is NULL");
51 ret = -ENODEV;
52 return ret;
53 }
54
55 /* Check CRC to Address cal data */
56 ret = CALL_EEPROMOPS(eeprom, eeprom_check_address, subdev);
57 if (ret) {
58 err("%s(): 16885C EEPROM Address section CRC check fail(%d)", __func__, ret);
59
60 /* All calibration data is zero set only Address section is invalid CRC */
61 fimc_is_eeprom_cal_data_set(eeprom->data, "all",
62 EEPROM_ADD_CRC_FST, EEPROM_DATA_SIZE, 0xff);
63
64 /*Set all cal_status to ERROR if Address cal data invalid*/
65 for (int i = 0; i < CAMERA_CRC_INDEX_MAX; i++)
66 sensor->cal_status[i] = CRC_ERROR;
67
68 return ret;
69 } else
70 info("16885C EEPROM Address section CRC check success\n");
71
72 /* Check CRC to Information cal data */
73 ret = CALL_EEPROMOPS(eeprom, eeprom_check_info, subdev);
74 if (ret) {
75 ret_sum++;
76 err("%s(): 16885C EEPROM Information CRC section check fail(%d)", __func__, ret);
77
78 /* All calibration data is 0xff set but exception Address section */
79 fimc_is_eeprom_cal_data_set(eeprom->data, "Information - End",
80 EEPROM_INFO_CRC_FST, EEPROM_ADD_CAL_SIZE, 0xff);
81
82 sensor->cal_status[CAMERA_CRC_INDEX_MNF] = CRC_ERROR;
83
84 } else {
85 info("16885C EEPROM Informaion section CRC check success\n");
86
87 sensor->cal_status[CAMERA_CRC_INDEX_MNF] = CRC_NO_ERROR;
88 }
89
90 /* Check CRC to AWB cal data */
91 ret = CALL_EEPROMOPS(eeprom, eeprom_check_awb, subdev);
92 if (ret) {
93 ret_sum++;
94 err("%s(): 16885C EEPROM AWB section CRC check fail(%d)", __func__, ret);
95
96 fimc_is_eeprom_cal_data_set(eeprom->data, "AWB",
97 EEPROM_AWB_CRC_FST, EEPROM_AWB_CAL_SIZE, 0xff);
98
99 sensor->cal_status[CAMERA_CRC_INDEX_AWB] = CRC_ERROR;
100
101 } else {
102 info("16885C EEPROM AWB section CRC check success\n");
103
104 sensor->cal_status[CAMERA_CRC_INDEX_AWB] = CRC_NO_ERROR;
105
106 ret = fimc_is_sensor_eeprom_check_awb_ratio(&eeprom->data[EEPROM_AWB_UNIT_OFFSET],
107 &eeprom->data[EEPROM_AWB_GOLDEN_OFFSET],&eeprom->data[EEPROM_AWB_LIMIT_OFFSET]);
108 if (ret) {
109 err("%s(): 16885C EEPROM AWB ratio out of limit(%d)", __func__, ret);
110
111 sensor->cal_status[CAMERA_CRC_INDEX_AWB] = LIMIT_FAILURE;
112 }
113 }
114
115 /* Check CRC to LSC cal data */
116 ret = CALL_EEPROMOPS(eeprom, eeprom_check_lsc, subdev);
117 if (ret) {
118 ret_sum++;
119 err("%s(): 16885C EEPROM LSC section CRC check fail(%d)", __func__, ret);
120
121 fimc_is_eeprom_cal_data_set(eeprom->data, "LSC",
122 EEPROM_LSC_CRC_FST, EEPROM_LSC_CAL_SIZE, 0xff);
123
124 } else
125 info("16885C EEPROM LSC section CRC check success\n");
126
127 /* Check CRC to SFR cal data */
128 ret = CALL_EEPROMOPS(eeprom, eeprom_check_sfr, subdev);
129 if (ret) {
130 ret_sum++;
131 err("%s(): EEPROM SFR section CRC check fail(%d)", __func__, ret);
132
133 fimc_is_eeprom_cal_data_set(eeprom->data, "SFR",
134 EEPROM_SFR_CRC_FST, EEPROM_SFR_CAL_SIZE, 0xff);
135
136 } else
137 info("16885C EEPROM SFR section CRC check success\n");
138
139 return ret_sum;
140 }
141
142 static int fimc_is_eeprom_16885c_check_address(struct v4l2_subdev *subdev)
143 {
144 int ret = 0;
145 u16 crc_value = 0;
146 u16 crc16 = 0;
147 struct fimc_is_eeprom *eeprom = NULL;
148
149 FIMC_BUG(!subdev);
150
151 eeprom = (struct fimc_is_eeprom *)v4l2_get_subdevdata(subdev);
152
153 FIMC_BUG(!eeprom);
154
155 crc_value = ((eeprom->data[EEPROM_ADD_CRC_SEC] << 8) | (eeprom->data[EEPROM_ADD_CRC_FST]));
156
157 crc16 = fimc_is_sensor_eeprom_check_crc(&eeprom->data[EEPROM_ADD_CRC_CHK_START], EEPROM_ADD_CRC_CHK_SIZE);
158 if (crc_value != crc16) {
159 err("Error to ADD CRC16: 0x%x, cal_buffer CRC: 0x%x", crc16, crc_value);
160
161 ret = -EINVAL;
162 } else
163 info("ADD CRC16: 0x%x, cal_buffer CRC: 0x%x\n", crc16, crc_value);
164
165 return ret;
166 }
167
168 static int fimc_is_eeprom_16885c_check_info(struct v4l2_subdev *subdev)
169 {
170 int ret = 0;
171 u16 crc_value = 0;
172 u16 crc16 = 0;
173 struct fimc_is_eeprom *eeprom = NULL;
174
175 FIMC_BUG(!subdev);
176
177 eeprom = (struct fimc_is_eeprom *)v4l2_get_subdevdata(subdev);
178
179 FIMC_BUG(!eeprom);
180
181 crc_value = ((eeprom->data[EEPROM_INFO_CRC_SEC] << 8) | (eeprom->data[EEPROM_INFO_CRC_FST]));
182
183 crc16 = fimc_is_sensor_eeprom_check_crc(&eeprom->data[EEPROM_INFO_CRC_CHK_START], EEPROM_INFO_CRC_CHK_SIZE);
184 if (crc_value != crc16) {
185 err("Error to INFO CRC16: 0x%x, cal_buffer CRC: 0x%x", crc16, crc_value);
186
187 ret = -EINVAL;
188 } else
189 info("INFO CRC16: 0x%x, cal_buffer CRC: 0x%x\n", crc16, crc_value);
190
191 return ret;
192 }
193
194 static int fimc_is_eeprom_16885c_check_awb(struct v4l2_subdev *subdev)
195 {
196 int ret = 0;
197 u16 crc_value = 0;
198 u16 crc16 = 0;
199 struct fimc_is_eeprom *eeprom = NULL;
200
201 FIMC_BUG(!subdev);
202
203 eeprom = (struct fimc_is_eeprom *)v4l2_get_subdevdata(subdev);
204
205 FIMC_BUG(!eeprom);
206
207 crc_value = ((eeprom->data[EEPROM_AWB_CRC_SEC] << 8) | (eeprom->data[EEPROM_AWB_CRC_FST]));
208
209 crc16 = fimc_is_sensor_eeprom_check_crc(&eeprom->data[EEPROM_AWB_CRC_CHK_START], EEPROM_AWB_CRC_CHK_SIZE);
210 if (crc_value != crc16) {
211 err("Error to AWB CRC16: 0x%x, cal_buffer CRC: 0x%x", crc16, crc_value);
212
213 ret = -EINVAL;
214 } else
215 info("AWB CRC16: 0x%x, cal_buffer CRC: 0x%x\n", crc16, crc_value);
216
217 return ret;
218 }
219
220 static int fimc_is_eeprom_16885c_check_lsc(struct v4l2_subdev *subdev)
221 {
222 int ret = 0;
223 u16 crc_value = 0;
224 u16 crc16 = 0;
225 struct fimc_is_eeprom *eeprom = NULL;
226
227 FIMC_BUG(!subdev);
228
229 eeprom = (struct fimc_is_eeprom *)v4l2_get_subdevdata(subdev);
230
231 FIMC_BUG(!eeprom);
232
233 crc_value = ((eeprom->data[EEPROM_LSC_CRC_SEC] << 8) | (eeprom->data[EEPROM_LSC_CRC_FST]));
234
235 crc16 = fimc_is_sensor_eeprom_check_crc(&eeprom->data[EEPROM_LSC_CRC_CHK_START], EEPROM_LSC_CRC_CHK_SIZE);
236 if (crc_value != crc16) {
237 err("Error to LSC CRC16: 0x%x, cal_buffer CRC: 0x%x", crc16, crc_value);
238
239 ret = -EINVAL;
240 } else
241 info("LSC CRC16: 0x%x, cal_buffer CRC: 0x%x\n", crc16, crc_value);
242
243 return ret;
244 }
245
246 static int fimc_is_eeprom_16885c_check_sfr(struct v4l2_subdev *subdev)
247 {
248 int ret = 0;
249 u16 crc_value = 0;
250 u16 crc16 = 0;
251 struct fimc_is_eeprom *eeprom = NULL;
252
253 FIMC_BUG(!subdev);
254
255 eeprom = (struct fimc_is_eeprom *)v4l2_get_subdevdata(subdev);
256
257 FIMC_BUG(!eeprom);
258
259 crc_value = ((eeprom->data[EEPROM_SFR_CRC_SEC] << 8) | (eeprom->data[EEPROM_SFR_CRC_FST]));
260
261 crc16 = fimc_is_sensor_eeprom_check_crc(&eeprom->data[EEPROM_SFR_CRC_CHK_START], EEPROM_SFR_CRC_CHK_SIZE);
262 if (crc_value != crc16) {
263 err("Error to SFR CRC16: 0x%x, cal_buffer CRC: 0x%x", crc16, crc_value);
264
265 ret = -EINVAL;
266 } else
267 info("SFR CRC16: 0x%x, cal_buffer CRC: 0x%x\n", crc16, crc_value);
268
269 return ret;
270 }
271
272 int fimc_is_eeprom_16885c_get_cal_data(struct v4l2_subdev *subdev)
273 {
274 int ret = 0;
275 struct fimc_is_eeprom *eeprom;
276 struct i2c_client *client;
277
278 FIMC_BUG(!subdev);
279
280 eeprom = (struct fimc_is_eeprom *)v4l2_get_subdevdata(subdev);
281
282 FIMC_BUG(!eeprom);
283
284 client = eeprom->client;
285 if (unlikely(!client)) {
286 err("client is NULL");
287 ret = -EINVAL;
288 return ret;
289 }
290
291 /*
292 * EEPROM data file(.bin) will be deleted at every reboot
293 * so, when eeprom->file_write is zero, there is no eeprom data file
294 */
295 if (!eeprom->file_write) {
296 I2C_MUTEX_LOCK(eeprom->i2c_lock);
297 /* I2C read to Sensor EEPROM cal data */
298 ret = fimc_is_eeprom_module_read(client, EEPROM_ADD_CRC_FST, eeprom->data, EEPROM_DATA_SIZE);
299 if (ret < 0) {
300 err("%s(): eeprom i2c read failed(%d)\n", __func__, ret);
301 I2C_MUTEX_UNLOCK(eeprom->i2c_lock);
302 return ret;
303 }
304 I2C_MUTEX_UNLOCK(eeprom->i2c_lock);
305
306 /* CRC check to each section cal data */
307 ret = CALL_EEPROMOPS(eeprom, eeprom_check_all_crc, subdev);
308 if (ret)
309 err("%s(): eeprom data invalid(%d)\n", __func__, ret);
310
311 /* Write file to Cal data */
312 ret = fimc_is_eeprom_file_write(EEPROM_DATA_PATH, (void *)eeprom->data, EEPROM_DATA_SIZE);
313 if (ret < 0) {
314 err("%s(), eeprom file write fail(%d)\n", __func__, ret);
315 return ret;
316 }
317 eeprom->file_write = 1;
318 } else {
319 ret = CALL_EEPROMOPS(eeprom, eeprom_check_all_crc, subdev);
320 if (ret) {
321 err("%s(): eeprom data invalid(%d)\n", __func__, ret);
322 err("retry to read data from eeprom module and update file\n");
323
324 I2C_MUTEX_LOCK(eeprom->i2c_lock);
325 /* I2C read to Sensor EEPROM cal data */
326 ret = fimc_is_eeprom_module_read(client, EEPROM_ADD_CRC_FST, eeprom->data, EEPROM_DATA_SIZE);
327 if (ret < 0) {
328 err("%s(): eeprom i2c read failed(%d)\n", __func__, ret);
329 I2C_MUTEX_UNLOCK(eeprom->i2c_lock);
330 return ret;
331 }
332 I2C_MUTEX_UNLOCK(eeprom->i2c_lock);
333
334 /* CRC check to each section cal data */
335 ret = CALL_EEPROMOPS(eeprom, eeprom_check_all_crc, subdev);
336 if (ret)
337 err("%s(): eeprom data invalid(%d)\n", __func__, ret);
338 }
339 }
340
341 return ret;
342 }
343
344 static struct fimc_is_eeprom_ops sensor_eeprom_ops = {
345 .eeprom_read = fimc_is_eeprom_16885c_get_cal_data,
346 .eeprom_check_all_crc = fimc_is_eeprom_16885c_check_all_crc,
347 .eeprom_check_address = fimc_is_eeprom_16885c_check_address,
348 .eeprom_check_info = fimc_is_eeprom_16885c_check_info,
349 .eeprom_check_awb = fimc_is_eeprom_16885c_check_awb,
350 .eeprom_check_lsc = fimc_is_eeprom_16885c_check_lsc,
351 .eeprom_check_sfr = fimc_is_eeprom_16885c_check_sfr,
352 };
353
354 static int sensor_eeprom_16885c_probe(struct i2c_client *client,
355 const struct i2c_device_id *id)
356 {
357 int ret = 0, i;
358 struct fimc_is_core *core;
359 struct v4l2_subdev *subdev_eeprom = NULL;
360 struct fimc_is_eeprom *eeprom = NULL;
361 struct fimc_is_device_sensor *device;
362 struct device *dev;
363 struct device_node *dnode;
364 u32 sensor_id = 0;
365
366 FIMC_BUG(!client);
367 FIMC_BUG(!fimc_is_dev);
368
369 core = (struct fimc_is_core *)dev_get_drvdata(fimc_is_dev);
370 if (!core) {
371 probe_info("core device is not yet probed");
372 return -EPROBE_DEFER;
373 }
374
375 dev = &client->dev;
376 dnode = dev->of_node;
377
378 ret = of_property_read_u32(dnode, "id", &sensor_id);
379 if (ret) {
380 probe_info("core device is not yet probed");
381 return -EPROBE_DEFER;
382 }
383
384 device = &core->sensor[sensor_id];
385 if (!device) {
386 err("sensor device is NULL");
387 ret = -ENOMEM;
388 goto p_err;
389 }
390
391 eeprom = kzalloc(sizeof(struct fimc_is_eeprom), GFP_KERNEL);
392 if (!eeprom) {
393 err("eeprom is NULL");
394 ret = -ENOMEM;
395 goto p_err;
396 }
397
398 subdev_eeprom = kzalloc(sizeof(struct v4l2_subdev), GFP_KERNEL);
399 if (!subdev_eeprom) {
400 probe_err("subdev_cis NULL");
401 ret = -ENOMEM;
402 goto p_err;
403 }
404
405 eeprom->data = kzalloc(EEPROM_DATA_SIZE, GFP_KERNEL);
406 if (!eeprom->data) {
407 err("data is NULL");
408 ret = -ENOMEM;
409 goto p_err;
410 }
411
412 eeprom->id = EEPROM_NAME_16885C;
413 eeprom->subdev = subdev_eeprom;
414 eeprom->device = sensor_id;
415 eeprom->client = client;
416 eeprom->i2c_lock = NULL;
417 eeprom->total_size = EEPROM_DATA_SIZE;
418 eeprom->eeprom_ops = &sensor_eeprom_ops;
419
420 for (i = 0; i < CAMERA_CRC_INDEX_MAX; i++)
421 device->cal_status[i] = CRC_NO_ERROR;
422
423 device->subdev_eeprom = subdev_eeprom;
424 device->eeprom = eeprom;
425
426 v4l2_set_subdevdata(subdev_eeprom, eeprom);
427 v4l2_set_subdev_hostdata(subdev_eeprom, device);
428
429 snprintf(subdev_eeprom->name, V4L2_SUBDEV_NAME_SIZE, "eeprom-subdev.%d", eeprom->id);
430
431 probe_info("%s done\n", __func__);
432
433 p_err:
434 return ret;
435 }
436
437 static const struct of_device_id sensor_eeprom_16885c_match[] = {
438 {
439 .compatible = "samsung,exynos5-fimc-is-sensor-eeprom-16885c",
440 },
441 {},
442 };
443 MODULE_DEVICE_TABLE(of, sensor_eeprom_16885c_match);
444
445 static const struct i2c_device_id sensor_eeprom_16885c_idt[] = {
446 { SENSOR_EEPROM_NAME, 0 },
447 {},
448 };
449
450 static struct i2c_driver sensor_eeprom_16885c_driver = {
451 .probe = sensor_eeprom_16885c_probe,
452 .driver = {
453 .name = SENSOR_EEPROM_NAME,
454 .owner = THIS_MODULE,
455 .of_match_table = sensor_eeprom_16885c_match,
456 .suppress_bind_attrs = true,
457 },
458 .id_table = sensor_eeprom_16885c_idt
459 };
460
461 static int __init sensor_eeprom_16885c_init(void)
462 {
463 int ret;
464
465 ret = i2c_add_driver(&sensor_eeprom_16885c_driver);
466 if (ret)
467 err("failed to add %s driver: %d\n",
468 sensor_eeprom_16885c_driver.driver.name, ret);
469
470 return ret;
471 }
472 late_initcall_sync(sensor_eeprom_16885c_init);