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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 #include <linux/module.h>
23 #include <linux/types.h>
24 #include <linux/kernel.h>
26 #include <linux/string.h>
27 #include <linux/errno.h>
28 #include <linux/init.h>
29 #include <linux/kmod.h>
30 #include <linux/device.h>
32 #include <linux/slab.h>
33 #include <linux/delay.h>
34 #include <linux/i2c.h>
35 #include <linux/gpio.h>
36 #include <linux/acpi.h>
37 #include "../include/linux/atomisp_gmin_platform.h"
38 #include <media/v4l2-device.h>
42 #define to_mt9m114_sensor(sd) container_of(sd, struct mt9m114_device, sd)
45 * TODO: use debug parameter to actually define when debug messages should
50 module_param(debug, int, 0644);
51 MODULE_PARM_DESC(debug, "Debug level (0-1)");
53 static int mt9m114_t_vflip(struct v4l2_subdev *sd, int value);
54 static int mt9m114_t_hflip(struct v4l2_subdev *sd, int value);
55 static int mt9m114_wait_state(struct i2c_client *client, int timeout);
58 mt9m114_read_reg(struct i2c_client *client, u16 data_length, u32 reg, u32 *val)
61 struct i2c_msg msg[2];
62 unsigned char data[4];
64 if (!client->adapter) {
65 v4l2_err(client, "%s error, no client->adapter\n", __func__);
69 if (data_length != MISENSOR_8BIT && data_length != MISENSOR_16BIT
70 && data_length != MISENSOR_32BIT) {
71 v4l2_err(client, "%s error, invalid data length\n", __func__);
75 msg[0].addr = client->addr;
77 msg[0].len = MSG_LEN_OFFSET;
80 /* high byte goes out first */
81 data[0] = (u16) (reg >> 8);
82 data[1] = (u16) (reg & 0xff);
84 msg[1].addr = client->addr;
85 msg[1].len = data_length;
86 msg[1].flags = I2C_M_RD;
89 err = i2c_transfer(client->adapter, msg, 2);
93 /* high byte comes first */
94 if (data_length == MISENSOR_8BIT)
96 else if (data_length == MISENSOR_16BIT)
97 *val = data[1] + (data[0] << 8);
99 *val = data[3] + (data[2] << 8) +
100 (data[1] << 16) + (data[0] << 24);
105 dev_err(&client->dev, "read from offset 0x%x error %d", reg, err);
110 mt9m114_write_reg(struct i2c_client *client, u16 data_length, u16 reg, u32 val)
114 unsigned char data[6] = {0};
118 if (!client->adapter) {
119 v4l2_err(client, "%s error, no client->adapter\n", __func__);
123 if (data_length != MISENSOR_8BIT && data_length != MISENSOR_16BIT
124 && data_length != MISENSOR_32BIT) {
125 v4l2_err(client, "%s error, invalid data_length\n", __func__);
129 memset(&msg, 0, sizeof(msg));
132 msg.addr = client->addr;
134 msg.len = 2 + data_length;
137 /* high byte goes out first */
139 *wreg = cpu_to_be16(reg);
141 if (data_length == MISENSOR_8BIT) {
143 } else if (data_length == MISENSOR_16BIT) {
144 u16 *wdata = (u16 *)&data[2];
145 *wdata = be16_to_cpu((u16)val);
148 u32 *wdata = (u32 *)&data[2];
149 *wdata = be32_to_cpu(val);
152 num_msg = i2c_transfer(client->adapter, &msg, 1);
155 * HACK: Need some delay here for Rev 2 sensors otherwise some
156 * registers do not seem to load correctly.
163 dev_err(&client->dev, "write error: wrote 0x%x to offset 0x%x error %d",
165 if (retry <= I2C_RETRY_COUNT) {
166 dev_dbg(&client->dev, "retrying... %d", retry);
176 * misensor_rmw_reg - Read/Modify/Write a value to a register in the sensor
178 * @client: i2c driver client structure
179 * @data_length: 8/16/32-bits length
180 * @reg: register address
181 * @mask: masked out bits
184 * Read/modify/write a value to a register in the sensor device.
185 * Returns zero if successful, or non-zero otherwise.
188 misensor_rmw_reg(struct i2c_client *client, u16 data_length, u16 reg,
194 /* Exit when no mask */
198 /* @mask must not exceed data length */
199 switch (data_length) {
211 /* Wrong @data_length */
215 err = mt9m114_read_reg(client, data_length, reg, &val);
217 v4l2_err(client, "misensor_rmw_reg error exit, read failed\n");
224 * Perform the OR function if the @set exists.
225 * Shift @set value to target bit location. @set should set only
226 * bits included in @mask.
228 * REVISIT: This function expects @set to be non-shifted. Its shift
229 * value is then defined to be equal to mask's LSB position.
230 * How about to inform values in their right offset position and avoid
231 * this unneeded shift operation?
233 set <<= ffs(mask) - 1;
236 err = mt9m114_write_reg(client, data_length, reg, val);
238 v4l2_err(client, "misensor_rmw_reg error exit, write failed\n");
246 static int __mt9m114_flush_reg_array(struct i2c_client *client,
247 struct mt9m114_write_ctrl *ctrl)
250 const int num_msg = 1;
254 if (ctrl->index == 0)
258 msg.addr = client->addr;
260 msg.len = 2 + ctrl->index;
261 ctrl->buffer.addr = cpu_to_be16(ctrl->buffer.addr);
262 msg.buf = (u8 *)&ctrl->buffer;
264 ret = i2c_transfer(client->adapter, &msg, num_msg);
265 if (ret != num_msg) {
266 if (++retry <= I2C_RETRY_COUNT) {
267 dev_dbg(&client->dev, "retrying... %d\n", retry);
271 dev_err(&client->dev, "%s: i2c transfer error\n", __func__);
278 * REVISIT: Previously we had a delay after writing data to sensor.
279 * But it was removed as our tests have shown it is not necessary
286 static int __mt9m114_buf_reg_array(struct i2c_client *client,
287 struct mt9m114_write_ctrl *ctrl,
288 const struct misensor_reg *next)
294 /* Insufficient buffer? Let's flush and get more free space. */
295 if (ctrl->index + next->length >= MT9M114_MAX_WRITE_BUF_SIZE) {
296 err = __mt9m114_flush_reg_array(client, ctrl);
301 switch (next->length) {
303 ctrl->buffer.data[ctrl->index] = (u8)next->val;
306 data16 = (u16 *)&ctrl->buffer.data[ctrl->index];
307 *data16 = cpu_to_be16((u16)next->val);
310 data32 = (u32 *)&ctrl->buffer.data[ctrl->index];
311 *data32 = cpu_to_be32(next->val);
317 /* When first item is added, we need to store its starting address */
318 if (ctrl->index == 0)
319 ctrl->buffer.addr = next->reg;
321 ctrl->index += next->length;
327 __mt9m114_write_reg_is_consecutive(struct i2c_client *client,
328 struct mt9m114_write_ctrl *ctrl,
329 const struct misensor_reg *next)
331 if (ctrl->index == 0)
334 return ctrl->buffer.addr + ctrl->index == next->reg;
338 * mt9m114_write_reg_array - Initializes a list of mt9m114 registers
339 * @client: i2c driver client structure
340 * @reglist: list of registers to be written
341 * @poll: completion polling requirement
342 * This function initializes a list of registers. When consecutive addresses
343 * are found in a row on the list, this function creates a buffer and sends
344 * consecutive data in a single i2c_transfer().
346 * __mt9m114_flush_reg_array, __mt9m114_buf_reg_array() and
347 * __mt9m114_write_reg_is_consecutive() are internal functions to
348 * mt9m114_write_reg_array() and should be not used anywhere else.
351 static int mt9m114_write_reg_array(struct i2c_client *client,
352 const struct misensor_reg *reglist,
355 const struct misensor_reg *next = reglist;
356 struct mt9m114_write_ctrl ctrl;
359 if (poll == PRE_POLLING) {
360 err = mt9m114_wait_state(client, MT9M114_WAIT_STAT_TIMEOUT);
366 for (; next->length != MISENSOR_TOK_TERM; next++) {
367 switch (next->length & MISENSOR_TOK_MASK) {
368 case MISENSOR_TOK_DELAY:
369 err = __mt9m114_flush_reg_array(client, &ctrl);
374 case MISENSOR_TOK_RMW:
375 err = __mt9m114_flush_reg_array(client, &ctrl);
376 err |= misensor_rmw_reg(client,
379 next->reg, next->val,
382 dev_err(&client->dev, "%s read err. aborted\n",
389 * If next address is not consecutive, data needs to be
390 * flushed before proceed.
392 if (!__mt9m114_write_reg_is_consecutive(client, &ctrl,
394 err = __mt9m114_flush_reg_array(client, &ctrl);
398 err = __mt9m114_buf_reg_array(client, &ctrl, next);
400 v4l2_err(client, "%s: write error, aborted\n",
408 err = __mt9m114_flush_reg_array(client, &ctrl);
412 if (poll == POST_POLLING)
413 return mt9m114_wait_state(client, MT9M114_WAIT_STAT_TIMEOUT);
418 static int mt9m114_wait_state(struct i2c_client *client, int timeout)
423 while (timeout-- > 0) {
424 ret = mt9m114_read_reg(client, MISENSOR_16BIT, 0x0080, &val);
427 if ((val & 0x2) == 0)
436 static int mt9m114_set_suspend(struct v4l2_subdev *sd)
438 struct i2c_client *client = v4l2_get_subdevdata(sd);
439 return mt9m114_write_reg_array(client,
440 mt9m114_standby_reg, POST_POLLING);
443 static int mt9m114_init_common(struct v4l2_subdev *sd)
445 struct i2c_client *client = v4l2_get_subdevdata(sd);
447 return mt9m114_write_reg_array(client, mt9m114_common, PRE_POLLING);
450 static int power_ctrl(struct v4l2_subdev *sd, bool flag)
453 struct mt9m114_device *dev = to_mt9m114_sensor(sd);
455 if (!dev || !dev->platform_data)
458 /* Non-gmin platforms use the legacy callback */
459 if (dev->platform_data->power_ctrl)
460 return dev->platform_data->power_ctrl(sd, flag);
463 ret = dev->platform_data->v2p8_ctrl(sd, 1);
465 ret = dev->platform_data->v1p8_ctrl(sd, 1);
467 ret = dev->platform_data->v2p8_ctrl(sd, 0);
470 ret = dev->platform_data->v2p8_ctrl(sd, 0);
471 ret = dev->platform_data->v1p8_ctrl(sd, 0);
476 static int gpio_ctrl(struct v4l2_subdev *sd, bool flag)
479 struct mt9m114_device *dev = to_mt9m114_sensor(sd);
481 if (!dev || !dev->platform_data)
484 /* Non-gmin platforms use the legacy callback */
485 if (dev->platform_data->gpio_ctrl)
486 return dev->platform_data->gpio_ctrl(sd, flag);
488 /* Note: current modules wire only one GPIO signal (RESET#),
489 * but the schematic wires up two to the connector. BIOS
490 * versions have been unfortunately inconsistent with which
491 * ACPI index RESET# is on, so hit both */
494 ret = dev->platform_data->gpio0_ctrl(sd, 0);
495 ret = dev->platform_data->gpio1_ctrl(sd, 0);
497 ret |= dev->platform_data->gpio0_ctrl(sd, 1);
498 ret |= dev->platform_data->gpio1_ctrl(sd, 1);
500 ret = dev->platform_data->gpio0_ctrl(sd, 0);
501 ret = dev->platform_data->gpio1_ctrl(sd, 0);
506 static int power_up(struct v4l2_subdev *sd)
508 struct mt9m114_device *dev = to_mt9m114_sensor(sd);
509 struct i2c_client *client = v4l2_get_subdevdata(sd);
512 if (NULL == dev->platform_data) {
513 dev_err(&client->dev, "no camera_sensor_platform_data");
518 ret = power_ctrl(sd, 1);
522 /* flis clock control */
523 ret = dev->platform_data->flisclk_ctrl(sd, 1);
528 ret = gpio_ctrl(sd, 1);
530 dev_err(&client->dev, "gpio failed 1\n");
532 * according to DS, 44ms is needed between power up and first i2c
540 dev->platform_data->flisclk_ctrl(sd, 0);
543 dev_err(&client->dev, "sensor power-up failed\n");
548 static int power_down(struct v4l2_subdev *sd)
550 struct mt9m114_device *dev = to_mt9m114_sensor(sd);
551 struct i2c_client *client = v4l2_get_subdevdata(sd);
554 if (NULL == dev->platform_data) {
555 dev_err(&client->dev, "no camera_sensor_platform_data");
559 ret = dev->platform_data->flisclk_ctrl(sd, 0);
561 dev_err(&client->dev, "flisclk failed\n");
564 ret = gpio_ctrl(sd, 0);
566 dev_err(&client->dev, "gpio failed 1\n");
569 ret = power_ctrl(sd, 0);
571 dev_err(&client->dev, "vprog failed.\n");
573 /*according to DS, 20ms is needed after power down*/
579 static int mt9m114_s_power(struct v4l2_subdev *sd, int power)
582 return power_down(sd);
587 return mt9m114_init_common(sd);
592 * distance - calculate the distance
597 * Get the gap between resolution and w/h.
598 * res->width/height smaller than w/h wouldn't be considered.
599 * Returns the value of gap or -1 if fail.
601 #define LARGEST_ALLOWED_RATIO_MISMATCH 600
602 static int distance(struct mt9m114_res_struct const *res, u32 w, u32 h)
604 unsigned int w_ratio;
605 unsigned int h_ratio;
610 w_ratio = (res->width << 13) / w;
613 h_ratio = (res->height << 13) / h;
616 match = abs(((w_ratio << 13) / h_ratio) - 8192);
618 if ((w_ratio < 8192) || (h_ratio < 8192) ||
619 (match > LARGEST_ALLOWED_RATIO_MISMATCH))
622 return w_ratio + h_ratio;
625 /* Return the nearest higher resolution index */
626 static int nearest_resolution_index(int w, int h)
631 int min_dist = INT_MAX;
632 const struct mt9m114_res_struct *tmp_res = NULL;
634 for (i = 0; i < ARRAY_SIZE(mt9m114_res); i++) {
635 tmp_res = &mt9m114_res[i];
636 dist = distance(tmp_res, w, h);
639 if (dist < min_dist) {
648 static int mt9m114_try_res(u32 *w, u32 *h)
652 if ((*w > MT9M114_RES_960P_SIZE_H)
653 || (*h > MT9M114_RES_960P_SIZE_V)) {
654 *w = MT9M114_RES_960P_SIZE_H;
655 *h = MT9M114_RES_960P_SIZE_V;
657 idx = nearest_resolution_index(*w, *h);
660 * nearest_resolution_index() doesn't return smaller
661 * resolutions. If it fails, it means the requested
662 * resolution is higher than wecan support. Fallback
663 * to highest possible resolution in this case.
666 idx = ARRAY_SIZE(mt9m114_res) - 1;
668 *w = mt9m114_res[idx].width;
669 *h = mt9m114_res[idx].height;
675 static struct mt9m114_res_struct *mt9m114_to_res(u32 w, u32 h)
679 for (index = 0; index < N_RES; index++) {
680 if ((mt9m114_res[index].width == w) &&
681 (mt9m114_res[index].height == h))
689 return &mt9m114_res[index];
692 static int mt9m114_res2size(struct v4l2_subdev *sd, int *h_size, int *v_size)
694 struct mt9m114_device *dev = to_mt9m114_sensor(sd);
695 unsigned short hsize;
696 unsigned short vsize;
699 case MT9M114_RES_736P:
700 hsize = MT9M114_RES_736P_SIZE_H;
701 vsize = MT9M114_RES_736P_SIZE_V;
703 case MT9M114_RES_864P:
704 hsize = MT9M114_RES_864P_SIZE_H;
705 vsize = MT9M114_RES_864P_SIZE_V;
707 case MT9M114_RES_960P:
708 hsize = MT9M114_RES_960P_SIZE_H;
709 vsize = MT9M114_RES_960P_SIZE_V;
712 v4l2_err(sd, "%s: Resolution 0x%08x unknown\n", __func__,
725 static int mt9m114_get_intg_factor(struct i2c_client *client,
726 struct camera_mipi_info *info,
727 const struct mt9m114_res_struct *res)
729 struct atomisp_sensor_mode_data *buf = &info->data;
736 ret = mt9m114_read_reg(client, MISENSOR_32BIT,
737 REG_PIXEL_CLK, ®_val);
740 buf->vt_pix_clk_freq_mhz = reg_val;
742 /* get integration time */
743 buf->coarse_integration_time_min = MT9M114_COARSE_INTG_TIME_MIN;
744 buf->coarse_integration_time_max_margin =
745 MT9M114_COARSE_INTG_TIME_MAX_MARGIN;
747 buf->fine_integration_time_min = MT9M114_FINE_INTG_TIME_MIN;
748 buf->fine_integration_time_max_margin =
749 MT9M114_FINE_INTG_TIME_MAX_MARGIN;
751 buf->fine_integration_time_def = MT9M114_FINE_INTG_TIME_MIN;
753 buf->frame_length_lines = res->lines_per_frame;
754 buf->line_length_pck = res->pixels_per_line;
755 buf->read_mode = res->bin_mode;
757 /* get the cropping and output resolution to ISP for this mode. */
758 ret = mt9m114_read_reg(client, MISENSOR_16BIT,
759 REG_H_START, ®_val);
762 buf->crop_horizontal_start = reg_val;
764 ret = mt9m114_read_reg(client, MISENSOR_16BIT,
765 REG_V_START, ®_val);
768 buf->crop_vertical_start = reg_val;
770 ret = mt9m114_read_reg(client, MISENSOR_16BIT,
771 REG_H_END, ®_val);
774 buf->crop_horizontal_end = reg_val;
776 ret = mt9m114_read_reg(client, MISENSOR_16BIT,
777 REG_V_END, ®_val);
780 buf->crop_vertical_end = reg_val;
782 ret = mt9m114_read_reg(client, MISENSOR_16BIT,
783 REG_WIDTH, ®_val);
786 buf->output_width = reg_val;
788 ret = mt9m114_read_reg(client, MISENSOR_16BIT,
789 REG_HEIGHT, ®_val);
792 buf->output_height = reg_val;
794 ret = mt9m114_read_reg(client, MISENSOR_16BIT,
795 REG_TIMING_HTS, ®_val);
798 buf->line_length_pck = reg_val;
800 ret = mt9m114_read_reg(client, MISENSOR_16BIT,
801 REG_TIMING_VTS, ®_val);
804 buf->frame_length_lines = reg_val;
806 buf->binning_factor_x = res->bin_factor_x ?
807 res->bin_factor_x : 1;
808 buf->binning_factor_y = res->bin_factor_y ?
809 res->bin_factor_y : 1;
813 static int mt9m114_get_fmt(struct v4l2_subdev *sd,
814 struct v4l2_subdev_pad_config *cfg,
815 struct v4l2_subdev_format *format)
817 struct v4l2_mbus_framefmt *fmt = &format->format;
822 fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
824 ret = mt9m114_res2size(sd, &width, &height);
828 fmt->height = height;
833 static int mt9m114_set_fmt(struct v4l2_subdev *sd,
834 struct v4l2_subdev_pad_config *cfg,
835 struct v4l2_subdev_format *format)
837 struct v4l2_mbus_framefmt *fmt = &format->format;
838 struct i2c_client *c = v4l2_get_subdevdata(sd);
839 struct mt9m114_device *dev = to_mt9m114_sensor(sd);
840 struct mt9m114_res_struct *res_index;
841 u32 width = fmt->width;
842 u32 height = fmt->height;
843 struct camera_mipi_info *mt9m114_info = NULL;
849 dev->first_exp = MT9M114_DEFAULT_FIRST_EXP;
851 mt9m114_info = v4l2_get_subdev_hostdata(sd);
852 if (mt9m114_info == NULL)
855 mt9m114_try_res(&width, &height);
856 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
860 res_index = mt9m114_to_res(width, height);
863 if (unlikely(!res_index)) {
868 switch (res_index->res) {
869 case MT9M114_RES_736P:
870 ret = mt9m114_write_reg_array(c, mt9m114_736P_init, NO_POLLING);
871 ret += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE,
872 MISENSOR_R_MODE_MASK, MISENSOR_NORMAL_SET);
874 case MT9M114_RES_864P:
875 ret = mt9m114_write_reg_array(c, mt9m114_864P_init, NO_POLLING);
876 ret += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE,
877 MISENSOR_R_MODE_MASK, MISENSOR_NORMAL_SET);
879 case MT9M114_RES_960P:
880 ret = mt9m114_write_reg_array(c, mt9m114_976P_init, NO_POLLING);
881 /* set sensor read_mode to Normal */
882 ret += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE,
883 MISENSOR_R_MODE_MASK, MISENSOR_NORMAL_SET);
886 v4l2_err(sd, "set resolution: %d failed!\n", res_index->res);
893 ret = mt9m114_write_reg_array(c, mt9m114_chgstat_reg, POST_POLLING);
897 if (mt9m114_set_suspend(sd))
900 if (dev->res != res_index->res) {
903 /* Switch to different size */
905 dev->nctx = 0x00; /* Set for context A */
908 * Context B is used for resolutions larger than 640x480
909 * Using YUV for Context B.
911 dev->nctx = 0x01; /* set for context B */
915 * Marked current sensor res as being "used"
917 * REVISIT: We don't need to use an "used" field on each mode
918 * list entry to know which mode is selected. If this
919 * information is really necessary, how about to use a single
920 * variable on sensor dev struct?
922 for (index = 0; index < N_RES; index++) {
923 if ((width == mt9m114_res[index].width) &&
924 (height == mt9m114_res[index].height)) {
925 mt9m114_res[index].used = true;
928 mt9m114_res[index].used = false;
931 ret = mt9m114_get_intg_factor(c, mt9m114_info,
932 &mt9m114_res[res_index->res]);
934 dev_err(&c->dev, "failed to get integration_factor\n");
938 * mt9m114 - we don't poll for context switch
939 * because it does not happen with streaming disabled.
941 dev->res = res_index->res;
944 fmt->height = height;
945 fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
949 /* TODO: Update to SOC functions, remove exposure and gain */
950 static int mt9m114_g_focal(struct v4l2_subdev *sd, s32 *val)
952 *val = (MT9M114_FOCAL_LENGTH_NUM << 16) | MT9M114_FOCAL_LENGTH_DEM;
956 static int mt9m114_g_fnumber(struct v4l2_subdev *sd, s32 *val)
958 /*const f number for mt9m114*/
959 *val = (MT9M114_F_NUMBER_DEFAULT_NUM << 16) | MT9M114_F_NUMBER_DEM;
963 static int mt9m114_g_fnumber_range(struct v4l2_subdev *sd, s32 *val)
965 *val = (MT9M114_F_NUMBER_DEFAULT_NUM << 24) |
966 (MT9M114_F_NUMBER_DEM << 16) |
967 (MT9M114_F_NUMBER_DEFAULT_NUM << 8) | MT9M114_F_NUMBER_DEM;
971 /* Horizontal flip the image. */
972 static int mt9m114_g_hflip(struct v4l2_subdev *sd, s32 *val)
974 struct i2c_client *c = v4l2_get_subdevdata(sd);
977 ret = mt9m114_read_reg(c, MISENSOR_16BIT,
978 (u32)MISENSOR_READ_MODE, &data);
981 *val = !!(data & MISENSOR_HFLIP_MASK);
986 static int mt9m114_g_vflip(struct v4l2_subdev *sd, s32 *val)
988 struct i2c_client *c = v4l2_get_subdevdata(sd);
992 ret = mt9m114_read_reg(c, MISENSOR_16BIT,
993 (u32)MISENSOR_READ_MODE, &data);
996 *val = !!(data & MISENSOR_VFLIP_MASK);
1001 static long mt9m114_s_exposure(struct v4l2_subdev *sd,
1002 struct atomisp_exposure *exposure)
1004 struct i2c_client *client = v4l2_get_subdevdata(sd);
1005 struct mt9m114_device *dev = to_mt9m114_sensor(sd);
1007 unsigned int coarse_integration = 0;
1008 unsigned int fine_integration = 0;
1009 unsigned int FLines = 0;
1010 unsigned int FrameLengthLines = 0; /* ExposureTime.FrameLengthLines; */
1011 unsigned int AnalogGain, DigitalGain;
1012 u32 AnalogGainToWrite = 0;
1013 u16 exposure_local[3];
1015 dev_dbg(&client->dev, "%s(0x%X 0x%X 0x%X)\n", __func__,
1016 exposure->integration_time[0], exposure->gain[0],
1019 coarse_integration = exposure->integration_time[0];
1020 /* fine_integration = ExposureTime.FineIntegrationTime; */
1021 /* FrameLengthLines = ExposureTime.FrameLengthLines; */
1022 FLines = mt9m114_res[dev->res].lines_per_frame;
1023 AnalogGain = exposure->gain[0];
1024 DigitalGain = exposure->gain[1];
1025 if (!dev->streamon) {
1026 /*Save the first exposure values while stream is off*/
1027 dev->first_exp = coarse_integration;
1028 dev->first_gain = AnalogGain;
1029 dev->first_diggain = DigitalGain;
1031 /* DigitalGain = 0x400 * (((u16) DigitalGain) >> 8) +
1032 ((unsigned int)(0x400 * (((u16) DigitalGain) & 0xFF)) >>8); */
1034 /* set frame length */
1035 if (FLines < coarse_integration + 6)
1036 FLines = coarse_integration + 6;
1037 if (FLines < FrameLengthLines)
1038 FLines = FrameLengthLines;
1039 ret = mt9m114_write_reg(client, MISENSOR_16BIT, 0x300A, FLines);
1041 v4l2_err(client, "%s: fail to set FLines\n", __func__);
1045 /* set coarse/fine integration */
1046 exposure_local[0] = REG_EXPO_COARSE;
1047 exposure_local[1] = (u16)coarse_integration;
1048 exposure_local[2] = (u16)fine_integration;
1049 /* 3A provide real exposure time.
1050 should not translate to any value here. */
1051 ret = mt9m114_write_reg(client, MISENSOR_16BIT,
1052 REG_EXPO_COARSE, (u16)(coarse_integration));
1054 v4l2_err(client, "%s: fail to set exposure time\n", __func__);
1059 // set analog/digital gain
1063 AnalogGainToWrite = 0x0;
1066 AnalogGainToWrite = 0x20;
1069 AnalogGainToWrite = 0x60;
1072 AnalogGainToWrite = 0xA0;
1075 AnalogGainToWrite = 0xE0;
1078 AnalogGainToWrite = 0x20;
1082 if (DigitalGain >= 16 || DigitalGain <= 1)
1084 /* AnalogGainToWrite =
1085 (u16)((DigitalGain << 12) | AnalogGainToWrite); */
1086 AnalogGainToWrite = (u16)((DigitalGain << 12) | (u16)AnalogGain);
1087 ret = mt9m114_write_reg(client, MISENSOR_16BIT,
1088 REG_GAIN, AnalogGainToWrite);
1090 v4l2_err(client, "%s: fail to set AnalogGainToWrite\n",
1098 static long mt9m114_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
1102 case ATOMISP_IOC_S_EXPOSURE:
1103 return mt9m114_s_exposure(sd, arg);
1111 /* This returns the exposure time being used. This should only be used
1112 for filling in EXIF data, not for actual image processing. */
1113 static int mt9m114_g_exposure(struct v4l2_subdev *sd, s32 *value)
1115 struct i2c_client *client = v4l2_get_subdevdata(sd);
1119 /* the fine integration time is currently not calculated */
1120 ret = mt9m114_read_reg(client, MISENSOR_16BIT,
1121 REG_EXPO_COARSE, &coarse);
1130 * This function will return the sensor supported max exposure zone number.
1131 * the sensor which supports max exposure zone number is 1.
1133 static int mt9m114_g_exposure_zone_num(struct v4l2_subdev *sd, s32 *val)
1141 * set exposure metering, average/center_weighted/spot/matrix.
1143 static int mt9m114_s_exposure_metering(struct v4l2_subdev *sd, s32 val)
1145 struct i2c_client *client = v4l2_get_subdevdata(sd);
1149 case V4L2_EXPOSURE_METERING_SPOT:
1150 ret = mt9m114_write_reg_array(client, mt9m114_exp_average,
1153 dev_err(&client->dev, "write exp_average reg err.\n");
1157 case V4L2_EXPOSURE_METERING_CENTER_WEIGHTED:
1159 ret = mt9m114_write_reg_array(client, mt9m114_exp_center,
1162 dev_err(&client->dev, "write exp_default reg err");
1171 * This function is for touch exposure feature.
1173 static int mt9m114_s_exposure_selection(struct v4l2_subdev *sd,
1174 struct v4l2_subdev_pad_config *cfg,
1175 struct v4l2_subdev_selection *sel)
1177 struct i2c_client *client = v4l2_get_subdevdata(sd);
1178 struct misensor_reg exp_reg;
1180 int grid_width, grid_height;
1181 int grid_left, grid_top, grid_right, grid_bottom;
1182 int win_left, win_top, win_right, win_bottom;
1186 if (sel->which != V4L2_SUBDEV_FORMAT_TRY &&
1187 sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
1190 grid_left = sel->r.left;
1191 grid_top = sel->r.top;
1192 grid_right = sel->r.left + sel->r.width - 1;
1193 grid_bottom = sel->r.top + sel->r.height - 1;
1195 ret = mt9m114_res2size(sd, &width, &height);
1199 grid_width = width / 5;
1200 grid_height = height / 5;
1202 if (grid_width && grid_height) {
1203 win_left = grid_left / grid_width;
1204 win_top = grid_top / grid_height;
1205 win_right = grid_right / grid_width;
1206 win_bottom = grid_bottom / grid_height;
1208 dev_err(&client->dev, "Incorrect exp grid.\n");
1212 clamp_t(int, win_left, 0, 4);
1213 clamp_t(int, win_top, 0, 4);
1214 clamp_t(int, win_right, 0, 4);
1215 clamp_t(int, win_bottom, 0, 4);
1217 ret = mt9m114_write_reg_array(client, mt9m114_exp_average, NO_POLLING);
1219 dev_err(&client->dev, "write exp_average reg err.\n");
1223 for (i = win_top; i <= win_bottom; i++) {
1224 for (j = win_left; j <= win_right; j++) {
1225 exp_reg = mt9m114_exp_win[i][j];
1227 ret = mt9m114_write_reg(client, exp_reg.length,
1228 exp_reg.reg, exp_reg.val);
1230 dev_err(&client->dev, "write exp_reg err.\n");
1240 static int mt9m114_g_bin_factor_x(struct v4l2_subdev *sd, s32 *val)
1242 struct mt9m114_device *dev = to_mt9m114_sensor(sd);
1244 *val = mt9m114_res[dev->res].bin_factor_x;
1249 static int mt9m114_g_bin_factor_y(struct v4l2_subdev *sd, s32 *val)
1251 struct mt9m114_device *dev = to_mt9m114_sensor(sd);
1253 *val = mt9m114_res[dev->res].bin_factor_y;
1258 static int mt9m114_s_ev(struct v4l2_subdev *sd, s32 val)
1260 struct i2c_client *c = v4l2_get_subdevdata(sd);
1264 /* EV value only support -2 to 2
1265 * 0: 0x37, 1:0x47, 2:0x57, -1:0x27, -2:0x17
1267 if (val < -2 || val > 2)
1270 dev_dbg(&c->dev, "%s val:%d luma:0x%x\n", __func__, val, luma);
1271 err = mt9m114_write_reg(c, MISENSOR_16BIT, 0x098E, 0xC87A);
1273 dev_err(&c->dev, "%s logic addr access error\n", __func__);
1276 err = mt9m114_write_reg(c, MISENSOR_8BIT, 0xC87A, (u32)luma);
1278 dev_err(&c->dev, "%s write target_average_luma failed\n",
1287 static int mt9m114_g_ev(struct v4l2_subdev *sd, s32 *val)
1289 struct i2c_client *c = v4l2_get_subdevdata(sd);
1293 err = mt9m114_write_reg(c, MISENSOR_16BIT, 0x098E, 0xC87A);
1295 dev_err(&c->dev, "%s logic addr access error\n", __func__);
1298 err = mt9m114_read_reg(c, MISENSOR_8BIT, 0xC87A, &luma);
1300 dev_err(&c->dev, "%s read target_average_luma failed\n",
1306 *val = (s32)luma - 2;
1307 dev_dbg(&c->dev, "%s val:%d\n", __func__, *val);
1313 * mt9m114 now can not support 3a_lock
1315 static int mt9m114_s_3a_lock(struct v4l2_subdev *sd, s32 val)
1321 static int mt9m114_g_3a_lock(struct v4l2_subdev *sd, s32 *val)
1324 return V4L2_LOCK_EXPOSURE | V4L2_LOCK_WHITE_BALANCE
1329 static int mt9m114_s_ctrl(struct v4l2_ctrl *ctrl)
1331 struct mt9m114_device *dev =
1332 container_of(ctrl->handler, struct mt9m114_device, ctrl_handler);
1333 struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
1337 case V4L2_CID_VFLIP:
1338 dev_dbg(&client->dev, "%s: CID_VFLIP:%d.\n",
1339 __func__, ctrl->val);
1340 ret = mt9m114_t_vflip(&dev->sd, ctrl->val);
1342 case V4L2_CID_HFLIP:
1343 dev_dbg(&client->dev, "%s: CID_HFLIP:%d.\n",
1344 __func__, ctrl->val);
1345 ret = mt9m114_t_hflip(&dev->sd, ctrl->val);
1348 case V4L2_CID_EXPOSURE_METERING:
1349 ret = mt9m114_s_exposure_metering(&dev->sd, ctrl->val);
1352 case V4L2_CID_EXPOSURE:
1353 ret = mt9m114_s_ev(&dev->sd, ctrl->val);
1355 case V4L2_CID_3A_LOCK:
1356 ret = mt9m114_s_3a_lock(&dev->sd, ctrl->val);
1364 static int mt9m114_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
1366 struct mt9m114_device *dev =
1367 container_of(ctrl->handler, struct mt9m114_device, ctrl_handler);
1371 case V4L2_CID_VFLIP:
1372 ret = mt9m114_g_vflip(&dev->sd, &ctrl->val);
1374 case V4L2_CID_HFLIP:
1375 ret = mt9m114_g_hflip(&dev->sd, &ctrl->val);
1377 case V4L2_CID_FOCAL_ABSOLUTE:
1378 ret = mt9m114_g_focal(&dev->sd, &ctrl->val);
1380 case V4L2_CID_FNUMBER_ABSOLUTE:
1381 ret = mt9m114_g_fnumber(&dev->sd, &ctrl->val);
1383 case V4L2_CID_FNUMBER_RANGE:
1384 ret = mt9m114_g_fnumber_range(&dev->sd, &ctrl->val);
1386 case V4L2_CID_EXPOSURE_ABSOLUTE:
1387 ret = mt9m114_g_exposure(&dev->sd, &ctrl->val);
1390 case V4L2_CID_EXPOSURE_ZONE_NUM:
1391 ret = mt9m114_g_exposure_zone_num(&dev->sd, &ctrl->val);
1394 case V4L2_CID_BIN_FACTOR_HORZ:
1395 ret = mt9m114_g_bin_factor_x(&dev->sd, &ctrl->val);
1397 case V4L2_CID_BIN_FACTOR_VERT:
1398 ret = mt9m114_g_bin_factor_y(&dev->sd, &ctrl->val);
1400 case V4L2_CID_EXPOSURE:
1401 ret = mt9m114_g_ev(&dev->sd, &ctrl->val);
1403 case V4L2_CID_3A_LOCK:
1404 ret = mt9m114_g_3a_lock(&dev->sd, &ctrl->val);
1413 static const struct v4l2_ctrl_ops ctrl_ops = {
1414 .s_ctrl = mt9m114_s_ctrl,
1415 .g_volatile_ctrl = mt9m114_g_volatile_ctrl
1418 static struct v4l2_ctrl_config mt9m114_controls[] = {
1421 .id = V4L2_CID_VFLIP,
1422 .name = "Image v-Flip",
1423 .type = V4L2_CTRL_TYPE_INTEGER,
1431 .id = V4L2_CID_HFLIP,
1432 .name = "Image h-Flip",
1433 .type = V4L2_CTRL_TYPE_INTEGER,
1441 .id = V4L2_CID_FOCAL_ABSOLUTE,
1442 .name = "focal length",
1443 .type = V4L2_CTRL_TYPE_INTEGER,
1444 .min = MT9M114_FOCAL_LENGTH_DEFAULT,
1445 .max = MT9M114_FOCAL_LENGTH_DEFAULT,
1447 .def = MT9M114_FOCAL_LENGTH_DEFAULT,
1452 .id = V4L2_CID_FNUMBER_ABSOLUTE,
1454 .type = V4L2_CTRL_TYPE_INTEGER,
1455 .min = MT9M114_F_NUMBER_DEFAULT,
1456 .max = MT9M114_F_NUMBER_DEFAULT,
1458 .def = MT9M114_F_NUMBER_DEFAULT,
1463 .id = V4L2_CID_FNUMBER_RANGE,
1464 .name = "f-number range",
1465 .type = V4L2_CTRL_TYPE_INTEGER,
1466 .min = MT9M114_F_NUMBER_RANGE,
1467 .max = MT9M114_F_NUMBER_RANGE,
1469 .def = MT9M114_F_NUMBER_RANGE,
1474 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
1476 .type = V4L2_CTRL_TYPE_INTEGER,
1486 .id = V4L2_CID_EXPOSURE_ZONE_NUM,
1487 .name = "one-time exposure zone number",
1488 .type = V4L2_CTRL_TYPE_INTEGER,
1497 .id = V4L2_CID_EXPOSURE_METERING,
1499 .type = V4L2_CTRL_TYPE_MENU,
1509 .id = V4L2_CID_BIN_FACTOR_HORZ,
1510 .name = "horizontal binning factor",
1511 .type = V4L2_CTRL_TYPE_INTEGER,
1513 .max = MT9M114_BIN_FACTOR_MAX,
1520 .id = V4L2_CID_BIN_FACTOR_VERT,
1521 .name = "vertical binning factor",
1522 .type = V4L2_CTRL_TYPE_INTEGER,
1524 .max = MT9M114_BIN_FACTOR_MAX,
1531 .id = V4L2_CID_EXPOSURE,
1532 .name = "exposure biasx",
1533 .type = V4L2_CTRL_TYPE_INTEGER,
1542 .id = V4L2_CID_3A_LOCK,
1544 .type = V4L2_CTRL_TYPE_BITMASK,
1546 .max = V4L2_LOCK_EXPOSURE | V4L2_LOCK_WHITE_BALANCE | V4L2_LOCK_FOCUS,
1553 static int mt9m114_detect(struct mt9m114_device *dev, struct i2c_client *client)
1555 struct i2c_adapter *adapter = client->adapter;
1558 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
1559 dev_err(&client->dev, "%s: i2c error", __func__);
1562 mt9m114_read_reg(client, MISENSOR_16BIT, (u32)MT9M114_PID, &retvalue);
1563 dev->real_model_id = retvalue;
1565 if (retvalue != MT9M114_MOD_ID) {
1566 dev_err(&client->dev, "%s: failed: client->addr = %x\n",
1567 __func__, client->addr);
1575 mt9m114_s_config(struct v4l2_subdev *sd, int irq, void *platform_data)
1577 struct mt9m114_device *dev = to_mt9m114_sensor(sd);
1578 struct i2c_client *client = v4l2_get_subdevdata(sd);
1581 if (NULL == platform_data)
1584 dev->platform_data =
1585 (struct camera_sensor_platform_data *)platform_data;
1587 if (dev->platform_data->platform_init) {
1588 ret = dev->platform_data->platform_init(client);
1590 v4l2_err(client, "mt9m114 platform init err\n");
1596 v4l2_err(client, "mt9m114 power-up err");
1600 /* config & detect sensor */
1601 ret = mt9m114_detect(dev, client);
1603 v4l2_err(client, "mt9m114_detect err s_config.\n");
1607 ret = dev->platform_data->csi_cfg(sd, 1);
1611 ret = mt9m114_set_suspend(sd);
1613 v4l2_err(client, "mt9m114 suspend err");
1617 ret = power_down(sd);
1619 v4l2_err(client, "mt9m114 power down err");
1626 dev->platform_data->csi_cfg(sd, 0);
1629 dev_err(&client->dev, "sensor power-gating failed\n");
1633 /* Horizontal flip the image. */
1634 static int mt9m114_t_hflip(struct v4l2_subdev *sd, int value)
1636 struct i2c_client *c = v4l2_get_subdevdata(sd);
1637 struct mt9m114_device *dev = to_mt9m114_sensor(sd);
1639 /* set for direct mode */
1640 err = mt9m114_write_reg(c, MISENSOR_16BIT, 0x098E, 0xC850);
1642 /* enable H flip ctx A */
1643 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC850, 0x01, 0x01);
1644 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC851, 0x01, 0x01);
1646 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC888, 0x01, 0x01);
1647 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC889, 0x01, 0x01);
1649 err += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE,
1650 MISENSOR_HFLIP_MASK, MISENSOR_FLIP_EN);
1652 dev->bpat = MT9M114_BPAT_GRGRBGBG;
1654 /* disable H flip ctx A */
1655 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC850, 0x01, 0x00);
1656 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC851, 0x01, 0x00);
1658 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC888, 0x01, 0x00);
1659 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC889, 0x01, 0x00);
1661 err += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE,
1662 MISENSOR_HFLIP_MASK, MISENSOR_FLIP_DIS);
1664 dev->bpat = MT9M114_BPAT_BGBGGRGR;
1667 err += mt9m114_write_reg(c, MISENSOR_8BIT, 0x8404, 0x06);
1673 /* Vertically flip the image */
1674 static int mt9m114_t_vflip(struct v4l2_subdev *sd, int value)
1676 struct i2c_client *c = v4l2_get_subdevdata(sd);
1678 /* set for direct mode */
1679 err = mt9m114_write_reg(c, MISENSOR_16BIT, 0x098E, 0xC850);
1681 /* enable H flip - ctx A */
1682 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC850, 0x02, 0x01);
1683 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC851, 0x02, 0x01);
1685 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC888, 0x02, 0x01);
1686 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC889, 0x02, 0x01);
1688 err += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE,
1689 MISENSOR_VFLIP_MASK, MISENSOR_FLIP_EN);
1691 /* disable H flip - ctx A */
1692 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC850, 0x02, 0x00);
1693 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC851, 0x02, 0x00);
1695 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC888, 0x02, 0x00);
1696 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC889, 0x02, 0x00);
1698 err += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE,
1699 MISENSOR_VFLIP_MASK, MISENSOR_FLIP_DIS);
1702 err += mt9m114_write_reg(c, MISENSOR_8BIT, 0x8404, 0x06);
1707 static int mt9m114_s_parm(struct v4l2_subdev *sd,
1708 struct v4l2_streamparm *param)
1713 static int mt9m114_g_frame_interval(struct v4l2_subdev *sd,
1714 struct v4l2_subdev_frame_interval *interval)
1716 struct mt9m114_device *dev = to_mt9m114_sensor(sd);
1718 interval->interval.numerator = 1;
1719 interval->interval.denominator = mt9m114_res[dev->res].fps;
1724 static int mt9m114_s_stream(struct v4l2_subdev *sd, int enable)
1727 struct i2c_client *c = v4l2_get_subdevdata(sd);
1728 struct mt9m114_device *dev = to_mt9m114_sensor(sd);
1729 struct atomisp_exposure exposure;
1732 ret = mt9m114_write_reg_array(c, mt9m114_chgstat_reg,
1737 if (dev->first_exp > MT9M114_MAX_FIRST_EXP) {
1738 exposure.integration_time[0] = dev->first_exp;
1739 exposure.gain[0] = dev->first_gain;
1740 exposure.gain[1] = dev->first_diggain;
1741 mt9m114_s_exposure(sd, &exposure);
1747 ret = mt9m114_set_suspend(sd);
1753 static int mt9m114_enum_mbus_code(struct v4l2_subdev *sd,
1754 struct v4l2_subdev_pad_config *cfg,
1755 struct v4l2_subdev_mbus_code_enum *code)
1759 code->code = MEDIA_BUS_FMT_SGRBG10_1X10;
1764 static int mt9m114_enum_frame_size(struct v4l2_subdev *sd,
1765 struct v4l2_subdev_pad_config *cfg,
1766 struct v4l2_subdev_frame_size_enum *fse)
1769 unsigned int index = fse->index;
1774 fse->min_width = mt9m114_res[index].width;
1775 fse->min_height = mt9m114_res[index].height;
1776 fse->max_width = mt9m114_res[index].width;
1777 fse->max_height = mt9m114_res[index].height;
1782 static int mt9m114_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
1785 struct mt9m114_device *snr = to_mt9m114_sensor(sd);
1790 for (index = 0; index < N_RES; index++) {
1791 if (mt9m114_res[index].res == snr->res)
1798 *frames = mt9m114_res[index].skip_frames;
1803 static const struct v4l2_subdev_video_ops mt9m114_video_ops = {
1804 .s_parm = mt9m114_s_parm,
1805 .s_stream = mt9m114_s_stream,
1806 .g_frame_interval = mt9m114_g_frame_interval,
1809 static struct v4l2_subdev_sensor_ops mt9m114_sensor_ops = {
1810 .g_skip_frames = mt9m114_g_skip_frames,
1813 static const struct v4l2_subdev_core_ops mt9m114_core_ops = {
1814 .s_power = mt9m114_s_power,
1815 .ioctl = mt9m114_ioctl,
1818 /* REVISIT: Do we need pad operations? */
1819 static const struct v4l2_subdev_pad_ops mt9m114_pad_ops = {
1820 .enum_mbus_code = mt9m114_enum_mbus_code,
1821 .enum_frame_size = mt9m114_enum_frame_size,
1822 .get_fmt = mt9m114_get_fmt,
1823 .set_fmt = mt9m114_set_fmt,
1825 .set_selection = mt9m114_s_exposure_selection,
1829 static const struct v4l2_subdev_ops mt9m114_ops = {
1830 .core = &mt9m114_core_ops,
1831 .video = &mt9m114_video_ops,
1832 .pad = &mt9m114_pad_ops,
1833 .sensor = &mt9m114_sensor_ops,
1836 static const struct media_entity_operations mt9m114_entity_ops = {
1840 static int mt9m114_remove(struct i2c_client *client)
1842 struct mt9m114_device *dev;
1843 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1845 dev = container_of(sd, struct mt9m114_device, sd);
1846 dev->platform_data->csi_cfg(sd, 0);
1847 if (dev->platform_data->platform_deinit)
1848 dev->platform_data->platform_deinit();
1849 v4l2_device_unregister_subdev(sd);
1850 media_entity_cleanup(&dev->sd.entity);
1851 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1856 static int mt9m114_probe(struct i2c_client *client,
1857 const struct i2c_device_id *id)
1859 struct mt9m114_device *dev;
1864 /* Setup sensor configuration structure */
1865 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1867 dev_err(&client->dev, "out of memory\n");
1871 v4l2_i2c_subdev_init(&dev->sd, client, &mt9m114_ops);
1872 pdata = client->dev.platform_data;
1873 if (ACPI_COMPANION(&client->dev))
1874 pdata = gmin_camera_platform_data(&dev->sd,
1875 ATOMISP_INPUT_FORMAT_RAW_10,
1876 atomisp_bayer_order_grbg);
1878 ret = mt9m114_s_config(&dev->sd, client->irq, pdata);
1879 if (!pdata || ret) {
1880 v4l2_device_unregister_subdev(&dev->sd);
1885 ret = atomisp_register_i2c_module(&dev->sd, pdata, RAW_CAMERA);
1887 v4l2_device_unregister_subdev(&dev->sd);
1889 /* Coverity CID 298095 - return on error */
1893 /*TODO add format code here*/
1894 dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1895 dev->pad.flags = MEDIA_PAD_FL_SOURCE;
1896 dev->format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
1897 dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1900 v4l2_ctrl_handler_init(&dev->ctrl_handler,
1901 ARRAY_SIZE(mt9m114_controls));
1903 mt9m114_remove(client);
1907 for (i = 0; i < ARRAY_SIZE(mt9m114_controls); i++)
1908 v4l2_ctrl_new_custom(&dev->ctrl_handler, &mt9m114_controls[i],
1911 if (dev->ctrl_handler.error) {
1912 mt9m114_remove(client);
1913 return dev->ctrl_handler.error;
1916 /* Use same lock for controls as for everything else. */
1917 dev->ctrl_handler.lock = &dev->input_lock;
1918 dev->sd.ctrl_handler = &dev->ctrl_handler;
1920 /* REVISIT: Do we need media controller? */
1921 ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad);
1923 mt9m114_remove(client);
1929 MODULE_DEVICE_TABLE(i2c, mt9m114_id);
1931 static struct acpi_device_id mt9m114_acpi_match[] = {
1937 MODULE_DEVICE_TABLE(acpi, mt9m114_acpi_match);
1939 static struct i2c_driver mt9m114_driver = {
1942 .acpi_match_table = ACPI_PTR(mt9m114_acpi_match),
1944 .probe = mt9m114_probe,
1945 .remove = mt9m114_remove,
1946 .id_table = mt9m114_id,
1949 static __init int init_mt9m114(void)
1951 return i2c_add_driver(&mt9m114_driver);
1954 static __exit void exit_mt9m114(void)
1956 i2c_del_driver(&mt9m114_driver);
1959 module_init(init_mt9m114);
1960 module_exit(exit_mt9m114);
1962 MODULE_AUTHOR("Shuguang Gong <Shuguang.gong@intel.com>");
1963 MODULE_LICENSE("GPL");