3f66cf928ad25fcf32f2e3f9f2ab752d7caf41b4
[GitHub/MotorolaMobilityLLC/kernel-slsi.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-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"
28
29 #define SENSOR_EEPROM_NAME "12A10FF"
30
31 int fimc_is_eeprom_12a10ff_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
38 FIMC_BUG(!subdev);
39
40 module = (struct fimc_is_module_enum *)v4l2_get_subdev_hostdata(subdev);
41
42 eeprom = (struct fimc_is_eeprom *)v4l2_get_subdevdata(subdev);
43
44 FIMC_BUG(!eeprom);
45 FIMC_BUG(!module);
46
47 sensor = (struct fimc_is_device_sensor *)v4l2_get_subdev_hostdata(module->subdev);
48 if (!sensor) {
49 err("device sensor is NULL");
50 ret = -ENODEV;
51 return ret;
52 }
53
54 /* Check CRC to Address cal data */
55 ret = CALL_EEPROMOPS(eeprom, eeprom_check_address, subdev);
56 if (ret) {
57 err("%s(): 12A10FF EEPROM Address section CRC check fail(%d)", __func__, ret);
58
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);
62
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;
66
67 return ret;
68 } else
69 info("12A10FF EEPROM Address section CRC check success\n");
70
71 /* Check CRC to Information cal data */
72 ret = CALL_EEPROMOPS(eeprom, eeprom_check_info, subdev);
73 if (ret) {
74 err("%s(): 12A10FF EEPROM Information CRC section check fail(%d)", __func__, ret);
75
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);
79
80 sensor->cal_status[CAMERA_CRC_INDEX_MNF] = CRC_ERROR;
81
82 } else {
83 info("12A10FF EEPROM Informaion section CRC check success\n");
84
85 sensor->cal_status[CAMERA_CRC_INDEX_MNF] = CRC_NO_ERROR;
86 }
87
88 /* Check CRC to AWB cal data */
89 ret = CALL_EEPROMOPS(eeprom, eeprom_check_awb, subdev);
90 if (ret) {
91 err("%s(): 12A10FF EEPROM AWB section CRC check fail(%d)", __func__, ret);
92
93 fimc_is_eeprom_cal_data_set(eeprom->data, "AWB",
94 EEPROM_AWB_CRC_SEC, EEPROM_AWB_CAL_SIZE, 0xff);
95
96 sensor->cal_status[CAMERA_CRC_INDEX_AWB] = CRC_ERROR;
97
98 } else {
99 info("12A10FF EEPROM AWB section CRC check success\n");
100
101 sensor->cal_status[CAMERA_CRC_INDEX_AWB] = CRC_NO_ERROR;
102
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]);
105 if (ret) {
106 err("%s(): 12A10FF EEPROM AWB ratio out of limit(%d)", __func__, ret);
107
108 sensor->cal_status[CAMERA_CRC_INDEX_AWB] = LIMIT_FAILURE;
109 }
110 }
111
112 /* Check CRC to LSC cal data */
113 ret = CALL_EEPROMOPS(eeprom, eeprom_check_lsc, subdev);
114 if (ret) {
115 err("%s(): 12A10FF EEPROM LSC section CRC check fail(%d)", __func__, ret);
116
117 fimc_is_eeprom_cal_data_set(eeprom->data, "LSC",
118 EEPROM_LSC_CRC_SEC, EEPROM_LSC_CAL_SIZE, 0xff);
119
120 } else
121 info("12A10FF EEPROM LSC section CRC check success\n");
122
123 /* Check CRC to SFR cal data */
124 ret = CALL_EEPROMOPS(eeprom, eeprom_check_sfr, subdev);
125 if (ret) {
126 err("%s(): EEPROM SFR section CRC check fail(%d)", __func__, ret);
127
128 fimc_is_eeprom_cal_data_set(eeprom->data, "SFR",
129 EEPROM_SFR_CRC_SEC, EEPROM_SFR_CAL_SIZE, 0xff);
130
131 } else
132 info("12A10FF EEPROM SFR section CRC check success\n");
133
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);
137 if (ret < 0)
138 err("%s(), DUAL cal file write fail(%d)", __func__, ret);
139
140 return ret;
141 }
142
143 static int fimc_is_eeprom_12a10ff_check_address(struct v4l2_subdev *subdev)
144 {
145 int ret = 0;
146 u16 crc_value = 0;
147 u16 crc16 = 0;
148 struct fimc_is_eeprom *eeprom = NULL;
149
150 FIMC_BUG(!subdev);
151
152 eeprom = (struct fimc_is_eeprom *)v4l2_get_subdevdata(subdev);
153
154 FIMC_BUG(!eeprom);
155
156 crc_value = ((eeprom->data[EEPROM_ADD_CRC_SEC] << 8) | (eeprom->data[EEPROM_ADD_CRC_FST]));
157
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);
161
162 ret = -EINVAL;
163 } else
164 info("ADD CRC16: 0x%x, cal_buffer CRC: 0x%x\n", crc16, crc_value);
165
166 return ret;
167 }
168
169 static int fimc_is_eeprom_12a10ff_check_info(struct v4l2_subdev *subdev)
170 {
171 int ret = 0;
172 u16 crc_value = 0;
173 u16 crc16 = 0;
174 struct fimc_is_eeprom *eeprom = NULL;
175
176 FIMC_BUG(!subdev);
177
178 eeprom = (struct fimc_is_eeprom *)v4l2_get_subdevdata(subdev);
179
180 FIMC_BUG(!eeprom);
181
182 crc_value = ((eeprom->data[EEPROM_INFO_CRC_SEC] << 8) | (eeprom->data[EEPROM_INFO_CRC_FST]));
183
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);
187 else
188 info("INFO CRC16: 0x%x, cal_buffer CRC: 0x%x\n", crc16, crc_value);
189
190 return ret;
191 }
192
193 static int fimc_is_eeprom_12a10ff_check_awb(struct v4l2_subdev *subdev)
194 {
195 int ret = 0;
196 u16 crc_value = 0;
197 u16 crc16 = 0;
198 struct fimc_is_eeprom *eeprom = NULL;
199
200 FIMC_BUG(!subdev);
201
202 eeprom = (struct fimc_is_eeprom *)v4l2_get_subdevdata(subdev);
203
204 FIMC_BUG(!eeprom);
205
206 crc_value = ((eeprom->data[EEPROM_AWB_CRC_SEC] << 8) | (eeprom->data[EEPROM_AWB_CRC_FST]));
207
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);
211 else
212 info("AWB CRC16: 0x%x, cal_buffer CRC: 0x%x\n", crc16, crc_value);
213
214 return ret;
215 }
216
217 static int fimc_is_eeprom_12a10ff_check_lsc(struct v4l2_subdev *subdev)
218 {
219 int ret = 0;
220 u16 crc_value = 0;
221 u16 crc16 = 0;
222 struct fimc_is_eeprom *eeprom = NULL;
223
224 FIMC_BUG(!subdev);
225
226 eeprom = (struct fimc_is_eeprom *)v4l2_get_subdevdata(subdev);
227
228 FIMC_BUG(!eeprom);
229
230 crc_value = ((eeprom->data[EEPROM_LSC_CRC_SEC] << 8) | (eeprom->data[EEPROM_LSC_CRC_FST]));
231
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);
235 else
236 info("LSC CRC16: 0x%x, cal_buffer CRC: 0x%x\n", crc16, crc_value);
237
238 return ret;
239 }
240
241 static int fimc_is_eeprom_12a10ff_check_sfr(struct v4l2_subdev *subdev)
242 {
243 int ret = 0;
244 u16 crc_value = 0;
245 u16 crc16 = 0;
246 struct fimc_is_eeprom *eeprom = NULL;
247
248 FIMC_BUG(!subdev);
249
250 eeprom = (struct fimc_is_eeprom *)v4l2_get_subdevdata(subdev);
251
252 FIMC_BUG(!eeprom);
253
254 crc_value = ((eeprom->data[EEPROM_SFR_CRC_SEC] << 8) | (eeprom->data[EEPROM_SFR_CRC_FST]));
255
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);
259 else
260 info("SFR CRC16: 0x%x, cal_buffer CRC: 0x%x\n", crc16, crc_value);
261
262 return ret;
263 }
264
265 int fimc_is_eeprom_12a10ff_get_cal_data(struct v4l2_subdev *subdev)
266 {
267 int ret = 0;
268 struct fimc_is_eeprom *eeprom;
269 struct i2c_client *client;
270
271 FIMC_BUG(!subdev);
272
273 eeprom = (struct fimc_is_eeprom *)v4l2_get_subdevdata(subdev);
274
275 FIMC_BUG(!eeprom);
276
277 client = eeprom->client;
278 if (unlikely(!client)) {
279 err("client is NULL");
280 ret = -EINVAL;
281 return ret;
282 }
283
284 /*
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
288 */
289 ret = fimc_is_eeprom_file_read(EEPROM_DATA_PATH, (void *)eeprom->data, EEPROM_DATA_SIZE);
290 if (ret) {
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);
293 if (ret < 0) {
294 err("%s(): eeprom i2c read failed(%d)\n", __func__, ret);
295 return ret;
296 }
297
298 /* CRC check to each section cal data */
299 ret = CALL_EEPROMOPS(eeprom, eeprom_check_all_crc, subdev);
300 if (ret < 0)
301 err("%s(): eeprom data invalid(%d)\n", __func__, ret);
302
303 /* Write file to Cal data */
304 ret = fimc_is_eeprom_file_write(EEPROM_DATA_PATH, (void *)eeprom->data, EEPROM_DATA_SIZE);
305 if (ret < 0) {
306 err("%s(), eeprom file write fail(%d)\n", __func__, ret);
307 return ret;
308 }
309 } else {
310 /* CRC check to each section cal data */
311 ret = CALL_EEPROMOPS(eeprom, eeprom_check_all_crc, subdev);
312 if (ret < 0)
313 err("%s(): eeprom data invalid(%d)\n", __func__, ret);
314 }
315
316 return ret;
317 }
318
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,
327 };
328
329 static int sensor_eeprom_12a10ff_probe(struct i2c_client *client,
330 const struct i2c_device_id *id)
331 {
332 int ret = 0, i;
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;
337 struct device *dev;
338 struct device_node *dnode;
339 u32 sensor_id = 0;
340
341 FIMC_BUG(!client);
342 FIMC_BUG(!fimc_is_dev);
343
344 core = (struct fimc_is_core *)dev_get_drvdata(fimc_is_dev);
345 if (!core) {
346 probe_info("core device is not yet probed");
347 return -EPROBE_DEFER;
348 }
349
350 dev = &client->dev;
351 dnode = dev->of_node;
352
353 ret = of_property_read_u32(dnode, "id", &sensor_id);
354 if (ret) {
355 probe_info("core device is not yet probed");
356 return -EPROBE_DEFER;
357 }
358
359 device = &core->sensor[sensor_id];
360 if (!device) {
361 err("sensor device is NULL");
362 ret = -ENOMEM;
363 goto p_err;
364 }
365
366 eeprom = kzalloc(sizeof(struct fimc_is_eeprom), GFP_KERNEL);
367 if (!eeprom) {
368 err("eeprom is NULL");
369 ret = -ENOMEM;
370 goto p_err;
371 }
372
373 subdev_eeprom = kzalloc(sizeof(struct v4l2_subdev), GFP_KERNEL);
374 if (!subdev_eeprom) {
375 probe_err("subdev_cis NULL");
376 ret = -ENOMEM;
377 goto p_err;
378 }
379
380 eeprom->data = kzalloc(EEPROM_DATA_SIZE, GFP_KERNEL);
381 if (!eeprom->data) {
382 err("data is NULL");
383 ret = -ENOMEM;
384 goto p_err;
385 }
386
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;
394
395 for (i = 0; i < CAMERA_CRC_INDEX_MAX; i++)
396 device->cal_status[i] = CRC_NO_ERROR;
397
398 device->subdev_eeprom = subdev_eeprom;
399 device->eeprom = eeprom;
400
401 v4l2_set_subdevdata(subdev_eeprom, eeprom);
402 v4l2_set_subdev_hostdata(subdev_eeprom, device);
403
404 snprintf(subdev_eeprom->name, V4L2_SUBDEV_NAME_SIZE, "eeprom-subdev.%d", eeprom->id);
405
406 probe_info("%s done\n", __func__);
407
408 p_err:
409 return ret;
410 }
411
412 static const struct of_device_id sensor_eeprom_12a10ff_match[] = {
413 {
414 .compatible = "samsung,exynos5-fimc-is-sensor-eeprom-12a10ff",
415 },
416 {},
417 };
418 MODULE_DEVICE_TABLE(of, sensor_eeprom_12a10ff_match);
419
420 static const struct i2c_device_id sensor_eeprom_12a10ff_idt[] = {
421 { SENSOR_EEPROM_NAME, 0 },
422 {},
423 };
424
425 static struct i2c_driver sensor_eeprom_12a10ff_driver = {
426 .probe = sensor_eeprom_12a10ff_probe,
427 .driver = {
428 .name = SENSOR_EEPROM_NAME,
429 .owner = THIS_MODULE,
430 .of_match_table = sensor_eeprom_12a10ff_match,
431 .suppress_bind_attrs = true,
432 },
433 .id_table = sensor_eeprom_12a10ff_idt
434 };
435
436 static int __init sensor_eeprom_12a10ff_init(void)
437 {
438 int ret;
439
440 ret = i2c_add_driver(&sensor_eeprom_12a10ff_driver);
441 if (ret)
442 err("failed to add %s driver: %d\n",
443 sensor_eeprom_12a10ff_driver.driver.name, ret);
444
445 return ret;
446 }
447 late_initcall_sync(sensor_eeprom_12a10ff_init);