ov7670.c 32 KB

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