blob: 365faef4b8a05e338025d959dede278b0eaab720 [file] [log] [blame]
Duy Truonge833aca2013-02-12 13:35:08 -08001/* Copyright (c) 2012, The Linux Foundation. All rights reserved.
Su Liu6c3bb322012-02-14 02:15:05 +05302 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#include <linux/delay.h>
14#include <linux/debugfs.h>
15#include <linux/types.h>
16#include <linux/i2c.h>
17#include <linux/uaccess.h>
18#include <linux/miscdevice.h>
19#include <linux/slab.h>
20#include <linux/gpio.h>
21#include <linux/bitops.h>
22#include <linux/leds.h>
23#include <mach/camera.h>
24#include <media/msm_camera.h>
25#include "ov5647.h"
26
27/* 16bit address - 8 bit context register structure */
28#define Q8 0x00000100
29#define Q10 0x00000400
30
31#define REG_OV5647_GAIN_MSB 0x350A
32#define REG_OV5647_GAIN_LSB 0x350B
33#define REG_OV5647_LINE_HSB 0x3500
34#define REG_OV5647_LINE_MSB 0x3501
35#define REG_OV5647_LINE_LSB 0x3502
36
37/* MCLK */
38#define OV5647_MASTER_CLK_RATE 24000000
39
40/* AF Total steps parameters */
41#define OV5647_TOTAL_STEPS_NEAR_TO_FAR 32
42
43#define OV5647_REG_PREV_FRAME_LEN_1 31
44#define OV5647_REG_PREV_FRAME_LEN_2 32
45#define OV5647_REG_PREV_LINE_LEN_1 33
46#define OV5647_REG_PREV_LINE_LEN_2 34
47
48#define OV5647_REG_SNAP_FRAME_LEN_1 15
49#define OV5647_REG_SNAP_FRAME_LEN_2 16
50#define OV5647_REG_SNAP_LINE_LEN_1 17
51#define OV5647_REG_SNAP_LINE_LEN_2 18
52#define MSB 1
53#define LSB 0
54
55/* Debug switch */
56#ifdef CDBG
57#undef CDBG
58#endif
59#ifdef CDBG_HIGH
60#undef CDBG_HIGH
61#endif
62
63/*#define OV5647_VERBOSE_DGB*/
64
65#ifdef OV5647_VERBOSE_DGB
66#define CDBG(fmt, args...) pr_debug(fmt, ##args)
67#define CDBG_HIGH(fmt, args...) pr_debug(fmt, ##args)
68#else
69#define CDBG(fmt, args...) do { } while (0)
70#define CDBG_HIGH(fmt, args...) pr_debug(fmt, ##args)
71#endif
72
73/*for debug*/
74#ifdef CDBG
75#undef CDBG
76#endif
77#define CDBG(fmt, args...) printk(fmt, ##args)
78
79static uint8_t mode_mask = 0x09;
80struct ov5647_work_t {
81 struct work_struct work;
82};
83
84static struct ov5647_work_t *ov5647_sensorw;
85static struct ov5647_work_t *ov5647_af_sensorw;
86static struct i2c_client *ov5647_af_client;
87static struct i2c_client *ov5647_client;
88
89struct ov5647_ctrl_t {
90 const struct msm_camera_sensor_info *sensordata;
91
92 uint32_t sensormode;
93 uint32_t fps_divider;/* init to 1 * 0x00000400 */
94 uint32_t pict_fps_divider;/* init to 1 * 0x00000400 */
95 uint16_t fps;
96
97 uint16_t curr_lens_pos;
98 uint16_t curr_step_pos;
99 uint16_t my_reg_gain;
100 uint32_t my_reg_line_count;
101 uint16_t total_lines_per_frame;
102
103 enum ov5647_resolution_t prev_res;
104 enum ov5647_resolution_t pict_res;
105 enum ov5647_resolution_t curr_res;
106 enum ov5647_test_mode_t set_test;
107};
108
109static bool CSI_CONFIG;
110static struct ov5647_ctrl_t *ov5647_ctrl;
111
112static DECLARE_WAIT_QUEUE_HEAD(ov5647_wait_queue);
113static DECLARE_WAIT_QUEUE_HEAD(ov5647_af_wait_queue);
114DEFINE_MUTEX(ov5647_mut);
115
116static uint16_t prev_line_length_pck;
117static uint16_t prev_frame_length_lines;
118static uint16_t snap_line_length_pck;
119static uint16_t snap_frame_length_lines;
120
121static int ov5647_i2c_rxdata(unsigned short saddr,
122 unsigned char *rxdata, int length)
123{
124 struct i2c_msg msgs[] = {
125 {
126 .addr = saddr,
127 .flags = 0,
128 .len = 2,
129 .buf = rxdata,
130 },
131 {
132 .addr = saddr,
133 .flags = I2C_M_RD,
134 .len = 1,
135 .buf = rxdata,
136 },
137 };
138 if (i2c_transfer(ov5647_client->adapter, msgs, 2) < 0) {
139 CDBG("ov5647_i2c_rxdata faild 0x%x\n", saddr);
140 return -EIO;
141 }
142 return 0;
143}
144
145static int32_t ov5647_i2c_txdata(unsigned short saddr,
146 unsigned char *txdata, int length)
147{
148 struct i2c_msg msg[] = {
149 {
150 .addr = saddr,
151 .flags = 0,
152 .len = length,
153 .buf = txdata,
154 },
155 };
156 if (i2c_transfer(ov5647_client->adapter, msg, 1) < 0) {
157 CDBG("ov5647_i2c_txdata faild 0x%x\n", saddr);
158 return -EIO;
159 }
160
161 return 0;
162}
163
164static int32_t ov5647_i2c_read(unsigned short raddr,
165 unsigned short *rdata)
166{
167 int32_t rc = 0;
168 unsigned char buf[2];
169
170 if (!rdata)
171 return -EIO;
172 CDBG("%s:saddr:0x%x raddr:0x%x data:0x%x",
173 __func__, ov5647_client->addr, raddr, *rdata);
174 memset(buf, 0, sizeof(buf));
175 buf[0] = (raddr & 0xFF00) >> 8;
176 buf[1] = (raddr & 0x00FF);
177 rc = ov5647_i2c_rxdata(ov5647_client->addr >> 1, buf, 1);
178 if (rc < 0) {
179 CDBG("ov5647_i2c_read 0x%x failed!\n", raddr);
180 return rc;
181 }
182 *rdata = buf[0];
183 CDBG("ov5647_i2c_read 0x%x val = 0x%x!\n", raddr, *rdata);
184
185 return rc;
186}
187
188static int32_t ov5647_i2c_write_b_sensor(unsigned short waddr, uint8_t bdata)
189{
190 int32_t rc = -EFAULT;
191 unsigned char buf[3];
192
193 memset(buf, 0, sizeof(buf));
194 buf[0] = (waddr & 0xFF00) >> 8;
195 buf[1] = (waddr & 0x00FF);
196 buf[2] = bdata;
197 CDBG("i2c_write_b addr = 0x%x, val = 0x%x\n", waddr, bdata);
198 rc = ov5647_i2c_txdata(ov5647_client->addr >> 1, buf, 3);
199 if (rc < 0) {
200 pr_err("i2c_write_b failed, addr = 0x%x, val = 0x%x!\n",
201 waddr, bdata);
202 }
203 return rc;
204}
205
206static int32_t ov5647_i2c_write_b_table(struct ov5647_i2c_reg_conf const
207 *reg_conf_tbl, int num)
208{
209 int i;
210 int32_t rc = -EIO;
211
212 for (i = 0; i < num; i++) {
213 rc = ov5647_i2c_write_b_sensor(reg_conf_tbl->waddr,
214 reg_conf_tbl->wdata);
215 if (rc < 0)
216 break;
217 reg_conf_tbl++;
218 }
219 return rc;
220}
221
222static int32_t ov5647_af_i2c_txdata(unsigned short saddr,
223 unsigned char *txdata, int length)
224{
225 struct i2c_msg msg[] = {
226 {
227 .addr = saddr,
228 .flags = 0,
229 .len = length,
230 .buf = txdata,
231 },
232 };
233 if (i2c_transfer(ov5647_af_client->adapter, msg, 1) < 0) {
234 pr_err("ov5647_af_i2c_txdata faild 0x%x\n", saddr);
235 return -EIO;
236 }
237
238 return 0;
239}
240
241static int32_t ov5647_af_i2c_write_b_sensor(uint8_t waddr, uint8_t bdata)
242{
243 int32_t rc = -EFAULT;
244 unsigned char buf[2];
245
246 memset(buf, 0, sizeof(buf));
247 buf[0] = waddr;
248 buf[1] = bdata;
249 CDBG("i2c_write_b addr = 0x%x, val = 0x%x\n", waddr, bdata);
250 rc = ov5647_af_i2c_txdata(ov5647_af_client->addr, buf, 2);
251 if (rc < 0) {
252 pr_err("i2c_write_b failed, addr = 0x%x, val = 0x%x!\n",
253 waddr, bdata);
254 }
255 return rc;
256}
257
258static void ov5647_start_stream(void)
259{
260 CDBG("CAMERA_DBG: 0x4202 0x0, stream on...\r\n");
261 ov5647_i2c_write_b_sensor(0x4202, 0x00);/* streaming on */
262}
263
264static void ov5647_stop_stream(void)
265{
266 CDBG("CAMERA_DBG: 0x4202 0xf, stream off...\r\n");
267 ov5647_i2c_write_b_sensor(0x4202, 0x0f);/* streaming off */
268}
269
270static void ov5647_group_hold_on(void)
271{
272 ov5647_i2c_write_b_sensor(0x0104, 0x01);
273}
274
275static void ov5647_group_hold_off(void)
276{
277 ov5647_i2c_write_b_sensor(0x0104, 0x0);
278}
279
280static void ov5647_get_pict_fps(uint16_t fps, uint16_t *pfps)
281{
282 /* input fps is preview fps in Q8 format */
283 uint32_t divider, d1, d2;
284 uint32_t preview_pclk = 0x37, snapshot_pclk = 0x4f;
285
286 d1 = (prev_frame_length_lines * 0x00000400) / snap_frame_length_lines;
287 d2 = (prev_line_length_pck * 0x00000400) / snap_line_length_pck;
288 divider = (d1 * d2*preview_pclk/snapshot_pclk) / 0x400;
289 CDBG(KERN_ERR "ov5647_get_pict_fps divider = %d", divider);
290 /*Verify PCLK settings and frame sizes.*/
291 *pfps = (uint16_t) (fps * divider / 0x400);
292}
293
294static uint16_t ov5647_get_prev_lines_pf(void)
295{
296 if (ov5647_ctrl->prev_res == QTR_SIZE)
297 return prev_frame_length_lines;
298 else
299 return snap_frame_length_lines;
300}
301
302static uint16_t ov5647_get_prev_pixels_pl(void)
303{
304 if (ov5647_ctrl->prev_res == QTR_SIZE)
305 return prev_line_length_pck;
306 else
307 return snap_line_length_pck;
308}
309
310static uint16_t ov5647_get_pict_lines_pf(void)
311{
312 if (ov5647_ctrl->pict_res == QTR_SIZE)
313 return prev_frame_length_lines;
314 else
315 return snap_frame_length_lines;
316}
317
318static uint16_t ov5647_get_pict_pixels_pl(void)
319{
320 if (ov5647_ctrl->pict_res == QTR_SIZE)
321 return prev_line_length_pck;
322 else
323 return snap_line_length_pck;
324}
325
326static uint32_t ov5647_get_pict_max_exp_lc(void)
327{
328 return snap_frame_length_lines * 24;
329}
330
331static int32_t ov5647_set_fps(struct fps_cfg *fps)
332{
333 uint16_t total_lines_per_frame;
334 int32_t rc = 0;
335
336 ov5647_ctrl->fps_divider = fps->fps_div;
337 ov5647_ctrl->pict_fps_divider = fps->pict_fps_div;
338
339 if (ov5647_ctrl->sensormode == SENSOR_PREVIEW_MODE) {
340 total_lines_per_frame = (uint16_t)
341 ((prev_frame_length_lines * ov5647_ctrl->fps_divider) / 0x400);
342 } else {
343 total_lines_per_frame = (uint16_t)
344 ((snap_frame_length_lines * ov5647_ctrl->fps_divider) / 0x400);
345 }
346
347 ov5647_group_hold_on();
348 rc = ov5647_i2c_write_b_sensor(0x0340,
349 ((total_lines_per_frame & 0xFF00) >> 8));
350 rc = ov5647_i2c_write_b_sensor(0x0341,
351 (total_lines_per_frame & 0x00FF));
352 ov5647_group_hold_off();
353
354 return rc;
355}
356
357static inline uint8_t ov5647_byte(uint16_t word, uint8_t offset)
358{
359 return word >> (offset * BITS_PER_BYTE);
360}
361
362static int32_t ov5647_write_exp_gain(uint16_t gain, uint32_t line)
363{
364 int rc = 0;
365 uint16_t max_line;
366 u8 intg_time_hsb, intg_time_msb, intg_time_lsb;
367 uint8_t gain_lsb, gain_hsb;
368 ov5647_ctrl->my_reg_gain = gain;
369 ov5647_ctrl->my_reg_line_count = (uint16_t)line;
370
371 CDBG(KERN_ERR "preview exposure setting 0x%x, 0x%x, %d",
372 gain, line, line);
373
374 gain_lsb = (uint8_t) (ov5647_ctrl->my_reg_gain);
375 gain_hsb = (uint8_t)((ov5647_ctrl->my_reg_gain & 0x300)>>8);
376 /* adjust frame rate */
377 if (line > 980) {
378 rc = ov5647_i2c_write_b_sensor(0x380E,
379 (uint8_t)((line+4) >> 8)) ;
380 rc = ov5647_i2c_write_b_sensor(0x380F,
381 (uint8_t)((line+4) & 0x00FF)) ;
382 max_line = line + 4;
383 } else if (max_line > 984) {
384 rc = ov5647_i2c_write_b_sensor(0x380E,
385 (uint8_t)(984 >> 8)) ;
386 rc = ov5647_i2c_write_b_sensor(0x380F,
387 (uint8_t)(984 & 0x00FF)) ;
388 max_line = 984;
389 }
390
391 line = line<<4;
392 /* ov5647 need this operation */
393 intg_time_hsb = (u8)(line>>16);
394 intg_time_msb = (u8) ((line & 0xFF00) >> 8);
395 intg_time_lsb = (u8) (line & 0x00FF);
396
397 ov5647_group_hold_on();
398 rc = ov5647_i2c_write_b_sensor(REG_OV5647_LINE_HSB, intg_time_hsb) ;
399 rc = ov5647_i2c_write_b_sensor(REG_OV5647_LINE_MSB, intg_time_msb) ;
400 rc = ov5647_i2c_write_b_sensor(REG_OV5647_LINE_LSB, intg_time_lsb) ;
401
402 rc = ov5647_i2c_write_b_sensor(REG_OV5647_GAIN_MSB, gain_hsb) ;
403 rc = ov5647_i2c_write_b_sensor(REG_OV5647_GAIN_LSB, gain_lsb) ;
404 ov5647_group_hold_off();
405
406 return rc;
407}
408
409
410static int32_t ov5647_set_pict_exp_gain(uint16_t gain, uint32_t line)
411{
412 uint16_t max_line;
413 int rc = 0;
414 uint8_t gain_lsb, gain_hsb;
415 u8 intg_time_hsb, intg_time_msb, intg_time_lsb;
416
417 ov5647_ctrl->my_reg_gain = gain;
418 ov5647_ctrl->my_reg_line_count = (uint16_t)line;
419
420 gain_lsb = (uint8_t) (ov5647_ctrl->my_reg_gain);
421 gain_hsb = (uint8_t)((ov5647_ctrl->my_reg_gain & 0x300)>>8);
422
423 CDBG(KERN_ERR "snapshot exposure seting 0x%x, 0x%x, %d"
424 , gain, line, line);
425
426 if (line > 1964) {
427 rc = ov5647_i2c_write_b_sensor(0x380E,
428 (uint8_t)((line+4) >> 8)) ;
429 rc = ov5647_i2c_write_b_sensor(0x380F,
430 (uint8_t)((line+4) & 0x00FF)) ;
431 max_line = line + 4;
432 } else if (max_line > 1968) {
433 rc = ov5647_i2c_write_b_sensor(0x380E,
434 (uint8_t)(1968 >> 8)) ;
435 rc = ov5647_i2c_write_b_sensor(0x380F,
436 (uint8_t)(1968 & 0x00FF)) ;
437 max_line = 1968;
438 }
439 line = line<<4;
440 /* ov5647 need this operation */
441 intg_time_hsb = (u8)(line>>16);
442 intg_time_msb = (u8) ((line & 0xFF00) >> 8);
443 intg_time_lsb = (u8) (line & 0x00FF);
444
445 /* FIXME for BLC trigger */
446 ov5647_group_hold_on();
447 rc = ov5647_i2c_write_b_sensor(REG_OV5647_LINE_HSB, intg_time_hsb) ;
448 rc = ov5647_i2c_write_b_sensor(REG_OV5647_LINE_MSB, intg_time_msb) ;
449 rc = ov5647_i2c_write_b_sensor(REG_OV5647_LINE_LSB, intg_time_lsb) ;
450
451 rc = ov5647_i2c_write_b_sensor(REG_OV5647_GAIN_MSB, gain_hsb) ;
452 rc = ov5647_i2c_write_b_sensor(REG_OV5647_GAIN_LSB, gain_lsb - 1) ;
453
454 rc = ov5647_i2c_write_b_sensor(REG_OV5647_LINE_HSB, intg_time_hsb) ;
455 rc = ov5647_i2c_write_b_sensor(REG_OV5647_LINE_MSB, intg_time_msb) ;
456 rc = ov5647_i2c_write_b_sensor(REG_OV5647_LINE_LSB, intg_time_lsb) ;
457
458 rc = ov5647_i2c_write_b_sensor(REG_OV5647_GAIN_MSB, gain_hsb) ;
459 rc = ov5647_i2c_write_b_sensor(REG_OV5647_GAIN_LSB, gain_lsb) ;
460 ov5647_group_hold_off();
461
462 msleep(500);
463 return rc;
464
465}
466
467static int32_t ov5647_move_focus(int direction, int32_t num_steps)
468{
469 uint8_t code_val_msb = 0;
470 uint8_t code_val_lsb = 0;
471 int16_t step_direction, actual_step, next_position;
472 int rc;
473
474 if (num_steps == 0)
475 return 0;
476
477 if (direction == MOVE_NEAR)
478 step_direction = 20;
479 else if (direction == MOVE_FAR)
480 step_direction = -20;
481 else
482 return -EINVAL;
483
484 actual_step = (int16_t)(step_direction * num_steps);
485 next_position = (int16_t)ov5647_ctrl->curr_lens_pos + actual_step;
486 if (next_position < 0) {
487 CDBG(KERN_ERR "%s: OV5647 position(=%d) out of range",
488 __func__, next_position);
489 next_position = 0;
490 }
491 if (next_position > 0x3FF) {
492 CDBG(KERN_ERR "%s: OV5647 position(=%d) out of range",
493 __func__, next_position);
494 next_position = 0x3FF;
495 }
496 ov5647_ctrl->curr_lens_pos = next_position;
497
498 code_val_msb = (uint8_t)((ov5647_ctrl->curr_lens_pos & 0x03FF) >> 4);
499 code_val_lsb = (uint8_t)((ov5647_ctrl->curr_lens_pos & 0x000F) << 4);
500 code_val_lsb |= mode_mask;
501
502 rc = ov5647_af_i2c_write_b_sensor(code_val_msb, code_val_lsb);
503 /* DAC Setting */
504 if (rc != 0) {
505 CDBG(KERN_ERR "%s: WRITE ERROR lsb = 0x%x, msb = 0x%x",
506 __func__, code_val_lsb, code_val_msb);
507 } else {
508 CDBG(KERN_ERR "%s: Successful lsb = 0x%x, msb = 0x%x",
509 __func__, code_val_lsb, code_val_msb);
510 /* delay may set based on the steps moved
511 when I2C write successful */
512 msleep(100);
513 }
514 return 0;
515}
516
517static int32_t ov5647_set_default_focus(uint8_t af_step)
518{
519 uint8_t code_val_msb = 0;
520 uint8_t code_val_lsb = 0;
521 int rc = 0;
522
523 ov5647_ctrl->curr_lens_pos = 200;
524
525
526 code_val_msb = (ov5647_ctrl->curr_lens_pos & 0x03FF) >> 4;
527 code_val_lsb = (ov5647_ctrl->curr_lens_pos & 0x000F) << 4;
528 code_val_lsb |= mode_mask;
529
530 CDBG(KERN_ERR "ov5647_set_default_focus:lens pos = %d",
531 ov5647_ctrl->curr_lens_pos);
532 rc = ov5647_af_i2c_write_b_sensor(code_val_msb, code_val_lsb);
533 /* DAC Setting */
534 if (rc != 0)
535 CDBG(KERN_ERR "%s: WRITE ERROR lsb = 0x%x, msb = 0x%x",
536 __func__, code_val_lsb, code_val_msb);
537 else
538 CDBG(KERN_ERR "%s: WRITE successful lsb = 0x%x, msb = 0x%x",
539 __func__, code_val_lsb, code_val_msb);
540
541 usleep_range(10000, 11000);
542 return 0;
543}
544
545static int32_t ov5647_test(enum ov5647_test_mode_t mo)
546{
547 int32_t rc = 0;
548
549 if (mo != TEST_OFF)
550 rc = ov5647_i2c_write_b_sensor(0x0601, (uint8_t) mo);
551
552 return rc;
553}
554
555static void ov5647_reset_sensor(void)
556{
557 ov5647_i2c_write_b_sensor(0x103, 0x1);
558}
559
560
561static int32_t ov5647_sensor_setting(int update_type, int rt)
562{
563
564 int32_t rc = 0;
565 struct msm_camera_csi_params ov5647_csi_params;
566
567 ov5647_stop_stream();
568
569 /* wait for clk/data really stop */
570 if ((rt == RES_CAPTURE) || (CSI_CONFIG == 0))
571 msleep(66);
572 else
573 msleep(266);
574
575 CDBG("CAMERA_DBG1: 0x4800 regVal:0x25\r\n");
576 ov5647_i2c_write_b_sensor(0x4800, 0x25);/* streaming off */
577
578 usleep_range(10000, 11000);
579
580 if (update_type == REG_INIT) {
581 ov5647_reset_sensor();
582 ov5647_i2c_write_b_table(ov5647_regs.rec_settings,
583 ov5647_regs.rec_size);
584 CSI_CONFIG = 0;
585 } else if (update_type == UPDATE_PERIODIC) {
586 /* turn off flash when preview */
587
588 if (rt == RES_PREVIEW) {
589 ov5647_i2c_write_b_table(ov5647_regs.reg_prev,
590 ov5647_regs.reg_prev_size);
591 CDBG("CAMERA_DBG:preview settings...\r\n");
592 } else {
593 ov5647_i2c_write_b_table(ov5647_regs.reg_snap,
594 ov5647_regs.reg_snap_size);
595 CDBG("CAMERA_DBG:snapshot settings...\r\n");
596 }
597
598 msleep(20);
599 if (!CSI_CONFIG) {
600 msm_camio_vfe_clk_rate_set(192000000);
601 ov5647_csi_params.data_format = CSI_8BIT;
602 ov5647_csi_params.lane_cnt = 2;
603 ov5647_csi_params.lane_assign = 0xe4;
604 ov5647_csi_params.dpcm_scheme = 0;
605 ov5647_csi_params.settle_cnt = 10;
606 rc = msm_camio_csi_config(&ov5647_csi_params);
607 msleep(20);
608 CSI_CONFIG = 1;
609 /* exit powerdown state */
610 ov5647_i2c_write_b_sensor(0x0100, 0x01);
611 }
612 CDBG("CAMERA_DBG: 0x4800 regVal:0x04\r\n");
613 /* streaming on */
614 ov5647_i2c_write_b_sensor(0x4800, 0x04);
615 msleep(266);
616 ov5647_start_stream();
617 msleep(30);
618 }
619 return rc;
620}
621
622static int32_t ov5647_video_config(int mode)
623{
624 int32_t rc = 0;
625 int rt;
626 CDBG("video config\n");
627 /* change sensor resolution if needed */
628 if (ov5647_ctrl->prev_res == QTR_SIZE)
629 rt = RES_PREVIEW;
630 else
631 rt = RES_CAPTURE;
632 if (ov5647_sensor_setting(UPDATE_PERIODIC, rt) < 0)
633 return rc;
634 if (ov5647_ctrl->set_test) {
635 if (ov5647_test(ov5647_ctrl->set_test) < 0)
636 return rc;
637 }
638
639 ov5647_ctrl->curr_res = ov5647_ctrl->prev_res;
640 ov5647_ctrl->sensormode = mode;
641 return rc;
642}
643
644static int32_t ov5647_snapshot_config(int mode)
645{
646 int32_t rc = 0;
647 int rt;
648
649 /*change sensor resolution if needed */
650 if (ov5647_ctrl->curr_res != ov5647_ctrl->pict_res) {
651 if (ov5647_ctrl->pict_res == QTR_SIZE)
652 rt = RES_PREVIEW;
653 else
654 rt = RES_CAPTURE;
655 if (ov5647_sensor_setting(UPDATE_PERIODIC, rt) < 0)
656 return rc;
657 }
658
659 ov5647_ctrl->curr_res = ov5647_ctrl->pict_res;
660 ov5647_ctrl->sensormode = mode;
661 return rc;
662}
663
664static int32_t ov5647_raw_snapshot_config(int mode)
665{
666 int32_t rc = 0;
667 int rt;
668
669 /* change sensor resolution if needed */
670 if (ov5647_ctrl->curr_res != ov5647_ctrl->pict_res) {
671 if (ov5647_ctrl->pict_res == QTR_SIZE)
672 rt = RES_PREVIEW;
673 else
674 rt = RES_CAPTURE;
675 if (ov5647_sensor_setting(UPDATE_PERIODIC, rt) < 0)
676 return rc;
677 }
678
679 ov5647_ctrl->curr_res = ov5647_ctrl->pict_res;
680 ov5647_ctrl->sensormode = mode;
681 return rc;
682}
683
684static int32_t ov5647_set_sensor_mode(int mode,
685 int res)
686{
687 int32_t rc = 0;
688
689 switch (mode) {
690 case SENSOR_PREVIEW_MODE:
691 rc = ov5647_video_config(mode);
692 break;
693 case SENSOR_SNAPSHOT_MODE:
694 rc = ov5647_snapshot_config(mode);
695 break;
696 case SENSOR_RAW_SNAPSHOT_MODE:
697 rc = ov5647_raw_snapshot_config(mode);
698 break;
699 default:
700 rc = -EINVAL;
701 break;
702 }
703 return rc;
704}
705
706static int32_t ov5647_power_down(void)
707{
708 ov5647_stop_stream();
709 return 0;
710}
711
712static int ov5647_probe_init_done(const struct msm_camera_sensor_info *data)
713{
714 CDBG("probe done\n");
715 gpio_direction_output(data->sensor_pwd, 1);
716 return 0;
717}
718
719static int ov5647_probe_init_sensor(const struct msm_camera_sensor_info *data)
720{
721 int32_t rc = 0;
722 uint16_t regaddress1 = 0x300a;
723 uint16_t regaddress2 = 0x300b;
724 uint16_t chipid1 = 0;
725 uint16_t chipid2 = 0;
726
727 CDBG("%s: %d\n", __func__, __LINE__);
728
729 gpio_direction_output(data->sensor_pwd, 0);
730 usleep_range(4000, 4100);
731 gpio_direction_output(data->sensor_reset, 1);
732 usleep_range(2000, 2100);
733
734 ov5647_i2c_read(regaddress1, &chipid1);
735 if (chipid1 != 0x56) {
736 rc = -ENODEV;
737 pr_err("ov5647_probe_init_sensor fail chip id doesnot match\n");
738 goto init_probe_fail;
739 }
740
741 ov5647_i2c_read(regaddress2, &chipid2);
742 if (chipid2 != 0x47) {
743 rc = -ENODEV;
744 pr_err("ov5647_probe_init_sensor fail chip id doesnot match\n");
745 goto init_probe_fail;
746 }
747
748 pr_err("ID1: 0x%x\n", chipid1);
749 pr_err("ID2: 0x%x\n", chipid2);
750 goto init_probe_done;
751
752init_probe_fail:
753 pr_err(" ov5647_probe_init_sensor fails\n");
754 ov5647_probe_init_done(data);
755 return rc;
756init_probe_done:
757 pr_debug(" ov5647_probe_init_sensor finishes\n");
758 gpio_direction_output(data->sensor_pwd, 1);
759 return rc;
760}
761
762
763static int ov5647_sensor_open_init(const struct msm_camera_sensor_info *data)
764{
765 int32_t rc = 0;
766
767 CDBG("%s: %d\n", __func__, __LINE__);
768 CDBG("Calling ov5647_sensor_open_init\n");
769
770 ov5647_ctrl = kzalloc(sizeof(struct ov5647_ctrl_t), GFP_KERNEL);
771 if (!ov5647_ctrl) {
772 CDBG("ov5647_init failed!\n");
773 rc = -ENOMEM;
774 goto init_done;
775 }
776 ov5647_ctrl->fps_divider = 1 * 0x00000400;
777 ov5647_ctrl->pict_fps_divider = 1 * 0x00000400;
778 ov5647_ctrl->set_test = TEST_OFF;
779 ov5647_ctrl->prev_res = QTR_SIZE;
780 ov5647_ctrl->pict_res = FULL_SIZE;
781
782 if (data)
783 ov5647_ctrl->sensordata = data;
784
785 prev_frame_length_lines = 0x3d8;
786
787 prev_line_length_pck = 0x768*2;
788
789 snap_frame_length_lines = 0x7b0;
790
791 snap_line_length_pck = 0xa8c;
792
793 /* enable mclk first */
794 msm_camio_clk_rate_set(OV5647_MASTER_CLK_RATE);
795
796 gpio_direction_output(data->sensor_pwd, 1);
797 gpio_direction_output(data->sensor_reset, 0);
798 usleep_range(10000, 11000);
799 /* power on camera ldo and vreg */
800 if (ov5647_ctrl->sensordata->pmic_gpio_enable)
801 lcd_camera_power_onoff(1);
802 usleep_range(10000, 11000); /*waiting for ldo stable*/
803 gpio_direction_output(data->sensor_pwd, 0);
804 msleep(20);
805 gpio_direction_output(data->sensor_reset, 1);
806 msleep(25);
807
808 CDBG("init settings\n");
809 if (ov5647_ctrl->prev_res == QTR_SIZE)
810 rc = ov5647_sensor_setting(REG_INIT, RES_PREVIEW);
811 else
812 rc = ov5647_sensor_setting(REG_INIT, RES_CAPTURE);
813 ov5647_ctrl->fps = 30 * Q8;
814
815 /* enable AF actuator */
816 if (ov5647_ctrl->sensordata->vcm_enable) {
817 CDBG("enable AF actuator, gpio = %d\n",
818 ov5647_ctrl->sensordata->vcm_pwd);
819 rc = gpio_request(ov5647_ctrl->sensordata->vcm_pwd,
820 "ov5647_af");
821 if (!rc)
822 gpio_direction_output(
823 ov5647_ctrl->sensordata->vcm_pwd,
824 1);
825 else {
826 pr_err("ov5647_ctrl gpio request failed!\n");
827 goto init_fail;
828 }
829 msleep(20);
830 rc = ov5647_set_default_focus(0);
831 if (rc < 0) {
832 gpio_direction_output(ov5647_ctrl->sensordata->vcm_pwd,
833 0);
834 gpio_free(ov5647_ctrl->sensordata->vcm_pwd);
835 }
836 }
837 if (rc < 0)
838 goto init_fail;
839 else
840 goto init_done;
841init_fail:
842 CDBG("init_fail\n");
843 ov5647_probe_init_done(data);
844 /* No need to power OFF camera ldo and vreg
845 affects Display while resume */
846init_done:
847 CDBG("init_done\n");
848 return rc;
849}
850
851static int ov5647_i2c_remove(struct i2c_client *client)
852{
853 return 0;
854}
855
856static int ov5647_init_client(struct i2c_client *client)
857{
858 /* Initialize the MSM_CAMI2C Chip */
859 init_waitqueue_head(&ov5647_wait_queue);
860 return 0;
861}
862
863static int ov5647_af_init_client(struct i2c_client *client)
864{
865 /* Initialize the MSM_CAMI2C Chip */
866 init_waitqueue_head(&ov5647_af_wait_queue);
867 return 0;
868}
869
870static const struct i2c_device_id ov5647_af_i2c_id[] = {
871 {"ov5647_af", 0},
872 { }
873};
874
875static int ov5647_af_i2c_probe(struct i2c_client *client,
876 const struct i2c_device_id *id)
877{
878 int rc = 0;
879 CDBG("ov5647_af_probe called!\n");
880
881 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
882 CDBG("i2c_check_functionality failed\n");
883 goto probe_failure;
884 }
885
886 ov5647_af_sensorw = kzalloc(sizeof(struct ov5647_work_t), GFP_KERNEL);
887 if (!ov5647_af_sensorw) {
888 CDBG("kzalloc failed.\n");
889 rc = -ENOMEM;
890 goto probe_failure;
891 }
892
893 i2c_set_clientdata(client, ov5647_af_sensorw);
894 ov5647_af_init_client(client);
895 ov5647_af_client = client;
896
897 msleep(50);
898
899 CDBG("ov5647_af_probe successed! rc = %d\n", rc);
900 return 0;
901
902probe_failure:
903 CDBG("ov5647_af_probe failed! rc = %d\n", rc);
904 return rc;
905}
906
907static const struct i2c_device_id ov5647_i2c_id[] = {
908 {"ov5647", 0}, {}
909};
910
911static int ov5647_i2c_probe(struct i2c_client *client,
912 const struct i2c_device_id *id)
913{
914 int rc = 0;
915 CDBG("ov5647_probe called!\n");
916
917 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
918 CDBG("i2c_check_functionality failed\n");
919 goto probe_failure;
920 }
921
922 ov5647_sensorw = kzalloc(sizeof(struct ov5647_work_t), GFP_KERNEL);
923 if (!ov5647_sensorw) {
924 CDBG("kzalloc failed.\n");
925 rc = -ENOMEM;
926 goto probe_failure;
927 }
928
929 i2c_set_clientdata(client, ov5647_sensorw);
930 ov5647_init_client(client);
931 ov5647_client = client;
932
933 msleep(50);
934
935 CDBG("ov5647_probe successed! rc = %d\n", rc);
936 return 0;
937
938probe_failure:
939 CDBG("ov5647_probe failed! rc = %d\n", rc);
940 return rc;
941}
942
943static int __devexit ov5647_remove(struct i2c_client *client)
944{
945 struct ov5647_work_t *sensorw = i2c_get_clientdata(client);
946 free_irq(client->irq, sensorw);
947 ov5647_client = NULL;
948 kfree(sensorw);
949 return 0;
950}
951
952static int __devexit ov5647_af_remove(struct i2c_client *client)
953{
954 struct ov5647_work_t *ov5647_af = i2c_get_clientdata(client);
955 free_irq(client->irq, ov5647_af);
956 ov5647_af_client = NULL;
957 kfree(ov5647_af);
958 return 0;
959}
960
961static struct i2c_driver ov5647_i2c_driver = {
962 .id_table = ov5647_i2c_id,
963 .probe = ov5647_i2c_probe,
964 .remove = ov5647_i2c_remove,
965 .driver = {
966 .name = "ov5647",
967 },
968};
969
970static struct i2c_driver ov5647_af_i2c_driver = {
971 .id_table = ov5647_af_i2c_id,
972 .probe = ov5647_af_i2c_probe,
973 .remove = __exit_p(ov5647_af_i2c_remove),
974 .driver = {
975 .name = "ov5647_af",
976 },
977};
978
979int ov5647_sensor_config(void __user *argp)
980{
981 struct sensor_cfg_data cdata;
982 long rc = 0;
983 if (copy_from_user(&cdata,
984 (void *)argp,
985 sizeof(struct sensor_cfg_data)))
986 return -EFAULT;
987 mutex_lock(&ov5647_mut);
988 CDBG("ov5647_sensor_config: cfgtype = %d\n",
989 cdata.cfgtype);
990 switch (cdata.cfgtype) {
991 case CFG_GET_PICT_FPS:
992 ov5647_get_pict_fps(
993 cdata.cfg.gfps.prevfps,
994 &(cdata.cfg.gfps.pictfps));
995
996 if (copy_to_user((void *)argp,
997 &cdata,
998 sizeof(struct sensor_cfg_data)))
999 rc = -EFAULT;
1000 break;
1001 case CFG_GET_PREV_L_PF:
1002 cdata.cfg.prevl_pf =
1003 ov5647_get_prev_lines_pf();
1004
1005 if (copy_to_user((void *)argp,
1006 &cdata,
1007 sizeof(struct sensor_cfg_data)))
1008 rc = -EFAULT;
1009 break;
1010 case CFG_GET_PREV_P_PL:
1011 cdata.cfg.prevp_pl =
1012 ov5647_get_prev_pixels_pl();
1013
1014 if (copy_to_user((void *)argp,
1015 &cdata,
1016 sizeof(struct sensor_cfg_data)))
1017 rc = -EFAULT;
1018 break;
1019 case CFG_GET_PICT_L_PF:
1020 cdata.cfg.pictl_pf =
1021 ov5647_get_pict_lines_pf();
1022
1023 if (copy_to_user((void *)argp,
1024 &cdata,
1025 sizeof(struct sensor_cfg_data)))
1026 rc = -EFAULT;
1027 break;
1028 case CFG_GET_PICT_P_PL:
1029 cdata.cfg.pictp_pl =
1030 ov5647_get_pict_pixels_pl();
1031 if (copy_to_user((void *)argp,
1032 &cdata,
1033 sizeof(struct sensor_cfg_data)))
1034 rc = -EFAULT;
1035 break;
1036 case CFG_GET_PICT_MAX_EXP_LC:
1037 cdata.cfg.pict_max_exp_lc =
1038 ov5647_get_pict_max_exp_lc();
1039
1040 if (copy_to_user((void *)argp,
1041 &cdata,
1042 sizeof(struct sensor_cfg_data)))
1043 rc = -EFAULT;
1044 break;
1045 case CFG_SET_FPS:
1046 case CFG_SET_PICT_FPS:
1047 rc = ov5647_set_fps(&(cdata.cfg.fps));
1048 break;
1049 case CFG_SET_EXP_GAIN:
1050 rc = ov5647_write_exp_gain(cdata.cfg.exp_gain.gain,
1051 cdata.cfg.exp_gain.line);
1052 break;
1053 case CFG_SET_PICT_EXP_GAIN:
1054 rc = ov5647_set_pict_exp_gain(cdata.cfg.exp_gain.gain,
1055 cdata.cfg.exp_gain.line);
1056 break;
1057 case CFG_SET_MODE:
1058 rc = ov5647_set_sensor_mode(cdata.mode, cdata.rs);
1059 break;
1060 case CFG_PWR_DOWN:
1061 rc = ov5647_power_down();
1062 break;
1063 case CFG_MOVE_FOCUS:
1064 rc = ov5647_move_focus(cdata.cfg.focus.dir,
1065 cdata.cfg.focus.steps);
1066 break;
1067 case CFG_SET_DEFAULT_FOCUS:
1068 rc = ov5647_set_default_focus(cdata.cfg.focus.steps);
1069 break;
1070
1071 case CFG_GET_AF_MAX_STEPS:
1072 cdata.max_steps = OV5647_TOTAL_STEPS_NEAR_TO_FAR;
1073 if (copy_to_user((void *)argp,
1074 &cdata,
1075 sizeof(struct sensor_cfg_data)))
1076 rc = -EFAULT;
1077 break;
1078 case CFG_SET_EFFECT:
1079 rc = ov5647_set_default_focus(cdata.cfg.effect);
1080 break;
1081 default:
1082 rc = -EFAULT;
1083 break;
1084 }
1085 mutex_unlock(&ov5647_mut);
1086
1087 return rc;
1088}
1089
1090static int ov5647_sensor_release(void)
1091{
1092 int rc = -EBADF;
1093 unsigned short rdata;
1094
1095 mutex_lock(&ov5647_mut);
1096 ov5647_power_down();
1097 msleep(20);
1098 ov5647_i2c_read(0x3018, &rdata);
1099 rdata |= 0x18; /*set bit 3 bit 4 to 1*/
1100 ov5647_i2c_write_b_sensor(0x3018, rdata);/*write back*/
1101 msleep(20);
1102
1103 gpio_set_value(ov5647_ctrl->sensordata->sensor_pwd, 1);
1104 usleep_range(5000, 5100);
1105 if (ov5647_ctrl->sensordata->vcm_enable) {
1106 gpio_direction_output(ov5647_ctrl->sensordata->vcm_pwd, 0);
1107 gpio_free(ov5647_ctrl->sensordata->vcm_pwd);
1108 }
1109
1110 /* No need to power OFF camera ldo and vreg
1111 affects Display while resume */
1112
1113 kfree(ov5647_ctrl);
1114 ov5647_ctrl = NULL;
1115 CDBG("ov5647_release completed\n");
1116 mutex_unlock(&ov5647_mut);
1117
1118 return rc;
1119}
1120
1121static int ov5647_sensor_probe(const struct msm_camera_sensor_info *info,
1122 struct msm_sensor_ctrl *s)
1123{
1124 int rc = 0;
1125
1126 CDBG("%s E\n", __func__);
1127
1128 gpio_direction_output(info->sensor_pwd, 1);
1129 gpio_direction_output(info->sensor_reset, 0);
1130 usleep_range(1000, 1100);
1131 /* turn on ldo and vreg */
1132 if (info->pmic_gpio_enable)
1133 lcd_camera_power_onoff(1);
1134
1135 rc = i2c_add_driver(&ov5647_i2c_driver);
1136 if (rc < 0 || ov5647_client == NULL) {
1137 rc = -ENOTSUPP;
1138 CDBG("I2C add driver ov5647 failed");
1139 goto probe_fail_2;
1140 }
1141 if (info->vcm_enable) {
1142 rc = i2c_add_driver(&ov5647_af_i2c_driver);
1143 if (rc < 0 || ov5647_af_client == NULL) {
1144 rc = -ENOTSUPP;
1145 CDBG("I2C add driver ov5647 af failed");
1146 goto probe_fail_3;
1147 }
1148 }
1149 msm_camio_clk_rate_set(OV5647_MASTER_CLK_RATE);
1150
1151 rc = ov5647_probe_init_sensor(info);
1152 if (rc < 0)
1153 goto probe_fail_1;
1154
1155 s->s_init = ov5647_sensor_open_init;
1156 s->s_release = ov5647_sensor_release;
1157 s->s_config = ov5647_sensor_config;
1158 s->s_mount_angle = info->sensor_platform_info->mount_angle;
1159 gpio_set_value(info->sensor_pwd, 1);
1160 ov5647_probe_init_done(info);
1161 /* turn off ldo and vreg */
1162 if (info->pmic_gpio_enable)
1163 lcd_camera_power_onoff(0);
1164
1165 CDBG("%s X", __func__);
1166 return rc;
1167
1168probe_fail_3:
1169 i2c_del_driver(&ov5647_af_i2c_driver);
1170probe_fail_2:
1171 i2c_del_driver(&ov5647_i2c_driver);
1172probe_fail_1:
1173 /* turn off ldo and vreg */
1174 if (info->pmic_gpio_enable)
1175 lcd_camera_power_onoff(0);
1176 CDBG("ov5647_sensor_probe: SENSOR PROBE FAILS!\n");
1177 CDBG("%s X", __func__);
1178 return rc;
1179}
1180
1181static int __devinit ov5647_probe(struct platform_device *pdev)
1182{
1183 return msm_camera_drv_start(pdev, ov5647_sensor_probe);
1184}
1185
1186static struct platform_driver msm_camera_driver = {
1187 .probe = ov5647_probe,
1188 .driver = {
1189 .name = "msm_camera_ov5647",
1190 .owner = THIS_MODULE,
1191 },
1192};
1193
1194static int __init ov5647_init(void)
1195{
1196 return platform_driver_register(&msm_camera_driver);
1197}
1198
1199module_init(ov5647_init);
1200MODULE_DESCRIPTION("Omnivision 5 MP Bayer sensor driver");
1201MODULE_LICENSE("GPL v2");