ov2640.c 38 KB

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