2 * Support for mt9m114 Camera Sensor.
4 * Copyright (c) 2010 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.
18 #include <linux/module.h>
19 #include <linux/types.h>
20 #include <linux/kernel.h>
22 #include <linux/string.h>
23 #include <linux/errno.h>
24 #include <linux/init.h>
25 #include <linux/kmod.h>
26 #include <linux/device.h>
28 #include <linux/slab.h>
29 #include <linux/delay.h>
30 #include <linux/i2c.h>
31 #include <linux/acpi.h>
32 #include "../include/linux/atomisp_gmin_platform.h"
33 #include <media/v4l2-device.h>
37 #define to_mt9m114_sensor(sd) container_of(sd, struct mt9m114_device, sd)
40 * TODO: use debug parameter to actually define when debug messages should
45 module_param(debug, int, 0644);
46 MODULE_PARM_DESC(debug, "Debug level (0-1)");
48 static int mt9m114_t_vflip(struct v4l2_subdev *sd, int value);
49 static int mt9m114_t_hflip(struct v4l2_subdev *sd, int value);
50 static int mt9m114_wait_state(struct i2c_client *client, int timeout);
53 mt9m114_read_reg(struct i2c_client *client, u16 data_length, u32 reg, u32 *val)
56 struct i2c_msg msg[2];
57 unsigned char data[4];
59 if (!client->adapter) {
60 v4l2_err(client, "%s error, no client->adapter\n", __func__);
64 if (data_length != MISENSOR_8BIT && data_length != MISENSOR_16BIT
65 && data_length != MISENSOR_32BIT) {
66 v4l2_err(client, "%s error, invalid data length\n", __func__);
70 msg[0].addr = client->addr;
72 msg[0].len = MSG_LEN_OFFSET;
75 /* high byte goes out first */
76 data[0] = (u16) (reg >> 8);
77 data[1] = (u16) (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);
88 /* high byte comes first */
89 if (data_length == MISENSOR_8BIT)
91 else if (data_length == MISENSOR_16BIT)
92 *val = data[1] + (data[0] << 8);
94 *val = data[3] + (data[2] << 8) +
95 (data[1] << 16) + (data[0] << 24);
100 dev_err(&client->dev, "read from offset 0x%x error %d", reg, err);
105 mt9m114_write_reg(struct i2c_client *client, u16 data_length, u16 reg, u32 val)
109 unsigned char data[6] = {0};
113 if (!client->adapter) {
114 v4l2_err(client, "%s error, no client->adapter\n", __func__);
118 if (data_length != MISENSOR_8BIT && data_length != MISENSOR_16BIT
119 && data_length != MISENSOR_32BIT) {
120 v4l2_err(client, "%s error, invalid data_length\n", __func__);
124 memset(&msg, 0, sizeof(msg));
127 msg.addr = client->addr;
129 msg.len = 2 + data_length;
132 /* high byte goes out first */
134 *wreg = cpu_to_be16(reg);
136 if (data_length == MISENSOR_8BIT) {
138 } else if (data_length == MISENSOR_16BIT) {
139 u16 *wdata = (u16 *)&data[2];
140 *wdata = be16_to_cpu((u16)val);
143 u32 *wdata = (u32 *)&data[2];
144 *wdata = be32_to_cpu(val);
147 num_msg = i2c_transfer(client->adapter, &msg, 1);
150 * HACK: Need some delay here for Rev 2 sensors otherwise some
151 * registers do not seem to load correctly.
158 dev_err(&client->dev, "write error: wrote 0x%x to offset 0x%x error %d",
160 if (retry <= I2C_RETRY_COUNT) {
161 dev_dbg(&client->dev, "retrying... %d", retry);
171 * misensor_rmw_reg - Read/Modify/Write a value to a register in the sensor
173 * @client: i2c driver client structure
174 * @data_length: 8/16/32-bits length
175 * @reg: register address
176 * @mask: masked out bits
179 * Read/modify/write a value to a register in the sensor device.
180 * Returns zero if successful, or non-zero otherwise.
183 misensor_rmw_reg(struct i2c_client *client, u16 data_length, u16 reg,
189 /* Exit when no mask */
193 /* @mask must not exceed data length */
194 switch (data_length) {
206 /* Wrong @data_length */
210 err = mt9m114_read_reg(client, data_length, reg, &val);
212 v4l2_err(client, "misensor_rmw_reg error exit, read failed\n");
219 * Perform the OR function if the @set exists.
220 * Shift @set value to target bit location. @set should set only
221 * bits included in @mask.
223 * REVISIT: This function expects @set to be non-shifted. Its shift
224 * value is then defined to be equal to mask's LSB position.
225 * How about to inform values in their right offset position and avoid
226 * this unneeded shift operation?
228 set <<= ffs(mask) - 1;
231 err = mt9m114_write_reg(client, data_length, reg, val);
233 v4l2_err(client, "misensor_rmw_reg error exit, write failed\n");
241 static int __mt9m114_flush_reg_array(struct i2c_client *client,
242 struct mt9m114_write_ctrl *ctrl)
245 const int num_msg = 1;
249 if (ctrl->index == 0)
253 msg.addr = client->addr;
255 msg.len = 2 + ctrl->index;
256 ctrl->buffer.addr = cpu_to_be16(ctrl->buffer.addr);
257 msg.buf = (u8 *)&ctrl->buffer;
259 ret = i2c_transfer(client->adapter, &msg, num_msg);
260 if (ret != num_msg) {
261 if (++retry <= I2C_RETRY_COUNT) {
262 dev_dbg(&client->dev, "retrying... %d\n", retry);
266 dev_err(&client->dev, "%s: i2c transfer error\n", __func__);
273 * REVISIT: Previously we had a delay after writing data to sensor.
274 * But it was removed as our tests have shown it is not necessary
281 static int __mt9m114_buf_reg_array(struct i2c_client *client,
282 struct mt9m114_write_ctrl *ctrl,
283 const struct misensor_reg *next)
289 /* Insufficient buffer? Let's flush and get more free space. */
290 if (ctrl->index + next->length >= MT9M114_MAX_WRITE_BUF_SIZE) {
291 err = __mt9m114_flush_reg_array(client, ctrl);
296 switch (next->length) {
298 ctrl->buffer.data[ctrl->index] = (u8)next->val;
301 data16 = (u16 *)&ctrl->buffer.data[ctrl->index];
302 *data16 = cpu_to_be16((u16)next->val);
305 data32 = (u32 *)&ctrl->buffer.data[ctrl->index];
306 *data32 = cpu_to_be32(next->val);
312 /* When first item is added, we need to store its starting address */
313 if (ctrl->index == 0)
314 ctrl->buffer.addr = next->reg;
316 ctrl->index += next->length;
322 __mt9m114_write_reg_is_consecutive(struct i2c_client *client,
323 struct mt9m114_write_ctrl *ctrl,
324 const struct misensor_reg *next)
326 if (ctrl->index == 0)
329 return ctrl->buffer.addr + ctrl->index == next->reg;
333 * mt9m114_write_reg_array - Initializes a list of mt9m114 registers
334 * @client: i2c driver client structure
335 * @reglist: list of registers to be written
336 * @poll: completion polling requirement
337 * This function initializes a list of registers. When consecutive addresses
338 * are found in a row on the list, this function creates a buffer and sends
339 * consecutive data in a single i2c_transfer().
341 * __mt9m114_flush_reg_array, __mt9m114_buf_reg_array() and
342 * __mt9m114_write_reg_is_consecutive() are internal functions to
343 * mt9m114_write_reg_array() and should be not used anywhere else.
346 static int mt9m114_write_reg_array(struct i2c_client *client,
347 const struct misensor_reg *reglist,
350 const struct misensor_reg *next = reglist;
351 struct mt9m114_write_ctrl ctrl;
354 if (poll == PRE_POLLING) {
355 err = mt9m114_wait_state(client, MT9M114_WAIT_STAT_TIMEOUT);
361 for (; next->length != MISENSOR_TOK_TERM; next++) {
362 switch (next->length & MISENSOR_TOK_MASK) {
363 case MISENSOR_TOK_DELAY:
364 err = __mt9m114_flush_reg_array(client, &ctrl);
369 case MISENSOR_TOK_RMW:
370 err = __mt9m114_flush_reg_array(client, &ctrl);
371 err |= misensor_rmw_reg(client,
374 next->reg, next->val,
377 dev_err(&client->dev, "%s read err. aborted\n",
384 * If next address is not consecutive, data needs to be
385 * flushed before proceed.
387 if (!__mt9m114_write_reg_is_consecutive(client, &ctrl,
389 err = __mt9m114_flush_reg_array(client, &ctrl);
393 err = __mt9m114_buf_reg_array(client, &ctrl, next);
395 v4l2_err(client, "%s: write error, aborted\n",
403 err = __mt9m114_flush_reg_array(client, &ctrl);
407 if (poll == POST_POLLING)
408 return mt9m114_wait_state(client, MT9M114_WAIT_STAT_TIMEOUT);
413 static int mt9m114_wait_state(struct i2c_client *client, int timeout)
418 while (timeout-- > 0) {
419 ret = mt9m114_read_reg(client, MISENSOR_16BIT, 0x0080, &val);
422 if ((val & 0x2) == 0)
431 static int mt9m114_set_suspend(struct v4l2_subdev *sd)
433 struct i2c_client *client = v4l2_get_subdevdata(sd);
434 return mt9m114_write_reg_array(client,
435 mt9m114_standby_reg, POST_POLLING);
438 static int mt9m114_init_common(struct v4l2_subdev *sd)
440 struct i2c_client *client = v4l2_get_subdevdata(sd);
442 return mt9m114_write_reg_array(client, mt9m114_common, PRE_POLLING);
445 static int power_ctrl(struct v4l2_subdev *sd, bool flag)
448 struct mt9m114_device *dev = to_mt9m114_sensor(sd);
450 if (!dev || !dev->platform_data)
454 ret = dev->platform_data->v2p8_ctrl(sd, 1);
456 ret = dev->platform_data->v1p8_ctrl(sd, 1);
458 ret = dev->platform_data->v2p8_ctrl(sd, 0);
461 ret = dev->platform_data->v2p8_ctrl(sd, 0);
462 ret = dev->platform_data->v1p8_ctrl(sd, 0);
467 static int gpio_ctrl(struct v4l2_subdev *sd, bool flag)
470 struct mt9m114_device *dev = to_mt9m114_sensor(sd);
472 if (!dev || !dev->platform_data)
475 /* Note: current modules wire only one GPIO signal (RESET#),
476 * but the schematic wires up two to the connector. BIOS
477 * versions have been unfortunately inconsistent with which
478 * ACPI index RESET# is on, so hit both */
481 ret = dev->platform_data->gpio0_ctrl(sd, 0);
482 ret = dev->platform_data->gpio1_ctrl(sd, 0);
484 ret |= dev->platform_data->gpio0_ctrl(sd, 1);
485 ret |= dev->platform_data->gpio1_ctrl(sd, 1);
487 ret = dev->platform_data->gpio0_ctrl(sd, 0);
488 ret = dev->platform_data->gpio1_ctrl(sd, 0);
493 static int power_up(struct v4l2_subdev *sd)
495 struct mt9m114_device *dev = to_mt9m114_sensor(sd);
496 struct i2c_client *client = v4l2_get_subdevdata(sd);
499 if (NULL == dev->platform_data) {
500 dev_err(&client->dev, "no camera_sensor_platform_data");
505 ret = power_ctrl(sd, 1);
509 /* flis clock control */
510 ret = dev->platform_data->flisclk_ctrl(sd, 1);
515 ret = gpio_ctrl(sd, 1);
517 dev_err(&client->dev, "gpio failed 1\n");
519 * according to DS, 44ms is needed between power up and first i2c
527 dev->platform_data->flisclk_ctrl(sd, 0);
530 dev_err(&client->dev, "sensor power-up failed\n");
535 static int power_down(struct v4l2_subdev *sd)
537 struct mt9m114_device *dev = to_mt9m114_sensor(sd);
538 struct i2c_client *client = v4l2_get_subdevdata(sd);
541 if (NULL == dev->platform_data) {
542 dev_err(&client->dev, "no camera_sensor_platform_data");
546 ret = dev->platform_data->flisclk_ctrl(sd, 0);
548 dev_err(&client->dev, "flisclk failed\n");
551 ret = gpio_ctrl(sd, 0);
553 dev_err(&client->dev, "gpio failed 1\n");
556 ret = power_ctrl(sd, 0);
558 dev_err(&client->dev, "vprog failed.\n");
560 /*according to DS, 20ms is needed after power down*/
566 static int mt9m114_s_power(struct v4l2_subdev *sd, int power)
569 return power_down(sd);
574 return mt9m114_init_common(sd);
579 * distance - calculate the distance
584 * Get the gap between resolution and w/h.
585 * res->width/height smaller than w/h wouldn't be considered.
586 * Returns the value of gap or -1 if fail.
588 #define LARGEST_ALLOWED_RATIO_MISMATCH 600
589 static int distance(struct mt9m114_res_struct const *res, u32 w, u32 h)
591 unsigned int w_ratio;
592 unsigned int h_ratio;
597 w_ratio = (res->width << 13) / w;
600 h_ratio = (res->height << 13) / h;
603 match = abs(((w_ratio << 13) / h_ratio) - 8192);
605 if ((w_ratio < 8192) || (h_ratio < 8192) ||
606 (match > LARGEST_ALLOWED_RATIO_MISMATCH))
609 return w_ratio + h_ratio;
612 /* Return the nearest higher resolution index */
613 static int nearest_resolution_index(int w, int h)
618 int min_dist = INT_MAX;
619 const struct mt9m114_res_struct *tmp_res = NULL;
621 for (i = 0; i < ARRAY_SIZE(mt9m114_res); i++) {
622 tmp_res = &mt9m114_res[i];
623 dist = distance(tmp_res, w, h);
626 if (dist < min_dist) {
635 static int mt9m114_try_res(u32 *w, u32 *h)
639 if ((*w > MT9M114_RES_960P_SIZE_H)
640 || (*h > MT9M114_RES_960P_SIZE_V)) {
641 *w = MT9M114_RES_960P_SIZE_H;
642 *h = MT9M114_RES_960P_SIZE_V;
644 idx = nearest_resolution_index(*w, *h);
647 * nearest_resolution_index() doesn't return smaller
648 * resolutions. If it fails, it means the requested
649 * resolution is higher than wecan support. Fallback
650 * to highest possible resolution in this case.
653 idx = ARRAY_SIZE(mt9m114_res) - 1;
655 *w = mt9m114_res[idx].width;
656 *h = mt9m114_res[idx].height;
662 static struct mt9m114_res_struct *mt9m114_to_res(u32 w, u32 h)
666 for (index = 0; index < N_RES; index++) {
667 if ((mt9m114_res[index].width == w) &&
668 (mt9m114_res[index].height == h))
676 return &mt9m114_res[index];
679 static int mt9m114_res2size(struct v4l2_subdev *sd, int *h_size, int *v_size)
681 struct mt9m114_device *dev = to_mt9m114_sensor(sd);
682 unsigned short hsize;
683 unsigned short vsize;
686 case MT9M114_RES_736P:
687 hsize = MT9M114_RES_736P_SIZE_H;
688 vsize = MT9M114_RES_736P_SIZE_V;
690 case MT9M114_RES_864P:
691 hsize = MT9M114_RES_864P_SIZE_H;
692 vsize = MT9M114_RES_864P_SIZE_V;
694 case MT9M114_RES_960P:
695 hsize = MT9M114_RES_960P_SIZE_H;
696 vsize = MT9M114_RES_960P_SIZE_V;
699 v4l2_err(sd, "%s: Resolution 0x%08x unknown\n", __func__,
712 static int mt9m114_get_intg_factor(struct i2c_client *client,
713 struct camera_mipi_info *info,
714 const struct mt9m114_res_struct *res)
716 struct atomisp_sensor_mode_data *buf = &info->data;
723 ret = mt9m114_read_reg(client, MISENSOR_32BIT,
724 REG_PIXEL_CLK, ®_val);
727 buf->vt_pix_clk_freq_mhz = reg_val;
729 /* get integration time */
730 buf->coarse_integration_time_min = MT9M114_COARSE_INTG_TIME_MIN;
731 buf->coarse_integration_time_max_margin =
732 MT9M114_COARSE_INTG_TIME_MAX_MARGIN;
734 buf->fine_integration_time_min = MT9M114_FINE_INTG_TIME_MIN;
735 buf->fine_integration_time_max_margin =
736 MT9M114_FINE_INTG_TIME_MAX_MARGIN;
738 buf->fine_integration_time_def = MT9M114_FINE_INTG_TIME_MIN;
740 buf->frame_length_lines = res->lines_per_frame;
741 buf->line_length_pck = res->pixels_per_line;
742 buf->read_mode = res->bin_mode;
744 /* get the cropping and output resolution to ISP for this mode. */
745 ret = mt9m114_read_reg(client, MISENSOR_16BIT,
746 REG_H_START, ®_val);
749 buf->crop_horizontal_start = reg_val;
751 ret = mt9m114_read_reg(client, MISENSOR_16BIT,
752 REG_V_START, ®_val);
755 buf->crop_vertical_start = reg_val;
757 ret = mt9m114_read_reg(client, MISENSOR_16BIT,
758 REG_H_END, ®_val);
761 buf->crop_horizontal_end = reg_val;
763 ret = mt9m114_read_reg(client, MISENSOR_16BIT,
764 REG_V_END, ®_val);
767 buf->crop_vertical_end = reg_val;
769 ret = mt9m114_read_reg(client, MISENSOR_16BIT,
770 REG_WIDTH, ®_val);
773 buf->output_width = reg_val;
775 ret = mt9m114_read_reg(client, MISENSOR_16BIT,
776 REG_HEIGHT, ®_val);
779 buf->output_height = reg_val;
781 ret = mt9m114_read_reg(client, MISENSOR_16BIT,
782 REG_TIMING_HTS, ®_val);
785 buf->line_length_pck = reg_val;
787 ret = mt9m114_read_reg(client, MISENSOR_16BIT,
788 REG_TIMING_VTS, ®_val);
791 buf->frame_length_lines = reg_val;
793 buf->binning_factor_x = res->bin_factor_x ?
794 res->bin_factor_x : 1;
795 buf->binning_factor_y = res->bin_factor_y ?
796 res->bin_factor_y : 1;
800 static int mt9m114_get_fmt(struct v4l2_subdev *sd,
801 struct v4l2_subdev_pad_config *cfg,
802 struct v4l2_subdev_format *format)
804 struct v4l2_mbus_framefmt *fmt = &format->format;
809 fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
811 ret = mt9m114_res2size(sd, &width, &height);
815 fmt->height = height;
820 static int mt9m114_set_fmt(struct v4l2_subdev *sd,
821 struct v4l2_subdev_pad_config *cfg,
822 struct v4l2_subdev_format *format)
824 struct v4l2_mbus_framefmt *fmt = &format->format;
825 struct i2c_client *c = v4l2_get_subdevdata(sd);
826 struct mt9m114_device *dev = to_mt9m114_sensor(sd);
827 struct mt9m114_res_struct *res_index;
828 u32 width = fmt->width;
829 u32 height = fmt->height;
830 struct camera_mipi_info *mt9m114_info = NULL;
836 dev->first_exp = MT9M114_DEFAULT_FIRST_EXP;
838 mt9m114_info = v4l2_get_subdev_hostdata(sd);
839 if (mt9m114_info == NULL)
842 mt9m114_try_res(&width, &height);
843 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
847 res_index = mt9m114_to_res(width, height);
850 if (unlikely(!res_index)) {
855 switch (res_index->res) {
856 case MT9M114_RES_736P:
857 ret = mt9m114_write_reg_array(c, mt9m114_736P_init, NO_POLLING);
858 ret += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE,
859 MISENSOR_R_MODE_MASK, MISENSOR_NORMAL_SET);
861 case MT9M114_RES_864P:
862 ret = mt9m114_write_reg_array(c, mt9m114_864P_init, NO_POLLING);
863 ret += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE,
864 MISENSOR_R_MODE_MASK, MISENSOR_NORMAL_SET);
866 case MT9M114_RES_960P:
867 ret = mt9m114_write_reg_array(c, mt9m114_976P_init, NO_POLLING);
868 /* set sensor read_mode to Normal */
869 ret += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE,
870 MISENSOR_R_MODE_MASK, MISENSOR_NORMAL_SET);
873 v4l2_err(sd, "set resolution: %d failed!\n", res_index->res);
880 ret = mt9m114_write_reg_array(c, mt9m114_chgstat_reg, POST_POLLING);
884 if (mt9m114_set_suspend(sd))
887 if (dev->res != res_index->res) {
890 /* Switch to different size */
892 dev->nctx = 0x00; /* Set for context A */
895 * Context B is used for resolutions larger than 640x480
896 * Using YUV for Context B.
898 dev->nctx = 0x01; /* set for context B */
902 * Marked current sensor res as being "used"
904 * REVISIT: We don't need to use an "used" field on each mode
905 * list entry to know which mode is selected. If this
906 * information is really necessary, how about to use a single
907 * variable on sensor dev struct?
909 for (index = 0; index < N_RES; index++) {
910 if ((width == mt9m114_res[index].width) &&
911 (height == mt9m114_res[index].height)) {
912 mt9m114_res[index].used = true;
915 mt9m114_res[index].used = false;
918 ret = mt9m114_get_intg_factor(c, mt9m114_info,
919 &mt9m114_res[res_index->res]);
921 dev_err(&c->dev, "failed to get integration_factor\n");
925 * mt9m114 - we don't poll for context switch
926 * because it does not happen with streaming disabled.
928 dev->res = res_index->res;
931 fmt->height = height;
932 fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
936 /* TODO: Update to SOC functions, remove exposure and gain */
937 static int mt9m114_g_focal(struct v4l2_subdev *sd, s32 *val)
939 *val = (MT9M114_FOCAL_LENGTH_NUM << 16) | MT9M114_FOCAL_LENGTH_DEM;
943 static int mt9m114_g_fnumber(struct v4l2_subdev *sd, s32 *val)
945 /*const f number for mt9m114*/
946 *val = (MT9M114_F_NUMBER_DEFAULT_NUM << 16) | MT9M114_F_NUMBER_DEM;
950 static int mt9m114_g_fnumber_range(struct v4l2_subdev *sd, s32 *val)
952 *val = (MT9M114_F_NUMBER_DEFAULT_NUM << 24) |
953 (MT9M114_F_NUMBER_DEM << 16) |
954 (MT9M114_F_NUMBER_DEFAULT_NUM << 8) | MT9M114_F_NUMBER_DEM;
958 /* Horizontal flip the image. */
959 static int mt9m114_g_hflip(struct v4l2_subdev *sd, s32 *val)
961 struct i2c_client *c = v4l2_get_subdevdata(sd);
964 ret = mt9m114_read_reg(c, MISENSOR_16BIT,
965 (u32)MISENSOR_READ_MODE, &data);
968 *val = !!(data & MISENSOR_HFLIP_MASK);
973 static int mt9m114_g_vflip(struct v4l2_subdev *sd, s32 *val)
975 struct i2c_client *c = v4l2_get_subdevdata(sd);
979 ret = mt9m114_read_reg(c, MISENSOR_16BIT,
980 (u32)MISENSOR_READ_MODE, &data);
983 *val = !!(data & MISENSOR_VFLIP_MASK);
988 static long mt9m114_s_exposure(struct v4l2_subdev *sd,
989 struct atomisp_exposure *exposure)
991 struct i2c_client *client = v4l2_get_subdevdata(sd);
992 struct mt9m114_device *dev = to_mt9m114_sensor(sd);
994 unsigned int coarse_integration = 0;
995 unsigned int fine_integration = 0;
996 unsigned int FLines = 0;
997 unsigned int FrameLengthLines = 0; /* ExposureTime.FrameLengthLines; */
998 unsigned int AnalogGain, DigitalGain;
999 u32 AnalogGainToWrite = 0;
1000 u16 exposure_local[3];
1002 dev_dbg(&client->dev, "%s(0x%X 0x%X 0x%X)\n", __func__,
1003 exposure->integration_time[0], exposure->gain[0],
1006 coarse_integration = exposure->integration_time[0];
1007 /* fine_integration = ExposureTime.FineIntegrationTime; */
1008 /* FrameLengthLines = ExposureTime.FrameLengthLines; */
1009 FLines = mt9m114_res[dev->res].lines_per_frame;
1010 AnalogGain = exposure->gain[0];
1011 DigitalGain = exposure->gain[1];
1012 if (!dev->streamon) {
1013 /*Save the first exposure values while stream is off*/
1014 dev->first_exp = coarse_integration;
1015 dev->first_gain = AnalogGain;
1016 dev->first_diggain = DigitalGain;
1018 /* DigitalGain = 0x400 * (((u16) DigitalGain) >> 8) +
1019 ((unsigned int)(0x400 * (((u16) DigitalGain) & 0xFF)) >>8); */
1021 /* set frame length */
1022 if (FLines < coarse_integration + 6)
1023 FLines = coarse_integration + 6;
1024 if (FLines < FrameLengthLines)
1025 FLines = FrameLengthLines;
1026 ret = mt9m114_write_reg(client, MISENSOR_16BIT, 0x300A, FLines);
1028 v4l2_err(client, "%s: fail to set FLines\n", __func__);
1032 /* set coarse/fine integration */
1033 exposure_local[0] = REG_EXPO_COARSE;
1034 exposure_local[1] = (u16)coarse_integration;
1035 exposure_local[2] = (u16)fine_integration;
1036 /* 3A provide real exposure time.
1037 should not translate to any value here. */
1038 ret = mt9m114_write_reg(client, MISENSOR_16BIT,
1039 REG_EXPO_COARSE, (u16)(coarse_integration));
1041 v4l2_err(client, "%s: fail to set exposure time\n", __func__);
1046 // set analog/digital gain
1050 AnalogGainToWrite = 0x0;
1053 AnalogGainToWrite = 0x20;
1056 AnalogGainToWrite = 0x60;
1059 AnalogGainToWrite = 0xA0;
1062 AnalogGainToWrite = 0xE0;
1065 AnalogGainToWrite = 0x20;
1069 if (DigitalGain >= 16 || DigitalGain <= 1)
1071 /* AnalogGainToWrite =
1072 (u16)((DigitalGain << 12) | AnalogGainToWrite); */
1073 AnalogGainToWrite = (u16)((DigitalGain << 12) | (u16)AnalogGain);
1074 ret = mt9m114_write_reg(client, MISENSOR_16BIT,
1075 REG_GAIN, AnalogGainToWrite);
1077 v4l2_err(client, "%s: fail to set AnalogGainToWrite\n",
1085 static long mt9m114_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
1089 case ATOMISP_IOC_S_EXPOSURE:
1090 return mt9m114_s_exposure(sd, arg);
1098 /* This returns the exposure time being used. This should only be used
1099 for filling in EXIF data, not for actual image processing. */
1100 static int mt9m114_g_exposure(struct v4l2_subdev *sd, s32 *value)
1102 struct i2c_client *client = v4l2_get_subdevdata(sd);
1106 /* the fine integration time is currently not calculated */
1107 ret = mt9m114_read_reg(client, MISENSOR_16BIT,
1108 REG_EXPO_COARSE, &coarse);
1117 * This function will return the sensor supported max exposure zone number.
1118 * the sensor which supports max exposure zone number is 1.
1120 static int mt9m114_g_exposure_zone_num(struct v4l2_subdev *sd, s32 *val)
1128 * set exposure metering, average/center_weighted/spot/matrix.
1130 static int mt9m114_s_exposure_metering(struct v4l2_subdev *sd, s32 val)
1132 struct i2c_client *client = v4l2_get_subdevdata(sd);
1136 case V4L2_EXPOSURE_METERING_SPOT:
1137 ret = mt9m114_write_reg_array(client, mt9m114_exp_average,
1140 dev_err(&client->dev, "write exp_average reg err.\n");
1144 case V4L2_EXPOSURE_METERING_CENTER_WEIGHTED:
1146 ret = mt9m114_write_reg_array(client, mt9m114_exp_center,
1149 dev_err(&client->dev, "write exp_default reg err");
1158 * This function is for touch exposure feature.
1160 static int mt9m114_s_exposure_selection(struct v4l2_subdev *sd,
1161 struct v4l2_subdev_pad_config *cfg,
1162 struct v4l2_subdev_selection *sel)
1164 struct i2c_client *client = v4l2_get_subdevdata(sd);
1165 struct misensor_reg exp_reg;
1167 int grid_width, grid_height;
1168 int grid_left, grid_top, grid_right, grid_bottom;
1169 int win_left, win_top, win_right, win_bottom;
1173 if (sel->which != V4L2_SUBDEV_FORMAT_TRY &&
1174 sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
1177 grid_left = sel->r.left;
1178 grid_top = sel->r.top;
1179 grid_right = sel->r.left + sel->r.width - 1;
1180 grid_bottom = sel->r.top + sel->r.height - 1;
1182 ret = mt9m114_res2size(sd, &width, &height);
1186 grid_width = width / 5;
1187 grid_height = height / 5;
1189 if (grid_width && grid_height) {
1190 win_left = grid_left / grid_width;
1191 win_top = grid_top / grid_height;
1192 win_right = grid_right / grid_width;
1193 win_bottom = grid_bottom / grid_height;
1195 dev_err(&client->dev, "Incorrect exp grid.\n");
1199 win_left = clamp_t(int, win_left, 0, 4);
1200 win_top = clamp_t(int, win_top, 0, 4);
1201 win_right = clamp_t(int, win_right, 0, 4);
1202 win_bottom = clamp_t(int, win_bottom, 0, 4);
1204 ret = mt9m114_write_reg_array(client, mt9m114_exp_average, NO_POLLING);
1206 dev_err(&client->dev, "write exp_average reg err.\n");
1210 for (i = win_top; i <= win_bottom; i++) {
1211 for (j = win_left; j <= win_right; j++) {
1212 exp_reg = mt9m114_exp_win[i][j];
1214 ret = mt9m114_write_reg(client, exp_reg.length,
1215 exp_reg.reg, exp_reg.val);
1217 dev_err(&client->dev, "write exp_reg err.\n");
1227 static int mt9m114_g_bin_factor_x(struct v4l2_subdev *sd, s32 *val)
1229 struct mt9m114_device *dev = to_mt9m114_sensor(sd);
1231 *val = mt9m114_res[dev->res].bin_factor_x;
1236 static int mt9m114_g_bin_factor_y(struct v4l2_subdev *sd, s32 *val)
1238 struct mt9m114_device *dev = to_mt9m114_sensor(sd);
1240 *val = mt9m114_res[dev->res].bin_factor_y;
1245 static int mt9m114_s_ev(struct v4l2_subdev *sd, s32 val)
1247 struct i2c_client *c = v4l2_get_subdevdata(sd);
1251 /* EV value only support -2 to 2
1252 * 0: 0x37, 1:0x47, 2:0x57, -1:0x27, -2:0x17
1254 if (val < -2 || val > 2)
1257 dev_dbg(&c->dev, "%s val:%d luma:0x%x\n", __func__, val, luma);
1258 err = mt9m114_write_reg(c, MISENSOR_16BIT, 0x098E, 0xC87A);
1260 dev_err(&c->dev, "%s logic addr access error\n", __func__);
1263 err = mt9m114_write_reg(c, MISENSOR_8BIT, 0xC87A, (u32)luma);
1265 dev_err(&c->dev, "%s write target_average_luma failed\n",
1274 static int mt9m114_g_ev(struct v4l2_subdev *sd, s32 *val)
1276 struct i2c_client *c = v4l2_get_subdevdata(sd);
1280 err = mt9m114_write_reg(c, MISENSOR_16BIT, 0x098E, 0xC87A);
1282 dev_err(&c->dev, "%s logic addr access error\n", __func__);
1285 err = mt9m114_read_reg(c, MISENSOR_8BIT, 0xC87A, &luma);
1287 dev_err(&c->dev, "%s read target_average_luma failed\n",
1293 *val = (s32)luma - 2;
1294 dev_dbg(&c->dev, "%s val:%d\n", __func__, *val);
1300 * mt9m114 now can not support 3a_lock
1302 static int mt9m114_s_3a_lock(struct v4l2_subdev *sd, s32 val)
1308 static int mt9m114_g_3a_lock(struct v4l2_subdev *sd, s32 *val)
1311 return V4L2_LOCK_EXPOSURE | V4L2_LOCK_WHITE_BALANCE
1316 static int mt9m114_s_ctrl(struct v4l2_ctrl *ctrl)
1318 struct mt9m114_device *dev =
1319 container_of(ctrl->handler, struct mt9m114_device, ctrl_handler);
1320 struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
1324 case V4L2_CID_VFLIP:
1325 dev_dbg(&client->dev, "%s: CID_VFLIP:%d.\n",
1326 __func__, ctrl->val);
1327 ret = mt9m114_t_vflip(&dev->sd, ctrl->val);
1329 case V4L2_CID_HFLIP:
1330 dev_dbg(&client->dev, "%s: CID_HFLIP:%d.\n",
1331 __func__, ctrl->val);
1332 ret = mt9m114_t_hflip(&dev->sd, ctrl->val);
1335 case V4L2_CID_EXPOSURE_METERING:
1336 ret = mt9m114_s_exposure_metering(&dev->sd, ctrl->val);
1339 case V4L2_CID_EXPOSURE:
1340 ret = mt9m114_s_ev(&dev->sd, ctrl->val);
1342 case V4L2_CID_3A_LOCK:
1343 ret = mt9m114_s_3a_lock(&dev->sd, ctrl->val);
1351 static int mt9m114_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
1353 struct mt9m114_device *dev =
1354 container_of(ctrl->handler, struct mt9m114_device, ctrl_handler);
1358 case V4L2_CID_VFLIP:
1359 ret = mt9m114_g_vflip(&dev->sd, &ctrl->val);
1361 case V4L2_CID_HFLIP:
1362 ret = mt9m114_g_hflip(&dev->sd, &ctrl->val);
1364 case V4L2_CID_FOCAL_ABSOLUTE:
1365 ret = mt9m114_g_focal(&dev->sd, &ctrl->val);
1367 case V4L2_CID_FNUMBER_ABSOLUTE:
1368 ret = mt9m114_g_fnumber(&dev->sd, &ctrl->val);
1370 case V4L2_CID_FNUMBER_RANGE:
1371 ret = mt9m114_g_fnumber_range(&dev->sd, &ctrl->val);
1373 case V4L2_CID_EXPOSURE_ABSOLUTE:
1374 ret = mt9m114_g_exposure(&dev->sd, &ctrl->val);
1377 case V4L2_CID_EXPOSURE_ZONE_NUM:
1378 ret = mt9m114_g_exposure_zone_num(&dev->sd, &ctrl->val);
1381 case V4L2_CID_BIN_FACTOR_HORZ:
1382 ret = mt9m114_g_bin_factor_x(&dev->sd, &ctrl->val);
1384 case V4L2_CID_BIN_FACTOR_VERT:
1385 ret = mt9m114_g_bin_factor_y(&dev->sd, &ctrl->val);
1387 case V4L2_CID_EXPOSURE:
1388 ret = mt9m114_g_ev(&dev->sd, &ctrl->val);
1390 case V4L2_CID_3A_LOCK:
1391 ret = mt9m114_g_3a_lock(&dev->sd, &ctrl->val);
1400 static const struct v4l2_ctrl_ops ctrl_ops = {
1401 .s_ctrl = mt9m114_s_ctrl,
1402 .g_volatile_ctrl = mt9m114_g_volatile_ctrl
1405 static struct v4l2_ctrl_config mt9m114_controls[] = {
1408 .id = V4L2_CID_VFLIP,
1409 .name = "Image v-Flip",
1410 .type = V4L2_CTRL_TYPE_INTEGER,
1418 .id = V4L2_CID_HFLIP,
1419 .name = "Image h-Flip",
1420 .type = V4L2_CTRL_TYPE_INTEGER,
1428 .id = V4L2_CID_FOCAL_ABSOLUTE,
1429 .name = "focal length",
1430 .type = V4L2_CTRL_TYPE_INTEGER,
1431 .min = MT9M114_FOCAL_LENGTH_DEFAULT,
1432 .max = MT9M114_FOCAL_LENGTH_DEFAULT,
1434 .def = MT9M114_FOCAL_LENGTH_DEFAULT,
1439 .id = V4L2_CID_FNUMBER_ABSOLUTE,
1441 .type = V4L2_CTRL_TYPE_INTEGER,
1442 .min = MT9M114_F_NUMBER_DEFAULT,
1443 .max = MT9M114_F_NUMBER_DEFAULT,
1445 .def = MT9M114_F_NUMBER_DEFAULT,
1450 .id = V4L2_CID_FNUMBER_RANGE,
1451 .name = "f-number range",
1452 .type = V4L2_CTRL_TYPE_INTEGER,
1453 .min = MT9M114_F_NUMBER_RANGE,
1454 .max = MT9M114_F_NUMBER_RANGE,
1456 .def = MT9M114_F_NUMBER_RANGE,
1461 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
1463 .type = V4L2_CTRL_TYPE_INTEGER,
1473 .id = V4L2_CID_EXPOSURE_ZONE_NUM,
1474 .name = "one-time exposure zone number",
1475 .type = V4L2_CTRL_TYPE_INTEGER,
1484 .id = V4L2_CID_EXPOSURE_METERING,
1486 .type = V4L2_CTRL_TYPE_MENU,
1496 .id = V4L2_CID_BIN_FACTOR_HORZ,
1497 .name = "horizontal binning factor",
1498 .type = V4L2_CTRL_TYPE_INTEGER,
1500 .max = MT9M114_BIN_FACTOR_MAX,
1507 .id = V4L2_CID_BIN_FACTOR_VERT,
1508 .name = "vertical binning factor",
1509 .type = V4L2_CTRL_TYPE_INTEGER,
1511 .max = MT9M114_BIN_FACTOR_MAX,
1518 .id = V4L2_CID_EXPOSURE,
1519 .name = "exposure biasx",
1520 .type = V4L2_CTRL_TYPE_INTEGER,
1529 .id = V4L2_CID_3A_LOCK,
1531 .type = V4L2_CTRL_TYPE_BITMASK,
1533 .max = V4L2_LOCK_EXPOSURE | V4L2_LOCK_WHITE_BALANCE | V4L2_LOCK_FOCUS,
1540 static int mt9m114_detect(struct mt9m114_device *dev, struct i2c_client *client)
1542 struct i2c_adapter *adapter = client->adapter;
1545 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
1546 dev_err(&client->dev, "%s: i2c error", __func__);
1549 mt9m114_read_reg(client, MISENSOR_16BIT, (u32)MT9M114_PID, &retvalue);
1550 dev->real_model_id = retvalue;
1552 if (retvalue != MT9M114_MOD_ID) {
1553 dev_err(&client->dev, "%s: failed: client->addr = %x\n",
1554 __func__, client->addr);
1562 mt9m114_s_config(struct v4l2_subdev *sd, int irq, void *platform_data)
1564 struct mt9m114_device *dev = to_mt9m114_sensor(sd);
1565 struct i2c_client *client = v4l2_get_subdevdata(sd);
1568 if (NULL == platform_data)
1571 dev->platform_data =
1572 (struct camera_sensor_platform_data *)platform_data;
1576 v4l2_err(client, "mt9m114 power-up err");
1580 /* config & detect sensor */
1581 ret = mt9m114_detect(dev, client);
1583 v4l2_err(client, "mt9m114_detect err s_config.\n");
1587 ret = dev->platform_data->csi_cfg(sd, 1);
1591 ret = mt9m114_set_suspend(sd);
1593 v4l2_err(client, "mt9m114 suspend err");
1597 ret = power_down(sd);
1599 v4l2_err(client, "mt9m114 power down err");
1606 dev->platform_data->csi_cfg(sd, 0);
1609 dev_err(&client->dev, "sensor power-gating failed\n");
1613 /* Horizontal flip the image. */
1614 static int mt9m114_t_hflip(struct v4l2_subdev *sd, int value)
1616 struct i2c_client *c = v4l2_get_subdevdata(sd);
1617 struct mt9m114_device *dev = to_mt9m114_sensor(sd);
1619 /* set for direct mode */
1620 err = mt9m114_write_reg(c, MISENSOR_16BIT, 0x098E, 0xC850);
1622 /* enable H flip ctx A */
1623 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC850, 0x01, 0x01);
1624 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC851, 0x01, 0x01);
1626 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC888, 0x01, 0x01);
1627 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC889, 0x01, 0x01);
1629 err += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE,
1630 MISENSOR_HFLIP_MASK, MISENSOR_FLIP_EN);
1632 dev->bpat = MT9M114_BPAT_GRGRBGBG;
1634 /* disable H flip ctx A */
1635 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC850, 0x01, 0x00);
1636 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC851, 0x01, 0x00);
1638 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC888, 0x01, 0x00);
1639 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC889, 0x01, 0x00);
1641 err += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE,
1642 MISENSOR_HFLIP_MASK, MISENSOR_FLIP_DIS);
1644 dev->bpat = MT9M114_BPAT_BGBGGRGR;
1647 err += mt9m114_write_reg(c, MISENSOR_8BIT, 0x8404, 0x06);
1653 /* Vertically flip the image */
1654 static int mt9m114_t_vflip(struct v4l2_subdev *sd, int value)
1656 struct i2c_client *c = v4l2_get_subdevdata(sd);
1658 /* set for direct mode */
1659 err = mt9m114_write_reg(c, MISENSOR_16BIT, 0x098E, 0xC850);
1661 /* enable H flip - ctx A */
1662 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC850, 0x02, 0x01);
1663 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC851, 0x02, 0x01);
1665 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC888, 0x02, 0x01);
1666 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC889, 0x02, 0x01);
1668 err += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE,
1669 MISENSOR_VFLIP_MASK, MISENSOR_FLIP_EN);
1671 /* disable H flip - ctx A */
1672 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC850, 0x02, 0x00);
1673 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC851, 0x02, 0x00);
1675 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC888, 0x02, 0x00);
1676 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC889, 0x02, 0x00);
1678 err += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE,
1679 MISENSOR_VFLIP_MASK, MISENSOR_FLIP_DIS);
1682 err += mt9m114_write_reg(c, MISENSOR_8BIT, 0x8404, 0x06);
1687 static int mt9m114_s_parm(struct v4l2_subdev *sd,
1688 struct v4l2_streamparm *param)
1693 static int mt9m114_g_frame_interval(struct v4l2_subdev *sd,
1694 struct v4l2_subdev_frame_interval *interval)
1696 struct mt9m114_device *dev = to_mt9m114_sensor(sd);
1698 interval->interval.numerator = 1;
1699 interval->interval.denominator = mt9m114_res[dev->res].fps;
1704 static int mt9m114_s_stream(struct v4l2_subdev *sd, int enable)
1707 struct i2c_client *c = v4l2_get_subdevdata(sd);
1708 struct mt9m114_device *dev = to_mt9m114_sensor(sd);
1709 struct atomisp_exposure exposure;
1712 ret = mt9m114_write_reg_array(c, mt9m114_chgstat_reg,
1717 if (dev->first_exp > MT9M114_MAX_FIRST_EXP) {
1718 exposure.integration_time[0] = dev->first_exp;
1719 exposure.gain[0] = dev->first_gain;
1720 exposure.gain[1] = dev->first_diggain;
1721 mt9m114_s_exposure(sd, &exposure);
1727 ret = mt9m114_set_suspend(sd);
1733 static int mt9m114_enum_mbus_code(struct v4l2_subdev *sd,
1734 struct v4l2_subdev_pad_config *cfg,
1735 struct v4l2_subdev_mbus_code_enum *code)
1739 code->code = MEDIA_BUS_FMT_SGRBG10_1X10;
1744 static int mt9m114_enum_frame_size(struct v4l2_subdev *sd,
1745 struct v4l2_subdev_pad_config *cfg,
1746 struct v4l2_subdev_frame_size_enum *fse)
1749 unsigned int index = fse->index;
1754 fse->min_width = mt9m114_res[index].width;
1755 fse->min_height = mt9m114_res[index].height;
1756 fse->max_width = mt9m114_res[index].width;
1757 fse->max_height = mt9m114_res[index].height;
1762 static int mt9m114_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
1765 struct mt9m114_device *snr = to_mt9m114_sensor(sd);
1770 for (index = 0; index < N_RES; index++) {
1771 if (mt9m114_res[index].res == snr->res)
1778 *frames = mt9m114_res[index].skip_frames;
1783 static const struct v4l2_subdev_video_ops mt9m114_video_ops = {
1784 .s_parm = mt9m114_s_parm,
1785 .s_stream = mt9m114_s_stream,
1786 .g_frame_interval = mt9m114_g_frame_interval,
1789 static const struct v4l2_subdev_sensor_ops mt9m114_sensor_ops = {
1790 .g_skip_frames = mt9m114_g_skip_frames,
1793 static const struct v4l2_subdev_core_ops mt9m114_core_ops = {
1794 .s_power = mt9m114_s_power,
1795 .ioctl = mt9m114_ioctl,
1798 /* REVISIT: Do we need pad operations? */
1799 static const struct v4l2_subdev_pad_ops mt9m114_pad_ops = {
1800 .enum_mbus_code = mt9m114_enum_mbus_code,
1801 .enum_frame_size = mt9m114_enum_frame_size,
1802 .get_fmt = mt9m114_get_fmt,
1803 .set_fmt = mt9m114_set_fmt,
1805 .set_selection = mt9m114_s_exposure_selection,
1809 static const struct v4l2_subdev_ops mt9m114_ops = {
1810 .core = &mt9m114_core_ops,
1811 .video = &mt9m114_video_ops,
1812 .pad = &mt9m114_pad_ops,
1813 .sensor = &mt9m114_sensor_ops,
1816 static const struct media_entity_operations mt9m114_entity_ops = {
1820 static int mt9m114_remove(struct i2c_client *client)
1822 struct mt9m114_device *dev;
1823 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1825 dev = container_of(sd, struct mt9m114_device, sd);
1826 dev->platform_data->csi_cfg(sd, 0);
1827 v4l2_device_unregister_subdev(sd);
1828 media_entity_cleanup(&dev->sd.entity);
1829 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1834 static int mt9m114_probe(struct i2c_client *client)
1836 struct mt9m114_device *dev;
1841 /* Setup sensor configuration structure */
1842 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1846 v4l2_i2c_subdev_init(&dev->sd, client, &mt9m114_ops);
1847 pdata = gmin_camera_platform_data(&dev->sd,
1848 ATOMISP_INPUT_FORMAT_RAW_10,
1849 atomisp_bayer_order_grbg);
1851 ret = mt9m114_s_config(&dev->sd, client->irq, pdata);
1852 if (!pdata || ret) {
1853 v4l2_device_unregister_subdev(&dev->sd);
1858 ret = atomisp_register_i2c_module(&dev->sd, pdata, RAW_CAMERA);
1860 v4l2_device_unregister_subdev(&dev->sd);
1862 /* Coverity CID 298095 - return on error */
1866 /*TODO add format code here*/
1867 dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1868 dev->pad.flags = MEDIA_PAD_FL_SOURCE;
1869 dev->format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
1870 dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1873 v4l2_ctrl_handler_init(&dev->ctrl_handler,
1874 ARRAY_SIZE(mt9m114_controls));
1876 mt9m114_remove(client);
1880 for (i = 0; i < ARRAY_SIZE(mt9m114_controls); i++)
1881 v4l2_ctrl_new_custom(&dev->ctrl_handler, &mt9m114_controls[i],
1884 if (dev->ctrl_handler.error) {
1885 mt9m114_remove(client);
1886 return dev->ctrl_handler.error;
1889 /* Use same lock for controls as for everything else. */
1890 dev->ctrl_handler.lock = &dev->input_lock;
1891 dev->sd.ctrl_handler = &dev->ctrl_handler;
1893 /* REVISIT: Do we need media controller? */
1894 ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad);
1896 mt9m114_remove(client);
1902 static const struct acpi_device_id mt9m114_acpi_match[] = {
1907 MODULE_DEVICE_TABLE(acpi, mt9m114_acpi_match);
1909 static struct i2c_driver mt9m114_driver = {
1912 .acpi_match_table = mt9m114_acpi_match,
1914 .probe_new = mt9m114_probe,
1915 .remove = mt9m114_remove,
1917 module_i2c_driver(mt9m114_driver);
1919 MODULE_AUTHOR("Shuguang Gong <Shuguang.gong@intel.com>");
1920 MODULE_LICENSE("GPL");