ov5695.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398
  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. /* Calculate the delay in us by clock rate and clock cycles */
  898. static inline u32 ov5695_cal_delay(u32 cycles)
  899. {
  900. return DIV_ROUND_UP(cycles, OV5695_XVCLK_FREQ / 1000 / 1000);
  901. }
  902. static int __ov5695_power_on(struct ov5695 *ov5695)
  903. {
  904. int ret;
  905. u32 delay_us;
  906. struct device *dev = &ov5695->client->dev;
  907. ret = clk_prepare_enable(ov5695->xvclk);
  908. if (ret < 0) {
  909. dev_err(dev, "Failed to enable xvclk\n");
  910. return ret;
  911. }
  912. gpiod_set_value_cansleep(ov5695->reset_gpio, 1);
  913. ret = regulator_bulk_enable(OV5695_NUM_SUPPLIES, ov5695->supplies);
  914. if (ret < 0) {
  915. dev_err(dev, "Failed to enable regulators\n");
  916. goto disable_clk;
  917. }
  918. gpiod_set_value_cansleep(ov5695->reset_gpio, 0);
  919. /* 8192 cycles prior to first SCCB transaction */
  920. delay_us = ov5695_cal_delay(8192);
  921. usleep_range(delay_us, delay_us * 2);
  922. return 0;
  923. disable_clk:
  924. clk_disable_unprepare(ov5695->xvclk);
  925. return ret;
  926. }
  927. static void __ov5695_power_off(struct ov5695 *ov5695)
  928. {
  929. clk_disable_unprepare(ov5695->xvclk);
  930. gpiod_set_value_cansleep(ov5695->reset_gpio, 1);
  931. regulator_bulk_disable(OV5695_NUM_SUPPLIES, ov5695->supplies);
  932. }
  933. static int __maybe_unused ov5695_runtime_resume(struct device *dev)
  934. {
  935. struct i2c_client *client = to_i2c_client(dev);
  936. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  937. struct ov5695 *ov5695 = to_ov5695(sd);
  938. return __ov5695_power_on(ov5695);
  939. }
  940. static int __maybe_unused ov5695_runtime_suspend(struct device *dev)
  941. {
  942. struct i2c_client *client = to_i2c_client(dev);
  943. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  944. struct ov5695 *ov5695 = to_ov5695(sd);
  945. __ov5695_power_off(ov5695);
  946. return 0;
  947. }
  948. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  949. static int ov5695_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  950. {
  951. struct ov5695 *ov5695 = to_ov5695(sd);
  952. struct v4l2_mbus_framefmt *try_fmt =
  953. v4l2_subdev_get_try_format(sd, fh->pad, 0);
  954. const struct ov5695_mode *def_mode = &supported_modes[0];
  955. mutex_lock(&ov5695->mutex);
  956. /* Initialize try_fmt */
  957. try_fmt->width = def_mode->width;
  958. try_fmt->height = def_mode->height;
  959. try_fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
  960. try_fmt->field = V4L2_FIELD_NONE;
  961. mutex_unlock(&ov5695->mutex);
  962. /* No crop or compose */
  963. return 0;
  964. }
  965. #endif
  966. static const struct dev_pm_ops ov5695_pm_ops = {
  967. SET_RUNTIME_PM_OPS(ov5695_runtime_suspend,
  968. ov5695_runtime_resume, NULL)
  969. };
  970. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  971. static const struct v4l2_subdev_internal_ops ov5695_internal_ops = {
  972. .open = ov5695_open,
  973. };
  974. #endif
  975. static const struct v4l2_subdev_video_ops ov5695_video_ops = {
  976. .s_stream = ov5695_s_stream,
  977. };
  978. static const struct v4l2_subdev_pad_ops ov5695_pad_ops = {
  979. .enum_mbus_code = ov5695_enum_mbus_code,
  980. .enum_frame_size = ov5695_enum_frame_sizes,
  981. .get_fmt = ov5695_get_fmt,
  982. .set_fmt = ov5695_set_fmt,
  983. };
  984. static const struct v4l2_subdev_ops ov5695_subdev_ops = {
  985. .video = &ov5695_video_ops,
  986. .pad = &ov5695_pad_ops,
  987. };
  988. static int ov5695_set_ctrl(struct v4l2_ctrl *ctrl)
  989. {
  990. struct ov5695 *ov5695 = container_of(ctrl->handler,
  991. struct ov5695, ctrl_handler);
  992. struct i2c_client *client = ov5695->client;
  993. s64 max;
  994. int ret = 0;
  995. /* Propagate change of current control to all related controls */
  996. switch (ctrl->id) {
  997. case V4L2_CID_VBLANK:
  998. /* Update max exposure while meeting expected vblanking */
  999. max = ov5695->cur_mode->height + ctrl->val - 4;
  1000. __v4l2_ctrl_modify_range(ov5695->exposure,
  1001. ov5695->exposure->minimum, max,
  1002. ov5695->exposure->step,
  1003. ov5695->exposure->default_value);
  1004. break;
  1005. }
  1006. if (pm_runtime_get_if_in_use(&client->dev) <= 0)
  1007. return 0;
  1008. switch (ctrl->id) {
  1009. case V4L2_CID_EXPOSURE:
  1010. /* 4 least significant bits of expsoure are fractional part */
  1011. ret = ov5695_write_reg(ov5695->client, OV5695_REG_EXPOSURE,
  1012. OV5695_REG_VALUE_24BIT, ctrl->val << 4);
  1013. break;
  1014. case V4L2_CID_ANALOGUE_GAIN:
  1015. ret = ov5695_write_reg(ov5695->client, OV5695_REG_ANALOG_GAIN,
  1016. OV5695_REG_VALUE_08BIT, ctrl->val);
  1017. break;
  1018. case V4L2_CID_DIGITAL_GAIN:
  1019. ret = ov5695_write_reg(ov5695->client, OV5695_REG_DIGI_GAIN_L,
  1020. OV5695_REG_VALUE_08BIT,
  1021. ctrl->val & OV5695_DIGI_GAIN_L_MASK);
  1022. ret = ov5695_write_reg(ov5695->client, OV5695_REG_DIGI_GAIN_H,
  1023. OV5695_REG_VALUE_08BIT,
  1024. ctrl->val >> OV5695_DIGI_GAIN_H_SHIFT);
  1025. break;
  1026. case V4L2_CID_VBLANK:
  1027. ret = ov5695_write_reg(ov5695->client, OV5695_REG_VTS,
  1028. OV5695_REG_VALUE_16BIT,
  1029. ctrl->val + ov5695->cur_mode->height);
  1030. break;
  1031. case V4L2_CID_TEST_PATTERN:
  1032. ret = ov5695_enable_test_pattern(ov5695, ctrl->val);
  1033. break;
  1034. default:
  1035. dev_warn(&client->dev, "%s Unhandled id:0x%x, val:0x%x\n",
  1036. __func__, ctrl->id, ctrl->val);
  1037. break;
  1038. };
  1039. pm_runtime_put(&client->dev);
  1040. return ret;
  1041. }
  1042. static const struct v4l2_ctrl_ops ov5695_ctrl_ops = {
  1043. .s_ctrl = ov5695_set_ctrl,
  1044. };
  1045. static int ov5695_initialize_controls(struct ov5695 *ov5695)
  1046. {
  1047. const struct ov5695_mode *mode;
  1048. struct v4l2_ctrl_handler *handler;
  1049. struct v4l2_ctrl *ctrl;
  1050. s64 exposure_max, vblank_def;
  1051. u32 h_blank;
  1052. int ret;
  1053. handler = &ov5695->ctrl_handler;
  1054. mode = ov5695->cur_mode;
  1055. ret = v4l2_ctrl_handler_init(handler, 8);
  1056. if (ret)
  1057. return ret;
  1058. handler->lock = &ov5695->mutex;
  1059. ctrl = v4l2_ctrl_new_int_menu(handler, NULL, V4L2_CID_LINK_FREQ,
  1060. 0, 0, link_freq_menu_items);
  1061. if (ctrl)
  1062. ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  1063. v4l2_ctrl_new_std(handler, NULL, V4L2_CID_PIXEL_RATE,
  1064. 0, OV5695_PIXEL_RATE, 1, OV5695_PIXEL_RATE);
  1065. h_blank = mode->hts_def - mode->width;
  1066. ov5695->hblank = v4l2_ctrl_new_std(handler, NULL, V4L2_CID_HBLANK,
  1067. h_blank, h_blank, 1, h_blank);
  1068. if (ov5695->hblank)
  1069. ov5695->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  1070. vblank_def = mode->vts_def - mode->height;
  1071. ov5695->vblank = v4l2_ctrl_new_std(handler, &ov5695_ctrl_ops,
  1072. V4L2_CID_VBLANK, vblank_def,
  1073. OV5695_VTS_MAX - mode->height,
  1074. 1, vblank_def);
  1075. exposure_max = mode->vts_def - 4;
  1076. ov5695->exposure = v4l2_ctrl_new_std(handler, &ov5695_ctrl_ops,
  1077. V4L2_CID_EXPOSURE, OV5695_EXPOSURE_MIN,
  1078. exposure_max, OV5695_EXPOSURE_STEP,
  1079. mode->exp_def);
  1080. ov5695->anal_gain = v4l2_ctrl_new_std(handler, &ov5695_ctrl_ops,
  1081. V4L2_CID_ANALOGUE_GAIN, ANALOG_GAIN_MIN,
  1082. ANALOG_GAIN_MAX, ANALOG_GAIN_STEP,
  1083. ANALOG_GAIN_DEFAULT);
  1084. /* Digital gain */
  1085. ov5695->digi_gain = v4l2_ctrl_new_std(handler, &ov5695_ctrl_ops,
  1086. V4L2_CID_DIGITAL_GAIN, OV5695_DIGI_GAIN_MIN,
  1087. OV5695_DIGI_GAIN_MAX, OV5695_DIGI_GAIN_STEP,
  1088. OV5695_DIGI_GAIN_DEFAULT);
  1089. ov5695->test_pattern = v4l2_ctrl_new_std_menu_items(handler,
  1090. &ov5695_ctrl_ops, V4L2_CID_TEST_PATTERN,
  1091. ARRAY_SIZE(ov5695_test_pattern_menu) - 1,
  1092. 0, 0, ov5695_test_pattern_menu);
  1093. if (handler->error) {
  1094. ret = handler->error;
  1095. dev_err(&ov5695->client->dev,
  1096. "Failed to init controls(%d)\n", ret);
  1097. goto err_free_handler;
  1098. }
  1099. ov5695->subdev.ctrl_handler = handler;
  1100. return 0;
  1101. err_free_handler:
  1102. v4l2_ctrl_handler_free(handler);
  1103. return ret;
  1104. }
  1105. static int ov5695_check_sensor_id(struct ov5695 *ov5695,
  1106. struct i2c_client *client)
  1107. {
  1108. struct device *dev = &ov5695->client->dev;
  1109. u32 id = 0;
  1110. int ret;
  1111. ret = ov5695_read_reg(client, OV5695_REG_CHIP_ID,
  1112. OV5695_REG_VALUE_24BIT, &id);
  1113. if (id != CHIP_ID) {
  1114. dev_err(dev, "Unexpected sensor id(%06x), ret(%d)\n", id, ret);
  1115. return ret;
  1116. }
  1117. dev_info(dev, "Detected OV%06x sensor\n", CHIP_ID);
  1118. return 0;
  1119. }
  1120. static int ov5695_configure_regulators(struct ov5695 *ov5695)
  1121. {
  1122. int i;
  1123. for (i = 0; i < OV5695_NUM_SUPPLIES; i++)
  1124. ov5695->supplies[i].supply = ov5695_supply_names[i];
  1125. return devm_regulator_bulk_get(&ov5695->client->dev,
  1126. OV5695_NUM_SUPPLIES,
  1127. ov5695->supplies);
  1128. }
  1129. static int ov5695_probe(struct i2c_client *client,
  1130. const struct i2c_device_id *id)
  1131. {
  1132. struct device *dev = &client->dev;
  1133. struct ov5695 *ov5695;
  1134. struct v4l2_subdev *sd;
  1135. int ret;
  1136. ov5695 = devm_kzalloc(dev, sizeof(*ov5695), GFP_KERNEL);
  1137. if (!ov5695)
  1138. return -ENOMEM;
  1139. ov5695->client = client;
  1140. ov5695->cur_mode = &supported_modes[0];
  1141. ov5695->xvclk = devm_clk_get(dev, "xvclk");
  1142. if (IS_ERR(ov5695->xvclk)) {
  1143. dev_err(dev, "Failed to get xvclk\n");
  1144. return -EINVAL;
  1145. }
  1146. ret = clk_set_rate(ov5695->xvclk, OV5695_XVCLK_FREQ);
  1147. if (ret < 0) {
  1148. dev_err(dev, "Failed to set xvclk rate (24MHz)\n");
  1149. return ret;
  1150. }
  1151. if (clk_get_rate(ov5695->xvclk) != OV5695_XVCLK_FREQ)
  1152. dev_warn(dev, "xvclk mismatched, modes are based on 24MHz\n");
  1153. ov5695->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
  1154. if (IS_ERR(ov5695->reset_gpio)) {
  1155. dev_err(dev, "Failed to get reset-gpios\n");
  1156. return -EINVAL;
  1157. }
  1158. ret = ov5695_configure_regulators(ov5695);
  1159. if (ret) {
  1160. dev_err(dev, "Failed to get power regulators\n");
  1161. return ret;
  1162. }
  1163. mutex_init(&ov5695->mutex);
  1164. sd = &ov5695->subdev;
  1165. v4l2_i2c_subdev_init(sd, client, &ov5695_subdev_ops);
  1166. ret = ov5695_initialize_controls(ov5695);
  1167. if (ret)
  1168. goto err_destroy_mutex;
  1169. ret = __ov5695_power_on(ov5695);
  1170. if (ret)
  1171. goto err_free_handler;
  1172. ret = ov5695_check_sensor_id(ov5695, client);
  1173. if (ret)
  1174. goto err_power_off;
  1175. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  1176. sd->internal_ops = &ov5695_internal_ops;
  1177. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  1178. #endif
  1179. #if defined(CONFIG_MEDIA_CONTROLLER)
  1180. ov5695->pad.flags = MEDIA_PAD_FL_SOURCE;
  1181. sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
  1182. ret = media_entity_pads_init(&sd->entity, 1, &ov5695->pad);
  1183. if (ret < 0)
  1184. goto err_power_off;
  1185. #endif
  1186. ret = v4l2_async_register_subdev(sd);
  1187. if (ret) {
  1188. dev_err(dev, "v4l2 async register subdev failed\n");
  1189. goto err_clean_entity;
  1190. }
  1191. pm_runtime_set_active(dev);
  1192. pm_runtime_enable(dev);
  1193. pm_runtime_idle(dev);
  1194. return 0;
  1195. err_clean_entity:
  1196. #if defined(CONFIG_MEDIA_CONTROLLER)
  1197. media_entity_cleanup(&sd->entity);
  1198. #endif
  1199. err_power_off:
  1200. __ov5695_power_off(ov5695);
  1201. err_free_handler:
  1202. v4l2_ctrl_handler_free(&ov5695->ctrl_handler);
  1203. err_destroy_mutex:
  1204. mutex_destroy(&ov5695->mutex);
  1205. return ret;
  1206. }
  1207. static int ov5695_remove(struct i2c_client *client)
  1208. {
  1209. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1210. struct ov5695 *ov5695 = to_ov5695(sd);
  1211. v4l2_async_unregister_subdev(sd);
  1212. #if defined(CONFIG_MEDIA_CONTROLLER)
  1213. media_entity_cleanup(&sd->entity);
  1214. #endif
  1215. v4l2_ctrl_handler_free(&ov5695->ctrl_handler);
  1216. mutex_destroy(&ov5695->mutex);
  1217. pm_runtime_disable(&client->dev);
  1218. if (!pm_runtime_status_suspended(&client->dev))
  1219. __ov5695_power_off(ov5695);
  1220. pm_runtime_set_suspended(&client->dev);
  1221. return 0;
  1222. }
  1223. #if IS_ENABLED(CONFIG_OF)
  1224. static const struct of_device_id ov5695_of_match[] = {
  1225. { .compatible = "ovti,ov5695" },
  1226. {},
  1227. };
  1228. MODULE_DEVICE_TABLE(of, ov5695_of_match);
  1229. #endif
  1230. static struct i2c_driver ov5695_i2c_driver = {
  1231. .driver = {
  1232. .name = "ov5695",
  1233. .pm = &ov5695_pm_ops,
  1234. .of_match_table = of_match_ptr(ov5695_of_match),
  1235. },
  1236. .probe = &ov5695_probe,
  1237. .remove = &ov5695_remove,
  1238. };
  1239. module_i2c_driver(ov5695_i2c_driver);
  1240. MODULE_DESCRIPTION("OmniVision ov5695 sensor driver");
  1241. MODULE_LICENSE("GPL v2");