ov5642.c 21 KB

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