ov2640.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205
  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/v4l2-chip-ident.h>
  22. #include <media/v4l2-subdev.h>
  23. #include <media/soc_camera.h>
  24. #include <media/soc_mediabus.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. struct ov2640_camera_info *info;
  293. enum v4l2_mbus_pixelcode cfmt_code;
  294. const struct ov2640_win_size *win;
  295. int model;
  296. u16 flag_vflip:1;
  297. u16 flag_hflip:1;
  298. };
  299. /*
  300. * Registers settings
  301. */
  302. #define ENDMARKER { 0xff, 0xff }
  303. static const struct regval_list ov2640_init_regs[] = {
  304. { BANK_SEL, BANK_SEL_DSP },
  305. { 0x2c, 0xff },
  306. { 0x2e, 0xdf },
  307. { BANK_SEL, BANK_SEL_SENS },
  308. { 0x3c, 0x32 },
  309. { CLKRC, CLKRC_DIV_SET(1) },
  310. { COM2, COM2_OCAP_Nx_SET(3) },
  311. { REG04, REG04_DEF | REG04_HREF_EN },
  312. { COM8, COM8_DEF | COM8_BNDF_EN | COM8_AGC_EN | COM8_AEC_EN },
  313. { COM9, COM9_AGC_GAIN_8x | 0x08},
  314. { 0x2c, 0x0c },
  315. { 0x33, 0x78 },
  316. { 0x3a, 0x33 },
  317. { 0x3b, 0xfb },
  318. { 0x3e, 0x00 },
  319. { 0x43, 0x11 },
  320. { 0x16, 0x10 },
  321. { 0x39, 0x02 },
  322. { 0x35, 0x88 },
  323. { 0x22, 0x0a },
  324. { 0x37, 0x40 },
  325. { 0x23, 0x00 },
  326. { ARCOM2, 0xa0 },
  327. { 0x06, 0x02 },
  328. { 0x06, 0x88 },
  329. { 0x07, 0xc0 },
  330. { 0x0d, 0xb7 },
  331. { 0x0e, 0x01 },
  332. { 0x4c, 0x00 },
  333. { 0x4a, 0x81 },
  334. { 0x21, 0x99 },
  335. { AEW, 0x40 },
  336. { AEB, 0x38 },
  337. { VV, VV_HIGH_TH_SET(0x08) | VV_LOW_TH_SET(0x02) },
  338. { 0x5c, 0x00 },
  339. { 0x63, 0x00 },
  340. { FLL, 0x22 },
  341. { COM3, 0x38 | COM3_BAND_AUTO },
  342. { REG5D, 0x55 },
  343. { REG5E, 0x7d },
  344. { REG5F, 0x7d },
  345. { REG60, 0x55 },
  346. { HISTO_LOW, 0x70 },
  347. { HISTO_HIGH, 0x80 },
  348. { 0x7c, 0x05 },
  349. { 0x20, 0x80 },
  350. { 0x28, 0x30 },
  351. { 0x6c, 0x00 },
  352. { 0x6d, 0x80 },
  353. { 0x6e, 0x00 },
  354. { 0x70, 0x02 },
  355. { 0x71, 0x94 },
  356. { 0x73, 0xc1 },
  357. { 0x3d, 0x34 },
  358. { COM7, COM7_RES_UXGA | COM7_ZOOM_EN },
  359. { 0x5a, 0x57 },
  360. { BD50, 0xbb },
  361. { BD60, 0x9c },
  362. { BANK_SEL, BANK_SEL_DSP },
  363. { 0xe5, 0x7f },
  364. { MC_BIST, MC_BIST_RESET | MC_BIST_BOOT_ROM_SEL },
  365. { 0x41, 0x24 },
  366. { RESET, RESET_JPEG | RESET_DVP },
  367. { 0x76, 0xff },
  368. { 0x33, 0xa0 },
  369. { 0x42, 0x20 },
  370. { 0x43, 0x18 },
  371. { 0x4c, 0x00 },
  372. { CTRL3, CTRL3_BPC_EN | CTRL3_WPC_EN | 0x10 },
  373. { 0x88, 0x3f },
  374. { 0xd7, 0x03 },
  375. { 0xd9, 0x10 },
  376. { R_DVP_SP , R_DVP_SP_AUTO_MODE | 0x2 },
  377. { 0xc8, 0x08 },
  378. { 0xc9, 0x80 },
  379. { BPADDR, 0x00 },
  380. { BPDATA, 0x00 },
  381. { BPADDR, 0x03 },
  382. { BPDATA, 0x48 },
  383. { BPDATA, 0x48 },
  384. { BPADDR, 0x08 },
  385. { BPDATA, 0x20 },
  386. { BPDATA, 0x10 },
  387. { BPDATA, 0x0e },
  388. { 0x90, 0x00 },
  389. { 0x91, 0x0e },
  390. { 0x91, 0x1a },
  391. { 0x91, 0x31 },
  392. { 0x91, 0x5a },
  393. { 0x91, 0x69 },
  394. { 0x91, 0x75 },
  395. { 0x91, 0x7e },
  396. { 0x91, 0x88 },
  397. { 0x91, 0x8f },
  398. { 0x91, 0x96 },
  399. { 0x91, 0xa3 },
  400. { 0x91, 0xaf },
  401. { 0x91, 0xc4 },
  402. { 0x91, 0xd7 },
  403. { 0x91, 0xe8 },
  404. { 0x91, 0x20 },
  405. { 0x92, 0x00 },
  406. { 0x93, 0x06 },
  407. { 0x93, 0xe3 },
  408. { 0x93, 0x03 },
  409. { 0x93, 0x03 },
  410. { 0x93, 0x00 },
  411. { 0x93, 0x02 },
  412. { 0x93, 0x00 },
  413. { 0x93, 0x00 },
  414. { 0x93, 0x00 },
  415. { 0x93, 0x00 },
  416. { 0x93, 0x00 },
  417. { 0x93, 0x00 },
  418. { 0x93, 0x00 },
  419. { 0x96, 0x00 },
  420. { 0x97, 0x08 },
  421. { 0x97, 0x19 },
  422. { 0x97, 0x02 },
  423. { 0x97, 0x0c },
  424. { 0x97, 0x24 },
  425. { 0x97, 0x30 },
  426. { 0x97, 0x28 },
  427. { 0x97, 0x26 },
  428. { 0x97, 0x02 },
  429. { 0x97, 0x98 },
  430. { 0x97, 0x80 },
  431. { 0x97, 0x00 },
  432. { 0x97, 0x00 },
  433. { 0xa4, 0x00 },
  434. { 0xa8, 0x00 },
  435. { 0xc5, 0x11 },
  436. { 0xc6, 0x51 },
  437. { 0xbf, 0x80 },
  438. { 0xc7, 0x10 },
  439. { 0xb6, 0x66 },
  440. { 0xb8, 0xA5 },
  441. { 0xb7, 0x64 },
  442. { 0xb9, 0x7C },
  443. { 0xb3, 0xaf },
  444. { 0xb4, 0x97 },
  445. { 0xb5, 0xFF },
  446. { 0xb0, 0xC5 },
  447. { 0xb1, 0x94 },
  448. { 0xb2, 0x0f },
  449. { 0xc4, 0x5c },
  450. { 0xa6, 0x00 },
  451. { 0xa7, 0x20 },
  452. { 0xa7, 0xd8 },
  453. { 0xa7, 0x1b },
  454. { 0xa7, 0x31 },
  455. { 0xa7, 0x00 },
  456. { 0xa7, 0x18 },
  457. { 0xa7, 0x20 },
  458. { 0xa7, 0xd8 },
  459. { 0xa7, 0x19 },
  460. { 0xa7, 0x31 },
  461. { 0xa7, 0x00 },
  462. { 0xa7, 0x18 },
  463. { 0xa7, 0x20 },
  464. { 0xa7, 0xd8 },
  465. { 0xa7, 0x19 },
  466. { 0xa7, 0x31 },
  467. { 0xa7, 0x00 },
  468. { 0xa7, 0x18 },
  469. { 0x7f, 0x00 },
  470. { 0xe5, 0x1f },
  471. { 0xe1, 0x77 },
  472. { 0xdd, 0x7f },
  473. { CTRL0, CTRL0_YUV422 | CTRL0_YUV_EN | CTRL0_RGB_EN },
  474. ENDMARKER,
  475. };
  476. /*
  477. * Register settings for window size
  478. * The preamble, setup the internal DSP to input an UXGA (1600x1200) image.
  479. * Then the different zooming configurations will setup the output image size.
  480. */
  481. static const struct regval_list ov2640_size_change_preamble_regs[] = {
  482. { BANK_SEL, BANK_SEL_DSP },
  483. { RESET, RESET_DVP },
  484. { HSIZE8, HSIZE8_SET(W_UXGA) },
  485. { VSIZE8, VSIZE8_SET(H_UXGA) },
  486. { CTRL2, CTRL2_DCW_EN | CTRL2_SDE_EN |
  487. CTRL2_UV_AVG_EN | CTRL2_CMX_EN | CTRL2_UV_ADJ_EN },
  488. { HSIZE, HSIZE_SET(W_UXGA) },
  489. { VSIZE, VSIZE_SET(H_UXGA) },
  490. { XOFFL, XOFFL_SET(0) },
  491. { YOFFL, YOFFL_SET(0) },
  492. { VHYX, VHYX_HSIZE_SET(W_UXGA) | VHYX_VSIZE_SET(H_UXGA) |
  493. VHYX_XOFF_SET(0) | VHYX_YOFF_SET(0)},
  494. { TEST, TEST_HSIZE_SET(W_UXGA) },
  495. ENDMARKER,
  496. };
  497. #define PER_SIZE_REG_SEQ(x, y, v_div, h_div, pclk_div) \
  498. { CTRLI, CTRLI_LP_DP | CTRLI_V_DIV_SET(v_div) | \
  499. CTRLI_H_DIV_SET(h_div)}, \
  500. { ZMOW, ZMOW_OUTW_SET(x) }, \
  501. { ZMOH, ZMOH_OUTH_SET(y) }, \
  502. { ZMHH, ZMHH_OUTW_SET(x) | ZMHH_OUTH_SET(y) }, \
  503. { R_DVP_SP, pclk_div }, \
  504. { RESET, 0x00}
  505. static const struct regval_list ov2640_qcif_regs[] = {
  506. PER_SIZE_REG_SEQ(W_QCIF, H_QCIF, 3, 3, 4),
  507. ENDMARKER,
  508. };
  509. static const struct regval_list ov2640_qvga_regs[] = {
  510. PER_SIZE_REG_SEQ(W_QVGA, H_QVGA, 2, 2, 4),
  511. ENDMARKER,
  512. };
  513. static const struct regval_list ov2640_cif_regs[] = {
  514. PER_SIZE_REG_SEQ(W_CIF, H_CIF, 2, 2, 8),
  515. ENDMARKER,
  516. };
  517. static const struct regval_list ov2640_vga_regs[] = {
  518. PER_SIZE_REG_SEQ(W_VGA, H_VGA, 0, 0, 2),
  519. ENDMARKER,
  520. };
  521. static const struct regval_list ov2640_svga_regs[] = {
  522. PER_SIZE_REG_SEQ(W_SVGA, H_SVGA, 1, 1, 2),
  523. ENDMARKER,
  524. };
  525. static const struct regval_list ov2640_xga_regs[] = {
  526. PER_SIZE_REG_SEQ(W_XGA, H_XGA, 0, 0, 2),
  527. { CTRLI, 0x00},
  528. ENDMARKER,
  529. };
  530. static const struct regval_list ov2640_sxga_regs[] = {
  531. PER_SIZE_REG_SEQ(W_SXGA, H_SXGA, 0, 0, 2),
  532. { CTRLI, 0x00},
  533. { R_DVP_SP, 2 | R_DVP_SP_AUTO_MODE },
  534. ENDMARKER,
  535. };
  536. static const struct regval_list ov2640_uxga_regs[] = {
  537. PER_SIZE_REG_SEQ(W_UXGA, H_UXGA, 0, 0, 0),
  538. { CTRLI, 0x00},
  539. { R_DVP_SP, 0 | R_DVP_SP_AUTO_MODE },
  540. ENDMARKER,
  541. };
  542. #define OV2640_SIZE(n, w, h, r) \
  543. {.name = n, .width = w , .height = h, .regs = r }
  544. static const struct ov2640_win_size ov2640_supported_win_sizes[] = {
  545. OV2640_SIZE("QCIF", W_QCIF, H_QCIF, ov2640_qcif_regs),
  546. OV2640_SIZE("QVGA", W_QVGA, H_QVGA, ov2640_qvga_regs),
  547. OV2640_SIZE("CIF", W_CIF, H_CIF, ov2640_cif_regs),
  548. OV2640_SIZE("VGA", W_VGA, H_VGA, ov2640_vga_regs),
  549. OV2640_SIZE("SVGA", W_SVGA, H_SVGA, ov2640_svga_regs),
  550. OV2640_SIZE("XGA", W_XGA, H_XGA, ov2640_xga_regs),
  551. OV2640_SIZE("SXGA", W_SXGA, H_SXGA, ov2640_sxga_regs),
  552. OV2640_SIZE("UXGA", W_UXGA, H_UXGA, ov2640_uxga_regs),
  553. };
  554. /*
  555. * Register settings for pixel formats
  556. */
  557. static const struct regval_list ov2640_format_change_preamble_regs[] = {
  558. { BANK_SEL, BANK_SEL_DSP },
  559. { R_BYPASS, R_BYPASS_USE_DSP },
  560. ENDMARKER,
  561. };
  562. static const struct regval_list ov2640_yuv422_regs[] = {
  563. { IMAGE_MODE, IMAGE_MODE_LBYTE_FIRST | IMAGE_MODE_YUV422 },
  564. { 0xD7, 0x01 },
  565. { 0x33, 0xa0 },
  566. { 0xe1, 0x67 },
  567. { RESET, 0x00 },
  568. { R_BYPASS, R_BYPASS_USE_DSP },
  569. ENDMARKER,
  570. };
  571. static const struct regval_list ov2640_rgb565_regs[] = {
  572. { IMAGE_MODE, IMAGE_MODE_LBYTE_FIRST | IMAGE_MODE_RGB565 },
  573. { 0xd7, 0x03 },
  574. { RESET, 0x00 },
  575. { R_BYPASS, R_BYPASS_USE_DSP },
  576. ENDMARKER,
  577. };
  578. static enum v4l2_mbus_pixelcode ov2640_codes[] = {
  579. V4L2_MBUS_FMT_UYVY8_2X8,
  580. V4L2_MBUS_FMT_RGB565_2X8_LE,
  581. };
  582. /*
  583. * Supported controls
  584. */
  585. static const struct v4l2_queryctrl ov2640_controls[] = {
  586. {
  587. .id = V4L2_CID_VFLIP,
  588. .type = V4L2_CTRL_TYPE_BOOLEAN,
  589. .name = "Flip Vertically",
  590. .minimum = 0,
  591. .maximum = 1,
  592. .step = 1,
  593. .default_value = 0,
  594. }, {
  595. .id = V4L2_CID_HFLIP,
  596. .type = V4L2_CTRL_TYPE_BOOLEAN,
  597. .name = "Flip Horizontally",
  598. .minimum = 0,
  599. .maximum = 1,
  600. .step = 1,
  601. .default_value = 0,
  602. },
  603. };
  604. /*
  605. * General functions
  606. */
  607. static struct ov2640_priv *to_ov2640(const struct i2c_client *client)
  608. {
  609. return container_of(i2c_get_clientdata(client), struct ov2640_priv,
  610. subdev);
  611. }
  612. static int ov2640_write_array(struct i2c_client *client,
  613. const struct regval_list *vals)
  614. {
  615. int ret;
  616. while ((vals->reg_num != 0xff) || (vals->value != 0xff)) {
  617. ret = i2c_smbus_write_byte_data(client,
  618. vals->reg_num, vals->value);
  619. dev_vdbg(&client->dev, "array: 0x%02x, 0x%02x",
  620. vals->reg_num, vals->value);
  621. if (ret < 0)
  622. return ret;
  623. vals++;
  624. }
  625. return 0;
  626. }
  627. static int ov2640_mask_set(struct i2c_client *client,
  628. u8 reg, u8 mask, u8 set)
  629. {
  630. s32 val = i2c_smbus_read_byte_data(client, reg);
  631. if (val < 0)
  632. return val;
  633. val &= ~mask;
  634. val |= set & mask;
  635. dev_vdbg(&client->dev, "masks: 0x%02x, 0x%02x", reg, val);
  636. return i2c_smbus_write_byte_data(client, reg, val);
  637. }
  638. static int ov2640_reset(struct i2c_client *client)
  639. {
  640. int ret;
  641. const struct regval_list reset_seq[] = {
  642. {BANK_SEL, BANK_SEL_SENS},
  643. {COM7, COM7_SRST},
  644. ENDMARKER,
  645. };
  646. ret = ov2640_write_array(client, reset_seq);
  647. if (ret)
  648. goto err;
  649. msleep(5);
  650. err:
  651. dev_dbg(&client->dev, "%s: (ret %d)", __func__, ret);
  652. return ret;
  653. }
  654. /*
  655. * soc_camera_ops functions
  656. */
  657. static int ov2640_s_stream(struct v4l2_subdev *sd, int enable)
  658. {
  659. return 0;
  660. }
  661. static int ov2640_set_bus_param(struct soc_camera_device *icd,
  662. unsigned long flags)
  663. {
  664. struct soc_camera_link *icl = to_soc_camera_link(icd);
  665. unsigned long width_flag = flags & SOCAM_DATAWIDTH_MASK;
  666. /* Only one width bit may be set */
  667. if (!is_power_of_2(width_flag))
  668. return -EINVAL;
  669. if (icl->set_bus_param)
  670. return icl->set_bus_param(icl, width_flag);
  671. /*
  672. * Without board specific bus width settings we support only the
  673. * sensors native bus width witch are tested working
  674. */
  675. if (width_flag & (SOCAM_DATAWIDTH_10 | SOCAM_DATAWIDTH_8))
  676. return 0;
  677. return 0;
  678. }
  679. static unsigned long ov2640_query_bus_param(struct soc_camera_device *icd)
  680. {
  681. struct soc_camera_link *icl = to_soc_camera_link(icd);
  682. unsigned long flags = SOCAM_PCLK_SAMPLE_RISING | SOCAM_MASTER |
  683. SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_HIGH |
  684. SOCAM_DATA_ACTIVE_HIGH;
  685. if (icl->query_bus_param)
  686. flags |= icl->query_bus_param(icl) & SOCAM_DATAWIDTH_MASK;
  687. else
  688. flags |= SOCAM_DATAWIDTH_10;
  689. return soc_camera_apply_sensor_flags(icl, flags);
  690. }
  691. static int ov2640_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  692. {
  693. struct i2c_client *client = v4l2_get_subdevdata(sd);
  694. struct ov2640_priv *priv = to_ov2640(client);
  695. switch (ctrl->id) {
  696. case V4L2_CID_VFLIP:
  697. ctrl->value = priv->flag_vflip;
  698. break;
  699. case V4L2_CID_HFLIP:
  700. ctrl->value = priv->flag_hflip;
  701. break;
  702. }
  703. return 0;
  704. }
  705. static int ov2640_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  706. {
  707. struct i2c_client *client = v4l2_get_subdevdata(sd);
  708. struct ov2640_priv *priv = to_ov2640(client);
  709. int ret = 0;
  710. u8 val;
  711. switch (ctrl->id) {
  712. case V4L2_CID_VFLIP:
  713. val = ctrl->value ? REG04_VFLIP_IMG : 0x00;
  714. priv->flag_vflip = ctrl->value ? 1 : 0;
  715. ret = ov2640_mask_set(client, REG04, REG04_VFLIP_IMG, val);
  716. break;
  717. case V4L2_CID_HFLIP:
  718. val = ctrl->value ? REG04_HFLIP_IMG : 0x00;
  719. priv->flag_hflip = ctrl->value ? 1 : 0;
  720. ret = ov2640_mask_set(client, REG04, REG04_HFLIP_IMG, val);
  721. break;
  722. }
  723. return ret;
  724. }
  725. static int ov2640_g_chip_ident(struct v4l2_subdev *sd,
  726. struct v4l2_dbg_chip_ident *id)
  727. {
  728. struct i2c_client *client = v4l2_get_subdevdata(sd);
  729. struct ov2640_priv *priv = to_ov2640(client);
  730. id->ident = priv->model;
  731. id->revision = 0;
  732. return 0;
  733. }
  734. #ifdef CONFIG_VIDEO_ADV_DEBUG
  735. static int ov2640_g_register(struct v4l2_subdev *sd,
  736. struct v4l2_dbg_register *reg)
  737. {
  738. struct i2c_client *client = v4l2_get_subdevdata(sd);
  739. int ret;
  740. reg->size = 1;
  741. if (reg->reg > 0xff)
  742. return -EINVAL;
  743. ret = i2c_smbus_read_byte_data(client, reg->reg);
  744. if (ret < 0)
  745. return ret;
  746. reg->val = ret;
  747. return 0;
  748. }
  749. static int ov2640_s_register(struct v4l2_subdev *sd,
  750. struct v4l2_dbg_register *reg)
  751. {
  752. struct i2c_client *client = v4l2_get_subdevdata(sd);
  753. if (reg->reg > 0xff ||
  754. reg->val > 0xff)
  755. return -EINVAL;
  756. return i2c_smbus_write_byte_data(client, reg->reg, reg->val);
  757. }
  758. #endif
  759. /* Select the nearest higher resolution for capture */
  760. static const struct ov2640_win_size *ov2640_select_win(u32 *width, u32 *height)
  761. {
  762. int i, default_size = ARRAY_SIZE(ov2640_supported_win_sizes) - 1;
  763. for (i = 0; i < ARRAY_SIZE(ov2640_supported_win_sizes); i++) {
  764. if (ov2640_supported_win_sizes[i].width >= *width &&
  765. ov2640_supported_win_sizes[i].height >= *height) {
  766. *width = ov2640_supported_win_sizes[i].width;
  767. *height = ov2640_supported_win_sizes[i].height;
  768. return &ov2640_supported_win_sizes[i];
  769. }
  770. }
  771. *width = ov2640_supported_win_sizes[default_size].width;
  772. *height = ov2640_supported_win_sizes[default_size].height;
  773. return &ov2640_supported_win_sizes[default_size];
  774. }
  775. static int ov2640_set_params(struct i2c_client *client, u32 *width, u32 *height,
  776. enum v4l2_mbus_pixelcode code)
  777. {
  778. struct ov2640_priv *priv = to_ov2640(client);
  779. const struct regval_list *selected_cfmt_regs;
  780. int ret;
  781. /* select win */
  782. priv->win = ov2640_select_win(width, height);
  783. /* select format */
  784. priv->cfmt_code = 0;
  785. switch (code) {
  786. case V4L2_MBUS_FMT_RGB565_2X8_LE:
  787. dev_dbg(&client->dev, "%s: Selected cfmt RGB565", __func__);
  788. selected_cfmt_regs = ov2640_rgb565_regs;
  789. break;
  790. default:
  791. case V4L2_MBUS_FMT_UYVY8_2X8:
  792. dev_dbg(&client->dev, "%s: Selected cfmt YUV422", __func__);
  793. selected_cfmt_regs = ov2640_yuv422_regs;
  794. }
  795. /* reset hardware */
  796. ov2640_reset(client);
  797. /* initialize the sensor with default data */
  798. dev_dbg(&client->dev, "%s: Init default", __func__);
  799. ret = ov2640_write_array(client, ov2640_init_regs);
  800. if (ret < 0)
  801. goto err;
  802. /* select preamble */
  803. dev_dbg(&client->dev, "%s: Set size to %s", __func__, priv->win->name);
  804. ret = ov2640_write_array(client, ov2640_size_change_preamble_regs);
  805. if (ret < 0)
  806. goto err;
  807. /* set size win */
  808. ret = ov2640_write_array(client, priv->win->regs);
  809. if (ret < 0)
  810. goto err;
  811. /* cfmt preamble */
  812. dev_dbg(&client->dev, "%s: Set cfmt", __func__);
  813. ret = ov2640_write_array(client, ov2640_format_change_preamble_regs);
  814. if (ret < 0)
  815. goto err;
  816. /* set cfmt */
  817. ret = ov2640_write_array(client, selected_cfmt_regs);
  818. if (ret < 0)
  819. goto err;
  820. priv->cfmt_code = code;
  821. *width = priv->win->width;
  822. *height = priv->win->height;
  823. return 0;
  824. err:
  825. dev_err(&client->dev, "%s: Error %d", __func__, ret);
  826. ov2640_reset(client);
  827. priv->win = NULL;
  828. return ret;
  829. }
  830. static int ov2640_g_fmt(struct v4l2_subdev *sd,
  831. struct v4l2_mbus_framefmt *mf)
  832. {
  833. struct i2c_client *client = v4l2_get_subdevdata(sd);
  834. struct ov2640_priv *priv = to_ov2640(client);
  835. if (!priv->win) {
  836. u32 width = W_SVGA, height = H_SVGA;
  837. int ret = ov2640_set_params(client, &width, &height,
  838. V4L2_MBUS_FMT_UYVY8_2X8);
  839. if (ret < 0)
  840. return ret;
  841. }
  842. mf->width = priv->win->width;
  843. mf->height = priv->win->height;
  844. mf->code = priv->cfmt_code;
  845. switch (mf->code) {
  846. case V4L2_MBUS_FMT_RGB565_2X8_LE:
  847. mf->colorspace = V4L2_COLORSPACE_SRGB;
  848. break;
  849. default:
  850. case V4L2_MBUS_FMT_UYVY8_2X8:
  851. mf->colorspace = V4L2_COLORSPACE_JPEG;
  852. }
  853. mf->field = V4L2_FIELD_NONE;
  854. return 0;
  855. }
  856. static int ov2640_s_fmt(struct v4l2_subdev *sd,
  857. struct v4l2_mbus_framefmt *mf)
  858. {
  859. struct i2c_client *client = v4l2_get_subdevdata(sd);
  860. int ret;
  861. switch (mf->code) {
  862. case V4L2_MBUS_FMT_RGB565_2X8_LE:
  863. mf->colorspace = V4L2_COLORSPACE_SRGB;
  864. break;
  865. default:
  866. mf->code = V4L2_MBUS_FMT_UYVY8_2X8;
  867. case V4L2_MBUS_FMT_UYVY8_2X8:
  868. mf->colorspace = V4L2_COLORSPACE_JPEG;
  869. }
  870. ret = ov2640_set_params(client, &mf->width, &mf->height, mf->code);
  871. return ret;
  872. }
  873. static int ov2640_try_fmt(struct v4l2_subdev *sd,
  874. struct v4l2_mbus_framefmt *mf)
  875. {
  876. const struct ov2640_win_size *win;
  877. /*
  878. * select suitable win
  879. */
  880. win = ov2640_select_win(&mf->width, &mf->height);
  881. mf->field = V4L2_FIELD_NONE;
  882. switch (mf->code) {
  883. case V4L2_MBUS_FMT_RGB565_2X8_LE:
  884. mf->colorspace = V4L2_COLORSPACE_SRGB;
  885. break;
  886. default:
  887. mf->code = V4L2_MBUS_FMT_UYVY8_2X8;
  888. case V4L2_MBUS_FMT_UYVY8_2X8:
  889. mf->colorspace = V4L2_COLORSPACE_JPEG;
  890. }
  891. return 0;
  892. }
  893. static int ov2640_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
  894. enum v4l2_mbus_pixelcode *code)
  895. {
  896. if (index >= ARRAY_SIZE(ov2640_codes))
  897. return -EINVAL;
  898. *code = ov2640_codes[index];
  899. return 0;
  900. }
  901. static int ov2640_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  902. {
  903. a->c.left = 0;
  904. a->c.top = 0;
  905. a->c.width = W_UXGA;
  906. a->c.height = H_UXGA;
  907. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  908. return 0;
  909. }
  910. static int ov2640_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
  911. {
  912. a->bounds.left = 0;
  913. a->bounds.top = 0;
  914. a->bounds.width = W_UXGA;
  915. a->bounds.height = H_UXGA;
  916. a->defrect = a->bounds;
  917. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  918. a->pixelaspect.numerator = 1;
  919. a->pixelaspect.denominator = 1;
  920. return 0;
  921. }
  922. static int ov2640_video_probe(struct soc_camera_device *icd,
  923. struct i2c_client *client)
  924. {
  925. struct ov2640_priv *priv = to_ov2640(client);
  926. u8 pid, ver, midh, midl;
  927. const char *devname;
  928. int ret;
  929. /*
  930. * we must have a parent by now. And it cannot be a wrong one.
  931. * So this entire test is completely redundant.
  932. */
  933. if (!icd->dev.parent ||
  934. to_soc_camera_host(icd->dev.parent)->nr != icd->iface) {
  935. dev_err(&client->dev, "Parent missing or invalid!\n");
  936. ret = -ENODEV;
  937. goto err;
  938. }
  939. /*
  940. * check and show product ID and manufacturer ID
  941. */
  942. i2c_smbus_write_byte_data(client, BANK_SEL, BANK_SEL_SENS);
  943. pid = i2c_smbus_read_byte_data(client, PID);
  944. ver = i2c_smbus_read_byte_data(client, VER);
  945. midh = i2c_smbus_read_byte_data(client, MIDH);
  946. midl = i2c_smbus_read_byte_data(client, MIDL);
  947. switch (VERSION(pid, ver)) {
  948. case PID_OV2640:
  949. devname = "ov2640";
  950. priv->model = V4L2_IDENT_OV2640;
  951. break;
  952. default:
  953. dev_err(&client->dev,
  954. "Product ID error %x:%x\n", pid, ver);
  955. ret = -ENODEV;
  956. goto err;
  957. }
  958. dev_info(&client->dev,
  959. "%s Product ID %0x:%0x Manufacturer ID %x:%x\n",
  960. devname, pid, ver, midh, midl);
  961. return 0;
  962. err:
  963. return ret;
  964. }
  965. static struct soc_camera_ops ov2640_ops = {
  966. .set_bus_param = ov2640_set_bus_param,
  967. .query_bus_param = ov2640_query_bus_param,
  968. .controls = ov2640_controls,
  969. .num_controls = ARRAY_SIZE(ov2640_controls),
  970. };
  971. static struct v4l2_subdev_core_ops ov2640_subdev_core_ops = {
  972. .g_ctrl = ov2640_g_ctrl,
  973. .s_ctrl = ov2640_s_ctrl,
  974. .g_chip_ident = ov2640_g_chip_ident,
  975. #ifdef CONFIG_VIDEO_ADV_DEBUG
  976. .g_register = ov2640_g_register,
  977. .s_register = ov2640_s_register,
  978. #endif
  979. };
  980. static struct v4l2_subdev_video_ops ov2640_subdev_video_ops = {
  981. .s_stream = ov2640_s_stream,
  982. .g_mbus_fmt = ov2640_g_fmt,
  983. .s_mbus_fmt = ov2640_s_fmt,
  984. .try_mbus_fmt = ov2640_try_fmt,
  985. .cropcap = ov2640_cropcap,
  986. .g_crop = ov2640_g_crop,
  987. .enum_mbus_fmt = ov2640_enum_fmt,
  988. };
  989. static struct v4l2_subdev_ops ov2640_subdev_ops = {
  990. .core = &ov2640_subdev_core_ops,
  991. .video = &ov2640_subdev_video_ops,
  992. };
  993. /*
  994. * i2c_driver functions
  995. */
  996. static int ov2640_probe(struct i2c_client *client,
  997. const struct i2c_device_id *did)
  998. {
  999. struct ov2640_priv *priv;
  1000. struct soc_camera_device *icd = client->dev.platform_data;
  1001. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  1002. struct soc_camera_link *icl;
  1003. int ret;
  1004. if (!icd) {
  1005. dev_err(&adapter->dev, "OV2640: missing soc-camera data!\n");
  1006. return -EINVAL;
  1007. }
  1008. icl = to_soc_camera_link(icd);
  1009. if (!icl) {
  1010. dev_err(&adapter->dev,
  1011. "OV2640: Missing platform_data for driver\n");
  1012. return -EINVAL;
  1013. }
  1014. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  1015. dev_err(&adapter->dev,
  1016. "OV2640: I2C-Adapter doesn't support SMBUS\n");
  1017. return -EIO;
  1018. }
  1019. priv = kzalloc(sizeof(struct ov2640_priv), GFP_KERNEL);
  1020. if (!priv) {
  1021. dev_err(&adapter->dev,
  1022. "Failed to allocate memory for private data!\n");
  1023. return -ENOMEM;
  1024. }
  1025. priv->info = icl->priv;
  1026. v4l2_i2c_subdev_init(&priv->subdev, client, &ov2640_subdev_ops);
  1027. icd->ops = &ov2640_ops;
  1028. ret = ov2640_video_probe(icd, client);
  1029. if (ret) {
  1030. icd->ops = NULL;
  1031. kfree(priv);
  1032. } else {
  1033. dev_info(&adapter->dev, "OV2640 Probed\n");
  1034. }
  1035. return ret;
  1036. }
  1037. static int ov2640_remove(struct i2c_client *client)
  1038. {
  1039. struct ov2640_priv *priv = to_ov2640(client);
  1040. struct soc_camera_device *icd = client->dev.platform_data;
  1041. icd->ops = NULL;
  1042. kfree(priv);
  1043. return 0;
  1044. }
  1045. static const struct i2c_device_id ov2640_id[] = {
  1046. { "ov2640", 0 },
  1047. { }
  1048. };
  1049. MODULE_DEVICE_TABLE(i2c, ov2640_id);
  1050. static struct i2c_driver ov2640_i2c_driver = {
  1051. .driver = {
  1052. .name = "ov2640",
  1053. },
  1054. .probe = ov2640_probe,
  1055. .remove = ov2640_remove,
  1056. .id_table = ov2640_id,
  1057. };
  1058. /*
  1059. * Module functions
  1060. */
  1061. static int __init ov2640_module_init(void)
  1062. {
  1063. return i2c_add_driver(&ov2640_i2c_driver);
  1064. }
  1065. static void __exit ov2640_module_exit(void)
  1066. {
  1067. i2c_del_driver(&ov2640_i2c_driver);
  1068. }
  1069. module_init(ov2640_module_init);
  1070. module_exit(ov2640_module_exit);
  1071. MODULE_DESCRIPTION("SoC Camera driver for Omni Vision 2640 sensor");
  1072. MODULE_AUTHOR("Alberto Panizzo");
  1073. MODULE_LICENSE("GPL v2");