ov2640.c 34 KB

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