2 * Copyright (C) 2008-2009 QUALCOMM Incorporated.
5 #include <linux/delay.h>
6 #include <linux/types.h>
8 #include <linux/uaccess.h>
9 #include <linux/miscdevice.h>
10 #include <linux/kernel.h>
11 #include <media/msm_camera.h>
12 #include <mach/gpio.h>
13 #include <mach/camera.h>
14 #include <asm/mach-types.h>
17 /*=============================================================
18 SENSOR REGISTER DEFINES
19 ==============================================================*/
20 #define MT9T013_REG_MODEL_ID 0x0000
21 #define MT9T013_MODEL_ID 0x2600
22 #define REG_GROUPED_PARAMETER_HOLD 0x0104
23 #define GROUPED_PARAMETER_HOLD 0x0100
24 #define GROUPED_PARAMETER_UPDATE 0x0000
25 #define REG_COARSE_INT_TIME 0x3012
26 #define REG_VT_PIX_CLK_DIV 0x0300
27 #define REG_VT_SYS_CLK_DIV 0x0302
28 #define REG_PRE_PLL_CLK_DIV 0x0304
29 #define REG_PLL_MULTIPLIER 0x0306
30 #define REG_OP_PIX_CLK_DIV 0x0308
31 #define REG_OP_SYS_CLK_DIV 0x030A
32 #define REG_SCALE_M 0x0404
33 #define REG_FRAME_LENGTH_LINES 0x300A
34 #define REG_LINE_LENGTH_PCK 0x300C
35 #define REG_X_ADDR_START 0x3004
36 #define REG_Y_ADDR_START 0x3002
37 #define REG_X_ADDR_END 0x3008
38 #define REG_Y_ADDR_END 0x3006
39 #define REG_X_OUTPUT_SIZE 0x034C
40 #define REG_Y_OUTPUT_SIZE 0x034E
41 #define REG_FINE_INT_TIME 0x3014
42 #define REG_ROW_SPEED 0x3016
43 #define MT9T013_REG_RESET_REGISTER 0x301A
44 #define MT9T013_RESET_REGISTER_PWON 0x10CC
45 #define MT9T013_RESET_REGISTER_PWOFF 0x1008 /* 0x10C8 stop streaming*/
46 #define REG_READ_MODE 0x3040
47 #define REG_GLOBAL_GAIN 0x305E
48 #define REG_TEST_PATTERN_MODE 0x3070
51 enum mt9t013_test_mode {
58 enum mt9t013_resolution {
64 enum mt9t013_reg_update {
65 REG_INIT, /* registers that need to be updated during initialization */
66 UPDATE_PERIODIC, /* registers that needs periodic I2C writes */
67 UPDATE_ALL, /* all registers will be updated */
71 enum mt9t013_setting {
76 /* actuator's Slave Address */
77 #define MT9T013_AF_I2C_ADDR 0x18
80 * AF Total steps parameters
82 #define MT9T013_TOTAL_STEPS_NEAR_TO_FAR 30
85 * Time in milisecs for waiting for the sensor to reset.
87 #define MT9T013_RESET_DELAY_MSECS 66
89 /* for 30 fps preview */
90 #define MT9T013_DEFAULT_CLOCK_RATE 24000000
91 #define MT9T013_DEFAULT_MAX_FPS 26
94 /* FIXME: Changes from here */
96 struct work_struct work;
99 static struct mt9t013_work *mt9t013_sensorw;
100 static struct i2c_client *mt9t013_client;
102 struct mt9t013_ctrl {
103 const struct msm_camera_sensor_info *sensordata;
106 uint32_t fps_divider; /* init to 1 * 0x00000400 */
107 uint32_t pict_fps_divider; /* init to 1 * 0x00000400 */
109 uint16_t curr_lens_pos;
110 uint16_t init_curr_lens_pos;
111 uint16_t my_reg_gain;
112 uint32_t my_reg_line_count;
114 enum mt9t013_resolution prev_res;
115 enum mt9t013_resolution pict_res;
116 enum mt9t013_resolution curr_res;
117 enum mt9t013_test_mode set_test;
119 unsigned short imgaddr;
123 static struct mt9t013_ctrl *mt9t013_ctrl;
124 static DECLARE_WAIT_QUEUE_HEAD(mt9t013_wait_queue);
125 DECLARE_MUTEX(mt9t013_sem);
127 extern struct mt9t013_reg mt9t013_regs; /* from mt9t013_reg.c */
129 static int mt9t013_i2c_rxdata(unsigned short saddr,
130 unsigned char *rxdata, int length)
132 struct i2c_msg msgs[] = {
147 if (i2c_transfer(mt9t013_client->adapter, msgs, 2) < 0) {
148 pr_err("mt9t013_i2c_rxdata failed!\n");
155 static int32_t mt9t013_i2c_read_w(unsigned short saddr,
156 unsigned short raddr, unsigned short *rdata)
159 unsigned char buf[4];
164 memset(buf, 0, sizeof(buf));
166 buf[0] = (raddr & 0xFF00)>>8;
167 buf[1] = (raddr & 0x00FF);
169 rc = mt9t013_i2c_rxdata(saddr, buf, 2);
173 *rdata = buf[0] << 8 | buf[1];
176 pr_err("mt9t013_i2c_read failed!\n");
181 static int32_t mt9t013_i2c_txdata(unsigned short saddr,
182 unsigned char *txdata, int length)
184 struct i2c_msg msg[] = {
193 if (i2c_transfer(mt9t013_client->adapter, msg, 1) < 0) {
194 pr_err("mt9t013_i2c_txdata failed\n");
201 static int32_t mt9t013_i2c_write_b(unsigned short saddr,
202 unsigned short waddr, unsigned short wdata)
205 unsigned char buf[2];
207 memset(buf, 0, sizeof(buf));
210 rc = mt9t013_i2c_txdata(saddr, buf, 2);
213 pr_err("i2c_write failed, addr = 0x%x, val = 0x%x!\n",
219 static int32_t mt9t013_i2c_write_w(unsigned short saddr,
220 unsigned short waddr, unsigned short wdata)
223 unsigned char buf[4];
225 memset(buf, 0, sizeof(buf));
226 buf[0] = (waddr & 0xFF00)>>8;
227 buf[1] = (waddr & 0x00FF);
228 buf[2] = (wdata & 0xFF00)>>8;
229 buf[3] = (wdata & 0x00FF);
231 rc = mt9t013_i2c_txdata(saddr, buf, 4);
234 pr_err("i2c_write_w failed, addr = 0x%x, val = 0x%x!\n",
240 static int32_t mt9t013_i2c_write_w_table(
241 struct mt9t013_i2c_reg_conf *reg_conf_tbl, int num_of_items_in_table)
246 for (i = 0; i < num_of_items_in_table; i++) {
247 rc = mt9t013_i2c_write_w(mt9t013_client->addr,
248 reg_conf_tbl->waddr, reg_conf_tbl->wdata);
257 static int32_t mt9t013_test(enum mt9t013_test_mode mo)
261 rc = mt9t013_i2c_write_w(mt9t013_client->addr,
262 REG_GROUPED_PARAMETER_HOLD,
263 GROUPED_PARAMETER_HOLD);
270 rc = mt9t013_i2c_write_w_table(mt9t013_regs.ttbl,
271 mt9t013_regs.ttbl_size);
274 rc = mt9t013_i2c_write_w(mt9t013_client->addr,
275 REG_TEST_PATTERN_MODE, (uint16_t)mo);
280 rc = mt9t013_i2c_write_w(mt9t013_client->addr,
281 REG_GROUPED_PARAMETER_HOLD,
282 GROUPED_PARAMETER_UPDATE);
289 static int32_t mt9t013_set_lc(void)
293 rc = mt9t013_i2c_write_w_table(mt9t013_regs.lctbl, mt9t013_regs.lctbl_size);
300 static int32_t mt9t013_set_default_focus(uint8_t af_step)
303 uint8_t code_val_msb, code_val_lsb;
305 code_val_lsb = af_step;
307 /* Write the digital code for current to the actuator */
308 rc = mt9t013_i2c_write_b(MT9T013_AF_I2C_ADDR>>1,
309 code_val_msb, code_val_lsb);
311 mt9t013_ctrl->curr_lens_pos = 0;
312 mt9t013_ctrl->init_curr_lens_pos = 0;
316 static void mt9t013_get_pict_fps(uint16_t fps, uint16_t *pfps)
318 /* input fps is preview fps in Q8 format */
319 uint32_t divider; /*Q10 */
320 uint32_t pclk_mult; /*Q10 */
322 if (mt9t013_ctrl->prev_res == QTR_SIZE) {
325 ((mt9t013_regs.reg_pat[RES_PREVIEW].frame_length_lines *
326 mt9t013_regs.reg_pat[RES_PREVIEW].line_length_pck) *
328 (mt9t013_regs.reg_pat[RES_CAPTURE].frame_length_lines *
329 mt9t013_regs.reg_pat[RES_CAPTURE].line_length_pck));
332 (uint32_t) ((mt9t013_regs.reg_pat[RES_CAPTURE].pll_multiplier *
334 (mt9t013_regs.reg_pat[RES_PREVIEW].pll_multiplier));
337 /* full size resolution used for preview. */
338 divider = 0x00000400; /*1.0 */
339 pclk_mult = 0x00000400; /*1.0 */
342 /* Verify PCLK settings and frame sizes. */
344 (uint16_t) (fps * divider * pclk_mult /
345 0x00000400 / 0x00000400);
348 static uint16_t mt9t013_get_prev_lines_pf(void)
350 if (mt9t013_ctrl->prev_res == QTR_SIZE)
351 return mt9t013_regs.reg_pat[RES_PREVIEW].frame_length_lines;
353 return mt9t013_regs.reg_pat[RES_CAPTURE].frame_length_lines;
356 static uint16_t mt9t013_get_prev_pixels_pl(void)
358 if (mt9t013_ctrl->prev_res == QTR_SIZE)
359 return mt9t013_regs.reg_pat[RES_PREVIEW].line_length_pck;
361 return mt9t013_regs.reg_pat[RES_CAPTURE].line_length_pck;
364 static uint16_t mt9t013_get_pict_lines_pf(void)
366 return mt9t013_regs.reg_pat[RES_CAPTURE].frame_length_lines;
369 static uint16_t mt9t013_get_pict_pixels_pl(void)
371 return mt9t013_regs.reg_pat[RES_CAPTURE].line_length_pck;
374 static uint32_t mt9t013_get_pict_max_exp_lc(void)
376 uint16_t snapshot_lines_per_frame;
378 if (mt9t013_ctrl->pict_res == QTR_SIZE) {
379 snapshot_lines_per_frame =
380 mt9t013_regs.reg_pat[RES_PREVIEW].frame_length_lines - 1;
382 snapshot_lines_per_frame =
383 mt9t013_regs.reg_pat[RES_CAPTURE].frame_length_lines - 1;
386 return snapshot_lines_per_frame * 24;
389 static int32_t mt9t013_set_fps(struct fps_cfg *fps)
391 /* input is new fps in Q8 format */
394 mt9t013_ctrl->fps_divider = fps->fps_div;
395 mt9t013_ctrl->pict_fps_divider = fps->pict_fps_div;
397 rc = mt9t013_i2c_write_w(mt9t013_client->addr,
398 REG_GROUPED_PARAMETER_HOLD,
399 GROUPED_PARAMETER_HOLD);
403 CDBG("mt9t013_set_fps: fps_div is %d, frame_rate is %d\n",
405 (uint16_t) (mt9t013_regs.reg_pat[RES_PREVIEW].
407 fps->fps_div/0x00000400));
409 CDBG("mt9t013_set_fps: fps_mult is %d, frame_rate is %d\n",
411 (uint16_t)(mt9t013_regs.reg_pat[RES_PREVIEW].
413 fps->f_mult / 0x00000400));
415 rc = mt9t013_i2c_write_w(mt9t013_client->addr,
418 mt9t013_regs.reg_pat[RES_PREVIEW].line_length_pck *
419 fps->f_mult / 0x00000400));
423 rc = mt9t013_i2c_write_w(mt9t013_client->addr,
424 REG_GROUPED_PARAMETER_HOLD,
425 GROUPED_PARAMETER_UPDATE);
432 static int32_t mt9t013_write_exp_gain(uint16_t gain, uint32_t line)
434 const uint16_t max_legal_gain = 0x01FF;
435 uint32_t line_length_ratio = 0x00000400;
436 enum mt9t013_setting setting;
439 if (mt9t013_ctrl->sensormode == SENSOR_PREVIEW_MODE) {
440 mt9t013_ctrl->my_reg_gain = gain;
441 mt9t013_ctrl->my_reg_line_count = (uint16_t) line;
444 if (gain > max_legal_gain)
445 gain = max_legal_gain;
447 /* Verify no overflow */
448 if (mt9t013_ctrl->sensormode != SENSOR_SNAPSHOT_MODE) {
449 line = (uint32_t) (line * mt9t013_ctrl->fps_divider /
452 setting = RES_PREVIEW;
455 line = (uint32_t) (line * mt9t013_ctrl->pict_fps_divider /
458 setting = RES_CAPTURE;
461 /*Set digital gain to 1 */
464 if ((mt9t013_regs.reg_pat[setting].frame_length_lines - 1) < line) {
467 (uint32_t) (line * 0x00000400) /
468 (mt9t013_regs.reg_pat[setting].frame_length_lines - 1);
470 line_length_ratio = 0x00000400;
472 /* There used to be PARAMETER_HOLD register write before and
473 * after REG_GLOBAL_GAIN & REG_COARSE_INIT_TIME. This causes
474 * aec oscillation. Hence removed. */
476 rc = mt9t013_i2c_write_w(mt9t013_client->addr, REG_GLOBAL_GAIN, gain);
480 rc = mt9t013_i2c_write_w(mt9t013_client->addr,
482 (uint16_t)((uint32_t) line * 0x00000400 /
490 static int32_t mt9t013_set_pict_exp_gain(uint16_t gain, uint32_t line)
494 rc = mt9t013_write_exp_gain(gain, line);
498 rc = mt9t013_i2c_write_w(mt9t013_client->addr,
499 MT9T013_REG_RESET_REGISTER,
504 /* camera_timed_wait(snapshot_wait*exposure_ratio); */
508 static int32_t mt9t013_setting(enum mt9t013_reg_update rupdate,
509 enum mt9t013_setting rt)
514 case UPDATE_PERIODIC: {
516 if (rt == RES_PREVIEW || rt == RES_CAPTURE) {
519 mt9t013_i2c_write_w(mt9t013_client->addr,
520 MT9T013_REG_RESET_REGISTER,
521 MT9T013_RESET_REGISTER_PWOFF);
527 mt9t013_i2c_write_w(mt9t013_client->addr,
529 mt9t013_regs.reg_pat[rt].vt_pix_clk_div);
534 mt9t013_i2c_write_w(mt9t013_client->addr,
536 mt9t013_regs.reg_pat[rt].vt_sys_clk_div);
541 mt9t013_i2c_write_w(mt9t013_client->addr,
543 mt9t013_regs.reg_pat[rt].pre_pll_clk_div);
548 mt9t013_i2c_write_w(mt9t013_client->addr,
550 mt9t013_regs.reg_pat[rt].pll_multiplier);
555 mt9t013_i2c_write_w(mt9t013_client->addr,
557 mt9t013_regs.reg_pat[rt].op_pix_clk_div);
562 mt9t013_i2c_write_w(mt9t013_client->addr,
564 mt9t013_regs.reg_pat[rt].op_sys_clk_div);
571 mt9t013_i2c_write_w(mt9t013_client->addr,
572 REG_GROUPED_PARAMETER_HOLD,
573 GROUPED_PARAMETER_HOLD);
578 mt9t013_i2c_write_w(mt9t013_client->addr,
580 mt9t013_regs.reg_pat[rt].row_speed);
585 mt9t013_i2c_write_w(mt9t013_client->addr,
587 mt9t013_regs.reg_pat[rt].x_addr_start);
592 mt9t013_i2c_write_w(mt9t013_client->addr,
594 mt9t013_regs.reg_pat[rt].x_addr_end);
599 mt9t013_i2c_write_w(mt9t013_client->addr,
601 mt9t013_regs.reg_pat[rt].y_addr_start);
606 mt9t013_i2c_write_w(mt9t013_client->addr,
608 mt9t013_regs.reg_pat[rt].y_addr_end);
612 if (machine_is_sapphire()) {
614 rc = mt9t013_i2c_write_w(mt9t013_client->addr,
618 rc = mt9t013_i2c_write_w(mt9t013_client->addr,
622 rc = mt9t013_i2c_write_w(mt9t013_client->addr,
624 mt9t013_regs.reg_pat[rt].read_mode);
629 mt9t013_i2c_write_w(mt9t013_client->addr,
631 mt9t013_regs.reg_pat[rt].scale_m);
637 mt9t013_i2c_write_w(mt9t013_client->addr,
639 mt9t013_regs.reg_pat[rt].x_output_size);
644 mt9t013_i2c_write_w(mt9t013_client->addr,
646 mt9t013_regs.reg_pat[rt].y_output_size);
651 mt9t013_i2c_write_w(mt9t013_client->addr,
653 mt9t013_regs.reg_pat[rt].line_length_pck);
658 mt9t013_i2c_write_w(mt9t013_client->addr,
659 REG_FRAME_LENGTH_LINES,
660 (mt9t013_regs.reg_pat[rt].frame_length_lines *
661 mt9t013_ctrl->fps_divider / 0x00000400));
666 mt9t013_i2c_write_w(mt9t013_client->addr,
668 mt9t013_regs.reg_pat[rt].coarse_int_time);
673 mt9t013_i2c_write_w(mt9t013_client->addr,
675 mt9t013_regs.reg_pat[rt].fine_int_time);
680 mt9t013_i2c_write_w(mt9t013_client->addr,
681 REG_GROUPED_PARAMETER_HOLD,
682 GROUPED_PARAMETER_UPDATE);
686 rc = mt9t013_test(mt9t013_ctrl->set_test);
691 mt9t013_i2c_write_w(mt9t013_client->addr,
692 MT9T013_REG_RESET_REGISTER,
693 MT9T013_RESET_REGISTER_PWON);
704 /*CAMSENSOR_REG_UPDATE_PERIODIC */
706 if (rt == RES_PREVIEW || rt == RES_CAPTURE) {
709 mt9t013_i2c_write_w(mt9t013_client->addr,
710 MT9T013_REG_RESET_REGISTER,
711 MT9T013_RESET_REGISTER_PWOFF);
713 /* MODE_SELECT, stop streaming */
717 mt9t013_i2c_write_w(mt9t013_client->addr,
719 mt9t013_regs.reg_pat[rt].vt_pix_clk_div);
724 mt9t013_i2c_write_w(mt9t013_client->addr,
726 mt9t013_regs.reg_pat[rt].vt_sys_clk_div);
731 mt9t013_i2c_write_w(mt9t013_client->addr,
733 mt9t013_regs.reg_pat[rt].pre_pll_clk_div);
738 mt9t013_i2c_write_w(mt9t013_client->addr,
740 mt9t013_regs.reg_pat[rt].pll_multiplier);
745 mt9t013_i2c_write_w(mt9t013_client->addr,
747 mt9t013_regs.reg_pat[rt].op_pix_clk_div);
752 mt9t013_i2c_write_w(mt9t013_client->addr,
754 mt9t013_regs.reg_pat[rt].op_sys_clk_div);
761 mt9t013_i2c_write_w(mt9t013_client->addr,
762 REG_GROUPED_PARAMETER_HOLD,
763 GROUPED_PARAMETER_HOLD);
767 /* additional power saving mode ok around 38.2MHz */
769 mt9t013_i2c_write_w(mt9t013_client->addr,
775 mt9t013_i2c_write_w(mt9t013_client->addr,
781 mt9t013_i2c_write_w(mt9t013_client->addr,
787 mt9t013_i2c_write_w(mt9t013_client->addr,
792 /* Set preview or snapshot mode */
794 mt9t013_i2c_write_w(mt9t013_client->addr,
796 mt9t013_regs.reg_pat[rt].row_speed);
801 mt9t013_i2c_write_w(mt9t013_client->addr,
803 mt9t013_regs.reg_pat[rt].x_addr_start);
808 mt9t013_i2c_write_w(mt9t013_client->addr,
810 mt9t013_regs.reg_pat[rt].x_addr_end);
815 mt9t013_i2c_write_w(mt9t013_client->addr,
817 mt9t013_regs.reg_pat[rt].y_addr_start);
822 mt9t013_i2c_write_w(mt9t013_client->addr,
824 mt9t013_regs.reg_pat[rt].y_addr_end);
828 if (machine_is_sapphire()) {
830 rc = mt9t013_i2c_write_w(mt9t013_client->addr,
834 rc = mt9t013_i2c_write_w(mt9t013_client->addr,
838 rc = mt9t013_i2c_write_w(mt9t013_client->addr,
840 mt9t013_regs.reg_pat[rt].read_mode);
845 mt9t013_i2c_write_w(mt9t013_client->addr,
847 mt9t013_regs.reg_pat[rt].scale_m);
852 mt9t013_i2c_write_w(mt9t013_client->addr,
854 mt9t013_regs.reg_pat[rt].x_output_size);
859 mt9t013_i2c_write_w(mt9t013_client->addr,
861 mt9t013_regs.reg_pat[rt].y_output_size);
866 mt9t013_i2c_write_w(mt9t013_client->addr,
868 mt9t013_regs.reg_pat[rt].line_length_pck);
873 mt9t013_i2c_write_w(mt9t013_client->addr,
874 REG_FRAME_LENGTH_LINES,
875 mt9t013_regs.reg_pat[rt].frame_length_lines);
880 mt9t013_i2c_write_w(mt9t013_client->addr,
882 mt9t013_regs.reg_pat[rt].coarse_int_time);
887 mt9t013_i2c_write_w(mt9t013_client->addr,
889 mt9t013_regs.reg_pat[rt].fine_int_time);
894 mt9t013_i2c_write_w(mt9t013_client->addr,
895 REG_GROUPED_PARAMETER_HOLD,
896 GROUPED_PARAMETER_UPDATE);
900 /* load lens shading */
902 mt9t013_i2c_write_w(mt9t013_client->addr,
903 REG_GROUPED_PARAMETER_HOLD,
904 GROUPED_PARAMETER_HOLD);
908 /* most likely needs to be written only once. */
909 rc = mt9t013_set_lc();
914 mt9t013_i2c_write_w(mt9t013_client->addr,
915 REG_GROUPED_PARAMETER_HOLD,
916 GROUPED_PARAMETER_UPDATE);
920 rc = mt9t013_test(mt9t013_ctrl->set_test);
927 mt9t013_i2c_write_w(mt9t013_client->addr,
928 MT9T013_REG_RESET_REGISTER,
929 MT9T013_RESET_REGISTER_PWON);
931 /* MODE_SELECT, stop streaming */
934 CDBG("!!! mt9t013 !!! PowerOn is done!\n");
938 } /* case CAMSENSOR_REG_INIT: */
941 /*CAMSENSOR_REG_INIT */
945 } /* switch (rupdate) */
950 static int32_t mt9t013_video_config(int mode, int res)
956 rc = mt9t013_setting(UPDATE_PERIODIC, RES_PREVIEW);
959 CDBG("sensor configuration done!\n");
963 rc = mt9t013_setting(UPDATE_PERIODIC, RES_CAPTURE);
972 mt9t013_ctrl->prev_res = res;
973 mt9t013_ctrl->curr_res = res;
974 mt9t013_ctrl->sensormode = mode;
976 return mt9t013_write_exp_gain(mt9t013_ctrl->my_reg_gain,
977 mt9t013_ctrl->my_reg_line_count);
980 static int32_t mt9t013_snapshot_config(int mode)
984 rc = mt9t013_setting(UPDATE_PERIODIC, RES_CAPTURE);
988 mt9t013_ctrl->curr_res = mt9t013_ctrl->pict_res;
989 mt9t013_ctrl->sensormode = mode;
993 static int32_t mt9t013_raw_snapshot_config(int mode)
997 rc = mt9t013_setting(UPDATE_PERIODIC, RES_CAPTURE);
1001 mt9t013_ctrl->curr_res = mt9t013_ctrl->pict_res;
1002 mt9t013_ctrl->sensormode = mode;
1006 static int32_t mt9t013_power_down(void)
1010 rc = mt9t013_i2c_write_w(mt9t013_client->addr,
1011 MT9T013_REG_RESET_REGISTER,
1012 MT9T013_RESET_REGISTER_PWOFF);
1018 static int32_t mt9t013_move_focus(int direction, int32_t num_steps)
1020 int16_t step_direction;
1021 int16_t actual_step;
1022 int16_t next_position;
1023 int16_t break_steps[4];
1024 uint8_t code_val_msb, code_val_lsb;
1027 if (num_steps > MT9T013_TOTAL_STEPS_NEAR_TO_FAR)
1028 num_steps = MT9T013_TOTAL_STEPS_NEAR_TO_FAR;
1029 else if (num_steps == 0)
1032 if (direction == MOVE_NEAR)
1034 else if (direction == MOVE_FAR)
1035 step_direction = -4;
1039 if (mt9t013_ctrl->curr_lens_pos < mt9t013_ctrl->init_curr_lens_pos)
1040 mt9t013_ctrl->curr_lens_pos = mt9t013_ctrl->init_curr_lens_pos;
1043 (int16_t) (step_direction *
1044 (int16_t) num_steps);
1046 for (i = 0; i < 4; i++)
1048 actual_step / 4 * (i + 1) - actual_step / 4 * i;
1050 for (i = 0; i < 4; i++) {
1053 (mt9t013_ctrl->curr_lens_pos + break_steps[i]);
1055 if (next_position > 255)
1056 next_position = 255;
1057 else if (next_position < 0)
1061 ((next_position >> 4) << 2) |
1062 ((next_position << 4) >> 6);
1065 ((next_position & 0x03) << 6);
1067 /* Writing the digital code for current to the actuator */
1068 if (mt9t013_i2c_write_b(MT9T013_AF_I2C_ADDR>>1,
1069 code_val_msb, code_val_lsb) < 0)
1072 /* Storing the current lens Position */
1073 mt9t013_ctrl->curr_lens_pos = next_position;
1082 static int mt9t013_sensor_init_done(const struct msm_camera_sensor_info *data)
1084 gpio_direction_output(data->sensor_reset, 0);
1085 gpio_free(data->sensor_reset);
1089 static int mt9t013_probe_init_sensor(const struct msm_camera_sensor_info *data)
1094 rc = gpio_request(data->sensor_reset, "mt9t013");
1096 gpio_direction_output(data->sensor_reset, 1);
1098 goto init_probe_done;
1102 /* RESET the sensor image part via I2C command */
1103 rc = mt9t013_i2c_write_w(mt9t013_client->addr,
1104 MT9T013_REG_RESET_REGISTER, 0x1009);
1106 goto init_probe_fail;
1108 /* 3. Read sensor Model ID: */
1109 rc = mt9t013_i2c_read_w(mt9t013_client->addr,
1110 MT9T013_REG_MODEL_ID, &chipid);
1113 goto init_probe_fail;
1115 CDBG("mt9t013 model_id = 0x%x\n", chipid);
1117 /* 4. Compare sensor ID to MT9T012VC ID: */
1118 if (chipid != MT9T013_MODEL_ID) {
1120 goto init_probe_fail;
1123 rc = mt9t013_i2c_write_w(mt9t013_client->addr,
1126 goto init_probe_fail;
1128 mdelay(MT9T013_RESET_DELAY_MSECS);
1130 goto init_probe_done;
1132 /* sensor: output enable */
1134 rc = mt9t013_i2c_write_w(mt9t013_client->addr,
1135 MT9T013_REG_RESET_REGISTER,
1136 MT9T013_RESET_REGISTER_PWON);
1138 /* if this fails, the sensor is not the MT9T013 */
1139 rc = mt9t013_set_default_focus(0);
1143 gpio_direction_output(data->sensor_reset, 0);
1144 gpio_free(data->sensor_reset);
1149 static int32_t mt9t013_poweron_af(void)
1153 /* enable AF actuator */
1154 CDBG("enable AF actuator, gpio = %d\n",
1155 mt9t013_ctrl->sensordata->vcm_pwd);
1156 rc = gpio_request(mt9t013_ctrl->sensordata->vcm_pwd, "mt9t013");
1158 gpio_direction_output(mt9t013_ctrl->sensordata->vcm_pwd, 0);
1160 rc = mt9t013_set_default_focus(0);
1162 pr_err("%s, gpio_request failed (%d)!\n", __func__, rc);
1166 static void mt9t013_poweroff_af(void)
1168 gpio_direction_output(mt9t013_ctrl->sensordata->vcm_pwd, 1);
1169 gpio_free(mt9t013_ctrl->sensordata->vcm_pwd);
1172 int mt9t013_sensor_open_init(const struct msm_camera_sensor_info *data)
1176 mt9t013_ctrl = kzalloc(sizeof(struct mt9t013_ctrl), GFP_KERNEL);
1177 if (!mt9t013_ctrl) {
1178 pr_err("mt9t013_init failed!\n");
1183 mt9t013_ctrl->fps_divider = 1 * 0x00000400;
1184 mt9t013_ctrl->pict_fps_divider = 1 * 0x00000400;
1185 mt9t013_ctrl->set_test = TEST_OFF;
1186 mt9t013_ctrl->prev_res = QTR_SIZE;
1187 mt9t013_ctrl->pict_res = FULL_SIZE;
1190 mt9t013_ctrl->sensordata = data;
1192 /* enable mclk first */
1193 msm_camio_clk_rate_set(MT9T013_DEFAULT_CLOCK_RATE);
1196 msm_camio_camif_pad_reg_reset();
1199 rc = mt9t013_probe_init_sensor(data);
1203 if (mt9t013_ctrl->prev_res == QTR_SIZE)
1204 rc = mt9t013_setting(REG_INIT, RES_PREVIEW);
1206 rc = mt9t013_setting(REG_INIT, RES_CAPTURE);
1209 rc = mt9t013_poweron_af();
1217 kfree(mt9t013_ctrl);
1222 static int mt9t013_init_client(struct i2c_client *client)
1224 /* Initialize the MSM_CAMI2C Chip */
1225 init_waitqueue_head(&mt9t013_wait_queue);
1230 static int32_t mt9t013_set_sensor_mode(int mode, int res)
1233 rc = mt9t013_i2c_write_w(mt9t013_client->addr,
1234 REG_GROUPED_PARAMETER_HOLD,
1235 GROUPED_PARAMETER_HOLD);
1240 case SENSOR_PREVIEW_MODE:
1241 rc = mt9t013_video_config(mode, res);
1244 case SENSOR_SNAPSHOT_MODE:
1245 rc = mt9t013_snapshot_config(mode);
1248 case SENSOR_RAW_SNAPSHOT_MODE:
1249 rc = mt9t013_raw_snapshot_config(mode);
1256 /* FIXME: what should we do if rc < 0? */
1258 return mt9t013_i2c_write_w(mt9t013_client->addr,
1259 REG_GROUPED_PARAMETER_HOLD,
1260 GROUPED_PARAMETER_UPDATE);
1264 int mt9t013_sensor_config(void __user *argp)
1266 struct sensor_cfg_data cdata;
1269 if (copy_from_user(&cdata, (void *)argp,
1270 sizeof(struct sensor_cfg_data)))
1275 CDBG("mt9t013_sensor_config: cfgtype = %d\n", cdata.cfgtype);
1276 switch (cdata.cfgtype) {
1277 case CFG_GET_PICT_FPS:
1278 mt9t013_get_pict_fps(cdata.cfg.gfps.prevfps,
1279 &(cdata.cfg.gfps.pictfps));
1280 if (copy_to_user((void *)argp,
1282 sizeof(struct sensor_cfg_data)))
1286 case CFG_GET_PREV_L_PF:
1287 cdata.cfg.prevl_pf = mt9t013_get_prev_lines_pf();
1288 if (copy_to_user((void *)argp,
1290 sizeof(struct sensor_cfg_data)))
1294 case CFG_GET_PREV_P_PL:
1295 cdata.cfg.prevp_pl = mt9t013_get_prev_pixels_pl();
1296 if (copy_to_user((void *)argp,
1298 sizeof(struct sensor_cfg_data)))
1302 case CFG_GET_PICT_L_PF:
1303 cdata.cfg.pictl_pf = mt9t013_get_pict_lines_pf();
1304 if (copy_to_user((void *)argp,
1306 sizeof(struct sensor_cfg_data)))
1310 case CFG_GET_PICT_P_PL:
1311 cdata.cfg.pictp_pl =
1312 mt9t013_get_pict_pixels_pl();
1314 if (copy_to_user((void *)argp,
1316 sizeof(struct sensor_cfg_data)))
1320 case CFG_GET_PICT_MAX_EXP_LC:
1321 cdata.cfg.pict_max_exp_lc =
1322 mt9t013_get_pict_max_exp_lc();
1324 if (copy_to_user((void *)argp,
1326 sizeof(struct sensor_cfg_data)))
1331 case CFG_SET_PICT_FPS:
1332 rc = mt9t013_set_fps(&(cdata.cfg.fps));
1335 case CFG_SET_EXP_GAIN:
1336 rc = mt9t013_write_exp_gain(cdata.cfg.exp_gain.gain,
1337 cdata.cfg.exp_gain.line);
1340 case CFG_SET_PICT_EXP_GAIN:
1341 rc = mt9t013_set_pict_exp_gain(cdata.cfg.exp_gain.gain,
1342 cdata.cfg.exp_gain.line);
1346 rc = mt9t013_set_sensor_mode(cdata.mode, cdata.rs);
1350 rc = mt9t013_power_down();
1353 case CFG_MOVE_FOCUS:
1354 rc = mt9t013_move_focus(cdata.cfg.focus.dir,
1355 cdata.cfg.focus.steps);
1358 case CFG_SET_DEFAULT_FOCUS:
1359 rc = mt9t013_set_default_focus(cdata.cfg.focus.steps);
1362 case CFG_GET_AF_MAX_STEPS:
1363 cdata.max_steps = MT9T013_TOTAL_STEPS_NEAR_TO_FAR;
1364 if (copy_to_user((void *)argp,
1366 sizeof(struct sensor_cfg_data)))
1370 case CFG_SET_EFFECT:
1380 int mt9t013_sensor_release(void)
1386 mt9t013_poweroff_af();
1387 mt9t013_power_down();
1389 gpio_direction_output(mt9t013_ctrl->sensordata->sensor_reset,
1391 gpio_free(mt9t013_ctrl->sensordata->sensor_reset);
1393 kfree(mt9t013_ctrl);
1396 CDBG("mt9t013_release completed!\n");
1400 static int mt9t013_i2c_probe(struct i2c_client *client,
1401 const struct i2c_device_id *id)
1404 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1410 kzalloc(sizeof(struct mt9t013_work), GFP_KERNEL);
1412 if (!mt9t013_sensorw) {
1417 i2c_set_clientdata(client, mt9t013_sensorw);
1418 mt9t013_init_client(client);
1419 mt9t013_client = client;
1420 mt9t013_client->addr = mt9t013_client->addr >> 1;
1423 CDBG("i2c probe ok\n");
1427 kfree(mt9t013_sensorw);
1428 mt9t013_sensorw = NULL;
1429 pr_err("i2c probe failure %d\n", rc);
1433 static const struct i2c_device_id mt9t013_i2c_id[] = {
1438 static struct i2c_driver mt9t013_i2c_driver = {
1439 .id_table = mt9t013_i2c_id,
1440 .probe = mt9t013_i2c_probe,
1441 .remove = __exit_p(mt9t013_i2c_remove),
1447 static int mt9t013_sensor_probe(
1448 const struct msm_camera_sensor_info *info,
1449 struct msm_sensor_ctrl *s)
1451 /* We expect this driver to match with the i2c device registered
1452 * in the board file immediately. */
1453 int rc = i2c_add_driver(&mt9t013_i2c_driver);
1454 if (rc < 0 || mt9t013_client == NULL) {
1459 /* enable mclk first */
1460 msm_camio_clk_rate_set(MT9T013_DEFAULT_CLOCK_RATE);
1463 rc = mt9t013_probe_init_sensor(info);
1465 i2c_del_driver(&mt9t013_i2c_driver);
1469 s->s_init = mt9t013_sensor_open_init;
1470 s->s_release = mt9t013_sensor_release;
1471 s->s_config = mt9t013_sensor_config;
1472 mt9t013_sensor_init_done(info);
1478 static int __mt9t013_probe(struct platform_device *pdev)
1480 return msm_camera_drv_start(pdev, mt9t013_sensor_probe);
1483 static struct platform_driver msm_camera_driver = {
1484 .probe = __mt9t013_probe,
1486 .name = "msm_camera_mt9t013",
1487 .owner = THIS_MODULE,
1491 static int __init mt9t013_init(void)
1493 return platform_driver_register(&msm_camera_driver);
1496 module_init(mt9t013_init);