ov7670.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295
  1. /*
  2. * A V4L2 driver for OmniVision OV7670 cameras.
  3. *
  4. * Copyright 2006 One Laptop Per Child Association, Inc. Written
  5. * by Jonathan Corbet with substantial inspiration from Mark
  6. * McClelland's ovcamchip code.
  7. *
  8. * Copyright 2006-7 Jonathan Corbet <corbet@lwn.net>
  9. *
  10. * This file may be distributed under the terms of the GNU General
  11. * Public License, version 2.
  12. */
  13. #include <linux/init.h>
  14. #include <linux/module.h>
  15. #include <linux/i2c.h>
  16. #include <linux/delay.h>
  17. #include <linux/videodev2.h>
  18. #include <media/v4l2-device.h>
  19. #include <media/v4l2-chip-ident.h>
  20. #include <media/v4l2-i2c-drv.h>
  21. MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
  22. MODULE_DESCRIPTION("A low-level driver for OmniVision ov7670 sensors");
  23. MODULE_LICENSE("GPL");
  24. static int debug;
  25. module_param(debug, bool, 0644);
  26. MODULE_PARM_DESC(debug, "Debug level (0-1)");
  27. /*
  28. * Basic window sizes. These probably belong somewhere more globally
  29. * useful.
  30. */
  31. #define VGA_WIDTH 640
  32. #define VGA_HEIGHT 480
  33. #define QVGA_WIDTH 320
  34. #define QVGA_HEIGHT 240
  35. #define CIF_WIDTH 352
  36. #define CIF_HEIGHT 288
  37. #define QCIF_WIDTH 176
  38. #define QCIF_HEIGHT 144
  39. /*
  40. * Our nominal (default) frame rate.
  41. */
  42. #define OV7670_FRAME_RATE 30
  43. /*
  44. * The 7670 sits on i2c with ID 0x42
  45. */
  46. #define OV7670_I2C_ADDR 0x42
  47. /* Registers */
  48. #define REG_GAIN 0x00 /* Gain lower 8 bits (rest in vref) */
  49. #define REG_BLUE 0x01 /* blue gain */
  50. #define REG_RED 0x02 /* red gain */
  51. #define REG_VREF 0x03 /* Pieces of GAIN, VSTART, VSTOP */
  52. #define REG_COM1 0x04 /* Control 1 */
  53. #define COM1_CCIR656 0x40 /* CCIR656 enable */
  54. #define REG_BAVE 0x05 /* U/B Average level */
  55. #define REG_GbAVE 0x06 /* Y/Gb Average level */
  56. #define REG_AECHH 0x07 /* AEC MS 5 bits */
  57. #define REG_RAVE 0x08 /* V/R Average level */
  58. #define REG_COM2 0x09 /* Control 2 */
  59. #define COM2_SSLEEP 0x10 /* Soft sleep mode */
  60. #define REG_PID 0x0a /* Product ID MSB */
  61. #define REG_VER 0x0b /* Product ID LSB */
  62. #define REG_COM3 0x0c /* Control 3 */
  63. #define COM3_SWAP 0x40 /* Byte swap */
  64. #define COM3_SCALEEN 0x08 /* Enable scaling */
  65. #define COM3_DCWEN 0x04 /* Enable downsamp/crop/window */
  66. #define REG_COM4 0x0d /* Control 4 */
  67. #define REG_COM5 0x0e /* All "reserved" */
  68. #define REG_COM6 0x0f /* Control 6 */
  69. #define REG_AECH 0x10 /* More bits of AEC value */
  70. #define REG_CLKRC 0x11 /* Clocl control */
  71. #define CLK_EXT 0x40 /* Use external clock directly */
  72. #define CLK_SCALE 0x3f /* Mask for internal clock scale */
  73. #define REG_COM7 0x12 /* Control 7 */
  74. #define COM7_RESET 0x80 /* Register reset */
  75. #define COM7_FMT_MASK 0x38
  76. #define COM7_FMT_VGA 0x00
  77. #define COM7_FMT_CIF 0x20 /* CIF format */
  78. #define COM7_FMT_QVGA 0x10 /* QVGA format */
  79. #define COM7_FMT_QCIF 0x08 /* QCIF format */
  80. #define COM7_RGB 0x04 /* bits 0 and 2 - RGB format */
  81. #define COM7_YUV 0x00 /* YUV */
  82. #define COM7_BAYER 0x01 /* Bayer format */
  83. #define COM7_PBAYER 0x05 /* "Processed bayer" */
  84. #define REG_COM8 0x13 /* Control 8 */
  85. #define COM8_FASTAEC 0x80 /* Enable fast AGC/AEC */
  86. #define COM8_AECSTEP 0x40 /* Unlimited AEC step size */
  87. #define COM8_BFILT 0x20 /* Band filter enable */
  88. #define COM8_AGC 0x04 /* Auto gain enable */
  89. #define COM8_AWB 0x02 /* White balance enable */
  90. #define COM8_AEC 0x01 /* Auto exposure enable */
  91. #define REG_COM9 0x14 /* Control 9 - gain ceiling */
  92. #define REG_COM10 0x15 /* Control 10 */
  93. #define COM10_HSYNC 0x40 /* HSYNC instead of HREF */
  94. #define COM10_PCLK_HB 0x20 /* Suppress PCLK on horiz blank */
  95. #define COM10_HREF_REV 0x08 /* Reverse HREF */
  96. #define COM10_VS_LEAD 0x04 /* VSYNC on clock leading edge */
  97. #define COM10_VS_NEG 0x02 /* VSYNC negative */
  98. #define COM10_HS_NEG 0x01 /* HSYNC negative */
  99. #define REG_HSTART 0x17 /* Horiz start high bits */
  100. #define REG_HSTOP 0x18 /* Horiz stop high bits */
  101. #define REG_VSTART 0x19 /* Vert start high bits */
  102. #define REG_VSTOP 0x1a /* Vert stop high bits */
  103. #define REG_PSHFT 0x1b /* Pixel delay after HREF */
  104. #define REG_MIDH 0x1c /* Manuf. ID high */
  105. #define REG_MIDL 0x1d /* Manuf. ID low */
  106. #define REG_MVFP 0x1e /* Mirror / vflip */
  107. #define MVFP_MIRROR 0x20 /* Mirror image */
  108. #define MVFP_FLIP 0x10 /* Vertical flip */
  109. #define REG_AEW 0x24 /* AGC upper limit */
  110. #define REG_AEB 0x25 /* AGC lower limit */
  111. #define REG_VPT 0x26 /* AGC/AEC fast mode op region */
  112. #define REG_HSYST 0x30 /* HSYNC rising edge delay */
  113. #define REG_HSYEN 0x31 /* HSYNC falling edge delay */
  114. #define REG_HREF 0x32 /* HREF pieces */
  115. #define REG_TSLB 0x3a /* lots of stuff */
  116. #define TSLB_YLAST 0x04 /* UYVY or VYUY - see com13 */
  117. #define REG_COM11 0x3b /* Control 11 */
  118. #define COM11_NIGHT 0x80 /* NIght mode enable */
  119. #define COM11_NMFR 0x60 /* Two bit NM frame rate */
  120. #define COM11_HZAUTO 0x10 /* Auto detect 50/60 Hz */
  121. #define COM11_50HZ 0x08 /* Manual 50Hz select */
  122. #define COM11_EXP 0x02
  123. #define REG_COM12 0x3c /* Control 12 */
  124. #define COM12_HREF 0x80 /* HREF always */
  125. #define REG_COM13 0x3d /* Control 13 */
  126. #define COM13_GAMMA 0x80 /* Gamma enable */
  127. #define COM13_UVSAT 0x40 /* UV saturation auto adjustment */
  128. #define COM13_UVSWAP 0x01 /* V before U - w/TSLB */
  129. #define REG_COM14 0x3e /* Control 14 */
  130. #define COM14_DCWEN 0x10 /* DCW/PCLK-scale enable */
  131. #define REG_EDGE 0x3f /* Edge enhancement factor */
  132. #define REG_COM15 0x40 /* Control 15 */
  133. #define COM15_R10F0 0x00 /* Data range 10 to F0 */
  134. #define COM15_R01FE 0x80 /* 01 to FE */
  135. #define COM15_R00FF 0xc0 /* 00 to FF */
  136. #define COM15_RGB565 0x10 /* RGB565 output */
  137. #define COM15_RGB555 0x30 /* RGB555 output */
  138. #define REG_COM16 0x41 /* Control 16 */
  139. #define COM16_AWBGAIN 0x08 /* AWB gain enable */
  140. #define REG_COM17 0x42 /* Control 17 */
  141. #define COM17_AECWIN 0xc0 /* AEC window - must match COM4 */
  142. #define COM17_CBAR 0x08 /* DSP Color bar */
  143. /*
  144. * This matrix defines how the colors are generated, must be
  145. * tweaked to adjust hue and saturation.
  146. *
  147. * Order: v-red, v-green, v-blue, u-red, u-green, u-blue
  148. *
  149. * They are nine-bit signed quantities, with the sign bit
  150. * stored in 0x58. Sign for v-red is bit 0, and up from there.
  151. */
  152. #define REG_CMATRIX_BASE 0x4f
  153. #define CMATRIX_LEN 6
  154. #define REG_CMATRIX_SIGN 0x58
  155. #define REG_BRIGHT 0x55 /* Brightness */
  156. #define REG_CONTRAS 0x56 /* Contrast control */
  157. #define REG_GFIX 0x69 /* Fix gain control */
  158. #define REG_REG76 0x76 /* OV's name */
  159. #define R76_BLKPCOR 0x80 /* Black pixel correction enable */
  160. #define R76_WHTPCOR 0x40 /* White pixel correction enable */
  161. #define REG_RGB444 0x8c /* RGB 444 control */
  162. #define R444_ENABLE 0x02 /* Turn on RGB444, overrides 5x5 */
  163. #define R444_RGBX 0x01 /* Empty nibble at end */
  164. #define REG_HAECC1 0x9f /* Hist AEC/AGC control 1 */
  165. #define REG_HAECC2 0xa0 /* Hist AEC/AGC control 2 */
  166. #define REG_BD50MAX 0xa5 /* 50hz banding step limit */
  167. #define REG_HAECC3 0xa6 /* Hist AEC/AGC control 3 */
  168. #define REG_HAECC4 0xa7 /* Hist AEC/AGC control 4 */
  169. #define REG_HAECC5 0xa8 /* Hist AEC/AGC control 5 */
  170. #define REG_HAECC6 0xa9 /* Hist AEC/AGC control 6 */
  171. #define REG_HAECC7 0xaa /* Hist AEC/AGC control 7 */
  172. #define REG_BD60MAX 0xab /* 60hz banding step limit */
  173. /*
  174. * Information we maintain about a known sensor.
  175. */
  176. struct ov7670_format_struct; /* coming later */
  177. struct ov7670_info {
  178. struct v4l2_subdev sd;
  179. struct ov7670_format_struct *fmt; /* Current format */
  180. unsigned char sat; /* Saturation value */
  181. int hue; /* Hue value */
  182. };
  183. static inline struct ov7670_info *to_state(struct v4l2_subdev *sd)
  184. {
  185. return container_of(sd, struct ov7670_info, sd);
  186. }
  187. /*
  188. * The default register settings, as obtained from OmniVision. There
  189. * is really no making sense of most of these - lots of "reserved" values
  190. * and such.
  191. *
  192. * These settings give VGA YUYV.
  193. */
  194. struct regval_list {
  195. unsigned char reg_num;
  196. unsigned char value;
  197. };
  198. static struct regval_list ov7670_default_regs[] = {
  199. { REG_COM7, COM7_RESET },
  200. /*
  201. * Clock scale: 3 = 15fps
  202. * 2 = 20fps
  203. * 1 = 30fps
  204. */
  205. { REG_CLKRC, 0x1 }, /* OV: clock scale (30 fps) */
  206. { REG_TSLB, 0x04 }, /* OV */
  207. { REG_COM7, 0 }, /* VGA */
  208. /*
  209. * Set the hardware window. These values from OV don't entirely
  210. * make sense - hstop is less than hstart. But they work...
  211. */
  212. { REG_HSTART, 0x13 }, { REG_HSTOP, 0x01 },
  213. { REG_HREF, 0xb6 }, { REG_VSTART, 0x02 },
  214. { REG_VSTOP, 0x7a }, { REG_VREF, 0x0a },
  215. { REG_COM3, 0 }, { REG_COM14, 0 },
  216. /* Mystery scaling numbers */
  217. { 0x70, 0x3a }, { 0x71, 0x35 },
  218. { 0x72, 0x11 }, { 0x73, 0xf0 },
  219. { 0xa2, 0x02 }, { REG_COM10, 0x0 },
  220. /* Gamma curve values */
  221. { 0x7a, 0x20 }, { 0x7b, 0x10 },
  222. { 0x7c, 0x1e }, { 0x7d, 0x35 },
  223. { 0x7e, 0x5a }, { 0x7f, 0x69 },
  224. { 0x80, 0x76 }, { 0x81, 0x80 },
  225. { 0x82, 0x88 }, { 0x83, 0x8f },
  226. { 0x84, 0x96 }, { 0x85, 0xa3 },
  227. { 0x86, 0xaf }, { 0x87, 0xc4 },
  228. { 0x88, 0xd7 }, { 0x89, 0xe8 },
  229. /* AGC and AEC parameters. Note we start by disabling those features,
  230. then turn them only after tweaking the values. */
  231. { REG_COM8, COM8_FASTAEC | COM8_AECSTEP | COM8_BFILT },
  232. { REG_GAIN, 0 }, { REG_AECH, 0 },
  233. { REG_COM4, 0x40 }, /* magic reserved bit */
  234. { REG_COM9, 0x18 }, /* 4x gain + magic rsvd bit */
  235. { REG_BD50MAX, 0x05 }, { REG_BD60MAX, 0x07 },
  236. { REG_AEW, 0x95 }, { REG_AEB, 0x33 },
  237. { REG_VPT, 0xe3 }, { REG_HAECC1, 0x78 },
  238. { REG_HAECC2, 0x68 }, { 0xa1, 0x03 }, /* magic */
  239. { REG_HAECC3, 0xd8 }, { REG_HAECC4, 0xd8 },
  240. { REG_HAECC5, 0xf0 }, { REG_HAECC6, 0x90 },
  241. { REG_HAECC7, 0x94 },
  242. { REG_COM8, COM8_FASTAEC|COM8_AECSTEP|COM8_BFILT|COM8_AGC|COM8_AEC },
  243. /* Almost all of these are magic "reserved" values. */
  244. { REG_COM5, 0x61 }, { REG_COM6, 0x4b },
  245. { 0x16, 0x02 }, { REG_MVFP, 0x07 },
  246. { 0x21, 0x02 }, { 0x22, 0x91 },
  247. { 0x29, 0x07 }, { 0x33, 0x0b },
  248. { 0x35, 0x0b }, { 0x37, 0x1d },
  249. { 0x38, 0x71 }, { 0x39, 0x2a },
  250. { REG_COM12, 0x78 }, { 0x4d, 0x40 },
  251. { 0x4e, 0x20 }, { REG_GFIX, 0 },
  252. { 0x6b, 0x4a }, { 0x74, 0x10 },
  253. { 0x8d, 0x4f }, { 0x8e, 0 },
  254. { 0x8f, 0 }, { 0x90, 0 },
  255. { 0x91, 0 }, { 0x96, 0 },
  256. { 0x9a, 0 }, { 0xb0, 0x84 },
  257. { 0xb1, 0x0c }, { 0xb2, 0x0e },
  258. { 0xb3, 0x82 }, { 0xb8, 0x0a },
  259. /* More reserved magic, some of which tweaks white balance */
  260. { 0x43, 0x0a }, { 0x44, 0xf0 },
  261. { 0x45, 0x34 }, { 0x46, 0x58 },
  262. { 0x47, 0x28 }, { 0x48, 0x3a },
  263. { 0x59, 0x88 }, { 0x5a, 0x88 },
  264. { 0x5b, 0x44 }, { 0x5c, 0x67 },
  265. { 0x5d, 0x49 }, { 0x5e, 0x0e },
  266. { 0x6c, 0x0a }, { 0x6d, 0x55 },
  267. { 0x6e, 0x11 }, { 0x6f, 0x9f }, /* "9e for advance AWB" */
  268. { 0x6a, 0x40 }, { REG_BLUE, 0x40 },
  269. { REG_RED, 0x60 },
  270. { REG_COM8, COM8_FASTAEC|COM8_AECSTEP|COM8_BFILT|COM8_AGC|COM8_AEC|COM8_AWB },
  271. /* Matrix coefficients */
  272. { 0x4f, 0x80 }, { 0x50, 0x80 },
  273. { 0x51, 0 }, { 0x52, 0x22 },
  274. { 0x53, 0x5e }, { 0x54, 0x80 },
  275. { 0x58, 0x9e },
  276. { REG_COM16, COM16_AWBGAIN }, { REG_EDGE, 0 },
  277. { 0x75, 0x05 }, { 0x76, 0xe1 },
  278. { 0x4c, 0 }, { 0x77, 0x01 },
  279. { REG_COM13, 0xc3 }, { 0x4b, 0x09 },
  280. { 0xc9, 0x60 }, { REG_COM16, 0x38 },
  281. { 0x56, 0x40 },
  282. { 0x34, 0x11 }, { REG_COM11, COM11_EXP|COM11_HZAUTO },
  283. { 0xa4, 0x88 }, { 0x96, 0 },
  284. { 0x97, 0x30 }, { 0x98, 0x20 },
  285. { 0x99, 0x30 }, { 0x9a, 0x84 },
  286. { 0x9b, 0x29 }, { 0x9c, 0x03 },
  287. { 0x9d, 0x4c }, { 0x9e, 0x3f },
  288. { 0x78, 0x04 },
  289. /* Extra-weird stuff. Some sort of multiplexor register */
  290. { 0x79, 0x01 }, { 0xc8, 0xf0 },
  291. { 0x79, 0x0f }, { 0xc8, 0x00 },
  292. { 0x79, 0x10 }, { 0xc8, 0x7e },
  293. { 0x79, 0x0a }, { 0xc8, 0x80 },
  294. { 0x79, 0x0b }, { 0xc8, 0x01 },
  295. { 0x79, 0x0c }, { 0xc8, 0x0f },
  296. { 0x79, 0x0d }, { 0xc8, 0x20 },
  297. { 0x79, 0x09 }, { 0xc8, 0x80 },
  298. { 0x79, 0x02 }, { 0xc8, 0xc0 },
  299. { 0x79, 0x03 }, { 0xc8, 0x40 },
  300. { 0x79, 0x05 }, { 0xc8, 0x30 },
  301. { 0x79, 0x26 },
  302. { 0xff, 0xff }, /* END MARKER */
  303. };
  304. /*
  305. * Here we'll try to encapsulate the changes for just the output
  306. * video format.
  307. *
  308. * RGB656 and YUV422 come from OV; RGB444 is homebrewed.
  309. *
  310. * IMPORTANT RULE: the first entry must be for COM7, see ov7670_s_fmt for why.
  311. */
  312. static struct regval_list ov7670_fmt_yuv422[] = {
  313. { REG_COM7, 0x0 }, /* Selects YUV mode */
  314. { REG_RGB444, 0 }, /* No RGB444 please */
  315. { REG_COM1, 0 },
  316. { REG_COM15, COM15_R00FF },
  317. { REG_COM9, 0x18 }, /* 4x gain ceiling; 0x8 is reserved bit */
  318. { 0x4f, 0x80 }, /* "matrix coefficient 1" */
  319. { 0x50, 0x80 }, /* "matrix coefficient 2" */
  320. { 0x51, 0 }, /* vb */
  321. { 0x52, 0x22 }, /* "matrix coefficient 4" */
  322. { 0x53, 0x5e }, /* "matrix coefficient 5" */
  323. { 0x54, 0x80 }, /* "matrix coefficient 6" */
  324. { REG_COM13, COM13_GAMMA|COM13_UVSAT },
  325. { 0xff, 0xff },
  326. };
  327. static struct regval_list ov7670_fmt_rgb565[] = {
  328. { REG_COM7, COM7_RGB }, /* Selects RGB mode */
  329. { REG_RGB444, 0 }, /* No RGB444 please */
  330. { REG_COM1, 0x0 },
  331. { REG_COM15, COM15_RGB565 },
  332. { REG_COM9, 0x38 }, /* 16x gain ceiling; 0x8 is reserved bit */
  333. { 0x4f, 0xb3 }, /* "matrix coefficient 1" */
  334. { 0x50, 0xb3 }, /* "matrix coefficient 2" */
  335. { 0x51, 0 }, /* vb */
  336. { 0x52, 0x3d }, /* "matrix coefficient 4" */
  337. { 0x53, 0xa7 }, /* "matrix coefficient 5" */
  338. { 0x54, 0xe4 }, /* "matrix coefficient 6" */
  339. { REG_COM13, COM13_GAMMA|COM13_UVSAT },
  340. { 0xff, 0xff },
  341. };
  342. static struct regval_list ov7670_fmt_rgb444[] = {
  343. { REG_COM7, COM7_RGB }, /* Selects RGB mode */
  344. { REG_RGB444, R444_ENABLE }, /* Enable xxxxrrrr ggggbbbb */
  345. { REG_COM1, 0x40 }, /* Magic reserved bit */
  346. { REG_COM15, COM15_R01FE|COM15_RGB565 }, /* Data range needed? */
  347. { REG_COM9, 0x38 }, /* 16x gain ceiling; 0x8 is reserved bit */
  348. { 0x4f, 0xb3 }, /* "matrix coefficient 1" */
  349. { 0x50, 0xb3 }, /* "matrix coefficient 2" */
  350. { 0x51, 0 }, /* vb */
  351. { 0x52, 0x3d }, /* "matrix coefficient 4" */
  352. { 0x53, 0xa7 }, /* "matrix coefficient 5" */
  353. { 0x54, 0xe4 }, /* "matrix coefficient 6" */
  354. { REG_COM13, COM13_GAMMA|COM13_UVSAT|0x2 }, /* Magic rsvd bit */
  355. { 0xff, 0xff },
  356. };
  357. static struct regval_list ov7670_fmt_raw[] = {
  358. { REG_COM7, COM7_BAYER },
  359. { REG_COM13, 0x08 }, /* No gamma, magic rsvd bit */
  360. { REG_COM16, 0x3d }, /* Edge enhancement, denoise */
  361. { REG_REG76, 0xe1 }, /* Pix correction, magic rsvd */
  362. { 0xff, 0xff },
  363. };
  364. /*
  365. * Low-level register I/O.
  366. */
  367. static int ov7670_read(struct v4l2_subdev *sd, unsigned char reg,
  368. unsigned char *value)
  369. {
  370. struct i2c_client *client = v4l2_get_subdevdata(sd);
  371. int ret;
  372. ret = i2c_smbus_read_byte_data(client, reg);
  373. if (ret >= 0) {
  374. *value = (unsigned char)ret;
  375. ret = 0;
  376. }
  377. return ret;
  378. }
  379. static int ov7670_write(struct v4l2_subdev *sd, unsigned char reg,
  380. unsigned char value)
  381. {
  382. struct i2c_client *client = v4l2_get_subdevdata(sd);
  383. int ret = i2c_smbus_write_byte_data(client, reg, value);
  384. if (reg == REG_COM7 && (value & COM7_RESET))
  385. msleep(2); /* Wait for reset to run */
  386. return ret;
  387. }
  388. /*
  389. * Write a list of register settings; ff/ff stops the process.
  390. */
  391. static int ov7670_write_array(struct v4l2_subdev *sd, struct regval_list *vals)
  392. {
  393. while (vals->reg_num != 0xff || vals->value != 0xff) {
  394. int ret = ov7670_write(sd, vals->reg_num, vals->value);
  395. if (ret < 0)
  396. return ret;
  397. vals++;
  398. }
  399. return 0;
  400. }
  401. /*
  402. * Stuff that knows about the sensor.
  403. */
  404. static int ov7670_reset(struct v4l2_subdev *sd, u32 val)
  405. {
  406. ov7670_write(sd, REG_COM7, COM7_RESET);
  407. msleep(1);
  408. return 0;
  409. }
  410. static int ov7670_init(struct v4l2_subdev *sd, u32 val)
  411. {
  412. return ov7670_write_array(sd, ov7670_default_regs);
  413. }
  414. static int ov7670_detect(struct v4l2_subdev *sd)
  415. {
  416. unsigned char v;
  417. int ret;
  418. ret = ov7670_init(sd, 0);
  419. if (ret < 0)
  420. return ret;
  421. ret = ov7670_read(sd, REG_MIDH, &v);
  422. if (ret < 0)
  423. return ret;
  424. if (v != 0x7f) /* OV manuf. id. */
  425. return -ENODEV;
  426. ret = ov7670_read(sd, REG_MIDL, &v);
  427. if (ret < 0)
  428. return ret;
  429. if (v != 0xa2)
  430. return -ENODEV;
  431. /*
  432. * OK, we know we have an OmniVision chip...but which one?
  433. */
  434. ret = ov7670_read(sd, REG_PID, &v);
  435. if (ret < 0)
  436. return ret;
  437. if (v != 0x76) /* PID + VER = 0x76 / 0x73 */
  438. return -ENODEV;
  439. ret = ov7670_read(sd, REG_VER, &v);
  440. if (ret < 0)
  441. return ret;
  442. if (v != 0x73) /* PID + VER = 0x76 / 0x73 */
  443. return -ENODEV;
  444. return 0;
  445. }
  446. /*
  447. * Store information about the video data format. The color matrix
  448. * is deeply tied into the format, so keep the relevant values here.
  449. * The magic matrix nubmers come from OmniVision.
  450. */
  451. static struct ov7670_format_struct {
  452. __u8 *desc;
  453. __u32 pixelformat;
  454. struct regval_list *regs;
  455. int cmatrix[CMATRIX_LEN];
  456. int bpp; /* Bytes per pixel */
  457. } ov7670_formats[] = {
  458. {
  459. .desc = "YUYV 4:2:2",
  460. .pixelformat = V4L2_PIX_FMT_YUYV,
  461. .regs = ov7670_fmt_yuv422,
  462. .cmatrix = { 128, -128, 0, -34, -94, 128 },
  463. .bpp = 2,
  464. },
  465. {
  466. .desc = "RGB 444",
  467. .pixelformat = V4L2_PIX_FMT_RGB444,
  468. .regs = ov7670_fmt_rgb444,
  469. .cmatrix = { 179, -179, 0, -61, -176, 228 },
  470. .bpp = 2,
  471. },
  472. {
  473. .desc = "RGB 565",
  474. .pixelformat = V4L2_PIX_FMT_RGB565,
  475. .regs = ov7670_fmt_rgb565,
  476. .cmatrix = { 179, -179, 0, -61, -176, 228 },
  477. .bpp = 2,
  478. },
  479. {
  480. .desc = "Raw RGB Bayer",
  481. .pixelformat = V4L2_PIX_FMT_SBGGR8,
  482. .regs = ov7670_fmt_raw,
  483. .cmatrix = { 0, 0, 0, 0, 0, 0 },
  484. .bpp = 1
  485. },
  486. };
  487. #define N_OV7670_FMTS ARRAY_SIZE(ov7670_formats)
  488. /*
  489. * Then there is the issue of window sizes. Try to capture the info here.
  490. */
  491. /*
  492. * QCIF mode is done (by OV) in a very strange way - it actually looks like
  493. * VGA with weird scaling options - they do *not* use the canned QCIF mode
  494. * which is allegedly provided by the sensor. So here's the weird register
  495. * settings.
  496. */
  497. static struct regval_list ov7670_qcif_regs[] = {
  498. { REG_COM3, COM3_SCALEEN|COM3_DCWEN },
  499. { REG_COM3, COM3_DCWEN },
  500. { REG_COM14, COM14_DCWEN | 0x01},
  501. { 0x73, 0xf1 },
  502. { 0xa2, 0x52 },
  503. { 0x7b, 0x1c },
  504. { 0x7c, 0x28 },
  505. { 0x7d, 0x3c },
  506. { 0x7f, 0x69 },
  507. { REG_COM9, 0x38 },
  508. { 0xa1, 0x0b },
  509. { 0x74, 0x19 },
  510. { 0x9a, 0x80 },
  511. { 0x43, 0x14 },
  512. { REG_COM13, 0xc0 },
  513. { 0xff, 0xff },
  514. };
  515. static struct ov7670_win_size {
  516. int width;
  517. int height;
  518. unsigned char com7_bit;
  519. int hstart; /* Start/stop values for the camera. Note */
  520. int hstop; /* that they do not always make complete */
  521. int vstart; /* sense to humans, but evidently the sensor */
  522. int vstop; /* will do the right thing... */
  523. struct regval_list *regs; /* Regs to tweak */
  524. /* h/vref stuff */
  525. } ov7670_win_sizes[] = {
  526. /* VGA */
  527. {
  528. .width = VGA_WIDTH,
  529. .height = VGA_HEIGHT,
  530. .com7_bit = COM7_FMT_VGA,
  531. .hstart = 158, /* These values from */
  532. .hstop = 14, /* Omnivision */
  533. .vstart = 10,
  534. .vstop = 490,
  535. .regs = NULL,
  536. },
  537. /* CIF */
  538. {
  539. .width = CIF_WIDTH,
  540. .height = CIF_HEIGHT,
  541. .com7_bit = COM7_FMT_CIF,
  542. .hstart = 170, /* Empirically determined */
  543. .hstop = 90,
  544. .vstart = 14,
  545. .vstop = 494,
  546. .regs = NULL,
  547. },
  548. /* QVGA */
  549. {
  550. .width = QVGA_WIDTH,
  551. .height = QVGA_HEIGHT,
  552. .com7_bit = COM7_FMT_QVGA,
  553. .hstart = 164, /* Empirically determined */
  554. .hstop = 20,
  555. .vstart = 14,
  556. .vstop = 494,
  557. .regs = NULL,
  558. },
  559. /* QCIF */
  560. {
  561. .width = QCIF_WIDTH,
  562. .height = QCIF_HEIGHT,
  563. .com7_bit = COM7_FMT_VGA, /* see comment above */
  564. .hstart = 456, /* Empirically determined */
  565. .hstop = 24,
  566. .vstart = 14,
  567. .vstop = 494,
  568. .regs = ov7670_qcif_regs,
  569. },
  570. };
  571. #define N_WIN_SIZES (ARRAY_SIZE(ov7670_win_sizes))
  572. /*
  573. * Store a set of start/stop values into the camera.
  574. */
  575. static int ov7670_set_hw(struct v4l2_subdev *sd, int hstart, int hstop,
  576. int vstart, int vstop)
  577. {
  578. int ret;
  579. unsigned char v;
  580. /*
  581. * Horizontal: 11 bits, top 8 live in hstart and hstop. Bottom 3 of
  582. * hstart are in href[2:0], bottom 3 of hstop in href[5:3]. There is
  583. * a mystery "edge offset" value in the top two bits of href.
  584. */
  585. ret = ov7670_write(sd, REG_HSTART, (hstart >> 3) & 0xff);
  586. ret += ov7670_write(sd, REG_HSTOP, (hstop >> 3) & 0xff);
  587. ret += ov7670_read(sd, REG_HREF, &v);
  588. v = (v & 0xc0) | ((hstop & 0x7) << 3) | (hstart & 0x7);
  589. msleep(10);
  590. ret += ov7670_write(sd, REG_HREF, v);
  591. /*
  592. * Vertical: similar arrangement, but only 10 bits.
  593. */
  594. ret += ov7670_write(sd, REG_VSTART, (vstart >> 2) & 0xff);
  595. ret += ov7670_write(sd, REG_VSTOP, (vstop >> 2) & 0xff);
  596. ret += ov7670_read(sd, REG_VREF, &v);
  597. v = (v & 0xf0) | ((vstop & 0x3) << 2) | (vstart & 0x3);
  598. msleep(10);
  599. ret += ov7670_write(sd, REG_VREF, v);
  600. return ret;
  601. }
  602. static int ov7670_enum_fmt(struct v4l2_subdev *sd, struct v4l2_fmtdesc *fmt)
  603. {
  604. struct ov7670_format_struct *ofmt;
  605. if (fmt->index >= N_OV7670_FMTS)
  606. return -EINVAL;
  607. ofmt = ov7670_formats + fmt->index;
  608. fmt->flags = 0;
  609. strcpy(fmt->description, ofmt->desc);
  610. fmt->pixelformat = ofmt->pixelformat;
  611. return 0;
  612. }
  613. static int ov7670_try_fmt_internal(struct v4l2_subdev *sd,
  614. struct v4l2_format *fmt,
  615. struct ov7670_format_struct **ret_fmt,
  616. struct ov7670_win_size **ret_wsize)
  617. {
  618. int index;
  619. struct ov7670_win_size *wsize;
  620. struct v4l2_pix_format *pix = &fmt->fmt.pix;
  621. for (index = 0; index < N_OV7670_FMTS; index++)
  622. if (ov7670_formats[index].pixelformat == pix->pixelformat)
  623. break;
  624. if (index >= N_OV7670_FMTS) {
  625. /* default to first format */
  626. index = 0;
  627. pix->pixelformat = ov7670_formats[0].pixelformat;
  628. }
  629. if (ret_fmt != NULL)
  630. *ret_fmt = ov7670_formats + index;
  631. /*
  632. * Fields: the OV devices claim to be progressive.
  633. */
  634. pix->field = V4L2_FIELD_NONE;
  635. /*
  636. * Round requested image size down to the nearest
  637. * we support, but not below the smallest.
  638. */
  639. for (wsize = ov7670_win_sizes; wsize < ov7670_win_sizes + N_WIN_SIZES;
  640. wsize++)
  641. if (pix->width >= wsize->width && pix->height >= wsize->height)
  642. break;
  643. if (wsize >= ov7670_win_sizes + N_WIN_SIZES)
  644. wsize--; /* Take the smallest one */
  645. if (ret_wsize != NULL)
  646. *ret_wsize = wsize;
  647. /*
  648. * Note the size we'll actually handle.
  649. */
  650. pix->width = wsize->width;
  651. pix->height = wsize->height;
  652. pix->bytesperline = pix->width*ov7670_formats[index].bpp;
  653. pix->sizeimage = pix->height*pix->bytesperline;
  654. return 0;
  655. }
  656. static int ov7670_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *fmt)
  657. {
  658. return ov7670_try_fmt_internal(sd, fmt, NULL, NULL);
  659. }
  660. /*
  661. * Set a format.
  662. */
  663. static int ov7670_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *fmt)
  664. {
  665. int ret;
  666. struct ov7670_format_struct *ovfmt;
  667. struct ov7670_win_size *wsize;
  668. struct ov7670_info *info = to_state(sd);
  669. unsigned char com7, clkrc = 0;
  670. ret = ov7670_try_fmt_internal(sd, fmt, &ovfmt, &wsize);
  671. if (ret)
  672. return ret;
  673. /*
  674. * HACK: if we're running rgb565 we need to grab then rewrite
  675. * CLKRC. If we're *not*, however, then rewriting clkrc hoses
  676. * the colors.
  677. */
  678. if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_RGB565) {
  679. ret = ov7670_read(sd, REG_CLKRC, &clkrc);
  680. if (ret)
  681. return ret;
  682. }
  683. /*
  684. * COM7 is a pain in the ass, it doesn't like to be read then
  685. * quickly written afterward. But we have everything we need
  686. * to set it absolutely here, as long as the format-specific
  687. * register sets list it first.
  688. */
  689. com7 = ovfmt->regs[0].value;
  690. com7 |= wsize->com7_bit;
  691. ov7670_write(sd, REG_COM7, com7);
  692. /*
  693. * Now write the rest of the array. Also store start/stops
  694. */
  695. ov7670_write_array(sd, ovfmt->regs + 1);
  696. ov7670_set_hw(sd, wsize->hstart, wsize->hstop, wsize->vstart,
  697. wsize->vstop);
  698. ret = 0;
  699. if (wsize->regs)
  700. ret = ov7670_write_array(sd, wsize->regs);
  701. info->fmt = ovfmt;
  702. if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_RGB565 && ret == 0)
  703. ret = ov7670_write(sd, REG_CLKRC, clkrc);
  704. return ret;
  705. }
  706. /*
  707. * Implement G/S_PARM. There is a "high quality" mode we could try
  708. * to do someday; for now, we just do the frame rate tweak.
  709. */
  710. static int ov7670_g_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *parms)
  711. {
  712. struct v4l2_captureparm *cp = &parms->parm.capture;
  713. unsigned char clkrc;
  714. int ret;
  715. if (parms->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  716. return -EINVAL;
  717. ret = ov7670_read(sd, REG_CLKRC, &clkrc);
  718. if (ret < 0)
  719. return ret;
  720. memset(cp, 0, sizeof(struct v4l2_captureparm));
  721. cp->capability = V4L2_CAP_TIMEPERFRAME;
  722. cp->timeperframe.numerator = 1;
  723. cp->timeperframe.denominator = OV7670_FRAME_RATE;
  724. if ((clkrc & CLK_EXT) == 0 && (clkrc & CLK_SCALE) > 1)
  725. cp->timeperframe.denominator /= (clkrc & CLK_SCALE);
  726. return 0;
  727. }
  728. static int ov7670_s_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *parms)
  729. {
  730. struct v4l2_captureparm *cp = &parms->parm.capture;
  731. struct v4l2_fract *tpf = &cp->timeperframe;
  732. unsigned char clkrc;
  733. int ret, div;
  734. if (parms->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  735. return -EINVAL;
  736. if (cp->extendedmode != 0)
  737. return -EINVAL;
  738. /*
  739. * CLKRC has a reserved bit, so let's preserve it.
  740. */
  741. ret = ov7670_read(sd, REG_CLKRC, &clkrc);
  742. if (ret < 0)
  743. return ret;
  744. if (tpf->numerator == 0 || tpf->denominator == 0)
  745. div = 1; /* Reset to full rate */
  746. else
  747. div = (tpf->numerator*OV7670_FRAME_RATE)/tpf->denominator;
  748. if (div == 0)
  749. div = 1;
  750. else if (div > CLK_SCALE)
  751. div = CLK_SCALE;
  752. clkrc = (clkrc & 0x80) | div;
  753. tpf->numerator = 1;
  754. tpf->denominator = OV7670_FRAME_RATE/div;
  755. return ov7670_write(sd, REG_CLKRC, clkrc);
  756. }
  757. /*
  758. * Code for dealing with controls.
  759. */
  760. static int ov7670_store_cmatrix(struct v4l2_subdev *sd,
  761. int matrix[CMATRIX_LEN])
  762. {
  763. int i, ret;
  764. unsigned char signbits = 0;
  765. /*
  766. * Weird crap seems to exist in the upper part of
  767. * the sign bits register, so let's preserve it.
  768. */
  769. ret = ov7670_read(sd, REG_CMATRIX_SIGN, &signbits);
  770. signbits &= 0xc0;
  771. for (i = 0; i < CMATRIX_LEN; i++) {
  772. unsigned char raw;
  773. if (matrix[i] < 0) {
  774. signbits |= (1 << i);
  775. if (matrix[i] < -255)
  776. raw = 0xff;
  777. else
  778. raw = (-1 * matrix[i]) & 0xff;
  779. }
  780. else {
  781. if (matrix[i] > 255)
  782. raw = 0xff;
  783. else
  784. raw = matrix[i] & 0xff;
  785. }
  786. ret += ov7670_write(sd, REG_CMATRIX_BASE + i, raw);
  787. }
  788. ret += ov7670_write(sd, REG_CMATRIX_SIGN, signbits);
  789. return ret;
  790. }
  791. /*
  792. * Hue also requires messing with the color matrix. It also requires
  793. * trig functions, which tend not to be well supported in the kernel.
  794. * So here is a simple table of sine values, 0-90 degrees, in steps
  795. * of five degrees. Values are multiplied by 1000.
  796. *
  797. * The following naive approximate trig functions require an argument
  798. * carefully limited to -180 <= theta <= 180.
  799. */
  800. #define SIN_STEP 5
  801. static const int ov7670_sin_table[] = {
  802. 0, 87, 173, 258, 342, 422,
  803. 499, 573, 642, 707, 766, 819,
  804. 866, 906, 939, 965, 984, 996,
  805. 1000
  806. };
  807. static int ov7670_sine(int theta)
  808. {
  809. int chs = 1;
  810. int sine;
  811. if (theta < 0) {
  812. theta = -theta;
  813. chs = -1;
  814. }
  815. if (theta <= 90)
  816. sine = ov7670_sin_table[theta/SIN_STEP];
  817. else {
  818. theta -= 90;
  819. sine = 1000 - ov7670_sin_table[theta/SIN_STEP];
  820. }
  821. return sine*chs;
  822. }
  823. static int ov7670_cosine(int theta)
  824. {
  825. theta = 90 - theta;
  826. if (theta > 180)
  827. theta -= 360;
  828. else if (theta < -180)
  829. theta += 360;
  830. return ov7670_sine(theta);
  831. }
  832. static void ov7670_calc_cmatrix(struct ov7670_info *info,
  833. int matrix[CMATRIX_LEN])
  834. {
  835. int i;
  836. /*
  837. * Apply the current saturation setting first.
  838. */
  839. for (i = 0; i < CMATRIX_LEN; i++)
  840. matrix[i] = (info->fmt->cmatrix[i]*info->sat) >> 7;
  841. /*
  842. * Then, if need be, rotate the hue value.
  843. */
  844. if (info->hue != 0) {
  845. int sinth, costh, tmpmatrix[CMATRIX_LEN];
  846. memcpy(tmpmatrix, matrix, CMATRIX_LEN*sizeof(int));
  847. sinth = ov7670_sine(info->hue);
  848. costh = ov7670_cosine(info->hue);
  849. matrix[0] = (matrix[3]*sinth + matrix[0]*costh)/1000;
  850. matrix[1] = (matrix[4]*sinth + matrix[1]*costh)/1000;
  851. matrix[2] = (matrix[5]*sinth + matrix[2]*costh)/1000;
  852. matrix[3] = (matrix[3]*costh - matrix[0]*sinth)/1000;
  853. matrix[4] = (matrix[4]*costh - matrix[1]*sinth)/1000;
  854. matrix[5] = (matrix[5]*costh - matrix[2]*sinth)/1000;
  855. }
  856. }
  857. static int ov7670_s_sat(struct v4l2_subdev *sd, int value)
  858. {
  859. struct ov7670_info *info = to_state(sd);
  860. int matrix[CMATRIX_LEN];
  861. int ret;
  862. info->sat = value;
  863. ov7670_calc_cmatrix(info, matrix);
  864. ret = ov7670_store_cmatrix(sd, matrix);
  865. return ret;
  866. }
  867. static int ov7670_g_sat(struct v4l2_subdev *sd, __s32 *value)
  868. {
  869. struct ov7670_info *info = to_state(sd);
  870. *value = info->sat;
  871. return 0;
  872. }
  873. static int ov7670_s_hue(struct v4l2_subdev *sd, int value)
  874. {
  875. struct ov7670_info *info = to_state(sd);
  876. int matrix[CMATRIX_LEN];
  877. int ret;
  878. if (value < -180 || value > 180)
  879. return -EINVAL;
  880. info->hue = value;
  881. ov7670_calc_cmatrix(info, matrix);
  882. ret = ov7670_store_cmatrix(sd, matrix);
  883. return ret;
  884. }
  885. static int ov7670_g_hue(struct v4l2_subdev *sd, __s32 *value)
  886. {
  887. struct ov7670_info *info = to_state(sd);
  888. *value = info->hue;
  889. return 0;
  890. }
  891. /*
  892. * Some weird registers seem to store values in a sign/magnitude format!
  893. */
  894. static unsigned char ov7670_sm_to_abs(unsigned char v)
  895. {
  896. if ((v & 0x80) == 0)
  897. return v + 128;
  898. return 128 - (v & 0x7f);
  899. }
  900. static unsigned char ov7670_abs_to_sm(unsigned char v)
  901. {
  902. if (v > 127)
  903. return v & 0x7f;
  904. return (128 - v) | 0x80;
  905. }
  906. static int ov7670_s_brightness(struct v4l2_subdev *sd, int value)
  907. {
  908. unsigned char com8 = 0, v;
  909. int ret;
  910. ov7670_read(sd, REG_COM8, &com8);
  911. com8 &= ~COM8_AEC;
  912. ov7670_write(sd, REG_COM8, com8);
  913. v = ov7670_abs_to_sm(value);
  914. ret = ov7670_write(sd, REG_BRIGHT, v);
  915. return ret;
  916. }
  917. static int ov7670_g_brightness(struct v4l2_subdev *sd, __s32 *value)
  918. {
  919. unsigned char v = 0;
  920. int ret = ov7670_read(sd, REG_BRIGHT, &v);
  921. *value = ov7670_sm_to_abs(v);
  922. return ret;
  923. }
  924. static int ov7670_s_contrast(struct v4l2_subdev *sd, int value)
  925. {
  926. return ov7670_write(sd, REG_CONTRAS, (unsigned char) value);
  927. }
  928. static int ov7670_g_contrast(struct v4l2_subdev *sd, __s32 *value)
  929. {
  930. unsigned char v = 0;
  931. int ret = ov7670_read(sd, REG_CONTRAS, &v);
  932. *value = v;
  933. return ret;
  934. }
  935. static int ov7670_g_hflip(struct v4l2_subdev *sd, __s32 *value)
  936. {
  937. int ret;
  938. unsigned char v = 0;
  939. ret = ov7670_read(sd, REG_MVFP, &v);
  940. *value = (v & MVFP_MIRROR) == MVFP_MIRROR;
  941. return ret;
  942. }
  943. static int ov7670_s_hflip(struct v4l2_subdev *sd, int value)
  944. {
  945. unsigned char v = 0;
  946. int ret;
  947. ret = ov7670_read(sd, REG_MVFP, &v);
  948. if (value)
  949. v |= MVFP_MIRROR;
  950. else
  951. v &= ~MVFP_MIRROR;
  952. msleep(10); /* FIXME */
  953. ret += ov7670_write(sd, REG_MVFP, v);
  954. return ret;
  955. }
  956. static int ov7670_g_vflip(struct v4l2_subdev *sd, __s32 *value)
  957. {
  958. int ret;
  959. unsigned char v = 0;
  960. ret = ov7670_read(sd, REG_MVFP, &v);
  961. *value = (v & MVFP_FLIP) == MVFP_FLIP;
  962. return ret;
  963. }
  964. static int ov7670_s_vflip(struct v4l2_subdev *sd, int value)
  965. {
  966. unsigned char v = 0;
  967. int ret;
  968. ret = ov7670_read(sd, REG_MVFP, &v);
  969. if (value)
  970. v |= MVFP_FLIP;
  971. else
  972. v &= ~MVFP_FLIP;
  973. msleep(10); /* FIXME */
  974. ret += ov7670_write(sd, REG_MVFP, v);
  975. return ret;
  976. }
  977. static int ov7670_queryctrl(struct v4l2_subdev *sd,
  978. struct v4l2_queryctrl *qc)
  979. {
  980. /* Fill in min, max, step and default value for these controls. */
  981. switch (qc->id) {
  982. case V4L2_CID_BRIGHTNESS:
  983. return v4l2_ctrl_query_fill(qc, 0, 255, 1, 128);
  984. case V4L2_CID_CONTRAST:
  985. return v4l2_ctrl_query_fill(qc, 0, 127, 1, 64);
  986. case V4L2_CID_VFLIP:
  987. case V4L2_CID_HFLIP:
  988. return v4l2_ctrl_query_fill(qc, 0, 1, 1, 0);
  989. case V4L2_CID_SATURATION:
  990. return v4l2_ctrl_query_fill(qc, 0, 256, 1, 128);
  991. case V4L2_CID_HUE:
  992. return v4l2_ctrl_query_fill(qc, -180, 180, 5, 0);
  993. }
  994. return -EINVAL;
  995. }
  996. static int ov7670_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  997. {
  998. switch (ctrl->id) {
  999. case V4L2_CID_BRIGHTNESS:
  1000. return ov7670_g_brightness(sd, &ctrl->value);
  1001. case V4L2_CID_CONTRAST:
  1002. return ov7670_g_contrast(sd, &ctrl->value);
  1003. case V4L2_CID_SATURATION:
  1004. return ov7670_g_sat(sd, &ctrl->value);
  1005. case V4L2_CID_HUE:
  1006. return ov7670_g_hue(sd, &ctrl->value);
  1007. case V4L2_CID_VFLIP:
  1008. return ov7670_g_vflip(sd, &ctrl->value);
  1009. case V4L2_CID_HFLIP:
  1010. return ov7670_g_hflip(sd, &ctrl->value);
  1011. }
  1012. return -EINVAL;
  1013. }
  1014. static int ov7670_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  1015. {
  1016. switch (ctrl->id) {
  1017. case V4L2_CID_BRIGHTNESS:
  1018. return ov7670_s_brightness(sd, ctrl->value);
  1019. case V4L2_CID_CONTRAST:
  1020. return ov7670_s_contrast(sd, ctrl->value);
  1021. case V4L2_CID_SATURATION:
  1022. return ov7670_s_sat(sd, ctrl->value);
  1023. case V4L2_CID_HUE:
  1024. return ov7670_s_hue(sd, ctrl->value);
  1025. case V4L2_CID_VFLIP:
  1026. return ov7670_s_vflip(sd, ctrl->value);
  1027. case V4L2_CID_HFLIP:
  1028. return ov7670_s_hflip(sd, ctrl->value);
  1029. }
  1030. return -EINVAL;
  1031. }
  1032. static int ov7670_g_chip_ident(struct v4l2_subdev *sd,
  1033. struct v4l2_dbg_chip_ident *chip)
  1034. {
  1035. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1036. return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_OV7670, 0);
  1037. }
  1038. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1039. static int ov7670_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
  1040. {
  1041. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1042. unsigned char val = 0;
  1043. int ret;
  1044. if (!v4l2_chip_match_i2c_client(client, &reg->match))
  1045. return -EINVAL;
  1046. if (!capable(CAP_SYS_ADMIN))
  1047. return -EPERM;
  1048. ret = ov7670_read(sd, reg->reg & 0xff, &val);
  1049. reg->val = val;
  1050. reg->size = 1;
  1051. return ret;
  1052. }
  1053. static int ov7670_s_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
  1054. {
  1055. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1056. if (!v4l2_chip_match_i2c_client(client, &reg->match))
  1057. return -EINVAL;
  1058. if (!capable(CAP_SYS_ADMIN))
  1059. return -EPERM;
  1060. ov7670_write(sd, reg->reg & 0xff, reg->val & 0xff);
  1061. return 0;
  1062. }
  1063. #endif
  1064. /* ----------------------------------------------------------------------- */
  1065. static const struct v4l2_subdev_core_ops ov7670_core_ops = {
  1066. .g_chip_ident = ov7670_g_chip_ident,
  1067. .g_ctrl = ov7670_g_ctrl,
  1068. .s_ctrl = ov7670_s_ctrl,
  1069. .queryctrl = ov7670_queryctrl,
  1070. .reset = ov7670_reset,
  1071. .init = ov7670_init,
  1072. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1073. .g_register = ov7670_g_register,
  1074. .s_register = ov7670_s_register,
  1075. #endif
  1076. };
  1077. static const struct v4l2_subdev_video_ops ov7670_video_ops = {
  1078. .enum_fmt = ov7670_enum_fmt,
  1079. .try_fmt = ov7670_try_fmt,
  1080. .s_fmt = ov7670_s_fmt,
  1081. .s_parm = ov7670_s_parm,
  1082. .g_parm = ov7670_g_parm,
  1083. };
  1084. static const struct v4l2_subdev_ops ov7670_ops = {
  1085. .core = &ov7670_core_ops,
  1086. .video = &ov7670_video_ops,
  1087. };
  1088. /* ----------------------------------------------------------------------- */
  1089. static int ov7670_probe(struct i2c_client *client,
  1090. const struct i2c_device_id *id)
  1091. {
  1092. struct v4l2_subdev *sd;
  1093. struct ov7670_info *info;
  1094. int ret;
  1095. info = kzalloc(sizeof(struct ov7670_info), GFP_KERNEL);
  1096. if (info == NULL)
  1097. return -ENOMEM;
  1098. sd = &info->sd;
  1099. v4l2_i2c_subdev_init(sd, client, &ov7670_ops);
  1100. /* Make sure it's an ov7670 */
  1101. ret = ov7670_detect(sd);
  1102. if (ret) {
  1103. v4l_dbg(1, debug, client,
  1104. "chip found @ 0x%x (%s) is not an ov7670 chip.\n",
  1105. client->addr << 1, client->adapter->name);
  1106. kfree(info);
  1107. return ret;
  1108. }
  1109. v4l_info(client, "chip found @ 0x%02x (%s)\n",
  1110. client->addr << 1, client->adapter->name);
  1111. info->fmt = &ov7670_formats[0];
  1112. info->sat = 128; /* Review this */
  1113. return 0;
  1114. }
  1115. static int ov7670_remove(struct i2c_client *client)
  1116. {
  1117. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1118. v4l2_device_unregister_subdev(sd);
  1119. kfree(to_state(sd));
  1120. return 0;
  1121. }
  1122. static const struct i2c_device_id ov7670_id[] = {
  1123. { "ov7670", 0 },
  1124. { }
  1125. };
  1126. MODULE_DEVICE_TABLE(i2c, ov7670_id);
  1127. static struct v4l2_i2c_driver_data v4l2_i2c_data = {
  1128. .name = "ov7670",
  1129. .probe = ov7670_probe,
  1130. .remove = ov7670_remove,
  1131. .id_table = ov7670_id,
  1132. };