dp_panel.c 81 KB

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