ov7670.c 34 KB

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