dp_panel.c 81 KB

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