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/acpi.h>
36 #include "../include/linux/atomisp_gmin_platform.h"
37 #include <media/v4l2-device.h>
41 #define to_mt9m114_sensor(sd) container_of(sd, struct mt9m114_device, sd)
44 * TODO: use debug parameter to actually define when debug messages should
49 module_param(debug
, int, 0644);
50 MODULE_PARM_DESC(debug
, "Debug level (0-1)");
52 static int mt9m114_t_vflip(struct v4l2_subdev
*sd
, int value
);
53 static int mt9m114_t_hflip(struct v4l2_subdev
*sd
, int value
);
54 static int mt9m114_wait_state(struct i2c_client
*client
, int timeout
);
57 mt9m114_read_reg(struct i2c_client
*client
, u16 data_length
, u32 reg
, u32
*val
)
60 struct i2c_msg msg
[2];
61 unsigned char data
[4];
63 if (!client
->adapter
) {
64 v4l2_err(client
, "%s error, no client->adapter\n", __func__
);
68 if (data_length
!= MISENSOR_8BIT
&& data_length
!= MISENSOR_16BIT
69 && data_length
!= MISENSOR_32BIT
) {
70 v4l2_err(client
, "%s error, invalid data length\n", __func__
);
74 msg
[0].addr
= client
->addr
;
76 msg
[0].len
= MSG_LEN_OFFSET
;
79 /* high byte goes out first */
80 data
[0] = (u16
) (reg
>> 8);
81 data
[1] = (u16
) (reg
& 0xff);
83 msg
[1].addr
= client
->addr
;
84 msg
[1].len
= data_length
;
85 msg
[1].flags
= I2C_M_RD
;
88 err
= i2c_transfer(client
->adapter
, msg
, 2);
92 /* high byte comes first */
93 if (data_length
== MISENSOR_8BIT
)
95 else if (data_length
== MISENSOR_16BIT
)
96 *val
= data
[1] + (data
[0] << 8);
98 *val
= data
[3] + (data
[2] << 8) +
99 (data
[1] << 16) + (data
[0] << 24);
104 dev_err(&client
->dev
, "read from offset 0x%x error %d", reg
, err
);
109 mt9m114_write_reg(struct i2c_client
*client
, u16 data_length
, u16 reg
, u32 val
)
113 unsigned char data
[6] = {0};
117 if (!client
->adapter
) {
118 v4l2_err(client
, "%s error, no client->adapter\n", __func__
);
122 if (data_length
!= MISENSOR_8BIT
&& data_length
!= MISENSOR_16BIT
123 && data_length
!= MISENSOR_32BIT
) {
124 v4l2_err(client
, "%s error, invalid data_length\n", __func__
);
128 memset(&msg
, 0, sizeof(msg
));
131 msg
.addr
= client
->addr
;
133 msg
.len
= 2 + data_length
;
136 /* high byte goes out first */
138 *wreg
= cpu_to_be16(reg
);
140 if (data_length
== MISENSOR_8BIT
) {
142 } else if (data_length
== MISENSOR_16BIT
) {
143 u16
*wdata
= (u16
*)&data
[2];
144 *wdata
= be16_to_cpu((u16
)val
);
147 u32
*wdata
= (u32
*)&data
[2];
148 *wdata
= be32_to_cpu(val
);
151 num_msg
= i2c_transfer(client
->adapter
, &msg
, 1);
154 * HACK: Need some delay here for Rev 2 sensors otherwise some
155 * registers do not seem to load correctly.
162 dev_err(&client
->dev
, "write error: wrote 0x%x to offset 0x%x error %d",
164 if (retry
<= I2C_RETRY_COUNT
) {
165 dev_dbg(&client
->dev
, "retrying... %d", retry
);
175 * misensor_rmw_reg - Read/Modify/Write a value to a register in the sensor
177 * @client: i2c driver client structure
178 * @data_length: 8/16/32-bits length
179 * @reg: register address
180 * @mask: masked out bits
183 * Read/modify/write a value to a register in the sensor device.
184 * Returns zero if successful, or non-zero otherwise.
187 misensor_rmw_reg(struct i2c_client
*client
, u16 data_length
, u16 reg
,
193 /* Exit when no mask */
197 /* @mask must not exceed data length */
198 switch (data_length
) {
210 /* Wrong @data_length */
214 err
= mt9m114_read_reg(client
, data_length
, reg
, &val
);
216 v4l2_err(client
, "misensor_rmw_reg error exit, read failed\n");
223 * Perform the OR function if the @set exists.
224 * Shift @set value to target bit location. @set should set only
225 * bits included in @mask.
227 * REVISIT: This function expects @set to be non-shifted. Its shift
228 * value is then defined to be equal to mask's LSB position.
229 * How about to inform values in their right offset position and avoid
230 * this unneeded shift operation?
232 set
<<= ffs(mask
) - 1;
235 err
= mt9m114_write_reg(client
, data_length
, reg
, val
);
237 v4l2_err(client
, "misensor_rmw_reg error exit, write failed\n");
245 static int __mt9m114_flush_reg_array(struct i2c_client
*client
,
246 struct mt9m114_write_ctrl
*ctrl
)
249 const int num_msg
= 1;
253 if (ctrl
->index
== 0)
257 msg
.addr
= client
->addr
;
259 msg
.len
= 2 + ctrl
->index
;
260 ctrl
->buffer
.addr
= cpu_to_be16(ctrl
->buffer
.addr
);
261 msg
.buf
= (u8
*)&ctrl
->buffer
;
263 ret
= i2c_transfer(client
->adapter
, &msg
, num_msg
);
264 if (ret
!= num_msg
) {
265 if (++retry
<= I2C_RETRY_COUNT
) {
266 dev_dbg(&client
->dev
, "retrying... %d\n", retry
);
270 dev_err(&client
->dev
, "%s: i2c transfer error\n", __func__
);
277 * REVISIT: Previously we had a delay after writing data to sensor.
278 * But it was removed as our tests have shown it is not necessary
285 static int __mt9m114_buf_reg_array(struct i2c_client
*client
,
286 struct mt9m114_write_ctrl
*ctrl
,
287 const struct misensor_reg
*next
)
293 /* Insufficient buffer? Let's flush and get more free space. */
294 if (ctrl
->index
+ next
->length
>= MT9M114_MAX_WRITE_BUF_SIZE
) {
295 err
= __mt9m114_flush_reg_array(client
, ctrl
);
300 switch (next
->length
) {
302 ctrl
->buffer
.data
[ctrl
->index
] = (u8
)next
->val
;
305 data16
= (u16
*)&ctrl
->buffer
.data
[ctrl
->index
];
306 *data16
= cpu_to_be16((u16
)next
->val
);
309 data32
= (u32
*)&ctrl
->buffer
.data
[ctrl
->index
];
310 *data32
= cpu_to_be32(next
->val
);
316 /* When first item is added, we need to store its starting address */
317 if (ctrl
->index
== 0)
318 ctrl
->buffer
.addr
= next
->reg
;
320 ctrl
->index
+= next
->length
;
326 __mt9m114_write_reg_is_consecutive(struct i2c_client
*client
,
327 struct mt9m114_write_ctrl
*ctrl
,
328 const struct misensor_reg
*next
)
330 if (ctrl
->index
== 0)
333 return ctrl
->buffer
.addr
+ ctrl
->index
== next
->reg
;
337 * mt9m114_write_reg_array - Initializes a list of mt9m114 registers
338 * @client: i2c driver client structure
339 * @reglist: list of registers to be written
340 * @poll: completion polling requirement
341 * This function initializes a list of registers. When consecutive addresses
342 * are found in a row on the list, this function creates a buffer and sends
343 * consecutive data in a single i2c_transfer().
345 * __mt9m114_flush_reg_array, __mt9m114_buf_reg_array() and
346 * __mt9m114_write_reg_is_consecutive() are internal functions to
347 * mt9m114_write_reg_array() and should be not used anywhere else.
350 static int mt9m114_write_reg_array(struct i2c_client
*client
,
351 const struct misensor_reg
*reglist
,
354 const struct misensor_reg
*next
= reglist
;
355 struct mt9m114_write_ctrl ctrl
;
358 if (poll
== PRE_POLLING
) {
359 err
= mt9m114_wait_state(client
, MT9M114_WAIT_STAT_TIMEOUT
);
365 for (; next
->length
!= MISENSOR_TOK_TERM
; next
++) {
366 switch (next
->length
& MISENSOR_TOK_MASK
) {
367 case MISENSOR_TOK_DELAY
:
368 err
= __mt9m114_flush_reg_array(client
, &ctrl
);
373 case MISENSOR_TOK_RMW
:
374 err
= __mt9m114_flush_reg_array(client
, &ctrl
);
375 err
|= misensor_rmw_reg(client
,
378 next
->reg
, next
->val
,
381 dev_err(&client
->dev
, "%s read err. aborted\n",
388 * If next address is not consecutive, data needs to be
389 * flushed before proceed.
391 if (!__mt9m114_write_reg_is_consecutive(client
, &ctrl
,
393 err
= __mt9m114_flush_reg_array(client
, &ctrl
);
397 err
= __mt9m114_buf_reg_array(client
, &ctrl
, next
);
399 v4l2_err(client
, "%s: write error, aborted\n",
407 err
= __mt9m114_flush_reg_array(client
, &ctrl
);
411 if (poll
== POST_POLLING
)
412 return mt9m114_wait_state(client
, MT9M114_WAIT_STAT_TIMEOUT
);
417 static int mt9m114_wait_state(struct i2c_client
*client
, int timeout
)
422 while (timeout
-- > 0) {
423 ret
= mt9m114_read_reg(client
, MISENSOR_16BIT
, 0x0080, &val
);
426 if ((val
& 0x2) == 0)
435 static int mt9m114_set_suspend(struct v4l2_subdev
*sd
)
437 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
438 return mt9m114_write_reg_array(client
,
439 mt9m114_standby_reg
, POST_POLLING
);
442 static int mt9m114_init_common(struct v4l2_subdev
*sd
)
444 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
446 return mt9m114_write_reg_array(client
, mt9m114_common
, PRE_POLLING
);
449 static int power_ctrl(struct v4l2_subdev
*sd
, bool flag
)
452 struct mt9m114_device
*dev
= to_mt9m114_sensor(sd
);
454 if (!dev
|| !dev
->platform_data
)
458 ret
= dev
->platform_data
->v2p8_ctrl(sd
, 1);
460 ret
= dev
->platform_data
->v1p8_ctrl(sd
, 1);
462 ret
= dev
->platform_data
->v2p8_ctrl(sd
, 0);
465 ret
= dev
->platform_data
->v2p8_ctrl(sd
, 0);
466 ret
= dev
->platform_data
->v1p8_ctrl(sd
, 0);
471 static int gpio_ctrl(struct v4l2_subdev
*sd
, bool flag
)
474 struct mt9m114_device
*dev
= to_mt9m114_sensor(sd
);
476 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 */
485 ret
= dev
->platform_data
->gpio0_ctrl(sd
, 0);
486 ret
= dev
->platform_data
->gpio1_ctrl(sd
, 0);
488 ret
|= dev
->platform_data
->gpio0_ctrl(sd
, 1);
489 ret
|= dev
->platform_data
->gpio1_ctrl(sd
, 1);
491 ret
= dev
->platform_data
->gpio0_ctrl(sd
, 0);
492 ret
= dev
->platform_data
->gpio1_ctrl(sd
, 0);
497 static int power_up(struct v4l2_subdev
*sd
)
499 struct mt9m114_device
*dev
= to_mt9m114_sensor(sd
);
500 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
503 if (NULL
== dev
->platform_data
) {
504 dev_err(&client
->dev
, "no camera_sensor_platform_data");
509 ret
= power_ctrl(sd
, 1);
513 /* flis clock control */
514 ret
= dev
->platform_data
->flisclk_ctrl(sd
, 1);
519 ret
= gpio_ctrl(sd
, 1);
521 dev_err(&client
->dev
, "gpio failed 1\n");
523 * according to DS, 44ms is needed between power up and first i2c
531 dev
->platform_data
->flisclk_ctrl(sd
, 0);
534 dev_err(&client
->dev
, "sensor power-up failed\n");
539 static int power_down(struct v4l2_subdev
*sd
)
541 struct mt9m114_device
*dev
= to_mt9m114_sensor(sd
);
542 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
545 if (NULL
== dev
->platform_data
) {
546 dev_err(&client
->dev
, "no camera_sensor_platform_data");
550 ret
= dev
->platform_data
->flisclk_ctrl(sd
, 0);
552 dev_err(&client
->dev
, "flisclk failed\n");
555 ret
= gpio_ctrl(sd
, 0);
557 dev_err(&client
->dev
, "gpio failed 1\n");
560 ret
= power_ctrl(sd
, 0);
562 dev_err(&client
->dev
, "vprog failed.\n");
564 /*according to DS, 20ms is needed after power down*/
570 static int mt9m114_s_power(struct v4l2_subdev
*sd
, int power
)
573 return power_down(sd
);
578 return mt9m114_init_common(sd
);
583 * distance - calculate the distance
588 * Get the gap between resolution and w/h.
589 * res->width/height smaller than w/h wouldn't be considered.
590 * Returns the value of gap or -1 if fail.
592 #define LARGEST_ALLOWED_RATIO_MISMATCH 600
593 static int distance(struct mt9m114_res_struct
const *res
, u32 w
, u32 h
)
595 unsigned int w_ratio
;
596 unsigned int h_ratio
;
601 w_ratio
= (res
->width
<< 13) / w
;
604 h_ratio
= (res
->height
<< 13) / h
;
607 match
= abs(((w_ratio
<< 13) / h_ratio
) - 8192);
609 if ((w_ratio
< 8192) || (h_ratio
< 8192) ||
610 (match
> LARGEST_ALLOWED_RATIO_MISMATCH
))
613 return w_ratio
+ h_ratio
;
616 /* Return the nearest higher resolution index */
617 static int nearest_resolution_index(int w
, int h
)
622 int min_dist
= INT_MAX
;
623 const struct mt9m114_res_struct
*tmp_res
= NULL
;
625 for (i
= 0; i
< ARRAY_SIZE(mt9m114_res
); i
++) {
626 tmp_res
= &mt9m114_res
[i
];
627 dist
= distance(tmp_res
, w
, h
);
630 if (dist
< min_dist
) {
639 static int mt9m114_try_res(u32
*w
, u32
*h
)
643 if ((*w
> MT9M114_RES_960P_SIZE_H
)
644 || (*h
> MT9M114_RES_960P_SIZE_V
)) {
645 *w
= MT9M114_RES_960P_SIZE_H
;
646 *h
= MT9M114_RES_960P_SIZE_V
;
648 idx
= nearest_resolution_index(*w
, *h
);
651 * nearest_resolution_index() doesn't return smaller
652 * resolutions. If it fails, it means the requested
653 * resolution is higher than wecan support. Fallback
654 * to highest possible resolution in this case.
657 idx
= ARRAY_SIZE(mt9m114_res
) - 1;
659 *w
= mt9m114_res
[idx
].width
;
660 *h
= mt9m114_res
[idx
].height
;
666 static struct mt9m114_res_struct
*mt9m114_to_res(u32 w
, u32 h
)
670 for (index
= 0; index
< N_RES
; index
++) {
671 if ((mt9m114_res
[index
].width
== w
) &&
672 (mt9m114_res
[index
].height
== h
))
680 return &mt9m114_res
[index
];
683 static int mt9m114_res2size(struct v4l2_subdev
*sd
, int *h_size
, int *v_size
)
685 struct mt9m114_device
*dev
= to_mt9m114_sensor(sd
);
686 unsigned short hsize
;
687 unsigned short vsize
;
690 case MT9M114_RES_736P
:
691 hsize
= MT9M114_RES_736P_SIZE_H
;
692 vsize
= MT9M114_RES_736P_SIZE_V
;
694 case MT9M114_RES_864P
:
695 hsize
= MT9M114_RES_864P_SIZE_H
;
696 vsize
= MT9M114_RES_864P_SIZE_V
;
698 case MT9M114_RES_960P
:
699 hsize
= MT9M114_RES_960P_SIZE_H
;
700 vsize
= MT9M114_RES_960P_SIZE_V
;
703 v4l2_err(sd
, "%s: Resolution 0x%08x unknown\n", __func__
,
716 static int mt9m114_get_intg_factor(struct i2c_client
*client
,
717 struct camera_mipi_info
*info
,
718 const struct mt9m114_res_struct
*res
)
720 struct atomisp_sensor_mode_data
*buf
= &info
->data
;
727 ret
= mt9m114_read_reg(client
, MISENSOR_32BIT
,
728 REG_PIXEL_CLK
, ®_val
);
731 buf
->vt_pix_clk_freq_mhz
= reg_val
;
733 /* get integration time */
734 buf
->coarse_integration_time_min
= MT9M114_COARSE_INTG_TIME_MIN
;
735 buf
->coarse_integration_time_max_margin
=
736 MT9M114_COARSE_INTG_TIME_MAX_MARGIN
;
738 buf
->fine_integration_time_min
= MT9M114_FINE_INTG_TIME_MIN
;
739 buf
->fine_integration_time_max_margin
=
740 MT9M114_FINE_INTG_TIME_MAX_MARGIN
;
742 buf
->fine_integration_time_def
= MT9M114_FINE_INTG_TIME_MIN
;
744 buf
->frame_length_lines
= res
->lines_per_frame
;
745 buf
->line_length_pck
= res
->pixels_per_line
;
746 buf
->read_mode
= res
->bin_mode
;
748 /* get the cropping and output resolution to ISP for this mode. */
749 ret
= mt9m114_read_reg(client
, MISENSOR_16BIT
,
750 REG_H_START
, ®_val
);
753 buf
->crop_horizontal_start
= reg_val
;
755 ret
= mt9m114_read_reg(client
, MISENSOR_16BIT
,
756 REG_V_START
, ®_val
);
759 buf
->crop_vertical_start
= reg_val
;
761 ret
= mt9m114_read_reg(client
, MISENSOR_16BIT
,
762 REG_H_END
, ®_val
);
765 buf
->crop_horizontal_end
= reg_val
;
767 ret
= mt9m114_read_reg(client
, MISENSOR_16BIT
,
768 REG_V_END
, ®_val
);
771 buf
->crop_vertical_end
= reg_val
;
773 ret
= mt9m114_read_reg(client
, MISENSOR_16BIT
,
774 REG_WIDTH
, ®_val
);
777 buf
->output_width
= reg_val
;
779 ret
= mt9m114_read_reg(client
, MISENSOR_16BIT
,
780 REG_HEIGHT
, ®_val
);
783 buf
->output_height
= reg_val
;
785 ret
= mt9m114_read_reg(client
, MISENSOR_16BIT
,
786 REG_TIMING_HTS
, ®_val
);
789 buf
->line_length_pck
= reg_val
;
791 ret
= mt9m114_read_reg(client
, MISENSOR_16BIT
,
792 REG_TIMING_VTS
, ®_val
);
795 buf
->frame_length_lines
= reg_val
;
797 buf
->binning_factor_x
= res
->bin_factor_x
?
798 res
->bin_factor_x
: 1;
799 buf
->binning_factor_y
= res
->bin_factor_y
?
800 res
->bin_factor_y
: 1;
804 static int mt9m114_get_fmt(struct v4l2_subdev
*sd
,
805 struct v4l2_subdev_pad_config
*cfg
,
806 struct v4l2_subdev_format
*format
)
808 struct v4l2_mbus_framefmt
*fmt
= &format
->format
;
813 fmt
->code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
815 ret
= mt9m114_res2size(sd
, &width
, &height
);
819 fmt
->height
= height
;
824 static int mt9m114_set_fmt(struct v4l2_subdev
*sd
,
825 struct v4l2_subdev_pad_config
*cfg
,
826 struct v4l2_subdev_format
*format
)
828 struct v4l2_mbus_framefmt
*fmt
= &format
->format
;
829 struct i2c_client
*c
= v4l2_get_subdevdata(sd
);
830 struct mt9m114_device
*dev
= to_mt9m114_sensor(sd
);
831 struct mt9m114_res_struct
*res_index
;
832 u32 width
= fmt
->width
;
833 u32 height
= fmt
->height
;
834 struct camera_mipi_info
*mt9m114_info
= NULL
;
840 dev
->first_exp
= MT9M114_DEFAULT_FIRST_EXP
;
842 mt9m114_info
= v4l2_get_subdev_hostdata(sd
);
843 if (mt9m114_info
== NULL
)
846 mt9m114_try_res(&width
, &height
);
847 if (format
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
851 res_index
= mt9m114_to_res(width
, height
);
854 if (unlikely(!res_index
)) {
859 switch (res_index
->res
) {
860 case MT9M114_RES_736P
:
861 ret
= mt9m114_write_reg_array(c
, mt9m114_736P_init
, NO_POLLING
);
862 ret
+= misensor_rmw_reg(c
, MISENSOR_16BIT
, MISENSOR_READ_MODE
,
863 MISENSOR_R_MODE_MASK
, MISENSOR_NORMAL_SET
);
865 case MT9M114_RES_864P
:
866 ret
= mt9m114_write_reg_array(c
, mt9m114_864P_init
, NO_POLLING
);
867 ret
+= misensor_rmw_reg(c
, MISENSOR_16BIT
, MISENSOR_READ_MODE
,
868 MISENSOR_R_MODE_MASK
, MISENSOR_NORMAL_SET
);
870 case MT9M114_RES_960P
:
871 ret
= mt9m114_write_reg_array(c
, mt9m114_976P_init
, NO_POLLING
);
872 /* set sensor read_mode to Normal */
873 ret
+= misensor_rmw_reg(c
, MISENSOR_16BIT
, MISENSOR_READ_MODE
,
874 MISENSOR_R_MODE_MASK
, MISENSOR_NORMAL_SET
);
877 v4l2_err(sd
, "set resolution: %d failed!\n", res_index
->res
);
884 ret
= mt9m114_write_reg_array(c
, mt9m114_chgstat_reg
, POST_POLLING
);
888 if (mt9m114_set_suspend(sd
))
891 if (dev
->res
!= res_index
->res
) {
894 /* Switch to different size */
896 dev
->nctx
= 0x00; /* Set for context A */
899 * Context B is used for resolutions larger than 640x480
900 * Using YUV for Context B.
902 dev
->nctx
= 0x01; /* set for context B */
906 * Marked current sensor res as being "used"
908 * REVISIT: We don't need to use an "used" field on each mode
909 * list entry to know which mode is selected. If this
910 * information is really necessary, how about to use a single
911 * variable on sensor dev struct?
913 for (index
= 0; index
< N_RES
; index
++) {
914 if ((width
== mt9m114_res
[index
].width
) &&
915 (height
== mt9m114_res
[index
].height
)) {
916 mt9m114_res
[index
].used
= true;
919 mt9m114_res
[index
].used
= false;
922 ret
= mt9m114_get_intg_factor(c
, mt9m114_info
,
923 &mt9m114_res
[res_index
->res
]);
925 dev_err(&c
->dev
, "failed to get integration_factor\n");
929 * mt9m114 - we don't poll for context switch
930 * because it does not happen with streaming disabled.
932 dev
->res
= res_index
->res
;
935 fmt
->height
= height
;
936 fmt
->code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
940 /* TODO: Update to SOC functions, remove exposure and gain */
941 static int mt9m114_g_focal(struct v4l2_subdev
*sd
, s32
*val
)
943 *val
= (MT9M114_FOCAL_LENGTH_NUM
<< 16) | MT9M114_FOCAL_LENGTH_DEM
;
947 static int mt9m114_g_fnumber(struct v4l2_subdev
*sd
, s32
*val
)
949 /*const f number for mt9m114*/
950 *val
= (MT9M114_F_NUMBER_DEFAULT_NUM
<< 16) | MT9M114_F_NUMBER_DEM
;
954 static int mt9m114_g_fnumber_range(struct v4l2_subdev
*sd
, s32
*val
)
956 *val
= (MT9M114_F_NUMBER_DEFAULT_NUM
<< 24) |
957 (MT9M114_F_NUMBER_DEM
<< 16) |
958 (MT9M114_F_NUMBER_DEFAULT_NUM
<< 8) | MT9M114_F_NUMBER_DEM
;
962 /* Horizontal flip the image. */
963 static int mt9m114_g_hflip(struct v4l2_subdev
*sd
, s32
*val
)
965 struct i2c_client
*c
= v4l2_get_subdevdata(sd
);
968 ret
= mt9m114_read_reg(c
, MISENSOR_16BIT
,
969 (u32
)MISENSOR_READ_MODE
, &data
);
972 *val
= !!(data
& MISENSOR_HFLIP_MASK
);
977 static int mt9m114_g_vflip(struct v4l2_subdev
*sd
, s32
*val
)
979 struct i2c_client
*c
= v4l2_get_subdevdata(sd
);
983 ret
= mt9m114_read_reg(c
, MISENSOR_16BIT
,
984 (u32
)MISENSOR_READ_MODE
, &data
);
987 *val
= !!(data
& MISENSOR_VFLIP_MASK
);
992 static long mt9m114_s_exposure(struct v4l2_subdev
*sd
,
993 struct atomisp_exposure
*exposure
)
995 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
996 struct mt9m114_device
*dev
= to_mt9m114_sensor(sd
);
998 unsigned int coarse_integration
= 0;
999 unsigned int fine_integration
= 0;
1000 unsigned int FLines
= 0;
1001 unsigned int FrameLengthLines
= 0; /* ExposureTime.FrameLengthLines; */
1002 unsigned int AnalogGain
, DigitalGain
;
1003 u32 AnalogGainToWrite
= 0;
1004 u16 exposure_local
[3];
1006 dev_dbg(&client
->dev
, "%s(0x%X 0x%X 0x%X)\n", __func__
,
1007 exposure
->integration_time
[0], exposure
->gain
[0],
1010 coarse_integration
= exposure
->integration_time
[0];
1011 /* fine_integration = ExposureTime.FineIntegrationTime; */
1012 /* FrameLengthLines = ExposureTime.FrameLengthLines; */
1013 FLines
= mt9m114_res
[dev
->res
].lines_per_frame
;
1014 AnalogGain
= exposure
->gain
[0];
1015 DigitalGain
= exposure
->gain
[1];
1016 if (!dev
->streamon
) {
1017 /*Save the first exposure values while stream is off*/
1018 dev
->first_exp
= coarse_integration
;
1019 dev
->first_gain
= AnalogGain
;
1020 dev
->first_diggain
= DigitalGain
;
1022 /* DigitalGain = 0x400 * (((u16) DigitalGain) >> 8) +
1023 ((unsigned int)(0x400 * (((u16) DigitalGain) & 0xFF)) >>8); */
1025 /* set frame length */
1026 if (FLines
< coarse_integration
+ 6)
1027 FLines
= coarse_integration
+ 6;
1028 if (FLines
< FrameLengthLines
)
1029 FLines
= FrameLengthLines
;
1030 ret
= mt9m114_write_reg(client
, MISENSOR_16BIT
, 0x300A, FLines
);
1032 v4l2_err(client
, "%s: fail to set FLines\n", __func__
);
1036 /* set coarse/fine integration */
1037 exposure_local
[0] = REG_EXPO_COARSE
;
1038 exposure_local
[1] = (u16
)coarse_integration
;
1039 exposure_local
[2] = (u16
)fine_integration
;
1040 /* 3A provide real exposure time.
1041 should not translate to any value here. */
1042 ret
= mt9m114_write_reg(client
, MISENSOR_16BIT
,
1043 REG_EXPO_COARSE
, (u16
)(coarse_integration
));
1045 v4l2_err(client
, "%s: fail to set exposure time\n", __func__
);
1050 // set analog/digital gain
1054 AnalogGainToWrite = 0x0;
1057 AnalogGainToWrite = 0x20;
1060 AnalogGainToWrite = 0x60;
1063 AnalogGainToWrite = 0xA0;
1066 AnalogGainToWrite = 0xE0;
1069 AnalogGainToWrite = 0x20;
1073 if (DigitalGain
>= 16 || DigitalGain
<= 1)
1075 /* AnalogGainToWrite =
1076 (u16)((DigitalGain << 12) | AnalogGainToWrite); */
1077 AnalogGainToWrite
= (u16
)((DigitalGain
<< 12) | (u16
)AnalogGain
);
1078 ret
= mt9m114_write_reg(client
, MISENSOR_16BIT
,
1079 REG_GAIN
, AnalogGainToWrite
);
1081 v4l2_err(client
, "%s: fail to set AnalogGainToWrite\n",
1089 static long mt9m114_ioctl(struct v4l2_subdev
*sd
, unsigned int cmd
, void *arg
)
1093 case ATOMISP_IOC_S_EXPOSURE
:
1094 return mt9m114_s_exposure(sd
, arg
);
1102 /* This returns the exposure time being used. This should only be used
1103 for filling in EXIF data, not for actual image processing. */
1104 static int mt9m114_g_exposure(struct v4l2_subdev
*sd
, s32
*value
)
1106 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1110 /* the fine integration time is currently not calculated */
1111 ret
= mt9m114_read_reg(client
, MISENSOR_16BIT
,
1112 REG_EXPO_COARSE
, &coarse
);
1121 * This function will return the sensor supported max exposure zone number.
1122 * the sensor which supports max exposure zone number is 1.
1124 static int mt9m114_g_exposure_zone_num(struct v4l2_subdev
*sd
, s32
*val
)
1132 * set exposure metering, average/center_weighted/spot/matrix.
1134 static int mt9m114_s_exposure_metering(struct v4l2_subdev
*sd
, s32 val
)
1136 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1140 case V4L2_EXPOSURE_METERING_SPOT
:
1141 ret
= mt9m114_write_reg_array(client
, mt9m114_exp_average
,
1144 dev_err(&client
->dev
, "write exp_average reg err.\n");
1148 case V4L2_EXPOSURE_METERING_CENTER_WEIGHTED
:
1150 ret
= mt9m114_write_reg_array(client
, mt9m114_exp_center
,
1153 dev_err(&client
->dev
, "write exp_default reg err");
1162 * This function is for touch exposure feature.
1164 static int mt9m114_s_exposure_selection(struct v4l2_subdev
*sd
,
1165 struct v4l2_subdev_pad_config
*cfg
,
1166 struct v4l2_subdev_selection
*sel
)
1168 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1169 struct misensor_reg exp_reg
;
1171 int grid_width
, grid_height
;
1172 int grid_left
, grid_top
, grid_right
, grid_bottom
;
1173 int win_left
, win_top
, win_right
, win_bottom
;
1177 if (sel
->which
!= V4L2_SUBDEV_FORMAT_TRY
&&
1178 sel
->which
!= V4L2_SUBDEV_FORMAT_ACTIVE
)
1181 grid_left
= sel
->r
.left
;
1182 grid_top
= sel
->r
.top
;
1183 grid_right
= sel
->r
.left
+ sel
->r
.width
- 1;
1184 grid_bottom
= sel
->r
.top
+ sel
->r
.height
- 1;
1186 ret
= mt9m114_res2size(sd
, &width
, &height
);
1190 grid_width
= width
/ 5;
1191 grid_height
= height
/ 5;
1193 if (grid_width
&& grid_height
) {
1194 win_left
= grid_left
/ grid_width
;
1195 win_top
= grid_top
/ grid_height
;
1196 win_right
= grid_right
/ grid_width
;
1197 win_bottom
= grid_bottom
/ grid_height
;
1199 dev_err(&client
->dev
, "Incorrect exp grid.\n");
1203 win_left
= clamp_t(int, win_left
, 0, 4);
1204 win_top
= clamp_t(int, win_top
, 0, 4);
1205 win_right
= clamp_t(int, win_right
, 0, 4);
1206 win_bottom
= clamp_t(int, win_bottom
, 0, 4);
1208 ret
= mt9m114_write_reg_array(client
, mt9m114_exp_average
, NO_POLLING
);
1210 dev_err(&client
->dev
, "write exp_average reg err.\n");
1214 for (i
= win_top
; i
<= win_bottom
; i
++) {
1215 for (j
= win_left
; j
<= win_right
; j
++) {
1216 exp_reg
= mt9m114_exp_win
[i
][j
];
1218 ret
= mt9m114_write_reg(client
, exp_reg
.length
,
1219 exp_reg
.reg
, exp_reg
.val
);
1221 dev_err(&client
->dev
, "write exp_reg err.\n");
1231 static int mt9m114_g_bin_factor_x(struct v4l2_subdev
*sd
, s32
*val
)
1233 struct mt9m114_device
*dev
= to_mt9m114_sensor(sd
);
1235 *val
= mt9m114_res
[dev
->res
].bin_factor_x
;
1240 static int mt9m114_g_bin_factor_y(struct v4l2_subdev
*sd
, s32
*val
)
1242 struct mt9m114_device
*dev
= to_mt9m114_sensor(sd
);
1244 *val
= mt9m114_res
[dev
->res
].bin_factor_y
;
1249 static int mt9m114_s_ev(struct v4l2_subdev
*sd
, s32 val
)
1251 struct i2c_client
*c
= v4l2_get_subdevdata(sd
);
1255 /* EV value only support -2 to 2
1256 * 0: 0x37, 1:0x47, 2:0x57, -1:0x27, -2:0x17
1258 if (val
< -2 || val
> 2)
1261 dev_dbg(&c
->dev
, "%s val:%d luma:0x%x\n", __func__
, val
, luma
);
1262 err
= mt9m114_write_reg(c
, MISENSOR_16BIT
, 0x098E, 0xC87A);
1264 dev_err(&c
->dev
, "%s logic addr access error\n", __func__
);
1267 err
= mt9m114_write_reg(c
, MISENSOR_8BIT
, 0xC87A, (u32
)luma
);
1269 dev_err(&c
->dev
, "%s write target_average_luma failed\n",
1278 static int mt9m114_g_ev(struct v4l2_subdev
*sd
, s32
*val
)
1280 struct i2c_client
*c
= v4l2_get_subdevdata(sd
);
1284 err
= mt9m114_write_reg(c
, MISENSOR_16BIT
, 0x098E, 0xC87A);
1286 dev_err(&c
->dev
, "%s logic addr access error\n", __func__
);
1289 err
= mt9m114_read_reg(c
, MISENSOR_8BIT
, 0xC87A, &luma
);
1291 dev_err(&c
->dev
, "%s read target_average_luma failed\n",
1297 *val
= (s32
)luma
- 2;
1298 dev_dbg(&c
->dev
, "%s val:%d\n", __func__
, *val
);
1304 * mt9m114 now can not support 3a_lock
1306 static int mt9m114_s_3a_lock(struct v4l2_subdev
*sd
, s32 val
)
1312 static int mt9m114_g_3a_lock(struct v4l2_subdev
*sd
, s32
*val
)
1315 return V4L2_LOCK_EXPOSURE
| V4L2_LOCK_WHITE_BALANCE
1320 static int mt9m114_s_ctrl(struct v4l2_ctrl
*ctrl
)
1322 struct mt9m114_device
*dev
=
1323 container_of(ctrl
->handler
, struct mt9m114_device
, ctrl_handler
);
1324 struct i2c_client
*client
= v4l2_get_subdevdata(&dev
->sd
);
1328 case V4L2_CID_VFLIP
:
1329 dev_dbg(&client
->dev
, "%s: CID_VFLIP:%d.\n",
1330 __func__
, ctrl
->val
);
1331 ret
= mt9m114_t_vflip(&dev
->sd
, ctrl
->val
);
1333 case V4L2_CID_HFLIP
:
1334 dev_dbg(&client
->dev
, "%s: CID_HFLIP:%d.\n",
1335 __func__
, ctrl
->val
);
1336 ret
= mt9m114_t_hflip(&dev
->sd
, ctrl
->val
);
1339 case V4L2_CID_EXPOSURE_METERING
:
1340 ret
= mt9m114_s_exposure_metering(&dev
->sd
, ctrl
->val
);
1343 case V4L2_CID_EXPOSURE
:
1344 ret
= mt9m114_s_ev(&dev
->sd
, ctrl
->val
);
1346 case V4L2_CID_3A_LOCK
:
1347 ret
= mt9m114_s_3a_lock(&dev
->sd
, ctrl
->val
);
1355 static int mt9m114_g_volatile_ctrl(struct v4l2_ctrl
*ctrl
)
1357 struct mt9m114_device
*dev
=
1358 container_of(ctrl
->handler
, struct mt9m114_device
, ctrl_handler
);
1362 case V4L2_CID_VFLIP
:
1363 ret
= mt9m114_g_vflip(&dev
->sd
, &ctrl
->val
);
1365 case V4L2_CID_HFLIP
:
1366 ret
= mt9m114_g_hflip(&dev
->sd
, &ctrl
->val
);
1368 case V4L2_CID_FOCAL_ABSOLUTE
:
1369 ret
= mt9m114_g_focal(&dev
->sd
, &ctrl
->val
);
1371 case V4L2_CID_FNUMBER_ABSOLUTE
:
1372 ret
= mt9m114_g_fnumber(&dev
->sd
, &ctrl
->val
);
1374 case V4L2_CID_FNUMBER_RANGE
:
1375 ret
= mt9m114_g_fnumber_range(&dev
->sd
, &ctrl
->val
);
1377 case V4L2_CID_EXPOSURE_ABSOLUTE
:
1378 ret
= mt9m114_g_exposure(&dev
->sd
, &ctrl
->val
);
1381 case V4L2_CID_EXPOSURE_ZONE_NUM
:
1382 ret
= mt9m114_g_exposure_zone_num(&dev
->sd
, &ctrl
->val
);
1385 case V4L2_CID_BIN_FACTOR_HORZ
:
1386 ret
= mt9m114_g_bin_factor_x(&dev
->sd
, &ctrl
->val
);
1388 case V4L2_CID_BIN_FACTOR_VERT
:
1389 ret
= mt9m114_g_bin_factor_y(&dev
->sd
, &ctrl
->val
);
1391 case V4L2_CID_EXPOSURE
:
1392 ret
= mt9m114_g_ev(&dev
->sd
, &ctrl
->val
);
1394 case V4L2_CID_3A_LOCK
:
1395 ret
= mt9m114_g_3a_lock(&dev
->sd
, &ctrl
->val
);
1404 static const struct v4l2_ctrl_ops ctrl_ops
= {
1405 .s_ctrl
= mt9m114_s_ctrl
,
1406 .g_volatile_ctrl
= mt9m114_g_volatile_ctrl
1409 static struct v4l2_ctrl_config mt9m114_controls
[] = {
1412 .id
= V4L2_CID_VFLIP
,
1413 .name
= "Image v-Flip",
1414 .type
= V4L2_CTRL_TYPE_INTEGER
,
1422 .id
= V4L2_CID_HFLIP
,
1423 .name
= "Image h-Flip",
1424 .type
= V4L2_CTRL_TYPE_INTEGER
,
1432 .id
= V4L2_CID_FOCAL_ABSOLUTE
,
1433 .name
= "focal length",
1434 .type
= V4L2_CTRL_TYPE_INTEGER
,
1435 .min
= MT9M114_FOCAL_LENGTH_DEFAULT
,
1436 .max
= MT9M114_FOCAL_LENGTH_DEFAULT
,
1438 .def
= MT9M114_FOCAL_LENGTH_DEFAULT
,
1443 .id
= V4L2_CID_FNUMBER_ABSOLUTE
,
1445 .type
= V4L2_CTRL_TYPE_INTEGER
,
1446 .min
= MT9M114_F_NUMBER_DEFAULT
,
1447 .max
= MT9M114_F_NUMBER_DEFAULT
,
1449 .def
= MT9M114_F_NUMBER_DEFAULT
,
1454 .id
= V4L2_CID_FNUMBER_RANGE
,
1455 .name
= "f-number range",
1456 .type
= V4L2_CTRL_TYPE_INTEGER
,
1457 .min
= MT9M114_F_NUMBER_RANGE
,
1458 .max
= MT9M114_F_NUMBER_RANGE
,
1460 .def
= MT9M114_F_NUMBER_RANGE
,
1465 .id
= V4L2_CID_EXPOSURE_ABSOLUTE
,
1467 .type
= V4L2_CTRL_TYPE_INTEGER
,
1477 .id
= V4L2_CID_EXPOSURE_ZONE_NUM
,
1478 .name
= "one-time exposure zone number",
1479 .type
= V4L2_CTRL_TYPE_INTEGER
,
1488 .id
= V4L2_CID_EXPOSURE_METERING
,
1490 .type
= V4L2_CTRL_TYPE_MENU
,
1500 .id
= V4L2_CID_BIN_FACTOR_HORZ
,
1501 .name
= "horizontal binning factor",
1502 .type
= V4L2_CTRL_TYPE_INTEGER
,
1504 .max
= MT9M114_BIN_FACTOR_MAX
,
1511 .id
= V4L2_CID_BIN_FACTOR_VERT
,
1512 .name
= "vertical binning factor",
1513 .type
= V4L2_CTRL_TYPE_INTEGER
,
1515 .max
= MT9M114_BIN_FACTOR_MAX
,
1522 .id
= V4L2_CID_EXPOSURE
,
1523 .name
= "exposure biasx",
1524 .type
= V4L2_CTRL_TYPE_INTEGER
,
1533 .id
= V4L2_CID_3A_LOCK
,
1535 .type
= V4L2_CTRL_TYPE_BITMASK
,
1537 .max
= V4L2_LOCK_EXPOSURE
| V4L2_LOCK_WHITE_BALANCE
| V4L2_LOCK_FOCUS
,
1544 static int mt9m114_detect(struct mt9m114_device
*dev
, struct i2c_client
*client
)
1546 struct i2c_adapter
*adapter
= client
->adapter
;
1549 if (!i2c_check_functionality(adapter
, I2C_FUNC_I2C
)) {
1550 dev_err(&client
->dev
, "%s: i2c error", __func__
);
1553 mt9m114_read_reg(client
, MISENSOR_16BIT
, (u32
)MT9M114_PID
, &retvalue
);
1554 dev
->real_model_id
= retvalue
;
1556 if (retvalue
!= MT9M114_MOD_ID
) {
1557 dev_err(&client
->dev
, "%s: failed: client->addr = %x\n",
1558 __func__
, client
->addr
);
1566 mt9m114_s_config(struct v4l2_subdev
*sd
, int irq
, void *platform_data
)
1568 struct mt9m114_device
*dev
= to_mt9m114_sensor(sd
);
1569 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1572 if (NULL
== platform_data
)
1575 dev
->platform_data
=
1576 (struct camera_sensor_platform_data
*)platform_data
;
1580 v4l2_err(client
, "mt9m114 power-up err");
1584 /* config & detect sensor */
1585 ret
= mt9m114_detect(dev
, client
);
1587 v4l2_err(client
, "mt9m114_detect err s_config.\n");
1591 ret
= dev
->platform_data
->csi_cfg(sd
, 1);
1595 ret
= mt9m114_set_suspend(sd
);
1597 v4l2_err(client
, "mt9m114 suspend err");
1601 ret
= power_down(sd
);
1603 v4l2_err(client
, "mt9m114 power down err");
1610 dev
->platform_data
->csi_cfg(sd
, 0);
1613 dev_err(&client
->dev
, "sensor power-gating failed\n");
1617 /* Horizontal flip the image. */
1618 static int mt9m114_t_hflip(struct v4l2_subdev
*sd
, int value
)
1620 struct i2c_client
*c
= v4l2_get_subdevdata(sd
);
1621 struct mt9m114_device
*dev
= to_mt9m114_sensor(sd
);
1623 /* set for direct mode */
1624 err
= mt9m114_write_reg(c
, MISENSOR_16BIT
, 0x098E, 0xC850);
1626 /* enable H flip ctx A */
1627 err
+= misensor_rmw_reg(c
, MISENSOR_8BIT
, 0xC850, 0x01, 0x01);
1628 err
+= misensor_rmw_reg(c
, MISENSOR_8BIT
, 0xC851, 0x01, 0x01);
1630 err
+= misensor_rmw_reg(c
, MISENSOR_8BIT
, 0xC888, 0x01, 0x01);
1631 err
+= misensor_rmw_reg(c
, MISENSOR_8BIT
, 0xC889, 0x01, 0x01);
1633 err
+= misensor_rmw_reg(c
, MISENSOR_16BIT
, MISENSOR_READ_MODE
,
1634 MISENSOR_HFLIP_MASK
, MISENSOR_FLIP_EN
);
1636 dev
->bpat
= MT9M114_BPAT_GRGRBGBG
;
1638 /* disable H flip ctx A */
1639 err
+= misensor_rmw_reg(c
, MISENSOR_8BIT
, 0xC850, 0x01, 0x00);
1640 err
+= misensor_rmw_reg(c
, MISENSOR_8BIT
, 0xC851, 0x01, 0x00);
1642 err
+= misensor_rmw_reg(c
, MISENSOR_8BIT
, 0xC888, 0x01, 0x00);
1643 err
+= misensor_rmw_reg(c
, MISENSOR_8BIT
, 0xC889, 0x01, 0x00);
1645 err
+= misensor_rmw_reg(c
, MISENSOR_16BIT
, MISENSOR_READ_MODE
,
1646 MISENSOR_HFLIP_MASK
, MISENSOR_FLIP_DIS
);
1648 dev
->bpat
= MT9M114_BPAT_BGBGGRGR
;
1651 err
+= mt9m114_write_reg(c
, MISENSOR_8BIT
, 0x8404, 0x06);
1657 /* Vertically flip the image */
1658 static int mt9m114_t_vflip(struct v4l2_subdev
*sd
, int value
)
1660 struct i2c_client
*c
= v4l2_get_subdevdata(sd
);
1662 /* set for direct mode */
1663 err
= mt9m114_write_reg(c
, MISENSOR_16BIT
, 0x098E, 0xC850);
1665 /* enable H flip - ctx A */
1666 err
+= misensor_rmw_reg(c
, MISENSOR_8BIT
, 0xC850, 0x02, 0x01);
1667 err
+= misensor_rmw_reg(c
, MISENSOR_8BIT
, 0xC851, 0x02, 0x01);
1669 err
+= misensor_rmw_reg(c
, MISENSOR_8BIT
, 0xC888, 0x02, 0x01);
1670 err
+= misensor_rmw_reg(c
, MISENSOR_8BIT
, 0xC889, 0x02, 0x01);
1672 err
+= misensor_rmw_reg(c
, MISENSOR_16BIT
, MISENSOR_READ_MODE
,
1673 MISENSOR_VFLIP_MASK
, MISENSOR_FLIP_EN
);
1675 /* disable H flip - ctx A */
1676 err
+= misensor_rmw_reg(c
, MISENSOR_8BIT
, 0xC850, 0x02, 0x00);
1677 err
+= misensor_rmw_reg(c
, MISENSOR_8BIT
, 0xC851, 0x02, 0x00);
1679 err
+= misensor_rmw_reg(c
, MISENSOR_8BIT
, 0xC888, 0x02, 0x00);
1680 err
+= misensor_rmw_reg(c
, MISENSOR_8BIT
, 0xC889, 0x02, 0x00);
1682 err
+= misensor_rmw_reg(c
, MISENSOR_16BIT
, MISENSOR_READ_MODE
,
1683 MISENSOR_VFLIP_MASK
, MISENSOR_FLIP_DIS
);
1686 err
+= mt9m114_write_reg(c
, MISENSOR_8BIT
, 0x8404, 0x06);
1691 static int mt9m114_s_parm(struct v4l2_subdev
*sd
,
1692 struct v4l2_streamparm
*param
)
1697 static int mt9m114_g_frame_interval(struct v4l2_subdev
*sd
,
1698 struct v4l2_subdev_frame_interval
*interval
)
1700 struct mt9m114_device
*dev
= to_mt9m114_sensor(sd
);
1702 interval
->interval
.numerator
= 1;
1703 interval
->interval
.denominator
= mt9m114_res
[dev
->res
].fps
;
1708 static int mt9m114_s_stream(struct v4l2_subdev
*sd
, int enable
)
1711 struct i2c_client
*c
= v4l2_get_subdevdata(sd
);
1712 struct mt9m114_device
*dev
= to_mt9m114_sensor(sd
);
1713 struct atomisp_exposure exposure
;
1716 ret
= mt9m114_write_reg_array(c
, mt9m114_chgstat_reg
,
1721 if (dev
->first_exp
> MT9M114_MAX_FIRST_EXP
) {
1722 exposure
.integration_time
[0] = dev
->first_exp
;
1723 exposure
.gain
[0] = dev
->first_gain
;
1724 exposure
.gain
[1] = dev
->first_diggain
;
1725 mt9m114_s_exposure(sd
, &exposure
);
1731 ret
= mt9m114_set_suspend(sd
);
1737 static int mt9m114_enum_mbus_code(struct v4l2_subdev
*sd
,
1738 struct v4l2_subdev_pad_config
*cfg
,
1739 struct v4l2_subdev_mbus_code_enum
*code
)
1743 code
->code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
1748 static int mt9m114_enum_frame_size(struct v4l2_subdev
*sd
,
1749 struct v4l2_subdev_pad_config
*cfg
,
1750 struct v4l2_subdev_frame_size_enum
*fse
)
1753 unsigned int index
= fse
->index
;
1758 fse
->min_width
= mt9m114_res
[index
].width
;
1759 fse
->min_height
= mt9m114_res
[index
].height
;
1760 fse
->max_width
= mt9m114_res
[index
].width
;
1761 fse
->max_height
= mt9m114_res
[index
].height
;
1766 static int mt9m114_g_skip_frames(struct v4l2_subdev
*sd
, u32
*frames
)
1769 struct mt9m114_device
*snr
= to_mt9m114_sensor(sd
);
1774 for (index
= 0; index
< N_RES
; index
++) {
1775 if (mt9m114_res
[index
].res
== snr
->res
)
1782 *frames
= mt9m114_res
[index
].skip_frames
;
1787 static const struct v4l2_subdev_video_ops mt9m114_video_ops
= {
1788 .s_parm
= mt9m114_s_parm
,
1789 .s_stream
= mt9m114_s_stream
,
1790 .g_frame_interval
= mt9m114_g_frame_interval
,
1793 static const struct v4l2_subdev_sensor_ops mt9m114_sensor_ops
= {
1794 .g_skip_frames
= mt9m114_g_skip_frames
,
1797 static const struct v4l2_subdev_core_ops mt9m114_core_ops
= {
1798 .s_power
= mt9m114_s_power
,
1799 .ioctl
= mt9m114_ioctl
,
1802 /* REVISIT: Do we need pad operations? */
1803 static const struct v4l2_subdev_pad_ops mt9m114_pad_ops
= {
1804 .enum_mbus_code
= mt9m114_enum_mbus_code
,
1805 .enum_frame_size
= mt9m114_enum_frame_size
,
1806 .get_fmt
= mt9m114_get_fmt
,
1807 .set_fmt
= mt9m114_set_fmt
,
1809 .set_selection
= mt9m114_s_exposure_selection
,
1813 static const struct v4l2_subdev_ops mt9m114_ops
= {
1814 .core
= &mt9m114_core_ops
,
1815 .video
= &mt9m114_video_ops
,
1816 .pad
= &mt9m114_pad_ops
,
1817 .sensor
= &mt9m114_sensor_ops
,
1820 static const struct media_entity_operations mt9m114_entity_ops
= {
1824 static int mt9m114_remove(struct i2c_client
*client
)
1826 struct mt9m114_device
*dev
;
1827 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1829 dev
= container_of(sd
, struct mt9m114_device
, sd
);
1830 dev
->platform_data
->csi_cfg(sd
, 0);
1831 v4l2_device_unregister_subdev(sd
);
1832 media_entity_cleanup(&dev
->sd
.entity
);
1833 v4l2_ctrl_handler_free(&dev
->ctrl_handler
);
1838 static int mt9m114_probe(struct i2c_client
*client
)
1840 struct mt9m114_device
*dev
;
1845 /* Setup sensor configuration structure */
1846 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
1850 v4l2_i2c_subdev_init(&dev
->sd
, client
, &mt9m114_ops
);
1851 pdata
= client
->dev
.platform_data
;
1852 if (ACPI_COMPANION(&client
->dev
))
1853 pdata
= gmin_camera_platform_data(&dev
->sd
,
1854 ATOMISP_INPUT_FORMAT_RAW_10
,
1855 atomisp_bayer_order_grbg
);
1857 ret
= mt9m114_s_config(&dev
->sd
, client
->irq
, pdata
);
1858 if (!pdata
|| ret
) {
1859 v4l2_device_unregister_subdev(&dev
->sd
);
1864 ret
= atomisp_register_i2c_module(&dev
->sd
, pdata
, RAW_CAMERA
);
1866 v4l2_device_unregister_subdev(&dev
->sd
);
1868 /* Coverity CID 298095 - return on error */
1872 /*TODO add format code here*/
1873 dev
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
1874 dev
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
1875 dev
->format
.code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
1876 dev
->sd
.entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
1879 v4l2_ctrl_handler_init(&dev
->ctrl_handler
,
1880 ARRAY_SIZE(mt9m114_controls
));
1882 mt9m114_remove(client
);
1886 for (i
= 0; i
< ARRAY_SIZE(mt9m114_controls
); i
++)
1887 v4l2_ctrl_new_custom(&dev
->ctrl_handler
, &mt9m114_controls
[i
],
1890 if (dev
->ctrl_handler
.error
) {
1891 mt9m114_remove(client
);
1892 return dev
->ctrl_handler
.error
;
1895 /* Use same lock for controls as for everything else. */
1896 dev
->ctrl_handler
.lock
= &dev
->input_lock
;
1897 dev
->sd
.ctrl_handler
= &dev
->ctrl_handler
;
1899 /* REVISIT: Do we need media controller? */
1900 ret
= media_entity_pads_init(&dev
->sd
.entity
, 1, &dev
->pad
);
1902 mt9m114_remove(client
);
1908 static const struct acpi_device_id mt9m114_acpi_match
[] = {
1913 MODULE_DEVICE_TABLE(acpi
, mt9m114_acpi_match
);
1915 static struct i2c_driver mt9m114_driver
= {
1918 .acpi_match_table
= mt9m114_acpi_match
,
1920 .probe_new
= mt9m114_probe
,
1921 .remove
= mt9m114_remove
,
1923 module_i2c_driver(mt9m114_driver
);
1925 MODULE_AUTHOR("Shuguang Gong <Shuguang.gong@intel.com>");
1926 MODULE_LICENSE("GPL");