meson_venc.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515
  1. /*
  2. * Copyright (C) 2016 BayLibre, SAS
  3. * Author: Neil Armstrong <narmstrong@baylibre.com>
  4. * Copyright (C) 2015 Amlogic, Inc. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of the
  9. * License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <drm/drmP.h>
  22. #include "meson_drv.h"
  23. #include "meson_venc.h"
  24. #include "meson_vpp.h"
  25. #include "meson_vclk.h"
  26. #include "meson_registers.h"
  27. /**
  28. * DOC: Video Encoder
  29. *
  30. * VENC Handle the pixels encoding to the output formats.
  31. * We handle the following encodings :
  32. *
  33. * - CVBS Encoding via the ENCI encoder and VDAC digital to analog converter
  34. * - TMDS/HDMI Encoding via ENCI_DIV and ENCP
  35. * - Setup of more clock rates for HDMI modes
  36. *
  37. * What is missing :
  38. *
  39. * - LCD Panel encoding via ENCL
  40. * - TV Panel encoding via ENCT
  41. *
  42. * VENC paths :
  43. *
  44. * .. code::
  45. *
  46. * _____ _____ ____________________
  47. * vd1---| |-| | | VENC /---------|----VDAC
  48. * vd2---| VIU |-| VPP |-|-----ENCI/-ENCI_DVI-|-|
  49. * osd1--| |-| | | \ | X--HDMI-TX
  50. * osd2--|_____|-|_____| | |\-ENCP--ENCP_DVI-|-|
  51. * | | |
  52. * | \--ENCL-----------|----LVDS
  53. * |____________________|
  54. *
  55. * The ENCI is designed for PAl or NTSC encoding and can go through the VDAC
  56. * directly for CVBS encoding or through the ENCI_DVI encoder for HDMI.
  57. * The ENCP is designed for Progressive encoding but can also generate
  58. * 1080i interlaced pixels, and was initialy desined to encode pixels for
  59. * VDAC to output RGB ou YUV analog outputs.
  60. * It's output is only used through the ENCP_DVI encoder for HDMI.
  61. * The ENCL LVDS encoder is not implemented.
  62. *
  63. * The ENCI and ENCP encoders needs specially defined parameters for each
  64. * supported mode and thus cannot be determined from standard video timings.
  65. *
  66. * The ENCI end ENCP DVI encoders are more generic and can generate any timings
  67. * from the pixel data generated by ENCI or ENCP, so can use the standard video
  68. * timings are source for HW parameters.
  69. */
  70. /* HHI Registers */
  71. #define HHI_VDAC_CNTL0 0x2F4 /* 0xbd offset in data sheet */
  72. #define HHI_VDAC_CNTL1 0x2F8 /* 0xbe offset in data sheet */
  73. #define HHI_HDMI_PHY_CNTL0 0x3a0 /* 0xe8 offset in data sheet */
  74. struct meson_cvbs_enci_mode meson_cvbs_enci_pal = {
  75. .mode_tag = MESON_VENC_MODE_CVBS_PAL,
  76. .hso_begin = 3,
  77. .hso_end = 129,
  78. .vso_even = 3,
  79. .vso_odd = 260,
  80. .macv_max_amp = 7,
  81. .video_prog_mode = 0xff,
  82. .video_mode = 0x13,
  83. .sch_adjust = 0x28,
  84. .yc_delay = 0x343,
  85. .pixel_start = 251,
  86. .pixel_end = 1691,
  87. .top_field_line_start = 22,
  88. .top_field_line_end = 310,
  89. .bottom_field_line_start = 23,
  90. .bottom_field_line_end = 311,
  91. .video_saturation = 9,
  92. .video_contrast = 0,
  93. .video_brightness = 0,
  94. .video_hue = 0,
  95. .analog_sync_adj = 0x8080,
  96. };
  97. struct meson_cvbs_enci_mode meson_cvbs_enci_ntsc = {
  98. .mode_tag = MESON_VENC_MODE_CVBS_NTSC,
  99. .hso_begin = 5,
  100. .hso_end = 129,
  101. .vso_even = 3,
  102. .vso_odd = 260,
  103. .macv_max_amp = 0xb,
  104. .video_prog_mode = 0xf0,
  105. .video_mode = 0x8,
  106. .sch_adjust = 0x20,
  107. .yc_delay = 0x333,
  108. .pixel_start = 227,
  109. .pixel_end = 1667,
  110. .top_field_line_start = 18,
  111. .top_field_line_end = 258,
  112. .bottom_field_line_start = 19,
  113. .bottom_field_line_end = 259,
  114. .video_saturation = 18,
  115. .video_contrast = 3,
  116. .video_brightness = 0,
  117. .video_hue = 0,
  118. .analog_sync_adj = 0x9c00,
  119. };
  120. union meson_hdmi_venc_mode {
  121. struct {
  122. unsigned int mode_tag;
  123. unsigned int hso_begin;
  124. unsigned int hso_end;
  125. unsigned int vso_even;
  126. unsigned int vso_odd;
  127. unsigned int macv_max_amp;
  128. unsigned int video_prog_mode;
  129. unsigned int video_mode;
  130. unsigned int sch_adjust;
  131. unsigned int yc_delay;
  132. unsigned int pixel_start;
  133. unsigned int pixel_end;
  134. unsigned int top_field_line_start;
  135. unsigned int top_field_line_end;
  136. unsigned int bottom_field_line_start;
  137. unsigned int bottom_field_line_end;
  138. } enci;
  139. struct {
  140. unsigned int dvi_settings;
  141. unsigned int video_mode;
  142. unsigned int video_mode_adv;
  143. unsigned int video_prog_mode;
  144. bool video_prog_mode_present;
  145. unsigned int video_sync_mode;
  146. bool video_sync_mode_present;
  147. unsigned int video_yc_dly;
  148. bool video_yc_dly_present;
  149. unsigned int video_rgb_ctrl;
  150. bool video_rgb_ctrl_present;
  151. unsigned int video_filt_ctrl;
  152. bool video_filt_ctrl_present;
  153. unsigned int video_ofld_voav_ofst;
  154. bool video_ofld_voav_ofst_present;
  155. unsigned int yfp1_htime;
  156. unsigned int yfp2_htime;
  157. unsigned int max_pxcnt;
  158. unsigned int hspuls_begin;
  159. unsigned int hspuls_end;
  160. unsigned int hspuls_switch;
  161. unsigned int vspuls_begin;
  162. unsigned int vspuls_end;
  163. unsigned int vspuls_bline;
  164. unsigned int vspuls_eline;
  165. unsigned int eqpuls_begin;
  166. bool eqpuls_begin_present;
  167. unsigned int eqpuls_end;
  168. bool eqpuls_end_present;
  169. unsigned int eqpuls_bline;
  170. bool eqpuls_bline_present;
  171. unsigned int eqpuls_eline;
  172. bool eqpuls_eline_present;
  173. unsigned int havon_begin;
  174. unsigned int havon_end;
  175. unsigned int vavon_bline;
  176. unsigned int vavon_eline;
  177. unsigned int hso_begin;
  178. unsigned int hso_end;
  179. unsigned int vso_begin;
  180. unsigned int vso_end;
  181. unsigned int vso_bline;
  182. unsigned int vso_eline;
  183. bool vso_eline_present;
  184. unsigned int sy_val;
  185. bool sy_val_present;
  186. unsigned int sy2_val;
  187. bool sy2_val_present;
  188. unsigned int max_lncnt;
  189. } encp;
  190. };
  191. union meson_hdmi_venc_mode meson_hdmi_enci_mode_480i = {
  192. .enci = {
  193. .hso_begin = 5,
  194. .hso_end = 129,
  195. .vso_even = 3,
  196. .vso_odd = 260,
  197. .macv_max_amp = 0x810b,
  198. .video_prog_mode = 0xf0,
  199. .video_mode = 0x8,
  200. .sch_adjust = 0x20,
  201. .yc_delay = 0,
  202. .pixel_start = 227,
  203. .pixel_end = 1667,
  204. .top_field_line_start = 18,
  205. .top_field_line_end = 258,
  206. .bottom_field_line_start = 19,
  207. .bottom_field_line_end = 259,
  208. },
  209. };
  210. union meson_hdmi_venc_mode meson_hdmi_enci_mode_576i = {
  211. .enci = {
  212. .hso_begin = 3,
  213. .hso_end = 129,
  214. .vso_even = 3,
  215. .vso_odd = 260,
  216. .macv_max_amp = 8107,
  217. .video_prog_mode = 0xff,
  218. .video_mode = 0x13,
  219. .sch_adjust = 0x28,
  220. .yc_delay = 0x333,
  221. .pixel_start = 251,
  222. .pixel_end = 1691,
  223. .top_field_line_start = 22,
  224. .top_field_line_end = 310,
  225. .bottom_field_line_start = 23,
  226. .bottom_field_line_end = 311,
  227. },
  228. };
  229. union meson_hdmi_venc_mode meson_hdmi_encp_mode_480p = {
  230. .encp = {
  231. .dvi_settings = 0x21,
  232. .video_mode = 0x4000,
  233. .video_mode_adv = 0x9,
  234. .video_prog_mode = 0,
  235. .video_prog_mode_present = true,
  236. .video_sync_mode = 7,
  237. .video_sync_mode_present = true,
  238. /* video_yc_dly */
  239. /* video_rgb_ctrl */
  240. .video_filt_ctrl = 0x2052,
  241. .video_filt_ctrl_present = true,
  242. /* video_ofld_voav_ofst */
  243. .yfp1_htime = 244,
  244. .yfp2_htime = 1630,
  245. .max_pxcnt = 1715,
  246. .hspuls_begin = 0x22,
  247. .hspuls_end = 0xa0,
  248. .hspuls_switch = 88,
  249. .vspuls_begin = 0,
  250. .vspuls_end = 1589,
  251. .vspuls_bline = 0,
  252. .vspuls_eline = 5,
  253. .havon_begin = 249,
  254. .havon_end = 1689,
  255. .vavon_bline = 42,
  256. .vavon_eline = 521,
  257. /* eqpuls_begin */
  258. /* eqpuls_end */
  259. /* eqpuls_bline */
  260. /* eqpuls_eline */
  261. .hso_begin = 3,
  262. .hso_end = 5,
  263. .vso_begin = 3,
  264. .vso_end = 5,
  265. .vso_bline = 0,
  266. /* vso_eline */
  267. .sy_val = 8,
  268. .sy_val_present = true,
  269. .sy2_val = 0x1d8,
  270. .sy2_val_present = true,
  271. .max_lncnt = 524,
  272. },
  273. };
  274. union meson_hdmi_venc_mode meson_hdmi_encp_mode_576p = {
  275. .encp = {
  276. .dvi_settings = 0x21,
  277. .video_mode = 0x4000,
  278. .video_mode_adv = 0x9,
  279. .video_prog_mode = 0,
  280. .video_prog_mode_present = true,
  281. .video_sync_mode = 7,
  282. .video_sync_mode_present = true,
  283. /* video_yc_dly */
  284. /* video_rgb_ctrl */
  285. .video_filt_ctrl = 0x52,
  286. .video_filt_ctrl_present = true,
  287. /* video_ofld_voav_ofst */
  288. .yfp1_htime = 235,
  289. .yfp2_htime = 1674,
  290. .max_pxcnt = 1727,
  291. .hspuls_begin = 0,
  292. .hspuls_end = 0x80,
  293. .hspuls_switch = 88,
  294. .vspuls_begin = 0,
  295. .vspuls_end = 1599,
  296. .vspuls_bline = 0,
  297. .vspuls_eline = 4,
  298. .havon_begin = 235,
  299. .havon_end = 1674,
  300. .vavon_bline = 44,
  301. .vavon_eline = 619,
  302. /* eqpuls_begin */
  303. /* eqpuls_end */
  304. /* eqpuls_bline */
  305. /* eqpuls_eline */
  306. .hso_begin = 0x80,
  307. .hso_end = 0,
  308. .vso_begin = 0,
  309. .vso_end = 5,
  310. .vso_bline = 0,
  311. /* vso_eline */
  312. .sy_val = 8,
  313. .sy_val_present = true,
  314. .sy2_val = 0x1d8,
  315. .sy2_val_present = true,
  316. .max_lncnt = 624,
  317. },
  318. };
  319. union meson_hdmi_venc_mode meson_hdmi_encp_mode_720p60 = {
  320. .encp = {
  321. .dvi_settings = 0x2029,
  322. .video_mode = 0x4040,
  323. .video_mode_adv = 0x19,
  324. /* video_prog_mode */
  325. /* video_sync_mode */
  326. /* video_yc_dly */
  327. /* video_rgb_ctrl */
  328. /* video_filt_ctrl */
  329. /* video_ofld_voav_ofst */
  330. .yfp1_htime = 648,
  331. .yfp2_htime = 3207,
  332. .max_pxcnt = 3299,
  333. .hspuls_begin = 80,
  334. .hspuls_end = 240,
  335. .hspuls_switch = 80,
  336. .vspuls_begin = 688,
  337. .vspuls_end = 3248,
  338. .vspuls_bline = 4,
  339. .vspuls_eline = 8,
  340. .havon_begin = 648,
  341. .havon_end = 3207,
  342. .vavon_bline = 29,
  343. .vavon_eline = 748,
  344. /* eqpuls_begin */
  345. /* eqpuls_end */
  346. /* eqpuls_bline */
  347. /* eqpuls_eline */
  348. .hso_begin = 256,
  349. .hso_end = 168,
  350. .vso_begin = 168,
  351. .vso_end = 256,
  352. .vso_bline = 0,
  353. .vso_eline = 5,
  354. .vso_eline_present = true,
  355. /* sy_val */
  356. /* sy2_val */
  357. .max_lncnt = 749,
  358. },
  359. };
  360. union meson_hdmi_venc_mode meson_hdmi_encp_mode_720p50 = {
  361. .encp = {
  362. .dvi_settings = 0x202d,
  363. .video_mode = 0x4040,
  364. .video_mode_adv = 0x19,
  365. .video_prog_mode = 0x100,
  366. .video_prog_mode_present = true,
  367. .video_sync_mode = 0x407,
  368. .video_sync_mode_present = true,
  369. .video_yc_dly = 0,
  370. .video_yc_dly_present = true,
  371. /* video_rgb_ctrl */
  372. /* video_filt_ctrl */
  373. /* video_ofld_voav_ofst */
  374. .yfp1_htime = 648,
  375. .yfp2_htime = 3207,
  376. .max_pxcnt = 3959,
  377. .hspuls_begin = 80,
  378. .hspuls_end = 240,
  379. .hspuls_switch = 80,
  380. .vspuls_begin = 688,
  381. .vspuls_end = 3248,
  382. .vspuls_bline = 4,
  383. .vspuls_eline = 8,
  384. .havon_begin = 648,
  385. .havon_end = 3207,
  386. .vavon_bline = 29,
  387. .vavon_eline = 748,
  388. /* eqpuls_begin */
  389. /* eqpuls_end */
  390. /* eqpuls_bline */
  391. /* eqpuls_eline */
  392. .hso_begin = 128,
  393. .hso_end = 208,
  394. .vso_begin = 128,
  395. .vso_end = 128,
  396. .vso_bline = 0,
  397. .vso_eline = 5,
  398. .vso_eline_present = true,
  399. /* sy_val */
  400. /* sy2_val */
  401. .max_lncnt = 749,
  402. },
  403. };
  404. union meson_hdmi_venc_mode meson_hdmi_encp_mode_1080i60 = {
  405. .encp = {
  406. .dvi_settings = 0x2029,
  407. .video_mode = 0x5ffc,
  408. .video_mode_adv = 0x19,
  409. .video_prog_mode = 0x100,
  410. .video_prog_mode_present = true,
  411. .video_sync_mode = 0x207,
  412. .video_sync_mode_present = true,
  413. /* video_yc_dly */
  414. /* video_rgb_ctrl */
  415. /* video_filt_ctrl */
  416. .video_ofld_voav_ofst = 0x11,
  417. .video_ofld_voav_ofst_present = true,
  418. .yfp1_htime = 516,
  419. .yfp2_htime = 4355,
  420. .max_pxcnt = 4399,
  421. .hspuls_begin = 88,
  422. .hspuls_end = 264,
  423. .hspuls_switch = 88,
  424. .vspuls_begin = 440,
  425. .vspuls_end = 2200,
  426. .vspuls_bline = 0,
  427. .vspuls_eline = 4,
  428. .havon_begin = 516,
  429. .havon_end = 4355,
  430. .vavon_bline = 20,
  431. .vavon_eline = 559,
  432. .eqpuls_begin = 2288,
  433. .eqpuls_begin_present = true,
  434. .eqpuls_end = 2464,
  435. .eqpuls_end_present = true,
  436. .eqpuls_bline = 0,
  437. .eqpuls_bline_present = true,
  438. .eqpuls_eline = 4,
  439. .eqpuls_eline_present = true,
  440. .hso_begin = 264,
  441. .hso_end = 176,
  442. .vso_begin = 88,
  443. .vso_end = 88,
  444. .vso_bline = 0,
  445. .vso_eline = 5,
  446. .vso_eline_present = true,
  447. /* sy_val */
  448. /* sy2_val */
  449. .max_lncnt = 1124,
  450. },
  451. };
  452. union meson_hdmi_venc_mode meson_hdmi_encp_mode_1080i50 = {
  453. .encp = {
  454. .dvi_settings = 0x202d,
  455. .video_mode = 0x5ffc,
  456. .video_mode_adv = 0x19,
  457. .video_prog_mode = 0x100,
  458. .video_prog_mode_present = true,
  459. .video_sync_mode = 0x7,
  460. .video_sync_mode_present = true,
  461. /* video_yc_dly */
  462. /* video_rgb_ctrl */
  463. /* video_filt_ctrl */
  464. .video_ofld_voav_ofst = 0x11,
  465. .video_ofld_voav_ofst_present = true,
  466. .yfp1_htime = 526,
  467. .yfp2_htime = 4365,
  468. .max_pxcnt = 5279,
  469. .hspuls_begin = 88,
  470. .hspuls_end = 264,
  471. .hspuls_switch = 88,
  472. .vspuls_begin = 440,
  473. .vspuls_end = 2200,
  474. .vspuls_bline = 0,
  475. .vspuls_eline = 4,
  476. .havon_begin = 526,
  477. .havon_end = 4365,
  478. .vavon_bline = 20,
  479. .vavon_eline = 559,
  480. .eqpuls_begin = 2288,
  481. .eqpuls_begin_present = true,
  482. .eqpuls_end = 2464,
  483. .eqpuls_end_present = true,
  484. .eqpuls_bline = 0,
  485. .eqpuls_bline_present = true,
  486. .eqpuls_eline = 4,
  487. .eqpuls_eline_present = true,
  488. .hso_begin = 142,
  489. .hso_end = 230,
  490. .vso_begin = 142,
  491. .vso_end = 142,
  492. .vso_bline = 0,
  493. .vso_eline = 5,
  494. .vso_eline_present = true,
  495. /* sy_val */
  496. /* sy2_val */
  497. .max_lncnt = 1124,
  498. },
  499. };
  500. union meson_hdmi_venc_mode meson_hdmi_encp_mode_1080p24 = {
  501. .encp = {
  502. .dvi_settings = 0xd,
  503. .video_mode = 0x4040,
  504. .video_mode_adv = 0x18,
  505. .video_prog_mode = 0x100,
  506. .video_prog_mode_present = true,
  507. .video_sync_mode = 0x7,
  508. .video_sync_mode_present = true,
  509. .video_yc_dly = 0,
  510. .video_yc_dly_present = true,
  511. .video_rgb_ctrl = 2,
  512. .video_rgb_ctrl_present = true,
  513. .video_filt_ctrl = 0x1052,
  514. .video_filt_ctrl_present = true,
  515. /* video_ofld_voav_ofst */
  516. .yfp1_htime = 271,
  517. .yfp2_htime = 2190,
  518. .max_pxcnt = 2749,
  519. .hspuls_begin = 44,
  520. .hspuls_end = 132,
  521. .hspuls_switch = 44,
  522. .vspuls_begin = 220,
  523. .vspuls_end = 2140,
  524. .vspuls_bline = 0,
  525. .vspuls_eline = 4,
  526. .havon_begin = 271,
  527. .havon_end = 2190,
  528. .vavon_bline = 41,
  529. .vavon_eline = 1120,
  530. /* eqpuls_begin */
  531. /* eqpuls_end */
  532. .eqpuls_bline = 0,
  533. .eqpuls_bline_present = true,
  534. .eqpuls_eline = 4,
  535. .eqpuls_eline_present = true,
  536. .hso_begin = 79,
  537. .hso_end = 123,
  538. .vso_begin = 79,
  539. .vso_end = 79,
  540. .vso_bline = 0,
  541. .vso_eline = 5,
  542. .vso_eline_present = true,
  543. /* sy_val */
  544. /* sy2_val */
  545. .max_lncnt = 1124,
  546. },
  547. };
  548. union meson_hdmi_venc_mode meson_hdmi_encp_mode_1080p30 = {
  549. .encp = {
  550. .dvi_settings = 0x1,
  551. .video_mode = 0x4040,
  552. .video_mode_adv = 0x18,
  553. .video_prog_mode = 0x100,
  554. .video_prog_mode_present = true,
  555. /* video_sync_mode */
  556. /* video_yc_dly */
  557. /* video_rgb_ctrl */
  558. .video_filt_ctrl = 0x1052,
  559. .video_filt_ctrl_present = true,
  560. /* video_ofld_voav_ofst */
  561. .yfp1_htime = 140,
  562. .yfp2_htime = 2060,
  563. .max_pxcnt = 2199,
  564. .hspuls_begin = 2156,
  565. .hspuls_end = 44,
  566. .hspuls_switch = 44,
  567. .vspuls_begin = 140,
  568. .vspuls_end = 2059,
  569. .vspuls_bline = 0,
  570. .vspuls_eline = 4,
  571. .havon_begin = 148,
  572. .havon_end = 2067,
  573. .vavon_bline = 41,
  574. .vavon_eline = 1120,
  575. /* eqpuls_begin */
  576. /* eqpuls_end */
  577. /* eqpuls_bline */
  578. /* eqpuls_eline */
  579. .hso_begin = 44,
  580. .hso_end = 2156,
  581. .vso_begin = 2100,
  582. .vso_end = 2164,
  583. .vso_bline = 0,
  584. .vso_eline = 5,
  585. .vso_eline_present = true,
  586. /* sy_val */
  587. /* sy2_val */
  588. .max_lncnt = 1124,
  589. },
  590. };
  591. union meson_hdmi_venc_mode meson_hdmi_encp_mode_1080p50 = {
  592. .encp = {
  593. .dvi_settings = 0xd,
  594. .video_mode = 0x4040,
  595. .video_mode_adv = 0x18,
  596. .video_prog_mode = 0x100,
  597. .video_prog_mode_present = true,
  598. .video_sync_mode = 0x7,
  599. .video_sync_mode_present = true,
  600. .video_yc_dly = 0,
  601. .video_yc_dly_present = true,
  602. .video_rgb_ctrl = 2,
  603. .video_rgb_ctrl_present = true,
  604. /* video_filt_ctrl */
  605. /* video_ofld_voav_ofst */
  606. .yfp1_htime = 271,
  607. .yfp2_htime = 2190,
  608. .max_pxcnt = 2639,
  609. .hspuls_begin = 44,
  610. .hspuls_end = 132,
  611. .hspuls_switch = 44,
  612. .vspuls_begin = 220,
  613. .vspuls_end = 2140,
  614. .vspuls_bline = 0,
  615. .vspuls_eline = 4,
  616. .havon_begin = 271,
  617. .havon_end = 2190,
  618. .vavon_bline = 41,
  619. .vavon_eline = 1120,
  620. /* eqpuls_begin */
  621. /* eqpuls_end */
  622. .eqpuls_bline = 0,
  623. .eqpuls_bline_present = true,
  624. .eqpuls_eline = 4,
  625. .eqpuls_eline_present = true,
  626. .hso_begin = 79,
  627. .hso_end = 123,
  628. .vso_begin = 79,
  629. .vso_end = 79,
  630. .vso_bline = 0,
  631. .vso_eline = 5,
  632. .vso_eline_present = true,
  633. /* sy_val */
  634. /* sy2_val */
  635. .max_lncnt = 1124,
  636. },
  637. };
  638. union meson_hdmi_venc_mode meson_hdmi_encp_mode_1080p60 = {
  639. .encp = {
  640. .dvi_settings = 0x1,
  641. .video_mode = 0x4040,
  642. .video_mode_adv = 0x18,
  643. .video_prog_mode = 0x100,
  644. .video_prog_mode_present = true,
  645. /* video_sync_mode */
  646. /* video_yc_dly */
  647. /* video_rgb_ctrl */
  648. .video_filt_ctrl = 0x1052,
  649. .video_filt_ctrl_present = true,
  650. /* video_ofld_voav_ofst */
  651. .yfp1_htime = 140,
  652. .yfp2_htime = 2060,
  653. .max_pxcnt = 2199,
  654. .hspuls_begin = 2156,
  655. .hspuls_end = 44,
  656. .hspuls_switch = 44,
  657. .vspuls_begin = 140,
  658. .vspuls_end = 2059,
  659. .vspuls_bline = 0,
  660. .vspuls_eline = 4,
  661. .havon_begin = 148,
  662. .havon_end = 2067,
  663. .vavon_bline = 41,
  664. .vavon_eline = 1120,
  665. /* eqpuls_begin */
  666. /* eqpuls_end */
  667. /* eqpuls_bline */
  668. /* eqpuls_eline */
  669. .hso_begin = 44,
  670. .hso_end = 2156,
  671. .vso_begin = 2100,
  672. .vso_end = 2164,
  673. .vso_bline = 0,
  674. .vso_eline = 5,
  675. .vso_eline_present = true,
  676. /* sy_val */
  677. /* sy2_val */
  678. .max_lncnt = 1124,
  679. },
  680. };
  681. struct meson_hdmi_venc_vic_mode {
  682. unsigned int vic;
  683. union meson_hdmi_venc_mode *mode;
  684. } meson_hdmi_venc_vic_modes[] = {
  685. { 6, &meson_hdmi_enci_mode_480i },
  686. { 7, &meson_hdmi_enci_mode_480i },
  687. { 21, &meson_hdmi_enci_mode_576i },
  688. { 22, &meson_hdmi_enci_mode_576i },
  689. { 2, &meson_hdmi_encp_mode_480p },
  690. { 3, &meson_hdmi_encp_mode_480p },
  691. { 17, &meson_hdmi_encp_mode_576p },
  692. { 18, &meson_hdmi_encp_mode_576p },
  693. { 4, &meson_hdmi_encp_mode_720p60 },
  694. { 19, &meson_hdmi_encp_mode_720p50 },
  695. { 5, &meson_hdmi_encp_mode_1080i60 },
  696. { 20, &meson_hdmi_encp_mode_1080i50 },
  697. { 32, &meson_hdmi_encp_mode_1080p24 },
  698. { 34, &meson_hdmi_encp_mode_1080p30 },
  699. { 31, &meson_hdmi_encp_mode_1080p50 },
  700. { 16, &meson_hdmi_encp_mode_1080p60 },
  701. { 0, NULL}, /* sentinel */
  702. };
  703. static signed int to_signed(unsigned int a)
  704. {
  705. if (a <= 7)
  706. return a;
  707. else
  708. return a - 16;
  709. }
  710. static unsigned long modulo(unsigned long a, unsigned long b)
  711. {
  712. if (a >= b)
  713. return a - b;
  714. else
  715. return a;
  716. }
  717. bool meson_venc_hdmi_supported_vic(int vic)
  718. {
  719. struct meson_hdmi_venc_vic_mode *vmode = meson_hdmi_venc_vic_modes;
  720. while (vmode->vic && vmode->mode) {
  721. if (vmode->vic == vic)
  722. return true;
  723. vmode++;
  724. }
  725. return false;
  726. }
  727. EXPORT_SYMBOL_GPL(meson_venc_hdmi_supported_vic);
  728. static union meson_hdmi_venc_mode *meson_venc_hdmi_get_vic_vmode(int vic)
  729. {
  730. struct meson_hdmi_venc_vic_mode *vmode = meson_hdmi_venc_vic_modes;
  731. while (vmode->vic && vmode->mode) {
  732. if (vmode->vic == vic)
  733. return vmode->mode;
  734. vmode++;
  735. }
  736. return NULL;
  737. }
  738. bool meson_venc_hdmi_venc_repeat(int vic)
  739. {
  740. /* Repeat VENC pixels for 480/576i/p, 720p50/60 and 1080p50/60 */
  741. if (vic == 6 || vic == 7 || /* 480i */
  742. vic == 21 || vic == 22 || /* 576i */
  743. vic == 17 || vic == 18 || /* 576p */
  744. vic == 2 || vic == 3 || /* 480p */
  745. vic == 4 || /* 720p60 */
  746. vic == 19 || /* 720p50 */
  747. vic == 5 || /* 1080i60 */
  748. vic == 20) /* 1080i50 */
  749. return true;
  750. return false;
  751. }
  752. EXPORT_SYMBOL_GPL(meson_venc_hdmi_venc_repeat);
  753. void meson_venc_hdmi_mode_set(struct meson_drm *priv, int vic,
  754. struct drm_display_mode *mode)
  755. {
  756. union meson_hdmi_venc_mode *vmode = NULL;
  757. bool use_enci = false;
  758. bool venc_repeat = false;
  759. bool hdmi_repeat = false;
  760. unsigned int venc_hdmi_latency = 2;
  761. unsigned long total_pixels_venc = 0;
  762. unsigned long active_pixels_venc = 0;
  763. unsigned long front_porch_venc = 0;
  764. unsigned long hsync_pixels_venc = 0;
  765. unsigned long de_h_begin = 0;
  766. unsigned long de_h_end = 0;
  767. unsigned long de_v_begin_even = 0;
  768. unsigned long de_v_end_even = 0;
  769. unsigned long de_v_begin_odd = 0;
  770. unsigned long de_v_end_odd = 0;
  771. unsigned long hs_begin = 0;
  772. unsigned long hs_end = 0;
  773. unsigned long vs_adjust = 0;
  774. unsigned long vs_bline_evn = 0;
  775. unsigned long vs_eline_evn = 0;
  776. unsigned long vs_bline_odd = 0;
  777. unsigned long vs_eline_odd = 0;
  778. unsigned long vso_begin_evn = 0;
  779. unsigned long vso_begin_odd = 0;
  780. unsigned int eof_lines;
  781. unsigned int sof_lines;
  782. unsigned int vsync_lines;
  783. vmode = meson_venc_hdmi_get_vic_vmode(vic);
  784. if (!vmode) {
  785. dev_err(priv->dev, "%s: Fatal Error, unsupported vic %d\n",
  786. __func__, vic);
  787. return;
  788. }
  789. /* Use VENCI for 480i and 576i and double HDMI pixels */
  790. if (mode->flags & DRM_MODE_FLAG_DBLCLK) {
  791. hdmi_repeat = true;
  792. use_enci = true;
  793. venc_hdmi_latency = 1;
  794. }
  795. /* Repeat VENC pixels for 480/576i/p, 720p50/60 and 1080p50/60 */
  796. if (meson_venc_hdmi_venc_repeat(vic))
  797. venc_repeat = true;
  798. eof_lines = mode->vsync_start - mode->vdisplay;
  799. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  800. eof_lines /= 2;
  801. sof_lines = mode->vtotal - mode->vsync_end;
  802. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  803. sof_lines /= 2;
  804. vsync_lines = mode->vsync_end - mode->vsync_start;
  805. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  806. vsync_lines /= 2;
  807. total_pixels_venc = mode->htotal;
  808. if (hdmi_repeat)
  809. total_pixels_venc /= 2;
  810. if (venc_repeat)
  811. total_pixels_venc *= 2;
  812. active_pixels_venc = mode->hdisplay;
  813. if (hdmi_repeat)
  814. active_pixels_venc /= 2;
  815. if (venc_repeat)
  816. active_pixels_venc *= 2;
  817. front_porch_venc = (mode->hsync_start - mode->hdisplay);
  818. if (hdmi_repeat)
  819. front_porch_venc /= 2;
  820. if (venc_repeat)
  821. front_porch_venc *= 2;
  822. hsync_pixels_venc = (mode->hsync_end - mode->hsync_start);
  823. if (hdmi_repeat)
  824. hsync_pixels_venc /= 2;
  825. if (venc_repeat)
  826. hsync_pixels_venc *= 2;
  827. /* Disable VDACs */
  828. writel_bits_relaxed(0x1f, 0x1f,
  829. priv->io_base + _REG(VENC_VDAC_SETTING));
  830. writel_relaxed(0, priv->io_base + _REG(ENCI_VIDEO_EN));
  831. writel_relaxed(0, priv->io_base + _REG(ENCP_VIDEO_EN));
  832. if (use_enci) {
  833. unsigned int lines_f0;
  834. unsigned int lines_f1;
  835. /* CVBS Filter settings */
  836. writel_relaxed(0x12, priv->io_base + _REG(ENCI_CFILT_CTRL));
  837. writel_relaxed(0x12, priv->io_base + _REG(ENCI_CFILT_CTRL2));
  838. /* Digital Video Select : Interlace, clk27 clk, external */
  839. writel_relaxed(0, priv->io_base + _REG(VENC_DVI_SETTING));
  840. /* Reset Video Mode */
  841. writel_relaxed(0, priv->io_base + _REG(ENCI_VIDEO_MODE));
  842. writel_relaxed(0, priv->io_base + _REG(ENCI_VIDEO_MODE_ADV));
  843. /* Horizontal sync signal output */
  844. writel_relaxed(vmode->enci.hso_begin,
  845. priv->io_base + _REG(ENCI_SYNC_HSO_BEGIN));
  846. writel_relaxed(vmode->enci.hso_end,
  847. priv->io_base + _REG(ENCI_SYNC_HSO_END));
  848. /* Vertical Sync lines */
  849. writel_relaxed(vmode->enci.vso_even,
  850. priv->io_base + _REG(ENCI_SYNC_VSO_EVNLN));
  851. writel_relaxed(vmode->enci.vso_odd,
  852. priv->io_base + _REG(ENCI_SYNC_VSO_ODDLN));
  853. /* Macrovision max amplitude change */
  854. writel_relaxed(vmode->enci.macv_max_amp,
  855. priv->io_base + _REG(ENCI_MACV_MAX_AMP));
  856. /* Video mode */
  857. writel_relaxed(vmode->enci.video_prog_mode,
  858. priv->io_base + _REG(VENC_VIDEO_PROG_MODE));
  859. writel_relaxed(vmode->enci.video_mode,
  860. priv->io_base + _REG(ENCI_VIDEO_MODE));
  861. /* Advanced Video Mode :
  862. * Demux shifting 0x2
  863. * Blank line end at line17/22
  864. * High bandwidth Luma Filter
  865. * Low bandwidth Chroma Filter
  866. * Bypass luma low pass filter
  867. * No macrovision on CSYNC
  868. */
  869. writel_relaxed(0x26, priv->io_base + _REG(ENCI_VIDEO_MODE_ADV));
  870. writel(vmode->enci.sch_adjust,
  871. priv->io_base + _REG(ENCI_VIDEO_SCH));
  872. /* Sync mode : MASTER Master mode, free run, send HSO/VSO out */
  873. writel_relaxed(0x07, priv->io_base + _REG(ENCI_SYNC_MODE));
  874. if (vmode->enci.yc_delay)
  875. writel_relaxed(vmode->enci.yc_delay,
  876. priv->io_base + _REG(ENCI_YC_DELAY));
  877. /* UNreset Interlaced TV Encoder */
  878. writel_relaxed(0, priv->io_base + _REG(ENCI_DBG_PX_RST));
  879. /* Enable Vfifo2vd, Y_Cb_Y_Cr select */
  880. writel_relaxed(0x4e01, priv->io_base + _REG(ENCI_VFIFO2VD_CTL));
  881. /* Timings */
  882. writel_relaxed(vmode->enci.pixel_start,
  883. priv->io_base + _REG(ENCI_VFIFO2VD_PIXEL_START));
  884. writel_relaxed(vmode->enci.pixel_end,
  885. priv->io_base + _REG(ENCI_VFIFO2VD_PIXEL_END));
  886. writel_relaxed(vmode->enci.top_field_line_start,
  887. priv->io_base + _REG(ENCI_VFIFO2VD_LINE_TOP_START));
  888. writel_relaxed(vmode->enci.top_field_line_end,
  889. priv->io_base + _REG(ENCI_VFIFO2VD_LINE_TOP_END));
  890. writel_relaxed(vmode->enci.bottom_field_line_start,
  891. priv->io_base + _REG(ENCI_VFIFO2VD_LINE_BOT_START));
  892. writel_relaxed(vmode->enci.bottom_field_line_end,
  893. priv->io_base + _REG(ENCI_VFIFO2VD_LINE_BOT_END));
  894. /* Select ENCI for VIU */
  895. meson_vpp_setup_mux(priv, MESON_VIU_VPP_MUX_ENCI);
  896. /* Interlace video enable */
  897. writel_relaxed(1, priv->io_base + _REG(ENCI_VIDEO_EN));
  898. lines_f0 = mode->vtotal >> 1;
  899. lines_f1 = lines_f0 + 1;
  900. de_h_begin = modulo(readl_relaxed(priv->io_base +
  901. _REG(ENCI_VFIFO2VD_PIXEL_START))
  902. + venc_hdmi_latency,
  903. total_pixels_venc);
  904. de_h_end = modulo(de_h_begin + active_pixels_venc,
  905. total_pixels_venc);
  906. writel_relaxed(de_h_begin,
  907. priv->io_base + _REG(ENCI_DE_H_BEGIN));
  908. writel_relaxed(de_h_end,
  909. priv->io_base + _REG(ENCI_DE_H_END));
  910. de_v_begin_even = readl_relaxed(priv->io_base +
  911. _REG(ENCI_VFIFO2VD_LINE_TOP_START));
  912. de_v_end_even = de_v_begin_even + mode->vdisplay;
  913. de_v_begin_odd = readl_relaxed(priv->io_base +
  914. _REG(ENCI_VFIFO2VD_LINE_BOT_START));
  915. de_v_end_odd = de_v_begin_odd + mode->vdisplay;
  916. writel_relaxed(de_v_begin_even,
  917. priv->io_base + _REG(ENCI_DE_V_BEGIN_EVEN));
  918. writel_relaxed(de_v_end_even,
  919. priv->io_base + _REG(ENCI_DE_V_END_EVEN));
  920. writel_relaxed(de_v_begin_odd,
  921. priv->io_base + _REG(ENCI_DE_V_BEGIN_ODD));
  922. writel_relaxed(de_v_end_odd,
  923. priv->io_base + _REG(ENCI_DE_V_END_ODD));
  924. /* Program Hsync timing */
  925. hs_begin = de_h_end + front_porch_venc;
  926. if (de_h_end + front_porch_venc >= total_pixels_venc) {
  927. hs_begin -= total_pixels_venc;
  928. vs_adjust = 1;
  929. } else {
  930. hs_begin = de_h_end + front_porch_venc;
  931. vs_adjust = 0;
  932. }
  933. hs_end = modulo(hs_begin + hsync_pixels_venc,
  934. total_pixels_venc);
  935. writel_relaxed(hs_begin,
  936. priv->io_base + _REG(ENCI_DVI_HSO_BEGIN));
  937. writel_relaxed(hs_end,
  938. priv->io_base + _REG(ENCI_DVI_HSO_END));
  939. /* Program Vsync timing for even field */
  940. if (((de_v_end_odd - 1) + eof_lines + vs_adjust) >= lines_f1) {
  941. vs_bline_evn = (de_v_end_odd - 1)
  942. + eof_lines
  943. + vs_adjust
  944. - lines_f1;
  945. vs_eline_evn = vs_bline_evn + vsync_lines;
  946. writel_relaxed(vs_bline_evn,
  947. priv->io_base + _REG(ENCI_DVI_VSO_BLINE_EVN));
  948. writel_relaxed(vs_eline_evn,
  949. priv->io_base + _REG(ENCI_DVI_VSO_ELINE_EVN));
  950. writel_relaxed(hs_begin,
  951. priv->io_base + _REG(ENCI_DVI_VSO_BEGIN_EVN));
  952. writel_relaxed(hs_begin,
  953. priv->io_base + _REG(ENCI_DVI_VSO_END_EVN));
  954. } else {
  955. vs_bline_odd = (de_v_end_odd - 1)
  956. + eof_lines
  957. + vs_adjust;
  958. writel_relaxed(vs_bline_odd,
  959. priv->io_base + _REG(ENCI_DVI_VSO_BLINE_ODD));
  960. writel_relaxed(hs_begin,
  961. priv->io_base + _REG(ENCI_DVI_VSO_BEGIN_ODD));
  962. if ((vs_bline_odd + vsync_lines) >= lines_f1) {
  963. vs_eline_evn = vs_bline_odd
  964. + vsync_lines
  965. - lines_f1;
  966. writel_relaxed(vs_eline_evn, priv->io_base
  967. + _REG(ENCI_DVI_VSO_ELINE_EVN));
  968. writel_relaxed(hs_begin, priv->io_base
  969. + _REG(ENCI_DVI_VSO_END_EVN));
  970. } else {
  971. vs_eline_odd = vs_bline_odd
  972. + vsync_lines;
  973. writel_relaxed(vs_eline_odd, priv->io_base
  974. + _REG(ENCI_DVI_VSO_ELINE_ODD));
  975. writel_relaxed(hs_begin, priv->io_base
  976. + _REG(ENCI_DVI_VSO_END_ODD));
  977. }
  978. }
  979. /* Program Vsync timing for odd field */
  980. if (((de_v_end_even - 1) + (eof_lines + 1)) >= lines_f0) {
  981. vs_bline_odd = (de_v_end_even - 1)
  982. + (eof_lines + 1)
  983. - lines_f0;
  984. vs_eline_odd = vs_bline_odd + vsync_lines;
  985. writel_relaxed(vs_bline_odd,
  986. priv->io_base + _REG(ENCI_DVI_VSO_BLINE_ODD));
  987. writel_relaxed(vs_eline_odd,
  988. priv->io_base + _REG(ENCI_DVI_VSO_ELINE_ODD));
  989. vso_begin_odd = modulo(hs_begin
  990. + (total_pixels_venc >> 1),
  991. total_pixels_venc);
  992. writel_relaxed(vso_begin_odd,
  993. priv->io_base + _REG(ENCI_DVI_VSO_BEGIN_ODD));
  994. writel_relaxed(vso_begin_odd,
  995. priv->io_base + _REG(ENCI_DVI_VSO_END_ODD));
  996. } else {
  997. vs_bline_evn = (de_v_end_even - 1)
  998. + (eof_lines + 1);
  999. writel_relaxed(vs_bline_evn,
  1000. priv->io_base + _REG(ENCI_DVI_VSO_BLINE_EVN));
  1001. vso_begin_evn = modulo(hs_begin
  1002. + (total_pixels_venc >> 1),
  1003. total_pixels_venc);
  1004. writel_relaxed(vso_begin_evn, priv->io_base
  1005. + _REG(ENCI_DVI_VSO_BEGIN_EVN));
  1006. if (vs_bline_evn + vsync_lines >= lines_f0) {
  1007. vs_eline_odd = vs_bline_evn
  1008. + vsync_lines
  1009. - lines_f0;
  1010. writel_relaxed(vs_eline_odd, priv->io_base
  1011. + _REG(ENCI_DVI_VSO_ELINE_ODD));
  1012. writel_relaxed(vso_begin_evn, priv->io_base
  1013. + _REG(ENCI_DVI_VSO_END_ODD));
  1014. } else {
  1015. vs_eline_evn = vs_bline_evn + vsync_lines;
  1016. writel_relaxed(vs_eline_evn, priv->io_base
  1017. + _REG(ENCI_DVI_VSO_ELINE_EVN));
  1018. writel_relaxed(vso_begin_evn, priv->io_base
  1019. + _REG(ENCI_DVI_VSO_END_EVN));
  1020. }
  1021. }
  1022. } else {
  1023. writel_relaxed(vmode->encp.dvi_settings,
  1024. priv->io_base + _REG(VENC_DVI_SETTING));
  1025. writel_relaxed(vmode->encp.video_mode,
  1026. priv->io_base + _REG(ENCP_VIDEO_MODE));
  1027. writel_relaxed(vmode->encp.video_mode_adv,
  1028. priv->io_base + _REG(ENCP_VIDEO_MODE_ADV));
  1029. if (vmode->encp.video_prog_mode_present)
  1030. writel_relaxed(vmode->encp.video_prog_mode,
  1031. priv->io_base + _REG(VENC_VIDEO_PROG_MODE));
  1032. if (vmode->encp.video_sync_mode_present)
  1033. writel_relaxed(vmode->encp.video_sync_mode,
  1034. priv->io_base + _REG(ENCP_VIDEO_SYNC_MODE));
  1035. if (vmode->encp.video_yc_dly_present)
  1036. writel_relaxed(vmode->encp.video_yc_dly,
  1037. priv->io_base + _REG(ENCP_VIDEO_YC_DLY));
  1038. if (vmode->encp.video_rgb_ctrl_present)
  1039. writel_relaxed(vmode->encp.video_rgb_ctrl,
  1040. priv->io_base + _REG(ENCP_VIDEO_RGB_CTRL));
  1041. if (vmode->encp.video_filt_ctrl_present)
  1042. writel_relaxed(vmode->encp.video_filt_ctrl,
  1043. priv->io_base + _REG(ENCP_VIDEO_FILT_CTRL));
  1044. if (vmode->encp.video_ofld_voav_ofst_present)
  1045. writel_relaxed(vmode->encp.video_ofld_voav_ofst,
  1046. priv->io_base
  1047. + _REG(ENCP_VIDEO_OFLD_VOAV_OFST));
  1048. writel_relaxed(vmode->encp.yfp1_htime,
  1049. priv->io_base + _REG(ENCP_VIDEO_YFP1_HTIME));
  1050. writel_relaxed(vmode->encp.yfp2_htime,
  1051. priv->io_base + _REG(ENCP_VIDEO_YFP2_HTIME));
  1052. writel_relaxed(vmode->encp.max_pxcnt,
  1053. priv->io_base + _REG(ENCP_VIDEO_MAX_PXCNT));
  1054. writel_relaxed(vmode->encp.hspuls_begin,
  1055. priv->io_base + _REG(ENCP_VIDEO_HSPULS_BEGIN));
  1056. writel_relaxed(vmode->encp.hspuls_end,
  1057. priv->io_base + _REG(ENCP_VIDEO_HSPULS_END));
  1058. writel_relaxed(vmode->encp.hspuls_switch,
  1059. priv->io_base + _REG(ENCP_VIDEO_HSPULS_SWITCH));
  1060. writel_relaxed(vmode->encp.vspuls_begin,
  1061. priv->io_base + _REG(ENCP_VIDEO_VSPULS_BEGIN));
  1062. writel_relaxed(vmode->encp.vspuls_end,
  1063. priv->io_base + _REG(ENCP_VIDEO_VSPULS_END));
  1064. writel_relaxed(vmode->encp.vspuls_bline,
  1065. priv->io_base + _REG(ENCP_VIDEO_VSPULS_BLINE));
  1066. writel_relaxed(vmode->encp.vspuls_eline,
  1067. priv->io_base + _REG(ENCP_VIDEO_VSPULS_ELINE));
  1068. if (vmode->encp.eqpuls_begin_present)
  1069. writel_relaxed(vmode->encp.eqpuls_begin,
  1070. priv->io_base + _REG(ENCP_VIDEO_EQPULS_BEGIN));
  1071. if (vmode->encp.eqpuls_end_present)
  1072. writel_relaxed(vmode->encp.eqpuls_end,
  1073. priv->io_base + _REG(ENCP_VIDEO_EQPULS_END));
  1074. if (vmode->encp.eqpuls_bline_present)
  1075. writel_relaxed(vmode->encp.eqpuls_bline,
  1076. priv->io_base + _REG(ENCP_VIDEO_EQPULS_BLINE));
  1077. if (vmode->encp.eqpuls_eline_present)
  1078. writel_relaxed(vmode->encp.eqpuls_eline,
  1079. priv->io_base + _REG(ENCP_VIDEO_EQPULS_ELINE));
  1080. writel_relaxed(vmode->encp.havon_begin,
  1081. priv->io_base + _REG(ENCP_VIDEO_HAVON_BEGIN));
  1082. writel_relaxed(vmode->encp.havon_end,
  1083. priv->io_base + _REG(ENCP_VIDEO_HAVON_END));
  1084. writel_relaxed(vmode->encp.vavon_bline,
  1085. priv->io_base + _REG(ENCP_VIDEO_VAVON_BLINE));
  1086. writel_relaxed(vmode->encp.vavon_eline,
  1087. priv->io_base + _REG(ENCP_VIDEO_VAVON_ELINE));
  1088. writel_relaxed(vmode->encp.hso_begin,
  1089. priv->io_base + _REG(ENCP_VIDEO_HSO_BEGIN));
  1090. writel_relaxed(vmode->encp.hso_end,
  1091. priv->io_base + _REG(ENCP_VIDEO_HSO_END));
  1092. writel_relaxed(vmode->encp.vso_begin,
  1093. priv->io_base + _REG(ENCP_VIDEO_VSO_BEGIN));
  1094. writel_relaxed(vmode->encp.vso_end,
  1095. priv->io_base + _REG(ENCP_VIDEO_VSO_END));
  1096. writel_relaxed(vmode->encp.vso_bline,
  1097. priv->io_base + _REG(ENCP_VIDEO_VSO_BLINE));
  1098. if (vmode->encp.vso_eline_present)
  1099. writel_relaxed(vmode->encp.vso_eline,
  1100. priv->io_base + _REG(ENCP_VIDEO_VSO_ELINE));
  1101. if (vmode->encp.sy_val_present)
  1102. writel_relaxed(vmode->encp.sy_val,
  1103. priv->io_base + _REG(ENCP_VIDEO_SY_VAL));
  1104. if (vmode->encp.sy2_val_present)
  1105. writel_relaxed(vmode->encp.sy2_val,
  1106. priv->io_base + _REG(ENCP_VIDEO_SY2_VAL));
  1107. writel_relaxed(vmode->encp.max_lncnt,
  1108. priv->io_base + _REG(ENCP_VIDEO_MAX_LNCNT));
  1109. writel_relaxed(1, priv->io_base + _REG(ENCP_VIDEO_EN));
  1110. /* Set DE signal’s polarity is active high */
  1111. writel_bits_relaxed(BIT(14), BIT(14),
  1112. priv->io_base + _REG(ENCP_VIDEO_MODE));
  1113. /* Program DE timing */
  1114. de_h_begin = modulo(readl_relaxed(priv->io_base +
  1115. _REG(ENCP_VIDEO_HAVON_BEGIN))
  1116. + venc_hdmi_latency,
  1117. total_pixels_venc);
  1118. de_h_end = modulo(de_h_begin + active_pixels_venc,
  1119. total_pixels_venc);
  1120. writel_relaxed(de_h_begin,
  1121. priv->io_base + _REG(ENCP_DE_H_BEGIN));
  1122. writel_relaxed(de_h_end,
  1123. priv->io_base + _REG(ENCP_DE_H_END));
  1124. /* Program DE timing for even field */
  1125. de_v_begin_even = readl_relaxed(priv->io_base
  1126. + _REG(ENCP_VIDEO_VAVON_BLINE));
  1127. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  1128. de_v_end_even = de_v_begin_even +
  1129. (mode->vdisplay / 2);
  1130. else
  1131. de_v_end_even = de_v_begin_even + mode->vdisplay;
  1132. writel_relaxed(de_v_begin_even,
  1133. priv->io_base + _REG(ENCP_DE_V_BEGIN_EVEN));
  1134. writel_relaxed(de_v_end_even,
  1135. priv->io_base + _REG(ENCP_DE_V_END_EVEN));
  1136. /* Program DE timing for odd field if needed */
  1137. if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
  1138. unsigned int ofld_voav_ofst =
  1139. readl_relaxed(priv->io_base +
  1140. _REG(ENCP_VIDEO_OFLD_VOAV_OFST));
  1141. de_v_begin_odd = to_signed((ofld_voav_ofst & 0xf0) >> 4)
  1142. + de_v_begin_even
  1143. + ((mode->vtotal - 1) / 2);
  1144. de_v_end_odd = de_v_begin_odd + (mode->vdisplay / 2);
  1145. writel_relaxed(de_v_begin_odd,
  1146. priv->io_base + _REG(ENCP_DE_V_BEGIN_ODD));
  1147. writel_relaxed(de_v_end_odd,
  1148. priv->io_base + _REG(ENCP_DE_V_END_ODD));
  1149. }
  1150. /* Program Hsync timing */
  1151. if ((de_h_end + front_porch_venc) >= total_pixels_venc) {
  1152. hs_begin = de_h_end
  1153. + front_porch_venc
  1154. - total_pixels_venc;
  1155. vs_adjust = 1;
  1156. } else {
  1157. hs_begin = de_h_end
  1158. + front_porch_venc;
  1159. vs_adjust = 0;
  1160. }
  1161. hs_end = modulo(hs_begin + hsync_pixels_venc,
  1162. total_pixels_venc);
  1163. writel_relaxed(hs_begin,
  1164. priv->io_base + _REG(ENCP_DVI_HSO_BEGIN));
  1165. writel_relaxed(hs_end,
  1166. priv->io_base + _REG(ENCP_DVI_HSO_END));
  1167. /* Program Vsync timing for even field */
  1168. if (de_v_begin_even >=
  1169. (sof_lines + vsync_lines + (1 - vs_adjust)))
  1170. vs_bline_evn = de_v_begin_even
  1171. - sof_lines
  1172. - vsync_lines
  1173. - (1 - vs_adjust);
  1174. else
  1175. vs_bline_evn = mode->vtotal
  1176. + de_v_begin_even
  1177. - sof_lines
  1178. - vsync_lines
  1179. - (1 - vs_adjust);
  1180. vs_eline_evn = modulo(vs_bline_evn + vsync_lines,
  1181. mode->vtotal);
  1182. writel_relaxed(vs_bline_evn,
  1183. priv->io_base + _REG(ENCP_DVI_VSO_BLINE_EVN));
  1184. writel_relaxed(vs_eline_evn,
  1185. priv->io_base + _REG(ENCP_DVI_VSO_ELINE_EVN));
  1186. vso_begin_evn = hs_begin;
  1187. writel_relaxed(vso_begin_evn,
  1188. priv->io_base + _REG(ENCP_DVI_VSO_BEGIN_EVN));
  1189. writel_relaxed(vso_begin_evn,
  1190. priv->io_base + _REG(ENCP_DVI_VSO_END_EVN));
  1191. /* Program Vsync timing for odd field if needed */
  1192. if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
  1193. vs_bline_odd = (de_v_begin_odd - 1)
  1194. - sof_lines
  1195. - vsync_lines;
  1196. vs_eline_odd = (de_v_begin_odd - 1)
  1197. - vsync_lines;
  1198. vso_begin_odd = modulo(hs_begin
  1199. + (total_pixels_venc >> 1),
  1200. total_pixels_venc);
  1201. writel_relaxed(vs_bline_odd,
  1202. priv->io_base + _REG(ENCP_DVI_VSO_BLINE_ODD));
  1203. writel_relaxed(vs_eline_odd,
  1204. priv->io_base + _REG(ENCP_DVI_VSO_ELINE_ODD));
  1205. writel_relaxed(vso_begin_odd,
  1206. priv->io_base + _REG(ENCP_DVI_VSO_BEGIN_ODD));
  1207. writel_relaxed(vso_begin_odd,
  1208. priv->io_base + _REG(ENCP_DVI_VSO_END_ODD));
  1209. }
  1210. /* Select ENCP for VIU */
  1211. meson_vpp_setup_mux(priv, MESON_VIU_VPP_MUX_ENCP);
  1212. }
  1213. writel_relaxed((use_enci ? 1 : 2) |
  1214. (mode->flags & DRM_MODE_FLAG_PHSYNC ? 1 << 2 : 0) |
  1215. (mode->flags & DRM_MODE_FLAG_PVSYNC ? 1 << 3 : 0) |
  1216. 4 << 5 |
  1217. (venc_repeat ? 1 << 8 : 0) |
  1218. (hdmi_repeat ? 1 << 12 : 0),
  1219. priv->io_base + _REG(VPU_HDMI_SETTING));
  1220. priv->venc.hdmi_repeat = hdmi_repeat;
  1221. priv->venc.venc_repeat = venc_repeat;
  1222. priv->venc.hdmi_use_enci = use_enci;
  1223. priv->venc.current_mode = MESON_VENC_MODE_HDMI;
  1224. }
  1225. EXPORT_SYMBOL_GPL(meson_venc_hdmi_mode_set);
  1226. void meson_venci_cvbs_mode_set(struct meson_drm *priv,
  1227. struct meson_cvbs_enci_mode *mode)
  1228. {
  1229. if (mode->mode_tag == priv->venc.current_mode)
  1230. return;
  1231. /* CVBS Filter settings */
  1232. writel_relaxed(0x12, priv->io_base + _REG(ENCI_CFILT_CTRL));
  1233. writel_relaxed(0x12, priv->io_base + _REG(ENCI_CFILT_CTRL2));
  1234. /* Digital Video Select : Interlace, clk27 clk, external */
  1235. writel_relaxed(0, priv->io_base + _REG(VENC_DVI_SETTING));
  1236. /* Reset Video Mode */
  1237. writel_relaxed(0, priv->io_base + _REG(ENCI_VIDEO_MODE));
  1238. writel_relaxed(0, priv->io_base + _REG(ENCI_VIDEO_MODE_ADV));
  1239. /* Horizontal sync signal output */
  1240. writel_relaxed(mode->hso_begin,
  1241. priv->io_base + _REG(ENCI_SYNC_HSO_BEGIN));
  1242. writel_relaxed(mode->hso_end,
  1243. priv->io_base + _REG(ENCI_SYNC_HSO_END));
  1244. /* Vertical Sync lines */
  1245. writel_relaxed(mode->vso_even,
  1246. priv->io_base + _REG(ENCI_SYNC_VSO_EVNLN));
  1247. writel_relaxed(mode->vso_odd,
  1248. priv->io_base + _REG(ENCI_SYNC_VSO_ODDLN));
  1249. /* Macrovision max amplitude change */
  1250. writel_relaxed(0x8100 + mode->macv_max_amp,
  1251. priv->io_base + _REG(ENCI_MACV_MAX_AMP));
  1252. /* Video mode */
  1253. writel_relaxed(mode->video_prog_mode,
  1254. priv->io_base + _REG(VENC_VIDEO_PROG_MODE));
  1255. writel_relaxed(mode->video_mode,
  1256. priv->io_base + _REG(ENCI_VIDEO_MODE));
  1257. /* Advanced Video Mode :
  1258. * Demux shifting 0x2
  1259. * Blank line end at line17/22
  1260. * High bandwidth Luma Filter
  1261. * Low bandwidth Chroma Filter
  1262. * Bypass luma low pass filter
  1263. * No macrovision on CSYNC
  1264. */
  1265. writel_relaxed(0x26, priv->io_base + _REG(ENCI_VIDEO_MODE_ADV));
  1266. writel(mode->sch_adjust, priv->io_base + _REG(ENCI_VIDEO_SCH));
  1267. /* Sync mode : MASTER Master mode, free run, send HSO/VSO out */
  1268. writel_relaxed(0x07, priv->io_base + _REG(ENCI_SYNC_MODE));
  1269. /* 0x3 Y, C, and Component Y delay */
  1270. writel_relaxed(mode->yc_delay, priv->io_base + _REG(ENCI_YC_DELAY));
  1271. /* Timings */
  1272. writel_relaxed(mode->pixel_start,
  1273. priv->io_base + _REG(ENCI_VFIFO2VD_PIXEL_START));
  1274. writel_relaxed(mode->pixel_end,
  1275. priv->io_base + _REG(ENCI_VFIFO2VD_PIXEL_END));
  1276. writel_relaxed(mode->top_field_line_start,
  1277. priv->io_base + _REG(ENCI_VFIFO2VD_LINE_TOP_START));
  1278. writel_relaxed(mode->top_field_line_end,
  1279. priv->io_base + _REG(ENCI_VFIFO2VD_LINE_TOP_END));
  1280. writel_relaxed(mode->bottom_field_line_start,
  1281. priv->io_base + _REG(ENCI_VFIFO2VD_LINE_BOT_START));
  1282. writel_relaxed(mode->bottom_field_line_end,
  1283. priv->io_base + _REG(ENCI_VFIFO2VD_LINE_BOT_END));
  1284. /* Internal Venc, Internal VIU Sync, Internal Vencoder */
  1285. writel_relaxed(0, priv->io_base + _REG(VENC_SYNC_ROUTE));
  1286. /* UNreset Interlaced TV Encoder */
  1287. writel_relaxed(0, priv->io_base + _REG(ENCI_DBG_PX_RST));
  1288. /* Enable Vfifo2vd, Y_Cb_Y_Cr select */
  1289. writel_relaxed(0x4e01, priv->io_base + _REG(ENCI_VFIFO2VD_CTL));
  1290. /* Power UP Dacs */
  1291. writel_relaxed(0, priv->io_base + _REG(VENC_VDAC_SETTING));
  1292. /* Video Upsampling */
  1293. writel_relaxed(0x0061, priv->io_base + _REG(VENC_UPSAMPLE_CTRL0));
  1294. writel_relaxed(0x4061, priv->io_base + _REG(VENC_UPSAMPLE_CTRL1));
  1295. writel_relaxed(0x5061, priv->io_base + _REG(VENC_UPSAMPLE_CTRL2));
  1296. /* Select Interlace Y DACs */
  1297. writel_relaxed(0, priv->io_base + _REG(VENC_VDAC_DACSEL0));
  1298. writel_relaxed(0, priv->io_base + _REG(VENC_VDAC_DACSEL1));
  1299. writel_relaxed(0, priv->io_base + _REG(VENC_VDAC_DACSEL2));
  1300. writel_relaxed(0, priv->io_base + _REG(VENC_VDAC_DACSEL3));
  1301. writel_relaxed(0, priv->io_base + _REG(VENC_VDAC_DACSEL4));
  1302. writel_relaxed(0, priv->io_base + _REG(VENC_VDAC_DACSEL5));
  1303. /* Select ENCI for VIU */
  1304. meson_vpp_setup_mux(priv, MESON_VIU_VPP_MUX_ENCI);
  1305. /* Enable ENCI FIFO */
  1306. writel_relaxed(0x2000, priv->io_base + _REG(VENC_VDAC_FIFO_CTRL));
  1307. /* Select ENCI DACs 0, 1, 4, and 5 */
  1308. writel_relaxed(0x11, priv->io_base + _REG(ENCI_DACSEL_0));
  1309. writel_relaxed(0x11, priv->io_base + _REG(ENCI_DACSEL_1));
  1310. /* Interlace video enable */
  1311. writel_relaxed(1, priv->io_base + _REG(ENCI_VIDEO_EN));
  1312. /* Configure Video Saturation / Contrast / Brightness / Hue */
  1313. writel_relaxed(mode->video_saturation,
  1314. priv->io_base + _REG(ENCI_VIDEO_SAT));
  1315. writel_relaxed(mode->video_contrast,
  1316. priv->io_base + _REG(ENCI_VIDEO_CONT));
  1317. writel_relaxed(mode->video_brightness,
  1318. priv->io_base + _REG(ENCI_VIDEO_BRIGHT));
  1319. writel_relaxed(mode->video_hue,
  1320. priv->io_base + _REG(ENCI_VIDEO_HUE));
  1321. /* Enable DAC0 Filter */
  1322. writel_relaxed(0x1, priv->io_base + _REG(VENC_VDAC_DAC0_FILT_CTRL0));
  1323. writel_relaxed(0xfc48, priv->io_base + _REG(VENC_VDAC_DAC0_FILT_CTRL1));
  1324. /* 0 in Macrovision register 0 */
  1325. writel_relaxed(0, priv->io_base + _REG(ENCI_MACV_N0));
  1326. /* Analog Synchronization and color burst value adjust */
  1327. writel_relaxed(mode->analog_sync_adj,
  1328. priv->io_base + _REG(ENCI_SYNC_ADJ));
  1329. priv->venc.current_mode = mode->mode_tag;
  1330. }
  1331. /* Returns the current ENCI field polarity */
  1332. unsigned int meson_venci_get_field(struct meson_drm *priv)
  1333. {
  1334. return readl_relaxed(priv->io_base + _REG(ENCI_INFO_READ)) & BIT(29);
  1335. }
  1336. void meson_venc_enable_vsync(struct meson_drm *priv)
  1337. {
  1338. writel_relaxed(2, priv->io_base + _REG(VENC_INTCTRL));
  1339. }
  1340. void meson_venc_disable_vsync(struct meson_drm *priv)
  1341. {
  1342. writel_relaxed(0, priv->io_base + _REG(VENC_INTCTRL));
  1343. }
  1344. void meson_venc_init(struct meson_drm *priv)
  1345. {
  1346. /* Disable CVBS VDAC */
  1347. regmap_write(priv->hhi, HHI_VDAC_CNTL0, 0);
  1348. regmap_write(priv->hhi, HHI_VDAC_CNTL1, 8);
  1349. /* Power Down Dacs */
  1350. writel_relaxed(0xff, priv->io_base + _REG(VENC_VDAC_SETTING));
  1351. /* Disable HDMI PHY */
  1352. regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL0, 0);
  1353. /* Disable HDMI */
  1354. writel_bits_relaxed(0x3, 0,
  1355. priv->io_base + _REG(VPU_HDMI_SETTING));
  1356. /* Disable all encoders */
  1357. writel_relaxed(0, priv->io_base + _REG(ENCI_VIDEO_EN));
  1358. writel_relaxed(0, priv->io_base + _REG(ENCP_VIDEO_EN));
  1359. writel_relaxed(0, priv->io_base + _REG(ENCL_VIDEO_EN));
  1360. /* Disable VSync IRQ */
  1361. meson_venc_disable_vsync(priv);
  1362. priv->venc.current_mode = MESON_VENC_MODE_NONE;
  1363. }