ov5642.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. /*
  2. * Driver for OV5642 CMOS Image Sensor from Omnivision
  3. *
  4. * Copyright (C) 2011, Bastian Hecht <hechtb@gmail.com>
  5. *
  6. * Based on Sony IMX074 Camera Driver
  7. * Copyright (C) 2010, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
  8. *
  9. * Based on Omnivision OV7670 Camera Driver
  10. * Copyright (C) 2006-7 Jonathan Corbet <corbet@lwn.net>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. */
  16. #include <linux/delay.h>
  17. #include <linux/i2c.h>
  18. #include <linux/slab.h>
  19. #include <linux/videodev2.h>
  20. #include <media/soc_camera.h>
  21. #include <media/soc_mediabus.h>
  22. #include <media/v4l2-chip-ident.h>
  23. #include <media/v4l2-subdev.h>
  24. /* OV5642 registers */
  25. #define REG_CHIP_ID_HIGH 0x300a
  26. #define REG_CHIP_ID_LOW 0x300b
  27. #define REG_WINDOW_START_X_HIGH 0x3800
  28. #define REG_WINDOW_START_X_LOW 0x3801
  29. #define REG_WINDOW_START_Y_HIGH 0x3802
  30. #define REG_WINDOW_START_Y_LOW 0x3803
  31. #define REG_WINDOW_WIDTH_HIGH 0x3804
  32. #define REG_WINDOW_WIDTH_LOW 0x3805
  33. #define REG_WINDOW_HEIGHT_HIGH 0x3806
  34. #define REG_WINDOW_HEIGHT_LOW 0x3807
  35. #define REG_OUT_WIDTH_HIGH 0x3808
  36. #define REG_OUT_WIDTH_LOW 0x3809
  37. #define REG_OUT_HEIGHT_HIGH 0x380a
  38. #define REG_OUT_HEIGHT_LOW 0x380b
  39. #define REG_OUT_TOTAL_WIDTH_HIGH 0x380c
  40. #define REG_OUT_TOTAL_WIDTH_LOW 0x380d
  41. #define REG_OUT_TOTAL_HEIGHT_HIGH 0x380e
  42. #define REG_OUT_TOTAL_HEIGHT_LOW 0x380f
  43. /*
  44. * define standard resolution.
  45. * Works currently only for up to 720 lines
  46. * eg. 320x240, 640x480, 800x600, 1280x720, 2048x720
  47. */
  48. #define OV5642_WIDTH 1280
  49. #define OV5642_HEIGHT 720
  50. #define OV5642_TOTAL_WIDTH 3200
  51. #define OV5642_TOTAL_HEIGHT 2000
  52. #define OV5642_SENSOR_SIZE_X 2592
  53. #define OV5642_SENSOR_SIZE_Y 1944
  54. struct regval_list {
  55. u16 reg_num;
  56. u8 value;
  57. };
  58. static struct regval_list ov5642_default_regs_init[] = {
  59. { 0x3103, 0x93 },
  60. { 0x3008, 0x82 },
  61. { 0x3017, 0x7f },
  62. { 0x3018, 0xfc },
  63. { 0x3810, 0xc2 },
  64. { 0x3615, 0xf0 },
  65. { 0x3000, 0x0 },
  66. { 0x3001, 0x0 },
  67. { 0x3002, 0x0 },
  68. { 0x3003, 0x0 },
  69. { 0x3004, 0xff },
  70. { 0x3030, 0x2b },
  71. { 0x3011, 0x8 },
  72. { 0x3010, 0x10 },
  73. { 0x3604, 0x60 },
  74. { 0x3622, 0x60 },
  75. { 0x3621, 0x9 },
  76. { 0x3709, 0x0 },
  77. { 0x4000, 0x21 },
  78. { 0x401d, 0x22 },
  79. { 0x3600, 0x54 },
  80. { 0x3605, 0x4 },
  81. { 0x3606, 0x3f },
  82. { 0x3c01, 0x80 },
  83. { 0x300d, 0x22 },
  84. { 0x3623, 0x22 },
  85. { 0x5000, 0x4f },
  86. { 0x5020, 0x4 },
  87. { 0x5181, 0x79 },
  88. { 0x5182, 0x0 },
  89. { 0x5185, 0x22 },
  90. { 0x5197, 0x1 },
  91. { 0x5500, 0xa },
  92. { 0x5504, 0x0 },
  93. { 0x5505, 0x7f },
  94. { 0x5080, 0x8 },
  95. { 0x300e, 0x18 },
  96. { 0x4610, 0x0 },
  97. { 0x471d, 0x5 },
  98. { 0x4708, 0x6 },
  99. { 0x370c, 0xa0 },
  100. { 0x5687, 0x94 },
  101. { 0x501f, 0x0 },
  102. { 0x5000, 0x4f },
  103. { 0x5001, 0xcf },
  104. { 0x4300, 0x30 },
  105. { 0x4300, 0x30 },
  106. { 0x460b, 0x35 },
  107. { 0x471d, 0x0 },
  108. { 0x3002, 0xc },
  109. { 0x3002, 0x0 },
  110. { 0x4713, 0x3 },
  111. { 0x471c, 0x50 },
  112. { 0x4721, 0x2 },
  113. { 0x4402, 0x90 },
  114. { 0x460c, 0x22 },
  115. { 0x3815, 0x44 },
  116. { 0x3503, 0x7 },
  117. { 0x3501, 0x73 },
  118. { 0x3502, 0x80 },
  119. { 0x350b, 0x0 },
  120. { 0x3818, 0xc8 },
  121. { 0x3824, 0x11 },
  122. { 0x3a00, 0x78 },
  123. { 0x3a1a, 0x4 },
  124. { 0x3a13, 0x30 },
  125. { 0x3a18, 0x0 },
  126. { 0x3a19, 0x7c },
  127. { 0x3a08, 0x12 },
  128. { 0x3a09, 0xc0 },
  129. { 0x3a0a, 0xf },
  130. { 0x3a0b, 0xa0 },
  131. { 0x350c, 0x7 },
  132. { 0x350d, 0xd0 },
  133. { 0x3a0d, 0x8 },
  134. { 0x3a0e, 0x6 },
  135. { 0x3500, 0x0 },
  136. { 0x3501, 0x0 },
  137. { 0x3502, 0x0 },
  138. { 0x350a, 0x0 },
  139. { 0x350b, 0x0 },
  140. { 0x3503, 0x0 },
  141. { 0x3a0f, 0x3c },
  142. { 0x3a10, 0x32 },
  143. { 0x3a1b, 0x3c },
  144. { 0x3a1e, 0x32 },
  145. { 0x3a11, 0x80 },
  146. { 0x3a1f, 0x20 },
  147. { 0x3030, 0x2b },
  148. { 0x3a02, 0x0 },
  149. { 0x3a03, 0x7d },
  150. { 0x3a04, 0x0 },
  151. { 0x3a14, 0x0 },
  152. { 0x3a15, 0x7d },
  153. { 0x3a16, 0x0 },
  154. { 0x3a00, 0x78 },
  155. { 0x3a08, 0x9 },
  156. { 0x3a09, 0x60 },
  157. { 0x3a0a, 0x7 },
  158. { 0x3a0b, 0xd0 },
  159. { 0x3a0d, 0x10 },
  160. { 0x3a0e, 0xd },
  161. { 0x4407, 0x4 },
  162. { 0x5193, 0x70 },
  163. { 0x589b, 0x0 },
  164. { 0x589a, 0xc0 },
  165. { 0x401e, 0x20 },
  166. { 0x4001, 0x42 },
  167. { 0x401c, 0x6 },
  168. { 0x3825, 0xac },
  169. { 0x3827, 0xc },
  170. { 0x528a, 0x1 },
  171. { 0x528b, 0x4 },
  172. { 0x528c, 0x8 },
  173. { 0x528d, 0x10 },
  174. { 0x528e, 0x20 },
  175. { 0x528f, 0x28 },
  176. { 0x5290, 0x30 },
  177. { 0x5292, 0x0 },
  178. { 0x5293, 0x1 },
  179. { 0x5294, 0x0 },
  180. { 0x5295, 0x4 },
  181. { 0x5296, 0x0 },
  182. { 0x5297, 0x8 },
  183. { 0x5298, 0x0 },
  184. { 0x5299, 0x10 },
  185. { 0x529a, 0x0 },
  186. { 0x529b, 0x20 },
  187. { 0x529c, 0x0 },
  188. { 0x529d, 0x28 },
  189. { 0x529e, 0x0 },
  190. { 0x529f, 0x30 },
  191. { 0x5282, 0x0 },
  192. { 0x5300, 0x0 },
  193. { 0x5301, 0x20 },
  194. { 0x5302, 0x0 },
  195. { 0x5303, 0x7c },
  196. { 0x530c, 0x0 },
  197. { 0x530d, 0xc },
  198. { 0x530e, 0x20 },
  199. { 0x530f, 0x80 },
  200. { 0x5310, 0x20 },
  201. { 0x5311, 0x80 },
  202. { 0x5308, 0x20 },
  203. { 0x5309, 0x40 },
  204. { 0x5304, 0x0 },
  205. { 0x5305, 0x30 },
  206. { 0x5306, 0x0 },
  207. { 0x5307, 0x80 },
  208. { 0x5314, 0x8 },
  209. { 0x5315, 0x20 },
  210. { 0x5319, 0x30 },
  211. { 0x5316, 0x10 },
  212. { 0x5317, 0x0 },
  213. { 0x5318, 0x2 },
  214. { 0x5380, 0x1 },
  215. { 0x5381, 0x0 },
  216. { 0x5382, 0x0 },
  217. { 0x5383, 0x4e },
  218. { 0x5384, 0x0 },
  219. { 0x5385, 0xf },
  220. { 0x5386, 0x0 },
  221. { 0x5387, 0x0 },
  222. { 0x5388, 0x1 },
  223. { 0x5389, 0x15 },
  224. { 0x538a, 0x0 },
  225. { 0x538b, 0x31 },
  226. { 0x538c, 0x0 },
  227. { 0x538d, 0x0 },
  228. { 0x538e, 0x0 },
  229. { 0x538f, 0xf },
  230. { 0x5390, 0x0 },
  231. { 0x5391, 0xab },
  232. { 0x5392, 0x0 },
  233. { 0x5393, 0xa2 },
  234. { 0x5394, 0x8 },
  235. { 0x5480, 0x14 },
  236. { 0x5481, 0x21 },
  237. { 0x5482, 0x36 },
  238. { 0x5483, 0x57 },
  239. { 0x5484, 0x65 },
  240. { 0x5485, 0x71 },
  241. { 0x5486, 0x7d },
  242. { 0x5487, 0x87 },
  243. { 0x5488, 0x91 },
  244. { 0x5489, 0x9a },
  245. { 0x548a, 0xaa },
  246. { 0x548b, 0xb8 },
  247. { 0x548c, 0xcd },
  248. { 0x548d, 0xdd },
  249. { 0x548e, 0xea },
  250. { 0x548f, 0x1d },
  251. { 0x5490, 0x5 },
  252. { 0x5491, 0x0 },
  253. { 0x5492, 0x4 },
  254. { 0x5493, 0x20 },
  255. { 0x5494, 0x3 },
  256. { 0x5495, 0x60 },
  257. { 0x5496, 0x2 },
  258. { 0x5497, 0xb8 },
  259. { 0x5498, 0x2 },
  260. { 0x5499, 0x86 },
  261. { 0x549a, 0x2 },
  262. { 0x549b, 0x5b },
  263. { 0x549c, 0x2 },
  264. { 0x549d, 0x3b },
  265. { 0x549e, 0x2 },
  266. { 0x549f, 0x1c },
  267. { 0x54a0, 0x2 },
  268. { 0x54a1, 0x4 },
  269. { 0x54a2, 0x1 },
  270. { 0x54a3, 0xed },
  271. { 0x54a4, 0x1 },
  272. { 0x54a5, 0xc5 },
  273. { 0x54a6, 0x1 },
  274. { 0x54a7, 0xa5 },
  275. { 0x54a8, 0x1 },
  276. { 0x54a9, 0x6c },
  277. { 0x54aa, 0x1 },
  278. { 0x54ab, 0x41 },
  279. { 0x54ac, 0x1 },
  280. { 0x54ad, 0x20 },
  281. { 0x54ae, 0x0 },
  282. { 0x54af, 0x16 },
  283. { 0x54b0, 0x1 },
  284. { 0x54b1, 0x20 },
  285. { 0x54b2, 0x0 },
  286. { 0x54b3, 0x10 },
  287. { 0x54b4, 0x0 },
  288. { 0x54b5, 0xf0 },
  289. { 0x54b6, 0x0 },
  290. { 0x54b7, 0xdf },
  291. { 0x5402, 0x3f },
  292. { 0x5403, 0x0 },
  293. { 0x3406, 0x0 },
  294. { 0x5180, 0xff },
  295. { 0x5181, 0x52 },
  296. { 0x5182, 0x11 },
  297. { 0x5183, 0x14 },
  298. { 0x5184, 0x25 },
  299. { 0x5185, 0x24 },
  300. { 0x5186, 0x6 },
  301. { 0x5187, 0x8 },
  302. { 0x5188, 0x8 },
  303. { 0x5189, 0x7c },
  304. { 0x518a, 0x60 },
  305. { 0x518b, 0xb2 },
  306. { 0x518c, 0xb2 },
  307. { 0x518d, 0x44 },
  308. { 0x518e, 0x3d },
  309. { 0x518f, 0x58 },
  310. { 0x5190, 0x46 },
  311. { 0x5191, 0xf8 },
  312. { 0x5192, 0x4 },
  313. { 0x5193, 0x70 },
  314. { 0x5194, 0xf0 },
  315. { 0x5195, 0xf0 },
  316. { 0x5196, 0x3 },
  317. { 0x5197, 0x1 },
  318. { 0x5198, 0x4 },
  319. { 0x5199, 0x12 },
  320. { 0x519a, 0x4 },
  321. { 0x519b, 0x0 },
  322. { 0x519c, 0x6 },
  323. { 0x519d, 0x82 },
  324. { 0x519e, 0x0 },
  325. { 0x5025, 0x80 },
  326. { 0x3a0f, 0x38 },
  327. { 0x3a10, 0x30 },
  328. { 0x3a1b, 0x3a },
  329. { 0x3a1e, 0x2e },
  330. { 0x3a11, 0x60 },
  331. { 0x3a1f, 0x10 },
  332. { 0x5688, 0xa6 },
  333. { 0x5689, 0x6a },
  334. { 0x568a, 0xea },
  335. { 0x568b, 0xae },
  336. { 0x568c, 0xa6 },
  337. { 0x568d, 0x6a },
  338. { 0x568e, 0x62 },
  339. { 0x568f, 0x26 },
  340. { 0x5583, 0x40 },
  341. { 0x5584, 0x40 },
  342. { 0x5580, 0x2 },
  343. { 0x5000, 0xcf },
  344. { 0x5800, 0x27 },
  345. { 0x5801, 0x19 },
  346. { 0x5802, 0x12 },
  347. { 0x5803, 0xf },
  348. { 0x5804, 0x10 },
  349. { 0x5805, 0x15 },
  350. { 0x5806, 0x1e },
  351. { 0x5807, 0x2f },
  352. { 0x5808, 0x15 },
  353. { 0x5809, 0xd },
  354. { 0x580a, 0xa },
  355. { 0x580b, 0x9 },
  356. { 0x580c, 0xa },
  357. { 0x580d, 0xc },
  358. { 0x580e, 0x12 },
  359. { 0x580f, 0x19 },
  360. { 0x5810, 0xb },
  361. { 0x5811, 0x7 },
  362. { 0x5812, 0x4 },
  363. { 0x5813, 0x3 },
  364. { 0x5814, 0x3 },
  365. { 0x5815, 0x6 },
  366. { 0x5816, 0xa },
  367. { 0x5817, 0xf },
  368. { 0x5818, 0xa },
  369. { 0x5819, 0x5 },
  370. { 0x581a, 0x1 },
  371. { 0x581b, 0x0 },
  372. { 0x581c, 0x0 },
  373. { 0x581d, 0x3 },
  374. { 0x581e, 0x8 },
  375. { 0x581f, 0xc },
  376. { 0x5820, 0xa },
  377. { 0x5821, 0x5 },
  378. { 0x5822, 0x1 },
  379. { 0x5823, 0x0 },
  380. { 0x5824, 0x0 },
  381. { 0x5825, 0x3 },
  382. { 0x5826, 0x8 },
  383. { 0x5827, 0xc },
  384. { 0x5828, 0xe },
  385. { 0x5829, 0x8 },
  386. { 0x582a, 0x6 },
  387. { 0x582b, 0x4 },
  388. { 0x582c, 0x5 },
  389. { 0x582d, 0x7 },
  390. { 0x582e, 0xb },
  391. { 0x582f, 0x12 },
  392. { 0x5830, 0x18 },
  393. { 0x5831, 0x10 },
  394. { 0x5832, 0xc },
  395. { 0x5833, 0xa },
  396. { 0x5834, 0xb },
  397. { 0x5835, 0xe },
  398. { 0x5836, 0x15 },
  399. { 0x5837, 0x19 },
  400. { 0x5838, 0x32 },
  401. { 0x5839, 0x1f },
  402. { 0x583a, 0x18 },
  403. { 0x583b, 0x16 },
  404. { 0x583c, 0x17 },
  405. { 0x583d, 0x1e },
  406. { 0x583e, 0x26 },
  407. { 0x583f, 0x53 },
  408. { 0x5840, 0x10 },
  409. { 0x5841, 0xf },
  410. { 0x5842, 0xd },
  411. { 0x5843, 0xc },
  412. { 0x5844, 0xe },
  413. { 0x5845, 0x9 },
  414. { 0x5846, 0x11 },
  415. { 0x5847, 0x10 },
  416. { 0x5848, 0x10 },
  417. { 0x5849, 0x10 },
  418. { 0x584a, 0x10 },
  419. { 0x584b, 0xe },
  420. { 0x584c, 0x10 },
  421. { 0x584d, 0x10 },
  422. { 0x584e, 0x11 },
  423. { 0x584f, 0x10 },
  424. { 0x5850, 0xf },
  425. { 0x5851, 0xc },
  426. { 0x5852, 0xf },
  427. { 0x5853, 0x10 },
  428. { 0x5854, 0x10 },
  429. { 0x5855, 0xf },
  430. { 0x5856, 0xe },
  431. { 0x5857, 0xb },
  432. { 0x5858, 0x10 },
  433. { 0x5859, 0xd },
  434. { 0x585a, 0xd },
  435. { 0x585b, 0xc },
  436. { 0x585c, 0xc },
  437. { 0x585d, 0xc },
  438. { 0x585e, 0xb },
  439. { 0x585f, 0xc },
  440. { 0x5860, 0xc },
  441. { 0x5861, 0xc },
  442. { 0x5862, 0xd },
  443. { 0x5863, 0x8 },
  444. { 0x5864, 0x11 },
  445. { 0x5865, 0x18 },
  446. { 0x5866, 0x18 },
  447. { 0x5867, 0x19 },
  448. { 0x5868, 0x17 },
  449. { 0x5869, 0x19 },
  450. { 0x586a, 0x16 },
  451. { 0x586b, 0x13 },
  452. { 0x586c, 0x13 },
  453. { 0x586d, 0x12 },
  454. { 0x586e, 0x13 },
  455. { 0x586f, 0x16 },
  456. { 0x5870, 0x14 },
  457. { 0x5871, 0x12 },
  458. { 0x5872, 0x10 },
  459. { 0x5873, 0x11 },
  460. { 0x5874, 0x11 },
  461. { 0x5875, 0x16 },
  462. { 0x5876, 0x14 },
  463. { 0x5877, 0x11 },
  464. { 0x5878, 0x10 },
  465. { 0x5879, 0xf },
  466. { 0x587a, 0x10 },
  467. { 0x587b, 0x14 },
  468. { 0x587c, 0x13 },
  469. { 0x587d, 0x12 },
  470. { 0x587e, 0x11 },
  471. { 0x587f, 0x11 },
  472. { 0x5880, 0x12 },
  473. { 0x5881, 0x15 },
  474. { 0x5882, 0x14 },
  475. { 0x5883, 0x15 },
  476. { 0x5884, 0x15 },
  477. { 0x5885, 0x15 },
  478. { 0x5886, 0x13 },
  479. { 0x5887, 0x17 },
  480. { 0x3710, 0x10 },
  481. { 0x3632, 0x51 },
  482. { 0x3702, 0x10 },
  483. { 0x3703, 0xb2 },
  484. { 0x3704, 0x18 },
  485. { 0x370b, 0x40 },
  486. { 0x370d, 0x3 },
  487. { 0x3631, 0x1 },
  488. { 0x3632, 0x52 },
  489. { 0x3606, 0x24 },
  490. { 0x3620, 0x96 },
  491. { 0x5785, 0x7 },
  492. { 0x3a13, 0x30 },
  493. { 0x3600, 0x52 },
  494. { 0x3604, 0x48 },
  495. { 0x3606, 0x1b },
  496. { 0x370d, 0xb },
  497. { 0x370f, 0xc0 },
  498. { 0x3709, 0x1 },
  499. { 0x3823, 0x0 },
  500. { 0x5007, 0x0 },
  501. { 0x5009, 0x0 },
  502. { 0x5011, 0x0 },
  503. { 0x5013, 0x0 },
  504. { 0x519e, 0x0 },
  505. { 0x5086, 0x0 },
  506. { 0x5087, 0x0 },
  507. { 0x5088, 0x0 },
  508. { 0x5089, 0x0 },
  509. { 0x302b, 0x0 },
  510. { 0x3503, 0x7 },
  511. { 0x3011, 0x8 },
  512. { 0x350c, 0x2 },
  513. { 0x350d, 0xe4 },
  514. { 0x3621, 0xc9 },
  515. { 0x370a, 0x81 },
  516. { 0xffff, 0xff },
  517. };
  518. static struct regval_list ov5642_default_regs_finalise[] = {
  519. { 0x3810, 0xc2 },
  520. { 0x3818, 0xc9 },
  521. { 0x381c, 0x10 },
  522. { 0x381d, 0xa0 },
  523. { 0x381e, 0x5 },
  524. { 0x381f, 0xb0 },
  525. { 0x3820, 0x0 },
  526. { 0x3821, 0x0 },
  527. { 0x3824, 0x11 },
  528. { 0x3a08, 0x1b },
  529. { 0x3a09, 0xc0 },
  530. { 0x3a0a, 0x17 },
  531. { 0x3a0b, 0x20 },
  532. { 0x3a0d, 0x2 },
  533. { 0x3a0e, 0x1 },
  534. { 0x401c, 0x4 },
  535. { 0x5682, 0x5 },
  536. { 0x5683, 0x0 },
  537. { 0x5686, 0x2 },
  538. { 0x5687, 0xcc },
  539. { 0x5001, 0x4f },
  540. { 0x589b, 0x6 },
  541. { 0x589a, 0xc5 },
  542. { 0x3503, 0x0 },
  543. { 0x460c, 0x20 },
  544. { 0x460b, 0x37 },
  545. { 0x471c, 0xd0 },
  546. { 0x471d, 0x5 },
  547. { 0x3815, 0x1 },
  548. { 0x3818, 0xc1 },
  549. { 0x501f, 0x0 },
  550. { 0x5002, 0xe0 },
  551. { 0x4300, 0x32 }, /* UYVY */
  552. { 0x3002, 0x1c },
  553. { 0x4800, 0x14 },
  554. { 0x4801, 0xf },
  555. { 0x3007, 0x3b },
  556. { 0x300e, 0x4 },
  557. { 0x4803, 0x50 },
  558. { 0x3815, 0x1 },
  559. { 0x4713, 0x2 },
  560. { 0x4842, 0x1 },
  561. { 0x300f, 0xe },
  562. { 0x3003, 0x3 },
  563. { 0x3003, 0x1 },
  564. { 0xffff, 0xff },
  565. };
  566. struct ov5642_datafmt {
  567. enum v4l2_mbus_pixelcode code;
  568. enum v4l2_colorspace colorspace;
  569. };
  570. struct ov5642 {
  571. struct v4l2_subdev subdev;
  572. const struct ov5642_datafmt *fmt;
  573. };
  574. static const struct ov5642_datafmt ov5642_colour_fmts[] = {
  575. {V4L2_MBUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG},
  576. };
  577. static struct ov5642 *to_ov5642(const struct i2c_client *client)
  578. {
  579. return container_of(i2c_get_clientdata(client), struct ov5642, subdev);
  580. }
  581. /* Find a data format by a pixel code in an array */
  582. static const struct ov5642_datafmt
  583. *ov5642_find_datafmt(enum v4l2_mbus_pixelcode code)
  584. {
  585. int i;
  586. for (i = 0; i < ARRAY_SIZE(ov5642_colour_fmts); i++)
  587. if (ov5642_colour_fmts[i].code == code)
  588. return ov5642_colour_fmts + i;
  589. return NULL;
  590. }
  591. static int reg_read(struct i2c_client *client, u16 reg, u8 *val)
  592. {
  593. int ret;
  594. /* We have 16-bit i2c addresses - care for endianess */
  595. unsigned char data[2] = { reg >> 8, reg & 0xff };
  596. ret = i2c_master_send(client, data, 2);
  597. if (ret < 2) {
  598. dev_err(&client->dev, "%s: i2c read error, reg: %x\n",
  599. __func__, reg);
  600. return ret < 0 ? ret : -EIO;
  601. }
  602. ret = i2c_master_recv(client, val, 1);
  603. if (ret < 1) {
  604. dev_err(&client->dev, "%s: i2c read error, reg: %x\n",
  605. __func__, reg);
  606. return ret < 0 ? ret : -EIO;
  607. }
  608. return 0;
  609. }
  610. static int reg_write(struct i2c_client *client, u16 reg, u8 val)
  611. {
  612. int ret;
  613. unsigned char data[3] = { reg >> 8, reg & 0xff, val };
  614. ret = i2c_master_send(client, data, 3);
  615. if (ret < 3) {
  616. dev_err(&client->dev, "%s: i2c write error, reg: %x\n",
  617. __func__, reg);
  618. return ret < 0 ? ret : -EIO;
  619. }
  620. return 0;
  621. }
  622. #ifdef CONFIG_VIDEO_ADV_DEBUG
  623. static int ov5642_get_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
  624. {
  625. struct i2c_client *client = v4l2_get_subdevdata(sd);
  626. int ret;
  627. u8 val;
  628. if (reg->reg & ~0xffff)
  629. return -EINVAL;
  630. reg->size = 1;
  631. ret = reg_read(client, reg->reg, &val);
  632. if (!ret)
  633. reg->val = (__u64)val;
  634. return ret;
  635. }
  636. static int ov5642_set_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
  637. {
  638. struct i2c_client *client = v4l2_get_subdevdata(sd);
  639. if (reg->reg & ~0xffff || reg->val & ~0xff)
  640. return -EINVAL;
  641. return reg_write(client, reg->reg, reg->val);
  642. }
  643. #endif
  644. static int ov5642_write_array(struct i2c_client *client,
  645. struct regval_list *vals)
  646. {
  647. while (vals->reg_num != 0xffff || vals->value != 0xff) {
  648. int ret = reg_write(client, vals->reg_num, vals->value);
  649. if (ret < 0)
  650. return ret;
  651. vals++;
  652. }
  653. dev_dbg(&client->dev, "Register list loaded\n");
  654. return 0;
  655. }
  656. static int ov5642_set_resolution(struct i2c_client *client)
  657. {
  658. int ret;
  659. u8 start_x_high = ((OV5642_SENSOR_SIZE_X - OV5642_WIDTH) / 2) >> 8;
  660. u8 start_x_low = ((OV5642_SENSOR_SIZE_X - OV5642_WIDTH) / 2) & 0xff;
  661. u8 start_y_high = ((OV5642_SENSOR_SIZE_Y - OV5642_HEIGHT) / 2) >> 8;
  662. u8 start_y_low = ((OV5642_SENSOR_SIZE_Y - OV5642_HEIGHT) / 2) & 0xff;
  663. u8 width_high = OV5642_WIDTH >> 8;
  664. u8 width_low = OV5642_WIDTH & 0xff;
  665. u8 height_high = OV5642_HEIGHT >> 8;
  666. u8 height_low = OV5642_HEIGHT & 0xff;
  667. u8 total_width_high = OV5642_TOTAL_WIDTH >> 8;
  668. u8 total_width_low = OV5642_TOTAL_WIDTH & 0xff;
  669. u8 total_height_high = OV5642_TOTAL_HEIGHT >> 8;
  670. u8 total_height_low = OV5642_TOTAL_HEIGHT & 0xff;
  671. ret = reg_write(client, REG_WINDOW_START_X_HIGH, start_x_high);
  672. if (!ret)
  673. ret = reg_write(client, REG_WINDOW_START_X_LOW, start_x_low);
  674. if (!ret)
  675. ret = reg_write(client, REG_WINDOW_START_Y_HIGH, start_y_high);
  676. if (!ret)
  677. ret = reg_write(client, REG_WINDOW_START_Y_LOW, start_y_low);
  678. if (!ret)
  679. ret = reg_write(client, REG_WINDOW_WIDTH_HIGH, width_high);
  680. if (!ret)
  681. ret = reg_write(client, REG_WINDOW_WIDTH_LOW , width_low);
  682. if (!ret)
  683. ret = reg_write(client, REG_WINDOW_HEIGHT_HIGH, height_high);
  684. if (!ret)
  685. ret = reg_write(client, REG_WINDOW_HEIGHT_LOW, height_low);
  686. if (!ret)
  687. ret = reg_write(client, REG_OUT_WIDTH_HIGH, width_high);
  688. if (!ret)
  689. ret = reg_write(client, REG_OUT_WIDTH_LOW , width_low);
  690. if (!ret)
  691. ret = reg_write(client, REG_OUT_HEIGHT_HIGH, height_high);
  692. if (!ret)
  693. ret = reg_write(client, REG_OUT_HEIGHT_LOW, height_low);
  694. if (!ret)
  695. ret = reg_write(client, REG_OUT_TOTAL_WIDTH_HIGH, total_width_high);
  696. if (!ret)
  697. ret = reg_write(client, REG_OUT_TOTAL_WIDTH_LOW, total_width_low);
  698. if (!ret)
  699. ret = reg_write(client, REG_OUT_TOTAL_HEIGHT_HIGH, total_height_high);
  700. if (!ret)
  701. ret = reg_write(client, REG_OUT_TOTAL_HEIGHT_LOW, total_height_low);
  702. return ret;
  703. }
  704. static int ov5642_try_fmt(struct v4l2_subdev *sd,
  705. struct v4l2_mbus_framefmt *mf)
  706. {
  707. const struct ov5642_datafmt *fmt = ov5642_find_datafmt(mf->code);
  708. dev_dbg(sd->v4l2_dev->dev, "%s(%u) width: %u heigth: %u\n",
  709. __func__, mf->code, mf->width, mf->height);
  710. if (!fmt) {
  711. mf->code = ov5642_colour_fmts[0].code;
  712. mf->colorspace = ov5642_colour_fmts[0].colorspace;
  713. }
  714. mf->width = OV5642_WIDTH;
  715. mf->height = OV5642_HEIGHT;
  716. mf->field = V4L2_FIELD_NONE;
  717. return 0;
  718. }
  719. static int ov5642_s_fmt(struct v4l2_subdev *sd,
  720. struct v4l2_mbus_framefmt *mf)
  721. {
  722. struct i2c_client *client = v4l2_get_subdevdata(sd);
  723. struct ov5642 *priv = to_ov5642(client);
  724. dev_dbg(sd->v4l2_dev->dev, "%s(%u)\n", __func__, mf->code);
  725. /* MIPI CSI could have changed the format, double-check */
  726. if (!ov5642_find_datafmt(mf->code))
  727. return -EINVAL;
  728. ov5642_try_fmt(sd, mf);
  729. priv->fmt = ov5642_find_datafmt(mf->code);
  730. ov5642_write_array(client, ov5642_default_regs_init);
  731. ov5642_set_resolution(client);
  732. ov5642_write_array(client, ov5642_default_regs_finalise);
  733. return 0;
  734. }
  735. static int ov5642_g_fmt(struct v4l2_subdev *sd,
  736. struct v4l2_mbus_framefmt *mf)
  737. {
  738. struct i2c_client *client = v4l2_get_subdevdata(sd);
  739. struct ov5642 *priv = to_ov5642(client);
  740. const struct ov5642_datafmt *fmt = priv->fmt;
  741. mf->code = fmt->code;
  742. mf->colorspace = fmt->colorspace;
  743. mf->width = OV5642_WIDTH;
  744. mf->height = OV5642_HEIGHT;
  745. mf->field = V4L2_FIELD_NONE;
  746. return 0;
  747. }
  748. static int ov5642_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
  749. enum v4l2_mbus_pixelcode *code)
  750. {
  751. if (index >= ARRAY_SIZE(ov5642_colour_fmts))
  752. return -EINVAL;
  753. *code = ov5642_colour_fmts[index].code;
  754. return 0;
  755. }
  756. static int ov5642_g_chip_ident(struct v4l2_subdev *sd,
  757. struct v4l2_dbg_chip_ident *id)
  758. {
  759. struct i2c_client *client = v4l2_get_subdevdata(sd);
  760. if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
  761. return -EINVAL;
  762. if (id->match.addr != client->addr)
  763. return -ENODEV;
  764. id->ident = V4L2_IDENT_OV5642;
  765. id->revision = 0;
  766. return 0;
  767. }
  768. static int ov5642_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  769. {
  770. struct v4l2_rect *rect = &a->c;
  771. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  772. rect->top = 0;
  773. rect->left = 0;
  774. rect->width = OV5642_WIDTH;
  775. rect->height = OV5642_HEIGHT;
  776. return 0;
  777. }
  778. static int ov5642_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
  779. {
  780. a->bounds.left = 0;
  781. a->bounds.top = 0;
  782. a->bounds.width = OV5642_WIDTH;
  783. a->bounds.height = OV5642_HEIGHT;
  784. a->defrect = a->bounds;
  785. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  786. a->pixelaspect.numerator = 1;
  787. a->pixelaspect.denominator = 1;
  788. return 0;
  789. }
  790. static struct v4l2_subdev_video_ops ov5642_subdev_video_ops = {
  791. .s_mbus_fmt = ov5642_s_fmt,
  792. .g_mbus_fmt = ov5642_g_fmt,
  793. .try_mbus_fmt = ov5642_try_fmt,
  794. .enum_mbus_fmt = ov5642_enum_fmt,
  795. .g_crop = ov5642_g_crop,
  796. .cropcap = ov5642_cropcap,
  797. };
  798. static struct v4l2_subdev_core_ops ov5642_subdev_core_ops = {
  799. .g_chip_ident = ov5642_g_chip_ident,
  800. #ifdef CONFIG_VIDEO_ADV_DEBUG
  801. .g_register = ov5642_get_register,
  802. .s_register = ov5642_set_register,
  803. #endif
  804. };
  805. static struct v4l2_subdev_ops ov5642_subdev_ops = {
  806. .core = &ov5642_subdev_core_ops,
  807. .video = &ov5642_subdev_video_ops,
  808. };
  809. /*
  810. * We have to provide soc-camera operations, but we don't have anything to say
  811. * there. The MIPI CSI2 driver will provide .query_bus_param and .set_bus_param
  812. */
  813. static unsigned long soc_ov5642_query_bus_param(struct soc_camera_device *icd)
  814. {
  815. return 0;
  816. }
  817. static int soc_ov5642_set_bus_param(struct soc_camera_device *icd,
  818. unsigned long flags)
  819. {
  820. return -EINVAL;
  821. }
  822. static struct soc_camera_ops soc_ov5642_ops = {
  823. .query_bus_param = soc_ov5642_query_bus_param,
  824. .set_bus_param = soc_ov5642_set_bus_param,
  825. };
  826. static int ov5642_video_probe(struct soc_camera_device *icd,
  827. struct i2c_client *client)
  828. {
  829. int ret;
  830. u8 id_high, id_low;
  831. u16 id;
  832. /* Read sensor Model ID */
  833. ret = reg_read(client, REG_CHIP_ID_HIGH, &id_high);
  834. if (ret < 0)
  835. return ret;
  836. id = id_high << 8;
  837. ret = reg_read(client, REG_CHIP_ID_LOW, &id_low);
  838. if (ret < 0)
  839. return ret;
  840. id |= id_low;
  841. dev_info(&client->dev, "Chip ID 0x%04x detected\n", id);
  842. if (id != 0x5642)
  843. return -ENODEV;
  844. return 0;
  845. }
  846. static int ov5642_probe(struct i2c_client *client,
  847. const struct i2c_device_id *did)
  848. {
  849. struct ov5642 *priv;
  850. struct soc_camera_device *icd = client->dev.platform_data;
  851. struct soc_camera_link *icl;
  852. int ret;
  853. if (!icd) {
  854. dev_err(&client->dev, "OV5642: missing soc-camera data!\n");
  855. return -EINVAL;
  856. }
  857. icl = to_soc_camera_link(icd);
  858. if (!icl) {
  859. dev_err(&client->dev, "OV5642: missing platform data!\n");
  860. return -EINVAL;
  861. }
  862. priv = kzalloc(sizeof(struct ov5642), GFP_KERNEL);
  863. if (!priv)
  864. return -ENOMEM;
  865. v4l2_i2c_subdev_init(&priv->subdev, client, &ov5642_subdev_ops);
  866. icd->ops = &soc_ov5642_ops;
  867. priv->fmt = &ov5642_colour_fmts[0];
  868. ret = ov5642_video_probe(icd, client);
  869. if (ret < 0)
  870. goto error;
  871. return 0;
  872. error:
  873. icd->ops = NULL;
  874. kfree(priv);
  875. return ret;
  876. }
  877. static int ov5642_remove(struct i2c_client *client)
  878. {
  879. struct ov5642 *priv = to_ov5642(client);
  880. struct soc_camera_device *icd = client->dev.platform_data;
  881. struct soc_camera_link *icl = to_soc_camera_link(icd);
  882. icd->ops = NULL;
  883. if (icl->free_bus)
  884. icl->free_bus(icl);
  885. kfree(priv);
  886. return 0;
  887. }
  888. static const struct i2c_device_id ov5642_id[] = {
  889. { "ov5642", 0 },
  890. { }
  891. };
  892. MODULE_DEVICE_TABLE(i2c, ov5642_id);
  893. static struct i2c_driver ov5642_i2c_driver = {
  894. .driver = {
  895. .name = "ov5642",
  896. },
  897. .probe = ov5642_probe,
  898. .remove = ov5642_remove,
  899. .id_table = ov5642_id,
  900. };
  901. static int __init ov5642_mod_init(void)
  902. {
  903. return i2c_add_driver(&ov5642_i2c_driver);
  904. }
  905. static void __exit ov5642_mod_exit(void)
  906. {
  907. i2c_del_driver(&ov5642_i2c_driver);
  908. }
  909. module_init(ov5642_mod_init);
  910. module_exit(ov5642_mod_exit);
  911. MODULE_DESCRIPTION("Omnivision OV5642 Camera driver");
  912. MODULE_AUTHOR("Bastian Hecht <hechtb@gmail.com>");
  913. MODULE_LICENSE("GPL v2");