ov13b10.c 33 KB

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