ov7670.c 35 KB

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