ov2659.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533
  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-fwnode.h>
  44. #include <media/v4l2-image-sizes.h>
  45. #include <media/v4l2-mediabus.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. { 0x370a, 0x12 },
  396. { 0x3a03, 0xe8 },
  397. { 0x3a09, 0x6f },
  398. { 0x3a0b, 0x5d },
  399. { 0x3a15, 0x9a },
  400. { REG_VFIFO_READ_START_H, 0x00 },
  401. { REG_VFIFO_READ_START_L, 0x80 },
  402. { REG_ISP_CTRL02, 0x00 },
  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, 0x80 },
  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, 0x80 },
  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. index = 0;
  1023. mf->code = ov2659_formats[index].code;
  1024. }
  1025. mf->colorspace = V4L2_COLORSPACE_SRGB;
  1026. mf->field = V4L2_FIELD_NONE;
  1027. mutex_lock(&ov2659->lock);
  1028. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  1029. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  1030. mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
  1031. *mf = fmt->format;
  1032. #else
  1033. ret = -ENOTTY;
  1034. #endif
  1035. } else {
  1036. s64 val;
  1037. if (ov2659->streaming) {
  1038. mutex_unlock(&ov2659->lock);
  1039. return -EBUSY;
  1040. }
  1041. ov2659->frame_size = size;
  1042. ov2659->format = fmt->format;
  1043. ov2659->format_ctrl_regs =
  1044. ov2659_formats[index].format_ctrl_regs;
  1045. if (ov2659->format.code != MEDIA_BUS_FMT_SBGGR8_1X8)
  1046. val = ov2659->pdata->link_frequency / 2;
  1047. else
  1048. val = ov2659->pdata->link_frequency;
  1049. ret = v4l2_ctrl_s_ctrl_int64(ov2659->link_frequency, val);
  1050. if (ret < 0)
  1051. dev_warn(&client->dev,
  1052. "failed to set link_frequency rate (%d)\n",
  1053. ret);
  1054. }
  1055. mutex_unlock(&ov2659->lock);
  1056. return ret;
  1057. }
  1058. static int ov2659_set_frame_size(struct ov2659 *ov2659)
  1059. {
  1060. struct i2c_client *client = ov2659->client;
  1061. dev_dbg(&client->dev, "%s\n", __func__);
  1062. return ov2659_write_array(ov2659->client, ov2659->frame_size->regs);
  1063. }
  1064. static int ov2659_set_format(struct ov2659 *ov2659)
  1065. {
  1066. struct i2c_client *client = ov2659->client;
  1067. dev_dbg(&client->dev, "%s\n", __func__);
  1068. return ov2659_write_array(ov2659->client, ov2659->format_ctrl_regs);
  1069. }
  1070. static int ov2659_s_stream(struct v4l2_subdev *sd, int on)
  1071. {
  1072. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1073. struct ov2659 *ov2659 = to_ov2659(sd);
  1074. int ret = 0;
  1075. dev_dbg(&client->dev, "%s: on: %d\n", __func__, on);
  1076. mutex_lock(&ov2659->lock);
  1077. on = !!on;
  1078. if (ov2659->streaming == on)
  1079. goto unlock;
  1080. if (!on) {
  1081. /* Stop Streaming Sequence */
  1082. ov2659_set_streaming(ov2659, 0);
  1083. ov2659->streaming = on;
  1084. goto unlock;
  1085. }
  1086. ret = ov2659_set_pixel_clock(ov2659);
  1087. if (!ret)
  1088. ret = ov2659_set_frame_size(ov2659);
  1089. if (!ret)
  1090. ret = ov2659_set_format(ov2659);
  1091. if (!ret) {
  1092. ov2659_set_streaming(ov2659, 1);
  1093. ov2659->streaming = on;
  1094. }
  1095. unlock:
  1096. mutex_unlock(&ov2659->lock);
  1097. return ret;
  1098. }
  1099. static int ov2659_set_test_pattern(struct ov2659 *ov2659, int value)
  1100. {
  1101. struct i2c_client *client = v4l2_get_subdevdata(&ov2659->sd);
  1102. int ret;
  1103. u8 val;
  1104. ret = ov2659_read(client, REG_PRE_ISP_CTRL00, &val);
  1105. if (ret < 0)
  1106. return ret;
  1107. switch (value) {
  1108. case 0:
  1109. val &= ~TEST_PATTERN_ENABLE;
  1110. break;
  1111. case 1:
  1112. val &= VERTICAL_COLOR_BAR_MASK;
  1113. val |= TEST_PATTERN_ENABLE;
  1114. break;
  1115. }
  1116. return ov2659_write(client, REG_PRE_ISP_CTRL00, val);
  1117. }
  1118. static int ov2659_s_ctrl(struct v4l2_ctrl *ctrl)
  1119. {
  1120. struct ov2659 *ov2659 =
  1121. container_of(ctrl->handler, struct ov2659, ctrls);
  1122. switch (ctrl->id) {
  1123. case V4L2_CID_TEST_PATTERN:
  1124. return ov2659_set_test_pattern(ov2659, ctrl->val);
  1125. }
  1126. return 0;
  1127. }
  1128. static const struct v4l2_ctrl_ops ov2659_ctrl_ops = {
  1129. .s_ctrl = ov2659_s_ctrl,
  1130. };
  1131. static const char * const ov2659_test_pattern_menu[] = {
  1132. "Disabled",
  1133. "Vertical Color Bars",
  1134. };
  1135. /* -----------------------------------------------------------------------------
  1136. * V4L2 subdev internal operations
  1137. */
  1138. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  1139. static int ov2659_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  1140. {
  1141. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1142. struct v4l2_mbus_framefmt *format =
  1143. v4l2_subdev_get_try_format(sd, fh->pad, 0);
  1144. dev_dbg(&client->dev, "%s:\n", __func__);
  1145. ov2659_get_default_format(format);
  1146. return 0;
  1147. }
  1148. #endif
  1149. static const struct v4l2_subdev_core_ops ov2659_subdev_core_ops = {
  1150. .log_status = v4l2_ctrl_subdev_log_status,
  1151. .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
  1152. .unsubscribe_event = v4l2_event_subdev_unsubscribe,
  1153. };
  1154. static const struct v4l2_subdev_video_ops ov2659_subdev_video_ops = {
  1155. .s_stream = ov2659_s_stream,
  1156. };
  1157. static const struct v4l2_subdev_pad_ops ov2659_subdev_pad_ops = {
  1158. .enum_mbus_code = ov2659_enum_mbus_code,
  1159. .enum_frame_size = ov2659_enum_frame_sizes,
  1160. .get_fmt = ov2659_get_fmt,
  1161. .set_fmt = ov2659_set_fmt,
  1162. };
  1163. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  1164. static const struct v4l2_subdev_ops ov2659_subdev_ops = {
  1165. .core = &ov2659_subdev_core_ops,
  1166. .video = &ov2659_subdev_video_ops,
  1167. .pad = &ov2659_subdev_pad_ops,
  1168. };
  1169. static const struct v4l2_subdev_internal_ops ov2659_subdev_internal_ops = {
  1170. .open = ov2659_open,
  1171. };
  1172. #endif
  1173. static int ov2659_detect(struct v4l2_subdev *sd)
  1174. {
  1175. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1176. u8 pid = 0;
  1177. u8 ver = 0;
  1178. int ret;
  1179. dev_dbg(&client->dev, "%s:\n", __func__);
  1180. ret = ov2659_write(client, REG_SOFTWARE_RESET, 0x01);
  1181. if (ret != 0) {
  1182. dev_err(&client->dev, "Sensor soft reset failed\n");
  1183. return -ENODEV;
  1184. }
  1185. usleep_range(1000, 2000);
  1186. /* Check sensor revision */
  1187. ret = ov2659_read(client, REG_SC_CHIP_ID_H, &pid);
  1188. if (!ret)
  1189. ret = ov2659_read(client, REG_SC_CHIP_ID_L, &ver);
  1190. if (!ret) {
  1191. unsigned short id;
  1192. id = OV265X_ID(pid, ver);
  1193. if (id != OV2659_ID)
  1194. dev_err(&client->dev,
  1195. "Sensor detection failed (%04X, %d)\n",
  1196. id, ret);
  1197. else {
  1198. dev_info(&client->dev, "Found OV%04X sensor\n", id);
  1199. ret = ov2659_init(sd, 0);
  1200. }
  1201. }
  1202. return ret;
  1203. }
  1204. static struct ov2659_platform_data *
  1205. ov2659_get_pdata(struct i2c_client *client)
  1206. {
  1207. struct ov2659_platform_data *pdata;
  1208. struct v4l2_fwnode_endpoint *bus_cfg;
  1209. struct device_node *endpoint;
  1210. if (!IS_ENABLED(CONFIG_OF) || !client->dev.of_node)
  1211. return client->dev.platform_data;
  1212. endpoint = of_graph_get_next_endpoint(client->dev.of_node, NULL);
  1213. if (!endpoint)
  1214. return NULL;
  1215. bus_cfg = v4l2_fwnode_endpoint_alloc_parse(of_fwnode_handle(endpoint));
  1216. if (IS_ERR(bus_cfg)) {
  1217. pdata = NULL;
  1218. goto done;
  1219. }
  1220. pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
  1221. if (!pdata)
  1222. goto done;
  1223. if (!bus_cfg->nr_of_link_frequencies) {
  1224. dev_err(&client->dev,
  1225. "link-frequencies property not found or too many\n");
  1226. pdata = NULL;
  1227. goto done;
  1228. }
  1229. pdata->link_frequency = bus_cfg->link_frequencies[0];
  1230. done:
  1231. v4l2_fwnode_endpoint_free(bus_cfg);
  1232. of_node_put(endpoint);
  1233. return pdata;
  1234. }
  1235. static int ov2659_probe(struct i2c_client *client,
  1236. const struct i2c_device_id *id)
  1237. {
  1238. const struct ov2659_platform_data *pdata = ov2659_get_pdata(client);
  1239. struct v4l2_subdev *sd;
  1240. struct ov2659 *ov2659;
  1241. struct clk *clk;
  1242. int ret;
  1243. if (!pdata) {
  1244. dev_err(&client->dev, "platform data not specified\n");
  1245. return -EINVAL;
  1246. }
  1247. ov2659 = devm_kzalloc(&client->dev, sizeof(*ov2659), GFP_KERNEL);
  1248. if (!ov2659)
  1249. return -ENOMEM;
  1250. ov2659->pdata = pdata;
  1251. ov2659->client = client;
  1252. clk = devm_clk_get(&client->dev, "xvclk");
  1253. if (IS_ERR(clk))
  1254. return PTR_ERR(clk);
  1255. ov2659->xvclk_frequency = clk_get_rate(clk);
  1256. if (ov2659->xvclk_frequency < 6000000 ||
  1257. ov2659->xvclk_frequency > 27000000)
  1258. return -EINVAL;
  1259. v4l2_ctrl_handler_init(&ov2659->ctrls, 2);
  1260. ov2659->link_frequency =
  1261. v4l2_ctrl_new_std(&ov2659->ctrls, &ov2659_ctrl_ops,
  1262. V4L2_CID_PIXEL_RATE,
  1263. pdata->link_frequency / 2,
  1264. pdata->link_frequency, 1,
  1265. pdata->link_frequency);
  1266. v4l2_ctrl_new_std_menu_items(&ov2659->ctrls, &ov2659_ctrl_ops,
  1267. V4L2_CID_TEST_PATTERN,
  1268. ARRAY_SIZE(ov2659_test_pattern_menu) - 1,
  1269. 0, 0, ov2659_test_pattern_menu);
  1270. ov2659->sd.ctrl_handler = &ov2659->ctrls;
  1271. if (ov2659->ctrls.error) {
  1272. dev_err(&client->dev, "%s: control initialization error %d\n",
  1273. __func__, ov2659->ctrls.error);
  1274. return ov2659->ctrls.error;
  1275. }
  1276. sd = &ov2659->sd;
  1277. client->flags |= I2C_CLIENT_SCCB;
  1278. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  1279. v4l2_i2c_subdev_init(sd, client, &ov2659_subdev_ops);
  1280. sd->internal_ops = &ov2659_subdev_internal_ops;
  1281. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
  1282. V4L2_SUBDEV_FL_HAS_EVENTS;
  1283. #endif
  1284. #if defined(CONFIG_MEDIA_CONTROLLER)
  1285. ov2659->pad.flags = MEDIA_PAD_FL_SOURCE;
  1286. sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
  1287. ret = media_entity_pads_init(&sd->entity, 1, &ov2659->pad);
  1288. if (ret < 0) {
  1289. v4l2_ctrl_handler_free(&ov2659->ctrls);
  1290. return ret;
  1291. }
  1292. #endif
  1293. mutex_init(&ov2659->lock);
  1294. ov2659_get_default_format(&ov2659->format);
  1295. ov2659->frame_size = &ov2659_framesizes[2];
  1296. ov2659->format_ctrl_regs = ov2659_formats[0].format_ctrl_regs;
  1297. ret = ov2659_detect(sd);
  1298. if (ret < 0)
  1299. goto error;
  1300. /* Calculate the PLL register value needed */
  1301. ov2659_pll_calc_params(ov2659);
  1302. ret = v4l2_async_register_subdev(&ov2659->sd);
  1303. if (ret)
  1304. goto error;
  1305. dev_info(&client->dev, "%s sensor driver registered !!\n", sd->name);
  1306. return 0;
  1307. error:
  1308. v4l2_ctrl_handler_free(&ov2659->ctrls);
  1309. media_entity_cleanup(&sd->entity);
  1310. mutex_destroy(&ov2659->lock);
  1311. return ret;
  1312. }
  1313. static int ov2659_remove(struct i2c_client *client)
  1314. {
  1315. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1316. struct ov2659 *ov2659 = to_ov2659(sd);
  1317. v4l2_ctrl_handler_free(&ov2659->ctrls);
  1318. v4l2_async_unregister_subdev(sd);
  1319. media_entity_cleanup(&sd->entity);
  1320. mutex_destroy(&ov2659->lock);
  1321. return 0;
  1322. }
  1323. static const struct i2c_device_id ov2659_id[] = {
  1324. { "ov2659", 0 },
  1325. { /* sentinel */ },
  1326. };
  1327. MODULE_DEVICE_TABLE(i2c, ov2659_id);
  1328. #if IS_ENABLED(CONFIG_OF)
  1329. static const struct of_device_id ov2659_of_match[] = {
  1330. { .compatible = "ovti,ov2659", },
  1331. { /* sentinel */ },
  1332. };
  1333. MODULE_DEVICE_TABLE(of, ov2659_of_match);
  1334. #endif
  1335. static struct i2c_driver ov2659_i2c_driver = {
  1336. .driver = {
  1337. .name = DRIVER_NAME,
  1338. .of_match_table = of_match_ptr(ov2659_of_match),
  1339. },
  1340. .probe = ov2659_probe,
  1341. .remove = ov2659_remove,
  1342. .id_table = ov2659_id,
  1343. };
  1344. module_i2c_driver(ov2659_i2c_driver);
  1345. MODULE_AUTHOR("Benoit Parrot <bparrot@ti.com>");
  1346. MODULE_DESCRIPTION("OV2659 CMOS Image Sensor driver");
  1347. MODULE_LICENSE("GPL v2");