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-12a10ff.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 "12A10FF"
31 int fimc_is_eeprom_12a10ff_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;
40 module = (struct fimc_is_module_enum *)v4l2_get_subdev_hostdata(subdev);
42 eeprom = (struct fimc_is_eeprom *)v4l2_get_subdevdata(subdev);
47 sensor = (struct fimc_is_device_sensor *)v4l2_get_subdev_hostdata(module->subdev);
49 err("device sensor is NULL");
54 /* Check CRC to Address cal data */
55 ret = CALL_EEPROMOPS(eeprom, eeprom_check_address, subdev);
57 err("%s(): 12A10FF EEPROM Address section CRC check fail(%d)", __func__, ret);
59 /* All calibration data is zero set only Address section is invalid CRC */
60 fimc_is_eeprom_cal_data_set(eeprom->data, "all",
61 EEPROM_ADD_CRC_SEC, EEPROM_DATA_SIZE, 0xff);
63 /*Set all cal_status to ERROR if Address cal data invalid*/
64 for (int i = 0; i < CAMERA_CRC_INDEX_MAX; i++)
65 sensor->cal_status[i] = CRC_ERROR;
69 info("12A10FF EEPROM Address section CRC check success\n");
71 /* Check CRC to Information cal data */
72 ret = CALL_EEPROMOPS(eeprom, eeprom_check_info, subdev);
74 err("%s(): 12A10FF EEPROM Information CRC section check fail(%d)", __func__, ret);
76 /* All calibration data is 0xff set but exception Address section */
77 fimc_is_eeprom_cal_data_set(eeprom->data, "Information - End",
78 EEPROM_INFO_CRC_SEC, EEPROM_ADD_CAL_SIZE, 0xff);
80 sensor->cal_status[CAMERA_CRC_INDEX_MNF] = CRC_ERROR;
83 info("12A10FF EEPROM Informaion section CRC check success\n");
85 sensor->cal_status[CAMERA_CRC_INDEX_MNF] = CRC_NO_ERROR;
88 /* Check CRC to AWB cal data */
89 ret = CALL_EEPROMOPS(eeprom, eeprom_check_awb, subdev);
91 err("%s(): 12A10FF EEPROM AWB section CRC check fail(%d)", __func__, ret);
93 fimc_is_eeprom_cal_data_set(eeprom->data, "AWB",
94 EEPROM_AWB_CRC_SEC, EEPROM_AWB_CAL_SIZE, 0xff);
96 sensor->cal_status[CAMERA_CRC_INDEX_AWB] = CRC_ERROR;
99 info("12A10FF EEPROM AWB section CRC check success\n");
101 sensor->cal_status[CAMERA_CRC_INDEX_AWB] = CRC_NO_ERROR;
103 ret = fimc_is_sensor_eeprom_check_awb_ratio(&eeprom->data[EEPROM_AWB_UNIT_OFFSET],
104 &eeprom->data[EEPROM_AWB_GOLDEN_OFFSET],&eeprom->data[EEPROM_AWB_LIMIT_OFFSET]);
106 err("%s(): 12A10FF EEPROM AWB ratio out of limit(%d)", __func__, ret);
108 sensor->cal_status[CAMERA_CRC_INDEX_AWB] = LIMIT_FAILURE;
112 /* Check CRC to LSC cal data */
113 ret = CALL_EEPROMOPS(eeprom, eeprom_check_lsc, subdev);
115 err("%s(): 12A10FF EEPROM LSC section CRC check fail(%d)", __func__, ret);
117 fimc_is_eeprom_cal_data_set(eeprom->data, "LSC",
118 EEPROM_LSC_CRC_SEC, EEPROM_LSC_CAL_SIZE, 0xff);
121 info("12A10FF EEPROM LSC section CRC check success\n");
123 /* Check CRC to SFR cal data */
124 ret = CALL_EEPROMOPS(eeprom, eeprom_check_sfr, subdev);
126 err("%s(): EEPROM SFR section CRC check fail(%d)", __func__, ret);
128 fimc_is_eeprom_cal_data_set(eeprom->data, "SFR",
129 EEPROM_SFR_CRC_SEC, EEPROM_SFR_CAL_SIZE, 0xff);
132 info("12A10FF EEPROM SFR section CRC check success\n");
134 /* Write file to serial number of Information calibration data */
135 ret = fimc_is_eeprom_file_write(EEPROM_SERIAL_NUM_DATA_PATH,
136 (void *)&eeprom->data[EEPROM_INFO_SERIAL_NUM_START], EEPROM_INFO_SERIAL_NUM_SIZE);
138 err("%s(), DUAL cal file write fail(%d)", __func__, ret);
143 static int fimc_is_eeprom_12a10ff_check_address(struct v4l2_subdev *subdev)
148 struct fimc_is_eeprom *eeprom = NULL;
152 eeprom = (struct fimc_is_eeprom *)v4l2_get_subdevdata(subdev);
156 crc_value = ((eeprom->data[EEPROM_ADD_CRC_SEC] << 8) | (eeprom->data[EEPROM_ADD_CRC_FST]));
158 crc16 = fimc_is_sensor_eeprom_check_crc(&eeprom->data[EEPROM_ADD_CRC_CHK_START], EEPROM_ADD_CRC_CHK_SIZE);
159 if (crc_value != crc16) {
160 err("Error to ADD CRC16: 0x%x, cal_buffer CRC: 0x%x", crc16, crc_value);
164 info("ADD CRC16: 0x%x, cal_buffer CRC: 0x%x\n", crc16, crc_value);
169 static int fimc_is_eeprom_12a10ff_check_info(struct v4l2_subdev *subdev)
174 struct fimc_is_eeprom *eeprom = NULL;
178 eeprom = (struct fimc_is_eeprom *)v4l2_get_subdevdata(subdev);
182 crc_value = ((eeprom->data[EEPROM_INFO_CRC_SEC] << 8) | (eeprom->data[EEPROM_INFO_CRC_FST]));
184 crc16 = fimc_is_sensor_eeprom_check_crc(&eeprom->data[EEPROM_INFO_CRC_CHK_START], EEPROM_INFO_CRC_CHK_SIZE);
185 if (crc_value != crc16)
186 err("Error to INFO CRC16: 0x%x, cal_buffer CRC: 0x%x", crc16, crc_value);
188 info("INFO CRC16: 0x%x, cal_buffer CRC: 0x%x\n", crc16, crc_value);
193 static int fimc_is_eeprom_12a10ff_check_awb(struct v4l2_subdev *subdev)
198 struct fimc_is_eeprom *eeprom = NULL;
202 eeprom = (struct fimc_is_eeprom *)v4l2_get_subdevdata(subdev);
206 crc_value = ((eeprom->data[EEPROM_AWB_CRC_SEC] << 8) | (eeprom->data[EEPROM_AWB_CRC_FST]));
208 crc16 = fimc_is_sensor_eeprom_check_crc(&eeprom->data[EEPROM_AWB_CRC_CHK_START], EEPROM_AWB_CRC_CHK_SIZE);
209 if (crc_value != crc16)
210 err("Error to AWB CRC16: 0x%x, cal_buffer CRC: 0x%x", crc16, crc_value);
212 info("AWB CRC16: 0x%x, cal_buffer CRC: 0x%x\n", crc16, crc_value);
217 static int fimc_is_eeprom_12a10ff_check_lsc(struct v4l2_subdev *subdev)
222 struct fimc_is_eeprom *eeprom = NULL;
226 eeprom = (struct fimc_is_eeprom *)v4l2_get_subdevdata(subdev);
230 crc_value = ((eeprom->data[EEPROM_LSC_CRC_SEC] << 8) | (eeprom->data[EEPROM_LSC_CRC_FST]));
232 crc16 = fimc_is_sensor_eeprom_check_crc(&eeprom->data[EEPROM_LSC_CRC_CHK_START], EEPROM_LSC_CRC_CHK_SIZE);
233 if (crc_value != crc16)
234 err("Error to LSC CRC16: 0x%x, cal_buffer CRC: 0x%x", crc16, crc_value);
236 info("LSC CRC16: 0x%x, cal_buffer CRC: 0x%x\n", crc16, crc_value);
241 static int fimc_is_eeprom_12a10ff_check_sfr(struct v4l2_subdev *subdev)
246 struct fimc_is_eeprom *eeprom = NULL;
250 eeprom = (struct fimc_is_eeprom *)v4l2_get_subdevdata(subdev);
254 crc_value = ((eeprom->data[EEPROM_SFR_CRC_SEC] << 8) | (eeprom->data[EEPROM_SFR_CRC_FST]));
256 crc16 = fimc_is_sensor_eeprom_check_crc(&eeprom->data[EEPROM_SFR_CRC_CHK_START], EEPROM_SFR_CRC_CHK_SIZE);
257 if (crc_value != crc16)
258 err("Error to SFR CRC16: 0x%x, cal_buffer CRC: 0x%x", crc16, crc_value);
260 info("SFR CRC16: 0x%x, cal_buffer CRC: 0x%x\n", crc16, crc_value);
265 int fimc_is_eeprom_12a10ff_get_cal_data(struct v4l2_subdev *subdev)
268 struct fimc_is_eeprom *eeprom;
269 struct i2c_client *client;
273 eeprom = (struct fimc_is_eeprom *)v4l2_get_subdevdata(subdev);
277 client = eeprom->client;
278 if (unlikely(!client)) {
279 err("client is NULL");
285 * If already read at EEPROM data in module
286 * don't again read at EEPROM but there isn't file or
287 * data is NULL read EEPROM data
289 ret = fimc_is_eeprom_file_read(EEPROM_DATA_PATH, (void *)eeprom->data, EEPROM_DATA_SIZE);
291 /* I2C read to Sensor EEPROM cal data */
292 ret = fimc_is_eeprom_module_read(client, EEPROM_ADD_CRC_FST, eeprom->data, EEPROM_DATA_SIZE);
294 err("%s(): eeprom i2c read failed(%d)\n", __func__, ret);
298 /* CRC check to each section cal data */
299 ret = CALL_EEPROMOPS(eeprom, eeprom_check_all_crc, subdev);
301 err("%s(): eeprom data invalid(%d)\n", __func__, ret);
303 /* Write file to Cal data */
304 ret = fimc_is_eeprom_file_write(EEPROM_DATA_PATH, (void *)eeprom->data, EEPROM_DATA_SIZE);
306 err("%s(), eeprom file write fail(%d)\n", __func__, ret);
310 /* CRC check to each section cal data */
311 ret = CALL_EEPROMOPS(eeprom, eeprom_check_all_crc, subdev);
313 err("%s(): eeprom data invalid(%d)\n", __func__, ret);
319 static struct fimc_is_eeprom_ops sensor_eeprom_ops = {
320 .eeprom_read = fimc_is_eeprom_12a10ff_get_cal_data,
321 .eeprom_check_all_crc = fimc_is_eeprom_12a10ff_check_all_crc,
322 .eeprom_check_address = fimc_is_eeprom_12a10ff_check_address,
323 .eeprom_check_info = fimc_is_eeprom_12a10ff_check_info,
324 .eeprom_check_awb = fimc_is_eeprom_12a10ff_check_awb,
325 .eeprom_check_lsc = fimc_is_eeprom_12a10ff_check_lsc,
326 .eeprom_check_sfr = fimc_is_eeprom_12a10ff_check_sfr,
329 static int sensor_eeprom_12a10ff_probe(struct i2c_client *client,
330 const struct i2c_device_id *id)
333 struct fimc_is_core *core;
334 struct v4l2_subdev *subdev_eeprom = NULL;
335 struct fimc_is_eeprom *eeprom = NULL;
336 struct fimc_is_device_sensor *device;
338 struct device_node *dnode;
342 FIMC_BUG(!fimc_is_dev);
344 core = (struct fimc_is_core *)dev_get_drvdata(fimc_is_dev);
346 probe_info("core device is not yet probed");
347 return -EPROBE_DEFER;
351 dnode = dev->of_node;
353 ret = of_property_read_u32(dnode, "id", &sensor_id);
355 probe_info("core device is not yet probed");
356 return -EPROBE_DEFER;
359 device = &core->sensor[sensor_id];
361 err("sensor device is NULL");
366 eeprom = kzalloc(sizeof(struct fimc_is_eeprom), GFP_KERNEL);
368 err("eeprom is NULL");
373 subdev_eeprom = kzalloc(sizeof(struct v4l2_subdev), GFP_KERNEL);
374 if (!subdev_eeprom) {
375 probe_err("subdev_cis NULL");
380 eeprom->data = kzalloc(EEPROM_DATA_SIZE, GFP_KERNEL);
387 eeprom->id = EEPROM_NAME_12A10FF;
388 eeprom->subdev = subdev_eeprom;
389 eeprom->device = sensor_id;
390 eeprom->client = client;
391 eeprom->i2c_lock = NULL;
392 eeprom->total_size = EEPROM_DATA_SIZE;
393 eeprom->eeprom_ops = &sensor_eeprom_ops;
395 for (i = 0; i < CAMERA_CRC_INDEX_MAX; i++)
396 device->cal_status[i] = CRC_NO_ERROR;
398 device->subdev_eeprom = subdev_eeprom;
399 device->eeprom = eeprom;
401 v4l2_set_subdevdata(subdev_eeprom, eeprom);
402 v4l2_set_subdev_hostdata(subdev_eeprom, device);
404 snprintf(subdev_eeprom->name, V4L2_SUBDEV_NAME_SIZE, "eeprom-subdev.%d", eeprom->id);
406 probe_info("%s done\n", __func__);
412 static const struct of_device_id sensor_eeprom_12a10ff_match[] = {
414 .compatible = "samsung,exynos5-fimc-is-sensor-eeprom-12a10ff",
418 MODULE_DEVICE_TABLE(of, sensor_eeprom_12a10ff_match);
420 static const struct i2c_device_id sensor_eeprom_12a10ff_idt[] = {
421 { SENSOR_EEPROM_NAME, 0 },
425 static struct i2c_driver sensor_eeprom_12a10ff_driver = {
426 .probe = sensor_eeprom_12a10ff_probe,
428 .name = SENSOR_EEPROM_NAME,
429 .owner = THIS_MODULE,
430 .of_match_table = sensor_eeprom_12a10ff_match,
431 .suppress_bind_attrs = true,
433 .id_table = sensor_eeprom_12a10ff_idt
436 static int __init sensor_eeprom_12a10ff_init(void)
440 ret = i2c_add_driver(&sensor_eeprom_12a10ff_driver);
442 err("failed to add %s driver: %d\n",
443 sensor_eeprom_12a10ff_driver.driver.name, ret);
447 late_initcall_sync(sensor_eeprom_12a10ff_init);