1 // SPDX-License-Identifier: GPL-2.0
3 * Support for OmniVision OV2680 1080p HD camera sensor.
5 * Copyright (c) 2013 Intel Corporation. All Rights Reserved.
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.
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.
18 #include <asm/unaligned.h>
20 #include <linux/module.h>
21 #include <linux/types.h>
22 #include <linux/kernel.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>
35 #include <linux/acpi.h>
36 #include "../include/linux/atomisp_gmin_platform.h"
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,
49 /* i2c read/write stuff */
50 static int ov2680_read_reg(struct i2c_client *client,
51 int len, u16 reg, u32 *val)
53 struct i2c_msg msgs[2];
54 u8 addr_buf[2] = { reg >> 8, reg & 0xff };
55 u8 data_buf[4] = { 0, };
61 msgs[0].addr = client->addr;
63 msgs[0].len = ARRAY_SIZE(addr_buf);
64 msgs[0].buf = addr_buf;
66 msgs[1].addr = client->addr;
67 msgs[1].flags = I2C_M_RD;
69 msgs[1].buf = &data_buf[4 - len];
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);
77 *val = get_unaligned_be32(data_buf);
82 static int ov2680_write_reg(struct i2c_client *client, unsigned int len,
89 put_unaligned_be16(val, buf + 2);
95 put_unaligned_be16(reg, buf);
97 ret = i2c_master_send(client, buf, 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);
107 static int ov2680_write_reg_array(struct i2c_client *client,
108 const struct ov2680_reg *reglist)
110 const struct ov2680_reg *next = reglist;
113 for (; next->reg != 0; next++) {
114 ret = ov2680_write_reg(client, 1, next->reg, next->val);
122 static int ov2680_g_focal(struct v4l2_subdev *sd, s32 *val)
124 *val = (OV2680_FOCAL_LENGTH_NUM << 16) | OV2680_FOCAL_LENGTH_DEM;
128 static int ov2680_g_fnumber(struct v4l2_subdev *sd, s32 *val)
130 /* const f number for ov2680 */
132 *val = (OV2680_F_NUMBER_DEFAULT_NUM << 16) | OV2680_F_NUMBER_DEM;
136 static int ov2680_g_fnumber_range(struct v4l2_subdev *sd, s32 *val)
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;
144 static int ov2680_g_bin_factor_x(struct v4l2_subdev *sd, s32 *val)
146 struct ov2680_device *dev = to_ov2680_sensor(sd);
147 struct i2c_client *client = v4l2_get_subdevdata(sd);
149 dev_dbg(&client->dev, "++++ov2680_g_bin_factor_x\n");
150 *val = dev->res->bin_factor_x;
155 static int ov2680_g_bin_factor_y(struct v4l2_subdev *sd, s32 *val)
157 struct ov2680_device *dev = to_ov2680_sensor(sd);
158 struct i2c_client *client = v4l2_get_subdevdata(sd);
160 *val = dev->res->bin_factor_y;
161 dev_dbg(&client->dev, "++++ov2680_g_bin_factor_y\n");
165 static int ov2680_get_intg_factor(struct i2c_client *client,
166 struct camera_mipi_info *info,
167 const struct ov2680_resolution *res)
169 struct atomisp_sensor_mode_data *buf = &info->data;
170 unsigned int pix_clk_freq_hz;
174 dev_dbg(&client->dev, "++++ov2680_get_intg_factor\n");
179 pix_clk_freq_hz = res->pix_clk_freq * 1000000;
181 buf->vt_pix_clk_freq_mhz = pix_clk_freq_hz;
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;
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;
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;
197 /* get the cropping and output resolution to ISP for this mode. */
198 ret = ov2680_read_reg(client, 2,
199 OV2680_HORIZONTAL_START_H, ®_val);
202 buf->crop_horizontal_start = reg_val;
204 ret = ov2680_read_reg(client, 2,
205 OV2680_VERTICAL_START_H, ®_val);
208 buf->crop_vertical_start = reg_val;
210 ret = ov2680_read_reg(client, 2,
211 OV2680_HORIZONTAL_END_H, ®_val);
214 buf->crop_horizontal_end = reg_val;
216 ret = ov2680_read_reg(client, 2,
217 OV2680_VERTICAL_END_H, ®_val);
220 buf->crop_vertical_end = reg_val;
222 ret = ov2680_read_reg(client, 2,
223 OV2680_HORIZONTAL_OUTPUT_SIZE_H, ®_val);
226 buf->output_width = reg_val;
228 ret = ov2680_read_reg(client, 2,
229 OV2680_VERTICAL_OUTPUT_SIZE_H, ®_val);
232 buf->output_height = reg_val;
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;
241 static long __ov2680_set_exposure(struct v4l2_subdev *sd, int coarse_itg,
242 int gain, int digitgain)
245 struct i2c_client *client = v4l2_get_subdevdata(sd);
246 struct ov2680_device *dev = to_ov2680_sensor(sd);
250 dev_dbg(&client->dev,
251 "+++++++__ov2680_set_exposure coarse_itg %d, gain %d, digitgain %d++\n",
252 coarse_itg, gain, digitgain);
254 vts = dev->res->lines_per_frame;
257 ret = ov2680_write_reg(client, 1,
258 OV2680_GROUP_ACCESS, 0x00);
260 dev_err(&client->dev, "%s: write 0x%02x: error, aborted\n",
261 __func__, OV2680_GROUP_ACCESS);
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;
269 ret = ov2680_write_reg(client, 2, OV2680_TIMING_VTS_H, vts);
271 dev_err(&client->dev, "%s: write 0x%02x: error, aborted\n",
272 __func__, OV2680_TIMING_VTS_H);
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);
283 dev_err(&client->dev, "%s: write 0x%02x: error, aborted\n",
284 __func__, OV2680_EXPOSURE_L);
288 ret = ov2680_write_reg(client, 1,
289 OV2680_EXPOSURE_M, (exp_val >> 8) & 0xFF);
291 dev_err(&client->dev, "%s: write 0x%02x: error, aborted\n",
292 __func__, OV2680_EXPOSURE_M);
296 ret = ov2680_write_reg(client, 1,
297 OV2680_EXPOSURE_H, (exp_val >> 16) & 0x0F);
299 dev_err(&client->dev, "%s: write 0x%02x: error, aborted\n",
300 __func__, OV2680_EXPOSURE_H);
305 ret = ov2680_write_reg(client, 2, OV2680_AGC_H, gain);
307 dev_err(&client->dev, "%s: write 0x%02x: error, aborted\n",
308 __func__, OV2680_AGC_H);
313 ret = ov2680_write_reg(client, 2,
314 OV2680_MWB_RED_GAIN_H, digitgain);
316 dev_err(&client->dev,
317 "%s: write 0x%02x: error, aborted\n",
318 __func__, OV2680_MWB_RED_GAIN_H);
322 ret = ov2680_write_reg(client, 2,
323 OV2680_MWB_GREEN_GAIN_H, digitgain);
325 dev_err(&client->dev,
326 "%s: write 0x%02x: error, aborted\n",
327 __func__, OV2680_MWB_RED_GAIN_H);
331 ret = ov2680_write_reg(client, 2,
332 OV2680_MWB_BLUE_GAIN_H, digitgain);
334 dev_err(&client->dev,
335 "%s: write 0x%02x: error, aborted\n",
336 __func__, OV2680_MWB_RED_GAIN_H);
342 ret = ov2680_write_reg(client, 1,
343 OV2680_GROUP_ACCESS, 0x10);
347 /* Delay launch group */
348 ret = ov2680_write_reg(client, 1,
349 OV2680_GROUP_ACCESS, 0xa0);
355 static int ov2680_set_exposure(struct v4l2_subdev *sd, int exposure,
356 int gain, int digitgain)
358 struct ov2680_device *dev = to_ov2680_sensor(sd);
361 mutex_lock(&dev->input_lock);
363 dev->exposure = exposure;
365 dev->digitgain = digitgain;
368 ret = __ov2680_set_exposure(sd, exposure, gain, digitgain);
370 mutex_unlock(&dev->input_lock);
375 static long ov2680_s_exposure(struct v4l2_subdev *sd,
376 struct atomisp_exposure *exposure)
378 u16 coarse_itg = exposure->integration_time[0];
379 u16 analog_gain = exposure->gain[0];
380 u16 digital_gain = exposure->gain[1];
382 /* we should not accept the invalid value below */
383 if (analog_gain == 0) {
384 struct i2c_client *client = v4l2_get_subdevdata(sd);
386 v4l2_err(client, "%s: invalid value\n", __func__);
390 return ov2680_set_exposure(sd, coarse_itg, analog_gain, digital_gain);
393 static long ov2680_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
396 case ATOMISP_IOC_S_EXPOSURE:
397 return ov2680_s_exposure(sd, arg);
406 * This returns the exposure time being used. This should only be used
407 * for filling in EXIF data, not for actual image processing.
409 static int ov2680_q_exposure(struct v4l2_subdev *sd, s32 *value)
411 struct i2c_client *client = v4l2_get_subdevdata(sd);
416 ret = ov2680_read_reg(client, 3, OV2680_EXPOSURE_H, ®_val);
420 /* Lower four bits are not part of the exposure val (always 0) */
421 *value = reg_val >> 4;
425 static int ov2680_v_flip(struct v4l2_subdev *sd, s32 value)
427 struct camera_mipi_info *ov2680_info = NULL;
428 struct i2c_client *client = v4l2_get_subdevdata(sd);
433 dev_dbg(&client->dev, "@%s: value:%d\n", __func__, value);
434 ret = ov2680_read_reg(client, 1, OV2680_FLIP_REG, &val);
438 val |= OV2680_FLIP_MIRROR_BIT_ENABLE;
440 val &= ~OV2680_FLIP_MIRROR_BIT_ENABLE;
442 ret = ov2680_write_reg(client, 1,
443 OV2680_FLIP_REG, val);
446 index = (v_flag > 0 ? OV2680_FLIP_BIT : 0) | (h_flag > 0 ? OV2680_MIRROR_BIT :
448 ov2680_info = v4l2_get_subdev_hostdata(sd);
450 ov2680_info->raw_bayer_order = ov2680_bayer_order_mapping[index];
455 static int ov2680_h_flip(struct v4l2_subdev *sd, s32 value)
457 struct camera_mipi_info *ov2680_info = NULL;
458 struct i2c_client *client = v4l2_get_subdevdata(sd);
463 dev_dbg(&client->dev, "@%s: value:%d\n", __func__, value);
465 ret = ov2680_read_reg(client, 1, OV2680_MIRROR_REG, &val);
469 val |= OV2680_FLIP_MIRROR_BIT_ENABLE;
471 val &= ~OV2680_FLIP_MIRROR_BIT_ENABLE;
473 ret = ov2680_write_reg(client, 1,
474 OV2680_MIRROR_REG, val);
477 index = (v_flag > 0 ? OV2680_FLIP_BIT : 0) | (h_flag > 0 ? OV2680_MIRROR_BIT :
479 ov2680_info = v4l2_get_subdev_hostdata(sd);
481 ov2680_info->raw_bayer_order = ov2680_bayer_order_mapping[index];
486 static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl)
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);
495 dev_dbg(&client->dev, "%s: CID_VFLIP:%d.\n",
496 __func__, ctrl->val);
497 ret = ov2680_v_flip(&dev->sd, ctrl->val);
500 dev_dbg(&client->dev, "%s: CID_HFLIP:%d.\n",
501 __func__, ctrl->val);
502 ret = ov2680_h_flip(&dev->sd, ctrl->val);
510 static int ov2680_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
512 struct ov2680_device *dev =
513 container_of(ctrl->handler, struct ov2680_device, ctrl_handler);
517 case V4L2_CID_EXPOSURE_ABSOLUTE:
518 ret = ov2680_q_exposure(&dev->sd, &ctrl->val);
520 case V4L2_CID_FOCAL_ABSOLUTE:
521 ret = ov2680_g_focal(&dev->sd, &ctrl->val);
523 case V4L2_CID_FNUMBER_ABSOLUTE:
524 ret = ov2680_g_fnumber(&dev->sd, &ctrl->val);
526 case V4L2_CID_FNUMBER_RANGE:
527 ret = ov2680_g_fnumber_range(&dev->sd, &ctrl->val);
529 case V4L2_CID_BIN_FACTOR_HORZ:
530 ret = ov2680_g_bin_factor_x(&dev->sd, &ctrl->val);
532 case V4L2_CID_BIN_FACTOR_VERT:
533 ret = ov2680_g_bin_factor_y(&dev->sd, &ctrl->val);
542 static const struct v4l2_ctrl_ops ctrl_ops = {
543 .s_ctrl = ov2680_s_ctrl,
544 .g_volatile_ctrl = ov2680_g_volatile_ctrl
547 static const struct v4l2_ctrl_config ov2680_controls[] = {
550 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
551 .type = V4L2_CTRL_TYPE_INTEGER,
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,
567 .def = OV2680_FOCAL_LENGTH_DEFAULT,
572 .id = V4L2_CID_FNUMBER_ABSOLUTE,
573 .type = V4L2_CTRL_TYPE_INTEGER,
575 .min = OV2680_F_NUMBER_DEFAULT,
576 .max = OV2680_F_NUMBER_DEFAULT,
578 .def = OV2680_F_NUMBER_DEFAULT,
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,
589 .def = OV2680_F_NUMBER_RANGE,
594 .id = V4L2_CID_BIN_FACTOR_HORZ,
595 .type = V4L2_CTRL_TYPE_INTEGER,
596 .name = "horizontal binning factor",
598 .max = OV2680_BIN_FACTOR_MAX,
605 .id = V4L2_CID_BIN_FACTOR_VERT,
606 .type = V4L2_CTRL_TYPE_INTEGER,
607 .name = "vertical binning factor",
609 .max = OV2680_BIN_FACTOR_MAX,
616 .id = V4L2_CID_VFLIP,
617 .type = V4L2_CTRL_TYPE_BOOLEAN,
626 .id = V4L2_CID_HFLIP,
627 .type = V4L2_CTRL_TYPE_BOOLEAN,
636 static int ov2680_init_registers(struct v4l2_subdev *sd)
638 struct i2c_client *client = v4l2_get_subdevdata(sd);
641 ret = ov2680_write_reg(client, 1, OV2680_SW_RESET, 0x01);
642 ret |= ov2680_write_reg_array(client, ov2680_global_setting);
647 static int power_ctrl(struct v4l2_subdev *sd, bool flag)
650 struct ov2680_device *dev = to_ov2680_sensor(sd);
651 struct i2c_client *client = v4l2_get_subdevdata(sd);
653 if (!dev || !dev->platform_data)
656 dev_dbg(&client->dev, "%s: %s", __func__, flag ? "on" : "off");
659 ret |= dev->platform_data->v1p8_ctrl(sd, 1);
660 ret |= dev->platform_data->v2p8_ctrl(sd, 1);
661 usleep_range(10000, 15000);
665 ret |= dev->platform_data->v1p8_ctrl(sd, 0);
666 ret |= dev->platform_data->v2p8_ctrl(sd, 0);
671 static int gpio_ctrl(struct v4l2_subdev *sd, bool flag)
674 struct ov2680_device *dev = to_ov2680_sensor(sd);
676 if (!dev || !dev->platform_data)
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.
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);
693 dev->platform_data->gpio1_ctrl(sd, 0);
694 ret = dev->platform_data->gpio0_ctrl(sd, 0);
699 static int power_up(struct v4l2_subdev *sd)
701 struct ov2680_device *dev = to_ov2680_sensor(sd);
702 struct i2c_client *client = v4l2_get_subdevdata(sd);
705 if (!dev->platform_data) {
706 dev_err(&client->dev,
707 "no camera_sensor_platform_data");
712 return 0; /* Already on */
715 ret = power_ctrl(sd, 1);
719 /* according to DS, at least 5ms is needed between DOVDD and PWDN */
720 usleep_range(5000, 6000);
723 ret = gpio_ctrl(sd, 1);
725 ret = gpio_ctrl(sd, 1);
730 /* flis clock control */
731 ret = dev->platform_data->flisclk_ctrl(sd, 1);
735 /* according to DS, 20ms is needed between PWDN and i2c access */
738 ret = ov2680_init_registers(sd);
740 goto fail_init_registers;
742 ret = __ov2680_set_exposure(sd, dev->exposure, dev->gain, dev->digitgain);
744 goto fail_init_registers;
746 dev->power_on = true;
750 dev->platform_data->flisclk_ctrl(sd, 0);
755 dev_err(&client->dev, "sensor power-up failed\n");
760 static int power_down(struct v4l2_subdev *sd)
762 struct ov2680_device *dev = to_ov2680_sensor(sd);
763 struct i2c_client *client = v4l2_get_subdevdata(sd);
768 if (!dev->platform_data) {
769 dev_err(&client->dev,
770 "no camera_sensor_platform_data");
775 return 0; /* Already off */
777 ret = dev->platform_data->flisclk_ctrl(sd, 0);
779 dev_err(&client->dev, "flisclk failed\n");
782 ret = gpio_ctrl(sd, 0);
784 ret = gpio_ctrl(sd, 0);
786 dev_err(&client->dev, "gpio failed 2\n");
790 ret = power_ctrl(sd, 0);
792 dev_err(&client->dev, "vprog failed.\n");
796 dev->power_on = false;
800 static int ov2680_s_power(struct v4l2_subdev *sd, int on)
802 struct ov2680_device *dev = to_ov2680_sensor(sd);
805 mutex_lock(&dev->input_lock);
808 ret = power_down(sd);
813 mutex_unlock(&dev->input_lock);
818 static int ov2680_set_fmt(struct v4l2_subdev *sd,
819 struct v4l2_subdev_state *sd_state,
820 struct v4l2_subdev_format *format)
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;
829 dev_dbg(&client->dev, "%s: %s: pad: %d, fmt: %p\n",
831 (format->which == V4L2_SUBDEV_FORMAT_TRY) ? "try" : "set",
840 ov2680_info = v4l2_get_subdev_hostdata(sd);
844 mutex_lock(&dev->input_lock);
846 res = v4l2_find_nearest_size(ov2680_res_preview,
847 ARRAY_SIZE(ov2680_res_preview), width,
848 height, fmt->width, fmt->height);
850 res = &ov2680_res_preview[N_RES_PREVIEW - 1];
852 fmt->width = res->width;
853 fmt->height = res->height;
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);
862 dev_dbg(&client->dev, "%s: %dx%d\n",
863 __func__, fmt->width, fmt->height);
865 /* s_power has not been called yet for std v4l2 clients (camorama) */
867 ret = ov2680_write_reg_array(client, dev->res->regs);
869 dev_err(&client->dev,
870 "ov2680 write resolution register err: %d\n", ret);
872 vts = dev->res->lines_per_frame;
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;
878 ret = ov2680_write_reg(client, 2, OV2680_TIMING_VTS_H, vts);
880 dev_err(&client->dev, "ov2680 write vts err: %d\n", ret);
882 ret = ov2680_get_intg_factor(client, ov2680_info, res);
884 dev_err(&client->dev, "failed to get integration factor\n");
889 * recall flip functions to avoid flip registers
890 * were overridden by default setting
893 ov2680_h_flip(sd, h_flag);
895 ov2680_v_flip(sd, v_flag);
900 * dev_err(&client->dev, "ov2680 startup err\n");
903 mutex_unlock(&dev->input_lock);
907 static int ov2680_get_fmt(struct v4l2_subdev *sd,
908 struct v4l2_subdev_state *sd_state,
909 struct v4l2_subdev_format *format)
911 struct v4l2_mbus_framefmt *fmt = &format->format;
912 struct ov2680_device *dev = to_ov2680_sensor(sd);
920 fmt->width = dev->res->width;
921 fmt->height = dev->res->height;
922 fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
927 static int ov2680_detect(struct i2c_client *client)
929 struct i2c_adapter *adapter = client->adapter;
935 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
938 ret = ov2680_read_reg(client, 1,
939 OV2680_SC_CMMN_CHIP_ID_H, &high);
941 dev_err(&client->dev, "sensor_id_high = 0x%x\n", high);
944 ret = ov2680_read_reg(client, 1,
945 OV2680_SC_CMMN_CHIP_ID_L, &low);
946 id = ((((u16)high) << 8) | (u16)low);
948 if (id != OV2680_ID) {
949 dev_err(&client->dev, "sensor ID error 0x%x\n", id);
953 ret = ov2680_read_reg(client, 1,
954 OV2680_SC_CMMN_SUB_ID, &high);
955 revision = (u8)high & 0x0f;
957 dev_info(&client->dev, "sensor_revision id = 0x%x, rev= %d\n",
963 static int ov2680_s_stream(struct v4l2_subdev *sd, int enable)
965 struct ov2680_device *dev = to_ov2680_sensor(sd);
966 struct i2c_client *client = v4l2_get_subdevdata(sd);
969 mutex_lock(&dev->input_lock);
971 dev_dbg(&client->dev, "ov2680_s_stream one\n");
973 dev_dbg(&client->dev, "ov2680_s_stream off\n");
975 ret = ov2680_write_reg(client, 1, OV2680_SW_STREAM,
976 enable ? OV2680_START_STREAMING :
977 OV2680_STOP_STREAMING);
979 //otp valid at stream on state
981 // dev->otp_data = ov2680_otp_read(sd);
983 mutex_unlock(&dev->input_lock);
988 static int ov2680_s_config(struct v4l2_subdev *sd,
989 int irq, void *platform_data)
991 struct ov2680_device *dev = to_ov2680_sensor(sd);
992 struct i2c_client *client = v4l2_get_subdevdata(sd);
999 (struct camera_sensor_platform_data *)platform_data;
1001 mutex_lock(&dev->input_lock);
1005 dev_err(&client->dev, "ov2680 power-up err.\n");
1009 ret = dev->platform_data->csi_cfg(sd, 1);
1013 /* config & detect sensor */
1014 ret = ov2680_detect(client);
1016 dev_err(&client->dev, "ov2680_detect err s_config.\n");
1020 /* turn off sensor, after probed */
1021 ret = power_down(sd);
1023 dev_err(&client->dev, "ov2680 power-off err.\n");
1026 mutex_unlock(&dev->input_lock);
1031 dev->platform_data->csi_cfg(sd, 0);
1034 dev_err(&client->dev, "sensor power-gating failed\n");
1035 mutex_unlock(&dev->input_lock);
1039 static int ov2680_g_frame_interval(struct v4l2_subdev *sd,
1040 struct v4l2_subdev_frame_interval *interval)
1042 struct ov2680_device *dev = to_ov2680_sensor(sd);
1044 interval->interval.numerator = 1;
1045 interval->interval.denominator = dev->res->fps;
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)
1054 if (code->index >= MAX_FMTS)
1057 code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
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)
1065 int index = fse->index;
1067 if (index >= N_RES_PREVIEW)
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;
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)
1082 struct v4l2_fract fract;
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)
1090 fract.denominator = ov2680_res_preview[fie->index].fps;
1091 fract.numerator = 1;
1093 fie->interval = fract;
1098 static int ov2680_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
1100 struct ov2680_device *dev = to_ov2680_sensor(sd);
1102 mutex_lock(&dev->input_lock);
1103 *frames = dev->res->skip_frames;
1104 mutex_unlock(&dev->input_lock);
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,
1114 static const struct v4l2_subdev_sensor_ops ov2680_sensor_ops = {
1115 .g_skip_frames = ov2680_g_skip_frames,
1118 static const struct v4l2_subdev_core_ops ov2680_core_ops = {
1119 .s_power = ov2680_s_power,
1120 .ioctl = ov2680_ioctl,
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,
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,
1138 static int ov2680_remove(struct i2c_client *client)
1140 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1141 struct ov2680_device *dev = to_ov2680_sensor(sd);
1143 dev_dbg(&client->dev, "ov2680_remove...\n");
1145 dev->platform_data->csi_cfg(sd, 0);
1147 v4l2_device_unregister_subdev(sd);
1148 media_entity_cleanup(&dev->sd.entity);
1149 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1155 static int ov2680_probe(struct i2c_client *client)
1157 struct ov2680_device *dev;
1162 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1166 mutex_init(&dev->input_lock);
1168 dev->res = &ov2680_res_preview[0];
1169 dev->exposure = dev->res->lines_per_frame - OV2680_INTEGRATION_TIME_MARGIN;
1170 dev->gain = 250; /* 0-2047 */
1171 v4l2_i2c_subdev_init(&dev->sd, client, &ov2680_ops);
1173 pdata = gmin_camera_platform_data(&dev->sd,
1174 ATOMISP_INPUT_FORMAT_RAW_10,
1175 atomisp_bayer_order_bggr);
1181 ret = ov2680_s_config(&dev->sd, client->irq, pdata);
1185 ret = atomisp_register_i2c_module(&dev->sd, pdata, RAW_CAMERA);
1189 dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1190 dev->pad.flags = MEDIA_PAD_FL_SOURCE;
1191 dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1193 v4l2_ctrl_handler_init(&dev->ctrl_handler,
1194 ARRAY_SIZE(ov2680_controls));
1196 ov2680_remove(client);
1200 for (i = 0; i < ARRAY_SIZE(ov2680_controls); i++)
1201 v4l2_ctrl_new_custom(&dev->ctrl_handler, &ov2680_controls[i],
1204 if (dev->ctrl_handler.error) {
1205 ov2680_remove(client);
1206 return dev->ctrl_handler.error;
1209 /* Use same lock for controls as for everything else. */
1210 dev->ctrl_handler.lock = &dev->input_lock;
1211 dev->sd.ctrl_handler = &dev->ctrl_handler;
1213 ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad);
1215 ov2680_remove(client);
1216 dev_dbg(&client->dev, "+++ remove ov2680\n");
1220 dev_dbg(&client->dev, "+++ out free\n");
1221 v4l2_device_unregister_subdev(&dev->sd);
1226 static const struct acpi_device_id ov2680_acpi_match[] = {
1231 MODULE_DEVICE_TABLE(acpi, ov2680_acpi_match);
1233 static struct i2c_driver ov2680_driver = {
1236 .acpi_match_table = ov2680_acpi_match,
1238 .probe_new = ov2680_probe,
1239 .remove = ov2680_remove,
1241 module_i2c_driver(ov2680_driver);
1243 MODULE_AUTHOR("Jacky Wang <Jacky_wang@ovt.com>");
1244 MODULE_DESCRIPTION("A low-level driver for OmniVision 2680 sensors");
1245 MODULE_LICENSE("GPL");