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