1 // SPDX-License-Identifier: GPL-2.0
3 * Support for mt9m114 Camera Sensor.
5 * Copyright (c) 2010 Intel Corporation. All Rights Reserved.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version
9 * 2 as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
19 #include <linux/module.h>
20 #include <linux/types.h>
21 #include <linux/kernel.h>
23 #include <linux/string.h>
24 #include <linux/errno.h>
25 #include <linux/init.h>
26 #include <linux/kmod.h>
27 #include <linux/device.h>
29 #include <linux/slab.h>
30 #include <linux/delay.h>
31 #include <linux/i2c.h>
32 #include <linux/acpi.h>
33 #include "../include/linux/atomisp_gmin_platform.h"
34 #include <media/v4l2-device.h>
38 #define to_mt9m114_sensor(sd) container_of(sd, struct mt9m114_device, sd)
41 * TODO: use debug parameter to actually define when debug messages should
46 module_param(debug, int, 0644);
47 MODULE_PARM_DESC(debug, "Debug level (0-1)");
49 static int mt9m114_t_vflip(struct v4l2_subdev *sd, int value);
50 static int mt9m114_t_hflip(struct v4l2_subdev *sd, int value);
51 static int mt9m114_wait_state(struct i2c_client *client, int timeout);
54 mt9m114_read_reg(struct i2c_client *client, u16 data_length, u32 reg, u32 *val)
57 struct i2c_msg msg[2];
58 unsigned char data[4];
60 if (!client->adapter) {
61 v4l2_err(client, "%s error, no client->adapter\n", __func__);
65 if (data_length != MISENSOR_8BIT && data_length != MISENSOR_16BIT
66 && data_length != MISENSOR_32BIT) {
67 v4l2_err(client, "%s error, invalid data length\n", __func__);
71 msg[0].addr = client->addr;
73 msg[0].len = MSG_LEN_OFFSET;
76 /* high byte goes out first */
77 data[0] = (u16)(reg >> 8);
78 data[1] = (u16)(reg & 0xff);
80 msg[1].addr = client->addr;
81 msg[1].len = data_length;
82 msg[1].flags = I2C_M_RD;
85 err = i2c_transfer(client->adapter, msg, 2);
89 /* high byte comes first */
90 if (data_length == MISENSOR_8BIT)
92 else if (data_length == MISENSOR_16BIT)
93 *val = data[1] + (data[0] << 8);
95 *val = data[3] + (data[2] << 8) +
96 (data[1] << 16) + (data[0] << 24);
101 dev_err(&client->dev, "read from offset 0x%x error %d", reg, err);
106 mt9m114_write_reg(struct i2c_client *client, u16 data_length, u16 reg, u32 val)
110 unsigned char data[6] = {0};
114 if (!client->adapter) {
115 v4l2_err(client, "%s error, no client->adapter\n", __func__);
119 if (data_length != MISENSOR_8BIT && data_length != MISENSOR_16BIT
120 && data_length != MISENSOR_32BIT) {
121 v4l2_err(client, "%s error, invalid data_length\n", __func__);
125 memset(&msg, 0, sizeof(msg));
128 msg.addr = client->addr;
130 msg.len = 2 + data_length;
133 /* high byte goes out first */
135 *wreg = cpu_to_be16(reg);
137 if (data_length == MISENSOR_8BIT) {
139 } else if (data_length == MISENSOR_16BIT) {
140 u16 *wdata = (void *)&data[2];
142 *wdata = be16_to_cpu(*(__be16 *)&data[2]);
145 u32 *wdata = (void *)&data[2];
147 *wdata = be32_to_cpu(*(__be32 *)&data[2]);
150 num_msg = i2c_transfer(client->adapter, &msg, 1);
153 * HACK: Need some delay here for Rev 2 sensors otherwise some
154 * registers do not seem to load correctly.
161 dev_err(&client->dev, "write error: wrote 0x%x to offset 0x%x error %d",
163 if (retry <= I2C_RETRY_COUNT) {
164 dev_dbg(&client->dev, "retrying... %d", retry);
174 * misensor_rmw_reg - Read/Modify/Write a value to a register in the sensor
176 * @client: i2c driver client structure
177 * @data_length: 8/16/32-bits length
178 * @reg: register address
179 * @mask: masked out bits
182 * Read/modify/write a value to a register in the sensor device.
183 * Returns zero if successful, or non-zero otherwise.
186 misensor_rmw_reg(struct i2c_client *client, u16 data_length, u16 reg,
192 /* Exit when no mask */
196 /* @mask must not exceed data length */
197 switch (data_length) {
209 /* Wrong @data_length */
213 err = mt9m114_read_reg(client, data_length, reg, &val);
215 v4l2_err(client, "%s error exit, read failed\n", __func__);
222 * Perform the OR function if the @set exists.
223 * Shift @set value to target bit location. @set should set only
224 * bits included in @mask.
226 * REVISIT: This function expects @set to be non-shifted. Its shift
227 * value is then defined to be equal to mask's LSB position.
228 * How about to inform values in their right offset position and avoid
229 * this unneeded shift operation?
231 set <<= ffs(mask) - 1;
234 err = mt9m114_write_reg(client, data_length, reg, val);
236 v4l2_err(client, "%s error exit, write failed\n", __func__);
243 static int __mt9m114_flush_reg_array(struct i2c_client *client,
244 struct mt9m114_write_ctrl *ctrl)
247 const int num_msg = 1;
250 __be16 *data16 = (void *)&ctrl->buffer.addr;
252 if (ctrl->index == 0)
256 msg.addr = client->addr;
258 msg.len = 2 + ctrl->index;
259 *data16 = cpu_to_be16(ctrl->buffer.addr);
260 msg.buf = (u8 *)&ctrl->buffer;
262 ret = i2c_transfer(client->adapter, &msg, num_msg);
263 if (ret != num_msg) {
264 if (++retry <= I2C_RETRY_COUNT) {
265 dev_dbg(&client->dev, "retrying... %d\n", retry);
269 dev_err(&client->dev, "%s: i2c transfer error\n", __func__);
276 * REVISIT: Previously we had a delay after writing data to sensor.
277 * But it was removed as our tests have shown it is not necessary
284 static int __mt9m114_buf_reg_array(struct i2c_client *client,
285 struct mt9m114_write_ctrl *ctrl,
286 const struct misensor_reg *next)
292 /* Insufficient buffer? Let's flush and get more free space. */
293 if (ctrl->index + next->length >= MT9M114_MAX_WRITE_BUF_SIZE) {
294 err = __mt9m114_flush_reg_array(client, ctrl);
299 switch (next->length) {
301 ctrl->buffer.data[ctrl->index] = (u8)next->val;
304 data16 = (__be16 *)&ctrl->buffer.data[ctrl->index];
305 *data16 = cpu_to_be16((u16)next->val);
308 data32 = (__be32 *)&ctrl->buffer.data[ctrl->index];
309 *data32 = cpu_to_be32(next->val);
315 /* When first item is added, we need to store its starting address */
316 if (ctrl->index == 0)
317 ctrl->buffer.addr = next->reg;
319 ctrl->index += next->length;
325 __mt9m114_write_reg_is_consecutive(struct i2c_client *client,
326 struct mt9m114_write_ctrl *ctrl,
327 const struct misensor_reg *next)
329 if (ctrl->index == 0)
332 return ctrl->buffer.addr + ctrl->index == next->reg;
336 * mt9m114_write_reg_array - Initializes a list of mt9m114 registers
337 * @client: i2c driver client structure
338 * @reglist: list of registers to be written
339 * @poll: completion polling requirement
340 * This function initializes a list of registers. When consecutive addresses
341 * are found in a row on the list, this function creates a buffer and sends
342 * consecutive data in a single i2c_transfer().
344 * __mt9m114_flush_reg_array, __mt9m114_buf_reg_array() and
345 * __mt9m114_write_reg_is_consecutive() are internal functions to
346 * mt9m114_write_reg_array() and should be not used anywhere else.
349 static int mt9m114_write_reg_array(struct i2c_client *client,
350 const struct misensor_reg *reglist,
353 const struct misensor_reg *next = reglist;
354 struct mt9m114_write_ctrl ctrl;
357 if (poll == PRE_POLLING) {
358 err = mt9m114_wait_state(client, MT9M114_WAIT_STAT_TIMEOUT);
364 for (; next->length != MISENSOR_TOK_TERM; next++) {
365 switch (next->length & MISENSOR_TOK_MASK) {
366 case MISENSOR_TOK_DELAY:
367 err = __mt9m114_flush_reg_array(client, &ctrl);
372 case MISENSOR_TOK_RMW:
373 err = __mt9m114_flush_reg_array(client, &ctrl);
374 err |= misensor_rmw_reg(client,
377 next->reg, next->val,
380 dev_err(&client->dev, "%s read err. aborted\n",
387 * If next address is not consecutive, data needs to be
388 * flushed before proceed.
390 if (!__mt9m114_write_reg_is_consecutive(client, &ctrl,
392 err = __mt9m114_flush_reg_array(client, &ctrl);
396 err = __mt9m114_buf_reg_array(client, &ctrl, next);
398 v4l2_err(client, "%s: write error, aborted\n",
406 err = __mt9m114_flush_reg_array(client, &ctrl);
410 if (poll == POST_POLLING)
411 return mt9m114_wait_state(client, MT9M114_WAIT_STAT_TIMEOUT);
416 static int mt9m114_wait_state(struct i2c_client *client, int timeout)
421 while (timeout-- > 0) {
422 ret = mt9m114_read_reg(client, MISENSOR_16BIT, 0x0080, &val);
425 if ((val & 0x2) == 0)
433 static int mt9m114_set_suspend(struct v4l2_subdev *sd)
435 struct i2c_client *client = v4l2_get_subdevdata(sd);
437 return mt9m114_write_reg_array(client,
438 mt9m114_standby_reg, POST_POLLING);
441 static int mt9m114_init_common(struct v4l2_subdev *sd)
443 struct i2c_client *client = v4l2_get_subdevdata(sd);
445 return mt9m114_write_reg_array(client, mt9m114_common, PRE_POLLING);
448 static int power_ctrl(struct v4l2_subdev *sd, bool flag)
451 struct mt9m114_device *dev = to_mt9m114_sensor(sd);
453 if (!dev || !dev->platform_data)
457 ret = dev->platform_data->v2p8_ctrl(sd, 1);
459 ret = dev->platform_data->v1p8_ctrl(sd, 1);
461 ret = dev->platform_data->v2p8_ctrl(sd, 0);
464 ret = dev->platform_data->v2p8_ctrl(sd, 0);
465 ret = dev->platform_data->v1p8_ctrl(sd, 0);
470 static int gpio_ctrl(struct v4l2_subdev *sd, bool flag)
473 struct mt9m114_device *dev = to_mt9m114_sensor(sd);
475 if (!dev || !dev->platform_data)
479 * Note: current modules wire only one GPIO signal (RESET#),
480 * but the schematic wires up two to the connector. BIOS
481 * versions have been unfortunately inconsistent with which
482 * ACPI index RESET# is on, so hit both
486 ret = dev->platform_data->gpio0_ctrl(sd, 0);
487 ret = dev->platform_data->gpio1_ctrl(sd, 0);
489 ret |= dev->platform_data->gpio0_ctrl(sd, 1);
490 ret |= dev->platform_data->gpio1_ctrl(sd, 1);
492 ret = dev->platform_data->gpio0_ctrl(sd, 0);
493 ret = dev->platform_data->gpio1_ctrl(sd, 0);
498 static int power_up(struct v4l2_subdev *sd)
500 struct mt9m114_device *dev = to_mt9m114_sensor(sd);
501 struct i2c_client *client = v4l2_get_subdevdata(sd);
504 if (!dev->platform_data) {
505 dev_err(&client->dev, "no camera_sensor_platform_data");
510 ret = power_ctrl(sd, 1);
514 /* flis clock control */
515 ret = dev->platform_data->flisclk_ctrl(sd, 1);
520 ret = gpio_ctrl(sd, 1);
522 dev_err(&client->dev, "gpio failed 1\n");
524 * according to DS, 44ms is needed between power up and first i2c
532 dev->platform_data->flisclk_ctrl(sd, 0);
535 dev_err(&client->dev, "sensor power-up failed\n");
540 static int power_down(struct v4l2_subdev *sd)
542 struct mt9m114_device *dev = to_mt9m114_sensor(sd);
543 struct i2c_client *client = v4l2_get_subdevdata(sd);
546 if (!dev->platform_data) {
547 dev_err(&client->dev, "no camera_sensor_platform_data");
551 ret = dev->platform_data->flisclk_ctrl(sd, 0);
553 dev_err(&client->dev, "flisclk failed\n");
556 ret = gpio_ctrl(sd, 0);
558 dev_err(&client->dev, "gpio failed 1\n");
561 ret = power_ctrl(sd, 0);
563 dev_err(&client->dev, "vprog failed.\n");
565 /* according to DS, 20ms is needed after power down */
571 static int mt9m114_s_power(struct v4l2_subdev *sd, int power)
574 return power_down(sd);
579 return mt9m114_init_common(sd);
582 static int mt9m114_res2size(struct v4l2_subdev *sd, int *h_size, int *v_size)
584 struct mt9m114_device *dev = to_mt9m114_sensor(sd);
585 unsigned short hsize;
586 unsigned short vsize;
589 case MT9M114_RES_736P:
590 hsize = MT9M114_RES_736P_SIZE_H;
591 vsize = MT9M114_RES_736P_SIZE_V;
593 case MT9M114_RES_864P:
594 hsize = MT9M114_RES_864P_SIZE_H;
595 vsize = MT9M114_RES_864P_SIZE_V;
597 case MT9M114_RES_960P:
598 hsize = MT9M114_RES_960P_SIZE_H;
599 vsize = MT9M114_RES_960P_SIZE_V;
602 v4l2_err(sd, "%s: Resolution 0x%08x unknown\n", __func__,
615 static int mt9m114_get_intg_factor(struct i2c_client *client,
616 struct camera_mipi_info *info,
617 const struct mt9m114_res_struct *res)
619 struct atomisp_sensor_mode_data *buf = &info->data;
626 ret = mt9m114_read_reg(client, MISENSOR_32BIT,
627 REG_PIXEL_CLK, ®_val);
630 buf->vt_pix_clk_freq_mhz = reg_val;
632 /* get integration time */
633 buf->coarse_integration_time_min = MT9M114_COARSE_INTG_TIME_MIN;
634 buf->coarse_integration_time_max_margin =
635 MT9M114_COARSE_INTG_TIME_MAX_MARGIN;
637 buf->fine_integration_time_min = MT9M114_FINE_INTG_TIME_MIN;
638 buf->fine_integration_time_max_margin =
639 MT9M114_FINE_INTG_TIME_MAX_MARGIN;
641 buf->fine_integration_time_def = MT9M114_FINE_INTG_TIME_MIN;
643 buf->frame_length_lines = res->lines_per_frame;
644 buf->line_length_pck = res->pixels_per_line;
645 buf->read_mode = res->bin_mode;
647 /* get the cropping and output resolution to ISP for this mode. */
648 ret = mt9m114_read_reg(client, MISENSOR_16BIT,
649 REG_H_START, ®_val);
652 buf->crop_horizontal_start = reg_val;
654 ret = mt9m114_read_reg(client, MISENSOR_16BIT,
655 REG_V_START, ®_val);
658 buf->crop_vertical_start = reg_val;
660 ret = mt9m114_read_reg(client, MISENSOR_16BIT,
661 REG_H_END, ®_val);
664 buf->crop_horizontal_end = reg_val;
666 ret = mt9m114_read_reg(client, MISENSOR_16BIT,
667 REG_V_END, ®_val);
670 buf->crop_vertical_end = reg_val;
672 ret = mt9m114_read_reg(client, MISENSOR_16BIT,
673 REG_WIDTH, ®_val);
676 buf->output_width = reg_val;
678 ret = mt9m114_read_reg(client, MISENSOR_16BIT,
679 REG_HEIGHT, ®_val);
682 buf->output_height = reg_val;
684 ret = mt9m114_read_reg(client, MISENSOR_16BIT,
685 REG_TIMING_HTS, ®_val);
688 buf->line_length_pck = reg_val;
690 ret = mt9m114_read_reg(client, MISENSOR_16BIT,
691 REG_TIMING_VTS, ®_val);
694 buf->frame_length_lines = reg_val;
696 buf->binning_factor_x = res->bin_factor_x ?
697 res->bin_factor_x : 1;
698 buf->binning_factor_y = res->bin_factor_y ?
699 res->bin_factor_y : 1;
703 static int mt9m114_get_fmt(struct v4l2_subdev *sd,
704 struct v4l2_subdev_state *sd_state,
705 struct v4l2_subdev_format *format)
707 struct v4l2_mbus_framefmt *fmt = &format->format;
713 fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
715 ret = mt9m114_res2size(sd, &width, &height);
719 fmt->height = height;
724 static int mt9m114_set_fmt(struct v4l2_subdev *sd,
725 struct v4l2_subdev_state *sd_state,
726 struct v4l2_subdev_format *format)
728 struct v4l2_mbus_framefmt *fmt = &format->format;
729 struct i2c_client *c = v4l2_get_subdevdata(sd);
730 struct mt9m114_device *dev = to_mt9m114_sensor(sd);
731 struct mt9m114_res_struct *res;
732 u32 width = fmt->width;
733 u32 height = fmt->height;
734 struct camera_mipi_info *mt9m114_info = NULL;
741 dev->first_exp = MT9M114_DEFAULT_FIRST_EXP;
743 mt9m114_info = v4l2_get_subdev_hostdata(sd);
747 res = v4l2_find_nearest_size(mt9m114_res,
748 ARRAY_SIZE(mt9m114_res), width,
749 height, fmt->width, fmt->height);
751 res = &mt9m114_res[N_RES - 1];
753 fmt->width = res->width;
754 fmt->height = res->height;
756 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
757 sd_state->pads->try_fmt = *fmt;
762 case MT9M114_RES_736P:
763 ret = mt9m114_write_reg_array(c, mt9m114_736P_init, NO_POLLING);
764 ret += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE,
765 MISENSOR_R_MODE_MASK, MISENSOR_NORMAL_SET);
767 case MT9M114_RES_864P:
768 ret = mt9m114_write_reg_array(c, mt9m114_864P_init, NO_POLLING);
769 ret += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE,
770 MISENSOR_R_MODE_MASK, MISENSOR_NORMAL_SET);
772 case MT9M114_RES_960P:
773 ret = mt9m114_write_reg_array(c, mt9m114_976P_init, NO_POLLING);
774 /* set sensor read_mode to Normal */
775 ret += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE,
776 MISENSOR_R_MODE_MASK, MISENSOR_NORMAL_SET);
779 v4l2_err(sd, "set resolution: %d failed!\n", res->res);
786 ret = mt9m114_write_reg_array(c, mt9m114_chgstat_reg, POST_POLLING);
790 if (mt9m114_set_suspend(sd))
793 if (dev->res != res->res) {
796 /* Switch to different size */
798 dev->nctx = 0x00; /* Set for context A */
801 * Context B is used for resolutions larger than 640x480
802 * Using YUV for Context B.
804 dev->nctx = 0x01; /* set for context B */
808 * Marked current sensor res as being "used"
810 * REVISIT: We don't need to use an "used" field on each mode
811 * list entry to know which mode is selected. If this
812 * information is really necessary, how about to use a single
813 * variable on sensor dev struct?
815 for (index = 0; index < N_RES; index++) {
816 if ((width == mt9m114_res[index].width) &&
817 (height == mt9m114_res[index].height)) {
818 mt9m114_res[index].used = true;
821 mt9m114_res[index].used = false;
824 ret = mt9m114_get_intg_factor(c, mt9m114_info,
825 &mt9m114_res[res->res]);
827 dev_err(&c->dev, "failed to get integration_factor\n");
831 * mt9m114 - we don't poll for context switch
832 * because it does not happen with streaming disabled.
837 fmt->height = height;
838 fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
842 /* TODO: Update to SOC functions, remove exposure and gain */
843 static int mt9m114_g_focal(struct v4l2_subdev *sd, s32 *val)
845 *val = (MT9M114_FOCAL_LENGTH_NUM << 16) | MT9M114_FOCAL_LENGTH_DEM;
849 static int mt9m114_g_fnumber(struct v4l2_subdev *sd, s32 *val)
851 /* const f number for mt9m114 */
852 *val = (MT9M114_F_NUMBER_DEFAULT_NUM << 16) | MT9M114_F_NUMBER_DEM;
856 static int mt9m114_g_fnumber_range(struct v4l2_subdev *sd, s32 *val)
858 *val = (MT9M114_F_NUMBER_DEFAULT_NUM << 24) |
859 (MT9M114_F_NUMBER_DEM << 16) |
860 (MT9M114_F_NUMBER_DEFAULT_NUM << 8) | MT9M114_F_NUMBER_DEM;
864 /* Horizontal flip the image. */
865 static int mt9m114_g_hflip(struct v4l2_subdev *sd, s32 *val)
867 struct i2c_client *c = v4l2_get_subdevdata(sd);
871 ret = mt9m114_read_reg(c, MISENSOR_16BIT,
872 (u32)MISENSOR_READ_MODE, &data);
875 *val = !!(data & MISENSOR_HFLIP_MASK);
880 static int mt9m114_g_vflip(struct v4l2_subdev *sd, s32 *val)
882 struct i2c_client *c = v4l2_get_subdevdata(sd);
886 ret = mt9m114_read_reg(c, MISENSOR_16BIT,
887 (u32)MISENSOR_READ_MODE, &data);
890 *val = !!(data & MISENSOR_VFLIP_MASK);
895 static long mt9m114_s_exposure(struct v4l2_subdev *sd,
896 struct atomisp_exposure *exposure)
898 struct i2c_client *client = v4l2_get_subdevdata(sd);
899 struct mt9m114_device *dev = to_mt9m114_sensor(sd);
901 unsigned int coarse_integration = 0;
902 unsigned int f_lines = 0;
903 unsigned int frame_len_lines = 0; /* ExposureTime.FrameLengthLines; */
904 unsigned int analog_gain, digital_gain;
905 u32 analog_gain_to_write = 0;
907 dev_dbg(&client->dev, "%s(0x%X 0x%X 0x%X)\n", __func__,
908 exposure->integration_time[0], exposure->gain[0],
911 coarse_integration = exposure->integration_time[0];
913 * fine_integration = ExposureTime.FineIntegrationTime;
914 * frame_len_lines = ExposureTime.FrameLengthLines;
916 f_lines = mt9m114_res[dev->res].lines_per_frame;
917 analog_gain = exposure->gain[0];
918 digital_gain = exposure->gain[1];
919 if (!dev->streamon) {
920 /*Save the first exposure values while stream is off*/
921 dev->first_exp = coarse_integration;
922 dev->first_gain = analog_gain;
923 dev->first_diggain = digital_gain;
925 /* digital_gain = 0x400 * (((u16) digital_gain) >> 8) + */
926 /* ((unsigned int)(0x400 * (((u16) digital_gain) & 0xFF)) >>8); */
928 /* set frame length */
929 if (f_lines < coarse_integration + 6)
930 f_lines = coarse_integration + 6;
931 if (f_lines < frame_len_lines)
932 f_lines = frame_len_lines;
933 ret = mt9m114_write_reg(client, MISENSOR_16BIT, 0x300A, f_lines);
935 v4l2_err(client, "%s: fail to set f_lines\n", __func__);
939 /* set coarse integration */
941 * 3A provide real exposure time.
942 * should not translate to any value here.
944 ret = mt9m114_write_reg(client, MISENSOR_16BIT,
945 REG_EXPO_COARSE, (u16)(coarse_integration));
947 v4l2_err(client, "%s: fail to set exposure time\n", __func__);
952 * set analog/digital gain
956 analog_gain_to_write = 0x0;
959 analog_gain_to_write = 0x20;
962 analog_gain_to_write = 0x60;
965 analog_gain_to_write = 0xA0;
968 analog_gain_to_write = 0xE0;
971 analog_gain_to_write = 0x20;
975 if (digital_gain >= 16 || digital_gain <= 1)
978 * analog_gain_to_write = (u16)((digital_gain << 12)
979 * | analog_gain_to_write);
981 analog_gain_to_write = (u16)((digital_gain << 12) | (u16)analog_gain);
982 ret = mt9m114_write_reg(client, MISENSOR_16BIT,
983 REG_GAIN, analog_gain_to_write);
985 v4l2_err(client, "%s: fail to set analog_gain_to_write\n",
993 static long mt9m114_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
996 case ATOMISP_IOC_S_EXPOSURE:
997 return mt9m114_s_exposure(sd, arg);
1006 * This returns the exposure time being used. This should only be used
1007 * for filling in EXIF data, not for actual image processing.
1009 static int mt9m114_g_exposure(struct v4l2_subdev *sd, s32 *value)
1011 struct i2c_client *client = v4l2_get_subdevdata(sd);
1015 /* the fine integration time is currently not calculated */
1016 ret = mt9m114_read_reg(client, MISENSOR_16BIT,
1017 REG_EXPO_COARSE, &coarse);
1026 * This function will return the sensor supported max exposure zone number.
1027 * the sensor which supports max exposure zone number is 1.
1029 static int mt9m114_g_exposure_zone_num(struct v4l2_subdev *sd, s32 *val)
1037 * set exposure metering, average/center_weighted/spot/matrix.
1039 static int mt9m114_s_exposure_metering(struct v4l2_subdev *sd, s32 val)
1041 struct i2c_client *client = v4l2_get_subdevdata(sd);
1045 case V4L2_EXPOSURE_METERING_SPOT:
1046 ret = mt9m114_write_reg_array(client, mt9m114_exp_average,
1049 dev_err(&client->dev, "write exp_average reg err.\n");
1053 case V4L2_EXPOSURE_METERING_CENTER_WEIGHTED:
1055 ret = mt9m114_write_reg_array(client, mt9m114_exp_center,
1058 dev_err(&client->dev, "write exp_default reg err");
1067 * This function is for touch exposure feature.
1069 static int mt9m114_s_exposure_selection(struct v4l2_subdev *sd,
1070 struct v4l2_subdev_state *sd_state,
1071 struct v4l2_subdev_selection *sel)
1073 struct i2c_client *client = v4l2_get_subdevdata(sd);
1074 struct misensor_reg exp_reg;
1076 int grid_width, grid_height;
1077 int grid_left, grid_top, grid_right, grid_bottom;
1078 int win_left, win_top, win_right, win_bottom;
1082 if (sel->which != V4L2_SUBDEV_FORMAT_TRY &&
1083 sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
1086 grid_left = sel->r.left;
1087 grid_top = sel->r.top;
1088 grid_right = sel->r.left + sel->r.width - 1;
1089 grid_bottom = sel->r.top + sel->r.height - 1;
1091 ret = mt9m114_res2size(sd, &width, &height);
1095 grid_width = width / 5;
1096 grid_height = height / 5;
1098 if (grid_width && grid_height) {
1099 win_left = grid_left / grid_width;
1100 win_top = grid_top / grid_height;
1101 win_right = grid_right / grid_width;
1102 win_bottom = grid_bottom / grid_height;
1104 dev_err(&client->dev, "Incorrect exp grid.\n");
1108 win_left = clamp_t(int, win_left, 0, 4);
1109 win_top = clamp_t(int, win_top, 0, 4);
1110 win_right = clamp_t(int, win_right, 0, 4);
1111 win_bottom = clamp_t(int, win_bottom, 0, 4);
1113 ret = mt9m114_write_reg_array(client, mt9m114_exp_average, NO_POLLING);
1115 dev_err(&client->dev, "write exp_average reg err.\n");
1119 for (i = win_top; i <= win_bottom; i++) {
1120 for (j = win_left; j <= win_right; j++) {
1121 exp_reg = mt9m114_exp_win[i][j];
1123 ret = mt9m114_write_reg(client, exp_reg.length,
1124 exp_reg.reg, exp_reg.val);
1126 dev_err(&client->dev, "write exp_reg err.\n");
1135 static int mt9m114_g_bin_factor_x(struct v4l2_subdev *sd, s32 *val)
1137 struct mt9m114_device *dev = to_mt9m114_sensor(sd);
1139 *val = mt9m114_res[dev->res].bin_factor_x;
1144 static int mt9m114_g_bin_factor_y(struct v4l2_subdev *sd, s32 *val)
1146 struct mt9m114_device *dev = to_mt9m114_sensor(sd);
1148 *val = mt9m114_res[dev->res].bin_factor_y;
1153 static int mt9m114_s_ev(struct v4l2_subdev *sd, s32 val)
1155 struct i2c_client *c = v4l2_get_subdevdata(sd);
1160 * EV value only support -2 to 2
1161 * 0: 0x37, 1:0x47, 2:0x57, -1:0x27, -2:0x17
1163 if (val < -2 || val > 2)
1166 dev_dbg(&c->dev, "%s val:%d luma:0x%x\n", __func__, val, luma);
1167 err = mt9m114_write_reg(c, MISENSOR_16BIT, 0x098E, 0xC87A);
1169 dev_err(&c->dev, "%s logic addr access error\n", __func__);
1172 err = mt9m114_write_reg(c, MISENSOR_8BIT, 0xC87A, (u32)luma);
1174 dev_err(&c->dev, "%s write target_average_luma failed\n",
1183 static int mt9m114_g_ev(struct v4l2_subdev *sd, s32 *val)
1185 struct i2c_client *c = v4l2_get_subdevdata(sd);
1189 err = mt9m114_write_reg(c, MISENSOR_16BIT, 0x098E, 0xC87A);
1191 dev_err(&c->dev, "%s logic addr access error\n", __func__);
1194 err = mt9m114_read_reg(c, MISENSOR_8BIT, 0xC87A, &luma);
1196 dev_err(&c->dev, "%s read target_average_luma failed\n",
1202 *val = (s32)luma - 2;
1203 dev_dbg(&c->dev, "%s val:%d\n", __func__, *val);
1210 * mt9m114 now can not support 3a_lock
1212 static int mt9m114_s_3a_lock(struct v4l2_subdev *sd, s32 val)
1218 static int mt9m114_g_3a_lock(struct v4l2_subdev *sd, s32 *val)
1221 return V4L2_LOCK_EXPOSURE | V4L2_LOCK_WHITE_BALANCE
1226 static int mt9m114_s_ctrl(struct v4l2_ctrl *ctrl)
1228 struct mt9m114_device *dev =
1229 container_of(ctrl->handler, struct mt9m114_device, ctrl_handler);
1230 struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
1234 case V4L2_CID_VFLIP:
1235 dev_dbg(&client->dev, "%s: CID_VFLIP:%d.\n",
1236 __func__, ctrl->val);
1237 ret = mt9m114_t_vflip(&dev->sd, ctrl->val);
1239 case V4L2_CID_HFLIP:
1240 dev_dbg(&client->dev, "%s: CID_HFLIP:%d.\n",
1241 __func__, ctrl->val);
1242 ret = mt9m114_t_hflip(&dev->sd, ctrl->val);
1244 case V4L2_CID_EXPOSURE_METERING:
1245 ret = mt9m114_s_exposure_metering(&dev->sd, ctrl->val);
1247 case V4L2_CID_EXPOSURE:
1248 ret = mt9m114_s_ev(&dev->sd, ctrl->val);
1250 case V4L2_CID_3A_LOCK:
1251 ret = mt9m114_s_3a_lock(&dev->sd, ctrl->val);
1259 static int mt9m114_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
1261 struct mt9m114_device *dev =
1262 container_of(ctrl->handler, struct mt9m114_device, ctrl_handler);
1266 case V4L2_CID_VFLIP:
1267 ret = mt9m114_g_vflip(&dev->sd, &ctrl->val);
1269 case V4L2_CID_HFLIP:
1270 ret = mt9m114_g_hflip(&dev->sd, &ctrl->val);
1272 case V4L2_CID_FOCAL_ABSOLUTE:
1273 ret = mt9m114_g_focal(&dev->sd, &ctrl->val);
1275 case V4L2_CID_FNUMBER_ABSOLUTE:
1276 ret = mt9m114_g_fnumber(&dev->sd, &ctrl->val);
1278 case V4L2_CID_FNUMBER_RANGE:
1279 ret = mt9m114_g_fnumber_range(&dev->sd, &ctrl->val);
1281 case V4L2_CID_EXPOSURE_ABSOLUTE:
1282 ret = mt9m114_g_exposure(&dev->sd, &ctrl->val);
1284 case V4L2_CID_EXPOSURE_ZONE_NUM:
1285 ret = mt9m114_g_exposure_zone_num(&dev->sd, &ctrl->val);
1287 case V4L2_CID_BIN_FACTOR_HORZ:
1288 ret = mt9m114_g_bin_factor_x(&dev->sd, &ctrl->val);
1290 case V4L2_CID_BIN_FACTOR_VERT:
1291 ret = mt9m114_g_bin_factor_y(&dev->sd, &ctrl->val);
1293 case V4L2_CID_EXPOSURE:
1294 ret = mt9m114_g_ev(&dev->sd, &ctrl->val);
1296 case V4L2_CID_3A_LOCK:
1297 ret = mt9m114_g_3a_lock(&dev->sd, &ctrl->val);
1306 static const struct v4l2_ctrl_ops ctrl_ops = {
1307 .s_ctrl = mt9m114_s_ctrl,
1308 .g_volatile_ctrl = mt9m114_g_volatile_ctrl
1311 static struct v4l2_ctrl_config mt9m114_controls[] = {
1314 .id = V4L2_CID_VFLIP,
1315 .name = "Image v-Flip",
1316 .type = V4L2_CTRL_TYPE_INTEGER,
1324 .id = V4L2_CID_HFLIP,
1325 .name = "Image h-Flip",
1326 .type = V4L2_CTRL_TYPE_INTEGER,
1334 .id = V4L2_CID_FOCAL_ABSOLUTE,
1335 .name = "focal length",
1336 .type = V4L2_CTRL_TYPE_INTEGER,
1337 .min = MT9M114_FOCAL_LENGTH_DEFAULT,
1338 .max = MT9M114_FOCAL_LENGTH_DEFAULT,
1340 .def = MT9M114_FOCAL_LENGTH_DEFAULT,
1345 .id = V4L2_CID_FNUMBER_ABSOLUTE,
1347 .type = V4L2_CTRL_TYPE_INTEGER,
1348 .min = MT9M114_F_NUMBER_DEFAULT,
1349 .max = MT9M114_F_NUMBER_DEFAULT,
1351 .def = MT9M114_F_NUMBER_DEFAULT,
1356 .id = V4L2_CID_FNUMBER_RANGE,
1357 .name = "f-number range",
1358 .type = V4L2_CTRL_TYPE_INTEGER,
1359 .min = MT9M114_F_NUMBER_RANGE,
1360 .max = MT9M114_F_NUMBER_RANGE,
1362 .def = MT9M114_F_NUMBER_RANGE,
1367 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
1369 .type = V4L2_CTRL_TYPE_INTEGER,
1378 .id = V4L2_CID_EXPOSURE_ZONE_NUM,
1379 .name = "one-time exposure zone number",
1380 .type = V4L2_CTRL_TYPE_INTEGER,
1389 .id = V4L2_CID_EXPOSURE_METERING,
1391 .type = V4L2_CTRL_TYPE_MENU,
1400 .id = V4L2_CID_BIN_FACTOR_HORZ,
1401 .name = "horizontal binning factor",
1402 .type = V4L2_CTRL_TYPE_INTEGER,
1404 .max = MT9M114_BIN_FACTOR_MAX,
1411 .id = V4L2_CID_BIN_FACTOR_VERT,
1412 .name = "vertical binning factor",
1413 .type = V4L2_CTRL_TYPE_INTEGER,
1415 .max = MT9M114_BIN_FACTOR_MAX,
1422 .id = V4L2_CID_EXPOSURE,
1423 .name = "exposure biasx",
1424 .type = V4L2_CTRL_TYPE_INTEGER,
1433 .id = V4L2_CID_3A_LOCK,
1435 .type = V4L2_CTRL_TYPE_BITMASK,
1437 .max = V4L2_LOCK_EXPOSURE | V4L2_LOCK_WHITE_BALANCE | V4L2_LOCK_FOCUS,
1444 static int mt9m114_detect(struct mt9m114_device *dev, struct i2c_client *client)
1446 struct i2c_adapter *adapter = client->adapter;
1450 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
1451 dev_err(&client->dev, "%s: i2c error", __func__);
1454 ret = mt9m114_read_reg(client, MISENSOR_16BIT, MT9M114_PID, &model);
1457 dev->real_model_id = model;
1459 if (model != MT9M114_MOD_ID) {
1460 dev_err(&client->dev, "%s: failed: client->addr = %x\n",
1461 __func__, client->addr);
1469 mt9m114_s_config(struct v4l2_subdev *sd, int irq, void *platform_data)
1471 struct mt9m114_device *dev = to_mt9m114_sensor(sd);
1472 struct i2c_client *client = v4l2_get_subdevdata(sd);
1478 dev->platform_data =
1479 (struct camera_sensor_platform_data *)platform_data;
1483 v4l2_err(client, "mt9m114 power-up err");
1487 /* config & detect sensor */
1488 ret = mt9m114_detect(dev, client);
1490 v4l2_err(client, "mt9m114_detect err s_config.\n");
1494 ret = dev->platform_data->csi_cfg(sd, 1);
1498 ret = mt9m114_set_suspend(sd);
1500 v4l2_err(client, "mt9m114 suspend err");
1504 ret = power_down(sd);
1506 v4l2_err(client, "mt9m114 power down err");
1513 dev->platform_data->csi_cfg(sd, 0);
1516 dev_err(&client->dev, "sensor power-gating failed\n");
1520 /* Horizontal flip the image. */
1521 static int mt9m114_t_hflip(struct v4l2_subdev *sd, int value)
1523 struct i2c_client *c = v4l2_get_subdevdata(sd);
1524 struct mt9m114_device *dev = to_mt9m114_sensor(sd);
1526 /* set for direct mode */
1527 err = mt9m114_write_reg(c, MISENSOR_16BIT, 0x098E, 0xC850);
1529 /* enable H flip ctx A */
1530 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC850, 0x01, 0x01);
1531 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC851, 0x01, 0x01);
1533 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC888, 0x01, 0x01);
1534 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC889, 0x01, 0x01);
1536 err += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE,
1537 MISENSOR_HFLIP_MASK, MISENSOR_FLIP_EN);
1539 dev->bpat = MT9M114_BPAT_GRGRBGBG;
1541 /* disable H flip ctx A */
1542 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC850, 0x01, 0x00);
1543 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC851, 0x01, 0x00);
1545 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC888, 0x01, 0x00);
1546 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC889, 0x01, 0x00);
1548 err += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE,
1549 MISENSOR_HFLIP_MASK, MISENSOR_FLIP_DIS);
1551 dev->bpat = MT9M114_BPAT_BGBGGRGR;
1554 err += mt9m114_write_reg(c, MISENSOR_8BIT, 0x8404, 0x06);
1560 /* Vertically flip the image */
1561 static int mt9m114_t_vflip(struct v4l2_subdev *sd, int value)
1563 struct i2c_client *c = v4l2_get_subdevdata(sd);
1565 /* set for direct mode */
1566 err = mt9m114_write_reg(c, MISENSOR_16BIT, 0x098E, 0xC850);
1568 /* enable H flip - ctx A */
1569 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC850, 0x02, 0x01);
1570 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC851, 0x02, 0x01);
1572 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC888, 0x02, 0x01);
1573 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC889, 0x02, 0x01);
1575 err += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE,
1576 MISENSOR_VFLIP_MASK, MISENSOR_FLIP_EN);
1578 /* disable H flip - ctx A */
1579 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC850, 0x02, 0x00);
1580 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC851, 0x02, 0x00);
1582 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC888, 0x02, 0x00);
1583 err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC889, 0x02, 0x00);
1585 err += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE,
1586 MISENSOR_VFLIP_MASK, MISENSOR_FLIP_DIS);
1589 err += mt9m114_write_reg(c, MISENSOR_8BIT, 0x8404, 0x06);
1595 static int mt9m114_g_frame_interval(struct v4l2_subdev *sd,
1596 struct v4l2_subdev_frame_interval *interval)
1598 struct mt9m114_device *dev = to_mt9m114_sensor(sd);
1600 interval->interval.numerator = 1;
1601 interval->interval.denominator = mt9m114_res[dev->res].fps;
1606 static int mt9m114_s_stream(struct v4l2_subdev *sd, int enable)
1609 struct i2c_client *c = v4l2_get_subdevdata(sd);
1610 struct mt9m114_device *dev = to_mt9m114_sensor(sd);
1611 struct atomisp_exposure exposure;
1614 ret = mt9m114_write_reg_array(c, mt9m114_chgstat_reg,
1619 if (dev->first_exp > MT9M114_MAX_FIRST_EXP) {
1620 exposure.integration_time[0] = dev->first_exp;
1621 exposure.gain[0] = dev->first_gain;
1622 exposure.gain[1] = dev->first_diggain;
1623 mt9m114_s_exposure(sd, &exposure);
1629 ret = mt9m114_set_suspend(sd);
1635 static int mt9m114_enum_mbus_code(struct v4l2_subdev *sd,
1636 struct v4l2_subdev_state *sd_state,
1637 struct v4l2_subdev_mbus_code_enum *code)
1641 code->code = MEDIA_BUS_FMT_SGRBG10_1X10;
1646 static int mt9m114_enum_frame_size(struct v4l2_subdev *sd,
1647 struct v4l2_subdev_state *sd_state,
1648 struct v4l2_subdev_frame_size_enum *fse)
1650 unsigned int index = fse->index;
1655 fse->min_width = mt9m114_res[index].width;
1656 fse->min_height = mt9m114_res[index].height;
1657 fse->max_width = mt9m114_res[index].width;
1658 fse->max_height = mt9m114_res[index].height;
1663 static int mt9m114_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
1666 struct mt9m114_device *snr = to_mt9m114_sensor(sd);
1671 for (index = 0; index < N_RES; index++) {
1672 if (mt9m114_res[index].res == snr->res)
1679 *frames = mt9m114_res[index].skip_frames;
1684 static const struct v4l2_subdev_video_ops mt9m114_video_ops = {
1685 .s_stream = mt9m114_s_stream,
1686 .g_frame_interval = mt9m114_g_frame_interval,
1689 static const struct v4l2_subdev_sensor_ops mt9m114_sensor_ops = {
1690 .g_skip_frames = mt9m114_g_skip_frames,
1693 static const struct v4l2_subdev_core_ops mt9m114_core_ops = {
1694 .s_power = mt9m114_s_power,
1695 .ioctl = mt9m114_ioctl,
1698 /* REVISIT: Do we need pad operations? */
1699 static const struct v4l2_subdev_pad_ops mt9m114_pad_ops = {
1700 .enum_mbus_code = mt9m114_enum_mbus_code,
1701 .enum_frame_size = mt9m114_enum_frame_size,
1702 .get_fmt = mt9m114_get_fmt,
1703 .set_fmt = mt9m114_set_fmt,
1704 .set_selection = mt9m114_s_exposure_selection,
1707 static const struct v4l2_subdev_ops mt9m114_ops = {
1708 .core = &mt9m114_core_ops,
1709 .video = &mt9m114_video_ops,
1710 .pad = &mt9m114_pad_ops,
1711 .sensor = &mt9m114_sensor_ops,
1714 static int mt9m114_remove(struct i2c_client *client)
1716 struct mt9m114_device *dev;
1717 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1719 dev = container_of(sd, struct mt9m114_device, sd);
1720 dev->platform_data->csi_cfg(sd, 0);
1721 v4l2_device_unregister_subdev(sd);
1722 media_entity_cleanup(&dev->sd.entity);
1723 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1728 static int mt9m114_probe(struct i2c_client *client)
1730 struct mt9m114_device *dev;
1735 /* Setup sensor configuration structure */
1736 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1740 v4l2_i2c_subdev_init(&dev->sd, client, &mt9m114_ops);
1741 pdata = gmin_camera_platform_data(&dev->sd,
1742 ATOMISP_INPUT_FORMAT_RAW_10,
1743 atomisp_bayer_order_grbg);
1745 ret = mt9m114_s_config(&dev->sd, client->irq, pdata);
1746 if (!pdata || ret) {
1747 v4l2_device_unregister_subdev(&dev->sd);
1752 ret = atomisp_register_i2c_module(&dev->sd, pdata, RAW_CAMERA);
1754 v4l2_device_unregister_subdev(&dev->sd);
1756 /* Coverity CID 298095 - return on error */
1760 /* TODO add format code here */
1761 dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1762 dev->pad.flags = MEDIA_PAD_FL_SOURCE;
1763 dev->format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
1764 dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1767 v4l2_ctrl_handler_init(&dev->ctrl_handler,
1768 ARRAY_SIZE(mt9m114_controls));
1770 mt9m114_remove(client);
1774 for (i = 0; i < ARRAY_SIZE(mt9m114_controls); i++)
1775 v4l2_ctrl_new_custom(&dev->ctrl_handler, &mt9m114_controls[i],
1778 if (dev->ctrl_handler.error) {
1779 mt9m114_remove(client);
1780 return dev->ctrl_handler.error;
1783 /* Use same lock for controls as for everything else. */
1784 dev->ctrl_handler.lock = &dev->input_lock;
1785 dev->sd.ctrl_handler = &dev->ctrl_handler;
1787 /* REVISIT: Do we need media controller? */
1788 ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad);
1790 mt9m114_remove(client);
1796 static const struct acpi_device_id mt9m114_acpi_match[] = {
1801 MODULE_DEVICE_TABLE(acpi, mt9m114_acpi_match);
1803 static struct i2c_driver mt9m114_driver = {
1806 .acpi_match_table = mt9m114_acpi_match,
1808 .probe_new = mt9m114_probe,
1809 .remove = mt9m114_remove,
1811 module_i2c_driver(mt9m114_driver);
1813 MODULE_AUTHOR("Shuguang Gong <Shuguang.gong@intel.com>");
1814 MODULE_LICENSE("GPL");