ov7670.c 45 KB

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