2 * Copyright (C) 2008-2009 QUALCOMM Incorporated.
5 #include <linux/delay.h>
6 #include <linux/types.h>
7 #include <linux/slab.h>
9 #include <linux/uaccess.h>
10 #include <linux/miscdevice.h>
11 #include <linux/kernel.h>
12 #include <media/msm_camera.h>
13 #include <mach/gpio.h>
14 #include <mach/camera.h>
15 #include <asm/mach-types.h>
18 /*=============================================================
19 SENSOR REGISTER DEFINES
20 ==============================================================*/
21 #define MT9T013_REG_MODEL_ID 0x0000
22 #define MT9T013_MODEL_ID 0x2600
23 #define REG_GROUPED_PARAMETER_HOLD 0x0104
24 #define GROUPED_PARAMETER_HOLD 0x0100
25 #define GROUPED_PARAMETER_UPDATE 0x0000
26 #define REG_COARSE_INT_TIME 0x3012
27 #define REG_VT_PIX_CLK_DIV 0x0300
28 #define REG_VT_SYS_CLK_DIV 0x0302
29 #define REG_PRE_PLL_CLK_DIV 0x0304
30 #define REG_PLL_MULTIPLIER 0x0306
31 #define REG_OP_PIX_CLK_DIV 0x0308
32 #define REG_OP_SYS_CLK_DIV 0x030A
33 #define REG_SCALE_M 0x0404
34 #define REG_FRAME_LENGTH_LINES 0x300A
35 #define REG_LINE_LENGTH_PCK 0x300C
36 #define REG_X_ADDR_START 0x3004
37 #define REG_Y_ADDR_START 0x3002
38 #define REG_X_ADDR_END 0x3008
39 #define REG_Y_ADDR_END 0x3006
40 #define REG_X_OUTPUT_SIZE 0x034C
41 #define REG_Y_OUTPUT_SIZE 0x034E
42 #define REG_FINE_INT_TIME 0x3014
43 #define REG_ROW_SPEED 0x3016
44 #define MT9T013_REG_RESET_REGISTER 0x301A
45 #define MT9T013_RESET_REGISTER_PWON 0x10CC
46 #define MT9T013_RESET_REGISTER_PWOFF 0x1008 /* 0x10C8 stop streaming*/
47 #define REG_READ_MODE 0x3040
48 #define REG_GLOBAL_GAIN 0x305E
49 #define REG_TEST_PATTERN_MODE 0x3070
52 enum mt9t013_test_mode
{
59 enum mt9t013_resolution
{
65 enum mt9t013_reg_update
{
66 REG_INIT
, /* registers that need to be updated during initialization */
67 UPDATE_PERIODIC
, /* registers that needs periodic I2C writes */
68 UPDATE_ALL
, /* all registers will be updated */
72 enum mt9t013_setting
{
77 /* actuator's Slave Address */
78 #define MT9T013_AF_I2C_ADDR 0x18
81 * AF Total steps parameters
83 #define MT9T013_TOTAL_STEPS_NEAR_TO_FAR 30
86 * Time in milisecs for waiting for the sensor to reset.
88 #define MT9T013_RESET_DELAY_MSECS 66
90 /* for 30 fps preview */
91 #define MT9T013_DEFAULT_CLOCK_RATE 24000000
92 #define MT9T013_DEFAULT_MAX_FPS 26
95 /* FIXME: Changes from here */
97 struct work_struct work
;
100 static struct mt9t013_work
*mt9t013_sensorw
;
101 static struct i2c_client
*mt9t013_client
;
103 struct mt9t013_ctrl
{
104 const struct msm_camera_sensor_info
*sensordata
;
107 uint32_t fps_divider
; /* init to 1 * 0x00000400 */
108 uint32_t pict_fps_divider
; /* init to 1 * 0x00000400 */
110 uint16_t curr_lens_pos
;
111 uint16_t init_curr_lens_pos
;
112 uint16_t my_reg_gain
;
113 uint32_t my_reg_line_count
;
115 enum mt9t013_resolution prev_res
;
116 enum mt9t013_resolution pict_res
;
117 enum mt9t013_resolution curr_res
;
118 enum mt9t013_test_mode set_test
;
120 unsigned short imgaddr
;
124 static struct mt9t013_ctrl
*mt9t013_ctrl
;
125 static DECLARE_WAIT_QUEUE_HEAD(mt9t013_wait_queue
);
126 DECLARE_MUTEX(mt9t013_sem
);
128 extern struct mt9t013_reg mt9t013_regs
; /* from mt9t013_reg.c */
130 static int mt9t013_i2c_rxdata(unsigned short saddr
,
131 unsigned char *rxdata
, int length
)
133 struct i2c_msg msgs
[] = {
148 if (i2c_transfer(mt9t013_client
->adapter
, msgs
, 2) < 0) {
149 pr_err("mt9t013_i2c_rxdata failed!\n");
156 static int32_t mt9t013_i2c_read_w(unsigned short saddr
,
157 unsigned short raddr
, unsigned short *rdata
)
160 unsigned char buf
[4];
165 memset(buf
, 0, sizeof(buf
));
167 buf
[0] = (raddr
& 0xFF00)>>8;
168 buf
[1] = (raddr
& 0x00FF);
170 rc
= mt9t013_i2c_rxdata(saddr
, buf
, 2);
174 *rdata
= buf
[0] << 8 | buf
[1];
177 pr_err("mt9t013_i2c_read failed!\n");
182 static int32_t mt9t013_i2c_txdata(unsigned short saddr
,
183 unsigned char *txdata
, int length
)
185 struct i2c_msg msg
[] = {
194 if (i2c_transfer(mt9t013_client
->adapter
, msg
, 1) < 0) {
195 pr_err("mt9t013_i2c_txdata failed\n");
202 static int32_t mt9t013_i2c_write_b(unsigned short saddr
,
203 unsigned short waddr
, unsigned short wdata
)
206 unsigned char buf
[2];
208 memset(buf
, 0, sizeof(buf
));
211 rc
= mt9t013_i2c_txdata(saddr
, buf
, 2);
214 pr_err("i2c_write failed, addr = 0x%x, val = 0x%x!\n",
220 static int32_t mt9t013_i2c_write_w(unsigned short saddr
,
221 unsigned short waddr
, unsigned short wdata
)
224 unsigned char buf
[4];
226 memset(buf
, 0, sizeof(buf
));
227 buf
[0] = (waddr
& 0xFF00)>>8;
228 buf
[1] = (waddr
& 0x00FF);
229 buf
[2] = (wdata
& 0xFF00)>>8;
230 buf
[3] = (wdata
& 0x00FF);
232 rc
= mt9t013_i2c_txdata(saddr
, buf
, 4);
235 pr_err("i2c_write_w failed, addr = 0x%x, val = 0x%x!\n",
241 static int32_t mt9t013_i2c_write_w_table(
242 struct mt9t013_i2c_reg_conf
*reg_conf_tbl
, int num_of_items_in_table
)
247 for (i
= 0; i
< num_of_items_in_table
; i
++) {
248 rc
= mt9t013_i2c_write_w(mt9t013_client
->addr
,
249 reg_conf_tbl
->waddr
, reg_conf_tbl
->wdata
);
258 static int32_t mt9t013_test(enum mt9t013_test_mode mo
)
262 rc
= mt9t013_i2c_write_w(mt9t013_client
->addr
,
263 REG_GROUPED_PARAMETER_HOLD
,
264 GROUPED_PARAMETER_HOLD
);
271 rc
= mt9t013_i2c_write_w_table(mt9t013_regs
.ttbl
,
272 mt9t013_regs
.ttbl_size
);
275 rc
= mt9t013_i2c_write_w(mt9t013_client
->addr
,
276 REG_TEST_PATTERN_MODE
, (uint16_t)mo
);
281 rc
= mt9t013_i2c_write_w(mt9t013_client
->addr
,
282 REG_GROUPED_PARAMETER_HOLD
,
283 GROUPED_PARAMETER_UPDATE
);
290 static int32_t mt9t013_set_lc(void)
294 rc
= mt9t013_i2c_write_w_table(mt9t013_regs
.lctbl
, mt9t013_regs
.lctbl_size
);
301 static int32_t mt9t013_set_default_focus(uint8_t af_step
)
304 uint8_t code_val_msb
, code_val_lsb
;
306 code_val_lsb
= af_step
;
308 /* Write the digital code for current to the actuator */
309 rc
= mt9t013_i2c_write_b(MT9T013_AF_I2C_ADDR
>>1,
310 code_val_msb
, code_val_lsb
);
312 mt9t013_ctrl
->curr_lens_pos
= 0;
313 mt9t013_ctrl
->init_curr_lens_pos
= 0;
317 static void mt9t013_get_pict_fps(uint16_t fps
, uint16_t *pfps
)
319 /* input fps is preview fps in Q8 format */
320 uint32_t divider
; /*Q10 */
321 uint32_t pclk_mult
; /*Q10 */
323 if (mt9t013_ctrl
->prev_res
== QTR_SIZE
) {
326 ((mt9t013_regs
.reg_pat
[RES_PREVIEW
].frame_length_lines
*
327 mt9t013_regs
.reg_pat
[RES_PREVIEW
].line_length_pck
) *
329 (mt9t013_regs
.reg_pat
[RES_CAPTURE
].frame_length_lines
*
330 mt9t013_regs
.reg_pat
[RES_CAPTURE
].line_length_pck
));
333 (uint32_t) ((mt9t013_regs
.reg_pat
[RES_CAPTURE
].pll_multiplier
*
335 (mt9t013_regs
.reg_pat
[RES_PREVIEW
].pll_multiplier
));
338 /* full size resolution used for preview. */
339 divider
= 0x00000400; /*1.0 */
340 pclk_mult
= 0x00000400; /*1.0 */
343 /* Verify PCLK settings and frame sizes. */
345 (uint16_t) (fps
* divider
* pclk_mult
/
346 0x00000400 / 0x00000400);
349 static uint16_t mt9t013_get_prev_lines_pf(void)
351 if (mt9t013_ctrl
->prev_res
== QTR_SIZE
)
352 return mt9t013_regs
.reg_pat
[RES_PREVIEW
].frame_length_lines
;
354 return mt9t013_regs
.reg_pat
[RES_CAPTURE
].frame_length_lines
;
357 static uint16_t mt9t013_get_prev_pixels_pl(void)
359 if (mt9t013_ctrl
->prev_res
== QTR_SIZE
)
360 return mt9t013_regs
.reg_pat
[RES_PREVIEW
].line_length_pck
;
362 return mt9t013_regs
.reg_pat
[RES_CAPTURE
].line_length_pck
;
365 static uint16_t mt9t013_get_pict_lines_pf(void)
367 return mt9t013_regs
.reg_pat
[RES_CAPTURE
].frame_length_lines
;
370 static uint16_t mt9t013_get_pict_pixels_pl(void)
372 return mt9t013_regs
.reg_pat
[RES_CAPTURE
].line_length_pck
;
375 static uint32_t mt9t013_get_pict_max_exp_lc(void)
377 uint16_t snapshot_lines_per_frame
;
379 if (mt9t013_ctrl
->pict_res
== QTR_SIZE
) {
380 snapshot_lines_per_frame
=
381 mt9t013_regs
.reg_pat
[RES_PREVIEW
].frame_length_lines
- 1;
383 snapshot_lines_per_frame
=
384 mt9t013_regs
.reg_pat
[RES_CAPTURE
].frame_length_lines
- 1;
387 return snapshot_lines_per_frame
* 24;
390 static int32_t mt9t013_set_fps(struct fps_cfg
*fps
)
392 /* input is new fps in Q8 format */
395 mt9t013_ctrl
->fps_divider
= fps
->fps_div
;
396 mt9t013_ctrl
->pict_fps_divider
= fps
->pict_fps_div
;
398 rc
= mt9t013_i2c_write_w(mt9t013_client
->addr
,
399 REG_GROUPED_PARAMETER_HOLD
,
400 GROUPED_PARAMETER_HOLD
);
404 CDBG("mt9t013_set_fps: fps_div is %d, frame_rate is %d\n",
406 (uint16_t) (mt9t013_regs
.reg_pat
[RES_PREVIEW
].
408 fps
->fps_div
/0x00000400));
410 CDBG("mt9t013_set_fps: fps_mult is %d, frame_rate is %d\n",
412 (uint16_t)(mt9t013_regs
.reg_pat
[RES_PREVIEW
].
414 fps
->f_mult
/ 0x00000400));
416 rc
= mt9t013_i2c_write_w(mt9t013_client
->addr
,
419 mt9t013_regs
.reg_pat
[RES_PREVIEW
].line_length_pck
*
420 fps
->f_mult
/ 0x00000400));
424 rc
= mt9t013_i2c_write_w(mt9t013_client
->addr
,
425 REG_GROUPED_PARAMETER_HOLD
,
426 GROUPED_PARAMETER_UPDATE
);
433 static int32_t mt9t013_write_exp_gain(uint16_t gain
, uint32_t line
)
435 const uint16_t max_legal_gain
= 0x01FF;
436 uint32_t line_length_ratio
= 0x00000400;
437 enum mt9t013_setting setting
;
440 if (mt9t013_ctrl
->sensormode
== SENSOR_PREVIEW_MODE
) {
441 mt9t013_ctrl
->my_reg_gain
= gain
;
442 mt9t013_ctrl
->my_reg_line_count
= (uint16_t) line
;
445 if (gain
> max_legal_gain
)
446 gain
= max_legal_gain
;
448 /* Verify no overflow */
449 if (mt9t013_ctrl
->sensormode
!= SENSOR_SNAPSHOT_MODE
) {
450 line
= (uint32_t) (line
* mt9t013_ctrl
->fps_divider
/
453 setting
= RES_PREVIEW
;
456 line
= (uint32_t) (line
* mt9t013_ctrl
->pict_fps_divider
/
459 setting
= RES_CAPTURE
;
462 /*Set digital gain to 1 */
465 if ((mt9t013_regs
.reg_pat
[setting
].frame_length_lines
- 1) < line
) {
468 (uint32_t) (line
* 0x00000400) /
469 (mt9t013_regs
.reg_pat
[setting
].frame_length_lines
- 1);
471 line_length_ratio
= 0x00000400;
473 /* There used to be PARAMETER_HOLD register write before and
474 * after REG_GLOBAL_GAIN & REG_COARSE_INIT_TIME. This causes
475 * aec oscillation. Hence removed. */
477 rc
= mt9t013_i2c_write_w(mt9t013_client
->addr
, REG_GLOBAL_GAIN
, gain
);
481 rc
= mt9t013_i2c_write_w(mt9t013_client
->addr
,
483 (uint16_t)((uint32_t) line
* 0x00000400 /
491 static int32_t mt9t013_set_pict_exp_gain(uint16_t gain
, uint32_t line
)
495 rc
= mt9t013_write_exp_gain(gain
, line
);
499 rc
= mt9t013_i2c_write_w(mt9t013_client
->addr
,
500 MT9T013_REG_RESET_REGISTER
,
505 /* camera_timed_wait(snapshot_wait*exposure_ratio); */
509 static int32_t mt9t013_setting(enum mt9t013_reg_update rupdate
,
510 enum mt9t013_setting rt
)
515 case UPDATE_PERIODIC
: {
517 if (rt
== RES_PREVIEW
|| rt
== RES_CAPTURE
) {
520 mt9t013_i2c_write_w(mt9t013_client
->addr
,
521 MT9T013_REG_RESET_REGISTER
,
522 MT9T013_RESET_REGISTER_PWOFF
);
528 mt9t013_i2c_write_w(mt9t013_client
->addr
,
530 mt9t013_regs
.reg_pat
[rt
].vt_pix_clk_div
);
535 mt9t013_i2c_write_w(mt9t013_client
->addr
,
537 mt9t013_regs
.reg_pat
[rt
].vt_sys_clk_div
);
542 mt9t013_i2c_write_w(mt9t013_client
->addr
,
544 mt9t013_regs
.reg_pat
[rt
].pre_pll_clk_div
);
549 mt9t013_i2c_write_w(mt9t013_client
->addr
,
551 mt9t013_regs
.reg_pat
[rt
].pll_multiplier
);
556 mt9t013_i2c_write_w(mt9t013_client
->addr
,
558 mt9t013_regs
.reg_pat
[rt
].op_pix_clk_div
);
563 mt9t013_i2c_write_w(mt9t013_client
->addr
,
565 mt9t013_regs
.reg_pat
[rt
].op_sys_clk_div
);
572 mt9t013_i2c_write_w(mt9t013_client
->addr
,
573 REG_GROUPED_PARAMETER_HOLD
,
574 GROUPED_PARAMETER_HOLD
);
579 mt9t013_i2c_write_w(mt9t013_client
->addr
,
581 mt9t013_regs
.reg_pat
[rt
].row_speed
);
586 mt9t013_i2c_write_w(mt9t013_client
->addr
,
588 mt9t013_regs
.reg_pat
[rt
].x_addr_start
);
593 mt9t013_i2c_write_w(mt9t013_client
->addr
,
595 mt9t013_regs
.reg_pat
[rt
].x_addr_end
);
600 mt9t013_i2c_write_w(mt9t013_client
->addr
,
602 mt9t013_regs
.reg_pat
[rt
].y_addr_start
);
607 mt9t013_i2c_write_w(mt9t013_client
->addr
,
609 mt9t013_regs
.reg_pat
[rt
].y_addr_end
);
613 if (machine_is_sapphire()) {
615 rc
= mt9t013_i2c_write_w(mt9t013_client
->addr
,
619 rc
= mt9t013_i2c_write_w(mt9t013_client
->addr
,
623 rc
= mt9t013_i2c_write_w(mt9t013_client
->addr
,
625 mt9t013_regs
.reg_pat
[rt
].read_mode
);
630 mt9t013_i2c_write_w(mt9t013_client
->addr
,
632 mt9t013_regs
.reg_pat
[rt
].scale_m
);
638 mt9t013_i2c_write_w(mt9t013_client
->addr
,
640 mt9t013_regs
.reg_pat
[rt
].x_output_size
);
645 mt9t013_i2c_write_w(mt9t013_client
->addr
,
647 mt9t013_regs
.reg_pat
[rt
].y_output_size
);
652 mt9t013_i2c_write_w(mt9t013_client
->addr
,
654 mt9t013_regs
.reg_pat
[rt
].line_length_pck
);
659 mt9t013_i2c_write_w(mt9t013_client
->addr
,
660 REG_FRAME_LENGTH_LINES
,
661 (mt9t013_regs
.reg_pat
[rt
].frame_length_lines
*
662 mt9t013_ctrl
->fps_divider
/ 0x00000400));
667 mt9t013_i2c_write_w(mt9t013_client
->addr
,
669 mt9t013_regs
.reg_pat
[rt
].coarse_int_time
);
674 mt9t013_i2c_write_w(mt9t013_client
->addr
,
676 mt9t013_regs
.reg_pat
[rt
].fine_int_time
);
681 mt9t013_i2c_write_w(mt9t013_client
->addr
,
682 REG_GROUPED_PARAMETER_HOLD
,
683 GROUPED_PARAMETER_UPDATE
);
687 rc
= mt9t013_test(mt9t013_ctrl
->set_test
);
692 mt9t013_i2c_write_w(mt9t013_client
->addr
,
693 MT9T013_REG_RESET_REGISTER
,
694 MT9T013_RESET_REGISTER_PWON
);
705 /*CAMSENSOR_REG_UPDATE_PERIODIC */
707 if (rt
== RES_PREVIEW
|| rt
== RES_CAPTURE
) {
710 mt9t013_i2c_write_w(mt9t013_client
->addr
,
711 MT9T013_REG_RESET_REGISTER
,
712 MT9T013_RESET_REGISTER_PWOFF
);
714 /* MODE_SELECT, stop streaming */
718 mt9t013_i2c_write_w(mt9t013_client
->addr
,
720 mt9t013_regs
.reg_pat
[rt
].vt_pix_clk_div
);
725 mt9t013_i2c_write_w(mt9t013_client
->addr
,
727 mt9t013_regs
.reg_pat
[rt
].vt_sys_clk_div
);
732 mt9t013_i2c_write_w(mt9t013_client
->addr
,
734 mt9t013_regs
.reg_pat
[rt
].pre_pll_clk_div
);
739 mt9t013_i2c_write_w(mt9t013_client
->addr
,
741 mt9t013_regs
.reg_pat
[rt
].pll_multiplier
);
746 mt9t013_i2c_write_w(mt9t013_client
->addr
,
748 mt9t013_regs
.reg_pat
[rt
].op_pix_clk_div
);
753 mt9t013_i2c_write_w(mt9t013_client
->addr
,
755 mt9t013_regs
.reg_pat
[rt
].op_sys_clk_div
);
762 mt9t013_i2c_write_w(mt9t013_client
->addr
,
763 REG_GROUPED_PARAMETER_HOLD
,
764 GROUPED_PARAMETER_HOLD
);
768 /* additional power saving mode ok around 38.2MHz */
770 mt9t013_i2c_write_w(mt9t013_client
->addr
,
776 mt9t013_i2c_write_w(mt9t013_client
->addr
,
782 mt9t013_i2c_write_w(mt9t013_client
->addr
,
788 mt9t013_i2c_write_w(mt9t013_client
->addr
,
793 /* Set preview or snapshot mode */
795 mt9t013_i2c_write_w(mt9t013_client
->addr
,
797 mt9t013_regs
.reg_pat
[rt
].row_speed
);
802 mt9t013_i2c_write_w(mt9t013_client
->addr
,
804 mt9t013_regs
.reg_pat
[rt
].x_addr_start
);
809 mt9t013_i2c_write_w(mt9t013_client
->addr
,
811 mt9t013_regs
.reg_pat
[rt
].x_addr_end
);
816 mt9t013_i2c_write_w(mt9t013_client
->addr
,
818 mt9t013_regs
.reg_pat
[rt
].y_addr_start
);
823 mt9t013_i2c_write_w(mt9t013_client
->addr
,
825 mt9t013_regs
.reg_pat
[rt
].y_addr_end
);
829 if (machine_is_sapphire()) {
831 rc
= mt9t013_i2c_write_w(mt9t013_client
->addr
,
835 rc
= mt9t013_i2c_write_w(mt9t013_client
->addr
,
839 rc
= mt9t013_i2c_write_w(mt9t013_client
->addr
,
841 mt9t013_regs
.reg_pat
[rt
].read_mode
);
846 mt9t013_i2c_write_w(mt9t013_client
->addr
,
848 mt9t013_regs
.reg_pat
[rt
].scale_m
);
853 mt9t013_i2c_write_w(mt9t013_client
->addr
,
855 mt9t013_regs
.reg_pat
[rt
].x_output_size
);
860 mt9t013_i2c_write_w(mt9t013_client
->addr
,
862 mt9t013_regs
.reg_pat
[rt
].y_output_size
);
867 mt9t013_i2c_write_w(mt9t013_client
->addr
,
869 mt9t013_regs
.reg_pat
[rt
].line_length_pck
);
874 mt9t013_i2c_write_w(mt9t013_client
->addr
,
875 REG_FRAME_LENGTH_LINES
,
876 mt9t013_regs
.reg_pat
[rt
].frame_length_lines
);
881 mt9t013_i2c_write_w(mt9t013_client
->addr
,
883 mt9t013_regs
.reg_pat
[rt
].coarse_int_time
);
888 mt9t013_i2c_write_w(mt9t013_client
->addr
,
890 mt9t013_regs
.reg_pat
[rt
].fine_int_time
);
895 mt9t013_i2c_write_w(mt9t013_client
->addr
,
896 REG_GROUPED_PARAMETER_HOLD
,
897 GROUPED_PARAMETER_UPDATE
);
901 /* load lens shading */
903 mt9t013_i2c_write_w(mt9t013_client
->addr
,
904 REG_GROUPED_PARAMETER_HOLD
,
905 GROUPED_PARAMETER_HOLD
);
909 /* most likely needs to be written only once. */
910 rc
= mt9t013_set_lc();
915 mt9t013_i2c_write_w(mt9t013_client
->addr
,
916 REG_GROUPED_PARAMETER_HOLD
,
917 GROUPED_PARAMETER_UPDATE
);
921 rc
= mt9t013_test(mt9t013_ctrl
->set_test
);
928 mt9t013_i2c_write_w(mt9t013_client
->addr
,
929 MT9T013_REG_RESET_REGISTER
,
930 MT9T013_RESET_REGISTER_PWON
);
932 /* MODE_SELECT, stop streaming */
935 CDBG("!!! mt9t013 !!! PowerOn is done!\n");
939 } /* case CAMSENSOR_REG_INIT: */
942 /*CAMSENSOR_REG_INIT */
946 } /* switch (rupdate) */
951 static int32_t mt9t013_video_config(int mode
, int res
)
957 rc
= mt9t013_setting(UPDATE_PERIODIC
, RES_PREVIEW
);
960 CDBG("sensor configuration done!\n");
964 rc
= mt9t013_setting(UPDATE_PERIODIC
, RES_CAPTURE
);
973 mt9t013_ctrl
->prev_res
= res
;
974 mt9t013_ctrl
->curr_res
= res
;
975 mt9t013_ctrl
->sensormode
= mode
;
977 return mt9t013_write_exp_gain(mt9t013_ctrl
->my_reg_gain
,
978 mt9t013_ctrl
->my_reg_line_count
);
981 static int32_t mt9t013_snapshot_config(int mode
)
985 rc
= mt9t013_setting(UPDATE_PERIODIC
, RES_CAPTURE
);
989 mt9t013_ctrl
->curr_res
= mt9t013_ctrl
->pict_res
;
990 mt9t013_ctrl
->sensormode
= mode
;
994 static int32_t mt9t013_raw_snapshot_config(int mode
)
998 rc
= mt9t013_setting(UPDATE_PERIODIC
, RES_CAPTURE
);
1002 mt9t013_ctrl
->curr_res
= mt9t013_ctrl
->pict_res
;
1003 mt9t013_ctrl
->sensormode
= mode
;
1007 static int32_t mt9t013_power_down(void)
1011 rc
= mt9t013_i2c_write_w(mt9t013_client
->addr
,
1012 MT9T013_REG_RESET_REGISTER
,
1013 MT9T013_RESET_REGISTER_PWOFF
);
1019 static int32_t mt9t013_move_focus(int direction
, int32_t num_steps
)
1021 int16_t step_direction
;
1022 int16_t actual_step
;
1023 int16_t next_position
;
1024 int16_t break_steps
[4];
1025 uint8_t code_val_msb
, code_val_lsb
;
1028 if (num_steps
> MT9T013_TOTAL_STEPS_NEAR_TO_FAR
)
1029 num_steps
= MT9T013_TOTAL_STEPS_NEAR_TO_FAR
;
1030 else if (num_steps
== 0)
1033 if (direction
== MOVE_NEAR
)
1035 else if (direction
== MOVE_FAR
)
1036 step_direction
= -4;
1040 if (mt9t013_ctrl
->curr_lens_pos
< mt9t013_ctrl
->init_curr_lens_pos
)
1041 mt9t013_ctrl
->curr_lens_pos
= mt9t013_ctrl
->init_curr_lens_pos
;
1044 (int16_t) (step_direction
*
1045 (int16_t) num_steps
);
1047 for (i
= 0; i
< 4; i
++)
1049 actual_step
/ 4 * (i
+ 1) - actual_step
/ 4 * i
;
1051 for (i
= 0; i
< 4; i
++) {
1054 (mt9t013_ctrl
->curr_lens_pos
+ break_steps
[i
]);
1056 if (next_position
> 255)
1057 next_position
= 255;
1058 else if (next_position
< 0)
1062 ((next_position
>> 4) << 2) |
1063 ((next_position
<< 4) >> 6);
1066 ((next_position
& 0x03) << 6);
1068 /* Writing the digital code for current to the actuator */
1069 if (mt9t013_i2c_write_b(MT9T013_AF_I2C_ADDR
>>1,
1070 code_val_msb
, code_val_lsb
) < 0)
1073 /* Storing the current lens Position */
1074 mt9t013_ctrl
->curr_lens_pos
= next_position
;
1083 static int mt9t013_sensor_init_done(const struct msm_camera_sensor_info
*data
)
1085 gpio_direction_output(data
->sensor_reset
, 0);
1086 gpio_free(data
->sensor_reset
);
1090 static int mt9t013_probe_init_sensor(const struct msm_camera_sensor_info
*data
)
1095 rc
= gpio_request(data
->sensor_reset
, "mt9t013");
1097 gpio_direction_output(data
->sensor_reset
, 1);
1099 goto init_probe_done
;
1103 /* RESET the sensor image part via I2C command */
1104 rc
= mt9t013_i2c_write_w(mt9t013_client
->addr
,
1105 MT9T013_REG_RESET_REGISTER
, 0x1009);
1107 goto init_probe_fail
;
1109 /* 3. Read sensor Model ID: */
1110 rc
= mt9t013_i2c_read_w(mt9t013_client
->addr
,
1111 MT9T013_REG_MODEL_ID
, &chipid
);
1114 goto init_probe_fail
;
1116 CDBG("mt9t013 model_id = 0x%x\n", chipid
);
1118 /* 4. Compare sensor ID to MT9T012VC ID: */
1119 if (chipid
!= MT9T013_MODEL_ID
) {
1121 goto init_probe_fail
;
1124 rc
= mt9t013_i2c_write_w(mt9t013_client
->addr
,
1127 goto init_probe_fail
;
1129 mdelay(MT9T013_RESET_DELAY_MSECS
);
1131 goto init_probe_done
;
1133 /* sensor: output enable */
1135 rc
= mt9t013_i2c_write_w(mt9t013_client
->addr
,
1136 MT9T013_REG_RESET_REGISTER
,
1137 MT9T013_RESET_REGISTER_PWON
);
1139 /* if this fails, the sensor is not the MT9T013 */
1140 rc
= mt9t013_set_default_focus(0);
1144 gpio_direction_output(data
->sensor_reset
, 0);
1145 gpio_free(data
->sensor_reset
);
1150 static int32_t mt9t013_poweron_af(void)
1154 /* enable AF actuator */
1155 CDBG("enable AF actuator, gpio = %d\n",
1156 mt9t013_ctrl
->sensordata
->vcm_pwd
);
1157 rc
= gpio_request(mt9t013_ctrl
->sensordata
->vcm_pwd
, "mt9t013");
1159 gpio_direction_output(mt9t013_ctrl
->sensordata
->vcm_pwd
, 0);
1161 rc
= mt9t013_set_default_focus(0);
1163 pr_err("%s, gpio_request failed (%d)!\n", __func__
, rc
);
1167 static void mt9t013_poweroff_af(void)
1169 gpio_direction_output(mt9t013_ctrl
->sensordata
->vcm_pwd
, 1);
1170 gpio_free(mt9t013_ctrl
->sensordata
->vcm_pwd
);
1173 int mt9t013_sensor_open_init(const struct msm_camera_sensor_info
*data
)
1177 mt9t013_ctrl
= kzalloc(sizeof(struct mt9t013_ctrl
), GFP_KERNEL
);
1178 if (!mt9t013_ctrl
) {
1179 pr_err("mt9t013_init failed!\n");
1184 mt9t013_ctrl
->fps_divider
= 1 * 0x00000400;
1185 mt9t013_ctrl
->pict_fps_divider
= 1 * 0x00000400;
1186 mt9t013_ctrl
->set_test
= TEST_OFF
;
1187 mt9t013_ctrl
->prev_res
= QTR_SIZE
;
1188 mt9t013_ctrl
->pict_res
= FULL_SIZE
;
1191 mt9t013_ctrl
->sensordata
= data
;
1193 /* enable mclk first */
1194 msm_camio_clk_rate_set(MT9T013_DEFAULT_CLOCK_RATE
);
1197 msm_camio_camif_pad_reg_reset();
1200 rc
= mt9t013_probe_init_sensor(data
);
1204 if (mt9t013_ctrl
->prev_res
== QTR_SIZE
)
1205 rc
= mt9t013_setting(REG_INIT
, RES_PREVIEW
);
1207 rc
= mt9t013_setting(REG_INIT
, RES_CAPTURE
);
1210 rc
= mt9t013_poweron_af();
1218 kfree(mt9t013_ctrl
);
1223 static int mt9t013_init_client(struct i2c_client
*client
)
1225 /* Initialize the MSM_CAMI2C Chip */
1226 init_waitqueue_head(&mt9t013_wait_queue
);
1231 static int32_t mt9t013_set_sensor_mode(int mode
, int res
)
1234 rc
= mt9t013_i2c_write_w(mt9t013_client
->addr
,
1235 REG_GROUPED_PARAMETER_HOLD
,
1236 GROUPED_PARAMETER_HOLD
);
1241 case SENSOR_PREVIEW_MODE
:
1242 rc
= mt9t013_video_config(mode
, res
);
1245 case SENSOR_SNAPSHOT_MODE
:
1246 rc
= mt9t013_snapshot_config(mode
);
1249 case SENSOR_RAW_SNAPSHOT_MODE
:
1250 rc
= mt9t013_raw_snapshot_config(mode
);
1257 /* FIXME: what should we do if rc < 0? */
1259 return mt9t013_i2c_write_w(mt9t013_client
->addr
,
1260 REG_GROUPED_PARAMETER_HOLD
,
1261 GROUPED_PARAMETER_UPDATE
);
1265 int mt9t013_sensor_config(void __user
*argp
)
1267 struct sensor_cfg_data cdata
;
1270 if (copy_from_user(&cdata
, (void *)argp
,
1271 sizeof(struct sensor_cfg_data
)))
1276 CDBG("mt9t013_sensor_config: cfgtype = %d\n", cdata
.cfgtype
);
1277 switch (cdata
.cfgtype
) {
1278 case CFG_GET_PICT_FPS
:
1279 mt9t013_get_pict_fps(cdata
.cfg
.gfps
.prevfps
,
1280 &(cdata
.cfg
.gfps
.pictfps
));
1281 if (copy_to_user((void *)argp
,
1283 sizeof(struct sensor_cfg_data
)))
1287 case CFG_GET_PREV_L_PF
:
1288 cdata
.cfg
.prevl_pf
= mt9t013_get_prev_lines_pf();
1289 if (copy_to_user((void *)argp
,
1291 sizeof(struct sensor_cfg_data
)))
1295 case CFG_GET_PREV_P_PL
:
1296 cdata
.cfg
.prevp_pl
= mt9t013_get_prev_pixels_pl();
1297 if (copy_to_user((void *)argp
,
1299 sizeof(struct sensor_cfg_data
)))
1303 case CFG_GET_PICT_L_PF
:
1304 cdata
.cfg
.pictl_pf
= mt9t013_get_pict_lines_pf();
1305 if (copy_to_user((void *)argp
,
1307 sizeof(struct sensor_cfg_data
)))
1311 case CFG_GET_PICT_P_PL
:
1312 cdata
.cfg
.pictp_pl
=
1313 mt9t013_get_pict_pixels_pl();
1315 if (copy_to_user((void *)argp
,
1317 sizeof(struct sensor_cfg_data
)))
1321 case CFG_GET_PICT_MAX_EXP_LC
:
1322 cdata
.cfg
.pict_max_exp_lc
=
1323 mt9t013_get_pict_max_exp_lc();
1325 if (copy_to_user((void *)argp
,
1327 sizeof(struct sensor_cfg_data
)))
1332 case CFG_SET_PICT_FPS
:
1333 rc
= mt9t013_set_fps(&(cdata
.cfg
.fps
));
1336 case CFG_SET_EXP_GAIN
:
1337 rc
= mt9t013_write_exp_gain(cdata
.cfg
.exp_gain
.gain
,
1338 cdata
.cfg
.exp_gain
.line
);
1341 case CFG_SET_PICT_EXP_GAIN
:
1342 rc
= mt9t013_set_pict_exp_gain(cdata
.cfg
.exp_gain
.gain
,
1343 cdata
.cfg
.exp_gain
.line
);
1347 rc
= mt9t013_set_sensor_mode(cdata
.mode
, cdata
.rs
);
1351 rc
= mt9t013_power_down();
1354 case CFG_MOVE_FOCUS
:
1355 rc
= mt9t013_move_focus(cdata
.cfg
.focus
.dir
,
1356 cdata
.cfg
.focus
.steps
);
1359 case CFG_SET_DEFAULT_FOCUS
:
1360 rc
= mt9t013_set_default_focus(cdata
.cfg
.focus
.steps
);
1363 case CFG_GET_AF_MAX_STEPS
:
1364 cdata
.max_steps
= MT9T013_TOTAL_STEPS_NEAR_TO_FAR
;
1365 if (copy_to_user((void *)argp
,
1367 sizeof(struct sensor_cfg_data
)))
1371 case CFG_SET_EFFECT
:
1381 int mt9t013_sensor_release(void)
1387 mt9t013_poweroff_af();
1388 mt9t013_power_down();
1390 gpio_direction_output(mt9t013_ctrl
->sensordata
->sensor_reset
,
1392 gpio_free(mt9t013_ctrl
->sensordata
->sensor_reset
);
1394 kfree(mt9t013_ctrl
);
1397 CDBG("mt9t013_release completed!\n");
1401 static int mt9t013_i2c_probe(struct i2c_client
*client
,
1402 const struct i2c_device_id
*id
)
1405 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
1411 kzalloc(sizeof(struct mt9t013_work
), GFP_KERNEL
);
1413 if (!mt9t013_sensorw
) {
1418 i2c_set_clientdata(client
, mt9t013_sensorw
);
1419 mt9t013_init_client(client
);
1420 mt9t013_client
= client
;
1421 mt9t013_client
->addr
= mt9t013_client
->addr
>> 1;
1424 CDBG("i2c probe ok\n");
1428 kfree(mt9t013_sensorw
);
1429 mt9t013_sensorw
= NULL
;
1430 pr_err("i2c probe failure %d\n", rc
);
1434 static const struct i2c_device_id mt9t013_i2c_id
[] = {
1439 static struct i2c_driver mt9t013_i2c_driver
= {
1440 .id_table
= mt9t013_i2c_id
,
1441 .probe
= mt9t013_i2c_probe
,
1442 .remove
= __exit_p(mt9t013_i2c_remove
),
1448 static int mt9t013_sensor_probe(
1449 const struct msm_camera_sensor_info
*info
,
1450 struct msm_sensor_ctrl
*s
)
1452 /* We expect this driver to match with the i2c device registered
1453 * in the board file immediately. */
1454 int rc
= i2c_add_driver(&mt9t013_i2c_driver
);
1455 if (rc
< 0 || mt9t013_client
== NULL
) {
1460 /* enable mclk first */
1461 msm_camio_clk_rate_set(MT9T013_DEFAULT_CLOCK_RATE
);
1464 rc
= mt9t013_probe_init_sensor(info
);
1466 i2c_del_driver(&mt9t013_i2c_driver
);
1470 s
->s_init
= mt9t013_sensor_open_init
;
1471 s
->s_release
= mt9t013_sensor_release
;
1472 s
->s_config
= mt9t013_sensor_config
;
1473 mt9t013_sensor_init_done(info
);
1479 static int __mt9t013_probe(struct platform_device
*pdev
)
1481 return msm_camera_drv_start(pdev
, mt9t013_sensor_probe
);
1484 static struct platform_driver msm_camera_driver
= {
1485 .probe
= __mt9t013_probe
,
1487 .name
= "msm_camera_mt9t013",
1488 .owner
= THIS_MODULE
,
1492 static int __init
mt9t013_init(void)
1494 return platform_driver_register(&msm_camera_driver
);
1497 module_init(mt9t013_init
);