GNU Linux-libre 4.14.290-gnu1
[releases.git] / drivers / staging / media / atomisp / i2c / ov2680.c
1 /*
2  * Support for OmniVision OV2680 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  */
16
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/kernel.h>
20 #include <linux/mm.h>
21 #include <linux/string.h>
22 #include <linux/errno.h>
23 #include <linux/init.h>
24 #include <linux/kmod.h>
25 #include <linux/device.h>
26 #include <linux/delay.h>
27 #include <linux/slab.h>
28 #include <linux/i2c.h>
29 #include <linux/gpio.h>
30 #include <linux/moduleparam.h>
31 #include <media/v4l2-device.h>
32 #include <linux/io.h>
33 #include <linux/acpi.h>
34 #include "../include/linux/atomisp_gmin_platform.h"
35
36 #include "ov2680.h"
37
38 static int h_flag = 0;
39 static int v_flag = 0;
40 static enum atomisp_bayer_order ov2680_bayer_order_mapping[] = {
41         atomisp_bayer_order_bggr,
42         atomisp_bayer_order_grbg,
43         atomisp_bayer_order_gbrg,
44         atomisp_bayer_order_rggb,
45 };
46
47 /* i2c read/write stuff */
48 static int ov2680_read_reg(struct i2c_client *client,
49                            u16 data_length, u16 reg, u16 *val)
50 {
51         int err;
52         struct i2c_msg msg[2];
53         unsigned char data[6];
54
55         if (!client->adapter) {
56                 dev_err(&client->dev, "%s error, no client->adapter\n",
57                         __func__);
58                 return -ENODEV;
59         }
60
61         if (data_length != OV2680_8BIT && data_length != OV2680_16BIT
62                                         && data_length != OV2680_32BIT) {
63                 dev_err(&client->dev, "%s error, invalid data length\n",
64                         __func__);
65                 return -EINVAL;
66         }
67
68         memset(msg, 0 , sizeof(msg));
69
70         msg[0].addr = client->addr;
71         msg[0].flags = 0;
72         msg[0].len = I2C_MSG_LENGTH;
73         msg[0].buf = data;
74
75         /* high byte goes out first */
76         data[0] = (u8)(reg >> 8);
77         data[1] = (u8)(reg & 0xff);
78
79         msg[1].addr = client->addr;
80         msg[1].len = data_length;
81         msg[1].flags = I2C_M_RD;
82         msg[1].buf = data;
83
84         err = i2c_transfer(client->adapter, msg, 2);
85         if (err != 2) {
86                 if (err >= 0)
87                         err = -EIO;
88                 dev_err(&client->dev,
89                         "read from offset 0x%x error %d", reg, err);
90                 return err;
91         }
92
93         *val = 0;
94         /* high byte comes first */
95         if (data_length == OV2680_8BIT)
96                 *val = (u8)data[0];
97         else if (data_length == OV2680_16BIT)
98                 *val = be16_to_cpu(*(u16 *)&data[0]);
99         else
100                 *val = be32_to_cpu(*(u32 *)&data[0]);
101         //dev_dbg(&client->dev,  "++++i2c read adr%x = %x\n", reg,*val);
102         return 0;
103 }
104
105 static int ov2680_i2c_write(struct i2c_client *client, u16 len, u8 *data)
106 {
107         struct i2c_msg msg;
108         const int num_msg = 1;
109         int ret;
110
111         msg.addr = client->addr;
112         msg.flags = 0;
113         msg.len = len;
114         msg.buf = data;
115         ret = i2c_transfer(client->adapter, &msg, 1);
116         //dev_dbg(&client->dev,  "+++i2c write reg=%x->%x\n", data[0]*256 +data[1],data[2]);
117         return ret == num_msg ? 0 : -EIO;
118 }
119
120 static int ov2680_write_reg(struct i2c_client *client, u16 data_length,
121                                                         u16 reg, u16 val)
122 {
123         int ret;
124         unsigned char data[4] = {0};
125         u16 *wreg = (u16 *)data;
126         const u16 len = data_length + sizeof(u16); /* 16-bit address + data */
127
128         if (data_length != OV2680_8BIT && data_length != OV2680_16BIT) {
129                 dev_err(&client->dev,
130                         "%s error, invalid data_length\n", __func__);
131                 return -EINVAL;
132         }
133
134         /* high byte goes out first */
135         *wreg = cpu_to_be16(reg);
136
137         if (data_length == OV2680_8BIT) {
138                 data[2] = (u8)(val);
139         } else {
140                 /* OV2680_16BIT */
141                 u16 *wdata = (u16 *)&data[2];
142                 *wdata = cpu_to_be16(val);
143         }
144
145         ret = ov2680_i2c_write(client, len, data);
146         if (ret)
147                 dev_err(&client->dev,
148                         "write error: wrote 0x%x to offset 0x%x error %d",
149                         val, reg, ret);
150
151         return ret;
152 }
153
154 /*
155  * ov2680_write_reg_array - Initializes a list of OV2680 registers
156  * @client: i2c driver client structure
157  * @reglist: list of registers to be written
158  *
159  * This function initializes a list of registers. When consecutive addresses
160  * are found in a row on the list, this function creates a buffer and sends
161  * consecutive data in a single i2c_transfer().
162  *
163  * __ov2680_flush_reg_array, __ov2680_buf_reg_array() and
164  * __ov2680_write_reg_is_consecutive() are internal functions to
165  * ov2680_write_reg_array_fast() and should be not used anywhere else.
166  *
167  */
168
169 static int __ov2680_flush_reg_array(struct i2c_client *client,
170                                     struct ov2680_write_ctrl *ctrl)
171 {
172         u16 size;
173
174         if (ctrl->index == 0)
175                 return 0;
176
177         size = sizeof(u16) + ctrl->index; /* 16-bit address + data */
178         ctrl->buffer.addr = cpu_to_be16(ctrl->buffer.addr);
179         ctrl->index = 0;
180
181         return ov2680_i2c_write(client, size, (u8 *)&ctrl->buffer);
182 }
183
184 static int __ov2680_buf_reg_array(struct i2c_client *client,
185                                   struct ov2680_write_ctrl *ctrl,
186                                   const struct ov2680_reg *next)
187 {
188         int size;
189         u16 *data16;
190
191         switch (next->type) {
192         case OV2680_8BIT:
193                 size = 1;
194                 ctrl->buffer.data[ctrl->index] = (u8)next->val;
195                 break;
196         case OV2680_16BIT:
197                 size = 2;
198                 data16 = (u16 *)&ctrl->buffer.data[ctrl->index];
199                 *data16 = cpu_to_be16((u16)next->val);
200                 break;
201         default:
202                 return -EINVAL;
203         }
204
205         /* When first item is added, we need to store its starting address */
206         if (ctrl->index == 0)
207                 ctrl->buffer.addr = next->reg;
208
209         ctrl->index += size;
210
211         /*
212          * Buffer cannot guarantee free space for u32? Better flush it to avoid
213          * possible lack of memory for next item.
214          */
215         if (ctrl->index + sizeof(u16) >= OV2680_MAX_WRITE_BUF_SIZE)
216                 return __ov2680_flush_reg_array(client, ctrl);
217
218         return 0;
219 }
220
221 static int __ov2680_write_reg_is_consecutive(struct i2c_client *client,
222                                              struct ov2680_write_ctrl *ctrl,
223                                              const struct ov2680_reg *next)
224 {
225         if (ctrl->index == 0)
226                 return 1;
227
228         return ctrl->buffer.addr + ctrl->index == next->reg;
229 }
230
231 static int ov2680_write_reg_array(struct i2c_client *client,
232                                   const struct ov2680_reg *reglist)
233 {
234         const struct ov2680_reg *next = reglist;
235         struct ov2680_write_ctrl ctrl;
236         int err;
237         dev_dbg(&client->dev,  "++++write reg array\n");
238         ctrl.index = 0;
239         for (; next->type != OV2680_TOK_TERM; next++) {
240                 switch (next->type & OV2680_TOK_MASK) {
241                 case OV2680_TOK_DELAY:
242                         err = __ov2680_flush_reg_array(client, &ctrl);
243                         if (err)
244                                 return err;
245                         msleep(next->val);
246                         break;
247                 default:
248                         /*
249                          * If next address is not consecutive, data needs to be
250                          * flushed before proceed.
251                          */
252                          dev_dbg(&client->dev,  "+++ov2680_write_reg_array reg=%x->%x\n", next->reg,next->val);
253                         if (!__ov2680_write_reg_is_consecutive(client, &ctrl,
254                                                                 next)) {
255                                 err = __ov2680_flush_reg_array(client, &ctrl);
256                         if (err)
257                                 return err;
258                         }
259                         err = __ov2680_buf_reg_array(client, &ctrl, next);
260                         if (err) {
261                                 dev_err(&client->dev, "%s: write error, aborted\n",
262                                          __func__);
263                                 return err;
264                         }
265                         break;
266                 }
267         }
268
269         return __ov2680_flush_reg_array(client, &ctrl);
270 }
271 static int ov2680_g_focal(struct v4l2_subdev *sd, s32 *val)
272 {
273
274         *val = (OV2680_FOCAL_LENGTH_NUM << 16) | OV2680_FOCAL_LENGTH_DEM;
275         return 0;
276 }
277
278 static int ov2680_g_fnumber(struct v4l2_subdev *sd, s32 *val)
279 {
280         /*const f number for ov2680*/
281
282         *val = (OV2680_F_NUMBER_DEFAULT_NUM << 16) | OV2680_F_NUMBER_DEM;
283         return 0;
284 }
285
286 static int ov2680_g_fnumber_range(struct v4l2_subdev *sd, s32 *val)
287 {
288         *val = (OV2680_F_NUMBER_DEFAULT_NUM << 24) |
289                 (OV2680_F_NUMBER_DEM << 16) |
290                 (OV2680_F_NUMBER_DEFAULT_NUM << 8) | OV2680_F_NUMBER_DEM;
291         return 0;
292 }
293
294 static int ov2680_g_bin_factor_x(struct v4l2_subdev *sd, s32 *val)
295 {
296         struct ov2680_device *dev = to_ov2680_sensor(sd);
297         struct i2c_client *client = v4l2_get_subdevdata(sd);
298         dev_dbg(&client->dev,  "++++ov2680_g_bin_factor_x\n");
299         *val = ov2680_res[dev->fmt_idx].bin_factor_x;
300
301         return 0;
302 }
303
304 static int ov2680_g_bin_factor_y(struct v4l2_subdev *sd, s32 *val)
305 {
306         struct ov2680_device *dev = to_ov2680_sensor(sd);
307         struct i2c_client *client = v4l2_get_subdevdata(sd);
308
309         *val = ov2680_res[dev->fmt_idx].bin_factor_y;
310         dev_dbg(&client->dev,  "++++ov2680_g_bin_factor_y\n");
311         return 0;
312 }
313
314
315 static int ov2680_get_intg_factor(struct i2c_client *client,
316                                 struct camera_mipi_info *info,
317                                 const struct ov2680_resolution *res)
318 {
319         struct v4l2_subdev *sd = i2c_get_clientdata(client);
320         struct ov2680_device *dev = to_ov2680_sensor(sd);
321         struct atomisp_sensor_mode_data *buf = &info->data;
322         unsigned int pix_clk_freq_hz;
323         u16 reg_val;
324         int ret;
325         dev_dbg(&client->dev,  "++++ov2680_get_intg_factor\n");
326         if (!info)
327                 return -EINVAL;
328
329         /* pixel clock */
330         pix_clk_freq_hz = res->pix_clk_freq * 1000000;
331
332         dev->vt_pix_clk_freq_mhz = pix_clk_freq_hz;
333         buf->vt_pix_clk_freq_mhz = pix_clk_freq_hz;
334
335         /* get integration time */
336         buf->coarse_integration_time_min = OV2680_COARSE_INTG_TIME_MIN;
337         buf->coarse_integration_time_max_margin =
338                                         OV2680_COARSE_INTG_TIME_MAX_MARGIN;
339
340         buf->fine_integration_time_min = OV2680_FINE_INTG_TIME_MIN;
341         buf->fine_integration_time_max_margin =
342                                         OV2680_FINE_INTG_TIME_MAX_MARGIN;
343
344         buf->fine_integration_time_def = OV2680_FINE_INTG_TIME_MIN;
345         buf->frame_length_lines = res->lines_per_frame;
346         buf->line_length_pck = res->pixels_per_line;
347         buf->read_mode = res->bin_mode;
348
349         /* get the cropping and output resolution to ISP for this mode. */
350         ret =  ov2680_read_reg(client, OV2680_16BIT,
351                                         OV2680_HORIZONTAL_START_H, &reg_val);
352         if (ret)
353                 return ret;
354         buf->crop_horizontal_start = reg_val;
355
356         ret =  ov2680_read_reg(client, OV2680_16BIT,
357                                         OV2680_VERTICAL_START_H, &reg_val);
358         if (ret)
359                 return ret;
360         buf->crop_vertical_start = reg_val;
361
362         ret = ov2680_read_reg(client, OV2680_16BIT,
363                                         OV2680_HORIZONTAL_END_H, &reg_val);
364         if (ret)
365                 return ret;
366         buf->crop_horizontal_end = reg_val;
367
368         ret = ov2680_read_reg(client, OV2680_16BIT,
369                                         OV2680_VERTICAL_END_H, &reg_val);
370         if (ret)
371                 return ret;
372         buf->crop_vertical_end = reg_val;
373
374         ret = ov2680_read_reg(client, OV2680_16BIT,
375                                         OV2680_HORIZONTAL_OUTPUT_SIZE_H, &reg_val);
376         if (ret)
377                 return ret;
378         buf->output_width = reg_val;
379
380         ret = ov2680_read_reg(client, OV2680_16BIT,
381                                         OV2680_VERTICAL_OUTPUT_SIZE_H, &reg_val);
382         if (ret)
383                 return ret;
384         buf->output_height = reg_val;
385
386         buf->binning_factor_x = res->bin_factor_x ?
387                                         (res->bin_factor_x * 2) : 1;
388         buf->binning_factor_y = res->bin_factor_y ?
389                                         (res->bin_factor_y * 2) : 1;
390         return 0;
391 }
392
393 static long __ov2680_set_exposure(struct v4l2_subdev *sd, int coarse_itg,
394                                  int gain, int digitgain)
395
396 {
397         struct i2c_client *client = v4l2_get_subdevdata(sd);
398         struct ov2680_device *dev = to_ov2680_sensor(sd);
399         u16 vts;
400         int ret,exp_val;
401
402        dev_dbg(&client->dev, "+++++++__ov2680_set_exposure coarse_itg %d, gain %d, digitgain %d++\n",coarse_itg, gain, digitgain);
403
404         vts = ov2680_res[dev->fmt_idx].lines_per_frame;
405
406         /* group hold */
407         ret = ov2680_write_reg(client, OV2680_8BIT,
408                                        OV2680_GROUP_ACCESS, 0x00);
409         if (ret) {
410                 dev_err(&client->dev, "%s: write %x error, aborted\n",
411                         __func__, OV2680_GROUP_ACCESS);
412                 return ret;
413         }
414
415         /* Increase the VTS to match exposure + MARGIN */
416         if (coarse_itg > vts - OV2680_INTEGRATION_TIME_MARGIN)
417                 vts = (u16) coarse_itg + OV2680_INTEGRATION_TIME_MARGIN;
418
419         ret = ov2680_write_reg(client, OV2680_16BIT, OV2680_TIMING_VTS_H, vts);
420         if (ret) {
421                 dev_err(&client->dev, "%s: write %x error, aborted\n",
422                         __func__, OV2680_TIMING_VTS_H);
423                 return ret;
424         }
425
426         /* set exposure */
427
428         /* Lower four bit should be 0*/
429         exp_val = coarse_itg << 4;
430         ret = ov2680_write_reg(client, OV2680_8BIT,
431                                OV2680_EXPOSURE_L, exp_val & 0xFF);
432         if (ret) {
433                 dev_err(&client->dev, "%s: write %x error, aborted\n",
434                         __func__, OV2680_EXPOSURE_L);
435                 return ret;
436         }
437
438         ret = ov2680_write_reg(client, OV2680_8BIT,
439                                OV2680_EXPOSURE_M, (exp_val >> 8) & 0xFF);
440         if (ret) {
441                 dev_err(&client->dev, "%s: write %x error, aborted\n",
442                         __func__, OV2680_EXPOSURE_M);
443                 return ret;
444         }
445
446         ret = ov2680_write_reg(client, OV2680_8BIT,
447                                OV2680_EXPOSURE_H, (exp_val >> 16) & 0x0F);
448         if (ret) {
449                 dev_err(&client->dev, "%s: write %x error, aborted\n",
450                         __func__, OV2680_EXPOSURE_H);
451                 return ret;
452         }
453
454         /* Analog gain */
455         ret = ov2680_write_reg(client, OV2680_16BIT, OV2680_AGC_H, gain);
456         if (ret) {
457                 dev_err(&client->dev, "%s: write %x error, aborted\n",
458                         __func__, OV2680_AGC_H);
459                 return ret;
460         }
461         /* Digital gain */
462         if (digitgain) {
463                 ret = ov2680_write_reg(client, OV2680_16BIT,
464                                 OV2680_MWB_RED_GAIN_H, digitgain);
465                 if (ret) {
466                         dev_err(&client->dev, "%s: write %x error, aborted\n",
467                                 __func__, OV2680_MWB_RED_GAIN_H);
468                         return ret;
469                 }
470
471                 ret = ov2680_write_reg(client, OV2680_16BIT,
472                                 OV2680_MWB_GREEN_GAIN_H, digitgain);
473                 if (ret) {
474                         dev_err(&client->dev, "%s: write %x error, aborted\n",
475                                 __func__, OV2680_MWB_RED_GAIN_H);
476                         return ret;
477                 }
478
479                 ret = ov2680_write_reg(client, OV2680_16BIT,
480                                 OV2680_MWB_BLUE_GAIN_H, digitgain);
481                 if (ret) {
482                         dev_err(&client->dev, "%s: write %x error, aborted\n",
483                                 __func__, OV2680_MWB_RED_GAIN_H);
484                         return ret;
485                 }
486         }
487
488         /* End group */
489         ret = ov2680_write_reg(client, OV2680_8BIT,
490                                OV2680_GROUP_ACCESS, 0x10);
491         if (ret)
492                 return ret;
493
494         /* Delay launch group */
495         ret = ov2680_write_reg(client, OV2680_8BIT,
496                                            OV2680_GROUP_ACCESS, 0xa0);
497         if (ret)
498                 return ret;
499         return ret;
500 }
501
502 static int ov2680_set_exposure(struct v4l2_subdev *sd, int exposure,
503         int gain, int digitgain)
504 {
505         struct ov2680_device *dev = to_ov2680_sensor(sd);
506         int ret;
507
508         mutex_lock(&dev->input_lock);
509         ret = __ov2680_set_exposure(sd, exposure, gain, digitgain);
510         mutex_unlock(&dev->input_lock);
511
512         return ret;
513 }
514
515 static long ov2680_s_exposure(struct v4l2_subdev *sd,
516                                struct atomisp_exposure *exposure)
517 {
518         u16 coarse_itg = exposure->integration_time[0];
519         u16 analog_gain = exposure->gain[0];
520         u16 digital_gain = exposure->gain[1];
521
522         /* we should not accept the invalid value below */
523         if (analog_gain == 0) {
524                 struct i2c_client *client = v4l2_get_subdevdata(sd);
525                 v4l2_err(client, "%s: invalid value\n", __func__);
526                 return -EINVAL;
527         }
528
529         // EXPOSURE CONTROL DISABLED FOR INITIAL CHECKIN, TUNING DOESN'T WORK
530         return ov2680_set_exposure(sd, coarse_itg, analog_gain, digital_gain);
531 }
532
533
534
535
536
537 static long ov2680_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
538 {
539
540         switch (cmd) {
541         case ATOMISP_IOC_S_EXPOSURE:
542                 return ov2680_s_exposure(sd, arg);
543
544         default:
545                 return -EINVAL;
546         }
547         return 0;
548 }
549
550 /* This returns the exposure time being used. This should only be used
551  * for filling in EXIF data, not for actual image processing.
552  */
553 static int ov2680_q_exposure(struct v4l2_subdev *sd, s32 *value)
554 {
555         struct i2c_client *client = v4l2_get_subdevdata(sd);
556         u16 reg_v, reg_v2;
557         int ret;
558
559         /* get exposure */
560         ret = ov2680_read_reg(client, OV2680_8BIT,
561                                         OV2680_EXPOSURE_L,
562                                         &reg_v);
563         if (ret)
564                 goto err;
565
566         ret = ov2680_read_reg(client, OV2680_8BIT,
567                                         OV2680_EXPOSURE_M,
568                                         &reg_v2);
569         if (ret)
570                 goto err;
571
572         reg_v += reg_v2 << 8;
573         ret = ov2680_read_reg(client, OV2680_8BIT,
574                                         OV2680_EXPOSURE_H,
575                                         &reg_v2);
576         if (ret)
577                 goto err;
578
579         *value = reg_v + (((u32)reg_v2 << 16));
580 err:
581         return ret;
582 }
583
584 static u32 ov2680_translate_bayer_order(enum atomisp_bayer_order code)
585 {
586         switch (code) {
587         case atomisp_bayer_order_rggb:
588                 return MEDIA_BUS_FMT_SRGGB10_1X10;
589         case atomisp_bayer_order_grbg:
590                 return MEDIA_BUS_FMT_SGRBG10_1X10;
591         case atomisp_bayer_order_bggr:
592                 return MEDIA_BUS_FMT_SBGGR10_1X10;
593         case atomisp_bayer_order_gbrg:
594                 return MEDIA_BUS_FMT_SGBRG10_1X10;
595         }
596         return 0;
597 }
598
599 static int ov2680_v_flip(struct v4l2_subdev *sd, s32 value)
600 {
601         struct ov2680_device *dev = to_ov2680_sensor(sd);
602         struct camera_mipi_info *ov2680_info = NULL;
603         struct i2c_client *client = v4l2_get_subdevdata(sd);
604         int ret;
605         u16 val;
606         u8 index;
607         dev_dbg(&client->dev, "@%s: value:%d\n", __func__, value);
608         ret = ov2680_read_reg(client, OV2680_8BIT, OV2680_FLIP_REG, &val);
609         if (ret)
610                 return ret;
611         if (value) {
612                 val |= OV2680_FLIP_MIRROR_BIT_ENABLE;
613         } else {
614                 val &= ~OV2680_FLIP_MIRROR_BIT_ENABLE;
615         }
616         ret = ov2680_write_reg(client, OV2680_8BIT,
617                         OV2680_FLIP_REG, val);
618         if (ret)
619                 return ret;
620         index = (v_flag>0?OV2680_FLIP_BIT:0) | (h_flag>0?OV2680_MIRROR_BIT:0);
621         ov2680_info = v4l2_get_subdev_hostdata(sd);
622         if (ov2680_info) {
623                 ov2680_info->raw_bayer_order = ov2680_bayer_order_mapping[index];
624                 dev->format.code = ov2680_translate_bayer_order(
625                         ov2680_info->raw_bayer_order);
626         }
627         return ret;
628 }
629
630 static int ov2680_h_flip(struct v4l2_subdev *sd, s32 value)
631 {
632         struct ov2680_device *dev = to_ov2680_sensor(sd);
633         struct camera_mipi_info *ov2680_info = NULL;
634         struct i2c_client *client = v4l2_get_subdevdata(sd);
635         int ret;
636         u16 val;
637         u8 index;
638         dev_dbg(&client->dev, "@%s: value:%d\n", __func__, value);
639
640         ret = ov2680_read_reg(client, OV2680_8BIT, OV2680_MIRROR_REG, &val);
641         if (ret)
642                 return ret;
643         if (value) {
644                 val |= OV2680_FLIP_MIRROR_BIT_ENABLE;
645         } else {
646                 val &= ~OV2680_FLIP_MIRROR_BIT_ENABLE;
647         }
648         ret = ov2680_write_reg(client, OV2680_8BIT,
649                         OV2680_MIRROR_REG, val);
650         if (ret)
651                 return ret;
652         index = (v_flag>0?OV2680_FLIP_BIT:0) | (h_flag>0?OV2680_MIRROR_BIT:0);
653         ov2680_info = v4l2_get_subdev_hostdata(sd);
654         if (ov2680_info) {
655                 ov2680_info->raw_bayer_order = ov2680_bayer_order_mapping[index];
656                 dev->format.code = ov2680_translate_bayer_order(
657                         ov2680_info->raw_bayer_order);
658         }
659         return ret;
660 }
661
662 static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl)
663 {
664         struct ov2680_device *dev =
665             container_of(ctrl->handler, struct ov2680_device, ctrl_handler);
666         struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
667         int ret = 0;
668
669         switch (ctrl->id) {
670         case V4L2_CID_VFLIP:
671                 dev_dbg(&client->dev, "%s: CID_VFLIP:%d.\n",
672                         __func__, ctrl->val);
673                 ret = ov2680_v_flip(&dev->sd, ctrl->val);
674                 break;
675         case V4L2_CID_HFLIP:
676                 dev_dbg(&client->dev, "%s: CID_HFLIP:%d.\n",
677                         __func__, ctrl->val);
678                 ret = ov2680_h_flip(&dev->sd, ctrl->val);
679                 break;
680         default:
681                 ret = -EINVAL;
682         }
683         return ret;
684 }
685
686 static int ov2680_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
687 {
688         struct ov2680_device *dev =
689             container_of(ctrl->handler, struct ov2680_device, ctrl_handler);
690         int ret = 0;
691
692         switch (ctrl->id) {
693         case V4L2_CID_EXPOSURE_ABSOLUTE:
694                 ret = ov2680_q_exposure(&dev->sd, &ctrl->val);
695                 break;
696         case V4L2_CID_FOCAL_ABSOLUTE:
697                 ret = ov2680_g_focal(&dev->sd, &ctrl->val);
698                 break;
699         case V4L2_CID_FNUMBER_ABSOLUTE:
700                 ret = ov2680_g_fnumber(&dev->sd, &ctrl->val);
701                 break;
702         case V4L2_CID_FNUMBER_RANGE:
703                 ret = ov2680_g_fnumber_range(&dev->sd, &ctrl->val);
704                 break;
705         case V4L2_CID_BIN_FACTOR_HORZ:
706                 ret = ov2680_g_bin_factor_x(&dev->sd, &ctrl->val);
707                 break;
708         case V4L2_CID_BIN_FACTOR_VERT:
709                 ret = ov2680_g_bin_factor_y(&dev->sd, &ctrl->val);
710                 break;
711         default:
712                 ret = -EINVAL;
713         }
714
715         return ret;
716 }
717
718 static const struct v4l2_ctrl_ops ctrl_ops = {
719         .s_ctrl = ov2680_s_ctrl,
720         .g_volatile_ctrl = ov2680_g_volatile_ctrl
721 };
722
723 struct v4l2_ctrl_config ov2680_controls[] = {
724         {
725          .ops = &ctrl_ops,
726          .id = V4L2_CID_EXPOSURE_ABSOLUTE,
727          .type = V4L2_CTRL_TYPE_INTEGER,
728          .name = "exposure",
729          .min = 0x0,
730          .max = 0xffff,
731          .step = 0x01,
732          .def = 0x00,
733          .flags = 0,
734          },
735         {
736          .ops = &ctrl_ops,
737          .id = V4L2_CID_FOCAL_ABSOLUTE,
738          .type = V4L2_CTRL_TYPE_INTEGER,
739          .name = "focal length",
740          .min = OV2680_FOCAL_LENGTH_DEFAULT,
741          .max = OV2680_FOCAL_LENGTH_DEFAULT,
742          .step = 0x01,
743          .def = OV2680_FOCAL_LENGTH_DEFAULT,
744          .flags = 0,
745          },
746         {
747          .ops = &ctrl_ops,
748          .id = V4L2_CID_FNUMBER_ABSOLUTE,
749          .type = V4L2_CTRL_TYPE_INTEGER,
750          .name = "f-number",
751          .min = OV2680_F_NUMBER_DEFAULT,
752          .max = OV2680_F_NUMBER_DEFAULT,
753          .step = 0x01,
754          .def = OV2680_F_NUMBER_DEFAULT,
755          .flags = 0,
756          },
757         {
758          .ops = &ctrl_ops,
759          .id = V4L2_CID_FNUMBER_RANGE,
760          .type = V4L2_CTRL_TYPE_INTEGER,
761          .name = "f-number range",
762          .min = OV2680_F_NUMBER_RANGE,
763          .max = OV2680_F_NUMBER_RANGE,
764          .step = 0x01,
765          .def = OV2680_F_NUMBER_RANGE,
766          .flags = 0,
767          },
768         {
769          .ops = &ctrl_ops,
770          .id = V4L2_CID_BIN_FACTOR_HORZ,
771          .type = V4L2_CTRL_TYPE_INTEGER,
772          .name = "horizontal binning factor",
773          .min = 0,
774          .max = OV2680_BIN_FACTOR_MAX,
775          .step = 1,
776          .def = 0,
777          .flags = 0,
778          },
779         {
780          .ops = &ctrl_ops,
781          .id = V4L2_CID_BIN_FACTOR_VERT,
782          .type = V4L2_CTRL_TYPE_INTEGER,
783          .name = "vertical binning factor",
784          .min = 0,
785          .max = OV2680_BIN_FACTOR_MAX,
786          .step = 1,
787          .def = 0,
788          .flags = 0,
789          },
790         {
791          .ops = &ctrl_ops,
792          .id = V4L2_CID_VFLIP,
793          .type = V4L2_CTRL_TYPE_BOOLEAN,
794          .name = "Flip",
795          .min = 0,
796          .max = 1,
797          .step = 1,
798          .def = 0,
799          },
800         {
801          .ops = &ctrl_ops,
802          .id = V4L2_CID_HFLIP,
803          .type = V4L2_CTRL_TYPE_BOOLEAN,
804          .name = "Mirror",
805          .min = 0,
806          .max = 1,
807          .step = 1,
808          .def = 0,
809          },
810 };
811
812 static int ov2680_init_registers(struct v4l2_subdev *sd)
813 {
814         struct i2c_client *client = v4l2_get_subdevdata(sd);
815         int ret;
816
817         ret = ov2680_write_reg(client, OV2680_8BIT, OV2680_SW_RESET, 0x01);
818         ret |= ov2680_write_reg_array(client, ov2680_global_setting);
819
820         return ret;
821 }
822
823 static int ov2680_init(struct v4l2_subdev *sd)
824 {
825         struct ov2680_device *dev = to_ov2680_sensor(sd);
826
827         int ret;
828
829         mutex_lock(&dev->input_lock);
830
831         /* restore settings */
832         ov2680_res = ov2680_res_preview;
833         N_RES = N_RES_PREVIEW;
834
835         ret = ov2680_init_registers(sd);
836
837         mutex_unlock(&dev->input_lock);
838
839         return ret;
840 }
841
842 static int power_ctrl(struct v4l2_subdev *sd, bool flag)
843 {
844         int ret = 0;
845         struct ov2680_device *dev = to_ov2680_sensor(sd);
846         if (!dev || !dev->platform_data)
847                 return -ENODEV;
848
849         /* Non-gmin platforms use the legacy callback */
850         if (dev->platform_data->power_ctrl)
851                 return dev->platform_data->power_ctrl(sd, flag);
852
853         if (flag) {
854                 ret |= dev->platform_data->v1p8_ctrl(sd, 1);
855                 ret |= dev->platform_data->v2p8_ctrl(sd, 1);
856                 usleep_range(10000, 15000);
857         }
858
859         if (!flag || ret) {
860                 ret |= dev->platform_data->v1p8_ctrl(sd, 0);
861                 ret |= dev->platform_data->v2p8_ctrl(sd, 0);
862         }
863         return ret;
864 }
865
866 static int gpio_ctrl(struct v4l2_subdev *sd, bool flag)
867 {
868         int ret;
869         struct ov2680_device *dev = to_ov2680_sensor(sd);
870
871         if (!dev || !dev->platform_data)
872                 return -ENODEV;
873
874         /* Non-gmin platforms use the legacy callback */
875         if (dev->platform_data->gpio_ctrl)
876                 return dev->platform_data->gpio_ctrl(sd, flag);
877
878         /* The OV2680 documents only one GPIO input (#XSHUTDN), but
879          * existing integrations often wire two (reset/power_down)
880          * because that is the way other sensors work.  There is no
881          * way to tell how it is wired internally, so existing
882          * firmwares expose both and we drive them symmetrically. */
883         if (flag) {
884                 ret = dev->platform_data->gpio0_ctrl(sd, 1);
885                 usleep_range(10000, 15000);
886                 /* Ignore return from second gpio, it may not be there */
887                 dev->platform_data->gpio1_ctrl(sd, 1);
888                 usleep_range(10000, 15000);
889         } else {
890                 dev->platform_data->gpio1_ctrl(sd, 0);
891                 ret = dev->platform_data->gpio0_ctrl(sd, 0);
892         }
893         return ret;
894 }
895
896 static int power_up(struct v4l2_subdev *sd)
897 {
898         struct ov2680_device *dev = to_ov2680_sensor(sd);
899         struct i2c_client *client = v4l2_get_subdevdata(sd);
900         int ret;
901
902         if (!dev->platform_data) {
903                 dev_err(&client->dev,
904                         "no camera_sensor_platform_data");
905                 return -ENODEV;
906         }
907
908         /* power control */
909         ret = power_ctrl(sd, 1);
910         if (ret)
911                 goto fail_power;
912
913         /* according to DS, at least 5ms is needed between DOVDD and PWDN */
914         usleep_range(5000, 6000);
915
916         /* gpio ctrl */
917         ret = gpio_ctrl(sd, 1);
918         if (ret) {
919                 ret = gpio_ctrl(sd, 1);
920                 if (ret)
921                         goto fail_power;
922         }
923
924         /* flis clock control */
925         ret = dev->platform_data->flisclk_ctrl(sd, 1);
926         if (ret)
927                 goto fail_clk;
928
929         /* according to DS, 20ms is needed between PWDN and i2c access */
930         msleep(20);
931
932         return 0;
933
934 fail_clk:
935         gpio_ctrl(sd, 0);
936 fail_power:
937         power_ctrl(sd, 0);
938         dev_err(&client->dev, "sensor power-up failed\n");
939
940         return ret;
941 }
942
943 static int power_down(struct v4l2_subdev *sd)
944 {
945         struct ov2680_device *dev = to_ov2680_sensor(sd);
946         struct i2c_client *client = v4l2_get_subdevdata(sd);
947         int ret = 0;
948
949         h_flag = 0;
950         v_flag = 0;
951         if (!dev->platform_data) {
952                 dev_err(&client->dev,
953                         "no camera_sensor_platform_data");
954                 return -ENODEV;
955         }
956
957         ret = dev->platform_data->flisclk_ctrl(sd, 0);
958         if (ret)
959                 dev_err(&client->dev, "flisclk failed\n");
960
961         /* gpio ctrl */
962         ret = gpio_ctrl(sd, 0);
963         if (ret) {
964                 ret = gpio_ctrl(sd, 0);
965                 if (ret)
966                         dev_err(&client->dev, "gpio failed 2\n");
967         }
968
969         /* power control */
970         ret = power_ctrl(sd, 0);
971         if (ret)
972                 dev_err(&client->dev, "vprog failed.\n");
973
974         return ret;
975 }
976
977 static int ov2680_s_power(struct v4l2_subdev *sd, int on)
978 {
979         int ret;
980
981         if (on == 0){
982                 ret = power_down(sd);
983         } else {
984                 ret = power_up(sd);
985                 if (!ret)
986                         return ov2680_init(sd);
987         }
988         return ret;
989 }
990
991 /*
992  * distance - calculate the distance
993  * @res: resolution
994  * @w: width
995  * @h: height
996  *
997  * Get the gap between resolution and w/h.
998  * res->width/height smaller than w/h wouldn't be considered.
999  * Returns the value of gap or -1 if fail.
1000  */
1001 #define LARGEST_ALLOWED_RATIO_MISMATCH 600
1002 static int distance(struct ov2680_resolution *res, u32 w, u32 h)
1003 {
1004         unsigned int w_ratio = (res->width << 13) / w;
1005         unsigned int h_ratio;
1006         int match;
1007
1008         if (h == 0)
1009                 return -1;
1010         h_ratio = (res->height << 13) / h;
1011         if (h_ratio == 0)
1012                 return -1;
1013         match   = abs(((w_ratio << 13) / h_ratio) - ((int)8192));
1014
1015
1016         if ((w_ratio < (int)8192) || (h_ratio < (int)8192)  ||
1017                 (match > LARGEST_ALLOWED_RATIO_MISMATCH))
1018                 return -1;
1019
1020         return w_ratio + h_ratio;
1021 }
1022
1023 /* Return the nearest higher resolution index */
1024 static int nearest_resolution_index(int w, int h)
1025 {
1026         int i;
1027         int idx = -1;
1028         int dist;
1029         int min_dist = INT_MAX;
1030         struct ov2680_resolution *tmp_res = NULL;
1031
1032         for (i = 0; i < N_RES; i++) {
1033                 tmp_res = &ov2680_res[i];
1034                 dist = distance(tmp_res, w, h);
1035                 if (dist == -1)
1036                         continue;
1037                 if (dist < min_dist) {
1038                         min_dist = dist;
1039                         idx = i;
1040                 }
1041         }
1042
1043         return idx;
1044 }
1045
1046 static int get_resolution_index(int w, int h)
1047 {
1048         int i;
1049
1050         for (i = 0; i < N_RES; i++) {
1051                 if (w != ov2680_res[i].width)
1052                         continue;
1053                 if (h != ov2680_res[i].height)
1054                         continue;
1055
1056                 return i;
1057         }
1058
1059         return -1;
1060 }
1061
1062 static int ov2680_set_fmt(struct v4l2_subdev *sd,
1063                           struct v4l2_subdev_pad_config *cfg,
1064                           struct v4l2_subdev_format *format)
1065 {
1066         struct v4l2_mbus_framefmt *fmt = &format->format;
1067         struct ov2680_device *dev = to_ov2680_sensor(sd);
1068         struct i2c_client *client = v4l2_get_subdevdata(sd);
1069         struct camera_mipi_info *ov2680_info = NULL;
1070         int ret = 0;
1071         int idx = 0;
1072         dev_dbg(&client->dev, "+++++ov2680_s_mbus_fmt+++++l\n");
1073         if (format->pad)
1074                 return -EINVAL;
1075
1076         if (!fmt)
1077                 return -EINVAL;
1078
1079         ov2680_info = v4l2_get_subdev_hostdata(sd);
1080         if (!ov2680_info)
1081                 return -EINVAL;
1082
1083         mutex_lock(&dev->input_lock);
1084         idx = nearest_resolution_index(fmt->width, fmt->height);
1085         if (idx == -1) {
1086                 /* return the largest resolution */
1087                 fmt->width = ov2680_res[N_RES - 1].width;
1088                 fmt->height = ov2680_res[N_RES - 1].height;
1089         } else {
1090                 fmt->width = ov2680_res[idx].width;
1091                 fmt->height = ov2680_res[idx].height;
1092         }
1093         fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
1094         if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
1095                 cfg->try_fmt = *fmt;
1096                 mutex_unlock(&dev->input_lock);
1097                 return 0;
1098                 }
1099         dev->fmt_idx = get_resolution_index(fmt->width, fmt->height);
1100         dev_dbg(&client->dev, "+++++get_resolution_index=%d+++++l\n",
1101                      dev->fmt_idx);
1102         if (dev->fmt_idx == -1) {
1103                 dev_err(&client->dev, "get resolution fail\n");
1104                 mutex_unlock(&dev->input_lock);
1105                 return -EINVAL;
1106         }
1107         v4l2_info(client, "__s_mbus_fmt i=%d, w=%d, h=%d\n", dev->fmt_idx,
1108                   fmt->width, fmt->height);
1109         dev_dbg(&client->dev, "__s_mbus_fmt i=%d, w=%d, h=%d\n",
1110                      dev->fmt_idx, fmt->width, fmt->height);
1111
1112         ret = ov2680_write_reg_array(client, ov2680_res[dev->fmt_idx].regs);
1113         if (ret)
1114                 dev_err(&client->dev, "ov2680 write resolution register err\n");
1115
1116         ret = ov2680_get_intg_factor(client, ov2680_info,
1117                                      &ov2680_res[dev->fmt_idx]);
1118         if (ret) {
1119                 dev_err(&client->dev, "failed to get integration_factor\n");
1120                 goto err;
1121         }
1122
1123         /*recall flip functions to avoid flip registers
1124          * were overridden by default setting
1125          */
1126         if (h_flag)
1127                 ov2680_h_flip(sd, h_flag);
1128         if (v_flag)
1129                 ov2680_v_flip(sd, v_flag);
1130
1131         v4l2_info(client, "\n%s idx %d \n", __func__, dev->fmt_idx);
1132
1133         /*ret = startup(sd);
1134          * if (ret)
1135          * dev_err(&client->dev, "ov2680 startup err\n");
1136          */
1137 err:
1138         mutex_unlock(&dev->input_lock);
1139         return ret;
1140 }
1141
1142 static int ov2680_get_fmt(struct v4l2_subdev *sd,
1143                           struct v4l2_subdev_pad_config *cfg,
1144                           struct v4l2_subdev_format *format)
1145 {
1146         struct v4l2_mbus_framefmt *fmt = &format->format;
1147         struct ov2680_device *dev = to_ov2680_sensor(sd);
1148
1149         if (format->pad)
1150                 return -EINVAL;
1151
1152         if (!fmt)
1153                 return -EINVAL;
1154
1155         fmt->width = ov2680_res[dev->fmt_idx].width;
1156         fmt->height = ov2680_res[dev->fmt_idx].height;
1157         fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
1158
1159         return 0;
1160 }
1161
1162 static int ov2680_detect(struct i2c_client *client)
1163 {
1164         struct i2c_adapter *adapter = client->adapter;
1165         u16 high, low;
1166         int ret;
1167         u16 id;
1168         u8 revision;
1169
1170         if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
1171                 return -ENODEV;
1172
1173         ret = ov2680_read_reg(client, OV2680_8BIT,
1174                                         OV2680_SC_CMMN_CHIP_ID_H, &high);
1175         if (ret) {
1176                 dev_err(&client->dev, "sensor_id_high = 0x%x\n", high);
1177                 return -ENODEV;
1178         }
1179         ret = ov2680_read_reg(client, OV2680_8BIT,
1180                                         OV2680_SC_CMMN_CHIP_ID_L, &low);
1181         id = ((((u16) high) << 8) | (u16) low);
1182
1183         if (id != OV2680_ID) {
1184                 dev_err(&client->dev, "sensor ID error 0x%x\n", id);
1185                 return -ENODEV;
1186         }
1187
1188         ret = ov2680_read_reg(client, OV2680_8BIT,
1189                                         OV2680_SC_CMMN_SUB_ID, &high);
1190         revision = (u8) high & 0x0f;
1191
1192         dev_info(&client->dev, "sensor_revision id = 0x%x, rev= %d\n",
1193                  id, revision);
1194
1195         return 0;
1196 }
1197
1198 static int ov2680_s_stream(struct v4l2_subdev *sd, int enable)
1199 {
1200         struct ov2680_device *dev = to_ov2680_sensor(sd);
1201         struct i2c_client *client = v4l2_get_subdevdata(sd);
1202         int ret;
1203
1204         mutex_lock(&dev->input_lock);
1205         if(enable )
1206                 dev_dbg(&client->dev, "ov2680_s_stream one \n");
1207         else
1208                 dev_dbg(&client->dev, "ov2680_s_stream off \n");
1209
1210         ret = ov2680_write_reg(client, OV2680_8BIT, OV2680_SW_STREAM,
1211                                 enable ? OV2680_START_STREAMING :
1212                                 OV2680_STOP_STREAMING);
1213 #if 0
1214         /* restore settings */
1215         ov2680_res = ov2680_res_preview;
1216         N_RES = N_RES_PREVIEW;
1217 #endif
1218
1219         //otp valid at stream on state
1220         //if(!dev->otp_data)
1221         //      dev->otp_data = ov2680_otp_read(sd);
1222
1223         mutex_unlock(&dev->input_lock);
1224
1225         return ret;
1226 }
1227
1228
1229 static int ov2680_s_config(struct v4l2_subdev *sd,
1230                            int irq, void *platform_data)
1231 {
1232         struct ov2680_device *dev = to_ov2680_sensor(sd);
1233         struct i2c_client *client = v4l2_get_subdevdata(sd);
1234         int ret = 0;
1235
1236         if (!platform_data)
1237                 return -ENODEV;
1238
1239         dev->platform_data =
1240                 (struct camera_sensor_platform_data *)platform_data;
1241
1242         mutex_lock(&dev->input_lock);
1243         /* power off the module, then power on it in future
1244          * as first power on by board may not fulfill the
1245          * power on sequqence needed by the module
1246          */
1247         ret = power_down(sd);
1248         if (ret) {
1249                 dev_err(&client->dev, "ov2680 power-off err.\n");
1250                 goto fail_power_off;
1251         }
1252
1253         ret = power_up(sd);
1254         if (ret) {
1255                 dev_err(&client->dev, "ov2680 power-up err.\n");
1256                 goto fail_power_on;
1257         }
1258
1259         ret = dev->platform_data->csi_cfg(sd, 1);
1260         if (ret)
1261                 goto fail_csi_cfg;
1262
1263         /* config & detect sensor */
1264         ret = ov2680_detect(client);
1265         if (ret) {
1266                 dev_err(&client->dev, "ov2680_detect err s_config.\n");
1267                 goto fail_csi_cfg;
1268         }
1269
1270         /* turn off sensor, after probed */
1271         ret = power_down(sd);
1272         if (ret) {
1273                 dev_err(&client->dev, "ov2680 power-off err.\n");
1274                 goto fail_csi_cfg;
1275         }
1276         mutex_unlock(&dev->input_lock);
1277
1278         return 0;
1279
1280 fail_csi_cfg:
1281         dev->platform_data->csi_cfg(sd, 0);
1282 fail_power_on:
1283         power_down(sd);
1284         dev_err(&client->dev, "sensor power-gating failed\n");
1285 fail_power_off:
1286         mutex_unlock(&dev->input_lock);
1287         return ret;
1288 }
1289
1290 static int ov2680_g_parm(struct v4l2_subdev *sd,
1291                         struct v4l2_streamparm *param)
1292 {
1293         struct ov2680_device *dev = to_ov2680_sensor(sd);
1294         struct i2c_client *client = v4l2_get_subdevdata(sd);
1295
1296         if (!param)
1297                 return -EINVAL;
1298
1299         if (param->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1300                 dev_err(&client->dev,  "unsupported buffer type.\n");
1301                 return -EINVAL;
1302         }
1303
1304         memset(param, 0, sizeof(*param));
1305         param->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1306
1307         if (dev->fmt_idx >= 0 && dev->fmt_idx < N_RES) {
1308                 param->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1309                 param->parm.capture.timeperframe.numerator = 1;
1310                 param->parm.capture.capturemode = dev->run_mode;
1311                 param->parm.capture.timeperframe.denominator =
1312                         ov2680_res[dev->fmt_idx].fps;
1313         }
1314         return 0;
1315 }
1316
1317 static int ov2680_s_parm(struct v4l2_subdev *sd,
1318                         struct v4l2_streamparm *param)
1319 {
1320         struct ov2680_device *dev = to_ov2680_sensor(sd);
1321         struct i2c_client *client = v4l2_get_subdevdata(sd);
1322         dev->run_mode = param->parm.capture.capturemode;
1323
1324         v4l2_info(client, "\n%s:run_mode :%x\n", __func__, dev->run_mode);
1325
1326         mutex_lock(&dev->input_lock);
1327         switch (dev->run_mode) {
1328         case CI_MODE_VIDEO:
1329                 ov2680_res = ov2680_res_video;
1330                 N_RES = N_RES_VIDEO;
1331                 break;
1332         case CI_MODE_STILL_CAPTURE:
1333                 ov2680_res = ov2680_res_still;
1334                 N_RES = N_RES_STILL;
1335                 break;
1336         default:
1337                 ov2680_res = ov2680_res_preview;
1338                 N_RES = N_RES_PREVIEW;
1339         }
1340         mutex_unlock(&dev->input_lock);
1341         return 0;
1342 }
1343
1344 static int ov2680_g_frame_interval(struct v4l2_subdev *sd,
1345                                    struct v4l2_subdev_frame_interval *interval)
1346 {
1347         struct ov2680_device *dev = to_ov2680_sensor(sd);
1348
1349         interval->interval.numerator = 1;
1350         interval->interval.denominator = ov2680_res[dev->fmt_idx].fps;
1351
1352         return 0;
1353 }
1354
1355 static int ov2680_enum_mbus_code(struct v4l2_subdev *sd,
1356                                  struct v4l2_subdev_pad_config *cfg,
1357                                  struct v4l2_subdev_mbus_code_enum *code)
1358 {
1359         if (code->index >= MAX_FMTS)
1360                 return -EINVAL;
1361
1362         code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
1363         return 0;
1364 }
1365
1366 static int ov2680_enum_frame_size(struct v4l2_subdev *sd,
1367                                   struct v4l2_subdev_pad_config *cfg,
1368                                   struct v4l2_subdev_frame_size_enum *fse)
1369 {
1370         int index = fse->index;
1371
1372         if (index >= N_RES)
1373                 return -EINVAL;
1374
1375         fse->min_width = ov2680_res[index].width;
1376         fse->min_height = ov2680_res[index].height;
1377         fse->max_width = ov2680_res[index].width;
1378         fse->max_height = ov2680_res[index].height;
1379
1380         return 0;
1381
1382 }
1383
1384 static int ov2680_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
1385 {
1386         struct ov2680_device *dev = to_ov2680_sensor(sd);
1387
1388         mutex_lock(&dev->input_lock);
1389         *frames = ov2680_res[dev->fmt_idx].skip_frames;
1390         mutex_unlock(&dev->input_lock);
1391
1392         return 0;
1393 }
1394
1395 static const struct v4l2_subdev_video_ops ov2680_video_ops = {
1396         .s_stream = ov2680_s_stream,
1397         .g_parm = ov2680_g_parm,
1398         .s_parm = ov2680_s_parm,
1399         .g_frame_interval = ov2680_g_frame_interval,
1400 };
1401
1402 static const struct v4l2_subdev_sensor_ops ov2680_sensor_ops = {
1403                 .g_skip_frames  = ov2680_g_skip_frames,
1404 };
1405
1406 static const struct v4l2_subdev_core_ops ov2680_core_ops = {
1407         .s_power = ov2680_s_power,
1408         .ioctl = ov2680_ioctl,
1409 };
1410
1411 static const struct v4l2_subdev_pad_ops ov2680_pad_ops = {
1412         .enum_mbus_code = ov2680_enum_mbus_code,
1413         .enum_frame_size = ov2680_enum_frame_size,
1414         .get_fmt = ov2680_get_fmt,
1415         .set_fmt = ov2680_set_fmt,
1416 };
1417
1418 static const struct v4l2_subdev_ops ov2680_ops = {
1419         .core = &ov2680_core_ops,
1420         .video = &ov2680_video_ops,
1421         .pad = &ov2680_pad_ops,
1422         .sensor = &ov2680_sensor_ops,
1423 };
1424
1425 static int ov2680_remove(struct i2c_client *client)
1426 {
1427         struct v4l2_subdev *sd = i2c_get_clientdata(client);
1428         struct ov2680_device *dev = to_ov2680_sensor(sd);
1429         dev_dbg(&client->dev, "ov2680_remove...\n");
1430
1431         dev->platform_data->csi_cfg(sd, 0);
1432
1433         v4l2_device_unregister_subdev(sd);
1434         media_entity_cleanup(&dev->sd.entity);
1435         v4l2_ctrl_handler_free(&dev->ctrl_handler);
1436         kfree(dev);
1437
1438         return 0;
1439 }
1440
1441 static int ov2680_probe(struct i2c_client *client,
1442                         const struct i2c_device_id *id)
1443 {
1444         struct ov2680_device *dev;
1445         int ret;
1446         void *pdata;
1447         unsigned int i;
1448
1449         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1450         if (!dev) {
1451                 dev_err(&client->dev, "out of memory\n");
1452                 return -ENOMEM;
1453         }
1454
1455         mutex_init(&dev->input_lock);
1456
1457         dev->fmt_idx = 0;
1458         v4l2_i2c_subdev_init(&(dev->sd), client, &ov2680_ops);
1459
1460         if (ACPI_COMPANION(&client->dev))
1461                 pdata = gmin_camera_platform_data(&dev->sd,
1462                                                   ATOMISP_INPUT_FORMAT_RAW_10,
1463                                                   atomisp_bayer_order_bggr);
1464         else
1465                 pdata = client->dev.platform_data;
1466
1467         if (!pdata) {
1468                 ret = -EINVAL;
1469                 goto out_free;
1470         }
1471
1472         ret = ov2680_s_config(&dev->sd, client->irq, pdata);
1473         if (ret)
1474                 goto out_free;
1475
1476         ret = atomisp_register_i2c_module(&dev->sd, pdata, RAW_CAMERA);
1477         if (ret)
1478                 goto out_free;
1479
1480         dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1481         dev->pad.flags = MEDIA_PAD_FL_SOURCE;
1482         dev->format.code = MEDIA_BUS_FMT_SBGGR10_1X10;
1483         dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1484         ret =
1485             v4l2_ctrl_handler_init(&dev->ctrl_handler,
1486                                    ARRAY_SIZE(ov2680_controls));
1487         if (ret) {
1488                 ov2680_remove(client);
1489                 return ret;
1490         }
1491
1492         for (i = 0; i < ARRAY_SIZE(ov2680_controls); i++)
1493                 v4l2_ctrl_new_custom(&dev->ctrl_handler, &ov2680_controls[i],
1494                                      NULL);
1495
1496         if (dev->ctrl_handler.error) {
1497                 ov2680_remove(client);
1498                 return dev->ctrl_handler.error;
1499         }
1500
1501         /* Use same lock for controls as for everything else. */
1502         dev->ctrl_handler.lock = &dev->input_lock;
1503         dev->sd.ctrl_handler = &dev->ctrl_handler;
1504
1505         ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad);
1506         if (ret)
1507         {
1508                 ov2680_remove(client);
1509                 dev_dbg(&client->dev, "+++ remove ov2680 \n");
1510         }
1511         return ret;
1512 out_free:
1513         dev_dbg(&client->dev, "+++ out free \n");
1514         v4l2_device_unregister_subdev(&dev->sd);
1515         kfree(dev);
1516         return ret;
1517 }
1518
1519 static const struct acpi_device_id ov2680_acpi_match[] = {
1520         {"XXOV2680"},
1521         {"OVTI2680"},
1522         {},
1523 };
1524 MODULE_DEVICE_TABLE(acpi, ov2680_acpi_match);
1525
1526
1527 MODULE_DEVICE_TABLE(i2c, ov2680_id);
1528 static struct i2c_driver ov2680_driver = {
1529         .driver = {
1530                 .owner = THIS_MODULE,
1531                 .name = OV2680_NAME,
1532                 .acpi_match_table = ACPI_PTR(ov2680_acpi_match),
1533
1534         },
1535         .probe = ov2680_probe,
1536         .remove = ov2680_remove,
1537         .id_table = ov2680_id,
1538 };
1539
1540 static int init_ov2680(void)
1541 {
1542         return i2c_add_driver(&ov2680_driver);
1543 }
1544
1545 static void exit_ov2680(void)
1546 {
1547
1548         i2c_del_driver(&ov2680_driver);
1549 }
1550
1551 module_init(init_ov2680);
1552 module_exit(exit_ov2680);
1553
1554 MODULE_AUTHOR("Jacky Wang <Jacky_wang@ovt.com>");
1555 MODULE_DESCRIPTION("A low-level driver for OmniVision 2680 sensors");
1556 MODULE_LICENSE("GPL");
1557