drm_dp_helper.c 121 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312
  1. /*
  2. * Copyright © 2009 Keith Packard
  3. *
  4. * Permission to use, copy, modify, distribute, and sell this software and its
  5. * documentation for any purpose is hereby granted without fee, provided that
  6. * the above copyright notice appear in all copies and that both that copyright
  7. * notice and this permission notice appear in supporting documentation, and
  8. * that the name of the copyright holders not be used in advertising or
  9. * publicity pertaining to distribution of the software without specific,
  10. * written prior permission. The copyright holders make no representations
  11. * about the suitability of this software for any purpose. It is provided "as
  12. * is" without express or implied warranty.
  13. *
  14. * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  16. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  19. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  20. * OF THIS SOFTWARE.
  21. */
  22. #include <linux/backlight.h>
  23. #include <linux/delay.h>
  24. #include <linux/errno.h>
  25. #include <linux/i2c.h>
  26. #include <linux/init.h>
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/sched.h>
  30. #include <linux/seq_file.h>
  31. #include <linux/string_helpers.h>
  32. #include <linux/dynamic_debug.h>
  33. #include <drm/display/drm_dp_helper.h>
  34. #include <drm/display/drm_dp_mst_helper.h>
  35. #include <drm/drm_edid.h>
  36. #include <drm/drm_fixed.h>
  37. #include <drm/drm_print.h>
  38. #include <drm/drm_vblank.h>
  39. #include <drm/drm_panel.h>
  40. #include "drm_dp_helper_internal.h"
  41. DECLARE_DYNDBG_CLASSMAP(drm_debug_classes, DD_CLASS_TYPE_DISJOINT_BITS, 0,
  42. "DRM_UT_CORE",
  43. "DRM_UT_DRIVER",
  44. "DRM_UT_KMS",
  45. "DRM_UT_PRIME",
  46. "DRM_UT_ATOMIC",
  47. "DRM_UT_VBL",
  48. "DRM_UT_STATE",
  49. "DRM_UT_LEASE",
  50. "DRM_UT_DP",
  51. "DRM_UT_DRMRES");
  52. struct dp_aux_backlight {
  53. struct backlight_device *base;
  54. struct drm_dp_aux *aux;
  55. struct drm_edp_backlight_info info;
  56. bool enabled;
  57. };
  58. /**
  59. * DOC: dp helpers
  60. *
  61. * These functions contain some common logic and helpers at various abstraction
  62. * levels to deal with Display Port sink devices and related things like DP aux
  63. * channel transfers, EDID reading over DP aux channels, decoding certain DPCD
  64. * blocks, ...
  65. */
  66. /* Helpers for DP link training */
  67. static u8 dp_link_status(const u8 link_status[DP_LINK_STATUS_SIZE], int r)
  68. {
  69. return link_status[r - DP_LANE0_1_STATUS];
  70. }
  71. static u8 dp_get_lane_status(const u8 link_status[DP_LINK_STATUS_SIZE],
  72. int lane)
  73. {
  74. int i = DP_LANE0_1_STATUS + (lane >> 1);
  75. int s = (lane & 1) * 4;
  76. u8 l = dp_link_status(link_status, i);
  77. return (l >> s) & 0xf;
  78. }
  79. bool drm_dp_channel_eq_ok(const u8 link_status[DP_LINK_STATUS_SIZE],
  80. int lane_count)
  81. {
  82. u8 lane_align;
  83. u8 lane_status;
  84. int lane;
  85. lane_align = dp_link_status(link_status,
  86. DP_LANE_ALIGN_STATUS_UPDATED);
  87. if ((lane_align & DP_INTERLANE_ALIGN_DONE) == 0)
  88. return false;
  89. for (lane = 0; lane < lane_count; lane++) {
  90. lane_status = dp_get_lane_status(link_status, lane);
  91. if ((lane_status & DP_CHANNEL_EQ_BITS) != DP_CHANNEL_EQ_BITS)
  92. return false;
  93. }
  94. return true;
  95. }
  96. EXPORT_SYMBOL(drm_dp_channel_eq_ok);
  97. bool drm_dp_clock_recovery_ok(const u8 link_status[DP_LINK_STATUS_SIZE],
  98. int lane_count)
  99. {
  100. int lane;
  101. u8 lane_status;
  102. for (lane = 0; lane < lane_count; lane++) {
  103. lane_status = dp_get_lane_status(link_status, lane);
  104. if ((lane_status & DP_LANE_CR_DONE) == 0)
  105. return false;
  106. }
  107. return true;
  108. }
  109. EXPORT_SYMBOL(drm_dp_clock_recovery_ok);
  110. u8 drm_dp_get_adjust_request_voltage(const u8 link_status[DP_LINK_STATUS_SIZE],
  111. int lane)
  112. {
  113. int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
  114. int s = ((lane & 1) ?
  115. DP_ADJUST_VOLTAGE_SWING_LANE1_SHIFT :
  116. DP_ADJUST_VOLTAGE_SWING_LANE0_SHIFT);
  117. u8 l = dp_link_status(link_status, i);
  118. return ((l >> s) & 0x3) << DP_TRAIN_VOLTAGE_SWING_SHIFT;
  119. }
  120. EXPORT_SYMBOL(drm_dp_get_adjust_request_voltage);
  121. u8 drm_dp_get_adjust_request_pre_emphasis(const u8 link_status[DP_LINK_STATUS_SIZE],
  122. int lane)
  123. {
  124. int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
  125. int s = ((lane & 1) ?
  126. DP_ADJUST_PRE_EMPHASIS_LANE1_SHIFT :
  127. DP_ADJUST_PRE_EMPHASIS_LANE0_SHIFT);
  128. u8 l = dp_link_status(link_status, i);
  129. return ((l >> s) & 0x3) << DP_TRAIN_PRE_EMPHASIS_SHIFT;
  130. }
  131. EXPORT_SYMBOL(drm_dp_get_adjust_request_pre_emphasis);
  132. /* DP 2.0 128b/132b */
  133. u8 drm_dp_get_adjust_tx_ffe_preset(const u8 link_status[DP_LINK_STATUS_SIZE],
  134. int lane)
  135. {
  136. int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
  137. int s = ((lane & 1) ?
  138. DP_ADJUST_TX_FFE_PRESET_LANE1_SHIFT :
  139. DP_ADJUST_TX_FFE_PRESET_LANE0_SHIFT);
  140. u8 l = dp_link_status(link_status, i);
  141. return (l >> s) & 0xf;
  142. }
  143. EXPORT_SYMBOL(drm_dp_get_adjust_tx_ffe_preset);
  144. /* DP 2.0 errata for 128b/132b */
  145. bool drm_dp_128b132b_lane_channel_eq_done(const u8 link_status[DP_LINK_STATUS_SIZE],
  146. int lane_count)
  147. {
  148. u8 lane_align, lane_status;
  149. int lane;
  150. lane_align = dp_link_status(link_status, DP_LANE_ALIGN_STATUS_UPDATED);
  151. if (!(lane_align & DP_INTERLANE_ALIGN_DONE))
  152. return false;
  153. for (lane = 0; lane < lane_count; lane++) {
  154. lane_status = dp_get_lane_status(link_status, lane);
  155. if (!(lane_status & DP_LANE_CHANNEL_EQ_DONE))
  156. return false;
  157. }
  158. return true;
  159. }
  160. EXPORT_SYMBOL(drm_dp_128b132b_lane_channel_eq_done);
  161. /* DP 2.0 errata for 128b/132b */
  162. bool drm_dp_128b132b_lane_symbol_locked(const u8 link_status[DP_LINK_STATUS_SIZE],
  163. int lane_count)
  164. {
  165. u8 lane_status;
  166. int lane;
  167. for (lane = 0; lane < lane_count; lane++) {
  168. lane_status = dp_get_lane_status(link_status, lane);
  169. if (!(lane_status & DP_LANE_SYMBOL_LOCKED))
  170. return false;
  171. }
  172. return true;
  173. }
  174. EXPORT_SYMBOL(drm_dp_128b132b_lane_symbol_locked);
  175. /* DP 2.0 errata for 128b/132b */
  176. bool drm_dp_128b132b_eq_interlane_align_done(const u8 link_status[DP_LINK_STATUS_SIZE])
  177. {
  178. u8 status = dp_link_status(link_status, DP_LANE_ALIGN_STATUS_UPDATED);
  179. return status & DP_128B132B_DPRX_EQ_INTERLANE_ALIGN_DONE;
  180. }
  181. EXPORT_SYMBOL(drm_dp_128b132b_eq_interlane_align_done);
  182. /* DP 2.0 errata for 128b/132b */
  183. bool drm_dp_128b132b_cds_interlane_align_done(const u8 link_status[DP_LINK_STATUS_SIZE])
  184. {
  185. u8 status = dp_link_status(link_status, DP_LANE_ALIGN_STATUS_UPDATED);
  186. return status & DP_128B132B_DPRX_CDS_INTERLANE_ALIGN_DONE;
  187. }
  188. EXPORT_SYMBOL(drm_dp_128b132b_cds_interlane_align_done);
  189. /* DP 2.0 errata for 128b/132b */
  190. bool drm_dp_128b132b_link_training_failed(const u8 link_status[DP_LINK_STATUS_SIZE])
  191. {
  192. u8 status = dp_link_status(link_status, DP_LANE_ALIGN_STATUS_UPDATED);
  193. return status & DP_128B132B_LT_FAILED;
  194. }
  195. EXPORT_SYMBOL(drm_dp_128b132b_link_training_failed);
  196. static int __8b10b_clock_recovery_delay_us(const struct drm_dp_aux *aux, u8 rd_interval)
  197. {
  198. if (rd_interval > 4)
  199. drm_dbg_kms(aux->drm_dev, "%s: invalid AUX interval 0x%02x (max 4)\n",
  200. aux->name, rd_interval);
  201. if (rd_interval == 0)
  202. return 100;
  203. return rd_interval * 4 * USEC_PER_MSEC;
  204. }
  205. static int __8b10b_channel_eq_delay_us(const struct drm_dp_aux *aux, u8 rd_interval)
  206. {
  207. if (rd_interval > 4)
  208. drm_dbg_kms(aux->drm_dev, "%s: invalid AUX interval 0x%02x (max 4)\n",
  209. aux->name, rd_interval);
  210. if (rd_interval == 0)
  211. return 400;
  212. return rd_interval * 4 * USEC_PER_MSEC;
  213. }
  214. static int __128b132b_channel_eq_delay_us(const struct drm_dp_aux *aux, u8 rd_interval)
  215. {
  216. switch (rd_interval) {
  217. default:
  218. drm_dbg_kms(aux->drm_dev, "%s: invalid AUX interval 0x%02x\n",
  219. aux->name, rd_interval);
  220. fallthrough;
  221. case DP_128B132B_TRAINING_AUX_RD_INTERVAL_400_US:
  222. return 400;
  223. case DP_128B132B_TRAINING_AUX_RD_INTERVAL_4_MS:
  224. return 4000;
  225. case DP_128B132B_TRAINING_AUX_RD_INTERVAL_8_MS:
  226. return 8000;
  227. case DP_128B132B_TRAINING_AUX_RD_INTERVAL_12_MS:
  228. return 12000;
  229. case DP_128B132B_TRAINING_AUX_RD_INTERVAL_16_MS:
  230. return 16000;
  231. case DP_128B132B_TRAINING_AUX_RD_INTERVAL_32_MS:
  232. return 32000;
  233. case DP_128B132B_TRAINING_AUX_RD_INTERVAL_64_MS:
  234. return 64000;
  235. }
  236. }
  237. /*
  238. * The link training delays are different for:
  239. *
  240. * - Clock recovery vs. channel equalization
  241. * - DPRX vs. LTTPR
  242. * - 128b/132b vs. 8b/10b
  243. * - DPCD rev 1.3 vs. later
  244. *
  245. * Get the correct delay in us, reading DPCD if necessary.
  246. */
  247. static int __read_delay(struct drm_dp_aux *aux, const u8 dpcd[DP_RECEIVER_CAP_SIZE],
  248. enum drm_dp_phy dp_phy, bool uhbr, bool cr)
  249. {
  250. int (*parse)(const struct drm_dp_aux *aux, u8 rd_interval);
  251. unsigned int offset;
  252. u8 rd_interval, mask;
  253. if (dp_phy == DP_PHY_DPRX) {
  254. if (uhbr) {
  255. if (cr)
  256. return 100;
  257. offset = DP_128B132B_TRAINING_AUX_RD_INTERVAL;
  258. mask = DP_128B132B_TRAINING_AUX_RD_INTERVAL_MASK;
  259. parse = __128b132b_channel_eq_delay_us;
  260. } else {
  261. if (cr && dpcd[DP_DPCD_REV] >= DP_DPCD_REV_14)
  262. return 100;
  263. offset = DP_TRAINING_AUX_RD_INTERVAL;
  264. mask = DP_TRAINING_AUX_RD_MASK;
  265. if (cr)
  266. parse = __8b10b_clock_recovery_delay_us;
  267. else
  268. parse = __8b10b_channel_eq_delay_us;
  269. }
  270. } else {
  271. if (uhbr) {
  272. offset = DP_128B132B_TRAINING_AUX_RD_INTERVAL_PHY_REPEATER(dp_phy);
  273. mask = DP_128B132B_TRAINING_AUX_RD_INTERVAL_MASK;
  274. parse = __128b132b_channel_eq_delay_us;
  275. } else {
  276. if (cr)
  277. return 100;
  278. offset = DP_TRAINING_AUX_RD_INTERVAL_PHY_REPEATER(dp_phy);
  279. mask = DP_TRAINING_AUX_RD_MASK;
  280. parse = __8b10b_channel_eq_delay_us;
  281. }
  282. }
  283. if (offset < DP_RECEIVER_CAP_SIZE) {
  284. rd_interval = dpcd[offset];
  285. } else {
  286. if (drm_dp_dpcd_readb(aux, offset, &rd_interval) != 1) {
  287. drm_dbg_kms(aux->drm_dev, "%s: failed rd interval read\n",
  288. aux->name);
  289. /* arbitrary default delay */
  290. return 400;
  291. }
  292. }
  293. return parse(aux, rd_interval & mask);
  294. }
  295. int drm_dp_read_clock_recovery_delay(struct drm_dp_aux *aux, const u8 dpcd[DP_RECEIVER_CAP_SIZE],
  296. enum drm_dp_phy dp_phy, bool uhbr)
  297. {
  298. return __read_delay(aux, dpcd, dp_phy, uhbr, true);
  299. }
  300. EXPORT_SYMBOL(drm_dp_read_clock_recovery_delay);
  301. int drm_dp_read_channel_eq_delay(struct drm_dp_aux *aux, const u8 dpcd[DP_RECEIVER_CAP_SIZE],
  302. enum drm_dp_phy dp_phy, bool uhbr)
  303. {
  304. return __read_delay(aux, dpcd, dp_phy, uhbr, false);
  305. }
  306. EXPORT_SYMBOL(drm_dp_read_channel_eq_delay);
  307. /* Per DP 2.0 Errata */
  308. int drm_dp_128b132b_read_aux_rd_interval(struct drm_dp_aux *aux)
  309. {
  310. int unit;
  311. u8 val;
  312. if (drm_dp_dpcd_readb(aux, DP_128B132B_TRAINING_AUX_RD_INTERVAL, &val) != 1) {
  313. drm_err(aux->drm_dev, "%s: failed rd interval read\n",
  314. aux->name);
  315. /* default to max */
  316. val = DP_128B132B_TRAINING_AUX_RD_INTERVAL_MASK;
  317. }
  318. unit = (val & DP_128B132B_TRAINING_AUX_RD_INTERVAL_1MS_UNIT) ? 1 : 2;
  319. val &= DP_128B132B_TRAINING_AUX_RD_INTERVAL_MASK;
  320. return (val + 1) * unit * 1000;
  321. }
  322. EXPORT_SYMBOL(drm_dp_128b132b_read_aux_rd_interval);
  323. void drm_dp_link_train_clock_recovery_delay(const struct drm_dp_aux *aux,
  324. const u8 dpcd[DP_RECEIVER_CAP_SIZE])
  325. {
  326. u8 rd_interval = dpcd[DP_TRAINING_AUX_RD_INTERVAL] &
  327. DP_TRAINING_AUX_RD_MASK;
  328. int delay_us;
  329. if (dpcd[DP_DPCD_REV] >= DP_DPCD_REV_14)
  330. delay_us = 100;
  331. else
  332. delay_us = __8b10b_clock_recovery_delay_us(aux, rd_interval);
  333. usleep_range(delay_us, delay_us * 2);
  334. }
  335. EXPORT_SYMBOL(drm_dp_link_train_clock_recovery_delay);
  336. static void __drm_dp_link_train_channel_eq_delay(const struct drm_dp_aux *aux,
  337. u8 rd_interval)
  338. {
  339. int delay_us = __8b10b_channel_eq_delay_us(aux, rd_interval);
  340. usleep_range(delay_us, delay_us * 2);
  341. }
  342. void drm_dp_link_train_channel_eq_delay(const struct drm_dp_aux *aux,
  343. const u8 dpcd[DP_RECEIVER_CAP_SIZE])
  344. {
  345. __drm_dp_link_train_channel_eq_delay(aux,
  346. dpcd[DP_TRAINING_AUX_RD_INTERVAL] &
  347. DP_TRAINING_AUX_RD_MASK);
  348. }
  349. EXPORT_SYMBOL(drm_dp_link_train_channel_eq_delay);
  350. /**
  351. * drm_dp_phy_name() - Get the name of the given DP PHY
  352. * @dp_phy: The DP PHY identifier
  353. *
  354. * Given the @dp_phy, get a user friendly name of the DP PHY, either "DPRX" or
  355. * "LTTPR <N>", or "<INVALID DP PHY>" on errors. The returned string is always
  356. * non-NULL and valid.
  357. *
  358. * Returns: Name of the DP PHY.
  359. */
  360. const char *drm_dp_phy_name(enum drm_dp_phy dp_phy)
  361. {
  362. static const char * const phy_names[] = {
  363. [DP_PHY_DPRX] = "DPRX",
  364. [DP_PHY_LTTPR1] = "LTTPR 1",
  365. [DP_PHY_LTTPR2] = "LTTPR 2",
  366. [DP_PHY_LTTPR3] = "LTTPR 3",
  367. [DP_PHY_LTTPR4] = "LTTPR 4",
  368. [DP_PHY_LTTPR5] = "LTTPR 5",
  369. [DP_PHY_LTTPR6] = "LTTPR 6",
  370. [DP_PHY_LTTPR7] = "LTTPR 7",
  371. [DP_PHY_LTTPR8] = "LTTPR 8",
  372. };
  373. if (dp_phy < 0 || dp_phy >= ARRAY_SIZE(phy_names) ||
  374. WARN_ON(!phy_names[dp_phy]))
  375. return "<INVALID DP PHY>";
  376. return phy_names[dp_phy];
  377. }
  378. EXPORT_SYMBOL(drm_dp_phy_name);
  379. void drm_dp_lttpr_link_train_clock_recovery_delay(void)
  380. {
  381. usleep_range(100, 200);
  382. }
  383. EXPORT_SYMBOL(drm_dp_lttpr_link_train_clock_recovery_delay);
  384. static u8 dp_lttpr_phy_cap(const u8 phy_cap[DP_LTTPR_PHY_CAP_SIZE], int r)
  385. {
  386. return phy_cap[r - DP_TRAINING_AUX_RD_INTERVAL_PHY_REPEATER1];
  387. }
  388. void drm_dp_lttpr_link_train_channel_eq_delay(const struct drm_dp_aux *aux,
  389. const u8 phy_cap[DP_LTTPR_PHY_CAP_SIZE])
  390. {
  391. u8 interval = dp_lttpr_phy_cap(phy_cap,
  392. DP_TRAINING_AUX_RD_INTERVAL_PHY_REPEATER1) &
  393. DP_TRAINING_AUX_RD_MASK;
  394. __drm_dp_link_train_channel_eq_delay(aux, interval);
  395. }
  396. EXPORT_SYMBOL(drm_dp_lttpr_link_train_channel_eq_delay);
  397. u8 drm_dp_link_rate_to_bw_code(int link_rate)
  398. {
  399. switch (link_rate) {
  400. case 1000000:
  401. return DP_LINK_BW_10;
  402. case 1350000:
  403. return DP_LINK_BW_13_5;
  404. case 2000000:
  405. return DP_LINK_BW_20;
  406. default:
  407. /* Spec says link_bw = link_rate / 0.27Gbps */
  408. return link_rate / 27000;
  409. }
  410. }
  411. EXPORT_SYMBOL(drm_dp_link_rate_to_bw_code);
  412. int drm_dp_bw_code_to_link_rate(u8 link_bw)
  413. {
  414. switch (link_bw) {
  415. case DP_LINK_BW_10:
  416. return 1000000;
  417. case DP_LINK_BW_13_5:
  418. return 1350000;
  419. case DP_LINK_BW_20:
  420. return 2000000;
  421. default:
  422. /* Spec says link_rate = link_bw * 0.27Gbps */
  423. return link_bw * 27000;
  424. }
  425. }
  426. EXPORT_SYMBOL(drm_dp_bw_code_to_link_rate);
  427. #define AUX_RETRY_INTERVAL 500 /* us */
  428. static inline void
  429. drm_dp_dump_access(const struct drm_dp_aux *aux,
  430. u8 request, uint offset, void *buffer, int ret)
  431. {
  432. const char *arrow = request == DP_AUX_NATIVE_READ ? "->" : "<-";
  433. if (ret > 0)
  434. drm_dbg_dp(aux->drm_dev, "%s: 0x%05x AUX %s (ret=%3d) %*ph\n",
  435. aux->name, offset, arrow, ret, min(ret, 20), buffer);
  436. else
  437. drm_dbg_dp(aux->drm_dev, "%s: 0x%05x AUX %s (ret=%3d)\n",
  438. aux->name, offset, arrow, ret);
  439. }
  440. /**
  441. * DOC: dp helpers
  442. *
  443. * The DisplayPort AUX channel is an abstraction to allow generic, driver-
  444. * independent access to AUX functionality. Drivers can take advantage of
  445. * this by filling in the fields of the drm_dp_aux structure.
  446. *
  447. * Transactions are described using a hardware-independent drm_dp_aux_msg
  448. * structure, which is passed into a driver's .transfer() implementation.
  449. * Both native and I2C-over-AUX transactions are supported.
  450. */
  451. static int drm_dp_dpcd_access(struct drm_dp_aux *aux, u8 request,
  452. unsigned int offset, void *buffer, size_t size)
  453. {
  454. struct drm_dp_aux_msg msg;
  455. unsigned int retry, native_reply;
  456. int err = 0, ret = 0;
  457. memset(&msg, 0, sizeof(msg));
  458. msg.address = offset;
  459. msg.request = request;
  460. msg.buffer = buffer;
  461. msg.size = size;
  462. mutex_lock(&aux->hw_mutex);
  463. /*
  464. * If the device attached to the aux bus is powered down then there's
  465. * no reason to attempt a transfer. Error out immediately.
  466. */
  467. if (aux->powered_down) {
  468. ret = -EBUSY;
  469. goto unlock;
  470. }
  471. /*
  472. * The specification doesn't give any recommendation on how often to
  473. * retry native transactions. We used to retry 7 times like for
  474. * aux i2c transactions but real world devices this wasn't
  475. * sufficient, bump to 32 which makes Dell 4k monitors happier.
  476. */
  477. for (retry = 0; retry < 32; retry++) {
  478. if (ret != 0 && ret != -ETIMEDOUT) {
  479. usleep_range(AUX_RETRY_INTERVAL,
  480. AUX_RETRY_INTERVAL + 100);
  481. }
  482. ret = aux->transfer(aux, &msg);
  483. if (ret >= 0) {
  484. native_reply = msg.reply & DP_AUX_NATIVE_REPLY_MASK;
  485. if (native_reply == DP_AUX_NATIVE_REPLY_ACK) {
  486. if (ret == size)
  487. goto unlock;
  488. ret = -EPROTO;
  489. } else
  490. ret = -EIO;
  491. }
  492. /*
  493. * We want the error we return to be the error we received on
  494. * the first transaction, since we may get a different error the
  495. * next time we retry
  496. */
  497. if (!err)
  498. err = ret;
  499. }
  500. drm_dbg_kms(aux->drm_dev, "%s: Too many retries, giving up. First error: %d\n",
  501. aux->name, err);
  502. ret = err;
  503. unlock:
  504. mutex_unlock(&aux->hw_mutex);
  505. return ret;
  506. }
  507. /**
  508. * drm_dp_dpcd_probe() - probe a given DPCD address with a 1-byte read access
  509. * @aux: DisplayPort AUX channel (SST)
  510. * @offset: address of the register to probe
  511. *
  512. * Probe the provided DPCD address by reading 1 byte from it. The function can
  513. * be used to trigger some side-effect the read access has, like waking up the
  514. * sink, without the need for the read-out value.
  515. *
  516. * Returns 0 if the read access suceeded, or a negative error code on failure.
  517. */
  518. int drm_dp_dpcd_probe(struct drm_dp_aux *aux, unsigned int offset)
  519. {
  520. u8 buffer;
  521. int ret;
  522. ret = drm_dp_dpcd_access(aux, DP_AUX_NATIVE_READ, offset, &buffer, 1);
  523. WARN_ON(ret == 0);
  524. drm_dp_dump_access(aux, DP_AUX_NATIVE_READ, offset, &buffer, ret);
  525. return ret < 0 ? ret : 0;
  526. }
  527. EXPORT_SYMBOL(drm_dp_dpcd_probe);
  528. /**
  529. * drm_dp_dpcd_set_powered() - Set whether the DP device is powered
  530. * @aux: DisplayPort AUX channel; for convenience it's OK to pass NULL here
  531. * and the function will be a no-op.
  532. * @powered: true if powered; false if not
  533. *
  534. * If the endpoint device on the DP AUX bus is known to be powered down
  535. * then this function can be called to make future transfers fail immediately
  536. * instead of needing to time out.
  537. *
  538. * If this function is never called then a device defaults to being powered.
  539. */
  540. void drm_dp_dpcd_set_powered(struct drm_dp_aux *aux, bool powered)
  541. {
  542. if (!aux)
  543. return;
  544. mutex_lock(&aux->hw_mutex);
  545. aux->powered_down = !powered;
  546. mutex_unlock(&aux->hw_mutex);
  547. }
  548. EXPORT_SYMBOL(drm_dp_dpcd_set_powered);
  549. /**
  550. * drm_dp_dpcd_read() - read a series of bytes from the DPCD
  551. * @aux: DisplayPort AUX channel (SST or MST)
  552. * @offset: address of the (first) register to read
  553. * @buffer: buffer to store the register values
  554. * @size: number of bytes in @buffer
  555. *
  556. * Returns the number of bytes transferred on success, or a negative error
  557. * code on failure. -EIO is returned if the request was NAKed by the sink or
  558. * if the retry count was exceeded. If not all bytes were transferred, this
  559. * function returns -EPROTO. Errors from the underlying AUX channel transfer
  560. * function, with the exception of -EBUSY (which causes the transaction to
  561. * be retried), are propagated to the caller.
  562. */
  563. ssize_t drm_dp_dpcd_read(struct drm_dp_aux *aux, unsigned int offset,
  564. void *buffer, size_t size)
  565. {
  566. int ret;
  567. /*
  568. * HP ZR24w corrupts the first DPCD access after entering power save
  569. * mode. Eg. on a read, the entire buffer will be filled with the same
  570. * byte. Do a throw away read to avoid corrupting anything we care
  571. * about. Afterwards things will work correctly until the monitor
  572. * gets woken up and subsequently re-enters power save mode.
  573. *
  574. * The user pressing any button on the monitor is enough to wake it
  575. * up, so there is no particularly good place to do the workaround.
  576. * We just have to do it before any DPCD access and hope that the
  577. * monitor doesn't power down exactly after the throw away read.
  578. */
  579. if (!aux->is_remote) {
  580. ret = drm_dp_dpcd_probe(aux, DP_DPCD_REV);
  581. if (ret < 0)
  582. return ret;
  583. }
  584. if (aux->is_remote)
  585. ret = drm_dp_mst_dpcd_read(aux, offset, buffer, size);
  586. else
  587. ret = drm_dp_dpcd_access(aux, DP_AUX_NATIVE_READ, offset,
  588. buffer, size);
  589. drm_dp_dump_access(aux, DP_AUX_NATIVE_READ, offset, buffer, ret);
  590. return ret;
  591. }
  592. EXPORT_SYMBOL(drm_dp_dpcd_read);
  593. /**
  594. * drm_dp_dpcd_write() - write a series of bytes to the DPCD
  595. * @aux: DisplayPort AUX channel (SST or MST)
  596. * @offset: address of the (first) register to write
  597. * @buffer: buffer containing the values to write
  598. * @size: number of bytes in @buffer
  599. *
  600. * Returns the number of bytes transferred on success, or a negative error
  601. * code on failure. -EIO is returned if the request was NAKed by the sink or
  602. * if the retry count was exceeded. If not all bytes were transferred, this
  603. * function returns -EPROTO. Errors from the underlying AUX channel transfer
  604. * function, with the exception of -EBUSY (which causes the transaction to
  605. * be retried), are propagated to the caller.
  606. */
  607. ssize_t drm_dp_dpcd_write(struct drm_dp_aux *aux, unsigned int offset,
  608. void *buffer, size_t size)
  609. {
  610. int ret;
  611. if (aux->is_remote)
  612. ret = drm_dp_mst_dpcd_write(aux, offset, buffer, size);
  613. else
  614. ret = drm_dp_dpcd_access(aux, DP_AUX_NATIVE_WRITE, offset,
  615. buffer, size);
  616. drm_dp_dump_access(aux, DP_AUX_NATIVE_WRITE, offset, buffer, ret);
  617. return ret;
  618. }
  619. EXPORT_SYMBOL(drm_dp_dpcd_write);
  620. /**
  621. * drm_dp_dpcd_read_link_status() - read DPCD link status (bytes 0x202-0x207)
  622. * @aux: DisplayPort AUX channel
  623. * @status: buffer to store the link status in (must be at least 6 bytes)
  624. *
  625. * Returns the number of bytes transferred on success or a negative error
  626. * code on failure.
  627. */
  628. int drm_dp_dpcd_read_link_status(struct drm_dp_aux *aux,
  629. u8 status[DP_LINK_STATUS_SIZE])
  630. {
  631. return drm_dp_dpcd_read(aux, DP_LANE0_1_STATUS, status,
  632. DP_LINK_STATUS_SIZE);
  633. }
  634. EXPORT_SYMBOL(drm_dp_dpcd_read_link_status);
  635. /**
  636. * drm_dp_dpcd_read_phy_link_status - get the link status information for a DP PHY
  637. * @aux: DisplayPort AUX channel
  638. * @dp_phy: the DP PHY to get the link status for
  639. * @link_status: buffer to return the status in
  640. *
  641. * Fetch the AUX DPCD registers for the DPRX or an LTTPR PHY link status. The
  642. * layout of the returned @link_status matches the DPCD register layout of the
  643. * DPRX PHY link status.
  644. *
  645. * Returns 0 if the information was read successfully or a negative error code
  646. * on failure.
  647. */
  648. int drm_dp_dpcd_read_phy_link_status(struct drm_dp_aux *aux,
  649. enum drm_dp_phy dp_phy,
  650. u8 link_status[DP_LINK_STATUS_SIZE])
  651. {
  652. int ret;
  653. if (dp_phy == DP_PHY_DPRX) {
  654. ret = drm_dp_dpcd_read(aux,
  655. DP_LANE0_1_STATUS,
  656. link_status,
  657. DP_LINK_STATUS_SIZE);
  658. if (ret < 0)
  659. return ret;
  660. WARN_ON(ret != DP_LINK_STATUS_SIZE);
  661. return 0;
  662. }
  663. ret = drm_dp_dpcd_read(aux,
  664. DP_LANE0_1_STATUS_PHY_REPEATER(dp_phy),
  665. link_status,
  666. DP_LINK_STATUS_SIZE - 1);
  667. if (ret < 0)
  668. return ret;
  669. WARN_ON(ret != DP_LINK_STATUS_SIZE - 1);
  670. /* Convert the LTTPR to the sink PHY link status layout */
  671. memmove(&link_status[DP_SINK_STATUS - DP_LANE0_1_STATUS + 1],
  672. &link_status[DP_SINK_STATUS - DP_LANE0_1_STATUS],
  673. DP_LINK_STATUS_SIZE - (DP_SINK_STATUS - DP_LANE0_1_STATUS) - 1);
  674. link_status[DP_SINK_STATUS - DP_LANE0_1_STATUS] = 0;
  675. return 0;
  676. }
  677. EXPORT_SYMBOL(drm_dp_dpcd_read_phy_link_status);
  678. static bool is_edid_digital_input_dp(const struct drm_edid *drm_edid)
  679. {
  680. /* FIXME: get rid of drm_edid_raw() */
  681. const struct edid *edid = drm_edid_raw(drm_edid);
  682. return edid && edid->revision >= 4 &&
  683. edid->input & DRM_EDID_INPUT_DIGITAL &&
  684. (edid->input & DRM_EDID_DIGITAL_TYPE_MASK) == DRM_EDID_DIGITAL_TYPE_DP;
  685. }
  686. /**
  687. * drm_dp_downstream_is_type() - is the downstream facing port of certain type?
  688. * @dpcd: DisplayPort configuration data
  689. * @port_cap: port capabilities
  690. * @type: port type to be checked. Can be:
  691. * %DP_DS_PORT_TYPE_DP, %DP_DS_PORT_TYPE_VGA, %DP_DS_PORT_TYPE_DVI,
  692. * %DP_DS_PORT_TYPE_HDMI, %DP_DS_PORT_TYPE_NON_EDID,
  693. * %DP_DS_PORT_TYPE_DP_DUALMODE or %DP_DS_PORT_TYPE_WIRELESS.
  694. *
  695. * Caveat: Only works with DPCD 1.1+ port caps.
  696. *
  697. * Returns: whether the downstream facing port matches the type.
  698. */
  699. bool drm_dp_downstream_is_type(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
  700. const u8 port_cap[4], u8 type)
  701. {
  702. return drm_dp_is_branch(dpcd) &&
  703. dpcd[DP_DPCD_REV] >= 0x11 &&
  704. (port_cap[0] & DP_DS_PORT_TYPE_MASK) == type;
  705. }
  706. EXPORT_SYMBOL(drm_dp_downstream_is_type);
  707. /**
  708. * drm_dp_downstream_is_tmds() - is the downstream facing port TMDS?
  709. * @dpcd: DisplayPort configuration data
  710. * @port_cap: port capabilities
  711. * @drm_edid: EDID
  712. *
  713. * Returns: whether the downstream facing port is TMDS (HDMI/DVI).
  714. */
  715. bool drm_dp_downstream_is_tmds(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
  716. const u8 port_cap[4],
  717. const struct drm_edid *drm_edid)
  718. {
  719. if (dpcd[DP_DPCD_REV] < 0x11) {
  720. switch (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_TYPE_MASK) {
  721. case DP_DWN_STRM_PORT_TYPE_TMDS:
  722. return true;
  723. default:
  724. return false;
  725. }
  726. }
  727. switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
  728. case DP_DS_PORT_TYPE_DP_DUALMODE:
  729. if (is_edid_digital_input_dp(drm_edid))
  730. return false;
  731. fallthrough;
  732. case DP_DS_PORT_TYPE_DVI:
  733. case DP_DS_PORT_TYPE_HDMI:
  734. return true;
  735. default:
  736. return false;
  737. }
  738. }
  739. EXPORT_SYMBOL(drm_dp_downstream_is_tmds);
  740. /**
  741. * drm_dp_send_real_edid_checksum() - send back real edid checksum value
  742. * @aux: DisplayPort AUX channel
  743. * @real_edid_checksum: real edid checksum for the last block
  744. *
  745. * Returns:
  746. * True on success
  747. */
  748. bool drm_dp_send_real_edid_checksum(struct drm_dp_aux *aux,
  749. u8 real_edid_checksum)
  750. {
  751. u8 link_edid_read = 0, auto_test_req = 0, test_resp = 0;
  752. if (drm_dp_dpcd_read(aux, DP_DEVICE_SERVICE_IRQ_VECTOR,
  753. &auto_test_req, 1) < 1) {
  754. drm_err(aux->drm_dev, "%s: DPCD failed read at register 0x%x\n",
  755. aux->name, DP_DEVICE_SERVICE_IRQ_VECTOR);
  756. return false;
  757. }
  758. auto_test_req &= DP_AUTOMATED_TEST_REQUEST;
  759. if (drm_dp_dpcd_read(aux, DP_TEST_REQUEST, &link_edid_read, 1) < 1) {
  760. drm_err(aux->drm_dev, "%s: DPCD failed read at register 0x%x\n",
  761. aux->name, DP_TEST_REQUEST);
  762. return false;
  763. }
  764. link_edid_read &= DP_TEST_LINK_EDID_READ;
  765. if (!auto_test_req || !link_edid_read) {
  766. drm_dbg_kms(aux->drm_dev, "%s: Source DUT does not support TEST_EDID_READ\n",
  767. aux->name);
  768. return false;
  769. }
  770. if (drm_dp_dpcd_write(aux, DP_DEVICE_SERVICE_IRQ_VECTOR,
  771. &auto_test_req, 1) < 1) {
  772. drm_err(aux->drm_dev, "%s: DPCD failed write at register 0x%x\n",
  773. aux->name, DP_DEVICE_SERVICE_IRQ_VECTOR);
  774. return false;
  775. }
  776. /* send back checksum for the last edid extension block data */
  777. if (drm_dp_dpcd_write(aux, DP_TEST_EDID_CHECKSUM,
  778. &real_edid_checksum, 1) < 1) {
  779. drm_err(aux->drm_dev, "%s: DPCD failed write at register 0x%x\n",
  780. aux->name, DP_TEST_EDID_CHECKSUM);
  781. return false;
  782. }
  783. test_resp |= DP_TEST_EDID_CHECKSUM_WRITE;
  784. if (drm_dp_dpcd_write(aux, DP_TEST_RESPONSE, &test_resp, 1) < 1) {
  785. drm_err(aux->drm_dev, "%s: DPCD failed write at register 0x%x\n",
  786. aux->name, DP_TEST_RESPONSE);
  787. return false;
  788. }
  789. return true;
  790. }
  791. EXPORT_SYMBOL(drm_dp_send_real_edid_checksum);
  792. static u8 drm_dp_downstream_port_count(const u8 dpcd[DP_RECEIVER_CAP_SIZE])
  793. {
  794. u8 port_count = dpcd[DP_DOWN_STREAM_PORT_COUNT] & DP_PORT_COUNT_MASK;
  795. if (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE && port_count > 4)
  796. port_count = 4;
  797. return port_count;
  798. }
  799. static int drm_dp_read_extended_dpcd_caps(struct drm_dp_aux *aux,
  800. u8 dpcd[DP_RECEIVER_CAP_SIZE])
  801. {
  802. u8 dpcd_ext[DP_RECEIVER_CAP_SIZE];
  803. int ret;
  804. /*
  805. * Prior to DP1.3 the bit represented by
  806. * DP_EXTENDED_RECEIVER_CAP_FIELD_PRESENT was reserved.
  807. * If it is set DP_DPCD_REV at 0000h could be at a value less than
  808. * the true capability of the panel. The only way to check is to
  809. * then compare 0000h and 2200h.
  810. */
  811. if (!(dpcd[DP_TRAINING_AUX_RD_INTERVAL] &
  812. DP_EXTENDED_RECEIVER_CAP_FIELD_PRESENT))
  813. return 0;
  814. ret = drm_dp_dpcd_read(aux, DP_DP13_DPCD_REV, &dpcd_ext,
  815. sizeof(dpcd_ext));
  816. if (ret < 0)
  817. return ret;
  818. if (ret != sizeof(dpcd_ext))
  819. return -EIO;
  820. if (dpcd[DP_DPCD_REV] > dpcd_ext[DP_DPCD_REV]) {
  821. drm_dbg_kms(aux->drm_dev,
  822. "%s: Extended DPCD rev less than base DPCD rev (%d > %d)\n",
  823. aux->name, dpcd[DP_DPCD_REV], dpcd_ext[DP_DPCD_REV]);
  824. return 0;
  825. }
  826. if (!memcmp(dpcd, dpcd_ext, sizeof(dpcd_ext)))
  827. return 0;
  828. drm_dbg_kms(aux->drm_dev, "%s: Base DPCD: %*ph\n", aux->name, DP_RECEIVER_CAP_SIZE, dpcd);
  829. memcpy(dpcd, dpcd_ext, sizeof(dpcd_ext));
  830. return 0;
  831. }
  832. /**
  833. * drm_dp_read_dpcd_caps() - read DPCD caps and extended DPCD caps if
  834. * available
  835. * @aux: DisplayPort AUX channel
  836. * @dpcd: Buffer to store the resulting DPCD in
  837. *
  838. * Attempts to read the base DPCD caps for @aux. Additionally, this function
  839. * checks for and reads the extended DPRX caps (%DP_DP13_DPCD_REV) if
  840. * present.
  841. *
  842. * Returns: %0 if the DPCD was read successfully, negative error code
  843. * otherwise.
  844. */
  845. int drm_dp_read_dpcd_caps(struct drm_dp_aux *aux,
  846. u8 dpcd[DP_RECEIVER_CAP_SIZE])
  847. {
  848. int ret;
  849. ret = drm_dp_dpcd_read(aux, DP_DPCD_REV, dpcd, DP_RECEIVER_CAP_SIZE);
  850. if (ret < 0)
  851. return ret;
  852. if (ret != DP_RECEIVER_CAP_SIZE || dpcd[DP_DPCD_REV] == 0)
  853. return -EIO;
  854. ret = drm_dp_read_extended_dpcd_caps(aux, dpcd);
  855. if (ret < 0)
  856. return ret;
  857. drm_dbg_kms(aux->drm_dev, "%s: DPCD: %*ph\n", aux->name, DP_RECEIVER_CAP_SIZE, dpcd);
  858. return ret;
  859. }
  860. EXPORT_SYMBOL(drm_dp_read_dpcd_caps);
  861. /**
  862. * drm_dp_read_downstream_info() - read DPCD downstream port info if available
  863. * @aux: DisplayPort AUX channel
  864. * @dpcd: A cached copy of the port's DPCD
  865. * @downstream_ports: buffer to store the downstream port info in
  866. *
  867. * See also:
  868. * drm_dp_downstream_max_clock()
  869. * drm_dp_downstream_max_bpc()
  870. *
  871. * Returns: 0 if either the downstream port info was read successfully or
  872. * there was no downstream info to read, or a negative error code otherwise.
  873. */
  874. int drm_dp_read_downstream_info(struct drm_dp_aux *aux,
  875. const u8 dpcd[DP_RECEIVER_CAP_SIZE],
  876. u8 downstream_ports[DP_MAX_DOWNSTREAM_PORTS])
  877. {
  878. int ret;
  879. u8 len;
  880. memset(downstream_ports, 0, DP_MAX_DOWNSTREAM_PORTS);
  881. /* No downstream info to read */
  882. if (!drm_dp_is_branch(dpcd) || dpcd[DP_DPCD_REV] == DP_DPCD_REV_10)
  883. return 0;
  884. /* Some branches advertise having 0 downstream ports, despite also advertising they have a
  885. * downstream port present. The DP spec isn't clear on if this is allowed or not, but since
  886. * some branches do it we need to handle it regardless.
  887. */
  888. len = drm_dp_downstream_port_count(dpcd);
  889. if (!len)
  890. return 0;
  891. if (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE)
  892. len *= 4;
  893. ret = drm_dp_dpcd_read(aux, DP_DOWNSTREAM_PORT_0, downstream_ports, len);
  894. if (ret < 0)
  895. return ret;
  896. if (ret != len)
  897. return -EIO;
  898. drm_dbg_kms(aux->drm_dev, "%s: DPCD DFP: %*ph\n", aux->name, len, downstream_ports);
  899. return 0;
  900. }
  901. EXPORT_SYMBOL(drm_dp_read_downstream_info);
  902. /**
  903. * drm_dp_downstream_max_dotclock() - extract downstream facing port max dot clock
  904. * @dpcd: DisplayPort configuration data
  905. * @port_cap: port capabilities
  906. *
  907. * Returns: Downstream facing port max dot clock in kHz on success,
  908. * or 0 if max clock not defined
  909. */
  910. int drm_dp_downstream_max_dotclock(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
  911. const u8 port_cap[4])
  912. {
  913. if (!drm_dp_is_branch(dpcd))
  914. return 0;
  915. if (dpcd[DP_DPCD_REV] < 0x11)
  916. return 0;
  917. switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
  918. case DP_DS_PORT_TYPE_VGA:
  919. if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
  920. return 0;
  921. return port_cap[1] * 8000;
  922. default:
  923. return 0;
  924. }
  925. }
  926. EXPORT_SYMBOL(drm_dp_downstream_max_dotclock);
  927. /**
  928. * drm_dp_downstream_max_tmds_clock() - extract downstream facing port max TMDS clock
  929. * @dpcd: DisplayPort configuration data
  930. * @port_cap: port capabilities
  931. * @drm_edid: EDID
  932. *
  933. * Returns: HDMI/DVI downstream facing port max TMDS clock in kHz on success,
  934. * or 0 if max TMDS clock not defined
  935. */
  936. int drm_dp_downstream_max_tmds_clock(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
  937. const u8 port_cap[4],
  938. const struct drm_edid *drm_edid)
  939. {
  940. if (!drm_dp_is_branch(dpcd))
  941. return 0;
  942. if (dpcd[DP_DPCD_REV] < 0x11) {
  943. switch (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_TYPE_MASK) {
  944. case DP_DWN_STRM_PORT_TYPE_TMDS:
  945. return 165000;
  946. default:
  947. return 0;
  948. }
  949. }
  950. switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
  951. case DP_DS_PORT_TYPE_DP_DUALMODE:
  952. if (is_edid_digital_input_dp(drm_edid))
  953. return 0;
  954. /*
  955. * It's left up to the driver to check the
  956. * DP dual mode adapter's max TMDS clock.
  957. *
  958. * Unfortunately it looks like branch devices
  959. * may not fordward that the DP dual mode i2c
  960. * access so we just usually get i2c nak :(
  961. */
  962. fallthrough;
  963. case DP_DS_PORT_TYPE_HDMI:
  964. /*
  965. * We should perhaps assume 165 MHz when detailed cap
  966. * info is not available. But looks like many typical
  967. * branch devices fall into that category and so we'd
  968. * probably end up with users complaining that they can't
  969. * get high resolution modes with their favorite dongle.
  970. *
  971. * So let's limit to 300 MHz instead since DPCD 1.4
  972. * HDMI 2.0 DFPs are required to have the detailed cap
  973. * info. So it's more likely we're dealing with a HDMI 1.4
  974. * compatible* device here.
  975. */
  976. if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
  977. return 300000;
  978. return port_cap[1] * 2500;
  979. case DP_DS_PORT_TYPE_DVI:
  980. if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
  981. return 165000;
  982. /* FIXME what to do about DVI dual link? */
  983. return port_cap[1] * 2500;
  984. default:
  985. return 0;
  986. }
  987. }
  988. EXPORT_SYMBOL(drm_dp_downstream_max_tmds_clock);
  989. /**
  990. * drm_dp_downstream_min_tmds_clock() - extract downstream facing port min TMDS clock
  991. * @dpcd: DisplayPort configuration data
  992. * @port_cap: port capabilities
  993. * @drm_edid: EDID
  994. *
  995. * Returns: HDMI/DVI downstream facing port min TMDS clock in kHz on success,
  996. * or 0 if max TMDS clock not defined
  997. */
  998. int drm_dp_downstream_min_tmds_clock(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
  999. const u8 port_cap[4],
  1000. const struct drm_edid *drm_edid)
  1001. {
  1002. if (!drm_dp_is_branch(dpcd))
  1003. return 0;
  1004. if (dpcd[DP_DPCD_REV] < 0x11) {
  1005. switch (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_TYPE_MASK) {
  1006. case DP_DWN_STRM_PORT_TYPE_TMDS:
  1007. return 25000;
  1008. default:
  1009. return 0;
  1010. }
  1011. }
  1012. switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
  1013. case DP_DS_PORT_TYPE_DP_DUALMODE:
  1014. if (is_edid_digital_input_dp(drm_edid))
  1015. return 0;
  1016. fallthrough;
  1017. case DP_DS_PORT_TYPE_DVI:
  1018. case DP_DS_PORT_TYPE_HDMI:
  1019. /*
  1020. * Unclear whether the protocol converter could
  1021. * utilize pixel replication. Assume it won't.
  1022. */
  1023. return 25000;
  1024. default:
  1025. return 0;
  1026. }
  1027. }
  1028. EXPORT_SYMBOL(drm_dp_downstream_min_tmds_clock);
  1029. /**
  1030. * drm_dp_downstream_max_bpc() - extract downstream facing port max
  1031. * bits per component
  1032. * @dpcd: DisplayPort configuration data
  1033. * @port_cap: downstream facing port capabilities
  1034. * @drm_edid: EDID
  1035. *
  1036. * Returns: Max bpc on success or 0 if max bpc not defined
  1037. */
  1038. int drm_dp_downstream_max_bpc(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
  1039. const u8 port_cap[4],
  1040. const struct drm_edid *drm_edid)
  1041. {
  1042. if (!drm_dp_is_branch(dpcd))
  1043. return 0;
  1044. if (dpcd[DP_DPCD_REV] < 0x11) {
  1045. switch (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_TYPE_MASK) {
  1046. case DP_DWN_STRM_PORT_TYPE_DP:
  1047. return 0;
  1048. default:
  1049. return 8;
  1050. }
  1051. }
  1052. switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
  1053. case DP_DS_PORT_TYPE_DP:
  1054. return 0;
  1055. case DP_DS_PORT_TYPE_DP_DUALMODE:
  1056. if (is_edid_digital_input_dp(drm_edid))
  1057. return 0;
  1058. fallthrough;
  1059. case DP_DS_PORT_TYPE_HDMI:
  1060. case DP_DS_PORT_TYPE_DVI:
  1061. case DP_DS_PORT_TYPE_VGA:
  1062. if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
  1063. return 8;
  1064. switch (port_cap[2] & DP_DS_MAX_BPC_MASK) {
  1065. case DP_DS_8BPC:
  1066. return 8;
  1067. case DP_DS_10BPC:
  1068. return 10;
  1069. case DP_DS_12BPC:
  1070. return 12;
  1071. case DP_DS_16BPC:
  1072. return 16;
  1073. default:
  1074. return 8;
  1075. }
  1076. break;
  1077. default:
  1078. return 8;
  1079. }
  1080. }
  1081. EXPORT_SYMBOL(drm_dp_downstream_max_bpc);
  1082. /**
  1083. * drm_dp_downstream_420_passthrough() - determine downstream facing port
  1084. * YCbCr 4:2:0 pass-through capability
  1085. * @dpcd: DisplayPort configuration data
  1086. * @port_cap: downstream facing port capabilities
  1087. *
  1088. * Returns: whether the downstream facing port can pass through YCbCr 4:2:0
  1089. */
  1090. bool drm_dp_downstream_420_passthrough(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
  1091. const u8 port_cap[4])
  1092. {
  1093. if (!drm_dp_is_branch(dpcd))
  1094. return false;
  1095. if (dpcd[DP_DPCD_REV] < 0x13)
  1096. return false;
  1097. switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
  1098. case DP_DS_PORT_TYPE_DP:
  1099. return true;
  1100. case DP_DS_PORT_TYPE_HDMI:
  1101. if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
  1102. return false;
  1103. return port_cap[3] & DP_DS_HDMI_YCBCR420_PASS_THROUGH;
  1104. default:
  1105. return false;
  1106. }
  1107. }
  1108. EXPORT_SYMBOL(drm_dp_downstream_420_passthrough);
  1109. /**
  1110. * drm_dp_downstream_444_to_420_conversion() - determine downstream facing port
  1111. * YCbCr 4:4:4->4:2:0 conversion capability
  1112. * @dpcd: DisplayPort configuration data
  1113. * @port_cap: downstream facing port capabilities
  1114. *
  1115. * Returns: whether the downstream facing port can convert YCbCr 4:4:4 to 4:2:0
  1116. */
  1117. bool drm_dp_downstream_444_to_420_conversion(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
  1118. const u8 port_cap[4])
  1119. {
  1120. if (!drm_dp_is_branch(dpcd))
  1121. return false;
  1122. if (dpcd[DP_DPCD_REV] < 0x13)
  1123. return false;
  1124. switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
  1125. case DP_DS_PORT_TYPE_HDMI:
  1126. if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
  1127. return false;
  1128. return port_cap[3] & DP_DS_HDMI_YCBCR444_TO_420_CONV;
  1129. default:
  1130. return false;
  1131. }
  1132. }
  1133. EXPORT_SYMBOL(drm_dp_downstream_444_to_420_conversion);
  1134. /**
  1135. * drm_dp_downstream_rgb_to_ycbcr_conversion() - determine downstream facing port
  1136. * RGB->YCbCr conversion capability
  1137. * @dpcd: DisplayPort configuration data
  1138. * @port_cap: downstream facing port capabilities
  1139. * @color_spc: Colorspace for which conversion cap is sought
  1140. *
  1141. * Returns: whether the downstream facing port can convert RGB->YCbCr for a given
  1142. * colorspace.
  1143. */
  1144. bool drm_dp_downstream_rgb_to_ycbcr_conversion(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
  1145. const u8 port_cap[4],
  1146. u8 color_spc)
  1147. {
  1148. if (!drm_dp_is_branch(dpcd))
  1149. return false;
  1150. if (dpcd[DP_DPCD_REV] < 0x13)
  1151. return false;
  1152. switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
  1153. case DP_DS_PORT_TYPE_HDMI:
  1154. if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
  1155. return false;
  1156. return port_cap[3] & color_spc;
  1157. default:
  1158. return false;
  1159. }
  1160. }
  1161. EXPORT_SYMBOL(drm_dp_downstream_rgb_to_ycbcr_conversion);
  1162. /**
  1163. * drm_dp_downstream_mode() - return a mode for downstream facing port
  1164. * @dev: DRM device
  1165. * @dpcd: DisplayPort configuration data
  1166. * @port_cap: port capabilities
  1167. *
  1168. * Provides a suitable mode for downstream facing ports without EDID.
  1169. *
  1170. * Returns: A new drm_display_mode on success or NULL on failure
  1171. */
  1172. struct drm_display_mode *
  1173. drm_dp_downstream_mode(struct drm_device *dev,
  1174. const u8 dpcd[DP_RECEIVER_CAP_SIZE],
  1175. const u8 port_cap[4])
  1176. {
  1177. u8 vic;
  1178. if (!drm_dp_is_branch(dpcd))
  1179. return NULL;
  1180. if (dpcd[DP_DPCD_REV] < 0x11)
  1181. return NULL;
  1182. switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
  1183. case DP_DS_PORT_TYPE_NON_EDID:
  1184. switch (port_cap[0] & DP_DS_NON_EDID_MASK) {
  1185. case DP_DS_NON_EDID_720x480i_60:
  1186. vic = 6;
  1187. break;
  1188. case DP_DS_NON_EDID_720x480i_50:
  1189. vic = 21;
  1190. break;
  1191. case DP_DS_NON_EDID_1920x1080i_60:
  1192. vic = 5;
  1193. break;
  1194. case DP_DS_NON_EDID_1920x1080i_50:
  1195. vic = 20;
  1196. break;
  1197. case DP_DS_NON_EDID_1280x720_60:
  1198. vic = 4;
  1199. break;
  1200. case DP_DS_NON_EDID_1280x720_50:
  1201. vic = 19;
  1202. break;
  1203. default:
  1204. return NULL;
  1205. }
  1206. return drm_display_mode_from_cea_vic(dev, vic);
  1207. default:
  1208. return NULL;
  1209. }
  1210. }
  1211. EXPORT_SYMBOL(drm_dp_downstream_mode);
  1212. /**
  1213. * drm_dp_downstream_id() - identify branch device
  1214. * @aux: DisplayPort AUX channel
  1215. * @id: DisplayPort branch device id
  1216. *
  1217. * Returns branch device id on success or NULL on failure
  1218. */
  1219. int drm_dp_downstream_id(struct drm_dp_aux *aux, char id[6])
  1220. {
  1221. return drm_dp_dpcd_read(aux, DP_BRANCH_ID, id, 6);
  1222. }
  1223. EXPORT_SYMBOL(drm_dp_downstream_id);
  1224. /**
  1225. * drm_dp_downstream_debug() - debug DP branch devices
  1226. * @m: pointer for debugfs file
  1227. * @dpcd: DisplayPort configuration data
  1228. * @port_cap: port capabilities
  1229. * @drm_edid: EDID
  1230. * @aux: DisplayPort AUX channel
  1231. *
  1232. */
  1233. void drm_dp_downstream_debug(struct seq_file *m,
  1234. const u8 dpcd[DP_RECEIVER_CAP_SIZE],
  1235. const u8 port_cap[4],
  1236. const struct drm_edid *drm_edid,
  1237. struct drm_dp_aux *aux)
  1238. {
  1239. bool detailed_cap_info = dpcd[DP_DOWNSTREAMPORT_PRESENT] &
  1240. DP_DETAILED_CAP_INFO_AVAILABLE;
  1241. int clk;
  1242. int bpc;
  1243. char id[7];
  1244. int len;
  1245. uint8_t rev[2];
  1246. int type = port_cap[0] & DP_DS_PORT_TYPE_MASK;
  1247. bool branch_device = drm_dp_is_branch(dpcd);
  1248. seq_printf(m, "\tDP branch device present: %s\n",
  1249. str_yes_no(branch_device));
  1250. if (!branch_device)
  1251. return;
  1252. switch (type) {
  1253. case DP_DS_PORT_TYPE_DP:
  1254. seq_puts(m, "\t\tType: DisplayPort\n");
  1255. break;
  1256. case DP_DS_PORT_TYPE_VGA:
  1257. seq_puts(m, "\t\tType: VGA\n");
  1258. break;
  1259. case DP_DS_PORT_TYPE_DVI:
  1260. seq_puts(m, "\t\tType: DVI\n");
  1261. break;
  1262. case DP_DS_PORT_TYPE_HDMI:
  1263. seq_puts(m, "\t\tType: HDMI\n");
  1264. break;
  1265. case DP_DS_PORT_TYPE_NON_EDID:
  1266. seq_puts(m, "\t\tType: others without EDID support\n");
  1267. break;
  1268. case DP_DS_PORT_TYPE_DP_DUALMODE:
  1269. seq_puts(m, "\t\tType: DP++\n");
  1270. break;
  1271. case DP_DS_PORT_TYPE_WIRELESS:
  1272. seq_puts(m, "\t\tType: Wireless\n");
  1273. break;
  1274. default:
  1275. seq_puts(m, "\t\tType: N/A\n");
  1276. }
  1277. memset(id, 0, sizeof(id));
  1278. drm_dp_downstream_id(aux, id);
  1279. seq_printf(m, "\t\tID: %s\n", id);
  1280. len = drm_dp_dpcd_read(aux, DP_BRANCH_HW_REV, &rev[0], 1);
  1281. if (len > 0)
  1282. seq_printf(m, "\t\tHW: %d.%d\n",
  1283. (rev[0] & 0xf0) >> 4, rev[0] & 0xf);
  1284. len = drm_dp_dpcd_read(aux, DP_BRANCH_SW_REV, rev, 2);
  1285. if (len > 0)
  1286. seq_printf(m, "\t\tSW: %d.%d\n", rev[0], rev[1]);
  1287. if (detailed_cap_info) {
  1288. clk = drm_dp_downstream_max_dotclock(dpcd, port_cap);
  1289. if (clk > 0)
  1290. seq_printf(m, "\t\tMax dot clock: %d kHz\n", clk);
  1291. clk = drm_dp_downstream_max_tmds_clock(dpcd, port_cap, drm_edid);
  1292. if (clk > 0)
  1293. seq_printf(m, "\t\tMax TMDS clock: %d kHz\n", clk);
  1294. clk = drm_dp_downstream_min_tmds_clock(dpcd, port_cap, drm_edid);
  1295. if (clk > 0)
  1296. seq_printf(m, "\t\tMin TMDS clock: %d kHz\n", clk);
  1297. bpc = drm_dp_downstream_max_bpc(dpcd, port_cap, drm_edid);
  1298. if (bpc > 0)
  1299. seq_printf(m, "\t\tMax bpc: %d\n", bpc);
  1300. }
  1301. }
  1302. EXPORT_SYMBOL(drm_dp_downstream_debug);
  1303. /**
  1304. * drm_dp_subconnector_type() - get DP branch device type
  1305. * @dpcd: DisplayPort configuration data
  1306. * @port_cap: port capabilities
  1307. */
  1308. enum drm_mode_subconnector
  1309. drm_dp_subconnector_type(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
  1310. const u8 port_cap[4])
  1311. {
  1312. int type;
  1313. if (!drm_dp_is_branch(dpcd))
  1314. return DRM_MODE_SUBCONNECTOR_Native;
  1315. /* DP 1.0 approach */
  1316. if (dpcd[DP_DPCD_REV] == DP_DPCD_REV_10) {
  1317. type = dpcd[DP_DOWNSTREAMPORT_PRESENT] &
  1318. DP_DWN_STRM_PORT_TYPE_MASK;
  1319. switch (type) {
  1320. case DP_DWN_STRM_PORT_TYPE_TMDS:
  1321. /* Can be HDMI or DVI-D, DVI-D is a safer option */
  1322. return DRM_MODE_SUBCONNECTOR_DVID;
  1323. case DP_DWN_STRM_PORT_TYPE_ANALOG:
  1324. /* Can be VGA or DVI-A, VGA is more popular */
  1325. return DRM_MODE_SUBCONNECTOR_VGA;
  1326. case DP_DWN_STRM_PORT_TYPE_DP:
  1327. return DRM_MODE_SUBCONNECTOR_DisplayPort;
  1328. case DP_DWN_STRM_PORT_TYPE_OTHER:
  1329. default:
  1330. return DRM_MODE_SUBCONNECTOR_Unknown;
  1331. }
  1332. }
  1333. type = port_cap[0] & DP_DS_PORT_TYPE_MASK;
  1334. switch (type) {
  1335. case DP_DS_PORT_TYPE_DP:
  1336. case DP_DS_PORT_TYPE_DP_DUALMODE:
  1337. return DRM_MODE_SUBCONNECTOR_DisplayPort;
  1338. case DP_DS_PORT_TYPE_VGA:
  1339. return DRM_MODE_SUBCONNECTOR_VGA;
  1340. case DP_DS_PORT_TYPE_DVI:
  1341. return DRM_MODE_SUBCONNECTOR_DVID;
  1342. case DP_DS_PORT_TYPE_HDMI:
  1343. return DRM_MODE_SUBCONNECTOR_HDMIA;
  1344. case DP_DS_PORT_TYPE_WIRELESS:
  1345. return DRM_MODE_SUBCONNECTOR_Wireless;
  1346. case DP_DS_PORT_TYPE_NON_EDID:
  1347. default:
  1348. return DRM_MODE_SUBCONNECTOR_Unknown;
  1349. }
  1350. }
  1351. EXPORT_SYMBOL(drm_dp_subconnector_type);
  1352. /**
  1353. * drm_dp_set_subconnector_property - set subconnector for DP connector
  1354. * @connector: connector to set property on
  1355. * @status: connector status
  1356. * @dpcd: DisplayPort configuration data
  1357. * @port_cap: port capabilities
  1358. *
  1359. * Called by a driver on every detect event.
  1360. */
  1361. void drm_dp_set_subconnector_property(struct drm_connector *connector,
  1362. enum drm_connector_status status,
  1363. const u8 *dpcd,
  1364. const u8 port_cap[4])
  1365. {
  1366. enum drm_mode_subconnector subconnector = DRM_MODE_SUBCONNECTOR_Unknown;
  1367. if (status == connector_status_connected)
  1368. subconnector = drm_dp_subconnector_type(dpcd, port_cap);
  1369. drm_object_property_set_value(&connector->base,
  1370. connector->dev->mode_config.dp_subconnector_property,
  1371. subconnector);
  1372. }
  1373. EXPORT_SYMBOL(drm_dp_set_subconnector_property);
  1374. /**
  1375. * drm_dp_read_sink_count_cap() - Check whether a given connector has a valid sink
  1376. * count
  1377. * @connector: The DRM connector to check
  1378. * @dpcd: A cached copy of the connector's DPCD RX capabilities
  1379. * @desc: A cached copy of the connector's DP descriptor
  1380. *
  1381. * See also: drm_dp_read_sink_count()
  1382. *
  1383. * Returns: %True if the (e)DP connector has a valid sink count that should
  1384. * be probed, %false otherwise.
  1385. */
  1386. bool drm_dp_read_sink_count_cap(struct drm_connector *connector,
  1387. const u8 dpcd[DP_RECEIVER_CAP_SIZE],
  1388. const struct drm_dp_desc *desc)
  1389. {
  1390. /* Some eDP panels don't set a valid value for the sink count */
  1391. return connector->connector_type != DRM_MODE_CONNECTOR_eDP &&
  1392. dpcd[DP_DPCD_REV] >= DP_DPCD_REV_11 &&
  1393. dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_PRESENT &&
  1394. !drm_dp_has_quirk(desc, DP_DPCD_QUIRK_NO_SINK_COUNT);
  1395. }
  1396. EXPORT_SYMBOL(drm_dp_read_sink_count_cap);
  1397. /**
  1398. * drm_dp_read_sink_count() - Retrieve the sink count for a given sink
  1399. * @aux: The DP AUX channel to use
  1400. *
  1401. * See also: drm_dp_read_sink_count_cap()
  1402. *
  1403. * Returns: The current sink count reported by @aux, or a negative error code
  1404. * otherwise.
  1405. */
  1406. int drm_dp_read_sink_count(struct drm_dp_aux *aux)
  1407. {
  1408. u8 count;
  1409. int ret;
  1410. ret = drm_dp_dpcd_readb(aux, DP_SINK_COUNT, &count);
  1411. if (ret < 0)
  1412. return ret;
  1413. if (ret != 1)
  1414. return -EIO;
  1415. return DP_GET_SINK_COUNT(count);
  1416. }
  1417. EXPORT_SYMBOL(drm_dp_read_sink_count);
  1418. /*
  1419. * I2C-over-AUX implementation
  1420. */
  1421. static u32 drm_dp_i2c_functionality(struct i2c_adapter *adapter)
  1422. {
  1423. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
  1424. I2C_FUNC_SMBUS_READ_BLOCK_DATA |
  1425. I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
  1426. I2C_FUNC_10BIT_ADDR;
  1427. }
  1428. static void drm_dp_i2c_msg_write_status_update(struct drm_dp_aux_msg *msg)
  1429. {
  1430. /*
  1431. * In case of i2c defer or short i2c ack reply to a write,
  1432. * we need to switch to WRITE_STATUS_UPDATE to drain the
  1433. * rest of the message
  1434. */
  1435. if ((msg->request & ~DP_AUX_I2C_MOT) == DP_AUX_I2C_WRITE) {
  1436. msg->request &= DP_AUX_I2C_MOT;
  1437. msg->request |= DP_AUX_I2C_WRITE_STATUS_UPDATE;
  1438. }
  1439. }
  1440. #define AUX_PRECHARGE_LEN 10 /* 10 to 16 */
  1441. #define AUX_SYNC_LEN (16 + 4) /* preamble + AUX_SYNC_END */
  1442. #define AUX_STOP_LEN 4
  1443. #define AUX_CMD_LEN 4
  1444. #define AUX_ADDRESS_LEN 20
  1445. #define AUX_REPLY_PAD_LEN 4
  1446. #define AUX_LENGTH_LEN 8
  1447. /*
  1448. * Calculate the duration of the AUX request/reply in usec. Gives the
  1449. * "best" case estimate, ie. successful while as short as possible.
  1450. */
  1451. static int drm_dp_aux_req_duration(const struct drm_dp_aux_msg *msg)
  1452. {
  1453. int len = AUX_PRECHARGE_LEN + AUX_SYNC_LEN + AUX_STOP_LEN +
  1454. AUX_CMD_LEN + AUX_ADDRESS_LEN + AUX_LENGTH_LEN;
  1455. if ((msg->request & DP_AUX_I2C_READ) == 0)
  1456. len += msg->size * 8;
  1457. return len;
  1458. }
  1459. static int drm_dp_aux_reply_duration(const struct drm_dp_aux_msg *msg)
  1460. {
  1461. int len = AUX_PRECHARGE_LEN + AUX_SYNC_LEN + AUX_STOP_LEN +
  1462. AUX_CMD_LEN + AUX_REPLY_PAD_LEN;
  1463. /*
  1464. * For read we expect what was asked. For writes there will
  1465. * be 0 or 1 data bytes. Assume 0 for the "best" case.
  1466. */
  1467. if (msg->request & DP_AUX_I2C_READ)
  1468. len += msg->size * 8;
  1469. return len;
  1470. }
  1471. #define I2C_START_LEN 1
  1472. #define I2C_STOP_LEN 1
  1473. #define I2C_ADDR_LEN 9 /* ADDRESS + R/W + ACK/NACK */
  1474. #define I2C_DATA_LEN 9 /* DATA + ACK/NACK */
  1475. /*
  1476. * Calculate the length of the i2c transfer in usec, assuming
  1477. * the i2c bus speed is as specified. Gives the "worst"
  1478. * case estimate, ie. successful while as long as possible.
  1479. * Doesn't account the "MOT" bit, and instead assumes each
  1480. * message includes a START, ADDRESS and STOP. Neither does it
  1481. * account for additional random variables such as clock stretching.
  1482. */
  1483. static int drm_dp_i2c_msg_duration(const struct drm_dp_aux_msg *msg,
  1484. int i2c_speed_khz)
  1485. {
  1486. /* AUX bitrate is 1MHz, i2c bitrate as specified */
  1487. return DIV_ROUND_UP((I2C_START_LEN + I2C_ADDR_LEN +
  1488. msg->size * I2C_DATA_LEN +
  1489. I2C_STOP_LEN) * 1000, i2c_speed_khz);
  1490. }
  1491. /*
  1492. * Determine how many retries should be attempted to successfully transfer
  1493. * the specified message, based on the estimated durations of the
  1494. * i2c and AUX transfers.
  1495. */
  1496. static int drm_dp_i2c_retry_count(const struct drm_dp_aux_msg *msg,
  1497. int i2c_speed_khz)
  1498. {
  1499. int aux_time_us = drm_dp_aux_req_duration(msg) +
  1500. drm_dp_aux_reply_duration(msg);
  1501. int i2c_time_us = drm_dp_i2c_msg_duration(msg, i2c_speed_khz);
  1502. return DIV_ROUND_UP(i2c_time_us, aux_time_us + AUX_RETRY_INTERVAL);
  1503. }
  1504. /*
  1505. * FIXME currently assumes 10 kHz as some real world devices seem
  1506. * to require it. We should query/set the speed via DPCD if supported.
  1507. */
  1508. static int dp_aux_i2c_speed_khz __read_mostly = 10;
  1509. module_param_unsafe(dp_aux_i2c_speed_khz, int, 0644);
  1510. MODULE_PARM_DESC(dp_aux_i2c_speed_khz,
  1511. "Assumed speed of the i2c bus in kHz, (1-400, default 10)");
  1512. /*
  1513. * Transfer a single I2C-over-AUX message and handle various error conditions,
  1514. * retrying the transaction as appropriate. It is assumed that the
  1515. * &drm_dp_aux.transfer function does not modify anything in the msg other than the
  1516. * reply field.
  1517. *
  1518. * Returns bytes transferred on success, or a negative error code on failure.
  1519. */
  1520. static int drm_dp_i2c_do_msg(struct drm_dp_aux *aux, struct drm_dp_aux_msg *msg)
  1521. {
  1522. unsigned int retry, defer_i2c;
  1523. int ret;
  1524. /*
  1525. * DP1.2 sections 2.7.7.1.5.6.1 and 2.7.7.1.6.6.1: A DP Source device
  1526. * is required to retry at least seven times upon receiving AUX_DEFER
  1527. * before giving up the AUX transaction.
  1528. *
  1529. * We also try to account for the i2c bus speed.
  1530. */
  1531. int max_retries = max(7, drm_dp_i2c_retry_count(msg, dp_aux_i2c_speed_khz));
  1532. for (retry = 0, defer_i2c = 0; retry < (max_retries + defer_i2c); retry++) {
  1533. ret = aux->transfer(aux, msg);
  1534. if (ret < 0) {
  1535. if (ret == -EBUSY)
  1536. continue;
  1537. /*
  1538. * While timeouts can be errors, they're usually normal
  1539. * behavior (for instance, when a driver tries to
  1540. * communicate with a non-existent DisplayPort device).
  1541. * Avoid spamming the kernel log with timeout errors.
  1542. */
  1543. if (ret == -ETIMEDOUT)
  1544. drm_dbg_kms_ratelimited(aux->drm_dev, "%s: transaction timed out\n",
  1545. aux->name);
  1546. else
  1547. drm_dbg_kms(aux->drm_dev, "%s: transaction failed: %d\n",
  1548. aux->name, ret);
  1549. return ret;
  1550. }
  1551. switch (msg->reply & DP_AUX_NATIVE_REPLY_MASK) {
  1552. case DP_AUX_NATIVE_REPLY_ACK:
  1553. /*
  1554. * For I2C-over-AUX transactions this isn't enough, we
  1555. * need to check for the I2C ACK reply.
  1556. */
  1557. break;
  1558. case DP_AUX_NATIVE_REPLY_NACK:
  1559. drm_dbg_kms(aux->drm_dev, "%s: native nack (result=%d, size=%zu)\n",
  1560. aux->name, ret, msg->size);
  1561. return -EREMOTEIO;
  1562. case DP_AUX_NATIVE_REPLY_DEFER:
  1563. drm_dbg_kms(aux->drm_dev, "%s: native defer\n", aux->name);
  1564. /*
  1565. * We could check for I2C bit rate capabilities and if
  1566. * available adjust this interval. We could also be
  1567. * more careful with DP-to-legacy adapters where a
  1568. * long legacy cable may force very low I2C bit rates.
  1569. *
  1570. * For now just defer for long enough to hopefully be
  1571. * safe for all use-cases.
  1572. */
  1573. usleep_range(AUX_RETRY_INTERVAL, AUX_RETRY_INTERVAL + 100);
  1574. continue;
  1575. default:
  1576. drm_err(aux->drm_dev, "%s: invalid native reply %#04x\n",
  1577. aux->name, msg->reply);
  1578. return -EREMOTEIO;
  1579. }
  1580. switch (msg->reply & DP_AUX_I2C_REPLY_MASK) {
  1581. case DP_AUX_I2C_REPLY_ACK:
  1582. /*
  1583. * Both native ACK and I2C ACK replies received. We
  1584. * can assume the transfer was successful.
  1585. */
  1586. if (ret != msg->size)
  1587. drm_dp_i2c_msg_write_status_update(msg);
  1588. return ret;
  1589. case DP_AUX_I2C_REPLY_NACK:
  1590. drm_dbg_kms(aux->drm_dev, "%s: I2C nack (result=%d, size=%zu)\n",
  1591. aux->name, ret, msg->size);
  1592. aux->i2c_nack_count++;
  1593. return -EREMOTEIO;
  1594. case DP_AUX_I2C_REPLY_DEFER:
  1595. drm_dbg_kms(aux->drm_dev, "%s: I2C defer\n", aux->name);
  1596. /* DP Compliance Test 4.2.2.5 Requirement:
  1597. * Must have at least 7 retries for I2C defers on the
  1598. * transaction to pass this test
  1599. */
  1600. aux->i2c_defer_count++;
  1601. if (defer_i2c < 7)
  1602. defer_i2c++;
  1603. usleep_range(AUX_RETRY_INTERVAL, AUX_RETRY_INTERVAL + 100);
  1604. drm_dp_i2c_msg_write_status_update(msg);
  1605. continue;
  1606. default:
  1607. drm_err(aux->drm_dev, "%s: invalid I2C reply %#04x\n",
  1608. aux->name, msg->reply);
  1609. return -EREMOTEIO;
  1610. }
  1611. }
  1612. drm_dbg_kms(aux->drm_dev, "%s: Too many retries, giving up\n", aux->name);
  1613. return -EREMOTEIO;
  1614. }
  1615. static void drm_dp_i2c_msg_set_request(struct drm_dp_aux_msg *msg,
  1616. const struct i2c_msg *i2c_msg)
  1617. {
  1618. msg->request = (i2c_msg->flags & I2C_M_RD) ?
  1619. DP_AUX_I2C_READ : DP_AUX_I2C_WRITE;
  1620. if (!(i2c_msg->flags & I2C_M_STOP))
  1621. msg->request |= DP_AUX_I2C_MOT;
  1622. }
  1623. /*
  1624. * Keep retrying drm_dp_i2c_do_msg until all data has been transferred.
  1625. *
  1626. * Returns an error code on failure, or a recommended transfer size on success.
  1627. */
  1628. static int drm_dp_i2c_drain_msg(struct drm_dp_aux *aux, struct drm_dp_aux_msg *orig_msg)
  1629. {
  1630. int err, ret = orig_msg->size;
  1631. struct drm_dp_aux_msg msg = *orig_msg;
  1632. while (msg.size > 0) {
  1633. err = drm_dp_i2c_do_msg(aux, &msg);
  1634. if (err <= 0)
  1635. return err == 0 ? -EPROTO : err;
  1636. if (err < msg.size && err < ret) {
  1637. drm_dbg_kms(aux->drm_dev,
  1638. "%s: Partial I2C reply: requested %zu bytes got %d bytes\n",
  1639. aux->name, msg.size, err);
  1640. ret = err;
  1641. }
  1642. msg.size -= err;
  1643. msg.buffer += err;
  1644. }
  1645. return ret;
  1646. }
  1647. /*
  1648. * Bizlink designed DP->DVI-D Dual Link adapters require the I2C over AUX
  1649. * packets to be as large as possible. If not, the I2C transactions never
  1650. * succeed. Hence the default is maximum.
  1651. */
  1652. static int dp_aux_i2c_transfer_size __read_mostly = DP_AUX_MAX_PAYLOAD_BYTES;
  1653. module_param_unsafe(dp_aux_i2c_transfer_size, int, 0644);
  1654. MODULE_PARM_DESC(dp_aux_i2c_transfer_size,
  1655. "Number of bytes to transfer in a single I2C over DP AUX CH message, (1-16, default 16)");
  1656. static int drm_dp_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
  1657. int num)
  1658. {
  1659. struct drm_dp_aux *aux = adapter->algo_data;
  1660. unsigned int i, j;
  1661. unsigned transfer_size;
  1662. struct drm_dp_aux_msg msg;
  1663. int err = 0;
  1664. if (aux->powered_down)
  1665. return -EBUSY;
  1666. dp_aux_i2c_transfer_size = clamp(dp_aux_i2c_transfer_size, 1, DP_AUX_MAX_PAYLOAD_BYTES);
  1667. memset(&msg, 0, sizeof(msg));
  1668. for (i = 0; i < num; i++) {
  1669. msg.address = msgs[i].addr;
  1670. drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
  1671. /* Send a bare address packet to start the transaction.
  1672. * Zero sized messages specify an address only (bare
  1673. * address) transaction.
  1674. */
  1675. msg.buffer = NULL;
  1676. msg.size = 0;
  1677. err = drm_dp_i2c_do_msg(aux, &msg);
  1678. /*
  1679. * Reset msg.request in case in case it got
  1680. * changed into a WRITE_STATUS_UPDATE.
  1681. */
  1682. drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
  1683. if (err < 0)
  1684. break;
  1685. /* We want each transaction to be as large as possible, but
  1686. * we'll go to smaller sizes if the hardware gives us a
  1687. * short reply.
  1688. */
  1689. transfer_size = dp_aux_i2c_transfer_size;
  1690. for (j = 0; j < msgs[i].len; j += msg.size) {
  1691. msg.buffer = msgs[i].buf + j;
  1692. msg.size = min(transfer_size, msgs[i].len - j);
  1693. err = drm_dp_i2c_drain_msg(aux, &msg);
  1694. /*
  1695. * Reset msg.request in case in case it got
  1696. * changed into a WRITE_STATUS_UPDATE.
  1697. */
  1698. drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
  1699. if (err < 0)
  1700. break;
  1701. transfer_size = err;
  1702. }
  1703. if (err < 0)
  1704. break;
  1705. }
  1706. if (err >= 0)
  1707. err = num;
  1708. /* Send a bare address packet to close out the transaction.
  1709. * Zero sized messages specify an address only (bare
  1710. * address) transaction.
  1711. */
  1712. msg.request &= ~DP_AUX_I2C_MOT;
  1713. msg.buffer = NULL;
  1714. msg.size = 0;
  1715. (void)drm_dp_i2c_do_msg(aux, &msg);
  1716. return err;
  1717. }
  1718. static const struct i2c_algorithm drm_dp_i2c_algo = {
  1719. .functionality = drm_dp_i2c_functionality,
  1720. .master_xfer = drm_dp_i2c_xfer,
  1721. };
  1722. static struct drm_dp_aux *i2c_to_aux(struct i2c_adapter *i2c)
  1723. {
  1724. return container_of(i2c, struct drm_dp_aux, ddc);
  1725. }
  1726. static void lock_bus(struct i2c_adapter *i2c, unsigned int flags)
  1727. {
  1728. mutex_lock(&i2c_to_aux(i2c)->hw_mutex);
  1729. }
  1730. static int trylock_bus(struct i2c_adapter *i2c, unsigned int flags)
  1731. {
  1732. return mutex_trylock(&i2c_to_aux(i2c)->hw_mutex);
  1733. }
  1734. static void unlock_bus(struct i2c_adapter *i2c, unsigned int flags)
  1735. {
  1736. mutex_unlock(&i2c_to_aux(i2c)->hw_mutex);
  1737. }
  1738. static const struct i2c_lock_operations drm_dp_i2c_lock_ops = {
  1739. .lock_bus = lock_bus,
  1740. .trylock_bus = trylock_bus,
  1741. .unlock_bus = unlock_bus,
  1742. };
  1743. static int drm_dp_aux_get_crc(struct drm_dp_aux *aux, u8 *crc)
  1744. {
  1745. u8 buf, count;
  1746. int ret;
  1747. ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK, &buf);
  1748. if (ret < 0)
  1749. return ret;
  1750. WARN_ON(!(buf & DP_TEST_SINK_START));
  1751. ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK_MISC, &buf);
  1752. if (ret < 0)
  1753. return ret;
  1754. count = buf & DP_TEST_COUNT_MASK;
  1755. if (count == aux->crc_count)
  1756. return -EAGAIN; /* No CRC yet */
  1757. aux->crc_count = count;
  1758. /*
  1759. * At DP_TEST_CRC_R_CR, there's 6 bytes containing CRC data, 2 bytes
  1760. * per component (RGB or CrYCb).
  1761. */
  1762. ret = drm_dp_dpcd_read(aux, DP_TEST_CRC_R_CR, crc, 6);
  1763. if (ret < 0)
  1764. return ret;
  1765. return 0;
  1766. }
  1767. static void drm_dp_aux_crc_work(struct work_struct *work)
  1768. {
  1769. struct drm_dp_aux *aux = container_of(work, struct drm_dp_aux,
  1770. crc_work);
  1771. struct drm_crtc *crtc;
  1772. u8 crc_bytes[6];
  1773. uint32_t crcs[3];
  1774. int ret;
  1775. if (WARN_ON(!aux->crtc))
  1776. return;
  1777. crtc = aux->crtc;
  1778. while (crtc->crc.opened) {
  1779. drm_crtc_wait_one_vblank(crtc);
  1780. if (!crtc->crc.opened)
  1781. break;
  1782. ret = drm_dp_aux_get_crc(aux, crc_bytes);
  1783. if (ret == -EAGAIN) {
  1784. usleep_range(1000, 2000);
  1785. ret = drm_dp_aux_get_crc(aux, crc_bytes);
  1786. }
  1787. if (ret == -EAGAIN) {
  1788. drm_dbg_kms(aux->drm_dev, "%s: Get CRC failed after retrying: %d\n",
  1789. aux->name, ret);
  1790. continue;
  1791. } else if (ret) {
  1792. drm_dbg_kms(aux->drm_dev, "%s: Failed to get a CRC: %d\n", aux->name, ret);
  1793. continue;
  1794. }
  1795. crcs[0] = crc_bytes[0] | crc_bytes[1] << 8;
  1796. crcs[1] = crc_bytes[2] | crc_bytes[3] << 8;
  1797. crcs[2] = crc_bytes[4] | crc_bytes[5] << 8;
  1798. drm_crtc_add_crc_entry(crtc, false, 0, crcs);
  1799. }
  1800. }
  1801. /**
  1802. * drm_dp_remote_aux_init() - minimally initialise a remote aux channel
  1803. * @aux: DisplayPort AUX channel
  1804. *
  1805. * Used for remote aux channel in general. Merely initialize the crc work
  1806. * struct.
  1807. */
  1808. void drm_dp_remote_aux_init(struct drm_dp_aux *aux)
  1809. {
  1810. INIT_WORK(&aux->crc_work, drm_dp_aux_crc_work);
  1811. }
  1812. EXPORT_SYMBOL(drm_dp_remote_aux_init);
  1813. /**
  1814. * drm_dp_aux_init() - minimally initialise an aux channel
  1815. * @aux: DisplayPort AUX channel
  1816. *
  1817. * If you need to use the drm_dp_aux's i2c adapter prior to registering it with
  1818. * the outside world, call drm_dp_aux_init() first. For drivers which are
  1819. * grandparents to their AUX adapters (e.g. the AUX adapter is parented by a
  1820. * &drm_connector), you must still call drm_dp_aux_register() once the connector
  1821. * has been registered to allow userspace access to the auxiliary DP channel.
  1822. * Likewise, for such drivers you should also assign &drm_dp_aux.drm_dev as
  1823. * early as possible so that the &drm_device that corresponds to the AUX adapter
  1824. * may be mentioned in debugging output from the DRM DP helpers.
  1825. *
  1826. * For devices which use a separate platform device for their AUX adapters, this
  1827. * may be called as early as required by the driver.
  1828. *
  1829. */
  1830. void drm_dp_aux_init(struct drm_dp_aux *aux)
  1831. {
  1832. mutex_init(&aux->hw_mutex);
  1833. mutex_init(&aux->cec.lock);
  1834. INIT_WORK(&aux->crc_work, drm_dp_aux_crc_work);
  1835. aux->ddc.algo = &drm_dp_i2c_algo;
  1836. aux->ddc.algo_data = aux;
  1837. aux->ddc.retries = 3;
  1838. aux->ddc.lock_ops = &drm_dp_i2c_lock_ops;
  1839. }
  1840. EXPORT_SYMBOL(drm_dp_aux_init);
  1841. /**
  1842. * drm_dp_aux_register() - initialise and register aux channel
  1843. * @aux: DisplayPort AUX channel
  1844. *
  1845. * Automatically calls drm_dp_aux_init() if this hasn't been done yet. This
  1846. * should only be called once the parent of @aux, &drm_dp_aux.dev, is
  1847. * initialized. For devices which are grandparents of their AUX channels,
  1848. * &drm_dp_aux.dev will typically be the &drm_connector &device which
  1849. * corresponds to @aux. For these devices, it's advised to call
  1850. * drm_dp_aux_register() in &drm_connector_funcs.late_register, and likewise to
  1851. * call drm_dp_aux_unregister() in &drm_connector_funcs.early_unregister.
  1852. * Functions which don't follow this will likely Oops when
  1853. * %CONFIG_DRM_DISPLAY_DP_AUX_CHARDEV is enabled.
  1854. *
  1855. * For devices where the AUX channel is a device that exists independently of
  1856. * the &drm_device that uses it, such as SoCs and bridge devices, it is
  1857. * recommended to call drm_dp_aux_register() after a &drm_device has been
  1858. * assigned to &drm_dp_aux.drm_dev, and likewise to call
  1859. * drm_dp_aux_unregister() once the &drm_device should no longer be associated
  1860. * with the AUX channel (e.g. on bridge detach).
  1861. *
  1862. * Drivers which need to use the aux channel before either of the two points
  1863. * mentioned above need to call drm_dp_aux_init() in order to use the AUX
  1864. * channel before registration.
  1865. *
  1866. * Returns 0 on success or a negative error code on failure.
  1867. */
  1868. int drm_dp_aux_register(struct drm_dp_aux *aux)
  1869. {
  1870. int ret;
  1871. WARN_ON_ONCE(!aux->drm_dev);
  1872. if (!aux->ddc.algo)
  1873. drm_dp_aux_init(aux);
  1874. aux->ddc.owner = THIS_MODULE;
  1875. aux->ddc.dev.parent = aux->dev;
  1876. strscpy(aux->ddc.name, aux->name ? aux->name : dev_name(aux->dev),
  1877. sizeof(aux->ddc.name));
  1878. ret = drm_dp_aux_register_devnode(aux);
  1879. if (ret)
  1880. return ret;
  1881. ret = i2c_add_adapter(&aux->ddc);
  1882. if (ret) {
  1883. drm_dp_aux_unregister_devnode(aux);
  1884. return ret;
  1885. }
  1886. return 0;
  1887. }
  1888. EXPORT_SYMBOL(drm_dp_aux_register);
  1889. /**
  1890. * drm_dp_aux_unregister() - unregister an AUX adapter
  1891. * @aux: DisplayPort AUX channel
  1892. */
  1893. void drm_dp_aux_unregister(struct drm_dp_aux *aux)
  1894. {
  1895. drm_dp_aux_unregister_devnode(aux);
  1896. i2c_del_adapter(&aux->ddc);
  1897. }
  1898. EXPORT_SYMBOL(drm_dp_aux_unregister);
  1899. #define PSR_SETUP_TIME(x) [DP_PSR_SETUP_TIME_ ## x >> DP_PSR_SETUP_TIME_SHIFT] = (x)
  1900. /**
  1901. * drm_dp_psr_setup_time() - PSR setup in time usec
  1902. * @psr_cap: PSR capabilities from DPCD
  1903. *
  1904. * Returns:
  1905. * PSR setup time for the panel in microseconds, negative
  1906. * error code on failure.
  1907. */
  1908. int drm_dp_psr_setup_time(const u8 psr_cap[EDP_PSR_RECEIVER_CAP_SIZE])
  1909. {
  1910. static const u16 psr_setup_time_us[] = {
  1911. PSR_SETUP_TIME(330),
  1912. PSR_SETUP_TIME(275),
  1913. PSR_SETUP_TIME(220),
  1914. PSR_SETUP_TIME(165),
  1915. PSR_SETUP_TIME(110),
  1916. PSR_SETUP_TIME(55),
  1917. PSR_SETUP_TIME(0),
  1918. };
  1919. int i;
  1920. i = (psr_cap[1] & DP_PSR_SETUP_TIME_MASK) >> DP_PSR_SETUP_TIME_SHIFT;
  1921. if (i >= ARRAY_SIZE(psr_setup_time_us))
  1922. return -EINVAL;
  1923. return psr_setup_time_us[i];
  1924. }
  1925. EXPORT_SYMBOL(drm_dp_psr_setup_time);
  1926. #undef PSR_SETUP_TIME
  1927. /**
  1928. * drm_dp_start_crc() - start capture of frame CRCs
  1929. * @aux: DisplayPort AUX channel
  1930. * @crtc: CRTC displaying the frames whose CRCs are to be captured
  1931. *
  1932. * Returns 0 on success or a negative error code on failure.
  1933. */
  1934. int drm_dp_start_crc(struct drm_dp_aux *aux, struct drm_crtc *crtc)
  1935. {
  1936. u8 buf;
  1937. int ret;
  1938. ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK, &buf);
  1939. if (ret < 0)
  1940. return ret;
  1941. ret = drm_dp_dpcd_writeb(aux, DP_TEST_SINK, buf | DP_TEST_SINK_START);
  1942. if (ret < 0)
  1943. return ret;
  1944. aux->crc_count = 0;
  1945. aux->crtc = crtc;
  1946. schedule_work(&aux->crc_work);
  1947. return 0;
  1948. }
  1949. EXPORT_SYMBOL(drm_dp_start_crc);
  1950. /**
  1951. * drm_dp_stop_crc() - stop capture of frame CRCs
  1952. * @aux: DisplayPort AUX channel
  1953. *
  1954. * Returns 0 on success or a negative error code on failure.
  1955. */
  1956. int drm_dp_stop_crc(struct drm_dp_aux *aux)
  1957. {
  1958. u8 buf;
  1959. int ret;
  1960. ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK, &buf);
  1961. if (ret < 0)
  1962. return ret;
  1963. ret = drm_dp_dpcd_writeb(aux, DP_TEST_SINK, buf & ~DP_TEST_SINK_START);
  1964. if (ret < 0)
  1965. return ret;
  1966. flush_work(&aux->crc_work);
  1967. aux->crtc = NULL;
  1968. return 0;
  1969. }
  1970. EXPORT_SYMBOL(drm_dp_stop_crc);
  1971. struct dpcd_quirk {
  1972. u8 oui[3];
  1973. u8 device_id[6];
  1974. bool is_branch;
  1975. u32 quirks;
  1976. };
  1977. #define OUI(first, second, third) { (first), (second), (third) }
  1978. #define DEVICE_ID(first, second, third, fourth, fifth, sixth) \
  1979. { (first), (second), (third), (fourth), (fifth), (sixth) }
  1980. #define DEVICE_ID_ANY DEVICE_ID(0, 0, 0, 0, 0, 0)
  1981. static const struct dpcd_quirk dpcd_quirk_list[] = {
  1982. /* Analogix 7737 needs reduced M and N at HBR2 link rates */
  1983. { OUI(0x00, 0x22, 0xb9), DEVICE_ID_ANY, true, BIT(DP_DPCD_QUIRK_CONSTANT_N) },
  1984. /* LG LP140WF6-SPM1 eDP panel */
  1985. { OUI(0x00, 0x22, 0xb9), DEVICE_ID('s', 'i', 'v', 'a', 'r', 'T'), false, BIT(DP_DPCD_QUIRK_CONSTANT_N) },
  1986. /* Apple panels need some additional handling to support PSR */
  1987. { OUI(0x00, 0x10, 0xfa), DEVICE_ID_ANY, false, BIT(DP_DPCD_QUIRK_NO_PSR) },
  1988. /* CH7511 seems to leave SINK_COUNT zeroed */
  1989. { OUI(0x00, 0x00, 0x00), DEVICE_ID('C', 'H', '7', '5', '1', '1'), false, BIT(DP_DPCD_QUIRK_NO_SINK_COUNT) },
  1990. /* Synaptics DP1.4 MST hubs can support DSC without virtual DPCD */
  1991. { OUI(0x90, 0xCC, 0x24), DEVICE_ID_ANY, true, BIT(DP_DPCD_QUIRK_DSC_WITHOUT_VIRTUAL_DPCD) },
  1992. /* Synaptics DP1.4 MST hubs require DSC for some modes on which it applies HBLANK expansion. */
  1993. { OUI(0x90, 0xCC, 0x24), DEVICE_ID_ANY, true, BIT(DP_DPCD_QUIRK_HBLANK_EXPANSION_REQUIRES_DSC) },
  1994. /* MediaTek panels (at least in U3224KBA) require DSC for modes with a short HBLANK on UHBR links. */
  1995. { OUI(0x00, 0x0C, 0xE7), DEVICE_ID_ANY, false, BIT(DP_DPCD_QUIRK_HBLANK_EXPANSION_REQUIRES_DSC) },
  1996. /* Apple MacBookPro 2017 15 inch eDP Retina panel reports too low DP_MAX_LINK_RATE */
  1997. { OUI(0x00, 0x10, 0xfa), DEVICE_ID(101, 68, 21, 101, 98, 97), false, BIT(DP_DPCD_QUIRK_CAN_DO_MAX_LINK_RATE_3_24_GBPS) },
  1998. };
  1999. #undef OUI
  2000. /*
  2001. * Get a bit mask of DPCD quirks for the sink/branch device identified by
  2002. * ident. The quirk data is shared but it's up to the drivers to act on the
  2003. * data.
  2004. *
  2005. * For now, only the OUI (first three bytes) is used, but this may be extended
  2006. * to device identification string and hardware/firmware revisions later.
  2007. */
  2008. static u32
  2009. drm_dp_get_quirks(const struct drm_dp_dpcd_ident *ident, bool is_branch)
  2010. {
  2011. const struct dpcd_quirk *quirk;
  2012. u32 quirks = 0;
  2013. int i;
  2014. u8 any_device[] = DEVICE_ID_ANY;
  2015. for (i = 0; i < ARRAY_SIZE(dpcd_quirk_list); i++) {
  2016. quirk = &dpcd_quirk_list[i];
  2017. if (quirk->is_branch != is_branch)
  2018. continue;
  2019. if (memcmp(quirk->oui, ident->oui, sizeof(ident->oui)) != 0)
  2020. continue;
  2021. if (memcmp(quirk->device_id, any_device, sizeof(any_device)) != 0 &&
  2022. memcmp(quirk->device_id, ident->device_id, sizeof(ident->device_id)) != 0)
  2023. continue;
  2024. quirks |= quirk->quirks;
  2025. }
  2026. return quirks;
  2027. }
  2028. #undef DEVICE_ID_ANY
  2029. #undef DEVICE_ID
  2030. static int drm_dp_read_ident(struct drm_dp_aux *aux, unsigned int offset,
  2031. struct drm_dp_dpcd_ident *ident)
  2032. {
  2033. int ret;
  2034. ret = drm_dp_dpcd_read(aux, offset, ident, sizeof(*ident));
  2035. return ret < 0 ? ret : 0;
  2036. }
  2037. static void drm_dp_dump_desc(struct drm_dp_aux *aux,
  2038. const char *device_name, const struct drm_dp_desc *desc)
  2039. {
  2040. const struct drm_dp_dpcd_ident *ident = &desc->ident;
  2041. drm_dbg_kms(aux->drm_dev,
  2042. "%s: %s: OUI %*phD dev-ID %*pE HW-rev %d.%d SW-rev %d.%d quirks 0x%04x\n",
  2043. aux->name, device_name,
  2044. (int)sizeof(ident->oui), ident->oui,
  2045. (int)strnlen(ident->device_id, sizeof(ident->device_id)), ident->device_id,
  2046. ident->hw_rev >> 4, ident->hw_rev & 0xf,
  2047. ident->sw_major_rev, ident->sw_minor_rev,
  2048. desc->quirks);
  2049. }
  2050. /**
  2051. * drm_dp_read_desc - read sink/branch descriptor from DPCD
  2052. * @aux: DisplayPort AUX channel
  2053. * @desc: Device descriptor to fill from DPCD
  2054. * @is_branch: true for branch devices, false for sink devices
  2055. *
  2056. * Read DPCD 0x400 (sink) or 0x500 (branch) into @desc. Also debug log the
  2057. * identification.
  2058. *
  2059. * Returns 0 on success or a negative error code on failure.
  2060. */
  2061. int drm_dp_read_desc(struct drm_dp_aux *aux, struct drm_dp_desc *desc,
  2062. bool is_branch)
  2063. {
  2064. struct drm_dp_dpcd_ident *ident = &desc->ident;
  2065. unsigned int offset = is_branch ? DP_BRANCH_OUI : DP_SINK_OUI;
  2066. int ret;
  2067. ret = drm_dp_read_ident(aux, offset, ident);
  2068. if (ret < 0)
  2069. return ret;
  2070. desc->quirks = drm_dp_get_quirks(ident, is_branch);
  2071. drm_dp_dump_desc(aux, is_branch ? "DP branch" : "DP sink", desc);
  2072. return 0;
  2073. }
  2074. EXPORT_SYMBOL(drm_dp_read_desc);
  2075. /**
  2076. * drm_dp_dump_lttpr_desc - read and dump the DPCD descriptor for an LTTPR PHY
  2077. * @aux: DisplayPort AUX channel
  2078. * @dp_phy: LTTPR PHY instance
  2079. *
  2080. * Read the DPCD LTTPR PHY descriptor for @dp_phy and print a debug message
  2081. * with its details to dmesg.
  2082. *
  2083. * Returns 0 on success or a negative error code on failure.
  2084. */
  2085. int drm_dp_dump_lttpr_desc(struct drm_dp_aux *aux, enum drm_dp_phy dp_phy)
  2086. {
  2087. struct drm_dp_desc desc = {};
  2088. int ret;
  2089. if (drm_WARN_ON(aux->drm_dev, dp_phy < DP_PHY_LTTPR1 || dp_phy > DP_MAX_LTTPR_COUNT))
  2090. return -EINVAL;
  2091. ret = drm_dp_read_ident(aux, DP_OUI_PHY_REPEATER(dp_phy), &desc.ident);
  2092. if (ret < 0)
  2093. return ret;
  2094. drm_dp_dump_desc(aux, drm_dp_phy_name(dp_phy), &desc);
  2095. return 0;
  2096. }
  2097. EXPORT_SYMBOL(drm_dp_dump_lttpr_desc);
  2098. /**
  2099. * drm_dp_dsc_sink_bpp_incr() - Get bits per pixel increment
  2100. * @dsc_dpcd: DSC capabilities from DPCD
  2101. *
  2102. * Returns the bpp precision supported by the DP sink.
  2103. */
  2104. u8 drm_dp_dsc_sink_bpp_incr(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE])
  2105. {
  2106. u8 bpp_increment_dpcd = dsc_dpcd[DP_DSC_BITS_PER_PIXEL_INC - DP_DSC_SUPPORT];
  2107. switch (bpp_increment_dpcd & DP_DSC_BITS_PER_PIXEL_MASK) {
  2108. case DP_DSC_BITS_PER_PIXEL_1_16:
  2109. return 16;
  2110. case DP_DSC_BITS_PER_PIXEL_1_8:
  2111. return 8;
  2112. case DP_DSC_BITS_PER_PIXEL_1_4:
  2113. return 4;
  2114. case DP_DSC_BITS_PER_PIXEL_1_2:
  2115. return 2;
  2116. case DP_DSC_BITS_PER_PIXEL_1_1:
  2117. return 1;
  2118. }
  2119. return 0;
  2120. }
  2121. EXPORT_SYMBOL(drm_dp_dsc_sink_bpp_incr);
  2122. /**
  2123. * drm_dp_dsc_sink_max_slice_count() - Get the max slice count
  2124. * supported by the DSC sink.
  2125. * @dsc_dpcd: DSC capabilities from DPCD
  2126. * @is_edp: true if its eDP, false for DP
  2127. *
  2128. * Read the slice capabilities DPCD register from DSC sink to get
  2129. * the maximum slice count supported. This is used to populate
  2130. * the DSC parameters in the &struct drm_dsc_config by the driver.
  2131. * Driver creates an infoframe using these parameters to populate
  2132. * &struct drm_dsc_pps_infoframe. These are sent to the sink using DSC
  2133. * infoframe using the helper function drm_dsc_pps_infoframe_pack()
  2134. *
  2135. * Returns:
  2136. * Maximum slice count supported by DSC sink or 0 its invalid
  2137. */
  2138. u8 drm_dp_dsc_sink_max_slice_count(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE],
  2139. bool is_edp)
  2140. {
  2141. u8 slice_cap1 = dsc_dpcd[DP_DSC_SLICE_CAP_1 - DP_DSC_SUPPORT];
  2142. if (is_edp) {
  2143. /* For eDP, register DSC_SLICE_CAPABILITIES_1 gives slice count */
  2144. if (slice_cap1 & DP_DSC_4_PER_DP_DSC_SINK)
  2145. return 4;
  2146. if (slice_cap1 & DP_DSC_2_PER_DP_DSC_SINK)
  2147. return 2;
  2148. if (slice_cap1 & DP_DSC_1_PER_DP_DSC_SINK)
  2149. return 1;
  2150. } else {
  2151. /* For DP, use values from DSC_SLICE_CAP_1 and DSC_SLICE_CAP2 */
  2152. u8 slice_cap2 = dsc_dpcd[DP_DSC_SLICE_CAP_2 - DP_DSC_SUPPORT];
  2153. if (slice_cap2 & DP_DSC_24_PER_DP_DSC_SINK)
  2154. return 24;
  2155. if (slice_cap2 & DP_DSC_20_PER_DP_DSC_SINK)
  2156. return 20;
  2157. if (slice_cap2 & DP_DSC_16_PER_DP_DSC_SINK)
  2158. return 16;
  2159. if (slice_cap1 & DP_DSC_12_PER_DP_DSC_SINK)
  2160. return 12;
  2161. if (slice_cap1 & DP_DSC_10_PER_DP_DSC_SINK)
  2162. return 10;
  2163. if (slice_cap1 & DP_DSC_8_PER_DP_DSC_SINK)
  2164. return 8;
  2165. if (slice_cap1 & DP_DSC_6_PER_DP_DSC_SINK)
  2166. return 6;
  2167. if (slice_cap1 & DP_DSC_4_PER_DP_DSC_SINK)
  2168. return 4;
  2169. if (slice_cap1 & DP_DSC_2_PER_DP_DSC_SINK)
  2170. return 2;
  2171. if (slice_cap1 & DP_DSC_1_PER_DP_DSC_SINK)
  2172. return 1;
  2173. }
  2174. return 0;
  2175. }
  2176. EXPORT_SYMBOL(drm_dp_dsc_sink_max_slice_count);
  2177. /**
  2178. * drm_dp_dsc_sink_line_buf_depth() - Get the line buffer depth in bits
  2179. * @dsc_dpcd: DSC capabilities from DPCD
  2180. *
  2181. * Read the DSC DPCD register to parse the line buffer depth in bits which is
  2182. * number of bits of precision within the decoder line buffer supported by
  2183. * the DSC sink. This is used to populate the DSC parameters in the
  2184. * &struct drm_dsc_config by the driver.
  2185. * Driver creates an infoframe using these parameters to populate
  2186. * &struct drm_dsc_pps_infoframe. These are sent to the sink using DSC
  2187. * infoframe using the helper function drm_dsc_pps_infoframe_pack()
  2188. *
  2189. * Returns:
  2190. * Line buffer depth supported by DSC panel or 0 its invalid
  2191. */
  2192. u8 drm_dp_dsc_sink_line_buf_depth(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE])
  2193. {
  2194. u8 line_buf_depth = dsc_dpcd[DP_DSC_LINE_BUF_BIT_DEPTH - DP_DSC_SUPPORT];
  2195. switch (line_buf_depth & DP_DSC_LINE_BUF_BIT_DEPTH_MASK) {
  2196. case DP_DSC_LINE_BUF_BIT_DEPTH_9:
  2197. return 9;
  2198. case DP_DSC_LINE_BUF_BIT_DEPTH_10:
  2199. return 10;
  2200. case DP_DSC_LINE_BUF_BIT_DEPTH_11:
  2201. return 11;
  2202. case DP_DSC_LINE_BUF_BIT_DEPTH_12:
  2203. return 12;
  2204. case DP_DSC_LINE_BUF_BIT_DEPTH_13:
  2205. return 13;
  2206. case DP_DSC_LINE_BUF_BIT_DEPTH_14:
  2207. return 14;
  2208. case DP_DSC_LINE_BUF_BIT_DEPTH_15:
  2209. return 15;
  2210. case DP_DSC_LINE_BUF_BIT_DEPTH_16:
  2211. return 16;
  2212. case DP_DSC_LINE_BUF_BIT_DEPTH_8:
  2213. return 8;
  2214. }
  2215. return 0;
  2216. }
  2217. EXPORT_SYMBOL(drm_dp_dsc_sink_line_buf_depth);
  2218. /**
  2219. * drm_dp_dsc_sink_supported_input_bpcs() - Get all the input bits per component
  2220. * values supported by the DSC sink.
  2221. * @dsc_dpcd: DSC capabilities from DPCD
  2222. * @dsc_bpc: An array to be filled by this helper with supported
  2223. * input bpcs.
  2224. *
  2225. * Read the DSC DPCD from the sink device to parse the supported bits per
  2226. * component values. This is used to populate the DSC parameters
  2227. * in the &struct drm_dsc_config by the driver.
  2228. * Driver creates an infoframe using these parameters to populate
  2229. * &struct drm_dsc_pps_infoframe. These are sent to the sink using DSC
  2230. * infoframe using the helper function drm_dsc_pps_infoframe_pack()
  2231. *
  2232. * Returns:
  2233. * Number of input BPC values parsed from the DPCD
  2234. */
  2235. int drm_dp_dsc_sink_supported_input_bpcs(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE],
  2236. u8 dsc_bpc[3])
  2237. {
  2238. int num_bpc = 0;
  2239. u8 color_depth = dsc_dpcd[DP_DSC_DEC_COLOR_DEPTH_CAP - DP_DSC_SUPPORT];
  2240. if (!drm_dp_sink_supports_dsc(dsc_dpcd))
  2241. return 0;
  2242. if (color_depth & DP_DSC_12_BPC)
  2243. dsc_bpc[num_bpc++] = 12;
  2244. if (color_depth & DP_DSC_10_BPC)
  2245. dsc_bpc[num_bpc++] = 10;
  2246. /* A DP DSC Sink device shall support 8 bpc. */
  2247. dsc_bpc[num_bpc++] = 8;
  2248. return num_bpc;
  2249. }
  2250. EXPORT_SYMBOL(drm_dp_dsc_sink_supported_input_bpcs);
  2251. static int drm_dp_read_lttpr_regs(struct drm_dp_aux *aux,
  2252. const u8 dpcd[DP_RECEIVER_CAP_SIZE], int address,
  2253. u8 *buf, int buf_size)
  2254. {
  2255. /*
  2256. * At least the DELL P2715Q monitor with a DPCD_REV < 0x14 returns
  2257. * corrupted values when reading from the 0xF0000- range with a block
  2258. * size bigger than 1.
  2259. */
  2260. int block_size = dpcd[DP_DPCD_REV] < 0x14 ? 1 : buf_size;
  2261. int offset;
  2262. int ret;
  2263. for (offset = 0; offset < buf_size; offset += block_size) {
  2264. ret = drm_dp_dpcd_read(aux,
  2265. address + offset,
  2266. &buf[offset], block_size);
  2267. if (ret < 0)
  2268. return ret;
  2269. WARN_ON(ret != block_size);
  2270. }
  2271. return 0;
  2272. }
  2273. /**
  2274. * drm_dp_read_lttpr_common_caps - read the LTTPR common capabilities
  2275. * @aux: DisplayPort AUX channel
  2276. * @dpcd: DisplayPort configuration data
  2277. * @caps: buffer to return the capability info in
  2278. *
  2279. * Read capabilities common to all LTTPRs.
  2280. *
  2281. * Returns 0 on success or a negative error code on failure.
  2282. */
  2283. int drm_dp_read_lttpr_common_caps(struct drm_dp_aux *aux,
  2284. const u8 dpcd[DP_RECEIVER_CAP_SIZE],
  2285. u8 caps[DP_LTTPR_COMMON_CAP_SIZE])
  2286. {
  2287. return drm_dp_read_lttpr_regs(aux, dpcd,
  2288. DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV,
  2289. caps, DP_LTTPR_COMMON_CAP_SIZE);
  2290. }
  2291. EXPORT_SYMBOL(drm_dp_read_lttpr_common_caps);
  2292. /**
  2293. * drm_dp_read_lttpr_phy_caps - read the capabilities for a given LTTPR PHY
  2294. * @aux: DisplayPort AUX channel
  2295. * @dpcd: DisplayPort configuration data
  2296. * @dp_phy: LTTPR PHY to read the capabilities for
  2297. * @caps: buffer to return the capability info in
  2298. *
  2299. * Read the capabilities for the given LTTPR PHY.
  2300. *
  2301. * Returns 0 on success or a negative error code on failure.
  2302. */
  2303. int drm_dp_read_lttpr_phy_caps(struct drm_dp_aux *aux,
  2304. const u8 dpcd[DP_RECEIVER_CAP_SIZE],
  2305. enum drm_dp_phy dp_phy,
  2306. u8 caps[DP_LTTPR_PHY_CAP_SIZE])
  2307. {
  2308. return drm_dp_read_lttpr_regs(aux, dpcd,
  2309. DP_TRAINING_AUX_RD_INTERVAL_PHY_REPEATER(dp_phy),
  2310. caps, DP_LTTPR_PHY_CAP_SIZE);
  2311. }
  2312. EXPORT_SYMBOL(drm_dp_read_lttpr_phy_caps);
  2313. static u8 dp_lttpr_common_cap(const u8 caps[DP_LTTPR_COMMON_CAP_SIZE], int r)
  2314. {
  2315. return caps[r - DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV];
  2316. }
  2317. /**
  2318. * drm_dp_lttpr_count - get the number of detected LTTPRs
  2319. * @caps: LTTPR common capabilities
  2320. *
  2321. * Get the number of detected LTTPRs from the LTTPR common capabilities info.
  2322. *
  2323. * Returns:
  2324. * -ERANGE if more than supported number (8) of LTTPRs are detected
  2325. * -EINVAL if the DP_PHY_REPEATER_CNT register contains an invalid value
  2326. * otherwise the number of detected LTTPRs
  2327. */
  2328. int drm_dp_lttpr_count(const u8 caps[DP_LTTPR_COMMON_CAP_SIZE])
  2329. {
  2330. u8 count = dp_lttpr_common_cap(caps, DP_PHY_REPEATER_CNT);
  2331. switch (hweight8(count)) {
  2332. case 0:
  2333. return 0;
  2334. case 1:
  2335. return 8 - ilog2(count);
  2336. case 8:
  2337. return -ERANGE;
  2338. default:
  2339. return -EINVAL;
  2340. }
  2341. }
  2342. EXPORT_SYMBOL(drm_dp_lttpr_count);
  2343. /**
  2344. * drm_dp_lttpr_max_link_rate - get the maximum link rate supported by all LTTPRs
  2345. * @caps: LTTPR common capabilities
  2346. *
  2347. * Returns the maximum link rate supported by all detected LTTPRs.
  2348. */
  2349. int drm_dp_lttpr_max_link_rate(const u8 caps[DP_LTTPR_COMMON_CAP_SIZE])
  2350. {
  2351. u8 rate = dp_lttpr_common_cap(caps, DP_MAX_LINK_RATE_PHY_REPEATER);
  2352. return drm_dp_bw_code_to_link_rate(rate);
  2353. }
  2354. EXPORT_SYMBOL(drm_dp_lttpr_max_link_rate);
  2355. /**
  2356. * drm_dp_lttpr_max_lane_count - get the maximum lane count supported by all LTTPRs
  2357. * @caps: LTTPR common capabilities
  2358. *
  2359. * Returns the maximum lane count supported by all detected LTTPRs.
  2360. */
  2361. int drm_dp_lttpr_max_lane_count(const u8 caps[DP_LTTPR_COMMON_CAP_SIZE])
  2362. {
  2363. u8 max_lanes = dp_lttpr_common_cap(caps, DP_MAX_LANE_COUNT_PHY_REPEATER);
  2364. return max_lanes & DP_MAX_LANE_COUNT_MASK;
  2365. }
  2366. EXPORT_SYMBOL(drm_dp_lttpr_max_lane_count);
  2367. /**
  2368. * drm_dp_lttpr_voltage_swing_level_3_supported - check for LTTPR vswing3 support
  2369. * @caps: LTTPR PHY capabilities
  2370. *
  2371. * Returns true if the @caps for an LTTPR TX PHY indicate support for
  2372. * voltage swing level 3.
  2373. */
  2374. bool
  2375. drm_dp_lttpr_voltage_swing_level_3_supported(const u8 caps[DP_LTTPR_PHY_CAP_SIZE])
  2376. {
  2377. u8 txcap = dp_lttpr_phy_cap(caps, DP_TRANSMITTER_CAPABILITY_PHY_REPEATER1);
  2378. return txcap & DP_VOLTAGE_SWING_LEVEL_3_SUPPORTED;
  2379. }
  2380. EXPORT_SYMBOL(drm_dp_lttpr_voltage_swing_level_3_supported);
  2381. /**
  2382. * drm_dp_lttpr_pre_emphasis_level_3_supported - check for LTTPR preemph3 support
  2383. * @caps: LTTPR PHY capabilities
  2384. *
  2385. * Returns true if the @caps for an LTTPR TX PHY indicate support for
  2386. * pre-emphasis level 3.
  2387. */
  2388. bool
  2389. drm_dp_lttpr_pre_emphasis_level_3_supported(const u8 caps[DP_LTTPR_PHY_CAP_SIZE])
  2390. {
  2391. u8 txcap = dp_lttpr_phy_cap(caps, DP_TRANSMITTER_CAPABILITY_PHY_REPEATER1);
  2392. return txcap & DP_PRE_EMPHASIS_LEVEL_3_SUPPORTED;
  2393. }
  2394. EXPORT_SYMBOL(drm_dp_lttpr_pre_emphasis_level_3_supported);
  2395. /**
  2396. * drm_dp_get_phy_test_pattern() - get the requested pattern from the sink.
  2397. * @aux: DisplayPort AUX channel
  2398. * @data: DP phy compliance test parameters.
  2399. *
  2400. * Returns 0 on success or a negative error code on failure.
  2401. */
  2402. int drm_dp_get_phy_test_pattern(struct drm_dp_aux *aux,
  2403. struct drm_dp_phy_test_params *data)
  2404. {
  2405. int err;
  2406. u8 rate, lanes;
  2407. err = drm_dp_dpcd_readb(aux, DP_TEST_LINK_RATE, &rate);
  2408. if (err < 0)
  2409. return err;
  2410. data->link_rate = drm_dp_bw_code_to_link_rate(rate);
  2411. err = drm_dp_dpcd_readb(aux, DP_TEST_LANE_COUNT, &lanes);
  2412. if (err < 0)
  2413. return err;
  2414. data->num_lanes = lanes & DP_MAX_LANE_COUNT_MASK;
  2415. if (lanes & DP_ENHANCED_FRAME_CAP)
  2416. data->enhanced_frame_cap = true;
  2417. err = drm_dp_dpcd_readb(aux, DP_PHY_TEST_PATTERN, &data->phy_pattern);
  2418. if (err < 0)
  2419. return err;
  2420. switch (data->phy_pattern) {
  2421. case DP_PHY_TEST_PATTERN_80BIT_CUSTOM:
  2422. err = drm_dp_dpcd_read(aux, DP_TEST_80BIT_CUSTOM_PATTERN_7_0,
  2423. &data->custom80, sizeof(data->custom80));
  2424. if (err < 0)
  2425. return err;
  2426. break;
  2427. case DP_PHY_TEST_PATTERN_CP2520:
  2428. err = drm_dp_dpcd_read(aux, DP_TEST_HBR2_SCRAMBLER_RESET,
  2429. &data->hbr2_reset,
  2430. sizeof(data->hbr2_reset));
  2431. if (err < 0)
  2432. return err;
  2433. }
  2434. return 0;
  2435. }
  2436. EXPORT_SYMBOL(drm_dp_get_phy_test_pattern);
  2437. /**
  2438. * drm_dp_set_phy_test_pattern() - set the pattern to the sink.
  2439. * @aux: DisplayPort AUX channel
  2440. * @data: DP phy compliance test parameters.
  2441. * @dp_rev: DP revision to use for compliance testing
  2442. *
  2443. * Returns 0 on success or a negative error code on failure.
  2444. */
  2445. int drm_dp_set_phy_test_pattern(struct drm_dp_aux *aux,
  2446. struct drm_dp_phy_test_params *data, u8 dp_rev)
  2447. {
  2448. int err, i;
  2449. u8 test_pattern;
  2450. test_pattern = data->phy_pattern;
  2451. if (dp_rev < 0x12) {
  2452. test_pattern = (test_pattern << 2) &
  2453. DP_LINK_QUAL_PATTERN_11_MASK;
  2454. err = drm_dp_dpcd_writeb(aux, DP_TRAINING_PATTERN_SET,
  2455. test_pattern);
  2456. if (err < 0)
  2457. return err;
  2458. } else {
  2459. for (i = 0; i < data->num_lanes; i++) {
  2460. err = drm_dp_dpcd_writeb(aux,
  2461. DP_LINK_QUAL_LANE0_SET + i,
  2462. test_pattern);
  2463. if (err < 0)
  2464. return err;
  2465. }
  2466. }
  2467. return 0;
  2468. }
  2469. EXPORT_SYMBOL(drm_dp_set_phy_test_pattern);
  2470. static const char *dp_pixelformat_get_name(enum dp_pixelformat pixelformat)
  2471. {
  2472. if (pixelformat < 0 || pixelformat > DP_PIXELFORMAT_RESERVED)
  2473. return "Invalid";
  2474. switch (pixelformat) {
  2475. case DP_PIXELFORMAT_RGB:
  2476. return "RGB";
  2477. case DP_PIXELFORMAT_YUV444:
  2478. return "YUV444";
  2479. case DP_PIXELFORMAT_YUV422:
  2480. return "YUV422";
  2481. case DP_PIXELFORMAT_YUV420:
  2482. return "YUV420";
  2483. case DP_PIXELFORMAT_Y_ONLY:
  2484. return "Y_ONLY";
  2485. case DP_PIXELFORMAT_RAW:
  2486. return "RAW";
  2487. default:
  2488. return "Reserved";
  2489. }
  2490. }
  2491. static const char *dp_colorimetry_get_name(enum dp_pixelformat pixelformat,
  2492. enum dp_colorimetry colorimetry)
  2493. {
  2494. if (pixelformat < 0 || pixelformat > DP_PIXELFORMAT_RESERVED)
  2495. return "Invalid";
  2496. switch (colorimetry) {
  2497. case DP_COLORIMETRY_DEFAULT:
  2498. switch (pixelformat) {
  2499. case DP_PIXELFORMAT_RGB:
  2500. return "sRGB";
  2501. case DP_PIXELFORMAT_YUV444:
  2502. case DP_PIXELFORMAT_YUV422:
  2503. case DP_PIXELFORMAT_YUV420:
  2504. return "BT.601";
  2505. case DP_PIXELFORMAT_Y_ONLY:
  2506. return "DICOM PS3.14";
  2507. case DP_PIXELFORMAT_RAW:
  2508. return "Custom Color Profile";
  2509. default:
  2510. return "Reserved";
  2511. }
  2512. case DP_COLORIMETRY_RGB_WIDE_FIXED: /* and DP_COLORIMETRY_BT709_YCC */
  2513. switch (pixelformat) {
  2514. case DP_PIXELFORMAT_RGB:
  2515. return "Wide Fixed";
  2516. case DP_PIXELFORMAT_YUV444:
  2517. case DP_PIXELFORMAT_YUV422:
  2518. case DP_PIXELFORMAT_YUV420:
  2519. return "BT.709";
  2520. default:
  2521. return "Reserved";
  2522. }
  2523. case DP_COLORIMETRY_RGB_WIDE_FLOAT: /* and DP_COLORIMETRY_XVYCC_601 */
  2524. switch (pixelformat) {
  2525. case DP_PIXELFORMAT_RGB:
  2526. return "Wide Float";
  2527. case DP_PIXELFORMAT_YUV444:
  2528. case DP_PIXELFORMAT_YUV422:
  2529. case DP_PIXELFORMAT_YUV420:
  2530. return "xvYCC 601";
  2531. default:
  2532. return "Reserved";
  2533. }
  2534. case DP_COLORIMETRY_OPRGB: /* and DP_COLORIMETRY_XVYCC_709 */
  2535. switch (pixelformat) {
  2536. case DP_PIXELFORMAT_RGB:
  2537. return "OpRGB";
  2538. case DP_PIXELFORMAT_YUV444:
  2539. case DP_PIXELFORMAT_YUV422:
  2540. case DP_PIXELFORMAT_YUV420:
  2541. return "xvYCC 709";
  2542. default:
  2543. return "Reserved";
  2544. }
  2545. case DP_COLORIMETRY_DCI_P3_RGB: /* and DP_COLORIMETRY_SYCC_601 */
  2546. switch (pixelformat) {
  2547. case DP_PIXELFORMAT_RGB:
  2548. return "DCI-P3";
  2549. case DP_PIXELFORMAT_YUV444:
  2550. case DP_PIXELFORMAT_YUV422:
  2551. case DP_PIXELFORMAT_YUV420:
  2552. return "sYCC 601";
  2553. default:
  2554. return "Reserved";
  2555. }
  2556. case DP_COLORIMETRY_RGB_CUSTOM: /* and DP_COLORIMETRY_OPYCC_601 */
  2557. switch (pixelformat) {
  2558. case DP_PIXELFORMAT_RGB:
  2559. return "Custom Profile";
  2560. case DP_PIXELFORMAT_YUV444:
  2561. case DP_PIXELFORMAT_YUV422:
  2562. case DP_PIXELFORMAT_YUV420:
  2563. return "OpYCC 601";
  2564. default:
  2565. return "Reserved";
  2566. }
  2567. case DP_COLORIMETRY_BT2020_RGB: /* and DP_COLORIMETRY_BT2020_CYCC */
  2568. switch (pixelformat) {
  2569. case DP_PIXELFORMAT_RGB:
  2570. return "BT.2020 RGB";
  2571. case DP_PIXELFORMAT_YUV444:
  2572. case DP_PIXELFORMAT_YUV422:
  2573. case DP_PIXELFORMAT_YUV420:
  2574. return "BT.2020 CYCC";
  2575. default:
  2576. return "Reserved";
  2577. }
  2578. case DP_COLORIMETRY_BT2020_YCC:
  2579. switch (pixelformat) {
  2580. case DP_PIXELFORMAT_YUV444:
  2581. case DP_PIXELFORMAT_YUV422:
  2582. case DP_PIXELFORMAT_YUV420:
  2583. return "BT.2020 YCC";
  2584. default:
  2585. return "Reserved";
  2586. }
  2587. default:
  2588. return "Invalid";
  2589. }
  2590. }
  2591. static const char *dp_dynamic_range_get_name(enum dp_dynamic_range dynamic_range)
  2592. {
  2593. switch (dynamic_range) {
  2594. case DP_DYNAMIC_RANGE_VESA:
  2595. return "VESA range";
  2596. case DP_DYNAMIC_RANGE_CTA:
  2597. return "CTA range";
  2598. default:
  2599. return "Invalid";
  2600. }
  2601. }
  2602. static const char *dp_content_type_get_name(enum dp_content_type content_type)
  2603. {
  2604. switch (content_type) {
  2605. case DP_CONTENT_TYPE_NOT_DEFINED:
  2606. return "Not defined";
  2607. case DP_CONTENT_TYPE_GRAPHICS:
  2608. return "Graphics";
  2609. case DP_CONTENT_TYPE_PHOTO:
  2610. return "Photo";
  2611. case DP_CONTENT_TYPE_VIDEO:
  2612. return "Video";
  2613. case DP_CONTENT_TYPE_GAME:
  2614. return "Game";
  2615. default:
  2616. return "Reserved";
  2617. }
  2618. }
  2619. void drm_dp_vsc_sdp_log(struct drm_printer *p, const struct drm_dp_vsc_sdp *vsc)
  2620. {
  2621. drm_printf(p, "DP SDP: VSC, revision %u, length %u\n",
  2622. vsc->revision, vsc->length);
  2623. drm_printf(p, " pixelformat: %s\n",
  2624. dp_pixelformat_get_name(vsc->pixelformat));
  2625. drm_printf(p, " colorimetry: %s\n",
  2626. dp_colorimetry_get_name(vsc->pixelformat, vsc->colorimetry));
  2627. drm_printf(p, " bpc: %u\n", vsc->bpc);
  2628. drm_printf(p, " dynamic range: %s\n",
  2629. dp_dynamic_range_get_name(vsc->dynamic_range));
  2630. drm_printf(p, " content type: %s\n",
  2631. dp_content_type_get_name(vsc->content_type));
  2632. }
  2633. EXPORT_SYMBOL(drm_dp_vsc_sdp_log);
  2634. void drm_dp_as_sdp_log(struct drm_printer *p, const struct drm_dp_as_sdp *as_sdp)
  2635. {
  2636. drm_printf(p, "DP SDP: AS_SDP, revision %u, length %u\n",
  2637. as_sdp->revision, as_sdp->length);
  2638. drm_printf(p, " vtotal: %d\n", as_sdp->vtotal);
  2639. drm_printf(p, " target_rr: %d\n", as_sdp->target_rr);
  2640. drm_printf(p, " duration_incr_ms: %d\n", as_sdp->duration_incr_ms);
  2641. drm_printf(p, " duration_decr_ms: %d\n", as_sdp->duration_decr_ms);
  2642. drm_printf(p, " operation_mode: %d\n", as_sdp->mode);
  2643. }
  2644. EXPORT_SYMBOL(drm_dp_as_sdp_log);
  2645. /**
  2646. * drm_dp_as_sdp_supported() - check if adaptive sync sdp is supported
  2647. * @aux: DisplayPort AUX channel
  2648. * @dpcd: DisplayPort configuration data
  2649. *
  2650. * Returns true if adaptive sync sdp is supported, else returns false
  2651. */
  2652. bool drm_dp_as_sdp_supported(struct drm_dp_aux *aux, const u8 dpcd[DP_RECEIVER_CAP_SIZE])
  2653. {
  2654. u8 rx_feature;
  2655. if (dpcd[DP_DPCD_REV] < DP_DPCD_REV_13)
  2656. return false;
  2657. if (drm_dp_dpcd_readb(aux, DP_DPRX_FEATURE_ENUMERATION_LIST_CONT_1,
  2658. &rx_feature) != 1) {
  2659. drm_dbg_dp(aux->drm_dev,
  2660. "Failed to read DP_DPRX_FEATURE_ENUMERATION_LIST_CONT_1\n");
  2661. return false;
  2662. }
  2663. return (rx_feature & DP_ADAPTIVE_SYNC_SDP_SUPPORTED);
  2664. }
  2665. EXPORT_SYMBOL(drm_dp_as_sdp_supported);
  2666. /**
  2667. * drm_dp_vsc_sdp_supported() - check if vsc sdp is supported
  2668. * @aux: DisplayPort AUX channel
  2669. * @dpcd: DisplayPort configuration data
  2670. *
  2671. * Returns true if vsc sdp is supported, else returns false
  2672. */
  2673. bool drm_dp_vsc_sdp_supported(struct drm_dp_aux *aux, const u8 dpcd[DP_RECEIVER_CAP_SIZE])
  2674. {
  2675. u8 rx_feature;
  2676. if (dpcd[DP_DPCD_REV] < DP_DPCD_REV_13)
  2677. return false;
  2678. if (drm_dp_dpcd_readb(aux, DP_DPRX_FEATURE_ENUMERATION_LIST, &rx_feature) != 1) {
  2679. drm_dbg_dp(aux->drm_dev, "failed to read DP_DPRX_FEATURE_ENUMERATION_LIST\n");
  2680. return false;
  2681. }
  2682. return (rx_feature & DP_VSC_SDP_EXT_FOR_COLORIMETRY_SUPPORTED);
  2683. }
  2684. EXPORT_SYMBOL(drm_dp_vsc_sdp_supported);
  2685. /**
  2686. * drm_dp_vsc_sdp_pack() - pack a given vsc sdp into generic dp_sdp
  2687. * @vsc: vsc sdp initialized according to its purpose as defined in
  2688. * table 2-118 - table 2-120 in DP 1.4a specification
  2689. * @sdp: valid handle to the generic dp_sdp which will be packed
  2690. *
  2691. * Returns length of sdp on success and error code on failure
  2692. */
  2693. ssize_t drm_dp_vsc_sdp_pack(const struct drm_dp_vsc_sdp *vsc,
  2694. struct dp_sdp *sdp)
  2695. {
  2696. size_t length = sizeof(struct dp_sdp);
  2697. memset(sdp, 0, sizeof(struct dp_sdp));
  2698. /*
  2699. * Prepare VSC Header for SU as per DP 1.4a spec, Table 2-119
  2700. * VSC SDP Header Bytes
  2701. */
  2702. sdp->sdp_header.HB0 = 0; /* Secondary-Data Packet ID = 0 */
  2703. sdp->sdp_header.HB1 = vsc->sdp_type; /* Secondary-data Packet Type */
  2704. sdp->sdp_header.HB2 = vsc->revision; /* Revision Number */
  2705. sdp->sdp_header.HB3 = vsc->length; /* Number of Valid Data Bytes */
  2706. if (vsc->revision == 0x6) {
  2707. sdp->db[0] = 1;
  2708. sdp->db[3] = 1;
  2709. }
  2710. /*
  2711. * Revision 0x5 and revision 0x7 supports Pixel Encoding/Colorimetry
  2712. * Format as per DP 1.4a spec and DP 2.0 respectively.
  2713. */
  2714. if (!(vsc->revision == 0x5 || vsc->revision == 0x7))
  2715. goto out;
  2716. /* VSC SDP Payload for DB16 through DB18 */
  2717. /* Pixel Encoding and Colorimetry Formats */
  2718. sdp->db[16] = (vsc->pixelformat & 0xf) << 4; /* DB16[7:4] */
  2719. sdp->db[16] |= vsc->colorimetry & 0xf; /* DB16[3:0] */
  2720. switch (vsc->bpc) {
  2721. case 6:
  2722. /* 6bpc: 0x0 */
  2723. break;
  2724. case 8:
  2725. sdp->db[17] = 0x1; /* DB17[3:0] */
  2726. break;
  2727. case 10:
  2728. sdp->db[17] = 0x2;
  2729. break;
  2730. case 12:
  2731. sdp->db[17] = 0x3;
  2732. break;
  2733. case 16:
  2734. sdp->db[17] = 0x4;
  2735. break;
  2736. default:
  2737. WARN(1, "Missing case %d\n", vsc->bpc);
  2738. return -EINVAL;
  2739. }
  2740. /* Dynamic Range and Component Bit Depth */
  2741. if (vsc->dynamic_range == DP_DYNAMIC_RANGE_CTA)
  2742. sdp->db[17] |= 0x80; /* DB17[7] */
  2743. /* Content Type */
  2744. sdp->db[18] = vsc->content_type & 0x7;
  2745. out:
  2746. return length;
  2747. }
  2748. EXPORT_SYMBOL(drm_dp_vsc_sdp_pack);
  2749. /**
  2750. * drm_dp_get_pcon_max_frl_bw() - maximum frl supported by PCON
  2751. * @dpcd: DisplayPort configuration data
  2752. * @port_cap: port capabilities
  2753. *
  2754. * Returns maximum frl bandwidth supported by PCON in GBPS,
  2755. * returns 0 if not supported.
  2756. */
  2757. int drm_dp_get_pcon_max_frl_bw(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
  2758. const u8 port_cap[4])
  2759. {
  2760. int bw;
  2761. u8 buf;
  2762. buf = port_cap[2];
  2763. bw = buf & DP_PCON_MAX_FRL_BW;
  2764. switch (bw) {
  2765. case DP_PCON_MAX_9GBPS:
  2766. return 9;
  2767. case DP_PCON_MAX_18GBPS:
  2768. return 18;
  2769. case DP_PCON_MAX_24GBPS:
  2770. return 24;
  2771. case DP_PCON_MAX_32GBPS:
  2772. return 32;
  2773. case DP_PCON_MAX_40GBPS:
  2774. return 40;
  2775. case DP_PCON_MAX_48GBPS:
  2776. return 48;
  2777. case DP_PCON_MAX_0GBPS:
  2778. default:
  2779. return 0;
  2780. }
  2781. return 0;
  2782. }
  2783. EXPORT_SYMBOL(drm_dp_get_pcon_max_frl_bw);
  2784. /**
  2785. * drm_dp_pcon_frl_prepare() - Prepare PCON for FRL.
  2786. * @aux: DisplayPort AUX channel
  2787. * @enable_frl_ready_hpd: Configure DP_PCON_ENABLE_HPD_READY.
  2788. *
  2789. * Returns 0 if success, else returns negative error code.
  2790. */
  2791. int drm_dp_pcon_frl_prepare(struct drm_dp_aux *aux, bool enable_frl_ready_hpd)
  2792. {
  2793. int ret;
  2794. u8 buf = DP_PCON_ENABLE_SOURCE_CTL_MODE |
  2795. DP_PCON_ENABLE_LINK_FRL_MODE;
  2796. if (enable_frl_ready_hpd)
  2797. buf |= DP_PCON_ENABLE_HPD_READY;
  2798. ret = drm_dp_dpcd_writeb(aux, DP_PCON_HDMI_LINK_CONFIG_1, buf);
  2799. return ret;
  2800. }
  2801. EXPORT_SYMBOL(drm_dp_pcon_frl_prepare);
  2802. /**
  2803. * drm_dp_pcon_is_frl_ready() - Is PCON ready for FRL
  2804. * @aux: DisplayPort AUX channel
  2805. *
  2806. * Returns true if success, else returns false.
  2807. */
  2808. bool drm_dp_pcon_is_frl_ready(struct drm_dp_aux *aux)
  2809. {
  2810. int ret;
  2811. u8 buf;
  2812. ret = drm_dp_dpcd_readb(aux, DP_PCON_HDMI_TX_LINK_STATUS, &buf);
  2813. if (ret < 0)
  2814. return false;
  2815. if (buf & DP_PCON_FRL_READY)
  2816. return true;
  2817. return false;
  2818. }
  2819. EXPORT_SYMBOL(drm_dp_pcon_is_frl_ready);
  2820. /**
  2821. * drm_dp_pcon_frl_configure_1() - Set HDMI LINK Configuration-Step1
  2822. * @aux: DisplayPort AUX channel
  2823. * @max_frl_gbps: maximum frl bw to be configured between PCON and HDMI sink
  2824. * @frl_mode: FRL Training mode, it can be either Concurrent or Sequential.
  2825. * In Concurrent Mode, the FRL link bring up can be done along with
  2826. * DP Link training. In Sequential mode, the FRL link bring up is done prior to
  2827. * the DP Link training.
  2828. *
  2829. * Returns 0 if success, else returns negative error code.
  2830. */
  2831. int drm_dp_pcon_frl_configure_1(struct drm_dp_aux *aux, int max_frl_gbps,
  2832. u8 frl_mode)
  2833. {
  2834. int ret;
  2835. u8 buf;
  2836. ret = drm_dp_dpcd_readb(aux, DP_PCON_HDMI_LINK_CONFIG_1, &buf);
  2837. if (ret < 0)
  2838. return ret;
  2839. if (frl_mode == DP_PCON_ENABLE_CONCURRENT_LINK)
  2840. buf |= DP_PCON_ENABLE_CONCURRENT_LINK;
  2841. else
  2842. buf &= ~DP_PCON_ENABLE_CONCURRENT_LINK;
  2843. switch (max_frl_gbps) {
  2844. case 9:
  2845. buf |= DP_PCON_ENABLE_MAX_BW_9GBPS;
  2846. break;
  2847. case 18:
  2848. buf |= DP_PCON_ENABLE_MAX_BW_18GBPS;
  2849. break;
  2850. case 24:
  2851. buf |= DP_PCON_ENABLE_MAX_BW_24GBPS;
  2852. break;
  2853. case 32:
  2854. buf |= DP_PCON_ENABLE_MAX_BW_32GBPS;
  2855. break;
  2856. case 40:
  2857. buf |= DP_PCON_ENABLE_MAX_BW_40GBPS;
  2858. break;
  2859. case 48:
  2860. buf |= DP_PCON_ENABLE_MAX_BW_48GBPS;
  2861. break;
  2862. case 0:
  2863. buf |= DP_PCON_ENABLE_MAX_BW_0GBPS;
  2864. break;
  2865. default:
  2866. return -EINVAL;
  2867. }
  2868. ret = drm_dp_dpcd_writeb(aux, DP_PCON_HDMI_LINK_CONFIG_1, buf);
  2869. if (ret < 0)
  2870. return ret;
  2871. return 0;
  2872. }
  2873. EXPORT_SYMBOL(drm_dp_pcon_frl_configure_1);
  2874. /**
  2875. * drm_dp_pcon_frl_configure_2() - Set HDMI Link configuration Step-2
  2876. * @aux: DisplayPort AUX channel
  2877. * @max_frl_mask : Max FRL BW to be tried by the PCON with HDMI Sink
  2878. * @frl_type : FRL training type, can be Extended, or Normal.
  2879. * In Normal FRL training, the PCON tries each frl bw from the max_frl_mask
  2880. * starting from min, and stops when link training is successful. In Extended
  2881. * FRL training, all frl bw selected in the mask are trained by the PCON.
  2882. *
  2883. * Returns 0 if success, else returns negative error code.
  2884. */
  2885. int drm_dp_pcon_frl_configure_2(struct drm_dp_aux *aux, int max_frl_mask,
  2886. u8 frl_type)
  2887. {
  2888. int ret;
  2889. u8 buf = max_frl_mask;
  2890. if (frl_type == DP_PCON_FRL_LINK_TRAIN_EXTENDED)
  2891. buf |= DP_PCON_FRL_LINK_TRAIN_EXTENDED;
  2892. else
  2893. buf &= ~DP_PCON_FRL_LINK_TRAIN_EXTENDED;
  2894. ret = drm_dp_dpcd_writeb(aux, DP_PCON_HDMI_LINK_CONFIG_2, buf);
  2895. if (ret < 0)
  2896. return ret;
  2897. return 0;
  2898. }
  2899. EXPORT_SYMBOL(drm_dp_pcon_frl_configure_2);
  2900. /**
  2901. * drm_dp_pcon_reset_frl_config() - Re-Set HDMI Link configuration.
  2902. * @aux: DisplayPort AUX channel
  2903. *
  2904. * Returns 0 if success, else returns negative error code.
  2905. */
  2906. int drm_dp_pcon_reset_frl_config(struct drm_dp_aux *aux)
  2907. {
  2908. int ret;
  2909. ret = drm_dp_dpcd_writeb(aux, DP_PCON_HDMI_LINK_CONFIG_1, 0x0);
  2910. if (ret < 0)
  2911. return ret;
  2912. return 0;
  2913. }
  2914. EXPORT_SYMBOL(drm_dp_pcon_reset_frl_config);
  2915. /**
  2916. * drm_dp_pcon_frl_enable() - Enable HDMI link through FRL
  2917. * @aux: DisplayPort AUX channel
  2918. *
  2919. * Returns 0 if success, else returns negative error code.
  2920. */
  2921. int drm_dp_pcon_frl_enable(struct drm_dp_aux *aux)
  2922. {
  2923. int ret;
  2924. u8 buf = 0;
  2925. ret = drm_dp_dpcd_readb(aux, DP_PCON_HDMI_LINK_CONFIG_1, &buf);
  2926. if (ret < 0)
  2927. return ret;
  2928. if (!(buf & DP_PCON_ENABLE_SOURCE_CTL_MODE)) {
  2929. drm_dbg_kms(aux->drm_dev, "%s: PCON in Autonomous mode, can't enable FRL\n",
  2930. aux->name);
  2931. return -EINVAL;
  2932. }
  2933. buf |= DP_PCON_ENABLE_HDMI_LINK;
  2934. ret = drm_dp_dpcd_writeb(aux, DP_PCON_HDMI_LINK_CONFIG_1, buf);
  2935. if (ret < 0)
  2936. return ret;
  2937. return 0;
  2938. }
  2939. EXPORT_SYMBOL(drm_dp_pcon_frl_enable);
  2940. /**
  2941. * drm_dp_pcon_hdmi_link_active() - check if the PCON HDMI LINK status is active.
  2942. * @aux: DisplayPort AUX channel
  2943. *
  2944. * Returns true if link is active else returns false.
  2945. */
  2946. bool drm_dp_pcon_hdmi_link_active(struct drm_dp_aux *aux)
  2947. {
  2948. u8 buf;
  2949. int ret;
  2950. ret = drm_dp_dpcd_readb(aux, DP_PCON_HDMI_TX_LINK_STATUS, &buf);
  2951. if (ret < 0)
  2952. return false;
  2953. return buf & DP_PCON_HDMI_TX_LINK_ACTIVE;
  2954. }
  2955. EXPORT_SYMBOL(drm_dp_pcon_hdmi_link_active);
  2956. /**
  2957. * drm_dp_pcon_hdmi_link_mode() - get the PCON HDMI LINK MODE
  2958. * @aux: DisplayPort AUX channel
  2959. * @frl_trained_mask: pointer to store bitmask of the trained bw configuration.
  2960. * Valid only if the MODE returned is FRL. For Normal Link training mode
  2961. * only 1 of the bits will be set, but in case of Extended mode, more than
  2962. * one bits can be set.
  2963. *
  2964. * Returns the link mode : TMDS or FRL on success, else returns negative error
  2965. * code.
  2966. */
  2967. int drm_dp_pcon_hdmi_link_mode(struct drm_dp_aux *aux, u8 *frl_trained_mask)
  2968. {
  2969. u8 buf;
  2970. int mode;
  2971. int ret;
  2972. ret = drm_dp_dpcd_readb(aux, DP_PCON_HDMI_POST_FRL_STATUS, &buf);
  2973. if (ret < 0)
  2974. return ret;
  2975. mode = buf & DP_PCON_HDMI_LINK_MODE;
  2976. if (frl_trained_mask && DP_PCON_HDMI_MODE_FRL == mode)
  2977. *frl_trained_mask = (buf & DP_PCON_HDMI_FRL_TRAINED_BW) >> 1;
  2978. return mode;
  2979. }
  2980. EXPORT_SYMBOL(drm_dp_pcon_hdmi_link_mode);
  2981. /**
  2982. * drm_dp_pcon_hdmi_frl_link_error_count() - print the error count per lane
  2983. * during link failure between PCON and HDMI sink
  2984. * @aux: DisplayPort AUX channel
  2985. * @connector: DRM connector
  2986. * code.
  2987. **/
  2988. void drm_dp_pcon_hdmi_frl_link_error_count(struct drm_dp_aux *aux,
  2989. struct drm_connector *connector)
  2990. {
  2991. u8 buf, error_count;
  2992. int i, num_error;
  2993. struct drm_hdmi_info *hdmi = &connector->display_info.hdmi;
  2994. for (i = 0; i < hdmi->max_lanes; i++) {
  2995. if (drm_dp_dpcd_readb(aux, DP_PCON_HDMI_ERROR_STATUS_LN0 + i, &buf) < 0)
  2996. return;
  2997. error_count = buf & DP_PCON_HDMI_ERROR_COUNT_MASK;
  2998. switch (error_count) {
  2999. case DP_PCON_HDMI_ERROR_COUNT_HUNDRED_PLUS:
  3000. num_error = 100;
  3001. break;
  3002. case DP_PCON_HDMI_ERROR_COUNT_TEN_PLUS:
  3003. num_error = 10;
  3004. break;
  3005. case DP_PCON_HDMI_ERROR_COUNT_THREE_PLUS:
  3006. num_error = 3;
  3007. break;
  3008. default:
  3009. num_error = 0;
  3010. }
  3011. drm_err(aux->drm_dev, "%s: More than %d errors since the last read for lane %d",
  3012. aux->name, num_error, i);
  3013. }
  3014. }
  3015. EXPORT_SYMBOL(drm_dp_pcon_hdmi_frl_link_error_count);
  3016. /*
  3017. * drm_dp_pcon_enc_is_dsc_1_2 - Does PCON Encoder supports DSC 1.2
  3018. * @pcon_dsc_dpcd: DSC capabilities of the PCON DSC Encoder
  3019. *
  3020. * Returns true is PCON encoder is DSC 1.2 else returns false.
  3021. */
  3022. bool drm_dp_pcon_enc_is_dsc_1_2(const u8 pcon_dsc_dpcd[DP_PCON_DSC_ENCODER_CAP_SIZE])
  3023. {
  3024. u8 buf;
  3025. u8 major_v, minor_v;
  3026. buf = pcon_dsc_dpcd[DP_PCON_DSC_VERSION - DP_PCON_DSC_ENCODER];
  3027. major_v = (buf & DP_PCON_DSC_MAJOR_MASK) >> DP_PCON_DSC_MAJOR_SHIFT;
  3028. minor_v = (buf & DP_PCON_DSC_MINOR_MASK) >> DP_PCON_DSC_MINOR_SHIFT;
  3029. if (major_v == 1 && minor_v == 2)
  3030. return true;
  3031. return false;
  3032. }
  3033. EXPORT_SYMBOL(drm_dp_pcon_enc_is_dsc_1_2);
  3034. /*
  3035. * drm_dp_pcon_dsc_max_slices - Get max slices supported by PCON DSC Encoder
  3036. * @pcon_dsc_dpcd: DSC capabilities of the PCON DSC Encoder
  3037. *
  3038. * Returns maximum no. of slices supported by the PCON DSC Encoder.
  3039. */
  3040. int drm_dp_pcon_dsc_max_slices(const u8 pcon_dsc_dpcd[DP_PCON_DSC_ENCODER_CAP_SIZE])
  3041. {
  3042. u8 slice_cap1, slice_cap2;
  3043. slice_cap1 = pcon_dsc_dpcd[DP_PCON_DSC_SLICE_CAP_1 - DP_PCON_DSC_ENCODER];
  3044. slice_cap2 = pcon_dsc_dpcd[DP_PCON_DSC_SLICE_CAP_2 - DP_PCON_DSC_ENCODER];
  3045. if (slice_cap2 & DP_PCON_DSC_24_PER_DSC_ENC)
  3046. return 24;
  3047. if (slice_cap2 & DP_PCON_DSC_20_PER_DSC_ENC)
  3048. return 20;
  3049. if (slice_cap2 & DP_PCON_DSC_16_PER_DSC_ENC)
  3050. return 16;
  3051. if (slice_cap1 & DP_PCON_DSC_12_PER_DSC_ENC)
  3052. return 12;
  3053. if (slice_cap1 & DP_PCON_DSC_10_PER_DSC_ENC)
  3054. return 10;
  3055. if (slice_cap1 & DP_PCON_DSC_8_PER_DSC_ENC)
  3056. return 8;
  3057. if (slice_cap1 & DP_PCON_DSC_6_PER_DSC_ENC)
  3058. return 6;
  3059. if (slice_cap1 & DP_PCON_DSC_4_PER_DSC_ENC)
  3060. return 4;
  3061. if (slice_cap1 & DP_PCON_DSC_2_PER_DSC_ENC)
  3062. return 2;
  3063. if (slice_cap1 & DP_PCON_DSC_1_PER_DSC_ENC)
  3064. return 1;
  3065. return 0;
  3066. }
  3067. EXPORT_SYMBOL(drm_dp_pcon_dsc_max_slices);
  3068. /*
  3069. * drm_dp_pcon_dsc_max_slice_width() - Get max slice width for Pcon DSC encoder
  3070. * @pcon_dsc_dpcd: DSC capabilities of the PCON DSC Encoder
  3071. *
  3072. * Returns maximum width of the slices in pixel width i.e. no. of pixels x 320.
  3073. */
  3074. int drm_dp_pcon_dsc_max_slice_width(const u8 pcon_dsc_dpcd[DP_PCON_DSC_ENCODER_CAP_SIZE])
  3075. {
  3076. u8 buf;
  3077. buf = pcon_dsc_dpcd[DP_PCON_DSC_MAX_SLICE_WIDTH - DP_PCON_DSC_ENCODER];
  3078. return buf * DP_DSC_SLICE_WIDTH_MULTIPLIER;
  3079. }
  3080. EXPORT_SYMBOL(drm_dp_pcon_dsc_max_slice_width);
  3081. /*
  3082. * drm_dp_pcon_dsc_bpp_incr() - Get bits per pixel increment for PCON DSC encoder
  3083. * @pcon_dsc_dpcd: DSC capabilities of the PCON DSC Encoder
  3084. *
  3085. * Returns the bpp precision supported by the PCON encoder.
  3086. */
  3087. int drm_dp_pcon_dsc_bpp_incr(const u8 pcon_dsc_dpcd[DP_PCON_DSC_ENCODER_CAP_SIZE])
  3088. {
  3089. u8 buf;
  3090. buf = pcon_dsc_dpcd[DP_PCON_DSC_BPP_INCR - DP_PCON_DSC_ENCODER];
  3091. switch (buf & DP_PCON_DSC_BPP_INCR_MASK) {
  3092. case DP_PCON_DSC_ONE_16TH_BPP:
  3093. return 16;
  3094. case DP_PCON_DSC_ONE_8TH_BPP:
  3095. return 8;
  3096. case DP_PCON_DSC_ONE_4TH_BPP:
  3097. return 4;
  3098. case DP_PCON_DSC_ONE_HALF_BPP:
  3099. return 2;
  3100. case DP_PCON_DSC_ONE_BPP:
  3101. return 1;
  3102. }
  3103. return 0;
  3104. }
  3105. EXPORT_SYMBOL(drm_dp_pcon_dsc_bpp_incr);
  3106. static
  3107. int drm_dp_pcon_configure_dsc_enc(struct drm_dp_aux *aux, u8 pps_buf_config)
  3108. {
  3109. u8 buf;
  3110. int ret;
  3111. ret = drm_dp_dpcd_readb(aux, DP_PROTOCOL_CONVERTER_CONTROL_2, &buf);
  3112. if (ret < 0)
  3113. return ret;
  3114. buf |= DP_PCON_ENABLE_DSC_ENCODER;
  3115. if (pps_buf_config <= DP_PCON_ENC_PPS_OVERRIDE_EN_BUFFER) {
  3116. buf &= ~DP_PCON_ENCODER_PPS_OVERRIDE_MASK;
  3117. buf |= pps_buf_config << 2;
  3118. }
  3119. ret = drm_dp_dpcd_writeb(aux, DP_PROTOCOL_CONVERTER_CONTROL_2, buf);
  3120. if (ret < 0)
  3121. return ret;
  3122. return 0;
  3123. }
  3124. /**
  3125. * drm_dp_pcon_pps_default() - Let PCON fill the default pps parameters
  3126. * for DSC1.2 between PCON & HDMI2.1 sink
  3127. * @aux: DisplayPort AUX channel
  3128. *
  3129. * Returns 0 on success, else returns negative error code.
  3130. */
  3131. int drm_dp_pcon_pps_default(struct drm_dp_aux *aux)
  3132. {
  3133. int ret;
  3134. ret = drm_dp_pcon_configure_dsc_enc(aux, DP_PCON_ENC_PPS_OVERRIDE_DISABLED);
  3135. if (ret < 0)
  3136. return ret;
  3137. return 0;
  3138. }
  3139. EXPORT_SYMBOL(drm_dp_pcon_pps_default);
  3140. /**
  3141. * drm_dp_pcon_pps_override_buf() - Configure PPS encoder override buffer for
  3142. * HDMI sink
  3143. * @aux: DisplayPort AUX channel
  3144. * @pps_buf: 128 bytes to be written into PPS buffer for HDMI sink by PCON.
  3145. *
  3146. * Returns 0 on success, else returns negative error code.
  3147. */
  3148. int drm_dp_pcon_pps_override_buf(struct drm_dp_aux *aux, u8 pps_buf[128])
  3149. {
  3150. int ret;
  3151. ret = drm_dp_dpcd_write(aux, DP_PCON_HDMI_PPS_OVERRIDE_BASE, &pps_buf, 128);
  3152. if (ret < 0)
  3153. return ret;
  3154. ret = drm_dp_pcon_configure_dsc_enc(aux, DP_PCON_ENC_PPS_OVERRIDE_EN_BUFFER);
  3155. if (ret < 0)
  3156. return ret;
  3157. return 0;
  3158. }
  3159. EXPORT_SYMBOL(drm_dp_pcon_pps_override_buf);
  3160. /*
  3161. * drm_dp_pcon_pps_override_param() - Write PPS parameters to DSC encoder
  3162. * override registers
  3163. * @aux: DisplayPort AUX channel
  3164. * @pps_param: 3 Parameters (2 Bytes each) : Slice Width, Slice Height,
  3165. * bits_per_pixel.
  3166. *
  3167. * Returns 0 on success, else returns negative error code.
  3168. */
  3169. int drm_dp_pcon_pps_override_param(struct drm_dp_aux *aux, u8 pps_param[6])
  3170. {
  3171. int ret;
  3172. ret = drm_dp_dpcd_write(aux, DP_PCON_HDMI_PPS_OVRD_SLICE_HEIGHT, &pps_param[0], 2);
  3173. if (ret < 0)
  3174. return ret;
  3175. ret = drm_dp_dpcd_write(aux, DP_PCON_HDMI_PPS_OVRD_SLICE_WIDTH, &pps_param[2], 2);
  3176. if (ret < 0)
  3177. return ret;
  3178. ret = drm_dp_dpcd_write(aux, DP_PCON_HDMI_PPS_OVRD_BPP, &pps_param[4], 2);
  3179. if (ret < 0)
  3180. return ret;
  3181. ret = drm_dp_pcon_configure_dsc_enc(aux, DP_PCON_ENC_PPS_OVERRIDE_EN_BUFFER);
  3182. if (ret < 0)
  3183. return ret;
  3184. return 0;
  3185. }
  3186. EXPORT_SYMBOL(drm_dp_pcon_pps_override_param);
  3187. /*
  3188. * drm_dp_pcon_convert_rgb_to_ycbcr() - Configure the PCon to convert RGB to Ycbcr
  3189. * @aux: displayPort AUX channel
  3190. * @color_spc: Color-space/s for which conversion is to be enabled, 0 for disable.
  3191. *
  3192. * Returns 0 on success, else returns negative error code.
  3193. */
  3194. int drm_dp_pcon_convert_rgb_to_ycbcr(struct drm_dp_aux *aux, u8 color_spc)
  3195. {
  3196. int ret;
  3197. u8 buf;
  3198. ret = drm_dp_dpcd_readb(aux, DP_PROTOCOL_CONVERTER_CONTROL_2, &buf);
  3199. if (ret < 0)
  3200. return ret;
  3201. if (color_spc & DP_CONVERSION_RGB_YCBCR_MASK)
  3202. buf |= (color_spc & DP_CONVERSION_RGB_YCBCR_MASK);
  3203. else
  3204. buf &= ~DP_CONVERSION_RGB_YCBCR_MASK;
  3205. ret = drm_dp_dpcd_writeb(aux, DP_PROTOCOL_CONVERTER_CONTROL_2, buf);
  3206. if (ret < 0)
  3207. return ret;
  3208. return 0;
  3209. }
  3210. EXPORT_SYMBOL(drm_dp_pcon_convert_rgb_to_ycbcr);
  3211. /**
  3212. * drm_edp_backlight_set_level() - Set the backlight level of an eDP panel via AUX
  3213. * @aux: The DP AUX channel to use
  3214. * @bl: Backlight capability info from drm_edp_backlight_init()
  3215. * @level: The brightness level to set
  3216. *
  3217. * Sets the brightness level of an eDP panel's backlight. Note that the panel's backlight must
  3218. * already have been enabled by the driver by calling drm_edp_backlight_enable().
  3219. *
  3220. * Returns: %0 on success, negative error code on failure
  3221. */
  3222. int drm_edp_backlight_set_level(struct drm_dp_aux *aux, const struct drm_edp_backlight_info *bl,
  3223. u16 level)
  3224. {
  3225. int ret;
  3226. u8 buf[2] = { 0 };
  3227. /* The panel uses the PWM for controlling brightness levels */
  3228. if (!bl->aux_set)
  3229. return 0;
  3230. if (bl->lsb_reg_used) {
  3231. buf[0] = (level & 0xff00) >> 8;
  3232. buf[1] = (level & 0x00ff);
  3233. } else {
  3234. buf[0] = level;
  3235. }
  3236. ret = drm_dp_dpcd_write(aux, DP_EDP_BACKLIGHT_BRIGHTNESS_MSB, buf, sizeof(buf));
  3237. if (ret != sizeof(buf)) {
  3238. drm_err(aux->drm_dev,
  3239. "%s: Failed to write aux backlight level: %d\n",
  3240. aux->name, ret);
  3241. return ret < 0 ? ret : -EIO;
  3242. }
  3243. return 0;
  3244. }
  3245. EXPORT_SYMBOL(drm_edp_backlight_set_level);
  3246. static int
  3247. drm_edp_backlight_set_enable(struct drm_dp_aux *aux, const struct drm_edp_backlight_info *bl,
  3248. bool enable)
  3249. {
  3250. int ret;
  3251. u8 buf;
  3252. /* This panel uses the EDP_BL_PWR GPIO for enablement */
  3253. if (!bl->aux_enable)
  3254. return 0;
  3255. ret = drm_dp_dpcd_readb(aux, DP_EDP_DISPLAY_CONTROL_REGISTER, &buf);
  3256. if (ret != 1) {
  3257. drm_err(aux->drm_dev, "%s: Failed to read eDP display control register: %d\n",
  3258. aux->name, ret);
  3259. return ret < 0 ? ret : -EIO;
  3260. }
  3261. if (enable)
  3262. buf |= DP_EDP_BACKLIGHT_ENABLE;
  3263. else
  3264. buf &= ~DP_EDP_BACKLIGHT_ENABLE;
  3265. ret = drm_dp_dpcd_writeb(aux, DP_EDP_DISPLAY_CONTROL_REGISTER, buf);
  3266. if (ret != 1) {
  3267. drm_err(aux->drm_dev, "%s: Failed to write eDP display control register: %d\n",
  3268. aux->name, ret);
  3269. return ret < 0 ? ret : -EIO;
  3270. }
  3271. return 0;
  3272. }
  3273. /**
  3274. * drm_edp_backlight_enable() - Enable an eDP panel's backlight using DPCD
  3275. * @aux: The DP AUX channel to use
  3276. * @bl: Backlight capability info from drm_edp_backlight_init()
  3277. * @level: The initial backlight level to set via AUX, if there is one
  3278. *
  3279. * This function handles enabling DPCD backlight controls on a panel over DPCD, while additionally
  3280. * restoring any important backlight state such as the given backlight level, the brightness byte
  3281. * count, backlight frequency, etc.
  3282. *
  3283. * Note that certain panels do not support being enabled or disabled via DPCD, but instead require
  3284. * that the driver handle enabling/disabling the panel through implementation-specific means using
  3285. * the EDP_BL_PWR GPIO. For such panels, &drm_edp_backlight_info.aux_enable will be set to %false,
  3286. * this function becomes a no-op, and the driver is expected to handle powering the panel on using
  3287. * the EDP_BL_PWR GPIO.
  3288. *
  3289. * Returns: %0 on success, negative error code on failure.
  3290. */
  3291. int drm_edp_backlight_enable(struct drm_dp_aux *aux, const struct drm_edp_backlight_info *bl,
  3292. const u16 level)
  3293. {
  3294. int ret;
  3295. u8 dpcd_buf;
  3296. if (bl->aux_set)
  3297. dpcd_buf = DP_EDP_BACKLIGHT_CONTROL_MODE_DPCD;
  3298. else
  3299. dpcd_buf = DP_EDP_BACKLIGHT_CONTROL_MODE_PWM;
  3300. if (bl->pwmgen_bit_count) {
  3301. ret = drm_dp_dpcd_writeb(aux, DP_EDP_PWMGEN_BIT_COUNT, bl->pwmgen_bit_count);
  3302. if (ret != 1)
  3303. drm_dbg_kms(aux->drm_dev, "%s: Failed to write aux pwmgen bit count: %d\n",
  3304. aux->name, ret);
  3305. }
  3306. if (bl->pwm_freq_pre_divider) {
  3307. ret = drm_dp_dpcd_writeb(aux, DP_EDP_BACKLIGHT_FREQ_SET, bl->pwm_freq_pre_divider);
  3308. if (ret != 1)
  3309. drm_dbg_kms(aux->drm_dev,
  3310. "%s: Failed to write aux backlight frequency: %d\n",
  3311. aux->name, ret);
  3312. else
  3313. dpcd_buf |= DP_EDP_BACKLIGHT_FREQ_AUX_SET_ENABLE;
  3314. }
  3315. ret = drm_dp_dpcd_writeb(aux, DP_EDP_BACKLIGHT_MODE_SET_REGISTER, dpcd_buf);
  3316. if (ret != 1) {
  3317. drm_dbg_kms(aux->drm_dev, "%s: Failed to write aux backlight mode: %d\n",
  3318. aux->name, ret);
  3319. return ret < 0 ? ret : -EIO;
  3320. }
  3321. ret = drm_edp_backlight_set_level(aux, bl, level);
  3322. if (ret < 0)
  3323. return ret;
  3324. ret = drm_edp_backlight_set_enable(aux, bl, true);
  3325. if (ret < 0)
  3326. return ret;
  3327. return 0;
  3328. }
  3329. EXPORT_SYMBOL(drm_edp_backlight_enable);
  3330. /**
  3331. * drm_edp_backlight_disable() - Disable an eDP backlight using DPCD, if supported
  3332. * @aux: The DP AUX channel to use
  3333. * @bl: Backlight capability info from drm_edp_backlight_init()
  3334. *
  3335. * This function handles disabling DPCD backlight controls on a panel over AUX.
  3336. *
  3337. * Note that certain panels do not support being enabled or disabled via DPCD, but instead require
  3338. * that the driver handle enabling/disabling the panel through implementation-specific means using
  3339. * the EDP_BL_PWR GPIO. For such panels, &drm_edp_backlight_info.aux_enable will be set to %false,
  3340. * this function becomes a no-op, and the driver is expected to handle powering the panel off using
  3341. * the EDP_BL_PWR GPIO.
  3342. *
  3343. * Returns: %0 on success or no-op, negative error code on failure.
  3344. */
  3345. int drm_edp_backlight_disable(struct drm_dp_aux *aux, const struct drm_edp_backlight_info *bl)
  3346. {
  3347. int ret;
  3348. ret = drm_edp_backlight_set_enable(aux, bl, false);
  3349. if (ret < 0)
  3350. return ret;
  3351. return 0;
  3352. }
  3353. EXPORT_SYMBOL(drm_edp_backlight_disable);
  3354. static inline int
  3355. drm_edp_backlight_probe_max(struct drm_dp_aux *aux, struct drm_edp_backlight_info *bl,
  3356. u16 driver_pwm_freq_hz, const u8 edp_dpcd[EDP_DISPLAY_CTL_CAP_SIZE])
  3357. {
  3358. int fxp, fxp_min, fxp_max, fxp_actual, f = 1;
  3359. int ret;
  3360. u8 pn, pn_min, pn_max;
  3361. if (!bl->aux_set)
  3362. return 0;
  3363. ret = drm_dp_dpcd_readb(aux, DP_EDP_PWMGEN_BIT_COUNT, &pn);
  3364. if (ret != 1) {
  3365. drm_dbg_kms(aux->drm_dev, "%s: Failed to read pwmgen bit count cap: %d\n",
  3366. aux->name, ret);
  3367. return -ENODEV;
  3368. }
  3369. pn &= DP_EDP_PWMGEN_BIT_COUNT_MASK;
  3370. bl->max = (1 << pn) - 1;
  3371. if (!driver_pwm_freq_hz)
  3372. return 0;
  3373. /*
  3374. * Set PWM Frequency divider to match desired frequency provided by the driver.
  3375. * The PWM Frequency is calculated as 27Mhz / (F x P).
  3376. * - Where F = PWM Frequency Pre-Divider value programmed by field 7:0 of the
  3377. * EDP_BACKLIGHT_FREQ_SET register (DPCD Address 00728h)
  3378. * - Where P = 2^Pn, where Pn is the value programmed by field 4:0 of the
  3379. * EDP_PWMGEN_BIT_COUNT register (DPCD Address 00724h)
  3380. */
  3381. /* Find desired value of (F x P)
  3382. * Note that, if F x P is out of supported range, the maximum value or minimum value will
  3383. * applied automatically. So no need to check that.
  3384. */
  3385. fxp = DIV_ROUND_CLOSEST(1000 * DP_EDP_BACKLIGHT_FREQ_BASE_KHZ, driver_pwm_freq_hz);
  3386. /* Use highest possible value of Pn for more granularity of brightness adjustment while
  3387. * satisfying the conditions below.
  3388. * - Pn is in the range of Pn_min and Pn_max
  3389. * - F is in the range of 1 and 255
  3390. * - FxP is within 25% of desired value.
  3391. * Note: 25% is arbitrary value and may need some tweak.
  3392. */
  3393. ret = drm_dp_dpcd_readb(aux, DP_EDP_PWMGEN_BIT_COUNT_CAP_MIN, &pn_min);
  3394. if (ret != 1) {
  3395. drm_dbg_kms(aux->drm_dev, "%s: Failed to read pwmgen bit count cap min: %d\n",
  3396. aux->name, ret);
  3397. return 0;
  3398. }
  3399. ret = drm_dp_dpcd_readb(aux, DP_EDP_PWMGEN_BIT_COUNT_CAP_MAX, &pn_max);
  3400. if (ret != 1) {
  3401. drm_dbg_kms(aux->drm_dev, "%s: Failed to read pwmgen bit count cap max: %d\n",
  3402. aux->name, ret);
  3403. return 0;
  3404. }
  3405. pn_min &= DP_EDP_PWMGEN_BIT_COUNT_MASK;
  3406. pn_max &= DP_EDP_PWMGEN_BIT_COUNT_MASK;
  3407. /* Ensure frequency is within 25% of desired value */
  3408. fxp_min = DIV_ROUND_CLOSEST(fxp * 3, 4);
  3409. fxp_max = DIV_ROUND_CLOSEST(fxp * 5, 4);
  3410. if (fxp_min < (1 << pn_min) || (255 << pn_max) < fxp_max) {
  3411. drm_dbg_kms(aux->drm_dev,
  3412. "%s: Driver defined backlight frequency (%d) out of range\n",
  3413. aux->name, driver_pwm_freq_hz);
  3414. return 0;
  3415. }
  3416. for (pn = pn_max; pn >= pn_min; pn--) {
  3417. f = clamp(DIV_ROUND_CLOSEST(fxp, 1 << pn), 1, 255);
  3418. fxp_actual = f << pn;
  3419. if (fxp_min <= fxp_actual && fxp_actual <= fxp_max)
  3420. break;
  3421. }
  3422. ret = drm_dp_dpcd_writeb(aux, DP_EDP_PWMGEN_BIT_COUNT, pn);
  3423. if (ret != 1) {
  3424. drm_dbg_kms(aux->drm_dev, "%s: Failed to write aux pwmgen bit count: %d\n",
  3425. aux->name, ret);
  3426. return 0;
  3427. }
  3428. bl->pwmgen_bit_count = pn;
  3429. bl->max = (1 << pn) - 1;
  3430. if (edp_dpcd[2] & DP_EDP_BACKLIGHT_FREQ_AUX_SET_CAP) {
  3431. bl->pwm_freq_pre_divider = f;
  3432. drm_dbg_kms(aux->drm_dev, "%s: Using backlight frequency from driver (%dHz)\n",
  3433. aux->name, driver_pwm_freq_hz);
  3434. }
  3435. return 0;
  3436. }
  3437. static inline int
  3438. drm_edp_backlight_probe_state(struct drm_dp_aux *aux, struct drm_edp_backlight_info *bl,
  3439. u8 *current_mode)
  3440. {
  3441. int ret;
  3442. u8 buf[2];
  3443. u8 mode_reg;
  3444. ret = drm_dp_dpcd_readb(aux, DP_EDP_BACKLIGHT_MODE_SET_REGISTER, &mode_reg);
  3445. if (ret != 1) {
  3446. drm_dbg_kms(aux->drm_dev, "%s: Failed to read backlight mode: %d\n",
  3447. aux->name, ret);
  3448. return ret < 0 ? ret : -EIO;
  3449. }
  3450. *current_mode = (mode_reg & DP_EDP_BACKLIGHT_CONTROL_MODE_MASK);
  3451. if (!bl->aux_set)
  3452. return 0;
  3453. if (*current_mode == DP_EDP_BACKLIGHT_CONTROL_MODE_DPCD) {
  3454. int size = 1 + bl->lsb_reg_used;
  3455. ret = drm_dp_dpcd_read(aux, DP_EDP_BACKLIGHT_BRIGHTNESS_MSB, buf, size);
  3456. if (ret != size) {
  3457. drm_dbg_kms(aux->drm_dev, "%s: Failed to read backlight level: %d\n",
  3458. aux->name, ret);
  3459. return ret < 0 ? ret : -EIO;
  3460. }
  3461. if (bl->lsb_reg_used)
  3462. return (buf[0] << 8) | buf[1];
  3463. else
  3464. return buf[0];
  3465. }
  3466. /*
  3467. * If we're not in DPCD control mode yet, the programmed brightness value is meaningless and
  3468. * the driver should assume max brightness
  3469. */
  3470. return bl->max;
  3471. }
  3472. /**
  3473. * drm_edp_backlight_init() - Probe a display panel's TCON using the standard VESA eDP backlight
  3474. * interface.
  3475. * @aux: The DP aux device to use for probing
  3476. * @bl: The &drm_edp_backlight_info struct to fill out with information on the backlight
  3477. * @driver_pwm_freq_hz: Optional PWM frequency from the driver in hz
  3478. * @edp_dpcd: A cached copy of the eDP DPCD
  3479. * @current_level: Where to store the probed brightness level, if any
  3480. * @current_mode: Where to store the currently set backlight control mode
  3481. *
  3482. * Initializes a &drm_edp_backlight_info struct by probing @aux for it's backlight capabilities,
  3483. * along with also probing the current and maximum supported brightness levels.
  3484. *
  3485. * If @driver_pwm_freq_hz is non-zero, this will be used as the backlight frequency. Otherwise, the
  3486. * default frequency from the panel is used.
  3487. *
  3488. * Returns: %0 on success, negative error code on failure.
  3489. */
  3490. int
  3491. drm_edp_backlight_init(struct drm_dp_aux *aux, struct drm_edp_backlight_info *bl,
  3492. u16 driver_pwm_freq_hz, const u8 edp_dpcd[EDP_DISPLAY_CTL_CAP_SIZE],
  3493. u16 *current_level, u8 *current_mode)
  3494. {
  3495. int ret;
  3496. if (edp_dpcd[1] & DP_EDP_BACKLIGHT_AUX_ENABLE_CAP)
  3497. bl->aux_enable = true;
  3498. if (edp_dpcd[2] & DP_EDP_BACKLIGHT_BRIGHTNESS_AUX_SET_CAP)
  3499. bl->aux_set = true;
  3500. if (edp_dpcd[2] & DP_EDP_BACKLIGHT_BRIGHTNESS_BYTE_COUNT)
  3501. bl->lsb_reg_used = true;
  3502. /* Sanity check caps */
  3503. if (!bl->aux_set && !(edp_dpcd[2] & DP_EDP_BACKLIGHT_BRIGHTNESS_PWM_PIN_CAP)) {
  3504. drm_dbg_kms(aux->drm_dev,
  3505. "%s: Panel supports neither AUX or PWM brightness control? Aborting\n",
  3506. aux->name);
  3507. return -EINVAL;
  3508. }
  3509. ret = drm_edp_backlight_probe_max(aux, bl, driver_pwm_freq_hz, edp_dpcd);
  3510. if (ret < 0)
  3511. return ret;
  3512. ret = drm_edp_backlight_probe_state(aux, bl, current_mode);
  3513. if (ret < 0)
  3514. return ret;
  3515. *current_level = ret;
  3516. drm_dbg_kms(aux->drm_dev,
  3517. "%s: Found backlight: aux_set=%d aux_enable=%d mode=%d\n",
  3518. aux->name, bl->aux_set, bl->aux_enable, *current_mode);
  3519. if (bl->aux_set) {
  3520. drm_dbg_kms(aux->drm_dev,
  3521. "%s: Backlight caps: level=%d/%d pwm_freq_pre_divider=%d lsb_reg_used=%d\n",
  3522. aux->name, *current_level, bl->max, bl->pwm_freq_pre_divider,
  3523. bl->lsb_reg_used);
  3524. }
  3525. return 0;
  3526. }
  3527. EXPORT_SYMBOL(drm_edp_backlight_init);
  3528. #if IS_BUILTIN(CONFIG_BACKLIGHT_CLASS_DEVICE) || \
  3529. (IS_MODULE(CONFIG_DRM_KMS_HELPER) && IS_MODULE(CONFIG_BACKLIGHT_CLASS_DEVICE))
  3530. static int dp_aux_backlight_update_status(struct backlight_device *bd)
  3531. {
  3532. struct dp_aux_backlight *bl = bl_get_data(bd);
  3533. u16 brightness = backlight_get_brightness(bd);
  3534. int ret = 0;
  3535. if (!backlight_is_blank(bd)) {
  3536. if (!bl->enabled) {
  3537. drm_edp_backlight_enable(bl->aux, &bl->info, brightness);
  3538. bl->enabled = true;
  3539. return 0;
  3540. }
  3541. ret = drm_edp_backlight_set_level(bl->aux, &bl->info, brightness);
  3542. } else {
  3543. if (bl->enabled) {
  3544. drm_edp_backlight_disable(bl->aux, &bl->info);
  3545. bl->enabled = false;
  3546. }
  3547. }
  3548. return ret;
  3549. }
  3550. static const struct backlight_ops dp_aux_bl_ops = {
  3551. .update_status = dp_aux_backlight_update_status,
  3552. };
  3553. /**
  3554. * drm_panel_dp_aux_backlight - create and use DP AUX backlight
  3555. * @panel: DRM panel
  3556. * @aux: The DP AUX channel to use
  3557. *
  3558. * Use this function to create and handle backlight if your panel
  3559. * supports backlight control over DP AUX channel using DPCD
  3560. * registers as per VESA's standard backlight control interface.
  3561. *
  3562. * When the panel is enabled backlight will be enabled after a
  3563. * successful call to &drm_panel_funcs.enable()
  3564. *
  3565. * When the panel is disabled backlight will be disabled before the
  3566. * call to &drm_panel_funcs.disable().
  3567. *
  3568. * A typical implementation for a panel driver supporting backlight
  3569. * control over DP AUX will call this function at probe time.
  3570. * Backlight will then be handled transparently without requiring
  3571. * any intervention from the driver.
  3572. *
  3573. * drm_panel_dp_aux_backlight() must be called after the call to drm_panel_init().
  3574. *
  3575. * Return: 0 on success or a negative error code on failure.
  3576. */
  3577. int drm_panel_dp_aux_backlight(struct drm_panel *panel, struct drm_dp_aux *aux)
  3578. {
  3579. struct dp_aux_backlight *bl;
  3580. struct backlight_properties props = { 0 };
  3581. u16 current_level;
  3582. u8 current_mode;
  3583. u8 edp_dpcd[EDP_DISPLAY_CTL_CAP_SIZE];
  3584. int ret;
  3585. if (!panel || !panel->dev || !aux)
  3586. return -EINVAL;
  3587. ret = drm_dp_dpcd_read(aux, DP_EDP_DPCD_REV, edp_dpcd,
  3588. EDP_DISPLAY_CTL_CAP_SIZE);
  3589. if (ret < 0)
  3590. return ret;
  3591. if (!drm_edp_backlight_supported(edp_dpcd)) {
  3592. DRM_DEV_INFO(panel->dev, "DP AUX backlight is not supported\n");
  3593. return 0;
  3594. }
  3595. bl = devm_kzalloc(panel->dev, sizeof(*bl), GFP_KERNEL);
  3596. if (!bl)
  3597. return -ENOMEM;
  3598. bl->aux = aux;
  3599. ret = drm_edp_backlight_init(aux, &bl->info, 0, edp_dpcd,
  3600. &current_level, &current_mode);
  3601. if (ret < 0)
  3602. return ret;
  3603. props.type = BACKLIGHT_RAW;
  3604. props.brightness = current_level;
  3605. props.max_brightness = bl->info.max;
  3606. bl->base = devm_backlight_device_register(panel->dev, "dp_aux_backlight",
  3607. panel->dev, bl,
  3608. &dp_aux_bl_ops, &props);
  3609. if (IS_ERR(bl->base))
  3610. return PTR_ERR(bl->base);
  3611. backlight_disable(bl->base);
  3612. panel->backlight = bl->base;
  3613. return 0;
  3614. }
  3615. EXPORT_SYMBOL(drm_panel_dp_aux_backlight);
  3616. #endif
  3617. /* See DP Standard v2.1 2.6.4.4.1.1, 2.8.4.4, 2.8.7 */
  3618. static int drm_dp_link_symbol_cycles(int lane_count, int pixels, int bpp_x16,
  3619. int symbol_size, bool is_mst)
  3620. {
  3621. int cycles = DIV_ROUND_UP(pixels * bpp_x16, 16 * symbol_size * lane_count);
  3622. int align = is_mst ? 4 / lane_count : 1;
  3623. return ALIGN(cycles, align);
  3624. }
  3625. static int drm_dp_link_dsc_symbol_cycles(int lane_count, int pixels, int slice_count,
  3626. int bpp_x16, int symbol_size, bool is_mst)
  3627. {
  3628. int slice_pixels = DIV_ROUND_UP(pixels, slice_count);
  3629. int slice_data_cycles = drm_dp_link_symbol_cycles(lane_count, slice_pixels,
  3630. bpp_x16, symbol_size, is_mst);
  3631. int slice_eoc_cycles = is_mst ? 4 / lane_count : 1;
  3632. return slice_count * (slice_data_cycles + slice_eoc_cycles);
  3633. }
  3634. /**
  3635. * drm_dp_bw_overhead - Calculate the BW overhead of a DP link stream
  3636. * @lane_count: DP link lane count
  3637. * @hactive: pixel count of the active period in one scanline of the stream
  3638. * @dsc_slice_count: DSC slice count if @flags/DRM_DP_LINK_BW_OVERHEAD_DSC is set
  3639. * @bpp_x16: bits per pixel in .4 binary fixed point
  3640. * @flags: DRM_DP_OVERHEAD_x flags
  3641. *
  3642. * Calculate the BW allocation overhead of a DP link stream, depending
  3643. * on the link's
  3644. * - @lane_count
  3645. * - SST/MST mode (@flags / %DRM_DP_OVERHEAD_MST)
  3646. * - symbol size (@flags / %DRM_DP_OVERHEAD_UHBR)
  3647. * - FEC mode (@flags / %DRM_DP_OVERHEAD_FEC)
  3648. * - SSC/REF_CLK mode (@flags / %DRM_DP_OVERHEAD_SSC_REF_CLK)
  3649. * as well as the stream's
  3650. * - @hactive timing
  3651. * - @bpp_x16 color depth
  3652. * - compression mode (@flags / %DRM_DP_OVERHEAD_DSC).
  3653. * Note that this overhead doesn't account for the 8b/10b, 128b/132b
  3654. * channel coding efficiency, for that see
  3655. * @drm_dp_link_bw_channel_coding_efficiency().
  3656. *
  3657. * Returns the overhead as 100% + overhead% in 1ppm units.
  3658. */
  3659. int drm_dp_bw_overhead(int lane_count, int hactive,
  3660. int dsc_slice_count,
  3661. int bpp_x16, unsigned long flags)
  3662. {
  3663. int symbol_size = flags & DRM_DP_BW_OVERHEAD_UHBR ? 32 : 8;
  3664. bool is_mst = flags & DRM_DP_BW_OVERHEAD_MST;
  3665. u32 overhead = 1000000;
  3666. int symbol_cycles;
  3667. if (lane_count == 0 || hactive == 0 || bpp_x16 == 0) {
  3668. DRM_DEBUG_KMS("Invalid BW overhead params: lane_count %d, hactive %d, bpp_x16 " FXP_Q4_FMT "\n",
  3669. lane_count, hactive,
  3670. FXP_Q4_ARGS(bpp_x16));
  3671. return 0;
  3672. }
  3673. /*
  3674. * DP Standard v2.1 2.6.4.1
  3675. * SSC downspread and ref clock variation margin:
  3676. * 5300ppm + 300ppm ~ 0.6%
  3677. */
  3678. if (flags & DRM_DP_BW_OVERHEAD_SSC_REF_CLK)
  3679. overhead += 6000;
  3680. /*
  3681. * DP Standard v2.1 2.6.4.1.1, 3.5.1.5.4:
  3682. * FEC symbol insertions for 8b/10b channel coding:
  3683. * After each 250 data symbols on 2-4 lanes:
  3684. * 250 LL + 5 FEC_PARITY_PH + 1 CD_ADJ (256 byte FEC block)
  3685. * After each 2 x 250 data symbols on 1 lane:
  3686. * 2 * 250 LL + 11 FEC_PARITY_PH + 1 CD_ADJ (512 byte FEC block)
  3687. * After 256 (2-4 lanes) or 128 (1 lane) FEC blocks:
  3688. * 256 * 256 bytes + 1 FEC_PM
  3689. * or
  3690. * 128 * 512 bytes + 1 FEC_PM
  3691. * (256 * 6 + 1) / (256 * 250) = 2.4015625 %
  3692. */
  3693. if (flags & DRM_DP_BW_OVERHEAD_FEC)
  3694. overhead += 24016;
  3695. /*
  3696. * DP Standard v2.1 2.7.9, 5.9.7
  3697. * The FEC overhead for UHBR is accounted for in its 96.71% channel
  3698. * coding efficiency.
  3699. */
  3700. WARN_ON((flags & DRM_DP_BW_OVERHEAD_UHBR) &&
  3701. (flags & DRM_DP_BW_OVERHEAD_FEC));
  3702. if (flags & DRM_DP_BW_OVERHEAD_DSC)
  3703. symbol_cycles = drm_dp_link_dsc_symbol_cycles(lane_count, hactive,
  3704. dsc_slice_count,
  3705. bpp_x16, symbol_size,
  3706. is_mst);
  3707. else
  3708. symbol_cycles = drm_dp_link_symbol_cycles(lane_count, hactive,
  3709. bpp_x16, symbol_size,
  3710. is_mst);
  3711. return DIV_ROUND_UP_ULL(mul_u32_u32(symbol_cycles * symbol_size * lane_count,
  3712. overhead * 16),
  3713. hactive * bpp_x16);
  3714. }
  3715. EXPORT_SYMBOL(drm_dp_bw_overhead);
  3716. /**
  3717. * drm_dp_bw_channel_coding_efficiency - Get a DP link's channel coding efficiency
  3718. * @is_uhbr: Whether the link has a 128b/132b channel coding
  3719. *
  3720. * Return the channel coding efficiency of the given DP link type, which is
  3721. * either 8b/10b or 128b/132b (aka UHBR). The corresponding overhead includes
  3722. * the 8b -> 10b, 128b -> 132b pixel data to link symbol conversion overhead
  3723. * and for 128b/132b any link or PHY level control symbol insertion overhead
  3724. * (LLCP, FEC, PHY sync, see DP Standard v2.1 3.5.2.18). For 8b/10b the
  3725. * corresponding FEC overhead is BW allocation specific, included in the value
  3726. * returned by drm_dp_bw_overhead().
  3727. *
  3728. * Returns the efficiency in the 100%/coding-overhead% ratio in
  3729. * 1ppm units.
  3730. */
  3731. int drm_dp_bw_channel_coding_efficiency(bool is_uhbr)
  3732. {
  3733. if (is_uhbr)
  3734. return 967100;
  3735. else
  3736. /*
  3737. * Note that on 8b/10b MST the efficiency is only
  3738. * 78.75% due to the 1 out of 64 MTPH packet overhead,
  3739. * not accounted for here.
  3740. */
  3741. return 800000;
  3742. }
  3743. EXPORT_SYMBOL(drm_dp_bw_channel_coding_efficiency);
  3744. /**
  3745. * drm_dp_max_dprx_data_rate - Get the max data bandwidth of a DPRX sink
  3746. * @max_link_rate: max DPRX link rate in 10kbps units
  3747. * @max_lanes: max DPRX lane count
  3748. *
  3749. * Given a link rate and lanes, get the data bandwidth.
  3750. *
  3751. * Data bandwidth is the actual payload rate, which depends on the data
  3752. * bandwidth efficiency and the link rate.
  3753. *
  3754. * Note that protocol layers above the DPRX link level considered here can
  3755. * further limit the maximum data rate. Such layers are the MST topology (with
  3756. * limits on the link between the source and first branch device as well as on
  3757. * the whole MST path until the DPRX link) and (Thunderbolt) DP tunnels -
  3758. * which in turn can encapsulate an MST link with its own limit - with each
  3759. * SST or MST encapsulated tunnel sharing the BW of a tunnel group.
  3760. *
  3761. * Returns the maximum data rate in kBps units.
  3762. */
  3763. int drm_dp_max_dprx_data_rate(int max_link_rate, int max_lanes)
  3764. {
  3765. int ch_coding_efficiency =
  3766. drm_dp_bw_channel_coding_efficiency(drm_dp_is_uhbr_rate(max_link_rate));
  3767. return DIV_ROUND_DOWN_ULL(mul_u32_u32(max_link_rate * 10 * max_lanes,
  3768. ch_coding_efficiency),
  3769. 1000000 * 8);
  3770. }
  3771. EXPORT_SYMBOL(drm_dp_max_dprx_data_rate);