ov772x.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  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/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_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  766. {
  767. a->c.left = 0;
  768. a->c.top = 0;
  769. a->c.width = VGA_WIDTH;
  770. a->c.height = VGA_HEIGHT;
  771. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  772. return 0;
  773. }
  774. static int ov772x_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
  775. {
  776. a->bounds.left = 0;
  777. a->bounds.top = 0;
  778. a->bounds.width = OV772X_MAX_WIDTH;
  779. a->bounds.height = OV772X_MAX_HEIGHT;
  780. a->defrect = a->bounds;
  781. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  782. a->pixelaspect.numerator = 1;
  783. a->pixelaspect.denominator = 1;
  784. return 0;
  785. }
  786. static int ov772x_get_fmt(struct v4l2_subdev *sd,
  787. struct v4l2_subdev_pad_config *cfg,
  788. struct v4l2_subdev_format *format)
  789. {
  790. struct v4l2_mbus_framefmt *mf = &format->format;
  791. struct ov772x_priv *priv = to_ov772x(sd);
  792. if (format->pad)
  793. return -EINVAL;
  794. mf->width = priv->win->rect.width;
  795. mf->height = priv->win->rect.height;
  796. mf->code = priv->cfmt->code;
  797. mf->colorspace = priv->cfmt->colorspace;
  798. mf->field = V4L2_FIELD_NONE;
  799. return 0;
  800. }
  801. static int ov772x_s_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
  802. {
  803. struct ov772x_priv *priv = to_ov772x(sd);
  804. const struct ov772x_color_format *cfmt;
  805. const struct ov772x_win_size *win;
  806. int ret;
  807. ov772x_select_params(mf, &cfmt, &win);
  808. ret = ov772x_set_params(priv, cfmt, win);
  809. if (ret < 0)
  810. return ret;
  811. priv->win = win;
  812. priv->cfmt = cfmt;
  813. mf->code = cfmt->code;
  814. mf->width = win->rect.width;
  815. mf->height = win->rect.height;
  816. mf->field = V4L2_FIELD_NONE;
  817. mf->colorspace = cfmt->colorspace;
  818. return 0;
  819. }
  820. static int ov772x_set_fmt(struct v4l2_subdev *sd,
  821. struct v4l2_subdev_pad_config *cfg,
  822. struct v4l2_subdev_format *format)
  823. {
  824. struct v4l2_mbus_framefmt *mf = &format->format;
  825. const struct ov772x_color_format *cfmt;
  826. const struct ov772x_win_size *win;
  827. if (format->pad)
  828. return -EINVAL;
  829. ov772x_select_params(mf, &cfmt, &win);
  830. mf->code = cfmt->code;
  831. mf->width = win->rect.width;
  832. mf->height = win->rect.height;
  833. mf->field = V4L2_FIELD_NONE;
  834. mf->colorspace = cfmt->colorspace;
  835. if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE)
  836. return ov772x_s_fmt(sd, mf);
  837. cfg->try_fmt = *mf;
  838. return 0;
  839. }
  840. static int ov772x_video_probe(struct ov772x_priv *priv)
  841. {
  842. struct i2c_client *client = v4l2_get_subdevdata(&priv->subdev);
  843. u8 pid, ver;
  844. const char *devname;
  845. int ret;
  846. ret = ov772x_s_power(&priv->subdev, 1);
  847. if (ret < 0)
  848. return ret;
  849. /*
  850. * check and show product ID and manufacturer ID
  851. */
  852. pid = ov772x_read(client, PID);
  853. ver = ov772x_read(client, VER);
  854. switch (VERSION(pid, ver)) {
  855. case OV7720:
  856. devname = "ov7720";
  857. break;
  858. case OV7725:
  859. devname = "ov7725";
  860. break;
  861. default:
  862. dev_err(&client->dev,
  863. "Product ID error %x:%x\n", pid, ver);
  864. ret = -ENODEV;
  865. goto done;
  866. }
  867. dev_info(&client->dev,
  868. "%s Product ID %0x:%0x Manufacturer ID %x:%x\n",
  869. devname,
  870. pid,
  871. ver,
  872. ov772x_read(client, MIDH),
  873. ov772x_read(client, MIDL));
  874. ret = v4l2_ctrl_handler_setup(&priv->hdl);
  875. done:
  876. ov772x_s_power(&priv->subdev, 0);
  877. return ret;
  878. }
  879. static const struct v4l2_ctrl_ops ov772x_ctrl_ops = {
  880. .s_ctrl = ov772x_s_ctrl,
  881. };
  882. static struct v4l2_subdev_core_ops ov772x_subdev_core_ops = {
  883. #ifdef CONFIG_VIDEO_ADV_DEBUG
  884. .g_register = ov772x_g_register,
  885. .s_register = ov772x_s_register,
  886. #endif
  887. .s_power = ov772x_s_power,
  888. };
  889. static int ov772x_enum_mbus_code(struct v4l2_subdev *sd,
  890. struct v4l2_subdev_pad_config *cfg,
  891. struct v4l2_subdev_mbus_code_enum *code)
  892. {
  893. if (code->pad || code->index >= ARRAY_SIZE(ov772x_cfmts))
  894. return -EINVAL;
  895. code->code = ov772x_cfmts[code->index].code;
  896. return 0;
  897. }
  898. static int ov772x_g_mbus_config(struct v4l2_subdev *sd,
  899. struct v4l2_mbus_config *cfg)
  900. {
  901. struct i2c_client *client = v4l2_get_subdevdata(sd);
  902. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  903. cfg->flags = V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_MASTER |
  904. V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_HIGH |
  905. V4L2_MBUS_DATA_ACTIVE_HIGH;
  906. cfg->type = V4L2_MBUS_PARALLEL;
  907. cfg->flags = soc_camera_apply_board_flags(ssdd, cfg);
  908. return 0;
  909. }
  910. static struct v4l2_subdev_video_ops ov772x_subdev_video_ops = {
  911. .s_stream = ov772x_s_stream,
  912. .cropcap = ov772x_cropcap,
  913. .g_crop = ov772x_g_crop,
  914. .g_mbus_config = ov772x_g_mbus_config,
  915. };
  916. static const struct v4l2_subdev_pad_ops ov772x_subdev_pad_ops = {
  917. .enum_mbus_code = ov772x_enum_mbus_code,
  918. .get_fmt = ov772x_get_fmt,
  919. .set_fmt = ov772x_set_fmt,
  920. };
  921. static struct v4l2_subdev_ops ov772x_subdev_ops = {
  922. .core = &ov772x_subdev_core_ops,
  923. .video = &ov772x_subdev_video_ops,
  924. .pad = &ov772x_subdev_pad_ops,
  925. };
  926. /*
  927. * i2c_driver function
  928. */
  929. static int ov772x_probe(struct i2c_client *client,
  930. const struct i2c_device_id *did)
  931. {
  932. struct ov772x_priv *priv;
  933. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  934. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  935. int ret;
  936. if (!ssdd || !ssdd->drv_priv) {
  937. dev_err(&client->dev, "OV772X: missing platform data!\n");
  938. return -EINVAL;
  939. }
  940. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  941. dev_err(&adapter->dev,
  942. "I2C-Adapter doesn't support "
  943. "I2C_FUNC_SMBUS_BYTE_DATA\n");
  944. return -EIO;
  945. }
  946. priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
  947. if (!priv)
  948. return -ENOMEM;
  949. priv->info = ssdd->drv_priv;
  950. v4l2_i2c_subdev_init(&priv->subdev, client, &ov772x_subdev_ops);
  951. v4l2_ctrl_handler_init(&priv->hdl, 3);
  952. v4l2_ctrl_new_std(&priv->hdl, &ov772x_ctrl_ops,
  953. V4L2_CID_VFLIP, 0, 1, 1, 0);
  954. v4l2_ctrl_new_std(&priv->hdl, &ov772x_ctrl_ops,
  955. V4L2_CID_HFLIP, 0, 1, 1, 0);
  956. v4l2_ctrl_new_std(&priv->hdl, &ov772x_ctrl_ops,
  957. V4L2_CID_BAND_STOP_FILTER, 0, 256, 1, 0);
  958. priv->subdev.ctrl_handler = &priv->hdl;
  959. if (priv->hdl.error)
  960. return priv->hdl.error;
  961. priv->clk = v4l2_clk_get(&client->dev, "mclk");
  962. if (IS_ERR(priv->clk)) {
  963. ret = PTR_ERR(priv->clk);
  964. goto eclkget;
  965. }
  966. ret = ov772x_video_probe(priv);
  967. if (ret < 0) {
  968. v4l2_clk_put(priv->clk);
  969. eclkget:
  970. v4l2_ctrl_handler_free(&priv->hdl);
  971. } else {
  972. priv->cfmt = &ov772x_cfmts[0];
  973. priv->win = &ov772x_win_sizes[0];
  974. }
  975. return ret;
  976. }
  977. static int ov772x_remove(struct i2c_client *client)
  978. {
  979. struct ov772x_priv *priv = to_ov772x(i2c_get_clientdata(client));
  980. v4l2_clk_put(priv->clk);
  981. v4l2_device_unregister_subdev(&priv->subdev);
  982. v4l2_ctrl_handler_free(&priv->hdl);
  983. return 0;
  984. }
  985. static const struct i2c_device_id ov772x_id[] = {
  986. { "ov772x", 0 },
  987. { }
  988. };
  989. MODULE_DEVICE_TABLE(i2c, ov772x_id);
  990. static struct i2c_driver ov772x_i2c_driver = {
  991. .driver = {
  992. .name = "ov772x",
  993. },
  994. .probe = ov772x_probe,
  995. .remove = ov772x_remove,
  996. .id_table = ov772x_id,
  997. };
  998. module_i2c_driver(ov772x_i2c_driver);
  999. MODULE_DESCRIPTION("SoC Camera driver for ov772x");
  1000. MODULE_AUTHOR("Kuninori Morimoto");
  1001. MODULE_LICENSE("GPL v2");