ov2640.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  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/v4l2-mediabus.h>
  21. #include <linux/videodev2.h>
  22. #include <media/soc_camera.h>
  23. #include <media/v4l2-chip-ident.h>
  24. #include <media/v4l2-subdev.h>
  25. #include <media/v4l2-ctrls.h>
  26. #define VAL_SET(x, mask, rshift, lshift) \
  27. ((((x) >> rshift) & mask) << lshift)
  28. /*
  29. * DSP registers
  30. * register offset for BANK_SEL == BANK_SEL_DSP
  31. */
  32. #define R_BYPASS 0x05 /* Bypass DSP */
  33. #define R_BYPASS_DSP_BYPAS 0x01 /* Bypass DSP, sensor out directly */
  34. #define R_BYPASS_USE_DSP 0x00 /* Use the internal DSP */
  35. #define QS 0x44 /* Quantization Scale Factor */
  36. #define CTRLI 0x50
  37. #define CTRLI_LP_DP 0x80
  38. #define CTRLI_ROUND 0x40
  39. #define CTRLI_V_DIV_SET(x) VAL_SET(x, 0x3, 0, 3)
  40. #define CTRLI_H_DIV_SET(x) VAL_SET(x, 0x3, 0, 0)
  41. #define HSIZE 0x51 /* H_SIZE[7:0] (real/4) */
  42. #define HSIZE_SET(x) VAL_SET(x, 0xFF, 2, 0)
  43. #define VSIZE 0x52 /* V_SIZE[7:0] (real/4) */
  44. #define VSIZE_SET(x) VAL_SET(x, 0xFF, 2, 0)
  45. #define XOFFL 0x53 /* OFFSET_X[7:0] */
  46. #define XOFFL_SET(x) VAL_SET(x, 0xFF, 0, 0)
  47. #define YOFFL 0x54 /* OFFSET_Y[7:0] */
  48. #define YOFFL_SET(x) VAL_SET(x, 0xFF, 0, 0)
  49. #define VHYX 0x55 /* Offset and size completion */
  50. #define VHYX_VSIZE_SET(x) VAL_SET(x, 0x1, (8+2), 7)
  51. #define VHYX_HSIZE_SET(x) VAL_SET(x, 0x1, (8+2), 3)
  52. #define VHYX_YOFF_SET(x) VAL_SET(x, 0x3, 8, 4)
  53. #define VHYX_XOFF_SET(x) VAL_SET(x, 0x3, 8, 0)
  54. #define DPRP 0x56
  55. #define TEST 0x57 /* Horizontal size completion */
  56. #define TEST_HSIZE_SET(x) VAL_SET(x, 0x1, (9+2), 7)
  57. #define ZMOW 0x5A /* Zoom: Out Width OUTW[7:0] (real/4) */
  58. #define ZMOW_OUTW_SET(x) VAL_SET(x, 0xFF, 2, 0)
  59. #define ZMOH 0x5B /* Zoom: Out Height OUTH[7:0] (real/4) */
  60. #define ZMOH_OUTH_SET(x) VAL_SET(x, 0xFF, 2, 0)
  61. #define ZMHH 0x5C /* Zoom: Speed and H&W completion */
  62. #define ZMHH_ZSPEED_SET(x) VAL_SET(x, 0x0F, 0, 4)
  63. #define ZMHH_OUTH_SET(x) VAL_SET(x, 0x1, (8+2), 2)
  64. #define ZMHH_OUTW_SET(x) VAL_SET(x, 0x3, (8+2), 0)
  65. #define BPADDR 0x7C /* SDE Indirect Register Access: Address */
  66. #define BPDATA 0x7D /* SDE Indirect Register Access: Data */
  67. #define CTRL2 0x86 /* DSP Module enable 2 */
  68. #define CTRL2_DCW_EN 0x20
  69. #define CTRL2_SDE_EN 0x10
  70. #define CTRL2_UV_ADJ_EN 0x08
  71. #define CTRL2_UV_AVG_EN 0x04
  72. #define CTRL2_CMX_EN 0x01
  73. #define CTRL3 0x87 /* DSP Module enable 3 */
  74. #define CTRL3_BPC_EN 0x80
  75. #define CTRL3_WPC_EN 0x40
  76. #define SIZEL 0x8C /* Image Size Completion */
  77. #define SIZEL_HSIZE8_11_SET(x) VAL_SET(x, 0x1, 11, 6)
  78. #define SIZEL_HSIZE8_SET(x) VAL_SET(x, 0x7, 0, 3)
  79. #define SIZEL_VSIZE8_SET(x) VAL_SET(x, 0x7, 0, 0)
  80. #define HSIZE8 0xC0 /* Image Horizontal Size HSIZE[10:3] */
  81. #define HSIZE8_SET(x) VAL_SET(x, 0xFF, 3, 0)
  82. #define VSIZE8 0xC1 /* Image Vertical Size VSIZE[10:3] */
  83. #define VSIZE8_SET(x) VAL_SET(x, 0xFF, 3, 0)
  84. #define CTRL0 0xC2 /* DSP Module enable 0 */
  85. #define CTRL0_AEC_EN 0x80
  86. #define CTRL0_AEC_SEL 0x40
  87. #define CTRL0_STAT_SEL 0x20
  88. #define CTRL0_VFIRST 0x10
  89. #define CTRL0_YUV422 0x08
  90. #define CTRL0_YUV_EN 0x04
  91. #define CTRL0_RGB_EN 0x02
  92. #define CTRL0_RAW_EN 0x01
  93. #define CTRL1 0xC3 /* DSP Module enable 1 */
  94. #define CTRL1_CIP 0x80
  95. #define CTRL1_DMY 0x40
  96. #define CTRL1_RAW_GMA 0x20
  97. #define CTRL1_DG 0x10
  98. #define CTRL1_AWB 0x08
  99. #define CTRL1_AWB_GAIN 0x04
  100. #define CTRL1_LENC 0x02
  101. #define CTRL1_PRE 0x01
  102. #define R_DVP_SP 0xD3 /* DVP output speed control */
  103. #define R_DVP_SP_AUTO_MODE 0x80
  104. #define R_DVP_SP_DVP_MASK 0x3F /* DVP PCLK = sysclk (48)/[6:0] (YUV0);
  105. * = sysclk (48)/(2*[6:0]) (RAW);*/
  106. #define IMAGE_MODE 0xDA /* Image Output Format Select */
  107. #define IMAGE_MODE_Y8_DVP_EN 0x40
  108. #define IMAGE_MODE_JPEG_EN 0x10
  109. #define IMAGE_MODE_YUV422 0x00
  110. #define IMAGE_MODE_RAW10 0x04 /* (DVP) */
  111. #define IMAGE_MODE_RGB565 0x08
  112. #define IMAGE_MODE_HREF_VSYNC 0x02 /* HREF timing select in DVP JPEG output
  113. * mode (0 for HREF is same as sensor) */
  114. #define IMAGE_MODE_LBYTE_FIRST 0x01 /* Byte swap enable for DVP
  115. * 1: Low byte first UYVY (C2[4] =0)
  116. * VYUY (C2[4] =1)
  117. * 0: High byte first YUYV (C2[4]=0)
  118. * YVYU (C2[4] = 1) */
  119. #define RESET 0xE0 /* Reset */
  120. #define RESET_MICROC 0x40
  121. #define RESET_SCCB 0x20
  122. #define RESET_JPEG 0x10
  123. #define RESET_DVP 0x04
  124. #define RESET_IPU 0x02
  125. #define RESET_CIF 0x01
  126. #define REGED 0xED /* Register ED */
  127. #define REGED_CLK_OUT_DIS 0x10
  128. #define MS_SP 0xF0 /* SCCB Master Speed */
  129. #define SS_ID 0xF7 /* SCCB Slave ID */
  130. #define SS_CTRL 0xF8 /* SCCB Slave Control */
  131. #define SS_CTRL_ADD_AUTO_INC 0x20
  132. #define SS_CTRL_EN 0x08
  133. #define SS_CTRL_DELAY_CLK 0x04
  134. #define SS_CTRL_ACC_EN 0x02
  135. #define SS_CTRL_SEN_PASS_THR 0x01
  136. #define MC_BIST 0xF9 /* Microcontroller misc register */
  137. #define MC_BIST_RESET 0x80 /* Microcontroller Reset */
  138. #define MC_BIST_BOOT_ROM_SEL 0x40
  139. #define MC_BIST_12KB_SEL 0x20
  140. #define MC_BIST_12KB_MASK 0x30
  141. #define MC_BIST_512KB_SEL 0x08
  142. #define MC_BIST_512KB_MASK 0x0C
  143. #define MC_BIST_BUSY_BIT_R 0x02
  144. #define MC_BIST_MC_RES_ONE_SH_W 0x02
  145. #define MC_BIST_LAUNCH 0x01
  146. #define BANK_SEL 0xFF /* Register Bank Select */
  147. #define BANK_SEL_DSP 0x00
  148. #define BANK_SEL_SENS 0x01
  149. /*
  150. * Sensor registers
  151. * register offset for BANK_SEL == BANK_SEL_SENS
  152. */
  153. #define GAIN 0x00 /* AGC - Gain control gain setting */
  154. #define COM1 0x03 /* Common control 1 */
  155. #define COM1_1_DUMMY_FR 0x40
  156. #define COM1_3_DUMMY_FR 0x80
  157. #define COM1_7_DUMMY_FR 0xC0
  158. #define COM1_VWIN_LSB_UXGA 0x0F
  159. #define COM1_VWIN_LSB_SVGA 0x0A
  160. #define COM1_VWIN_LSB_CIF 0x06
  161. #define REG04 0x04 /* Register 04 */
  162. #define REG04_DEF 0x20 /* Always set */
  163. #define REG04_HFLIP_IMG 0x80 /* Horizontal mirror image ON/OFF */
  164. #define REG04_VFLIP_IMG 0x40 /* Vertical flip image ON/OFF */
  165. #define REG04_VREF_EN 0x10
  166. #define REG04_HREF_EN 0x08
  167. #define REG04_AEC_SET(x) VAL_SET(x, 0x3, 0, 0)
  168. #define REG08 0x08 /* Frame Exposure One-pin Control Pre-charge Row Num */
  169. #define COM2 0x09 /* Common control 2 */
  170. #define COM2_SOFT_SLEEP_MODE 0x10 /* Soft sleep mode */
  171. /* Output drive capability */
  172. #define COM2_OCAP_Nx_SET(N) (((N) - 1) & 0x03) /* N = [1x .. 4x] */
  173. #define PID 0x0A /* Product ID Number MSB */
  174. #define VER 0x0B /* Product ID Number LSB */
  175. #define COM3 0x0C /* Common control 3 */
  176. #define COM3_BAND_50H 0x04 /* 0 For Banding at 60H */
  177. #define COM3_BAND_AUTO 0x02 /* Auto Banding */
  178. #define COM3_SING_FR_SNAPSH 0x01 /* 0 For enable live video output after the
  179. * snapshot sequence*/
  180. #define AEC 0x10 /* AEC[9:2] Exposure Value */
  181. #define CLKRC 0x11 /* Internal clock */
  182. #define CLKRC_EN 0x80
  183. #define CLKRC_DIV_SET(x) (((x) - 1) & 0x1F) /* CLK = XVCLK/(x) */
  184. #define COM7 0x12 /* Common control 7 */
  185. #define COM7_SRST 0x80 /* Initiates system reset. All registers are
  186. * set to factory default values after which
  187. * the chip resumes normal operation */
  188. #define COM7_RES_UXGA 0x00 /* Resolution selectors for UXGA */
  189. #define COM7_RES_SVGA 0x40 /* SVGA */
  190. #define COM7_RES_CIF 0x20 /* CIF */
  191. #define COM7_ZOOM_EN 0x04 /* Enable Zoom mode */
  192. #define COM7_COLOR_BAR_TEST 0x02 /* Enable Color Bar Test Pattern */
  193. #define COM8 0x13 /* Common control 8 */
  194. #define COM8_DEF 0xC0 /* Banding filter ON/OFF */
  195. #define COM8_BNDF_EN 0x20 /* Banding filter ON/OFF */
  196. #define COM8_AGC_EN 0x04 /* AGC Auto/Manual control selection */
  197. #define COM8_AEC_EN 0x01 /* Auto/Manual Exposure control */
  198. #define COM9 0x14 /* Common control 9
  199. * Automatic gain ceiling - maximum AGC value [7:5]*/
  200. #define COM9_AGC_GAIN_2x 0x00 /* 000 : 2x */
  201. #define COM9_AGC_GAIN_4x 0x20 /* 001 : 4x */
  202. #define COM9_AGC_GAIN_8x 0x40 /* 010 : 8x */
  203. #define COM9_AGC_GAIN_16x 0x60 /* 011 : 16x */
  204. #define COM9_AGC_GAIN_32x 0x80 /* 100 : 32x */
  205. #define COM9_AGC_GAIN_64x 0xA0 /* 101 : 64x */
  206. #define COM9_AGC_GAIN_128x 0xC0 /* 110 : 128x */
  207. #define COM10 0x15 /* Common control 10 */
  208. #define COM10_PCLK_HREF 0x20 /* PCLK output qualified by HREF */
  209. #define COM10_PCLK_RISE 0x10 /* Data is updated at the rising edge of
  210. * PCLK (user can latch data at the next
  211. * falling edge of PCLK).
  212. * 0 otherwise. */
  213. #define COM10_HREF_INV 0x08 /* Invert HREF polarity:
  214. * HREF negative for valid data*/
  215. #define COM10_VSINC_INV 0x02 /* Invert VSYNC polarity */
  216. #define HSTART 0x17 /* Horizontal Window start MSB 8 bit */
  217. #define HEND 0x18 /* Horizontal Window end MSB 8 bit */
  218. #define VSTART 0x19 /* Vertical Window start MSB 8 bit */
  219. #define VEND 0x1A /* Vertical Window end MSB 8 bit */
  220. #define MIDH 0x1C /* Manufacturer ID byte - high */
  221. #define MIDL 0x1D /* Manufacturer ID byte - low */
  222. #define AEW 0x24 /* AGC/AEC - Stable operating region (upper limit) */
  223. #define AEB 0x25 /* AGC/AEC - Stable operating region (lower limit) */
  224. #define VV 0x26 /* AGC/AEC Fast mode operating region */
  225. #define VV_HIGH_TH_SET(x) VAL_SET(x, 0xF, 0, 4)
  226. #define VV_LOW_TH_SET(x) VAL_SET(x, 0xF, 0, 0)
  227. #define REG2A 0x2A /* Dummy pixel insert MSB */
  228. #define FRARL 0x2B /* Dummy pixel insert LSB */
  229. #define ADDVFL 0x2D /* LSB of insert dummy lines in Vertical direction */
  230. #define ADDVFH 0x2E /* MSB of insert dummy lines in Vertical direction */
  231. #define YAVG 0x2F /* Y/G Channel Average value */
  232. #define REG32 0x32 /* Common Control 32 */
  233. #define REG32_PCLK_DIV_2 0x80 /* PCLK freq divided by 2 */
  234. #define REG32_PCLK_DIV_4 0xC0 /* PCLK freq divided by 4 */
  235. #define ARCOM2 0x34 /* Zoom: Horizontal start point */
  236. #define REG45 0x45 /* Register 45 */
  237. #define FLL 0x46 /* Frame Length Adjustment LSBs */
  238. #define FLH 0x47 /* Frame Length Adjustment MSBs */
  239. #define COM19 0x48 /* Zoom: Vertical start point */
  240. #define ZOOMS 0x49 /* Zoom: Vertical start point */
  241. #define COM22 0x4B /* Flash light control */
  242. #define COM25 0x4E /* For Banding operations */
  243. #define BD50 0x4F /* 50Hz Banding AEC 8 LSBs */
  244. #define BD60 0x50 /* 60Hz Banding AEC 8 LSBs */
  245. #define REG5D 0x5D /* AVGsel[7:0], 16-zone average weight option */
  246. #define REG5E 0x5E /* AVGsel[15:8], 16-zone average weight option */
  247. #define REG5F 0x5F /* AVGsel[23:16], 16-zone average weight option */
  248. #define REG60 0x60 /* AVGsel[31:24], 16-zone average weight option */
  249. #define HISTO_LOW 0x61 /* Histogram Algorithm Low Level */
  250. #define HISTO_HIGH 0x62 /* Histogram Algorithm High Level */
  251. /*
  252. * ID
  253. */
  254. #define MANUFACTURER_ID 0x7FA2
  255. #define PID_OV2640 0x2642
  256. #define VERSION(pid, ver) ((pid << 8) | (ver & 0xFF))
  257. /*
  258. * Struct
  259. */
  260. struct regval_list {
  261. u8 reg_num;
  262. u8 value;
  263. };
  264. /* Supported resolutions */
  265. enum ov2640_width {
  266. W_QCIF = 176,
  267. W_QVGA = 320,
  268. W_CIF = 352,
  269. W_VGA = 640,
  270. W_SVGA = 800,
  271. W_XGA = 1024,
  272. W_SXGA = 1280,
  273. W_UXGA = 1600,
  274. };
  275. enum ov2640_height {
  276. H_QCIF = 144,
  277. H_QVGA = 240,
  278. H_CIF = 288,
  279. H_VGA = 480,
  280. H_SVGA = 600,
  281. H_XGA = 768,
  282. H_SXGA = 1024,
  283. H_UXGA = 1200,
  284. };
  285. struct ov2640_win_size {
  286. char *name;
  287. enum ov2640_width width;
  288. enum ov2640_height height;
  289. const struct regval_list *regs;
  290. };
  291. struct ov2640_priv {
  292. struct v4l2_subdev subdev;
  293. struct v4l2_ctrl_handler hdl;
  294. enum v4l2_mbus_pixelcode cfmt_code;
  295. const struct ov2640_win_size *win;
  296. int model;
  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. * General functions
  583. */
  584. static struct ov2640_priv *to_ov2640(const struct i2c_client *client)
  585. {
  586. return container_of(i2c_get_clientdata(client), struct ov2640_priv,
  587. subdev);
  588. }
  589. static int ov2640_write_array(struct i2c_client *client,
  590. const struct regval_list *vals)
  591. {
  592. int ret;
  593. while ((vals->reg_num != 0xff) || (vals->value != 0xff)) {
  594. ret = i2c_smbus_write_byte_data(client,
  595. vals->reg_num, vals->value);
  596. dev_vdbg(&client->dev, "array: 0x%02x, 0x%02x",
  597. vals->reg_num, vals->value);
  598. if (ret < 0)
  599. return ret;
  600. vals++;
  601. }
  602. return 0;
  603. }
  604. static int ov2640_mask_set(struct i2c_client *client,
  605. u8 reg, u8 mask, u8 set)
  606. {
  607. s32 val = i2c_smbus_read_byte_data(client, reg);
  608. if (val < 0)
  609. return val;
  610. val &= ~mask;
  611. val |= set & mask;
  612. dev_vdbg(&client->dev, "masks: 0x%02x, 0x%02x", reg, val);
  613. return i2c_smbus_write_byte_data(client, reg, val);
  614. }
  615. static int ov2640_reset(struct i2c_client *client)
  616. {
  617. int ret;
  618. const struct regval_list reset_seq[] = {
  619. {BANK_SEL, BANK_SEL_SENS},
  620. {COM7, COM7_SRST},
  621. ENDMARKER,
  622. };
  623. ret = ov2640_write_array(client, reset_seq);
  624. if (ret)
  625. goto err;
  626. msleep(5);
  627. err:
  628. dev_dbg(&client->dev, "%s: (ret %d)", __func__, ret);
  629. return ret;
  630. }
  631. /*
  632. * soc_camera_ops functions
  633. */
  634. static int ov2640_s_stream(struct v4l2_subdev *sd, int enable)
  635. {
  636. return 0;
  637. }
  638. static int ov2640_s_ctrl(struct v4l2_ctrl *ctrl)
  639. {
  640. struct v4l2_subdev *sd =
  641. &container_of(ctrl->handler, struct ov2640_priv, hdl)->subdev;
  642. struct i2c_client *client = v4l2_get_subdevdata(sd);
  643. u8 val;
  644. switch (ctrl->id) {
  645. case V4L2_CID_VFLIP:
  646. val = ctrl->val ? REG04_VFLIP_IMG : 0x00;
  647. return ov2640_mask_set(client, REG04, REG04_VFLIP_IMG, val);
  648. case V4L2_CID_HFLIP:
  649. val = ctrl->val ? REG04_HFLIP_IMG : 0x00;
  650. return ov2640_mask_set(client, REG04, REG04_HFLIP_IMG, val);
  651. }
  652. return -EINVAL;
  653. }
  654. static int ov2640_g_chip_ident(struct v4l2_subdev *sd,
  655. struct v4l2_dbg_chip_ident *id)
  656. {
  657. struct i2c_client *client = v4l2_get_subdevdata(sd);
  658. struct ov2640_priv *priv = to_ov2640(client);
  659. id->ident = priv->model;
  660. id->revision = 0;
  661. return 0;
  662. }
  663. #ifdef CONFIG_VIDEO_ADV_DEBUG
  664. static int ov2640_g_register(struct v4l2_subdev *sd,
  665. struct v4l2_dbg_register *reg)
  666. {
  667. struct i2c_client *client = v4l2_get_subdevdata(sd);
  668. int ret;
  669. reg->size = 1;
  670. if (reg->reg > 0xff)
  671. return -EINVAL;
  672. ret = i2c_smbus_read_byte_data(client, reg->reg);
  673. if (ret < 0)
  674. return ret;
  675. reg->val = ret;
  676. return 0;
  677. }
  678. static int ov2640_s_register(struct v4l2_subdev *sd,
  679. struct v4l2_dbg_register *reg)
  680. {
  681. struct i2c_client *client = v4l2_get_subdevdata(sd);
  682. if (reg->reg > 0xff ||
  683. reg->val > 0xff)
  684. return -EINVAL;
  685. return i2c_smbus_write_byte_data(client, reg->reg, reg->val);
  686. }
  687. #endif
  688. /* Select the nearest higher resolution for capture */
  689. static const struct ov2640_win_size *ov2640_select_win(u32 *width, u32 *height)
  690. {
  691. int i, default_size = ARRAY_SIZE(ov2640_supported_win_sizes) - 1;
  692. for (i = 0; i < ARRAY_SIZE(ov2640_supported_win_sizes); i++) {
  693. if (ov2640_supported_win_sizes[i].width >= *width &&
  694. ov2640_supported_win_sizes[i].height >= *height) {
  695. *width = ov2640_supported_win_sizes[i].width;
  696. *height = ov2640_supported_win_sizes[i].height;
  697. return &ov2640_supported_win_sizes[i];
  698. }
  699. }
  700. *width = ov2640_supported_win_sizes[default_size].width;
  701. *height = ov2640_supported_win_sizes[default_size].height;
  702. return &ov2640_supported_win_sizes[default_size];
  703. }
  704. static int ov2640_set_params(struct i2c_client *client, u32 *width, u32 *height,
  705. enum v4l2_mbus_pixelcode code)
  706. {
  707. struct ov2640_priv *priv = to_ov2640(client);
  708. const struct regval_list *selected_cfmt_regs;
  709. int ret;
  710. /* select win */
  711. priv->win = ov2640_select_win(width, height);
  712. /* select format */
  713. priv->cfmt_code = 0;
  714. switch (code) {
  715. case V4L2_MBUS_FMT_RGB565_2X8_LE:
  716. dev_dbg(&client->dev, "%s: Selected cfmt RGB565", __func__);
  717. selected_cfmt_regs = ov2640_rgb565_regs;
  718. break;
  719. default:
  720. case V4L2_MBUS_FMT_UYVY8_2X8:
  721. dev_dbg(&client->dev, "%s: Selected cfmt YUV422", __func__);
  722. selected_cfmt_regs = ov2640_yuv422_regs;
  723. }
  724. /* reset hardware */
  725. ov2640_reset(client);
  726. /* initialize the sensor with default data */
  727. dev_dbg(&client->dev, "%s: Init default", __func__);
  728. ret = ov2640_write_array(client, ov2640_init_regs);
  729. if (ret < 0)
  730. goto err;
  731. /* select preamble */
  732. dev_dbg(&client->dev, "%s: Set size to %s", __func__, priv->win->name);
  733. ret = ov2640_write_array(client, ov2640_size_change_preamble_regs);
  734. if (ret < 0)
  735. goto err;
  736. /* set size win */
  737. ret = ov2640_write_array(client, priv->win->regs);
  738. if (ret < 0)
  739. goto err;
  740. /* cfmt preamble */
  741. dev_dbg(&client->dev, "%s: Set cfmt", __func__);
  742. ret = ov2640_write_array(client, ov2640_format_change_preamble_regs);
  743. if (ret < 0)
  744. goto err;
  745. /* set cfmt */
  746. ret = ov2640_write_array(client, selected_cfmt_regs);
  747. if (ret < 0)
  748. goto err;
  749. priv->cfmt_code = code;
  750. *width = priv->win->width;
  751. *height = priv->win->height;
  752. return 0;
  753. err:
  754. dev_err(&client->dev, "%s: Error %d", __func__, ret);
  755. ov2640_reset(client);
  756. priv->win = NULL;
  757. return ret;
  758. }
  759. static int ov2640_g_fmt(struct v4l2_subdev *sd,
  760. struct v4l2_mbus_framefmt *mf)
  761. {
  762. struct i2c_client *client = v4l2_get_subdevdata(sd);
  763. struct ov2640_priv *priv = to_ov2640(client);
  764. if (!priv->win) {
  765. u32 width = W_SVGA, height = H_SVGA;
  766. int ret = ov2640_set_params(client, &width, &height,
  767. V4L2_MBUS_FMT_UYVY8_2X8);
  768. if (ret < 0)
  769. return ret;
  770. }
  771. mf->width = priv->win->width;
  772. mf->height = priv->win->height;
  773. mf->code = priv->cfmt_code;
  774. switch (mf->code) {
  775. case V4L2_MBUS_FMT_RGB565_2X8_LE:
  776. mf->colorspace = V4L2_COLORSPACE_SRGB;
  777. break;
  778. default:
  779. case V4L2_MBUS_FMT_UYVY8_2X8:
  780. mf->colorspace = V4L2_COLORSPACE_JPEG;
  781. }
  782. mf->field = V4L2_FIELD_NONE;
  783. return 0;
  784. }
  785. static int ov2640_s_fmt(struct v4l2_subdev *sd,
  786. struct v4l2_mbus_framefmt *mf)
  787. {
  788. struct i2c_client *client = v4l2_get_subdevdata(sd);
  789. int ret;
  790. switch (mf->code) {
  791. case V4L2_MBUS_FMT_RGB565_2X8_LE:
  792. mf->colorspace = V4L2_COLORSPACE_SRGB;
  793. break;
  794. default:
  795. mf->code = V4L2_MBUS_FMT_UYVY8_2X8;
  796. case V4L2_MBUS_FMT_UYVY8_2X8:
  797. mf->colorspace = V4L2_COLORSPACE_JPEG;
  798. }
  799. ret = ov2640_set_params(client, &mf->width, &mf->height, mf->code);
  800. return ret;
  801. }
  802. static int ov2640_try_fmt(struct v4l2_subdev *sd,
  803. struct v4l2_mbus_framefmt *mf)
  804. {
  805. const struct ov2640_win_size *win;
  806. /*
  807. * select suitable win
  808. */
  809. win = ov2640_select_win(&mf->width, &mf->height);
  810. mf->field = V4L2_FIELD_NONE;
  811. switch (mf->code) {
  812. case V4L2_MBUS_FMT_RGB565_2X8_LE:
  813. mf->colorspace = V4L2_COLORSPACE_SRGB;
  814. break;
  815. default:
  816. mf->code = V4L2_MBUS_FMT_UYVY8_2X8;
  817. case V4L2_MBUS_FMT_UYVY8_2X8:
  818. mf->colorspace = V4L2_COLORSPACE_JPEG;
  819. }
  820. return 0;
  821. }
  822. static int ov2640_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
  823. enum v4l2_mbus_pixelcode *code)
  824. {
  825. if (index >= ARRAY_SIZE(ov2640_codes))
  826. return -EINVAL;
  827. *code = ov2640_codes[index];
  828. return 0;
  829. }
  830. static int ov2640_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  831. {
  832. a->c.left = 0;
  833. a->c.top = 0;
  834. a->c.width = W_UXGA;
  835. a->c.height = H_UXGA;
  836. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  837. return 0;
  838. }
  839. static int ov2640_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
  840. {
  841. a->bounds.left = 0;
  842. a->bounds.top = 0;
  843. a->bounds.width = W_UXGA;
  844. a->bounds.height = H_UXGA;
  845. a->defrect = a->bounds;
  846. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  847. a->pixelaspect.numerator = 1;
  848. a->pixelaspect.denominator = 1;
  849. return 0;
  850. }
  851. static int ov2640_video_probe(struct i2c_client *client)
  852. {
  853. struct ov2640_priv *priv = to_ov2640(client);
  854. u8 pid, ver, midh, midl;
  855. const char *devname;
  856. int ret;
  857. /*
  858. * check and show product ID and manufacturer ID
  859. */
  860. i2c_smbus_write_byte_data(client, BANK_SEL, BANK_SEL_SENS);
  861. pid = i2c_smbus_read_byte_data(client, PID);
  862. ver = i2c_smbus_read_byte_data(client, VER);
  863. midh = i2c_smbus_read_byte_data(client, MIDH);
  864. midl = i2c_smbus_read_byte_data(client, MIDL);
  865. switch (VERSION(pid, ver)) {
  866. case PID_OV2640:
  867. devname = "ov2640";
  868. priv->model = V4L2_IDENT_OV2640;
  869. break;
  870. default:
  871. dev_err(&client->dev,
  872. "Product ID error %x:%x\n", pid, ver);
  873. ret = -ENODEV;
  874. goto err;
  875. }
  876. dev_info(&client->dev,
  877. "%s Product ID %0x:%0x Manufacturer ID %x:%x\n",
  878. devname, pid, ver, midh, midl);
  879. return v4l2_ctrl_handler_setup(&priv->hdl);
  880. err:
  881. return ret;
  882. }
  883. static const struct v4l2_ctrl_ops ov2640_ctrl_ops = {
  884. .s_ctrl = ov2640_s_ctrl,
  885. };
  886. static struct v4l2_subdev_core_ops ov2640_subdev_core_ops = {
  887. .g_chip_ident = ov2640_g_chip_ident,
  888. #ifdef CONFIG_VIDEO_ADV_DEBUG
  889. .g_register = ov2640_g_register,
  890. .s_register = ov2640_s_register,
  891. #endif
  892. };
  893. static int ov2640_g_mbus_config(struct v4l2_subdev *sd,
  894. struct v4l2_mbus_config *cfg)
  895. {
  896. struct i2c_client *client = v4l2_get_subdevdata(sd);
  897. struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
  898. cfg->flags = V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_MASTER |
  899. V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_HIGH |
  900. V4L2_MBUS_DATA_ACTIVE_HIGH;
  901. cfg->type = V4L2_MBUS_PARALLEL;
  902. cfg->flags = soc_camera_apply_board_flags(icl, cfg);
  903. return 0;
  904. }
  905. static struct v4l2_subdev_video_ops ov2640_subdev_video_ops = {
  906. .s_stream = ov2640_s_stream,
  907. .g_mbus_fmt = ov2640_g_fmt,
  908. .s_mbus_fmt = ov2640_s_fmt,
  909. .try_mbus_fmt = ov2640_try_fmt,
  910. .cropcap = ov2640_cropcap,
  911. .g_crop = ov2640_g_crop,
  912. .enum_mbus_fmt = ov2640_enum_fmt,
  913. .g_mbus_config = ov2640_g_mbus_config,
  914. };
  915. static struct v4l2_subdev_ops ov2640_subdev_ops = {
  916. .core = &ov2640_subdev_core_ops,
  917. .video = &ov2640_subdev_video_ops,
  918. };
  919. /*
  920. * i2c_driver functions
  921. */
  922. static int ov2640_probe(struct i2c_client *client,
  923. const struct i2c_device_id *did)
  924. {
  925. struct ov2640_priv *priv;
  926. struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
  927. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  928. int ret;
  929. if (!icl) {
  930. dev_err(&adapter->dev,
  931. "OV2640: Missing platform_data for driver\n");
  932. return -EINVAL;
  933. }
  934. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  935. dev_err(&adapter->dev,
  936. "OV2640: I2C-Adapter doesn't support SMBUS\n");
  937. return -EIO;
  938. }
  939. priv = kzalloc(sizeof(struct ov2640_priv), GFP_KERNEL);
  940. if (!priv) {
  941. dev_err(&adapter->dev,
  942. "Failed to allocate memory for private data!\n");
  943. return -ENOMEM;
  944. }
  945. v4l2_i2c_subdev_init(&priv->subdev, client, &ov2640_subdev_ops);
  946. v4l2_ctrl_handler_init(&priv->hdl, 2);
  947. v4l2_ctrl_new_std(&priv->hdl, &ov2640_ctrl_ops,
  948. V4L2_CID_VFLIP, 0, 1, 1, 0);
  949. v4l2_ctrl_new_std(&priv->hdl, &ov2640_ctrl_ops,
  950. V4L2_CID_HFLIP, 0, 1, 1, 0);
  951. priv->subdev.ctrl_handler = &priv->hdl;
  952. if (priv->hdl.error) {
  953. int err = priv->hdl.error;
  954. kfree(priv);
  955. return err;
  956. }
  957. ret = ov2640_video_probe(client);
  958. if (ret) {
  959. v4l2_ctrl_handler_free(&priv->hdl);
  960. kfree(priv);
  961. } else {
  962. dev_info(&adapter->dev, "OV2640 Probed\n");
  963. }
  964. return ret;
  965. }
  966. static int ov2640_remove(struct i2c_client *client)
  967. {
  968. struct ov2640_priv *priv = to_ov2640(client);
  969. v4l2_device_unregister_subdev(&priv->subdev);
  970. v4l2_ctrl_handler_free(&priv->hdl);
  971. kfree(priv);
  972. return 0;
  973. }
  974. static const struct i2c_device_id ov2640_id[] = {
  975. { "ov2640", 0 },
  976. { }
  977. };
  978. MODULE_DEVICE_TABLE(i2c, ov2640_id);
  979. static struct i2c_driver ov2640_i2c_driver = {
  980. .driver = {
  981. .name = "ov2640",
  982. },
  983. .probe = ov2640_probe,
  984. .remove = ov2640_remove,
  985. .id_table = ov2640_id,
  986. };
  987. module_i2c_driver(ov2640_i2c_driver);
  988. MODULE_DESCRIPTION("SoC Camera driver for Omni Vision 2640 sensor");
  989. MODULE_AUTHOR("Alberto Panizzo");
  990. MODULE_LICENSE("GPL v2");