ov772x.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  1. /*
  2. * ov772x Camera Driver
  3. *
  4. * Copyright (C) 2008 Renesas Solutions Corp.
  5. * Kuninori Morimoto <morimoto.kuninori@renesas.com>
  6. *
  7. * Based on ov7670 and soc_camera_platform driver,
  8. *
  9. * Copyright 2006-7 Jonathan Corbet <corbet@lwn.net>
  10. * Copyright (C) 2008 Magnus Damm
  11. * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License version 2 as
  15. * published by the Free Software Foundation.
  16. */
  17. #include <linux/init.h>
  18. #include <linux/module.h>
  19. #include <linux/i2c.h>
  20. #include <linux/slab.h>
  21. #include <linux/delay.h>
  22. #include <linux/videodev2.h>
  23. #include <media/v4l2-chip-ident.h>
  24. #include <media/v4l2-subdev.h>
  25. #include <media/soc_camera.h>
  26. #include <media/soc_mediabus.h>
  27. #include <media/ov772x.h>
  28. /*
  29. * register offset
  30. */
  31. #define GAIN 0x00 /* AGC - Gain control gain setting */
  32. #define BLUE 0x01 /* AWB - Blue channel gain setting */
  33. #define RED 0x02 /* AWB - Red channel gain setting */
  34. #define GREEN 0x03 /* AWB - Green channel gain setting */
  35. #define COM1 0x04 /* Common control 1 */
  36. #define BAVG 0x05 /* U/B Average Level */
  37. #define GAVG 0x06 /* Y/Gb Average Level */
  38. #define RAVG 0x07 /* V/R Average Level */
  39. #define AECH 0x08 /* Exposure Value - AEC MSBs */
  40. #define COM2 0x09 /* Common control 2 */
  41. #define PID 0x0A /* Product ID Number MSB */
  42. #define VER 0x0B /* Product ID Number LSB */
  43. #define COM3 0x0C /* Common control 3 */
  44. #define COM4 0x0D /* Common control 4 */
  45. #define COM5 0x0E /* Common control 5 */
  46. #define COM6 0x0F /* Common control 6 */
  47. #define AEC 0x10 /* Exposure Value */
  48. #define CLKRC 0x11 /* Internal clock */
  49. #define COM7 0x12 /* Common control 7 */
  50. #define COM8 0x13 /* Common control 8 */
  51. #define COM9 0x14 /* Common control 9 */
  52. #define COM10 0x15 /* Common control 10 */
  53. #define REG16 0x16 /* Register 16 */
  54. #define HSTART 0x17 /* Horizontal sensor size */
  55. #define HSIZE 0x18 /* Horizontal frame (HREF column) end high 8-bit */
  56. #define VSTART 0x19 /* Vertical frame (row) start high 8-bit */
  57. #define VSIZE 0x1A /* Vertical sensor size */
  58. #define PSHFT 0x1B /* Data format - pixel delay select */
  59. #define MIDH 0x1C /* Manufacturer ID byte - high */
  60. #define MIDL 0x1D /* Manufacturer ID byte - low */
  61. #define LAEC 0x1F /* Fine AEC value */
  62. #define COM11 0x20 /* Common control 11 */
  63. #define BDBASE 0x22 /* Banding filter Minimum AEC value */
  64. #define DBSTEP 0x23 /* Banding filter Maximum Setp */
  65. #define AEW 0x24 /* AGC/AEC - Stable operating region (upper limit) */
  66. #define AEB 0x25 /* AGC/AEC - Stable operating region (lower limit) */
  67. #define VPT 0x26 /* AGC/AEC Fast mode operating region */
  68. #define REG28 0x28 /* Register 28 */
  69. #define HOUTSIZE 0x29 /* Horizontal data output size MSBs */
  70. #define EXHCH 0x2A /* Dummy pixel insert MSB */
  71. #define EXHCL 0x2B /* Dummy pixel insert LSB */
  72. #define VOUTSIZE 0x2C /* Vertical data output size MSBs */
  73. #define ADVFL 0x2D /* LSB of insert dummy lines in Vertical direction */
  74. #define ADVFH 0x2E /* MSG of insert dummy lines in Vertical direction */
  75. #define YAVE 0x2F /* Y/G Channel Average value */
  76. #define LUMHTH 0x30 /* Histogram AEC/AGC Luminance high level threshold */
  77. #define LUMLTH 0x31 /* Histogram AEC/AGC Luminance low level threshold */
  78. #define HREF 0x32 /* Image start and size control */
  79. #define DM_LNL 0x33 /* Dummy line low 8 bits */
  80. #define DM_LNH 0x34 /* Dummy line high 8 bits */
  81. #define ADOFF_B 0x35 /* AD offset compensation value for B channel */
  82. #define ADOFF_R 0x36 /* AD offset compensation value for R channel */
  83. #define ADOFF_GB 0x37 /* AD offset compensation value for Gb channel */
  84. #define ADOFF_GR 0x38 /* AD offset compensation value for Gr channel */
  85. #define OFF_B 0x39 /* Analog process B channel offset value */
  86. #define OFF_R 0x3A /* Analog process R channel offset value */
  87. #define OFF_GB 0x3B /* Analog process Gb channel offset value */
  88. #define OFF_GR 0x3C /* Analog process Gr channel offset value */
  89. #define COM12 0x3D /* Common control 12 */
  90. #define COM13 0x3E /* Common control 13 */
  91. #define COM14 0x3F /* Common control 14 */
  92. #define COM15 0x40 /* Common control 15*/
  93. #define COM16 0x41 /* Common control 16 */
  94. #define TGT_B 0x42 /* BLC blue channel target value */
  95. #define TGT_R 0x43 /* BLC red channel target value */
  96. #define TGT_GB 0x44 /* BLC Gb channel target value */
  97. #define TGT_GR 0x45 /* BLC Gr channel target value */
  98. /* for ov7720 */
  99. #define LCC0 0x46 /* Lens correction control 0 */
  100. #define LCC1 0x47 /* Lens correction option 1 - X coordinate */
  101. #define LCC2 0x48 /* Lens correction option 2 - Y coordinate */
  102. #define LCC3 0x49 /* Lens correction option 3 */
  103. #define LCC4 0x4A /* Lens correction option 4 - radius of the circular */
  104. #define LCC5 0x4B /* Lens correction option 5 */
  105. #define LCC6 0x4C /* Lens correction option 6 */
  106. /* for ov7725 */
  107. #define LC_CTR 0x46 /* Lens correction control */
  108. #define LC_XC 0x47 /* X coordinate of lens correction center relative */
  109. #define LC_YC 0x48 /* Y coordinate of lens correction center relative */
  110. #define LC_COEF 0x49 /* Lens correction coefficient */
  111. #define LC_RADI 0x4A /* Lens correction radius */
  112. #define LC_COEFB 0x4B /* Lens B channel compensation coefficient */
  113. #define LC_COEFR 0x4C /* Lens R channel compensation coefficient */
  114. #define FIXGAIN 0x4D /* Analog fix gain amplifer */
  115. #define AREF0 0x4E /* Sensor reference control */
  116. #define AREF1 0x4F /* Sensor reference current control */
  117. #define AREF2 0x50 /* Analog reference control */
  118. #define AREF3 0x51 /* ADC reference control */
  119. #define AREF4 0x52 /* ADC reference control */
  120. #define AREF5 0x53 /* ADC reference control */
  121. #define AREF6 0x54 /* Analog reference control */
  122. #define AREF7 0x55 /* Analog reference control */
  123. #define UFIX 0x60 /* U channel fixed value output */
  124. #define VFIX 0x61 /* V channel fixed value output */
  125. #define AWBB_BLK 0x62 /* AWB option for advanced AWB */
  126. #define AWB_CTRL0 0x63 /* AWB control byte 0 */
  127. #define DSP_CTRL1 0x64 /* DSP control byte 1 */
  128. #define DSP_CTRL2 0x65 /* DSP control byte 2 */
  129. #define DSP_CTRL3 0x66 /* DSP control byte 3 */
  130. #define DSP_CTRL4 0x67 /* DSP control byte 4 */
  131. #define AWB_BIAS 0x68 /* AWB BLC level clip */
  132. #define AWB_CTRL1 0x69 /* AWB control 1 */
  133. #define AWB_CTRL2 0x6A /* AWB control 2 */
  134. #define AWB_CTRL3 0x6B /* AWB control 3 */
  135. #define AWB_CTRL4 0x6C /* AWB control 4 */
  136. #define AWB_CTRL5 0x6D /* AWB control 5 */
  137. #define AWB_CTRL6 0x6E /* AWB control 6 */
  138. #define AWB_CTRL7 0x6F /* AWB control 7 */
  139. #define AWB_CTRL8 0x70 /* AWB control 8 */
  140. #define AWB_CTRL9 0x71 /* AWB control 9 */
  141. #define AWB_CTRL10 0x72 /* AWB control 10 */
  142. #define AWB_CTRL11 0x73 /* AWB control 11 */
  143. #define AWB_CTRL12 0x74 /* AWB control 12 */
  144. #define AWB_CTRL13 0x75 /* AWB control 13 */
  145. #define AWB_CTRL14 0x76 /* AWB control 14 */
  146. #define AWB_CTRL15 0x77 /* AWB control 15 */
  147. #define AWB_CTRL16 0x78 /* AWB control 16 */
  148. #define AWB_CTRL17 0x79 /* AWB control 17 */
  149. #define AWB_CTRL18 0x7A /* AWB control 18 */
  150. #define AWB_CTRL19 0x7B /* AWB control 19 */
  151. #define AWB_CTRL20 0x7C /* AWB control 20 */
  152. #define AWB_CTRL21 0x7D /* AWB control 21 */
  153. #define GAM1 0x7E /* Gamma Curve 1st segment input end point */
  154. #define GAM2 0x7F /* Gamma Curve 2nd segment input end point */
  155. #define GAM3 0x80 /* Gamma Curve 3rd segment input end point */
  156. #define GAM4 0x81 /* Gamma Curve 4th segment input end point */
  157. #define GAM5 0x82 /* Gamma Curve 5th segment input end point */
  158. #define GAM6 0x83 /* Gamma Curve 6th segment input end point */
  159. #define GAM7 0x84 /* Gamma Curve 7th segment input end point */
  160. #define GAM8 0x85 /* Gamma Curve 8th segment input end point */
  161. #define GAM9 0x86 /* Gamma Curve 9th segment input end point */
  162. #define GAM10 0x87 /* Gamma Curve 10th segment input end point */
  163. #define GAM11 0x88 /* Gamma Curve 11th segment input end point */
  164. #define GAM12 0x89 /* Gamma Curve 12th segment input end point */
  165. #define GAM13 0x8A /* Gamma Curve 13th segment input end point */
  166. #define GAM14 0x8B /* Gamma Curve 14th segment input end point */
  167. #define GAM15 0x8C /* Gamma Curve 15th segment input end point */
  168. #define SLOP 0x8D /* Gamma curve highest segment slope */
  169. #define DNSTH 0x8E /* De-noise threshold */
  170. #define EDGE_STRNGT 0x8F /* Edge strength control when manual mode */
  171. #define EDGE_TRSHLD 0x90 /* Edge threshold control when manual mode */
  172. #define DNSOFF 0x91 /* Auto De-noise threshold control */
  173. #define EDGE_UPPER 0x92 /* Edge strength upper limit when Auto mode */
  174. #define EDGE_LOWER 0x93 /* Edge strength lower limit when Auto mode */
  175. #define MTX1 0x94 /* Matrix coefficient 1 */
  176. #define MTX2 0x95 /* Matrix coefficient 2 */
  177. #define MTX3 0x96 /* Matrix coefficient 3 */
  178. #define MTX4 0x97 /* Matrix coefficient 4 */
  179. #define MTX5 0x98 /* Matrix coefficient 5 */
  180. #define MTX6 0x99 /* Matrix coefficient 6 */
  181. #define MTX_CTRL 0x9A /* Matrix control */
  182. #define BRIGHT 0x9B /* Brightness control */
  183. #define CNTRST 0x9C /* Contrast contrast */
  184. #define CNTRST_CTRL 0x9D /* Contrast contrast center */
  185. #define UVAD_J0 0x9E /* Auto UV adjust contrast 0 */
  186. #define UVAD_J1 0x9F /* Auto UV adjust contrast 1 */
  187. #define SCAL0 0xA0 /* Scaling control 0 */
  188. #define SCAL1 0xA1 /* Scaling control 1 */
  189. #define SCAL2 0xA2 /* Scaling control 2 */
  190. #define FIFODLYM 0xA3 /* FIFO manual mode delay control */
  191. #define FIFODLYA 0xA4 /* FIFO auto mode delay control */
  192. #define SDE 0xA6 /* Special digital effect control */
  193. #define USAT 0xA7 /* U component saturation control */
  194. #define VSAT 0xA8 /* V component saturation control */
  195. /* for ov7720 */
  196. #define HUE0 0xA9 /* Hue control 0 */
  197. #define HUE1 0xAA /* Hue control 1 */
  198. /* for ov7725 */
  199. #define HUECOS 0xA9 /* Cosine value */
  200. #define HUESIN 0xAA /* Sine value */
  201. #define SIGN 0xAB /* Sign bit for Hue and contrast */
  202. #define DSPAUTO 0xAC /* DSP auto function ON/OFF control */
  203. /*
  204. * register detail
  205. */
  206. /* COM2 */
  207. #define SOFT_SLEEP_MODE 0x10 /* Soft sleep mode */
  208. /* Output drive capability */
  209. #define OCAP_1x 0x00 /* 1x */
  210. #define OCAP_2x 0x01 /* 2x */
  211. #define OCAP_3x 0x02 /* 3x */
  212. #define OCAP_4x 0x03 /* 4x */
  213. /* COM3 */
  214. #define SWAP_MASK (SWAP_RGB | SWAP_YUV | SWAP_ML)
  215. #define IMG_MASK (VFLIP_IMG | HFLIP_IMG)
  216. #define VFLIP_IMG 0x80 /* Vertical flip image ON/OFF selection */
  217. #define HFLIP_IMG 0x40 /* Horizontal mirror image ON/OFF selection */
  218. #define SWAP_RGB 0x20 /* Swap B/R output sequence in RGB mode */
  219. #define SWAP_YUV 0x10 /* Swap Y/UV output sequence in YUV mode */
  220. #define SWAP_ML 0x08 /* Swap output MSB/LSB */
  221. /* Tri-state option for output clock */
  222. #define NOTRI_CLOCK 0x04 /* 0: Tri-state at this period */
  223. /* 1: No tri-state at this period */
  224. /* Tri-state option for output data */
  225. #define NOTRI_DATA 0x02 /* 0: Tri-state at this period */
  226. /* 1: No tri-state at this period */
  227. #define SCOLOR_TEST 0x01 /* Sensor color bar test pattern */
  228. /* COM4 */
  229. /* PLL frequency control */
  230. #define PLL_BYPASS 0x00 /* 00: Bypass PLL */
  231. #define PLL_4x 0x40 /* 01: PLL 4x */
  232. #define PLL_6x 0x80 /* 10: PLL 6x */
  233. #define PLL_8x 0xc0 /* 11: PLL 8x */
  234. /* AEC evaluate window */
  235. #define AEC_FULL 0x00 /* 00: Full window */
  236. #define AEC_1p2 0x10 /* 01: 1/2 window */
  237. #define AEC_1p4 0x20 /* 10: 1/4 window */
  238. #define AEC_2p3 0x30 /* 11: Low 2/3 window */
  239. /* COM5 */
  240. #define AFR_ON_OFF 0x80 /* Auto frame rate control ON/OFF selection */
  241. #define AFR_SPPED 0x40 /* Auto frame rate control speed selection */
  242. /* Auto frame rate max rate control */
  243. #define AFR_NO_RATE 0x00 /* No reduction of frame rate */
  244. #define AFR_1p2 0x10 /* Max reduction to 1/2 frame rate */
  245. #define AFR_1p4 0x20 /* Max reduction to 1/4 frame rate */
  246. #define AFR_1p8 0x30 /* Max reduction to 1/8 frame rate */
  247. /* Auto frame rate active point control */
  248. #define AF_2x 0x00 /* Add frame when AGC reaches 2x gain */
  249. #define AF_4x 0x04 /* Add frame when AGC reaches 4x gain */
  250. #define AF_8x 0x08 /* Add frame when AGC reaches 8x gain */
  251. #define AF_16x 0x0c /* Add frame when AGC reaches 16x gain */
  252. /* AEC max step control */
  253. #define AEC_NO_LIMIT 0x01 /* 0 : AEC incease step has limit */
  254. /* 1 : No limit to AEC increase step */
  255. /* COM7 */
  256. /* SCCB Register Reset */
  257. #define SCCB_RESET 0x80 /* 0 : No change */
  258. /* 1 : Resets all registers to default */
  259. /* Resolution selection */
  260. #define SLCT_MASK 0x40 /* Mask of VGA or QVGA */
  261. #define SLCT_VGA 0x00 /* 0 : VGA */
  262. #define SLCT_QVGA 0x40 /* 1 : QVGA */
  263. #define ITU656_ON_OFF 0x20 /* ITU656 protocol ON/OFF selection */
  264. /* RGB output format control */
  265. #define FMT_MASK 0x0c /* Mask of color format */
  266. #define FMT_GBR422 0x00 /* 00 : GBR 4:2:2 */
  267. #define FMT_RGB565 0x04 /* 01 : RGB 565 */
  268. #define FMT_RGB555 0x08 /* 10 : RGB 555 */
  269. #define FMT_RGB444 0x0c /* 11 : RGB 444 */
  270. /* Output format control */
  271. #define OFMT_MASK 0x03 /* Mask of output format */
  272. #define OFMT_YUV 0x00 /* 00 : YUV */
  273. #define OFMT_P_BRAW 0x01 /* 01 : Processed Bayer RAW */
  274. #define OFMT_RGB 0x02 /* 10 : RGB */
  275. #define OFMT_BRAW 0x03 /* 11 : Bayer RAW */
  276. /* COM8 */
  277. #define FAST_ALGO 0x80 /* Enable fast AGC/AEC algorithm */
  278. /* AEC Setp size limit */
  279. #define UNLMT_STEP 0x40 /* 0 : Step size is limited */
  280. /* 1 : Unlimited step size */
  281. #define BNDF_ON_OFF 0x20 /* Banding filter ON/OFF */
  282. #define AEC_BND 0x10 /* Enable AEC below banding value */
  283. #define AEC_ON_OFF 0x08 /* Fine AEC ON/OFF control */
  284. #define AGC_ON 0x04 /* AGC Enable */
  285. #define AWB_ON 0x02 /* AWB Enable */
  286. #define AEC_ON 0x01 /* AEC Enable */
  287. /* COM9 */
  288. #define BASE_AECAGC 0x80 /* Histogram or average based AEC/AGC */
  289. /* Automatic gain ceiling - maximum AGC value */
  290. #define GAIN_2x 0x00 /* 000 : 2x */
  291. #define GAIN_4x 0x10 /* 001 : 4x */
  292. #define GAIN_8x 0x20 /* 010 : 8x */
  293. #define GAIN_16x 0x30 /* 011 : 16x */
  294. #define GAIN_32x 0x40 /* 100 : 32x */
  295. #define GAIN_64x 0x50 /* 101 : 64x */
  296. #define GAIN_128x 0x60 /* 110 : 128x */
  297. #define DROP_VSYNC 0x04 /* Drop VSYNC output of corrupt frame */
  298. #define DROP_HREF 0x02 /* Drop HREF output of corrupt frame */
  299. /* COM11 */
  300. #define SGLF_ON_OFF 0x02 /* Single frame ON/OFF selection */
  301. #define SGLF_TRIG 0x01 /* Single frame transfer trigger */
  302. /* EXHCH */
  303. #define VSIZE_LSB 0x04 /* Vertical data output size LSB */
  304. /* DSP_CTRL1 */
  305. #define FIFO_ON 0x80 /* FIFO enable/disable selection */
  306. #define UV_ON_OFF 0x40 /* UV adjust function ON/OFF selection */
  307. #define YUV444_2_422 0x20 /* YUV444 to 422 UV channel option selection */
  308. #define CLR_MTRX_ON_OFF 0x10 /* Color matrix ON/OFF selection */
  309. #define INTPLT_ON_OFF 0x08 /* Interpolation ON/OFF selection */
  310. #define GMM_ON_OFF 0x04 /* Gamma function ON/OFF selection */
  311. #define AUTO_BLK_ON_OFF 0x02 /* Black defect auto correction ON/OFF */
  312. #define AUTO_WHT_ON_OFF 0x01 /* White define auto correction ON/OFF */
  313. /* DSP_CTRL3 */
  314. #define UV_MASK 0x80 /* UV output sequence option */
  315. #define UV_ON 0x80 /* ON */
  316. #define UV_OFF 0x00 /* OFF */
  317. #define CBAR_MASK 0x20 /* DSP Color bar mask */
  318. #define CBAR_ON 0x20 /* ON */
  319. #define CBAR_OFF 0x00 /* OFF */
  320. /* HSTART */
  321. #define HST_VGA 0x23
  322. #define HST_QVGA 0x3F
  323. /* HSIZE */
  324. #define HSZ_VGA 0xA0
  325. #define HSZ_QVGA 0x50
  326. /* VSTART */
  327. #define VST_VGA 0x07
  328. #define VST_QVGA 0x03
  329. /* VSIZE */
  330. #define VSZ_VGA 0xF0
  331. #define VSZ_QVGA 0x78
  332. /* HOUTSIZE */
  333. #define HOSZ_VGA 0xA0
  334. #define HOSZ_QVGA 0x50
  335. /* VOUTSIZE */
  336. #define VOSZ_VGA 0xF0
  337. #define VOSZ_QVGA 0x78
  338. /* DSPAUTO (DSP Auto Function ON/OFF Control) */
  339. #define AWB_ACTRL 0x80 /* AWB auto threshold control */
  340. #define DENOISE_ACTRL 0x40 /* De-noise auto threshold control */
  341. #define EDGE_ACTRL 0x20 /* Edge enhancement auto strength control */
  342. #define UV_ACTRL 0x10 /* UV adjust auto slope control */
  343. #define SCAL0_ACTRL 0x08 /* Auto scaling factor control */
  344. #define SCAL1_2_ACTRL 0x04 /* Auto scaling factor control */
  345. /*
  346. * ID
  347. */
  348. #define OV7720 0x7720
  349. #define OV7725 0x7721
  350. #define VERSION(pid, ver) ((pid<<8)|(ver&0xFF))
  351. /*
  352. * struct
  353. */
  354. struct regval_list {
  355. unsigned char reg_num;
  356. unsigned char value;
  357. };
  358. struct ov772x_color_format {
  359. enum v4l2_mbus_pixelcode code;
  360. enum v4l2_colorspace colorspace;
  361. u8 dsp3;
  362. u8 com3;
  363. u8 com7;
  364. };
  365. struct ov772x_win_size {
  366. char *name;
  367. __u32 width;
  368. __u32 height;
  369. unsigned char com7_bit;
  370. const struct regval_list *regs;
  371. };
  372. struct ov772x_priv {
  373. struct v4l2_subdev subdev;
  374. struct ov772x_camera_info *info;
  375. const struct ov772x_color_format *cfmt;
  376. const struct ov772x_win_size *win;
  377. int model;
  378. unsigned short flag_vflip:1;
  379. unsigned short flag_hflip:1;
  380. /* band_filter = COM8[5] ? 256 - BDBASE : 0 */
  381. unsigned short band_filter;
  382. };
  383. #define ENDMARKER { 0xff, 0xff }
  384. /*
  385. * register setting for window size
  386. */
  387. static const struct regval_list ov772x_qvga_regs[] = {
  388. { HSTART, HST_QVGA },
  389. { HSIZE, HSZ_QVGA },
  390. { VSTART, VST_QVGA },
  391. { VSIZE, VSZ_QVGA },
  392. { HOUTSIZE, HOSZ_QVGA },
  393. { VOUTSIZE, VOSZ_QVGA },
  394. ENDMARKER,
  395. };
  396. static const struct regval_list ov772x_vga_regs[] = {
  397. { HSTART, HST_VGA },
  398. { HSIZE, HSZ_VGA },
  399. { VSTART, VST_VGA },
  400. { VSIZE, VSZ_VGA },
  401. { HOUTSIZE, HOSZ_VGA },
  402. { VOUTSIZE, VOSZ_VGA },
  403. ENDMARKER,
  404. };
  405. /*
  406. * supported color format list
  407. */
  408. static const struct ov772x_color_format ov772x_cfmts[] = {
  409. {
  410. .code = V4L2_MBUS_FMT_YUYV8_2X8_LE,
  411. .colorspace = V4L2_COLORSPACE_JPEG,
  412. .dsp3 = 0x0,
  413. .com3 = SWAP_YUV,
  414. .com7 = OFMT_YUV,
  415. },
  416. {
  417. .code = V4L2_MBUS_FMT_YVYU8_2X8_LE,
  418. .colorspace = V4L2_COLORSPACE_JPEG,
  419. .dsp3 = UV_ON,
  420. .com3 = SWAP_YUV,
  421. .com7 = OFMT_YUV,
  422. },
  423. {
  424. .code = V4L2_MBUS_FMT_YUYV8_2X8_BE,
  425. .colorspace = V4L2_COLORSPACE_JPEG,
  426. .dsp3 = 0x0,
  427. .com3 = 0x0,
  428. .com7 = OFMT_YUV,
  429. },
  430. {
  431. .code = V4L2_MBUS_FMT_RGB555_2X8_PADHI_LE,
  432. .colorspace = V4L2_COLORSPACE_SRGB,
  433. .dsp3 = 0x0,
  434. .com3 = SWAP_RGB,
  435. .com7 = FMT_RGB555 | OFMT_RGB,
  436. },
  437. {
  438. .code = V4L2_MBUS_FMT_RGB555_2X8_PADHI_BE,
  439. .colorspace = V4L2_COLORSPACE_SRGB,
  440. .dsp3 = 0x0,
  441. .com3 = 0x0,
  442. .com7 = FMT_RGB555 | OFMT_RGB,
  443. },
  444. {
  445. .code = V4L2_MBUS_FMT_RGB565_2X8_LE,
  446. .colorspace = V4L2_COLORSPACE_SRGB,
  447. .dsp3 = 0x0,
  448. .com3 = SWAP_RGB,
  449. .com7 = FMT_RGB565 | OFMT_RGB,
  450. },
  451. {
  452. .code = V4L2_MBUS_FMT_RGB565_2X8_BE,
  453. .colorspace = V4L2_COLORSPACE_SRGB,
  454. .dsp3 = 0x0,
  455. .com3 = 0x0,
  456. .com7 = FMT_RGB565 | OFMT_RGB,
  457. },
  458. };
  459. /*
  460. * window size list
  461. */
  462. #define VGA_WIDTH 640
  463. #define VGA_HEIGHT 480
  464. #define QVGA_WIDTH 320
  465. #define QVGA_HEIGHT 240
  466. #define MAX_WIDTH VGA_WIDTH
  467. #define MAX_HEIGHT VGA_HEIGHT
  468. static const struct ov772x_win_size ov772x_win_vga = {
  469. .name = "VGA",
  470. .width = VGA_WIDTH,
  471. .height = VGA_HEIGHT,
  472. .com7_bit = SLCT_VGA,
  473. .regs = ov772x_vga_regs,
  474. };
  475. static const struct ov772x_win_size ov772x_win_qvga = {
  476. .name = "QVGA",
  477. .width = QVGA_WIDTH,
  478. .height = QVGA_HEIGHT,
  479. .com7_bit = SLCT_QVGA,
  480. .regs = ov772x_qvga_regs,
  481. };
  482. static const struct v4l2_queryctrl ov772x_controls[] = {
  483. {
  484. .id = V4L2_CID_VFLIP,
  485. .type = V4L2_CTRL_TYPE_BOOLEAN,
  486. .name = "Flip Vertically",
  487. .minimum = 0,
  488. .maximum = 1,
  489. .step = 1,
  490. .default_value = 0,
  491. },
  492. {
  493. .id = V4L2_CID_HFLIP,
  494. .type = V4L2_CTRL_TYPE_BOOLEAN,
  495. .name = "Flip Horizontally",
  496. .minimum = 0,
  497. .maximum = 1,
  498. .step = 1,
  499. .default_value = 0,
  500. },
  501. {
  502. .id = V4L2_CID_BAND_STOP_FILTER,
  503. .type = V4L2_CTRL_TYPE_INTEGER,
  504. .name = "Band-stop filter",
  505. .minimum = 0,
  506. .maximum = 256,
  507. .step = 1,
  508. .default_value = 0,
  509. },
  510. };
  511. /*
  512. * general function
  513. */
  514. static struct ov772x_priv *to_ov772x(const struct i2c_client *client)
  515. {
  516. return container_of(i2c_get_clientdata(client), struct ov772x_priv,
  517. subdev);
  518. }
  519. static int ov772x_write_array(struct i2c_client *client,
  520. const struct regval_list *vals)
  521. {
  522. while (vals->reg_num != 0xff) {
  523. int ret = i2c_smbus_write_byte_data(client,
  524. vals->reg_num,
  525. vals->value);
  526. if (ret < 0)
  527. return ret;
  528. vals++;
  529. }
  530. return 0;
  531. }
  532. static int ov772x_mask_set(struct i2c_client *client,
  533. u8 command,
  534. u8 mask,
  535. u8 set)
  536. {
  537. s32 val = i2c_smbus_read_byte_data(client, command);
  538. if (val < 0)
  539. return val;
  540. val &= ~mask;
  541. val |= set & mask;
  542. return i2c_smbus_write_byte_data(client, command, val);
  543. }
  544. static int ov772x_reset(struct i2c_client *client)
  545. {
  546. int ret = i2c_smbus_write_byte_data(client, COM7, SCCB_RESET);
  547. msleep(1);
  548. return ret;
  549. }
  550. /*
  551. * soc_camera_ops function
  552. */
  553. static int ov772x_s_stream(struct v4l2_subdev *sd, int enable)
  554. {
  555. struct i2c_client *client = sd->priv;
  556. struct ov772x_priv *priv = to_ov772x(client);
  557. if (!enable) {
  558. ov772x_mask_set(client, COM2, SOFT_SLEEP_MODE, SOFT_SLEEP_MODE);
  559. return 0;
  560. }
  561. if (!priv->win || !priv->cfmt) {
  562. dev_err(&client->dev, "norm or win select error\n");
  563. return -EPERM;
  564. }
  565. ov772x_mask_set(client, COM2, SOFT_SLEEP_MODE, 0);
  566. dev_dbg(&client->dev, "format %d, win %s\n",
  567. priv->cfmt->code, priv->win->name);
  568. return 0;
  569. }
  570. static int ov772x_set_bus_param(struct soc_camera_device *icd,
  571. unsigned long flags)
  572. {
  573. return 0;
  574. }
  575. static unsigned long ov772x_query_bus_param(struct soc_camera_device *icd)
  576. {
  577. struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
  578. struct ov772x_priv *priv = i2c_get_clientdata(client);
  579. struct soc_camera_link *icl = to_soc_camera_link(icd);
  580. unsigned long flags = SOCAM_PCLK_SAMPLE_RISING | SOCAM_MASTER |
  581. SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_HIGH |
  582. SOCAM_DATA_ACTIVE_HIGH | priv->info->buswidth;
  583. return soc_camera_apply_sensor_flags(icl, flags);
  584. }
  585. static int ov772x_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  586. {
  587. struct i2c_client *client = sd->priv;
  588. struct ov772x_priv *priv = to_ov772x(client);
  589. switch (ctrl->id) {
  590. case V4L2_CID_VFLIP:
  591. ctrl->value = priv->flag_vflip;
  592. break;
  593. case V4L2_CID_HFLIP:
  594. ctrl->value = priv->flag_hflip;
  595. break;
  596. case V4L2_CID_BAND_STOP_FILTER:
  597. ctrl->value = priv->band_filter;
  598. break;
  599. }
  600. return 0;
  601. }
  602. static int ov772x_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  603. {
  604. struct i2c_client *client = sd->priv;
  605. struct ov772x_priv *priv = to_ov772x(client);
  606. int ret = 0;
  607. u8 val;
  608. switch (ctrl->id) {
  609. case V4L2_CID_VFLIP:
  610. val = ctrl->value ? VFLIP_IMG : 0x00;
  611. priv->flag_vflip = ctrl->value;
  612. if (priv->info->flags & OV772X_FLAG_VFLIP)
  613. val ^= VFLIP_IMG;
  614. ret = ov772x_mask_set(client, COM3, VFLIP_IMG, val);
  615. break;
  616. case V4L2_CID_HFLIP:
  617. val = ctrl->value ? HFLIP_IMG : 0x00;
  618. priv->flag_hflip = ctrl->value;
  619. if (priv->info->flags & OV772X_FLAG_HFLIP)
  620. val ^= HFLIP_IMG;
  621. ret = ov772x_mask_set(client, COM3, HFLIP_IMG, val);
  622. break;
  623. case V4L2_CID_BAND_STOP_FILTER:
  624. if ((unsigned)ctrl->value > 256)
  625. ctrl->value = 256;
  626. if (ctrl->value == priv->band_filter)
  627. break;
  628. if (!ctrl->value) {
  629. /* Switch the filter off, it is on now */
  630. ret = ov772x_mask_set(client, BDBASE, 0xff, 0xff);
  631. if (!ret)
  632. ret = ov772x_mask_set(client, COM8,
  633. BNDF_ON_OFF, 0);
  634. } else {
  635. /* Switch the filter on, set AEC low limit */
  636. val = 256 - ctrl->value;
  637. ret = ov772x_mask_set(client, COM8,
  638. BNDF_ON_OFF, BNDF_ON_OFF);
  639. if (!ret)
  640. ret = ov772x_mask_set(client, BDBASE,
  641. 0xff, val);
  642. }
  643. if (!ret)
  644. priv->band_filter = ctrl->value;
  645. break;
  646. }
  647. return ret;
  648. }
  649. static int ov772x_g_chip_ident(struct v4l2_subdev *sd,
  650. struct v4l2_dbg_chip_ident *id)
  651. {
  652. struct i2c_client *client = sd->priv;
  653. struct ov772x_priv *priv = to_ov772x(client);
  654. id->ident = priv->model;
  655. id->revision = 0;
  656. return 0;
  657. }
  658. #ifdef CONFIG_VIDEO_ADV_DEBUG
  659. static int ov772x_g_register(struct v4l2_subdev *sd,
  660. struct v4l2_dbg_register *reg)
  661. {
  662. struct i2c_client *client = sd->priv;
  663. int ret;
  664. reg->size = 1;
  665. if (reg->reg > 0xff)
  666. return -EINVAL;
  667. ret = i2c_smbus_read_byte_data(client, reg->reg);
  668. if (ret < 0)
  669. return ret;
  670. reg->val = (__u64)ret;
  671. return 0;
  672. }
  673. static int ov772x_s_register(struct v4l2_subdev *sd,
  674. struct v4l2_dbg_register *reg)
  675. {
  676. struct i2c_client *client = sd->priv;
  677. if (reg->reg > 0xff ||
  678. reg->val > 0xff)
  679. return -EINVAL;
  680. return i2c_smbus_write_byte_data(client, reg->reg, reg->val);
  681. }
  682. #endif
  683. static const struct ov772x_win_size *ov772x_select_win(u32 width, u32 height)
  684. {
  685. __u32 diff;
  686. const struct ov772x_win_size *win;
  687. /* default is QVGA */
  688. diff = abs(width - ov772x_win_qvga.width) +
  689. abs(height - ov772x_win_qvga.height);
  690. win = &ov772x_win_qvga;
  691. /* VGA */
  692. if (diff >
  693. abs(width - ov772x_win_vga.width) +
  694. abs(height - ov772x_win_vga.height))
  695. win = &ov772x_win_vga;
  696. return win;
  697. }
  698. static int ov772x_set_params(struct i2c_client *client, u32 *width, u32 *height,
  699. enum v4l2_mbus_pixelcode code)
  700. {
  701. struct ov772x_priv *priv = to_ov772x(client);
  702. int ret = -EINVAL;
  703. u8 val;
  704. int i;
  705. /*
  706. * select format
  707. */
  708. priv->cfmt = NULL;
  709. for (i = 0; i < ARRAY_SIZE(ov772x_cfmts); i++) {
  710. if (code == ov772x_cfmts[i].code) {
  711. priv->cfmt = ov772x_cfmts + i;
  712. break;
  713. }
  714. }
  715. if (!priv->cfmt)
  716. goto ov772x_set_fmt_error;
  717. /*
  718. * select win
  719. */
  720. priv->win = ov772x_select_win(*width, *height);
  721. /*
  722. * reset hardware
  723. */
  724. ov772x_reset(client);
  725. /*
  726. * Edge Ctrl
  727. */
  728. if (priv->info->edgectrl.strength & OV772X_MANUAL_EDGE_CTRL) {
  729. /*
  730. * Manual Edge Control Mode
  731. *
  732. * Edge auto strength bit is set by default.
  733. * Remove it when manual mode.
  734. */
  735. ret = ov772x_mask_set(client, DSPAUTO, EDGE_ACTRL, 0x00);
  736. if (ret < 0)
  737. goto ov772x_set_fmt_error;
  738. ret = ov772x_mask_set(client,
  739. EDGE_TRSHLD, EDGE_THRESHOLD_MASK,
  740. priv->info->edgectrl.threshold);
  741. if (ret < 0)
  742. goto ov772x_set_fmt_error;
  743. ret = ov772x_mask_set(client,
  744. EDGE_STRNGT, EDGE_STRENGTH_MASK,
  745. priv->info->edgectrl.strength);
  746. if (ret < 0)
  747. goto ov772x_set_fmt_error;
  748. } else if (priv->info->edgectrl.upper > priv->info->edgectrl.lower) {
  749. /*
  750. * Auto Edge Control Mode
  751. *
  752. * set upper and lower limit
  753. */
  754. ret = ov772x_mask_set(client,
  755. EDGE_UPPER, EDGE_UPPER_MASK,
  756. priv->info->edgectrl.upper);
  757. if (ret < 0)
  758. goto ov772x_set_fmt_error;
  759. ret = ov772x_mask_set(client,
  760. EDGE_LOWER, EDGE_LOWER_MASK,
  761. priv->info->edgectrl.lower);
  762. if (ret < 0)
  763. goto ov772x_set_fmt_error;
  764. }
  765. /*
  766. * set size format
  767. */
  768. ret = ov772x_write_array(client, priv->win->regs);
  769. if (ret < 0)
  770. goto ov772x_set_fmt_error;
  771. /*
  772. * set DSP_CTRL3
  773. */
  774. val = priv->cfmt->dsp3;
  775. if (val) {
  776. ret = ov772x_mask_set(client,
  777. DSP_CTRL3, UV_MASK, val);
  778. if (ret < 0)
  779. goto ov772x_set_fmt_error;
  780. }
  781. /*
  782. * set COM3
  783. */
  784. val = priv->cfmt->com3;
  785. if (priv->info->flags & OV772X_FLAG_VFLIP)
  786. val |= VFLIP_IMG;
  787. if (priv->info->flags & OV772X_FLAG_HFLIP)
  788. val |= HFLIP_IMG;
  789. if (priv->flag_vflip)
  790. val ^= VFLIP_IMG;
  791. if (priv->flag_hflip)
  792. val ^= HFLIP_IMG;
  793. ret = ov772x_mask_set(client,
  794. COM3, SWAP_MASK | IMG_MASK, val);
  795. if (ret < 0)
  796. goto ov772x_set_fmt_error;
  797. /*
  798. * set COM7
  799. */
  800. val = priv->win->com7_bit | priv->cfmt->com7;
  801. ret = ov772x_mask_set(client,
  802. COM7, SLCT_MASK | FMT_MASK | OFMT_MASK,
  803. val);
  804. if (ret < 0)
  805. goto ov772x_set_fmt_error;
  806. /*
  807. * set COM8
  808. */
  809. if (priv->band_filter) {
  810. ret = ov772x_mask_set(client, COM8, BNDF_ON_OFF, 1);
  811. if (!ret)
  812. ret = ov772x_mask_set(client, BDBASE,
  813. 0xff, 256 - priv->band_filter);
  814. if (ret < 0)
  815. goto ov772x_set_fmt_error;
  816. }
  817. *width = priv->win->width;
  818. *height = priv->win->height;
  819. return ret;
  820. ov772x_set_fmt_error:
  821. ov772x_reset(client);
  822. priv->win = NULL;
  823. priv->cfmt = NULL;
  824. return ret;
  825. }
  826. static int ov772x_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  827. {
  828. a->c.left = 0;
  829. a->c.top = 0;
  830. a->c.width = VGA_WIDTH;
  831. a->c.height = VGA_HEIGHT;
  832. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  833. return 0;
  834. }
  835. static int ov772x_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
  836. {
  837. a->bounds.left = 0;
  838. a->bounds.top = 0;
  839. a->bounds.width = VGA_WIDTH;
  840. a->bounds.height = VGA_HEIGHT;
  841. a->defrect = a->bounds;
  842. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  843. a->pixelaspect.numerator = 1;
  844. a->pixelaspect.denominator = 1;
  845. return 0;
  846. }
  847. static int ov772x_g_fmt(struct v4l2_subdev *sd,
  848. struct v4l2_mbus_framefmt *mf)
  849. {
  850. struct i2c_client *client = sd->priv;
  851. struct ov772x_priv *priv = to_ov772x(client);
  852. if (!priv->win || !priv->cfmt) {
  853. u32 width = VGA_WIDTH, height = VGA_HEIGHT;
  854. int ret = ov772x_set_params(client, &width, &height,
  855. V4L2_MBUS_FMT_YUYV8_2X8_LE);
  856. if (ret < 0)
  857. return ret;
  858. }
  859. mf->width = priv->win->width;
  860. mf->height = priv->win->height;
  861. mf->code = priv->cfmt->code;
  862. mf->colorspace = priv->cfmt->colorspace;
  863. mf->field = V4L2_FIELD_NONE;
  864. return 0;
  865. }
  866. static int ov772x_s_fmt(struct v4l2_subdev *sd,
  867. struct v4l2_mbus_framefmt *mf)
  868. {
  869. struct i2c_client *client = sd->priv;
  870. struct ov772x_priv *priv = to_ov772x(client);
  871. int ret = ov772x_set_params(client, &mf->width, &mf->height,
  872. mf->code);
  873. if (!ret)
  874. mf->colorspace = priv->cfmt->colorspace;
  875. return ret;
  876. }
  877. static int ov772x_try_fmt(struct v4l2_subdev *sd,
  878. struct v4l2_mbus_framefmt *mf)
  879. {
  880. struct i2c_client *client = sd->priv;
  881. struct ov772x_priv *priv = to_ov772x(client);
  882. const struct ov772x_win_size *win;
  883. int i;
  884. /*
  885. * select suitable win
  886. */
  887. win = ov772x_select_win(mf->width, mf->height);
  888. mf->width = win->width;
  889. mf->height = win->height;
  890. mf->field = V4L2_FIELD_NONE;
  891. for (i = 0; i < ARRAY_SIZE(ov772x_cfmts); i++)
  892. if (mf->code == ov772x_cfmts[i].code)
  893. break;
  894. if (i == ARRAY_SIZE(ov772x_cfmts)) {
  895. /* Unsupported format requested. Propose either */
  896. if (priv->cfmt) {
  897. /* the current one or */
  898. mf->colorspace = priv->cfmt->colorspace;
  899. mf->code = priv->cfmt->code;
  900. } else {
  901. /* the default one */
  902. mf->colorspace = ov772x_cfmts[0].colorspace;
  903. mf->code = ov772x_cfmts[0].code;
  904. }
  905. } else {
  906. /* Also return the colorspace */
  907. mf->colorspace = ov772x_cfmts[i].colorspace;
  908. }
  909. return 0;
  910. }
  911. static int ov772x_video_probe(struct soc_camera_device *icd,
  912. struct i2c_client *client)
  913. {
  914. struct ov772x_priv *priv = to_ov772x(client);
  915. u8 pid, ver;
  916. const char *devname;
  917. /*
  918. * We must have a parent by now. And it cannot be a wrong one.
  919. * So this entire test is completely redundant.
  920. */
  921. if (!icd->dev.parent ||
  922. to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
  923. return -ENODEV;
  924. /*
  925. * ov772x only use 8 or 10 bit bus width
  926. */
  927. if (SOCAM_DATAWIDTH_10 != priv->info->buswidth &&
  928. SOCAM_DATAWIDTH_8 != priv->info->buswidth) {
  929. dev_err(&client->dev, "bus width error\n");
  930. return -ENODEV;
  931. }
  932. /*
  933. * check and show product ID and manufacturer ID
  934. */
  935. pid = i2c_smbus_read_byte_data(client, PID);
  936. ver = i2c_smbus_read_byte_data(client, VER);
  937. switch (VERSION(pid, ver)) {
  938. case OV7720:
  939. devname = "ov7720";
  940. priv->model = V4L2_IDENT_OV7720;
  941. break;
  942. case OV7725:
  943. devname = "ov7725";
  944. priv->model = V4L2_IDENT_OV7725;
  945. break;
  946. default:
  947. dev_err(&client->dev,
  948. "Product ID error %x:%x\n", pid, ver);
  949. return -ENODEV;
  950. }
  951. dev_info(&client->dev,
  952. "%s Product ID %0x:%0x Manufacturer ID %x:%x\n",
  953. devname,
  954. pid,
  955. ver,
  956. i2c_smbus_read_byte_data(client, MIDH),
  957. i2c_smbus_read_byte_data(client, MIDL));
  958. return 0;
  959. }
  960. static struct soc_camera_ops ov772x_ops = {
  961. .set_bus_param = ov772x_set_bus_param,
  962. .query_bus_param = ov772x_query_bus_param,
  963. .controls = ov772x_controls,
  964. .num_controls = ARRAY_SIZE(ov772x_controls),
  965. };
  966. static struct v4l2_subdev_core_ops ov772x_subdev_core_ops = {
  967. .g_ctrl = ov772x_g_ctrl,
  968. .s_ctrl = ov772x_s_ctrl,
  969. .g_chip_ident = ov772x_g_chip_ident,
  970. #ifdef CONFIG_VIDEO_ADV_DEBUG
  971. .g_register = ov772x_g_register,
  972. .s_register = ov772x_s_register,
  973. #endif
  974. };
  975. static int ov772x_enum_fmt(struct v4l2_subdev *sd, int index,
  976. enum v4l2_mbus_pixelcode *code)
  977. {
  978. if ((unsigned int)index >= ARRAY_SIZE(ov772x_cfmts))
  979. return -EINVAL;
  980. *code = ov772x_cfmts[index].code;
  981. return 0;
  982. }
  983. static struct v4l2_subdev_video_ops ov772x_subdev_video_ops = {
  984. .s_stream = ov772x_s_stream,
  985. .g_mbus_fmt = ov772x_g_fmt,
  986. .s_mbus_fmt = ov772x_s_fmt,
  987. .try_mbus_fmt = ov772x_try_fmt,
  988. .cropcap = ov772x_cropcap,
  989. .g_crop = ov772x_g_crop,
  990. .enum_mbus_fmt = ov772x_enum_fmt,
  991. };
  992. static struct v4l2_subdev_ops ov772x_subdev_ops = {
  993. .core = &ov772x_subdev_core_ops,
  994. .video = &ov772x_subdev_video_ops,
  995. };
  996. /*
  997. * i2c_driver function
  998. */
  999. static int ov772x_probe(struct i2c_client *client,
  1000. const struct i2c_device_id *did)
  1001. {
  1002. struct ov772x_priv *priv;
  1003. struct ov772x_camera_info *info;
  1004. struct soc_camera_device *icd = client->dev.platform_data;
  1005. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  1006. struct soc_camera_link *icl;
  1007. int ret;
  1008. if (!icd) {
  1009. dev_err(&client->dev, "OV772X: missing soc-camera data!\n");
  1010. return -EINVAL;
  1011. }
  1012. icl = to_soc_camera_link(icd);
  1013. if (!icl || !icl->priv)
  1014. return -EINVAL;
  1015. info = icl->priv;
  1016. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  1017. dev_err(&adapter->dev,
  1018. "I2C-Adapter doesn't support "
  1019. "I2C_FUNC_SMBUS_BYTE_DATA\n");
  1020. return -EIO;
  1021. }
  1022. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  1023. if (!priv)
  1024. return -ENOMEM;
  1025. priv->info = info;
  1026. v4l2_i2c_subdev_init(&priv->subdev, client, &ov772x_subdev_ops);
  1027. icd->ops = &ov772x_ops;
  1028. ret = ov772x_video_probe(icd, client);
  1029. if (ret) {
  1030. icd->ops = NULL;
  1031. i2c_set_clientdata(client, NULL);
  1032. kfree(priv);
  1033. }
  1034. return ret;
  1035. }
  1036. static int ov772x_remove(struct i2c_client *client)
  1037. {
  1038. struct ov772x_priv *priv = to_ov772x(client);
  1039. struct soc_camera_device *icd = client->dev.platform_data;
  1040. icd->ops = NULL;
  1041. i2c_set_clientdata(client, NULL);
  1042. kfree(priv);
  1043. return 0;
  1044. }
  1045. static const struct i2c_device_id ov772x_id[] = {
  1046. { "ov772x", 0 },
  1047. { }
  1048. };
  1049. MODULE_DEVICE_TABLE(i2c, ov772x_id);
  1050. static struct i2c_driver ov772x_i2c_driver = {
  1051. .driver = {
  1052. .name = "ov772x",
  1053. },
  1054. .probe = ov772x_probe,
  1055. .remove = ov772x_remove,
  1056. .id_table = ov772x_id,
  1057. };
  1058. /*
  1059. * module function
  1060. */
  1061. static int __init ov772x_module_init(void)
  1062. {
  1063. return i2c_add_driver(&ov772x_i2c_driver);
  1064. }
  1065. static void __exit ov772x_module_exit(void)
  1066. {
  1067. i2c_del_driver(&ov772x_i2c_driver);
  1068. }
  1069. module_init(ov772x_module_init);
  1070. module_exit(ov772x_module_exit);
  1071. MODULE_DESCRIPTION("SoC Camera driver for ov772x");
  1072. MODULE_AUTHOR("Kuninori Morimoto");
  1073. MODULE_LICENSE("GPL v2");