2 * Samsung Exynos5 SoC series Sensor driver
5 * Copyright (c) 2018 Samsung Electronics Co., Ltd
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.
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>
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"
29 #define SENSOR_EEPROM_NAME "16885C"
31 int fimc_is_eeprom_16885c_check_all_crc(struct v4l2_subdev *subdev)
34 struct fimc_is_module_enum *module;
35 struct fimc_is_eeprom *eeprom = NULL;
36 struct fimc_is_device_sensor *sensor = NULL;
41 module = (struct fimc_is_module_enum *)v4l2_get_subdev_hostdata(subdev);
43 eeprom = (struct fimc_is_eeprom *)v4l2_get_subdevdata(subdev);
48 sensor = (struct fimc_is_device_sensor *)v4l2_get_subdev_hostdata(module->subdev);
50 err("device sensor is NULL");
55 /* Check CRC to Address cal data */
56 ret = CALL_EEPROMOPS(eeprom, eeprom_check_address, subdev);
58 err("%s(): 16885C EEPROM Address section CRC check fail(%d)", __func__, ret);
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);
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;
70 info("16885C EEPROM Address section CRC check success\n");
72 /* Check CRC to Information cal data */
73 ret = CALL_EEPROMOPS(eeprom, eeprom_check_info, subdev);
76 err("%s(): 16885C EEPROM Information CRC section check fail(%d)", __func__, ret);
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);
82 sensor->cal_status[CAMERA_CRC_INDEX_MNF] = CRC_ERROR;
85 info("16885C EEPROM Informaion section CRC check success\n");
87 sensor->cal_status[CAMERA_CRC_INDEX_MNF] = CRC_NO_ERROR;
90 /* Check CRC to AWB cal data */
91 ret = CALL_EEPROMOPS(eeprom, eeprom_check_awb, subdev);
94 err("%s(): 16885C EEPROM AWB section CRC check fail(%d)", __func__, ret);
96 fimc_is_eeprom_cal_data_set(eeprom->data, "AWB",
97 EEPROM_AWB_CRC_FST, EEPROM_AWB_CAL_SIZE, 0xff);
99 sensor->cal_status[CAMERA_CRC_INDEX_AWB] = CRC_ERROR;
102 info("16885C EEPROM AWB section CRC check success\n");
104 sensor->cal_status[CAMERA_CRC_INDEX_AWB] = CRC_NO_ERROR;
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]);
109 err("%s(): 16885C EEPROM AWB ratio out of limit(%d)", __func__, ret);
111 sensor->cal_status[CAMERA_CRC_INDEX_AWB] = LIMIT_FAILURE;
115 /* Check CRC to LSC cal data */
116 ret = CALL_EEPROMOPS(eeprom, eeprom_check_lsc, subdev);
119 err("%s(): 16885C EEPROM LSC section CRC check fail(%d)", __func__, ret);
121 fimc_is_eeprom_cal_data_set(eeprom->data, "LSC",
122 EEPROM_LSC_CRC_FST, EEPROM_LSC_CAL_SIZE, 0xff);
125 info("16885C EEPROM LSC section CRC check success\n");
127 /* Check CRC to SFR cal data */
128 ret = CALL_EEPROMOPS(eeprom, eeprom_check_sfr, subdev);
131 err("%s(): EEPROM SFR section CRC check fail(%d)", __func__, ret);
133 fimc_is_eeprom_cal_data_set(eeprom->data, "SFR",
134 EEPROM_SFR_CRC_FST, EEPROM_SFR_CAL_SIZE, 0xff);
137 info("16885C EEPROM SFR section CRC check success\n");
142 static int fimc_is_eeprom_16885c_check_address(struct v4l2_subdev *subdev)
147 struct fimc_is_eeprom *eeprom = NULL;
151 eeprom = (struct fimc_is_eeprom *)v4l2_get_subdevdata(subdev);
155 crc_value = ((eeprom->data[EEPROM_ADD_CRC_SEC] << 8) | (eeprom->data[EEPROM_ADD_CRC_FST]));
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);
163 info("ADD CRC16: 0x%x, cal_buffer CRC: 0x%x\n", crc16, crc_value);
168 static int fimc_is_eeprom_16885c_check_info(struct v4l2_subdev *subdev)
173 struct fimc_is_eeprom *eeprom = NULL;
177 eeprom = (struct fimc_is_eeprom *)v4l2_get_subdevdata(subdev);
181 crc_value = ((eeprom->data[EEPROM_INFO_CRC_SEC] << 8) | (eeprom->data[EEPROM_INFO_CRC_FST]));
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);
189 info("INFO CRC16: 0x%x, cal_buffer CRC: 0x%x\n", crc16, crc_value);
194 static int fimc_is_eeprom_16885c_check_awb(struct v4l2_subdev *subdev)
199 struct fimc_is_eeprom *eeprom = NULL;
203 eeprom = (struct fimc_is_eeprom *)v4l2_get_subdevdata(subdev);
207 crc_value = ((eeprom->data[EEPROM_AWB_CRC_SEC] << 8) | (eeprom->data[EEPROM_AWB_CRC_FST]));
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);
215 info("AWB CRC16: 0x%x, cal_buffer CRC: 0x%x\n", crc16, crc_value);
220 static int fimc_is_eeprom_16885c_check_lsc(struct v4l2_subdev *subdev)
225 struct fimc_is_eeprom *eeprom = NULL;
229 eeprom = (struct fimc_is_eeprom *)v4l2_get_subdevdata(subdev);
233 crc_value = ((eeprom->data[EEPROM_LSC_CRC_SEC] << 8) | (eeprom->data[EEPROM_LSC_CRC_FST]));
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);
241 info("LSC CRC16: 0x%x, cal_buffer CRC: 0x%x\n", crc16, crc_value);
246 static int fimc_is_eeprom_16885c_check_sfr(struct v4l2_subdev *subdev)
251 struct fimc_is_eeprom *eeprom = NULL;
255 eeprom = (struct fimc_is_eeprom *)v4l2_get_subdevdata(subdev);
259 crc_value = ((eeprom->data[EEPROM_SFR_CRC_SEC] << 8) | (eeprom->data[EEPROM_SFR_CRC_FST]));
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);
267 info("SFR CRC16: 0x%x, cal_buffer CRC: 0x%x\n", crc16, crc_value);
272 int fimc_is_eeprom_16885c_get_cal_data(struct v4l2_subdev *subdev)
275 struct fimc_is_eeprom *eeprom;
276 struct i2c_client *client;
280 eeprom = (struct fimc_is_eeprom *)v4l2_get_subdevdata(subdev);
284 client = eeprom->client;
285 if (unlikely(!client)) {
286 err("client is NULL");
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
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);
300 err("%s(): eeprom i2c read failed(%d)\n", __func__, ret);
301 I2C_MUTEX_UNLOCK(eeprom->i2c_lock);
304 I2C_MUTEX_UNLOCK(eeprom->i2c_lock);
306 /* CRC check to each section cal data */
307 ret = CALL_EEPROMOPS(eeprom, eeprom_check_all_crc, subdev);
309 err("%s(): eeprom data invalid(%d)\n", __func__, ret);
311 /* Write file to Cal data */
312 ret = fimc_is_eeprom_file_write(EEPROM_DATA_PATH, (void *)eeprom->data, EEPROM_DATA_SIZE);
314 err("%s(), eeprom file write fail(%d)\n", __func__, ret);
317 eeprom->file_write = 1;
319 ret = CALL_EEPROMOPS(eeprom, eeprom_check_all_crc, subdev);
321 err("%s(): eeprom data invalid(%d)\n", __func__, ret);
322 err("retry to read data from eeprom module and update file\n");
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);
328 err("%s(): eeprom i2c read failed(%d)\n", __func__, ret);
329 I2C_MUTEX_UNLOCK(eeprom->i2c_lock);
332 I2C_MUTEX_UNLOCK(eeprom->i2c_lock);
334 /* CRC check to each section cal data */
335 ret = CALL_EEPROMOPS(eeprom, eeprom_check_all_crc, subdev);
337 err("%s(): eeprom data invalid(%d)\n", __func__, ret);
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,
354 static int sensor_eeprom_16885c_probe(struct i2c_client *client,
355 const struct i2c_device_id *id)
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;
363 struct device_node *dnode;
367 FIMC_BUG(!fimc_is_dev);
369 core = (struct fimc_is_core *)dev_get_drvdata(fimc_is_dev);
371 probe_info("core device is not yet probed");
372 return -EPROBE_DEFER;
376 dnode = dev->of_node;
378 ret = of_property_read_u32(dnode, "id", &sensor_id);
380 probe_info("core device is not yet probed");
381 return -EPROBE_DEFER;
384 device = &core->sensor[sensor_id];
386 err("sensor device is NULL");
391 eeprom = kzalloc(sizeof(struct fimc_is_eeprom), GFP_KERNEL);
393 err("eeprom is NULL");
398 subdev_eeprom = kzalloc(sizeof(struct v4l2_subdev), GFP_KERNEL);
399 if (!subdev_eeprom) {
400 probe_err("subdev_cis NULL");
405 eeprom->data = kzalloc(EEPROM_DATA_SIZE, GFP_KERNEL);
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;
420 for (i = 0; i < CAMERA_CRC_INDEX_MAX; i++)
421 device->cal_status[i] = CRC_NO_ERROR;
423 device->subdev_eeprom = subdev_eeprom;
424 device->eeprom = eeprom;
426 v4l2_set_subdevdata(subdev_eeprom, eeprom);
427 v4l2_set_subdev_hostdata(subdev_eeprom, device);
429 snprintf(subdev_eeprom->name, V4L2_SUBDEV_NAME_SIZE, "eeprom-subdev.%d", eeprom->id);
431 probe_info("%s done\n", __func__);
437 static const struct of_device_id sensor_eeprom_16885c_match[] = {
439 .compatible = "samsung,exynos5-fimc-is-sensor-eeprom-16885c",
443 MODULE_DEVICE_TABLE(of, sensor_eeprom_16885c_match);
445 static const struct i2c_device_id sensor_eeprom_16885c_idt[] = {
446 { SENSOR_EEPROM_NAME, 0 },
450 static struct i2c_driver sensor_eeprom_16885c_driver = {
451 .probe = sensor_eeprom_16885c_probe,
453 .name = SENSOR_EEPROM_NAME,
454 .owner = THIS_MODULE,
455 .of_match_table = sensor_eeprom_16885c_match,
456 .suppress_bind_attrs = true,
458 .id_table = sensor_eeprom_16885c_idt
461 static int __init sensor_eeprom_16885c_init(void)
465 ret = i2c_add_driver(&sensor_eeprom_16885c_driver);
467 err("failed to add %s driver: %d\n",
468 sensor_eeprom_16885c_driver.driver.name, ret);
472 late_initcall_sync(sensor_eeprom_16885c_init);