2 * Support for OmniVision OV2722 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/moduleparam.h>
30 #include <media/v4l2-device.h>
31 #include "../include/linux/atomisp_gmin_platform.h"
32 #include <linux/acpi.h>
37 /* i2c read/write stuff */
38 static int ov2722_read_reg(struct i2c_client *client,
39 u16 data_length, u16 reg, u16 *val)
42 struct i2c_msg msg[2];
43 unsigned char data[6];
45 if (!client->adapter) {
46 dev_err(&client->dev, "%s error, no client->adapter\n",
51 if (data_length != OV2722_8BIT && data_length != OV2722_16BIT
52 && data_length != OV2722_32BIT) {
53 dev_err(&client->dev, "%s error, invalid data length\n",
58 memset(msg, 0, sizeof(msg));
60 msg[0].addr = client->addr;
62 msg[0].len = I2C_MSG_LENGTH;
65 /* high byte goes out first */
66 data[0] = (u8)(reg >> 8);
67 data[1] = (u8)(reg & 0xff);
69 msg[1].addr = client->addr;
70 msg[1].len = data_length;
71 msg[1].flags = I2C_M_RD;
74 err = i2c_transfer(client->adapter, msg, 2);
79 "read from offset 0x%x error %d", reg, err);
84 /* high byte comes first */
85 if (data_length == OV2722_8BIT)
87 else if (data_length == OV2722_16BIT)
88 *val = be16_to_cpu(*(__be16 *)&data[0]);
90 *val = be32_to_cpu(*(__be32 *)&data[0]);
95 static int ov2722_i2c_write(struct i2c_client *client, u16 len, u8 *data)
98 const int num_msg = 1;
101 msg.addr = client->addr;
105 ret = i2c_transfer(client->adapter, &msg, 1);
107 return ret == num_msg ? 0 : -EIO;
110 static int ov2722_write_reg(struct i2c_client *client, u16 data_length,
114 unsigned char data[4] = {0};
115 __be16 *wreg = (__be16 *)data;
116 const u16 len = data_length + sizeof(u16); /* 16-bit address + data */
118 if (data_length != OV2722_8BIT && data_length != OV2722_16BIT) {
119 dev_err(&client->dev,
120 "%s error, invalid data_length\n", __func__);
124 /* high byte goes out first */
125 *wreg = cpu_to_be16(reg);
127 if (data_length == OV2722_8BIT) {
131 __be16 *wdata = (__be16 *)&data[2];
133 *wdata = cpu_to_be16(val);
136 ret = ov2722_i2c_write(client, len, data);
138 dev_err(&client->dev,
139 "write error: wrote 0x%x to offset 0x%x error %d",
146 * ov2722_write_reg_array - Initializes a list of OV2722 registers
147 * @client: i2c driver client structure
148 * @reglist: list of registers to be written
150 * This function initializes a list of registers. When consecutive addresses
151 * are found in a row on the list, this function creates a buffer and sends
152 * consecutive data in a single i2c_transfer().
154 * __ov2722_flush_reg_array, __ov2722_buf_reg_array() and
155 * __ov2722_write_reg_is_consecutive() are internal functions to
156 * ov2722_write_reg_array_fast() and should be not used anywhere else.
160 static int __ov2722_flush_reg_array(struct i2c_client *client,
161 struct ov2722_write_ctrl *ctrl)
164 __be16 *data16 = (void *)&ctrl->buffer.addr;
166 if (ctrl->index == 0)
169 size = sizeof(u16) + ctrl->index; /* 16-bit address + data */
170 *data16 = cpu_to_be16(ctrl->buffer.addr);
173 return ov2722_i2c_write(client, size, (u8 *)&ctrl->buffer);
176 static int __ov2722_buf_reg_array(struct i2c_client *client,
177 struct ov2722_write_ctrl *ctrl,
178 const struct ov2722_reg *next)
183 switch (next->type) {
186 ctrl->buffer.data[ctrl->index] = (u8)next->val;
190 data16 = (void *)&ctrl->buffer.data[ctrl->index];
191 *data16 = cpu_to_be16((u16)next->val);
197 /* When first item is added, we need to store its starting address */
198 if (ctrl->index == 0)
199 ctrl->buffer.addr = next->reg;
204 * Buffer cannot guarantee free space for u32? Better flush it to avoid
205 * possible lack of memory for next item.
207 if (ctrl->index + sizeof(u16) >= OV2722_MAX_WRITE_BUF_SIZE)
208 return __ov2722_flush_reg_array(client, ctrl);
213 static int __ov2722_write_reg_is_consecutive(struct i2c_client *client,
214 struct ov2722_write_ctrl *ctrl,
215 const struct ov2722_reg *next)
217 if (ctrl->index == 0)
220 return ctrl->buffer.addr + ctrl->index == next->reg;
223 static int ov2722_write_reg_array(struct i2c_client *client,
224 const struct ov2722_reg *reglist)
226 const struct ov2722_reg *next = reglist;
227 struct ov2722_write_ctrl ctrl;
231 for (; next->type != OV2722_TOK_TERM; next++) {
232 switch (next->type & OV2722_TOK_MASK) {
233 case OV2722_TOK_DELAY:
234 err = __ov2722_flush_reg_array(client, &ctrl);
241 * If next address is not consecutive, data needs to be
242 * flushed before proceed.
244 if (!__ov2722_write_reg_is_consecutive(client, &ctrl,
246 err = __ov2722_flush_reg_array(client, &ctrl);
250 err = __ov2722_buf_reg_array(client, &ctrl, next);
252 dev_err(&client->dev, "%s: write error, aborted\n",
260 return __ov2722_flush_reg_array(client, &ctrl);
263 static int ov2722_g_focal(struct v4l2_subdev *sd, s32 *val)
265 *val = (OV2722_FOCAL_LENGTH_NUM << 16) | OV2722_FOCAL_LENGTH_DEM;
269 static int ov2722_g_fnumber(struct v4l2_subdev *sd, s32 *val)
271 /*const f number for imx*/
272 *val = (OV2722_F_NUMBER_DEFAULT_NUM << 16) | OV2722_F_NUMBER_DEM;
276 static int ov2722_g_fnumber_range(struct v4l2_subdev *sd, s32 *val)
278 *val = (OV2722_F_NUMBER_DEFAULT_NUM << 24) |
279 (OV2722_F_NUMBER_DEM << 16) |
280 (OV2722_F_NUMBER_DEFAULT_NUM << 8) | OV2722_F_NUMBER_DEM;
284 static int ov2722_get_intg_factor(struct i2c_client *client,
285 struct camera_mipi_info *info,
286 const struct ov2722_resolution *res)
288 struct v4l2_subdev *sd = i2c_get_clientdata(client);
289 struct ov2722_device *dev = NULL;
290 struct atomisp_sensor_mode_data *buf = &info->data;
291 const unsigned int ext_clk_freq_hz = 19200000;
292 const unsigned int pll_invariant_div = 10;
293 unsigned int pix_clk_freq_hz;
303 dev = to_ov2722_sensor(sd);
305 /* pixel clock calculattion */
306 ret = ov2722_read_reg(client, OV2722_8BIT,
307 OV2722_SC_CMMN_PLL_CTRL3, &pre_pll_clk_div);
311 ret = ov2722_read_reg(client, OV2722_8BIT,
312 OV2722_SC_CMMN_PLL_MULTIPLIER, &pll_multiplier);
316 ret = ov2722_read_reg(client, OV2722_8BIT,
317 OV2722_SC_CMMN_PLL_DEBUG_OPT, &op_pix_clk_div);
321 pre_pll_clk_div = (pre_pll_clk_div & 0x70) >> 4;
322 if (!pre_pll_clk_div)
325 pll_multiplier = pll_multiplier & 0x7f;
326 op_pix_clk_div = op_pix_clk_div & 0x03;
327 pix_clk_freq_hz = ext_clk_freq_hz / pre_pll_clk_div * pll_multiplier
328 * op_pix_clk_div / pll_invariant_div;
330 dev->vt_pix_clk_freq_mhz = pix_clk_freq_hz;
331 buf->vt_pix_clk_freq_mhz = pix_clk_freq_hz;
333 /* get integration time */
334 buf->coarse_integration_time_min = OV2722_COARSE_INTG_TIME_MIN;
335 buf->coarse_integration_time_max_margin =
336 OV2722_COARSE_INTG_TIME_MAX_MARGIN;
338 buf->fine_integration_time_min = OV2722_FINE_INTG_TIME_MIN;
339 buf->fine_integration_time_max_margin =
340 OV2722_FINE_INTG_TIME_MAX_MARGIN;
342 buf->fine_integration_time_def = OV2722_FINE_INTG_TIME_MIN;
343 buf->frame_length_lines = res->lines_per_frame;
344 buf->line_length_pck = res->pixels_per_line;
345 buf->read_mode = res->bin_mode;
347 /* get the cropping and output resolution to ISP for this mode. */
348 ret = ov2722_read_reg(client, OV2722_16BIT,
349 OV2722_H_CROP_START_H, ®_val);
352 buf->crop_horizontal_start = reg_val;
354 ret = ov2722_read_reg(client, OV2722_16BIT,
355 OV2722_V_CROP_START_H, ®_val);
358 buf->crop_vertical_start = reg_val;
360 ret = ov2722_read_reg(client, OV2722_16BIT,
361 OV2722_H_CROP_END_H, ®_val);
364 buf->crop_horizontal_end = reg_val;
366 ret = ov2722_read_reg(client, OV2722_16BIT,
367 OV2722_V_CROP_END_H, ®_val);
370 buf->crop_vertical_end = reg_val;
372 ret = ov2722_read_reg(client, OV2722_16BIT,
373 OV2722_H_OUTSIZE_H, ®_val);
376 buf->output_width = reg_val;
378 ret = ov2722_read_reg(client, OV2722_16BIT,
379 OV2722_V_OUTSIZE_H, ®_val);
382 buf->output_height = reg_val;
384 buf->binning_factor_x = res->bin_factor_x ?
385 res->bin_factor_x : 1;
386 buf->binning_factor_y = res->bin_factor_y ?
387 res->bin_factor_y : 1;
391 static long __ov2722_set_exposure(struct v4l2_subdev *sd, int coarse_itg,
392 int gain, int digitgain)
395 struct i2c_client *client = v4l2_get_subdevdata(sd);
396 struct ov2722_device *dev = to_ov2722_sensor(sd);
400 dev_dbg(&client->dev, "set_exposure without group hold\n");
402 /* clear VTS_DIFF on manual mode */
403 ret = ov2722_write_reg(client, OV2722_16BIT, OV2722_VTS_DIFF_H, 0);
407 hts = dev->pixels_per_line;
408 vts = dev->lines_per_frame;
410 if ((coarse_itg + OV2722_COARSE_INTG_TIME_MAX_MARGIN) > vts)
411 vts = coarse_itg + OV2722_COARSE_INTG_TIME_MAX_MARGIN;
416 ret = ov2722_write_reg(client, OV2722_16BIT,
421 ret = ov2722_write_reg(client, OV2722_16BIT,
427 ret = ov2722_write_reg(client, OV2722_8BIT,
428 OV2722_AEC_PK_EXPO_L,
433 ret = ov2722_write_reg(client, OV2722_16BIT,
434 OV2722_AEC_PK_EXPO_H,
435 (coarse_itg >> 8) & 0xfff);
439 /* set analog gain */
440 ret = ov2722_write_reg(client, OV2722_16BIT,
441 OV2722_AGC_ADJ_H, gain);
445 /* set digital gain */
446 ret = ov2722_write_reg(client, OV2722_16BIT,
447 OV2722_MWB_GAIN_R_H, digitgain);
451 ret = ov2722_write_reg(client, OV2722_16BIT,
452 OV2722_MWB_GAIN_G_H, digitgain);
456 ret = ov2722_write_reg(client, OV2722_16BIT,
457 OV2722_MWB_GAIN_B_H, digitgain);
462 static int ov2722_set_exposure(struct v4l2_subdev *sd, int exposure,
463 int gain, int digitgain)
465 struct ov2722_device *dev = to_ov2722_sensor(sd);
468 mutex_lock(&dev->input_lock);
469 ret = __ov2722_set_exposure(sd, exposure, gain, digitgain);
470 mutex_unlock(&dev->input_lock);
475 static long ov2722_s_exposure(struct v4l2_subdev *sd,
476 struct atomisp_exposure *exposure)
478 int exp = exposure->integration_time[0];
479 int gain = exposure->gain[0];
480 int digitgain = exposure->gain[1];
482 /* we should not accept the invalid value below. */
484 struct i2c_client *client = v4l2_get_subdevdata(sd);
486 v4l2_err(client, "%s: invalid value\n", __func__);
490 return ov2722_set_exposure(sd, exp, gain, digitgain);
493 static long ov2722_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
496 case ATOMISP_IOC_S_EXPOSURE:
497 return ov2722_s_exposure(sd, arg);
504 /* This returns the exposure time being used. This should only be used
505 * for filling in EXIF data, not for actual image processing.
507 static int ov2722_q_exposure(struct v4l2_subdev *sd, s32 *value)
509 struct i2c_client *client = v4l2_get_subdevdata(sd);
514 ret = ov2722_read_reg(client, OV2722_8BIT,
515 OV2722_AEC_PK_EXPO_L,
520 ret = ov2722_read_reg(client, OV2722_8BIT,
521 OV2722_AEC_PK_EXPO_M,
526 reg_v += reg_v2 << 8;
527 ret = ov2722_read_reg(client, OV2722_8BIT,
528 OV2722_AEC_PK_EXPO_H,
533 *value = reg_v + (((u32)reg_v2 << 16));
538 static int ov2722_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
540 struct ov2722_device *dev =
541 container_of(ctrl->handler, struct ov2722_device, ctrl_handler);
546 case V4L2_CID_EXPOSURE_ABSOLUTE:
547 ret = ov2722_q_exposure(&dev->sd, &ctrl->val);
549 case V4L2_CID_FOCAL_ABSOLUTE:
550 ret = ov2722_g_focal(&dev->sd, &ctrl->val);
552 case V4L2_CID_FNUMBER_ABSOLUTE:
553 ret = ov2722_g_fnumber(&dev->sd, &ctrl->val);
555 case V4L2_CID_FNUMBER_RANGE:
556 ret = ov2722_g_fnumber_range(&dev->sd, &ctrl->val);
558 case V4L2_CID_LINK_FREQ:
559 val = ov2722_res[dev->fmt_idx].mipi_freq;
563 ctrl->val = val * 1000; /* To Hz */
572 static const struct v4l2_ctrl_ops ctrl_ops = {
573 .g_volatile_ctrl = ov2722_g_volatile_ctrl
576 static const struct v4l2_ctrl_config ov2722_controls[] = {
579 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
580 .type = V4L2_CTRL_TYPE_INTEGER,
590 .id = V4L2_CID_FOCAL_ABSOLUTE,
591 .type = V4L2_CTRL_TYPE_INTEGER,
592 .name = "focal length",
593 .min = OV2722_FOCAL_LENGTH_DEFAULT,
594 .max = OV2722_FOCAL_LENGTH_DEFAULT,
596 .def = OV2722_FOCAL_LENGTH_DEFAULT,
601 .id = V4L2_CID_FNUMBER_ABSOLUTE,
602 .type = V4L2_CTRL_TYPE_INTEGER,
604 .min = OV2722_F_NUMBER_DEFAULT,
605 .max = OV2722_F_NUMBER_DEFAULT,
607 .def = OV2722_F_NUMBER_DEFAULT,
612 .id = V4L2_CID_FNUMBER_RANGE,
613 .type = V4L2_CTRL_TYPE_INTEGER,
614 .name = "f-number range",
615 .min = OV2722_F_NUMBER_RANGE,
616 .max = OV2722_F_NUMBER_RANGE,
618 .def = OV2722_F_NUMBER_RANGE,
623 .id = V4L2_CID_LINK_FREQ,
624 .name = "Link Frequency",
625 .type = V4L2_CTRL_TYPE_INTEGER,
627 .max = 1500000 * 1000,
630 .flags = V4L2_CTRL_FLAG_VOLATILE | V4L2_CTRL_FLAG_READ_ONLY,
634 static int ov2722_init(struct v4l2_subdev *sd)
636 struct ov2722_device *dev = to_ov2722_sensor(sd);
638 mutex_lock(&dev->input_lock);
640 /* restore settings */
641 ov2722_res = ov2722_res_preview;
642 N_RES = N_RES_PREVIEW;
644 mutex_unlock(&dev->input_lock);
649 static int power_ctrl(struct v4l2_subdev *sd, bool flag)
652 struct ov2722_device *dev = to_ov2722_sensor(sd);
654 if (!dev || !dev->platform_data)
658 ret = dev->platform_data->v1p8_ctrl(sd, 1);
660 ret = dev->platform_data->v2p8_ctrl(sd, 1);
662 dev->platform_data->v1p8_ctrl(sd, 0);
665 ret = dev->platform_data->v1p8_ctrl(sd, 0);
666 ret |= dev->platform_data->v2p8_ctrl(sd, 0);
672 static int gpio_ctrl(struct v4l2_subdev *sd, bool flag)
674 struct ov2722_device *dev = to_ov2722_sensor(sd);
677 if (!dev || !dev->platform_data)
680 /* Note: the GPIO order is asymmetric: always RESET#
681 * before PWDN# when turning it on or off.
683 ret = dev->platform_data->gpio0_ctrl(sd, flag);
685 *ov2722 PWDN# active high when pull down,opposite to the convention
687 ret |= dev->platform_data->gpio1_ctrl(sd, !flag);
691 static int power_up(struct v4l2_subdev *sd)
693 struct ov2722_device *dev = to_ov2722_sensor(sd);
694 struct i2c_client *client = v4l2_get_subdevdata(sd);
697 if (!dev->platform_data) {
698 dev_err(&client->dev,
699 "no camera_sensor_platform_data");
704 ret = power_ctrl(sd, 1);
708 /* according to DS, at least 5ms is needed between DOVDD and PWDN */
709 usleep_range(5000, 6000);
712 ret = gpio_ctrl(sd, 1);
714 ret = gpio_ctrl(sd, 0);
719 /* flis clock control */
720 ret = dev->platform_data->flisclk_ctrl(sd, 1);
724 /* according to DS, 20ms is needed between PWDN and i2c access */
733 dev_err(&client->dev, "sensor power-up failed\n");
738 static int power_down(struct v4l2_subdev *sd)
740 struct ov2722_device *dev = to_ov2722_sensor(sd);
741 struct i2c_client *client = v4l2_get_subdevdata(sd);
744 if (!dev->platform_data) {
745 dev_err(&client->dev,
746 "no camera_sensor_platform_data");
750 ret = dev->platform_data->flisclk_ctrl(sd, 0);
752 dev_err(&client->dev, "flisclk failed\n");
755 ret = gpio_ctrl(sd, 0);
757 ret = gpio_ctrl(sd, 0);
759 dev_err(&client->dev, "gpio failed 2\n");
763 ret = power_ctrl(sd, 0);
765 dev_err(&client->dev, "vprog failed.\n");
770 static int ov2722_s_power(struct v4l2_subdev *sd, int on)
775 return power_down(sd);
779 return ov2722_init(sd);
785 * distance - calculate the distance
790 * Get the gap between resolution and w/h.
791 * res->width/height smaller than w/h wouldn't be considered.
792 * Returns the value of gap or -1 if fail.
794 #define LARGEST_ALLOWED_RATIO_MISMATCH 800
795 static int distance(struct ov2722_resolution *res, u32 w, u32 h)
797 unsigned int w_ratio = (res->width << 13) / w;
798 unsigned int h_ratio;
803 h_ratio = (res->height << 13) / h;
806 match = abs(((w_ratio << 13) / h_ratio) - 8192);
808 if ((w_ratio < 8192) || (h_ratio < 8192) ||
809 (match > LARGEST_ALLOWED_RATIO_MISMATCH))
812 return w_ratio + h_ratio;
815 /* Return the nearest higher resolution index */
816 static int nearest_resolution_index(int w, int h)
821 int min_dist = INT_MAX;
822 struct ov2722_resolution *tmp_res = NULL;
824 for (i = 0; i < N_RES; i++) {
825 tmp_res = &ov2722_res[i];
826 dist = distance(tmp_res, w, h);
829 if (dist < min_dist) {
838 static int get_resolution_index(int w, int h)
842 for (i = 0; i < N_RES; i++) {
843 if (w != ov2722_res[i].width)
845 if (h != ov2722_res[i].height)
854 /* TODO: remove it. */
855 static int startup(struct v4l2_subdev *sd)
857 struct ov2722_device *dev = to_ov2722_sensor(sd);
858 struct i2c_client *client = v4l2_get_subdevdata(sd);
861 ret = ov2722_write_reg(client, OV2722_8BIT,
862 OV2722_SW_RESET, 0x01);
864 dev_err(&client->dev, "ov2722 reset err.\n");
868 ret = ov2722_write_reg_array(client, ov2722_res[dev->fmt_idx].regs);
870 dev_err(&client->dev, "ov2722 write register err.\n");
877 static int ov2722_set_fmt(struct v4l2_subdev *sd,
878 struct v4l2_subdev_pad_config *cfg,
879 struct v4l2_subdev_format *format)
881 struct v4l2_mbus_framefmt *fmt = &format->format;
882 struct ov2722_device *dev = to_ov2722_sensor(sd);
883 struct i2c_client *client = v4l2_get_subdevdata(sd);
884 struct camera_mipi_info *ov2722_info = NULL;
892 ov2722_info = v4l2_get_subdev_hostdata(sd);
896 mutex_lock(&dev->input_lock);
897 idx = nearest_resolution_index(fmt->width, fmt->height);
899 /* return the largest resolution */
900 fmt->width = ov2722_res[N_RES - 1].width;
901 fmt->height = ov2722_res[N_RES - 1].height;
903 fmt->width = ov2722_res[idx].width;
904 fmt->height = ov2722_res[idx].height;
906 fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
907 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
909 mutex_unlock(&dev->input_lock);
913 dev->fmt_idx = get_resolution_index(fmt->width, fmt->height);
914 if (dev->fmt_idx == -1) {
915 dev_err(&client->dev, "get resolution fail\n");
916 mutex_unlock(&dev->input_lock);
920 dev->pixels_per_line = ov2722_res[dev->fmt_idx].pixels_per_line;
921 dev->lines_per_frame = ov2722_res[dev->fmt_idx].lines_per_frame;
927 dev_err(&client->dev, "ov2722 startup err, retry to power up\n");
928 for (i = 0; i < OV2722_POWER_UP_RETRY_NUM; i++) {
929 dev_err(&client->dev,
930 "ov2722 retry to power up %d/%d times, result: ",
931 i + 1, OV2722_POWER_UP_RETRY_NUM);
935 dev_err(&client->dev, "power up failed, continue\n");
940 dev_err(&client->dev, " startup FAILED!\n");
942 dev_err(&client->dev, " startup SUCCESS!\n");
947 dev_err(&client->dev, "ov2722 startup err\n");
952 ret = ov2722_get_intg_factor(client, ov2722_info,
953 &ov2722_res[dev->fmt_idx]);
955 dev_err(&client->dev, "failed to get integration_factor\n");
958 mutex_unlock(&dev->input_lock);
962 static int ov2722_get_fmt(struct v4l2_subdev *sd,
963 struct v4l2_subdev_pad_config *cfg,
964 struct v4l2_subdev_format *format)
966 struct v4l2_mbus_framefmt *fmt = &format->format;
967 struct ov2722_device *dev = to_ov2722_sensor(sd);
974 fmt->width = ov2722_res[dev->fmt_idx].width;
975 fmt->height = ov2722_res[dev->fmt_idx].height;
976 fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
981 static int ov2722_detect(struct i2c_client *client)
983 struct i2c_adapter *adapter = client->adapter;
989 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
992 ret = ov2722_read_reg(client, OV2722_8BIT,
993 OV2722_SC_CMMN_CHIP_ID_H, &high);
995 dev_err(&client->dev, "sensor_id_high = 0x%x\n", high);
998 ret = ov2722_read_reg(client, OV2722_8BIT,
999 OV2722_SC_CMMN_CHIP_ID_L, &low);
1000 id = (high << 8) | low;
1002 if ((id != OV2722_ID) && (id != OV2720_ID)) {
1003 dev_err(&client->dev, "sensor ID error\n");
1007 ret = ov2722_read_reg(client, OV2722_8BIT,
1008 OV2722_SC_CMMN_SUB_ID, &high);
1009 revision = (u8)high & 0x0f;
1011 dev_dbg(&client->dev, "sensor_revision = 0x%x\n", revision);
1012 dev_dbg(&client->dev, "detect ov2722 success\n");
1016 static int ov2722_s_stream(struct v4l2_subdev *sd, int enable)
1018 struct ov2722_device *dev = to_ov2722_sensor(sd);
1019 struct i2c_client *client = v4l2_get_subdevdata(sd);
1022 mutex_lock(&dev->input_lock);
1024 ret = ov2722_write_reg(client, OV2722_8BIT, OV2722_SW_STREAM,
1025 enable ? OV2722_START_STREAMING :
1026 OV2722_STOP_STREAMING);
1028 mutex_unlock(&dev->input_lock);
1032 static int ov2722_s_config(struct v4l2_subdev *sd,
1033 int irq, void *platform_data)
1035 struct ov2722_device *dev = to_ov2722_sensor(sd);
1036 struct i2c_client *client = v4l2_get_subdevdata(sd);
1042 dev->platform_data =
1043 (struct camera_sensor_platform_data *)platform_data;
1045 mutex_lock(&dev->input_lock);
1047 /* power off the module, then power on it in future
1048 * as first power on by board may not fulfill the
1049 * power on sequqence needed by the module
1051 ret = power_down(sd);
1053 dev_err(&client->dev, "ov2722 power-off err.\n");
1054 goto fail_power_off;
1059 dev_err(&client->dev, "ov2722 power-up err.\n");
1063 ret = dev->platform_data->csi_cfg(sd, 1);
1067 /* config & detect sensor */
1068 ret = ov2722_detect(client);
1070 dev_err(&client->dev, "ov2722_detect err s_config.\n");
1074 /* turn off sensor, after probed */
1075 ret = power_down(sd);
1077 dev_err(&client->dev, "ov2722 power-off err.\n");
1080 mutex_unlock(&dev->input_lock);
1085 dev->platform_data->csi_cfg(sd, 0);
1088 dev_err(&client->dev, "sensor power-gating failed\n");
1090 mutex_unlock(&dev->input_lock);
1094 static int ov2722_g_frame_interval(struct v4l2_subdev *sd,
1095 struct v4l2_subdev_frame_interval *interval)
1097 struct ov2722_device *dev = to_ov2722_sensor(sd);
1099 interval->interval.numerator = 1;
1100 interval->interval.denominator = ov2722_res[dev->fmt_idx].fps;
1105 static int ov2722_enum_mbus_code(struct v4l2_subdev *sd,
1106 struct v4l2_subdev_pad_config *cfg,
1107 struct v4l2_subdev_mbus_code_enum *code)
1109 if (code->index >= MAX_FMTS)
1112 code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
1116 static int ov2722_enum_frame_size(struct v4l2_subdev *sd,
1117 struct v4l2_subdev_pad_config *cfg,
1118 struct v4l2_subdev_frame_size_enum *fse)
1120 int index = fse->index;
1125 fse->min_width = ov2722_res[index].width;
1126 fse->min_height = ov2722_res[index].height;
1127 fse->max_width = ov2722_res[index].width;
1128 fse->max_height = ov2722_res[index].height;
1133 static int ov2722_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
1135 struct ov2722_device *dev = to_ov2722_sensor(sd);
1137 mutex_lock(&dev->input_lock);
1138 *frames = ov2722_res[dev->fmt_idx].skip_frames;
1139 mutex_unlock(&dev->input_lock);
1144 static const struct v4l2_subdev_sensor_ops ov2722_sensor_ops = {
1145 .g_skip_frames = ov2722_g_skip_frames,
1148 static const struct v4l2_subdev_video_ops ov2722_video_ops = {
1149 .s_stream = ov2722_s_stream,
1150 .g_frame_interval = ov2722_g_frame_interval,
1153 static const struct v4l2_subdev_core_ops ov2722_core_ops = {
1154 .s_power = ov2722_s_power,
1155 .ioctl = ov2722_ioctl,
1158 static const struct v4l2_subdev_pad_ops ov2722_pad_ops = {
1159 .enum_mbus_code = ov2722_enum_mbus_code,
1160 .enum_frame_size = ov2722_enum_frame_size,
1161 .get_fmt = ov2722_get_fmt,
1162 .set_fmt = ov2722_set_fmt,
1165 static const struct v4l2_subdev_ops ov2722_ops = {
1166 .core = &ov2722_core_ops,
1167 .video = &ov2722_video_ops,
1168 .pad = &ov2722_pad_ops,
1169 .sensor = &ov2722_sensor_ops,
1172 static int ov2722_remove(struct i2c_client *client)
1174 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1175 struct ov2722_device *dev = to_ov2722_sensor(sd);
1177 dev_dbg(&client->dev, "ov2722_remove...\n");
1179 dev->platform_data->csi_cfg(sd, 0);
1180 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1181 v4l2_device_unregister_subdev(sd);
1183 atomisp_gmin_remove_subdev(sd);
1185 media_entity_cleanup(&dev->sd.entity);
1191 static int __ov2722_init_ctrl_handler(struct ov2722_device *dev)
1193 struct v4l2_ctrl_handler *hdl;
1196 hdl = &dev->ctrl_handler;
1197 v4l2_ctrl_handler_init(&dev->ctrl_handler, ARRAY_SIZE(ov2722_controls));
1198 for (i = 0; i < ARRAY_SIZE(ov2722_controls); i++)
1199 v4l2_ctrl_new_custom(&dev->ctrl_handler, &ov2722_controls[i],
1202 dev->link_freq = v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_LINK_FREQ);
1204 if (dev->ctrl_handler.error || !dev->link_freq)
1205 return dev->ctrl_handler.error;
1207 dev->sd.ctrl_handler = hdl;
1212 static int ov2722_probe(struct i2c_client *client)
1214 struct ov2722_device *dev;
1218 struct acpi_device *adev;
1220 handle = ACPI_HANDLE(&client->dev);
1221 if (!handle || acpi_bus_get_device(handle, &adev)) {
1222 dev_err(&client->dev, "Error could not get ACPI device\n");
1225 pr_info("%s: ACPI detected it on bus ID=%s, HID=%s\n",
1226 __func__, acpi_device_bid(adev), acpi_device_hid(adev));
1227 // FIXME: may need to release resources allocated by acpi_bus_get_device()
1229 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1233 mutex_init(&dev->input_lock);
1236 v4l2_i2c_subdev_init(&dev->sd, client, &ov2722_ops);
1238 ovpdev = gmin_camera_platform_data(&dev->sd,
1239 ATOMISP_INPUT_FORMAT_RAW_10,
1240 atomisp_bayer_order_grbg);
1242 ret = ov2722_s_config(&dev->sd, client->irq, ovpdev);
1246 ret = __ov2722_init_ctrl_handler(dev);
1248 goto out_ctrl_handler_free;
1250 dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1251 dev->pad.flags = MEDIA_PAD_FL_SOURCE;
1252 dev->format.code = MEDIA_BUS_FMT_SBGGR10_1X10;
1253 dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1255 ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad);
1257 ov2722_remove(client);
1259 return atomisp_register_i2c_module(&dev->sd, ovpdev, RAW_CAMERA);
1261 out_ctrl_handler_free:
1262 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1265 v4l2_device_unregister_subdev(&dev->sd);
1270 static const struct acpi_device_id ov2722_acpi_match[] = {
1274 MODULE_DEVICE_TABLE(acpi, ov2722_acpi_match);
1276 static struct i2c_driver ov2722_driver = {
1279 .acpi_match_table = ov2722_acpi_match,
1281 .probe_new = ov2722_probe,
1282 .remove = ov2722_remove,
1284 module_i2c_driver(ov2722_driver);
1286 MODULE_AUTHOR("Wei Liu <wei.liu@intel.com>");
1287 MODULE_DESCRIPTION("A low-level driver for OmniVision 2722 sensors");
1288 MODULE_LICENSE("GPL");