dp_panel.c 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093
  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. u32 dsc_version_major, dsc_version_minor;
  1133. bool dsc_version_supported = false;
  1134. dsc_version_major = dp_panel->sink_dsc_caps.version & 0xF;
  1135. dsc_version_minor = (dp_panel->sink_dsc_caps.version >> 4) & 0xF;
  1136. dsc_version_supported = (dsc_version_major == 0x1 &&
  1137. (dsc_version_minor == 0x1 || dsc_version_minor == 0x2))
  1138. ? true : false;
  1139. DP_DEBUG("DSC version: %d.%d, dpcd value: %x\n",
  1140. dsc_version_major, dsc_version_minor,
  1141. dp_panel->sink_dsc_caps.version);
  1142. if (!dsc_version_supported) {
  1143. dsc_version_major = 1;
  1144. dsc_version_minor = 1;
  1145. DP_ERR("invalid sink DSC version, fallback to %d.%d\n",
  1146. dsc_version_major, dsc_version_minor);
  1147. }
  1148. comp_info->dsc_info.config.dsc_version_major = dsc_version_major;
  1149. comp_info->dsc_info.config.dsc_version_minor = dsc_version_minor;
  1150. comp_info->dsc_info.scr_rev = 0x0;
  1151. comp_info->dsc_info.slice_per_pkt = 0;
  1152. for (i = 0; i < ARRAY_SIZE(slice_per_line_tbl); i++) {
  1153. rec = &slice_per_line_tbl[i];
  1154. if ((ppr > rec->min_ppr) && (ppr <= rec->max_ppr)) {
  1155. comp_info->dsc_info.slice_per_pkt = rec->num_slices;
  1156. i++;
  1157. break;
  1158. }
  1159. }
  1160. if (comp_info->dsc_info.slice_per_pkt == 0)
  1161. return -EINVAL;
  1162. ppr_max_index = dp_panel->dsc_dpcd[11] &= 0xf;
  1163. if (!ppr_max_index || ppr_max_index >= 15) {
  1164. DP_DEBUG("Throughput mode 0 not supported");
  1165. return -EINVAL;
  1166. }
  1167. tput = &peak_throughput_mode_0_tbl[ppr_max_index];
  1168. peak_throughput = tput->peak_throughput;
  1169. max_slice_width = dp_panel->dsc_dpcd[12] * 320;
  1170. slice_width = (dp_mode->timing.h_active /
  1171. comp_info->dsc_info.slice_per_pkt);
  1172. ppr_per_slice = ppr/comp_info->dsc_info.slice_per_pkt;
  1173. slice_caps_1 = dp_panel->dsc_dpcd[4];
  1174. slice_caps_2 = dp_panel->dsc_dpcd[13] & 0x7;
  1175. /*
  1176. * There are 3 conditions to check for sink support:
  1177. * 1. The slice width cannot exceed the maximum.
  1178. * 2. The ppr per slice cannot exceed the maximum.
  1179. * 3. The number of slices must be explicitly supported.
  1180. */
  1181. while (slice_width >= max_slice_width ||
  1182. ppr_per_slice > peak_throughput ||
  1183. !dp_panel_check_slice_support(
  1184. comp_info->dsc_info.slice_per_pkt, slice_caps_1,
  1185. slice_caps_2)) {
  1186. if (i == ARRAY_SIZE(slice_per_line_tbl))
  1187. return -EINVAL;
  1188. rec = &slice_per_line_tbl[i];
  1189. comp_info->dsc_info.slice_per_pkt = rec->num_slices;
  1190. slice_width = (dp_mode->timing.h_active /
  1191. comp_info->dsc_info.slice_per_pkt);
  1192. ppr_per_slice = ppr/comp_info->dsc_info.slice_per_pkt;
  1193. i++;
  1194. }
  1195. comp_info->dsc_info.config.block_pred_enable =
  1196. dp_panel->sink_dsc_caps.block_pred_en;
  1197. comp_info->dsc_info.config.pic_width = dp_mode->timing.h_active;
  1198. comp_info->dsc_info.config.pic_height = dp_mode->timing.v_active;
  1199. comp_info->dsc_info.config.slice_width = slice_width;
  1200. if (comp_info->dsc_info.config.pic_height % 108 == 0)
  1201. comp_info->dsc_info.config.slice_height = 108;
  1202. else if (comp_info->dsc_info.config.pic_height % 16 == 0)
  1203. comp_info->dsc_info.config.slice_height = 16;
  1204. else if (comp_info->dsc_info.config.pic_height % 12 == 0)
  1205. comp_info->dsc_info.config.slice_height = 12;
  1206. else
  1207. comp_info->dsc_info.config.slice_height = 15;
  1208. comp_info->dsc_info.config.bits_per_component =
  1209. (dp_mode->timing.bpp / 3);
  1210. comp_info->dsc_info.config.bits_per_pixel =
  1211. comp_info->dsc_info.config.bits_per_component << 4;
  1212. comp_info->dsc_info.config.slice_count =
  1213. DIV_ROUND_UP(dp_mode->timing.h_active, slice_width);
  1214. comp_info->comp_type = MSM_DISPLAY_COMPRESSION_DSC;
  1215. comp_info->comp_ratio = DP_COMPRESSION_RATIO_3_TO_1;
  1216. return 0;
  1217. }
  1218. static int dp_panel_read_dpcd(struct dp_panel *dp_panel, bool multi_func)
  1219. {
  1220. int rlen, rc = 0;
  1221. struct dp_panel_private *panel;
  1222. struct drm_dp_link *link_info;
  1223. struct drm_dp_aux *drm_aux;
  1224. u8 *dpcd, rx_feature, temp;
  1225. u32 dfp_count = 0, offset = DP_DPCD_REV;
  1226. if (!dp_panel) {
  1227. DP_ERR("invalid input\n");
  1228. rc = -EINVAL;
  1229. goto end;
  1230. }
  1231. dpcd = dp_panel->dpcd;
  1232. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  1233. drm_aux = panel->aux->drm_aux;
  1234. link_info = &dp_panel->link_info;
  1235. /* reset vsc data */
  1236. panel->vsc_supported = false;
  1237. panel->vscext_supported = false;
  1238. panel->vscext_chaining_supported = false;
  1239. if (panel->custom_dpcd) {
  1240. DP_DEBUG("skip dpcd read in debug mode\n");
  1241. goto skip_dpcd_read;
  1242. }
  1243. rlen = drm_dp_dpcd_read(drm_aux, DP_TRAINING_AUX_RD_INTERVAL, &temp, 1);
  1244. if (rlen != 1) {
  1245. DP_ERR("error reading DP_TRAINING_AUX_RD_INTERVAL\n");
  1246. rc = -EINVAL;
  1247. goto end;
  1248. }
  1249. /* check for EXTENDED_RECEIVER_CAPABILITY_FIELD_PRESENT */
  1250. if (temp & BIT(7)) {
  1251. DP_DEBUG("using EXTENDED_RECEIVER_CAPABILITY_FIELD\n");
  1252. offset = DPRX_EXTENDED_DPCD_FIELD;
  1253. }
  1254. rlen = drm_dp_dpcd_read(drm_aux, offset,
  1255. dp_panel->dpcd, (DP_RECEIVER_CAP_SIZE + 1));
  1256. if (rlen < (DP_RECEIVER_CAP_SIZE + 1)) {
  1257. DP_ERR("dpcd read failed, rlen=%d\n", rlen);
  1258. if (rlen == -ETIMEDOUT)
  1259. rc = rlen;
  1260. else
  1261. rc = -EINVAL;
  1262. goto end;
  1263. }
  1264. print_hex_dump_debug("[drm-dp] SINK DPCD: ",
  1265. DUMP_PREFIX_NONE, 8, 1, dp_panel->dpcd, rlen, false);
  1266. rlen = drm_dp_dpcd_read(panel->aux->drm_aux,
  1267. DPRX_FEATURE_ENUMERATION_LIST, &rx_feature, 1);
  1268. if (rlen != 1) {
  1269. DP_DEBUG("failed to read DPRX_FEATURE_ENUMERATION_LIST\n");
  1270. rx_feature = 0;
  1271. }
  1272. skip_dpcd_read:
  1273. if (panel->custom_dpcd)
  1274. rx_feature = dp_panel->dpcd[DP_RECEIVER_CAP_SIZE + 1];
  1275. panel->vsc_supported = !!(rx_feature &
  1276. VSC_SDP_EXTENSION_FOR_COLORIMETRY_SUPPORTED);
  1277. panel->vscext_supported = !!(rx_feature & VSC_EXT_VESA_SDP_SUPPORTED);
  1278. panel->vscext_chaining_supported = !!(rx_feature &
  1279. VSC_EXT_VESA_SDP_CHAINING_SUPPORTED);
  1280. DP_DEBUG("vsc=%d, vscext=%d, vscext_chaining=%d\n",
  1281. panel->vsc_supported, panel->vscext_supported,
  1282. panel->vscext_chaining_supported);
  1283. link_info->revision = dpcd[DP_DPCD_REV];
  1284. panel->major = (link_info->revision >> 4) & 0x0f;
  1285. panel->minor = link_info->revision & 0x0f;
  1286. /* override link params updated in dp_panel_init_panel_info */
  1287. link_info->rate = min_t(unsigned long, panel->parser->max_lclk_khz,
  1288. drm_dp_bw_code_to_link_rate(dpcd[DP_MAX_LINK_RATE]));
  1289. link_info->num_lanes = dpcd[DP_MAX_LANE_COUNT] & DP_MAX_LANE_COUNT_MASK;
  1290. if (is_link_rate_valid(panel->dp_panel.link_bw_code)) {
  1291. DP_DEBUG("debug link bandwidth code: 0x%x\n",
  1292. panel->dp_panel.link_bw_code);
  1293. link_info->rate = drm_dp_bw_code_to_link_rate(
  1294. panel->dp_panel.link_bw_code);
  1295. }
  1296. if (is_lane_count_valid(panel->dp_panel.lane_count)) {
  1297. DP_DEBUG("debug lane count: %d\n", panel->dp_panel.lane_count);
  1298. link_info->num_lanes = panel->dp_panel.lane_count;
  1299. }
  1300. if (multi_func)
  1301. link_info->num_lanes = min_t(unsigned int,
  1302. link_info->num_lanes, 2);
  1303. DP_DEBUG("version:%d.%d, rate:%d, lanes:%d\n", panel->major,
  1304. panel->minor, link_info->rate, link_info->num_lanes);
  1305. if (drm_dp_enhanced_frame_cap(dpcd))
  1306. link_info->capabilities |= DP_LINK_CAP_ENHANCED_FRAMING;
  1307. dfp_count = dpcd[DP_DOWN_STREAM_PORT_COUNT] &
  1308. DP_DOWN_STREAM_PORT_COUNT;
  1309. if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_PRESENT)
  1310. && (dpcd[DP_DPCD_REV] > 0x10)) {
  1311. rlen = drm_dp_dpcd_read(panel->aux->drm_aux,
  1312. DP_DOWNSTREAM_PORT_0, dp_panel->ds_ports,
  1313. DP_MAX_DOWNSTREAM_PORTS);
  1314. if (rlen < DP_MAX_DOWNSTREAM_PORTS) {
  1315. DP_ERR("ds port status failed, rlen=%d\n", rlen);
  1316. rc = -EINVAL;
  1317. goto end;
  1318. }
  1319. }
  1320. if (dfp_count > DP_MAX_DS_PORT_COUNT)
  1321. DP_DEBUG("DS port count %d greater that max (%d) supported\n",
  1322. dfp_count, DP_MAX_DS_PORT_COUNT);
  1323. end:
  1324. return rc;
  1325. }
  1326. static int dp_panel_set_default_link_params(struct dp_panel *dp_panel)
  1327. {
  1328. struct drm_dp_link *link_info;
  1329. const int default_bw_code = 162000;
  1330. const int default_num_lanes = 1;
  1331. if (!dp_panel) {
  1332. DP_ERR("invalid input\n");
  1333. return -EINVAL;
  1334. }
  1335. link_info = &dp_panel->link_info;
  1336. link_info->rate = default_bw_code;
  1337. link_info->num_lanes = default_num_lanes;
  1338. DP_DEBUG("link_rate=%d num_lanes=%d\n",
  1339. link_info->rate, link_info->num_lanes);
  1340. return 0;
  1341. }
  1342. static int dp_panel_set_edid(struct dp_panel *dp_panel, u8 *edid)
  1343. {
  1344. struct dp_panel_private *panel;
  1345. if (!dp_panel) {
  1346. DP_ERR("invalid input\n");
  1347. return -EINVAL;
  1348. }
  1349. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  1350. if (edid) {
  1351. dp_panel->edid_ctrl->edid = (struct edid *)edid;
  1352. panel->custom_edid = true;
  1353. } else {
  1354. panel->custom_edid = false;
  1355. dp_panel->edid_ctrl->edid = NULL;
  1356. }
  1357. DP_DEBUG("%d\n", panel->custom_edid);
  1358. return 0;
  1359. }
  1360. static int dp_panel_set_dpcd(struct dp_panel *dp_panel, u8 *dpcd)
  1361. {
  1362. struct dp_panel_private *panel;
  1363. u8 *dp_dpcd;
  1364. if (!dp_panel) {
  1365. DP_ERR("invalid input\n");
  1366. return -EINVAL;
  1367. }
  1368. dp_dpcd = dp_panel->dpcd;
  1369. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  1370. if (dpcd) {
  1371. memcpy(dp_dpcd, dpcd, DP_RECEIVER_CAP_SIZE +
  1372. DP_RECEIVER_EXT_CAP_SIZE + 1);
  1373. panel->custom_dpcd = true;
  1374. } else {
  1375. panel->custom_dpcd = false;
  1376. }
  1377. DP_DEBUG("%d\n", panel->custom_dpcd);
  1378. return 0;
  1379. }
  1380. static int dp_panel_read_edid(struct dp_panel *dp_panel,
  1381. struct drm_connector *connector)
  1382. {
  1383. int ret = 0;
  1384. struct dp_panel_private *panel;
  1385. struct edid *edid;
  1386. if (!dp_panel) {
  1387. DP_ERR("invalid input\n");
  1388. return -EINVAL;
  1389. }
  1390. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  1391. if (panel->custom_edid) {
  1392. DP_DEBUG("skip edid read in debug mode\n");
  1393. goto end;
  1394. }
  1395. sde_get_edid(connector, &panel->aux->drm_aux->ddc,
  1396. (void **)&dp_panel->edid_ctrl);
  1397. if (!dp_panel->edid_ctrl->edid) {
  1398. DP_ERR("EDID read failed\n");
  1399. ret = -EINVAL;
  1400. goto end;
  1401. }
  1402. end:
  1403. edid = dp_panel->edid_ctrl->edid;
  1404. dp_panel->audio_supported = drm_detect_monitor_audio(edid);
  1405. return ret;
  1406. }
  1407. static void dp_panel_decode_dsc_dpcd(struct dp_panel *dp_panel)
  1408. {
  1409. if (dp_panel->dsc_dpcd[0]) {
  1410. dp_panel->sink_dsc_caps.dsc_capable = true;
  1411. dp_panel->sink_dsc_caps.version = dp_panel->dsc_dpcd[1];
  1412. dp_panel->sink_dsc_caps.block_pred_en =
  1413. dp_panel->dsc_dpcd[6] ? true : false;
  1414. dp_panel->sink_dsc_caps.color_depth =
  1415. dp_panel->dsc_dpcd[10];
  1416. if (dp_panel->sink_dsc_caps.version >= 0x11)
  1417. dp_panel->dsc_en = true;
  1418. } else {
  1419. dp_panel->sink_dsc_caps.dsc_capable = false;
  1420. dp_panel->dsc_en = false;
  1421. }
  1422. }
  1423. static void dp_panel_read_sink_dsc_caps(struct dp_panel *dp_panel)
  1424. {
  1425. int rlen;
  1426. struct dp_panel_private *panel;
  1427. int dpcd_rev;
  1428. if (!dp_panel) {
  1429. DP_ERR("invalid input\n");
  1430. return;
  1431. }
  1432. dpcd_rev = dp_panel->dpcd[DP_DPCD_REV];
  1433. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  1434. if (panel->parser->dsc_feature_enable && dpcd_rev >= 0x14) {
  1435. rlen = drm_dp_dpcd_read(panel->aux->drm_aux, DP_DSC_SUPPORT,
  1436. dp_panel->dsc_dpcd, (DP_RECEIVER_DSC_CAP_SIZE + 1));
  1437. if (rlen < (DP_RECEIVER_DSC_CAP_SIZE + 1)) {
  1438. DP_DEBUG("dsc dpcd read failed, rlen=%d\n", rlen);
  1439. return;
  1440. }
  1441. print_hex_dump_debug("[drm-dp] SINK DSC DPCD: ",
  1442. DUMP_PREFIX_NONE, 8, 1, dp_panel->dsc_dpcd, rlen,
  1443. false);
  1444. dp_panel_decode_dsc_dpcd(dp_panel);
  1445. }
  1446. }
  1447. static void dp_panel_read_sink_fec_caps(struct dp_panel *dp_panel)
  1448. {
  1449. int rlen;
  1450. struct dp_panel_private *panel;
  1451. s64 fec_overhead_fp = drm_fixp_from_fraction(1, 1);
  1452. if (!dp_panel) {
  1453. DP_ERR("invalid input\n");
  1454. return;
  1455. }
  1456. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  1457. rlen = drm_dp_dpcd_readb(panel->aux->drm_aux, DP_FEC_CAPABILITY,
  1458. &dp_panel->fec_dpcd);
  1459. if (rlen < 1) {
  1460. DP_ERR("fec capability read failed, rlen=%d\n", rlen);
  1461. return;
  1462. }
  1463. dp_panel->fec_en = dp_panel->fec_dpcd & DP_FEC_CAPABLE;
  1464. if (dp_panel->fec_en)
  1465. fec_overhead_fp = drm_fixp_from_fraction(100000, 97582);
  1466. dp_panel->fec_overhead_fp = fec_overhead_fp;
  1467. return;
  1468. }
  1469. static int dp_panel_read_sink_caps(struct dp_panel *dp_panel,
  1470. struct drm_connector *connector, bool multi_func)
  1471. {
  1472. int rc = 0, rlen, count, downstream_ports;
  1473. const int count_len = 1;
  1474. struct dp_panel_private *panel;
  1475. if (!dp_panel || !connector) {
  1476. DP_ERR("invalid input\n");
  1477. rc = -EINVAL;
  1478. goto end;
  1479. }
  1480. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  1481. rc = dp_panel_read_dpcd(dp_panel, multi_func);
  1482. if (rc || !is_link_rate_valid(drm_dp_link_rate_to_bw_code(
  1483. dp_panel->link_info.rate)) || !is_lane_count_valid(
  1484. dp_panel->link_info.num_lanes) ||
  1485. ((drm_dp_link_rate_to_bw_code(dp_panel->link_info.rate)) >
  1486. dp_panel->max_bw_code)) {
  1487. if ((rc == -ETIMEDOUT) || (rc == -ENODEV)) {
  1488. DP_ERR("DPCD read failed, return early\n");
  1489. goto end;
  1490. }
  1491. DP_ERR("panel dpcd read failed/incorrect, set default params\n");
  1492. dp_panel_set_default_link_params(dp_panel);
  1493. }
  1494. downstream_ports = dp_panel->dpcd[DP_DOWNSTREAMPORT_PRESENT] &
  1495. DP_DWN_STRM_PORT_PRESENT;
  1496. if (downstream_ports) {
  1497. rlen = drm_dp_dpcd_read(panel->aux->drm_aux, DP_SINK_COUNT,
  1498. &count, count_len);
  1499. if (rlen == count_len) {
  1500. count = DP_GET_SINK_COUNT(count);
  1501. if (!count) {
  1502. DP_ERR("no downstream ports connected\n");
  1503. panel->link->sink_count.count = 0;
  1504. rc = -ENOTCONN;
  1505. goto end;
  1506. }
  1507. }
  1508. }
  1509. /* There is no need to read EDID from MST branch */
  1510. if (panel->parser->has_mst && dp_panel->read_mst_cap(dp_panel))
  1511. goto skip_edid;
  1512. rc = dp_panel_read_edid(dp_panel, connector);
  1513. if (rc) {
  1514. DP_ERR("panel edid read failed, set failsafe mode\n");
  1515. return rc;
  1516. }
  1517. skip_edid:
  1518. dp_panel->widebus_en = panel->parser->has_widebus;
  1519. dp_panel->dsc_feature_enable = panel->parser->dsc_feature_enable;
  1520. dp_panel->fec_feature_enable = panel->parser->fec_feature_enable;
  1521. dp_panel->fec_en = false;
  1522. dp_panel->dsc_en = false;
  1523. if (dp_panel->dpcd[DP_DPCD_REV] >= DP_DPCD_REV_14 &&
  1524. dp_panel->fec_feature_enable) {
  1525. dp_panel_read_sink_fec_caps(dp_panel);
  1526. if (dp_panel->dsc_feature_enable && dp_panel->fec_en)
  1527. dp_panel_read_sink_dsc_caps(dp_panel);
  1528. }
  1529. DP_INFO("fec_en=%d, dsc_en=%d, widebus_en=%d\n", dp_panel->fec_en,
  1530. dp_panel->dsc_en, dp_panel->widebus_en);
  1531. end:
  1532. return rc;
  1533. }
  1534. static u32 dp_panel_get_supported_bpp(struct dp_panel *dp_panel,
  1535. u32 mode_edid_bpp, u32 mode_pclk_khz)
  1536. {
  1537. struct dp_link_params *link_params;
  1538. struct dp_panel_private *panel;
  1539. const u32 max_supported_bpp = 30;
  1540. u32 min_supported_bpp = 18;
  1541. u32 bpp = 0, data_rate_khz = 0;
  1542. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  1543. if (dp_panel->dsc_en)
  1544. min_supported_bpp = 24;
  1545. bpp = min_t(u32, mode_edid_bpp, max_supported_bpp);
  1546. link_params = &panel->link->link_params;
  1547. data_rate_khz = link_params->lane_count *
  1548. drm_dp_bw_code_to_link_rate(link_params->bw_code) * 8;
  1549. for (; bpp > min_supported_bpp; bpp -= 6) {
  1550. if (dp_panel->dsc_en) {
  1551. if (bpp == 36 && !(dp_panel->sink_dsc_caps.color_depth
  1552. & DP_DSC_12_BPC))
  1553. continue;
  1554. else if (bpp == 30 &&
  1555. !(dp_panel->sink_dsc_caps.color_depth &
  1556. DP_DSC_10_BPC))
  1557. continue;
  1558. else if (bpp == 24 &&
  1559. !(dp_panel->sink_dsc_caps.color_depth &
  1560. DP_DSC_8_BPC))
  1561. continue;
  1562. }
  1563. if (mode_pclk_khz * bpp <= data_rate_khz)
  1564. break;
  1565. }
  1566. if (bpp < min_supported_bpp)
  1567. DP_ERR("bpp %d is below minimum supported bpp %d\n", bpp,
  1568. min_supported_bpp);
  1569. if (dp_panel->dsc_en && bpp != 24 && bpp != 30 && bpp != 36)
  1570. DP_ERR("bpp %d is not supported when dsc is enabled\n", bpp);
  1571. return bpp;
  1572. }
  1573. static u32 dp_panel_get_mode_bpp(struct dp_panel *dp_panel,
  1574. u32 mode_edid_bpp, u32 mode_pclk_khz)
  1575. {
  1576. struct dp_panel_private *panel;
  1577. u32 bpp = mode_edid_bpp;
  1578. if (!dp_panel || !mode_edid_bpp || !mode_pclk_khz) {
  1579. DP_ERR("invalid input\n");
  1580. return 0;
  1581. }
  1582. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  1583. if (dp_panel->video_test)
  1584. bpp = dp_link_bit_depth_to_bpp(
  1585. panel->link->test_video.test_bit_depth);
  1586. else
  1587. bpp = dp_panel_get_supported_bpp(dp_panel, mode_edid_bpp,
  1588. mode_pclk_khz);
  1589. return bpp;
  1590. }
  1591. static void dp_panel_set_test_mode(struct dp_panel_private *panel,
  1592. struct dp_display_mode *mode)
  1593. {
  1594. struct dp_panel_info *pinfo = NULL;
  1595. struct dp_link_test_video *test_info = NULL;
  1596. if (!panel) {
  1597. DP_ERR("invalid params\n");
  1598. return;
  1599. }
  1600. pinfo = &mode->timing;
  1601. test_info = &panel->link->test_video;
  1602. pinfo->h_active = test_info->test_h_width;
  1603. pinfo->h_sync_width = test_info->test_hsync_width;
  1604. pinfo->h_back_porch = test_info->test_h_start -
  1605. test_info->test_hsync_width;
  1606. pinfo->h_front_porch = test_info->test_h_total -
  1607. (test_info->test_h_start + test_info->test_h_width);
  1608. pinfo->v_active = test_info->test_v_height;
  1609. pinfo->v_sync_width = test_info->test_vsync_width;
  1610. pinfo->v_back_porch = test_info->test_v_start -
  1611. test_info->test_vsync_width;
  1612. pinfo->v_front_porch = test_info->test_v_total -
  1613. (test_info->test_v_start + test_info->test_v_height);
  1614. pinfo->bpp = dp_link_bit_depth_to_bpp(test_info->test_bit_depth);
  1615. pinfo->h_active_low = test_info->test_hsync_pol;
  1616. pinfo->v_active_low = test_info->test_vsync_pol;
  1617. pinfo->refresh_rate = test_info->test_rr_n;
  1618. pinfo->pixel_clk_khz = test_info->test_h_total *
  1619. test_info->test_v_total * pinfo->refresh_rate;
  1620. if (test_info->test_rr_d == 0)
  1621. pinfo->pixel_clk_khz /= 1000;
  1622. else
  1623. pinfo->pixel_clk_khz /= 1001;
  1624. if (test_info->test_h_width == 640)
  1625. pinfo->pixel_clk_khz = 25170;
  1626. }
  1627. static int dp_panel_get_modes(struct dp_panel *dp_panel,
  1628. struct drm_connector *connector, struct dp_display_mode *mode)
  1629. {
  1630. struct dp_panel_private *panel;
  1631. if (!dp_panel) {
  1632. DP_ERR("invalid input\n");
  1633. return -EINVAL;
  1634. }
  1635. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  1636. if (dp_panel->video_test) {
  1637. dp_panel_set_test_mode(panel, mode);
  1638. return 1;
  1639. } else if (dp_panel->edid_ctrl->edid) {
  1640. return _sde_edid_update_modes(connector, dp_panel->edid_ctrl);
  1641. }
  1642. /* fail-safe mode */
  1643. memcpy(&mode->timing, &fail_safe,
  1644. sizeof(fail_safe));
  1645. return 1;
  1646. }
  1647. static void dp_panel_handle_sink_request(struct dp_panel *dp_panel)
  1648. {
  1649. struct dp_panel_private *panel;
  1650. if (!dp_panel) {
  1651. DP_ERR("invalid input\n");
  1652. return;
  1653. }
  1654. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  1655. if (panel->link->sink_request & DP_TEST_LINK_EDID_READ) {
  1656. u8 checksum;
  1657. if (dp_panel->edid_ctrl->edid)
  1658. checksum = sde_get_edid_checksum(dp_panel->edid_ctrl);
  1659. else
  1660. checksum = dp_panel->connector->real_edid_checksum;
  1661. panel->link->send_edid_checksum(panel->link, checksum);
  1662. panel->link->send_test_response(panel->link);
  1663. }
  1664. }
  1665. static void dp_panel_tpg_config(struct dp_panel *dp_panel, bool enable)
  1666. {
  1667. u32 hsync_start_x, hsync_end_x;
  1668. struct dp_catalog_panel *catalog;
  1669. struct dp_panel_private *panel;
  1670. struct dp_panel_info *pinfo;
  1671. if (!dp_panel) {
  1672. DP_ERR("invalid input\n");
  1673. return;
  1674. }
  1675. if (dp_panel->stream_id >= DP_STREAM_MAX) {
  1676. DP_ERR("invalid stream id:%d\n", dp_panel->stream_id);
  1677. return;
  1678. }
  1679. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  1680. catalog = panel->catalog;
  1681. pinfo = &panel->dp_panel.pinfo;
  1682. if (!panel->panel_on) {
  1683. DP_DEBUG("DP panel not enabled, handle TPG on next panel on\n");
  1684. return;
  1685. }
  1686. if (!enable) {
  1687. panel->catalog->tpg_config(catalog, false);
  1688. return;
  1689. }
  1690. /* TPG config */
  1691. catalog->hsync_period = pinfo->h_sync_width + pinfo->h_back_porch +
  1692. pinfo->h_active + pinfo->h_front_porch;
  1693. catalog->vsync_period = pinfo->v_sync_width + pinfo->v_back_porch +
  1694. pinfo->v_active + pinfo->v_front_porch;
  1695. catalog->display_v_start = ((pinfo->v_sync_width +
  1696. pinfo->v_back_porch) * catalog->hsync_period);
  1697. catalog->display_v_end = ((catalog->vsync_period -
  1698. pinfo->v_front_porch) * catalog->hsync_period) - 1;
  1699. catalog->display_v_start += pinfo->h_sync_width + pinfo->h_back_porch;
  1700. catalog->display_v_end -= pinfo->h_front_porch;
  1701. hsync_start_x = pinfo->h_back_porch + pinfo->h_sync_width;
  1702. hsync_end_x = catalog->hsync_period - pinfo->h_front_porch - 1;
  1703. catalog->v_sync_width = pinfo->v_sync_width;
  1704. catalog->hsync_ctl = (catalog->hsync_period << 16) |
  1705. pinfo->h_sync_width;
  1706. catalog->display_hctl = (hsync_end_x << 16) | hsync_start_x;
  1707. panel->catalog->tpg_config(catalog, true);
  1708. }
  1709. static int dp_panel_config_timing(struct dp_panel *dp_panel)
  1710. {
  1711. int rc = 0;
  1712. u32 data, total_ver, total_hor;
  1713. struct dp_catalog_panel *catalog;
  1714. struct dp_panel_private *panel;
  1715. struct dp_panel_info *pinfo;
  1716. if (!dp_panel) {
  1717. DP_ERR("invalid input\n");
  1718. rc = -EINVAL;
  1719. goto end;
  1720. }
  1721. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  1722. catalog = panel->catalog;
  1723. pinfo = &panel->dp_panel.pinfo;
  1724. DP_DEBUG("width=%d hporch= %d %d %d\n",
  1725. pinfo->h_active, pinfo->h_back_porch,
  1726. pinfo->h_front_porch, pinfo->h_sync_width);
  1727. DP_DEBUG("height=%d vporch= %d %d %d\n",
  1728. pinfo->v_active, pinfo->v_back_porch,
  1729. pinfo->v_front_porch, pinfo->v_sync_width);
  1730. total_hor = pinfo->h_active + pinfo->h_back_porch +
  1731. pinfo->h_front_porch + pinfo->h_sync_width;
  1732. total_ver = pinfo->v_active + pinfo->v_back_porch +
  1733. pinfo->v_front_porch + pinfo->v_sync_width;
  1734. data = total_ver;
  1735. data <<= 16;
  1736. data |= total_hor;
  1737. catalog->total = data;
  1738. data = (pinfo->v_back_porch + pinfo->v_sync_width);
  1739. data <<= 16;
  1740. data |= (pinfo->h_back_porch + pinfo->h_sync_width);
  1741. catalog->sync_start = data;
  1742. data = pinfo->v_sync_width;
  1743. data <<= 16;
  1744. data |= (pinfo->v_active_low << 31);
  1745. data |= pinfo->h_sync_width;
  1746. data |= (pinfo->h_active_low << 15);
  1747. catalog->width_blanking = data;
  1748. data = pinfo->v_active;
  1749. data <<= 16;
  1750. data |= pinfo->h_active;
  1751. catalog->dp_active = data;
  1752. catalog->widebus_en = pinfo->widebus_en;
  1753. panel->catalog->timing_cfg(catalog);
  1754. panel->panel_on = true;
  1755. end:
  1756. return rc;
  1757. }
  1758. static u32 _dp_panel_calc_be_in_lane(struct dp_panel *dp_panel)
  1759. {
  1760. struct dp_panel_info *pinfo;
  1761. struct msm_compression_info *comp_info;
  1762. u32 dsc_htot_byte_cnt, mod_result;
  1763. u32 numerator, denominator;
  1764. s64 temp_fp;
  1765. u32 be_in_lane = 10;
  1766. pinfo = &dp_panel->pinfo;
  1767. comp_info = &pinfo->comp_info;
  1768. if (!dp_panel->mst_state)
  1769. return be_in_lane;
  1770. if (pinfo->comp_info.comp_ratio == DP_COMPRESSION_RATIO_2_TO_1)
  1771. denominator = 16; /* 2 * bits-in-byte */
  1772. else if (pinfo->comp_info.comp_ratio == DP_COMPRESSION_RATIO_3_TO_1)
  1773. denominator = 24; /* 3 * bits-in-byte */
  1774. else
  1775. denominator = 8;
  1776. numerator = (pinfo->h_active + pinfo->h_back_porch +
  1777. pinfo->h_front_porch + pinfo->h_sync_width) *
  1778. pinfo->bpp;
  1779. temp_fp = drm_fixp_from_fraction(numerator, denominator);
  1780. dsc_htot_byte_cnt = drm_fixp2int_ceil(temp_fp);
  1781. mod_result = dsc_htot_byte_cnt % 12;
  1782. if (mod_result == 0)
  1783. be_in_lane = 8;
  1784. else if (mod_result <= 3)
  1785. be_in_lane = 1;
  1786. else if (mod_result <= 6)
  1787. be_in_lane = 2;
  1788. else if (mod_result <= 9)
  1789. be_in_lane = 4;
  1790. else if (mod_result <= 11)
  1791. be_in_lane = 8;
  1792. else
  1793. be_in_lane = 10;
  1794. return be_in_lane;
  1795. }
  1796. static void dp_panel_config_dsc(struct dp_panel *dp_panel, bool enable)
  1797. {
  1798. struct dp_catalog_panel *catalog;
  1799. struct dp_panel_private *panel;
  1800. struct dp_panel_info *pinfo;
  1801. struct msm_compression_info *comp_info;
  1802. struct dp_dsc_cfg_data *dsc;
  1803. int rc;
  1804. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  1805. catalog = panel->catalog;
  1806. dsc = &catalog->dsc;
  1807. pinfo = &dp_panel->pinfo;
  1808. comp_info = &pinfo->comp_info;
  1809. if (comp_info->comp_type == MSM_DISPLAY_COMPRESSION_DSC && enable) {
  1810. rc = sde_dsc_create_pps_buf_cmd(&comp_info->dsc_info,
  1811. dsc->pps, 0, sizeof(dsc->pps));
  1812. if (rc) {
  1813. DP_ERR("failed to create pps cmd %d\n", rc);
  1814. return;
  1815. }
  1816. dsc->pps_len = DSC_1_1_PPS_PARAMETER_SET_ELEMENTS;
  1817. dp_panel_dsc_prepare_pps_packet(dp_panel);
  1818. dsc->slice_per_pkt = comp_info->dsc_info.slice_per_pkt - 1;
  1819. dsc->bytes_per_pkt = comp_info->dsc_info.bytes_per_pkt;
  1820. dsc->bytes_per_pkt /= comp_info->dsc_info.slice_per_pkt;
  1821. dsc->eol_byte_num = comp_info->dsc_info.eol_byte_num;
  1822. dsc->dto_count = comp_info->dsc_info.pclk_per_line;
  1823. dsc->be_in_lane = _dp_panel_calc_be_in_lane(dp_panel);
  1824. dsc->dsc_en = true;
  1825. dsc->dto_en = true;
  1826. dsc->continuous_pps = dp_panel->dsc_continuous_pps;
  1827. dp_panel_get_dto_params(comp_info->comp_ratio, &dsc->dto_n,
  1828. &dsc->dto_d, pinfo->bpp);
  1829. } else {
  1830. dsc->dsc_en = false;
  1831. dsc->dto_en = false;
  1832. dsc->dto_n = 0;
  1833. dsc->dto_d = 0;
  1834. dsc->continuous_pps = false;
  1835. }
  1836. catalog->stream_id = dp_panel->stream_id;
  1837. catalog->dsc_cfg(catalog);
  1838. if (catalog->dsc.dsc_en && enable)
  1839. catalog->pps_flush(catalog);
  1840. }
  1841. static int dp_panel_edid_register(struct dp_panel_private *panel)
  1842. {
  1843. int rc = 0;
  1844. panel->dp_panel.edid_ctrl = sde_edid_init();
  1845. if (!panel->dp_panel.edid_ctrl) {
  1846. DP_ERR("sde edid init for DP failed\n");
  1847. rc = -ENOMEM;
  1848. }
  1849. return rc;
  1850. }
  1851. static void dp_panel_edid_deregister(struct dp_panel_private *panel)
  1852. {
  1853. sde_edid_deinit((void **)&panel->dp_panel.edid_ctrl);
  1854. }
  1855. static int dp_panel_set_stream_info(struct dp_panel *dp_panel,
  1856. enum dp_stream_id stream_id, u32 ch_start_slot,
  1857. u32 ch_tot_slots, u32 pbn, int vcpi)
  1858. {
  1859. if (!dp_panel || stream_id > DP_STREAM_MAX) {
  1860. DP_ERR("invalid input. stream_id: %d\n", stream_id);
  1861. return -EINVAL;
  1862. }
  1863. dp_panel->vcpi = vcpi;
  1864. dp_panel->stream_id = stream_id;
  1865. dp_panel->channel_start_slot = ch_start_slot;
  1866. dp_panel->channel_total_slots = ch_tot_slots;
  1867. dp_panel->pbn = pbn;
  1868. return 0;
  1869. }
  1870. static int dp_panel_init_panel_info(struct dp_panel *dp_panel)
  1871. {
  1872. int rc = 0;
  1873. struct dp_panel_private *panel;
  1874. struct dp_panel_info *pinfo;
  1875. if (!dp_panel) {
  1876. DP_ERR("invalid input\n");
  1877. rc = -EINVAL;
  1878. goto end;
  1879. }
  1880. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  1881. pinfo = &dp_panel->pinfo;
  1882. drm_dp_dpcd_writeb(panel->aux->drm_aux, DP_SET_POWER, DP_SET_POWER_D3);
  1883. /* 200us propagation time for the power down to take effect */
  1884. usleep_range(200, 205);
  1885. drm_dp_dpcd_writeb(panel->aux->drm_aux, DP_SET_POWER, DP_SET_POWER_D0);
  1886. /*
  1887. * According to the DP 1.1 specification, a "Sink Device must exit the
  1888. * power saving state within 1 ms" (Section 2.5.3.1, Table 5-52, "Sink
  1889. * Control Field" (register 0x600).
  1890. */
  1891. usleep_range(1000, 2000);
  1892. end:
  1893. return rc;
  1894. }
  1895. static int dp_panel_deinit_panel_info(struct dp_panel *dp_panel, u32 flags)
  1896. {
  1897. int rc = 0;
  1898. struct dp_panel_private *panel;
  1899. struct drm_msm_ext_hdr_metadata *hdr_meta;
  1900. struct dp_sdp_header *dhdr_vsif_sdp;
  1901. struct sde_connector *sde_conn;
  1902. struct dp_sdp_header *shdr_if_sdp;
  1903. struct dp_catalog_vsc_sdp_colorimetry *vsc_colorimetry;
  1904. struct drm_connector *connector;
  1905. struct sde_connector_state *c_state;
  1906. if (flags & DP_PANEL_SRC_INITIATED_POWER_DOWN) {
  1907. DP_DEBUG("retain states in src initiated power down request\n");
  1908. return 0;
  1909. }
  1910. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  1911. hdr_meta = &panel->catalog->hdr_meta;
  1912. dhdr_vsif_sdp = &panel->catalog->dhdr_vsif_sdp;
  1913. shdr_if_sdp = &panel->catalog->shdr_if_sdp;
  1914. vsc_colorimetry = &panel->catalog->vsc_colorimetry;
  1915. if (!panel->custom_edid && dp_panel->edid_ctrl->edid)
  1916. sde_free_edid((void **)&dp_panel->edid_ctrl);
  1917. dp_panel_set_stream_info(dp_panel, DP_STREAM_MAX, 0, 0, 0, 0);
  1918. memset(&dp_panel->pinfo, 0, sizeof(dp_panel->pinfo));
  1919. memset(hdr_meta, 0, sizeof(struct drm_msm_ext_hdr_metadata));
  1920. memset(dhdr_vsif_sdp, 0, sizeof(struct dp_sdp_header));
  1921. memset(shdr_if_sdp, 0, sizeof(struct dp_sdp_header));
  1922. memset(vsc_colorimetry, 0,
  1923. sizeof(struct dp_catalog_vsc_sdp_colorimetry));
  1924. panel->panel_on = false;
  1925. connector = dp_panel->connector;
  1926. sde_conn = to_sde_connector(connector);
  1927. c_state = to_sde_connector_state(connector->state);
  1928. sde_conn->hdr_eotf = 0;
  1929. sde_conn->hdr_metadata_type_one = 0;
  1930. sde_conn->hdr_max_luminance = 0;
  1931. sde_conn->hdr_avg_luminance = 0;
  1932. sde_conn->hdr_min_luminance = 0;
  1933. sde_conn->hdr_supported = false;
  1934. sde_conn->hdr_plus_app_ver = 0;
  1935. sde_conn->colorspace_updated = false;
  1936. memset(&c_state->hdr_meta, 0, sizeof(c_state->hdr_meta));
  1937. memset(&c_state->dyn_hdr_meta, 0, sizeof(c_state->dyn_hdr_meta));
  1938. dp_panel->link_bw_code = 0;
  1939. dp_panel->lane_count = 0;
  1940. return rc;
  1941. }
  1942. static bool dp_panel_hdr_supported(struct dp_panel *dp_panel)
  1943. {
  1944. struct dp_panel_private *panel;
  1945. if (!dp_panel) {
  1946. DP_ERR("invalid input\n");
  1947. return false;
  1948. }
  1949. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  1950. return panel->major >= 1 && panel->vsc_supported &&
  1951. (panel->minor >= 4 || panel->vscext_supported);
  1952. }
  1953. static u32 dp_panel_calc_dhdr_pkt_limit(struct dp_panel *dp_panel,
  1954. struct dp_dhdr_maxpkt_calc_input *input)
  1955. {
  1956. s64 mdpclk_fp = drm_fixp_from_fraction(input->mdp_clk, 1000000);
  1957. s64 lclk_fp = drm_fixp_from_fraction(input->lclk, 1000);
  1958. s64 pclk_fp = drm_fixp_from_fraction(input->pclk, 1000);
  1959. s64 nlanes_fp = drm_int2fixp(input->nlanes);
  1960. s64 target_sc = input->mst_target_sc;
  1961. s64 hactive_fp = drm_int2fixp(input->h_active);
  1962. const s64 i1_fp = DRM_FIXED_ONE;
  1963. const s64 i2_fp = drm_int2fixp(2);
  1964. const s64 i10_fp = drm_int2fixp(10);
  1965. const s64 i56_fp = drm_int2fixp(56);
  1966. const s64 i64_fp = drm_int2fixp(64);
  1967. s64 mst_bw_fp = i1_fp;
  1968. s64 fec_factor_fp = i1_fp;
  1969. s64 mst_bw64_fp, mst_bw64_ceil_fp, nlanes56_fp;
  1970. u32 f1, f2, f3, f4, f5, deploy_period, target_period;
  1971. s64 f3_f5_slot_fp;
  1972. u32 calc_pkt_limit;
  1973. const u32 max_pkt_limit = 64;
  1974. if (input->fec_en && input->mst_en)
  1975. fec_factor_fp = drm_fixp_from_fraction(64000, 65537);
  1976. if (input->mst_en)
  1977. mst_bw_fp = drm_fixp_div(target_sc, i64_fp);
  1978. f1 = drm_fixp2int_ceil(drm_fixp_div(drm_fixp_mul(i10_fp, lclk_fp),
  1979. mdpclk_fp));
  1980. f2 = drm_fixp2int_ceil(drm_fixp_div(drm_fixp_mul(i2_fp, lclk_fp),
  1981. mdpclk_fp)) + drm_fixp2int_ceil(drm_fixp_div(
  1982. drm_fixp_mul(i1_fp, lclk_fp), mdpclk_fp));
  1983. mst_bw64_fp = drm_fixp_mul(mst_bw_fp, i64_fp);
  1984. if (drm_fixp2int(mst_bw64_fp) == 0)
  1985. f3_f5_slot_fp = drm_fixp_div(i1_fp, drm_int2fixp(
  1986. drm_fixp2int_ceil(drm_fixp_div(
  1987. i1_fp, mst_bw64_fp))));
  1988. else
  1989. f3_f5_slot_fp = drm_int2fixp(drm_fixp2int(mst_bw_fp));
  1990. mst_bw64_ceil_fp = drm_int2fixp(drm_fixp2int_ceil(mst_bw64_fp));
  1991. f3 = drm_fixp2int(drm_fixp_mul(drm_int2fixp(drm_fixp2int(
  1992. drm_fixp_div(i2_fp, f3_f5_slot_fp)) + 1),
  1993. (i64_fp - mst_bw64_ceil_fp))) + 2;
  1994. if (!input->mst_en) {
  1995. f4 = 1 + drm_fixp2int(drm_fixp_div(drm_int2fixp(50),
  1996. nlanes_fp)) + drm_fixp2int(drm_fixp_div(
  1997. nlanes_fp, i2_fp));
  1998. f5 = 0;
  1999. } else {
  2000. f4 = 0;
  2001. nlanes56_fp = drm_fixp_div(i56_fp, nlanes_fp);
  2002. f5 = drm_fixp2int(drm_fixp_mul(drm_int2fixp(drm_fixp2int(
  2003. drm_fixp_div(i1_fp + nlanes56_fp,
  2004. f3_f5_slot_fp)) + 1), (i64_fp -
  2005. mst_bw64_ceil_fp + i1_fp + nlanes56_fp)));
  2006. }
  2007. deploy_period = f1 + f2 + f3 + f4 + f5 + 19;
  2008. target_period = drm_fixp2int(drm_fixp_mul(fec_factor_fp, drm_fixp_mul(
  2009. hactive_fp, drm_fixp_div(lclk_fp, pclk_fp))));
  2010. calc_pkt_limit = target_period / deploy_period;
  2011. DP_DEBUG("input: %d, %d, %d, %d, %d, 0x%llx, %d, %d\n",
  2012. input->mdp_clk, input->lclk, input->pclk, input->h_active,
  2013. input->nlanes, input->mst_target_sc, input->mst_en ? 1 : 0,
  2014. input->fec_en ? 1 : 0);
  2015. DP_DEBUG("factors: %d, %d, %d, %d, %d\n", f1, f2, f3, f4, f5);
  2016. DP_DEBUG("d_p: %d, t_p: %d, maxPkts: %d%s\n", deploy_period,
  2017. target_period, calc_pkt_limit, calc_pkt_limit > max_pkt_limit ?
  2018. " CAPPED" : "");
  2019. if (calc_pkt_limit > max_pkt_limit)
  2020. calc_pkt_limit = max_pkt_limit;
  2021. DP_DEBUG("packet limit per line = %d\n", calc_pkt_limit);
  2022. return calc_pkt_limit;
  2023. }
  2024. static void dp_panel_setup_colorimetry_sdp(struct dp_panel *dp_panel,
  2025. u32 cspace)
  2026. {
  2027. struct dp_panel_private *panel;
  2028. struct dp_catalog_vsc_sdp_colorimetry *hdr_colorimetry;
  2029. u8 bpc;
  2030. u32 colorimetry = 0;
  2031. u32 dynamic_range = 0;
  2032. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  2033. hdr_colorimetry = &panel->catalog->vsc_colorimetry;
  2034. hdr_colorimetry->header.HB0 = 0x00;
  2035. hdr_colorimetry->header.HB1 = 0x07;
  2036. hdr_colorimetry->header.HB2 = 0x05;
  2037. hdr_colorimetry->header.HB3 = 0x13;
  2038. get_sdp_colorimetry_range(panel, cspace, &colorimetry,
  2039. &dynamic_range);
  2040. /* VSC SDP Payload for DB16 */
  2041. hdr_colorimetry->data[16] = (RGB << 4) | colorimetry;
  2042. /* VSC SDP Payload for DB17 */
  2043. hdr_colorimetry->data[17] = (dynamic_range << 7);
  2044. bpc = (dp_panel->pinfo.bpp / 3);
  2045. switch (bpc) {
  2046. default:
  2047. case 10:
  2048. hdr_colorimetry->data[17] |= BIT(1);
  2049. break;
  2050. case 8:
  2051. hdr_colorimetry->data[17] |= BIT(0);
  2052. break;
  2053. case 6:
  2054. hdr_colorimetry->data[17] |= 0;
  2055. break;
  2056. }
  2057. /* VSC SDP Payload for DB18 */
  2058. hdr_colorimetry->data[18] = GRAPHICS;
  2059. }
  2060. static void dp_panel_setup_hdr_if(struct dp_panel_private *panel)
  2061. {
  2062. struct dp_sdp_header *shdr_if;
  2063. shdr_if = &panel->catalog->shdr_if_sdp;
  2064. shdr_if->HB0 = 0x00;
  2065. shdr_if->HB1 = 0x87;
  2066. shdr_if->HB2 = 0x1D;
  2067. shdr_if->HB3 = 0x13 << 2;
  2068. }
  2069. static void dp_panel_setup_dhdr_vsif(struct dp_panel_private *panel)
  2070. {
  2071. struct dp_sdp_header *dhdr_vsif;
  2072. dhdr_vsif = &panel->catalog->dhdr_vsif_sdp;
  2073. dhdr_vsif->HB0 = 0x00;
  2074. dhdr_vsif->HB1 = 0x81;
  2075. dhdr_vsif->HB2 = 0x1D;
  2076. dhdr_vsif->HB3 = 0x13 << 2;
  2077. }
  2078. static void dp_panel_setup_misc_colorimetry(struct dp_panel *dp_panel,
  2079. u32 colorspace)
  2080. {
  2081. struct dp_panel_private *panel;
  2082. struct dp_catalog_panel *catalog;
  2083. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  2084. catalog = panel->catalog;
  2085. catalog->misc_val &= ~0x1e;
  2086. catalog->misc_val |= (get_misc_colorimetry_val(panel,
  2087. colorspace) << 1);
  2088. }
  2089. static int dp_panel_set_colorspace(struct dp_panel *dp_panel,
  2090. u32 colorspace)
  2091. {
  2092. int rc = 0;
  2093. struct dp_panel_private *panel;
  2094. if (!dp_panel) {
  2095. pr_err("invalid input\n");
  2096. rc = -EINVAL;
  2097. goto end;
  2098. }
  2099. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  2100. if (panel->vsc_supported)
  2101. dp_panel_setup_colorimetry_sdp(dp_panel,
  2102. colorspace);
  2103. else
  2104. dp_panel_setup_misc_colorimetry(dp_panel,
  2105. colorspace);
  2106. /*
  2107. * During the first frame update panel_on will be false and
  2108. * the colorspace will be cached in the connector's state which
  2109. * shall be used in the dp_panel_hw_cfg
  2110. */
  2111. if (panel->panel_on) {
  2112. DP_DEBUG("panel is ON programming colorspace\n");
  2113. rc = panel->catalog->set_colorspace(panel->catalog,
  2114. panel->vsc_supported);
  2115. }
  2116. end:
  2117. return rc;
  2118. }
  2119. static int dp_panel_setup_hdr(struct dp_panel *dp_panel,
  2120. struct drm_msm_ext_hdr_metadata *hdr_meta,
  2121. bool dhdr_update, u64 core_clk_rate, bool flush)
  2122. {
  2123. int rc = 0, max_pkts = 0;
  2124. struct dp_panel_private *panel;
  2125. struct dp_dhdr_maxpkt_calc_input input;
  2126. struct drm_msm_ext_hdr_metadata *catalog_hdr_meta;
  2127. if (!dp_panel) {
  2128. DP_ERR("invalid input\n");
  2129. rc = -EINVAL;
  2130. goto end;
  2131. }
  2132. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  2133. catalog_hdr_meta = &panel->catalog->hdr_meta;
  2134. /* use cached meta data in case meta data not provided */
  2135. if (!hdr_meta) {
  2136. if (catalog_hdr_meta->hdr_state)
  2137. goto cached;
  2138. else
  2139. goto end;
  2140. }
  2141. panel->hdr_state = hdr_meta->hdr_state;
  2142. dp_panel_setup_hdr_if(panel);
  2143. if (panel->hdr_state) {
  2144. memcpy(catalog_hdr_meta, hdr_meta,
  2145. sizeof(struct drm_msm_ext_hdr_metadata));
  2146. } else {
  2147. memset(catalog_hdr_meta, 0,
  2148. sizeof(struct drm_msm_ext_hdr_metadata));
  2149. }
  2150. cached:
  2151. if (dhdr_update) {
  2152. dp_panel_setup_dhdr_vsif(panel);
  2153. input.mdp_clk = core_clk_rate;
  2154. input.lclk = drm_dp_bw_code_to_link_rate(
  2155. panel->link->link_params.bw_code);
  2156. input.nlanes = panel->link->link_params.lane_count;
  2157. input.pclk = dp_panel->pinfo.pixel_clk_khz;
  2158. input.h_active = dp_panel->pinfo.h_active;
  2159. input.mst_target_sc = dp_panel->mst_target_sc;
  2160. input.mst_en = dp_panel->mst_state;
  2161. input.fec_en = dp_panel->fec_en;
  2162. max_pkts = dp_panel_calc_dhdr_pkt_limit(dp_panel, &input);
  2163. }
  2164. if (panel->panel_on) {
  2165. panel->catalog->stream_id = dp_panel->stream_id;
  2166. panel->catalog->config_hdr(panel->catalog, panel->hdr_state,
  2167. max_pkts, flush);
  2168. if (dhdr_update)
  2169. panel->catalog->dhdr_flush(panel->catalog);
  2170. }
  2171. end:
  2172. return rc;
  2173. }
  2174. static int dp_panel_spd_config(struct dp_panel *dp_panel)
  2175. {
  2176. int rc = 0;
  2177. struct dp_panel_private *panel;
  2178. if (!dp_panel) {
  2179. DP_ERR("invalid input\n");
  2180. rc = -EINVAL;
  2181. goto end;
  2182. }
  2183. if (dp_panel->stream_id >= DP_STREAM_MAX) {
  2184. DP_ERR("invalid stream id:%d\n", dp_panel->stream_id);
  2185. return -EINVAL;
  2186. }
  2187. if (!dp_panel->spd_enabled) {
  2188. DP_DEBUG("SPD Infoframe not enabled\n");
  2189. goto end;
  2190. }
  2191. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  2192. panel->catalog->spd_vendor_name = panel->spd_vendor_name;
  2193. panel->catalog->spd_product_description =
  2194. panel->spd_product_description;
  2195. panel->catalog->stream_id = dp_panel->stream_id;
  2196. panel->catalog->config_spd(panel->catalog);
  2197. end:
  2198. return rc;
  2199. }
  2200. static void dp_panel_config_ctrl(struct dp_panel *dp_panel)
  2201. {
  2202. u32 config = 0, tbd;
  2203. u8 *dpcd = dp_panel->dpcd;
  2204. struct dp_panel_private *panel;
  2205. struct dp_catalog_panel *catalog;
  2206. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  2207. catalog = panel->catalog;
  2208. config |= (2 << 13); /* Default-> LSCLK DIV: 1/4 LCLK */
  2209. config |= (0 << 11); /* RGB */
  2210. tbd = panel->link->get_test_bits_depth(panel->link,
  2211. dp_panel->pinfo.bpp);
  2212. if (tbd == DP_TEST_BIT_DEPTH_UNKNOWN || dp_panel->dsc_en)
  2213. tbd = (DP_TEST_BIT_DEPTH_8 >> DP_TEST_BIT_DEPTH_SHIFT);
  2214. config |= tbd << 8;
  2215. /* Num of Lanes */
  2216. config |= ((panel->link->link_params.lane_count - 1) << 4);
  2217. if (drm_dp_enhanced_frame_cap(dpcd))
  2218. config |= 0x40;
  2219. config |= 0x04; /* progressive video */
  2220. config |= 0x03; /* sycn clock & static Mvid */
  2221. catalog->config_ctrl(catalog, config);
  2222. }
  2223. static void dp_panel_config_misc(struct dp_panel *dp_panel)
  2224. {
  2225. struct dp_panel_private *panel;
  2226. struct dp_catalog_panel *catalog;
  2227. struct drm_connector *connector;
  2228. u32 misc_val;
  2229. u32 tb, cc, colorspace;
  2230. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  2231. catalog = panel->catalog;
  2232. connector = dp_panel->connector;
  2233. cc = 0;
  2234. tb = panel->link->get_test_bits_depth(panel->link, dp_panel->pinfo.bpp);
  2235. colorspace = connector->state->colorspace;
  2236. cc = (get_misc_colorimetry_val(panel, colorspace) << 1);
  2237. misc_val = cc;
  2238. misc_val |= (tb << 5);
  2239. misc_val |= BIT(0); /* Configure clock to synchronous mode */
  2240. /* if VSC is supported then set bit 6 of MISC1 */
  2241. if (panel->vsc_supported)
  2242. misc_val |= BIT(14);
  2243. catalog->misc_val = misc_val;
  2244. catalog->config_misc(catalog);
  2245. }
  2246. static void dp_panel_config_msa(struct dp_panel *dp_panel)
  2247. {
  2248. struct dp_panel_private *panel;
  2249. struct dp_catalog_panel *catalog;
  2250. u32 rate;
  2251. u32 stream_rate_khz;
  2252. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  2253. catalog = panel->catalog;
  2254. catalog->widebus_en = dp_panel->widebus_en;
  2255. rate = drm_dp_bw_code_to_link_rate(panel->link->link_params.bw_code);
  2256. stream_rate_khz = dp_panel->pinfo.pixel_clk_khz;
  2257. catalog->config_msa(catalog, rate, stream_rate_khz);
  2258. }
  2259. static void dp_panel_resolution_info(struct dp_panel_private *panel)
  2260. {
  2261. struct dp_panel_info *pinfo = &panel->dp_panel.pinfo;
  2262. /*
  2263. * print resolution info as this is a result
  2264. * of user initiated action of cable connection
  2265. */
  2266. DP_INFO("DP RESOLUTION: active(back|front|width|low)\n");
  2267. DP_INFO("%d(%d|%d|%d|%d)x%d(%d|%d|%d|%d)@%dfps %dbpp %dKhz %dLR %dLn\n",
  2268. pinfo->h_active, pinfo->h_back_porch, pinfo->h_front_porch,
  2269. pinfo->h_sync_width, pinfo->h_active_low,
  2270. pinfo->v_active, pinfo->v_back_porch, pinfo->v_front_porch,
  2271. pinfo->v_sync_width, pinfo->v_active_low,
  2272. pinfo->refresh_rate, pinfo->bpp, pinfo->pixel_clk_khz,
  2273. panel->link->link_params.bw_code,
  2274. panel->link->link_params.lane_count);
  2275. }
  2276. static void dp_panel_config_sdp(struct dp_panel *dp_panel,
  2277. bool en)
  2278. {
  2279. struct dp_panel_private *panel;
  2280. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  2281. panel->catalog->stream_id = dp_panel->stream_id;
  2282. panel->catalog->config_sdp(panel->catalog, en);
  2283. }
  2284. static int dp_panel_hw_cfg(struct dp_panel *dp_panel, bool enable)
  2285. {
  2286. struct dp_panel_private *panel;
  2287. struct drm_connector *connector;
  2288. if (!dp_panel) {
  2289. DP_ERR("invalid input\n");
  2290. return -EINVAL;
  2291. }
  2292. if (dp_panel->stream_id >= DP_STREAM_MAX) {
  2293. DP_ERR("invalid stream_id: %d\n", dp_panel->stream_id);
  2294. return -EINVAL;
  2295. }
  2296. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  2297. panel->catalog->stream_id = dp_panel->stream_id;
  2298. connector = dp_panel->connector;
  2299. if (enable) {
  2300. dp_panel_config_ctrl(dp_panel);
  2301. dp_panel_config_misc(dp_panel);
  2302. dp_panel_config_msa(dp_panel);
  2303. if (panel->vsc_supported) {
  2304. dp_panel_setup_colorimetry_sdp(dp_panel,
  2305. connector->state->colorspace);
  2306. dp_panel_config_sdp(dp_panel, true);
  2307. }
  2308. dp_panel_config_dsc(dp_panel, enable);
  2309. dp_panel_config_tr_unit(dp_panel);
  2310. dp_panel_config_timing(dp_panel);
  2311. dp_panel_resolution_info(panel);
  2312. } else {
  2313. dp_panel_config_sdp(dp_panel, false);
  2314. }
  2315. panel->catalog->config_dto(panel->catalog, !enable);
  2316. return 0;
  2317. }
  2318. static int dp_panel_read_sink_sts(struct dp_panel *dp_panel, u8 *sts, u32 size)
  2319. {
  2320. int rlen, rc = 0;
  2321. struct dp_panel_private *panel;
  2322. if (!dp_panel || !sts || !size) {
  2323. DP_ERR("invalid input\n");
  2324. rc = -EINVAL;
  2325. return rc;
  2326. }
  2327. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  2328. rlen = drm_dp_dpcd_read(panel->aux->drm_aux, DP_SINK_COUNT_ESI,
  2329. sts, size);
  2330. if (rlen != size) {
  2331. DP_ERR("dpcd sink sts fail rlen:%d size:%d\n", rlen, size);
  2332. rc = -EINVAL;
  2333. return rc;
  2334. }
  2335. return 0;
  2336. }
  2337. static int dp_panel_update_edid(struct dp_panel *dp_panel, struct edid *edid)
  2338. {
  2339. int rc;
  2340. dp_panel->edid_ctrl->edid = edid;
  2341. sde_parse_edid(dp_panel->edid_ctrl);
  2342. rc = _sde_edid_update_modes(dp_panel->connector, dp_panel->edid_ctrl);
  2343. dp_panel->audio_supported = drm_detect_monitor_audio(edid);
  2344. return rc;
  2345. }
  2346. static bool dp_panel_read_mst_cap(struct dp_panel *dp_panel)
  2347. {
  2348. int rlen;
  2349. struct dp_panel_private *panel;
  2350. u8 dpcd;
  2351. bool mst_cap = false;
  2352. if (!dp_panel) {
  2353. DP_ERR("invalid input\n");
  2354. return 0;
  2355. }
  2356. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  2357. rlen = drm_dp_dpcd_read(panel->aux->drm_aux, DP_MSTM_CAP,
  2358. &dpcd, 1);
  2359. if (rlen < 1) {
  2360. DP_ERR("dpcd mstm_cap read failed, rlen=%d\n", rlen);
  2361. goto end;
  2362. }
  2363. mst_cap = (dpcd & DP_MST_CAP) ? true : false;
  2364. end:
  2365. DP_DEBUG("dp mst-cap: %d\n", mst_cap);
  2366. return mst_cap;
  2367. }
  2368. static void dp_panel_convert_to_dp_mode(struct dp_panel *dp_panel,
  2369. const struct drm_display_mode *drm_mode,
  2370. struct dp_display_mode *dp_mode)
  2371. {
  2372. const u32 num_components = 3, default_bpp = 24;
  2373. struct msm_compression_info *comp_info;
  2374. bool dsc_cap = (dp_mode->capabilities & DP_PANEL_CAPS_DSC) ?
  2375. true : false;
  2376. int rc;
  2377. dp_mode->timing.h_active = drm_mode->hdisplay;
  2378. dp_mode->timing.h_back_porch = drm_mode->htotal - drm_mode->hsync_end;
  2379. dp_mode->timing.h_sync_width = drm_mode->htotal -
  2380. (drm_mode->hsync_start + dp_mode->timing.h_back_porch);
  2381. dp_mode->timing.h_front_porch = drm_mode->hsync_start -
  2382. drm_mode->hdisplay;
  2383. dp_mode->timing.h_skew = drm_mode->hskew;
  2384. dp_mode->timing.v_active = drm_mode->vdisplay;
  2385. dp_mode->timing.v_back_porch = drm_mode->vtotal - drm_mode->vsync_end;
  2386. dp_mode->timing.v_sync_width = drm_mode->vtotal -
  2387. (drm_mode->vsync_start + dp_mode->timing.v_back_porch);
  2388. dp_mode->timing.v_front_porch = drm_mode->vsync_start -
  2389. drm_mode->vdisplay;
  2390. dp_mode->timing.refresh_rate = drm_mode->vrefresh;
  2391. dp_mode->timing.pixel_clk_khz = drm_mode->clock;
  2392. dp_mode->timing.v_active_low =
  2393. !!(drm_mode->flags & DRM_MODE_FLAG_NVSYNC);
  2394. dp_mode->timing.h_active_low =
  2395. !!(drm_mode->flags & DRM_MODE_FLAG_NHSYNC);
  2396. dp_mode->timing.bpp =
  2397. dp_panel->connector->display_info.bpc * num_components;
  2398. if (!dp_mode->timing.bpp)
  2399. dp_mode->timing.bpp = default_bpp;
  2400. dp_mode->timing.bpp = dp_panel_get_mode_bpp(dp_panel,
  2401. dp_mode->timing.bpp, dp_mode->timing.pixel_clk_khz);
  2402. dp_mode->timing.widebus_en = dp_panel->widebus_en;
  2403. dp_mode->timing.dsc_overhead_fp = 0;
  2404. comp_info = &dp_mode->timing.comp_info;
  2405. comp_info->comp_ratio = DP_COMPRESSION_RATIO_NONE;
  2406. comp_info->comp_type = MSM_DISPLAY_COMPRESSION_NONE;
  2407. if (dp_panel->dsc_en && dsc_cap) {
  2408. if (dp_panel_dsc_prepare_basic_params(comp_info,
  2409. dp_mode, dp_panel)) {
  2410. DP_DEBUG("prepare DSC basic params failed\n");
  2411. return;
  2412. }
  2413. rc = sde_dsc_populate_dsc_config(&comp_info->dsc_info.config, 0);
  2414. if (rc) {
  2415. DP_DEBUG("failed populating dsc params \n");
  2416. return;
  2417. }
  2418. rc = sde_dsc_populate_dsc_private_params(&comp_info->dsc_info,
  2419. dp_mode->timing.h_active);
  2420. if (rc) {
  2421. DP_DEBUG("failed populating other dsc params\n");
  2422. return;
  2423. }
  2424. dp_panel_dsc_pclk_param_calc(dp_panel,
  2425. &comp_info->dsc_info,
  2426. comp_info->comp_ratio,
  2427. dp_mode);
  2428. }
  2429. dp_mode->fec_overhead_fp = dp_panel->fec_overhead_fp;
  2430. }
  2431. static void dp_panel_update_pps(struct dp_panel *dp_panel, char *pps_cmd)
  2432. {
  2433. struct dp_catalog_panel *catalog;
  2434. struct dp_panel_private *panel;
  2435. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  2436. catalog = panel->catalog;
  2437. catalog->stream_id = dp_panel->stream_id;
  2438. catalog->pps_flush(catalog);
  2439. }
  2440. struct dp_panel *dp_panel_get(struct dp_panel_in *in)
  2441. {
  2442. int rc = 0;
  2443. struct dp_panel_private *panel;
  2444. struct dp_panel *dp_panel;
  2445. struct sde_connector *sde_conn;
  2446. if (!in->dev || !in->catalog || !in->aux ||
  2447. !in->link || !in->connector) {
  2448. DP_ERR("invalid input\n");
  2449. rc = -EINVAL;
  2450. goto error;
  2451. }
  2452. panel = devm_kzalloc(in->dev, sizeof(*panel), GFP_KERNEL);
  2453. if (!panel) {
  2454. rc = -ENOMEM;
  2455. goto error;
  2456. }
  2457. panel->dev = in->dev;
  2458. panel->aux = in->aux;
  2459. panel->catalog = in->catalog;
  2460. panel->link = in->link;
  2461. panel->parser = in->parser;
  2462. dp_panel = &panel->dp_panel;
  2463. dp_panel->max_bw_code = DP_LINK_BW_8_1;
  2464. dp_panel->spd_enabled = true;
  2465. dp_panel->link_bw_code = 0;
  2466. dp_panel->lane_count = 0;
  2467. memcpy(panel->spd_vendor_name, vendor_name, (sizeof(u8) * 8));
  2468. memcpy(panel->spd_product_description, product_desc, (sizeof(u8) * 16));
  2469. dp_panel->connector = in->connector;
  2470. dp_panel->dsc_feature_enable = panel->parser->dsc_feature_enable;
  2471. dp_panel->fec_feature_enable = panel->parser->fec_feature_enable;
  2472. dp_panel->dsc_continuous_pps = panel->parser->dsc_continuous_pps;
  2473. if (in->base_panel) {
  2474. memcpy(dp_panel->dpcd, in->base_panel->dpcd,
  2475. DP_RECEIVER_CAP_SIZE + 1);
  2476. memcpy(dp_panel->dsc_dpcd, in->base_panel->dsc_dpcd,
  2477. DP_RECEIVER_DSC_CAP_SIZE + 1);
  2478. memcpy(&dp_panel->link_info, &in->base_panel->link_info,
  2479. sizeof(dp_panel->link_info));
  2480. dp_panel->mst_state = in->base_panel->mst_state;
  2481. dp_panel->widebus_en = in->base_panel->widebus_en;
  2482. dp_panel->fec_en = in->base_panel->fec_en;
  2483. dp_panel->dsc_en = in->base_panel->dsc_en;
  2484. dp_panel->fec_overhead_fp = in->base_panel->fec_overhead_fp;
  2485. }
  2486. dp_panel->init = dp_panel_init_panel_info;
  2487. dp_panel->deinit = dp_panel_deinit_panel_info;
  2488. dp_panel->hw_cfg = dp_panel_hw_cfg;
  2489. dp_panel->read_sink_caps = dp_panel_read_sink_caps;
  2490. dp_panel->get_mode_bpp = dp_panel_get_mode_bpp;
  2491. dp_panel->get_modes = dp_panel_get_modes;
  2492. dp_panel->handle_sink_request = dp_panel_handle_sink_request;
  2493. dp_panel->set_edid = dp_panel_set_edid;
  2494. dp_panel->set_dpcd = dp_panel_set_dpcd;
  2495. dp_panel->tpg_config = dp_panel_tpg_config;
  2496. dp_panel->spd_config = dp_panel_spd_config;
  2497. dp_panel->setup_hdr = dp_panel_setup_hdr;
  2498. dp_panel->set_colorspace = dp_panel_set_colorspace;
  2499. dp_panel->hdr_supported = dp_panel_hdr_supported;
  2500. dp_panel->set_stream_info = dp_panel_set_stream_info;
  2501. dp_panel->read_sink_status = dp_panel_read_sink_sts;
  2502. dp_panel->update_edid = dp_panel_update_edid;
  2503. dp_panel->read_mst_cap = dp_panel_read_mst_cap;
  2504. dp_panel->convert_to_dp_mode = dp_panel_convert_to_dp_mode;
  2505. dp_panel->update_pps = dp_panel_update_pps;
  2506. sde_conn = to_sde_connector(dp_panel->connector);
  2507. sde_conn->drv_panel = dp_panel;
  2508. dp_panel_edid_register(panel);
  2509. return dp_panel;
  2510. error:
  2511. return ERR_PTR(rc);
  2512. }
  2513. void dp_panel_put(struct dp_panel *dp_panel)
  2514. {
  2515. struct dp_panel_private *panel;
  2516. struct sde_connector *sde_conn;
  2517. if (!dp_panel)
  2518. return;
  2519. panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
  2520. dp_panel_edid_deregister(panel);
  2521. sde_conn = to_sde_connector(dp_panel->connector);
  2522. if (sde_conn)
  2523. sde_conn->drv_panel = NULL;
  2524. devm_kfree(panel->dev, panel);
  2525. }