2 * Support for OmniVision OV2680 1080p HD camera sensor.
4 * Copyright (c) 2013 Intel Corporation. All Rights Reserved.
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.
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.
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/kernel.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>
33 #include <linux/acpi.h>
34 #include "../include/linux/atomisp_gmin_platform.h"
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,
47 /* i2c read/write stuff */
48 static int ov2680_read_reg(struct i2c_client *client,
49 u16 data_length, u16 reg, u16 *val)
52 struct i2c_msg msg[2];
53 unsigned char data[6];
55 if (!client->adapter) {
56 dev_err(&client->dev, "%s error, no client->adapter\n",
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",
68 memset(msg, 0 , sizeof(msg));
70 msg[0].addr = client->addr;
72 msg[0].len = I2C_MSG_LENGTH;
75 /* high byte goes out first */
76 data[0] = (u8)(reg >> 8);
77 data[1] = (u8)(reg & 0xff);
79 msg[1].addr = client->addr;
80 msg[1].len = data_length;
81 msg[1].flags = I2C_M_RD;
84 err = i2c_transfer(client->adapter, msg, 2);
89 "read from offset 0x%x error %d", reg, err);
94 /* high byte comes first */
95 if (data_length == OV2680_8BIT)
97 else if (data_length == OV2680_16BIT)
98 *val = be16_to_cpu(*(u16 *)&data[0]);
100 *val = be32_to_cpu(*(u32 *)&data[0]);
101 //dev_dbg(&client->dev, "++++i2c read adr%x = %x\n", reg,*val);
105 static int ov2680_i2c_write(struct i2c_client *client, u16 len, u8 *data)
108 const int num_msg = 1;
111 msg.addr = client->addr;
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;
120 static int ov2680_write_reg(struct i2c_client *client, u16 data_length,
124 unsigned char data[4] = {0};
125 u16 *wreg = (u16 *)data;
126 const u16 len = data_length + sizeof(u16); /* 16-bit address + data */
128 if (data_length != OV2680_8BIT && data_length != OV2680_16BIT) {
129 dev_err(&client->dev,
130 "%s error, invalid data_length\n", __func__);
134 /* high byte goes out first */
135 *wreg = cpu_to_be16(reg);
137 if (data_length == OV2680_8BIT) {
141 u16 *wdata = (u16 *)&data[2];
142 *wdata = cpu_to_be16(val);
145 ret = ov2680_i2c_write(client, len, data);
147 dev_err(&client->dev,
148 "write error: wrote 0x%x to offset 0x%x error %d",
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
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().
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.
169 static int __ov2680_flush_reg_array(struct i2c_client *client,
170 struct ov2680_write_ctrl *ctrl)
174 if (ctrl->index == 0)
177 size = sizeof(u16) + ctrl->index; /* 16-bit address + data */
178 ctrl->buffer.addr = cpu_to_be16(ctrl->buffer.addr);
181 return ov2680_i2c_write(client, size, (u8 *)&ctrl->buffer);
184 static int __ov2680_buf_reg_array(struct i2c_client *client,
185 struct ov2680_write_ctrl *ctrl,
186 const struct ov2680_reg *next)
191 switch (next->type) {
194 ctrl->buffer.data[ctrl->index] = (u8)next->val;
198 data16 = (u16 *)&ctrl->buffer.data[ctrl->index];
199 *data16 = cpu_to_be16((u16)next->val);
205 /* When first item is added, we need to store its starting address */
206 if (ctrl->index == 0)
207 ctrl->buffer.addr = next->reg;
212 * Buffer cannot guarantee free space for u32? Better flush it to avoid
213 * possible lack of memory for next item.
215 if (ctrl->index + sizeof(u16) >= OV2680_MAX_WRITE_BUF_SIZE)
216 return __ov2680_flush_reg_array(client, ctrl);
221 static int __ov2680_write_reg_is_consecutive(struct i2c_client *client,
222 struct ov2680_write_ctrl *ctrl,
223 const struct ov2680_reg *next)
225 if (ctrl->index == 0)
228 return ctrl->buffer.addr + ctrl->index == next->reg;
231 static int ov2680_write_reg_array(struct i2c_client *client,
232 const struct ov2680_reg *reglist)
234 const struct ov2680_reg *next = reglist;
235 struct ov2680_write_ctrl ctrl;
237 dev_dbg(&client->dev, "++++write reg array\n");
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);
249 * If next address is not consecutive, data needs to be
250 * flushed before proceed.
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,
255 err = __ov2680_flush_reg_array(client, &ctrl);
259 err = __ov2680_buf_reg_array(client, &ctrl, next);
261 dev_err(&client->dev, "%s: write error, aborted\n",
269 return __ov2680_flush_reg_array(client, &ctrl);
271 static int ov2680_g_focal(struct v4l2_subdev *sd, s32 *val)
274 *val = (OV2680_FOCAL_LENGTH_NUM << 16) | OV2680_FOCAL_LENGTH_DEM;
278 static int ov2680_g_fnumber(struct v4l2_subdev *sd, s32 *val)
280 /*const f number for ov2680*/
282 *val = (OV2680_F_NUMBER_DEFAULT_NUM << 16) | OV2680_F_NUMBER_DEM;
286 static int ov2680_g_fnumber_range(struct v4l2_subdev *sd, s32 *val)
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;
294 static int ov2680_g_bin_factor_x(struct v4l2_subdev *sd, s32 *val)
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;
304 static int ov2680_g_bin_factor_y(struct v4l2_subdev *sd, s32 *val)
306 struct ov2680_device *dev = to_ov2680_sensor(sd);
307 struct i2c_client *client = v4l2_get_subdevdata(sd);
309 *val = ov2680_res[dev->fmt_idx].bin_factor_y;
310 dev_dbg(&client->dev, "++++ov2680_g_bin_factor_y\n");
315 static int ov2680_get_intg_factor(struct i2c_client *client,
316 struct camera_mipi_info *info,
317 const struct ov2680_resolution *res)
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;
325 dev_dbg(&client->dev, "++++ov2680_get_intg_factor\n");
330 pix_clk_freq_hz = res->pix_clk_freq * 1000000;
332 dev->vt_pix_clk_freq_mhz = pix_clk_freq_hz;
333 buf->vt_pix_clk_freq_mhz = pix_clk_freq_hz;
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;
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;
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;
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, ®_val);
354 buf->crop_horizontal_start = reg_val;
356 ret = ov2680_read_reg(client, OV2680_16BIT,
357 OV2680_VERTICAL_START_H, ®_val);
360 buf->crop_vertical_start = reg_val;
362 ret = ov2680_read_reg(client, OV2680_16BIT,
363 OV2680_HORIZONTAL_END_H, ®_val);
366 buf->crop_horizontal_end = reg_val;
368 ret = ov2680_read_reg(client, OV2680_16BIT,
369 OV2680_VERTICAL_END_H, ®_val);
372 buf->crop_vertical_end = reg_val;
374 ret = ov2680_read_reg(client, OV2680_16BIT,
375 OV2680_HORIZONTAL_OUTPUT_SIZE_H, ®_val);
378 buf->output_width = reg_val;
380 ret = ov2680_read_reg(client, OV2680_16BIT,
381 OV2680_VERTICAL_OUTPUT_SIZE_H, ®_val);
384 buf->output_height = reg_val;
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;
393 static long __ov2680_set_exposure(struct v4l2_subdev *sd, int coarse_itg,
394 int gain, int digitgain)
397 struct i2c_client *client = v4l2_get_subdevdata(sd);
398 struct ov2680_device *dev = to_ov2680_sensor(sd);
402 dev_dbg(&client->dev, "+++++++__ov2680_set_exposure coarse_itg %d, gain %d, digitgain %d++\n",coarse_itg, gain, digitgain);
404 vts = ov2680_res[dev->fmt_idx].lines_per_frame;
407 ret = ov2680_write_reg(client, OV2680_8BIT,
408 OV2680_GROUP_ACCESS, 0x00);
410 dev_err(&client->dev, "%s: write %x error, aborted\n",
411 __func__, OV2680_GROUP_ACCESS);
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;
419 ret = ov2680_write_reg(client, OV2680_16BIT, OV2680_TIMING_VTS_H, vts);
421 dev_err(&client->dev, "%s: write %x error, aborted\n",
422 __func__, OV2680_TIMING_VTS_H);
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);
433 dev_err(&client->dev, "%s: write %x error, aborted\n",
434 __func__, OV2680_EXPOSURE_L);
438 ret = ov2680_write_reg(client, OV2680_8BIT,
439 OV2680_EXPOSURE_M, (exp_val >> 8) & 0xFF);
441 dev_err(&client->dev, "%s: write %x error, aborted\n",
442 __func__, OV2680_EXPOSURE_M);
446 ret = ov2680_write_reg(client, OV2680_8BIT,
447 OV2680_EXPOSURE_H, (exp_val >> 16) & 0x0F);
449 dev_err(&client->dev, "%s: write %x error, aborted\n",
450 __func__, OV2680_EXPOSURE_H);
455 ret = ov2680_write_reg(client, OV2680_16BIT, OV2680_AGC_H, gain);
457 dev_err(&client->dev, "%s: write %x error, aborted\n",
458 __func__, OV2680_AGC_H);
463 ret = ov2680_write_reg(client, OV2680_16BIT,
464 OV2680_MWB_RED_GAIN_H, digitgain);
466 dev_err(&client->dev, "%s: write %x error, aborted\n",
467 __func__, OV2680_MWB_RED_GAIN_H);
471 ret = ov2680_write_reg(client, OV2680_16BIT,
472 OV2680_MWB_GREEN_GAIN_H, digitgain);
474 dev_err(&client->dev, "%s: write %x error, aborted\n",
475 __func__, OV2680_MWB_RED_GAIN_H);
479 ret = ov2680_write_reg(client, OV2680_16BIT,
480 OV2680_MWB_BLUE_GAIN_H, digitgain);
482 dev_err(&client->dev, "%s: write %x error, aborted\n",
483 __func__, OV2680_MWB_RED_GAIN_H);
489 ret = ov2680_write_reg(client, OV2680_8BIT,
490 OV2680_GROUP_ACCESS, 0x10);
494 /* Delay launch group */
495 ret = ov2680_write_reg(client, OV2680_8BIT,
496 OV2680_GROUP_ACCESS, 0xa0);
502 static int ov2680_set_exposure(struct v4l2_subdev *sd, int exposure,
503 int gain, int digitgain)
505 struct ov2680_device *dev = to_ov2680_sensor(sd);
508 mutex_lock(&dev->input_lock);
509 ret = __ov2680_set_exposure(sd, exposure, gain, digitgain);
510 mutex_unlock(&dev->input_lock);
515 static long ov2680_s_exposure(struct v4l2_subdev *sd,
516 struct atomisp_exposure *exposure)
518 u16 coarse_itg = exposure->integration_time[0];
519 u16 analog_gain = exposure->gain[0];
520 u16 digital_gain = exposure->gain[1];
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__);
529 // EXPOSURE CONTROL DISABLED FOR INITIAL CHECKIN, TUNING DOESN'T WORK
530 return ov2680_set_exposure(sd, coarse_itg, analog_gain, digital_gain);
537 static long ov2680_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
541 case ATOMISP_IOC_S_EXPOSURE:
542 return ov2680_s_exposure(sd, arg);
550 /* This returns the exposure time being used. This should only be used
551 * for filling in EXIF data, not for actual image processing.
553 static int ov2680_q_exposure(struct v4l2_subdev *sd, s32 *value)
555 struct i2c_client *client = v4l2_get_subdevdata(sd);
560 ret = ov2680_read_reg(client, OV2680_8BIT,
566 ret = ov2680_read_reg(client, OV2680_8BIT,
572 reg_v += reg_v2 << 8;
573 ret = ov2680_read_reg(client, OV2680_8BIT,
579 *value = reg_v + (((u32)reg_v2 << 16));
584 static u32 ov2680_translate_bayer_order(enum atomisp_bayer_order 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;
599 static int ov2680_v_flip(struct v4l2_subdev *sd, s32 value)
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);
607 dev_dbg(&client->dev, "@%s: value:%d\n", __func__, value);
608 ret = ov2680_read_reg(client, OV2680_8BIT, OV2680_FLIP_REG, &val);
612 val |= OV2680_FLIP_MIRROR_BIT_ENABLE;
614 val &= ~OV2680_FLIP_MIRROR_BIT_ENABLE;
616 ret = ov2680_write_reg(client, OV2680_8BIT,
617 OV2680_FLIP_REG, val);
620 index = (v_flag>0?OV2680_FLIP_BIT:0) | (h_flag>0?OV2680_MIRROR_BIT:0);
621 ov2680_info = v4l2_get_subdev_hostdata(sd);
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);
630 static int ov2680_h_flip(struct v4l2_subdev *sd, s32 value)
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);
638 dev_dbg(&client->dev, "@%s: value:%d\n", __func__, value);
640 ret = ov2680_read_reg(client, OV2680_8BIT, OV2680_MIRROR_REG, &val);
644 val |= OV2680_FLIP_MIRROR_BIT_ENABLE;
646 val &= ~OV2680_FLIP_MIRROR_BIT_ENABLE;
648 ret = ov2680_write_reg(client, OV2680_8BIT,
649 OV2680_MIRROR_REG, val);
652 index = (v_flag>0?OV2680_FLIP_BIT:0) | (h_flag>0?OV2680_MIRROR_BIT:0);
653 ov2680_info = v4l2_get_subdev_hostdata(sd);
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);
662 static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl)
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);
671 dev_dbg(&client->dev, "%s: CID_VFLIP:%d.\n",
672 __func__, ctrl->val);
673 ret = ov2680_v_flip(&dev->sd, ctrl->val);
676 dev_dbg(&client->dev, "%s: CID_HFLIP:%d.\n",
677 __func__, ctrl->val);
678 ret = ov2680_h_flip(&dev->sd, ctrl->val);
686 static int ov2680_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
688 struct ov2680_device *dev =
689 container_of(ctrl->handler, struct ov2680_device, ctrl_handler);
693 case V4L2_CID_EXPOSURE_ABSOLUTE:
694 ret = ov2680_q_exposure(&dev->sd, &ctrl->val);
696 case V4L2_CID_FOCAL_ABSOLUTE:
697 ret = ov2680_g_focal(&dev->sd, &ctrl->val);
699 case V4L2_CID_FNUMBER_ABSOLUTE:
700 ret = ov2680_g_fnumber(&dev->sd, &ctrl->val);
702 case V4L2_CID_FNUMBER_RANGE:
703 ret = ov2680_g_fnumber_range(&dev->sd, &ctrl->val);
705 case V4L2_CID_BIN_FACTOR_HORZ:
706 ret = ov2680_g_bin_factor_x(&dev->sd, &ctrl->val);
708 case V4L2_CID_BIN_FACTOR_VERT:
709 ret = ov2680_g_bin_factor_y(&dev->sd, &ctrl->val);
718 static const struct v4l2_ctrl_ops ctrl_ops = {
719 .s_ctrl = ov2680_s_ctrl,
720 .g_volatile_ctrl = ov2680_g_volatile_ctrl
723 struct v4l2_ctrl_config ov2680_controls[] = {
726 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
727 .type = V4L2_CTRL_TYPE_INTEGER,
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,
743 .def = OV2680_FOCAL_LENGTH_DEFAULT,
748 .id = V4L2_CID_FNUMBER_ABSOLUTE,
749 .type = V4L2_CTRL_TYPE_INTEGER,
751 .min = OV2680_F_NUMBER_DEFAULT,
752 .max = OV2680_F_NUMBER_DEFAULT,
754 .def = OV2680_F_NUMBER_DEFAULT,
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,
765 .def = OV2680_F_NUMBER_RANGE,
770 .id = V4L2_CID_BIN_FACTOR_HORZ,
771 .type = V4L2_CTRL_TYPE_INTEGER,
772 .name = "horizontal binning factor",
774 .max = OV2680_BIN_FACTOR_MAX,
781 .id = V4L2_CID_BIN_FACTOR_VERT,
782 .type = V4L2_CTRL_TYPE_INTEGER,
783 .name = "vertical binning factor",
785 .max = OV2680_BIN_FACTOR_MAX,
792 .id = V4L2_CID_VFLIP,
793 .type = V4L2_CTRL_TYPE_BOOLEAN,
802 .id = V4L2_CID_HFLIP,
803 .type = V4L2_CTRL_TYPE_BOOLEAN,
812 static int ov2680_init_registers(struct v4l2_subdev *sd)
814 struct i2c_client *client = v4l2_get_subdevdata(sd);
817 ret = ov2680_write_reg(client, OV2680_8BIT, OV2680_SW_RESET, 0x01);
818 ret |= ov2680_write_reg_array(client, ov2680_global_setting);
823 static int ov2680_init(struct v4l2_subdev *sd)
825 struct ov2680_device *dev = to_ov2680_sensor(sd);
829 mutex_lock(&dev->input_lock);
831 /* restore settings */
832 ov2680_res = ov2680_res_preview;
833 N_RES = N_RES_PREVIEW;
835 ret = ov2680_init_registers(sd);
837 mutex_unlock(&dev->input_lock);
842 static int power_ctrl(struct v4l2_subdev *sd, bool flag)
845 struct ov2680_device *dev = to_ov2680_sensor(sd);
846 if (!dev || !dev->platform_data)
849 /* Non-gmin platforms use the legacy callback */
850 if (dev->platform_data->power_ctrl)
851 return dev->platform_data->power_ctrl(sd, flag);
854 ret |= dev->platform_data->v1p8_ctrl(sd, 1);
855 ret |= dev->platform_data->v2p8_ctrl(sd, 1);
856 usleep_range(10000, 15000);
860 ret |= dev->platform_data->v1p8_ctrl(sd, 0);
861 ret |= dev->platform_data->v2p8_ctrl(sd, 0);
866 static int gpio_ctrl(struct v4l2_subdev *sd, bool flag)
869 struct ov2680_device *dev = to_ov2680_sensor(sd);
871 if (!dev || !dev->platform_data)
874 /* Non-gmin platforms use the legacy callback */
875 if (dev->platform_data->gpio_ctrl)
876 return dev->platform_data->gpio_ctrl(sd, flag);
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. */
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);
890 dev->platform_data->gpio1_ctrl(sd, 0);
891 ret = dev->platform_data->gpio0_ctrl(sd, 0);
896 static int power_up(struct v4l2_subdev *sd)
898 struct ov2680_device *dev = to_ov2680_sensor(sd);
899 struct i2c_client *client = v4l2_get_subdevdata(sd);
902 if (!dev->platform_data) {
903 dev_err(&client->dev,
904 "no camera_sensor_platform_data");
909 ret = power_ctrl(sd, 1);
913 /* according to DS, at least 5ms is needed between DOVDD and PWDN */
914 usleep_range(5000, 6000);
917 ret = gpio_ctrl(sd, 1);
919 ret = gpio_ctrl(sd, 1);
924 /* flis clock control */
925 ret = dev->platform_data->flisclk_ctrl(sd, 1);
929 /* according to DS, 20ms is needed between PWDN and i2c access */
938 dev_err(&client->dev, "sensor power-up failed\n");
943 static int power_down(struct v4l2_subdev *sd)
945 struct ov2680_device *dev = to_ov2680_sensor(sd);
946 struct i2c_client *client = v4l2_get_subdevdata(sd);
951 if (!dev->platform_data) {
952 dev_err(&client->dev,
953 "no camera_sensor_platform_data");
957 ret = dev->platform_data->flisclk_ctrl(sd, 0);
959 dev_err(&client->dev, "flisclk failed\n");
962 ret = gpio_ctrl(sd, 0);
964 ret = gpio_ctrl(sd, 0);
966 dev_err(&client->dev, "gpio failed 2\n");
970 ret = power_ctrl(sd, 0);
972 dev_err(&client->dev, "vprog failed.\n");
977 static int ov2680_s_power(struct v4l2_subdev *sd, int on)
982 ret = power_down(sd);
986 return ov2680_init(sd);
992 * distance - calculate the distance
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.
1001 #define LARGEST_ALLOWED_RATIO_MISMATCH 600
1002 static int distance(struct ov2680_resolution *res, u32 w, u32 h)
1004 unsigned int w_ratio = (res->width << 13) / w;
1005 unsigned int h_ratio;
1010 h_ratio = (res->height << 13) / h;
1013 match = abs(((w_ratio << 13) / h_ratio) - ((int)8192));
1016 if ((w_ratio < (int)8192) || (h_ratio < (int)8192) ||
1017 (match > LARGEST_ALLOWED_RATIO_MISMATCH))
1020 return w_ratio + h_ratio;
1023 /* Return the nearest higher resolution index */
1024 static int nearest_resolution_index(int w, int h)
1029 int min_dist = INT_MAX;
1030 struct ov2680_resolution *tmp_res = NULL;
1032 for (i = 0; i < N_RES; i++) {
1033 tmp_res = &ov2680_res[i];
1034 dist = distance(tmp_res, w, h);
1037 if (dist < min_dist) {
1046 static int get_resolution_index(int w, int h)
1050 for (i = 0; i < N_RES; i++) {
1051 if (w != ov2680_res[i].width)
1053 if (h != ov2680_res[i].height)
1062 static int ov2680_set_fmt(struct v4l2_subdev *sd,
1063 struct v4l2_subdev_pad_config *cfg,
1064 struct v4l2_subdev_format *format)
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;
1072 dev_dbg(&client->dev, "+++++ov2680_s_mbus_fmt+++++l\n");
1079 ov2680_info = v4l2_get_subdev_hostdata(sd);
1083 mutex_lock(&dev->input_lock);
1084 idx = nearest_resolution_index(fmt->width, fmt->height);
1086 /* return the largest resolution */
1087 fmt->width = ov2680_res[N_RES - 1].width;
1088 fmt->height = ov2680_res[N_RES - 1].height;
1090 fmt->width = ov2680_res[idx].width;
1091 fmt->height = ov2680_res[idx].height;
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);
1099 dev->fmt_idx = get_resolution_index(fmt->width, fmt->height);
1100 dev_dbg(&client->dev, "+++++get_resolution_index=%d+++++l\n",
1102 if (dev->fmt_idx == -1) {
1103 dev_err(&client->dev, "get resolution fail\n");
1104 mutex_unlock(&dev->input_lock);
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);
1112 ret = ov2680_write_reg_array(client, ov2680_res[dev->fmt_idx].regs);
1114 dev_err(&client->dev, "ov2680 write resolution register err\n");
1116 ret = ov2680_get_intg_factor(client, ov2680_info,
1117 &ov2680_res[dev->fmt_idx]);
1119 dev_err(&client->dev, "failed to get integration_factor\n");
1123 /*recall flip functions to avoid flip registers
1124 * were overridden by default setting
1127 ov2680_h_flip(sd, h_flag);
1129 ov2680_v_flip(sd, v_flag);
1131 v4l2_info(client, "\n%s idx %d \n", __func__, dev->fmt_idx);
1133 /*ret = startup(sd);
1135 * dev_err(&client->dev, "ov2680 startup err\n");
1138 mutex_unlock(&dev->input_lock);
1142 static int ov2680_get_fmt(struct v4l2_subdev *sd,
1143 struct v4l2_subdev_pad_config *cfg,
1144 struct v4l2_subdev_format *format)
1146 struct v4l2_mbus_framefmt *fmt = &format->format;
1147 struct ov2680_device *dev = to_ov2680_sensor(sd);
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;
1162 static int ov2680_detect(struct i2c_client *client)
1164 struct i2c_adapter *adapter = client->adapter;
1170 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
1173 ret = ov2680_read_reg(client, OV2680_8BIT,
1174 OV2680_SC_CMMN_CHIP_ID_H, &high);
1176 dev_err(&client->dev, "sensor_id_high = 0x%x\n", high);
1179 ret = ov2680_read_reg(client, OV2680_8BIT,
1180 OV2680_SC_CMMN_CHIP_ID_L, &low);
1181 id = ((((u16) high) << 8) | (u16) low);
1183 if (id != OV2680_ID) {
1184 dev_err(&client->dev, "sensor ID error 0x%x\n", id);
1188 ret = ov2680_read_reg(client, OV2680_8BIT,
1189 OV2680_SC_CMMN_SUB_ID, &high);
1190 revision = (u8) high & 0x0f;
1192 dev_info(&client->dev, "sensor_revision id = 0x%x, rev= %d\n",
1198 static int ov2680_s_stream(struct v4l2_subdev *sd, int enable)
1200 struct ov2680_device *dev = to_ov2680_sensor(sd);
1201 struct i2c_client *client = v4l2_get_subdevdata(sd);
1204 mutex_lock(&dev->input_lock);
1206 dev_dbg(&client->dev, "ov2680_s_stream one \n");
1208 dev_dbg(&client->dev, "ov2680_s_stream off \n");
1210 ret = ov2680_write_reg(client, OV2680_8BIT, OV2680_SW_STREAM,
1211 enable ? OV2680_START_STREAMING :
1212 OV2680_STOP_STREAMING);
1214 /* restore settings */
1215 ov2680_res = ov2680_res_preview;
1216 N_RES = N_RES_PREVIEW;
1219 //otp valid at stream on state
1220 //if(!dev->otp_data)
1221 // dev->otp_data = ov2680_otp_read(sd);
1223 mutex_unlock(&dev->input_lock);
1229 static int ov2680_s_config(struct v4l2_subdev *sd,
1230 int irq, void *platform_data)
1232 struct ov2680_device *dev = to_ov2680_sensor(sd);
1233 struct i2c_client *client = v4l2_get_subdevdata(sd);
1239 dev->platform_data =
1240 (struct camera_sensor_platform_data *)platform_data;
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
1247 ret = power_down(sd);
1249 dev_err(&client->dev, "ov2680 power-off err.\n");
1250 goto fail_power_off;
1255 dev_err(&client->dev, "ov2680 power-up err.\n");
1259 ret = dev->platform_data->csi_cfg(sd, 1);
1263 /* config & detect sensor */
1264 ret = ov2680_detect(client);
1266 dev_err(&client->dev, "ov2680_detect err s_config.\n");
1270 /* turn off sensor, after probed */
1271 ret = power_down(sd);
1273 dev_err(&client->dev, "ov2680 power-off err.\n");
1276 mutex_unlock(&dev->input_lock);
1281 dev->platform_data->csi_cfg(sd, 0);
1284 dev_err(&client->dev, "sensor power-gating failed\n");
1286 mutex_unlock(&dev->input_lock);
1290 static int ov2680_g_parm(struct v4l2_subdev *sd,
1291 struct v4l2_streamparm *param)
1293 struct ov2680_device *dev = to_ov2680_sensor(sd);
1294 struct i2c_client *client = v4l2_get_subdevdata(sd);
1299 if (param->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1300 dev_err(&client->dev, "unsupported buffer type.\n");
1304 memset(param, 0, sizeof(*param));
1305 param->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
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;
1317 static int ov2680_s_parm(struct v4l2_subdev *sd,
1318 struct v4l2_streamparm *param)
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;
1324 v4l2_info(client, "\n%s:run_mode :%x\n", __func__, dev->run_mode);
1326 mutex_lock(&dev->input_lock);
1327 switch (dev->run_mode) {
1329 ov2680_res = ov2680_res_video;
1330 N_RES = N_RES_VIDEO;
1332 case CI_MODE_STILL_CAPTURE:
1333 ov2680_res = ov2680_res_still;
1334 N_RES = N_RES_STILL;
1337 ov2680_res = ov2680_res_preview;
1338 N_RES = N_RES_PREVIEW;
1340 mutex_unlock(&dev->input_lock);
1344 static int ov2680_g_frame_interval(struct v4l2_subdev *sd,
1345 struct v4l2_subdev_frame_interval *interval)
1347 struct ov2680_device *dev = to_ov2680_sensor(sd);
1349 interval->interval.numerator = 1;
1350 interval->interval.denominator = ov2680_res[dev->fmt_idx].fps;
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)
1359 if (code->index >= MAX_FMTS)
1362 code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
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)
1370 int index = fse->index;
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;
1384 static int ov2680_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
1386 struct ov2680_device *dev = to_ov2680_sensor(sd);
1388 mutex_lock(&dev->input_lock);
1389 *frames = ov2680_res[dev->fmt_idx].skip_frames;
1390 mutex_unlock(&dev->input_lock);
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,
1402 static const struct v4l2_subdev_sensor_ops ov2680_sensor_ops = {
1403 .g_skip_frames = ov2680_g_skip_frames,
1406 static const struct v4l2_subdev_core_ops ov2680_core_ops = {
1407 .s_power = ov2680_s_power,
1408 .ioctl = ov2680_ioctl,
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,
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,
1425 static int ov2680_remove(struct i2c_client *client)
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");
1431 dev->platform_data->csi_cfg(sd, 0);
1433 v4l2_device_unregister_subdev(sd);
1434 media_entity_cleanup(&dev->sd.entity);
1435 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1441 static int ov2680_probe(struct i2c_client *client,
1442 const struct i2c_device_id *id)
1444 struct ov2680_device *dev;
1449 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1451 dev_err(&client->dev, "out of memory\n");
1455 mutex_init(&dev->input_lock);
1458 v4l2_i2c_subdev_init(&(dev->sd), client, &ov2680_ops);
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);
1465 pdata = client->dev.platform_data;
1472 ret = ov2680_s_config(&dev->sd, client->irq, pdata);
1476 ret = atomisp_register_i2c_module(&dev->sd, pdata, RAW_CAMERA);
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;
1485 v4l2_ctrl_handler_init(&dev->ctrl_handler,
1486 ARRAY_SIZE(ov2680_controls));
1488 ov2680_remove(client);
1492 for (i = 0; i < ARRAY_SIZE(ov2680_controls); i++)
1493 v4l2_ctrl_new_custom(&dev->ctrl_handler, &ov2680_controls[i],
1496 if (dev->ctrl_handler.error) {
1497 ov2680_remove(client);
1498 return dev->ctrl_handler.error;
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;
1505 ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad);
1508 ov2680_remove(client);
1509 dev_dbg(&client->dev, "+++ remove ov2680 \n");
1513 dev_dbg(&client->dev, "+++ out free \n");
1514 v4l2_device_unregister_subdev(&dev->sd);
1519 static const struct acpi_device_id ov2680_acpi_match[] = {
1524 MODULE_DEVICE_TABLE(acpi, ov2680_acpi_match);
1527 MODULE_DEVICE_TABLE(i2c, ov2680_id);
1528 static struct i2c_driver ov2680_driver = {
1530 .owner = THIS_MODULE,
1531 .name = OV2680_NAME,
1532 .acpi_match_table = ACPI_PTR(ov2680_acpi_match),
1535 .probe = ov2680_probe,
1536 .remove = ov2680_remove,
1537 .id_table = ov2680_id,
1540 static int init_ov2680(void)
1542 return i2c_add_driver(&ov2680_driver);
1545 static void exit_ov2680(void)
1548 i2c_del_driver(&ov2680_driver);
1551 module_init(init_ov2680);
1552 module_exit(exit_ov2680);
1554 MODULE_AUTHOR("Jacky Wang <Jacky_wang@ovt.com>");
1555 MODULE_DESCRIPTION("A low-level driver for OmniVision 2680 sensors");
1556 MODULE_LICENSE("GPL");