ov519.c 73 KB

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