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, u16 *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 << (8 * (4 - len)), 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 = ov2680_res[dev->fmt_idx].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 = ov2680_res[dev->fmt_idx].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 v4l2_subdev *sd = i2c_get_clientdata(client);
170 struct ov2680_device *dev = to_ov2680_sensor(sd);
171 struct atomisp_sensor_mode_data *buf = &info->data;
172 unsigned int pix_clk_freq_hz;
176 dev_dbg(&client->dev, "++++ov2680_get_intg_factor\n");
181 pix_clk_freq_hz = res->pix_clk_freq * 1000000;
183 dev->vt_pix_clk_freq_mhz = pix_clk_freq_hz;
184 buf->vt_pix_clk_freq_mhz = pix_clk_freq_hz;
186 /* get integration time */
187 buf->coarse_integration_time_min = OV2680_COARSE_INTG_TIME_MIN;
188 buf->coarse_integration_time_max_margin =
189 OV2680_COARSE_INTG_TIME_MAX_MARGIN;
191 buf->fine_integration_time_min = OV2680_FINE_INTG_TIME_MIN;
192 buf->fine_integration_time_max_margin =
193 OV2680_FINE_INTG_TIME_MAX_MARGIN;
195 buf->fine_integration_time_def = OV2680_FINE_INTG_TIME_MIN;
196 buf->frame_length_lines = res->lines_per_frame;
197 buf->line_length_pck = res->pixels_per_line;
198 buf->read_mode = res->bin_mode;
200 /* get the cropping and output resolution to ISP for this mode. */
201 ret = ov2680_read_reg(client, 2,
202 OV2680_HORIZONTAL_START_H, ®_val);
205 buf->crop_horizontal_start = reg_val;
207 ret = ov2680_read_reg(client, 2,
208 OV2680_VERTICAL_START_H, ®_val);
211 buf->crop_vertical_start = reg_val;
213 ret = ov2680_read_reg(client, 2,
214 OV2680_HORIZONTAL_END_H, ®_val);
217 buf->crop_horizontal_end = reg_val;
219 ret = ov2680_read_reg(client, 2,
220 OV2680_VERTICAL_END_H, ®_val);
223 buf->crop_vertical_end = reg_val;
225 ret = ov2680_read_reg(client, 2,
226 OV2680_HORIZONTAL_OUTPUT_SIZE_H, ®_val);
229 buf->output_width = reg_val;
231 ret = ov2680_read_reg(client, 2,
232 OV2680_VERTICAL_OUTPUT_SIZE_H, ®_val);
235 buf->output_height = reg_val;
237 buf->binning_factor_x = res->bin_factor_x ?
238 (res->bin_factor_x * 2) : 1;
239 buf->binning_factor_y = res->bin_factor_y ?
240 (res->bin_factor_y * 2) : 1;
244 static long __ov2680_set_exposure(struct v4l2_subdev *sd, int coarse_itg,
245 int gain, int digitgain)
248 struct i2c_client *client = v4l2_get_subdevdata(sd);
249 struct ov2680_device *dev = to_ov2680_sensor(sd);
253 dev_dbg(&client->dev,
254 "+++++++__ov2680_set_exposure coarse_itg %d, gain %d, digitgain %d++\n",
255 coarse_itg, gain, digitgain);
257 vts = ov2680_res[dev->fmt_idx].lines_per_frame;
260 ret = ov2680_write_reg(client, 1,
261 OV2680_GROUP_ACCESS, 0x00);
263 dev_err(&client->dev, "%s: write 0x%02x: error, aborted\n",
264 __func__, OV2680_GROUP_ACCESS);
268 /* Increase the VTS to match exposure + MARGIN */
269 if (coarse_itg > vts - OV2680_INTEGRATION_TIME_MARGIN)
270 vts = (u16)coarse_itg + OV2680_INTEGRATION_TIME_MARGIN;
272 ret = ov2680_write_reg(client, 2, OV2680_TIMING_VTS_H, vts);
274 dev_err(&client->dev, "%s: write 0x%02x: error, aborted\n",
275 __func__, OV2680_TIMING_VTS_H);
281 /* Lower four bit should be 0*/
282 exp_val = coarse_itg << 4;
283 ret = ov2680_write_reg(client, 1,
284 OV2680_EXPOSURE_L, exp_val & 0xFF);
286 dev_err(&client->dev, "%s: write 0x%02x: error, aborted\n",
287 __func__, OV2680_EXPOSURE_L);
291 ret = ov2680_write_reg(client, 1,
292 OV2680_EXPOSURE_M, (exp_val >> 8) & 0xFF);
294 dev_err(&client->dev, "%s: write 0x%02x: error, aborted\n",
295 __func__, OV2680_EXPOSURE_M);
299 ret = ov2680_write_reg(client, 1,
300 OV2680_EXPOSURE_H, (exp_val >> 16) & 0x0F);
302 dev_err(&client->dev, "%s: write 0x%02x: error, aborted\n",
303 __func__, OV2680_EXPOSURE_H);
308 ret = ov2680_write_reg(client, 2, OV2680_AGC_H, gain);
310 dev_err(&client->dev, "%s: write 0x%02x: error, aborted\n",
311 __func__, OV2680_AGC_H);
316 ret = ov2680_write_reg(client, 2,
317 OV2680_MWB_RED_GAIN_H, digitgain);
319 dev_err(&client->dev,
320 "%s: write 0x%02x: error, aborted\n",
321 __func__, OV2680_MWB_RED_GAIN_H);
325 ret = ov2680_write_reg(client, 2,
326 OV2680_MWB_GREEN_GAIN_H, digitgain);
328 dev_err(&client->dev,
329 "%s: write 0x%02x: error, aborted\n",
330 __func__, OV2680_MWB_RED_GAIN_H);
334 ret = ov2680_write_reg(client, 2,
335 OV2680_MWB_BLUE_GAIN_H, digitgain);
337 dev_err(&client->dev,
338 "%s: write 0x%02x: error, aborted\n",
339 __func__, OV2680_MWB_RED_GAIN_H);
345 ret = ov2680_write_reg(client, 1,
346 OV2680_GROUP_ACCESS, 0x10);
350 /* Delay launch group */
351 ret = ov2680_write_reg(client, 1,
352 OV2680_GROUP_ACCESS, 0xa0);
358 static int ov2680_set_exposure(struct v4l2_subdev *sd, int exposure,
359 int gain, int digitgain)
361 struct ov2680_device *dev = to_ov2680_sensor(sd);
364 mutex_lock(&dev->input_lock);
365 ret = __ov2680_set_exposure(sd, exposure, gain, digitgain);
366 mutex_unlock(&dev->input_lock);
371 static long ov2680_s_exposure(struct v4l2_subdev *sd,
372 struct atomisp_exposure *exposure)
374 u16 coarse_itg = exposure->integration_time[0];
375 u16 analog_gain = exposure->gain[0];
376 u16 digital_gain = exposure->gain[1];
378 /* we should not accept the invalid value below */
379 if (analog_gain == 0) {
380 struct i2c_client *client = v4l2_get_subdevdata(sd);
382 v4l2_err(client, "%s: invalid value\n", __func__);
386 // EXPOSURE CONTROL DISABLED FOR INITIAL CHECKIN, TUNING DOESN'T WORK
387 return ov2680_set_exposure(sd, coarse_itg, analog_gain, digital_gain);
390 static long ov2680_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
393 case ATOMISP_IOC_S_EXPOSURE:
394 return ov2680_s_exposure(sd, arg);
403 * This returns the exposure time being used. This should only be used
404 * for filling in EXIF data, not for actual image processing.
406 static int ov2680_q_exposure(struct v4l2_subdev *sd, s32 *value)
408 struct i2c_client *client = v4l2_get_subdevdata(sd);
413 ret = ov2680_read_reg(client, 1,
419 ret = ov2680_read_reg(client, 1,
425 reg_v += reg_v2 << 8;
426 ret = ov2680_read_reg(client, 1,
432 *value = reg_v + (((u32)reg_v2 << 16));
437 static u32 ov2680_translate_bayer_order(enum atomisp_bayer_order code)
440 case atomisp_bayer_order_rggb:
441 return MEDIA_BUS_FMT_SRGGB10_1X10;
442 case atomisp_bayer_order_grbg:
443 return MEDIA_BUS_FMT_SGRBG10_1X10;
444 case atomisp_bayer_order_bggr:
445 return MEDIA_BUS_FMT_SBGGR10_1X10;
446 case atomisp_bayer_order_gbrg:
447 return MEDIA_BUS_FMT_SGBRG10_1X10;
452 static int ov2680_v_flip(struct v4l2_subdev *sd, s32 value)
454 struct ov2680_device *dev = to_ov2680_sensor(sd);
455 struct camera_mipi_info *ov2680_info = NULL;
456 struct i2c_client *client = v4l2_get_subdevdata(sd);
461 dev_dbg(&client->dev, "@%s: value:%d\n", __func__, value);
462 ret = ov2680_read_reg(client, 1, OV2680_FLIP_REG, &val);
466 val |= OV2680_FLIP_MIRROR_BIT_ENABLE;
468 val &= ~OV2680_FLIP_MIRROR_BIT_ENABLE;
470 ret = ov2680_write_reg(client, 1,
471 OV2680_FLIP_REG, val);
474 index = (v_flag > 0 ? OV2680_FLIP_BIT : 0) | (h_flag > 0 ? OV2680_MIRROR_BIT :
476 ov2680_info = v4l2_get_subdev_hostdata(sd);
478 ov2680_info->raw_bayer_order = ov2680_bayer_order_mapping[index];
479 dev->format.code = ov2680_translate_bayer_order(
480 ov2680_info->raw_bayer_order);
485 static int ov2680_h_flip(struct v4l2_subdev *sd, s32 value)
487 struct ov2680_device *dev = to_ov2680_sensor(sd);
488 struct camera_mipi_info *ov2680_info = NULL;
489 struct i2c_client *client = v4l2_get_subdevdata(sd);
494 dev_dbg(&client->dev, "@%s: value:%d\n", __func__, value);
496 ret = ov2680_read_reg(client, 1, OV2680_MIRROR_REG, &val);
500 val |= OV2680_FLIP_MIRROR_BIT_ENABLE;
502 val &= ~OV2680_FLIP_MIRROR_BIT_ENABLE;
504 ret = ov2680_write_reg(client, 1,
505 OV2680_MIRROR_REG, val);
508 index = (v_flag > 0 ? OV2680_FLIP_BIT : 0) | (h_flag > 0 ? OV2680_MIRROR_BIT :
510 ov2680_info = v4l2_get_subdev_hostdata(sd);
512 ov2680_info->raw_bayer_order = ov2680_bayer_order_mapping[index];
513 dev->format.code = ov2680_translate_bayer_order(
514 ov2680_info->raw_bayer_order);
519 static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl)
521 struct ov2680_device *dev =
522 container_of(ctrl->handler, struct ov2680_device, ctrl_handler);
523 struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
528 dev_dbg(&client->dev, "%s: CID_VFLIP:%d.\n",
529 __func__, ctrl->val);
530 ret = ov2680_v_flip(&dev->sd, ctrl->val);
533 dev_dbg(&client->dev, "%s: CID_HFLIP:%d.\n",
534 __func__, ctrl->val);
535 ret = ov2680_h_flip(&dev->sd, ctrl->val);
543 static int ov2680_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
545 struct ov2680_device *dev =
546 container_of(ctrl->handler, struct ov2680_device, ctrl_handler);
550 case V4L2_CID_EXPOSURE_ABSOLUTE:
551 ret = ov2680_q_exposure(&dev->sd, &ctrl->val);
553 case V4L2_CID_FOCAL_ABSOLUTE:
554 ret = ov2680_g_focal(&dev->sd, &ctrl->val);
556 case V4L2_CID_FNUMBER_ABSOLUTE:
557 ret = ov2680_g_fnumber(&dev->sd, &ctrl->val);
559 case V4L2_CID_FNUMBER_RANGE:
560 ret = ov2680_g_fnumber_range(&dev->sd, &ctrl->val);
562 case V4L2_CID_BIN_FACTOR_HORZ:
563 ret = ov2680_g_bin_factor_x(&dev->sd, &ctrl->val);
565 case V4L2_CID_BIN_FACTOR_VERT:
566 ret = ov2680_g_bin_factor_y(&dev->sd, &ctrl->val);
575 static const struct v4l2_ctrl_ops ctrl_ops = {
576 .s_ctrl = ov2680_s_ctrl,
577 .g_volatile_ctrl = ov2680_g_volatile_ctrl
580 static const struct v4l2_ctrl_config ov2680_controls[] = {
583 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
584 .type = V4L2_CTRL_TYPE_INTEGER,
594 .id = V4L2_CID_FOCAL_ABSOLUTE,
595 .type = V4L2_CTRL_TYPE_INTEGER,
596 .name = "focal length",
597 .min = OV2680_FOCAL_LENGTH_DEFAULT,
598 .max = OV2680_FOCAL_LENGTH_DEFAULT,
600 .def = OV2680_FOCAL_LENGTH_DEFAULT,
605 .id = V4L2_CID_FNUMBER_ABSOLUTE,
606 .type = V4L2_CTRL_TYPE_INTEGER,
608 .min = OV2680_F_NUMBER_DEFAULT,
609 .max = OV2680_F_NUMBER_DEFAULT,
611 .def = OV2680_F_NUMBER_DEFAULT,
616 .id = V4L2_CID_FNUMBER_RANGE,
617 .type = V4L2_CTRL_TYPE_INTEGER,
618 .name = "f-number range",
619 .min = OV2680_F_NUMBER_RANGE,
620 .max = OV2680_F_NUMBER_RANGE,
622 .def = OV2680_F_NUMBER_RANGE,
627 .id = V4L2_CID_BIN_FACTOR_HORZ,
628 .type = V4L2_CTRL_TYPE_INTEGER,
629 .name = "horizontal binning factor",
631 .max = OV2680_BIN_FACTOR_MAX,
638 .id = V4L2_CID_BIN_FACTOR_VERT,
639 .type = V4L2_CTRL_TYPE_INTEGER,
640 .name = "vertical binning factor",
642 .max = OV2680_BIN_FACTOR_MAX,
649 .id = V4L2_CID_VFLIP,
650 .type = V4L2_CTRL_TYPE_BOOLEAN,
659 .id = V4L2_CID_HFLIP,
660 .type = V4L2_CTRL_TYPE_BOOLEAN,
669 static int ov2680_init_registers(struct v4l2_subdev *sd)
671 struct i2c_client *client = v4l2_get_subdevdata(sd);
674 ret = ov2680_write_reg(client, 1, OV2680_SW_RESET, 0x01);
675 ret |= ov2680_write_reg_array(client, ov2680_global_setting);
680 static int ov2680_init(struct v4l2_subdev *sd)
682 struct ov2680_device *dev = to_ov2680_sensor(sd);
686 mutex_lock(&dev->input_lock);
688 /* restore settings */
689 ov2680_res = ov2680_res_preview;
690 N_RES = N_RES_PREVIEW;
692 ret = ov2680_init_registers(sd);
694 mutex_unlock(&dev->input_lock);
699 static int power_ctrl(struct v4l2_subdev *sd, bool flag)
702 struct ov2680_device *dev = to_ov2680_sensor(sd);
703 struct i2c_client *client = v4l2_get_subdevdata(sd);
705 if (!dev || !dev->platform_data)
708 dev_dbg(&client->dev, "%s: %s", __func__, flag ? "on" : "off");
711 ret |= dev->platform_data->v1p8_ctrl(sd, 1);
712 ret |= dev->platform_data->v2p8_ctrl(sd, 1);
713 usleep_range(10000, 15000);
717 ret |= dev->platform_data->v1p8_ctrl(sd, 0);
718 ret |= dev->platform_data->v2p8_ctrl(sd, 0);
723 static int gpio_ctrl(struct v4l2_subdev *sd, bool flag)
726 struct ov2680_device *dev = to_ov2680_sensor(sd);
728 if (!dev || !dev->platform_data)
732 * The OV2680 documents only one GPIO input (#XSHUTDN), but
733 * existing integrations often wire two (reset/power_down)
734 * because that is the way other sensors work. There is no
735 * way to tell how it is wired internally, so existing
736 * firmwares expose both and we drive them symmetrically.
739 ret = dev->platform_data->gpio0_ctrl(sd, 1);
740 usleep_range(10000, 15000);
741 /* Ignore return from second gpio, it may not be there */
742 dev->platform_data->gpio1_ctrl(sd, 1);
743 usleep_range(10000, 15000);
745 dev->platform_data->gpio1_ctrl(sd, 0);
746 ret = dev->platform_data->gpio0_ctrl(sd, 0);
751 static int power_up(struct v4l2_subdev *sd)
753 struct ov2680_device *dev = to_ov2680_sensor(sd);
754 struct i2c_client *client = v4l2_get_subdevdata(sd);
757 if (!dev->platform_data) {
758 dev_err(&client->dev,
759 "no camera_sensor_platform_data");
764 ret = power_ctrl(sd, 1);
768 /* according to DS, at least 5ms is needed between DOVDD and PWDN */
769 usleep_range(5000, 6000);
772 ret = gpio_ctrl(sd, 1);
774 ret = gpio_ctrl(sd, 1);
779 /* flis clock control */
780 ret = dev->platform_data->flisclk_ctrl(sd, 1);
784 /* according to DS, 20ms is needed between PWDN and i2c access */
793 dev_err(&client->dev, "sensor power-up failed\n");
798 static int power_down(struct v4l2_subdev *sd)
800 struct ov2680_device *dev = to_ov2680_sensor(sd);
801 struct i2c_client *client = v4l2_get_subdevdata(sd);
806 if (!dev->platform_data) {
807 dev_err(&client->dev,
808 "no camera_sensor_platform_data");
812 ret = dev->platform_data->flisclk_ctrl(sd, 0);
814 dev_err(&client->dev, "flisclk failed\n");
817 ret = gpio_ctrl(sd, 0);
819 ret = gpio_ctrl(sd, 0);
821 dev_err(&client->dev, "gpio failed 2\n");
825 ret = power_ctrl(sd, 0);
827 dev_err(&client->dev, "vprog failed.\n");
832 static int ov2680_s_power(struct v4l2_subdev *sd, int on)
837 ret = power_down(sd);
841 return ov2680_init(sd);
847 * distance - calculate the distance
852 * Get the gap between resolution and w/h.
853 * res->width/height smaller than w/h wouldn't be considered.
854 * Returns the value of gap or -1 if fail.
856 #define LARGEST_ALLOWED_RATIO_MISMATCH 600
857 static int distance(struct ov2680_resolution *res, u32 w, u32 h)
859 unsigned int w_ratio = (res->width << 13) / w;
860 unsigned int h_ratio;
865 h_ratio = (res->height << 13) / h;
868 match = abs(((w_ratio << 13) / h_ratio) - 8192);
870 if ((w_ratio < 8192) || (h_ratio < 8192) ||
871 (match > LARGEST_ALLOWED_RATIO_MISMATCH))
874 return w_ratio + h_ratio;
877 /* Return the nearest higher resolution index */
878 static int nearest_resolution_index(int w, int h)
883 int min_dist = INT_MAX;
884 struct ov2680_resolution *tmp_res = NULL;
886 for (i = 0; i < N_RES; i++) {
887 tmp_res = &ov2680_res[i];
888 dist = distance(tmp_res, w, h);
891 if (dist < min_dist) {
900 static int get_resolution_index(int w, int h)
904 for (i = 0; i < N_RES; i++) {
905 if (w != ov2680_res[i].width)
907 if (h != ov2680_res[i].height)
916 static int ov2680_set_fmt(struct v4l2_subdev *sd,
917 struct v4l2_subdev_state *sd_state,
918 struct v4l2_subdev_format *format)
920 struct v4l2_mbus_framefmt *fmt = &format->format;
921 struct ov2680_device *dev = to_ov2680_sensor(sd);
922 struct i2c_client *client = v4l2_get_subdevdata(sd);
923 struct camera_mipi_info *ov2680_info = NULL;
927 dev_dbg(&client->dev, "%s: %s: pad: %d, fmt: %p\n",
929 (format->which == V4L2_SUBDEV_FORMAT_TRY) ? "try" : "set",
938 ov2680_info = v4l2_get_subdev_hostdata(sd);
942 mutex_lock(&dev->input_lock);
943 idx = nearest_resolution_index(fmt->width, fmt->height);
945 /* return the largest resolution */
946 fmt->width = ov2680_res[N_RES - 1].width;
947 fmt->height = ov2680_res[N_RES - 1].height;
949 fmt->width = ov2680_res[idx].width;
950 fmt->height = ov2680_res[idx].height;
952 fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
953 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
954 sd_state->pads->try_fmt = *fmt;
955 mutex_unlock(&dev->input_lock);
958 dev->fmt_idx = get_resolution_index(fmt->width, fmt->height);
959 dev_dbg(&client->dev, "%s: Resolution index: %d\n",
960 __func__, dev->fmt_idx);
961 if (dev->fmt_idx == -1) {
962 dev_err(&client->dev, "get resolution fail\n");
963 mutex_unlock(&dev->input_lock);
966 dev_dbg(&client->dev, "%s: i=%d, w=%d, h=%d\n",
967 __func__, dev->fmt_idx, fmt->width, fmt->height);
971 ret = ov2680_write_reg_array(client, ov2680_res[dev->fmt_idx].regs);
973 dev_err(&client->dev,
974 "ov2680 write resolution register err: %d\n", ret);
976 ret = ov2680_get_intg_factor(client, ov2680_info,
977 &ov2680_res[dev->fmt_idx]);
979 dev_err(&client->dev, "failed to get integration factor\n");
984 * recall flip functions to avoid flip registers
985 * were overridden by default setting
988 ov2680_h_flip(sd, h_flag);
990 ov2680_v_flip(sd, v_flag);
992 v4l2_info(client, "\n%s idx %d\n", __func__, dev->fmt_idx);
997 * dev_err(&client->dev, "ov2680 startup err\n");
1000 mutex_unlock(&dev->input_lock);
1004 static int ov2680_get_fmt(struct v4l2_subdev *sd,
1005 struct v4l2_subdev_state *sd_state,
1006 struct v4l2_subdev_format *format)
1008 struct v4l2_mbus_framefmt *fmt = &format->format;
1009 struct ov2680_device *dev = to_ov2680_sensor(sd);
1017 fmt->width = ov2680_res[dev->fmt_idx].width;
1018 fmt->height = ov2680_res[dev->fmt_idx].height;
1019 fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
1024 static int ov2680_detect(struct i2c_client *client)
1026 struct i2c_adapter *adapter = client->adapter;
1032 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
1035 ret = ov2680_read_reg(client, 1,
1036 OV2680_SC_CMMN_CHIP_ID_H, &high);
1038 dev_err(&client->dev, "sensor_id_high = 0x%x\n", high);
1041 ret = ov2680_read_reg(client, 1,
1042 OV2680_SC_CMMN_CHIP_ID_L, &low);
1043 id = ((((u16)high) << 8) | (u16)low);
1045 if (id != OV2680_ID) {
1046 dev_err(&client->dev, "sensor ID error 0x%x\n", id);
1050 ret = ov2680_read_reg(client, 1,
1051 OV2680_SC_CMMN_SUB_ID, &high);
1052 revision = (u8)high & 0x0f;
1054 dev_info(&client->dev, "sensor_revision id = 0x%x, rev= %d\n",
1060 static int ov2680_s_stream(struct v4l2_subdev *sd, int enable)
1062 struct ov2680_device *dev = to_ov2680_sensor(sd);
1063 struct i2c_client *client = v4l2_get_subdevdata(sd);
1066 mutex_lock(&dev->input_lock);
1068 dev_dbg(&client->dev, "ov2680_s_stream one\n");
1070 dev_dbg(&client->dev, "ov2680_s_stream off\n");
1072 ret = ov2680_write_reg(client, 1, OV2680_SW_STREAM,
1073 enable ? OV2680_START_STREAMING :
1074 OV2680_STOP_STREAMING);
1076 /* restore settings */
1077 ov2680_res = ov2680_res_preview;
1078 N_RES = N_RES_PREVIEW;
1081 //otp valid at stream on state
1082 //if(!dev->otp_data)
1083 // dev->otp_data = ov2680_otp_read(sd);
1085 mutex_unlock(&dev->input_lock);
1090 static int ov2680_s_config(struct v4l2_subdev *sd,
1091 int irq, void *platform_data)
1093 struct ov2680_device *dev = to_ov2680_sensor(sd);
1094 struct i2c_client *client = v4l2_get_subdevdata(sd);
1100 dev->platform_data =
1101 (struct camera_sensor_platform_data *)platform_data;
1103 mutex_lock(&dev->input_lock);
1105 * power off the module, then power on it in future
1106 * as first power on by board may not fulfill the
1107 * power on sequqence needed by the module
1109 ret = power_down(sd);
1111 dev_err(&client->dev, "ov2680 power-off err.\n");
1112 goto fail_power_off;
1117 dev_err(&client->dev, "ov2680 power-up err.\n");
1121 ret = dev->platform_data->csi_cfg(sd, 1);
1125 /* config & detect sensor */
1126 ret = ov2680_detect(client);
1128 dev_err(&client->dev, "ov2680_detect err s_config.\n");
1132 /* turn off sensor, after probed */
1133 ret = power_down(sd);
1135 dev_err(&client->dev, "ov2680 power-off err.\n");
1138 mutex_unlock(&dev->input_lock);
1143 dev->platform_data->csi_cfg(sd, 0);
1146 dev_err(&client->dev, "sensor power-gating failed\n");
1148 mutex_unlock(&dev->input_lock);
1152 static int ov2680_g_frame_interval(struct v4l2_subdev *sd,
1153 struct v4l2_subdev_frame_interval *interval)
1155 struct ov2680_device *dev = to_ov2680_sensor(sd);
1157 interval->interval.numerator = 1;
1158 interval->interval.denominator = ov2680_res[dev->fmt_idx].fps;
1163 static int ov2680_enum_mbus_code(struct v4l2_subdev *sd,
1164 struct v4l2_subdev_state *sd_state,
1165 struct v4l2_subdev_mbus_code_enum *code)
1167 if (code->index >= MAX_FMTS)
1170 code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
1174 static int ov2680_enum_frame_size(struct v4l2_subdev *sd,
1175 struct v4l2_subdev_state *sd_state,
1176 struct v4l2_subdev_frame_size_enum *fse)
1178 int index = fse->index;
1183 fse->min_width = ov2680_res[index].width;
1184 fse->min_height = ov2680_res[index].height;
1185 fse->max_width = ov2680_res[index].width;
1186 fse->max_height = ov2680_res[index].height;
1191 static int ov2680_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
1193 struct ov2680_device *dev = to_ov2680_sensor(sd);
1195 mutex_lock(&dev->input_lock);
1196 *frames = ov2680_res[dev->fmt_idx].skip_frames;
1197 mutex_unlock(&dev->input_lock);
1202 static const struct v4l2_subdev_video_ops ov2680_video_ops = {
1203 .s_stream = ov2680_s_stream,
1204 .g_frame_interval = ov2680_g_frame_interval,
1207 static const struct v4l2_subdev_sensor_ops ov2680_sensor_ops = {
1208 .g_skip_frames = ov2680_g_skip_frames,
1211 static const struct v4l2_subdev_core_ops ov2680_core_ops = {
1212 .s_power = ov2680_s_power,
1213 .ioctl = ov2680_ioctl,
1216 static const struct v4l2_subdev_pad_ops ov2680_pad_ops = {
1217 .enum_mbus_code = ov2680_enum_mbus_code,
1218 .enum_frame_size = ov2680_enum_frame_size,
1219 .get_fmt = ov2680_get_fmt,
1220 .set_fmt = ov2680_set_fmt,
1223 static const struct v4l2_subdev_ops ov2680_ops = {
1224 .core = &ov2680_core_ops,
1225 .video = &ov2680_video_ops,
1226 .pad = &ov2680_pad_ops,
1227 .sensor = &ov2680_sensor_ops,
1230 static int ov2680_remove(struct i2c_client *client)
1232 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1233 struct ov2680_device *dev = to_ov2680_sensor(sd);
1235 dev_dbg(&client->dev, "ov2680_remove...\n");
1237 dev->platform_data->csi_cfg(sd, 0);
1239 v4l2_device_unregister_subdev(sd);
1240 media_entity_cleanup(&dev->sd.entity);
1241 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1247 static int ov2680_probe(struct i2c_client *client)
1249 struct ov2680_device *dev;
1254 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1258 mutex_init(&dev->input_lock);
1261 v4l2_i2c_subdev_init(&dev->sd, client, &ov2680_ops);
1263 pdata = gmin_camera_platform_data(&dev->sd,
1264 ATOMISP_INPUT_FORMAT_RAW_10,
1265 atomisp_bayer_order_bggr);
1271 ret = ov2680_s_config(&dev->sd, client->irq, pdata);
1275 ret = atomisp_register_i2c_module(&dev->sd, pdata, RAW_CAMERA);
1279 dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1280 dev->pad.flags = MEDIA_PAD_FL_SOURCE;
1281 dev->format.code = MEDIA_BUS_FMT_SBGGR10_1X10;
1282 dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1284 v4l2_ctrl_handler_init(&dev->ctrl_handler,
1285 ARRAY_SIZE(ov2680_controls));
1287 ov2680_remove(client);
1291 for (i = 0; i < ARRAY_SIZE(ov2680_controls); i++)
1292 v4l2_ctrl_new_custom(&dev->ctrl_handler, &ov2680_controls[i],
1295 if (dev->ctrl_handler.error) {
1296 ov2680_remove(client);
1297 return dev->ctrl_handler.error;
1300 /* Use same lock for controls as for everything else. */
1301 dev->ctrl_handler.lock = &dev->input_lock;
1302 dev->sd.ctrl_handler = &dev->ctrl_handler;
1304 ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad);
1306 ov2680_remove(client);
1307 dev_dbg(&client->dev, "+++ remove ov2680\n");
1311 dev_dbg(&client->dev, "+++ out free\n");
1312 v4l2_device_unregister_subdev(&dev->sd);
1317 static const struct acpi_device_id ov2680_acpi_match[] = {
1322 MODULE_DEVICE_TABLE(acpi, ov2680_acpi_match);
1324 static struct i2c_driver ov2680_driver = {
1327 .acpi_match_table = ov2680_acpi_match,
1329 .probe_new = ov2680_probe,
1330 .remove = ov2680_remove,
1332 module_i2c_driver(ov2680_driver);
1334 MODULE_AUTHOR("Jacky Wang <Jacky_wang@ovt.com>");
1335 MODULE_DESCRIPTION("A low-level driver for OmniVision 2680 sensors");
1336 MODULE_LICENSE("GPL");