ov7670.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741
  1. /*
  2. * A V4L2 driver for OmniVision OV7670 cameras.
  3. *
  4. * Copyright 2006 One Laptop Per Child Association, Inc. Written
  5. * by Jonathan Corbet with substantial inspiration from Mark
  6. * McClelland's ovcamchip code.
  7. *
  8. * Copyright 2006-7 Jonathan Corbet <corbet@lwn.net>
  9. *
  10. * This file may be distributed under the terms of the GNU General
  11. * Public License, version 2.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/i2c.h>
  18. #include <linux/delay.h>
  19. #include <linux/videodev2.h>
  20. #include <linux/gpio.h>
  21. #include <linux/gpio/consumer.h>
  22. #include <media/v4l2-device.h>
  23. #include <media/v4l2-ctrls.h>
  24. #include <media/v4l2-mediabus.h>
  25. #include <media/v4l2-image-sizes.h>
  26. #include <media/i2c/ov7670.h>
  27. MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
  28. MODULE_DESCRIPTION("A low-level driver for OmniVision ov7670 sensors");
  29. MODULE_LICENSE("GPL");
  30. static bool debug;
  31. module_param(debug, bool, 0644);
  32. MODULE_PARM_DESC(debug, "Debug level (0-1)");
  33. /*
  34. * The 7670 sits on i2c with ID 0x42
  35. */
  36. #define OV7670_I2C_ADDR 0x42
  37. #define PLL_FACTOR 4
  38. /* Registers */
  39. #define REG_GAIN 0x00 /* Gain lower 8 bits (rest in vref) */
  40. #define REG_BLUE 0x01 /* blue gain */
  41. #define REG_RED 0x02 /* red gain */
  42. #define REG_VREF 0x03 /* Pieces of GAIN, VSTART, VSTOP */
  43. #define REG_COM1 0x04 /* Control 1 */
  44. #define COM1_CCIR656 0x40 /* CCIR656 enable */
  45. #define REG_BAVE 0x05 /* U/B Average level */
  46. #define REG_GbAVE 0x06 /* Y/Gb Average level */
  47. #define REG_AECHH 0x07 /* AEC MS 5 bits */
  48. #define REG_RAVE 0x08 /* V/R Average level */
  49. #define REG_COM2 0x09 /* Control 2 */
  50. #define COM2_SSLEEP 0x10 /* Soft sleep mode */
  51. #define REG_PID 0x0a /* Product ID MSB */
  52. #define REG_VER 0x0b /* Product ID LSB */
  53. #define REG_COM3 0x0c /* Control 3 */
  54. #define COM3_SWAP 0x40 /* Byte swap */
  55. #define COM3_SCALEEN 0x08 /* Enable scaling */
  56. #define COM3_DCWEN 0x04 /* Enable downsamp/crop/window */
  57. #define REG_COM4 0x0d /* Control 4 */
  58. #define REG_COM5 0x0e /* All "reserved" */
  59. #define REG_COM6 0x0f /* Control 6 */
  60. #define REG_AECH 0x10 /* More bits of AEC value */
  61. #define REG_CLKRC 0x11 /* Clocl control */
  62. #define CLK_EXT 0x40 /* Use external clock directly */
  63. #define CLK_SCALE 0x3f /* Mask for internal clock scale */
  64. #define REG_COM7 0x12 /* Control 7 */
  65. #define COM7_RESET 0x80 /* Register reset */
  66. #define COM7_FMT_MASK 0x38
  67. #define COM7_FMT_VGA 0x00
  68. #define COM7_FMT_CIF 0x20 /* CIF format */
  69. #define COM7_FMT_QVGA 0x10 /* QVGA format */
  70. #define COM7_FMT_QCIF 0x08 /* QCIF format */
  71. #define COM7_RGB 0x04 /* bits 0 and 2 - RGB format */
  72. #define COM7_YUV 0x00 /* YUV */
  73. #define COM7_BAYER 0x01 /* Bayer format */
  74. #define COM7_PBAYER 0x05 /* "Processed bayer" */
  75. #define REG_COM8 0x13 /* Control 8 */
  76. #define COM8_FASTAEC 0x80 /* Enable fast AGC/AEC */
  77. #define COM8_AECSTEP 0x40 /* Unlimited AEC step size */
  78. #define COM8_BFILT 0x20 /* Band filter enable */
  79. #define COM8_AGC 0x04 /* Auto gain enable */
  80. #define COM8_AWB 0x02 /* White balance enable */
  81. #define COM8_AEC 0x01 /* Auto exposure enable */
  82. #define REG_COM9 0x14 /* Control 9 - gain ceiling */
  83. #define REG_COM10 0x15 /* Control 10 */
  84. #define COM10_HSYNC 0x40 /* HSYNC instead of HREF */
  85. #define COM10_PCLK_HB 0x20 /* Suppress PCLK on horiz blank */
  86. #define COM10_HREF_REV 0x08 /* Reverse HREF */
  87. #define COM10_VS_LEAD 0x04 /* VSYNC on clock leading edge */
  88. #define COM10_VS_NEG 0x02 /* VSYNC negative */
  89. #define COM10_HS_NEG 0x01 /* HSYNC negative */
  90. #define REG_HSTART 0x17 /* Horiz start high bits */
  91. #define REG_HSTOP 0x18 /* Horiz stop high bits */
  92. #define REG_VSTART 0x19 /* Vert start high bits */
  93. #define REG_VSTOP 0x1a /* Vert stop high bits */
  94. #define REG_PSHFT 0x1b /* Pixel delay after HREF */
  95. #define REG_MIDH 0x1c /* Manuf. ID high */
  96. #define REG_MIDL 0x1d /* Manuf. ID low */
  97. #define REG_MVFP 0x1e /* Mirror / vflip */
  98. #define MVFP_MIRROR 0x20 /* Mirror image */
  99. #define MVFP_FLIP 0x10 /* Vertical flip */
  100. #define REG_AEW 0x24 /* AGC upper limit */
  101. #define REG_AEB 0x25 /* AGC lower limit */
  102. #define REG_VPT 0x26 /* AGC/AEC fast mode op region */
  103. #define REG_HSYST 0x30 /* HSYNC rising edge delay */
  104. #define REG_HSYEN 0x31 /* HSYNC falling edge delay */
  105. #define REG_HREF 0x32 /* HREF pieces */
  106. #define REG_TSLB 0x3a /* lots of stuff */
  107. #define TSLB_YLAST 0x04 /* UYVY or VYUY - see com13 */
  108. #define REG_COM11 0x3b /* Control 11 */
  109. #define COM11_NIGHT 0x80 /* NIght mode enable */
  110. #define COM11_NMFR 0x60 /* Two bit NM frame rate */
  111. #define COM11_HZAUTO 0x10 /* Auto detect 50/60 Hz */
  112. #define COM11_50HZ 0x08 /* Manual 50Hz select */
  113. #define COM11_EXP 0x02
  114. #define REG_COM12 0x3c /* Control 12 */
  115. #define COM12_HREF 0x80 /* HREF always */
  116. #define REG_COM13 0x3d /* Control 13 */
  117. #define COM13_GAMMA 0x80 /* Gamma enable */
  118. #define COM13_UVSAT 0x40 /* UV saturation auto adjustment */
  119. #define COM13_UVSWAP 0x01 /* V before U - w/TSLB */
  120. #define REG_COM14 0x3e /* Control 14 */
  121. #define COM14_DCWEN 0x10 /* DCW/PCLK-scale enable */
  122. #define REG_EDGE 0x3f /* Edge enhancement factor */
  123. #define REG_COM15 0x40 /* Control 15 */
  124. #define COM15_R10F0 0x00 /* Data range 10 to F0 */
  125. #define COM15_R01FE 0x80 /* 01 to FE */
  126. #define COM15_R00FF 0xc0 /* 00 to FF */
  127. #define COM15_RGB565 0x10 /* RGB565 output */
  128. #define COM15_RGB555 0x30 /* RGB555 output */
  129. #define REG_COM16 0x41 /* Control 16 */
  130. #define COM16_AWBGAIN 0x08 /* AWB gain enable */
  131. #define REG_COM17 0x42 /* Control 17 */
  132. #define COM17_AECWIN 0xc0 /* AEC window - must match COM4 */
  133. #define COM17_CBAR 0x08 /* DSP Color bar */
  134. /*
  135. * This matrix defines how the colors are generated, must be
  136. * tweaked to adjust hue and saturation.
  137. *
  138. * Order: v-red, v-green, v-blue, u-red, u-green, u-blue
  139. *
  140. * They are nine-bit signed quantities, with the sign bit
  141. * stored in 0x58. Sign for v-red is bit 0, and up from there.
  142. */
  143. #define REG_CMATRIX_BASE 0x4f
  144. #define CMATRIX_LEN 6
  145. #define REG_CMATRIX_SIGN 0x58
  146. #define REG_BRIGHT 0x55 /* Brightness */
  147. #define REG_CONTRAS 0x56 /* Contrast control */
  148. #define REG_GFIX 0x69 /* Fix gain control */
  149. #define REG_DBLV 0x6b /* PLL control an debugging */
  150. #define DBLV_BYPASS 0x00 /* Bypass PLL */
  151. #define DBLV_X4 0x01 /* clock x4 */
  152. #define DBLV_X6 0x10 /* clock x6 */
  153. #define DBLV_X8 0x11 /* clock x8 */
  154. #define REG_REG76 0x76 /* OV's name */
  155. #define R76_BLKPCOR 0x80 /* Black pixel correction enable */
  156. #define R76_WHTPCOR 0x40 /* White pixel correction enable */
  157. #define REG_RGB444 0x8c /* RGB 444 control */
  158. #define R444_ENABLE 0x02 /* Turn on RGB444, overrides 5x5 */
  159. #define R444_RGBX 0x01 /* Empty nibble at end */
  160. #define REG_HAECC1 0x9f /* Hist AEC/AGC control 1 */
  161. #define REG_HAECC2 0xa0 /* Hist AEC/AGC control 2 */
  162. #define REG_BD50MAX 0xa5 /* 50hz banding step limit */
  163. #define REG_HAECC3 0xa6 /* Hist AEC/AGC control 3 */
  164. #define REG_HAECC4 0xa7 /* Hist AEC/AGC control 4 */
  165. #define REG_HAECC5 0xa8 /* Hist AEC/AGC control 5 */
  166. #define REG_HAECC6 0xa9 /* Hist AEC/AGC control 6 */
  167. #define REG_HAECC7 0xaa /* Hist AEC/AGC control 7 */
  168. #define REG_BD60MAX 0xab /* 60hz banding step limit */
  169. enum ov7670_model {
  170. MODEL_OV7670 = 0,
  171. MODEL_OV7675,
  172. };
  173. struct ov7670_win_size {
  174. int width;
  175. int height;
  176. unsigned char com7_bit;
  177. int hstart; /* Start/stop values for the camera. Note */
  178. int hstop; /* that they do not always make complete */
  179. int vstart; /* sense to humans, but evidently the sensor */
  180. int vstop; /* will do the right thing... */
  181. struct regval_list *regs; /* Regs to tweak */
  182. };
  183. struct ov7670_devtype {
  184. /* formats supported for each model */
  185. struct ov7670_win_size *win_sizes;
  186. unsigned int n_win_sizes;
  187. /* callbacks for frame rate control */
  188. int (*set_framerate)(struct v4l2_subdev *, struct v4l2_fract *);
  189. void (*get_framerate)(struct v4l2_subdev *, struct v4l2_fract *);
  190. };
  191. /*
  192. * Information we maintain about a known sensor.
  193. */
  194. struct ov7670_format_struct; /* coming later */
  195. struct ov7670_info {
  196. struct v4l2_subdev sd;
  197. struct v4l2_ctrl_handler hdl;
  198. struct {
  199. /* gain cluster */
  200. struct v4l2_ctrl *auto_gain;
  201. struct v4l2_ctrl *gain;
  202. };
  203. struct {
  204. /* exposure cluster */
  205. struct v4l2_ctrl *auto_exposure;
  206. struct v4l2_ctrl *exposure;
  207. };
  208. struct {
  209. /* saturation/hue cluster */
  210. struct v4l2_ctrl *saturation;
  211. struct v4l2_ctrl *hue;
  212. };
  213. struct ov7670_format_struct *fmt; /* Current format */
  214. struct clk *clk;
  215. struct gpio_desc *resetb_gpio;
  216. struct gpio_desc *pwdn_gpio;
  217. int min_width; /* Filter out smaller sizes */
  218. int min_height; /* Filter out smaller sizes */
  219. int clock_speed; /* External clock speed (MHz) */
  220. u8 clkrc; /* Clock divider value */
  221. bool use_smbus; /* Use smbus I/O instead of I2C */
  222. bool pll_bypass;
  223. bool pclk_hb_disable;
  224. const struct ov7670_devtype *devtype; /* Device specifics */
  225. };
  226. static inline struct ov7670_info *to_state(struct v4l2_subdev *sd)
  227. {
  228. return container_of(sd, struct ov7670_info, sd);
  229. }
  230. static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
  231. {
  232. return &container_of(ctrl->handler, struct ov7670_info, hdl)->sd;
  233. }
  234. /*
  235. * The default register settings, as obtained from OmniVision. There
  236. * is really no making sense of most of these - lots of "reserved" values
  237. * and such.
  238. *
  239. * These settings give VGA YUYV.
  240. */
  241. struct regval_list {
  242. unsigned char reg_num;
  243. unsigned char value;
  244. };
  245. static struct regval_list ov7670_default_regs[] = {
  246. { REG_COM7, COM7_RESET },
  247. /*
  248. * Clock scale: 3 = 15fps
  249. * 2 = 20fps
  250. * 1 = 30fps
  251. */
  252. { REG_CLKRC, 0x1 }, /* OV: clock scale (30 fps) */
  253. { REG_TSLB, 0x04 }, /* OV */
  254. { REG_COM7, 0 }, /* VGA */
  255. /*
  256. * Set the hardware window. These values from OV don't entirely
  257. * make sense - hstop is less than hstart. But they work...
  258. */
  259. { REG_HSTART, 0x13 }, { REG_HSTOP, 0x01 },
  260. { REG_HREF, 0xb6 }, { REG_VSTART, 0x02 },
  261. { REG_VSTOP, 0x7a }, { REG_VREF, 0x0a },
  262. { REG_COM3, 0 }, { REG_COM14, 0 },
  263. /* Mystery scaling numbers */
  264. { 0x70, 0x3a }, { 0x71, 0x35 },
  265. { 0x72, 0x11 }, { 0x73, 0xf0 },
  266. { 0xa2, 0x02 }, { REG_COM10, 0x0 },
  267. /* Gamma curve values */
  268. { 0x7a, 0x20 }, { 0x7b, 0x10 },
  269. { 0x7c, 0x1e }, { 0x7d, 0x35 },
  270. { 0x7e, 0x5a }, { 0x7f, 0x69 },
  271. { 0x80, 0x76 }, { 0x81, 0x80 },
  272. { 0x82, 0x88 }, { 0x83, 0x8f },
  273. { 0x84, 0x96 }, { 0x85, 0xa3 },
  274. { 0x86, 0xaf }, { 0x87, 0xc4 },
  275. { 0x88, 0xd7 }, { 0x89, 0xe8 },
  276. /* AGC and AEC parameters. Note we start by disabling those features,
  277. then turn them only after tweaking the values. */
  278. { REG_COM8, COM8_FASTAEC | COM8_AECSTEP | COM8_BFILT },
  279. { REG_GAIN, 0 }, { REG_AECH, 0 },
  280. { REG_COM4, 0x40 }, /* magic reserved bit */
  281. { REG_COM9, 0x18 }, /* 4x gain + magic rsvd bit */
  282. { REG_BD50MAX, 0x05 }, { REG_BD60MAX, 0x07 },
  283. { REG_AEW, 0x95 }, { REG_AEB, 0x33 },
  284. { REG_VPT, 0xe3 }, { REG_HAECC1, 0x78 },
  285. { REG_HAECC2, 0x68 }, { 0xa1, 0x03 }, /* magic */
  286. { REG_HAECC3, 0xd8 }, { REG_HAECC4, 0xd8 },
  287. { REG_HAECC5, 0xf0 }, { REG_HAECC6, 0x90 },
  288. { REG_HAECC7, 0x94 },
  289. { REG_COM8, COM8_FASTAEC|COM8_AECSTEP|COM8_BFILT|COM8_AGC|COM8_AEC },
  290. /* Almost all of these are magic "reserved" values. */
  291. { REG_COM5, 0x61 }, { REG_COM6, 0x4b },
  292. { 0x16, 0x02 }, { REG_MVFP, 0x07 },
  293. { 0x21, 0x02 }, { 0x22, 0x91 },
  294. { 0x29, 0x07 }, { 0x33, 0x0b },
  295. { 0x35, 0x0b }, { 0x37, 0x1d },
  296. { 0x38, 0x71 }, { 0x39, 0x2a },
  297. { REG_COM12, 0x78 }, { 0x4d, 0x40 },
  298. { 0x4e, 0x20 }, { REG_GFIX, 0 },
  299. { 0x6b, 0x4a }, { 0x74, 0x10 },
  300. { 0x8d, 0x4f }, { 0x8e, 0 },
  301. { 0x8f, 0 }, { 0x90, 0 },
  302. { 0x91, 0 }, { 0x96, 0 },
  303. { 0x9a, 0 }, { 0xb0, 0x84 },
  304. { 0xb1, 0x0c }, { 0xb2, 0x0e },
  305. { 0xb3, 0x82 }, { 0xb8, 0x0a },
  306. /* More reserved magic, some of which tweaks white balance */
  307. { 0x43, 0x0a }, { 0x44, 0xf0 },
  308. { 0x45, 0x34 }, { 0x46, 0x58 },
  309. { 0x47, 0x28 }, { 0x48, 0x3a },
  310. { 0x59, 0x88 }, { 0x5a, 0x88 },
  311. { 0x5b, 0x44 }, { 0x5c, 0x67 },
  312. { 0x5d, 0x49 }, { 0x5e, 0x0e },
  313. { 0x6c, 0x0a }, { 0x6d, 0x55 },
  314. { 0x6e, 0x11 }, { 0x6f, 0x9f }, /* "9e for advance AWB" */
  315. { 0x6a, 0x40 }, { REG_BLUE, 0x40 },
  316. { REG_RED, 0x60 },
  317. { REG_COM8, COM8_FASTAEC|COM8_AECSTEP|COM8_BFILT|COM8_AGC|COM8_AEC|COM8_AWB },
  318. /* Matrix coefficients */
  319. { 0x4f, 0x80 }, { 0x50, 0x80 },
  320. { 0x51, 0 }, { 0x52, 0x22 },
  321. { 0x53, 0x5e }, { 0x54, 0x80 },
  322. { 0x58, 0x9e },
  323. { REG_COM16, COM16_AWBGAIN }, { REG_EDGE, 0 },
  324. { 0x75, 0x05 }, { 0x76, 0xe1 },
  325. { 0x4c, 0 }, { 0x77, 0x01 },
  326. { REG_COM13, 0xc3 }, { 0x4b, 0x09 },
  327. { 0xc9, 0x60 }, { REG_COM16, 0x38 },
  328. { 0x56, 0x40 },
  329. { 0x34, 0x11 }, { REG_COM11, COM11_EXP|COM11_HZAUTO },
  330. { 0xa4, 0x88 }, { 0x96, 0 },
  331. { 0x97, 0x30 }, { 0x98, 0x20 },
  332. { 0x99, 0x30 }, { 0x9a, 0x84 },
  333. { 0x9b, 0x29 }, { 0x9c, 0x03 },
  334. { 0x9d, 0x4c }, { 0x9e, 0x3f },
  335. { 0x78, 0x04 },
  336. /* Extra-weird stuff. Some sort of multiplexor register */
  337. { 0x79, 0x01 }, { 0xc8, 0xf0 },
  338. { 0x79, 0x0f }, { 0xc8, 0x00 },
  339. { 0x79, 0x10 }, { 0xc8, 0x7e },
  340. { 0x79, 0x0a }, { 0xc8, 0x80 },
  341. { 0x79, 0x0b }, { 0xc8, 0x01 },
  342. { 0x79, 0x0c }, { 0xc8, 0x0f },
  343. { 0x79, 0x0d }, { 0xc8, 0x20 },
  344. { 0x79, 0x09 }, { 0xc8, 0x80 },
  345. { 0x79, 0x02 }, { 0xc8, 0xc0 },
  346. { 0x79, 0x03 }, { 0xc8, 0x40 },
  347. { 0x79, 0x05 }, { 0xc8, 0x30 },
  348. { 0x79, 0x26 },
  349. { 0xff, 0xff }, /* END MARKER */
  350. };
  351. /*
  352. * Here we'll try to encapsulate the changes for just the output
  353. * video format.
  354. *
  355. * RGB656 and YUV422 come from OV; RGB444 is homebrewed.
  356. *
  357. * IMPORTANT RULE: the first entry must be for COM7, see ov7670_s_fmt for why.
  358. */
  359. static struct regval_list ov7670_fmt_yuv422[] = {
  360. { REG_COM7, 0x0 }, /* Selects YUV mode */
  361. { REG_RGB444, 0 }, /* No RGB444 please */
  362. { REG_COM1, 0 }, /* CCIR601 */
  363. { REG_COM15, COM15_R00FF },
  364. { REG_COM9, 0x48 }, /* 32x gain ceiling; 0x8 is reserved bit */
  365. { 0x4f, 0x80 }, /* "matrix coefficient 1" */
  366. { 0x50, 0x80 }, /* "matrix coefficient 2" */
  367. { 0x51, 0 }, /* vb */
  368. { 0x52, 0x22 }, /* "matrix coefficient 4" */
  369. { 0x53, 0x5e }, /* "matrix coefficient 5" */
  370. { 0x54, 0x80 }, /* "matrix coefficient 6" */
  371. { REG_COM13, COM13_GAMMA|COM13_UVSAT },
  372. { 0xff, 0xff },
  373. };
  374. static struct regval_list ov7670_fmt_rgb565[] = {
  375. { REG_COM7, COM7_RGB }, /* Selects RGB mode */
  376. { REG_RGB444, 0 }, /* No RGB444 please */
  377. { REG_COM1, 0x0 }, /* CCIR601 */
  378. { REG_COM15, COM15_RGB565 },
  379. { REG_COM9, 0x38 }, /* 16x gain ceiling; 0x8 is reserved bit */
  380. { 0x4f, 0xb3 }, /* "matrix coefficient 1" */
  381. { 0x50, 0xb3 }, /* "matrix coefficient 2" */
  382. { 0x51, 0 }, /* vb */
  383. { 0x52, 0x3d }, /* "matrix coefficient 4" */
  384. { 0x53, 0xa7 }, /* "matrix coefficient 5" */
  385. { 0x54, 0xe4 }, /* "matrix coefficient 6" */
  386. { REG_COM13, COM13_GAMMA|COM13_UVSAT },
  387. { 0xff, 0xff },
  388. };
  389. static struct regval_list ov7670_fmt_rgb444[] = {
  390. { REG_COM7, COM7_RGB }, /* Selects RGB mode */
  391. { REG_RGB444, R444_ENABLE }, /* Enable xxxxrrrr ggggbbbb */
  392. { REG_COM1, 0x0 }, /* CCIR601 */
  393. { REG_COM15, COM15_R01FE|COM15_RGB565 }, /* Data range needed? */
  394. { REG_COM9, 0x38 }, /* 16x gain ceiling; 0x8 is reserved bit */
  395. { 0x4f, 0xb3 }, /* "matrix coefficient 1" */
  396. { 0x50, 0xb3 }, /* "matrix coefficient 2" */
  397. { 0x51, 0 }, /* vb */
  398. { 0x52, 0x3d }, /* "matrix coefficient 4" */
  399. { 0x53, 0xa7 }, /* "matrix coefficient 5" */
  400. { 0x54, 0xe4 }, /* "matrix coefficient 6" */
  401. { REG_COM13, COM13_GAMMA|COM13_UVSAT|0x2 }, /* Magic rsvd bit */
  402. { 0xff, 0xff },
  403. };
  404. static struct regval_list ov7670_fmt_raw[] = {
  405. { REG_COM7, COM7_BAYER },
  406. { REG_COM13, 0x08 }, /* No gamma, magic rsvd bit */
  407. { REG_COM16, 0x3d }, /* Edge enhancement, denoise */
  408. { REG_REG76, 0xe1 }, /* Pix correction, magic rsvd */
  409. { 0xff, 0xff },
  410. };
  411. /*
  412. * Low-level register I/O.
  413. *
  414. * Note that there are two versions of these. On the XO 1, the
  415. * i2c controller only does SMBUS, so that's what we use. The
  416. * ov7670 is not really an SMBUS device, though, so the communication
  417. * is not always entirely reliable.
  418. */
  419. static int ov7670_read_smbus(struct v4l2_subdev *sd, unsigned char reg,
  420. unsigned char *value)
  421. {
  422. struct i2c_client *client = v4l2_get_subdevdata(sd);
  423. int ret;
  424. ret = i2c_smbus_read_byte_data(client, reg);
  425. if (ret >= 0) {
  426. *value = (unsigned char)ret;
  427. ret = 0;
  428. }
  429. return ret;
  430. }
  431. static int ov7670_write_smbus(struct v4l2_subdev *sd, unsigned char reg,
  432. unsigned char value)
  433. {
  434. struct i2c_client *client = v4l2_get_subdevdata(sd);
  435. int ret = i2c_smbus_write_byte_data(client, reg, value);
  436. if (reg == REG_COM7 && (value & COM7_RESET))
  437. msleep(5); /* Wait for reset to run */
  438. return ret;
  439. }
  440. /*
  441. * On most platforms, we'd rather do straight i2c I/O.
  442. */
  443. static int ov7670_read_i2c(struct v4l2_subdev *sd, unsigned char reg,
  444. unsigned char *value)
  445. {
  446. struct i2c_client *client = v4l2_get_subdevdata(sd);
  447. u8 data = reg;
  448. struct i2c_msg msg;
  449. int ret;
  450. /*
  451. * Send out the register address...
  452. */
  453. msg.addr = client->addr;
  454. msg.flags = 0;
  455. msg.len = 1;
  456. msg.buf = &data;
  457. ret = i2c_transfer(client->adapter, &msg, 1);
  458. if (ret < 0) {
  459. printk(KERN_ERR "Error %d on register write\n", ret);
  460. return ret;
  461. }
  462. /*
  463. * ...then read back the result.
  464. */
  465. msg.flags = I2C_M_RD;
  466. ret = i2c_transfer(client->adapter, &msg, 1);
  467. if (ret >= 0) {
  468. *value = data;
  469. ret = 0;
  470. }
  471. return ret;
  472. }
  473. static int ov7670_write_i2c(struct v4l2_subdev *sd, unsigned char reg,
  474. unsigned char value)
  475. {
  476. struct i2c_client *client = v4l2_get_subdevdata(sd);
  477. struct i2c_msg msg;
  478. unsigned char data[2] = { reg, value };
  479. int ret;
  480. msg.addr = client->addr;
  481. msg.flags = 0;
  482. msg.len = 2;
  483. msg.buf = data;
  484. ret = i2c_transfer(client->adapter, &msg, 1);
  485. if (ret > 0)
  486. ret = 0;
  487. if (reg == REG_COM7 && (value & COM7_RESET))
  488. msleep(5); /* Wait for reset to run */
  489. return ret;
  490. }
  491. static int ov7670_read(struct v4l2_subdev *sd, unsigned char reg,
  492. unsigned char *value)
  493. {
  494. struct ov7670_info *info = to_state(sd);
  495. if (info->use_smbus)
  496. return ov7670_read_smbus(sd, reg, value);
  497. else
  498. return ov7670_read_i2c(sd, reg, value);
  499. }
  500. static int ov7670_write(struct v4l2_subdev *sd, unsigned char reg,
  501. unsigned char value)
  502. {
  503. struct ov7670_info *info = to_state(sd);
  504. if (info->use_smbus)
  505. return ov7670_write_smbus(sd, reg, value);
  506. else
  507. return ov7670_write_i2c(sd, reg, value);
  508. }
  509. /*
  510. * Write a list of register settings; ff/ff stops the process.
  511. */
  512. static int ov7670_write_array(struct v4l2_subdev *sd, struct regval_list *vals)
  513. {
  514. while (vals->reg_num != 0xff || vals->value != 0xff) {
  515. int ret = ov7670_write(sd, vals->reg_num, vals->value);
  516. if (ret < 0)
  517. return ret;
  518. vals++;
  519. }
  520. return 0;
  521. }
  522. /*
  523. * Stuff that knows about the sensor.
  524. */
  525. static int ov7670_reset(struct v4l2_subdev *sd, u32 val)
  526. {
  527. ov7670_write(sd, REG_COM7, COM7_RESET);
  528. msleep(1);
  529. return 0;
  530. }
  531. static int ov7670_init(struct v4l2_subdev *sd, u32 val)
  532. {
  533. return ov7670_write_array(sd, ov7670_default_regs);
  534. }
  535. static int ov7670_detect(struct v4l2_subdev *sd)
  536. {
  537. unsigned char v;
  538. int ret;
  539. ret = ov7670_init(sd, 0);
  540. if (ret < 0)
  541. return ret;
  542. ret = ov7670_read(sd, REG_MIDH, &v);
  543. if (ret < 0)
  544. return ret;
  545. if (v != 0x7f) /* OV manuf. id. */
  546. return -ENODEV;
  547. ret = ov7670_read(sd, REG_MIDL, &v);
  548. if (ret < 0)
  549. return ret;
  550. if (v != 0xa2)
  551. return -ENODEV;
  552. /*
  553. * OK, we know we have an OmniVision chip...but which one?
  554. */
  555. ret = ov7670_read(sd, REG_PID, &v);
  556. if (ret < 0)
  557. return ret;
  558. if (v != 0x76) /* PID + VER = 0x76 / 0x73 */
  559. return -ENODEV;
  560. ret = ov7670_read(sd, REG_VER, &v);
  561. if (ret < 0)
  562. return ret;
  563. if (v != 0x73) /* PID + VER = 0x76 / 0x73 */
  564. return -ENODEV;
  565. return 0;
  566. }
  567. /*
  568. * Store information about the video data format. The color matrix
  569. * is deeply tied into the format, so keep the relevant values here.
  570. * The magic matrix numbers come from OmniVision.
  571. */
  572. static struct ov7670_format_struct {
  573. u32 mbus_code;
  574. enum v4l2_colorspace colorspace;
  575. struct regval_list *regs;
  576. int cmatrix[CMATRIX_LEN];
  577. } ov7670_formats[] = {
  578. {
  579. .mbus_code = MEDIA_BUS_FMT_YUYV8_2X8,
  580. .colorspace = V4L2_COLORSPACE_SRGB,
  581. .regs = ov7670_fmt_yuv422,
  582. .cmatrix = { 128, -128, 0, -34, -94, 128 },
  583. },
  584. {
  585. .mbus_code = MEDIA_BUS_FMT_RGB444_2X8_PADHI_LE,
  586. .colorspace = V4L2_COLORSPACE_SRGB,
  587. .regs = ov7670_fmt_rgb444,
  588. .cmatrix = { 179, -179, 0, -61, -176, 228 },
  589. },
  590. {
  591. .mbus_code = MEDIA_BUS_FMT_RGB565_2X8_LE,
  592. .colorspace = V4L2_COLORSPACE_SRGB,
  593. .regs = ov7670_fmt_rgb565,
  594. .cmatrix = { 179, -179, 0, -61, -176, 228 },
  595. },
  596. {
  597. .mbus_code = MEDIA_BUS_FMT_SBGGR8_1X8,
  598. .colorspace = V4L2_COLORSPACE_SRGB,
  599. .regs = ov7670_fmt_raw,
  600. .cmatrix = { 0, 0, 0, 0, 0, 0 },
  601. },
  602. };
  603. #define N_OV7670_FMTS ARRAY_SIZE(ov7670_formats)
  604. /*
  605. * Then there is the issue of window sizes. Try to capture the info here.
  606. */
  607. /*
  608. * QCIF mode is done (by OV) in a very strange way - it actually looks like
  609. * VGA with weird scaling options - they do *not* use the canned QCIF mode
  610. * which is allegedly provided by the sensor. So here's the weird register
  611. * settings.
  612. */
  613. static struct regval_list ov7670_qcif_regs[] = {
  614. { REG_COM3, COM3_SCALEEN|COM3_DCWEN },
  615. { REG_COM3, COM3_DCWEN },
  616. { REG_COM14, COM14_DCWEN | 0x01},
  617. { 0x73, 0xf1 },
  618. { 0xa2, 0x52 },
  619. { 0x7b, 0x1c },
  620. { 0x7c, 0x28 },
  621. { 0x7d, 0x3c },
  622. { 0x7f, 0x69 },
  623. { REG_COM9, 0x38 },
  624. { 0xa1, 0x0b },
  625. { 0x74, 0x19 },
  626. { 0x9a, 0x80 },
  627. { 0x43, 0x14 },
  628. { REG_COM13, 0xc0 },
  629. { 0xff, 0xff },
  630. };
  631. static struct ov7670_win_size ov7670_win_sizes[] = {
  632. /* VGA */
  633. {
  634. .width = VGA_WIDTH,
  635. .height = VGA_HEIGHT,
  636. .com7_bit = COM7_FMT_VGA,
  637. .hstart = 158, /* These values from */
  638. .hstop = 14, /* Omnivision */
  639. .vstart = 10,
  640. .vstop = 490,
  641. .regs = NULL,
  642. },
  643. /* CIF */
  644. {
  645. .width = CIF_WIDTH,
  646. .height = CIF_HEIGHT,
  647. .com7_bit = COM7_FMT_CIF,
  648. .hstart = 170, /* Empirically determined */
  649. .hstop = 90,
  650. .vstart = 14,
  651. .vstop = 494,
  652. .regs = NULL,
  653. },
  654. /* QVGA */
  655. {
  656. .width = QVGA_WIDTH,
  657. .height = QVGA_HEIGHT,
  658. .com7_bit = COM7_FMT_QVGA,
  659. .hstart = 168, /* Empirically determined */
  660. .hstop = 24,
  661. .vstart = 12,
  662. .vstop = 492,
  663. .regs = NULL,
  664. },
  665. /* QCIF */
  666. {
  667. .width = QCIF_WIDTH,
  668. .height = QCIF_HEIGHT,
  669. .com7_bit = COM7_FMT_VGA, /* see comment above */
  670. .hstart = 456, /* Empirically determined */
  671. .hstop = 24,
  672. .vstart = 14,
  673. .vstop = 494,
  674. .regs = ov7670_qcif_regs,
  675. }
  676. };
  677. static struct ov7670_win_size ov7675_win_sizes[] = {
  678. /*
  679. * Currently, only VGA is supported. Theoretically it could be possible
  680. * to support CIF, QVGA and QCIF too. Taking values for ov7670 as a
  681. * base and tweak them empirically could be required.
  682. */
  683. {
  684. .width = VGA_WIDTH,
  685. .height = VGA_HEIGHT,
  686. .com7_bit = COM7_FMT_VGA,
  687. .hstart = 158, /* These values from */
  688. .hstop = 14, /* Omnivision */
  689. .vstart = 14, /* Empirically determined */
  690. .vstop = 494,
  691. .regs = NULL,
  692. }
  693. };
  694. static void ov7675_get_framerate(struct v4l2_subdev *sd,
  695. struct v4l2_fract *tpf)
  696. {
  697. struct ov7670_info *info = to_state(sd);
  698. u32 clkrc = info->clkrc;
  699. int pll_factor;
  700. if (info->pll_bypass)
  701. pll_factor = 1;
  702. else
  703. pll_factor = PLL_FACTOR;
  704. clkrc++;
  705. if (info->fmt->mbus_code == MEDIA_BUS_FMT_SBGGR8_1X8)
  706. clkrc = (clkrc >> 1);
  707. tpf->numerator = 1;
  708. tpf->denominator = (5 * pll_factor * info->clock_speed) /
  709. (4 * clkrc);
  710. }
  711. static int ov7675_set_framerate(struct v4l2_subdev *sd,
  712. struct v4l2_fract *tpf)
  713. {
  714. struct ov7670_info *info = to_state(sd);
  715. u32 clkrc;
  716. int pll_factor;
  717. int ret;
  718. /*
  719. * The formula is fps = 5/4*pixclk for YUV/RGB and
  720. * fps = 5/2*pixclk for RAW.
  721. *
  722. * pixclk = clock_speed / (clkrc + 1) * PLLfactor
  723. *
  724. */
  725. if (info->pll_bypass) {
  726. pll_factor = 1;
  727. ret = ov7670_write(sd, REG_DBLV, DBLV_BYPASS);
  728. } else {
  729. pll_factor = PLL_FACTOR;
  730. ret = ov7670_write(sd, REG_DBLV, DBLV_X4);
  731. }
  732. if (ret < 0)
  733. return ret;
  734. if (tpf->numerator == 0 || tpf->denominator == 0) {
  735. clkrc = 0;
  736. } else {
  737. clkrc = (5 * pll_factor * info->clock_speed * tpf->numerator) /
  738. (4 * tpf->denominator);
  739. if (info->fmt->mbus_code == MEDIA_BUS_FMT_SBGGR8_1X8)
  740. clkrc = (clkrc << 1);
  741. clkrc--;
  742. }
  743. /*
  744. * The datasheet claims that clkrc = 0 will divide the input clock by 1
  745. * but we've checked with an oscilloscope that it divides by 2 instead.
  746. * So, if clkrc = 0 just bypass the divider.
  747. */
  748. if (clkrc <= 0)
  749. clkrc = CLK_EXT;
  750. else if (clkrc > CLK_SCALE)
  751. clkrc = CLK_SCALE;
  752. info->clkrc = clkrc;
  753. /* Recalculate frame rate */
  754. ov7675_get_framerate(sd, tpf);
  755. ret = ov7670_write(sd, REG_CLKRC, info->clkrc);
  756. if (ret < 0)
  757. return ret;
  758. return ov7670_write(sd, REG_DBLV, DBLV_X4);
  759. }
  760. static void ov7670_get_framerate_legacy(struct v4l2_subdev *sd,
  761. struct v4l2_fract *tpf)
  762. {
  763. struct ov7670_info *info = to_state(sd);
  764. tpf->numerator = 1;
  765. tpf->denominator = info->clock_speed;
  766. if ((info->clkrc & CLK_EXT) == 0 && (info->clkrc & CLK_SCALE) > 1)
  767. tpf->denominator /= (info->clkrc & CLK_SCALE);
  768. }
  769. static int ov7670_set_framerate_legacy(struct v4l2_subdev *sd,
  770. struct v4l2_fract *tpf)
  771. {
  772. struct ov7670_info *info = to_state(sd);
  773. int div;
  774. if (tpf->numerator == 0 || tpf->denominator == 0)
  775. div = 1; /* Reset to full rate */
  776. else
  777. div = (tpf->numerator * info->clock_speed) / tpf->denominator;
  778. if (div == 0)
  779. div = 1;
  780. else if (div > CLK_SCALE)
  781. div = CLK_SCALE;
  782. info->clkrc = (info->clkrc & 0x80) | div;
  783. tpf->numerator = 1;
  784. tpf->denominator = info->clock_speed / div;
  785. return ov7670_write(sd, REG_CLKRC, info->clkrc);
  786. }
  787. /*
  788. * Store a set of start/stop values into the camera.
  789. */
  790. static int ov7670_set_hw(struct v4l2_subdev *sd, int hstart, int hstop,
  791. int vstart, int vstop)
  792. {
  793. int ret;
  794. unsigned char v;
  795. /*
  796. * Horizontal: 11 bits, top 8 live in hstart and hstop. Bottom 3 of
  797. * hstart are in href[2:0], bottom 3 of hstop in href[5:3]. There is
  798. * a mystery "edge offset" value in the top two bits of href.
  799. */
  800. ret = ov7670_write(sd, REG_HSTART, (hstart >> 3) & 0xff);
  801. ret += ov7670_write(sd, REG_HSTOP, (hstop >> 3) & 0xff);
  802. ret += ov7670_read(sd, REG_HREF, &v);
  803. v = (v & 0xc0) | ((hstop & 0x7) << 3) | (hstart & 0x7);
  804. msleep(10);
  805. ret += ov7670_write(sd, REG_HREF, v);
  806. /*
  807. * Vertical: similar arrangement, but only 10 bits.
  808. */
  809. ret += ov7670_write(sd, REG_VSTART, (vstart >> 2) & 0xff);
  810. ret += ov7670_write(sd, REG_VSTOP, (vstop >> 2) & 0xff);
  811. ret += ov7670_read(sd, REG_VREF, &v);
  812. v = (v & 0xf0) | ((vstop & 0x3) << 2) | (vstart & 0x3);
  813. msleep(10);
  814. ret += ov7670_write(sd, REG_VREF, v);
  815. return ret;
  816. }
  817. static int ov7670_enum_mbus_code(struct v4l2_subdev *sd,
  818. struct v4l2_subdev_pad_config *cfg,
  819. struct v4l2_subdev_mbus_code_enum *code)
  820. {
  821. if (code->pad || code->index >= N_OV7670_FMTS)
  822. return -EINVAL;
  823. code->code = ov7670_formats[code->index].mbus_code;
  824. return 0;
  825. }
  826. static int ov7670_try_fmt_internal(struct v4l2_subdev *sd,
  827. struct v4l2_mbus_framefmt *fmt,
  828. struct ov7670_format_struct **ret_fmt,
  829. struct ov7670_win_size **ret_wsize)
  830. {
  831. int index, i;
  832. struct ov7670_win_size *wsize;
  833. struct ov7670_info *info = to_state(sd);
  834. unsigned int n_win_sizes = info->devtype->n_win_sizes;
  835. unsigned int win_sizes_limit = n_win_sizes;
  836. for (index = 0; index < N_OV7670_FMTS; index++)
  837. if (ov7670_formats[index].mbus_code == fmt->code)
  838. break;
  839. if (index >= N_OV7670_FMTS) {
  840. /* default to first format */
  841. index = 0;
  842. fmt->code = ov7670_formats[0].mbus_code;
  843. }
  844. if (ret_fmt != NULL)
  845. *ret_fmt = ov7670_formats + index;
  846. /*
  847. * Fields: the OV devices claim to be progressive.
  848. */
  849. fmt->field = V4L2_FIELD_NONE;
  850. /*
  851. * Don't consider values that don't match min_height and min_width
  852. * constraints.
  853. */
  854. if (info->min_width || info->min_height)
  855. for (i = 0; i < n_win_sizes; i++) {
  856. wsize = info->devtype->win_sizes + i;
  857. if (wsize->width < info->min_width ||
  858. wsize->height < info->min_height) {
  859. win_sizes_limit = i;
  860. break;
  861. }
  862. }
  863. /*
  864. * Round requested image size down to the nearest
  865. * we support, but not below the smallest.
  866. */
  867. for (wsize = info->devtype->win_sizes;
  868. wsize < info->devtype->win_sizes + win_sizes_limit; wsize++)
  869. if (fmt->width >= wsize->width && fmt->height >= wsize->height)
  870. break;
  871. if (wsize >= info->devtype->win_sizes + win_sizes_limit)
  872. wsize--; /* Take the smallest one */
  873. if (ret_wsize != NULL)
  874. *ret_wsize = wsize;
  875. /*
  876. * Note the size we'll actually handle.
  877. */
  878. fmt->width = wsize->width;
  879. fmt->height = wsize->height;
  880. fmt->colorspace = ov7670_formats[index].colorspace;
  881. return 0;
  882. }
  883. /*
  884. * Set a format.
  885. */
  886. static int ov7670_set_fmt(struct v4l2_subdev *sd,
  887. struct v4l2_subdev_pad_config *cfg,
  888. struct v4l2_subdev_format *format)
  889. {
  890. struct ov7670_format_struct *ovfmt;
  891. struct ov7670_win_size *wsize;
  892. struct ov7670_info *info = to_state(sd);
  893. unsigned char com7;
  894. int ret;
  895. if (format->pad)
  896. return -EINVAL;
  897. if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
  898. ret = ov7670_try_fmt_internal(sd, &format->format, NULL, NULL);
  899. if (ret)
  900. return ret;
  901. cfg->try_fmt = format->format;
  902. return 0;
  903. }
  904. ret = ov7670_try_fmt_internal(sd, &format->format, &ovfmt, &wsize);
  905. if (ret)
  906. return ret;
  907. /*
  908. * COM7 is a pain in the ass, it doesn't like to be read then
  909. * quickly written afterward. But we have everything we need
  910. * to set it absolutely here, as long as the format-specific
  911. * register sets list it first.
  912. */
  913. com7 = ovfmt->regs[0].value;
  914. com7 |= wsize->com7_bit;
  915. ov7670_write(sd, REG_COM7, com7);
  916. /*
  917. * Now write the rest of the array. Also store start/stops
  918. */
  919. ov7670_write_array(sd, ovfmt->regs + 1);
  920. ov7670_set_hw(sd, wsize->hstart, wsize->hstop, wsize->vstart,
  921. wsize->vstop);
  922. ret = 0;
  923. if (wsize->regs)
  924. ret = ov7670_write_array(sd, wsize->regs);
  925. info->fmt = ovfmt;
  926. /*
  927. * If we're running RGB565, we must rewrite clkrc after setting
  928. * the other parameters or the image looks poor. If we're *not*
  929. * doing RGB565, we must not rewrite clkrc or the image looks
  930. * *really* poor.
  931. *
  932. * (Update) Now that we retain clkrc state, we should be able
  933. * to write it unconditionally, and that will make the frame
  934. * rate persistent too.
  935. */
  936. if (ret == 0)
  937. ret = ov7670_write(sd, REG_CLKRC, info->clkrc);
  938. return 0;
  939. }
  940. /*
  941. * Implement G/S_PARM. There is a "high quality" mode we could try
  942. * to do someday; for now, we just do the frame rate tweak.
  943. */
  944. static int ov7670_g_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *parms)
  945. {
  946. struct v4l2_captureparm *cp = &parms->parm.capture;
  947. struct ov7670_info *info = to_state(sd);
  948. if (parms->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  949. return -EINVAL;
  950. cp->capability = V4L2_CAP_TIMEPERFRAME;
  951. info->devtype->get_framerate(sd, &cp->timeperframe);
  952. return 0;
  953. }
  954. static int ov7670_s_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *parms)
  955. {
  956. struct v4l2_captureparm *cp = &parms->parm.capture;
  957. struct v4l2_fract *tpf = &cp->timeperframe;
  958. struct ov7670_info *info = to_state(sd);
  959. if (parms->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  960. return -EINVAL;
  961. cp->capability = V4L2_CAP_TIMEPERFRAME;
  962. return info->devtype->set_framerate(sd, tpf);
  963. }
  964. /*
  965. * Frame intervals. Since frame rates are controlled with the clock
  966. * divider, we can only do 30/n for integer n values. So no continuous
  967. * or stepwise options. Here we just pick a handful of logical values.
  968. */
  969. static int ov7670_frame_rates[] = { 30, 15, 10, 5, 1 };
  970. static int ov7670_enum_frame_interval(struct v4l2_subdev *sd,
  971. struct v4l2_subdev_pad_config *cfg,
  972. struct v4l2_subdev_frame_interval_enum *fie)
  973. {
  974. struct ov7670_info *info = to_state(sd);
  975. unsigned int n_win_sizes = info->devtype->n_win_sizes;
  976. int i;
  977. if (fie->pad)
  978. return -EINVAL;
  979. if (fie->index >= ARRAY_SIZE(ov7670_frame_rates))
  980. return -EINVAL;
  981. /*
  982. * Check if the width/height is valid.
  983. *
  984. * If a minimum width/height was requested, filter out the capture
  985. * windows that fall outside that.
  986. */
  987. for (i = 0; i < n_win_sizes; i++) {
  988. struct ov7670_win_size *win = &info->devtype->win_sizes[i];
  989. if (info->min_width && win->width < info->min_width)
  990. continue;
  991. if (info->min_height && win->height < info->min_height)
  992. continue;
  993. if (fie->width == win->width && fie->height == win->height)
  994. break;
  995. }
  996. if (i == n_win_sizes)
  997. return -EINVAL;
  998. fie->interval.numerator = 1;
  999. fie->interval.denominator = ov7670_frame_rates[fie->index];
  1000. return 0;
  1001. }
  1002. /*
  1003. * Frame size enumeration
  1004. */
  1005. static int ov7670_enum_frame_size(struct v4l2_subdev *sd,
  1006. struct v4l2_subdev_pad_config *cfg,
  1007. struct v4l2_subdev_frame_size_enum *fse)
  1008. {
  1009. struct ov7670_info *info = to_state(sd);
  1010. int i;
  1011. int num_valid = -1;
  1012. __u32 index = fse->index;
  1013. unsigned int n_win_sizes = info->devtype->n_win_sizes;
  1014. if (fse->pad)
  1015. return -EINVAL;
  1016. /*
  1017. * If a minimum width/height was requested, filter out the capture
  1018. * windows that fall outside that.
  1019. */
  1020. for (i = 0; i < n_win_sizes; i++) {
  1021. struct ov7670_win_size *win = &info->devtype->win_sizes[i];
  1022. if (info->min_width && win->width < info->min_width)
  1023. continue;
  1024. if (info->min_height && win->height < info->min_height)
  1025. continue;
  1026. if (index == ++num_valid) {
  1027. fse->min_width = fse->max_width = win->width;
  1028. fse->min_height = fse->max_height = win->height;
  1029. return 0;
  1030. }
  1031. }
  1032. return -EINVAL;
  1033. }
  1034. /*
  1035. * Code for dealing with controls.
  1036. */
  1037. static int ov7670_store_cmatrix(struct v4l2_subdev *sd,
  1038. int matrix[CMATRIX_LEN])
  1039. {
  1040. int i, ret;
  1041. unsigned char signbits = 0;
  1042. /*
  1043. * Weird crap seems to exist in the upper part of
  1044. * the sign bits register, so let's preserve it.
  1045. */
  1046. ret = ov7670_read(sd, REG_CMATRIX_SIGN, &signbits);
  1047. signbits &= 0xc0;
  1048. for (i = 0; i < CMATRIX_LEN; i++) {
  1049. unsigned char raw;
  1050. if (matrix[i] < 0) {
  1051. signbits |= (1 << i);
  1052. if (matrix[i] < -255)
  1053. raw = 0xff;
  1054. else
  1055. raw = (-1 * matrix[i]) & 0xff;
  1056. }
  1057. else {
  1058. if (matrix[i] > 255)
  1059. raw = 0xff;
  1060. else
  1061. raw = matrix[i] & 0xff;
  1062. }
  1063. ret += ov7670_write(sd, REG_CMATRIX_BASE + i, raw);
  1064. }
  1065. ret += ov7670_write(sd, REG_CMATRIX_SIGN, signbits);
  1066. return ret;
  1067. }
  1068. /*
  1069. * Hue also requires messing with the color matrix. It also requires
  1070. * trig functions, which tend not to be well supported in the kernel.
  1071. * So here is a simple table of sine values, 0-90 degrees, in steps
  1072. * of five degrees. Values are multiplied by 1000.
  1073. *
  1074. * The following naive approximate trig functions require an argument
  1075. * carefully limited to -180 <= theta <= 180.
  1076. */
  1077. #define SIN_STEP 5
  1078. static const int ov7670_sin_table[] = {
  1079. 0, 87, 173, 258, 342, 422,
  1080. 499, 573, 642, 707, 766, 819,
  1081. 866, 906, 939, 965, 984, 996,
  1082. 1000
  1083. };
  1084. static int ov7670_sine(int theta)
  1085. {
  1086. int chs = 1;
  1087. int sine;
  1088. if (theta < 0) {
  1089. theta = -theta;
  1090. chs = -1;
  1091. }
  1092. if (theta <= 90)
  1093. sine = ov7670_sin_table[theta/SIN_STEP];
  1094. else {
  1095. theta -= 90;
  1096. sine = 1000 - ov7670_sin_table[theta/SIN_STEP];
  1097. }
  1098. return sine*chs;
  1099. }
  1100. static int ov7670_cosine(int theta)
  1101. {
  1102. theta = 90 - theta;
  1103. if (theta > 180)
  1104. theta -= 360;
  1105. else if (theta < -180)
  1106. theta += 360;
  1107. return ov7670_sine(theta);
  1108. }
  1109. static void ov7670_calc_cmatrix(struct ov7670_info *info,
  1110. int matrix[CMATRIX_LEN], int sat, int hue)
  1111. {
  1112. int i;
  1113. /*
  1114. * Apply the current saturation setting first.
  1115. */
  1116. for (i = 0; i < CMATRIX_LEN; i++)
  1117. matrix[i] = (info->fmt->cmatrix[i] * sat) >> 7;
  1118. /*
  1119. * Then, if need be, rotate the hue value.
  1120. */
  1121. if (hue != 0) {
  1122. int sinth, costh, tmpmatrix[CMATRIX_LEN];
  1123. memcpy(tmpmatrix, matrix, CMATRIX_LEN*sizeof(int));
  1124. sinth = ov7670_sine(hue);
  1125. costh = ov7670_cosine(hue);
  1126. matrix[0] = (matrix[3]*sinth + matrix[0]*costh)/1000;
  1127. matrix[1] = (matrix[4]*sinth + matrix[1]*costh)/1000;
  1128. matrix[2] = (matrix[5]*sinth + matrix[2]*costh)/1000;
  1129. matrix[3] = (matrix[3]*costh - matrix[0]*sinth)/1000;
  1130. matrix[4] = (matrix[4]*costh - matrix[1]*sinth)/1000;
  1131. matrix[5] = (matrix[5]*costh - matrix[2]*sinth)/1000;
  1132. }
  1133. }
  1134. static int ov7670_s_sat_hue(struct v4l2_subdev *sd, int sat, int hue)
  1135. {
  1136. struct ov7670_info *info = to_state(sd);
  1137. int matrix[CMATRIX_LEN];
  1138. int ret;
  1139. ov7670_calc_cmatrix(info, matrix, sat, hue);
  1140. ret = ov7670_store_cmatrix(sd, matrix);
  1141. return ret;
  1142. }
  1143. /*
  1144. * Some weird registers seem to store values in a sign/magnitude format!
  1145. */
  1146. static unsigned char ov7670_abs_to_sm(unsigned char v)
  1147. {
  1148. if (v > 127)
  1149. return v & 0x7f;
  1150. return (128 - v) | 0x80;
  1151. }
  1152. static int ov7670_s_brightness(struct v4l2_subdev *sd, int value)
  1153. {
  1154. unsigned char com8 = 0, v;
  1155. int ret;
  1156. ov7670_read(sd, REG_COM8, &com8);
  1157. com8 &= ~COM8_AEC;
  1158. ov7670_write(sd, REG_COM8, com8);
  1159. v = ov7670_abs_to_sm(value);
  1160. ret = ov7670_write(sd, REG_BRIGHT, v);
  1161. return ret;
  1162. }
  1163. static int ov7670_s_contrast(struct v4l2_subdev *sd, int value)
  1164. {
  1165. return ov7670_write(sd, REG_CONTRAS, (unsigned char) value);
  1166. }
  1167. static int ov7670_s_hflip(struct v4l2_subdev *sd, int value)
  1168. {
  1169. unsigned char v = 0;
  1170. int ret;
  1171. ret = ov7670_read(sd, REG_MVFP, &v);
  1172. if (value)
  1173. v |= MVFP_MIRROR;
  1174. else
  1175. v &= ~MVFP_MIRROR;
  1176. msleep(10); /* FIXME */
  1177. ret += ov7670_write(sd, REG_MVFP, v);
  1178. return ret;
  1179. }
  1180. static int ov7670_s_vflip(struct v4l2_subdev *sd, int value)
  1181. {
  1182. unsigned char v = 0;
  1183. int ret;
  1184. ret = ov7670_read(sd, REG_MVFP, &v);
  1185. if (value)
  1186. v |= MVFP_FLIP;
  1187. else
  1188. v &= ~MVFP_FLIP;
  1189. msleep(10); /* FIXME */
  1190. ret += ov7670_write(sd, REG_MVFP, v);
  1191. return ret;
  1192. }
  1193. /*
  1194. * GAIN is split between REG_GAIN and REG_VREF[7:6]. If one believes
  1195. * the data sheet, the VREF parts should be the most significant, but
  1196. * experience shows otherwise. There seems to be little value in
  1197. * messing with the VREF bits, so we leave them alone.
  1198. */
  1199. static int ov7670_g_gain(struct v4l2_subdev *sd, __s32 *value)
  1200. {
  1201. int ret;
  1202. unsigned char gain;
  1203. ret = ov7670_read(sd, REG_GAIN, &gain);
  1204. *value = gain;
  1205. return ret;
  1206. }
  1207. static int ov7670_s_gain(struct v4l2_subdev *sd, int value)
  1208. {
  1209. int ret;
  1210. unsigned char com8;
  1211. ret = ov7670_write(sd, REG_GAIN, value & 0xff);
  1212. /* Have to turn off AGC as well */
  1213. if (ret == 0) {
  1214. ret = ov7670_read(sd, REG_COM8, &com8);
  1215. ret = ov7670_write(sd, REG_COM8, com8 & ~COM8_AGC);
  1216. }
  1217. return ret;
  1218. }
  1219. /*
  1220. * Tweak autogain.
  1221. */
  1222. static int ov7670_s_autogain(struct v4l2_subdev *sd, int value)
  1223. {
  1224. int ret;
  1225. unsigned char com8;
  1226. ret = ov7670_read(sd, REG_COM8, &com8);
  1227. if (ret == 0) {
  1228. if (value)
  1229. com8 |= COM8_AGC;
  1230. else
  1231. com8 &= ~COM8_AGC;
  1232. ret = ov7670_write(sd, REG_COM8, com8);
  1233. }
  1234. return ret;
  1235. }
  1236. static int ov7670_s_exp(struct v4l2_subdev *sd, int value)
  1237. {
  1238. int ret;
  1239. unsigned char com1, com8, aech, aechh;
  1240. ret = ov7670_read(sd, REG_COM1, &com1) +
  1241. ov7670_read(sd, REG_COM8, &com8) +
  1242. ov7670_read(sd, REG_AECHH, &aechh);
  1243. if (ret)
  1244. return ret;
  1245. com1 = (com1 & 0xfc) | (value & 0x03);
  1246. aech = (value >> 2) & 0xff;
  1247. aechh = (aechh & 0xc0) | ((value >> 10) & 0x3f);
  1248. ret = ov7670_write(sd, REG_COM1, com1) +
  1249. ov7670_write(sd, REG_AECH, aech) +
  1250. ov7670_write(sd, REG_AECHH, aechh);
  1251. /* Have to turn off AEC as well */
  1252. if (ret == 0)
  1253. ret = ov7670_write(sd, REG_COM8, com8 & ~COM8_AEC);
  1254. return ret;
  1255. }
  1256. /*
  1257. * Tweak autoexposure.
  1258. */
  1259. static int ov7670_s_autoexp(struct v4l2_subdev *sd,
  1260. enum v4l2_exposure_auto_type value)
  1261. {
  1262. int ret;
  1263. unsigned char com8;
  1264. ret = ov7670_read(sd, REG_COM8, &com8);
  1265. if (ret == 0) {
  1266. if (value == V4L2_EXPOSURE_AUTO)
  1267. com8 |= COM8_AEC;
  1268. else
  1269. com8 &= ~COM8_AEC;
  1270. ret = ov7670_write(sd, REG_COM8, com8);
  1271. }
  1272. return ret;
  1273. }
  1274. static int ov7670_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
  1275. {
  1276. struct v4l2_subdev *sd = to_sd(ctrl);
  1277. struct ov7670_info *info = to_state(sd);
  1278. switch (ctrl->id) {
  1279. case V4L2_CID_AUTOGAIN:
  1280. return ov7670_g_gain(sd, &info->gain->val);
  1281. }
  1282. return -EINVAL;
  1283. }
  1284. static int ov7670_s_ctrl(struct v4l2_ctrl *ctrl)
  1285. {
  1286. struct v4l2_subdev *sd = to_sd(ctrl);
  1287. struct ov7670_info *info = to_state(sd);
  1288. switch (ctrl->id) {
  1289. case V4L2_CID_BRIGHTNESS:
  1290. return ov7670_s_brightness(sd, ctrl->val);
  1291. case V4L2_CID_CONTRAST:
  1292. return ov7670_s_contrast(sd, ctrl->val);
  1293. case V4L2_CID_SATURATION:
  1294. return ov7670_s_sat_hue(sd,
  1295. info->saturation->val, info->hue->val);
  1296. case V4L2_CID_VFLIP:
  1297. return ov7670_s_vflip(sd, ctrl->val);
  1298. case V4L2_CID_HFLIP:
  1299. return ov7670_s_hflip(sd, ctrl->val);
  1300. case V4L2_CID_AUTOGAIN:
  1301. /* Only set manual gain if auto gain is not explicitly
  1302. turned on. */
  1303. if (!ctrl->val) {
  1304. /* ov7670_s_gain turns off auto gain */
  1305. return ov7670_s_gain(sd, info->gain->val);
  1306. }
  1307. return ov7670_s_autogain(sd, ctrl->val);
  1308. case V4L2_CID_EXPOSURE_AUTO:
  1309. /* Only set manual exposure if auto exposure is not explicitly
  1310. turned on. */
  1311. if (ctrl->val == V4L2_EXPOSURE_MANUAL) {
  1312. /* ov7670_s_exp turns off auto exposure */
  1313. return ov7670_s_exp(sd, info->exposure->val);
  1314. }
  1315. return ov7670_s_autoexp(sd, ctrl->val);
  1316. }
  1317. return -EINVAL;
  1318. }
  1319. static const struct v4l2_ctrl_ops ov7670_ctrl_ops = {
  1320. .s_ctrl = ov7670_s_ctrl,
  1321. .g_volatile_ctrl = ov7670_g_volatile_ctrl,
  1322. };
  1323. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1324. static int ov7670_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
  1325. {
  1326. unsigned char val = 0;
  1327. int ret;
  1328. ret = ov7670_read(sd, reg->reg & 0xff, &val);
  1329. reg->val = val;
  1330. reg->size = 1;
  1331. return ret;
  1332. }
  1333. static int ov7670_s_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg)
  1334. {
  1335. ov7670_write(sd, reg->reg & 0xff, reg->val & 0xff);
  1336. return 0;
  1337. }
  1338. #endif
  1339. /* ----------------------------------------------------------------------- */
  1340. static const struct v4l2_subdev_core_ops ov7670_core_ops = {
  1341. .reset = ov7670_reset,
  1342. .init = ov7670_init,
  1343. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1344. .g_register = ov7670_g_register,
  1345. .s_register = ov7670_s_register,
  1346. #endif
  1347. };
  1348. static const struct v4l2_subdev_video_ops ov7670_video_ops = {
  1349. .s_parm = ov7670_s_parm,
  1350. .g_parm = ov7670_g_parm,
  1351. };
  1352. static const struct v4l2_subdev_pad_ops ov7670_pad_ops = {
  1353. .enum_frame_interval = ov7670_enum_frame_interval,
  1354. .enum_frame_size = ov7670_enum_frame_size,
  1355. .enum_mbus_code = ov7670_enum_mbus_code,
  1356. .set_fmt = ov7670_set_fmt,
  1357. };
  1358. static const struct v4l2_subdev_ops ov7670_ops = {
  1359. .core = &ov7670_core_ops,
  1360. .video = &ov7670_video_ops,
  1361. .pad = &ov7670_pad_ops,
  1362. };
  1363. /* ----------------------------------------------------------------------- */
  1364. static const struct ov7670_devtype ov7670_devdata[] = {
  1365. [MODEL_OV7670] = {
  1366. .win_sizes = ov7670_win_sizes,
  1367. .n_win_sizes = ARRAY_SIZE(ov7670_win_sizes),
  1368. .set_framerate = ov7670_set_framerate_legacy,
  1369. .get_framerate = ov7670_get_framerate_legacy,
  1370. },
  1371. [MODEL_OV7675] = {
  1372. .win_sizes = ov7675_win_sizes,
  1373. .n_win_sizes = ARRAY_SIZE(ov7675_win_sizes),
  1374. .set_framerate = ov7675_set_framerate,
  1375. .get_framerate = ov7675_get_framerate,
  1376. },
  1377. };
  1378. static int ov7670_init_gpio(struct i2c_client *client, struct ov7670_info *info)
  1379. {
  1380. info->pwdn_gpio = devm_gpiod_get_optional(&client->dev, "powerdown",
  1381. GPIOD_OUT_LOW);
  1382. if (IS_ERR(info->pwdn_gpio)) {
  1383. dev_info(&client->dev, "can't get %s GPIO\n", "powerdown");
  1384. return PTR_ERR(info->pwdn_gpio);
  1385. }
  1386. info->resetb_gpio = devm_gpiod_get_optional(&client->dev, "reset",
  1387. GPIOD_OUT_LOW);
  1388. if (IS_ERR(info->resetb_gpio)) {
  1389. dev_info(&client->dev, "can't get %s GPIO\n", "reset");
  1390. return PTR_ERR(info->resetb_gpio);
  1391. }
  1392. usleep_range(3000, 5000);
  1393. return 0;
  1394. }
  1395. static int ov7670_probe(struct i2c_client *client,
  1396. const struct i2c_device_id *id)
  1397. {
  1398. struct v4l2_fract tpf;
  1399. struct v4l2_subdev *sd;
  1400. struct ov7670_info *info;
  1401. int ret;
  1402. info = devm_kzalloc(&client->dev, sizeof(*info), GFP_KERNEL);
  1403. if (info == NULL)
  1404. return -ENOMEM;
  1405. sd = &info->sd;
  1406. v4l2_i2c_subdev_init(sd, client, &ov7670_ops);
  1407. info->clock_speed = 30; /* default: a guess */
  1408. if (client->dev.platform_data) {
  1409. struct ov7670_config *config = client->dev.platform_data;
  1410. /*
  1411. * Must apply configuration before initializing device, because it
  1412. * selects I/O method.
  1413. */
  1414. info->min_width = config->min_width;
  1415. info->min_height = config->min_height;
  1416. info->use_smbus = config->use_smbus;
  1417. if (config->clock_speed)
  1418. info->clock_speed = config->clock_speed;
  1419. /*
  1420. * It should be allowed for ov7670 too when it is migrated to
  1421. * the new frame rate formula.
  1422. */
  1423. if (config->pll_bypass && id->driver_data != MODEL_OV7670)
  1424. info->pll_bypass = true;
  1425. if (config->pclk_hb_disable)
  1426. info->pclk_hb_disable = true;
  1427. }
  1428. info->clk = devm_clk_get(&client->dev, "xclk");
  1429. if (IS_ERR(info->clk))
  1430. return -EPROBE_DEFER;
  1431. clk_prepare_enable(info->clk);
  1432. ret = ov7670_init_gpio(client, info);
  1433. if (ret)
  1434. goto clk_disable;
  1435. info->clock_speed = clk_get_rate(info->clk) / 1000000;
  1436. if (info->clock_speed < 10 || info->clock_speed > 48) {
  1437. ret = -EINVAL;
  1438. goto clk_disable;
  1439. }
  1440. /* Make sure it's an ov7670 */
  1441. ret = ov7670_detect(sd);
  1442. if (ret) {
  1443. v4l_dbg(1, debug, client,
  1444. "chip found @ 0x%x (%s) is not an ov7670 chip.\n",
  1445. client->addr << 1, client->adapter->name);
  1446. goto clk_disable;
  1447. }
  1448. v4l_info(client, "chip found @ 0x%02x (%s)\n",
  1449. client->addr << 1, client->adapter->name);
  1450. info->devtype = &ov7670_devdata[id->driver_data];
  1451. info->fmt = &ov7670_formats[0];
  1452. info->clkrc = 0;
  1453. /* Set default frame rate to 30 fps */
  1454. tpf.numerator = 1;
  1455. tpf.denominator = 30;
  1456. info->devtype->set_framerate(sd, &tpf);
  1457. if (info->pclk_hb_disable)
  1458. ov7670_write(sd, REG_COM10, COM10_PCLK_HB);
  1459. v4l2_ctrl_handler_init(&info->hdl, 10);
  1460. v4l2_ctrl_new_std(&info->hdl, &ov7670_ctrl_ops,
  1461. V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
  1462. v4l2_ctrl_new_std(&info->hdl, &ov7670_ctrl_ops,
  1463. V4L2_CID_CONTRAST, 0, 127, 1, 64);
  1464. v4l2_ctrl_new_std(&info->hdl, &ov7670_ctrl_ops,
  1465. V4L2_CID_VFLIP, 0, 1, 1, 0);
  1466. v4l2_ctrl_new_std(&info->hdl, &ov7670_ctrl_ops,
  1467. V4L2_CID_HFLIP, 0, 1, 1, 0);
  1468. info->saturation = v4l2_ctrl_new_std(&info->hdl, &ov7670_ctrl_ops,
  1469. V4L2_CID_SATURATION, 0, 256, 1, 128);
  1470. info->hue = v4l2_ctrl_new_std(&info->hdl, &ov7670_ctrl_ops,
  1471. V4L2_CID_HUE, -180, 180, 5, 0);
  1472. info->gain = v4l2_ctrl_new_std(&info->hdl, &ov7670_ctrl_ops,
  1473. V4L2_CID_GAIN, 0, 255, 1, 128);
  1474. info->auto_gain = v4l2_ctrl_new_std(&info->hdl, &ov7670_ctrl_ops,
  1475. V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
  1476. info->exposure = v4l2_ctrl_new_std(&info->hdl, &ov7670_ctrl_ops,
  1477. V4L2_CID_EXPOSURE, 0, 65535, 1, 500);
  1478. info->auto_exposure = v4l2_ctrl_new_std_menu(&info->hdl, &ov7670_ctrl_ops,
  1479. V4L2_CID_EXPOSURE_AUTO, V4L2_EXPOSURE_MANUAL, 0,
  1480. V4L2_EXPOSURE_AUTO);
  1481. sd->ctrl_handler = &info->hdl;
  1482. if (info->hdl.error) {
  1483. ret = info->hdl.error;
  1484. goto hdl_free;
  1485. }
  1486. /*
  1487. * We have checked empirically that hw allows to read back the gain
  1488. * value chosen by auto gain but that's not the case for auto exposure.
  1489. */
  1490. v4l2_ctrl_auto_cluster(2, &info->auto_gain, 0, true);
  1491. v4l2_ctrl_auto_cluster(2, &info->auto_exposure,
  1492. V4L2_EXPOSURE_MANUAL, false);
  1493. v4l2_ctrl_cluster(2, &info->saturation);
  1494. v4l2_ctrl_handler_setup(&info->hdl);
  1495. ret = v4l2_async_register_subdev(&info->sd);
  1496. if (ret < 0)
  1497. goto hdl_free;
  1498. return 0;
  1499. hdl_free:
  1500. v4l2_ctrl_handler_free(&info->hdl);
  1501. clk_disable:
  1502. clk_disable_unprepare(info->clk);
  1503. return ret;
  1504. }
  1505. static int ov7670_remove(struct i2c_client *client)
  1506. {
  1507. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1508. struct ov7670_info *info = to_state(sd);
  1509. v4l2_device_unregister_subdev(sd);
  1510. v4l2_ctrl_handler_free(&info->hdl);
  1511. clk_disable_unprepare(info->clk);
  1512. return 0;
  1513. }
  1514. static const struct i2c_device_id ov7670_id[] = {
  1515. { "ov7670", MODEL_OV7670 },
  1516. { "ov7675", MODEL_OV7675 },
  1517. { }
  1518. };
  1519. MODULE_DEVICE_TABLE(i2c, ov7670_id);
  1520. #if IS_ENABLED(CONFIG_OF)
  1521. static const struct of_device_id ov7670_of_match[] = {
  1522. { .compatible = "ovti,ov7670", },
  1523. { /* sentinel */ },
  1524. };
  1525. MODULE_DEVICE_TABLE(of, ov7670_of_match);
  1526. #endif
  1527. static struct i2c_driver ov7670_driver = {
  1528. .driver = {
  1529. .name = "ov7670",
  1530. .of_match_table = of_match_ptr(ov7670_of_match),
  1531. },
  1532. .probe = ov7670_probe,
  1533. .remove = ov7670_remove,
  1534. .id_table = ov7670_id,
  1535. };
  1536. module_i2c_driver(ov7670_driver);