ov2659.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528
  1. /*
  2. * Omnivision OV2659 CMOS Image Sensor driver
  3. *
  4. * Copyright (C) 2015 Texas Instruments, Inc.
  5. *
  6. * Benoit Parrot <bparrot@ti.com>
  7. * Lad, Prabhakar <prabhakar.csengg@gmail.com>
  8. *
  9. * This program is free software; you may redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; version 2 of the License.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  17. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  18. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. #include <linux/clk.h>
  23. #include <linux/delay.h>
  24. #include <linux/err.h>
  25. #include <linux/init.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/io.h>
  28. #include <linux/i2c.h>
  29. #include <linux/kernel.h>
  30. #include <linux/media.h>
  31. #include <linux/module.h>
  32. #include <linux/of.h>
  33. #include <linux/of_graph.h>
  34. #include <linux/slab.h>
  35. #include <linux/uaccess.h>
  36. #include <linux/videodev2.h>
  37. #include <media/media-entity.h>
  38. #include <media/i2c/ov2659.h>
  39. #include <media/v4l2-common.h>
  40. #include <media/v4l2-ctrls.h>
  41. #include <media/v4l2-device.h>
  42. #include <media/v4l2-event.h>
  43. #include <media/v4l2-image-sizes.h>
  44. #include <media/v4l2-mediabus.h>
  45. #include <media/v4l2-of.h>
  46. #include <media/v4l2-subdev.h>
  47. #define DRIVER_NAME "ov2659"
  48. /*
  49. * OV2659 register definitions
  50. */
  51. #define REG_SOFTWARE_STANDBY 0x0100
  52. #define REG_SOFTWARE_RESET 0x0103
  53. #define REG_IO_CTRL00 0x3000
  54. #define REG_IO_CTRL01 0x3001
  55. #define REG_IO_CTRL02 0x3002
  56. #define REG_OUTPUT_VALUE00 0x3008
  57. #define REG_OUTPUT_VALUE01 0x3009
  58. #define REG_OUTPUT_VALUE02 0x300d
  59. #define REG_OUTPUT_SELECT00 0x300e
  60. #define REG_OUTPUT_SELECT01 0x300f
  61. #define REG_OUTPUT_SELECT02 0x3010
  62. #define REG_OUTPUT_DRIVE 0x3011
  63. #define REG_INPUT_READOUT00 0x302d
  64. #define REG_INPUT_READOUT01 0x302e
  65. #define REG_INPUT_READOUT02 0x302f
  66. #define REG_SC_PLL_CTRL0 0x3003
  67. #define REG_SC_PLL_CTRL1 0x3004
  68. #define REG_SC_PLL_CTRL2 0x3005
  69. #define REG_SC_PLL_CTRL3 0x3006
  70. #define REG_SC_CHIP_ID_H 0x300a
  71. #define REG_SC_CHIP_ID_L 0x300b
  72. #define REG_SC_PWC 0x3014
  73. #define REG_SC_CLKRST0 0x301a
  74. #define REG_SC_CLKRST1 0x301b
  75. #define REG_SC_CLKRST2 0x301c
  76. #define REG_SC_CLKRST3 0x301d
  77. #define REG_SC_SUB_ID 0x302a
  78. #define REG_SC_SCCB_ID 0x302b
  79. #define REG_GROUP_ADDRESS_00 0x3200
  80. #define REG_GROUP_ADDRESS_01 0x3201
  81. #define REG_GROUP_ADDRESS_02 0x3202
  82. #define REG_GROUP_ADDRESS_03 0x3203
  83. #define REG_GROUP_ACCESS 0x3208
  84. #define REG_AWB_R_GAIN_H 0x3400
  85. #define REG_AWB_R_GAIN_L 0x3401
  86. #define REG_AWB_G_GAIN_H 0x3402
  87. #define REG_AWB_G_GAIN_L 0x3403
  88. #define REG_AWB_B_GAIN_H 0x3404
  89. #define REG_AWB_B_GAIN_L 0x3405
  90. #define REG_AWB_MANUAL_CONTROL 0x3406
  91. #define REG_TIMING_HS_H 0x3800
  92. #define REG_TIMING_HS_L 0x3801
  93. #define REG_TIMING_VS_H 0x3802
  94. #define REG_TIMING_VS_L 0x3803
  95. #define REG_TIMING_HW_H 0x3804
  96. #define REG_TIMING_HW_L 0x3805
  97. #define REG_TIMING_VH_H 0x3806
  98. #define REG_TIMING_VH_L 0x3807
  99. #define REG_TIMING_DVPHO_H 0x3808
  100. #define REG_TIMING_DVPHO_L 0x3809
  101. #define REG_TIMING_DVPVO_H 0x380a
  102. #define REG_TIMING_DVPVO_L 0x380b
  103. #define REG_TIMING_HTS_H 0x380c
  104. #define REG_TIMING_HTS_L 0x380d
  105. #define REG_TIMING_VTS_H 0x380e
  106. #define REG_TIMING_VTS_L 0x380f
  107. #define REG_TIMING_HOFFS_H 0x3810
  108. #define REG_TIMING_HOFFS_L 0x3811
  109. #define REG_TIMING_VOFFS_H 0x3812
  110. #define REG_TIMING_VOFFS_L 0x3813
  111. #define REG_TIMING_XINC 0x3814
  112. #define REG_TIMING_YINC 0x3815
  113. #define REG_TIMING_VERT_FORMAT 0x3820
  114. #define REG_TIMING_HORIZ_FORMAT 0x3821
  115. #define REG_FORMAT_CTRL00 0x4300
  116. #define REG_VFIFO_READ_START_H 0x4608
  117. #define REG_VFIFO_READ_START_L 0x4609
  118. #define REG_DVP_CTRL02 0x4708
  119. #define REG_ISP_CTRL00 0x5000
  120. #define REG_ISP_CTRL01 0x5001
  121. #define REG_ISP_CTRL02 0x5002
  122. #define REG_LENC_RED_X0_H 0x500c
  123. #define REG_LENC_RED_X0_L 0x500d
  124. #define REG_LENC_RED_Y0_H 0x500e
  125. #define REG_LENC_RED_Y0_L 0x500f
  126. #define REG_LENC_RED_A1 0x5010
  127. #define REG_LENC_RED_B1 0x5011
  128. #define REG_LENC_RED_A2_B2 0x5012
  129. #define REG_LENC_GREEN_X0_H 0x5013
  130. #define REG_LENC_GREEN_X0_L 0x5014
  131. #define REG_LENC_GREEN_Y0_H 0x5015
  132. #define REG_LENC_GREEN_Y0_L 0x5016
  133. #define REG_LENC_GREEN_A1 0x5017
  134. #define REG_LENC_GREEN_B1 0x5018
  135. #define REG_LENC_GREEN_A2_B2 0x5019
  136. #define REG_LENC_BLUE_X0_H 0x501a
  137. #define REG_LENC_BLUE_X0_L 0x501b
  138. #define REG_LENC_BLUE_Y0_H 0x501c
  139. #define REG_LENC_BLUE_Y0_L 0x501d
  140. #define REG_LENC_BLUE_A1 0x501e
  141. #define REG_LENC_BLUE_B1 0x501f
  142. #define REG_LENC_BLUE_A2_B2 0x5020
  143. #define REG_AWB_CTRL00 0x5035
  144. #define REG_AWB_CTRL01 0x5036
  145. #define REG_AWB_CTRL02 0x5037
  146. #define REG_AWB_CTRL03 0x5038
  147. #define REG_AWB_CTRL04 0x5039
  148. #define REG_AWB_LOCAL_LIMIT 0x503a
  149. #define REG_AWB_CTRL12 0x5049
  150. #define REG_AWB_CTRL13 0x504a
  151. #define REG_AWB_CTRL14 0x504b
  152. #define REG_SHARPENMT_THRESH1 0x5064
  153. #define REG_SHARPENMT_THRESH2 0x5065
  154. #define REG_SHARPENMT_OFFSET1 0x5066
  155. #define REG_SHARPENMT_OFFSET2 0x5067
  156. #define REG_DENOISE_THRESH1 0x5068
  157. #define REG_DENOISE_THRESH2 0x5069
  158. #define REG_DENOISE_OFFSET1 0x506a
  159. #define REG_DENOISE_OFFSET2 0x506b
  160. #define REG_SHARPEN_THRESH1 0x506c
  161. #define REG_SHARPEN_THRESH2 0x506d
  162. #define REG_CIP_CTRL00 0x506e
  163. #define REG_CIP_CTRL01 0x506f
  164. #define REG_CMX_SIGN 0x5079
  165. #define REG_CMX_MISC_CTRL 0x507a
  166. #define REG_PRE_ISP_CTRL00 0x50a0
  167. #define TEST_PATTERN_ENABLE BIT(7)
  168. #define VERTICAL_COLOR_BAR_MASK 0x53
  169. #define REG_NULL 0x0000 /* Array end token */
  170. #define OV265X_ID(_msb, _lsb) ((_msb) << 8 | (_lsb))
  171. #define OV2659_ID 0x2656
  172. struct sensor_register {
  173. u16 addr;
  174. u8 value;
  175. };
  176. struct ov2659_framesize {
  177. u16 width;
  178. u16 height;
  179. u16 max_exp_lines;
  180. const struct sensor_register *regs;
  181. };
  182. struct ov2659_pll_ctrl {
  183. u8 ctrl1;
  184. u8 ctrl2;
  185. u8 ctrl3;
  186. };
  187. struct ov2659_pixfmt {
  188. u32 code;
  189. /* Output format Register Value (REG_FORMAT_CTRL00) */
  190. struct sensor_register *format_ctrl_regs;
  191. };
  192. struct pll_ctrl_reg {
  193. unsigned int div;
  194. unsigned char reg;
  195. };
  196. struct ov2659 {
  197. struct v4l2_subdev sd;
  198. struct media_pad pad;
  199. struct v4l2_mbus_framefmt format;
  200. unsigned int xvclk_frequency;
  201. const struct ov2659_platform_data *pdata;
  202. struct mutex lock;
  203. struct i2c_client *client;
  204. struct v4l2_ctrl_handler ctrls;
  205. struct v4l2_ctrl *link_frequency;
  206. const struct ov2659_framesize *frame_size;
  207. struct sensor_register *format_ctrl_regs;
  208. struct ov2659_pll_ctrl pll;
  209. int streaming;
  210. };
  211. static const struct sensor_register ov2659_init_regs[] = {
  212. { REG_IO_CTRL00, 0x03 },
  213. { REG_IO_CTRL01, 0xff },
  214. { REG_IO_CTRL02, 0xe0 },
  215. { 0x3633, 0x3d },
  216. { 0x3620, 0x02 },
  217. { 0x3631, 0x11 },
  218. { 0x3612, 0x04 },
  219. { 0x3630, 0x20 },
  220. { 0x4702, 0x02 },
  221. { 0x370c, 0x34 },
  222. { REG_TIMING_HS_H, 0x00 },
  223. { REG_TIMING_HS_L, 0x00 },
  224. { REG_TIMING_VS_H, 0x00 },
  225. { REG_TIMING_VS_L, 0x00 },
  226. { REG_TIMING_HW_H, 0x06 },
  227. { REG_TIMING_HW_L, 0x5f },
  228. { REG_TIMING_VH_H, 0x04 },
  229. { REG_TIMING_VH_L, 0xb7 },
  230. { REG_TIMING_DVPHO_H, 0x03 },
  231. { REG_TIMING_DVPHO_L, 0x20 },
  232. { REG_TIMING_DVPVO_H, 0x02 },
  233. { REG_TIMING_DVPVO_L, 0x58 },
  234. { REG_TIMING_HTS_H, 0x05 },
  235. { REG_TIMING_HTS_L, 0x14 },
  236. { REG_TIMING_VTS_H, 0x02 },
  237. { REG_TIMING_VTS_L, 0x68 },
  238. { REG_TIMING_HOFFS_L, 0x08 },
  239. { REG_TIMING_VOFFS_L, 0x02 },
  240. { REG_TIMING_XINC, 0x31 },
  241. { REG_TIMING_YINC, 0x31 },
  242. { 0x3a02, 0x02 },
  243. { 0x3a03, 0x68 },
  244. { 0x3a08, 0x00 },
  245. { 0x3a09, 0x5c },
  246. { 0x3a0a, 0x00 },
  247. { 0x3a0b, 0x4d },
  248. { 0x3a0d, 0x08 },
  249. { 0x3a0e, 0x06 },
  250. { 0x3a14, 0x02 },
  251. { 0x3a15, 0x28 },
  252. { REG_DVP_CTRL02, 0x01 },
  253. { 0x3623, 0x00 },
  254. { 0x3634, 0x76 },
  255. { 0x3701, 0x44 },
  256. { 0x3702, 0x18 },
  257. { 0x3703, 0x24 },
  258. { 0x3704, 0x24 },
  259. { 0x3705, 0x0c },
  260. { REG_TIMING_VERT_FORMAT, 0x81 },
  261. { REG_TIMING_HORIZ_FORMAT, 0x01 },
  262. { 0x370a, 0x52 },
  263. { REG_VFIFO_READ_START_H, 0x00 },
  264. { REG_VFIFO_READ_START_L, 0x80 },
  265. { REG_FORMAT_CTRL00, 0x30 },
  266. { 0x5086, 0x02 },
  267. { REG_ISP_CTRL00, 0xfb },
  268. { REG_ISP_CTRL01, 0x1f },
  269. { REG_ISP_CTRL02, 0x00 },
  270. { 0x5025, 0x0e },
  271. { 0x5026, 0x18 },
  272. { 0x5027, 0x34 },
  273. { 0x5028, 0x4c },
  274. { 0x5029, 0x62 },
  275. { 0x502a, 0x74 },
  276. { 0x502b, 0x85 },
  277. { 0x502c, 0x92 },
  278. { 0x502d, 0x9e },
  279. { 0x502e, 0xb2 },
  280. { 0x502f, 0xc0 },
  281. { 0x5030, 0xcc },
  282. { 0x5031, 0xe0 },
  283. { 0x5032, 0xee },
  284. { 0x5033, 0xf6 },
  285. { 0x5034, 0x11 },
  286. { 0x5070, 0x1c },
  287. { 0x5071, 0x5b },
  288. { 0x5072, 0x05 },
  289. { 0x5073, 0x20 },
  290. { 0x5074, 0x94 },
  291. { 0x5075, 0xb4 },
  292. { 0x5076, 0xb4 },
  293. { 0x5077, 0xaf },
  294. { 0x5078, 0x05 },
  295. { REG_CMX_SIGN, 0x98 },
  296. { REG_CMX_MISC_CTRL, 0x21 },
  297. { REG_AWB_CTRL00, 0x6a },
  298. { REG_AWB_CTRL01, 0x11 },
  299. { REG_AWB_CTRL02, 0x92 },
  300. { REG_AWB_CTRL03, 0x21 },
  301. { REG_AWB_CTRL04, 0xe1 },
  302. { REG_AWB_LOCAL_LIMIT, 0x01 },
  303. { 0x503c, 0x05 },
  304. { 0x503d, 0x08 },
  305. { 0x503e, 0x08 },
  306. { 0x503f, 0x64 },
  307. { 0x5040, 0x58 },
  308. { 0x5041, 0x2a },
  309. { 0x5042, 0xc5 },
  310. { 0x5043, 0x2e },
  311. { 0x5044, 0x3a },
  312. { 0x5045, 0x3c },
  313. { 0x5046, 0x44 },
  314. { 0x5047, 0xf8 },
  315. { 0x5048, 0x08 },
  316. { REG_AWB_CTRL12, 0x70 },
  317. { REG_AWB_CTRL13, 0xf0 },
  318. { REG_AWB_CTRL14, 0xf0 },
  319. { REG_LENC_RED_X0_H, 0x03 },
  320. { REG_LENC_RED_X0_L, 0x20 },
  321. { REG_LENC_RED_Y0_H, 0x02 },
  322. { REG_LENC_RED_Y0_L, 0x5c },
  323. { REG_LENC_RED_A1, 0x48 },
  324. { REG_LENC_RED_B1, 0x00 },
  325. { REG_LENC_RED_A2_B2, 0x66 },
  326. { REG_LENC_GREEN_X0_H, 0x03 },
  327. { REG_LENC_GREEN_X0_L, 0x30 },
  328. { REG_LENC_GREEN_Y0_H, 0x02 },
  329. { REG_LENC_GREEN_Y0_L, 0x7c },
  330. { REG_LENC_GREEN_A1, 0x40 },
  331. { REG_LENC_GREEN_B1, 0x00 },
  332. { REG_LENC_GREEN_A2_B2, 0x66 },
  333. { REG_LENC_BLUE_X0_H, 0x03 },
  334. { REG_LENC_BLUE_X0_L, 0x10 },
  335. { REG_LENC_BLUE_Y0_H, 0x02 },
  336. { REG_LENC_BLUE_Y0_L, 0x7c },
  337. { REG_LENC_BLUE_A1, 0x3a },
  338. { REG_LENC_BLUE_B1, 0x00 },
  339. { REG_LENC_BLUE_A2_B2, 0x66 },
  340. { REG_CIP_CTRL00, 0x44 },
  341. { REG_SHARPENMT_THRESH1, 0x08 },
  342. { REG_SHARPENMT_THRESH2, 0x10 },
  343. { REG_SHARPENMT_OFFSET1, 0x12 },
  344. { REG_SHARPENMT_OFFSET2, 0x02 },
  345. { REG_SHARPEN_THRESH1, 0x08 },
  346. { REG_SHARPEN_THRESH2, 0x10 },
  347. { REG_CIP_CTRL01, 0xa6 },
  348. { REG_DENOISE_THRESH1, 0x08 },
  349. { REG_DENOISE_THRESH2, 0x10 },
  350. { REG_DENOISE_OFFSET1, 0x04 },
  351. { REG_DENOISE_OFFSET2, 0x12 },
  352. { 0x507e, 0x40 },
  353. { 0x507f, 0x20 },
  354. { 0x507b, 0x02 },
  355. { REG_CMX_MISC_CTRL, 0x01 },
  356. { 0x5084, 0x0c },
  357. { 0x5085, 0x3e },
  358. { 0x5005, 0x80 },
  359. { 0x3a0f, 0x30 },
  360. { 0x3a10, 0x28 },
  361. { 0x3a1b, 0x32 },
  362. { 0x3a1e, 0x26 },
  363. { 0x3a11, 0x60 },
  364. { 0x3a1f, 0x14 },
  365. { 0x5060, 0x69 },
  366. { 0x5061, 0x7d },
  367. { 0x5062, 0x7d },
  368. { 0x5063, 0x69 },
  369. { REG_NULL, 0x00 },
  370. };
  371. /* 1280X720 720p */
  372. static struct sensor_register ov2659_720p[] = {
  373. { REG_TIMING_HS_H, 0x00 },
  374. { REG_TIMING_HS_L, 0xa0 },
  375. { REG_TIMING_VS_H, 0x00 },
  376. { REG_TIMING_VS_L, 0xf0 },
  377. { REG_TIMING_HW_H, 0x05 },
  378. { REG_TIMING_HW_L, 0xbf },
  379. { REG_TIMING_VH_H, 0x03 },
  380. { REG_TIMING_VH_L, 0xcb },
  381. { REG_TIMING_DVPHO_H, 0x05 },
  382. { REG_TIMING_DVPHO_L, 0x00 },
  383. { REG_TIMING_DVPVO_H, 0x02 },
  384. { REG_TIMING_DVPVO_L, 0xd0 },
  385. { REG_TIMING_HTS_H, 0x06 },
  386. { REG_TIMING_HTS_L, 0x4c },
  387. { REG_TIMING_VTS_H, 0x02 },
  388. { REG_TIMING_VTS_L, 0xe8 },
  389. { REG_TIMING_HOFFS_L, 0x10 },
  390. { REG_TIMING_VOFFS_L, 0x06 },
  391. { REG_TIMING_XINC, 0x11 },
  392. { REG_TIMING_YINC, 0x11 },
  393. { REG_TIMING_VERT_FORMAT, 0x80 },
  394. { REG_TIMING_HORIZ_FORMAT, 0x00 },
  395. { 0x3a03, 0xe8 },
  396. { 0x3a09, 0x6f },
  397. { 0x3a0b, 0x5d },
  398. { 0x3a15, 0x9a },
  399. { REG_NULL, 0x00 },
  400. };
  401. /* 1600X1200 UXGA */
  402. static struct sensor_register ov2659_uxga[] = {
  403. { REG_TIMING_HS_H, 0x00 },
  404. { REG_TIMING_HS_L, 0x00 },
  405. { REG_TIMING_VS_H, 0x00 },
  406. { REG_TIMING_VS_L, 0x00 },
  407. { REG_TIMING_HW_H, 0x06 },
  408. { REG_TIMING_HW_L, 0x5f },
  409. { REG_TIMING_VH_H, 0x04 },
  410. { REG_TIMING_VH_L, 0xbb },
  411. { REG_TIMING_DVPHO_H, 0x06 },
  412. { REG_TIMING_DVPHO_L, 0x40 },
  413. { REG_TIMING_DVPVO_H, 0x04 },
  414. { REG_TIMING_DVPVO_L, 0xb0 },
  415. { REG_TIMING_HTS_H, 0x07 },
  416. { REG_TIMING_HTS_L, 0x9f },
  417. { REG_TIMING_VTS_H, 0x04 },
  418. { REG_TIMING_VTS_L, 0xd0 },
  419. { REG_TIMING_HOFFS_L, 0x10 },
  420. { REG_TIMING_VOFFS_L, 0x06 },
  421. { REG_TIMING_XINC, 0x11 },
  422. { REG_TIMING_YINC, 0x11 },
  423. { 0x3a02, 0x04 },
  424. { 0x3a03, 0xd0 },
  425. { 0x3a08, 0x00 },
  426. { 0x3a09, 0xb8 },
  427. { 0x3a0a, 0x00 },
  428. { 0x3a0b, 0x9a },
  429. { 0x3a0d, 0x08 },
  430. { 0x3a0e, 0x06 },
  431. { 0x3a14, 0x04 },
  432. { 0x3a15, 0x50 },
  433. { 0x3623, 0x00 },
  434. { 0x3634, 0x44 },
  435. { 0x3701, 0x44 },
  436. { 0x3702, 0x30 },
  437. { 0x3703, 0x48 },
  438. { 0x3704, 0x48 },
  439. { 0x3705, 0x18 },
  440. { REG_TIMING_VERT_FORMAT, 0x80 },
  441. { REG_TIMING_HORIZ_FORMAT, 0x00 },
  442. { 0x370a, 0x12 },
  443. { REG_VFIFO_READ_START_H, 0x00 },
  444. { REG_VFIFO_READ_START_L, 0x80 },
  445. { REG_ISP_CTRL02, 0x00 },
  446. { REG_NULL, 0x00 },
  447. };
  448. /* 1280X1024 SXGA */
  449. static struct sensor_register ov2659_sxga[] = {
  450. { REG_TIMING_HS_H, 0x00 },
  451. { REG_TIMING_HS_L, 0x00 },
  452. { REG_TIMING_VS_H, 0x00 },
  453. { REG_TIMING_VS_L, 0x00 },
  454. { REG_TIMING_HW_H, 0x06 },
  455. { REG_TIMING_HW_L, 0x5f },
  456. { REG_TIMING_VH_H, 0x04 },
  457. { REG_TIMING_VH_L, 0xb7 },
  458. { REG_TIMING_DVPHO_H, 0x05 },
  459. { REG_TIMING_DVPHO_L, 0x00 },
  460. { REG_TIMING_DVPVO_H, 0x04 },
  461. { REG_TIMING_DVPVO_L, 0x00 },
  462. { REG_TIMING_HTS_H, 0x07 },
  463. { REG_TIMING_HTS_L, 0x9c },
  464. { REG_TIMING_VTS_H, 0x04 },
  465. { REG_TIMING_VTS_L, 0xd0 },
  466. { REG_TIMING_HOFFS_L, 0x10 },
  467. { REG_TIMING_VOFFS_L, 0x06 },
  468. { REG_TIMING_XINC, 0x11 },
  469. { REG_TIMING_YINC, 0x11 },
  470. { 0x3a02, 0x02 },
  471. { 0x3a03, 0x68 },
  472. { 0x3a08, 0x00 },
  473. { 0x3a09, 0x5c },
  474. { 0x3a0a, 0x00 },
  475. { 0x3a0b, 0x4d },
  476. { 0x3a0d, 0x08 },
  477. { 0x3a0e, 0x06 },
  478. { 0x3a14, 0x02 },
  479. { 0x3a15, 0x28 },
  480. { 0x3623, 0x00 },
  481. { 0x3634, 0x76 },
  482. { 0x3701, 0x44 },
  483. { 0x3702, 0x18 },
  484. { 0x3703, 0x24 },
  485. { 0x3704, 0x24 },
  486. { 0x3705, 0x0c },
  487. { REG_TIMING_VERT_FORMAT, 0x80 },
  488. { REG_TIMING_HORIZ_FORMAT, 0x00 },
  489. { 0x370a, 0x52 },
  490. { REG_VFIFO_READ_START_H, 0x00 },
  491. { REG_VFIFO_READ_START_L, 0x80 },
  492. { REG_ISP_CTRL02, 0x00 },
  493. { REG_NULL, 0x00 },
  494. };
  495. /* 1024X768 SXGA */
  496. static struct sensor_register ov2659_xga[] = {
  497. { REG_TIMING_HS_H, 0x00 },
  498. { REG_TIMING_HS_L, 0x00 },
  499. { REG_TIMING_VS_H, 0x00 },
  500. { REG_TIMING_VS_L, 0x00 },
  501. { REG_TIMING_HW_H, 0x06 },
  502. { REG_TIMING_HW_L, 0x5f },
  503. { REG_TIMING_VH_H, 0x04 },
  504. { REG_TIMING_VH_L, 0xb7 },
  505. { REG_TIMING_DVPHO_H, 0x04 },
  506. { REG_TIMING_DVPHO_L, 0x00 },
  507. { REG_TIMING_DVPVO_H, 0x03 },
  508. { REG_TIMING_DVPVO_L, 0x00 },
  509. { REG_TIMING_HTS_H, 0x07 },
  510. { REG_TIMING_HTS_L, 0x9c },
  511. { REG_TIMING_VTS_H, 0x04 },
  512. { REG_TIMING_VTS_L, 0xd0 },
  513. { REG_TIMING_HOFFS_L, 0x10 },
  514. { REG_TIMING_VOFFS_L, 0x06 },
  515. { REG_TIMING_XINC, 0x11 },
  516. { REG_TIMING_YINC, 0x11 },
  517. { 0x3a02, 0x02 },
  518. { 0x3a03, 0x68 },
  519. { 0x3a08, 0x00 },
  520. { 0x3a09, 0x5c },
  521. { 0x3a0a, 0x00 },
  522. { 0x3a0b, 0x4d },
  523. { 0x3a0d, 0x08 },
  524. { 0x3a0e, 0x06 },
  525. { 0x3a14, 0x02 },
  526. { 0x3a15, 0x28 },
  527. { 0x3623, 0x00 },
  528. { 0x3634, 0x76 },
  529. { 0x3701, 0x44 },
  530. { 0x3702, 0x18 },
  531. { 0x3703, 0x24 },
  532. { 0x3704, 0x24 },
  533. { 0x3705, 0x0c },
  534. { REG_TIMING_VERT_FORMAT, 0x80 },
  535. { REG_TIMING_HORIZ_FORMAT, 0x00 },
  536. { 0x370a, 0x52 },
  537. { REG_VFIFO_READ_START_H, 0x00 },
  538. { REG_VFIFO_READ_START_L, 0x80 },
  539. { REG_ISP_CTRL02, 0x00 },
  540. { REG_NULL, 0x00 },
  541. };
  542. /* 800X600 SVGA */
  543. static struct sensor_register ov2659_svga[] = {
  544. { REG_TIMING_HS_H, 0x00 },
  545. { REG_TIMING_HS_L, 0x00 },
  546. { REG_TIMING_VS_H, 0x00 },
  547. { REG_TIMING_VS_L, 0x00 },
  548. { REG_TIMING_HW_H, 0x06 },
  549. { REG_TIMING_HW_L, 0x5f },
  550. { REG_TIMING_VH_H, 0x04 },
  551. { REG_TIMING_VH_L, 0xb7 },
  552. { REG_TIMING_DVPHO_H, 0x03 },
  553. { REG_TIMING_DVPHO_L, 0x20 },
  554. { REG_TIMING_DVPVO_H, 0x02 },
  555. { REG_TIMING_DVPVO_L, 0x58 },
  556. { REG_TIMING_HTS_H, 0x05 },
  557. { REG_TIMING_HTS_L, 0x14 },
  558. { REG_TIMING_VTS_H, 0x02 },
  559. { REG_TIMING_VTS_L, 0x68 },
  560. { REG_TIMING_HOFFS_L, 0x08 },
  561. { REG_TIMING_VOFFS_L, 0x02 },
  562. { REG_TIMING_XINC, 0x31 },
  563. { REG_TIMING_YINC, 0x31 },
  564. { 0x3a02, 0x02 },
  565. { 0x3a03, 0x68 },
  566. { 0x3a08, 0x00 },
  567. { 0x3a09, 0x5c },
  568. { 0x3a0a, 0x00 },
  569. { 0x3a0b, 0x4d },
  570. { 0x3a0d, 0x08 },
  571. { 0x3a0e, 0x06 },
  572. { 0x3a14, 0x02 },
  573. { 0x3a15, 0x28 },
  574. { 0x3623, 0x00 },
  575. { 0x3634, 0x76 },
  576. { 0x3701, 0x44 },
  577. { 0x3702, 0x18 },
  578. { 0x3703, 0x24 },
  579. { 0x3704, 0x24 },
  580. { 0x3705, 0x0c },
  581. { REG_TIMING_VERT_FORMAT, 0x81 },
  582. { REG_TIMING_HORIZ_FORMAT, 0x01 },
  583. { 0x370a, 0x52 },
  584. { REG_VFIFO_READ_START_H, 0x00 },
  585. { REG_VFIFO_READ_START_L, 0x80 },
  586. { REG_ISP_CTRL02, 0x00 },
  587. { REG_NULL, 0x00 },
  588. };
  589. /* 640X480 VGA */
  590. static struct sensor_register ov2659_vga[] = {
  591. { REG_TIMING_HS_H, 0x00 },
  592. { REG_TIMING_HS_L, 0x00 },
  593. { REG_TIMING_VS_H, 0x00 },
  594. { REG_TIMING_VS_L, 0x00 },
  595. { REG_TIMING_HW_H, 0x06 },
  596. { REG_TIMING_HW_L, 0x5f },
  597. { REG_TIMING_VH_H, 0x04 },
  598. { REG_TIMING_VH_L, 0xb7 },
  599. { REG_TIMING_DVPHO_H, 0x02 },
  600. { REG_TIMING_DVPHO_L, 0x80 },
  601. { REG_TIMING_DVPVO_H, 0x01 },
  602. { REG_TIMING_DVPVO_L, 0xe0 },
  603. { REG_TIMING_HTS_H, 0x05 },
  604. { REG_TIMING_HTS_L, 0x14 },
  605. { REG_TIMING_VTS_H, 0x02 },
  606. { REG_TIMING_VTS_L, 0x68 },
  607. { REG_TIMING_HOFFS_L, 0x08 },
  608. { REG_TIMING_VOFFS_L, 0x02 },
  609. { REG_TIMING_XINC, 0x31 },
  610. { REG_TIMING_YINC, 0x31 },
  611. { 0x3a02, 0x02 },
  612. { 0x3a03, 0x68 },
  613. { 0x3a08, 0x00 },
  614. { 0x3a09, 0x5c },
  615. { 0x3a0a, 0x00 },
  616. { 0x3a0b, 0x4d },
  617. { 0x3a0d, 0x08 },
  618. { 0x3a0e, 0x06 },
  619. { 0x3a14, 0x02 },
  620. { 0x3a15, 0x28 },
  621. { 0x3623, 0x00 },
  622. { 0x3634, 0x76 },
  623. { 0x3701, 0x44 },
  624. { 0x3702, 0x18 },
  625. { 0x3703, 0x24 },
  626. { 0x3704, 0x24 },
  627. { 0x3705, 0x0c },
  628. { REG_TIMING_VERT_FORMAT, 0x81 },
  629. { REG_TIMING_HORIZ_FORMAT, 0x01 },
  630. { 0x370a, 0x52 },
  631. { REG_VFIFO_READ_START_H, 0x00 },
  632. { REG_VFIFO_READ_START_L, 0x80 },
  633. { REG_ISP_CTRL02, 0x10 },
  634. { REG_NULL, 0x00 },
  635. };
  636. /* 320X240 QVGA */
  637. static struct sensor_register ov2659_qvga[] = {
  638. { REG_TIMING_HS_H, 0x00 },
  639. { REG_TIMING_HS_L, 0x00 },
  640. { REG_TIMING_VS_H, 0x00 },
  641. { REG_TIMING_VS_L, 0x00 },
  642. { REG_TIMING_HW_H, 0x06 },
  643. { REG_TIMING_HW_L, 0x5f },
  644. { REG_TIMING_VH_H, 0x04 },
  645. { REG_TIMING_VH_L, 0xb7 },
  646. { REG_TIMING_DVPHO_H, 0x01 },
  647. { REG_TIMING_DVPHO_L, 0x40 },
  648. { REG_TIMING_DVPVO_H, 0x00 },
  649. { REG_TIMING_DVPVO_L, 0xf0 },
  650. { REG_TIMING_HTS_H, 0x05 },
  651. { REG_TIMING_HTS_L, 0x14 },
  652. { REG_TIMING_VTS_H, 0x02 },
  653. { REG_TIMING_VTS_L, 0x68 },
  654. { REG_TIMING_HOFFS_L, 0x08 },
  655. { REG_TIMING_VOFFS_L, 0x02 },
  656. { REG_TIMING_XINC, 0x31 },
  657. { REG_TIMING_YINC, 0x31 },
  658. { 0x3a02, 0x02 },
  659. { 0x3a03, 0x68 },
  660. { 0x3a08, 0x00 },
  661. { 0x3a09, 0x5c },
  662. { 0x3a0a, 0x00 },
  663. { 0x3a0b, 0x4d },
  664. { 0x3a0d, 0x08 },
  665. { 0x3a0e, 0x06 },
  666. { 0x3a14, 0x02 },
  667. { 0x3a15, 0x28 },
  668. { 0x3623, 0x00 },
  669. { 0x3634, 0x76 },
  670. { 0x3701, 0x44 },
  671. { 0x3702, 0x18 },
  672. { 0x3703, 0x24 },
  673. { 0x3704, 0x24 },
  674. { 0x3705, 0x0c },
  675. { REG_TIMING_VERT_FORMAT, 0x81 },
  676. { REG_TIMING_HORIZ_FORMAT, 0x01 },
  677. { 0x370a, 0x52 },
  678. { REG_VFIFO_READ_START_H, 0x00 },
  679. { REG_VFIFO_READ_START_L, 0x80 },
  680. { REG_ISP_CTRL02, 0x10 },
  681. { REG_NULL, 0x00 },
  682. };
  683. static const struct pll_ctrl_reg ctrl3[] = {
  684. { 1, 0x00 },
  685. { 2, 0x02 },
  686. { 3, 0x03 },
  687. { 4, 0x06 },
  688. { 6, 0x0d },
  689. { 8, 0x0e },
  690. { 12, 0x0f },
  691. { 16, 0x12 },
  692. { 24, 0x13 },
  693. { 32, 0x16 },
  694. { 48, 0x1b },
  695. { 64, 0x1e },
  696. { 96, 0x1f },
  697. { 0, 0x00 },
  698. };
  699. static const struct pll_ctrl_reg ctrl1[] = {
  700. { 2, 0x10 },
  701. { 4, 0x20 },
  702. { 6, 0x30 },
  703. { 8, 0x40 },
  704. { 10, 0x50 },
  705. { 12, 0x60 },
  706. { 14, 0x70 },
  707. { 16, 0x80 },
  708. { 18, 0x90 },
  709. { 20, 0xa0 },
  710. { 22, 0xb0 },
  711. { 24, 0xc0 },
  712. { 26, 0xd0 },
  713. { 28, 0xe0 },
  714. { 30, 0xf0 },
  715. { 0, 0x00 },
  716. };
  717. static const struct ov2659_framesize ov2659_framesizes[] = {
  718. { /* QVGA */
  719. .width = 320,
  720. .height = 240,
  721. .regs = ov2659_qvga,
  722. .max_exp_lines = 248,
  723. }, { /* VGA */
  724. .width = 640,
  725. .height = 480,
  726. .regs = ov2659_vga,
  727. .max_exp_lines = 498,
  728. }, { /* SVGA */
  729. .width = 800,
  730. .height = 600,
  731. .regs = ov2659_svga,
  732. .max_exp_lines = 498,
  733. }, { /* XGA */
  734. .width = 1024,
  735. .height = 768,
  736. .regs = ov2659_xga,
  737. .max_exp_lines = 498,
  738. }, { /* 720P */
  739. .width = 1280,
  740. .height = 720,
  741. .regs = ov2659_720p,
  742. .max_exp_lines = 498,
  743. }, { /* SXGA */
  744. .width = 1280,
  745. .height = 1024,
  746. .regs = ov2659_sxga,
  747. .max_exp_lines = 1048,
  748. }, { /* UXGA */
  749. .width = 1600,
  750. .height = 1200,
  751. .regs = ov2659_uxga,
  752. .max_exp_lines = 498,
  753. },
  754. };
  755. /* YUV422 YUYV*/
  756. static struct sensor_register ov2659_format_yuyv[] = {
  757. { REG_FORMAT_CTRL00, 0x30 },
  758. { REG_NULL, 0x0 },
  759. };
  760. /* YUV422 UYVY */
  761. static struct sensor_register ov2659_format_uyvy[] = {
  762. { REG_FORMAT_CTRL00, 0x32 },
  763. { REG_NULL, 0x0 },
  764. };
  765. /* Raw Bayer BGGR */
  766. static struct sensor_register ov2659_format_bggr[] = {
  767. { REG_FORMAT_CTRL00, 0x00 },
  768. { REG_NULL, 0x0 },
  769. };
  770. /* RGB565 */
  771. static struct sensor_register ov2659_format_rgb565[] = {
  772. { REG_FORMAT_CTRL00, 0x60 },
  773. { REG_NULL, 0x0 },
  774. };
  775. static const struct ov2659_pixfmt ov2659_formats[] = {
  776. {
  777. .code = MEDIA_BUS_FMT_YUYV8_2X8,
  778. .format_ctrl_regs = ov2659_format_yuyv,
  779. }, {
  780. .code = MEDIA_BUS_FMT_UYVY8_2X8,
  781. .format_ctrl_regs = ov2659_format_uyvy,
  782. }, {
  783. .code = MEDIA_BUS_FMT_RGB565_2X8_BE,
  784. .format_ctrl_regs = ov2659_format_rgb565,
  785. }, {
  786. .code = MEDIA_BUS_FMT_SBGGR8_1X8,
  787. .format_ctrl_regs = ov2659_format_bggr,
  788. },
  789. };
  790. static inline struct ov2659 *to_ov2659(struct v4l2_subdev *sd)
  791. {
  792. return container_of(sd, struct ov2659, sd);
  793. }
  794. /* sensor register write */
  795. static int ov2659_write(struct i2c_client *client, u16 reg, u8 val)
  796. {
  797. struct i2c_msg msg;
  798. u8 buf[3];
  799. int ret;
  800. buf[0] = reg >> 8;
  801. buf[1] = reg & 0xFF;
  802. buf[2] = val;
  803. msg.addr = client->addr;
  804. msg.flags = client->flags;
  805. msg.buf = buf;
  806. msg.len = sizeof(buf);
  807. ret = i2c_transfer(client->adapter, &msg, 1);
  808. if (ret >= 0)
  809. return 0;
  810. dev_dbg(&client->dev,
  811. "ov2659 write reg(0x%x val:0x%x) failed !\n", reg, val);
  812. return ret;
  813. }
  814. /* sensor register read */
  815. static int ov2659_read(struct i2c_client *client, u16 reg, u8 *val)
  816. {
  817. struct i2c_msg msg[2];
  818. u8 buf[2];
  819. int ret;
  820. buf[0] = reg >> 8;
  821. buf[1] = reg & 0xFF;
  822. msg[0].addr = client->addr;
  823. msg[0].flags = client->flags;
  824. msg[0].buf = buf;
  825. msg[0].len = sizeof(buf);
  826. msg[1].addr = client->addr;
  827. msg[1].flags = client->flags | I2C_M_RD;
  828. msg[1].buf = buf;
  829. msg[1].len = 1;
  830. ret = i2c_transfer(client->adapter, msg, 2);
  831. if (ret >= 0) {
  832. *val = buf[0];
  833. return 0;
  834. }
  835. dev_dbg(&client->dev,
  836. "ov2659 read reg(0x%x val:0x%x) failed !\n", reg, *val);
  837. return ret;
  838. }
  839. static int ov2659_write_array(struct i2c_client *client,
  840. const struct sensor_register *regs)
  841. {
  842. int i, ret = 0;
  843. for (i = 0; ret == 0 && regs[i].addr; i++)
  844. ret = ov2659_write(client, regs[i].addr, regs[i].value);
  845. return ret;
  846. }
  847. static void ov2659_pll_calc_params(struct ov2659 *ov2659)
  848. {
  849. const struct ov2659_platform_data *pdata = ov2659->pdata;
  850. u8 ctrl1_reg = 0, ctrl2_reg = 0, ctrl3_reg = 0;
  851. struct i2c_client *client = ov2659->client;
  852. unsigned int desired = pdata->link_frequency;
  853. u32 prediv, postdiv, mult;
  854. u32 bestdelta = -1;
  855. u32 delta, actual;
  856. int i, j;
  857. for (i = 0; ctrl1[i].div != 0; i++) {
  858. postdiv = ctrl1[i].div;
  859. for (j = 0; ctrl3[j].div != 0; j++) {
  860. prediv = ctrl3[j].div;
  861. for (mult = 1; mult <= 63; mult++) {
  862. actual = ov2659->xvclk_frequency;
  863. actual *= mult;
  864. actual /= prediv;
  865. actual /= postdiv;
  866. delta = actual - desired;
  867. delta = abs(delta);
  868. if ((delta < bestdelta) || (bestdelta == -1)) {
  869. bestdelta = delta;
  870. ctrl1_reg = ctrl1[i].reg;
  871. ctrl2_reg = mult;
  872. ctrl3_reg = ctrl3[j].reg;
  873. }
  874. }
  875. }
  876. }
  877. ov2659->pll.ctrl1 = ctrl1_reg;
  878. ov2659->pll.ctrl2 = ctrl2_reg;
  879. ov2659->pll.ctrl3 = ctrl3_reg;
  880. dev_dbg(&client->dev,
  881. "Actual reg config: ctrl1_reg: %02x ctrl2_reg: %02x ctrl3_reg: %02x\n",
  882. ctrl1_reg, ctrl2_reg, ctrl3_reg);
  883. }
  884. static int ov2659_set_pixel_clock(struct ov2659 *ov2659)
  885. {
  886. struct i2c_client *client = ov2659->client;
  887. struct sensor_register pll_regs[] = {
  888. {REG_SC_PLL_CTRL1, ov2659->pll.ctrl1},
  889. {REG_SC_PLL_CTRL2, ov2659->pll.ctrl2},
  890. {REG_SC_PLL_CTRL3, ov2659->pll.ctrl3},
  891. {REG_NULL, 0x00},
  892. };
  893. dev_dbg(&client->dev, "%s\n", __func__);
  894. return ov2659_write_array(client, pll_regs);
  895. };
  896. static void ov2659_get_default_format(struct v4l2_mbus_framefmt *format)
  897. {
  898. format->width = ov2659_framesizes[2].width;
  899. format->height = ov2659_framesizes[2].height;
  900. format->colorspace = V4L2_COLORSPACE_SRGB;
  901. format->code = ov2659_formats[0].code;
  902. format->field = V4L2_FIELD_NONE;
  903. }
  904. static void ov2659_set_streaming(struct ov2659 *ov2659, int on)
  905. {
  906. struct i2c_client *client = ov2659->client;
  907. int ret;
  908. on = !!on;
  909. dev_dbg(&client->dev, "%s: on: %d\n", __func__, on);
  910. ret = ov2659_write(client, REG_SOFTWARE_STANDBY, on);
  911. if (ret)
  912. dev_err(&client->dev, "ov2659 soft standby failed\n");
  913. }
  914. static int ov2659_init(struct v4l2_subdev *sd, u32 val)
  915. {
  916. struct i2c_client *client = v4l2_get_subdevdata(sd);
  917. return ov2659_write_array(client, ov2659_init_regs);
  918. }
  919. /*
  920. * V4L2 subdev video and pad level operations
  921. */
  922. static int ov2659_enum_mbus_code(struct v4l2_subdev *sd,
  923. struct v4l2_subdev_pad_config *cfg,
  924. struct v4l2_subdev_mbus_code_enum *code)
  925. {
  926. struct i2c_client *client = v4l2_get_subdevdata(sd);
  927. dev_dbg(&client->dev, "%s:\n", __func__);
  928. if (code->index >= ARRAY_SIZE(ov2659_formats))
  929. return -EINVAL;
  930. code->code = ov2659_formats[code->index].code;
  931. return 0;
  932. }
  933. static int ov2659_enum_frame_sizes(struct v4l2_subdev *sd,
  934. struct v4l2_subdev_pad_config *cfg,
  935. struct v4l2_subdev_frame_size_enum *fse)
  936. {
  937. struct i2c_client *client = v4l2_get_subdevdata(sd);
  938. int i = ARRAY_SIZE(ov2659_formats);
  939. dev_dbg(&client->dev, "%s:\n", __func__);
  940. if (fse->index >= ARRAY_SIZE(ov2659_framesizes))
  941. return -EINVAL;
  942. while (--i)
  943. if (fse->code == ov2659_formats[i].code)
  944. break;
  945. fse->code = ov2659_formats[i].code;
  946. fse->min_width = ov2659_framesizes[fse->index].width;
  947. fse->max_width = fse->min_width;
  948. fse->max_height = ov2659_framesizes[fse->index].height;
  949. fse->min_height = fse->max_height;
  950. return 0;
  951. }
  952. static int ov2659_get_fmt(struct v4l2_subdev *sd,
  953. struct v4l2_subdev_pad_config *cfg,
  954. struct v4l2_subdev_format *fmt)
  955. {
  956. struct i2c_client *client = v4l2_get_subdevdata(sd);
  957. struct ov2659 *ov2659 = to_ov2659(sd);
  958. dev_dbg(&client->dev, "ov2659_get_fmt\n");
  959. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  960. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  961. struct v4l2_mbus_framefmt *mf;
  962. mf = v4l2_subdev_get_try_format(sd, cfg, 0);
  963. mutex_lock(&ov2659->lock);
  964. fmt->format = *mf;
  965. mutex_unlock(&ov2659->lock);
  966. return 0;
  967. #else
  968. return -ENOTTY;
  969. #endif
  970. }
  971. mutex_lock(&ov2659->lock);
  972. fmt->format = ov2659->format;
  973. mutex_unlock(&ov2659->lock);
  974. dev_dbg(&client->dev, "ov2659_get_fmt: %x %dx%d\n",
  975. ov2659->format.code, ov2659->format.width,
  976. ov2659->format.height);
  977. return 0;
  978. }
  979. static void __ov2659_try_frame_size(struct v4l2_mbus_framefmt *mf,
  980. const struct ov2659_framesize **size)
  981. {
  982. const struct ov2659_framesize *fsize = &ov2659_framesizes[0];
  983. const struct ov2659_framesize *match = NULL;
  984. int i = ARRAY_SIZE(ov2659_framesizes);
  985. unsigned int min_err = UINT_MAX;
  986. while (i--) {
  987. int err = abs(fsize->width - mf->width)
  988. + abs(fsize->height - mf->height);
  989. if ((err < min_err) && (fsize->regs[0].addr)) {
  990. min_err = err;
  991. match = fsize;
  992. }
  993. fsize++;
  994. }
  995. if (!match)
  996. match = &ov2659_framesizes[2];
  997. mf->width = match->width;
  998. mf->height = match->height;
  999. if (size)
  1000. *size = match;
  1001. }
  1002. static int ov2659_set_fmt(struct v4l2_subdev *sd,
  1003. struct v4l2_subdev_pad_config *cfg,
  1004. struct v4l2_subdev_format *fmt)
  1005. {
  1006. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1007. int index = ARRAY_SIZE(ov2659_formats);
  1008. struct v4l2_mbus_framefmt *mf = &fmt->format;
  1009. const struct ov2659_framesize *size = NULL;
  1010. struct ov2659 *ov2659 = to_ov2659(sd);
  1011. int ret = 0;
  1012. dev_dbg(&client->dev, "ov2659_set_fmt\n");
  1013. __ov2659_try_frame_size(mf, &size);
  1014. while (--index >= 0)
  1015. if (ov2659_formats[index].code == mf->code)
  1016. break;
  1017. if (index < 0)
  1018. return -EINVAL;
  1019. mf->colorspace = V4L2_COLORSPACE_SRGB;
  1020. mf->code = ov2659_formats[index].code;
  1021. mf->field = V4L2_FIELD_NONE;
  1022. mutex_lock(&ov2659->lock);
  1023. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  1024. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  1025. mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
  1026. *mf = fmt->format;
  1027. #else
  1028. return -ENOTTY;
  1029. #endif
  1030. } else {
  1031. s64 val;
  1032. if (ov2659->streaming) {
  1033. mutex_unlock(&ov2659->lock);
  1034. return -EBUSY;
  1035. }
  1036. ov2659->frame_size = size;
  1037. ov2659->format = fmt->format;
  1038. ov2659->format_ctrl_regs =
  1039. ov2659_formats[index].format_ctrl_regs;
  1040. if (ov2659->format.code != MEDIA_BUS_FMT_SBGGR8_1X8)
  1041. val = ov2659->pdata->link_frequency / 2;
  1042. else
  1043. val = ov2659->pdata->link_frequency;
  1044. ret = v4l2_ctrl_s_ctrl_int64(ov2659->link_frequency, val);
  1045. if (ret < 0)
  1046. dev_warn(&client->dev,
  1047. "failed to set link_frequency rate (%d)\n",
  1048. ret);
  1049. }
  1050. mutex_unlock(&ov2659->lock);
  1051. return ret;
  1052. }
  1053. static int ov2659_set_frame_size(struct ov2659 *ov2659)
  1054. {
  1055. struct i2c_client *client = ov2659->client;
  1056. dev_dbg(&client->dev, "%s\n", __func__);
  1057. return ov2659_write_array(ov2659->client, ov2659->frame_size->regs);
  1058. }
  1059. static int ov2659_set_format(struct ov2659 *ov2659)
  1060. {
  1061. struct i2c_client *client = ov2659->client;
  1062. dev_dbg(&client->dev, "%s\n", __func__);
  1063. return ov2659_write_array(ov2659->client, ov2659->format_ctrl_regs);
  1064. }
  1065. static int ov2659_s_stream(struct v4l2_subdev *sd, int on)
  1066. {
  1067. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1068. struct ov2659 *ov2659 = to_ov2659(sd);
  1069. int ret = 0;
  1070. dev_dbg(&client->dev, "%s: on: %d\n", __func__, on);
  1071. mutex_lock(&ov2659->lock);
  1072. on = !!on;
  1073. if (ov2659->streaming == on)
  1074. goto unlock;
  1075. if (!on) {
  1076. /* Stop Streaming Sequence */
  1077. ov2659_set_streaming(ov2659, 0);
  1078. ov2659->streaming = on;
  1079. goto unlock;
  1080. }
  1081. ov2659_set_pixel_clock(ov2659);
  1082. ov2659_set_frame_size(ov2659);
  1083. ov2659_set_format(ov2659);
  1084. ov2659_set_streaming(ov2659, 1);
  1085. ov2659->streaming = on;
  1086. unlock:
  1087. mutex_unlock(&ov2659->lock);
  1088. return ret;
  1089. }
  1090. static int ov2659_set_test_pattern(struct ov2659 *ov2659, int value)
  1091. {
  1092. struct i2c_client *client = v4l2_get_subdevdata(&ov2659->sd);
  1093. int ret;
  1094. u8 val;
  1095. ret = ov2659_read(client, REG_PRE_ISP_CTRL00, &val);
  1096. if (ret < 0)
  1097. return ret;
  1098. switch (value) {
  1099. case 0:
  1100. val &= ~TEST_PATTERN_ENABLE;
  1101. break;
  1102. case 1:
  1103. val &= VERTICAL_COLOR_BAR_MASK;
  1104. val |= TEST_PATTERN_ENABLE;
  1105. break;
  1106. }
  1107. return ov2659_write(client, REG_PRE_ISP_CTRL00, val);
  1108. }
  1109. static int ov2659_s_ctrl(struct v4l2_ctrl *ctrl)
  1110. {
  1111. struct ov2659 *ov2659 =
  1112. container_of(ctrl->handler, struct ov2659, ctrls);
  1113. switch (ctrl->id) {
  1114. case V4L2_CID_TEST_PATTERN:
  1115. return ov2659_set_test_pattern(ov2659, ctrl->val);
  1116. }
  1117. return 0;
  1118. }
  1119. static const struct v4l2_ctrl_ops ov2659_ctrl_ops = {
  1120. .s_ctrl = ov2659_s_ctrl,
  1121. };
  1122. static const char * const ov2659_test_pattern_menu[] = {
  1123. "Disabled",
  1124. "Vertical Color Bars",
  1125. };
  1126. /* -----------------------------------------------------------------------------
  1127. * V4L2 subdev internal operations
  1128. */
  1129. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  1130. static int ov2659_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  1131. {
  1132. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1133. struct v4l2_mbus_framefmt *format =
  1134. v4l2_subdev_get_try_format(sd, fh->pad, 0);
  1135. dev_dbg(&client->dev, "%s:\n", __func__);
  1136. ov2659_get_default_format(format);
  1137. return 0;
  1138. }
  1139. #endif
  1140. static const struct v4l2_subdev_core_ops ov2659_subdev_core_ops = {
  1141. .log_status = v4l2_ctrl_subdev_log_status,
  1142. .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
  1143. .unsubscribe_event = v4l2_event_subdev_unsubscribe,
  1144. };
  1145. static const struct v4l2_subdev_video_ops ov2659_subdev_video_ops = {
  1146. .s_stream = ov2659_s_stream,
  1147. };
  1148. static const struct v4l2_subdev_pad_ops ov2659_subdev_pad_ops = {
  1149. .enum_mbus_code = ov2659_enum_mbus_code,
  1150. .enum_frame_size = ov2659_enum_frame_sizes,
  1151. .get_fmt = ov2659_get_fmt,
  1152. .set_fmt = ov2659_set_fmt,
  1153. };
  1154. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  1155. static const struct v4l2_subdev_ops ov2659_subdev_ops = {
  1156. .core = &ov2659_subdev_core_ops,
  1157. .video = &ov2659_subdev_video_ops,
  1158. .pad = &ov2659_subdev_pad_ops,
  1159. };
  1160. static const struct v4l2_subdev_internal_ops ov2659_subdev_internal_ops = {
  1161. .open = ov2659_open,
  1162. };
  1163. #endif
  1164. static int ov2659_detect(struct v4l2_subdev *sd)
  1165. {
  1166. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1167. u8 pid, ver;
  1168. int ret;
  1169. dev_dbg(&client->dev, "%s:\n", __func__);
  1170. ret = ov2659_write(client, REG_SOFTWARE_RESET, 0x01);
  1171. if (ret != 0) {
  1172. dev_err(&client->dev, "Sensor soft reset failed\n");
  1173. return -ENODEV;
  1174. }
  1175. usleep_range(1000, 2000);
  1176. /* Check sensor revision */
  1177. ret = ov2659_read(client, REG_SC_CHIP_ID_H, &pid);
  1178. if (!ret)
  1179. ret = ov2659_read(client, REG_SC_CHIP_ID_L, &ver);
  1180. if (!ret) {
  1181. unsigned short id;
  1182. id = OV265X_ID(pid, ver);
  1183. if (id != OV2659_ID)
  1184. dev_err(&client->dev,
  1185. "Sensor detection failed (%04X, %d)\n",
  1186. id, ret);
  1187. else {
  1188. dev_info(&client->dev, "Found OV%04X sensor\n", id);
  1189. ret = ov2659_init(sd, 0);
  1190. }
  1191. }
  1192. return ret;
  1193. }
  1194. static struct ov2659_platform_data *
  1195. ov2659_get_pdata(struct i2c_client *client)
  1196. {
  1197. struct ov2659_platform_data *pdata;
  1198. struct v4l2_of_endpoint *bus_cfg;
  1199. struct device_node *endpoint;
  1200. if (!IS_ENABLED(CONFIG_OF) || !client->dev.of_node)
  1201. return client->dev.platform_data;
  1202. endpoint = of_graph_get_next_endpoint(client->dev.of_node, NULL);
  1203. if (!endpoint)
  1204. return NULL;
  1205. bus_cfg = v4l2_of_alloc_parse_endpoint(endpoint);
  1206. if (IS_ERR(bus_cfg)) {
  1207. pdata = NULL;
  1208. goto done;
  1209. }
  1210. pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
  1211. if (!pdata)
  1212. goto done;
  1213. if (!bus_cfg->nr_of_link_frequencies) {
  1214. dev_err(&client->dev,
  1215. "link-frequencies property not found or too many\n");
  1216. pdata = NULL;
  1217. goto done;
  1218. }
  1219. pdata->link_frequency = bus_cfg->link_frequencies[0];
  1220. done:
  1221. v4l2_of_free_endpoint(bus_cfg);
  1222. of_node_put(endpoint);
  1223. return pdata;
  1224. }
  1225. static int ov2659_probe(struct i2c_client *client,
  1226. const struct i2c_device_id *id)
  1227. {
  1228. const struct ov2659_platform_data *pdata = ov2659_get_pdata(client);
  1229. struct v4l2_subdev *sd;
  1230. struct ov2659 *ov2659;
  1231. struct clk *clk;
  1232. int ret;
  1233. if (!pdata) {
  1234. dev_err(&client->dev, "platform data not specified\n");
  1235. return -EINVAL;
  1236. }
  1237. ov2659 = devm_kzalloc(&client->dev, sizeof(*ov2659), GFP_KERNEL);
  1238. if (!ov2659)
  1239. return -ENOMEM;
  1240. ov2659->pdata = pdata;
  1241. ov2659->client = client;
  1242. clk = devm_clk_get(&client->dev, "xvclk");
  1243. if (IS_ERR(clk))
  1244. return PTR_ERR(clk);
  1245. ov2659->xvclk_frequency = clk_get_rate(clk);
  1246. if (ov2659->xvclk_frequency < 6000000 ||
  1247. ov2659->xvclk_frequency > 27000000)
  1248. return -EINVAL;
  1249. v4l2_ctrl_handler_init(&ov2659->ctrls, 2);
  1250. ov2659->link_frequency =
  1251. v4l2_ctrl_new_std(&ov2659->ctrls, &ov2659_ctrl_ops,
  1252. V4L2_CID_PIXEL_RATE,
  1253. pdata->link_frequency / 2,
  1254. pdata->link_frequency, 1,
  1255. pdata->link_frequency);
  1256. v4l2_ctrl_new_std_menu_items(&ov2659->ctrls, &ov2659_ctrl_ops,
  1257. V4L2_CID_TEST_PATTERN,
  1258. ARRAY_SIZE(ov2659_test_pattern_menu) - 1,
  1259. 0, 0, ov2659_test_pattern_menu);
  1260. ov2659->sd.ctrl_handler = &ov2659->ctrls;
  1261. if (ov2659->ctrls.error) {
  1262. dev_err(&client->dev, "%s: control initialization error %d\n",
  1263. __func__, ov2659->ctrls.error);
  1264. return ov2659->ctrls.error;
  1265. }
  1266. sd = &ov2659->sd;
  1267. client->flags |= I2C_CLIENT_SCCB;
  1268. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  1269. v4l2_i2c_subdev_init(sd, client, &ov2659_subdev_ops);
  1270. sd->internal_ops = &ov2659_subdev_internal_ops;
  1271. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
  1272. V4L2_SUBDEV_FL_HAS_EVENTS;
  1273. #endif
  1274. #if defined(CONFIG_MEDIA_CONTROLLER)
  1275. ov2659->pad.flags = MEDIA_PAD_FL_SOURCE;
  1276. sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
  1277. ret = media_entity_pads_init(&sd->entity, 1, &ov2659->pad);
  1278. if (ret < 0) {
  1279. v4l2_ctrl_handler_free(&ov2659->ctrls);
  1280. return ret;
  1281. }
  1282. #endif
  1283. mutex_init(&ov2659->lock);
  1284. ov2659_get_default_format(&ov2659->format);
  1285. ov2659->frame_size = &ov2659_framesizes[2];
  1286. ov2659->format_ctrl_regs = ov2659_formats[0].format_ctrl_regs;
  1287. ret = ov2659_detect(sd);
  1288. if (ret < 0)
  1289. goto error;
  1290. /* Calculate the PLL register value needed */
  1291. ov2659_pll_calc_params(ov2659);
  1292. ret = v4l2_async_register_subdev(&ov2659->sd);
  1293. if (ret)
  1294. goto error;
  1295. dev_info(&client->dev, "%s sensor driver registered !!\n", sd->name);
  1296. return 0;
  1297. error:
  1298. v4l2_ctrl_handler_free(&ov2659->ctrls);
  1299. #if defined(CONFIG_MEDIA_CONTROLLER)
  1300. media_entity_cleanup(&sd->entity);
  1301. #endif
  1302. mutex_destroy(&ov2659->lock);
  1303. return ret;
  1304. }
  1305. static int ov2659_remove(struct i2c_client *client)
  1306. {
  1307. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1308. struct ov2659 *ov2659 = to_ov2659(sd);
  1309. v4l2_ctrl_handler_free(&ov2659->ctrls);
  1310. v4l2_async_unregister_subdev(sd);
  1311. #if defined(CONFIG_MEDIA_CONTROLLER)
  1312. media_entity_cleanup(&sd->entity);
  1313. #endif
  1314. mutex_destroy(&ov2659->lock);
  1315. return 0;
  1316. }
  1317. static const struct i2c_device_id ov2659_id[] = {
  1318. { "ov2659", 0 },
  1319. { /* sentinel */ },
  1320. };
  1321. MODULE_DEVICE_TABLE(i2c, ov2659_id);
  1322. #if IS_ENABLED(CONFIG_OF)
  1323. static const struct of_device_id ov2659_of_match[] = {
  1324. { .compatible = "ovti,ov2659", },
  1325. { /* sentinel */ },
  1326. };
  1327. MODULE_DEVICE_TABLE(of, ov2659_of_match);
  1328. #endif
  1329. static struct i2c_driver ov2659_i2c_driver = {
  1330. .driver = {
  1331. .name = DRIVER_NAME,
  1332. .of_match_table = of_match_ptr(ov2659_of_match),
  1333. },
  1334. .probe = ov2659_probe,
  1335. .remove = ov2659_remove,
  1336. .id_table = ov2659_id,
  1337. };
  1338. module_i2c_driver(ov2659_i2c_driver);
  1339. MODULE_AUTHOR("Benoit Parrot <bparrot@ti.com>");
  1340. MODULE_DESCRIPTION("OV2659 CMOS Image Sensor driver");
  1341. MODULE_LICENSE("GPL v2");