Merge drm/drm-fixes into drm-misc-fixes
[platform/kernel/linux-rpi.git] / drivers / staging / media / atomisp / i2c / atomisp-ov2680.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Support for OmniVision OV2680 1080p HD camera sensor.
4  *
5  * Copyright (c) 2013 Intel Corporation. All Rights Reserved.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License version
9  * 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  */
17
18 #include <asm/unaligned.h>
19
20 #include <linux/module.h>
21 #include <linux/types.h>
22 #include <linux/kernel.h>
23 #include <linux/mm.h>
24 #include <linux/string.h>
25 #include <linux/errno.h>
26 #include <linux/init.h>
27 #include <linux/kmod.h>
28 #include <linux/device.h>
29 #include <linux/delay.h>
30 #include <linux/slab.h>
31 #include <linux/i2c.h>
32 #include <linux/moduleparam.h>
33 #include <media/v4l2-device.h>
34 #include <linux/io.h>
35 #include <linux/acpi.h>
36 #include "../include/linux/atomisp_gmin_platform.h"
37
38 #include "ov2680.h"
39
40 static int h_flag;
41 static int v_flag;
42 static enum atomisp_bayer_order ov2680_bayer_order_mapping[] = {
43         atomisp_bayer_order_bggr,
44         atomisp_bayer_order_grbg,
45         atomisp_bayer_order_gbrg,
46         atomisp_bayer_order_rggb,
47 };
48
49 /* i2c read/write stuff */
50 static int ov2680_read_reg(struct i2c_client *client,
51                            int len, u16 reg, u32 *val)
52 {
53         struct i2c_msg msgs[2];
54         u8 addr_buf[2] = { reg >> 8, reg & 0xff };
55         u8 data_buf[4] = { 0, };
56         int ret;
57
58         if (len > 4)
59                 return -EINVAL;
60
61         msgs[0].addr = client->addr;
62         msgs[0].flags = 0;
63         msgs[0].len = ARRAY_SIZE(addr_buf);
64         msgs[0].buf = addr_buf;
65
66         msgs[1].addr = client->addr;
67         msgs[1].flags = I2C_M_RD;
68         msgs[1].len = len;
69         msgs[1].buf = &data_buf[4 - len];
70
71         ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
72         if (ret != ARRAY_SIZE(msgs)) {
73                 dev_err(&client->dev, "read error: reg=0x%4x: %d\n", reg, ret);
74                 return -EIO;
75         }
76
77         *val = get_unaligned_be32(data_buf);
78
79         return 0;
80 }
81
82 static int ov2680_write_reg(struct i2c_client *client, unsigned int len,
83                             u16 reg, u16 val)
84 {
85         u8 buf[6];
86         int ret;
87
88         if (len == 2)
89                 put_unaligned_be16(val, buf + 2);
90         else if (len == 1)
91                 buf[2] = val;
92         else
93                 return -EINVAL;
94
95         put_unaligned_be16(reg, buf);
96
97         ret = i2c_master_send(client, buf, len + 2);
98         if (ret != len + 2) {
99                 dev_err(&client->dev, "write error %d reg 0x%04x, val 0x%02x: buf sent: %*ph\n",
100                         ret, reg, val, len + 2, &buf);
101                 return -EIO;
102         }
103
104         return 0;
105 }
106
107 static int ov2680_write_reg_array(struct i2c_client *client,
108                                   const struct ov2680_reg *reglist)
109 {
110         const struct ov2680_reg *next = reglist;
111         int ret;
112
113         for (; next->reg != 0; next++) {
114                 ret = ov2680_write_reg(client, 1, next->reg, next->val);
115                 if (ret)
116                         return ret;
117         }
118
119         return 0;
120 }
121
122 static int ov2680_g_focal(struct v4l2_subdev *sd, s32 *val)
123 {
124         *val = (OV2680_FOCAL_LENGTH_NUM << 16) | OV2680_FOCAL_LENGTH_DEM;
125         return 0;
126 }
127
128 static int ov2680_g_fnumber(struct v4l2_subdev *sd, s32 *val)
129 {
130         /* const f number for ov2680 */
131
132         *val = (OV2680_F_NUMBER_DEFAULT_NUM << 16) | OV2680_F_NUMBER_DEM;
133         return 0;
134 }
135
136 static int ov2680_g_fnumber_range(struct v4l2_subdev *sd, s32 *val)
137 {
138         *val = (OV2680_F_NUMBER_DEFAULT_NUM << 24) |
139                (OV2680_F_NUMBER_DEM << 16) |
140                (OV2680_F_NUMBER_DEFAULT_NUM << 8) | OV2680_F_NUMBER_DEM;
141         return 0;
142 }
143
144 static int ov2680_g_bin_factor_x(struct v4l2_subdev *sd, s32 *val)
145 {
146         struct ov2680_device *dev = to_ov2680_sensor(sd);
147         struct i2c_client *client = v4l2_get_subdevdata(sd);
148
149         dev_dbg(&client->dev,  "++++ov2680_g_bin_factor_x\n");
150         *val = dev->res->bin_factor_x;
151
152         return 0;
153 }
154
155 static int ov2680_g_bin_factor_y(struct v4l2_subdev *sd, s32 *val)
156 {
157         struct ov2680_device *dev = to_ov2680_sensor(sd);
158         struct i2c_client *client = v4l2_get_subdevdata(sd);
159
160         *val = dev->res->bin_factor_y;
161         dev_dbg(&client->dev,  "++++ov2680_g_bin_factor_y\n");
162         return 0;
163 }
164
165 static int ov2680_get_intg_factor(struct i2c_client *client,
166                                   struct camera_mipi_info *info,
167                                   const struct ov2680_resolution *res)
168 {
169         struct atomisp_sensor_mode_data *buf = &info->data;
170         unsigned int pix_clk_freq_hz;
171         u32 reg_val;
172         int ret;
173
174         dev_dbg(&client->dev,  "++++ov2680_get_intg_factor\n");
175         if (!info)
176                 return -EINVAL;
177
178         /* pixel clock */
179         pix_clk_freq_hz = res->pix_clk_freq * 1000000;
180
181         buf->vt_pix_clk_freq_mhz = pix_clk_freq_hz;
182
183         /* get integration time */
184         buf->coarse_integration_time_min = OV2680_COARSE_INTG_TIME_MIN;
185         buf->coarse_integration_time_max_margin =
186             OV2680_COARSE_INTG_TIME_MAX_MARGIN;
187
188         buf->fine_integration_time_min = OV2680_FINE_INTG_TIME_MIN;
189         buf->fine_integration_time_max_margin =
190             OV2680_FINE_INTG_TIME_MAX_MARGIN;
191
192         buf->fine_integration_time_def = OV2680_FINE_INTG_TIME_MIN;
193         buf->frame_length_lines = res->lines_per_frame;
194         buf->line_length_pck = res->pixels_per_line;
195         buf->read_mode = res->bin_mode;
196
197         /* get the cropping and output resolution to ISP for this mode. */
198         ret =  ov2680_read_reg(client, 2,
199                                OV2680_HORIZONTAL_START_H, &reg_val);
200         if (ret)
201                 return ret;
202         buf->crop_horizontal_start = reg_val;
203
204         ret =  ov2680_read_reg(client, 2,
205                                OV2680_VERTICAL_START_H, &reg_val);
206         if (ret)
207                 return ret;
208         buf->crop_vertical_start = reg_val;
209
210         ret = ov2680_read_reg(client, 2,
211                               OV2680_HORIZONTAL_END_H, &reg_val);
212         if (ret)
213                 return ret;
214         buf->crop_horizontal_end = reg_val;
215
216         ret = ov2680_read_reg(client, 2,
217                               OV2680_VERTICAL_END_H, &reg_val);
218         if (ret)
219                 return ret;
220         buf->crop_vertical_end = reg_val;
221
222         ret = ov2680_read_reg(client, 2,
223                               OV2680_HORIZONTAL_OUTPUT_SIZE_H, &reg_val);
224         if (ret)
225                 return ret;
226         buf->output_width = reg_val;
227
228         ret = ov2680_read_reg(client, 2,
229                               OV2680_VERTICAL_OUTPUT_SIZE_H, &reg_val);
230         if (ret)
231                 return ret;
232         buf->output_height = reg_val;
233
234         buf->binning_factor_x = res->bin_factor_x ?
235                                 (res->bin_factor_x * 2) : 1;
236         buf->binning_factor_y = res->bin_factor_y ?
237                                 (res->bin_factor_y * 2) : 1;
238         return 0;
239 }
240
241 static long __ov2680_set_exposure(struct v4l2_subdev *sd, int coarse_itg,
242                                   int gain, int digitgain)
243
244 {
245         struct i2c_client *client = v4l2_get_subdevdata(sd);
246         struct ov2680_device *dev = to_ov2680_sensor(sd);
247         u16 vts;
248         int ret, exp_val;
249
250         dev_dbg(&client->dev,
251                 "+++++++__ov2680_set_exposure coarse_itg %d, gain %d, digitgain %d++\n",
252                 coarse_itg, gain, digitgain);
253
254         vts = dev->res->lines_per_frame;
255
256         /* group hold */
257         ret = ov2680_write_reg(client, 1,
258                                OV2680_GROUP_ACCESS, 0x00);
259         if (ret) {
260                 dev_err(&client->dev, "%s: write 0x%02x: error, aborted\n",
261                         __func__, OV2680_GROUP_ACCESS);
262                 return ret;
263         }
264
265         /* Increase the VTS to match exposure + MARGIN */
266         if (coarse_itg > vts - OV2680_INTEGRATION_TIME_MARGIN)
267                 vts = (u16)coarse_itg + OV2680_INTEGRATION_TIME_MARGIN;
268
269         ret = ov2680_write_reg(client, 2, OV2680_TIMING_VTS_H, vts);
270         if (ret) {
271                 dev_err(&client->dev, "%s: write 0x%02x: error, aborted\n",
272                         __func__, OV2680_TIMING_VTS_H);
273                 return ret;
274         }
275
276         /* set exposure */
277
278         /* Lower four bit should be 0*/
279         exp_val = coarse_itg << 4;
280         ret = ov2680_write_reg(client, 1,
281                                OV2680_EXPOSURE_L, exp_val & 0xFF);
282         if (ret) {
283                 dev_err(&client->dev, "%s: write 0x%02x: error, aborted\n",
284                         __func__, OV2680_EXPOSURE_L);
285                 return ret;
286         }
287
288         ret = ov2680_write_reg(client, 1,
289                                OV2680_EXPOSURE_M, (exp_val >> 8) & 0xFF);
290         if (ret) {
291                 dev_err(&client->dev, "%s: write 0x%02x: error, aborted\n",
292                         __func__, OV2680_EXPOSURE_M);
293                 return ret;
294         }
295
296         ret = ov2680_write_reg(client, 1,
297                                OV2680_EXPOSURE_H, (exp_val >> 16) & 0x0F);
298         if (ret) {
299                 dev_err(&client->dev, "%s: write 0x%02x: error, aborted\n",
300                         __func__, OV2680_EXPOSURE_H);
301                 return ret;
302         }
303
304         /* Analog gain */
305         ret = ov2680_write_reg(client, 2, OV2680_AGC_H, gain);
306         if (ret) {
307                 dev_err(&client->dev, "%s: write 0x%02x: error, aborted\n",
308                         __func__, OV2680_AGC_H);
309                 return ret;
310         }
311         /* Digital gain */
312         if (digitgain) {
313                 ret = ov2680_write_reg(client, 2,
314                                        OV2680_MWB_RED_GAIN_H, digitgain);
315                 if (ret) {
316                         dev_err(&client->dev,
317                                 "%s: write 0x%02x: error, aborted\n",
318                                 __func__, OV2680_MWB_RED_GAIN_H);
319                         return ret;
320                 }
321
322                 ret = ov2680_write_reg(client, 2,
323                                        OV2680_MWB_GREEN_GAIN_H, digitgain);
324                 if (ret) {
325                         dev_err(&client->dev,
326                                 "%s: write 0x%02x: error, aborted\n",
327                                 __func__, OV2680_MWB_RED_GAIN_H);
328                         return ret;
329                 }
330
331                 ret = ov2680_write_reg(client, 2,
332                                        OV2680_MWB_BLUE_GAIN_H, digitgain);
333                 if (ret) {
334                         dev_err(&client->dev,
335                                 "%s: write 0x%02x: error, aborted\n",
336                                 __func__, OV2680_MWB_RED_GAIN_H);
337                         return ret;
338                 }
339         }
340
341         /* End group */
342         ret = ov2680_write_reg(client, 1,
343                                OV2680_GROUP_ACCESS, 0x10);
344         if (ret)
345                 return ret;
346
347         /* Delay launch group */
348         ret = ov2680_write_reg(client, 1,
349                                OV2680_GROUP_ACCESS, 0xa0);
350         if (ret)
351                 return ret;
352         return ret;
353 }
354
355 static int ov2680_set_exposure(struct v4l2_subdev *sd, int exposure,
356                                int gain, int digitgain)
357 {
358         struct ov2680_device *dev = to_ov2680_sensor(sd);
359         int ret = 0;
360
361         mutex_lock(&dev->input_lock);
362
363         dev->exposure = exposure;
364         dev->gain = gain;
365         dev->digitgain = digitgain;
366
367         if (dev->power_on)
368                 ret = __ov2680_set_exposure(sd, exposure, gain, digitgain);
369
370         mutex_unlock(&dev->input_lock);
371
372         return ret;
373 }
374
375 static long ov2680_s_exposure(struct v4l2_subdev *sd,
376                               struct atomisp_exposure *exposure)
377 {
378         u16 coarse_itg = exposure->integration_time[0];
379         u16 analog_gain = exposure->gain[0];
380         u16 digital_gain = exposure->gain[1];
381
382         /* we should not accept the invalid value below */
383         if (analog_gain == 0) {
384                 struct i2c_client *client = v4l2_get_subdevdata(sd);
385
386                 v4l2_err(client, "%s: invalid value\n", __func__);
387                 return -EINVAL;
388         }
389
390         return ov2680_set_exposure(sd, coarse_itg, analog_gain, digital_gain);
391 }
392
393 static long ov2680_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
394 {
395         switch (cmd) {
396         case ATOMISP_IOC_S_EXPOSURE:
397                 return ov2680_s_exposure(sd, arg);
398
399         default:
400                 return -EINVAL;
401         }
402         return 0;
403 }
404
405 /*
406  * This returns the exposure time being used. This should only be used
407  * for filling in EXIF data, not for actual image processing.
408  */
409 static int ov2680_q_exposure(struct v4l2_subdev *sd, s32 *value)
410 {
411         struct i2c_client *client = v4l2_get_subdevdata(sd);
412         u32 reg_val;
413         int ret;
414
415         /* get exposure */
416         ret = ov2680_read_reg(client, 3, OV2680_EXPOSURE_H, &reg_val);
417         if (ret)
418                 return ret;
419
420         /* Lower four bits are not part of the exposure val (always 0) */
421         *value = reg_val >> 4;
422         return 0;
423 }
424
425 static int ov2680_v_flip(struct v4l2_subdev *sd, s32 value)
426 {
427         struct camera_mipi_info *ov2680_info = NULL;
428         struct i2c_client *client = v4l2_get_subdevdata(sd);
429         int ret;
430         u32 val;
431         u8 index;
432
433         dev_dbg(&client->dev, "@%s: value:%d\n", __func__, value);
434         ret = ov2680_read_reg(client, 1, OV2680_FLIP_REG, &val);
435         if (ret)
436                 return ret;
437         if (value)
438                 val |= OV2680_FLIP_MIRROR_BIT_ENABLE;
439         else
440                 val &= ~OV2680_FLIP_MIRROR_BIT_ENABLE;
441
442         ret = ov2680_write_reg(client, 1,
443                                OV2680_FLIP_REG, val);
444         if (ret)
445                 return ret;
446         index = (v_flag > 0 ? OV2680_FLIP_BIT : 0) | (h_flag > 0 ? OV2680_MIRROR_BIT :
447                 0);
448         ov2680_info = v4l2_get_subdev_hostdata(sd);
449         if (ov2680_info) {
450                 ov2680_info->raw_bayer_order = ov2680_bayer_order_mapping[index];
451         }
452         return ret;
453 }
454
455 static int ov2680_h_flip(struct v4l2_subdev *sd, s32 value)
456 {
457         struct camera_mipi_info *ov2680_info = NULL;
458         struct i2c_client *client = v4l2_get_subdevdata(sd);
459         int ret;
460         u32 val;
461         u8 index;
462
463         dev_dbg(&client->dev, "@%s: value:%d\n", __func__, value);
464
465         ret = ov2680_read_reg(client, 1, OV2680_MIRROR_REG, &val);
466         if (ret)
467                 return ret;
468         if (value)
469                 val |= OV2680_FLIP_MIRROR_BIT_ENABLE;
470         else
471                 val &= ~OV2680_FLIP_MIRROR_BIT_ENABLE;
472
473         ret = ov2680_write_reg(client, 1,
474                                OV2680_MIRROR_REG, val);
475         if (ret)
476                 return ret;
477         index = (v_flag > 0 ? OV2680_FLIP_BIT : 0) | (h_flag > 0 ? OV2680_MIRROR_BIT :
478                 0);
479         ov2680_info = v4l2_get_subdev_hostdata(sd);
480         if (ov2680_info) {
481                 ov2680_info->raw_bayer_order = ov2680_bayer_order_mapping[index];
482         }
483         return ret;
484 }
485
486 static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl)
487 {
488         struct ov2680_device *dev =
489             container_of(ctrl->handler, struct ov2680_device, ctrl_handler);
490         struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
491         int ret = 0;
492
493         switch (ctrl->id) {
494         case V4L2_CID_VFLIP:
495                 dev_dbg(&client->dev, "%s: CID_VFLIP:%d.\n",
496                         __func__, ctrl->val);
497                 ret = ov2680_v_flip(&dev->sd, ctrl->val);
498                 break;
499         case V4L2_CID_HFLIP:
500                 dev_dbg(&client->dev, "%s: CID_HFLIP:%d.\n",
501                         __func__, ctrl->val);
502                 ret = ov2680_h_flip(&dev->sd, ctrl->val);
503                 break;
504         default:
505                 ret = -EINVAL;
506         }
507         return ret;
508 }
509
510 static int ov2680_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
511 {
512         struct ov2680_device *dev =
513             container_of(ctrl->handler, struct ov2680_device, ctrl_handler);
514         int ret = 0;
515
516         switch (ctrl->id) {
517         case V4L2_CID_EXPOSURE_ABSOLUTE:
518                 ret = ov2680_q_exposure(&dev->sd, &ctrl->val);
519                 break;
520         case V4L2_CID_FOCAL_ABSOLUTE:
521                 ret = ov2680_g_focal(&dev->sd, &ctrl->val);
522                 break;
523         case V4L2_CID_FNUMBER_ABSOLUTE:
524                 ret = ov2680_g_fnumber(&dev->sd, &ctrl->val);
525                 break;
526         case V4L2_CID_FNUMBER_RANGE:
527                 ret = ov2680_g_fnumber_range(&dev->sd, &ctrl->val);
528                 break;
529         case V4L2_CID_BIN_FACTOR_HORZ:
530                 ret = ov2680_g_bin_factor_x(&dev->sd, &ctrl->val);
531                 break;
532         case V4L2_CID_BIN_FACTOR_VERT:
533                 ret = ov2680_g_bin_factor_y(&dev->sd, &ctrl->val);
534                 break;
535         default:
536                 ret = -EINVAL;
537         }
538
539         return ret;
540 }
541
542 static const struct v4l2_ctrl_ops ctrl_ops = {
543         .s_ctrl = ov2680_s_ctrl,
544         .g_volatile_ctrl = ov2680_g_volatile_ctrl
545 };
546
547 static const struct v4l2_ctrl_config ov2680_controls[] = {
548         {
549                 .ops = &ctrl_ops,
550                 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
551                 .type = V4L2_CTRL_TYPE_INTEGER,
552                 .name = "exposure",
553                 .min = 0x0,
554                 .max = 0xffff,
555                 .step = 0x01,
556                 .def = 0x00,
557                 .flags = 0,
558         },
559         {
560                 .ops = &ctrl_ops,
561                 .id = V4L2_CID_FOCAL_ABSOLUTE,
562                 .type = V4L2_CTRL_TYPE_INTEGER,
563                 .name = "focal length",
564                 .min = OV2680_FOCAL_LENGTH_DEFAULT,
565                 .max = OV2680_FOCAL_LENGTH_DEFAULT,
566                 .step = 0x01,
567                 .def = OV2680_FOCAL_LENGTH_DEFAULT,
568                 .flags = 0,
569         },
570         {
571                 .ops = &ctrl_ops,
572                 .id = V4L2_CID_FNUMBER_ABSOLUTE,
573                 .type = V4L2_CTRL_TYPE_INTEGER,
574                 .name = "f-number",
575                 .min = OV2680_F_NUMBER_DEFAULT,
576                 .max = OV2680_F_NUMBER_DEFAULT,
577                 .step = 0x01,
578                 .def = OV2680_F_NUMBER_DEFAULT,
579                 .flags = 0,
580         },
581         {
582                 .ops = &ctrl_ops,
583                 .id = V4L2_CID_FNUMBER_RANGE,
584                 .type = V4L2_CTRL_TYPE_INTEGER,
585                 .name = "f-number range",
586                 .min = OV2680_F_NUMBER_RANGE,
587                 .max = OV2680_F_NUMBER_RANGE,
588                 .step = 0x01,
589                 .def = OV2680_F_NUMBER_RANGE,
590                 .flags = 0,
591         },
592         {
593                 .ops = &ctrl_ops,
594                 .id = V4L2_CID_BIN_FACTOR_HORZ,
595                 .type = V4L2_CTRL_TYPE_INTEGER,
596                 .name = "horizontal binning factor",
597                 .min = 0,
598                 .max = OV2680_BIN_FACTOR_MAX,
599                 .step = 1,
600                 .def = 0,
601                 .flags = 0,
602         },
603         {
604                 .ops = &ctrl_ops,
605                 .id = V4L2_CID_BIN_FACTOR_VERT,
606                 .type = V4L2_CTRL_TYPE_INTEGER,
607                 .name = "vertical binning factor",
608                 .min = 0,
609                 .max = OV2680_BIN_FACTOR_MAX,
610                 .step = 1,
611                 .def = 0,
612                 .flags = 0,
613         },
614         {
615                 .ops = &ctrl_ops,
616                 .id = V4L2_CID_VFLIP,
617                 .type = V4L2_CTRL_TYPE_BOOLEAN,
618                 .name = "Flip",
619                 .min = 0,
620                 .max = 1,
621                 .step = 1,
622                 .def = 0,
623         },
624         {
625                 .ops = &ctrl_ops,
626                 .id = V4L2_CID_HFLIP,
627                 .type = V4L2_CTRL_TYPE_BOOLEAN,
628                 .name = "Mirror",
629                 .min = 0,
630                 .max = 1,
631                 .step = 1,
632                 .def = 0,
633         },
634 };
635
636 static int ov2680_init_registers(struct v4l2_subdev *sd)
637 {
638         struct i2c_client *client = v4l2_get_subdevdata(sd);
639         int ret;
640
641         ret = ov2680_write_reg(client, 1, OV2680_SW_RESET, 0x01);
642         ret |= ov2680_write_reg_array(client, ov2680_global_setting);
643
644         return ret;
645 }
646
647 static int power_ctrl(struct v4l2_subdev *sd, bool flag)
648 {
649         int ret = 0;
650         struct ov2680_device *dev = to_ov2680_sensor(sd);
651         struct i2c_client *client = v4l2_get_subdevdata(sd);
652
653         if (!dev || !dev->platform_data)
654                 return -ENODEV;
655
656         dev_dbg(&client->dev, "%s: %s", __func__, flag ? "on" : "off");
657
658         if (flag) {
659                 ret |= dev->platform_data->v1p8_ctrl(sd, 1);
660                 ret |= dev->platform_data->v2p8_ctrl(sd, 1);
661                 usleep_range(10000, 15000);
662         }
663
664         if (!flag || ret) {
665                 ret |= dev->platform_data->v1p8_ctrl(sd, 0);
666                 ret |= dev->platform_data->v2p8_ctrl(sd, 0);
667         }
668         return ret;
669 }
670
671 static int gpio_ctrl(struct v4l2_subdev *sd, bool flag)
672 {
673         int ret;
674         struct ov2680_device *dev = to_ov2680_sensor(sd);
675
676         if (!dev || !dev->platform_data)
677                 return -ENODEV;
678
679         /*
680          * The OV2680 documents only one GPIO input (#XSHUTDN), but
681          * existing integrations often wire two (reset/power_down)
682          * because that is the way other sensors work.  There is no
683          * way to tell how it is wired internally, so existing
684          * firmwares expose both and we drive them symmetrically.
685          */
686         if (flag) {
687                 ret = dev->platform_data->gpio0_ctrl(sd, 1);
688                 usleep_range(10000, 15000);
689                 /* Ignore return from second gpio, it may not be there */
690                 dev->platform_data->gpio1_ctrl(sd, 1);
691                 usleep_range(10000, 15000);
692         } else {
693                 dev->platform_data->gpio1_ctrl(sd, 0);
694                 ret = dev->platform_data->gpio0_ctrl(sd, 0);
695         }
696         return ret;
697 }
698
699 static int power_up(struct v4l2_subdev *sd)
700 {
701         struct ov2680_device *dev = to_ov2680_sensor(sd);
702         struct i2c_client *client = v4l2_get_subdevdata(sd);
703         int ret;
704
705         if (!dev->platform_data) {
706                 dev_err(&client->dev,
707                         "no camera_sensor_platform_data");
708                 return -ENODEV;
709         }
710
711         if (dev->power_on)
712                 return 0; /* Already on */
713
714         /* power control */
715         ret = power_ctrl(sd, 1);
716         if (ret)
717                 goto fail_power;
718
719         /* according to DS, at least 5ms is needed between DOVDD and PWDN */
720         usleep_range(5000, 6000);
721
722         /* gpio ctrl */
723         ret = gpio_ctrl(sd, 1);
724         if (ret) {
725                 ret = gpio_ctrl(sd, 1);
726                 if (ret)
727                         goto fail_power;
728         }
729
730         /* flis clock control */
731         ret = dev->platform_data->flisclk_ctrl(sd, 1);
732         if (ret)
733                 goto fail_clk;
734
735         /* according to DS, 20ms is needed between PWDN and i2c access */
736         msleep(20);
737
738         ret = ov2680_init_registers(sd);
739         if (ret)
740                 goto fail_init_registers;
741
742         ret = __ov2680_set_exposure(sd, dev->exposure, dev->gain, dev->digitgain);
743         if (ret)
744                 goto fail_init_registers;
745
746         dev->power_on = true;
747         return 0;
748
749 fail_init_registers:
750         dev->platform_data->flisclk_ctrl(sd, 0);
751 fail_clk:
752         gpio_ctrl(sd, 0);
753 fail_power:
754         power_ctrl(sd, 0);
755         dev_err(&client->dev, "sensor power-up failed\n");
756
757         return ret;
758 }
759
760 static int power_down(struct v4l2_subdev *sd)
761 {
762         struct ov2680_device *dev = to_ov2680_sensor(sd);
763         struct i2c_client *client = v4l2_get_subdevdata(sd);
764         int ret = 0;
765
766         h_flag = 0;
767         v_flag = 0;
768         if (!dev->platform_data) {
769                 dev_err(&client->dev,
770                         "no camera_sensor_platform_data");
771                 return -ENODEV;
772         }
773
774         if (!dev->power_on)
775                 return 0; /* Already off */
776
777         ret = dev->platform_data->flisclk_ctrl(sd, 0);
778         if (ret)
779                 dev_err(&client->dev, "flisclk failed\n");
780
781         /* gpio ctrl */
782         ret = gpio_ctrl(sd, 0);
783         if (ret) {
784                 ret = gpio_ctrl(sd, 0);
785                 if (ret)
786                         dev_err(&client->dev, "gpio failed 2\n");
787         }
788
789         /* power control */
790         ret = power_ctrl(sd, 0);
791         if (ret) {
792                 dev_err(&client->dev, "vprog failed.\n");
793                 return ret;
794         }
795
796         dev->power_on = false;
797         return 0;
798 }
799
800 static int ov2680_s_power(struct v4l2_subdev *sd, int on)
801 {
802         struct ov2680_device *dev = to_ov2680_sensor(sd);
803         int ret;
804
805         mutex_lock(&dev->input_lock);
806
807         if (on == 0) {
808                 ret = power_down(sd);
809         } else {
810                 ret = power_up(sd);
811         }
812
813         mutex_unlock(&dev->input_lock);
814
815         return ret;
816 }
817
818 static int ov2680_set_fmt(struct v4l2_subdev *sd,
819                           struct v4l2_subdev_state *sd_state,
820                           struct v4l2_subdev_format *format)
821 {
822         struct v4l2_mbus_framefmt *fmt = &format->format;
823         struct ov2680_device *dev = to_ov2680_sensor(sd);
824         struct i2c_client *client = v4l2_get_subdevdata(sd);
825         struct camera_mipi_info *ov2680_info = NULL;
826         struct ov2680_resolution *res;
827         int vts, ret = 0;
828
829         dev_dbg(&client->dev, "%s: %s: pad: %d, fmt: %p\n",
830                 __func__,
831                 (format->which == V4L2_SUBDEV_FORMAT_TRY) ? "try" : "set",
832                 format->pad, fmt);
833
834         if (format->pad)
835                 return -EINVAL;
836
837         if (!fmt)
838                 return -EINVAL;
839
840         ov2680_info = v4l2_get_subdev_hostdata(sd);
841         if (!ov2680_info)
842                 return -EINVAL;
843
844         mutex_lock(&dev->input_lock);
845
846         res = v4l2_find_nearest_size(ov2680_res_preview,
847                                      ARRAY_SIZE(ov2680_res_preview), width,
848                                      height, fmt->width, fmt->height);
849         if (!res)
850                 res = &ov2680_res_preview[N_RES_PREVIEW - 1];
851
852         fmt->width = res->width;
853         fmt->height = res->height;
854
855         fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
856         if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
857                 sd_state->pads->try_fmt = *fmt;
858                 mutex_unlock(&dev->input_lock);
859                 return 0;
860         }
861
862         dev_dbg(&client->dev, "%s: %dx%d\n",
863                 __func__, fmt->width, fmt->height);
864
865         /* s_power has not been called yet for std v4l2 clients (camorama) */
866         power_up(sd);
867         ret = ov2680_write_reg_array(client, dev->res->regs);
868         if (ret)
869                 dev_err(&client->dev,
870                         "ov2680 write resolution register err: %d\n", ret);
871
872         vts = dev->res->lines_per_frame;
873
874         /* If necessary increase the VTS to match exposure + MARGIN */
875         if (dev->exposure > vts - OV2680_INTEGRATION_TIME_MARGIN)
876                 vts = dev->exposure + OV2680_INTEGRATION_TIME_MARGIN;
877
878         ret = ov2680_write_reg(client, 2, OV2680_TIMING_VTS_H, vts);
879         if (ret)
880                 dev_err(&client->dev, "ov2680 write vts err: %d\n", ret);
881
882         ret = ov2680_get_intg_factor(client, ov2680_info, res);
883         if (ret) {
884                 dev_err(&client->dev, "failed to get integration factor\n");
885                 goto err;
886         }
887
888         /*
889          * recall flip functions to avoid flip registers
890          * were overridden by default setting
891          */
892         if (h_flag)
893                 ov2680_h_flip(sd, h_flag);
894         if (v_flag)
895                 ov2680_v_flip(sd, v_flag);
896
897         /*
898          * ret = startup(sd);
899          * if (ret)
900          * dev_err(&client->dev, "ov2680 startup err\n");
901          */
902 err:
903         mutex_unlock(&dev->input_lock);
904         return ret;
905 }
906
907 static int ov2680_get_fmt(struct v4l2_subdev *sd,
908                           struct v4l2_subdev_state *sd_state,
909                           struct v4l2_subdev_format *format)
910 {
911         struct v4l2_mbus_framefmt *fmt = &format->format;
912         struct ov2680_device *dev = to_ov2680_sensor(sd);
913
914         if (format->pad)
915                 return -EINVAL;
916
917         if (!fmt)
918                 return -EINVAL;
919
920         fmt->width = dev->res->width;
921         fmt->height = dev->res->height;
922         fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
923
924         return 0;
925 }
926
927 static int ov2680_detect(struct i2c_client *client)
928 {
929         struct i2c_adapter *adapter = client->adapter;
930         u32 high, low;
931         int ret;
932         u16 id;
933         u8 revision;
934
935         if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
936                 return -ENODEV;
937
938         ret = ov2680_read_reg(client, 1,
939                               OV2680_SC_CMMN_CHIP_ID_H, &high);
940         if (ret) {
941                 dev_err(&client->dev, "sensor_id_high = 0x%x\n", high);
942                 return -ENODEV;
943         }
944         ret = ov2680_read_reg(client, 1,
945                               OV2680_SC_CMMN_CHIP_ID_L, &low);
946         id = ((((u16)high) << 8) | (u16)low);
947
948         if (id != OV2680_ID) {
949                 dev_err(&client->dev, "sensor ID error 0x%x\n", id);
950                 return -ENODEV;
951         }
952
953         ret = ov2680_read_reg(client, 1,
954                               OV2680_SC_CMMN_SUB_ID, &high);
955         revision = (u8)high & 0x0f;
956
957         dev_info(&client->dev, "sensor_revision id = 0x%x, rev= %d\n",
958                  id, revision);
959
960         return 0;
961 }
962
963 static int ov2680_s_stream(struct v4l2_subdev *sd, int enable)
964 {
965         struct ov2680_device *dev = to_ov2680_sensor(sd);
966         struct i2c_client *client = v4l2_get_subdevdata(sd);
967         int ret;
968
969         mutex_lock(&dev->input_lock);
970         if (enable)
971                 dev_dbg(&client->dev, "ov2680_s_stream one\n");
972         else
973                 dev_dbg(&client->dev, "ov2680_s_stream off\n");
974
975         ret = ov2680_write_reg(client, 1, OV2680_SW_STREAM,
976                                enable ? OV2680_START_STREAMING :
977                                OV2680_STOP_STREAMING);
978
979         //otp valid at stream on state
980         //if(!dev->otp_data)
981         //      dev->otp_data = ov2680_otp_read(sd);
982
983         mutex_unlock(&dev->input_lock);
984
985         return ret;
986 }
987
988 static int ov2680_s_config(struct v4l2_subdev *sd,
989                            int irq, void *platform_data)
990 {
991         struct ov2680_device *dev = to_ov2680_sensor(sd);
992         struct i2c_client *client = v4l2_get_subdevdata(sd);
993         int ret = 0;
994
995         if (!platform_data)
996                 return -ENODEV;
997
998         dev->platform_data =
999             (struct camera_sensor_platform_data *)platform_data;
1000
1001         mutex_lock(&dev->input_lock);
1002
1003         ret = power_up(sd);
1004         if (ret) {
1005                 dev_err(&client->dev, "ov2680 power-up err.\n");
1006                 goto fail_power_on;
1007         }
1008
1009         ret = dev->platform_data->csi_cfg(sd, 1);
1010         if (ret)
1011                 goto fail_csi_cfg;
1012
1013         /* config & detect sensor */
1014         ret = ov2680_detect(client);
1015         if (ret) {
1016                 dev_err(&client->dev, "ov2680_detect err s_config.\n");
1017                 goto fail_csi_cfg;
1018         }
1019
1020         /* turn off sensor, after probed */
1021         ret = power_down(sd);
1022         if (ret) {
1023                 dev_err(&client->dev, "ov2680 power-off err.\n");
1024                 goto fail_csi_cfg;
1025         }
1026         mutex_unlock(&dev->input_lock);
1027
1028         return 0;
1029
1030 fail_csi_cfg:
1031         dev->platform_data->csi_cfg(sd, 0);
1032 fail_power_on:
1033         power_down(sd);
1034         dev_err(&client->dev, "sensor power-gating failed\n");
1035         mutex_unlock(&dev->input_lock);
1036         return ret;
1037 }
1038
1039 static int ov2680_g_frame_interval(struct v4l2_subdev *sd,
1040                                    struct v4l2_subdev_frame_interval *interval)
1041 {
1042         struct ov2680_device *dev = to_ov2680_sensor(sd);
1043
1044         interval->interval.numerator = 1;
1045         interval->interval.denominator = dev->res->fps;
1046
1047         return 0;
1048 }
1049
1050 static int ov2680_enum_mbus_code(struct v4l2_subdev *sd,
1051                                  struct v4l2_subdev_state *sd_state,
1052                                  struct v4l2_subdev_mbus_code_enum *code)
1053 {
1054         if (code->index >= MAX_FMTS)
1055                 return -EINVAL;
1056
1057         code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
1058         return 0;
1059 }
1060
1061 static int ov2680_enum_frame_size(struct v4l2_subdev *sd,
1062                                   struct v4l2_subdev_state *sd_state,
1063                                   struct v4l2_subdev_frame_size_enum *fse)
1064 {
1065         int index = fse->index;
1066
1067         if (index >= N_RES_PREVIEW)
1068                 return -EINVAL;
1069
1070         fse->min_width = ov2680_res_preview[index].width;
1071         fse->min_height = ov2680_res_preview[index].height;
1072         fse->max_width = ov2680_res_preview[index].width;
1073         fse->max_height = ov2680_res_preview[index].height;
1074
1075         return 0;
1076 }
1077
1078 static int ov2680_enum_frame_interval(struct v4l2_subdev *sd,
1079                                       struct v4l2_subdev_state *sd_state,
1080                                       struct v4l2_subdev_frame_interval_enum *fie)
1081 {
1082         struct v4l2_fract fract;
1083
1084         if (fie->index >= N_RES_PREVIEW ||
1085             fie->width > ov2680_res_preview[0].width ||
1086             fie->height > ov2680_res_preview[0].height ||
1087             fie->which > V4L2_SUBDEV_FORMAT_ACTIVE)
1088                 return -EINVAL;
1089
1090         fract.denominator = ov2680_res_preview[fie->index].fps;
1091         fract.numerator = 1;
1092
1093         fie->interval = fract;
1094
1095         return 0;
1096 }
1097
1098 static int ov2680_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
1099 {
1100         struct ov2680_device *dev = to_ov2680_sensor(sd);
1101
1102         mutex_lock(&dev->input_lock);
1103         *frames = dev->res->skip_frames;
1104         mutex_unlock(&dev->input_lock);
1105
1106         return 0;
1107 }
1108
1109 static const struct v4l2_subdev_video_ops ov2680_video_ops = {
1110         .s_stream = ov2680_s_stream,
1111         .g_frame_interval = ov2680_g_frame_interval,
1112 };
1113
1114 static const struct v4l2_subdev_sensor_ops ov2680_sensor_ops = {
1115         .g_skip_frames  = ov2680_g_skip_frames,
1116 };
1117
1118 static const struct v4l2_subdev_core_ops ov2680_core_ops = {
1119         .s_power = ov2680_s_power,
1120         .ioctl = ov2680_ioctl,
1121 };
1122
1123 static const struct v4l2_subdev_pad_ops ov2680_pad_ops = {
1124         .enum_mbus_code = ov2680_enum_mbus_code,
1125         .enum_frame_size = ov2680_enum_frame_size,
1126         .enum_frame_interval = ov2680_enum_frame_interval,
1127         .get_fmt = ov2680_get_fmt,
1128         .set_fmt = ov2680_set_fmt,
1129 };
1130
1131 static const struct v4l2_subdev_ops ov2680_ops = {
1132         .core = &ov2680_core_ops,
1133         .video = &ov2680_video_ops,
1134         .pad = &ov2680_pad_ops,
1135         .sensor = &ov2680_sensor_ops,
1136 };
1137
1138 static void ov2680_remove(struct i2c_client *client)
1139 {
1140         struct v4l2_subdev *sd = i2c_get_clientdata(client);
1141         struct ov2680_device *dev = to_ov2680_sensor(sd);
1142
1143         dev_dbg(&client->dev, "ov2680_remove...\n");
1144
1145         dev->platform_data->csi_cfg(sd, 0);
1146
1147         v4l2_device_unregister_subdev(sd);
1148         media_entity_cleanup(&dev->sd.entity);
1149         v4l2_ctrl_handler_free(&dev->ctrl_handler);
1150         kfree(dev);
1151 }
1152
1153 static int ov2680_probe(struct i2c_client *client)
1154 {
1155         struct ov2680_device *dev;
1156         int ret;
1157         void *pdata;
1158         unsigned int i;
1159
1160         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1161         if (!dev)
1162                 return -ENOMEM;
1163
1164         mutex_init(&dev->input_lock);
1165
1166         dev->res = &ov2680_res_preview[0];
1167         dev->exposure = dev->res->lines_per_frame - OV2680_INTEGRATION_TIME_MARGIN;
1168         dev->gain = 250; /* 0-2047 */
1169         v4l2_i2c_subdev_init(&dev->sd, client, &ov2680_ops);
1170
1171         pdata = gmin_camera_platform_data(&dev->sd,
1172                                           ATOMISP_INPUT_FORMAT_RAW_10,
1173                                           atomisp_bayer_order_bggr);
1174         if (!pdata) {
1175                 ret = -EINVAL;
1176                 goto out_free;
1177         }
1178
1179         ret = ov2680_s_config(&dev->sd, client->irq, pdata);
1180         if (ret)
1181                 goto out_free;
1182
1183         ret = atomisp_register_i2c_module(&dev->sd, pdata, RAW_CAMERA);
1184         if (ret)
1185                 goto out_free;
1186
1187         dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1188         dev->pad.flags = MEDIA_PAD_FL_SOURCE;
1189         dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1190         ret =
1191             v4l2_ctrl_handler_init(&dev->ctrl_handler,
1192                                    ARRAY_SIZE(ov2680_controls));
1193         if (ret) {
1194                 ov2680_remove(client);
1195                 return ret;
1196         }
1197
1198         for (i = 0; i < ARRAY_SIZE(ov2680_controls); i++)
1199                 v4l2_ctrl_new_custom(&dev->ctrl_handler, &ov2680_controls[i],
1200                                      NULL);
1201
1202         if (dev->ctrl_handler.error) {
1203                 ov2680_remove(client);
1204                 return dev->ctrl_handler.error;
1205         }
1206
1207         /* Use same lock for controls as for everything else. */
1208         dev->ctrl_handler.lock = &dev->input_lock;
1209         dev->sd.ctrl_handler = &dev->ctrl_handler;
1210
1211         ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad);
1212         if (ret) {
1213                 ov2680_remove(client);
1214                 dev_dbg(&client->dev, "+++ remove ov2680\n");
1215         }
1216         return ret;
1217 out_free:
1218         dev_dbg(&client->dev, "+++ out free\n");
1219         v4l2_device_unregister_subdev(&dev->sd);
1220         kfree(dev);
1221         return ret;
1222 }
1223
1224 static const struct acpi_device_id ov2680_acpi_match[] = {
1225         {"XXOV2680"},
1226         {"OVTI2680"},
1227         {},
1228 };
1229 MODULE_DEVICE_TABLE(acpi, ov2680_acpi_match);
1230
1231 static struct i2c_driver ov2680_driver = {
1232         .driver = {
1233                 .name = "ov2680",
1234                 .acpi_match_table = ov2680_acpi_match,
1235         },
1236         .probe_new = ov2680_probe,
1237         .remove = ov2680_remove,
1238 };
1239 module_i2c_driver(ov2680_driver);
1240
1241 MODULE_AUTHOR("Jacky Wang <Jacky_wang@ovt.com>");
1242 MODULE_DESCRIPTION("A low-level driver for OmniVision 2680 sensors");
1243 MODULE_LICENSE("GPL");