ov519.c 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915
  1. /**
  2. * OV519 driver
  3. *
  4. * Copyright (C) 2008 Jean-Francois Moine (http://moinejf.free.fr)
  5. *
  6. * This module is adapted from the ov51x-jpeg package, which itself
  7. * was adapted from the ov511 driver.
  8. *
  9. * Original copyright for the ov511 driver is:
  10. *
  11. * Copyright (c) 1999-2004 Mark W. McClelland
  12. * Support for OV519, OV8610 Copyright (c) 2003 Joerg Heckenbach
  13. *
  14. * ov51x-jpeg original copyright is:
  15. *
  16. * Copyright (c) 2004-2007 Romain Beauxis <toots@rastageeks.org>
  17. * Support for OV7670 sensors was contributed by Sam Skipsey <aoanla@yahoo.com>
  18. *
  19. * This program is free software; you can redistribute it and/or modify
  20. * it under the terms of the GNU General Public License as published by
  21. * the Free Software Foundation; either version 2 of the License, or
  22. * any later version.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU General Public License
  30. * along with this program; if not, write to the Free Software
  31. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  32. *
  33. */
  34. #define MODULE_NAME "ov519"
  35. #include "gspca.h"
  36. MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
  37. MODULE_DESCRIPTION("OV519 USB Camera Driver");
  38. MODULE_LICENSE("GPL");
  39. /* global parameters */
  40. static int frame_rate;
  41. /* Number of times to retry a failed I2C transaction. Increase this if you
  42. * are getting "Failed to read sensor ID..." */
  43. static int i2c_detect_tries = 10;
  44. /* ov519 device descriptor */
  45. struct sd {
  46. struct gspca_dev gspca_dev; /* !! must be the first item */
  47. __u8 packet_nr;
  48. char bridge;
  49. #define BRIDGE_OV511 0
  50. #define BRIDGE_OV511PLUS 1
  51. #define BRIDGE_OV518 2
  52. #define BRIDGE_OV518PLUS 3
  53. #define BRIDGE_OV519 4
  54. #define BRIDGE_MASK 7
  55. char invert_led;
  56. #define BRIDGE_INVERT_LED 8
  57. /* Determined by sensor type */
  58. __u8 sif;
  59. __u8 brightness;
  60. __u8 contrast;
  61. __u8 colors;
  62. __u8 hflip;
  63. __u8 vflip;
  64. __u8 autobrightness;
  65. __u8 freq;
  66. __u8 stopped; /* Streaming is temporarily paused */
  67. __u8 frame_rate; /* current Framerate (OV519 only) */
  68. __u8 clockdiv; /* clockdiv override for OV519 only */
  69. char sensor; /* Type of image sensor chip (SEN_*) */
  70. #define SEN_UNKNOWN 0
  71. #define SEN_OV6620 1
  72. #define SEN_OV6630 2
  73. #define SEN_OV66308AF 3
  74. #define SEN_OV7610 4
  75. #define SEN_OV7620 5
  76. #define SEN_OV7640 6
  77. #define SEN_OV7670 7
  78. #define SEN_OV76BE 8
  79. #define SEN_OV8610 9
  80. };
  81. /* V4L2 controls supported by the driver */
  82. static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
  83. static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
  84. static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
  85. static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
  86. static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val);
  87. static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val);
  88. static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val);
  89. static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val);
  90. static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val);
  91. static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val);
  92. static int sd_setautobrightness(struct gspca_dev *gspca_dev, __s32 val);
  93. static int sd_getautobrightness(struct gspca_dev *gspca_dev, __s32 *val);
  94. static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val);
  95. static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val);
  96. static void setbrightness(struct gspca_dev *gspca_dev);
  97. static void setcontrast(struct gspca_dev *gspca_dev);
  98. static void setcolors(struct gspca_dev *gspca_dev);
  99. static void setautobrightness(struct sd *sd);
  100. static void setfreq(struct sd *sd);
  101. static const struct ctrl sd_ctrls[] = {
  102. {
  103. {
  104. .id = V4L2_CID_BRIGHTNESS,
  105. .type = V4L2_CTRL_TYPE_INTEGER,
  106. .name = "Brightness",
  107. .minimum = 0,
  108. .maximum = 255,
  109. .step = 1,
  110. #define BRIGHTNESS_DEF 127
  111. .default_value = BRIGHTNESS_DEF,
  112. },
  113. .set = sd_setbrightness,
  114. .get = sd_getbrightness,
  115. },
  116. {
  117. {
  118. .id = V4L2_CID_CONTRAST,
  119. .type = V4L2_CTRL_TYPE_INTEGER,
  120. .name = "Contrast",
  121. .minimum = 0,
  122. .maximum = 255,
  123. .step = 1,
  124. #define CONTRAST_DEF 127
  125. .default_value = CONTRAST_DEF,
  126. },
  127. .set = sd_setcontrast,
  128. .get = sd_getcontrast,
  129. },
  130. {
  131. {
  132. .id = V4L2_CID_SATURATION,
  133. .type = V4L2_CTRL_TYPE_INTEGER,
  134. .name = "Color",
  135. .minimum = 0,
  136. .maximum = 255,
  137. .step = 1,
  138. #define COLOR_DEF 127
  139. .default_value = COLOR_DEF,
  140. },
  141. .set = sd_setcolors,
  142. .get = sd_getcolors,
  143. },
  144. /* The flip controls work with ov7670 only */
  145. #define HFLIP_IDX 3
  146. {
  147. {
  148. .id = V4L2_CID_HFLIP,
  149. .type = V4L2_CTRL_TYPE_BOOLEAN,
  150. .name = "Mirror",
  151. .minimum = 0,
  152. .maximum = 1,
  153. .step = 1,
  154. #define HFLIP_DEF 0
  155. .default_value = HFLIP_DEF,
  156. },
  157. .set = sd_sethflip,
  158. .get = sd_gethflip,
  159. },
  160. #define VFLIP_IDX 4
  161. {
  162. {
  163. .id = V4L2_CID_VFLIP,
  164. .type = V4L2_CTRL_TYPE_BOOLEAN,
  165. .name = "Vflip",
  166. .minimum = 0,
  167. .maximum = 1,
  168. .step = 1,
  169. #define VFLIP_DEF 0
  170. .default_value = VFLIP_DEF,
  171. },
  172. .set = sd_setvflip,
  173. .get = sd_getvflip,
  174. },
  175. #define AUTOBRIGHT_IDX 5
  176. {
  177. {
  178. .id = V4L2_CID_AUTOBRIGHTNESS,
  179. .type = V4L2_CTRL_TYPE_BOOLEAN,
  180. .name = "Auto Brightness",
  181. .minimum = 0,
  182. .maximum = 1,
  183. .step = 1,
  184. #define AUTOBRIGHT_DEF 1
  185. .default_value = AUTOBRIGHT_DEF,
  186. },
  187. .set = sd_setautobrightness,
  188. .get = sd_getautobrightness,
  189. },
  190. #define FREQ_IDX 6
  191. {
  192. {
  193. .id = V4L2_CID_POWER_LINE_FREQUENCY,
  194. .type = V4L2_CTRL_TYPE_MENU,
  195. .name = "Light frequency filter",
  196. .minimum = 0,
  197. .maximum = 2, /* 0: 0, 1: 50Hz, 2:60Hz */
  198. .step = 1,
  199. #define FREQ_DEF 0
  200. .default_value = FREQ_DEF,
  201. },
  202. .set = sd_setfreq,
  203. .get = sd_getfreq,
  204. },
  205. #define OV7670_FREQ_IDX 7
  206. {
  207. {
  208. .id = V4L2_CID_POWER_LINE_FREQUENCY,
  209. .type = V4L2_CTRL_TYPE_MENU,
  210. .name = "Light frequency filter",
  211. .minimum = 0,
  212. .maximum = 3, /* 0: 0, 1: 50Hz, 2:60Hz 3: Auto Hz */
  213. .step = 1,
  214. #define OV7670_FREQ_DEF 3
  215. .default_value = OV7670_FREQ_DEF,
  216. },
  217. .set = sd_setfreq,
  218. .get = sd_getfreq,
  219. },
  220. };
  221. static const struct v4l2_pix_format ov519_vga_mode[] = {
  222. {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  223. .bytesperline = 320,
  224. .sizeimage = 320 * 240 * 3 / 8 + 590,
  225. .colorspace = V4L2_COLORSPACE_JPEG,
  226. .priv = 1},
  227. {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  228. .bytesperline = 640,
  229. .sizeimage = 640 * 480 * 3 / 8 + 590,
  230. .colorspace = V4L2_COLORSPACE_JPEG,
  231. .priv = 0},
  232. };
  233. static const struct v4l2_pix_format ov519_sif_mode[] = {
  234. {160, 120, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  235. .bytesperline = 160,
  236. .sizeimage = 160 * 120 * 3 / 8 + 590,
  237. .colorspace = V4L2_COLORSPACE_JPEG,
  238. .priv = 3},
  239. {176, 144, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  240. .bytesperline = 176,
  241. .sizeimage = 176 * 144 * 3 / 8 + 590,
  242. .colorspace = V4L2_COLORSPACE_JPEG,
  243. .priv = 1},
  244. {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  245. .bytesperline = 320,
  246. .sizeimage = 320 * 240 * 3 / 8 + 590,
  247. .colorspace = V4L2_COLORSPACE_JPEG,
  248. .priv = 2},
  249. {352, 288, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  250. .bytesperline = 352,
  251. .sizeimage = 352 * 288 * 3 / 8 + 590,
  252. .colorspace = V4L2_COLORSPACE_JPEG,
  253. .priv = 0},
  254. };
  255. static const struct v4l2_pix_format ov518_vga_mode[] = {
  256. {320, 240, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
  257. .bytesperline = 320,
  258. .sizeimage = 320 * 240 * 3 / 8 + 590,
  259. .colorspace = V4L2_COLORSPACE_JPEG,
  260. .priv = 1},
  261. {640, 480, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
  262. .bytesperline = 640,
  263. .sizeimage = 640 * 480 * 3 / 8 + 590,
  264. .colorspace = V4L2_COLORSPACE_JPEG,
  265. .priv = 0},
  266. };
  267. static const struct v4l2_pix_format ov518_sif_mode[] = {
  268. {160, 120, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
  269. .bytesperline = 160,
  270. .sizeimage = 40000,
  271. .colorspace = V4L2_COLORSPACE_JPEG,
  272. .priv = 3},
  273. {176, 144, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
  274. .bytesperline = 176,
  275. .sizeimage = 40000,
  276. .colorspace = V4L2_COLORSPACE_JPEG,
  277. .priv = 1},
  278. {320, 240, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
  279. .bytesperline = 320,
  280. .sizeimage = 320 * 240 * 3 / 8 + 590,
  281. .colorspace = V4L2_COLORSPACE_JPEG,
  282. .priv = 2},
  283. {352, 288, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
  284. .bytesperline = 352,
  285. .sizeimage = 352 * 288 * 3 / 8 + 590,
  286. .colorspace = V4L2_COLORSPACE_JPEG,
  287. .priv = 0},
  288. };
  289. /* Registers common to OV511 / OV518 */
  290. #define R51x_SYS_RESET 0x50
  291. #define R51x_SYS_INIT 0x53
  292. #define R51x_SYS_SNAP 0x52
  293. #define R51x_SYS_CUST_ID 0x5F
  294. #define R51x_COMP_LUT_BEGIN 0x80
  295. /* OV511 Camera interface register numbers */
  296. #define R511_SYS_LED_CTL 0x55 /* OV511+ only */
  297. #define OV511_RESET_NOREGS 0x3F /* All but OV511 & regs */
  298. /* OV518 Camera interface register numbers */
  299. #define R518_GPIO_OUT 0x56 /* OV518(+) only */
  300. #define R518_GPIO_CTL 0x57 /* OV518(+) only */
  301. /* OV519 Camera interface register numbers */
  302. #define OV519_R10_H_SIZE 0x10
  303. #define OV519_R11_V_SIZE 0x11
  304. #define OV519_R12_X_OFFSETL 0x12
  305. #define OV519_R13_X_OFFSETH 0x13
  306. #define OV519_R14_Y_OFFSETL 0x14
  307. #define OV519_R15_Y_OFFSETH 0x15
  308. #define OV519_R16_DIVIDER 0x16
  309. #define OV519_R20_DFR 0x20
  310. #define OV519_R25_FORMAT 0x25
  311. /* OV519 System Controller register numbers */
  312. #define OV519_SYS_RESET1 0x51
  313. #define OV519_SYS_EN_CLK1 0x54
  314. #define OV519_GPIO_DATA_OUT0 0x71
  315. #define OV519_GPIO_IO_CTRL0 0x72
  316. #define OV511_ENDPOINT_ADDRESS 1 /* Isoc endpoint number */
  317. /* I2C registers */
  318. #define R51x_I2C_W_SID 0x41
  319. #define R51x_I2C_SADDR_3 0x42
  320. #define R51x_I2C_SADDR_2 0x43
  321. #define R51x_I2C_R_SID 0x44
  322. #define R51x_I2C_DATA 0x45
  323. #define R518_I2C_CTL 0x47 /* OV518(+) only */
  324. /* I2C ADDRESSES */
  325. #define OV7xx0_SID 0x42
  326. #define OV8xx0_SID 0xa0
  327. #define OV6xx0_SID 0xc0
  328. /* OV7610 registers */
  329. #define OV7610_REG_GAIN 0x00 /* gain setting (5:0) */
  330. #define OV7610_REG_BLUE 0x01 /* blue channel balance */
  331. #define OV7610_REG_RED 0x02 /* red channel balance */
  332. #define OV7610_REG_SAT 0x03 /* saturation */
  333. #define OV8610_REG_HUE 0x04 /* 04 reserved */
  334. #define OV7610_REG_CNT 0x05 /* Y contrast */
  335. #define OV7610_REG_BRT 0x06 /* Y brightness */
  336. #define OV7610_REG_COM_C 0x14 /* misc common regs */
  337. #define OV7610_REG_ID_HIGH 0x1c /* manufacturer ID MSB */
  338. #define OV7610_REG_ID_LOW 0x1d /* manufacturer ID LSB */
  339. #define OV7610_REG_COM_I 0x29 /* misc settings */
  340. /* OV7670 registers */
  341. #define OV7670_REG_GAIN 0x00 /* Gain lower 8 bits (rest in vref) */
  342. #define OV7670_REG_BLUE 0x01 /* blue gain */
  343. #define OV7670_REG_RED 0x02 /* red gain */
  344. #define OV7670_REG_VREF 0x03 /* Pieces of GAIN, VSTART, VSTOP */
  345. #define OV7670_REG_COM1 0x04 /* Control 1 */
  346. #define OV7670_REG_AECHH 0x07 /* AEC MS 5 bits */
  347. #define OV7670_REG_COM3 0x0c /* Control 3 */
  348. #define OV7670_REG_COM4 0x0d /* Control 4 */
  349. #define OV7670_REG_COM5 0x0e /* All "reserved" */
  350. #define OV7670_REG_COM6 0x0f /* Control 6 */
  351. #define OV7670_REG_AECH 0x10 /* More bits of AEC value */
  352. #define OV7670_REG_CLKRC 0x11 /* Clock control */
  353. #define OV7670_REG_COM7 0x12 /* Control 7 */
  354. #define OV7670_COM7_FMT_VGA 0x00
  355. #define OV7670_COM7_YUV 0x00 /* YUV */
  356. #define OV7670_COM7_FMT_QVGA 0x10 /* QVGA format */
  357. #define OV7670_COM7_FMT_MASK 0x38
  358. #define OV7670_COM7_RESET 0x80 /* Register reset */
  359. #define OV7670_REG_COM8 0x13 /* Control 8 */
  360. #define OV7670_COM8_AEC 0x01 /* Auto exposure enable */
  361. #define OV7670_COM8_AWB 0x02 /* White balance enable */
  362. #define OV7670_COM8_AGC 0x04 /* Auto gain enable */
  363. #define OV7670_COM8_BFILT 0x20 /* Band filter enable */
  364. #define OV7670_COM8_AECSTEP 0x40 /* Unlimited AEC step size */
  365. #define OV7670_COM8_FASTAEC 0x80 /* Enable fast AGC/AEC */
  366. #define OV7670_REG_COM9 0x14 /* Control 9 - gain ceiling */
  367. #define OV7670_REG_COM10 0x15 /* Control 10 */
  368. #define OV7670_REG_HSTART 0x17 /* Horiz start high bits */
  369. #define OV7670_REG_HSTOP 0x18 /* Horiz stop high bits */
  370. #define OV7670_REG_VSTART 0x19 /* Vert start high bits */
  371. #define OV7670_REG_VSTOP 0x1a /* Vert stop high bits */
  372. #define OV7670_REG_MVFP 0x1e /* Mirror / vflip */
  373. #define OV7670_MVFP_VFLIP 0x10 /* vertical flip */
  374. #define OV7670_MVFP_MIRROR 0x20 /* Mirror image */
  375. #define OV7670_REG_AEW 0x24 /* AGC upper limit */
  376. #define OV7670_REG_AEB 0x25 /* AGC lower limit */
  377. #define OV7670_REG_VPT 0x26 /* AGC/AEC fast mode op region */
  378. #define OV7670_REG_HREF 0x32 /* HREF pieces */
  379. #define OV7670_REG_TSLB 0x3a /* lots of stuff */
  380. #define OV7670_REG_COM11 0x3b /* Control 11 */
  381. #define OV7670_COM11_EXP 0x02
  382. #define OV7670_COM11_HZAUTO 0x10 /* Auto detect 50/60 Hz */
  383. #define OV7670_REG_COM12 0x3c /* Control 12 */
  384. #define OV7670_REG_COM13 0x3d /* Control 13 */
  385. #define OV7670_COM13_GAMMA 0x80 /* Gamma enable */
  386. #define OV7670_COM13_UVSAT 0x40 /* UV saturation auto adjustment */
  387. #define OV7670_REG_COM14 0x3e /* Control 14 */
  388. #define OV7670_REG_EDGE 0x3f /* Edge enhancement factor */
  389. #define OV7670_REG_COM15 0x40 /* Control 15 */
  390. #define OV7670_COM15_R00FF 0xc0 /* 00 to FF */
  391. #define OV7670_REG_COM16 0x41 /* Control 16 */
  392. #define OV7670_COM16_AWBGAIN 0x08 /* AWB gain enable */
  393. #define OV7670_REG_BRIGHT 0x55 /* Brightness */
  394. #define OV7670_REG_CONTRAS 0x56 /* Contrast control */
  395. #define OV7670_REG_GFIX 0x69 /* Fix gain control */
  396. #define OV7670_REG_RGB444 0x8c /* RGB 444 control */
  397. #define OV7670_REG_HAECC1 0x9f /* Hist AEC/AGC control 1 */
  398. #define OV7670_REG_HAECC2 0xa0 /* Hist AEC/AGC control 2 */
  399. #define OV7670_REG_BD50MAX 0xa5 /* 50hz banding step limit */
  400. #define OV7670_REG_HAECC3 0xa6 /* Hist AEC/AGC control 3 */
  401. #define OV7670_REG_HAECC4 0xa7 /* Hist AEC/AGC control 4 */
  402. #define OV7670_REG_HAECC5 0xa8 /* Hist AEC/AGC control 5 */
  403. #define OV7670_REG_HAECC6 0xa9 /* Hist AEC/AGC control 6 */
  404. #define OV7670_REG_HAECC7 0xaa /* Hist AEC/AGC control 7 */
  405. #define OV7670_REG_BD60MAX 0xab /* 60hz banding step limit */
  406. struct ov_regvals {
  407. __u8 reg;
  408. __u8 val;
  409. };
  410. struct ov_i2c_regvals {
  411. __u8 reg;
  412. __u8 val;
  413. };
  414. static const struct ov_i2c_regvals norm_6x20[] = {
  415. { 0x12, 0x80 }, /* reset */
  416. { 0x11, 0x01 },
  417. { 0x03, 0x60 },
  418. { 0x05, 0x7f }, /* For when autoadjust is off */
  419. { 0x07, 0xa8 },
  420. /* The ratio of 0x0c and 0x0d controls the white point */
  421. { 0x0c, 0x24 },
  422. { 0x0d, 0x24 },
  423. { 0x0f, 0x15 }, /* COMS */
  424. { 0x10, 0x75 }, /* AEC Exposure time */
  425. { 0x12, 0x24 }, /* Enable AGC */
  426. { 0x14, 0x04 },
  427. /* 0x16: 0x06 helps frame stability with moving objects */
  428. { 0x16, 0x06 },
  429. /* { 0x20, 0x30 }, * Aperture correction enable */
  430. { 0x26, 0xb2 }, /* BLC enable */
  431. /* 0x28: 0x05 Selects RGB format if RGB on */
  432. { 0x28, 0x05 },
  433. { 0x2a, 0x04 }, /* Disable framerate adjust */
  434. /* { 0x2b, 0xac }, * Framerate; Set 2a[7] first */
  435. { 0x2d, 0x99 },
  436. { 0x33, 0xa0 }, /* Color Processing Parameter */
  437. { 0x34, 0xd2 }, /* Max A/D range */
  438. { 0x38, 0x8b },
  439. { 0x39, 0x40 },
  440. { 0x3c, 0x39 }, /* Enable AEC mode changing */
  441. { 0x3c, 0x3c }, /* Change AEC mode */
  442. { 0x3c, 0x24 }, /* Disable AEC mode changing */
  443. { 0x3d, 0x80 },
  444. /* These next two registers (0x4a, 0x4b) are undocumented.
  445. * They control the color balance */
  446. { 0x4a, 0x80 },
  447. { 0x4b, 0x80 },
  448. { 0x4d, 0xd2 }, /* This reduces noise a bit */
  449. { 0x4e, 0xc1 },
  450. { 0x4f, 0x04 },
  451. /* Do 50-53 have any effect? */
  452. /* Toggle 0x12[2] off and on here? */
  453. };
  454. static const struct ov_i2c_regvals norm_6x30[] = {
  455. { 0x12, 0x80 }, /* Reset */
  456. { 0x00, 0x1f }, /* Gain */
  457. { 0x01, 0x99 }, /* Blue gain */
  458. { 0x02, 0x7c }, /* Red gain */
  459. { 0x03, 0xc0 }, /* Saturation */
  460. { 0x05, 0x0a }, /* Contrast */
  461. { 0x06, 0x95 }, /* Brightness */
  462. { 0x07, 0x2d }, /* Sharpness */
  463. { 0x0c, 0x20 },
  464. { 0x0d, 0x20 },
  465. { 0x0e, 0xa0 }, /* Was 0x20, bit7 enables a 2x gain which we need */
  466. { 0x0f, 0x05 },
  467. { 0x10, 0x9a },
  468. { 0x11, 0x00 }, /* Pixel clock = fastest */
  469. { 0x12, 0x24 }, /* Enable AGC and AWB */
  470. { 0x13, 0x21 },
  471. { 0x14, 0x80 },
  472. { 0x15, 0x01 },
  473. { 0x16, 0x03 },
  474. { 0x17, 0x38 },
  475. { 0x18, 0xea },
  476. { 0x19, 0x04 },
  477. { 0x1a, 0x93 },
  478. { 0x1b, 0x00 },
  479. { 0x1e, 0xc4 },
  480. { 0x1f, 0x04 },
  481. { 0x20, 0x20 },
  482. { 0x21, 0x10 },
  483. { 0x22, 0x88 },
  484. { 0x23, 0xc0 }, /* Crystal circuit power level */
  485. { 0x25, 0x9a }, /* Increase AEC black ratio */
  486. { 0x26, 0xb2 }, /* BLC enable */
  487. { 0x27, 0xa2 },
  488. { 0x28, 0x00 },
  489. { 0x29, 0x00 },
  490. { 0x2a, 0x84 }, /* 60 Hz power */
  491. { 0x2b, 0xa8 }, /* 60 Hz power */
  492. { 0x2c, 0xa0 },
  493. { 0x2d, 0x95 }, /* Enable auto-brightness */
  494. { 0x2e, 0x88 },
  495. { 0x33, 0x26 },
  496. { 0x34, 0x03 },
  497. { 0x36, 0x8f },
  498. { 0x37, 0x80 },
  499. { 0x38, 0x83 },
  500. { 0x39, 0x80 },
  501. { 0x3a, 0x0f },
  502. { 0x3b, 0x3c },
  503. { 0x3c, 0x1a },
  504. { 0x3d, 0x80 },
  505. { 0x3e, 0x80 },
  506. { 0x3f, 0x0e },
  507. { 0x40, 0x00 }, /* White bal */
  508. { 0x41, 0x00 }, /* White bal */
  509. { 0x42, 0x80 },
  510. { 0x43, 0x3f }, /* White bal */
  511. { 0x44, 0x80 },
  512. { 0x45, 0x20 },
  513. { 0x46, 0x20 },
  514. { 0x47, 0x80 },
  515. { 0x48, 0x7f },
  516. { 0x49, 0x00 },
  517. { 0x4a, 0x00 },
  518. { 0x4b, 0x80 },
  519. { 0x4c, 0xd0 },
  520. { 0x4d, 0x10 }, /* U = 0.563u, V = 0.714v */
  521. { 0x4e, 0x40 },
  522. { 0x4f, 0x07 }, /* UV avg., col. killer: max */
  523. { 0x50, 0xff },
  524. { 0x54, 0x23 }, /* Max AGC gain: 18dB */
  525. { 0x55, 0xff },
  526. { 0x56, 0x12 },
  527. { 0x57, 0x81 },
  528. { 0x58, 0x75 },
  529. { 0x59, 0x01 }, /* AGC dark current comp.: +1 */
  530. { 0x5a, 0x2c },
  531. { 0x5b, 0x0f }, /* AWB chrominance levels */
  532. { 0x5c, 0x10 },
  533. { 0x3d, 0x80 },
  534. { 0x27, 0xa6 },
  535. { 0x12, 0x20 }, /* Toggle AWB */
  536. { 0x12, 0x24 },
  537. };
  538. /* Lawrence Glaister <lg@jfm.bc.ca> reports:
  539. *
  540. * Register 0x0f in the 7610 has the following effects:
  541. *
  542. * 0x85 (AEC method 1): Best overall, good contrast range
  543. * 0x45 (AEC method 2): Very overexposed
  544. * 0xa5 (spec sheet default): Ok, but the black level is
  545. * shifted resulting in loss of contrast
  546. * 0x05 (old driver setting): very overexposed, too much
  547. * contrast
  548. */
  549. static const struct ov_i2c_regvals norm_7610[] = {
  550. { 0x10, 0xff },
  551. { 0x16, 0x06 },
  552. { 0x28, 0x24 },
  553. { 0x2b, 0xac },
  554. { 0x12, 0x00 },
  555. { 0x38, 0x81 },
  556. { 0x28, 0x24 }, /* 0c */
  557. { 0x0f, 0x85 }, /* lg's setting */
  558. { 0x15, 0x01 },
  559. { 0x20, 0x1c },
  560. { 0x23, 0x2a },
  561. { 0x24, 0x10 },
  562. { 0x25, 0x8a },
  563. { 0x26, 0xa2 },
  564. { 0x27, 0xc2 },
  565. { 0x2a, 0x04 },
  566. { 0x2c, 0xfe },
  567. { 0x2d, 0x93 },
  568. { 0x30, 0x71 },
  569. { 0x31, 0x60 },
  570. { 0x32, 0x26 },
  571. { 0x33, 0x20 },
  572. { 0x34, 0x48 },
  573. { 0x12, 0x24 },
  574. { 0x11, 0x01 },
  575. { 0x0c, 0x24 },
  576. { 0x0d, 0x24 },
  577. };
  578. static const struct ov_i2c_regvals norm_7620[] = {
  579. { 0x00, 0x00 }, /* gain */
  580. { 0x01, 0x80 }, /* blue gain */
  581. { 0x02, 0x80 }, /* red gain */
  582. { 0x03, 0xc0 }, /* OV7670_REG_VREF */
  583. { 0x06, 0x60 },
  584. { 0x07, 0x00 },
  585. { 0x0c, 0x24 },
  586. { 0x0c, 0x24 },
  587. { 0x0d, 0x24 },
  588. { 0x11, 0x01 },
  589. { 0x12, 0x24 },
  590. { 0x13, 0x01 },
  591. { 0x14, 0x84 },
  592. { 0x15, 0x01 },
  593. { 0x16, 0x03 },
  594. { 0x17, 0x2f },
  595. { 0x18, 0xcf },
  596. { 0x19, 0x06 },
  597. { 0x1a, 0xf5 },
  598. { 0x1b, 0x00 },
  599. { 0x20, 0x18 },
  600. { 0x21, 0x80 },
  601. { 0x22, 0x80 },
  602. { 0x23, 0x00 },
  603. { 0x26, 0xa2 },
  604. { 0x27, 0xea },
  605. { 0x28, 0x20 },
  606. { 0x29, 0x00 },
  607. { 0x2a, 0x10 },
  608. { 0x2b, 0x00 },
  609. { 0x2c, 0x88 },
  610. { 0x2d, 0x91 },
  611. { 0x2e, 0x80 },
  612. { 0x2f, 0x44 },
  613. { 0x60, 0x27 },
  614. { 0x61, 0x02 },
  615. { 0x62, 0x5f },
  616. { 0x63, 0xd5 },
  617. { 0x64, 0x57 },
  618. { 0x65, 0x83 },
  619. { 0x66, 0x55 },
  620. { 0x67, 0x92 },
  621. { 0x68, 0xcf },
  622. { 0x69, 0x76 },
  623. { 0x6a, 0x22 },
  624. { 0x6b, 0x00 },
  625. { 0x6c, 0x02 },
  626. { 0x6d, 0x44 },
  627. { 0x6e, 0x80 },
  628. { 0x6f, 0x1d },
  629. { 0x70, 0x8b },
  630. { 0x71, 0x00 },
  631. { 0x72, 0x14 },
  632. { 0x73, 0x54 },
  633. { 0x74, 0x00 },
  634. { 0x75, 0x8e },
  635. { 0x76, 0x00 },
  636. { 0x77, 0xff },
  637. { 0x78, 0x80 },
  638. { 0x79, 0x80 },
  639. { 0x7a, 0x80 },
  640. { 0x7b, 0xe2 },
  641. { 0x7c, 0x00 },
  642. };
  643. /* 7640 and 7648. The defaults should be OK for most registers. */
  644. static const struct ov_i2c_regvals norm_7640[] = {
  645. { 0x12, 0x80 },
  646. { 0x12, 0x14 },
  647. };
  648. /* 7670. Defaults taken from OmniVision provided data,
  649. * as provided by Jonathan Corbet of OLPC */
  650. static const struct ov_i2c_regvals norm_7670[] = {
  651. { OV7670_REG_COM7, OV7670_COM7_RESET },
  652. { OV7670_REG_TSLB, 0x04 }, /* OV */
  653. { OV7670_REG_COM7, OV7670_COM7_FMT_VGA }, /* VGA */
  654. { OV7670_REG_CLKRC, 0x01 },
  655. /*
  656. * Set the hardware window. These values from OV don't entirely
  657. * make sense - hstop is less than hstart. But they work...
  658. */
  659. { OV7670_REG_HSTART, 0x13 },
  660. { OV7670_REG_HSTOP, 0x01 },
  661. { OV7670_REG_HREF, 0xb6 },
  662. { OV7670_REG_VSTART, 0x02 },
  663. { OV7670_REG_VSTOP, 0x7a },
  664. { OV7670_REG_VREF, 0x0a },
  665. { OV7670_REG_COM3, 0x00 },
  666. { OV7670_REG_COM14, 0x00 },
  667. /* Mystery scaling numbers */
  668. { 0x70, 0x3a },
  669. { 0x71, 0x35 },
  670. { 0x72, 0x11 },
  671. { 0x73, 0xf0 },
  672. { 0xa2, 0x02 },
  673. /* { OV7670_REG_COM10, 0x0 }, */
  674. /* Gamma curve values */
  675. { 0x7a, 0x20 },
  676. { 0x7b, 0x10 },
  677. { 0x7c, 0x1e },
  678. { 0x7d, 0x35 },
  679. { 0x7e, 0x5a },
  680. { 0x7f, 0x69 },
  681. { 0x80, 0x76 },
  682. { 0x81, 0x80 },
  683. { 0x82, 0x88 },
  684. { 0x83, 0x8f },
  685. { 0x84, 0x96 },
  686. { 0x85, 0xa3 },
  687. { 0x86, 0xaf },
  688. { 0x87, 0xc4 },
  689. { 0x88, 0xd7 },
  690. { 0x89, 0xe8 },
  691. /* AGC and AEC parameters. Note we start by disabling those features,
  692. then turn them only after tweaking the values. */
  693. { OV7670_REG_COM8, OV7670_COM8_FASTAEC
  694. | OV7670_COM8_AECSTEP
  695. | OV7670_COM8_BFILT },
  696. { OV7670_REG_GAIN, 0x00 },
  697. { OV7670_REG_AECH, 0x00 },
  698. { OV7670_REG_COM4, 0x40 }, /* magic reserved bit */
  699. { OV7670_REG_COM9, 0x18 }, /* 4x gain + magic rsvd bit */
  700. { OV7670_REG_BD50MAX, 0x05 },
  701. { OV7670_REG_BD60MAX, 0x07 },
  702. { OV7670_REG_AEW, 0x95 },
  703. { OV7670_REG_AEB, 0x33 },
  704. { OV7670_REG_VPT, 0xe3 },
  705. { OV7670_REG_HAECC1, 0x78 },
  706. { OV7670_REG_HAECC2, 0x68 },
  707. { 0xa1, 0x03 }, /* magic */
  708. { OV7670_REG_HAECC3, 0xd8 },
  709. { OV7670_REG_HAECC4, 0xd8 },
  710. { OV7670_REG_HAECC5, 0xf0 },
  711. { OV7670_REG_HAECC6, 0x90 },
  712. { OV7670_REG_HAECC7, 0x94 },
  713. { OV7670_REG_COM8, OV7670_COM8_FASTAEC
  714. | OV7670_COM8_AECSTEP
  715. | OV7670_COM8_BFILT
  716. | OV7670_COM8_AGC
  717. | OV7670_COM8_AEC },
  718. /* Almost all of these are magic "reserved" values. */
  719. { OV7670_REG_COM5, 0x61 },
  720. { OV7670_REG_COM6, 0x4b },
  721. { 0x16, 0x02 },
  722. { OV7670_REG_MVFP, 0x07 },
  723. { 0x21, 0x02 },
  724. { 0x22, 0x91 },
  725. { 0x29, 0x07 },
  726. { 0x33, 0x0b },
  727. { 0x35, 0x0b },
  728. { 0x37, 0x1d },
  729. { 0x38, 0x71 },
  730. { 0x39, 0x2a },
  731. { OV7670_REG_COM12, 0x78 },
  732. { 0x4d, 0x40 },
  733. { 0x4e, 0x20 },
  734. { OV7670_REG_GFIX, 0x00 },
  735. { 0x6b, 0x4a },
  736. { 0x74, 0x10 },
  737. { 0x8d, 0x4f },
  738. { 0x8e, 0x00 },
  739. { 0x8f, 0x00 },
  740. { 0x90, 0x00 },
  741. { 0x91, 0x00 },
  742. { 0x96, 0x00 },
  743. { 0x9a, 0x00 },
  744. { 0xb0, 0x84 },
  745. { 0xb1, 0x0c },
  746. { 0xb2, 0x0e },
  747. { 0xb3, 0x82 },
  748. { 0xb8, 0x0a },
  749. /* More reserved magic, some of which tweaks white balance */
  750. { 0x43, 0x0a },
  751. { 0x44, 0xf0 },
  752. { 0x45, 0x34 },
  753. { 0x46, 0x58 },
  754. { 0x47, 0x28 },
  755. { 0x48, 0x3a },
  756. { 0x59, 0x88 },
  757. { 0x5a, 0x88 },
  758. { 0x5b, 0x44 },
  759. { 0x5c, 0x67 },
  760. { 0x5d, 0x49 },
  761. { 0x5e, 0x0e },
  762. { 0x6c, 0x0a },
  763. { 0x6d, 0x55 },
  764. { 0x6e, 0x11 },
  765. { 0x6f, 0x9f },
  766. /* "9e for advance AWB" */
  767. { 0x6a, 0x40 },
  768. { OV7670_REG_BLUE, 0x40 },
  769. { OV7670_REG_RED, 0x60 },
  770. { OV7670_REG_COM8, OV7670_COM8_FASTAEC
  771. | OV7670_COM8_AECSTEP
  772. | OV7670_COM8_BFILT
  773. | OV7670_COM8_AGC
  774. | OV7670_COM8_AEC
  775. | OV7670_COM8_AWB },
  776. /* Matrix coefficients */
  777. { 0x4f, 0x80 },
  778. { 0x50, 0x80 },
  779. { 0x51, 0x00 },
  780. { 0x52, 0x22 },
  781. { 0x53, 0x5e },
  782. { 0x54, 0x80 },
  783. { 0x58, 0x9e },
  784. { OV7670_REG_COM16, OV7670_COM16_AWBGAIN },
  785. { OV7670_REG_EDGE, 0x00 },
  786. { 0x75, 0x05 },
  787. { 0x76, 0xe1 },
  788. { 0x4c, 0x00 },
  789. { 0x77, 0x01 },
  790. { OV7670_REG_COM13, OV7670_COM13_GAMMA
  791. | OV7670_COM13_UVSAT
  792. | 2}, /* was 3 */
  793. { 0x4b, 0x09 },
  794. { 0xc9, 0x60 },
  795. { OV7670_REG_COM16, 0x38 },
  796. { 0x56, 0x40 },
  797. { 0x34, 0x11 },
  798. { OV7670_REG_COM11, OV7670_COM11_EXP|OV7670_COM11_HZAUTO },
  799. { 0xa4, 0x88 },
  800. { 0x96, 0x00 },
  801. { 0x97, 0x30 },
  802. { 0x98, 0x20 },
  803. { 0x99, 0x30 },
  804. { 0x9a, 0x84 },
  805. { 0x9b, 0x29 },
  806. { 0x9c, 0x03 },
  807. { 0x9d, 0x4c },
  808. { 0x9e, 0x3f },
  809. { 0x78, 0x04 },
  810. /* Extra-weird stuff. Some sort of multiplexor register */
  811. { 0x79, 0x01 },
  812. { 0xc8, 0xf0 },
  813. { 0x79, 0x0f },
  814. { 0xc8, 0x00 },
  815. { 0x79, 0x10 },
  816. { 0xc8, 0x7e },
  817. { 0x79, 0x0a },
  818. { 0xc8, 0x80 },
  819. { 0x79, 0x0b },
  820. { 0xc8, 0x01 },
  821. { 0x79, 0x0c },
  822. { 0xc8, 0x0f },
  823. { 0x79, 0x0d },
  824. { 0xc8, 0x20 },
  825. { 0x79, 0x09 },
  826. { 0xc8, 0x80 },
  827. { 0x79, 0x02 },
  828. { 0xc8, 0xc0 },
  829. { 0x79, 0x03 },
  830. { 0xc8, 0x40 },
  831. { 0x79, 0x05 },
  832. { 0xc8, 0x30 },
  833. { 0x79, 0x26 },
  834. };
  835. static const struct ov_i2c_regvals norm_8610[] = {
  836. { 0x12, 0x80 },
  837. { 0x00, 0x00 },
  838. { 0x01, 0x80 },
  839. { 0x02, 0x80 },
  840. { 0x03, 0xc0 },
  841. { 0x04, 0x30 },
  842. { 0x05, 0x30 }, /* was 0x10, new from windrv 090403 */
  843. { 0x06, 0x70 }, /* was 0x80, new from windrv 090403 */
  844. { 0x0a, 0x86 },
  845. { 0x0b, 0xb0 },
  846. { 0x0c, 0x20 },
  847. { 0x0d, 0x20 },
  848. { 0x11, 0x01 },
  849. { 0x12, 0x25 },
  850. { 0x13, 0x01 },
  851. { 0x14, 0x04 },
  852. { 0x15, 0x01 }, /* Lin and Win think different about UV order */
  853. { 0x16, 0x03 },
  854. { 0x17, 0x38 }, /* was 0x2f, new from windrv 090403 */
  855. { 0x18, 0xea }, /* was 0xcf, new from windrv 090403 */
  856. { 0x19, 0x02 }, /* was 0x06, new from windrv 090403 */
  857. { 0x1a, 0xf5 },
  858. { 0x1b, 0x00 },
  859. { 0x20, 0xd0 }, /* was 0x90, new from windrv 090403 */
  860. { 0x23, 0xc0 }, /* was 0x00, new from windrv 090403 */
  861. { 0x24, 0x30 }, /* was 0x1d, new from windrv 090403 */
  862. { 0x25, 0x50 }, /* was 0x57, new from windrv 090403 */
  863. { 0x26, 0xa2 },
  864. { 0x27, 0xea },
  865. { 0x28, 0x00 },
  866. { 0x29, 0x00 },
  867. { 0x2a, 0x80 },
  868. { 0x2b, 0xc8 }, /* was 0xcc, new from windrv 090403 */
  869. { 0x2c, 0xac },
  870. { 0x2d, 0x45 }, /* was 0xd5, new from windrv 090403 */
  871. { 0x2e, 0x80 },
  872. { 0x2f, 0x14 }, /* was 0x01, new from windrv 090403 */
  873. { 0x4c, 0x00 },
  874. { 0x4d, 0x30 }, /* was 0x10, new from windrv 090403 */
  875. { 0x60, 0x02 }, /* was 0x01, new from windrv 090403 */
  876. { 0x61, 0x00 }, /* was 0x09, new from windrv 090403 */
  877. { 0x62, 0x5f }, /* was 0xd7, new from windrv 090403 */
  878. { 0x63, 0xff },
  879. { 0x64, 0x53 }, /* new windrv 090403 says 0x57,
  880. * maybe thats wrong */
  881. { 0x65, 0x00 },
  882. { 0x66, 0x55 },
  883. { 0x67, 0xb0 },
  884. { 0x68, 0xc0 }, /* was 0xaf, new from windrv 090403 */
  885. { 0x69, 0x02 },
  886. { 0x6a, 0x22 },
  887. { 0x6b, 0x00 },
  888. { 0x6c, 0x99 }, /* was 0x80, old windrv says 0x00, but
  889. * deleting bit7 colors the first images red */
  890. { 0x6d, 0x11 }, /* was 0x00, new from windrv 090403 */
  891. { 0x6e, 0x11 }, /* was 0x00, new from windrv 090403 */
  892. { 0x6f, 0x01 },
  893. { 0x70, 0x8b },
  894. { 0x71, 0x00 },
  895. { 0x72, 0x14 },
  896. { 0x73, 0x54 },
  897. { 0x74, 0x00 },/* 0x60? - was 0x00, new from windrv 090403 */
  898. { 0x75, 0x0e },
  899. { 0x76, 0x02 }, /* was 0x02, new from windrv 090403 */
  900. { 0x77, 0xff },
  901. { 0x78, 0x80 },
  902. { 0x79, 0x80 },
  903. { 0x7a, 0x80 },
  904. { 0x7b, 0x10 }, /* was 0x13, new from windrv 090403 */
  905. { 0x7c, 0x00 },
  906. { 0x7d, 0x08 }, /* was 0x09, new from windrv 090403 */
  907. { 0x7e, 0x08 }, /* was 0xc0, new from windrv 090403 */
  908. { 0x7f, 0xfb },
  909. { 0x80, 0x28 },
  910. { 0x81, 0x00 },
  911. { 0x82, 0x23 },
  912. { 0x83, 0x0b },
  913. { 0x84, 0x00 },
  914. { 0x85, 0x62 }, /* was 0x61, new from windrv 090403 */
  915. { 0x86, 0xc9 },
  916. { 0x87, 0x00 },
  917. { 0x88, 0x00 },
  918. { 0x89, 0x01 },
  919. { 0x12, 0x20 },
  920. { 0x12, 0x25 }, /* was 0x24, new from windrv 090403 */
  921. };
  922. static unsigned char ov7670_abs_to_sm(unsigned char v)
  923. {
  924. if (v > 127)
  925. return v & 0x7f;
  926. return (128 - v) | 0x80;
  927. }
  928. /* Write a OV519 register */
  929. static int reg_w(struct sd *sd, __u16 index, __u8 value)
  930. {
  931. int ret;
  932. int req = (sd->bridge <= BRIDGE_OV511PLUS) ? 2 : 1;
  933. sd->gspca_dev.usb_buf[0] = value;
  934. ret = usb_control_msg(sd->gspca_dev.dev,
  935. usb_sndctrlpipe(sd->gspca_dev.dev, 0),
  936. req,
  937. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  938. 0, index,
  939. sd->gspca_dev.usb_buf, 1, 500);
  940. if (ret < 0)
  941. PDEBUG(D_ERR, "Write reg [%02x] %02x failed", index, value);
  942. return ret;
  943. }
  944. /* Read from a OV519 register */
  945. /* returns: negative is error, pos or zero is data */
  946. static int reg_r(struct sd *sd, __u16 index)
  947. {
  948. int ret;
  949. int req = (sd->bridge <= BRIDGE_OV511PLUS) ? 3 : 1;
  950. ret = usb_control_msg(sd->gspca_dev.dev,
  951. usb_rcvctrlpipe(sd->gspca_dev.dev, 0),
  952. req,
  953. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  954. 0, index, sd->gspca_dev.usb_buf, 1, 500);
  955. if (ret >= 0)
  956. ret = sd->gspca_dev.usb_buf[0];
  957. else
  958. PDEBUG(D_ERR, "Read reg [0x%02x] failed", index);
  959. return ret;
  960. }
  961. /* Read 8 values from a OV519 register */
  962. static int reg_r8(struct sd *sd,
  963. __u16 index)
  964. {
  965. int ret;
  966. ret = usb_control_msg(sd->gspca_dev.dev,
  967. usb_rcvctrlpipe(sd->gspca_dev.dev, 0),
  968. 1, /* REQ_IO */
  969. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  970. 0, index, sd->gspca_dev.usb_buf, 8, 500);
  971. if (ret >= 0)
  972. ret = sd->gspca_dev.usb_buf[0];
  973. else
  974. PDEBUG(D_ERR, "Read reg 8 [0x%02x] failed", index);
  975. return ret;
  976. }
  977. /*
  978. * Writes bits at positions specified by mask to an OV51x reg. Bits that are in
  979. * the same position as 1's in "mask" are cleared and set to "value". Bits
  980. * that are in the same position as 0's in "mask" are preserved, regardless
  981. * of their respective state in "value".
  982. */
  983. static int reg_w_mask(struct sd *sd,
  984. __u16 index,
  985. __u8 value,
  986. __u8 mask)
  987. {
  988. int ret;
  989. __u8 oldval;
  990. if (mask != 0xff) {
  991. value &= mask; /* Enforce mask on value */
  992. ret = reg_r(sd, index);
  993. if (ret < 0)
  994. return ret;
  995. oldval = ret & ~mask; /* Clear the masked bits */
  996. value |= oldval; /* Set the desired bits */
  997. }
  998. return reg_w(sd, index, value);
  999. }
  1000. /*
  1001. * Writes multiple (n) byte value to a single register. Only valid with certain
  1002. * registers (0x30 and 0xc4 - 0xce).
  1003. */
  1004. static int ov518_reg_w32(struct sd *sd, __u16 index, u32 value, int n)
  1005. {
  1006. int ret;
  1007. *((u32 *)sd->gspca_dev.usb_buf) = __cpu_to_le32(value);
  1008. ret = usb_control_msg(sd->gspca_dev.dev,
  1009. usb_sndctrlpipe(sd->gspca_dev.dev, 0),
  1010. 1 /* REG_IO */,
  1011. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  1012. 0, index,
  1013. sd->gspca_dev.usb_buf, n, 500);
  1014. if (ret < 0)
  1015. PDEBUG(D_ERR, "Write reg32 [%02x] %08x failed", index, value);
  1016. return ret;
  1017. }
  1018. /*
  1019. * The OV518 I2C I/O procedure is different, hence, this function.
  1020. * This is normally only called from i2c_w(). Note that this function
  1021. * always succeeds regardless of whether the sensor is present and working.
  1022. */
  1023. static int i2c_w(struct sd *sd,
  1024. __u8 reg,
  1025. __u8 value)
  1026. {
  1027. int rc;
  1028. PDEBUG(D_USBO, "i2c 0x%02x -> [0x%02x]", value, reg);
  1029. /* Select camera register */
  1030. rc = reg_w(sd, R51x_I2C_SADDR_3, reg);
  1031. if (rc < 0)
  1032. return rc;
  1033. /* Write "value" to I2C data port of OV511 */
  1034. rc = reg_w(sd, R51x_I2C_DATA, value);
  1035. if (rc < 0)
  1036. return rc;
  1037. /* Initiate 3-byte write cycle */
  1038. rc = reg_w(sd, R518_I2C_CTL, 0x01);
  1039. if (rc < 0)
  1040. return rc;
  1041. /* wait for write complete */
  1042. msleep(4);
  1043. return reg_r8(sd, R518_I2C_CTL);
  1044. }
  1045. /*
  1046. * returns: negative is error, pos or zero is data
  1047. *
  1048. * The OV518 I2C I/O procedure is different, hence, this function.
  1049. * This is normally only called from i2c_r(). Note that this function
  1050. * always succeeds regardless of whether the sensor is present and working.
  1051. */
  1052. static int i2c_r(struct sd *sd, __u8 reg)
  1053. {
  1054. int rc, value;
  1055. /* Select camera register */
  1056. rc = reg_w(sd, R51x_I2C_SADDR_2, reg);
  1057. if (rc < 0)
  1058. return rc;
  1059. /* Initiate 2-byte write cycle */
  1060. rc = reg_w(sd, R518_I2C_CTL, 0x03);
  1061. if (rc < 0)
  1062. return rc;
  1063. /* Initiate 2-byte read cycle */
  1064. rc = reg_w(sd, R518_I2C_CTL, 0x05);
  1065. if (rc < 0)
  1066. return rc;
  1067. value = reg_r(sd, R51x_I2C_DATA);
  1068. PDEBUG(D_USBI, "i2c [0x%02X] -> 0x%02X", reg, value);
  1069. return value;
  1070. }
  1071. /* Writes bits at positions specified by mask to an I2C reg. Bits that are in
  1072. * the same position as 1's in "mask" are cleared and set to "value". Bits
  1073. * that are in the same position as 0's in "mask" are preserved, regardless
  1074. * of their respective state in "value".
  1075. */
  1076. static int i2c_w_mask(struct sd *sd,
  1077. __u8 reg,
  1078. __u8 value,
  1079. __u8 mask)
  1080. {
  1081. int rc;
  1082. __u8 oldval;
  1083. value &= mask; /* Enforce mask on value */
  1084. rc = i2c_r(sd, reg);
  1085. if (rc < 0)
  1086. return rc;
  1087. oldval = rc & ~mask; /* Clear the masked bits */
  1088. value |= oldval; /* Set the desired bits */
  1089. return i2c_w(sd, reg, value);
  1090. }
  1091. /* Temporarily stops OV511 from functioning. Must do this before changing
  1092. * registers while the camera is streaming */
  1093. static inline int ov51x_stop(struct sd *sd)
  1094. {
  1095. PDEBUG(D_STREAM, "stopping");
  1096. sd->stopped = 1;
  1097. switch (sd->bridge) {
  1098. case BRIDGE_OV511:
  1099. case BRIDGE_OV511PLUS:
  1100. return reg_w(sd, R51x_SYS_RESET, 0x3d);
  1101. case BRIDGE_OV518:
  1102. case BRIDGE_OV518PLUS:
  1103. return reg_w_mask(sd, R51x_SYS_RESET, 0x3a, 0x3a);
  1104. case BRIDGE_OV519:
  1105. return reg_w(sd, OV519_SYS_RESET1, 0x0f);
  1106. }
  1107. return 0;
  1108. }
  1109. /* Restarts OV511 after ov511_stop() is called. Has no effect if it is not
  1110. * actually stopped (for performance). */
  1111. static inline int ov51x_restart(struct sd *sd)
  1112. {
  1113. int rc;
  1114. PDEBUG(D_STREAM, "restarting");
  1115. if (!sd->stopped)
  1116. return 0;
  1117. sd->stopped = 0;
  1118. /* Reinitialize the stream */
  1119. switch (sd->bridge) {
  1120. case BRIDGE_OV511:
  1121. case BRIDGE_OV511PLUS:
  1122. return reg_w(sd, R51x_SYS_RESET, 0x00);
  1123. case BRIDGE_OV518:
  1124. case BRIDGE_OV518PLUS:
  1125. rc = reg_w(sd, 0x2f, 0x80);
  1126. if (rc < 0)
  1127. return rc;
  1128. return reg_w(sd, R51x_SYS_RESET, 0x00);
  1129. case BRIDGE_OV519:
  1130. return reg_w(sd, OV519_SYS_RESET1, 0x00);
  1131. }
  1132. return 0;
  1133. }
  1134. /* This does an initial reset of an OmniVision sensor and ensures that I2C
  1135. * is synchronized. Returns <0 on failure.
  1136. */
  1137. static int init_ov_sensor(struct sd *sd)
  1138. {
  1139. int i;
  1140. /* Reset the sensor */
  1141. if (i2c_w(sd, 0x12, 0x80) < 0)
  1142. return -EIO;
  1143. /* Wait for it to initialize */
  1144. msleep(150);
  1145. for (i = 0; i < i2c_detect_tries; i++) {
  1146. if (i2c_r(sd, OV7610_REG_ID_HIGH) == 0x7f &&
  1147. i2c_r(sd, OV7610_REG_ID_LOW) == 0xa2) {
  1148. PDEBUG(D_PROBE, "I2C synced in %d attempt(s)", i);
  1149. return 0;
  1150. }
  1151. /* Reset the sensor */
  1152. if (i2c_w(sd, 0x12, 0x80) < 0)
  1153. return -EIO;
  1154. /* Wait for it to initialize */
  1155. msleep(150);
  1156. /* Dummy read to sync I2C */
  1157. if (i2c_r(sd, 0x00) < 0)
  1158. return -EIO;
  1159. }
  1160. return -EIO;
  1161. }
  1162. /* Set the read and write slave IDs. The "slave" argument is the write slave,
  1163. * and the read slave will be set to (slave + 1).
  1164. * This should not be called from outside the i2c I/O functions.
  1165. * Sets I2C read and write slave IDs. Returns <0 for error
  1166. */
  1167. static int ov51x_set_slave_ids(struct sd *sd,
  1168. __u8 slave)
  1169. {
  1170. int rc;
  1171. rc = reg_w(sd, R51x_I2C_W_SID, slave);
  1172. if (rc < 0)
  1173. return rc;
  1174. return reg_w(sd, R51x_I2C_R_SID, slave + 1);
  1175. }
  1176. static int write_regvals(struct sd *sd,
  1177. const struct ov_regvals *regvals,
  1178. int n)
  1179. {
  1180. int rc;
  1181. while (--n >= 0) {
  1182. rc = reg_w(sd, regvals->reg, regvals->val);
  1183. if (rc < 0)
  1184. return rc;
  1185. regvals++;
  1186. }
  1187. return 0;
  1188. }
  1189. static int write_i2c_regvals(struct sd *sd,
  1190. const struct ov_i2c_regvals *regvals,
  1191. int n)
  1192. {
  1193. int rc;
  1194. while (--n >= 0) {
  1195. rc = i2c_w(sd, regvals->reg, regvals->val);
  1196. if (rc < 0)
  1197. return rc;
  1198. regvals++;
  1199. }
  1200. return 0;
  1201. }
  1202. /****************************************************************************
  1203. *
  1204. * OV511 and sensor configuration
  1205. *
  1206. ***************************************************************************/
  1207. /* This initializes the OV8110, OV8610 sensor. The OV8110 uses
  1208. * the same register settings as the OV8610, since they are very similar.
  1209. */
  1210. static int ov8xx0_configure(struct sd *sd)
  1211. {
  1212. int rc;
  1213. PDEBUG(D_PROBE, "starting ov8xx0 configuration");
  1214. /* Detect sensor (sub)type */
  1215. rc = i2c_r(sd, OV7610_REG_COM_I);
  1216. if (rc < 0) {
  1217. PDEBUG(D_ERR, "Error detecting sensor type");
  1218. return -1;
  1219. }
  1220. if ((rc & 3) == 1) {
  1221. sd->sensor = SEN_OV8610;
  1222. } else {
  1223. PDEBUG(D_ERR, "Unknown image sensor version: %d", rc & 3);
  1224. return -1;
  1225. }
  1226. /* Set sensor-specific vars */
  1227. /* sd->sif = 0; already done */
  1228. return 0;
  1229. }
  1230. /* This initializes the OV7610, OV7620, or OV76BE sensor. The OV76BE uses
  1231. * the same register settings as the OV7610, since they are very similar.
  1232. */
  1233. static int ov7xx0_configure(struct sd *sd)
  1234. {
  1235. int rc, high, low;
  1236. PDEBUG(D_PROBE, "starting OV7xx0 configuration");
  1237. /* Detect sensor (sub)type */
  1238. rc = i2c_r(sd, OV7610_REG_COM_I);
  1239. /* add OV7670 here
  1240. * it appears to be wrongly detected as a 7610 by default */
  1241. if (rc < 0) {
  1242. PDEBUG(D_ERR, "Error detecting sensor type");
  1243. return -1;
  1244. }
  1245. if ((rc & 3) == 3) {
  1246. /* quick hack to make OV7670s work */
  1247. high = i2c_r(sd, 0x0a);
  1248. low = i2c_r(sd, 0x0b);
  1249. /* info("%x, %x", high, low); */
  1250. if (high == 0x76 && low == 0x73) {
  1251. PDEBUG(D_PROBE, "Sensor is an OV7670");
  1252. sd->sensor = SEN_OV7670;
  1253. } else {
  1254. PDEBUG(D_PROBE, "Sensor is an OV7610");
  1255. sd->sensor = SEN_OV7610;
  1256. }
  1257. } else if ((rc & 3) == 1) {
  1258. /* I don't know what's different about the 76BE yet. */
  1259. if (i2c_r(sd, 0x15) & 1)
  1260. PDEBUG(D_PROBE, "Sensor is an OV7620AE");
  1261. else
  1262. PDEBUG(D_PROBE, "Sensor is an OV76BE");
  1263. /* OV511+ will return all zero isoc data unless we
  1264. * configure the sensor as a 7620. Someone needs to
  1265. * find the exact reg. setting that causes this. */
  1266. sd->sensor = SEN_OV76BE;
  1267. } else if ((rc & 3) == 0) {
  1268. /* try to read product id registers */
  1269. high = i2c_r(sd, 0x0a);
  1270. if (high < 0) {
  1271. PDEBUG(D_ERR, "Error detecting camera chip PID");
  1272. return high;
  1273. }
  1274. low = i2c_r(sd, 0x0b);
  1275. if (low < 0) {
  1276. PDEBUG(D_ERR, "Error detecting camera chip VER");
  1277. return low;
  1278. }
  1279. if (high == 0x76) {
  1280. switch (low) {
  1281. case 0x30:
  1282. PDEBUG(D_PROBE, "Sensor is an OV7630/OV7635");
  1283. PDEBUG(D_ERR,
  1284. "7630 is not supported by this driver");
  1285. return -1;
  1286. case 0x40:
  1287. PDEBUG(D_PROBE, "Sensor is an OV7645");
  1288. sd->sensor = SEN_OV7640; /* FIXME */
  1289. break;
  1290. case 0x45:
  1291. PDEBUG(D_PROBE, "Sensor is an OV7645B");
  1292. sd->sensor = SEN_OV7640; /* FIXME */
  1293. break;
  1294. case 0x48:
  1295. PDEBUG(D_PROBE, "Sensor is an OV7648");
  1296. sd->sensor = SEN_OV7640; /* FIXME */
  1297. break;
  1298. default:
  1299. PDEBUG(D_PROBE, "Unknown sensor: 0x76%x", low);
  1300. return -1;
  1301. }
  1302. } else {
  1303. PDEBUG(D_PROBE, "Sensor is an OV7620");
  1304. sd->sensor = SEN_OV7620;
  1305. }
  1306. } else {
  1307. PDEBUG(D_ERR, "Unknown image sensor version: %d", rc & 3);
  1308. return -1;
  1309. }
  1310. /* Set sensor-specific vars */
  1311. /* sd->sif = 0; already done */
  1312. return 0;
  1313. }
  1314. /* This initializes the OV6620, OV6630, OV6630AE, or OV6630AF sensor. */
  1315. static int ov6xx0_configure(struct sd *sd)
  1316. {
  1317. int rc;
  1318. PDEBUG(D_PROBE, "starting OV6xx0 configuration");
  1319. /* Detect sensor (sub)type */
  1320. rc = i2c_r(sd, OV7610_REG_COM_I);
  1321. if (rc < 0) {
  1322. PDEBUG(D_ERR, "Error detecting sensor type");
  1323. return -1;
  1324. }
  1325. /* Ugh. The first two bits are the version bits, but
  1326. * the entire register value must be used. I guess OVT
  1327. * underestimated how many variants they would make. */
  1328. switch (rc) {
  1329. case 0x00:
  1330. sd->sensor = SEN_OV6630;
  1331. PDEBUG(D_ERR,
  1332. "WARNING: Sensor is an OV66308. Your camera may have");
  1333. PDEBUG(D_ERR, "been misdetected in previous driver versions.");
  1334. break;
  1335. case 0x01:
  1336. sd->sensor = SEN_OV6620;
  1337. PDEBUG(D_PROBE, "Sensor is an OV6620");
  1338. break;
  1339. case 0x02:
  1340. sd->sensor = SEN_OV6630;
  1341. PDEBUG(D_PROBE, "Sensor is an OV66308AE");
  1342. break;
  1343. case 0x03:
  1344. sd->sensor = SEN_OV66308AF;
  1345. PDEBUG(D_PROBE, "Sensor is an OV66308AF");
  1346. break;
  1347. case 0x90:
  1348. sd->sensor = SEN_OV6630;
  1349. PDEBUG(D_ERR,
  1350. "WARNING: Sensor is an OV66307. Your camera may have");
  1351. PDEBUG(D_ERR, "been misdetected in previous driver versions.");
  1352. break;
  1353. default:
  1354. PDEBUG(D_ERR, "FATAL: Unknown sensor version: 0x%02x", rc);
  1355. return -1;
  1356. }
  1357. /* Set sensor-specific vars */
  1358. sd->sif = 1;
  1359. return 0;
  1360. }
  1361. /* Turns on or off the LED. Only has an effect with OV511+/OV518(+)/OV519 */
  1362. static void ov51x_led_control(struct sd *sd, int on)
  1363. {
  1364. if (sd->invert_led)
  1365. on = !on;
  1366. switch (sd->bridge) {
  1367. /* OV511 has no LED control */
  1368. case BRIDGE_OV511PLUS:
  1369. reg_w(sd, R511_SYS_LED_CTL, on ? 1 : 0);
  1370. break;
  1371. case BRIDGE_OV518:
  1372. case BRIDGE_OV518PLUS:
  1373. reg_w_mask(sd, R518_GPIO_OUT, on ? 0x02 : 0x00, 0x02);
  1374. break;
  1375. case BRIDGE_OV519:
  1376. reg_w_mask(sd, OV519_GPIO_DATA_OUT0, !on, 1); /* 0 / 1 */
  1377. break;
  1378. }
  1379. }
  1380. /* OV518 quantization tables are 8x4 (instead of 8x8) */
  1381. static int ov518_upload_quan_tables(struct sd *sd)
  1382. {
  1383. const unsigned char yQuanTable518[] = {
  1384. 5, 4, 5, 6, 6, 7, 7, 7,
  1385. 5, 5, 5, 5, 6, 7, 7, 7,
  1386. 6, 6, 6, 6, 7, 7, 7, 8,
  1387. 7, 7, 6, 7, 7, 7, 8, 8
  1388. };
  1389. const unsigned char uvQuanTable518[] = {
  1390. 6, 6, 6, 7, 7, 7, 7, 7,
  1391. 6, 6, 6, 7, 7, 7, 7, 7,
  1392. 6, 6, 6, 7, 7, 7, 7, 8,
  1393. 7, 7, 7, 7, 7, 7, 8, 8
  1394. };
  1395. const unsigned char *pYTable = yQuanTable518;
  1396. const unsigned char *pUVTable = uvQuanTable518;
  1397. unsigned char val0, val1;
  1398. int i, rc, reg = R51x_COMP_LUT_BEGIN;
  1399. PDEBUG(D_PROBE, "Uploading quantization tables");
  1400. for (i = 0; i < 16; i++) {
  1401. val0 = *pYTable++;
  1402. val1 = *pYTable++;
  1403. val0 &= 0x0f;
  1404. val1 &= 0x0f;
  1405. val0 |= val1 << 4;
  1406. rc = reg_w(sd, reg, val0);
  1407. if (rc < 0)
  1408. return rc;
  1409. val0 = *pUVTable++;
  1410. val1 = *pUVTable++;
  1411. val0 &= 0x0f;
  1412. val1 &= 0x0f;
  1413. val0 |= val1 << 4;
  1414. rc = reg_w(sd, reg + 16, val0);
  1415. if (rc < 0)
  1416. return rc;
  1417. reg++;
  1418. }
  1419. return 0;
  1420. }
  1421. /* This initializes the OV518/OV518+ and the sensor */
  1422. static int ov518_configure(struct gspca_dev *gspca_dev)
  1423. {
  1424. struct sd *sd = (struct sd *) gspca_dev;
  1425. int rc;
  1426. /* For 518 and 518+ */
  1427. static struct ov_regvals init_518[] = {
  1428. { R51x_SYS_RESET, 0x40 },
  1429. { R51x_SYS_INIT, 0xe1 },
  1430. { R51x_SYS_RESET, 0x3e },
  1431. { R51x_SYS_INIT, 0xe1 },
  1432. { R51x_SYS_RESET, 0x00 },
  1433. { R51x_SYS_INIT, 0xe1 },
  1434. { 0x46, 0x00 },
  1435. { 0x5d, 0x03 },
  1436. };
  1437. static struct ov_regvals norm_518[] = {
  1438. { R51x_SYS_SNAP, 0x02 }, /* Reset */
  1439. { R51x_SYS_SNAP, 0x01 }, /* Enable */
  1440. { 0x31, 0x0f },
  1441. { 0x5d, 0x03 },
  1442. { 0x24, 0x9f },
  1443. { 0x25, 0x90 },
  1444. { 0x20, 0x00 },
  1445. { 0x51, 0x04 },
  1446. { 0x71, 0x19 },
  1447. { 0x2f, 0x80 },
  1448. };
  1449. static struct ov_regvals norm_518_p[] = {
  1450. { R51x_SYS_SNAP, 0x02 }, /* Reset */
  1451. { R51x_SYS_SNAP, 0x01 }, /* Enable */
  1452. { 0x31, 0x0f },
  1453. { 0x5d, 0x03 },
  1454. { 0x24, 0x9f },
  1455. { 0x25, 0x90 },
  1456. { 0x20, 0x60 },
  1457. { 0x51, 0x02 },
  1458. { 0x71, 0x19 },
  1459. { 0x40, 0xff },
  1460. { 0x41, 0x42 },
  1461. { 0x46, 0x00 },
  1462. { 0x33, 0x04 },
  1463. { 0x21, 0x19 },
  1464. { 0x3f, 0x10 },
  1465. { 0x2f, 0x80 },
  1466. };
  1467. /* First 5 bits of custom ID reg are a revision ID on OV518 */
  1468. PDEBUG(D_PROBE, "Device revision %d",
  1469. 0x1F & reg_r(sd, R51x_SYS_CUST_ID));
  1470. rc = write_regvals(sd, init_518, ARRAY_SIZE(init_518));
  1471. if (rc < 0)
  1472. return rc;
  1473. /* Set LED GPIO pin to output mode */
  1474. rc = reg_w_mask(sd, R518_GPIO_CTL, 0x00, 0x02);
  1475. if (rc < 0)
  1476. return rc;
  1477. switch (sd->bridge) {
  1478. case BRIDGE_OV518:
  1479. rc = write_regvals(sd, norm_518, ARRAY_SIZE(norm_518));
  1480. if (rc < 0)
  1481. return rc;
  1482. break;
  1483. case BRIDGE_OV518PLUS:
  1484. rc = write_regvals(sd, norm_518_p, ARRAY_SIZE(norm_518_p));
  1485. if (rc < 0)
  1486. return rc;
  1487. break;
  1488. }
  1489. rc = ov518_upload_quan_tables(sd);
  1490. if (rc < 0) {
  1491. PDEBUG(D_ERR, "Error uploading quantization tables");
  1492. return rc;
  1493. }
  1494. rc = reg_w(sd, 0x2f, 0x80);
  1495. if (rc < 0)
  1496. return rc;
  1497. return 0;
  1498. }
  1499. static int ov519_configure(struct sd *sd)
  1500. {
  1501. static const struct ov_regvals init_519[] = {
  1502. { 0x5a, 0x6d }, /* EnableSystem */
  1503. { 0x53, 0x9b },
  1504. { 0x54, 0xff }, /* set bit2 to enable jpeg */
  1505. { 0x5d, 0x03 },
  1506. { 0x49, 0x01 },
  1507. { 0x48, 0x00 },
  1508. /* Set LED pin to output mode. Bit 4 must be cleared or sensor
  1509. * detection will fail. This deserves further investigation. */
  1510. { OV519_GPIO_IO_CTRL0, 0xee },
  1511. { 0x51, 0x0f }, /* SetUsbInit */
  1512. { 0x51, 0x00 },
  1513. { 0x22, 0x00 },
  1514. /* windows reads 0x55 at this point*/
  1515. };
  1516. return write_regvals(sd, init_519, ARRAY_SIZE(init_519));
  1517. }
  1518. /* this function is called at probe time */
  1519. static int sd_config(struct gspca_dev *gspca_dev,
  1520. const struct usb_device_id *id)
  1521. {
  1522. struct sd *sd = (struct sd *) gspca_dev;
  1523. struct cam *cam;
  1524. int ret = 0;
  1525. sd->bridge = id->driver_info & BRIDGE_MASK;
  1526. sd->invert_led = id->driver_info & BRIDGE_INVERT_LED;
  1527. switch (sd->bridge) {
  1528. case BRIDGE_OV518:
  1529. case BRIDGE_OV518PLUS:
  1530. ret = ov518_configure(gspca_dev);
  1531. break;
  1532. case BRIDGE_OV519:
  1533. ret = ov519_configure(sd);
  1534. break;
  1535. }
  1536. if (ret)
  1537. goto error;
  1538. ov51x_led_control(sd, 0); /* turn LED off */
  1539. /* Test for 76xx */
  1540. if (ov51x_set_slave_ids(sd, OV7xx0_SID) < 0)
  1541. goto error;
  1542. /* The OV519 must be more aggressive about sensor detection since
  1543. * I2C write will never fail if the sensor is not present. We have
  1544. * to try to initialize the sensor to detect its presence */
  1545. if (init_ov_sensor(sd) >= 0) {
  1546. if (ov7xx0_configure(sd) < 0) {
  1547. PDEBUG(D_ERR, "Failed to configure OV7xx0");
  1548. goto error;
  1549. }
  1550. } else {
  1551. /* Test for 6xx0 */
  1552. if (ov51x_set_slave_ids(sd, OV6xx0_SID) < 0)
  1553. goto error;
  1554. if (init_ov_sensor(sd) >= 0) {
  1555. if (ov6xx0_configure(sd) < 0) {
  1556. PDEBUG(D_ERR, "Failed to configure OV6xx0");
  1557. goto error;
  1558. }
  1559. } else {
  1560. /* Test for 8xx0 */
  1561. if (ov51x_set_slave_ids(sd, OV8xx0_SID) < 0)
  1562. goto error;
  1563. if (init_ov_sensor(sd) < 0) {
  1564. PDEBUG(D_ERR,
  1565. "Can't determine sensor slave IDs");
  1566. goto error;
  1567. }
  1568. if (ov8xx0_configure(sd) < 0) {
  1569. PDEBUG(D_ERR,
  1570. "Failed to configure OV8xx0 sensor");
  1571. goto error;
  1572. }
  1573. }
  1574. }
  1575. cam = &gspca_dev->cam;
  1576. switch (sd->bridge) {
  1577. case BRIDGE_OV518:
  1578. case BRIDGE_OV518PLUS:
  1579. if (!sd->sif) {
  1580. cam->cam_mode = ov518_vga_mode;
  1581. cam->nmodes = ARRAY_SIZE(ov518_vga_mode);
  1582. } else {
  1583. cam->cam_mode = ov518_sif_mode;
  1584. cam->nmodes = ARRAY_SIZE(ov518_sif_mode);
  1585. }
  1586. break;
  1587. case BRIDGE_OV519:
  1588. if (!sd->sif) {
  1589. cam->cam_mode = ov519_vga_mode;
  1590. cam->nmodes = ARRAY_SIZE(ov519_vga_mode);
  1591. } else {
  1592. cam->cam_mode = ov519_sif_mode;
  1593. cam->nmodes = ARRAY_SIZE(ov519_sif_mode);
  1594. }
  1595. break;
  1596. }
  1597. sd->brightness = BRIGHTNESS_DEF;
  1598. if (sd->sensor == SEN_OV6630 || sd->sensor == SEN_OV66308AF)
  1599. sd->contrast = 200; /* The default is too low for the ov6630 */
  1600. else
  1601. sd->contrast = CONTRAST_DEF;
  1602. sd->colors = COLOR_DEF;
  1603. sd->hflip = HFLIP_DEF;
  1604. sd->vflip = VFLIP_DEF;
  1605. sd->autobrightness = AUTOBRIGHT_DEF;
  1606. if (sd->sensor == SEN_OV7670) {
  1607. sd->freq = OV7670_FREQ_DEF;
  1608. gspca_dev->ctrl_dis = 1 << FREQ_IDX;
  1609. } else {
  1610. sd->freq = FREQ_DEF;
  1611. gspca_dev->ctrl_dis = (1 << HFLIP_IDX) | (1 << VFLIP_IDX) |
  1612. (1 << OV7670_FREQ_IDX);
  1613. }
  1614. if (sd->sensor == SEN_OV7640 || sd->sensor == SEN_OV7670)
  1615. gspca_dev->ctrl_dis |= 1 << AUTOBRIGHT_IDX;
  1616. /* OV8610 Frequency filter control should work but needs testing */
  1617. if (sd->sensor == SEN_OV8610)
  1618. gspca_dev->ctrl_dis |= 1 << FREQ_IDX;
  1619. return 0;
  1620. error:
  1621. PDEBUG(D_ERR, "OV519 Config failed");
  1622. return -EBUSY;
  1623. }
  1624. /* this function is called at probe and resume time */
  1625. static int sd_init(struct gspca_dev *gspca_dev)
  1626. {
  1627. struct sd *sd = (struct sd *) gspca_dev;
  1628. /* initialize the sensor */
  1629. switch (sd->sensor) {
  1630. case SEN_OV6620:
  1631. if (write_i2c_regvals(sd, norm_6x20, ARRAY_SIZE(norm_6x20)))
  1632. return -EIO;
  1633. break;
  1634. case SEN_OV6630:
  1635. case SEN_OV66308AF:
  1636. if (write_i2c_regvals(sd, norm_6x30, ARRAY_SIZE(norm_6x30)))
  1637. return -EIO;
  1638. break;
  1639. default:
  1640. /* case SEN_OV7610: */
  1641. /* case SEN_OV76BE: */
  1642. if (write_i2c_regvals(sd, norm_7610, ARRAY_SIZE(norm_7610)))
  1643. return -EIO;
  1644. break;
  1645. case SEN_OV7620:
  1646. if (write_i2c_regvals(sd, norm_7620, ARRAY_SIZE(norm_7620)))
  1647. return -EIO;
  1648. break;
  1649. case SEN_OV7640:
  1650. if (write_i2c_regvals(sd, norm_7640, ARRAY_SIZE(norm_7640)))
  1651. return -EIO;
  1652. break;
  1653. case SEN_OV7670:
  1654. if (write_i2c_regvals(sd, norm_7670, ARRAY_SIZE(norm_7670)))
  1655. return -EIO;
  1656. break;
  1657. case SEN_OV8610:
  1658. if (write_i2c_regvals(sd, norm_8610, ARRAY_SIZE(norm_8610)))
  1659. return -EIO;
  1660. break;
  1661. }
  1662. return 0;
  1663. }
  1664. /* Sets up the OV518/OV518+ with the given image parameters
  1665. *
  1666. * OV518 needs a completely different approach, until we can figure out what
  1667. * the individual registers do. Also, only 15 FPS is supported now.
  1668. *
  1669. * Do not put any sensor-specific code in here (including I2C I/O functions)
  1670. */
  1671. static int ov518_mode_init_regs(struct sd *sd)
  1672. {
  1673. int hsegs, vsegs;
  1674. /******** Set the mode ********/
  1675. reg_w(sd, 0x2b, 0);
  1676. reg_w(sd, 0x2c, 0);
  1677. reg_w(sd, 0x2d, 0);
  1678. reg_w(sd, 0x2e, 0);
  1679. reg_w(sd, 0x3b, 0);
  1680. reg_w(sd, 0x3c, 0);
  1681. reg_w(sd, 0x3d, 0);
  1682. reg_w(sd, 0x3e, 0);
  1683. if (sd->bridge == BRIDGE_OV518) {
  1684. /* Set 8-bit (YVYU) input format */
  1685. reg_w_mask(sd, 0x20, 0x08, 0x08);
  1686. /* Set 12-bit (4:2:0) output format */
  1687. reg_w_mask(sd, 0x28, 0x80, 0xf0);
  1688. reg_w_mask(sd, 0x38, 0x80, 0xf0);
  1689. } else {
  1690. reg_w(sd, 0x28, 0x80);
  1691. reg_w(sd, 0x38, 0x80);
  1692. }
  1693. hsegs = sd->gspca_dev.width / 16;
  1694. vsegs = sd->gspca_dev.height / 4;
  1695. reg_w(sd, 0x29, hsegs);
  1696. reg_w(sd, 0x2a, vsegs);
  1697. reg_w(sd, 0x39, hsegs);
  1698. reg_w(sd, 0x3a, vsegs);
  1699. /* Windows driver does this here; who knows why */
  1700. reg_w(sd, 0x2f, 0x80);
  1701. /******** Set the framerate (to 30 FPS) ********/
  1702. if (sd->bridge == BRIDGE_OV518PLUS)
  1703. sd->clockdiv = 1;
  1704. else
  1705. sd->clockdiv = 0;
  1706. /* Mode independent, but framerate dependent, regs */
  1707. reg_w(sd, 0x51, 0x04); /* Clock divider; lower==faster */
  1708. reg_w(sd, 0x22, 0x18);
  1709. reg_w(sd, 0x23, 0xff);
  1710. if (sd->bridge == BRIDGE_OV518PLUS)
  1711. reg_w(sd, 0x21, 0x19);
  1712. else
  1713. reg_w(sd, 0x71, 0x17); /* Compression-related? */
  1714. /* FIXME: Sensor-specific */
  1715. /* Bit 5 is what matters here. Of course, it is "reserved" */
  1716. i2c_w(sd, 0x54, 0x23);
  1717. reg_w(sd, 0x2f, 0x80);
  1718. if (sd->bridge == BRIDGE_OV518PLUS) {
  1719. reg_w(sd, 0x24, 0x94);
  1720. reg_w(sd, 0x25, 0x90);
  1721. ov518_reg_w32(sd, 0xc4, 400, 2); /* 190h */
  1722. ov518_reg_w32(sd, 0xc6, 540, 2); /* 21ch */
  1723. ov518_reg_w32(sd, 0xc7, 540, 2); /* 21ch */
  1724. ov518_reg_w32(sd, 0xc8, 108, 2); /* 6ch */
  1725. ov518_reg_w32(sd, 0xca, 131098, 3); /* 2001ah */
  1726. ov518_reg_w32(sd, 0xcb, 532, 2); /* 214h */
  1727. ov518_reg_w32(sd, 0xcc, 2400, 2); /* 960h */
  1728. ov518_reg_w32(sd, 0xcd, 32, 2); /* 20h */
  1729. ov518_reg_w32(sd, 0xce, 608, 2); /* 260h */
  1730. } else {
  1731. reg_w(sd, 0x24, 0x9f);
  1732. reg_w(sd, 0x25, 0x90);
  1733. ov518_reg_w32(sd, 0xc4, 400, 2); /* 190h */
  1734. ov518_reg_w32(sd, 0xc6, 381, 2); /* 17dh */
  1735. ov518_reg_w32(sd, 0xc7, 381, 2); /* 17dh */
  1736. ov518_reg_w32(sd, 0xc8, 128, 2); /* 80h */
  1737. ov518_reg_w32(sd, 0xca, 183331, 3); /* 2cc23h */
  1738. ov518_reg_w32(sd, 0xcb, 746, 2); /* 2eah */
  1739. ov518_reg_w32(sd, 0xcc, 1750, 2); /* 6d6h */
  1740. ov518_reg_w32(sd, 0xcd, 45, 2); /* 2dh */
  1741. ov518_reg_w32(sd, 0xce, 851, 2); /* 353h */
  1742. }
  1743. reg_w(sd, 0x2f, 0x80);
  1744. return 0;
  1745. }
  1746. /* Sets up the OV519 with the given image parameters
  1747. *
  1748. * OV519 needs a completely different approach, until we can figure out what
  1749. * the individual registers do.
  1750. *
  1751. * Do not put any sensor-specific code in here (including I2C I/O functions)
  1752. */
  1753. static int ov519_mode_init_regs(struct sd *sd)
  1754. {
  1755. static const struct ov_regvals mode_init_519_ov7670[] = {
  1756. { 0x5d, 0x03 }, /* Turn off suspend mode */
  1757. { 0x53, 0x9f }, /* was 9b in 1.65-1.08 */
  1758. { 0x54, 0x0f }, /* bit2 (jpeg enable) */
  1759. { 0xa2, 0x20 }, /* a2-a5 are undocumented */
  1760. { 0xa3, 0x18 },
  1761. { 0xa4, 0x04 },
  1762. { 0xa5, 0x28 },
  1763. { 0x37, 0x00 }, /* SetUsbInit */
  1764. { 0x55, 0x02 }, /* 4.096 Mhz audio clock */
  1765. /* Enable both fields, YUV Input, disable defect comp (why?) */
  1766. { 0x20, 0x0c },
  1767. { 0x21, 0x38 },
  1768. { 0x22, 0x1d },
  1769. { 0x17, 0x50 }, /* undocumented */
  1770. { 0x37, 0x00 }, /* undocumented */
  1771. { 0x40, 0xff }, /* I2C timeout counter */
  1772. { 0x46, 0x00 }, /* I2C clock prescaler */
  1773. { 0x59, 0x04 }, /* new from windrv 090403 */
  1774. { 0xff, 0x00 }, /* undocumented */
  1775. /* windows reads 0x55 at this point, why? */
  1776. };
  1777. static const struct ov_regvals mode_init_519[] = {
  1778. { 0x5d, 0x03 }, /* Turn off suspend mode */
  1779. { 0x53, 0x9f }, /* was 9b in 1.65-1.08 */
  1780. { 0x54, 0x0f }, /* bit2 (jpeg enable) */
  1781. { 0xa2, 0x20 }, /* a2-a5 are undocumented */
  1782. { 0xa3, 0x18 },
  1783. { 0xa4, 0x04 },
  1784. { 0xa5, 0x28 },
  1785. { 0x37, 0x00 }, /* SetUsbInit */
  1786. { 0x55, 0x02 }, /* 4.096 Mhz audio clock */
  1787. /* Enable both fields, YUV Input, disable defect comp (why?) */
  1788. { 0x22, 0x1d },
  1789. { 0x17, 0x50 }, /* undocumented */
  1790. { 0x37, 0x00 }, /* undocumented */
  1791. { 0x40, 0xff }, /* I2C timeout counter */
  1792. { 0x46, 0x00 }, /* I2C clock prescaler */
  1793. { 0x59, 0x04 }, /* new from windrv 090403 */
  1794. { 0xff, 0x00 }, /* undocumented */
  1795. /* windows reads 0x55 at this point, why? */
  1796. };
  1797. /******** Set the mode ********/
  1798. if (sd->sensor != SEN_OV7670) {
  1799. if (write_regvals(sd, mode_init_519,
  1800. ARRAY_SIZE(mode_init_519)))
  1801. return -EIO;
  1802. if (sd->sensor == SEN_OV7640) {
  1803. /* Select 8-bit input mode */
  1804. reg_w_mask(sd, OV519_R20_DFR, 0x10, 0x10);
  1805. }
  1806. } else {
  1807. if (write_regvals(sd, mode_init_519_ov7670,
  1808. ARRAY_SIZE(mode_init_519_ov7670)))
  1809. return -EIO;
  1810. }
  1811. reg_w(sd, OV519_R10_H_SIZE, sd->gspca_dev.width >> 4);
  1812. reg_w(sd, OV519_R11_V_SIZE, sd->gspca_dev.height >> 3);
  1813. if (sd->sensor == SEN_OV7670 &&
  1814. sd->gspca_dev.cam.cam_mode[sd->gspca_dev.curr_mode].priv)
  1815. reg_w(sd, OV519_R12_X_OFFSETL, 0x04);
  1816. else
  1817. reg_w(sd, OV519_R12_X_OFFSETL, 0x00);
  1818. reg_w(sd, OV519_R13_X_OFFSETH, 0x00);
  1819. reg_w(sd, OV519_R14_Y_OFFSETL, 0x00);
  1820. reg_w(sd, OV519_R15_Y_OFFSETH, 0x00);
  1821. reg_w(sd, OV519_R16_DIVIDER, 0x00);
  1822. reg_w(sd, OV519_R25_FORMAT, 0x03); /* YUV422 */
  1823. reg_w(sd, 0x26, 0x00); /* Undocumented */
  1824. /******** Set the framerate ********/
  1825. if (frame_rate > 0)
  1826. sd->frame_rate = frame_rate;
  1827. /* FIXME: These are only valid at the max resolution. */
  1828. sd->clockdiv = 0;
  1829. switch (sd->sensor) {
  1830. case SEN_OV7640:
  1831. switch (sd->frame_rate) {
  1832. default:
  1833. /* case 30: */
  1834. reg_w(sd, 0xa4, 0x0c);
  1835. reg_w(sd, 0x23, 0xff);
  1836. break;
  1837. case 25:
  1838. reg_w(sd, 0xa4, 0x0c);
  1839. reg_w(sd, 0x23, 0x1f);
  1840. break;
  1841. case 20:
  1842. reg_w(sd, 0xa4, 0x0c);
  1843. reg_w(sd, 0x23, 0x1b);
  1844. break;
  1845. case 15:
  1846. reg_w(sd, 0xa4, 0x04);
  1847. reg_w(sd, 0x23, 0xff);
  1848. sd->clockdiv = 1;
  1849. break;
  1850. case 10:
  1851. reg_w(sd, 0xa4, 0x04);
  1852. reg_w(sd, 0x23, 0x1f);
  1853. sd->clockdiv = 1;
  1854. break;
  1855. case 5:
  1856. reg_w(sd, 0xa4, 0x04);
  1857. reg_w(sd, 0x23, 0x1b);
  1858. sd->clockdiv = 1;
  1859. break;
  1860. }
  1861. break;
  1862. case SEN_OV8610:
  1863. switch (sd->frame_rate) {
  1864. default: /* 15 fps */
  1865. /* case 15: */
  1866. reg_w(sd, 0xa4, 0x06);
  1867. reg_w(sd, 0x23, 0xff);
  1868. break;
  1869. case 10:
  1870. reg_w(sd, 0xa4, 0x06);
  1871. reg_w(sd, 0x23, 0x1f);
  1872. break;
  1873. case 5:
  1874. reg_w(sd, 0xa4, 0x06);
  1875. reg_w(sd, 0x23, 0x1b);
  1876. break;
  1877. }
  1878. break;
  1879. case SEN_OV7670: /* guesses, based on 7640 */
  1880. PDEBUG(D_STREAM, "Setting framerate to %d fps",
  1881. (sd->frame_rate == 0) ? 15 : sd->frame_rate);
  1882. reg_w(sd, 0xa4, 0x10);
  1883. switch (sd->frame_rate) {
  1884. case 30:
  1885. reg_w(sd, 0x23, 0xff);
  1886. break;
  1887. case 20:
  1888. reg_w(sd, 0x23, 0x1b);
  1889. break;
  1890. default:
  1891. /* case 15: */
  1892. reg_w(sd, 0x23, 0xff);
  1893. sd->clockdiv = 1;
  1894. break;
  1895. }
  1896. break;
  1897. }
  1898. return 0;
  1899. }
  1900. static int mode_init_ov_sensor_regs(struct sd *sd)
  1901. {
  1902. struct gspca_dev *gspca_dev;
  1903. int qvga;
  1904. gspca_dev = &sd->gspca_dev;
  1905. qvga = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv & 1;
  1906. /******** Mode (VGA/QVGA) and sensor specific regs ********/
  1907. switch (sd->sensor) {
  1908. case SEN_OV8610:
  1909. /* For OV8610 qvga means qsvga */
  1910. i2c_w_mask(sd, OV7610_REG_COM_C, qvga ? (1 << 5) : 0, 1 << 5);
  1911. break;
  1912. case SEN_OV7610:
  1913. i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
  1914. break;
  1915. case SEN_OV7620:
  1916. /* i2c_w(sd, 0x2b, 0x00); */
  1917. i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
  1918. i2c_w_mask(sd, 0x28, qvga ? 0x00 : 0x20, 0x20);
  1919. i2c_w(sd, 0x24, qvga ? 0x20 : 0x3a);
  1920. i2c_w(sd, 0x25, qvga ? 0x30 : 0x60);
  1921. i2c_w_mask(sd, 0x2d, qvga ? 0x40 : 0x00, 0x40);
  1922. i2c_w_mask(sd, 0x67, qvga ? 0xf0 : 0x90, 0xf0);
  1923. i2c_w_mask(sd, 0x74, qvga ? 0x20 : 0x00, 0x20);
  1924. break;
  1925. case SEN_OV76BE:
  1926. /* i2c_w(sd, 0x2b, 0x00); */
  1927. i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
  1928. break;
  1929. case SEN_OV7640:
  1930. /* i2c_w(sd, 0x2b, 0x00); */
  1931. i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
  1932. i2c_w_mask(sd, 0x28, qvga ? 0x00 : 0x20, 0x20);
  1933. /* i2c_w(sd, 0x24, qvga ? 0x20 : 0x3a); */
  1934. /* i2c_w(sd, 0x25, qvga ? 0x30 : 0x60); */
  1935. /* i2c_w_mask(sd, 0x2d, qvga ? 0x40 : 0x00, 0x40); */
  1936. /* i2c_w_mask(sd, 0x67, qvga ? 0xf0 : 0x90, 0xf0); */
  1937. /* i2c_w_mask(sd, 0x74, qvga ? 0x20 : 0x00, 0x20); */
  1938. break;
  1939. case SEN_OV7670:
  1940. /* set COM7_FMT_VGA or COM7_FMT_QVGA
  1941. * do we need to set anything else?
  1942. * HSTART etc are set in set_ov_sensor_window itself */
  1943. i2c_w_mask(sd, OV7670_REG_COM7,
  1944. qvga ? OV7670_COM7_FMT_QVGA : OV7670_COM7_FMT_VGA,
  1945. OV7670_COM7_FMT_MASK);
  1946. break;
  1947. case SEN_OV6620:
  1948. case SEN_OV6630:
  1949. case SEN_OV66308AF:
  1950. i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
  1951. break;
  1952. default:
  1953. return -EINVAL;
  1954. }
  1955. /******** Palette-specific regs ********/
  1956. if (sd->sensor == SEN_OV7610 || sd->sensor == SEN_OV76BE) {
  1957. /* not valid on the OV6620/OV7620/6630? */
  1958. i2c_w_mask(sd, 0x0e, 0x00, 0x40);
  1959. }
  1960. /* The OV518 needs special treatment. Although both the OV518
  1961. * and the OV6630 support a 16-bit video bus, only the 8 bit Y
  1962. * bus is actually used. The UV bus is tied to ground.
  1963. * Therefore, the OV6630 needs to be in 8-bit multiplexed
  1964. * output mode */
  1965. /* OV7640 is 8-bit only */
  1966. if (sd->sensor != SEN_OV6630 && sd->sensor != SEN_OV66308AF &&
  1967. sd->sensor != SEN_OV7640)
  1968. i2c_w_mask(sd, 0x13, 0x00, 0x20);
  1969. /******** Clock programming ********/
  1970. /* The OV6620 needs special handling. This prevents the
  1971. * severe banding that normally occurs */
  1972. if (sd->sensor == SEN_OV6620) {
  1973. /* Clock down */
  1974. i2c_w(sd, 0x2a, 0x04);
  1975. i2c_w(sd, 0x11, sd->clockdiv);
  1976. i2c_w(sd, 0x2a, 0x84);
  1977. /* This next setting is critical. It seems to improve
  1978. * the gain or the contrast. The "reserved" bits seem
  1979. * to have some effect in this case. */
  1980. i2c_w(sd, 0x2d, 0x85);
  1981. } else {
  1982. i2c_w(sd, 0x11, sd->clockdiv);
  1983. }
  1984. /******** Special Features ********/
  1985. /* no evidence this is possible with OV7670, either */
  1986. /* Test Pattern */
  1987. if (sd->sensor != SEN_OV7640 && sd->sensor != SEN_OV7670)
  1988. i2c_w_mask(sd, 0x12, 0x00, 0x02);
  1989. /* Enable auto white balance */
  1990. if (sd->sensor == SEN_OV7670)
  1991. i2c_w_mask(sd, OV7670_REG_COM8, OV7670_COM8_AWB,
  1992. OV7670_COM8_AWB);
  1993. else
  1994. i2c_w_mask(sd, 0x12, 0x04, 0x04);
  1995. /* This will go away as soon as ov51x_mode_init_sensor_regs() */
  1996. /* is fully tested. */
  1997. /* 7620/6620/6630? don't have register 0x35, so play it safe */
  1998. if (sd->sensor == SEN_OV7610 || sd->sensor == SEN_OV76BE) {
  1999. if (!qvga)
  2000. i2c_w(sd, 0x35, 0x9e);
  2001. else
  2002. i2c_w(sd, 0x35, 0x1e);
  2003. }
  2004. return 0;
  2005. }
  2006. static void sethvflip(struct sd *sd)
  2007. {
  2008. if (sd->sensor != SEN_OV7670)
  2009. return;
  2010. if (sd->gspca_dev.streaming)
  2011. ov51x_stop(sd);
  2012. i2c_w_mask(sd, OV7670_REG_MVFP,
  2013. OV7670_MVFP_MIRROR * sd->hflip
  2014. | OV7670_MVFP_VFLIP * sd->vflip,
  2015. OV7670_MVFP_MIRROR | OV7670_MVFP_VFLIP);
  2016. if (sd->gspca_dev.streaming)
  2017. ov51x_restart(sd);
  2018. }
  2019. static int set_ov_sensor_window(struct sd *sd)
  2020. {
  2021. struct gspca_dev *gspca_dev;
  2022. int qvga, crop;
  2023. int hwsbase, hwebase, vwsbase, vwebase, hwscale, vwscale;
  2024. int ret, hstart, hstop, vstop, vstart;
  2025. __u8 v;
  2026. gspca_dev = &sd->gspca_dev;
  2027. qvga = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv & 1;
  2028. crop = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv & 2;
  2029. /* The different sensor ICs handle setting up of window differently.
  2030. * IF YOU SET IT WRONG, YOU WILL GET ALL ZERO ISOC DATA FROM OV51x!! */
  2031. switch (sd->sensor) {
  2032. case SEN_OV8610:
  2033. hwsbase = 0x1e;
  2034. hwebase = 0x1e;
  2035. vwsbase = 0x02;
  2036. vwebase = 0x02;
  2037. break;
  2038. case SEN_OV7610:
  2039. case SEN_OV76BE:
  2040. hwsbase = 0x38;
  2041. hwebase = 0x3a;
  2042. vwsbase = vwebase = 0x05;
  2043. break;
  2044. case SEN_OV6620:
  2045. case SEN_OV6630:
  2046. case SEN_OV66308AF:
  2047. hwsbase = 0x38;
  2048. hwebase = 0x3a;
  2049. vwsbase = 0x05;
  2050. vwebase = 0x06;
  2051. if (sd->sensor == SEN_OV66308AF && qvga)
  2052. /* HDG: this fixes U and V getting swapped */
  2053. hwsbase++;
  2054. if (crop) {
  2055. hwsbase += 8;
  2056. hwebase += 8;
  2057. vwsbase += 11;
  2058. vwebase += 11;
  2059. }
  2060. break;
  2061. case SEN_OV7620:
  2062. hwsbase = 0x2f; /* From 7620.SET (spec is wrong) */
  2063. hwebase = 0x2f;
  2064. vwsbase = vwebase = 0x05;
  2065. break;
  2066. case SEN_OV7640:
  2067. hwsbase = 0x1a;
  2068. hwebase = 0x1a;
  2069. vwsbase = vwebase = 0x03;
  2070. break;
  2071. case SEN_OV7670:
  2072. /*handling of OV7670 hardware sensor start and stop values
  2073. * is very odd, compared to the other OV sensors */
  2074. vwsbase = vwebase = hwebase = hwsbase = 0x00;
  2075. break;
  2076. default:
  2077. return -EINVAL;
  2078. }
  2079. switch (sd->sensor) {
  2080. case SEN_OV6620:
  2081. case SEN_OV6630:
  2082. case SEN_OV66308AF:
  2083. if (qvga) { /* QCIF */
  2084. hwscale = 0;
  2085. vwscale = 0;
  2086. } else { /* CIF */
  2087. hwscale = 1;
  2088. vwscale = 1; /* The datasheet says 0;
  2089. * it's wrong */
  2090. }
  2091. break;
  2092. case SEN_OV8610:
  2093. if (qvga) { /* QSVGA */
  2094. hwscale = 1;
  2095. vwscale = 1;
  2096. } else { /* SVGA */
  2097. hwscale = 2;
  2098. vwscale = 2;
  2099. }
  2100. break;
  2101. default: /* SEN_OV7xx0 */
  2102. if (qvga) { /* QVGA */
  2103. hwscale = 1;
  2104. vwscale = 0;
  2105. } else { /* VGA */
  2106. hwscale = 2;
  2107. vwscale = 1;
  2108. }
  2109. }
  2110. ret = mode_init_ov_sensor_regs(sd);
  2111. if (ret < 0)
  2112. return ret;
  2113. if (sd->sensor == SEN_OV8610) {
  2114. i2c_w_mask(sd, 0x2d, 0x05, 0x40);
  2115. /* old 0x95, new 0x05 from windrv 090403 */
  2116. /* bits 5-7: reserved */
  2117. i2c_w_mask(sd, 0x28, 0x20, 0x20);
  2118. /* bit 5: progressive mode on */
  2119. }
  2120. /* The below is wrong for OV7670s because their window registers
  2121. * only store the high bits in 0x17 to 0x1a */
  2122. /* SRH Use sd->max values instead of requested win values */
  2123. /* SCS Since we're sticking with only the max hardware widths
  2124. * for a given mode */
  2125. /* I can hard code this for OV7670s */
  2126. /* Yes, these numbers do look odd, but they're tested and work! */
  2127. if (sd->sensor == SEN_OV7670) {
  2128. if (qvga) { /* QVGA from ov7670.c by
  2129. * Jonathan Corbet */
  2130. hstart = 164;
  2131. hstop = 28;
  2132. vstart = 14;
  2133. vstop = 494;
  2134. } else { /* VGA */
  2135. hstart = 158;
  2136. hstop = 14;
  2137. vstart = 10;
  2138. vstop = 490;
  2139. }
  2140. /* OV7670 hardware window registers are split across
  2141. * multiple locations */
  2142. i2c_w(sd, OV7670_REG_HSTART, hstart >> 3);
  2143. i2c_w(sd, OV7670_REG_HSTOP, hstop >> 3);
  2144. v = i2c_r(sd, OV7670_REG_HREF);
  2145. v = (v & 0xc0) | ((hstop & 0x7) << 3) | (hstart & 0x07);
  2146. msleep(10); /* need to sleep between read and write to
  2147. * same reg! */
  2148. i2c_w(sd, OV7670_REG_HREF, v);
  2149. i2c_w(sd, OV7670_REG_VSTART, vstart >> 2);
  2150. i2c_w(sd, OV7670_REG_VSTOP, vstop >> 2);
  2151. v = i2c_r(sd, OV7670_REG_VREF);
  2152. v = (v & 0xc0) | ((vstop & 0x3) << 2) | (vstart & 0x03);
  2153. msleep(10); /* need to sleep between read and write to
  2154. * same reg! */
  2155. i2c_w(sd, OV7670_REG_VREF, v);
  2156. } else {
  2157. i2c_w(sd, 0x17, hwsbase);
  2158. i2c_w(sd, 0x18, hwebase + (sd->gspca_dev.width >> hwscale));
  2159. i2c_w(sd, 0x19, vwsbase);
  2160. i2c_w(sd, 0x1a, vwebase + (sd->gspca_dev.height >> vwscale));
  2161. }
  2162. return 0;
  2163. }
  2164. /* -- start the camera -- */
  2165. static int sd_start(struct gspca_dev *gspca_dev)
  2166. {
  2167. struct sd *sd = (struct sd *) gspca_dev;
  2168. int ret = 0;
  2169. switch (sd->bridge) {
  2170. case BRIDGE_OV518:
  2171. case BRIDGE_OV518PLUS:
  2172. ret = ov518_mode_init_regs(sd);
  2173. break;
  2174. case BRIDGE_OV519:
  2175. ret = ov519_mode_init_regs(sd);
  2176. break;
  2177. }
  2178. if (ret < 0)
  2179. goto out;
  2180. ret = set_ov_sensor_window(sd);
  2181. if (ret < 0)
  2182. goto out;
  2183. setcontrast(gspca_dev);
  2184. setbrightness(gspca_dev);
  2185. setcolors(gspca_dev);
  2186. sethvflip(sd);
  2187. setautobrightness(sd);
  2188. setfreq(sd);
  2189. ret = ov51x_restart(sd);
  2190. if (ret < 0)
  2191. goto out;
  2192. ov51x_led_control(sd, 1);
  2193. return 0;
  2194. out:
  2195. PDEBUG(D_ERR, "camera start error:%d", ret);
  2196. return ret;
  2197. }
  2198. static void sd_stopN(struct gspca_dev *gspca_dev)
  2199. {
  2200. struct sd *sd = (struct sd *) gspca_dev;
  2201. ov51x_stop(sd);
  2202. ov51x_led_control(sd, 0);
  2203. }
  2204. static void ov518_pkt_scan(struct gspca_dev *gspca_dev,
  2205. struct gspca_frame *frame, /* target */
  2206. __u8 *data, /* isoc packet */
  2207. int len) /* iso packet length */
  2208. {
  2209. struct sd *sd = (struct sd *) gspca_dev;
  2210. /* A false positive here is likely, until OVT gives me
  2211. * the definitive SOF/EOF format */
  2212. if ((!(data[0] | data[1] | data[2] | data[3] | data[5])) && data[6]) {
  2213. gspca_frame_add(gspca_dev, LAST_PACKET, frame, data, 0);
  2214. gspca_frame_add(gspca_dev, FIRST_PACKET, frame, data, 0);
  2215. sd->packet_nr = 0;
  2216. }
  2217. if (gspca_dev->last_packet_type == DISCARD_PACKET)
  2218. return;
  2219. /* Does this device use packet numbers ? */
  2220. if (len & 7) {
  2221. len--;
  2222. if (sd->packet_nr == data[len])
  2223. sd->packet_nr++;
  2224. /* The last few packets of the frame (which are all 0's
  2225. except that they may contain part of the footer), are
  2226. numbered 0 */
  2227. else if (sd->packet_nr == 0 || data[len]) {
  2228. PDEBUG(D_ERR, "Invalid packet nr: %d (expect: %d)",
  2229. (int)data[len], (int)sd->packet_nr);
  2230. gspca_dev->last_packet_type = DISCARD_PACKET;
  2231. return;
  2232. }
  2233. }
  2234. /* intermediate packet */
  2235. gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
  2236. }
  2237. static void ov519_pkt_scan(struct gspca_dev *gspca_dev,
  2238. struct gspca_frame *frame, /* target */
  2239. __u8 *data, /* isoc packet */
  2240. int len) /* iso packet length */
  2241. {
  2242. /* Header of ov519 is 16 bytes:
  2243. * Byte Value Description
  2244. * 0 0xff magic
  2245. * 1 0xff magic
  2246. * 2 0xff magic
  2247. * 3 0xXX 0x50 = SOF, 0x51 = EOF
  2248. * 9 0xXX 0x01 initial frame without data,
  2249. * 0x00 standard frame with image
  2250. * 14 Lo in EOF: length of image data / 8
  2251. * 15 Hi
  2252. */
  2253. if (data[0] == 0xff && data[1] == 0xff && data[2] == 0xff) {
  2254. switch (data[3]) {
  2255. case 0x50: /* start of frame */
  2256. #define HDRSZ 16
  2257. data += HDRSZ;
  2258. len -= HDRSZ;
  2259. #undef HDRSZ
  2260. if (data[0] == 0xff || data[1] == 0xd8)
  2261. gspca_frame_add(gspca_dev, FIRST_PACKET, frame,
  2262. data, len);
  2263. else
  2264. gspca_dev->last_packet_type = DISCARD_PACKET;
  2265. return;
  2266. case 0x51: /* end of frame */
  2267. if (data[9] != 0)
  2268. gspca_dev->last_packet_type = DISCARD_PACKET;
  2269. gspca_frame_add(gspca_dev, LAST_PACKET, frame,
  2270. data, 0);
  2271. return;
  2272. }
  2273. }
  2274. /* intermediate packet */
  2275. gspca_frame_add(gspca_dev, INTER_PACKET, frame,
  2276. data, len);
  2277. }
  2278. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  2279. struct gspca_frame *frame, /* target */
  2280. __u8 *data, /* isoc packet */
  2281. int len) /* iso packet length */
  2282. {
  2283. struct sd *sd = (struct sd *) gspca_dev;
  2284. switch (sd->bridge) {
  2285. case BRIDGE_OV511:
  2286. case BRIDGE_OV511PLUS:
  2287. break;
  2288. case BRIDGE_OV518:
  2289. case BRIDGE_OV518PLUS:
  2290. ov518_pkt_scan(gspca_dev, frame, data, len);
  2291. break;
  2292. case BRIDGE_OV519:
  2293. ov519_pkt_scan(gspca_dev, frame, data, len);
  2294. break;
  2295. }
  2296. }
  2297. /* -- management routines -- */
  2298. static void setbrightness(struct gspca_dev *gspca_dev)
  2299. {
  2300. struct sd *sd = (struct sd *) gspca_dev;
  2301. int val;
  2302. val = sd->brightness;
  2303. switch (sd->sensor) {
  2304. case SEN_OV8610:
  2305. case SEN_OV7610:
  2306. case SEN_OV76BE:
  2307. case SEN_OV6620:
  2308. case SEN_OV6630:
  2309. case SEN_OV66308AF:
  2310. case SEN_OV7640:
  2311. i2c_w(sd, OV7610_REG_BRT, val);
  2312. break;
  2313. case SEN_OV7620:
  2314. /* 7620 doesn't like manual changes when in auto mode */
  2315. if (!sd->autobrightness)
  2316. i2c_w(sd, OV7610_REG_BRT, val);
  2317. break;
  2318. case SEN_OV7670:
  2319. /*win trace
  2320. * i2c_w_mask(sd, OV7670_REG_COM8, 0, OV7670_COM8_AEC); */
  2321. i2c_w(sd, OV7670_REG_BRIGHT, ov7670_abs_to_sm(val));
  2322. break;
  2323. }
  2324. }
  2325. static void setcontrast(struct gspca_dev *gspca_dev)
  2326. {
  2327. struct sd *sd = (struct sd *) gspca_dev;
  2328. int val;
  2329. val = sd->contrast;
  2330. switch (sd->sensor) {
  2331. case SEN_OV7610:
  2332. case SEN_OV6620:
  2333. i2c_w(sd, OV7610_REG_CNT, val);
  2334. break;
  2335. case SEN_OV6630:
  2336. case SEN_OV66308AF:
  2337. i2c_w_mask(sd, OV7610_REG_CNT, val >> 4, 0x0f);
  2338. break;
  2339. case SEN_OV8610: {
  2340. static const __u8 ctab[] = {
  2341. 0x03, 0x09, 0x0b, 0x0f, 0x53, 0x6f, 0x35, 0x7f
  2342. };
  2343. /* Use Y gamma control instead. Bit 0 enables it. */
  2344. i2c_w(sd, 0x64, ctab[val >> 5]);
  2345. break;
  2346. }
  2347. case SEN_OV7620: {
  2348. static const __u8 ctab[] = {
  2349. 0x01, 0x05, 0x09, 0x11, 0x15, 0x35, 0x37, 0x57,
  2350. 0x5b, 0xa5, 0xa7, 0xc7, 0xc9, 0xcf, 0xef, 0xff
  2351. };
  2352. /* Use Y gamma control instead. Bit 0 enables it. */
  2353. i2c_w(sd, 0x64, ctab[val >> 4]);
  2354. break;
  2355. }
  2356. case SEN_OV7640:
  2357. /* Use gain control instead. */
  2358. i2c_w(sd, OV7610_REG_GAIN, val >> 2);
  2359. break;
  2360. case SEN_OV7670:
  2361. /* check that this isn't just the same as ov7610 */
  2362. i2c_w(sd, OV7670_REG_CONTRAS, val >> 1);
  2363. break;
  2364. }
  2365. }
  2366. static void setcolors(struct gspca_dev *gspca_dev)
  2367. {
  2368. struct sd *sd = (struct sd *) gspca_dev;
  2369. int val;
  2370. val = sd->colors;
  2371. switch (sd->sensor) {
  2372. case SEN_OV8610:
  2373. case SEN_OV7610:
  2374. case SEN_OV76BE:
  2375. case SEN_OV6620:
  2376. case SEN_OV6630:
  2377. case SEN_OV66308AF:
  2378. i2c_w(sd, OV7610_REG_SAT, val);
  2379. break;
  2380. case SEN_OV7620:
  2381. /* Use UV gamma control instead. Bits 0 & 7 are reserved. */
  2382. /* rc = ov_i2c_write(sd->dev, 0x62, (val >> 9) & 0x7e);
  2383. if (rc < 0)
  2384. goto out; */
  2385. i2c_w(sd, OV7610_REG_SAT, val);
  2386. break;
  2387. case SEN_OV7640:
  2388. i2c_w(sd, OV7610_REG_SAT, val & 0xf0);
  2389. break;
  2390. case SEN_OV7670:
  2391. /* supported later once I work out how to do it
  2392. * transparently fail now! */
  2393. /* set REG_COM13 values for UV sat auto mode */
  2394. break;
  2395. }
  2396. }
  2397. static void setautobrightness(struct sd *sd)
  2398. {
  2399. if (sd->sensor == SEN_OV7640 || sd->sensor == SEN_OV7670)
  2400. return;
  2401. i2c_w_mask(sd, 0x2d, sd->autobrightness ? 0x10 : 0x00, 0x10);
  2402. }
  2403. static void setfreq(struct sd *sd)
  2404. {
  2405. if (sd->sensor == SEN_OV7670) {
  2406. switch (sd->freq) {
  2407. case 0: /* Banding filter disabled */
  2408. i2c_w_mask(sd, OV7670_REG_COM8, 0, OV7670_COM8_BFILT);
  2409. break;
  2410. case 1: /* 50 hz */
  2411. i2c_w_mask(sd, OV7670_REG_COM8, OV7670_COM8_BFILT,
  2412. OV7670_COM8_BFILT);
  2413. i2c_w_mask(sd, OV7670_REG_COM11, 0x08, 0x18);
  2414. break;
  2415. case 2: /* 60 hz */
  2416. i2c_w_mask(sd, OV7670_REG_COM8, OV7670_COM8_BFILT,
  2417. OV7670_COM8_BFILT);
  2418. i2c_w_mask(sd, OV7670_REG_COM11, 0x00, 0x18);
  2419. break;
  2420. case 3: /* Auto hz */
  2421. i2c_w_mask(sd, OV7670_REG_COM8, OV7670_COM8_BFILT,
  2422. OV7670_COM8_BFILT);
  2423. i2c_w_mask(sd, OV7670_REG_COM11, OV7670_COM11_HZAUTO,
  2424. 0x18);
  2425. break;
  2426. }
  2427. } else {
  2428. switch (sd->freq) {
  2429. case 0: /* Banding filter disabled */
  2430. i2c_w_mask(sd, 0x2d, 0x00, 0x04);
  2431. i2c_w_mask(sd, 0x2a, 0x00, 0x80);
  2432. break;
  2433. case 1: /* 50 hz (filter on and framerate adj) */
  2434. i2c_w_mask(sd, 0x2d, 0x04, 0x04);
  2435. i2c_w_mask(sd, 0x2a, 0x80, 0x80);
  2436. /* 20 fps -> 16.667 fps */
  2437. if (sd->sensor == SEN_OV6620 ||
  2438. sd->sensor == SEN_OV6630 ||
  2439. sd->sensor == SEN_OV66308AF)
  2440. i2c_w(sd, 0x2b, 0x5e);
  2441. else
  2442. i2c_w(sd, 0x2b, 0xac);
  2443. break;
  2444. case 2: /* 60 hz (filter on, ...) */
  2445. i2c_w_mask(sd, 0x2d, 0x04, 0x04);
  2446. if (sd->sensor == SEN_OV6620 ||
  2447. sd->sensor == SEN_OV6630 ||
  2448. sd->sensor == SEN_OV66308AF) {
  2449. /* 20 fps -> 15 fps */
  2450. i2c_w_mask(sd, 0x2a, 0x80, 0x80);
  2451. i2c_w(sd, 0x2b, 0xa8);
  2452. } else {
  2453. /* no framerate adj. */
  2454. i2c_w_mask(sd, 0x2a, 0x00, 0x80);
  2455. }
  2456. break;
  2457. }
  2458. }
  2459. }
  2460. static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
  2461. {
  2462. struct sd *sd = (struct sd *) gspca_dev;
  2463. sd->brightness = val;
  2464. if (gspca_dev->streaming)
  2465. setbrightness(gspca_dev);
  2466. return 0;
  2467. }
  2468. static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
  2469. {
  2470. struct sd *sd = (struct sd *) gspca_dev;
  2471. *val = sd->brightness;
  2472. return 0;
  2473. }
  2474. static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
  2475. {
  2476. struct sd *sd = (struct sd *) gspca_dev;
  2477. sd->contrast = val;
  2478. if (gspca_dev->streaming)
  2479. setcontrast(gspca_dev);
  2480. return 0;
  2481. }
  2482. static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
  2483. {
  2484. struct sd *sd = (struct sd *) gspca_dev;
  2485. *val = sd->contrast;
  2486. return 0;
  2487. }
  2488. static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val)
  2489. {
  2490. struct sd *sd = (struct sd *) gspca_dev;
  2491. sd->colors = val;
  2492. if (gspca_dev->streaming)
  2493. setcolors(gspca_dev);
  2494. return 0;
  2495. }
  2496. static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val)
  2497. {
  2498. struct sd *sd = (struct sd *) gspca_dev;
  2499. *val = sd->colors;
  2500. return 0;
  2501. }
  2502. static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val)
  2503. {
  2504. struct sd *sd = (struct sd *) gspca_dev;
  2505. sd->hflip = val;
  2506. if (gspca_dev->streaming)
  2507. sethvflip(sd);
  2508. return 0;
  2509. }
  2510. static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val)
  2511. {
  2512. struct sd *sd = (struct sd *) gspca_dev;
  2513. *val = sd->hflip;
  2514. return 0;
  2515. }
  2516. static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val)
  2517. {
  2518. struct sd *sd = (struct sd *) gspca_dev;
  2519. sd->vflip = val;
  2520. if (gspca_dev->streaming)
  2521. sethvflip(sd);
  2522. return 0;
  2523. }
  2524. static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val)
  2525. {
  2526. struct sd *sd = (struct sd *) gspca_dev;
  2527. *val = sd->vflip;
  2528. return 0;
  2529. }
  2530. static int sd_setautobrightness(struct gspca_dev *gspca_dev, __s32 val)
  2531. {
  2532. struct sd *sd = (struct sd *) gspca_dev;
  2533. sd->autobrightness = val;
  2534. if (gspca_dev->streaming)
  2535. setautobrightness(sd);
  2536. return 0;
  2537. }
  2538. static int sd_getautobrightness(struct gspca_dev *gspca_dev, __s32 *val)
  2539. {
  2540. struct sd *sd = (struct sd *) gspca_dev;
  2541. *val = sd->autobrightness;
  2542. return 0;
  2543. }
  2544. static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val)
  2545. {
  2546. struct sd *sd = (struct sd *) gspca_dev;
  2547. sd->freq = val;
  2548. if (gspca_dev->streaming)
  2549. setfreq(sd);
  2550. return 0;
  2551. }
  2552. static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val)
  2553. {
  2554. struct sd *sd = (struct sd *) gspca_dev;
  2555. *val = sd->freq;
  2556. return 0;
  2557. }
  2558. static int sd_querymenu(struct gspca_dev *gspca_dev,
  2559. struct v4l2_querymenu *menu)
  2560. {
  2561. struct sd *sd = (struct sd *) gspca_dev;
  2562. switch (menu->id) {
  2563. case V4L2_CID_POWER_LINE_FREQUENCY:
  2564. switch (menu->index) {
  2565. case 0: /* V4L2_CID_POWER_LINE_FREQUENCY_DISABLED */
  2566. strcpy((char *) menu->name, "NoFliker");
  2567. return 0;
  2568. case 1: /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
  2569. strcpy((char *) menu->name, "50 Hz");
  2570. return 0;
  2571. case 2: /* V4L2_CID_POWER_LINE_FREQUENCY_60HZ */
  2572. strcpy((char *) menu->name, "60 Hz");
  2573. return 0;
  2574. case 3:
  2575. if (sd->sensor != SEN_OV7670)
  2576. return -EINVAL;
  2577. strcpy((char *) menu->name, "Automatic");
  2578. return 0;
  2579. }
  2580. break;
  2581. }
  2582. return -EINVAL;
  2583. }
  2584. /* sub-driver description */
  2585. static const struct sd_desc sd_desc = {
  2586. .name = MODULE_NAME,
  2587. .ctrls = sd_ctrls,
  2588. .nctrls = ARRAY_SIZE(sd_ctrls),
  2589. .config = sd_config,
  2590. .init = sd_init,
  2591. .start = sd_start,
  2592. .stopN = sd_stopN,
  2593. .pkt_scan = sd_pkt_scan,
  2594. .querymenu = sd_querymenu,
  2595. };
  2596. /* -- module initialisation -- */
  2597. static const __devinitdata struct usb_device_id device_table[] = {
  2598. {USB_DEVICE(0x041e, 0x4052), .driver_info = BRIDGE_OV519 },
  2599. {USB_DEVICE(0x041e, 0x405f), .driver_info = BRIDGE_OV519 },
  2600. {USB_DEVICE(0x041e, 0x4060), .driver_info = BRIDGE_OV519 },
  2601. {USB_DEVICE(0x041e, 0x4061), .driver_info = BRIDGE_OV519 },
  2602. {USB_DEVICE(0x041e, 0x4064),
  2603. .driver_info = BRIDGE_OV519 | BRIDGE_INVERT_LED },
  2604. {USB_DEVICE(0x041e, 0x4068),
  2605. .driver_info = BRIDGE_OV519 | BRIDGE_INVERT_LED },
  2606. {USB_DEVICE(0x045e, 0x028c), .driver_info = BRIDGE_OV519 },
  2607. {USB_DEVICE(0x054c, 0x0154), .driver_info = BRIDGE_OV519 },
  2608. {USB_DEVICE(0x054c, 0x0155), .driver_info = BRIDGE_OV519 },
  2609. {USB_DEVICE(0x05a9, 0x0518), .driver_info = BRIDGE_OV518 },
  2610. {USB_DEVICE(0x05a9, 0x0519), .driver_info = BRIDGE_OV519 },
  2611. {USB_DEVICE(0x05a9, 0x0530), .driver_info = BRIDGE_OV519 },
  2612. {USB_DEVICE(0x05a9, 0x4519), .driver_info = BRIDGE_OV519 },
  2613. {USB_DEVICE(0x05a9, 0x8519), .driver_info = BRIDGE_OV519 },
  2614. {USB_DEVICE(0x05a9, 0xa518), .driver_info = BRIDGE_OV518PLUS },
  2615. {}
  2616. };
  2617. MODULE_DEVICE_TABLE(usb, device_table);
  2618. /* -- device connect -- */
  2619. static int sd_probe(struct usb_interface *intf,
  2620. const struct usb_device_id *id)
  2621. {
  2622. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  2623. THIS_MODULE);
  2624. }
  2625. static struct usb_driver sd_driver = {
  2626. .name = MODULE_NAME,
  2627. .id_table = device_table,
  2628. .probe = sd_probe,
  2629. .disconnect = gspca_disconnect,
  2630. #ifdef CONFIG_PM
  2631. .suspend = gspca_suspend,
  2632. .resume = gspca_resume,
  2633. #endif
  2634. };
  2635. /* -- module insert / remove -- */
  2636. static int __init sd_mod_init(void)
  2637. {
  2638. int ret;
  2639. ret = usb_register(&sd_driver);
  2640. if (ret < 0)
  2641. return ret;
  2642. PDEBUG(D_PROBE, "registered");
  2643. return 0;
  2644. }
  2645. static void __exit sd_mod_exit(void)
  2646. {
  2647. usb_deregister(&sd_driver);
  2648. PDEBUG(D_PROBE, "deregistered");
  2649. }
  2650. module_init(sd_mod_init);
  2651. module_exit(sd_mod_exit);
  2652. module_param(frame_rate, int, 0644);
  2653. MODULE_PARM_DESC(frame_rate, "Frame rate (5, 10, 15, 20 or 30 fps)");