ov08d10.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2022 Intel Corporation.
  3. #include <linux/acpi.h>
  4. #include <linux/clk.h>
  5. #include <linux/delay.h>
  6. #include <linux/i2c.h>
  7. #include <linux/module.h>
  8. #include <linux/pm_runtime.h>
  9. #include <linux/regulator/consumer.h>
  10. #include <media/v4l2-ctrls.h>
  11. #include <media/v4l2-device.h>
  12. #include <media/v4l2-fwnode.h>
  13. #define OV08D10_SCLK 144000000ULL
  14. #define OV08D10_XVCLK_19_2 19200000
  15. #define OV08D10_ROWCLK 36000
  16. #define OV08D10_DATA_LANES 2
  17. #define OV08D10_RGB_DEPTH 10
  18. #define OV08D10_REG_PAGE 0xfd
  19. #define OV08D10_REG_GLOBAL_EFFECTIVE 0x01
  20. #define OV08D10_REG_CHIP_ID_0 0x00
  21. #define OV08D10_REG_CHIP_ID_1 0x01
  22. #define OV08D10_ID_MASK GENMASK(15, 0)
  23. #define OV08D10_CHIP_ID 0x5608
  24. #define OV08D10_REG_MODE_SELECT 0xa0
  25. #define OV08D10_MODE_STANDBY 0x00
  26. #define OV08D10_MODE_STREAMING 0x01
  27. /* vertical-timings from sensor */
  28. #define OV08D10_REG_VTS_H 0x05
  29. #define OV08D10_REG_VTS_L 0x06
  30. #define OV08D10_VTS_MAX 0x7fff
  31. /* Exposure controls from sensor */
  32. #define OV08D10_REG_EXPOSURE_H 0x02
  33. #define OV08D10_REG_EXPOSURE_M 0x03
  34. #define OV08D10_REG_EXPOSURE_L 0x04
  35. #define OV08D10_EXPOSURE_MIN 6
  36. #define OV08D10_EXPOSURE_MAX_MARGIN 6
  37. #define OV08D10_EXPOSURE_STEP 1
  38. /* Analog gain controls from sensor */
  39. #define OV08D10_REG_ANALOG_GAIN 0x24
  40. #define OV08D10_ANAL_GAIN_MIN 128
  41. #define OV08D10_ANAL_GAIN_MAX 2047
  42. #define OV08D10_ANAL_GAIN_STEP 1
  43. /* Digital gain controls from sensor */
  44. #define OV08D10_REG_MWB_DGAIN_C 0x21
  45. #define OV08D10_REG_MWB_DGAIN_F 0x22
  46. #define OV08D10_DGTL_GAIN_MIN 0
  47. #define OV08D10_DGTL_GAIN_MAX 4095
  48. #define OV08D10_DGTL_GAIN_STEP 1
  49. #define OV08D10_DGTL_GAIN_DEFAULT 1024
  50. /* Test Pattern Control */
  51. #define OV08D10_REG_TEST_PATTERN 0x12
  52. #define OV08D10_TEST_PATTERN_ENABLE 0x01
  53. #define OV08D10_TEST_PATTERN_DISABLE 0x00
  54. /* Flip Mirror Controls from sensor */
  55. #define OV08D10_REG_FLIP_OPT 0x32
  56. #define OV08D10_REG_FLIP_MASK 0x3
  57. #define to_ov08d10(_sd) container_of(_sd, struct ov08d10, sd)
  58. struct ov08d10_reg {
  59. u8 address;
  60. u8 val;
  61. };
  62. struct ov08d10_reg_list {
  63. u32 num_of_regs;
  64. const struct ov08d10_reg *regs;
  65. };
  66. struct ov08d10_link_freq_config {
  67. const struct ov08d10_reg_list reg_list;
  68. };
  69. struct ov08d10_mode {
  70. /* Frame width in pixels */
  71. u32 width;
  72. /* Frame height in pixels */
  73. u32 height;
  74. /* Horizontal timining size */
  75. u32 hts;
  76. /* Default vertical timining size */
  77. u32 vts_def;
  78. /* Min vertical timining size */
  79. u32 vts_min;
  80. /* Link frequency needed for this resolution */
  81. u32 link_freq_index;
  82. /* Sensor register settings for this resolution */
  83. const struct ov08d10_reg_list reg_list;
  84. /* Number of data lanes */
  85. u8 data_lanes;
  86. };
  87. /* 3280x2460, 3264x2448 need 720Mbps/lane, 2 lanes */
  88. static const struct ov08d10_reg mipi_data_rate_720mbps[] = {
  89. {0xfd, 0x00},
  90. {0x11, 0x2a},
  91. {0x14, 0x43},
  92. {0x1a, 0x04},
  93. {0x1b, 0xe1},
  94. {0x1e, 0x13},
  95. {0xb7, 0x02}
  96. };
  97. /* 1632x1224 needs 360Mbps/lane, 2 lanes */
  98. static const struct ov08d10_reg mipi_data_rate_360mbps[] = {
  99. {0xfd, 0x00},
  100. {0x1a, 0x04},
  101. {0x1b, 0xe1},
  102. {0x1d, 0x00},
  103. {0x1c, 0x19},
  104. {0x11, 0x2a},
  105. {0x14, 0x54},
  106. {0x1e, 0x13},
  107. {0xb7, 0x02}
  108. };
  109. static const struct ov08d10_reg lane_2_mode_3280x2460[] = {
  110. /* 3280x2460 resolution */
  111. {0xfd, 0x01},
  112. {0x12, 0x00},
  113. {0x03, 0x12},
  114. {0x04, 0x58},
  115. {0x07, 0x05},
  116. {0x21, 0x02},
  117. {0x24, 0x30},
  118. {0x33, 0x03},
  119. {0x01, 0x03},
  120. {0x19, 0x10},
  121. {0x42, 0x55},
  122. {0x43, 0x00},
  123. {0x47, 0x07},
  124. {0x48, 0x08},
  125. {0xb2, 0x7f},
  126. {0xb3, 0x7b},
  127. {0xbd, 0x08},
  128. {0xd2, 0x57},
  129. {0xd3, 0x10},
  130. {0xd4, 0x08},
  131. {0xd5, 0x08},
  132. {0xd6, 0x06},
  133. {0xb1, 0x00},
  134. {0xb4, 0x00},
  135. {0xb7, 0x0a},
  136. {0xbc, 0x44},
  137. {0xbf, 0x48},
  138. {0xc1, 0x10},
  139. {0xc3, 0x24},
  140. {0xc8, 0x03},
  141. {0xc9, 0xf8},
  142. {0xe1, 0x33},
  143. {0xe2, 0xbb},
  144. {0x51, 0x0c},
  145. {0x52, 0x0a},
  146. {0x57, 0x8c},
  147. {0x59, 0x09},
  148. {0x5a, 0x08},
  149. {0x5e, 0x10},
  150. {0x60, 0x02},
  151. {0x6d, 0x5c},
  152. {0x76, 0x16},
  153. {0x7c, 0x11},
  154. {0x90, 0x28},
  155. {0x91, 0x16},
  156. {0x92, 0x1c},
  157. {0x93, 0x24},
  158. {0x95, 0x48},
  159. {0x9c, 0x06},
  160. {0xca, 0x0c},
  161. {0xce, 0x0d},
  162. {0xfd, 0x01},
  163. {0xc0, 0x00},
  164. {0xdd, 0x18},
  165. {0xde, 0x19},
  166. {0xdf, 0x32},
  167. {0xe0, 0x70},
  168. {0xfd, 0x01},
  169. {0xc2, 0x05},
  170. {0xd7, 0x88},
  171. {0xd8, 0x77},
  172. {0xd9, 0x00},
  173. {0xfd, 0x07},
  174. {0x00, 0xf8},
  175. {0x01, 0x2b},
  176. {0x05, 0x40},
  177. {0x08, 0x06},
  178. {0x09, 0x11},
  179. {0x28, 0x6f},
  180. {0x2a, 0x20},
  181. {0x2b, 0x05},
  182. {0x5e, 0x10},
  183. {0x52, 0x00},
  184. {0x53, 0x7c},
  185. {0x54, 0x00},
  186. {0x55, 0x7c},
  187. {0x56, 0x00},
  188. {0x57, 0x7c},
  189. {0x58, 0x00},
  190. {0x59, 0x7c},
  191. {0xfd, 0x02},
  192. {0x9a, 0x30},
  193. {0xa8, 0x02},
  194. {0xfd, 0x02},
  195. {0xa1, 0x01},
  196. {0xa2, 0x09},
  197. {0xa3, 0x9c},
  198. {0xa5, 0x00},
  199. {0xa6, 0x0c},
  200. {0xa7, 0xd0},
  201. {0xfd, 0x00},
  202. {0x24, 0x01},
  203. {0xc0, 0x16},
  204. {0xc1, 0x08},
  205. {0xc2, 0x30},
  206. {0x8e, 0x0c},
  207. {0x8f, 0xd0},
  208. {0x90, 0x09},
  209. {0x91, 0x9c},
  210. {0xfd, 0x05},
  211. {0x04, 0x40},
  212. {0x07, 0x00},
  213. {0x0d, 0x01},
  214. {0x0f, 0x01},
  215. {0x10, 0x00},
  216. {0x11, 0x00},
  217. {0x12, 0x0c},
  218. {0x13, 0xcf},
  219. {0x14, 0x00},
  220. {0x15, 0x00},
  221. {0xfd, 0x00},
  222. {0x20, 0x0f},
  223. {0xe7, 0x03},
  224. {0xe7, 0x00}
  225. };
  226. static const struct ov08d10_reg lane_2_mode_3264x2448[] = {
  227. /* 3264x2448 resolution */
  228. {0xfd, 0x01},
  229. {0x12, 0x00},
  230. {0x03, 0x12},
  231. {0x04, 0x58},
  232. {0x07, 0x05},
  233. {0x21, 0x02},
  234. {0x24, 0x30},
  235. {0x33, 0x03},
  236. {0x01, 0x03},
  237. {0x19, 0x10},
  238. {0x42, 0x55},
  239. {0x43, 0x00},
  240. {0x47, 0x07},
  241. {0x48, 0x08},
  242. {0xb2, 0x7f},
  243. {0xb3, 0x7b},
  244. {0xbd, 0x08},
  245. {0xd2, 0x57},
  246. {0xd3, 0x10},
  247. {0xd4, 0x08},
  248. {0xd5, 0x08},
  249. {0xd6, 0x06},
  250. {0xb1, 0x00},
  251. {0xb4, 0x00},
  252. {0xb7, 0x0a},
  253. {0xbc, 0x44},
  254. {0xbf, 0x48},
  255. {0xc1, 0x10},
  256. {0xc3, 0x24},
  257. {0xc8, 0x03},
  258. {0xc9, 0xf8},
  259. {0xe1, 0x33},
  260. {0xe2, 0xbb},
  261. {0x51, 0x0c},
  262. {0x52, 0x0a},
  263. {0x57, 0x8c},
  264. {0x59, 0x09},
  265. {0x5a, 0x08},
  266. {0x5e, 0x10},
  267. {0x60, 0x02},
  268. {0x6d, 0x5c},
  269. {0x76, 0x16},
  270. {0x7c, 0x11},
  271. {0x90, 0x28},
  272. {0x91, 0x16},
  273. {0x92, 0x1c},
  274. {0x93, 0x24},
  275. {0x95, 0x48},
  276. {0x9c, 0x06},
  277. {0xca, 0x0c},
  278. {0xce, 0x0d},
  279. {0xfd, 0x01},
  280. {0xc0, 0x00},
  281. {0xdd, 0x18},
  282. {0xde, 0x19},
  283. {0xdf, 0x32},
  284. {0xe0, 0x70},
  285. {0xfd, 0x01},
  286. {0xc2, 0x05},
  287. {0xd7, 0x88},
  288. {0xd8, 0x77},
  289. {0xd9, 0x00},
  290. {0xfd, 0x07},
  291. {0x00, 0xf8},
  292. {0x01, 0x2b},
  293. {0x05, 0x40},
  294. {0x08, 0x06},
  295. {0x09, 0x11},
  296. {0x28, 0x6f},
  297. {0x2a, 0x20},
  298. {0x2b, 0x05},
  299. {0x5e, 0x10},
  300. {0x52, 0x00},
  301. {0x53, 0x7c},
  302. {0x54, 0x00},
  303. {0x55, 0x7c},
  304. {0x56, 0x00},
  305. {0x57, 0x7c},
  306. {0x58, 0x00},
  307. {0x59, 0x7c},
  308. {0xfd, 0x02},
  309. {0x9a, 0x30},
  310. {0xa8, 0x02},
  311. {0xfd, 0x02},
  312. {0xa1, 0x09},
  313. {0xa2, 0x09},
  314. {0xa3, 0x90},
  315. {0xa5, 0x08},
  316. {0xa6, 0x0c},
  317. {0xa7, 0xc0},
  318. {0xfd, 0x00},
  319. {0x24, 0x01},
  320. {0xc0, 0x16},
  321. {0xc1, 0x08},
  322. {0xc2, 0x30},
  323. {0x8e, 0x0c},
  324. {0x8f, 0xc0},
  325. {0x90, 0x09},
  326. {0x91, 0x90},
  327. {0xfd, 0x05},
  328. {0x04, 0x40},
  329. {0x07, 0x00},
  330. {0x0d, 0x01},
  331. {0x0f, 0x01},
  332. {0x10, 0x00},
  333. {0x11, 0x00},
  334. {0x12, 0x0c},
  335. {0x13, 0xcf},
  336. {0x14, 0x00},
  337. {0x15, 0x00},
  338. {0xfd, 0x00},
  339. {0x20, 0x0f},
  340. {0xe7, 0x03},
  341. {0xe7, 0x00}
  342. };
  343. static const struct ov08d10_reg lane_2_mode_1632x1224[] = {
  344. /* 1640x1232 resolution */
  345. {0xfd, 0x01},
  346. {0x1a, 0x0a},
  347. {0x1b, 0x08},
  348. {0x2a, 0x01},
  349. {0x2b, 0x9a},
  350. {0xfd, 0x01},
  351. {0x12, 0x00},
  352. {0x03, 0x05},
  353. {0x04, 0xe2},
  354. {0x07, 0x05},
  355. {0x21, 0x02},
  356. {0x24, 0x30},
  357. {0x33, 0x03},
  358. {0x31, 0x06},
  359. {0x33, 0x03},
  360. {0x01, 0x03},
  361. {0x19, 0x10},
  362. {0x42, 0x55},
  363. {0x43, 0x00},
  364. {0x47, 0x07},
  365. {0x48, 0x08},
  366. {0xb2, 0x7f},
  367. {0xb3, 0x7b},
  368. {0xbd, 0x08},
  369. {0xd2, 0x57},
  370. {0xd3, 0x10},
  371. {0xd4, 0x08},
  372. {0xd5, 0x08},
  373. {0xd6, 0x06},
  374. {0xb1, 0x00},
  375. {0xb4, 0x00},
  376. {0xb7, 0x0a},
  377. {0xbc, 0x44},
  378. {0xbf, 0x48},
  379. {0xc1, 0x10},
  380. {0xc3, 0x24},
  381. {0xc8, 0x03},
  382. {0xc9, 0xf8},
  383. {0xe1, 0x33},
  384. {0xe2, 0xbb},
  385. {0x51, 0x0c},
  386. {0x52, 0x0a},
  387. {0x57, 0x8c},
  388. {0x59, 0x09},
  389. {0x5a, 0x08},
  390. {0x5e, 0x10},
  391. {0x60, 0x02},
  392. {0x6d, 0x5c},
  393. {0x76, 0x16},
  394. {0x7c, 0x1a},
  395. {0x90, 0x28},
  396. {0x91, 0x16},
  397. {0x92, 0x1c},
  398. {0x93, 0x24},
  399. {0x95, 0x48},
  400. {0x9c, 0x06},
  401. {0xca, 0x0c},
  402. {0xce, 0x0d},
  403. {0xfd, 0x01},
  404. {0xc0, 0x00},
  405. {0xdd, 0x18},
  406. {0xde, 0x19},
  407. {0xdf, 0x32},
  408. {0xe0, 0x70},
  409. {0xfd, 0x01},
  410. {0xc2, 0x05},
  411. {0xd7, 0x88},
  412. {0xd8, 0x77},
  413. {0xd9, 0x00},
  414. {0xfd, 0x07},
  415. {0x00, 0xf8},
  416. {0x01, 0x2b},
  417. {0x05, 0x40},
  418. {0x08, 0x03},
  419. {0x09, 0x08},
  420. {0x28, 0x6f},
  421. {0x2a, 0x20},
  422. {0x2b, 0x05},
  423. {0x2c, 0x01},
  424. {0x50, 0x02},
  425. {0x51, 0x03},
  426. {0x5e, 0x00},
  427. {0x52, 0x00},
  428. {0x53, 0x7c},
  429. {0x54, 0x00},
  430. {0x55, 0x7c},
  431. {0x56, 0x00},
  432. {0x57, 0x7c},
  433. {0x58, 0x00},
  434. {0x59, 0x7c},
  435. {0xfd, 0x02},
  436. {0x9a, 0x30},
  437. {0xa8, 0x02},
  438. {0xfd, 0x02},
  439. {0xa9, 0x04},
  440. {0xaa, 0xd0},
  441. {0xab, 0x06},
  442. {0xac, 0x68},
  443. {0xa1, 0x09},
  444. {0xa2, 0x04},
  445. {0xa3, 0xc8},
  446. {0xa5, 0x04},
  447. {0xa6, 0x06},
  448. {0xa7, 0x60},
  449. {0xfd, 0x05},
  450. {0x06, 0x80},
  451. {0x18, 0x06},
  452. {0x19, 0x68},
  453. {0xfd, 0x00},
  454. {0x24, 0x01},
  455. {0xc0, 0x16},
  456. {0xc1, 0x08},
  457. {0xc2, 0x30},
  458. {0x8e, 0x06},
  459. {0x8f, 0x60},
  460. {0x90, 0x04},
  461. {0x91, 0xc8},
  462. {0x93, 0x0e},
  463. {0x94, 0x77},
  464. {0x95, 0x77},
  465. {0x96, 0x10},
  466. {0x98, 0x88},
  467. {0x9c, 0x1a},
  468. {0xfd, 0x05},
  469. {0x04, 0x40},
  470. {0x07, 0x99},
  471. {0x0d, 0x03},
  472. {0x0f, 0x03},
  473. {0x10, 0x00},
  474. {0x11, 0x00},
  475. {0x12, 0x0c},
  476. {0x13, 0xcf},
  477. {0x14, 0x00},
  478. {0x15, 0x00},
  479. {0xfd, 0x00},
  480. {0x20, 0x0f},
  481. {0xe7, 0x03},
  482. {0xe7, 0x00},
  483. };
  484. static const char * const ov08d10_test_pattern_menu[] = {
  485. "Disabled",
  486. "Standard Color Bar",
  487. };
  488. struct ov08d10 {
  489. struct device *dev;
  490. struct clk *clk;
  491. struct v4l2_subdev sd;
  492. struct media_pad pad;
  493. struct v4l2_ctrl_handler ctrl_handler;
  494. /* V4L2 Controls */
  495. struct v4l2_ctrl *link_freq;
  496. struct v4l2_ctrl *pixel_rate;
  497. struct v4l2_ctrl *vblank;
  498. struct v4l2_ctrl *hblank;
  499. struct v4l2_ctrl *vflip;
  500. struct v4l2_ctrl *hflip;
  501. struct v4l2_ctrl *exposure;
  502. /* Current mode */
  503. const struct ov08d10_mode *cur_mode;
  504. /* To serialize asynchronus callbacks */
  505. struct mutex mutex;
  506. /* lanes index */
  507. u8 nlanes;
  508. const struct ov08d10_lane_cfg *priv_lane;
  509. u8 modes_size;
  510. };
  511. struct ov08d10_lane_cfg {
  512. const s64 link_freq_menu[2];
  513. const struct ov08d10_link_freq_config link_freq_configs[2];
  514. const struct ov08d10_mode sp_modes[3];
  515. };
  516. static const struct ov08d10_lane_cfg lane_cfg_2 = {
  517. {
  518. 720000000,
  519. 360000000,
  520. },
  521. {{
  522. .reg_list = {
  523. .num_of_regs =
  524. ARRAY_SIZE(mipi_data_rate_720mbps),
  525. .regs = mipi_data_rate_720mbps,
  526. }
  527. },
  528. {
  529. .reg_list = {
  530. .num_of_regs =
  531. ARRAY_SIZE(mipi_data_rate_360mbps),
  532. .regs = mipi_data_rate_360mbps,
  533. }
  534. }},
  535. {{
  536. .width = 3280,
  537. .height = 2460,
  538. .hts = 1840,
  539. .vts_def = 2504,
  540. .vts_min = 2504,
  541. .reg_list = {
  542. .num_of_regs = ARRAY_SIZE(lane_2_mode_3280x2460),
  543. .regs = lane_2_mode_3280x2460,
  544. },
  545. .link_freq_index = 0,
  546. .data_lanes = 2,
  547. },
  548. {
  549. .width = 3264,
  550. .height = 2448,
  551. .hts = 1840,
  552. .vts_def = 2504,
  553. .vts_min = 2504,
  554. .reg_list = {
  555. .num_of_regs = ARRAY_SIZE(lane_2_mode_3264x2448),
  556. .regs = lane_2_mode_3264x2448,
  557. },
  558. .link_freq_index = 0,
  559. .data_lanes = 2,
  560. },
  561. {
  562. .width = 1632,
  563. .height = 1224,
  564. .hts = 1912,
  565. .vts_def = 3736,
  566. .vts_min = 3736,
  567. .reg_list = {
  568. .num_of_regs = ARRAY_SIZE(lane_2_mode_1632x1224),
  569. .regs = lane_2_mode_1632x1224,
  570. },
  571. .link_freq_index = 1,
  572. .data_lanes = 2,
  573. }}
  574. };
  575. static u32 ov08d10_get_format_code(struct ov08d10 *ov08d10)
  576. {
  577. static const u32 codes[2][2] = {
  578. { MEDIA_BUS_FMT_SGRBG10_1X10, MEDIA_BUS_FMT_SRGGB10_1X10},
  579. { MEDIA_BUS_FMT_SBGGR10_1X10, MEDIA_BUS_FMT_SGBRG10_1X10},
  580. };
  581. return codes[ov08d10->vflip->val][ov08d10->hflip->val];
  582. }
  583. static unsigned int ov08d10_modes_num(const struct ov08d10 *ov08d10)
  584. {
  585. unsigned int i, count = 0;
  586. for (i = 0; i < ARRAY_SIZE(ov08d10->priv_lane->sp_modes); i++) {
  587. if (ov08d10->priv_lane->sp_modes[i].width == 0)
  588. break;
  589. count++;
  590. }
  591. return count;
  592. }
  593. static u64 to_rate(const s64 *link_freq_menu,
  594. u32 f_index, u8 nlanes)
  595. {
  596. u64 pixel_rate = link_freq_menu[f_index] * 2 * nlanes;
  597. do_div(pixel_rate, OV08D10_RGB_DEPTH);
  598. return pixel_rate;
  599. }
  600. static u64 to_pixels_per_line(const s64 *link_freq_menu, u32 hts,
  601. u32 f_index, u8 nlanes)
  602. {
  603. u64 ppl = hts * to_rate(link_freq_menu, f_index, nlanes);
  604. do_div(ppl, OV08D10_SCLK);
  605. return ppl;
  606. }
  607. static int ov08d10_write_reg_list(struct ov08d10 *ov08d10,
  608. const struct ov08d10_reg_list *r_list)
  609. {
  610. struct i2c_client *client = v4l2_get_subdevdata(&ov08d10->sd);
  611. unsigned int i;
  612. int ret;
  613. for (i = 0; i < r_list->num_of_regs; i++) {
  614. ret = i2c_smbus_write_byte_data(client, r_list->regs[i].address,
  615. r_list->regs[i].val);
  616. if (ret) {
  617. dev_err_ratelimited(ov08d10->dev,
  618. "failed to write reg 0x%2.2x. error = %d",
  619. r_list->regs[i].address, ret);
  620. return ret;
  621. }
  622. }
  623. return 0;
  624. }
  625. static int ov08d10_update_analog_gain(struct ov08d10 *ov08d10, u32 a_gain)
  626. {
  627. struct i2c_client *client = v4l2_get_subdevdata(&ov08d10->sd);
  628. u8 val;
  629. int ret;
  630. val = ((a_gain >> 3) & 0xFF);
  631. /* CIS control registers */
  632. ret = i2c_smbus_write_byte_data(client, OV08D10_REG_PAGE, 0x01);
  633. if (ret < 0)
  634. return ret;
  635. /* update AGAIN */
  636. ret = i2c_smbus_write_byte_data(client, OV08D10_REG_ANALOG_GAIN, val);
  637. if (ret < 0)
  638. return ret;
  639. return i2c_smbus_write_byte_data(client,
  640. OV08D10_REG_GLOBAL_EFFECTIVE, 0x01);
  641. }
  642. static int ov08d10_update_digital_gain(struct ov08d10 *ov08d10, u32 d_gain)
  643. {
  644. struct i2c_client *client = v4l2_get_subdevdata(&ov08d10->sd);
  645. u8 val;
  646. int ret;
  647. d_gain = (d_gain >> 1);
  648. /* CIS control registers */
  649. ret = i2c_smbus_write_byte_data(client, OV08D10_REG_PAGE, 0x01);
  650. if (ret < 0)
  651. return ret;
  652. val = ((d_gain >> 8) & 0x3F);
  653. /* update DGAIN */
  654. ret = i2c_smbus_write_byte_data(client, OV08D10_REG_MWB_DGAIN_C, val);
  655. if (ret < 0)
  656. return ret;
  657. val = d_gain & 0xFF;
  658. ret = i2c_smbus_write_byte_data(client, OV08D10_REG_MWB_DGAIN_F, val);
  659. if (ret < 0)
  660. return ret;
  661. return i2c_smbus_write_byte_data(client,
  662. OV08D10_REG_GLOBAL_EFFECTIVE, 0x01);
  663. }
  664. static int ov08d10_set_exposure(struct ov08d10 *ov08d10, u32 exposure)
  665. {
  666. struct i2c_client *client = v4l2_get_subdevdata(&ov08d10->sd);
  667. u8 val;
  668. u8 hts_h, hts_l;
  669. u32 hts, cur_vts, exp_cal;
  670. int ret;
  671. cur_vts = ov08d10->cur_mode->vts_def;
  672. ret = i2c_smbus_write_byte_data(client, OV08D10_REG_PAGE, 0x01);
  673. if (ret < 0)
  674. return ret;
  675. hts_h = i2c_smbus_read_byte_data(client, 0x37);
  676. hts_l = i2c_smbus_read_byte_data(client, 0x38);
  677. hts = ((hts_h << 8) | (hts_l));
  678. exp_cal = 66 * OV08D10_ROWCLK / hts;
  679. exposure = exposure * exp_cal / (cur_vts - OV08D10_EXPOSURE_MAX_MARGIN);
  680. /* CIS control registers */
  681. ret = i2c_smbus_write_byte_data(client, OV08D10_REG_PAGE, 0x01);
  682. if (ret < 0)
  683. return ret;
  684. /* update exposure */
  685. val = ((exposure >> 16) & 0xFF);
  686. ret = i2c_smbus_write_byte_data(client, OV08D10_REG_EXPOSURE_H, val);
  687. if (ret < 0)
  688. return ret;
  689. val = ((exposure >> 8) & 0xFF);
  690. ret = i2c_smbus_write_byte_data(client, OV08D10_REG_EXPOSURE_M, val);
  691. if (ret < 0)
  692. return ret;
  693. val = exposure & 0xFF;
  694. ret = i2c_smbus_write_byte_data(client, OV08D10_REG_EXPOSURE_L, val);
  695. if (ret < 0)
  696. return ret;
  697. return i2c_smbus_write_byte_data(client,
  698. OV08D10_REG_GLOBAL_EFFECTIVE, 0x01);
  699. }
  700. static int ov08d10_set_vblank(struct ov08d10 *ov08d10, u32 vblank)
  701. {
  702. struct i2c_client *client = v4l2_get_subdevdata(&ov08d10->sd);
  703. u8 val;
  704. int ret;
  705. /* CIS control registers */
  706. ret = i2c_smbus_write_byte_data(client, OV08D10_REG_PAGE, 0x01);
  707. if (ret < 0)
  708. return ret;
  709. val = ((vblank >> 8) & 0xFF);
  710. /* update vblank */
  711. ret = i2c_smbus_write_byte_data(client, OV08D10_REG_VTS_H, val);
  712. if (ret < 0)
  713. return ret;
  714. val = vblank & 0xFF;
  715. ret = i2c_smbus_write_byte_data(client, OV08D10_REG_VTS_L, val);
  716. if (ret < 0)
  717. return ret;
  718. return i2c_smbus_write_byte_data(client,
  719. OV08D10_REG_GLOBAL_EFFECTIVE, 0x01);
  720. }
  721. static int ov08d10_test_pattern(struct ov08d10 *ov08d10, u32 pattern)
  722. {
  723. struct i2c_client *client = v4l2_get_subdevdata(&ov08d10->sd);
  724. u8 val;
  725. int ret;
  726. if (pattern)
  727. val = OV08D10_TEST_PATTERN_ENABLE;
  728. else
  729. val = OV08D10_TEST_PATTERN_DISABLE;
  730. /* CIS control registers */
  731. ret = i2c_smbus_write_byte_data(client, OV08D10_REG_PAGE, 0x01);
  732. if (ret < 0)
  733. return ret;
  734. ret = i2c_smbus_write_byte_data(client,
  735. OV08D10_REG_TEST_PATTERN, val);
  736. if (ret < 0)
  737. return ret;
  738. return i2c_smbus_write_byte_data(client,
  739. OV08D10_REG_GLOBAL_EFFECTIVE, 0x01);
  740. }
  741. static int ov08d10_set_ctrl_flip(struct ov08d10 *ov08d10, u32 ctrl_val)
  742. {
  743. struct i2c_client *client = v4l2_get_subdevdata(&ov08d10->sd);
  744. u8 val;
  745. int ret;
  746. /* System control registers */
  747. ret = i2c_smbus_write_byte_data(client, OV08D10_REG_PAGE, 0x01);
  748. if (ret < 0)
  749. return ret;
  750. ret = i2c_smbus_read_byte_data(client, OV08D10_REG_FLIP_OPT);
  751. if (ret < 0)
  752. return ret;
  753. val = ret | (ctrl_val & OV08D10_REG_FLIP_MASK);
  754. ret = i2c_smbus_write_byte_data(client, OV08D10_REG_PAGE, 0x01);
  755. if (ret < 0)
  756. return ret;
  757. ret = i2c_smbus_write_byte_data(client, OV08D10_REG_FLIP_OPT, val);
  758. if (ret < 0)
  759. return ret;
  760. return i2c_smbus_write_byte_data(client,
  761. OV08D10_REG_GLOBAL_EFFECTIVE, 0x01);
  762. }
  763. static int ov08d10_set_ctrl(struct v4l2_ctrl *ctrl)
  764. {
  765. struct ov08d10 *ov08d10 = container_of(ctrl->handler,
  766. struct ov08d10, ctrl_handler);
  767. s64 exposure_max;
  768. int ret;
  769. /* Propagate change of current control to all related controls */
  770. if (ctrl->id == V4L2_CID_VBLANK) {
  771. /* Update max exposure while meeting expected vblanking */
  772. exposure_max = ov08d10->cur_mode->height + ctrl->val -
  773. OV08D10_EXPOSURE_MAX_MARGIN;
  774. __v4l2_ctrl_modify_range(ov08d10->exposure,
  775. ov08d10->exposure->minimum,
  776. exposure_max, ov08d10->exposure->step,
  777. exposure_max);
  778. }
  779. /* V4L2 controls values will be applied only when power is already up */
  780. if (!pm_runtime_get_if_in_use(ov08d10->dev))
  781. return 0;
  782. switch (ctrl->id) {
  783. case V4L2_CID_ANALOGUE_GAIN:
  784. ret = ov08d10_update_analog_gain(ov08d10, ctrl->val);
  785. break;
  786. case V4L2_CID_DIGITAL_GAIN:
  787. ret = ov08d10_update_digital_gain(ov08d10, ctrl->val);
  788. break;
  789. case V4L2_CID_EXPOSURE:
  790. ret = ov08d10_set_exposure(ov08d10, ctrl->val);
  791. break;
  792. case V4L2_CID_VBLANK:
  793. ret = ov08d10_set_vblank(ov08d10, ctrl->val);
  794. break;
  795. case V4L2_CID_TEST_PATTERN:
  796. ret = ov08d10_test_pattern(ov08d10, ctrl->val);
  797. break;
  798. case V4L2_CID_HFLIP:
  799. case V4L2_CID_VFLIP:
  800. ret = ov08d10_set_ctrl_flip(ov08d10,
  801. ov08d10->hflip->val |
  802. ov08d10->vflip->val << 1);
  803. break;
  804. default:
  805. ret = -EINVAL;
  806. break;
  807. }
  808. pm_runtime_put(ov08d10->dev);
  809. return ret;
  810. }
  811. static const struct v4l2_ctrl_ops ov08d10_ctrl_ops = {
  812. .s_ctrl = ov08d10_set_ctrl,
  813. };
  814. static int ov08d10_init_controls(struct ov08d10 *ov08d10)
  815. {
  816. struct v4l2_ctrl_handler *ctrl_hdlr;
  817. u8 link_freq_size;
  818. s64 exposure_max;
  819. s64 vblank_def;
  820. s64 vblank_min;
  821. s64 h_blank;
  822. s64 pixel_rate_max;
  823. const struct ov08d10_mode *mode;
  824. int ret;
  825. ctrl_hdlr = &ov08d10->ctrl_handler;
  826. ret = v4l2_ctrl_handler_init(ctrl_hdlr, 8);
  827. if (ret)
  828. return ret;
  829. ctrl_hdlr->lock = &ov08d10->mutex;
  830. link_freq_size = ARRAY_SIZE(ov08d10->priv_lane->link_freq_menu);
  831. ov08d10->link_freq =
  832. v4l2_ctrl_new_int_menu(ctrl_hdlr, &ov08d10_ctrl_ops,
  833. V4L2_CID_LINK_FREQ,
  834. link_freq_size - 1,
  835. 0,
  836. ov08d10->priv_lane->link_freq_menu);
  837. if (ov08d10->link_freq)
  838. ov08d10->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  839. pixel_rate_max = to_rate(ov08d10->priv_lane->link_freq_menu, 0,
  840. ov08d10->cur_mode->data_lanes);
  841. ov08d10->pixel_rate =
  842. v4l2_ctrl_new_std(ctrl_hdlr, &ov08d10_ctrl_ops,
  843. V4L2_CID_PIXEL_RATE, 0, pixel_rate_max, 1,
  844. pixel_rate_max);
  845. mode = ov08d10->cur_mode;
  846. vblank_def = mode->vts_def - mode->height;
  847. vblank_min = mode->vts_min - mode->height;
  848. ov08d10->vblank =
  849. v4l2_ctrl_new_std(ctrl_hdlr, &ov08d10_ctrl_ops,
  850. V4L2_CID_VBLANK, vblank_min,
  851. OV08D10_VTS_MAX - mode->height, 1,
  852. vblank_def);
  853. h_blank = to_pixels_per_line(ov08d10->priv_lane->link_freq_menu,
  854. mode->hts, mode->link_freq_index,
  855. mode->data_lanes) -
  856. mode->width;
  857. ov08d10->hblank = v4l2_ctrl_new_std(ctrl_hdlr, &ov08d10_ctrl_ops,
  858. V4L2_CID_HBLANK, h_blank, h_blank,
  859. 1, h_blank);
  860. if (ov08d10->hblank)
  861. ov08d10->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  862. v4l2_ctrl_new_std(ctrl_hdlr, &ov08d10_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
  863. OV08D10_ANAL_GAIN_MIN, OV08D10_ANAL_GAIN_MAX,
  864. OV08D10_ANAL_GAIN_STEP, OV08D10_ANAL_GAIN_MIN);
  865. v4l2_ctrl_new_std(ctrl_hdlr, &ov08d10_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
  866. OV08D10_DGTL_GAIN_MIN, OV08D10_DGTL_GAIN_MAX,
  867. OV08D10_DGTL_GAIN_STEP, OV08D10_DGTL_GAIN_DEFAULT);
  868. exposure_max = mode->vts_def - OV08D10_EXPOSURE_MAX_MARGIN;
  869. ov08d10->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &ov08d10_ctrl_ops,
  870. V4L2_CID_EXPOSURE,
  871. OV08D10_EXPOSURE_MIN,
  872. exposure_max,
  873. OV08D10_EXPOSURE_STEP,
  874. exposure_max);
  875. v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &ov08d10_ctrl_ops,
  876. V4L2_CID_TEST_PATTERN,
  877. ARRAY_SIZE(ov08d10_test_pattern_menu) - 1,
  878. 0, 0, ov08d10_test_pattern_menu);
  879. ov08d10->hflip = v4l2_ctrl_new_std(ctrl_hdlr, &ov08d10_ctrl_ops,
  880. V4L2_CID_HFLIP, 0, 1, 1, 0);
  881. if (ov08d10->hflip)
  882. ov08d10->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
  883. ov08d10->vflip = v4l2_ctrl_new_std(ctrl_hdlr, &ov08d10_ctrl_ops,
  884. V4L2_CID_VFLIP, 0, 1, 1, 0);
  885. if (ov08d10->vflip)
  886. ov08d10->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
  887. if (ctrl_hdlr->error)
  888. return ctrl_hdlr->error;
  889. ov08d10->sd.ctrl_handler = ctrl_hdlr;
  890. return 0;
  891. }
  892. static void ov08d10_update_pad_format(struct ov08d10 *ov08d10,
  893. const struct ov08d10_mode *mode,
  894. struct v4l2_mbus_framefmt *fmt)
  895. {
  896. fmt->width = mode->width;
  897. fmt->height = mode->height;
  898. fmt->code = ov08d10_get_format_code(ov08d10);
  899. fmt->field = V4L2_FIELD_NONE;
  900. }
  901. static int ov08d10_start_streaming(struct ov08d10 *ov08d10)
  902. {
  903. struct i2c_client *client = v4l2_get_subdevdata(&ov08d10->sd);
  904. const struct ov08d10_reg_list *reg_list;
  905. int link_freq_index, ret;
  906. link_freq_index = ov08d10->cur_mode->link_freq_index;
  907. reg_list =
  908. &ov08d10->priv_lane->link_freq_configs[link_freq_index].reg_list;
  909. /* soft reset */
  910. ret = i2c_smbus_write_byte_data(client, OV08D10_REG_PAGE, 0x00);
  911. if (ret < 0) {
  912. dev_err(ov08d10->dev, "failed to reset sensor");
  913. return ret;
  914. }
  915. ret = i2c_smbus_write_byte_data(client, 0x20, 0x0e);
  916. if (ret < 0) {
  917. dev_err(ov08d10->dev, "failed to reset sensor");
  918. return ret;
  919. }
  920. usleep_range(3000, 4000);
  921. ret = i2c_smbus_write_byte_data(client, 0x20, 0x0b);
  922. if (ret < 0) {
  923. dev_err(ov08d10->dev, "failed to reset sensor");
  924. return ret;
  925. }
  926. /* update sensor setting */
  927. ret = ov08d10_write_reg_list(ov08d10, reg_list);
  928. if (ret) {
  929. dev_err(ov08d10->dev, "failed to set plls");
  930. return ret;
  931. }
  932. reg_list = &ov08d10->cur_mode->reg_list;
  933. ret = ov08d10_write_reg_list(ov08d10, reg_list);
  934. if (ret) {
  935. dev_err(ov08d10->dev, "failed to set mode");
  936. return ret;
  937. }
  938. ret = __v4l2_ctrl_handler_setup(ov08d10->sd.ctrl_handler);
  939. if (ret)
  940. return ret;
  941. ret = i2c_smbus_write_byte_data(client, OV08D10_REG_PAGE, 0x00);
  942. if (ret < 0)
  943. return ret;
  944. ret = i2c_smbus_write_byte_data(client, OV08D10_REG_MODE_SELECT,
  945. OV08D10_MODE_STREAMING);
  946. if (ret < 0)
  947. return ret;
  948. return i2c_smbus_write_byte_data(client, OV08D10_REG_PAGE, 0x01);
  949. }
  950. static void ov08d10_stop_streaming(struct ov08d10 *ov08d10)
  951. {
  952. struct i2c_client *client = v4l2_get_subdevdata(&ov08d10->sd);
  953. int ret;
  954. ret = i2c_smbus_write_byte_data(client, OV08D10_REG_PAGE, 0x00);
  955. if (ret < 0) {
  956. dev_err(ov08d10->dev, "failed to stop streaming");
  957. return;
  958. }
  959. ret = i2c_smbus_write_byte_data(client, OV08D10_REG_MODE_SELECT,
  960. OV08D10_MODE_STANDBY);
  961. if (ret < 0) {
  962. dev_err(ov08d10->dev, "failed to stop streaming");
  963. return;
  964. }
  965. ret = i2c_smbus_write_byte_data(client, OV08D10_REG_PAGE, 0x01);
  966. if (ret < 0) {
  967. dev_err(ov08d10->dev, "failed to stop streaming");
  968. return;
  969. }
  970. }
  971. static int ov08d10_set_stream(struct v4l2_subdev *sd, int enable)
  972. {
  973. struct ov08d10 *ov08d10 = to_ov08d10(sd);
  974. int ret = 0;
  975. mutex_lock(&ov08d10->mutex);
  976. if (enable) {
  977. ret = pm_runtime_resume_and_get(ov08d10->dev);
  978. if (ret < 0) {
  979. mutex_unlock(&ov08d10->mutex);
  980. return ret;
  981. }
  982. ret = ov08d10_start_streaming(ov08d10);
  983. if (ret) {
  984. enable = 0;
  985. ov08d10_stop_streaming(ov08d10);
  986. pm_runtime_put(ov08d10->dev);
  987. }
  988. } else {
  989. ov08d10_stop_streaming(ov08d10);
  990. pm_runtime_put(ov08d10->dev);
  991. }
  992. /* vflip and hflip cannot change during streaming */
  993. __v4l2_ctrl_grab(ov08d10->vflip, enable);
  994. __v4l2_ctrl_grab(ov08d10->hflip, enable);
  995. mutex_unlock(&ov08d10->mutex);
  996. return ret;
  997. }
  998. static int ov08d10_set_format(struct v4l2_subdev *sd,
  999. struct v4l2_subdev_state *sd_state,
  1000. struct v4l2_subdev_format *fmt)
  1001. {
  1002. struct ov08d10 *ov08d10 = to_ov08d10(sd);
  1003. const struct ov08d10_mode *mode;
  1004. s32 vblank_def, h_blank;
  1005. s64 pixel_rate;
  1006. mode = v4l2_find_nearest_size(ov08d10->priv_lane->sp_modes,
  1007. ov08d10->modes_size,
  1008. width, height, fmt->format.width,
  1009. fmt->format.height);
  1010. mutex_lock(&ov08d10->mutex);
  1011. ov08d10_update_pad_format(ov08d10, mode, &fmt->format);
  1012. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  1013. *v4l2_subdev_state_get_format(sd_state, fmt->pad) =
  1014. fmt->format;
  1015. } else {
  1016. ov08d10->cur_mode = mode;
  1017. __v4l2_ctrl_s_ctrl(ov08d10->link_freq, mode->link_freq_index);
  1018. pixel_rate = to_rate(ov08d10->priv_lane->link_freq_menu,
  1019. mode->link_freq_index,
  1020. ov08d10->cur_mode->data_lanes);
  1021. __v4l2_ctrl_s_ctrl_int64(ov08d10->pixel_rate, pixel_rate);
  1022. /* Update limits and set FPS to default */
  1023. vblank_def = mode->vts_def - mode->height;
  1024. __v4l2_ctrl_modify_range(ov08d10->vblank,
  1025. mode->vts_min - mode->height,
  1026. OV08D10_VTS_MAX - mode->height, 1,
  1027. vblank_def);
  1028. __v4l2_ctrl_s_ctrl(ov08d10->vblank, vblank_def);
  1029. h_blank = to_pixels_per_line(ov08d10->priv_lane->link_freq_menu,
  1030. mode->hts,
  1031. mode->link_freq_index,
  1032. ov08d10->cur_mode->data_lanes)
  1033. - mode->width;
  1034. __v4l2_ctrl_modify_range(ov08d10->hblank, h_blank, h_blank, 1,
  1035. h_blank);
  1036. }
  1037. mutex_unlock(&ov08d10->mutex);
  1038. return 0;
  1039. }
  1040. static int ov08d10_get_format(struct v4l2_subdev *sd,
  1041. struct v4l2_subdev_state *sd_state,
  1042. struct v4l2_subdev_format *fmt)
  1043. {
  1044. struct ov08d10 *ov08d10 = to_ov08d10(sd);
  1045. mutex_lock(&ov08d10->mutex);
  1046. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
  1047. fmt->format = *v4l2_subdev_state_get_format(sd_state,
  1048. fmt->pad);
  1049. else
  1050. ov08d10_update_pad_format(ov08d10, ov08d10->cur_mode,
  1051. &fmt->format);
  1052. mutex_unlock(&ov08d10->mutex);
  1053. return 0;
  1054. }
  1055. static int ov08d10_enum_mbus_code(struct v4l2_subdev *sd,
  1056. struct v4l2_subdev_state *sd_state,
  1057. struct v4l2_subdev_mbus_code_enum *code)
  1058. {
  1059. struct ov08d10 *ov08d10 = to_ov08d10(sd);
  1060. if (code->index > 0)
  1061. return -EINVAL;
  1062. mutex_lock(&ov08d10->mutex);
  1063. code->code = ov08d10_get_format_code(ov08d10);
  1064. mutex_unlock(&ov08d10->mutex);
  1065. return 0;
  1066. }
  1067. static int ov08d10_enum_frame_size(struct v4l2_subdev *sd,
  1068. struct v4l2_subdev_state *sd_state,
  1069. struct v4l2_subdev_frame_size_enum *fse)
  1070. {
  1071. struct ov08d10 *ov08d10 = to_ov08d10(sd);
  1072. if (fse->index >= ov08d10->modes_size)
  1073. return -EINVAL;
  1074. mutex_lock(&ov08d10->mutex);
  1075. if (fse->code != ov08d10_get_format_code(ov08d10)) {
  1076. mutex_unlock(&ov08d10->mutex);
  1077. return -EINVAL;
  1078. }
  1079. mutex_unlock(&ov08d10->mutex);
  1080. fse->min_width = ov08d10->priv_lane->sp_modes[fse->index].width;
  1081. fse->max_width = fse->min_width;
  1082. fse->min_height = ov08d10->priv_lane->sp_modes[fse->index].height;
  1083. fse->max_height = fse->min_height;
  1084. return 0;
  1085. }
  1086. static int ov08d10_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  1087. {
  1088. struct ov08d10 *ov08d10 = to_ov08d10(sd);
  1089. mutex_lock(&ov08d10->mutex);
  1090. ov08d10_update_pad_format(ov08d10, &ov08d10->priv_lane->sp_modes[0],
  1091. v4l2_subdev_state_get_format(fh->state, 0));
  1092. mutex_unlock(&ov08d10->mutex);
  1093. return 0;
  1094. }
  1095. static const struct v4l2_subdev_video_ops ov08d10_video_ops = {
  1096. .s_stream = ov08d10_set_stream,
  1097. };
  1098. static const struct v4l2_subdev_pad_ops ov08d10_pad_ops = {
  1099. .set_fmt = ov08d10_set_format,
  1100. .get_fmt = ov08d10_get_format,
  1101. .enum_mbus_code = ov08d10_enum_mbus_code,
  1102. .enum_frame_size = ov08d10_enum_frame_size,
  1103. };
  1104. static const struct v4l2_subdev_ops ov08d10_subdev_ops = {
  1105. .video = &ov08d10_video_ops,
  1106. .pad = &ov08d10_pad_ops,
  1107. };
  1108. static const struct v4l2_subdev_internal_ops ov08d10_internal_ops = {
  1109. .open = ov08d10_open,
  1110. };
  1111. static int ov08d10_identify_module(struct ov08d10 *ov08d10)
  1112. {
  1113. struct i2c_client *client = v4l2_get_subdevdata(&ov08d10->sd);
  1114. u32 val;
  1115. u16 chip_id;
  1116. int ret;
  1117. /* System control registers */
  1118. ret = i2c_smbus_write_byte_data(client, OV08D10_REG_PAGE, 0x00);
  1119. if (ret < 0)
  1120. return ret;
  1121. /* Validate the chip ID */
  1122. ret = i2c_smbus_read_byte_data(client, OV08D10_REG_CHIP_ID_0);
  1123. if (ret < 0)
  1124. return ret;
  1125. val = ret << 8;
  1126. ret = i2c_smbus_read_byte_data(client, OV08D10_REG_CHIP_ID_1);
  1127. if (ret < 0)
  1128. return ret;
  1129. chip_id = val | ret;
  1130. if ((chip_id & OV08D10_ID_MASK) != OV08D10_CHIP_ID) {
  1131. dev_err(ov08d10->dev, "unexpected sensor id(0x%04x)\n",
  1132. chip_id);
  1133. return -EINVAL;
  1134. }
  1135. return 0;
  1136. }
  1137. static int ov08d10_get_hwcfg(struct ov08d10 *ov08d10)
  1138. {
  1139. struct device *dev = ov08d10->dev;
  1140. struct fwnode_handle *ep;
  1141. struct fwnode_handle *fwnode = dev_fwnode(dev);
  1142. struct v4l2_fwnode_endpoint bus_cfg = {
  1143. .bus_type = V4L2_MBUS_CSI2_DPHY
  1144. };
  1145. unsigned int i, j;
  1146. int ret;
  1147. if (!fwnode)
  1148. return -ENXIO;
  1149. ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
  1150. if (!ep)
  1151. return -ENXIO;
  1152. ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
  1153. fwnode_handle_put(ep);
  1154. if (ret)
  1155. return ret;
  1156. /* Get number of data lanes */
  1157. if (bus_cfg.bus.mipi_csi2.num_data_lanes != 2) {
  1158. dev_err(dev, "number of CSI2 data lanes %d is not supported",
  1159. bus_cfg.bus.mipi_csi2.num_data_lanes);
  1160. ret = -EINVAL;
  1161. goto check_hwcfg_error;
  1162. }
  1163. dev_dbg(dev, "Using %u data lanes\n", ov08d10->cur_mode->data_lanes);
  1164. ov08d10->priv_lane = &lane_cfg_2;
  1165. ov08d10->modes_size = ov08d10_modes_num(ov08d10);
  1166. if (!bus_cfg.nr_of_link_frequencies) {
  1167. dev_err(dev, "no link frequencies defined");
  1168. ret = -EINVAL;
  1169. goto check_hwcfg_error;
  1170. }
  1171. for (i = 0; i < ARRAY_SIZE(ov08d10->priv_lane->link_freq_menu); i++) {
  1172. for (j = 0; j < bus_cfg.nr_of_link_frequencies; j++) {
  1173. if (ov08d10->priv_lane->link_freq_menu[i] ==
  1174. bus_cfg.link_frequencies[j])
  1175. break;
  1176. }
  1177. if (j == bus_cfg.nr_of_link_frequencies) {
  1178. dev_err(dev, "no link frequency %lld supported",
  1179. ov08d10->priv_lane->link_freq_menu[i]);
  1180. ret = -EINVAL;
  1181. goto check_hwcfg_error;
  1182. }
  1183. }
  1184. check_hwcfg_error:
  1185. v4l2_fwnode_endpoint_free(&bus_cfg);
  1186. return ret;
  1187. }
  1188. static void ov08d10_remove(struct i2c_client *client)
  1189. {
  1190. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1191. struct ov08d10 *ov08d10 = to_ov08d10(sd);
  1192. v4l2_async_unregister_subdev(sd);
  1193. media_entity_cleanup(&sd->entity);
  1194. v4l2_ctrl_handler_free(sd->ctrl_handler);
  1195. pm_runtime_disable(ov08d10->dev);
  1196. mutex_destroy(&ov08d10->mutex);
  1197. }
  1198. static int ov08d10_probe(struct i2c_client *client)
  1199. {
  1200. struct ov08d10 *ov08d10;
  1201. unsigned long freq;
  1202. int ret;
  1203. ov08d10 = devm_kzalloc(&client->dev, sizeof(*ov08d10), GFP_KERNEL);
  1204. if (!ov08d10)
  1205. return -ENOMEM;
  1206. ov08d10->dev = &client->dev;
  1207. ov08d10->clk = devm_v4l2_sensor_clk_get(ov08d10->dev, NULL);
  1208. if (IS_ERR(ov08d10->clk))
  1209. return dev_err_probe(ov08d10->dev, PTR_ERR(ov08d10->clk),
  1210. "failed to get clock\n");
  1211. freq = clk_get_rate(ov08d10->clk);
  1212. if (freq != OV08D10_XVCLK_19_2)
  1213. dev_warn(ov08d10->dev,
  1214. "external clock rate %lu is not supported\n", freq);
  1215. ret = ov08d10_get_hwcfg(ov08d10);
  1216. if (ret) {
  1217. dev_err(ov08d10->dev, "failed to get HW configuration: %d",
  1218. ret);
  1219. return ret;
  1220. }
  1221. v4l2_i2c_subdev_init(&ov08d10->sd, client, &ov08d10_subdev_ops);
  1222. ret = ov08d10_identify_module(ov08d10);
  1223. if (ret) {
  1224. dev_err(ov08d10->dev, "failed to find sensor: %d", ret);
  1225. return ret;
  1226. }
  1227. mutex_init(&ov08d10->mutex);
  1228. ov08d10->cur_mode = &ov08d10->priv_lane->sp_modes[0];
  1229. ret = ov08d10_init_controls(ov08d10);
  1230. if (ret) {
  1231. dev_err(ov08d10->dev, "failed to init controls: %d", ret);
  1232. goto probe_error_v4l2_ctrl_handler_free;
  1233. }
  1234. ov08d10->sd.internal_ops = &ov08d10_internal_ops;
  1235. ov08d10->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  1236. ov08d10->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
  1237. ov08d10->pad.flags = MEDIA_PAD_FL_SOURCE;
  1238. ret = media_entity_pads_init(&ov08d10->sd.entity, 1, &ov08d10->pad);
  1239. if (ret) {
  1240. dev_err(ov08d10->dev, "failed to init entity pads: %d", ret);
  1241. goto probe_error_v4l2_ctrl_handler_free;
  1242. }
  1243. ret = v4l2_async_register_subdev_sensor(&ov08d10->sd);
  1244. if (ret < 0) {
  1245. dev_err(ov08d10->dev, "failed to register V4L2 subdev: %d",
  1246. ret);
  1247. goto probe_error_media_entity_cleanup;
  1248. }
  1249. /*
  1250. * Device is already turned on by i2c-core with ACPI domain PM.
  1251. * Enable runtime PM and turn off the device.
  1252. */
  1253. pm_runtime_set_active(ov08d10->dev);
  1254. pm_runtime_enable(ov08d10->dev);
  1255. pm_runtime_idle(ov08d10->dev);
  1256. return 0;
  1257. probe_error_media_entity_cleanup:
  1258. media_entity_cleanup(&ov08d10->sd.entity);
  1259. probe_error_v4l2_ctrl_handler_free:
  1260. v4l2_ctrl_handler_free(ov08d10->sd.ctrl_handler);
  1261. mutex_destroy(&ov08d10->mutex);
  1262. return ret;
  1263. }
  1264. #ifdef CONFIG_ACPI
  1265. static const struct acpi_device_id ov08d10_acpi_ids[] = {
  1266. { "OVTI08D1" },
  1267. { /* sentinel */ }
  1268. };
  1269. MODULE_DEVICE_TABLE(acpi, ov08d10_acpi_ids);
  1270. #endif
  1271. static struct i2c_driver ov08d10_i2c_driver = {
  1272. .driver = {
  1273. .name = "ov08d10",
  1274. .acpi_match_table = ACPI_PTR(ov08d10_acpi_ids),
  1275. },
  1276. .probe = ov08d10_probe,
  1277. .remove = ov08d10_remove,
  1278. };
  1279. module_i2c_driver(ov08d10_i2c_driver);
  1280. MODULE_AUTHOR("Su, Jimmy <jimmy.su@intel.com>");
  1281. MODULE_DESCRIPTION("OmniVision ov08d10 sensor driver");
  1282. MODULE_LICENSE("GPL v2");