ov5695.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * ov5695 driver
  4. *
  5. * Copyright (C) 2017 Fuzhou Rockchip Electronics Co., Ltd.
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/device.h>
  9. #include <linux/delay.h>
  10. #include <linux/gpio/consumer.h>
  11. #include <linux/i2c.h>
  12. #include <linux/module.h>
  13. #include <linux/pm_runtime.h>
  14. #include <linux/regulator/consumer.h>
  15. #include <linux/sysfs.h>
  16. #include <media/media-entity.h>
  17. #include <media/v4l2-async.h>
  18. #include <media/v4l2-ctrls.h>
  19. #include <media/v4l2-subdev.h>
  20. #ifndef V4L2_CID_DIGITAL_GAIN
  21. #define V4L2_CID_DIGITAL_GAIN V4L2_CID_GAIN
  22. #endif
  23. /* 45Mhz * 4 Binning */
  24. #define OV5695_PIXEL_RATE (45 * 1000 * 1000 * 4)
  25. #define OV5695_XVCLK_FREQ 24000000
  26. #define CHIP_ID 0x005695
  27. #define OV5695_REG_CHIP_ID 0x300a
  28. #define OV5695_REG_CTRL_MODE 0x0100
  29. #define OV5695_MODE_SW_STANDBY 0x0
  30. #define OV5695_MODE_STREAMING BIT(0)
  31. #define OV5695_REG_EXPOSURE 0x3500
  32. #define OV5695_EXPOSURE_MIN 4
  33. #define OV5695_EXPOSURE_STEP 1
  34. #define OV5695_VTS_MAX 0x7fff
  35. #define OV5695_REG_ANALOG_GAIN 0x3509
  36. #define ANALOG_GAIN_MIN 0x10
  37. #define ANALOG_GAIN_MAX 0xf8
  38. #define ANALOG_GAIN_STEP 1
  39. #define ANALOG_GAIN_DEFAULT 0xf8
  40. #define OV5695_REG_DIGI_GAIN_H 0x350a
  41. #define OV5695_REG_DIGI_GAIN_L 0x350b
  42. #define OV5695_DIGI_GAIN_L_MASK 0x3f
  43. #define OV5695_DIGI_GAIN_H_SHIFT 6
  44. #define OV5695_DIGI_GAIN_MIN 0
  45. #define OV5695_DIGI_GAIN_MAX (0x4000 - 1)
  46. #define OV5695_DIGI_GAIN_STEP 1
  47. #define OV5695_DIGI_GAIN_DEFAULT 1024
  48. #define OV5695_REG_TEST_PATTERN 0x4503
  49. #define OV5695_TEST_PATTERN_ENABLE 0x80
  50. #define OV5695_TEST_PATTERN_DISABLE 0x0
  51. #define OV5695_REG_VTS 0x380e
  52. #define REG_NULL 0xFFFF
  53. #define OV5695_REG_VALUE_08BIT 1
  54. #define OV5695_REG_VALUE_16BIT 2
  55. #define OV5695_REG_VALUE_24BIT 3
  56. #define OV5695_LANES 2
  57. #define OV5695_BITS_PER_SAMPLE 10
  58. static const char * const ov5695_supply_names[] = {
  59. "avdd", /* Analog power */
  60. "dovdd", /* Digital I/O power */
  61. "dvdd", /* Digital core power */
  62. };
  63. #define OV5695_NUM_SUPPLIES ARRAY_SIZE(ov5695_supply_names)
  64. struct regval {
  65. u16 addr;
  66. u8 val;
  67. };
  68. struct ov5695_mode {
  69. u32 width;
  70. u32 height;
  71. u32 max_fps;
  72. u32 hts_def;
  73. u32 vts_def;
  74. u32 exp_def;
  75. const struct regval *reg_list;
  76. };
  77. struct ov5695 {
  78. struct i2c_client *client;
  79. struct clk *xvclk;
  80. struct gpio_desc *reset_gpio;
  81. struct regulator_bulk_data supplies[OV5695_NUM_SUPPLIES];
  82. struct v4l2_subdev subdev;
  83. struct media_pad pad;
  84. struct v4l2_ctrl_handler ctrl_handler;
  85. struct v4l2_ctrl *exposure;
  86. struct v4l2_ctrl *anal_gain;
  87. struct v4l2_ctrl *digi_gain;
  88. struct v4l2_ctrl *hblank;
  89. struct v4l2_ctrl *vblank;
  90. struct v4l2_ctrl *test_pattern;
  91. struct mutex mutex;
  92. bool streaming;
  93. const struct ov5695_mode *cur_mode;
  94. };
  95. #define to_ov5695(sd) container_of(sd, struct ov5695, subdev)
  96. /*
  97. * Xclk 24Mhz
  98. * Pclk 45Mhz
  99. * linelength 672(0x2a0)
  100. * framelength 2232(0x8b8)
  101. * grabwindow_width 1296
  102. * grabwindow_height 972
  103. * max_framerate 30fps
  104. * mipi_datarate per lane 840Mbps
  105. */
  106. static const struct regval ov5695_global_regs[] = {
  107. {0x0103, 0x01},
  108. {0x0100, 0x00},
  109. {0x0300, 0x04},
  110. {0x0301, 0x00},
  111. {0x0302, 0x69},
  112. {0x0303, 0x00},
  113. {0x0304, 0x00},
  114. {0x0305, 0x01},
  115. {0x0307, 0x00},
  116. {0x030b, 0x00},
  117. {0x030c, 0x00},
  118. {0x030d, 0x1e},
  119. {0x030e, 0x04},
  120. {0x030f, 0x03},
  121. {0x0312, 0x01},
  122. {0x3000, 0x00},
  123. {0x3002, 0xa1},
  124. {0x3008, 0x00},
  125. {0x3010, 0x00},
  126. {0x3022, 0x51},
  127. {0x3106, 0x15},
  128. {0x3107, 0x01},
  129. {0x3108, 0x05},
  130. {0x3500, 0x00},
  131. {0x3501, 0x45},
  132. {0x3502, 0x00},
  133. {0x3503, 0x08},
  134. {0x3504, 0x03},
  135. {0x3505, 0x8c},
  136. {0x3507, 0x03},
  137. {0x3508, 0x00},
  138. {0x3509, 0x10},
  139. {0x350c, 0x00},
  140. {0x350d, 0x80},
  141. {0x3510, 0x00},
  142. {0x3511, 0x02},
  143. {0x3512, 0x00},
  144. {0x3601, 0x55},
  145. {0x3602, 0x58},
  146. {0x3614, 0x30},
  147. {0x3615, 0x77},
  148. {0x3621, 0x08},
  149. {0x3624, 0x40},
  150. {0x3633, 0x0c},
  151. {0x3634, 0x0c},
  152. {0x3635, 0x0c},
  153. {0x3636, 0x0c},
  154. {0x3638, 0x00},
  155. {0x3639, 0x00},
  156. {0x363a, 0x00},
  157. {0x363b, 0x00},
  158. {0x363c, 0xff},
  159. {0x363d, 0xfa},
  160. {0x3650, 0x44},
  161. {0x3651, 0x44},
  162. {0x3652, 0x44},
  163. {0x3653, 0x44},
  164. {0x3654, 0x44},
  165. {0x3655, 0x44},
  166. {0x3656, 0x44},
  167. {0x3657, 0x44},
  168. {0x3660, 0x00},
  169. {0x3661, 0x00},
  170. {0x3662, 0x00},
  171. {0x366a, 0x00},
  172. {0x366e, 0x0c},
  173. {0x3673, 0x04},
  174. {0x3700, 0x14},
  175. {0x3703, 0x0c},
  176. {0x3715, 0x01},
  177. {0x3733, 0x10},
  178. {0x3734, 0x40},
  179. {0x373f, 0xa0},
  180. {0x3765, 0x20},
  181. {0x37a1, 0x1d},
  182. {0x37a8, 0x26},
  183. {0x37ab, 0x14},
  184. {0x37c2, 0x04},
  185. {0x37cb, 0x09},
  186. {0x37cc, 0x13},
  187. {0x37cd, 0x1f},
  188. {0x37ce, 0x1f},
  189. {0x3800, 0x00},
  190. {0x3801, 0x00},
  191. {0x3802, 0x00},
  192. {0x3803, 0x00},
  193. {0x3804, 0x0a},
  194. {0x3805, 0x3f},
  195. {0x3806, 0x07},
  196. {0x3807, 0xaf},
  197. {0x3808, 0x05},
  198. {0x3809, 0x10},
  199. {0x380a, 0x03},
  200. {0x380b, 0xcc},
  201. {0x380c, 0x02},
  202. {0x380d, 0xa0},
  203. {0x380e, 0x08},
  204. {0x380f, 0xb8},
  205. {0x3810, 0x00},
  206. {0x3811, 0x06},
  207. {0x3812, 0x00},
  208. {0x3813, 0x06},
  209. {0x3814, 0x03},
  210. {0x3815, 0x01},
  211. {0x3816, 0x03},
  212. {0x3817, 0x01},
  213. {0x3818, 0x00},
  214. {0x3819, 0x00},
  215. {0x381a, 0x00},
  216. {0x381b, 0x01},
  217. {0x3820, 0x8b},
  218. {0x3821, 0x01},
  219. {0x3c80, 0x08},
  220. {0x3c82, 0x00},
  221. {0x3c83, 0x00},
  222. {0x3c88, 0x00},
  223. {0x3d85, 0x14},
  224. {0x3f02, 0x08},
  225. {0x3f03, 0x10},
  226. {0x4008, 0x02},
  227. {0x4009, 0x09},
  228. {0x404e, 0x20},
  229. {0x4501, 0x00},
  230. {0x4502, 0x10},
  231. {0x4800, 0x00},
  232. {0x481f, 0x2a},
  233. {0x4837, 0x13},
  234. {0x5000, 0x17},
  235. {0x5780, 0x3e},
  236. {0x5781, 0x0f},
  237. {0x5782, 0x44},
  238. {0x5783, 0x02},
  239. {0x5784, 0x01},
  240. {0x5785, 0x01},
  241. {0x5786, 0x00},
  242. {0x5787, 0x04},
  243. {0x5788, 0x02},
  244. {0x5789, 0x0f},
  245. {0x578a, 0xfd},
  246. {0x578b, 0xf5},
  247. {0x578c, 0xf5},
  248. {0x578d, 0x03},
  249. {0x578e, 0x08},
  250. {0x578f, 0x0c},
  251. {0x5790, 0x08},
  252. {0x5791, 0x06},
  253. {0x5792, 0x00},
  254. {0x5793, 0x52},
  255. {0x5794, 0xa3},
  256. {0x5b00, 0x00},
  257. {0x5b01, 0x1c},
  258. {0x5b02, 0x00},
  259. {0x5b03, 0x7f},
  260. {0x5b05, 0x6c},
  261. {0x5e10, 0xfc},
  262. {0x4010, 0xf1},
  263. {0x3503, 0x08},
  264. {0x3505, 0x8c},
  265. {0x3507, 0x03},
  266. {0x3508, 0x00},
  267. {0x3509, 0xf8},
  268. {REG_NULL, 0x00},
  269. };
  270. /*
  271. * Xclk 24Mhz
  272. * Pclk 45Mhz
  273. * linelength 740(0x2e4)
  274. * framelength 2024(0x7e8)
  275. * grabwindow_width 2592
  276. * grabwindow_height 1944
  277. * max_framerate 30fps
  278. * mipi_datarate per lane 840Mbps
  279. */
  280. static const struct regval ov5695_2592x1944_regs[] = {
  281. {0x3501, 0x7e},
  282. {0x366e, 0x18},
  283. {0x3800, 0x00},
  284. {0x3801, 0x00},
  285. {0x3802, 0x00},
  286. {0x3803, 0x04},
  287. {0x3804, 0x0a},
  288. {0x3805, 0x3f},
  289. {0x3806, 0x07},
  290. {0x3807, 0xab},
  291. {0x3808, 0x0a},
  292. {0x3809, 0x20},
  293. {0x380a, 0x07},
  294. {0x380b, 0x98},
  295. {0x380c, 0x02},
  296. {0x380d, 0xe4},
  297. {0x380e, 0x07},
  298. {0x380f, 0xe8},
  299. {0x3811, 0x06},
  300. {0x3813, 0x08},
  301. {0x3814, 0x01},
  302. {0x3816, 0x01},
  303. {0x3817, 0x01},
  304. {0x3820, 0x88},
  305. {0x3821, 0x00},
  306. {0x4501, 0x00},
  307. {0x4008, 0x04},
  308. {0x4009, 0x13},
  309. {REG_NULL, 0x00},
  310. };
  311. /*
  312. * Xclk 24Mhz
  313. * Pclk 45Mhz
  314. * linelength 672(0x2a0)
  315. * framelength 2232(0x8b8)
  316. * grabwindow_width 1920
  317. * grabwindow_height 1080
  318. * max_framerate 30fps
  319. * mipi_datarate per lane 840Mbps
  320. */
  321. static const struct regval ov5695_1920x1080_regs[] = {
  322. {0x3501, 0x45},
  323. {0x366e, 0x18},
  324. {0x3800, 0x01},
  325. {0x3801, 0x50},
  326. {0x3802, 0x01},
  327. {0x3803, 0xb8},
  328. {0x3804, 0x08},
  329. {0x3805, 0xef},
  330. {0x3806, 0x05},
  331. {0x3807, 0xf7},
  332. {0x3808, 0x07},
  333. {0x3809, 0x80},
  334. {0x380a, 0x04},
  335. {0x380b, 0x38},
  336. {0x380c, 0x02},
  337. {0x380d, 0xa0},
  338. {0x380e, 0x08},
  339. {0x380f, 0xb8},
  340. {0x3811, 0x06},
  341. {0x3813, 0x04},
  342. {0x3814, 0x01},
  343. {0x3816, 0x01},
  344. {0x3817, 0x01},
  345. {0x3820, 0x88},
  346. {0x3821, 0x00},
  347. {0x4501, 0x00},
  348. {0x4008, 0x04},
  349. {0x4009, 0x13},
  350. {REG_NULL, 0x00}
  351. };
  352. /*
  353. * Xclk 24Mhz
  354. * Pclk 45Mhz
  355. * linelength 740(0x02e4)
  356. * framelength 1012(0x03f4)
  357. * grabwindow_width 1296
  358. * grabwindow_height 972
  359. * max_framerate 60fps
  360. * mipi_datarate per lane 840Mbps
  361. */
  362. static const struct regval ov5695_1296x972_regs[] = {
  363. {0x0103, 0x01},
  364. {0x0100, 0x00},
  365. {0x0300, 0x04},
  366. {0x0301, 0x00},
  367. {0x0302, 0x69},
  368. {0x0303, 0x00},
  369. {0x0304, 0x00},
  370. {0x0305, 0x01},
  371. {0x0307, 0x00},
  372. {0x030b, 0x00},
  373. {0x030c, 0x00},
  374. {0x030d, 0x1e},
  375. {0x030e, 0x04},
  376. {0x030f, 0x03},
  377. {0x0312, 0x01},
  378. {0x3000, 0x00},
  379. {0x3002, 0xa1},
  380. {0x3008, 0x00},
  381. {0x3010, 0x00},
  382. {0x3016, 0x32},
  383. {0x3022, 0x51},
  384. {0x3106, 0x15},
  385. {0x3107, 0x01},
  386. {0x3108, 0x05},
  387. {0x3500, 0x00},
  388. {0x3501, 0x3e},
  389. {0x3502, 0x00},
  390. {0x3503, 0x08},
  391. {0x3504, 0x03},
  392. {0x3505, 0x8c},
  393. {0x3507, 0x03},
  394. {0x3508, 0x00},
  395. {0x3509, 0x10},
  396. {0x350c, 0x00},
  397. {0x350d, 0x80},
  398. {0x3510, 0x00},
  399. {0x3511, 0x02},
  400. {0x3512, 0x00},
  401. {0x3601, 0x55},
  402. {0x3602, 0x58},
  403. {0x3611, 0x58},
  404. {0x3614, 0x30},
  405. {0x3615, 0x77},
  406. {0x3621, 0x08},
  407. {0x3624, 0x40},
  408. {0x3633, 0x0c},
  409. {0x3634, 0x0c},
  410. {0x3635, 0x0c},
  411. {0x3636, 0x0c},
  412. {0x3638, 0x00},
  413. {0x3639, 0x00},
  414. {0x363a, 0x00},
  415. {0x363b, 0x00},
  416. {0x363c, 0xff},
  417. {0x363d, 0xfa},
  418. {0x3650, 0x44},
  419. {0x3651, 0x44},
  420. {0x3652, 0x44},
  421. {0x3653, 0x44},
  422. {0x3654, 0x44},
  423. {0x3655, 0x44},
  424. {0x3656, 0x44},
  425. {0x3657, 0x44},
  426. {0x3660, 0x00},
  427. {0x3661, 0x00},
  428. {0x3662, 0x00},
  429. {0x366a, 0x00},
  430. {0x366e, 0x0c},
  431. {0x3673, 0x04},
  432. {0x3700, 0x14},
  433. {0x3703, 0x0c},
  434. {0x3706, 0x24},
  435. {0x3714, 0x27},
  436. {0x3715, 0x01},
  437. {0x3716, 0x00},
  438. {0x3717, 0x02},
  439. {0x3733, 0x10},
  440. {0x3734, 0x40},
  441. {0x373f, 0xa0},
  442. {0x3765, 0x20},
  443. {0x37a1, 0x1d},
  444. {0x37a8, 0x26},
  445. {0x37ab, 0x14},
  446. {0x37c2, 0x04},
  447. {0x37c3, 0xf0},
  448. {0x37cb, 0x09},
  449. {0x37cc, 0x13},
  450. {0x37cd, 0x1f},
  451. {0x37ce, 0x1f},
  452. {0x3800, 0x00},
  453. {0x3801, 0x00},
  454. {0x3802, 0x00},
  455. {0x3803, 0x00},
  456. {0x3804, 0x0a},
  457. {0x3805, 0x3f},
  458. {0x3806, 0x07},
  459. {0x3807, 0xaf},
  460. {0x3808, 0x05},
  461. {0x3809, 0x10},
  462. {0x380a, 0x03},
  463. {0x380b, 0xcc},
  464. {0x380c, 0x02},
  465. {0x380d, 0xe4},
  466. {0x380e, 0x03},
  467. {0x380f, 0xf4},
  468. {0x3810, 0x00},
  469. {0x3811, 0x00},
  470. {0x3812, 0x00},
  471. {0x3813, 0x06},
  472. {0x3814, 0x03},
  473. {0x3815, 0x01},
  474. {0x3816, 0x03},
  475. {0x3817, 0x01},
  476. {0x3818, 0x00},
  477. {0x3819, 0x00},
  478. {0x381a, 0x00},
  479. {0x381b, 0x01},
  480. {0x3820, 0x8b},
  481. {0x3821, 0x01},
  482. {0x3c80, 0x08},
  483. {0x3c82, 0x00},
  484. {0x3c83, 0x00},
  485. {0x3c88, 0x00},
  486. {0x3d85, 0x14},
  487. {0x3f02, 0x08},
  488. {0x3f03, 0x10},
  489. {0x4008, 0x02},
  490. {0x4009, 0x09},
  491. {0x404e, 0x20},
  492. {0x4501, 0x00},
  493. {0x4502, 0x10},
  494. {0x4800, 0x00},
  495. {0x481f, 0x2a},
  496. {0x4837, 0x13},
  497. {0x5000, 0x13},
  498. {0x5780, 0x3e},
  499. {0x5781, 0x0f},
  500. {0x5782, 0x44},
  501. {0x5783, 0x02},
  502. {0x5784, 0x01},
  503. {0x5785, 0x01},
  504. {0x5786, 0x00},
  505. {0x5787, 0x04},
  506. {0x5788, 0x02},
  507. {0x5789, 0x0f},
  508. {0x578a, 0xfd},
  509. {0x578b, 0xf5},
  510. {0x578c, 0xf5},
  511. {0x578d, 0x03},
  512. {0x578e, 0x08},
  513. {0x578f, 0x0c},
  514. {0x5790, 0x08},
  515. {0x5791, 0x06},
  516. {0x5792, 0x00},
  517. {0x5793, 0x52},
  518. {0x5794, 0xa3},
  519. {0x5b00, 0x00},
  520. {0x5b01, 0x1c},
  521. {0x5b02, 0x00},
  522. {0x5b03, 0x7f},
  523. {0x5b05, 0x6c},
  524. {0x5e10, 0xfc},
  525. {0x4010, 0xf1},
  526. {0x3503, 0x08},
  527. {0x3505, 0x8c},
  528. {0x3507, 0x03},
  529. {0x3508, 0x00},
  530. {0x3509, 0xf8},
  531. {0x0100, 0x01},
  532. {REG_NULL, 0x00}
  533. };
  534. /*
  535. * Xclk 24Mhz
  536. * Pclk 45Mhz
  537. * linelength 672(0x2a0)
  538. * framelength 2232(0x8b8)
  539. * grabwindow_width 1280
  540. * grabwindow_height 720
  541. * max_framerate 30fps
  542. * mipi_datarate per lane 840Mbps
  543. */
  544. static const struct regval ov5695_1280x720_regs[] = {
  545. {0x3501, 0x45},
  546. {0x366e, 0x0c},
  547. {0x3800, 0x00},
  548. {0x3801, 0x00},
  549. {0x3802, 0x01},
  550. {0x3803, 0x00},
  551. {0x3804, 0x0a},
  552. {0x3805, 0x3f},
  553. {0x3806, 0x06},
  554. {0x3807, 0xaf},
  555. {0x3808, 0x05},
  556. {0x3809, 0x00},
  557. {0x380a, 0x02},
  558. {0x380b, 0xd0},
  559. {0x380c, 0x02},
  560. {0x380d, 0xa0},
  561. {0x380e, 0x08},
  562. {0x380f, 0xb8},
  563. {0x3811, 0x06},
  564. {0x3813, 0x02},
  565. {0x3814, 0x03},
  566. {0x3816, 0x03},
  567. {0x3817, 0x01},
  568. {0x3820, 0x8b},
  569. {0x3821, 0x01},
  570. {0x4501, 0x00},
  571. {0x4008, 0x02},
  572. {0x4009, 0x09},
  573. {REG_NULL, 0x00}
  574. };
  575. /*
  576. * Xclk 24Mhz
  577. * Pclk 45Mhz
  578. * linelength 672(0x2a0)
  579. * framelength 558(0x22e)
  580. * grabwindow_width 640
  581. * grabwindow_height 480
  582. * max_framerate 120fps
  583. * mipi_datarate per lane 840Mbps
  584. */
  585. static const struct regval ov5695_640x480_regs[] = {
  586. {0x3501, 0x22},
  587. {0x366e, 0x0c},
  588. {0x3800, 0x00},
  589. {0x3801, 0x00},
  590. {0x3802, 0x00},
  591. {0x3803, 0x08},
  592. {0x3804, 0x0a},
  593. {0x3805, 0x3f},
  594. {0x3806, 0x07},
  595. {0x3807, 0xa7},
  596. {0x3808, 0x02},
  597. {0x3809, 0x80},
  598. {0x380a, 0x01},
  599. {0x380b, 0xe0},
  600. {0x380c, 0x02},
  601. {0x380d, 0xa0},
  602. {0x380e, 0x02},
  603. {0x380f, 0x2e},
  604. {0x3811, 0x06},
  605. {0x3813, 0x04},
  606. {0x3814, 0x07},
  607. {0x3816, 0x05},
  608. {0x3817, 0x03},
  609. {0x3820, 0x8d},
  610. {0x3821, 0x01},
  611. {0x4501, 0x00},
  612. {0x4008, 0x02},
  613. {0x4009, 0x09},
  614. {REG_NULL, 0x00}
  615. };
  616. static const struct ov5695_mode supported_modes[] = {
  617. {
  618. .width = 2592,
  619. .height = 1944,
  620. .max_fps = 30,
  621. .exp_def = 0x0450,
  622. .hts_def = 0x02e4 * 4,
  623. .vts_def = 0x07e8,
  624. .reg_list = ov5695_2592x1944_regs,
  625. },
  626. {
  627. .width = 1920,
  628. .height = 1080,
  629. .max_fps = 30,
  630. .exp_def = 0x0450,
  631. .hts_def = 0x02a0 * 4,
  632. .vts_def = 0x08b8,
  633. .reg_list = ov5695_1920x1080_regs,
  634. },
  635. {
  636. .width = 1296,
  637. .height = 972,
  638. .max_fps = 60,
  639. .exp_def = 0x03e0,
  640. .hts_def = 0x02e4 * 4,
  641. .vts_def = 0x03f4,
  642. .reg_list = ov5695_1296x972_regs,
  643. },
  644. {
  645. .width = 1280,
  646. .height = 720,
  647. .max_fps = 30,
  648. .exp_def = 0x0450,
  649. .hts_def = 0x02a0 * 4,
  650. .vts_def = 0x08b8,
  651. .reg_list = ov5695_1280x720_regs,
  652. },
  653. {
  654. .width = 640,
  655. .height = 480,
  656. .max_fps = 120,
  657. .exp_def = 0x0450,
  658. .hts_def = 0x02a0 * 4,
  659. .vts_def = 0x022e,
  660. .reg_list = ov5695_640x480_regs,
  661. },
  662. };
  663. #define OV5695_LINK_FREQ_420MHZ 420000000
  664. static const s64 link_freq_menu_items[] = {
  665. OV5695_LINK_FREQ_420MHZ
  666. };
  667. static const char * const ov5695_test_pattern_menu[] = {
  668. "Disabled",
  669. "Vertical Color Bar Type 1",
  670. "Vertical Color Bar Type 2",
  671. "Vertical Color Bar Type 3",
  672. "Vertical Color Bar Type 4"
  673. };
  674. /* Write registers up to 4 at a time */
  675. static int ov5695_write_reg(struct i2c_client *client, u16 reg,
  676. u32 len, u32 val)
  677. {
  678. u32 buf_i, val_i;
  679. u8 buf[6];
  680. u8 *val_p;
  681. __be32 val_be;
  682. if (len > 4)
  683. return -EINVAL;
  684. buf[0] = reg >> 8;
  685. buf[1] = reg & 0xff;
  686. val_be = cpu_to_be32(val);
  687. val_p = (u8 *)&val_be;
  688. buf_i = 2;
  689. val_i = 4 - len;
  690. while (val_i < 4)
  691. buf[buf_i++] = val_p[val_i++];
  692. if (i2c_master_send(client, buf, len + 2) != len + 2)
  693. return -EIO;
  694. return 0;
  695. }
  696. static int ov5695_write_array(struct i2c_client *client,
  697. const struct regval *regs)
  698. {
  699. u32 i;
  700. int ret = 0;
  701. for (i = 0; ret == 0 && regs[i].addr != REG_NULL; i++)
  702. ret = ov5695_write_reg(client, regs[i].addr,
  703. OV5695_REG_VALUE_08BIT, regs[i].val);
  704. return ret;
  705. }
  706. /* Read registers up to 4 at a time */
  707. static int ov5695_read_reg(struct i2c_client *client, u16 reg, unsigned int len,
  708. u32 *val)
  709. {
  710. struct i2c_msg msgs[2];
  711. u8 *data_be_p;
  712. __be32 data_be = 0;
  713. __be16 reg_addr_be = cpu_to_be16(reg);
  714. int ret;
  715. if (len > 4)
  716. return -EINVAL;
  717. data_be_p = (u8 *)&data_be;
  718. /* Write register address */
  719. msgs[0].addr = client->addr;
  720. msgs[0].flags = 0;
  721. msgs[0].len = 2;
  722. msgs[0].buf = (u8 *)&reg_addr_be;
  723. /* Read data from register */
  724. msgs[1].addr = client->addr;
  725. msgs[1].flags = I2C_M_RD;
  726. msgs[1].len = len;
  727. msgs[1].buf = &data_be_p[4 - len];
  728. ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  729. if (ret != ARRAY_SIZE(msgs))
  730. return -EIO;
  731. *val = be32_to_cpu(data_be);
  732. return 0;
  733. }
  734. static int ov5695_get_reso_dist(const struct ov5695_mode *mode,
  735. struct v4l2_mbus_framefmt *framefmt)
  736. {
  737. return abs(mode->width - framefmt->width) +
  738. abs(mode->height - framefmt->height);
  739. }
  740. static const struct ov5695_mode *
  741. ov5695_find_best_fit(struct v4l2_subdev_format *fmt)
  742. {
  743. struct v4l2_mbus_framefmt *framefmt = &fmt->format;
  744. int dist;
  745. int cur_best_fit = 0;
  746. int cur_best_fit_dist = -1;
  747. int i;
  748. for (i = 0; i < ARRAY_SIZE(supported_modes); i++) {
  749. dist = ov5695_get_reso_dist(&supported_modes[i], framefmt);
  750. if (cur_best_fit_dist == -1 || dist < cur_best_fit_dist) {
  751. cur_best_fit_dist = dist;
  752. cur_best_fit = i;
  753. }
  754. }
  755. return &supported_modes[cur_best_fit];
  756. }
  757. static int ov5695_set_fmt(struct v4l2_subdev *sd,
  758. struct v4l2_subdev_pad_config *cfg,
  759. struct v4l2_subdev_format *fmt)
  760. {
  761. struct ov5695 *ov5695 = to_ov5695(sd);
  762. const struct ov5695_mode *mode;
  763. s64 h_blank, vblank_def;
  764. mutex_lock(&ov5695->mutex);
  765. mode = ov5695_find_best_fit(fmt);
  766. fmt->format.code = MEDIA_BUS_FMT_SBGGR10_1X10;
  767. fmt->format.width = mode->width;
  768. fmt->format.height = mode->height;
  769. fmt->format.field = V4L2_FIELD_NONE;
  770. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  771. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  772. *v4l2_subdev_get_try_format(sd, cfg, fmt->pad) = fmt->format;
  773. #else
  774. mutex_unlock(&ov5695->mutex);
  775. return -ENOTTY;
  776. #endif
  777. } else {
  778. ov5695->cur_mode = mode;
  779. h_blank = mode->hts_def - mode->width;
  780. __v4l2_ctrl_modify_range(ov5695->hblank, h_blank,
  781. h_blank, 1, h_blank);
  782. vblank_def = mode->vts_def - mode->height;
  783. __v4l2_ctrl_modify_range(ov5695->vblank, vblank_def,
  784. OV5695_VTS_MAX - mode->height,
  785. 1, vblank_def);
  786. }
  787. mutex_unlock(&ov5695->mutex);
  788. return 0;
  789. }
  790. static int ov5695_get_fmt(struct v4l2_subdev *sd,
  791. struct v4l2_subdev_pad_config *cfg,
  792. struct v4l2_subdev_format *fmt)
  793. {
  794. struct ov5695 *ov5695 = to_ov5695(sd);
  795. const struct ov5695_mode *mode = ov5695->cur_mode;
  796. mutex_lock(&ov5695->mutex);
  797. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  798. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  799. fmt->format = *v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
  800. #else
  801. mutex_unlock(&ov5695->mutex);
  802. return -ENOTTY;
  803. #endif
  804. } else {
  805. fmt->format.width = mode->width;
  806. fmt->format.height = mode->height;
  807. fmt->format.code = MEDIA_BUS_FMT_SBGGR10_1X10;
  808. fmt->format.field = V4L2_FIELD_NONE;
  809. }
  810. mutex_unlock(&ov5695->mutex);
  811. return 0;
  812. }
  813. static int ov5695_enum_mbus_code(struct v4l2_subdev *sd,
  814. struct v4l2_subdev_pad_config *cfg,
  815. struct v4l2_subdev_mbus_code_enum *code)
  816. {
  817. if (code->index != 0)
  818. return -EINVAL;
  819. code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
  820. return 0;
  821. }
  822. static int ov5695_enum_frame_sizes(struct v4l2_subdev *sd,
  823. struct v4l2_subdev_pad_config *cfg,
  824. struct v4l2_subdev_frame_size_enum *fse)
  825. {
  826. if (fse->index >= ARRAY_SIZE(supported_modes))
  827. return -EINVAL;
  828. if (fse->code != MEDIA_BUS_FMT_SBGGR10_1X10)
  829. return -EINVAL;
  830. fse->min_width = supported_modes[fse->index].width;
  831. fse->max_width = supported_modes[fse->index].width;
  832. fse->max_height = supported_modes[fse->index].height;
  833. fse->min_height = supported_modes[fse->index].height;
  834. return 0;
  835. }
  836. static int ov5695_enable_test_pattern(struct ov5695 *ov5695, u32 pattern)
  837. {
  838. u32 val;
  839. if (pattern)
  840. val = (pattern - 1) | OV5695_TEST_PATTERN_ENABLE;
  841. else
  842. val = OV5695_TEST_PATTERN_DISABLE;
  843. return ov5695_write_reg(ov5695->client, OV5695_REG_TEST_PATTERN,
  844. OV5695_REG_VALUE_08BIT, val);
  845. }
  846. static int __ov5695_start_stream(struct ov5695 *ov5695)
  847. {
  848. int ret;
  849. ret = ov5695_write_array(ov5695->client, ov5695_global_regs);
  850. if (ret)
  851. return ret;
  852. ret = ov5695_write_array(ov5695->client, ov5695->cur_mode->reg_list);
  853. if (ret)
  854. return ret;
  855. /* In case these controls are set before streaming */
  856. ret = __v4l2_ctrl_handler_setup(&ov5695->ctrl_handler);
  857. if (ret)
  858. return ret;
  859. return ov5695_write_reg(ov5695->client, OV5695_REG_CTRL_MODE,
  860. OV5695_REG_VALUE_08BIT, OV5695_MODE_STREAMING);
  861. }
  862. static int __ov5695_stop_stream(struct ov5695 *ov5695)
  863. {
  864. return ov5695_write_reg(ov5695->client, OV5695_REG_CTRL_MODE,
  865. OV5695_REG_VALUE_08BIT, OV5695_MODE_SW_STANDBY);
  866. }
  867. static int ov5695_s_stream(struct v4l2_subdev *sd, int on)
  868. {
  869. struct ov5695 *ov5695 = to_ov5695(sd);
  870. struct i2c_client *client = ov5695->client;
  871. int ret = 0;
  872. mutex_lock(&ov5695->mutex);
  873. on = !!on;
  874. if (on == ov5695->streaming)
  875. goto unlock_and_return;
  876. if (on) {
  877. ret = pm_runtime_get_sync(&client->dev);
  878. if (ret < 0) {
  879. pm_runtime_put_noidle(&client->dev);
  880. goto unlock_and_return;
  881. }
  882. ret = __ov5695_start_stream(ov5695);
  883. if (ret) {
  884. v4l2_err(sd, "start stream failed while write regs\n");
  885. pm_runtime_put(&client->dev);
  886. goto unlock_and_return;
  887. }
  888. } else {
  889. __ov5695_stop_stream(ov5695);
  890. pm_runtime_put(&client->dev);
  891. }
  892. ov5695->streaming = on;
  893. unlock_and_return:
  894. mutex_unlock(&ov5695->mutex);
  895. return ret;
  896. }
  897. static int __ov5695_power_on(struct ov5695 *ov5695)
  898. {
  899. int i, ret;
  900. struct device *dev = &ov5695->client->dev;
  901. ret = clk_prepare_enable(ov5695->xvclk);
  902. if (ret < 0) {
  903. dev_err(dev, "Failed to enable xvclk\n");
  904. return ret;
  905. }
  906. gpiod_set_value_cansleep(ov5695->reset_gpio, 1);
  907. /*
  908. * The hardware requires the regulators to be powered on in order,
  909. * so enable them one by one.
  910. */
  911. for (i = 0; i < OV5695_NUM_SUPPLIES; i++) {
  912. ret = regulator_enable(ov5695->supplies[i].consumer);
  913. if (ret) {
  914. dev_err(dev, "Failed to enable %s: %d\n",
  915. ov5695->supplies[i].supply, ret);
  916. goto disable_reg_clk;
  917. }
  918. }
  919. gpiod_set_value_cansleep(ov5695->reset_gpio, 0);
  920. usleep_range(1000, 1200);
  921. return 0;
  922. disable_reg_clk:
  923. for (--i; i >= 0; i--)
  924. regulator_disable(ov5695->supplies[i].consumer);
  925. clk_disable_unprepare(ov5695->xvclk);
  926. return ret;
  927. }
  928. static void __ov5695_power_off(struct ov5695 *ov5695)
  929. {
  930. struct device *dev = &ov5695->client->dev;
  931. int i, ret;
  932. clk_disable_unprepare(ov5695->xvclk);
  933. gpiod_set_value_cansleep(ov5695->reset_gpio, 1);
  934. /*
  935. * The hardware requires the regulators to be powered off in order,
  936. * so disable them one by one.
  937. */
  938. for (i = OV5695_NUM_SUPPLIES - 1; i >= 0; i--) {
  939. ret = regulator_disable(ov5695->supplies[i].consumer);
  940. if (ret)
  941. dev_err(dev, "Failed to disable %s: %d\n",
  942. ov5695->supplies[i].supply, ret);
  943. }
  944. }
  945. static int __maybe_unused ov5695_runtime_resume(struct device *dev)
  946. {
  947. struct i2c_client *client = to_i2c_client(dev);
  948. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  949. struct ov5695 *ov5695 = to_ov5695(sd);
  950. return __ov5695_power_on(ov5695);
  951. }
  952. static int __maybe_unused ov5695_runtime_suspend(struct device *dev)
  953. {
  954. struct i2c_client *client = to_i2c_client(dev);
  955. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  956. struct ov5695 *ov5695 = to_ov5695(sd);
  957. __ov5695_power_off(ov5695);
  958. return 0;
  959. }
  960. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  961. static int ov5695_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  962. {
  963. struct ov5695 *ov5695 = to_ov5695(sd);
  964. struct v4l2_mbus_framefmt *try_fmt =
  965. v4l2_subdev_get_try_format(sd, fh->pad, 0);
  966. const struct ov5695_mode *def_mode = &supported_modes[0];
  967. mutex_lock(&ov5695->mutex);
  968. /* Initialize try_fmt */
  969. try_fmt->width = def_mode->width;
  970. try_fmt->height = def_mode->height;
  971. try_fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
  972. try_fmt->field = V4L2_FIELD_NONE;
  973. mutex_unlock(&ov5695->mutex);
  974. /* No crop or compose */
  975. return 0;
  976. }
  977. #endif
  978. static const struct dev_pm_ops ov5695_pm_ops = {
  979. SET_RUNTIME_PM_OPS(ov5695_runtime_suspend,
  980. ov5695_runtime_resume, NULL)
  981. };
  982. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  983. static const struct v4l2_subdev_internal_ops ov5695_internal_ops = {
  984. .open = ov5695_open,
  985. };
  986. #endif
  987. static const struct v4l2_subdev_video_ops ov5695_video_ops = {
  988. .s_stream = ov5695_s_stream,
  989. };
  990. static const struct v4l2_subdev_pad_ops ov5695_pad_ops = {
  991. .enum_mbus_code = ov5695_enum_mbus_code,
  992. .enum_frame_size = ov5695_enum_frame_sizes,
  993. .get_fmt = ov5695_get_fmt,
  994. .set_fmt = ov5695_set_fmt,
  995. };
  996. static const struct v4l2_subdev_ops ov5695_subdev_ops = {
  997. .video = &ov5695_video_ops,
  998. .pad = &ov5695_pad_ops,
  999. };
  1000. static int ov5695_set_ctrl(struct v4l2_ctrl *ctrl)
  1001. {
  1002. struct ov5695 *ov5695 = container_of(ctrl->handler,
  1003. struct ov5695, ctrl_handler);
  1004. struct i2c_client *client = ov5695->client;
  1005. s64 max;
  1006. int ret = 0;
  1007. /* Propagate change of current control to all related controls */
  1008. switch (ctrl->id) {
  1009. case V4L2_CID_VBLANK:
  1010. /* Update max exposure while meeting expected vblanking */
  1011. max = ov5695->cur_mode->height + ctrl->val - 4;
  1012. __v4l2_ctrl_modify_range(ov5695->exposure,
  1013. ov5695->exposure->minimum, max,
  1014. ov5695->exposure->step,
  1015. ov5695->exposure->default_value);
  1016. break;
  1017. }
  1018. if (!pm_runtime_get_if_in_use(&client->dev))
  1019. return 0;
  1020. switch (ctrl->id) {
  1021. case V4L2_CID_EXPOSURE:
  1022. /* 4 least significant bits of expsoure are fractional part */
  1023. ret = ov5695_write_reg(ov5695->client, OV5695_REG_EXPOSURE,
  1024. OV5695_REG_VALUE_24BIT, ctrl->val << 4);
  1025. break;
  1026. case V4L2_CID_ANALOGUE_GAIN:
  1027. ret = ov5695_write_reg(ov5695->client, OV5695_REG_ANALOG_GAIN,
  1028. OV5695_REG_VALUE_08BIT, ctrl->val);
  1029. break;
  1030. case V4L2_CID_DIGITAL_GAIN:
  1031. ret = ov5695_write_reg(ov5695->client, OV5695_REG_DIGI_GAIN_L,
  1032. OV5695_REG_VALUE_08BIT,
  1033. ctrl->val & OV5695_DIGI_GAIN_L_MASK);
  1034. ret = ov5695_write_reg(ov5695->client, OV5695_REG_DIGI_GAIN_H,
  1035. OV5695_REG_VALUE_08BIT,
  1036. ctrl->val >> OV5695_DIGI_GAIN_H_SHIFT);
  1037. break;
  1038. case V4L2_CID_VBLANK:
  1039. ret = ov5695_write_reg(ov5695->client, OV5695_REG_VTS,
  1040. OV5695_REG_VALUE_16BIT,
  1041. ctrl->val + ov5695->cur_mode->height);
  1042. break;
  1043. case V4L2_CID_TEST_PATTERN:
  1044. ret = ov5695_enable_test_pattern(ov5695, ctrl->val);
  1045. break;
  1046. default:
  1047. dev_warn(&client->dev, "%s Unhandled id:0x%x, val:0x%x\n",
  1048. __func__, ctrl->id, ctrl->val);
  1049. break;
  1050. };
  1051. pm_runtime_put(&client->dev);
  1052. return ret;
  1053. }
  1054. static const struct v4l2_ctrl_ops ov5695_ctrl_ops = {
  1055. .s_ctrl = ov5695_set_ctrl,
  1056. };
  1057. static int ov5695_initialize_controls(struct ov5695 *ov5695)
  1058. {
  1059. const struct ov5695_mode *mode;
  1060. struct v4l2_ctrl_handler *handler;
  1061. struct v4l2_ctrl *ctrl;
  1062. s64 exposure_max, vblank_def;
  1063. u32 h_blank;
  1064. int ret;
  1065. handler = &ov5695->ctrl_handler;
  1066. mode = ov5695->cur_mode;
  1067. ret = v4l2_ctrl_handler_init(handler, 8);
  1068. if (ret)
  1069. return ret;
  1070. handler->lock = &ov5695->mutex;
  1071. ctrl = v4l2_ctrl_new_int_menu(handler, NULL, V4L2_CID_LINK_FREQ,
  1072. 0, 0, link_freq_menu_items);
  1073. if (ctrl)
  1074. ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  1075. v4l2_ctrl_new_std(handler, NULL, V4L2_CID_PIXEL_RATE,
  1076. 0, OV5695_PIXEL_RATE, 1, OV5695_PIXEL_RATE);
  1077. h_blank = mode->hts_def - mode->width;
  1078. ov5695->hblank = v4l2_ctrl_new_std(handler, NULL, V4L2_CID_HBLANK,
  1079. h_blank, h_blank, 1, h_blank);
  1080. if (ov5695->hblank)
  1081. ov5695->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  1082. vblank_def = mode->vts_def - mode->height;
  1083. ov5695->vblank = v4l2_ctrl_new_std(handler, &ov5695_ctrl_ops,
  1084. V4L2_CID_VBLANK, vblank_def,
  1085. OV5695_VTS_MAX - mode->height,
  1086. 1, vblank_def);
  1087. exposure_max = mode->vts_def - 4;
  1088. ov5695->exposure = v4l2_ctrl_new_std(handler, &ov5695_ctrl_ops,
  1089. V4L2_CID_EXPOSURE, OV5695_EXPOSURE_MIN,
  1090. exposure_max, OV5695_EXPOSURE_STEP,
  1091. mode->exp_def);
  1092. ov5695->anal_gain = v4l2_ctrl_new_std(handler, &ov5695_ctrl_ops,
  1093. V4L2_CID_ANALOGUE_GAIN, ANALOG_GAIN_MIN,
  1094. ANALOG_GAIN_MAX, ANALOG_GAIN_STEP,
  1095. ANALOG_GAIN_DEFAULT);
  1096. /* Digital gain */
  1097. ov5695->digi_gain = v4l2_ctrl_new_std(handler, &ov5695_ctrl_ops,
  1098. V4L2_CID_DIGITAL_GAIN, OV5695_DIGI_GAIN_MIN,
  1099. OV5695_DIGI_GAIN_MAX, OV5695_DIGI_GAIN_STEP,
  1100. OV5695_DIGI_GAIN_DEFAULT);
  1101. ov5695->test_pattern = v4l2_ctrl_new_std_menu_items(handler,
  1102. &ov5695_ctrl_ops, V4L2_CID_TEST_PATTERN,
  1103. ARRAY_SIZE(ov5695_test_pattern_menu) - 1,
  1104. 0, 0, ov5695_test_pattern_menu);
  1105. if (handler->error) {
  1106. ret = handler->error;
  1107. dev_err(&ov5695->client->dev,
  1108. "Failed to init controls(%d)\n", ret);
  1109. goto err_free_handler;
  1110. }
  1111. ov5695->subdev.ctrl_handler = handler;
  1112. return 0;
  1113. err_free_handler:
  1114. v4l2_ctrl_handler_free(handler);
  1115. return ret;
  1116. }
  1117. static int ov5695_check_sensor_id(struct ov5695 *ov5695,
  1118. struct i2c_client *client)
  1119. {
  1120. struct device *dev = &ov5695->client->dev;
  1121. u32 id = 0;
  1122. int ret;
  1123. ret = ov5695_read_reg(client, OV5695_REG_CHIP_ID,
  1124. OV5695_REG_VALUE_24BIT, &id);
  1125. if (id != CHIP_ID) {
  1126. dev_err(dev, "Unexpected sensor id(%06x), ret(%d)\n", id, ret);
  1127. return ret;
  1128. }
  1129. dev_info(dev, "Detected OV%06x sensor\n", CHIP_ID);
  1130. return 0;
  1131. }
  1132. static int ov5695_configure_regulators(struct ov5695 *ov5695)
  1133. {
  1134. int i;
  1135. for (i = 0; i < OV5695_NUM_SUPPLIES; i++)
  1136. ov5695->supplies[i].supply = ov5695_supply_names[i];
  1137. return devm_regulator_bulk_get(&ov5695->client->dev,
  1138. OV5695_NUM_SUPPLIES,
  1139. ov5695->supplies);
  1140. }
  1141. static int ov5695_probe(struct i2c_client *client,
  1142. const struct i2c_device_id *id)
  1143. {
  1144. struct device *dev = &client->dev;
  1145. struct ov5695 *ov5695;
  1146. struct v4l2_subdev *sd;
  1147. int ret;
  1148. ov5695 = devm_kzalloc(dev, sizeof(*ov5695), GFP_KERNEL);
  1149. if (!ov5695)
  1150. return -ENOMEM;
  1151. ov5695->client = client;
  1152. ov5695->cur_mode = &supported_modes[0];
  1153. ov5695->xvclk = devm_clk_get(dev, "xvclk");
  1154. if (IS_ERR(ov5695->xvclk)) {
  1155. dev_err(dev, "Failed to get xvclk\n");
  1156. return -EINVAL;
  1157. }
  1158. ret = clk_set_rate(ov5695->xvclk, OV5695_XVCLK_FREQ);
  1159. if (ret < 0) {
  1160. dev_err(dev, "Failed to set xvclk rate (24MHz)\n");
  1161. return ret;
  1162. }
  1163. if (clk_get_rate(ov5695->xvclk) != OV5695_XVCLK_FREQ)
  1164. dev_warn(dev, "xvclk mismatched, modes are based on 24MHz\n");
  1165. ov5695->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
  1166. if (IS_ERR(ov5695->reset_gpio)) {
  1167. dev_err(dev, "Failed to get reset-gpios\n");
  1168. return -EINVAL;
  1169. }
  1170. ret = ov5695_configure_regulators(ov5695);
  1171. if (ret) {
  1172. dev_err(dev, "Failed to get power regulators\n");
  1173. return ret;
  1174. }
  1175. mutex_init(&ov5695->mutex);
  1176. sd = &ov5695->subdev;
  1177. v4l2_i2c_subdev_init(sd, client, &ov5695_subdev_ops);
  1178. ret = ov5695_initialize_controls(ov5695);
  1179. if (ret)
  1180. goto err_destroy_mutex;
  1181. ret = __ov5695_power_on(ov5695);
  1182. if (ret)
  1183. goto err_free_handler;
  1184. ret = ov5695_check_sensor_id(ov5695, client);
  1185. if (ret)
  1186. goto err_power_off;
  1187. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  1188. sd->internal_ops = &ov5695_internal_ops;
  1189. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  1190. #endif
  1191. #if defined(CONFIG_MEDIA_CONTROLLER)
  1192. ov5695->pad.flags = MEDIA_PAD_FL_SOURCE;
  1193. sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
  1194. ret = media_entity_pads_init(&sd->entity, 1, &ov5695->pad);
  1195. if (ret < 0)
  1196. goto err_power_off;
  1197. #endif
  1198. ret = v4l2_async_register_subdev(sd);
  1199. if (ret) {
  1200. dev_err(dev, "v4l2 async register subdev failed\n");
  1201. goto err_clean_entity;
  1202. }
  1203. pm_runtime_set_active(dev);
  1204. pm_runtime_enable(dev);
  1205. pm_runtime_idle(dev);
  1206. return 0;
  1207. err_clean_entity:
  1208. #if defined(CONFIG_MEDIA_CONTROLLER)
  1209. media_entity_cleanup(&sd->entity);
  1210. #endif
  1211. err_power_off:
  1212. __ov5695_power_off(ov5695);
  1213. err_free_handler:
  1214. v4l2_ctrl_handler_free(&ov5695->ctrl_handler);
  1215. err_destroy_mutex:
  1216. mutex_destroy(&ov5695->mutex);
  1217. return ret;
  1218. }
  1219. static int ov5695_remove(struct i2c_client *client)
  1220. {
  1221. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1222. struct ov5695 *ov5695 = to_ov5695(sd);
  1223. v4l2_async_unregister_subdev(sd);
  1224. #if defined(CONFIG_MEDIA_CONTROLLER)
  1225. media_entity_cleanup(&sd->entity);
  1226. #endif
  1227. v4l2_ctrl_handler_free(&ov5695->ctrl_handler);
  1228. mutex_destroy(&ov5695->mutex);
  1229. pm_runtime_disable(&client->dev);
  1230. if (!pm_runtime_status_suspended(&client->dev))
  1231. __ov5695_power_off(ov5695);
  1232. pm_runtime_set_suspended(&client->dev);
  1233. return 0;
  1234. }
  1235. #if IS_ENABLED(CONFIG_OF)
  1236. static const struct of_device_id ov5695_of_match[] = {
  1237. { .compatible = "ovti,ov5695" },
  1238. {},
  1239. };
  1240. MODULE_DEVICE_TABLE(of, ov5695_of_match);
  1241. #endif
  1242. static struct i2c_driver ov5695_i2c_driver = {
  1243. .driver = {
  1244. .name = "ov5695",
  1245. .pm = &ov5695_pm_ops,
  1246. .of_match_table = of_match_ptr(ov5695_of_match),
  1247. },
  1248. .probe = &ov5695_probe,
  1249. .remove = &ov5695_remove,
  1250. };
  1251. module_i2c_driver(ov5695_i2c_driver);
  1252. MODULE_DESCRIPTION("OmniVision ov5695 sensor driver");
  1253. MODULE_LICENSE("GPL v2");