dp_panel.c 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2012-2019, The Linux Foundation. All rights reserved.
  4. */
  5. #define pr_fmt(fmt) "[drm-dp] %s: " fmt, __func__
  6. #include "dp_panel.h"
  7. #include <drm/drm_fixed.h>
  8. #define DP_KHZ_TO_HZ 1000
  9. #define DP_PANEL_DEFAULT_BPP 24
  10. #define DP_MAX_DS_PORT_COUNT 1
  11. #define DPRX_FEATURE_ENUMERATION_LIST 0x2210
  12. #define DPRX_EXTENDED_DPCD_FIELD 0x2200
  13. #define VSC_SDP_EXTENSION_FOR_COLORIMETRY_SUPPORTED BIT(3)
  14. #define VSC_EXT_VESA_SDP_SUPPORTED BIT(4)
  15. #define VSC_EXT_VESA_SDP_CHAINING_SUPPORTED BIT(5)
  16. enum dp_panel_hdr_pixel_encoding {
  17. RGB,
  18. YCbCr444,
  19. YCbCr422,
  20. YCbCr420,
  21. YONLY,
  22. RAW,
  23. };
  24. enum dp_panel_hdr_rgb_colorimetry {
  25. sRGB,
  26. RGB_WIDE_GAMUT_FIXED_POINT,
  27. RGB_WIDE_GAMUT_FLOATING_POINT,
  28. ADOBERGB,
  29. DCI_P3,
  30. CUSTOM_COLOR_PROFILE,
  31. ITU_R_BT_2020_RGB,
  32. };
  33. enum dp_panel_hdr_dynamic_range {
  34. VESA,
  35. CEA,
  36. };
  37. enum dp_panel_hdr_content_type {
  38. NOT_DEFINED,
  39. GRAPHICS,
  40. PHOTO,
  41. VIDEO,
  42. GAME,
  43. };
  44. enum dp_panel_hdr_state {
  45. HDR_DISABLED,
  46. HDR_ENABLED,
  47. };
  48. struct dp_panel_private {
  49. struct device *dev;
  50. struct dp_panel dp_panel;
  51. struct dp_aux *aux;
  52. struct dp_link *link;
  53. struct dp_parser *parser;
  54. struct dp_catalog_panel *catalog;
  55. bool custom_edid;
  56. bool custom_dpcd;
  57. bool panel_on;
  58. bool vsc_supported;
  59. bool vscext_supported;
  60. bool vscext_chaining_supported;
  61. enum dp_panel_hdr_state hdr_state;
  62. u8 spd_vendor_name[8];
  63. u8 spd_product_description[16];
  64. u8 major;
  65. u8 minor;
  66. };
  67. static const struct dp_panel_info fail_safe = {
  68. .h_active = 640,
  69. .v_active = 480,
  70. .h_back_porch = 48,
  71. .h_front_porch = 16,
  72. .h_sync_width = 96,
  73. .h_active_low = 0,
  74. .v_back_porch = 33,
  75. .v_front_porch = 10,
  76. .v_sync_width = 2,
  77. .v_active_low = 0,
  78. .h_skew = 0,
  79. .refresh_rate = 60,
  80. .pixel_clk_khz = 25200,
  81. .bpp = 24,
  82. };
  83. /* OEM NAME */
  84. static const u8 vendor_name[8] = {81, 117, 97, 108, 99, 111, 109, 109};
  85. /* MODEL NAME */
  86. static const u8 product_desc[16] = {83, 110, 97, 112, 100, 114, 97, 103,
  87. 111, 110, 0, 0, 0, 0, 0, 0};
  88. struct dp_dhdr_maxpkt_calc_input {
  89. u32 mdp_clk;
  90. u32 lclk;
  91. u32 pclk;
  92. u32 h_active;
  93. u32 nlanes;
  94. s64 mst_target_sc;
  95. bool mst_en;
  96. bool fec_en;
  97. };
  98. struct tu_algo_data {
  99. s64 lclk_fp;
  100. s64 pclk_fp;
  101. s64 lwidth;
  102. s64 lwidth_fp;
  103. s64 hbp_relative_to_pclk;
  104. s64 hbp_relative_to_pclk_fp;
  105. int nlanes;
  106. int bpp;
  107. int pixelEnc;
  108. int dsc_en;
  109. int async_en;
  110. int bpc;
  111. uint delay_start_link_extra_pixclk;
  112. int extra_buffer_margin;
  113. s64 ratio_fp;
  114. s64 original_ratio_fp;
  115. s64 err_fp;
  116. s64 n_err_fp;
  117. s64 n_n_err_fp;
  118. int tu_size;
  119. int tu_size_desired;
  120. int tu_size_minus1;
  121. int valid_boundary_link;
  122. s64 resulting_valid_fp;
  123. s64 total_valid_fp;
  124. s64 effective_valid_fp;
  125. s64 effective_valid_recorded_fp;
  126. int n_tus;
  127. int n_tus_per_lane;
  128. int paired_tus;
  129. int remainder_tus;
  130. int remainder_tus_upper;
  131. int remainder_tus_lower;
  132. int extra_bytes;
  133. int filler_size;
  134. int delay_start_link;
  135. int extra_pclk_cycles;
  136. int extra_pclk_cycles_in_link_clk;
  137. s64 ratio_by_tu_fp;
  138. s64 average_valid2_fp;
  139. int new_valid_boundary_link;
  140. int remainder_symbols_exist;
  141. int n_symbols;
  142. s64 n_remainder_symbols_per_lane_fp;
  143. s64 last_partial_tu_fp;
  144. s64 TU_ratio_err_fp;
  145. int n_tus_incl_last_incomplete_tu;
  146. int extra_pclk_cycles_tmp;
  147. int extra_pclk_cycles_in_link_clk_tmp;
  148. int extra_required_bytes_new_tmp;
  149. int filler_size_tmp;
  150. int lower_filler_size_tmp;
  151. int delay_start_link_tmp;
  152. bool boundary_moderation_en;
  153. int boundary_mod_lower_err;
  154. int upper_boundary_count;
  155. int lower_boundary_count;
  156. int i_upper_boundary_count;
  157. int i_lower_boundary_count;
  158. int valid_lower_boundary_link;
  159. int even_distribution_BF;
  160. int even_distribution_legacy;
  161. int even_distribution;
  162. int min_hblank_violated;
  163. s64 delay_start_time_fp;
  164. s64 hbp_time_fp;
  165. s64 hactive_time_fp;
  166. s64 diff_abs_fp;
  167. s64 ratio;
  168. };
  169. static int _tu_param_compare(s64 a, s64 b)
  170. {
  171. u32 a_int, a_frac, a_sign;
  172. u32 b_int, b_frac, b_sign;
  173. s64 a_temp, b_temp, minus_1;
  174. if (a == b)
  175. return 0;
  176. minus_1 = drm_fixp_from_fraction(-1, 1);
  177. a_int = (a >> 32) & 0x7FFFFFFF;
  178. a_frac = a & 0xFFFFFFFF;
  179. a_sign = (a >> 32) & 0x80000000 ? 1 : 0;
  180. b_int = (b >> 32) & 0x7FFFFFFF;
  181. b_frac = b & 0xFFFFFFFF;
  182. b_sign = (b >> 32) & 0x80000000 ? 1 : 0;
  183. if (a_sign > b_sign)
  184. return 2;
  185. else if (b_sign > a_sign)
  186. return 1;
  187. if (!a_sign && !b_sign) { /* positive */
  188. if (a > b)
  189. return 1;
  190. else
  191. return 2;
  192. } else { /* negative */
  193. a_temp = drm_fixp_mul(a, minus_1);
  194. b_temp = drm_fixp_mul(b, minus_1);
  195. if (a_temp > b_temp)
  196. return 2;
  197. else
  198. return 1;
  199. }
  200. }
  201. static void dp_panel_update_tu_timings(struct dp_tu_calc_input *in,
  202. struct tu_algo_data *tu)
  203. {
  204. int nlanes = in->nlanes;
  205. int dsc_num_slices = in->num_of_dsc_slices;
  206. int dsc_num_bytes = 0;
  207. int numerator;
  208. s64 pclk_dsc_fp;
  209. s64 dwidth_dsc_fp;
  210. s64 hbp_dsc_fp;
  211. s64 overhead_dsc;
  212. int tot_num_eoc_symbols = 0;
  213. int tot_num_hor_bytes = 0;
  214. int tot_num_dummy_bytes = 0;
  215. int dwidth_dsc_bytes = 0;
  216. int eoc_bytes = 0;
  217. s64 temp1_fp, temp2_fp, temp3_fp;
  218. tu->lclk_fp = drm_fixp_from_fraction(in->lclk, 1);
  219. tu->pclk_fp = drm_fixp_from_fraction(in->pclk_khz, 1000);
  220. tu->lwidth = in->hactive;
  221. tu->hbp_relative_to_pclk = in->hporch;
  222. tu->nlanes = in->nlanes;
  223. tu->bpp = in->bpp;
  224. tu->pixelEnc = in->pixel_enc;
  225. tu->dsc_en = in->dsc_en;
  226. tu->async_en = in->async_en;
  227. tu->lwidth_fp = drm_fixp_from_fraction(in->hactive, 1);
  228. tu->hbp_relative_to_pclk_fp = drm_fixp_from_fraction(in->hporch, 1);
  229. if (tu->pixelEnc == 420) {
  230. temp1_fp = drm_fixp_from_fraction(2, 1);
  231. tu->pclk_fp = drm_fixp_div(tu->pclk_fp, temp1_fp);
  232. tu->lwidth_fp = drm_fixp_div(tu->lwidth_fp, temp1_fp);
  233. tu->hbp_relative_to_pclk_fp =
  234. drm_fixp_div(tu->hbp_relative_to_pclk_fp, 2);
  235. }
  236. if (tu->pixelEnc == 422) {
  237. switch (tu->bpp) {
  238. case 24:
  239. tu->bpp = 16;
  240. tu->bpc = 8;
  241. break;
  242. case 30:
  243. tu->bpp = 20;
  244. tu->bpc = 10;
  245. break;
  246. default:
  247. tu->bpp = 16;
  248. tu->bpc = 8;
  249. break;
  250. }
  251. } else
  252. tu->bpc = tu->bpp/3;
  253. if (!in->dsc_en)
  254. goto fec_check;
  255. temp1_fp = drm_fixp_from_fraction(in->compress_ratio, 100);
  256. temp2_fp = drm_fixp_from_fraction(in->bpp, 1);
  257. temp3_fp = drm_fixp_div(temp2_fp, temp1_fp);
  258. temp2_fp = drm_fixp_mul(tu->lwidth_fp, temp3_fp);
  259. temp1_fp = drm_fixp_from_fraction(8, 1);
  260. temp3_fp = drm_fixp_div(temp2_fp, temp1_fp);
  261. numerator = drm_fixp2int(temp3_fp);
  262. dsc_num_bytes = numerator / dsc_num_slices;
  263. eoc_bytes = dsc_num_bytes % nlanes;
  264. tot_num_eoc_symbols = nlanes * dsc_num_slices;
  265. tot_num_hor_bytes = dsc_num_bytes * dsc_num_slices;
  266. tot_num_dummy_bytes = (nlanes - eoc_bytes) * dsc_num_slices;
  267. if (dsc_num_bytes == 0)
  268. pr_info("incorrect no of bytes per slice=%d\n", dsc_num_bytes);
  269. dwidth_dsc_bytes = (tot_num_hor_bytes +
  270. tot_num_eoc_symbols +
  271. (eoc_bytes == 0 ? 0 : tot_num_dummy_bytes));
  272. overhead_dsc = dwidth_dsc_bytes / tot_num_hor_bytes;
  273. dwidth_dsc_fp = drm_fixp_from_fraction(dwidth_dsc_bytes, 3);
  274. temp2_fp = drm_fixp_mul(tu->pclk_fp, dwidth_dsc_fp);
  275. temp1_fp = drm_fixp_div(temp2_fp, tu->lwidth_fp);
  276. pclk_dsc_fp = temp1_fp;
  277. temp1_fp = drm_fixp_div(pclk_dsc_fp, tu->pclk_fp);
  278. temp2_fp = drm_fixp_mul(tu->hbp_relative_to_pclk_fp, temp1_fp);
  279. hbp_dsc_fp = temp2_fp;
  280. /* output */
  281. tu->pclk_fp = pclk_dsc_fp;
  282. tu->lwidth_fp = dwidth_dsc_fp;
  283. tu->hbp_relative_to_pclk_fp = hbp_dsc_fp;
  284. fec_check:
  285. if (in->fec_en) {
  286. temp1_fp = drm_fixp_from_fraction(976, 1000); /* 0.976 */
  287. tu->lclk_fp = drm_fixp_mul(tu->lclk_fp, temp1_fp);
  288. }
  289. }
  290. static void _tu_valid_boundary_calc(struct tu_algo_data *tu)
  291. {
  292. s64 temp1_fp, temp2_fp, temp, temp1, temp2;
  293. int compare_result_1, compare_result_2, compare_result_3;
  294. temp1_fp = drm_fixp_from_fraction(tu->tu_size, 1);
  295. temp2_fp = drm_fixp_mul(tu->ratio_fp, temp1_fp);
  296. tu->new_valid_boundary_link = drm_fixp2int_ceil(temp2_fp);
  297. temp = (tu->i_upper_boundary_count *
  298. tu->new_valid_boundary_link +
  299. tu->i_lower_boundary_count *
  300. (tu->new_valid_boundary_link-1));
  301. tu->average_valid2_fp = drm_fixp_from_fraction(temp,
  302. (tu->i_upper_boundary_count +
  303. tu->i_lower_boundary_count));
  304. temp1_fp = drm_fixp_from_fraction(tu->bpp, 8);
  305. temp2_fp = tu->lwidth_fp;
  306. temp1_fp = drm_fixp_mul(temp2_fp, temp1_fp);
  307. temp2_fp = drm_fixp_div(temp1_fp, tu->average_valid2_fp);
  308. tu->n_tus = drm_fixp2int(temp2_fp);
  309. if ((temp2_fp & 0xFFFFFFFF) > 0xFFFFF000)
  310. tu->n_tus += 1;
  311. temp1_fp = drm_fixp_from_fraction(tu->n_tus, 1);
  312. temp2_fp = drm_fixp_mul(temp1_fp, tu->average_valid2_fp);
  313. temp1_fp = drm_fixp_from_fraction(tu->n_symbols, 1);
  314. temp2_fp = temp1_fp - temp2_fp;
  315. temp1_fp = drm_fixp_from_fraction(tu->nlanes, 1);
  316. temp2_fp = drm_fixp_div(temp2_fp, temp1_fp);
  317. tu->n_remainder_symbols_per_lane_fp = temp2_fp;
  318. temp1_fp = drm_fixp_from_fraction(tu->tu_size, 1);
  319. tu->last_partial_tu_fp =
  320. drm_fixp_div(tu->n_remainder_symbols_per_lane_fp,
  321. temp1_fp);
  322. if (tu->n_remainder_symbols_per_lane_fp != 0)
  323. tu->remainder_symbols_exist = 1;
  324. else
  325. tu->remainder_symbols_exist = 0;
  326. temp1_fp = drm_fixp_from_fraction(tu->n_tus, tu->nlanes);
  327. tu->n_tus_per_lane = drm_fixp2int(temp1_fp);
  328. tu->paired_tus = (int)((tu->n_tus_per_lane) /
  329. (tu->i_upper_boundary_count +
  330. tu->i_lower_boundary_count));
  331. tu->remainder_tus = tu->n_tus_per_lane - tu->paired_tus *
  332. (tu->i_upper_boundary_count +
  333. tu->i_lower_boundary_count);
  334. if ((tu->remainder_tus - tu->i_upper_boundary_count) > 0) {
  335. tu->remainder_tus_upper = tu->i_upper_boundary_count;
  336. tu->remainder_tus_lower = tu->remainder_tus -
  337. tu->i_upper_boundary_count;
  338. } else {
  339. tu->remainder_tus_upper = tu->remainder_tus;
  340. tu->remainder_tus_lower = 0;
  341. }
  342. temp = tu->paired_tus * (tu->i_upper_boundary_count *
  343. tu->new_valid_boundary_link +
  344. tu->i_lower_boundary_count *
  345. (tu->new_valid_boundary_link - 1)) +
  346. (tu->remainder_tus_upper *
  347. tu->new_valid_boundary_link) +
  348. (tu->remainder_tus_lower *
  349. (tu->new_valid_boundary_link - 1));
  350. tu->total_valid_fp = drm_fixp_from_fraction(temp, 1);
  351. if (tu->remainder_symbols_exist) {
  352. temp1_fp = tu->total_valid_fp +
  353. tu->n_remainder_symbols_per_lane_fp;
  354. temp2_fp = drm_fixp_from_fraction(tu->n_tus_per_lane, 1);
  355. temp2_fp = temp2_fp + tu->last_partial_tu_fp;
  356. temp1_fp = drm_fixp_div(temp1_fp, temp2_fp);
  357. } else {
  358. temp2_fp = drm_fixp_from_fraction(tu->n_tus_per_lane, 1);
  359. temp1_fp = drm_fixp_div(tu->total_valid_fp, temp2_fp);
  360. }
  361. tu->effective_valid_fp = temp1_fp;
  362. temp1_fp = drm_fixp_from_fraction(tu->tu_size, 1);
  363. temp2_fp = drm_fixp_mul(tu->ratio_fp, temp1_fp);
  364. tu->n_n_err_fp = tu->effective_valid_fp - temp2_fp;
  365. temp1_fp = drm_fixp_from_fraction(tu->tu_size, 1);
  366. temp2_fp = drm_fixp_mul(tu->ratio_fp, temp1_fp);
  367. tu->n_err_fp = tu->average_valid2_fp - temp2_fp;
  368. tu->even_distribution = tu->n_tus % tu->nlanes == 0 ? 1 : 0;
  369. temp1_fp = drm_fixp_from_fraction(tu->bpp, 8);
  370. temp2_fp = tu->lwidth_fp;
  371. temp1_fp = drm_fixp_mul(temp2_fp, temp1_fp);
  372. temp2_fp = drm_fixp_div(temp1_fp, tu->average_valid2_fp);
  373. if (temp2_fp)
  374. tu->n_tus_incl_last_incomplete_tu = drm_fixp2int_ceil(temp2_fp);
  375. else
  376. tu->n_tus_incl_last_incomplete_tu = 0;
  377. temp1 = 0;
  378. temp1_fp = drm_fixp_from_fraction(tu->tu_size, 1);
  379. temp2_fp = drm_fixp_mul(tu->original_ratio_fp, temp1_fp);
  380. temp1_fp = tu->average_valid2_fp - temp2_fp;
  381. temp2_fp = drm_fixp_from_fraction(tu->n_tus_incl_last_incomplete_tu, 1);
  382. temp1_fp = drm_fixp_mul(temp2_fp, temp1_fp);
  383. if (temp1_fp)
  384. temp1 = drm_fixp2int_ceil(temp1_fp);
  385. temp = tu->i_upper_boundary_count * tu->nlanes;
  386. temp1_fp = drm_fixp_from_fraction(tu->tu_size, 1);
  387. temp2_fp = drm_fixp_mul(tu->original_ratio_fp, temp1_fp);
  388. temp1_fp = drm_fixp_from_fraction(tu->new_valid_boundary_link, 1);
  389. temp2_fp = temp1_fp - temp2_fp;
  390. temp1_fp = drm_fixp_from_fraction(temp, 1);
  391. temp2_fp = drm_fixp_mul(temp1_fp, temp2_fp);
  392. if (temp2_fp)
  393. temp2 = drm_fixp2int_ceil(temp2_fp);
  394. else
  395. temp2 = 0;
  396. tu->extra_required_bytes_new_tmp = (int)(temp1 + temp2);
  397. temp1_fp = drm_fixp_from_fraction(8, tu->bpp);
  398. temp2_fp = drm_fixp_from_fraction(
  399. tu->extra_required_bytes_new_tmp, 1);
  400. temp1_fp = drm_fixp_mul(temp2_fp, temp1_fp);
  401. if (temp1_fp)
  402. tu->extra_pclk_cycles_tmp = drm_fixp2int_ceil(temp1_fp);
  403. else
  404. tu->extra_pclk_cycles_tmp = 0;
  405. temp1_fp = drm_fixp_from_fraction(tu->extra_pclk_cycles_tmp, 1);
  406. temp2_fp = drm_fixp_div(tu->lclk_fp, tu->pclk_fp);
  407. temp1_fp = drm_fixp_mul(temp1_fp, temp2_fp);
  408. if (temp1_fp)
  409. tu->extra_pclk_cycles_in_link_clk_tmp =
  410. drm_fixp2int_ceil(temp1_fp);
  411. else
  412. tu->extra_pclk_cycles_in_link_clk_tmp = 0;
  413. tu->filler_size_tmp = tu->tu_size - tu->new_valid_boundary_link;
  414. tu->lower_filler_size_tmp = tu->filler_size_tmp + 1;
  415. tu->delay_start_link_tmp = tu->extra_pclk_cycles_in_link_clk_tmp +
  416. tu->lower_filler_size_tmp +
  417. tu->extra_buffer_margin;
  418. temp1_fp = drm_fixp_from_fraction(tu->delay_start_link_tmp, 1);
  419. tu->delay_start_time_fp = drm_fixp_div(temp1_fp, tu->lclk_fp);
  420. compare_result_1 = _tu_param_compare(tu->n_n_err_fp, tu->diff_abs_fp);
  421. if (compare_result_1 == 2)
  422. compare_result_1 = 1;
  423. else
  424. compare_result_1 = 0;
  425. compare_result_2 = _tu_param_compare(tu->n_n_err_fp, tu->err_fp);
  426. if (compare_result_2 == 2)
  427. compare_result_2 = 1;
  428. else
  429. compare_result_2 = 0;
  430. compare_result_3 = _tu_param_compare(tu->hbp_time_fp,
  431. tu->delay_start_time_fp);
  432. if (compare_result_3 == 2)
  433. compare_result_3 = 0;
  434. else
  435. compare_result_3 = 1;
  436. if (((tu->even_distribution == 1) ||
  437. ((tu->even_distribution_BF == 0) &&
  438. (tu->even_distribution_legacy == 0))) &&
  439. tu->n_err_fp >= 0 && tu->n_n_err_fp >= 0 &&
  440. compare_result_2 &&
  441. (compare_result_1 || (tu->min_hblank_violated == 1)) &&
  442. (tu->new_valid_boundary_link - 1) > 0 &&
  443. compare_result_3 &&
  444. (tu->delay_start_link_tmp <= 1023)) {
  445. tu->upper_boundary_count = tu->i_upper_boundary_count;
  446. tu->lower_boundary_count = tu->i_lower_boundary_count;
  447. tu->err_fp = tu->n_n_err_fp;
  448. tu->boundary_moderation_en = true;
  449. tu->tu_size_desired = tu->tu_size;
  450. tu->valid_boundary_link = tu->new_valid_boundary_link;
  451. tu->effective_valid_recorded_fp = tu->effective_valid_fp;
  452. tu->even_distribution_BF = 1;
  453. tu->delay_start_link = tu->delay_start_link_tmp;
  454. } else if (tu->boundary_mod_lower_err == 0) {
  455. compare_result_1 = _tu_param_compare(tu->n_n_err_fp,
  456. tu->diff_abs_fp);
  457. if (compare_result_1 == 2)
  458. tu->boundary_mod_lower_err = 1;
  459. }
  460. }
  461. static void _dp_calc_boundary(struct tu_algo_data *tu)
  462. {
  463. s64 temp1_fp = 0, temp2_fp = 0;
  464. do {
  465. tu->err_fp = drm_fixp_from_fraction(1000, 1);
  466. temp1_fp = drm_fixp_div(tu->lclk_fp, tu->pclk_fp);
  467. temp2_fp = drm_fixp_from_fraction(
  468. tu->delay_start_link_extra_pixclk, 1);
  469. temp1_fp = drm_fixp_mul(temp2_fp, temp1_fp);
  470. if (temp1_fp)
  471. tu->extra_buffer_margin =
  472. drm_fixp2int_ceil(temp1_fp);
  473. else
  474. tu->extra_buffer_margin = 0;
  475. temp1_fp = drm_fixp_from_fraction(tu->bpp, 8);
  476. temp1_fp = drm_fixp_mul(tu->lwidth_fp, temp1_fp);
  477. if (temp1_fp)
  478. tu->n_symbols = drm_fixp2int_ceil(temp1_fp);
  479. else
  480. tu->n_symbols = 0;
  481. for (tu->tu_size = 32; tu->tu_size <= 64; tu->tu_size++) {
  482. for (tu->i_upper_boundary_count = 1;
  483. tu->i_upper_boundary_count <= 15;
  484. tu->i_upper_boundary_count++) {
  485. for (tu->i_lower_boundary_count = 1;
  486. tu->i_lower_boundary_count <= 15;
  487. tu->i_lower_boundary_count++) {
  488. _tu_valid_boundary_calc(tu);
  489. }
  490. }
  491. }
  492. tu->delay_start_link_extra_pixclk--;
  493. } while (!tu->boundary_moderation_en &&
  494. tu->boundary_mod_lower_err == 1 &&
  495. tu->delay_start_link_extra_pixclk != 0);
  496. }
  497. static void _dp_calc_extra_bytes(struct tu_algo_data *tu)
  498. {
  499. u64 temp = 0;
  500. s64 temp1_fp = 0, temp2_fp = 0;
  501. temp1_fp = drm_fixp_from_fraction(tu->tu_size_desired, 1);
  502. temp2_fp = drm_fixp_mul(tu->original_ratio_fp, temp1_fp);
  503. temp1_fp = drm_fixp_from_fraction(tu->valid_boundary_link, 1);
  504. temp2_fp = temp1_fp - temp2_fp;
  505. temp1_fp = drm_fixp_from_fraction(tu->n_tus + 1, 1);
  506. temp2_fp = drm_fixp_mul(temp1_fp, temp2_fp);
  507. temp = drm_fixp2int(temp2_fp);
  508. if (temp && temp2_fp)
  509. tu->extra_bytes = drm_fixp2int_ceil(temp2_fp);
  510. else
  511. tu->extra_bytes = 0;
  512. temp1_fp = drm_fixp_from_fraction(tu->extra_bytes, 1);
  513. temp2_fp = drm_fixp_from_fraction(8, tu->bpp);
  514. temp1_fp = drm_fixp_mul(temp1_fp, temp2_fp);
  515. if (temp1_fp)
  516. tu->extra_pclk_cycles = drm_fixp2int_ceil(temp1_fp);
  517. else
  518. tu->extra_pclk_cycles = drm_fixp2int(temp1_fp);
  519. temp1_fp = drm_fixp_div(tu->lclk_fp, tu->pclk_fp);
  520. temp2_fp = drm_fixp_from_fraction(tu->extra_pclk_cycles, 1);
  521. temp1_fp = drm_fixp_mul(temp2_fp, temp1_fp);
  522. if (temp1_fp)
  523. tu->extra_pclk_cycles_in_link_clk = drm_fixp2int_ceil(temp1_fp);
  524. else
  525. tu->extra_pclk_cycles_in_link_clk = drm_fixp2int(temp1_fp);
  526. }
  527. static void _dp_panel_calc_tu(struct dp_tu_calc_input *in,
  528. struct dp_vc_tu_mapping_table *tu_table)
  529. {
  530. struct tu_algo_data tu;
  531. int compare_result_1, compare_result_2;
  532. u64 temp = 0;
  533. s64 temp_fp = 0, temp1_fp = 0, temp2_fp = 0;
  534. s64 LCLK_FAST_SKEW_fp = drm_fixp_from_fraction(6, 10000); /* 0.0006 */
  535. s64 const_p49_fp = drm_fixp_from_fraction(49, 100); /* 0.49 */
  536. s64 const_p56_fp = drm_fixp_from_fraction(56, 100); /* 0.56 */
  537. s64 RATIO_SCALE_fp = drm_fixp_from_fraction(1001, 1000);
  538. u8 DP_BRUTE_FORCE = 1;
  539. s64 BRUTE_FORCE_THRESHOLD_fp = drm_fixp_from_fraction(1, 10); /* 0.1 */
  540. uint EXTRA_PIXCLK_CYCLE_DELAY = 4;
  541. uint HBLANK_MARGIN = 4;
  542. memset(&tu, 0, sizeof(tu));
  543. dp_panel_update_tu_timings(in, &tu);
  544. tu.err_fp = drm_fixp_from_fraction(1000, 1); /* 1000 */
  545. temp1_fp = drm_fixp_from_fraction(4, 1);
  546. temp2_fp = drm_fixp_mul(temp1_fp, tu.lclk_fp);
  547. temp_fp = drm_fixp_div(temp2_fp, tu.pclk_fp);
  548. tu.extra_buffer_margin = drm_fixp2int_ceil(temp_fp);
  549. temp1_fp = drm_fixp_from_fraction(tu.bpp, 8);
  550. temp2_fp = drm_fixp_mul(tu.pclk_fp, temp1_fp);
  551. temp1_fp = drm_fixp_from_fraction(tu.nlanes, 1);
  552. temp2_fp = drm_fixp_div(temp2_fp, temp1_fp);
  553. tu.ratio_fp = drm_fixp_div(temp2_fp, tu.lclk_fp);
  554. tu.original_ratio_fp = tu.ratio_fp;
  555. tu.boundary_moderation_en = false;
  556. tu.upper_boundary_count = 0;
  557. tu.lower_boundary_count = 0;
  558. tu.i_upper_boundary_count = 0;
  559. tu.i_lower_boundary_count = 0;
  560. tu.valid_lower_boundary_link = 0;
  561. tu.even_distribution_BF = 0;
  562. tu.even_distribution_legacy = 0;
  563. tu.even_distribution = 0;
  564. tu.delay_start_time_fp = 0;
  565. tu.err_fp = drm_fixp_from_fraction(1000, 1);
  566. tu.n_err_fp = 0;
  567. tu.n_n_err_fp = 0;
  568. tu.ratio = drm_fixp2int(tu.ratio_fp);
  569. temp1_fp = drm_fixp_from_fraction(tu.nlanes, 1);
  570. temp2_fp = tu.lwidth_fp % temp1_fp;
  571. if (temp2_fp != 0 &&
  572. !tu.ratio && tu.dsc_en == 0) {
  573. tu.ratio_fp = drm_fixp_mul(tu.ratio_fp, RATIO_SCALE_fp);
  574. tu.ratio = drm_fixp2int(tu.ratio_fp);
  575. if (tu.ratio)
  576. tu.ratio_fp = drm_fixp_from_fraction(1, 1);
  577. }
  578. if (tu.ratio > 1)
  579. tu.ratio = 1;
  580. if (tu.ratio == 1)
  581. goto tu_size_calc;
  582. compare_result_1 = _tu_param_compare(tu.ratio_fp, const_p49_fp);
  583. if (!compare_result_1 || compare_result_1 == 1)
  584. compare_result_1 = 1;
  585. else
  586. compare_result_1 = 0;
  587. compare_result_2 = _tu_param_compare(tu.ratio_fp, const_p56_fp);
  588. if (!compare_result_2 || compare_result_2 == 2)
  589. compare_result_2 = 1;
  590. else
  591. compare_result_2 = 0;
  592. if (tu.dsc_en && compare_result_1 && compare_result_2) {
  593. HBLANK_MARGIN += 4;
  594. pr_info("Info: increase HBLANK_MARGIN to %d\n", HBLANK_MARGIN);
  595. }
  596. tu_size_calc:
  597. for (tu.tu_size = 32; tu.tu_size <= 64; tu.tu_size++) {
  598. temp1_fp = drm_fixp_from_fraction(tu.tu_size, 1);
  599. temp2_fp = drm_fixp_mul(tu.ratio_fp, temp1_fp);
  600. temp = drm_fixp2int_ceil(temp2_fp);
  601. temp1_fp = drm_fixp_from_fraction(temp, 1);
  602. tu.n_err_fp = temp1_fp - temp2_fp;
  603. if (tu.n_err_fp < tu.err_fp) {
  604. tu.err_fp = tu.n_err_fp;
  605. tu.tu_size_desired = tu.tu_size;
  606. }
  607. }
  608. tu.tu_size_minus1 = tu.tu_size_desired - 1;
  609. temp1_fp = drm_fixp_from_fraction(tu.tu_size_desired, 1);
  610. temp2_fp = drm_fixp_mul(tu.ratio_fp, temp1_fp);
  611. tu.valid_boundary_link = drm_fixp2int_ceil(temp2_fp);
  612. temp1_fp = drm_fixp_from_fraction(tu.bpp, 8);
  613. temp2_fp = tu.lwidth_fp;
  614. temp2_fp = drm_fixp_mul(temp2_fp, temp1_fp);
  615. temp1_fp = drm_fixp_from_fraction(tu.valid_boundary_link, 1);
  616. temp2_fp = drm_fixp_div(temp2_fp, temp1_fp);
  617. tu.n_tus = drm_fixp2int(temp2_fp);
  618. if ((temp2_fp & 0xFFFFFFFF) > 0xFFFFF000)
  619. tu.n_tus += 1;
  620. tu.even_distribution_legacy = tu.n_tus % tu.nlanes == 0 ? 1 : 0;
  621. pr_info("Info: n_sym = %d, num_of_tus = %d\n",
  622. tu.valid_boundary_link, tu.n_tus);
  623. _dp_calc_extra_bytes(&tu);
  624. tu.filler_size = tu.tu_size_desired - tu.valid_boundary_link;
  625. temp1_fp = drm_fixp_from_fraction(tu.tu_size_desired, 1);
  626. tu.ratio_by_tu_fp = drm_fixp_mul(tu.ratio_fp, temp1_fp);
  627. tu.delay_start_link = tu.extra_pclk_cycles_in_link_clk +
  628. tu.filler_size + tu.extra_buffer_margin;
  629. tu.resulting_valid_fp =
  630. drm_fixp_from_fraction(tu.valid_boundary_link, 1);
  631. temp1_fp = drm_fixp_from_fraction(tu.tu_size_desired, 1);
  632. temp2_fp = drm_fixp_div(tu.resulting_valid_fp, temp1_fp);
  633. tu.TU_ratio_err_fp = temp2_fp - tu.original_ratio_fp;
  634. temp1_fp = drm_fixp_from_fraction(HBLANK_MARGIN, 1);
  635. temp1_fp = tu.hbp_relative_to_pclk_fp - temp1_fp;
  636. tu.hbp_time_fp = drm_fixp_div(temp1_fp, tu.pclk_fp);
  637. temp1_fp = drm_fixp_from_fraction(tu.delay_start_link, 1);
  638. tu.delay_start_time_fp = drm_fixp_div(temp1_fp, tu.lclk_fp);
  639. compare_result_1 = _tu_param_compare(tu.hbp_time_fp,
  640. tu.delay_start_time_fp);
  641. if (compare_result_1 == 2) /* hbp_time_fp < delay_start_time_fp */
  642. tu.min_hblank_violated = 1;
  643. tu.hactive_time_fp = drm_fixp_div(tu.lwidth_fp, tu.pclk_fp);
  644. compare_result_2 = _tu_param_compare(tu.hactive_time_fp,
  645. tu.delay_start_time_fp);
  646. if (compare_result_2 == 2)
  647. tu.min_hblank_violated = 1;
  648. tu.delay_start_time_fp = 0;
  649. /* brute force */
  650. tu.delay_start_link_extra_pixclk = EXTRA_PIXCLK_CYCLE_DELAY;
  651. tu.diff_abs_fp = tu.resulting_valid_fp - tu.ratio_by_tu_fp;
  652. temp = drm_fixp2int(tu.diff_abs_fp);
  653. if (!temp && tu.diff_abs_fp <= 0xffff)
  654. tu.diff_abs_fp = 0;
  655. /* if(diff_abs < 0) diff_abs *= -1 */
  656. if (tu.diff_abs_fp < 0)
  657. tu.diff_abs_fp = drm_fixp_mul(tu.diff_abs_fp, -1);
  658. tu.boundary_mod_lower_err = 0;
  659. if ((tu.diff_abs_fp != 0 &&
  660. ((tu.diff_abs_fp > BRUTE_FORCE_THRESHOLD_fp) ||
  661. (tu.even_distribution_legacy == 0) ||
  662. (DP_BRUTE_FORCE == 1))) ||
  663. (tu.min_hblank_violated == 1)) {
  664. _dp_calc_boundary(&tu);
  665. if (tu.boundary_moderation_en) {
  666. temp1_fp = drm_fixp_from_fraction(
  667. (tu.upper_boundary_count *
  668. tu.valid_boundary_link +
  669. tu.lower_boundary_count *
  670. (tu.valid_boundary_link - 1)), 1);
  671. temp2_fp = drm_fixp_from_fraction(
  672. (tu.upper_boundary_count +
  673. tu.lower_boundary_count), 1);
  674. tu.resulting_valid_fp =
  675. drm_fixp_div(temp1_fp, temp2_fp);
  676. temp1_fp = drm_fixp_from_fraction(
  677. tu.tu_size_desired, 1);
  678. tu.ratio_by_tu_fp =
  679. drm_fixp_mul(tu.original_ratio_fp, temp1_fp);
  680. tu.valid_lower_boundary_link =
  681. tu.valid_boundary_link - 1;
  682. temp1_fp = drm_fixp_from_fraction(tu.bpp, 8);
  683. temp1_fp = drm_fixp_mul(tu.lwidth_fp, temp1_fp);
  684. temp2_fp = drm_fixp_div(temp1_fp,
  685. tu.resulting_valid_fp);
  686. tu.n_tus = drm_fixp2int(temp2_fp);
  687. tu.tu_size_minus1 = tu.tu_size_desired - 1;
  688. tu.even_distribution_BF = 1;
  689. temp1_fp =
  690. drm_fixp_from_fraction(tu.tu_size_desired, 1);
  691. temp2_fp =
  692. drm_fixp_div(tu.resulting_valid_fp, temp1_fp);
  693. tu.TU_ratio_err_fp = temp2_fp - tu.original_ratio_fp;
  694. }
  695. }
  696. temp2_fp = drm_fixp_mul(LCLK_FAST_SKEW_fp, tu.lwidth_fp);
  697. if (temp2_fp)
  698. temp = drm_fixp2int_ceil(temp2_fp);
  699. else
  700. temp = 0;
  701. temp1_fp = drm_fixp_from_fraction(tu.nlanes, 1);
  702. temp2_fp = drm_fixp_mul(tu.original_ratio_fp, temp1_fp);
  703. temp1_fp = drm_fixp_from_fraction(tu.bpp, 8);
  704. temp2_fp = drm_fixp_div(temp1_fp, temp2_fp);
  705. temp1_fp = drm_fixp_from_fraction(temp, 1);
  706. temp2_fp = drm_fixp_mul(temp1_fp, temp2_fp);
  707. temp = drm_fixp2int(temp2_fp);
  708. if (tu.async_en)
  709. tu.delay_start_link += (int)temp;
  710. temp1_fp = drm_fixp_from_fraction(tu.delay_start_link, 1);
  711. tu.delay_start_time_fp = drm_fixp_div(temp1_fp, tu.lclk_fp);
  712. /* OUTPUTS */
  713. tu_table->valid_boundary_link = tu.valid_boundary_link;
  714. tu_table->delay_start_link = tu.delay_start_link;
  715. tu_table->boundary_moderation_en = tu.boundary_moderation_en;
  716. tu_table->valid_lower_boundary_link = tu.valid_lower_boundary_link;
  717. tu_table->upper_boundary_count = tu.upper_boundary_count;
  718. tu_table->lower_boundary_count = tu.lower_boundary_count;
  719. tu_table->tu_size_minus1 = tu.tu_size_minus1;
  720. pr_info("TU: valid_boundary_link: %d\n", tu_table->valid_boundary_link);
  721. pr_info("TU: delay_start_link: %d\n", tu_table->delay_start_link);
  722. pr_info("TU: boundary_moderation_en: %d\n",
  723. tu_table->boundary_moderation_en);
  724. pr_info("TU: valid_lower_boundary_link: %d\n",
  725. tu_table->valid_lower_boundary_link);
  726. pr_info("TU: upper_boundary_count: %d\n",
  727. tu_table->upper_boundary_count);
  728. pr_info("TU: lower_boundary_count: %d\n",
  729. tu_table->lower_boundary_count);
  730. pr_info("TU: tu_size_minus1: %d\n", tu_table->tu_size_minus1);
  731. }
  732. static void dp_panel_calc_tu_parameters(struct dp_panel *dp_panel,
  733. struct dp_vc_tu_mapping_table *tu_table)
  734. {
  735. struct dp_tu_calc_input in;
  736. struct dp_panel_info *pinfo;
  737. struct dp_panel_private *panel;
  738. int bw_code;
  739. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  740. pinfo = &dp_panel->pinfo;
  741. bw_code = panel->link->link_params.bw_code;
  742. in.lclk = drm_dp_bw_code_to_link_rate(bw_code) / 1000;
  743. in.pclk_khz = pinfo->pixel_clk_khz;
  744. in.hactive = pinfo->h_active;
  745. in.hporch = pinfo->h_back_porch + pinfo->h_front_porch +
  746. pinfo->h_sync_width;
  747. in.nlanes = panel->link->link_params.lane_count;
  748. in.bpp = pinfo->bpp;
  749. in.pixel_enc = 444;
  750. in.dsc_en = dp_panel->dsc_en;
  751. in.async_en = 0;
  752. in.fec_en = dp_panel->fec_en;
  753. in.num_of_dsc_slices = pinfo->comp_info.dsc_info.slice_per_pkt;
  754. switch (pinfo->comp_info.comp_ratio) {
  755. case MSM_DISPLAY_COMPRESSION_RATIO_2_TO_1:
  756. in.compress_ratio = 200;
  757. break;
  758. case MSM_DISPLAY_COMPRESSION_RATIO_3_TO_1:
  759. in.compress_ratio = 300;
  760. break;
  761. default:
  762. in.compress_ratio = 100;
  763. }
  764. _dp_panel_calc_tu(&in, tu_table);
  765. }
  766. void dp_panel_calc_tu_test(struct dp_tu_calc_input *in,
  767. struct dp_vc_tu_mapping_table *tu_table)
  768. {
  769. _dp_panel_calc_tu(in, tu_table);
  770. }
  771. static void dp_panel_config_tr_unit(struct dp_panel *dp_panel)
  772. {
  773. struct dp_panel_private *panel;
  774. struct dp_catalog_panel *catalog;
  775. u32 dp_tu = 0x0;
  776. u32 valid_boundary = 0x0;
  777. u32 valid_boundary2 = 0x0;
  778. struct dp_vc_tu_mapping_table tu_calc_table;
  779. if (!dp_panel) {
  780. pr_err("invalid input\n");
  781. return;
  782. }
  783. if (dp_panel->stream_id != DP_STREAM_0)
  784. return;
  785. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  786. catalog = panel->catalog;
  787. dp_panel_calc_tu_parameters(dp_panel, &tu_calc_table);
  788. dp_tu |= tu_calc_table.tu_size_minus1;
  789. valid_boundary |= tu_calc_table.valid_boundary_link;
  790. valid_boundary |= (tu_calc_table.delay_start_link << 16);
  791. valid_boundary2 |= (tu_calc_table.valid_lower_boundary_link << 1);
  792. valid_boundary2 |= (tu_calc_table.upper_boundary_count << 16);
  793. valid_boundary2 |= (tu_calc_table.lower_boundary_count << 20);
  794. if (tu_calc_table.boundary_moderation_en)
  795. valid_boundary2 |= BIT(0);
  796. pr_debug("dp_tu=0x%x, valid_boundary=0x%x, valid_boundary2=0x%x\n",
  797. dp_tu, valid_boundary, valid_boundary2);
  798. catalog->dp_tu = dp_tu;
  799. catalog->valid_boundary = valid_boundary;
  800. catalog->valid_boundary2 = valid_boundary2;
  801. catalog->update_transfer_unit(catalog);
  802. }
  803. enum dp_dsc_ratio_type {
  804. DSC_8BPC_8BPP,
  805. DSC_10BPC_8BPP,
  806. DSC_12BPC_8BPP,
  807. DSC_10BPC_10BPP,
  808. DSC_RATIO_TYPE_MAX
  809. };
  810. static u32 dp_dsc_rc_buf_thresh[] = {0x0e, 0x1c, 0x2a, 0x38, 0x46, 0x54,
  811. 0x62, 0x69, 0x70, 0x77, 0x79, 0x7b, 0x7d, 0x7e};
  812. /*
  813. * DSC 1.1
  814. * Rate control - Min QP values for each ratio type in dp_dsc_ratio_type
  815. */
  816. static char dp_dsc_rc_range_min_qp_1_1[][15] = {
  817. {0, 0, 1, 1, 3, 3, 3, 3, 3, 3, 5, 5, 5, 7, 13},
  818. {0, 4, 5, 5, 7, 7, 7, 7, 7, 7, 9, 9, 9, 11, 17},
  819. {0, 4, 9, 9, 11, 11, 11, 11, 11, 11, 13, 13, 13, 15, 21},
  820. {0, 4, 5, 6, 7, 7, 7, 7, 7, 7, 9, 9, 9, 11, 15},
  821. };
  822. /*
  823. * DSC 1.1 SCR
  824. * Rate control - Min QP values for each ratio type in dp_dsc_ratio_type
  825. */
  826. static char dp_dsc_rc_range_min_qp_1_1_scr1[][15] = {
  827. {0, 0, 1, 1, 3, 3, 3, 3, 3, 3, 5, 5, 5, 9, 12},
  828. {0, 4, 5, 5, 7, 7, 7, 7, 7, 7, 9, 9, 9, 13, 16},
  829. {0, 4, 9, 9, 11, 11, 11, 11, 11, 11, 13, 13, 13, 17, 20},
  830. {0, 4, 5, 6, 7, 7, 7, 7, 7, 7, 9, 9, 9, 11, 15},
  831. };
  832. /*
  833. * DSC 1.1
  834. * Rate control - Max QP values for each ratio type in dp_dsc_ratio_type
  835. */
  836. static char dp_dsc_rc_range_max_qp_1_1[][15] = {
  837. {4, 4, 5, 6, 7, 7, 7, 8, 9, 10, 11, 12, 13, 13, 15},
  838. {8, 8, 9, 10, 11, 11, 11, 12, 13, 14, 15, 16, 17, 17, 19},
  839. {12, 12, 13, 14, 15, 15, 15, 16, 17, 18, 19, 20, 21, 21, 23},
  840. {7, 8, 9, 10, 11, 11, 11, 12, 13, 13, 14, 14, 15, 15, 16},
  841. };
  842. /*
  843. * DSC 1.1 SCR
  844. * Rate control - Max QP values for each ratio type in dp_dsc_ratio_type
  845. */
  846. static char dp_dsc_rc_range_max_qp_1_1_scr1[][15] = {
  847. {4, 4, 5, 6, 7, 7, 7, 8, 9, 10, 10, 11, 11, 12, 13},
  848. {8, 8, 9, 10, 11, 11, 11, 12, 13, 14, 14, 15, 15, 16, 17},
  849. {12, 12, 13, 14, 15, 15, 15, 16, 17, 18, 18, 19, 19, 20, 21},
  850. {7, 8, 9, 10, 11, 11, 11, 12, 13, 13, 14, 14, 15, 15, 16},
  851. };
  852. /*
  853. * DSC 1.1 and DSC 1.1 SCR
  854. * Rate control - bpg offset values
  855. */
  856. static char dp_dsc_rc_range_bpg_offset[] = {2, 0, 0, -2, -4, -6, -8, -8,
  857. -8, -10, -10, -12, -12, -12, -12};
  858. struct dp_dsc_dto_data {
  859. enum msm_display_compression_ratio comp_ratio;
  860. u32 org_bpp; /* bits */
  861. u32 dto_numerator;
  862. u32 dto_denominator;
  863. };
  864. struct dp_dsc_dto_data dto_tbl[] = {
  865. {MSM_DISPLAY_COMPRESSION_RATIO_2_TO_1, 24, 1, 2},
  866. {MSM_DISPLAY_COMPRESSION_RATIO_2_TO_1, 30, 5, 8},
  867. {MSM_DISPLAY_COMPRESSION_RATIO_3_TO_1, 24, 1, 3},
  868. {MSM_DISPLAY_COMPRESSION_RATIO_3_TO_1, 30, 5, 12},
  869. };
  870. static void _dp_panel_get_dto_m_n(enum msm_display_compression_ratio ratio,
  871. u32 org_bpp, u32 *dto_n, u32 *dto_d)
  872. {
  873. u32 idx;
  874. for (idx = 0; idx < ARRAY_SIZE(dto_tbl); idx++) {
  875. if (ratio == dto_tbl[idx].comp_ratio &&
  876. org_bpp == dto_tbl[idx].org_bpp) {
  877. *dto_n = dto_tbl[idx].dto_numerator;
  878. *dto_d = dto_tbl[idx].dto_denominator;
  879. return;
  880. }
  881. }
  882. }
  883. static int dp_panel_dsc_create_pps_buf_cmd(struct msm_display_dsc_info *dsc,
  884. char *buf, int pps_id)
  885. {
  886. char *bp = buf;
  887. char data;
  888. int i, bpp;
  889. *bp++ = (dsc->version & 0xff); /* pps0 */
  890. *bp++ = (pps_id & 0xff); /* pps1 */
  891. bp++; /* pps2, reserved */
  892. data = dsc->line_buf_depth & 0x0f;
  893. data |= ((dsc->bpc & 0xf) << 4);
  894. *bp++ = data; /* pps3 */
  895. bpp = dsc->bpp;
  896. bpp <<= 4; /* 4 fraction bits */
  897. data = (bpp >> 8);
  898. data &= 0x03; /* upper two bits */
  899. data |= ((dsc->block_pred_enable & 0x1) << 5);
  900. data |= ((dsc->convert_rgb & 0x1) << 4);
  901. data |= ((dsc->enable_422 & 0x1) << 3);
  902. data |= ((dsc->vbr_enable & 0x1) << 2);
  903. *bp++ = data; /* pps4 */
  904. *bp++ = (bpp & 0xff); /* pps5 */
  905. *bp++ = ((dsc->pic_height >> 8) & 0xff); /* pps6 */
  906. *bp++ = (dsc->pic_height & 0x0ff); /* pps7 */
  907. *bp++ = ((dsc->pic_width >> 8) & 0xff); /* pps8 */
  908. *bp++ = (dsc->pic_width & 0x0ff); /* pps9 */
  909. *bp++ = ((dsc->slice_height >> 8) & 0xff);/* pps10 */
  910. *bp++ = (dsc->slice_height & 0x0ff); /* pps11 */
  911. *bp++ = ((dsc->slice_width >> 8) & 0xff); /* pps12 */
  912. *bp++ = (dsc->slice_width & 0x0ff); /* pps13 */
  913. *bp++ = ((dsc->chunk_size >> 8) & 0xff);/* pps14 */
  914. *bp++ = (dsc->chunk_size & 0x0ff); /* pps15 */
  915. *bp++ = (dsc->initial_xmit_delay >> 8) & 0x3; /* pps16*/
  916. *bp++ = (dsc->initial_xmit_delay & 0xff);/* pps17 */
  917. *bp++ = ((dsc->initial_dec_delay >> 8) & 0xff); /* pps18 */
  918. *bp++ = (dsc->initial_dec_delay & 0xff);/* pps19 */
  919. bp++; /* pps20, reserved */
  920. *bp++ = (dsc->initial_scale_value & 0x3f); /* pps21 */
  921. *bp++ = ((dsc->scale_increment_interval >> 8) & 0xff); /* pps22 */
  922. *bp++ = (dsc->scale_increment_interval & 0xff); /* pps23 */
  923. *bp++ = ((dsc->scale_decrement_interval >> 8) & 0xf); /* pps24 */
  924. *bp++ = (dsc->scale_decrement_interval & 0x0ff);/* pps25 */
  925. bp++; /* pps26, reserved */
  926. *bp++ = (dsc->first_line_bpg_offset & 0x1f);/* pps27 */
  927. *bp++ = ((dsc->nfl_bpg_offset >> 8) & 0xff);/* pps28 */
  928. *bp++ = (dsc->nfl_bpg_offset & 0x0ff); /* pps29 */
  929. *bp++ = ((dsc->slice_bpg_offset >> 8) & 0xff);/* pps30 */
  930. *bp++ = (dsc->slice_bpg_offset & 0x0ff);/* pps31 */
  931. *bp++ = ((dsc->initial_offset >> 8) & 0xff);/* pps32 */
  932. *bp++ = (dsc->initial_offset & 0x0ff); /* pps33 */
  933. *bp++ = ((dsc->final_offset >> 8) & 0xff);/* pps34 */
  934. *bp++ = (dsc->final_offset & 0x0ff); /* pps35 */
  935. *bp++ = (dsc->min_qp_flatness & 0x1f); /* pps36 */
  936. *bp++ = (dsc->max_qp_flatness & 0x1f); /* pps37 */
  937. *bp++ = ((dsc->rc_model_size >> 8) & 0xff);/* pps38 */
  938. *bp++ = (dsc->rc_model_size & 0x0ff); /* pps39 */
  939. *bp++ = (dsc->edge_factor & 0x0f); /* pps40 */
  940. *bp++ = (dsc->quant_incr_limit0 & 0x1f); /* pps41 */
  941. *bp++ = (dsc->quant_incr_limit1 & 0x1f); /* pps42 */
  942. data = ((dsc->tgt_offset_hi & 0xf) << 4);
  943. data |= (dsc->tgt_offset_lo & 0x0f);
  944. *bp++ = data; /* pps43 */
  945. for (i = 0; i < ARRAY_SIZE(dp_dsc_rc_buf_thresh); i++)
  946. *bp++ = (dsc->buf_thresh[i] & 0xff); /* pps44 - pps57 */
  947. for (i = 0; i < 15; i++) { /* pps58 - pps87 */
  948. data = (dsc->range_min_qp[i] & 0x1f);
  949. data <<= 3;
  950. data |= ((dsc->range_max_qp[i] >> 2) & 0x07);
  951. *bp++ = data;
  952. data = (dsc->range_max_qp[i] & 0x03);
  953. data <<= 6;
  954. data |= (dsc->range_bpg_offset[i] & 0x3f);
  955. *bp++ = data;
  956. }
  957. return 88;
  958. }
  959. static void dp_panel_dsc_prepare_pps_packet(struct dp_panel *dp_panel)
  960. {
  961. struct dp_panel_private *panel;
  962. struct dp_dsc_cfg_data *dsc;
  963. u8 *pps, *parity;
  964. u32 *pps_word, *parity_word;
  965. int i, index_4;
  966. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  967. dsc = &panel->catalog->dsc;
  968. pps = dsc->pps;
  969. pps_word = dsc->pps_word;
  970. parity = dsc->parity;
  971. parity_word = dsc->parity_word;
  972. memset(parity, 0, sizeof(dsc->parity));
  973. dsc->pps_word_len = dsc->pps_len >> 2;
  974. dsc->parity_len = dsc->pps_word_len;
  975. dsc->parity_word_len = (dsc->parity_len >> 2) + 1;
  976. for (i = 0; i < dsc->pps_word_len; i++) {
  977. index_4 = i << 2;
  978. pps_word[i] = pps[index_4 + 0] << 0 |
  979. pps[index_4 + 1] << 8 |
  980. pps[index_4 + 2] << 16 |
  981. pps[index_4 + 3] << 24;
  982. parity[i] = dp_header_get_parity(pps_word[i]);
  983. }
  984. for (i = 0; i < dsc->parity_word_len; i++) {
  985. index_4 = i << 2;
  986. parity_word[i] = parity[index_4 + 0] << 0 |
  987. parity[index_4 + 1] << 8 |
  988. parity[index_4 + 2] << 16 |
  989. parity[index_4 + 3] << 24;
  990. }
  991. }
  992. static void _dp_panel_dsc_get_num_extra_pclk(struct msm_display_dsc_info *dsc,
  993. enum msm_display_compression_ratio ratio)
  994. {
  995. unsigned int dto_n = 0, dto_d = 0, remainder;
  996. int ack_required, last_few_ack_required, accum_ack;
  997. int last_few_pclk, last_few_pclk_required;
  998. int start, temp, line_width = dsc->pic_width/2;
  999. s64 temp1_fp, temp2_fp;
  1000. _dp_panel_get_dto_m_n(ratio, dsc->bpc * 3, &dto_n, &dto_d);
  1001. ack_required = dsc->pclk_per_line;
  1002. /* number of pclk cycles left outside of the complete DTO set */
  1003. last_few_pclk = line_width % dto_d;
  1004. /* number of pclk cycles outside of the complete dto */
  1005. temp1_fp = drm_fixp_from_fraction(line_width, dto_d);
  1006. temp2_fp = drm_fixp_from_fraction(dto_n, 1);
  1007. temp1_fp = drm_fixp_mul(temp1_fp, temp2_fp);
  1008. temp = drm_fixp2int(temp1_fp);
  1009. last_few_ack_required = ack_required - temp;
  1010. /*
  1011. * check how many more pclk is needed to
  1012. * accommodate the last few ack required
  1013. */
  1014. remainder = dto_n;
  1015. accum_ack = 0;
  1016. last_few_pclk_required = 0;
  1017. while (accum_ack < last_few_ack_required) {
  1018. last_few_pclk_required++;
  1019. if (remainder >= dto_n)
  1020. start = remainder;
  1021. else
  1022. start = remainder + dto_d;
  1023. remainder = start - dto_n;
  1024. if (remainder < dto_n)
  1025. accum_ack++;
  1026. }
  1027. /* if fewer pclk than required */
  1028. if (last_few_pclk < last_few_pclk_required)
  1029. dsc->extra_width = last_few_pclk_required - last_few_pclk;
  1030. else
  1031. dsc->extra_width = 0;
  1032. pr_debug("extra pclks required: %d\n", dsc->extra_width);
  1033. }
  1034. static void _dp_panel_dsc_bw_overhead_calc(struct dp_panel *dp_panel,
  1035. struct msm_display_dsc_info *dsc,
  1036. struct dp_display_mode *dp_mode, u32 dsc_byte_cnt)
  1037. {
  1038. int num_slices, tot_num_eoc_symbols;
  1039. int tot_num_hor_bytes, tot_num_dummy_bytes;
  1040. int dwidth_dsc_bytes, eoc_bytes;
  1041. u32 num_lanes;
  1042. num_lanes = dp_panel->link_info.num_lanes;
  1043. num_slices = dsc->slice_per_pkt;
  1044. eoc_bytes = dsc_byte_cnt % num_lanes;
  1045. tot_num_eoc_symbols = num_lanes * num_slices;
  1046. tot_num_hor_bytes = dsc_byte_cnt * num_slices;
  1047. tot_num_dummy_bytes = (num_lanes - eoc_bytes) * num_slices;
  1048. if (!eoc_bytes)
  1049. tot_num_dummy_bytes = 0;
  1050. dwidth_dsc_bytes = tot_num_hor_bytes + tot_num_eoc_symbols +
  1051. tot_num_dummy_bytes;
  1052. pr_debug("dwidth_dsc_bytes:%d, tot_num_hor_bytes:%d\n",
  1053. dwidth_dsc_bytes, tot_num_hor_bytes);
  1054. dp_mode->dsc_overhead_fp = drm_fixp_from_fraction(dwidth_dsc_bytes,
  1055. tot_num_hor_bytes);
  1056. dp_mode->timing.dsc_overhead_fp = dp_mode->dsc_overhead_fp;
  1057. }
  1058. static void dp_panel_dsc_pclk_param_calc(struct dp_panel *dp_panel,
  1059. struct msm_display_dsc_info *dsc,
  1060. enum msm_display_compression_ratio ratio,
  1061. struct dp_display_mode *dp_mode)
  1062. {
  1063. int slice_per_pkt, slice_per_intf, intf_width;
  1064. int bytes_in_slice, total_bytes_per_intf;
  1065. int comp_ratio;
  1066. s64 temp1_fp, temp2_fp;
  1067. s64 numerator_fp, denominator_fp;
  1068. s64 dsc_byte_count_fp;
  1069. u32 dsc_byte_count, temp1, temp2;
  1070. intf_width = dp_mode->timing.h_active;
  1071. if (!dsc || !dsc->slice_width || !dsc->slice_per_pkt ||
  1072. (intf_width < dsc->slice_width))
  1073. return;
  1074. slice_per_pkt = dsc->slice_per_pkt;
  1075. slice_per_intf = DIV_ROUND_UP(intf_width, dsc->slice_width);
  1076. if (slice_per_pkt > slice_per_intf)
  1077. slice_per_pkt = 1;
  1078. bytes_in_slice = DIV_ROUND_UP(dsc->slice_width * dsc->bpp, 8);
  1079. total_bytes_per_intf = bytes_in_slice * slice_per_intf;
  1080. dsc->bytes_in_slice = bytes_in_slice;
  1081. dsc->bytes_per_pkt = bytes_in_slice * slice_per_pkt;
  1082. dsc->pkt_per_line = slice_per_intf / slice_per_pkt;
  1083. switch (ratio) {
  1084. case MSM_DISPLAY_COMPRESSION_RATIO_2_TO_1:
  1085. comp_ratio = 200;
  1086. break;
  1087. case MSM_DISPLAY_COMPRESSION_RATIO_3_TO_1:
  1088. comp_ratio = 300;
  1089. break;
  1090. default:
  1091. comp_ratio = 100;
  1092. break;
  1093. }
  1094. temp1_fp = drm_fixp_from_fraction(comp_ratio, 100);
  1095. temp2_fp = drm_fixp_from_fraction(slice_per_pkt * 8, 1);
  1096. denominator_fp = drm_fixp_mul(temp1_fp, temp2_fp);
  1097. numerator_fp = drm_fixp_from_fraction(intf_width * dsc->bpc * 3, 1);
  1098. dsc_byte_count_fp = drm_fixp_div(numerator_fp, denominator_fp);
  1099. dsc_byte_count = drm_fixp2int_ceil(dsc_byte_count_fp);
  1100. temp1 = dsc_byte_count * slice_per_intf;
  1101. temp2 = temp1;
  1102. if (temp1 % 3 != 0)
  1103. temp1 += 3 - (temp1 % 3);
  1104. dsc->eol_byte_num = temp1 - temp2;
  1105. temp1_fp = drm_fixp_from_fraction(slice_per_intf, 6);
  1106. temp2_fp = drm_fixp_mul(dsc_byte_count_fp, temp1_fp);
  1107. dsc->pclk_per_line = drm_fixp2int_ceil(temp2_fp);
  1108. _dp_panel_dsc_get_num_extra_pclk(dsc, ratio);
  1109. dsc->pclk_per_line--;
  1110. _dp_panel_dsc_bw_overhead_calc(dp_panel, dsc, dp_mode, dsc_byte_count);
  1111. }
  1112. static void dp_panel_dsc_populate_static_params(
  1113. struct msm_display_dsc_info *dsc, struct dp_panel *panel)
  1114. {
  1115. int bpp, bpc;
  1116. int mux_words_size;
  1117. int groups_per_line, groups_total;
  1118. int min_rate_buffer_size;
  1119. int hrd_delay;
  1120. int pre_num_extra_mux_bits, num_extra_mux_bits;
  1121. int slice_bits;
  1122. int data;
  1123. int final_value, final_scale;
  1124. int ratio_index, mod_offset;
  1125. int line_buf_depth_raw, line_buf_depth;
  1126. dsc->version = 0x11;
  1127. dsc->scr_rev = 0;
  1128. dsc->rc_model_size = 8192;
  1129. if (dsc->version == 0x11 && dsc->scr_rev == 0x1)
  1130. dsc->first_line_bpg_offset = 15;
  1131. else
  1132. dsc->first_line_bpg_offset = 12;
  1133. dsc->edge_factor = 6;
  1134. dsc->tgt_offset_hi = 3;
  1135. dsc->tgt_offset_lo = 3;
  1136. dsc->enable_422 = 0;
  1137. dsc->convert_rgb = 1;
  1138. dsc->vbr_enable = 0;
  1139. dsc->buf_thresh = dp_dsc_rc_buf_thresh;
  1140. bpp = dsc->bpp;
  1141. bpc = dsc->bpc;
  1142. if (bpc == 12 && bpp == 8)
  1143. ratio_index = DSC_12BPC_8BPP;
  1144. else if (bpc == 10 && bpp == 8)
  1145. ratio_index = DSC_10BPC_8BPP;
  1146. else if (bpc == 10 && bpp == 10)
  1147. ratio_index = DSC_10BPC_10BPP;
  1148. else
  1149. ratio_index = DSC_8BPC_8BPP;
  1150. if (dsc->version == 0x11 && dsc->scr_rev == 0x1) {
  1151. dsc->range_min_qp =
  1152. dp_dsc_rc_range_min_qp_1_1_scr1[ratio_index];
  1153. dsc->range_max_qp =
  1154. dp_dsc_rc_range_max_qp_1_1_scr1[ratio_index];
  1155. } else {
  1156. dsc->range_min_qp = dp_dsc_rc_range_min_qp_1_1[ratio_index];
  1157. dsc->range_max_qp = dp_dsc_rc_range_max_qp_1_1[ratio_index];
  1158. }
  1159. dsc->range_bpg_offset = dp_dsc_rc_range_bpg_offset;
  1160. if (bpp == 8) {
  1161. dsc->initial_offset = 6144;
  1162. dsc->initial_xmit_delay = 512;
  1163. } else if (bpp == 10) {
  1164. dsc->initial_offset = 5632;
  1165. dsc->initial_xmit_delay = 410;
  1166. } else {
  1167. dsc->initial_offset = 2048;
  1168. dsc->initial_xmit_delay = 341;
  1169. }
  1170. line_buf_depth_raw = panel->dsc_dpcd[5] & 0x0f;
  1171. line_buf_depth = (line_buf_depth_raw == 8) ? 8 :
  1172. (line_buf_depth_raw + 9);
  1173. dsc->line_buf_depth = min(line_buf_depth, dsc->bpc + 1);
  1174. if (bpc == 8) {
  1175. dsc->input_10_bits = 0;
  1176. dsc->min_qp_flatness = 3;
  1177. dsc->max_qp_flatness = 12;
  1178. dsc->quant_incr_limit0 = 11;
  1179. dsc->quant_incr_limit1 = 11;
  1180. mux_words_size = 48;
  1181. } else if (bpc == 10) { /* 10bpc */
  1182. dsc->input_10_bits = 1;
  1183. dsc->min_qp_flatness = 7;
  1184. dsc->max_qp_flatness = 16;
  1185. dsc->quant_incr_limit0 = 15;
  1186. dsc->quant_incr_limit1 = 15;
  1187. mux_words_size = 48;
  1188. } else { /* 12 bpc */
  1189. dsc->input_10_bits = 0;
  1190. dsc->min_qp_flatness = 11;
  1191. dsc->max_qp_flatness = 20;
  1192. dsc->quant_incr_limit0 = 19;
  1193. dsc->quant_incr_limit1 = 19;
  1194. mux_words_size = 64;
  1195. }
  1196. mod_offset = dsc->slice_width % 3;
  1197. switch (mod_offset) {
  1198. case 0:
  1199. dsc->slice_last_group_size = 2;
  1200. break;
  1201. case 1:
  1202. dsc->slice_last_group_size = 0;
  1203. break;
  1204. case 2:
  1205. dsc->slice_last_group_size = 1;
  1206. break;
  1207. default:
  1208. break;
  1209. }
  1210. dsc->det_thresh_flatness = 2 << (bpc - 8);
  1211. groups_per_line = DIV_ROUND_UP(dsc->slice_width, 3);
  1212. dsc->chunk_size = dsc->slice_width * bpp / 8;
  1213. if ((dsc->slice_width * bpp) % 8)
  1214. dsc->chunk_size++;
  1215. /* rbs-min */
  1216. min_rate_buffer_size = dsc->rc_model_size - dsc->initial_offset +
  1217. dsc->initial_xmit_delay * bpp +
  1218. groups_per_line * dsc->first_line_bpg_offset;
  1219. hrd_delay = DIV_ROUND_UP(min_rate_buffer_size, bpp);
  1220. dsc->initial_dec_delay = hrd_delay - dsc->initial_xmit_delay;
  1221. dsc->initial_scale_value = 8 * dsc->rc_model_size /
  1222. (dsc->rc_model_size - dsc->initial_offset);
  1223. slice_bits = 8 * dsc->chunk_size * dsc->slice_height;
  1224. groups_total = groups_per_line * dsc->slice_height;
  1225. data = dsc->first_line_bpg_offset * 2048;
  1226. dsc->nfl_bpg_offset = DIV_ROUND_UP(data, (dsc->slice_height - 1));
  1227. pre_num_extra_mux_bits = 3 * (mux_words_size + (4 * bpc + 4) - 2);
  1228. num_extra_mux_bits = pre_num_extra_mux_bits - (mux_words_size -
  1229. ((slice_bits - pre_num_extra_mux_bits) % mux_words_size));
  1230. data = 2048 * (dsc->rc_model_size - dsc->initial_offset
  1231. + num_extra_mux_bits);
  1232. dsc->slice_bpg_offset = DIV_ROUND_UP(data, groups_total);
  1233. data = dsc->initial_xmit_delay * bpp;
  1234. final_value = dsc->rc_model_size - data + num_extra_mux_bits;
  1235. final_scale = 8 * dsc->rc_model_size /
  1236. (dsc->rc_model_size - final_value);
  1237. dsc->final_offset = final_value;
  1238. data = (final_scale - 9) * (dsc->nfl_bpg_offset +
  1239. dsc->slice_bpg_offset);
  1240. dsc->scale_increment_interval = (2048 * dsc->final_offset) / data;
  1241. dsc->scale_decrement_interval = groups_per_line /
  1242. (dsc->initial_scale_value - 8);
  1243. }
  1244. struct dp_dsc_slices_per_line {
  1245. u32 min_ppr;
  1246. u32 max_ppr;
  1247. u8 num_slices;
  1248. };
  1249. struct dp_dsc_peak_throughput {
  1250. u32 index;
  1251. u32 peak_throughput;
  1252. };
  1253. struct dp_dsc_slice_caps_bit_map {
  1254. u32 num_slices;
  1255. u32 bit_index;
  1256. };
  1257. const struct dp_dsc_slices_per_line slice_per_line_tbl[] = {
  1258. {0, 340, 1 },
  1259. {340, 680, 2 },
  1260. {680, 1360, 4 },
  1261. {1360, 3200, 8 },
  1262. {3200, 4800, 12 },
  1263. {4800, 6400, 16 },
  1264. {6400, 8000, 20 },
  1265. {8000, 9600, 24 }
  1266. };
  1267. const struct dp_dsc_peak_throughput peak_throughput_mode_0_tbl[] = {
  1268. {0, 0},
  1269. {1, 340},
  1270. {2, 400},
  1271. {3, 450},
  1272. {4, 500},
  1273. {5, 550},
  1274. {6, 600},
  1275. {7, 650},
  1276. {8, 700},
  1277. {9, 750},
  1278. {10, 800},
  1279. {11, 850},
  1280. {12, 900},
  1281. {13, 950},
  1282. {14, 1000},
  1283. };
  1284. const struct dp_dsc_slice_caps_bit_map slice_caps_bit_map_tbl[] = {
  1285. {1, 0},
  1286. {2, 1},
  1287. {4, 3},
  1288. {6, 4},
  1289. {8, 5},
  1290. {10, 6},
  1291. {12, 7},
  1292. {16, 0},
  1293. {20, 1},
  1294. {24, 2},
  1295. };
  1296. static bool dp_panel_check_slice_support(u32 num_slices, u32 raw_data_1,
  1297. u32 raw_data_2)
  1298. {
  1299. const struct dp_dsc_slice_caps_bit_map *bcap;
  1300. u32 raw_data;
  1301. int i;
  1302. if (num_slices <= 12)
  1303. raw_data = raw_data_1;
  1304. else
  1305. raw_data = raw_data_2;
  1306. for (i = 0; i < ARRAY_SIZE(slice_caps_bit_map_tbl); i++) {
  1307. bcap = &slice_caps_bit_map_tbl[i];
  1308. if (bcap->num_slices == num_slices) {
  1309. raw_data &= (1 << bcap->bit_index);
  1310. if (raw_data)
  1311. return true;
  1312. else
  1313. return false;
  1314. }
  1315. }
  1316. return false;
  1317. }
  1318. static int dp_panel_dsc_prepare_basic_params(
  1319. struct msm_compression_info *comp_info,
  1320. const struct dp_display_mode *dp_mode,
  1321. struct dp_panel *dp_panel)
  1322. {
  1323. int i;
  1324. const struct dp_dsc_slices_per_line *rec;
  1325. const struct dp_dsc_peak_throughput *tput;
  1326. u32 slice_width;
  1327. u32 ppr = dp_mode->timing.pixel_clk_khz/1000;
  1328. u32 max_slice_width;
  1329. u32 ppr_max_index;
  1330. u32 peak_throughput;
  1331. u32 ppr_per_slice;
  1332. u32 slice_caps_1;
  1333. u32 slice_caps_2;
  1334. comp_info->dsc_info.slice_per_pkt = 0;
  1335. for (i = 0; i < ARRAY_SIZE(slice_per_line_tbl); i++) {
  1336. rec = &slice_per_line_tbl[i];
  1337. if ((ppr > rec->min_ppr) && (ppr <= rec->max_ppr)) {
  1338. comp_info->dsc_info.slice_per_pkt = rec->num_slices;
  1339. i++;
  1340. break;
  1341. }
  1342. }
  1343. if (comp_info->dsc_info.slice_per_pkt == 0)
  1344. return -EINVAL;
  1345. ppr_max_index = dp_panel->dsc_dpcd[11] &= 0xf;
  1346. if (!ppr_max_index || ppr_max_index >= 15) {
  1347. pr_debug("Throughput mode 0 not supported");
  1348. return -EINVAL;
  1349. }
  1350. tput = &peak_throughput_mode_0_tbl[ppr_max_index];
  1351. peak_throughput = tput->peak_throughput;
  1352. max_slice_width = dp_panel->dsc_dpcd[12] * 320;
  1353. slice_width = (dp_mode->timing.h_active /
  1354. comp_info->dsc_info.slice_per_pkt);
  1355. ppr_per_slice = ppr/comp_info->dsc_info.slice_per_pkt;
  1356. slice_caps_1 = dp_panel->dsc_dpcd[4];
  1357. slice_caps_2 = dp_panel->dsc_dpcd[13] & 0x7;
  1358. /*
  1359. * There are 3 conditions to check for sink support:
  1360. * 1. The slice width cannot exceed the maximum.
  1361. * 2. The ppr per slice cannot exceed the maximum.
  1362. * 3. The number of slices must be explicitly supported.
  1363. */
  1364. while (slice_width >= max_slice_width ||
  1365. ppr_per_slice > peak_throughput ||
  1366. !dp_panel_check_slice_support(
  1367. comp_info->dsc_info.slice_per_pkt, slice_caps_1,
  1368. slice_caps_2)) {
  1369. if (i == ARRAY_SIZE(slice_per_line_tbl))
  1370. return -EINVAL;
  1371. rec = &slice_per_line_tbl[i];
  1372. comp_info->dsc_info.slice_per_pkt = rec->num_slices;
  1373. slice_width = (dp_mode->timing.h_active /
  1374. comp_info->dsc_info.slice_per_pkt);
  1375. ppr_per_slice = ppr/comp_info->dsc_info.slice_per_pkt;
  1376. i++;
  1377. }
  1378. comp_info->dsc_info.block_pred_enable =
  1379. dp_panel->sink_dsc_caps.block_pred_en;
  1380. comp_info->dsc_info.vbr_enable = 0;
  1381. comp_info->dsc_info.enable_422 = 0;
  1382. comp_info->dsc_info.convert_rgb = 1;
  1383. comp_info->dsc_info.input_10_bits = 0;
  1384. comp_info->dsc_info.pic_width = dp_mode->timing.h_active;
  1385. comp_info->dsc_info.pic_height = dp_mode->timing.v_active;
  1386. comp_info->dsc_info.slice_width = slice_width;
  1387. if (comp_info->dsc_info.pic_height % 16 == 0)
  1388. comp_info->dsc_info.slice_height = 16;
  1389. else if (comp_info->dsc_info.pic_height % 12 == 0)
  1390. comp_info->dsc_info.slice_height = 12;
  1391. else
  1392. comp_info->dsc_info.slice_height = 15;
  1393. comp_info->dsc_info.bpc = dp_mode->timing.bpp / 3;
  1394. comp_info->dsc_info.bpp = comp_info->dsc_info.bpc;
  1395. comp_info->dsc_info.full_frame_slices =
  1396. DIV_ROUND_UP(dp_mode->timing.h_active, slice_width);
  1397. comp_info->comp_type = MSM_DISPLAY_COMPRESSION_DSC;
  1398. comp_info->comp_ratio = MSM_DISPLAY_COMPRESSION_RATIO_3_TO_1;
  1399. return 0;
  1400. }
  1401. static int dp_panel_read_dpcd(struct dp_panel *dp_panel, bool multi_func)
  1402. {
  1403. int rlen, rc = 0;
  1404. struct dp_panel_private *panel;
  1405. struct drm_dp_link *link_info;
  1406. struct drm_dp_aux *drm_aux;
  1407. u8 *dpcd, rx_feature, temp;
  1408. u32 dfp_count = 0, offset = DP_DPCD_REV;
  1409. if (!dp_panel) {
  1410. pr_err("invalid input\n");
  1411. rc = -EINVAL;
  1412. goto end;
  1413. }
  1414. dpcd = dp_panel->dpcd;
  1415. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  1416. drm_aux = panel->aux->drm_aux;
  1417. link_info = &dp_panel->link_info;
  1418. /* reset vsc data */
  1419. panel->vsc_supported = false;
  1420. panel->vscext_supported = false;
  1421. panel->vscext_chaining_supported = false;
  1422. if (panel->custom_dpcd) {
  1423. pr_debug("skip dpcd read in debug mode\n");
  1424. goto skip_dpcd_read;
  1425. }
  1426. rlen = drm_dp_dpcd_read(drm_aux, DP_TRAINING_AUX_RD_INTERVAL, &temp, 1);
  1427. if (rlen != 1) {
  1428. pr_err("error reading DP_TRAINING_AUX_RD_INTERVAL\n");
  1429. rc = -EINVAL;
  1430. goto end;
  1431. }
  1432. /* check for EXTENDED_RECEIVER_CAPABILITY_FIELD_PRESENT */
  1433. if (temp & BIT(7)) {
  1434. pr_debug("using EXTENDED_RECEIVER_CAPABILITY_FIELD\n");
  1435. offset = DPRX_EXTENDED_DPCD_FIELD;
  1436. }
  1437. rlen = drm_dp_dpcd_read(drm_aux, offset,
  1438. dp_panel->dpcd, (DP_RECEIVER_CAP_SIZE + 1));
  1439. if (rlen < (DP_RECEIVER_CAP_SIZE + 1)) {
  1440. pr_err("dpcd read failed, rlen=%d\n", rlen);
  1441. if (rlen == -ETIMEDOUT)
  1442. rc = rlen;
  1443. else
  1444. rc = -EINVAL;
  1445. goto end;
  1446. }
  1447. print_hex_dump(KERN_DEBUG, "[drm-dp] SINK DPCD: ",
  1448. DUMP_PREFIX_NONE, 8, 1, dp_panel->dpcd, rlen, false);
  1449. rlen = drm_dp_dpcd_read(panel->aux->drm_aux,
  1450. DPRX_FEATURE_ENUMERATION_LIST, &rx_feature, 1);
  1451. if (rlen != 1) {
  1452. pr_debug("failed to read DPRX_FEATURE_ENUMERATION_LIST\n");
  1453. goto skip_dpcd_read;
  1454. }
  1455. panel->vsc_supported = !!(rx_feature &
  1456. VSC_SDP_EXTENSION_FOR_COLORIMETRY_SUPPORTED);
  1457. panel->vscext_supported = !!(rx_feature & VSC_EXT_VESA_SDP_SUPPORTED);
  1458. panel->vscext_chaining_supported = !!(rx_feature &
  1459. VSC_EXT_VESA_SDP_CHAINING_SUPPORTED);
  1460. pr_debug("vsc=%d, vscext=%d, vscext_chaining=%d\n",
  1461. panel->vsc_supported, panel->vscext_supported,
  1462. panel->vscext_chaining_supported);
  1463. skip_dpcd_read:
  1464. link_info->revision = dpcd[DP_DPCD_REV];
  1465. panel->major = (link_info->revision >> 4) & 0x0f;
  1466. panel->minor = link_info->revision & 0x0f;
  1467. /* override link params updated in dp_panel_init_panel_info */
  1468. link_info->rate = min_t(unsigned long, panel->parser->max_lclk_khz,
  1469. drm_dp_bw_code_to_link_rate(dpcd[DP_MAX_LINK_RATE]));
  1470. link_info->num_lanes = dpcd[DP_MAX_LANE_COUNT] & DP_MAX_LANE_COUNT_MASK;
  1471. if (multi_func)
  1472. link_info->num_lanes = min_t(unsigned int,
  1473. link_info->num_lanes, 2);
  1474. pr_debug("version:%d.%d, rate:%d, lanes:%d\n", panel->major,
  1475. panel->minor, link_info->rate, link_info->num_lanes);
  1476. if (drm_dp_enhanced_frame_cap(dpcd))
  1477. link_info->capabilities |= DP_LINK_CAP_ENHANCED_FRAMING;
  1478. dfp_count = dpcd[DP_DOWN_STREAM_PORT_COUNT] &
  1479. DP_DOWN_STREAM_PORT_COUNT;
  1480. if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_PRESENT)
  1481. && (dpcd[DP_DPCD_REV] > 0x10)) {
  1482. rlen = drm_dp_dpcd_read(panel->aux->drm_aux,
  1483. DP_DOWNSTREAM_PORT_0, dp_panel->ds_ports,
  1484. DP_MAX_DOWNSTREAM_PORTS);
  1485. if (rlen < DP_MAX_DOWNSTREAM_PORTS) {
  1486. pr_err("ds port status failed, rlen=%d\n", rlen);
  1487. rc = -EINVAL;
  1488. goto end;
  1489. }
  1490. }
  1491. if (dfp_count > DP_MAX_DS_PORT_COUNT)
  1492. pr_debug("DS port count %d greater that max (%d) supported\n",
  1493. dfp_count, DP_MAX_DS_PORT_COUNT);
  1494. end:
  1495. return rc;
  1496. }
  1497. static int dp_panel_set_default_link_params(struct dp_panel *dp_panel)
  1498. {
  1499. struct drm_dp_link *link_info;
  1500. const int default_bw_code = 162000;
  1501. const int default_num_lanes = 1;
  1502. if (!dp_panel) {
  1503. pr_err("invalid input\n");
  1504. return -EINVAL;
  1505. }
  1506. link_info = &dp_panel->link_info;
  1507. link_info->rate = default_bw_code;
  1508. link_info->num_lanes = default_num_lanes;
  1509. pr_debug("link_rate=%d num_lanes=%d\n",
  1510. link_info->rate, link_info->num_lanes);
  1511. return 0;
  1512. }
  1513. static int dp_panel_set_edid(struct dp_panel *dp_panel, u8 *edid)
  1514. {
  1515. struct dp_panel_private *panel;
  1516. if (!dp_panel) {
  1517. pr_err("invalid input\n");
  1518. return -EINVAL;
  1519. }
  1520. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  1521. if (edid) {
  1522. dp_panel->edid_ctrl->edid = (struct edid *)edid;
  1523. panel->custom_edid = true;
  1524. } else {
  1525. panel->custom_edid = false;
  1526. dp_panel->edid_ctrl->edid = NULL;
  1527. }
  1528. pr_debug("%d\n", panel->custom_edid);
  1529. return 0;
  1530. }
  1531. static int dp_panel_set_dpcd(struct dp_panel *dp_panel, u8 *dpcd)
  1532. {
  1533. struct dp_panel_private *panel;
  1534. u8 *dp_dpcd;
  1535. if (!dp_panel) {
  1536. pr_err("invalid input\n");
  1537. return -EINVAL;
  1538. }
  1539. dp_dpcd = dp_panel->dpcd;
  1540. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  1541. if (dpcd) {
  1542. memcpy(dp_dpcd, dpcd, DP_RECEIVER_CAP_SIZE + 1);
  1543. panel->custom_dpcd = true;
  1544. } else {
  1545. panel->custom_dpcd = false;
  1546. }
  1547. pr_debug("%d\n", panel->custom_dpcd);
  1548. return 0;
  1549. }
  1550. static int dp_panel_read_edid(struct dp_panel *dp_panel,
  1551. struct drm_connector *connector)
  1552. {
  1553. int ret = 0;
  1554. struct dp_panel_private *panel;
  1555. struct edid *edid;
  1556. if (!dp_panel) {
  1557. pr_err("invalid input\n");
  1558. return -EINVAL;
  1559. }
  1560. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  1561. if (panel->custom_edid) {
  1562. pr_debug("skip edid read in debug mode\n");
  1563. goto end;
  1564. }
  1565. sde_get_edid(connector, &panel->aux->drm_aux->ddc,
  1566. (void **)&dp_panel->edid_ctrl);
  1567. if (!dp_panel->edid_ctrl->edid) {
  1568. pr_err("EDID read failed\n");
  1569. ret = -EINVAL;
  1570. goto end;
  1571. }
  1572. end:
  1573. edid = dp_panel->edid_ctrl->edid;
  1574. dp_panel->audio_supported = drm_detect_monitor_audio(edid);
  1575. return ret;
  1576. }
  1577. static void dp_panel_decode_dsc_dpcd(struct dp_panel *dp_panel)
  1578. {
  1579. s64 fec_overhead_fp = drm_fixp_from_fraction(1, 1);
  1580. if (!dp_panel->dsc_feature_enable || !dp_panel->fec_feature_enable) {
  1581. pr_debug("source dsc is not supported\n");
  1582. return;
  1583. }
  1584. if (dp_panel->dsc_dpcd[0] && dp_panel->fec_dpcd) {
  1585. dp_panel->sink_dsc_caps.dsc_capable = true;
  1586. dp_panel->sink_dsc_caps.version = dp_panel->dsc_dpcd[1];
  1587. dp_panel->sink_dsc_caps.block_pred_en =
  1588. dp_panel->dsc_dpcd[6] ? true : false;
  1589. if (dp_panel->sink_dsc_caps.version >= 0x11)
  1590. dp_panel->dsc_en = true;
  1591. } else {
  1592. dp_panel->sink_dsc_caps.dsc_capable = false;
  1593. dp_panel->dsc_en = false;
  1594. }
  1595. dp_panel->fec_en = dp_panel->dsc_en;
  1596. dp_panel->widebus_en = dp_panel->dsc_en;
  1597. /* fec_overhead = 1.00 / 0.97582 */
  1598. if (dp_panel->fec_en)
  1599. fec_overhead_fp = drm_fixp_from_fraction(100000, 97582);
  1600. dp_panel->fec_overhead_fp = fec_overhead_fp;
  1601. }
  1602. static void dp_panel_read_sink_dsc_caps(struct dp_panel *dp_panel)
  1603. {
  1604. int rlen;
  1605. struct dp_panel_private *panel;
  1606. const int fec_cap = 0x90;
  1607. int dpcd_rev;
  1608. if (!dp_panel) {
  1609. pr_err("invalid input\n");
  1610. return;
  1611. }
  1612. dp_panel->dsc_en = false;
  1613. dp_panel->fec_en = false;
  1614. dpcd_rev = dp_panel->dpcd[DP_DPCD_REV];
  1615. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  1616. dp_panel->fec_overhead_fp = 0;
  1617. if (panel->parser->dsc_feature_enable && dpcd_rev >= 0x14) {
  1618. rlen = drm_dp_dpcd_read(panel->aux->drm_aux, DP_DSC_SUPPORT,
  1619. dp_panel->dsc_dpcd, (DP_RECEIVER_DSC_CAP_SIZE + 1));
  1620. if (rlen < (DP_RECEIVER_DSC_CAP_SIZE + 1)) {
  1621. pr_debug("dsc dpcd read failed, rlen=%d\n", rlen);
  1622. return;
  1623. }
  1624. print_hex_dump(KERN_DEBUG, "[drm-dp] SINK DSC DPCD: ",
  1625. DUMP_PREFIX_NONE, 8, 1, dp_panel->dsc_dpcd, rlen,
  1626. false);
  1627. rlen = drm_dp_dpcd_read(panel->aux->drm_aux, fec_cap,
  1628. &dp_panel->fec_dpcd, 1);
  1629. if (rlen < 1) {
  1630. pr_err("fec dpcd read failed, rlen=%d\n", rlen);
  1631. return;
  1632. }
  1633. dp_panel_decode_dsc_dpcd(dp_panel);
  1634. }
  1635. }
  1636. static int dp_panel_read_sink_caps(struct dp_panel *dp_panel,
  1637. struct drm_connector *connector, bool multi_func)
  1638. {
  1639. int rc = 0, rlen, count, downstream_ports;
  1640. const int count_len = 1;
  1641. struct dp_panel_private *panel;
  1642. if (!dp_panel || !connector) {
  1643. pr_err("invalid input\n");
  1644. rc = -EINVAL;
  1645. goto end;
  1646. }
  1647. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  1648. rc = dp_panel_read_dpcd(dp_panel, multi_func);
  1649. if (rc || !is_link_rate_valid(drm_dp_link_rate_to_bw_code(
  1650. dp_panel->link_info.rate)) || !is_lane_count_valid(
  1651. dp_panel->link_info.num_lanes) ||
  1652. ((drm_dp_link_rate_to_bw_code(dp_panel->link_info.rate)) >
  1653. dp_panel->max_bw_code)) {
  1654. if ((rc == -ETIMEDOUT) || (rc == -ENODEV)) {
  1655. pr_err("DPCD read failed, return early\n");
  1656. goto end;
  1657. }
  1658. pr_err("panel dpcd read failed/incorrect, set default params\n");
  1659. dp_panel_set_default_link_params(dp_panel);
  1660. }
  1661. downstream_ports = dp_panel->dpcd[DP_DOWNSTREAMPORT_PRESENT] &
  1662. DP_DWN_STRM_PORT_PRESENT;
  1663. if (downstream_ports) {
  1664. rlen = drm_dp_dpcd_read(panel->aux->drm_aux, DP_SINK_COUNT,
  1665. &count, count_len);
  1666. if (rlen == count_len) {
  1667. count = DP_GET_SINK_COUNT(count);
  1668. if (!count) {
  1669. pr_err("no downstream ports connected\n");
  1670. panel->link->sink_count.count = 0;
  1671. rc = -ENOTCONN;
  1672. goto end;
  1673. }
  1674. }
  1675. }
  1676. rc = dp_panel_read_edid(dp_panel, connector);
  1677. if (rc) {
  1678. pr_err("panel edid read failed, set failsafe mode\n");
  1679. return rc;
  1680. }
  1681. dp_panel->widebus_en = panel->parser->has_widebus;
  1682. dp_panel->dsc_feature_enable = panel->parser->dsc_feature_enable;
  1683. dp_panel->fec_feature_enable = panel->parser->fec_feature_enable;
  1684. dp_panel_read_sink_dsc_caps(dp_panel);
  1685. end:
  1686. return rc;
  1687. }
  1688. static u32 dp_panel_get_supported_bpp(struct dp_panel *dp_panel,
  1689. u32 mode_edid_bpp, u32 mode_pclk_khz)
  1690. {
  1691. struct drm_dp_link *link_info;
  1692. const u32 max_supported_bpp = 30, min_supported_bpp = 18;
  1693. u32 bpp = 0, data_rate_khz = 0;
  1694. bpp = min_t(u32, mode_edid_bpp, max_supported_bpp);
  1695. link_info = &dp_panel->link_info;
  1696. data_rate_khz = link_info->num_lanes * link_info->rate * 8;
  1697. while (bpp > min_supported_bpp) {
  1698. if (mode_pclk_khz * bpp <= data_rate_khz)
  1699. break;
  1700. bpp -= 6;
  1701. }
  1702. return bpp;
  1703. }
  1704. static u32 dp_panel_get_mode_bpp(struct dp_panel *dp_panel,
  1705. u32 mode_edid_bpp, u32 mode_pclk_khz)
  1706. {
  1707. struct dp_panel_private *panel;
  1708. u32 bpp = mode_edid_bpp;
  1709. if (!dp_panel || !mode_edid_bpp || !mode_pclk_khz) {
  1710. pr_err("invalid input\n");
  1711. return 0;
  1712. }
  1713. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  1714. if (dp_panel->video_test)
  1715. bpp = dp_link_bit_depth_to_bpp(
  1716. panel->link->test_video.test_bit_depth);
  1717. else
  1718. bpp = dp_panel_get_supported_bpp(dp_panel, mode_edid_bpp,
  1719. mode_pclk_khz);
  1720. return bpp;
  1721. }
  1722. static void dp_panel_set_test_mode(struct dp_panel_private *panel,
  1723. struct dp_display_mode *mode)
  1724. {
  1725. struct dp_panel_info *pinfo = NULL;
  1726. struct dp_link_test_video *test_info = NULL;
  1727. if (!panel) {
  1728. pr_err("invalid params\n");
  1729. return;
  1730. }
  1731. pinfo = &mode->timing;
  1732. test_info = &panel->link->test_video;
  1733. pinfo->h_active = test_info->test_h_width;
  1734. pinfo->h_sync_width = test_info->test_hsync_width;
  1735. pinfo->h_back_porch = test_info->test_h_start -
  1736. test_info->test_hsync_width;
  1737. pinfo->h_front_porch = test_info->test_h_total -
  1738. (test_info->test_h_start + test_info->test_h_width);
  1739. pinfo->v_active = test_info->test_v_height;
  1740. pinfo->v_sync_width = test_info->test_vsync_width;
  1741. pinfo->v_back_porch = test_info->test_v_start -
  1742. test_info->test_vsync_width;
  1743. pinfo->v_front_porch = test_info->test_v_total -
  1744. (test_info->test_v_start + test_info->test_v_height);
  1745. pinfo->bpp = dp_link_bit_depth_to_bpp(test_info->test_bit_depth);
  1746. pinfo->h_active_low = test_info->test_hsync_pol;
  1747. pinfo->v_active_low = test_info->test_vsync_pol;
  1748. pinfo->refresh_rate = test_info->test_rr_n;
  1749. pinfo->pixel_clk_khz = test_info->test_h_total *
  1750. test_info->test_v_total * pinfo->refresh_rate;
  1751. if (test_info->test_rr_d == 0)
  1752. pinfo->pixel_clk_khz /= 1000;
  1753. else
  1754. pinfo->pixel_clk_khz /= 1001;
  1755. if (test_info->test_h_width == 640)
  1756. pinfo->pixel_clk_khz = 25170;
  1757. }
  1758. static int dp_panel_get_modes(struct dp_panel *dp_panel,
  1759. struct drm_connector *connector, struct dp_display_mode *mode)
  1760. {
  1761. struct dp_panel_private *panel;
  1762. if (!dp_panel) {
  1763. pr_err("invalid input\n");
  1764. return -EINVAL;
  1765. }
  1766. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  1767. if (dp_panel->video_test) {
  1768. dp_panel_set_test_mode(panel, mode);
  1769. return 1;
  1770. } else if (dp_panel->edid_ctrl->edid) {
  1771. return _sde_edid_update_modes(connector, dp_panel->edid_ctrl);
  1772. }
  1773. /* fail-safe mode */
  1774. memcpy(&mode->timing, &fail_safe,
  1775. sizeof(fail_safe));
  1776. return 1;
  1777. }
  1778. static void dp_panel_handle_sink_request(struct dp_panel *dp_panel)
  1779. {
  1780. struct dp_panel_private *panel;
  1781. if (!dp_panel) {
  1782. pr_err("invalid input\n");
  1783. return;
  1784. }
  1785. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  1786. if (panel->link->sink_request & DP_TEST_LINK_EDID_READ) {
  1787. u8 checksum = sde_get_edid_checksum(dp_panel->edid_ctrl);
  1788. panel->link->send_edid_checksum(panel->link, checksum);
  1789. panel->link->send_test_response(panel->link);
  1790. }
  1791. }
  1792. static void dp_panel_tpg_config(struct dp_panel *dp_panel, bool enable)
  1793. {
  1794. u32 hsync_start_x, hsync_end_x;
  1795. struct dp_catalog_panel *catalog;
  1796. struct dp_panel_private *panel;
  1797. struct dp_panel_info *pinfo;
  1798. if (!dp_panel) {
  1799. pr_err("invalid input\n");
  1800. return;
  1801. }
  1802. if (dp_panel->stream_id >= DP_STREAM_MAX) {
  1803. pr_err("invalid stream id:%d\n", dp_panel->stream_id);
  1804. return;
  1805. }
  1806. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  1807. catalog = panel->catalog;
  1808. pinfo = &panel->dp_panel.pinfo;
  1809. if (!panel->panel_on) {
  1810. pr_debug("DP panel not enabled, handle TPG on next panel on\n");
  1811. return;
  1812. }
  1813. if (!enable) {
  1814. panel->catalog->tpg_config(catalog, false);
  1815. return;
  1816. }
  1817. /* TPG config */
  1818. catalog->hsync_period = pinfo->h_sync_width + pinfo->h_back_porch +
  1819. pinfo->h_active + pinfo->h_front_porch;
  1820. catalog->vsync_period = pinfo->v_sync_width + pinfo->v_back_porch +
  1821. pinfo->v_active + pinfo->v_front_porch;
  1822. catalog->display_v_start = ((pinfo->v_sync_width +
  1823. pinfo->v_back_porch) * catalog->hsync_period);
  1824. catalog->display_v_end = ((catalog->vsync_period -
  1825. pinfo->v_front_porch) * catalog->hsync_period) - 1;
  1826. catalog->display_v_start += pinfo->h_sync_width + pinfo->h_back_porch;
  1827. catalog->display_v_end -= pinfo->h_front_porch;
  1828. hsync_start_x = pinfo->h_back_porch + pinfo->h_sync_width;
  1829. hsync_end_x = catalog->hsync_period - pinfo->h_front_porch - 1;
  1830. catalog->v_sync_width = pinfo->v_sync_width;
  1831. catalog->hsync_ctl = (catalog->hsync_period << 16) |
  1832. pinfo->h_sync_width;
  1833. catalog->display_hctl = (hsync_end_x << 16) | hsync_start_x;
  1834. panel->catalog->tpg_config(catalog, true);
  1835. }
  1836. static int dp_panel_config_timing(struct dp_panel *dp_panel)
  1837. {
  1838. int rc = 0;
  1839. u32 data, total_ver, total_hor;
  1840. struct dp_catalog_panel *catalog;
  1841. struct dp_panel_private *panel;
  1842. struct dp_panel_info *pinfo;
  1843. if (!dp_panel) {
  1844. pr_err("invalid input\n");
  1845. rc = -EINVAL;
  1846. goto end;
  1847. }
  1848. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  1849. catalog = panel->catalog;
  1850. pinfo = &panel->dp_panel.pinfo;
  1851. pr_debug("width=%d hporch= %d %d %d\n",
  1852. pinfo->h_active, pinfo->h_back_porch,
  1853. pinfo->h_front_porch, pinfo->h_sync_width);
  1854. pr_debug("height=%d vporch= %d %d %d\n",
  1855. pinfo->v_active, pinfo->v_back_porch,
  1856. pinfo->v_front_porch, pinfo->v_sync_width);
  1857. total_hor = pinfo->h_active + pinfo->h_back_porch +
  1858. pinfo->h_front_porch + pinfo->h_sync_width;
  1859. total_ver = pinfo->v_active + pinfo->v_back_porch +
  1860. pinfo->v_front_porch + pinfo->v_sync_width;
  1861. data = total_ver;
  1862. data <<= 16;
  1863. data |= total_hor;
  1864. catalog->total = data;
  1865. data = (pinfo->v_back_porch + pinfo->v_sync_width);
  1866. data <<= 16;
  1867. data |= (pinfo->h_back_porch + pinfo->h_sync_width);
  1868. catalog->sync_start = data;
  1869. data = pinfo->v_sync_width;
  1870. data <<= 16;
  1871. data |= (pinfo->v_active_low << 31);
  1872. data |= pinfo->h_sync_width;
  1873. data |= (pinfo->h_active_low << 15);
  1874. catalog->width_blanking = data;
  1875. data = pinfo->v_active;
  1876. data <<= 16;
  1877. data |= pinfo->h_active;
  1878. catalog->dp_active = data;
  1879. catalog->widebus_en = pinfo->widebus_en;
  1880. panel->catalog->timing_cfg(catalog);
  1881. panel->panel_on = true;
  1882. end:
  1883. return rc;
  1884. }
  1885. static u32 _dp_panel_calc_be_in_lane(struct dp_panel *dp_panel)
  1886. {
  1887. struct dp_panel_info *pinfo;
  1888. struct msm_compression_info *comp_info;
  1889. u32 dsc_htot_byte_cnt, mod_result;
  1890. u32 numerator, denominator;
  1891. s64 temp_fp;
  1892. u32 be_in_lane = 10;
  1893. pinfo = &dp_panel->pinfo;
  1894. comp_info = &pinfo->comp_info;
  1895. if (!dp_panel->mst_state)
  1896. return be_in_lane;
  1897. switch (pinfo->comp_info.comp_ratio) {
  1898. case MSM_DISPLAY_COMPRESSION_RATIO_2_TO_1:
  1899. denominator = 16; /* 2 * bits-in-byte */
  1900. break;
  1901. case MSM_DISPLAY_COMPRESSION_RATIO_3_TO_1:
  1902. denominator = 24; /* 3 * bits-in-byte */
  1903. break;
  1904. default:
  1905. denominator = 8; /* 1 * bits-in-byte */
  1906. }
  1907. numerator = (pinfo->h_active + pinfo->h_back_porch +
  1908. pinfo->h_front_porch + pinfo->h_sync_width) *
  1909. pinfo->bpp;
  1910. temp_fp = drm_fixp_from_fraction(numerator, denominator);
  1911. dsc_htot_byte_cnt = drm_fixp2int_ceil(temp_fp);
  1912. mod_result = dsc_htot_byte_cnt % 12;
  1913. if (mod_result == 0)
  1914. be_in_lane = 8;
  1915. else if (mod_result <= 3)
  1916. be_in_lane = 1;
  1917. else if (mod_result <= 6)
  1918. be_in_lane = 2;
  1919. else if (mod_result <= 9)
  1920. be_in_lane = 4;
  1921. else if (mod_result <= 11)
  1922. be_in_lane = 8;
  1923. else
  1924. be_in_lane = 10;
  1925. return be_in_lane;
  1926. }
  1927. static void dp_panel_config_dsc(struct dp_panel *dp_panel, bool enable)
  1928. {
  1929. struct dp_catalog_panel *catalog;
  1930. struct dp_panel_private *panel;
  1931. struct dp_panel_info *pinfo;
  1932. struct msm_compression_info *comp_info;
  1933. struct dp_dsc_cfg_data *dsc;
  1934. int pps_len;
  1935. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  1936. catalog = panel->catalog;
  1937. dsc = &catalog->dsc;
  1938. pinfo = &dp_panel->pinfo;
  1939. comp_info = &pinfo->comp_info;
  1940. if (comp_info->comp_type == MSM_DISPLAY_COMPRESSION_DSC && enable) {
  1941. pps_len = dp_panel_dsc_create_pps_buf_cmd(&comp_info->dsc_info,
  1942. dsc->pps, 0);
  1943. dsc->pps_len = pps_len;
  1944. dp_panel_dsc_prepare_pps_packet(dp_panel);
  1945. dsc->slice_per_pkt = comp_info->dsc_info.slice_per_pkt - 1;
  1946. dsc->bytes_per_pkt = comp_info->dsc_info.bytes_per_pkt;
  1947. dsc->bytes_per_pkt /= comp_info->dsc_info.slice_per_pkt;
  1948. dsc->eol_byte_num = comp_info->dsc_info.eol_byte_num;
  1949. dsc->dto_count = comp_info->dsc_info.pclk_per_line;
  1950. dsc->be_in_lane = _dp_panel_calc_be_in_lane(dp_panel);
  1951. dsc->dsc_en = true;
  1952. dsc->dto_en = true;
  1953. _dp_panel_get_dto_m_n(comp_info->comp_ratio, pinfo->bpp,
  1954. &dsc->dto_n, &dsc->dto_d);
  1955. } else {
  1956. dsc->dsc_en = false;
  1957. dsc->dto_en = false;
  1958. dsc->dto_n = 0;
  1959. dsc->dto_d = 0;
  1960. }
  1961. catalog->stream_id = dp_panel->stream_id;
  1962. catalog->dsc_cfg(catalog);
  1963. if (catalog->dsc.dsc_en && enable)
  1964. catalog->pps_flush(catalog);
  1965. }
  1966. static int dp_panel_edid_register(struct dp_panel_private *panel)
  1967. {
  1968. int rc = 0;
  1969. panel->dp_panel.edid_ctrl = sde_edid_init();
  1970. if (!panel->dp_panel.edid_ctrl) {
  1971. pr_err("sde edid init for DP failed\n");
  1972. rc = -ENOMEM;
  1973. }
  1974. return rc;
  1975. }
  1976. static void dp_panel_edid_deregister(struct dp_panel_private *panel)
  1977. {
  1978. sde_edid_deinit((void **)&panel->dp_panel.edid_ctrl);
  1979. }
  1980. static int dp_panel_set_stream_info(struct dp_panel *dp_panel,
  1981. enum dp_stream_id stream_id, u32 ch_start_slot,
  1982. u32 ch_tot_slots, u32 pbn, int vcpi)
  1983. {
  1984. if (!dp_panel || stream_id > DP_STREAM_MAX) {
  1985. pr_err("invalid input. stream_id: %d\n", stream_id);
  1986. return -EINVAL;
  1987. }
  1988. dp_panel->vcpi = vcpi;
  1989. dp_panel->stream_id = stream_id;
  1990. dp_panel->channel_start_slot = ch_start_slot;
  1991. dp_panel->channel_total_slots = ch_tot_slots;
  1992. dp_panel->pbn = pbn;
  1993. return 0;
  1994. }
  1995. static int dp_panel_init_panel_info(struct dp_panel *dp_panel)
  1996. {
  1997. int rc = 0;
  1998. struct dp_panel_private *panel;
  1999. struct dp_panel_info *pinfo;
  2000. if (!dp_panel) {
  2001. pr_err("invalid input\n");
  2002. rc = -EINVAL;
  2003. goto end;
  2004. }
  2005. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  2006. pinfo = &dp_panel->pinfo;
  2007. drm_dp_dpcd_writeb(panel->aux->drm_aux, DP_SET_POWER, DP_SET_POWER_D0);
  2008. /*
  2009. * According to the DP 1.1 specification, a "Sink Device must exit the
  2010. * power saving state within 1 ms" (Section 2.5.3.1, Table 5-52, "Sink
  2011. * Control Field" (register 0x600).
  2012. */
  2013. usleep_range(1000, 2000);
  2014. drm_dp_link_probe(panel->aux->drm_aux, &dp_panel->link_info);
  2015. end:
  2016. return rc;
  2017. }
  2018. static int dp_panel_deinit_panel_info(struct dp_panel *dp_panel, u32 flags)
  2019. {
  2020. int rc = 0;
  2021. struct dp_panel_private *panel;
  2022. struct dp_catalog_hdr_data *hdr;
  2023. struct drm_connector *connector;
  2024. struct sde_connector_state *c_state;
  2025. if (!dp_panel) {
  2026. pr_err("invalid input\n");
  2027. return -EINVAL;
  2028. }
  2029. if (flags & DP_PANEL_SRC_INITIATED_POWER_DOWN) {
  2030. pr_debug("retain states in src initiated power down request\n");
  2031. return 0;
  2032. }
  2033. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  2034. hdr = &panel->catalog->hdr_data;
  2035. if (!panel->custom_edid && dp_panel->edid_ctrl->edid)
  2036. sde_free_edid((void **)&dp_panel->edid_ctrl);
  2037. dp_panel_set_stream_info(dp_panel, DP_STREAM_MAX, 0, 0, 0, 0);
  2038. memset(&dp_panel->pinfo, 0, sizeof(dp_panel->pinfo));
  2039. memset(&hdr->hdr_meta, 0, sizeof(hdr->hdr_meta));
  2040. panel->panel_on = false;
  2041. connector = dp_panel->connector;
  2042. c_state = to_sde_connector_state(connector->state);
  2043. connector->hdr_eotf = 0;
  2044. connector->hdr_metadata_type_one = 0;
  2045. connector->hdr_max_luminance = 0;
  2046. connector->hdr_avg_luminance = 0;
  2047. connector->hdr_min_luminance = 0;
  2048. connector->hdr_supported = false;
  2049. connector->hdr_plus_app_ver = 0;
  2050. memset(&c_state->hdr_meta, 0, sizeof(c_state->hdr_meta));
  2051. memset(&c_state->dyn_hdr_meta, 0, sizeof(c_state->dyn_hdr_meta));
  2052. return rc;
  2053. }
  2054. static u32 dp_panel_get_min_req_link_rate(struct dp_panel *dp_panel)
  2055. {
  2056. const u32 encoding_factx10 = 8;
  2057. u32 min_link_rate_khz = 0, lane_cnt;
  2058. struct dp_panel_info *pinfo;
  2059. if (!dp_panel) {
  2060. pr_err("invalid input\n");
  2061. goto end;
  2062. }
  2063. lane_cnt = dp_panel->link_info.num_lanes;
  2064. pinfo = &dp_panel->pinfo;
  2065. /* num_lanes * lane_count * 8 >= pclk * bpp * 10 */
  2066. min_link_rate_khz = pinfo->pixel_clk_khz /
  2067. (lane_cnt * encoding_factx10);
  2068. min_link_rate_khz *= pinfo->bpp;
  2069. pr_debug("min lclk req=%d khz for pclk=%d khz, lanes=%d, bpp=%d\n",
  2070. min_link_rate_khz, pinfo->pixel_clk_khz, lane_cnt,
  2071. pinfo->bpp);
  2072. end:
  2073. return min_link_rate_khz;
  2074. }
  2075. static bool dp_panel_hdr_supported(struct dp_panel *dp_panel)
  2076. {
  2077. struct dp_panel_private *panel;
  2078. if (!dp_panel) {
  2079. pr_err("invalid input\n");
  2080. return false;
  2081. }
  2082. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  2083. return panel->major >= 1 && panel->vsc_supported &&
  2084. (panel->minor >= 4 || panel->vscext_supported);
  2085. }
  2086. static u32 dp_panel_calc_dhdr_pkt_limit(struct dp_panel *dp_panel,
  2087. struct dp_dhdr_maxpkt_calc_input *input)
  2088. {
  2089. s64 mdpclk_fp = drm_fixp_from_fraction(input->mdp_clk, 1000000);
  2090. s64 lclk_fp = drm_fixp_from_fraction(input->lclk, 1000);
  2091. s64 pclk_fp = drm_fixp_from_fraction(input->pclk, 1000);
  2092. s64 nlanes_fp = drm_int2fixp(input->nlanes);
  2093. s64 target_sc = input->mst_target_sc;
  2094. s64 hactive_fp = drm_int2fixp(input->h_active);
  2095. const s64 i1_fp = DRM_FIXED_ONE;
  2096. const s64 i2_fp = drm_int2fixp(2);
  2097. const s64 i10_fp = drm_int2fixp(10);
  2098. const s64 i56_fp = drm_int2fixp(56);
  2099. const s64 i64_fp = drm_int2fixp(64);
  2100. s64 mst_bw_fp = i1_fp;
  2101. s64 fec_factor_fp = i1_fp;
  2102. s64 mst_bw64_fp, mst_bw64_ceil_fp, nlanes56_fp;
  2103. u32 f1, f2, f3, f4, f5, deploy_period, target_period;
  2104. s64 f3_f5_slot_fp;
  2105. u32 calc_pkt_limit;
  2106. const u32 max_pkt_limit = 64;
  2107. if (input->fec_en && input->mst_en)
  2108. fec_factor_fp = drm_fixp_from_fraction(64000, 65537);
  2109. if (input->mst_en)
  2110. mst_bw_fp = drm_fixp_div(target_sc, i64_fp);
  2111. f1 = drm_fixp2int_ceil(drm_fixp_div(drm_fixp_mul(i10_fp, lclk_fp),
  2112. mdpclk_fp));
  2113. f2 = drm_fixp2int_ceil(drm_fixp_div(drm_fixp_mul(i2_fp, lclk_fp),
  2114. mdpclk_fp)) + drm_fixp2int_ceil(drm_fixp_div(
  2115. drm_fixp_mul(i1_fp, lclk_fp), mdpclk_fp));
  2116. mst_bw64_fp = drm_fixp_mul(mst_bw_fp, i64_fp);
  2117. if (drm_fixp2int(mst_bw64_fp) == 0)
  2118. f3_f5_slot_fp = drm_fixp_div(i1_fp, drm_int2fixp(
  2119. drm_fixp2int_ceil(drm_fixp_div(
  2120. i1_fp, mst_bw64_fp))));
  2121. else
  2122. f3_f5_slot_fp = drm_int2fixp(drm_fixp2int(mst_bw_fp));
  2123. mst_bw64_ceil_fp = drm_int2fixp(drm_fixp2int_ceil(mst_bw64_fp));
  2124. f3 = drm_fixp2int(drm_fixp_mul(drm_int2fixp(drm_fixp2int(
  2125. drm_fixp_div(i2_fp, f3_f5_slot_fp)) + 1),
  2126. (i64_fp - mst_bw64_ceil_fp))) + 2;
  2127. if (!input->mst_en) {
  2128. f4 = 1 + drm_fixp2int(drm_fixp_div(drm_int2fixp(50),
  2129. nlanes_fp)) + drm_fixp2int(drm_fixp_div(
  2130. nlanes_fp, i2_fp));
  2131. f5 = 0;
  2132. } else {
  2133. f4 = 0;
  2134. nlanes56_fp = drm_fixp_div(i56_fp, nlanes_fp);
  2135. f5 = drm_fixp2int(drm_fixp_mul(drm_int2fixp(drm_fixp2int(
  2136. drm_fixp_div(i1_fp + nlanes56_fp,
  2137. f3_f5_slot_fp)) + 1), (i64_fp -
  2138. mst_bw64_ceil_fp + i1_fp + nlanes56_fp)));
  2139. }
  2140. deploy_period = f1 + f2 + f3 + f4 + f5 + 19;
  2141. target_period = drm_fixp2int(drm_fixp_mul(fec_factor_fp, drm_fixp_mul(
  2142. hactive_fp, drm_fixp_div(lclk_fp, pclk_fp))));
  2143. calc_pkt_limit = target_period / deploy_period;
  2144. pr_debug("input: %d, %d, %d, %d, %d, 0x%llx, %d, %d\n",
  2145. input->mdp_clk, input->lclk, input->pclk, input->h_active,
  2146. input->nlanes, input->mst_target_sc, input->mst_en ? 1 : 0,
  2147. input->fec_en ? 1 : 0);
  2148. pr_debug("factors: %d, %d, %d, %d, %d\n", f1, f2, f3, f4, f5);
  2149. pr_debug("d_p: %d, t_p: %d, maxPkts: %d%s\n", deploy_period,
  2150. target_period, calc_pkt_limit, calc_pkt_limit > max_pkt_limit ?
  2151. " CAPPED" : "");
  2152. if (calc_pkt_limit > max_pkt_limit)
  2153. calc_pkt_limit = max_pkt_limit;
  2154. pr_debug("packet limit per line = %d\n", calc_pkt_limit);
  2155. return calc_pkt_limit;
  2156. }
  2157. static int dp_panel_setup_hdr(struct dp_panel *dp_panel,
  2158. struct drm_msm_ext_hdr_metadata *hdr_meta,
  2159. bool dhdr_update, u64 core_clk_rate)
  2160. {
  2161. int rc = 0, max_pkts = 0;
  2162. struct dp_panel_private *panel;
  2163. struct dp_catalog_hdr_data *hdr;
  2164. struct dp_dhdr_maxpkt_calc_input input;
  2165. if (!dp_panel) {
  2166. pr_err("invalid input\n");
  2167. rc = -EINVAL;
  2168. goto end;
  2169. }
  2170. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  2171. hdr = &panel->catalog->hdr_data;
  2172. /* use cached meta data in case meta data not provided */
  2173. if (!hdr_meta) {
  2174. if (hdr->hdr_meta.hdr_state)
  2175. goto cached;
  2176. else
  2177. goto end;
  2178. }
  2179. panel->hdr_state = hdr_meta->hdr_state;
  2180. hdr->vsc_header_byte0 = 0x00;
  2181. hdr->vsc_header_byte1 = 0x07;
  2182. hdr->vsc_header_byte2 = 0x05;
  2183. hdr->vsc_header_byte3 = 0x13;
  2184. hdr->shdr_header_byte0 = 0x00;
  2185. hdr->shdr_header_byte1 = 0x87;
  2186. hdr->shdr_header_byte2 = 0x1D;
  2187. hdr->shdr_header_byte3 = 0x13 << 2;
  2188. /* VSC SDP Payload for DB16 */
  2189. hdr->pixel_encoding = RGB;
  2190. hdr->colorimetry = ITU_R_BT_2020_RGB;
  2191. /* VSC SDP Payload for DB17 */
  2192. hdr->dynamic_range = CEA;
  2193. /* VSC SDP Payload for DB18 */
  2194. hdr->content_type = GRAPHICS;
  2195. hdr->bpc = dp_panel->pinfo.bpp / 3;
  2196. hdr->version = 0x01;
  2197. hdr->length = 0x1A;
  2198. if (panel->hdr_state)
  2199. memcpy(&hdr->hdr_meta, hdr_meta, sizeof(hdr->hdr_meta));
  2200. else
  2201. memset(&hdr->hdr_meta, 0, sizeof(hdr->hdr_meta));
  2202. cached:
  2203. if (dhdr_update) {
  2204. hdr->vscext_header_byte0 = 0x00;
  2205. hdr->vscext_header_byte1 = 0x81;
  2206. hdr->vscext_header_byte2 = 0x1D;
  2207. hdr->vscext_header_byte3 = 0x13 << 2;
  2208. input.mdp_clk = core_clk_rate;
  2209. input.lclk = dp_panel->link_info.rate;
  2210. input.nlanes = dp_panel->link_info.num_lanes;
  2211. input.pclk = dp_panel->pinfo.pixel_clk_khz;
  2212. input.h_active = dp_panel->pinfo.h_active;
  2213. input.mst_target_sc = dp_panel->mst_target_sc;
  2214. input.mst_en = dp_panel->mst_state;
  2215. input.fec_en = dp_panel->fec_en;
  2216. max_pkts = dp_panel_calc_dhdr_pkt_limit(dp_panel, &input);
  2217. }
  2218. if (panel->panel_on) {
  2219. panel->catalog->stream_id = dp_panel->stream_id;
  2220. panel->catalog->config_hdr(panel->catalog, panel->hdr_state,
  2221. max_pkts);
  2222. if (dhdr_update)
  2223. panel->catalog->dhdr_flush(panel->catalog);
  2224. }
  2225. end:
  2226. return rc;
  2227. }
  2228. static int dp_panel_spd_config(struct dp_panel *dp_panel)
  2229. {
  2230. int rc = 0;
  2231. struct dp_panel_private *panel;
  2232. if (!dp_panel) {
  2233. pr_err("invalid input\n");
  2234. rc = -EINVAL;
  2235. goto end;
  2236. }
  2237. if (dp_panel->stream_id >= DP_STREAM_MAX) {
  2238. pr_err("invalid stream id:%d\n", dp_panel->stream_id);
  2239. return -EINVAL;
  2240. }
  2241. if (!dp_panel->spd_enabled) {
  2242. pr_debug("SPD Infoframe not enabled\n");
  2243. goto end;
  2244. }
  2245. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  2246. panel->catalog->spd_vendor_name = panel->spd_vendor_name;
  2247. panel->catalog->spd_product_description =
  2248. panel->spd_product_description;
  2249. panel->catalog->stream_id = dp_panel->stream_id;
  2250. panel->catalog->config_spd(panel->catalog);
  2251. end:
  2252. return rc;
  2253. }
  2254. static void dp_panel_config_ctrl(struct dp_panel *dp_panel)
  2255. {
  2256. u32 config = 0, tbd;
  2257. u8 *dpcd = dp_panel->dpcd;
  2258. struct dp_panel_private *panel;
  2259. struct dp_catalog_panel *catalog;
  2260. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  2261. catalog = panel->catalog;
  2262. config |= (2 << 13); /* Default-> LSCLK DIV: 1/4 LCLK */
  2263. config |= (0 << 11); /* RGB */
  2264. tbd = panel->link->get_test_bits_depth(panel->link,
  2265. dp_panel->pinfo.bpp);
  2266. if (tbd == DP_TEST_BIT_DEPTH_UNKNOWN)
  2267. tbd = DP_TEST_BIT_DEPTH_8;
  2268. config |= tbd << 8;
  2269. /* Num of Lanes */
  2270. config |= ((panel->link->link_params.lane_count - 1) << 4);
  2271. if (drm_dp_enhanced_frame_cap(dpcd))
  2272. config |= 0x40;
  2273. config |= 0x04; /* progressive video */
  2274. config |= 0x03; /* sycn clock & static Mvid */
  2275. catalog->config_ctrl(catalog, config);
  2276. }
  2277. static void dp_panel_config_misc(struct dp_panel *dp_panel)
  2278. {
  2279. struct dp_panel_private *panel;
  2280. struct dp_catalog_panel *catalog;
  2281. u32 misc_val;
  2282. u32 tb, cc;
  2283. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  2284. catalog = panel->catalog;
  2285. tb = panel->link->get_test_bits_depth(panel->link, dp_panel->pinfo.bpp);
  2286. cc = panel->link->get_colorimetry_config(panel->link);
  2287. misc_val = cc;
  2288. misc_val |= (tb << 5);
  2289. misc_val |= BIT(0); /* Configure clock to synchronous mode */
  2290. catalog->misc_val = misc_val;
  2291. catalog->config_misc(catalog);
  2292. }
  2293. static void dp_panel_config_msa(struct dp_panel *dp_panel)
  2294. {
  2295. struct dp_panel_private *panel;
  2296. struct dp_catalog_panel *catalog;
  2297. u32 rate;
  2298. u32 stream_rate_khz;
  2299. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  2300. catalog = panel->catalog;
  2301. catalog->widebus_en = dp_panel->widebus_en;
  2302. rate = drm_dp_bw_code_to_link_rate(panel->link->link_params.bw_code);
  2303. stream_rate_khz = dp_panel->pinfo.pixel_clk_khz;
  2304. catalog->config_msa(catalog, rate, stream_rate_khz);
  2305. }
  2306. static void dp_panel_resolution_info(struct dp_panel_private *panel)
  2307. {
  2308. struct dp_panel_info *pinfo = &panel->dp_panel.pinfo;
  2309. /*
  2310. * print resolution info as this is a result
  2311. * of user initiated action of cable connection
  2312. */
  2313. pr_info("DP RESOLUTION: active(back|front|width|low)\n");
  2314. pr_info("%d(%d|%d|%d|%d)x%d(%d|%d|%d|%d)@%dfps %dbpp %dKhz %dLR %dLn\n",
  2315. pinfo->h_active, pinfo->h_back_porch, pinfo->h_front_porch,
  2316. pinfo->h_sync_width, pinfo->h_active_low,
  2317. pinfo->v_active, pinfo->v_back_porch, pinfo->v_front_porch,
  2318. pinfo->v_sync_width, pinfo->v_active_low,
  2319. pinfo->refresh_rate, pinfo->bpp, pinfo->pixel_clk_khz,
  2320. panel->link->link_params.bw_code,
  2321. panel->link->link_params.lane_count);
  2322. }
  2323. static int dp_panel_hw_cfg(struct dp_panel *dp_panel, bool enable)
  2324. {
  2325. struct dp_panel_private *panel;
  2326. if (!dp_panel) {
  2327. pr_err("invalid input\n");
  2328. return -EINVAL;
  2329. }
  2330. if (dp_panel->stream_id >= DP_STREAM_MAX) {
  2331. pr_err("invalid stream_id: %d\n", dp_panel->stream_id);
  2332. return -EINVAL;
  2333. }
  2334. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  2335. panel->catalog->stream_id = dp_panel->stream_id;
  2336. if (enable) {
  2337. dp_panel_config_ctrl(dp_panel);
  2338. dp_panel_config_misc(dp_panel);
  2339. dp_panel_config_msa(dp_panel);
  2340. dp_panel_config_dsc(dp_panel, enable);
  2341. dp_panel_config_tr_unit(dp_panel);
  2342. dp_panel_config_timing(dp_panel);
  2343. dp_panel_resolution_info(panel);
  2344. }
  2345. panel->catalog->config_dto(panel->catalog, !enable);
  2346. return 0;
  2347. }
  2348. static int dp_panel_read_sink_sts(struct dp_panel *dp_panel, u8 *sts, u32 size)
  2349. {
  2350. int rlen, rc = 0;
  2351. struct dp_panel_private *panel;
  2352. if (!dp_panel || !sts || !size) {
  2353. pr_err("invalid input\n");
  2354. rc = -EINVAL;
  2355. return rc;
  2356. }
  2357. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  2358. rlen = drm_dp_dpcd_read(panel->aux->drm_aux, DP_SINK_COUNT_ESI,
  2359. sts, size);
  2360. if (rlen != size) {
  2361. pr_err("dpcd sink sts fail rlen:%d size:%d\n", rlen, size);
  2362. rc = -EINVAL;
  2363. return rc;
  2364. }
  2365. return 0;
  2366. }
  2367. static int dp_panel_update_edid(struct dp_panel *dp_panel, struct edid *edid)
  2368. {
  2369. int rc;
  2370. dp_panel->edid_ctrl->edid = edid;
  2371. sde_parse_edid(dp_panel->edid_ctrl);
  2372. rc = _sde_edid_update_modes(dp_panel->connector, dp_panel->edid_ctrl);
  2373. dp_panel->audio_supported = drm_detect_monitor_audio(edid);
  2374. return rc;
  2375. }
  2376. static bool dp_panel_read_mst_cap(struct dp_panel *dp_panel)
  2377. {
  2378. int rlen;
  2379. struct dp_panel_private *panel;
  2380. u8 dpcd;
  2381. bool mst_cap = false;
  2382. if (!dp_panel) {
  2383. pr_err("invalid input\n");
  2384. return 0;
  2385. }
  2386. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  2387. rlen = drm_dp_dpcd_read(panel->aux->drm_aux, DP_MSTM_CAP,
  2388. &dpcd, 1);
  2389. if (rlen < 1) {
  2390. pr_err("dpcd mstm_cap read failed, rlen=%d\n", rlen);
  2391. goto end;
  2392. }
  2393. mst_cap = (dpcd & DP_MST_CAP) ? true : false;
  2394. end:
  2395. pr_debug("dp mst-cap: %d\n", mst_cap);
  2396. return mst_cap;
  2397. }
  2398. static void dp_panel_convert_to_dp_mode(struct dp_panel *dp_panel,
  2399. const struct drm_display_mode *drm_mode,
  2400. struct dp_display_mode *dp_mode)
  2401. {
  2402. const u32 num_components = 3, default_bpp = 24;
  2403. struct msm_compression_info *comp_info;
  2404. bool dsc_cap = (dp_mode->capabilities & DP_PANEL_CAPS_DSC) ?
  2405. true : false;
  2406. dp_mode->timing.h_active = drm_mode->hdisplay;
  2407. dp_mode->timing.h_back_porch = drm_mode->htotal - drm_mode->hsync_end;
  2408. dp_mode->timing.h_sync_width = drm_mode->htotal -
  2409. (drm_mode->hsync_start + dp_mode->timing.h_back_porch);
  2410. dp_mode->timing.h_front_porch = drm_mode->hsync_start -
  2411. drm_mode->hdisplay;
  2412. dp_mode->timing.h_skew = drm_mode->hskew;
  2413. dp_mode->timing.v_active = drm_mode->vdisplay;
  2414. dp_mode->timing.v_back_porch = drm_mode->vtotal - drm_mode->vsync_end;
  2415. dp_mode->timing.v_sync_width = drm_mode->vtotal -
  2416. (drm_mode->vsync_start + dp_mode->timing.v_back_porch);
  2417. dp_mode->timing.v_front_porch = drm_mode->vsync_start -
  2418. drm_mode->vdisplay;
  2419. dp_mode->timing.refresh_rate = drm_mode->vrefresh;
  2420. dp_mode->timing.pixel_clk_khz = drm_mode->clock;
  2421. dp_mode->timing.v_active_low =
  2422. !!(drm_mode->flags & DRM_MODE_FLAG_NVSYNC);
  2423. dp_mode->timing.h_active_low =
  2424. !!(drm_mode->flags & DRM_MODE_FLAG_NHSYNC);
  2425. dp_mode->timing.bpp =
  2426. dp_panel->connector->display_info.bpc * num_components;
  2427. if (!dp_mode->timing.bpp)
  2428. dp_mode->timing.bpp = default_bpp;
  2429. dp_mode->timing.bpp = dp_panel_get_mode_bpp(dp_panel,
  2430. dp_mode->timing.bpp, dp_mode->timing.pixel_clk_khz);
  2431. dp_mode->timing.widebus_en = dp_panel->widebus_en;
  2432. dp_mode->timing.dsc_overhead_fp = 0;
  2433. if (dp_panel->dsc_en && dsc_cap) {
  2434. comp_info = &dp_mode->timing.comp_info;
  2435. if (dp_panel_dsc_prepare_basic_params(comp_info,
  2436. dp_mode, dp_panel)) {
  2437. pr_debug("prepare DSC basic params failed\n");
  2438. return;
  2439. }
  2440. dp_panel_dsc_populate_static_params(&comp_info->dsc_info,
  2441. dp_panel);
  2442. dp_panel_dsc_pclk_param_calc(dp_panel,
  2443. &comp_info->dsc_info,
  2444. comp_info->comp_ratio,
  2445. dp_mode);
  2446. }
  2447. dp_mode->fec_overhead_fp = dp_panel->fec_overhead_fp;
  2448. }
  2449. static void dp_panel_update_pps(struct dp_panel *dp_panel, char *pps_cmd)
  2450. {
  2451. struct dp_catalog_panel *catalog;
  2452. struct dp_panel_private *panel;
  2453. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  2454. catalog = panel->catalog;
  2455. catalog->stream_id = dp_panel->stream_id;
  2456. catalog->pps_flush(catalog);
  2457. }
  2458. struct dp_panel *dp_panel_get(struct dp_panel_in *in)
  2459. {
  2460. int rc = 0;
  2461. struct dp_panel_private *panel;
  2462. struct dp_panel *dp_panel;
  2463. struct sde_connector *sde_conn;
  2464. if (!in->dev || !in->catalog || !in->aux ||
  2465. !in->link || !in->connector) {
  2466. pr_err("invalid input\n");
  2467. rc = -EINVAL;
  2468. goto error;
  2469. }
  2470. panel = devm_kzalloc(in->dev, sizeof(*panel), GFP_KERNEL);
  2471. if (!panel) {
  2472. rc = -ENOMEM;
  2473. goto error;
  2474. }
  2475. panel->dev = in->dev;
  2476. panel->aux = in->aux;
  2477. panel->catalog = in->catalog;
  2478. panel->link = in->link;
  2479. panel->parser = in->parser;
  2480. dp_panel = &panel->dp_panel;
  2481. dp_panel->max_bw_code = DP_LINK_BW_8_1;
  2482. dp_panel->spd_enabled = true;
  2483. memcpy(panel->spd_vendor_name, vendor_name, (sizeof(u8) * 8));
  2484. memcpy(panel->spd_product_description, product_desc, (sizeof(u8) * 16));
  2485. dp_panel->connector = in->connector;
  2486. dp_panel->dsc_feature_enable = panel->parser->dsc_feature_enable;
  2487. dp_panel->fec_feature_enable = panel->parser->fec_feature_enable;
  2488. if (in->base_panel) {
  2489. memcpy(dp_panel->dpcd, in->base_panel->dpcd,
  2490. DP_RECEIVER_CAP_SIZE + 1);
  2491. memcpy(dp_panel->dsc_dpcd, in->base_panel->dsc_dpcd,
  2492. DP_RECEIVER_DSC_CAP_SIZE + 1);
  2493. memcpy(&dp_panel->link_info, &in->base_panel->link_info,
  2494. sizeof(dp_panel->link_info));
  2495. dp_panel->mst_state = in->base_panel->mst_state;
  2496. dp_panel->widebus_en = in->base_panel->widebus_en;
  2497. dp_panel->fec_en = in->base_panel->fec_en;
  2498. dp_panel->dsc_en = in->base_panel->dsc_en;
  2499. dp_panel->fec_overhead_fp = in->base_panel->fec_overhead_fp;
  2500. }
  2501. dp_panel->init = dp_panel_init_panel_info;
  2502. dp_panel->deinit = dp_panel_deinit_panel_info;
  2503. dp_panel->hw_cfg = dp_panel_hw_cfg;
  2504. dp_panel->read_sink_caps = dp_panel_read_sink_caps;
  2505. dp_panel->get_min_req_link_rate = dp_panel_get_min_req_link_rate;
  2506. dp_panel->get_mode_bpp = dp_panel_get_mode_bpp;
  2507. dp_panel->get_modes = dp_panel_get_modes;
  2508. dp_panel->handle_sink_request = dp_panel_handle_sink_request;
  2509. dp_panel->set_edid = dp_panel_set_edid;
  2510. dp_panel->set_dpcd = dp_panel_set_dpcd;
  2511. dp_panel->tpg_config = dp_panel_tpg_config;
  2512. dp_panel->spd_config = dp_panel_spd_config;
  2513. dp_panel->setup_hdr = dp_panel_setup_hdr;
  2514. dp_panel->hdr_supported = dp_panel_hdr_supported;
  2515. dp_panel->set_stream_info = dp_panel_set_stream_info;
  2516. dp_panel->read_sink_status = dp_panel_read_sink_sts;
  2517. dp_panel->update_edid = dp_panel_update_edid;
  2518. dp_panel->read_mst_cap = dp_panel_read_mst_cap;
  2519. dp_panel->convert_to_dp_mode = dp_panel_convert_to_dp_mode;
  2520. dp_panel->update_pps = dp_panel_update_pps;
  2521. sde_conn = to_sde_connector(dp_panel->connector);
  2522. sde_conn->drv_panel = dp_panel;
  2523. dp_panel_edid_register(panel);
  2524. return dp_panel;
  2525. error:
  2526. return ERR_PTR(rc);
  2527. }
  2528. void dp_panel_put(struct dp_panel *dp_panel)
  2529. {
  2530. struct dp_panel_private *panel;
  2531. if (!dp_panel)
  2532. return;
  2533. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  2534. dp_panel_edid_deregister(panel);
  2535. devm_kfree(panel->dev, panel);
  2536. }