ov7670.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645
  1. /*
  2. * A V4L2 driver for OmniVision OV7670 cameras.
  3. *
  4. * Copyright 2006 One Laptop Per Child Association, Inc. Written
  5. * by Jonathan Corbet with substantial inspiration from Mark
  6. * McClelland's ovcamchip code.
  7. *
  8. * Copyright 2006-7 Jonathan Corbet <corbet@lwn.net>
  9. *
  10. * This file may be distributed under the terms of the GNU General
  11. * Public License, version 2.
  12. */
  13. #include <linux/init.h>
  14. #include <linux/module.h>
  15. #include <linux/slab.h>
  16. #include <linux/i2c.h>
  17. #include <linux/delay.h>
  18. #include <linux/videodev2.h>
  19. #include <media/v4l2-device.h>
  20. #include <media/v4l2-chip-ident.h>
  21. #include <media/v4l2-mediabus.h>
  22. #include <media/ov7670.h>
  23. MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
  24. MODULE_DESCRIPTION("A low-level driver for OmniVision ov7670 sensors");
  25. MODULE_LICENSE("GPL");
  26. static bool debug;
  27. module_param(debug, bool, 0644);
  28. MODULE_PARM_DESC(debug, "Debug level (0-1)");
  29. /*
  30. * Basic window sizes. These probably belong somewhere more globally
  31. * useful.
  32. */
  33. #define VGA_WIDTH 640
  34. #define VGA_HEIGHT 480
  35. #define QVGA_WIDTH 320
  36. #define QVGA_HEIGHT 240
  37. #define CIF_WIDTH 352
  38. #define CIF_HEIGHT 288
  39. #define QCIF_WIDTH 176
  40. #define QCIF_HEIGHT 144
  41. /*
  42. * The 7670 sits on i2c with ID 0x42
  43. */
  44. #define OV7670_I2C_ADDR 0x42
  45. /* Registers */
  46. #define REG_GAIN 0x00 /* Gain lower 8 bits (rest in vref) */
  47. #define REG_BLUE 0x01 /* blue gain */
  48. #define REG_RED 0x02 /* red gain */
  49. #define REG_VREF 0x03 /* Pieces of GAIN, VSTART, VSTOP */
  50. #define REG_COM1 0x04 /* Control 1 */
  51. #define COM1_CCIR656 0x40 /* CCIR656 enable */
  52. #define REG_BAVE 0x05 /* U/B Average level */
  53. #define REG_GbAVE 0x06 /* Y/Gb Average level */
  54. #define REG_AECHH 0x07 /* AEC MS 5 bits */
  55. #define REG_RAVE 0x08 /* V/R Average level */
  56. #define REG_COM2 0x09 /* Control 2 */
  57. #define COM2_SSLEEP 0x10 /* Soft sleep mode */
  58. #define REG_PID 0x0a /* Product ID MSB */
  59. #define REG_VER 0x0b /* Product ID LSB */
  60. #define REG_COM3 0x0c /* Control 3 */
  61. #define COM3_SWAP 0x40 /* Byte swap */
  62. #define COM3_SCALEEN 0x08 /* Enable scaling */
  63. #define COM3_DCWEN 0x04 /* Enable downsamp/crop/window */
  64. #define REG_COM4 0x0d /* Control 4 */
  65. #define REG_COM5 0x0e /* All "reserved" */
  66. #define REG_COM6 0x0f /* Control 6 */
  67. #define REG_AECH 0x10 /* More bits of AEC value */
  68. #define REG_CLKRC 0x11 /* Clocl control */
  69. #define CLK_EXT 0x40 /* Use external clock directly */
  70. #define CLK_SCALE 0x3f /* Mask for internal clock scale */
  71. #define REG_COM7 0x12 /* Control 7 */
  72. #define COM7_RESET 0x80 /* Register reset */
  73. #define COM7_FMT_MASK 0x38
  74. #define COM7_FMT_VGA 0x00
  75. #define COM7_FMT_CIF 0x20 /* CIF format */
  76. #define COM7_FMT_QVGA 0x10 /* QVGA format */
  77. #define COM7_FMT_QCIF 0x08 /* QCIF format */
  78. #define COM7_RGB 0x04 /* bits 0 and 2 - RGB format */
  79. #define COM7_YUV 0x00 /* YUV */
  80. #define COM7_BAYER 0x01 /* Bayer format */
  81. #define COM7_PBAYER 0x05 /* "Processed bayer" */
  82. #define REG_COM8 0x13 /* Control 8 */
  83. #define COM8_FASTAEC 0x80 /* Enable fast AGC/AEC */
  84. #define COM8_AECSTEP 0x40 /* Unlimited AEC step size */
  85. #define COM8_BFILT 0x20 /* Band filter enable */
  86. #define COM8_AGC 0x04 /* Auto gain enable */
  87. #define COM8_AWB 0x02 /* White balance enable */
  88. #define COM8_AEC 0x01 /* Auto exposure enable */
  89. #define REG_COM9 0x14 /* Control 9 - gain ceiling */
  90. #define REG_COM10 0x15 /* Control 10 */
  91. #define COM10_HSYNC 0x40 /* HSYNC instead of HREF */
  92. #define COM10_PCLK_HB 0x20 /* Suppress PCLK on horiz blank */
  93. #define COM10_HREF_REV 0x08 /* Reverse HREF */
  94. #define COM10_VS_LEAD 0x04 /* VSYNC on clock leading edge */
  95. #define COM10_VS_NEG 0x02 /* VSYNC negative */
  96. #define COM10_HS_NEG 0x01 /* HSYNC negative */
  97. #define REG_HSTART 0x17 /* Horiz start high bits */
  98. #define REG_HSTOP 0x18 /* Horiz stop high bits */
  99. #define REG_VSTART 0x19 /* Vert start high bits */
  100. #define REG_VSTOP 0x1a /* Vert stop high bits */
  101. #define REG_PSHFT 0x1b /* Pixel delay after HREF */
  102. #define REG_MIDH 0x1c /* Manuf. ID high */
  103. #define REG_MIDL 0x1d /* Manuf. ID low */
  104. #define REG_MVFP 0x1e /* Mirror / vflip */
  105. #define MVFP_MIRROR 0x20 /* Mirror image */
  106. #define MVFP_FLIP 0x10 /* Vertical flip */
  107. #define REG_AEW 0x24 /* AGC upper limit */
  108. #define REG_AEB 0x25 /* AGC lower limit */
  109. #define REG_VPT 0x26 /* AGC/AEC fast mode op region */
  110. #define REG_HSYST 0x30 /* HSYNC rising edge delay */
  111. #define REG_HSYEN 0x31 /* HSYNC falling edge delay */
  112. #define REG_HREF 0x32 /* HREF pieces */
  113. #define REG_TSLB 0x3a /* lots of stuff */
  114. #define TSLB_YLAST 0x04 /* UYVY or VYUY - see com13 */
  115. #define REG_COM11 0x3b /* Control 11 */
  116. #define COM11_NIGHT 0x80 /* NIght mode enable */
  117. #define COM11_NMFR 0x60 /* Two bit NM frame rate */
  118. #define COM11_HZAUTO 0x10 /* Auto detect 50/60 Hz */
  119. #define COM11_50HZ 0x08 /* Manual 50Hz select */
  120. #define COM11_EXP 0x02
  121. #define REG_COM12 0x3c /* Control 12 */
  122. #define COM12_HREF 0x80 /* HREF always */
  123. #define REG_COM13 0x3d /* Control 13 */
  124. #define COM13_GAMMA 0x80 /* Gamma enable */
  125. #define COM13_UVSAT 0x40 /* UV saturation auto adjustment */
  126. #define COM13_UVSWAP 0x01 /* V before U - w/TSLB */
  127. #define REG_COM14 0x3e /* Control 14 */
  128. #define COM14_DCWEN 0x10 /* DCW/PCLK-scale enable */
  129. #define REG_EDGE 0x3f /* Edge enhancement factor */
  130. #define REG_COM15 0x40 /* Control 15 */
  131. #define COM15_R10F0 0x00 /* Data range 10 to F0 */
  132. #define COM15_R01FE 0x80 /* 01 to FE */
  133. #define COM15_R00FF 0xc0 /* 00 to FF */
  134. #define COM15_RGB565 0x10 /* RGB565 output */
  135. #define COM15_RGB555 0x30 /* RGB555 output */
  136. #define REG_COM16 0x41 /* Control 16 */
  137. #define COM16_AWBGAIN 0x08 /* AWB gain enable */
  138. #define REG_COM17 0x42 /* Control 17 */
  139. #define COM17_AECWIN 0xc0 /* AEC window - must match COM4 */
  140. #define COM17_CBAR 0x08 /* DSP Color bar */
  141. /*
  142. * This matrix defines how the colors are generated, must be
  143. * tweaked to adjust hue and saturation.
  144. *
  145. * Order: v-red, v-green, v-blue, u-red, u-green, u-blue
  146. *
  147. * They are nine-bit signed quantities, with the sign bit
  148. * stored in 0x58. Sign for v-red is bit 0, and up from there.
  149. */
  150. #define REG_CMATRIX_BASE 0x4f
  151. #define CMATRIX_LEN 6
  152. #define REG_CMATRIX_SIGN 0x58
  153. #define REG_BRIGHT 0x55 /* Brightness */
  154. #define REG_CONTRAS 0x56 /* Contrast control */
  155. #define REG_GFIX 0x69 /* Fix gain control */
  156. #define REG_REG76 0x76 /* OV's name */
  157. #define R76_BLKPCOR 0x80 /* Black pixel correction enable */
  158. #define R76_WHTPCOR 0x40 /* White pixel correction enable */
  159. #define REG_RGB444 0x8c /* RGB 444 control */
  160. #define R444_ENABLE 0x02 /* Turn on RGB444, overrides 5x5 */
  161. #define R444_RGBX 0x01 /* Empty nibble at end */
  162. #define REG_HAECC1 0x9f /* Hist AEC/AGC control 1 */
  163. #define REG_HAECC2 0xa0 /* Hist AEC/AGC control 2 */
  164. #define REG_BD50MAX 0xa5 /* 50hz banding step limit */
  165. #define REG_HAECC3 0xa6 /* Hist AEC/AGC control 3 */
  166. #define REG_HAECC4 0xa7 /* Hist AEC/AGC control 4 */
  167. #define REG_HAECC5 0xa8 /* Hist AEC/AGC control 5 */
  168. #define REG_HAECC6 0xa9 /* Hist AEC/AGC control 6 */
  169. #define REG_HAECC7 0xaa /* Hist AEC/AGC control 7 */
  170. #define REG_BD60MAX 0xab /* 60hz banding step limit */
  171. enum ov7670_model {
  172. MODEL_OV7670 = 0,
  173. MODEL_OV7675,
  174. };
  175. struct ov7670_win_size {
  176. int width;
  177. int height;
  178. unsigned char com7_bit;
  179. int hstart; /* Start/stop values for the camera. Note */
  180. int hstop; /* that they do not always make complete */
  181. int vstart; /* sense to humans, but evidently the sensor */
  182. int vstop; /* will do the right thing... */
  183. struct regval_list *regs; /* Regs to tweak */
  184. };
  185. struct ov7670_devtype {
  186. /* formats supported for each model */
  187. struct ov7670_win_size *win_sizes;
  188. unsigned int n_win_sizes;
  189. };
  190. /*
  191. * Information we maintain about a known sensor.
  192. */
  193. struct ov7670_format_struct; /* coming later */
  194. struct ov7670_info {
  195. struct v4l2_subdev sd;
  196. struct ov7670_format_struct *fmt; /* Current format */
  197. unsigned char sat; /* Saturation value */
  198. int hue; /* Hue value */
  199. int min_width; /* Filter out smaller sizes */
  200. int min_height; /* Filter out smaller sizes */
  201. int clock_speed; /* External clock speed (MHz) */
  202. u8 clkrc; /* Clock divider value */
  203. bool use_smbus; /* Use smbus I/O instead of I2C */
  204. const struct ov7670_devtype *devtype; /* Device specifics */
  205. };
  206. static inline struct ov7670_info *to_state(struct v4l2_subdev *sd)
  207. {
  208. return container_of(sd, struct ov7670_info, sd);
  209. }
  210. /*
  211. * The default register settings, as obtained from OmniVision. There
  212. * is really no making sense of most of these - lots of "reserved" values
  213. * and such.
  214. *
  215. * These settings give VGA YUYV.
  216. */
  217. struct regval_list {
  218. unsigned char reg_num;
  219. unsigned char value;
  220. };
  221. static struct regval_list ov7670_default_regs[] = {
  222. { REG_COM7, COM7_RESET },
  223. /*
  224. * Clock scale: 3 = 15fps
  225. * 2 = 20fps
  226. * 1 = 30fps
  227. */
  228. { REG_CLKRC, 0x1 }, /* OV: clock scale (30 fps) */
  229. { REG_TSLB, 0x04 }, /* OV */
  230. { REG_COM7, 0 }, /* VGA */
  231. /*
  232. * Set the hardware window. These values from OV don't entirely
  233. * make sense - hstop is less than hstart. But they work...
  234. */
  235. { REG_HSTART, 0x13 }, { REG_HSTOP, 0x01 },
  236. { REG_HREF, 0xb6 }, { REG_VSTART, 0x02 },
  237. { REG_VSTOP, 0x7a }, { REG_VREF, 0x0a },
  238. { REG_COM3, 0 }, { REG_COM14, 0 },
  239. /* Mystery scaling numbers */
  240. { 0x70, 0x3a }, { 0x71, 0x35 },
  241. { 0x72, 0x11 }, { 0x73, 0xf0 },
  242. { 0xa2, 0x02 }, { REG_COM10, 0x0 },
  243. /* Gamma curve values */
  244. { 0x7a, 0x20 }, { 0x7b, 0x10 },
  245. { 0x7c, 0x1e }, { 0x7d, 0x35 },
  246. { 0x7e, 0x5a }, { 0x7f, 0x69 },
  247. { 0x80, 0x76 }, { 0x81, 0x80 },
  248. { 0x82, 0x88 }, { 0x83, 0x8f },
  249. { 0x84, 0x96 }, { 0x85, 0xa3 },
  250. { 0x86, 0xaf }, { 0x87, 0xc4 },
  251. { 0x88, 0xd7 }, { 0x89, 0xe8 },
  252. /* AGC and AEC parameters. Note we start by disabling those features,
  253. then turn them only after tweaking the values. */
  254. { REG_COM8, COM8_FASTAEC | COM8_AECSTEP | COM8_BFILT },
  255. { REG_GAIN, 0 }, { REG_AECH, 0 },
  256. { REG_COM4, 0x40 }, /* magic reserved bit */
  257. { REG_COM9, 0x18 }, /* 4x gain + magic rsvd bit */
  258. { REG_BD50MAX, 0x05 }, { REG_BD60MAX, 0x07 },
  259. { REG_AEW, 0x95 }, { REG_AEB, 0x33 },
  260. { REG_VPT, 0xe3 }, { REG_HAECC1, 0x78 },
  261. { REG_HAECC2, 0x68 }, { 0xa1, 0x03 }, /* magic */
  262. { REG_HAECC3, 0xd8 }, { REG_HAECC4, 0xd8 },
  263. { REG_HAECC5, 0xf0 }, { REG_HAECC6, 0x90 },
  264. { REG_HAECC7, 0x94 },
  265. { REG_COM8, COM8_FASTAEC|COM8_AECSTEP|COM8_BFILT|COM8_AGC|COM8_AEC },
  266. /* Almost all of these are magic "reserved" values. */
  267. { REG_COM5, 0x61 }, { REG_COM6, 0x4b },
  268. { 0x16, 0x02 }, { REG_MVFP, 0x07 },
  269. { 0x21, 0x02 }, { 0x22, 0x91 },
  270. { 0x29, 0x07 }, { 0x33, 0x0b },
  271. { 0x35, 0x0b }, { 0x37, 0x1d },
  272. { 0x38, 0x71 }, { 0x39, 0x2a },
  273. { REG_COM12, 0x78 }, { 0x4d, 0x40 },
  274. { 0x4e, 0x20 }, { REG_GFIX, 0 },
  275. { 0x6b, 0x4a }, { 0x74, 0x10 },
  276. { 0x8d, 0x4f }, { 0x8e, 0 },
  277. { 0x8f, 0 }, { 0x90, 0 },
  278. { 0x91, 0 }, { 0x96, 0 },
  279. { 0x9a, 0 }, { 0xb0, 0x84 },
  280. { 0xb1, 0x0c }, { 0xb2, 0x0e },
  281. { 0xb3, 0x82 }, { 0xb8, 0x0a },
  282. /* More reserved magic, some of which tweaks white balance */
  283. { 0x43, 0x0a }, { 0x44, 0xf0 },
  284. { 0x45, 0x34 }, { 0x46, 0x58 },
  285. { 0x47, 0x28 }, { 0x48, 0x3a },
  286. { 0x59, 0x88 }, { 0x5a, 0x88 },
  287. { 0x5b, 0x44 }, { 0x5c, 0x67 },
  288. { 0x5d, 0x49 }, { 0x5e, 0x0e },
  289. { 0x6c, 0x0a }, { 0x6d, 0x55 },
  290. { 0x6e, 0x11 }, { 0x6f, 0x9f }, /* "9e for advance AWB" */
  291. { 0x6a, 0x40 }, { REG_BLUE, 0x40 },
  292. { REG_RED, 0x60 },
  293. { REG_COM8, COM8_FASTAEC|COM8_AECSTEP|COM8_BFILT|COM8_AGC|COM8_AEC|COM8_AWB },
  294. /* Matrix coefficients */
  295. { 0x4f, 0x80 }, { 0x50, 0x80 },
  296. { 0x51, 0 }, { 0x52, 0x22 },
  297. { 0x53, 0x5e }, { 0x54, 0x80 },
  298. { 0x58, 0x9e },
  299. { REG_COM16, COM16_AWBGAIN }, { REG_EDGE, 0 },
  300. { 0x75, 0x05 }, { 0x76, 0xe1 },
  301. { 0x4c, 0 }, { 0x77, 0x01 },
  302. { REG_COM13, 0xc3 }, { 0x4b, 0x09 },
  303. { 0xc9, 0x60 }, { REG_COM16, 0x38 },
  304. { 0x56, 0x40 },
  305. { 0x34, 0x11 }, { REG_COM11, COM11_EXP|COM11_HZAUTO },
  306. { 0xa4, 0x88 }, { 0x96, 0 },
  307. { 0x97, 0x30 }, { 0x98, 0x20 },
  308. { 0x99, 0x30 }, { 0x9a, 0x84 },
  309. { 0x9b, 0x29 }, { 0x9c, 0x03 },
  310. { 0x9d, 0x4c }, { 0x9e, 0x3f },
  311. { 0x78, 0x04 },
  312. /* Extra-weird stuff. Some sort of multiplexor register */
  313. { 0x79, 0x01 }, { 0xc8, 0xf0 },
  314. { 0x79, 0x0f }, { 0xc8, 0x00 },
  315. { 0x79, 0x10 }, { 0xc8, 0x7e },
  316. { 0x79, 0x0a }, { 0xc8, 0x80 },
  317. { 0x79, 0x0b }, { 0xc8, 0x01 },
  318. { 0x79, 0x0c }, { 0xc8, 0x0f },
  319. { 0x79, 0x0d }, { 0xc8, 0x20 },
  320. { 0x79, 0x09 }, { 0xc8, 0x80 },
  321. { 0x79, 0x02 }, { 0xc8, 0xc0 },
  322. { 0x79, 0x03 }, { 0xc8, 0x40 },
  323. { 0x79, 0x05 }, { 0xc8, 0x30 },
  324. { 0x79, 0x26 },
  325. { 0xff, 0xff }, /* END MARKER */
  326. };
  327. /*
  328. * Here we'll try to encapsulate the changes for just the output
  329. * video format.
  330. *
  331. * RGB656 and YUV422 come from OV; RGB444 is homebrewed.
  332. *
  333. * IMPORTANT RULE: the first entry must be for COM7, see ov7670_s_fmt for why.
  334. */
  335. static struct regval_list ov7670_fmt_yuv422[] = {
  336. { REG_COM7, 0x0 }, /* Selects YUV mode */
  337. { REG_RGB444, 0 }, /* No RGB444 please */
  338. { REG_COM1, 0 }, /* CCIR601 */
  339. { REG_COM15, COM15_R00FF },
  340. { REG_COM9, 0x48 }, /* 32x gain ceiling; 0x8 is reserved bit */
  341. { 0x4f, 0x80 }, /* "matrix coefficient 1" */
  342. { 0x50, 0x80 }, /* "matrix coefficient 2" */
  343. { 0x51, 0 }, /* vb */
  344. { 0x52, 0x22 }, /* "matrix coefficient 4" */
  345. { 0x53, 0x5e }, /* "matrix coefficient 5" */
  346. { 0x54, 0x80 }, /* "matrix coefficient 6" */
  347. { REG_COM13, COM13_GAMMA|COM13_UVSAT },
  348. { 0xff, 0xff },
  349. };
  350. static struct regval_list ov7670_fmt_rgb565[] = {
  351. { REG_COM7, COM7_RGB }, /* Selects RGB mode */
  352. { REG_RGB444, 0 }, /* No RGB444 please */
  353. { REG_COM1, 0x0 }, /* CCIR601 */
  354. { REG_COM15, COM15_RGB565 },
  355. { REG_COM9, 0x38 }, /* 16x gain ceiling; 0x8 is reserved bit */
  356. { 0x4f, 0xb3 }, /* "matrix coefficient 1" */
  357. { 0x50, 0xb3 }, /* "matrix coefficient 2" */
  358. { 0x51, 0 }, /* vb */
  359. { 0x52, 0x3d }, /* "matrix coefficient 4" */
  360. { 0x53, 0xa7 }, /* "matrix coefficient 5" */
  361. { 0x54, 0xe4 }, /* "matrix coefficient 6" */
  362. { REG_COM13, COM13_GAMMA|COM13_UVSAT },
  363. { 0xff, 0xff },
  364. };
  365. static struct regval_list ov7670_fmt_rgb444[] = {
  366. { REG_COM7, COM7_RGB }, /* Selects RGB mode */
  367. { REG_RGB444, R444_ENABLE }, /* Enable xxxxrrrr ggggbbbb */
  368. { REG_COM1, 0x0 }, /* CCIR601 */
  369. { REG_COM15, COM15_R01FE|COM15_RGB565 }, /* Data range needed? */
  370. { REG_COM9, 0x38 }, /* 16x gain ceiling; 0x8 is reserved bit */
  371. { 0x4f, 0xb3 }, /* "matrix coefficient 1" */
  372. { 0x50, 0xb3 }, /* "matrix coefficient 2" */
  373. { 0x51, 0 }, /* vb */
  374. { 0x52, 0x3d }, /* "matrix coefficient 4" */
  375. { 0x53, 0xa7 }, /* "matrix coefficient 5" */
  376. { 0x54, 0xe4 }, /* "matrix coefficient 6" */
  377. { REG_COM13, COM13_GAMMA|COM13_UVSAT|0x2 }, /* Magic rsvd bit */
  378. { 0xff, 0xff },
  379. };
  380. static struct regval_list ov7670_fmt_raw[] = {
  381. { REG_COM7, COM7_BAYER },
  382. { REG_COM13, 0x08 }, /* No gamma, magic rsvd bit */
  383. { REG_COM16, 0x3d }, /* Edge enhancement, denoise */
  384. { REG_REG76, 0xe1 }, /* Pix correction, magic rsvd */
  385. { 0xff, 0xff },
  386. };
  387. /*
  388. * Low-level register I/O.
  389. *
  390. * Note that there are two versions of these. On the XO 1, the
  391. * i2c controller only does SMBUS, so that's what we use. The
  392. * ov7670 is not really an SMBUS device, though, so the communication
  393. * is not always entirely reliable.
  394. */
  395. static int ov7670_read_smbus(struct v4l2_subdev *sd, unsigned char reg,
  396. unsigned char *value)
  397. {
  398. struct i2c_client *client = v4l2_get_subdevdata(sd);
  399. int ret;
  400. ret = i2c_smbus_read_byte_data(client, reg);
  401. if (ret >= 0) {
  402. *value = (unsigned char)ret;
  403. ret = 0;
  404. }
  405. return ret;
  406. }
  407. static int ov7670_write_smbus(struct v4l2_subdev *sd, unsigned char reg,
  408. unsigned char value)
  409. {
  410. struct i2c_client *client = v4l2_get_subdevdata(sd);
  411. int ret = i2c_smbus_write_byte_data(client, reg, value);
  412. if (reg == REG_COM7 && (value & COM7_RESET))
  413. msleep(5); /* Wait for reset to run */
  414. return ret;
  415. }
  416. /*
  417. * On most platforms, we'd rather do straight i2c I/O.
  418. */
  419. static int ov7670_read_i2c(struct v4l2_subdev *sd, unsigned char reg,
  420. unsigned char *value)
  421. {
  422. struct i2c_client *client = v4l2_get_subdevdata(sd);
  423. u8 data = reg;
  424. struct i2c_msg msg;
  425. int ret;
  426. /*
  427. * Send out the register address...
  428. */
  429. msg.addr = client->addr;
  430. msg.flags = 0;
  431. msg.len = 1;
  432. msg.buf = &data;
  433. ret = i2c_transfer(client->adapter, &msg, 1);
  434. if (ret < 0) {
  435. printk(KERN_ERR "Error %d on register write\n", ret);
  436. return ret;
  437. }
  438. /*
  439. * ...then read back the result.
  440. */
  441. msg.flags = I2C_M_RD;
  442. ret = i2c_transfer(client->adapter, &msg, 1);
  443. if (ret >= 0) {
  444. *value = data;
  445. ret = 0;
  446. }
  447. return ret;
  448. }
  449. static int ov7670_write_i2c(struct v4l2_subdev *sd, unsigned char reg,
  450. unsigned char value)
  451. {
  452. struct i2c_client *client = v4l2_get_subdevdata(sd);
  453. struct i2c_msg msg;
  454. unsigned char data[2] = { reg, value };
  455. int ret;
  456. msg.addr = client->addr;
  457. msg.flags = 0;
  458. msg.len = 2;
  459. msg.buf = data;
  460. ret = i2c_transfer(client->adapter, &msg, 1);
  461. if (ret > 0)
  462. ret = 0;
  463. if (reg == REG_COM7 && (value & COM7_RESET))
  464. msleep(5); /* Wait for reset to run */
  465. return ret;
  466. }
  467. static int ov7670_read(struct v4l2_subdev *sd, unsigned char reg,
  468. unsigned char *value)
  469. {
  470. struct ov7670_info *info = to_state(sd);
  471. if (info->use_smbus)
  472. return ov7670_read_smbus(sd, reg, value);
  473. else
  474. return ov7670_read_i2c(sd, reg, value);
  475. }
  476. static int ov7670_write(struct v4l2_subdev *sd, unsigned char reg,
  477. unsigned char value)
  478. {
  479. struct ov7670_info *info = to_state(sd);
  480. if (info->use_smbus)
  481. return ov7670_write_smbus(sd, reg, value);
  482. else
  483. return ov7670_write_i2c(sd, reg, value);
  484. }
  485. /*
  486. * Write a list of register settings; ff/ff stops the process.
  487. */
  488. static int ov7670_write_array(struct v4l2_subdev *sd, struct regval_list *vals)
  489. {
  490. while (vals->reg_num != 0xff || vals->value != 0xff) {
  491. int ret = ov7670_write(sd, vals->reg_num, vals->value);
  492. if (ret < 0)
  493. return ret;
  494. vals++;
  495. }
  496. return 0;
  497. }
  498. /*
  499. * Stuff that knows about the sensor.
  500. */
  501. static int ov7670_reset(struct v4l2_subdev *sd, u32 val)
  502. {
  503. ov7670_write(sd, REG_COM7, COM7_RESET);
  504. msleep(1);
  505. return 0;
  506. }
  507. static int ov7670_init(struct v4l2_subdev *sd, u32 val)
  508. {
  509. return ov7670_write_array(sd, ov7670_default_regs);
  510. }
  511. static int ov7670_detect(struct v4l2_subdev *sd)
  512. {
  513. unsigned char v;
  514. int ret;
  515. ret = ov7670_init(sd, 0);
  516. if (ret < 0)
  517. return ret;
  518. ret = ov7670_read(sd, REG_MIDH, &v);
  519. if (ret < 0)
  520. return ret;
  521. if (v != 0x7f) /* OV manuf. id. */
  522. return -ENODEV;
  523. ret = ov7670_read(sd, REG_MIDL, &v);
  524. if (ret < 0)
  525. return ret;
  526. if (v != 0xa2)
  527. return -ENODEV;
  528. /*
  529. * OK, we know we have an OmniVision chip...but which one?
  530. */
  531. ret = ov7670_read(sd, REG_PID, &v);
  532. if (ret < 0)
  533. return ret;
  534. if (v != 0x76) /* PID + VER = 0x76 / 0x73 */
  535. return -ENODEV;
  536. ret = ov7670_read(sd, REG_VER, &v);
  537. if (ret < 0)
  538. return ret;
  539. if (v != 0x73) /* PID + VER = 0x76 / 0x73 */
  540. return -ENODEV;
  541. return 0;
  542. }
  543. /*
  544. * Store information about the video data format. The color matrix
  545. * is deeply tied into the format, so keep the relevant values here.
  546. * The magic matrix numbers come from OmniVision.
  547. */
  548. static struct ov7670_format_struct {
  549. enum v4l2_mbus_pixelcode mbus_code;
  550. enum v4l2_colorspace colorspace;
  551. struct regval_list *regs;
  552. int cmatrix[CMATRIX_LEN];
  553. } ov7670_formats[] = {
  554. {
  555. .mbus_code = V4L2_MBUS_FMT_YUYV8_2X8,
  556. .colorspace = V4L2_COLORSPACE_JPEG,
  557. .regs = ov7670_fmt_yuv422,
  558. .cmatrix = { 128, -128, 0, -34, -94, 128 },
  559. },
  560. {
  561. .mbus_code = V4L2_MBUS_FMT_RGB444_2X8_PADHI_LE,
  562. .colorspace = V4L2_COLORSPACE_SRGB,
  563. .regs = ov7670_fmt_rgb444,
  564. .cmatrix = { 179, -179, 0, -61, -176, 228 },
  565. },
  566. {
  567. .mbus_code = V4L2_MBUS_FMT_RGB565_2X8_LE,
  568. .colorspace = V4L2_COLORSPACE_SRGB,
  569. .regs = ov7670_fmt_rgb565,
  570. .cmatrix = { 179, -179, 0, -61, -176, 228 },
  571. },
  572. {
  573. .mbus_code = V4L2_MBUS_FMT_SBGGR8_1X8,
  574. .colorspace = V4L2_COLORSPACE_SRGB,
  575. .regs = ov7670_fmt_raw,
  576. .cmatrix = { 0, 0, 0, 0, 0, 0 },
  577. },
  578. };
  579. #define N_OV7670_FMTS ARRAY_SIZE(ov7670_formats)
  580. /*
  581. * Then there is the issue of window sizes. Try to capture the info here.
  582. */
  583. /*
  584. * QCIF mode is done (by OV) in a very strange way - it actually looks like
  585. * VGA with weird scaling options - they do *not* use the canned QCIF mode
  586. * which is allegedly provided by the sensor. So here's the weird register
  587. * settings.
  588. */
  589. static struct regval_list ov7670_qcif_regs[] = {
  590. { REG_COM3, COM3_SCALEEN|COM3_DCWEN },
  591. { REG_COM3, COM3_DCWEN },
  592. { REG_COM14, COM14_DCWEN | 0x01},
  593. { 0x73, 0xf1 },
  594. { 0xa2, 0x52 },
  595. { 0x7b, 0x1c },
  596. { 0x7c, 0x28 },
  597. { 0x7d, 0x3c },
  598. { 0x7f, 0x69 },
  599. { REG_COM9, 0x38 },
  600. { 0xa1, 0x0b },
  601. { 0x74, 0x19 },
  602. { 0x9a, 0x80 },
  603. { 0x43, 0x14 },
  604. { REG_COM13, 0xc0 },
  605. { 0xff, 0xff },
  606. };
  607. static struct ov7670_win_size ov7670_win_sizes[] = {
  608. /* VGA */
  609. {
  610. .width = VGA_WIDTH,
  611. .height = VGA_HEIGHT,
  612. .com7_bit = COM7_FMT_VGA,
  613. .hstart = 158, /* These values from */
  614. .hstop = 14, /* Omnivision */
  615. .vstart = 10,
  616. .vstop = 490,
  617. .regs = NULL,
  618. },
  619. /* CIF */
  620. {
  621. .width = CIF_WIDTH,
  622. .height = CIF_HEIGHT,
  623. .com7_bit = COM7_FMT_CIF,
  624. .hstart = 170, /* Empirically determined */
  625. .hstop = 90,
  626. .vstart = 14,
  627. .vstop = 494,
  628. .regs = NULL,
  629. },
  630. /* QVGA */
  631. {
  632. .width = QVGA_WIDTH,
  633. .height = QVGA_HEIGHT,
  634. .com7_bit = COM7_FMT_QVGA,
  635. .hstart = 168, /* Empirically determined */
  636. .hstop = 24,
  637. .vstart = 12,
  638. .vstop = 492,
  639. .regs = NULL,
  640. },
  641. /* QCIF */
  642. {
  643. .width = QCIF_WIDTH,
  644. .height = QCIF_HEIGHT,
  645. .com7_bit = COM7_FMT_VGA, /* see comment above */
  646. .hstart = 456, /* Empirically determined */
  647. .hstop = 24,
  648. .vstart = 14,
  649. .vstop = 494,
  650. .regs = ov7670_qcif_regs,
  651. }
  652. };
  653. static struct ov7670_win_size ov7675_win_sizes[] = {
  654. /*
  655. * Currently, only VGA is supported. Theoretically it could be possible
  656. * to support CIF, QVGA and QCIF too. Taking values for ov7670 as a
  657. * base and tweak them empirically could be required.
  658. */
  659. {
  660. .width = VGA_WIDTH,
  661. .height = VGA_HEIGHT,
  662. .com7_bit = COM7_FMT_VGA,
  663. .hstart = 158, /* These values from */
  664. .hstop = 14, /* Omnivision */
  665. .vstart = 14, /* Empirically determined */
  666. .vstop = 494,
  667. .regs = NULL,
  668. }
  669. };
  670. /*
  671. * Store a set of start/stop values into the camera.
  672. */
  673. static int ov7670_set_hw(struct v4l2_subdev *sd, int hstart, int hstop,
  674. int vstart, int vstop)
  675. {
  676. int ret;
  677. unsigned char v;
  678. /*
  679. * Horizontal: 11 bits, top 8 live in hstart and hstop. Bottom 3 of
  680. * hstart are in href[2:0], bottom 3 of hstop in href[5:3]. There is
  681. * a mystery "edge offset" value in the top two bits of href.
  682. */
  683. ret = ov7670_write(sd, REG_HSTART, (hstart >> 3) & 0xff);
  684. ret += ov7670_write(sd, REG_HSTOP, (hstop >> 3) & 0xff);
  685. ret += ov7670_read(sd, REG_HREF, &v);
  686. v = (v & 0xc0) | ((hstop & 0x7) << 3) | (hstart & 0x7);
  687. msleep(10);
  688. ret += ov7670_write(sd, REG_HREF, v);
  689. /*
  690. * Vertical: similar arrangement, but only 10 bits.
  691. */
  692. ret += ov7670_write(sd, REG_VSTART, (vstart >> 2) & 0xff);
  693. ret += ov7670_write(sd, REG_VSTOP, (vstop >> 2) & 0xff);
  694. ret += ov7670_read(sd, REG_VREF, &v);
  695. v = (v & 0xf0) | ((vstop & 0x3) << 2) | (vstart & 0x3);
  696. msleep(10);
  697. ret += ov7670_write(sd, REG_VREF, v);
  698. return ret;
  699. }
  700. static int ov7670_enum_mbus_fmt(struct v4l2_subdev *sd, unsigned index,
  701. enum v4l2_mbus_pixelcode *code)
  702. {
  703. if (index >= N_OV7670_FMTS)
  704. return -EINVAL;
  705. *code = ov7670_formats[index].mbus_code;
  706. return 0;
  707. }
  708. static int ov7670_try_fmt_internal(struct v4l2_subdev *sd,
  709. struct v4l2_mbus_framefmt *fmt,
  710. struct ov7670_format_struct **ret_fmt,
  711. struct ov7670_win_size **ret_wsize)
  712. {
  713. int index, i;
  714. struct ov7670_win_size *wsize;
  715. struct ov7670_info *info = to_state(sd);
  716. unsigned int n_win_sizes = info->devtype->n_win_sizes;
  717. unsigned int win_sizes_limit = n_win_sizes;
  718. for (index = 0; index < N_OV7670_FMTS; index++)
  719. if (ov7670_formats[index].mbus_code == fmt->code)
  720. break;
  721. if (index >= N_OV7670_FMTS) {
  722. /* default to first format */
  723. index = 0;
  724. fmt->code = ov7670_formats[0].mbus_code;
  725. }
  726. if (ret_fmt != NULL)
  727. *ret_fmt = ov7670_formats + index;
  728. /*
  729. * Fields: the OV devices claim to be progressive.
  730. */
  731. fmt->field = V4L2_FIELD_NONE;
  732. /*
  733. * Don't consider values that don't match min_height and min_width
  734. * constraints.
  735. */
  736. if (info->min_width || info->min_height)
  737. for (i = 0; i < n_win_sizes; i++) {
  738. wsize = info->devtype->win_sizes + i;
  739. if (wsize->width < info->min_width ||
  740. wsize->height < info->min_height) {
  741. win_sizes_limit = i;
  742. break;
  743. }
  744. }
  745. /*
  746. * Round requested image size down to the nearest
  747. * we support, but not below the smallest.
  748. */
  749. for (wsize = info->devtype->win_sizes;
  750. wsize < info->devtype->win_sizes + win_sizes_limit; wsize++)
  751. if (fmt->width >= wsize->width && fmt->height >= wsize->height)
  752. break;
  753. if (wsize >= info->devtype->win_sizes + win_sizes_limit)
  754. wsize--; /* Take the smallest one */
  755. if (ret_wsize != NULL)
  756. *ret_wsize = wsize;
  757. /*
  758. * Note the size we'll actually handle.
  759. */
  760. fmt->width = wsize->width;
  761. fmt->height = wsize->height;
  762. fmt->colorspace = ov7670_formats[index].colorspace;
  763. return 0;
  764. }
  765. static int ov7670_try_mbus_fmt(struct v4l2_subdev *sd,
  766. struct v4l2_mbus_framefmt *fmt)
  767. {
  768. return ov7670_try_fmt_internal(sd, fmt, NULL, NULL);
  769. }
  770. /*
  771. * Set a format.
  772. */
  773. static int ov7670_s_mbus_fmt(struct v4l2_subdev *sd,
  774. struct v4l2_mbus_framefmt *fmt)
  775. {
  776. struct ov7670_format_struct *ovfmt;
  777. struct ov7670_win_size *wsize;
  778. struct ov7670_info *info = to_state(sd);
  779. unsigned char com7;
  780. int ret;
  781. ret = ov7670_try_fmt_internal(sd, fmt, &ovfmt, &wsize);
  782. if (ret)
  783. return ret;
  784. /*
  785. * COM7 is a pain in the ass, it doesn't like to be read then
  786. * quickly written afterward. But we have everything we need
  787. * to set it absolutely here, as long as the format-specific
  788. * register sets list it first.
  789. */
  790. com7 = ovfmt->regs[0].value;
  791. com7 |= wsize->com7_bit;
  792. ov7670_write(sd, REG_COM7, com7);
  793. /*
  794. * Now write the rest of the array. Also store start/stops
  795. */
  796. ov7670_write_array(sd, ovfmt->regs + 1);
  797. ov7670_set_hw(sd, wsize->hstart, wsize->hstop, wsize->vstart,
  798. wsize->vstop);
  799. ret = 0;
  800. if (wsize->regs)
  801. ret = ov7670_write_array(sd, wsize->regs);
  802. info->fmt = ovfmt;
  803. /*
  804. * If we're running RGB565, we must rewrite clkrc after setting
  805. * the other parameters or the image looks poor. If we're *not*
  806. * doing RGB565, we must not rewrite clkrc or the image looks
  807. * *really* poor.
  808. *
  809. * (Update) Now that we retain clkrc state, we should be able
  810. * to write it unconditionally, and that will make the frame
  811. * rate persistent too.
  812. */
  813. if (ret == 0)
  814. ret = ov7670_write(sd, REG_CLKRC, info->clkrc);
  815. return 0;
  816. }
  817. /*
  818. * Implement G/S_PARM. There is a "high quality" mode we could try
  819. * to do someday; for now, we just do the frame rate tweak.
  820. */
  821. static int ov7670_g_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *parms)
  822. {
  823. struct v4l2_captureparm *cp = &parms->parm.capture;
  824. struct ov7670_info *info = to_state(sd);
  825. if (parms->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  826. return -EINVAL;
  827. memset(cp, 0, sizeof(struct v4l2_captureparm));
  828. cp->capability = V4L2_CAP_TIMEPERFRAME;
  829. cp->timeperframe.numerator = 1;
  830. cp->timeperframe.denominator = info->clock_speed;
  831. if ((info->clkrc & CLK_EXT) == 0 && (info->clkrc & CLK_SCALE) > 1)
  832. cp->timeperframe.denominator /= (info->clkrc & CLK_SCALE);
  833. return 0;
  834. }
  835. static int ov7670_s_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *parms)
  836. {
  837. struct v4l2_captureparm *cp = &parms->parm.capture;
  838. struct v4l2_fract *tpf = &cp->timeperframe;
  839. struct ov7670_info *info = to_state(sd);
  840. int div;
  841. if (parms->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  842. return -EINVAL;
  843. if (cp->extendedmode != 0)
  844. return -EINVAL;
  845. if (tpf->numerator == 0 || tpf->denominator == 0)
  846. div = 1; /* Reset to full rate */
  847. else
  848. div = (tpf->numerator * info->clock_speed) / tpf->denominator;
  849. if (div == 0)
  850. div = 1;
  851. else if (div > CLK_SCALE)
  852. div = CLK_SCALE;
  853. info->clkrc = (info->clkrc & 0x80) | div;
  854. tpf->numerator = 1;
  855. tpf->denominator = info->clock_speed / div;
  856. return ov7670_write(sd, REG_CLKRC, info->clkrc);
  857. }
  858. /*
  859. * Frame intervals. Since frame rates are controlled with the clock
  860. * divider, we can only do 30/n for integer n values. So no continuous
  861. * or stepwise options. Here we just pick a handful of logical values.
  862. */
  863. static int ov7670_frame_rates[] = { 30, 15, 10, 5, 1 };
  864. static int ov7670_enum_frameintervals(struct v4l2_subdev *sd,
  865. struct v4l2_frmivalenum *interval)
  866. {
  867. if (interval->index >= ARRAY_SIZE(ov7670_frame_rates))
  868. return -EINVAL;
  869. interval->type = V4L2_FRMIVAL_TYPE_DISCRETE;
  870. interval->discrete.numerator = 1;
  871. interval->discrete.denominator = ov7670_frame_rates[interval->index];
  872. return 0;
  873. }
  874. /*
  875. * Frame size enumeration
  876. */
  877. static int ov7670_enum_framesizes(struct v4l2_subdev *sd,
  878. struct v4l2_frmsizeenum *fsize)
  879. {
  880. struct ov7670_info *info = to_state(sd);
  881. int i;
  882. int num_valid = -1;
  883. __u32 index = fsize->index;
  884. unsigned int n_win_sizes = info->devtype->n_win_sizes;
  885. /*
  886. * If a minimum width/height was requested, filter out the capture
  887. * windows that fall outside that.
  888. */
  889. for (i = 0; i < n_win_sizes; i++) {
  890. struct ov7670_win_size *win = &info->devtype->win_sizes[index];
  891. if (info->min_width && win->width < info->min_width)
  892. continue;
  893. if (info->min_height && win->height < info->min_height)
  894. continue;
  895. if (index == ++num_valid) {
  896. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  897. fsize->discrete.width = win->width;
  898. fsize->discrete.height = win->height;
  899. return 0;
  900. }
  901. }
  902. return -EINVAL;
  903. }
  904. /*
  905. * Code for dealing with controls.
  906. */
  907. static int ov7670_store_cmatrix(struct v4l2_subdev *sd,
  908. int matrix[CMATRIX_LEN])
  909. {
  910. int i, ret;
  911. unsigned char signbits = 0;
  912. /*
  913. * Weird crap seems to exist in the upper part of
  914. * the sign bits register, so let's preserve it.
  915. */
  916. ret = ov7670_read(sd, REG_CMATRIX_SIGN, &signbits);
  917. signbits &= 0xc0;
  918. for (i = 0; i < CMATRIX_LEN; i++) {
  919. unsigned char raw;
  920. if (matrix[i] < 0) {
  921. signbits |= (1 << i);
  922. if (matrix[i] < -255)
  923. raw = 0xff;
  924. else
  925. raw = (-1 * matrix[i]) & 0xff;
  926. }
  927. else {
  928. if (matrix[i] > 255)
  929. raw = 0xff;
  930. else
  931. raw = matrix[i] & 0xff;
  932. }
  933. ret += ov7670_write(sd, REG_CMATRIX_BASE + i, raw);
  934. }
  935. ret += ov7670_write(sd, REG_CMATRIX_SIGN, signbits);
  936. return ret;
  937. }
  938. /*
  939. * Hue also requires messing with the color matrix. It also requires
  940. * trig functions, which tend not to be well supported in the kernel.
  941. * So here is a simple table of sine values, 0-90 degrees, in steps
  942. * of five degrees. Values are multiplied by 1000.
  943. *
  944. * The following naive approximate trig functions require an argument
  945. * carefully limited to -180 <= theta <= 180.
  946. */
  947. #define SIN_STEP 5
  948. static const int ov7670_sin_table[] = {
  949. 0, 87, 173, 258, 342, 422,
  950. 499, 573, 642, 707, 766, 819,
  951. 866, 906, 939, 965, 984, 996,
  952. 1000
  953. };
  954. static int ov7670_sine(int theta)
  955. {
  956. int chs = 1;
  957. int sine;
  958. if (theta < 0) {
  959. theta = -theta;
  960. chs = -1;
  961. }
  962. if (theta <= 90)
  963. sine = ov7670_sin_table[theta/SIN_STEP];
  964. else {
  965. theta -= 90;
  966. sine = 1000 - ov7670_sin_table[theta/SIN_STEP];
  967. }
  968. return sine*chs;
  969. }
  970. static int ov7670_cosine(int theta)
  971. {
  972. theta = 90 - theta;
  973. if (theta > 180)
  974. theta -= 360;
  975. else if (theta < -180)
  976. theta += 360;
  977. return ov7670_sine(theta);
  978. }
  979. static void ov7670_calc_cmatrix(struct ov7670_info *info,
  980. int matrix[CMATRIX_LEN])
  981. {
  982. int i;
  983. /*
  984. * Apply the current saturation setting first.
  985. */
  986. for (i = 0; i < CMATRIX_LEN; i++)
  987. matrix[i] = (info->fmt->cmatrix[i]*info->sat) >> 7;
  988. /*
  989. * Then, if need be, rotate the hue value.
  990. */
  991. if (info->hue != 0) {
  992. int sinth, costh, tmpmatrix[CMATRIX_LEN];
  993. memcpy(tmpmatrix, matrix, CMATRIX_LEN*sizeof(int));
  994. sinth = ov7670_sine(info->hue);
  995. costh = ov7670_cosine(info->hue);
  996. matrix[0] = (matrix[3]*sinth + matrix[0]*costh)/1000;
  997. matrix[1] = (matrix[4]*sinth + matrix[1]*costh)/1000;
  998. matrix[2] = (matrix[5]*sinth + matrix[2]*costh)/1000;
  999. matrix[3] = (matrix[3]*costh - matrix[0]*sinth)/1000;
  1000. matrix[4] = (matrix[4]*costh - matrix[1]*sinth)/1000;
  1001. matrix[5] = (matrix[5]*costh - matrix[2]*sinth)/1000;
  1002. }
  1003. }
  1004. static int ov7670_s_sat(struct v4l2_subdev *sd, int value)
  1005. {
  1006. struct ov7670_info *info = to_state(sd);
  1007. int matrix[CMATRIX_LEN];
  1008. int ret;
  1009. info->sat = value;
  1010. ov7670_calc_cmatrix(info, matrix);
  1011. ret = ov7670_store_cmatrix(sd, matrix);
  1012. return ret;
  1013. }
  1014. static int ov7670_g_sat(struct v4l2_subdev *sd, __s32 *value)
  1015. {
  1016. struct ov7670_info *info = to_state(sd);
  1017. *value = info->sat;
  1018. return 0;
  1019. }
  1020. static int ov7670_s_hue(struct v4l2_subdev *sd, int value)
  1021. {
  1022. struct ov7670_info *info = to_state(sd);
  1023. int matrix[CMATRIX_LEN];
  1024. int ret;
  1025. if (value < -180 || value > 180)
  1026. return -EINVAL;
  1027. info->hue = value;
  1028. ov7670_calc_cmatrix(info, matrix);
  1029. ret = ov7670_store_cmatrix(sd, matrix);
  1030. return ret;
  1031. }
  1032. static int ov7670_g_hue(struct v4l2_subdev *sd, __s32 *value)
  1033. {
  1034. struct ov7670_info *info = to_state(sd);
  1035. *value = info->hue;
  1036. return 0;
  1037. }
  1038. /*
  1039. * Some weird registers seem to store values in a sign/magnitude format!
  1040. */
  1041. static unsigned char ov7670_sm_to_abs(unsigned char v)
  1042. {
  1043. if ((v & 0x80) == 0)
  1044. return v + 128;
  1045. return 128 - (v & 0x7f);
  1046. }
  1047. static unsigned char ov7670_abs_to_sm(unsigned char v)
  1048. {
  1049. if (v > 127)
  1050. return v & 0x7f;
  1051. return (128 - v) | 0x80;
  1052. }
  1053. static int ov7670_s_brightness(struct v4l2_subdev *sd, int value)
  1054. {
  1055. unsigned char com8 = 0, v;
  1056. int ret;
  1057. ov7670_read(sd, REG_COM8, &com8);
  1058. com8 &= ~COM8_AEC;
  1059. ov7670_write(sd, REG_COM8, com8);
  1060. v = ov7670_abs_to_sm(value);
  1061. ret = ov7670_write(sd, REG_BRIGHT, v);
  1062. return ret;
  1063. }
  1064. static int ov7670_g_brightness(struct v4l2_subdev *sd, __s32 *value)
  1065. {
  1066. unsigned char v = 0;
  1067. int ret = ov7670_read(sd, REG_BRIGHT, &v);
  1068. *value = ov7670_sm_to_abs(v);
  1069. return ret;
  1070. }
  1071. static int ov7670_s_contrast(struct v4l2_subdev *sd, int value)
  1072. {
  1073. return ov7670_write(sd, REG_CONTRAS, (unsigned char) value);
  1074. }
  1075. static int ov7670_g_contrast(struct v4l2_subdev *sd, __s32 *value)
  1076. {
  1077. unsigned char v = 0;
  1078. int ret = ov7670_read(sd, REG_CONTRAS, &v);
  1079. *value = v;
  1080. return ret;
  1081. }
  1082. static int ov7670_g_hflip(struct v4l2_subdev *sd, __s32 *value)
  1083. {
  1084. int ret;
  1085. unsigned char v = 0;
  1086. ret = ov7670_read(sd, REG_MVFP, &v);
  1087. *value = (v & MVFP_MIRROR) == MVFP_MIRROR;
  1088. return ret;
  1089. }
  1090. static int ov7670_s_hflip(struct v4l2_subdev *sd, int value)
  1091. {
  1092. unsigned char v = 0;
  1093. int ret;
  1094. ret = ov7670_read(sd, REG_MVFP, &v);
  1095. if (value)
  1096. v |= MVFP_MIRROR;
  1097. else
  1098. v &= ~MVFP_MIRROR;
  1099. msleep(10); /* FIXME */
  1100. ret += ov7670_write(sd, REG_MVFP, v);
  1101. return ret;
  1102. }
  1103. static int ov7670_g_vflip(struct v4l2_subdev *sd, __s32 *value)
  1104. {
  1105. int ret;
  1106. unsigned char v = 0;
  1107. ret = ov7670_read(sd, REG_MVFP, &v);
  1108. *value = (v & MVFP_FLIP) == MVFP_FLIP;
  1109. return ret;
  1110. }
  1111. static int ov7670_s_vflip(struct v4l2_subdev *sd, int value)
  1112. {
  1113. unsigned char v = 0;
  1114. int ret;
  1115. ret = ov7670_read(sd, REG_MVFP, &v);
  1116. if (value)
  1117. v |= MVFP_FLIP;
  1118. else
  1119. v &= ~MVFP_FLIP;
  1120. msleep(10); /* FIXME */
  1121. ret += ov7670_write(sd, REG_MVFP, v);
  1122. return ret;
  1123. }
  1124. /*
  1125. * GAIN is split between REG_GAIN and REG_VREF[7:6]. If one believes
  1126. * the data sheet, the VREF parts should be the most significant, but
  1127. * experience shows otherwise. There seems to be little value in
  1128. * messing with the VREF bits, so we leave them alone.
  1129. */
  1130. static int ov7670_g_gain(struct v4l2_subdev *sd, __s32 *value)
  1131. {
  1132. int ret;
  1133. unsigned char gain;
  1134. ret = ov7670_read(sd, REG_GAIN, &gain);
  1135. *value = gain;
  1136. return ret;
  1137. }
  1138. static int ov7670_s_gain(struct v4l2_subdev *sd, int value)
  1139. {
  1140. int ret;
  1141. unsigned char com8;
  1142. ret = ov7670_write(sd, REG_GAIN, value & 0xff);
  1143. /* Have to turn off AGC as well */
  1144. if (ret == 0) {
  1145. ret = ov7670_read(sd, REG_COM8, &com8);
  1146. ret = ov7670_write(sd, REG_COM8, com8 & ~COM8_AGC);
  1147. }
  1148. return ret;
  1149. }
  1150. /*
  1151. * Tweak autogain.
  1152. */
  1153. static int ov7670_g_autogain(struct v4l2_subdev *sd, __s32 *value)
  1154. {
  1155. int ret;
  1156. unsigned char com8;
  1157. ret = ov7670_read(sd, REG_COM8, &com8);
  1158. *value = (com8 & COM8_AGC) != 0;
  1159. return ret;
  1160. }
  1161. static int ov7670_s_autogain(struct v4l2_subdev *sd, int value)
  1162. {
  1163. int ret;
  1164. unsigned char com8;
  1165. ret = ov7670_read(sd, REG_COM8, &com8);
  1166. if (ret == 0) {
  1167. if (value)
  1168. com8 |= COM8_AGC;
  1169. else
  1170. com8 &= ~COM8_AGC;
  1171. ret = ov7670_write(sd, REG_COM8, com8);
  1172. }
  1173. return ret;
  1174. }
  1175. /*
  1176. * Exposure is spread all over the place: top 6 bits in AECHH, middle
  1177. * 8 in AECH, and two stashed in COM1 just for the hell of it.
  1178. */
  1179. static int ov7670_g_exp(struct v4l2_subdev *sd, __s32 *value)
  1180. {
  1181. int ret;
  1182. unsigned char com1, aech, aechh;
  1183. ret = ov7670_read(sd, REG_COM1, &com1) +
  1184. ov7670_read(sd, REG_AECH, &aech) +
  1185. ov7670_read(sd, REG_AECHH, &aechh);
  1186. *value = ((aechh & 0x3f) << 10) | (aech << 2) | (com1 & 0x03);
  1187. return ret;
  1188. }
  1189. static int ov7670_s_exp(struct v4l2_subdev *sd, int value)
  1190. {
  1191. int ret;
  1192. unsigned char com1, com8, aech, aechh;
  1193. ret = ov7670_read(sd, REG_COM1, &com1) +
  1194. ov7670_read(sd, REG_COM8, &com8);
  1195. ov7670_read(sd, REG_AECHH, &aechh);
  1196. if (ret)
  1197. return ret;
  1198. com1 = (com1 & 0xfc) | (value & 0x03);
  1199. aech = (value >> 2) & 0xff;
  1200. aechh = (aechh & 0xc0) | ((value >> 10) & 0x3f);
  1201. ret = ov7670_write(sd, REG_COM1, com1) +
  1202. ov7670_write(sd, REG_AECH, aech) +
  1203. ov7670_write(sd, REG_AECHH, aechh);
  1204. /* Have to turn off AEC as well */
  1205. if (ret == 0)
  1206. ret = ov7670_write(sd, REG_COM8, com8 & ~COM8_AEC);
  1207. return ret;
  1208. }
  1209. /*
  1210. * Tweak autoexposure.
  1211. */
  1212. static int ov7670_g_autoexp(struct v4l2_subdev *sd, __s32 *value)
  1213. {
  1214. int ret;
  1215. unsigned char com8;
  1216. enum v4l2_exposure_auto_type *atype = (enum v4l2_exposure_auto_type *) value;
  1217. ret = ov7670_read(sd, REG_COM8, &com8);
  1218. if (com8 & COM8_AEC)
  1219. *atype = V4L2_EXPOSURE_AUTO;
  1220. else
  1221. *atype = V4L2_EXPOSURE_MANUAL;
  1222. return ret;
  1223. }
  1224. static int ov7670_s_autoexp(struct v4l2_subdev *sd,
  1225. enum v4l2_exposure_auto_type value)
  1226. {
  1227. int ret;
  1228. unsigned char com8;
  1229. ret = ov7670_read(sd, REG_COM8, &com8);
  1230. if (ret == 0) {
  1231. if (value == V4L2_EXPOSURE_AUTO)
  1232. com8 |= COM8_AEC;
  1233. else
  1234. com8 &= ~COM8_AEC;
  1235. ret = ov7670_write(sd, REG_COM8, com8);
  1236. }
  1237. return ret;
  1238. }
  1239. static int ov7670_queryctrl(struct v4l2_subdev *sd,
  1240. struct v4l2_queryctrl *qc)
  1241. {
  1242. /* Fill in min, max, step and default value for these controls. */
  1243. switch (qc->id) {
  1244. case V4L2_CID_BRIGHTNESS:
  1245. return v4l2_ctrl_query_fill(qc, 0, 255, 1, 128);
  1246. case V4L2_CID_CONTRAST:
  1247. return v4l2_ctrl_query_fill(qc, 0, 127, 1, 64);
  1248. case V4L2_CID_VFLIP:
  1249. case V4L2_CID_HFLIP:
  1250. return v4l2_ctrl_query_fill(qc, 0, 1, 1, 0);
  1251. case V4L2_CID_SATURATION:
  1252. return v4l2_ctrl_query_fill(qc, 0, 256, 1, 128);
  1253. case V4L2_CID_HUE:
  1254. return v4l2_ctrl_query_fill(qc, -180, 180, 5, 0);
  1255. case V4L2_CID_GAIN:
  1256. return v4l2_ctrl_query_fill(qc, 0, 255, 1, 128);
  1257. case V4L2_CID_AUTOGAIN:
  1258. return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
  1259. case V4L2_CID_EXPOSURE:
  1260. return v4l2_ctrl_query_fill(qc, 0, 65535, 1, 500);
  1261. case V4L2_CID_EXPOSURE_AUTO:
  1262. return v4l2_ctrl_query_fill(qc, 0, 1, 1, 0);
  1263. }
  1264. return -EINVAL;
  1265. }
  1266. static int ov7670_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  1267. {
  1268. switch (ctrl->id) {
  1269. case V4L2_CID_BRIGHTNESS:
  1270. return ov7670_g_brightness(sd, &ctrl->value);
  1271. case V4L2_CID_CONTRAST:
  1272. return ov7670_g_contrast(sd, &ctrl->value);
  1273. case V4L2_CID_SATURATION:
  1274. return ov7670_g_sat(sd, &ctrl->value);
  1275. case V4L2_CID_HUE:
  1276. return ov7670_g_hue(sd, &ctrl->value);
  1277. case V4L2_CID_VFLIP:
  1278. return ov7670_g_vflip(sd, &ctrl->value);
  1279. case V4L2_CID_HFLIP:
  1280. return ov7670_g_hflip(sd, &ctrl->value);
  1281. case V4L2_CID_GAIN:
  1282. return ov7670_g_gain(sd, &ctrl->value);
  1283. case V4L2_CID_AUTOGAIN:
  1284. return ov7670_g_autogain(sd, &ctrl->value);
  1285. case V4L2_CID_EXPOSURE:
  1286. return ov7670_g_exp(sd, &ctrl->value);
  1287. case V4L2_CID_EXPOSURE_AUTO:
  1288. return ov7670_g_autoexp(sd, &ctrl->value);
  1289. }
  1290. return -EINVAL;
  1291. }
  1292. static int ov7670_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  1293. {
  1294. switch (ctrl->id) {
  1295. case V4L2_CID_BRIGHTNESS:
  1296. return ov7670_s_brightness(sd, ctrl->value);
  1297. case V4L2_CID_CONTRAST:
  1298. return ov7670_s_contrast(sd, ctrl->value);
  1299. case V4L2_CID_SATURATION:
  1300. return ov7670_s_sat(sd, ctrl->value);
  1301. case V4L2_CID_HUE:
  1302. return ov7670_s_hue(sd, ctrl->value);
  1303. case V4L2_CID_VFLIP:
  1304. return ov7670_s_vflip(sd, ctrl->value);
  1305. case V4L2_CID_HFLIP:
  1306. return ov7670_s_hflip(sd, ctrl->value);
  1307. case V4L2_CID_GAIN:
  1308. return ov7670_s_gain(sd, ctrl->value);
  1309. case V4L2_CID_AUTOGAIN:
  1310. return ov7670_s_autogain(sd, ctrl->value);
  1311. case V4L2_CID_EXPOSURE:
  1312. return ov7670_s_exp(sd, ctrl->value);
  1313. case V4L2_CID_EXPOSURE_AUTO:
  1314. return ov7670_s_autoexp(sd,
  1315. (enum v4l2_exposure_auto_type) ctrl->value);
  1316. }
  1317. return -EINVAL;
  1318. }
  1319. static int ov7670_g_chip_ident(struct v4l2_subdev *sd,
  1320. struct v4l2_dbg_chip_ident *chip)
  1321. {
  1322. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1323. return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_OV7670, 0);
  1324. }
  1325. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1326. static int ov7670_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
  1327. {
  1328. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1329. unsigned char val = 0;
  1330. int ret;
  1331. if (!v4l2_chip_match_i2c_client(client, &reg->match))
  1332. return -EINVAL;
  1333. if (!capable(CAP_SYS_ADMIN))
  1334. return -EPERM;
  1335. ret = ov7670_read(sd, reg->reg & 0xff, &val);
  1336. reg->val = val;
  1337. reg->size = 1;
  1338. return ret;
  1339. }
  1340. static int ov7670_s_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
  1341. {
  1342. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1343. if (!v4l2_chip_match_i2c_client(client, &reg->match))
  1344. return -EINVAL;
  1345. if (!capable(CAP_SYS_ADMIN))
  1346. return -EPERM;
  1347. ov7670_write(sd, reg->reg & 0xff, reg->val & 0xff);
  1348. return 0;
  1349. }
  1350. #endif
  1351. /* ----------------------------------------------------------------------- */
  1352. static const struct v4l2_subdev_core_ops ov7670_core_ops = {
  1353. .g_chip_ident = ov7670_g_chip_ident,
  1354. .g_ctrl = ov7670_g_ctrl,
  1355. .s_ctrl = ov7670_s_ctrl,
  1356. .queryctrl = ov7670_queryctrl,
  1357. .reset = ov7670_reset,
  1358. .init = ov7670_init,
  1359. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1360. .g_register = ov7670_g_register,
  1361. .s_register = ov7670_s_register,
  1362. #endif
  1363. };
  1364. static const struct v4l2_subdev_video_ops ov7670_video_ops = {
  1365. .enum_mbus_fmt = ov7670_enum_mbus_fmt,
  1366. .try_mbus_fmt = ov7670_try_mbus_fmt,
  1367. .s_mbus_fmt = ov7670_s_mbus_fmt,
  1368. .s_parm = ov7670_s_parm,
  1369. .g_parm = ov7670_g_parm,
  1370. .enum_frameintervals = ov7670_enum_frameintervals,
  1371. .enum_framesizes = ov7670_enum_framesizes,
  1372. };
  1373. static const struct v4l2_subdev_ops ov7670_ops = {
  1374. .core = &ov7670_core_ops,
  1375. .video = &ov7670_video_ops,
  1376. };
  1377. /* ----------------------------------------------------------------------- */
  1378. static const struct ov7670_devtype ov7670_devdata[] = {
  1379. [MODEL_OV7670] = {
  1380. .win_sizes = ov7670_win_sizes,
  1381. .n_win_sizes = ARRAY_SIZE(ov7670_win_sizes),
  1382. },
  1383. [MODEL_OV7675] = {
  1384. .win_sizes = ov7675_win_sizes,
  1385. .n_win_sizes = ARRAY_SIZE(ov7675_win_sizes),
  1386. },
  1387. };
  1388. static int ov7670_probe(struct i2c_client *client,
  1389. const struct i2c_device_id *id)
  1390. {
  1391. struct v4l2_subdev *sd;
  1392. struct ov7670_info *info;
  1393. int ret;
  1394. info = kzalloc(sizeof(struct ov7670_info), GFP_KERNEL);
  1395. if (info == NULL)
  1396. return -ENOMEM;
  1397. sd = &info->sd;
  1398. v4l2_i2c_subdev_init(sd, client, &ov7670_ops);
  1399. info->clock_speed = 30; /* default: a guess */
  1400. if (client->dev.platform_data) {
  1401. struct ov7670_config *config = client->dev.platform_data;
  1402. /*
  1403. * Must apply configuration before initializing device, because it
  1404. * selects I/O method.
  1405. */
  1406. info->min_width = config->min_width;
  1407. info->min_height = config->min_height;
  1408. info->use_smbus = config->use_smbus;
  1409. if (config->clock_speed)
  1410. info->clock_speed = config->clock_speed;
  1411. }
  1412. /* Make sure it's an ov7670 */
  1413. ret = ov7670_detect(sd);
  1414. if (ret) {
  1415. v4l_dbg(1, debug, client,
  1416. "chip found @ 0x%x (%s) is not an ov7670 chip.\n",
  1417. client->addr << 1, client->adapter->name);
  1418. kfree(info);
  1419. return ret;
  1420. }
  1421. v4l_info(client, "chip found @ 0x%02x (%s)\n",
  1422. client->addr << 1, client->adapter->name);
  1423. info->devtype = &ov7670_devdata[id->driver_data];
  1424. info->fmt = &ov7670_formats[0];
  1425. info->sat = 128; /* Review this */
  1426. info->clkrc = info->clock_speed / 30;
  1427. return 0;
  1428. }
  1429. static int ov7670_remove(struct i2c_client *client)
  1430. {
  1431. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1432. v4l2_device_unregister_subdev(sd);
  1433. kfree(to_state(sd));
  1434. return 0;
  1435. }
  1436. static const struct i2c_device_id ov7670_id[] = {
  1437. { "ov7670", MODEL_OV7670 },
  1438. { "ov7675", MODEL_OV7675 },
  1439. { }
  1440. };
  1441. MODULE_DEVICE_TABLE(i2c, ov7670_id);
  1442. static struct i2c_driver ov7670_driver = {
  1443. .driver = {
  1444. .owner = THIS_MODULE,
  1445. .name = "ov7670",
  1446. },
  1447. .probe = ov7670_probe,
  1448. .remove = ov7670_remove,
  1449. .id_table = ov7670_id,
  1450. };
  1451. module_i2c_driver(ov7670_driver);