1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Support for OmniVision OV5693 1080p HD camera sensor.
4 *
5 * Copyright (c) 2013 Intel Corporation. All Rights Reserved.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version
9 * 2 as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 *
17 */
18
19 #include <linux/module.h>
20 #include <linux/types.h>
21 #include <linux/kernel.h>
22 #include <linux/mm.h>
23 #include <linux/string.h>
24 #include <linux/errno.h>
25 #include <linux/init.h>
26 #include <linux/kmod.h>
27 #include <linux/device.h>
28 #include <linux/delay.h>
29 #include <linux/slab.h>
30 #include <linux/i2c.h>
31 #include <linux/moduleparam.h>
32 #include <media/v4l2-device.h>
33 #include <linux/io.h>
34 #include <linux/acpi.h>
35 #include "../../include/linux/atomisp_gmin_platform.h"
36
37 #include "ov5693.h"
38 #include "ad5823.h"
39
40 #define __cci_delay(t) \
41 do { \
42 if ((t) < 10) { \
43 usleep_range((t) * 1000, ((t) + 1) * 1000); \
44 } else { \
45 msleep((t)); \
46 } \
47 } while (0)
48
49 /* Value 30ms reached through experimentation on byt ecs.
50 * The DS specifies a much lower value but when using a smaller value
51 * the I2C bus sometimes locks up permanently when starting the camera.
52 * This issue could not be reproduced on cht, so we can reduce the
53 * delay value to a lower value when insmod.
54 */
55 static uint up_delay = 30;
56 module_param(up_delay, uint, 0644);
57 MODULE_PARM_DESC(up_delay,
58 "Delay prior to the first CCI transaction for ov5693");
59
vcm_ad_i2c_wr8(struct i2c_client * client,u8 reg,u8 val)60 static int vcm_ad_i2c_wr8(struct i2c_client *client, u8 reg, u8 val)
61 {
62 int err;
63 struct i2c_msg msg;
64 u8 buf[2];
65
66 buf[0] = reg;
67 buf[1] = val;
68
69 msg.addr = VCM_ADDR;
70 msg.flags = 0;
71 msg.len = 2;
72 msg.buf = &buf[0];
73
74 err = i2c_transfer(client->adapter, &msg, 1);
75 if (err != 1) {
76 dev_err(&client->dev, "%s: vcm i2c fail, err code = %d\n",
77 __func__, err);
78 return -EIO;
79 }
80 return 0;
81 }
82
ad5823_i2c_write(struct i2c_client * client,u8 reg,u8 val)83 static int ad5823_i2c_write(struct i2c_client *client, u8 reg, u8 val)
84 {
85 struct i2c_msg msg;
86 u8 buf[2];
87
88 buf[0] = reg;
89 buf[1] = val;
90 msg.addr = AD5823_VCM_ADDR;
91 msg.flags = 0;
92 msg.len = 0x02;
93 msg.buf = &buf[0];
94
95 if (i2c_transfer(client->adapter, &msg, 1) != 1)
96 return -EIO;
97 return 0;
98 }
99
ad5823_i2c_read(struct i2c_client * client,u8 reg,u8 * val)100 static int ad5823_i2c_read(struct i2c_client *client, u8 reg, u8 *val)
101 {
102 struct i2c_msg msg[2];
103 u8 buf[2];
104
105 buf[0] = reg;
106 buf[1] = 0;
107
108 msg[0].addr = AD5823_VCM_ADDR;
109 msg[0].flags = 0;
110 msg[0].len = 0x01;
111 msg[0].buf = &buf[0];
112
113 msg[1].addr = 0x0c;
114 msg[1].flags = I2C_M_RD;
115 msg[1].len = 0x01;
116 msg[1].buf = &buf[1];
117 *val = 0;
118 if (i2c_transfer(client->adapter, msg, 2) != 2)
119 return -EIO;
120 *val = buf[1];
121 return 0;
122 }
123
124 static const u32 ov5693_embedded_effective_size = 28;
125
126 /* i2c read/write stuff */
ov5693_read_reg(struct i2c_client * client,u16 data_length,u16 reg,u16 * val)127 static int ov5693_read_reg(struct i2c_client *client,
128 u16 data_length, u16 reg, u16 *val)
129 {
130 int err;
131 struct i2c_msg msg[2];
132 unsigned char data[6];
133
134 if (!client->adapter) {
135 dev_err(&client->dev, "%s error, no client->adapter\n",
136 __func__);
137 return -ENODEV;
138 }
139
140 if (data_length != OV5693_8BIT && data_length != OV5693_16BIT
141 && data_length != OV5693_32BIT) {
142 dev_err(&client->dev, "%s error, invalid data length\n",
143 __func__);
144 return -EINVAL;
145 }
146
147 memset(msg, 0, sizeof(msg));
148
149 msg[0].addr = client->addr;
150 msg[0].flags = 0;
151 msg[0].len = I2C_MSG_LENGTH;
152 msg[0].buf = data;
153
154 /* high byte goes out first */
155 data[0] = (u8)(reg >> 8);
156 data[1] = (u8)(reg & 0xff);
157
158 msg[1].addr = client->addr;
159 msg[1].len = data_length;
160 msg[1].flags = I2C_M_RD;
161 msg[1].buf = data;
162
163 err = i2c_transfer(client->adapter, msg, 2);
164 if (err != 2) {
165 if (err >= 0)
166 err = -EIO;
167 dev_err(&client->dev,
168 "read from offset 0x%x error %d", reg, err);
169 return err;
170 }
171
172 *val = 0;
173 /* high byte comes first */
174 if (data_length == OV5693_8BIT)
175 *val = (u8)data[0];
176 else if (data_length == OV5693_16BIT)
177 *val = be16_to_cpu(*(__be16 *)&data[0]);
178 else
179 *val = be32_to_cpu(*(__be32 *)&data[0]);
180
181 return 0;
182 }
183
ov5693_i2c_write(struct i2c_client * client,u16 len,u8 * data)184 static int ov5693_i2c_write(struct i2c_client *client, u16 len, u8 *data)
185 {
186 struct i2c_msg msg;
187 const int num_msg = 1;
188 int ret;
189
190 msg.addr = client->addr;
191 msg.flags = 0;
192 msg.len = len;
193 msg.buf = data;
194 ret = i2c_transfer(client->adapter, &msg, 1);
195
196 return ret == num_msg ? 0 : -EIO;
197 }
198
vcm_dw_i2c_write(struct i2c_client * client,u16 data)199 static int vcm_dw_i2c_write(struct i2c_client *client, u16 data)
200 {
201 struct i2c_msg msg;
202 const int num_msg = 1;
203 int ret;
204 __be16 val;
205
206 val = cpu_to_be16(data);
207 msg.addr = VCM_ADDR;
208 msg.flags = 0;
209 msg.len = OV5693_16BIT;
210 msg.buf = (void *)&val;
211
212 ret = i2c_transfer(client->adapter, &msg, 1);
213
214 return ret == num_msg ? 0 : -EIO;
215 }
216
217 /*
218 * Theory: per datasheet, the two VCMs both allow for a 2-byte read.
219 * The DW9714 doesn't actually specify what this does (it has a
220 * two-byte write-only protocol, but specifies the read sequence as
221 * legal), but it returns the same data (zeroes) always, after an
222 * undocumented initial NAK. The AD5823 has a one-byte address
223 * register to which all writes go, and subsequent reads will cycle
224 * through the 8 bytes of registers. Notably, the default values (the
225 * device is always power-cycled affirmatively, so we can rely on
226 * these) in AD5823 are not pairwise repetitions of the same 16 bit
227 * word. So all we have to do is sequentially read two bytes at a
228 * time and see if we detect a difference in any of the first four
229 * pairs.
230 */
vcm_detect(struct i2c_client * client)231 static int vcm_detect(struct i2c_client *client)
232 {
233 int i, ret;
234 struct i2c_msg msg;
235 u16 data0 = 0, data;
236
237 for (i = 0; i < 4; i++) {
238 msg.addr = VCM_ADDR;
239 msg.flags = I2C_M_RD;
240 msg.len = sizeof(data);
241 msg.buf = (u8 *)&data;
242 ret = i2c_transfer(client->adapter, &msg, 1);
243
244 /*
245 * DW9714 always fails the first read and returns
246 * zeroes for subsequent ones
247 */
248 if (i == 0 && ret == -EREMOTEIO) {
249 data0 = 0;
250 continue;
251 }
252
253 if (i == 0)
254 data0 = data;
255
256 if (data != data0)
257 return VCM_AD5823;
258 }
259 return ret == 1 ? VCM_DW9714 : ret;
260 }
261
ov5693_write_reg(struct i2c_client * client,u16 data_length,u16 reg,u16 val)262 static int ov5693_write_reg(struct i2c_client *client, u16 data_length,
263 u16 reg, u16 val)
264 {
265 int ret;
266 unsigned char data[4] = {0};
267 __be16 *wreg = (void *)data;
268 const u16 len = data_length + sizeof(u16); /* 16-bit address + data */
269
270 if (data_length != OV5693_8BIT && data_length != OV5693_16BIT) {
271 dev_err(&client->dev,
272 "%s error, invalid data_length\n", __func__);
273 return -EINVAL;
274 }
275
276 /* high byte goes out first */
277 *wreg = cpu_to_be16(reg);
278
279 if (data_length == OV5693_8BIT) {
280 data[2] = (u8)(val);
281 } else {
282 /* OV5693_16BIT */
283 __be16 *wdata = (void *)&data[2];
284
285 *wdata = cpu_to_be16(val);
286 }
287
288 ret = ov5693_i2c_write(client, len, data);
289 if (ret)
290 dev_err(&client->dev,
291 "write error: wrote 0x%x to offset 0x%x error %d",
292 val, reg, ret);
293
294 return ret;
295 }
296
297 /*
298 * ov5693_write_reg_array - Initializes a list of OV5693 registers
299 * @client: i2c driver client structure
300 * @reglist: list of registers to be written
301 *
302 * This function initializes a list of registers. When consecutive addresses
303 * are found in a row on the list, this function creates a buffer and sends
304 * consecutive data in a single i2c_transfer().
305 *
306 * __ov5693_flush_reg_array, __ov5693_buf_reg_array() and
307 * __ov5693_write_reg_is_consecutive() are internal functions to
308 * ov5693_write_reg_array_fast() and should be not used anywhere else.
309 *
310 */
311
__ov5693_flush_reg_array(struct i2c_client * client,struct ov5693_write_ctrl * ctrl)312 static int __ov5693_flush_reg_array(struct i2c_client *client,
313 struct ov5693_write_ctrl *ctrl)
314 {
315 u16 size;
316 __be16 *reg = (void *)&ctrl->buffer.addr;
317
318 if (ctrl->index == 0)
319 return 0;
320
321 size = sizeof(u16) + ctrl->index; /* 16-bit address + data */
322
323 *reg = cpu_to_be16(ctrl->buffer.addr);
324 ctrl->index = 0;
325
326 return ov5693_i2c_write(client, size, (u8 *)reg);
327 }
328
__ov5693_buf_reg_array(struct i2c_client * client,struct ov5693_write_ctrl * ctrl,const struct ov5693_reg * next)329 static int __ov5693_buf_reg_array(struct i2c_client *client,
330 struct ov5693_write_ctrl *ctrl,
331 const struct ov5693_reg *next)
332 {
333 int size;
334 __be16 *data16;
335
336 switch (next->type) {
337 case OV5693_8BIT:
338 size = 1;
339 ctrl->buffer.data[ctrl->index] = (u8)next->val;
340 break;
341 case OV5693_16BIT:
342 size = 2;
343
344 data16 = (void *)&ctrl->buffer.data[ctrl->index];
345 *data16 = cpu_to_be16((u16)next->val);
346 break;
347 default:
348 return -EINVAL;
349 }
350
351 /* When first item is added, we need to store its starting address */
352 if (ctrl->index == 0)
353 ctrl->buffer.addr = next->reg;
354
355 ctrl->index += size;
356
357 /*
358 * Buffer cannot guarantee free space for u32? Better flush it to avoid
359 * possible lack of memory for next item.
360 */
361 if (ctrl->index + sizeof(u16) >= OV5693_MAX_WRITE_BUF_SIZE)
362 return __ov5693_flush_reg_array(client, ctrl);
363
364 return 0;
365 }
366
__ov5693_write_reg_is_consecutive(struct i2c_client * client,struct ov5693_write_ctrl * ctrl,const struct ov5693_reg * next)367 static int __ov5693_write_reg_is_consecutive(struct i2c_client *client,
368 struct ov5693_write_ctrl *ctrl,
369 const struct ov5693_reg *next)
370 {
371 if (ctrl->index == 0)
372 return 1;
373
374 return ctrl->buffer.addr + ctrl->index == next->reg;
375 }
376
ov5693_write_reg_array(struct i2c_client * client,const struct ov5693_reg * reglist)377 static int ov5693_write_reg_array(struct i2c_client *client,
378 const struct ov5693_reg *reglist)
379 {
380 const struct ov5693_reg *next = reglist;
381 struct ov5693_write_ctrl ctrl;
382 int err;
383
384 ctrl.index = 0;
385 for (; next->type != OV5693_TOK_TERM; next++) {
386 switch (next->type & OV5693_TOK_MASK) {
387 case OV5693_TOK_DELAY:
388 err = __ov5693_flush_reg_array(client, &ctrl);
389 if (err)
390 return err;
391 msleep(next->val);
392 break;
393 default:
394 /*
395 * If next address is not consecutive, data needs to be
396 * flushed before proceed.
397 */
398 if (!__ov5693_write_reg_is_consecutive(client, &ctrl,
399 next)) {
400 err = __ov5693_flush_reg_array(client, &ctrl);
401 if (err)
402 return err;
403 }
404 err = __ov5693_buf_reg_array(client, &ctrl, next);
405 if (err) {
406 dev_err(&client->dev,
407 "%s: write error, aborted\n",
408 __func__);
409 return err;
410 }
411 break;
412 }
413 }
414
415 return __ov5693_flush_reg_array(client, &ctrl);
416 }
417
ov5693_g_focal(struct v4l2_subdev * sd,s32 * val)418 static int ov5693_g_focal(struct v4l2_subdev *sd, s32 *val)
419 {
420 *val = (OV5693_FOCAL_LENGTH_NUM << 16) | OV5693_FOCAL_LENGTH_DEM;
421 return 0;
422 }
423
ov5693_g_fnumber(struct v4l2_subdev * sd,s32 * val)424 static int ov5693_g_fnumber(struct v4l2_subdev *sd, s32 *val)
425 {
426 /*const f number for imx*/
427 *val = (OV5693_F_NUMBER_DEFAULT_NUM << 16) | OV5693_F_NUMBER_DEM;
428 return 0;
429 }
430
ov5693_g_fnumber_range(struct v4l2_subdev * sd,s32 * val)431 static int ov5693_g_fnumber_range(struct v4l2_subdev *sd, s32 *val)
432 {
433 *val = (OV5693_F_NUMBER_DEFAULT_NUM << 24) |
434 (OV5693_F_NUMBER_DEM << 16) |
435 (OV5693_F_NUMBER_DEFAULT_NUM << 8) | OV5693_F_NUMBER_DEM;
436 return 0;
437 }
438
ov5693_g_bin_factor_x(struct v4l2_subdev * sd,s32 * val)439 static int ov5693_g_bin_factor_x(struct v4l2_subdev *sd, s32 *val)
440 {
441 struct ov5693_device *dev = to_ov5693_sensor(sd);
442
443 *val = ov5693_res[dev->fmt_idx].bin_factor_x;
444
445 return 0;
446 }
447
ov5693_g_bin_factor_y(struct v4l2_subdev * sd,s32 * val)448 static int ov5693_g_bin_factor_y(struct v4l2_subdev *sd, s32 *val)
449 {
450 struct ov5693_device *dev = to_ov5693_sensor(sd);
451
452 *val = ov5693_res[dev->fmt_idx].bin_factor_y;
453
454 return 0;
455 }
456
ov5693_get_intg_factor(struct i2c_client * client,struct camera_mipi_info * info,const struct ov5693_resolution * res)457 static int ov5693_get_intg_factor(struct i2c_client *client,
458 struct camera_mipi_info *info,
459 const struct ov5693_resolution *res)
460 {
461 struct v4l2_subdev *sd = i2c_get_clientdata(client);
462 struct ov5693_device *dev = to_ov5693_sensor(sd);
463 struct atomisp_sensor_mode_data *buf = &info->data;
464 unsigned int pix_clk_freq_hz;
465 u16 reg_val;
466 int ret;
467
468 if (!info)
469 return -EINVAL;
470
471 /* pixel clock */
472 pix_clk_freq_hz = res->pix_clk_freq * 1000000;
473
474 dev->vt_pix_clk_freq_mhz = pix_clk_freq_hz;
475 buf->vt_pix_clk_freq_mhz = pix_clk_freq_hz;
476
477 /* get integration time */
478 buf->coarse_integration_time_min = OV5693_COARSE_INTG_TIME_MIN;
479 buf->coarse_integration_time_max_margin =
480 OV5693_COARSE_INTG_TIME_MAX_MARGIN;
481
482 buf->fine_integration_time_min = OV5693_FINE_INTG_TIME_MIN;
483 buf->fine_integration_time_max_margin =
484 OV5693_FINE_INTG_TIME_MAX_MARGIN;
485
486 buf->fine_integration_time_def = OV5693_FINE_INTG_TIME_MIN;
487 buf->frame_length_lines = res->lines_per_frame;
488 buf->line_length_pck = res->pixels_per_line;
489 buf->read_mode = res->bin_mode;
490
491 /* get the cropping and output resolution to ISP for this mode. */
492 ret = ov5693_read_reg(client, OV5693_16BIT,
493 OV5693_HORIZONTAL_START_H, ®_val);
494 if (ret)
495 return ret;
496 buf->crop_horizontal_start = reg_val;
497
498 ret = ov5693_read_reg(client, OV5693_16BIT,
499 OV5693_VERTICAL_START_H, ®_val);
500 if (ret)
501 return ret;
502 buf->crop_vertical_start = reg_val;
503
504 ret = ov5693_read_reg(client, OV5693_16BIT,
505 OV5693_HORIZONTAL_END_H, ®_val);
506 if (ret)
507 return ret;
508 buf->crop_horizontal_end = reg_val;
509
510 ret = ov5693_read_reg(client, OV5693_16BIT,
511 OV5693_VERTICAL_END_H, ®_val);
512 if (ret)
513 return ret;
514 buf->crop_vertical_end = reg_val;
515
516 ret = ov5693_read_reg(client, OV5693_16BIT,
517 OV5693_HORIZONTAL_OUTPUT_SIZE_H, ®_val);
518 if (ret)
519 return ret;
520 buf->output_width = reg_val;
521
522 ret = ov5693_read_reg(client, OV5693_16BIT,
523 OV5693_VERTICAL_OUTPUT_SIZE_H, ®_val);
524 if (ret)
525 return ret;
526 buf->output_height = reg_val;
527
528 buf->binning_factor_x = res->bin_factor_x ?
529 res->bin_factor_x : 1;
530 buf->binning_factor_y = res->bin_factor_y ?
531 res->bin_factor_y : 1;
532 return 0;
533 }
534
__ov5693_set_exposure(struct v4l2_subdev * sd,int coarse_itg,int gain,int digitgain)535 static long __ov5693_set_exposure(struct v4l2_subdev *sd, int coarse_itg,
536 int gain, int digitgain)
537
538 {
539 struct i2c_client *client = v4l2_get_subdevdata(sd);
540 struct ov5693_device *dev = to_ov5693_sensor(sd);
541 u16 vts, hts;
542 int ret, exp_val;
543
544 hts = ov5693_res[dev->fmt_idx].pixels_per_line;
545 vts = ov5693_res[dev->fmt_idx].lines_per_frame;
546 /*
547 * If coarse_itg is larger than 1<<15, can not write to reg directly.
548 * The way is to write coarse_itg/2 to the reg, meanwhile write 2*hts
549 * to the reg.
550 */
551 if (coarse_itg > (1 << 15)) {
552 hts = hts * 2;
553 coarse_itg = (int)coarse_itg / 2;
554 }
555 /* group hold */
556 ret = ov5693_write_reg(client, OV5693_8BIT,
557 OV5693_GROUP_ACCESS, 0x00);
558 if (ret) {
559 dev_err(&client->dev, "%s: write %x error, aborted\n",
560 __func__, OV5693_GROUP_ACCESS);
561 return ret;
562 }
563
564 ret = ov5693_write_reg(client, OV5693_8BIT,
565 OV5693_TIMING_HTS_H, (hts >> 8) & 0xFF);
566 if (ret) {
567 dev_err(&client->dev, "%s: write %x error, aborted\n",
568 __func__, OV5693_TIMING_HTS_H);
569 return ret;
570 }
571
572 ret = ov5693_write_reg(client, OV5693_8BIT,
573 OV5693_TIMING_HTS_L, hts & 0xFF);
574 if (ret) {
575 dev_err(&client->dev, "%s: write %x error, aborted\n",
576 __func__, OV5693_TIMING_HTS_L);
577 return ret;
578 }
579 /* Increase the VTS to match exposure + MARGIN */
580 if (coarse_itg > vts - OV5693_INTEGRATION_TIME_MARGIN)
581 vts = (u16)coarse_itg + OV5693_INTEGRATION_TIME_MARGIN;
582
583 ret = ov5693_write_reg(client, OV5693_8BIT,
584 OV5693_TIMING_VTS_H, (vts >> 8) & 0xFF);
585 if (ret) {
586 dev_err(&client->dev, "%s: write %x error, aborted\n",
587 __func__, OV5693_TIMING_VTS_H);
588 return ret;
589 }
590
591 ret = ov5693_write_reg(client, OV5693_8BIT,
592 OV5693_TIMING_VTS_L, vts & 0xFF);
593 if (ret) {
594 dev_err(&client->dev, "%s: write %x error, aborted\n",
595 __func__, OV5693_TIMING_VTS_L);
596 return ret;
597 }
598
599 /* set exposure */
600
601 /* Lower four bit should be 0*/
602 exp_val = coarse_itg << 4;
603 ret = ov5693_write_reg(client, OV5693_8BIT,
604 OV5693_EXPOSURE_L, exp_val & 0xFF);
605 if (ret) {
606 dev_err(&client->dev, "%s: write %x error, aborted\n",
607 __func__, OV5693_EXPOSURE_L);
608 return ret;
609 }
610
611 ret = ov5693_write_reg(client, OV5693_8BIT,
612 OV5693_EXPOSURE_M, (exp_val >> 8) & 0xFF);
613 if (ret) {
614 dev_err(&client->dev, "%s: write %x error, aborted\n",
615 __func__, OV5693_EXPOSURE_M);
616 return ret;
617 }
618
619 ret = ov5693_write_reg(client, OV5693_8BIT,
620 OV5693_EXPOSURE_H, (exp_val >> 16) & 0x0F);
621 if (ret) {
622 dev_err(&client->dev, "%s: write %x error, aborted\n",
623 __func__, OV5693_EXPOSURE_H);
624 return ret;
625 }
626
627 /* Analog gain */
628 ret = ov5693_write_reg(client, OV5693_8BIT,
629 OV5693_AGC_L, gain & 0xff);
630 if (ret) {
631 dev_err(&client->dev, "%s: write %x error, aborted\n",
632 __func__, OV5693_AGC_L);
633 return ret;
634 }
635
636 ret = ov5693_write_reg(client, OV5693_8BIT,
637 OV5693_AGC_H, (gain >> 8) & 0xff);
638 if (ret) {
639 dev_err(&client->dev, "%s: write %x error, aborted\n",
640 __func__, OV5693_AGC_H);
641 return ret;
642 }
643
644 /* Digital gain */
645 if (digitgain) {
646 ret = ov5693_write_reg(client, OV5693_16BIT,
647 OV5693_MWB_RED_GAIN_H, digitgain);
648 if (ret) {
649 dev_err(&client->dev, "%s: write %x error, aborted\n",
650 __func__, OV5693_MWB_RED_GAIN_H);
651 return ret;
652 }
653
654 ret = ov5693_write_reg(client, OV5693_16BIT,
655 OV5693_MWB_GREEN_GAIN_H, digitgain);
656 if (ret) {
657 dev_err(&client->dev, "%s: write %x error, aborted\n",
658 __func__, OV5693_MWB_RED_GAIN_H);
659 return ret;
660 }
661
662 ret = ov5693_write_reg(client, OV5693_16BIT,
663 OV5693_MWB_BLUE_GAIN_H, digitgain);
664 if (ret) {
665 dev_err(&client->dev, "%s: write %x error, aborted\n",
666 __func__, OV5693_MWB_RED_GAIN_H);
667 return ret;
668 }
669 }
670
671 /* End group */
672 ret = ov5693_write_reg(client, OV5693_8BIT,
673 OV5693_GROUP_ACCESS, 0x10);
674 if (ret)
675 return ret;
676
677 /* Delay launch group */
678 ret = ov5693_write_reg(client, OV5693_8BIT,
679 OV5693_GROUP_ACCESS, 0xa0);
680 if (ret)
681 return ret;
682 return ret;
683 }
684
ov5693_set_exposure(struct v4l2_subdev * sd,int exposure,int gain,int digitgain)685 static int ov5693_set_exposure(struct v4l2_subdev *sd, int exposure,
686 int gain, int digitgain)
687 {
688 struct ov5693_device *dev = to_ov5693_sensor(sd);
689 int ret;
690
691 mutex_lock(&dev->input_lock);
692 ret = __ov5693_set_exposure(sd, exposure, gain, digitgain);
693 mutex_unlock(&dev->input_lock);
694
695 return ret;
696 }
697
ov5693_s_exposure(struct v4l2_subdev * sd,struct atomisp_exposure * exposure)698 static long ov5693_s_exposure(struct v4l2_subdev *sd,
699 struct atomisp_exposure *exposure)
700 {
701 u16 coarse_itg = exposure->integration_time[0];
702 u16 analog_gain = exposure->gain[0];
703 u16 digital_gain = exposure->gain[1];
704
705 /* we should not accept the invalid value below */
706 if (analog_gain == 0) {
707 struct i2c_client *client = v4l2_get_subdevdata(sd);
708
709 v4l2_err(client, "%s: invalid value\n", __func__);
710 return -EINVAL;
711 }
712 return ov5693_set_exposure(sd, coarse_itg, analog_gain, digital_gain);
713 }
714
ov5693_read_otp_reg_array(struct i2c_client * client,u16 size,u16 addr,u8 * buf)715 static int ov5693_read_otp_reg_array(struct i2c_client *client, u16 size,
716 u16 addr, u8 *buf)
717 {
718 u16 index;
719 int ret;
720 u16 *pVal = NULL;
721
722 for (index = 0; index <= size; index++) {
723 pVal = (u16 *)(buf + index);
724 ret =
725 ov5693_read_reg(client, OV5693_8BIT, addr + index,
726 pVal);
727 if (ret)
728 return ret;
729 }
730
731 return 0;
732 }
733
__ov5693_otp_read(struct v4l2_subdev * sd,u8 * buf)734 static int __ov5693_otp_read(struct v4l2_subdev *sd, u8 *buf)
735 {
736 struct i2c_client *client = v4l2_get_subdevdata(sd);
737 struct ov5693_device *dev = to_ov5693_sensor(sd);
738 int ret;
739 int i;
740 u8 *b = buf;
741
742 dev->otp_size = 0;
743 for (i = 1; i < OV5693_OTP_BANK_MAX; i++) {
744 /*set bank NO and OTP read mode. */
745 ret = ov5693_write_reg(client, OV5693_8BIT, OV5693_OTP_BANK_REG,
746 (i | 0xc0)); //[7:6] 2'b11 [5:0] bank no
747 if (ret) {
748 dev_err(&client->dev, "failed to prepare OTP page\n");
749 return ret;
750 }
751 //pr_debug("write 0x%x->0x%x\n",OV5693_OTP_BANK_REG,(i|0xc0));
752
753 /*enable read */
754 ret = ov5693_write_reg(client, OV5693_8BIT, OV5693_OTP_READ_REG,
755 OV5693_OTP_MODE_READ); // enable :1
756 if (ret) {
757 dev_err(&client->dev,
758 "failed to set OTP reading mode page");
759 return ret;
760 }
761 //pr_debug("write 0x%x->0x%x\n",OV5693_OTP_READ_REG,OV5693_OTP_MODE_READ);
762
763 /* Reading the OTP data array */
764 ret = ov5693_read_otp_reg_array(client, OV5693_OTP_BANK_SIZE,
765 OV5693_OTP_START_ADDR,
766 b);
767 if (ret) {
768 dev_err(&client->dev, "failed to read OTP data\n");
769 return ret;
770 }
771
772 //pr_debug("BANK[%2d] %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n", i, *b, *(b+1), *(b+2), *(b+3), *(b+4), *(b+5), *(b+6), *(b+7), *(b+8), *(b+9), *(b+10), *(b+11), *(b+12), *(b+13), *(b+14), *(b+15));
773
774 //Intel OTP map, try to read 320byts first.
775 if (i == 21) {
776 if ((*b) == 0) {
777 dev->otp_size = 320;
778 break;
779 } else {
780 b = buf;
781 continue;
782 }
783 } else if (i ==
784 24) { //if the first 320bytes data doesn't not exist, try to read the next 32bytes data.
785 if ((*b) == 0) {
786 dev->otp_size = 32;
787 break;
788 } else {
789 b = buf;
790 continue;
791 }
792 } else if (i ==
793 27) { //if the prvious 32bytes data doesn't exist, try to read the next 32bytes data again.
794 if ((*b) == 0) {
795 dev->otp_size = 32;
796 break;
797 } else {
798 dev->otp_size = 0; // no OTP data.
799 break;
800 }
801 }
802
803 b = b + OV5693_OTP_BANK_SIZE;
804 }
805 return 0;
806 }
807
808 /*
809 * Read otp data and store it into a kmalloced buffer.
810 * The caller must kfree the buffer when no more needed.
811 * @size: set to the size of the returned otp data.
812 */
ov5693_otp_read(struct v4l2_subdev * sd)813 static void *ov5693_otp_read(struct v4l2_subdev *sd)
814 {
815 struct i2c_client *client = v4l2_get_subdevdata(sd);
816 u8 *buf;
817 int ret;
818
819 buf = devm_kzalloc(&client->dev, (OV5693_OTP_DATA_SIZE + 16), GFP_KERNEL);
820 if (!buf)
821 return ERR_PTR(-ENOMEM);
822
823 //otp valid after mipi on and sw stream on
824 ret = ov5693_write_reg(client, OV5693_8BIT, OV5693_FRAME_OFF_NUM, 0x00);
825
826 ret = ov5693_write_reg(client, OV5693_8BIT,
827 OV5693_SW_STREAM, OV5693_START_STREAMING);
828
829 ret = __ov5693_otp_read(sd, buf);
830
831 //mipi off and sw stream off after otp read
832 ret = ov5693_write_reg(client, OV5693_8BIT, OV5693_FRAME_OFF_NUM, 0x0f);
833
834 ret = ov5693_write_reg(client, OV5693_8BIT,
835 OV5693_SW_STREAM, OV5693_STOP_STREAMING);
836
837 /* Driver has failed to find valid data */
838 if (ret) {
839 dev_err(&client->dev, "sensor found no valid OTP data\n");
840 return ERR_PTR(ret);
841 }
842
843 return buf;
844 }
845
ov5693_g_priv_int_data(struct v4l2_subdev * sd,struct v4l2_private_int_data * priv)846 static int ov5693_g_priv_int_data(struct v4l2_subdev *sd,
847 struct v4l2_private_int_data *priv)
848 {
849 struct i2c_client *client = v4l2_get_subdevdata(sd);
850 struct ov5693_device *dev = to_ov5693_sensor(sd);
851 u8 __user *to = priv->data;
852 u32 read_size = priv->size;
853 int ret;
854
855 /* No need to copy data if size is 0 */
856 if (!read_size)
857 goto out;
858
859 if (IS_ERR(dev->otp_data)) {
860 dev_err(&client->dev, "OTP data not available");
861 return PTR_ERR(dev->otp_data);
862 }
863
864 /* Correct read_size value only if bigger than maximum */
865 if (read_size > OV5693_OTP_DATA_SIZE)
866 read_size = OV5693_OTP_DATA_SIZE;
867
868 ret = copy_to_user(to, dev->otp_data, read_size);
869 if (ret) {
870 dev_err(&client->dev, "%s: failed to copy OTP data to user\n",
871 __func__);
872 return -EFAULT;
873 }
874
875 pr_debug("%s read_size:%d\n", __func__, read_size);
876
877 out:
878 /* Return correct size */
879 priv->size = dev->otp_size;
880
881 return 0;
882 }
883
ov5693_ioctl(struct v4l2_subdev * sd,unsigned int cmd,void * arg)884 static long ov5693_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
885 {
886 switch (cmd) {
887 case ATOMISP_IOC_S_EXPOSURE:
888 return ov5693_s_exposure(sd, arg);
889 case ATOMISP_IOC_G_SENSOR_PRIV_INT_DATA:
890 return ov5693_g_priv_int_data(sd, arg);
891 default:
892 return -EINVAL;
893 }
894 return 0;
895 }
896
897 /*
898 * This returns the exposure time being used. This should only be used
899 * for filling in EXIF data, not for actual image processing.
900 */
ov5693_q_exposure(struct v4l2_subdev * sd,s32 * value)901 static int ov5693_q_exposure(struct v4l2_subdev *sd, s32 *value)
902 {
903 struct i2c_client *client = v4l2_get_subdevdata(sd);
904 u16 reg_v, reg_v2;
905 int ret;
906
907 /* get exposure */
908 ret = ov5693_read_reg(client, OV5693_8BIT,
909 OV5693_EXPOSURE_L,
910 ®_v);
911 if (ret)
912 goto err;
913
914 ret = ov5693_read_reg(client, OV5693_8BIT,
915 OV5693_EXPOSURE_M,
916 ®_v2);
917 if (ret)
918 goto err;
919
920 reg_v += reg_v2 << 8;
921 ret = ov5693_read_reg(client, OV5693_8BIT,
922 OV5693_EXPOSURE_H,
923 ®_v2);
924 if (ret)
925 goto err;
926
927 *value = reg_v + (((u32)reg_v2 << 16));
928 err:
929 return ret;
930 }
931
ad5823_t_focus_vcm(struct v4l2_subdev * sd,u16 val)932 static int ad5823_t_focus_vcm(struct v4l2_subdev *sd, u16 val)
933 {
934 struct i2c_client *client = v4l2_get_subdevdata(sd);
935 int ret;
936 u8 vcm_code;
937
938 ret = ad5823_i2c_read(client, AD5823_REG_VCM_CODE_MSB, &vcm_code);
939 if (ret)
940 return ret;
941
942 /* set reg VCM_CODE_MSB Bit[1:0] */
943 vcm_code = (vcm_code & VCM_CODE_MSB_MASK) |
944 ((val >> 8) & ~VCM_CODE_MSB_MASK);
945 ret = ad5823_i2c_write(client, AD5823_REG_VCM_CODE_MSB, vcm_code);
946 if (ret)
947 return ret;
948
949 /* set reg VCM_CODE_LSB Bit[7:0] */
950 ret = ad5823_i2c_write(client, AD5823_REG_VCM_CODE_LSB, (val & 0xff));
951 if (ret)
952 return ret;
953
954 /* set required vcm move time */
955 vcm_code = AD5823_RESONANCE_PERIOD / AD5823_RESONANCE_COEF
956 - AD5823_HIGH_FREQ_RANGE;
957 ret = ad5823_i2c_write(client, AD5823_REG_VCM_MOVE_TIME, vcm_code);
958
959 return ret;
960 }
961
ad5823_t_focus_abs(struct v4l2_subdev * sd,s32 value)962 static int ad5823_t_focus_abs(struct v4l2_subdev *sd, s32 value)
963 {
964 value = min(value, AD5823_MAX_FOCUS_POS);
965 return ad5823_t_focus_vcm(sd, value);
966 }
967
ov5693_t_focus_abs(struct v4l2_subdev * sd,s32 value)968 static int ov5693_t_focus_abs(struct v4l2_subdev *sd, s32 value)
969 {
970 struct ov5693_device *dev = to_ov5693_sensor(sd);
971 struct i2c_client *client = v4l2_get_subdevdata(sd);
972 int ret = 0;
973
974 dev_dbg(&client->dev, "%s: FOCUS_POS: 0x%x\n", __func__, value);
975 value = clamp(value, 0, OV5693_VCM_MAX_FOCUS_POS);
976 if (dev->vcm == VCM_DW9714) {
977 if (dev->vcm_update) {
978 ret = vcm_dw_i2c_write(client, VCM_PROTECTION_OFF);
979 if (ret)
980 return ret;
981 ret = vcm_dw_i2c_write(client, DIRECT_VCM);
982 if (ret)
983 return ret;
984 ret = vcm_dw_i2c_write(client, VCM_PROTECTION_ON);
985 if (ret)
986 return ret;
987 dev->vcm_update = false;
988 }
989 ret = vcm_dw_i2c_write(client,
990 vcm_val(value, VCM_DEFAULT_S));
991 } else if (dev->vcm == VCM_AD5823) {
992 ad5823_t_focus_abs(sd, value);
993 }
994 if (ret == 0) {
995 dev->number_of_steps = value - dev->focus;
996 dev->focus = value;
997 dev->timestamp_t_focus_abs = ktime_get();
998 } else
999 dev_err(&client->dev,
1000 "%s: i2c failed. ret %d\n", __func__, ret);
1001
1002 return ret;
1003 }
1004
ov5693_t_focus_rel(struct v4l2_subdev * sd,s32 value)1005 static int ov5693_t_focus_rel(struct v4l2_subdev *sd, s32 value)
1006 {
1007 struct ov5693_device *dev = to_ov5693_sensor(sd);
1008
1009 return ov5693_t_focus_abs(sd, dev->focus + value);
1010 }
1011
1012 #define DELAY_PER_STEP_NS 1000000
1013 #define DELAY_MAX_PER_STEP_NS (1000000 * 1023)
ov5693_q_focus_status(struct v4l2_subdev * sd,s32 * value)1014 static int ov5693_q_focus_status(struct v4l2_subdev *sd, s32 *value)
1015 {
1016 u32 status = 0;
1017 struct ov5693_device *dev = to_ov5693_sensor(sd);
1018 ktime_t temptime;
1019 ktime_t timedelay = ns_to_ktime(min_t(u32,
1020 abs(dev->number_of_steps) * DELAY_PER_STEP_NS,
1021 DELAY_MAX_PER_STEP_NS));
1022
1023 temptime = ktime_sub(ktime_get(), (dev->timestamp_t_focus_abs));
1024 if (ktime_compare(temptime, timedelay) <= 0) {
1025 status |= ATOMISP_FOCUS_STATUS_MOVING;
1026 status |= ATOMISP_FOCUS_HP_IN_PROGRESS;
1027 } else {
1028 status |= ATOMISP_FOCUS_STATUS_ACCEPTS_NEW_MOVE;
1029 status |= ATOMISP_FOCUS_HP_COMPLETE;
1030 }
1031
1032 *value = status;
1033
1034 return 0;
1035 }
1036
ov5693_q_focus_abs(struct v4l2_subdev * sd,s32 * value)1037 static int ov5693_q_focus_abs(struct v4l2_subdev *sd, s32 *value)
1038 {
1039 struct ov5693_device *dev = to_ov5693_sensor(sd);
1040 s32 val;
1041
1042 ov5693_q_focus_status(sd, &val);
1043
1044 if (val & ATOMISP_FOCUS_STATUS_MOVING)
1045 *value = dev->focus - dev->number_of_steps;
1046 else
1047 *value = dev->focus;
1048
1049 return 0;
1050 }
1051
ov5693_t_vcm_slew(struct v4l2_subdev * sd,s32 value)1052 static int ov5693_t_vcm_slew(struct v4l2_subdev *sd, s32 value)
1053 {
1054 struct ov5693_device *dev = to_ov5693_sensor(sd);
1055
1056 dev->number_of_steps = value;
1057 dev->vcm_update = true;
1058 return 0;
1059 }
1060
ov5693_t_vcm_timing(struct v4l2_subdev * sd,s32 value)1061 static int ov5693_t_vcm_timing(struct v4l2_subdev *sd, s32 value)
1062 {
1063 struct ov5693_device *dev = to_ov5693_sensor(sd);
1064
1065 dev->number_of_steps = value;
1066 dev->vcm_update = true;
1067 return 0;
1068 }
1069
ov5693_s_ctrl(struct v4l2_ctrl * ctrl)1070 static int ov5693_s_ctrl(struct v4l2_ctrl *ctrl)
1071 {
1072 struct ov5693_device *dev =
1073 container_of(ctrl->handler, struct ov5693_device, ctrl_handler);
1074 struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
1075 int ret = 0;
1076
1077 switch (ctrl->id) {
1078 case V4L2_CID_FOCUS_ABSOLUTE:
1079 dev_dbg(&client->dev, "%s: CID_FOCUS_ABSOLUTE:%d.\n",
1080 __func__, ctrl->val);
1081 ret = ov5693_t_focus_abs(&dev->sd, ctrl->val);
1082 break;
1083 case V4L2_CID_FOCUS_RELATIVE:
1084 dev_dbg(&client->dev, "%s: CID_FOCUS_RELATIVE:%d.\n",
1085 __func__, ctrl->val);
1086 ret = ov5693_t_focus_rel(&dev->sd, ctrl->val);
1087 break;
1088 case V4L2_CID_VCM_SLEW:
1089 ret = ov5693_t_vcm_slew(&dev->sd, ctrl->val);
1090 break;
1091 case V4L2_CID_VCM_TIMING:
1092 ret = ov5693_t_vcm_timing(&dev->sd, ctrl->val);
1093 break;
1094 default:
1095 ret = -EINVAL;
1096 }
1097 return ret;
1098 }
1099
ov5693_g_volatile_ctrl(struct v4l2_ctrl * ctrl)1100 static int ov5693_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
1101 {
1102 struct ov5693_device *dev =
1103 container_of(ctrl->handler, struct ov5693_device, ctrl_handler);
1104 int ret = 0;
1105
1106 switch (ctrl->id) {
1107 case V4L2_CID_EXPOSURE_ABSOLUTE:
1108 ret = ov5693_q_exposure(&dev->sd, &ctrl->val);
1109 break;
1110 case V4L2_CID_FOCAL_ABSOLUTE:
1111 ret = ov5693_g_focal(&dev->sd, &ctrl->val);
1112 break;
1113 case V4L2_CID_FNUMBER_ABSOLUTE:
1114 ret = ov5693_g_fnumber(&dev->sd, &ctrl->val);
1115 break;
1116 case V4L2_CID_FNUMBER_RANGE:
1117 ret = ov5693_g_fnumber_range(&dev->sd, &ctrl->val);
1118 break;
1119 case V4L2_CID_FOCUS_ABSOLUTE:
1120 ret = ov5693_q_focus_abs(&dev->sd, &ctrl->val);
1121 break;
1122 case V4L2_CID_FOCUS_STATUS:
1123 ret = ov5693_q_focus_status(&dev->sd, &ctrl->val);
1124 break;
1125 case V4L2_CID_BIN_FACTOR_HORZ:
1126 ret = ov5693_g_bin_factor_x(&dev->sd, &ctrl->val);
1127 break;
1128 case V4L2_CID_BIN_FACTOR_VERT:
1129 ret = ov5693_g_bin_factor_y(&dev->sd, &ctrl->val);
1130 break;
1131 default:
1132 ret = -EINVAL;
1133 }
1134
1135 return ret;
1136 }
1137
1138 static const struct v4l2_ctrl_ops ctrl_ops = {
1139 .s_ctrl = ov5693_s_ctrl,
1140 .g_volatile_ctrl = ov5693_g_volatile_ctrl
1141 };
1142
1143 static const struct v4l2_ctrl_config ov5693_controls[] = {
1144 {
1145 .ops = &ctrl_ops,
1146 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
1147 .type = V4L2_CTRL_TYPE_INTEGER,
1148 .name = "exposure",
1149 .min = 0x0,
1150 .max = 0xffff,
1151 .step = 0x01,
1152 .def = 0x00,
1153 .flags = 0,
1154 },
1155 {
1156 .ops = &ctrl_ops,
1157 .id = V4L2_CID_FOCAL_ABSOLUTE,
1158 .type = V4L2_CTRL_TYPE_INTEGER,
1159 .name = "focal length",
1160 .min = OV5693_FOCAL_LENGTH_DEFAULT,
1161 .max = OV5693_FOCAL_LENGTH_DEFAULT,
1162 .step = 0x01,
1163 .def = OV5693_FOCAL_LENGTH_DEFAULT,
1164 .flags = 0,
1165 },
1166 {
1167 .ops = &ctrl_ops,
1168 .id = V4L2_CID_FNUMBER_ABSOLUTE,
1169 .type = V4L2_CTRL_TYPE_INTEGER,
1170 .name = "f-number",
1171 .min = OV5693_F_NUMBER_DEFAULT,
1172 .max = OV5693_F_NUMBER_DEFAULT,
1173 .step = 0x01,
1174 .def = OV5693_F_NUMBER_DEFAULT,
1175 .flags = 0,
1176 },
1177 {
1178 .ops = &ctrl_ops,
1179 .id = V4L2_CID_FNUMBER_RANGE,
1180 .type = V4L2_CTRL_TYPE_INTEGER,
1181 .name = "f-number range",
1182 .min = OV5693_F_NUMBER_RANGE,
1183 .max = OV5693_F_NUMBER_RANGE,
1184 .step = 0x01,
1185 .def = OV5693_F_NUMBER_RANGE,
1186 .flags = 0,
1187 },
1188 {
1189 .ops = &ctrl_ops,
1190 .id = V4L2_CID_FOCUS_ABSOLUTE,
1191 .type = V4L2_CTRL_TYPE_INTEGER,
1192 .name = "focus move absolute",
1193 .min = 0,
1194 .max = OV5693_VCM_MAX_FOCUS_POS,
1195 .step = 1,
1196 .def = 0,
1197 .flags = 0,
1198 },
1199 {
1200 .ops = &ctrl_ops,
1201 .id = V4L2_CID_FOCUS_RELATIVE,
1202 .type = V4L2_CTRL_TYPE_INTEGER,
1203 .name = "focus move relative",
1204 .min = OV5693_VCM_MAX_FOCUS_NEG,
1205 .max = OV5693_VCM_MAX_FOCUS_POS,
1206 .step = 1,
1207 .def = 0,
1208 .flags = 0,
1209 },
1210 {
1211 .ops = &ctrl_ops,
1212 .id = V4L2_CID_FOCUS_STATUS,
1213 .type = V4L2_CTRL_TYPE_INTEGER,
1214 .name = "focus status",
1215 .min = 0,
1216 .max = 100, /* allow enum to grow in the future */
1217 .step = 1,
1218 .def = 0,
1219 .flags = 0,
1220 },
1221 {
1222 .ops = &ctrl_ops,
1223 .id = V4L2_CID_VCM_SLEW,
1224 .type = V4L2_CTRL_TYPE_INTEGER,
1225 .name = "vcm slew",
1226 .min = 0,
1227 .max = OV5693_VCM_SLEW_STEP_MAX,
1228 .step = 1,
1229 .def = 0,
1230 .flags = 0,
1231 },
1232 {
1233 .ops = &ctrl_ops,
1234 .id = V4L2_CID_VCM_TIMING,
1235 .type = V4L2_CTRL_TYPE_INTEGER,
1236 .name = "vcm step time",
1237 .min = 0,
1238 .max = OV5693_VCM_SLEW_TIME_MAX,
1239 .step = 1,
1240 .def = 0,
1241 .flags = 0,
1242 },
1243 {
1244 .ops = &ctrl_ops,
1245 .id = V4L2_CID_BIN_FACTOR_HORZ,
1246 .type = V4L2_CTRL_TYPE_INTEGER,
1247 .name = "horizontal binning factor",
1248 .min = 0,
1249 .max = OV5693_BIN_FACTOR_MAX,
1250 .step = 1,
1251 .def = 0,
1252 .flags = 0,
1253 },
1254 {
1255 .ops = &ctrl_ops,
1256 .id = V4L2_CID_BIN_FACTOR_VERT,
1257 .type = V4L2_CTRL_TYPE_INTEGER,
1258 .name = "vertical binning factor",
1259 .min = 0,
1260 .max = OV5693_BIN_FACTOR_MAX,
1261 .step = 1,
1262 .def = 0,
1263 .flags = 0,
1264 },
1265 };
1266
ov5693_init(struct v4l2_subdev * sd)1267 static int ov5693_init(struct v4l2_subdev *sd)
1268 {
1269 struct ov5693_device *dev = to_ov5693_sensor(sd);
1270 struct i2c_client *client = v4l2_get_subdevdata(sd);
1271 int ret;
1272
1273 pr_info("%s\n", __func__);
1274 mutex_lock(&dev->input_lock);
1275 dev->vcm_update = false;
1276
1277 if (dev->vcm == VCM_AD5823) {
1278 ret = vcm_ad_i2c_wr8(client, 0x01, 0x01); /* vcm init test */
1279 if (ret)
1280 dev_err(&client->dev,
1281 "vcm reset failed\n");
1282 /*change the mode*/
1283 ret = ad5823_i2c_write(client, AD5823_REG_VCM_CODE_MSB,
1284 AD5823_RING_CTRL_ENABLE);
1285 if (ret)
1286 dev_err(&client->dev,
1287 "vcm enable ringing failed\n");
1288 ret = ad5823_i2c_write(client, AD5823_REG_MODE,
1289 AD5823_ARC_RES1);
1290 if (ret)
1291 dev_err(&client->dev,
1292 "vcm change mode failed\n");
1293 }
1294
1295 /*change initial focus value for ad5823*/
1296 if (dev->vcm == VCM_AD5823) {
1297 dev->focus = AD5823_INIT_FOCUS_POS;
1298 ov5693_t_focus_abs(sd, AD5823_INIT_FOCUS_POS);
1299 } else {
1300 dev->focus = 0;
1301 ov5693_t_focus_abs(sd, 0);
1302 }
1303
1304 mutex_unlock(&dev->input_lock);
1305
1306 return 0;
1307 }
1308
power_ctrl(struct v4l2_subdev * sd,bool flag)1309 static int power_ctrl(struct v4l2_subdev *sd, bool flag)
1310 {
1311 int ret;
1312 struct ov5693_device *dev = to_ov5693_sensor(sd);
1313
1314 if (!dev || !dev->platform_data)
1315 return -ENODEV;
1316
1317 /*
1318 * This driver assumes "internal DVDD, PWDNB tied to DOVDD".
1319 * In this set up only gpio0 (XSHUTDN) should be available
1320 * but in some products (for example ECS) gpio1 (PWDNB) is
1321 * also available. If gpio1 is available we emulate it being
1322 * tied to DOVDD here.
1323 */
1324 if (flag) {
1325 ret = dev->platform_data->v2p8_ctrl(sd, 1);
1326 dev->platform_data->gpio1_ctrl(sd, 1);
1327 if (ret == 0) {
1328 ret = dev->platform_data->v1p8_ctrl(sd, 1);
1329 if (ret) {
1330 dev->platform_data->gpio1_ctrl(sd, 0);
1331 ret = dev->platform_data->v2p8_ctrl(sd, 0);
1332 }
1333 }
1334 } else {
1335 dev->platform_data->gpio1_ctrl(sd, 0);
1336 ret = dev->platform_data->v1p8_ctrl(sd, 0);
1337 ret |= dev->platform_data->v2p8_ctrl(sd, 0);
1338 }
1339
1340 return ret;
1341 }
1342
gpio_ctrl(struct v4l2_subdev * sd,bool flag)1343 static int gpio_ctrl(struct v4l2_subdev *sd, bool flag)
1344 {
1345 struct ov5693_device *dev = to_ov5693_sensor(sd);
1346
1347 if (!dev || !dev->platform_data)
1348 return -ENODEV;
1349
1350 return dev->platform_data->gpio0_ctrl(sd, flag);
1351 }
1352
__power_up(struct v4l2_subdev * sd)1353 static int __power_up(struct v4l2_subdev *sd)
1354 {
1355 struct ov5693_device *dev = to_ov5693_sensor(sd);
1356 struct i2c_client *client = v4l2_get_subdevdata(sd);
1357 int ret;
1358
1359 if (!dev->platform_data) {
1360 dev_err(&client->dev,
1361 "no camera_sensor_platform_data");
1362 return -ENODEV;
1363 }
1364
1365 /* power control */
1366 ret = power_ctrl(sd, 1);
1367 if (ret)
1368 goto fail_power;
1369
1370 /* according to DS, at least 5ms is needed between DOVDD and PWDN */
1371 /* add this delay time to 10~11ms*/
1372 usleep_range(10000, 11000);
1373
1374 /* gpio ctrl */
1375 ret = gpio_ctrl(sd, 1);
1376 if (ret) {
1377 ret = gpio_ctrl(sd, 1);
1378 if (ret)
1379 goto fail_power;
1380 }
1381
1382 /* flis clock control */
1383 ret = dev->platform_data->flisclk_ctrl(sd, 1);
1384 if (ret)
1385 goto fail_clk;
1386
1387 __cci_delay(up_delay);
1388
1389 return 0;
1390
1391 fail_clk:
1392 gpio_ctrl(sd, 0);
1393 fail_power:
1394 power_ctrl(sd, 0);
1395 dev_err(&client->dev, "sensor power-up failed\n");
1396
1397 return ret;
1398 }
1399
power_down(struct v4l2_subdev * sd)1400 static int power_down(struct v4l2_subdev *sd)
1401 {
1402 struct ov5693_device *dev = to_ov5693_sensor(sd);
1403 struct i2c_client *client = v4l2_get_subdevdata(sd);
1404 int ret = 0;
1405
1406 dev->focus = OV5693_INVALID_CONFIG;
1407 if (!dev->platform_data) {
1408 dev_err(&client->dev,
1409 "no camera_sensor_platform_data");
1410 return -ENODEV;
1411 }
1412
1413 ret = dev->platform_data->flisclk_ctrl(sd, 0);
1414 if (ret)
1415 dev_err(&client->dev, "flisclk failed\n");
1416
1417 /* gpio ctrl */
1418 ret = gpio_ctrl(sd, 0);
1419 if (ret) {
1420 ret = gpio_ctrl(sd, 0);
1421 if (ret)
1422 dev_err(&client->dev, "gpio failed 2\n");
1423 }
1424
1425 /* power control */
1426 ret = power_ctrl(sd, 0);
1427 if (ret)
1428 dev_err(&client->dev, "vprog failed.\n");
1429
1430 return ret;
1431 }
1432
power_up(struct v4l2_subdev * sd)1433 static int power_up(struct v4l2_subdev *sd)
1434 {
1435 static const int retry_count = 4;
1436 int i, ret;
1437
1438 for (i = 0; i < retry_count; i++) {
1439 ret = __power_up(sd);
1440 if (!ret)
1441 return 0;
1442
1443 power_down(sd);
1444 }
1445 return ret;
1446 }
1447
ov5693_s_power(struct v4l2_subdev * sd,int on)1448 static int ov5693_s_power(struct v4l2_subdev *sd, int on)
1449 {
1450 int ret;
1451
1452 pr_info("%s: on %d\n", __func__, on);
1453 if (on == 0)
1454 return power_down(sd);
1455 else {
1456 ret = power_up(sd);
1457 if (!ret) {
1458 ret = ov5693_init(sd);
1459 /* restore settings */
1460 ov5693_res = ov5693_res_preview;
1461 N_RES = N_RES_PREVIEW;
1462 }
1463 }
1464 return ret;
1465 }
1466
1467 /*
1468 * distance - calculate the distance
1469 * @res: resolution
1470 * @w: width
1471 * @h: height
1472 *
1473 * Get the gap between res_w/res_h and w/h.
1474 * distance = (res_w/res_h - w/h) / (w/h) * 8192
1475 * res->width/height smaller than w/h wouldn't be considered.
1476 * The gap of ratio larger than 1/8 wouldn't be considered.
1477 * Returns the value of gap or -1 if fail.
1478 */
1479 #define LARGEST_ALLOWED_RATIO_MISMATCH 1024
distance(struct ov5693_resolution * res,u32 w,u32 h)1480 static int distance(struct ov5693_resolution *res, u32 w, u32 h)
1481 {
1482 int ratio;
1483 int distance;
1484
1485 if (w == 0 || h == 0 ||
1486 res->width < w || res->height < h)
1487 return -1;
1488
1489 ratio = res->width << 13;
1490 ratio /= w;
1491 ratio *= h;
1492 ratio /= res->height;
1493
1494 distance = abs(ratio - 8192);
1495
1496 if (distance > LARGEST_ALLOWED_RATIO_MISMATCH)
1497 return -1;
1498
1499 return distance;
1500 }
1501
1502 /* Return the nearest higher resolution index
1503 * Firstly try to find the approximate aspect ratio resolution
1504 * If we find multiple same AR resolutions, choose the
1505 * minimal size.
1506 */
nearest_resolution_index(int w,int h)1507 static int nearest_resolution_index(int w, int h)
1508 {
1509 int i;
1510 int idx = -1;
1511 int dist;
1512 int min_dist = INT_MAX;
1513 int min_res_w = INT_MAX;
1514 struct ov5693_resolution *tmp_res = NULL;
1515
1516 for (i = 0; i < N_RES; i++) {
1517 tmp_res = &ov5693_res[i];
1518 dist = distance(tmp_res, w, h);
1519 if (dist == -1)
1520 continue;
1521 if (dist < min_dist) {
1522 min_dist = dist;
1523 idx = i;
1524 min_res_w = ov5693_res[i].width;
1525 continue;
1526 }
1527 if (dist == min_dist && ov5693_res[i].width < min_res_w)
1528 idx = i;
1529 }
1530
1531 return idx;
1532 }
1533
get_resolution_index(int w,int h)1534 static int get_resolution_index(int w, int h)
1535 {
1536 int i;
1537
1538 for (i = 0; i < N_RES; i++) {
1539 if (w != ov5693_res[i].width)
1540 continue;
1541 if (h != ov5693_res[i].height)
1542 continue;
1543
1544 return i;
1545 }
1546
1547 return -1;
1548 }
1549
1550 /* TODO: remove it. */
startup(struct v4l2_subdev * sd)1551 static int startup(struct v4l2_subdev *sd)
1552 {
1553 struct ov5693_device *dev = to_ov5693_sensor(sd);
1554 struct i2c_client *client = v4l2_get_subdevdata(sd);
1555 int ret = 0;
1556
1557 ret = ov5693_write_reg(client, OV5693_8BIT,
1558 OV5693_SW_RESET, 0x01);
1559 if (ret) {
1560 dev_err(&client->dev, "ov5693 reset err.\n");
1561 return ret;
1562 }
1563
1564 ret = ov5693_write_reg_array(client, ov5693_global_setting);
1565 if (ret) {
1566 dev_err(&client->dev, "ov5693 write register err.\n");
1567 return ret;
1568 }
1569
1570 ret = ov5693_write_reg_array(client, ov5693_res[dev->fmt_idx].regs);
1571 if (ret) {
1572 dev_err(&client->dev, "ov5693 write register err.\n");
1573 return ret;
1574 }
1575
1576 return ret;
1577 }
1578
ov5693_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * format)1579 static int ov5693_set_fmt(struct v4l2_subdev *sd,
1580 struct v4l2_subdev_state *sd_state,
1581 struct v4l2_subdev_format *format)
1582 {
1583 struct v4l2_mbus_framefmt *fmt = &format->format;
1584 struct ov5693_device *dev = to_ov5693_sensor(sd);
1585 struct i2c_client *client = v4l2_get_subdevdata(sd);
1586 struct camera_mipi_info *ov5693_info = NULL;
1587 int ret = 0;
1588 int idx;
1589
1590 if (format->pad)
1591 return -EINVAL;
1592 if (!fmt)
1593 return -EINVAL;
1594 ov5693_info = v4l2_get_subdev_hostdata(sd);
1595 if (!ov5693_info)
1596 return -EINVAL;
1597
1598 mutex_lock(&dev->input_lock);
1599 idx = nearest_resolution_index(fmt->width, fmt->height);
1600 if (idx == -1) {
1601 /* return the largest resolution */
1602 fmt->width = ov5693_res[N_RES - 1].width;
1603 fmt->height = ov5693_res[N_RES - 1].height;
1604 } else {
1605 fmt->width = ov5693_res[idx].width;
1606 fmt->height = ov5693_res[idx].height;
1607 }
1608
1609 fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
1610 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
1611 sd_state->pads->try_fmt = *fmt;
1612 mutex_unlock(&dev->input_lock);
1613 return 0;
1614 }
1615
1616 dev->fmt_idx = get_resolution_index(fmt->width, fmt->height);
1617 if (dev->fmt_idx == -1) {
1618 dev_err(&client->dev, "get resolution fail\n");
1619 mutex_unlock(&dev->input_lock);
1620 return -EINVAL;
1621 }
1622
1623 ret = startup(sd);
1624 if (ret) {
1625 int i = 0;
1626
1627 dev_err(&client->dev, "ov5693 startup err, retry to power up\n");
1628 for (i = 0; i < OV5693_POWER_UP_RETRY_NUM; i++) {
1629 dev_err(&client->dev,
1630 "ov5693 retry to power up %d/%d times, result: ",
1631 i + 1, OV5693_POWER_UP_RETRY_NUM);
1632 power_down(sd);
1633 ret = power_up(sd);
1634 if (!ret) {
1635 mutex_unlock(&dev->input_lock);
1636 ov5693_init(sd);
1637 mutex_lock(&dev->input_lock);
1638 } else {
1639 dev_err(&client->dev, "power up failed, continue\n");
1640 continue;
1641 }
1642 ret = startup(sd);
1643 if (ret) {
1644 dev_err(&client->dev, " startup FAILED!\n");
1645 } else {
1646 dev_err(&client->dev, " startup SUCCESS!\n");
1647 break;
1648 }
1649 }
1650 }
1651
1652 /*
1653 * After sensor settings are set to HW, sometimes stream is started.
1654 * This would cause ISP timeout because ISP is not ready to receive
1655 * data yet. So add stop streaming here.
1656 */
1657 ret = ov5693_write_reg(client, OV5693_8BIT, OV5693_SW_STREAM,
1658 OV5693_STOP_STREAMING);
1659 if (ret)
1660 dev_warn(&client->dev, "ov5693 stream off err\n");
1661
1662 ret = ov5693_get_intg_factor(client, ov5693_info,
1663 &ov5693_res[dev->fmt_idx]);
1664 if (ret) {
1665 dev_err(&client->dev, "failed to get integration_factor\n");
1666 goto err;
1667 }
1668
1669 ov5693_info->metadata_width = fmt->width * 10 / 8;
1670 ov5693_info->metadata_height = 1;
1671 ov5693_info->metadata_effective_width = &ov5693_embedded_effective_size;
1672
1673 err:
1674 mutex_unlock(&dev->input_lock);
1675 return ret;
1676 }
1677
ov5693_get_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * format)1678 static int ov5693_get_fmt(struct v4l2_subdev *sd,
1679 struct v4l2_subdev_state *sd_state,
1680 struct v4l2_subdev_format *format)
1681 {
1682 struct v4l2_mbus_framefmt *fmt = &format->format;
1683 struct ov5693_device *dev = to_ov5693_sensor(sd);
1684
1685 if (format->pad)
1686 return -EINVAL;
1687
1688 if (!fmt)
1689 return -EINVAL;
1690
1691 fmt->width = ov5693_res[dev->fmt_idx].width;
1692 fmt->height = ov5693_res[dev->fmt_idx].height;
1693 fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
1694
1695 return 0;
1696 }
1697
ov5693_detect(struct i2c_client * client)1698 static int ov5693_detect(struct i2c_client *client)
1699 {
1700 struct i2c_adapter *adapter = client->adapter;
1701 u16 high, low;
1702 int ret;
1703 u16 id;
1704 u8 revision;
1705
1706 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
1707 return -ENODEV;
1708
1709 ret = ov5693_read_reg(client, OV5693_8BIT,
1710 OV5693_SC_CMMN_CHIP_ID_H, &high);
1711 if (ret) {
1712 dev_err(&client->dev, "sensor_id_high = 0x%x\n", high);
1713 return -ENODEV;
1714 }
1715 ret = ov5693_read_reg(client, OV5693_8BIT,
1716 OV5693_SC_CMMN_CHIP_ID_L, &low);
1717 if (ret)
1718 return ret;
1719 id = ((((u16)high) << 8) | (u16)low);
1720
1721 if (id != OV5693_ID) {
1722 dev_err(&client->dev, "sensor ID error 0x%x\n", id);
1723 return -ENODEV;
1724 }
1725
1726 ret = ov5693_read_reg(client, OV5693_8BIT,
1727 OV5693_SC_CMMN_SUB_ID, &high);
1728 revision = (u8)high & 0x0f;
1729
1730 dev_dbg(&client->dev, "sensor_revision = 0x%x\n", revision);
1731 dev_dbg(&client->dev, "detect ov5693 success\n");
1732 return 0;
1733 }
1734
ov5693_s_stream(struct v4l2_subdev * sd,int enable)1735 static int ov5693_s_stream(struct v4l2_subdev *sd, int enable)
1736 {
1737 struct ov5693_device *dev = to_ov5693_sensor(sd);
1738 struct i2c_client *client = v4l2_get_subdevdata(sd);
1739 int ret;
1740
1741 mutex_lock(&dev->input_lock);
1742
1743 ret = ov5693_write_reg(client, OV5693_8BIT, OV5693_SW_STREAM,
1744 enable ? OV5693_START_STREAMING :
1745 OV5693_STOP_STREAMING);
1746
1747 mutex_unlock(&dev->input_lock);
1748
1749 return ret;
1750 }
1751
ov5693_s_config(struct v4l2_subdev * sd,int irq,void * platform_data)1752 static int ov5693_s_config(struct v4l2_subdev *sd,
1753 int irq, void *platform_data)
1754 {
1755 struct ov5693_device *dev = to_ov5693_sensor(sd);
1756 struct i2c_client *client = v4l2_get_subdevdata(sd);
1757 int ret = 0;
1758
1759 if (!platform_data)
1760 return -ENODEV;
1761
1762 dev->platform_data =
1763 (struct camera_sensor_platform_data *)platform_data;
1764
1765 mutex_lock(&dev->input_lock);
1766 /* power off the module, then power on it in future
1767 * as first power on by board may not fulfill the
1768 * power on sequqence needed by the module
1769 */
1770 ret = power_down(sd);
1771 if (ret) {
1772 dev_err(&client->dev, "ov5693 power-off err.\n");
1773 goto fail_power_off;
1774 }
1775
1776 ret = power_up(sd);
1777 if (ret) {
1778 dev_err(&client->dev, "ov5693 power-up err.\n");
1779 goto fail_power_on;
1780 }
1781
1782 if (!dev->vcm)
1783 dev->vcm = vcm_detect(client);
1784
1785 ret = dev->platform_data->csi_cfg(sd, 1);
1786 if (ret)
1787 goto fail_csi_cfg;
1788
1789 /* config & detect sensor */
1790 ret = ov5693_detect(client);
1791 if (ret) {
1792 dev_err(&client->dev, "ov5693_detect err s_config.\n");
1793 goto fail_csi_cfg;
1794 }
1795
1796 dev->otp_data = ov5693_otp_read(sd);
1797
1798 /* turn off sensor, after probed */
1799 ret = power_down(sd);
1800 if (ret) {
1801 dev_err(&client->dev, "ov5693 power-off err.\n");
1802 goto fail_csi_cfg;
1803 }
1804 mutex_unlock(&dev->input_lock);
1805
1806 return ret;
1807
1808 fail_csi_cfg:
1809 dev->platform_data->csi_cfg(sd, 0);
1810 fail_power_on:
1811 power_down(sd);
1812 dev_err(&client->dev, "sensor power-gating failed\n");
1813 fail_power_off:
1814 mutex_unlock(&dev->input_lock);
1815 return ret;
1816 }
1817
ov5693_g_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_frame_interval * interval)1818 static int ov5693_g_frame_interval(struct v4l2_subdev *sd,
1819 struct v4l2_subdev_frame_interval *interval)
1820 {
1821 struct ov5693_device *dev = to_ov5693_sensor(sd);
1822
1823 interval->interval.numerator = 1;
1824 interval->interval.denominator = ov5693_res[dev->fmt_idx].fps;
1825
1826 return 0;
1827 }
1828
ov5693_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_mbus_code_enum * code)1829 static int ov5693_enum_mbus_code(struct v4l2_subdev *sd,
1830 struct v4l2_subdev_state *sd_state,
1831 struct v4l2_subdev_mbus_code_enum *code)
1832 {
1833 if (code->index >= MAX_FMTS)
1834 return -EINVAL;
1835
1836 code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
1837 return 0;
1838 }
1839
ov5693_enum_frame_size(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_frame_size_enum * fse)1840 static int ov5693_enum_frame_size(struct v4l2_subdev *sd,
1841 struct v4l2_subdev_state *sd_state,
1842 struct v4l2_subdev_frame_size_enum *fse)
1843 {
1844 int index = fse->index;
1845
1846 if (index >= N_RES)
1847 return -EINVAL;
1848
1849 fse->min_width = ov5693_res[index].width;
1850 fse->min_height = ov5693_res[index].height;
1851 fse->max_width = ov5693_res[index].width;
1852 fse->max_height = ov5693_res[index].height;
1853
1854 return 0;
1855 }
1856
1857 static const struct v4l2_subdev_video_ops ov5693_video_ops = {
1858 .s_stream = ov5693_s_stream,
1859 .g_frame_interval = ov5693_g_frame_interval,
1860 };
1861
1862 static const struct v4l2_subdev_core_ops ov5693_core_ops = {
1863 .s_power = ov5693_s_power,
1864 .ioctl = ov5693_ioctl,
1865 };
1866
1867 static const struct v4l2_subdev_pad_ops ov5693_pad_ops = {
1868 .enum_mbus_code = ov5693_enum_mbus_code,
1869 .enum_frame_size = ov5693_enum_frame_size,
1870 .get_fmt = ov5693_get_fmt,
1871 .set_fmt = ov5693_set_fmt,
1872 };
1873
1874 static const struct v4l2_subdev_ops ov5693_ops = {
1875 .core = &ov5693_core_ops,
1876 .video = &ov5693_video_ops,
1877 .pad = &ov5693_pad_ops,
1878 };
1879
ov5693_remove(struct i2c_client * client)1880 static void ov5693_remove(struct i2c_client *client)
1881 {
1882 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1883 struct ov5693_device *dev = to_ov5693_sensor(sd);
1884
1885 dev_dbg(&client->dev, "ov5693_remove...\n");
1886
1887 dev->platform_data->csi_cfg(sd, 0);
1888
1889 v4l2_device_unregister_subdev(sd);
1890
1891 atomisp_gmin_remove_subdev(sd);
1892
1893 media_entity_cleanup(&dev->sd.entity);
1894 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1895 kfree(dev);
1896 }
1897
ov5693_probe(struct i2c_client * client)1898 static int ov5693_probe(struct i2c_client *client)
1899 {
1900 struct ov5693_device *dev;
1901 int i2c;
1902 int ret;
1903 void *pdata;
1904 unsigned int i;
1905
1906 /*
1907 * Firmware workaround: Some modules use a "secondary default"
1908 * address of 0x10 which doesn't appear on schematics, and
1909 * some BIOS versions haven't gotten the memo. Work around
1910 * via config.
1911 */
1912 i2c = gmin_get_var_int(&client->dev, false, "I2CAddr", -1);
1913 if (i2c != -1) {
1914 dev_info(&client->dev,
1915 "Overriding firmware-provided I2C address (0x%x) with 0x%x\n",
1916 client->addr, i2c);
1917 client->addr = i2c;
1918 }
1919
1920 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1921 if (!dev)
1922 return -ENOMEM;
1923
1924 mutex_init(&dev->input_lock);
1925
1926 dev->fmt_idx = 0;
1927 v4l2_i2c_subdev_init(&dev->sd, client, &ov5693_ops);
1928
1929 pdata = gmin_camera_platform_data(&dev->sd,
1930 ATOMISP_INPUT_FORMAT_RAW_10,
1931 atomisp_bayer_order_bggr);
1932 if (!pdata) {
1933 ret = -EINVAL;
1934 goto out_free;
1935 }
1936
1937 ret = ov5693_s_config(&dev->sd, client->irq, pdata);
1938 if (ret)
1939 goto out_free;
1940
1941 ret = atomisp_register_i2c_module(&dev->sd, pdata, RAW_CAMERA);
1942 if (ret)
1943 goto out_free;
1944
1945 dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1946 dev->pad.flags = MEDIA_PAD_FL_SOURCE;
1947 dev->format.code = MEDIA_BUS_FMT_SBGGR10_1X10;
1948 dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1949 ret =
1950 v4l2_ctrl_handler_init(&dev->ctrl_handler,
1951 ARRAY_SIZE(ov5693_controls));
1952 if (ret) {
1953 ov5693_remove(client);
1954 return ret;
1955 }
1956
1957 for (i = 0; i < ARRAY_SIZE(ov5693_controls); i++)
1958 v4l2_ctrl_new_custom(&dev->ctrl_handler, &ov5693_controls[i],
1959 NULL);
1960
1961 if (dev->ctrl_handler.error) {
1962 ov5693_remove(client);
1963 return dev->ctrl_handler.error;
1964 }
1965
1966 /* Use same lock for controls as for everything else. */
1967 dev->ctrl_handler.lock = &dev->input_lock;
1968 dev->sd.ctrl_handler = &dev->ctrl_handler;
1969
1970 ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad);
1971 if (ret)
1972 ov5693_remove(client);
1973
1974 return ret;
1975 out_free:
1976 v4l2_device_unregister_subdev(&dev->sd);
1977 kfree(dev);
1978 return ret;
1979 }
1980
1981 static const struct acpi_device_id ov5693_acpi_match[] = {
1982 {"INT33BE"},
1983 {},
1984 };
1985 MODULE_DEVICE_TABLE(acpi, ov5693_acpi_match);
1986
1987 static struct i2c_driver ov5693_driver = {
1988 .driver = {
1989 .name = "ov5693",
1990 .acpi_match_table = ov5693_acpi_match,
1991 },
1992 .probe_new = ov5693_probe,
1993 .remove = ov5693_remove,
1994 };
1995 module_i2c_driver(ov5693_driver);
1996
1997 MODULE_DESCRIPTION("A low-level driver for OmniVision 5693 sensors");
1998 MODULE_LICENSE("GPL");
1999