ov5645.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  1. /*
  2. * Driver for the OV5645 camera sensor.
  3. *
  4. * Copyright (c) 2011-2015, The Linux Foundation. All rights reserved.
  5. * Copyright (C) 2015 By Tech Design S.L. All Rights Reserved.
  6. * Copyright (C) 2012-2013 Freescale Semiconductor, Inc. All Rights Reserved.
  7. *
  8. * Based on:
  9. * - the OV5645 driver from QC msm-3.10 kernel on codeaurora.org:
  10. * https://us.codeaurora.org/cgit/quic/la/kernel/msm-3.10/tree/drivers/
  11. * media/platform/msm/camera_v2/sensor/ov5645.c?h=LA.BR.1.2.4_rb1.41
  12. * - the OV5640 driver posted on linux-media:
  13. * https://www.mail-archive.com/linux-media%40vger.kernel.org/msg92671.html
  14. */
  15. /*
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2 of the License, or
  19. * (at your option) any later version.
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. */
  25. #include <linux/bitops.h>
  26. #include <linux/clk.h>
  27. #include <linux/delay.h>
  28. #include <linux/device.h>
  29. #include <linux/gpio/consumer.h>
  30. #include <linux/i2c.h>
  31. #include <linux/init.h>
  32. #include <linux/module.h>
  33. #include <linux/of.h>
  34. #include <linux/of_graph.h>
  35. #include <linux/regulator/consumer.h>
  36. #include <linux/slab.h>
  37. #include <linux/types.h>
  38. #include <media/v4l2-ctrls.h>
  39. #include <media/v4l2-of.h>
  40. #include <media/v4l2-subdev.h>
  41. #define OV5645_VOLTAGE_ANALOG 2800000
  42. #define OV5645_VOLTAGE_DIGITAL_CORE 1500000
  43. #define OV5645_VOLTAGE_DIGITAL_IO 1800000
  44. #define OV5645_SYSTEM_CTRL0 0x3008
  45. #define OV5645_SYSTEM_CTRL0_START 0x02
  46. #define OV5645_SYSTEM_CTRL0_STOP 0x42
  47. #define OV5645_CHIP_ID_HIGH 0x300a
  48. #define OV5645_CHIP_ID_HIGH_BYTE 0x56
  49. #define OV5645_CHIP_ID_LOW 0x300b
  50. #define OV5645_CHIP_ID_LOW_BYTE 0x45
  51. #define OV5645_AWB_MANUAL_CONTROL 0x3406
  52. #define OV5645_AWB_MANUAL_ENABLE BIT(0)
  53. #define OV5645_AEC_PK_MANUAL 0x3503
  54. #define OV5645_AEC_MANUAL_ENABLE BIT(0)
  55. #define OV5645_AGC_MANUAL_ENABLE BIT(1)
  56. #define OV5645_TIMING_TC_REG20 0x3820
  57. #define OV5645_SENSOR_VFLIP BIT(1)
  58. #define OV5645_ISP_VFLIP BIT(2)
  59. #define OV5645_TIMING_TC_REG21 0x3821
  60. #define OV5645_SENSOR_MIRROR BIT(1)
  61. #define OV5645_PRE_ISP_TEST_SETTING_1 0x503d
  62. #define OV5645_TEST_PATTERN_MASK 0x3
  63. #define OV5645_SET_TEST_PATTERN(x) ((x) & OV5645_TEST_PATTERN_MASK)
  64. #define OV5645_TEST_PATTERN_ENABLE BIT(7)
  65. #define OV5645_SDE_SAT_U 0x5583
  66. #define OV5645_SDE_SAT_V 0x5584
  67. struct reg_value {
  68. u16 reg;
  69. u8 val;
  70. };
  71. struct ov5645_mode_info {
  72. u32 width;
  73. u32 height;
  74. const struct reg_value *data;
  75. u32 data_size;
  76. };
  77. struct ov5645 {
  78. struct i2c_client *i2c_client;
  79. struct device *dev;
  80. struct v4l2_subdev sd;
  81. struct media_pad pad;
  82. struct v4l2_of_endpoint ep;
  83. struct v4l2_mbus_framefmt fmt;
  84. struct v4l2_rect crop;
  85. struct clk *xclk;
  86. struct regulator *io_regulator;
  87. struct regulator *core_regulator;
  88. struct regulator *analog_regulator;
  89. const struct ov5645_mode_info *current_mode;
  90. struct v4l2_ctrl_handler ctrls;
  91. /* Cached register values */
  92. u8 aec_pk_manual;
  93. u8 timing_tc_reg20;
  94. u8 timing_tc_reg21;
  95. struct mutex power_lock; /* lock to protect power state */
  96. int power_count;
  97. struct gpio_desc *enable_gpio;
  98. struct gpio_desc *rst_gpio;
  99. };
  100. static inline struct ov5645 *to_ov5645(struct v4l2_subdev *sd)
  101. {
  102. return container_of(sd, struct ov5645, sd);
  103. }
  104. static const struct reg_value ov5645_global_init_setting[] = {
  105. { 0x3103, 0x11 },
  106. { 0x3008, 0x82 },
  107. { 0x3008, 0x42 },
  108. { 0x3103, 0x03 },
  109. { 0x3503, 0x07 },
  110. { 0x3002, 0x1c },
  111. { 0x3006, 0xc3 },
  112. { 0x300e, 0x45 },
  113. { 0x3017, 0x00 },
  114. { 0x3018, 0x00 },
  115. { 0x302e, 0x0b },
  116. { 0x3037, 0x13 },
  117. { 0x3108, 0x01 },
  118. { 0x3611, 0x06 },
  119. { 0x3500, 0x00 },
  120. { 0x3501, 0x01 },
  121. { 0x3502, 0x00 },
  122. { 0x350a, 0x00 },
  123. { 0x350b, 0x3f },
  124. { 0x3620, 0x33 },
  125. { 0x3621, 0xe0 },
  126. { 0x3622, 0x01 },
  127. { 0x3630, 0x2e },
  128. { 0x3631, 0x00 },
  129. { 0x3632, 0x32 },
  130. { 0x3633, 0x52 },
  131. { 0x3634, 0x70 },
  132. { 0x3635, 0x13 },
  133. { 0x3636, 0x03 },
  134. { 0x3703, 0x5a },
  135. { 0x3704, 0xa0 },
  136. { 0x3705, 0x1a },
  137. { 0x3709, 0x12 },
  138. { 0x370b, 0x61 },
  139. { 0x370f, 0x10 },
  140. { 0x3715, 0x78 },
  141. { 0x3717, 0x01 },
  142. { 0x371b, 0x20 },
  143. { 0x3731, 0x12 },
  144. { 0x3901, 0x0a },
  145. { 0x3905, 0x02 },
  146. { 0x3906, 0x10 },
  147. { 0x3719, 0x86 },
  148. { 0x3810, 0x00 },
  149. { 0x3811, 0x10 },
  150. { 0x3812, 0x00 },
  151. { 0x3821, 0x01 },
  152. { 0x3824, 0x01 },
  153. { 0x3826, 0x03 },
  154. { 0x3828, 0x08 },
  155. { 0x3a19, 0xf8 },
  156. { 0x3c01, 0x34 },
  157. { 0x3c04, 0x28 },
  158. { 0x3c05, 0x98 },
  159. { 0x3c07, 0x07 },
  160. { 0x3c09, 0xc2 },
  161. { 0x3c0a, 0x9c },
  162. { 0x3c0b, 0x40 },
  163. { 0x3c01, 0x34 },
  164. { 0x4001, 0x02 },
  165. { 0x4514, 0x00 },
  166. { 0x4520, 0xb0 },
  167. { 0x460b, 0x37 },
  168. { 0x460c, 0x20 },
  169. { 0x4818, 0x01 },
  170. { 0x481d, 0xf0 },
  171. { 0x481f, 0x50 },
  172. { 0x4823, 0x70 },
  173. { 0x4831, 0x14 },
  174. { 0x5000, 0xa7 },
  175. { 0x5001, 0x83 },
  176. { 0x501d, 0x00 },
  177. { 0x501f, 0x00 },
  178. { 0x503d, 0x00 },
  179. { 0x505c, 0x30 },
  180. { 0x5181, 0x59 },
  181. { 0x5183, 0x00 },
  182. { 0x5191, 0xf0 },
  183. { 0x5192, 0x03 },
  184. { 0x5684, 0x10 },
  185. { 0x5685, 0xa0 },
  186. { 0x5686, 0x0c },
  187. { 0x5687, 0x78 },
  188. { 0x5a00, 0x08 },
  189. { 0x5a21, 0x00 },
  190. { 0x5a24, 0x00 },
  191. { 0x3008, 0x02 },
  192. { 0x3503, 0x00 },
  193. { 0x5180, 0xff },
  194. { 0x5181, 0xf2 },
  195. { 0x5182, 0x00 },
  196. { 0x5183, 0x14 },
  197. { 0x5184, 0x25 },
  198. { 0x5185, 0x24 },
  199. { 0x5186, 0x09 },
  200. { 0x5187, 0x09 },
  201. { 0x5188, 0x0a },
  202. { 0x5189, 0x75 },
  203. { 0x518a, 0x52 },
  204. { 0x518b, 0xea },
  205. { 0x518c, 0xa8 },
  206. { 0x518d, 0x42 },
  207. { 0x518e, 0x38 },
  208. { 0x518f, 0x56 },
  209. { 0x5190, 0x42 },
  210. { 0x5191, 0xf8 },
  211. { 0x5192, 0x04 },
  212. { 0x5193, 0x70 },
  213. { 0x5194, 0xf0 },
  214. { 0x5195, 0xf0 },
  215. { 0x5196, 0x03 },
  216. { 0x5197, 0x01 },
  217. { 0x5198, 0x04 },
  218. { 0x5199, 0x12 },
  219. { 0x519a, 0x04 },
  220. { 0x519b, 0x00 },
  221. { 0x519c, 0x06 },
  222. { 0x519d, 0x82 },
  223. { 0x519e, 0x38 },
  224. { 0x5381, 0x1e },
  225. { 0x5382, 0x5b },
  226. { 0x5383, 0x08 },
  227. { 0x5384, 0x0a },
  228. { 0x5385, 0x7e },
  229. { 0x5386, 0x88 },
  230. { 0x5387, 0x7c },
  231. { 0x5388, 0x6c },
  232. { 0x5389, 0x10 },
  233. { 0x538a, 0x01 },
  234. { 0x538b, 0x98 },
  235. { 0x5300, 0x08 },
  236. { 0x5301, 0x30 },
  237. { 0x5302, 0x10 },
  238. { 0x5303, 0x00 },
  239. { 0x5304, 0x08 },
  240. { 0x5305, 0x30 },
  241. { 0x5306, 0x08 },
  242. { 0x5307, 0x16 },
  243. { 0x5309, 0x08 },
  244. { 0x530a, 0x30 },
  245. { 0x530b, 0x04 },
  246. { 0x530c, 0x06 },
  247. { 0x5480, 0x01 },
  248. { 0x5481, 0x08 },
  249. { 0x5482, 0x14 },
  250. { 0x5483, 0x28 },
  251. { 0x5484, 0x51 },
  252. { 0x5485, 0x65 },
  253. { 0x5486, 0x71 },
  254. { 0x5487, 0x7d },
  255. { 0x5488, 0x87 },
  256. { 0x5489, 0x91 },
  257. { 0x548a, 0x9a },
  258. { 0x548b, 0xaa },
  259. { 0x548c, 0xb8 },
  260. { 0x548d, 0xcd },
  261. { 0x548e, 0xdd },
  262. { 0x548f, 0xea },
  263. { 0x5490, 0x1d },
  264. { 0x5580, 0x02 },
  265. { 0x5583, 0x40 },
  266. { 0x5584, 0x10 },
  267. { 0x5589, 0x10 },
  268. { 0x558a, 0x00 },
  269. { 0x558b, 0xf8 },
  270. { 0x5800, 0x3f },
  271. { 0x5801, 0x16 },
  272. { 0x5802, 0x0e },
  273. { 0x5803, 0x0d },
  274. { 0x5804, 0x17 },
  275. { 0x5805, 0x3f },
  276. { 0x5806, 0x0b },
  277. { 0x5807, 0x06 },
  278. { 0x5808, 0x04 },
  279. { 0x5809, 0x04 },
  280. { 0x580a, 0x06 },
  281. { 0x580b, 0x0b },
  282. { 0x580c, 0x09 },
  283. { 0x580d, 0x03 },
  284. { 0x580e, 0x00 },
  285. { 0x580f, 0x00 },
  286. { 0x5810, 0x03 },
  287. { 0x5811, 0x08 },
  288. { 0x5812, 0x0a },
  289. { 0x5813, 0x03 },
  290. { 0x5814, 0x00 },
  291. { 0x5815, 0x00 },
  292. { 0x5816, 0x04 },
  293. { 0x5817, 0x09 },
  294. { 0x5818, 0x0f },
  295. { 0x5819, 0x08 },
  296. { 0x581a, 0x06 },
  297. { 0x581b, 0x06 },
  298. { 0x581c, 0x08 },
  299. { 0x581d, 0x0c },
  300. { 0x581e, 0x3f },
  301. { 0x581f, 0x1e },
  302. { 0x5820, 0x12 },
  303. { 0x5821, 0x13 },
  304. { 0x5822, 0x21 },
  305. { 0x5823, 0x3f },
  306. { 0x5824, 0x68 },
  307. { 0x5825, 0x28 },
  308. { 0x5826, 0x2c },
  309. { 0x5827, 0x28 },
  310. { 0x5828, 0x08 },
  311. { 0x5829, 0x48 },
  312. { 0x582a, 0x64 },
  313. { 0x582b, 0x62 },
  314. { 0x582c, 0x64 },
  315. { 0x582d, 0x28 },
  316. { 0x582e, 0x46 },
  317. { 0x582f, 0x62 },
  318. { 0x5830, 0x60 },
  319. { 0x5831, 0x62 },
  320. { 0x5832, 0x26 },
  321. { 0x5833, 0x48 },
  322. { 0x5834, 0x66 },
  323. { 0x5835, 0x44 },
  324. { 0x5836, 0x64 },
  325. { 0x5837, 0x28 },
  326. { 0x5838, 0x66 },
  327. { 0x5839, 0x48 },
  328. { 0x583a, 0x2c },
  329. { 0x583b, 0x28 },
  330. { 0x583c, 0x26 },
  331. { 0x583d, 0xae },
  332. { 0x5025, 0x00 },
  333. { 0x3a0f, 0x30 },
  334. { 0x3a10, 0x28 },
  335. { 0x3a1b, 0x30 },
  336. { 0x3a1e, 0x26 },
  337. { 0x3a11, 0x60 },
  338. { 0x3a1f, 0x14 },
  339. { 0x0601, 0x02 },
  340. { 0x3008, 0x42 },
  341. { 0x3008, 0x02 }
  342. };
  343. static const struct reg_value ov5645_setting_sxga[] = {
  344. { 0x3612, 0xa9 },
  345. { 0x3614, 0x50 },
  346. { 0x3618, 0x00 },
  347. { 0x3034, 0x18 },
  348. { 0x3035, 0x21 },
  349. { 0x3036, 0x70 },
  350. { 0x3600, 0x09 },
  351. { 0x3601, 0x43 },
  352. { 0x3708, 0x66 },
  353. { 0x370c, 0xc3 },
  354. { 0x3800, 0x00 },
  355. { 0x3801, 0x00 },
  356. { 0x3802, 0x00 },
  357. { 0x3803, 0x06 },
  358. { 0x3804, 0x0a },
  359. { 0x3805, 0x3f },
  360. { 0x3806, 0x07 },
  361. { 0x3807, 0x9d },
  362. { 0x3808, 0x05 },
  363. { 0x3809, 0x00 },
  364. { 0x380a, 0x03 },
  365. { 0x380b, 0xc0 },
  366. { 0x380c, 0x07 },
  367. { 0x380d, 0x68 },
  368. { 0x380e, 0x03 },
  369. { 0x380f, 0xd8 },
  370. { 0x3813, 0x06 },
  371. { 0x3814, 0x31 },
  372. { 0x3815, 0x31 },
  373. { 0x3820, 0x47 },
  374. { 0x3a02, 0x03 },
  375. { 0x3a03, 0xd8 },
  376. { 0x3a08, 0x01 },
  377. { 0x3a09, 0xf8 },
  378. { 0x3a0a, 0x01 },
  379. { 0x3a0b, 0xa4 },
  380. { 0x3a0e, 0x02 },
  381. { 0x3a0d, 0x02 },
  382. { 0x3a14, 0x03 },
  383. { 0x3a15, 0xd8 },
  384. { 0x3a18, 0x00 },
  385. { 0x4004, 0x02 },
  386. { 0x4005, 0x18 },
  387. { 0x4300, 0x32 },
  388. { 0x4202, 0x00 }
  389. };
  390. static const struct reg_value ov5645_setting_1080p[] = {
  391. { 0x3612, 0xab },
  392. { 0x3614, 0x50 },
  393. { 0x3618, 0x04 },
  394. { 0x3034, 0x18 },
  395. { 0x3035, 0x11 },
  396. { 0x3036, 0x54 },
  397. { 0x3600, 0x08 },
  398. { 0x3601, 0x33 },
  399. { 0x3708, 0x63 },
  400. { 0x370c, 0xc0 },
  401. { 0x3800, 0x01 },
  402. { 0x3801, 0x50 },
  403. { 0x3802, 0x01 },
  404. { 0x3803, 0xb2 },
  405. { 0x3804, 0x08 },
  406. { 0x3805, 0xef },
  407. { 0x3806, 0x05 },
  408. { 0x3807, 0xf1 },
  409. { 0x3808, 0x07 },
  410. { 0x3809, 0x80 },
  411. { 0x380a, 0x04 },
  412. { 0x380b, 0x38 },
  413. { 0x380c, 0x09 },
  414. { 0x380d, 0xc4 },
  415. { 0x380e, 0x04 },
  416. { 0x380f, 0x60 },
  417. { 0x3813, 0x04 },
  418. { 0x3814, 0x11 },
  419. { 0x3815, 0x11 },
  420. { 0x3820, 0x47 },
  421. { 0x4514, 0x88 },
  422. { 0x3a02, 0x04 },
  423. { 0x3a03, 0x60 },
  424. { 0x3a08, 0x01 },
  425. { 0x3a09, 0x50 },
  426. { 0x3a0a, 0x01 },
  427. { 0x3a0b, 0x18 },
  428. { 0x3a0e, 0x03 },
  429. { 0x3a0d, 0x04 },
  430. { 0x3a14, 0x04 },
  431. { 0x3a15, 0x60 },
  432. { 0x3a18, 0x00 },
  433. { 0x4004, 0x06 },
  434. { 0x4005, 0x18 },
  435. { 0x4300, 0x32 },
  436. { 0x4202, 0x00 },
  437. { 0x4837, 0x0b }
  438. };
  439. static const struct reg_value ov5645_setting_full[] = {
  440. { 0x3612, 0xab },
  441. { 0x3614, 0x50 },
  442. { 0x3618, 0x04 },
  443. { 0x3034, 0x18 },
  444. { 0x3035, 0x11 },
  445. { 0x3036, 0x54 },
  446. { 0x3600, 0x08 },
  447. { 0x3601, 0x33 },
  448. { 0x3708, 0x63 },
  449. { 0x370c, 0xc0 },
  450. { 0x3800, 0x00 },
  451. { 0x3801, 0x00 },
  452. { 0x3802, 0x00 },
  453. { 0x3803, 0x00 },
  454. { 0x3804, 0x0a },
  455. { 0x3805, 0x3f },
  456. { 0x3806, 0x07 },
  457. { 0x3807, 0x9f },
  458. { 0x3808, 0x0a },
  459. { 0x3809, 0x20 },
  460. { 0x380a, 0x07 },
  461. { 0x380b, 0x98 },
  462. { 0x380c, 0x0b },
  463. { 0x380d, 0x1c },
  464. { 0x380e, 0x07 },
  465. { 0x380f, 0xb0 },
  466. { 0x3813, 0x06 },
  467. { 0x3814, 0x11 },
  468. { 0x3815, 0x11 },
  469. { 0x3820, 0x47 },
  470. { 0x4514, 0x88 },
  471. { 0x3a02, 0x07 },
  472. { 0x3a03, 0xb0 },
  473. { 0x3a08, 0x01 },
  474. { 0x3a09, 0x27 },
  475. { 0x3a0a, 0x00 },
  476. { 0x3a0b, 0xf6 },
  477. { 0x3a0e, 0x06 },
  478. { 0x3a0d, 0x08 },
  479. { 0x3a14, 0x07 },
  480. { 0x3a15, 0xb0 },
  481. { 0x3a18, 0x01 },
  482. { 0x4004, 0x06 },
  483. { 0x4005, 0x18 },
  484. { 0x4300, 0x32 },
  485. { 0x4837, 0x0b },
  486. { 0x4202, 0x00 }
  487. };
  488. static const struct ov5645_mode_info ov5645_mode_info_data[] = {
  489. {
  490. .width = 1280,
  491. .height = 960,
  492. .data = ov5645_setting_sxga,
  493. .data_size = ARRAY_SIZE(ov5645_setting_sxga)
  494. },
  495. {
  496. .width = 1920,
  497. .height = 1080,
  498. .data = ov5645_setting_1080p,
  499. .data_size = ARRAY_SIZE(ov5645_setting_1080p)
  500. },
  501. {
  502. .width = 2592,
  503. .height = 1944,
  504. .data = ov5645_setting_full,
  505. .data_size = ARRAY_SIZE(ov5645_setting_full)
  506. },
  507. };
  508. static int ov5645_regulators_enable(struct ov5645 *ov5645)
  509. {
  510. int ret;
  511. ret = regulator_enable(ov5645->io_regulator);
  512. if (ret < 0) {
  513. dev_err(ov5645->dev, "set io voltage failed\n");
  514. return ret;
  515. }
  516. ret = regulator_enable(ov5645->analog_regulator);
  517. if (ret) {
  518. dev_err(ov5645->dev, "set analog voltage failed\n");
  519. goto err_disable_io;
  520. }
  521. ret = regulator_enable(ov5645->core_regulator);
  522. if (ret) {
  523. dev_err(ov5645->dev, "set core voltage failed\n");
  524. goto err_disable_analog;
  525. }
  526. return 0;
  527. err_disable_analog:
  528. regulator_disable(ov5645->analog_regulator);
  529. err_disable_io:
  530. regulator_disable(ov5645->io_regulator);
  531. return ret;
  532. }
  533. static void ov5645_regulators_disable(struct ov5645 *ov5645)
  534. {
  535. int ret;
  536. ret = regulator_disable(ov5645->core_regulator);
  537. if (ret < 0)
  538. dev_err(ov5645->dev, "core regulator disable failed\n");
  539. ret = regulator_disable(ov5645->analog_regulator);
  540. if (ret < 0)
  541. dev_err(ov5645->dev, "analog regulator disable failed\n");
  542. ret = regulator_disable(ov5645->io_regulator);
  543. if (ret < 0)
  544. dev_err(ov5645->dev, "io regulator disable failed\n");
  545. }
  546. static int ov5645_write_reg(struct ov5645 *ov5645, u16 reg, u8 val)
  547. {
  548. u8 regbuf[3];
  549. int ret;
  550. regbuf[0] = reg >> 8;
  551. regbuf[1] = reg & 0xff;
  552. regbuf[2] = val;
  553. ret = i2c_master_send(ov5645->i2c_client, regbuf, 3);
  554. if (ret < 0)
  555. dev_err(ov5645->dev, "%s: write reg error %d: reg=%x, val=%x\n",
  556. __func__, ret, reg, val);
  557. return ret;
  558. }
  559. static int ov5645_read_reg(struct ov5645 *ov5645, u16 reg, u8 *val)
  560. {
  561. u8 regbuf[2];
  562. int ret;
  563. regbuf[0] = reg >> 8;
  564. regbuf[1] = reg & 0xff;
  565. ret = i2c_master_send(ov5645->i2c_client, regbuf, 2);
  566. if (ret < 0) {
  567. dev_err(ov5645->dev, "%s: write reg error %d: reg=%x\n",
  568. __func__, ret, reg);
  569. return ret;
  570. }
  571. ret = i2c_master_recv(ov5645->i2c_client, val, 1);
  572. if (ret < 0) {
  573. dev_err(ov5645->dev, "%s: read reg error %d: reg=%x\n",
  574. __func__, ret, reg);
  575. return ret;
  576. }
  577. return 0;
  578. }
  579. static int ov5645_set_aec_mode(struct ov5645 *ov5645, u32 mode)
  580. {
  581. u8 val = ov5645->aec_pk_manual;
  582. int ret;
  583. if (mode == V4L2_EXPOSURE_AUTO)
  584. val &= ~OV5645_AEC_MANUAL_ENABLE;
  585. else /* V4L2_EXPOSURE_MANUAL */
  586. val |= OV5645_AEC_MANUAL_ENABLE;
  587. ret = ov5645_write_reg(ov5645, OV5645_AEC_PK_MANUAL, val);
  588. if (!ret)
  589. ov5645->aec_pk_manual = val;
  590. return ret;
  591. }
  592. static int ov5645_set_agc_mode(struct ov5645 *ov5645, u32 enable)
  593. {
  594. u8 val = ov5645->aec_pk_manual;
  595. int ret;
  596. if (enable)
  597. val &= ~OV5645_AGC_MANUAL_ENABLE;
  598. else
  599. val |= OV5645_AGC_MANUAL_ENABLE;
  600. ret = ov5645_write_reg(ov5645, OV5645_AEC_PK_MANUAL, val);
  601. if (!ret)
  602. ov5645->aec_pk_manual = val;
  603. return ret;
  604. }
  605. static int ov5645_set_register_array(struct ov5645 *ov5645,
  606. const struct reg_value *settings,
  607. unsigned int num_settings)
  608. {
  609. unsigned int i;
  610. int ret;
  611. for (i = 0; i < num_settings; ++i, ++settings) {
  612. ret = ov5645_write_reg(ov5645, settings->reg, settings->val);
  613. if (ret < 0)
  614. return ret;
  615. }
  616. return 0;
  617. }
  618. static int ov5645_set_power_on(struct ov5645 *ov5645)
  619. {
  620. int ret;
  621. ret = ov5645_regulators_enable(ov5645);
  622. if (ret < 0) {
  623. return ret;
  624. }
  625. ret = clk_prepare_enable(ov5645->xclk);
  626. if (ret < 0) {
  627. dev_err(ov5645->dev, "clk prepare enable failed\n");
  628. ov5645_regulators_disable(ov5645);
  629. return ret;
  630. }
  631. usleep_range(5000, 15000);
  632. gpiod_set_value_cansleep(ov5645->enable_gpio, 1);
  633. usleep_range(1000, 2000);
  634. gpiod_set_value_cansleep(ov5645->rst_gpio, 0);
  635. msleep(20);
  636. return 0;
  637. }
  638. static void ov5645_set_power_off(struct ov5645 *ov5645)
  639. {
  640. gpiod_set_value_cansleep(ov5645->rst_gpio, 1);
  641. gpiod_set_value_cansleep(ov5645->enable_gpio, 0);
  642. clk_disable_unprepare(ov5645->xclk);
  643. ov5645_regulators_disable(ov5645);
  644. }
  645. static int ov5645_s_power(struct v4l2_subdev *sd, int on)
  646. {
  647. struct ov5645 *ov5645 = to_ov5645(sd);
  648. int ret = 0;
  649. mutex_lock(&ov5645->power_lock);
  650. /* If the power count is modified from 0 to != 0 or from != 0 to 0,
  651. * update the power state.
  652. */
  653. if (ov5645->power_count == !on) {
  654. if (on) {
  655. ret = ov5645_set_power_on(ov5645);
  656. if (ret < 0)
  657. goto exit;
  658. ret = ov5645_set_register_array(ov5645,
  659. ov5645_global_init_setting,
  660. ARRAY_SIZE(ov5645_global_init_setting));
  661. if (ret < 0) {
  662. dev_err(ov5645->dev,
  663. "could not set init registers\n");
  664. ov5645_set_power_off(ov5645);
  665. goto exit;
  666. }
  667. ret = ov5645_write_reg(ov5645, OV5645_SYSTEM_CTRL0,
  668. OV5645_SYSTEM_CTRL0_STOP);
  669. if (ret < 0) {
  670. ov5645_set_power_off(ov5645);
  671. goto exit;
  672. }
  673. } else {
  674. ov5645_set_power_off(ov5645);
  675. }
  676. }
  677. /* Update the power count. */
  678. ov5645->power_count += on ? 1 : -1;
  679. WARN_ON(ov5645->power_count < 0);
  680. exit:
  681. mutex_unlock(&ov5645->power_lock);
  682. return ret;
  683. }
  684. static int ov5645_set_saturation(struct ov5645 *ov5645, s32 value)
  685. {
  686. u32 reg_value = (value * 0x10) + 0x40;
  687. int ret;
  688. ret = ov5645_write_reg(ov5645, OV5645_SDE_SAT_U, reg_value);
  689. if (ret < 0)
  690. return ret;
  691. return ov5645_write_reg(ov5645, OV5645_SDE_SAT_V, reg_value);
  692. }
  693. static int ov5645_set_hflip(struct ov5645 *ov5645, s32 value)
  694. {
  695. u8 val = ov5645->timing_tc_reg21;
  696. int ret;
  697. if (value == 0)
  698. val &= ~(OV5645_SENSOR_MIRROR);
  699. else
  700. val |= (OV5645_SENSOR_MIRROR);
  701. ret = ov5645_write_reg(ov5645, OV5645_TIMING_TC_REG21, val);
  702. if (!ret)
  703. ov5645->timing_tc_reg21 = val;
  704. return ret;
  705. }
  706. static int ov5645_set_vflip(struct ov5645 *ov5645, s32 value)
  707. {
  708. u8 val = ov5645->timing_tc_reg20;
  709. int ret;
  710. if (value == 0)
  711. val |= (OV5645_SENSOR_VFLIP | OV5645_ISP_VFLIP);
  712. else
  713. val &= ~(OV5645_SENSOR_VFLIP | OV5645_ISP_VFLIP);
  714. ret = ov5645_write_reg(ov5645, OV5645_TIMING_TC_REG20, val);
  715. if (!ret)
  716. ov5645->timing_tc_reg20 = val;
  717. return ret;
  718. }
  719. static int ov5645_set_test_pattern(struct ov5645 *ov5645, s32 value)
  720. {
  721. u8 val = 0;
  722. if (value) {
  723. val = OV5645_SET_TEST_PATTERN(value - 1);
  724. val |= OV5645_TEST_PATTERN_ENABLE;
  725. }
  726. return ov5645_write_reg(ov5645, OV5645_PRE_ISP_TEST_SETTING_1, val);
  727. }
  728. static const char * const ov5645_test_pattern_menu[] = {
  729. "Disabled",
  730. "Vertical Color Bars",
  731. "Pseudo-Random Data",
  732. "Color Square",
  733. "Black Image",
  734. };
  735. static int ov5645_set_awb(struct ov5645 *ov5645, s32 enable_auto)
  736. {
  737. u8 val = 0;
  738. if (!enable_auto)
  739. val = OV5645_AWB_MANUAL_ENABLE;
  740. return ov5645_write_reg(ov5645, OV5645_AWB_MANUAL_CONTROL, val);
  741. }
  742. static int ov5645_s_ctrl(struct v4l2_ctrl *ctrl)
  743. {
  744. struct ov5645 *ov5645 = container_of(ctrl->handler,
  745. struct ov5645, ctrls);
  746. int ret;
  747. mutex_lock(&ov5645->power_lock);
  748. if (!ov5645->power_count) {
  749. mutex_unlock(&ov5645->power_lock);
  750. return 0;
  751. }
  752. switch (ctrl->id) {
  753. case V4L2_CID_SATURATION:
  754. ret = ov5645_set_saturation(ov5645, ctrl->val);
  755. break;
  756. case V4L2_CID_AUTO_WHITE_BALANCE:
  757. ret = ov5645_set_awb(ov5645, ctrl->val);
  758. break;
  759. case V4L2_CID_AUTOGAIN:
  760. ret = ov5645_set_agc_mode(ov5645, ctrl->val);
  761. break;
  762. case V4L2_CID_EXPOSURE_AUTO:
  763. ret = ov5645_set_aec_mode(ov5645, ctrl->val);
  764. break;
  765. case V4L2_CID_TEST_PATTERN:
  766. ret = ov5645_set_test_pattern(ov5645, ctrl->val);
  767. break;
  768. case V4L2_CID_HFLIP:
  769. ret = ov5645_set_hflip(ov5645, ctrl->val);
  770. break;
  771. case V4L2_CID_VFLIP:
  772. ret = ov5645_set_vflip(ov5645, ctrl->val);
  773. break;
  774. default:
  775. ret = -EINVAL;
  776. break;
  777. }
  778. mutex_unlock(&ov5645->power_lock);
  779. return ret;
  780. }
  781. static struct v4l2_ctrl_ops ov5645_ctrl_ops = {
  782. .s_ctrl = ov5645_s_ctrl,
  783. };
  784. static int ov5645_enum_mbus_code(struct v4l2_subdev *sd,
  785. struct v4l2_subdev_pad_config *cfg,
  786. struct v4l2_subdev_mbus_code_enum *code)
  787. {
  788. if (code->index > 0)
  789. return -EINVAL;
  790. code->code = MEDIA_BUS_FMT_UYVY8_2X8;
  791. return 0;
  792. }
  793. static int ov5645_enum_frame_size(struct v4l2_subdev *subdev,
  794. struct v4l2_subdev_pad_config *cfg,
  795. struct v4l2_subdev_frame_size_enum *fse)
  796. {
  797. if (fse->code != MEDIA_BUS_FMT_UYVY8_2X8)
  798. return -EINVAL;
  799. if (fse->index >= ARRAY_SIZE(ov5645_mode_info_data))
  800. return -EINVAL;
  801. fse->min_width = ov5645_mode_info_data[fse->index].width;
  802. fse->max_width = ov5645_mode_info_data[fse->index].width;
  803. fse->min_height = ov5645_mode_info_data[fse->index].height;
  804. fse->max_height = ov5645_mode_info_data[fse->index].height;
  805. return 0;
  806. }
  807. static struct v4l2_mbus_framefmt *
  808. __ov5645_get_pad_format(struct ov5645 *ov5645,
  809. struct v4l2_subdev_pad_config *cfg,
  810. unsigned int pad,
  811. enum v4l2_subdev_format_whence which)
  812. {
  813. switch (which) {
  814. case V4L2_SUBDEV_FORMAT_TRY:
  815. return v4l2_subdev_get_try_format(&ov5645->sd, cfg, pad);
  816. case V4L2_SUBDEV_FORMAT_ACTIVE:
  817. return &ov5645->fmt;
  818. default:
  819. return NULL;
  820. }
  821. }
  822. static int ov5645_get_format(struct v4l2_subdev *sd,
  823. struct v4l2_subdev_pad_config *cfg,
  824. struct v4l2_subdev_format *format)
  825. {
  826. struct ov5645 *ov5645 = to_ov5645(sd);
  827. format->format = *__ov5645_get_pad_format(ov5645, cfg, format->pad,
  828. format->which);
  829. return 0;
  830. }
  831. static struct v4l2_rect *
  832. __ov5645_get_pad_crop(struct ov5645 *ov5645, struct v4l2_subdev_pad_config *cfg,
  833. unsigned int pad, enum v4l2_subdev_format_whence which)
  834. {
  835. switch (which) {
  836. case V4L2_SUBDEV_FORMAT_TRY:
  837. return v4l2_subdev_get_try_crop(&ov5645->sd, cfg, pad);
  838. case V4L2_SUBDEV_FORMAT_ACTIVE:
  839. return &ov5645->crop;
  840. default:
  841. return NULL;
  842. }
  843. }
  844. static const struct ov5645_mode_info *
  845. ov5645_find_nearest_mode(unsigned int width, unsigned int height)
  846. {
  847. int i;
  848. for (i = ARRAY_SIZE(ov5645_mode_info_data) - 1; i >= 0; i--) {
  849. if (ov5645_mode_info_data[i].width <= width &&
  850. ov5645_mode_info_data[i].height <= height)
  851. break;
  852. }
  853. if (i < 0)
  854. i = 0;
  855. return &ov5645_mode_info_data[i];
  856. }
  857. static int ov5645_set_format(struct v4l2_subdev *sd,
  858. struct v4l2_subdev_pad_config *cfg,
  859. struct v4l2_subdev_format *format)
  860. {
  861. struct ov5645 *ov5645 = to_ov5645(sd);
  862. struct v4l2_mbus_framefmt *__format;
  863. struct v4l2_rect *__crop;
  864. const struct ov5645_mode_info *new_mode;
  865. __crop = __ov5645_get_pad_crop(ov5645, cfg, format->pad,
  866. format->which);
  867. new_mode = ov5645_find_nearest_mode(format->format.width,
  868. format->format.height);
  869. __crop->width = new_mode->width;
  870. __crop->height = new_mode->height;
  871. if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE)
  872. ov5645->current_mode = new_mode;
  873. __format = __ov5645_get_pad_format(ov5645, cfg, format->pad,
  874. format->which);
  875. __format->width = __crop->width;
  876. __format->height = __crop->height;
  877. __format->code = MEDIA_BUS_FMT_UYVY8_2X8;
  878. __format->field = V4L2_FIELD_NONE;
  879. __format->colorspace = V4L2_COLORSPACE_SRGB;
  880. format->format = *__format;
  881. return 0;
  882. }
  883. static int ov5645_entity_init_cfg(struct v4l2_subdev *subdev,
  884. struct v4l2_subdev_pad_config *cfg)
  885. {
  886. struct v4l2_subdev_format fmt = { 0 };
  887. fmt.which = cfg ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
  888. fmt.format.width = 1920;
  889. fmt.format.height = 1080;
  890. ov5645_set_format(subdev, cfg, &fmt);
  891. return 0;
  892. }
  893. static int ov5645_get_selection(struct v4l2_subdev *sd,
  894. struct v4l2_subdev_pad_config *cfg,
  895. struct v4l2_subdev_selection *sel)
  896. {
  897. struct ov5645 *ov5645 = to_ov5645(sd);
  898. if (sel->target != V4L2_SEL_TGT_CROP)
  899. return -EINVAL;
  900. sel->r = *__ov5645_get_pad_crop(ov5645, cfg, sel->pad,
  901. sel->which);
  902. return 0;
  903. }
  904. static int ov5645_s_stream(struct v4l2_subdev *subdev, int enable)
  905. {
  906. struct ov5645 *ov5645 = to_ov5645(subdev);
  907. int ret;
  908. if (enable) {
  909. ret = ov5645_set_register_array(ov5645,
  910. ov5645->current_mode->data,
  911. ov5645->current_mode->data_size);
  912. if (ret < 0) {
  913. dev_err(ov5645->dev, "could not set mode %dx%d\n",
  914. ov5645->current_mode->width,
  915. ov5645->current_mode->height);
  916. return ret;
  917. }
  918. ret = v4l2_ctrl_handler_setup(&ov5645->ctrls);
  919. if (ret < 0) {
  920. dev_err(ov5645->dev, "could not sync v4l2 controls\n");
  921. return ret;
  922. }
  923. ret = ov5645_write_reg(ov5645, OV5645_SYSTEM_CTRL0,
  924. OV5645_SYSTEM_CTRL0_START);
  925. if (ret < 0)
  926. return ret;
  927. } else {
  928. ret = ov5645_write_reg(ov5645, OV5645_SYSTEM_CTRL0,
  929. OV5645_SYSTEM_CTRL0_STOP);
  930. if (ret < 0)
  931. return ret;
  932. }
  933. return 0;
  934. }
  935. static const struct v4l2_subdev_core_ops ov5645_core_ops = {
  936. .s_power = ov5645_s_power,
  937. };
  938. static const struct v4l2_subdev_video_ops ov5645_video_ops = {
  939. .s_stream = ov5645_s_stream,
  940. };
  941. static const struct v4l2_subdev_pad_ops ov5645_subdev_pad_ops = {
  942. .init_cfg = ov5645_entity_init_cfg,
  943. .enum_mbus_code = ov5645_enum_mbus_code,
  944. .enum_frame_size = ov5645_enum_frame_size,
  945. .get_fmt = ov5645_get_format,
  946. .set_fmt = ov5645_set_format,
  947. .get_selection = ov5645_get_selection,
  948. };
  949. static const struct v4l2_subdev_ops ov5645_subdev_ops = {
  950. .core = &ov5645_core_ops,
  951. .video = &ov5645_video_ops,
  952. .pad = &ov5645_subdev_pad_ops,
  953. };
  954. static int ov5645_probe(struct i2c_client *client,
  955. const struct i2c_device_id *id)
  956. {
  957. struct device *dev = &client->dev;
  958. struct device_node *endpoint;
  959. struct ov5645 *ov5645;
  960. u8 chip_id_high, chip_id_low;
  961. u32 xclk_freq;
  962. int ret;
  963. ov5645 = devm_kzalloc(dev, sizeof(struct ov5645), GFP_KERNEL);
  964. if (!ov5645)
  965. return -ENOMEM;
  966. ov5645->i2c_client = client;
  967. ov5645->dev = dev;
  968. endpoint = of_graph_get_next_endpoint(dev->of_node, NULL);
  969. if (!endpoint) {
  970. dev_err(dev, "endpoint node not found\n");
  971. return -EINVAL;
  972. }
  973. ret = v4l2_of_parse_endpoint(endpoint, &ov5645->ep);
  974. if (ret < 0) {
  975. dev_err(dev, "parsing endpoint node failed\n");
  976. return ret;
  977. }
  978. of_node_put(endpoint);
  979. if (ov5645->ep.bus_type != V4L2_MBUS_CSI2) {
  980. dev_err(dev, "invalid bus type, must be CSI2\n");
  981. return -EINVAL;
  982. }
  983. /* get system clock (xclk) */
  984. ov5645->xclk = devm_clk_get(dev, "xclk");
  985. if (IS_ERR(ov5645->xclk)) {
  986. dev_err(dev, "could not get xclk");
  987. return PTR_ERR(ov5645->xclk);
  988. }
  989. ret = of_property_read_u32(dev->of_node, "clock-frequency", &xclk_freq);
  990. if (ret) {
  991. dev_err(dev, "could not get xclk frequency\n");
  992. return ret;
  993. }
  994. if (xclk_freq != 23880000) {
  995. dev_err(dev, "external clock frequency %u is not supported\n",
  996. xclk_freq);
  997. return -EINVAL;
  998. }
  999. ret = clk_set_rate(ov5645->xclk, xclk_freq);
  1000. if (ret) {
  1001. dev_err(dev, "could not set xclk frequency\n");
  1002. return ret;
  1003. }
  1004. ov5645->io_regulator = devm_regulator_get(dev, "vdddo");
  1005. if (IS_ERR(ov5645->io_regulator)) {
  1006. dev_err(dev, "cannot get io regulator\n");
  1007. return PTR_ERR(ov5645->io_regulator);
  1008. }
  1009. ret = regulator_set_voltage(ov5645->io_regulator,
  1010. OV5645_VOLTAGE_DIGITAL_IO,
  1011. OV5645_VOLTAGE_DIGITAL_IO);
  1012. if (ret < 0) {
  1013. dev_err(dev, "cannot set io voltage\n");
  1014. return ret;
  1015. }
  1016. ov5645->core_regulator = devm_regulator_get(dev, "vddd");
  1017. if (IS_ERR(ov5645->core_regulator)) {
  1018. dev_err(dev, "cannot get core regulator\n");
  1019. return PTR_ERR(ov5645->core_regulator);
  1020. }
  1021. ret = regulator_set_voltage(ov5645->core_regulator,
  1022. OV5645_VOLTAGE_DIGITAL_CORE,
  1023. OV5645_VOLTAGE_DIGITAL_CORE);
  1024. if (ret < 0) {
  1025. dev_err(dev, "cannot set core voltage\n");
  1026. return ret;
  1027. }
  1028. ov5645->analog_regulator = devm_regulator_get(dev, "vdda");
  1029. if (IS_ERR(ov5645->analog_regulator)) {
  1030. dev_err(dev, "cannot get analog regulator\n");
  1031. return PTR_ERR(ov5645->analog_regulator);
  1032. }
  1033. ret = regulator_set_voltage(ov5645->analog_regulator,
  1034. OV5645_VOLTAGE_ANALOG,
  1035. OV5645_VOLTAGE_ANALOG);
  1036. if (ret < 0) {
  1037. dev_err(dev, "cannot set analog voltage\n");
  1038. return ret;
  1039. }
  1040. ov5645->enable_gpio = devm_gpiod_get(dev, "enable", GPIOD_OUT_HIGH);
  1041. if (IS_ERR(ov5645->enable_gpio)) {
  1042. dev_err(dev, "cannot get enable gpio\n");
  1043. return PTR_ERR(ov5645->enable_gpio);
  1044. }
  1045. ov5645->rst_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
  1046. if (IS_ERR(ov5645->rst_gpio)) {
  1047. dev_err(dev, "cannot get reset gpio\n");
  1048. return PTR_ERR(ov5645->rst_gpio);
  1049. }
  1050. mutex_init(&ov5645->power_lock);
  1051. v4l2_ctrl_handler_init(&ov5645->ctrls, 7);
  1052. v4l2_ctrl_new_std(&ov5645->ctrls, &ov5645_ctrl_ops,
  1053. V4L2_CID_SATURATION, -4, 4, 1, 0);
  1054. v4l2_ctrl_new_std(&ov5645->ctrls, &ov5645_ctrl_ops,
  1055. V4L2_CID_HFLIP, 0, 1, 1, 0);
  1056. v4l2_ctrl_new_std(&ov5645->ctrls, &ov5645_ctrl_ops,
  1057. V4L2_CID_VFLIP, 0, 1, 1, 0);
  1058. v4l2_ctrl_new_std(&ov5645->ctrls, &ov5645_ctrl_ops,
  1059. V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
  1060. v4l2_ctrl_new_std(&ov5645->ctrls, &ov5645_ctrl_ops,
  1061. V4L2_CID_AUTO_WHITE_BALANCE, 0, 1, 1, 1);
  1062. v4l2_ctrl_new_std_menu(&ov5645->ctrls, &ov5645_ctrl_ops,
  1063. V4L2_CID_EXPOSURE_AUTO, V4L2_EXPOSURE_MANUAL,
  1064. 0, V4L2_EXPOSURE_AUTO);
  1065. v4l2_ctrl_new_std_menu_items(&ov5645->ctrls, &ov5645_ctrl_ops,
  1066. V4L2_CID_TEST_PATTERN,
  1067. ARRAY_SIZE(ov5645_test_pattern_menu) - 1,
  1068. 0, 0, ov5645_test_pattern_menu);
  1069. ov5645->sd.ctrl_handler = &ov5645->ctrls;
  1070. if (ov5645->ctrls.error) {
  1071. dev_err(dev, "%s: control initialization error %d\n",
  1072. __func__, ov5645->ctrls.error);
  1073. ret = ov5645->ctrls.error;
  1074. goto free_ctrl;
  1075. }
  1076. v4l2_i2c_subdev_init(&ov5645->sd, client, &ov5645_subdev_ops);
  1077. ov5645->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  1078. ov5645->pad.flags = MEDIA_PAD_FL_SOURCE;
  1079. ov5645->sd.dev = &client->dev;
  1080. ret = media_entity_pads_init(&ov5645->sd.entity, 1, &ov5645->pad);
  1081. if (ret < 0) {
  1082. dev_err(dev, "could not register media entity\n");
  1083. goto free_ctrl;
  1084. }
  1085. ret = ov5645_s_power(&ov5645->sd, true);
  1086. if (ret < 0) {
  1087. dev_err(dev, "could not power up OV5645\n");
  1088. goto free_entity;
  1089. }
  1090. ret = ov5645_read_reg(ov5645, OV5645_CHIP_ID_HIGH, &chip_id_high);
  1091. if (ret < 0 || chip_id_high != OV5645_CHIP_ID_HIGH_BYTE) {
  1092. dev_err(dev, "could not read ID high\n");
  1093. ret = -ENODEV;
  1094. goto power_down;
  1095. }
  1096. ret = ov5645_read_reg(ov5645, OV5645_CHIP_ID_LOW, &chip_id_low);
  1097. if (ret < 0 || chip_id_low != OV5645_CHIP_ID_LOW_BYTE) {
  1098. dev_err(dev, "could not read ID low\n");
  1099. ret = -ENODEV;
  1100. goto power_down;
  1101. }
  1102. dev_info(dev, "OV5645 detected at address 0x%02x\n", client->addr);
  1103. ret = ov5645_read_reg(ov5645, OV5645_AEC_PK_MANUAL,
  1104. &ov5645->aec_pk_manual);
  1105. if (ret < 0) {
  1106. dev_err(dev, "could not read AEC/AGC mode\n");
  1107. ret = -ENODEV;
  1108. goto power_down;
  1109. }
  1110. ret = ov5645_read_reg(ov5645, OV5645_TIMING_TC_REG20,
  1111. &ov5645->timing_tc_reg20);
  1112. if (ret < 0) {
  1113. dev_err(dev, "could not read vflip value\n");
  1114. ret = -ENODEV;
  1115. goto power_down;
  1116. }
  1117. ret = ov5645_read_reg(ov5645, OV5645_TIMING_TC_REG21,
  1118. &ov5645->timing_tc_reg21);
  1119. if (ret < 0) {
  1120. dev_err(dev, "could not read hflip value\n");
  1121. ret = -ENODEV;
  1122. goto power_down;
  1123. }
  1124. ov5645_s_power(&ov5645->sd, false);
  1125. ret = v4l2_async_register_subdev(&ov5645->sd);
  1126. if (ret < 0) {
  1127. dev_err(dev, "could not register v4l2 device\n");
  1128. goto free_entity;
  1129. }
  1130. ov5645_entity_init_cfg(&ov5645->sd, NULL);
  1131. return 0;
  1132. power_down:
  1133. ov5645_s_power(&ov5645->sd, false);
  1134. free_entity:
  1135. media_entity_cleanup(&ov5645->sd.entity);
  1136. free_ctrl:
  1137. v4l2_ctrl_handler_free(&ov5645->ctrls);
  1138. mutex_destroy(&ov5645->power_lock);
  1139. return ret;
  1140. }
  1141. static int ov5645_remove(struct i2c_client *client)
  1142. {
  1143. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1144. struct ov5645 *ov5645 = to_ov5645(sd);
  1145. v4l2_async_unregister_subdev(&ov5645->sd);
  1146. media_entity_cleanup(&ov5645->sd.entity);
  1147. v4l2_ctrl_handler_free(&ov5645->ctrls);
  1148. mutex_destroy(&ov5645->power_lock);
  1149. return 0;
  1150. }
  1151. static const struct i2c_device_id ov5645_id[] = {
  1152. { "ov5645", 0 },
  1153. {}
  1154. };
  1155. MODULE_DEVICE_TABLE(i2c, ov5645_id);
  1156. static const struct of_device_id ov5645_of_match[] = {
  1157. { .compatible = "ovti,ov5645" },
  1158. { /* sentinel */ }
  1159. };
  1160. MODULE_DEVICE_TABLE(of, ov5645_of_match);
  1161. static struct i2c_driver ov5645_i2c_driver = {
  1162. .driver = {
  1163. .of_match_table = of_match_ptr(ov5645_of_match),
  1164. .name = "ov5645",
  1165. },
  1166. .probe = ov5645_probe,
  1167. .remove = ov5645_remove,
  1168. .id_table = ov5645_id,
  1169. };
  1170. module_i2c_driver(ov5645_i2c_driver);
  1171. MODULE_DESCRIPTION("Omnivision OV5645 Camera Driver");
  1172. MODULE_AUTHOR("Todor Tomov <todor.tomov@linaro.org>");
  1173. MODULE_LICENSE("GPL v2");