ov519.c 86 KB

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