ov2640.c 36 KB

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