ov2659.c 36 KB

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