ov2640.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  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_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  661. {
  662. struct i2c_client *client = v4l2_get_subdevdata(sd);
  663. struct ov2640_priv *priv = to_ov2640(client);
  664. switch (ctrl->id) {
  665. case V4L2_CID_VFLIP:
  666. ctrl->value = priv->flag_vflip;
  667. break;
  668. case V4L2_CID_HFLIP:
  669. ctrl->value = priv->flag_hflip;
  670. break;
  671. }
  672. return 0;
  673. }
  674. static int ov2640_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  675. {
  676. struct i2c_client *client = v4l2_get_subdevdata(sd);
  677. struct ov2640_priv *priv = to_ov2640(client);
  678. int ret = 0;
  679. u8 val;
  680. switch (ctrl->id) {
  681. case V4L2_CID_VFLIP:
  682. val = ctrl->value ? REG04_VFLIP_IMG : 0x00;
  683. priv->flag_vflip = ctrl->value ? 1 : 0;
  684. ret = ov2640_mask_set(client, REG04, REG04_VFLIP_IMG, val);
  685. break;
  686. case V4L2_CID_HFLIP:
  687. val = ctrl->value ? REG04_HFLIP_IMG : 0x00;
  688. priv->flag_hflip = ctrl->value ? 1 : 0;
  689. ret = ov2640_mask_set(client, REG04, REG04_HFLIP_IMG, val);
  690. break;
  691. }
  692. return ret;
  693. }
  694. static int ov2640_g_chip_ident(struct v4l2_subdev *sd,
  695. struct v4l2_dbg_chip_ident *id)
  696. {
  697. struct i2c_client *client = v4l2_get_subdevdata(sd);
  698. struct ov2640_priv *priv = to_ov2640(client);
  699. id->ident = priv->model;
  700. id->revision = 0;
  701. return 0;
  702. }
  703. #ifdef CONFIG_VIDEO_ADV_DEBUG
  704. static int ov2640_g_register(struct v4l2_subdev *sd,
  705. struct v4l2_dbg_register *reg)
  706. {
  707. struct i2c_client *client = v4l2_get_subdevdata(sd);
  708. int ret;
  709. reg->size = 1;
  710. if (reg->reg > 0xff)
  711. return -EINVAL;
  712. ret = i2c_smbus_read_byte_data(client, reg->reg);
  713. if (ret < 0)
  714. return ret;
  715. reg->val = ret;
  716. return 0;
  717. }
  718. static int ov2640_s_register(struct v4l2_subdev *sd,
  719. struct v4l2_dbg_register *reg)
  720. {
  721. struct i2c_client *client = v4l2_get_subdevdata(sd);
  722. if (reg->reg > 0xff ||
  723. reg->val > 0xff)
  724. return -EINVAL;
  725. return i2c_smbus_write_byte_data(client, reg->reg, reg->val);
  726. }
  727. #endif
  728. /* Select the nearest higher resolution for capture */
  729. static const struct ov2640_win_size *ov2640_select_win(u32 *width, u32 *height)
  730. {
  731. int i, default_size = ARRAY_SIZE(ov2640_supported_win_sizes) - 1;
  732. for (i = 0; i < ARRAY_SIZE(ov2640_supported_win_sizes); i++) {
  733. if (ov2640_supported_win_sizes[i].width >= *width &&
  734. ov2640_supported_win_sizes[i].height >= *height) {
  735. *width = ov2640_supported_win_sizes[i].width;
  736. *height = ov2640_supported_win_sizes[i].height;
  737. return &ov2640_supported_win_sizes[i];
  738. }
  739. }
  740. *width = ov2640_supported_win_sizes[default_size].width;
  741. *height = ov2640_supported_win_sizes[default_size].height;
  742. return &ov2640_supported_win_sizes[default_size];
  743. }
  744. static int ov2640_set_params(struct i2c_client *client, u32 *width, u32 *height,
  745. enum v4l2_mbus_pixelcode code)
  746. {
  747. struct ov2640_priv *priv = to_ov2640(client);
  748. const struct regval_list *selected_cfmt_regs;
  749. int ret;
  750. /* select win */
  751. priv->win = ov2640_select_win(width, height);
  752. /* select format */
  753. priv->cfmt_code = 0;
  754. switch (code) {
  755. case V4L2_MBUS_FMT_RGB565_2X8_LE:
  756. dev_dbg(&client->dev, "%s: Selected cfmt RGB565", __func__);
  757. selected_cfmt_regs = ov2640_rgb565_regs;
  758. break;
  759. default:
  760. case V4L2_MBUS_FMT_UYVY8_2X8:
  761. dev_dbg(&client->dev, "%s: Selected cfmt YUV422", __func__);
  762. selected_cfmt_regs = ov2640_yuv422_regs;
  763. }
  764. /* reset hardware */
  765. ov2640_reset(client);
  766. /* initialize the sensor with default data */
  767. dev_dbg(&client->dev, "%s: Init default", __func__);
  768. ret = ov2640_write_array(client, ov2640_init_regs);
  769. if (ret < 0)
  770. goto err;
  771. /* select preamble */
  772. dev_dbg(&client->dev, "%s: Set size to %s", __func__, priv->win->name);
  773. ret = ov2640_write_array(client, ov2640_size_change_preamble_regs);
  774. if (ret < 0)
  775. goto err;
  776. /* set size win */
  777. ret = ov2640_write_array(client, priv->win->regs);
  778. if (ret < 0)
  779. goto err;
  780. /* cfmt preamble */
  781. dev_dbg(&client->dev, "%s: Set cfmt", __func__);
  782. ret = ov2640_write_array(client, ov2640_format_change_preamble_regs);
  783. if (ret < 0)
  784. goto err;
  785. /* set cfmt */
  786. ret = ov2640_write_array(client, selected_cfmt_regs);
  787. if (ret < 0)
  788. goto err;
  789. priv->cfmt_code = code;
  790. *width = priv->win->width;
  791. *height = priv->win->height;
  792. return 0;
  793. err:
  794. dev_err(&client->dev, "%s: Error %d", __func__, ret);
  795. ov2640_reset(client);
  796. priv->win = NULL;
  797. return ret;
  798. }
  799. static int ov2640_g_fmt(struct v4l2_subdev *sd,
  800. struct v4l2_mbus_framefmt *mf)
  801. {
  802. struct i2c_client *client = v4l2_get_subdevdata(sd);
  803. struct ov2640_priv *priv = to_ov2640(client);
  804. if (!priv->win) {
  805. u32 width = W_SVGA, height = H_SVGA;
  806. int ret = ov2640_set_params(client, &width, &height,
  807. V4L2_MBUS_FMT_UYVY8_2X8);
  808. if (ret < 0)
  809. return ret;
  810. }
  811. mf->width = priv->win->width;
  812. mf->height = priv->win->height;
  813. mf->code = priv->cfmt_code;
  814. switch (mf->code) {
  815. case V4L2_MBUS_FMT_RGB565_2X8_LE:
  816. mf->colorspace = V4L2_COLORSPACE_SRGB;
  817. break;
  818. default:
  819. case V4L2_MBUS_FMT_UYVY8_2X8:
  820. mf->colorspace = V4L2_COLORSPACE_JPEG;
  821. }
  822. mf->field = V4L2_FIELD_NONE;
  823. return 0;
  824. }
  825. static int ov2640_s_fmt(struct v4l2_subdev *sd,
  826. struct v4l2_mbus_framefmt *mf)
  827. {
  828. struct i2c_client *client = v4l2_get_subdevdata(sd);
  829. int ret;
  830. switch (mf->code) {
  831. case V4L2_MBUS_FMT_RGB565_2X8_LE:
  832. mf->colorspace = V4L2_COLORSPACE_SRGB;
  833. break;
  834. default:
  835. mf->code = V4L2_MBUS_FMT_UYVY8_2X8;
  836. case V4L2_MBUS_FMT_UYVY8_2X8:
  837. mf->colorspace = V4L2_COLORSPACE_JPEG;
  838. }
  839. ret = ov2640_set_params(client, &mf->width, &mf->height, mf->code);
  840. return ret;
  841. }
  842. static int ov2640_try_fmt(struct v4l2_subdev *sd,
  843. struct v4l2_mbus_framefmt *mf)
  844. {
  845. const struct ov2640_win_size *win;
  846. /*
  847. * select suitable win
  848. */
  849. win = ov2640_select_win(&mf->width, &mf->height);
  850. mf->field = V4L2_FIELD_NONE;
  851. switch (mf->code) {
  852. case V4L2_MBUS_FMT_RGB565_2X8_LE:
  853. mf->colorspace = V4L2_COLORSPACE_SRGB;
  854. break;
  855. default:
  856. mf->code = V4L2_MBUS_FMT_UYVY8_2X8;
  857. case V4L2_MBUS_FMT_UYVY8_2X8:
  858. mf->colorspace = V4L2_COLORSPACE_JPEG;
  859. }
  860. return 0;
  861. }
  862. static int ov2640_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
  863. enum v4l2_mbus_pixelcode *code)
  864. {
  865. if (index >= ARRAY_SIZE(ov2640_codes))
  866. return -EINVAL;
  867. *code = ov2640_codes[index];
  868. return 0;
  869. }
  870. static int ov2640_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  871. {
  872. a->c.left = 0;
  873. a->c.top = 0;
  874. a->c.width = W_UXGA;
  875. a->c.height = H_UXGA;
  876. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  877. return 0;
  878. }
  879. static int ov2640_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
  880. {
  881. a->bounds.left = 0;
  882. a->bounds.top = 0;
  883. a->bounds.width = W_UXGA;
  884. a->bounds.height = H_UXGA;
  885. a->defrect = a->bounds;
  886. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  887. a->pixelaspect.numerator = 1;
  888. a->pixelaspect.denominator = 1;
  889. return 0;
  890. }
  891. static int ov2640_video_probe(struct soc_camera_device *icd,
  892. struct i2c_client *client)
  893. {
  894. struct ov2640_priv *priv = to_ov2640(client);
  895. u8 pid, ver, midh, midl;
  896. const char *devname;
  897. int ret;
  898. /* We must have a parent by now. And it cannot be a wrong one. */
  899. BUG_ON(!icd->parent ||
  900. to_soc_camera_host(icd->parent)->nr != icd->iface);
  901. /*
  902. * check and show product ID and manufacturer ID
  903. */
  904. i2c_smbus_write_byte_data(client, BANK_SEL, BANK_SEL_SENS);
  905. pid = i2c_smbus_read_byte_data(client, PID);
  906. ver = i2c_smbus_read_byte_data(client, VER);
  907. midh = i2c_smbus_read_byte_data(client, MIDH);
  908. midl = i2c_smbus_read_byte_data(client, MIDL);
  909. switch (VERSION(pid, ver)) {
  910. case PID_OV2640:
  911. devname = "ov2640";
  912. priv->model = V4L2_IDENT_OV2640;
  913. break;
  914. default:
  915. dev_err(&client->dev,
  916. "Product ID error %x:%x\n", pid, ver);
  917. ret = -ENODEV;
  918. goto err;
  919. }
  920. dev_info(&client->dev,
  921. "%s Product ID %0x:%0x Manufacturer ID %x:%x\n",
  922. devname, pid, ver, midh, midl);
  923. return 0;
  924. err:
  925. return ret;
  926. }
  927. static struct soc_camera_ops ov2640_ops = {
  928. .controls = ov2640_controls,
  929. .num_controls = ARRAY_SIZE(ov2640_controls),
  930. };
  931. static struct v4l2_subdev_core_ops ov2640_subdev_core_ops = {
  932. .g_ctrl = ov2640_g_ctrl,
  933. .s_ctrl = ov2640_s_ctrl,
  934. .g_chip_ident = ov2640_g_chip_ident,
  935. #ifdef CONFIG_VIDEO_ADV_DEBUG
  936. .g_register = ov2640_g_register,
  937. .s_register = ov2640_s_register,
  938. #endif
  939. };
  940. static int ov2640_g_mbus_config(struct v4l2_subdev *sd,
  941. struct v4l2_mbus_config *cfg)
  942. {
  943. struct i2c_client *client = v4l2_get_subdevdata(sd);
  944. struct soc_camera_device *icd = client->dev.platform_data;
  945. struct soc_camera_link *icl = to_soc_camera_link(icd);
  946. cfg->flags = V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_MASTER |
  947. V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_HIGH |
  948. V4L2_MBUS_DATA_ACTIVE_HIGH;
  949. cfg->type = V4L2_MBUS_PARALLEL;
  950. cfg->flags = soc_camera_apply_board_flags(icl, cfg);
  951. return 0;
  952. }
  953. static struct v4l2_subdev_video_ops ov2640_subdev_video_ops = {
  954. .s_stream = ov2640_s_stream,
  955. .g_mbus_fmt = ov2640_g_fmt,
  956. .s_mbus_fmt = ov2640_s_fmt,
  957. .try_mbus_fmt = ov2640_try_fmt,
  958. .cropcap = ov2640_cropcap,
  959. .g_crop = ov2640_g_crop,
  960. .enum_mbus_fmt = ov2640_enum_fmt,
  961. .g_mbus_config = ov2640_g_mbus_config,
  962. };
  963. static struct v4l2_subdev_ops ov2640_subdev_ops = {
  964. .core = &ov2640_subdev_core_ops,
  965. .video = &ov2640_subdev_video_ops,
  966. };
  967. /*
  968. * i2c_driver functions
  969. */
  970. static int ov2640_probe(struct i2c_client *client,
  971. const struct i2c_device_id *did)
  972. {
  973. struct ov2640_priv *priv;
  974. struct soc_camera_device *icd = client->dev.platform_data;
  975. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  976. struct soc_camera_link *icl;
  977. int ret;
  978. if (!icd) {
  979. dev_err(&adapter->dev, "OV2640: missing soc-camera data!\n");
  980. return -EINVAL;
  981. }
  982. icl = to_soc_camera_link(icd);
  983. if (!icl) {
  984. dev_err(&adapter->dev,
  985. "OV2640: Missing platform_data for driver\n");
  986. return -EINVAL;
  987. }
  988. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  989. dev_err(&adapter->dev,
  990. "OV2640: I2C-Adapter doesn't support SMBUS\n");
  991. return -EIO;
  992. }
  993. priv = kzalloc(sizeof(struct ov2640_priv), GFP_KERNEL);
  994. if (!priv) {
  995. dev_err(&adapter->dev,
  996. "Failed to allocate memory for private data!\n");
  997. return -ENOMEM;
  998. }
  999. v4l2_i2c_subdev_init(&priv->subdev, client, &ov2640_subdev_ops);
  1000. icd->ops = &ov2640_ops;
  1001. ret = ov2640_video_probe(icd, client);
  1002. if (ret) {
  1003. icd->ops = NULL;
  1004. kfree(priv);
  1005. } else {
  1006. dev_info(&adapter->dev, "OV2640 Probed\n");
  1007. }
  1008. return ret;
  1009. }
  1010. static int ov2640_remove(struct i2c_client *client)
  1011. {
  1012. struct ov2640_priv *priv = to_ov2640(client);
  1013. struct soc_camera_device *icd = client->dev.platform_data;
  1014. icd->ops = NULL;
  1015. kfree(priv);
  1016. return 0;
  1017. }
  1018. static const struct i2c_device_id ov2640_id[] = {
  1019. { "ov2640", 0 },
  1020. { }
  1021. };
  1022. MODULE_DEVICE_TABLE(i2c, ov2640_id);
  1023. static struct i2c_driver ov2640_i2c_driver = {
  1024. .driver = {
  1025. .name = "ov2640",
  1026. },
  1027. .probe = ov2640_probe,
  1028. .remove = ov2640_remove,
  1029. .id_table = ov2640_id,
  1030. };
  1031. /*
  1032. * Module functions
  1033. */
  1034. static int __init ov2640_module_init(void)
  1035. {
  1036. return i2c_add_driver(&ov2640_i2c_driver);
  1037. }
  1038. static void __exit ov2640_module_exit(void)
  1039. {
  1040. i2c_del_driver(&ov2640_i2c_driver);
  1041. }
  1042. module_init(ov2640_module_init);
  1043. module_exit(ov2640_module_exit);
  1044. MODULE_DESCRIPTION("SoC Camera driver for Omni Vision 2640 sensor");
  1045. MODULE_AUTHOR("Alberto Panizzo");
  1046. MODULE_LICENSE("GPL v2");