linux GT2005攝像頭驅動源代碼分析

由於近期在研究Android的一些視頻通話的一些技術,所以很快就發現實現攝像頭的驅動成爲必須研究的課題。下面是在telechips公司提供的Android SDK中已經包含的GT2005型號sensor的部分驅動代碼。雖然已經有了比較完善的驅動代碼,但是我TCC8902板子上的GT2005攝像頭還是沒能preview出圖像來,最近一直在試圖進行各種方法的調試和分析。最後覺得硬着頭皮來好好研究GT2005 camera的driver,我會分幾期來進行分析,如果有不正確的地方,非常歡迎指正。

 

下面的代碼是GT2005_2mp.h(GT2005 2百萬像素攝像頭頭文件)

  1. /* 
  2.  * drivers/media/video/tcc83xx/GT2005_2mp.h 
  3.  * 
  4.  * Register definitions for the GT2005 CameraChip. 
  5.  * 
  6.  * Author: zzau ([email protected]) 
  7.  * 
  8.  * Copyright (C) 2008 Telechips, Inc. 
  9.  * 
  10.  * This file is licensed under the terms of the GNU General Public License  
  11.  * version 2. This program is licensed "as is" without any warranty of any  
  12.  * kind, whether express or implied. 
  13.  */  
  14. #include <mach/globals.h>  
  15. #include "sensor_if.h"  
  16.   
  17. #ifndef GT2005_H  
  18. #define GT2005_H  
  19.   
  20. /* The MT9D112 I2C sensor chip has a fixed slave address of 0x78. */  
  21. #define SENSOR_I2C_ADDR     0x78                            //[suting] : 攝像頭涉及I2C總線協議  
  22.   
  23. #define REG_TERM 0x0000 /* terminating list entry for reg */  
  24. #define VAL_TERM 0x0000 /* terminating list entry for val */  
  25.   
  26. //CLOCK  
  27. #define CKC_CAMERA_MCLK             240000  
  28. #define CKC_CAMERA_MCLK_SRC         DIRECTPLL1  
  29. #define CKC_CAMERA_SCLK             480000  
  30. #define CKC_CAMERA_SCLK_SRC         DIRECTPLL2  
  31.   
  32. #define FRAMESKIP_COUNT_FOR_CAPTURE 3  
  33.   
  34. // ZOOM Setting!!  
  35. #define PRV_W           800  
  36. #define PRV_H           600  
  37. #define PRV_ZOFFX       8  
  38. #define PRV_ZOFFY       6  
  39.   
  40. #define CAP_W           1600  
  41. #define CAP_H           1200  
  42. #define CAP_ZOFFX       16  
  43. #define CAP_ZOFFY       12  
  44.   
  45. #define CAM_2XMAX_ZOOM_STEP     25  
  46. #define CAM_CAPCHG_WIDTH        800  
  47.   
  48.   
  49. struct sensor_reg {                     //[suting]: register number and number hold  
  50.     unsigned short reg;  
  51.     unsigned short val;  
  52. };  
  53.   
  54. struct capture_size {                       //[suting] : width and height  
  55.     unsigned long width;  
  56.     unsigned long height;  
  57. };  
  58.   
  59. extern struct capture_size sensor_sizes[];  
  60. extern void sensor_init_fnc(SENSOR_FUNC_TYPE *sensor_func);  
  61.   
  62. #endif /* GT2005_H */  

 

從上面的代碼可以看出,GT2005攝像頭涉及到了I2C總線協議,因爲它在發送命令和接受數據時,都是通過I2C總線的方式來完成的。

上面比較重要的數據結構是:

(1)struct sensor_reg ,它提供的是一個鍵值對:寄存器號 、 寄存器的值,這樣定義的目的是在進行初始化等各種操作時,能夠很方便的由寄存器號獲得寄存器值,進行I2C總線的讀寫。

(2)struct capture_size ,它定義了capture後的image的width、heigth。

(3)extern void sensor_init_fnc(SENSOR_FUNC_TYPE *sensor_func);

這個函數用於註冊GT2005_2mp.c中實際提供給上層應用程序的接口函數。

SENSOR_FUNC_TYPE類型是在 sensor_if.c/.h中定義的。

 

下面是GT2005_2mp.c 實現文件

  1. #include <linux/delay.h>  
  2. #include <asm/system.h>  
  3. #include <mach/hardware.h>  
  4. #include <asm/io.h>  
  5. #include "sensor_if.h"  
  6. #include "cam.h"  
  7. #include "tcc_cam_i2c.h"  
  8.   
  9. #if defined(CONFIG_ARCH_TCC92X) || defined(CONFIG_ARCH_TCC93XX)  
  10. #include <mach/bsp.h>  
  11. #elif defined(CONFIG_ARCH_TCC79X)  
  12. #include <mach/tcc79x.h>  
  13. #endif  
  14.   
  15. //省略了一些註釋掉的代嗎  
  16. //................  
  17.   
  18. /* Array of image sizes supported by GT2005.  These must be ordered from  
  19.  * smallest image size to largest. 
  20.  */  
  21. struct capture_size sensor_sizes[] = {  
  22.     { 1600, 1200 }, /* UXGA */  
  23.     { 1280,  960 }, /* SXGA */  
  24.     { 1024,  768 }, /* XGA */  
  25.     {  800,  600 }, /* SVGA */  
  26.     {  640,  480 }, /* VGA */  
  27.     {  320,  240 }, /* QVGA */  
  28.     {  176,  144 }, /* QCIF */  
  29. };  
  30. /* 上面這個結構主要是用於保存capture size,它每個數據項必須按從小到大來排列,因爲在sensor_if.c的sensor_find_size()函數中,需要使用這個struct的定義,根據app給出的capture size,協商出最合適的size */  
  31.   
  32. /* register initialization tables for sensor */  
  33. /* common sensor register initialization for all image sizes, pixel formats,  
  34.  * and frame rates 
  35.  */  
  36. /* ....省略,後面是一些寄存器的鍵值對定義,主要用於寄存器的初始化,指定對應的image size / pixel format / frmae rate ,這些都可以通過GT2005的數據手冊獲得 */  
  37.   
  38. /* .....省略,接下來的是一些針對camera的不同屬性的寄存器定義,比如:white balance / brightness / special effect等等 */  
  39.   
  40. static int write_regs(const struct sensor_reg reglist[])            //[suting]: write register list  
  41. {  
  42.   
  43.     int err;  
  44.     int err_cnt = 0;      
  45.     unsigned char data[132];  
  46.     unsigned char bytes;  
  47.     const struct sensor_reg *next = reglist;  
  48.       
  49.     while (!((next->reg == REG_TERM) && (next->val == VAL_TERM)))  
  50.     {  
  51.           
  52.         if(next->reg == REG_TERM && next->val != VAL_TERM)  
  53.         {  
  54.             mdelay(next->val*2);  
  55.             printk("Sensor init Delay[%d]!!!! /n", next->val);  
  56.             next++;  
  57.         }  
  58.         else  
  59.         {  
  60.             bytes = 0;  
  61.             data[bytes]= next->reg>>8;     bytes++;          
  62.             data[bytes]= (u8)next->reg&0xff;     bytes++;  
  63.   
  64.             data[bytes]= next->val;      bytes++;  
  65.               
  66.                 err = DDI_I2C_Write(data, 2, bytes-2);              //[suting] : DDI_I2C_Write  
  67.               
  68.             if (err)  
  69.             {  
  70.                 err_cnt++;  
  71.                 if(err_cnt >= 3)  
  72.                 {  
  73.                     printk("ERROR: Sensor I2C !!!! /n");   
  74.                     return err;  
  75.                 }  
  76.             }  
  77.             else  
  78.             {  
  79.                 err_cnt = 0;  
  80.                 next++;  
  81.             }  
  82.         }  
  83.     }  
  84.   
  85.     return 0;  
  86. }  
  87.   
  88. /* 上面這個函數主要用於寫寄存器,它被下面很多的接口函數所調用 */  
  89.   
  90. static int sensor_open(void)  
  91. {  
  92.     int id = 0;  
  93.     int id1 = 0;  
  94.     sensor_power_disable();  
  95.     sensor_delay(10);  
  96.       
  97.     sensor_power_enable();  
  98.     sensor_delay(10);  
  99.   
  100.     sensor_powerdown_disable();  
  101.     sensor_delay(10);  
  102.   
  103.     sensor_powerdown_enable();  
  104.     sensor_delay(10);  
  105.   
  106.     sensor_reset_low();  
  107.     sensor_delay(10);  
  108.   
  109.     CIF_Open();                                             //[suting] : open CIF  
  110.     sensor_delay(40);  
  111.   
  112.     sensor_reset_high();  
  113.     sensor_delay(15);  
  114.   
  115.     printk("init sensor GT2005 !!!! /n");   
  116.     DDI_I2C_Read(0x0000, 2, &id, 1);                          
  117.     DDI_I2C_Read(0x0001, 2, &id1, 1);  
  118.     printk("read sensor ID : %x%x/n", id, id1);   
  119.     return write_regs(sensor_reg_common[0]);                //[suting]: write_regs for initialization  
  120. }  
  121.   
  122. static int sensor_close(void)  
  123. {  
  124.     CIF_ONOFF(OFF);                                         //[suting] : close CIF  
  125.   
  126.     sensor_reset_low();  
  127.     sensor_power_disable();  
  128.     sensor_powerdown_disable();  
  129.   
  130.     CIF_Close();  
  131.     msleep(5);  
  132.   
  133.     return 0;  
  134. }  
  135.   
  136. static int sensor_preview(void)  
  137. {  
  138.     printk("sensor_preview/r/n");  
  139.     return write_regs(sensor_reg_common[1]);                //[suting] : write_regs for preview  
  140. }  
  141.   
  142. static int sensor_capture(void)  
  143. {  
  144.     printk("sensor_capture/r/n");  
  145.     return write_regs(sensor_reg_common[2]);                //[suting] : write_regs for capture  
  146. }  
  147.   
  148. static int sensor_capturecfg(int width, int height)  
  149. {  
  150.     return 0;  
  151. }  
  152.   
  153. /* 上面就是幾個比較有代表性的接口函數 */  
  154.   
  155. void sensor_init_fnc(SENSOR_FUNC_TYPE *sensor_func)         //[suting] just like register callback functions   
  156. {  
  157.     sensor_func->Open        = sensor_open;  
  158.     sensor_func->Close       = sensor_close;  
  159.   
  160.     sensor_func->Set_Preview     = sensor_preview;  
  161.     sensor_func->Set_Capture     = sensor_capture;  
  162.     sensor_func->Set_CaptureCfg = sensor_capturecfg;  
  163.   
  164.     sensor_func->Set_Zoom    = sensor_zoom;  
  165.     sensor_func->Set_AF      = sensor_autofocus;  
  166.     sensor_func->Set_Effect  = sensor_effect;  
  167.     sensor_func->Set_Flip    = sensor_flip;  
  168.     sensor_func->Set_ISO     = sensor_iso;  
  169.     sensor_func->Set_ME      = sensor_me;  
  170.     sensor_func->Set_WB      = sensor_wb;  
  171.     sensor_func->Set_Bright  = sensor_bright;  
  172.     sensor_func->Set_Scene   = sensor_scene;  
  173.   
  174.     sensor_func->Check_ESD   = sensor_check_esd;  
  175.     sensor_func->Check_Luma  = sensor_check_luma;  
  176. }  
  177.   
  178. /* 上面就是註冊所有的接口函數 */ 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章