ov13b10.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2021 Intel Corporation.
  3. #include <linux/acpi.h>
  4. #include <linux/clk.h>
  5. #include <linux/delay.h>
  6. #include <linux/gpio/consumer.h>
  7. #include <linux/i2c.h>
  8. #include <linux/module.h>
  9. #include <linux/pm_runtime.h>
  10. #include <media/v4l2-ctrls.h>
  11. #include <media/v4l2-device.h>
  12. #include <media/v4l2-fwnode.h>
  13. #define OV13B10_REG_VALUE_08BIT 1
  14. #define OV13B10_REG_VALUE_16BIT 2
  15. #define OV13B10_REG_VALUE_24BIT 3
  16. #define OV13B10_REG_MODE_SELECT 0x0100
  17. #define OV13B10_MODE_STANDBY 0x00
  18. #define OV13B10_MODE_STREAMING 0x01
  19. #define OV13B10_REG_SOFTWARE_RST 0x0103
  20. #define OV13B10_SOFTWARE_RST 0x01
  21. /* Chip ID */
  22. #define OV13B10_REG_CHIP_ID 0x300a
  23. #define OV13B10_CHIP_ID 0x560d42
  24. /* V_TIMING internal */
  25. #define OV13B10_REG_VTS 0x380e
  26. #define OV13B10_VTS_30FPS 0x0c7c
  27. #define OV13B10_VTS_60FPS 0x063e
  28. #define OV13B10_VTS_120FPS 0x0320
  29. #define OV13B10_VTS_MAX 0x7fff
  30. /* Exposure control */
  31. #define OV13B10_REG_EXPOSURE 0x3500
  32. #define OV13B10_EXPOSURE_MIN 4
  33. #define OV13B10_EXPOSURE_STEP 1
  34. #define OV13B10_EXPOSURE_DEFAULT 0x40
  35. /* Analog gain control */
  36. #define OV13B10_REG_ANALOG_GAIN 0x3508
  37. #define OV13B10_ANA_GAIN_MIN 0x80
  38. #define OV13B10_ANA_GAIN_MAX 0x07c0
  39. #define OV13B10_ANA_GAIN_STEP 1
  40. #define OV13B10_ANA_GAIN_DEFAULT 0x80
  41. /* Digital gain control */
  42. #define OV13B10_REG_DGTL_GAIN_H 0x350a
  43. #define OV13B10_REG_DGTL_GAIN_M 0x350b
  44. #define OV13B10_REG_DGTL_GAIN_L 0x350c
  45. #define OV13B10_DGTL_GAIN_MIN 1024 /* Min = 1 X */
  46. #define OV13B10_DGTL_GAIN_MAX (4096 - 1) /* Max = 4 X */
  47. #define OV13B10_DGTL_GAIN_DEFAULT 2560 /* Default gain = 2.5 X */
  48. #define OV13B10_DGTL_GAIN_STEP 1 /* Each step = 1/1024 */
  49. #define OV13B10_DGTL_GAIN_L_SHIFT 6
  50. #define OV13B10_DGTL_GAIN_L_MASK 0x3
  51. #define OV13B10_DGTL_GAIN_M_SHIFT 2
  52. #define OV13B10_DGTL_GAIN_M_MASK 0xff
  53. #define OV13B10_DGTL_GAIN_H_SHIFT 10
  54. #define OV13B10_DGTL_GAIN_H_MASK 0x3
  55. /* Test Pattern Control */
  56. #define OV13B10_REG_TEST_PATTERN 0x5080
  57. #define OV13B10_TEST_PATTERN_ENABLE BIT(7)
  58. #define OV13B10_TEST_PATTERN_MASK 0xf3
  59. #define OV13B10_TEST_PATTERN_BAR_SHIFT 2
  60. /* Flip Control */
  61. #define OV13B10_REG_FORMAT1 0x3820
  62. #define OV13B10_REG_FORMAT2 0x3821
  63. /* Horizontal Window Offset */
  64. #define OV13B10_REG_H_WIN_OFFSET 0x3811
  65. /* Vertical Window Offset */
  66. #define OV13B10_REG_V_WIN_OFFSET 0x3813
  67. struct ov13b10_reg {
  68. u16 address;
  69. u8 val;
  70. };
  71. struct ov13b10_reg_list {
  72. u32 num_of_regs;
  73. const struct ov13b10_reg *regs;
  74. };
  75. /* Link frequency config */
  76. struct ov13b10_link_freq_config {
  77. u64 link_freq;
  78. /* registers for this link frequency */
  79. struct ov13b10_reg_list reg_list;
  80. };
  81. /* Mode : resolution and related config&values */
  82. struct ov13b10_mode {
  83. /* Frame width */
  84. u32 width;
  85. /* Frame height */
  86. u32 height;
  87. /* V-timing */
  88. u32 vts_def;
  89. u32 vts_min;
  90. /* Index of Link frequency config to be used */
  91. u32 link_freq_index;
  92. /* Pixels per line in current mode */
  93. u32 ppl;
  94. /* Default register values */
  95. struct ov13b10_reg_list reg_list;
  96. };
  97. /* 4208x3120 needs 1120Mbps/lane, 4 lanes */
  98. static const struct ov13b10_reg mipi_data_rate_1120mbps[] = {
  99. {0x0103, 0x01},
  100. {0x0303, 0x04},
  101. {0x0305, 0xaf},
  102. {0x0321, 0x00},
  103. {0x0323, 0x04},
  104. {0x0324, 0x01},
  105. {0x0325, 0xa4},
  106. {0x0326, 0x81},
  107. {0x0327, 0x04},
  108. {0x3012, 0x07},
  109. {0x3013, 0x32},
  110. {0x3107, 0x23},
  111. {0x3501, 0x0c},
  112. {0x3502, 0x10},
  113. {0x3504, 0x08},
  114. {0x3508, 0x07},
  115. {0x3509, 0xc0},
  116. {0x3600, 0x16},
  117. {0x3601, 0x54},
  118. {0x3612, 0x4e},
  119. {0x3620, 0x00},
  120. {0x3621, 0x68},
  121. {0x3622, 0x66},
  122. {0x3623, 0x03},
  123. {0x3662, 0x92},
  124. {0x3666, 0xbb},
  125. {0x3667, 0x44},
  126. {0x366e, 0xff},
  127. {0x366f, 0xf3},
  128. {0x3675, 0x44},
  129. {0x3676, 0x00},
  130. {0x367f, 0xe9},
  131. {0x3681, 0x32},
  132. {0x3682, 0x1f},
  133. {0x3683, 0x0b},
  134. {0x3684, 0x0b},
  135. {0x3704, 0x0f},
  136. {0x3706, 0x40},
  137. {0x3708, 0x3b},
  138. {0x3709, 0x72},
  139. {0x370b, 0xa2},
  140. {0x3714, 0x24},
  141. {0x371a, 0x3e},
  142. {0x3725, 0x42},
  143. {0x3739, 0x12},
  144. {0x3767, 0x00},
  145. {0x377a, 0x0d},
  146. {0x3789, 0x18},
  147. {0x3790, 0x40},
  148. {0x3791, 0xa2},
  149. {0x37c2, 0x04},
  150. {0x37c3, 0xf1},
  151. {0x37d9, 0x0c},
  152. {0x37da, 0x02},
  153. {0x37dc, 0x02},
  154. {0x37e1, 0x04},
  155. {0x37e2, 0x0a},
  156. {0x3800, 0x00},
  157. {0x3801, 0x00},
  158. {0x3802, 0x00},
  159. {0x3803, 0x08},
  160. {0x3804, 0x10},
  161. {0x3805, 0x8f},
  162. {0x3806, 0x0c},
  163. {0x3807, 0x47},
  164. {0x3808, 0x10},
  165. {0x3809, 0x70},
  166. {0x380a, 0x0c},
  167. {0x380b, 0x30},
  168. {0x380c, 0x04},
  169. {0x380d, 0x98},
  170. {0x380e, 0x0c},
  171. {0x380f, 0x7c},
  172. {0x3811, 0x0f},
  173. {0x3813, 0x09},
  174. {0x3814, 0x01},
  175. {0x3815, 0x01},
  176. {0x3816, 0x01},
  177. {0x3817, 0x01},
  178. {0x381f, 0x08},
  179. {0x3820, 0x88},
  180. {0x3821, 0x00},
  181. {0x3822, 0x14},
  182. {0x382e, 0xe6},
  183. {0x3c80, 0x00},
  184. {0x3c87, 0x01},
  185. {0x3c8c, 0x19},
  186. {0x3c8d, 0x1c},
  187. {0x3ca0, 0x00},
  188. {0x3ca1, 0x00},
  189. {0x3ca2, 0x00},
  190. {0x3ca3, 0x00},
  191. {0x3ca4, 0x50},
  192. {0x3ca5, 0x11},
  193. {0x3ca6, 0x01},
  194. {0x3ca7, 0x00},
  195. {0x3ca8, 0x00},
  196. {0x4008, 0x02},
  197. {0x4009, 0x0f},
  198. {0x400a, 0x01},
  199. {0x400b, 0x19},
  200. {0x4011, 0x21},
  201. {0x4017, 0x08},
  202. {0x4019, 0x04},
  203. {0x401a, 0x58},
  204. {0x4032, 0x1e},
  205. {0x4050, 0x02},
  206. {0x4051, 0x09},
  207. {0x405e, 0x00},
  208. {0x4066, 0x02},
  209. {0x4501, 0x00},
  210. {0x4502, 0x10},
  211. {0x4505, 0x00},
  212. {0x4800, 0x64},
  213. {0x481b, 0x3e},
  214. {0x481f, 0x30},
  215. {0x4825, 0x34},
  216. {0x4837, 0x0e},
  217. {0x484b, 0x01},
  218. {0x4883, 0x02},
  219. {0x5000, 0xff},
  220. {0x5001, 0x0f},
  221. {0x5045, 0x20},
  222. {0x5046, 0x20},
  223. {0x5047, 0xa4},
  224. {0x5048, 0x20},
  225. {0x5049, 0xa4},
  226. };
  227. static const struct ov13b10_reg mode_4208x3120_regs[] = {
  228. {0x0305, 0xaf},
  229. {0x3501, 0x0c},
  230. {0x3662, 0x92},
  231. {0x3714, 0x24},
  232. {0x3739, 0x12},
  233. {0x37c2, 0x04},
  234. {0x37d9, 0x0c},
  235. {0x37e2, 0x0a},
  236. {0x3800, 0x00},
  237. {0x3801, 0x00},
  238. {0x3802, 0x00},
  239. {0x3803, 0x08},
  240. {0x3804, 0x10},
  241. {0x3805, 0x8f},
  242. {0x3806, 0x0c},
  243. {0x3807, 0x47},
  244. {0x3808, 0x10},
  245. {0x3809, 0x70},
  246. {0x380a, 0x0c},
  247. {0x380b, 0x30},
  248. {0x380c, 0x04},
  249. {0x380d, 0x98},
  250. {0x380e, 0x0c},
  251. {0x380f, 0x7c},
  252. {0x3810, 0x00},
  253. {0x3811, 0x0f},
  254. {0x3812, 0x00},
  255. {0x3813, 0x09},
  256. {0x3814, 0x01},
  257. {0x3816, 0x01},
  258. {0x3820, 0x88},
  259. {0x3c8c, 0x19},
  260. {0x4008, 0x02},
  261. {0x4009, 0x0f},
  262. {0x4050, 0x02},
  263. {0x4051, 0x09},
  264. {0x4501, 0x00},
  265. {0x4505, 0x00},
  266. {0x4837, 0x0e},
  267. {0x5000, 0xff},
  268. {0x5001, 0x0f},
  269. };
  270. static const struct ov13b10_reg mode_4160x3120_regs[] = {
  271. {0x0305, 0xaf},
  272. {0x3501, 0x0c},
  273. {0x3662, 0x92},
  274. {0x3714, 0x24},
  275. {0x3739, 0x12},
  276. {0x37c2, 0x04},
  277. {0x37d9, 0x0c},
  278. {0x37e2, 0x0a},
  279. {0x3800, 0x00},
  280. {0x3801, 0x00},
  281. {0x3802, 0x00},
  282. {0x3803, 0x08},
  283. {0x3804, 0x10},
  284. {0x3805, 0x8f},
  285. {0x3806, 0x0c},
  286. {0x3807, 0x47},
  287. {0x3808, 0x10},
  288. {0x3809, 0x40},
  289. {0x380a, 0x0c},
  290. {0x380b, 0x30},
  291. {0x380c, 0x04},
  292. {0x380d, 0x98},
  293. {0x380e, 0x0c},
  294. {0x380f, 0x7c},
  295. {0x3810, 0x00},
  296. {0x3811, 0x27},
  297. {0x3812, 0x00},
  298. {0x3813, 0x09},
  299. {0x3814, 0x01},
  300. {0x3816, 0x01},
  301. {0x3820, 0x88},
  302. {0x3c8c, 0x19},
  303. {0x4008, 0x02},
  304. {0x4009, 0x0f},
  305. {0x4050, 0x02},
  306. {0x4051, 0x09},
  307. {0x4501, 0x00},
  308. {0x4505, 0x00},
  309. {0x4837, 0x0e},
  310. {0x5000, 0xff},
  311. {0x5001, 0x0f},
  312. };
  313. static const struct ov13b10_reg mode_4160x2340_regs[] = {
  314. {0x0305, 0xaf},
  315. {0x3501, 0x0c},
  316. {0x3662, 0x92},
  317. {0x3714, 0x24},
  318. {0x3739, 0x12},
  319. {0x37c2, 0x04},
  320. {0x37d9, 0x0c},
  321. {0x37e2, 0x0a},
  322. {0x3800, 0x00},
  323. {0x3801, 0x00},
  324. {0x3802, 0x00},
  325. {0x3803, 0x08},
  326. {0x3804, 0x10},
  327. {0x3805, 0x8f},
  328. {0x3806, 0x0c},
  329. {0x3807, 0x47},
  330. {0x3808, 0x10},
  331. {0x3809, 0x40},
  332. {0x380a, 0x09},
  333. {0x380b, 0x24},
  334. {0x380c, 0x04},
  335. {0x380d, 0x98},
  336. {0x380e, 0x0c},
  337. {0x380f, 0x7c},
  338. {0x3810, 0x00},
  339. {0x3811, 0x27},
  340. {0x3812, 0x01},
  341. {0x3813, 0x8f},
  342. {0x3814, 0x01},
  343. {0x3816, 0x01},
  344. {0x3820, 0x88},
  345. {0x3c8c, 0x19},
  346. {0x4008, 0x02},
  347. {0x4009, 0x0f},
  348. {0x4050, 0x02},
  349. {0x4051, 0x09},
  350. {0x4501, 0x00},
  351. {0x4505, 0x00},
  352. {0x4837, 0x0e},
  353. {0x5000, 0xff},
  354. {0x5001, 0x0f},
  355. };
  356. static const struct ov13b10_reg mode_2104x1560_regs[] = {
  357. {0x0305, 0xaf},
  358. {0x3501, 0x06},
  359. {0x3662, 0x88},
  360. {0x3714, 0x28},
  361. {0x3739, 0x10},
  362. {0x37c2, 0x14},
  363. {0x37d9, 0x06},
  364. {0x37e2, 0x0c},
  365. {0x3800, 0x00},
  366. {0x3801, 0x00},
  367. {0x3802, 0x00},
  368. {0x3803, 0x08},
  369. {0x3804, 0x10},
  370. {0x3805, 0x8f},
  371. {0x3806, 0x0c},
  372. {0x3807, 0x47},
  373. {0x3808, 0x08},
  374. {0x3809, 0x38},
  375. {0x380a, 0x06},
  376. {0x380b, 0x18},
  377. {0x380c, 0x04},
  378. {0x380d, 0x98},
  379. {0x380e, 0x06},
  380. {0x380f, 0x3e},
  381. {0x3810, 0x00},
  382. {0x3811, 0x07},
  383. {0x3812, 0x00},
  384. {0x3813, 0x05},
  385. {0x3814, 0x03},
  386. {0x3816, 0x03},
  387. {0x3820, 0x8b},
  388. {0x3c8c, 0x18},
  389. {0x4008, 0x00},
  390. {0x4009, 0x05},
  391. {0x4050, 0x00},
  392. {0x4051, 0x05},
  393. {0x4501, 0x08},
  394. {0x4505, 0x00},
  395. {0x4837, 0x0e},
  396. {0x5000, 0xfd},
  397. {0x5001, 0x0d},
  398. };
  399. static const struct ov13b10_reg mode_2080x1170_regs[] = {
  400. {0x0305, 0xaf},
  401. {0x3501, 0x06},
  402. {0x3662, 0x88},
  403. {0x3714, 0x28},
  404. {0x3739, 0x10},
  405. {0x37c2, 0x14},
  406. {0x37d9, 0x06},
  407. {0x37e2, 0x0c},
  408. {0x3800, 0x00},
  409. {0x3801, 0x00},
  410. {0x3802, 0x00},
  411. {0x3803, 0x08},
  412. {0x3804, 0x10},
  413. {0x3805, 0x8f},
  414. {0x3806, 0x0c},
  415. {0x3807, 0x47},
  416. {0x3808, 0x08},
  417. {0x3809, 0x20},
  418. {0x380a, 0x04},
  419. {0x380b, 0x92},
  420. {0x380c, 0x04},
  421. {0x380d, 0x98},
  422. {0x380e, 0x06},
  423. {0x380f, 0x3e},
  424. {0x3810, 0x00},
  425. {0x3811, 0x13},
  426. {0x3812, 0x00},
  427. {0x3813, 0xc9},
  428. {0x3814, 0x03},
  429. {0x3816, 0x03},
  430. {0x3820, 0x8b},
  431. {0x3c8c, 0x18},
  432. {0x4008, 0x00},
  433. {0x4009, 0x05},
  434. {0x4050, 0x00},
  435. {0x4051, 0x05},
  436. {0x4501, 0x08},
  437. {0x4505, 0x00},
  438. {0x4837, 0x0e},
  439. {0x5000, 0xfd},
  440. {0x5001, 0x0d},
  441. };
  442. static const struct ov13b10_reg mode_1364x768_120fps_regs[] = {
  443. {0x0305, 0xaf},
  444. {0x3011, 0x7c},
  445. {0x3501, 0x03},
  446. {0x3502, 0x00},
  447. {0x3662, 0x88},
  448. {0x3714, 0x28},
  449. {0x3739, 0x10},
  450. {0x37c2, 0x14},
  451. {0x37d9, 0x06},
  452. {0x37e2, 0x0c},
  453. {0x37e4, 0x00},
  454. {0x3800, 0x02},
  455. {0x3801, 0xe4},
  456. {0x3802, 0x03},
  457. {0x3803, 0x48},
  458. {0x3804, 0x0d},
  459. {0x3805, 0xab},
  460. {0x3806, 0x09},
  461. {0x3807, 0x60},
  462. {0x3808, 0x05},
  463. {0x3809, 0x54},
  464. {0x380a, 0x03},
  465. {0x380b, 0x00},
  466. {0x380c, 0x04},
  467. {0x380d, 0x8e},
  468. {0x380e, 0x03},
  469. {0x380f, 0x20},
  470. {0x3811, 0x07},
  471. {0x3813, 0x07},
  472. {0x3814, 0x03},
  473. {0x3816, 0x03},
  474. {0x3820, 0x8b},
  475. {0x3c8c, 0x18},
  476. {0x4008, 0x00},
  477. {0x4009, 0x05},
  478. {0x4050, 0x00},
  479. {0x4051, 0x05},
  480. {0x4501, 0x08},
  481. {0x4505, 0x04},
  482. {0x5000, 0xfd},
  483. {0x5001, 0x0d},
  484. };
  485. static const struct ov13b10_reg mode_2lanes_2104x1560_60fps_regs[] = {
  486. {0x3016, 0x32},
  487. {0x3106, 0x29},
  488. {0x0305, 0xaf},
  489. {0x3501, 0x06},
  490. {0x3662, 0x88},
  491. {0x3714, 0x28},
  492. {0x3739, 0x10},
  493. {0x37c2, 0x14},
  494. {0x37d9, 0x06},
  495. {0x37e2, 0x0c},
  496. {0x3800, 0x00},
  497. {0x3801, 0x00},
  498. {0x3802, 0x00},
  499. {0x3803, 0x08},
  500. {0x3804, 0x10},
  501. {0x3805, 0x8f},
  502. {0x3806, 0x0c},
  503. {0x3807, 0x47},
  504. {0x3808, 0x08},
  505. {0x3809, 0x38},
  506. {0x380a, 0x06},
  507. {0x380b, 0x18},
  508. {0x380c, 0x04},
  509. {0x380d, 0x98},
  510. {0x380e, 0x06},
  511. {0x380f, 0x3e},
  512. {0x3810, 0x00},
  513. {0x3811, 0x07},
  514. {0x3812, 0x00},
  515. {0x3813, 0x05},
  516. {0x3814, 0x03},
  517. {0x3816, 0x03},
  518. {0x3820, 0x8b},
  519. {0x3c8c, 0x18},
  520. {0x4008, 0x00},
  521. {0x4009, 0x05},
  522. {0x4050, 0x00},
  523. {0x4051, 0x05},
  524. {0x4501, 0x08},
  525. {0x4505, 0x00},
  526. {0x4837, 0x0e},
  527. {0x5000, 0xfd},
  528. {0x5001, 0x0d},
  529. };
  530. static const char * const ov13b10_test_pattern_menu[] = {
  531. "Disabled",
  532. "Vertical Color Bar Type 1",
  533. "Vertical Color Bar Type 2",
  534. "Vertical Color Bar Type 3",
  535. "Vertical Color Bar Type 4"
  536. };
  537. /* Configurations for supported link frequencies */
  538. #define OV13B10_LINK_FREQ_560MHZ 560000000ULL
  539. #define OV13B10_LINK_FREQ_INDEX_0 0
  540. #define OV13B10_EXT_CLK 19200000
  541. #define OV13B10_4_DATA_LANES 4
  542. #define OV13B10_2_DATA_LANES 2
  543. /*
  544. * pixel_rate = data_rate * nr_of_lanes / bits_per_pixel
  545. * data_rate => link_freq * 2; number of lanes => 4 or 2; bits per pixel => 10
  546. */
  547. static u64 link_freq_to_pixel_rate(u64 f, u8 lanes)
  548. {
  549. f *= 2 * lanes;
  550. do_div(f, 10);
  551. return f;
  552. }
  553. /* Menu items for LINK_FREQ V4L2 control */
  554. static const s64 link_freq_menu_items[] = {
  555. OV13B10_LINK_FREQ_560MHZ
  556. };
  557. /* Link frequency configs */
  558. static const struct ov13b10_link_freq_config
  559. link_freq_configs[] = {
  560. {
  561. .link_freq = OV13B10_LINK_FREQ_560MHZ,
  562. .reg_list = {
  563. .num_of_regs = ARRAY_SIZE(mipi_data_rate_1120mbps),
  564. .regs = mipi_data_rate_1120mbps,
  565. }
  566. }
  567. };
  568. /* Mode configs */
  569. static const struct ov13b10_mode supported_4_lanes_modes[] = {
  570. /* 4 data lanes */
  571. {
  572. .width = 4208,
  573. .height = 3120,
  574. .vts_def = OV13B10_VTS_30FPS,
  575. .vts_min = OV13B10_VTS_30FPS,
  576. .ppl = 4704,
  577. .reg_list = {
  578. .num_of_regs = ARRAY_SIZE(mode_4208x3120_regs),
  579. .regs = mode_4208x3120_regs,
  580. },
  581. .link_freq_index = OV13B10_LINK_FREQ_INDEX_0,
  582. },
  583. {
  584. .width = 4160,
  585. .height = 3120,
  586. .vts_def = OV13B10_VTS_30FPS,
  587. .vts_min = OV13B10_VTS_30FPS,
  588. .ppl = 4704,
  589. .reg_list = {
  590. .num_of_regs = ARRAY_SIZE(mode_4160x3120_regs),
  591. .regs = mode_4160x3120_regs,
  592. },
  593. .link_freq_index = OV13B10_LINK_FREQ_INDEX_0,
  594. },
  595. {
  596. .width = 4160,
  597. .height = 2340,
  598. .vts_def = OV13B10_VTS_30FPS,
  599. .vts_min = OV13B10_VTS_30FPS,
  600. .ppl = 4704,
  601. .reg_list = {
  602. .num_of_regs = ARRAY_SIZE(mode_4160x2340_regs),
  603. .regs = mode_4160x2340_regs,
  604. },
  605. .link_freq_index = OV13B10_LINK_FREQ_INDEX_0,
  606. },
  607. {
  608. .width = 2104,
  609. .height = 1560,
  610. .vts_def = OV13B10_VTS_60FPS,
  611. .vts_min = OV13B10_VTS_60FPS,
  612. .ppl = 4704,
  613. .reg_list = {
  614. .num_of_regs = ARRAY_SIZE(mode_2104x1560_regs),
  615. .regs = mode_2104x1560_regs,
  616. },
  617. .link_freq_index = OV13B10_LINK_FREQ_INDEX_0,
  618. },
  619. {
  620. .width = 2080,
  621. .height = 1170,
  622. .vts_def = OV13B10_VTS_60FPS,
  623. .vts_min = OV13B10_VTS_60FPS,
  624. .ppl = 4704,
  625. .reg_list = {
  626. .num_of_regs = ARRAY_SIZE(mode_2080x1170_regs),
  627. .regs = mode_2080x1170_regs,
  628. },
  629. .link_freq_index = OV13B10_LINK_FREQ_INDEX_0,
  630. },
  631. {
  632. .width = 1364,
  633. .height = 768,
  634. .vts_def = OV13B10_VTS_120FPS,
  635. .vts_min = OV13B10_VTS_120FPS,
  636. .link_freq_index = OV13B10_LINK_FREQ_INDEX_0,
  637. .ppl = 4664,
  638. .reg_list = {
  639. .num_of_regs = ARRAY_SIZE(mode_1364x768_120fps_regs),
  640. .regs = mode_1364x768_120fps_regs,
  641. },
  642. },
  643. };
  644. static const struct ov13b10_mode supported_2_lanes_modes[] = {
  645. /* 2 data lanes */
  646. {
  647. .width = 2104,
  648. .height = 1560,
  649. .vts_def = OV13B10_VTS_60FPS,
  650. .vts_min = OV13B10_VTS_60FPS,
  651. .link_freq_index = OV13B10_LINK_FREQ_INDEX_0,
  652. .ppl = 2352,
  653. .reg_list = {
  654. .num_of_regs =
  655. ARRAY_SIZE(mode_2lanes_2104x1560_60fps_regs),
  656. .regs = mode_2lanes_2104x1560_60fps_regs,
  657. },
  658. },
  659. };
  660. struct ov13b10 {
  661. struct device *dev;
  662. struct v4l2_subdev sd;
  663. struct media_pad pad;
  664. struct v4l2_ctrl_handler ctrl_handler;
  665. struct clk *img_clk;
  666. struct regulator *avdd;
  667. struct gpio_desc *reset;
  668. /* V4L2 Controls */
  669. struct v4l2_ctrl *link_freq;
  670. struct v4l2_ctrl *pixel_rate;
  671. struct v4l2_ctrl *vblank;
  672. struct v4l2_ctrl *hblank;
  673. struct v4l2_ctrl *exposure;
  674. /* Supported modes */
  675. const struct ov13b10_mode *supported_modes;
  676. /* Current mode */
  677. const struct ov13b10_mode *cur_mode;
  678. /* Mutex for serialized access */
  679. struct mutex mutex;
  680. u8 supported_modes_num;
  681. /* Data lanes used */
  682. u8 data_lanes;
  683. /* True if the device has been identified */
  684. bool identified;
  685. };
  686. #define to_ov13b10(_sd) container_of(_sd, struct ov13b10, sd)
  687. /* Read registers up to 4 at a time */
  688. static int ov13b10_read_reg(struct ov13b10 *ov13b,
  689. u16 reg, u32 len, u32 *val)
  690. {
  691. struct i2c_client *client = v4l2_get_subdevdata(&ov13b->sd);
  692. struct i2c_msg msgs[2];
  693. u8 *data_be_p;
  694. int ret;
  695. __be32 data_be = 0;
  696. __be16 reg_addr_be = cpu_to_be16(reg);
  697. if (len > 4)
  698. return -EINVAL;
  699. data_be_p = (u8 *)&data_be;
  700. /* Write register address */
  701. msgs[0].addr = client->addr;
  702. msgs[0].flags = 0;
  703. msgs[0].len = 2;
  704. msgs[0].buf = (u8 *)&reg_addr_be;
  705. /* Read data from register */
  706. msgs[1].addr = client->addr;
  707. msgs[1].flags = I2C_M_RD;
  708. msgs[1].len = len;
  709. msgs[1].buf = &data_be_p[4 - len];
  710. ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  711. if (ret != ARRAY_SIZE(msgs))
  712. return -EIO;
  713. *val = be32_to_cpu(data_be);
  714. return 0;
  715. }
  716. /* Write registers up to 4 at a time */
  717. static int ov13b10_write_reg(struct ov13b10 *ov13b,
  718. u16 reg, u32 len, u32 __val)
  719. {
  720. struct i2c_client *client = v4l2_get_subdevdata(&ov13b->sd);
  721. int buf_i, val_i;
  722. u8 buf[6], *val_p;
  723. __be32 val;
  724. if (len > 4)
  725. return -EINVAL;
  726. buf[0] = reg >> 8;
  727. buf[1] = reg & 0xff;
  728. val = cpu_to_be32(__val);
  729. val_p = (u8 *)&val;
  730. buf_i = 2;
  731. val_i = 4 - len;
  732. while (val_i < 4)
  733. buf[buf_i++] = val_p[val_i++];
  734. if (i2c_master_send(client, buf, len + 2) != len + 2)
  735. return -EIO;
  736. return 0;
  737. }
  738. /* Write a list of registers */
  739. static int ov13b10_write_regs(struct ov13b10 *ov13b,
  740. const struct ov13b10_reg *regs, u32 len)
  741. {
  742. int ret;
  743. u32 i;
  744. for (i = 0; i < len; i++) {
  745. ret = ov13b10_write_reg(ov13b, regs[i].address, 1,
  746. regs[i].val);
  747. if (ret) {
  748. dev_err_ratelimited(ov13b->dev,
  749. "Failed to write reg 0x%4.4x. error = %d\n",
  750. regs[i].address, ret);
  751. return ret;
  752. }
  753. }
  754. return 0;
  755. }
  756. static int ov13b10_write_reg_list(struct ov13b10 *ov13b,
  757. const struct ov13b10_reg_list *r_list)
  758. {
  759. return ov13b10_write_regs(ov13b, r_list->regs, r_list->num_of_regs);
  760. }
  761. /* Open sub-device */
  762. static int ov13b10_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  763. {
  764. struct ov13b10 *ov13b = to_ov13b10(sd);
  765. const struct ov13b10_mode *default_mode = ov13b->supported_modes;
  766. struct v4l2_mbus_framefmt *try_fmt = v4l2_subdev_state_get_format(fh->state,
  767. 0);
  768. mutex_lock(&ov13b->mutex);
  769. /* Initialize try_fmt */
  770. try_fmt->width = default_mode->width;
  771. try_fmt->height = default_mode->height;
  772. try_fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
  773. try_fmt->field = V4L2_FIELD_NONE;
  774. /* No crop or compose */
  775. mutex_unlock(&ov13b->mutex);
  776. return 0;
  777. }
  778. static int ov13b10_update_digital_gain(struct ov13b10 *ov13b, u32 d_gain)
  779. {
  780. int ret;
  781. u32 val;
  782. /*
  783. * 0x350C[7:6], 0x350B[7:0], 0x350A[1:0]
  784. */
  785. val = (d_gain & OV13B10_DGTL_GAIN_L_MASK) << OV13B10_DGTL_GAIN_L_SHIFT;
  786. ret = ov13b10_write_reg(ov13b, OV13B10_REG_DGTL_GAIN_L,
  787. OV13B10_REG_VALUE_08BIT, val);
  788. if (ret)
  789. return ret;
  790. val = (d_gain >> OV13B10_DGTL_GAIN_M_SHIFT) & OV13B10_DGTL_GAIN_M_MASK;
  791. ret = ov13b10_write_reg(ov13b, OV13B10_REG_DGTL_GAIN_M,
  792. OV13B10_REG_VALUE_08BIT, val);
  793. if (ret)
  794. return ret;
  795. val = (d_gain >> OV13B10_DGTL_GAIN_H_SHIFT) & OV13B10_DGTL_GAIN_H_MASK;
  796. ret = ov13b10_write_reg(ov13b, OV13B10_REG_DGTL_GAIN_H,
  797. OV13B10_REG_VALUE_08BIT, val);
  798. return ret;
  799. }
  800. static int ov13b10_enable_test_pattern(struct ov13b10 *ov13b, u32 pattern)
  801. {
  802. int ret;
  803. u32 val;
  804. ret = ov13b10_read_reg(ov13b, OV13B10_REG_TEST_PATTERN,
  805. OV13B10_REG_VALUE_08BIT, &val);
  806. if (ret)
  807. return ret;
  808. if (pattern) {
  809. val &= OV13B10_TEST_PATTERN_MASK;
  810. val |= ((pattern - 1) << OV13B10_TEST_PATTERN_BAR_SHIFT) |
  811. OV13B10_TEST_PATTERN_ENABLE;
  812. } else {
  813. val &= ~OV13B10_TEST_PATTERN_ENABLE;
  814. }
  815. return ov13b10_write_reg(ov13b, OV13B10_REG_TEST_PATTERN,
  816. OV13B10_REG_VALUE_08BIT, val);
  817. }
  818. static int ov13b10_set_ctrl_hflip(struct ov13b10 *ov13b, u32 ctrl_val)
  819. {
  820. int ret;
  821. u32 val;
  822. ret = ov13b10_read_reg(ov13b, OV13B10_REG_FORMAT1,
  823. OV13B10_REG_VALUE_08BIT, &val);
  824. if (ret)
  825. return ret;
  826. ret = ov13b10_write_reg(ov13b, OV13B10_REG_FORMAT1,
  827. OV13B10_REG_VALUE_08BIT,
  828. ctrl_val ? val & ~BIT(3) : val);
  829. if (ret)
  830. return ret;
  831. ret = ov13b10_read_reg(ov13b, OV13B10_REG_H_WIN_OFFSET,
  832. OV13B10_REG_VALUE_08BIT, &val);
  833. if (ret)
  834. return ret;
  835. /*
  836. * Applying cropping offset to reverse the change of Bayer order
  837. * after mirroring image
  838. */
  839. return ov13b10_write_reg(ov13b, OV13B10_REG_H_WIN_OFFSET,
  840. OV13B10_REG_VALUE_08BIT,
  841. ctrl_val ? ++val : val);
  842. }
  843. static int ov13b10_set_ctrl_vflip(struct ov13b10 *ov13b, u32 ctrl_val)
  844. {
  845. int ret;
  846. u32 val;
  847. ret = ov13b10_read_reg(ov13b, OV13B10_REG_FORMAT1,
  848. OV13B10_REG_VALUE_08BIT, &val);
  849. if (ret)
  850. return ret;
  851. ret = ov13b10_write_reg(ov13b, OV13B10_REG_FORMAT1,
  852. OV13B10_REG_VALUE_08BIT,
  853. ctrl_val ? val | BIT(4) | BIT(5) : val);
  854. if (ret)
  855. return ret;
  856. ret = ov13b10_read_reg(ov13b, OV13B10_REG_V_WIN_OFFSET,
  857. OV13B10_REG_VALUE_08BIT, &val);
  858. if (ret)
  859. return ret;
  860. /*
  861. * Applying cropping offset to reverse the change of Bayer order
  862. * after flipping image
  863. */
  864. return ov13b10_write_reg(ov13b, OV13B10_REG_V_WIN_OFFSET,
  865. OV13B10_REG_VALUE_08BIT,
  866. ctrl_val ? --val : val);
  867. }
  868. static int ov13b10_set_ctrl(struct v4l2_ctrl *ctrl)
  869. {
  870. struct ov13b10 *ov13b = container_of(ctrl->handler,
  871. struct ov13b10, ctrl_handler);
  872. s64 max;
  873. int ret;
  874. /* Propagate change of current control to all related controls */
  875. switch (ctrl->id) {
  876. case V4L2_CID_VBLANK:
  877. /* Update max exposure while meeting expected vblanking */
  878. max = ov13b->cur_mode->height + ctrl->val - 8;
  879. __v4l2_ctrl_modify_range(ov13b->exposure,
  880. ov13b->exposure->minimum,
  881. max, ov13b->exposure->step, max);
  882. break;
  883. }
  884. /*
  885. * Applying V4L2 control value only happens
  886. * when power is up for streaming
  887. */
  888. if (!pm_runtime_get_if_in_use(ov13b->dev))
  889. return 0;
  890. ret = 0;
  891. switch (ctrl->id) {
  892. case V4L2_CID_ANALOGUE_GAIN:
  893. ret = ov13b10_write_reg(ov13b, OV13B10_REG_ANALOG_GAIN,
  894. OV13B10_REG_VALUE_16BIT,
  895. ctrl->val << 1);
  896. break;
  897. case V4L2_CID_DIGITAL_GAIN:
  898. ret = ov13b10_update_digital_gain(ov13b, ctrl->val);
  899. break;
  900. case V4L2_CID_EXPOSURE:
  901. ret = ov13b10_write_reg(ov13b, OV13B10_REG_EXPOSURE,
  902. OV13B10_REG_VALUE_24BIT,
  903. ctrl->val);
  904. break;
  905. case V4L2_CID_VBLANK:
  906. ret = ov13b10_write_reg(ov13b, OV13B10_REG_VTS,
  907. OV13B10_REG_VALUE_16BIT,
  908. ov13b->cur_mode->height
  909. + ctrl->val);
  910. break;
  911. case V4L2_CID_TEST_PATTERN:
  912. ret = ov13b10_enable_test_pattern(ov13b, ctrl->val);
  913. break;
  914. case V4L2_CID_HFLIP:
  915. ov13b10_set_ctrl_hflip(ov13b, ctrl->val);
  916. break;
  917. case V4L2_CID_VFLIP:
  918. ov13b10_set_ctrl_vflip(ov13b, ctrl->val);
  919. break;
  920. default:
  921. dev_info(ov13b->dev,
  922. "ctrl(id:0x%x,val:0x%x) is not handled\n",
  923. ctrl->id, ctrl->val);
  924. break;
  925. }
  926. pm_runtime_put(ov13b->dev);
  927. return ret;
  928. }
  929. static const struct v4l2_ctrl_ops ov13b10_ctrl_ops = {
  930. .s_ctrl = ov13b10_set_ctrl,
  931. };
  932. static int ov13b10_enum_mbus_code(struct v4l2_subdev *sd,
  933. struct v4l2_subdev_state *sd_state,
  934. struct v4l2_subdev_mbus_code_enum *code)
  935. {
  936. /* Only one bayer order(GRBG) is supported */
  937. if (code->index > 0)
  938. return -EINVAL;
  939. code->code = MEDIA_BUS_FMT_SGRBG10_1X10;
  940. return 0;
  941. }
  942. static int ov13b10_enum_frame_size(struct v4l2_subdev *sd,
  943. struct v4l2_subdev_state *sd_state,
  944. struct v4l2_subdev_frame_size_enum *fse)
  945. {
  946. struct ov13b10 *ov13b = to_ov13b10(sd);
  947. const struct ov13b10_mode *supported_modes = ov13b->supported_modes;
  948. if (fse->index >= ov13b->supported_modes_num)
  949. return -EINVAL;
  950. if (fse->code != MEDIA_BUS_FMT_SGRBG10_1X10)
  951. return -EINVAL;
  952. fse->min_width = supported_modes[fse->index].width;
  953. fse->max_width = fse->min_width;
  954. fse->min_height = supported_modes[fse->index].height;
  955. fse->max_height = fse->min_height;
  956. return 0;
  957. }
  958. static void ov13b10_update_pad_format(const struct ov13b10_mode *mode,
  959. struct v4l2_subdev_format *fmt)
  960. {
  961. fmt->format.width = mode->width;
  962. fmt->format.height = mode->height;
  963. fmt->format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
  964. fmt->format.field = V4L2_FIELD_NONE;
  965. }
  966. static int ov13b10_do_get_pad_format(struct ov13b10 *ov13b,
  967. struct v4l2_subdev_state *sd_state,
  968. struct v4l2_subdev_format *fmt)
  969. {
  970. struct v4l2_mbus_framefmt *framefmt;
  971. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  972. framefmt = v4l2_subdev_state_get_format(sd_state, fmt->pad);
  973. fmt->format = *framefmt;
  974. } else {
  975. ov13b10_update_pad_format(ov13b->cur_mode, fmt);
  976. }
  977. return 0;
  978. }
  979. static int ov13b10_get_pad_format(struct v4l2_subdev *sd,
  980. struct v4l2_subdev_state *sd_state,
  981. struct v4l2_subdev_format *fmt)
  982. {
  983. struct ov13b10 *ov13b = to_ov13b10(sd);
  984. int ret;
  985. mutex_lock(&ov13b->mutex);
  986. ret = ov13b10_do_get_pad_format(ov13b, sd_state, fmt);
  987. mutex_unlock(&ov13b->mutex);
  988. return ret;
  989. }
  990. static int
  991. ov13b10_set_pad_format(struct v4l2_subdev *sd,
  992. struct v4l2_subdev_state *sd_state,
  993. struct v4l2_subdev_format *fmt)
  994. {
  995. struct ov13b10 *ov13b = to_ov13b10(sd);
  996. const struct ov13b10_mode *mode;
  997. const struct ov13b10_mode *supported_modes = ov13b->supported_modes;
  998. struct v4l2_mbus_framefmt *framefmt;
  999. s32 vblank_def;
  1000. s32 vblank_min;
  1001. s64 h_blank;
  1002. s64 pixel_rate;
  1003. s64 link_freq;
  1004. mutex_lock(&ov13b->mutex);
  1005. /* Only one raw bayer(GRBG) order is supported */
  1006. if (fmt->format.code != MEDIA_BUS_FMT_SGRBG10_1X10)
  1007. fmt->format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
  1008. mode = v4l2_find_nearest_size(supported_modes,
  1009. ov13b->supported_modes_num,
  1010. width, height,
  1011. fmt->format.width, fmt->format.height);
  1012. ov13b10_update_pad_format(mode, fmt);
  1013. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  1014. framefmt = v4l2_subdev_state_get_format(sd_state, fmt->pad);
  1015. *framefmt = fmt->format;
  1016. } else {
  1017. ov13b->cur_mode = mode;
  1018. __v4l2_ctrl_s_ctrl(ov13b->link_freq, mode->link_freq_index);
  1019. link_freq = link_freq_menu_items[mode->link_freq_index];
  1020. pixel_rate = link_freq_to_pixel_rate(link_freq,
  1021. ov13b->data_lanes);
  1022. __v4l2_ctrl_s_ctrl_int64(ov13b->pixel_rate, pixel_rate);
  1023. /* Update limits and set FPS to default */
  1024. vblank_def = mode->vts_def - mode->height;
  1025. vblank_min = mode->vts_min - mode->height;
  1026. __v4l2_ctrl_modify_range(ov13b->vblank, vblank_min,
  1027. OV13B10_VTS_MAX - mode->height,
  1028. 1, vblank_def);
  1029. __v4l2_ctrl_s_ctrl(ov13b->vblank, vblank_def);
  1030. h_blank = mode->ppl - mode->width;
  1031. __v4l2_ctrl_modify_range(ov13b->hblank, h_blank,
  1032. h_blank, 1, h_blank);
  1033. }
  1034. mutex_unlock(&ov13b->mutex);
  1035. return 0;
  1036. }
  1037. /* Verify chip ID */
  1038. static int ov13b10_identify_module(struct ov13b10 *ov13b)
  1039. {
  1040. int ret;
  1041. u32 val;
  1042. if (ov13b->identified)
  1043. return 0;
  1044. ret = ov13b10_read_reg(ov13b, OV13B10_REG_CHIP_ID,
  1045. OV13B10_REG_VALUE_24BIT, &val);
  1046. if (ret)
  1047. return ret;
  1048. if (val != OV13B10_CHIP_ID) {
  1049. dev_err(ov13b->dev, "chip id mismatch: %x!=%x\n",
  1050. OV13B10_CHIP_ID, val);
  1051. return -EIO;
  1052. }
  1053. ov13b->identified = true;
  1054. return 0;
  1055. }
  1056. static int ov13b10_power_off(struct device *dev)
  1057. {
  1058. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  1059. struct ov13b10 *ov13b10 = to_ov13b10(sd);
  1060. gpiod_set_value_cansleep(ov13b10->reset, 1);
  1061. if (ov13b10->avdd)
  1062. regulator_disable(ov13b10->avdd);
  1063. clk_disable_unprepare(ov13b10->img_clk);
  1064. return 0;
  1065. }
  1066. static int ov13b10_power_on(struct device *dev)
  1067. {
  1068. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  1069. struct ov13b10 *ov13b10 = to_ov13b10(sd);
  1070. int ret;
  1071. ret = clk_prepare_enable(ov13b10->img_clk);
  1072. if (ret < 0) {
  1073. dev_err(dev, "failed to enable imaging clock: %d", ret);
  1074. return ret;
  1075. }
  1076. if (ov13b10->avdd) {
  1077. ret = regulator_enable(ov13b10->avdd);
  1078. if (ret < 0) {
  1079. dev_err(dev, "failed to enable avdd: %d", ret);
  1080. clk_disable_unprepare(ov13b10->img_clk);
  1081. return ret;
  1082. }
  1083. }
  1084. gpiod_set_value_cansleep(ov13b10->reset, 0);
  1085. /* 5ms to wait ready after XSHUTDN assert */
  1086. usleep_range(5000, 5500);
  1087. return 0;
  1088. }
  1089. static int ov13b10_start_streaming(struct ov13b10 *ov13b)
  1090. {
  1091. const struct ov13b10_reg_list *reg_list;
  1092. int ret, link_freq_index;
  1093. ret = ov13b10_identify_module(ov13b);
  1094. if (ret)
  1095. return ret;
  1096. /* Get out of from software reset */
  1097. ret = ov13b10_write_reg(ov13b, OV13B10_REG_SOFTWARE_RST,
  1098. OV13B10_REG_VALUE_08BIT, OV13B10_SOFTWARE_RST);
  1099. if (ret) {
  1100. dev_err(ov13b->dev, "%s failed to set powerup registers\n",
  1101. __func__);
  1102. return ret;
  1103. }
  1104. link_freq_index = ov13b->cur_mode->link_freq_index;
  1105. reg_list = &link_freq_configs[link_freq_index].reg_list;
  1106. ret = ov13b10_write_reg_list(ov13b, reg_list);
  1107. if (ret) {
  1108. dev_err(ov13b->dev, "%s failed to set plls\n", __func__);
  1109. return ret;
  1110. }
  1111. /* Apply default values of current mode */
  1112. reg_list = &ov13b->cur_mode->reg_list;
  1113. ret = ov13b10_write_reg_list(ov13b, reg_list);
  1114. if (ret) {
  1115. dev_err(ov13b->dev, "%s failed to set mode\n", __func__);
  1116. return ret;
  1117. }
  1118. /* Apply customized values from user */
  1119. ret = __v4l2_ctrl_handler_setup(ov13b->sd.ctrl_handler);
  1120. if (ret)
  1121. return ret;
  1122. return ov13b10_write_reg(ov13b, OV13B10_REG_MODE_SELECT,
  1123. OV13B10_REG_VALUE_08BIT,
  1124. OV13B10_MODE_STREAMING);
  1125. }
  1126. /* Stop streaming */
  1127. static int ov13b10_stop_streaming(struct ov13b10 *ov13b)
  1128. {
  1129. return ov13b10_write_reg(ov13b, OV13B10_REG_MODE_SELECT,
  1130. OV13B10_REG_VALUE_08BIT, OV13B10_MODE_STANDBY);
  1131. }
  1132. static int ov13b10_set_stream(struct v4l2_subdev *sd, int enable)
  1133. {
  1134. struct ov13b10 *ov13b = to_ov13b10(sd);
  1135. int ret = 0;
  1136. mutex_lock(&ov13b->mutex);
  1137. if (enable) {
  1138. ret = pm_runtime_resume_and_get(ov13b->dev);
  1139. if (ret < 0)
  1140. goto err_unlock;
  1141. /*
  1142. * Apply default & customized values
  1143. * and then start streaming.
  1144. */
  1145. ret = ov13b10_start_streaming(ov13b);
  1146. if (ret)
  1147. goto err_rpm_put;
  1148. } else {
  1149. ov13b10_stop_streaming(ov13b);
  1150. pm_runtime_put(ov13b->dev);
  1151. }
  1152. mutex_unlock(&ov13b->mutex);
  1153. return ret;
  1154. err_rpm_put:
  1155. pm_runtime_put(ov13b->dev);
  1156. err_unlock:
  1157. mutex_unlock(&ov13b->mutex);
  1158. return ret;
  1159. }
  1160. static int ov13b10_suspend(struct device *dev)
  1161. {
  1162. ov13b10_power_off(dev);
  1163. return 0;
  1164. }
  1165. static int ov13b10_resume(struct device *dev)
  1166. {
  1167. return ov13b10_power_on(dev);
  1168. }
  1169. static const struct v4l2_subdev_video_ops ov13b10_video_ops = {
  1170. .s_stream = ov13b10_set_stream,
  1171. };
  1172. static const struct v4l2_subdev_pad_ops ov13b10_pad_ops = {
  1173. .enum_mbus_code = ov13b10_enum_mbus_code,
  1174. .get_fmt = ov13b10_get_pad_format,
  1175. .set_fmt = ov13b10_set_pad_format,
  1176. .enum_frame_size = ov13b10_enum_frame_size,
  1177. };
  1178. static const struct v4l2_subdev_ops ov13b10_subdev_ops = {
  1179. .video = &ov13b10_video_ops,
  1180. .pad = &ov13b10_pad_ops,
  1181. };
  1182. static const struct media_entity_operations ov13b10_subdev_entity_ops = {
  1183. .link_validate = v4l2_subdev_link_validate,
  1184. };
  1185. static const struct v4l2_subdev_internal_ops ov13b10_internal_ops = {
  1186. .open = ov13b10_open,
  1187. };
  1188. /* Initialize control handlers */
  1189. static int ov13b10_init_controls(struct ov13b10 *ov13b)
  1190. {
  1191. struct v4l2_fwnode_device_properties props;
  1192. struct v4l2_ctrl_handler *ctrl_hdlr;
  1193. s64 exposure_max;
  1194. s64 vblank_def;
  1195. s64 vblank_min;
  1196. s64 hblank;
  1197. s64 pixel_rate_min;
  1198. s64 pixel_rate_max;
  1199. const struct ov13b10_mode *mode;
  1200. u32 max;
  1201. int ret;
  1202. ctrl_hdlr = &ov13b->ctrl_handler;
  1203. ret = v4l2_ctrl_handler_init(ctrl_hdlr, 10);
  1204. if (ret)
  1205. return ret;
  1206. mutex_init(&ov13b->mutex);
  1207. ctrl_hdlr->lock = &ov13b->mutex;
  1208. max = ARRAY_SIZE(link_freq_menu_items) - 1;
  1209. ov13b->link_freq = v4l2_ctrl_new_int_menu(ctrl_hdlr,
  1210. &ov13b10_ctrl_ops,
  1211. V4L2_CID_LINK_FREQ,
  1212. max,
  1213. 0,
  1214. link_freq_menu_items);
  1215. if (ov13b->link_freq)
  1216. ov13b->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  1217. pixel_rate_max = link_freq_to_pixel_rate(link_freq_menu_items[0],
  1218. ov13b->data_lanes);
  1219. pixel_rate_min = 0;
  1220. /* By default, PIXEL_RATE is read only */
  1221. ov13b->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &ov13b10_ctrl_ops,
  1222. V4L2_CID_PIXEL_RATE,
  1223. pixel_rate_min, pixel_rate_max,
  1224. 1, pixel_rate_max);
  1225. mode = ov13b->cur_mode;
  1226. vblank_def = mode->vts_def - mode->height;
  1227. vblank_min = mode->vts_min - mode->height;
  1228. ov13b->vblank = v4l2_ctrl_new_std(ctrl_hdlr, &ov13b10_ctrl_ops,
  1229. V4L2_CID_VBLANK,
  1230. vblank_min,
  1231. OV13B10_VTS_MAX - mode->height, 1,
  1232. vblank_def);
  1233. hblank = mode->ppl - mode->width;
  1234. ov13b->hblank = v4l2_ctrl_new_std(ctrl_hdlr, &ov13b10_ctrl_ops,
  1235. V4L2_CID_HBLANK,
  1236. hblank, hblank, 1, hblank);
  1237. if (ov13b->hblank)
  1238. ov13b->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  1239. exposure_max = mode->vts_def - 8;
  1240. ov13b->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &ov13b10_ctrl_ops,
  1241. V4L2_CID_EXPOSURE,
  1242. OV13B10_EXPOSURE_MIN,
  1243. exposure_max, OV13B10_EXPOSURE_STEP,
  1244. exposure_max);
  1245. v4l2_ctrl_new_std(ctrl_hdlr, &ov13b10_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
  1246. OV13B10_ANA_GAIN_MIN, OV13B10_ANA_GAIN_MAX,
  1247. OV13B10_ANA_GAIN_STEP, OV13B10_ANA_GAIN_DEFAULT);
  1248. /* Digital gain */
  1249. v4l2_ctrl_new_std(ctrl_hdlr, &ov13b10_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
  1250. OV13B10_DGTL_GAIN_MIN, OV13B10_DGTL_GAIN_MAX,
  1251. OV13B10_DGTL_GAIN_STEP, OV13B10_DGTL_GAIN_DEFAULT);
  1252. v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &ov13b10_ctrl_ops,
  1253. V4L2_CID_TEST_PATTERN,
  1254. ARRAY_SIZE(ov13b10_test_pattern_menu) - 1,
  1255. 0, 0, ov13b10_test_pattern_menu);
  1256. v4l2_ctrl_new_std(ctrl_hdlr, &ov13b10_ctrl_ops,
  1257. V4L2_CID_HFLIP, 0, 1, 1, 0);
  1258. v4l2_ctrl_new_std(ctrl_hdlr, &ov13b10_ctrl_ops,
  1259. V4L2_CID_VFLIP, 0, 1, 1, 0);
  1260. if (ctrl_hdlr->error) {
  1261. ret = ctrl_hdlr->error;
  1262. dev_err(ov13b->dev, "%s control init failed (%d)\n",
  1263. __func__, ret);
  1264. goto error;
  1265. }
  1266. ret = v4l2_fwnode_device_parse(ov13b->dev, &props);
  1267. if (ret)
  1268. goto error;
  1269. ret = v4l2_ctrl_new_fwnode_properties(ctrl_hdlr, &ov13b10_ctrl_ops,
  1270. &props);
  1271. if (ret)
  1272. goto error;
  1273. ov13b->sd.ctrl_handler = ctrl_hdlr;
  1274. return 0;
  1275. error:
  1276. v4l2_ctrl_handler_free(ctrl_hdlr);
  1277. mutex_destroy(&ov13b->mutex);
  1278. return ret;
  1279. }
  1280. static void ov13b10_free_controls(struct ov13b10 *ov13b)
  1281. {
  1282. v4l2_ctrl_handler_free(ov13b->sd.ctrl_handler);
  1283. mutex_destroy(&ov13b->mutex);
  1284. }
  1285. static int ov13b10_get_pm_resources(struct ov13b10 *ov13b)
  1286. {
  1287. unsigned long freq;
  1288. int ret;
  1289. ov13b->reset = devm_gpiod_get_optional(ov13b->dev, "reset", GPIOD_OUT_LOW);
  1290. if (IS_ERR(ov13b->reset))
  1291. return dev_err_probe(ov13b->dev, PTR_ERR(ov13b->reset),
  1292. "failed to get reset gpio\n");
  1293. ov13b->img_clk = devm_v4l2_sensor_clk_get(ov13b->dev, NULL);
  1294. if (IS_ERR(ov13b->img_clk))
  1295. return dev_err_probe(ov13b->dev, PTR_ERR(ov13b->img_clk),
  1296. "failed to get imaging clock\n");
  1297. freq = clk_get_rate(ov13b->img_clk);
  1298. if (freq != OV13B10_EXT_CLK)
  1299. return dev_err_probe(ov13b->dev, -EINVAL,
  1300. "external clock %lu is not supported\n",
  1301. freq);
  1302. ov13b->avdd = devm_regulator_get_optional(ov13b->dev, "avdd");
  1303. if (IS_ERR(ov13b->avdd)) {
  1304. ret = PTR_ERR(ov13b->avdd);
  1305. ov13b->avdd = NULL;
  1306. if (ret != -ENODEV)
  1307. return dev_err_probe(ov13b->dev, ret,
  1308. "failed to get avdd regulator\n");
  1309. }
  1310. return 0;
  1311. }
  1312. static int ov13b10_check_hwcfg(struct ov13b10 *ov13b)
  1313. {
  1314. struct v4l2_fwnode_endpoint bus_cfg = {
  1315. .bus_type = V4L2_MBUS_CSI2_DPHY
  1316. };
  1317. struct device *dev = ov13b->dev;
  1318. struct fwnode_handle *ep;
  1319. struct fwnode_handle *fwnode = dev_fwnode(dev);
  1320. unsigned int i, j;
  1321. int ret;
  1322. u8 dlane;
  1323. if (!fwnode)
  1324. return -ENXIO;
  1325. ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
  1326. if (!ep)
  1327. return -EPROBE_DEFER;
  1328. ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
  1329. fwnode_handle_put(ep);
  1330. if (ret)
  1331. return ret;
  1332. dlane = bus_cfg.bus.mipi_csi2.num_data_lanes;
  1333. switch (dlane) {
  1334. case OV13B10_4_DATA_LANES:
  1335. ov13b->supported_modes = supported_4_lanes_modes;
  1336. ov13b->supported_modes_num =
  1337. ARRAY_SIZE(supported_4_lanes_modes);
  1338. break;
  1339. case OV13B10_2_DATA_LANES:
  1340. ov13b->supported_modes = supported_2_lanes_modes;
  1341. ov13b->supported_modes_num =
  1342. ARRAY_SIZE(supported_2_lanes_modes);
  1343. break;
  1344. default:
  1345. dev_err(dev, "number of CSI2 data lanes %d is not supported",
  1346. dlane);
  1347. ret = -EINVAL;
  1348. goto out_err;
  1349. }
  1350. ov13b->data_lanes = dlane;
  1351. ov13b->cur_mode = ov13b->supported_modes;
  1352. dev_dbg(dev, "%u lanes with %u modes selected\n",
  1353. ov13b->data_lanes, ov13b->supported_modes_num);
  1354. if (!bus_cfg.nr_of_link_frequencies) {
  1355. dev_err(dev, "no link frequencies defined");
  1356. ret = -EINVAL;
  1357. goto out_err;
  1358. }
  1359. for (i = 0; i < ARRAY_SIZE(link_freq_menu_items); i++) {
  1360. for (j = 0; j < bus_cfg.nr_of_link_frequencies; j++) {
  1361. if (link_freq_menu_items[i] ==
  1362. bus_cfg.link_frequencies[j])
  1363. break;
  1364. }
  1365. if (j == bus_cfg.nr_of_link_frequencies) {
  1366. dev_err(dev, "no link frequency %lld supported",
  1367. link_freq_menu_items[i]);
  1368. ret = -EINVAL;
  1369. goto out_err;
  1370. }
  1371. }
  1372. out_err:
  1373. v4l2_fwnode_endpoint_free(&bus_cfg);
  1374. return ret;
  1375. }
  1376. static int ov13b10_probe(struct i2c_client *client)
  1377. {
  1378. struct ov13b10 *ov13b;
  1379. bool full_power;
  1380. int ret;
  1381. ov13b = devm_kzalloc(&client->dev, sizeof(*ov13b), GFP_KERNEL);
  1382. if (!ov13b)
  1383. return -ENOMEM;
  1384. ov13b->dev = &client->dev;
  1385. /* Check HW config */
  1386. ret = ov13b10_check_hwcfg(ov13b);
  1387. if (ret) {
  1388. dev_err(ov13b->dev, "failed to check hwcfg: %d", ret);
  1389. return ret;
  1390. }
  1391. /* Initialize subdev */
  1392. v4l2_i2c_subdev_init(&ov13b->sd, client, &ov13b10_subdev_ops);
  1393. ret = ov13b10_get_pm_resources(ov13b);
  1394. if (ret)
  1395. return ret;
  1396. full_power = acpi_dev_state_d0(ov13b->dev);
  1397. if (full_power) {
  1398. ret = ov13b10_power_on(ov13b->dev);
  1399. if (ret) {
  1400. dev_err(ov13b->dev, "failed to power on\n");
  1401. return ret;
  1402. }
  1403. /* Check module identity */
  1404. ret = ov13b10_identify_module(ov13b);
  1405. if (ret) {
  1406. dev_err(ov13b->dev, "failed to find sensor: %d\n", ret);
  1407. goto error_power_off;
  1408. }
  1409. }
  1410. ret = ov13b10_init_controls(ov13b);
  1411. if (ret)
  1412. goto error_power_off;
  1413. /* Initialize subdev */
  1414. ov13b->sd.internal_ops = &ov13b10_internal_ops;
  1415. ov13b->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  1416. ov13b->sd.entity.ops = &ov13b10_subdev_entity_ops;
  1417. ov13b->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
  1418. /* Initialize source pad */
  1419. ov13b->pad.flags = MEDIA_PAD_FL_SOURCE;
  1420. ret = media_entity_pads_init(&ov13b->sd.entity, 1, &ov13b->pad);
  1421. if (ret) {
  1422. dev_err(ov13b->dev, "%s failed:%d\n", __func__, ret);
  1423. goto error_handler_free;
  1424. }
  1425. /*
  1426. * Device is already turned on by i2c-core with ACPI domain PM.
  1427. * Enable runtime PM and turn off the device.
  1428. */
  1429. /* Set the device's state to active if it's in D0 state. */
  1430. if (full_power)
  1431. pm_runtime_set_active(ov13b->dev);
  1432. pm_runtime_enable(ov13b->dev);
  1433. pm_runtime_idle(ov13b->dev);
  1434. ret = v4l2_async_register_subdev_sensor(&ov13b->sd);
  1435. if (ret < 0)
  1436. goto error_media_entity_runtime_pm;
  1437. return 0;
  1438. error_media_entity_runtime_pm:
  1439. pm_runtime_disable(ov13b->dev);
  1440. if (full_power)
  1441. pm_runtime_set_suspended(ov13b->dev);
  1442. media_entity_cleanup(&ov13b->sd.entity);
  1443. error_handler_free:
  1444. ov13b10_free_controls(ov13b);
  1445. dev_err(ov13b->dev, "%s failed:%d\n", __func__, ret);
  1446. error_power_off:
  1447. ov13b10_power_off(ov13b->dev);
  1448. return ret;
  1449. }
  1450. static void ov13b10_remove(struct i2c_client *client)
  1451. {
  1452. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1453. struct ov13b10 *ov13b = to_ov13b10(sd);
  1454. v4l2_async_unregister_subdev(sd);
  1455. media_entity_cleanup(&sd->entity);
  1456. ov13b10_free_controls(ov13b);
  1457. pm_runtime_disable(ov13b->dev);
  1458. pm_runtime_set_suspended(ov13b->dev);
  1459. }
  1460. static DEFINE_RUNTIME_DEV_PM_OPS(ov13b10_pm_ops, ov13b10_suspend,
  1461. ov13b10_resume, NULL);
  1462. #ifdef CONFIG_ACPI
  1463. static const struct acpi_device_id ov13b10_acpi_ids[] = {
  1464. {"OVTIDB10"},
  1465. {"OVTI13B1"},
  1466. {"OMNI13B1"}, /* ASUS ROG Flow Z13 (GZ302) uses this ACPI ID */
  1467. { /* sentinel */ }
  1468. };
  1469. MODULE_DEVICE_TABLE(acpi, ov13b10_acpi_ids);
  1470. #endif
  1471. static struct i2c_driver ov13b10_i2c_driver = {
  1472. .driver = {
  1473. .name = "ov13b10",
  1474. .pm = pm_ptr(&ov13b10_pm_ops),
  1475. .acpi_match_table = ACPI_PTR(ov13b10_acpi_ids),
  1476. },
  1477. .probe = ov13b10_probe,
  1478. .remove = ov13b10_remove,
  1479. .flags = I2C_DRV_ACPI_WAIVE_D0_PROBE,
  1480. };
  1481. module_i2c_driver(ov13b10_i2c_driver);
  1482. MODULE_AUTHOR("Kao, Arec <arec.kao@intel.com>");
  1483. MODULE_DESCRIPTION("Omnivision ov13b10 sensor driver");
  1484. MODULE_LICENSE("GPL v2");