ov519.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263
  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)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #define MODULE_NAME "ov519"
  24. #include "gspca.h"
  25. MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
  26. MODULE_DESCRIPTION("OV519 USB Camera Driver");
  27. MODULE_LICENSE("GPL");
  28. /* global parameters */
  29. static int frame_rate;
  30. /* Number of times to retry a failed I2C transaction. Increase this if you
  31. * are getting "Failed to read sensor ID..." */
  32. static int i2c_detect_tries = 10;
  33. /* ov519 device descriptor */
  34. struct sd {
  35. struct gspca_dev gspca_dev; /* !! must be the first item */
  36. /* Determined by sensor type */
  37. short maxwidth;
  38. short maxheight;
  39. unsigned char primary_i2c_slave; /* I2C write id of sensor */
  40. unsigned char brightness;
  41. unsigned char contrast;
  42. unsigned char colors;
  43. __u8 hflip;
  44. __u8 vflip;
  45. char compress; /* Should the next frame be compressed? */
  46. char compress_inited; /* Are compression params uploaded? */
  47. char stopped; /* Streaming is temporarily paused */
  48. char frame_rate; /* current Framerate (OV519 only) */
  49. char clockdiv; /* clockdiv override for OV519 only */
  50. char sensor; /* Type of image sensor chip (SEN_*) */
  51. #define SEN_UNKNOWN 0
  52. #define SEN_OV6620 1
  53. #define SEN_OV6630 2
  54. #define SEN_OV7610 3
  55. #define SEN_OV7620 4
  56. #define SEN_OV7630 5
  57. #define SEN_OV7640 6
  58. #define SEN_OV7670 7
  59. #define SEN_OV76BE 8
  60. #define SEN_OV8610 9
  61. };
  62. /* V4L2 controls supported by the driver */
  63. static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
  64. static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
  65. static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
  66. static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
  67. static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val);
  68. static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val);
  69. static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val);
  70. static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val);
  71. static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val);
  72. static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val);
  73. static struct ctrl sd_ctrls[] = {
  74. #define SD_BRIGHTNESS 0
  75. {
  76. {
  77. .id = V4L2_CID_BRIGHTNESS,
  78. .type = V4L2_CTRL_TYPE_INTEGER,
  79. .name = "Brightness",
  80. .minimum = 0,
  81. .maximum = 255,
  82. .step = 1,
  83. .default_value = 127,
  84. },
  85. .set = sd_setbrightness,
  86. .get = sd_getbrightness,
  87. },
  88. #define SD_CONTRAST 1
  89. {
  90. {
  91. .id = V4L2_CID_CONTRAST,
  92. .type = V4L2_CTRL_TYPE_INTEGER,
  93. .name = "Contrast",
  94. .minimum = 0,
  95. .maximum = 255,
  96. .step = 1,
  97. .default_value = 127,
  98. },
  99. .set = sd_setcontrast,
  100. .get = sd_getcontrast,
  101. },
  102. #define SD_COLOR 2
  103. {
  104. {
  105. .id = V4L2_CID_SATURATION,
  106. .type = V4L2_CTRL_TYPE_INTEGER,
  107. .name = "Saturation",
  108. .minimum = 0,
  109. .maximum = 255,
  110. .step = 1,
  111. .default_value = 127,
  112. },
  113. .set = sd_setcolors,
  114. .get = sd_getcolors,
  115. },
  116. /* next controls work with ov7670 only */
  117. {
  118. {
  119. .id = V4L2_CID_HFLIP,
  120. .type = V4L2_CTRL_TYPE_BOOLEAN,
  121. .name = "Mirror",
  122. .minimum = 0,
  123. .maximum = 1,
  124. .step = 1,
  125. #define HFLIP_DEF 0
  126. .default_value = HFLIP_DEF,
  127. },
  128. .set = sd_sethflip,
  129. .get = sd_gethflip,
  130. },
  131. {
  132. {
  133. .id = V4L2_CID_VFLIP,
  134. .type = V4L2_CTRL_TYPE_BOOLEAN,
  135. .name = "Vflip",
  136. .minimum = 0,
  137. .maximum = 1,
  138. .step = 1,
  139. #define VFLIP_DEF 0
  140. .default_value = VFLIP_DEF,
  141. },
  142. .set = sd_setvflip,
  143. .get = sd_getvflip,
  144. },
  145. };
  146. static struct v4l2_pix_format vga_mode[] = {
  147. {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  148. .bytesperline = 320,
  149. .sizeimage = 320 * 240 * 3 / 8 + 589,
  150. .colorspace = V4L2_COLORSPACE_JPEG,
  151. .priv = 1},
  152. {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  153. .bytesperline = 640,
  154. .sizeimage = 640 * 480 * 3 / 8 + 590,
  155. .colorspace = V4L2_COLORSPACE_JPEG,
  156. .priv = 0},
  157. };
  158. static struct v4l2_pix_format sif_mode[] = {
  159. {176, 144, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  160. .bytesperline = 176,
  161. .sizeimage = 176 * 144 * 3 / 8 + 589,
  162. .colorspace = V4L2_COLORSPACE_JPEG,
  163. .priv = 1},
  164. {352, 288, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  165. .bytesperline = 352,
  166. .sizeimage = 352 * 288 * 3 / 8 + 589,
  167. .colorspace = V4L2_COLORSPACE_JPEG,
  168. .priv = 0},
  169. };
  170. /* OV519 Camera interface register numbers */
  171. #define OV519_CAM_H_SIZE 0x10
  172. #define OV519_CAM_V_SIZE 0x11
  173. #define OV519_CAM_X_OFFSETL 0x12
  174. #define OV519_CAM_X_OFFSETH 0x13
  175. #define OV519_CAM_Y_OFFSETL 0x14
  176. #define OV519_CAM_Y_OFFSETH 0x15
  177. #define OV519_CAM_DIVIDER 0x16
  178. #define OV519_CAM_DFR 0x20
  179. #define OV519_CAM_FORMAT 0x25
  180. /* OV519 System Controller register numbers */
  181. #define OV519_SYS_RESET1 0x51
  182. #define OV519_SYS_EN_CLK1 0x54
  183. #define OV519_GPIO_DATA_OUT0 0x71
  184. #define OV519_GPIO_IO_CTRL0 0x72
  185. #define OV511_ENDPOINT_ADDRESS 1 /* Isoc endpoint number */
  186. /* I2C registers */
  187. #define R51x_I2C_W_SID 0x41
  188. #define R51x_I2C_SADDR_3 0x42
  189. #define R51x_I2C_SADDR_2 0x43
  190. #define R51x_I2C_R_SID 0x44
  191. #define R51x_I2C_DATA 0x45
  192. #define R518_I2C_CTL 0x47 /* OV518(+) only */
  193. /* I2C ADDRESSES */
  194. #define OV7xx0_SID 0x42
  195. #define OV8xx0_SID 0xa0
  196. #define OV6xx0_SID 0xc0
  197. /* OV7610 registers */
  198. #define OV7610_REG_GAIN 0x00 /* gain setting (5:0) */
  199. #define OV7610_REG_SAT 0x03 /* saturation */
  200. #define OV8610_REG_HUE 0x04 /* 04 reserved */
  201. #define OV7610_REG_CNT 0x05 /* Y contrast */
  202. #define OV7610_REG_BRT 0x06 /* Y brightness */
  203. #define OV7610_REG_COM_C 0x14 /* misc common regs */
  204. #define OV7610_REG_ID_HIGH 0x1c /* manufacturer ID MSB */
  205. #define OV7610_REG_ID_LOW 0x1d /* manufacturer ID LSB */
  206. #define OV7610_REG_COM_I 0x29 /* misc settings */
  207. /* OV7670 registers */
  208. #define OV7670_REG_GAIN 0x00 /* Gain lower 8 bits (rest in vref) */
  209. #define OV7670_REG_BLUE 0x01 /* blue gain */
  210. #define OV7670_REG_RED 0x02 /* red gain */
  211. #define OV7670_REG_VREF 0x03 /* Pieces of GAIN, VSTART, VSTOP */
  212. #define OV7670_REG_COM1 0x04 /* Control 1 */
  213. #define OV7670_REG_AECHH 0x07 /* AEC MS 5 bits */
  214. #define OV7670_REG_COM3 0x0c /* Control 3 */
  215. #define OV7670_REG_COM4 0x0d /* Control 4 */
  216. #define OV7670_REG_COM5 0x0e /* All "reserved" */
  217. #define OV7670_REG_COM6 0x0f /* Control 6 */
  218. #define OV7670_REG_AECH 0x10 /* More bits of AEC value */
  219. #define OV7670_REG_CLKRC 0x11 /* Clock control */
  220. #define OV7670_REG_COM7 0x12 /* Control 7 */
  221. #define OV7670_COM7_FMT_VGA 0x00
  222. #define OV7670_COM7_YUV 0x00 /* YUV */
  223. #define OV7670_COM7_FMT_QVGA 0x10 /* QVGA format */
  224. #define OV7670_COM7_FMT_MASK 0x38
  225. #define OV7670_COM7_RESET 0x80 /* Register reset */
  226. #define OV7670_REG_COM8 0x13 /* Control 8 */
  227. #define OV7670_COM8_AEC 0x01 /* Auto exposure enable */
  228. #define OV7670_COM8_AWB 0x02 /* White balance enable */
  229. #define OV7670_COM8_AGC 0x04 /* Auto gain enable */
  230. #define OV7670_COM8_BFILT 0x20 /* Band filter enable */
  231. #define OV7670_COM8_AECSTEP 0x40 /* Unlimited AEC step size */
  232. #define OV7670_COM8_FASTAEC 0x80 /* Enable fast AGC/AEC */
  233. #define OV7670_REG_COM9 0x14 /* Control 9 - gain ceiling */
  234. #define OV7670_REG_COM10 0x15 /* Control 10 */
  235. #define OV7670_REG_HSTART 0x17 /* Horiz start high bits */
  236. #define OV7670_REG_HSTOP 0x18 /* Horiz stop high bits */
  237. #define OV7670_REG_VSTART 0x19 /* Vert start high bits */
  238. #define OV7670_REG_VSTOP 0x1a /* Vert stop high bits */
  239. #define OV7670_REG_MVFP 0x1e /* Mirror / vflip */
  240. #define OV7670_MVFP_VFLIP 0x10 /* vertical flip */
  241. #define OV7670_MVFP_MIRROR 0x20 /* Mirror image */
  242. #define OV7670_REG_AEW 0x24 /* AGC upper limit */
  243. #define OV7670_REG_AEB 0x25 /* AGC lower limit */
  244. #define OV7670_REG_VPT 0x26 /* AGC/AEC fast mode op region */
  245. #define OV7670_REG_HREF 0x32 /* HREF pieces */
  246. #define OV7670_REG_TSLB 0x3a /* lots of stuff */
  247. #define OV7670_REG_COM11 0x3b /* Control 11 */
  248. #define OV7670_COM11_EXP 0x02
  249. #define OV7670_COM11_HZAUTO 0x10 /* Auto detect 50/60 Hz */
  250. #define OV7670_REG_COM12 0x3c /* Control 12 */
  251. #define OV7670_REG_COM13 0x3d /* Control 13 */
  252. #define OV7670_COM13_GAMMA 0x80 /* Gamma enable */
  253. #define OV7670_COM13_UVSAT 0x40 /* UV saturation auto adjustment */
  254. #define OV7670_REG_COM14 0x3e /* Control 14 */
  255. #define OV7670_REG_EDGE 0x3f /* Edge enhancement factor */
  256. #define OV7670_REG_COM15 0x40 /* Control 15 */
  257. #define OV7670_COM15_R00FF 0xc0 /* 00 to FF */
  258. #define OV7670_REG_COM16 0x41 /* Control 16 */
  259. #define OV7670_COM16_AWBGAIN 0x08 /* AWB gain enable */
  260. #define OV7670_REG_BRIGHT 0x55 /* Brightness */
  261. #define OV7670_REG_CONTRAS 0x56 /* Contrast control */
  262. #define OV7670_REG_GFIX 0x69 /* Fix gain control */
  263. #define OV7670_REG_RGB444 0x8c /* RGB 444 control */
  264. #define OV7670_REG_HAECC1 0x9f /* Hist AEC/AGC control 1 */
  265. #define OV7670_REG_HAECC2 0xa0 /* Hist AEC/AGC control 2 */
  266. #define OV7670_REG_BD50MAX 0xa5 /* 50hz banding step limit */
  267. #define OV7670_REG_HAECC3 0xa6 /* Hist AEC/AGC control 3 */
  268. #define OV7670_REG_HAECC4 0xa7 /* Hist AEC/AGC control 4 */
  269. #define OV7670_REG_HAECC5 0xa8 /* Hist AEC/AGC control 5 */
  270. #define OV7670_REG_HAECC6 0xa9 /* Hist AEC/AGC control 6 */
  271. #define OV7670_REG_HAECC7 0xaa /* Hist AEC/AGC control 7 */
  272. #define OV7670_REG_BD60MAX 0xab /* 60hz banding step limit */
  273. struct ovsensor_window {
  274. short x;
  275. short y;
  276. short width;
  277. short height;
  278. /* int format; */
  279. short quarter; /* Scale width and height down 2x */
  280. short clockdiv; /* Clock divisor setting */
  281. };
  282. static unsigned char ov7670_abs_to_sm(unsigned char v)
  283. {
  284. if (v > 127)
  285. return v & 0x7f;
  286. return (128 - v) | 0x80;
  287. }
  288. /* Write a OV519 register */
  289. static int reg_w(struct sd *sd, __u16 index, __u8 value)
  290. {
  291. int ret;
  292. sd->gspca_dev.usb_buf[0] = value;
  293. ret = usb_control_msg(sd->gspca_dev.dev,
  294. usb_sndctrlpipe(sd->gspca_dev.dev, 0),
  295. 1, /* REQ_IO (ov518/519) */
  296. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  297. 0, index,
  298. sd->gspca_dev.usb_buf, 1, 500);
  299. if (ret < 0)
  300. PDEBUG(D_ERR, "Write reg [%02x] %02x failed", index, value);
  301. return ret;
  302. }
  303. /* Read from a OV519 register */
  304. /* returns: negative is error, pos or zero is data */
  305. static int reg_r(struct sd *sd, __u16 index)
  306. {
  307. int ret;
  308. ret = usb_control_msg(sd->gspca_dev.dev,
  309. usb_rcvctrlpipe(sd->gspca_dev.dev, 0),
  310. 1, /* REQ_IO */
  311. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  312. 0, index, sd->gspca_dev.usb_buf, 1, 500);
  313. if (ret >= 0)
  314. ret = sd->gspca_dev.usb_buf[0];
  315. else
  316. PDEBUG(D_ERR, "Read reg [0x%02x] failed", index);
  317. return ret;
  318. }
  319. /* Read 8 values from a OV519 register */
  320. static int reg_r8(struct sd *sd,
  321. __u16 index)
  322. {
  323. int ret;
  324. ret = usb_control_msg(sd->gspca_dev.dev,
  325. usb_rcvctrlpipe(sd->gspca_dev.dev, 0),
  326. 1, /* REQ_IO */
  327. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  328. 0, index, sd->gspca_dev.usb_buf, 8, 500);
  329. if (ret >= 0)
  330. ret = sd->gspca_dev.usb_buf[0];
  331. else
  332. PDEBUG(D_ERR, "Read reg 8 [0x%02x] failed", index);
  333. return ret;
  334. }
  335. /*
  336. * Writes bits at positions specified by mask to an OV51x reg. Bits that are in
  337. * the same position as 1's in "mask" are cleared and set to "value". Bits
  338. * that are in the same position as 0's in "mask" are preserved, regardless
  339. * of their respective state in "value".
  340. */
  341. static int reg_w_mask(struct sd *sd,
  342. __u16 index,
  343. __u8 value,
  344. __u8 mask)
  345. {
  346. int ret;
  347. __u8 oldval;
  348. if (mask != 0xff) {
  349. value &= mask; /* Enforce mask on value */
  350. ret = reg_r(sd, index);
  351. if (ret < 0)
  352. return ret;
  353. oldval = ret & ~mask; /* Clear the masked bits */
  354. value |= oldval; /* Set the desired bits */
  355. }
  356. return reg_w(sd, index, value);
  357. }
  358. /*
  359. * The OV518 I2C I/O procedure is different, hence, this function.
  360. * This is normally only called from i2c_w(). Note that this function
  361. * always succeeds regardless of whether the sensor is present and working.
  362. */
  363. static int i2c_w(struct sd *sd,
  364. __u8 reg,
  365. __u8 value)
  366. {
  367. int rc;
  368. PDEBUG(D_USBO, "i2c 0x%02x -> [0x%02x]", value, reg);
  369. /* Select camera register */
  370. rc = reg_w(sd, R51x_I2C_SADDR_3, reg);
  371. if (rc < 0)
  372. return rc;
  373. /* Write "value" to I2C data port of OV511 */
  374. rc = reg_w(sd, R51x_I2C_DATA, value);
  375. if (rc < 0)
  376. return rc;
  377. /* Initiate 3-byte write cycle */
  378. rc = reg_w(sd, R518_I2C_CTL, 0x01);
  379. /* wait for write complete */
  380. msleep(4);
  381. if (rc < 0)
  382. return rc;
  383. return reg_r8(sd, R518_I2C_CTL);
  384. }
  385. /*
  386. * returns: negative is error, pos or zero is data
  387. *
  388. * The OV518 I2C I/O procedure is different, hence, this function.
  389. * This is normally only called from i2c_r(). Note that this function
  390. * always succeeds regardless of whether the sensor is present and working.
  391. */
  392. static int i2c_r(struct sd *sd, __u8 reg)
  393. {
  394. int rc, value;
  395. /* Select camera register */
  396. rc = reg_w(sd, R51x_I2C_SADDR_2, reg);
  397. if (rc < 0)
  398. return rc;
  399. /* Initiate 2-byte write cycle */
  400. rc = reg_w(sd, R518_I2C_CTL, 0x03);
  401. if (rc < 0)
  402. return rc;
  403. /* Initiate 2-byte read cycle */
  404. rc = reg_w(sd, R518_I2C_CTL, 0x05);
  405. if (rc < 0)
  406. return rc;
  407. value = reg_r(sd, R51x_I2C_DATA);
  408. PDEBUG(D_USBI, "i2c [0x%02X] -> 0x%02X", reg, value);
  409. return value;
  410. }
  411. /* Writes bits at positions specified by mask to an I2C reg. Bits that are in
  412. * the same position as 1's in "mask" are cleared and set to "value". Bits
  413. * that are in the same position as 0's in "mask" are preserved, regardless
  414. * of their respective state in "value".
  415. */
  416. static int i2c_w_mask(struct sd *sd,
  417. __u8 reg,
  418. __u8 value,
  419. __u8 mask)
  420. {
  421. int rc;
  422. __u8 oldval;
  423. value &= mask; /* Enforce mask on value */
  424. rc = i2c_r(sd, reg);
  425. if (rc < 0)
  426. return rc;
  427. oldval = rc & ~mask; /* Clear the masked bits */
  428. value |= oldval; /* Set the desired bits */
  429. return i2c_w(sd, reg, value);
  430. }
  431. /* Temporarily stops OV511 from functioning. Must do this before changing
  432. * registers while the camera is streaming */
  433. static inline int ov51x_stop(struct sd *sd)
  434. {
  435. PDEBUG(D_STREAM, "stopping");
  436. sd->stopped = 1;
  437. return reg_w(sd, OV519_SYS_RESET1, 0x0f);
  438. }
  439. /* Restarts OV511 after ov511_stop() is called. Has no effect if it is not
  440. * actually stopped (for performance). */
  441. static inline int ov51x_restart(struct sd *sd)
  442. {
  443. PDEBUG(D_STREAM, "restarting");
  444. if (!sd->stopped)
  445. return 0;
  446. sd->stopped = 0;
  447. /* Reinitialize the stream */
  448. return reg_w(sd, OV519_SYS_RESET1, 0x00);
  449. }
  450. /* This does an initial reset of an OmniVision sensor and ensures that I2C
  451. * is synchronized. Returns <0 on failure.
  452. */
  453. static int init_ov_sensor(struct sd *sd)
  454. {
  455. int i, success;
  456. /* Reset the sensor */
  457. if (i2c_w(sd, 0x12, 0x80) < 0)
  458. return -EIO;
  459. /* Wait for it to initialize */
  460. msleep(150);
  461. for (i = 0, success = 0; i < i2c_detect_tries && !success; i++) {
  462. if (i2c_r(sd, OV7610_REG_ID_HIGH) == 0x7f &&
  463. i2c_r(sd, OV7610_REG_ID_LOW) == 0xa2) {
  464. success = 1;
  465. continue;
  466. }
  467. /* Reset the sensor */
  468. if (i2c_w(sd, 0x12, 0x80) < 0)
  469. return -EIO;
  470. /* Wait for it to initialize */
  471. msleep(150);
  472. /* Dummy read to sync I2C */
  473. if (i2c_r(sd, 0x00) < 0)
  474. return -EIO;
  475. }
  476. if (!success)
  477. return -EIO;
  478. PDEBUG(D_PROBE, "I2C synced in %d attempt(s)", i);
  479. return 0;
  480. }
  481. /* Switch on standard JPEG compression. Returns 0 for success. */
  482. static int ov519_init_compression(struct sd *sd)
  483. {
  484. if (!sd->compress_inited) {
  485. if (reg_w_mask(sd, OV519_SYS_EN_CLK1, 1 << 2, 1 << 2) < 0) {
  486. PDEBUG(D_ERR, "Error switching to compressed mode");
  487. return -EIO;
  488. }
  489. sd->compress_inited = 1;
  490. }
  491. return 0;
  492. }
  493. /* Set the read and write slave IDs. The "slave" argument is the write slave,
  494. * and the read slave will be set to (slave + 1).
  495. * This should not be called from outside the i2c I/O functions.
  496. * Sets I2C read and write slave IDs. Returns <0 for error
  497. */
  498. static int ov51x_set_slave_ids(struct sd *sd,
  499. __u8 slave)
  500. {
  501. int rc;
  502. rc = reg_w(sd, R51x_I2C_W_SID, slave);
  503. if (rc < 0)
  504. return rc;
  505. return reg_w(sd, R51x_I2C_R_SID, slave + 1);
  506. }
  507. struct ov_regvals {
  508. __u8 reg;
  509. __u8 val;
  510. };
  511. struct ov_i2c_regvals {
  512. __u8 reg;
  513. __u8 val;
  514. };
  515. static int write_regvals(struct sd *sd,
  516. const struct ov_regvals *regvals,
  517. int n)
  518. {
  519. int rc;
  520. while (--n >= 0) {
  521. rc = reg_w(sd, regvals->reg, regvals->val);
  522. if (rc < 0)
  523. return rc;
  524. regvals++;
  525. }
  526. return 0;
  527. }
  528. static int write_i2c_regvals(struct sd *sd,
  529. const struct ov_i2c_regvals *regvals,
  530. int n)
  531. {
  532. int rc;
  533. while (--n >= 0) {
  534. rc = i2c_w(sd, regvals->reg, regvals->val);
  535. if (rc < 0)
  536. return rc;
  537. regvals++;
  538. }
  539. return 0;
  540. }
  541. /****************************************************************************
  542. *
  543. * OV511 and sensor configuration
  544. *
  545. ***************************************************************************/
  546. /* This initializes the OV8110, OV8610 sensor. The OV8110 uses
  547. * the same register settings as the OV8610, since they are very similar.
  548. */
  549. static int ov8xx0_configure(struct sd *sd)
  550. {
  551. int rc;
  552. static const struct ov_i2c_regvals norm_8610[] = {
  553. { 0x12, 0x80 },
  554. { 0x00, 0x00 },
  555. { 0x01, 0x80 },
  556. { 0x02, 0x80 },
  557. { 0x03, 0xc0 },
  558. { 0x04, 0x30 },
  559. { 0x05, 0x30 }, /* was 0x10, new from windrv 090403 */
  560. { 0x06, 0x70 }, /* was 0x80, new from windrv 090403 */
  561. { 0x0a, 0x86 },
  562. { 0x0b, 0xb0 },
  563. { 0x0c, 0x20 },
  564. { 0x0d, 0x20 },
  565. { 0x11, 0x01 },
  566. { 0x12, 0x25 },
  567. { 0x13, 0x01 },
  568. { 0x14, 0x04 },
  569. { 0x15, 0x01 }, /* Lin and Win think different about UV order */
  570. { 0x16, 0x03 },
  571. { 0x17, 0x38 }, /* was 0x2f, new from windrv 090403 */
  572. { 0x18, 0xea }, /* was 0xcf, new from windrv 090403 */
  573. { 0x19, 0x02 }, /* was 0x06, new from windrv 090403 */
  574. { 0x1a, 0xf5 },
  575. { 0x1b, 0x00 },
  576. { 0x20, 0xd0 }, /* was 0x90, new from windrv 090403 */
  577. { 0x23, 0xc0 }, /* was 0x00, new from windrv 090403 */
  578. { 0x24, 0x30 }, /* was 0x1d, new from windrv 090403 */
  579. { 0x25, 0x50 }, /* was 0x57, new from windrv 090403 */
  580. { 0x26, 0xa2 },
  581. { 0x27, 0xea },
  582. { 0x28, 0x00 },
  583. { 0x29, 0x00 },
  584. { 0x2a, 0x80 },
  585. { 0x2b, 0xc8 }, /* was 0xcc, new from windrv 090403 */
  586. { 0x2c, 0xac },
  587. { 0x2d, 0x45 }, /* was 0xd5, new from windrv 090403 */
  588. { 0x2e, 0x80 },
  589. { 0x2f, 0x14 }, /* was 0x01, new from windrv 090403 */
  590. { 0x4c, 0x00 },
  591. { 0x4d, 0x30 }, /* was 0x10, new from windrv 090403 */
  592. { 0x60, 0x02 }, /* was 0x01, new from windrv 090403 */
  593. { 0x61, 0x00 }, /* was 0x09, new from windrv 090403 */
  594. { 0x62, 0x5f }, /* was 0xd7, new from windrv 090403 */
  595. { 0x63, 0xff },
  596. { 0x64, 0x53 }, /* new windrv 090403 says 0x57,
  597. * maybe thats wrong */
  598. { 0x65, 0x00 },
  599. { 0x66, 0x55 },
  600. { 0x67, 0xb0 },
  601. { 0x68, 0xc0 }, /* was 0xaf, new from windrv 090403 */
  602. { 0x69, 0x02 },
  603. { 0x6a, 0x22 },
  604. { 0x6b, 0x00 },
  605. { 0x6c, 0x99 }, /* was 0x80, old windrv says 0x00, but
  606. deleting bit7 colors the first images red */
  607. { 0x6d, 0x11 }, /* was 0x00, new from windrv 090403 */
  608. { 0x6e, 0x11 }, /* was 0x00, new from windrv 090403 */
  609. { 0x6f, 0x01 },
  610. { 0x70, 0x8b },
  611. { 0x71, 0x00 },
  612. { 0x72, 0x14 },
  613. { 0x73, 0x54 },
  614. { 0x74, 0x00 },/* 0x60? - was 0x00, new from windrv 090403 */
  615. { 0x75, 0x0e },
  616. { 0x76, 0x02 }, /* was 0x02, new from windrv 090403 */
  617. { 0x77, 0xff },
  618. { 0x78, 0x80 },
  619. { 0x79, 0x80 },
  620. { 0x7a, 0x80 },
  621. { 0x7b, 0x10 }, /* was 0x13, new from windrv 090403 */
  622. { 0x7c, 0x00 },
  623. { 0x7d, 0x08 }, /* was 0x09, new from windrv 090403 */
  624. { 0x7e, 0x08 }, /* was 0xc0, new from windrv 090403 */
  625. { 0x7f, 0xfb },
  626. { 0x80, 0x28 },
  627. { 0x81, 0x00 },
  628. { 0x82, 0x23 },
  629. { 0x83, 0x0b },
  630. { 0x84, 0x00 },
  631. { 0x85, 0x62 }, /* was 0x61, new from windrv 090403 */
  632. { 0x86, 0xc9 },
  633. { 0x87, 0x00 },
  634. { 0x88, 0x00 },
  635. { 0x89, 0x01 },
  636. { 0x12, 0x20 },
  637. { 0x12, 0x25 }, /* was 0x24, new from windrv 090403 */
  638. };
  639. PDEBUG(D_PROBE, "starting ov8xx0 configuration");
  640. if (init_ov_sensor(sd) < 0)
  641. PDEBUG(D_ERR|D_PROBE, "Failed to read sensor ID");
  642. else
  643. PDEBUG(D_PROBE, "OV86x0 initialized");
  644. /* Detect sensor (sub)type */
  645. rc = i2c_r(sd, OV7610_REG_COM_I);
  646. if (rc < 0) {
  647. PDEBUG(D_ERR, "Error detecting sensor type");
  648. return -1;
  649. }
  650. if ((rc & 3) == 1) {
  651. PDEBUG(D_PROBE, "Sensor is an OV8610");
  652. sd->sensor = SEN_OV8610;
  653. } else {
  654. PDEBUG(D_ERR, "Unknown image sensor version: %d", rc & 3);
  655. return -1;
  656. }
  657. PDEBUG(D_PROBE, "Writing 8610 registers");
  658. if (write_i2c_regvals(sd,
  659. norm_8610,
  660. sizeof norm_8610 / sizeof norm_8610[0]))
  661. return -1;
  662. /* Set sensor-specific vars */
  663. sd->maxwidth = 640;
  664. sd->maxheight = 480;
  665. return 0;
  666. }
  667. /* This initializes the OV7610, OV7620, or OV76BE sensor. The OV76BE uses
  668. * the same register settings as the OV7610, since they are very similar.
  669. */
  670. static int ov7xx0_configure(struct sd *sd)
  671. {
  672. int rc, high, low;
  673. /* Lawrence Glaister <lg@jfm.bc.ca> reports:
  674. *
  675. * Register 0x0f in the 7610 has the following effects:
  676. *
  677. * 0x85 (AEC method 1): Best overall, good contrast range
  678. * 0x45 (AEC method 2): Very overexposed
  679. * 0xa5 (spec sheet default): Ok, but the black level is
  680. * shifted resulting in loss of contrast
  681. * 0x05 (old driver setting): very overexposed, too much
  682. * contrast
  683. */
  684. static const struct ov_i2c_regvals norm_7610[] = {
  685. { 0x10, 0xff },
  686. { 0x16, 0x06 },
  687. { 0x28, 0x24 },
  688. { 0x2b, 0xac },
  689. { 0x12, 0x00 },
  690. { 0x38, 0x81 },
  691. { 0x28, 0x24 }, /* 0c */
  692. { 0x0f, 0x85 }, /* lg's setting */
  693. { 0x15, 0x01 },
  694. { 0x20, 0x1c },
  695. { 0x23, 0x2a },
  696. { 0x24, 0x10 },
  697. { 0x25, 0x8a },
  698. { 0x26, 0xa2 },
  699. { 0x27, 0xc2 },
  700. { 0x2a, 0x04 },
  701. { 0x2c, 0xfe },
  702. { 0x2d, 0x93 },
  703. { 0x30, 0x71 },
  704. { 0x31, 0x60 },
  705. { 0x32, 0x26 },
  706. { 0x33, 0x20 },
  707. { 0x34, 0x48 },
  708. { 0x12, 0x24 },
  709. { 0x11, 0x01 },
  710. { 0x0c, 0x24 },
  711. { 0x0d, 0x24 },
  712. };
  713. static const struct ov_i2c_regvals norm_7620[] = {
  714. { 0x00, 0x00 }, /* gain */
  715. { 0x01, 0x80 }, /* blue gain */
  716. { 0x02, 0x80 }, /* red gain */
  717. { 0x03, 0xc0 }, /* OV7670_REG_VREF */
  718. { 0x06, 0x60 },
  719. { 0x07, 0x00 },
  720. { 0x0c, 0x24 },
  721. { 0x0c, 0x24 },
  722. { 0x0d, 0x24 },
  723. { 0x11, 0x01 },
  724. { 0x12, 0x24 },
  725. { 0x13, 0x01 },
  726. { 0x14, 0x84 },
  727. { 0x15, 0x01 },
  728. { 0x16, 0x03 },
  729. { 0x17, 0x2f },
  730. { 0x18, 0xcf },
  731. { 0x19, 0x06 },
  732. { 0x1a, 0xf5 },
  733. { 0x1b, 0x00 },
  734. { 0x20, 0x18 },
  735. { 0x21, 0x80 },
  736. { 0x22, 0x80 },
  737. { 0x23, 0x00 },
  738. { 0x26, 0xa2 },
  739. { 0x27, 0xea },
  740. { 0x28, 0x20 },
  741. { 0x29, 0x00 },
  742. { 0x2a, 0x10 },
  743. { 0x2b, 0x00 },
  744. { 0x2c, 0x88 },
  745. { 0x2d, 0x91 },
  746. { 0x2e, 0x80 },
  747. { 0x2f, 0x44 },
  748. { 0x60, 0x27 },
  749. { 0x61, 0x02 },
  750. { 0x62, 0x5f },
  751. { 0x63, 0xd5 },
  752. { 0x64, 0x57 },
  753. { 0x65, 0x83 },
  754. { 0x66, 0x55 },
  755. { 0x67, 0x92 },
  756. { 0x68, 0xcf },
  757. { 0x69, 0x76 },
  758. { 0x6a, 0x22 },
  759. { 0x6b, 0x00 },
  760. { 0x6c, 0x02 },
  761. { 0x6d, 0x44 },
  762. { 0x6e, 0x80 },
  763. { 0x6f, 0x1d },
  764. { 0x70, 0x8b },
  765. { 0x71, 0x00 },
  766. { 0x72, 0x14 },
  767. { 0x73, 0x54 },
  768. { 0x74, 0x00 },
  769. { 0x75, 0x8e },
  770. { 0x76, 0x00 },
  771. { 0x77, 0xff },
  772. { 0x78, 0x80 },
  773. { 0x79, 0x80 },
  774. { 0x7a, 0x80 },
  775. { 0x7b, 0xe2 },
  776. { 0x7c, 0x00 },
  777. };
  778. /* 7640 and 7648. The defaults should be OK for most registers. */
  779. static const struct ov_i2c_regvals norm_7640[] = {
  780. { 0x12, 0x80 },
  781. { 0x12, 0x14 },
  782. };
  783. /* 7670. Defaults taken from OmniVision provided data,
  784. * as provided by Jonathan Corbet of OLPC */
  785. static const struct ov_i2c_regvals norm_7670[] = {
  786. { OV7670_REG_COM7, OV7670_COM7_RESET },
  787. { OV7670_REG_TSLB, 0x04 }, /* OV */
  788. { OV7670_REG_COM7, OV7670_COM7_FMT_VGA }, /* VGA */
  789. { OV7670_REG_CLKRC, 0x1 },
  790. /*
  791. * Set the hardware window. These values from OV don't entirely
  792. * make sense - hstop is less than hstart. But they work...
  793. */
  794. { OV7670_REG_HSTART, 0x13 }, { OV7670_REG_HSTOP, 0x01 },
  795. { OV7670_REG_HREF, 0xb6 }, { OV7670_REG_VSTART, 0x02 },
  796. { OV7670_REG_VSTOP, 0x7a }, { OV7670_REG_VREF, 0x0a },
  797. { OV7670_REG_COM3, 0 }, { OV7670_REG_COM14, 0 },
  798. /* Mystery scaling numbers */
  799. { 0x70, 0x3a }, { 0x71, 0x35 },
  800. { 0x72, 0x11 }, { 0x73, 0xf0 },
  801. { 0xa2, 0x02 },
  802. /* jfm */
  803. /* { OV7670_REG_COM10, 0x0 }, */
  804. /* Gamma curve values */
  805. { 0x7a, 0x20 },
  806. /* jfm:win 7b=1c */
  807. { 0x7b, 0x10 },
  808. /* jfm:win 7c=28 */
  809. { 0x7c, 0x1e },
  810. /* jfm:win 7d=3c */
  811. { 0x7d, 0x35 },
  812. { 0x7e, 0x5a }, { 0x7f, 0x69 },
  813. { 0x80, 0x76 }, { 0x81, 0x80 },
  814. { 0x82, 0x88 }, { 0x83, 0x8f },
  815. { 0x84, 0x96 }, { 0x85, 0xa3 },
  816. { 0x86, 0xaf }, { 0x87, 0xc4 },
  817. { 0x88, 0xd7 }, { 0x89, 0xe8 },
  818. /* AGC and AEC parameters. Note we start by disabling those features,
  819. then turn them only after tweaking the values. */
  820. { OV7670_REG_COM8, OV7670_COM8_FASTAEC
  821. | OV7670_COM8_AECSTEP
  822. | OV7670_COM8_BFILT },
  823. { OV7670_REG_GAIN, 0 }, { OV7670_REG_AECH, 0 },
  824. { OV7670_REG_COM4, 0x40 }, /* magic reserved bit */
  825. /* jfm:win 14=38 */
  826. { OV7670_REG_COM9, 0x18 }, /* 4x gain + magic rsvd bit */
  827. { OV7670_REG_BD50MAX, 0x05 }, { OV7670_REG_BD60MAX, 0x07 },
  828. { OV7670_REG_AEW, 0x95 }, { OV7670_REG_AEB, 0x33 },
  829. { OV7670_REG_VPT, 0xe3 }, { OV7670_REG_HAECC1, 0x78 },
  830. { OV7670_REG_HAECC2, 0x68 },
  831. /* jfm:win a1=0b */
  832. { 0xa1, 0x03 }, /* magic */
  833. { OV7670_REG_HAECC3, 0xd8 }, { OV7670_REG_HAECC4, 0xd8 },
  834. { OV7670_REG_HAECC5, 0xf0 }, { OV7670_REG_HAECC6, 0x90 },
  835. { OV7670_REG_HAECC7, 0x94 },
  836. { OV7670_REG_COM8, OV7670_COM8_FASTAEC
  837. | OV7670_COM8_AECSTEP
  838. | OV7670_COM8_BFILT
  839. | OV7670_COM8_AGC
  840. | OV7670_COM8_AEC },
  841. /* Almost all of these are magic "reserved" values. */
  842. { OV7670_REG_COM5, 0x61 }, { OV7670_REG_COM6, 0x4b },
  843. { 0x16, 0x02 },
  844. /* jfm */
  845. /* { OV7670_REG_MVFP, 0x07|OV7670_MVFP_MIRROR }, */
  846. { OV7670_REG_MVFP, 0x07 },
  847. { 0x21, 0x02 }, { 0x22, 0x91 },
  848. { 0x29, 0x07 }, { 0x33, 0x0b },
  849. { 0x35, 0x0b }, { 0x37, 0x1d },
  850. { 0x38, 0x71 }, { 0x39, 0x2a },
  851. { OV7670_REG_COM12, 0x78 }, { 0x4d, 0x40 },
  852. { 0x4e, 0x20 }, { OV7670_REG_GFIX, 0 },
  853. { 0x6b, 0x4a }, { 0x74, 0x10 },
  854. { 0x8d, 0x4f }, { 0x8e, 0 },
  855. { 0x8f, 0 }, { 0x90, 0 },
  856. { 0x91, 0 }, { 0x96, 0 },
  857. { 0x9a, 0 }, { 0xb0, 0x84 },
  858. { 0xb1, 0x0c }, { 0xb2, 0x0e },
  859. { 0xb3, 0x82 }, { 0xb8, 0x0a },
  860. /* More reserved magic, some of which tweaks white balance */
  861. { 0x43, 0x0a }, { 0x44, 0xf0 },
  862. { 0x45, 0x34 }, { 0x46, 0x58 },
  863. { 0x47, 0x28 }, { 0x48, 0x3a },
  864. { 0x59, 0x88 }, { 0x5a, 0x88 },
  865. { 0x5b, 0x44 }, { 0x5c, 0x67 },
  866. { 0x5d, 0x49 }, { 0x5e, 0x0e },
  867. { 0x6c, 0x0a }, { 0x6d, 0x55 },
  868. { 0x6e, 0x11 }, { 0x6f, 0x9f },
  869. /* "9e for advance AWB" */
  870. { 0x6a, 0x40 }, { OV7670_REG_BLUE, 0x40 },
  871. { OV7670_REG_RED, 0x60 },
  872. { OV7670_REG_COM8, OV7670_COM8_FASTAEC
  873. | OV7670_COM8_AECSTEP
  874. | OV7670_COM8_BFILT
  875. | OV7670_COM8_AGC
  876. | OV7670_COM8_AEC
  877. | OV7670_COM8_AWB },
  878. /* Matrix coefficients */
  879. { 0x4f, 0x80 }, { 0x50, 0x80 },
  880. { 0x51, 0 }, { 0x52, 0x22 },
  881. { 0x53, 0x5e }, { 0x54, 0x80 },
  882. { 0x58, 0x9e },
  883. { OV7670_REG_COM16, OV7670_COM16_AWBGAIN },
  884. { OV7670_REG_EDGE, 0 },
  885. { 0x75, 0x05 }, { 0x76, 0xe1 },
  886. { 0x4c, 0 }, { 0x77, 0x01 },
  887. { OV7670_REG_COM13, OV7670_COM13_GAMMA
  888. | OV7670_COM13_UVSAT
  889. | 2}, /* was 3 */
  890. { 0x4b, 0x09 },
  891. { 0xc9, 0x60 }, { OV7670_REG_COM16, 0x38 },
  892. { 0x56, 0x40 },
  893. { 0x34, 0x11 },
  894. { OV7670_REG_COM11, OV7670_COM11_EXP|OV7670_COM11_HZAUTO },
  895. { 0xa4, 0x88 }, { 0x96, 0 },
  896. { 0x97, 0x30 }, { 0x98, 0x20 },
  897. { 0x99, 0x30 }, { 0x9a, 0x84 },
  898. { 0x9b, 0x29 }, { 0x9c, 0x03 },
  899. { 0x9d, 0x4c }, { 0x9e, 0x3f },
  900. { 0x78, 0x04 },
  901. /* Extra-weird stuff. Some sort of multiplexor register */
  902. { 0x79, 0x01 }, { 0xc8, 0xf0 },
  903. { 0x79, 0x0f }, { 0xc8, 0x00 },
  904. { 0x79, 0x10 }, { 0xc8, 0x7e },
  905. { 0x79, 0x0a }, { 0xc8, 0x80 },
  906. { 0x79, 0x0b }, { 0xc8, 0x01 },
  907. { 0x79, 0x0c }, { 0xc8, 0x0f },
  908. { 0x79, 0x0d }, { 0xc8, 0x20 },
  909. { 0x79, 0x09 }, { 0xc8, 0x80 },
  910. { 0x79, 0x02 }, { 0xc8, 0xc0 },
  911. { 0x79, 0x03 }, { 0xc8, 0x40 },
  912. { 0x79, 0x05 }, { 0xc8, 0x30 },
  913. { 0x79, 0x26 },
  914. };
  915. PDEBUG(D_PROBE, "starting OV7xx0 configuration");
  916. /* jfm:already done? */
  917. if (init_ov_sensor(sd) < 0)
  918. PDEBUG(D_ERR, "Failed to read sensor ID");
  919. else
  920. PDEBUG(D_PROBE, "OV7xx0 initialized");
  921. /* Detect sensor (sub)type */
  922. rc = i2c_r(sd, OV7610_REG_COM_I);
  923. /* add OV7670 here
  924. * it appears to be wrongly detected as a 7610 by default */
  925. if (rc < 0) {
  926. PDEBUG(D_ERR, "Error detecting sensor type");
  927. return -1;
  928. }
  929. if ((rc & 3) == 3) {
  930. /* quick hack to make OV7670s work */
  931. high = i2c_r(sd, 0x0a);
  932. low = i2c_r(sd, 0x0b);
  933. /* info("%x, %x", high, low); */
  934. if (high == 0x76 && low == 0x73) {
  935. PDEBUG(D_PROBE, "Sensor is an OV7670");
  936. sd->sensor = SEN_OV7670;
  937. } else {
  938. PDEBUG(D_PROBE, "Sensor is an OV7610");
  939. sd->sensor = SEN_OV7610;
  940. }
  941. } else if ((rc & 3) == 1) {
  942. /* I don't know what's different about the 76BE yet. */
  943. if (i2c_r(sd, 0x15) & 1)
  944. PDEBUG(D_PROBE, "Sensor is an OV7620AE");
  945. else
  946. PDEBUG(D_PROBE, "Sensor is an OV76BE");
  947. /* OV511+ will return all zero isoc data unless we
  948. * configure the sensor as a 7620. Someone needs to
  949. * find the exact reg. setting that causes this. */
  950. sd->sensor = SEN_OV76BE;
  951. } else if ((rc & 3) == 0) {
  952. /* try to read product id registers */
  953. high = i2c_r(sd, 0x0a);
  954. if (high < 0) {
  955. PDEBUG(D_ERR, "Error detecting camera chip PID");
  956. return high;
  957. }
  958. low = i2c_r(sd, 0x0b);
  959. if (low < 0) {
  960. PDEBUG(D_ERR, "Error detecting camera chip VER");
  961. return low;
  962. }
  963. if (high == 0x76) {
  964. if (low == 0x30) {
  965. PDEBUG(D_PROBE, "Sensor is an OV7630/OV7635");
  966. sd->sensor = SEN_OV7630;
  967. } else if (low == 0x40) {
  968. PDEBUG(D_PROBE, "Sensor is an OV7645");
  969. sd->sensor = SEN_OV7640; /* FIXME */
  970. } else if (low == 0x45) {
  971. PDEBUG(D_PROBE, "Sensor is an OV7645B");
  972. sd->sensor = SEN_OV7640; /* FIXME */
  973. } else if (low == 0x48) {
  974. PDEBUG(D_PROBE, "Sensor is an OV7648");
  975. sd->sensor = SEN_OV7640; /* FIXME */
  976. } else {
  977. PDEBUG(D_PROBE, "Unknown sensor: 0x76%X", low);
  978. return -1;
  979. }
  980. } else {
  981. PDEBUG(D_PROBE, "Sensor is an OV7620");
  982. sd->sensor = SEN_OV7620;
  983. }
  984. } else {
  985. PDEBUG(D_ERR, "Unknown image sensor version: %d", rc & 3);
  986. return -1;
  987. }
  988. if (sd->sensor == SEN_OV7620) {
  989. PDEBUG(D_PROBE, "Writing 7620 registers");
  990. if (write_i2c_regvals(sd, norm_7620,
  991. sizeof norm_7620 / sizeof norm_7620[0]))
  992. return -1;
  993. } else if (sd->sensor == SEN_OV7630) {
  994. PDEBUG(D_ERR, "7630 is not supported by this driver version");
  995. return -1;
  996. } else if (sd->sensor == SEN_OV7640) {
  997. PDEBUG(D_PROBE, "Writing 7640 registers");
  998. if (write_i2c_regvals(sd, norm_7640,
  999. sizeof norm_7640 / sizeof norm_7640[0]))
  1000. return -1;
  1001. } else if (sd->sensor == SEN_OV7670) {
  1002. PDEBUG(D_PROBE, "Writing 7670 registers");
  1003. if (write_i2c_regvals(sd, norm_7670,
  1004. sizeof norm_7670 / sizeof norm_7670[0]))
  1005. return -1;
  1006. } else {
  1007. PDEBUG(D_PROBE, "Writing 7610 registers");
  1008. if (write_i2c_regvals(sd, norm_7610,
  1009. sizeof norm_7610 / sizeof norm_7610[0]))
  1010. return -1;
  1011. }
  1012. /* Set sensor-specific vars */
  1013. sd->maxwidth = 640;
  1014. sd->maxheight = 480;
  1015. return 0;
  1016. }
  1017. /* This initializes the OV6620, OV6630, OV6630AE, or OV6630AF sensor. */
  1018. static int ov6xx0_configure(struct sd *sd)
  1019. {
  1020. int rc;
  1021. static const struct ov_i2c_regvals norm_6x20[] = {
  1022. { 0x12, 0x80 }, /* reset */
  1023. { 0x11, 0x01 },
  1024. { 0x03, 0x60 },
  1025. { 0x05, 0x7f }, /* For when autoadjust is off */
  1026. { 0x07, 0xa8 },
  1027. /* The ratio of 0x0c and 0x0d controls the white point */
  1028. { 0x0c, 0x24 },
  1029. { 0x0d, 0x24 },
  1030. { 0x0f, 0x15 }, /* COMS */
  1031. { 0x10, 0x75 }, /* AEC Exposure time */
  1032. { 0x12, 0x24 }, /* Enable AGC */
  1033. { 0x14, 0x04 },
  1034. /* 0x16: 0x06 helps frame stability with moving objects */
  1035. { 0x16, 0x06 },
  1036. /* { 0x20, 0x30 }, * Aperture correction enable */
  1037. { 0x26, 0xb2 }, /* BLC enable */
  1038. /* 0x28: 0x05 Selects RGB format if RGB on */
  1039. { 0x28, 0x05 },
  1040. { 0x2a, 0x04 }, /* Disable framerate adjust */
  1041. /* { 0x2b, 0xac }, * Framerate; Set 2a[7] first */
  1042. { 0x2d, 0x99 },
  1043. { 0x33, 0xa0 }, /* Color Processing Parameter */
  1044. { 0x34, 0xd2 }, /* Max A/D range */
  1045. { 0x38, 0x8b },
  1046. { 0x39, 0x40 },
  1047. { 0x3c, 0x39 }, /* Enable AEC mode changing */
  1048. { 0x3c, 0x3c }, /* Change AEC mode */
  1049. { 0x3c, 0x24 }, /* Disable AEC mode changing */
  1050. { 0x3d, 0x80 },
  1051. /* These next two registers (0x4a, 0x4b) are undocumented.
  1052. * They control the color balance */
  1053. { 0x4a, 0x80 },
  1054. { 0x4b, 0x80 },
  1055. { 0x4d, 0xd2 }, /* This reduces noise a bit */
  1056. { 0x4e, 0xc1 },
  1057. { 0x4f, 0x04 },
  1058. /* Do 50-53 have any effect? */
  1059. /* Toggle 0x12[2] off and on here? */
  1060. };
  1061. static const struct ov_i2c_regvals norm_6x30[] = {
  1062. { 0x12, 0x80 }, /* Reset */
  1063. { 0x00, 0x1f }, /* Gain */
  1064. { 0x01, 0x99 }, /* Blue gain */
  1065. { 0x02, 0x7c }, /* Red gain */
  1066. { 0x03, 0xc0 }, /* Saturation */
  1067. { 0x05, 0x0a }, /* Contrast */
  1068. { 0x06, 0x95 }, /* Brightness */
  1069. { 0x07, 0x2d }, /* Sharpness */
  1070. { 0x0c, 0x20 },
  1071. { 0x0d, 0x20 },
  1072. { 0x0e, 0x20 },
  1073. { 0x0f, 0x05 },
  1074. { 0x10, 0x9a },
  1075. { 0x11, 0x00 }, /* Pixel clock = fastest */
  1076. { 0x12, 0x24 }, /* Enable AGC and AWB */
  1077. { 0x13, 0x21 },
  1078. { 0x14, 0x80 },
  1079. { 0x15, 0x01 },
  1080. { 0x16, 0x03 },
  1081. { 0x17, 0x38 },
  1082. { 0x18, 0xea },
  1083. { 0x19, 0x04 },
  1084. { 0x1a, 0x93 },
  1085. { 0x1b, 0x00 },
  1086. { 0x1e, 0xc4 },
  1087. { 0x1f, 0x04 },
  1088. { 0x20, 0x20 },
  1089. { 0x21, 0x10 },
  1090. { 0x22, 0x88 },
  1091. { 0x23, 0xc0 }, /* Crystal circuit power level */
  1092. { 0x25, 0x9a }, /* Increase AEC black ratio */
  1093. { 0x26, 0xb2 }, /* BLC enable */
  1094. { 0x27, 0xa2 },
  1095. { 0x28, 0x00 },
  1096. { 0x29, 0x00 },
  1097. { 0x2a, 0x84 }, /* 60 Hz power */
  1098. { 0x2b, 0xa8 }, /* 60 Hz power */
  1099. { 0x2c, 0xa0 },
  1100. { 0x2d, 0x95 }, /* Enable auto-brightness */
  1101. { 0x2e, 0x88 },
  1102. { 0x33, 0x26 },
  1103. { 0x34, 0x03 },
  1104. { 0x36, 0x8f },
  1105. { 0x37, 0x80 },
  1106. { 0x38, 0x83 },
  1107. { 0x39, 0x80 },
  1108. { 0x3a, 0x0f },
  1109. { 0x3b, 0x3c },
  1110. { 0x3c, 0x1a },
  1111. { 0x3d, 0x80 },
  1112. { 0x3e, 0x80 },
  1113. { 0x3f, 0x0e },
  1114. { 0x40, 0x00 }, /* White bal */
  1115. { 0x41, 0x00 }, /* White bal */
  1116. { 0x42, 0x80 },
  1117. { 0x43, 0x3f }, /* White bal */
  1118. { 0x44, 0x80 },
  1119. { 0x45, 0x20 },
  1120. { 0x46, 0x20 },
  1121. { 0x47, 0x80 },
  1122. { 0x48, 0x7f },
  1123. { 0x49, 0x00 },
  1124. { 0x4a, 0x00 },
  1125. { 0x4b, 0x80 },
  1126. { 0x4c, 0xd0 },
  1127. { 0x4d, 0x10 }, /* U = 0.563u, V = 0.714v */
  1128. { 0x4e, 0x40 },
  1129. { 0x4f, 0x07 }, /* UV avg., col. killer: max */
  1130. { 0x50, 0xff },
  1131. { 0x54, 0x23 }, /* Max AGC gain: 18dB */
  1132. { 0x55, 0xff },
  1133. { 0x56, 0x12 },
  1134. { 0x57, 0x81 },
  1135. { 0x58, 0x75 },
  1136. { 0x59, 0x01 }, /* AGC dark current comp.: +1 */
  1137. { 0x5a, 0x2c },
  1138. { 0x5b, 0x0f }, /* AWB chrominance levels */
  1139. { 0x5c, 0x10 },
  1140. { 0x3d, 0x80 },
  1141. { 0x27, 0xa6 },
  1142. { 0x12, 0x20 }, /* Toggle AWB */
  1143. { 0x12, 0x24 },
  1144. };
  1145. PDEBUG(D_PROBE, "starting sensor configuration");
  1146. if (init_ov_sensor(sd) < 0) {
  1147. PDEBUG(D_ERR, "Failed to read sensor ID.");
  1148. return -1;
  1149. }
  1150. PDEBUG(D_PROBE, "OV6xx0 sensor detected");
  1151. /* Detect sensor (sub)type */
  1152. rc = i2c_r(sd, OV7610_REG_COM_I);
  1153. if (rc < 0) {
  1154. PDEBUG(D_ERR, "Error detecting sensor type");
  1155. return -1;
  1156. }
  1157. /* Ugh. The first two bits are the version bits, but
  1158. * the entire register value must be used. I guess OVT
  1159. * underestimated how many variants they would make. */
  1160. if (rc == 0x00) {
  1161. sd->sensor = SEN_OV6630;
  1162. PDEBUG(D_ERR,
  1163. "WARNING: Sensor is an OV66308. Your camera may have");
  1164. PDEBUG(D_ERR, "been misdetected in previous driver versions.");
  1165. } else if (rc == 0x01) {
  1166. sd->sensor = SEN_OV6620;
  1167. PDEBUG(D_PROBE, "Sensor is an OV6620");
  1168. } else if (rc == 0x02) {
  1169. sd->sensor = SEN_OV6630;
  1170. PDEBUG(D_PROBE, "Sensor is an OV66308AE");
  1171. } else if (rc == 0x03) {
  1172. sd->sensor = SEN_OV6630;
  1173. PDEBUG(D_PROBE, "Sensor is an OV66308AF");
  1174. } else if (rc == 0x90) {
  1175. sd->sensor = SEN_OV6630;
  1176. PDEBUG(D_ERR,
  1177. "WARNING: Sensor is an OV66307. Your camera may have");
  1178. PDEBUG(D_ERR, "been misdetected in previous driver versions.");
  1179. } else {
  1180. PDEBUG(D_ERR, "FATAL: Unknown sensor version: 0x%02x", rc);
  1181. return -1;
  1182. }
  1183. /* Set sensor-specific vars */
  1184. sd->maxwidth = 352;
  1185. sd->maxheight = 288;
  1186. if (sd->sensor == SEN_OV6620) {
  1187. PDEBUG(D_PROBE, "Writing 6x20 registers");
  1188. if (write_i2c_regvals(sd, norm_6x20,
  1189. sizeof norm_6x20 / sizeof norm_6x20[0]))
  1190. return -1;
  1191. } else {
  1192. PDEBUG(D_PROBE, "Writing 6x30 registers");
  1193. if (write_i2c_regvals(sd, norm_6x30,
  1194. sizeof norm_6x30 / sizeof norm_6x30[0]))
  1195. return -1;
  1196. }
  1197. return 0;
  1198. }
  1199. /* Turns on or off the LED. Only has an effect with OV511+/OV518(+)/OV519 */
  1200. static void ov51x_led_control(struct sd *sd, int on)
  1201. {
  1202. PDEBUG(D_STREAM, "LED (%s)", on ? "on" : "off");
  1203. /* if (sd->bridge == BRG_OV511PLUS) */
  1204. /* reg_w(sd, R511_SYS_LED_CTL, on ? 1 : 0); */
  1205. /* else if (sd->bridge == BRG_OV519) */
  1206. reg_w_mask(sd, OV519_GPIO_DATA_OUT0, !on, 1); /* 0 / 1 */
  1207. /* else if (sd->bclass == BCL_OV518) */
  1208. /* reg_w_mask(sd, R518_GPIO_OUT, on ? 0x02 : 0x00, 0x02); */
  1209. }
  1210. /* this function is called at probe time */
  1211. static int sd_config(struct gspca_dev *gspca_dev,
  1212. const struct usb_device_id *id)
  1213. {
  1214. struct sd *sd = (struct sd *) gspca_dev;
  1215. struct cam *cam;
  1216. /* (from ov519_configure) */
  1217. static const struct ov_regvals init_519[] = {
  1218. { 0x5a, 0x6d }, /* EnableSystem */
  1219. /* jfm trace usbsnoop3-1.txt */
  1220. /* jfm 53 = fb */
  1221. { 0x53, 0x9b },
  1222. { 0x54, 0xff }, /* set bit2 to enable jpeg */
  1223. { 0x5d, 0x03 },
  1224. { 0x49, 0x01 },
  1225. { 0x48, 0x00 },
  1226. /* Set LED pin to output mode. Bit 4 must be cleared or sensor
  1227. * detection will fail. This deserves further investigation. */
  1228. { OV519_GPIO_IO_CTRL0, 0xee },
  1229. { 0x51, 0x0f }, /* SetUsbInit */
  1230. { 0x51, 0x00 },
  1231. { 0x22, 0x00 },
  1232. /* windows reads 0x55 at this point*/
  1233. };
  1234. if (write_regvals(sd, init_519, ARRAY_SIZE(init_519)))
  1235. goto error;
  1236. /* jfm: not seen in windows trace */
  1237. if (ov519_init_compression(sd))
  1238. goto error;
  1239. ov51x_led_control(sd, 0); /* turn LED off */
  1240. /* Test for 76xx */
  1241. sd->primary_i2c_slave = OV7xx0_SID;
  1242. if (ov51x_set_slave_ids(sd, OV7xx0_SID) < 0)
  1243. goto error;
  1244. /* The OV519 must be more aggressive about sensor detection since
  1245. * I2C write will never fail if the sensor is not present. We have
  1246. * to try to initialize the sensor to detect its presence */
  1247. if (init_ov_sensor(sd) < 0) {
  1248. /* Test for 6xx0 */
  1249. sd->primary_i2c_slave = OV6xx0_SID;
  1250. if (ov51x_set_slave_ids(sd, OV6xx0_SID) < 0)
  1251. goto error;
  1252. if (init_ov_sensor(sd) < 0) {
  1253. /* Test for 8xx0 */
  1254. sd->primary_i2c_slave = OV8xx0_SID;
  1255. if (ov51x_set_slave_ids(sd, OV8xx0_SID) < 0)
  1256. goto error;
  1257. if (init_ov_sensor(sd) < 0) {
  1258. PDEBUG(D_ERR,
  1259. "Can't determine sensor slave IDs");
  1260. goto error;
  1261. } else {
  1262. if (ov8xx0_configure(sd) < 0) {
  1263. PDEBUG(D_ERR,
  1264. "Failed to configure OV8xx0 sensor");
  1265. goto error;
  1266. }
  1267. }
  1268. } else {
  1269. if (ov6xx0_configure(sd) < 0) {
  1270. PDEBUG(D_ERR, "Failed to configure OV6xx0");
  1271. goto error;
  1272. }
  1273. }
  1274. } else {
  1275. if (ov7xx0_configure(sd) < 0) {
  1276. PDEBUG(D_ERR, "Failed to configure OV7xx0");
  1277. goto error;
  1278. }
  1279. }
  1280. cam = &gspca_dev->cam;
  1281. cam->epaddr = OV511_ENDPOINT_ADDRESS;
  1282. if (sd->maxwidth == 640) {
  1283. cam->cam_mode = vga_mode;
  1284. cam->nmodes = sizeof vga_mode / sizeof vga_mode[0];
  1285. } else {
  1286. cam->cam_mode = sif_mode;
  1287. cam->nmodes = sizeof sif_mode / sizeof sif_mode[0];
  1288. }
  1289. sd->brightness = sd_ctrls[SD_BRIGHTNESS].qctrl.default_value;
  1290. sd->contrast = sd_ctrls[SD_CONTRAST].qctrl.default_value;
  1291. sd->colors = sd_ctrls[SD_COLOR].qctrl.default_value;
  1292. sd->hflip = HFLIP_DEF;
  1293. sd->vflip = VFLIP_DEF;
  1294. return 0;
  1295. error:
  1296. PDEBUG(D_ERR, "OV519 Config failed");
  1297. return -EBUSY;
  1298. }
  1299. /* this function is called at open time */
  1300. static int sd_open(struct gspca_dev *gspca_dev)
  1301. {
  1302. return 0;
  1303. }
  1304. /* Sets up the OV519 with the given image parameters
  1305. *
  1306. * OV519 needs a completely different approach, until we can figure out what
  1307. * the individual registers do.
  1308. *
  1309. * Do not put any sensor-specific code in here (including I2C I/O functions)
  1310. */
  1311. static int ov519_mode_init_regs(struct sd *sd,
  1312. int width, int height)
  1313. {
  1314. static const struct ov_regvals mode_init_519_ov7670[] = {
  1315. { 0x5d, 0x03 }, /* Turn off suspend mode */
  1316. { 0x53, 0x9f }, /* was 9b in 1.65-1.08 */
  1317. { 0x54, 0x0f }, /* bit2 (jpeg enable) */
  1318. { 0xa2, 0x20 }, /* a2-a5 are undocumented */
  1319. { 0xa3, 0x18 },
  1320. { 0xa4, 0x04 },
  1321. { 0xa5, 0x28 },
  1322. { 0x37, 0x00 }, /* SetUsbInit */
  1323. { 0x55, 0x02 }, /* 4.096 Mhz audio clock */
  1324. /* Enable both fields, YUV Input, disable defect comp (why?) */
  1325. { 0x20, 0x0c },
  1326. { 0x21, 0x38 },
  1327. { 0x22, 0x1d },
  1328. { 0x17, 0x50 }, /* undocumented */
  1329. { 0x37, 0x00 }, /* undocumented */
  1330. { 0x40, 0xff }, /* I2C timeout counter */
  1331. { 0x46, 0x00 }, /* I2C clock prescaler */
  1332. { 0x59, 0x04 }, /* new from windrv 090403 */
  1333. { 0xff, 0x00 }, /* undocumented */
  1334. /* windows reads 0x55 at this point, why? */
  1335. };
  1336. static const struct ov_regvals mode_init_519[] = {
  1337. { 0x5d, 0x03 }, /* Turn off suspend mode */
  1338. { 0x53, 0x9f }, /* was 9b in 1.65-1.08 */
  1339. { 0x54, 0x0f }, /* bit2 (jpeg enable) */
  1340. { 0xa2, 0x20 }, /* a2-a5 are undocumented */
  1341. { 0xa3, 0x18 },
  1342. { 0xa4, 0x04 },
  1343. { 0xa5, 0x28 },
  1344. { 0x37, 0x00 }, /* SetUsbInit */
  1345. { 0x55, 0x02 }, /* 4.096 Mhz audio clock */
  1346. /* Enable both fields, YUV Input, disable defect comp (why?) */
  1347. { 0x22, 0x1d },
  1348. { 0x17, 0x50 }, /* undocumented */
  1349. { 0x37, 0x00 }, /* undocumented */
  1350. { 0x40, 0xff }, /* I2C timeout counter */
  1351. { 0x46, 0x00 }, /* I2C clock prescaler */
  1352. { 0x59, 0x04 }, /* new from windrv 090403 */
  1353. { 0xff, 0x00 }, /* undocumented */
  1354. /* windows reads 0x55 at this point, why? */
  1355. };
  1356. /* int hi_res; */
  1357. PDEBUG(D_CONF, "mode init %dx%d", width, height);
  1358. /* if (width >= 800 && height >= 600)
  1359. hi_res = 1;
  1360. else
  1361. hi_res = 0; */
  1362. /* if (ov51x_stop(sd) < 0)
  1363. return -EIO; */
  1364. /******** Set the mode ********/
  1365. if (sd->sensor != SEN_OV7670) {
  1366. if (write_regvals(sd, mode_init_519,
  1367. ARRAY_SIZE(mode_init_519)))
  1368. return -EIO;
  1369. } else {
  1370. if (write_regvals(sd, mode_init_519_ov7670,
  1371. ARRAY_SIZE(mode_init_519_ov7670)))
  1372. return -EIO;
  1373. }
  1374. if (sd->sensor == SEN_OV7640) {
  1375. /* Select 8-bit input mode */
  1376. reg_w_mask(sd, OV519_CAM_DFR, 0x10, 0x10);
  1377. }
  1378. reg_w(sd, OV519_CAM_H_SIZE, width >> 4);
  1379. reg_w(sd, OV519_CAM_V_SIZE, height >> 3);
  1380. reg_w(sd, OV519_CAM_X_OFFSETL, 0x00);
  1381. reg_w(sd, OV519_CAM_X_OFFSETH, 0x00);
  1382. reg_w(sd, OV519_CAM_Y_OFFSETL, 0x00);
  1383. reg_w(sd, OV519_CAM_Y_OFFSETH, 0x00);
  1384. reg_w(sd, OV519_CAM_DIVIDER, 0x00);
  1385. reg_w(sd, OV519_CAM_FORMAT, 0x03); /* YUV422 */
  1386. reg_w(sd, 0x26, 0x00); /* Undocumented */
  1387. /******** Set the framerate ********/
  1388. if (frame_rate > 0)
  1389. sd->frame_rate = frame_rate;
  1390. /* FIXME: These are only valid at the max resolution. */
  1391. sd->clockdiv = 0;
  1392. if (sd->sensor == SEN_OV7640) {
  1393. switch (sd->frame_rate) {
  1394. /*jfm: default was 30 fps */
  1395. case 30:
  1396. reg_w(sd, 0xa4, 0x0c);
  1397. reg_w(sd, 0x23, 0xff);
  1398. break;
  1399. case 25:
  1400. reg_w(sd, 0xa4, 0x0c);
  1401. reg_w(sd, 0x23, 0x1f);
  1402. break;
  1403. case 20:
  1404. reg_w(sd, 0xa4, 0x0c);
  1405. reg_w(sd, 0x23, 0x1b);
  1406. break;
  1407. default:
  1408. /* case 15: */
  1409. reg_w(sd, 0xa4, 0x04);
  1410. reg_w(sd, 0x23, 0xff);
  1411. sd->clockdiv = 1;
  1412. break;
  1413. case 10:
  1414. reg_w(sd, 0xa4, 0x04);
  1415. reg_w(sd, 0x23, 0x1f);
  1416. sd->clockdiv = 1;
  1417. break;
  1418. case 5:
  1419. reg_w(sd, 0xa4, 0x04);
  1420. reg_w(sd, 0x23, 0x1b);
  1421. sd->clockdiv = 1;
  1422. break;
  1423. }
  1424. } else if (sd->sensor == SEN_OV8610) {
  1425. switch (sd->frame_rate) {
  1426. default: /* 15 fps */
  1427. /* case 15: */
  1428. reg_w(sd, 0xa4, 0x06);
  1429. reg_w(sd, 0x23, 0xff);
  1430. break;
  1431. case 10:
  1432. reg_w(sd, 0xa4, 0x06);
  1433. reg_w(sd, 0x23, 0x1f);
  1434. break;
  1435. case 5:
  1436. reg_w(sd, 0xa4, 0x06);
  1437. reg_w(sd, 0x23, 0x1b);
  1438. break;
  1439. }
  1440. sd->clockdiv = 0;
  1441. } else if (sd->sensor == SEN_OV7670) { /* guesses, based on 7640 */
  1442. PDEBUG(D_STREAM, "Setting framerate to %d fps",
  1443. (sd->frame_rate == 0) ? 15 : sd->frame_rate);
  1444. switch (sd->frame_rate) {
  1445. case 30:
  1446. reg_w(sd, 0xa4, 0x10);
  1447. reg_w(sd, 0x23, 0xff);
  1448. break;
  1449. case 20:
  1450. reg_w(sd, 0xa4, 0x10);
  1451. reg_w(sd, 0x23, 0x1b);
  1452. break;
  1453. default: /* 15 fps */
  1454. /* case 15: */
  1455. reg_w(sd, 0xa4, 0x10);
  1456. reg_w(sd, 0x23, 0xff);
  1457. sd->clockdiv = 1;
  1458. break;
  1459. }
  1460. }
  1461. /* if (ov51x_restart(sd) < 0)
  1462. return -EIO; */
  1463. /* Reset it just for good measure */
  1464. /* if (ov51x_reset(sd, OV511_RESET_NOREGS) < 0)
  1465. return -EIO; */
  1466. return 0;
  1467. }
  1468. static int mode_init_ov_sensor_regs(struct sd *sd,
  1469. struct ovsensor_window *win)
  1470. {
  1471. int qvga = win->quarter;
  1472. /******** Mode (VGA/QVGA) and sensor specific regs ********/
  1473. switch (sd->sensor) {
  1474. case SEN_OV8610:
  1475. /* For OV8610 qvga means qsvga */
  1476. i2c_w_mask(sd, OV7610_REG_COM_C, qvga ? (1 << 5) : 0, 1 << 5);
  1477. break;
  1478. case SEN_OV7610:
  1479. i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
  1480. break;
  1481. case SEN_OV7620:
  1482. /* i2c_w(sd, 0x2b, 0x00); */
  1483. i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
  1484. i2c_w_mask(sd, 0x28, qvga ? 0x00 : 0x20, 0x20);
  1485. i2c_w(sd, 0x24, qvga ? 0x20 : 0x3a);
  1486. i2c_w(sd, 0x25, qvga ? 0x30 : 0x60);
  1487. i2c_w_mask(sd, 0x2d, qvga ? 0x40 : 0x00, 0x40);
  1488. i2c_w_mask(sd, 0x67, qvga ? 0xf0 : 0x90, 0xf0);
  1489. i2c_w_mask(sd, 0x74, qvga ? 0x20 : 0x00, 0x20);
  1490. break;
  1491. case SEN_OV76BE:
  1492. /* i2c_w(sd, 0x2b, 0x00); */
  1493. i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
  1494. break;
  1495. case SEN_OV7640:
  1496. /* i2c_w(sd, 0x2b, 0x00); */
  1497. i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
  1498. i2c_w_mask(sd, 0x28, qvga ? 0x00 : 0x20, 0x20);
  1499. /* i2c_w(sd, 0x24, qvga ? 0x20 : 0x3a); */
  1500. /* i2c_w(sd, 0x25, qvga ? 0x30 : 0x60); */
  1501. /* i2c_w_mask(sd, 0x2d, qvga ? 0x40 : 0x00, 0x40); */
  1502. /* i2c_w_mask(sd, 0x67, qvga ? 0xf0 : 0x90, 0xf0); */
  1503. /* i2c_w_mask(sd, 0x74, qvga ? 0x20 : 0x00, 0x20); */
  1504. break;
  1505. case SEN_OV7670:
  1506. /* set COM7_FMT_VGA or COM7_FMT_QVGA
  1507. * do we need to set anything else?
  1508. * HSTART etc are set in set_ov_sensor_window itself */
  1509. i2c_w_mask(sd, OV7670_REG_COM7,
  1510. qvga ? OV7670_COM7_FMT_QVGA : OV7670_COM7_FMT_VGA,
  1511. OV7670_COM7_FMT_MASK);
  1512. break;
  1513. case SEN_OV6620:
  1514. i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
  1515. break;
  1516. case SEN_OV6630:
  1517. i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
  1518. break;
  1519. default:
  1520. return -EINVAL;
  1521. }
  1522. /******** Palette-specific regs ********/
  1523. /* Need to do work here for the OV7670 */
  1524. if (sd->sensor == SEN_OV7610 || sd->sensor == SEN_OV76BE) {
  1525. /* not valid on the OV6620/OV7620/6630? */
  1526. i2c_w_mask(sd, 0x0e, 0x00, 0x40);
  1527. }
  1528. /* The OV518 needs special treatment. Although both the OV518
  1529. * and the OV6630 support a 16-bit video bus, only the 8 bit Y
  1530. * bus is actually used. The UV bus is tied to ground.
  1531. * Therefore, the OV6630 needs to be in 8-bit multiplexed
  1532. * output mode */
  1533. /* OV7640 is 8-bit only */
  1534. if (sd->sensor != SEN_OV6630 && sd->sensor != SEN_OV7640)
  1535. i2c_w_mask(sd, 0x13, 0x00, 0x20);
  1536. /* } */
  1537. /******** Clock programming ********/
  1538. /* The OV6620 needs special handling. This prevents the
  1539. * severe banding that normally occurs */
  1540. if (sd->sensor == SEN_OV6620) {
  1541. /* Clock down */
  1542. i2c_w(sd, 0x2a, 0x04);
  1543. i2c_w(sd, 0x11, win->clockdiv);
  1544. i2c_w(sd, 0x2a, 0x84);
  1545. /* This next setting is critical. It seems to improve
  1546. * the gain or the contrast. The "reserved" bits seem
  1547. * to have some effect in this case. */
  1548. i2c_w(sd, 0x2d, 0x85);
  1549. } else if (win->clockdiv >= 0) {
  1550. i2c_w(sd, 0x11, win->clockdiv);
  1551. }
  1552. /******** Special Features ********/
  1553. /* no evidence this is possible with OV7670, either */
  1554. /* Test Pattern */
  1555. if (sd->sensor != SEN_OV7640 && sd->sensor != SEN_OV7670)
  1556. i2c_w_mask(sd, 0x12, 0x00, 0x02);
  1557. /* Enable auto white balance */
  1558. if (sd->sensor == SEN_OV7670)
  1559. i2c_w_mask(sd, OV7670_REG_COM8, OV7670_COM8_AWB,
  1560. OV7670_COM8_AWB);
  1561. else
  1562. i2c_w_mask(sd, 0x12, 0x04, 0x04);
  1563. /* This will go away as soon as ov51x_mode_init_sensor_regs() */
  1564. /* is fully tested. */
  1565. /* 7620/6620/6630? don't have register 0x35, so play it safe */
  1566. if (sd->sensor == SEN_OV7610 || sd->sensor == SEN_OV76BE) {
  1567. if (win->width == 640 /*&& win->height == 480*/)
  1568. i2c_w(sd, 0x35, 0x9e);
  1569. else
  1570. i2c_w(sd, 0x35, 0x1e);
  1571. }
  1572. return 0;
  1573. }
  1574. static void sethflip(struct sd *sd)
  1575. {
  1576. if (sd->gspca_dev.streaming)
  1577. ov51x_stop(sd);
  1578. i2c_w_mask(sd, OV7670_REG_MVFP,
  1579. OV7670_MVFP_MIRROR * sd->hflip, OV7670_MVFP_MIRROR);
  1580. if (sd->gspca_dev.streaming)
  1581. ov51x_restart(sd);
  1582. }
  1583. static void setvflip(struct sd *sd)
  1584. {
  1585. if (sd->gspca_dev.streaming)
  1586. ov51x_stop(sd);
  1587. i2c_w_mask(sd, OV7670_REG_MVFP,
  1588. OV7670_MVFP_VFLIP * sd->vflip, OV7670_MVFP_VFLIP);
  1589. if (sd->gspca_dev.streaming)
  1590. ov51x_restart(sd);
  1591. }
  1592. static int set_ov_sensor_window(struct sd *sd,
  1593. struct ovsensor_window *win)
  1594. {
  1595. int hwsbase, hwebase, vwsbase, vwebase, hwscale, vwscale;
  1596. int ret, hstart, hstop, vstop, vstart;
  1597. __u8 v;
  1598. /* The different sensor ICs handle setting up of window differently.
  1599. * IF YOU SET IT WRONG, YOU WILL GET ALL ZERO ISOC DATA FROM OV51x!! */
  1600. switch (sd->sensor) {
  1601. case SEN_OV8610:
  1602. hwsbase = 0x1e;
  1603. hwebase = 0x1e;
  1604. vwsbase = 0x02;
  1605. vwebase = 0x02;
  1606. break;
  1607. case SEN_OV7610:
  1608. case SEN_OV76BE:
  1609. hwsbase = 0x38;
  1610. hwebase = 0x3a;
  1611. vwsbase = vwebase = 0x05;
  1612. break;
  1613. case SEN_OV6620:
  1614. case SEN_OV6630:
  1615. hwsbase = 0x38;
  1616. hwebase = 0x3a;
  1617. vwsbase = 0x05;
  1618. vwebase = 0x06;
  1619. break;
  1620. case SEN_OV7620:
  1621. hwsbase = 0x2f; /* From 7620.SET (spec is wrong) */
  1622. hwebase = 0x2f;
  1623. vwsbase = vwebase = 0x05;
  1624. break;
  1625. case SEN_OV7640:
  1626. hwsbase = 0x1a;
  1627. hwebase = 0x1a;
  1628. vwsbase = vwebase = 0x03;
  1629. break;
  1630. case SEN_OV7670:
  1631. /*handling of OV7670 hardware sensor start and stop values
  1632. * is very odd, compared to the other OV sensors */
  1633. vwsbase = vwebase = hwebase = hwsbase = 0x00;
  1634. break;
  1635. default:
  1636. return -EINVAL;
  1637. }
  1638. switch (sd->sensor) {
  1639. case SEN_OV6620:
  1640. case SEN_OV6630:
  1641. if (win->quarter) { /* QCIF */
  1642. hwscale = 0;
  1643. vwscale = 0;
  1644. } else { /* CIF */
  1645. hwscale = 1;
  1646. vwscale = 1; /* The datasheet says 0;
  1647. * it's wrong */
  1648. }
  1649. break;
  1650. case SEN_OV8610:
  1651. if (win->quarter) { /* QSVGA */
  1652. hwscale = 1;
  1653. vwscale = 1;
  1654. } else { /* SVGA */
  1655. hwscale = 2;
  1656. vwscale = 2;
  1657. }
  1658. break;
  1659. default: /* SEN_OV7xx0 */
  1660. if (win->quarter) { /* QVGA */
  1661. hwscale = 1;
  1662. vwscale = 0;
  1663. } else { /* VGA */
  1664. hwscale = 2;
  1665. vwscale = 1;
  1666. }
  1667. }
  1668. ret = mode_init_ov_sensor_regs(sd, win);
  1669. if (ret < 0)
  1670. return ret;
  1671. if (sd->sensor == SEN_OV8610) {
  1672. i2c_w_mask(sd, 0x2d, 0x05, 0x40);
  1673. /* old 0x95, new 0x05 from windrv 090403 */
  1674. /* bits 5-7: reserved */
  1675. i2c_w_mask(sd, 0x28, 0x20, 0x20);
  1676. /* bit 5: progressive mode on */
  1677. }
  1678. /* The below is wrong for OV7670s because their window registers
  1679. * only store the high bits in 0x17 to 0x1a */
  1680. /* SRH Use sd->max values instead of requested win values */
  1681. /* SCS Since we're sticking with only the max hardware widths
  1682. * for a given mode */
  1683. /* I can hard code this for OV7670s */
  1684. /* Yes, these numbers do look odd, but they're tested and work! */
  1685. if (sd->sensor == SEN_OV7670) {
  1686. if (win->quarter) { /* QVGA from ov7670.c by
  1687. * Jonathan Corbet */
  1688. hstart = 164;
  1689. hstop = 20;
  1690. vstart = 14;
  1691. vstop = 494;
  1692. } else { /* VGA */
  1693. hstart = 158;
  1694. hstop = 14;
  1695. vstart = 10;
  1696. vstop = 490;
  1697. }
  1698. /* OV7670 hardware window registers are split across
  1699. * multiple locations */
  1700. i2c_w(sd, OV7670_REG_HSTART, (hstart >> 3) & 0xff);
  1701. i2c_w(sd, OV7670_REG_HSTOP, (hstop >> 3) & 0xff);
  1702. v = i2c_r(sd, OV7670_REG_HREF);
  1703. v = (v & 0xc0) | ((hstop & 0x7) << 3) | (hstart & 0x07);
  1704. msleep(10); /* need to sleep between read and write to
  1705. * same reg! */
  1706. i2c_w(sd, OV7670_REG_HREF, v);
  1707. i2c_w(sd, OV7670_REG_VSTART, (vstart >> 2) & 0xff);
  1708. i2c_w(sd, OV7670_REG_VSTOP, (vstop >> 2) & 0xff);
  1709. v = i2c_r(sd, OV7670_REG_VREF);
  1710. v = (v & 0xc0) | ((vstop & 0x3) << 2) | (vstart & 0x03);
  1711. msleep(10); /* need to sleep between read and write to
  1712. * same reg! */
  1713. i2c_w(sd, OV7670_REG_VREF, v);
  1714. sethflip(sd);
  1715. setvflip(sd);
  1716. } else {
  1717. i2c_w(sd, 0x17, hwsbase + (win->x >> hwscale));
  1718. i2c_w(sd, 0x18, hwebase + ((win->x + win->width) >> hwscale));
  1719. i2c_w(sd, 0x19, vwsbase + (win->y >> vwscale));
  1720. i2c_w(sd, 0x1a, vwebase + ((win->y + win->height) >> vwscale));
  1721. }
  1722. return 0;
  1723. }
  1724. static int ov_sensor_mode_setup(struct sd *sd,
  1725. int width, int height)
  1726. {
  1727. struct ovsensor_window win;
  1728. /* win.format = mode; */
  1729. /* Unless subcapture is enabled,
  1730. * center the image window and downsample
  1731. * if possible to increase the field of view */
  1732. /* NOTE: OV518(+) and OV519 does downsampling on its own */
  1733. win.width = width;
  1734. win.height = height;
  1735. if (width == sd->maxwidth)
  1736. win.quarter = 0;
  1737. else
  1738. win.quarter = 1;
  1739. /* Center it */
  1740. win.x = (win.width - width) / 2;
  1741. win.y = (win.height - height) / 2;
  1742. /* Clock is determined by OV519 frame rate code */
  1743. win.clockdiv = sd->clockdiv;
  1744. PDEBUG(D_CONF, "Setting clock divider to %d", win.clockdiv);
  1745. return set_ov_sensor_window(sd, &win);
  1746. }
  1747. /* -- start the camera -- */
  1748. static void sd_start(struct gspca_dev *gspca_dev)
  1749. {
  1750. struct sd *sd = (struct sd *) gspca_dev;
  1751. int ret;
  1752. ret = ov519_mode_init_regs(sd, gspca_dev->width, gspca_dev->height);
  1753. if (ret < 0)
  1754. goto out;
  1755. ret = ov_sensor_mode_setup(sd, gspca_dev->width, gspca_dev->height);
  1756. if (ret < 0)
  1757. goto out;
  1758. ret = ov51x_restart((struct sd *) gspca_dev);
  1759. if (ret < 0)
  1760. goto out;
  1761. PDEBUG(D_STREAM, "camera started alt: 0x%02x", gspca_dev->alt);
  1762. ov51x_led_control(sd, 1);
  1763. return;
  1764. out:
  1765. PDEBUG(D_ERR, "camera start error:%d", ret);
  1766. }
  1767. static void sd_stopN(struct gspca_dev *gspca_dev)
  1768. {
  1769. ov51x_stop((struct sd *) gspca_dev);
  1770. ov51x_led_control((struct sd *) gspca_dev, 0);
  1771. }
  1772. static void sd_stop0(struct gspca_dev *gspca_dev)
  1773. {
  1774. }
  1775. static void sd_close(struct gspca_dev *gspca_dev)
  1776. {
  1777. }
  1778. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  1779. struct gspca_frame *frame, /* target */
  1780. __u8 *data, /* isoc packet */
  1781. int len) /* iso packet length */
  1782. {
  1783. /* Header of ov519 is 16 bytes:
  1784. * Byte Value Description
  1785. * 0 0xff magic
  1786. * 1 0xff magic
  1787. * 2 0xff magic
  1788. * 3 0xXX 0x50 = SOF, 0x51 = EOF
  1789. * 9 0xXX 0x01 initial frame without data,
  1790. * 0x00 standard frame with image
  1791. * 14 Lo in EOF: length of image data / 8
  1792. * 15 Hi
  1793. */
  1794. if (data[0] == 0xff && data[1] == 0xff && data[2] == 0xff) {
  1795. switch (data[3]) {
  1796. case 0x50: /* start of frame */
  1797. #define HDRSZ 16
  1798. data += HDRSZ;
  1799. len -= HDRSZ;
  1800. #undef HDRSZ
  1801. if (data[0] == 0xff || data[1] == 0xd8)
  1802. gspca_frame_add(gspca_dev, FIRST_PACKET, frame,
  1803. data, len);
  1804. else
  1805. gspca_dev->last_packet_type = DISCARD_PACKET;
  1806. return;
  1807. case 0x51: /* end of frame */
  1808. if (data[9] != 0)
  1809. gspca_dev->last_packet_type = DISCARD_PACKET;
  1810. gspca_frame_add(gspca_dev, LAST_PACKET, frame,
  1811. data, 0);
  1812. return;
  1813. }
  1814. }
  1815. /* intermediate packet */
  1816. gspca_frame_add(gspca_dev, INTER_PACKET, frame,
  1817. data, len);
  1818. }
  1819. /* -- management routines -- */
  1820. static void setbrightness(struct gspca_dev *gspca_dev)
  1821. {
  1822. struct sd *sd = (struct sd *) gspca_dev;
  1823. int val;
  1824. /* int was_streaming; */
  1825. val = sd->brightness;
  1826. PDEBUG(D_CONF, "brightness:%d", val);
  1827. /* was_streaming = gspca_dev->streaming;
  1828. * if (was_streaming)
  1829. * ov51x_stop(sd); */
  1830. switch (sd->sensor) {
  1831. case SEN_OV8610:
  1832. case SEN_OV7610:
  1833. case SEN_OV76BE:
  1834. case SEN_OV6620:
  1835. case SEN_OV6630:
  1836. case SEN_OV7640:
  1837. i2c_w(sd, OV7610_REG_BRT, val);
  1838. break;
  1839. case SEN_OV7620:
  1840. /* 7620 doesn't like manual changes when in auto mode */
  1841. /*fixme
  1842. * if (!sd->auto_brt) */
  1843. i2c_w(sd, OV7610_REG_BRT, val);
  1844. break;
  1845. case SEN_OV7670:
  1846. /*jfm - from windblows
  1847. * i2c_w_mask(sd, OV7670_REG_COM8, 0, OV7670_COM8_AEC); */
  1848. i2c_w(sd, OV7670_REG_BRIGHT, ov7670_abs_to_sm(val));
  1849. break;
  1850. }
  1851. /* if (was_streaming)
  1852. * ov51x_restart(sd); */
  1853. }
  1854. static void setcontrast(struct gspca_dev *gspca_dev)
  1855. {
  1856. struct sd *sd = (struct sd *) gspca_dev;
  1857. int val;
  1858. /* int was_streaming; */
  1859. val = sd->contrast;
  1860. PDEBUG(D_CONF, "contrast:%d", val);
  1861. /* was_streaming = gspca_dev->streaming;
  1862. if (was_streaming)
  1863. ov51x_stop(sd); */
  1864. switch (sd->sensor) {
  1865. case SEN_OV7610:
  1866. case SEN_OV6620:
  1867. i2c_w(sd, OV7610_REG_CNT, val);
  1868. break;
  1869. case SEN_OV6630:
  1870. i2c_w_mask(sd, OV7610_REG_CNT, val >> 4, 0x0f);
  1871. case SEN_OV8610: {
  1872. static const __u8 ctab[] = {
  1873. 0x03, 0x09, 0x0b, 0x0f, 0x53, 0x6f, 0x35, 0x7f
  1874. };
  1875. /* Use Y gamma control instead. Bit 0 enables it. */
  1876. i2c_w(sd, 0x64, ctab[val >> 5]);
  1877. break;
  1878. }
  1879. case SEN_OV7620: {
  1880. static const __u8 ctab[] = {
  1881. 0x01, 0x05, 0x09, 0x11, 0x15, 0x35, 0x37, 0x57,
  1882. 0x5b, 0xa5, 0xa7, 0xc7, 0xc9, 0xcf, 0xef, 0xff
  1883. };
  1884. /* Use Y gamma control instead. Bit 0 enables it. */
  1885. i2c_w(sd, 0x64, ctab[val >> 4]);
  1886. break;
  1887. }
  1888. case SEN_OV7640:
  1889. /* Use gain control instead. */
  1890. i2c_w(sd, OV7610_REG_GAIN, val >> 2);
  1891. break;
  1892. case SEN_OV7670:
  1893. /* check that this isn't just the same as ov7610 */
  1894. i2c_w(sd, OV7670_REG_CONTRAS, val >> 1);
  1895. break;
  1896. }
  1897. /* if (was_streaming)
  1898. ov51x_restart(sd); */
  1899. }
  1900. static void setcolors(struct gspca_dev *gspca_dev)
  1901. {
  1902. struct sd *sd = (struct sd *) gspca_dev;
  1903. int val;
  1904. /* int was_streaming; */
  1905. val = sd->colors;
  1906. PDEBUG(D_CONF, "saturation:%d", val);
  1907. /* was_streaming = gspca_dev->streaming;
  1908. if (was_streaming)
  1909. ov51x_stop(sd); */
  1910. switch (sd->sensor) {
  1911. case SEN_OV8610:
  1912. case SEN_OV7610:
  1913. case SEN_OV76BE:
  1914. case SEN_OV6620:
  1915. case SEN_OV6630:
  1916. i2c_w(sd, OV7610_REG_SAT, val);
  1917. break;
  1918. case SEN_OV7620:
  1919. /* Use UV gamma control instead. Bits 0 & 7 are reserved. */
  1920. /* rc = ov_i2c_write(sd->dev, 0x62, (val >> 9) & 0x7e);
  1921. if (rc < 0)
  1922. goto out; */
  1923. i2c_w(sd, OV7610_REG_SAT, val);
  1924. break;
  1925. case SEN_OV7640:
  1926. i2c_w(sd, OV7610_REG_SAT, val & 0xf0);
  1927. break;
  1928. case SEN_OV7670:
  1929. /* supported later once I work out how to do it
  1930. * transparently fail now! */
  1931. /* set REG_COM13 values for UV sat auto mode */
  1932. break;
  1933. }
  1934. /* if (was_streaming)
  1935. ov51x_restart(sd); */
  1936. }
  1937. static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
  1938. {
  1939. struct sd *sd = (struct sd *) gspca_dev;
  1940. sd->brightness = val;
  1941. setbrightness(gspca_dev);
  1942. return 0;
  1943. }
  1944. static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
  1945. {
  1946. struct sd *sd = (struct sd *) gspca_dev;
  1947. *val = sd->brightness;
  1948. return 0;
  1949. }
  1950. static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
  1951. {
  1952. struct sd *sd = (struct sd *) gspca_dev;
  1953. sd->contrast = val;
  1954. setcontrast(gspca_dev);
  1955. return 0;
  1956. }
  1957. static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
  1958. {
  1959. struct sd *sd = (struct sd *) gspca_dev;
  1960. *val = sd->contrast;
  1961. return 0;
  1962. }
  1963. static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val)
  1964. {
  1965. struct sd *sd = (struct sd *) gspca_dev;
  1966. sd->colors = val;
  1967. setcolors(gspca_dev);
  1968. return 0;
  1969. }
  1970. static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val)
  1971. {
  1972. struct sd *sd = (struct sd *) gspca_dev;
  1973. *val = sd->colors;
  1974. return 0;
  1975. }
  1976. static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val)
  1977. {
  1978. struct sd *sd = (struct sd *) gspca_dev;
  1979. sd->hflip = val;
  1980. sethflip(sd);
  1981. return 0;
  1982. }
  1983. static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val)
  1984. {
  1985. struct sd *sd = (struct sd *) gspca_dev;
  1986. *val = sd->hflip;
  1987. return 0;
  1988. }
  1989. static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val)
  1990. {
  1991. struct sd *sd = (struct sd *) gspca_dev;
  1992. sd->vflip = val;
  1993. setvflip(sd);
  1994. return 0;
  1995. }
  1996. static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val)
  1997. {
  1998. struct sd *sd = (struct sd *) gspca_dev;
  1999. *val = sd->vflip;
  2000. return 0;
  2001. }
  2002. /* sub-driver description */
  2003. static const struct sd_desc sd_desc = {
  2004. .name = MODULE_NAME,
  2005. .ctrls = sd_ctrls,
  2006. .nctrls = ARRAY_SIZE(sd_ctrls),
  2007. .config = sd_config,
  2008. .open = sd_open,
  2009. .start = sd_start,
  2010. .stopN = sd_stopN,
  2011. .stop0 = sd_stop0,
  2012. .close = sd_close,
  2013. .pkt_scan = sd_pkt_scan,
  2014. };
  2015. /* -- module initialisation -- */
  2016. static const __devinitdata struct usb_device_id device_table[] = {
  2017. {USB_DEVICE(0x041e, 0x4052)},
  2018. {USB_DEVICE(0x041e, 0x405f)},
  2019. {USB_DEVICE(0x041e, 0x4060)},
  2020. {USB_DEVICE(0x041e, 0x4061)},
  2021. {USB_DEVICE(0x041e, 0x4064)},
  2022. {USB_DEVICE(0x041e, 0x4068)},
  2023. {USB_DEVICE(0x045e, 0x028c)},
  2024. {USB_DEVICE(0x054c, 0x0154)},
  2025. {USB_DEVICE(0x054c, 0x0155)},
  2026. {USB_DEVICE(0x05a9, 0x0519)},
  2027. {USB_DEVICE(0x05a9, 0x0530)},
  2028. {USB_DEVICE(0x05a9, 0x4519)},
  2029. {USB_DEVICE(0x05a9, 0x8519)},
  2030. {}
  2031. };
  2032. #undef DVNAME
  2033. MODULE_DEVICE_TABLE(usb, device_table);
  2034. /* -- device connect -- */
  2035. static int sd_probe(struct usb_interface *intf,
  2036. const struct usb_device_id *id)
  2037. {
  2038. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  2039. THIS_MODULE);
  2040. }
  2041. static struct usb_driver sd_driver = {
  2042. .name = MODULE_NAME,
  2043. .id_table = device_table,
  2044. .probe = sd_probe,
  2045. .disconnect = gspca_disconnect,
  2046. };
  2047. /* -- module insert / remove -- */
  2048. static int __init sd_mod_init(void)
  2049. {
  2050. if (usb_register(&sd_driver) < 0)
  2051. return -1;
  2052. PDEBUG(D_PROBE, "registered");
  2053. return 0;
  2054. }
  2055. static void __exit sd_mod_exit(void)
  2056. {
  2057. usb_deregister(&sd_driver);
  2058. PDEBUG(D_PROBE, "deregistered");
  2059. }
  2060. module_init(sd_mod_init);
  2061. module_exit(sd_mod_exit);
  2062. module_param(frame_rate, int, 0644);
  2063. MODULE_PARM_DESC(frame_rate, "Frame rate (5, 10, 15, 20 or 30 fps)");