dp_panel.c 81 KB

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