ov7251.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver for the OV7251 camera sensor.
  4. *
  5. * Copyright (c) 2017-2018, The Linux Foundation. All rights reserved.
  6. * Copyright (c) 2017-2018, Linaro Ltd.
  7. */
  8. #include <linux/bitops.h>
  9. #include <linux/clk.h>
  10. #include <linux/delay.h>
  11. #include <linux/device.h>
  12. #include <linux/gpio/consumer.h>
  13. #include <linux/i2c.h>
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/regulator/consumer.h>
  17. #include <linux/slab.h>
  18. #include <linux/types.h>
  19. #include <media/v4l2-ctrls.h>
  20. #include <media/v4l2-fwnode.h>
  21. #include <media/v4l2-subdev.h>
  22. #define OV7251_SC_MODE_SELECT 0x0100
  23. #define OV7251_SC_MODE_SELECT_SW_STANDBY 0x0
  24. #define OV7251_SC_MODE_SELECT_STREAMING 0x1
  25. #define OV7251_CHIP_ID_HIGH 0x300a
  26. #define OV7251_CHIP_ID_HIGH_BYTE 0x77
  27. #define OV7251_CHIP_ID_LOW 0x300b
  28. #define OV7251_CHIP_ID_LOW_BYTE 0x50
  29. #define OV7251_SC_GP_IO_IN1 0x3029
  30. #define OV7251_AEC_EXPO_0 0x3500
  31. #define OV7251_AEC_EXPO_1 0x3501
  32. #define OV7251_AEC_EXPO_2 0x3502
  33. #define OV7251_AEC_AGC_ADJ_0 0x350a
  34. #define OV7251_AEC_AGC_ADJ_1 0x350b
  35. #define OV7251_TIMING_FORMAT1 0x3820
  36. #define OV7251_TIMING_FORMAT1_VFLIP BIT(2)
  37. #define OV7251_TIMING_FORMAT2 0x3821
  38. #define OV7251_TIMING_FORMAT2_MIRROR BIT(2)
  39. #define OV7251_PRE_ISP_00 0x5e00
  40. #define OV7251_PRE_ISP_00_TEST_PATTERN BIT(7)
  41. struct reg_value {
  42. u16 reg;
  43. u8 val;
  44. };
  45. struct ov7251_mode_info {
  46. u32 width;
  47. u32 height;
  48. const struct reg_value *data;
  49. u32 data_size;
  50. u32 pixel_clock;
  51. u32 link_freq;
  52. u16 exposure_max;
  53. u16 exposure_def;
  54. struct v4l2_fract timeperframe;
  55. };
  56. struct ov7251 {
  57. struct i2c_client *i2c_client;
  58. struct device *dev;
  59. struct v4l2_subdev sd;
  60. struct media_pad pad;
  61. struct v4l2_fwnode_endpoint ep;
  62. struct v4l2_mbus_framefmt fmt;
  63. struct v4l2_rect crop;
  64. struct clk *xclk;
  65. u32 xclk_freq;
  66. struct regulator *io_regulator;
  67. struct regulator *core_regulator;
  68. struct regulator *analog_regulator;
  69. const struct ov7251_mode_info *current_mode;
  70. struct v4l2_ctrl_handler ctrls;
  71. struct v4l2_ctrl *pixel_clock;
  72. struct v4l2_ctrl *link_freq;
  73. struct v4l2_ctrl *exposure;
  74. struct v4l2_ctrl *gain;
  75. /* Cached register values */
  76. u8 aec_pk_manual;
  77. u8 pre_isp_00;
  78. u8 timing_format1;
  79. u8 timing_format2;
  80. struct mutex lock; /* lock to protect power state, ctrls and mode */
  81. bool power_on;
  82. struct gpio_desc *enable_gpio;
  83. };
  84. static inline struct ov7251 *to_ov7251(struct v4l2_subdev *sd)
  85. {
  86. return container_of(sd, struct ov7251, sd);
  87. }
  88. static const struct reg_value ov7251_global_init_setting[] = {
  89. { 0x0103, 0x01 },
  90. { 0x303b, 0x02 },
  91. };
  92. static const struct reg_value ov7251_setting_vga_30fps[] = {
  93. { 0x3005, 0x00 },
  94. { 0x3012, 0xc0 },
  95. { 0x3013, 0xd2 },
  96. { 0x3014, 0x04 },
  97. { 0x3016, 0xf0 },
  98. { 0x3017, 0xf0 },
  99. { 0x3018, 0xf0 },
  100. { 0x301a, 0xf0 },
  101. { 0x301b, 0xf0 },
  102. { 0x301c, 0xf0 },
  103. { 0x3023, 0x05 },
  104. { 0x3037, 0xf0 },
  105. { 0x3098, 0x04 }, /* pll2 pre divider */
  106. { 0x3099, 0x28 }, /* pll2 multiplier */
  107. { 0x309a, 0x05 }, /* pll2 sys divider */
  108. { 0x309b, 0x04 }, /* pll2 adc divider */
  109. { 0x309d, 0x00 }, /* pll2 divider */
  110. { 0x30b0, 0x0a }, /* pll1 pix divider */
  111. { 0x30b1, 0x01 }, /* pll1 divider */
  112. { 0x30b3, 0x64 }, /* pll1 multiplier */
  113. { 0x30b4, 0x03 }, /* pll1 pre divider */
  114. { 0x30b5, 0x05 }, /* pll1 mipi divider */
  115. { 0x3106, 0xda },
  116. { 0x3503, 0x07 },
  117. { 0x3509, 0x10 },
  118. { 0x3600, 0x1c },
  119. { 0x3602, 0x62 },
  120. { 0x3620, 0xb7 },
  121. { 0x3622, 0x04 },
  122. { 0x3626, 0x21 },
  123. { 0x3627, 0x30 },
  124. { 0x3630, 0x44 },
  125. { 0x3631, 0x35 },
  126. { 0x3634, 0x60 },
  127. { 0x3636, 0x00 },
  128. { 0x3662, 0x01 },
  129. { 0x3663, 0x70 },
  130. { 0x3664, 0x50 },
  131. { 0x3666, 0x0a },
  132. { 0x3669, 0x1a },
  133. { 0x366a, 0x00 },
  134. { 0x366b, 0x50 },
  135. { 0x3673, 0x01 },
  136. { 0x3674, 0xff },
  137. { 0x3675, 0x03 },
  138. { 0x3705, 0xc1 },
  139. { 0x3709, 0x40 },
  140. { 0x373c, 0x08 },
  141. { 0x3742, 0x00 },
  142. { 0x3757, 0xb3 },
  143. { 0x3788, 0x00 },
  144. { 0x37a8, 0x01 },
  145. { 0x37a9, 0xc0 },
  146. { 0x3800, 0x00 },
  147. { 0x3801, 0x04 },
  148. { 0x3802, 0x00 },
  149. { 0x3803, 0x04 },
  150. { 0x3804, 0x02 },
  151. { 0x3805, 0x8b },
  152. { 0x3806, 0x01 },
  153. { 0x3807, 0xeb },
  154. { 0x3808, 0x02 }, /* width high */
  155. { 0x3809, 0x80 }, /* width low */
  156. { 0x380a, 0x01 }, /* height high */
  157. { 0x380b, 0xe0 }, /* height low */
  158. { 0x380c, 0x03 }, /* total horiz timing high */
  159. { 0x380d, 0xa0 }, /* total horiz timing low */
  160. { 0x380e, 0x06 }, /* total vertical timing high */
  161. { 0x380f, 0xbc }, /* total vertical timing low */
  162. { 0x3810, 0x00 },
  163. { 0x3811, 0x04 },
  164. { 0x3812, 0x00 },
  165. { 0x3813, 0x05 },
  166. { 0x3814, 0x11 },
  167. { 0x3815, 0x11 },
  168. { 0x3820, 0x40 },
  169. { 0x3821, 0x00 },
  170. { 0x382f, 0x0e },
  171. { 0x3832, 0x00 },
  172. { 0x3833, 0x05 },
  173. { 0x3834, 0x00 },
  174. { 0x3835, 0x0c },
  175. { 0x3837, 0x00 },
  176. { 0x3b80, 0x00 },
  177. { 0x3b81, 0xa5 },
  178. { 0x3b82, 0x10 },
  179. { 0x3b83, 0x00 },
  180. { 0x3b84, 0x08 },
  181. { 0x3b85, 0x00 },
  182. { 0x3b86, 0x01 },
  183. { 0x3b87, 0x00 },
  184. { 0x3b88, 0x00 },
  185. { 0x3b89, 0x00 },
  186. { 0x3b8a, 0x00 },
  187. { 0x3b8b, 0x05 },
  188. { 0x3b8c, 0x00 },
  189. { 0x3b8d, 0x00 },
  190. { 0x3b8e, 0x00 },
  191. { 0x3b8f, 0x1a },
  192. { 0x3b94, 0x05 },
  193. { 0x3b95, 0xf2 },
  194. { 0x3b96, 0x40 },
  195. { 0x3c00, 0x89 },
  196. { 0x3c01, 0x63 },
  197. { 0x3c02, 0x01 },
  198. { 0x3c03, 0x00 },
  199. { 0x3c04, 0x00 },
  200. { 0x3c05, 0x03 },
  201. { 0x3c06, 0x00 },
  202. { 0x3c07, 0x06 },
  203. { 0x3c0c, 0x01 },
  204. { 0x3c0d, 0xd0 },
  205. { 0x3c0e, 0x02 },
  206. { 0x3c0f, 0x0a },
  207. { 0x4001, 0x42 },
  208. { 0x4004, 0x04 },
  209. { 0x4005, 0x00 },
  210. { 0x404e, 0x01 },
  211. { 0x4300, 0xff },
  212. { 0x4301, 0x00 },
  213. { 0x4315, 0x00 },
  214. { 0x4501, 0x48 },
  215. { 0x4600, 0x00 },
  216. { 0x4601, 0x4e },
  217. { 0x4801, 0x0f },
  218. { 0x4806, 0x0f },
  219. { 0x4819, 0xaa },
  220. { 0x4823, 0x3e },
  221. { 0x4837, 0x19 },
  222. { 0x4a0d, 0x00 },
  223. { 0x4a47, 0x7f },
  224. { 0x4a49, 0xf0 },
  225. { 0x4a4b, 0x30 },
  226. { 0x5000, 0x85 },
  227. { 0x5001, 0x80 },
  228. };
  229. static const struct reg_value ov7251_setting_vga_60fps[] = {
  230. { 0x3005, 0x00 },
  231. { 0x3012, 0xc0 },
  232. { 0x3013, 0xd2 },
  233. { 0x3014, 0x04 },
  234. { 0x3016, 0x10 },
  235. { 0x3017, 0x00 },
  236. { 0x3018, 0x00 },
  237. { 0x301a, 0x00 },
  238. { 0x301b, 0x00 },
  239. { 0x301c, 0x00 },
  240. { 0x3023, 0x05 },
  241. { 0x3037, 0xf0 },
  242. { 0x3098, 0x04 }, /* pll2 pre divider */
  243. { 0x3099, 0x28 }, /* pll2 multiplier */
  244. { 0x309a, 0x05 }, /* pll2 sys divider */
  245. { 0x309b, 0x04 }, /* pll2 adc divider */
  246. { 0x309d, 0x00 }, /* pll2 divider */
  247. { 0x30b0, 0x0a }, /* pll1 pix divider */
  248. { 0x30b1, 0x01 }, /* pll1 divider */
  249. { 0x30b3, 0x64 }, /* pll1 multiplier */
  250. { 0x30b4, 0x03 }, /* pll1 pre divider */
  251. { 0x30b5, 0x05 }, /* pll1 mipi divider */
  252. { 0x3106, 0xda },
  253. { 0x3503, 0x07 },
  254. { 0x3509, 0x10 },
  255. { 0x3600, 0x1c },
  256. { 0x3602, 0x62 },
  257. { 0x3620, 0xb7 },
  258. { 0x3622, 0x04 },
  259. { 0x3626, 0x21 },
  260. { 0x3627, 0x30 },
  261. { 0x3630, 0x44 },
  262. { 0x3631, 0x35 },
  263. { 0x3634, 0x60 },
  264. { 0x3636, 0x00 },
  265. { 0x3662, 0x01 },
  266. { 0x3663, 0x70 },
  267. { 0x3664, 0x50 },
  268. { 0x3666, 0x0a },
  269. { 0x3669, 0x1a },
  270. { 0x366a, 0x00 },
  271. { 0x366b, 0x50 },
  272. { 0x3673, 0x01 },
  273. { 0x3674, 0xff },
  274. { 0x3675, 0x03 },
  275. { 0x3705, 0xc1 },
  276. { 0x3709, 0x40 },
  277. { 0x373c, 0x08 },
  278. { 0x3742, 0x00 },
  279. { 0x3757, 0xb3 },
  280. { 0x3788, 0x00 },
  281. { 0x37a8, 0x01 },
  282. { 0x37a9, 0xc0 },
  283. { 0x3800, 0x00 },
  284. { 0x3801, 0x04 },
  285. { 0x3802, 0x00 },
  286. { 0x3803, 0x04 },
  287. { 0x3804, 0x02 },
  288. { 0x3805, 0x8b },
  289. { 0x3806, 0x01 },
  290. { 0x3807, 0xeb },
  291. { 0x3808, 0x02 }, /* width high */
  292. { 0x3809, 0x80 }, /* width low */
  293. { 0x380a, 0x01 }, /* height high */
  294. { 0x380b, 0xe0 }, /* height low */
  295. { 0x380c, 0x03 }, /* total horiz timing high */
  296. { 0x380d, 0xa0 }, /* total horiz timing low */
  297. { 0x380e, 0x03 }, /* total vertical timing high */
  298. { 0x380f, 0x5c }, /* total vertical timing low */
  299. { 0x3810, 0x00 },
  300. { 0x3811, 0x04 },
  301. { 0x3812, 0x00 },
  302. { 0x3813, 0x05 },
  303. { 0x3814, 0x11 },
  304. { 0x3815, 0x11 },
  305. { 0x3820, 0x40 },
  306. { 0x3821, 0x00 },
  307. { 0x382f, 0x0e },
  308. { 0x3832, 0x00 },
  309. { 0x3833, 0x05 },
  310. { 0x3834, 0x00 },
  311. { 0x3835, 0x0c },
  312. { 0x3837, 0x00 },
  313. { 0x3b80, 0x00 },
  314. { 0x3b81, 0xa5 },
  315. { 0x3b82, 0x10 },
  316. { 0x3b83, 0x00 },
  317. { 0x3b84, 0x08 },
  318. { 0x3b85, 0x00 },
  319. { 0x3b86, 0x01 },
  320. { 0x3b87, 0x00 },
  321. { 0x3b88, 0x00 },
  322. { 0x3b89, 0x00 },
  323. { 0x3b8a, 0x00 },
  324. { 0x3b8b, 0x05 },
  325. { 0x3b8c, 0x00 },
  326. { 0x3b8d, 0x00 },
  327. { 0x3b8e, 0x00 },
  328. { 0x3b8f, 0x1a },
  329. { 0x3b94, 0x05 },
  330. { 0x3b95, 0xf2 },
  331. { 0x3b96, 0x40 },
  332. { 0x3c00, 0x89 },
  333. { 0x3c01, 0x63 },
  334. { 0x3c02, 0x01 },
  335. { 0x3c03, 0x00 },
  336. { 0x3c04, 0x00 },
  337. { 0x3c05, 0x03 },
  338. { 0x3c06, 0x00 },
  339. { 0x3c07, 0x06 },
  340. { 0x3c0c, 0x01 },
  341. { 0x3c0d, 0xd0 },
  342. { 0x3c0e, 0x02 },
  343. { 0x3c0f, 0x0a },
  344. { 0x4001, 0x42 },
  345. { 0x4004, 0x04 },
  346. { 0x4005, 0x00 },
  347. { 0x404e, 0x01 },
  348. { 0x4300, 0xff },
  349. { 0x4301, 0x00 },
  350. { 0x4315, 0x00 },
  351. { 0x4501, 0x48 },
  352. { 0x4600, 0x00 },
  353. { 0x4601, 0x4e },
  354. { 0x4801, 0x0f },
  355. { 0x4806, 0x0f },
  356. { 0x4819, 0xaa },
  357. { 0x4823, 0x3e },
  358. { 0x4837, 0x19 },
  359. { 0x4a0d, 0x00 },
  360. { 0x4a47, 0x7f },
  361. { 0x4a49, 0xf0 },
  362. { 0x4a4b, 0x30 },
  363. { 0x5000, 0x85 },
  364. { 0x5001, 0x80 },
  365. };
  366. static const struct reg_value ov7251_setting_vga_90fps[] = {
  367. { 0x3005, 0x00 },
  368. { 0x3012, 0xc0 },
  369. { 0x3013, 0xd2 },
  370. { 0x3014, 0x04 },
  371. { 0x3016, 0x10 },
  372. { 0x3017, 0x00 },
  373. { 0x3018, 0x00 },
  374. { 0x301a, 0x00 },
  375. { 0x301b, 0x00 },
  376. { 0x301c, 0x00 },
  377. { 0x3023, 0x05 },
  378. { 0x3037, 0xf0 },
  379. { 0x3098, 0x04 }, /* pll2 pre divider */
  380. { 0x3099, 0x28 }, /* pll2 multiplier */
  381. { 0x309a, 0x05 }, /* pll2 sys divider */
  382. { 0x309b, 0x04 }, /* pll2 adc divider */
  383. { 0x309d, 0x00 }, /* pll2 divider */
  384. { 0x30b0, 0x0a }, /* pll1 pix divider */
  385. { 0x30b1, 0x01 }, /* pll1 divider */
  386. { 0x30b3, 0x64 }, /* pll1 multiplier */
  387. { 0x30b4, 0x03 }, /* pll1 pre divider */
  388. { 0x30b5, 0x05 }, /* pll1 mipi divider */
  389. { 0x3106, 0xda },
  390. { 0x3503, 0x07 },
  391. { 0x3509, 0x10 },
  392. { 0x3600, 0x1c },
  393. { 0x3602, 0x62 },
  394. { 0x3620, 0xb7 },
  395. { 0x3622, 0x04 },
  396. { 0x3626, 0x21 },
  397. { 0x3627, 0x30 },
  398. { 0x3630, 0x44 },
  399. { 0x3631, 0x35 },
  400. { 0x3634, 0x60 },
  401. { 0x3636, 0x00 },
  402. { 0x3662, 0x01 },
  403. { 0x3663, 0x70 },
  404. { 0x3664, 0x50 },
  405. { 0x3666, 0x0a },
  406. { 0x3669, 0x1a },
  407. { 0x366a, 0x00 },
  408. { 0x366b, 0x50 },
  409. { 0x3673, 0x01 },
  410. { 0x3674, 0xff },
  411. { 0x3675, 0x03 },
  412. { 0x3705, 0xc1 },
  413. { 0x3709, 0x40 },
  414. { 0x373c, 0x08 },
  415. { 0x3742, 0x00 },
  416. { 0x3757, 0xb3 },
  417. { 0x3788, 0x00 },
  418. { 0x37a8, 0x01 },
  419. { 0x37a9, 0xc0 },
  420. { 0x3800, 0x00 },
  421. { 0x3801, 0x04 },
  422. { 0x3802, 0x00 },
  423. { 0x3803, 0x04 },
  424. { 0x3804, 0x02 },
  425. { 0x3805, 0x8b },
  426. { 0x3806, 0x01 },
  427. { 0x3807, 0xeb },
  428. { 0x3808, 0x02 }, /* width high */
  429. { 0x3809, 0x80 }, /* width low */
  430. { 0x380a, 0x01 }, /* height high */
  431. { 0x380b, 0xe0 }, /* height low */
  432. { 0x380c, 0x03 }, /* total horiz timing high */
  433. { 0x380d, 0xa0 }, /* total horiz timing low */
  434. { 0x380e, 0x02 }, /* total vertical timing high */
  435. { 0x380f, 0x3c }, /* total vertical timing low */
  436. { 0x3810, 0x00 },
  437. { 0x3811, 0x04 },
  438. { 0x3812, 0x00 },
  439. { 0x3813, 0x05 },
  440. { 0x3814, 0x11 },
  441. { 0x3815, 0x11 },
  442. { 0x3820, 0x40 },
  443. { 0x3821, 0x00 },
  444. { 0x382f, 0x0e },
  445. { 0x3832, 0x00 },
  446. { 0x3833, 0x05 },
  447. { 0x3834, 0x00 },
  448. { 0x3835, 0x0c },
  449. { 0x3837, 0x00 },
  450. { 0x3b80, 0x00 },
  451. { 0x3b81, 0xa5 },
  452. { 0x3b82, 0x10 },
  453. { 0x3b83, 0x00 },
  454. { 0x3b84, 0x08 },
  455. { 0x3b85, 0x00 },
  456. { 0x3b86, 0x01 },
  457. { 0x3b87, 0x00 },
  458. { 0x3b88, 0x00 },
  459. { 0x3b89, 0x00 },
  460. { 0x3b8a, 0x00 },
  461. { 0x3b8b, 0x05 },
  462. { 0x3b8c, 0x00 },
  463. { 0x3b8d, 0x00 },
  464. { 0x3b8e, 0x00 },
  465. { 0x3b8f, 0x1a },
  466. { 0x3b94, 0x05 },
  467. { 0x3b95, 0xf2 },
  468. { 0x3b96, 0x40 },
  469. { 0x3c00, 0x89 },
  470. { 0x3c01, 0x63 },
  471. { 0x3c02, 0x01 },
  472. { 0x3c03, 0x00 },
  473. { 0x3c04, 0x00 },
  474. { 0x3c05, 0x03 },
  475. { 0x3c06, 0x00 },
  476. { 0x3c07, 0x06 },
  477. { 0x3c0c, 0x01 },
  478. { 0x3c0d, 0xd0 },
  479. { 0x3c0e, 0x02 },
  480. { 0x3c0f, 0x0a },
  481. { 0x4001, 0x42 },
  482. { 0x4004, 0x04 },
  483. { 0x4005, 0x00 },
  484. { 0x404e, 0x01 },
  485. { 0x4300, 0xff },
  486. { 0x4301, 0x00 },
  487. { 0x4315, 0x00 },
  488. { 0x4501, 0x48 },
  489. { 0x4600, 0x00 },
  490. { 0x4601, 0x4e },
  491. { 0x4801, 0x0f },
  492. { 0x4806, 0x0f },
  493. { 0x4819, 0xaa },
  494. { 0x4823, 0x3e },
  495. { 0x4837, 0x19 },
  496. { 0x4a0d, 0x00 },
  497. { 0x4a47, 0x7f },
  498. { 0x4a49, 0xf0 },
  499. { 0x4a4b, 0x30 },
  500. { 0x5000, 0x85 },
  501. { 0x5001, 0x80 },
  502. };
  503. static const s64 link_freq[] = {
  504. 240000000,
  505. };
  506. static const struct ov7251_mode_info ov7251_mode_info_data[] = {
  507. {
  508. .width = 640,
  509. .height = 480,
  510. .data = ov7251_setting_vga_30fps,
  511. .data_size = ARRAY_SIZE(ov7251_setting_vga_30fps),
  512. .pixel_clock = 48000000,
  513. .link_freq = 0, /* an index in link_freq[] */
  514. .exposure_max = 1704,
  515. .exposure_def = 504,
  516. .timeperframe = {
  517. .numerator = 100,
  518. .denominator = 3000
  519. }
  520. },
  521. {
  522. .width = 640,
  523. .height = 480,
  524. .data = ov7251_setting_vga_60fps,
  525. .data_size = ARRAY_SIZE(ov7251_setting_vga_60fps),
  526. .pixel_clock = 48000000,
  527. .link_freq = 0, /* an index in link_freq[] */
  528. .exposure_max = 840,
  529. .exposure_def = 504,
  530. .timeperframe = {
  531. .numerator = 100,
  532. .denominator = 6014
  533. }
  534. },
  535. {
  536. .width = 640,
  537. .height = 480,
  538. .data = ov7251_setting_vga_90fps,
  539. .data_size = ARRAY_SIZE(ov7251_setting_vga_90fps),
  540. .pixel_clock = 48000000,
  541. .link_freq = 0, /* an index in link_freq[] */
  542. .exposure_max = 552,
  543. .exposure_def = 504,
  544. .timeperframe = {
  545. .numerator = 100,
  546. .denominator = 9043
  547. }
  548. },
  549. };
  550. static int ov7251_regulators_enable(struct ov7251 *ov7251)
  551. {
  552. int ret;
  553. /* OV7251 power up sequence requires core regulator
  554. * to be enabled not earlier than io regulator
  555. */
  556. ret = regulator_enable(ov7251->io_regulator);
  557. if (ret < 0) {
  558. dev_err(ov7251->dev, "set io voltage failed\n");
  559. return ret;
  560. }
  561. ret = regulator_enable(ov7251->analog_regulator);
  562. if (ret) {
  563. dev_err(ov7251->dev, "set analog voltage failed\n");
  564. goto err_disable_io;
  565. }
  566. ret = regulator_enable(ov7251->core_regulator);
  567. if (ret) {
  568. dev_err(ov7251->dev, "set core voltage failed\n");
  569. goto err_disable_analog;
  570. }
  571. return 0;
  572. err_disable_analog:
  573. regulator_disable(ov7251->analog_regulator);
  574. err_disable_io:
  575. regulator_disable(ov7251->io_regulator);
  576. return ret;
  577. }
  578. static void ov7251_regulators_disable(struct ov7251 *ov7251)
  579. {
  580. int ret;
  581. ret = regulator_disable(ov7251->core_regulator);
  582. if (ret < 0)
  583. dev_err(ov7251->dev, "core regulator disable failed\n");
  584. ret = regulator_disable(ov7251->analog_regulator);
  585. if (ret < 0)
  586. dev_err(ov7251->dev, "analog regulator disable failed\n");
  587. ret = regulator_disable(ov7251->io_regulator);
  588. if (ret < 0)
  589. dev_err(ov7251->dev, "io regulator disable failed\n");
  590. }
  591. static int ov7251_write_reg(struct ov7251 *ov7251, u16 reg, u8 val)
  592. {
  593. u8 regbuf[3];
  594. int ret;
  595. regbuf[0] = reg >> 8;
  596. regbuf[1] = reg & 0xff;
  597. regbuf[2] = val;
  598. ret = i2c_master_send(ov7251->i2c_client, regbuf, 3);
  599. if (ret < 0) {
  600. dev_err(ov7251->dev, "%s: write reg error %d: reg=%x, val=%x\n",
  601. __func__, ret, reg, val);
  602. return ret;
  603. }
  604. return 0;
  605. }
  606. static int ov7251_write_seq_regs(struct ov7251 *ov7251, u16 reg, u8 *val,
  607. u8 num)
  608. {
  609. u8 regbuf[5];
  610. u8 nregbuf = sizeof(reg) + num * sizeof(*val);
  611. int ret = 0;
  612. if (nregbuf > sizeof(regbuf))
  613. return -EINVAL;
  614. regbuf[0] = reg >> 8;
  615. regbuf[1] = reg & 0xff;
  616. memcpy(regbuf + 2, val, num);
  617. ret = i2c_master_send(ov7251->i2c_client, regbuf, nregbuf);
  618. if (ret < 0) {
  619. dev_err(ov7251->dev,
  620. "%s: write seq regs error %d: first reg=%x\n",
  621. __func__, ret, reg);
  622. return ret;
  623. }
  624. return 0;
  625. }
  626. static int ov7251_read_reg(struct ov7251 *ov7251, u16 reg, u8 *val)
  627. {
  628. u8 regbuf[2];
  629. int ret;
  630. regbuf[0] = reg >> 8;
  631. regbuf[1] = reg & 0xff;
  632. ret = i2c_master_send(ov7251->i2c_client, regbuf, 2);
  633. if (ret < 0) {
  634. dev_err(ov7251->dev, "%s: write reg error %d: reg=%x\n",
  635. __func__, ret, reg);
  636. return ret;
  637. }
  638. ret = i2c_master_recv(ov7251->i2c_client, val, 1);
  639. if (ret < 0) {
  640. dev_err(ov7251->dev, "%s: read reg error %d: reg=%x\n",
  641. __func__, ret, reg);
  642. return ret;
  643. }
  644. return 0;
  645. }
  646. static int ov7251_set_exposure(struct ov7251 *ov7251, s32 exposure)
  647. {
  648. u16 reg;
  649. u8 val[3];
  650. reg = OV7251_AEC_EXPO_0;
  651. val[0] = (exposure & 0xf000) >> 12; /* goes to OV7251_AEC_EXPO_0 */
  652. val[1] = (exposure & 0x0ff0) >> 4; /* goes to OV7251_AEC_EXPO_1 */
  653. val[2] = (exposure & 0x000f) << 4; /* goes to OV7251_AEC_EXPO_2 */
  654. return ov7251_write_seq_regs(ov7251, reg, val, 3);
  655. }
  656. static int ov7251_set_gain(struct ov7251 *ov7251, s32 gain)
  657. {
  658. u16 reg;
  659. u8 val[2];
  660. reg = OV7251_AEC_AGC_ADJ_0;
  661. val[0] = (gain & 0x0300) >> 8; /* goes to OV7251_AEC_AGC_ADJ_0 */
  662. val[1] = gain & 0xff; /* goes to OV7251_AEC_AGC_ADJ_1 */
  663. return ov7251_write_seq_regs(ov7251, reg, val, 2);
  664. }
  665. static int ov7251_set_register_array(struct ov7251 *ov7251,
  666. const struct reg_value *settings,
  667. unsigned int num_settings)
  668. {
  669. unsigned int i;
  670. int ret;
  671. for (i = 0; i < num_settings; ++i, ++settings) {
  672. ret = ov7251_write_reg(ov7251, settings->reg, settings->val);
  673. if (ret < 0)
  674. return ret;
  675. }
  676. return 0;
  677. }
  678. static int ov7251_set_power_on(struct ov7251 *ov7251)
  679. {
  680. int ret;
  681. u32 wait_us;
  682. ret = ov7251_regulators_enable(ov7251);
  683. if (ret < 0)
  684. return ret;
  685. ret = clk_prepare_enable(ov7251->xclk);
  686. if (ret < 0) {
  687. dev_err(ov7251->dev, "clk prepare enable failed\n");
  688. ov7251_regulators_disable(ov7251);
  689. return ret;
  690. }
  691. gpiod_set_value_cansleep(ov7251->enable_gpio, 1);
  692. /* wait at least 65536 external clock cycles */
  693. wait_us = DIV_ROUND_UP(65536 * 1000,
  694. DIV_ROUND_UP(ov7251->xclk_freq, 1000));
  695. usleep_range(wait_us, wait_us + 1000);
  696. return 0;
  697. }
  698. static void ov7251_set_power_off(struct ov7251 *ov7251)
  699. {
  700. clk_disable_unprepare(ov7251->xclk);
  701. gpiod_set_value_cansleep(ov7251->enable_gpio, 0);
  702. ov7251_regulators_disable(ov7251);
  703. }
  704. static int ov7251_s_power(struct v4l2_subdev *sd, int on)
  705. {
  706. struct ov7251 *ov7251 = to_ov7251(sd);
  707. int ret = 0;
  708. mutex_lock(&ov7251->lock);
  709. /* If the power state is not modified - no work to do. */
  710. if (ov7251->power_on == !!on)
  711. goto exit;
  712. if (on) {
  713. ret = ov7251_set_power_on(ov7251);
  714. if (ret < 0)
  715. goto exit;
  716. ret = ov7251_set_register_array(ov7251,
  717. ov7251_global_init_setting,
  718. ARRAY_SIZE(ov7251_global_init_setting));
  719. if (ret < 0) {
  720. dev_err(ov7251->dev, "could not set init registers\n");
  721. ov7251_set_power_off(ov7251);
  722. goto exit;
  723. }
  724. ov7251->power_on = true;
  725. } else {
  726. ov7251_set_power_off(ov7251);
  727. ov7251->power_on = false;
  728. }
  729. exit:
  730. mutex_unlock(&ov7251->lock);
  731. return ret;
  732. }
  733. static int ov7251_set_hflip(struct ov7251 *ov7251, s32 value)
  734. {
  735. u8 val = ov7251->timing_format2;
  736. int ret;
  737. if (value)
  738. val |= OV7251_TIMING_FORMAT2_MIRROR;
  739. else
  740. val &= ~OV7251_TIMING_FORMAT2_MIRROR;
  741. ret = ov7251_write_reg(ov7251, OV7251_TIMING_FORMAT2, val);
  742. if (!ret)
  743. ov7251->timing_format2 = val;
  744. return ret;
  745. }
  746. static int ov7251_set_vflip(struct ov7251 *ov7251, s32 value)
  747. {
  748. u8 val = ov7251->timing_format1;
  749. int ret;
  750. if (value)
  751. val |= OV7251_TIMING_FORMAT1_VFLIP;
  752. else
  753. val &= ~OV7251_TIMING_FORMAT1_VFLIP;
  754. ret = ov7251_write_reg(ov7251, OV7251_TIMING_FORMAT1, val);
  755. if (!ret)
  756. ov7251->timing_format1 = val;
  757. return ret;
  758. }
  759. static int ov7251_set_test_pattern(struct ov7251 *ov7251, s32 value)
  760. {
  761. u8 val = ov7251->pre_isp_00;
  762. int ret;
  763. if (value)
  764. val |= OV7251_PRE_ISP_00_TEST_PATTERN;
  765. else
  766. val &= ~OV7251_PRE_ISP_00_TEST_PATTERN;
  767. ret = ov7251_write_reg(ov7251, OV7251_PRE_ISP_00, val);
  768. if (!ret)
  769. ov7251->pre_isp_00 = val;
  770. return ret;
  771. }
  772. static const char * const ov7251_test_pattern_menu[] = {
  773. "Disabled",
  774. "Vertical Pattern Bars",
  775. };
  776. static int ov7251_s_ctrl(struct v4l2_ctrl *ctrl)
  777. {
  778. struct ov7251 *ov7251 = container_of(ctrl->handler,
  779. struct ov7251, ctrls);
  780. int ret;
  781. /* v4l2_ctrl_lock() locks our mutex */
  782. if (!ov7251->power_on)
  783. return 0;
  784. switch (ctrl->id) {
  785. case V4L2_CID_EXPOSURE:
  786. ret = ov7251_set_exposure(ov7251, ctrl->val);
  787. break;
  788. case V4L2_CID_GAIN:
  789. ret = ov7251_set_gain(ov7251, ctrl->val);
  790. break;
  791. case V4L2_CID_TEST_PATTERN:
  792. ret = ov7251_set_test_pattern(ov7251, ctrl->val);
  793. break;
  794. case V4L2_CID_HFLIP:
  795. ret = ov7251_set_hflip(ov7251, ctrl->val);
  796. break;
  797. case V4L2_CID_VFLIP:
  798. ret = ov7251_set_vflip(ov7251, ctrl->val);
  799. break;
  800. default:
  801. ret = -EINVAL;
  802. break;
  803. }
  804. return ret;
  805. }
  806. static const struct v4l2_ctrl_ops ov7251_ctrl_ops = {
  807. .s_ctrl = ov7251_s_ctrl,
  808. };
  809. static int ov7251_enum_mbus_code(struct v4l2_subdev *sd,
  810. struct v4l2_subdev_pad_config *cfg,
  811. struct v4l2_subdev_mbus_code_enum *code)
  812. {
  813. if (code->index > 0)
  814. return -EINVAL;
  815. code->code = MEDIA_BUS_FMT_Y10_1X10;
  816. return 0;
  817. }
  818. static int ov7251_enum_frame_size(struct v4l2_subdev *subdev,
  819. struct v4l2_subdev_pad_config *cfg,
  820. struct v4l2_subdev_frame_size_enum *fse)
  821. {
  822. if (fse->code != MEDIA_BUS_FMT_Y10_1X10)
  823. return -EINVAL;
  824. if (fse->index >= ARRAY_SIZE(ov7251_mode_info_data))
  825. return -EINVAL;
  826. fse->min_width = ov7251_mode_info_data[fse->index].width;
  827. fse->max_width = ov7251_mode_info_data[fse->index].width;
  828. fse->min_height = ov7251_mode_info_data[fse->index].height;
  829. fse->max_height = ov7251_mode_info_data[fse->index].height;
  830. return 0;
  831. }
  832. static int ov7251_enum_frame_ival(struct v4l2_subdev *subdev,
  833. struct v4l2_subdev_pad_config *cfg,
  834. struct v4l2_subdev_frame_interval_enum *fie)
  835. {
  836. unsigned int index = fie->index;
  837. unsigned int i;
  838. for (i = 0; i < ARRAY_SIZE(ov7251_mode_info_data); i++) {
  839. if (fie->width != ov7251_mode_info_data[i].width ||
  840. fie->height != ov7251_mode_info_data[i].height)
  841. continue;
  842. if (index-- == 0) {
  843. fie->interval = ov7251_mode_info_data[i].timeperframe;
  844. return 0;
  845. }
  846. }
  847. return -EINVAL;
  848. }
  849. static struct v4l2_mbus_framefmt *
  850. __ov7251_get_pad_format(struct ov7251 *ov7251,
  851. struct v4l2_subdev_pad_config *cfg,
  852. unsigned int pad,
  853. enum v4l2_subdev_format_whence which)
  854. {
  855. switch (which) {
  856. case V4L2_SUBDEV_FORMAT_TRY:
  857. return v4l2_subdev_get_try_format(&ov7251->sd, cfg, pad);
  858. case V4L2_SUBDEV_FORMAT_ACTIVE:
  859. return &ov7251->fmt;
  860. default:
  861. return NULL;
  862. }
  863. }
  864. static int ov7251_get_format(struct v4l2_subdev *sd,
  865. struct v4l2_subdev_pad_config *cfg,
  866. struct v4l2_subdev_format *format)
  867. {
  868. struct ov7251 *ov7251 = to_ov7251(sd);
  869. mutex_lock(&ov7251->lock);
  870. format->format = *__ov7251_get_pad_format(ov7251, cfg, format->pad,
  871. format->which);
  872. mutex_unlock(&ov7251->lock);
  873. return 0;
  874. }
  875. static struct v4l2_rect *
  876. __ov7251_get_pad_crop(struct ov7251 *ov7251, struct v4l2_subdev_pad_config *cfg,
  877. unsigned int pad, enum v4l2_subdev_format_whence which)
  878. {
  879. switch (which) {
  880. case V4L2_SUBDEV_FORMAT_TRY:
  881. return v4l2_subdev_get_try_crop(&ov7251->sd, cfg, pad);
  882. case V4L2_SUBDEV_FORMAT_ACTIVE:
  883. return &ov7251->crop;
  884. default:
  885. return NULL;
  886. }
  887. }
  888. static inline u32 avg_fps(const struct v4l2_fract *t)
  889. {
  890. return (t->denominator + (t->numerator >> 1)) / t->numerator;
  891. }
  892. static const struct ov7251_mode_info *
  893. ov7251_find_mode_by_ival(struct ov7251 *ov7251, struct v4l2_fract *timeperframe)
  894. {
  895. const struct ov7251_mode_info *mode = ov7251->current_mode;
  896. unsigned int fps_req = avg_fps(timeperframe);
  897. unsigned int max_dist_match = (unsigned int) -1;
  898. unsigned int i, n = 0;
  899. for (i = 0; i < ARRAY_SIZE(ov7251_mode_info_data); i++) {
  900. unsigned int dist;
  901. unsigned int fps_tmp;
  902. if (mode->width != ov7251_mode_info_data[i].width ||
  903. mode->height != ov7251_mode_info_data[i].height)
  904. continue;
  905. fps_tmp = avg_fps(&ov7251_mode_info_data[i].timeperframe);
  906. dist = abs(fps_req - fps_tmp);
  907. if (dist < max_dist_match) {
  908. n = i;
  909. max_dist_match = dist;
  910. }
  911. }
  912. return &ov7251_mode_info_data[n];
  913. }
  914. static int ov7251_set_format(struct v4l2_subdev *sd,
  915. struct v4l2_subdev_pad_config *cfg,
  916. struct v4l2_subdev_format *format)
  917. {
  918. struct ov7251 *ov7251 = to_ov7251(sd);
  919. struct v4l2_mbus_framefmt *__format;
  920. struct v4l2_rect *__crop;
  921. const struct ov7251_mode_info *new_mode;
  922. int ret = 0;
  923. mutex_lock(&ov7251->lock);
  924. __crop = __ov7251_get_pad_crop(ov7251, cfg, format->pad, format->which);
  925. new_mode = v4l2_find_nearest_size(ov7251_mode_info_data,
  926. ARRAY_SIZE(ov7251_mode_info_data),
  927. width, height,
  928. format->format.width, format->format.height);
  929. __crop->width = new_mode->width;
  930. __crop->height = new_mode->height;
  931. if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
  932. ret = __v4l2_ctrl_s_ctrl_int64(ov7251->pixel_clock,
  933. new_mode->pixel_clock);
  934. if (ret < 0)
  935. goto exit;
  936. ret = __v4l2_ctrl_s_ctrl(ov7251->link_freq,
  937. new_mode->link_freq);
  938. if (ret < 0)
  939. goto exit;
  940. ret = __v4l2_ctrl_modify_range(ov7251->exposure,
  941. 1, new_mode->exposure_max,
  942. 1, new_mode->exposure_def);
  943. if (ret < 0)
  944. goto exit;
  945. ret = __v4l2_ctrl_s_ctrl(ov7251->exposure,
  946. new_mode->exposure_def);
  947. if (ret < 0)
  948. goto exit;
  949. ret = __v4l2_ctrl_s_ctrl(ov7251->gain, 16);
  950. if (ret < 0)
  951. goto exit;
  952. ov7251->current_mode = new_mode;
  953. }
  954. __format = __ov7251_get_pad_format(ov7251, cfg, format->pad,
  955. format->which);
  956. __format->width = __crop->width;
  957. __format->height = __crop->height;
  958. __format->code = MEDIA_BUS_FMT_Y10_1X10;
  959. __format->field = V4L2_FIELD_NONE;
  960. __format->colorspace = V4L2_COLORSPACE_SRGB;
  961. __format->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(__format->colorspace);
  962. __format->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true,
  963. __format->colorspace, __format->ycbcr_enc);
  964. __format->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(__format->colorspace);
  965. format->format = *__format;
  966. exit:
  967. mutex_unlock(&ov7251->lock);
  968. return ret;
  969. }
  970. static int ov7251_entity_init_cfg(struct v4l2_subdev *subdev,
  971. struct v4l2_subdev_pad_config *cfg)
  972. {
  973. struct v4l2_subdev_format fmt = {
  974. .which = cfg ? V4L2_SUBDEV_FORMAT_TRY
  975. : V4L2_SUBDEV_FORMAT_ACTIVE,
  976. .format = {
  977. .width = 640,
  978. .height = 480
  979. }
  980. };
  981. ov7251_set_format(subdev, cfg, &fmt);
  982. return 0;
  983. }
  984. static int ov7251_get_selection(struct v4l2_subdev *sd,
  985. struct v4l2_subdev_pad_config *cfg,
  986. struct v4l2_subdev_selection *sel)
  987. {
  988. struct ov7251 *ov7251 = to_ov7251(sd);
  989. if (sel->target != V4L2_SEL_TGT_CROP)
  990. return -EINVAL;
  991. mutex_lock(&ov7251->lock);
  992. sel->r = *__ov7251_get_pad_crop(ov7251, cfg, sel->pad,
  993. sel->which);
  994. mutex_unlock(&ov7251->lock);
  995. return 0;
  996. }
  997. static int ov7251_s_stream(struct v4l2_subdev *subdev, int enable)
  998. {
  999. struct ov7251 *ov7251 = to_ov7251(subdev);
  1000. int ret;
  1001. mutex_lock(&ov7251->lock);
  1002. if (enable) {
  1003. ret = ov7251_set_register_array(ov7251,
  1004. ov7251->current_mode->data,
  1005. ov7251->current_mode->data_size);
  1006. if (ret < 0) {
  1007. dev_err(ov7251->dev, "could not set mode %dx%d\n",
  1008. ov7251->current_mode->width,
  1009. ov7251->current_mode->height);
  1010. goto exit;
  1011. }
  1012. ret = __v4l2_ctrl_handler_setup(&ov7251->ctrls);
  1013. if (ret < 0) {
  1014. dev_err(ov7251->dev, "could not sync v4l2 controls\n");
  1015. goto exit;
  1016. }
  1017. ret = ov7251_write_reg(ov7251, OV7251_SC_MODE_SELECT,
  1018. OV7251_SC_MODE_SELECT_STREAMING);
  1019. } else {
  1020. ret = ov7251_write_reg(ov7251, OV7251_SC_MODE_SELECT,
  1021. OV7251_SC_MODE_SELECT_SW_STANDBY);
  1022. }
  1023. exit:
  1024. mutex_unlock(&ov7251->lock);
  1025. return ret;
  1026. }
  1027. static int ov7251_get_frame_interval(struct v4l2_subdev *subdev,
  1028. struct v4l2_subdev_frame_interval *fi)
  1029. {
  1030. struct ov7251 *ov7251 = to_ov7251(subdev);
  1031. mutex_lock(&ov7251->lock);
  1032. fi->interval = ov7251->current_mode->timeperframe;
  1033. mutex_unlock(&ov7251->lock);
  1034. return 0;
  1035. }
  1036. static int ov7251_set_frame_interval(struct v4l2_subdev *subdev,
  1037. struct v4l2_subdev_frame_interval *fi)
  1038. {
  1039. struct ov7251 *ov7251 = to_ov7251(subdev);
  1040. const struct ov7251_mode_info *new_mode;
  1041. int ret = 0;
  1042. mutex_lock(&ov7251->lock);
  1043. new_mode = ov7251_find_mode_by_ival(ov7251, &fi->interval);
  1044. if (new_mode != ov7251->current_mode) {
  1045. ret = __v4l2_ctrl_s_ctrl_int64(ov7251->pixel_clock,
  1046. new_mode->pixel_clock);
  1047. if (ret < 0)
  1048. goto exit;
  1049. ret = __v4l2_ctrl_s_ctrl(ov7251->link_freq,
  1050. new_mode->link_freq);
  1051. if (ret < 0)
  1052. goto exit;
  1053. ret = __v4l2_ctrl_modify_range(ov7251->exposure,
  1054. 1, new_mode->exposure_max,
  1055. 1, new_mode->exposure_def);
  1056. if (ret < 0)
  1057. goto exit;
  1058. ret = __v4l2_ctrl_s_ctrl(ov7251->exposure,
  1059. new_mode->exposure_def);
  1060. if (ret < 0)
  1061. goto exit;
  1062. ret = __v4l2_ctrl_s_ctrl(ov7251->gain, 16);
  1063. if (ret < 0)
  1064. goto exit;
  1065. ov7251->current_mode = new_mode;
  1066. }
  1067. fi->interval = ov7251->current_mode->timeperframe;
  1068. exit:
  1069. mutex_unlock(&ov7251->lock);
  1070. return ret;
  1071. }
  1072. static const struct v4l2_subdev_core_ops ov7251_core_ops = {
  1073. .s_power = ov7251_s_power,
  1074. };
  1075. static const struct v4l2_subdev_video_ops ov7251_video_ops = {
  1076. .s_stream = ov7251_s_stream,
  1077. .g_frame_interval = ov7251_get_frame_interval,
  1078. .s_frame_interval = ov7251_set_frame_interval,
  1079. };
  1080. static const struct v4l2_subdev_pad_ops ov7251_subdev_pad_ops = {
  1081. .init_cfg = ov7251_entity_init_cfg,
  1082. .enum_mbus_code = ov7251_enum_mbus_code,
  1083. .enum_frame_size = ov7251_enum_frame_size,
  1084. .enum_frame_interval = ov7251_enum_frame_ival,
  1085. .get_fmt = ov7251_get_format,
  1086. .set_fmt = ov7251_set_format,
  1087. .get_selection = ov7251_get_selection,
  1088. };
  1089. static const struct v4l2_subdev_ops ov7251_subdev_ops = {
  1090. .core = &ov7251_core_ops,
  1091. .video = &ov7251_video_ops,
  1092. .pad = &ov7251_subdev_pad_ops,
  1093. };
  1094. static int ov7251_probe(struct i2c_client *client)
  1095. {
  1096. struct device *dev = &client->dev;
  1097. struct fwnode_handle *endpoint;
  1098. struct ov7251 *ov7251;
  1099. u8 chip_id_high, chip_id_low, chip_rev;
  1100. int ret;
  1101. ov7251 = devm_kzalloc(dev, sizeof(struct ov7251), GFP_KERNEL);
  1102. if (!ov7251)
  1103. return -ENOMEM;
  1104. ov7251->i2c_client = client;
  1105. ov7251->dev = dev;
  1106. endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
  1107. if (!endpoint) {
  1108. dev_err(dev, "endpoint node not found\n");
  1109. return -EINVAL;
  1110. }
  1111. ret = v4l2_fwnode_endpoint_parse(endpoint, &ov7251->ep);
  1112. fwnode_handle_put(endpoint);
  1113. if (ret < 0) {
  1114. dev_err(dev, "parsing endpoint node failed\n");
  1115. return ret;
  1116. }
  1117. if (ov7251->ep.bus_type != V4L2_MBUS_CSI2) {
  1118. dev_err(dev, "invalid bus type (%u), must be CSI2 (%u)\n",
  1119. ov7251->ep.bus_type, V4L2_MBUS_CSI2);
  1120. return -EINVAL;
  1121. }
  1122. /* get system clock (xclk) */
  1123. ov7251->xclk = devm_clk_get(dev, "xclk");
  1124. if (IS_ERR(ov7251->xclk)) {
  1125. dev_err(dev, "could not get xclk");
  1126. return PTR_ERR(ov7251->xclk);
  1127. }
  1128. ret = fwnode_property_read_u32(dev_fwnode(dev), "clock-frequency",
  1129. &ov7251->xclk_freq);
  1130. if (ret) {
  1131. dev_err(dev, "could not get xclk frequency\n");
  1132. return ret;
  1133. }
  1134. /* external clock must be 24MHz, allow 1% tolerance */
  1135. if (ov7251->xclk_freq < 23760000 || ov7251->xclk_freq > 24240000) {
  1136. dev_err(dev, "external clock frequency %u is not supported\n",
  1137. ov7251->xclk_freq);
  1138. return -EINVAL;
  1139. }
  1140. ret = clk_set_rate(ov7251->xclk, ov7251->xclk_freq);
  1141. if (ret) {
  1142. dev_err(dev, "could not set xclk frequency\n");
  1143. return ret;
  1144. }
  1145. ov7251->io_regulator = devm_regulator_get(dev, "vdddo");
  1146. if (IS_ERR(ov7251->io_regulator)) {
  1147. dev_err(dev, "cannot get io regulator\n");
  1148. return PTR_ERR(ov7251->io_regulator);
  1149. }
  1150. ov7251->core_regulator = devm_regulator_get(dev, "vddd");
  1151. if (IS_ERR(ov7251->core_regulator)) {
  1152. dev_err(dev, "cannot get core regulator\n");
  1153. return PTR_ERR(ov7251->core_regulator);
  1154. }
  1155. ov7251->analog_regulator = devm_regulator_get(dev, "vdda");
  1156. if (IS_ERR(ov7251->analog_regulator)) {
  1157. dev_err(dev, "cannot get analog regulator\n");
  1158. return PTR_ERR(ov7251->analog_regulator);
  1159. }
  1160. ov7251->enable_gpio = devm_gpiod_get(dev, "enable", GPIOD_OUT_HIGH);
  1161. if (IS_ERR(ov7251->enable_gpio)) {
  1162. dev_err(dev, "cannot get enable gpio\n");
  1163. return PTR_ERR(ov7251->enable_gpio);
  1164. }
  1165. mutex_init(&ov7251->lock);
  1166. v4l2_ctrl_handler_init(&ov7251->ctrls, 7);
  1167. ov7251->ctrls.lock = &ov7251->lock;
  1168. v4l2_ctrl_new_std(&ov7251->ctrls, &ov7251_ctrl_ops,
  1169. V4L2_CID_HFLIP, 0, 1, 1, 0);
  1170. v4l2_ctrl_new_std(&ov7251->ctrls, &ov7251_ctrl_ops,
  1171. V4L2_CID_VFLIP, 0, 1, 1, 0);
  1172. ov7251->exposure = v4l2_ctrl_new_std(&ov7251->ctrls, &ov7251_ctrl_ops,
  1173. V4L2_CID_EXPOSURE, 1, 32, 1, 32);
  1174. ov7251->gain = v4l2_ctrl_new_std(&ov7251->ctrls, &ov7251_ctrl_ops,
  1175. V4L2_CID_GAIN, 16, 1023, 1, 16);
  1176. v4l2_ctrl_new_std_menu_items(&ov7251->ctrls, &ov7251_ctrl_ops,
  1177. V4L2_CID_TEST_PATTERN,
  1178. ARRAY_SIZE(ov7251_test_pattern_menu) - 1,
  1179. 0, 0, ov7251_test_pattern_menu);
  1180. ov7251->pixel_clock = v4l2_ctrl_new_std(&ov7251->ctrls,
  1181. &ov7251_ctrl_ops,
  1182. V4L2_CID_PIXEL_RATE,
  1183. 1, INT_MAX, 1, 1);
  1184. ov7251->link_freq = v4l2_ctrl_new_int_menu(&ov7251->ctrls,
  1185. &ov7251_ctrl_ops,
  1186. V4L2_CID_LINK_FREQ,
  1187. ARRAY_SIZE(link_freq) - 1,
  1188. 0, link_freq);
  1189. if (ov7251->link_freq)
  1190. ov7251->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  1191. ov7251->sd.ctrl_handler = &ov7251->ctrls;
  1192. if (ov7251->ctrls.error) {
  1193. dev_err(dev, "%s: control initialization error %d\n",
  1194. __func__, ov7251->ctrls.error);
  1195. ret = ov7251->ctrls.error;
  1196. goto free_ctrl;
  1197. }
  1198. v4l2_i2c_subdev_init(&ov7251->sd, client, &ov7251_subdev_ops);
  1199. ov7251->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  1200. ov7251->pad.flags = MEDIA_PAD_FL_SOURCE;
  1201. ov7251->sd.dev = &client->dev;
  1202. ov7251->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
  1203. ret = media_entity_pads_init(&ov7251->sd.entity, 1, &ov7251->pad);
  1204. if (ret < 0) {
  1205. dev_err(dev, "could not register media entity\n");
  1206. goto free_ctrl;
  1207. }
  1208. ret = ov7251_s_power(&ov7251->sd, true);
  1209. if (ret < 0) {
  1210. dev_err(dev, "could not power up OV7251\n");
  1211. goto free_entity;
  1212. }
  1213. ret = ov7251_read_reg(ov7251, OV7251_CHIP_ID_HIGH, &chip_id_high);
  1214. if (ret < 0 || chip_id_high != OV7251_CHIP_ID_HIGH_BYTE) {
  1215. dev_err(dev, "could not read ID high\n");
  1216. ret = -ENODEV;
  1217. goto power_down;
  1218. }
  1219. ret = ov7251_read_reg(ov7251, OV7251_CHIP_ID_LOW, &chip_id_low);
  1220. if (ret < 0 || chip_id_low != OV7251_CHIP_ID_LOW_BYTE) {
  1221. dev_err(dev, "could not read ID low\n");
  1222. ret = -ENODEV;
  1223. goto power_down;
  1224. }
  1225. ret = ov7251_read_reg(ov7251, OV7251_SC_GP_IO_IN1, &chip_rev);
  1226. if (ret < 0) {
  1227. dev_err(dev, "could not read revision\n");
  1228. ret = -ENODEV;
  1229. goto power_down;
  1230. }
  1231. chip_rev >>= 4;
  1232. dev_info(dev, "OV7251 revision %x (%s) detected at address 0x%02x\n",
  1233. chip_rev,
  1234. chip_rev == 0x4 ? "1A / 1B" :
  1235. chip_rev == 0x5 ? "1C / 1D" :
  1236. chip_rev == 0x6 ? "1E" :
  1237. chip_rev == 0x7 ? "1F" : "unknown",
  1238. client->addr);
  1239. ret = ov7251_read_reg(ov7251, OV7251_PRE_ISP_00,
  1240. &ov7251->pre_isp_00);
  1241. if (ret < 0) {
  1242. dev_err(dev, "could not read test pattern value\n");
  1243. ret = -ENODEV;
  1244. goto power_down;
  1245. }
  1246. ret = ov7251_read_reg(ov7251, OV7251_TIMING_FORMAT1,
  1247. &ov7251->timing_format1);
  1248. if (ret < 0) {
  1249. dev_err(dev, "could not read vflip value\n");
  1250. ret = -ENODEV;
  1251. goto power_down;
  1252. }
  1253. ret = ov7251_read_reg(ov7251, OV7251_TIMING_FORMAT2,
  1254. &ov7251->timing_format2);
  1255. if (ret < 0) {
  1256. dev_err(dev, "could not read hflip value\n");
  1257. ret = -ENODEV;
  1258. goto power_down;
  1259. }
  1260. ov7251_s_power(&ov7251->sd, false);
  1261. ret = v4l2_async_register_subdev(&ov7251->sd);
  1262. if (ret < 0) {
  1263. dev_err(dev, "could not register v4l2 device\n");
  1264. goto free_entity;
  1265. }
  1266. ov7251_entity_init_cfg(&ov7251->sd, NULL);
  1267. return 0;
  1268. power_down:
  1269. ov7251_s_power(&ov7251->sd, false);
  1270. free_entity:
  1271. media_entity_cleanup(&ov7251->sd.entity);
  1272. free_ctrl:
  1273. v4l2_ctrl_handler_free(&ov7251->ctrls);
  1274. mutex_destroy(&ov7251->lock);
  1275. return ret;
  1276. }
  1277. static int ov7251_remove(struct i2c_client *client)
  1278. {
  1279. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1280. struct ov7251 *ov7251 = to_ov7251(sd);
  1281. v4l2_async_unregister_subdev(&ov7251->sd);
  1282. media_entity_cleanup(&ov7251->sd.entity);
  1283. v4l2_ctrl_handler_free(&ov7251->ctrls);
  1284. mutex_destroy(&ov7251->lock);
  1285. return 0;
  1286. }
  1287. static const struct of_device_id ov7251_of_match[] = {
  1288. { .compatible = "ovti,ov7251" },
  1289. { /* sentinel */ }
  1290. };
  1291. MODULE_DEVICE_TABLE(of, ov7251_of_match);
  1292. static struct i2c_driver ov7251_i2c_driver = {
  1293. .driver = {
  1294. .of_match_table = ov7251_of_match,
  1295. .name = "ov7251",
  1296. },
  1297. .probe_new = ov7251_probe,
  1298. .remove = ov7251_remove,
  1299. };
  1300. module_i2c_driver(ov7251_i2c_driver);
  1301. MODULE_DESCRIPTION("Omnivision OV7251 Camera Driver");
  1302. MODULE_AUTHOR("Todor Tomov <todor.tomov@linaro.org>");
  1303. MODULE_LICENSE("GPL v2");