ov772x.c 28 KB

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