ov2640.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ov2640 Camera Driver
  4. *
  5. * Copyright (C) 2010 Alberto Panizzo <maramaopercheseimorto@gmail.com>
  6. *
  7. * Based on ov772x, ov9640 drivers and previous non merged implementations.
  8. *
  9. * Copyright 2005-2009 Freescale Semiconductor, Inc. All Rights Reserved.
  10. * Copyright (C) 2006, OmniVision
  11. */
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/i2c.h>
  15. #include <linux/clk.h>
  16. #include <linux/slab.h>
  17. #include <linux/delay.h>
  18. #include <linux/gpio/consumer.h>
  19. #include <linux/v4l2-mediabus.h>
  20. #include <linux/videodev2.h>
  21. #include <media/v4l2-device.h>
  22. #include <media/v4l2-event.h>
  23. #include <media/v4l2-subdev.h>
  24. #include <media/v4l2-ctrls.h>
  25. #include <media/v4l2-image-sizes.h>
  26. #define VAL_SET(x, mask, rshift, lshift) \
  27. ((((x) >> rshift) & mask) << lshift)
  28. /*
  29. * DSP registers
  30. * register offset for BANK_SEL == BANK_SEL_DSP
  31. */
  32. #define R_BYPASS 0x05 /* Bypass DSP */
  33. #define R_BYPASS_DSP_BYPAS 0x01 /* Bypass DSP, sensor out directly */
  34. #define R_BYPASS_USE_DSP 0x00 /* Use the internal DSP */
  35. #define QS 0x44 /* Quantization Scale Factor */
  36. #define CTRLI 0x50
  37. #define CTRLI_LP_DP 0x80
  38. #define CTRLI_ROUND 0x40
  39. #define CTRLI_V_DIV_SET(x) VAL_SET(x, 0x3, 0, 3)
  40. #define CTRLI_H_DIV_SET(x) VAL_SET(x, 0x3, 0, 0)
  41. #define HSIZE 0x51 /* H_SIZE[7:0] (real/4) */
  42. #define HSIZE_SET(x) VAL_SET(x, 0xFF, 2, 0)
  43. #define VSIZE 0x52 /* V_SIZE[7:0] (real/4) */
  44. #define VSIZE_SET(x) VAL_SET(x, 0xFF, 2, 0)
  45. #define XOFFL 0x53 /* OFFSET_X[7:0] */
  46. #define XOFFL_SET(x) VAL_SET(x, 0xFF, 0, 0)
  47. #define YOFFL 0x54 /* OFFSET_Y[7:0] */
  48. #define YOFFL_SET(x) VAL_SET(x, 0xFF, 0, 0)
  49. #define VHYX 0x55 /* Offset and size completion */
  50. #define VHYX_VSIZE_SET(x) VAL_SET(x, 0x1, (8+2), 7)
  51. #define VHYX_HSIZE_SET(x) VAL_SET(x, 0x1, (8+2), 3)
  52. #define VHYX_YOFF_SET(x) VAL_SET(x, 0x3, 8, 4)
  53. #define VHYX_XOFF_SET(x) VAL_SET(x, 0x3, 8, 0)
  54. #define DPRP 0x56
  55. #define TEST 0x57 /* Horizontal size completion */
  56. #define TEST_HSIZE_SET(x) VAL_SET(x, 0x1, (9+2), 7)
  57. #define ZMOW 0x5A /* Zoom: Out Width OUTW[7:0] (real/4) */
  58. #define ZMOW_OUTW_SET(x) VAL_SET(x, 0xFF, 2, 0)
  59. #define ZMOH 0x5B /* Zoom: Out Height OUTH[7:0] (real/4) */
  60. #define ZMOH_OUTH_SET(x) VAL_SET(x, 0xFF, 2, 0)
  61. #define ZMHH 0x5C /* Zoom: Speed and H&W completion */
  62. #define ZMHH_ZSPEED_SET(x) VAL_SET(x, 0x0F, 0, 4)
  63. #define ZMHH_OUTH_SET(x) VAL_SET(x, 0x1, (8+2), 2)
  64. #define ZMHH_OUTW_SET(x) VAL_SET(x, 0x3, (8+2), 0)
  65. #define BPADDR 0x7C /* SDE Indirect Register Access: Address */
  66. #define BPDATA 0x7D /* SDE Indirect Register Access: Data */
  67. #define CTRL2 0x86 /* DSP Module enable 2 */
  68. #define CTRL2_DCW_EN 0x20
  69. #define CTRL2_SDE_EN 0x10
  70. #define CTRL2_UV_ADJ_EN 0x08
  71. #define CTRL2_UV_AVG_EN 0x04
  72. #define CTRL2_CMX_EN 0x01
  73. #define CTRL3 0x87 /* DSP Module enable 3 */
  74. #define CTRL3_BPC_EN 0x80
  75. #define CTRL3_WPC_EN 0x40
  76. #define SIZEL 0x8C /* Image Size Completion */
  77. #define SIZEL_HSIZE8_11_SET(x) VAL_SET(x, 0x1, 11, 6)
  78. #define SIZEL_HSIZE8_SET(x) VAL_SET(x, 0x7, 0, 3)
  79. #define SIZEL_VSIZE8_SET(x) VAL_SET(x, 0x7, 0, 0)
  80. #define HSIZE8 0xC0 /* Image Horizontal Size HSIZE[10:3] */
  81. #define HSIZE8_SET(x) VAL_SET(x, 0xFF, 3, 0)
  82. #define VSIZE8 0xC1 /* Image Vertical Size VSIZE[10:3] */
  83. #define VSIZE8_SET(x) VAL_SET(x, 0xFF, 3, 0)
  84. #define CTRL0 0xC2 /* DSP Module enable 0 */
  85. #define CTRL0_AEC_EN 0x80
  86. #define CTRL0_AEC_SEL 0x40
  87. #define CTRL0_STAT_SEL 0x20
  88. #define CTRL0_VFIRST 0x10
  89. #define CTRL0_YUV422 0x08
  90. #define CTRL0_YUV_EN 0x04
  91. #define CTRL0_RGB_EN 0x02
  92. #define CTRL0_RAW_EN 0x01
  93. #define CTRL1 0xC3 /* DSP Module enable 1 */
  94. #define CTRL1_CIP 0x80
  95. #define CTRL1_DMY 0x40
  96. #define CTRL1_RAW_GMA 0x20
  97. #define CTRL1_DG 0x10
  98. #define CTRL1_AWB 0x08
  99. #define CTRL1_AWB_GAIN 0x04
  100. #define CTRL1_LENC 0x02
  101. #define CTRL1_PRE 0x01
  102. /* REG 0xC7 (unknown name): affects Auto White Balance (AWB)
  103. * AWB_OFF 0x40
  104. * AWB_SIMPLE 0x10
  105. * AWB_ON 0x00 (Advanced AWB ?) */
  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
  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 COM25_50HZ_BANDING_AEC_MSBS_MASK 0xC0 /* 50Hz Bd. AEC 2 MSBs */
  248. #define COM25_60HZ_BANDING_AEC_MSBS_MASK 0x30 /* 60Hz Bd. AEC 2 MSBs */
  249. #define COM25_50HZ_BANDING_AEC_MSBS_SET(x) VAL_SET(x, 0x3, 8, 6)
  250. #define COM25_60HZ_BANDING_AEC_MSBS_SET(x) VAL_SET(x, 0x3, 8, 4)
  251. #define BD50 0x4F /* 50Hz Banding AEC 8 LSBs */
  252. #define BD50_50HZ_BANDING_AEC_LSBS_SET(x) VAL_SET(x, 0xFF, 0, 0)
  253. #define BD60 0x50 /* 60Hz Banding AEC 8 LSBs */
  254. #define BD60_60HZ_BANDING_AEC_LSBS_SET(x) VAL_SET(x, 0xFF, 0, 0)
  255. #define REG5A 0x5A /* 50/60Hz Banding Maximum AEC Step */
  256. #define BD50_MAX_AEC_STEP_MASK 0xF0 /* 50Hz Banding Max. AEC Step */
  257. #define BD60_MAX_AEC_STEP_MASK 0x0F /* 60Hz Banding Max. AEC Step */
  258. #define BD50_MAX_AEC_STEP_SET(x) VAL_SET((x - 1), 0x0F, 0, 4)
  259. #define BD60_MAX_AEC_STEP_SET(x) VAL_SET((x - 1), 0x0F, 0, 0)
  260. #define REG5D 0x5D /* AVGsel[7:0], 16-zone average weight option */
  261. #define REG5E 0x5E /* AVGsel[15:8], 16-zone average weight option */
  262. #define REG5F 0x5F /* AVGsel[23:16], 16-zone average weight option */
  263. #define REG60 0x60 /* AVGsel[31:24], 16-zone average weight option */
  264. #define HISTO_LOW 0x61 /* Histogram Algorithm Low Level */
  265. #define HISTO_HIGH 0x62 /* Histogram Algorithm High Level */
  266. /*
  267. * ID
  268. */
  269. #define MANUFACTURER_ID 0x7FA2
  270. #define PID_OV2640 0x2642
  271. #define VERSION(pid, ver) ((pid << 8) | (ver & 0xFF))
  272. /*
  273. * Struct
  274. */
  275. struct regval_list {
  276. u8 reg_num;
  277. u8 value;
  278. };
  279. struct ov2640_win_size {
  280. char *name;
  281. u32 width;
  282. u32 height;
  283. const struct regval_list *regs;
  284. };
  285. struct ov2640_priv {
  286. struct v4l2_subdev subdev;
  287. struct media_pad pad;
  288. struct v4l2_ctrl_handler hdl;
  289. u32 cfmt_code;
  290. struct clk *clk;
  291. const struct ov2640_win_size *win;
  292. struct gpio_desc *resetb_gpio;
  293. struct gpio_desc *pwdn_gpio;
  294. struct mutex lock; /* lock to protect streaming and power_count */
  295. bool streaming;
  296. int power_count;
  297. };
  298. /*
  299. * Registers settings
  300. */
  301. #define ENDMARKER { 0xff, 0xff }
  302. static const struct regval_list ov2640_init_regs[] = {
  303. { BANK_SEL, BANK_SEL_DSP },
  304. { 0x2c, 0xff },
  305. { 0x2e, 0xdf },
  306. { BANK_SEL, BANK_SEL_SENS },
  307. { 0x3c, 0x32 },
  308. { CLKRC, CLKRC_DIV_SET(1) },
  309. { COM2, COM2_OCAP_Nx_SET(3) },
  310. { REG04, REG04_DEF | REG04_HREF_EN },
  311. { COM8, COM8_DEF | COM8_BNDF_EN | COM8_AGC_EN | COM8_AEC_EN },
  312. { COM9, COM9_AGC_GAIN_8x | 0x08},
  313. { 0x2c, 0x0c },
  314. { 0x33, 0x78 },
  315. { 0x3a, 0x33 },
  316. { 0x3b, 0xfb },
  317. { 0x3e, 0x00 },
  318. { 0x43, 0x11 },
  319. { 0x16, 0x10 },
  320. { 0x39, 0x02 },
  321. { 0x35, 0x88 },
  322. { 0x22, 0x0a },
  323. { 0x37, 0x40 },
  324. { 0x23, 0x00 },
  325. { ARCOM2, 0xa0 },
  326. { 0x06, 0x02 },
  327. { 0x06, 0x88 },
  328. { 0x07, 0xc0 },
  329. { 0x0d, 0xb7 },
  330. { 0x0e, 0x01 },
  331. { 0x4c, 0x00 },
  332. { 0x4a, 0x81 },
  333. { 0x21, 0x99 },
  334. { AEW, 0x40 },
  335. { AEB, 0x38 },
  336. { VV, VV_HIGH_TH_SET(0x08) | VV_LOW_TH_SET(0x02) },
  337. { 0x5c, 0x00 },
  338. { 0x63, 0x00 },
  339. { FLL, 0x22 },
  340. { COM3, 0x38 | COM3_BAND_AUTO },
  341. { REG5D, 0x55 },
  342. { REG5E, 0x7d },
  343. { REG5F, 0x7d },
  344. { REG60, 0x55 },
  345. { HISTO_LOW, 0x70 },
  346. { HISTO_HIGH, 0x80 },
  347. { 0x7c, 0x05 },
  348. { 0x20, 0x80 },
  349. { 0x28, 0x30 },
  350. { 0x6c, 0x00 },
  351. { 0x6d, 0x80 },
  352. { 0x6e, 0x00 },
  353. { 0x70, 0x02 },
  354. { 0x71, 0x94 },
  355. { 0x73, 0xc1 },
  356. { 0x3d, 0x34 },
  357. { COM7, COM7_RES_UXGA | COM7_ZOOM_EN },
  358. { REG5A, BD50_MAX_AEC_STEP_SET(6)
  359. | BD60_MAX_AEC_STEP_SET(8) }, /* 0x57 */
  360. { COM25, COM25_50HZ_BANDING_AEC_MSBS_SET(0x0bb)
  361. | COM25_60HZ_BANDING_AEC_MSBS_SET(0x09c) }, /* 0x00 */
  362. { BD50, BD50_50HZ_BANDING_AEC_LSBS_SET(0x0bb) }, /* 0xbb */
  363. { BD60, BD60_60HZ_BANDING_AEC_LSBS_SET(0x09c) }, /* 0x9c */
  364. { BANK_SEL, BANK_SEL_DSP },
  365. { 0xe5, 0x7f },
  366. { MC_BIST, MC_BIST_RESET | MC_BIST_BOOT_ROM_SEL },
  367. { 0x41, 0x24 },
  368. { RESET, RESET_JPEG | RESET_DVP },
  369. { 0x76, 0xff },
  370. { 0x33, 0xa0 },
  371. { 0x42, 0x20 },
  372. { 0x43, 0x18 },
  373. { 0x4c, 0x00 },
  374. { CTRL3, CTRL3_BPC_EN | CTRL3_WPC_EN | 0x10 },
  375. { 0x88, 0x3f },
  376. { 0xd7, 0x03 },
  377. { 0xd9, 0x10 },
  378. { R_DVP_SP, R_DVP_SP_AUTO_MODE | 0x2 },
  379. { 0xc8, 0x08 },
  380. { 0xc9, 0x80 },
  381. { BPADDR, 0x00 },
  382. { BPDATA, 0x00 },
  383. { BPADDR, 0x03 },
  384. { BPDATA, 0x48 },
  385. { BPDATA, 0x48 },
  386. { BPADDR, 0x08 },
  387. { BPDATA, 0x20 },
  388. { BPDATA, 0x10 },
  389. { BPDATA, 0x0e },
  390. { 0x90, 0x00 },
  391. { 0x91, 0x0e },
  392. { 0x91, 0x1a },
  393. { 0x91, 0x31 },
  394. { 0x91, 0x5a },
  395. { 0x91, 0x69 },
  396. { 0x91, 0x75 },
  397. { 0x91, 0x7e },
  398. { 0x91, 0x88 },
  399. { 0x91, 0x8f },
  400. { 0x91, 0x96 },
  401. { 0x91, 0xa3 },
  402. { 0x91, 0xaf },
  403. { 0x91, 0xc4 },
  404. { 0x91, 0xd7 },
  405. { 0x91, 0xe8 },
  406. { 0x91, 0x20 },
  407. { 0x92, 0x00 },
  408. { 0x93, 0x06 },
  409. { 0x93, 0xe3 },
  410. { 0x93, 0x03 },
  411. { 0x93, 0x03 },
  412. { 0x93, 0x00 },
  413. { 0x93, 0x02 },
  414. { 0x93, 0x00 },
  415. { 0x93, 0x00 },
  416. { 0x93, 0x00 },
  417. { 0x93, 0x00 },
  418. { 0x93, 0x00 },
  419. { 0x93, 0x00 },
  420. { 0x93, 0x00 },
  421. { 0x96, 0x00 },
  422. { 0x97, 0x08 },
  423. { 0x97, 0x19 },
  424. { 0x97, 0x02 },
  425. { 0x97, 0x0c },
  426. { 0x97, 0x24 },
  427. { 0x97, 0x30 },
  428. { 0x97, 0x28 },
  429. { 0x97, 0x26 },
  430. { 0x97, 0x02 },
  431. { 0x97, 0x98 },
  432. { 0x97, 0x80 },
  433. { 0x97, 0x00 },
  434. { 0x97, 0x00 },
  435. { 0xa4, 0x00 },
  436. { 0xa8, 0x00 },
  437. { 0xc5, 0x11 },
  438. { 0xc6, 0x51 },
  439. { 0xbf, 0x80 },
  440. { 0xc7, 0x10 }, /* simple AWB */
  441. { 0xb6, 0x66 },
  442. { 0xb8, 0xA5 },
  443. { 0xb7, 0x64 },
  444. { 0xb9, 0x7C },
  445. { 0xb3, 0xaf },
  446. { 0xb4, 0x97 },
  447. { 0xb5, 0xFF },
  448. { 0xb0, 0xC5 },
  449. { 0xb1, 0x94 },
  450. { 0xb2, 0x0f },
  451. { 0xc4, 0x5c },
  452. { 0xa6, 0x00 },
  453. { 0xa7, 0x20 },
  454. { 0xa7, 0xd8 },
  455. { 0xa7, 0x1b },
  456. { 0xa7, 0x31 },
  457. { 0xa7, 0x00 },
  458. { 0xa7, 0x18 },
  459. { 0xa7, 0x20 },
  460. { 0xa7, 0xd8 },
  461. { 0xa7, 0x19 },
  462. { 0xa7, 0x31 },
  463. { 0xa7, 0x00 },
  464. { 0xa7, 0x18 },
  465. { 0xa7, 0x20 },
  466. { 0xa7, 0xd8 },
  467. { 0xa7, 0x19 },
  468. { 0xa7, 0x31 },
  469. { 0xa7, 0x00 },
  470. { 0xa7, 0x18 },
  471. { 0x7f, 0x00 },
  472. { 0xe5, 0x1f },
  473. { 0xe1, 0x77 },
  474. { 0xdd, 0x7f },
  475. { CTRL0, CTRL0_YUV422 | CTRL0_YUV_EN | CTRL0_RGB_EN },
  476. ENDMARKER,
  477. };
  478. /*
  479. * Register settings for window size
  480. * The preamble, setup the internal DSP to input an UXGA (1600x1200) image.
  481. * Then the different zooming configurations will setup the output image size.
  482. */
  483. static const struct regval_list ov2640_size_change_preamble_regs[] = {
  484. { BANK_SEL, BANK_SEL_DSP },
  485. { RESET, RESET_DVP },
  486. { SIZEL, SIZEL_HSIZE8_11_SET(UXGA_WIDTH) |
  487. SIZEL_HSIZE8_SET(UXGA_WIDTH) |
  488. SIZEL_VSIZE8_SET(UXGA_HEIGHT) },
  489. { HSIZE8, HSIZE8_SET(UXGA_WIDTH) },
  490. { VSIZE8, VSIZE8_SET(UXGA_HEIGHT) },
  491. { CTRL2, CTRL2_DCW_EN | CTRL2_SDE_EN |
  492. CTRL2_UV_AVG_EN | CTRL2_CMX_EN | CTRL2_UV_ADJ_EN },
  493. { HSIZE, HSIZE_SET(UXGA_WIDTH) },
  494. { VSIZE, VSIZE_SET(UXGA_HEIGHT) },
  495. { XOFFL, XOFFL_SET(0) },
  496. { YOFFL, YOFFL_SET(0) },
  497. { VHYX, VHYX_HSIZE_SET(UXGA_WIDTH) | VHYX_VSIZE_SET(UXGA_HEIGHT) |
  498. VHYX_XOFF_SET(0) | VHYX_YOFF_SET(0)},
  499. { TEST, TEST_HSIZE_SET(UXGA_WIDTH) },
  500. ENDMARKER,
  501. };
  502. #define PER_SIZE_REG_SEQ(x, y, v_div, h_div, pclk_div) \
  503. { CTRLI, CTRLI_LP_DP | CTRLI_V_DIV_SET(v_div) | \
  504. CTRLI_H_DIV_SET(h_div)}, \
  505. { ZMOW, ZMOW_OUTW_SET(x) }, \
  506. { ZMOH, ZMOH_OUTH_SET(y) }, \
  507. { ZMHH, ZMHH_OUTW_SET(x) | ZMHH_OUTH_SET(y) }, \
  508. { R_DVP_SP, pclk_div }, \
  509. { RESET, 0x00}
  510. static const struct regval_list ov2640_qcif_regs[] = {
  511. PER_SIZE_REG_SEQ(QCIF_WIDTH, QCIF_HEIGHT, 3, 3, 4),
  512. ENDMARKER,
  513. };
  514. static const struct regval_list ov2640_qvga_regs[] = {
  515. PER_SIZE_REG_SEQ(QVGA_WIDTH, QVGA_HEIGHT, 2, 2, 4),
  516. ENDMARKER,
  517. };
  518. static const struct regval_list ov2640_cif_regs[] = {
  519. PER_SIZE_REG_SEQ(CIF_WIDTH, CIF_HEIGHT, 2, 2, 8),
  520. ENDMARKER,
  521. };
  522. static const struct regval_list ov2640_vga_regs[] = {
  523. PER_SIZE_REG_SEQ(VGA_WIDTH, VGA_HEIGHT, 0, 0, 2),
  524. ENDMARKER,
  525. };
  526. static const struct regval_list ov2640_svga_regs[] = {
  527. PER_SIZE_REG_SEQ(SVGA_WIDTH, SVGA_HEIGHT, 1, 1, 2),
  528. ENDMARKER,
  529. };
  530. static const struct regval_list ov2640_xga_regs[] = {
  531. PER_SIZE_REG_SEQ(XGA_WIDTH, XGA_HEIGHT, 0, 0, 2),
  532. { CTRLI, 0x00},
  533. ENDMARKER,
  534. };
  535. static const struct regval_list ov2640_sxga_regs[] = {
  536. PER_SIZE_REG_SEQ(SXGA_WIDTH, SXGA_HEIGHT, 0, 0, 2),
  537. { CTRLI, 0x00},
  538. { R_DVP_SP, 2 | R_DVP_SP_AUTO_MODE },
  539. ENDMARKER,
  540. };
  541. static const struct regval_list ov2640_uxga_regs[] = {
  542. PER_SIZE_REG_SEQ(UXGA_WIDTH, UXGA_HEIGHT, 0, 0, 0),
  543. { CTRLI, 0x00},
  544. { R_DVP_SP, 0 | R_DVP_SP_AUTO_MODE },
  545. ENDMARKER,
  546. };
  547. #define OV2640_SIZE(n, w, h, r) \
  548. {.name = n, .width = w , .height = h, .regs = r }
  549. static const struct ov2640_win_size ov2640_supported_win_sizes[] = {
  550. OV2640_SIZE("QCIF", QCIF_WIDTH, QCIF_HEIGHT, ov2640_qcif_regs),
  551. OV2640_SIZE("QVGA", QVGA_WIDTH, QVGA_HEIGHT, ov2640_qvga_regs),
  552. OV2640_SIZE("CIF", CIF_WIDTH, CIF_HEIGHT, ov2640_cif_regs),
  553. OV2640_SIZE("VGA", VGA_WIDTH, VGA_HEIGHT, ov2640_vga_regs),
  554. OV2640_SIZE("SVGA", SVGA_WIDTH, SVGA_HEIGHT, ov2640_svga_regs),
  555. OV2640_SIZE("XGA", XGA_WIDTH, XGA_HEIGHT, ov2640_xga_regs),
  556. OV2640_SIZE("SXGA", SXGA_WIDTH, SXGA_HEIGHT, ov2640_sxga_regs),
  557. OV2640_SIZE("UXGA", UXGA_WIDTH, UXGA_HEIGHT, ov2640_uxga_regs),
  558. };
  559. /*
  560. * Register settings for pixel formats
  561. */
  562. static const struct regval_list ov2640_format_change_preamble_regs[] = {
  563. { BANK_SEL, BANK_SEL_DSP },
  564. { R_BYPASS, R_BYPASS_USE_DSP },
  565. ENDMARKER,
  566. };
  567. static const struct regval_list ov2640_yuyv_regs[] = {
  568. { IMAGE_MODE, IMAGE_MODE_YUV422 },
  569. { 0xd7, 0x03 },
  570. { 0x33, 0xa0 },
  571. { 0xe5, 0x1f },
  572. { 0xe1, 0x67 },
  573. { RESET, 0x00 },
  574. { R_BYPASS, R_BYPASS_USE_DSP },
  575. ENDMARKER,
  576. };
  577. static const struct regval_list ov2640_uyvy_regs[] = {
  578. { IMAGE_MODE, IMAGE_MODE_LBYTE_FIRST | IMAGE_MODE_YUV422 },
  579. { 0xd7, 0x01 },
  580. { 0x33, 0xa0 },
  581. { 0xe1, 0x67 },
  582. { RESET, 0x00 },
  583. { R_BYPASS, R_BYPASS_USE_DSP },
  584. ENDMARKER,
  585. };
  586. static const struct regval_list ov2640_rgb565_be_regs[] = {
  587. { IMAGE_MODE, IMAGE_MODE_RGB565 },
  588. { 0xd7, 0x03 },
  589. { RESET, 0x00 },
  590. { R_BYPASS, R_BYPASS_USE_DSP },
  591. ENDMARKER,
  592. };
  593. static const struct regval_list ov2640_rgb565_le_regs[] = {
  594. { IMAGE_MODE, IMAGE_MODE_LBYTE_FIRST | IMAGE_MODE_RGB565 },
  595. { 0xd7, 0x03 },
  596. { RESET, 0x00 },
  597. { R_BYPASS, R_BYPASS_USE_DSP },
  598. ENDMARKER,
  599. };
  600. static u32 ov2640_codes[] = {
  601. MEDIA_BUS_FMT_YUYV8_2X8,
  602. MEDIA_BUS_FMT_UYVY8_2X8,
  603. MEDIA_BUS_FMT_YVYU8_2X8,
  604. MEDIA_BUS_FMT_VYUY8_2X8,
  605. MEDIA_BUS_FMT_RGB565_2X8_BE,
  606. MEDIA_BUS_FMT_RGB565_2X8_LE,
  607. };
  608. /*
  609. * General functions
  610. */
  611. static struct ov2640_priv *to_ov2640(const struct i2c_client *client)
  612. {
  613. return container_of(i2c_get_clientdata(client), struct ov2640_priv,
  614. subdev);
  615. }
  616. static int ov2640_write_array(struct i2c_client *client,
  617. const struct regval_list *vals)
  618. {
  619. int ret;
  620. while ((vals->reg_num != 0xff) || (vals->value != 0xff)) {
  621. ret = i2c_smbus_write_byte_data(client,
  622. vals->reg_num, vals->value);
  623. dev_vdbg(&client->dev, "array: 0x%02x, 0x%02x",
  624. vals->reg_num, vals->value);
  625. if (ret < 0)
  626. return ret;
  627. vals++;
  628. }
  629. return 0;
  630. }
  631. static int ov2640_mask_set(struct i2c_client *client,
  632. u8 reg, u8 mask, u8 set)
  633. {
  634. s32 val = i2c_smbus_read_byte_data(client, reg);
  635. if (val < 0)
  636. return val;
  637. val &= ~mask;
  638. val |= set & mask;
  639. dev_vdbg(&client->dev, "masks: 0x%02x, 0x%02x", reg, val);
  640. return i2c_smbus_write_byte_data(client, reg, val);
  641. }
  642. static int ov2640_reset(struct i2c_client *client)
  643. {
  644. int ret;
  645. static const struct regval_list reset_seq[] = {
  646. {BANK_SEL, BANK_SEL_SENS},
  647. {COM7, COM7_SRST},
  648. ENDMARKER,
  649. };
  650. ret = ov2640_write_array(client, reset_seq);
  651. if (ret)
  652. goto err;
  653. msleep(5);
  654. err:
  655. dev_dbg(&client->dev, "%s: (ret %d)", __func__, ret);
  656. return ret;
  657. }
  658. static const char * const ov2640_test_pattern_menu[] = {
  659. "Disabled",
  660. "Eight Vertical Colour Bars",
  661. };
  662. /*
  663. * functions
  664. */
  665. static int ov2640_s_ctrl(struct v4l2_ctrl *ctrl)
  666. {
  667. struct v4l2_subdev *sd =
  668. &container_of(ctrl->handler, struct ov2640_priv, hdl)->subdev;
  669. struct i2c_client *client = v4l2_get_subdevdata(sd);
  670. struct ov2640_priv *priv = to_ov2640(client);
  671. u8 val;
  672. int ret;
  673. /* v4l2_ctrl_lock() locks our own mutex */
  674. /*
  675. * If the device is not powered up by the host driver, do not apply any
  676. * controls to H/W at this time. Instead the controls will be restored
  677. * when the streaming is started.
  678. */
  679. if (!priv->power_count)
  680. return 0;
  681. ret = i2c_smbus_write_byte_data(client, BANK_SEL, BANK_SEL_SENS);
  682. if (ret < 0)
  683. return ret;
  684. switch (ctrl->id) {
  685. case V4L2_CID_VFLIP:
  686. val = ctrl->val ? REG04_VFLIP_IMG | REG04_VREF_EN : 0x00;
  687. return ov2640_mask_set(client, REG04,
  688. REG04_VFLIP_IMG | REG04_VREF_EN, val);
  689. /* NOTE: REG04_VREF_EN: 1 line shift / even/odd line swap */
  690. case V4L2_CID_HFLIP:
  691. val = ctrl->val ? REG04_HFLIP_IMG : 0x00;
  692. return ov2640_mask_set(client, REG04, REG04_HFLIP_IMG, val);
  693. case V4L2_CID_TEST_PATTERN:
  694. val = ctrl->val ? COM7_COLOR_BAR_TEST : 0x00;
  695. return ov2640_mask_set(client, COM7, COM7_COLOR_BAR_TEST, val);
  696. }
  697. return -EINVAL;
  698. }
  699. #ifdef CONFIG_VIDEO_ADV_DEBUG
  700. static int ov2640_g_register(struct v4l2_subdev *sd,
  701. struct v4l2_dbg_register *reg)
  702. {
  703. struct i2c_client *client = v4l2_get_subdevdata(sd);
  704. int ret;
  705. reg->size = 1;
  706. if (reg->reg > 0xff)
  707. return -EINVAL;
  708. ret = i2c_smbus_read_byte_data(client, reg->reg);
  709. if (ret < 0)
  710. return ret;
  711. reg->val = ret;
  712. return 0;
  713. }
  714. static int ov2640_s_register(struct v4l2_subdev *sd,
  715. const struct v4l2_dbg_register *reg)
  716. {
  717. struct i2c_client *client = v4l2_get_subdevdata(sd);
  718. if (reg->reg > 0xff ||
  719. reg->val > 0xff)
  720. return -EINVAL;
  721. return i2c_smbus_write_byte_data(client, reg->reg, reg->val);
  722. }
  723. #endif
  724. static void ov2640_set_power(struct ov2640_priv *priv, int on)
  725. {
  726. #ifdef CONFIG_GPIOLIB
  727. if (priv->pwdn_gpio)
  728. gpiod_direction_output(priv->pwdn_gpio, !on);
  729. if (on && priv->resetb_gpio) {
  730. /* Active the resetb pin to perform a reset pulse */
  731. gpiod_direction_output(priv->resetb_gpio, 1);
  732. usleep_range(3000, 5000);
  733. gpiod_set_value(priv->resetb_gpio, 0);
  734. }
  735. #endif
  736. }
  737. static int ov2640_s_power(struct v4l2_subdev *sd, int on)
  738. {
  739. struct i2c_client *client = v4l2_get_subdevdata(sd);
  740. struct ov2640_priv *priv = to_ov2640(client);
  741. mutex_lock(&priv->lock);
  742. /*
  743. * If the power count is modified from 0 to != 0 or from != 0 to 0,
  744. * update the power state.
  745. */
  746. if (priv->power_count == !on)
  747. ov2640_set_power(priv, on);
  748. priv->power_count += on ? 1 : -1;
  749. WARN_ON(priv->power_count < 0);
  750. mutex_unlock(&priv->lock);
  751. return 0;
  752. }
  753. /* Select the nearest higher resolution for capture */
  754. static const struct ov2640_win_size *ov2640_select_win(u32 width, u32 height)
  755. {
  756. int i, default_size = ARRAY_SIZE(ov2640_supported_win_sizes) - 1;
  757. for (i = 0; i < ARRAY_SIZE(ov2640_supported_win_sizes); i++) {
  758. if (ov2640_supported_win_sizes[i].width >= width &&
  759. ov2640_supported_win_sizes[i].height >= height)
  760. return &ov2640_supported_win_sizes[i];
  761. }
  762. return &ov2640_supported_win_sizes[default_size];
  763. }
  764. static int ov2640_set_params(struct i2c_client *client,
  765. const struct ov2640_win_size *win, u32 code)
  766. {
  767. const struct regval_list *selected_cfmt_regs;
  768. u8 val;
  769. int ret;
  770. switch (code) {
  771. case MEDIA_BUS_FMT_RGB565_2X8_BE:
  772. dev_dbg(&client->dev, "%s: Selected cfmt RGB565 BE", __func__);
  773. selected_cfmt_regs = ov2640_rgb565_be_regs;
  774. break;
  775. case MEDIA_BUS_FMT_RGB565_2X8_LE:
  776. dev_dbg(&client->dev, "%s: Selected cfmt RGB565 LE", __func__);
  777. selected_cfmt_regs = ov2640_rgb565_le_regs;
  778. break;
  779. case MEDIA_BUS_FMT_YUYV8_2X8:
  780. dev_dbg(&client->dev, "%s: Selected cfmt YUYV (YUV422)", __func__);
  781. selected_cfmt_regs = ov2640_yuyv_regs;
  782. break;
  783. case MEDIA_BUS_FMT_UYVY8_2X8:
  784. default:
  785. dev_dbg(&client->dev, "%s: Selected cfmt UYVY", __func__);
  786. selected_cfmt_regs = ov2640_uyvy_regs;
  787. break;
  788. case MEDIA_BUS_FMT_YVYU8_2X8:
  789. dev_dbg(&client->dev, "%s: Selected cfmt YVYU", __func__);
  790. selected_cfmt_regs = ov2640_yuyv_regs;
  791. break;
  792. case MEDIA_BUS_FMT_VYUY8_2X8:
  793. dev_dbg(&client->dev, "%s: Selected cfmt VYUY", __func__);
  794. selected_cfmt_regs = ov2640_uyvy_regs;
  795. break;
  796. }
  797. /* reset hardware */
  798. ov2640_reset(client);
  799. /* initialize the sensor with default data */
  800. dev_dbg(&client->dev, "%s: Init default", __func__);
  801. ret = ov2640_write_array(client, ov2640_init_regs);
  802. if (ret < 0)
  803. goto err;
  804. /* select preamble */
  805. dev_dbg(&client->dev, "%s: Set size to %s", __func__, win->name);
  806. ret = ov2640_write_array(client, ov2640_size_change_preamble_regs);
  807. if (ret < 0)
  808. goto err;
  809. /* set size win */
  810. ret = ov2640_write_array(client, win->regs);
  811. if (ret < 0)
  812. goto err;
  813. /* cfmt preamble */
  814. dev_dbg(&client->dev, "%s: Set cfmt", __func__);
  815. ret = ov2640_write_array(client, ov2640_format_change_preamble_regs);
  816. if (ret < 0)
  817. goto err;
  818. /* set cfmt */
  819. ret = ov2640_write_array(client, selected_cfmt_regs);
  820. if (ret < 0)
  821. goto err;
  822. val = (code == MEDIA_BUS_FMT_YVYU8_2X8)
  823. || (code == MEDIA_BUS_FMT_VYUY8_2X8) ? CTRL0_VFIRST : 0x00;
  824. ret = ov2640_mask_set(client, CTRL0, CTRL0_VFIRST, val);
  825. if (ret < 0)
  826. goto err;
  827. return 0;
  828. err:
  829. dev_err(&client->dev, "%s: Error %d", __func__, ret);
  830. ov2640_reset(client);
  831. return ret;
  832. }
  833. static int ov2640_get_fmt(struct v4l2_subdev *sd,
  834. struct v4l2_subdev_state *sd_state,
  835. struct v4l2_subdev_format *format)
  836. {
  837. struct v4l2_mbus_framefmt *mf = &format->format;
  838. struct i2c_client *client = v4l2_get_subdevdata(sd);
  839. struct ov2640_priv *priv = to_ov2640(client);
  840. if (format->pad)
  841. return -EINVAL;
  842. if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
  843. mf = v4l2_subdev_state_get_format(sd_state, 0);
  844. format->format = *mf;
  845. return 0;
  846. }
  847. mf->width = priv->win->width;
  848. mf->height = priv->win->height;
  849. mf->code = priv->cfmt_code;
  850. mf->colorspace = V4L2_COLORSPACE_SRGB;
  851. mf->field = V4L2_FIELD_NONE;
  852. mf->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
  853. mf->quantization = V4L2_QUANTIZATION_DEFAULT;
  854. mf->xfer_func = V4L2_XFER_FUNC_DEFAULT;
  855. return 0;
  856. }
  857. static int ov2640_set_fmt(struct v4l2_subdev *sd,
  858. struct v4l2_subdev_state *sd_state,
  859. struct v4l2_subdev_format *format)
  860. {
  861. struct v4l2_mbus_framefmt *mf = &format->format;
  862. struct i2c_client *client = v4l2_get_subdevdata(sd);
  863. struct ov2640_priv *priv = to_ov2640(client);
  864. const struct ov2640_win_size *win;
  865. int ret = 0;
  866. if (format->pad)
  867. return -EINVAL;
  868. mutex_lock(&priv->lock);
  869. /* select suitable win */
  870. win = ov2640_select_win(mf->width, mf->height);
  871. mf->width = win->width;
  872. mf->height = win->height;
  873. mf->field = V4L2_FIELD_NONE;
  874. mf->colorspace = V4L2_COLORSPACE_SRGB;
  875. mf->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
  876. mf->quantization = V4L2_QUANTIZATION_DEFAULT;
  877. mf->xfer_func = V4L2_XFER_FUNC_DEFAULT;
  878. switch (mf->code) {
  879. case MEDIA_BUS_FMT_RGB565_2X8_BE:
  880. case MEDIA_BUS_FMT_RGB565_2X8_LE:
  881. case MEDIA_BUS_FMT_YUYV8_2X8:
  882. case MEDIA_BUS_FMT_UYVY8_2X8:
  883. case MEDIA_BUS_FMT_YVYU8_2X8:
  884. case MEDIA_BUS_FMT_VYUY8_2X8:
  885. break;
  886. default:
  887. mf->code = MEDIA_BUS_FMT_UYVY8_2X8;
  888. break;
  889. }
  890. if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
  891. struct ov2640_priv *priv = to_ov2640(client);
  892. if (priv->streaming) {
  893. ret = -EBUSY;
  894. goto out;
  895. }
  896. /* select win */
  897. priv->win = win;
  898. /* select format */
  899. priv->cfmt_code = mf->code;
  900. } else {
  901. *v4l2_subdev_state_get_format(sd_state, 0) = *mf;
  902. }
  903. out:
  904. mutex_unlock(&priv->lock);
  905. return ret;
  906. }
  907. static int ov2640_init_state(struct v4l2_subdev *sd,
  908. struct v4l2_subdev_state *sd_state)
  909. {
  910. struct v4l2_mbus_framefmt *try_fmt =
  911. v4l2_subdev_state_get_format(sd_state, 0);
  912. const struct ov2640_win_size *win =
  913. ov2640_select_win(SVGA_WIDTH, SVGA_HEIGHT);
  914. try_fmt->width = win->width;
  915. try_fmt->height = win->height;
  916. try_fmt->code = MEDIA_BUS_FMT_UYVY8_2X8;
  917. try_fmt->colorspace = V4L2_COLORSPACE_SRGB;
  918. try_fmt->field = V4L2_FIELD_NONE;
  919. try_fmt->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
  920. try_fmt->quantization = V4L2_QUANTIZATION_DEFAULT;
  921. try_fmt->xfer_func = V4L2_XFER_FUNC_DEFAULT;
  922. return 0;
  923. }
  924. static int ov2640_enum_mbus_code(struct v4l2_subdev *sd,
  925. struct v4l2_subdev_state *sd_state,
  926. struct v4l2_subdev_mbus_code_enum *code)
  927. {
  928. if (code->pad || code->index >= ARRAY_SIZE(ov2640_codes))
  929. return -EINVAL;
  930. code->code = ov2640_codes[code->index];
  931. return 0;
  932. }
  933. static int ov2640_get_selection(struct v4l2_subdev *sd,
  934. struct v4l2_subdev_state *sd_state,
  935. struct v4l2_subdev_selection *sel)
  936. {
  937. if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  938. return -EINVAL;
  939. switch (sel->target) {
  940. case V4L2_SEL_TGT_CROP_BOUNDS:
  941. case V4L2_SEL_TGT_CROP:
  942. sel->r.left = 0;
  943. sel->r.top = 0;
  944. sel->r.width = UXGA_WIDTH;
  945. sel->r.height = UXGA_HEIGHT;
  946. return 0;
  947. default:
  948. return -EINVAL;
  949. }
  950. }
  951. static int ov2640_s_stream(struct v4l2_subdev *sd, int on)
  952. {
  953. struct i2c_client *client = v4l2_get_subdevdata(sd);
  954. struct ov2640_priv *priv = to_ov2640(client);
  955. int ret = 0;
  956. mutex_lock(&priv->lock);
  957. if (priv->streaming == !on) {
  958. if (on) {
  959. ret = ov2640_set_params(client, priv->win,
  960. priv->cfmt_code);
  961. if (!ret)
  962. ret = __v4l2_ctrl_handler_setup(&priv->hdl);
  963. }
  964. }
  965. if (!ret)
  966. priv->streaming = on;
  967. mutex_unlock(&priv->lock);
  968. return ret;
  969. }
  970. static int ov2640_video_probe(struct i2c_client *client)
  971. {
  972. struct ov2640_priv *priv = to_ov2640(client);
  973. u8 pid, ver, midh, midl;
  974. const char *devname;
  975. int ret;
  976. ret = ov2640_s_power(&priv->subdev, 1);
  977. if (ret < 0)
  978. return ret;
  979. /*
  980. * check and show product ID and manufacturer ID
  981. */
  982. i2c_smbus_write_byte_data(client, BANK_SEL, BANK_SEL_SENS);
  983. pid = i2c_smbus_read_byte_data(client, PID);
  984. ver = i2c_smbus_read_byte_data(client, VER);
  985. midh = i2c_smbus_read_byte_data(client, MIDH);
  986. midl = i2c_smbus_read_byte_data(client, MIDL);
  987. switch (VERSION(pid, ver)) {
  988. case PID_OV2640:
  989. devname = "ov2640";
  990. break;
  991. default:
  992. dev_err(&client->dev,
  993. "Product ID error %x:%x\n", pid, ver);
  994. ret = -ENODEV;
  995. goto done;
  996. }
  997. dev_info(&client->dev,
  998. "%s Product ID %0x:%0x Manufacturer ID %x:%x\n",
  999. devname, pid, ver, midh, midl);
  1000. done:
  1001. ov2640_s_power(&priv->subdev, 0);
  1002. return ret;
  1003. }
  1004. static const struct v4l2_ctrl_ops ov2640_ctrl_ops = {
  1005. .s_ctrl = ov2640_s_ctrl,
  1006. };
  1007. static const struct v4l2_subdev_core_ops ov2640_subdev_core_ops = {
  1008. .log_status = v4l2_ctrl_subdev_log_status,
  1009. .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
  1010. .unsubscribe_event = v4l2_event_subdev_unsubscribe,
  1011. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1012. .g_register = ov2640_g_register,
  1013. .s_register = ov2640_s_register,
  1014. #endif
  1015. .s_power = ov2640_s_power,
  1016. };
  1017. static const struct v4l2_subdev_pad_ops ov2640_subdev_pad_ops = {
  1018. .enum_mbus_code = ov2640_enum_mbus_code,
  1019. .get_selection = ov2640_get_selection,
  1020. .get_fmt = ov2640_get_fmt,
  1021. .set_fmt = ov2640_set_fmt,
  1022. };
  1023. static const struct v4l2_subdev_video_ops ov2640_subdev_video_ops = {
  1024. .s_stream = ov2640_s_stream,
  1025. };
  1026. static const struct v4l2_subdev_ops ov2640_subdev_ops = {
  1027. .core = &ov2640_subdev_core_ops,
  1028. .pad = &ov2640_subdev_pad_ops,
  1029. .video = &ov2640_subdev_video_ops,
  1030. };
  1031. static const struct v4l2_subdev_internal_ops ov2640_internal_ops = {
  1032. .init_state = ov2640_init_state,
  1033. };
  1034. static int ov2640_probe_dt(struct i2c_client *client,
  1035. struct ov2640_priv *priv)
  1036. {
  1037. int ret;
  1038. /* Request the reset GPIO deasserted */
  1039. priv->resetb_gpio = devm_gpiod_get_optional(&client->dev, "resetb",
  1040. GPIOD_OUT_LOW);
  1041. if (!priv->resetb_gpio)
  1042. dev_dbg(&client->dev, "resetb gpio is not assigned!\n");
  1043. ret = PTR_ERR_OR_ZERO(priv->resetb_gpio);
  1044. if (ret && ret != -ENOSYS) {
  1045. dev_dbg(&client->dev,
  1046. "Error %d while getting resetb gpio\n", ret);
  1047. return ret;
  1048. }
  1049. /* Request the power down GPIO asserted */
  1050. priv->pwdn_gpio = devm_gpiod_get_optional(&client->dev, "pwdn",
  1051. GPIOD_OUT_HIGH);
  1052. if (!priv->pwdn_gpio)
  1053. dev_dbg(&client->dev, "pwdn gpio is not assigned!\n");
  1054. ret = PTR_ERR_OR_ZERO(priv->pwdn_gpio);
  1055. if (ret && ret != -ENOSYS) {
  1056. dev_dbg(&client->dev,
  1057. "Error %d while getting pwdn gpio\n", ret);
  1058. return ret;
  1059. }
  1060. return 0;
  1061. }
  1062. /*
  1063. * i2c_driver functions
  1064. */
  1065. static int ov2640_probe(struct i2c_client *client)
  1066. {
  1067. struct ov2640_priv *priv;
  1068. struct i2c_adapter *adapter = client->adapter;
  1069. int ret;
  1070. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  1071. dev_err(&adapter->dev,
  1072. "OV2640: I2C-Adapter doesn't support SMBUS\n");
  1073. return -EIO;
  1074. }
  1075. priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
  1076. if (!priv)
  1077. return -ENOMEM;
  1078. if (client->dev.of_node) {
  1079. priv->clk = devm_clk_get_enabled(&client->dev, "xvclk");
  1080. if (IS_ERR(priv->clk))
  1081. return PTR_ERR(priv->clk);
  1082. }
  1083. ret = ov2640_probe_dt(client, priv);
  1084. if (ret)
  1085. return ret;
  1086. priv->win = ov2640_select_win(SVGA_WIDTH, SVGA_HEIGHT);
  1087. priv->cfmt_code = MEDIA_BUS_FMT_UYVY8_2X8;
  1088. v4l2_i2c_subdev_init(&priv->subdev, client, &ov2640_subdev_ops);
  1089. priv->subdev.internal_ops = &ov2640_internal_ops;
  1090. priv->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
  1091. V4L2_SUBDEV_FL_HAS_EVENTS;
  1092. mutex_init(&priv->lock);
  1093. v4l2_ctrl_handler_init(&priv->hdl, 3);
  1094. priv->hdl.lock = &priv->lock;
  1095. v4l2_ctrl_new_std(&priv->hdl, &ov2640_ctrl_ops,
  1096. V4L2_CID_VFLIP, 0, 1, 1, 0);
  1097. v4l2_ctrl_new_std(&priv->hdl, &ov2640_ctrl_ops,
  1098. V4L2_CID_HFLIP, 0, 1, 1, 0);
  1099. v4l2_ctrl_new_std_menu_items(&priv->hdl, &ov2640_ctrl_ops,
  1100. V4L2_CID_TEST_PATTERN,
  1101. ARRAY_SIZE(ov2640_test_pattern_menu) - 1, 0, 0,
  1102. ov2640_test_pattern_menu);
  1103. priv->subdev.ctrl_handler = &priv->hdl;
  1104. if (priv->hdl.error) {
  1105. ret = priv->hdl.error;
  1106. goto err_hdl;
  1107. }
  1108. priv->pad.flags = MEDIA_PAD_FL_SOURCE;
  1109. priv->subdev.entity.function = MEDIA_ENT_F_CAM_SENSOR;
  1110. ret = media_entity_pads_init(&priv->subdev.entity, 1, &priv->pad);
  1111. if (ret < 0)
  1112. goto err_hdl;
  1113. ret = ov2640_video_probe(client);
  1114. if (ret < 0)
  1115. goto err_videoprobe;
  1116. ret = v4l2_async_register_subdev(&priv->subdev);
  1117. if (ret < 0)
  1118. goto err_videoprobe;
  1119. dev_info(&adapter->dev, "OV2640 Probed\n");
  1120. return 0;
  1121. err_videoprobe:
  1122. media_entity_cleanup(&priv->subdev.entity);
  1123. err_hdl:
  1124. v4l2_ctrl_handler_free(&priv->hdl);
  1125. mutex_destroy(&priv->lock);
  1126. return ret;
  1127. }
  1128. static void ov2640_remove(struct i2c_client *client)
  1129. {
  1130. struct ov2640_priv *priv = to_ov2640(client);
  1131. v4l2_async_unregister_subdev(&priv->subdev);
  1132. v4l2_ctrl_handler_free(&priv->hdl);
  1133. mutex_destroy(&priv->lock);
  1134. media_entity_cleanup(&priv->subdev.entity);
  1135. v4l2_device_unregister_subdev(&priv->subdev);
  1136. }
  1137. static const struct i2c_device_id ov2640_id[] = {
  1138. { "ov2640" },
  1139. { }
  1140. };
  1141. MODULE_DEVICE_TABLE(i2c, ov2640_id);
  1142. static const struct of_device_id ov2640_of_match[] = {
  1143. {.compatible = "ovti,ov2640", },
  1144. {},
  1145. };
  1146. MODULE_DEVICE_TABLE(of, ov2640_of_match);
  1147. static struct i2c_driver ov2640_i2c_driver = {
  1148. .driver = {
  1149. .name = "ov2640",
  1150. .of_match_table = ov2640_of_match,
  1151. },
  1152. .probe = ov2640_probe,
  1153. .remove = ov2640_remove,
  1154. .id_table = ov2640_id,
  1155. };
  1156. module_i2c_driver(ov2640_i2c_driver);
  1157. MODULE_DESCRIPTION("Driver for Omni Vision 2640 sensor");
  1158. MODULE_AUTHOR("Alberto Panizzo");
  1159. MODULE_LICENSE("GPL v2");