ov2640.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  1. /*
  2. * ov2640 Camera Driver
  3. *
  4. * Copyright (C) 2010 Alberto Panizzo <maramaopercheseimorto@gmail.com>
  5. *
  6. * Based on ov772x, ov9640 drivers and previous non merged implementations.
  7. *
  8. * Copyright 2005-2009 Freescale Semiconductor, Inc. All Rights Reserved.
  9. * Copyright (C) 2006, OmniVision
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/i2c.h>
  18. #include <linux/slab.h>
  19. #include <linux/delay.h>
  20. #include <linux/gpio.h>
  21. #include <linux/gpio/consumer.h>
  22. #include <linux/of_gpio.h>
  23. #include <linux/v4l2-mediabus.h>
  24. #include <linux/videodev2.h>
  25. #include <media/soc_camera.h>
  26. #include <media/v4l2-clk.h>
  27. #include <media/v4l2-subdev.h>
  28. #include <media/v4l2-ctrls.h>
  29. #include <media/v4l2-image-sizes.h>
  30. #define VAL_SET(x, mask, rshift, lshift) \
  31. ((((x) >> rshift) & mask) << lshift)
  32. /*
  33. * DSP registers
  34. * register offset for BANK_SEL == BANK_SEL_DSP
  35. */
  36. #define R_BYPASS 0x05 /* Bypass DSP */
  37. #define R_BYPASS_DSP_BYPAS 0x01 /* Bypass DSP, sensor out directly */
  38. #define R_BYPASS_USE_DSP 0x00 /* Use the internal DSP */
  39. #define QS 0x44 /* Quantization Scale Factor */
  40. #define CTRLI 0x50
  41. #define CTRLI_LP_DP 0x80
  42. #define CTRLI_ROUND 0x40
  43. #define CTRLI_V_DIV_SET(x) VAL_SET(x, 0x3, 0, 3)
  44. #define CTRLI_H_DIV_SET(x) VAL_SET(x, 0x3, 0, 0)
  45. #define HSIZE 0x51 /* H_SIZE[7:0] (real/4) */
  46. #define HSIZE_SET(x) VAL_SET(x, 0xFF, 2, 0)
  47. #define VSIZE 0x52 /* V_SIZE[7:0] (real/4) */
  48. #define VSIZE_SET(x) VAL_SET(x, 0xFF, 2, 0)
  49. #define XOFFL 0x53 /* OFFSET_X[7:0] */
  50. #define XOFFL_SET(x) VAL_SET(x, 0xFF, 0, 0)
  51. #define YOFFL 0x54 /* OFFSET_Y[7:0] */
  52. #define YOFFL_SET(x) VAL_SET(x, 0xFF, 0, 0)
  53. #define VHYX 0x55 /* Offset and size completion */
  54. #define VHYX_VSIZE_SET(x) VAL_SET(x, 0x1, (8+2), 7)
  55. #define VHYX_HSIZE_SET(x) VAL_SET(x, 0x1, (8+2), 3)
  56. #define VHYX_YOFF_SET(x) VAL_SET(x, 0x3, 8, 4)
  57. #define VHYX_XOFF_SET(x) VAL_SET(x, 0x3, 8, 0)
  58. #define DPRP 0x56
  59. #define TEST 0x57 /* Horizontal size completion */
  60. #define TEST_HSIZE_SET(x) VAL_SET(x, 0x1, (9+2), 7)
  61. #define ZMOW 0x5A /* Zoom: Out Width OUTW[7:0] (real/4) */
  62. #define ZMOW_OUTW_SET(x) VAL_SET(x, 0xFF, 2, 0)
  63. #define ZMOH 0x5B /* Zoom: Out Height OUTH[7:0] (real/4) */
  64. #define ZMOH_OUTH_SET(x) VAL_SET(x, 0xFF, 2, 0)
  65. #define ZMHH 0x5C /* Zoom: Speed and H&W completion */
  66. #define ZMHH_ZSPEED_SET(x) VAL_SET(x, 0x0F, 0, 4)
  67. #define ZMHH_OUTH_SET(x) VAL_SET(x, 0x1, (8+2), 2)
  68. #define ZMHH_OUTW_SET(x) VAL_SET(x, 0x3, (8+2), 0)
  69. #define BPADDR 0x7C /* SDE Indirect Register Access: Address */
  70. #define BPDATA 0x7D /* SDE Indirect Register Access: Data */
  71. #define CTRL2 0x86 /* DSP Module enable 2 */
  72. #define CTRL2_DCW_EN 0x20
  73. #define CTRL2_SDE_EN 0x10
  74. #define CTRL2_UV_ADJ_EN 0x08
  75. #define CTRL2_UV_AVG_EN 0x04
  76. #define CTRL2_CMX_EN 0x01
  77. #define CTRL3 0x87 /* DSP Module enable 3 */
  78. #define CTRL3_BPC_EN 0x80
  79. #define CTRL3_WPC_EN 0x40
  80. #define SIZEL 0x8C /* Image Size Completion */
  81. #define SIZEL_HSIZE8_11_SET(x) VAL_SET(x, 0x1, 11, 6)
  82. #define SIZEL_HSIZE8_SET(x) VAL_SET(x, 0x7, 0, 3)
  83. #define SIZEL_VSIZE8_SET(x) VAL_SET(x, 0x7, 0, 0)
  84. #define HSIZE8 0xC0 /* Image Horizontal Size HSIZE[10:3] */
  85. #define HSIZE8_SET(x) VAL_SET(x, 0xFF, 3, 0)
  86. #define VSIZE8 0xC1 /* Image Vertical Size VSIZE[10:3] */
  87. #define VSIZE8_SET(x) VAL_SET(x, 0xFF, 3, 0)
  88. #define CTRL0 0xC2 /* DSP Module enable 0 */
  89. #define CTRL0_AEC_EN 0x80
  90. #define CTRL0_AEC_SEL 0x40
  91. #define CTRL0_STAT_SEL 0x20
  92. #define CTRL0_VFIRST 0x10
  93. #define CTRL0_YUV422 0x08
  94. #define CTRL0_YUV_EN 0x04
  95. #define CTRL0_RGB_EN 0x02
  96. #define CTRL0_RAW_EN 0x01
  97. #define CTRL1 0xC3 /* DSP Module enable 1 */
  98. #define CTRL1_CIP 0x80
  99. #define CTRL1_DMY 0x40
  100. #define CTRL1_RAW_GMA 0x20
  101. #define CTRL1_DG 0x10
  102. #define CTRL1_AWB 0x08
  103. #define CTRL1_AWB_GAIN 0x04
  104. #define CTRL1_LENC 0x02
  105. #define CTRL1_PRE 0x01
  106. #define R_DVP_SP 0xD3 /* DVP output speed control */
  107. #define R_DVP_SP_AUTO_MODE 0x80
  108. #define R_DVP_SP_DVP_MASK 0x3F /* DVP PCLK = sysclk (48)/[6:0] (YUV0);
  109. * = sysclk (48)/(2*[6:0]) (RAW);*/
  110. #define IMAGE_MODE 0xDA /* Image Output Format Select */
  111. #define IMAGE_MODE_Y8_DVP_EN 0x40
  112. #define IMAGE_MODE_JPEG_EN 0x10
  113. #define IMAGE_MODE_YUV422 0x00
  114. #define IMAGE_MODE_RAW10 0x04 /* (DVP) */
  115. #define IMAGE_MODE_RGB565 0x08
  116. #define IMAGE_MODE_HREF_VSYNC 0x02 /* HREF timing select in DVP JPEG output
  117. * mode (0 for HREF is same as sensor) */
  118. #define IMAGE_MODE_LBYTE_FIRST 0x01 /* Byte swap enable for DVP
  119. * 1: Low byte first UYVY (C2[4] =0)
  120. * VYUY (C2[4] =1)
  121. * 0: High byte first YUYV (C2[4]=0)
  122. * YVYU (C2[4] = 1) */
  123. #define RESET 0xE0 /* Reset */
  124. #define RESET_MICROC 0x40
  125. #define RESET_SCCB 0x20
  126. #define RESET_JPEG 0x10
  127. #define RESET_DVP 0x04
  128. #define RESET_IPU 0x02
  129. #define RESET_CIF 0x01
  130. #define REGED 0xED /* Register ED */
  131. #define REGED_CLK_OUT_DIS 0x10
  132. #define MS_SP 0xF0 /* SCCB Master Speed */
  133. #define SS_ID 0xF7 /* SCCB Slave ID */
  134. #define SS_CTRL 0xF8 /* SCCB Slave Control */
  135. #define SS_CTRL_ADD_AUTO_INC 0x20
  136. #define SS_CTRL_EN 0x08
  137. #define SS_CTRL_DELAY_CLK 0x04
  138. #define SS_CTRL_ACC_EN 0x02
  139. #define SS_CTRL_SEN_PASS_THR 0x01
  140. #define MC_BIST 0xF9 /* Microcontroller misc register */
  141. #define MC_BIST_RESET 0x80 /* Microcontroller Reset */
  142. #define MC_BIST_BOOT_ROM_SEL 0x40
  143. #define MC_BIST_12KB_SEL 0x20
  144. #define MC_BIST_12KB_MASK 0x30
  145. #define MC_BIST_512KB_SEL 0x08
  146. #define MC_BIST_512KB_MASK 0x0C
  147. #define MC_BIST_BUSY_BIT_R 0x02
  148. #define MC_BIST_MC_RES_ONE_SH_W 0x02
  149. #define MC_BIST_LAUNCH 0x01
  150. #define BANK_SEL 0xFF /* Register Bank Select */
  151. #define BANK_SEL_DSP 0x00
  152. #define BANK_SEL_SENS 0x01
  153. /*
  154. * Sensor registers
  155. * register offset for BANK_SEL == BANK_SEL_SENS
  156. */
  157. #define GAIN 0x00 /* AGC - Gain control gain setting */
  158. #define COM1 0x03 /* Common control 1 */
  159. #define COM1_1_DUMMY_FR 0x40
  160. #define COM1_3_DUMMY_FR 0x80
  161. #define COM1_7_DUMMY_FR 0xC0
  162. #define COM1_VWIN_LSB_UXGA 0x0F
  163. #define COM1_VWIN_LSB_SVGA 0x0A
  164. #define COM1_VWIN_LSB_CIF 0x06
  165. #define REG04 0x04 /* Register 04 */
  166. #define REG04_DEF 0x20 /* Always set */
  167. #define REG04_HFLIP_IMG 0x80 /* Horizontal mirror image ON/OFF */
  168. #define REG04_VFLIP_IMG 0x40 /* Vertical flip image ON/OFF */
  169. #define REG04_VREF_EN 0x10
  170. #define REG04_HREF_EN 0x08
  171. #define REG04_AEC_SET(x) VAL_SET(x, 0x3, 0, 0)
  172. #define REG08 0x08 /* Frame Exposure One-pin Control Pre-charge Row Num */
  173. #define COM2 0x09 /* Common control 2 */
  174. #define COM2_SOFT_SLEEP_MODE 0x10 /* Soft sleep mode */
  175. /* Output drive capability */
  176. #define COM2_OCAP_Nx_SET(N) (((N) - 1) & 0x03) /* N = [1x .. 4x] */
  177. #define PID 0x0A /* Product ID Number MSB */
  178. #define VER 0x0B /* Product ID Number LSB */
  179. #define COM3 0x0C /* Common control 3 */
  180. #define COM3_BAND_50H 0x04 /* 0 For Banding at 60H */
  181. #define COM3_BAND_AUTO 0x02 /* Auto Banding */
  182. #define COM3_SING_FR_SNAPSH 0x01 /* 0 For enable live video output after the
  183. * snapshot sequence*/
  184. #define AEC 0x10 /* AEC[9:2] Exposure Value */
  185. #define CLKRC 0x11 /* Internal clock */
  186. #define CLKRC_EN 0x80
  187. #define CLKRC_DIV_SET(x) (((x) - 1) & 0x1F) /* CLK = XVCLK/(x) */
  188. #define COM7 0x12 /* Common control 7 */
  189. #define COM7_SRST 0x80 /* Initiates system reset. All registers are
  190. * set to factory default values after which
  191. * the chip resumes normal operation */
  192. #define COM7_RES_UXGA 0x00 /* Resolution selectors for UXGA */
  193. #define COM7_RES_SVGA 0x40 /* SVGA */
  194. #define COM7_RES_CIF 0x20 /* CIF */
  195. #define COM7_ZOOM_EN 0x04 /* Enable Zoom mode */
  196. #define COM7_COLOR_BAR_TEST 0x02 /* Enable Color Bar Test Pattern */
  197. #define COM8 0x13 /* Common control 8 */
  198. #define COM8_DEF 0xC0 /* Banding filter ON/OFF */
  199. #define COM8_BNDF_EN 0x20 /* Banding filter ON/OFF */
  200. #define COM8_AGC_EN 0x04 /* AGC Auto/Manual control selection */
  201. #define COM8_AEC_EN 0x01 /* Auto/Manual Exposure control */
  202. #define COM9 0x14 /* Common control 9
  203. * Automatic gain ceiling - maximum AGC value [7:5]*/
  204. #define COM9_AGC_GAIN_2x 0x00 /* 000 : 2x */
  205. #define COM9_AGC_GAIN_4x 0x20 /* 001 : 4x */
  206. #define COM9_AGC_GAIN_8x 0x40 /* 010 : 8x */
  207. #define COM9_AGC_GAIN_16x 0x60 /* 011 : 16x */
  208. #define COM9_AGC_GAIN_32x 0x80 /* 100 : 32x */
  209. #define COM9_AGC_GAIN_64x 0xA0 /* 101 : 64x */
  210. #define COM9_AGC_GAIN_128x 0xC0 /* 110 : 128x */
  211. #define COM10 0x15 /* Common control 10 */
  212. #define COM10_PCLK_HREF 0x20 /* PCLK output qualified by HREF */
  213. #define COM10_PCLK_RISE 0x10 /* Data is updated at the rising edge of
  214. * PCLK (user can latch data at the next
  215. * falling edge of PCLK).
  216. * 0 otherwise. */
  217. #define COM10_HREF_INV 0x08 /* Invert HREF polarity:
  218. * HREF negative for valid data*/
  219. #define COM10_VSINC_INV 0x02 /* Invert VSYNC polarity */
  220. #define HSTART 0x17 /* Horizontal Window start MSB 8 bit */
  221. #define HEND 0x18 /* Horizontal Window end MSB 8 bit */
  222. #define VSTART 0x19 /* Vertical Window start MSB 8 bit */
  223. #define VEND 0x1A /* Vertical Window end MSB 8 bit */
  224. #define MIDH 0x1C /* Manufacturer ID byte - high */
  225. #define MIDL 0x1D /* Manufacturer ID byte - low */
  226. #define AEW 0x24 /* AGC/AEC - Stable operating region (upper limit) */
  227. #define AEB 0x25 /* AGC/AEC - Stable operating region (lower limit) */
  228. #define VV 0x26 /* AGC/AEC Fast mode operating region */
  229. #define VV_HIGH_TH_SET(x) VAL_SET(x, 0xF, 0, 4)
  230. #define VV_LOW_TH_SET(x) VAL_SET(x, 0xF, 0, 0)
  231. #define REG2A 0x2A /* Dummy pixel insert MSB */
  232. #define FRARL 0x2B /* Dummy pixel insert LSB */
  233. #define ADDVFL 0x2D /* LSB of insert dummy lines in Vertical direction */
  234. #define ADDVFH 0x2E /* MSB of insert dummy lines in Vertical direction */
  235. #define YAVG 0x2F /* Y/G Channel Average value */
  236. #define REG32 0x32 /* Common Control 32 */
  237. #define REG32_PCLK_DIV_2 0x80 /* PCLK freq divided by 2 */
  238. #define REG32_PCLK_DIV_4 0xC0 /* PCLK freq divided by 4 */
  239. #define ARCOM2 0x34 /* Zoom: Horizontal start point */
  240. #define REG45 0x45 /* Register 45 */
  241. #define FLL 0x46 /* Frame Length Adjustment LSBs */
  242. #define FLH 0x47 /* Frame Length Adjustment MSBs */
  243. #define COM19 0x48 /* Zoom: Vertical start point */
  244. #define ZOOMS 0x49 /* Zoom: Vertical start point */
  245. #define COM22 0x4B /* Flash light control */
  246. #define COM25 0x4E /* For Banding operations */
  247. #define BD50 0x4F /* 50Hz Banding AEC 8 LSBs */
  248. #define BD60 0x50 /* 60Hz Banding AEC 8 LSBs */
  249. #define REG5D 0x5D /* AVGsel[7:0], 16-zone average weight option */
  250. #define REG5E 0x5E /* AVGsel[15:8], 16-zone average weight option */
  251. #define REG5F 0x5F /* AVGsel[23:16], 16-zone average weight option */
  252. #define REG60 0x60 /* AVGsel[31:24], 16-zone average weight option */
  253. #define HISTO_LOW 0x61 /* Histogram Algorithm Low Level */
  254. #define HISTO_HIGH 0x62 /* Histogram Algorithm High Level */
  255. /*
  256. * ID
  257. */
  258. #define MANUFACTURER_ID 0x7FA2
  259. #define PID_OV2640 0x2642
  260. #define VERSION(pid, ver) ((pid << 8) | (ver & 0xFF))
  261. /*
  262. * Struct
  263. */
  264. struct regval_list {
  265. u8 reg_num;
  266. u8 value;
  267. };
  268. struct ov2640_win_size {
  269. char *name;
  270. u32 width;
  271. u32 height;
  272. const struct regval_list *regs;
  273. };
  274. struct ov2640_priv {
  275. struct v4l2_subdev subdev;
  276. struct v4l2_ctrl_handler hdl;
  277. u32 cfmt_code;
  278. struct v4l2_clk *clk;
  279. const struct ov2640_win_size *win;
  280. struct soc_camera_subdev_desc ssdd_dt;
  281. struct gpio_desc *resetb_gpio;
  282. struct gpio_desc *pwdn_gpio;
  283. };
  284. /*
  285. * Registers settings
  286. */
  287. #define ENDMARKER { 0xff, 0xff }
  288. static const struct regval_list ov2640_init_regs[] = {
  289. { BANK_SEL, BANK_SEL_DSP },
  290. { 0x2c, 0xff },
  291. { 0x2e, 0xdf },
  292. { BANK_SEL, BANK_SEL_SENS },
  293. { 0x3c, 0x32 },
  294. { CLKRC, CLKRC_DIV_SET(1) },
  295. { COM2, COM2_OCAP_Nx_SET(3) },
  296. { REG04, REG04_DEF | REG04_HREF_EN },
  297. { COM8, COM8_DEF | COM8_BNDF_EN | COM8_AGC_EN | COM8_AEC_EN },
  298. { COM9, COM9_AGC_GAIN_8x | 0x08},
  299. { 0x2c, 0x0c },
  300. { 0x33, 0x78 },
  301. { 0x3a, 0x33 },
  302. { 0x3b, 0xfb },
  303. { 0x3e, 0x00 },
  304. { 0x43, 0x11 },
  305. { 0x16, 0x10 },
  306. { 0x39, 0x02 },
  307. { 0x35, 0x88 },
  308. { 0x22, 0x0a },
  309. { 0x37, 0x40 },
  310. { 0x23, 0x00 },
  311. { ARCOM2, 0xa0 },
  312. { 0x06, 0x02 },
  313. { 0x06, 0x88 },
  314. { 0x07, 0xc0 },
  315. { 0x0d, 0xb7 },
  316. { 0x0e, 0x01 },
  317. { 0x4c, 0x00 },
  318. { 0x4a, 0x81 },
  319. { 0x21, 0x99 },
  320. { AEW, 0x40 },
  321. { AEB, 0x38 },
  322. { VV, VV_HIGH_TH_SET(0x08) | VV_LOW_TH_SET(0x02) },
  323. { 0x5c, 0x00 },
  324. { 0x63, 0x00 },
  325. { FLL, 0x22 },
  326. { COM3, 0x38 | COM3_BAND_AUTO },
  327. { REG5D, 0x55 },
  328. { REG5E, 0x7d },
  329. { REG5F, 0x7d },
  330. { REG60, 0x55 },
  331. { HISTO_LOW, 0x70 },
  332. { HISTO_HIGH, 0x80 },
  333. { 0x7c, 0x05 },
  334. { 0x20, 0x80 },
  335. { 0x28, 0x30 },
  336. { 0x6c, 0x00 },
  337. { 0x6d, 0x80 },
  338. { 0x6e, 0x00 },
  339. { 0x70, 0x02 },
  340. { 0x71, 0x94 },
  341. { 0x73, 0xc1 },
  342. { 0x3d, 0x34 },
  343. { COM7, COM7_RES_UXGA | COM7_ZOOM_EN },
  344. { 0x5a, 0x57 },
  345. { BD50, 0xbb },
  346. { BD60, 0x9c },
  347. { BANK_SEL, BANK_SEL_DSP },
  348. { 0xe5, 0x7f },
  349. { MC_BIST, MC_BIST_RESET | MC_BIST_BOOT_ROM_SEL },
  350. { 0x41, 0x24 },
  351. { RESET, RESET_JPEG | RESET_DVP },
  352. { 0x76, 0xff },
  353. { 0x33, 0xa0 },
  354. { 0x42, 0x20 },
  355. { 0x43, 0x18 },
  356. { 0x4c, 0x00 },
  357. { CTRL3, CTRL3_BPC_EN | CTRL3_WPC_EN | 0x10 },
  358. { 0x88, 0x3f },
  359. { 0xd7, 0x03 },
  360. { 0xd9, 0x10 },
  361. { R_DVP_SP , R_DVP_SP_AUTO_MODE | 0x2 },
  362. { 0xc8, 0x08 },
  363. { 0xc9, 0x80 },
  364. { BPADDR, 0x00 },
  365. { BPDATA, 0x00 },
  366. { BPADDR, 0x03 },
  367. { BPDATA, 0x48 },
  368. { BPDATA, 0x48 },
  369. { BPADDR, 0x08 },
  370. { BPDATA, 0x20 },
  371. { BPDATA, 0x10 },
  372. { BPDATA, 0x0e },
  373. { 0x90, 0x00 },
  374. { 0x91, 0x0e },
  375. { 0x91, 0x1a },
  376. { 0x91, 0x31 },
  377. { 0x91, 0x5a },
  378. { 0x91, 0x69 },
  379. { 0x91, 0x75 },
  380. { 0x91, 0x7e },
  381. { 0x91, 0x88 },
  382. { 0x91, 0x8f },
  383. { 0x91, 0x96 },
  384. { 0x91, 0xa3 },
  385. { 0x91, 0xaf },
  386. { 0x91, 0xc4 },
  387. { 0x91, 0xd7 },
  388. { 0x91, 0xe8 },
  389. { 0x91, 0x20 },
  390. { 0x92, 0x00 },
  391. { 0x93, 0x06 },
  392. { 0x93, 0xe3 },
  393. { 0x93, 0x03 },
  394. { 0x93, 0x03 },
  395. { 0x93, 0x00 },
  396. { 0x93, 0x02 },
  397. { 0x93, 0x00 },
  398. { 0x93, 0x00 },
  399. { 0x93, 0x00 },
  400. { 0x93, 0x00 },
  401. { 0x93, 0x00 },
  402. { 0x93, 0x00 },
  403. { 0x93, 0x00 },
  404. { 0x96, 0x00 },
  405. { 0x97, 0x08 },
  406. { 0x97, 0x19 },
  407. { 0x97, 0x02 },
  408. { 0x97, 0x0c },
  409. { 0x97, 0x24 },
  410. { 0x97, 0x30 },
  411. { 0x97, 0x28 },
  412. { 0x97, 0x26 },
  413. { 0x97, 0x02 },
  414. { 0x97, 0x98 },
  415. { 0x97, 0x80 },
  416. { 0x97, 0x00 },
  417. { 0x97, 0x00 },
  418. { 0xa4, 0x00 },
  419. { 0xa8, 0x00 },
  420. { 0xc5, 0x11 },
  421. { 0xc6, 0x51 },
  422. { 0xbf, 0x80 },
  423. { 0xc7, 0x10 },
  424. { 0xb6, 0x66 },
  425. { 0xb8, 0xA5 },
  426. { 0xb7, 0x64 },
  427. { 0xb9, 0x7C },
  428. { 0xb3, 0xaf },
  429. { 0xb4, 0x97 },
  430. { 0xb5, 0xFF },
  431. { 0xb0, 0xC5 },
  432. { 0xb1, 0x94 },
  433. { 0xb2, 0x0f },
  434. { 0xc4, 0x5c },
  435. { 0xa6, 0x00 },
  436. { 0xa7, 0x20 },
  437. { 0xa7, 0xd8 },
  438. { 0xa7, 0x1b },
  439. { 0xa7, 0x31 },
  440. { 0xa7, 0x00 },
  441. { 0xa7, 0x18 },
  442. { 0xa7, 0x20 },
  443. { 0xa7, 0xd8 },
  444. { 0xa7, 0x19 },
  445. { 0xa7, 0x31 },
  446. { 0xa7, 0x00 },
  447. { 0xa7, 0x18 },
  448. { 0xa7, 0x20 },
  449. { 0xa7, 0xd8 },
  450. { 0xa7, 0x19 },
  451. { 0xa7, 0x31 },
  452. { 0xa7, 0x00 },
  453. { 0xa7, 0x18 },
  454. { 0x7f, 0x00 },
  455. { 0xe5, 0x1f },
  456. { 0xe1, 0x77 },
  457. { 0xdd, 0x7f },
  458. { CTRL0, CTRL0_YUV422 | CTRL0_YUV_EN | CTRL0_RGB_EN },
  459. ENDMARKER,
  460. };
  461. /*
  462. * Register settings for window size
  463. * The preamble, setup the internal DSP to input an UXGA (1600x1200) image.
  464. * Then the different zooming configurations will setup the output image size.
  465. */
  466. static const struct regval_list ov2640_size_change_preamble_regs[] = {
  467. { BANK_SEL, BANK_SEL_DSP },
  468. { RESET, RESET_DVP },
  469. { HSIZE8, HSIZE8_SET(UXGA_WIDTH) },
  470. { VSIZE8, VSIZE8_SET(UXGA_HEIGHT) },
  471. { CTRL2, CTRL2_DCW_EN | CTRL2_SDE_EN |
  472. CTRL2_UV_AVG_EN | CTRL2_CMX_EN | CTRL2_UV_ADJ_EN },
  473. { HSIZE, HSIZE_SET(UXGA_WIDTH) },
  474. { VSIZE, VSIZE_SET(UXGA_HEIGHT) },
  475. { XOFFL, XOFFL_SET(0) },
  476. { YOFFL, YOFFL_SET(0) },
  477. { VHYX, VHYX_HSIZE_SET(UXGA_WIDTH) | VHYX_VSIZE_SET(UXGA_HEIGHT) |
  478. VHYX_XOFF_SET(0) | VHYX_YOFF_SET(0)},
  479. { TEST, TEST_HSIZE_SET(UXGA_WIDTH) },
  480. ENDMARKER,
  481. };
  482. #define PER_SIZE_REG_SEQ(x, y, v_div, h_div, pclk_div) \
  483. { CTRLI, CTRLI_LP_DP | CTRLI_V_DIV_SET(v_div) | \
  484. CTRLI_H_DIV_SET(h_div)}, \
  485. { ZMOW, ZMOW_OUTW_SET(x) }, \
  486. { ZMOH, ZMOH_OUTH_SET(y) }, \
  487. { ZMHH, ZMHH_OUTW_SET(x) | ZMHH_OUTH_SET(y) }, \
  488. { R_DVP_SP, pclk_div }, \
  489. { RESET, 0x00}
  490. static const struct regval_list ov2640_qcif_regs[] = {
  491. PER_SIZE_REG_SEQ(QCIF_WIDTH, QCIF_HEIGHT, 3, 3, 4),
  492. ENDMARKER,
  493. };
  494. static const struct regval_list ov2640_qvga_regs[] = {
  495. PER_SIZE_REG_SEQ(QVGA_WIDTH, QVGA_HEIGHT, 2, 2, 4),
  496. ENDMARKER,
  497. };
  498. static const struct regval_list ov2640_cif_regs[] = {
  499. PER_SIZE_REG_SEQ(CIF_WIDTH, CIF_HEIGHT, 2, 2, 8),
  500. ENDMARKER,
  501. };
  502. static const struct regval_list ov2640_vga_regs[] = {
  503. PER_SIZE_REG_SEQ(VGA_WIDTH, VGA_HEIGHT, 0, 0, 2),
  504. ENDMARKER,
  505. };
  506. static const struct regval_list ov2640_svga_regs[] = {
  507. PER_SIZE_REG_SEQ(SVGA_WIDTH, SVGA_HEIGHT, 1, 1, 2),
  508. ENDMARKER,
  509. };
  510. static const struct regval_list ov2640_xga_regs[] = {
  511. PER_SIZE_REG_SEQ(XGA_WIDTH, XGA_HEIGHT, 0, 0, 2),
  512. { CTRLI, 0x00},
  513. ENDMARKER,
  514. };
  515. static const struct regval_list ov2640_sxga_regs[] = {
  516. PER_SIZE_REG_SEQ(SXGA_WIDTH, SXGA_HEIGHT, 0, 0, 2),
  517. { CTRLI, 0x00},
  518. { R_DVP_SP, 2 | R_DVP_SP_AUTO_MODE },
  519. ENDMARKER,
  520. };
  521. static const struct regval_list ov2640_uxga_regs[] = {
  522. PER_SIZE_REG_SEQ(UXGA_WIDTH, UXGA_HEIGHT, 0, 0, 0),
  523. { CTRLI, 0x00},
  524. { R_DVP_SP, 0 | R_DVP_SP_AUTO_MODE },
  525. ENDMARKER,
  526. };
  527. #define OV2640_SIZE(n, w, h, r) \
  528. {.name = n, .width = w , .height = h, .regs = r }
  529. static const struct ov2640_win_size ov2640_supported_win_sizes[] = {
  530. OV2640_SIZE("QCIF", QCIF_WIDTH, QCIF_HEIGHT, ov2640_qcif_regs),
  531. OV2640_SIZE("QVGA", QVGA_WIDTH, QVGA_HEIGHT, ov2640_qvga_regs),
  532. OV2640_SIZE("CIF", CIF_WIDTH, CIF_HEIGHT, ov2640_cif_regs),
  533. OV2640_SIZE("VGA", VGA_WIDTH, VGA_HEIGHT, ov2640_vga_regs),
  534. OV2640_SIZE("SVGA", SVGA_WIDTH, SVGA_HEIGHT, ov2640_svga_regs),
  535. OV2640_SIZE("XGA", XGA_WIDTH, XGA_HEIGHT, ov2640_xga_regs),
  536. OV2640_SIZE("SXGA", SXGA_WIDTH, SXGA_HEIGHT, ov2640_sxga_regs),
  537. OV2640_SIZE("UXGA", UXGA_WIDTH, UXGA_HEIGHT, ov2640_uxga_regs),
  538. };
  539. /*
  540. * Register settings for pixel formats
  541. */
  542. static const struct regval_list ov2640_format_change_preamble_regs[] = {
  543. { BANK_SEL, BANK_SEL_DSP },
  544. { R_BYPASS, R_BYPASS_USE_DSP },
  545. ENDMARKER,
  546. };
  547. static const struct regval_list ov2640_yuyv_regs[] = {
  548. { IMAGE_MODE, IMAGE_MODE_YUV422 },
  549. { 0xd7, 0x03 },
  550. { 0x33, 0xa0 },
  551. { 0xe5, 0x1f },
  552. { 0xe1, 0x67 },
  553. { RESET, 0x00 },
  554. { R_BYPASS, R_BYPASS_USE_DSP },
  555. ENDMARKER,
  556. };
  557. static const struct regval_list ov2640_uyvy_regs[] = {
  558. { IMAGE_MODE, IMAGE_MODE_LBYTE_FIRST | IMAGE_MODE_YUV422 },
  559. { 0xd7, 0x01 },
  560. { 0x33, 0xa0 },
  561. { 0xe1, 0x67 },
  562. { RESET, 0x00 },
  563. { R_BYPASS, R_BYPASS_USE_DSP },
  564. ENDMARKER,
  565. };
  566. static const struct regval_list ov2640_rgb565_be_regs[] = {
  567. { IMAGE_MODE, IMAGE_MODE_RGB565 },
  568. { 0xd7, 0x03 },
  569. { RESET, 0x00 },
  570. { R_BYPASS, R_BYPASS_USE_DSP },
  571. ENDMARKER,
  572. };
  573. static const struct regval_list ov2640_rgb565_le_regs[] = {
  574. { IMAGE_MODE, IMAGE_MODE_LBYTE_FIRST | IMAGE_MODE_RGB565 },
  575. { 0xd7, 0x03 },
  576. { RESET, 0x00 },
  577. { R_BYPASS, R_BYPASS_USE_DSP },
  578. ENDMARKER,
  579. };
  580. static u32 ov2640_codes[] = {
  581. MEDIA_BUS_FMT_YUYV8_2X8,
  582. MEDIA_BUS_FMT_UYVY8_2X8,
  583. MEDIA_BUS_FMT_RGB565_2X8_BE,
  584. MEDIA_BUS_FMT_RGB565_2X8_LE,
  585. };
  586. /*
  587. * General functions
  588. */
  589. static struct ov2640_priv *to_ov2640(const struct i2c_client *client)
  590. {
  591. return container_of(i2c_get_clientdata(client), struct ov2640_priv,
  592. subdev);
  593. }
  594. static int ov2640_write_array(struct i2c_client *client,
  595. const struct regval_list *vals)
  596. {
  597. int ret;
  598. while ((vals->reg_num != 0xff) || (vals->value != 0xff)) {
  599. ret = i2c_smbus_write_byte_data(client,
  600. vals->reg_num, vals->value);
  601. dev_vdbg(&client->dev, "array: 0x%02x, 0x%02x",
  602. vals->reg_num, vals->value);
  603. if (ret < 0)
  604. return ret;
  605. vals++;
  606. }
  607. return 0;
  608. }
  609. static int ov2640_mask_set(struct i2c_client *client,
  610. u8 reg, u8 mask, u8 set)
  611. {
  612. s32 val = i2c_smbus_read_byte_data(client, reg);
  613. if (val < 0)
  614. return val;
  615. val &= ~mask;
  616. val |= set & mask;
  617. dev_vdbg(&client->dev, "masks: 0x%02x, 0x%02x", reg, val);
  618. return i2c_smbus_write_byte_data(client, reg, val);
  619. }
  620. static int ov2640_reset(struct i2c_client *client)
  621. {
  622. int ret;
  623. const struct regval_list reset_seq[] = {
  624. {BANK_SEL, BANK_SEL_SENS},
  625. {COM7, COM7_SRST},
  626. ENDMARKER,
  627. };
  628. ret = ov2640_write_array(client, reset_seq);
  629. if (ret)
  630. goto err;
  631. msleep(5);
  632. err:
  633. dev_dbg(&client->dev, "%s: (ret %d)", __func__, ret);
  634. return ret;
  635. }
  636. /*
  637. * soc_camera_ops functions
  638. */
  639. static int ov2640_s_stream(struct v4l2_subdev *sd, int enable)
  640. {
  641. return 0;
  642. }
  643. static int ov2640_s_ctrl(struct v4l2_ctrl *ctrl)
  644. {
  645. struct v4l2_subdev *sd =
  646. &container_of(ctrl->handler, struct ov2640_priv, hdl)->subdev;
  647. struct i2c_client *client = v4l2_get_subdevdata(sd);
  648. u8 val;
  649. int ret;
  650. ret = i2c_smbus_write_byte_data(client, BANK_SEL, BANK_SEL_SENS);
  651. if (ret < 0)
  652. return ret;
  653. switch (ctrl->id) {
  654. case V4L2_CID_VFLIP:
  655. val = ctrl->val ? REG04_VFLIP_IMG : 0x00;
  656. return ov2640_mask_set(client, REG04, REG04_VFLIP_IMG, val);
  657. case V4L2_CID_HFLIP:
  658. val = ctrl->val ? REG04_HFLIP_IMG : 0x00;
  659. return ov2640_mask_set(client, REG04, REG04_HFLIP_IMG, val);
  660. }
  661. return -EINVAL;
  662. }
  663. #ifdef CONFIG_VIDEO_ADV_DEBUG
  664. static int ov2640_g_register(struct v4l2_subdev *sd,
  665. struct v4l2_dbg_register *reg)
  666. {
  667. struct i2c_client *client = v4l2_get_subdevdata(sd);
  668. int ret;
  669. reg->size = 1;
  670. if (reg->reg > 0xff)
  671. return -EINVAL;
  672. ret = i2c_smbus_read_byte_data(client, reg->reg);
  673. if (ret < 0)
  674. return ret;
  675. reg->val = ret;
  676. return 0;
  677. }
  678. static int ov2640_s_register(struct v4l2_subdev *sd,
  679. const struct v4l2_dbg_register *reg)
  680. {
  681. struct i2c_client *client = v4l2_get_subdevdata(sd);
  682. if (reg->reg > 0xff ||
  683. reg->val > 0xff)
  684. return -EINVAL;
  685. return i2c_smbus_write_byte_data(client, reg->reg, reg->val);
  686. }
  687. #endif
  688. static int ov2640_s_power(struct v4l2_subdev *sd, int on)
  689. {
  690. struct i2c_client *client = v4l2_get_subdevdata(sd);
  691. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  692. struct ov2640_priv *priv = to_ov2640(client);
  693. return soc_camera_set_power(&client->dev, ssdd, priv->clk, on);
  694. }
  695. /* Select the nearest higher resolution for capture */
  696. static const struct ov2640_win_size *ov2640_select_win(u32 *width, u32 *height)
  697. {
  698. int i, default_size = ARRAY_SIZE(ov2640_supported_win_sizes) - 1;
  699. for (i = 0; i < ARRAY_SIZE(ov2640_supported_win_sizes); i++) {
  700. if (ov2640_supported_win_sizes[i].width >= *width &&
  701. ov2640_supported_win_sizes[i].height >= *height) {
  702. *width = ov2640_supported_win_sizes[i].width;
  703. *height = ov2640_supported_win_sizes[i].height;
  704. return &ov2640_supported_win_sizes[i];
  705. }
  706. }
  707. *width = ov2640_supported_win_sizes[default_size].width;
  708. *height = ov2640_supported_win_sizes[default_size].height;
  709. return &ov2640_supported_win_sizes[default_size];
  710. }
  711. static int ov2640_set_params(struct i2c_client *client, u32 *width, u32 *height,
  712. u32 code)
  713. {
  714. struct ov2640_priv *priv = to_ov2640(client);
  715. const struct regval_list *selected_cfmt_regs;
  716. int ret;
  717. /* select win */
  718. priv->win = ov2640_select_win(width, height);
  719. /* select format */
  720. priv->cfmt_code = 0;
  721. switch (code) {
  722. case MEDIA_BUS_FMT_RGB565_2X8_BE:
  723. dev_dbg(&client->dev, "%s: Selected cfmt RGB565 BE", __func__);
  724. selected_cfmt_regs = ov2640_rgb565_be_regs;
  725. break;
  726. case MEDIA_BUS_FMT_RGB565_2X8_LE:
  727. dev_dbg(&client->dev, "%s: Selected cfmt RGB565 LE", __func__);
  728. selected_cfmt_regs = ov2640_rgb565_le_regs;
  729. break;
  730. case MEDIA_BUS_FMT_YUYV8_2X8:
  731. dev_dbg(&client->dev, "%s: Selected cfmt YUYV (YUV422)", __func__);
  732. selected_cfmt_regs = ov2640_yuyv_regs;
  733. break;
  734. default:
  735. case MEDIA_BUS_FMT_UYVY8_2X8:
  736. dev_dbg(&client->dev, "%s: Selected cfmt UYVY", __func__);
  737. selected_cfmt_regs = ov2640_uyvy_regs;
  738. }
  739. /* reset hardware */
  740. ov2640_reset(client);
  741. /* initialize the sensor with default data */
  742. dev_dbg(&client->dev, "%s: Init default", __func__);
  743. ret = ov2640_write_array(client, ov2640_init_regs);
  744. if (ret < 0)
  745. goto err;
  746. /* select preamble */
  747. dev_dbg(&client->dev, "%s: Set size to %s", __func__, priv->win->name);
  748. ret = ov2640_write_array(client, ov2640_size_change_preamble_regs);
  749. if (ret < 0)
  750. goto err;
  751. /* set size win */
  752. ret = ov2640_write_array(client, priv->win->regs);
  753. if (ret < 0)
  754. goto err;
  755. /* cfmt preamble */
  756. dev_dbg(&client->dev, "%s: Set cfmt", __func__);
  757. ret = ov2640_write_array(client, ov2640_format_change_preamble_regs);
  758. if (ret < 0)
  759. goto err;
  760. /* set cfmt */
  761. ret = ov2640_write_array(client, selected_cfmt_regs);
  762. if (ret < 0)
  763. goto err;
  764. priv->cfmt_code = code;
  765. *width = priv->win->width;
  766. *height = priv->win->height;
  767. return 0;
  768. err:
  769. dev_err(&client->dev, "%s: Error %d", __func__, ret);
  770. ov2640_reset(client);
  771. priv->win = NULL;
  772. return ret;
  773. }
  774. static int ov2640_get_fmt(struct v4l2_subdev *sd,
  775. struct v4l2_subdev_pad_config *cfg,
  776. struct v4l2_subdev_format *format)
  777. {
  778. struct v4l2_mbus_framefmt *mf = &format->format;
  779. struct i2c_client *client = v4l2_get_subdevdata(sd);
  780. struct ov2640_priv *priv = to_ov2640(client);
  781. if (format->pad)
  782. return -EINVAL;
  783. if (!priv->win) {
  784. u32 width = SVGA_WIDTH, height = SVGA_HEIGHT;
  785. priv->win = ov2640_select_win(&width, &height);
  786. priv->cfmt_code = MEDIA_BUS_FMT_UYVY8_2X8;
  787. }
  788. mf->width = priv->win->width;
  789. mf->height = priv->win->height;
  790. mf->code = priv->cfmt_code;
  791. switch (mf->code) {
  792. case MEDIA_BUS_FMT_RGB565_2X8_BE:
  793. case MEDIA_BUS_FMT_RGB565_2X8_LE:
  794. mf->colorspace = V4L2_COLORSPACE_SRGB;
  795. break;
  796. default:
  797. case MEDIA_BUS_FMT_YUYV8_2X8:
  798. case MEDIA_BUS_FMT_UYVY8_2X8:
  799. mf->colorspace = V4L2_COLORSPACE_JPEG;
  800. }
  801. mf->field = V4L2_FIELD_NONE;
  802. return 0;
  803. }
  804. static int ov2640_set_fmt(struct v4l2_subdev *sd,
  805. struct v4l2_subdev_pad_config *cfg,
  806. struct v4l2_subdev_format *format)
  807. {
  808. struct v4l2_mbus_framefmt *mf = &format->format;
  809. struct i2c_client *client = v4l2_get_subdevdata(sd);
  810. if (format->pad)
  811. return -EINVAL;
  812. /*
  813. * select suitable win, but don't store it
  814. */
  815. ov2640_select_win(&mf->width, &mf->height);
  816. mf->field = V4L2_FIELD_NONE;
  817. switch (mf->code) {
  818. case MEDIA_BUS_FMT_RGB565_2X8_BE:
  819. case MEDIA_BUS_FMT_RGB565_2X8_LE:
  820. mf->colorspace = V4L2_COLORSPACE_SRGB;
  821. break;
  822. default:
  823. mf->code = MEDIA_BUS_FMT_UYVY8_2X8;
  824. case MEDIA_BUS_FMT_YUYV8_2X8:
  825. case MEDIA_BUS_FMT_UYVY8_2X8:
  826. mf->colorspace = V4L2_COLORSPACE_JPEG;
  827. }
  828. if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE)
  829. return ov2640_set_params(client, &mf->width,
  830. &mf->height, mf->code);
  831. cfg->try_fmt = *mf;
  832. return 0;
  833. }
  834. static int ov2640_enum_mbus_code(struct v4l2_subdev *sd,
  835. struct v4l2_subdev_pad_config *cfg,
  836. struct v4l2_subdev_mbus_code_enum *code)
  837. {
  838. if (code->pad || code->index >= ARRAY_SIZE(ov2640_codes))
  839. return -EINVAL;
  840. code->code = ov2640_codes[code->index];
  841. return 0;
  842. }
  843. static int ov2640_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  844. {
  845. a->c.left = 0;
  846. a->c.top = 0;
  847. a->c.width = UXGA_WIDTH;
  848. a->c.height = UXGA_HEIGHT;
  849. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  850. return 0;
  851. }
  852. static int ov2640_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
  853. {
  854. a->bounds.left = 0;
  855. a->bounds.top = 0;
  856. a->bounds.width = UXGA_WIDTH;
  857. a->bounds.height = UXGA_HEIGHT;
  858. a->defrect = a->bounds;
  859. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  860. a->pixelaspect.numerator = 1;
  861. a->pixelaspect.denominator = 1;
  862. return 0;
  863. }
  864. static int ov2640_video_probe(struct i2c_client *client)
  865. {
  866. struct ov2640_priv *priv = to_ov2640(client);
  867. u8 pid, ver, midh, midl;
  868. const char *devname;
  869. int ret;
  870. ret = ov2640_s_power(&priv->subdev, 1);
  871. if (ret < 0)
  872. return ret;
  873. /*
  874. * check and show product ID and manufacturer ID
  875. */
  876. i2c_smbus_write_byte_data(client, BANK_SEL, BANK_SEL_SENS);
  877. pid = i2c_smbus_read_byte_data(client, PID);
  878. ver = i2c_smbus_read_byte_data(client, VER);
  879. midh = i2c_smbus_read_byte_data(client, MIDH);
  880. midl = i2c_smbus_read_byte_data(client, MIDL);
  881. switch (VERSION(pid, ver)) {
  882. case PID_OV2640:
  883. devname = "ov2640";
  884. break;
  885. default:
  886. dev_err(&client->dev,
  887. "Product ID error %x:%x\n", pid, ver);
  888. ret = -ENODEV;
  889. goto done;
  890. }
  891. dev_info(&client->dev,
  892. "%s Product ID %0x:%0x Manufacturer ID %x:%x\n",
  893. devname, pid, ver, midh, midl);
  894. ret = v4l2_ctrl_handler_setup(&priv->hdl);
  895. done:
  896. ov2640_s_power(&priv->subdev, 0);
  897. return ret;
  898. }
  899. static const struct v4l2_ctrl_ops ov2640_ctrl_ops = {
  900. .s_ctrl = ov2640_s_ctrl,
  901. };
  902. static struct v4l2_subdev_core_ops ov2640_subdev_core_ops = {
  903. #ifdef CONFIG_VIDEO_ADV_DEBUG
  904. .g_register = ov2640_g_register,
  905. .s_register = ov2640_s_register,
  906. #endif
  907. .s_power = ov2640_s_power,
  908. };
  909. static int ov2640_g_mbus_config(struct v4l2_subdev *sd,
  910. struct v4l2_mbus_config *cfg)
  911. {
  912. struct i2c_client *client = v4l2_get_subdevdata(sd);
  913. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  914. cfg->flags = V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_MASTER |
  915. V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_HIGH |
  916. V4L2_MBUS_DATA_ACTIVE_HIGH;
  917. cfg->type = V4L2_MBUS_PARALLEL;
  918. cfg->flags = soc_camera_apply_board_flags(ssdd, cfg);
  919. return 0;
  920. }
  921. static struct v4l2_subdev_video_ops ov2640_subdev_video_ops = {
  922. .s_stream = ov2640_s_stream,
  923. .cropcap = ov2640_cropcap,
  924. .g_crop = ov2640_g_crop,
  925. .g_mbus_config = ov2640_g_mbus_config,
  926. };
  927. static const struct v4l2_subdev_pad_ops ov2640_subdev_pad_ops = {
  928. .enum_mbus_code = ov2640_enum_mbus_code,
  929. .get_fmt = ov2640_get_fmt,
  930. .set_fmt = ov2640_set_fmt,
  931. };
  932. static struct v4l2_subdev_ops ov2640_subdev_ops = {
  933. .core = &ov2640_subdev_core_ops,
  934. .video = &ov2640_subdev_video_ops,
  935. .pad = &ov2640_subdev_pad_ops,
  936. };
  937. /* OF probe functions */
  938. static int ov2640_hw_power(struct device *dev, int on)
  939. {
  940. struct i2c_client *client = to_i2c_client(dev);
  941. struct ov2640_priv *priv = to_ov2640(client);
  942. dev_dbg(&client->dev, "%s: %s the camera\n",
  943. __func__, on ? "ENABLE" : "DISABLE");
  944. if (priv->pwdn_gpio)
  945. gpiod_direction_output(priv->pwdn_gpio, !on);
  946. return 0;
  947. }
  948. static int ov2640_hw_reset(struct device *dev)
  949. {
  950. struct i2c_client *client = to_i2c_client(dev);
  951. struct ov2640_priv *priv = to_ov2640(client);
  952. if (priv->resetb_gpio) {
  953. /* Active the resetb pin to perform a reset pulse */
  954. gpiod_direction_output(priv->resetb_gpio, 1);
  955. usleep_range(3000, 5000);
  956. gpiod_direction_output(priv->resetb_gpio, 0);
  957. }
  958. return 0;
  959. }
  960. static int ov2640_probe_dt(struct i2c_client *client,
  961. struct ov2640_priv *priv)
  962. {
  963. /* Request the reset GPIO deasserted */
  964. priv->resetb_gpio = devm_gpiod_get_optional(&client->dev, "resetb",
  965. GPIOD_OUT_LOW);
  966. if (!priv->resetb_gpio)
  967. dev_dbg(&client->dev, "resetb gpio is not assigned!\n");
  968. else if (IS_ERR(priv->resetb_gpio))
  969. return PTR_ERR(priv->resetb_gpio);
  970. /* Request the power down GPIO asserted */
  971. priv->pwdn_gpio = devm_gpiod_get_optional(&client->dev, "pwdn",
  972. GPIOD_OUT_HIGH);
  973. if (!priv->pwdn_gpio)
  974. dev_dbg(&client->dev, "pwdn gpio is not assigned!\n");
  975. else if (IS_ERR(priv->pwdn_gpio))
  976. return PTR_ERR(priv->pwdn_gpio);
  977. /* Initialize the soc_camera_subdev_desc */
  978. priv->ssdd_dt.power = ov2640_hw_power;
  979. priv->ssdd_dt.reset = ov2640_hw_reset;
  980. client->dev.platform_data = &priv->ssdd_dt;
  981. return 0;
  982. }
  983. /*
  984. * i2c_driver functions
  985. */
  986. static int ov2640_probe(struct i2c_client *client,
  987. const struct i2c_device_id *did)
  988. {
  989. struct ov2640_priv *priv;
  990. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  991. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  992. int ret;
  993. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  994. dev_err(&adapter->dev,
  995. "OV2640: I2C-Adapter doesn't support SMBUS\n");
  996. return -EIO;
  997. }
  998. priv = devm_kzalloc(&client->dev, sizeof(struct ov2640_priv), GFP_KERNEL);
  999. if (!priv) {
  1000. dev_err(&adapter->dev,
  1001. "Failed to allocate memory for private data!\n");
  1002. return -ENOMEM;
  1003. }
  1004. priv->clk = v4l2_clk_get(&client->dev, "xvclk");
  1005. if (IS_ERR(priv->clk))
  1006. return -EPROBE_DEFER;
  1007. if (!ssdd && !client->dev.of_node) {
  1008. dev_err(&client->dev, "Missing platform_data for driver\n");
  1009. ret = -EINVAL;
  1010. goto err_clk;
  1011. }
  1012. if (!ssdd) {
  1013. ret = ov2640_probe_dt(client, priv);
  1014. if (ret)
  1015. goto err_clk;
  1016. }
  1017. v4l2_i2c_subdev_init(&priv->subdev, client, &ov2640_subdev_ops);
  1018. v4l2_ctrl_handler_init(&priv->hdl, 2);
  1019. v4l2_ctrl_new_std(&priv->hdl, &ov2640_ctrl_ops,
  1020. V4L2_CID_VFLIP, 0, 1, 1, 0);
  1021. v4l2_ctrl_new_std(&priv->hdl, &ov2640_ctrl_ops,
  1022. V4L2_CID_HFLIP, 0, 1, 1, 0);
  1023. priv->subdev.ctrl_handler = &priv->hdl;
  1024. if (priv->hdl.error) {
  1025. ret = priv->hdl.error;
  1026. goto err_clk;
  1027. }
  1028. ret = ov2640_video_probe(client);
  1029. if (ret < 0)
  1030. goto err_videoprobe;
  1031. ret = v4l2_async_register_subdev(&priv->subdev);
  1032. if (ret < 0)
  1033. goto err_videoprobe;
  1034. dev_info(&adapter->dev, "OV2640 Probed\n");
  1035. return 0;
  1036. err_videoprobe:
  1037. v4l2_ctrl_handler_free(&priv->hdl);
  1038. err_clk:
  1039. v4l2_clk_put(priv->clk);
  1040. return ret;
  1041. }
  1042. static int ov2640_remove(struct i2c_client *client)
  1043. {
  1044. struct ov2640_priv *priv = to_ov2640(client);
  1045. v4l2_async_unregister_subdev(&priv->subdev);
  1046. v4l2_clk_put(priv->clk);
  1047. v4l2_device_unregister_subdev(&priv->subdev);
  1048. v4l2_ctrl_handler_free(&priv->hdl);
  1049. return 0;
  1050. }
  1051. static const struct i2c_device_id ov2640_id[] = {
  1052. { "ov2640", 0 },
  1053. { }
  1054. };
  1055. MODULE_DEVICE_TABLE(i2c, ov2640_id);
  1056. static const struct of_device_id ov2640_of_match[] = {
  1057. {.compatible = "ovti,ov2640", },
  1058. {},
  1059. };
  1060. MODULE_DEVICE_TABLE(of, ov2640_of_match);
  1061. static struct i2c_driver ov2640_i2c_driver = {
  1062. .driver = {
  1063. .name = "ov2640",
  1064. .of_match_table = of_match_ptr(ov2640_of_match),
  1065. },
  1066. .probe = ov2640_probe,
  1067. .remove = ov2640_remove,
  1068. .id_table = ov2640_id,
  1069. };
  1070. module_i2c_driver(ov2640_i2c_driver);
  1071. MODULE_DESCRIPTION("SoC Camera driver for Omni Vision 2640 sensor");
  1072. MODULE_AUTHOR("Alberto Panizzo");
  1073. MODULE_LICENSE("GPL v2");