ov772x.c 34 KB

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