dp_panel.c 90 KB

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