ov2659.c 36 KB

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