dp_panel.c 81 KB

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