dp_ctrl.c 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2012-2020, The Linux Foundation. All rights reserved.
  4. */
  5. #define pr_fmt(fmt) "[drm-dp] %s: " fmt, __func__
  6. #include <linux/types.h>
  7. #include <linux/clk.h>
  8. #include <linux/completion.h>
  9. #include <linux/delay.h>
  10. #include <linux/iopoll.h>
  11. #include <linux/phy/phy.h>
  12. #include <linux/phy/phy-dp.h>
  13. #include <linux/pm_opp.h>
  14. #include <linux/rational.h>
  15. #include <linux/string_choices.h>
  16. #include <drm/display/drm_dp_helper.h>
  17. #include <drm/drm_device.h>
  18. #include <drm/drm_fixed.h>
  19. #include <drm/drm_print.h>
  20. #include "dp_reg.h"
  21. #include "dp_ctrl.h"
  22. #include "dp_link.h"
  23. #define POLLING_SLEEP_US 1000
  24. #define POLLING_TIMEOUT_US 10000
  25. #define DP_KHZ_TO_HZ 1000
  26. #define IDLE_PATTERN_COMPLETION_TIMEOUT_JIFFIES (30 * HZ / 1000) /* 30 ms */
  27. #define PSR_OPERATION_COMPLETION_TIMEOUT_JIFFIES (300 * HZ / 1000) /* 300 ms */
  28. #define WAIT_FOR_VIDEO_READY_TIMEOUT_JIFFIES (HZ / 2)
  29. #define DP_INTERRUPT_STATUS_ACK_SHIFT 1
  30. #define DP_INTERRUPT_STATUS_MASK_SHIFT 2
  31. #define DP_INTERRUPT_STATUS1 \
  32. (DP_INTR_AUX_XFER_DONE| \
  33. DP_INTR_WRONG_ADDR | DP_INTR_TIMEOUT | \
  34. DP_INTR_NACK_DEFER | DP_INTR_WRONG_DATA_CNT | \
  35. DP_INTR_I2C_NACK | DP_INTR_I2C_DEFER | \
  36. DP_INTR_PLL_UNLOCKED | DP_INTR_AUX_ERROR)
  37. #define DP_INTERRUPT_STATUS1_ACK \
  38. (DP_INTERRUPT_STATUS1 << DP_INTERRUPT_STATUS_ACK_SHIFT)
  39. #define DP_INTERRUPT_STATUS1_MASK \
  40. (DP_INTERRUPT_STATUS1 << DP_INTERRUPT_STATUS_MASK_SHIFT)
  41. #define DP_INTERRUPT_STATUS2 \
  42. (DP_INTR_READY_FOR_VIDEO | DP_INTR_IDLE_PATTERN_SENT | \
  43. DP_INTR_FRAME_END | DP_INTR_CRC_UPDATED)
  44. #define DP_INTERRUPT_STATUS2_ACK \
  45. (DP_INTERRUPT_STATUS2 << DP_INTERRUPT_STATUS_ACK_SHIFT)
  46. #define DP_INTERRUPT_STATUS2_MASK \
  47. (DP_INTERRUPT_STATUS2 << DP_INTERRUPT_STATUS_MASK_SHIFT)
  48. #define DP_INTERRUPT_STATUS4 \
  49. (PSR_UPDATE_INT | PSR_CAPTURE_INT | PSR_EXIT_INT | \
  50. PSR_UPDATE_ERROR_INT | PSR_WAKE_ERROR_INT)
  51. #define DP_INTERRUPT_MASK4 \
  52. (PSR_UPDATE_MASK | PSR_CAPTURE_MASK | PSR_EXIT_MASK | \
  53. PSR_UPDATE_ERROR_MASK | PSR_WAKE_ERROR_MASK)
  54. #define DP_CTRL_INTR_READY_FOR_VIDEO BIT(0)
  55. #define DP_CTRL_INTR_IDLE_PATTERN_SENT BIT(3)
  56. #define MR_LINK_TRAINING1 0x8
  57. #define MR_LINK_SYMBOL_ERM 0x80
  58. #define MR_LINK_PRBS7 0x100
  59. #define MR_LINK_CUSTOM80 0x200
  60. #define MR_LINK_TRAINING4 0x40
  61. enum {
  62. DP_TRAINING_NONE,
  63. DP_TRAINING_1,
  64. DP_TRAINING_2,
  65. };
  66. struct msm_dp_tu_calc_input {
  67. u64 lclk; /* 162, 270, 540 and 810 */
  68. u64 pclk_khz; /* in KHz */
  69. u64 hactive; /* active h-width */
  70. u64 hporch; /* bp + fp + pulse */
  71. int nlanes; /* no.of.lanes */
  72. int bpp; /* bits */
  73. int pixel_enc; /* 444, 420, 422 */
  74. int dsc_en; /* dsc on/off */
  75. int async_en; /* async mode */
  76. int fec_en; /* fec */
  77. int compress_ratio; /* 2:1 = 200, 3:1 = 300, 3.75:1 = 375 */
  78. int num_of_dsc_slices; /* number of slices per line */
  79. };
  80. struct msm_dp_vc_tu_mapping_table {
  81. u32 vic;
  82. u8 lanes;
  83. u8 lrate; /* DP_LINK_RATE -> 162(6), 270(10), 540(20), 810 (30) */
  84. u8 bpp;
  85. u8 valid_boundary_link;
  86. u16 delay_start_link;
  87. bool boundary_moderation_en;
  88. u8 valid_lower_boundary_link;
  89. u8 upper_boundary_count;
  90. u8 lower_boundary_count;
  91. u8 tu_size_minus1;
  92. };
  93. struct msm_dp_ctrl_private {
  94. struct msm_dp_ctrl msm_dp_ctrl;
  95. struct drm_device *drm_dev;
  96. struct device *dev;
  97. struct drm_dp_aux *aux;
  98. struct msm_dp_panel *panel;
  99. struct msm_dp_link *link;
  100. void __iomem *ahb_base;
  101. void __iomem *link_base;
  102. struct phy *phy;
  103. unsigned int num_core_clks;
  104. struct clk_bulk_data *core_clks;
  105. unsigned int num_link_clks;
  106. struct clk_bulk_data *link_clks;
  107. struct clk *pixel_clk;
  108. union phy_configure_opts phy_opts;
  109. struct completion idle_comp;
  110. struct completion psr_op_comp;
  111. struct completion video_comp;
  112. u32 hw_revision;
  113. bool core_clks_on;
  114. bool link_clks_on;
  115. bool stream_clks_on;
  116. };
  117. static inline u32 msm_dp_read_ahb(const struct msm_dp_ctrl_private *ctrl, u32 offset)
  118. {
  119. return readl_relaxed(ctrl->ahb_base + offset);
  120. }
  121. static inline void msm_dp_write_ahb(struct msm_dp_ctrl_private *ctrl,
  122. u32 offset, u32 data)
  123. {
  124. /*
  125. * To make sure phy reg writes happens before any other operation,
  126. * this function uses writel() instread of writel_relaxed()
  127. */
  128. writel(data, ctrl->ahb_base + offset);
  129. }
  130. static inline u32 msm_dp_read_link(struct msm_dp_ctrl_private *ctrl, u32 offset)
  131. {
  132. return readl_relaxed(ctrl->link_base + offset);
  133. }
  134. static inline void msm_dp_write_link(struct msm_dp_ctrl_private *ctrl,
  135. u32 offset, u32 data)
  136. {
  137. /*
  138. * To make sure link reg writes happens before any other operation,
  139. * this function uses writel() instread of writel_relaxed()
  140. */
  141. writel(data, ctrl->link_base + offset);
  142. }
  143. static int msm_dp_aux_link_configure(struct drm_dp_aux *aux,
  144. struct msm_dp_link_info *link)
  145. {
  146. u8 lane_count, bw_code;
  147. int err;
  148. lane_count = link->num_lanes;
  149. if (link->capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
  150. lane_count |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
  151. err = drm_dp_dpcd_writeb(aux, DP_LANE_COUNT_SET, lane_count);
  152. if (err < 0)
  153. return err;
  154. if (link->use_rate_set) {
  155. DRM_DEBUG_DP("using LINK_RATE_SET: 0x%02x", link->rate_set);
  156. err = drm_dp_dpcd_writeb(aux, DP_LINK_RATE_SET, link->rate_set);
  157. } else {
  158. bw_code = drm_dp_link_rate_to_bw_code(link->rate);
  159. DRM_DEBUG_DP("using LINK_BW_SET: 0x%02x", bw_code);
  160. err = drm_dp_dpcd_writeb(aux, DP_LINK_BW_SET, bw_code);
  161. }
  162. return err;
  163. }
  164. /*
  165. * NOTE: resetting DP controller will also clear any pending HPD related interrupts
  166. */
  167. void msm_dp_ctrl_reset(struct msm_dp_ctrl *msm_dp_ctrl)
  168. {
  169. struct msm_dp_ctrl_private *ctrl =
  170. container_of(msm_dp_ctrl, struct msm_dp_ctrl_private, msm_dp_ctrl);
  171. u32 sw_reset;
  172. sw_reset = msm_dp_read_ahb(ctrl, REG_DP_SW_RESET);
  173. sw_reset |= DP_SW_RESET;
  174. msm_dp_write_ahb(ctrl, REG_DP_SW_RESET, sw_reset);
  175. usleep_range(1000, 1100); /* h/w recommended delay */
  176. sw_reset &= ~DP_SW_RESET;
  177. msm_dp_write_ahb(ctrl, REG_DP_SW_RESET, sw_reset);
  178. if (!ctrl->hw_revision) {
  179. ctrl->hw_revision = msm_dp_read_ahb(ctrl, REG_DP_HW_VERSION);
  180. ctrl->panel->hw_revision = ctrl->hw_revision;
  181. }
  182. }
  183. static u32 msm_dp_ctrl_get_aux_interrupt(struct msm_dp_ctrl_private *ctrl)
  184. {
  185. u32 intr, intr_ack;
  186. intr = msm_dp_read_ahb(ctrl, REG_DP_INTR_STATUS);
  187. intr &= ~DP_INTERRUPT_STATUS1_MASK;
  188. intr_ack = (intr & DP_INTERRUPT_STATUS1)
  189. << DP_INTERRUPT_STATUS_ACK_SHIFT;
  190. msm_dp_write_ahb(ctrl, REG_DP_INTR_STATUS,
  191. intr_ack | DP_INTERRUPT_STATUS1_MASK);
  192. return intr;
  193. }
  194. static u32 msm_dp_ctrl_get_interrupt(struct msm_dp_ctrl_private *ctrl)
  195. {
  196. u32 intr, intr_ack;
  197. intr = msm_dp_read_ahb(ctrl, REG_DP_INTR_STATUS2);
  198. intr &= ~DP_INTERRUPT_STATUS2_MASK;
  199. intr_ack = (intr & DP_INTERRUPT_STATUS2)
  200. << DP_INTERRUPT_STATUS_ACK_SHIFT;
  201. msm_dp_write_ahb(ctrl, REG_DP_INTR_STATUS2,
  202. intr_ack | DP_INTERRUPT_STATUS2_MASK);
  203. return intr;
  204. }
  205. void msm_dp_ctrl_enable_irq(struct msm_dp_ctrl *msm_dp_ctrl)
  206. {
  207. struct msm_dp_ctrl_private *ctrl =
  208. container_of(msm_dp_ctrl, struct msm_dp_ctrl_private, msm_dp_ctrl);
  209. msm_dp_write_ahb(ctrl, REG_DP_INTR_STATUS,
  210. DP_INTERRUPT_STATUS1_MASK);
  211. msm_dp_write_ahb(ctrl, REG_DP_INTR_STATUS2,
  212. DP_INTERRUPT_STATUS2_MASK);
  213. }
  214. void msm_dp_ctrl_disable_irq(struct msm_dp_ctrl *msm_dp_ctrl)
  215. {
  216. struct msm_dp_ctrl_private *ctrl =
  217. container_of(msm_dp_ctrl, struct msm_dp_ctrl_private, msm_dp_ctrl);
  218. msm_dp_write_ahb(ctrl, REG_DP_INTR_STATUS, 0x00);
  219. msm_dp_write_ahb(ctrl, REG_DP_INTR_STATUS2, 0x00);
  220. }
  221. static u32 msm_dp_ctrl_get_psr_interrupt(struct msm_dp_ctrl_private *ctrl)
  222. {
  223. u32 intr, intr_ack;
  224. intr = msm_dp_read_ahb(ctrl, REG_DP_INTR_STATUS4);
  225. intr_ack = (intr & DP_INTERRUPT_STATUS4)
  226. << DP_INTERRUPT_STATUS_ACK_SHIFT;
  227. msm_dp_write_ahb(ctrl, REG_DP_INTR_STATUS4, intr_ack);
  228. return intr;
  229. }
  230. static void msm_dp_ctrl_config_psr_interrupt(struct msm_dp_ctrl_private *ctrl)
  231. {
  232. msm_dp_write_ahb(ctrl, REG_DP_INTR_MASK4, DP_INTERRUPT_MASK4);
  233. }
  234. static void msm_dp_ctrl_psr_mainlink_enable(struct msm_dp_ctrl_private *ctrl)
  235. {
  236. u32 val;
  237. val = msm_dp_read_link(ctrl, REG_DP_MAINLINK_CTRL);
  238. val |= DP_MAINLINK_CTRL_ENABLE;
  239. msm_dp_write_link(ctrl, REG_DP_MAINLINK_CTRL, val);
  240. }
  241. static void msm_dp_ctrl_psr_mainlink_disable(struct msm_dp_ctrl_private *ctrl)
  242. {
  243. u32 val;
  244. val = msm_dp_read_link(ctrl, REG_DP_MAINLINK_CTRL);
  245. val &= ~DP_MAINLINK_CTRL_ENABLE;
  246. msm_dp_write_link(ctrl, REG_DP_MAINLINK_CTRL, val);
  247. }
  248. static void msm_dp_ctrl_mainlink_enable(struct msm_dp_ctrl_private *ctrl)
  249. {
  250. u32 mainlink_ctrl;
  251. drm_dbg_dp(ctrl->drm_dev, "enable\n");
  252. mainlink_ctrl = msm_dp_read_link(ctrl, REG_DP_MAINLINK_CTRL);
  253. mainlink_ctrl &= ~(DP_MAINLINK_CTRL_RESET |
  254. DP_MAINLINK_CTRL_ENABLE);
  255. msm_dp_write_link(ctrl, REG_DP_MAINLINK_CTRL, mainlink_ctrl);
  256. mainlink_ctrl |= DP_MAINLINK_CTRL_RESET;
  257. msm_dp_write_link(ctrl, REG_DP_MAINLINK_CTRL, mainlink_ctrl);
  258. mainlink_ctrl &= ~DP_MAINLINK_CTRL_RESET;
  259. msm_dp_write_link(ctrl, REG_DP_MAINLINK_CTRL, mainlink_ctrl);
  260. mainlink_ctrl |= (DP_MAINLINK_CTRL_ENABLE |
  261. DP_MAINLINK_FB_BOUNDARY_SEL);
  262. msm_dp_write_link(ctrl, REG_DP_MAINLINK_CTRL, mainlink_ctrl);
  263. }
  264. static void msm_dp_ctrl_mainlink_disable(struct msm_dp_ctrl_private *ctrl)
  265. {
  266. u32 mainlink_ctrl;
  267. drm_dbg_dp(ctrl->drm_dev, "disable\n");
  268. mainlink_ctrl = msm_dp_read_link(ctrl, REG_DP_MAINLINK_CTRL);
  269. mainlink_ctrl &= ~DP_MAINLINK_CTRL_ENABLE;
  270. msm_dp_write_link(ctrl, REG_DP_MAINLINK_CTRL, mainlink_ctrl);
  271. }
  272. static void msm_dp_setup_peripheral_flush(struct msm_dp_ctrl_private *ctrl)
  273. {
  274. u32 mainlink_ctrl;
  275. mainlink_ctrl = msm_dp_read_link(ctrl, REG_DP_MAINLINK_CTRL);
  276. if (ctrl->hw_revision >= DP_HW_VERSION_1_2)
  277. mainlink_ctrl |= DP_MAINLINK_FLUSH_MODE_SDE_PERIPH_UPDATE;
  278. else
  279. mainlink_ctrl |= DP_MAINLINK_FLUSH_MODE_UPDATE_SDP;
  280. msm_dp_write_link(ctrl, REG_DP_MAINLINK_CTRL, mainlink_ctrl);
  281. }
  282. static bool msm_dp_ctrl_mainlink_ready(struct msm_dp_ctrl_private *ctrl)
  283. {
  284. u32 data;
  285. int ret;
  286. /* Poll for mainlink ready status */
  287. ret = readl_poll_timeout(ctrl->link_base + REG_DP_MAINLINK_READY,
  288. data, data & DP_MAINLINK_READY_FOR_VIDEO,
  289. POLLING_SLEEP_US, POLLING_TIMEOUT_US);
  290. if (ret < 0) {
  291. DRM_ERROR("mainlink not ready\n");
  292. return false;
  293. }
  294. return true;
  295. }
  296. void msm_dp_ctrl_push_idle(struct msm_dp_ctrl *msm_dp_ctrl)
  297. {
  298. struct msm_dp_ctrl_private *ctrl;
  299. ctrl = container_of(msm_dp_ctrl, struct msm_dp_ctrl_private, msm_dp_ctrl);
  300. reinit_completion(&ctrl->idle_comp);
  301. msm_dp_write_link(ctrl, REG_DP_STATE_CTRL, DP_STATE_CTRL_PUSH_IDLE);
  302. if (!wait_for_completion_timeout(&ctrl->idle_comp,
  303. IDLE_PATTERN_COMPLETION_TIMEOUT_JIFFIES))
  304. pr_warn("PUSH_IDLE pattern timedout\n");
  305. drm_dbg_dp(ctrl->drm_dev, "mainlink off\n");
  306. }
  307. static void msm_dp_ctrl_config_ctrl(struct msm_dp_ctrl_private *ctrl)
  308. {
  309. u32 config = 0, tbd;
  310. const u8 *dpcd = ctrl->panel->dpcd;
  311. /* Default-> LSCLK DIV: 1/4 LCLK */
  312. config |= (2 << DP_CONFIGURATION_CTRL_LSCLK_DIV_SHIFT);
  313. if (ctrl->panel->msm_dp_mode.out_fmt_is_yuv_420)
  314. config |= DP_CONFIGURATION_CTRL_RGB_YUV; /* YUV420 */
  315. /* Scrambler reset enable */
  316. if (drm_dp_alternate_scrambler_reset_cap(dpcd))
  317. config |= DP_CONFIGURATION_CTRL_ASSR;
  318. tbd = msm_dp_link_get_test_bits_depth(ctrl->link,
  319. ctrl->panel->msm_dp_mode.bpp);
  320. config |= tbd << DP_CONFIGURATION_CTRL_BPC_SHIFT;
  321. /* Num of Lanes */
  322. config |= ((ctrl->link->link_params.num_lanes - 1)
  323. << DP_CONFIGURATION_CTRL_NUM_OF_LANES_SHIFT);
  324. if (drm_dp_enhanced_frame_cap(dpcd))
  325. config |= DP_CONFIGURATION_CTRL_ENHANCED_FRAMING;
  326. config |= DP_CONFIGURATION_CTRL_P_INTERLACED; /* progressive video */
  327. /* sync clock & static Mvid */
  328. config |= DP_CONFIGURATION_CTRL_STATIC_DYNAMIC_CN;
  329. config |= DP_CONFIGURATION_CTRL_SYNC_ASYNC_CLK;
  330. if (ctrl->panel->psr_cap.version)
  331. config |= DP_CONFIGURATION_CTRL_SEND_VSC;
  332. drm_dbg_dp(ctrl->drm_dev, "DP_CONFIGURATION_CTRL=0x%x\n", config);
  333. msm_dp_write_link(ctrl, REG_DP_CONFIGURATION_CTRL, config);
  334. }
  335. static void msm_dp_ctrl_lane_mapping(struct msm_dp_ctrl_private *ctrl)
  336. {
  337. u32 *lane_map = ctrl->link->lane_map;
  338. u32 ln_mapping;
  339. ln_mapping = lane_map[0] << LANE0_MAPPING_SHIFT;
  340. ln_mapping |= lane_map[1] << LANE1_MAPPING_SHIFT;
  341. ln_mapping |= lane_map[2] << LANE2_MAPPING_SHIFT;
  342. ln_mapping |= lane_map[3] << LANE3_MAPPING_SHIFT;
  343. msm_dp_write_link(ctrl, REG_DP_LOGICAL2PHYSICAL_LANE_MAPPING,
  344. ln_mapping);
  345. }
  346. static void msm_dp_ctrl_configure_source_params(struct msm_dp_ctrl_private *ctrl)
  347. {
  348. u32 colorimetry_cfg, test_bits_depth, misc_val;
  349. msm_dp_ctrl_lane_mapping(ctrl);
  350. msm_dp_setup_peripheral_flush(ctrl);
  351. msm_dp_ctrl_config_ctrl(ctrl);
  352. test_bits_depth = msm_dp_link_get_test_bits_depth(ctrl->link, ctrl->panel->msm_dp_mode.bpp);
  353. colorimetry_cfg = msm_dp_link_get_colorimetry_config(ctrl->link);
  354. misc_val = msm_dp_read_link(ctrl, REG_DP_MISC1_MISC0);
  355. /* clear bpp bits */
  356. misc_val &= ~(0x07 << DP_MISC0_TEST_BITS_DEPTH_SHIFT);
  357. misc_val |= colorimetry_cfg << DP_MISC0_COLORIMETRY_CFG_SHIFT;
  358. misc_val |= test_bits_depth << DP_MISC0_TEST_BITS_DEPTH_SHIFT;
  359. /* Configure clock to synchronous mode */
  360. misc_val |= DP_MISC0_SYNCHRONOUS_CLK;
  361. drm_dbg_dp(ctrl->drm_dev, "misc settings = 0x%x\n", misc_val);
  362. msm_dp_write_link(ctrl, REG_DP_MISC1_MISC0, misc_val);
  363. msm_dp_panel_timing_cfg(ctrl->panel, ctrl->msm_dp_ctrl.wide_bus_en);
  364. }
  365. /*
  366. * The structure and few functions present below are IP/Hardware
  367. * specific implementation. Most of the implementation will not
  368. * have coding comments
  369. */
  370. struct tu_algo_data {
  371. s64 lclk_fp;
  372. s64 pclk_fp;
  373. s64 lwidth;
  374. s64 lwidth_fp;
  375. s64 hbp_relative_to_pclk;
  376. s64 hbp_relative_to_pclk_fp;
  377. int nlanes;
  378. int bpp;
  379. int pixelEnc;
  380. int dsc_en;
  381. int async_en;
  382. int bpc;
  383. uint delay_start_link_extra_pixclk;
  384. int extra_buffer_margin;
  385. s64 ratio_fp;
  386. s64 original_ratio_fp;
  387. s64 err_fp;
  388. s64 n_err_fp;
  389. s64 n_n_err_fp;
  390. int tu_size;
  391. int tu_size_desired;
  392. int tu_size_minus1;
  393. int valid_boundary_link;
  394. s64 resulting_valid_fp;
  395. s64 total_valid_fp;
  396. s64 effective_valid_fp;
  397. s64 effective_valid_recorded_fp;
  398. int n_tus;
  399. int n_tus_per_lane;
  400. int paired_tus;
  401. int remainder_tus;
  402. int remainder_tus_upper;
  403. int remainder_tus_lower;
  404. int extra_bytes;
  405. int filler_size;
  406. int delay_start_link;
  407. int extra_pclk_cycles;
  408. int extra_pclk_cycles_in_link_clk;
  409. s64 ratio_by_tu_fp;
  410. s64 average_valid2_fp;
  411. int new_valid_boundary_link;
  412. int remainder_symbols_exist;
  413. int n_symbols;
  414. s64 n_remainder_symbols_per_lane_fp;
  415. s64 last_partial_tu_fp;
  416. s64 TU_ratio_err_fp;
  417. int n_tus_incl_last_incomplete_tu;
  418. int extra_pclk_cycles_tmp;
  419. int extra_pclk_cycles_in_link_clk_tmp;
  420. int extra_required_bytes_new_tmp;
  421. int filler_size_tmp;
  422. int lower_filler_size_tmp;
  423. int delay_start_link_tmp;
  424. bool boundary_moderation_en;
  425. int boundary_mod_lower_err;
  426. int upper_boundary_count;
  427. int lower_boundary_count;
  428. int i_upper_boundary_count;
  429. int i_lower_boundary_count;
  430. int valid_lower_boundary_link;
  431. int even_distribution_BF;
  432. int even_distribution_legacy;
  433. int even_distribution;
  434. int min_hblank_violated;
  435. s64 delay_start_time_fp;
  436. s64 hbp_time_fp;
  437. s64 hactive_time_fp;
  438. s64 diff_abs_fp;
  439. s64 ratio;
  440. };
  441. static int _tu_param_compare(s64 a, s64 b)
  442. {
  443. u32 a_sign;
  444. u32 b_sign;
  445. s64 a_temp, b_temp, minus_1;
  446. if (a == b)
  447. return 0;
  448. minus_1 = drm_fixp_from_fraction(-1, 1);
  449. a_sign = (a >> 32) & 0x80000000 ? 1 : 0;
  450. b_sign = (b >> 32) & 0x80000000 ? 1 : 0;
  451. if (a_sign > b_sign)
  452. return 2;
  453. else if (b_sign > a_sign)
  454. return 1;
  455. if (!a_sign && !b_sign) { /* positive */
  456. if (a > b)
  457. return 1;
  458. else
  459. return 2;
  460. } else { /* negative */
  461. a_temp = drm_fixp_mul(a, minus_1);
  462. b_temp = drm_fixp_mul(b, minus_1);
  463. if (a_temp > b_temp)
  464. return 2;
  465. else
  466. return 1;
  467. }
  468. }
  469. static void msm_dp_panel_update_tu_timings(struct msm_dp_tu_calc_input *in,
  470. struct tu_algo_data *tu)
  471. {
  472. int nlanes = in->nlanes;
  473. int dsc_num_slices = in->num_of_dsc_slices;
  474. int dsc_num_bytes = 0;
  475. int numerator;
  476. s64 pclk_dsc_fp;
  477. s64 dwidth_dsc_fp;
  478. s64 hbp_dsc_fp;
  479. int tot_num_eoc_symbols = 0;
  480. int tot_num_hor_bytes = 0;
  481. int tot_num_dummy_bytes = 0;
  482. int dwidth_dsc_bytes = 0;
  483. int eoc_bytes = 0;
  484. s64 temp1_fp, temp2_fp, temp3_fp;
  485. tu->lclk_fp = drm_fixp_from_fraction(in->lclk, 1);
  486. tu->pclk_fp = drm_fixp_from_fraction(in->pclk_khz, 1000);
  487. tu->lwidth = in->hactive;
  488. tu->hbp_relative_to_pclk = in->hporch;
  489. tu->nlanes = in->nlanes;
  490. tu->bpp = in->bpp;
  491. tu->pixelEnc = in->pixel_enc;
  492. tu->dsc_en = in->dsc_en;
  493. tu->async_en = in->async_en;
  494. tu->lwidth_fp = drm_fixp_from_fraction(in->hactive, 1);
  495. tu->hbp_relative_to_pclk_fp = drm_fixp_from_fraction(in->hporch, 1);
  496. if (tu->pixelEnc == 420) {
  497. temp1_fp = drm_fixp_from_fraction(2, 1);
  498. tu->pclk_fp = drm_fixp_div(tu->pclk_fp, temp1_fp);
  499. tu->lwidth_fp = drm_fixp_div(tu->lwidth_fp, temp1_fp);
  500. tu->hbp_relative_to_pclk_fp =
  501. drm_fixp_div(tu->hbp_relative_to_pclk_fp, 2);
  502. }
  503. if (tu->pixelEnc == 422) {
  504. switch (tu->bpp) {
  505. case 24:
  506. tu->bpp = 16;
  507. tu->bpc = 8;
  508. break;
  509. case 30:
  510. tu->bpp = 20;
  511. tu->bpc = 10;
  512. break;
  513. default:
  514. tu->bpp = 16;
  515. tu->bpc = 8;
  516. break;
  517. }
  518. } else {
  519. tu->bpc = tu->bpp/3;
  520. }
  521. if (!in->dsc_en)
  522. goto fec_check;
  523. temp1_fp = drm_fixp_from_fraction(in->compress_ratio, 100);
  524. temp2_fp = drm_fixp_from_fraction(in->bpp, 1);
  525. temp3_fp = drm_fixp_div(temp2_fp, temp1_fp);
  526. temp2_fp = drm_fixp_mul(tu->lwidth_fp, temp3_fp);
  527. temp1_fp = drm_fixp_from_fraction(8, 1);
  528. temp3_fp = drm_fixp_div(temp2_fp, temp1_fp);
  529. numerator = drm_fixp2int(temp3_fp);
  530. dsc_num_bytes = numerator / dsc_num_slices;
  531. eoc_bytes = dsc_num_bytes % nlanes;
  532. tot_num_eoc_symbols = nlanes * dsc_num_slices;
  533. tot_num_hor_bytes = dsc_num_bytes * dsc_num_slices;
  534. tot_num_dummy_bytes = (nlanes - eoc_bytes) * dsc_num_slices;
  535. if (dsc_num_bytes == 0)
  536. pr_info("incorrect no of bytes per slice=%d\n", dsc_num_bytes);
  537. dwidth_dsc_bytes = (tot_num_hor_bytes +
  538. tot_num_eoc_symbols +
  539. (eoc_bytes == 0 ? 0 : tot_num_dummy_bytes));
  540. dwidth_dsc_fp = drm_fixp_from_fraction(dwidth_dsc_bytes, 3);
  541. temp2_fp = drm_fixp_mul(tu->pclk_fp, dwidth_dsc_fp);
  542. temp1_fp = drm_fixp_div(temp2_fp, tu->lwidth_fp);
  543. pclk_dsc_fp = temp1_fp;
  544. temp1_fp = drm_fixp_div(pclk_dsc_fp, tu->pclk_fp);
  545. temp2_fp = drm_fixp_mul(tu->hbp_relative_to_pclk_fp, temp1_fp);
  546. hbp_dsc_fp = temp2_fp;
  547. /* output */
  548. tu->pclk_fp = pclk_dsc_fp;
  549. tu->lwidth_fp = dwidth_dsc_fp;
  550. tu->hbp_relative_to_pclk_fp = hbp_dsc_fp;
  551. fec_check:
  552. if (in->fec_en) {
  553. temp1_fp = drm_fixp_from_fraction(976, 1000); /* 0.976 */
  554. tu->lclk_fp = drm_fixp_mul(tu->lclk_fp, temp1_fp);
  555. }
  556. }
  557. static void _tu_valid_boundary_calc(struct tu_algo_data *tu)
  558. {
  559. s64 temp1_fp, temp2_fp, temp, temp1, temp2;
  560. int compare_result_1, compare_result_2, compare_result_3;
  561. temp1_fp = drm_fixp_from_fraction(tu->tu_size, 1);
  562. temp2_fp = drm_fixp_mul(tu->ratio_fp, temp1_fp);
  563. tu->new_valid_boundary_link = drm_fixp2int_ceil(temp2_fp);
  564. temp = (tu->i_upper_boundary_count *
  565. tu->new_valid_boundary_link +
  566. tu->i_lower_boundary_count *
  567. (tu->new_valid_boundary_link-1));
  568. tu->average_valid2_fp = drm_fixp_from_fraction(temp,
  569. (tu->i_upper_boundary_count +
  570. tu->i_lower_boundary_count));
  571. temp1_fp = drm_fixp_from_fraction(tu->bpp, 8);
  572. temp2_fp = tu->lwidth_fp;
  573. temp1_fp = drm_fixp_mul(temp2_fp, temp1_fp);
  574. temp2_fp = drm_fixp_div(temp1_fp, tu->average_valid2_fp);
  575. tu->n_tus = drm_fixp2int(temp2_fp);
  576. if ((temp2_fp & 0xFFFFFFFF) > 0xFFFFF000)
  577. tu->n_tus += 1;
  578. temp1_fp = drm_fixp_from_fraction(tu->n_tus, 1);
  579. temp2_fp = drm_fixp_mul(temp1_fp, tu->average_valid2_fp);
  580. temp1_fp = drm_fixp_from_fraction(tu->n_symbols, 1);
  581. temp2_fp = temp1_fp - temp2_fp;
  582. temp1_fp = drm_fixp_from_fraction(tu->nlanes, 1);
  583. temp2_fp = drm_fixp_div(temp2_fp, temp1_fp);
  584. tu->n_remainder_symbols_per_lane_fp = temp2_fp;
  585. temp1_fp = drm_fixp_from_fraction(tu->tu_size, 1);
  586. tu->last_partial_tu_fp =
  587. drm_fixp_div(tu->n_remainder_symbols_per_lane_fp,
  588. temp1_fp);
  589. if (tu->n_remainder_symbols_per_lane_fp != 0)
  590. tu->remainder_symbols_exist = 1;
  591. else
  592. tu->remainder_symbols_exist = 0;
  593. temp1_fp = drm_fixp_from_fraction(tu->n_tus, tu->nlanes);
  594. tu->n_tus_per_lane = drm_fixp2int(temp1_fp);
  595. tu->paired_tus = (int)((tu->n_tus_per_lane) /
  596. (tu->i_upper_boundary_count +
  597. tu->i_lower_boundary_count));
  598. tu->remainder_tus = tu->n_tus_per_lane - tu->paired_tus *
  599. (tu->i_upper_boundary_count +
  600. tu->i_lower_boundary_count);
  601. if ((tu->remainder_tus - tu->i_upper_boundary_count) > 0) {
  602. tu->remainder_tus_upper = tu->i_upper_boundary_count;
  603. tu->remainder_tus_lower = tu->remainder_tus -
  604. tu->i_upper_boundary_count;
  605. } else {
  606. tu->remainder_tus_upper = tu->remainder_tus;
  607. tu->remainder_tus_lower = 0;
  608. }
  609. temp = tu->paired_tus * (tu->i_upper_boundary_count *
  610. tu->new_valid_boundary_link +
  611. tu->i_lower_boundary_count *
  612. (tu->new_valid_boundary_link - 1)) +
  613. (tu->remainder_tus_upper *
  614. tu->new_valid_boundary_link) +
  615. (tu->remainder_tus_lower *
  616. (tu->new_valid_boundary_link - 1));
  617. tu->total_valid_fp = drm_fixp_from_fraction(temp, 1);
  618. if (tu->remainder_symbols_exist) {
  619. temp1_fp = tu->total_valid_fp +
  620. tu->n_remainder_symbols_per_lane_fp;
  621. temp2_fp = drm_fixp_from_fraction(tu->n_tus_per_lane, 1);
  622. temp2_fp = temp2_fp + tu->last_partial_tu_fp;
  623. temp1_fp = drm_fixp_div(temp1_fp, temp2_fp);
  624. } else {
  625. temp2_fp = drm_fixp_from_fraction(tu->n_tus_per_lane, 1);
  626. temp1_fp = drm_fixp_div(tu->total_valid_fp, temp2_fp);
  627. }
  628. tu->effective_valid_fp = temp1_fp;
  629. temp1_fp = drm_fixp_from_fraction(tu->tu_size, 1);
  630. temp2_fp = drm_fixp_mul(tu->ratio_fp, temp1_fp);
  631. tu->n_n_err_fp = tu->effective_valid_fp - temp2_fp;
  632. temp1_fp = drm_fixp_from_fraction(tu->tu_size, 1);
  633. temp2_fp = drm_fixp_mul(tu->ratio_fp, temp1_fp);
  634. tu->n_err_fp = tu->average_valid2_fp - temp2_fp;
  635. tu->even_distribution = tu->n_tus % tu->nlanes == 0 ? 1 : 0;
  636. temp1_fp = drm_fixp_from_fraction(tu->bpp, 8);
  637. temp2_fp = tu->lwidth_fp;
  638. temp1_fp = drm_fixp_mul(temp2_fp, temp1_fp);
  639. temp2_fp = drm_fixp_div(temp1_fp, tu->average_valid2_fp);
  640. if (temp2_fp)
  641. tu->n_tus_incl_last_incomplete_tu = drm_fixp2int_ceil(temp2_fp);
  642. else
  643. tu->n_tus_incl_last_incomplete_tu = 0;
  644. temp1 = 0;
  645. temp1_fp = drm_fixp_from_fraction(tu->tu_size, 1);
  646. temp2_fp = drm_fixp_mul(tu->original_ratio_fp, temp1_fp);
  647. temp1_fp = tu->average_valid2_fp - temp2_fp;
  648. temp2_fp = drm_fixp_from_fraction(tu->n_tus_incl_last_incomplete_tu, 1);
  649. temp1_fp = drm_fixp_mul(temp2_fp, temp1_fp);
  650. if (temp1_fp)
  651. temp1 = drm_fixp2int_ceil(temp1_fp);
  652. temp = tu->i_upper_boundary_count * tu->nlanes;
  653. temp1_fp = drm_fixp_from_fraction(tu->tu_size, 1);
  654. temp2_fp = drm_fixp_mul(tu->original_ratio_fp, temp1_fp);
  655. temp1_fp = drm_fixp_from_fraction(tu->new_valid_boundary_link, 1);
  656. temp2_fp = temp1_fp - temp2_fp;
  657. temp1_fp = drm_fixp_from_fraction(temp, 1);
  658. temp2_fp = drm_fixp_mul(temp1_fp, temp2_fp);
  659. if (temp2_fp)
  660. temp2 = drm_fixp2int_ceil(temp2_fp);
  661. else
  662. temp2 = 0;
  663. tu->extra_required_bytes_new_tmp = (int)(temp1 + temp2);
  664. temp1_fp = drm_fixp_from_fraction(8, tu->bpp);
  665. temp2_fp = drm_fixp_from_fraction(
  666. tu->extra_required_bytes_new_tmp, 1);
  667. temp1_fp = drm_fixp_mul(temp2_fp, temp1_fp);
  668. if (temp1_fp)
  669. tu->extra_pclk_cycles_tmp = drm_fixp2int_ceil(temp1_fp);
  670. else
  671. tu->extra_pclk_cycles_tmp = 0;
  672. temp1_fp = drm_fixp_from_fraction(tu->extra_pclk_cycles_tmp, 1);
  673. temp2_fp = drm_fixp_div(tu->lclk_fp, tu->pclk_fp);
  674. temp1_fp = drm_fixp_mul(temp1_fp, temp2_fp);
  675. if (temp1_fp)
  676. tu->extra_pclk_cycles_in_link_clk_tmp =
  677. drm_fixp2int_ceil(temp1_fp);
  678. else
  679. tu->extra_pclk_cycles_in_link_clk_tmp = 0;
  680. tu->filler_size_tmp = tu->tu_size - tu->new_valid_boundary_link;
  681. tu->lower_filler_size_tmp = tu->filler_size_tmp + 1;
  682. tu->delay_start_link_tmp = tu->extra_pclk_cycles_in_link_clk_tmp +
  683. tu->lower_filler_size_tmp +
  684. tu->extra_buffer_margin;
  685. temp1_fp = drm_fixp_from_fraction(tu->delay_start_link_tmp, 1);
  686. tu->delay_start_time_fp = drm_fixp_div(temp1_fp, tu->lclk_fp);
  687. compare_result_1 = _tu_param_compare(tu->n_n_err_fp, tu->diff_abs_fp);
  688. if (compare_result_1 == 2)
  689. compare_result_1 = 1;
  690. else
  691. compare_result_1 = 0;
  692. compare_result_2 = _tu_param_compare(tu->n_n_err_fp, tu->err_fp);
  693. if (compare_result_2 == 2)
  694. compare_result_2 = 1;
  695. else
  696. compare_result_2 = 0;
  697. compare_result_3 = _tu_param_compare(tu->hbp_time_fp,
  698. tu->delay_start_time_fp);
  699. if (compare_result_3 == 2)
  700. compare_result_3 = 0;
  701. else
  702. compare_result_3 = 1;
  703. if (((tu->even_distribution == 1) ||
  704. ((tu->even_distribution_BF == 0) &&
  705. (tu->even_distribution_legacy == 0))) &&
  706. tu->n_err_fp >= 0 && tu->n_n_err_fp >= 0 &&
  707. compare_result_2 &&
  708. (compare_result_1 || (tu->min_hblank_violated == 1)) &&
  709. (tu->new_valid_boundary_link - 1) > 0 &&
  710. compare_result_3 &&
  711. (tu->delay_start_link_tmp <= 1023)) {
  712. tu->upper_boundary_count = tu->i_upper_boundary_count;
  713. tu->lower_boundary_count = tu->i_lower_boundary_count;
  714. tu->err_fp = tu->n_n_err_fp;
  715. tu->boundary_moderation_en = true;
  716. tu->tu_size_desired = tu->tu_size;
  717. tu->valid_boundary_link = tu->new_valid_boundary_link;
  718. tu->effective_valid_recorded_fp = tu->effective_valid_fp;
  719. tu->even_distribution_BF = 1;
  720. tu->delay_start_link = tu->delay_start_link_tmp;
  721. } else if (tu->boundary_mod_lower_err == 0) {
  722. compare_result_1 = _tu_param_compare(tu->n_n_err_fp,
  723. tu->diff_abs_fp);
  724. if (compare_result_1 == 2)
  725. tu->boundary_mod_lower_err = 1;
  726. }
  727. }
  728. static void _dp_ctrl_calc_tu(struct msm_dp_ctrl_private *ctrl,
  729. struct msm_dp_tu_calc_input *in,
  730. struct msm_dp_vc_tu_mapping_table *tu_table)
  731. {
  732. struct tu_algo_data *tu;
  733. int compare_result_1, compare_result_2;
  734. u64 temp = 0;
  735. s64 temp_fp = 0, temp1_fp = 0, temp2_fp = 0;
  736. s64 LCLK_FAST_SKEW_fp = drm_fixp_from_fraction(6, 10000); /* 0.0006 */
  737. s64 const_p49_fp = drm_fixp_from_fraction(49, 100); /* 0.49 */
  738. s64 const_p56_fp = drm_fixp_from_fraction(56, 100); /* 0.56 */
  739. s64 RATIO_SCALE_fp = drm_fixp_from_fraction(1001, 1000);
  740. u8 DP_BRUTE_FORCE = 1;
  741. s64 BRUTE_FORCE_THRESHOLD_fp = drm_fixp_from_fraction(1, 10); /* 0.1 */
  742. uint EXTRA_PIXCLK_CYCLE_DELAY = 4;
  743. uint HBLANK_MARGIN = 4;
  744. tu = kzalloc_obj(*tu);
  745. if (!tu)
  746. return;
  747. msm_dp_panel_update_tu_timings(in, tu);
  748. tu->err_fp = drm_fixp_from_fraction(1000, 1); /* 1000 */
  749. temp1_fp = drm_fixp_from_fraction(4, 1);
  750. temp2_fp = drm_fixp_mul(temp1_fp, tu->lclk_fp);
  751. temp_fp = drm_fixp_div(temp2_fp, tu->pclk_fp);
  752. tu->extra_buffer_margin = drm_fixp2int_ceil(temp_fp);
  753. temp1_fp = drm_fixp_from_fraction(tu->bpp, 8);
  754. temp2_fp = drm_fixp_mul(tu->pclk_fp, temp1_fp);
  755. temp1_fp = drm_fixp_from_fraction(tu->nlanes, 1);
  756. temp2_fp = drm_fixp_div(temp2_fp, temp1_fp);
  757. tu->ratio_fp = drm_fixp_div(temp2_fp, tu->lclk_fp);
  758. tu->original_ratio_fp = tu->ratio_fp;
  759. tu->boundary_moderation_en = false;
  760. tu->upper_boundary_count = 0;
  761. tu->lower_boundary_count = 0;
  762. tu->i_upper_boundary_count = 0;
  763. tu->i_lower_boundary_count = 0;
  764. tu->valid_lower_boundary_link = 0;
  765. tu->even_distribution_BF = 0;
  766. tu->even_distribution_legacy = 0;
  767. tu->even_distribution = 0;
  768. tu->delay_start_time_fp = 0;
  769. tu->err_fp = drm_fixp_from_fraction(1000, 1);
  770. tu->n_err_fp = 0;
  771. tu->n_n_err_fp = 0;
  772. tu->ratio = drm_fixp2int(tu->ratio_fp);
  773. temp1_fp = drm_fixp_from_fraction(tu->nlanes, 1);
  774. div64_u64_rem(tu->lwidth_fp, temp1_fp, &temp2_fp);
  775. if (temp2_fp != 0 &&
  776. !tu->ratio && tu->dsc_en == 0) {
  777. tu->ratio_fp = drm_fixp_mul(tu->ratio_fp, RATIO_SCALE_fp);
  778. tu->ratio = drm_fixp2int(tu->ratio_fp);
  779. if (tu->ratio)
  780. tu->ratio_fp = drm_fixp_from_fraction(1, 1);
  781. }
  782. if (tu->ratio > 1)
  783. tu->ratio = 1;
  784. if (tu->ratio == 1)
  785. goto tu_size_calc;
  786. compare_result_1 = _tu_param_compare(tu->ratio_fp, const_p49_fp);
  787. if (!compare_result_1 || compare_result_1 == 1)
  788. compare_result_1 = 1;
  789. else
  790. compare_result_1 = 0;
  791. compare_result_2 = _tu_param_compare(tu->ratio_fp, const_p56_fp);
  792. if (!compare_result_2 || compare_result_2 == 2)
  793. compare_result_2 = 1;
  794. else
  795. compare_result_2 = 0;
  796. if (tu->dsc_en && compare_result_1 && compare_result_2) {
  797. HBLANK_MARGIN += 4;
  798. drm_dbg_dp(ctrl->drm_dev,
  799. "increase HBLANK_MARGIN to %d\n", HBLANK_MARGIN);
  800. }
  801. tu_size_calc:
  802. for (tu->tu_size = 32; tu->tu_size <= 64; tu->tu_size++) {
  803. temp1_fp = drm_fixp_from_fraction(tu->tu_size, 1);
  804. temp2_fp = drm_fixp_mul(tu->ratio_fp, temp1_fp);
  805. temp = drm_fixp2int_ceil(temp2_fp);
  806. temp1_fp = drm_fixp_from_fraction(temp, 1);
  807. tu->n_err_fp = temp1_fp - temp2_fp;
  808. if (tu->n_err_fp < tu->err_fp) {
  809. tu->err_fp = tu->n_err_fp;
  810. tu->tu_size_desired = tu->tu_size;
  811. }
  812. }
  813. tu->tu_size_minus1 = tu->tu_size_desired - 1;
  814. temp1_fp = drm_fixp_from_fraction(tu->tu_size_desired, 1);
  815. temp2_fp = drm_fixp_mul(tu->ratio_fp, temp1_fp);
  816. tu->valid_boundary_link = drm_fixp2int_ceil(temp2_fp);
  817. temp1_fp = drm_fixp_from_fraction(tu->bpp, 8);
  818. temp2_fp = tu->lwidth_fp;
  819. temp2_fp = drm_fixp_mul(temp2_fp, temp1_fp);
  820. temp1_fp = drm_fixp_from_fraction(tu->valid_boundary_link, 1);
  821. temp2_fp = drm_fixp_div(temp2_fp, temp1_fp);
  822. tu->n_tus = drm_fixp2int(temp2_fp);
  823. if ((temp2_fp & 0xFFFFFFFF) > 0xFFFFF000)
  824. tu->n_tus += 1;
  825. tu->even_distribution_legacy = tu->n_tus % tu->nlanes == 0 ? 1 : 0;
  826. drm_dbg_dp(ctrl->drm_dev,
  827. "n_sym = %d, num_of_tus = %d\n",
  828. tu->valid_boundary_link, tu->n_tus);
  829. temp1_fp = drm_fixp_from_fraction(tu->tu_size_desired, 1);
  830. temp2_fp = drm_fixp_mul(tu->original_ratio_fp, temp1_fp);
  831. temp1_fp = drm_fixp_from_fraction(tu->valid_boundary_link, 1);
  832. temp2_fp = temp1_fp - temp2_fp;
  833. temp1_fp = drm_fixp_from_fraction(tu->n_tus + 1, 1);
  834. temp2_fp = drm_fixp_mul(temp1_fp, temp2_fp);
  835. temp = drm_fixp2int(temp2_fp);
  836. if (temp && temp2_fp)
  837. tu->extra_bytes = drm_fixp2int_ceil(temp2_fp);
  838. else
  839. tu->extra_bytes = 0;
  840. temp1_fp = drm_fixp_from_fraction(tu->extra_bytes, 1);
  841. temp2_fp = drm_fixp_from_fraction(8, tu->bpp);
  842. temp1_fp = drm_fixp_mul(temp1_fp, temp2_fp);
  843. if (temp && temp1_fp)
  844. tu->extra_pclk_cycles = drm_fixp2int_ceil(temp1_fp);
  845. else
  846. tu->extra_pclk_cycles = drm_fixp2int(temp1_fp);
  847. temp1_fp = drm_fixp_div(tu->lclk_fp, tu->pclk_fp);
  848. temp2_fp = drm_fixp_from_fraction(tu->extra_pclk_cycles, 1);
  849. temp1_fp = drm_fixp_mul(temp2_fp, temp1_fp);
  850. if (temp1_fp)
  851. tu->extra_pclk_cycles_in_link_clk = drm_fixp2int_ceil(temp1_fp);
  852. else
  853. tu->extra_pclk_cycles_in_link_clk = drm_fixp2int(temp1_fp);
  854. tu->filler_size = tu->tu_size_desired - tu->valid_boundary_link;
  855. temp1_fp = drm_fixp_from_fraction(tu->tu_size_desired, 1);
  856. tu->ratio_by_tu_fp = drm_fixp_mul(tu->ratio_fp, temp1_fp);
  857. tu->delay_start_link = tu->extra_pclk_cycles_in_link_clk +
  858. tu->filler_size + tu->extra_buffer_margin;
  859. tu->resulting_valid_fp =
  860. drm_fixp_from_fraction(tu->valid_boundary_link, 1);
  861. temp1_fp = drm_fixp_from_fraction(tu->tu_size_desired, 1);
  862. temp2_fp = drm_fixp_div(tu->resulting_valid_fp, temp1_fp);
  863. tu->TU_ratio_err_fp = temp2_fp - tu->original_ratio_fp;
  864. temp1_fp = drm_fixp_from_fraction(HBLANK_MARGIN, 1);
  865. temp1_fp = tu->hbp_relative_to_pclk_fp - temp1_fp;
  866. tu->hbp_time_fp = drm_fixp_div(temp1_fp, tu->pclk_fp);
  867. temp1_fp = drm_fixp_from_fraction(tu->delay_start_link, 1);
  868. tu->delay_start_time_fp = drm_fixp_div(temp1_fp, tu->lclk_fp);
  869. compare_result_1 = _tu_param_compare(tu->hbp_time_fp,
  870. tu->delay_start_time_fp);
  871. if (compare_result_1 == 2) /* if (hbp_time_fp < delay_start_time_fp) */
  872. tu->min_hblank_violated = 1;
  873. tu->hactive_time_fp = drm_fixp_div(tu->lwidth_fp, tu->pclk_fp);
  874. compare_result_2 = _tu_param_compare(tu->hactive_time_fp,
  875. tu->delay_start_time_fp);
  876. if (compare_result_2 == 2)
  877. tu->min_hblank_violated = 1;
  878. tu->delay_start_time_fp = 0;
  879. /* brute force */
  880. tu->delay_start_link_extra_pixclk = EXTRA_PIXCLK_CYCLE_DELAY;
  881. tu->diff_abs_fp = tu->resulting_valid_fp - tu->ratio_by_tu_fp;
  882. temp = drm_fixp2int(tu->diff_abs_fp);
  883. if (!temp && tu->diff_abs_fp <= 0xffff)
  884. tu->diff_abs_fp = 0;
  885. /* if(diff_abs < 0) diff_abs *= -1 */
  886. if (tu->diff_abs_fp < 0)
  887. tu->diff_abs_fp = drm_fixp_mul(tu->diff_abs_fp, -1);
  888. tu->boundary_mod_lower_err = 0;
  889. if ((tu->diff_abs_fp != 0 &&
  890. ((tu->diff_abs_fp > BRUTE_FORCE_THRESHOLD_fp) ||
  891. (tu->even_distribution_legacy == 0) ||
  892. (DP_BRUTE_FORCE == 1))) ||
  893. (tu->min_hblank_violated == 1)) {
  894. do {
  895. tu->err_fp = drm_fixp_from_fraction(1000, 1);
  896. temp1_fp = drm_fixp_div(tu->lclk_fp, tu->pclk_fp);
  897. temp2_fp = drm_fixp_from_fraction(
  898. tu->delay_start_link_extra_pixclk, 1);
  899. temp1_fp = drm_fixp_mul(temp2_fp, temp1_fp);
  900. if (temp1_fp)
  901. tu->extra_buffer_margin =
  902. drm_fixp2int_ceil(temp1_fp);
  903. else
  904. tu->extra_buffer_margin = 0;
  905. temp1_fp = drm_fixp_from_fraction(tu->bpp, 8);
  906. temp1_fp = drm_fixp_mul(tu->lwidth_fp, temp1_fp);
  907. if (temp1_fp)
  908. tu->n_symbols = drm_fixp2int_ceil(temp1_fp);
  909. else
  910. tu->n_symbols = 0;
  911. for (tu->tu_size = 32; tu->tu_size <= 64; tu->tu_size++) {
  912. for (tu->i_upper_boundary_count = 1;
  913. tu->i_upper_boundary_count <= 15;
  914. tu->i_upper_boundary_count++) {
  915. for (tu->i_lower_boundary_count = 1;
  916. tu->i_lower_boundary_count <= 15;
  917. tu->i_lower_boundary_count++) {
  918. _tu_valid_boundary_calc(tu);
  919. }
  920. }
  921. }
  922. tu->delay_start_link_extra_pixclk--;
  923. } while (tu->boundary_moderation_en != true &&
  924. tu->boundary_mod_lower_err == 1 &&
  925. tu->delay_start_link_extra_pixclk != 0);
  926. if (tu->boundary_moderation_en == true) {
  927. temp1_fp = drm_fixp_from_fraction(
  928. (tu->upper_boundary_count *
  929. tu->valid_boundary_link +
  930. tu->lower_boundary_count *
  931. (tu->valid_boundary_link - 1)), 1);
  932. temp2_fp = drm_fixp_from_fraction(
  933. (tu->upper_boundary_count +
  934. tu->lower_boundary_count), 1);
  935. tu->resulting_valid_fp =
  936. drm_fixp_div(temp1_fp, temp2_fp);
  937. temp1_fp = drm_fixp_from_fraction(
  938. tu->tu_size_desired, 1);
  939. tu->ratio_by_tu_fp =
  940. drm_fixp_mul(tu->original_ratio_fp, temp1_fp);
  941. tu->valid_lower_boundary_link =
  942. tu->valid_boundary_link - 1;
  943. temp1_fp = drm_fixp_from_fraction(tu->bpp, 8);
  944. temp1_fp = drm_fixp_mul(tu->lwidth_fp, temp1_fp);
  945. temp2_fp = drm_fixp_div(temp1_fp,
  946. tu->resulting_valid_fp);
  947. tu->n_tus = drm_fixp2int(temp2_fp);
  948. tu->tu_size_minus1 = tu->tu_size_desired - 1;
  949. tu->even_distribution_BF = 1;
  950. temp1_fp =
  951. drm_fixp_from_fraction(tu->tu_size_desired, 1);
  952. temp2_fp =
  953. drm_fixp_div(tu->resulting_valid_fp, temp1_fp);
  954. tu->TU_ratio_err_fp = temp2_fp - tu->original_ratio_fp;
  955. }
  956. }
  957. temp2_fp = drm_fixp_mul(LCLK_FAST_SKEW_fp, tu->lwidth_fp);
  958. if (temp2_fp)
  959. temp = drm_fixp2int_ceil(temp2_fp);
  960. else
  961. temp = 0;
  962. temp1_fp = drm_fixp_from_fraction(tu->nlanes, 1);
  963. temp2_fp = drm_fixp_mul(tu->original_ratio_fp, temp1_fp);
  964. temp1_fp = drm_fixp_from_fraction(tu->bpp, 8);
  965. temp2_fp = drm_fixp_div(temp1_fp, temp2_fp);
  966. temp1_fp = drm_fixp_from_fraction(temp, 1);
  967. temp2_fp = drm_fixp_mul(temp1_fp, temp2_fp);
  968. temp = drm_fixp2int(temp2_fp);
  969. if (tu->async_en)
  970. tu->delay_start_link += (int)temp;
  971. temp1_fp = drm_fixp_from_fraction(tu->delay_start_link, 1);
  972. tu->delay_start_time_fp = drm_fixp_div(temp1_fp, tu->lclk_fp);
  973. /* OUTPUTS */
  974. tu_table->valid_boundary_link = tu->valid_boundary_link;
  975. tu_table->delay_start_link = tu->delay_start_link;
  976. tu_table->boundary_moderation_en = tu->boundary_moderation_en;
  977. tu_table->valid_lower_boundary_link = tu->valid_lower_boundary_link;
  978. tu_table->upper_boundary_count = tu->upper_boundary_count;
  979. tu_table->lower_boundary_count = tu->lower_boundary_count;
  980. tu_table->tu_size_minus1 = tu->tu_size_minus1;
  981. drm_dbg_dp(ctrl->drm_dev, "TU: valid_boundary_link: %d\n",
  982. tu_table->valid_boundary_link);
  983. drm_dbg_dp(ctrl->drm_dev, "TU: delay_start_link: %d\n",
  984. tu_table->delay_start_link);
  985. drm_dbg_dp(ctrl->drm_dev, "TU: boundary_moderation_en: %d\n",
  986. tu_table->boundary_moderation_en);
  987. drm_dbg_dp(ctrl->drm_dev, "TU: valid_lower_boundary_link: %d\n",
  988. tu_table->valid_lower_boundary_link);
  989. drm_dbg_dp(ctrl->drm_dev, "TU: upper_boundary_count: %d\n",
  990. tu_table->upper_boundary_count);
  991. drm_dbg_dp(ctrl->drm_dev, "TU: lower_boundary_count: %d\n",
  992. tu_table->lower_boundary_count);
  993. drm_dbg_dp(ctrl->drm_dev, "TU: tu_size_minus1: %d\n",
  994. tu_table->tu_size_minus1);
  995. kfree(tu);
  996. }
  997. static void msm_dp_ctrl_calc_tu_parameters(struct msm_dp_ctrl_private *ctrl,
  998. struct msm_dp_vc_tu_mapping_table *tu_table)
  999. {
  1000. struct msm_dp_tu_calc_input in;
  1001. struct drm_display_mode *drm_mode;
  1002. drm_mode = &ctrl->panel->msm_dp_mode.drm_mode;
  1003. in.lclk = ctrl->link->link_params.rate / 1000;
  1004. in.pclk_khz = drm_mode->clock;
  1005. in.hactive = drm_mode->hdisplay;
  1006. in.hporch = drm_mode->htotal - drm_mode->hdisplay;
  1007. in.nlanes = ctrl->link->link_params.num_lanes;
  1008. in.bpp = ctrl->panel->msm_dp_mode.bpp;
  1009. in.pixel_enc = ctrl->panel->msm_dp_mode.out_fmt_is_yuv_420 ? 420 : 444;
  1010. in.dsc_en = 0;
  1011. in.async_en = 0;
  1012. in.fec_en = 0;
  1013. in.num_of_dsc_slices = 0;
  1014. in.compress_ratio = 100;
  1015. _dp_ctrl_calc_tu(ctrl, &in, tu_table);
  1016. }
  1017. static void msm_dp_ctrl_setup_tr_unit(struct msm_dp_ctrl_private *ctrl)
  1018. {
  1019. u32 msm_dp_tu = 0x0;
  1020. u32 valid_boundary = 0x0;
  1021. u32 valid_boundary2 = 0x0;
  1022. struct msm_dp_vc_tu_mapping_table tu_calc_table;
  1023. msm_dp_ctrl_calc_tu_parameters(ctrl, &tu_calc_table);
  1024. msm_dp_tu |= tu_calc_table.tu_size_minus1;
  1025. valid_boundary |= tu_calc_table.valid_boundary_link;
  1026. valid_boundary |= (tu_calc_table.delay_start_link << 16);
  1027. valid_boundary2 |= (tu_calc_table.valid_lower_boundary_link << 1);
  1028. valid_boundary2 |= (tu_calc_table.upper_boundary_count << 16);
  1029. valid_boundary2 |= (tu_calc_table.lower_boundary_count << 20);
  1030. if (tu_calc_table.boundary_moderation_en)
  1031. valid_boundary2 |= BIT(0);
  1032. pr_debug("dp_tu=0x%x, valid_boundary=0x%x, valid_boundary2=0x%x\n",
  1033. msm_dp_tu, valid_boundary, valid_boundary2);
  1034. msm_dp_write_link(ctrl, REG_DP_VALID_BOUNDARY, valid_boundary);
  1035. msm_dp_write_link(ctrl, REG_DP_TU, msm_dp_tu);
  1036. msm_dp_write_link(ctrl, REG_DP_VALID_BOUNDARY_2, valid_boundary2);
  1037. }
  1038. static int msm_dp_ctrl_wait4video_ready(struct msm_dp_ctrl_private *ctrl)
  1039. {
  1040. int ret = 0;
  1041. if (!wait_for_completion_timeout(&ctrl->video_comp,
  1042. WAIT_FOR_VIDEO_READY_TIMEOUT_JIFFIES)) {
  1043. DRM_ERROR("wait4video timedout\n");
  1044. ret = -ETIMEDOUT;
  1045. }
  1046. return ret;
  1047. }
  1048. static int msm_dp_ctrl_set_vx_px(struct msm_dp_ctrl_private *ctrl,
  1049. u8 v_level, u8 p_level)
  1050. {
  1051. union phy_configure_opts *phy_opts = &ctrl->phy_opts;
  1052. /* TODO: Update for all lanes instead of just first one */
  1053. phy_opts->dp.voltage[0] = v_level;
  1054. phy_opts->dp.pre[0] = p_level;
  1055. phy_opts->dp.set_voltages = 1;
  1056. phy_configure(ctrl->phy, phy_opts);
  1057. phy_opts->dp.set_voltages = 0;
  1058. return 0;
  1059. }
  1060. static int msm_dp_ctrl_update_phy_vx_px(struct msm_dp_ctrl_private *ctrl,
  1061. enum drm_dp_phy dp_phy)
  1062. {
  1063. struct msm_dp_link *link = ctrl->link;
  1064. int lane, lane_cnt, reg;
  1065. int ret = 0;
  1066. u8 buf[4];
  1067. u32 max_level_reached = 0;
  1068. u32 voltage_swing_level = link->phy_params.v_level;
  1069. u32 pre_emphasis_level = link->phy_params.p_level;
  1070. drm_dbg_dp(ctrl->drm_dev,
  1071. "voltage level: %d emphasis level: %d\n",
  1072. voltage_swing_level, pre_emphasis_level);
  1073. ret = msm_dp_ctrl_set_vx_px(ctrl,
  1074. voltage_swing_level, pre_emphasis_level);
  1075. if (ret)
  1076. return ret;
  1077. if (voltage_swing_level >= DP_TRAIN_LEVEL_MAX) {
  1078. drm_dbg_dp(ctrl->drm_dev,
  1079. "max. voltage swing level reached %d\n",
  1080. voltage_swing_level);
  1081. max_level_reached |= DP_TRAIN_MAX_SWING_REACHED;
  1082. }
  1083. if (pre_emphasis_level >= DP_TRAIN_LEVEL_MAX) {
  1084. drm_dbg_dp(ctrl->drm_dev,
  1085. "max. pre-emphasis level reached %d\n",
  1086. pre_emphasis_level);
  1087. max_level_reached |= DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
  1088. }
  1089. pre_emphasis_level <<= DP_TRAIN_PRE_EMPHASIS_SHIFT;
  1090. lane_cnt = ctrl->link->link_params.num_lanes;
  1091. for (lane = 0; lane < lane_cnt; lane++)
  1092. buf[lane] = voltage_swing_level | pre_emphasis_level
  1093. | max_level_reached;
  1094. drm_dbg_dp(ctrl->drm_dev, "sink: p|v=0x%x\n",
  1095. voltage_swing_level | pre_emphasis_level);
  1096. if (dp_phy == DP_PHY_DPRX)
  1097. reg = DP_TRAINING_LANE0_SET;
  1098. else
  1099. reg = DP_TRAINING_LANE0_SET_PHY_REPEATER(dp_phy);
  1100. ret = drm_dp_dpcd_write(ctrl->aux, reg, buf, lane_cnt);
  1101. if (ret == lane_cnt)
  1102. ret = 0;
  1103. return ret;
  1104. }
  1105. static bool msm_dp_ctrl_train_pattern_set(struct msm_dp_ctrl_private *ctrl,
  1106. u8 pattern, enum drm_dp_phy dp_phy)
  1107. {
  1108. u8 buf;
  1109. int reg;
  1110. int ret = 0;
  1111. drm_dbg_dp(ctrl->drm_dev, "sink: pattern=%x\n", pattern);
  1112. buf = pattern;
  1113. if (pattern && pattern != DP_TRAINING_PATTERN_4)
  1114. buf |= DP_LINK_SCRAMBLING_DISABLE;
  1115. if (dp_phy == DP_PHY_DPRX)
  1116. reg = DP_TRAINING_PATTERN_SET;
  1117. else
  1118. reg = DP_TRAINING_PATTERN_SET_PHY_REPEATER(dp_phy);
  1119. ret = drm_dp_dpcd_writeb(ctrl->aux, reg, buf);
  1120. return ret == 1;
  1121. }
  1122. static int msm_dp_ctrl_set_pattern_state_bit(struct msm_dp_ctrl_private *ctrl,
  1123. u32 state_bit)
  1124. {
  1125. int bit, ret;
  1126. u32 data;
  1127. bit = BIT(state_bit - 1);
  1128. drm_dbg_dp(ctrl->drm_dev, "hw: bit=%d train=%d\n", bit, state_bit);
  1129. msm_dp_write_link(ctrl, REG_DP_STATE_CTRL, bit);
  1130. bit = BIT(state_bit - 1) << DP_MAINLINK_READY_LINK_TRAINING_SHIFT;
  1131. /* Poll for mainlink ready status */
  1132. ret = readx_poll_timeout(readl, ctrl->link_base + REG_DP_MAINLINK_READY,
  1133. data, data & bit,
  1134. POLLING_SLEEP_US, POLLING_TIMEOUT_US);
  1135. if (ret < 0) {
  1136. DRM_ERROR("set state_bit for link_train=%d failed\n", state_bit);
  1137. return ret;
  1138. }
  1139. return 0;
  1140. }
  1141. static int msm_dp_ctrl_link_train_1(struct msm_dp_ctrl_private *ctrl,
  1142. int *training_step, enum drm_dp_phy dp_phy)
  1143. {
  1144. int delay_us;
  1145. int tries, old_v_level, ret = 0;
  1146. u8 link_status[DP_LINK_STATUS_SIZE];
  1147. int const maximum_retries = 4;
  1148. delay_us = drm_dp_read_clock_recovery_delay(ctrl->aux,
  1149. ctrl->panel->dpcd, dp_phy, false);
  1150. msm_dp_write_link(ctrl, REG_DP_STATE_CTRL, 0);
  1151. *training_step = DP_TRAINING_1;
  1152. ret = msm_dp_ctrl_set_pattern_state_bit(ctrl, 1);
  1153. if (ret)
  1154. return ret;
  1155. msm_dp_ctrl_train_pattern_set(ctrl, DP_TRAINING_PATTERN_1 |
  1156. DP_LINK_SCRAMBLING_DISABLE, dp_phy);
  1157. msm_dp_link_reset_phy_params_vx_px(ctrl->link);
  1158. ret = msm_dp_ctrl_update_phy_vx_px(ctrl, dp_phy);
  1159. if (ret)
  1160. return ret;
  1161. tries = 0;
  1162. old_v_level = ctrl->link->phy_params.v_level;
  1163. for (tries = 0; tries < maximum_retries; tries++) {
  1164. fsleep(delay_us);
  1165. ret = drm_dp_dpcd_read_phy_link_status(ctrl->aux, dp_phy, link_status);
  1166. if (ret)
  1167. return ret;
  1168. if (drm_dp_clock_recovery_ok(link_status,
  1169. ctrl->link->link_params.num_lanes)) {
  1170. return 0;
  1171. }
  1172. if (ctrl->link->phy_params.v_level >=
  1173. DP_TRAIN_LEVEL_MAX) {
  1174. DRM_ERROR_RATELIMITED("max v_level reached\n");
  1175. return -EAGAIN;
  1176. }
  1177. if (old_v_level != ctrl->link->phy_params.v_level) {
  1178. tries = 0;
  1179. old_v_level = ctrl->link->phy_params.v_level;
  1180. }
  1181. msm_dp_link_adjust_levels(ctrl->link, link_status);
  1182. ret = msm_dp_ctrl_update_phy_vx_px(ctrl, dp_phy);
  1183. if (ret)
  1184. return ret;
  1185. }
  1186. DRM_ERROR("max tries reached\n");
  1187. return -ETIMEDOUT;
  1188. }
  1189. static int msm_dp_ctrl_link_rate_down_shift(struct msm_dp_ctrl_private *ctrl)
  1190. {
  1191. int ret = 0;
  1192. struct msm_dp_link_info *link_params = &ctrl->link->link_params;
  1193. if (link_params->rate_set) {
  1194. --link_params->rate_set;
  1195. link_params->rate = link_params->supported_rates[link_params->rate_set];
  1196. } else {
  1197. switch (link_params->rate) {
  1198. case 810000:
  1199. link_params->rate = 540000;
  1200. break;
  1201. case 540000:
  1202. link_params->rate = 270000;
  1203. break;
  1204. case 270000:
  1205. link_params->rate = 162000;
  1206. break;
  1207. case 162000:
  1208. default:
  1209. ret = -EINVAL;
  1210. break;
  1211. }
  1212. }
  1213. if (!ret) {
  1214. drm_dbg_dp(ctrl->drm_dev, "new rate=0x%x\n",
  1215. link_params->rate);
  1216. }
  1217. return ret;
  1218. }
  1219. static int msm_dp_ctrl_link_lane_down_shift(struct msm_dp_ctrl_private *ctrl)
  1220. {
  1221. if (ctrl->link->link_params.num_lanes == 1)
  1222. return -1;
  1223. ctrl->link->link_params.num_lanes /= 2;
  1224. ctrl->link->link_params.rate = ctrl->panel->link_info.rate;
  1225. ctrl->link->phy_params.p_level = 0;
  1226. ctrl->link->phy_params.v_level = 0;
  1227. return 0;
  1228. }
  1229. static void msm_dp_ctrl_clear_training_pattern(struct msm_dp_ctrl_private *ctrl,
  1230. enum drm_dp_phy dp_phy)
  1231. {
  1232. int delay_us;
  1233. msm_dp_ctrl_train_pattern_set(ctrl, DP_TRAINING_PATTERN_DISABLE, dp_phy);
  1234. delay_us = drm_dp_read_channel_eq_delay(ctrl->aux,
  1235. ctrl->panel->dpcd, dp_phy, false);
  1236. fsleep(delay_us);
  1237. }
  1238. static int msm_dp_ctrl_link_train_2(struct msm_dp_ctrl_private *ctrl,
  1239. int *training_step, enum drm_dp_phy dp_phy)
  1240. {
  1241. int delay_us;
  1242. int tries = 0, ret = 0;
  1243. u8 pattern;
  1244. u32 state_ctrl_bit;
  1245. int const maximum_retries = 5;
  1246. u8 link_status[DP_LINK_STATUS_SIZE];
  1247. delay_us = drm_dp_read_channel_eq_delay(ctrl->aux,
  1248. ctrl->panel->dpcd, dp_phy, false);
  1249. msm_dp_write_link(ctrl, REG_DP_STATE_CTRL, 0);
  1250. *training_step = DP_TRAINING_2;
  1251. if (drm_dp_tps4_supported(ctrl->panel->dpcd)) {
  1252. pattern = DP_TRAINING_PATTERN_4;
  1253. state_ctrl_bit = 4;
  1254. } else if (drm_dp_tps3_supported(ctrl->panel->dpcd)) {
  1255. pattern = DP_TRAINING_PATTERN_3;
  1256. state_ctrl_bit = 3;
  1257. } else {
  1258. pattern = DP_TRAINING_PATTERN_2;
  1259. state_ctrl_bit = 2;
  1260. }
  1261. ret = msm_dp_ctrl_set_pattern_state_bit(ctrl, state_ctrl_bit);
  1262. if (ret)
  1263. return ret;
  1264. msm_dp_ctrl_train_pattern_set(ctrl, pattern, dp_phy);
  1265. for (tries = 0; tries <= maximum_retries; tries++) {
  1266. fsleep(delay_us);
  1267. ret = drm_dp_dpcd_read_phy_link_status(ctrl->aux, dp_phy, link_status);
  1268. if (ret)
  1269. return ret;
  1270. if (drm_dp_channel_eq_ok(link_status,
  1271. ctrl->link->link_params.num_lanes)) {
  1272. return 0;
  1273. }
  1274. msm_dp_link_adjust_levels(ctrl->link, link_status);
  1275. ret = msm_dp_ctrl_update_phy_vx_px(ctrl, dp_phy);
  1276. if (ret)
  1277. return ret;
  1278. }
  1279. return -ETIMEDOUT;
  1280. }
  1281. static int msm_dp_ctrl_link_train_1_2(struct msm_dp_ctrl_private *ctrl,
  1282. int *training_step, enum drm_dp_phy dp_phy)
  1283. {
  1284. int ret;
  1285. ret = msm_dp_ctrl_link_train_1(ctrl, training_step, dp_phy);
  1286. if (ret) {
  1287. DRM_ERROR("link training #1 on phy %d failed. ret=%d\n", dp_phy, ret);
  1288. return ret;
  1289. }
  1290. drm_dbg_dp(ctrl->drm_dev, "link training #1 on phy %d successful\n", dp_phy);
  1291. ret = msm_dp_ctrl_link_train_2(ctrl, training_step, dp_phy);
  1292. if (ret) {
  1293. DRM_ERROR("link training #2 on phy %d failed. ret=%d\n", dp_phy, ret);
  1294. return ret;
  1295. }
  1296. drm_dbg_dp(ctrl->drm_dev, "link training #2 on phy %d successful\n", dp_phy);
  1297. return 0;
  1298. }
  1299. static int msm_dp_ctrl_link_train(struct msm_dp_ctrl_private *ctrl,
  1300. int *training_step)
  1301. {
  1302. int i;
  1303. int ret = 0;
  1304. const u8 *dpcd = ctrl->panel->dpcd;
  1305. u8 encoding[] = { 0, DP_SET_ANSI_8B10B };
  1306. u8 assr;
  1307. struct msm_dp_link_info link_info = {0};
  1308. msm_dp_ctrl_config_ctrl(ctrl);
  1309. link_info.num_lanes = ctrl->link->link_params.num_lanes;
  1310. link_info.rate = ctrl->link->link_params.rate;
  1311. link_info.capabilities = DP_LINK_CAP_ENHANCED_FRAMING;
  1312. msm_dp_aux_link_configure(ctrl->aux, &link_info);
  1313. if (drm_dp_max_downspread(dpcd))
  1314. encoding[0] |= DP_SPREAD_AMP_0_5;
  1315. /* config DOWNSPREAD_CTRL and MAIN_LINK_CHANNEL_CODING_SET */
  1316. drm_dp_dpcd_write(ctrl->aux, DP_DOWNSPREAD_CTRL, encoding, 2);
  1317. if (drm_dp_alternate_scrambler_reset_cap(dpcd)) {
  1318. assr = DP_ALTERNATE_SCRAMBLER_RESET_ENABLE;
  1319. drm_dp_dpcd_write(ctrl->aux, DP_EDP_CONFIGURATION_SET,
  1320. &assr, 1);
  1321. }
  1322. for (i = ctrl->link->lttpr_count - 1; i >= 0; i--) {
  1323. enum drm_dp_phy dp_phy = DP_PHY_LTTPR(i);
  1324. ret = msm_dp_ctrl_link_train_1_2(ctrl, training_step, dp_phy);
  1325. msm_dp_ctrl_clear_training_pattern(ctrl, dp_phy);
  1326. if (ret)
  1327. break;
  1328. }
  1329. if (ret) {
  1330. DRM_ERROR("link training of LTTPR(s) failed. ret=%d\n", ret);
  1331. goto end;
  1332. }
  1333. ret = msm_dp_ctrl_link_train_1_2(ctrl, training_step, DP_PHY_DPRX);
  1334. if (ret) {
  1335. DRM_ERROR("link training on sink failed. ret=%d\n", ret);
  1336. goto end;
  1337. }
  1338. end:
  1339. msm_dp_write_link(ctrl, REG_DP_STATE_CTRL, 0);
  1340. return ret;
  1341. }
  1342. static int msm_dp_ctrl_setup_main_link(struct msm_dp_ctrl_private *ctrl,
  1343. int *training_step)
  1344. {
  1345. int ret = 0;
  1346. msm_dp_ctrl_mainlink_enable(ctrl);
  1347. if (ctrl->link->sink_request & DP_TEST_LINK_PHY_TEST_PATTERN)
  1348. return ret;
  1349. /*
  1350. * As part of previous calls, DP controller state might have
  1351. * transitioned to PUSH_IDLE. In order to start transmitting
  1352. * a link training pattern, we have to first do soft reset.
  1353. */
  1354. ret = msm_dp_ctrl_link_train(ctrl, training_step);
  1355. return ret;
  1356. }
  1357. int msm_dp_ctrl_core_clk_enable(struct msm_dp_ctrl *msm_dp_ctrl)
  1358. {
  1359. struct msm_dp_ctrl_private *ctrl;
  1360. int ret = 0;
  1361. ctrl = container_of(msm_dp_ctrl, struct msm_dp_ctrl_private, msm_dp_ctrl);
  1362. if (ctrl->core_clks_on) {
  1363. drm_dbg_dp(ctrl->drm_dev, "core clks already enabled\n");
  1364. return 0;
  1365. }
  1366. ret = clk_bulk_prepare_enable(ctrl->num_core_clks, ctrl->core_clks);
  1367. if (ret)
  1368. return ret;
  1369. ctrl->core_clks_on = true;
  1370. drm_dbg_dp(ctrl->drm_dev, "enable core clocks \n");
  1371. drm_dbg_dp(ctrl->drm_dev, "stream_clks:%s link_clks:%s core_clks:%s\n",
  1372. str_on_off(ctrl->stream_clks_on),
  1373. str_on_off(ctrl->link_clks_on),
  1374. str_on_off(ctrl->core_clks_on));
  1375. return 0;
  1376. }
  1377. void msm_dp_ctrl_core_clk_disable(struct msm_dp_ctrl *msm_dp_ctrl)
  1378. {
  1379. struct msm_dp_ctrl_private *ctrl;
  1380. ctrl = container_of(msm_dp_ctrl, struct msm_dp_ctrl_private, msm_dp_ctrl);
  1381. clk_bulk_disable_unprepare(ctrl->num_core_clks, ctrl->core_clks);
  1382. ctrl->core_clks_on = false;
  1383. drm_dbg_dp(ctrl->drm_dev, "disable core clocks \n");
  1384. drm_dbg_dp(ctrl->drm_dev, "stream_clks:%s link_clks:%s core_clks:%s\n",
  1385. str_on_off(ctrl->stream_clks_on),
  1386. str_on_off(ctrl->link_clks_on),
  1387. str_on_off(ctrl->core_clks_on));
  1388. }
  1389. static int msm_dp_ctrl_link_clk_enable(struct msm_dp_ctrl *msm_dp_ctrl)
  1390. {
  1391. struct msm_dp_ctrl_private *ctrl;
  1392. int ret = 0;
  1393. ctrl = container_of(msm_dp_ctrl, struct msm_dp_ctrl_private, msm_dp_ctrl);
  1394. if (ctrl->link_clks_on) {
  1395. drm_dbg_dp(ctrl->drm_dev, "links clks already enabled\n");
  1396. return 0;
  1397. }
  1398. if (!ctrl->core_clks_on) {
  1399. drm_dbg_dp(ctrl->drm_dev, "Enable core clks before link clks\n");
  1400. msm_dp_ctrl_core_clk_enable(msm_dp_ctrl);
  1401. }
  1402. ret = clk_bulk_prepare_enable(ctrl->num_link_clks, ctrl->link_clks);
  1403. if (ret)
  1404. return ret;
  1405. ctrl->link_clks_on = true;
  1406. drm_dbg_dp(ctrl->drm_dev, "enable link clocks\n");
  1407. drm_dbg_dp(ctrl->drm_dev, "stream_clks:%s link_clks:%s core_clks:%s\n",
  1408. str_on_off(ctrl->stream_clks_on),
  1409. str_on_off(ctrl->link_clks_on),
  1410. str_on_off(ctrl->core_clks_on));
  1411. return 0;
  1412. }
  1413. static void msm_dp_ctrl_link_clk_disable(struct msm_dp_ctrl *msm_dp_ctrl)
  1414. {
  1415. struct msm_dp_ctrl_private *ctrl;
  1416. ctrl = container_of(msm_dp_ctrl, struct msm_dp_ctrl_private, msm_dp_ctrl);
  1417. clk_bulk_disable_unprepare(ctrl->num_link_clks, ctrl->link_clks);
  1418. ctrl->link_clks_on = false;
  1419. drm_dbg_dp(ctrl->drm_dev, "disabled link clocks\n");
  1420. drm_dbg_dp(ctrl->drm_dev, "stream_clks:%s link_clks:%s core_clks:%s\n",
  1421. str_on_off(ctrl->stream_clks_on),
  1422. str_on_off(ctrl->link_clks_on),
  1423. str_on_off(ctrl->core_clks_on));
  1424. }
  1425. static int msm_dp_ctrl_enable_mainlink_clocks(struct msm_dp_ctrl_private *ctrl)
  1426. {
  1427. int ret = 0;
  1428. struct phy *phy = ctrl->phy;
  1429. const u8 *dpcd = ctrl->panel->dpcd;
  1430. ctrl->phy_opts.dp.lanes = ctrl->link->link_params.num_lanes;
  1431. ctrl->phy_opts.dp.link_rate = ctrl->link->link_params.rate / 100;
  1432. ctrl->phy_opts.dp.ssc = drm_dp_max_downspread(dpcd);
  1433. phy_configure(phy, &ctrl->phy_opts);
  1434. phy_power_on(phy);
  1435. dev_pm_opp_set_rate(ctrl->dev, ctrl->link->link_params.rate * 1000);
  1436. ret = msm_dp_ctrl_link_clk_enable(&ctrl->msm_dp_ctrl);
  1437. if (ret)
  1438. DRM_ERROR("Unable to start link clocks. ret=%d\n", ret);
  1439. drm_dbg_dp(ctrl->drm_dev, "link rate=%d\n", ctrl->link->link_params.rate);
  1440. return ret;
  1441. }
  1442. static void msm_dp_ctrl_enable_sdp(struct msm_dp_ctrl_private *ctrl)
  1443. {
  1444. /* trigger sdp */
  1445. msm_dp_write_link(ctrl, MMSS_DP_SDP_CFG3, UPDATE_SDP);
  1446. msm_dp_write_link(ctrl, MMSS_DP_SDP_CFG3, 0x0);
  1447. }
  1448. static void msm_dp_ctrl_psr_enter(struct msm_dp_ctrl_private *ctrl)
  1449. {
  1450. u32 cmd;
  1451. cmd = msm_dp_read_link(ctrl, REG_PSR_CMD);
  1452. cmd &= ~(PSR_ENTER | PSR_EXIT);
  1453. cmd |= PSR_ENTER;
  1454. msm_dp_ctrl_enable_sdp(ctrl);
  1455. msm_dp_write_link(ctrl, REG_PSR_CMD, cmd);
  1456. }
  1457. static void msm_dp_ctrl_psr_exit(struct msm_dp_ctrl_private *ctrl)
  1458. {
  1459. u32 cmd;
  1460. cmd = msm_dp_read_link(ctrl, REG_PSR_CMD);
  1461. cmd &= ~(PSR_ENTER | PSR_EXIT);
  1462. cmd |= PSR_EXIT;
  1463. msm_dp_ctrl_enable_sdp(ctrl);
  1464. msm_dp_write_link(ctrl, REG_PSR_CMD, cmd);
  1465. }
  1466. void msm_dp_ctrl_config_psr(struct msm_dp_ctrl *msm_dp_ctrl)
  1467. {
  1468. struct msm_dp_ctrl_private *ctrl = container_of(msm_dp_ctrl,
  1469. struct msm_dp_ctrl_private, msm_dp_ctrl);
  1470. u32 cfg;
  1471. if (!ctrl->panel->psr_cap.version)
  1472. return;
  1473. /* enable PSR1 function */
  1474. cfg = msm_dp_read_link(ctrl, REG_PSR_CONFIG);
  1475. cfg |= PSR1_SUPPORTED;
  1476. msm_dp_write_link(ctrl, REG_PSR_CONFIG, cfg);
  1477. msm_dp_ctrl_config_psr_interrupt(ctrl);
  1478. msm_dp_ctrl_enable_sdp(ctrl);
  1479. cfg = DP_PSR_ENABLE;
  1480. drm_dp_dpcd_write(ctrl->aux, DP_PSR_EN_CFG, &cfg, 1);
  1481. }
  1482. void msm_dp_ctrl_set_psr(struct msm_dp_ctrl *msm_dp_ctrl, bool enter)
  1483. {
  1484. struct msm_dp_ctrl_private *ctrl = container_of(msm_dp_ctrl,
  1485. struct msm_dp_ctrl_private, msm_dp_ctrl);
  1486. if (!ctrl->panel->psr_cap.version)
  1487. return;
  1488. /*
  1489. * When entering PSR,
  1490. * 1. Send PSR enter SDP and wait for the PSR_UPDATE_INT
  1491. * 2. Turn off video
  1492. * 3. Disable the mainlink
  1493. *
  1494. * When exiting PSR,
  1495. * 1. Enable the mainlink
  1496. * 2. Send the PSR exit SDP
  1497. */
  1498. if (enter) {
  1499. reinit_completion(&ctrl->psr_op_comp);
  1500. msm_dp_ctrl_psr_enter(ctrl);
  1501. if (!wait_for_completion_timeout(&ctrl->psr_op_comp,
  1502. PSR_OPERATION_COMPLETION_TIMEOUT_JIFFIES)) {
  1503. DRM_ERROR("PSR_ENTRY timedout\n");
  1504. msm_dp_ctrl_psr_exit(ctrl);
  1505. return;
  1506. }
  1507. msm_dp_ctrl_push_idle(msm_dp_ctrl);
  1508. msm_dp_write_link(ctrl, REG_DP_STATE_CTRL, 0);
  1509. msm_dp_ctrl_psr_mainlink_disable(ctrl);
  1510. } else {
  1511. msm_dp_ctrl_psr_mainlink_enable(ctrl);
  1512. msm_dp_ctrl_psr_exit(ctrl);
  1513. msm_dp_write_link(ctrl, REG_DP_STATE_CTRL, DP_STATE_CTRL_SEND_VIDEO);
  1514. msm_dp_ctrl_wait4video_ready(ctrl);
  1515. msm_dp_write_link(ctrl, REG_DP_STATE_CTRL, 0);
  1516. }
  1517. }
  1518. static void msm_dp_ctrl_phy_reset(struct msm_dp_ctrl_private *ctrl)
  1519. {
  1520. msm_dp_write_ahb(ctrl, REG_DP_PHY_CTRL,
  1521. DP_PHY_CTRL_SW_RESET | DP_PHY_CTRL_SW_RESET_PLL);
  1522. usleep_range(1000, 1100); /* h/w recommended delay */
  1523. msm_dp_write_ahb(ctrl, REG_DP_PHY_CTRL, 0x0);
  1524. }
  1525. void msm_dp_ctrl_phy_init(struct msm_dp_ctrl *msm_dp_ctrl)
  1526. {
  1527. struct msm_dp_ctrl_private *ctrl;
  1528. struct phy *phy;
  1529. ctrl = container_of(msm_dp_ctrl, struct msm_dp_ctrl_private, msm_dp_ctrl);
  1530. phy = ctrl->phy;
  1531. msm_dp_ctrl_phy_reset(ctrl);
  1532. phy_init(phy);
  1533. drm_dbg_dp(ctrl->drm_dev, "phy=%p init=%d power_on=%d\n",
  1534. phy, phy->init_count, phy->power_count);
  1535. }
  1536. void msm_dp_ctrl_phy_exit(struct msm_dp_ctrl *msm_dp_ctrl)
  1537. {
  1538. struct msm_dp_ctrl_private *ctrl;
  1539. struct phy *phy;
  1540. ctrl = container_of(msm_dp_ctrl, struct msm_dp_ctrl_private, msm_dp_ctrl);
  1541. phy = ctrl->phy;
  1542. msm_dp_ctrl_phy_reset(ctrl);
  1543. phy_exit(phy);
  1544. drm_dbg_dp(ctrl->drm_dev, "phy=%p init=%d power_on=%d\n",
  1545. phy, phy->init_count, phy->power_count);
  1546. }
  1547. static int msm_dp_ctrl_reinitialize_mainlink(struct msm_dp_ctrl_private *ctrl)
  1548. {
  1549. struct phy *phy = ctrl->phy;
  1550. int ret = 0;
  1551. msm_dp_ctrl_mainlink_disable(ctrl);
  1552. ctrl->phy_opts.dp.lanes = ctrl->link->link_params.num_lanes;
  1553. phy_configure(phy, &ctrl->phy_opts);
  1554. /*
  1555. * Disable and re-enable the mainlink clock since the
  1556. * link clock might have been adjusted as part of the
  1557. * link maintenance.
  1558. */
  1559. dev_pm_opp_set_rate(ctrl->dev, 0);
  1560. msm_dp_ctrl_link_clk_disable(&ctrl->msm_dp_ctrl);
  1561. phy_power_off(phy);
  1562. /* hw recommended delay before re-enabling clocks */
  1563. msleep(20);
  1564. ret = msm_dp_ctrl_enable_mainlink_clocks(ctrl);
  1565. if (ret) {
  1566. DRM_ERROR("Failed to enable mainlink clks. ret=%d\n", ret);
  1567. return ret;
  1568. }
  1569. return ret;
  1570. }
  1571. static int msm_dp_ctrl_deinitialize_mainlink(struct msm_dp_ctrl_private *ctrl)
  1572. {
  1573. struct phy *phy;
  1574. phy = ctrl->phy;
  1575. msm_dp_ctrl_mainlink_disable(ctrl);
  1576. msm_dp_ctrl_reset(&ctrl->msm_dp_ctrl);
  1577. dev_pm_opp_set_rate(ctrl->dev, 0);
  1578. msm_dp_ctrl_link_clk_disable(&ctrl->msm_dp_ctrl);
  1579. phy_power_off(phy);
  1580. /* aux channel down, reinit phy */
  1581. phy_exit(phy);
  1582. phy_init(phy);
  1583. drm_dbg_dp(ctrl->drm_dev, "phy=%p init=%d power_on=%d\n",
  1584. phy, phy->init_count, phy->power_count);
  1585. return 0;
  1586. }
  1587. static int msm_dp_ctrl_link_maintenance(struct msm_dp_ctrl_private *ctrl)
  1588. {
  1589. int ret = 0;
  1590. int training_step = DP_TRAINING_NONE;
  1591. msm_dp_ctrl_push_idle(&ctrl->msm_dp_ctrl);
  1592. ctrl->link->phy_params.p_level = 0;
  1593. ctrl->link->phy_params.v_level = 0;
  1594. ret = msm_dp_ctrl_setup_main_link(ctrl, &training_step);
  1595. if (ret)
  1596. goto end;
  1597. msm_dp_ctrl_clear_training_pattern(ctrl, DP_PHY_DPRX);
  1598. msm_dp_write_link(ctrl, REG_DP_STATE_CTRL, DP_STATE_CTRL_SEND_VIDEO);
  1599. ret = msm_dp_ctrl_wait4video_ready(ctrl);
  1600. end:
  1601. return ret;
  1602. }
  1603. #define SCRAMBLER_RESET_COUNT_VALUE 0xFC
  1604. static void msm_dp_ctrl_send_phy_pattern(struct msm_dp_ctrl_private *ctrl,
  1605. u32 pattern)
  1606. {
  1607. u32 value = 0x0;
  1608. /* Make sure to clear the current pattern before starting a new one */
  1609. msm_dp_write_link(ctrl, REG_DP_STATE_CTRL, 0x0);
  1610. drm_dbg_dp(ctrl->drm_dev, "pattern: %#x\n", pattern);
  1611. switch (pattern) {
  1612. case DP_PHY_TEST_PATTERN_D10_2:
  1613. msm_dp_write_link(ctrl, REG_DP_STATE_CTRL,
  1614. DP_STATE_CTRL_LINK_TRAINING_PATTERN1);
  1615. break;
  1616. case DP_PHY_TEST_PATTERN_ERROR_COUNT:
  1617. value &= ~(1 << 16);
  1618. msm_dp_write_link(ctrl, REG_DP_HBR2_COMPLIANCE_SCRAMBLER_RESET,
  1619. value);
  1620. value |= SCRAMBLER_RESET_COUNT_VALUE;
  1621. msm_dp_write_link(ctrl, REG_DP_HBR2_COMPLIANCE_SCRAMBLER_RESET,
  1622. value);
  1623. msm_dp_write_link(ctrl, REG_DP_MAINLINK_LEVELS,
  1624. DP_MAINLINK_SAFE_TO_EXIT_LEVEL_2);
  1625. msm_dp_write_link(ctrl, REG_DP_STATE_CTRL,
  1626. DP_STATE_CTRL_LINK_SYMBOL_ERR_MEASURE);
  1627. break;
  1628. case DP_PHY_TEST_PATTERN_PRBS7:
  1629. msm_dp_write_link(ctrl, REG_DP_STATE_CTRL,
  1630. DP_STATE_CTRL_LINK_PRBS7);
  1631. break;
  1632. case DP_PHY_TEST_PATTERN_80BIT_CUSTOM:
  1633. msm_dp_write_link(ctrl, REG_DP_STATE_CTRL,
  1634. DP_STATE_CTRL_LINK_TEST_CUSTOM_PATTERN);
  1635. /* 00111110000011111000001111100000 */
  1636. msm_dp_write_link(ctrl, REG_DP_TEST_80BIT_CUSTOM_PATTERN_REG0,
  1637. 0x3E0F83E0);
  1638. /* 00001111100000111110000011111000 */
  1639. msm_dp_write_link(ctrl, REG_DP_TEST_80BIT_CUSTOM_PATTERN_REG1,
  1640. 0x0F83E0F8);
  1641. /* 1111100000111110 */
  1642. msm_dp_write_link(ctrl, REG_DP_TEST_80BIT_CUSTOM_PATTERN_REG2,
  1643. 0x0000F83E);
  1644. break;
  1645. case DP_PHY_TEST_PATTERN_CP2520:
  1646. value = msm_dp_read_link(ctrl, REG_DP_MAINLINK_CTRL);
  1647. value &= ~DP_MAINLINK_CTRL_SW_BYPASS_SCRAMBLER;
  1648. msm_dp_write_link(ctrl, REG_DP_MAINLINK_CTRL, value);
  1649. value = DP_HBR2_ERM_PATTERN;
  1650. msm_dp_write_link(ctrl, REG_DP_HBR2_COMPLIANCE_SCRAMBLER_RESET,
  1651. value);
  1652. value |= SCRAMBLER_RESET_COUNT_VALUE;
  1653. msm_dp_write_link(ctrl, REG_DP_HBR2_COMPLIANCE_SCRAMBLER_RESET,
  1654. value);
  1655. msm_dp_write_link(ctrl, REG_DP_MAINLINK_LEVELS,
  1656. DP_MAINLINK_SAFE_TO_EXIT_LEVEL_2);
  1657. msm_dp_write_link(ctrl, REG_DP_STATE_CTRL,
  1658. DP_STATE_CTRL_LINK_SYMBOL_ERR_MEASURE);
  1659. value = msm_dp_read_link(ctrl, REG_DP_MAINLINK_CTRL);
  1660. value |= DP_MAINLINK_CTRL_ENABLE;
  1661. msm_dp_write_link(ctrl, REG_DP_MAINLINK_CTRL, value);
  1662. break;
  1663. case DP_PHY_TEST_PATTERN_SEL_MASK:
  1664. msm_dp_write_link(ctrl, REG_DP_MAINLINK_CTRL,
  1665. DP_MAINLINK_CTRL_ENABLE);
  1666. msm_dp_write_link(ctrl, REG_DP_STATE_CTRL,
  1667. DP_STATE_CTRL_LINK_TRAINING_PATTERN4);
  1668. break;
  1669. default:
  1670. drm_dbg_dp(ctrl->drm_dev,
  1671. "No valid test pattern requested: %#x\n", pattern);
  1672. break;
  1673. }
  1674. }
  1675. static bool msm_dp_ctrl_send_phy_test_pattern(struct msm_dp_ctrl_private *ctrl)
  1676. {
  1677. bool success = false;
  1678. u32 pattern_sent = 0x0;
  1679. u32 pattern_requested = ctrl->link->phy_params.phy_test_pattern_sel;
  1680. drm_dbg_dp(ctrl->drm_dev, "request: 0x%x\n", pattern_requested);
  1681. if (msm_dp_ctrl_set_vx_px(ctrl,
  1682. ctrl->link->phy_params.v_level,
  1683. ctrl->link->phy_params.p_level)) {
  1684. DRM_ERROR("Failed to set v/p levels\n");
  1685. return false;
  1686. }
  1687. msm_dp_ctrl_send_phy_pattern(ctrl, pattern_requested);
  1688. msm_dp_ctrl_update_phy_vx_px(ctrl, DP_PHY_DPRX);
  1689. msm_dp_link_send_test_response(ctrl->link);
  1690. pattern_sent = msm_dp_read_link(ctrl, REG_DP_MAINLINK_READY);
  1691. switch (pattern_sent) {
  1692. case MR_LINK_TRAINING1:
  1693. success = (pattern_requested ==
  1694. DP_PHY_TEST_PATTERN_D10_2);
  1695. break;
  1696. case MR_LINK_SYMBOL_ERM:
  1697. success = ((pattern_requested ==
  1698. DP_PHY_TEST_PATTERN_ERROR_COUNT) ||
  1699. (pattern_requested ==
  1700. DP_PHY_TEST_PATTERN_CP2520));
  1701. break;
  1702. case MR_LINK_PRBS7:
  1703. success = (pattern_requested ==
  1704. DP_PHY_TEST_PATTERN_PRBS7);
  1705. break;
  1706. case MR_LINK_CUSTOM80:
  1707. success = (pattern_requested ==
  1708. DP_PHY_TEST_PATTERN_80BIT_CUSTOM);
  1709. break;
  1710. case MR_LINK_TRAINING4:
  1711. success = (pattern_requested ==
  1712. DP_PHY_TEST_PATTERN_SEL_MASK);
  1713. break;
  1714. default:
  1715. success = false;
  1716. }
  1717. drm_dbg_dp(ctrl->drm_dev, "%s: test->0x%x\n",
  1718. success ? "success" : "failed", pattern_requested);
  1719. return success;
  1720. }
  1721. static int msm_dp_ctrl_process_phy_test_request(struct msm_dp_ctrl_private *ctrl)
  1722. {
  1723. int ret;
  1724. unsigned long pixel_rate;
  1725. if (!ctrl->link->phy_params.phy_test_pattern_sel) {
  1726. drm_dbg_dp(ctrl->drm_dev,
  1727. "no test pattern selected by sink\n");
  1728. return 0;
  1729. }
  1730. /*
  1731. * The global reset will need DP link related clocks to be
  1732. * running. Add the global reset just before disabling the
  1733. * link clocks and core clocks.
  1734. */
  1735. msm_dp_ctrl_off(&ctrl->msm_dp_ctrl);
  1736. ret = msm_dp_ctrl_on_link(&ctrl->msm_dp_ctrl);
  1737. if (ret) {
  1738. DRM_ERROR("failed to enable DP link controller\n");
  1739. return ret;
  1740. }
  1741. pixel_rate = ctrl->panel->msm_dp_mode.drm_mode.clock;
  1742. ret = clk_set_rate(ctrl->pixel_clk, pixel_rate * 1000);
  1743. if (ret) {
  1744. DRM_ERROR("Failed to set pixel clock rate. ret=%d\n", ret);
  1745. return ret;
  1746. }
  1747. if (ctrl->stream_clks_on) {
  1748. drm_dbg_dp(ctrl->drm_dev, "pixel clks already enabled\n");
  1749. } else {
  1750. ret = clk_prepare_enable(ctrl->pixel_clk);
  1751. if (ret) {
  1752. DRM_ERROR("Failed to start pixel clocks. ret=%d\n", ret);
  1753. return ret;
  1754. }
  1755. ctrl->stream_clks_on = true;
  1756. }
  1757. msm_dp_ctrl_send_phy_test_pattern(ctrl);
  1758. return 0;
  1759. }
  1760. void msm_dp_ctrl_handle_sink_request(struct msm_dp_ctrl *msm_dp_ctrl)
  1761. {
  1762. struct msm_dp_ctrl_private *ctrl;
  1763. u32 sink_request = 0x0;
  1764. if (!msm_dp_ctrl) {
  1765. DRM_ERROR("invalid input\n");
  1766. return;
  1767. }
  1768. ctrl = container_of(msm_dp_ctrl, struct msm_dp_ctrl_private, msm_dp_ctrl);
  1769. sink_request = ctrl->link->sink_request;
  1770. if (sink_request & DP_TEST_LINK_PHY_TEST_PATTERN) {
  1771. drm_dbg_dp(ctrl->drm_dev, "PHY_TEST_PATTERN request\n");
  1772. if (msm_dp_ctrl_process_phy_test_request(ctrl)) {
  1773. DRM_ERROR("process phy_test_req failed\n");
  1774. return;
  1775. }
  1776. }
  1777. if (sink_request & DP_LINK_STATUS_UPDATED) {
  1778. if (msm_dp_ctrl_link_maintenance(ctrl)) {
  1779. DRM_ERROR("LM failed: TEST_LINK_TRAINING\n");
  1780. return;
  1781. }
  1782. }
  1783. if (sink_request & DP_TEST_LINK_TRAINING) {
  1784. msm_dp_link_send_test_response(ctrl->link);
  1785. if (msm_dp_ctrl_link_maintenance(ctrl)) {
  1786. DRM_ERROR("LM failed: TEST_LINK_TRAINING\n");
  1787. return;
  1788. }
  1789. }
  1790. }
  1791. static bool msm_dp_ctrl_clock_recovery_any_ok(
  1792. const u8 link_status[DP_LINK_STATUS_SIZE],
  1793. int lane_count)
  1794. {
  1795. int reduced_cnt;
  1796. if (lane_count <= 1)
  1797. return false;
  1798. /*
  1799. * only interested in the lane number after reduced
  1800. * lane_count = 4, then only interested in 2 lanes
  1801. * lane_count = 2, then only interested in 1 lane
  1802. */
  1803. reduced_cnt = lane_count >> 1;
  1804. return drm_dp_clock_recovery_ok(link_status, reduced_cnt);
  1805. }
  1806. static bool msm_dp_ctrl_channel_eq_ok(struct msm_dp_ctrl_private *ctrl)
  1807. {
  1808. u8 link_status[DP_LINK_STATUS_SIZE];
  1809. int num_lanes = ctrl->link->link_params.num_lanes;
  1810. drm_dp_dpcd_read_link_status(ctrl->aux, link_status);
  1811. return drm_dp_channel_eq_ok(link_status, num_lanes);
  1812. }
  1813. int msm_dp_ctrl_on_link(struct msm_dp_ctrl *msm_dp_ctrl)
  1814. {
  1815. int rc = 0;
  1816. struct msm_dp_ctrl_private *ctrl;
  1817. u32 rate;
  1818. int link_train_max_retries = 5;
  1819. u32 const phy_cts_pixel_clk_khz = 148500;
  1820. u8 link_status[DP_LINK_STATUS_SIZE];
  1821. unsigned int training_step;
  1822. unsigned long pixel_rate;
  1823. if (!msm_dp_ctrl)
  1824. return -EINVAL;
  1825. ctrl = container_of(msm_dp_ctrl, struct msm_dp_ctrl_private, msm_dp_ctrl);
  1826. rate = ctrl->panel->link_info.rate;
  1827. pixel_rate = ctrl->panel->msm_dp_mode.drm_mode.clock;
  1828. msm_dp_ctrl_core_clk_enable(&ctrl->msm_dp_ctrl);
  1829. if (ctrl->link->sink_request & DP_TEST_LINK_PHY_TEST_PATTERN) {
  1830. drm_dbg_dp(ctrl->drm_dev,
  1831. "using phy test link parameters\n");
  1832. if (!pixel_rate)
  1833. pixel_rate = phy_cts_pixel_clk_khz;
  1834. } else {
  1835. ctrl->link->link_params.rate = rate;
  1836. ctrl->link->link_params.num_lanes =
  1837. ctrl->panel->link_info.num_lanes;
  1838. if (ctrl->panel->msm_dp_mode.out_fmt_is_yuv_420)
  1839. pixel_rate >>= 1;
  1840. }
  1841. drm_dbg_dp(ctrl->drm_dev, "rate=%d, num_lanes=%d, pixel_rate=%lu\n",
  1842. ctrl->link->link_params.rate, ctrl->link->link_params.num_lanes,
  1843. pixel_rate);
  1844. rc = msm_dp_ctrl_enable_mainlink_clocks(ctrl);
  1845. if (rc)
  1846. return rc;
  1847. while (--link_train_max_retries) {
  1848. training_step = DP_TRAINING_NONE;
  1849. rc = msm_dp_ctrl_setup_main_link(ctrl, &training_step);
  1850. if (rc == 0) {
  1851. /* training completed successfully */
  1852. break;
  1853. } else if (training_step == DP_TRAINING_1) {
  1854. /* link train_1 failed */
  1855. if (!msm_dp_aux_is_link_connected(ctrl->aux))
  1856. break;
  1857. drm_dp_dpcd_read_link_status(ctrl->aux, link_status);
  1858. rc = msm_dp_ctrl_link_rate_down_shift(ctrl);
  1859. if (rc < 0) { /* already in RBR = 1.6G */
  1860. if (msm_dp_ctrl_clock_recovery_any_ok(link_status,
  1861. ctrl->link->link_params.num_lanes)) {
  1862. /*
  1863. * some lanes are ready,
  1864. * reduce lane number
  1865. */
  1866. rc = msm_dp_ctrl_link_lane_down_shift(ctrl);
  1867. if (rc < 0) { /* lane == 1 already */
  1868. /* end with failure */
  1869. break;
  1870. }
  1871. } else {
  1872. /* end with failure */
  1873. break; /* lane == 1 already */
  1874. }
  1875. }
  1876. } else if (training_step == DP_TRAINING_2) {
  1877. /* link train_2 failed */
  1878. if (!msm_dp_aux_is_link_connected(ctrl->aux))
  1879. break;
  1880. drm_dp_dpcd_read_link_status(ctrl->aux, link_status);
  1881. if (!drm_dp_clock_recovery_ok(link_status,
  1882. ctrl->link->link_params.num_lanes))
  1883. rc = msm_dp_ctrl_link_rate_down_shift(ctrl);
  1884. else
  1885. rc = msm_dp_ctrl_link_lane_down_shift(ctrl);
  1886. if (rc < 0) {
  1887. /* end with failure */
  1888. break; /* lane == 1 already */
  1889. }
  1890. /* stop link training before start re training */
  1891. msm_dp_ctrl_clear_training_pattern(ctrl, DP_PHY_DPRX);
  1892. }
  1893. rc = msm_dp_ctrl_reinitialize_mainlink(ctrl);
  1894. if (rc) {
  1895. DRM_ERROR("Failed to reinitialize mainlink. rc=%d\n", rc);
  1896. break;
  1897. }
  1898. }
  1899. if (ctrl->link->sink_request & DP_TEST_LINK_PHY_TEST_PATTERN)
  1900. return rc;
  1901. if (rc == 0) { /* link train successfully */
  1902. /*
  1903. * do not stop train pattern here
  1904. * stop link training at on_stream
  1905. * to pass compliance test
  1906. */
  1907. } else {
  1908. /*
  1909. * link training failed
  1910. * end txing train pattern here
  1911. */
  1912. msm_dp_ctrl_clear_training_pattern(ctrl, DP_PHY_DPRX);
  1913. msm_dp_ctrl_deinitialize_mainlink(ctrl);
  1914. rc = -ECONNRESET;
  1915. }
  1916. return rc;
  1917. }
  1918. static int msm_dp_ctrl_link_retrain(struct msm_dp_ctrl_private *ctrl)
  1919. {
  1920. int training_step = DP_TRAINING_NONE;
  1921. return msm_dp_ctrl_setup_main_link(ctrl, &training_step);
  1922. }
  1923. static void msm_dp_ctrl_config_msa(struct msm_dp_ctrl_private *ctrl,
  1924. u32 rate, u32 stream_rate_khz,
  1925. bool is_ycbcr_420)
  1926. {
  1927. u32 pixel_m, pixel_n;
  1928. u32 mvid, nvid, pixel_div, dispcc_input_rate;
  1929. u32 const nvid_fixed = DP_LINK_CONSTANT_N_VALUE;
  1930. u32 const link_rate_hbr2 = 540000;
  1931. u32 const link_rate_hbr3 = 810000;
  1932. unsigned long den, num;
  1933. switch (rate) {
  1934. case link_rate_hbr3:
  1935. pixel_div = 6;
  1936. break;
  1937. case link_rate_hbr2:
  1938. pixel_div = 4;
  1939. break;
  1940. case 162000:
  1941. case 270000:
  1942. pixel_div = 2;
  1943. break;
  1944. default:
  1945. /*
  1946. * This cannot be reached but the compiler is not able to know
  1947. * that statically so return early to avoid a possibly invalid
  1948. * division.
  1949. */
  1950. DRM_ERROR("Invalid pixel mux divider\n");
  1951. return;
  1952. }
  1953. dispcc_input_rate = (rate * 10) / pixel_div;
  1954. rational_best_approximation(dispcc_input_rate, stream_rate_khz,
  1955. (unsigned long)(1 << 16) - 1,
  1956. (unsigned long)(1 << 16) - 1, &den, &num);
  1957. den = ~(den - num);
  1958. den = den & 0xFFFF;
  1959. pixel_m = num;
  1960. pixel_n = den;
  1961. mvid = (pixel_m & 0xFFFF) * 5;
  1962. nvid = (0xFFFF & (~pixel_n)) + (pixel_m & 0xFFFF);
  1963. if (nvid < nvid_fixed) {
  1964. u32 temp;
  1965. temp = (nvid_fixed / nvid) * nvid;
  1966. mvid = (nvid_fixed / nvid) * mvid;
  1967. nvid = temp;
  1968. }
  1969. if (is_ycbcr_420)
  1970. mvid /= 2;
  1971. if (link_rate_hbr2 == rate)
  1972. nvid *= 2;
  1973. if (link_rate_hbr3 == rate)
  1974. nvid *= 3;
  1975. drm_dbg_dp(ctrl->drm_dev, "mvid=0x%x, nvid=0x%x\n", mvid, nvid);
  1976. msm_dp_write_link(ctrl, REG_DP_SOFTWARE_MVID, mvid);
  1977. msm_dp_write_link(ctrl, REG_DP_SOFTWARE_NVID, nvid);
  1978. }
  1979. int msm_dp_ctrl_on_stream(struct msm_dp_ctrl *msm_dp_ctrl, bool force_link_train)
  1980. {
  1981. int ret = 0;
  1982. bool mainlink_ready = false;
  1983. struct msm_dp_ctrl_private *ctrl;
  1984. unsigned long pixel_rate;
  1985. unsigned long pixel_rate_orig;
  1986. if (!msm_dp_ctrl)
  1987. return -EINVAL;
  1988. ctrl = container_of(msm_dp_ctrl, struct msm_dp_ctrl_private, msm_dp_ctrl);
  1989. pixel_rate = pixel_rate_orig = ctrl->panel->msm_dp_mode.drm_mode.clock;
  1990. if (msm_dp_ctrl->wide_bus_en || ctrl->panel->msm_dp_mode.out_fmt_is_yuv_420)
  1991. pixel_rate >>= 1;
  1992. drm_dbg_dp(ctrl->drm_dev, "rate=%d, num_lanes=%d, pixel_rate=%lu\n",
  1993. ctrl->link->link_params.rate,
  1994. ctrl->link->link_params.num_lanes, pixel_rate);
  1995. drm_dbg_dp(ctrl->drm_dev,
  1996. "core_clk_on=%d link_clk_on=%d stream_clk_on=%d\n",
  1997. ctrl->core_clks_on, ctrl->link_clks_on, ctrl->stream_clks_on);
  1998. if (!ctrl->link_clks_on) { /* link clk is off */
  1999. ret = msm_dp_ctrl_enable_mainlink_clocks(ctrl);
  2000. if (ret) {
  2001. DRM_ERROR("Failed to start link clocks. ret=%d\n", ret);
  2002. goto end;
  2003. }
  2004. }
  2005. ret = clk_set_rate(ctrl->pixel_clk, pixel_rate * 1000);
  2006. if (ret) {
  2007. DRM_ERROR("Failed to set pixel clock rate. ret=%d\n", ret);
  2008. goto end;
  2009. }
  2010. if (ctrl->stream_clks_on) {
  2011. drm_dbg_dp(ctrl->drm_dev, "pixel clks already enabled\n");
  2012. } else {
  2013. ret = clk_prepare_enable(ctrl->pixel_clk);
  2014. if (ret) {
  2015. DRM_ERROR("Failed to start pixel clocks. ret=%d\n", ret);
  2016. goto end;
  2017. }
  2018. ctrl->stream_clks_on = true;
  2019. }
  2020. if (force_link_train || !msm_dp_ctrl_channel_eq_ok(ctrl))
  2021. msm_dp_ctrl_link_retrain(ctrl);
  2022. /* stop txing train pattern to end link training */
  2023. msm_dp_ctrl_clear_training_pattern(ctrl, DP_PHY_DPRX);
  2024. /*
  2025. * Set up transfer unit values and set controller state to send
  2026. * video.
  2027. */
  2028. reinit_completion(&ctrl->video_comp);
  2029. msm_dp_ctrl_configure_source_params(ctrl);
  2030. msm_dp_ctrl_config_msa(ctrl,
  2031. ctrl->link->link_params.rate,
  2032. pixel_rate_orig,
  2033. ctrl->panel->msm_dp_mode.out_fmt_is_yuv_420);
  2034. msm_dp_panel_clear_dsc_dto(ctrl->panel);
  2035. msm_dp_ctrl_setup_tr_unit(ctrl);
  2036. msm_dp_write_link(ctrl, REG_DP_STATE_CTRL, DP_STATE_CTRL_SEND_VIDEO);
  2037. ret = msm_dp_ctrl_wait4video_ready(ctrl);
  2038. if (ret)
  2039. return ret;
  2040. mainlink_ready = msm_dp_ctrl_mainlink_ready(ctrl);
  2041. drm_dbg_dp(ctrl->drm_dev,
  2042. "mainlink %s\n", mainlink_ready ? "READY" : "NOT READY");
  2043. end:
  2044. return ret;
  2045. }
  2046. void msm_dp_ctrl_off_link_stream(struct msm_dp_ctrl *msm_dp_ctrl)
  2047. {
  2048. struct msm_dp_ctrl_private *ctrl;
  2049. struct phy *phy;
  2050. ctrl = container_of(msm_dp_ctrl, struct msm_dp_ctrl_private, msm_dp_ctrl);
  2051. phy = ctrl->phy;
  2052. msm_dp_panel_disable_vsc_sdp(ctrl->panel);
  2053. /* set dongle to D3 (power off) mode */
  2054. msm_dp_link_psm_config(ctrl->link, &ctrl->panel->link_info, true);
  2055. msm_dp_ctrl_mainlink_disable(ctrl);
  2056. if (ctrl->stream_clks_on) {
  2057. clk_disable_unprepare(ctrl->pixel_clk);
  2058. ctrl->stream_clks_on = false;
  2059. }
  2060. dev_pm_opp_set_rate(ctrl->dev, 0);
  2061. msm_dp_ctrl_link_clk_disable(&ctrl->msm_dp_ctrl);
  2062. phy_power_off(phy);
  2063. /* aux channel down, reinit phy */
  2064. phy_exit(phy);
  2065. phy_init(phy);
  2066. drm_dbg_dp(ctrl->drm_dev, "phy=%p init=%d power_on=%d\n",
  2067. phy, phy->init_count, phy->power_count);
  2068. }
  2069. void msm_dp_ctrl_off_link(struct msm_dp_ctrl *msm_dp_ctrl)
  2070. {
  2071. struct msm_dp_ctrl_private *ctrl;
  2072. struct phy *phy;
  2073. ctrl = container_of(msm_dp_ctrl, struct msm_dp_ctrl_private, msm_dp_ctrl);
  2074. phy = ctrl->phy;
  2075. msm_dp_ctrl_mainlink_disable(ctrl);
  2076. dev_pm_opp_set_rate(ctrl->dev, 0);
  2077. msm_dp_ctrl_link_clk_disable(&ctrl->msm_dp_ctrl);
  2078. DRM_DEBUG_DP("Before, phy=%p init_count=%d power_on=%d\n",
  2079. phy, phy->init_count, phy->power_count);
  2080. phy_power_off(phy);
  2081. DRM_DEBUG_DP("After, phy=%p init_count=%d power_on=%d\n",
  2082. phy, phy->init_count, phy->power_count);
  2083. }
  2084. void msm_dp_ctrl_off(struct msm_dp_ctrl *msm_dp_ctrl)
  2085. {
  2086. struct msm_dp_ctrl_private *ctrl;
  2087. struct phy *phy;
  2088. ctrl = container_of(msm_dp_ctrl, struct msm_dp_ctrl_private, msm_dp_ctrl);
  2089. phy = ctrl->phy;
  2090. msm_dp_panel_disable_vsc_sdp(ctrl->panel);
  2091. msm_dp_ctrl_mainlink_disable(ctrl);
  2092. msm_dp_ctrl_reset(&ctrl->msm_dp_ctrl);
  2093. if (ctrl->stream_clks_on) {
  2094. clk_disable_unprepare(ctrl->pixel_clk);
  2095. ctrl->stream_clks_on = false;
  2096. }
  2097. dev_pm_opp_set_rate(ctrl->dev, 0);
  2098. msm_dp_ctrl_link_clk_disable(&ctrl->msm_dp_ctrl);
  2099. phy_power_off(phy);
  2100. drm_dbg_dp(ctrl->drm_dev, "phy=%p init=%d power_on=%d\n",
  2101. phy, phy->init_count, phy->power_count);
  2102. }
  2103. irqreturn_t msm_dp_ctrl_isr(struct msm_dp_ctrl *msm_dp_ctrl)
  2104. {
  2105. struct msm_dp_ctrl_private *ctrl;
  2106. u32 isr;
  2107. irqreturn_t ret = IRQ_NONE;
  2108. if (!msm_dp_ctrl)
  2109. return IRQ_NONE;
  2110. ctrl = container_of(msm_dp_ctrl, struct msm_dp_ctrl_private, msm_dp_ctrl);
  2111. if (ctrl->panel->psr_cap.version) {
  2112. isr = msm_dp_ctrl_get_psr_interrupt(ctrl);
  2113. if (isr)
  2114. complete(&ctrl->psr_op_comp);
  2115. if (isr & PSR_EXIT_INT)
  2116. drm_dbg_dp(ctrl->drm_dev, "PSR exit done\n");
  2117. if (isr & PSR_UPDATE_INT)
  2118. drm_dbg_dp(ctrl->drm_dev, "PSR frame update done\n");
  2119. if (isr & PSR_CAPTURE_INT)
  2120. drm_dbg_dp(ctrl->drm_dev, "PSR frame capture done\n");
  2121. }
  2122. isr = msm_dp_ctrl_get_interrupt(ctrl);
  2123. if (isr & DP_CTRL_INTR_READY_FOR_VIDEO) {
  2124. drm_dbg_dp(ctrl->drm_dev, "dp_video_ready\n");
  2125. complete(&ctrl->video_comp);
  2126. ret = IRQ_HANDLED;
  2127. }
  2128. if (isr & DP_CTRL_INTR_IDLE_PATTERN_SENT) {
  2129. drm_dbg_dp(ctrl->drm_dev, "idle_patterns_sent\n");
  2130. complete(&ctrl->idle_comp);
  2131. ret = IRQ_HANDLED;
  2132. }
  2133. /* DP aux isr */
  2134. isr = msm_dp_ctrl_get_aux_interrupt(ctrl);
  2135. if (isr)
  2136. ret |= msm_dp_aux_isr(ctrl->aux, isr);
  2137. return ret;
  2138. }
  2139. static const char *core_clks[] = {
  2140. "core_iface",
  2141. "core_aux",
  2142. };
  2143. static const char *ctrl_clks[] = {
  2144. "ctrl_link",
  2145. "ctrl_link_iface",
  2146. };
  2147. static int msm_dp_ctrl_clk_init(struct msm_dp_ctrl *msm_dp_ctrl)
  2148. {
  2149. struct msm_dp_ctrl_private *ctrl;
  2150. struct device *dev;
  2151. int i, rc;
  2152. ctrl = container_of(msm_dp_ctrl, struct msm_dp_ctrl_private, msm_dp_ctrl);
  2153. dev = ctrl->dev;
  2154. ctrl->num_core_clks = ARRAY_SIZE(core_clks);
  2155. ctrl->core_clks = devm_kcalloc(dev, ctrl->num_core_clks, sizeof(*ctrl->core_clks), GFP_KERNEL);
  2156. if (!ctrl->core_clks)
  2157. return -ENOMEM;
  2158. for (i = 0; i < ctrl->num_core_clks; i++)
  2159. ctrl->core_clks[i].id = core_clks[i];
  2160. rc = devm_clk_bulk_get(dev, ctrl->num_core_clks, ctrl->core_clks);
  2161. if (rc)
  2162. return rc;
  2163. ctrl->num_link_clks = ARRAY_SIZE(ctrl_clks);
  2164. ctrl->link_clks = devm_kcalloc(dev, ctrl->num_link_clks, sizeof(*ctrl->link_clks), GFP_KERNEL);
  2165. if (!ctrl->link_clks)
  2166. return -ENOMEM;
  2167. for (i = 0; i < ctrl->num_link_clks; i++)
  2168. ctrl->link_clks[i].id = ctrl_clks[i];
  2169. rc = devm_clk_bulk_get(dev, ctrl->num_link_clks, ctrl->link_clks);
  2170. if (rc)
  2171. return rc;
  2172. ctrl->pixel_clk = devm_clk_get(dev, "stream_pixel");
  2173. if (IS_ERR(ctrl->pixel_clk))
  2174. return PTR_ERR(ctrl->pixel_clk);
  2175. return 0;
  2176. }
  2177. struct msm_dp_ctrl *msm_dp_ctrl_get(struct device *dev, struct msm_dp_link *link,
  2178. struct msm_dp_panel *panel, struct drm_dp_aux *aux,
  2179. struct phy *phy,
  2180. void __iomem *ahb_base,
  2181. void __iomem *link_base)
  2182. {
  2183. struct msm_dp_ctrl_private *ctrl;
  2184. int ret;
  2185. if (!dev || !panel || !aux || !link) {
  2186. DRM_ERROR("invalid input\n");
  2187. return ERR_PTR(-EINVAL);
  2188. }
  2189. ctrl = devm_kzalloc(dev, sizeof(*ctrl), GFP_KERNEL);
  2190. if (!ctrl) {
  2191. DRM_ERROR("Mem allocation failure\n");
  2192. return ERR_PTR(-ENOMEM);
  2193. }
  2194. ret = devm_pm_opp_set_clkname(dev, "ctrl_link");
  2195. if (ret) {
  2196. dev_err(dev, "invalid DP OPP table in device tree\n");
  2197. /* caller do PTR_ERR(opp_table) */
  2198. return (struct msm_dp_ctrl *)ERR_PTR(ret);
  2199. }
  2200. /* OPP table is optional */
  2201. ret = devm_pm_opp_of_add_table(dev);
  2202. if (ret)
  2203. dev_err(dev, "failed to add DP OPP table\n");
  2204. init_completion(&ctrl->idle_comp);
  2205. init_completion(&ctrl->psr_op_comp);
  2206. init_completion(&ctrl->video_comp);
  2207. /* in parameters */
  2208. ctrl->panel = panel;
  2209. ctrl->aux = aux;
  2210. ctrl->link = link;
  2211. ctrl->dev = dev;
  2212. ctrl->phy = phy;
  2213. ctrl->ahb_base = ahb_base;
  2214. ctrl->link_base = link_base;
  2215. ret = msm_dp_ctrl_clk_init(&ctrl->msm_dp_ctrl);
  2216. if (ret) {
  2217. dev_err(dev, "failed to init clocks\n");
  2218. return ERR_PTR(ret);
  2219. }
  2220. return &ctrl->msm_dp_ctrl;
  2221. }