ov2640.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201
  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. };
  301. /*
  302. * Registers settings
  303. */
  304. #define ENDMARKER { 0xff, 0xff }
  305. static const struct regval_list ov2640_init_regs[] = {
  306. { BANK_SEL, BANK_SEL_DSP },
  307. { 0x2c, 0xff },
  308. { 0x2e, 0xdf },
  309. { BANK_SEL, BANK_SEL_SENS },
  310. { 0x3c, 0x32 },
  311. { CLKRC, CLKRC_DIV_SET(1) },
  312. { COM2, COM2_OCAP_Nx_SET(3) },
  313. { REG04, REG04_DEF | REG04_HREF_EN },
  314. { COM8, COM8_DEF | COM8_BNDF_EN | COM8_AGC_EN | COM8_AEC_EN },
  315. { COM9, COM9_AGC_GAIN_8x | 0x08},
  316. { 0x2c, 0x0c },
  317. { 0x33, 0x78 },
  318. { 0x3a, 0x33 },
  319. { 0x3b, 0xfb },
  320. { 0x3e, 0x00 },
  321. { 0x43, 0x11 },
  322. { 0x16, 0x10 },
  323. { 0x39, 0x02 },
  324. { 0x35, 0x88 },
  325. { 0x22, 0x0a },
  326. { 0x37, 0x40 },
  327. { 0x23, 0x00 },
  328. { ARCOM2, 0xa0 },
  329. { 0x06, 0x02 },
  330. { 0x06, 0x88 },
  331. { 0x07, 0xc0 },
  332. { 0x0d, 0xb7 },
  333. { 0x0e, 0x01 },
  334. { 0x4c, 0x00 },
  335. { 0x4a, 0x81 },
  336. { 0x21, 0x99 },
  337. { AEW, 0x40 },
  338. { AEB, 0x38 },
  339. { VV, VV_HIGH_TH_SET(0x08) | VV_LOW_TH_SET(0x02) },
  340. { 0x5c, 0x00 },
  341. { 0x63, 0x00 },
  342. { FLL, 0x22 },
  343. { COM3, 0x38 | COM3_BAND_AUTO },
  344. { REG5D, 0x55 },
  345. { REG5E, 0x7d },
  346. { REG5F, 0x7d },
  347. { REG60, 0x55 },
  348. { HISTO_LOW, 0x70 },
  349. { HISTO_HIGH, 0x80 },
  350. { 0x7c, 0x05 },
  351. { 0x20, 0x80 },
  352. { 0x28, 0x30 },
  353. { 0x6c, 0x00 },
  354. { 0x6d, 0x80 },
  355. { 0x6e, 0x00 },
  356. { 0x70, 0x02 },
  357. { 0x71, 0x94 },
  358. { 0x73, 0xc1 },
  359. { 0x3d, 0x34 },
  360. { COM7, COM7_RES_UXGA | COM7_ZOOM_EN },
  361. { REG5A, BD50_MAX_AEC_STEP_SET(6)
  362. | BD60_MAX_AEC_STEP_SET(8) }, /* 0x57 */
  363. { COM25, COM25_50HZ_BANDING_AEC_MSBS_SET(0x0bb)
  364. | COM25_60HZ_BANDING_AEC_MSBS_SET(0x09c) }, /* 0x00 */
  365. { BD50, BD50_50HZ_BANDING_AEC_LSBS_SET(0x0bb) }, /* 0xbb */
  366. { BD60, BD60_60HZ_BANDING_AEC_LSBS_SET(0x09c) }, /* 0x9c */
  367. { BANK_SEL, BANK_SEL_DSP },
  368. { 0xe5, 0x7f },
  369. { MC_BIST, MC_BIST_RESET | MC_BIST_BOOT_ROM_SEL },
  370. { 0x41, 0x24 },
  371. { RESET, RESET_JPEG | RESET_DVP },
  372. { 0x76, 0xff },
  373. { 0x33, 0xa0 },
  374. { 0x42, 0x20 },
  375. { 0x43, 0x18 },
  376. { 0x4c, 0x00 },
  377. { CTRL3, CTRL3_BPC_EN | CTRL3_WPC_EN | 0x10 },
  378. { 0x88, 0x3f },
  379. { 0xd7, 0x03 },
  380. { 0xd9, 0x10 },
  381. { R_DVP_SP, R_DVP_SP_AUTO_MODE | 0x2 },
  382. { 0xc8, 0x08 },
  383. { 0xc9, 0x80 },
  384. { BPADDR, 0x00 },
  385. { BPDATA, 0x00 },
  386. { BPADDR, 0x03 },
  387. { BPDATA, 0x48 },
  388. { BPDATA, 0x48 },
  389. { BPADDR, 0x08 },
  390. { BPDATA, 0x20 },
  391. { BPDATA, 0x10 },
  392. { BPDATA, 0x0e },
  393. { 0x90, 0x00 },
  394. { 0x91, 0x0e },
  395. { 0x91, 0x1a },
  396. { 0x91, 0x31 },
  397. { 0x91, 0x5a },
  398. { 0x91, 0x69 },
  399. { 0x91, 0x75 },
  400. { 0x91, 0x7e },
  401. { 0x91, 0x88 },
  402. { 0x91, 0x8f },
  403. { 0x91, 0x96 },
  404. { 0x91, 0xa3 },
  405. { 0x91, 0xaf },
  406. { 0x91, 0xc4 },
  407. { 0x91, 0xd7 },
  408. { 0x91, 0xe8 },
  409. { 0x91, 0x20 },
  410. { 0x92, 0x00 },
  411. { 0x93, 0x06 },
  412. { 0x93, 0xe3 },
  413. { 0x93, 0x03 },
  414. { 0x93, 0x03 },
  415. { 0x93, 0x00 },
  416. { 0x93, 0x02 },
  417. { 0x93, 0x00 },
  418. { 0x93, 0x00 },
  419. { 0x93, 0x00 },
  420. { 0x93, 0x00 },
  421. { 0x93, 0x00 },
  422. { 0x93, 0x00 },
  423. { 0x93, 0x00 },
  424. { 0x96, 0x00 },
  425. { 0x97, 0x08 },
  426. { 0x97, 0x19 },
  427. { 0x97, 0x02 },
  428. { 0x97, 0x0c },
  429. { 0x97, 0x24 },
  430. { 0x97, 0x30 },
  431. { 0x97, 0x28 },
  432. { 0x97, 0x26 },
  433. { 0x97, 0x02 },
  434. { 0x97, 0x98 },
  435. { 0x97, 0x80 },
  436. { 0x97, 0x00 },
  437. { 0x97, 0x00 },
  438. { 0xa4, 0x00 },
  439. { 0xa8, 0x00 },
  440. { 0xc5, 0x11 },
  441. { 0xc6, 0x51 },
  442. { 0xbf, 0x80 },
  443. { 0xc7, 0x10 }, /* simple AWB */
  444. { 0xb6, 0x66 },
  445. { 0xb8, 0xA5 },
  446. { 0xb7, 0x64 },
  447. { 0xb9, 0x7C },
  448. { 0xb3, 0xaf },
  449. { 0xb4, 0x97 },
  450. { 0xb5, 0xFF },
  451. { 0xb0, 0xC5 },
  452. { 0xb1, 0x94 },
  453. { 0xb2, 0x0f },
  454. { 0xc4, 0x5c },
  455. { 0xa6, 0x00 },
  456. { 0xa7, 0x20 },
  457. { 0xa7, 0xd8 },
  458. { 0xa7, 0x1b },
  459. { 0xa7, 0x31 },
  460. { 0xa7, 0x00 },
  461. { 0xa7, 0x18 },
  462. { 0xa7, 0x20 },
  463. { 0xa7, 0xd8 },
  464. { 0xa7, 0x19 },
  465. { 0xa7, 0x31 },
  466. { 0xa7, 0x00 },
  467. { 0xa7, 0x18 },
  468. { 0xa7, 0x20 },
  469. { 0xa7, 0xd8 },
  470. { 0xa7, 0x19 },
  471. { 0xa7, 0x31 },
  472. { 0xa7, 0x00 },
  473. { 0xa7, 0x18 },
  474. { 0x7f, 0x00 },
  475. { 0xe5, 0x1f },
  476. { 0xe1, 0x77 },
  477. { 0xdd, 0x7f },
  478. { CTRL0, CTRL0_YUV422 | CTRL0_YUV_EN | CTRL0_RGB_EN },
  479. ENDMARKER,
  480. };
  481. /*
  482. * Register settings for window size
  483. * The preamble, setup the internal DSP to input an UXGA (1600x1200) image.
  484. * Then the different zooming configurations will setup the output image size.
  485. */
  486. static const struct regval_list ov2640_size_change_preamble_regs[] = {
  487. { BANK_SEL, BANK_SEL_DSP },
  488. { RESET, RESET_DVP },
  489. { SIZEL, SIZEL_HSIZE8_11_SET(UXGA_WIDTH) |
  490. SIZEL_HSIZE8_SET(UXGA_WIDTH) |
  491. SIZEL_VSIZE8_SET(UXGA_HEIGHT) },
  492. { HSIZE8, HSIZE8_SET(UXGA_WIDTH) },
  493. { VSIZE8, VSIZE8_SET(UXGA_HEIGHT) },
  494. { CTRL2, CTRL2_DCW_EN | CTRL2_SDE_EN |
  495. CTRL2_UV_AVG_EN | CTRL2_CMX_EN | CTRL2_UV_ADJ_EN },
  496. { HSIZE, HSIZE_SET(UXGA_WIDTH) },
  497. { VSIZE, VSIZE_SET(UXGA_HEIGHT) },
  498. { XOFFL, XOFFL_SET(0) },
  499. { YOFFL, YOFFL_SET(0) },
  500. { VHYX, VHYX_HSIZE_SET(UXGA_WIDTH) | VHYX_VSIZE_SET(UXGA_HEIGHT) |
  501. VHYX_XOFF_SET(0) | VHYX_YOFF_SET(0)},
  502. { TEST, TEST_HSIZE_SET(UXGA_WIDTH) },
  503. ENDMARKER,
  504. };
  505. #define PER_SIZE_REG_SEQ(x, y, v_div, h_div, pclk_div) \
  506. { CTRLI, CTRLI_LP_DP | CTRLI_V_DIV_SET(v_div) | \
  507. CTRLI_H_DIV_SET(h_div)}, \
  508. { ZMOW, ZMOW_OUTW_SET(x) }, \
  509. { ZMOH, ZMOH_OUTH_SET(y) }, \
  510. { ZMHH, ZMHH_OUTW_SET(x) | ZMHH_OUTH_SET(y) }, \
  511. { R_DVP_SP, pclk_div }, \
  512. { RESET, 0x00}
  513. static const struct regval_list ov2640_qcif_regs[] = {
  514. PER_SIZE_REG_SEQ(QCIF_WIDTH, QCIF_HEIGHT, 3, 3, 4),
  515. ENDMARKER,
  516. };
  517. static const struct regval_list ov2640_qvga_regs[] = {
  518. PER_SIZE_REG_SEQ(QVGA_WIDTH, QVGA_HEIGHT, 2, 2, 4),
  519. ENDMARKER,
  520. };
  521. static const struct regval_list ov2640_cif_regs[] = {
  522. PER_SIZE_REG_SEQ(CIF_WIDTH, CIF_HEIGHT, 2, 2, 8),
  523. ENDMARKER,
  524. };
  525. static const struct regval_list ov2640_vga_regs[] = {
  526. PER_SIZE_REG_SEQ(VGA_WIDTH, VGA_HEIGHT, 0, 0, 2),
  527. ENDMARKER,
  528. };
  529. static const struct regval_list ov2640_svga_regs[] = {
  530. PER_SIZE_REG_SEQ(SVGA_WIDTH, SVGA_HEIGHT, 1, 1, 2),
  531. ENDMARKER,
  532. };
  533. static const struct regval_list ov2640_xga_regs[] = {
  534. PER_SIZE_REG_SEQ(XGA_WIDTH, XGA_HEIGHT, 0, 0, 2),
  535. { CTRLI, 0x00},
  536. ENDMARKER,
  537. };
  538. static const struct regval_list ov2640_sxga_regs[] = {
  539. PER_SIZE_REG_SEQ(SXGA_WIDTH, SXGA_HEIGHT, 0, 0, 2),
  540. { CTRLI, 0x00},
  541. { R_DVP_SP, 2 | R_DVP_SP_AUTO_MODE },
  542. ENDMARKER,
  543. };
  544. static const struct regval_list ov2640_uxga_regs[] = {
  545. PER_SIZE_REG_SEQ(UXGA_WIDTH, UXGA_HEIGHT, 0, 0, 0),
  546. { CTRLI, 0x00},
  547. { R_DVP_SP, 0 | R_DVP_SP_AUTO_MODE },
  548. ENDMARKER,
  549. };
  550. #define OV2640_SIZE(n, w, h, r) \
  551. {.name = n, .width = w , .height = h, .regs = r }
  552. static const struct ov2640_win_size ov2640_supported_win_sizes[] = {
  553. OV2640_SIZE("QCIF", QCIF_WIDTH, QCIF_HEIGHT, ov2640_qcif_regs),
  554. OV2640_SIZE("QVGA", QVGA_WIDTH, QVGA_HEIGHT, ov2640_qvga_regs),
  555. OV2640_SIZE("CIF", CIF_WIDTH, CIF_HEIGHT, ov2640_cif_regs),
  556. OV2640_SIZE("VGA", VGA_WIDTH, VGA_HEIGHT, ov2640_vga_regs),
  557. OV2640_SIZE("SVGA", SVGA_WIDTH, SVGA_HEIGHT, ov2640_svga_regs),
  558. OV2640_SIZE("XGA", XGA_WIDTH, XGA_HEIGHT, ov2640_xga_regs),
  559. OV2640_SIZE("SXGA", SXGA_WIDTH, SXGA_HEIGHT, ov2640_sxga_regs),
  560. OV2640_SIZE("UXGA", UXGA_WIDTH, UXGA_HEIGHT, ov2640_uxga_regs),
  561. };
  562. /*
  563. * Register settings for pixel formats
  564. */
  565. static const struct regval_list ov2640_format_change_preamble_regs[] = {
  566. { BANK_SEL, BANK_SEL_DSP },
  567. { R_BYPASS, R_BYPASS_USE_DSP },
  568. ENDMARKER,
  569. };
  570. static const struct regval_list ov2640_yuyv_regs[] = {
  571. { IMAGE_MODE, IMAGE_MODE_YUV422 },
  572. { 0xd7, 0x03 },
  573. { 0x33, 0xa0 },
  574. { 0xe5, 0x1f },
  575. { 0xe1, 0x67 },
  576. { RESET, 0x00 },
  577. { R_BYPASS, R_BYPASS_USE_DSP },
  578. ENDMARKER,
  579. };
  580. static const struct regval_list ov2640_uyvy_regs[] = {
  581. { IMAGE_MODE, IMAGE_MODE_LBYTE_FIRST | IMAGE_MODE_YUV422 },
  582. { 0xd7, 0x01 },
  583. { 0x33, 0xa0 },
  584. { 0xe1, 0x67 },
  585. { RESET, 0x00 },
  586. { R_BYPASS, R_BYPASS_USE_DSP },
  587. ENDMARKER,
  588. };
  589. static const struct regval_list ov2640_rgb565_be_regs[] = {
  590. { IMAGE_MODE, IMAGE_MODE_RGB565 },
  591. { 0xd7, 0x03 },
  592. { RESET, 0x00 },
  593. { R_BYPASS, R_BYPASS_USE_DSP },
  594. ENDMARKER,
  595. };
  596. static const struct regval_list ov2640_rgb565_le_regs[] = {
  597. { IMAGE_MODE, IMAGE_MODE_LBYTE_FIRST | IMAGE_MODE_RGB565 },
  598. { 0xd7, 0x03 },
  599. { RESET, 0x00 },
  600. { R_BYPASS, R_BYPASS_USE_DSP },
  601. ENDMARKER,
  602. };
  603. static u32 ov2640_codes[] = {
  604. MEDIA_BUS_FMT_YUYV8_2X8,
  605. MEDIA_BUS_FMT_UYVY8_2X8,
  606. MEDIA_BUS_FMT_YVYU8_2X8,
  607. MEDIA_BUS_FMT_VYUY8_2X8,
  608. MEDIA_BUS_FMT_RGB565_2X8_BE,
  609. MEDIA_BUS_FMT_RGB565_2X8_LE,
  610. };
  611. /*
  612. * General functions
  613. */
  614. static struct ov2640_priv *to_ov2640(const struct i2c_client *client)
  615. {
  616. return container_of(i2c_get_clientdata(client), struct ov2640_priv,
  617. subdev);
  618. }
  619. static int ov2640_write_array(struct i2c_client *client,
  620. const struct regval_list *vals)
  621. {
  622. int ret;
  623. while ((vals->reg_num != 0xff) || (vals->value != 0xff)) {
  624. ret = i2c_smbus_write_byte_data(client,
  625. vals->reg_num, vals->value);
  626. dev_vdbg(&client->dev, "array: 0x%02x, 0x%02x",
  627. vals->reg_num, vals->value);
  628. if (ret < 0)
  629. return ret;
  630. vals++;
  631. }
  632. return 0;
  633. }
  634. static int ov2640_mask_set(struct i2c_client *client,
  635. u8 reg, u8 mask, u8 set)
  636. {
  637. s32 val = i2c_smbus_read_byte_data(client, reg);
  638. if (val < 0)
  639. return val;
  640. val &= ~mask;
  641. val |= set & mask;
  642. dev_vdbg(&client->dev, "masks: 0x%02x, 0x%02x", reg, val);
  643. return i2c_smbus_write_byte_data(client, reg, val);
  644. }
  645. static int ov2640_reset(struct i2c_client *client)
  646. {
  647. int ret;
  648. const struct regval_list reset_seq[] = {
  649. {BANK_SEL, BANK_SEL_SENS},
  650. {COM7, COM7_SRST},
  651. ENDMARKER,
  652. };
  653. ret = ov2640_write_array(client, reset_seq);
  654. if (ret)
  655. goto err;
  656. msleep(5);
  657. err:
  658. dev_dbg(&client->dev, "%s: (ret %d)", __func__, ret);
  659. return ret;
  660. }
  661. /*
  662. * functions
  663. */
  664. static int ov2640_s_ctrl(struct v4l2_ctrl *ctrl)
  665. {
  666. struct v4l2_subdev *sd =
  667. &container_of(ctrl->handler, struct ov2640_priv, hdl)->subdev;
  668. struct i2c_client *client = v4l2_get_subdevdata(sd);
  669. u8 val;
  670. int ret;
  671. ret = i2c_smbus_write_byte_data(client, BANK_SEL, BANK_SEL_SENS);
  672. if (ret < 0)
  673. return ret;
  674. switch (ctrl->id) {
  675. case V4L2_CID_VFLIP:
  676. val = ctrl->val ? REG04_VFLIP_IMG | REG04_VREF_EN : 0x00;
  677. return ov2640_mask_set(client, REG04,
  678. REG04_VFLIP_IMG | REG04_VREF_EN, val);
  679. /* NOTE: REG04_VREF_EN: 1 line shift / even/odd line swap */
  680. case V4L2_CID_HFLIP:
  681. val = ctrl->val ? REG04_HFLIP_IMG : 0x00;
  682. return ov2640_mask_set(client, REG04, REG04_HFLIP_IMG, val);
  683. }
  684. return -EINVAL;
  685. }
  686. #ifdef CONFIG_VIDEO_ADV_DEBUG
  687. static int ov2640_g_register(struct v4l2_subdev *sd,
  688. struct v4l2_dbg_register *reg)
  689. {
  690. struct i2c_client *client = v4l2_get_subdevdata(sd);
  691. int ret;
  692. reg->size = 1;
  693. if (reg->reg > 0xff)
  694. return -EINVAL;
  695. ret = i2c_smbus_read_byte_data(client, reg->reg);
  696. if (ret < 0)
  697. return ret;
  698. reg->val = ret;
  699. return 0;
  700. }
  701. static int ov2640_s_register(struct v4l2_subdev *sd,
  702. const struct v4l2_dbg_register *reg)
  703. {
  704. struct i2c_client *client = v4l2_get_subdevdata(sd);
  705. if (reg->reg > 0xff ||
  706. reg->val > 0xff)
  707. return -EINVAL;
  708. return i2c_smbus_write_byte_data(client, reg->reg, reg->val);
  709. }
  710. #endif
  711. static int ov2640_s_power(struct v4l2_subdev *sd, int on)
  712. {
  713. #ifdef CONFIG_GPIOLIB
  714. struct i2c_client *client = v4l2_get_subdevdata(sd);
  715. struct ov2640_priv *priv = to_ov2640(client);
  716. if (priv->pwdn_gpio)
  717. gpiod_direction_output(priv->pwdn_gpio, !on);
  718. if (on && priv->resetb_gpio) {
  719. /* Active the resetb pin to perform a reset pulse */
  720. gpiod_direction_output(priv->resetb_gpio, 1);
  721. usleep_range(3000, 5000);
  722. gpiod_set_value(priv->resetb_gpio, 0);
  723. }
  724. #endif
  725. return 0;
  726. }
  727. /* Select the nearest higher resolution for capture */
  728. static const struct ov2640_win_size *ov2640_select_win(u32 width, u32 height)
  729. {
  730. int i, default_size = ARRAY_SIZE(ov2640_supported_win_sizes) - 1;
  731. for (i = 0; i < ARRAY_SIZE(ov2640_supported_win_sizes); i++) {
  732. if (ov2640_supported_win_sizes[i].width >= width &&
  733. ov2640_supported_win_sizes[i].height >= height)
  734. return &ov2640_supported_win_sizes[i];
  735. }
  736. return &ov2640_supported_win_sizes[default_size];
  737. }
  738. static int ov2640_set_params(struct i2c_client *client,
  739. const struct ov2640_win_size *win, u32 code)
  740. {
  741. struct ov2640_priv *priv = to_ov2640(client);
  742. const struct regval_list *selected_cfmt_regs;
  743. u8 val;
  744. int ret;
  745. /* select win */
  746. priv->win = win;
  747. /* select format */
  748. priv->cfmt_code = 0;
  749. switch (code) {
  750. case MEDIA_BUS_FMT_RGB565_2X8_BE:
  751. dev_dbg(&client->dev, "%s: Selected cfmt RGB565 BE", __func__);
  752. selected_cfmt_regs = ov2640_rgb565_be_regs;
  753. break;
  754. case MEDIA_BUS_FMT_RGB565_2X8_LE:
  755. dev_dbg(&client->dev, "%s: Selected cfmt RGB565 LE", __func__);
  756. selected_cfmt_regs = ov2640_rgb565_le_regs;
  757. break;
  758. case MEDIA_BUS_FMT_YUYV8_2X8:
  759. dev_dbg(&client->dev, "%s: Selected cfmt YUYV (YUV422)", __func__);
  760. selected_cfmt_regs = ov2640_yuyv_regs;
  761. break;
  762. case MEDIA_BUS_FMT_UYVY8_2X8:
  763. default:
  764. dev_dbg(&client->dev, "%s: Selected cfmt UYVY", __func__);
  765. selected_cfmt_regs = ov2640_uyvy_regs;
  766. break;
  767. case MEDIA_BUS_FMT_YVYU8_2X8:
  768. dev_dbg(&client->dev, "%s: Selected cfmt YVYU", __func__);
  769. selected_cfmt_regs = ov2640_yuyv_regs;
  770. break;
  771. case MEDIA_BUS_FMT_VYUY8_2X8:
  772. dev_dbg(&client->dev, "%s: Selected cfmt VYUY", __func__);
  773. selected_cfmt_regs = ov2640_uyvy_regs;
  774. break;
  775. }
  776. /* reset hardware */
  777. ov2640_reset(client);
  778. /* initialize the sensor with default data */
  779. dev_dbg(&client->dev, "%s: Init default", __func__);
  780. ret = ov2640_write_array(client, ov2640_init_regs);
  781. if (ret < 0)
  782. goto err;
  783. /* select preamble */
  784. dev_dbg(&client->dev, "%s: Set size to %s", __func__, priv->win->name);
  785. ret = ov2640_write_array(client, ov2640_size_change_preamble_regs);
  786. if (ret < 0)
  787. goto err;
  788. /* set size win */
  789. ret = ov2640_write_array(client, priv->win->regs);
  790. if (ret < 0)
  791. goto err;
  792. /* cfmt preamble */
  793. dev_dbg(&client->dev, "%s: Set cfmt", __func__);
  794. ret = ov2640_write_array(client, ov2640_format_change_preamble_regs);
  795. if (ret < 0)
  796. goto err;
  797. /* set cfmt */
  798. ret = ov2640_write_array(client, selected_cfmt_regs);
  799. if (ret < 0)
  800. goto err;
  801. val = (code == MEDIA_BUS_FMT_YVYU8_2X8)
  802. || (code == MEDIA_BUS_FMT_VYUY8_2X8) ? CTRL0_VFIRST : 0x00;
  803. ret = ov2640_mask_set(client, CTRL0, CTRL0_VFIRST, val);
  804. if (ret < 0)
  805. goto err;
  806. priv->cfmt_code = code;
  807. return 0;
  808. err:
  809. dev_err(&client->dev, "%s: Error %d", __func__, ret);
  810. ov2640_reset(client);
  811. priv->win = NULL;
  812. return ret;
  813. }
  814. static int ov2640_get_fmt(struct v4l2_subdev *sd,
  815. struct v4l2_subdev_pad_config *cfg,
  816. struct v4l2_subdev_format *format)
  817. {
  818. struct v4l2_mbus_framefmt *mf = &format->format;
  819. struct i2c_client *client = v4l2_get_subdevdata(sd);
  820. struct ov2640_priv *priv = to_ov2640(client);
  821. if (format->pad)
  822. return -EINVAL;
  823. if (!priv->win) {
  824. priv->win = ov2640_select_win(SVGA_WIDTH, SVGA_HEIGHT);
  825. priv->cfmt_code = MEDIA_BUS_FMT_UYVY8_2X8;
  826. }
  827. mf->width = priv->win->width;
  828. mf->height = priv->win->height;
  829. mf->code = priv->cfmt_code;
  830. mf->colorspace = V4L2_COLORSPACE_SRGB;
  831. mf->field = V4L2_FIELD_NONE;
  832. return 0;
  833. }
  834. static int ov2640_set_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. const struct ov2640_win_size *win;
  841. if (format->pad)
  842. return -EINVAL;
  843. /* select suitable win */
  844. win = ov2640_select_win(mf->width, mf->height);
  845. mf->width = win->width;
  846. mf->height = win->height;
  847. mf->field = V4L2_FIELD_NONE;
  848. mf->colorspace = V4L2_COLORSPACE_SRGB;
  849. switch (mf->code) {
  850. case MEDIA_BUS_FMT_RGB565_2X8_BE:
  851. case MEDIA_BUS_FMT_RGB565_2X8_LE:
  852. case MEDIA_BUS_FMT_YUYV8_2X8:
  853. case MEDIA_BUS_FMT_UYVY8_2X8:
  854. case MEDIA_BUS_FMT_YVYU8_2X8:
  855. case MEDIA_BUS_FMT_VYUY8_2X8:
  856. break;
  857. default:
  858. mf->code = MEDIA_BUS_FMT_UYVY8_2X8;
  859. break;
  860. }
  861. if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE)
  862. return ov2640_set_params(client, win, mf->code);
  863. cfg->try_fmt = *mf;
  864. return 0;
  865. }
  866. static int ov2640_enum_mbus_code(struct v4l2_subdev *sd,
  867. struct v4l2_subdev_pad_config *cfg,
  868. struct v4l2_subdev_mbus_code_enum *code)
  869. {
  870. if (code->pad || code->index >= ARRAY_SIZE(ov2640_codes))
  871. return -EINVAL;
  872. code->code = ov2640_codes[code->index];
  873. return 0;
  874. }
  875. static int ov2640_get_selection(struct v4l2_subdev *sd,
  876. struct v4l2_subdev_pad_config *cfg,
  877. struct v4l2_subdev_selection *sel)
  878. {
  879. if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  880. return -EINVAL;
  881. switch (sel->target) {
  882. case V4L2_SEL_TGT_CROP_BOUNDS:
  883. case V4L2_SEL_TGT_CROP_DEFAULT:
  884. case V4L2_SEL_TGT_CROP:
  885. sel->r.left = 0;
  886. sel->r.top = 0;
  887. sel->r.width = UXGA_WIDTH;
  888. sel->r.height = UXGA_HEIGHT;
  889. return 0;
  890. default:
  891. return -EINVAL;
  892. }
  893. }
  894. static int ov2640_video_probe(struct i2c_client *client)
  895. {
  896. struct ov2640_priv *priv = to_ov2640(client);
  897. u8 pid, ver, midh, midl;
  898. const char *devname;
  899. int ret;
  900. ret = ov2640_s_power(&priv->subdev, 1);
  901. if (ret < 0)
  902. return ret;
  903. /*
  904. * check and show product ID and manufacturer ID
  905. */
  906. i2c_smbus_write_byte_data(client, BANK_SEL, BANK_SEL_SENS);
  907. pid = i2c_smbus_read_byte_data(client, PID);
  908. ver = i2c_smbus_read_byte_data(client, VER);
  909. midh = i2c_smbus_read_byte_data(client, MIDH);
  910. midl = i2c_smbus_read_byte_data(client, MIDL);
  911. switch (VERSION(pid, ver)) {
  912. case PID_OV2640:
  913. devname = "ov2640";
  914. break;
  915. default:
  916. dev_err(&client->dev,
  917. "Product ID error %x:%x\n", pid, ver);
  918. ret = -ENODEV;
  919. goto done;
  920. }
  921. dev_info(&client->dev,
  922. "%s Product ID %0x:%0x Manufacturer ID %x:%x\n",
  923. devname, pid, ver, midh, midl);
  924. ret = v4l2_ctrl_handler_setup(&priv->hdl);
  925. done:
  926. ov2640_s_power(&priv->subdev, 0);
  927. return ret;
  928. }
  929. static const struct v4l2_ctrl_ops ov2640_ctrl_ops = {
  930. .s_ctrl = ov2640_s_ctrl,
  931. };
  932. static const struct v4l2_subdev_core_ops ov2640_subdev_core_ops = {
  933. #ifdef CONFIG_VIDEO_ADV_DEBUG
  934. .g_register = ov2640_g_register,
  935. .s_register = ov2640_s_register,
  936. #endif
  937. .s_power = ov2640_s_power,
  938. };
  939. static const struct v4l2_subdev_pad_ops ov2640_subdev_pad_ops = {
  940. .enum_mbus_code = ov2640_enum_mbus_code,
  941. .get_selection = ov2640_get_selection,
  942. .get_fmt = ov2640_get_fmt,
  943. .set_fmt = ov2640_set_fmt,
  944. };
  945. static const struct v4l2_subdev_ops ov2640_subdev_ops = {
  946. .core = &ov2640_subdev_core_ops,
  947. .pad = &ov2640_subdev_pad_ops,
  948. };
  949. static int ov2640_probe_dt(struct i2c_client *client,
  950. struct ov2640_priv *priv)
  951. {
  952. int ret;
  953. /* Request the reset GPIO deasserted */
  954. priv->resetb_gpio = devm_gpiod_get_optional(&client->dev, "resetb",
  955. GPIOD_OUT_LOW);
  956. if (!priv->resetb_gpio)
  957. dev_dbg(&client->dev, "resetb gpio is not assigned!\n");
  958. ret = PTR_ERR_OR_ZERO(priv->resetb_gpio);
  959. if (ret && ret != -ENOSYS) {
  960. dev_dbg(&client->dev,
  961. "Error %d while getting resetb gpio\n", ret);
  962. return ret;
  963. }
  964. /* Request the power down GPIO asserted */
  965. priv->pwdn_gpio = devm_gpiod_get_optional(&client->dev, "pwdn",
  966. GPIOD_OUT_HIGH);
  967. if (!priv->pwdn_gpio)
  968. dev_dbg(&client->dev, "pwdn gpio is not assigned!\n");
  969. ret = PTR_ERR_OR_ZERO(priv->pwdn_gpio);
  970. if (ret && ret != -ENOSYS) {
  971. dev_dbg(&client->dev,
  972. "Error %d while getting pwdn gpio\n", ret);
  973. return ret;
  974. }
  975. return 0;
  976. }
  977. /*
  978. * i2c_driver functions
  979. */
  980. static int ov2640_probe(struct i2c_client *client,
  981. const struct i2c_device_id *did)
  982. {
  983. struct ov2640_priv *priv;
  984. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  985. int ret;
  986. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  987. dev_err(&adapter->dev,
  988. "OV2640: I2C-Adapter doesn't support SMBUS\n");
  989. return -EIO;
  990. }
  991. priv = devm_kzalloc(&client->dev, sizeof(struct ov2640_priv), GFP_KERNEL);
  992. if (!priv) {
  993. dev_err(&adapter->dev,
  994. "Failed to allocate memory for private data!\n");
  995. return -ENOMEM;
  996. }
  997. if (client->dev.of_node) {
  998. priv->clk = devm_clk_get(&client->dev, "xvclk");
  999. if (IS_ERR(priv->clk))
  1000. return -EPROBE_DEFER;
  1001. clk_prepare_enable(priv->clk);
  1002. }
  1003. ret = ov2640_probe_dt(client, priv);
  1004. if (ret)
  1005. goto err_clk;
  1006. v4l2_i2c_subdev_init(&priv->subdev, client, &ov2640_subdev_ops);
  1007. priv->subdev.flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
  1008. v4l2_ctrl_handler_init(&priv->hdl, 2);
  1009. v4l2_ctrl_new_std(&priv->hdl, &ov2640_ctrl_ops,
  1010. V4L2_CID_VFLIP, 0, 1, 1, 0);
  1011. v4l2_ctrl_new_std(&priv->hdl, &ov2640_ctrl_ops,
  1012. V4L2_CID_HFLIP, 0, 1, 1, 0);
  1013. priv->subdev.ctrl_handler = &priv->hdl;
  1014. if (priv->hdl.error) {
  1015. ret = priv->hdl.error;
  1016. goto err_hdl;
  1017. }
  1018. #if defined(CONFIG_MEDIA_CONTROLLER)
  1019. priv->pad.flags = MEDIA_PAD_FL_SOURCE;
  1020. priv->subdev.entity.function = MEDIA_ENT_F_CAM_SENSOR;
  1021. ret = media_entity_pads_init(&priv->subdev.entity, 1, &priv->pad);
  1022. if (ret < 0)
  1023. goto err_hdl;
  1024. #endif
  1025. ret = ov2640_video_probe(client);
  1026. if (ret < 0)
  1027. goto err_videoprobe;
  1028. ret = v4l2_async_register_subdev(&priv->subdev);
  1029. if (ret < 0)
  1030. goto err_videoprobe;
  1031. dev_info(&adapter->dev, "OV2640 Probed\n");
  1032. return 0;
  1033. err_videoprobe:
  1034. #if defined(CONFIG_MEDIA_CONTROLLER)
  1035. media_entity_cleanup(&priv->subdev.entity);
  1036. #endif
  1037. err_hdl:
  1038. v4l2_ctrl_handler_free(&priv->hdl);
  1039. err_clk:
  1040. clk_disable_unprepare(priv->clk);
  1041. return ret;
  1042. }
  1043. static int ov2640_remove(struct i2c_client *client)
  1044. {
  1045. struct ov2640_priv *priv = to_ov2640(client);
  1046. v4l2_async_unregister_subdev(&priv->subdev);
  1047. v4l2_ctrl_handler_free(&priv->hdl);
  1048. #if defined(CONFIG_MEDIA_CONTROLLER)
  1049. media_entity_cleanup(&priv->subdev.entity);
  1050. #endif
  1051. v4l2_device_unregister_subdev(&priv->subdev);
  1052. clk_disable_unprepare(priv->clk);
  1053. return 0;
  1054. }
  1055. static const struct i2c_device_id ov2640_id[] = {
  1056. { "ov2640", 0 },
  1057. { }
  1058. };
  1059. MODULE_DEVICE_TABLE(i2c, ov2640_id);
  1060. static const struct of_device_id ov2640_of_match[] = {
  1061. {.compatible = "ovti,ov2640", },
  1062. {},
  1063. };
  1064. MODULE_DEVICE_TABLE(of, ov2640_of_match);
  1065. static struct i2c_driver ov2640_i2c_driver = {
  1066. .driver = {
  1067. .name = "ov2640",
  1068. .of_match_table = of_match_ptr(ov2640_of_match),
  1069. },
  1070. .probe = ov2640_probe,
  1071. .remove = ov2640_remove,
  1072. .id_table = ov2640_id,
  1073. };
  1074. module_i2c_driver(ov2640_i2c_driver);
  1075. MODULE_DESCRIPTION("Driver for Omni Vision 2640 sensor");
  1076. MODULE_AUTHOR("Alberto Panizzo");
  1077. MODULE_LICENSE("GPL v2");