ov7670.c 46 KB

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