ov5695.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403
  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_state *sd_state,
  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, sd_state, fmt->pad) = fmt->format;
  773. #endif
  774. } else {
  775. ov5695->cur_mode = mode;
  776. h_blank = mode->hts_def - mode->width;
  777. __v4l2_ctrl_modify_range(ov5695->hblank, h_blank,
  778. h_blank, 1, h_blank);
  779. vblank_def = mode->vts_def - mode->height;
  780. __v4l2_ctrl_modify_range(ov5695->vblank, vblank_def,
  781. OV5695_VTS_MAX - mode->height,
  782. 1, vblank_def);
  783. }
  784. mutex_unlock(&ov5695->mutex);
  785. return 0;
  786. }
  787. static int ov5695_get_fmt(struct v4l2_subdev *sd,
  788. struct v4l2_subdev_state *sd_state,
  789. struct v4l2_subdev_format *fmt)
  790. {
  791. struct ov5695 *ov5695 = to_ov5695(sd);
  792. const struct ov5695_mode *mode = ov5695->cur_mode;
  793. mutex_lock(&ov5695->mutex);
  794. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  795. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  796. fmt->format = *v4l2_subdev_get_try_format(sd, sd_state,
  797. fmt->pad);
  798. #else
  799. mutex_unlock(&ov5695->mutex);
  800. return -EINVAL;
  801. #endif
  802. } else {
  803. fmt->format.width = mode->width;
  804. fmt->format.height = mode->height;
  805. fmt->format.code = MEDIA_BUS_FMT_SBGGR10_1X10;
  806. fmt->format.field = V4L2_FIELD_NONE;
  807. }
  808. mutex_unlock(&ov5695->mutex);
  809. return 0;
  810. }
  811. static int ov5695_enum_mbus_code(struct v4l2_subdev *sd,
  812. struct v4l2_subdev_state *sd_state,
  813. struct v4l2_subdev_mbus_code_enum *code)
  814. {
  815. if (code->index != 0)
  816. return -EINVAL;
  817. code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
  818. return 0;
  819. }
  820. static int ov5695_enum_frame_sizes(struct v4l2_subdev *sd,
  821. struct v4l2_subdev_state *sd_state,
  822. struct v4l2_subdev_frame_size_enum *fse)
  823. {
  824. if (fse->index >= ARRAY_SIZE(supported_modes))
  825. return -EINVAL;
  826. if (fse->code != MEDIA_BUS_FMT_SBGGR10_1X10)
  827. return -EINVAL;
  828. fse->min_width = supported_modes[fse->index].width;
  829. fse->max_width = supported_modes[fse->index].width;
  830. fse->max_height = supported_modes[fse->index].height;
  831. fse->min_height = supported_modes[fse->index].height;
  832. return 0;
  833. }
  834. static int ov5695_enable_test_pattern(struct ov5695 *ov5695, u32 pattern)
  835. {
  836. u32 val;
  837. if (pattern)
  838. val = (pattern - 1) | OV5695_TEST_PATTERN_ENABLE;
  839. else
  840. val = OV5695_TEST_PATTERN_DISABLE;
  841. return ov5695_write_reg(ov5695->client, OV5695_REG_TEST_PATTERN,
  842. OV5695_REG_VALUE_08BIT, val);
  843. }
  844. static int __ov5695_start_stream(struct ov5695 *ov5695)
  845. {
  846. int ret;
  847. ret = ov5695_write_array(ov5695->client, ov5695_global_regs);
  848. if (ret)
  849. return ret;
  850. ret = ov5695_write_array(ov5695->client, ov5695->cur_mode->reg_list);
  851. if (ret)
  852. return ret;
  853. /* In case these controls are set before streaming */
  854. ret = __v4l2_ctrl_handler_setup(&ov5695->ctrl_handler);
  855. if (ret)
  856. return ret;
  857. return ov5695_write_reg(ov5695->client, OV5695_REG_CTRL_MODE,
  858. OV5695_REG_VALUE_08BIT, OV5695_MODE_STREAMING);
  859. }
  860. static int __ov5695_stop_stream(struct ov5695 *ov5695)
  861. {
  862. return ov5695_write_reg(ov5695->client, OV5695_REG_CTRL_MODE,
  863. OV5695_REG_VALUE_08BIT, OV5695_MODE_SW_STANDBY);
  864. }
  865. static int ov5695_s_stream(struct v4l2_subdev *sd, int on)
  866. {
  867. struct ov5695 *ov5695 = to_ov5695(sd);
  868. struct i2c_client *client = ov5695->client;
  869. int ret = 0;
  870. mutex_lock(&ov5695->mutex);
  871. on = !!on;
  872. if (on == ov5695->streaming)
  873. goto unlock_and_return;
  874. if (on) {
  875. ret = pm_runtime_resume_and_get(&client->dev);
  876. if (ret < 0)
  877. goto unlock_and_return;
  878. ret = __ov5695_start_stream(ov5695);
  879. if (ret) {
  880. v4l2_err(sd, "start stream failed while write regs\n");
  881. pm_runtime_put(&client->dev);
  882. goto unlock_and_return;
  883. }
  884. } else {
  885. __ov5695_stop_stream(ov5695);
  886. pm_runtime_put(&client->dev);
  887. }
  888. ov5695->streaming = on;
  889. unlock_and_return:
  890. mutex_unlock(&ov5695->mutex);
  891. return ret;
  892. }
  893. static int __ov5695_power_on(struct ov5695 *ov5695)
  894. {
  895. int i, ret;
  896. struct device *dev = &ov5695->client->dev;
  897. ret = clk_prepare_enable(ov5695->xvclk);
  898. if (ret < 0) {
  899. dev_err(dev, "Failed to enable xvclk\n");
  900. return ret;
  901. }
  902. gpiod_set_value_cansleep(ov5695->reset_gpio, 1);
  903. /*
  904. * The hardware requires the regulators to be powered on in order,
  905. * so enable them one by one.
  906. */
  907. for (i = 0; i < OV5695_NUM_SUPPLIES; i++) {
  908. ret = regulator_enable(ov5695->supplies[i].consumer);
  909. if (ret) {
  910. dev_err(dev, "Failed to enable %s: %d\n",
  911. ov5695->supplies[i].supply, ret);
  912. goto disable_reg_clk;
  913. }
  914. }
  915. gpiod_set_value_cansleep(ov5695->reset_gpio, 0);
  916. usleep_range(1000, 1200);
  917. return 0;
  918. disable_reg_clk:
  919. for (--i; i >= 0; i--)
  920. regulator_disable(ov5695->supplies[i].consumer);
  921. clk_disable_unprepare(ov5695->xvclk);
  922. return ret;
  923. }
  924. static void __ov5695_power_off(struct ov5695 *ov5695)
  925. {
  926. struct device *dev = &ov5695->client->dev;
  927. int i, ret;
  928. clk_disable_unprepare(ov5695->xvclk);
  929. gpiod_set_value_cansleep(ov5695->reset_gpio, 1);
  930. /*
  931. * The hardware requires the regulators to be powered off in order,
  932. * so disable them one by one.
  933. */
  934. for (i = OV5695_NUM_SUPPLIES - 1; i >= 0; i--) {
  935. ret = regulator_disable(ov5695->supplies[i].consumer);
  936. if (ret)
  937. dev_err(dev, "Failed to disable %s: %d\n",
  938. ov5695->supplies[i].supply, ret);
  939. }
  940. }
  941. static int __maybe_unused ov5695_runtime_resume(struct device *dev)
  942. {
  943. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  944. struct ov5695 *ov5695 = to_ov5695(sd);
  945. return __ov5695_power_on(ov5695);
  946. }
  947. static int __maybe_unused ov5695_runtime_suspend(struct device *dev)
  948. {
  949. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  950. struct ov5695 *ov5695 = to_ov5695(sd);
  951. __ov5695_power_off(ov5695);
  952. return 0;
  953. }
  954. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  955. static int ov5695_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  956. {
  957. struct ov5695 *ov5695 = to_ov5695(sd);
  958. struct v4l2_mbus_framefmt *try_fmt =
  959. v4l2_subdev_get_try_format(sd, fh->state, 0);
  960. const struct ov5695_mode *def_mode = &supported_modes[0];
  961. mutex_lock(&ov5695->mutex);
  962. /* Initialize try_fmt */
  963. try_fmt->width = def_mode->width;
  964. try_fmt->height = def_mode->height;
  965. try_fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
  966. try_fmt->field = V4L2_FIELD_NONE;
  967. mutex_unlock(&ov5695->mutex);
  968. /* No crop or compose */
  969. return 0;
  970. }
  971. #endif
  972. static const struct dev_pm_ops ov5695_pm_ops = {
  973. SET_RUNTIME_PM_OPS(ov5695_runtime_suspend,
  974. ov5695_runtime_resume, NULL)
  975. };
  976. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  977. static const struct v4l2_subdev_internal_ops ov5695_internal_ops = {
  978. .open = ov5695_open,
  979. };
  980. #endif
  981. static const struct v4l2_subdev_video_ops ov5695_video_ops = {
  982. .s_stream = ov5695_s_stream,
  983. };
  984. static const struct v4l2_subdev_pad_ops ov5695_pad_ops = {
  985. .enum_mbus_code = ov5695_enum_mbus_code,
  986. .enum_frame_size = ov5695_enum_frame_sizes,
  987. .get_fmt = ov5695_get_fmt,
  988. .set_fmt = ov5695_set_fmt,
  989. };
  990. static const struct v4l2_subdev_ops ov5695_subdev_ops = {
  991. .video = &ov5695_video_ops,
  992. .pad = &ov5695_pad_ops,
  993. };
  994. static int ov5695_set_ctrl(struct v4l2_ctrl *ctrl)
  995. {
  996. struct ov5695 *ov5695 = container_of(ctrl->handler,
  997. struct ov5695, ctrl_handler);
  998. struct i2c_client *client = ov5695->client;
  999. s64 max;
  1000. int ret = 0;
  1001. /* Propagate change of current control to all related controls */
  1002. switch (ctrl->id) {
  1003. case V4L2_CID_VBLANK:
  1004. /* Update max exposure while meeting expected vblanking */
  1005. max = ov5695->cur_mode->height + ctrl->val - 4;
  1006. __v4l2_ctrl_modify_range(ov5695->exposure,
  1007. ov5695->exposure->minimum, max,
  1008. ov5695->exposure->step,
  1009. ov5695->exposure->default_value);
  1010. break;
  1011. }
  1012. if (!pm_runtime_get_if_in_use(&client->dev))
  1013. return 0;
  1014. switch (ctrl->id) {
  1015. case V4L2_CID_EXPOSURE:
  1016. /* 4 least significant bits of exposure are fractional part */
  1017. ret = ov5695_write_reg(ov5695->client, OV5695_REG_EXPOSURE,
  1018. OV5695_REG_VALUE_24BIT, ctrl->val << 4);
  1019. break;
  1020. case V4L2_CID_ANALOGUE_GAIN:
  1021. ret = ov5695_write_reg(ov5695->client, OV5695_REG_ANALOG_GAIN,
  1022. OV5695_REG_VALUE_08BIT, ctrl->val);
  1023. break;
  1024. case V4L2_CID_DIGITAL_GAIN:
  1025. ret = ov5695_write_reg(ov5695->client, OV5695_REG_DIGI_GAIN_L,
  1026. OV5695_REG_VALUE_08BIT,
  1027. ctrl->val & OV5695_DIGI_GAIN_L_MASK);
  1028. ret = ov5695_write_reg(ov5695->client, OV5695_REG_DIGI_GAIN_H,
  1029. OV5695_REG_VALUE_08BIT,
  1030. ctrl->val >> OV5695_DIGI_GAIN_H_SHIFT);
  1031. break;
  1032. case V4L2_CID_VBLANK:
  1033. ret = ov5695_write_reg(ov5695->client, OV5695_REG_VTS,
  1034. OV5695_REG_VALUE_16BIT,
  1035. ctrl->val + ov5695->cur_mode->height);
  1036. break;
  1037. case V4L2_CID_TEST_PATTERN:
  1038. ret = ov5695_enable_test_pattern(ov5695, ctrl->val);
  1039. break;
  1040. default:
  1041. dev_warn(&client->dev, "%s Unhandled id:0x%x, val:0x%x\n",
  1042. __func__, ctrl->id, ctrl->val);
  1043. break;
  1044. }
  1045. pm_runtime_put(&client->dev);
  1046. return ret;
  1047. }
  1048. static const struct v4l2_ctrl_ops ov5695_ctrl_ops = {
  1049. .s_ctrl = ov5695_set_ctrl,
  1050. };
  1051. static int ov5695_initialize_controls(struct ov5695 *ov5695)
  1052. {
  1053. const struct ov5695_mode *mode;
  1054. struct v4l2_ctrl_handler *handler;
  1055. struct v4l2_ctrl *ctrl;
  1056. s64 exposure_max, vblank_def;
  1057. u32 h_blank;
  1058. int ret;
  1059. handler = &ov5695->ctrl_handler;
  1060. mode = ov5695->cur_mode;
  1061. ret = v4l2_ctrl_handler_init(handler, 8);
  1062. if (ret)
  1063. return ret;
  1064. handler->lock = &ov5695->mutex;
  1065. ctrl = v4l2_ctrl_new_int_menu(handler, NULL, V4L2_CID_LINK_FREQ,
  1066. 0, 0, link_freq_menu_items);
  1067. if (ctrl)
  1068. ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  1069. v4l2_ctrl_new_std(handler, NULL, V4L2_CID_PIXEL_RATE,
  1070. 0, OV5695_PIXEL_RATE, 1, OV5695_PIXEL_RATE);
  1071. h_blank = mode->hts_def - mode->width;
  1072. ov5695->hblank = v4l2_ctrl_new_std(handler, NULL, V4L2_CID_HBLANK,
  1073. h_blank, h_blank, 1, h_blank);
  1074. if (ov5695->hblank)
  1075. ov5695->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  1076. vblank_def = mode->vts_def - mode->height;
  1077. ov5695->vblank = v4l2_ctrl_new_std(handler, &ov5695_ctrl_ops,
  1078. V4L2_CID_VBLANK, vblank_def,
  1079. OV5695_VTS_MAX - mode->height,
  1080. 1, vblank_def);
  1081. exposure_max = mode->vts_def - 4;
  1082. ov5695->exposure = v4l2_ctrl_new_std(handler, &ov5695_ctrl_ops,
  1083. V4L2_CID_EXPOSURE, OV5695_EXPOSURE_MIN,
  1084. exposure_max, OV5695_EXPOSURE_STEP,
  1085. mode->exp_def);
  1086. ov5695->anal_gain = v4l2_ctrl_new_std(handler, &ov5695_ctrl_ops,
  1087. V4L2_CID_ANALOGUE_GAIN, ANALOG_GAIN_MIN,
  1088. ANALOG_GAIN_MAX, ANALOG_GAIN_STEP,
  1089. ANALOG_GAIN_DEFAULT);
  1090. /* Digital gain */
  1091. ov5695->digi_gain = v4l2_ctrl_new_std(handler, &ov5695_ctrl_ops,
  1092. V4L2_CID_DIGITAL_GAIN, OV5695_DIGI_GAIN_MIN,
  1093. OV5695_DIGI_GAIN_MAX, OV5695_DIGI_GAIN_STEP,
  1094. OV5695_DIGI_GAIN_DEFAULT);
  1095. ov5695->test_pattern = v4l2_ctrl_new_std_menu_items(handler,
  1096. &ov5695_ctrl_ops, V4L2_CID_TEST_PATTERN,
  1097. ARRAY_SIZE(ov5695_test_pattern_menu) - 1,
  1098. 0, 0, ov5695_test_pattern_menu);
  1099. if (handler->error) {
  1100. ret = handler->error;
  1101. dev_err(&ov5695->client->dev,
  1102. "Failed to init controls(%d)\n", ret);
  1103. goto err_free_handler;
  1104. }
  1105. ov5695->subdev.ctrl_handler = handler;
  1106. return 0;
  1107. err_free_handler:
  1108. v4l2_ctrl_handler_free(handler);
  1109. return ret;
  1110. }
  1111. static int ov5695_check_sensor_id(struct ov5695 *ov5695,
  1112. struct i2c_client *client)
  1113. {
  1114. struct device *dev = &ov5695->client->dev;
  1115. u32 id = 0;
  1116. int ret;
  1117. ret = ov5695_read_reg(client, OV5695_REG_CHIP_ID,
  1118. OV5695_REG_VALUE_24BIT, &id);
  1119. if (id != CHIP_ID) {
  1120. dev_err(dev, "Unexpected sensor id(%06x), ret(%d)\n", id, ret);
  1121. return ret;
  1122. }
  1123. dev_info(dev, "Detected OV%06x sensor\n", CHIP_ID);
  1124. return 0;
  1125. }
  1126. static int ov5695_configure_regulators(struct ov5695 *ov5695)
  1127. {
  1128. int i;
  1129. for (i = 0; i < OV5695_NUM_SUPPLIES; i++)
  1130. ov5695->supplies[i].supply = ov5695_supply_names[i];
  1131. return devm_regulator_bulk_get(&ov5695->client->dev,
  1132. OV5695_NUM_SUPPLIES,
  1133. ov5695->supplies);
  1134. }
  1135. static int ov5695_probe(struct i2c_client *client,
  1136. const struct i2c_device_id *id)
  1137. {
  1138. struct device *dev = &client->dev;
  1139. struct ov5695 *ov5695;
  1140. struct v4l2_subdev *sd;
  1141. int ret;
  1142. ov5695 = devm_kzalloc(dev, sizeof(*ov5695), GFP_KERNEL);
  1143. if (!ov5695)
  1144. return -ENOMEM;
  1145. ov5695->client = client;
  1146. ov5695->cur_mode = &supported_modes[0];
  1147. ov5695->xvclk = devm_clk_get(dev, "xvclk");
  1148. if (IS_ERR(ov5695->xvclk)) {
  1149. dev_err(dev, "Failed to get xvclk\n");
  1150. return -EINVAL;
  1151. }
  1152. ret = clk_set_rate(ov5695->xvclk, OV5695_XVCLK_FREQ);
  1153. if (ret < 0) {
  1154. dev_err(dev, "Failed to set xvclk rate (24MHz)\n");
  1155. return ret;
  1156. }
  1157. if (clk_get_rate(ov5695->xvclk) != OV5695_XVCLK_FREQ)
  1158. dev_warn(dev, "xvclk mismatched, modes are based on 24MHz\n");
  1159. ov5695->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
  1160. if (IS_ERR(ov5695->reset_gpio)) {
  1161. dev_err(dev, "Failed to get reset-gpios\n");
  1162. return -EINVAL;
  1163. }
  1164. ret = ov5695_configure_regulators(ov5695);
  1165. if (ret) {
  1166. dev_err(dev, "Failed to get power regulators\n");
  1167. return ret;
  1168. }
  1169. mutex_init(&ov5695->mutex);
  1170. sd = &ov5695->subdev;
  1171. v4l2_i2c_subdev_init(sd, client, &ov5695_subdev_ops);
  1172. ret = ov5695_initialize_controls(ov5695);
  1173. if (ret)
  1174. goto err_destroy_mutex;
  1175. ret = __ov5695_power_on(ov5695);
  1176. if (ret)
  1177. goto err_free_handler;
  1178. ret = ov5695_check_sensor_id(ov5695, client);
  1179. if (ret)
  1180. goto err_power_off;
  1181. #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
  1182. sd->internal_ops = &ov5695_internal_ops;
  1183. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  1184. #endif
  1185. #if defined(CONFIG_MEDIA_CONTROLLER)
  1186. ov5695->pad.flags = MEDIA_PAD_FL_SOURCE;
  1187. sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
  1188. ret = media_entity_pads_init(&sd->entity, 1, &ov5695->pad);
  1189. if (ret < 0)
  1190. goto err_power_off;
  1191. #endif
  1192. ret = v4l2_async_register_subdev_sensor(sd);
  1193. if (ret) {
  1194. dev_err(dev, "v4l2 async register subdev failed\n");
  1195. goto err_clean_entity;
  1196. }
  1197. pm_runtime_set_active(dev);
  1198. pm_runtime_enable(dev);
  1199. pm_runtime_idle(dev);
  1200. return 0;
  1201. err_clean_entity:
  1202. #if defined(CONFIG_MEDIA_CONTROLLER)
  1203. media_entity_cleanup(&sd->entity);
  1204. #endif
  1205. err_power_off:
  1206. __ov5695_power_off(ov5695);
  1207. err_free_handler:
  1208. v4l2_ctrl_handler_free(&ov5695->ctrl_handler);
  1209. err_destroy_mutex:
  1210. mutex_destroy(&ov5695->mutex);
  1211. return ret;
  1212. }
  1213. static void ov5695_remove(struct i2c_client *client)
  1214. {
  1215. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1216. struct ov5695 *ov5695 = to_ov5695(sd);
  1217. v4l2_async_unregister_subdev(sd);
  1218. #if defined(CONFIG_MEDIA_CONTROLLER)
  1219. media_entity_cleanup(&sd->entity);
  1220. #endif
  1221. v4l2_ctrl_handler_free(&ov5695->ctrl_handler);
  1222. mutex_destroy(&ov5695->mutex);
  1223. pm_runtime_disable(&client->dev);
  1224. if (!pm_runtime_status_suspended(&client->dev))
  1225. __ov5695_power_off(ov5695);
  1226. pm_runtime_set_suspended(&client->dev);
  1227. }
  1228. #if IS_ENABLED(CONFIG_OF)
  1229. static const struct of_device_id ov5695_of_match[] = {
  1230. { .compatible = "ovti,ov5695" },
  1231. {},
  1232. };
  1233. MODULE_DEVICE_TABLE(of, ov5695_of_match);
  1234. #endif
  1235. static struct i2c_driver ov5695_i2c_driver = {
  1236. .driver = {
  1237. .name = "ov5695",
  1238. .pm = &ov5695_pm_ops,
  1239. .of_match_table = of_match_ptr(ov5695_of_match),
  1240. },
  1241. .probe = &ov5695_probe,
  1242. .remove = &ov5695_remove,
  1243. };
  1244. module_i2c_driver(ov5695_i2c_driver);
  1245. MODULE_DESCRIPTION("OmniVision ov5695 sensor driver");
  1246. MODULE_LICENSE("GPL v2");