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