ov8856.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2019 Intel Corporation.
  3. #include <asm/unaligned.h>
  4. #include <linux/acpi.h>
  5. #include <linux/clk.h>
  6. #include <linux/delay.h>
  7. #include <linux/gpio/consumer.h>
  8. #include <linux/i2c.h>
  9. #include <linux/module.h>
  10. #include <linux/pm_runtime.h>
  11. #include <linux/regulator/consumer.h>
  12. #include <media/v4l2-ctrls.h>
  13. #include <media/v4l2-device.h>
  14. #include <media/v4l2-fwnode.h>
  15. #define OV8856_REG_VALUE_08BIT 1
  16. #define OV8856_REG_VALUE_16BIT 2
  17. #define OV8856_REG_VALUE_24BIT 3
  18. #define OV8856_LINK_FREQ_360MHZ 360000000ULL
  19. #define OV8856_LINK_FREQ_180MHZ 180000000ULL
  20. #define OV8856_SCLK 144000000ULL
  21. #define OV8856_XVCLK_19_2 19200000
  22. #define OV8856_DATA_LANES 4
  23. #define OV8856_RGB_DEPTH 10
  24. #define OV8856_REG_CHIP_ID 0x300a
  25. #define OV8856_CHIP_ID 0x00885a
  26. #define OV8856_REG_MODE_SELECT 0x0100
  27. #define OV8856_MODE_STANDBY 0x00
  28. #define OV8856_MODE_STREAMING 0x01
  29. /* module revisions */
  30. #define OV8856_2A_MODULE 0x01
  31. #define OV8856_1B_MODULE 0x02
  32. /* the OTP read-out buffer is at 0x7000 and 0xf is the offset
  33. * of the byte in the OTP that means the module revision
  34. */
  35. #define OV8856_MODULE_REVISION 0x700f
  36. #define OV8856_OTP_MODE_CTRL 0x3d84
  37. #define OV8856_OTP_LOAD_CTRL 0x3d81
  38. #define OV8856_OTP_MODE_AUTO 0x00
  39. #define OV8856_OTP_LOAD_CTRL_ENABLE BIT(0)
  40. /* vertical-timings from sensor */
  41. #define OV8856_REG_VTS 0x380e
  42. #define OV8856_VTS_MAX 0x7fff
  43. /* horizontal-timings from sensor */
  44. #define OV8856_REG_HTS 0x380c
  45. /* Exposure controls from sensor */
  46. #define OV8856_REG_EXPOSURE 0x3500
  47. #define OV8856_EXPOSURE_MIN 6
  48. #define OV8856_EXPOSURE_MAX_MARGIN 6
  49. #define OV8856_EXPOSURE_STEP 1
  50. /* Analog gain controls from sensor */
  51. #define OV8856_REG_ANALOG_GAIN 0x3508
  52. #define OV8856_ANAL_GAIN_MIN 128
  53. #define OV8856_ANAL_GAIN_MAX 2047
  54. #define OV8856_ANAL_GAIN_STEP 1
  55. /* Digital gain controls from sensor */
  56. #define OV8856_REG_MWB_R_GAIN 0x5019
  57. #define OV8856_REG_MWB_G_GAIN 0x501b
  58. #define OV8856_REG_MWB_B_GAIN 0x501d
  59. #define OV8856_DGTL_GAIN_MIN 0
  60. #define OV8856_DGTL_GAIN_MAX 4095
  61. #define OV8856_DGTL_GAIN_STEP 1
  62. #define OV8856_DGTL_GAIN_DEFAULT 1024
  63. /* Test Pattern Control */
  64. #define OV8856_REG_TEST_PATTERN 0x5e00
  65. #define OV8856_TEST_PATTERN_ENABLE BIT(7)
  66. #define OV8856_TEST_PATTERN_BAR_SHIFT 2
  67. #define to_ov8856(_sd) container_of(_sd, struct ov8856, sd)
  68. static const char * const ov8856_supply_names[] = {
  69. "dovdd", /* Digital I/O power */
  70. "avdd", /* Analog power */
  71. "dvdd", /* Digital core power */
  72. };
  73. enum {
  74. OV8856_LINK_FREQ_720MBPS,
  75. OV8856_LINK_FREQ_360MBPS,
  76. };
  77. struct ov8856_reg {
  78. u16 address;
  79. u8 val;
  80. };
  81. struct ov8856_reg_list {
  82. u32 num_of_regs;
  83. const struct ov8856_reg *regs;
  84. };
  85. struct ov8856_link_freq_config {
  86. const struct ov8856_reg_list reg_list;
  87. };
  88. struct ov8856_mode {
  89. /* Frame width in pixels */
  90. u32 width;
  91. /* Frame height in pixels */
  92. u32 height;
  93. /* Horizontal timining size */
  94. u32 hts;
  95. /* Default vertical timining size */
  96. u32 vts_def;
  97. /* Min vertical timining size */
  98. u32 vts_min;
  99. /* Link frequency needed for this resolution */
  100. u32 link_freq_index;
  101. /* Sensor register settings for this resolution */
  102. const struct ov8856_reg_list reg_list;
  103. };
  104. static const struct ov8856_reg mipi_data_rate_720mbps[] = {
  105. {0x0103, 0x01},
  106. {0x0100, 0x00},
  107. {0x0302, 0x4b},
  108. {0x0303, 0x01},
  109. {0x030b, 0x02},
  110. {0x030d, 0x4b},
  111. {0x031e, 0x0c},
  112. };
  113. static const struct ov8856_reg mipi_data_rate_360mbps[] = {
  114. {0x0103, 0x01},
  115. {0x0100, 0x00},
  116. {0x0302, 0x4b},
  117. {0x0303, 0x03},
  118. {0x030b, 0x02},
  119. {0x030d, 0x4b},
  120. {0x031e, 0x0c},
  121. };
  122. static const struct ov8856_reg mode_3280x2464_regs[] = {
  123. {0x3000, 0x20},
  124. {0x3003, 0x08},
  125. {0x300e, 0x20},
  126. {0x3010, 0x00},
  127. {0x3015, 0x84},
  128. {0x3018, 0x72},
  129. {0x3021, 0x23},
  130. {0x3033, 0x24},
  131. {0x3500, 0x00},
  132. {0x3501, 0x9a},
  133. {0x3502, 0x20},
  134. {0x3503, 0x08},
  135. {0x3505, 0x83},
  136. {0x3508, 0x01},
  137. {0x3509, 0x80},
  138. {0x350c, 0x00},
  139. {0x350d, 0x80},
  140. {0x350e, 0x04},
  141. {0x350f, 0x00},
  142. {0x3510, 0x00},
  143. {0x3511, 0x02},
  144. {0x3512, 0x00},
  145. {0x3600, 0x72},
  146. {0x3601, 0x40},
  147. {0x3602, 0x30},
  148. {0x3610, 0xc5},
  149. {0x3611, 0x58},
  150. {0x3612, 0x5c},
  151. {0x3613, 0xca},
  152. {0x3614, 0x20},
  153. {0x3628, 0xff},
  154. {0x3629, 0xff},
  155. {0x362a, 0xff},
  156. {0x3633, 0x10},
  157. {0x3634, 0x10},
  158. {0x3635, 0x10},
  159. {0x3636, 0x10},
  160. {0x3663, 0x08},
  161. {0x3669, 0x34},
  162. {0x366e, 0x10},
  163. {0x3706, 0x86},
  164. {0x370b, 0x7e},
  165. {0x3714, 0x23},
  166. {0x3730, 0x12},
  167. {0x3733, 0x10},
  168. {0x3764, 0x00},
  169. {0x3765, 0x00},
  170. {0x3769, 0x62},
  171. {0x376a, 0x2a},
  172. {0x376b, 0x30},
  173. {0x3780, 0x00},
  174. {0x3781, 0x24},
  175. {0x3782, 0x00},
  176. {0x3783, 0x23},
  177. {0x3798, 0x2f},
  178. {0x37a1, 0x60},
  179. {0x37a8, 0x6a},
  180. {0x37ab, 0x3f},
  181. {0x37c2, 0x04},
  182. {0x37c3, 0xf1},
  183. {0x37c9, 0x80},
  184. {0x37cb, 0x16},
  185. {0x37cc, 0x16},
  186. {0x37cd, 0x16},
  187. {0x37ce, 0x16},
  188. {0x3800, 0x00},
  189. {0x3801, 0x00},
  190. {0x3802, 0x00},
  191. {0x3803, 0x06},
  192. {0x3804, 0x0c},
  193. {0x3805, 0xdf},
  194. {0x3806, 0x09},
  195. {0x3807, 0xa7},
  196. {0x3808, 0x0c},
  197. {0x3809, 0xd0},
  198. {0x380a, 0x09},
  199. {0x380b, 0xa0},
  200. {0x380c, 0x07},
  201. {0x380d, 0x88},
  202. {0x380e, 0x09},
  203. {0x380f, 0xb8},
  204. {0x3810, 0x00},
  205. {0x3811, 0x00},
  206. {0x3812, 0x00},
  207. {0x3813, 0x01},
  208. {0x3814, 0x01},
  209. {0x3815, 0x01},
  210. {0x3816, 0x00},
  211. {0x3817, 0x00},
  212. {0x3818, 0x00},
  213. {0x3819, 0x10},
  214. {0x3820, 0x80},
  215. {0x3821, 0x46},
  216. {0x382a, 0x01},
  217. {0x382b, 0x01},
  218. {0x3830, 0x06},
  219. {0x3836, 0x02},
  220. {0x3862, 0x04},
  221. {0x3863, 0x08},
  222. {0x3cc0, 0x33},
  223. {0x3d85, 0x17},
  224. {0x3d8c, 0x73},
  225. {0x3d8d, 0xde},
  226. {0x4001, 0xe0},
  227. {0x4003, 0x40},
  228. {0x4008, 0x00},
  229. {0x4009, 0x0b},
  230. {0x400a, 0x00},
  231. {0x400b, 0x84},
  232. {0x400f, 0x80},
  233. {0x4010, 0xf0},
  234. {0x4011, 0xff},
  235. {0x4012, 0x02},
  236. {0x4013, 0x01},
  237. {0x4014, 0x01},
  238. {0x4015, 0x01},
  239. {0x4042, 0x00},
  240. {0x4043, 0x80},
  241. {0x4044, 0x00},
  242. {0x4045, 0x80},
  243. {0x4046, 0x00},
  244. {0x4047, 0x80},
  245. {0x4048, 0x00},
  246. {0x4049, 0x80},
  247. {0x4041, 0x03},
  248. {0x404c, 0x20},
  249. {0x404d, 0x00},
  250. {0x404e, 0x20},
  251. {0x4203, 0x80},
  252. {0x4307, 0x30},
  253. {0x4317, 0x00},
  254. {0x4503, 0x08},
  255. {0x4601, 0x80},
  256. {0x4800, 0x44},
  257. {0x4816, 0x53},
  258. {0x481b, 0x58},
  259. {0x481f, 0x27},
  260. {0x4837, 0x16},
  261. {0x483c, 0x0f},
  262. {0x484b, 0x05},
  263. {0x5000, 0x57},
  264. {0x5001, 0x0a},
  265. {0x5004, 0x04},
  266. {0x502e, 0x03},
  267. {0x5030, 0x41},
  268. {0x5780, 0x14},
  269. {0x5781, 0x0f},
  270. {0x5782, 0x44},
  271. {0x5783, 0x02},
  272. {0x5784, 0x01},
  273. {0x5785, 0x01},
  274. {0x5786, 0x00},
  275. {0x5787, 0x04},
  276. {0x5788, 0x02},
  277. {0x5789, 0x0f},
  278. {0x578a, 0xfd},
  279. {0x578b, 0xf5},
  280. {0x578c, 0xf5},
  281. {0x578d, 0x03},
  282. {0x578e, 0x08},
  283. {0x578f, 0x0c},
  284. {0x5790, 0x08},
  285. {0x5791, 0x04},
  286. {0x5792, 0x00},
  287. {0x5793, 0x52},
  288. {0x5794, 0xa3},
  289. {0x5795, 0x02},
  290. {0x5796, 0x20},
  291. {0x5797, 0x20},
  292. {0x5798, 0xd5},
  293. {0x5799, 0xd5},
  294. {0x579a, 0x00},
  295. {0x579b, 0x50},
  296. {0x579c, 0x00},
  297. {0x579d, 0x2c},
  298. {0x579e, 0x0c},
  299. {0x579f, 0x40},
  300. {0x57a0, 0x09},
  301. {0x57a1, 0x40},
  302. {0x59f8, 0x3d},
  303. {0x5a08, 0x02},
  304. {0x5b00, 0x02},
  305. {0x5b01, 0x10},
  306. {0x5b02, 0x03},
  307. {0x5b03, 0xcf},
  308. {0x5b05, 0x6c},
  309. {0x5e00, 0x00}
  310. };
  311. static const struct ov8856_reg mode_3264x2448_regs[] = {
  312. {0x0103, 0x01},
  313. {0x0302, 0x3c},
  314. {0x0303, 0x01},
  315. {0x031e, 0x0c},
  316. {0x3000, 0x20},
  317. {0x3003, 0x08},
  318. {0x300e, 0x20},
  319. {0x3010, 0x00},
  320. {0x3015, 0x84},
  321. {0x3018, 0x72},
  322. {0x3021, 0x23},
  323. {0x3033, 0x24},
  324. {0x3500, 0x00},
  325. {0x3501, 0x9a},
  326. {0x3502, 0x20},
  327. {0x3503, 0x08},
  328. {0x3505, 0x83},
  329. {0x3508, 0x01},
  330. {0x3509, 0x80},
  331. {0x350c, 0x00},
  332. {0x350d, 0x80},
  333. {0x350e, 0x04},
  334. {0x350f, 0x00},
  335. {0x3510, 0x00},
  336. {0x3511, 0x02},
  337. {0x3512, 0x00},
  338. {0x3600, 0x72},
  339. {0x3601, 0x40},
  340. {0x3602, 0x30},
  341. {0x3610, 0xc5},
  342. {0x3611, 0x58},
  343. {0x3612, 0x5c},
  344. {0x3613, 0xca},
  345. {0x3614, 0x60},
  346. {0x3628, 0xff},
  347. {0x3629, 0xff},
  348. {0x362a, 0xff},
  349. {0x3633, 0x10},
  350. {0x3634, 0x10},
  351. {0x3635, 0x10},
  352. {0x3636, 0x10},
  353. {0x3663, 0x08},
  354. {0x3669, 0x34},
  355. {0x366d, 0x00},
  356. {0x366e, 0x10},
  357. {0x3706, 0x86},
  358. {0x370b, 0x7e},
  359. {0x3714, 0x23},
  360. {0x3730, 0x12},
  361. {0x3733, 0x10},
  362. {0x3764, 0x00},
  363. {0x3765, 0x00},
  364. {0x3769, 0x62},
  365. {0x376a, 0x2a},
  366. {0x376b, 0x30},
  367. {0x3780, 0x00},
  368. {0x3781, 0x24},
  369. {0x3782, 0x00},
  370. {0x3783, 0x23},
  371. {0x3798, 0x2f},
  372. {0x37a1, 0x60},
  373. {0x37a8, 0x6a},
  374. {0x37ab, 0x3f},
  375. {0x37c2, 0x04},
  376. {0x37c3, 0xf1},
  377. {0x37c9, 0x80},
  378. {0x37cb, 0x16},
  379. {0x37cc, 0x16},
  380. {0x37cd, 0x16},
  381. {0x37ce, 0x16},
  382. {0x3800, 0x00},
  383. {0x3801, 0x00},
  384. {0x3802, 0x00},
  385. {0x3803, 0x0c},
  386. {0x3804, 0x0c},
  387. {0x3805, 0xdf},
  388. {0x3806, 0x09},
  389. {0x3807, 0xa3},
  390. {0x3808, 0x0c},
  391. {0x3809, 0xc0},
  392. {0x380a, 0x09},
  393. {0x380b, 0x90},
  394. {0x380c, 0x07},
  395. {0x380d, 0x8c},
  396. {0x380e, 0x09},
  397. {0x380f, 0xb2},
  398. {0x3810, 0x00},
  399. {0x3811, 0x04},
  400. {0x3812, 0x00},
  401. {0x3813, 0x02},
  402. {0x3814, 0x01},
  403. {0x3815, 0x01},
  404. {0x3816, 0x00},
  405. {0x3817, 0x00},
  406. {0x3818, 0x00},
  407. {0x3819, 0x10},
  408. {0x3820, 0x80},
  409. {0x3821, 0x46},
  410. {0x382a, 0x01},
  411. {0x382b, 0x01},
  412. {0x3830, 0x06},
  413. {0x3836, 0x02},
  414. {0x3862, 0x04},
  415. {0x3863, 0x08},
  416. {0x3cc0, 0x33},
  417. {0x3d85, 0x17},
  418. {0x3d8c, 0x73},
  419. {0x3d8d, 0xde},
  420. {0x4001, 0xe0},
  421. {0x4003, 0x40},
  422. {0x4008, 0x00},
  423. {0x4009, 0x0b},
  424. {0x400a, 0x00},
  425. {0x400b, 0x84},
  426. {0x400f, 0x80},
  427. {0x4010, 0xf0},
  428. {0x4011, 0xff},
  429. {0x4012, 0x02},
  430. {0x4013, 0x01},
  431. {0x4014, 0x01},
  432. {0x4015, 0x01},
  433. {0x4042, 0x00},
  434. {0x4043, 0x80},
  435. {0x4044, 0x00},
  436. {0x4045, 0x80},
  437. {0x4046, 0x00},
  438. {0x4047, 0x80},
  439. {0x4048, 0x00},
  440. {0x4049, 0x80},
  441. {0x4041, 0x03},
  442. {0x404c, 0x20},
  443. {0x404d, 0x00},
  444. {0x404e, 0x20},
  445. {0x4203, 0x80},
  446. {0x4307, 0x30},
  447. {0x4317, 0x00},
  448. {0x4502, 0x50},
  449. {0x4503, 0x08},
  450. {0x4601, 0x80},
  451. {0x4800, 0x44},
  452. {0x4816, 0x53},
  453. {0x481b, 0x50},
  454. {0x481f, 0x27},
  455. {0x4823, 0x3c},
  456. {0x482b, 0x00},
  457. {0x4831, 0x66},
  458. {0x4837, 0x16},
  459. {0x483c, 0x0f},
  460. {0x484b, 0x05},
  461. {0x5000, 0x77},
  462. {0x5001, 0x0a},
  463. {0x5003, 0xc8},
  464. {0x5004, 0x04},
  465. {0x5006, 0x00},
  466. {0x5007, 0x00},
  467. {0x502e, 0x03},
  468. {0x5030, 0x41},
  469. {0x5780, 0x14},
  470. {0x5781, 0x0f},
  471. {0x5782, 0x44},
  472. {0x5783, 0x02},
  473. {0x5784, 0x01},
  474. {0x5785, 0x01},
  475. {0x5786, 0x00},
  476. {0x5787, 0x04},
  477. {0x5788, 0x02},
  478. {0x5789, 0x0f},
  479. {0x578a, 0xfd},
  480. {0x578b, 0xf5},
  481. {0x578c, 0xf5},
  482. {0x578d, 0x03},
  483. {0x578e, 0x08},
  484. {0x578f, 0x0c},
  485. {0x5790, 0x08},
  486. {0x5791, 0x04},
  487. {0x5792, 0x00},
  488. {0x5793, 0x52},
  489. {0x5794, 0xa3},
  490. {0x5795, 0x02},
  491. {0x5796, 0x20},
  492. {0x5797, 0x20},
  493. {0x5798, 0xd5},
  494. {0x5799, 0xd5},
  495. {0x579a, 0x00},
  496. {0x579b, 0x50},
  497. {0x579c, 0x00},
  498. {0x579d, 0x2c},
  499. {0x579e, 0x0c},
  500. {0x579f, 0x40},
  501. {0x57a0, 0x09},
  502. {0x57a1, 0x40},
  503. {0x59f8, 0x3d},
  504. {0x5a08, 0x02},
  505. {0x5b00, 0x02},
  506. {0x5b01, 0x10},
  507. {0x5b02, 0x03},
  508. {0x5b03, 0xcf},
  509. {0x5b05, 0x6c},
  510. {0x5e00, 0x00},
  511. {0x5e10, 0xfc}
  512. };
  513. static const struct ov8856_reg mode_1640x1232_regs[] = {
  514. {0x3000, 0x20},
  515. {0x3003, 0x08},
  516. {0x300e, 0x20},
  517. {0x3010, 0x00},
  518. {0x3015, 0x84},
  519. {0x3018, 0x72},
  520. {0x3021, 0x23},
  521. {0x3033, 0x24},
  522. {0x3500, 0x00},
  523. {0x3501, 0x4c},
  524. {0x3502, 0xe0},
  525. {0x3503, 0x08},
  526. {0x3505, 0x83},
  527. {0x3508, 0x01},
  528. {0x3509, 0x80},
  529. {0x350c, 0x00},
  530. {0x350d, 0x80},
  531. {0x350e, 0x04},
  532. {0x350f, 0x00},
  533. {0x3510, 0x00},
  534. {0x3511, 0x02},
  535. {0x3512, 0x00},
  536. {0x3600, 0x72},
  537. {0x3601, 0x40},
  538. {0x3602, 0x30},
  539. {0x3610, 0xc5},
  540. {0x3611, 0x58},
  541. {0x3612, 0x5c},
  542. {0x3613, 0xca},
  543. {0x3614, 0x20},
  544. {0x3628, 0xff},
  545. {0x3629, 0xff},
  546. {0x362a, 0xff},
  547. {0x3633, 0x10},
  548. {0x3634, 0x10},
  549. {0x3635, 0x10},
  550. {0x3636, 0x10},
  551. {0x3663, 0x08},
  552. {0x3669, 0x34},
  553. {0x366e, 0x08},
  554. {0x3706, 0x86},
  555. {0x370b, 0x7e},
  556. {0x3714, 0x27},
  557. {0x3730, 0x12},
  558. {0x3733, 0x10},
  559. {0x3764, 0x00},
  560. {0x3765, 0x00},
  561. {0x3769, 0x62},
  562. {0x376a, 0x2a},
  563. {0x376b, 0x30},
  564. {0x3780, 0x00},
  565. {0x3781, 0x24},
  566. {0x3782, 0x00},
  567. {0x3783, 0x23},
  568. {0x3798, 0x2f},
  569. {0x37a1, 0x60},
  570. {0x37a8, 0x6a},
  571. {0x37ab, 0x3f},
  572. {0x37c2, 0x14},
  573. {0x37c3, 0xf1},
  574. {0x37c9, 0x80},
  575. {0x37cb, 0x16},
  576. {0x37cc, 0x16},
  577. {0x37cd, 0x16},
  578. {0x37ce, 0x16},
  579. {0x3800, 0x00},
  580. {0x3801, 0x00},
  581. {0x3802, 0x00},
  582. {0x3803, 0x06},
  583. {0x3804, 0x0c},
  584. {0x3805, 0xdf},
  585. {0x3806, 0x09},
  586. {0x3807, 0xa7},
  587. {0x3808, 0x06},
  588. {0x3809, 0x68},
  589. {0x380a, 0x04},
  590. {0x380b, 0xd0},
  591. {0x380c, 0x0e},
  592. {0x380d, 0xec},
  593. {0x380e, 0x04},
  594. {0x380f, 0xe8},
  595. {0x3810, 0x00},
  596. {0x3811, 0x00},
  597. {0x3812, 0x00},
  598. {0x3813, 0x01},
  599. {0x3814, 0x03},
  600. {0x3815, 0x01},
  601. {0x3816, 0x00},
  602. {0x3817, 0x00},
  603. {0x3818, 0x00},
  604. {0x3819, 0x10},
  605. {0x3820, 0x90},
  606. {0x3821, 0x67},
  607. {0x382a, 0x03},
  608. {0x382b, 0x01},
  609. {0x3830, 0x06},
  610. {0x3836, 0x02},
  611. {0x3862, 0x04},
  612. {0x3863, 0x08},
  613. {0x3cc0, 0x33},
  614. {0x3d85, 0x17},
  615. {0x3d8c, 0x73},
  616. {0x3d8d, 0xde},
  617. {0x4001, 0xe0},
  618. {0x4003, 0x40},
  619. {0x4008, 0x00},
  620. {0x4009, 0x05},
  621. {0x400a, 0x00},
  622. {0x400b, 0x84},
  623. {0x400f, 0x80},
  624. {0x4010, 0xf0},
  625. {0x4011, 0xff},
  626. {0x4012, 0x02},
  627. {0x4013, 0x01},
  628. {0x4014, 0x01},
  629. {0x4015, 0x01},
  630. {0x4042, 0x00},
  631. {0x4043, 0x80},
  632. {0x4044, 0x00},
  633. {0x4045, 0x80},
  634. {0x4046, 0x00},
  635. {0x4047, 0x80},
  636. {0x4048, 0x00},
  637. {0x4049, 0x80},
  638. {0x4041, 0x03},
  639. {0x404c, 0x20},
  640. {0x404d, 0x00},
  641. {0x404e, 0x20},
  642. {0x4203, 0x80},
  643. {0x4307, 0x30},
  644. {0x4317, 0x00},
  645. {0x4503, 0x08},
  646. {0x4601, 0x80},
  647. {0x4800, 0x44},
  648. {0x4816, 0x53},
  649. {0x481b, 0x58},
  650. {0x481f, 0x27},
  651. {0x4837, 0x16},
  652. {0x483c, 0x0f},
  653. {0x484b, 0x05},
  654. {0x5000, 0x57},
  655. {0x5001, 0x0a},
  656. {0x5004, 0x04},
  657. {0x502e, 0x03},
  658. {0x5030, 0x41},
  659. {0x5780, 0x14},
  660. {0x5781, 0x0f},
  661. {0x5782, 0x44},
  662. {0x5783, 0x02},
  663. {0x5784, 0x01},
  664. {0x5785, 0x01},
  665. {0x5786, 0x00},
  666. {0x5787, 0x04},
  667. {0x5788, 0x02},
  668. {0x5789, 0x0f},
  669. {0x578a, 0xfd},
  670. {0x578b, 0xf5},
  671. {0x578c, 0xf5},
  672. {0x578d, 0x03},
  673. {0x578e, 0x08},
  674. {0x578f, 0x0c},
  675. {0x5790, 0x08},
  676. {0x5791, 0x04},
  677. {0x5792, 0x00},
  678. {0x5793, 0x52},
  679. {0x5794, 0xa3},
  680. {0x5795, 0x00},
  681. {0x5796, 0x10},
  682. {0x5797, 0x10},
  683. {0x5798, 0x73},
  684. {0x5799, 0x73},
  685. {0x579a, 0x00},
  686. {0x579b, 0x28},
  687. {0x579c, 0x00},
  688. {0x579d, 0x16},
  689. {0x579e, 0x06},
  690. {0x579f, 0x20},
  691. {0x57a0, 0x04},
  692. {0x57a1, 0xa0},
  693. {0x59f8, 0x3d},
  694. {0x5a08, 0x02},
  695. {0x5b00, 0x02},
  696. {0x5b01, 0x10},
  697. {0x5b02, 0x03},
  698. {0x5b03, 0xcf},
  699. {0x5b05, 0x6c},
  700. {0x5e00, 0x00}
  701. };
  702. static const struct ov8856_reg mode_1632x1224_regs[] = {
  703. {0x0103, 0x01},
  704. {0x0302, 0x3c},
  705. {0x0303, 0x01},
  706. {0x031e, 0x0c},
  707. {0x3000, 0x20},
  708. {0x3003, 0x08},
  709. {0x300e, 0x20},
  710. {0x3010, 0x00},
  711. {0x3015, 0x84},
  712. {0x3018, 0x72},
  713. {0x3021, 0x23},
  714. {0x3033, 0x24},
  715. {0x3500, 0x00},
  716. {0x3501, 0x4c},
  717. {0x3502, 0xe0},
  718. {0x3503, 0x08},
  719. {0x3505, 0x83},
  720. {0x3508, 0x01},
  721. {0x3509, 0x80},
  722. {0x350c, 0x00},
  723. {0x350d, 0x80},
  724. {0x350e, 0x04},
  725. {0x350f, 0x00},
  726. {0x3510, 0x00},
  727. {0x3511, 0x02},
  728. {0x3512, 0x00},
  729. {0x3600, 0x72},
  730. {0x3601, 0x40},
  731. {0x3602, 0x30},
  732. {0x3610, 0xc5},
  733. {0x3611, 0x58},
  734. {0x3612, 0x5c},
  735. {0x3613, 0xca},
  736. {0x3614, 0x60},
  737. {0x3628, 0xff},
  738. {0x3629, 0xff},
  739. {0x362a, 0xff},
  740. {0x3633, 0x10},
  741. {0x3634, 0x10},
  742. {0x3635, 0x10},
  743. {0x3636, 0x10},
  744. {0x3663, 0x08},
  745. {0x3669, 0x34},
  746. {0x366d, 0x00},
  747. {0x366e, 0x08},
  748. {0x3706, 0x86},
  749. {0x370b, 0x7e},
  750. {0x3714, 0x27},
  751. {0x3730, 0x12},
  752. {0x3733, 0x10},
  753. {0x3764, 0x00},
  754. {0x3765, 0x00},
  755. {0x3769, 0x62},
  756. {0x376a, 0x2a},
  757. {0x376b, 0x30},
  758. {0x3780, 0x00},
  759. {0x3781, 0x24},
  760. {0x3782, 0x00},
  761. {0x3783, 0x23},
  762. {0x3798, 0x2f},
  763. {0x37a1, 0x60},
  764. {0x37a8, 0x6a},
  765. {0x37ab, 0x3f},
  766. {0x37c2, 0x14},
  767. {0x37c3, 0xf1},
  768. {0x37c9, 0x80},
  769. {0x37cb, 0x16},
  770. {0x37cc, 0x16},
  771. {0x37cd, 0x16},
  772. {0x37ce, 0x16},
  773. {0x3800, 0x00},
  774. {0x3801, 0x00},
  775. {0x3802, 0x00},
  776. {0x3803, 0x0c},
  777. {0x3804, 0x0c},
  778. {0x3805, 0xdf},
  779. {0x3806, 0x09},
  780. {0x3807, 0xa3},
  781. {0x3808, 0x06},
  782. {0x3809, 0x60},
  783. {0x380a, 0x04},
  784. {0x380b, 0xc8},
  785. {0x380c, 0x07},
  786. {0x380d, 0x8c},
  787. {0x380e, 0x09},
  788. {0x380f, 0xb2},
  789. {0x3810, 0x00},
  790. {0x3811, 0x02},
  791. {0x3812, 0x00},
  792. {0x3813, 0x02},
  793. {0x3814, 0x03},
  794. {0x3815, 0x01},
  795. {0x3816, 0x00},
  796. {0x3817, 0x00},
  797. {0x3818, 0x00},
  798. {0x3819, 0x10},
  799. {0x3820, 0x80},
  800. {0x3821, 0x47},
  801. {0x382a, 0x03},
  802. {0x382b, 0x01},
  803. {0x3830, 0x06},
  804. {0x3836, 0x02},
  805. {0x3862, 0x04},
  806. {0x3863, 0x08},
  807. {0x3cc0, 0x33},
  808. {0x3d85, 0x17},
  809. {0x3d8c, 0x73},
  810. {0x3d8d, 0xde},
  811. {0x4001, 0xe0},
  812. {0x4003, 0x40},
  813. {0x4008, 0x00},
  814. {0x4009, 0x05},
  815. {0x400a, 0x00},
  816. {0x400b, 0x84},
  817. {0x400f, 0x80},
  818. {0x4010, 0xf0},
  819. {0x4011, 0xff},
  820. {0x4012, 0x02},
  821. {0x4013, 0x01},
  822. {0x4014, 0x01},
  823. {0x4015, 0x01},
  824. {0x4042, 0x00},
  825. {0x4043, 0x80},
  826. {0x4044, 0x00},
  827. {0x4045, 0x80},
  828. {0x4046, 0x00},
  829. {0x4047, 0x80},
  830. {0x4048, 0x00},
  831. {0x4049, 0x80},
  832. {0x4041, 0x03},
  833. {0x404c, 0x20},
  834. {0x404d, 0x00},
  835. {0x404e, 0x20},
  836. {0x4203, 0x80},
  837. {0x4307, 0x30},
  838. {0x4317, 0x00},
  839. {0x4502, 0x50},
  840. {0x4503, 0x08},
  841. {0x4601, 0x80},
  842. {0x4800, 0x44},
  843. {0x4816, 0x53},
  844. {0x481b, 0x50},
  845. {0x481f, 0x27},
  846. {0x4823, 0x3c},
  847. {0x482b, 0x00},
  848. {0x4831, 0x66},
  849. {0x4837, 0x16},
  850. {0x483c, 0x0f},
  851. {0x484b, 0x05},
  852. {0x5000, 0x77},
  853. {0x5001, 0x0a},
  854. {0x5003, 0xc8},
  855. {0x5004, 0x04},
  856. {0x5006, 0x00},
  857. {0x5007, 0x00},
  858. {0x502e, 0x03},
  859. {0x5030, 0x41},
  860. {0x5795, 0x00},
  861. {0x5796, 0x10},
  862. {0x5797, 0x10},
  863. {0x5798, 0x73},
  864. {0x5799, 0x73},
  865. {0x579a, 0x00},
  866. {0x579b, 0x28},
  867. {0x579c, 0x00},
  868. {0x579d, 0x16},
  869. {0x579e, 0x06},
  870. {0x579f, 0x20},
  871. {0x57a0, 0x04},
  872. {0x57a1, 0xa0},
  873. {0x5780, 0x14},
  874. {0x5781, 0x0f},
  875. {0x5782, 0x44},
  876. {0x5783, 0x02},
  877. {0x5784, 0x01},
  878. {0x5785, 0x01},
  879. {0x5786, 0x00},
  880. {0x5787, 0x04},
  881. {0x5788, 0x02},
  882. {0x5789, 0x0f},
  883. {0x578a, 0xfd},
  884. {0x578b, 0xf5},
  885. {0x578c, 0xf5},
  886. {0x578d, 0x03},
  887. {0x578e, 0x08},
  888. {0x578f, 0x0c},
  889. {0x5790, 0x08},
  890. {0x5791, 0x04},
  891. {0x5792, 0x00},
  892. {0x5793, 0x52},
  893. {0x5794, 0xa3},
  894. {0x59f8, 0x3d},
  895. {0x5a08, 0x02},
  896. {0x5b00, 0x02},
  897. {0x5b01, 0x10},
  898. {0x5b02, 0x03},
  899. {0x5b03, 0xcf},
  900. {0x5b05, 0x6c},
  901. {0x5e00, 0x00},
  902. {0x5e10, 0xfc}
  903. };
  904. static const char * const ov8856_test_pattern_menu[] = {
  905. "Disabled",
  906. "Standard Color Bar",
  907. "Top-Bottom Darker Color Bar",
  908. "Right-Left Darker Color Bar",
  909. "Bottom-Top Darker Color Bar"
  910. };
  911. static const s64 link_freq_menu_items[] = {
  912. OV8856_LINK_FREQ_360MHZ,
  913. OV8856_LINK_FREQ_180MHZ
  914. };
  915. static const struct ov8856_link_freq_config link_freq_configs[] = {
  916. [OV8856_LINK_FREQ_720MBPS] = {
  917. .reg_list = {
  918. .num_of_regs = ARRAY_SIZE(mipi_data_rate_720mbps),
  919. .regs = mipi_data_rate_720mbps,
  920. }
  921. },
  922. [OV8856_LINK_FREQ_360MBPS] = {
  923. .reg_list = {
  924. .num_of_regs = ARRAY_SIZE(mipi_data_rate_360mbps),
  925. .regs = mipi_data_rate_360mbps,
  926. }
  927. }
  928. };
  929. static const struct ov8856_mode supported_modes[] = {
  930. {
  931. .width = 3280,
  932. .height = 2464,
  933. .hts = 1928,
  934. .vts_def = 2488,
  935. .vts_min = 2488,
  936. .reg_list = {
  937. .num_of_regs = ARRAY_SIZE(mode_3280x2464_regs),
  938. .regs = mode_3280x2464_regs,
  939. },
  940. .link_freq_index = OV8856_LINK_FREQ_720MBPS,
  941. },
  942. {
  943. .width = 3264,
  944. .height = 2448,
  945. .hts = 1932,
  946. .vts_def = 2482,
  947. .vts_min = 2482,
  948. .reg_list = {
  949. .num_of_regs = ARRAY_SIZE(mode_3264x2448_regs),
  950. .regs = mode_3264x2448_regs,
  951. },
  952. .link_freq_index = OV8856_LINK_FREQ_720MBPS,
  953. },
  954. {
  955. .width = 1640,
  956. .height = 1232,
  957. .hts = 3820,
  958. .vts_def = 1256,
  959. .vts_min = 1256,
  960. .reg_list = {
  961. .num_of_regs = ARRAY_SIZE(mode_1640x1232_regs),
  962. .regs = mode_1640x1232_regs,
  963. },
  964. .link_freq_index = OV8856_LINK_FREQ_360MBPS,
  965. },
  966. {
  967. .width = 1632,
  968. .height = 1224,
  969. .hts = 1932,
  970. .vts_def = 2482,
  971. .vts_min = 2482,
  972. .reg_list = {
  973. .num_of_regs = ARRAY_SIZE(mode_1632x1224_regs),
  974. .regs = mode_1632x1224_regs,
  975. },
  976. .link_freq_index = OV8856_LINK_FREQ_360MBPS,
  977. }
  978. };
  979. struct ov8856 {
  980. struct v4l2_subdev sd;
  981. struct media_pad pad;
  982. struct v4l2_ctrl_handler ctrl_handler;
  983. struct clk *xvclk;
  984. struct gpio_desc *reset_gpio;
  985. struct regulator_bulk_data supplies[ARRAY_SIZE(ov8856_supply_names)];
  986. /* V4L2 Controls */
  987. struct v4l2_ctrl *link_freq;
  988. struct v4l2_ctrl *pixel_rate;
  989. struct v4l2_ctrl *vblank;
  990. struct v4l2_ctrl *hblank;
  991. struct v4l2_ctrl *exposure;
  992. /* Current mode */
  993. const struct ov8856_mode *cur_mode;
  994. /* To serialize asynchronus callbacks */
  995. struct mutex mutex;
  996. /* Streaming on/off */
  997. bool streaming;
  998. };
  999. static u64 to_pixel_rate(u32 f_index)
  1000. {
  1001. u64 pixel_rate = link_freq_menu_items[f_index] * 2 * OV8856_DATA_LANES;
  1002. do_div(pixel_rate, OV8856_RGB_DEPTH);
  1003. return pixel_rate;
  1004. }
  1005. static u64 to_pixels_per_line(u32 hts, u32 f_index)
  1006. {
  1007. u64 ppl = hts * to_pixel_rate(f_index);
  1008. do_div(ppl, OV8856_SCLK);
  1009. return ppl;
  1010. }
  1011. static int ov8856_read_reg(struct ov8856 *ov8856, u16 reg, u16 len, u32 *val)
  1012. {
  1013. struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd);
  1014. struct i2c_msg msgs[2];
  1015. u8 addr_buf[2];
  1016. u8 data_buf[4] = {0};
  1017. int ret;
  1018. if (len > 4)
  1019. return -EINVAL;
  1020. put_unaligned_be16(reg, addr_buf);
  1021. msgs[0].addr = client->addr;
  1022. msgs[0].flags = 0;
  1023. msgs[0].len = sizeof(addr_buf);
  1024. msgs[0].buf = addr_buf;
  1025. msgs[1].addr = client->addr;
  1026. msgs[1].flags = I2C_M_RD;
  1027. msgs[1].len = len;
  1028. msgs[1].buf = &data_buf[4 - len];
  1029. ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  1030. if (ret != ARRAY_SIZE(msgs))
  1031. return -EIO;
  1032. *val = get_unaligned_be32(data_buf);
  1033. return 0;
  1034. }
  1035. static int ov8856_write_reg(struct ov8856 *ov8856, u16 reg, u16 len, u32 val)
  1036. {
  1037. struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd);
  1038. u8 buf[6];
  1039. if (len > 4)
  1040. return -EINVAL;
  1041. put_unaligned_be16(reg, buf);
  1042. put_unaligned_be32(val << 8 * (4 - len), buf + 2);
  1043. if (i2c_master_send(client, buf, len + 2) != len + 2)
  1044. return -EIO;
  1045. return 0;
  1046. }
  1047. static int ov8856_write_reg_list(struct ov8856 *ov8856,
  1048. const struct ov8856_reg_list *r_list)
  1049. {
  1050. struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd);
  1051. unsigned int i;
  1052. int ret;
  1053. for (i = 0; i < r_list->num_of_regs; i++) {
  1054. ret = ov8856_write_reg(ov8856, r_list->regs[i].address, 1,
  1055. r_list->regs[i].val);
  1056. if (ret) {
  1057. dev_err_ratelimited(&client->dev,
  1058. "failed to write reg 0x%4.4x. error = %d",
  1059. r_list->regs[i].address, ret);
  1060. return ret;
  1061. }
  1062. }
  1063. return 0;
  1064. }
  1065. static int ov8856_update_digital_gain(struct ov8856 *ov8856, u32 d_gain)
  1066. {
  1067. int ret;
  1068. ret = ov8856_write_reg(ov8856, OV8856_REG_MWB_R_GAIN,
  1069. OV8856_REG_VALUE_16BIT, d_gain);
  1070. if (ret)
  1071. return ret;
  1072. ret = ov8856_write_reg(ov8856, OV8856_REG_MWB_G_GAIN,
  1073. OV8856_REG_VALUE_16BIT, d_gain);
  1074. if (ret)
  1075. return ret;
  1076. return ov8856_write_reg(ov8856, OV8856_REG_MWB_B_GAIN,
  1077. OV8856_REG_VALUE_16BIT, d_gain);
  1078. }
  1079. static int ov8856_test_pattern(struct ov8856 *ov8856, u32 pattern)
  1080. {
  1081. if (pattern)
  1082. pattern = (pattern - 1) << OV8856_TEST_PATTERN_BAR_SHIFT |
  1083. OV8856_TEST_PATTERN_ENABLE;
  1084. return ov8856_write_reg(ov8856, OV8856_REG_TEST_PATTERN,
  1085. OV8856_REG_VALUE_08BIT, pattern);
  1086. }
  1087. static int ov8856_set_ctrl(struct v4l2_ctrl *ctrl)
  1088. {
  1089. struct ov8856 *ov8856 = container_of(ctrl->handler,
  1090. struct ov8856, ctrl_handler);
  1091. struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd);
  1092. s64 exposure_max;
  1093. int ret = 0;
  1094. /* Propagate change of current control to all related controls */
  1095. if (ctrl->id == V4L2_CID_VBLANK) {
  1096. /* Update max exposure while meeting expected vblanking */
  1097. exposure_max = ov8856->cur_mode->height + ctrl->val -
  1098. OV8856_EXPOSURE_MAX_MARGIN;
  1099. __v4l2_ctrl_modify_range(ov8856->exposure,
  1100. ov8856->exposure->minimum,
  1101. exposure_max, ov8856->exposure->step,
  1102. exposure_max);
  1103. }
  1104. /* V4L2 controls values will be applied only when power is already up */
  1105. if (!pm_runtime_get_if_in_use(&client->dev))
  1106. return 0;
  1107. switch (ctrl->id) {
  1108. case V4L2_CID_ANALOGUE_GAIN:
  1109. ret = ov8856_write_reg(ov8856, OV8856_REG_ANALOG_GAIN,
  1110. OV8856_REG_VALUE_16BIT, ctrl->val);
  1111. break;
  1112. case V4L2_CID_DIGITAL_GAIN:
  1113. ret = ov8856_update_digital_gain(ov8856, ctrl->val);
  1114. break;
  1115. case V4L2_CID_EXPOSURE:
  1116. /* 4 least significant bits of expsoure are fractional part */
  1117. ret = ov8856_write_reg(ov8856, OV8856_REG_EXPOSURE,
  1118. OV8856_REG_VALUE_24BIT, ctrl->val << 4);
  1119. break;
  1120. case V4L2_CID_VBLANK:
  1121. ret = ov8856_write_reg(ov8856, OV8856_REG_VTS,
  1122. OV8856_REG_VALUE_16BIT,
  1123. ov8856->cur_mode->height + ctrl->val);
  1124. break;
  1125. case V4L2_CID_TEST_PATTERN:
  1126. ret = ov8856_test_pattern(ov8856, ctrl->val);
  1127. break;
  1128. default:
  1129. ret = -EINVAL;
  1130. break;
  1131. }
  1132. pm_runtime_put(&client->dev);
  1133. return ret;
  1134. }
  1135. static const struct v4l2_ctrl_ops ov8856_ctrl_ops = {
  1136. .s_ctrl = ov8856_set_ctrl,
  1137. };
  1138. static int ov8856_init_controls(struct ov8856 *ov8856)
  1139. {
  1140. struct v4l2_ctrl_handler *ctrl_hdlr;
  1141. s64 exposure_max, h_blank;
  1142. int ret;
  1143. ctrl_hdlr = &ov8856->ctrl_handler;
  1144. ret = v4l2_ctrl_handler_init(ctrl_hdlr, 8);
  1145. if (ret)
  1146. return ret;
  1147. ctrl_hdlr->lock = &ov8856->mutex;
  1148. ov8856->link_freq = v4l2_ctrl_new_int_menu(ctrl_hdlr, &ov8856_ctrl_ops,
  1149. V4L2_CID_LINK_FREQ,
  1150. ARRAY_SIZE(link_freq_menu_items) - 1,
  1151. 0, link_freq_menu_items);
  1152. if (ov8856->link_freq)
  1153. ov8856->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  1154. ov8856->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &ov8856_ctrl_ops,
  1155. V4L2_CID_PIXEL_RATE, 0,
  1156. to_pixel_rate(OV8856_LINK_FREQ_720MBPS),
  1157. 1,
  1158. to_pixel_rate(OV8856_LINK_FREQ_720MBPS));
  1159. ov8856->vblank = v4l2_ctrl_new_std(ctrl_hdlr, &ov8856_ctrl_ops,
  1160. V4L2_CID_VBLANK,
  1161. ov8856->cur_mode->vts_min - ov8856->cur_mode->height,
  1162. OV8856_VTS_MAX - ov8856->cur_mode->height, 1,
  1163. ov8856->cur_mode->vts_def - ov8856->cur_mode->height);
  1164. h_blank = to_pixels_per_line(ov8856->cur_mode->hts,
  1165. ov8856->cur_mode->link_freq_index) - ov8856->cur_mode->width;
  1166. ov8856->hblank = v4l2_ctrl_new_std(ctrl_hdlr, &ov8856_ctrl_ops,
  1167. V4L2_CID_HBLANK, h_blank, h_blank, 1,
  1168. h_blank);
  1169. if (ov8856->hblank)
  1170. ov8856->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  1171. v4l2_ctrl_new_std(ctrl_hdlr, &ov8856_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
  1172. OV8856_ANAL_GAIN_MIN, OV8856_ANAL_GAIN_MAX,
  1173. OV8856_ANAL_GAIN_STEP, OV8856_ANAL_GAIN_MIN);
  1174. v4l2_ctrl_new_std(ctrl_hdlr, &ov8856_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
  1175. OV8856_DGTL_GAIN_MIN, OV8856_DGTL_GAIN_MAX,
  1176. OV8856_DGTL_GAIN_STEP, OV8856_DGTL_GAIN_DEFAULT);
  1177. exposure_max = ov8856->cur_mode->vts_def - OV8856_EXPOSURE_MAX_MARGIN;
  1178. ov8856->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &ov8856_ctrl_ops,
  1179. V4L2_CID_EXPOSURE,
  1180. OV8856_EXPOSURE_MIN, exposure_max,
  1181. OV8856_EXPOSURE_STEP,
  1182. exposure_max);
  1183. v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &ov8856_ctrl_ops,
  1184. V4L2_CID_TEST_PATTERN,
  1185. ARRAY_SIZE(ov8856_test_pattern_menu) - 1,
  1186. 0, 0, ov8856_test_pattern_menu);
  1187. if (ctrl_hdlr->error)
  1188. return ctrl_hdlr->error;
  1189. ov8856->sd.ctrl_handler = ctrl_hdlr;
  1190. return 0;
  1191. }
  1192. static void ov8856_update_pad_format(const struct ov8856_mode *mode,
  1193. struct v4l2_mbus_framefmt *fmt)
  1194. {
  1195. fmt->width = mode->width;
  1196. fmt->height = mode->height;
  1197. fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
  1198. fmt->field = V4L2_FIELD_NONE;
  1199. }
  1200. static int ov8856_start_streaming(struct ov8856 *ov8856)
  1201. {
  1202. struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd);
  1203. const struct ov8856_reg_list *reg_list;
  1204. int link_freq_index, ret;
  1205. link_freq_index = ov8856->cur_mode->link_freq_index;
  1206. reg_list = &link_freq_configs[link_freq_index].reg_list;
  1207. ret = ov8856_write_reg_list(ov8856, reg_list);
  1208. if (ret) {
  1209. dev_err(&client->dev, "failed to set plls");
  1210. return ret;
  1211. }
  1212. reg_list = &ov8856->cur_mode->reg_list;
  1213. ret = ov8856_write_reg_list(ov8856, reg_list);
  1214. if (ret) {
  1215. dev_err(&client->dev, "failed to set mode");
  1216. return ret;
  1217. }
  1218. ret = __v4l2_ctrl_handler_setup(ov8856->sd.ctrl_handler);
  1219. if (ret)
  1220. return ret;
  1221. ret = ov8856_write_reg(ov8856, OV8856_REG_MODE_SELECT,
  1222. OV8856_REG_VALUE_08BIT, OV8856_MODE_STREAMING);
  1223. if (ret) {
  1224. dev_err(&client->dev, "failed to set stream");
  1225. return ret;
  1226. }
  1227. return 0;
  1228. }
  1229. static void ov8856_stop_streaming(struct ov8856 *ov8856)
  1230. {
  1231. struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd);
  1232. if (ov8856_write_reg(ov8856, OV8856_REG_MODE_SELECT,
  1233. OV8856_REG_VALUE_08BIT, OV8856_MODE_STANDBY))
  1234. dev_err(&client->dev, "failed to set stream");
  1235. }
  1236. static int ov8856_set_stream(struct v4l2_subdev *sd, int enable)
  1237. {
  1238. struct ov8856 *ov8856 = to_ov8856(sd);
  1239. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1240. int ret = 0;
  1241. if (ov8856->streaming == enable)
  1242. return 0;
  1243. mutex_lock(&ov8856->mutex);
  1244. if (enable) {
  1245. ret = pm_runtime_get_sync(&client->dev);
  1246. if (ret < 0) {
  1247. pm_runtime_put_noidle(&client->dev);
  1248. mutex_unlock(&ov8856->mutex);
  1249. return ret;
  1250. }
  1251. ret = ov8856_start_streaming(ov8856);
  1252. if (ret) {
  1253. enable = 0;
  1254. ov8856_stop_streaming(ov8856);
  1255. pm_runtime_put(&client->dev);
  1256. }
  1257. } else {
  1258. ov8856_stop_streaming(ov8856);
  1259. pm_runtime_put(&client->dev);
  1260. }
  1261. ov8856->streaming = enable;
  1262. mutex_unlock(&ov8856->mutex);
  1263. return ret;
  1264. }
  1265. static int __ov8856_power_on(struct ov8856 *ov8856)
  1266. {
  1267. struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd);
  1268. int ret;
  1269. if (is_acpi_node(dev_fwnode(&client->dev)))
  1270. return 0;
  1271. ret = clk_prepare_enable(ov8856->xvclk);
  1272. if (ret < 0) {
  1273. dev_err(&client->dev, "failed to enable xvclk\n");
  1274. return ret;
  1275. }
  1276. if (ov8856->reset_gpio) {
  1277. gpiod_set_value_cansleep(ov8856->reset_gpio, 1);
  1278. usleep_range(1000, 2000);
  1279. }
  1280. ret = regulator_bulk_enable(ARRAY_SIZE(ov8856_supply_names),
  1281. ov8856->supplies);
  1282. if (ret < 0) {
  1283. dev_err(&client->dev, "failed to enable regulators\n");
  1284. goto disable_clk;
  1285. }
  1286. gpiod_set_value_cansleep(ov8856->reset_gpio, 0);
  1287. usleep_range(1500, 1800);
  1288. return 0;
  1289. disable_clk:
  1290. gpiod_set_value_cansleep(ov8856->reset_gpio, 1);
  1291. clk_disable_unprepare(ov8856->xvclk);
  1292. return ret;
  1293. }
  1294. static void __ov8856_power_off(struct ov8856 *ov8856)
  1295. {
  1296. struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd);
  1297. if (is_acpi_node(dev_fwnode(&client->dev)))
  1298. return;
  1299. gpiod_set_value_cansleep(ov8856->reset_gpio, 1);
  1300. regulator_bulk_disable(ARRAY_SIZE(ov8856_supply_names),
  1301. ov8856->supplies);
  1302. clk_disable_unprepare(ov8856->xvclk);
  1303. }
  1304. static int __maybe_unused ov8856_suspend(struct device *dev)
  1305. {
  1306. struct i2c_client *client = to_i2c_client(dev);
  1307. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1308. struct ov8856 *ov8856 = to_ov8856(sd);
  1309. mutex_lock(&ov8856->mutex);
  1310. if (ov8856->streaming)
  1311. ov8856_stop_streaming(ov8856);
  1312. __ov8856_power_off(ov8856);
  1313. mutex_unlock(&ov8856->mutex);
  1314. return 0;
  1315. }
  1316. static int __maybe_unused ov8856_resume(struct device *dev)
  1317. {
  1318. struct i2c_client *client = to_i2c_client(dev);
  1319. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1320. struct ov8856 *ov8856 = to_ov8856(sd);
  1321. int ret;
  1322. mutex_lock(&ov8856->mutex);
  1323. __ov8856_power_on(ov8856);
  1324. if (ov8856->streaming) {
  1325. ret = ov8856_start_streaming(ov8856);
  1326. if (ret) {
  1327. ov8856->streaming = false;
  1328. ov8856_stop_streaming(ov8856);
  1329. mutex_unlock(&ov8856->mutex);
  1330. return ret;
  1331. }
  1332. }
  1333. mutex_unlock(&ov8856->mutex);
  1334. return 0;
  1335. }
  1336. static int ov8856_set_format(struct v4l2_subdev *sd,
  1337. struct v4l2_subdev_pad_config *cfg,
  1338. struct v4l2_subdev_format *fmt)
  1339. {
  1340. struct ov8856 *ov8856 = to_ov8856(sd);
  1341. const struct ov8856_mode *mode;
  1342. s32 vblank_def, h_blank;
  1343. mode = v4l2_find_nearest_size(supported_modes,
  1344. ARRAY_SIZE(supported_modes), width,
  1345. height, fmt->format.width,
  1346. fmt->format.height);
  1347. mutex_lock(&ov8856->mutex);
  1348. ov8856_update_pad_format(mode, &fmt->format);
  1349. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  1350. *v4l2_subdev_get_try_format(sd, cfg, fmt->pad) = fmt->format;
  1351. } else {
  1352. ov8856->cur_mode = mode;
  1353. __v4l2_ctrl_s_ctrl(ov8856->link_freq, mode->link_freq_index);
  1354. __v4l2_ctrl_s_ctrl_int64(ov8856->pixel_rate,
  1355. to_pixel_rate(mode->link_freq_index));
  1356. /* Update limits and set FPS to default */
  1357. vblank_def = mode->vts_def - mode->height;
  1358. __v4l2_ctrl_modify_range(ov8856->vblank,
  1359. mode->vts_min - mode->height,
  1360. OV8856_VTS_MAX - mode->height, 1,
  1361. vblank_def);
  1362. __v4l2_ctrl_s_ctrl(ov8856->vblank, vblank_def);
  1363. h_blank = to_pixels_per_line(mode->hts, mode->link_freq_index) -
  1364. mode->width;
  1365. __v4l2_ctrl_modify_range(ov8856->hblank, h_blank, h_blank, 1,
  1366. h_blank);
  1367. }
  1368. mutex_unlock(&ov8856->mutex);
  1369. return 0;
  1370. }
  1371. static int ov8856_get_format(struct v4l2_subdev *sd,
  1372. struct v4l2_subdev_pad_config *cfg,
  1373. struct v4l2_subdev_format *fmt)
  1374. {
  1375. struct ov8856 *ov8856 = to_ov8856(sd);
  1376. mutex_lock(&ov8856->mutex);
  1377. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
  1378. fmt->format = *v4l2_subdev_get_try_format(&ov8856->sd, cfg,
  1379. fmt->pad);
  1380. else
  1381. ov8856_update_pad_format(ov8856->cur_mode, &fmt->format);
  1382. mutex_unlock(&ov8856->mutex);
  1383. return 0;
  1384. }
  1385. static int ov8856_enum_mbus_code(struct v4l2_subdev *sd,
  1386. struct v4l2_subdev_pad_config *cfg,
  1387. struct v4l2_subdev_mbus_code_enum *code)
  1388. {
  1389. /* Only one bayer order GRBG is supported */
  1390. if (code->index > 0)
  1391. return -EINVAL;
  1392. code->code = MEDIA_BUS_FMT_SGRBG10_1X10;
  1393. return 0;
  1394. }
  1395. static int ov8856_enum_frame_size(struct v4l2_subdev *sd,
  1396. struct v4l2_subdev_pad_config *cfg,
  1397. struct v4l2_subdev_frame_size_enum *fse)
  1398. {
  1399. if (fse->index >= ARRAY_SIZE(supported_modes))
  1400. return -EINVAL;
  1401. if (fse->code != MEDIA_BUS_FMT_SGRBG10_1X10)
  1402. return -EINVAL;
  1403. fse->min_width = supported_modes[fse->index].width;
  1404. fse->max_width = fse->min_width;
  1405. fse->min_height = supported_modes[fse->index].height;
  1406. fse->max_height = fse->min_height;
  1407. return 0;
  1408. }
  1409. static int ov8856_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  1410. {
  1411. struct ov8856 *ov8856 = to_ov8856(sd);
  1412. mutex_lock(&ov8856->mutex);
  1413. ov8856_update_pad_format(&supported_modes[0],
  1414. v4l2_subdev_get_try_format(sd, fh->pad, 0));
  1415. mutex_unlock(&ov8856->mutex);
  1416. return 0;
  1417. }
  1418. static const struct v4l2_subdev_video_ops ov8856_video_ops = {
  1419. .s_stream = ov8856_set_stream,
  1420. };
  1421. static const struct v4l2_subdev_pad_ops ov8856_pad_ops = {
  1422. .set_fmt = ov8856_set_format,
  1423. .get_fmt = ov8856_get_format,
  1424. .enum_mbus_code = ov8856_enum_mbus_code,
  1425. .enum_frame_size = ov8856_enum_frame_size,
  1426. };
  1427. static const struct v4l2_subdev_ops ov8856_subdev_ops = {
  1428. .video = &ov8856_video_ops,
  1429. .pad = &ov8856_pad_ops,
  1430. };
  1431. static const struct media_entity_operations ov8856_subdev_entity_ops = {
  1432. .link_validate = v4l2_subdev_link_validate,
  1433. };
  1434. static const struct v4l2_subdev_internal_ops ov8856_internal_ops = {
  1435. .open = ov8856_open,
  1436. };
  1437. static int ov8856_identify_module(struct ov8856 *ov8856)
  1438. {
  1439. struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd);
  1440. int ret;
  1441. u32 val;
  1442. ret = ov8856_read_reg(ov8856, OV8856_REG_CHIP_ID,
  1443. OV8856_REG_VALUE_24BIT, &val);
  1444. if (ret)
  1445. return ret;
  1446. if (val != OV8856_CHIP_ID) {
  1447. dev_err(&client->dev, "chip id mismatch: %x!=%x",
  1448. OV8856_CHIP_ID, val);
  1449. return -ENXIO;
  1450. }
  1451. ret = ov8856_write_reg(ov8856, OV8856_REG_MODE_SELECT,
  1452. OV8856_REG_VALUE_08BIT, OV8856_MODE_STREAMING);
  1453. if (ret)
  1454. return ret;
  1455. ret = ov8856_write_reg(ov8856, OV8856_OTP_MODE_CTRL,
  1456. OV8856_REG_VALUE_08BIT, OV8856_OTP_MODE_AUTO);
  1457. if (ret) {
  1458. dev_err(&client->dev, "failed to set otp mode");
  1459. return ret;
  1460. }
  1461. ret = ov8856_write_reg(ov8856, OV8856_OTP_LOAD_CTRL,
  1462. OV8856_REG_VALUE_08BIT,
  1463. OV8856_OTP_LOAD_CTRL_ENABLE);
  1464. if (ret) {
  1465. dev_err(&client->dev, "failed to enable load control");
  1466. return ret;
  1467. }
  1468. ret = ov8856_read_reg(ov8856, OV8856_MODULE_REVISION,
  1469. OV8856_REG_VALUE_08BIT, &val);
  1470. if (ret) {
  1471. dev_err(&client->dev, "failed to read module revision");
  1472. return ret;
  1473. }
  1474. dev_info(&client->dev, "OV8856 revision %x (%s) at address 0x%02x\n",
  1475. val,
  1476. val == OV8856_2A_MODULE ? "2A" :
  1477. val == OV8856_1B_MODULE ? "1B" : "unknown revision",
  1478. client->addr);
  1479. ret = ov8856_write_reg(ov8856, OV8856_REG_MODE_SELECT,
  1480. OV8856_REG_VALUE_08BIT, OV8856_MODE_STANDBY);
  1481. if (ret) {
  1482. dev_err(&client->dev, "failed to exit streaming mode");
  1483. return ret;
  1484. }
  1485. return 0;
  1486. }
  1487. static int ov8856_get_hwcfg(struct ov8856 *ov8856, struct device *dev)
  1488. {
  1489. struct fwnode_handle *ep;
  1490. struct fwnode_handle *fwnode = dev_fwnode(dev);
  1491. struct v4l2_fwnode_endpoint bus_cfg = {
  1492. .bus_type = V4L2_MBUS_CSI2_DPHY
  1493. };
  1494. u32 xvclk_rate;
  1495. int ret;
  1496. unsigned int i, j;
  1497. if (!fwnode)
  1498. return -ENXIO;
  1499. ret = fwnode_property_read_u32(fwnode, "clock-frequency", &xvclk_rate);
  1500. if (ret)
  1501. return ret;
  1502. if (!is_acpi_node(fwnode)) {
  1503. ov8856->xvclk = devm_clk_get(dev, "xvclk");
  1504. if (IS_ERR(ov8856->xvclk)) {
  1505. dev_err(dev, "could not get xvclk clock (%pe)\n",
  1506. ov8856->xvclk);
  1507. return PTR_ERR(ov8856->xvclk);
  1508. }
  1509. clk_set_rate(ov8856->xvclk, xvclk_rate);
  1510. xvclk_rate = clk_get_rate(ov8856->xvclk);
  1511. }
  1512. if (xvclk_rate != OV8856_XVCLK_19_2)
  1513. dev_warn(dev, "external clock rate %u is unsupported",
  1514. xvclk_rate);
  1515. ov8856->reset_gpio = devm_gpiod_get_optional(dev, "reset",
  1516. GPIOD_OUT_LOW);
  1517. if (IS_ERR(ov8856->reset_gpio))
  1518. return PTR_ERR(ov8856->reset_gpio);
  1519. for (i = 0; i < ARRAY_SIZE(ov8856_supply_names); i++)
  1520. ov8856->supplies[i].supply = ov8856_supply_names[i];
  1521. ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ov8856_supply_names),
  1522. ov8856->supplies);
  1523. if (ret)
  1524. return ret;
  1525. ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
  1526. if (!ep)
  1527. return -ENXIO;
  1528. ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
  1529. fwnode_handle_put(ep);
  1530. if (ret)
  1531. return ret;
  1532. if (bus_cfg.bus.mipi_csi2.num_data_lanes != OV8856_DATA_LANES) {
  1533. dev_err(dev, "number of CSI2 data lanes %d is not supported",
  1534. bus_cfg.bus.mipi_csi2.num_data_lanes);
  1535. ret = -EINVAL;
  1536. goto check_hwcfg_error;
  1537. }
  1538. if (!bus_cfg.nr_of_link_frequencies) {
  1539. dev_err(dev, "no link frequencies defined");
  1540. ret = -EINVAL;
  1541. goto check_hwcfg_error;
  1542. }
  1543. for (i = 0; i < ARRAY_SIZE(link_freq_menu_items); i++) {
  1544. for (j = 0; j < bus_cfg.nr_of_link_frequencies; j++) {
  1545. if (link_freq_menu_items[i] ==
  1546. bus_cfg.link_frequencies[j])
  1547. break;
  1548. }
  1549. if (j == bus_cfg.nr_of_link_frequencies) {
  1550. dev_err(dev, "no link frequency %lld supported",
  1551. link_freq_menu_items[i]);
  1552. ret = -EINVAL;
  1553. goto check_hwcfg_error;
  1554. }
  1555. }
  1556. check_hwcfg_error:
  1557. v4l2_fwnode_endpoint_free(&bus_cfg);
  1558. return ret;
  1559. }
  1560. static int ov8856_remove(struct i2c_client *client)
  1561. {
  1562. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1563. struct ov8856 *ov8856 = to_ov8856(sd);
  1564. v4l2_async_unregister_subdev(sd);
  1565. media_entity_cleanup(&sd->entity);
  1566. v4l2_ctrl_handler_free(sd->ctrl_handler);
  1567. pm_runtime_disable(&client->dev);
  1568. mutex_destroy(&ov8856->mutex);
  1569. __ov8856_power_off(ov8856);
  1570. return 0;
  1571. }
  1572. static int ov8856_probe(struct i2c_client *client)
  1573. {
  1574. struct ov8856 *ov8856;
  1575. int ret;
  1576. ov8856 = devm_kzalloc(&client->dev, sizeof(*ov8856), GFP_KERNEL);
  1577. if (!ov8856)
  1578. return -ENOMEM;
  1579. ret = ov8856_get_hwcfg(ov8856, &client->dev);
  1580. if (ret) {
  1581. dev_err(&client->dev, "failed to get HW configuration: %d",
  1582. ret);
  1583. return ret;
  1584. }
  1585. v4l2_i2c_subdev_init(&ov8856->sd, client, &ov8856_subdev_ops);
  1586. ret = __ov8856_power_on(ov8856);
  1587. if (ret) {
  1588. dev_err(&client->dev, "failed to power on\n");
  1589. return ret;
  1590. }
  1591. ret = ov8856_identify_module(ov8856);
  1592. if (ret) {
  1593. dev_err(&client->dev, "failed to find sensor: %d", ret);
  1594. goto probe_power_off;
  1595. }
  1596. mutex_init(&ov8856->mutex);
  1597. ov8856->cur_mode = &supported_modes[0];
  1598. ret = ov8856_init_controls(ov8856);
  1599. if (ret) {
  1600. dev_err(&client->dev, "failed to init controls: %d", ret);
  1601. goto probe_error_v4l2_ctrl_handler_free;
  1602. }
  1603. ov8856->sd.internal_ops = &ov8856_internal_ops;
  1604. ov8856->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  1605. ov8856->sd.entity.ops = &ov8856_subdev_entity_ops;
  1606. ov8856->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
  1607. ov8856->pad.flags = MEDIA_PAD_FL_SOURCE;
  1608. ret = media_entity_pads_init(&ov8856->sd.entity, 1, &ov8856->pad);
  1609. if (ret) {
  1610. dev_err(&client->dev, "failed to init entity pads: %d", ret);
  1611. goto probe_error_v4l2_ctrl_handler_free;
  1612. }
  1613. ret = v4l2_async_register_subdev_sensor_common(&ov8856->sd);
  1614. if (ret < 0) {
  1615. dev_err(&client->dev, "failed to register V4L2 subdev: %d",
  1616. ret);
  1617. goto probe_error_media_entity_cleanup;
  1618. }
  1619. /*
  1620. * Device is already turned on by i2c-core with ACPI domain PM.
  1621. * Enable runtime PM and turn off the device.
  1622. */
  1623. pm_runtime_set_active(&client->dev);
  1624. pm_runtime_enable(&client->dev);
  1625. pm_runtime_idle(&client->dev);
  1626. return 0;
  1627. probe_error_media_entity_cleanup:
  1628. media_entity_cleanup(&ov8856->sd.entity);
  1629. probe_error_v4l2_ctrl_handler_free:
  1630. v4l2_ctrl_handler_free(ov8856->sd.ctrl_handler);
  1631. mutex_destroy(&ov8856->mutex);
  1632. probe_power_off:
  1633. __ov8856_power_off(ov8856);
  1634. return ret;
  1635. }
  1636. static const struct dev_pm_ops ov8856_pm_ops = {
  1637. SET_SYSTEM_SLEEP_PM_OPS(ov8856_suspend, ov8856_resume)
  1638. };
  1639. #ifdef CONFIG_ACPI
  1640. static const struct acpi_device_id ov8856_acpi_ids[] = {
  1641. {"OVTI8856"},
  1642. {}
  1643. };
  1644. MODULE_DEVICE_TABLE(acpi, ov8856_acpi_ids);
  1645. #endif
  1646. static const struct of_device_id ov8856_of_match[] = {
  1647. { .compatible = "ovti,ov8856" },
  1648. { /* sentinel */ }
  1649. };
  1650. MODULE_DEVICE_TABLE(of, ov8856_of_match);
  1651. static struct i2c_driver ov8856_i2c_driver = {
  1652. .driver = {
  1653. .name = "ov8856",
  1654. .pm = &ov8856_pm_ops,
  1655. .acpi_match_table = ACPI_PTR(ov8856_acpi_ids),
  1656. .of_match_table = ov8856_of_match,
  1657. },
  1658. .probe_new = ov8856_probe,
  1659. .remove = ov8856_remove,
  1660. };
  1661. module_i2c_driver(ov8856_i2c_driver);
  1662. MODULE_AUTHOR("Ben Kao <ben.kao@intel.com>");
  1663. MODULE_DESCRIPTION("OmniVision OV8856 sensor driver");
  1664. MODULE_LICENSE("GPL v2");