ov5642.c 24 KB

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