ov772x.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  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-common.h>
  25. #include <media/soc_camera.h>
  26. #include <media/ov772x.h>
  27. /*
  28. * register offset
  29. */
  30. #define GAIN 0x00 /* AGC - Gain control gain setting */
  31. #define BLUE 0x01 /* AWB - Blue channel gain setting */
  32. #define RED 0x02 /* AWB - Red channel gain setting */
  33. #define GREEN 0x03 /* AWB - Green channel gain setting */
  34. #define COM1 0x04 /* Common control 1 */
  35. #define BAVG 0x05 /* U/B Average Level */
  36. #define GAVG 0x06 /* Y/Gb Average Level */
  37. #define RAVG 0x07 /* V/R Average Level */
  38. #define AECH 0x08 /* Exposure Value - AEC MSBs */
  39. #define COM2 0x09 /* Common control 2 */
  40. #define PID 0x0A /* Product ID Number MSB */
  41. #define VER 0x0B /* Product ID Number LSB */
  42. #define COM3 0x0C /* Common control 3 */
  43. #define COM4 0x0D /* Common control 4 */
  44. #define COM5 0x0E /* Common control 5 */
  45. #define COM6 0x0F /* Common control 6 */
  46. #define AEC 0x10 /* Exposure Value */
  47. #define CLKRC 0x11 /* Internal clock */
  48. #define COM7 0x12 /* Common control 7 */
  49. #define COM8 0x13 /* Common control 8 */
  50. #define COM9 0x14 /* Common control 9 */
  51. #define COM10 0x15 /* Common control 10 */
  52. #define REG16 0x16 /* Register 16 */
  53. #define HSTART 0x17 /* Horizontal sensor size */
  54. #define HSIZE 0x18 /* Horizontal frame (HREF column) end high 8-bit */
  55. #define VSTART 0x19 /* Vertical frame (row) start high 8-bit */
  56. #define VSIZE 0x1A /* Vertical sensor size */
  57. #define PSHFT 0x1B /* Data format - pixel delay select */
  58. #define MIDH 0x1C /* Manufacturer ID byte - high */
  59. #define MIDL 0x1D /* Manufacturer ID byte - low */
  60. #define LAEC 0x1F /* Fine AEC value */
  61. #define COM11 0x20 /* Common control 11 */
  62. #define BDBASE 0x22 /* Banding filter Minimum AEC value */
  63. #define DBSTEP 0x23 /* Banding filter Maximum Setp */
  64. #define AEW 0x24 /* AGC/AEC - Stable operating region (upper limit) */
  65. #define AEB 0x25 /* AGC/AEC - Stable operating region (lower limit) */
  66. #define VPT 0x26 /* AGC/AEC Fast mode operating region */
  67. #define REG28 0x28 /* Register 28 */
  68. #define HOUTSIZE 0x29 /* Horizontal data output size MSBs */
  69. #define EXHCH 0x2A /* Dummy pixel insert MSB */
  70. #define EXHCL 0x2B /* Dummy pixel insert LSB */
  71. #define VOUTSIZE 0x2C /* Vertical data output size MSBs */
  72. #define ADVFL 0x2D /* LSB of insert dummy lines in Vertical direction */
  73. #define ADVFH 0x2E /* MSG of insert dummy lines in Vertical direction */
  74. #define YAVE 0x2F /* Y/G Channel Average value */
  75. #define LUMHTH 0x30 /* Histogram AEC/AGC Luminance high level threshold */
  76. #define LUMLTH 0x31 /* Histogram AEC/AGC Luminance low level threshold */
  77. #define HREF 0x32 /* Image start and size control */
  78. #define DM_LNL 0x33 /* Dummy line low 8 bits */
  79. #define DM_LNH 0x34 /* Dummy line high 8 bits */
  80. #define ADOFF_B 0x35 /* AD offset compensation value for B channel */
  81. #define ADOFF_R 0x36 /* AD offset compensation value for R channel */
  82. #define ADOFF_GB 0x37 /* AD offset compensation value for Gb channel */
  83. #define ADOFF_GR 0x38 /* AD offset compensation value for Gr channel */
  84. #define OFF_B 0x39 /* Analog process B channel offset value */
  85. #define OFF_R 0x3A /* Analog process R channel offset value */
  86. #define OFF_GB 0x3B /* Analog process Gb channel offset value */
  87. #define OFF_GR 0x3C /* Analog process Gr channel offset value */
  88. #define COM12 0x3D /* Common control 12 */
  89. #define COM13 0x3E /* Common control 13 */
  90. #define COM14 0x3F /* Common control 14 */
  91. #define COM15 0x40 /* Common control 15*/
  92. #define COM16 0x41 /* Common control 16 */
  93. #define TGT_B 0x42 /* BLC blue channel target value */
  94. #define TGT_R 0x43 /* BLC red channel target value */
  95. #define TGT_GB 0x44 /* BLC Gb channel target value */
  96. #define TGT_GR 0x45 /* BLC Gr channel target value */
  97. /* for ov7720 */
  98. #define LCC0 0x46 /* Lens correction control 0 */
  99. #define LCC1 0x47 /* Lens correction option 1 - X coordinate */
  100. #define LCC2 0x48 /* Lens correction option 2 - Y coordinate */
  101. #define LCC3 0x49 /* Lens correction option 3 */
  102. #define LCC4 0x4A /* Lens correction option 4 - radius of the circular */
  103. #define LCC5 0x4B /* Lens correction option 5 */
  104. #define LCC6 0x4C /* Lens correction option 6 */
  105. /* for ov7725 */
  106. #define LC_CTR 0x46 /* Lens correction control */
  107. #define LC_XC 0x47 /* X coordinate of lens correction center relative */
  108. #define LC_YC 0x48 /* Y coordinate of lens correction center relative */
  109. #define LC_COEF 0x49 /* Lens correction coefficient */
  110. #define LC_RADI 0x4A /* Lens correction radius */
  111. #define LC_COEFB 0x4B /* Lens B channel compensation coefficient */
  112. #define LC_COEFR 0x4C /* Lens R channel compensation coefficient */
  113. #define FIXGAIN 0x4D /* Analog fix gain amplifer */
  114. #define AREF0 0x4E /* Sensor reference control */
  115. #define AREF1 0x4F /* Sensor reference current control */
  116. #define AREF2 0x50 /* Analog reference control */
  117. #define AREF3 0x51 /* ADC reference control */
  118. #define AREF4 0x52 /* ADC reference control */
  119. #define AREF5 0x53 /* ADC reference control */
  120. #define AREF6 0x54 /* Analog reference control */
  121. #define AREF7 0x55 /* Analog reference control */
  122. #define UFIX 0x60 /* U channel fixed value output */
  123. #define VFIX 0x61 /* V channel fixed value output */
  124. #define AWBB_BLK 0x62 /* AWB option for advanced AWB */
  125. #define AWB_CTRL0 0x63 /* AWB control byte 0 */
  126. #define DSP_CTRL1 0x64 /* DSP control byte 1 */
  127. #define DSP_CTRL2 0x65 /* DSP control byte 2 */
  128. #define DSP_CTRL3 0x66 /* DSP control byte 3 */
  129. #define DSP_CTRL4 0x67 /* DSP control byte 4 */
  130. #define AWB_BIAS 0x68 /* AWB BLC level clip */
  131. #define AWB_CTRL1 0x69 /* AWB control 1 */
  132. #define AWB_CTRL2 0x6A /* AWB control 2 */
  133. #define AWB_CTRL3 0x6B /* AWB control 3 */
  134. #define AWB_CTRL4 0x6C /* AWB control 4 */
  135. #define AWB_CTRL5 0x6D /* AWB control 5 */
  136. #define AWB_CTRL6 0x6E /* AWB control 6 */
  137. #define AWB_CTRL7 0x6F /* AWB control 7 */
  138. #define AWB_CTRL8 0x70 /* AWB control 8 */
  139. #define AWB_CTRL9 0x71 /* AWB control 9 */
  140. #define AWB_CTRL10 0x72 /* AWB control 10 */
  141. #define AWB_CTRL11 0x73 /* AWB control 11 */
  142. #define AWB_CTRL12 0x74 /* AWB control 12 */
  143. #define AWB_CTRL13 0x75 /* AWB control 13 */
  144. #define AWB_CTRL14 0x76 /* AWB control 14 */
  145. #define AWB_CTRL15 0x77 /* AWB control 15 */
  146. #define AWB_CTRL16 0x78 /* AWB control 16 */
  147. #define AWB_CTRL17 0x79 /* AWB control 17 */
  148. #define AWB_CTRL18 0x7A /* AWB control 18 */
  149. #define AWB_CTRL19 0x7B /* AWB control 19 */
  150. #define AWB_CTRL20 0x7C /* AWB control 20 */
  151. #define AWB_CTRL21 0x7D /* AWB control 21 */
  152. #define GAM1 0x7E /* Gamma Curve 1st segment input end point */
  153. #define GAM2 0x7F /* Gamma Curve 2nd segment input end point */
  154. #define GAM3 0x80 /* Gamma Curve 3rd segment input end point */
  155. #define GAM4 0x81 /* Gamma Curve 4th segment input end point */
  156. #define GAM5 0x82 /* Gamma Curve 5th segment input end point */
  157. #define GAM6 0x83 /* Gamma Curve 6th segment input end point */
  158. #define GAM7 0x84 /* Gamma Curve 7th segment input end point */
  159. #define GAM8 0x85 /* Gamma Curve 8th segment input end point */
  160. #define GAM9 0x86 /* Gamma Curve 9th segment input end point */
  161. #define GAM10 0x87 /* Gamma Curve 10th segment input end point */
  162. #define GAM11 0x88 /* Gamma Curve 11th segment input end point */
  163. #define GAM12 0x89 /* Gamma Curve 12th segment input end point */
  164. #define GAM13 0x8A /* Gamma Curve 13th segment input end point */
  165. #define GAM14 0x8B /* Gamma Curve 14th segment input end point */
  166. #define GAM15 0x8C /* Gamma Curve 15th segment input end point */
  167. #define SLOP 0x8D /* Gamma curve highest segment slope */
  168. #define DNSTH 0x8E /* De-noise threshold */
  169. #define EDGE0 0x8F /* Edge enhancement control 0 */
  170. #define EDGE1 0x90 /* Edge enhancement control 1 */
  171. #define DNSOFF 0x91 /* Auto De-noise threshold control */
  172. #define EDGE2 0x92 /* Edge enhancement strength low point control */
  173. #define EDGE3 0x93 /* Edge enhancement strength high point control */
  174. #define MTX1 0x94 /* Matrix coefficient 1 */
  175. #define MTX2 0x95 /* Matrix coefficient 2 */
  176. #define MTX3 0x96 /* Matrix coefficient 3 */
  177. #define MTX4 0x97 /* Matrix coefficient 4 */
  178. #define MTX5 0x98 /* Matrix coefficient 5 */
  179. #define MTX6 0x99 /* Matrix coefficient 6 */
  180. #define MTX_CTRL 0x9A /* Matrix control */
  181. #define BRIGHT 0x9B /* Brightness control */
  182. #define CNTRST 0x9C /* Contrast contrast */
  183. #define CNTRST_CTRL 0x9D /* Contrast contrast center */
  184. #define UVAD_J0 0x9E /* Auto UV adjust contrast 0 */
  185. #define UVAD_J1 0x9F /* Auto UV adjust contrast 1 */
  186. #define SCAL0 0xA0 /* Scaling control 0 */
  187. #define SCAL1 0xA1 /* Scaling control 1 */
  188. #define SCAL2 0xA2 /* Scaling control 2 */
  189. #define FIFODLYM 0xA3 /* FIFO manual mode delay control */
  190. #define FIFODLYA 0xA4 /* FIFO auto mode delay control */
  191. #define SDE 0xA6 /* Special digital effect control */
  192. #define USAT 0xA7 /* U component saturation control */
  193. #define VSAT 0xA8 /* V component saturation control */
  194. /* for ov7720 */
  195. #define HUE0 0xA9 /* Hue control 0 */
  196. #define HUE1 0xAA /* Hue control 1 */
  197. /* for ov7725 */
  198. #define HUECOS 0xA9 /* Cosine value */
  199. #define HUESIN 0xAA /* Sine value */
  200. #define SIGN 0xAB /* Sign bit for Hue and contrast */
  201. #define DSPAUTO 0xAC /* DSP auto function ON/OFF control */
  202. /*
  203. * register detail
  204. */
  205. /* COM2 */
  206. #define SOFT_SLEEP_MODE 0x10 /* Soft sleep mode */
  207. /* Output drive capability */
  208. #define OCAP_1x 0x00 /* 1x */
  209. #define OCAP_2x 0x01 /* 2x */
  210. #define OCAP_3x 0x02 /* 3x */
  211. #define OCAP_4x 0x03 /* 4x */
  212. /* COM3 */
  213. #define SWAP_MASK 0x38
  214. #define VFIMG_ON_OFF 0x80 /* Vertical flip image ON/OFF selection */
  215. #define HMIMG_ON_OFF 0x40 /* Horizontal mirror image ON/OFF selection */
  216. #define SWAP_RGB 0x20 /* Swap B/R output sequence in RGB mode */
  217. #define SWAP_YUV 0x10 /* Swap Y/UV output sequence in YUV mode */
  218. #define SWAP_ML 0x08 /* Swap output MSB/LSB */
  219. /* Tri-state option for output clock */
  220. #define NOTRI_CLOCK 0x04 /* 0: Tri-state at this period */
  221. /* 1: No tri-state at this period */
  222. /* Tri-state option for output data */
  223. #define NOTRI_DATA 0x02 /* 0: Tri-state at this period */
  224. /* 1: No tri-state at this period */
  225. #define SCOLOR_TEST 0x01 /* Sensor color bar test pattern */
  226. /* COM4 */
  227. /* PLL frequency control */
  228. #define PLL_BYPASS 0x00 /* 00: Bypass PLL */
  229. #define PLL_4x 0x40 /* 01: PLL 4x */
  230. #define PLL_6x 0x80 /* 10: PLL 6x */
  231. #define PLL_8x 0xc0 /* 11: PLL 8x */
  232. /* AEC evaluate window */
  233. #define AEC_FULL 0x00 /* 00: Full window */
  234. #define AEC_1p2 0x10 /* 01: 1/2 window */
  235. #define AEC_1p4 0x20 /* 10: 1/4 window */
  236. #define AEC_2p3 0x30 /* 11: Low 2/3 window */
  237. /* COM5 */
  238. #define AFR_ON_OFF 0x80 /* Auto frame rate control ON/OFF selection */
  239. #define AFR_SPPED 0x40 /* Auto frame rate control speed slection */
  240. /* Auto frame rate max rate control */
  241. #define AFR_NO_RATE 0x00 /* No reduction of frame rate */
  242. #define AFR_1p2 0x10 /* Max reduction to 1/2 frame rate */
  243. #define AFR_1p4 0x20 /* Max reduction to 1/4 frame rate */
  244. #define AFR_1p8 0x30 /* Max reduction to 1/8 frame rate */
  245. /* Auto frame rate active point control */
  246. #define AF_2x 0x00 /* Add frame when AGC reaches 2x gain */
  247. #define AF_4x 0x04 /* Add frame when AGC reaches 4x gain */
  248. #define AF_8x 0x08 /* Add frame when AGC reaches 8x gain */
  249. #define AF_16x 0x0c /* Add frame when AGC reaches 16x gain */
  250. /* AEC max step control */
  251. #define AEC_NO_LIMIT 0x01 /* 0 : AEC incease step has limit */
  252. /* 1 : No limit to AEC increase step */
  253. /* COM7 */
  254. /* SCCB Register Reset */
  255. #define SCCB_RESET 0x80 /* 0 : No change */
  256. /* 1 : Resets all registers to default */
  257. /* Resolution selection */
  258. #define SLCT_MASK 0x40 /* Mask of VGA or QVGA */
  259. #define SLCT_VGA 0x00 /* 0 : VGA */
  260. #define SLCT_QVGA 0x40 /* 1 : QVGA */
  261. #define ITU656_ON_OFF 0x20 /* ITU656 protocol ON/OFF selection */
  262. /* RGB output format control */
  263. #define FMT_GBR422 0x00 /* 00 : GBR 4:2:2 */
  264. #define FMT_RGB565 0x04 /* 01 : RGB 565 */
  265. #define FMT_RGB555 0x08 /* 10 : RGB 555 */
  266. #define FMT_RGB444 0x0c /* 11 : RGB 444 */
  267. /* Output format control */
  268. #define OFMT_YUV 0x00 /* 00 : YUV */
  269. #define OFMT_P_BRAW 0x01 /* 01 : Processed Bayer RAW */
  270. #define OFMT_RGB 0x02 /* 10 : RGB */
  271. #define OFMT_BRAW 0x03 /* 11 : Bayer RAW */
  272. /* COM8 */
  273. #define FAST_ALGO 0x80 /* Enable fast AGC/AEC algorithm */
  274. /* AEC Setp size limit */
  275. #define UNLMT_STEP 0x40 /* 0 : Step size is limited */
  276. /* 1 : Unlimited step size */
  277. #define BNDF_ON_OFF 0x20 /* Banding filter ON/OFF */
  278. #define AEC_BND 0x10 /* Enable AEC below banding value */
  279. #define AEC_ON_OFF 0x08 /* Fine AEC ON/OFF control */
  280. #define AGC_ON 0x04 /* AGC Enable */
  281. #define AWB_ON 0x02 /* AWB Enable */
  282. #define AEC_ON 0x01 /* AEC Enable */
  283. /* COM9 */
  284. #define BASE_AECAGC 0x80 /* Histogram or average based AEC/AGC */
  285. /* Automatic gain ceiling - maximum AGC value */
  286. #define GAIN_2x 0x00 /* 000 : 2x */
  287. #define GAIN_4x 0x10 /* 001 : 4x */
  288. #define GAIN_8x 0x20 /* 010 : 8x */
  289. #define GAIN_16x 0x30 /* 011 : 16x */
  290. #define GAIN_32x 0x40 /* 100 : 32x */
  291. #define GAIN_64x 0x50 /* 101 : 64x */
  292. #define GAIN_128x 0x60 /* 110 : 128x */
  293. #define DROP_VSYNC 0x04 /* Drop VSYNC output of corrupt frame */
  294. #define DROP_HREF 0x02 /* Drop HREF output of corrupt frame */
  295. /* COM11 */
  296. #define SGLF_ON_OFF 0x02 /* Single frame ON/OFF selection */
  297. #define SGLF_TRIG 0x01 /* Single frame transfer trigger */
  298. /* EXHCH */
  299. #define VSIZE_LSB 0x04 /* Vertical data output size LSB */
  300. /* DSP_CTRL1 */
  301. #define FIFO_ON 0x80 /* FIFO enable/disable selection */
  302. #define UV_ON_OFF 0x40 /* UV adjust function ON/OFF selection */
  303. #define YUV444_2_422 0x20 /* YUV444 to 422 UV channel option selection */
  304. #define CLR_MTRX_ON_OFF 0x10 /* Color matrix ON/OFF selection */
  305. #define INTPLT_ON_OFF 0x08 /* Interpolation ON/OFF selection */
  306. #define GMM_ON_OFF 0x04 /* Gamma function ON/OFF selection */
  307. #define AUTO_BLK_ON_OFF 0x02 /* Black defect auto correction ON/OFF */
  308. #define AUTO_WHT_ON_OFF 0x01 /* White define auto correction ON/OFF */
  309. /* DSP_CTRL3 */
  310. #define UV_MASK 0x80 /* UV output sequence option */
  311. #define UV_ON 0x80 /* ON */
  312. #define UV_OFF 0x00 /* OFF */
  313. #define CBAR_MASK 0x20 /* DSP Color bar mask */
  314. #define CBAR_ON 0x20 /* ON */
  315. #define CBAR_OFF 0x00 /* OFF */
  316. /* HSTART */
  317. #define HST_VGA 0x23
  318. #define HST_QVGA 0x3F
  319. /* HSIZE */
  320. #define HSZ_VGA 0xA0
  321. #define HSZ_QVGA 0x50
  322. /* VSTART */
  323. #define VST_VGA 0x07
  324. #define VST_QVGA 0x03
  325. /* VSIZE */
  326. #define VSZ_VGA 0xF0
  327. #define VSZ_QVGA 0x78
  328. /* HOUTSIZE */
  329. #define HOSZ_VGA 0xA0
  330. #define HOSZ_QVGA 0x50
  331. /* VOUTSIZE */
  332. #define VOSZ_VGA 0xF0
  333. #define VOSZ_QVGA 0x78
  334. /*
  335. * bit configure (32 bit)
  336. * this is used in struct ov772x_color_format :: option
  337. */
  338. #define OP_UV 0x00000001
  339. #define OP_SWAP_RGB 0x00000002
  340. /*
  341. * ID
  342. */
  343. #define OV7720 0x7720
  344. #define OV7725 0x7721
  345. #define VERSION(pid, ver) ((pid<<8)|(ver&0xFF))
  346. /*
  347. * struct
  348. */
  349. struct regval_list {
  350. unsigned char reg_num;
  351. unsigned char value;
  352. };
  353. struct ov772x_color_format {
  354. char *name;
  355. __u32 fourcc;
  356. const struct regval_list *regs;
  357. unsigned int option;
  358. };
  359. struct ov772x_win_size {
  360. char *name;
  361. __u32 width;
  362. __u32 height;
  363. unsigned char com7_bit;
  364. const struct regval_list *regs;
  365. };
  366. struct ov772x_priv {
  367. struct ov772x_camera_info *info;
  368. struct i2c_client *client;
  369. struct soc_camera_device icd;
  370. const struct ov772x_color_format *fmt;
  371. const struct ov772x_win_size *win;
  372. int model;
  373. };
  374. #define ENDMARKER { 0xff, 0xff }
  375. /*
  376. * register setting for color format
  377. */
  378. static const struct regval_list ov772x_RGB555_regs[] = {
  379. { COM3, 0x00 },
  380. { COM7, FMT_RGB555 | OFMT_RGB },
  381. ENDMARKER,
  382. };
  383. static const struct regval_list ov772x_RGB565_regs[] = {
  384. { COM3, 0x00 },
  385. { COM7, FMT_RGB565 | OFMT_RGB },
  386. ENDMARKER,
  387. };
  388. static const struct regval_list ov772x_YYUV_regs[] = {
  389. { COM3, SWAP_YUV },
  390. { COM7, OFMT_YUV },
  391. ENDMARKER,
  392. };
  393. static const struct regval_list ov772x_UVYY_regs[] = {
  394. { COM3, 0x00 },
  395. { COM7, OFMT_YUV },
  396. ENDMARKER,
  397. };
  398. /*
  399. * register setting for window size
  400. */
  401. static const struct regval_list ov772x_qvga_regs[] = {
  402. { HSTART, HST_QVGA },
  403. { HSIZE, HSZ_QVGA },
  404. { VSTART, VST_QVGA },
  405. { VSIZE, VSZ_QVGA },
  406. { HOUTSIZE, HOSZ_QVGA },
  407. { VOUTSIZE, VOSZ_QVGA },
  408. ENDMARKER,
  409. };
  410. static const struct regval_list ov772x_vga_regs[] = {
  411. { HSTART, HST_VGA },
  412. { HSIZE, HSZ_VGA },
  413. { VSTART, VST_VGA },
  414. { VSIZE, VSZ_VGA },
  415. { HOUTSIZE, HOSZ_VGA },
  416. { VOUTSIZE, VOSZ_VGA },
  417. ENDMARKER,
  418. };
  419. /*
  420. * supported format list
  421. */
  422. #define SETFOURCC(type) .name = (#type), .fourcc = (V4L2_PIX_FMT_ ## type)
  423. static const struct soc_camera_data_format ov772x_fmt_lists[] = {
  424. {
  425. SETFOURCC(YUYV),
  426. .depth = 16,
  427. .colorspace = V4L2_COLORSPACE_JPEG,
  428. },
  429. {
  430. SETFOURCC(YVYU),
  431. .depth = 16,
  432. .colorspace = V4L2_COLORSPACE_JPEG,
  433. },
  434. {
  435. SETFOURCC(UYVY),
  436. .depth = 16,
  437. .colorspace = V4L2_COLORSPACE_JPEG,
  438. },
  439. {
  440. SETFOURCC(RGB555),
  441. .depth = 16,
  442. .colorspace = V4L2_COLORSPACE_SRGB,
  443. },
  444. {
  445. SETFOURCC(RGB555X),
  446. .depth = 16,
  447. .colorspace = V4L2_COLORSPACE_SRGB,
  448. },
  449. {
  450. SETFOURCC(RGB565),
  451. .depth = 16,
  452. .colorspace = V4L2_COLORSPACE_SRGB,
  453. },
  454. {
  455. SETFOURCC(RGB565X),
  456. .depth = 16,
  457. .colorspace = V4L2_COLORSPACE_SRGB,
  458. },
  459. };
  460. /*
  461. * color format list
  462. */
  463. #define T_YUYV 0
  464. static const struct ov772x_color_format ov772x_cfmts[] = {
  465. [T_YUYV] = {
  466. SETFOURCC(YUYV),
  467. .regs = ov772x_YYUV_regs,
  468. },
  469. {
  470. SETFOURCC(YVYU),
  471. .regs = ov772x_YYUV_regs,
  472. .option = OP_UV,
  473. },
  474. {
  475. SETFOURCC(UYVY),
  476. .regs = ov772x_UVYY_regs,
  477. },
  478. {
  479. SETFOURCC(RGB555),
  480. .regs = ov772x_RGB555_regs,
  481. .option = OP_SWAP_RGB,
  482. },
  483. {
  484. SETFOURCC(RGB555X),
  485. .regs = ov772x_RGB555_regs,
  486. },
  487. {
  488. SETFOURCC(RGB565),
  489. .regs = ov772x_RGB565_regs,
  490. .option = OP_SWAP_RGB,
  491. },
  492. {
  493. SETFOURCC(RGB565X),
  494. .regs = ov772x_RGB565_regs,
  495. },
  496. };
  497. /*
  498. * window size list
  499. */
  500. #define VGA_WIDTH 640
  501. #define VGA_HEIGHT 480
  502. #define QVGA_WIDTH 320
  503. #define QVGA_HEIGHT 240
  504. #define MAX_WIDTH VGA_WIDTH
  505. #define MAX_HEIGHT VGA_HEIGHT
  506. static const struct ov772x_win_size ov772x_win_vga = {
  507. .name = "VGA",
  508. .width = VGA_WIDTH,
  509. .height = VGA_HEIGHT,
  510. .com7_bit = SLCT_VGA,
  511. .regs = ov772x_vga_regs,
  512. };
  513. static const struct ov772x_win_size ov772x_win_qvga = {
  514. .name = "QVGA",
  515. .width = QVGA_WIDTH,
  516. .height = QVGA_HEIGHT,
  517. .com7_bit = SLCT_QVGA,
  518. .regs = ov772x_qvga_regs,
  519. };
  520. /*
  521. * general function
  522. */
  523. static int ov772x_write_array(struct i2c_client *client,
  524. const struct regval_list *vals)
  525. {
  526. while (vals->reg_num != 0xff) {
  527. int ret = i2c_smbus_write_byte_data(client,
  528. vals->reg_num,
  529. vals->value);
  530. if (ret < 0)
  531. return ret;
  532. vals++;
  533. }
  534. return 0;
  535. }
  536. static int ov772x_mask_set(struct i2c_client *client,
  537. u8 command,
  538. u8 mask,
  539. u8 set)
  540. {
  541. s32 val = i2c_smbus_read_byte_data(client, command);
  542. val &= ~mask;
  543. val |= set;
  544. return i2c_smbus_write_byte_data(client, command, val);
  545. }
  546. static int ov772x_reset(struct i2c_client *client)
  547. {
  548. int ret = i2c_smbus_write_byte_data(client, COM7, SCCB_RESET);
  549. msleep(1);
  550. return ret;
  551. }
  552. /*
  553. * soc_camera_ops function
  554. */
  555. static int ov772x_init(struct soc_camera_device *icd)
  556. {
  557. struct ov772x_priv *priv = container_of(icd, struct ov772x_priv, icd);
  558. int ret = 0;
  559. if (priv->info->link.power) {
  560. ret = priv->info->link.power(&priv->client->dev, 1);
  561. if (ret < 0)
  562. return ret;
  563. }
  564. if (priv->info->link.reset)
  565. ret = priv->info->link.reset(&priv->client->dev);
  566. return ret;
  567. }
  568. static int ov772x_release(struct soc_camera_device *icd)
  569. {
  570. struct ov772x_priv *priv = container_of(icd, struct ov772x_priv, icd);
  571. int ret = 0;
  572. if (priv->info->link.power)
  573. ret = priv->info->link.power(&priv->client->dev, 0);
  574. return ret;
  575. }
  576. static int ov772x_start_capture(struct soc_camera_device *icd)
  577. {
  578. struct ov772x_priv *priv = container_of(icd, struct ov772x_priv, icd);
  579. int ret;
  580. if (!priv->win)
  581. priv->win = &ov772x_win_vga;
  582. if (!priv->fmt)
  583. priv->fmt = &ov772x_cfmts[T_YUYV];
  584. /*
  585. * reset hardware
  586. */
  587. ov772x_reset(priv->client);
  588. /*
  589. * set color format
  590. */
  591. ret = ov772x_write_array(priv->client, priv->fmt->regs);
  592. if (ret < 0)
  593. goto start_end;
  594. /*
  595. * set size format
  596. */
  597. ret = ov772x_write_array(priv->client, priv->win->regs);
  598. if (ret < 0)
  599. goto start_end;
  600. /*
  601. * set COM7 bit ( QVGA or VGA )
  602. */
  603. ret = ov772x_mask_set(priv->client,
  604. COM7, SLCT_MASK, priv->win->com7_bit);
  605. if (ret < 0)
  606. goto start_end;
  607. /*
  608. * set UV setting
  609. */
  610. if (priv->fmt->option & OP_UV) {
  611. ret = ov772x_mask_set(priv->client,
  612. DSP_CTRL3, UV_MASK, UV_ON);
  613. if (ret < 0)
  614. goto start_end;
  615. }
  616. /*
  617. * set SWAP setting
  618. */
  619. if (priv->fmt->option & OP_SWAP_RGB) {
  620. ret = ov772x_mask_set(priv->client,
  621. COM3, SWAP_MASK, SWAP_RGB);
  622. if (ret < 0)
  623. goto start_end;
  624. }
  625. dev_dbg(&icd->dev,
  626. "format %s, win %s\n", priv->fmt->name, priv->win->name);
  627. start_end:
  628. priv->fmt = NULL;
  629. priv->win = NULL;
  630. return ret;
  631. }
  632. static int ov772x_stop_capture(struct soc_camera_device *icd)
  633. {
  634. struct ov772x_priv *priv = container_of(icd, struct ov772x_priv, icd);
  635. ov772x_reset(priv->client);
  636. return 0;
  637. }
  638. static int ov772x_set_bus_param(struct soc_camera_device *icd,
  639. unsigned long flags)
  640. {
  641. return 0;
  642. }
  643. static unsigned long ov772x_query_bus_param(struct soc_camera_device *icd)
  644. {
  645. struct ov772x_priv *priv = container_of(icd, struct ov772x_priv, icd);
  646. struct soc_camera_link *icl = priv->client->dev.platform_data;
  647. unsigned long flags = SOCAM_PCLK_SAMPLE_RISING | SOCAM_MASTER |
  648. SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_HIGH |
  649. priv->info->buswidth;
  650. return soc_camera_apply_sensor_flags(icl, flags);
  651. }
  652. static int ov772x_get_chip_id(struct soc_camera_device *icd,
  653. struct v4l2_dbg_chip_ident *id)
  654. {
  655. struct ov772x_priv *priv = container_of(icd, struct ov772x_priv, icd);
  656. id->ident = priv->model;
  657. id->revision = 0;
  658. return 0;
  659. }
  660. #ifdef CONFIG_VIDEO_ADV_DEBUG
  661. static int ov772x_get_register(struct soc_camera_device *icd,
  662. struct v4l2_dbg_register *reg)
  663. {
  664. struct ov772x_priv *priv = container_of(icd, struct ov772x_priv, icd);
  665. int ret;
  666. reg->size = 1;
  667. if (reg->reg > 0xff)
  668. return -EINVAL;
  669. ret = i2c_smbus_read_byte_data(priv->client, reg->reg);
  670. if (ret < 0)
  671. return ret;
  672. reg->val = (__u64)ret;
  673. return 0;
  674. }
  675. static int ov772x_set_register(struct soc_camera_device *icd,
  676. struct v4l2_dbg_register *reg)
  677. {
  678. struct ov772x_priv *priv = container_of(icd, struct ov772x_priv, icd);
  679. if (reg->reg > 0xff ||
  680. reg->val > 0xff)
  681. return -EINVAL;
  682. return i2c_smbus_write_byte_data(priv->client, reg->reg, reg->val);
  683. }
  684. #endif
  685. static const struct ov772x_win_size*
  686. ov772x_select_win(u32 width, u32 height)
  687. {
  688. __u32 diff;
  689. const struct ov772x_win_size *win;
  690. /* default is QVGA */
  691. diff = abs(width - ov772x_win_qvga.width) +
  692. abs(height - ov772x_win_qvga.height);
  693. win = &ov772x_win_qvga;
  694. /* VGA */
  695. if (diff >
  696. abs(width - ov772x_win_vga.width) +
  697. abs(height - ov772x_win_vga.height))
  698. win = &ov772x_win_vga;
  699. return win;
  700. }
  701. static int ov772x_set_fmt(struct soc_camera_device *icd,
  702. __u32 pixfmt,
  703. struct v4l2_rect *rect)
  704. {
  705. struct ov772x_priv *priv = container_of(icd, struct ov772x_priv, icd);
  706. int ret = -EINVAL;
  707. int i;
  708. /*
  709. * select format
  710. */
  711. priv->fmt = NULL;
  712. for (i = 0; i < ARRAY_SIZE(ov772x_cfmts); i++) {
  713. if (pixfmt == ov772x_cfmts[i].fourcc) {
  714. priv->fmt = ov772x_cfmts + i;
  715. ret = 0;
  716. break;
  717. }
  718. }
  719. /*
  720. * select win
  721. */
  722. priv->win = ov772x_select_win(rect->width, rect->height);
  723. return ret;
  724. }
  725. static int ov772x_try_fmt(struct soc_camera_device *icd,
  726. struct v4l2_format *f)
  727. {
  728. struct v4l2_pix_format *pix = &f->fmt.pix;
  729. const struct ov772x_win_size *win;
  730. /*
  731. * select suitable win
  732. */
  733. win = ov772x_select_win(pix->width, pix->height);
  734. pix->width = win->width;
  735. pix->height = win->height;
  736. pix->field = V4L2_FIELD_NONE;
  737. return 0;
  738. }
  739. static int ov772x_video_probe(struct soc_camera_device *icd)
  740. {
  741. struct ov772x_priv *priv = container_of(icd, struct ov772x_priv, icd);
  742. u8 pid, ver;
  743. const char *devname;
  744. /*
  745. * We must have a parent by now. And it cannot be a wrong one.
  746. * So this entire test is completely redundant.
  747. */
  748. if (!icd->dev.parent ||
  749. to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
  750. return -ENODEV;
  751. /*
  752. * ov772x only use 8 or 10 bit bus width
  753. */
  754. if (SOCAM_DATAWIDTH_10 != priv->info->buswidth &&
  755. SOCAM_DATAWIDTH_8 != priv->info->buswidth) {
  756. dev_err(&icd->dev, "bus width error\n");
  757. return -ENODEV;
  758. }
  759. icd->formats = ov772x_fmt_lists;
  760. icd->num_formats = ARRAY_SIZE(ov772x_fmt_lists);
  761. /*
  762. * check and show product ID and manufacturer ID
  763. */
  764. pid = i2c_smbus_read_byte_data(priv->client, PID);
  765. ver = i2c_smbus_read_byte_data(priv->client, VER);
  766. switch (VERSION(pid, ver)) {
  767. case OV7720:
  768. devname = "ov7720";
  769. priv->model = V4L2_IDENT_OV7720;
  770. break;
  771. case OV7725:
  772. devname = "ov7725";
  773. priv->model = V4L2_IDENT_OV7725;
  774. break;
  775. default:
  776. dev_err(&icd->dev,
  777. "Product ID error %x:%x\n", pid, ver);
  778. return -ENODEV;
  779. }
  780. dev_info(&icd->dev,
  781. "%s Product ID %0x:%0x Manufacturer ID %x:%x\n",
  782. devname,
  783. pid,
  784. ver,
  785. i2c_smbus_read_byte_data(priv->client, MIDH),
  786. i2c_smbus_read_byte_data(priv->client, MIDL));
  787. return soc_camera_video_start(icd);
  788. }
  789. static void ov772x_video_remove(struct soc_camera_device *icd)
  790. {
  791. soc_camera_video_stop(icd);
  792. }
  793. static struct soc_camera_ops ov772x_ops = {
  794. .owner = THIS_MODULE,
  795. .probe = ov772x_video_probe,
  796. .remove = ov772x_video_remove,
  797. .init = ov772x_init,
  798. .release = ov772x_release,
  799. .start_capture = ov772x_start_capture,
  800. .stop_capture = ov772x_stop_capture,
  801. .set_fmt = ov772x_set_fmt,
  802. .try_fmt = ov772x_try_fmt,
  803. .set_bus_param = ov772x_set_bus_param,
  804. .query_bus_param = ov772x_query_bus_param,
  805. .get_chip_id = ov772x_get_chip_id,
  806. #ifdef CONFIG_VIDEO_ADV_DEBUG
  807. .get_register = ov772x_get_register,
  808. .set_register = ov772x_set_register,
  809. #endif
  810. };
  811. /*
  812. * i2c_driver function
  813. */
  814. static int ov772x_probe(struct i2c_client *client,
  815. const struct i2c_device_id *did)
  816. {
  817. struct ov772x_priv *priv;
  818. struct ov772x_camera_info *info;
  819. struct soc_camera_device *icd;
  820. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  821. int ret;
  822. info = client->dev.platform_data;
  823. if (!info)
  824. return -EINVAL;
  825. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  826. dev_err(&adapter->dev,
  827. "I2C-Adapter doesn't support "
  828. "I2C_FUNC_SMBUS_BYTE_DATA\n");
  829. return -EIO;
  830. }
  831. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  832. if (!priv)
  833. return -ENOMEM;
  834. priv->info = info;
  835. priv->client = client;
  836. i2c_set_clientdata(client, priv);
  837. icd = &priv->icd;
  838. icd->ops = &ov772x_ops;
  839. icd->control = &client->dev;
  840. icd->width_max = MAX_WIDTH;
  841. icd->height_max = MAX_HEIGHT;
  842. icd->iface = priv->info->link.bus_id;
  843. ret = soc_camera_device_register(icd);
  844. if (ret) {
  845. i2c_set_clientdata(client, NULL);
  846. kfree(priv);
  847. }
  848. return ret;
  849. }
  850. static int ov772x_remove(struct i2c_client *client)
  851. {
  852. struct ov772x_priv *priv = i2c_get_clientdata(client);
  853. soc_camera_device_unregister(&priv->icd);
  854. i2c_set_clientdata(client, NULL);
  855. kfree(priv);
  856. return 0;
  857. }
  858. static const struct i2c_device_id ov772x_id[] = {
  859. { "ov772x", 0 },
  860. { }
  861. };
  862. MODULE_DEVICE_TABLE(i2c, ov772x_id);
  863. static struct i2c_driver ov772x_i2c_driver = {
  864. .driver = {
  865. .name = "ov772x",
  866. },
  867. .probe = ov772x_probe,
  868. .remove = ov772x_remove,
  869. .id_table = ov772x_id,
  870. };
  871. /*
  872. * module function
  873. */
  874. static int __init ov772x_module_init(void)
  875. {
  876. return i2c_add_driver(&ov772x_i2c_driver);
  877. }
  878. static void __exit ov772x_module_exit(void)
  879. {
  880. i2c_del_driver(&ov772x_i2c_driver);
  881. }
  882. module_init(ov772x_module_init);
  883. module_exit(ov772x_module_exit);
  884. MODULE_DESCRIPTION("SoC Camera driver for ov772x");
  885. MODULE_AUTHOR("Kuninori Morimoto");
  886. MODULE_LICENSE("GPL v2");