dp_panel.c 83 KB

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