ov519.c 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425
  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, 0x4068),
  3049. .driver_info = BRIDGE_OV519 | BRIDGE_INVERT_LED },
  3050. {USB_DEVICE(0x045e, 0x028c), .driver_info = BRIDGE_OV519 },
  3051. {USB_DEVICE(0x054c, 0x0154), .driver_info = BRIDGE_OV519 },
  3052. {USB_DEVICE(0x054c, 0x0155), .driver_info = BRIDGE_OV519 },
  3053. {USB_DEVICE(0x05a9, 0x0511), .driver_info = BRIDGE_OV511 },
  3054. {USB_DEVICE(0x05a9, 0x0518), .driver_info = BRIDGE_OV518 },
  3055. {USB_DEVICE(0x05a9, 0x0519), .driver_info = BRIDGE_OV519 },
  3056. {USB_DEVICE(0x05a9, 0x0530), .driver_info = BRIDGE_OV519 },
  3057. {USB_DEVICE(0x05a9, 0x4519), .driver_info = BRIDGE_OV519 },
  3058. {USB_DEVICE(0x05a9, 0x8519), .driver_info = BRIDGE_OV519 },
  3059. {USB_DEVICE(0x05a9, 0xa511), .driver_info = BRIDGE_OV511PLUS },
  3060. {USB_DEVICE(0x05a9, 0xa518), .driver_info = BRIDGE_OV518PLUS },
  3061. {USB_DEVICE(0x0813, 0x0002), .driver_info = BRIDGE_OV511PLUS },
  3062. {}
  3063. };
  3064. MODULE_DEVICE_TABLE(usb, device_table);
  3065. /* -- device connect -- */
  3066. static int sd_probe(struct usb_interface *intf,
  3067. const struct usb_device_id *id)
  3068. {
  3069. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  3070. THIS_MODULE);
  3071. }
  3072. static struct usb_driver sd_driver = {
  3073. .name = MODULE_NAME,
  3074. .id_table = device_table,
  3075. .probe = sd_probe,
  3076. .disconnect = gspca_disconnect,
  3077. #ifdef CONFIG_PM
  3078. .suspend = gspca_suspend,
  3079. .resume = gspca_resume,
  3080. #endif
  3081. };
  3082. /* -- module insert / remove -- */
  3083. static int __init sd_mod_init(void)
  3084. {
  3085. int ret;
  3086. ret = usb_register(&sd_driver);
  3087. if (ret < 0)
  3088. return ret;
  3089. PDEBUG(D_PROBE, "registered");
  3090. return 0;
  3091. }
  3092. static void __exit sd_mod_exit(void)
  3093. {
  3094. usb_deregister(&sd_driver);
  3095. PDEBUG(D_PROBE, "deregistered");
  3096. }
  3097. module_init(sd_mod_init);
  3098. module_exit(sd_mod_exit);
  3099. module_param(frame_rate, int, 0644);
  3100. MODULE_PARM_DESC(frame_rate, "Frame rate (5, 10, 15, 20 or 30 fps)");