dp_panel.c 81 KB

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