dp_panel.c 83 KB

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