1 /*
2 * linux-5.4/drivers/media/platform/sunxi-vin/vin-cci/cci_helper.c
3 *
4 * Copyright (c) 2007-2017 Allwinnertech Co., Ltd.
5 *
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 */
16
17 #include <linux/module.h>
18 #include "cci_helper.h"
19 #include "../platform/platform_cfg.h"
20 #include "../modules/sensor/sensor_helper.h"
21 #include "../modules/sensor/camera.h"
22
23 #if defined(CONFIG_CCI_MODULE) || defined(CONFIG_CCI)
24 #include "bsp_cci.h"
25 #define USE_SPECIFIC_CCI
26 #endif
27
28 #ifdef USE_SPECIFIC_CCI
29 #define cci_print(x, arg...) pr_info("[VIN_DEV_CCI]"x, ##arg)
30 #define cci_err(x, arg...) pr_err("[VIN_DEV_CCI_ERR]"x, ##arg)
31 #else
32 #define cci_print(x, arg...) pr_info("[VIN_DEV_I2C]"x, ##arg)
33 #define cci_err(x, arg...) pr_err("[VIN_DEV_I2C_ERR]"x, ##arg)
34 #endif
35
cci_device_release(struct device * dev)36 static void cci_device_release(struct device *dev)
37 {
38
39 }
40 struct device cci_device_def = {
41 .release = cci_device_release,
42 };
43
44 #define SHOW_CCI_DEVICE_ATTR(name) \
45 static ssize_t cci_device_##name##_show(struct device *dev, \
46 struct device_attribute *attr, \
47 char *buf) \
48 {\
49 struct cci_driver *cci_drv = dev_get_drvdata(dev); \
50 cci_print("Get val = 0x%x !\n", cci_drv->name);\
51 return sprintf(buf, "0x%x\n", cci_drv->name); \
52 }
53
54 #define STORE_CCI_DEVICE_ATTR(name) \
55 static ssize_t cci_device_##name##_store(struct device *dev, \
56 struct device_attribute *attr, \
57 const char *buf,\
58 size_t count) \
59 {\
60 struct cci_driver *cci_drv = dev_get_drvdata(dev); \
61 unsigned int val;\
62 val = simple_strtoul(buf, NULL, 16);\
63 cci_drv->name = val;\
64 cci_print("Set val = 0x%x !\n", val);\
65 return count;\
66 }
67
68 SHOW_CCI_DEVICE_ATTR(addr_width)
SHOW_CCI_DEVICE_ATTR(data_width)69 SHOW_CCI_DEVICE_ATTR(data_width)
70 SHOW_CCI_DEVICE_ATTR(read_value)
71 SHOW_CCI_DEVICE_ATTR(read_flag)
72 STORE_CCI_DEVICE_ATTR(addr_width)
73 STORE_CCI_DEVICE_ATTR(data_width)
74 STORE_CCI_DEVICE_ATTR(read_flag)
75
76 static ssize_t cci_sys_show(struct device *dev,
77 struct device_attribute *attr, char *buf)
78 {
79 struct cci_driver *cci_drv = dev_get_drvdata(dev);
80
81 cci_print("echo reg(16)|val(16) > cci_client,eg: echo 30350011 > cci_client!\n");
82 cci_print("Note: current read_flag = %d\n", cci_drv->read_flag);
83 return sprintf(buf, "0x%x\n", cci_drv->read_flag);
84 }
85
cci_sys_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)86 static ssize_t cci_sys_store(struct device *dev,
87 struct device_attribute *attr, const char *buf,
88 size_t count)
89 {
90 static int val;
91 unsigned short reg, value;
92 struct cci_driver *cci_drv = dev_get_drvdata(dev);
93 struct v4l2_subdev *sd = cci_drv->sd;
94
95 val = simple_strtoul(buf, NULL, 16);
96 reg = (val >> 16) & 0xFFFF;
97 value = val & 0xFFFF;
98 if (cci_drv->read_flag == 0) {
99 cci_write(sd, (addr_type) reg, (addr_type) value);
100 cci_print("Write reg = 0x%x, write value = 0x%x\n", reg, value);
101 } else {
102 cci_read(sd, (addr_type) reg,
103 (addr_type *)&cci_drv->read_value);
104 cci_print("Read reg = 0x%x, read value = 0x%x\n", reg,
105 cci_drv->read_value);
106 }
107 return count;
108 }
109
110 static struct device_attribute cci_device_attrs[] = {
111 __ATTR(addr_width, S_IWUSR | S_IRUGO, cci_device_addr_width_show,
112 cci_device_addr_width_store),
113 __ATTR(data_width, S_IWUSR | S_IRUGO, cci_device_data_width_show,
114 cci_device_data_width_store),
115 __ATTR(read_value, S_IRUGO, cci_device_read_value_show, NULL),
116 __ATTR(read_flag, S_IWUSR | S_IRUGO, cci_device_read_flag_show,
117 cci_device_read_flag_store),
118 __ATTR(cci_client, S_IWUSR | S_IRUGO, cci_sys_show, cci_sys_store),
119 };
cci_sys_register(struct cci_driver * drv_data)120 static int cci_sys_register(struct cci_driver *drv_data)
121 {
122 int i, ret;
123
124 drv_data->cci_device = cci_device_def;
125 dev_set_name(&drv_data->cci_device, drv_data->name);
126 if (device_register(&drv_data->cci_device)) {
127 cci_err("error device_register()\n");
128 return -1;
129 }
130 dev_set_drvdata(&drv_data->cci_device, drv_data);
131 /* sysfs entries */
132 for (i = 0; i < ARRAY_SIZE(cci_device_attrs); i++) {
133 ret =
134 device_create_file(&drv_data->cci_device,
135 &cci_device_attrs[i]);
136 if (ret) {
137 cci_err("device_create_file error\n");
138 device_remove_file(&drv_data->cci_device,
139 &drv_data->dev_attr_cci);
140 }
141 }
142 return 0;
143 }
cci_sys_unregister(struct cci_driver * drv_data)144 static int cci_sys_unregister(struct cci_driver *drv_data)
145 {
146 int i;
147
148 for (i = 0; i < ARRAY_SIZE(cci_device_attrs); i++)
149 device_remove_file(&drv_data->cci_device, &cci_device_attrs[i]);
150 device_unregister(&drv_data->cci_device);
151 return 0;
152 }
153
sensor_registered(struct v4l2_subdev * sd)154 static int sensor_registered(struct v4l2_subdev *sd)
155 {
156 int ret;
157 struct sensor_info *info = to_state(sd);
158
159 mutex_lock(&info->lock);
160
161 #if !defined SENSOR_POER_BEFORE_VIN
162 v4l2_subdev_call(sd, core, s_power, PWR_ON);
163 #endif
164 sd->entity.use_count++;
165 ret = v4l2_subdev_call(sd, core, init, 0);
166 sd->entity.use_count--;
167 #if !defined SENSOR_POER_BEFORE_VIN
168 v4l2_subdev_call(sd, core, s_power, PWR_OFF);
169 #endif
170
171 mutex_unlock(&info->lock);
172
173 return ret;
174 }
175
176 static const struct v4l2_subdev_internal_ops sensor_internal_ops = {
177 .registered = sensor_registered,
178 };
179
180 static LIST_HEAD(cci_drv_list);
181 static int cci_dev_num_id;
182
cci_subdev_init(struct v4l2_subdev * sd,struct cci_driver * drv_data,const struct v4l2_subdev_ops * ops)183 static void cci_subdev_init(struct v4l2_subdev *sd, struct cci_driver *drv_data,
184 const struct v4l2_subdev_ops *ops)
185 {
186 v4l2_subdev_init(sd, ops);
187 /* initialize name */
188 snprintf(sd->name, sizeof(sd->name), "%s", drv_data->name);
189 drv_data->sd = sd;
190 drv_data->id = cci_dev_num_id;
191 drv_data->is_registerd = 1;
192 v4l2_set_subdevdata(sd, drv_data);
193
194 list_add(&drv_data->cci_list, &cci_drv_list);
195 cci_dev_num_id++;
196 }
197
cci_subdev_remove(struct cci_driver * cci_drv_p)198 static void cci_subdev_remove(struct cci_driver *cci_drv_p)
199 {
200 if (cci_drv_p->is_registerd == 0) {
201 cci_err("CCI subdev exit: this device is not registerd!\n");
202 return;
203 } else {
204 cci_drv_p->is_matched = 0;
205 cci_drv_p->is_registerd = 0;
206 if (cci_drv_p->sd == NULL)
207 cci_err("CCI subdev exit: cci_drv_p->sd is NULL!\n");
208 else
209 v4l2_set_subdevdata(cci_drv_p->sd, NULL);
210
211 list_del(&cci_drv_p->cci_list);
212 cci_dev_num_id--;
213 }
214 }
215
cci_bus_match(char * name,unsigned short cci_id,unsigned short cci_saddr)216 struct v4l2_subdev *cci_bus_match(char *name, unsigned short cci_id,
217 unsigned short cci_saddr)
218 {
219 struct cci_driver *cci_drv;
220
221 list_for_each_entry(cci_drv, &cci_drv_list, cci_list) {
222 if (cci_drv->is_registerd == 1 && cci_drv->is_matched == 0) {
223 if (!strcmp(cci_drv->name, name)) {
224 cci_drv->cci_id = cci_id;
225 cci_drv->cci_saddr = cci_saddr;
226 cci_drv->is_matched = 1;
227 return cci_drv->sd;
228 }
229 }
230 }
231 return NULL;
232 }
233 EXPORT_SYMBOL_GPL(cci_bus_match);
234
cci_bus_match_cancel(struct cci_driver * cci_drv_p)235 void cci_bus_match_cancel(struct cci_driver *cci_drv_p)
236 {
237 if (cci_drv_p->is_registerd == 0) {
238 cci_err("This device is not registerd!\n");
239 return;
240 } else {
241 if (cci_drv_p->is_matched == 0) {
242 cci_err("This device has not been matched!\n");
243 return;
244 } else {
245 cci_drv_p->is_matched = 0;
246 cci_drv_p->cci_id = 0;
247 cci_drv_p->cci_saddr = 0;
248 }
249 }
250 }
251 EXPORT_SYMBOL_GPL(cci_bus_match_cancel);
252
csi_cci_init_helper(unsigned int sel)253 void csi_cci_init_helper(unsigned int sel)
254 {
255 #ifdef USE_SPECIFIC_CCI
256 cci_s_power(sel, 1);
257 #endif
258 }
259 EXPORT_SYMBOL_GPL(csi_cci_init_helper);
260
csi_cci_exit_helper(unsigned int sel)261 void csi_cci_exit_helper(unsigned int sel)
262 {
263 #ifdef USE_SPECIFIC_CCI
264 cci_s_power(sel, 0);
265 #endif
266 }
267 EXPORT_SYMBOL_GPL(csi_cci_exit_helper);
268
cci_lock(struct v4l2_subdev * sd)269 void cci_lock(struct v4l2_subdev *sd)
270 {
271 #ifdef USE_SPECIFIC_CCI
272
273 #else
274 struct i2c_client *client = v4l2_get_subdevdata(sd);
275
276 i2c_mark_adapter_suspended(client->adapter);
277 #endif
278 }
279 EXPORT_SYMBOL_GPL(cci_lock);
280
cci_unlock(struct v4l2_subdev * sd)281 void cci_unlock(struct v4l2_subdev *sd)
282 {
283 #ifdef USE_SPECIFIC_CCI
284
285 #else
286 struct i2c_client *client = v4l2_get_subdevdata(sd);
287
288 i2c_mark_adapter_resumed(client->adapter);
289 #endif
290 }
291 EXPORT_SYMBOL_GPL(cci_unlock);
292
cci_dev_init_helper(struct i2c_driver * sensor_driver)293 int cci_dev_init_helper(struct i2c_driver *sensor_driver)
294 {
295 #ifdef USE_SPECIFIC_CCI
296 return sensor_driver->probe(NULL, NULL);
297 #else
298 return i2c_add_driver(sensor_driver);
299 #endif
300
301 }
302 EXPORT_SYMBOL_GPL(cci_dev_init_helper);
303
cci_dev_exit_helper(struct i2c_driver * sensor_driver)304 void cci_dev_exit_helper(struct i2c_driver *sensor_driver)
305 {
306 #ifdef USE_SPECIFIC_CCI
307 sensor_driver->remove(NULL);
308 #else
309 i2c_del_driver(sensor_driver);
310 #endif
311 }
312 EXPORT_SYMBOL_GPL(cci_dev_exit_helper);
313
cci_media_entity_init_helper(struct v4l2_subdev * sd,struct cci_driver * cci_drv)314 static int cci_media_entity_init_helper(struct v4l2_subdev *sd,
315 struct cci_driver *cci_drv)
316 {
317 struct sensor_info *si;
318
319 switch (cci_drv->type) {
320 case CCI_TYPE_SENSOR:
321 si = container_of(sd, struct sensor_info, sd);
322 BUG_ON(si->magic_num && (si->magic_num != SENSOR_MAGIC_NUMBER));
323 si->sensor_pads[SENSOR_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
324 sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
325 return media_entity_pads_init(&sd->entity, SENSOR_PAD_NUM, si->sensor_pads);
326 case CCI_TYPE_ACT:
327 sd->entity.function = MEDIA_ENT_F_LENS;
328 return media_entity_pads_init(&sd->entity, 0, NULL);
329 case CCI_TYPE_FLASH:
330 sd->entity.function = MEDIA_ENT_F_FLASH;
331 return media_entity_pads_init(&sd->entity, 0, NULL);
332 default:
333 return media_entity_pads_init(&sd->entity, 0, NULL);
334 }
335 }
336
cci_dev_probe_helper(struct v4l2_subdev * sd,struct i2c_client * client,const struct v4l2_subdev_ops * sensor_ops,struct cci_driver * cci_drv)337 int cci_dev_probe_helper(struct v4l2_subdev *sd, struct i2c_client *client,
338 const struct v4l2_subdev_ops *sensor_ops,
339 struct cci_driver *cci_drv)
340 {
341 if (client) {
342 v4l2_i2c_subdev_init(sd, client, sensor_ops);
343 /*change sd name to sensor driver name*/
344 snprintf(sd->name, sizeof(sd->name), "%s", cci_drv->name);
345 cci_drv->sd = sd;
346 } else {
347 cci_subdev_init(sd, cci_drv, sensor_ops);
348 }
349 v4l2_set_subdev_hostdata(sd, cci_drv);
350 sd->grp_id = VIN_GRP_ID_SENSOR;
351 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
352 if (cci_drv->type != CCI_TYPE_ACT)
353 sd->internal_ops = &sensor_internal_ops;
354 cci_sys_register(cci_drv);
355 cci_media_entity_init_helper(sd, cci_drv);
356
357 return 0;
358 }
359 EXPORT_SYMBOL_GPL(cci_dev_probe_helper);
360
cci_dev_remove_helper(struct i2c_client * client,struct cci_driver * cci_drv)361 struct v4l2_subdev *cci_dev_remove_helper(struct i2c_client *client,
362 struct cci_driver *cci_drv)
363 {
364 struct v4l2_subdev *sd;
365
366 if (client)
367 sd = i2c_get_clientdata(client);
368 else {
369 sd = cci_drv->sd;
370 cci_subdev_remove(cci_drv);
371 }
372 if (sd->entity.use_count > 0) {
373 sd->entity.use_count = 0;
374 sd->entity.stream_count = 0;
375 vin_err("%s is in using, cannot be removed!\n", sd->name);
376 v4l2_subdev_call(sd, core, s_power, PWR_OFF);
377 }
378 v4l2_set_subdev_hostdata(sd, NULL);
379 v4l2_device_unregister_subdev(sd);
380 cci_sys_unregister(cci_drv);
381 return sd;
382 }
383 EXPORT_SYMBOL_GPL(cci_dev_remove_helper);
384
385 /*
386 * On most platforms, we'd rather do straight i2c I/O.
387 */
388
cci_read_a8_d8(struct v4l2_subdev * sd,unsigned char addr,unsigned char * value)389 int cci_read_a8_d8(struct v4l2_subdev *sd, unsigned char addr,
390 unsigned char *value)
391 {
392 #ifdef USE_SPECIFIC_CCI
393 struct cci_driver *cci_drv = v4l2_get_subdevdata(sd);
394
395 return cci_rd_8_8(cci_drv->cci_id, addr, value, cci_drv->cci_saddr);
396 #else
397
398 __maybe_unused struct sensor_info *info = to_state(sd);
399 unsigned char data[2];
400 struct i2c_msg msg[2];
401 int ret;
402 struct i2c_client *client = v4l2_get_subdevdata(sd);
403
404 data[0] = addr;
405 data[1] = 0xee;
406 /*
407 * Send out the register address...
408 */
409 #ifdef CONFIG_SAME_I2C
410 if (sd->ops->video)
411 msg[0].addr = info->sensor_i2c_addr;
412 else
413 msg[0].addr = client->addr;
414 #else
415 msg[0].addr = client->addr;
416 #endif
417
418 msg[0].flags = 0;
419 msg[0].len = 1;
420 msg[0].buf = &data[0];
421 /*
422 * ...then read back the result.
423 */
424 #ifdef CONFIG_SAME_I2C
425 if (sd->ops->video)
426 msg[1].addr = info->sensor_i2c_addr;
427 else
428 msg[1].addr = client->addr;
429 #else
430 msg[1].addr = client->addr;
431 #endif
432
433 msg[1].flags = I2C_M_RD;
434 msg[1].len = 1;
435 msg[1].buf = &data[1];
436
437 ret = i2c_transfer(client->adapter, msg, 2);
438 if (ret >= 0) {
439 *value = data[1];
440 ret = 0;
441 }
442 return ret;
443 #endif
444 }
445 EXPORT_SYMBOL_GPL(cci_read_a8_d8);
446
cci_write_a8_d8(struct v4l2_subdev * sd,unsigned char addr,unsigned char value)447 int cci_write_a8_d8(struct v4l2_subdev *sd, unsigned char addr,
448 unsigned char value)
449 {
450 #ifdef USE_SPECIFIC_CCI
451 struct cci_driver *cci_drv = v4l2_get_subdevdata(sd);
452
453 return cci_wr_8_8(cci_drv->cci_id, addr, value, cci_drv->cci_saddr);
454 #else
455
456 __maybe_unused struct sensor_info *info = to_state(sd);
457 struct i2c_msg msg;
458 unsigned char data[2];
459 int ret;
460 struct i2c_client *client = v4l2_get_subdevdata(sd);
461
462 data[0] = addr;
463 data[1] = value;
464
465 #ifdef CONFIG_SAME_I2C
466 if (sd->ops->video)
467 msg.addr = info->sensor_i2c_addr;
468 else
469 msg.addr = client->addr;
470 #else
471 msg.addr = client->addr;
472 #endif
473
474 msg.flags = 0;
475 msg.len = 2;
476 msg.buf = data;
477
478 ret = i2c_transfer(client->adapter, &msg, 1);
479 if (ret >= 0)
480 ret = 0;
481 return ret;
482 #endif
483 }
484 EXPORT_SYMBOL_GPL(cci_write_a8_d8);
485
cci_read_a8_d16(struct v4l2_subdev * sd,unsigned char addr,unsigned short * value)486 int cci_read_a8_d16(struct v4l2_subdev *sd, unsigned char addr,
487 unsigned short *value)
488 {
489 #ifdef USE_SPECIFIC_CCI
490 struct cci_driver *cci_drv = v4l2_get_subdevdata(sd);
491
492 return cci_rd_8_16(cci_drv->cci_id, addr, value, cci_drv->cci_saddr);
493 #else
494
495 __maybe_unused struct sensor_info *info = to_state(sd);
496 unsigned char data[3];
497 struct i2c_msg msg[2];
498 int ret;
499 struct i2c_client *client = v4l2_get_subdevdata(sd);
500
501 data[0] = addr;
502 data[1] = 0xee;
503 data[2] = 0xee;
504 /*
505 * Send out the register address...
506 */
507 #ifdef CONFIG_SAME_I2C
508 if (sd->ops->video)
509 msg[0].addr = info->sensor_i2c_addr;
510 else
511 msg[0].addr = client->addr;
512 #else
513 msg[0].addr = client->addr;
514 #endif
515
516 msg[0].flags = 0;
517 msg[0].len = 1;
518 msg[0].buf = &data[0];
519 /*
520 * ...then read back the result.
521 */
522 #ifdef CONFIG_SAME_I2C
523 if (sd->ops->video)
524 msg[1].addr = info->sensor_i2c_addr;
525 else
526 msg[1].addr = client->addr;
527 #else
528 msg[1].addr = client->addr;
529 #endif
530
531 msg[1].flags = I2C_M_RD;
532 msg[1].len = 2;
533 msg[1].buf = &data[1];
534
535 ret = i2c_transfer(client->adapter, msg, 2);
536 if (ret >= 0) {
537 *value = data[1] * 256 + data[2];
538 ret = 0;
539 }
540 return ret;
541 #endif
542 }
543 EXPORT_SYMBOL_GPL(cci_read_a8_d16);
544
cci_write_a8_d16(struct v4l2_subdev * sd,unsigned char addr,unsigned short value)545 int cci_write_a8_d16(struct v4l2_subdev *sd, unsigned char addr,
546 unsigned short value)
547 {
548 #ifdef USE_SPECIFIC_CCI
549 struct cci_driver *cci_drv = v4l2_get_subdevdata(sd);
550
551 return cci_wr_8_16(cci_drv->cci_id, addr, value, cci_drv->cci_saddr);
552 #else
553
554 __maybe_unused struct sensor_info *info = to_state(sd);
555 struct i2c_msg msg;
556 unsigned char data[3];
557 int ret;
558 struct i2c_client *client = v4l2_get_subdevdata(sd);
559
560 data[0] = addr;
561 data[1] = (value & 0xff00) >> 8;
562 data[2] = (value & 0x00ff);
563
564 #ifdef CONFIG_SAME_I2C
565 if (sd->ops->video)
566 msg.addr = info->sensor_i2c_addr;
567 else
568 msg.addr = client->addr;
569 #else
570 msg.addr = client->addr;
571 #endif
572
573 msg.flags = 0;
574 msg.len = 3;
575 msg.buf = data;
576
577 ret = i2c_transfer(client->adapter, &msg, 1);
578 if (ret >= 0)
579 ret = 0;
580 return ret;
581 #endif
582 }
583 EXPORT_SYMBOL_GPL(cci_write_a8_d16);
584
cci_read_a16_d8(struct v4l2_subdev * sd,unsigned short addr,unsigned char * value)585 int cci_read_a16_d8(struct v4l2_subdev *sd, unsigned short addr,
586 unsigned char *value)
587 {
588 #ifdef USE_SPECIFIC_CCI
589 struct cci_driver *cci_drv = v4l2_get_subdevdata(sd);
590
591 return cci_rd_16_8(cci_drv->cci_id, addr, value, cci_drv->cci_saddr);
592 #else
593
594 __maybe_unused struct sensor_info *info = to_state(sd);
595 int ret;
596 unsigned char data[3];
597 struct i2c_msg msg[2];
598 struct i2c_client *client = v4l2_get_subdevdata(sd);
599
600 data[0] = (addr & 0xff00) >> 8;
601 data[1] = (addr & 0x00ff);
602 data[2] = 0xee;
603 /*
604 * Send out the register address...
605 */
606 #ifdef CONFIG_SAME_I2C
607 if (sd->ops->video)
608 msg[0].addr = info->sensor_i2c_addr;
609 else
610 msg[0].addr = client->addr;
611 #else
612 msg[0].addr = client->addr;
613 #endif
614
615 msg[0].flags = 0;
616 msg[0].len = 2;
617 msg[0].buf = &data[0];
618 /*
619 * ...then read back the result.
620 */
621 #ifdef CONFIG_SAME_I2C
622 if (sd->ops->video)
623 msg[1].addr = info->sensor_i2c_addr;
624 else
625 msg[1].addr = client->addr;
626 #else
627 msg[1].addr = client->addr;
628 #endif
629
630 msg[1].flags = I2C_M_RD;
631 msg[1].len = 1;
632 msg[1].buf = &data[2];
633
634 ret = i2c_transfer(client->adapter, msg, 2);
635 if (ret >= 0) {
636 *value = data[2];
637 ret = 0;
638 }
639 return ret;
640 #endif
641 }
642 EXPORT_SYMBOL_GPL(cci_read_a16_d8);
643
cci_write_a16_d8(struct v4l2_subdev * sd,unsigned short addr,unsigned char value)644 int cci_write_a16_d8(struct v4l2_subdev *sd, unsigned short addr,
645 unsigned char value)
646 {
647 #ifdef USE_SPECIFIC_CCI
648 struct cci_driver *cci_drv = v4l2_get_subdevdata(sd);
649
650 return cci_wr_16_8(cci_drv->cci_id, addr, value, cci_drv->cci_saddr);
651 #else
652
653 __maybe_unused struct sensor_info *info = to_state(sd);
654 int ret = 0;
655 struct i2c_msg msg;
656 unsigned char data[3];
657 struct i2c_client *client = v4l2_get_subdevdata(sd);
658
659 data[0] = (addr & 0xff00) >> 8;
660 data[1] = (addr & 0x00ff);
661 data[2] = value;
662
663 #ifdef CONFIG_SAME_I2C
664 if (sd->ops->video)
665 msg.addr = info->sensor_i2c_addr;
666 else
667 msg.addr = client->addr;
668 #else
669 msg.addr = client->addr;
670 #endif
671
672 msg.flags = 0;
673 msg.len = 3;
674 msg.buf = data;
675
676 ret = i2c_transfer(client->adapter, &msg, 1);
677 if (ret >= 0)
678 ret = 0;
679 return ret;
680 #endif
681 }
682 EXPORT_SYMBOL_GPL(cci_write_a16_d8);
683
cci_read_a16_d16(struct v4l2_subdev * sd,unsigned short addr,unsigned short * value)684 int cci_read_a16_d16(struct v4l2_subdev *sd, unsigned short addr,
685 unsigned short *value)
686 {
687 #ifdef USE_SPECIFIC_CCI
688 struct cci_driver *cci_drv = v4l2_get_subdevdata(sd);
689
690 return cci_rd_16_16(cci_drv->cci_id, addr, value, cci_drv->cci_saddr);
691 #else
692
693 __maybe_unused struct sensor_info *info = to_state(sd);
694 unsigned char data[4];
695 struct i2c_msg msg[2];
696 int ret;
697 struct i2c_client *client = v4l2_get_subdevdata(sd);
698
699 data[0] = (addr & 0xff00) >> 8;
700 data[1] = (addr & 0x00ff);
701 data[2] = 0xee;
702 data[3] = 0xee;
703 /*
704 * Send out the register address...
705 */
706 #ifdef CONFIG_SAME_I2C
707 if (sd->ops->video)
708 msg[0].addr = info->sensor_i2c_addr;
709 else
710 msg[0].addr = client->addr;
711 #else
712 msg[0].addr = client->addr;
713 #endif
714
715 msg[0].flags = 0;
716 msg[0].len = 2;
717 msg[0].buf = &data[0];
718 /*
719 * ...then read back the result.
720 */
721 #ifdef CONFIG_SAME_I2C
722 if (sd->ops->video)
723 msg[1].addr = info->sensor_i2c_addr;
724 else
725 msg[1].addr = client->addr;
726 #else
727 msg[1].addr = client->addr;
728 #endif
729
730 msg[1].flags = I2C_M_RD;
731 msg[1].len = 2;
732 msg[1].buf = &data[2];
733
734 ret = i2c_transfer(client->adapter, msg, 2);
735 if (ret >= 0) {
736 *value = data[2] * 256 + data[3];
737 ret = 0;
738 }
739 return ret;
740 #endif
741 }
742 EXPORT_SYMBOL_GPL(cci_read_a16_d16);
743
cci_write_a16_d16(struct v4l2_subdev * sd,unsigned short addr,unsigned short value)744 int cci_write_a16_d16(struct v4l2_subdev *sd, unsigned short addr,
745 unsigned short value)
746 {
747 #ifdef USE_SPECIFIC_CCI
748 struct cci_driver *cci_drv = v4l2_get_subdevdata(sd);
749
750 return cci_wr_16_16(cci_drv->cci_id, addr, value, cci_drv->cci_saddr);
751 #else
752
753 __maybe_unused struct sensor_info *info = to_state(sd);
754 struct i2c_msg msg;
755 unsigned char data[4];
756 int ret;
757 struct i2c_client *client = v4l2_get_subdevdata(sd);
758
759 data[0] = (addr & 0xff00) >> 8;
760 data[1] = (addr & 0x00ff);
761 data[2] = (value & 0xff00) >> 8;
762 data[3] = (value & 0x00ff);
763
764 #ifdef CONFIG_SAME_I2C
765 if (sd->ops->video)
766 msg.addr = info->sensor_i2c_addr;
767 else
768 msg.addr = client->addr;
769 #else
770 msg.addr = client->addr;
771 #endif
772
773 msg.flags = 0;
774 msg.len = 4;
775 msg.buf = data;
776
777 ret = i2c_transfer(client->adapter, &msg, 1);
778 if (ret >= 0)
779 ret = 0;
780 return ret;
781 #endif
782 }
783 EXPORT_SYMBOL_GPL(cci_write_a16_d16);
784
cci_read_a0_d16(struct v4l2_subdev * sd,unsigned short * value)785 int cci_read_a0_d16(struct v4l2_subdev *sd, unsigned short *value)
786 {
787 #ifdef USE_SPECIFIC_CCI
788 struct cci_driver *cci_drv = v4l2_get_subdevdata(sd);
789
790 return cci_rd_0_16(cci_drv->cci_id, value, cci_drv->cci_saddr);
791 #else
792
793 __maybe_unused struct sensor_info *info = to_state(sd);
794 struct i2c_msg msg;
795 unsigned char data[2];
796 int ret;
797 struct i2c_client *client = v4l2_get_subdevdata(sd);
798
799 data[0] = 0xee;
800 data[1] = 0xee;
801
802 #ifdef CONFIG_SAME_I2C
803 if (sd->ops->video)
804 msg.addr = info->sensor_i2c_addr;
805 else
806 msg.addr = client->addr;
807 #else
808 msg.addr = client->addr;
809 #endif
810
811 msg.flags = 1;
812 msg.len = 2;
813 msg.buf = &data[0];
814
815 ret = i2c_transfer(client->adapter, &msg, 1);
816 if (ret >= 0) {
817 *value = data[0] * 256 + data[1];
818 ret = 0;
819 }
820 return ret;
821 #endif
822 }
823 EXPORT_SYMBOL_GPL(cci_read_a0_d16);
824
cci_write_a0_d16(struct v4l2_subdev * sd,unsigned short value)825 int cci_write_a0_d16(struct v4l2_subdev *sd, unsigned short value)
826 {
827 #ifdef USE_SPECIFIC_CCI
828 struct cci_driver *cci_drv = v4l2_get_subdevdata(sd);
829
830 return cci_wr_0_16(cci_drv->cci_id, value, cci_drv->cci_saddr);
831 #else
832
833 __maybe_unused struct sensor_info *info = to_state(sd);
834 struct i2c_msg msg;
835 unsigned char data[2];
836 int ret;
837 struct i2c_client *client = v4l2_get_subdevdata(sd);
838
839 data[0] = (value & 0xff00) >> 8;
840 data[1] = (value & 0x00ff);
841
842 #ifdef CONFIG_SAME_I2C
843 if (sd->ops->video)
844 msg.addr = info->sensor_i2c_addr;
845 else
846 msg.addr = client->addr;
847 #else
848 msg.addr = client->addr;
849 #endif
850
851 msg.flags = 0;
852 msg.len = 2;
853 msg.buf = data;
854
855 ret = i2c_transfer(client->adapter, &msg, 1);
856 if (ret >= 0)
857 ret = 0;
858 return ret;
859 #endif
860 }
861 EXPORT_SYMBOL_GPL(cci_write_a0_d16);
862
cci_write_a16_d8_continuous_helper(struct v4l2_subdev * sd,unsigned short addr,unsigned char * vals,uint size)863 int cci_write_a16_d8_continuous_helper(struct v4l2_subdev *sd,
864 unsigned short addr, unsigned char *vals,
865 uint size)
866 {
867 #ifdef USE_SPECIFIC_CCI
868 struct cci_driver *cci_drv = v4l2_get_subdevdata(sd);
869
870 return cci_wr_a16_d8_continuous(cci_drv->cci_id, addr, vals, cci_drv->cci_saddr, size);
871 #else
872 __maybe_unused struct sensor_info *info = to_state(sd);
873 struct i2c_client *client = v4l2_get_subdevdata(sd);
874 struct i2c_msg msg;
875 unsigned char data[2 + 32];
876 unsigned char *p = vals;
877 int ret, i, len;
878
879 while (size > 0) {
880 len = size > 32 ? 32 : size;
881 data[0] = (addr & 0xff00) >> 8;
882 data[1] = (addr & 0x00ff);
883
884 for (i = 2; i < 2 + len; i++)
885 data[i] = *p++;
886
887 #ifdef CONFIG_SAME_I2C
888 if (sd->ops->video)
889 msg.addr = info->sensor_i2c_addr;
890 else
891 msg.addr = client->addr;
892 #else
893 msg.addr = client->addr;
894 #endif
895
896 msg.flags = 0;
897 msg.len = 2 + len;
898 msg.buf = data;
899
900 ret = i2c_transfer(client->adapter, &msg, 1);
901
902 if (ret > 0) {
903 ret = 0;
904 } else if (ret < 0) {
905 cci_err("sensor_write error!\n");
906 break;
907 }
908 addr += len;
909 size -= len;
910 }
911 return ret;
912 #endif
913 }
914 EXPORT_SYMBOL_GPL(cci_write_a16_d8_continuous_helper);
915
916 #if 0
917 #define MAX_TWI_DEPTH 127
918 int twi_write_ndata_by_dma(struct v4l2_subdev *sd, struct regval_list *regs, int array_size)
919 {
920 struct cci_driver *cci_drv = v4l2_get_subdev_hostdata(sd);
921 struct i2c_client *client = v4l2_get_subdevdata(sd);
922 struct i2c_msg *msg = kzalloc(MAX_TWI_DEPTH * sizeof(struct i2c_msg), GFP_KERNEL);
923 unsigned char data[MAX_TWI_DEPTH][4];
924 int ret = 0, i, len, valid_len = 0;
925
926 if (!regs || !array_size)
927 return -EINVAL;
928
929 while (array_size > 0) {
930 len = array_size > MAX_TWI_DEPTH ? MAX_TWI_DEPTH : array_size;
931 valid_len = 0;
932 for (i = 0; i < len; i++) {
933 if (regs->addr == REG_DLY) {
934 usleep_range(regs->data * 1000, regs->data * 1000 + 100);
935 regs++;
936 array_size--;
937 break;
938 }
939 if (8 == cci_drv->addr_width && 8 == cci_drv->data_width) {
940 data[valid_len][0] = regs->addr;
941 data[valid_len][1] = regs->data;
942 msg[valid_len].len = 2;
943 } else if (8 == cci_drv->addr_width && 16 == cci_drv->data_width) {
944 data[valid_len][0] = regs->addr;
945 data[valid_len][1] = (regs->data & 0xff00) >> 8;
946 data[valid_len][2] = (regs->data & 0x00ff);
947 msg[valid_len].len = 3;
948 } else if (16 == cci_drv->addr_width && 8 == cci_drv->data_width) {
949 data[valid_len][0] = (regs->addr & 0xff00) >> 8;
950 data[valid_len][1] = (regs->addr & 0x00ff);
951 data[valid_len][2] = regs->data;
952 msg[valid_len].len = 3;
953 } else if (16 == cci_drv->addr_width && 16 == cci_drv->data_width) {
954 data[valid_len][0] = (regs->addr & 0xff00) >> 8;
955 data[valid_len][1] = (regs->addr & 0x00ff);
956 data[valid_len][2] = (regs->data & 0xff00) >> 8;
957 data[valid_len][3] = (regs->data & 0x00ff);
958 msg[valid_len].len = 4;
959 } else {
960 kfree(msg);
961 return -1;
962 }
963
964 msg[valid_len].addr = client->addr;
965 msg[valid_len].flags = 0;
966 msg[valid_len].buf = &data[valid_len][0];
967 valid_len++;
968
969 regs++;
970 array_size--;
971 }
972 if (valid_len == 0)
973 continue;
974
975 usleep_range(2000, 3000);
976 cci_print("%s: write %d i2c_msg one time!\n", __func__, valid_len);
977
978 ret = i2c_transfer(client->adapter, msg, valid_len);
979 if (ret >= 0)
980 ret = 0;
981 else
982 break;
983 }
984
985 kfree(msg);
986
987 return ret;
988 }
989 #endif
990
cci_write(struct v4l2_subdev * sd,addr_type addr,data_type value)991 int cci_write(struct v4l2_subdev *sd, addr_type addr, data_type value)
992 {
993 struct cci_driver *cci_drv = v4l2_get_subdev_hostdata(sd);
994 int addr_width = cci_drv->addr_width;
995 int data_width = cci_drv->data_width;
996
997 if (8 == addr_width && 8 == data_width) {
998 return cci_write_a8_d8(sd, (unsigned char)addr, (unsigned char)value);
999 } else if (8 == addr_width && 16 == data_width) {
1000 return cci_write_a8_d16(sd, (unsigned char)addr, value);
1001 } else if (16 == addr_width && 8 == data_width) {
1002 return cci_write_a16_d8(sd, addr, (unsigned char)value);
1003 } else if (16 == addr_width && 16 == data_width) {
1004 return cci_write_a16_d16(sd, addr, value);
1005 } else if (0 == addr_width && 16 == data_width) {
1006 return cci_write_a0_d16(sd, value);
1007 } else {
1008 cci_err("at %s error, addr_width = %d , data_width = %d!\n ", __func__, addr_width, data_width);
1009 return -1;
1010 }
1011 }
1012 EXPORT_SYMBOL_GPL(cci_write);
1013
cci_read(struct v4l2_subdev * sd,addr_type addr,data_type * value)1014 int cci_read(struct v4l2_subdev *sd, addr_type addr, data_type *value)
1015 {
1016 struct cci_driver *cci_drv = v4l2_get_subdev_hostdata(sd);
1017 int addr_width = cci_drv->addr_width;
1018 int data_width = cci_drv->data_width;
1019
1020 *value = 0;
1021 if (8 == addr_width && 8 == data_width) {
1022 return cci_read_a8_d8(sd, (unsigned char)addr, (unsigned char *)value);
1023 } else if (8 == addr_width && 16 == data_width) {
1024 return cci_read_a8_d16(sd, (unsigned char)addr, value);
1025 } else if (16 == addr_width && 8 == data_width) {
1026 return cci_read_a16_d8(sd, addr, (unsigned char *)value);
1027 } else if (16 == addr_width && 16 == data_width) {
1028 return cci_read_a16_d16(sd, addr, value);
1029 } else if (0 == addr_width && 16 == data_width) {
1030 return cci_read_a0_d16(sd, value);
1031 } else {
1032 cci_err("%s error! addr_width = %d , data_width = %d!\n ", __func__, addr_width, data_width);
1033 return -1;
1034 }
1035 }
1036 EXPORT_SYMBOL_GPL(cci_read);
1037
1038 /*
1039 * On most platforms, we'd rather do straight i2c I/O.
1040 */
sensor_read(struct v4l2_subdev * sd,addr_type reg,data_type * value)1041 int sensor_read(struct v4l2_subdev *sd, addr_type reg, data_type *value)
1042 {
1043 int ret = 0, cnt = 0;
1044
1045 if (!sd || !sd->entity.use_count) {
1046 cci_err("%s error! sensor is not used!\n", __func__);
1047 return -1;
1048 }
1049
1050 ret = cci_read(sd, reg, value);
1051 while ((ret != 0) && (cnt < 2)) {
1052 ret = cci_read(sd, reg, value);
1053 cnt++;
1054 }
1055 if (cnt > 0)
1056 cci_print("%s sensor read retry = %d\n", sd->name, cnt);
1057
1058 return ret;
1059 }
1060 EXPORT_SYMBOL_GPL(sensor_read);
1061
sensor_write(struct v4l2_subdev * sd,addr_type reg,data_type value)1062 int sensor_write(struct v4l2_subdev *sd, addr_type reg, data_type value)
1063 {
1064 int ret = 0, cnt = 0;
1065
1066 if (!sd || !sd->entity.use_count) {
1067 cci_err("%s error! sensor is not used!\n", __func__);
1068 return -1;
1069 }
1070
1071 ret = cci_write(sd, reg, value);
1072 while ((ret != 0) && (cnt < 2)) {
1073 ret = cci_write(sd, reg, value);
1074 cnt++;
1075 }
1076 if (cnt > 0)
1077 cci_print("%s sensor write retry = %d\n", sd->name, cnt);
1078
1079 return ret;
1080 }
1081 EXPORT_SYMBOL_GPL(sensor_write);
1082
1083 /*
1084 * Write a list of register settings;
1085 */
sensor_write_array(struct v4l2_subdev * sd,struct regval_list * regs,int array_size)1086 int sensor_write_array(struct v4l2_subdev *sd, struct regval_list *regs, int array_size)
1087 {
1088 struct cci_driver *cci_drv = v4l2_get_subdev_hostdata(sd);
1089 int ret = 0, i = 0, len = 1;
1090 unsigned char data[32] = {0};
1091
1092 if (!regs)
1093 return -EINVAL;
1094
1095 while (i < array_size) {
1096 len = 1;
1097 if (regs->addr == REG_DLY) {
1098 usleep_range(regs->data * 1000, regs->data * 1000 + 100);
1099 } else {
1100 if (cci_drv->addr_width == CCI_BITS_16 && cci_drv->data_width == CCI_BITS_8) {
1101 while (regs[len-1].addr == (regs[len].addr - 1)) {
1102 data[len-1] = regs[len-1].data;
1103 len++;
1104 if (len-1 >= 31)
1105 break;
1106 }
1107 data[len-1] = regs[len-1].data;
1108 ret = cci_write_a16_d8_continuous_helper(sd, regs[0].addr, data, len);
1109 } else {
1110 #if 0 && defined (CONFIG_ARCH_SUN8IW16P1)
1111 len = array_size - i;
1112 ret = twi_write_ndata_by_dma(sd, regs, len);
1113 #else
1114 ret = sensor_write(sd, regs->addr, regs->data);
1115 #endif
1116 }
1117 if (ret < 0) {
1118 cci_err("%s sensor write array error, array_size %d!\n", sd->name, array_size);
1119 return -1;
1120 }
1121 }
1122 i += len;
1123 regs += len;
1124 }
1125 return 0;
1126 }
1127 EXPORT_SYMBOL_GPL(sensor_write_array);
1128
1129 /*
1130 * Read a list of register settings, only using for debug;
1131 */
sensor_read_array(struct v4l2_subdev * sd,struct regval_list * regs,int array_size)1132 int sensor_read_array(struct v4l2_subdev *sd, struct regval_list *regs, int array_size)
1133 {
1134 int ret = 0, i = 0, len = 1;
1135 data_type data;
1136
1137 if (!regs)
1138 return -EINVAL;
1139
1140 while (i < array_size) {
1141 len = 1;
1142 if (regs->addr == REG_DLY) {
1143 continue;
1144 } else {
1145 ret = sensor_read(sd, regs->addr, &data);
1146 if (ret < 0) {
1147 cci_err("%s sensor write array error, array_size %d!\n", sd->name, array_size);
1148 return -1;
1149 } else {
1150 cci_print("read from %s addr is 0x%x, data is 0x%x\n", sd->name, regs->addr, data);
1151 }
1152 }
1153 i += len;
1154 regs += len;
1155 }
1156 return 0;
1157 }
1158 EXPORT_SYMBOL_GPL(sensor_read_array);
1159