ov2640.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  1. /*
  2. * ov2640 Camera Driver
  3. *
  4. * Copyright (C) 2010 Alberto Panizzo <maramaopercheseimorto@gmail.com>
  5. *
  6. * Based on ov772x, ov9640 drivers and previous non merged implementations.
  7. *
  8. * Copyright 2005-2009 Freescale Semiconductor, Inc. All Rights Reserved.
  9. * Copyright (C) 2006, OmniVision
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/i2c.h>
  18. #include <linux/slab.h>
  19. #include <linux/delay.h>
  20. #include <linux/videodev2.h>
  21. #include <media/soc_camera.h>
  22. #include <media/soc_mediabus.h>
  23. #include <media/v4l2-chip-ident.h>
  24. #include <media/v4l2-subdev.h>
  25. #define VAL_SET(x, mask, rshift, lshift) \
  26. ((((x) >> rshift) & mask) << lshift)
  27. /*
  28. * DSP registers
  29. * register offset for BANK_SEL == BANK_SEL_DSP
  30. */
  31. #define R_BYPASS 0x05 /* Bypass DSP */
  32. #define R_BYPASS_DSP_BYPAS 0x01 /* Bypass DSP, sensor out directly */
  33. #define R_BYPASS_USE_DSP 0x00 /* Use the internal DSP */
  34. #define QS 0x44 /* Quantization Scale Factor */
  35. #define CTRLI 0x50
  36. #define CTRLI_LP_DP 0x80
  37. #define CTRLI_ROUND 0x40
  38. #define CTRLI_V_DIV_SET(x) VAL_SET(x, 0x3, 0, 3)
  39. #define CTRLI_H_DIV_SET(x) VAL_SET(x, 0x3, 0, 0)
  40. #define HSIZE 0x51 /* H_SIZE[7:0] (real/4) */
  41. #define HSIZE_SET(x) VAL_SET(x, 0xFF, 2, 0)
  42. #define VSIZE 0x52 /* V_SIZE[7:0] (real/4) */
  43. #define VSIZE_SET(x) VAL_SET(x, 0xFF, 2, 0)
  44. #define XOFFL 0x53 /* OFFSET_X[7:0] */
  45. #define XOFFL_SET(x) VAL_SET(x, 0xFF, 0, 0)
  46. #define YOFFL 0x54 /* OFFSET_Y[7:0] */
  47. #define YOFFL_SET(x) VAL_SET(x, 0xFF, 0, 0)
  48. #define VHYX 0x55 /* Offset and size completion */
  49. #define VHYX_VSIZE_SET(x) VAL_SET(x, 0x1, (8+2), 7)
  50. #define VHYX_HSIZE_SET(x) VAL_SET(x, 0x1, (8+2), 3)
  51. #define VHYX_YOFF_SET(x) VAL_SET(x, 0x3, 8, 4)
  52. #define VHYX_XOFF_SET(x) VAL_SET(x, 0x3, 8, 0)
  53. #define DPRP 0x56
  54. #define TEST 0x57 /* Horizontal size completion */
  55. #define TEST_HSIZE_SET(x) VAL_SET(x, 0x1, (9+2), 7)
  56. #define ZMOW 0x5A /* Zoom: Out Width OUTW[7:0] (real/4) */
  57. #define ZMOW_OUTW_SET(x) VAL_SET(x, 0xFF, 2, 0)
  58. #define ZMOH 0x5B /* Zoom: Out Height OUTH[7:0] (real/4) */
  59. #define ZMOH_OUTH_SET(x) VAL_SET(x, 0xFF, 2, 0)
  60. #define ZMHH 0x5C /* Zoom: Speed and H&W completion */
  61. #define ZMHH_ZSPEED_SET(x) VAL_SET(x, 0x0F, 0, 4)
  62. #define ZMHH_OUTH_SET(x) VAL_SET(x, 0x1, (8+2), 2)
  63. #define ZMHH_OUTW_SET(x) VAL_SET(x, 0x3, (8+2), 0)
  64. #define BPADDR 0x7C /* SDE Indirect Register Access: Address */
  65. #define BPDATA 0x7D /* SDE Indirect Register Access: Data */
  66. #define CTRL2 0x86 /* DSP Module enable 2 */
  67. #define CTRL2_DCW_EN 0x20
  68. #define CTRL2_SDE_EN 0x10
  69. #define CTRL2_UV_ADJ_EN 0x08
  70. #define CTRL2_UV_AVG_EN 0x04
  71. #define CTRL2_CMX_EN 0x01
  72. #define CTRL3 0x87 /* DSP Module enable 3 */
  73. #define CTRL3_BPC_EN 0x80
  74. #define CTRL3_WPC_EN 0x40
  75. #define SIZEL 0x8C /* Image Size Completion */
  76. #define SIZEL_HSIZE8_11_SET(x) VAL_SET(x, 0x1, 11, 6)
  77. #define SIZEL_HSIZE8_SET(x) VAL_SET(x, 0x7, 0, 3)
  78. #define SIZEL_VSIZE8_SET(x) VAL_SET(x, 0x7, 0, 0)
  79. #define HSIZE8 0xC0 /* Image Horizontal Size HSIZE[10:3] */
  80. #define HSIZE8_SET(x) VAL_SET(x, 0xFF, 3, 0)
  81. #define VSIZE8 0xC1 /* Image Vertical Size VSIZE[10:3] */
  82. #define VSIZE8_SET(x) VAL_SET(x, 0xFF, 3, 0)
  83. #define CTRL0 0xC2 /* DSP Module enable 0 */
  84. #define CTRL0_AEC_EN 0x80
  85. #define CTRL0_AEC_SEL 0x40
  86. #define CTRL0_STAT_SEL 0x20
  87. #define CTRL0_VFIRST 0x10
  88. #define CTRL0_YUV422 0x08
  89. #define CTRL0_YUV_EN 0x04
  90. #define CTRL0_RGB_EN 0x02
  91. #define CTRL0_RAW_EN 0x01
  92. #define CTRL1 0xC3 /* DSP Module enable 1 */
  93. #define CTRL1_CIP 0x80
  94. #define CTRL1_DMY 0x40
  95. #define CTRL1_RAW_GMA 0x20
  96. #define CTRL1_DG 0x10
  97. #define CTRL1_AWB 0x08
  98. #define CTRL1_AWB_GAIN 0x04
  99. #define CTRL1_LENC 0x02
  100. #define CTRL1_PRE 0x01
  101. #define R_DVP_SP 0xD3 /* DVP output speed control */
  102. #define R_DVP_SP_AUTO_MODE 0x80
  103. #define R_DVP_SP_DVP_MASK 0x3F /* DVP PCLK = sysclk (48)/[6:0] (YUV0);
  104. * = sysclk (48)/(2*[6:0]) (RAW);*/
  105. #define IMAGE_MODE 0xDA /* Image Output Format Select */
  106. #define IMAGE_MODE_Y8_DVP_EN 0x40
  107. #define IMAGE_MODE_JPEG_EN 0x10
  108. #define IMAGE_MODE_YUV422 0x00
  109. #define IMAGE_MODE_RAW10 0x04 /* (DVP) */
  110. #define IMAGE_MODE_RGB565 0x08
  111. #define IMAGE_MODE_HREF_VSYNC 0x02 /* HREF timing select in DVP JPEG output
  112. * mode (0 for HREF is same as sensor) */
  113. #define IMAGE_MODE_LBYTE_FIRST 0x01 /* Byte swap enable for DVP
  114. * 1: Low byte first UYVY (C2[4] =0)
  115. * VYUY (C2[4] =1)
  116. * 0: High byte first YUYV (C2[4]=0)
  117. * YVYU (C2[4] = 1) */
  118. #define RESET 0xE0 /* Reset */
  119. #define RESET_MICROC 0x40
  120. #define RESET_SCCB 0x20
  121. #define RESET_JPEG 0x10
  122. #define RESET_DVP 0x04
  123. #define RESET_IPU 0x02
  124. #define RESET_CIF 0x01
  125. #define REGED 0xED /* Register ED */
  126. #define REGED_CLK_OUT_DIS 0x10
  127. #define MS_SP 0xF0 /* SCCB Master Speed */
  128. #define SS_ID 0xF7 /* SCCB Slave ID */
  129. #define SS_CTRL 0xF8 /* SCCB Slave Control */
  130. #define SS_CTRL_ADD_AUTO_INC 0x20
  131. #define SS_CTRL_EN 0x08
  132. #define SS_CTRL_DELAY_CLK 0x04
  133. #define SS_CTRL_ACC_EN 0x02
  134. #define SS_CTRL_SEN_PASS_THR 0x01
  135. #define MC_BIST 0xF9 /* Microcontroller misc register */
  136. #define MC_BIST_RESET 0x80 /* Microcontroller Reset */
  137. #define MC_BIST_BOOT_ROM_SEL 0x40
  138. #define MC_BIST_12KB_SEL 0x20
  139. #define MC_BIST_12KB_MASK 0x30
  140. #define MC_BIST_512KB_SEL 0x08
  141. #define MC_BIST_512KB_MASK 0x0C
  142. #define MC_BIST_BUSY_BIT_R 0x02
  143. #define MC_BIST_MC_RES_ONE_SH_W 0x02
  144. #define MC_BIST_LAUNCH 0x01
  145. #define BANK_SEL 0xFF /* Register Bank Select */
  146. #define BANK_SEL_DSP 0x00
  147. #define BANK_SEL_SENS 0x01
  148. /*
  149. * Sensor registers
  150. * register offset for BANK_SEL == BANK_SEL_SENS
  151. */
  152. #define GAIN 0x00 /* AGC - Gain control gain setting */
  153. #define COM1 0x03 /* Common control 1 */
  154. #define COM1_1_DUMMY_FR 0x40
  155. #define COM1_3_DUMMY_FR 0x80
  156. #define COM1_7_DUMMY_FR 0xC0
  157. #define COM1_VWIN_LSB_UXGA 0x0F
  158. #define COM1_VWIN_LSB_SVGA 0x0A
  159. #define COM1_VWIN_LSB_CIF 0x06
  160. #define REG04 0x04 /* Register 04 */
  161. #define REG04_DEF 0x20 /* Always set */
  162. #define REG04_HFLIP_IMG 0x80 /* Horizontal mirror image ON/OFF */
  163. #define REG04_VFLIP_IMG 0x40 /* Vertical flip image ON/OFF */
  164. #define REG04_VREF_EN 0x10
  165. #define REG04_HREF_EN 0x08
  166. #define REG04_AEC_SET(x) VAL_SET(x, 0x3, 0, 0)
  167. #define REG08 0x08 /* Frame Exposure One-pin Control Pre-charge Row Num */
  168. #define COM2 0x09 /* Common control 2 */
  169. #define COM2_SOFT_SLEEP_MODE 0x10 /* Soft sleep mode */
  170. /* Output drive capability */
  171. #define COM2_OCAP_Nx_SET(N) (((N) - 1) & 0x03) /* N = [1x .. 4x] */
  172. #define PID 0x0A /* Product ID Number MSB */
  173. #define VER 0x0B /* Product ID Number LSB */
  174. #define COM3 0x0C /* Common control 3 */
  175. #define COM3_BAND_50H 0x04 /* 0 For Banding at 60H */
  176. #define COM3_BAND_AUTO 0x02 /* Auto Banding */
  177. #define COM3_SING_FR_SNAPSH 0x01 /* 0 For enable live video output after the
  178. * snapshot sequence*/
  179. #define AEC 0x10 /* AEC[9:2] Exposure Value */
  180. #define CLKRC 0x11 /* Internal clock */
  181. #define CLKRC_EN 0x80
  182. #define CLKRC_DIV_SET(x) (((x) - 1) & 0x1F) /* CLK = XVCLK/(x) */
  183. #define COM7 0x12 /* Common control 7 */
  184. #define COM7_SRST 0x80 /* Initiates system reset. All registers are
  185. * set to factory default values after which
  186. * the chip resumes normal operation */
  187. #define COM7_RES_UXGA 0x00 /* Resolution selectors for UXGA */
  188. #define COM7_RES_SVGA 0x40 /* SVGA */
  189. #define COM7_RES_CIF 0x20 /* CIF */
  190. #define COM7_ZOOM_EN 0x04 /* Enable Zoom mode */
  191. #define COM7_COLOR_BAR_TEST 0x02 /* Enable Color Bar Test Pattern */
  192. #define COM8 0x13 /* Common control 8 */
  193. #define COM8_DEF 0xC0 /* Banding filter ON/OFF */
  194. #define COM8_BNDF_EN 0x20 /* Banding filter ON/OFF */
  195. #define COM8_AGC_EN 0x04 /* AGC Auto/Manual control selection */
  196. #define COM8_AEC_EN 0x01 /* Auto/Manual Exposure control */
  197. #define COM9 0x14 /* Common control 9
  198. * Automatic gain ceiling - maximum AGC value [7:5]*/
  199. #define COM9_AGC_GAIN_2x 0x00 /* 000 : 2x */
  200. #define COM9_AGC_GAIN_4x 0x20 /* 001 : 4x */
  201. #define COM9_AGC_GAIN_8x 0x40 /* 010 : 8x */
  202. #define COM9_AGC_GAIN_16x 0x60 /* 011 : 16x */
  203. #define COM9_AGC_GAIN_32x 0x80 /* 100 : 32x */
  204. #define COM9_AGC_GAIN_64x 0xA0 /* 101 : 64x */
  205. #define COM9_AGC_GAIN_128x 0xC0 /* 110 : 128x */
  206. #define COM10 0x15 /* Common control 10 */
  207. #define COM10_PCLK_HREF 0x20 /* PCLK output qualified by HREF */
  208. #define COM10_PCLK_RISE 0x10 /* Data is updated at the rising edge of
  209. * PCLK (user can latch data at the next
  210. * falling edge of PCLK).
  211. * 0 otherwise. */
  212. #define COM10_HREF_INV 0x08 /* Invert HREF polarity:
  213. * HREF negative for valid data*/
  214. #define COM10_VSINC_INV 0x02 /* Invert VSYNC polarity */
  215. #define HSTART 0x17 /* Horizontal Window start MSB 8 bit */
  216. #define HEND 0x18 /* Horizontal Window end MSB 8 bit */
  217. #define VSTART 0x19 /* Vertical Window start MSB 8 bit */
  218. #define VEND 0x1A /* Vertical Window end MSB 8 bit */
  219. #define MIDH 0x1C /* Manufacturer ID byte - high */
  220. #define MIDL 0x1D /* Manufacturer ID byte - low */
  221. #define AEW 0x24 /* AGC/AEC - Stable operating region (upper limit) */
  222. #define AEB 0x25 /* AGC/AEC - Stable operating region (lower limit) */
  223. #define VV 0x26 /* AGC/AEC Fast mode operating region */
  224. #define VV_HIGH_TH_SET(x) VAL_SET(x, 0xF, 0, 4)
  225. #define VV_LOW_TH_SET(x) VAL_SET(x, 0xF, 0, 0)
  226. #define REG2A 0x2A /* Dummy pixel insert MSB */
  227. #define FRARL 0x2B /* Dummy pixel insert LSB */
  228. #define ADDVFL 0x2D /* LSB of insert dummy lines in Vertical direction */
  229. #define ADDVFH 0x2E /* MSB of insert dummy lines in Vertical direction */
  230. #define YAVG 0x2F /* Y/G Channel Average value */
  231. #define REG32 0x32 /* Common Control 32 */
  232. #define REG32_PCLK_DIV_2 0x80 /* PCLK freq divided by 2 */
  233. #define REG32_PCLK_DIV_4 0xC0 /* PCLK freq divided by 4 */
  234. #define ARCOM2 0x34 /* Zoom: Horizontal start point */
  235. #define REG45 0x45 /* Register 45 */
  236. #define FLL 0x46 /* Frame Length Adjustment LSBs */
  237. #define FLH 0x47 /* Frame Length Adjustment MSBs */
  238. #define COM19 0x48 /* Zoom: Vertical start point */
  239. #define ZOOMS 0x49 /* Zoom: Vertical start point */
  240. #define COM22 0x4B /* Flash light control */
  241. #define COM25 0x4E /* For Banding operations */
  242. #define BD50 0x4F /* 50Hz Banding AEC 8 LSBs */
  243. #define BD60 0x50 /* 60Hz Banding AEC 8 LSBs */
  244. #define REG5D 0x5D /* AVGsel[7:0], 16-zone average weight option */
  245. #define REG5E 0x5E /* AVGsel[15:8], 16-zone average weight option */
  246. #define REG5F 0x5F /* AVGsel[23:16], 16-zone average weight option */
  247. #define REG60 0x60 /* AVGsel[31:24], 16-zone average weight option */
  248. #define HISTO_LOW 0x61 /* Histogram Algorithm Low Level */
  249. #define HISTO_HIGH 0x62 /* Histogram Algorithm High Level */
  250. /*
  251. * ID
  252. */
  253. #define MANUFACTURER_ID 0x7FA2
  254. #define PID_OV2640 0x2642
  255. #define VERSION(pid, ver) ((pid << 8) | (ver & 0xFF))
  256. /*
  257. * Struct
  258. */
  259. struct regval_list {
  260. u8 reg_num;
  261. u8 value;
  262. };
  263. /* Supported resolutions */
  264. enum ov2640_width {
  265. W_QCIF = 176,
  266. W_QVGA = 320,
  267. W_CIF = 352,
  268. W_VGA = 640,
  269. W_SVGA = 800,
  270. W_XGA = 1024,
  271. W_SXGA = 1280,
  272. W_UXGA = 1600,
  273. };
  274. enum ov2640_height {
  275. H_QCIF = 144,
  276. H_QVGA = 240,
  277. H_CIF = 288,
  278. H_VGA = 480,
  279. H_SVGA = 600,
  280. H_XGA = 768,
  281. H_SXGA = 1024,
  282. H_UXGA = 1200,
  283. };
  284. struct ov2640_win_size {
  285. char *name;
  286. enum ov2640_width width;
  287. enum ov2640_height height;
  288. const struct regval_list *regs;
  289. };
  290. struct ov2640_priv {
  291. struct v4l2_subdev subdev;
  292. enum v4l2_mbus_pixelcode cfmt_code;
  293. const struct ov2640_win_size *win;
  294. int model;
  295. u16 flag_vflip:1;
  296. u16 flag_hflip:1;
  297. };
  298. /*
  299. * Registers settings
  300. */
  301. #define ENDMARKER { 0xff, 0xff }
  302. static const struct regval_list ov2640_init_regs[] = {
  303. { BANK_SEL, BANK_SEL_DSP },
  304. { 0x2c, 0xff },
  305. { 0x2e, 0xdf },
  306. { BANK_SEL, BANK_SEL_SENS },
  307. { 0x3c, 0x32 },
  308. { CLKRC, CLKRC_DIV_SET(1) },
  309. { COM2, COM2_OCAP_Nx_SET(3) },
  310. { REG04, REG04_DEF | REG04_HREF_EN },
  311. { COM8, COM8_DEF | COM8_BNDF_EN | COM8_AGC_EN | COM8_AEC_EN },
  312. { COM9, COM9_AGC_GAIN_8x | 0x08},
  313. { 0x2c, 0x0c },
  314. { 0x33, 0x78 },
  315. { 0x3a, 0x33 },
  316. { 0x3b, 0xfb },
  317. { 0x3e, 0x00 },
  318. { 0x43, 0x11 },
  319. { 0x16, 0x10 },
  320. { 0x39, 0x02 },
  321. { 0x35, 0x88 },
  322. { 0x22, 0x0a },
  323. { 0x37, 0x40 },
  324. { 0x23, 0x00 },
  325. { ARCOM2, 0xa0 },
  326. { 0x06, 0x02 },
  327. { 0x06, 0x88 },
  328. { 0x07, 0xc0 },
  329. { 0x0d, 0xb7 },
  330. { 0x0e, 0x01 },
  331. { 0x4c, 0x00 },
  332. { 0x4a, 0x81 },
  333. { 0x21, 0x99 },
  334. { AEW, 0x40 },
  335. { AEB, 0x38 },
  336. { VV, VV_HIGH_TH_SET(0x08) | VV_LOW_TH_SET(0x02) },
  337. { 0x5c, 0x00 },
  338. { 0x63, 0x00 },
  339. { FLL, 0x22 },
  340. { COM3, 0x38 | COM3_BAND_AUTO },
  341. { REG5D, 0x55 },
  342. { REG5E, 0x7d },
  343. { REG5F, 0x7d },
  344. { REG60, 0x55 },
  345. { HISTO_LOW, 0x70 },
  346. { HISTO_HIGH, 0x80 },
  347. { 0x7c, 0x05 },
  348. { 0x20, 0x80 },
  349. { 0x28, 0x30 },
  350. { 0x6c, 0x00 },
  351. { 0x6d, 0x80 },
  352. { 0x6e, 0x00 },
  353. { 0x70, 0x02 },
  354. { 0x71, 0x94 },
  355. { 0x73, 0xc1 },
  356. { 0x3d, 0x34 },
  357. { COM7, COM7_RES_UXGA | COM7_ZOOM_EN },
  358. { 0x5a, 0x57 },
  359. { BD50, 0xbb },
  360. { BD60, 0x9c },
  361. { BANK_SEL, BANK_SEL_DSP },
  362. { 0xe5, 0x7f },
  363. { MC_BIST, MC_BIST_RESET | MC_BIST_BOOT_ROM_SEL },
  364. { 0x41, 0x24 },
  365. { RESET, RESET_JPEG | RESET_DVP },
  366. { 0x76, 0xff },
  367. { 0x33, 0xa0 },
  368. { 0x42, 0x20 },
  369. { 0x43, 0x18 },
  370. { 0x4c, 0x00 },
  371. { CTRL3, CTRL3_BPC_EN | CTRL3_WPC_EN | 0x10 },
  372. { 0x88, 0x3f },
  373. { 0xd7, 0x03 },
  374. { 0xd9, 0x10 },
  375. { R_DVP_SP , R_DVP_SP_AUTO_MODE | 0x2 },
  376. { 0xc8, 0x08 },
  377. { 0xc9, 0x80 },
  378. { BPADDR, 0x00 },
  379. { BPDATA, 0x00 },
  380. { BPADDR, 0x03 },
  381. { BPDATA, 0x48 },
  382. { BPDATA, 0x48 },
  383. { BPADDR, 0x08 },
  384. { BPDATA, 0x20 },
  385. { BPDATA, 0x10 },
  386. { BPDATA, 0x0e },
  387. { 0x90, 0x00 },
  388. { 0x91, 0x0e },
  389. { 0x91, 0x1a },
  390. { 0x91, 0x31 },
  391. { 0x91, 0x5a },
  392. { 0x91, 0x69 },
  393. { 0x91, 0x75 },
  394. { 0x91, 0x7e },
  395. { 0x91, 0x88 },
  396. { 0x91, 0x8f },
  397. { 0x91, 0x96 },
  398. { 0x91, 0xa3 },
  399. { 0x91, 0xaf },
  400. { 0x91, 0xc4 },
  401. { 0x91, 0xd7 },
  402. { 0x91, 0xe8 },
  403. { 0x91, 0x20 },
  404. { 0x92, 0x00 },
  405. { 0x93, 0x06 },
  406. { 0x93, 0xe3 },
  407. { 0x93, 0x03 },
  408. { 0x93, 0x03 },
  409. { 0x93, 0x00 },
  410. { 0x93, 0x02 },
  411. { 0x93, 0x00 },
  412. { 0x93, 0x00 },
  413. { 0x93, 0x00 },
  414. { 0x93, 0x00 },
  415. { 0x93, 0x00 },
  416. { 0x93, 0x00 },
  417. { 0x93, 0x00 },
  418. { 0x96, 0x00 },
  419. { 0x97, 0x08 },
  420. { 0x97, 0x19 },
  421. { 0x97, 0x02 },
  422. { 0x97, 0x0c },
  423. { 0x97, 0x24 },
  424. { 0x97, 0x30 },
  425. { 0x97, 0x28 },
  426. { 0x97, 0x26 },
  427. { 0x97, 0x02 },
  428. { 0x97, 0x98 },
  429. { 0x97, 0x80 },
  430. { 0x97, 0x00 },
  431. { 0x97, 0x00 },
  432. { 0xa4, 0x00 },
  433. { 0xa8, 0x00 },
  434. { 0xc5, 0x11 },
  435. { 0xc6, 0x51 },
  436. { 0xbf, 0x80 },
  437. { 0xc7, 0x10 },
  438. { 0xb6, 0x66 },
  439. { 0xb8, 0xA5 },
  440. { 0xb7, 0x64 },
  441. { 0xb9, 0x7C },
  442. { 0xb3, 0xaf },
  443. { 0xb4, 0x97 },
  444. { 0xb5, 0xFF },
  445. { 0xb0, 0xC5 },
  446. { 0xb1, 0x94 },
  447. { 0xb2, 0x0f },
  448. { 0xc4, 0x5c },
  449. { 0xa6, 0x00 },
  450. { 0xa7, 0x20 },
  451. { 0xa7, 0xd8 },
  452. { 0xa7, 0x1b },
  453. { 0xa7, 0x31 },
  454. { 0xa7, 0x00 },
  455. { 0xa7, 0x18 },
  456. { 0xa7, 0x20 },
  457. { 0xa7, 0xd8 },
  458. { 0xa7, 0x19 },
  459. { 0xa7, 0x31 },
  460. { 0xa7, 0x00 },
  461. { 0xa7, 0x18 },
  462. { 0xa7, 0x20 },
  463. { 0xa7, 0xd8 },
  464. { 0xa7, 0x19 },
  465. { 0xa7, 0x31 },
  466. { 0xa7, 0x00 },
  467. { 0xa7, 0x18 },
  468. { 0x7f, 0x00 },
  469. { 0xe5, 0x1f },
  470. { 0xe1, 0x77 },
  471. { 0xdd, 0x7f },
  472. { CTRL0, CTRL0_YUV422 | CTRL0_YUV_EN | CTRL0_RGB_EN },
  473. ENDMARKER,
  474. };
  475. /*
  476. * Register settings for window size
  477. * The preamble, setup the internal DSP to input an UXGA (1600x1200) image.
  478. * Then the different zooming configurations will setup the output image size.
  479. */
  480. static const struct regval_list ov2640_size_change_preamble_regs[] = {
  481. { BANK_SEL, BANK_SEL_DSP },
  482. { RESET, RESET_DVP },
  483. { HSIZE8, HSIZE8_SET(W_UXGA) },
  484. { VSIZE8, VSIZE8_SET(H_UXGA) },
  485. { CTRL2, CTRL2_DCW_EN | CTRL2_SDE_EN |
  486. CTRL2_UV_AVG_EN | CTRL2_CMX_EN | CTRL2_UV_ADJ_EN },
  487. { HSIZE, HSIZE_SET(W_UXGA) },
  488. { VSIZE, VSIZE_SET(H_UXGA) },
  489. { XOFFL, XOFFL_SET(0) },
  490. { YOFFL, YOFFL_SET(0) },
  491. { VHYX, VHYX_HSIZE_SET(W_UXGA) | VHYX_VSIZE_SET(H_UXGA) |
  492. VHYX_XOFF_SET(0) | VHYX_YOFF_SET(0)},
  493. { TEST, TEST_HSIZE_SET(W_UXGA) },
  494. ENDMARKER,
  495. };
  496. #define PER_SIZE_REG_SEQ(x, y, v_div, h_div, pclk_div) \
  497. { CTRLI, CTRLI_LP_DP | CTRLI_V_DIV_SET(v_div) | \
  498. CTRLI_H_DIV_SET(h_div)}, \
  499. { ZMOW, ZMOW_OUTW_SET(x) }, \
  500. { ZMOH, ZMOH_OUTH_SET(y) }, \
  501. { ZMHH, ZMHH_OUTW_SET(x) | ZMHH_OUTH_SET(y) }, \
  502. { R_DVP_SP, pclk_div }, \
  503. { RESET, 0x00}
  504. static const struct regval_list ov2640_qcif_regs[] = {
  505. PER_SIZE_REG_SEQ(W_QCIF, H_QCIF, 3, 3, 4),
  506. ENDMARKER,
  507. };
  508. static const struct regval_list ov2640_qvga_regs[] = {
  509. PER_SIZE_REG_SEQ(W_QVGA, H_QVGA, 2, 2, 4),
  510. ENDMARKER,
  511. };
  512. static const struct regval_list ov2640_cif_regs[] = {
  513. PER_SIZE_REG_SEQ(W_CIF, H_CIF, 2, 2, 8),
  514. ENDMARKER,
  515. };
  516. static const struct regval_list ov2640_vga_regs[] = {
  517. PER_SIZE_REG_SEQ(W_VGA, H_VGA, 0, 0, 2),
  518. ENDMARKER,
  519. };
  520. static const struct regval_list ov2640_svga_regs[] = {
  521. PER_SIZE_REG_SEQ(W_SVGA, H_SVGA, 1, 1, 2),
  522. ENDMARKER,
  523. };
  524. static const struct regval_list ov2640_xga_regs[] = {
  525. PER_SIZE_REG_SEQ(W_XGA, H_XGA, 0, 0, 2),
  526. { CTRLI, 0x00},
  527. ENDMARKER,
  528. };
  529. static const struct regval_list ov2640_sxga_regs[] = {
  530. PER_SIZE_REG_SEQ(W_SXGA, H_SXGA, 0, 0, 2),
  531. { CTRLI, 0x00},
  532. { R_DVP_SP, 2 | R_DVP_SP_AUTO_MODE },
  533. ENDMARKER,
  534. };
  535. static const struct regval_list ov2640_uxga_regs[] = {
  536. PER_SIZE_REG_SEQ(W_UXGA, H_UXGA, 0, 0, 0),
  537. { CTRLI, 0x00},
  538. { R_DVP_SP, 0 | R_DVP_SP_AUTO_MODE },
  539. ENDMARKER,
  540. };
  541. #define OV2640_SIZE(n, w, h, r) \
  542. {.name = n, .width = w , .height = h, .regs = r }
  543. static const struct ov2640_win_size ov2640_supported_win_sizes[] = {
  544. OV2640_SIZE("QCIF", W_QCIF, H_QCIF, ov2640_qcif_regs),
  545. OV2640_SIZE("QVGA", W_QVGA, H_QVGA, ov2640_qvga_regs),
  546. OV2640_SIZE("CIF", W_CIF, H_CIF, ov2640_cif_regs),
  547. OV2640_SIZE("VGA", W_VGA, H_VGA, ov2640_vga_regs),
  548. OV2640_SIZE("SVGA", W_SVGA, H_SVGA, ov2640_svga_regs),
  549. OV2640_SIZE("XGA", W_XGA, H_XGA, ov2640_xga_regs),
  550. OV2640_SIZE("SXGA", W_SXGA, H_SXGA, ov2640_sxga_regs),
  551. OV2640_SIZE("UXGA", W_UXGA, H_UXGA, ov2640_uxga_regs),
  552. };
  553. /*
  554. * Register settings for pixel formats
  555. */
  556. static const struct regval_list ov2640_format_change_preamble_regs[] = {
  557. { BANK_SEL, BANK_SEL_DSP },
  558. { R_BYPASS, R_BYPASS_USE_DSP },
  559. ENDMARKER,
  560. };
  561. static const struct regval_list ov2640_yuv422_regs[] = {
  562. { IMAGE_MODE, IMAGE_MODE_LBYTE_FIRST | IMAGE_MODE_YUV422 },
  563. { 0xD7, 0x01 },
  564. { 0x33, 0xa0 },
  565. { 0xe1, 0x67 },
  566. { RESET, 0x00 },
  567. { R_BYPASS, R_BYPASS_USE_DSP },
  568. ENDMARKER,
  569. };
  570. static const struct regval_list ov2640_rgb565_regs[] = {
  571. { IMAGE_MODE, IMAGE_MODE_LBYTE_FIRST | IMAGE_MODE_RGB565 },
  572. { 0xd7, 0x03 },
  573. { RESET, 0x00 },
  574. { R_BYPASS, R_BYPASS_USE_DSP },
  575. ENDMARKER,
  576. };
  577. static enum v4l2_mbus_pixelcode ov2640_codes[] = {
  578. V4L2_MBUS_FMT_UYVY8_2X8,
  579. V4L2_MBUS_FMT_RGB565_2X8_LE,
  580. };
  581. /*
  582. * Supported controls
  583. */
  584. static const struct v4l2_queryctrl ov2640_controls[] = {
  585. {
  586. .id = V4L2_CID_VFLIP,
  587. .type = V4L2_CTRL_TYPE_BOOLEAN,
  588. .name = "Flip Vertically",
  589. .minimum = 0,
  590. .maximum = 1,
  591. .step = 1,
  592. .default_value = 0,
  593. }, {
  594. .id = V4L2_CID_HFLIP,
  595. .type = V4L2_CTRL_TYPE_BOOLEAN,
  596. .name = "Flip Horizontally",
  597. .minimum = 0,
  598. .maximum = 1,
  599. .step = 1,
  600. .default_value = 0,
  601. },
  602. };
  603. /*
  604. * General functions
  605. */
  606. static struct ov2640_priv *to_ov2640(const struct i2c_client *client)
  607. {
  608. return container_of(i2c_get_clientdata(client), struct ov2640_priv,
  609. subdev);
  610. }
  611. static int ov2640_write_array(struct i2c_client *client,
  612. const struct regval_list *vals)
  613. {
  614. int ret;
  615. while ((vals->reg_num != 0xff) || (vals->value != 0xff)) {
  616. ret = i2c_smbus_write_byte_data(client,
  617. vals->reg_num, vals->value);
  618. dev_vdbg(&client->dev, "array: 0x%02x, 0x%02x",
  619. vals->reg_num, vals->value);
  620. if (ret < 0)
  621. return ret;
  622. vals++;
  623. }
  624. return 0;
  625. }
  626. static int ov2640_mask_set(struct i2c_client *client,
  627. u8 reg, u8 mask, u8 set)
  628. {
  629. s32 val = i2c_smbus_read_byte_data(client, reg);
  630. if (val < 0)
  631. return val;
  632. val &= ~mask;
  633. val |= set & mask;
  634. dev_vdbg(&client->dev, "masks: 0x%02x, 0x%02x", reg, val);
  635. return i2c_smbus_write_byte_data(client, reg, val);
  636. }
  637. static int ov2640_reset(struct i2c_client *client)
  638. {
  639. int ret;
  640. const struct regval_list reset_seq[] = {
  641. {BANK_SEL, BANK_SEL_SENS},
  642. {COM7, COM7_SRST},
  643. ENDMARKER,
  644. };
  645. ret = ov2640_write_array(client, reset_seq);
  646. if (ret)
  647. goto err;
  648. msleep(5);
  649. err:
  650. dev_dbg(&client->dev, "%s: (ret %d)", __func__, ret);
  651. return ret;
  652. }
  653. /*
  654. * soc_camera_ops functions
  655. */
  656. static int ov2640_s_stream(struct v4l2_subdev *sd, int enable)
  657. {
  658. return 0;
  659. }
  660. static int ov2640_set_bus_param(struct soc_camera_device *icd,
  661. unsigned long flags)
  662. {
  663. struct soc_camera_link *icl = to_soc_camera_link(icd);
  664. unsigned long width_flag = flags & SOCAM_DATAWIDTH_MASK;
  665. /* Only one width bit may be set */
  666. if (!is_power_of_2(width_flag))
  667. return -EINVAL;
  668. if (icl->set_bus_param)
  669. return icl->set_bus_param(icl, width_flag);
  670. /*
  671. * Without board specific bus width settings we support only the
  672. * sensors native bus width witch are tested working
  673. */
  674. if (width_flag & (SOCAM_DATAWIDTH_10 | SOCAM_DATAWIDTH_8))
  675. return 0;
  676. return 0;
  677. }
  678. static unsigned long ov2640_query_bus_param(struct soc_camera_device *icd)
  679. {
  680. struct soc_camera_link *icl = to_soc_camera_link(icd);
  681. unsigned long flags = SOCAM_PCLK_SAMPLE_RISING | SOCAM_MASTER |
  682. SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_HIGH |
  683. SOCAM_DATA_ACTIVE_HIGH;
  684. if (icl->query_bus_param)
  685. flags |= icl->query_bus_param(icl) & SOCAM_DATAWIDTH_MASK;
  686. else
  687. flags |= SOCAM_DATAWIDTH_10;
  688. return soc_camera_apply_sensor_flags(icl, flags);
  689. }
  690. static int ov2640_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  691. {
  692. struct i2c_client *client = v4l2_get_subdevdata(sd);
  693. struct ov2640_priv *priv = to_ov2640(client);
  694. switch (ctrl->id) {
  695. case V4L2_CID_VFLIP:
  696. ctrl->value = priv->flag_vflip;
  697. break;
  698. case V4L2_CID_HFLIP:
  699. ctrl->value = priv->flag_hflip;
  700. break;
  701. }
  702. return 0;
  703. }
  704. static int ov2640_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  705. {
  706. struct i2c_client *client = v4l2_get_subdevdata(sd);
  707. struct ov2640_priv *priv = to_ov2640(client);
  708. int ret = 0;
  709. u8 val;
  710. switch (ctrl->id) {
  711. case V4L2_CID_VFLIP:
  712. val = ctrl->value ? REG04_VFLIP_IMG : 0x00;
  713. priv->flag_vflip = ctrl->value ? 1 : 0;
  714. ret = ov2640_mask_set(client, REG04, REG04_VFLIP_IMG, val);
  715. break;
  716. case V4L2_CID_HFLIP:
  717. val = ctrl->value ? REG04_HFLIP_IMG : 0x00;
  718. priv->flag_hflip = ctrl->value ? 1 : 0;
  719. ret = ov2640_mask_set(client, REG04, REG04_HFLIP_IMG, val);
  720. break;
  721. }
  722. return ret;
  723. }
  724. static int ov2640_g_chip_ident(struct v4l2_subdev *sd,
  725. struct v4l2_dbg_chip_ident *id)
  726. {
  727. struct i2c_client *client = v4l2_get_subdevdata(sd);
  728. struct ov2640_priv *priv = to_ov2640(client);
  729. id->ident = priv->model;
  730. id->revision = 0;
  731. return 0;
  732. }
  733. #ifdef CONFIG_VIDEO_ADV_DEBUG
  734. static int ov2640_g_register(struct v4l2_subdev *sd,
  735. struct v4l2_dbg_register *reg)
  736. {
  737. struct i2c_client *client = v4l2_get_subdevdata(sd);
  738. int ret;
  739. reg->size = 1;
  740. if (reg->reg > 0xff)
  741. return -EINVAL;
  742. ret = i2c_smbus_read_byte_data(client, reg->reg);
  743. if (ret < 0)
  744. return ret;
  745. reg->val = ret;
  746. return 0;
  747. }
  748. static int ov2640_s_register(struct v4l2_subdev *sd,
  749. struct v4l2_dbg_register *reg)
  750. {
  751. struct i2c_client *client = v4l2_get_subdevdata(sd);
  752. if (reg->reg > 0xff ||
  753. reg->val > 0xff)
  754. return -EINVAL;
  755. return i2c_smbus_write_byte_data(client, reg->reg, reg->val);
  756. }
  757. #endif
  758. /* Select the nearest higher resolution for capture */
  759. static const struct ov2640_win_size *ov2640_select_win(u32 *width, u32 *height)
  760. {
  761. int i, default_size = ARRAY_SIZE(ov2640_supported_win_sizes) - 1;
  762. for (i = 0; i < ARRAY_SIZE(ov2640_supported_win_sizes); i++) {
  763. if (ov2640_supported_win_sizes[i].width >= *width &&
  764. ov2640_supported_win_sizes[i].height >= *height) {
  765. *width = ov2640_supported_win_sizes[i].width;
  766. *height = ov2640_supported_win_sizes[i].height;
  767. return &ov2640_supported_win_sizes[i];
  768. }
  769. }
  770. *width = ov2640_supported_win_sizes[default_size].width;
  771. *height = ov2640_supported_win_sizes[default_size].height;
  772. return &ov2640_supported_win_sizes[default_size];
  773. }
  774. static int ov2640_set_params(struct i2c_client *client, u32 *width, u32 *height,
  775. enum v4l2_mbus_pixelcode code)
  776. {
  777. struct ov2640_priv *priv = to_ov2640(client);
  778. const struct regval_list *selected_cfmt_regs;
  779. int ret;
  780. /* select win */
  781. priv->win = ov2640_select_win(width, height);
  782. /* select format */
  783. priv->cfmt_code = 0;
  784. switch (code) {
  785. case V4L2_MBUS_FMT_RGB565_2X8_LE:
  786. dev_dbg(&client->dev, "%s: Selected cfmt RGB565", __func__);
  787. selected_cfmt_regs = ov2640_rgb565_regs;
  788. break;
  789. default:
  790. case V4L2_MBUS_FMT_UYVY8_2X8:
  791. dev_dbg(&client->dev, "%s: Selected cfmt YUV422", __func__);
  792. selected_cfmt_regs = ov2640_yuv422_regs;
  793. }
  794. /* reset hardware */
  795. ov2640_reset(client);
  796. /* initialize the sensor with default data */
  797. dev_dbg(&client->dev, "%s: Init default", __func__);
  798. ret = ov2640_write_array(client, ov2640_init_regs);
  799. if (ret < 0)
  800. goto err;
  801. /* select preamble */
  802. dev_dbg(&client->dev, "%s: Set size to %s", __func__, priv->win->name);
  803. ret = ov2640_write_array(client, ov2640_size_change_preamble_regs);
  804. if (ret < 0)
  805. goto err;
  806. /* set size win */
  807. ret = ov2640_write_array(client, priv->win->regs);
  808. if (ret < 0)
  809. goto err;
  810. /* cfmt preamble */
  811. dev_dbg(&client->dev, "%s: Set cfmt", __func__);
  812. ret = ov2640_write_array(client, ov2640_format_change_preamble_regs);
  813. if (ret < 0)
  814. goto err;
  815. /* set cfmt */
  816. ret = ov2640_write_array(client, selected_cfmt_regs);
  817. if (ret < 0)
  818. goto err;
  819. priv->cfmt_code = code;
  820. *width = priv->win->width;
  821. *height = priv->win->height;
  822. return 0;
  823. err:
  824. dev_err(&client->dev, "%s: Error %d", __func__, ret);
  825. ov2640_reset(client);
  826. priv->win = NULL;
  827. return ret;
  828. }
  829. static int ov2640_g_fmt(struct v4l2_subdev *sd,
  830. struct v4l2_mbus_framefmt *mf)
  831. {
  832. struct i2c_client *client = v4l2_get_subdevdata(sd);
  833. struct ov2640_priv *priv = to_ov2640(client);
  834. if (!priv->win) {
  835. u32 width = W_SVGA, height = H_SVGA;
  836. int ret = ov2640_set_params(client, &width, &height,
  837. V4L2_MBUS_FMT_UYVY8_2X8);
  838. if (ret < 0)
  839. return ret;
  840. }
  841. mf->width = priv->win->width;
  842. mf->height = priv->win->height;
  843. mf->code = priv->cfmt_code;
  844. switch (mf->code) {
  845. case V4L2_MBUS_FMT_RGB565_2X8_LE:
  846. mf->colorspace = V4L2_COLORSPACE_SRGB;
  847. break;
  848. default:
  849. case V4L2_MBUS_FMT_UYVY8_2X8:
  850. mf->colorspace = V4L2_COLORSPACE_JPEG;
  851. }
  852. mf->field = V4L2_FIELD_NONE;
  853. return 0;
  854. }
  855. static int ov2640_s_fmt(struct v4l2_subdev *sd,
  856. struct v4l2_mbus_framefmt *mf)
  857. {
  858. struct i2c_client *client = v4l2_get_subdevdata(sd);
  859. int ret;
  860. switch (mf->code) {
  861. case V4L2_MBUS_FMT_RGB565_2X8_LE:
  862. mf->colorspace = V4L2_COLORSPACE_SRGB;
  863. break;
  864. default:
  865. mf->code = V4L2_MBUS_FMT_UYVY8_2X8;
  866. case V4L2_MBUS_FMT_UYVY8_2X8:
  867. mf->colorspace = V4L2_COLORSPACE_JPEG;
  868. }
  869. ret = ov2640_set_params(client, &mf->width, &mf->height, mf->code);
  870. return ret;
  871. }
  872. static int ov2640_try_fmt(struct v4l2_subdev *sd,
  873. struct v4l2_mbus_framefmt *mf)
  874. {
  875. const struct ov2640_win_size *win;
  876. /*
  877. * select suitable win
  878. */
  879. win = ov2640_select_win(&mf->width, &mf->height);
  880. mf->field = V4L2_FIELD_NONE;
  881. switch (mf->code) {
  882. case V4L2_MBUS_FMT_RGB565_2X8_LE:
  883. mf->colorspace = V4L2_COLORSPACE_SRGB;
  884. break;
  885. default:
  886. mf->code = V4L2_MBUS_FMT_UYVY8_2X8;
  887. case V4L2_MBUS_FMT_UYVY8_2X8:
  888. mf->colorspace = V4L2_COLORSPACE_JPEG;
  889. }
  890. return 0;
  891. }
  892. static int ov2640_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
  893. enum v4l2_mbus_pixelcode *code)
  894. {
  895. if (index >= ARRAY_SIZE(ov2640_codes))
  896. return -EINVAL;
  897. *code = ov2640_codes[index];
  898. return 0;
  899. }
  900. static int ov2640_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  901. {
  902. a->c.left = 0;
  903. a->c.top = 0;
  904. a->c.width = W_UXGA;
  905. a->c.height = H_UXGA;
  906. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  907. return 0;
  908. }
  909. static int ov2640_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
  910. {
  911. a->bounds.left = 0;
  912. a->bounds.top = 0;
  913. a->bounds.width = W_UXGA;
  914. a->bounds.height = H_UXGA;
  915. a->defrect = a->bounds;
  916. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  917. a->pixelaspect.numerator = 1;
  918. a->pixelaspect.denominator = 1;
  919. return 0;
  920. }
  921. static int ov2640_video_probe(struct soc_camera_device *icd,
  922. struct i2c_client *client)
  923. {
  924. struct ov2640_priv *priv = to_ov2640(client);
  925. u8 pid, ver, midh, midl;
  926. const char *devname;
  927. int ret;
  928. /* We must have a parent by now. And it cannot be a wrong one. */
  929. BUG_ON(!icd->parent ||
  930. to_soc_camera_host(icd->parent)->nr != icd->iface);
  931. /*
  932. * check and show product ID and manufacturer ID
  933. */
  934. i2c_smbus_write_byte_data(client, BANK_SEL, BANK_SEL_SENS);
  935. pid = i2c_smbus_read_byte_data(client, PID);
  936. ver = i2c_smbus_read_byte_data(client, VER);
  937. midh = i2c_smbus_read_byte_data(client, MIDH);
  938. midl = i2c_smbus_read_byte_data(client, MIDL);
  939. switch (VERSION(pid, ver)) {
  940. case PID_OV2640:
  941. devname = "ov2640";
  942. priv->model = V4L2_IDENT_OV2640;
  943. break;
  944. default:
  945. dev_err(&client->dev,
  946. "Product ID error %x:%x\n", pid, ver);
  947. ret = -ENODEV;
  948. goto err;
  949. }
  950. dev_info(&client->dev,
  951. "%s Product ID %0x:%0x Manufacturer ID %x:%x\n",
  952. devname, pid, ver, midh, midl);
  953. return 0;
  954. err:
  955. return ret;
  956. }
  957. static struct soc_camera_ops ov2640_ops = {
  958. .set_bus_param = ov2640_set_bus_param,
  959. .query_bus_param = ov2640_query_bus_param,
  960. .controls = ov2640_controls,
  961. .num_controls = ARRAY_SIZE(ov2640_controls),
  962. };
  963. static struct v4l2_subdev_core_ops ov2640_subdev_core_ops = {
  964. .g_ctrl = ov2640_g_ctrl,
  965. .s_ctrl = ov2640_s_ctrl,
  966. .g_chip_ident = ov2640_g_chip_ident,
  967. #ifdef CONFIG_VIDEO_ADV_DEBUG
  968. .g_register = ov2640_g_register,
  969. .s_register = ov2640_s_register,
  970. #endif
  971. };
  972. static int ov2640_g_mbus_config(struct v4l2_subdev *sd,
  973. struct v4l2_mbus_config *cfg)
  974. {
  975. struct i2c_client *client = v4l2_get_subdevdata(sd);
  976. struct soc_camera_device *icd = client->dev.platform_data;
  977. struct soc_camera_link *icl = to_soc_camera_link(icd);
  978. cfg->flags = V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_MASTER |
  979. V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_HIGH |
  980. V4L2_MBUS_DATA_ACTIVE_HIGH;
  981. cfg->type = V4L2_MBUS_PARALLEL;
  982. cfg->flags = soc_camera_apply_board_flags(icl, cfg);
  983. return 0;
  984. }
  985. static struct v4l2_subdev_video_ops ov2640_subdev_video_ops = {
  986. .s_stream = ov2640_s_stream,
  987. .g_mbus_fmt = ov2640_g_fmt,
  988. .s_mbus_fmt = ov2640_s_fmt,
  989. .try_mbus_fmt = ov2640_try_fmt,
  990. .cropcap = ov2640_cropcap,
  991. .g_crop = ov2640_g_crop,
  992. .enum_mbus_fmt = ov2640_enum_fmt,
  993. .g_mbus_config = ov2640_g_mbus_config,
  994. };
  995. static struct v4l2_subdev_ops ov2640_subdev_ops = {
  996. .core = &ov2640_subdev_core_ops,
  997. .video = &ov2640_subdev_video_ops,
  998. };
  999. /*
  1000. * i2c_driver functions
  1001. */
  1002. static int ov2640_probe(struct i2c_client *client,
  1003. const struct i2c_device_id *did)
  1004. {
  1005. struct ov2640_priv *priv;
  1006. struct soc_camera_device *icd = client->dev.platform_data;
  1007. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  1008. struct soc_camera_link *icl;
  1009. int ret;
  1010. if (!icd) {
  1011. dev_err(&adapter->dev, "OV2640: missing soc-camera data!\n");
  1012. return -EINVAL;
  1013. }
  1014. icl = to_soc_camera_link(icd);
  1015. if (!icl) {
  1016. dev_err(&adapter->dev,
  1017. "OV2640: Missing platform_data for driver\n");
  1018. return -EINVAL;
  1019. }
  1020. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  1021. dev_err(&adapter->dev,
  1022. "OV2640: I2C-Adapter doesn't support SMBUS\n");
  1023. return -EIO;
  1024. }
  1025. priv = kzalloc(sizeof(struct ov2640_priv), GFP_KERNEL);
  1026. if (!priv) {
  1027. dev_err(&adapter->dev,
  1028. "Failed to allocate memory for private data!\n");
  1029. return -ENOMEM;
  1030. }
  1031. v4l2_i2c_subdev_init(&priv->subdev, client, &ov2640_subdev_ops);
  1032. icd->ops = &ov2640_ops;
  1033. ret = ov2640_video_probe(icd, client);
  1034. if (ret) {
  1035. icd->ops = NULL;
  1036. kfree(priv);
  1037. } else {
  1038. dev_info(&adapter->dev, "OV2640 Probed\n");
  1039. }
  1040. return ret;
  1041. }
  1042. static int ov2640_remove(struct i2c_client *client)
  1043. {
  1044. struct ov2640_priv *priv = to_ov2640(client);
  1045. struct soc_camera_device *icd = client->dev.platform_data;
  1046. icd->ops = NULL;
  1047. kfree(priv);
  1048. return 0;
  1049. }
  1050. static const struct i2c_device_id ov2640_id[] = {
  1051. { "ov2640", 0 },
  1052. { }
  1053. };
  1054. MODULE_DEVICE_TABLE(i2c, ov2640_id);
  1055. static struct i2c_driver ov2640_i2c_driver = {
  1056. .driver = {
  1057. .name = "ov2640",
  1058. },
  1059. .probe = ov2640_probe,
  1060. .remove = ov2640_remove,
  1061. .id_table = ov2640_id,
  1062. };
  1063. /*
  1064. * Module functions
  1065. */
  1066. static int __init ov2640_module_init(void)
  1067. {
  1068. return i2c_add_driver(&ov2640_i2c_driver);
  1069. }
  1070. static void __exit ov2640_module_exit(void)
  1071. {
  1072. i2c_del_driver(&ov2640_i2c_driver);
  1073. }
  1074. module_init(ov2640_module_init);
  1075. module_exit(ov2640_module_exit);
  1076. MODULE_DESCRIPTION("SoC Camera driver for Omni Vision 2640 sensor");
  1077. MODULE_AUTHOR("Alberto Panizzo");
  1078. MODULE_LICENSE("GPL v2");