ov519.c 72 KB

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